Comment typo fixes.
[binutils.git] / bfd / elf.c
blob45c7d753cb3d334984828c818c48a8e1b8f88570
1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 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 INLINE struct elf_segment_map *make_mapping
44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, bfd_boolean));
45 static bfd_boolean map_sections_to_segments
46 PARAMS ((bfd *));
47 static int elf_sort_sections
48 PARAMS ((const PTR, const PTR));
49 static bfd_boolean assign_file_positions_for_segments
50 PARAMS ((bfd *));
51 static bfd_boolean assign_file_positions_except_relocs
52 PARAMS ((bfd *));
53 static bfd_boolean prep_headers
54 PARAMS ((bfd *));
55 static bfd_boolean swap_out_syms
56 PARAMS ((bfd *, struct bfd_strtab_hash **, int));
57 static bfd_boolean copy_private_bfd_data
58 PARAMS ((bfd *, bfd *));
59 static char *elf_read
60 PARAMS ((bfd *, file_ptr, bfd_size_type));
61 static const char *group_signature
62 PARAMS ((bfd *, Elf_Internal_Shdr *));
63 static bfd_boolean setup_group
64 PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
65 static void merge_sections_remove_hook
66 PARAMS ((bfd *, asection *));
67 static void elf_fake_sections
68 PARAMS ((bfd *, asection *, PTR));
69 static bfd_boolean assign_section_numbers
70 PARAMS ((bfd *));
71 static INLINE int sym_is_global
72 PARAMS ((bfd *, asymbol *));
73 static bfd_boolean elf_map_symbols
74 PARAMS ((bfd *));
75 static bfd_size_type get_program_header_size
76 PARAMS ((bfd *));
77 static bfd_boolean elfcore_read_notes
78 PARAMS ((bfd *, file_ptr, bfd_size_type));
79 static bfd_boolean elf_find_function
80 PARAMS ((bfd *, asection *, asymbol **, bfd_vma, const char **,
81 const char **));
82 static int elfcore_make_pid
83 PARAMS ((bfd *));
84 static bfd_boolean elfcore_maybe_make_sect
85 PARAMS ((bfd *, char *, asection *));
86 static bfd_boolean elfcore_make_note_pseudosection
87 PARAMS ((bfd *, char *, Elf_Internal_Note *));
88 static bfd_boolean elfcore_grok_prfpreg
89 PARAMS ((bfd *, Elf_Internal_Note *));
90 static bfd_boolean elfcore_grok_prxfpreg
91 PARAMS ((bfd *, Elf_Internal_Note *));
92 static bfd_boolean elfcore_grok_note
93 PARAMS ((bfd *, Elf_Internal_Note *));
94 static bfd_boolean elfcore_netbsd_get_lwpid
95 PARAMS ((Elf_Internal_Note *, int *));
96 static bfd_boolean elfcore_grok_netbsd_procinfo
97 PARAMS ((bfd *, Elf_Internal_Note *));
98 static bfd_boolean elfcore_grok_netbsd_note
99 PARAMS ((bfd *, Elf_Internal_Note *));
101 /* Swap version information in and out. The version information is
102 currently size independent. If that ever changes, this code will
103 need to move into elfcode.h. */
105 /* Swap in a Verdef structure. */
107 void
108 _bfd_elf_swap_verdef_in (abfd, src, dst)
109 bfd *abfd;
110 const Elf_External_Verdef *src;
111 Elf_Internal_Verdef *dst;
113 dst->vd_version = H_GET_16 (abfd, src->vd_version);
114 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
115 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
116 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
117 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
118 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
119 dst->vd_next = H_GET_32 (abfd, src->vd_next);
122 /* Swap out a Verdef structure. */
124 void
125 _bfd_elf_swap_verdef_out (abfd, src, dst)
126 bfd *abfd;
127 const Elf_Internal_Verdef *src;
128 Elf_External_Verdef *dst;
130 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
131 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
132 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
133 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
134 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
135 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
136 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
139 /* Swap in a Verdaux structure. */
141 void
142 _bfd_elf_swap_verdaux_in (abfd, src, dst)
143 bfd *abfd;
144 const Elf_External_Verdaux *src;
145 Elf_Internal_Verdaux *dst;
147 dst->vda_name = H_GET_32 (abfd, src->vda_name);
148 dst->vda_next = H_GET_32 (abfd, src->vda_next);
151 /* Swap out a Verdaux structure. */
153 void
154 _bfd_elf_swap_verdaux_out (abfd, src, dst)
155 bfd *abfd;
156 const Elf_Internal_Verdaux *src;
157 Elf_External_Verdaux *dst;
159 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
160 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
163 /* Swap in a Verneed structure. */
165 void
166 _bfd_elf_swap_verneed_in (abfd, src, dst)
167 bfd *abfd;
168 const Elf_External_Verneed *src;
169 Elf_Internal_Verneed *dst;
171 dst->vn_version = H_GET_16 (abfd, src->vn_version);
172 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
173 dst->vn_file = H_GET_32 (abfd, src->vn_file);
174 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
175 dst->vn_next = H_GET_32 (abfd, src->vn_next);
178 /* Swap out a Verneed structure. */
180 void
181 _bfd_elf_swap_verneed_out (abfd, src, dst)
182 bfd *abfd;
183 const Elf_Internal_Verneed *src;
184 Elf_External_Verneed *dst;
186 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
187 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
188 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
189 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
190 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
193 /* Swap in a Vernaux structure. */
195 void
196 _bfd_elf_swap_vernaux_in (abfd, src, dst)
197 bfd *abfd;
198 const Elf_External_Vernaux *src;
199 Elf_Internal_Vernaux *dst;
201 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
202 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
203 dst->vna_other = H_GET_16 (abfd, src->vna_other);
204 dst->vna_name = H_GET_32 (abfd, src->vna_name);
205 dst->vna_next = H_GET_32 (abfd, src->vna_next);
208 /* Swap out a Vernaux structure. */
210 void
211 _bfd_elf_swap_vernaux_out (abfd, src, dst)
212 bfd *abfd;
213 const Elf_Internal_Vernaux *src;
214 Elf_External_Vernaux *dst;
216 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
217 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
218 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
219 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
220 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
223 /* Swap in a Versym structure. */
225 void
226 _bfd_elf_swap_versym_in (abfd, src, dst)
227 bfd *abfd;
228 const Elf_External_Versym *src;
229 Elf_Internal_Versym *dst;
231 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
234 /* Swap out a Versym structure. */
236 void
237 _bfd_elf_swap_versym_out (abfd, src, dst)
238 bfd *abfd;
239 const Elf_Internal_Versym *src;
240 Elf_External_Versym *dst;
242 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
245 /* Standard ELF hash function. Do not change this function; you will
246 cause invalid hash tables to be generated. */
248 unsigned long
249 bfd_elf_hash (namearg)
250 const char *namearg;
252 const unsigned char *name = (const unsigned char *) namearg;
253 unsigned long h = 0;
254 unsigned long g;
255 int ch;
257 while ((ch = *name++) != '\0')
259 h = (h << 4) + ch;
260 if ((g = (h & 0xf0000000)) != 0)
262 h ^= g >> 24;
263 /* The ELF ABI says `h &= ~g', but this is equivalent in
264 this case and on some machines one insn instead of two. */
265 h ^= g;
268 return h;
271 /* Read a specified number of bytes at a specified offset in an ELF
272 file, into a newly allocated buffer, and return a pointer to the
273 buffer. */
275 static char *
276 elf_read (abfd, offset, size)
277 bfd *abfd;
278 file_ptr offset;
279 bfd_size_type size;
281 char *buf;
283 if ((buf = bfd_alloc (abfd, size)) == NULL)
284 return NULL;
285 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
286 return NULL;
287 if (bfd_bread ((PTR) buf, size, abfd) != size)
289 if (bfd_get_error () != bfd_error_system_call)
290 bfd_set_error (bfd_error_file_truncated);
291 return NULL;
293 return buf;
296 bfd_boolean
297 bfd_elf_mkobject (abfd)
298 bfd *abfd;
300 /* This just does initialization. */
301 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
302 bfd_size_type amt = sizeof (struct elf_obj_tdata);
303 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
304 if (elf_tdata (abfd) == 0)
305 return FALSE;
306 /* Since everything is done at close time, do we need any
307 initialization? */
309 return TRUE;
312 bfd_boolean
313 bfd_elf_mkcorefile (abfd)
314 bfd *abfd;
316 /* I think this can be done just like an object file. */
317 return bfd_elf_mkobject (abfd);
320 char *
321 bfd_elf_get_str_section (abfd, shindex)
322 bfd *abfd;
323 unsigned int shindex;
325 Elf_Internal_Shdr **i_shdrp;
326 char *shstrtab = NULL;
327 file_ptr offset;
328 bfd_size_type shstrtabsize;
330 i_shdrp = elf_elfsections (abfd);
331 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
332 return 0;
334 shstrtab = (char *) i_shdrp[shindex]->contents;
335 if (shstrtab == NULL)
337 /* No cached one, attempt to read, and cache what we read. */
338 offset = i_shdrp[shindex]->sh_offset;
339 shstrtabsize = i_shdrp[shindex]->sh_size;
340 shstrtab = elf_read (abfd, offset, shstrtabsize);
341 i_shdrp[shindex]->contents = (PTR) shstrtab;
343 return shstrtab;
346 char *
347 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
348 bfd *abfd;
349 unsigned int shindex;
350 unsigned int strindex;
352 Elf_Internal_Shdr *hdr;
354 if (strindex == 0)
355 return "";
357 hdr = elf_elfsections (abfd)[shindex];
359 if (hdr->contents == NULL
360 && bfd_elf_get_str_section (abfd, shindex) == NULL)
361 return NULL;
363 if (strindex >= hdr->sh_size)
365 (*_bfd_error_handler)
366 (_("%s: invalid string offset %u >= %lu for section `%s'"),
367 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
368 ((shindex == elf_elfheader(abfd)->e_shstrndx
369 && strindex == hdr->sh_name)
370 ? ".shstrtab"
371 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
372 return "";
375 return ((char *) hdr->contents) + strindex;
378 /* Read and convert symbols to internal format.
379 SYMCOUNT specifies the number of symbols to read, starting from
380 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF
381 are non-NULL, they are used to store the internal symbols, external
382 symbols, and symbol section index extensions, respectively. */
384 Elf_Internal_Sym *
385 bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset,
386 intsym_buf, extsym_buf, extshndx_buf)
387 bfd *ibfd;
388 Elf_Internal_Shdr *symtab_hdr;
389 size_t symcount;
390 size_t symoffset;
391 Elf_Internal_Sym *intsym_buf;
392 PTR extsym_buf;
393 Elf_External_Sym_Shndx *extshndx_buf;
395 Elf_Internal_Shdr *shndx_hdr;
396 PTR alloc_ext;
397 const bfd_byte *esym;
398 Elf_External_Sym_Shndx *alloc_extshndx;
399 Elf_External_Sym_Shndx *shndx;
400 Elf_Internal_Sym *isym;
401 Elf_Internal_Sym *isymend;
402 struct elf_backend_data *bed;
403 size_t extsym_size;
404 bfd_size_type amt;
405 file_ptr pos;
407 if (symcount == 0)
408 return intsym_buf;
410 /* Normal syms might have section extension entries. */
411 shndx_hdr = NULL;
412 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr)
413 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr;
415 /* Read the symbols. */
416 alloc_ext = NULL;
417 alloc_extshndx = NULL;
418 bed = get_elf_backend_data (ibfd);
419 extsym_size = bed->s->sizeof_sym;
420 amt = symcount * extsym_size;
421 pos = symtab_hdr->sh_offset + symoffset * extsym_size;
422 if (extsym_buf == NULL)
424 alloc_ext = bfd_malloc (amt);
425 extsym_buf = alloc_ext;
427 if (extsym_buf == NULL
428 || bfd_seek (ibfd, pos, SEEK_SET) != 0
429 || bfd_bread (extsym_buf, amt, ibfd) != amt)
431 intsym_buf = NULL;
432 goto out;
435 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0)
436 extshndx_buf = NULL;
437 else
439 amt = symcount * sizeof (Elf_External_Sym_Shndx);
440 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx);
441 if (extshndx_buf == NULL)
443 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
444 extshndx_buf = alloc_extshndx;
446 if (extshndx_buf == NULL
447 || bfd_seek (ibfd, pos, SEEK_SET) != 0
448 || bfd_bread (extshndx_buf, amt, ibfd) != amt)
450 intsym_buf = NULL;
451 goto out;
455 if (intsym_buf == NULL)
457 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym);
458 intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt);
459 if (intsym_buf == NULL)
460 goto out;
463 /* Convert the symbols to internal form. */
464 isymend = intsym_buf + symcount;
465 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf;
466 isym < isymend;
467 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL)
468 (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym);
470 out:
471 if (alloc_ext != NULL)
472 free (alloc_ext);
473 if (alloc_extshndx != NULL)
474 free (alloc_extshndx);
476 return intsym_buf;
479 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
480 sections. The first element is the flags, the rest are section
481 pointers. */
483 typedef union elf_internal_group {
484 Elf_Internal_Shdr *shdr;
485 unsigned int flags;
486 } Elf_Internal_Group;
488 /* Return the name of the group signature symbol. Why isn't the
489 signature just a string? */
491 static const char *
492 group_signature (abfd, ghdr)
493 bfd *abfd;
494 Elf_Internal_Shdr *ghdr;
496 Elf_Internal_Shdr *hdr;
497 unsigned char esym[sizeof (Elf64_External_Sym)];
498 Elf_External_Sym_Shndx eshndx;
499 Elf_Internal_Sym isym;
500 unsigned int iname;
501 unsigned int shindex;
503 /* First we need to ensure the symbol table is available. */
504 if (! bfd_section_from_shdr (abfd, ghdr->sh_link))
505 return NULL;
507 /* Go read the symbol. */
508 hdr = &elf_tdata (abfd)->symtab_hdr;
509 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info,
510 &isym, esym, &eshndx) == NULL)
511 return NULL;
513 /* Look up the symbol name. */
514 iname = isym.st_name;
515 shindex = hdr->sh_link;
516 if (iname == 0 && ELF_ST_TYPE (isym.st_info) == STT_SECTION)
518 iname = elf_elfsections (abfd)[isym.st_shndx]->sh_name;
519 shindex = elf_elfheader (abfd)->e_shstrndx;
522 return bfd_elf_string_from_elf_section (abfd, shindex, iname);
525 /* Set next_in_group list pointer, and group name for NEWSECT. */
527 static bfd_boolean
528 setup_group (abfd, hdr, newsect)
529 bfd *abfd;
530 Elf_Internal_Shdr *hdr;
531 asection *newsect;
533 unsigned int num_group = elf_tdata (abfd)->num_group;
535 /* If num_group is zero, read in all SHT_GROUP sections. The count
536 is set to -1 if there are no SHT_GROUP sections. */
537 if (num_group == 0)
539 unsigned int i, shnum;
541 /* First count the number of groups. If we have a SHT_GROUP
542 section with just a flag word (ie. sh_size is 4), ignore it. */
543 shnum = elf_numsections (abfd);
544 num_group = 0;
545 for (i = 0; i < shnum; i++)
547 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
548 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
549 num_group += 1;
552 if (num_group == 0)
553 num_group = (unsigned) -1;
554 elf_tdata (abfd)->num_group = num_group;
556 if (num_group > 0)
558 /* We keep a list of elf section headers for group sections,
559 so we can find them quickly. */
560 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
561 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
562 if (elf_tdata (abfd)->group_sect_ptr == NULL)
563 return FALSE;
565 num_group = 0;
566 for (i = 0; i < shnum; i++)
568 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
569 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
571 unsigned char *src;
572 Elf_Internal_Group *dest;
574 /* Add to list of sections. */
575 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
576 num_group += 1;
578 /* Read the raw contents. */
579 BFD_ASSERT (sizeof (*dest) >= 4);
580 amt = shdr->sh_size * sizeof (*dest) / 4;
581 shdr->contents = bfd_alloc (abfd, amt);
582 if (shdr->contents == NULL
583 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
584 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
585 != shdr->sh_size))
586 return FALSE;
588 /* Translate raw contents, a flag word followed by an
589 array of elf section indices all in target byte order,
590 to the flag word followed by an array of elf section
591 pointers. */
592 src = shdr->contents + shdr->sh_size;
593 dest = (Elf_Internal_Group *) (shdr->contents + amt);
594 while (1)
596 unsigned int idx;
598 src -= 4;
599 --dest;
600 idx = H_GET_32 (abfd, src);
601 if (src == shdr->contents)
603 dest->flags = idx;
604 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT))
605 shdr->bfd_section->flags
606 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
607 break;
609 if (idx >= shnum)
611 ((*_bfd_error_handler)
612 (_("%s: invalid SHT_GROUP entry"),
613 bfd_archive_filename (abfd)));
614 idx = 0;
616 dest->shdr = elf_elfsections (abfd)[idx];
623 if (num_group != (unsigned) -1)
625 unsigned int i;
627 for (i = 0; i < num_group; i++)
629 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
630 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
631 unsigned int n_elt = shdr->sh_size / 4;
633 /* Look through this group's sections to see if current
634 section is a member. */
635 while (--n_elt != 0)
636 if ((++idx)->shdr == hdr)
638 asection *s = NULL;
640 /* We are a member of this group. Go looking through
641 other members to see if any others are linked via
642 next_in_group. */
643 idx = (Elf_Internal_Group *) shdr->contents;
644 n_elt = shdr->sh_size / 4;
645 while (--n_elt != 0)
646 if ((s = (++idx)->shdr->bfd_section) != NULL
647 && elf_next_in_group (s) != NULL)
648 break;
649 if (n_elt != 0)
651 /* Snarf the group name from other member, and
652 insert current section in circular list. */
653 elf_group_name (newsect) = elf_group_name (s);
654 elf_next_in_group (newsect) = elf_next_in_group (s);
655 elf_next_in_group (s) = newsect;
657 else
659 const char *gname;
661 gname = group_signature (abfd, shdr);
662 if (gname == NULL)
663 return FALSE;
664 elf_group_name (newsect) = gname;
666 /* Start a circular list with one element. */
667 elf_next_in_group (newsect) = newsect;
670 /* If the group section has been created, point to the
671 new member. */
672 if (shdr->bfd_section != NULL)
673 elf_next_in_group (shdr->bfd_section) = newsect;
675 i = num_group - 1;
676 break;
681 if (elf_group_name (newsect) == NULL)
683 (*_bfd_error_handler) (_("%s: no group info for section %s"),
684 bfd_archive_filename (abfd), newsect->name);
686 return TRUE;
689 bfd_boolean
690 bfd_elf_discard_group (abfd, group)
691 bfd *abfd ATTRIBUTE_UNUSED;
692 asection *group;
694 asection *first = elf_next_in_group (group);
695 asection *s = first;
697 while (s != NULL)
699 s->output_section = bfd_abs_section_ptr;
700 s = elf_next_in_group (s);
701 /* These lists are circular. */
702 if (s == first)
703 break;
705 return TRUE;
708 /* Make a BFD section from an ELF section. We store a pointer to the
709 BFD section in the bfd_section field of the header. */
711 bfd_boolean
712 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
713 bfd *abfd;
714 Elf_Internal_Shdr *hdr;
715 const char *name;
717 asection *newsect;
718 flagword flags;
719 struct elf_backend_data *bed;
721 if (hdr->bfd_section != NULL)
723 BFD_ASSERT (strcmp (name,
724 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
725 return TRUE;
728 newsect = bfd_make_section_anyway (abfd, name);
729 if (newsect == NULL)
730 return FALSE;
732 newsect->filepos = hdr->sh_offset;
734 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
735 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
736 || ! bfd_set_section_alignment (abfd, newsect,
737 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
738 return FALSE;
740 flags = SEC_NO_FLAGS;
741 if (hdr->sh_type != SHT_NOBITS)
742 flags |= SEC_HAS_CONTENTS;
743 if (hdr->sh_type == SHT_GROUP)
744 flags |= SEC_GROUP | SEC_EXCLUDE;
745 if ((hdr->sh_flags & SHF_ALLOC) != 0)
747 flags |= SEC_ALLOC;
748 if (hdr->sh_type != SHT_NOBITS)
749 flags |= SEC_LOAD;
751 if ((hdr->sh_flags & SHF_WRITE) == 0)
752 flags |= SEC_READONLY;
753 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
754 flags |= SEC_CODE;
755 else if ((flags & SEC_LOAD) != 0)
756 flags |= SEC_DATA;
757 if ((hdr->sh_flags & SHF_MERGE) != 0)
759 flags |= SEC_MERGE;
760 newsect->entsize = hdr->sh_entsize;
761 if ((hdr->sh_flags & SHF_STRINGS) != 0)
762 flags |= SEC_STRINGS;
764 if (hdr->sh_flags & SHF_GROUP)
765 if (!setup_group (abfd, hdr, newsect))
766 return FALSE;
767 if ((hdr->sh_flags & SHF_TLS) != 0)
768 flags |= SEC_THREAD_LOCAL;
770 /* The debugging sections appear to be recognized only by name, not
771 any sort of flag. */
773 static const char *debug_sec_names [] =
775 ".debug",
776 ".gnu.linkonce.wi.",
777 ".line",
778 ".stab"
780 int i;
782 for (i = ARRAY_SIZE (debug_sec_names); i--;)
783 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
784 break;
786 if (i >= 0)
787 flags |= SEC_DEBUGGING;
790 /* As a GNU extension, if the name begins with .gnu.linkonce, we
791 only link a single copy of the section. This is used to support
792 g++. g++ will emit each template expansion in its own section.
793 The symbols will be defined as weak, so that multiple definitions
794 are permitted. The GNU linker extension is to actually discard
795 all but one of the sections. */
796 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0
797 && elf_next_in_group (newsect) == NULL)
798 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
800 bed = get_elf_backend_data (abfd);
801 if (bed->elf_backend_section_flags)
802 if (! bed->elf_backend_section_flags (&flags, hdr))
803 return FALSE;
805 if (! bfd_set_section_flags (abfd, newsect, flags))
806 return FALSE;
808 if ((flags & SEC_ALLOC) != 0)
810 Elf_Internal_Phdr *phdr;
811 unsigned int i;
813 /* Look through the phdrs to see if we need to adjust the lma.
814 If all the p_paddr fields are zero, we ignore them, since
815 some ELF linkers produce such output. */
816 phdr = elf_tdata (abfd)->phdr;
817 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
819 if (phdr->p_paddr != 0)
820 break;
822 if (i < elf_elfheader (abfd)->e_phnum)
824 phdr = elf_tdata (abfd)->phdr;
825 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
827 /* This section is part of this segment if its file
828 offset plus size lies within the segment's memory
829 span and, if the section is loaded, the extent of the
830 loaded data lies within the extent of the segment.
832 Note - we used to check the p_paddr field as well, and
833 refuse to set the LMA if it was 0. This is wrong
834 though, as a perfectly valid initialised segment can
835 have a p_paddr of zero. Some architectures, eg ARM,
836 place special significance on the address 0 and
837 executables need to be able to have a segment which
838 covers this address. */
839 if (phdr->p_type == PT_LOAD
840 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
841 && (hdr->sh_offset + hdr->sh_size
842 <= phdr->p_offset + phdr->p_memsz)
843 && ((flags & SEC_LOAD) == 0
844 || (hdr->sh_offset + hdr->sh_size
845 <= phdr->p_offset + phdr->p_filesz)))
847 if ((flags & SEC_LOAD) == 0)
848 newsect->lma = (phdr->p_paddr
849 + hdr->sh_addr - phdr->p_vaddr);
850 else
851 /* We used to use the same adjustment for SEC_LOAD
852 sections, but that doesn't work if the segment
853 is packed with code from multiple VMAs.
854 Instead we calculate the section LMA based on
855 the segment LMA. It is assumed that the
856 segment will contain sections with contiguous
857 LMAs, even if the VMAs are not. */
858 newsect->lma = (phdr->p_paddr
859 + hdr->sh_offset - phdr->p_offset);
861 /* With contiguous segments, we can't tell from file
862 offsets whether a section with zero size should
863 be placed at the end of one segment or the
864 beginning of the next. Decide based on vaddr. */
865 if (hdr->sh_addr >= phdr->p_vaddr
866 && (hdr->sh_addr + hdr->sh_size
867 <= phdr->p_vaddr + phdr->p_memsz))
868 break;
874 hdr->bfd_section = newsect;
875 elf_section_data (newsect)->this_hdr = *hdr;
877 return TRUE;
881 INTERNAL_FUNCTION
882 bfd_elf_find_section
884 SYNOPSIS
885 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
887 DESCRIPTION
888 Helper functions for GDB to locate the string tables.
889 Since BFD hides string tables from callers, GDB needs to use an
890 internal hook to find them. Sun's .stabstr, in particular,
891 isn't even pointed to by the .stab section, so ordinary
892 mechanisms wouldn't work to find it, even if we had some.
895 struct elf_internal_shdr *
896 bfd_elf_find_section (abfd, name)
897 bfd *abfd;
898 char *name;
900 Elf_Internal_Shdr **i_shdrp;
901 char *shstrtab;
902 unsigned int max;
903 unsigned int i;
905 i_shdrp = elf_elfsections (abfd);
906 if (i_shdrp != NULL)
908 shstrtab = bfd_elf_get_str_section (abfd,
909 elf_elfheader (abfd)->e_shstrndx);
910 if (shstrtab != NULL)
912 max = elf_numsections (abfd);
913 for (i = 1; i < max; i++)
914 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
915 return i_shdrp[i];
918 return 0;
921 const char *const bfd_elf_section_type_names[] = {
922 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
923 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
924 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
927 /* ELF relocs are against symbols. If we are producing relocateable
928 output, and the reloc is against an external symbol, and nothing
929 has given us any additional addend, the resulting reloc will also
930 be against the same symbol. In such a case, we don't want to
931 change anything about the way the reloc is handled, since it will
932 all be done at final link time. Rather than put special case code
933 into bfd_perform_relocation, all the reloc types use this howto
934 function. It just short circuits the reloc if producing
935 relocateable output against an external symbol. */
937 bfd_reloc_status_type
938 bfd_elf_generic_reloc (abfd,
939 reloc_entry,
940 symbol,
941 data,
942 input_section,
943 output_bfd,
944 error_message)
945 bfd *abfd ATTRIBUTE_UNUSED;
946 arelent *reloc_entry;
947 asymbol *symbol;
948 PTR data ATTRIBUTE_UNUSED;
949 asection *input_section;
950 bfd *output_bfd;
951 char **error_message ATTRIBUTE_UNUSED;
953 if (output_bfd != (bfd *) NULL
954 && (symbol->flags & BSF_SECTION_SYM) == 0
955 && (! reloc_entry->howto->partial_inplace
956 || reloc_entry->addend == 0))
958 reloc_entry->address += input_section->output_offset;
959 return bfd_reloc_ok;
962 return bfd_reloc_continue;
965 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
967 static void
968 merge_sections_remove_hook (abfd, sec)
969 bfd *abfd ATTRIBUTE_UNUSED;
970 asection *sec;
972 struct bfd_elf_section_data *sec_data;
974 sec_data = elf_section_data (sec);
975 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);
976 sec_data->sec_info_type = ELF_INFO_TYPE_NONE;
979 /* Finish SHF_MERGE section merging. */
981 bfd_boolean
982 _bfd_elf_merge_sections (abfd, info)
983 bfd *abfd;
984 struct bfd_link_info *info;
986 if (!is_elf_hash_table (info))
987 return FALSE;
988 if (elf_hash_table (info)->merge_info)
989 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
990 merge_sections_remove_hook);
991 return TRUE;
994 void
995 _bfd_elf_link_just_syms (sec, info)
996 asection *sec;
997 struct bfd_link_info *info;
999 sec->output_section = bfd_abs_section_ptr;
1000 sec->output_offset = sec->vma;
1001 if (!is_elf_hash_table (info))
1002 return;
1004 elf_section_data (sec)->sec_info_type = ELF_INFO_TYPE_JUST_SYMS;
1007 /* Copy the program header and other data from one object module to
1008 another. */
1010 bfd_boolean
1011 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
1012 bfd *ibfd;
1013 bfd *obfd;
1015 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1016 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1017 return TRUE;
1019 BFD_ASSERT (!elf_flags_init (obfd)
1020 || (elf_elfheader (obfd)->e_flags
1021 == elf_elfheader (ibfd)->e_flags));
1023 elf_gp (obfd) = elf_gp (ibfd);
1024 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1025 elf_flags_init (obfd) = TRUE;
1026 return TRUE;
1029 /* Print out the program headers. */
1031 bfd_boolean
1032 _bfd_elf_print_private_bfd_data (abfd, farg)
1033 bfd *abfd;
1034 PTR farg;
1036 FILE *f = (FILE *) farg;
1037 Elf_Internal_Phdr *p;
1038 asection *s;
1039 bfd_byte *dynbuf = NULL;
1041 p = elf_tdata (abfd)->phdr;
1042 if (p != NULL)
1044 unsigned int i, c;
1046 fprintf (f, _("\nProgram Header:\n"));
1047 c = elf_elfheader (abfd)->e_phnum;
1048 for (i = 0; i < c; i++, p++)
1050 const char *pt;
1051 char buf[20];
1053 switch (p->p_type)
1055 case PT_NULL: pt = "NULL"; break;
1056 case PT_LOAD: pt = "LOAD"; break;
1057 case PT_DYNAMIC: pt = "DYNAMIC"; break;
1058 case PT_INTERP: pt = "INTERP"; break;
1059 case PT_NOTE: pt = "NOTE"; break;
1060 case PT_SHLIB: pt = "SHLIB"; break;
1061 case PT_PHDR: pt = "PHDR"; break;
1062 case PT_TLS: pt = "TLS"; break;
1063 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
1064 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
1066 fprintf (f, "%8s off 0x", pt);
1067 bfd_fprintf_vma (abfd, f, p->p_offset);
1068 fprintf (f, " vaddr 0x");
1069 bfd_fprintf_vma (abfd, f, p->p_vaddr);
1070 fprintf (f, " paddr 0x");
1071 bfd_fprintf_vma (abfd, f, p->p_paddr);
1072 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
1073 fprintf (f, " filesz 0x");
1074 bfd_fprintf_vma (abfd, f, p->p_filesz);
1075 fprintf (f, " memsz 0x");
1076 bfd_fprintf_vma (abfd, f, p->p_memsz);
1077 fprintf (f, " flags %c%c%c",
1078 (p->p_flags & PF_R) != 0 ? 'r' : '-',
1079 (p->p_flags & PF_W) != 0 ? 'w' : '-',
1080 (p->p_flags & PF_X) != 0 ? 'x' : '-');
1081 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
1082 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
1083 fprintf (f, "\n");
1087 s = bfd_get_section_by_name (abfd, ".dynamic");
1088 if (s != NULL)
1090 int elfsec;
1091 unsigned long shlink;
1092 bfd_byte *extdyn, *extdynend;
1093 size_t extdynsize;
1094 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1096 fprintf (f, _("\nDynamic Section:\n"));
1098 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1099 if (dynbuf == NULL)
1100 goto error_return;
1101 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1102 s->_raw_size))
1103 goto error_return;
1105 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1106 if (elfsec == -1)
1107 goto error_return;
1108 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1110 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1111 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1113 extdyn = dynbuf;
1114 extdynend = extdyn + s->_raw_size;
1115 for (; extdyn < extdynend; extdyn += extdynsize)
1117 Elf_Internal_Dyn dyn;
1118 const char *name;
1119 char ab[20];
1120 bfd_boolean stringp;
1122 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1124 if (dyn.d_tag == DT_NULL)
1125 break;
1127 stringp = FALSE;
1128 switch (dyn.d_tag)
1130 default:
1131 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
1132 name = ab;
1133 break;
1135 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break;
1136 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
1137 case DT_PLTGOT: name = "PLTGOT"; break;
1138 case DT_HASH: name = "HASH"; break;
1139 case DT_STRTAB: name = "STRTAB"; break;
1140 case DT_SYMTAB: name = "SYMTAB"; break;
1141 case DT_RELA: name = "RELA"; break;
1142 case DT_RELASZ: name = "RELASZ"; break;
1143 case DT_RELAENT: name = "RELAENT"; break;
1144 case DT_STRSZ: name = "STRSZ"; break;
1145 case DT_SYMENT: name = "SYMENT"; break;
1146 case DT_INIT: name = "INIT"; break;
1147 case DT_FINI: name = "FINI"; break;
1148 case DT_SONAME: name = "SONAME"; stringp = TRUE; break;
1149 case DT_RPATH: name = "RPATH"; stringp = TRUE; break;
1150 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
1151 case DT_REL: name = "REL"; break;
1152 case DT_RELSZ: name = "RELSZ"; break;
1153 case DT_RELENT: name = "RELENT"; break;
1154 case DT_PLTREL: name = "PLTREL"; break;
1155 case DT_DEBUG: name = "DEBUG"; break;
1156 case DT_TEXTREL: name = "TEXTREL"; break;
1157 case DT_JMPREL: name = "JMPREL"; break;
1158 case DT_BIND_NOW: name = "BIND_NOW"; break;
1159 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
1160 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
1161 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
1162 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
1163 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break;
1164 case DT_FLAGS: name = "FLAGS"; break;
1165 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
1166 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
1167 case DT_CHECKSUM: name = "CHECKSUM"; break;
1168 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
1169 case DT_MOVEENT: name = "MOVEENT"; break;
1170 case DT_MOVESZ: name = "MOVESZ"; break;
1171 case DT_FEATURE: name = "FEATURE"; break;
1172 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
1173 case DT_SYMINSZ: name = "SYMINSZ"; break;
1174 case DT_SYMINENT: name = "SYMINENT"; break;
1175 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break;
1176 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break;
1177 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break;
1178 case DT_PLTPAD: name = "PLTPAD"; break;
1179 case DT_MOVETAB: name = "MOVETAB"; break;
1180 case DT_SYMINFO: name = "SYMINFO"; break;
1181 case DT_RELACOUNT: name = "RELACOUNT"; break;
1182 case DT_RELCOUNT: name = "RELCOUNT"; break;
1183 case DT_FLAGS_1: name = "FLAGS_1"; break;
1184 case DT_VERSYM: name = "VERSYM"; break;
1185 case DT_VERDEF: name = "VERDEF"; break;
1186 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
1187 case DT_VERNEED: name = "VERNEED"; break;
1188 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
1189 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break;
1190 case DT_USED: name = "USED"; break;
1191 case DT_FILTER: name = "FILTER"; stringp = TRUE; break;
1194 fprintf (f, " %-11s ", name);
1195 if (! stringp)
1196 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
1197 else
1199 const char *string;
1200 unsigned int tagv = dyn.d_un.d_val;
1202 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1203 if (string == NULL)
1204 goto error_return;
1205 fprintf (f, "%s", string);
1207 fprintf (f, "\n");
1210 free (dynbuf);
1211 dynbuf = NULL;
1214 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1215 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1217 if (! _bfd_elf_slurp_version_tables (abfd))
1218 return FALSE;
1221 if (elf_dynverdef (abfd) != 0)
1223 Elf_Internal_Verdef *t;
1225 fprintf (f, _("\nVersion definitions:\n"));
1226 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1228 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1229 t->vd_flags, t->vd_hash, t->vd_nodename);
1230 if (t->vd_auxptr->vda_nextptr != NULL)
1232 Elf_Internal_Verdaux *a;
1234 fprintf (f, "\t");
1235 for (a = t->vd_auxptr->vda_nextptr;
1236 a != NULL;
1237 a = a->vda_nextptr)
1238 fprintf (f, "%s ", a->vda_nodename);
1239 fprintf (f, "\n");
1244 if (elf_dynverref (abfd) != 0)
1246 Elf_Internal_Verneed *t;
1248 fprintf (f, _("\nVersion References:\n"));
1249 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1251 Elf_Internal_Vernaux *a;
1253 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1254 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1255 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1256 a->vna_flags, a->vna_other, a->vna_nodename);
1260 return TRUE;
1262 error_return:
1263 if (dynbuf != NULL)
1264 free (dynbuf);
1265 return FALSE;
1268 /* Display ELF-specific fields of a symbol. */
1270 void
1271 bfd_elf_print_symbol (abfd, filep, symbol, how)
1272 bfd *abfd;
1273 PTR filep;
1274 asymbol *symbol;
1275 bfd_print_symbol_type how;
1277 FILE *file = (FILE *) filep;
1278 switch (how)
1280 case bfd_print_symbol_name:
1281 fprintf (file, "%s", symbol->name);
1282 break;
1283 case bfd_print_symbol_more:
1284 fprintf (file, "elf ");
1285 bfd_fprintf_vma (abfd, file, symbol->value);
1286 fprintf (file, " %lx", (long) symbol->flags);
1287 break;
1288 case bfd_print_symbol_all:
1290 const char *section_name;
1291 const char *name = NULL;
1292 struct elf_backend_data *bed;
1293 unsigned char st_other;
1294 bfd_vma val;
1296 section_name = symbol->section ? symbol->section->name : "(*none*)";
1298 bed = get_elf_backend_data (abfd);
1299 if (bed->elf_backend_print_symbol_all)
1300 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1302 if (name == NULL)
1304 name = symbol->name;
1305 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1308 fprintf (file, " %s\t", section_name);
1309 /* Print the "other" value for a symbol. For common symbols,
1310 we've already printed the size; now print the alignment.
1311 For other symbols, we have no specified alignment, and
1312 we've printed the address; now print the size. */
1313 if (bfd_is_com_section (symbol->section))
1314 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1315 else
1316 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1317 bfd_fprintf_vma (abfd, file, val);
1319 /* If we have version information, print it. */
1320 if (elf_tdata (abfd)->dynversym_section != 0
1321 && (elf_tdata (abfd)->dynverdef_section != 0
1322 || elf_tdata (abfd)->dynverref_section != 0))
1324 unsigned int vernum;
1325 const char *version_string;
1327 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1329 if (vernum == 0)
1330 version_string = "";
1331 else if (vernum == 1)
1332 version_string = "Base";
1333 else if (vernum <= elf_tdata (abfd)->cverdefs)
1334 version_string =
1335 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1336 else
1338 Elf_Internal_Verneed *t;
1340 version_string = "";
1341 for (t = elf_tdata (abfd)->verref;
1342 t != NULL;
1343 t = t->vn_nextref)
1345 Elf_Internal_Vernaux *a;
1347 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1349 if (a->vna_other == vernum)
1351 version_string = a->vna_nodename;
1352 break;
1358 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1359 fprintf (file, " %-11s", version_string);
1360 else
1362 int i;
1364 fprintf (file, " (%s)", version_string);
1365 for (i = 10 - strlen (version_string); i > 0; --i)
1366 putc (' ', file);
1370 /* If the st_other field is not zero, print it. */
1371 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1373 switch (st_other)
1375 case 0: break;
1376 case STV_INTERNAL: fprintf (file, " .internal"); break;
1377 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1378 case STV_PROTECTED: fprintf (file, " .protected"); break;
1379 default:
1380 /* Some other non-defined flags are also present, so print
1381 everything hex. */
1382 fprintf (file, " 0x%02x", (unsigned int) st_other);
1385 fprintf (file, " %s", name);
1387 break;
1391 /* Create an entry in an ELF linker hash table. */
1393 struct bfd_hash_entry *
1394 _bfd_elf_link_hash_newfunc (entry, table, string)
1395 struct bfd_hash_entry *entry;
1396 struct bfd_hash_table *table;
1397 const char *string;
1399 /* Allocate the structure if it has not already been allocated by a
1400 subclass. */
1401 if (entry == NULL)
1403 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1404 if (entry == NULL)
1405 return entry;
1408 /* Call the allocation method of the superclass. */
1409 entry = _bfd_link_hash_newfunc (entry, table, string);
1410 if (entry != NULL)
1412 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1413 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1415 /* Set local fields. */
1416 ret->indx = -1;
1417 ret->dynindx = -1;
1418 ret->dynstr_index = 0;
1419 ret->elf_hash_value = 0;
1420 ret->weakdef = NULL;
1421 ret->linker_section_pointer = NULL;
1422 ret->verinfo.verdef = NULL;
1423 ret->vtable_entries_size = 0;
1424 ret->vtable_entries_used = NULL;
1425 ret->vtable_parent = NULL;
1426 ret->got.refcount = htab->init_refcount;
1427 ret->plt.refcount = htab->init_refcount;
1428 ret->size = 0;
1429 ret->type = STT_NOTYPE;
1430 ret->other = 0;
1431 /* Assume that we have been called by a non-ELF symbol reader.
1432 This flag is then reset by the code which reads an ELF input
1433 file. This ensures that a symbol created by a non-ELF symbol
1434 reader will have the flag set correctly. */
1435 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1438 return entry;
1441 /* Copy data from an indirect symbol to its direct symbol, hiding the
1442 old indirect symbol. Also used for copying flags to a weakdef. */
1444 void
1445 _bfd_elf_link_hash_copy_indirect (bed, dir, ind)
1446 struct elf_backend_data *bed;
1447 struct elf_link_hash_entry *dir, *ind;
1449 bfd_signed_vma tmp;
1450 bfd_signed_vma lowest_valid = bed->can_refcount;
1452 /* Copy down any references that we may have already seen to the
1453 symbol which just became indirect. */
1455 dir->elf_link_hash_flags |=
1456 (ind->elf_link_hash_flags
1457 & (ELF_LINK_HASH_REF_DYNAMIC
1458 | ELF_LINK_HASH_REF_REGULAR
1459 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1460 | ELF_LINK_NON_GOT_REF));
1462 if (ind->root.type != bfd_link_hash_indirect)
1463 return;
1465 /* Copy over the global and procedure linkage table refcount entries.
1466 These may have been already set up by a check_relocs routine. */
1467 tmp = dir->got.refcount;
1468 if (tmp < lowest_valid)
1470 dir->got.refcount = ind->got.refcount;
1471 ind->got.refcount = tmp;
1473 else
1474 BFD_ASSERT (ind->got.refcount < lowest_valid);
1476 tmp = dir->plt.refcount;
1477 if (tmp < lowest_valid)
1479 dir->plt.refcount = ind->plt.refcount;
1480 ind->plt.refcount = tmp;
1482 else
1483 BFD_ASSERT (ind->plt.refcount < lowest_valid);
1485 if (dir->dynindx == -1)
1487 dir->dynindx = ind->dynindx;
1488 dir->dynstr_index = ind->dynstr_index;
1489 ind->dynindx = -1;
1490 ind->dynstr_index = 0;
1492 else
1493 BFD_ASSERT (ind->dynindx == -1);
1496 void
1497 _bfd_elf_link_hash_hide_symbol (info, h, force_local)
1498 struct bfd_link_info *info;
1499 struct elf_link_hash_entry *h;
1500 bfd_boolean force_local;
1502 h->plt.offset = (bfd_vma) -1;
1503 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1504 if (force_local)
1506 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1507 if (h->dynindx != -1)
1509 h->dynindx = -1;
1510 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1511 h->dynstr_index);
1516 /* Initialize an ELF linker hash table. */
1518 bfd_boolean
1519 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1520 struct elf_link_hash_table *table;
1521 bfd *abfd;
1522 struct bfd_hash_entry *(*newfunc)
1523 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *,
1524 const char *));
1526 bfd_boolean ret;
1528 table->dynamic_sections_created = FALSE;
1529 table->dynobj = NULL;
1530 /* Make sure can_refcount is extended to the width and signedness of
1531 init_refcount before we subtract one from it. */
1532 table->init_refcount = get_elf_backend_data (abfd)->can_refcount;
1533 --table->init_refcount;
1534 /* The first dynamic symbol is a dummy. */
1535 table->dynsymcount = 1;
1536 table->dynstr = NULL;
1537 table->bucketcount = 0;
1538 table->needed = NULL;
1539 table->hgot = NULL;
1540 table->stab_info = NULL;
1541 table->merge_info = NULL;
1542 memset (&table->eh_info, 0, sizeof (table->eh_info));
1543 table->dynlocal = NULL;
1544 table->runpath = NULL;
1545 table->tls_segment = NULL;
1546 table->loaded = NULL;
1548 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1549 table->root.type = bfd_link_elf_hash_table;
1551 return ret;
1554 /* Create an ELF linker hash table. */
1556 struct bfd_link_hash_table *
1557 _bfd_elf_link_hash_table_create (abfd)
1558 bfd *abfd;
1560 struct elf_link_hash_table *ret;
1561 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1563 ret = (struct elf_link_hash_table *) bfd_malloc (amt);
1564 if (ret == (struct elf_link_hash_table *) NULL)
1565 return NULL;
1567 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1569 free (ret);
1570 return NULL;
1573 return &ret->root;
1576 /* This is a hook for the ELF emulation code in the generic linker to
1577 tell the backend linker what file name to use for the DT_NEEDED
1578 entry for a dynamic object. The generic linker passes name as an
1579 empty string to indicate that no DT_NEEDED entry should be made. */
1581 void
1582 bfd_elf_set_dt_needed_name (abfd, name)
1583 bfd *abfd;
1584 const char *name;
1586 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1587 && bfd_get_format (abfd) == bfd_object)
1588 elf_dt_name (abfd) = name;
1591 void
1592 bfd_elf_set_dt_needed_soname (abfd, name)
1593 bfd *abfd;
1594 const char *name;
1596 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1597 && bfd_get_format (abfd) == bfd_object)
1598 elf_dt_soname (abfd) = name;
1601 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1602 the linker ELF emulation code. */
1604 struct bfd_link_needed_list *
1605 bfd_elf_get_needed_list (abfd, info)
1606 bfd *abfd ATTRIBUTE_UNUSED;
1607 struct bfd_link_info *info;
1609 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1610 return NULL;
1611 return elf_hash_table (info)->needed;
1614 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1615 hook for the linker ELF emulation code. */
1617 struct bfd_link_needed_list *
1618 bfd_elf_get_runpath_list (abfd, info)
1619 bfd *abfd ATTRIBUTE_UNUSED;
1620 struct bfd_link_info *info;
1622 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1623 return NULL;
1624 return elf_hash_table (info)->runpath;
1627 /* Get the name actually used for a dynamic object for a link. This
1628 is the SONAME entry if there is one. Otherwise, it is the string
1629 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1631 const char *
1632 bfd_elf_get_dt_soname (abfd)
1633 bfd *abfd;
1635 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1636 && bfd_get_format (abfd) == bfd_object)
1637 return elf_dt_name (abfd);
1638 return NULL;
1641 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1642 the ELF linker emulation code. */
1644 bfd_boolean
1645 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1646 bfd *abfd;
1647 struct bfd_link_needed_list **pneeded;
1649 asection *s;
1650 bfd_byte *dynbuf = NULL;
1651 int elfsec;
1652 unsigned long shlink;
1653 bfd_byte *extdyn, *extdynend;
1654 size_t extdynsize;
1655 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1657 *pneeded = NULL;
1659 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1660 || bfd_get_format (abfd) != bfd_object)
1661 return TRUE;
1663 s = bfd_get_section_by_name (abfd, ".dynamic");
1664 if (s == NULL || s->_raw_size == 0)
1665 return TRUE;
1667 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1668 if (dynbuf == NULL)
1669 goto error_return;
1671 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1672 s->_raw_size))
1673 goto error_return;
1675 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1676 if (elfsec == -1)
1677 goto error_return;
1679 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1681 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1682 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1684 extdyn = dynbuf;
1685 extdynend = extdyn + s->_raw_size;
1686 for (; extdyn < extdynend; extdyn += extdynsize)
1688 Elf_Internal_Dyn dyn;
1690 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1692 if (dyn.d_tag == DT_NULL)
1693 break;
1695 if (dyn.d_tag == DT_NEEDED)
1697 const char *string;
1698 struct bfd_link_needed_list *l;
1699 unsigned int tagv = dyn.d_un.d_val;
1700 bfd_size_type amt;
1702 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1703 if (string == NULL)
1704 goto error_return;
1706 amt = sizeof *l;
1707 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1708 if (l == NULL)
1709 goto error_return;
1711 l->by = abfd;
1712 l->name = string;
1713 l->next = *pneeded;
1714 *pneeded = l;
1718 free (dynbuf);
1720 return TRUE;
1722 error_return:
1723 if (dynbuf != NULL)
1724 free (dynbuf);
1725 return FALSE;
1728 /* Allocate an ELF string table--force the first byte to be zero. */
1730 struct bfd_strtab_hash *
1731 _bfd_elf_stringtab_init ()
1733 struct bfd_strtab_hash *ret;
1735 ret = _bfd_stringtab_init ();
1736 if (ret != NULL)
1738 bfd_size_type loc;
1740 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE);
1741 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1742 if (loc == (bfd_size_type) -1)
1744 _bfd_stringtab_free (ret);
1745 ret = NULL;
1748 return ret;
1751 /* ELF .o/exec file reading */
1753 /* Create a new bfd section from an ELF section header. */
1755 bfd_boolean
1756 bfd_section_from_shdr (abfd, shindex)
1757 bfd *abfd;
1758 unsigned int shindex;
1760 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1761 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1762 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1763 const char *name;
1765 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1767 switch (hdr->sh_type)
1769 case SHT_NULL:
1770 /* Inactive section. Throw it away. */
1771 return TRUE;
1773 case SHT_PROGBITS: /* Normal section with contents. */
1774 case SHT_NOBITS: /* .bss section. */
1775 case SHT_HASH: /* .hash section. */
1776 case SHT_NOTE: /* .note section. */
1777 case SHT_INIT_ARRAY: /* .init_array section. */
1778 case SHT_FINI_ARRAY: /* .fini_array section. */
1779 case SHT_PREINIT_ARRAY: /* .preinit_array section. */
1780 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1782 case SHT_DYNAMIC: /* Dynamic linking information. */
1783 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1784 return FALSE;
1785 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB)
1787 Elf_Internal_Shdr *dynsymhdr;
1789 /* The shared libraries distributed with hpux11 have a bogus
1790 sh_link field for the ".dynamic" section. Find the
1791 string table for the ".dynsym" section instead. */
1792 if (elf_dynsymtab (abfd) != 0)
1794 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)];
1795 hdr->sh_link = dynsymhdr->sh_link;
1797 else
1799 unsigned int i, num_sec;
1801 num_sec = elf_numsections (abfd);
1802 for (i = 1; i < num_sec; i++)
1804 dynsymhdr = elf_elfsections (abfd)[i];
1805 if (dynsymhdr->sh_type == SHT_DYNSYM)
1807 hdr->sh_link = dynsymhdr->sh_link;
1808 break;
1813 break;
1815 case SHT_SYMTAB: /* A symbol table */
1816 if (elf_onesymtab (abfd) == shindex)
1817 return TRUE;
1819 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1820 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1821 elf_onesymtab (abfd) = shindex;
1822 elf_tdata (abfd)->symtab_hdr = *hdr;
1823 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1824 abfd->flags |= HAS_SYMS;
1826 /* Sometimes a shared object will map in the symbol table. If
1827 SHF_ALLOC is set, and this is a shared object, then we also
1828 treat this section as a BFD section. We can not base the
1829 decision purely on SHF_ALLOC, because that flag is sometimes
1830 set in a relocateable object file, which would confuse the
1831 linker. */
1832 if ((hdr->sh_flags & SHF_ALLOC) != 0
1833 && (abfd->flags & DYNAMIC) != 0
1834 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1835 return FALSE;
1837 return TRUE;
1839 case SHT_DYNSYM: /* A dynamic symbol table */
1840 if (elf_dynsymtab (abfd) == shindex)
1841 return TRUE;
1843 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1844 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1845 elf_dynsymtab (abfd) = shindex;
1846 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1847 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1848 abfd->flags |= HAS_SYMS;
1850 /* Besides being a symbol table, we also treat this as a regular
1851 section, so that objcopy can handle it. */
1852 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1854 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1855 if (elf_symtab_shndx (abfd) == shindex)
1856 return TRUE;
1858 /* Get the associated symbol table. */
1859 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1860 || hdr->sh_link != elf_onesymtab (abfd))
1861 return FALSE;
1863 elf_symtab_shndx (abfd) = shindex;
1864 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1865 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1866 return TRUE;
1868 case SHT_STRTAB: /* A string table */
1869 if (hdr->bfd_section != NULL)
1870 return TRUE;
1871 if (ehdr->e_shstrndx == shindex)
1873 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1874 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1875 return TRUE;
1878 unsigned int i, num_sec;
1880 num_sec = elf_numsections (abfd);
1881 for (i = 1; i < num_sec; i++)
1883 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1884 if (hdr2->sh_link == shindex)
1886 if (! bfd_section_from_shdr (abfd, i))
1887 return FALSE;
1888 if (elf_onesymtab (abfd) == i)
1890 elf_tdata (abfd)->strtab_hdr = *hdr;
1891 elf_elfsections (abfd)[shindex] =
1892 &elf_tdata (abfd)->strtab_hdr;
1893 return TRUE;
1895 if (elf_dynsymtab (abfd) == i)
1897 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1898 elf_elfsections (abfd)[shindex] = hdr =
1899 &elf_tdata (abfd)->dynstrtab_hdr;
1900 /* We also treat this as a regular section, so
1901 that objcopy can handle it. */
1902 break;
1904 #if 0 /* Not handling other string tables specially right now. */
1905 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1906 /* We have a strtab for some random other section. */
1907 newsect = (asection *) hdr2->bfd_section;
1908 if (!newsect)
1909 break;
1910 hdr->bfd_section = newsect;
1911 hdr2 = &elf_section_data (newsect)->str_hdr;
1912 *hdr2 = *hdr;
1913 elf_elfsections (abfd)[shindex] = hdr2;
1914 #endif
1919 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1921 case SHT_REL:
1922 case SHT_RELA:
1923 /* *These* do a lot of work -- but build no sections! */
1925 asection *target_sect;
1926 Elf_Internal_Shdr *hdr2;
1927 unsigned int num_sec = elf_numsections (abfd);
1929 /* Check for a bogus link to avoid crashing. */
1930 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1931 || hdr->sh_link >= num_sec)
1933 ((*_bfd_error_handler)
1934 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1935 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1936 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1939 /* For some incomprehensible reason Oracle distributes
1940 libraries for Solaris in which some of the objects have
1941 bogus sh_link fields. It would be nice if we could just
1942 reject them, but, unfortunately, some people need to use
1943 them. We scan through the section headers; if we find only
1944 one suitable symbol table, we clobber the sh_link to point
1945 to it. I hope this doesn't break anything. */
1946 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1947 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1949 unsigned int scan;
1950 int found;
1952 found = 0;
1953 for (scan = 1; scan < num_sec; scan++)
1955 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1956 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1958 if (found != 0)
1960 found = 0;
1961 break;
1963 found = scan;
1966 if (found != 0)
1967 hdr->sh_link = found;
1970 /* Get the symbol table. */
1971 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1972 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1973 return FALSE;
1975 /* If this reloc section does not use the main symbol table we
1976 don't treat it as a reloc section. BFD can't adequately
1977 represent such a section, so at least for now, we don't
1978 try. We just present it as a normal section. We also
1979 can't use it as a reloc section if it points to the null
1980 section. */
1981 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1982 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1984 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1985 return FALSE;
1986 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1987 if (target_sect == NULL)
1988 return FALSE;
1990 if ((target_sect->flags & SEC_RELOC) == 0
1991 || target_sect->reloc_count == 0)
1992 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1993 else
1995 bfd_size_type amt;
1996 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1997 amt = sizeof (*hdr2);
1998 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1999 elf_section_data (target_sect)->rel_hdr2 = hdr2;
2001 *hdr2 = *hdr;
2002 elf_elfsections (abfd)[shindex] = hdr2;
2003 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
2004 target_sect->flags |= SEC_RELOC;
2005 target_sect->relocation = NULL;
2006 target_sect->rel_filepos = hdr->sh_offset;
2007 /* In the section to which the relocations apply, mark whether
2008 its relocations are of the REL or RELA variety. */
2009 if (hdr->sh_size != 0)
2010 elf_section_data (target_sect)->use_rela_p
2011 = (hdr->sh_type == SHT_RELA);
2012 abfd->flags |= HAS_RELOC;
2013 return TRUE;
2015 break;
2017 case SHT_GNU_verdef:
2018 elf_dynverdef (abfd) = shindex;
2019 elf_tdata (abfd)->dynverdef_hdr = *hdr;
2020 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2021 break;
2023 case SHT_GNU_versym:
2024 elf_dynversym (abfd) = shindex;
2025 elf_tdata (abfd)->dynversym_hdr = *hdr;
2026 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2027 break;
2029 case SHT_GNU_verneed:
2030 elf_dynverref (abfd) = shindex;
2031 elf_tdata (abfd)->dynverref_hdr = *hdr;
2032 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
2033 break;
2035 case SHT_SHLIB:
2036 return TRUE;
2038 case SHT_GROUP:
2039 /* We need a BFD section for objcopy and relocatable linking,
2040 and it's handy to have the signature available as the section
2041 name. */
2042 name = group_signature (abfd, hdr);
2043 if (name == NULL)
2044 return FALSE;
2045 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
2046 return FALSE;
2047 if (hdr->contents != NULL)
2049 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
2050 unsigned int n_elt = hdr->sh_size / 4;
2051 asection *s;
2053 if (idx->flags & GRP_COMDAT)
2054 hdr->bfd_section->flags
2055 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2057 while (--n_elt != 0)
2058 if ((s = (++idx)->shdr->bfd_section) != NULL
2059 && elf_next_in_group (s) != NULL)
2061 elf_next_in_group (hdr->bfd_section) = s;
2062 break;
2065 break;
2067 default:
2068 /* Check for any processor-specific section types. */
2070 if (bed->elf_backend_section_from_shdr)
2071 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
2073 break;
2076 return TRUE;
2079 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
2080 Return SEC for sections that have no elf section, and NULL on error. */
2082 asection *
2083 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
2084 bfd *abfd;
2085 struct sym_sec_cache *cache;
2086 asection *sec;
2087 unsigned long r_symndx;
2089 Elf_Internal_Shdr *symtab_hdr;
2090 unsigned char esym[sizeof (Elf64_External_Sym)];
2091 Elf_External_Sym_Shndx eshndx;
2092 Elf_Internal_Sym isym;
2093 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
2095 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
2096 return cache->sec[ent];
2098 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2099 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx,
2100 &isym, esym, &eshndx) == NULL)
2101 return NULL;
2103 if (cache->abfd != abfd)
2105 memset (cache->indx, -1, sizeof (cache->indx));
2106 cache->abfd = abfd;
2108 cache->indx[ent] = r_symndx;
2109 cache->sec[ent] = sec;
2110 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE)
2112 asection *s;
2113 s = bfd_section_from_elf_index (abfd, isym.st_shndx);
2114 if (s != NULL)
2115 cache->sec[ent] = s;
2117 return cache->sec[ent];
2120 /* Given an ELF section number, retrieve the corresponding BFD
2121 section. */
2123 asection *
2124 bfd_section_from_elf_index (abfd, index)
2125 bfd *abfd;
2126 unsigned int index;
2128 if (index >= elf_numsections (abfd))
2129 return NULL;
2130 return elf_elfsections (abfd)[index]->bfd_section;
2133 bfd_boolean
2134 _bfd_elf_new_section_hook (abfd, sec)
2135 bfd *abfd;
2136 asection *sec;
2138 struct bfd_elf_section_data *sdata;
2139 bfd_size_type amt = sizeof (*sdata);
2141 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
2142 if (!sdata)
2143 return FALSE;
2144 sec->used_by_bfd = (PTR) sdata;
2146 /* Indicate whether or not this section should use RELA relocations. */
2147 sdata->use_rela_p
2148 = get_elf_backend_data (abfd)->default_use_rela_p;
2150 return TRUE;
2153 /* Create a new bfd section from an ELF program header.
2155 Since program segments have no names, we generate a synthetic name
2156 of the form segment<NUM>, where NUM is generally the index in the
2157 program header table. For segments that are split (see below) we
2158 generate the names segment<NUM>a and segment<NUM>b.
2160 Note that some program segments may have a file size that is different than
2161 (less than) the memory size. All this means is that at execution the
2162 system must allocate the amount of memory specified by the memory size,
2163 but only initialize it with the first "file size" bytes read from the
2164 file. This would occur for example, with program segments consisting
2165 of combined data+bss.
2167 To handle the above situation, this routine generates TWO bfd sections
2168 for the single program segment. The first has the length specified by
2169 the file size of the segment, and the second has the length specified
2170 by the difference between the two sizes. In effect, the segment is split
2171 into it's initialized and uninitialized parts.
2175 bfd_boolean
2176 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
2177 bfd *abfd;
2178 Elf_Internal_Phdr *hdr;
2179 int index;
2180 const char *typename;
2182 asection *newsect;
2183 char *name;
2184 char namebuf[64];
2185 size_t len;
2186 int split;
2188 split = ((hdr->p_memsz > 0)
2189 && (hdr->p_filesz > 0)
2190 && (hdr->p_memsz > hdr->p_filesz));
2191 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
2192 len = strlen (namebuf) + 1;
2193 name = bfd_alloc (abfd, (bfd_size_type) len);
2194 if (!name)
2195 return FALSE;
2196 memcpy (name, namebuf, len);
2197 newsect = bfd_make_section (abfd, name);
2198 if (newsect == NULL)
2199 return FALSE;
2200 newsect->vma = hdr->p_vaddr;
2201 newsect->lma = hdr->p_paddr;
2202 newsect->_raw_size = hdr->p_filesz;
2203 newsect->filepos = hdr->p_offset;
2204 newsect->flags |= SEC_HAS_CONTENTS;
2205 if (hdr->p_type == PT_LOAD)
2207 newsect->flags |= SEC_ALLOC;
2208 newsect->flags |= SEC_LOAD;
2209 if (hdr->p_flags & PF_X)
2211 /* FIXME: all we known is that it has execute PERMISSION,
2212 may be data. */
2213 newsect->flags |= SEC_CODE;
2216 if (!(hdr->p_flags & PF_W))
2218 newsect->flags |= SEC_READONLY;
2221 if (split)
2223 sprintf (namebuf, "%s%db", typename, index);
2224 len = strlen (namebuf) + 1;
2225 name = bfd_alloc (abfd, (bfd_size_type) len);
2226 if (!name)
2227 return FALSE;
2228 memcpy (name, namebuf, len);
2229 newsect = bfd_make_section (abfd, name);
2230 if (newsect == NULL)
2231 return FALSE;
2232 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2233 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2234 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2235 if (hdr->p_type == PT_LOAD)
2237 newsect->flags |= SEC_ALLOC;
2238 if (hdr->p_flags & PF_X)
2239 newsect->flags |= SEC_CODE;
2241 if (!(hdr->p_flags & PF_W))
2242 newsect->flags |= SEC_READONLY;
2245 return TRUE;
2248 bfd_boolean
2249 bfd_section_from_phdr (abfd, hdr, index)
2250 bfd *abfd;
2251 Elf_Internal_Phdr *hdr;
2252 int index;
2254 struct elf_backend_data *bed;
2256 switch (hdr->p_type)
2258 case PT_NULL:
2259 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2261 case PT_LOAD:
2262 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2264 case PT_DYNAMIC:
2265 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2267 case PT_INTERP:
2268 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2270 case PT_NOTE:
2271 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2272 return FALSE;
2273 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2274 return FALSE;
2275 return TRUE;
2277 case PT_SHLIB:
2278 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2280 case PT_PHDR:
2281 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2283 default:
2284 /* Check for any processor-specific program segment types.
2285 If no handler for them, default to making "segment" sections. */
2286 bed = get_elf_backend_data (abfd);
2287 if (bed->elf_backend_section_from_phdr)
2288 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2289 else
2290 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2294 /* Initialize REL_HDR, the section-header for new section, containing
2295 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA
2296 relocations; otherwise, we use REL relocations. */
2298 bfd_boolean
2299 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2300 bfd *abfd;
2301 Elf_Internal_Shdr *rel_hdr;
2302 asection *asect;
2303 bfd_boolean use_rela_p;
2305 char *name;
2306 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2307 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2309 name = bfd_alloc (abfd, amt);
2310 if (name == NULL)
2311 return FALSE;
2312 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2313 rel_hdr->sh_name =
2314 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2315 FALSE);
2316 if (rel_hdr->sh_name == (unsigned int) -1)
2317 return FALSE;
2318 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2319 rel_hdr->sh_entsize = (use_rela_p
2320 ? bed->s->sizeof_rela
2321 : bed->s->sizeof_rel);
2322 rel_hdr->sh_addralign = bed->s->file_align;
2323 rel_hdr->sh_flags = 0;
2324 rel_hdr->sh_addr = 0;
2325 rel_hdr->sh_size = 0;
2326 rel_hdr->sh_offset = 0;
2328 return TRUE;
2331 /* Set up an ELF internal section header for a section. */
2333 static void
2334 elf_fake_sections (abfd, asect, failedptrarg)
2335 bfd *abfd;
2336 asection *asect;
2337 PTR failedptrarg;
2339 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2340 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2341 Elf_Internal_Shdr *this_hdr;
2343 if (*failedptr)
2345 /* We already failed; just get out of the bfd_map_over_sections
2346 loop. */
2347 return;
2350 this_hdr = &elf_section_data (asect)->this_hdr;
2352 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2353 asect->name, FALSE);
2354 if (this_hdr->sh_name == (unsigned long) -1)
2356 *failedptr = TRUE;
2357 return;
2360 this_hdr->sh_flags = 0;
2362 if ((asect->flags & SEC_ALLOC) != 0
2363 || asect->user_set_vma)
2364 this_hdr->sh_addr = asect->vma;
2365 else
2366 this_hdr->sh_addr = 0;
2368 this_hdr->sh_offset = 0;
2369 this_hdr->sh_size = asect->_raw_size;
2370 this_hdr->sh_link = 0;
2371 this_hdr->sh_addralign = 1 << asect->alignment_power;
2372 /* The sh_entsize and sh_info fields may have been set already by
2373 copy_private_section_data. */
2375 this_hdr->bfd_section = asect;
2376 this_hdr->contents = NULL;
2378 /* FIXME: This should not be based on section names. */
2379 if (strcmp (asect->name, ".dynstr") == 0)
2380 this_hdr->sh_type = SHT_STRTAB;
2381 else if (strcmp (asect->name, ".hash") == 0)
2383 this_hdr->sh_type = SHT_HASH;
2384 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2386 else if (strcmp (asect->name, ".dynsym") == 0)
2388 this_hdr->sh_type = SHT_DYNSYM;
2389 this_hdr->sh_entsize = bed->s->sizeof_sym;
2391 else if (strcmp (asect->name, ".dynamic") == 0)
2393 this_hdr->sh_type = SHT_DYNAMIC;
2394 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2396 else if (strncmp (asect->name, ".rela", 5) == 0
2397 && get_elf_backend_data (abfd)->may_use_rela_p)
2399 this_hdr->sh_type = SHT_RELA;
2400 this_hdr->sh_entsize = bed->s->sizeof_rela;
2402 else if (strncmp (asect->name, ".rel", 4) == 0
2403 && get_elf_backend_data (abfd)->may_use_rel_p)
2405 this_hdr->sh_type = SHT_REL;
2406 this_hdr->sh_entsize = bed->s->sizeof_rel;
2408 else if (strcmp (asect->name, ".init_array") == 0)
2409 this_hdr->sh_type = SHT_INIT_ARRAY;
2410 else if (strcmp (asect->name, ".fini_array") == 0)
2411 this_hdr->sh_type = SHT_FINI_ARRAY;
2412 else if (strcmp (asect->name, ".preinit_array") == 0)
2413 this_hdr->sh_type = SHT_PREINIT_ARRAY;
2414 else if (strncmp (asect->name, ".note", 5) == 0)
2415 this_hdr->sh_type = SHT_NOTE;
2416 else if (strncmp (asect->name, ".stab", 5) == 0
2417 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2418 this_hdr->sh_type = SHT_STRTAB;
2419 else if (strcmp (asect->name, ".gnu.version") == 0)
2421 this_hdr->sh_type = SHT_GNU_versym;
2422 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2424 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2426 this_hdr->sh_type = SHT_GNU_verdef;
2427 this_hdr->sh_entsize = 0;
2428 /* objcopy or strip will copy over sh_info, but may not set
2429 cverdefs. The linker will set cverdefs, but sh_info will be
2430 zero. */
2431 if (this_hdr->sh_info == 0)
2432 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2433 else
2434 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2435 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2437 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2439 this_hdr->sh_type = SHT_GNU_verneed;
2440 this_hdr->sh_entsize = 0;
2441 /* objcopy or strip will copy over sh_info, but may not set
2442 cverrefs. The linker will set cverrefs, but sh_info will be
2443 zero. */
2444 if (this_hdr->sh_info == 0)
2445 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2446 else
2447 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2448 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2450 else if ((asect->flags & SEC_GROUP) != 0)
2452 this_hdr->sh_type = SHT_GROUP;
2453 this_hdr->sh_entsize = 4;
2455 else if ((asect->flags & SEC_ALLOC) != 0
2456 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2457 || (asect->flags & SEC_NEVER_LOAD) != 0))
2458 this_hdr->sh_type = SHT_NOBITS;
2459 else
2460 this_hdr->sh_type = SHT_PROGBITS;
2462 if ((asect->flags & SEC_ALLOC) != 0)
2463 this_hdr->sh_flags |= SHF_ALLOC;
2464 if ((asect->flags & SEC_READONLY) == 0)
2465 this_hdr->sh_flags |= SHF_WRITE;
2466 if ((asect->flags & SEC_CODE) != 0)
2467 this_hdr->sh_flags |= SHF_EXECINSTR;
2468 if ((asect->flags & SEC_MERGE) != 0)
2470 this_hdr->sh_flags |= SHF_MERGE;
2471 this_hdr->sh_entsize = asect->entsize;
2472 if ((asect->flags & SEC_STRINGS) != 0)
2473 this_hdr->sh_flags |= SHF_STRINGS;
2475 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL)
2476 this_hdr->sh_flags |= SHF_GROUP;
2477 if ((asect->flags & SEC_THREAD_LOCAL) != 0)
2479 this_hdr->sh_flags |= SHF_TLS;
2480 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0)
2482 struct bfd_link_order *o;
2484 this_hdr->sh_size = 0;
2485 for (o = asect->link_order_head; o != NULL; o = o->next)
2486 if (this_hdr->sh_size < o->offset + o->size)
2487 this_hdr->sh_size = o->offset + o->size;
2488 if (this_hdr->sh_size)
2489 this_hdr->sh_type = SHT_NOBITS;
2493 /* Check for processor-specific section types. */
2494 if (bed->elf_backend_fake_sections
2495 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2496 *failedptr = TRUE;
2498 /* If the section has relocs, set up a section header for the
2499 SHT_REL[A] section. If two relocation sections are required for
2500 this section, it is up to the processor-specific back-end to
2501 create the other. */
2502 if ((asect->flags & SEC_RELOC) != 0
2503 && !_bfd_elf_init_reloc_shdr (abfd,
2504 &elf_section_data (asect)->rel_hdr,
2505 asect,
2506 elf_section_data (asect)->use_rela_p))
2507 *failedptr = TRUE;
2510 /* Fill in the contents of a SHT_GROUP section. */
2512 void
2513 bfd_elf_set_group_contents (abfd, sec, failedptrarg)
2514 bfd *abfd;
2515 asection *sec;
2516 PTR failedptrarg;
2518 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg;
2519 unsigned long symindx;
2520 asection *elt, *first;
2521 unsigned char *loc;
2522 struct bfd_link_order *l;
2523 bfd_boolean gas;
2525 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2526 || *failedptr)
2527 return;
2529 symindx = 0;
2530 if (elf_group_id (sec) != NULL)
2531 symindx = elf_group_id (sec)->udata.i;
2533 if (symindx == 0)
2535 /* If called from the assembler, swap_out_syms will have set up
2536 elf_section_syms; If called for "ld -r", use target_index. */
2537 if (elf_section_syms (abfd) != NULL)
2538 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2539 else
2540 symindx = sec->target_index;
2542 elf_section_data (sec)->this_hdr.sh_info = symindx;
2544 /* The contents won't be allocated for "ld -r" or objcopy. */
2545 gas = TRUE;
2546 if (sec->contents == NULL)
2548 gas = FALSE;
2549 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2551 /* Arrange for the section to be written out. */
2552 elf_section_data (sec)->this_hdr.contents = sec->contents;
2553 if (sec->contents == NULL)
2555 *failedptr = TRUE;
2556 return;
2560 loc = sec->contents + sec->_raw_size;
2562 /* Get the pointer to the first section in the group that gas
2563 squirreled away here. objcopy arranges for this to be set to the
2564 start of the input section group. */
2565 first = elt = elf_next_in_group (sec);
2567 /* First element is a flag word. Rest of section is elf section
2568 indices for all the sections of the group. Write them backwards
2569 just to keep the group in the same order as given in .section
2570 directives, not that it matters. */
2571 while (elt != NULL)
2573 asection *s;
2574 unsigned int idx;
2576 loc -= 4;
2577 s = elt;
2578 if (!gas)
2579 s = s->output_section;
2580 idx = 0;
2581 if (s != NULL)
2582 idx = elf_section_data (s)->this_idx;
2583 H_PUT_32 (abfd, idx, loc);
2584 elt = elf_next_in_group (elt);
2585 if (elt == first)
2586 break;
2589 /* If this is a relocatable link, then the above did nothing because
2590 SEC is the output section. Look through the input sections
2591 instead. */
2592 for (l = sec->link_order_head; l != NULL; l = l->next)
2593 if (l->type == bfd_indirect_link_order
2594 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2597 loc -= 4;
2598 H_PUT_32 (abfd,
2599 elf_section_data (elt->output_section)->this_idx, loc);
2600 elt = elf_next_in_group (elt);
2601 /* During a relocatable link, the lists are circular. */
2603 while (elt != elf_next_in_group (l->u.indirect.section));
2605 /* With ld -r, merging SHT_GROUP sections results in wasted space
2606 due to allowing for the flag word on each input. We may well
2607 duplicate entries too. */
2608 while ((loc -= 4) > sec->contents)
2609 H_PUT_32 (abfd, 0, loc);
2611 if (loc != sec->contents)
2612 abort ();
2614 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc);
2617 /* Assign all ELF section numbers. The dummy first section is handled here
2618 too. The link/info pointers for the standard section types are filled
2619 in here too, while we're at it. */
2621 static bfd_boolean
2622 assign_section_numbers (abfd)
2623 bfd *abfd;
2625 struct elf_obj_tdata *t = elf_tdata (abfd);
2626 asection *sec;
2627 unsigned int section_number, secn;
2628 Elf_Internal_Shdr **i_shdrp;
2629 bfd_size_type amt;
2631 section_number = 1;
2633 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2635 for (sec = abfd->sections; sec; sec = sec->next)
2637 struct bfd_elf_section_data *d = elf_section_data (sec);
2639 if (section_number == SHN_LORESERVE)
2640 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2641 d->this_idx = section_number++;
2642 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2643 if ((sec->flags & SEC_RELOC) == 0)
2644 d->rel_idx = 0;
2645 else
2647 if (section_number == SHN_LORESERVE)
2648 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2649 d->rel_idx = section_number++;
2650 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2653 if (d->rel_hdr2)
2655 if (section_number == SHN_LORESERVE)
2656 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2657 d->rel_idx2 = section_number++;
2658 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2660 else
2661 d->rel_idx2 = 0;
2664 if (section_number == SHN_LORESERVE)
2665 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2666 t->shstrtab_section = section_number++;
2667 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2668 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2670 if (bfd_get_symcount (abfd) > 0)
2672 if (section_number == SHN_LORESERVE)
2673 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2674 t->symtab_section = section_number++;
2675 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2676 if (section_number > SHN_LORESERVE - 2)
2678 if (section_number == SHN_LORESERVE)
2679 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2680 t->symtab_shndx_section = section_number++;
2681 t->symtab_shndx_hdr.sh_name
2682 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2683 ".symtab_shndx", FALSE);
2684 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2685 return FALSE;
2687 if (section_number == SHN_LORESERVE)
2688 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2689 t->strtab_section = section_number++;
2690 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2693 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2694 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2696 elf_numsections (abfd) = section_number;
2697 elf_elfheader (abfd)->e_shnum = section_number;
2698 if (section_number > SHN_LORESERVE)
2699 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2701 /* Set up the list of section header pointers, in agreement with the
2702 indices. */
2703 amt = section_number * sizeof (Elf_Internal_Shdr *);
2704 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt);
2705 if (i_shdrp == NULL)
2706 return FALSE;
2708 amt = sizeof (Elf_Internal_Shdr);
2709 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, amt);
2710 if (i_shdrp[0] == NULL)
2712 bfd_release (abfd, i_shdrp);
2713 return FALSE;
2716 elf_elfsections (abfd) = i_shdrp;
2718 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2719 if (bfd_get_symcount (abfd) > 0)
2721 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2722 if (elf_numsections (abfd) > SHN_LORESERVE)
2724 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2725 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2727 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2728 t->symtab_hdr.sh_link = t->strtab_section;
2730 for (sec = abfd->sections; sec; sec = sec->next)
2732 struct bfd_elf_section_data *d = elf_section_data (sec);
2733 asection *s;
2734 const char *name;
2736 i_shdrp[d->this_idx] = &d->this_hdr;
2737 if (d->rel_idx != 0)
2738 i_shdrp[d->rel_idx] = &d->rel_hdr;
2739 if (d->rel_idx2 != 0)
2740 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2742 /* Fill in the sh_link and sh_info fields while we're at it. */
2744 /* sh_link of a reloc section is the section index of the symbol
2745 table. sh_info is the section index of the section to which
2746 the relocation entries apply. */
2747 if (d->rel_idx != 0)
2749 d->rel_hdr.sh_link = t->symtab_section;
2750 d->rel_hdr.sh_info = d->this_idx;
2752 if (d->rel_idx2 != 0)
2754 d->rel_hdr2->sh_link = t->symtab_section;
2755 d->rel_hdr2->sh_info = d->this_idx;
2758 switch (d->this_hdr.sh_type)
2760 case SHT_REL:
2761 case SHT_RELA:
2762 /* A reloc section which we are treating as a normal BFD
2763 section. sh_link is the section index of the symbol
2764 table. sh_info is the section index of the section to
2765 which the relocation entries apply. We assume that an
2766 allocated reloc section uses the dynamic symbol table.
2767 FIXME: How can we be sure? */
2768 s = bfd_get_section_by_name (abfd, ".dynsym");
2769 if (s != NULL)
2770 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2772 /* We look up the section the relocs apply to by name. */
2773 name = sec->name;
2774 if (d->this_hdr.sh_type == SHT_REL)
2775 name += 4;
2776 else
2777 name += 5;
2778 s = bfd_get_section_by_name (abfd, name);
2779 if (s != NULL)
2780 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2781 break;
2783 case SHT_STRTAB:
2784 /* We assume that a section named .stab*str is a stabs
2785 string section. We look for a section with the same name
2786 but without the trailing ``str'', and set its sh_link
2787 field to point to this section. */
2788 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2789 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2791 size_t len;
2792 char *alc;
2794 len = strlen (sec->name);
2795 alc = (char *) bfd_malloc ((bfd_size_type) (len - 2));
2796 if (alc == NULL)
2797 return FALSE;
2798 memcpy (alc, sec->name, len - 3);
2799 alc[len - 3] = '\0';
2800 s = bfd_get_section_by_name (abfd, alc);
2801 free (alc);
2802 if (s != NULL)
2804 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2806 /* This is a .stab section. */
2807 if (elf_section_data (s)->this_hdr.sh_entsize == 0)
2808 elf_section_data (s)->this_hdr.sh_entsize
2809 = 4 + 2 * bfd_get_arch_size (abfd) / 8;
2812 break;
2814 case SHT_DYNAMIC:
2815 case SHT_DYNSYM:
2816 case SHT_GNU_verneed:
2817 case SHT_GNU_verdef:
2818 /* sh_link is the section header index of the string table
2819 used for the dynamic entries, or the symbol table, or the
2820 version strings. */
2821 s = bfd_get_section_by_name (abfd, ".dynstr");
2822 if (s != NULL)
2823 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2824 break;
2826 case SHT_HASH:
2827 case SHT_GNU_versym:
2828 /* sh_link is the section header index of the symbol table
2829 this hash table or version table is for. */
2830 s = bfd_get_section_by_name (abfd, ".dynsym");
2831 if (s != NULL)
2832 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2833 break;
2835 case SHT_GROUP:
2836 d->this_hdr.sh_link = t->symtab_section;
2840 for (secn = 1; secn < section_number; ++secn)
2841 if (i_shdrp[secn] == NULL)
2842 i_shdrp[secn] = i_shdrp[0];
2843 else
2844 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2845 i_shdrp[secn]->sh_name);
2846 return TRUE;
2849 /* Map symbol from it's internal number to the external number, moving
2850 all local symbols to be at the head of the list. */
2852 static INLINE int
2853 sym_is_global (abfd, sym)
2854 bfd *abfd;
2855 asymbol *sym;
2857 /* If the backend has a special mapping, use it. */
2858 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2859 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2860 (abfd, sym));
2862 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2863 || bfd_is_und_section (bfd_get_section (sym))
2864 || bfd_is_com_section (bfd_get_section (sym)));
2867 static bfd_boolean
2868 elf_map_symbols (abfd)
2869 bfd *abfd;
2871 unsigned int symcount = bfd_get_symcount (abfd);
2872 asymbol **syms = bfd_get_outsymbols (abfd);
2873 asymbol **sect_syms;
2874 unsigned int num_locals = 0;
2875 unsigned int num_globals = 0;
2876 unsigned int num_locals2 = 0;
2877 unsigned int num_globals2 = 0;
2878 int max_index = 0;
2879 unsigned int idx;
2880 asection *asect;
2881 asymbol **new_syms;
2882 bfd_size_type amt;
2884 #ifdef DEBUG
2885 fprintf (stderr, "elf_map_symbols\n");
2886 fflush (stderr);
2887 #endif
2889 for (asect = abfd->sections; asect; asect = asect->next)
2891 if (max_index < asect->index)
2892 max_index = asect->index;
2895 max_index++;
2896 amt = max_index * sizeof (asymbol *);
2897 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2898 if (sect_syms == NULL)
2899 return FALSE;
2900 elf_section_syms (abfd) = sect_syms;
2901 elf_num_section_syms (abfd) = max_index;
2903 /* Init sect_syms entries for any section symbols we have already
2904 decided to output. */
2905 for (idx = 0; idx < symcount; idx++)
2907 asymbol *sym = syms[idx];
2909 if ((sym->flags & BSF_SECTION_SYM) != 0
2910 && sym->value == 0)
2912 asection *sec;
2914 sec = sym->section;
2916 if (sec->owner != NULL)
2918 if (sec->owner != abfd)
2920 if (sec->output_offset != 0)
2921 continue;
2923 sec = sec->output_section;
2925 /* Empty sections in the input files may have had a
2926 section symbol created for them. (See the comment
2927 near the end of _bfd_generic_link_output_symbols in
2928 linker.c). If the linker script discards such
2929 sections then we will reach this point. Since we know
2930 that we cannot avoid this case, we detect it and skip
2931 the abort and the assignment to the sect_syms array.
2932 To reproduce this particular case try running the
2933 linker testsuite test ld-scripts/weak.exp for an ELF
2934 port that uses the generic linker. */
2935 if (sec->owner == NULL)
2936 continue;
2938 BFD_ASSERT (sec->owner == abfd);
2940 sect_syms[sec->index] = syms[idx];
2945 /* Classify all of the symbols. */
2946 for (idx = 0; idx < symcount; idx++)
2948 if (!sym_is_global (abfd, syms[idx]))
2949 num_locals++;
2950 else
2951 num_globals++;
2954 /* We will be adding a section symbol for each BFD section. Most normal
2955 sections will already have a section symbol in outsymbols, but
2956 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2957 at least in that case. */
2958 for (asect = abfd->sections; asect; asect = asect->next)
2960 if (sect_syms[asect->index] == NULL)
2962 if (!sym_is_global (abfd, asect->symbol))
2963 num_locals++;
2964 else
2965 num_globals++;
2969 /* Now sort the symbols so the local symbols are first. */
2970 amt = (num_locals + num_globals) * sizeof (asymbol *);
2971 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2973 if (new_syms == NULL)
2974 return FALSE;
2976 for (idx = 0; idx < symcount; idx++)
2978 asymbol *sym = syms[idx];
2979 unsigned int i;
2981 if (!sym_is_global (abfd, sym))
2982 i = num_locals2++;
2983 else
2984 i = num_locals + num_globals2++;
2985 new_syms[i] = sym;
2986 sym->udata.i = i + 1;
2988 for (asect = abfd->sections; asect; asect = asect->next)
2990 if (sect_syms[asect->index] == NULL)
2992 asymbol *sym = asect->symbol;
2993 unsigned int i;
2995 sect_syms[asect->index] = sym;
2996 if (!sym_is_global (abfd, sym))
2997 i = num_locals2++;
2998 else
2999 i = num_locals + num_globals2++;
3000 new_syms[i] = sym;
3001 sym->udata.i = i + 1;
3005 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
3007 elf_num_locals (abfd) = num_locals;
3008 elf_num_globals (abfd) = num_globals;
3009 return TRUE;
3012 /* Align to the maximum file alignment that could be required for any
3013 ELF data structure. */
3015 static INLINE file_ptr align_file_position
3016 PARAMS ((file_ptr, int));
3017 static INLINE file_ptr
3018 align_file_position (off, align)
3019 file_ptr off;
3020 int align;
3022 return (off + align - 1) & ~(align - 1);
3025 /* Assign a file position to a section, optionally aligning to the
3026 required section alignment. */
3028 INLINE file_ptr
3029 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
3030 Elf_Internal_Shdr *i_shdrp;
3031 file_ptr offset;
3032 bfd_boolean align;
3034 if (align)
3036 unsigned int al;
3038 al = i_shdrp->sh_addralign;
3039 if (al > 1)
3040 offset = BFD_ALIGN (offset, al);
3042 i_shdrp->sh_offset = offset;
3043 if (i_shdrp->bfd_section != NULL)
3044 i_shdrp->bfd_section->filepos = offset;
3045 if (i_shdrp->sh_type != SHT_NOBITS)
3046 offset += i_shdrp->sh_size;
3047 return offset;
3050 /* Compute the file positions we are going to put the sections at, and
3051 otherwise prepare to begin writing out the ELF file. If LINK_INFO
3052 is not NULL, this is being called by the ELF backend linker. */
3054 bfd_boolean
3055 _bfd_elf_compute_section_file_positions (abfd, link_info)
3056 bfd *abfd;
3057 struct bfd_link_info *link_info;
3059 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3060 bfd_boolean failed;
3061 struct bfd_strtab_hash *strtab;
3062 Elf_Internal_Shdr *shstrtab_hdr;
3064 if (abfd->output_has_begun)
3065 return TRUE;
3067 /* Do any elf backend specific processing first. */
3068 if (bed->elf_backend_begin_write_processing)
3069 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
3071 if (! prep_headers (abfd))
3072 return FALSE;
3074 /* Post process the headers if necessary. */
3075 if (bed->elf_backend_post_process_headers)
3076 (*bed->elf_backend_post_process_headers) (abfd, link_info);
3078 failed = FALSE;
3079 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
3080 if (failed)
3081 return FALSE;
3083 if (!assign_section_numbers (abfd))
3084 return FALSE;
3086 /* The backend linker builds symbol table information itself. */
3087 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3089 /* Non-zero if doing a relocatable link. */
3090 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
3092 if (! swap_out_syms (abfd, &strtab, relocatable_p))
3093 return FALSE;
3096 if (link_info == NULL)
3098 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed);
3099 if (failed)
3100 return FALSE;
3103 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
3104 /* sh_name was set in prep_headers. */
3105 shstrtab_hdr->sh_type = SHT_STRTAB;
3106 shstrtab_hdr->sh_flags = 0;
3107 shstrtab_hdr->sh_addr = 0;
3108 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
3109 shstrtab_hdr->sh_entsize = 0;
3110 shstrtab_hdr->sh_link = 0;
3111 shstrtab_hdr->sh_info = 0;
3112 /* sh_offset is set in assign_file_positions_except_relocs. */
3113 shstrtab_hdr->sh_addralign = 1;
3115 if (!assign_file_positions_except_relocs (abfd))
3116 return FALSE;
3118 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
3120 file_ptr off;
3121 Elf_Internal_Shdr *hdr;
3123 off = elf_tdata (abfd)->next_file_pos;
3125 hdr = &elf_tdata (abfd)->symtab_hdr;
3126 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3128 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
3129 if (hdr->sh_size != 0)
3130 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3132 hdr = &elf_tdata (abfd)->strtab_hdr;
3133 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
3135 elf_tdata (abfd)->next_file_pos = off;
3137 /* Now that we know where the .strtab section goes, write it
3138 out. */
3139 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
3140 || ! _bfd_stringtab_emit (abfd, strtab))
3141 return FALSE;
3142 _bfd_stringtab_free (strtab);
3145 abfd->output_has_begun = TRUE;
3147 return TRUE;
3150 /* Create a mapping from a set of sections to a program segment. */
3152 static INLINE struct elf_segment_map *
3153 make_mapping (abfd, sections, from, to, phdr)
3154 bfd *abfd;
3155 asection **sections;
3156 unsigned int from;
3157 unsigned int to;
3158 bfd_boolean phdr;
3160 struct elf_segment_map *m;
3161 unsigned int i;
3162 asection **hdrpp;
3163 bfd_size_type amt;
3165 amt = sizeof (struct elf_segment_map);
3166 amt += (to - from - 1) * sizeof (asection *);
3167 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3168 if (m == NULL)
3169 return NULL;
3170 m->next = NULL;
3171 m->p_type = PT_LOAD;
3172 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
3173 m->sections[i - from] = *hdrpp;
3174 m->count = to - from;
3176 if (from == 0 && phdr)
3178 /* Include the headers in the first PT_LOAD segment. */
3179 m->includes_filehdr = 1;
3180 m->includes_phdrs = 1;
3183 return m;
3186 /* Set up a mapping from BFD sections to program segments. */
3188 static bfd_boolean
3189 map_sections_to_segments (abfd)
3190 bfd *abfd;
3192 asection **sections = NULL;
3193 asection *s;
3194 unsigned int i;
3195 unsigned int count;
3196 struct elf_segment_map *mfirst;
3197 struct elf_segment_map **pm;
3198 struct elf_segment_map *m;
3199 asection *last_hdr;
3200 unsigned int phdr_index;
3201 bfd_vma maxpagesize;
3202 asection **hdrpp;
3203 bfd_boolean phdr_in_segment = TRUE;
3204 bfd_boolean writable;
3205 int tls_count = 0;
3206 asection *first_tls = NULL;
3207 asection *dynsec, *eh_frame_hdr;
3208 bfd_size_type amt;
3210 if (elf_tdata (abfd)->segment_map != NULL)
3211 return TRUE;
3213 if (bfd_count_sections (abfd) == 0)
3214 return TRUE;
3216 /* Select the allocated sections, and sort them. */
3218 amt = bfd_count_sections (abfd) * sizeof (asection *);
3219 sections = (asection **) bfd_malloc (amt);
3220 if (sections == NULL)
3221 goto error_return;
3223 i = 0;
3224 for (s = abfd->sections; s != NULL; s = s->next)
3226 if ((s->flags & SEC_ALLOC) != 0)
3228 sections[i] = s;
3229 ++i;
3232 BFD_ASSERT (i <= bfd_count_sections (abfd));
3233 count = i;
3235 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
3237 /* Build the mapping. */
3239 mfirst = NULL;
3240 pm = &mfirst;
3242 /* If we have a .interp section, then create a PT_PHDR segment for
3243 the program headers and a PT_INTERP segment for the .interp
3244 section. */
3245 s = bfd_get_section_by_name (abfd, ".interp");
3246 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3248 amt = sizeof (struct elf_segment_map);
3249 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3250 if (m == NULL)
3251 goto error_return;
3252 m->next = NULL;
3253 m->p_type = PT_PHDR;
3254 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
3255 m->p_flags = PF_R | PF_X;
3256 m->p_flags_valid = 1;
3257 m->includes_phdrs = 1;
3259 *pm = m;
3260 pm = &m->next;
3262 amt = sizeof (struct elf_segment_map);
3263 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3264 if (m == NULL)
3265 goto error_return;
3266 m->next = NULL;
3267 m->p_type = PT_INTERP;
3268 m->count = 1;
3269 m->sections[0] = s;
3271 *pm = m;
3272 pm = &m->next;
3275 /* Look through the sections. We put sections in the same program
3276 segment when the start of the second section can be placed within
3277 a few bytes of the end of the first section. */
3278 last_hdr = NULL;
3279 phdr_index = 0;
3280 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
3281 writable = FALSE;
3282 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
3283 if (dynsec != NULL
3284 && (dynsec->flags & SEC_LOAD) == 0)
3285 dynsec = NULL;
3287 /* Deal with -Ttext or something similar such that the first section
3288 is not adjacent to the program headers. This is an
3289 approximation, since at this point we don't know exactly how many
3290 program headers we will need. */
3291 if (count > 0)
3293 bfd_size_type phdr_size;
3295 phdr_size = elf_tdata (abfd)->program_header_size;
3296 if (phdr_size == 0)
3297 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3298 if ((abfd->flags & D_PAGED) == 0
3299 || sections[0]->lma < phdr_size
3300 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3301 phdr_in_segment = FALSE;
3304 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3306 asection *hdr;
3307 bfd_boolean new_segment;
3309 hdr = *hdrpp;
3311 /* See if this section and the last one will fit in the same
3312 segment. */
3314 if (last_hdr == NULL)
3316 /* If we don't have a segment yet, then we don't need a new
3317 one (we build the last one after this loop). */
3318 new_segment = FALSE;
3320 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3322 /* If this section has a different relation between the
3323 virtual address and the load address, then we need a new
3324 segment. */
3325 new_segment = TRUE;
3327 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3328 < BFD_ALIGN (hdr->lma, maxpagesize))
3330 /* If putting this section in this segment would force us to
3331 skip a page in the segment, then we need a new segment. */
3332 new_segment = TRUE;
3334 else if ((last_hdr->flags & SEC_LOAD) == 0
3335 && (hdr->flags & SEC_LOAD) != 0)
3337 /* We don't want to put a loadable section after a
3338 nonloadable section in the same segment. */
3339 new_segment = TRUE;
3341 else if ((abfd->flags & D_PAGED) == 0)
3343 /* If the file is not demand paged, which means that we
3344 don't require the sections to be correctly aligned in the
3345 file, then there is no other reason for a new segment. */
3346 new_segment = FALSE;
3348 else if (! writable
3349 && (hdr->flags & SEC_READONLY) == 0
3350 && (((last_hdr->lma + last_hdr->_raw_size - 1)
3351 & ~(maxpagesize - 1))
3352 != (hdr->lma & ~(maxpagesize - 1))))
3354 /* We don't want to put a writable section in a read only
3355 segment, unless they are on the same page in memory
3356 anyhow. We already know that the last section does not
3357 bring us past the current section on the page, so the
3358 only case in which the new section is not on the same
3359 page as the previous section is when the previous section
3360 ends precisely on a page boundary. */
3361 new_segment = TRUE;
3363 else
3365 /* Otherwise, we can use the same segment. */
3366 new_segment = FALSE;
3369 if (! new_segment)
3371 if ((hdr->flags & SEC_READONLY) == 0)
3372 writable = TRUE;
3373 last_hdr = hdr;
3374 continue;
3377 /* We need a new program segment. We must create a new program
3378 header holding all the sections from phdr_index until hdr. */
3380 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3381 if (m == NULL)
3382 goto error_return;
3384 *pm = m;
3385 pm = &m->next;
3387 if ((hdr->flags & SEC_READONLY) == 0)
3388 writable = TRUE;
3389 else
3390 writable = FALSE;
3392 last_hdr = hdr;
3393 phdr_index = i;
3394 phdr_in_segment = FALSE;
3397 /* Create a final PT_LOAD program segment. */
3398 if (last_hdr != NULL)
3400 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3401 if (m == NULL)
3402 goto error_return;
3404 *pm = m;
3405 pm = &m->next;
3408 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3409 if (dynsec != NULL)
3411 amt = sizeof (struct elf_segment_map);
3412 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3413 if (m == NULL)
3414 goto error_return;
3415 m->next = NULL;
3416 m->p_type = PT_DYNAMIC;
3417 m->count = 1;
3418 m->sections[0] = dynsec;
3420 *pm = m;
3421 pm = &m->next;
3424 /* For each loadable .note section, add a PT_NOTE segment. We don't
3425 use bfd_get_section_by_name, because if we link together
3426 nonloadable .note sections and loadable .note sections, we will
3427 generate two .note sections in the output file. FIXME: Using
3428 names for section types is bogus anyhow. */
3429 for (s = abfd->sections; s != NULL; s = s->next)
3431 if ((s->flags & SEC_LOAD) != 0
3432 && strncmp (s->name, ".note", 5) == 0)
3434 amt = sizeof (struct elf_segment_map);
3435 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3436 if (m == NULL)
3437 goto error_return;
3438 m->next = NULL;
3439 m->p_type = PT_NOTE;
3440 m->count = 1;
3441 m->sections[0] = s;
3443 *pm = m;
3444 pm = &m->next;
3446 if (s->flags & SEC_THREAD_LOCAL)
3448 if (! tls_count)
3449 first_tls = s;
3450 tls_count++;
3454 /* If there are any SHF_TLS output sections, add PT_TLS segment. */
3455 if (tls_count > 0)
3457 int i;
3459 amt = sizeof (struct elf_segment_map);
3460 amt += (tls_count - 1) * sizeof (asection *);
3461 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3462 if (m == NULL)
3463 goto error_return;
3464 m->next = NULL;
3465 m->p_type = PT_TLS;
3466 m->count = tls_count;
3467 /* Mandated PF_R. */
3468 m->p_flags = PF_R;
3469 m->p_flags_valid = 1;
3470 for (i = 0; i < tls_count; ++i)
3472 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL);
3473 m->sections[i] = first_tls;
3474 first_tls = first_tls->next;
3477 *pm = m;
3478 pm = &m->next;
3481 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3482 segment. */
3483 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr;
3484 if (eh_frame_hdr != NULL
3485 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0)
3487 amt = sizeof (struct elf_segment_map);
3488 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3489 if (m == NULL)
3490 goto error_return;
3491 m->next = NULL;
3492 m->p_type = PT_GNU_EH_FRAME;
3493 m->count = 1;
3494 m->sections[0] = eh_frame_hdr->output_section;
3496 *pm = m;
3497 pm = &m->next;
3500 free (sections);
3501 sections = NULL;
3503 elf_tdata (abfd)->segment_map = mfirst;
3504 return TRUE;
3506 error_return:
3507 if (sections != NULL)
3508 free (sections);
3509 return FALSE;
3512 /* Sort sections by address. */
3514 static int
3515 elf_sort_sections (arg1, arg2)
3516 const PTR arg1;
3517 const PTR arg2;
3519 const asection *sec1 = *(const asection **) arg1;
3520 const asection *sec2 = *(const asection **) arg2;
3522 /* Sort by LMA first, since this is the address used to
3523 place the section into a segment. */
3524 if (sec1->lma < sec2->lma)
3525 return -1;
3526 else if (sec1->lma > sec2->lma)
3527 return 1;
3529 /* Then sort by VMA. Normally the LMA and the VMA will be
3530 the same, and this will do nothing. */
3531 if (sec1->vma < sec2->vma)
3532 return -1;
3533 else if (sec1->vma > sec2->vma)
3534 return 1;
3536 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3538 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3540 if (TOEND (sec1))
3542 if (TOEND (sec2))
3544 /* If the indicies are the same, do not return 0
3545 here, but continue to try the next comparison. */
3546 if (sec1->target_index - sec2->target_index != 0)
3547 return sec1->target_index - sec2->target_index;
3549 else
3550 return 1;
3552 else if (TOEND (sec2))
3553 return -1;
3555 #undef TOEND
3557 /* Sort by size, to put zero sized sections
3558 before others at the same address. */
3560 if (sec1->_raw_size < sec2->_raw_size)
3561 return -1;
3562 if (sec1->_raw_size > sec2->_raw_size)
3563 return 1;
3565 return sec1->target_index - sec2->target_index;
3568 /* Assign file positions to the sections based on the mapping from
3569 sections to segments. This function also sets up some fields in
3570 the file header, and writes out the program headers. */
3572 static bfd_boolean
3573 assign_file_positions_for_segments (abfd)
3574 bfd *abfd;
3576 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3577 unsigned int count;
3578 struct elf_segment_map *m;
3579 unsigned int alloc;
3580 Elf_Internal_Phdr *phdrs;
3581 file_ptr off, voff;
3582 bfd_vma filehdr_vaddr, filehdr_paddr;
3583 bfd_vma phdrs_vaddr, phdrs_paddr;
3584 Elf_Internal_Phdr *p;
3585 bfd_size_type amt;
3587 if (elf_tdata (abfd)->segment_map == NULL)
3589 if (! map_sections_to_segments (abfd))
3590 return FALSE;
3592 else
3594 /* The placement algorithm assumes that non allocated sections are
3595 not in PT_LOAD segments. We ensure this here by removing such
3596 sections from the segment map. */
3597 for (m = elf_tdata (abfd)->segment_map;
3598 m != NULL;
3599 m = m->next)
3601 unsigned int new_count;
3602 unsigned int i;
3604 if (m->p_type != PT_LOAD)
3605 continue;
3607 new_count = 0;
3608 for (i = 0; i < m->count; i ++)
3610 if ((m->sections[i]->flags & SEC_ALLOC) != 0)
3612 if (i != new_count)
3613 m->sections[new_count] = m->sections[i];
3615 new_count ++;
3619 if (new_count != m->count)
3620 m->count = new_count;
3624 if (bed->elf_backend_modify_segment_map)
3626 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3627 return FALSE;
3630 count = 0;
3631 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3632 ++count;
3634 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3635 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3636 elf_elfheader (abfd)->e_phnum = count;
3638 if (count == 0)
3639 return TRUE;
3641 /* If we already counted the number of program segments, make sure
3642 that we allocated enough space. This happens when SIZEOF_HEADERS
3643 is used in a linker script. */
3644 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3645 if (alloc != 0 && count > alloc)
3647 ((*_bfd_error_handler)
3648 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3649 bfd_get_filename (abfd), alloc, count));
3650 bfd_set_error (bfd_error_bad_value);
3651 return FALSE;
3654 if (alloc == 0)
3655 alloc = count;
3657 amt = alloc * sizeof (Elf_Internal_Phdr);
3658 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3659 if (phdrs == NULL)
3660 return FALSE;
3662 off = bed->s->sizeof_ehdr;
3663 off += alloc * bed->s->sizeof_phdr;
3665 filehdr_vaddr = 0;
3666 filehdr_paddr = 0;
3667 phdrs_vaddr = 0;
3668 phdrs_paddr = 0;
3670 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3671 m != NULL;
3672 m = m->next, p++)
3674 unsigned int i;
3675 asection **secpp;
3677 /* If elf_segment_map is not from map_sections_to_segments, the
3678 sections may not be correctly ordered. NOTE: sorting should
3679 not be done to the PT_NOTE section of a corefile, which may
3680 contain several pseudo-sections artificially created by bfd.
3681 Sorting these pseudo-sections breaks things badly. */
3682 if (m->count > 1
3683 && !(elf_elfheader (abfd)->e_type == ET_CORE
3684 && m->p_type == PT_NOTE))
3685 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3686 elf_sort_sections);
3688 p->p_type = m->p_type;
3689 p->p_flags = m->p_flags;
3691 if (p->p_type == PT_LOAD
3692 && m->count > 0
3693 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3695 if ((abfd->flags & D_PAGED) != 0)
3696 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3697 else
3699 bfd_size_type align;
3701 align = 0;
3702 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3704 bfd_size_type secalign;
3706 secalign = bfd_get_section_alignment (abfd, *secpp);
3707 if (secalign > align)
3708 align = secalign;
3711 off += (m->sections[0]->vma - off) % (1 << align);
3715 if (m->count == 0)
3716 p->p_vaddr = 0;
3717 else
3718 p->p_vaddr = m->sections[0]->vma;
3720 if (m->p_paddr_valid)
3721 p->p_paddr = m->p_paddr;
3722 else if (m->count == 0)
3723 p->p_paddr = 0;
3724 else
3725 p->p_paddr = m->sections[0]->lma;
3727 if (p->p_type == PT_LOAD
3728 && (abfd->flags & D_PAGED) != 0)
3729 p->p_align = bed->maxpagesize;
3730 else if (m->count == 0)
3731 p->p_align = bed->s->file_align;
3732 else
3733 p->p_align = 0;
3735 p->p_offset = 0;
3736 p->p_filesz = 0;
3737 p->p_memsz = 0;
3739 if (m->includes_filehdr)
3741 if (! m->p_flags_valid)
3742 p->p_flags |= PF_R;
3743 p->p_offset = 0;
3744 p->p_filesz = bed->s->sizeof_ehdr;
3745 p->p_memsz = bed->s->sizeof_ehdr;
3746 if (m->count > 0)
3748 BFD_ASSERT (p->p_type == PT_LOAD);
3750 if (p->p_vaddr < (bfd_vma) off)
3752 (*_bfd_error_handler)
3753 (_("%s: Not enough room for program headers, try linking with -N"),
3754 bfd_get_filename (abfd));
3755 bfd_set_error (bfd_error_bad_value);
3756 return FALSE;
3759 p->p_vaddr -= off;
3760 if (! m->p_paddr_valid)
3761 p->p_paddr -= off;
3763 if (p->p_type == PT_LOAD)
3765 filehdr_vaddr = p->p_vaddr;
3766 filehdr_paddr = p->p_paddr;
3770 if (m->includes_phdrs)
3772 if (! m->p_flags_valid)
3773 p->p_flags |= PF_R;
3775 if (m->includes_filehdr)
3777 if (p->p_type == PT_LOAD)
3779 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3780 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3783 else
3785 p->p_offset = bed->s->sizeof_ehdr;
3787 if (m->count > 0)
3789 BFD_ASSERT (p->p_type == PT_LOAD);
3790 p->p_vaddr -= off - p->p_offset;
3791 if (! m->p_paddr_valid)
3792 p->p_paddr -= off - p->p_offset;
3795 if (p->p_type == PT_LOAD)
3797 phdrs_vaddr = p->p_vaddr;
3798 phdrs_paddr = p->p_paddr;
3800 else
3801 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3804 p->p_filesz += alloc * bed->s->sizeof_phdr;
3805 p->p_memsz += alloc * bed->s->sizeof_phdr;
3808 if (p->p_type == PT_LOAD
3809 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3811 if (! m->includes_filehdr && ! m->includes_phdrs)
3812 p->p_offset = off;
3813 else
3815 file_ptr adjust;
3817 adjust = off - (p->p_offset + p->p_filesz);
3818 p->p_filesz += adjust;
3819 p->p_memsz += adjust;
3823 voff = off;
3825 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3827 asection *sec;
3828 flagword flags;
3829 bfd_size_type align;
3831 sec = *secpp;
3832 flags = sec->flags;
3833 align = 1 << bfd_get_section_alignment (abfd, sec);
3835 /* The section may have artificial alignment forced by a
3836 link script. Notice this case by the gap between the
3837 cumulative phdr lma and the section's lma. */
3838 if (p->p_paddr + p->p_memsz < sec->lma)
3840 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3842 p->p_memsz += adjust;
3843 off += adjust;
3844 voff += adjust;
3845 if ((flags & SEC_LOAD) != 0)
3846 p->p_filesz += adjust;
3849 if (p->p_type == PT_LOAD)
3851 bfd_signed_vma adjust;
3853 if ((flags & SEC_LOAD) != 0)
3855 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3856 if (adjust < 0)
3857 adjust = 0;
3859 else if ((flags & SEC_ALLOC) != 0)
3861 /* The section VMA must equal the file position
3862 modulo the page size. FIXME: I'm not sure if
3863 this adjustment is really necessary. We used to
3864 not have the SEC_LOAD case just above, and then
3865 this was necessary, but now I'm not sure. */
3866 if ((abfd->flags & D_PAGED) != 0)
3867 adjust = (sec->vma - voff) % bed->maxpagesize;
3868 else
3869 adjust = (sec->vma - voff) % align;
3871 else
3872 adjust = 0;
3874 if (adjust != 0)
3876 if (i == 0)
3878 (* _bfd_error_handler) (_("\
3879 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3880 bfd_section_name (abfd, sec),
3881 sec->lma,
3882 p->p_paddr);
3883 return FALSE;
3885 p->p_memsz += adjust;
3886 off += adjust;
3887 voff += adjust;
3888 if ((flags & SEC_LOAD) != 0)
3889 p->p_filesz += adjust;
3892 sec->filepos = off;
3894 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3895 used in a linker script we may have a section with
3896 SEC_LOAD clear but which is supposed to have
3897 contents. */
3898 if ((flags & SEC_LOAD) != 0
3899 || (flags & SEC_HAS_CONTENTS) != 0)
3900 off += sec->_raw_size;
3902 if ((flags & SEC_ALLOC) != 0)
3903 voff += sec->_raw_size;
3906 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3908 /* The actual "note" segment has i == 0.
3909 This is the one that actually contains everything. */
3910 if (i == 0)
3912 sec->filepos = off;
3913 p->p_filesz = sec->_raw_size;
3914 off += sec->_raw_size;
3915 voff = off;
3917 else
3919 /* Fake sections -- don't need to be written. */
3920 sec->filepos = 0;
3921 sec->_raw_size = 0;
3922 flags = sec->flags = 0;
3924 p->p_memsz = 0;
3925 p->p_align = 1;
3927 else
3929 p->p_memsz += sec->_raw_size;
3931 if ((flags & SEC_LOAD) != 0)
3932 p->p_filesz += sec->_raw_size;
3934 if (p->p_type == PT_TLS
3935 && sec->_raw_size == 0
3936 && (sec->flags & SEC_HAS_CONTENTS) == 0)
3938 struct bfd_link_order *o;
3939 bfd_vma tbss_size = 0;
3941 for (o = sec->link_order_head; o != NULL; o = o->next)
3942 if (tbss_size < o->offset + o->size)
3943 tbss_size = o->offset + o->size;
3945 p->p_memsz += tbss_size;
3948 if (align > p->p_align
3949 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3950 p->p_align = align;
3953 if (! m->p_flags_valid)
3955 p->p_flags |= PF_R;
3956 if ((flags & SEC_CODE) != 0)
3957 p->p_flags |= PF_X;
3958 if ((flags & SEC_READONLY) == 0)
3959 p->p_flags |= PF_W;
3964 /* Now that we have set the section file positions, we can set up
3965 the file positions for the non PT_LOAD segments. */
3966 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3967 m != NULL;
3968 m = m->next, p++)
3970 if (p->p_type != PT_LOAD && m->count > 0)
3972 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3973 p->p_offset = m->sections[0]->filepos;
3975 if (m->count == 0)
3977 if (m->includes_filehdr)
3979 p->p_vaddr = filehdr_vaddr;
3980 if (! m->p_paddr_valid)
3981 p->p_paddr = filehdr_paddr;
3983 else if (m->includes_phdrs)
3985 p->p_vaddr = phdrs_vaddr;
3986 if (! m->p_paddr_valid)
3987 p->p_paddr = phdrs_paddr;
3992 /* Clear out any program headers we allocated but did not use. */
3993 for (; count < alloc; count++, p++)
3995 memset (p, 0, sizeof *p);
3996 p->p_type = PT_NULL;
3999 elf_tdata (abfd)->phdr = phdrs;
4001 elf_tdata (abfd)->next_file_pos = off;
4003 /* Write out the program headers. */
4004 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
4005 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
4006 return FALSE;
4008 return TRUE;
4011 /* Get the size of the program header.
4013 If this is called by the linker before any of the section VMA's are set, it
4014 can't calculate the correct value for a strange memory layout. This only
4015 happens when SIZEOF_HEADERS is used in a linker script. In this case,
4016 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
4017 data segment (exclusive of .interp and .dynamic).
4019 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
4020 will be two segments. */
4022 static bfd_size_type
4023 get_program_header_size (abfd)
4024 bfd *abfd;
4026 size_t segs;
4027 asection *s;
4028 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4030 /* We can't return a different result each time we're called. */
4031 if (elf_tdata (abfd)->program_header_size != 0)
4032 return elf_tdata (abfd)->program_header_size;
4034 if (elf_tdata (abfd)->segment_map != NULL)
4036 struct elf_segment_map *m;
4038 segs = 0;
4039 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
4040 ++segs;
4041 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4042 return elf_tdata (abfd)->program_header_size;
4045 /* Assume we will need exactly two PT_LOAD segments: one for text
4046 and one for data. */
4047 segs = 2;
4049 s = bfd_get_section_by_name (abfd, ".interp");
4050 if (s != NULL && (s->flags & SEC_LOAD) != 0)
4052 /* If we have a loadable interpreter section, we need a
4053 PT_INTERP segment. In this case, assume we also need a
4054 PT_PHDR segment, although that may not be true for all
4055 targets. */
4056 segs += 2;
4059 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
4061 /* We need a PT_DYNAMIC segment. */
4062 ++segs;
4065 if (elf_tdata (abfd)->eh_frame_hdr)
4067 /* We need a PT_GNU_EH_FRAME segment. */
4068 ++segs;
4071 for (s = abfd->sections; s != NULL; s = s->next)
4073 if ((s->flags & SEC_LOAD) != 0
4074 && strncmp (s->name, ".note", 5) == 0)
4076 /* We need a PT_NOTE segment. */
4077 ++segs;
4081 for (s = abfd->sections; s != NULL; s = s->next)
4083 if (s->flags & SEC_THREAD_LOCAL)
4085 /* We need a PT_TLS segment. */
4086 ++segs;
4087 break;
4091 /* Let the backend count up any program headers it might need. */
4092 if (bed->elf_backend_additional_program_headers)
4094 int a;
4096 a = (*bed->elf_backend_additional_program_headers) (abfd);
4097 if (a == -1)
4098 abort ();
4099 segs += a;
4102 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
4103 return elf_tdata (abfd)->program_header_size;
4106 /* Work out the file positions of all the sections. This is called by
4107 _bfd_elf_compute_section_file_positions. All the section sizes and
4108 VMAs must be known before this is called.
4110 We do not consider reloc sections at this point, unless they form
4111 part of the loadable image. Reloc sections are assigned file
4112 positions in assign_file_positions_for_relocs, which is called by
4113 write_object_contents and final_link.
4115 We also don't set the positions of the .symtab and .strtab here. */
4117 static bfd_boolean
4118 assign_file_positions_except_relocs (abfd)
4119 bfd *abfd;
4121 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
4122 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
4123 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
4124 unsigned int num_sec = elf_numsections (abfd);
4125 file_ptr off;
4126 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4128 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
4129 && bfd_get_format (abfd) != bfd_core)
4131 Elf_Internal_Shdr **hdrpp;
4132 unsigned int i;
4134 /* Start after the ELF header. */
4135 off = i_ehdrp->e_ehsize;
4137 /* We are not creating an executable, which means that we are
4138 not creating a program header, and that the actual order of
4139 the sections in the file is unimportant. */
4140 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4142 Elf_Internal_Shdr *hdr;
4144 hdr = *hdrpp;
4145 if (hdr->sh_type == SHT_REL
4146 || hdr->sh_type == SHT_RELA
4147 || i == tdata->symtab_section
4148 || i == tdata->symtab_shndx_section
4149 || i == tdata->strtab_section)
4151 hdr->sh_offset = -1;
4153 else
4154 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4156 if (i == SHN_LORESERVE - 1)
4158 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4159 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4163 else
4165 unsigned int i;
4166 Elf_Internal_Shdr **hdrpp;
4168 /* Assign file positions for the loaded sections based on the
4169 assignment of sections to segments. */
4170 if (! assign_file_positions_for_segments (abfd))
4171 return FALSE;
4173 /* Assign file positions for the other sections. */
4175 off = elf_tdata (abfd)->next_file_pos;
4176 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
4178 Elf_Internal_Shdr *hdr;
4180 hdr = *hdrpp;
4181 if (hdr->bfd_section != NULL
4182 && hdr->bfd_section->filepos != 0)
4183 hdr->sh_offset = hdr->bfd_section->filepos;
4184 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
4186 ((*_bfd_error_handler)
4187 (_("%s: warning: allocated section `%s' not in segment"),
4188 bfd_get_filename (abfd),
4189 (hdr->bfd_section == NULL
4190 ? "*unknown*"
4191 : hdr->bfd_section->name)));
4192 if ((abfd->flags & D_PAGED) != 0)
4193 off += (hdr->sh_addr - off) % bed->maxpagesize;
4194 else
4195 off += (hdr->sh_addr - off) % hdr->sh_addralign;
4196 off = _bfd_elf_assign_file_position_for_section (hdr, off,
4197 FALSE);
4199 else if (hdr->sh_type == SHT_REL
4200 || hdr->sh_type == SHT_RELA
4201 || hdr == i_shdrpp[tdata->symtab_section]
4202 || hdr == i_shdrpp[tdata->symtab_shndx_section]
4203 || hdr == i_shdrpp[tdata->strtab_section])
4204 hdr->sh_offset = -1;
4205 else
4206 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE);
4208 if (i == SHN_LORESERVE - 1)
4210 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4211 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4216 /* Place the section headers. */
4217 off = align_file_position (off, bed->s->file_align);
4218 i_ehdrp->e_shoff = off;
4219 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
4221 elf_tdata (abfd)->next_file_pos = off;
4223 return TRUE;
4226 static bfd_boolean
4227 prep_headers (abfd)
4228 bfd *abfd;
4230 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
4231 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
4232 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
4233 struct elf_strtab_hash *shstrtab;
4234 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4236 i_ehdrp = elf_elfheader (abfd);
4237 i_shdrp = elf_elfsections (abfd);
4239 shstrtab = _bfd_elf_strtab_init ();
4240 if (shstrtab == NULL)
4241 return FALSE;
4243 elf_shstrtab (abfd) = shstrtab;
4245 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
4246 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
4247 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
4248 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
4250 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
4251 i_ehdrp->e_ident[EI_DATA] =
4252 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
4253 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
4255 if ((abfd->flags & DYNAMIC) != 0)
4256 i_ehdrp->e_type = ET_DYN;
4257 else if ((abfd->flags & EXEC_P) != 0)
4258 i_ehdrp->e_type = ET_EXEC;
4259 else if (bfd_get_format (abfd) == bfd_core)
4260 i_ehdrp->e_type = ET_CORE;
4261 else
4262 i_ehdrp->e_type = ET_REL;
4264 switch (bfd_get_arch (abfd))
4266 case bfd_arch_unknown:
4267 i_ehdrp->e_machine = EM_NONE;
4268 break;
4270 /* There used to be a long list of cases here, each one setting
4271 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
4272 in the corresponding bfd definition. To avoid duplication,
4273 the switch was removed. Machines that need special handling
4274 can generally do it in elf_backend_final_write_processing(),
4275 unless they need the information earlier than the final write.
4276 Such need can generally be supplied by replacing the tests for
4277 e_machine with the conditions used to determine it. */
4278 default:
4279 if (get_elf_backend_data (abfd) != NULL)
4280 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
4281 else
4282 i_ehdrp->e_machine = EM_NONE;
4285 i_ehdrp->e_version = bed->s->ev_current;
4286 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
4288 /* No program header, for now. */
4289 i_ehdrp->e_phoff = 0;
4290 i_ehdrp->e_phentsize = 0;
4291 i_ehdrp->e_phnum = 0;
4293 /* Each bfd section is section header entry. */
4294 i_ehdrp->e_entry = bfd_get_start_address (abfd);
4295 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
4297 /* If we're building an executable, we'll need a program header table. */
4298 if (abfd->flags & EXEC_P)
4300 /* It all happens later. */
4301 #if 0
4302 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
4304 /* elf_build_phdrs() returns a (NULL-terminated) array of
4305 Elf_Internal_Phdrs. */
4306 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
4307 i_ehdrp->e_phoff = outbase;
4308 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
4309 #endif
4311 else
4313 i_ehdrp->e_phentsize = 0;
4314 i_phdrp = 0;
4315 i_ehdrp->e_phoff = 0;
4318 elf_tdata (abfd)->symtab_hdr.sh_name =
4319 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE);
4320 elf_tdata (abfd)->strtab_hdr.sh_name =
4321 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE);
4322 elf_tdata (abfd)->shstrtab_hdr.sh_name =
4323 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE);
4324 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4325 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
4326 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
4327 return FALSE;
4329 return TRUE;
4332 /* Assign file positions for all the reloc sections which are not part
4333 of the loadable file image. */
4335 void
4336 _bfd_elf_assign_file_positions_for_relocs (abfd)
4337 bfd *abfd;
4339 file_ptr off;
4340 unsigned int i, num_sec;
4341 Elf_Internal_Shdr **shdrpp;
4343 off = elf_tdata (abfd)->next_file_pos;
4345 num_sec = elf_numsections (abfd);
4346 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
4348 Elf_Internal_Shdr *shdrp;
4350 shdrp = *shdrpp;
4351 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
4352 && shdrp->sh_offset == -1)
4353 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE);
4356 elf_tdata (abfd)->next_file_pos = off;
4359 bfd_boolean
4360 _bfd_elf_write_object_contents (abfd)
4361 bfd *abfd;
4363 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4364 Elf_Internal_Ehdr *i_ehdrp;
4365 Elf_Internal_Shdr **i_shdrp;
4366 bfd_boolean failed;
4367 unsigned int count, num_sec;
4369 if (! abfd->output_has_begun
4370 && ! _bfd_elf_compute_section_file_positions
4371 (abfd, (struct bfd_link_info *) NULL))
4372 return FALSE;
4374 i_shdrp = elf_elfsections (abfd);
4375 i_ehdrp = elf_elfheader (abfd);
4377 failed = FALSE;
4378 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4379 if (failed)
4380 return FALSE;
4382 _bfd_elf_assign_file_positions_for_relocs (abfd);
4384 /* After writing the headers, we need to write the sections too... */
4385 num_sec = elf_numsections (abfd);
4386 for (count = 1; count < num_sec; count++)
4388 if (bed->elf_backend_section_processing)
4389 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4390 if (i_shdrp[count]->contents)
4392 bfd_size_type amt = i_shdrp[count]->sh_size;
4394 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4395 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4396 return FALSE;
4398 if (count == SHN_LORESERVE - 1)
4399 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4402 /* Write out the section header names. */
4403 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4404 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4405 return FALSE;
4407 if (bed->elf_backend_final_write_processing)
4408 (*bed->elf_backend_final_write_processing) (abfd,
4409 elf_tdata (abfd)->linker);
4411 return bed->s->write_shdrs_and_ehdr (abfd);
4414 bfd_boolean
4415 _bfd_elf_write_corefile_contents (abfd)
4416 bfd *abfd;
4418 /* Hopefully this can be done just like an object file. */
4419 return _bfd_elf_write_object_contents (abfd);
4422 /* Given a section, search the header to find them. */
4425 _bfd_elf_section_from_bfd_section (abfd, asect)
4426 bfd *abfd;
4427 struct sec *asect;
4429 struct elf_backend_data *bed;
4430 int index;
4432 if (elf_section_data (asect) != NULL
4433 && elf_section_data (asect)->this_idx != 0)
4434 return elf_section_data (asect)->this_idx;
4436 if (bfd_is_abs_section (asect))
4437 index = SHN_ABS;
4438 else if (bfd_is_com_section (asect))
4439 index = SHN_COMMON;
4440 else if (bfd_is_und_section (asect))
4441 index = SHN_UNDEF;
4442 else
4444 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4445 int maxindex = elf_numsections (abfd);
4447 for (index = 1; index < maxindex; index++)
4449 Elf_Internal_Shdr *hdr = i_shdrp[index];
4451 if (hdr != NULL && hdr->bfd_section == asect)
4452 return index;
4454 index = -1;
4457 bed = get_elf_backend_data (abfd);
4458 if (bed->elf_backend_section_from_bfd_section)
4460 int retval = index;
4462 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4463 return retval;
4466 if (index == -1)
4467 bfd_set_error (bfd_error_nonrepresentable_section);
4469 return index;
4472 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4473 on error. */
4476 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4477 bfd *abfd;
4478 asymbol **asym_ptr_ptr;
4480 asymbol *asym_ptr = *asym_ptr_ptr;
4481 int idx;
4482 flagword flags = asym_ptr->flags;
4484 /* When gas creates relocations against local labels, it creates its
4485 own symbol for the section, but does put the symbol into the
4486 symbol chain, so udata is 0. When the linker is generating
4487 relocatable output, this section symbol may be for one of the
4488 input sections rather than the output section. */
4489 if (asym_ptr->udata.i == 0
4490 && (flags & BSF_SECTION_SYM)
4491 && asym_ptr->section)
4493 int indx;
4495 if (asym_ptr->section->output_section != NULL)
4496 indx = asym_ptr->section->output_section->index;
4497 else
4498 indx = asym_ptr->section->index;
4499 if (indx < elf_num_section_syms (abfd)
4500 && elf_section_syms (abfd)[indx] != NULL)
4501 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4504 idx = asym_ptr->udata.i;
4506 if (idx == 0)
4508 /* This case can occur when using --strip-symbol on a symbol
4509 which is used in a relocation entry. */
4510 (*_bfd_error_handler)
4511 (_("%s: symbol `%s' required but not present"),
4512 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4513 bfd_set_error (bfd_error_no_symbols);
4514 return -1;
4517 #if DEBUG & 4
4519 fprintf (stderr,
4520 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
4521 (long) asym_ptr, asym_ptr->name, idx, flags,
4522 elf_symbol_flags (flags));
4523 fflush (stderr);
4525 #endif
4527 return idx;
4530 /* Copy private BFD data. This copies any program header information. */
4532 static bfd_boolean
4533 copy_private_bfd_data (ibfd, obfd)
4534 bfd *ibfd;
4535 bfd *obfd;
4537 Elf_Internal_Ehdr *iehdr;
4538 struct elf_segment_map *map;
4539 struct elf_segment_map *map_first;
4540 struct elf_segment_map **pointer_to_map;
4541 Elf_Internal_Phdr *segment;
4542 asection *section;
4543 unsigned int i;
4544 unsigned int num_segments;
4545 bfd_boolean phdr_included = FALSE;
4546 bfd_vma maxpagesize;
4547 struct elf_segment_map *phdr_adjust_seg = NULL;
4548 unsigned int phdr_adjust_num = 0;
4549 struct elf_backend_data *bed;
4551 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4552 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4553 return TRUE;
4555 if (elf_tdata (ibfd)->phdr == NULL)
4556 return TRUE;
4558 bed = get_elf_backend_data (ibfd);
4559 iehdr = elf_elfheader (ibfd);
4561 map_first = NULL;
4562 pointer_to_map = &map_first;
4564 num_segments = elf_elfheader (ibfd)->e_phnum;
4565 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4567 /* Returns the end address of the segment + 1. */
4568 #define SEGMENT_END(segment, start) \
4569 (start + (segment->p_memsz > segment->p_filesz \
4570 ? segment->p_memsz : segment->p_filesz))
4572 /* Returns TRUE if the given section is contained within
4573 the given segment. VMA addresses are compared. */
4574 #define IS_CONTAINED_BY_VMA(section, segment) \
4575 (section->vma >= segment->p_vaddr \
4576 && (section->vma + section->_raw_size \
4577 <= (SEGMENT_END (segment, segment->p_vaddr))))
4579 /* Returns TRUE if the given section is contained within
4580 the given segment. LMA addresses are compared. */
4581 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4582 (section->lma >= base \
4583 && (section->lma + section->_raw_size \
4584 <= SEGMENT_END (segment, base)))
4586 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4587 #define IS_COREFILE_NOTE(p, s) \
4588 (p->p_type == PT_NOTE \
4589 && bfd_get_format (ibfd) == bfd_core \
4590 && s->vma == 0 && s->lma == 0 \
4591 && (bfd_vma) s->filepos >= p->p_offset \
4592 && ((bfd_vma) s->filepos + s->_raw_size \
4593 <= p->p_offset + p->p_filesz))
4595 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4596 linker, which generates a PT_INTERP section with p_vaddr and
4597 p_memsz set to 0. */
4598 #define IS_SOLARIS_PT_INTERP(p, s) \
4599 (p->p_vaddr == 0 \
4600 && p->p_paddr == 0 \
4601 && p->p_memsz == 0 \
4602 && p->p_filesz > 0 \
4603 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4604 && s->_raw_size > 0 \
4605 && (bfd_vma) s->filepos >= p->p_offset \
4606 && ((bfd_vma) s->filepos + s->_raw_size \
4607 <= p->p_offset + p->p_filesz))
4609 /* Decide if the given section should be included in the given segment.
4610 A section will be included if:
4611 1. It is within the address space of the segment -- we use the LMA
4612 if that is set for the segment and the VMA otherwise,
4613 2. It is an allocated segment,
4614 3. There is an output section associated with it,
4615 4. The section has not already been allocated to a previous segment. */
4616 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \
4617 ((((segment->p_paddr \
4618 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4619 : IS_CONTAINED_BY_VMA (section, segment)) \
4620 && (section->flags & SEC_ALLOC) != 0) \
4621 || IS_COREFILE_NOTE (segment, section)) \
4622 && section->output_section != NULL \
4623 && ! section->segment_mark)
4625 /* Returns TRUE iff seg1 starts after the end of seg2. */
4626 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4627 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4629 /* Returns TRUE iff seg1 and seg2 overlap. */
4630 #define SEGMENT_OVERLAPS(seg1, seg2) \
4631 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) \
4632 || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4634 /* Initialise the segment mark field. */
4635 for (section = ibfd->sections; section != NULL; section = section->next)
4636 section->segment_mark = FALSE;
4638 /* Scan through the segments specified in the program header
4639 of the input BFD. For this first scan we look for overlaps
4640 in the loadable segments. These can be created by weird
4641 parameters to objcopy. Also, fix some solaris weirdness. */
4642 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4643 i < num_segments;
4644 i++, segment++)
4646 unsigned int j;
4647 Elf_Internal_Phdr *segment2;
4649 if (segment->p_type == PT_INTERP)
4650 for (section = ibfd->sections; section; section = section->next)
4651 if (IS_SOLARIS_PT_INTERP (segment, section))
4653 /* Mininal change so that the normal section to segment
4654 assigment code will work. */
4655 segment->p_vaddr = section->vma;
4656 break;
4659 if (segment->p_type != PT_LOAD)
4660 continue;
4662 /* Determine if this segment overlaps any previous segments. */
4663 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4665 bfd_signed_vma extra_length;
4667 if (segment2->p_type != PT_LOAD
4668 || ! SEGMENT_OVERLAPS (segment, segment2))
4669 continue;
4671 /* Merge the two segments together. */
4672 if (segment2->p_vaddr < segment->p_vaddr)
4674 /* Extend SEGMENT2 to include SEGMENT and then delete
4675 SEGMENT. */
4676 extra_length =
4677 SEGMENT_END (segment, segment->p_vaddr)
4678 - SEGMENT_END (segment2, segment2->p_vaddr);
4680 if (extra_length > 0)
4682 segment2->p_memsz += extra_length;
4683 segment2->p_filesz += extra_length;
4686 segment->p_type = PT_NULL;
4688 /* Since we have deleted P we must restart the outer loop. */
4689 i = 0;
4690 segment = elf_tdata (ibfd)->phdr;
4691 break;
4693 else
4695 /* Extend SEGMENT to include SEGMENT2 and then delete
4696 SEGMENT2. */
4697 extra_length =
4698 SEGMENT_END (segment2, segment2->p_vaddr)
4699 - SEGMENT_END (segment, segment->p_vaddr);
4701 if (extra_length > 0)
4703 segment->p_memsz += extra_length;
4704 segment->p_filesz += extra_length;
4707 segment2->p_type = PT_NULL;
4712 /* The second scan attempts to assign sections to segments. */
4713 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4714 i < num_segments;
4715 i ++, segment ++)
4717 unsigned int section_count;
4718 asection ** sections;
4719 asection * output_section;
4720 unsigned int isec;
4721 bfd_vma matching_lma;
4722 bfd_vma suggested_lma;
4723 unsigned int j;
4724 bfd_size_type amt;
4726 if (segment->p_type == PT_NULL)
4727 continue;
4729 /* Compute how many sections might be placed into this segment. */
4730 section_count = 0;
4731 for (section = ibfd->sections; section != NULL; section = section->next)
4732 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4733 ++section_count;
4735 /* Allocate a segment map big enough to contain all of the
4736 sections we have selected. */
4737 amt = sizeof (struct elf_segment_map);
4738 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4739 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4740 if (map == NULL)
4741 return FALSE;
4743 /* Initialise the fields of the segment map. Default to
4744 using the physical address of the segment in the input BFD. */
4745 map->next = NULL;
4746 map->p_type = segment->p_type;
4747 map->p_flags = segment->p_flags;
4748 map->p_flags_valid = 1;
4749 map->p_paddr = segment->p_paddr;
4750 map->p_paddr_valid = 1;
4752 /* Determine if this segment contains the ELF file header
4753 and if it contains the program headers themselves. */
4754 map->includes_filehdr = (segment->p_offset == 0
4755 && segment->p_filesz >= iehdr->e_ehsize);
4757 map->includes_phdrs = 0;
4759 if (! phdr_included || segment->p_type != PT_LOAD)
4761 map->includes_phdrs =
4762 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4763 && (segment->p_offset + segment->p_filesz
4764 >= ((bfd_vma) iehdr->e_phoff
4765 + iehdr->e_phnum * iehdr->e_phentsize)));
4767 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4768 phdr_included = TRUE;
4771 if (section_count == 0)
4773 /* Special segments, such as the PT_PHDR segment, may contain
4774 no sections, but ordinary, loadable segments should contain
4775 something. They are allowed by the ELF spec however, so only
4776 a warning is produced. */
4777 if (segment->p_type == PT_LOAD)
4778 (*_bfd_error_handler)
4779 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"),
4780 bfd_archive_filename (ibfd));
4782 map->count = 0;
4783 *pointer_to_map = map;
4784 pointer_to_map = &map->next;
4786 continue;
4789 /* Now scan the sections in the input BFD again and attempt
4790 to add their corresponding output sections to the segment map.
4791 The problem here is how to handle an output section which has
4792 been moved (ie had its LMA changed). There are four possibilities:
4794 1. None of the sections have been moved.
4795 In this case we can continue to use the segment LMA from the
4796 input BFD.
4798 2. All of the sections have been moved by the same amount.
4799 In this case we can change the segment's LMA to match the LMA
4800 of the first section.
4802 3. Some of the sections have been moved, others have not.
4803 In this case those sections which have not been moved can be
4804 placed in the current segment which will have to have its size,
4805 and possibly its LMA changed, and a new segment or segments will
4806 have to be created to contain the other sections.
4808 4. The sections have been moved, but not be the same amount.
4809 In this case we can change the segment's LMA to match the LMA
4810 of the first section and we will have to create a new segment
4811 or segments to contain the other sections.
4813 In order to save time, we allocate an array to hold the section
4814 pointers that we are interested in. As these sections get assigned
4815 to a segment, they are removed from this array. */
4817 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here
4818 to work around this long long bug. */
4819 amt = section_count * sizeof (asection *);
4820 sections = (asection **) bfd_malloc (amt);
4821 if (sections == NULL)
4822 return FALSE;
4824 /* Step One: Scan for segment vs section LMA conflicts.
4825 Also add the sections to the section array allocated above.
4826 Also add the sections to the current segment. In the common
4827 case, where the sections have not been moved, this means that
4828 we have completely filled the segment, and there is nothing
4829 more to do. */
4830 isec = 0;
4831 matching_lma = 0;
4832 suggested_lma = 0;
4834 for (j = 0, section = ibfd->sections;
4835 section != NULL;
4836 section = section->next)
4838 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed))
4840 output_section = section->output_section;
4842 sections[j ++] = section;
4844 /* The Solaris native linker always sets p_paddr to 0.
4845 We try to catch that case here, and set it to the
4846 correct value. Note - some backends require that
4847 p_paddr be left as zero. */
4848 if (segment->p_paddr == 0
4849 && segment->p_vaddr != 0
4850 && (! bed->want_p_paddr_set_to_zero)
4851 && isec == 0
4852 && output_section->lma != 0
4853 && (output_section->vma == (segment->p_vaddr
4854 + (map->includes_filehdr
4855 ? iehdr->e_ehsize
4856 : 0)
4857 + (map->includes_phdrs
4858 ? (iehdr->e_phnum
4859 * iehdr->e_phentsize)
4860 : 0))))
4861 map->p_paddr = segment->p_vaddr;
4863 /* Match up the physical address of the segment with the
4864 LMA address of the output section. */
4865 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4866 || IS_COREFILE_NOTE (segment, section)
4867 || (bed->want_p_paddr_set_to_zero &&
4868 IS_CONTAINED_BY_VMA (output_section, segment))
4871 if (matching_lma == 0)
4872 matching_lma = output_section->lma;
4874 /* We assume that if the section fits within the segment
4875 then it does not overlap any other section within that
4876 segment. */
4877 map->sections[isec ++] = output_section;
4879 else if (suggested_lma == 0)
4880 suggested_lma = output_section->lma;
4884 BFD_ASSERT (j == section_count);
4886 /* Step Two: Adjust the physical address of the current segment,
4887 if necessary. */
4888 if (isec == section_count)
4890 /* All of the sections fitted within the segment as currently
4891 specified. This is the default case. Add the segment to
4892 the list of built segments and carry on to process the next
4893 program header in the input BFD. */
4894 map->count = section_count;
4895 *pointer_to_map = map;
4896 pointer_to_map = &map->next;
4898 free (sections);
4899 continue;
4901 else
4903 if (matching_lma != 0)
4905 /* At least one section fits inside the current segment.
4906 Keep it, but modify its physical address to match the
4907 LMA of the first section that fitted. */
4908 map->p_paddr = matching_lma;
4910 else
4912 /* None of the sections fitted inside the current segment.
4913 Change the current segment's physical address to match
4914 the LMA of the first section. */
4915 map->p_paddr = suggested_lma;
4918 /* Offset the segment physical address from the lma
4919 to allow for space taken up by elf headers. */
4920 if (map->includes_filehdr)
4921 map->p_paddr -= iehdr->e_ehsize;
4923 if (map->includes_phdrs)
4925 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4927 /* iehdr->e_phnum is just an estimate of the number
4928 of program headers that we will need. Make a note
4929 here of the number we used and the segment we chose
4930 to hold these headers, so that we can adjust the
4931 offset when we know the correct value. */
4932 phdr_adjust_num = iehdr->e_phnum;
4933 phdr_adjust_seg = map;
4937 /* Step Three: Loop over the sections again, this time assigning
4938 those that fit to the current segment and removing them from the
4939 sections array; but making sure not to leave large gaps. Once all
4940 possible sections have been assigned to the current segment it is
4941 added to the list of built segments and if sections still remain
4942 to be assigned, a new segment is constructed before repeating
4943 the loop. */
4944 isec = 0;
4947 map->count = 0;
4948 suggested_lma = 0;
4950 /* Fill the current segment with sections that fit. */
4951 for (j = 0; j < section_count; j++)
4953 section = sections[j];
4955 if (section == NULL)
4956 continue;
4958 output_section = section->output_section;
4960 BFD_ASSERT (output_section != NULL);
4962 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4963 || IS_COREFILE_NOTE (segment, section))
4965 if (map->count == 0)
4967 /* If the first section in a segment does not start at
4968 the beginning of the segment, then something is
4969 wrong. */
4970 if (output_section->lma !=
4971 (map->p_paddr
4972 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4973 + (map->includes_phdrs
4974 ? iehdr->e_phnum * iehdr->e_phentsize
4975 : 0)))
4976 abort ();
4978 else
4980 asection * prev_sec;
4982 prev_sec = map->sections[map->count - 1];
4984 /* If the gap between the end of the previous section
4985 and the start of this section is more than
4986 maxpagesize then we need to start a new segment. */
4987 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4988 maxpagesize)
4989 < BFD_ALIGN (output_section->lma, maxpagesize))
4990 || ((prev_sec->lma + prev_sec->_raw_size)
4991 > output_section->lma))
4993 if (suggested_lma == 0)
4994 suggested_lma = output_section->lma;
4996 continue;
5000 map->sections[map->count++] = output_section;
5001 ++isec;
5002 sections[j] = NULL;
5003 section->segment_mark = TRUE;
5005 else if (suggested_lma == 0)
5006 suggested_lma = output_section->lma;
5009 BFD_ASSERT (map->count > 0);
5011 /* Add the current segment to the list of built segments. */
5012 *pointer_to_map = map;
5013 pointer_to_map = &map->next;
5015 if (isec < section_count)
5017 /* We still have not allocated all of the sections to
5018 segments. Create a new segment here, initialise it
5019 and carry on looping. */
5020 amt = sizeof (struct elf_segment_map);
5021 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
5022 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
5023 if (map == NULL)
5024 return FALSE;
5026 /* Initialise the fields of the segment map. Set the physical
5027 physical address to the LMA of the first section that has
5028 not yet been assigned. */
5029 map->next = NULL;
5030 map->p_type = segment->p_type;
5031 map->p_flags = segment->p_flags;
5032 map->p_flags_valid = 1;
5033 map->p_paddr = suggested_lma;
5034 map->p_paddr_valid = 1;
5035 map->includes_filehdr = 0;
5036 map->includes_phdrs = 0;
5039 while (isec < section_count);
5041 free (sections);
5044 /* The Solaris linker creates program headers in which all the
5045 p_paddr fields are zero. When we try to objcopy or strip such a
5046 file, we get confused. Check for this case, and if we find it
5047 reset the p_paddr_valid fields. */
5048 for (map = map_first; map != NULL; map = map->next)
5049 if (map->p_paddr != 0)
5050 break;
5051 if (map == NULL)
5053 for (map = map_first; map != NULL; map = map->next)
5054 map->p_paddr_valid = 0;
5057 elf_tdata (obfd)->segment_map = map_first;
5059 /* If we had to estimate the number of program headers that were
5060 going to be needed, then check our estimate now and adjust
5061 the offset if necessary. */
5062 if (phdr_adjust_seg != NULL)
5064 unsigned int count;
5066 for (count = 0, map = map_first; map != NULL; map = map->next)
5067 count++;
5069 if (count > phdr_adjust_num)
5070 phdr_adjust_seg->p_paddr
5071 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
5074 #if 0
5075 /* Final Step: Sort the segments into ascending order of physical
5076 address. */
5077 if (map_first != NULL)
5079 struct elf_segment_map *prev;
5081 prev = map_first;
5082 for (map = map_first->next; map != NULL; prev = map, map = map->next)
5084 /* Yes I know - its a bubble sort.... */
5085 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
5087 /* Swap map and map->next. */
5088 prev->next = map->next;
5089 map->next = map->next->next;
5090 prev->next->next = map;
5092 /* Restart loop. */
5093 map = map_first;
5097 #endif
5099 #undef SEGMENT_END
5100 #undef IS_CONTAINED_BY_VMA
5101 #undef IS_CONTAINED_BY_LMA
5102 #undef IS_COREFILE_NOTE
5103 #undef IS_SOLARIS_PT_INTERP
5104 #undef INCLUDE_SECTION_IN_SEGMENT
5105 #undef SEGMENT_AFTER_SEGMENT
5106 #undef SEGMENT_OVERLAPS
5107 return TRUE;
5110 /* Copy private section information. This copies over the entsize
5111 field, and sometimes the info field. */
5113 bfd_boolean
5114 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
5115 bfd *ibfd;
5116 asection *isec;
5117 bfd *obfd;
5118 asection *osec;
5120 Elf_Internal_Shdr *ihdr, *ohdr;
5122 if (ibfd->xvec->flavour != bfd_target_elf_flavour
5123 || obfd->xvec->flavour != bfd_target_elf_flavour)
5124 return TRUE;
5126 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL)
5128 asection *s;
5130 /* Only set up the segments if there are no more SEC_ALLOC
5131 sections. FIXME: This won't do the right thing if objcopy is
5132 used to remove the last SEC_ALLOC section, since objcopy
5133 won't call this routine in that case. */
5134 for (s = isec->next; s != NULL; s = s->next)
5135 if ((s->flags & SEC_ALLOC) != 0)
5136 break;
5137 if (s == NULL)
5139 if (! copy_private_bfd_data (ibfd, obfd))
5140 return FALSE;
5144 ihdr = &elf_section_data (isec)->this_hdr;
5145 ohdr = &elf_section_data (osec)->this_hdr;
5147 ohdr->sh_entsize = ihdr->sh_entsize;
5149 if (ihdr->sh_type == SHT_SYMTAB
5150 || ihdr->sh_type == SHT_DYNSYM
5151 || ihdr->sh_type == SHT_GNU_verneed
5152 || ihdr->sh_type == SHT_GNU_verdef)
5153 ohdr->sh_info = ihdr->sh_info;
5155 /* Set things up for objcopy. The output SHT_GROUP section will
5156 have its elf_next_in_group pointing back to the input group
5157 members. */
5158 elf_next_in_group (osec) = elf_next_in_group (isec);
5159 elf_group_name (osec) = elf_group_name (isec);
5161 elf_section_data (osec)->use_rela_p
5162 = elf_section_data (isec)->use_rela_p;
5164 return TRUE;
5167 /* Copy private symbol information. If this symbol is in a section
5168 which we did not map into a BFD section, try to map the section
5169 index correctly. We use special macro definitions for the mapped
5170 section indices; these definitions are interpreted by the
5171 swap_out_syms function. */
5173 #define MAP_ONESYMTAB (SHN_HIOS + 1)
5174 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
5175 #define MAP_STRTAB (SHN_HIOS + 3)
5176 #define MAP_SHSTRTAB (SHN_HIOS + 4)
5177 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
5179 bfd_boolean
5180 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
5181 bfd *ibfd;
5182 asymbol *isymarg;
5183 bfd *obfd;
5184 asymbol *osymarg;
5186 elf_symbol_type *isym, *osym;
5188 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5189 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5190 return TRUE;
5192 isym = elf_symbol_from (ibfd, isymarg);
5193 osym = elf_symbol_from (obfd, osymarg);
5195 if (isym != NULL
5196 && osym != NULL
5197 && bfd_is_abs_section (isym->symbol.section))
5199 unsigned int shndx;
5201 shndx = isym->internal_elf_sym.st_shndx;
5202 if (shndx == elf_onesymtab (ibfd))
5203 shndx = MAP_ONESYMTAB;
5204 else if (shndx == elf_dynsymtab (ibfd))
5205 shndx = MAP_DYNSYMTAB;
5206 else if (shndx == elf_tdata (ibfd)->strtab_section)
5207 shndx = MAP_STRTAB;
5208 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
5209 shndx = MAP_SHSTRTAB;
5210 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
5211 shndx = MAP_SYM_SHNDX;
5212 osym->internal_elf_sym.st_shndx = shndx;
5215 return TRUE;
5218 /* Swap out the symbols. */
5220 static bfd_boolean
5221 swap_out_syms (abfd, sttp, relocatable_p)
5222 bfd *abfd;
5223 struct bfd_strtab_hash **sttp;
5224 int relocatable_p;
5226 struct elf_backend_data *bed;
5227 int symcount;
5228 asymbol **syms;
5229 struct bfd_strtab_hash *stt;
5230 Elf_Internal_Shdr *symtab_hdr;
5231 Elf_Internal_Shdr *symtab_shndx_hdr;
5232 Elf_Internal_Shdr *symstrtab_hdr;
5233 char *outbound_syms;
5234 char *outbound_shndx;
5235 int idx;
5236 bfd_size_type amt;
5238 if (!elf_map_symbols (abfd))
5239 return FALSE;
5241 /* Dump out the symtabs. */
5242 stt = _bfd_elf_stringtab_init ();
5243 if (stt == NULL)
5244 return FALSE;
5246 bed = get_elf_backend_data (abfd);
5247 symcount = bfd_get_symcount (abfd);
5248 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5249 symtab_hdr->sh_type = SHT_SYMTAB;
5250 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
5251 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
5252 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
5253 symtab_hdr->sh_addralign = bed->s->file_align;
5255 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
5256 symstrtab_hdr->sh_type = SHT_STRTAB;
5258 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
5259 outbound_syms = bfd_alloc (abfd, amt);
5260 if (outbound_syms == NULL)
5261 return FALSE;
5262 symtab_hdr->contents = (PTR) outbound_syms;
5264 outbound_shndx = NULL;
5265 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
5266 if (symtab_shndx_hdr->sh_name != 0)
5268 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
5269 outbound_shndx = bfd_zalloc (abfd, amt);
5270 if (outbound_shndx == NULL)
5271 return FALSE;
5272 symtab_shndx_hdr->contents = outbound_shndx;
5273 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
5274 symtab_shndx_hdr->sh_size = amt;
5275 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
5276 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
5279 /* now generate the data (for "contents") */
5281 /* Fill in zeroth symbol and swap it out. */
5282 Elf_Internal_Sym sym;
5283 sym.st_name = 0;
5284 sym.st_value = 0;
5285 sym.st_size = 0;
5286 sym.st_info = 0;
5287 sym.st_other = 0;
5288 sym.st_shndx = SHN_UNDEF;
5289 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5290 outbound_syms += bed->s->sizeof_sym;
5291 if (outbound_shndx != NULL)
5292 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5295 syms = bfd_get_outsymbols (abfd);
5296 for (idx = 0; idx < symcount; idx++)
5298 Elf_Internal_Sym sym;
5299 bfd_vma value = syms[idx]->value;
5300 elf_symbol_type *type_ptr;
5301 flagword flags = syms[idx]->flags;
5302 int type;
5304 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
5306 /* Local section symbols have no name. */
5307 sym.st_name = 0;
5309 else
5311 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
5312 syms[idx]->name,
5313 TRUE, FALSE);
5314 if (sym.st_name == (unsigned long) -1)
5315 return FALSE;
5318 type_ptr = elf_symbol_from (abfd, syms[idx]);
5320 if ((flags & BSF_SECTION_SYM) == 0
5321 && bfd_is_com_section (syms[idx]->section))
5323 /* ELF common symbols put the alignment into the `value' field,
5324 and the size into the `size' field. This is backwards from
5325 how BFD handles it, so reverse it here. */
5326 sym.st_size = value;
5327 if (type_ptr == NULL
5328 || type_ptr->internal_elf_sym.st_value == 0)
5329 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
5330 else
5331 sym.st_value = type_ptr->internal_elf_sym.st_value;
5332 sym.st_shndx = _bfd_elf_section_from_bfd_section
5333 (abfd, syms[idx]->section);
5335 else
5337 asection *sec = syms[idx]->section;
5338 int shndx;
5340 if (sec->output_section)
5342 value += sec->output_offset;
5343 sec = sec->output_section;
5345 /* Don't add in the section vma for relocatable output. */
5346 if (! relocatable_p)
5347 value += sec->vma;
5348 sym.st_value = value;
5349 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
5351 if (bfd_is_abs_section (sec)
5352 && type_ptr != NULL
5353 && type_ptr->internal_elf_sym.st_shndx != 0)
5355 /* This symbol is in a real ELF section which we did
5356 not create as a BFD section. Undo the mapping done
5357 by copy_private_symbol_data. */
5358 shndx = type_ptr->internal_elf_sym.st_shndx;
5359 switch (shndx)
5361 case MAP_ONESYMTAB:
5362 shndx = elf_onesymtab (abfd);
5363 break;
5364 case MAP_DYNSYMTAB:
5365 shndx = elf_dynsymtab (abfd);
5366 break;
5367 case MAP_STRTAB:
5368 shndx = elf_tdata (abfd)->strtab_section;
5369 break;
5370 case MAP_SHSTRTAB:
5371 shndx = elf_tdata (abfd)->shstrtab_section;
5372 break;
5373 case MAP_SYM_SHNDX:
5374 shndx = elf_tdata (abfd)->symtab_shndx_section;
5375 break;
5376 default:
5377 break;
5380 else
5382 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
5384 if (shndx == -1)
5386 asection *sec2;
5388 /* Writing this would be a hell of a lot easier if
5389 we had some decent documentation on bfd, and
5390 knew what to expect of the library, and what to
5391 demand of applications. For example, it
5392 appears that `objcopy' might not set the
5393 section of a symbol to be a section that is
5394 actually in the output file. */
5395 sec2 = bfd_get_section_by_name (abfd, sec->name);
5396 BFD_ASSERT (sec2 != 0);
5397 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5398 BFD_ASSERT (shndx != -1);
5402 sym.st_shndx = shndx;
5405 if ((flags & BSF_THREAD_LOCAL) != 0)
5406 type = STT_TLS;
5407 else if ((flags & BSF_FUNCTION) != 0)
5408 type = STT_FUNC;
5409 else if ((flags & BSF_OBJECT) != 0)
5410 type = STT_OBJECT;
5411 else
5412 type = STT_NOTYPE;
5414 if (syms[idx]->section->flags & SEC_THREAD_LOCAL)
5415 type = STT_TLS;
5417 /* Processor-specific types */
5418 if (type_ptr != NULL
5419 && bed->elf_backend_get_symbol_type)
5420 type = ((*bed->elf_backend_get_symbol_type)
5421 (&type_ptr->internal_elf_sym, type));
5423 if (flags & BSF_SECTION_SYM)
5425 if (flags & BSF_GLOBAL)
5426 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5427 else
5428 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5430 else if (bfd_is_com_section (syms[idx]->section))
5431 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5432 else if (bfd_is_und_section (syms[idx]->section))
5433 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5434 ? STB_WEAK
5435 : STB_GLOBAL),
5436 type);
5437 else if (flags & BSF_FILE)
5438 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5439 else
5441 int bind = STB_LOCAL;
5443 if (flags & BSF_LOCAL)
5444 bind = STB_LOCAL;
5445 else if (flags & BSF_WEAK)
5446 bind = STB_WEAK;
5447 else if (flags & BSF_GLOBAL)
5448 bind = STB_GLOBAL;
5450 sym.st_info = ELF_ST_INFO (bind, type);
5453 if (type_ptr != NULL)
5454 sym.st_other = type_ptr->internal_elf_sym.st_other;
5455 else
5456 sym.st_other = 0;
5458 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5459 outbound_syms += bed->s->sizeof_sym;
5460 if (outbound_shndx != NULL)
5461 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5464 *sttp = stt;
5465 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5466 symstrtab_hdr->sh_type = SHT_STRTAB;
5468 symstrtab_hdr->sh_flags = 0;
5469 symstrtab_hdr->sh_addr = 0;
5470 symstrtab_hdr->sh_entsize = 0;
5471 symstrtab_hdr->sh_link = 0;
5472 symstrtab_hdr->sh_info = 0;
5473 symstrtab_hdr->sh_addralign = 1;
5475 return TRUE;
5478 /* Return the number of bytes required to hold the symtab vector.
5480 Note that we base it on the count plus 1, since we will null terminate
5481 the vector allocated based on this size. However, the ELF symbol table
5482 always has a dummy entry as symbol #0, so it ends up even. */
5484 long
5485 _bfd_elf_get_symtab_upper_bound (abfd)
5486 bfd *abfd;
5488 long symcount;
5489 long symtab_size;
5490 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5492 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5493 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5494 if (symcount > 0)
5495 symtab_size -= sizeof (asymbol *);
5497 return symtab_size;
5500 long
5501 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5502 bfd *abfd;
5504 long symcount;
5505 long symtab_size;
5506 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5508 if (elf_dynsymtab (abfd) == 0)
5510 bfd_set_error (bfd_error_invalid_operation);
5511 return -1;
5514 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5515 symtab_size = (symcount + 1) * (sizeof (asymbol *));
5516 if (symcount > 0)
5517 symtab_size -= sizeof (asymbol *);
5519 return symtab_size;
5522 long
5523 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5524 bfd *abfd ATTRIBUTE_UNUSED;
5525 sec_ptr asect;
5527 return (asect->reloc_count + 1) * sizeof (arelent *);
5530 /* Canonicalize the relocs. */
5532 long
5533 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5534 bfd *abfd;
5535 sec_ptr section;
5536 arelent **relptr;
5537 asymbol **symbols;
5539 arelent *tblptr;
5540 unsigned int i;
5541 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5543 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
5544 return -1;
5546 tblptr = section->relocation;
5547 for (i = 0; i < section->reloc_count; i++)
5548 *relptr++ = tblptr++;
5550 *relptr = NULL;
5552 return section->reloc_count;
5555 long
5556 _bfd_elf_get_symtab (abfd, alocation)
5557 bfd *abfd;
5558 asymbol **alocation;
5560 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5561 long symcount = bed->s->slurp_symbol_table (abfd, alocation, FALSE);
5563 if (symcount >= 0)
5564 bfd_get_symcount (abfd) = symcount;
5565 return symcount;
5568 long
5569 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5570 bfd *abfd;
5571 asymbol **alocation;
5573 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5574 long symcount = bed->s->slurp_symbol_table (abfd, alocation, TRUE);
5576 if (symcount >= 0)
5577 bfd_get_dynamic_symcount (abfd) = symcount;
5578 return symcount;
5581 /* Return the size required for the dynamic reloc entries. Any
5582 section that was actually installed in the BFD, and has type
5583 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5584 considered to be a dynamic reloc section. */
5586 long
5587 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5588 bfd *abfd;
5590 long ret;
5591 asection *s;
5593 if (elf_dynsymtab (abfd) == 0)
5595 bfd_set_error (bfd_error_invalid_operation);
5596 return -1;
5599 ret = sizeof (arelent *);
5600 for (s = abfd->sections; s != NULL; s = s->next)
5601 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5602 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5603 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5604 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5605 * sizeof (arelent *));
5607 return ret;
5610 /* Canonicalize the dynamic relocation entries. Note that we return
5611 the dynamic relocations as a single block, although they are
5612 actually associated with particular sections; the interface, which
5613 was designed for SunOS style shared libraries, expects that there
5614 is only one set of dynamic relocs. Any section that was actually
5615 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5616 the dynamic symbol table, is considered to be a dynamic reloc
5617 section. */
5619 long
5620 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5621 bfd *abfd;
5622 arelent **storage;
5623 asymbol **syms;
5625 bfd_boolean (*slurp_relocs)
5626 PARAMS ((bfd *, asection *, asymbol **, bfd_boolean));
5627 asection *s;
5628 long ret;
5630 if (elf_dynsymtab (abfd) == 0)
5632 bfd_set_error (bfd_error_invalid_operation);
5633 return -1;
5636 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5637 ret = 0;
5638 for (s = abfd->sections; s != NULL; s = s->next)
5640 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5641 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5642 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5644 arelent *p;
5645 long count, i;
5647 if (! (*slurp_relocs) (abfd, s, syms, TRUE))
5648 return -1;
5649 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5650 p = s->relocation;
5651 for (i = 0; i < count; i++)
5652 *storage++ = p++;
5653 ret += count;
5657 *storage = NULL;
5659 return ret;
5662 /* Read in the version information. */
5664 bfd_boolean
5665 _bfd_elf_slurp_version_tables (abfd)
5666 bfd *abfd;
5668 bfd_byte *contents = NULL;
5669 bfd_size_type amt;
5671 if (elf_dynverdef (abfd) != 0)
5673 Elf_Internal_Shdr *hdr;
5674 Elf_External_Verdef *everdef;
5675 Elf_Internal_Verdef *iverdef;
5676 Elf_Internal_Verdef *iverdefarr;
5677 Elf_Internal_Verdef iverdefmem;
5678 unsigned int i;
5679 unsigned int maxidx;
5681 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5683 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5684 if (contents == NULL)
5685 goto error_return;
5686 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5687 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5688 goto error_return;
5690 /* We know the number of entries in the section but not the maximum
5691 index. Therefore we have to run through all entries and find
5692 the maximum. */
5693 everdef = (Elf_External_Verdef *) contents;
5694 maxidx = 0;
5695 for (i = 0; i < hdr->sh_info; ++i)
5697 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5699 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5700 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5702 everdef = ((Elf_External_Verdef *)
5703 ((bfd_byte *) everdef + iverdefmem.vd_next));
5706 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5707 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5708 if (elf_tdata (abfd)->verdef == NULL)
5709 goto error_return;
5711 elf_tdata (abfd)->cverdefs = maxidx;
5713 everdef = (Elf_External_Verdef *) contents;
5714 iverdefarr = elf_tdata (abfd)->verdef;
5715 for (i = 0; i < hdr->sh_info; i++)
5717 Elf_External_Verdaux *everdaux;
5718 Elf_Internal_Verdaux *iverdaux;
5719 unsigned int j;
5721 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5723 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5724 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5726 iverdef->vd_bfd = abfd;
5728 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5729 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5730 if (iverdef->vd_auxptr == NULL)
5731 goto error_return;
5733 everdaux = ((Elf_External_Verdaux *)
5734 ((bfd_byte *) everdef + iverdef->vd_aux));
5735 iverdaux = iverdef->vd_auxptr;
5736 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5738 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5740 iverdaux->vda_nodename =
5741 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5742 iverdaux->vda_name);
5743 if (iverdaux->vda_nodename == NULL)
5744 goto error_return;
5746 if (j + 1 < iverdef->vd_cnt)
5747 iverdaux->vda_nextptr = iverdaux + 1;
5748 else
5749 iverdaux->vda_nextptr = NULL;
5751 everdaux = ((Elf_External_Verdaux *)
5752 ((bfd_byte *) everdaux + iverdaux->vda_next));
5755 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5757 if (i + 1 < hdr->sh_info)
5758 iverdef->vd_nextdef = iverdef + 1;
5759 else
5760 iverdef->vd_nextdef = NULL;
5762 everdef = ((Elf_External_Verdef *)
5763 ((bfd_byte *) everdef + iverdef->vd_next));
5766 free (contents);
5767 contents = NULL;
5770 if (elf_dynverref (abfd) != 0)
5772 Elf_Internal_Shdr *hdr;
5773 Elf_External_Verneed *everneed;
5774 Elf_Internal_Verneed *iverneed;
5775 unsigned int i;
5777 hdr = &elf_tdata (abfd)->dynverref_hdr;
5779 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5780 elf_tdata (abfd)->verref =
5781 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5782 if (elf_tdata (abfd)->verref == NULL)
5783 goto error_return;
5785 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5787 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5788 if (contents == NULL)
5789 goto error_return;
5790 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5791 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5792 goto error_return;
5794 everneed = (Elf_External_Verneed *) contents;
5795 iverneed = elf_tdata (abfd)->verref;
5796 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5798 Elf_External_Vernaux *evernaux;
5799 Elf_Internal_Vernaux *ivernaux;
5800 unsigned int j;
5802 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5804 iverneed->vn_bfd = abfd;
5806 iverneed->vn_filename =
5807 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5808 iverneed->vn_file);
5809 if (iverneed->vn_filename == NULL)
5810 goto error_return;
5812 amt = iverneed->vn_cnt;
5813 amt *= sizeof (Elf_Internal_Vernaux);
5814 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5816 evernaux = ((Elf_External_Vernaux *)
5817 ((bfd_byte *) everneed + iverneed->vn_aux));
5818 ivernaux = iverneed->vn_auxptr;
5819 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5821 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5823 ivernaux->vna_nodename =
5824 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5825 ivernaux->vna_name);
5826 if (ivernaux->vna_nodename == NULL)
5827 goto error_return;
5829 if (j + 1 < iverneed->vn_cnt)
5830 ivernaux->vna_nextptr = ivernaux + 1;
5831 else
5832 ivernaux->vna_nextptr = NULL;
5834 evernaux = ((Elf_External_Vernaux *)
5835 ((bfd_byte *) evernaux + ivernaux->vna_next));
5838 if (i + 1 < hdr->sh_info)
5839 iverneed->vn_nextref = iverneed + 1;
5840 else
5841 iverneed->vn_nextref = NULL;
5843 everneed = ((Elf_External_Verneed *)
5844 ((bfd_byte *) everneed + iverneed->vn_next));
5847 free (contents);
5848 contents = NULL;
5851 return TRUE;
5853 error_return:
5854 if (contents == NULL)
5855 free (contents);
5856 return FALSE;
5859 asymbol *
5860 _bfd_elf_make_empty_symbol (abfd)
5861 bfd *abfd;
5863 elf_symbol_type *newsym;
5864 bfd_size_type amt = sizeof (elf_symbol_type);
5866 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5867 if (!newsym)
5868 return NULL;
5869 else
5871 newsym->symbol.the_bfd = abfd;
5872 return &newsym->symbol;
5876 void
5877 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5878 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5879 asymbol *symbol;
5880 symbol_info *ret;
5882 bfd_symbol_info (symbol, ret);
5885 /* Return whether a symbol name implies a local symbol. Most targets
5886 use this function for the is_local_label_name entry point, but some
5887 override it. */
5889 bfd_boolean
5890 _bfd_elf_is_local_label_name (abfd, name)
5891 bfd *abfd ATTRIBUTE_UNUSED;
5892 const char *name;
5894 /* Normal local symbols start with ``.L''. */
5895 if (name[0] == '.' && name[1] == 'L')
5896 return TRUE;
5898 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5899 DWARF debugging symbols starting with ``..''. */
5900 if (name[0] == '.' && name[1] == '.')
5901 return TRUE;
5903 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5904 emitting DWARF debugging output. I suspect this is actually a
5905 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5906 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5907 underscore to be emitted on some ELF targets). For ease of use,
5908 we treat such symbols as local. */
5909 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5910 return TRUE;
5912 return FALSE;
5915 alent *
5916 _bfd_elf_get_lineno (ignore_abfd, symbol)
5917 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5918 asymbol *symbol ATTRIBUTE_UNUSED;
5920 abort ();
5921 return NULL;
5924 bfd_boolean
5925 _bfd_elf_set_arch_mach (abfd, arch, machine)
5926 bfd *abfd;
5927 enum bfd_architecture arch;
5928 unsigned long machine;
5930 /* If this isn't the right architecture for this backend, and this
5931 isn't the generic backend, fail. */
5932 if (arch != get_elf_backend_data (abfd)->arch
5933 && arch != bfd_arch_unknown
5934 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5935 return FALSE;
5937 return bfd_default_set_arch_mach (abfd, arch, machine);
5940 /* Find the function to a particular section and offset,
5941 for error reporting. */
5943 static bfd_boolean
5944 elf_find_function (abfd, section, symbols, offset,
5945 filename_ptr, functionname_ptr)
5946 bfd *abfd ATTRIBUTE_UNUSED;
5947 asection *section;
5948 asymbol **symbols;
5949 bfd_vma offset;
5950 const char **filename_ptr;
5951 const char **functionname_ptr;
5953 const char *filename;
5954 asymbol *func;
5955 bfd_vma low_func;
5956 asymbol **p;
5958 filename = NULL;
5959 func = NULL;
5960 low_func = 0;
5962 for (p = symbols; *p != NULL; p++)
5964 elf_symbol_type *q;
5966 q = (elf_symbol_type *) *p;
5968 if (bfd_get_section (&q->symbol) != section)
5969 continue;
5971 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5973 default:
5974 break;
5975 case STT_FILE:
5976 filename = bfd_asymbol_name (&q->symbol);
5977 break;
5978 case STT_NOTYPE:
5979 case STT_FUNC:
5980 if (q->symbol.section == section
5981 && q->symbol.value >= low_func
5982 && q->symbol.value <= offset)
5984 func = (asymbol *) q;
5985 low_func = q->symbol.value;
5987 break;
5991 if (func == NULL)
5992 return FALSE;
5994 if (filename_ptr)
5995 *filename_ptr = filename;
5996 if (functionname_ptr)
5997 *functionname_ptr = bfd_asymbol_name (func);
5999 return TRUE;
6002 /* Find the nearest line to a particular section and offset,
6003 for error reporting. */
6005 bfd_boolean
6006 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
6007 filename_ptr, functionname_ptr, line_ptr)
6008 bfd *abfd;
6009 asection *section;
6010 asymbol **symbols;
6011 bfd_vma offset;
6012 const char **filename_ptr;
6013 const char **functionname_ptr;
6014 unsigned int *line_ptr;
6016 bfd_boolean found;
6018 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
6019 filename_ptr, functionname_ptr,
6020 line_ptr))
6022 if (!*functionname_ptr)
6023 elf_find_function (abfd, section, symbols, offset,
6024 *filename_ptr ? NULL : filename_ptr,
6025 functionname_ptr);
6027 return TRUE;
6030 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
6031 filename_ptr, functionname_ptr,
6032 line_ptr, 0,
6033 &elf_tdata (abfd)->dwarf2_find_line_info))
6035 if (!*functionname_ptr)
6036 elf_find_function (abfd, section, symbols, offset,
6037 *filename_ptr ? NULL : filename_ptr,
6038 functionname_ptr);
6040 return TRUE;
6043 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
6044 &found, filename_ptr,
6045 functionname_ptr, line_ptr,
6046 &elf_tdata (abfd)->line_info))
6047 return FALSE;
6048 if (found && (*functionname_ptr || *line_ptr))
6049 return TRUE;
6051 if (symbols == NULL)
6052 return FALSE;
6054 if (! elf_find_function (abfd, section, symbols, offset,
6055 filename_ptr, functionname_ptr))
6056 return FALSE;
6058 *line_ptr = 0;
6059 return TRUE;
6063 _bfd_elf_sizeof_headers (abfd, reloc)
6064 bfd *abfd;
6065 bfd_boolean reloc;
6067 int ret;
6069 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
6070 if (! reloc)
6071 ret += get_program_header_size (abfd);
6072 return ret;
6075 bfd_boolean
6076 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
6077 bfd *abfd;
6078 sec_ptr section;
6079 PTR location;
6080 file_ptr offset;
6081 bfd_size_type count;
6083 Elf_Internal_Shdr *hdr;
6084 bfd_signed_vma pos;
6086 if (! abfd->output_has_begun
6087 && ! (_bfd_elf_compute_section_file_positions
6088 (abfd, (struct bfd_link_info *) NULL)))
6089 return FALSE;
6091 hdr = &elf_section_data (section)->this_hdr;
6092 pos = hdr->sh_offset + offset;
6093 if (bfd_seek (abfd, pos, SEEK_SET) != 0
6094 || bfd_bwrite (location, count, abfd) != count)
6095 return FALSE;
6097 return TRUE;
6100 void
6101 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
6102 bfd *abfd ATTRIBUTE_UNUSED;
6103 arelent *cache_ptr ATTRIBUTE_UNUSED;
6104 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
6106 abort ();
6109 /* Try to convert a non-ELF reloc into an ELF one. */
6111 bfd_boolean
6112 _bfd_elf_validate_reloc (abfd, areloc)
6113 bfd *abfd;
6114 arelent *areloc;
6116 /* Check whether we really have an ELF howto. */
6118 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
6120 bfd_reloc_code_real_type code;
6121 reloc_howto_type *howto;
6123 /* Alien reloc: Try to determine its type to replace it with an
6124 equivalent ELF reloc. */
6126 if (areloc->howto->pc_relative)
6128 switch (areloc->howto->bitsize)
6130 case 8:
6131 code = BFD_RELOC_8_PCREL;
6132 break;
6133 case 12:
6134 code = BFD_RELOC_12_PCREL;
6135 break;
6136 case 16:
6137 code = BFD_RELOC_16_PCREL;
6138 break;
6139 case 24:
6140 code = BFD_RELOC_24_PCREL;
6141 break;
6142 case 32:
6143 code = BFD_RELOC_32_PCREL;
6144 break;
6145 case 64:
6146 code = BFD_RELOC_64_PCREL;
6147 break;
6148 default:
6149 goto fail;
6152 howto = bfd_reloc_type_lookup (abfd, code);
6154 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
6156 if (howto->pcrel_offset)
6157 areloc->addend += areloc->address;
6158 else
6159 areloc->addend -= areloc->address; /* addend is unsigned!! */
6162 else
6164 switch (areloc->howto->bitsize)
6166 case 8:
6167 code = BFD_RELOC_8;
6168 break;
6169 case 14:
6170 code = BFD_RELOC_14;
6171 break;
6172 case 16:
6173 code = BFD_RELOC_16;
6174 break;
6175 case 26:
6176 code = BFD_RELOC_26;
6177 break;
6178 case 32:
6179 code = BFD_RELOC_32;
6180 break;
6181 case 64:
6182 code = BFD_RELOC_64;
6183 break;
6184 default:
6185 goto fail;
6188 howto = bfd_reloc_type_lookup (abfd, code);
6191 if (howto)
6192 areloc->howto = howto;
6193 else
6194 goto fail;
6197 return TRUE;
6199 fail:
6200 (*_bfd_error_handler)
6201 (_("%s: unsupported relocation type %s"),
6202 bfd_archive_filename (abfd), areloc->howto->name);
6203 bfd_set_error (bfd_error_bad_value);
6204 return FALSE;
6207 bfd_boolean
6208 _bfd_elf_close_and_cleanup (abfd)
6209 bfd *abfd;
6211 if (bfd_get_format (abfd) == bfd_object)
6213 if (elf_shstrtab (abfd) != NULL)
6214 _bfd_elf_strtab_free (elf_shstrtab (abfd));
6217 return _bfd_generic_close_and_cleanup (abfd);
6220 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
6221 in the relocation's offset. Thus we cannot allow any sort of sanity
6222 range-checking to interfere. There is nothing else to do in processing
6223 this reloc. */
6225 bfd_reloc_status_type
6226 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
6227 bfd *abfd ATTRIBUTE_UNUSED;
6228 arelent *re ATTRIBUTE_UNUSED;
6229 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
6230 PTR data ATTRIBUTE_UNUSED;
6231 asection *is ATTRIBUTE_UNUSED;
6232 bfd *obfd ATTRIBUTE_UNUSED;
6233 char **errmsg ATTRIBUTE_UNUSED;
6235 return bfd_reloc_ok;
6238 /* Elf core file support. Much of this only works on native
6239 toolchains, since we rely on knowing the
6240 machine-dependent procfs structure in order to pick
6241 out details about the corefile. */
6243 #ifdef HAVE_SYS_PROCFS_H
6244 # include <sys/procfs.h>
6245 #endif
6247 /* FIXME: this is kinda wrong, but it's what gdb wants. */
6249 static int
6250 elfcore_make_pid (abfd)
6251 bfd *abfd;
6253 return ((elf_tdata (abfd)->core_lwpid << 16)
6254 + (elf_tdata (abfd)->core_pid));
6257 /* If there isn't a section called NAME, make one, using
6258 data from SECT. Note, this function will generate a
6259 reference to NAME, so you shouldn't deallocate or
6260 overwrite it. */
6262 static bfd_boolean
6263 elfcore_maybe_make_sect (abfd, name, sect)
6264 bfd *abfd;
6265 char *name;
6266 asection *sect;
6268 asection *sect2;
6270 if (bfd_get_section_by_name (abfd, name) != NULL)
6271 return TRUE;
6273 sect2 = bfd_make_section (abfd, name);
6274 if (sect2 == NULL)
6275 return FALSE;
6277 sect2->_raw_size = sect->_raw_size;
6278 sect2->filepos = sect->filepos;
6279 sect2->flags = sect->flags;
6280 sect2->alignment_power = sect->alignment_power;
6281 return TRUE;
6284 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
6285 actually creates up to two pseudosections:
6286 - For the single-threaded case, a section named NAME, unless
6287 such a section already exists.
6288 - For the multi-threaded case, a section named "NAME/PID", where
6289 PID is elfcore_make_pid (abfd).
6290 Both pseudosections have identical contents. */
6291 bfd_boolean
6292 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
6293 bfd *abfd;
6294 char *name;
6295 size_t size;
6296 ufile_ptr filepos;
6298 char buf[100];
6299 char *threaded_name;
6300 size_t len;
6301 asection *sect;
6303 /* Build the section name. */
6305 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
6306 len = strlen (buf) + 1;
6307 threaded_name = bfd_alloc (abfd, (bfd_size_type) len);
6308 if (threaded_name == NULL)
6309 return FALSE;
6310 memcpy (threaded_name, buf, len);
6312 sect = bfd_make_section (abfd, threaded_name);
6313 if (sect == NULL)
6314 return FALSE;
6315 sect->_raw_size = size;
6316 sect->filepos = filepos;
6317 sect->flags = SEC_HAS_CONTENTS;
6318 sect->alignment_power = 2;
6320 return elfcore_maybe_make_sect (abfd, name, sect);
6323 /* prstatus_t exists on:
6324 solaris 2.5+
6325 linux 2.[01] + glibc
6326 unixware 4.2
6329 #if defined (HAVE_PRSTATUS_T)
6330 static bfd_boolean elfcore_grok_prstatus
6331 PARAMS ((bfd *, Elf_Internal_Note *));
6333 static bfd_boolean
6334 elfcore_grok_prstatus (abfd, note)
6335 bfd *abfd;
6336 Elf_Internal_Note *note;
6338 size_t raw_size;
6339 int offset;
6341 if (note->descsz == sizeof (prstatus_t))
6343 prstatus_t prstat;
6345 raw_size = sizeof (prstat.pr_reg);
6346 offset = offsetof (prstatus_t, pr_reg);
6347 memcpy (&prstat, note->descdata, sizeof (prstat));
6349 /* Do not overwrite the core signal if it
6350 has already been set by another thread. */
6351 if (elf_tdata (abfd)->core_signal == 0)
6352 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
6353 elf_tdata (abfd)->core_pid = prstat.pr_pid;
6355 /* pr_who exists on:
6356 solaris 2.5+
6357 unixware 4.2
6358 pr_who doesn't exist on:
6359 linux 2.[01]
6361 #if defined (HAVE_PRSTATUS_T_PR_WHO)
6362 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6363 #endif
6365 #if defined (HAVE_PRSTATUS32_T)
6366 else if (note->descsz == sizeof (prstatus32_t))
6368 /* 64-bit host, 32-bit corefile */
6369 prstatus32_t prstat;
6371 raw_size = sizeof (prstat.pr_reg);
6372 offset = offsetof (prstatus32_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_PRSTATUS32_T_PR_WHO)
6388 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
6389 #endif
6391 #endif /* HAVE_PRSTATUS32_T */
6392 else
6394 /* Fail - we don't know how to handle any other
6395 note size (ie. data object type). */
6396 return TRUE;
6399 /* Make a ".reg/999" section and a ".reg" section. */
6400 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6401 raw_size, note->descpos + offset);
6403 #endif /* defined (HAVE_PRSTATUS_T) */
6405 /* Create a pseudosection containing the exact contents of NOTE. */
6406 static bfd_boolean
6407 elfcore_make_note_pseudosection (abfd, name, note)
6408 bfd *abfd;
6409 char *name;
6410 Elf_Internal_Note *note;
6412 return _bfd_elfcore_make_pseudosection (abfd, name,
6413 note->descsz, note->descpos);
6416 /* There isn't a consistent prfpregset_t across platforms,
6417 but it doesn't matter, because we don't have to pick this
6418 data structure apart. */
6420 static bfd_boolean
6421 elfcore_grok_prfpreg (abfd, note)
6422 bfd *abfd;
6423 Elf_Internal_Note *note;
6425 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6428 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6429 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6430 literally. */
6432 static bfd_boolean
6433 elfcore_grok_prxfpreg (abfd, note)
6434 bfd *abfd;
6435 Elf_Internal_Note *note;
6437 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6440 #if defined (HAVE_PRPSINFO_T)
6441 typedef prpsinfo_t elfcore_psinfo_t;
6442 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6443 typedef prpsinfo32_t elfcore_psinfo32_t;
6444 #endif
6445 #endif
6447 #if defined (HAVE_PSINFO_T)
6448 typedef psinfo_t elfcore_psinfo_t;
6449 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6450 typedef psinfo32_t elfcore_psinfo32_t;
6451 #endif
6452 #endif
6454 /* return a malloc'ed copy of a string at START which is at
6455 most MAX bytes long, possibly without a terminating '\0'.
6456 the copy will always have a terminating '\0'. */
6458 char *
6459 _bfd_elfcore_strndup (abfd, start, max)
6460 bfd *abfd;
6461 char *start;
6462 size_t max;
6464 char *dups;
6465 char *end = memchr (start, '\0', max);
6466 size_t len;
6468 if (end == NULL)
6469 len = max;
6470 else
6471 len = end - start;
6473 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6474 if (dups == NULL)
6475 return NULL;
6477 memcpy (dups, start, len);
6478 dups[len] = '\0';
6480 return dups;
6483 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6484 static bfd_boolean elfcore_grok_psinfo
6485 PARAMS ((bfd *, Elf_Internal_Note *));
6487 static bfd_boolean
6488 elfcore_grok_psinfo (abfd, note)
6489 bfd *abfd;
6490 Elf_Internal_Note *note;
6492 if (note->descsz == sizeof (elfcore_psinfo_t))
6494 elfcore_psinfo_t psinfo;
6496 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6498 elf_tdata (abfd)->core_program
6499 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6500 sizeof (psinfo.pr_fname));
6502 elf_tdata (abfd)->core_command
6503 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6504 sizeof (psinfo.pr_psargs));
6506 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6507 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6509 /* 64-bit host, 32-bit corefile */
6510 elfcore_psinfo32_t psinfo;
6512 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6514 elf_tdata (abfd)->core_program
6515 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6516 sizeof (psinfo.pr_fname));
6518 elf_tdata (abfd)->core_command
6519 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6520 sizeof (psinfo.pr_psargs));
6522 #endif
6524 else
6526 /* Fail - we don't know how to handle any other
6527 note size (ie. data object type). */
6528 return TRUE;
6531 /* Note that for some reason, a spurious space is tacked
6532 onto the end of the args in some (at least one anyway)
6533 implementations, so strip it off if it exists. */
6536 char *command = elf_tdata (abfd)->core_command;
6537 int n = strlen (command);
6539 if (0 < n && command[n - 1] == ' ')
6540 command[n - 1] = '\0';
6543 return TRUE;
6545 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6547 #if defined (HAVE_PSTATUS_T)
6548 static bfd_boolean elfcore_grok_pstatus
6549 PARAMS ((bfd *, Elf_Internal_Note *));
6551 static bfd_boolean
6552 elfcore_grok_pstatus (abfd, note)
6553 bfd *abfd;
6554 Elf_Internal_Note *note;
6556 if (note->descsz == sizeof (pstatus_t)
6557 #if defined (HAVE_PXSTATUS_T)
6558 || note->descsz == sizeof (pxstatus_t)
6559 #endif
6562 pstatus_t pstat;
6564 memcpy (&pstat, note->descdata, sizeof (pstat));
6566 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6568 #if defined (HAVE_PSTATUS32_T)
6569 else if (note->descsz == sizeof (pstatus32_t))
6571 /* 64-bit host, 32-bit corefile */
6572 pstatus32_t pstat;
6574 memcpy (&pstat, note->descdata, sizeof (pstat));
6576 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6578 #endif
6579 /* Could grab some more details from the "representative"
6580 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6581 NT_LWPSTATUS note, presumably. */
6583 return TRUE;
6585 #endif /* defined (HAVE_PSTATUS_T) */
6587 #if defined (HAVE_LWPSTATUS_T)
6588 static bfd_boolean elfcore_grok_lwpstatus
6589 PARAMS ((bfd *, Elf_Internal_Note *));
6591 static bfd_boolean
6592 elfcore_grok_lwpstatus (abfd, note)
6593 bfd *abfd;
6594 Elf_Internal_Note *note;
6596 lwpstatus_t lwpstat;
6597 char buf[100];
6598 char *name;
6599 size_t len;
6600 asection *sect;
6602 if (note->descsz != sizeof (lwpstat)
6603 #if defined (HAVE_LWPXSTATUS_T)
6604 && note->descsz != sizeof (lwpxstatus_t)
6605 #endif
6607 return TRUE;
6609 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6611 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6612 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6614 /* Make a ".reg/999" section. */
6616 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6617 len = strlen (buf) + 1;
6618 name = bfd_alloc (abfd, (bfd_size_type) len);
6619 if (name == NULL)
6620 return FALSE;
6621 memcpy (name, buf, len);
6623 sect = bfd_make_section (abfd, name);
6624 if (sect == NULL)
6625 return FALSE;
6627 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6628 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6629 sect->filepos = note->descpos
6630 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6631 #endif
6633 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6634 sect->_raw_size = sizeof (lwpstat.pr_reg);
6635 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6636 #endif
6638 sect->flags = SEC_HAS_CONTENTS;
6639 sect->alignment_power = 2;
6641 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6642 return FALSE;
6644 /* Make a ".reg2/999" section */
6646 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6647 len = strlen (buf) + 1;
6648 name = bfd_alloc (abfd, (bfd_size_type) len);
6649 if (name == NULL)
6650 return FALSE;
6651 memcpy (name, buf, len);
6653 sect = bfd_make_section (abfd, name);
6654 if (sect == NULL)
6655 return FALSE;
6657 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6658 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6659 sect->filepos = note->descpos
6660 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6661 #endif
6663 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6664 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6665 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6666 #endif
6668 sect->flags = SEC_HAS_CONTENTS;
6669 sect->alignment_power = 2;
6671 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6673 #endif /* defined (HAVE_LWPSTATUS_T) */
6675 #if defined (HAVE_WIN32_PSTATUS_T)
6676 static bfd_boolean
6677 elfcore_grok_win32pstatus (abfd, note)
6678 bfd *abfd;
6679 Elf_Internal_Note *note;
6681 char buf[30];
6682 char *name;
6683 size_t len;
6684 asection *sect;
6685 win32_pstatus_t pstatus;
6687 if (note->descsz < sizeof (pstatus))
6688 return TRUE;
6690 memcpy (&pstatus, note->descdata, sizeof (pstatus));
6692 switch (pstatus.data_type)
6694 case NOTE_INFO_PROCESS:
6695 /* FIXME: need to add ->core_command. */
6696 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6697 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6698 break;
6700 case NOTE_INFO_THREAD:
6701 /* Make a ".reg/999" section. */
6702 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6704 len = strlen (buf) + 1;
6705 name = bfd_alloc (abfd, (bfd_size_type) len);
6706 if (name == NULL)
6707 return FALSE;
6709 memcpy (name, buf, len);
6711 sect = bfd_make_section (abfd, name);
6712 if (sect == NULL)
6713 return FALSE;
6715 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6716 sect->filepos = (note->descpos
6717 + offsetof (struct win32_pstatus,
6718 data.thread_info.thread_context));
6719 sect->flags = SEC_HAS_CONTENTS;
6720 sect->alignment_power = 2;
6722 if (pstatus.data.thread_info.is_active_thread)
6723 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6724 return FALSE;
6725 break;
6727 case NOTE_INFO_MODULE:
6728 /* Make a ".module/xxxxxxxx" section. */
6729 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6731 len = strlen (buf) + 1;
6732 name = bfd_alloc (abfd, (bfd_size_type) len);
6733 if (name == NULL)
6734 return FALSE;
6736 memcpy (name, buf, len);
6738 sect = bfd_make_section (abfd, name);
6740 if (sect == NULL)
6741 return FALSE;
6743 sect->_raw_size = note->descsz;
6744 sect->filepos = note->descpos;
6745 sect->flags = SEC_HAS_CONTENTS;
6746 sect->alignment_power = 2;
6747 break;
6749 default:
6750 return TRUE;
6753 return TRUE;
6755 #endif /* HAVE_WIN32_PSTATUS_T */
6757 static bfd_boolean
6758 elfcore_grok_note (abfd, note)
6759 bfd *abfd;
6760 Elf_Internal_Note *note;
6762 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6764 switch (note->type)
6766 default:
6767 return TRUE;
6769 case NT_PRSTATUS:
6770 if (bed->elf_backend_grok_prstatus)
6771 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6772 return TRUE;
6773 #if defined (HAVE_PRSTATUS_T)
6774 return elfcore_grok_prstatus (abfd, note);
6775 #else
6776 return TRUE;
6777 #endif
6779 #if defined (HAVE_PSTATUS_T)
6780 case NT_PSTATUS:
6781 return elfcore_grok_pstatus (abfd, note);
6782 #endif
6784 #if defined (HAVE_LWPSTATUS_T)
6785 case NT_LWPSTATUS:
6786 return elfcore_grok_lwpstatus (abfd, note);
6787 #endif
6789 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6790 return elfcore_grok_prfpreg (abfd, note);
6792 #if defined (HAVE_WIN32_PSTATUS_T)
6793 case NT_WIN32PSTATUS:
6794 return elfcore_grok_win32pstatus (abfd, note);
6795 #endif
6797 case NT_PRXFPREG: /* Linux SSE extension */
6798 if (note->namesz == 6
6799 && strcmp (note->namedata, "LINUX") == 0)
6800 return elfcore_grok_prxfpreg (abfd, note);
6801 else
6802 return TRUE;
6804 case NT_PRPSINFO:
6805 case NT_PSINFO:
6806 if (bed->elf_backend_grok_psinfo)
6807 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6808 return TRUE;
6809 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6810 return elfcore_grok_psinfo (abfd, note);
6811 #else
6812 return TRUE;
6813 #endif
6817 static bfd_boolean
6818 elfcore_netbsd_get_lwpid (note, lwpidp)
6819 Elf_Internal_Note *note;
6820 int *lwpidp;
6822 char *cp;
6824 cp = strchr (note->namedata, '@');
6825 if (cp != NULL)
6827 *lwpidp = atoi(cp + 1);
6828 return TRUE;
6830 return FALSE;
6833 static bfd_boolean
6834 elfcore_grok_netbsd_procinfo (abfd, note)
6835 bfd *abfd;
6836 Elf_Internal_Note *note;
6839 /* Signal number at offset 0x08. */
6840 elf_tdata (abfd)->core_signal
6841 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6843 /* Process ID at offset 0x50. */
6844 elf_tdata (abfd)->core_pid
6845 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6847 /* Command name at 0x7c (max 32 bytes, including nul). */
6848 elf_tdata (abfd)->core_command
6849 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6851 return TRUE;
6854 static bfd_boolean
6855 elfcore_grok_netbsd_note (abfd, note)
6856 bfd *abfd;
6857 Elf_Internal_Note *note;
6859 int lwp;
6861 if (elfcore_netbsd_get_lwpid (note, &lwp))
6862 elf_tdata (abfd)->core_lwpid = lwp;
6864 if (note->type == NT_NETBSDCORE_PROCINFO)
6866 /* NetBSD-specific core "procinfo". Note that we expect to
6867 find this note before any of the others, which is fine,
6868 since the kernel writes this note out first when it
6869 creates a core file. */
6871 return elfcore_grok_netbsd_procinfo (abfd, note);
6874 /* As of Jan 2002 there are no other machine-independent notes
6875 defined for NetBSD core files. If the note type is less
6876 than the start of the machine-dependent note types, we don't
6877 understand it. */
6879 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6880 return TRUE;
6883 switch (bfd_get_arch (abfd))
6885 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6886 PT_GETFPREGS == mach+2. */
6888 case bfd_arch_alpha:
6889 case bfd_arch_sparc:
6890 switch (note->type)
6892 case NT_NETBSDCORE_FIRSTMACH+0:
6893 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6895 case NT_NETBSDCORE_FIRSTMACH+2:
6896 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6898 default:
6899 return TRUE;
6902 /* On all other arch's, PT_GETREGS == mach+1 and
6903 PT_GETFPREGS == mach+3. */
6905 default:
6906 switch (note->type)
6908 case NT_NETBSDCORE_FIRSTMACH+1:
6909 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6911 case NT_NETBSDCORE_FIRSTMACH+3:
6912 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6914 default:
6915 return TRUE;
6918 /* NOTREACHED */
6921 /* Function: elfcore_write_note
6923 Inputs:
6924 buffer to hold note
6925 name of note
6926 type of note
6927 data for note
6928 size of data for note
6930 Return:
6931 End of buffer containing note. */
6933 char *
6934 elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6935 bfd *abfd;
6936 char *buf;
6937 int *bufsiz;
6938 const char *name;
6939 int type;
6940 const PTR input;
6941 int size;
6943 Elf_External_Note *xnp;
6944 size_t namesz;
6945 size_t pad;
6946 size_t newspace;
6947 char *p, *dest;
6949 namesz = 0;
6950 pad = 0;
6951 if (name != NULL)
6953 struct elf_backend_data *bed;
6955 namesz = strlen (name) + 1;
6956 bed = get_elf_backend_data (abfd);
6957 pad = -namesz & (bed->s->file_align - 1);
6960 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size;
6962 p = realloc (buf, *bufsiz + newspace);
6963 dest = p + *bufsiz;
6964 *bufsiz += newspace;
6965 xnp = (Elf_External_Note *) dest;
6966 H_PUT_32 (abfd, namesz, xnp->namesz);
6967 H_PUT_32 (abfd, size, xnp->descsz);
6968 H_PUT_32 (abfd, type, xnp->type);
6969 dest = xnp->name;
6970 if (name != NULL)
6972 memcpy (dest, name, namesz);
6973 dest += namesz;
6974 while (pad != 0)
6976 *dest++ = '\0';
6977 --pad;
6980 memcpy (dest, input, size);
6981 return p;
6984 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6985 char *
6986 elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6987 bfd *abfd;
6988 char *buf;
6989 int *bufsiz;
6990 const char *fname;
6991 const char *psargs;
6993 int note_type;
6994 char *note_name = "CORE";
6996 #if defined (HAVE_PSINFO_T)
6997 psinfo_t data;
6998 note_type = NT_PSINFO;
6999 #else
7000 prpsinfo_t data;
7001 note_type = NT_PRPSINFO;
7002 #endif
7004 memset (&data, 0, sizeof (data));
7005 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
7006 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
7007 return elfcore_write_note (abfd, buf, bufsiz,
7008 note_name, note_type, &data, sizeof (data));
7010 #endif /* PSINFO_T or PRPSINFO_T */
7012 #if defined (HAVE_PRSTATUS_T)
7013 char *
7014 elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7015 bfd *abfd;
7016 char *buf;
7017 int *bufsiz;
7018 long pid;
7019 int cursig;
7020 const PTR gregs;
7022 prstatus_t prstat;
7023 char *note_name = "CORE";
7025 memset (&prstat, 0, sizeof (prstat));
7026 prstat.pr_pid = pid;
7027 prstat.pr_cursig = cursig;
7028 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
7029 return elfcore_write_note (abfd, buf, bufsiz,
7030 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
7032 #endif /* HAVE_PRSTATUS_T */
7034 #if defined (HAVE_LWPSTATUS_T)
7035 char *
7036 elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7037 bfd *abfd;
7038 char *buf;
7039 int *bufsiz;
7040 long pid;
7041 int cursig;
7042 const PTR gregs;
7044 lwpstatus_t lwpstat;
7045 char *note_name = "CORE";
7047 memset (&lwpstat, 0, sizeof (lwpstat));
7048 lwpstat.pr_lwpid = pid >> 16;
7049 lwpstat.pr_cursig = cursig;
7050 #if defined (HAVE_LWPSTATUS_T_PR_REG)
7051 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg));
7052 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
7053 #if !defined(gregs)
7054 memcpy (lwpstat.pr_context.uc_mcontext.gregs,
7055 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs));
7056 #else
7057 memcpy (lwpstat.pr_context.uc_mcontext.__gregs,
7058 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs));
7059 #endif
7060 #endif
7061 return elfcore_write_note (abfd, buf, bufsiz, note_name,
7062 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat));
7064 #endif /* HAVE_LWPSTATUS_T */
7066 #if defined (HAVE_PSTATUS_T)
7067 char *
7068 elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
7069 bfd *abfd;
7070 char *buf;
7071 int *bufsiz;
7072 long pid;
7073 int cursig;
7074 const PTR gregs;
7076 pstatus_t pstat;
7077 char *note_name = "CORE";
7079 memset (&pstat, 0, sizeof (pstat));
7080 pstat.pr_pid = pid & 0xffff;
7081 buf = elfcore_write_note (abfd, buf, bufsiz, note_name,
7082 NT_PSTATUS, &pstat, sizeof (pstat));
7083 return buf;
7085 #endif /* HAVE_PSTATUS_T */
7087 char *
7088 elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
7089 bfd *abfd;
7090 char *buf;
7091 int *bufsiz;
7092 const PTR fpregs;
7093 int size;
7095 char *note_name = "CORE";
7096 return elfcore_write_note (abfd, buf, bufsiz,
7097 note_name, NT_FPREGSET, fpregs, size);
7100 char *
7101 elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
7102 bfd *abfd;
7103 char *buf;
7104 int *bufsiz;
7105 const PTR xfpregs;
7106 int size;
7108 char *note_name = "LINUX";
7109 return elfcore_write_note (abfd, buf, bufsiz,
7110 note_name, NT_PRXFPREG, xfpregs, size);
7113 static bfd_boolean
7114 elfcore_read_notes (abfd, offset, size)
7115 bfd *abfd;
7116 file_ptr offset;
7117 bfd_size_type size;
7119 char *buf;
7120 char *p;
7122 if (size <= 0)
7123 return TRUE;
7125 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
7126 return FALSE;
7128 buf = bfd_malloc (size);
7129 if (buf == NULL)
7130 return FALSE;
7132 if (bfd_bread (buf, size, abfd) != size)
7134 error:
7135 free (buf);
7136 return FALSE;
7139 p = buf;
7140 while (p < buf + size)
7142 /* FIXME: bad alignment assumption. */
7143 Elf_External_Note *xnp = (Elf_External_Note *) p;
7144 Elf_Internal_Note in;
7146 in.type = H_GET_32 (abfd, xnp->type);
7148 in.namesz = H_GET_32 (abfd, xnp->namesz);
7149 in.namedata = xnp->name;
7151 in.descsz = H_GET_32 (abfd, xnp->descsz);
7152 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
7153 in.descpos = offset + (in.descdata - buf);
7155 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
7157 if (! elfcore_grok_netbsd_note (abfd, &in))
7158 goto error;
7160 else
7162 if (! elfcore_grok_note (abfd, &in))
7163 goto error;
7166 p = in.descdata + BFD_ALIGN (in.descsz, 4);
7169 free (buf);
7170 return TRUE;
7173 /* Providing external access to the ELF program header table. */
7175 /* Return an upper bound on the number of bytes required to store a
7176 copy of ABFD's program header table entries. Return -1 if an error
7177 occurs; bfd_get_error will return an appropriate code. */
7179 long
7180 bfd_get_elf_phdr_upper_bound (abfd)
7181 bfd *abfd;
7183 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7185 bfd_set_error (bfd_error_wrong_format);
7186 return -1;
7189 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
7192 /* Copy ABFD's program header table entries to *PHDRS. The entries
7193 will be stored as an array of Elf_Internal_Phdr structures, as
7194 defined in include/elf/internal.h. To find out how large the
7195 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
7197 Return the number of program header table entries read, or -1 if an
7198 error occurs; bfd_get_error will return an appropriate code. */
7201 bfd_get_elf_phdrs (abfd, phdrs)
7202 bfd *abfd;
7203 void *phdrs;
7205 int num_phdrs;
7207 if (abfd->xvec->flavour != bfd_target_elf_flavour)
7209 bfd_set_error (bfd_error_wrong_format);
7210 return -1;
7213 num_phdrs = elf_elfheader (abfd)->e_phnum;
7214 memcpy (phdrs, elf_tdata (abfd)->phdr,
7215 num_phdrs * sizeof (Elf_Internal_Phdr));
7217 return num_phdrs;
7220 void
7221 _bfd_elf_sprintf_vma (abfd, buf, value)
7222 bfd *abfd ATTRIBUTE_UNUSED;
7223 char *buf;
7224 bfd_vma value;
7226 #ifdef BFD64
7227 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7229 i_ehdrp = elf_elfheader (abfd);
7230 if (i_ehdrp == NULL)
7231 sprintf_vma (buf, value);
7232 else
7234 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7236 #if BFD_HOST_64BIT_LONG
7237 sprintf (buf, "%016lx", value);
7238 #else
7239 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
7240 _bfd_int64_low (value));
7241 #endif
7243 else
7244 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
7246 #else
7247 sprintf_vma (buf, value);
7248 #endif
7251 void
7252 _bfd_elf_fprintf_vma (abfd, stream, value)
7253 bfd *abfd ATTRIBUTE_UNUSED;
7254 PTR stream;
7255 bfd_vma value;
7257 #ifdef BFD64
7258 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
7260 i_ehdrp = elf_elfheader (abfd);
7261 if (i_ehdrp == NULL)
7262 fprintf_vma ((FILE *) stream, value);
7263 else
7265 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
7267 #if BFD_HOST_64BIT_LONG
7268 fprintf ((FILE *) stream, "%016lx", value);
7269 #else
7270 fprintf ((FILE *) stream, "%08lx%08lx",
7271 _bfd_int64_high (value), _bfd_int64_low (value));
7272 #endif
7274 else
7275 fprintf ((FILE *) stream, "%08lx",
7276 (unsigned long) (value & 0xffffffff));
7278 #else
7279 fprintf_vma ((FILE *) stream, value);
7280 #endif
7283 enum elf_reloc_type_class
7284 _bfd_elf_reloc_type_class (rela)
7285 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
7287 return reloc_class_normal;
7290 /* For RELA architectures, return the relocation value for a
7291 relocation against a local symbol. */
7293 bfd_vma
7294 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
7295 bfd *abfd;
7296 Elf_Internal_Sym *sym;
7297 asection *sec;
7298 Elf_Internal_Rela *rel;
7300 bfd_vma relocation;
7302 relocation = (sec->output_section->vma
7303 + sec->output_offset
7304 + sym->st_value);
7305 if ((sec->flags & SEC_MERGE)
7306 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
7307 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
7309 asection *msec;
7311 msec = sec;
7312 rel->r_addend =
7313 _bfd_merged_section_offset (abfd, &msec,
7314 elf_section_data (sec)->sec_info,
7315 sym->st_value + rel->r_addend,
7316 (bfd_vma) 0)
7317 - relocation;
7318 rel->r_addend += msec->output_section->vma + msec->output_offset;
7320 return relocation;
7323 bfd_vma
7324 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
7325 bfd *abfd;
7326 Elf_Internal_Sym *sym;
7327 asection **psec;
7328 bfd_vma addend;
7330 asection *sec = *psec;
7332 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
7333 return sym->st_value + addend;
7335 return _bfd_merged_section_offset (abfd, psec,
7336 elf_section_data (sec)->sec_info,
7337 sym->st_value + addend, (bfd_vma) 0);
7340 bfd_vma
7341 _bfd_elf_section_offset (abfd, info, sec, offset)
7342 bfd *abfd;
7343 struct bfd_link_info *info;
7344 asection *sec;
7345 bfd_vma offset;
7347 struct bfd_elf_section_data *sec_data;
7349 sec_data = elf_section_data (sec);
7350 switch (sec_data->sec_info_type)
7352 case ELF_INFO_TYPE_STABS:
7353 return _bfd_stab_section_offset (abfd,
7354 &elf_hash_table (info)->merge_info,
7355 sec, &sec_data->sec_info, offset);
7356 case ELF_INFO_TYPE_EH_FRAME:
7357 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
7358 default:
7359 return offset;