Reset AIX vector function overrides for HP-UX.
[binutils.git] / bfd / elf.c
blob1df8459a9afef594bd915ba7ffa346455010986e
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. */
23 SECTION
24 ELF backends
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet.
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "bfd.h"
38 #include "sysdep.h"
39 #include "bfdlink.h"
40 #include "libbfd.h"
41 #define ARCH_SIZE 0
42 #include "elf-bfd.h"
43 #include "libiberty.h"
45 static INLINE struct elf_segment_map *make_mapping
46 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
47 static boolean map_sections_to_segments PARAMS ((bfd *));
48 static int elf_sort_sections PARAMS ((const PTR, const PTR));
49 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
50 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
51 static boolean prep_headers PARAMS ((bfd *));
52 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
53 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
54 static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
55 static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
56 static void merge_sections_remove_hook PARAMS ((bfd *, asection *));
57 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
58 static void set_group_contents PARAMS ((bfd *, asection *, PTR));
59 static boolean assign_section_numbers PARAMS ((bfd *));
60 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
61 static boolean elf_map_symbols PARAMS ((bfd *));
62 static bfd_size_type get_program_header_size PARAMS ((bfd *));
63 static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
64 static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
65 bfd_vma, const char **,
66 const char **));
67 static int elfcore_make_pid PARAMS ((bfd *));
68 static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
69 static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
70 Elf_Internal_Note *));
71 static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
72 static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
73 static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
75 static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *));
76 static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *,
77 Elf_Internal_Note *));
78 static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *));
80 /* Swap version information in and out. The version information is
81 currently size independent. If that ever changes, this code will
82 need to move into elfcode.h. */
84 /* Swap in a Verdef structure. */
86 void
87 _bfd_elf_swap_verdef_in (abfd, src, dst)
88 bfd *abfd;
89 const Elf_External_Verdef *src;
90 Elf_Internal_Verdef *dst;
92 dst->vd_version = H_GET_16 (abfd, src->vd_version);
93 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
94 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
95 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
96 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
97 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
98 dst->vd_next = H_GET_32 (abfd, src->vd_next);
101 /* Swap out a Verdef structure. */
103 void
104 _bfd_elf_swap_verdef_out (abfd, src, dst)
105 bfd *abfd;
106 const Elf_Internal_Verdef *src;
107 Elf_External_Verdef *dst;
109 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
110 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
111 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
112 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
113 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
114 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
115 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
118 /* Swap in a Verdaux structure. */
120 void
121 _bfd_elf_swap_verdaux_in (abfd, src, dst)
122 bfd *abfd;
123 const Elf_External_Verdaux *src;
124 Elf_Internal_Verdaux *dst;
126 dst->vda_name = H_GET_32 (abfd, src->vda_name);
127 dst->vda_next = H_GET_32 (abfd, src->vda_next);
130 /* Swap out a Verdaux structure. */
132 void
133 _bfd_elf_swap_verdaux_out (abfd, src, dst)
134 bfd *abfd;
135 const Elf_Internal_Verdaux *src;
136 Elf_External_Verdaux *dst;
138 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
139 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
142 /* Swap in a Verneed structure. */
144 void
145 _bfd_elf_swap_verneed_in (abfd, src, dst)
146 bfd *abfd;
147 const Elf_External_Verneed *src;
148 Elf_Internal_Verneed *dst;
150 dst->vn_version = H_GET_16 (abfd, src->vn_version);
151 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
152 dst->vn_file = H_GET_32 (abfd, src->vn_file);
153 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
154 dst->vn_next = H_GET_32 (abfd, src->vn_next);
157 /* Swap out a Verneed structure. */
159 void
160 _bfd_elf_swap_verneed_out (abfd, src, dst)
161 bfd *abfd;
162 const Elf_Internal_Verneed *src;
163 Elf_External_Verneed *dst;
165 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
166 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
167 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
168 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
169 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
172 /* Swap in a Vernaux structure. */
174 void
175 _bfd_elf_swap_vernaux_in (abfd, src, dst)
176 bfd *abfd;
177 const Elf_External_Vernaux *src;
178 Elf_Internal_Vernaux *dst;
180 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
181 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
182 dst->vna_other = H_GET_16 (abfd, src->vna_other);
183 dst->vna_name = H_GET_32 (abfd, src->vna_name);
184 dst->vna_next = H_GET_32 (abfd, src->vna_next);
187 /* Swap out a Vernaux structure. */
189 void
190 _bfd_elf_swap_vernaux_out (abfd, src, dst)
191 bfd *abfd;
192 const Elf_Internal_Vernaux *src;
193 Elf_External_Vernaux *dst;
195 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
196 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
197 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
198 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
199 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
202 /* Swap in a Versym structure. */
204 void
205 _bfd_elf_swap_versym_in (abfd, src, dst)
206 bfd *abfd;
207 const Elf_External_Versym *src;
208 Elf_Internal_Versym *dst;
210 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
213 /* Swap out a Versym structure. */
215 void
216 _bfd_elf_swap_versym_out (abfd, src, dst)
217 bfd *abfd;
218 const Elf_Internal_Versym *src;
219 Elf_External_Versym *dst;
221 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
224 /* Standard ELF hash function. Do not change this function; you will
225 cause invalid hash tables to be generated. */
227 unsigned long
228 bfd_elf_hash (namearg)
229 const char *namearg;
231 const unsigned char *name = (const unsigned char *) namearg;
232 unsigned long h = 0;
233 unsigned long g;
234 int ch;
236 while ((ch = *name++) != '\0')
238 h = (h << 4) + ch;
239 if ((g = (h & 0xf0000000)) != 0)
241 h ^= g >> 24;
242 /* The ELF ABI says `h &= ~g', but this is equivalent in
243 this case and on some machines one insn instead of two. */
244 h ^= g;
247 return h;
250 /* Read a specified number of bytes at a specified offset in an ELF
251 file, into a newly allocated buffer, and return a pointer to the
252 buffer. */
254 static char *
255 elf_read (abfd, offset, size)
256 bfd *abfd;
257 file_ptr offset;
258 bfd_size_type size;
260 char *buf;
262 if ((buf = bfd_alloc (abfd, size)) == NULL)
263 return NULL;
264 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
265 return NULL;
266 if (bfd_bread ((PTR) buf, size, abfd) != size)
268 if (bfd_get_error () != bfd_error_system_call)
269 bfd_set_error (bfd_error_file_truncated);
270 return NULL;
272 return buf;
275 boolean
276 bfd_elf_mkobject (abfd)
277 bfd *abfd;
279 /* This just does initialization. */
280 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
281 bfd_size_type amt = sizeof (struct elf_obj_tdata);
282 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
283 if (elf_tdata (abfd) == 0)
284 return false;
285 /* Since everything is done at close time, do we need any
286 initialization? */
288 return true;
291 boolean
292 bfd_elf_mkcorefile (abfd)
293 bfd *abfd;
295 /* I think this can be done just like an object file. */
296 return bfd_elf_mkobject (abfd);
299 char *
300 bfd_elf_get_str_section (abfd, shindex)
301 bfd *abfd;
302 unsigned int shindex;
304 Elf_Internal_Shdr **i_shdrp;
305 char *shstrtab = NULL;
306 file_ptr offset;
307 bfd_size_type shstrtabsize;
309 i_shdrp = elf_elfsections (abfd);
310 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
311 return 0;
313 shstrtab = (char *) i_shdrp[shindex]->contents;
314 if (shstrtab == NULL)
316 /* No cached one, attempt to read, and cache what we read. */
317 offset = i_shdrp[shindex]->sh_offset;
318 shstrtabsize = i_shdrp[shindex]->sh_size;
319 shstrtab = elf_read (abfd, offset, shstrtabsize);
320 i_shdrp[shindex]->contents = (PTR) shstrtab;
322 return shstrtab;
325 char *
326 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
327 bfd *abfd;
328 unsigned int shindex;
329 unsigned int strindex;
331 Elf_Internal_Shdr *hdr;
333 if (strindex == 0)
334 return "";
336 hdr = elf_elfsections (abfd)[shindex];
338 if (hdr->contents == NULL
339 && bfd_elf_get_str_section (abfd, shindex) == NULL)
340 return NULL;
342 if (strindex >= hdr->sh_size)
344 (*_bfd_error_handler)
345 (_("%s: invalid string offset %u >= %lu for section `%s'"),
346 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
347 ((shindex == elf_elfheader(abfd)->e_shstrndx
348 && strindex == hdr->sh_name)
349 ? ".shstrtab"
350 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
351 return "";
354 return ((char *) hdr->contents) + strindex;
357 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
358 sections. The first element is the flags, the rest are section
359 pointers. */
361 typedef union elf_internal_group {
362 Elf_Internal_Shdr *shdr;
363 unsigned int flags;
364 } Elf_Internal_Group;
366 /* Set next_in_group list pointer, and group name for NEWSECT. */
368 static boolean
369 setup_group (abfd, hdr, newsect)
370 bfd *abfd;
371 Elf_Internal_Shdr *hdr;
372 asection *newsect;
374 unsigned int num_group = elf_tdata (abfd)->num_group;
376 /* If num_group is zero, read in all SHT_GROUP sections. The count
377 is set to -1 if there are no SHT_GROUP sections. */
378 if (num_group == 0)
380 unsigned int i, shnum;
382 /* First count the number of groups. If we have a SHT_GROUP
383 section with just a flag word (ie. sh_size is 4), ignore it. */
384 shnum = elf_numsections (abfd);
385 num_group = 0;
386 for (i = 0; i < shnum; i++)
388 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
389 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
390 num_group += 1;
393 if (num_group == 0)
394 num_group = (unsigned) -1;
395 elf_tdata (abfd)->num_group = num_group;
397 if (num_group > 0)
399 /* We keep a list of elf section headers for group sections,
400 so we can find them quickly. */
401 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
402 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
403 if (elf_tdata (abfd)->group_sect_ptr == NULL)
404 return false;
406 num_group = 0;
407 for (i = 0; i < shnum; i++)
409 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
410 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
412 unsigned char *src;
413 Elf_Internal_Group *dest;
415 /* Add to list of sections. */
416 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
417 num_group += 1;
419 /* Read the raw contents. */
420 BFD_ASSERT (sizeof (*dest) >= 4);
421 amt = shdr->sh_size * sizeof (*dest) / 4;
422 shdr->contents = bfd_alloc (abfd, amt);
423 if (shdr->contents == NULL
424 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
425 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
426 != shdr->sh_size))
427 return false;
429 /* Translate raw contents, a flag word followed by an
430 array of elf section indices all in target byte order,
431 to the flag word followed by an array of elf section
432 pointers. */
433 src = shdr->contents + shdr->sh_size;
434 dest = (Elf_Internal_Group *) (shdr->contents + amt);
435 while (1)
437 unsigned int idx;
439 src -= 4;
440 --dest;
441 idx = H_GET_32 (abfd, src);
442 if (src == shdr->contents)
444 dest->flags = idx;
445 break;
447 if (idx >= shnum)
449 ((*_bfd_error_handler)
450 (_("%s: invalid SHT_GROUP entry"),
451 bfd_archive_filename (abfd)));
452 idx = 0;
454 dest->shdr = elf_elfsections (abfd)[idx];
461 if (num_group != (unsigned) -1)
463 unsigned int i;
465 for (i = 0; i < num_group; i++)
467 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
468 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
469 unsigned int n_elt = shdr->sh_size / 4;
471 /* Look through this group's sections to see if current
472 section is a member. */
473 while (--n_elt != 0)
474 if ((++idx)->shdr == hdr)
476 asection *s = NULL;
478 /* We are a member of this group. Go looking through
479 other members to see if any others are linked via
480 next_in_group. */
481 idx = (Elf_Internal_Group *) shdr->contents;
482 n_elt = shdr->sh_size / 4;
483 while (--n_elt != 0)
484 if ((s = (++idx)->shdr->bfd_section) != NULL
485 && elf_next_in_group (s) != NULL)
486 break;
487 if (n_elt != 0)
489 /* Snarf the group name from other member, and
490 insert current section in circular list. */
491 elf_group_name (newsect) = elf_group_name (s);
492 elf_next_in_group (newsect) = elf_next_in_group (s);
493 elf_next_in_group (s) = newsect;
495 else
497 struct elf_backend_data *bed;
498 file_ptr pos;
499 unsigned char ename[4];
500 unsigned long iname;
501 const char *gname;
503 /* Humbug. Get the name from the group signature
504 symbol. Why isn't the signature just a string?
505 Fortunately, the name index is at the same
506 place in the external symbol for both 32 and 64
507 bit ELF. */
508 bed = get_elf_backend_data (abfd);
509 pos = elf_tdata (abfd)->symtab_hdr.sh_offset;
510 pos += shdr->sh_info * bed->s->sizeof_sym;
511 if (bfd_seek (abfd, pos, SEEK_SET) != 0
512 || bfd_bread (ename, (bfd_size_type) 4, abfd) != 4)
513 return false;
514 iname = H_GET_32 (abfd, ename);
515 gname = elf_string_from_elf_strtab (abfd, iname);
516 elf_group_name (newsect) = gname;
518 /* Start a circular list with one element. */
519 elf_next_in_group (newsect) = newsect;
521 if (shdr->bfd_section != NULL)
522 elf_next_in_group (shdr->bfd_section) = newsect;
523 i = num_group - 1;
524 break;
529 if (elf_group_name (newsect) == NULL)
531 (*_bfd_error_handler) (_("%s: no group info for section %s"),
532 bfd_archive_filename (abfd), newsect->name);
534 return true;
537 /* Make a BFD section from an ELF section. We store a pointer to the
538 BFD section in the bfd_section field of the header. */
540 boolean
541 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
542 bfd *abfd;
543 Elf_Internal_Shdr *hdr;
544 const char *name;
546 asection *newsect;
547 flagword flags;
548 struct elf_backend_data *bed;
550 if (hdr->bfd_section != NULL)
552 BFD_ASSERT (strcmp (name,
553 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
554 return true;
557 newsect = bfd_make_section_anyway (abfd, name);
558 if (newsect == NULL)
559 return false;
561 newsect->filepos = hdr->sh_offset;
563 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
564 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
565 || ! bfd_set_section_alignment (abfd, newsect,
566 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
567 return false;
569 flags = SEC_NO_FLAGS;
570 if (hdr->sh_type != SHT_NOBITS)
571 flags |= SEC_HAS_CONTENTS;
572 if (hdr->sh_type == SHT_GROUP)
573 flags |= SEC_GROUP | SEC_EXCLUDE;
574 if ((hdr->sh_flags & SHF_ALLOC) != 0)
576 flags |= SEC_ALLOC;
577 if (hdr->sh_type != SHT_NOBITS)
578 flags |= SEC_LOAD;
580 if ((hdr->sh_flags & SHF_WRITE) == 0)
581 flags |= SEC_READONLY;
582 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
583 flags |= SEC_CODE;
584 else if ((flags & SEC_LOAD) != 0)
585 flags |= SEC_DATA;
586 if ((hdr->sh_flags & SHF_MERGE) != 0)
588 flags |= SEC_MERGE;
589 newsect->entsize = hdr->sh_entsize;
590 if ((hdr->sh_flags & SHF_STRINGS) != 0)
591 flags |= SEC_STRINGS;
593 if (hdr->sh_flags & SHF_GROUP)
594 if (!setup_group (abfd, hdr, newsect))
595 return false;
597 /* The debugging sections appear to be recognized only by name, not
598 any sort of flag. */
600 static const char *debug_sec_names [] =
602 ".debug",
603 ".gnu.linkonce.wi.",
604 ".line",
605 ".stab"
607 int i;
609 for (i = ARRAY_SIZE (debug_sec_names); i--;)
610 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
611 break;
613 if (i >= 0)
614 flags |= SEC_DEBUGGING;
617 /* As a GNU extension, if the name begins with .gnu.linkonce, we
618 only link a single copy of the section. This is used to support
619 g++. g++ will emit each template expansion in its own section.
620 The symbols will be defined as weak, so that multiple definitions
621 are permitted. The GNU linker extension is to actually discard
622 all but one of the sections. */
623 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
624 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
626 bed = get_elf_backend_data (abfd);
627 if (bed->elf_backend_section_flags)
628 if (! bed->elf_backend_section_flags (&flags, hdr))
629 return false;
631 if (! bfd_set_section_flags (abfd, newsect, flags))
632 return false;
634 if ((flags & SEC_ALLOC) != 0)
636 Elf_Internal_Phdr *phdr;
637 unsigned int i;
639 /* Look through the phdrs to see if we need to adjust the lma.
640 If all the p_paddr fields are zero, we ignore them, since
641 some ELF linkers produce such output. */
642 phdr = elf_tdata (abfd)->phdr;
643 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
645 if (phdr->p_paddr != 0)
646 break;
648 if (i < elf_elfheader (abfd)->e_phnum)
650 phdr = elf_tdata (abfd)->phdr;
651 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
653 /* This section is part of this segment if its file
654 offset plus size lies within the segment's memory
655 span and, if the section is loaded, the extent of the
656 loaded data lies within the extent of the segment.
657 If the p_paddr field is not set, we don't alter the
658 LMA. */
659 if (phdr->p_type == PT_LOAD
660 && phdr->p_paddr
661 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
662 && (hdr->sh_offset + hdr->sh_size
663 <= phdr->p_offset + phdr->p_memsz)
664 && ((flags & SEC_LOAD) == 0
665 || (phdr->p_offset + phdr->p_filesz
666 >= hdr->sh_offset + hdr->sh_size)))
668 /* We used to do a relative adjustment here, but
669 that doesn't work if the segment is packed with
670 code from multiple VMAs. Instead we calculate
671 the LMA absoultely, based on the LMA of the
672 segment (it is assumed that the segment will
673 contain sections with contiguous LMAs, even if
674 the VMAs are not). */
675 newsect->lma = phdr->p_paddr
676 + hdr->sh_offset - phdr->p_offset;
677 break;
683 hdr->bfd_section = newsect;
684 elf_section_data (newsect)->this_hdr = *hdr;
686 return true;
690 INTERNAL_FUNCTION
691 bfd_elf_find_section
693 SYNOPSIS
694 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
696 DESCRIPTION
697 Helper functions for GDB to locate the string tables.
698 Since BFD hides string tables from callers, GDB needs to use an
699 internal hook to find them. Sun's .stabstr, in particular,
700 isn't even pointed to by the .stab section, so ordinary
701 mechanisms wouldn't work to find it, even if we had some.
704 struct elf_internal_shdr *
705 bfd_elf_find_section (abfd, name)
706 bfd *abfd;
707 char *name;
709 Elf_Internal_Shdr **i_shdrp;
710 char *shstrtab;
711 unsigned int max;
712 unsigned int i;
714 i_shdrp = elf_elfsections (abfd);
715 if (i_shdrp != NULL)
717 shstrtab = bfd_elf_get_str_section (abfd,
718 elf_elfheader (abfd)->e_shstrndx);
719 if (shstrtab != NULL)
721 max = elf_numsections (abfd);
722 for (i = 1; i < max; i++)
723 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
724 return i_shdrp[i];
727 return 0;
730 const char *const bfd_elf_section_type_names[] = {
731 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
732 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
733 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
736 /* ELF relocs are against symbols. If we are producing relocateable
737 output, and the reloc is against an external symbol, and nothing
738 has given us any additional addend, the resulting reloc will also
739 be against the same symbol. In such a case, we don't want to
740 change anything about the way the reloc is handled, since it will
741 all be done at final link time. Rather than put special case code
742 into bfd_perform_relocation, all the reloc types use this howto
743 function. It just short circuits the reloc if producing
744 relocateable output against an external symbol. */
746 bfd_reloc_status_type
747 bfd_elf_generic_reloc (abfd,
748 reloc_entry,
749 symbol,
750 data,
751 input_section,
752 output_bfd,
753 error_message)
754 bfd *abfd ATTRIBUTE_UNUSED;
755 arelent *reloc_entry;
756 asymbol *symbol;
757 PTR data ATTRIBUTE_UNUSED;
758 asection *input_section;
759 bfd *output_bfd;
760 char **error_message ATTRIBUTE_UNUSED;
762 if (output_bfd != (bfd *) NULL
763 && (symbol->flags & BSF_SECTION_SYM) == 0
764 && (! reloc_entry->howto->partial_inplace
765 || reloc_entry->addend == 0))
767 reloc_entry->address += input_section->output_offset;
768 return bfd_reloc_ok;
771 return bfd_reloc_continue;
774 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
776 static void
777 merge_sections_remove_hook (abfd, sec)
778 bfd *abfd ATTRIBUTE_UNUSED;
779 asection *sec;
781 struct bfd_elf_section_data *sec_data;
783 sec_data = elf_section_data (sec);
784 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);
785 sec_data->sec_info_type = ELF_INFO_TYPE_NONE;
788 /* Finish SHF_MERGE section merging. */
790 boolean
791 _bfd_elf_merge_sections (abfd, info)
792 bfd *abfd;
793 struct bfd_link_info *info;
795 if (!is_elf_hash_table (info))
796 return false;
797 if (elf_hash_table (info)->merge_info)
798 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
799 merge_sections_remove_hook);
800 return true;
803 /* Copy the program header and other data from one object module to
804 another. */
806 boolean
807 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
808 bfd *ibfd;
809 bfd *obfd;
811 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
812 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
813 return true;
815 BFD_ASSERT (!elf_flags_init (obfd)
816 || (elf_elfheader (obfd)->e_flags
817 == elf_elfheader (ibfd)->e_flags));
819 elf_gp (obfd) = elf_gp (ibfd);
820 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
821 elf_flags_init (obfd) = true;
822 return true;
825 /* Print out the program headers. */
827 boolean
828 _bfd_elf_print_private_bfd_data (abfd, farg)
829 bfd *abfd;
830 PTR farg;
832 FILE *f = (FILE *) farg;
833 Elf_Internal_Phdr *p;
834 asection *s;
835 bfd_byte *dynbuf = NULL;
837 p = elf_tdata (abfd)->phdr;
838 if (p != NULL)
840 unsigned int i, c;
842 fprintf (f, _("\nProgram Header:\n"));
843 c = elf_elfheader (abfd)->e_phnum;
844 for (i = 0; i < c; i++, p++)
846 const char *pt;
847 char buf[20];
849 switch (p->p_type)
851 case PT_NULL: pt = "NULL"; break;
852 case PT_LOAD: pt = "LOAD"; break;
853 case PT_DYNAMIC: pt = "DYNAMIC"; break;
854 case PT_INTERP: pt = "INTERP"; break;
855 case PT_NOTE: pt = "NOTE"; break;
856 case PT_SHLIB: pt = "SHLIB"; break;
857 case PT_PHDR: pt = "PHDR"; break;
858 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
859 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
861 fprintf (f, "%8s off 0x", pt);
862 bfd_fprintf_vma (abfd, f, p->p_offset);
863 fprintf (f, " vaddr 0x");
864 bfd_fprintf_vma (abfd, f, p->p_vaddr);
865 fprintf (f, " paddr 0x");
866 bfd_fprintf_vma (abfd, f, p->p_paddr);
867 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
868 fprintf (f, " filesz 0x");
869 bfd_fprintf_vma (abfd, f, p->p_filesz);
870 fprintf (f, " memsz 0x");
871 bfd_fprintf_vma (abfd, f, p->p_memsz);
872 fprintf (f, " flags %c%c%c",
873 (p->p_flags & PF_R) != 0 ? 'r' : '-',
874 (p->p_flags & PF_W) != 0 ? 'w' : '-',
875 (p->p_flags & PF_X) != 0 ? 'x' : '-');
876 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
877 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
878 fprintf (f, "\n");
882 s = bfd_get_section_by_name (abfd, ".dynamic");
883 if (s != NULL)
885 int elfsec;
886 unsigned long shlink;
887 bfd_byte *extdyn, *extdynend;
888 size_t extdynsize;
889 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
891 fprintf (f, _("\nDynamic Section:\n"));
893 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
894 if (dynbuf == NULL)
895 goto error_return;
896 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
897 s->_raw_size))
898 goto error_return;
900 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
901 if (elfsec == -1)
902 goto error_return;
903 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
905 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
906 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
908 extdyn = dynbuf;
909 extdynend = extdyn + s->_raw_size;
910 for (; extdyn < extdynend; extdyn += extdynsize)
912 Elf_Internal_Dyn dyn;
913 const char *name;
914 char ab[20];
915 boolean stringp;
917 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
919 if (dyn.d_tag == DT_NULL)
920 break;
922 stringp = false;
923 switch (dyn.d_tag)
925 default:
926 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
927 name = ab;
928 break;
930 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
931 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
932 case DT_PLTGOT: name = "PLTGOT"; break;
933 case DT_HASH: name = "HASH"; break;
934 case DT_STRTAB: name = "STRTAB"; break;
935 case DT_SYMTAB: name = "SYMTAB"; break;
936 case DT_RELA: name = "RELA"; break;
937 case DT_RELASZ: name = "RELASZ"; break;
938 case DT_RELAENT: name = "RELAENT"; break;
939 case DT_STRSZ: name = "STRSZ"; break;
940 case DT_SYMENT: name = "SYMENT"; break;
941 case DT_INIT: name = "INIT"; break;
942 case DT_FINI: name = "FINI"; break;
943 case DT_SONAME: name = "SONAME"; stringp = true; break;
944 case DT_RPATH: name = "RPATH"; stringp = true; break;
945 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
946 case DT_REL: name = "REL"; break;
947 case DT_RELSZ: name = "RELSZ"; break;
948 case DT_RELENT: name = "RELENT"; break;
949 case DT_PLTREL: name = "PLTREL"; break;
950 case DT_DEBUG: name = "DEBUG"; break;
951 case DT_TEXTREL: name = "TEXTREL"; break;
952 case DT_JMPREL: name = "JMPREL"; break;
953 case DT_BIND_NOW: name = "BIND_NOW"; break;
954 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
955 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
956 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
957 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
958 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
959 case DT_FLAGS: name = "FLAGS"; break;
960 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
961 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
962 case DT_CHECKSUM: name = "CHECKSUM"; break;
963 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
964 case DT_MOVEENT: name = "MOVEENT"; break;
965 case DT_MOVESZ: name = "MOVESZ"; break;
966 case DT_FEATURE: name = "FEATURE"; break;
967 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
968 case DT_SYMINSZ: name = "SYMINSZ"; break;
969 case DT_SYMINENT: name = "SYMINENT"; break;
970 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
971 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
972 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
973 case DT_PLTPAD: name = "PLTPAD"; break;
974 case DT_MOVETAB: name = "MOVETAB"; break;
975 case DT_SYMINFO: name = "SYMINFO"; break;
976 case DT_RELACOUNT: name = "RELACOUNT"; break;
977 case DT_RELCOUNT: name = "RELCOUNT"; break;
978 case DT_FLAGS_1: name = "FLAGS_1"; break;
979 case DT_VERSYM: name = "VERSYM"; break;
980 case DT_VERDEF: name = "VERDEF"; break;
981 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
982 case DT_VERNEED: name = "VERNEED"; break;
983 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
984 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
985 case DT_USED: name = "USED"; break;
986 case DT_FILTER: name = "FILTER"; stringp = true; break;
989 fprintf (f, " %-11s ", name);
990 if (! stringp)
991 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
992 else
994 const char *string;
995 unsigned int tagv = dyn.d_un.d_val;
997 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
998 if (string == NULL)
999 goto error_return;
1000 fprintf (f, "%s", string);
1002 fprintf (f, "\n");
1005 free (dynbuf);
1006 dynbuf = NULL;
1009 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1010 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1012 if (! _bfd_elf_slurp_version_tables (abfd))
1013 return false;
1016 if (elf_dynverdef (abfd) != 0)
1018 Elf_Internal_Verdef *t;
1020 fprintf (f, _("\nVersion definitions:\n"));
1021 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1023 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1024 t->vd_flags, t->vd_hash, t->vd_nodename);
1025 if (t->vd_auxptr->vda_nextptr != NULL)
1027 Elf_Internal_Verdaux *a;
1029 fprintf (f, "\t");
1030 for (a = t->vd_auxptr->vda_nextptr;
1031 a != NULL;
1032 a = a->vda_nextptr)
1033 fprintf (f, "%s ", a->vda_nodename);
1034 fprintf (f, "\n");
1039 if (elf_dynverref (abfd) != 0)
1041 Elf_Internal_Verneed *t;
1043 fprintf (f, _("\nVersion References:\n"));
1044 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1046 Elf_Internal_Vernaux *a;
1048 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1049 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1050 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1051 a->vna_flags, a->vna_other, a->vna_nodename);
1055 return true;
1057 error_return:
1058 if (dynbuf != NULL)
1059 free (dynbuf);
1060 return false;
1063 /* Display ELF-specific fields of a symbol. */
1065 void
1066 bfd_elf_print_symbol (abfd, filep, symbol, how)
1067 bfd *abfd;
1068 PTR filep;
1069 asymbol *symbol;
1070 bfd_print_symbol_type how;
1072 FILE *file = (FILE *) filep;
1073 switch (how)
1075 case bfd_print_symbol_name:
1076 fprintf (file, "%s", symbol->name);
1077 break;
1078 case bfd_print_symbol_more:
1079 fprintf (file, "elf ");
1080 bfd_fprintf_vma (abfd, file, symbol->value);
1081 fprintf (file, " %lx", (long) symbol->flags);
1082 break;
1083 case bfd_print_symbol_all:
1085 const char *section_name;
1086 const char *name = NULL;
1087 struct elf_backend_data *bed;
1088 unsigned char st_other;
1089 bfd_vma val;
1091 section_name = symbol->section ? symbol->section->name : "(*none*)";
1093 bed = get_elf_backend_data (abfd);
1094 if (bed->elf_backend_print_symbol_all)
1095 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1097 if (name == NULL)
1099 name = symbol->name;
1100 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1103 fprintf (file, " %s\t", section_name);
1104 /* Print the "other" value for a symbol. For common symbols,
1105 we've already printed the size; now print the alignment.
1106 For other symbols, we have no specified alignment, and
1107 we've printed the address; now print the size. */
1108 if (bfd_is_com_section (symbol->section))
1109 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1110 else
1111 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1112 bfd_fprintf_vma (abfd, file, val);
1114 /* If we have version information, print it. */
1115 if (elf_tdata (abfd)->dynversym_section != 0
1116 && (elf_tdata (abfd)->dynverdef_section != 0
1117 || elf_tdata (abfd)->dynverref_section != 0))
1119 unsigned int vernum;
1120 const char *version_string;
1122 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1124 if (vernum == 0)
1125 version_string = "";
1126 else if (vernum == 1)
1127 version_string = "Base";
1128 else if (vernum <= elf_tdata (abfd)->cverdefs)
1129 version_string =
1130 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1131 else
1133 Elf_Internal_Verneed *t;
1135 version_string = "";
1136 for (t = elf_tdata (abfd)->verref;
1137 t != NULL;
1138 t = t->vn_nextref)
1140 Elf_Internal_Vernaux *a;
1142 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1144 if (a->vna_other == vernum)
1146 version_string = a->vna_nodename;
1147 break;
1153 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1154 fprintf (file, " %-11s", version_string);
1155 else
1157 int i;
1159 fprintf (file, " (%s)", version_string);
1160 for (i = 10 - strlen (version_string); i > 0; --i)
1161 putc (' ', file);
1165 /* If the st_other field is not zero, print it. */
1166 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1168 switch (st_other)
1170 case 0: break;
1171 case STV_INTERNAL: fprintf (file, " .internal"); break;
1172 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1173 case STV_PROTECTED: fprintf (file, " .protected"); break;
1174 default:
1175 /* Some other non-defined flags are also present, so print
1176 everything hex. */
1177 fprintf (file, " 0x%02x", (unsigned int) st_other);
1180 fprintf (file, " %s", name);
1182 break;
1186 /* Create an entry in an ELF linker hash table. */
1188 struct bfd_hash_entry *
1189 _bfd_elf_link_hash_newfunc (entry, table, string)
1190 struct bfd_hash_entry *entry;
1191 struct bfd_hash_table *table;
1192 const char *string;
1194 /* Allocate the structure if it has not already been allocated by a
1195 subclass. */
1196 if (entry == NULL)
1198 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1199 if (entry == NULL)
1200 return entry;
1203 /* Call the allocation method of the superclass. */
1204 entry = _bfd_link_hash_newfunc (entry, table, string);
1205 if (entry != NULL)
1207 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1208 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1210 /* Set local fields. */
1211 ret->indx = -1;
1212 ret->size = 0;
1213 ret->dynindx = -1;
1214 ret->dynstr_index = 0;
1215 ret->weakdef = NULL;
1216 ret->got.refcount = htab->init_refcount;
1217 ret->plt.refcount = htab->init_refcount;
1218 ret->linker_section_pointer = NULL;
1219 ret->verinfo.verdef = NULL;
1220 ret->vtable_entries_used = NULL;
1221 ret->vtable_entries_size = 0;
1222 ret->vtable_parent = NULL;
1223 ret->type = STT_NOTYPE;
1224 ret->other = 0;
1225 /* Assume that we have been called by a non-ELF symbol reader.
1226 This flag is then reset by the code which reads an ELF input
1227 file. This ensures that a symbol created by a non-ELF symbol
1228 reader will have the flag set correctly. */
1229 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1232 return entry;
1235 /* Copy data from an indirect symbol to its direct symbol, hiding the
1236 old indirect symbol. Also used for copying flags to a weakdef. */
1238 void
1239 _bfd_elf_link_hash_copy_indirect (dir, ind)
1240 struct elf_link_hash_entry *dir, *ind;
1242 bfd_signed_vma tmp;
1244 /* Copy down any references that we may have already seen to the
1245 symbol which just became indirect. */
1247 dir->elf_link_hash_flags |=
1248 (ind->elf_link_hash_flags
1249 & (ELF_LINK_HASH_REF_DYNAMIC
1250 | ELF_LINK_HASH_REF_REGULAR
1251 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1252 | ELF_LINK_NON_GOT_REF));
1254 if (ind->root.type != bfd_link_hash_indirect)
1255 return;
1257 /* Copy over the global and procedure linkage table refcount entries.
1258 These may have been already set up by a check_relocs routine. */
1259 tmp = dir->got.refcount;
1260 if (tmp <= 0)
1262 dir->got.refcount = ind->got.refcount;
1263 ind->got.refcount = tmp;
1265 else
1266 BFD_ASSERT (ind->got.refcount <= 0);
1268 tmp = dir->plt.refcount;
1269 if (tmp <= 0)
1271 dir->plt.refcount = ind->plt.refcount;
1272 ind->plt.refcount = tmp;
1274 else
1275 BFD_ASSERT (ind->plt.refcount <= 0);
1277 if (dir->dynindx == -1)
1279 dir->dynindx = ind->dynindx;
1280 dir->dynstr_index = ind->dynstr_index;
1281 ind->dynindx = -1;
1282 ind->dynstr_index = 0;
1284 else
1285 BFD_ASSERT (ind->dynindx == -1);
1288 void
1289 _bfd_elf_link_hash_hide_symbol (info, h, force_local)
1290 struct bfd_link_info *info;
1291 struct elf_link_hash_entry *h;
1292 boolean force_local;
1294 h->plt.offset = (bfd_vma) -1;
1295 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1296 if (force_local)
1298 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
1299 if (h->dynindx != -1)
1301 h->dynindx = -1;
1302 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1303 h->dynstr_index);
1308 /* Initialize an ELF linker hash table. */
1310 boolean
1311 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1312 struct elf_link_hash_table *table;
1313 bfd *abfd;
1314 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1315 struct bfd_hash_table *,
1316 const char *));
1318 boolean ret;
1320 table->dynamic_sections_created = false;
1321 table->dynobj = NULL;
1322 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
1323 /* The first dynamic symbol is a dummy. */
1324 table->dynsymcount = 1;
1325 table->dynstr = NULL;
1326 table->bucketcount = 0;
1327 table->needed = NULL;
1328 table->runpath = NULL;
1329 table->hgot = NULL;
1330 table->stab_info = NULL;
1331 table->merge_info = NULL;
1332 table->dynlocal = NULL;
1333 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1334 table->root.type = bfd_link_elf_hash_table;
1336 return ret;
1339 /* Create an ELF linker hash table. */
1341 struct bfd_link_hash_table *
1342 _bfd_elf_link_hash_table_create (abfd)
1343 bfd *abfd;
1345 struct elf_link_hash_table *ret;
1346 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1348 ret = (struct elf_link_hash_table *) bfd_alloc (abfd, amt);
1349 if (ret == (struct elf_link_hash_table *) NULL)
1350 return NULL;
1352 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1354 bfd_release (abfd, ret);
1355 return NULL;
1358 return &ret->root;
1361 /* This is a hook for the ELF emulation code in the generic linker to
1362 tell the backend linker what file name to use for the DT_NEEDED
1363 entry for a dynamic object. The generic linker passes name as an
1364 empty string to indicate that no DT_NEEDED entry should be made. */
1366 void
1367 bfd_elf_set_dt_needed_name (abfd, name)
1368 bfd *abfd;
1369 const char *name;
1371 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1372 && bfd_get_format (abfd) == bfd_object)
1373 elf_dt_name (abfd) = name;
1376 void
1377 bfd_elf_set_dt_needed_soname (abfd, name)
1378 bfd *abfd;
1379 const char *name;
1381 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1382 && bfd_get_format (abfd) == bfd_object)
1383 elf_dt_soname (abfd) = name;
1386 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1387 the linker ELF emulation code. */
1389 struct bfd_link_needed_list *
1390 bfd_elf_get_needed_list (abfd, info)
1391 bfd *abfd ATTRIBUTE_UNUSED;
1392 struct bfd_link_info *info;
1394 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1395 return NULL;
1396 return elf_hash_table (info)->needed;
1399 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1400 hook for the linker ELF emulation code. */
1402 struct bfd_link_needed_list *
1403 bfd_elf_get_runpath_list (abfd, info)
1404 bfd *abfd ATTRIBUTE_UNUSED;
1405 struct bfd_link_info *info;
1407 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1408 return NULL;
1409 return elf_hash_table (info)->runpath;
1412 /* Get the name actually used for a dynamic object for a link. This
1413 is the SONAME entry if there is one. Otherwise, it is the string
1414 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1416 const char *
1417 bfd_elf_get_dt_soname (abfd)
1418 bfd *abfd;
1420 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1421 && bfd_get_format (abfd) == bfd_object)
1422 return elf_dt_name (abfd);
1423 return NULL;
1426 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1427 the ELF linker emulation code. */
1429 boolean
1430 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1431 bfd *abfd;
1432 struct bfd_link_needed_list **pneeded;
1434 asection *s;
1435 bfd_byte *dynbuf = NULL;
1436 int elfsec;
1437 unsigned long shlink;
1438 bfd_byte *extdyn, *extdynend;
1439 size_t extdynsize;
1440 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1442 *pneeded = NULL;
1444 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1445 || bfd_get_format (abfd) != bfd_object)
1446 return true;
1448 s = bfd_get_section_by_name (abfd, ".dynamic");
1449 if (s == NULL || s->_raw_size == 0)
1450 return true;
1452 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1453 if (dynbuf == NULL)
1454 goto error_return;
1456 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1457 s->_raw_size))
1458 goto error_return;
1460 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1461 if (elfsec == -1)
1462 goto error_return;
1464 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1466 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1467 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1469 extdyn = dynbuf;
1470 extdynend = extdyn + s->_raw_size;
1471 for (; extdyn < extdynend; extdyn += extdynsize)
1473 Elf_Internal_Dyn dyn;
1475 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1477 if (dyn.d_tag == DT_NULL)
1478 break;
1480 if (dyn.d_tag == DT_NEEDED)
1482 const char *string;
1483 struct bfd_link_needed_list *l;
1484 unsigned int tagv = dyn.d_un.d_val;
1485 bfd_size_type amt;
1487 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1488 if (string == NULL)
1489 goto error_return;
1491 amt = sizeof *l;
1492 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1493 if (l == NULL)
1494 goto error_return;
1496 l->by = abfd;
1497 l->name = string;
1498 l->next = *pneeded;
1499 *pneeded = l;
1503 free (dynbuf);
1505 return true;
1507 error_return:
1508 if (dynbuf != NULL)
1509 free (dynbuf);
1510 return false;
1513 /* Allocate an ELF string table--force the first byte to be zero. */
1515 struct bfd_strtab_hash *
1516 _bfd_elf_stringtab_init ()
1518 struct bfd_strtab_hash *ret;
1520 ret = _bfd_stringtab_init ();
1521 if (ret != NULL)
1523 bfd_size_type loc;
1525 loc = _bfd_stringtab_add (ret, "", true, false);
1526 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1527 if (loc == (bfd_size_type) -1)
1529 _bfd_stringtab_free (ret);
1530 ret = NULL;
1533 return ret;
1536 /* ELF .o/exec file reading */
1538 /* Create a new bfd section from an ELF section header. */
1540 boolean
1541 bfd_section_from_shdr (abfd, shindex)
1542 bfd *abfd;
1543 unsigned int shindex;
1545 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1546 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1547 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1548 char *name;
1550 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1552 switch (hdr->sh_type)
1554 case SHT_NULL:
1555 /* Inactive section. Throw it away. */
1556 return true;
1558 case SHT_PROGBITS: /* Normal section with contents. */
1559 case SHT_DYNAMIC: /* Dynamic linking information. */
1560 case SHT_NOBITS: /* .bss section. */
1561 case SHT_HASH: /* .hash section. */
1562 case SHT_NOTE: /* .note section. */
1563 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1565 case SHT_SYMTAB: /* A symbol table */
1566 if (elf_onesymtab (abfd) == shindex)
1567 return true;
1569 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1570 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1571 elf_onesymtab (abfd) = shindex;
1572 elf_tdata (abfd)->symtab_hdr = *hdr;
1573 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1574 abfd->flags |= HAS_SYMS;
1576 /* Sometimes a shared object will map in the symbol table. If
1577 SHF_ALLOC is set, and this is a shared object, then we also
1578 treat this section as a BFD section. We can not base the
1579 decision purely on SHF_ALLOC, because that flag is sometimes
1580 set in a relocateable object file, which would confuse the
1581 linker. */
1582 if ((hdr->sh_flags & SHF_ALLOC) != 0
1583 && (abfd->flags & DYNAMIC) != 0
1584 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1585 return false;
1587 return true;
1589 case SHT_DYNSYM: /* A dynamic symbol table */
1590 if (elf_dynsymtab (abfd) == shindex)
1591 return true;
1593 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1594 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1595 elf_dynsymtab (abfd) = shindex;
1596 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1597 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1598 abfd->flags |= HAS_SYMS;
1600 /* Besides being a symbol table, we also treat this as a regular
1601 section, so that objcopy can handle it. */
1602 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1604 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1605 if (elf_symtab_shndx (abfd) == shindex)
1606 return true;
1608 /* Get the associated symbol table. */
1609 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1610 || hdr->sh_link != elf_onesymtab (abfd))
1611 return false;
1613 elf_symtab_shndx (abfd) = shindex;
1614 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1615 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1616 return true;
1618 case SHT_STRTAB: /* A string table */
1619 if (hdr->bfd_section != NULL)
1620 return true;
1621 if (ehdr->e_shstrndx == shindex)
1623 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1624 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1625 return true;
1628 unsigned int i, num_sec;
1630 num_sec = elf_numsections (abfd);
1631 for (i = 1; i < num_sec; i++)
1633 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1634 if (hdr2->sh_link == shindex)
1636 if (! bfd_section_from_shdr (abfd, i))
1637 return false;
1638 if (elf_onesymtab (abfd) == i)
1640 elf_tdata (abfd)->strtab_hdr = *hdr;
1641 elf_elfsections (abfd)[shindex] =
1642 &elf_tdata (abfd)->strtab_hdr;
1643 return true;
1645 if (elf_dynsymtab (abfd) == i)
1647 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1648 elf_elfsections (abfd)[shindex] = hdr =
1649 &elf_tdata (abfd)->dynstrtab_hdr;
1650 /* We also treat this as a regular section, so
1651 that objcopy can handle it. */
1652 break;
1654 #if 0 /* Not handling other string tables specially right now. */
1655 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1656 /* We have a strtab for some random other section. */
1657 newsect = (asection *) hdr2->bfd_section;
1658 if (!newsect)
1659 break;
1660 hdr->bfd_section = newsect;
1661 hdr2 = &elf_section_data (newsect)->str_hdr;
1662 *hdr2 = *hdr;
1663 elf_elfsections (abfd)[shindex] = hdr2;
1664 #endif
1669 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1671 case SHT_REL:
1672 case SHT_RELA:
1673 /* *These* do a lot of work -- but build no sections! */
1675 asection *target_sect;
1676 Elf_Internal_Shdr *hdr2;
1677 unsigned int num_sec = elf_numsections (abfd);
1679 /* Check for a bogus link to avoid crashing. */
1680 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1681 || hdr->sh_link >= num_sec)
1683 ((*_bfd_error_handler)
1684 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1685 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1686 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1689 /* For some incomprehensible reason Oracle distributes
1690 libraries for Solaris in which some of the objects have
1691 bogus sh_link fields. It would be nice if we could just
1692 reject them, but, unfortunately, some people need to use
1693 them. We scan through the section headers; if we find only
1694 one suitable symbol table, we clobber the sh_link to point
1695 to it. I hope this doesn't break anything. */
1696 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1697 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1699 unsigned int scan;
1700 int found;
1702 found = 0;
1703 for (scan = 1; scan < num_sec; scan++)
1705 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1706 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1708 if (found != 0)
1710 found = 0;
1711 break;
1713 found = scan;
1716 if (found != 0)
1717 hdr->sh_link = found;
1720 /* Get the symbol table. */
1721 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1722 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1723 return false;
1725 /* If this reloc section does not use the main symbol table we
1726 don't treat it as a reloc section. BFD can't adequately
1727 represent such a section, so at least for now, we don't
1728 try. We just present it as a normal section. We also
1729 can't use it as a reloc section if it points to the null
1730 section. */
1731 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1732 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1734 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1735 return false;
1736 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1737 if (target_sect == NULL)
1738 return false;
1740 if ((target_sect->flags & SEC_RELOC) == 0
1741 || target_sect->reloc_count == 0)
1742 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1743 else
1745 bfd_size_type amt;
1746 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1747 amt = sizeof (*hdr2);
1748 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1749 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1751 *hdr2 = *hdr;
1752 elf_elfsections (abfd)[shindex] = hdr2;
1753 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1754 target_sect->flags |= SEC_RELOC;
1755 target_sect->relocation = NULL;
1756 target_sect->rel_filepos = hdr->sh_offset;
1757 /* In the section to which the relocations apply, mark whether
1758 its relocations are of the REL or RELA variety. */
1759 if (hdr->sh_size != 0)
1760 elf_section_data (target_sect)->use_rela_p
1761 = (hdr->sh_type == SHT_RELA);
1762 abfd->flags |= HAS_RELOC;
1763 return true;
1765 break;
1767 case SHT_GNU_verdef:
1768 elf_dynverdef (abfd) = shindex;
1769 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1770 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1771 break;
1773 case SHT_GNU_versym:
1774 elf_dynversym (abfd) = shindex;
1775 elf_tdata (abfd)->dynversym_hdr = *hdr;
1776 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1777 break;
1779 case SHT_GNU_verneed:
1780 elf_dynverref (abfd) = shindex;
1781 elf_tdata (abfd)->dynverref_hdr = *hdr;
1782 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1783 break;
1785 case SHT_SHLIB:
1786 return true;
1788 case SHT_GROUP:
1789 /* Make a section for objcopy and relocatable links. */
1790 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1791 return false;
1792 if (hdr->contents != NULL)
1794 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1795 unsigned int n_elt = hdr->sh_size / 4;
1796 asection *s;
1798 while (--n_elt != 0)
1799 if ((s = (++idx)->shdr->bfd_section) != NULL
1800 && elf_next_in_group (s) != NULL)
1802 elf_next_in_group (hdr->bfd_section) = s;
1803 break;
1806 break;
1808 default:
1809 /* Check for any processor-specific section types. */
1811 if (bed->elf_backend_section_from_shdr)
1812 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1814 break;
1817 return true;
1820 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1821 Return SEC for sections that have no elf section, and NULL on error. */
1823 asection *
1824 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
1825 bfd *abfd;
1826 struct sym_sec_cache *cache;
1827 asection *sec;
1828 unsigned long r_symndx;
1830 unsigned char esym_shndx[4];
1831 unsigned int isym_shndx;
1832 Elf_Internal_Shdr *symtab_hdr;
1833 file_ptr pos;
1834 bfd_size_type amt;
1835 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1837 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1838 return cache->sec[ent];
1840 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1841 pos = symtab_hdr->sh_offset;
1842 if (get_elf_backend_data (abfd)->s->sizeof_sym
1843 == sizeof (Elf64_External_Sym))
1845 pos += r_symndx * sizeof (Elf64_External_Sym);
1846 pos += offsetof (Elf64_External_Sym, st_shndx);
1847 amt = sizeof (((Elf64_External_Sym *) 0)->st_shndx);
1849 else
1851 pos += r_symndx * sizeof (Elf32_External_Sym);
1852 pos += offsetof (Elf32_External_Sym, st_shndx);
1853 amt = sizeof (((Elf32_External_Sym *) 0)->st_shndx);
1855 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1856 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1857 return NULL;
1858 isym_shndx = H_GET_16 (abfd, esym_shndx);
1860 if (isym_shndx == SHN_XINDEX)
1862 Elf_Internal_Shdr *shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1863 if (shndx_hdr->sh_size != 0)
1865 pos = shndx_hdr->sh_offset;
1866 pos += r_symndx * sizeof (Elf_External_Sym_Shndx);
1867 amt = sizeof (Elf_External_Sym_Shndx);
1868 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1869 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1870 return NULL;
1871 isym_shndx = H_GET_32 (abfd, esym_shndx);
1875 if (cache->abfd != abfd)
1877 memset (cache->indx, -1, sizeof (cache->indx));
1878 cache->abfd = abfd;
1880 cache->indx[ent] = r_symndx;
1881 cache->sec[ent] = sec;
1882 if (isym_shndx < SHN_LORESERVE || isym_shndx > SHN_HIRESERVE)
1884 asection *s;
1885 s = bfd_section_from_elf_index (abfd, isym_shndx);
1886 if (s != NULL)
1887 cache->sec[ent] = s;
1889 return cache->sec[ent];
1892 /* Given an ELF section number, retrieve the corresponding BFD
1893 section. */
1895 asection *
1896 bfd_section_from_elf_index (abfd, index)
1897 bfd *abfd;
1898 unsigned int index;
1900 if (index >= elf_numsections (abfd))
1901 return NULL;
1902 return elf_elfsections (abfd)[index]->bfd_section;
1905 boolean
1906 _bfd_elf_new_section_hook (abfd, sec)
1907 bfd *abfd;
1908 asection *sec;
1910 struct bfd_elf_section_data *sdata;
1911 bfd_size_type amt = sizeof (*sdata);
1913 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
1914 if (!sdata)
1915 return false;
1916 sec->used_by_bfd = (PTR) sdata;
1918 /* Indicate whether or not this section should use RELA relocations. */
1919 sdata->use_rela_p
1920 = get_elf_backend_data (abfd)->default_use_rela_p;
1922 return true;
1925 /* Create a new bfd section from an ELF program header.
1927 Since program segments have no names, we generate a synthetic name
1928 of the form segment<NUM>, where NUM is generally the index in the
1929 program header table. For segments that are split (see below) we
1930 generate the names segment<NUM>a and segment<NUM>b.
1932 Note that some program segments may have a file size that is different than
1933 (less than) the memory size. All this means is that at execution the
1934 system must allocate the amount of memory specified by the memory size,
1935 but only initialize it with the first "file size" bytes read from the
1936 file. This would occur for example, with program segments consisting
1937 of combined data+bss.
1939 To handle the above situation, this routine generates TWO bfd sections
1940 for the single program segment. The first has the length specified by
1941 the file size of the segment, and the second has the length specified
1942 by the difference between the two sizes. In effect, the segment is split
1943 into it's initialized and uninitialized parts.
1947 boolean
1948 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1949 bfd *abfd;
1950 Elf_Internal_Phdr *hdr;
1951 int index;
1952 const char *typename;
1954 asection *newsect;
1955 char *name;
1956 char namebuf[64];
1957 int split;
1959 split = ((hdr->p_memsz > 0)
1960 && (hdr->p_filesz > 0)
1961 && (hdr->p_memsz > hdr->p_filesz));
1962 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1963 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1964 if (!name)
1965 return false;
1966 strcpy (name, namebuf);
1967 newsect = bfd_make_section (abfd, name);
1968 if (newsect == NULL)
1969 return false;
1970 newsect->vma = hdr->p_vaddr;
1971 newsect->lma = hdr->p_paddr;
1972 newsect->_raw_size = hdr->p_filesz;
1973 newsect->filepos = hdr->p_offset;
1974 newsect->flags |= SEC_HAS_CONTENTS;
1975 if (hdr->p_type == PT_LOAD)
1977 newsect->flags |= SEC_ALLOC;
1978 newsect->flags |= SEC_LOAD;
1979 if (hdr->p_flags & PF_X)
1981 /* FIXME: all we known is that it has execute PERMISSION,
1982 may be data. */
1983 newsect->flags |= SEC_CODE;
1986 if (!(hdr->p_flags & PF_W))
1988 newsect->flags |= SEC_READONLY;
1991 if (split)
1993 sprintf (namebuf, "%s%db", typename, index);
1994 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1995 if (!name)
1996 return false;
1997 strcpy (name, namebuf);
1998 newsect = bfd_make_section (abfd, name);
1999 if (newsect == NULL)
2000 return false;
2001 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
2002 newsect->lma = hdr->p_paddr + hdr->p_filesz;
2003 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
2004 if (hdr->p_type == PT_LOAD)
2006 newsect->flags |= SEC_ALLOC;
2007 if (hdr->p_flags & PF_X)
2008 newsect->flags |= SEC_CODE;
2010 if (!(hdr->p_flags & PF_W))
2011 newsect->flags |= SEC_READONLY;
2014 return true;
2017 boolean
2018 bfd_section_from_phdr (abfd, hdr, index)
2019 bfd *abfd;
2020 Elf_Internal_Phdr *hdr;
2021 int index;
2023 struct elf_backend_data *bed;
2025 switch (hdr->p_type)
2027 case PT_NULL:
2028 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2030 case PT_LOAD:
2031 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2033 case PT_DYNAMIC:
2034 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2036 case PT_INTERP:
2037 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2039 case PT_NOTE:
2040 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2041 return false;
2042 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2043 return false;
2044 return true;
2046 case PT_SHLIB:
2047 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2049 case PT_PHDR:
2050 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2052 default:
2053 /* Check for any processor-specific program segment types.
2054 If no handler for them, default to making "segment" sections. */
2055 bed = get_elf_backend_data (abfd);
2056 if (bed->elf_backend_section_from_phdr)
2057 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2058 else
2059 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2063 /* Initialize REL_HDR, the section-header for new section, containing
2064 relocations against ASECT. If USE_RELA_P is true, we use RELA
2065 relocations; otherwise, we use REL relocations. */
2067 boolean
2068 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2069 bfd *abfd;
2070 Elf_Internal_Shdr *rel_hdr;
2071 asection *asect;
2072 boolean use_rela_p;
2074 char *name;
2075 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2076 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2078 name = bfd_alloc (abfd, amt);
2079 if (name == NULL)
2080 return false;
2081 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2082 rel_hdr->sh_name =
2083 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2084 false);
2085 if (rel_hdr->sh_name == (unsigned int) -1)
2086 return false;
2087 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2088 rel_hdr->sh_entsize = (use_rela_p
2089 ? bed->s->sizeof_rela
2090 : bed->s->sizeof_rel);
2091 rel_hdr->sh_addralign = bed->s->file_align;
2092 rel_hdr->sh_flags = 0;
2093 rel_hdr->sh_addr = 0;
2094 rel_hdr->sh_size = 0;
2095 rel_hdr->sh_offset = 0;
2097 return true;
2100 /* Set up an ELF internal section header for a section. */
2102 static void
2103 elf_fake_sections (abfd, asect, failedptrarg)
2104 bfd *abfd;
2105 asection *asect;
2106 PTR failedptrarg;
2108 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2109 boolean *failedptr = (boolean *) failedptrarg;
2110 Elf_Internal_Shdr *this_hdr;
2112 if (*failedptr)
2114 /* We already failed; just get out of the bfd_map_over_sections
2115 loop. */
2116 return;
2119 this_hdr = &elf_section_data (asect)->this_hdr;
2121 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2122 asect->name, false);
2123 if (this_hdr->sh_name == (unsigned long) -1)
2125 *failedptr = true;
2126 return;
2129 this_hdr->sh_flags = 0;
2131 if ((asect->flags & SEC_ALLOC) != 0
2132 || asect->user_set_vma)
2133 this_hdr->sh_addr = asect->vma;
2134 else
2135 this_hdr->sh_addr = 0;
2137 this_hdr->sh_offset = 0;
2138 this_hdr->sh_size = asect->_raw_size;
2139 this_hdr->sh_link = 0;
2140 this_hdr->sh_addralign = 1 << asect->alignment_power;
2141 /* The sh_entsize and sh_info fields may have been set already by
2142 copy_private_section_data. */
2144 this_hdr->bfd_section = asect;
2145 this_hdr->contents = NULL;
2147 /* FIXME: This should not be based on section names. */
2148 if (strcmp (asect->name, ".dynstr") == 0)
2149 this_hdr->sh_type = SHT_STRTAB;
2150 else if (strcmp (asect->name, ".hash") == 0)
2152 this_hdr->sh_type = SHT_HASH;
2153 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2155 else if (strcmp (asect->name, ".dynsym") == 0)
2157 this_hdr->sh_type = SHT_DYNSYM;
2158 this_hdr->sh_entsize = bed->s->sizeof_sym;
2160 else if (strcmp (asect->name, ".dynamic") == 0)
2162 this_hdr->sh_type = SHT_DYNAMIC;
2163 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2165 else if (strncmp (asect->name, ".rela", 5) == 0
2166 && get_elf_backend_data (abfd)->may_use_rela_p)
2168 this_hdr->sh_type = SHT_RELA;
2169 this_hdr->sh_entsize = bed->s->sizeof_rela;
2171 else if (strncmp (asect->name, ".rel", 4) == 0
2172 && get_elf_backend_data (abfd)->may_use_rel_p)
2174 this_hdr->sh_type = SHT_REL;
2175 this_hdr->sh_entsize = bed->s->sizeof_rel;
2177 else if (strncmp (asect->name, ".note", 5) == 0)
2178 this_hdr->sh_type = SHT_NOTE;
2179 else if (strncmp (asect->name, ".stab", 5) == 0
2180 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2181 this_hdr->sh_type = SHT_STRTAB;
2182 else if (strcmp (asect->name, ".gnu.version") == 0)
2184 this_hdr->sh_type = SHT_GNU_versym;
2185 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2187 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2189 this_hdr->sh_type = SHT_GNU_verdef;
2190 this_hdr->sh_entsize = 0;
2191 /* objcopy or strip will copy over sh_info, but may not set
2192 cverdefs. The linker will set cverdefs, but sh_info will be
2193 zero. */
2194 if (this_hdr->sh_info == 0)
2195 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2196 else
2197 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2198 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2200 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2202 this_hdr->sh_type = SHT_GNU_verneed;
2203 this_hdr->sh_entsize = 0;
2204 /* objcopy or strip will copy over sh_info, but may not set
2205 cverrefs. The linker will set cverrefs, but sh_info will be
2206 zero. */
2207 if (this_hdr->sh_info == 0)
2208 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2209 else
2210 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2211 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2213 else if ((asect->flags & SEC_GROUP) != 0)
2215 this_hdr->sh_type = SHT_GROUP;
2216 this_hdr->sh_entsize = 4;
2218 else if ((asect->flags & SEC_ALLOC) != 0
2219 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
2220 this_hdr->sh_type = SHT_NOBITS;
2221 else
2222 this_hdr->sh_type = SHT_PROGBITS;
2224 if ((asect->flags & SEC_ALLOC) != 0)
2225 this_hdr->sh_flags |= SHF_ALLOC;
2226 if ((asect->flags & SEC_READONLY) == 0)
2227 this_hdr->sh_flags |= SHF_WRITE;
2228 if ((asect->flags & SEC_CODE) != 0)
2229 this_hdr->sh_flags |= SHF_EXECINSTR;
2230 if ((asect->flags & SEC_MERGE) != 0)
2232 this_hdr->sh_flags |= SHF_MERGE;
2233 this_hdr->sh_entsize = asect->entsize;
2234 if ((asect->flags & SEC_STRINGS) != 0)
2235 this_hdr->sh_flags |= SHF_STRINGS;
2237 if (elf_group_name (asect) != NULL)
2238 this_hdr->sh_flags |= SHF_GROUP;
2240 /* Check for processor-specific section types. */
2241 if (bed->elf_backend_fake_sections
2242 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2243 *failedptr = true;
2245 /* If the section has relocs, set up a section header for the
2246 SHT_REL[A] section. If two relocation sections are required for
2247 this section, it is up to the processor-specific back-end to
2248 create the other. */
2249 if ((asect->flags & SEC_RELOC) != 0
2250 && !_bfd_elf_init_reloc_shdr (abfd,
2251 &elf_section_data (asect)->rel_hdr,
2252 asect,
2253 elf_section_data (asect)->use_rela_p))
2254 *failedptr = true;
2257 /* Fill in the contents of a SHT_GROUP section. */
2259 static void
2260 set_group_contents (abfd, sec, failedptrarg)
2261 bfd *abfd;
2262 asection *sec;
2263 PTR failedptrarg ATTRIBUTE_UNUSED;
2265 boolean *failedptr = (boolean *) failedptrarg;
2266 unsigned long symindx;
2267 asection *elt;
2268 unsigned char *loc;
2269 struct bfd_link_order *l;
2271 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2272 || *failedptr)
2273 return;
2275 /* If called from the assembler, swap_out_syms will have set up
2276 elf_section_syms; If called for "ld -r", the symbols won't yet
2277 be mapped, so emulate elf_bfd_final_link. */
2278 if (elf_section_syms (abfd) != NULL)
2279 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2280 else
2281 symindx = elf_section_data (sec)->this_idx;
2282 elf_section_data (sec)->this_hdr.sh_info = symindx;
2284 /* Nor will the contents be allocated for "ld -r". */
2285 if (sec->contents == NULL)
2287 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2288 if (sec->contents == NULL)
2290 *failedptr = true;
2291 return;
2295 loc = sec->contents + sec->_raw_size;
2297 /* Get the pointer to the first section in the group that we
2298 squirreled away here. */
2299 elt = elf_next_in_group (sec);
2301 /* First element is a flag word. Rest of section is elf section
2302 indices for all the sections of the group. Write them backwards
2303 just to keep the group in the same order as given in .section
2304 directives, not that it matters. */
2305 while (elt != NULL)
2307 loc -= 4;
2308 H_PUT_32 (abfd, elf_section_data (elt)->this_idx, loc);
2309 elt = elf_next_in_group (elt);
2312 /* If this is a relocatable link, then the above did nothing because
2313 SEC is the output section. Look through the input sections
2314 instead. */
2315 for (l = sec->link_order_head; l != NULL; l = l->next)
2316 if (l->type == bfd_indirect_link_order
2317 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2320 loc -= 4;
2321 H_PUT_32 (abfd,
2322 elf_section_data (elt->output_section)->this_idx, loc);
2323 elt = elf_next_in_group (elt);
2324 /* During a relocatable link, the lists are circular. */
2326 while (elt != elf_next_in_group (l->u.indirect.section));
2328 loc -= 4;
2329 H_PUT_32 (abfd, 0, loc);
2331 BFD_ASSERT (loc == sec->contents);
2334 /* Assign all ELF section numbers. The dummy first section is handled here
2335 too. The link/info pointers for the standard section types are filled
2336 in here too, while we're at it. */
2338 static boolean
2339 assign_section_numbers (abfd)
2340 bfd *abfd;
2342 struct elf_obj_tdata *t = elf_tdata (abfd);
2343 asection *sec;
2344 unsigned int section_number, secn;
2345 Elf_Internal_Shdr **i_shdrp;
2346 bfd_size_type amt;
2348 section_number = 1;
2350 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2352 for (sec = abfd->sections; sec; sec = sec->next)
2354 struct bfd_elf_section_data *d = elf_section_data (sec);
2356 if (section_number == SHN_LORESERVE)
2357 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2358 d->this_idx = section_number++;
2359 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2360 if ((sec->flags & SEC_RELOC) == 0)
2361 d->rel_idx = 0;
2362 else
2364 if (section_number == SHN_LORESERVE)
2365 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2366 d->rel_idx = section_number++;
2367 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2370 if (d->rel_hdr2)
2372 if (section_number == SHN_LORESERVE)
2373 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2374 d->rel_idx2 = section_number++;
2375 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2377 else
2378 d->rel_idx2 = 0;
2381 if (section_number == SHN_LORESERVE)
2382 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2383 t->shstrtab_section = section_number++;
2384 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2385 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2387 if (bfd_get_symcount (abfd) > 0)
2389 if (section_number == SHN_LORESERVE)
2390 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2391 t->symtab_section = section_number++;
2392 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2393 if (section_number > SHN_LORESERVE - 2)
2395 if (section_number == SHN_LORESERVE)
2396 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2397 t->symtab_shndx_section = section_number++;
2398 t->symtab_shndx_hdr.sh_name
2399 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2400 ".symtab_shndx", false);
2401 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2402 return false;
2404 if (section_number == SHN_LORESERVE)
2405 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2406 t->strtab_section = section_number++;
2407 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2410 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2411 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2413 elf_numsections (abfd) = section_number;
2414 elf_elfheader (abfd)->e_shnum = section_number;
2415 if (section_number > SHN_LORESERVE)
2416 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2418 /* Set up the list of section header pointers, in agreement with the
2419 indices. */
2420 amt = section_number * sizeof (Elf_Internal_Shdr *);
2421 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
2422 if (i_shdrp == NULL)
2423 return false;
2425 amt = sizeof (Elf_Internal_Shdr);
2426 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2427 if (i_shdrp[0] == NULL)
2429 bfd_release (abfd, i_shdrp);
2430 return false;
2432 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2434 elf_elfsections (abfd) = i_shdrp;
2436 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2437 if (bfd_get_symcount (abfd) > 0)
2439 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2440 if (elf_numsections (abfd) > SHN_LORESERVE)
2442 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2443 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2445 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2446 t->symtab_hdr.sh_link = t->strtab_section;
2448 for (sec = abfd->sections; sec; sec = sec->next)
2450 struct bfd_elf_section_data *d = elf_section_data (sec);
2451 asection *s;
2452 const char *name;
2454 i_shdrp[d->this_idx] = &d->this_hdr;
2455 if (d->rel_idx != 0)
2456 i_shdrp[d->rel_idx] = &d->rel_hdr;
2457 if (d->rel_idx2 != 0)
2458 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2460 /* Fill in the sh_link and sh_info fields while we're at it. */
2462 /* sh_link of a reloc section is the section index of the symbol
2463 table. sh_info is the section index of the section to which
2464 the relocation entries apply. */
2465 if (d->rel_idx != 0)
2467 d->rel_hdr.sh_link = t->symtab_section;
2468 d->rel_hdr.sh_info = d->this_idx;
2470 if (d->rel_idx2 != 0)
2472 d->rel_hdr2->sh_link = t->symtab_section;
2473 d->rel_hdr2->sh_info = d->this_idx;
2476 switch (d->this_hdr.sh_type)
2478 case SHT_REL:
2479 case SHT_RELA:
2480 /* A reloc section which we are treating as a normal BFD
2481 section. sh_link is the section index of the symbol
2482 table. sh_info is the section index of the section to
2483 which the relocation entries apply. We assume that an
2484 allocated reloc section uses the dynamic symbol table.
2485 FIXME: How can we be sure? */
2486 s = bfd_get_section_by_name (abfd, ".dynsym");
2487 if (s != NULL)
2488 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2490 /* We look up the section the relocs apply to by name. */
2491 name = sec->name;
2492 if (d->this_hdr.sh_type == SHT_REL)
2493 name += 4;
2494 else
2495 name += 5;
2496 s = bfd_get_section_by_name (abfd, name);
2497 if (s != NULL)
2498 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2499 break;
2501 case SHT_STRTAB:
2502 /* We assume that a section named .stab*str is a stabs
2503 string section. We look for a section with the same name
2504 but without the trailing ``str'', and set its sh_link
2505 field to point to this section. */
2506 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2507 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2509 size_t len;
2510 char *alc;
2512 len = strlen (sec->name);
2513 alc = (char *) bfd_malloc ((bfd_size_type) len - 2);
2514 if (alc == NULL)
2515 return false;
2516 strncpy (alc, sec->name, len - 3);
2517 alc[len - 3] = '\0';
2518 s = bfd_get_section_by_name (abfd, alc);
2519 free (alc);
2520 if (s != NULL)
2522 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2524 /* This is a .stab section. */
2525 elf_section_data (s)->this_hdr.sh_entsize =
2526 4 + 2 * bfd_get_arch_size (abfd) / 8;
2529 break;
2531 case SHT_DYNAMIC:
2532 case SHT_DYNSYM:
2533 case SHT_GNU_verneed:
2534 case SHT_GNU_verdef:
2535 /* sh_link is the section header index of the string table
2536 used for the dynamic entries, or the symbol table, or the
2537 version strings. */
2538 s = bfd_get_section_by_name (abfd, ".dynstr");
2539 if (s != NULL)
2540 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2541 break;
2543 case SHT_HASH:
2544 case SHT_GNU_versym:
2545 /* sh_link is the section header index of the symbol table
2546 this hash table or version table is for. */
2547 s = bfd_get_section_by_name (abfd, ".dynsym");
2548 if (s != NULL)
2549 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2550 break;
2552 case SHT_GROUP:
2553 d->this_hdr.sh_link = t->symtab_section;
2557 for (secn = 1; secn < section_number; ++secn)
2558 if (i_shdrp[secn] == NULL)
2559 i_shdrp[secn] = i_shdrp[0];
2560 else
2561 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2562 i_shdrp[secn]->sh_name);
2563 return true;
2566 /* Map symbol from it's internal number to the external number, moving
2567 all local symbols to be at the head of the list. */
2569 static INLINE int
2570 sym_is_global (abfd, sym)
2571 bfd *abfd;
2572 asymbol *sym;
2574 /* If the backend has a special mapping, use it. */
2575 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2576 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2577 (abfd, sym));
2579 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2580 || bfd_is_und_section (bfd_get_section (sym))
2581 || bfd_is_com_section (bfd_get_section (sym)));
2584 static boolean
2585 elf_map_symbols (abfd)
2586 bfd *abfd;
2588 unsigned int symcount = bfd_get_symcount (abfd);
2589 asymbol **syms = bfd_get_outsymbols (abfd);
2590 asymbol **sect_syms;
2591 unsigned int num_locals = 0;
2592 unsigned int num_globals = 0;
2593 unsigned int num_locals2 = 0;
2594 unsigned int num_globals2 = 0;
2595 int max_index = 0;
2596 unsigned int idx;
2597 asection *asect;
2598 asymbol **new_syms;
2599 bfd_size_type amt;
2601 #ifdef DEBUG
2602 fprintf (stderr, "elf_map_symbols\n");
2603 fflush (stderr);
2604 #endif
2606 for (asect = abfd->sections; asect; asect = asect->next)
2608 if (max_index < asect->index)
2609 max_index = asect->index;
2612 max_index++;
2613 amt = max_index * sizeof (asymbol *);
2614 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2615 if (sect_syms == NULL)
2616 return false;
2617 elf_section_syms (abfd) = sect_syms;
2618 elf_num_section_syms (abfd) = max_index;
2620 /* Init sect_syms entries for any section symbols we have already
2621 decided to output. */
2622 for (idx = 0; idx < symcount; idx++)
2624 asymbol *sym = syms[idx];
2626 if ((sym->flags & BSF_SECTION_SYM) != 0
2627 && sym->value == 0)
2629 asection *sec;
2631 sec = sym->section;
2633 if (sec->owner != NULL)
2635 if (sec->owner != abfd)
2637 if (sec->output_offset != 0)
2638 continue;
2640 sec = sec->output_section;
2642 /* Empty sections in the input files may have had a
2643 section symbol created for them. (See the comment
2644 near the end of _bfd_generic_link_output_symbols in
2645 linker.c). If the linker script discards such
2646 sections then we will reach this point. Since we know
2647 that we cannot avoid this case, we detect it and skip
2648 the abort and the assignment to the sect_syms array.
2649 To reproduce this particular case try running the
2650 linker testsuite test ld-scripts/weak.exp for an ELF
2651 port that uses the generic linker. */
2652 if (sec->owner == NULL)
2653 continue;
2655 BFD_ASSERT (sec->owner == abfd);
2657 sect_syms[sec->index] = syms[idx];
2662 /* Classify all of the symbols. */
2663 for (idx = 0; idx < symcount; idx++)
2665 if (!sym_is_global (abfd, syms[idx]))
2666 num_locals++;
2667 else
2668 num_globals++;
2671 /* We will be adding a section symbol for each BFD section. Most normal
2672 sections will already have a section symbol in outsymbols, but
2673 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2674 at least in that case. */
2675 for (asect = abfd->sections; asect; asect = asect->next)
2677 if (sect_syms[asect->index] == NULL)
2679 if (!sym_is_global (abfd, asect->symbol))
2680 num_locals++;
2681 else
2682 num_globals++;
2686 /* Now sort the symbols so the local symbols are first. */
2687 amt = (num_locals + num_globals) * sizeof (asymbol *);
2688 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2690 if (new_syms == NULL)
2691 return false;
2693 for (idx = 0; idx < symcount; idx++)
2695 asymbol *sym = syms[idx];
2696 unsigned int i;
2698 if (!sym_is_global (abfd, sym))
2699 i = num_locals2++;
2700 else
2701 i = num_locals + num_globals2++;
2702 new_syms[i] = sym;
2703 sym->udata.i = i + 1;
2705 for (asect = abfd->sections; asect; asect = asect->next)
2707 if (sect_syms[asect->index] == NULL)
2709 asymbol *sym = asect->symbol;
2710 unsigned int i;
2712 sect_syms[asect->index] = sym;
2713 if (!sym_is_global (abfd, sym))
2714 i = num_locals2++;
2715 else
2716 i = num_locals + num_globals2++;
2717 new_syms[i] = sym;
2718 sym->udata.i = i + 1;
2722 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2724 elf_num_locals (abfd) = num_locals;
2725 elf_num_globals (abfd) = num_globals;
2726 return true;
2729 /* Align to the maximum file alignment that could be required for any
2730 ELF data structure. */
2732 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2733 static INLINE file_ptr
2734 align_file_position (off, align)
2735 file_ptr off;
2736 int align;
2738 return (off + align - 1) & ~(align - 1);
2741 /* Assign a file position to a section, optionally aligning to the
2742 required section alignment. */
2744 INLINE file_ptr
2745 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2746 Elf_Internal_Shdr *i_shdrp;
2747 file_ptr offset;
2748 boolean align;
2750 if (align)
2752 unsigned int al;
2754 al = i_shdrp->sh_addralign;
2755 if (al > 1)
2756 offset = BFD_ALIGN (offset, al);
2758 i_shdrp->sh_offset = offset;
2759 if (i_shdrp->bfd_section != NULL)
2760 i_shdrp->bfd_section->filepos = offset;
2761 if (i_shdrp->sh_type != SHT_NOBITS)
2762 offset += i_shdrp->sh_size;
2763 return offset;
2766 /* Compute the file positions we are going to put the sections at, and
2767 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2768 is not NULL, this is being called by the ELF backend linker. */
2770 boolean
2771 _bfd_elf_compute_section_file_positions (abfd, link_info)
2772 bfd *abfd;
2773 struct bfd_link_info *link_info;
2775 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2776 boolean failed;
2777 struct bfd_strtab_hash *strtab;
2778 Elf_Internal_Shdr *shstrtab_hdr;
2780 if (abfd->output_has_begun)
2781 return true;
2783 /* Do any elf backend specific processing first. */
2784 if (bed->elf_backend_begin_write_processing)
2785 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2787 if (! prep_headers (abfd))
2788 return false;
2790 /* Post process the headers if necessary. */
2791 if (bed->elf_backend_post_process_headers)
2792 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2794 failed = false;
2795 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2796 if (failed)
2797 return false;
2799 if (!assign_section_numbers (abfd))
2800 return false;
2802 /* The backend linker builds symbol table information itself. */
2803 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2805 /* Non-zero if doing a relocatable link. */
2806 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2808 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2809 return false;
2812 if (link_info == NULL || link_info->relocateable)
2814 bfd_map_over_sections (abfd, set_group_contents, &failed);
2815 if (failed)
2816 return false;
2819 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2820 /* sh_name was set in prep_headers. */
2821 shstrtab_hdr->sh_type = SHT_STRTAB;
2822 shstrtab_hdr->sh_flags = 0;
2823 shstrtab_hdr->sh_addr = 0;
2824 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2825 shstrtab_hdr->sh_entsize = 0;
2826 shstrtab_hdr->sh_link = 0;
2827 shstrtab_hdr->sh_info = 0;
2828 /* sh_offset is set in assign_file_positions_except_relocs. */
2829 shstrtab_hdr->sh_addralign = 1;
2831 if (!assign_file_positions_except_relocs (abfd))
2832 return false;
2834 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2836 file_ptr off;
2837 Elf_Internal_Shdr *hdr;
2839 off = elf_tdata (abfd)->next_file_pos;
2841 hdr = &elf_tdata (abfd)->symtab_hdr;
2842 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2844 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2845 if (hdr->sh_size != 0)
2846 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2848 hdr = &elf_tdata (abfd)->strtab_hdr;
2849 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2851 elf_tdata (abfd)->next_file_pos = off;
2853 /* Now that we know where the .strtab section goes, write it
2854 out. */
2855 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2856 || ! _bfd_stringtab_emit (abfd, strtab))
2857 return false;
2858 _bfd_stringtab_free (strtab);
2861 abfd->output_has_begun = true;
2863 return true;
2866 /* Create a mapping from a set of sections to a program segment. */
2868 static INLINE struct elf_segment_map *
2869 make_mapping (abfd, sections, from, to, phdr)
2870 bfd *abfd;
2871 asection **sections;
2872 unsigned int from;
2873 unsigned int to;
2874 boolean phdr;
2876 struct elf_segment_map *m;
2877 unsigned int i;
2878 asection **hdrpp;
2879 bfd_size_type amt;
2881 amt = sizeof (struct elf_segment_map);
2882 amt += (to - from - 1) * sizeof (asection *);
2883 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2884 if (m == NULL)
2885 return NULL;
2886 m->next = NULL;
2887 m->p_type = PT_LOAD;
2888 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2889 m->sections[i - from] = *hdrpp;
2890 m->count = to - from;
2892 if (from == 0 && phdr)
2894 /* Include the headers in the first PT_LOAD segment. */
2895 m->includes_filehdr = 1;
2896 m->includes_phdrs = 1;
2899 return m;
2902 /* Set up a mapping from BFD sections to program segments. */
2904 static boolean
2905 map_sections_to_segments (abfd)
2906 bfd *abfd;
2908 asection **sections = NULL;
2909 asection *s;
2910 unsigned int i;
2911 unsigned int count;
2912 struct elf_segment_map *mfirst;
2913 struct elf_segment_map **pm;
2914 struct elf_segment_map *m;
2915 asection *last_hdr;
2916 unsigned int phdr_index;
2917 bfd_vma maxpagesize;
2918 asection **hdrpp;
2919 boolean phdr_in_segment = true;
2920 boolean writable;
2921 asection *dynsec, *eh_frame_hdr;
2922 bfd_size_type amt;
2924 if (elf_tdata (abfd)->segment_map != NULL)
2925 return true;
2927 if (bfd_count_sections (abfd) == 0)
2928 return true;
2930 /* Select the allocated sections, and sort them. */
2932 amt = bfd_count_sections (abfd) * sizeof (asection *);
2933 sections = (asection **) bfd_malloc (amt);
2934 if (sections == NULL)
2935 goto error_return;
2937 i = 0;
2938 for (s = abfd->sections; s != NULL; s = s->next)
2940 if ((s->flags & SEC_ALLOC) != 0)
2942 sections[i] = s;
2943 ++i;
2946 BFD_ASSERT (i <= bfd_count_sections (abfd));
2947 count = i;
2949 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2951 /* Build the mapping. */
2953 mfirst = NULL;
2954 pm = &mfirst;
2956 /* If we have a .interp section, then create a PT_PHDR segment for
2957 the program headers and a PT_INTERP segment for the .interp
2958 section. */
2959 s = bfd_get_section_by_name (abfd, ".interp");
2960 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2962 amt = sizeof (struct elf_segment_map);
2963 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2964 if (m == NULL)
2965 goto error_return;
2966 m->next = NULL;
2967 m->p_type = PT_PHDR;
2968 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2969 m->p_flags = PF_R | PF_X;
2970 m->p_flags_valid = 1;
2971 m->includes_phdrs = 1;
2973 *pm = m;
2974 pm = &m->next;
2976 amt = sizeof (struct elf_segment_map);
2977 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2978 if (m == NULL)
2979 goto error_return;
2980 m->next = NULL;
2981 m->p_type = PT_INTERP;
2982 m->count = 1;
2983 m->sections[0] = s;
2985 *pm = m;
2986 pm = &m->next;
2989 /* Look through the sections. We put sections in the same program
2990 segment when the start of the second section can be placed within
2991 a few bytes of the end of the first section. */
2992 last_hdr = NULL;
2993 phdr_index = 0;
2994 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2995 writable = false;
2996 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2997 if (dynsec != NULL
2998 && (dynsec->flags & SEC_LOAD) == 0)
2999 dynsec = NULL;
3001 /* Deal with -Ttext or something similar such that the first section
3002 is not adjacent to the program headers. This is an
3003 approximation, since at this point we don't know exactly how many
3004 program headers we will need. */
3005 if (count > 0)
3007 bfd_size_type phdr_size;
3009 phdr_size = elf_tdata (abfd)->program_header_size;
3010 if (phdr_size == 0)
3011 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3012 if ((abfd->flags & D_PAGED) == 0
3013 || sections[0]->lma < phdr_size
3014 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3015 phdr_in_segment = false;
3018 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3020 asection *hdr;
3021 boolean new_segment;
3023 hdr = *hdrpp;
3025 /* See if this section and the last one will fit in the same
3026 segment. */
3028 if (last_hdr == NULL)
3030 /* If we don't have a segment yet, then we don't need a new
3031 one (we build the last one after this loop). */
3032 new_segment = false;
3034 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3036 /* If this section has a different relation between the
3037 virtual address and the load address, then we need a new
3038 segment. */
3039 new_segment = true;
3041 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3042 < BFD_ALIGN (hdr->lma, maxpagesize))
3044 /* If putting this section in this segment would force us to
3045 skip a page in the segment, then we need a new segment. */
3046 new_segment = true;
3048 else if ((last_hdr->flags & SEC_LOAD) == 0
3049 && (hdr->flags & SEC_LOAD) != 0)
3051 /* We don't want to put a loadable section after a
3052 nonloadable section in the same segment. */
3053 new_segment = true;
3055 else if ((abfd->flags & D_PAGED) == 0)
3057 /* If the file is not demand paged, which means that we
3058 don't require the sections to be correctly aligned in the
3059 file, then there is no other reason for a new segment. */
3060 new_segment = false;
3062 else if (! writable
3063 && (hdr->flags & SEC_READONLY) == 0
3064 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3065 == hdr->lma))
3067 /* We don't want to put a writable section in a read only
3068 segment, unless they are on the same page in memory
3069 anyhow. We already know that the last section does not
3070 bring us past the current section on the page, so the
3071 only case in which the new section is not on the same
3072 page as the previous section is when the previous section
3073 ends precisely on a page boundary. */
3074 new_segment = true;
3076 else
3078 /* Otherwise, we can use the same segment. */
3079 new_segment = false;
3082 if (! new_segment)
3084 if ((hdr->flags & SEC_READONLY) == 0)
3085 writable = true;
3086 last_hdr = hdr;
3087 continue;
3090 /* We need a new program segment. We must create a new program
3091 header holding all the sections from phdr_index until hdr. */
3093 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3094 if (m == NULL)
3095 goto error_return;
3097 *pm = m;
3098 pm = &m->next;
3100 if ((hdr->flags & SEC_READONLY) == 0)
3101 writable = true;
3102 else
3103 writable = false;
3105 last_hdr = hdr;
3106 phdr_index = i;
3107 phdr_in_segment = false;
3110 /* Create a final PT_LOAD program segment. */
3111 if (last_hdr != NULL)
3113 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3114 if (m == NULL)
3115 goto error_return;
3117 *pm = m;
3118 pm = &m->next;
3121 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3122 if (dynsec != NULL)
3124 amt = sizeof (struct elf_segment_map);
3125 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3126 if (m == NULL)
3127 goto error_return;
3128 m->next = NULL;
3129 m->p_type = PT_DYNAMIC;
3130 m->count = 1;
3131 m->sections[0] = dynsec;
3133 *pm = m;
3134 pm = &m->next;
3137 /* For each loadable .note section, add a PT_NOTE segment. We don't
3138 use bfd_get_section_by_name, because if we link together
3139 nonloadable .note sections and loadable .note sections, we will
3140 generate two .note sections in the output file. FIXME: Using
3141 names for section types is bogus anyhow. */
3142 for (s = abfd->sections; s != NULL; s = s->next)
3144 if ((s->flags & SEC_LOAD) != 0
3145 && strncmp (s->name, ".note", 5) == 0)
3147 amt = sizeof (struct elf_segment_map);
3148 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3149 if (m == NULL)
3150 goto error_return;
3151 m->next = NULL;
3152 m->p_type = PT_NOTE;
3153 m->count = 1;
3154 m->sections[0] = s;
3156 *pm = m;
3157 pm = &m->next;
3161 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3162 segment. */
3163 eh_frame_hdr = NULL;
3164 if (elf_tdata (abfd)->eh_frame_hdr)
3165 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
3166 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3168 amt = sizeof (struct elf_segment_map);
3169 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3170 if (m == NULL)
3171 goto error_return;
3172 m->next = NULL;
3173 m->p_type = PT_GNU_EH_FRAME;
3174 m->count = 1;
3175 m->sections[0] = eh_frame_hdr;
3177 *pm = m;
3178 pm = &m->next;
3181 free (sections);
3182 sections = NULL;
3184 elf_tdata (abfd)->segment_map = mfirst;
3185 return true;
3187 error_return:
3188 if (sections != NULL)
3189 free (sections);
3190 return false;
3193 /* Sort sections by address. */
3195 static int
3196 elf_sort_sections (arg1, arg2)
3197 const PTR arg1;
3198 const PTR arg2;
3200 const asection *sec1 = *(const asection **) arg1;
3201 const asection *sec2 = *(const asection **) arg2;
3203 /* Sort by LMA first, since this is the address used to
3204 place the section into a segment. */
3205 if (sec1->lma < sec2->lma)
3206 return -1;
3207 else if (sec1->lma > sec2->lma)
3208 return 1;
3210 /* Then sort by VMA. Normally the LMA and the VMA will be
3211 the same, and this will do nothing. */
3212 if (sec1->vma < sec2->vma)
3213 return -1;
3214 else if (sec1->vma > sec2->vma)
3215 return 1;
3217 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3219 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3221 if (TOEND (sec1))
3223 if (TOEND (sec2))
3225 /* If the indicies are the same, do not return 0
3226 here, but continue to try the next comparison. */
3227 if (sec1->target_index - sec2->target_index != 0)
3228 return sec1->target_index - sec2->target_index;
3230 else
3231 return 1;
3233 else if (TOEND (sec2))
3234 return -1;
3236 #undef TOEND
3238 /* Sort by size, to put zero sized sections
3239 before others at the same address. */
3241 if (sec1->_raw_size < sec2->_raw_size)
3242 return -1;
3243 if (sec1->_raw_size > sec2->_raw_size)
3244 return 1;
3246 return sec1->target_index - sec2->target_index;
3249 /* Assign file positions to the sections based on the mapping from
3250 sections to segments. This function also sets up some fields in
3251 the file header, and writes out the program headers. */
3253 static boolean
3254 assign_file_positions_for_segments (abfd)
3255 bfd *abfd;
3257 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3258 unsigned int count;
3259 struct elf_segment_map *m;
3260 unsigned int alloc;
3261 Elf_Internal_Phdr *phdrs;
3262 file_ptr off, voff;
3263 bfd_vma filehdr_vaddr, filehdr_paddr;
3264 bfd_vma phdrs_vaddr, phdrs_paddr;
3265 Elf_Internal_Phdr *p;
3266 bfd_size_type amt;
3268 if (elf_tdata (abfd)->segment_map == NULL)
3270 if (! map_sections_to_segments (abfd))
3271 return false;
3274 if (bed->elf_backend_modify_segment_map)
3276 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3277 return false;
3280 count = 0;
3281 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3282 ++count;
3284 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3285 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3286 elf_elfheader (abfd)->e_phnum = count;
3288 if (count == 0)
3289 return true;
3291 /* If we already counted the number of program segments, make sure
3292 that we allocated enough space. This happens when SIZEOF_HEADERS
3293 is used in a linker script. */
3294 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3295 if (alloc != 0 && count > alloc)
3297 ((*_bfd_error_handler)
3298 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3299 bfd_get_filename (abfd), alloc, count));
3300 bfd_set_error (bfd_error_bad_value);
3301 return false;
3304 if (alloc == 0)
3305 alloc = count;
3307 amt = alloc * sizeof (Elf_Internal_Phdr);
3308 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3309 if (phdrs == NULL)
3310 return false;
3312 off = bed->s->sizeof_ehdr;
3313 off += alloc * bed->s->sizeof_phdr;
3315 filehdr_vaddr = 0;
3316 filehdr_paddr = 0;
3317 phdrs_vaddr = 0;
3318 phdrs_paddr = 0;
3320 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3321 m != NULL;
3322 m = m->next, p++)
3324 unsigned int i;
3325 asection **secpp;
3327 /* If elf_segment_map is not from map_sections_to_segments, the
3328 sections may not be correctly ordered. NOTE: sorting should
3329 not be done to the PT_NOTE section of a corefile, which may
3330 contain several pseudo-sections artificially created by bfd.
3331 Sorting these pseudo-sections breaks things badly. */
3332 if (m->count > 1
3333 && !(elf_elfheader (abfd)->e_type == ET_CORE
3334 && m->p_type == PT_NOTE))
3335 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3336 elf_sort_sections);
3338 p->p_type = m->p_type;
3339 p->p_flags = m->p_flags;
3341 if (p->p_type == PT_LOAD
3342 && m->count > 0
3343 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3345 if ((abfd->flags & D_PAGED) != 0)
3346 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3347 else
3349 bfd_size_type align;
3351 align = 0;
3352 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3354 bfd_size_type secalign;
3356 secalign = bfd_get_section_alignment (abfd, *secpp);
3357 if (secalign > align)
3358 align = secalign;
3361 off += (m->sections[0]->vma - off) % (1 << align);
3365 if (m->count == 0)
3366 p->p_vaddr = 0;
3367 else
3368 p->p_vaddr = m->sections[0]->vma;
3370 if (m->p_paddr_valid)
3371 p->p_paddr = m->p_paddr;
3372 else if (m->count == 0)
3373 p->p_paddr = 0;
3374 else
3375 p->p_paddr = m->sections[0]->lma;
3377 if (p->p_type == PT_LOAD
3378 && (abfd->flags & D_PAGED) != 0)
3379 p->p_align = bed->maxpagesize;
3380 else if (m->count == 0)
3381 p->p_align = bed->s->file_align;
3382 else
3383 p->p_align = 0;
3385 p->p_offset = 0;
3386 p->p_filesz = 0;
3387 p->p_memsz = 0;
3389 if (m->includes_filehdr)
3391 if (! m->p_flags_valid)
3392 p->p_flags |= PF_R;
3393 p->p_offset = 0;
3394 p->p_filesz = bed->s->sizeof_ehdr;
3395 p->p_memsz = bed->s->sizeof_ehdr;
3396 if (m->count > 0)
3398 BFD_ASSERT (p->p_type == PT_LOAD);
3400 if (p->p_vaddr < (bfd_vma) off)
3402 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3403 bfd_get_filename (abfd));
3404 bfd_set_error (bfd_error_bad_value);
3405 return false;
3408 p->p_vaddr -= off;
3409 if (! m->p_paddr_valid)
3410 p->p_paddr -= off;
3412 if (p->p_type == PT_LOAD)
3414 filehdr_vaddr = p->p_vaddr;
3415 filehdr_paddr = p->p_paddr;
3419 if (m->includes_phdrs)
3421 if (! m->p_flags_valid)
3422 p->p_flags |= PF_R;
3424 if (m->includes_filehdr)
3426 if (p->p_type == PT_LOAD)
3428 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3429 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3432 else
3434 p->p_offset = bed->s->sizeof_ehdr;
3436 if (m->count > 0)
3438 BFD_ASSERT (p->p_type == PT_LOAD);
3439 p->p_vaddr -= off - p->p_offset;
3440 if (! m->p_paddr_valid)
3441 p->p_paddr -= off - p->p_offset;
3444 if (p->p_type == PT_LOAD)
3446 phdrs_vaddr = p->p_vaddr;
3447 phdrs_paddr = p->p_paddr;
3449 else
3450 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3453 p->p_filesz += alloc * bed->s->sizeof_phdr;
3454 p->p_memsz += alloc * bed->s->sizeof_phdr;
3457 if (p->p_type == PT_LOAD
3458 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3460 if (! m->includes_filehdr && ! m->includes_phdrs)
3461 p->p_offset = off;
3462 else
3464 file_ptr adjust;
3466 adjust = off - (p->p_offset + p->p_filesz);
3467 p->p_filesz += adjust;
3468 p->p_memsz += adjust;
3472 voff = off;
3474 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3476 asection *sec;
3477 flagword flags;
3478 bfd_size_type align;
3480 sec = *secpp;
3481 flags = sec->flags;
3482 align = 1 << bfd_get_section_alignment (abfd, sec);
3484 /* The section may have artificial alignment forced by a
3485 link script. Notice this case by the gap between the
3486 cumulative phdr lma and the section's lma. */
3487 if (p->p_paddr + p->p_memsz < sec->lma)
3489 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3491 p->p_memsz += adjust;
3492 off += adjust;
3493 voff += adjust;
3494 if ((flags & SEC_LOAD) != 0)
3495 p->p_filesz += adjust;
3498 if (p->p_type == PT_LOAD)
3500 bfd_signed_vma adjust;
3502 if ((flags & SEC_LOAD) != 0)
3504 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3505 if (adjust < 0)
3506 adjust = 0;
3508 else if ((flags & SEC_ALLOC) != 0)
3510 /* The section VMA must equal the file position
3511 modulo the page size. FIXME: I'm not sure if
3512 this adjustment is really necessary. We used to
3513 not have the SEC_LOAD case just above, and then
3514 this was necessary, but now I'm not sure. */
3515 if ((abfd->flags & D_PAGED) != 0)
3516 adjust = (sec->vma - voff) % bed->maxpagesize;
3517 else
3518 adjust = (sec->vma - voff) % align;
3520 else
3521 adjust = 0;
3523 if (adjust != 0)
3525 if (i == 0)
3527 (* _bfd_error_handler) (_("\
3528 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3529 bfd_section_name (abfd, sec),
3530 sec->lma,
3531 p->p_paddr);
3532 return false;
3534 p->p_memsz += adjust;
3535 off += adjust;
3536 voff += adjust;
3537 if ((flags & SEC_LOAD) != 0)
3538 p->p_filesz += adjust;
3541 sec->filepos = off;
3543 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3544 used in a linker script we may have a section with
3545 SEC_LOAD clear but which is supposed to have
3546 contents. */
3547 if ((flags & SEC_LOAD) != 0
3548 || (flags & SEC_HAS_CONTENTS) != 0)
3549 off += sec->_raw_size;
3551 if ((flags & SEC_ALLOC) != 0)
3552 voff += sec->_raw_size;
3555 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3557 /* The actual "note" segment has i == 0.
3558 This is the one that actually contains everything. */
3559 if (i == 0)
3561 sec->filepos = off;
3562 p->p_filesz = sec->_raw_size;
3563 off += sec->_raw_size;
3564 voff = off;
3566 else
3568 /* Fake sections -- don't need to be written. */
3569 sec->filepos = 0;
3570 sec->_raw_size = 0;
3571 flags = sec->flags = 0;
3573 p->p_memsz = 0;
3574 p->p_align = 1;
3576 else
3578 p->p_memsz += sec->_raw_size;
3580 if ((flags & SEC_LOAD) != 0)
3581 p->p_filesz += sec->_raw_size;
3583 if (align > p->p_align
3584 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3585 p->p_align = align;
3588 if (! m->p_flags_valid)
3590 p->p_flags |= PF_R;
3591 if ((flags & SEC_CODE) != 0)
3592 p->p_flags |= PF_X;
3593 if ((flags & SEC_READONLY) == 0)
3594 p->p_flags |= PF_W;
3599 /* Now that we have set the section file positions, we can set up
3600 the file positions for the non PT_LOAD segments. */
3601 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3602 m != NULL;
3603 m = m->next, p++)
3605 if (p->p_type != PT_LOAD && m->count > 0)
3607 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3608 p->p_offset = m->sections[0]->filepos;
3610 if (m->count == 0)
3612 if (m->includes_filehdr)
3614 p->p_vaddr = filehdr_vaddr;
3615 if (! m->p_paddr_valid)
3616 p->p_paddr = filehdr_paddr;
3618 else if (m->includes_phdrs)
3620 p->p_vaddr = phdrs_vaddr;
3621 if (! m->p_paddr_valid)
3622 p->p_paddr = phdrs_paddr;
3627 /* Clear out any program headers we allocated but did not use. */
3628 for (; count < alloc; count++, p++)
3630 memset (p, 0, sizeof *p);
3631 p->p_type = PT_NULL;
3634 elf_tdata (abfd)->phdr = phdrs;
3636 elf_tdata (abfd)->next_file_pos = off;
3638 /* Write out the program headers. */
3639 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
3640 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3641 return false;
3643 return true;
3646 /* Get the size of the program header.
3648 If this is called by the linker before any of the section VMA's are set, it
3649 can't calculate the correct value for a strange memory layout. This only
3650 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3651 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3652 data segment (exclusive of .interp and .dynamic).
3654 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3655 will be two segments. */
3657 static bfd_size_type
3658 get_program_header_size (abfd)
3659 bfd *abfd;
3661 size_t segs;
3662 asection *s;
3663 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3665 /* We can't return a different result each time we're called. */
3666 if (elf_tdata (abfd)->program_header_size != 0)
3667 return elf_tdata (abfd)->program_header_size;
3669 if (elf_tdata (abfd)->segment_map != NULL)
3671 struct elf_segment_map *m;
3673 segs = 0;
3674 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3675 ++segs;
3676 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3677 return elf_tdata (abfd)->program_header_size;
3680 /* Assume we will need exactly two PT_LOAD segments: one for text
3681 and one for data. */
3682 segs = 2;
3684 s = bfd_get_section_by_name (abfd, ".interp");
3685 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3687 /* If we have a loadable interpreter section, we need a
3688 PT_INTERP segment. In this case, assume we also need a
3689 PT_PHDR segment, although that may not be true for all
3690 targets. */
3691 segs += 2;
3694 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3696 /* We need a PT_DYNAMIC segment. */
3697 ++segs;
3700 if (elf_tdata (abfd)->eh_frame_hdr
3701 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
3703 /* We need a PT_GNU_EH_FRAME segment. */
3704 ++segs;
3707 for (s = abfd->sections; s != NULL; s = s->next)
3709 if ((s->flags & SEC_LOAD) != 0
3710 && strncmp (s->name, ".note", 5) == 0)
3712 /* We need a PT_NOTE segment. */
3713 ++segs;
3717 /* Let the backend count up any program headers it might need. */
3718 if (bed->elf_backend_additional_program_headers)
3720 int a;
3722 a = (*bed->elf_backend_additional_program_headers) (abfd);
3723 if (a == -1)
3724 abort ();
3725 segs += a;
3728 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3729 return elf_tdata (abfd)->program_header_size;
3732 /* Work out the file positions of all the sections. This is called by
3733 _bfd_elf_compute_section_file_positions. All the section sizes and
3734 VMAs must be known before this is called.
3736 We do not consider reloc sections at this point, unless they form
3737 part of the loadable image. Reloc sections are assigned file
3738 positions in assign_file_positions_for_relocs, which is called by
3739 write_object_contents and final_link.
3741 We also don't set the positions of the .symtab and .strtab here. */
3743 static boolean
3744 assign_file_positions_except_relocs (abfd)
3745 bfd *abfd;
3747 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3748 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3749 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3750 unsigned int num_sec = elf_numsections (abfd);
3751 file_ptr off;
3752 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3754 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3755 && bfd_get_format (abfd) != bfd_core)
3757 Elf_Internal_Shdr **hdrpp;
3758 unsigned int i;
3760 /* Start after the ELF header. */
3761 off = i_ehdrp->e_ehsize;
3763 /* We are not creating an executable, which means that we are
3764 not creating a program header, and that the actual order of
3765 the sections in the file is unimportant. */
3766 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3768 Elf_Internal_Shdr *hdr;
3770 hdr = *hdrpp;
3771 if (hdr->sh_type == SHT_REL
3772 || hdr->sh_type == SHT_RELA
3773 || i == tdata->symtab_section
3774 || i == tdata->symtab_shndx_section
3775 || i == tdata->strtab_section)
3777 hdr->sh_offset = -1;
3779 else
3780 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3782 if (i == SHN_LORESERVE - 1)
3784 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3785 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3789 else
3791 unsigned int i;
3792 Elf_Internal_Shdr **hdrpp;
3794 /* Assign file positions for the loaded sections based on the
3795 assignment of sections to segments. */
3796 if (! assign_file_positions_for_segments (abfd))
3797 return false;
3799 /* Assign file positions for the other sections. */
3801 off = elf_tdata (abfd)->next_file_pos;
3802 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3804 Elf_Internal_Shdr *hdr;
3806 hdr = *hdrpp;
3807 if (hdr->bfd_section != NULL
3808 && hdr->bfd_section->filepos != 0)
3809 hdr->sh_offset = hdr->bfd_section->filepos;
3810 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3812 ((*_bfd_error_handler)
3813 (_("%s: warning: allocated section `%s' not in segment"),
3814 bfd_get_filename (abfd),
3815 (hdr->bfd_section == NULL
3816 ? "*unknown*"
3817 : hdr->bfd_section->name)));
3818 if ((abfd->flags & D_PAGED) != 0)
3819 off += (hdr->sh_addr - off) % bed->maxpagesize;
3820 else
3821 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3822 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3823 false);
3825 else if (hdr->sh_type == SHT_REL
3826 || hdr->sh_type == SHT_RELA
3827 || hdr == i_shdrpp[tdata->symtab_section]
3828 || hdr == i_shdrpp[tdata->symtab_shndx_section]
3829 || hdr == i_shdrpp[tdata->strtab_section])
3830 hdr->sh_offset = -1;
3831 else
3832 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3834 if (i == SHN_LORESERVE - 1)
3836 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3837 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3842 /* Place the section headers. */
3843 off = align_file_position (off, bed->s->file_align);
3844 i_ehdrp->e_shoff = off;
3845 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3847 elf_tdata (abfd)->next_file_pos = off;
3849 return true;
3852 static boolean
3853 prep_headers (abfd)
3854 bfd *abfd;
3856 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3857 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3858 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3859 int count;
3860 struct elf_strtab_hash *shstrtab;
3861 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3863 i_ehdrp = elf_elfheader (abfd);
3864 i_shdrp = elf_elfsections (abfd);
3866 shstrtab = _bfd_elf_strtab_init ();
3867 if (shstrtab == NULL)
3868 return false;
3870 elf_shstrtab (abfd) = shstrtab;
3872 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3873 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3874 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3875 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3877 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3878 i_ehdrp->e_ident[EI_DATA] =
3879 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3880 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3882 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3883 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3885 for (count = EI_PAD; count < EI_NIDENT; count++)
3886 i_ehdrp->e_ident[count] = 0;
3888 if ((abfd->flags & DYNAMIC) != 0)
3889 i_ehdrp->e_type = ET_DYN;
3890 else if ((abfd->flags & EXEC_P) != 0)
3891 i_ehdrp->e_type = ET_EXEC;
3892 else if (bfd_get_format (abfd) == bfd_core)
3893 i_ehdrp->e_type = ET_CORE;
3894 else
3895 i_ehdrp->e_type = ET_REL;
3897 switch (bfd_get_arch (abfd))
3899 case bfd_arch_unknown:
3900 i_ehdrp->e_machine = EM_NONE;
3901 break;
3903 /* There used to be a long list of cases here, each one setting
3904 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3905 in the corresponding bfd definition. To avoid duplication,
3906 the switch was removed. Machines that need special handling
3907 can generally do it in elf_backend_final_write_processing(),
3908 unless they need the information earlier than the final write.
3909 Such need can generally be supplied by replacing the tests for
3910 e_machine with the conditions used to determine it. */
3911 default:
3912 if (get_elf_backend_data (abfd) != NULL)
3913 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
3914 else
3915 i_ehdrp->e_machine = EM_NONE;
3918 i_ehdrp->e_version = bed->s->ev_current;
3919 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3921 /* No program header, for now. */
3922 i_ehdrp->e_phoff = 0;
3923 i_ehdrp->e_phentsize = 0;
3924 i_ehdrp->e_phnum = 0;
3926 /* Each bfd section is section header entry. */
3927 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3928 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3930 /* If we're building an executable, we'll need a program header table. */
3931 if (abfd->flags & EXEC_P)
3933 /* It all happens later. */
3934 #if 0
3935 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3937 /* elf_build_phdrs() returns a (NULL-terminated) array of
3938 Elf_Internal_Phdrs. */
3939 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3940 i_ehdrp->e_phoff = outbase;
3941 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3942 #endif
3944 else
3946 i_ehdrp->e_phentsize = 0;
3947 i_phdrp = 0;
3948 i_ehdrp->e_phoff = 0;
3951 elf_tdata (abfd)->symtab_hdr.sh_name =
3952 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
3953 elf_tdata (abfd)->strtab_hdr.sh_name =
3954 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
3955 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3956 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
3957 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3958 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3959 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3960 return false;
3962 return true;
3965 /* Assign file positions for all the reloc sections which are not part
3966 of the loadable file image. */
3968 void
3969 _bfd_elf_assign_file_positions_for_relocs (abfd)
3970 bfd *abfd;
3972 file_ptr off;
3973 unsigned int i, num_sec;
3974 Elf_Internal_Shdr **shdrpp;
3976 off = elf_tdata (abfd)->next_file_pos;
3978 num_sec = elf_numsections (abfd);
3979 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
3981 Elf_Internal_Shdr *shdrp;
3983 shdrp = *shdrpp;
3984 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3985 && shdrp->sh_offset == -1)
3986 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3989 elf_tdata (abfd)->next_file_pos = off;
3992 boolean
3993 _bfd_elf_write_object_contents (abfd)
3994 bfd *abfd;
3996 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3997 Elf_Internal_Ehdr *i_ehdrp;
3998 Elf_Internal_Shdr **i_shdrp;
3999 boolean failed;
4000 unsigned int count, num_sec;
4002 if (! abfd->output_has_begun
4003 && ! _bfd_elf_compute_section_file_positions
4004 (abfd, (struct bfd_link_info *) NULL))
4005 return false;
4007 i_shdrp = elf_elfsections (abfd);
4008 i_ehdrp = elf_elfheader (abfd);
4010 failed = false;
4011 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4012 if (failed)
4013 return false;
4015 _bfd_elf_assign_file_positions_for_relocs (abfd);
4017 /* After writing the headers, we need to write the sections too... */
4018 num_sec = elf_numsections (abfd);
4019 for (count = 1; count < num_sec; count++)
4021 if (bed->elf_backend_section_processing)
4022 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4023 if (i_shdrp[count]->contents)
4025 bfd_size_type amt = i_shdrp[count]->sh_size;
4027 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4028 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4029 return false;
4031 if (count == SHN_LORESERVE - 1)
4032 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4035 /* Write out the section header names. */
4036 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4037 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4038 return false;
4040 if (bed->elf_backend_final_write_processing)
4041 (*bed->elf_backend_final_write_processing) (abfd,
4042 elf_tdata (abfd)->linker);
4044 return bed->s->write_shdrs_and_ehdr (abfd);
4047 boolean
4048 _bfd_elf_write_corefile_contents (abfd)
4049 bfd *abfd;
4051 /* Hopefully this can be done just like an object file. */
4052 return _bfd_elf_write_object_contents (abfd);
4055 /* Given a section, search the header to find them. */
4058 _bfd_elf_section_from_bfd_section (abfd, asect)
4059 bfd *abfd;
4060 struct sec *asect;
4062 struct elf_backend_data *bed;
4063 int index;
4065 if (elf_section_data (asect) != NULL
4066 && elf_section_data (asect)->this_idx != 0)
4067 return elf_section_data (asect)->this_idx;
4069 if (bfd_is_abs_section (asect))
4070 index = SHN_ABS;
4071 else if (bfd_is_com_section (asect))
4072 index = SHN_COMMON;
4073 else if (bfd_is_und_section (asect))
4074 index = SHN_UNDEF;
4075 else
4077 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4078 int maxindex = elf_numsections (abfd);
4080 for (index = 1; index < maxindex; index++)
4082 Elf_Internal_Shdr *hdr = i_shdrp[index];
4084 if (hdr != NULL && hdr->bfd_section == asect)
4085 return index;
4087 index = -1;
4090 bed = get_elf_backend_data (abfd);
4091 if (bed->elf_backend_section_from_bfd_section)
4093 int retval = index;
4095 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4096 return retval;
4099 if (index == -1)
4100 bfd_set_error (bfd_error_nonrepresentable_section);
4102 return index;
4105 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4106 on error. */
4109 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4110 bfd *abfd;
4111 asymbol **asym_ptr_ptr;
4113 asymbol *asym_ptr = *asym_ptr_ptr;
4114 int idx;
4115 flagword flags = asym_ptr->flags;
4117 /* When gas creates relocations against local labels, it creates its
4118 own symbol for the section, but does put the symbol into the
4119 symbol chain, so udata is 0. When the linker is generating
4120 relocatable output, this section symbol may be for one of the
4121 input sections rather than the output section. */
4122 if (asym_ptr->udata.i == 0
4123 && (flags & BSF_SECTION_SYM)
4124 && asym_ptr->section)
4126 int indx;
4128 if (asym_ptr->section->output_section != NULL)
4129 indx = asym_ptr->section->output_section->index;
4130 else
4131 indx = asym_ptr->section->index;
4132 if (indx < elf_num_section_syms (abfd)
4133 && elf_section_syms (abfd)[indx] != NULL)
4134 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4137 idx = asym_ptr->udata.i;
4139 if (idx == 0)
4141 /* This case can occur when using --strip-symbol on a symbol
4142 which is used in a relocation entry. */
4143 (*_bfd_error_handler)
4144 (_("%s: symbol `%s' required but not present"),
4145 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4146 bfd_set_error (bfd_error_no_symbols);
4147 return -1;
4150 #if DEBUG & 4
4152 fprintf (stderr,
4153 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
4154 (long) asym_ptr, asym_ptr->name, idx, flags,
4155 elf_symbol_flags (flags));
4156 fflush (stderr);
4158 #endif
4160 return idx;
4163 /* Copy private BFD data. This copies any program header information. */
4165 static boolean
4166 copy_private_bfd_data (ibfd, obfd)
4167 bfd *ibfd;
4168 bfd *obfd;
4170 Elf_Internal_Ehdr * iehdr;
4171 struct elf_segment_map * map;
4172 struct elf_segment_map * map_first;
4173 struct elf_segment_map ** pointer_to_map;
4174 Elf_Internal_Phdr * segment;
4175 asection * section;
4176 unsigned int i;
4177 unsigned int num_segments;
4178 boolean phdr_included = false;
4179 bfd_vma maxpagesize;
4180 struct elf_segment_map * phdr_adjust_seg = NULL;
4181 unsigned int phdr_adjust_num = 0;
4183 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4184 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4185 return true;
4187 if (elf_tdata (ibfd)->phdr == NULL)
4188 return true;
4190 iehdr = elf_elfheader (ibfd);
4192 map_first = NULL;
4193 pointer_to_map = &map_first;
4195 num_segments = elf_elfheader (ibfd)->e_phnum;
4196 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4198 /* Returns the end address of the segment + 1. */
4199 #define SEGMENT_END(segment, start) \
4200 (start + (segment->p_memsz > segment->p_filesz \
4201 ? segment->p_memsz : segment->p_filesz))
4203 /* Returns true if the given section is contained within
4204 the given segment. VMA addresses are compared. */
4205 #define IS_CONTAINED_BY_VMA(section, segment) \
4206 (section->vma >= segment->p_vaddr \
4207 && (section->vma + section->_raw_size) \
4208 <= (SEGMENT_END (segment, segment->p_vaddr)))
4210 /* Returns true if the given section is contained within
4211 the given segment. LMA addresses are compared. */
4212 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4213 (section->lma >= base \
4214 && (section->lma + section->_raw_size) \
4215 <= SEGMENT_END (segment, base))
4217 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4218 #define IS_COREFILE_NOTE(p, s) \
4219 (p->p_type == PT_NOTE \
4220 && bfd_get_format (ibfd) == bfd_core \
4221 && s->vma == 0 && s->lma == 0 \
4222 && (bfd_vma) s->filepos >= p->p_offset \
4223 && (bfd_vma) s->filepos + s->_raw_size \
4224 <= p->p_offset + p->p_filesz)
4226 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4227 linker, which generates a PT_INTERP section with p_vaddr and
4228 p_memsz set to 0. */
4229 #define IS_SOLARIS_PT_INTERP(p, s) \
4230 ( p->p_vaddr == 0 \
4231 && p->p_filesz > 0 \
4232 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4233 && s->_raw_size > 0 \
4234 && (bfd_vma) s->filepos >= p->p_offset \
4235 && ((bfd_vma) s->filepos + s->_raw_size \
4236 <= p->p_offset + p->p_filesz))
4238 /* Decide if the given section should be included in the given segment.
4239 A section will be included if:
4240 1. It is within the address space of the segment -- we use the LMA
4241 if that is set for the segment and the VMA otherwise,
4242 2. It is an allocated segment,
4243 3. There is an output section associated with it,
4244 4. The section has not already been allocated to a previous segment. */
4245 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4246 (((((segment->p_paddr \
4247 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4248 : IS_CONTAINED_BY_VMA (section, segment)) \
4249 || IS_SOLARIS_PT_INTERP (segment, section)) \
4250 && (section->flags & SEC_ALLOC) != 0) \
4251 || IS_COREFILE_NOTE (segment, section)) \
4252 && section->output_section != NULL \
4253 && section->segment_mark == false)
4255 /* Returns true iff seg1 starts after the end of seg2. */
4256 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4257 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4259 /* Returns true iff seg1 and seg2 overlap. */
4260 #define SEGMENT_OVERLAPS(seg1, seg2) \
4261 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4263 /* Initialise the segment mark field. */
4264 for (section = ibfd->sections; section != NULL; section = section->next)
4265 section->segment_mark = false;
4267 /* Scan through the segments specified in the program header
4268 of the input BFD. For this first scan we look for overlaps
4269 in the loadable segments. These can be created by weird
4270 parameters to objcopy. */
4271 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4272 i < num_segments;
4273 i++, segment++)
4275 unsigned int j;
4276 Elf_Internal_Phdr *segment2;
4278 if (segment->p_type != PT_LOAD)
4279 continue;
4281 /* Determine if this segment overlaps any previous segments. */
4282 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4284 bfd_signed_vma extra_length;
4286 if (segment2->p_type != PT_LOAD
4287 || ! SEGMENT_OVERLAPS (segment, segment2))
4288 continue;
4290 /* Merge the two segments together. */
4291 if (segment2->p_vaddr < segment->p_vaddr)
4293 /* Extend SEGMENT2 to include SEGMENT and then delete
4294 SEGMENT. */
4295 extra_length =
4296 SEGMENT_END (segment, segment->p_vaddr)
4297 - SEGMENT_END (segment2, segment2->p_vaddr);
4299 if (extra_length > 0)
4301 segment2->p_memsz += extra_length;
4302 segment2->p_filesz += extra_length;
4305 segment->p_type = PT_NULL;
4307 /* Since we have deleted P we must restart the outer loop. */
4308 i = 0;
4309 segment = elf_tdata (ibfd)->phdr;
4310 break;
4312 else
4314 /* Extend SEGMENT to include SEGMENT2 and then delete
4315 SEGMENT2. */
4316 extra_length =
4317 SEGMENT_END (segment2, segment2->p_vaddr)
4318 - SEGMENT_END (segment, segment->p_vaddr);
4320 if (extra_length > 0)
4322 segment->p_memsz += extra_length;
4323 segment->p_filesz += extra_length;
4326 segment2->p_type = PT_NULL;
4331 /* The second scan attempts to assign sections to segments. */
4332 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4333 i < num_segments;
4334 i ++, segment ++)
4336 unsigned int section_count;
4337 asection ** sections;
4338 asection * output_section;
4339 unsigned int isec;
4340 bfd_vma matching_lma;
4341 bfd_vma suggested_lma;
4342 unsigned int j;
4343 bfd_size_type amt;
4345 if (segment->p_type == PT_NULL)
4346 continue;
4348 /* Compute how many sections might be placed into this segment. */
4349 section_count = 0;
4350 for (section = ibfd->sections; section != NULL; section = section->next)
4351 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4352 ++section_count;
4354 /* Allocate a segment map big enough to contain all of the
4355 sections we have selected. */
4356 amt = sizeof (struct elf_segment_map);
4357 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4358 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4359 if (map == NULL)
4360 return false;
4362 /* Initialise the fields of the segment map. Default to
4363 using the physical address of the segment in the input BFD. */
4364 map->next = NULL;
4365 map->p_type = segment->p_type;
4366 map->p_flags = segment->p_flags;
4367 map->p_flags_valid = 1;
4368 map->p_paddr = segment->p_paddr;
4369 map->p_paddr_valid = 1;
4371 /* Determine if this segment contains the ELF file header
4372 and if it contains the program headers themselves. */
4373 map->includes_filehdr = (segment->p_offset == 0
4374 && segment->p_filesz >= iehdr->e_ehsize);
4376 map->includes_phdrs = 0;
4378 if (! phdr_included || segment->p_type != PT_LOAD)
4380 map->includes_phdrs =
4381 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4382 && (segment->p_offset + segment->p_filesz
4383 >= ((bfd_vma) iehdr->e_phoff
4384 + iehdr->e_phnum * iehdr->e_phentsize)));
4386 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4387 phdr_included = true;
4390 if (section_count == 0)
4392 /* Special segments, such as the PT_PHDR segment, may contain
4393 no sections, but ordinary, loadable segments should contain
4394 something. */
4395 if (segment->p_type == PT_LOAD)
4396 _bfd_error_handler
4397 (_("%s: warning: Empty loadable segment detected\n"),
4398 bfd_archive_filename (ibfd));
4400 map->count = 0;
4401 *pointer_to_map = map;
4402 pointer_to_map = &map->next;
4404 continue;
4407 /* Now scan the sections in the input BFD again and attempt
4408 to add their corresponding output sections to the segment map.
4409 The problem here is how to handle an output section which has
4410 been moved (ie had its LMA changed). There are four possibilities:
4412 1. None of the sections have been moved.
4413 In this case we can continue to use the segment LMA from the
4414 input BFD.
4416 2. All of the sections have been moved by the same amount.
4417 In this case we can change the segment's LMA to match the LMA
4418 of the first section.
4420 3. Some of the sections have been moved, others have not.
4421 In this case those sections which have not been moved can be
4422 placed in the current segment which will have to have its size,
4423 and possibly its LMA changed, and a new segment or segments will
4424 have to be created to contain the other sections.
4426 4. The sections have been moved, but not be the same amount.
4427 In this case we can change the segment's LMA to match the LMA
4428 of the first section and we will have to create a new segment
4429 or segments to contain the other sections.
4431 In order to save time, we allocate an array to hold the section
4432 pointers that we are interested in. As these sections get assigned
4433 to a segment, they are removed from this array. */
4435 amt = (bfd_size_type) section_count * sizeof (asection *);
4436 sections = (asection **) bfd_malloc (amt);
4437 if (sections == NULL)
4438 return false;
4440 /* Step One: Scan for segment vs section LMA conflicts.
4441 Also add the sections to the section array allocated above.
4442 Also add the sections to the current segment. In the common
4443 case, where the sections have not been moved, this means that
4444 we have completely filled the segment, and there is nothing
4445 more to do. */
4446 isec = 0;
4447 matching_lma = 0;
4448 suggested_lma = 0;
4450 for (j = 0, section = ibfd->sections;
4451 section != NULL;
4452 section = section->next)
4454 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4456 output_section = section->output_section;
4458 sections[j ++] = section;
4460 /* The Solaris native linker always sets p_paddr to 0.
4461 We try to catch that case here, and set it to the
4462 correct value. */
4463 if (segment->p_paddr == 0
4464 && segment->p_vaddr != 0
4465 && isec == 0
4466 && output_section->lma != 0
4467 && (output_section->vma == (segment->p_vaddr
4468 + (map->includes_filehdr
4469 ? iehdr->e_ehsize
4470 : 0)
4471 + (map->includes_phdrs
4472 ? (iehdr->e_phnum
4473 * iehdr->e_phentsize)
4474 : 0))))
4475 map->p_paddr = segment->p_vaddr;
4477 /* Match up the physical address of the segment with the
4478 LMA address of the output section. */
4479 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4480 || IS_COREFILE_NOTE (segment, section))
4482 if (matching_lma == 0)
4483 matching_lma = output_section->lma;
4485 /* We assume that if the section fits within the segment
4486 then it does not overlap any other section within that
4487 segment. */
4488 map->sections[isec ++] = output_section;
4490 else if (suggested_lma == 0)
4491 suggested_lma = output_section->lma;
4495 BFD_ASSERT (j == section_count);
4497 /* Step Two: Adjust the physical address of the current segment,
4498 if necessary. */
4499 if (isec == section_count)
4501 /* All of the sections fitted within the segment as currently
4502 specified. This is the default case. Add the segment to
4503 the list of built segments and carry on to process the next
4504 program header in the input BFD. */
4505 map->count = section_count;
4506 *pointer_to_map = map;
4507 pointer_to_map = &map->next;
4509 free (sections);
4510 continue;
4512 else
4514 if (matching_lma != 0)
4516 /* At least one section fits inside the current segment.
4517 Keep it, but modify its physical address to match the
4518 LMA of the first section that fitted. */
4519 map->p_paddr = matching_lma;
4521 else
4523 /* None of the sections fitted inside the current segment.
4524 Change the current segment's physical address to match
4525 the LMA of the first section. */
4526 map->p_paddr = suggested_lma;
4529 /* Offset the segment physical address from the lma
4530 to allow for space taken up by elf headers. */
4531 if (map->includes_filehdr)
4532 map->p_paddr -= iehdr->e_ehsize;
4534 if (map->includes_phdrs)
4536 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4538 /* iehdr->e_phnum is just an estimate of the number
4539 of program headers that we will need. Make a note
4540 here of the number we used and the segment we chose
4541 to hold these headers, so that we can adjust the
4542 offset when we know the correct value. */
4543 phdr_adjust_num = iehdr->e_phnum;
4544 phdr_adjust_seg = map;
4548 /* Step Three: Loop over the sections again, this time assigning
4549 those that fit to the current segment and remvoing them from the
4550 sections array; but making sure not to leave large gaps. Once all
4551 possible sections have been assigned to the current segment it is
4552 added to the list of built segments and if sections still remain
4553 to be assigned, a new segment is constructed before repeating
4554 the loop. */
4555 isec = 0;
4558 map->count = 0;
4559 suggested_lma = 0;
4561 /* Fill the current segment with sections that fit. */
4562 for (j = 0; j < section_count; j++)
4564 section = sections[j];
4566 if (section == NULL)
4567 continue;
4569 output_section = section->output_section;
4571 BFD_ASSERT (output_section != NULL);
4573 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4574 || IS_COREFILE_NOTE (segment, section))
4576 if (map->count == 0)
4578 /* If the first section in a segment does not start at
4579 the beginning of the segment, then something is
4580 wrong. */
4581 if (output_section->lma !=
4582 (map->p_paddr
4583 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4584 + (map->includes_phdrs
4585 ? iehdr->e_phnum * iehdr->e_phentsize
4586 : 0)))
4587 abort ();
4589 else
4591 asection * prev_sec;
4593 prev_sec = map->sections[map->count - 1];
4595 /* If the gap between the end of the previous section
4596 and the start of this section is more than
4597 maxpagesize then we need to start a new segment. */
4598 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4599 maxpagesize)
4600 < BFD_ALIGN (output_section->lma, maxpagesize))
4601 || ((prev_sec->lma + prev_sec->_raw_size)
4602 > output_section->lma))
4604 if (suggested_lma == 0)
4605 suggested_lma = output_section->lma;
4607 continue;
4611 map->sections[map->count++] = output_section;
4612 ++isec;
4613 sections[j] = NULL;
4614 section->segment_mark = true;
4616 else if (suggested_lma == 0)
4617 suggested_lma = output_section->lma;
4620 BFD_ASSERT (map->count > 0);
4622 /* Add the current segment to the list of built segments. */
4623 *pointer_to_map = map;
4624 pointer_to_map = &map->next;
4626 if (isec < section_count)
4628 /* We still have not allocated all of the sections to
4629 segments. Create a new segment here, initialise it
4630 and carry on looping. */
4631 amt = sizeof (struct elf_segment_map);
4632 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4633 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4634 if (map == NULL)
4635 return false;
4637 /* Initialise the fields of the segment map. Set the physical
4638 physical address to the LMA of the first section that has
4639 not yet been assigned. */
4640 map->next = NULL;
4641 map->p_type = segment->p_type;
4642 map->p_flags = segment->p_flags;
4643 map->p_flags_valid = 1;
4644 map->p_paddr = suggested_lma;
4645 map->p_paddr_valid = 1;
4646 map->includes_filehdr = 0;
4647 map->includes_phdrs = 0;
4650 while (isec < section_count);
4652 free (sections);
4655 /* The Solaris linker creates program headers in which all the
4656 p_paddr fields are zero. When we try to objcopy or strip such a
4657 file, we get confused. Check for this case, and if we find it
4658 reset the p_paddr_valid fields. */
4659 for (map = map_first; map != NULL; map = map->next)
4660 if (map->p_paddr != 0)
4661 break;
4662 if (map == NULL)
4664 for (map = map_first; map != NULL; map = map->next)
4665 map->p_paddr_valid = 0;
4668 elf_tdata (obfd)->segment_map = map_first;
4670 /* If we had to estimate the number of program headers that were
4671 going to be needed, then check our estimate now and adjust
4672 the offset if necessary. */
4673 if (phdr_adjust_seg != NULL)
4675 unsigned int count;
4677 for (count = 0, map = map_first; map != NULL; map = map->next)
4678 count++;
4680 if (count > phdr_adjust_num)
4681 phdr_adjust_seg->p_paddr
4682 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4685 #if 0
4686 /* Final Step: Sort the segments into ascending order of physical
4687 address. */
4688 if (map_first != NULL)
4690 struct elf_segment_map *prev;
4692 prev = map_first;
4693 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4695 /* Yes I know - its a bubble sort.... */
4696 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4698 /* Swap map and map->next. */
4699 prev->next = map->next;
4700 map->next = map->next->next;
4701 prev->next->next = map;
4703 /* Restart loop. */
4704 map = map_first;
4708 #endif
4710 #undef SEGMENT_END
4711 #undef IS_CONTAINED_BY_VMA
4712 #undef IS_CONTAINED_BY_LMA
4713 #undef IS_COREFILE_NOTE
4714 #undef IS_SOLARIS_PT_INTERP
4715 #undef INCLUDE_SECTION_IN_SEGMENT
4716 #undef SEGMENT_AFTER_SEGMENT
4717 #undef SEGMENT_OVERLAPS
4718 return true;
4721 /* Copy private section information. This copies over the entsize
4722 field, and sometimes the info field. */
4724 boolean
4725 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4726 bfd *ibfd;
4727 asection *isec;
4728 bfd *obfd;
4729 asection *osec;
4731 Elf_Internal_Shdr *ihdr, *ohdr;
4733 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4734 || obfd->xvec->flavour != bfd_target_elf_flavour)
4735 return true;
4737 /* Copy over private BFD data if it has not already been copied.
4738 This must be done here, rather than in the copy_private_bfd_data
4739 entry point, because the latter is called after the section
4740 contents have been set, which means that the program headers have
4741 already been worked out. */
4742 if (elf_tdata (obfd)->segment_map == NULL
4743 && elf_tdata (ibfd)->phdr != NULL)
4745 asection *s;
4747 /* Only set up the segments if there are no more SEC_ALLOC
4748 sections. FIXME: This won't do the right thing if objcopy is
4749 used to remove the last SEC_ALLOC section, since objcopy
4750 won't call this routine in that case. */
4751 for (s = isec->next; s != NULL; s = s->next)
4752 if ((s->flags & SEC_ALLOC) != 0)
4753 break;
4754 if (s == NULL)
4756 if (! copy_private_bfd_data (ibfd, obfd))
4757 return false;
4761 ihdr = &elf_section_data (isec)->this_hdr;
4762 ohdr = &elf_section_data (osec)->this_hdr;
4764 ohdr->sh_entsize = ihdr->sh_entsize;
4766 if (ihdr->sh_type == SHT_SYMTAB
4767 || ihdr->sh_type == SHT_DYNSYM
4768 || ihdr->sh_type == SHT_GNU_verneed
4769 || ihdr->sh_type == SHT_GNU_verdef)
4770 ohdr->sh_info = ihdr->sh_info;
4772 elf_section_data (osec)->use_rela_p
4773 = elf_section_data (isec)->use_rela_p;
4775 return true;
4778 /* Copy private symbol information. If this symbol is in a section
4779 which we did not map into a BFD section, try to map the section
4780 index correctly. We use special macro definitions for the mapped
4781 section indices; these definitions are interpreted by the
4782 swap_out_syms function. */
4784 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4785 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4786 #define MAP_STRTAB (SHN_HIOS + 3)
4787 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4788 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4790 boolean
4791 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4792 bfd *ibfd;
4793 asymbol *isymarg;
4794 bfd *obfd;
4795 asymbol *osymarg;
4797 elf_symbol_type *isym, *osym;
4799 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4800 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4801 return true;
4803 isym = elf_symbol_from (ibfd, isymarg);
4804 osym = elf_symbol_from (obfd, osymarg);
4806 if (isym != NULL
4807 && osym != NULL
4808 && bfd_is_abs_section (isym->symbol.section))
4810 unsigned int shndx;
4812 shndx = isym->internal_elf_sym.st_shndx;
4813 if (shndx == elf_onesymtab (ibfd))
4814 shndx = MAP_ONESYMTAB;
4815 else if (shndx == elf_dynsymtab (ibfd))
4816 shndx = MAP_DYNSYMTAB;
4817 else if (shndx == elf_tdata (ibfd)->strtab_section)
4818 shndx = MAP_STRTAB;
4819 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4820 shndx = MAP_SHSTRTAB;
4821 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
4822 shndx = MAP_SYM_SHNDX;
4823 osym->internal_elf_sym.st_shndx = shndx;
4826 return true;
4829 /* Swap out the symbols. */
4831 static boolean
4832 swap_out_syms (abfd, sttp, relocatable_p)
4833 bfd *abfd;
4834 struct bfd_strtab_hash **sttp;
4835 int relocatable_p;
4837 struct elf_backend_data *bed;
4838 int symcount;
4839 asymbol **syms;
4840 struct bfd_strtab_hash *stt;
4841 Elf_Internal_Shdr *symtab_hdr;
4842 Elf_Internal_Shdr *symtab_shndx_hdr;
4843 Elf_Internal_Shdr *symstrtab_hdr;
4844 char *outbound_syms;
4845 char *outbound_shndx;
4846 int idx;
4847 bfd_size_type amt;
4849 if (!elf_map_symbols (abfd))
4850 return false;
4852 /* Dump out the symtabs. */
4853 stt = _bfd_elf_stringtab_init ();
4854 if (stt == NULL)
4855 return false;
4857 bed = get_elf_backend_data (abfd);
4858 symcount = bfd_get_symcount (abfd);
4859 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4860 symtab_hdr->sh_type = SHT_SYMTAB;
4861 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4862 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4863 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4864 symtab_hdr->sh_addralign = bed->s->file_align;
4866 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4867 symstrtab_hdr->sh_type = SHT_STRTAB;
4869 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
4870 outbound_syms = bfd_alloc (abfd, amt);
4871 if (outbound_syms == NULL)
4872 return false;
4873 symtab_hdr->contents = (PTR) outbound_syms;
4875 outbound_shndx = NULL;
4876 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
4877 if (symtab_shndx_hdr->sh_name != 0)
4879 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
4880 outbound_shndx = bfd_alloc (abfd, amt);
4881 if (outbound_shndx == NULL)
4882 return false;
4883 memset (outbound_shndx, 0, (unsigned long) amt);
4884 symtab_shndx_hdr->contents = outbound_shndx;
4885 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
4886 symtab_shndx_hdr->sh_size = amt;
4887 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
4888 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
4891 /* now generate the data (for "contents") */
4893 /* Fill in zeroth symbol and swap it out. */
4894 Elf_Internal_Sym sym;
4895 sym.st_name = 0;
4896 sym.st_value = 0;
4897 sym.st_size = 0;
4898 sym.st_info = 0;
4899 sym.st_other = 0;
4900 sym.st_shndx = SHN_UNDEF;
4901 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
4902 outbound_syms += bed->s->sizeof_sym;
4903 if (outbound_shndx != NULL)
4904 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
4907 syms = bfd_get_outsymbols (abfd);
4908 for (idx = 0; idx < symcount; idx++)
4910 Elf_Internal_Sym sym;
4911 bfd_vma value = syms[idx]->value;
4912 elf_symbol_type *type_ptr;
4913 flagword flags = syms[idx]->flags;
4914 int type;
4916 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4918 /* Local section symbols have no name. */
4919 sym.st_name = 0;
4921 else
4923 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4924 syms[idx]->name,
4925 true, false);
4926 if (sym.st_name == (unsigned long) -1)
4927 return false;
4930 type_ptr = elf_symbol_from (abfd, syms[idx]);
4932 if ((flags & BSF_SECTION_SYM) == 0
4933 && bfd_is_com_section (syms[idx]->section))
4935 /* ELF common symbols put the alignment into the `value' field,
4936 and the size into the `size' field. This is backwards from
4937 how BFD handles it, so reverse it here. */
4938 sym.st_size = value;
4939 if (type_ptr == NULL
4940 || type_ptr->internal_elf_sym.st_value == 0)
4941 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4942 else
4943 sym.st_value = type_ptr->internal_elf_sym.st_value;
4944 sym.st_shndx = _bfd_elf_section_from_bfd_section
4945 (abfd, syms[idx]->section);
4947 else
4949 asection *sec = syms[idx]->section;
4950 int shndx;
4952 if (sec->output_section)
4954 value += sec->output_offset;
4955 sec = sec->output_section;
4957 /* Don't add in the section vma for relocatable output. */
4958 if (! relocatable_p)
4959 value += sec->vma;
4960 sym.st_value = value;
4961 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4963 if (bfd_is_abs_section (sec)
4964 && type_ptr != NULL
4965 && type_ptr->internal_elf_sym.st_shndx != 0)
4967 /* This symbol is in a real ELF section which we did
4968 not create as a BFD section. Undo the mapping done
4969 by copy_private_symbol_data. */
4970 shndx = type_ptr->internal_elf_sym.st_shndx;
4971 switch (shndx)
4973 case MAP_ONESYMTAB:
4974 shndx = elf_onesymtab (abfd);
4975 break;
4976 case MAP_DYNSYMTAB:
4977 shndx = elf_dynsymtab (abfd);
4978 break;
4979 case MAP_STRTAB:
4980 shndx = elf_tdata (abfd)->strtab_section;
4981 break;
4982 case MAP_SHSTRTAB:
4983 shndx = elf_tdata (abfd)->shstrtab_section;
4984 break;
4985 case MAP_SYM_SHNDX:
4986 shndx = elf_tdata (abfd)->symtab_shndx_section;
4987 break;
4988 default:
4989 break;
4992 else
4994 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4996 if (shndx == -1)
4998 asection *sec2;
5000 /* Writing this would be a hell of a lot easier if
5001 we had some decent documentation on bfd, and
5002 knew what to expect of the library, and what to
5003 demand of applications. For example, it
5004 appears that `objcopy' might not set the
5005 section of a symbol to be a section that is
5006 actually in the output file. */
5007 sec2 = bfd_get_section_by_name (abfd, sec->name);
5008 BFD_ASSERT (sec2 != 0);
5009 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5010 BFD_ASSERT (shndx != -1);
5014 sym.st_shndx = shndx;
5017 if ((flags & BSF_FUNCTION) != 0)
5018 type = STT_FUNC;
5019 else if ((flags & BSF_OBJECT) != 0)
5020 type = STT_OBJECT;
5021 else
5022 type = STT_NOTYPE;
5024 /* Processor-specific types */
5025 if (type_ptr != NULL
5026 && bed->elf_backend_get_symbol_type)
5027 type = ((*bed->elf_backend_get_symbol_type)
5028 (&type_ptr->internal_elf_sym, type));
5030 if (flags & BSF_SECTION_SYM)
5032 if (flags & BSF_GLOBAL)
5033 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5034 else
5035 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5037 else if (bfd_is_com_section (syms[idx]->section))
5038 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5039 else if (bfd_is_und_section (syms[idx]->section))
5040 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5041 ? STB_WEAK
5042 : STB_GLOBAL),
5043 type);
5044 else if (flags & BSF_FILE)
5045 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5046 else
5048 int bind = STB_LOCAL;
5050 if (flags & BSF_LOCAL)
5051 bind = STB_LOCAL;
5052 else if (flags & BSF_WEAK)
5053 bind = STB_WEAK;
5054 else if (flags & BSF_GLOBAL)
5055 bind = STB_GLOBAL;
5057 sym.st_info = ELF_ST_INFO (bind, type);
5060 if (type_ptr != NULL)
5061 sym.st_other = type_ptr->internal_elf_sym.st_other;
5062 else
5063 sym.st_other = 0;
5065 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5066 outbound_syms += bed->s->sizeof_sym;
5067 if (outbound_shndx != NULL)
5068 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5071 *sttp = stt;
5072 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5073 symstrtab_hdr->sh_type = SHT_STRTAB;
5075 symstrtab_hdr->sh_flags = 0;
5076 symstrtab_hdr->sh_addr = 0;
5077 symstrtab_hdr->sh_entsize = 0;
5078 symstrtab_hdr->sh_link = 0;
5079 symstrtab_hdr->sh_info = 0;
5080 symstrtab_hdr->sh_addralign = 1;
5082 return true;
5085 /* Return the number of bytes required to hold the symtab vector.
5087 Note that we base it on the count plus 1, since we will null terminate
5088 the vector allocated based on this size. However, the ELF symbol table
5089 always has a dummy entry as symbol #0, so it ends up even. */
5091 long
5092 _bfd_elf_get_symtab_upper_bound (abfd)
5093 bfd *abfd;
5095 long symcount;
5096 long symtab_size;
5097 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5099 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5100 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5102 return symtab_size;
5105 long
5106 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5107 bfd *abfd;
5109 long symcount;
5110 long symtab_size;
5111 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5113 if (elf_dynsymtab (abfd) == 0)
5115 bfd_set_error (bfd_error_invalid_operation);
5116 return -1;
5119 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5120 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5122 return symtab_size;
5125 long
5126 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5127 bfd *abfd ATTRIBUTE_UNUSED;
5128 sec_ptr asect;
5130 return (asect->reloc_count + 1) * sizeof (arelent *);
5133 /* Canonicalize the relocs. */
5135 long
5136 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5137 bfd *abfd;
5138 sec_ptr section;
5139 arelent **relptr;
5140 asymbol **symbols;
5142 arelent *tblptr;
5143 unsigned int i;
5144 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5146 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
5147 return -1;
5149 tblptr = section->relocation;
5150 for (i = 0; i < section->reloc_count; i++)
5151 *relptr++ = tblptr++;
5153 *relptr = NULL;
5155 return section->reloc_count;
5158 long
5159 _bfd_elf_get_symtab (abfd, alocation)
5160 bfd *abfd;
5161 asymbol **alocation;
5163 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5164 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
5166 if (symcount >= 0)
5167 bfd_get_symcount (abfd) = symcount;
5168 return symcount;
5171 long
5172 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5173 bfd *abfd;
5174 asymbol **alocation;
5176 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5177 return bed->s->slurp_symbol_table (abfd, alocation, true);
5180 /* Return the size required for the dynamic reloc entries. Any
5181 section that was actually installed in the BFD, and has type
5182 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5183 considered to be a dynamic reloc section. */
5185 long
5186 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5187 bfd *abfd;
5189 long ret;
5190 asection *s;
5192 if (elf_dynsymtab (abfd) == 0)
5194 bfd_set_error (bfd_error_invalid_operation);
5195 return -1;
5198 ret = sizeof (arelent *);
5199 for (s = abfd->sections; s != NULL; s = s->next)
5200 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5201 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5202 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5203 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5204 * sizeof (arelent *));
5206 return ret;
5209 /* Canonicalize the dynamic relocation entries. Note that we return
5210 the dynamic relocations as a single block, although they are
5211 actually associated with particular sections; the interface, which
5212 was designed for SunOS style shared libraries, expects that there
5213 is only one set of dynamic relocs. Any section that was actually
5214 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5215 the dynamic symbol table, is considered to be a dynamic reloc
5216 section. */
5218 long
5219 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5220 bfd *abfd;
5221 arelent **storage;
5222 asymbol **syms;
5224 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5225 asection *s;
5226 long ret;
5228 if (elf_dynsymtab (abfd) == 0)
5230 bfd_set_error (bfd_error_invalid_operation);
5231 return -1;
5234 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5235 ret = 0;
5236 for (s = abfd->sections; s != NULL; s = s->next)
5238 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5239 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5240 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5242 arelent *p;
5243 long count, i;
5245 if (! (*slurp_relocs) (abfd, s, syms, true))
5246 return -1;
5247 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5248 p = s->relocation;
5249 for (i = 0; i < count; i++)
5250 *storage++ = p++;
5251 ret += count;
5255 *storage = NULL;
5257 return ret;
5260 /* Read in the version information. */
5262 boolean
5263 _bfd_elf_slurp_version_tables (abfd)
5264 bfd *abfd;
5266 bfd_byte *contents = NULL;
5267 bfd_size_type amt;
5269 if (elf_dynverdef (abfd) != 0)
5271 Elf_Internal_Shdr *hdr;
5272 Elf_External_Verdef *everdef;
5273 Elf_Internal_Verdef *iverdef;
5274 Elf_Internal_Verdef *iverdefarr;
5275 Elf_Internal_Verdef iverdefmem;
5276 unsigned int i;
5277 unsigned int maxidx;
5279 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5281 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5282 if (contents == NULL)
5283 goto error_return;
5284 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5285 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5286 goto error_return;
5288 /* We know the number of entries in the section but not the maximum
5289 index. Therefore we have to run through all entries and find
5290 the maximum. */
5291 everdef = (Elf_External_Verdef *) contents;
5292 maxidx = 0;
5293 for (i = 0; i < hdr->sh_info; ++i)
5295 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5297 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5298 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5300 everdef = ((Elf_External_Verdef *)
5301 ((bfd_byte *) everdef + iverdefmem.vd_next));
5304 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5305 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5306 if (elf_tdata (abfd)->verdef == NULL)
5307 goto error_return;
5309 elf_tdata (abfd)->cverdefs = maxidx;
5311 everdef = (Elf_External_Verdef *) contents;
5312 iverdefarr = elf_tdata (abfd)->verdef;
5313 for (i = 0; i < hdr->sh_info; i++)
5315 Elf_External_Verdaux *everdaux;
5316 Elf_Internal_Verdaux *iverdaux;
5317 unsigned int j;
5319 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5321 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5322 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5324 iverdef->vd_bfd = abfd;
5326 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5327 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5328 if (iverdef->vd_auxptr == NULL)
5329 goto error_return;
5331 everdaux = ((Elf_External_Verdaux *)
5332 ((bfd_byte *) everdef + iverdef->vd_aux));
5333 iverdaux = iverdef->vd_auxptr;
5334 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5336 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5338 iverdaux->vda_nodename =
5339 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5340 iverdaux->vda_name);
5341 if (iverdaux->vda_nodename == NULL)
5342 goto error_return;
5344 if (j + 1 < iverdef->vd_cnt)
5345 iverdaux->vda_nextptr = iverdaux + 1;
5346 else
5347 iverdaux->vda_nextptr = NULL;
5349 everdaux = ((Elf_External_Verdaux *)
5350 ((bfd_byte *) everdaux + iverdaux->vda_next));
5353 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5355 if (i + 1 < hdr->sh_info)
5356 iverdef->vd_nextdef = iverdef + 1;
5357 else
5358 iverdef->vd_nextdef = NULL;
5360 everdef = ((Elf_External_Verdef *)
5361 ((bfd_byte *) everdef + iverdef->vd_next));
5364 free (contents);
5365 contents = NULL;
5368 if (elf_dynverref (abfd) != 0)
5370 Elf_Internal_Shdr *hdr;
5371 Elf_External_Verneed *everneed;
5372 Elf_Internal_Verneed *iverneed;
5373 unsigned int i;
5375 hdr = &elf_tdata (abfd)->dynverref_hdr;
5377 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5378 elf_tdata (abfd)->verref =
5379 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5380 if (elf_tdata (abfd)->verref == NULL)
5381 goto error_return;
5383 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5385 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5386 if (contents == NULL)
5387 goto error_return;
5388 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5389 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5390 goto error_return;
5392 everneed = (Elf_External_Verneed *) contents;
5393 iverneed = elf_tdata (abfd)->verref;
5394 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5396 Elf_External_Vernaux *evernaux;
5397 Elf_Internal_Vernaux *ivernaux;
5398 unsigned int j;
5400 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5402 iverneed->vn_bfd = abfd;
5404 iverneed->vn_filename =
5405 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5406 iverneed->vn_file);
5407 if (iverneed->vn_filename == NULL)
5408 goto error_return;
5410 amt = iverneed->vn_cnt;
5411 amt *= sizeof (Elf_Internal_Vernaux);
5412 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5414 evernaux = ((Elf_External_Vernaux *)
5415 ((bfd_byte *) everneed + iverneed->vn_aux));
5416 ivernaux = iverneed->vn_auxptr;
5417 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5419 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5421 ivernaux->vna_nodename =
5422 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5423 ivernaux->vna_name);
5424 if (ivernaux->vna_nodename == NULL)
5425 goto error_return;
5427 if (j + 1 < iverneed->vn_cnt)
5428 ivernaux->vna_nextptr = ivernaux + 1;
5429 else
5430 ivernaux->vna_nextptr = NULL;
5432 evernaux = ((Elf_External_Vernaux *)
5433 ((bfd_byte *) evernaux + ivernaux->vna_next));
5436 if (i + 1 < hdr->sh_info)
5437 iverneed->vn_nextref = iverneed + 1;
5438 else
5439 iverneed->vn_nextref = NULL;
5441 everneed = ((Elf_External_Verneed *)
5442 ((bfd_byte *) everneed + iverneed->vn_next));
5445 free (contents);
5446 contents = NULL;
5449 return true;
5451 error_return:
5452 if (contents == NULL)
5453 free (contents);
5454 return false;
5457 asymbol *
5458 _bfd_elf_make_empty_symbol (abfd)
5459 bfd *abfd;
5461 elf_symbol_type *newsym;
5462 bfd_size_type amt = sizeof (elf_symbol_type);
5464 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5465 if (!newsym)
5466 return NULL;
5467 else
5469 newsym->symbol.the_bfd = abfd;
5470 return &newsym->symbol;
5474 void
5475 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5476 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5477 asymbol *symbol;
5478 symbol_info *ret;
5480 bfd_symbol_info (symbol, ret);
5483 /* Return whether a symbol name implies a local symbol. Most targets
5484 use this function for the is_local_label_name entry point, but some
5485 override it. */
5487 boolean
5488 _bfd_elf_is_local_label_name (abfd, name)
5489 bfd *abfd ATTRIBUTE_UNUSED;
5490 const char *name;
5492 /* Normal local symbols start with ``.L''. */
5493 if (name[0] == '.' && name[1] == 'L')
5494 return true;
5496 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5497 DWARF debugging symbols starting with ``..''. */
5498 if (name[0] == '.' && name[1] == '.')
5499 return true;
5501 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5502 emitting DWARF debugging output. I suspect this is actually a
5503 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5504 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5505 underscore to be emitted on some ELF targets). For ease of use,
5506 we treat such symbols as local. */
5507 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5508 return true;
5510 return false;
5513 alent *
5514 _bfd_elf_get_lineno (ignore_abfd, symbol)
5515 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5516 asymbol *symbol ATTRIBUTE_UNUSED;
5518 abort ();
5519 return NULL;
5522 boolean
5523 _bfd_elf_set_arch_mach (abfd, arch, machine)
5524 bfd *abfd;
5525 enum bfd_architecture arch;
5526 unsigned long machine;
5528 /* If this isn't the right architecture for this backend, and this
5529 isn't the generic backend, fail. */
5530 if (arch != get_elf_backend_data (abfd)->arch
5531 && arch != bfd_arch_unknown
5532 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5533 return false;
5535 return bfd_default_set_arch_mach (abfd, arch, machine);
5538 /* Find the function to a particular section and offset,
5539 for error reporting. */
5541 static boolean
5542 elf_find_function (abfd, section, symbols, offset,
5543 filename_ptr, functionname_ptr)
5544 bfd *abfd ATTRIBUTE_UNUSED;
5545 asection *section;
5546 asymbol **symbols;
5547 bfd_vma offset;
5548 const char **filename_ptr;
5549 const char **functionname_ptr;
5551 const char *filename;
5552 asymbol *func;
5553 bfd_vma low_func;
5554 asymbol **p;
5556 filename = NULL;
5557 func = NULL;
5558 low_func = 0;
5560 for (p = symbols; *p != NULL; p++)
5562 elf_symbol_type *q;
5564 q = (elf_symbol_type *) *p;
5566 if (bfd_get_section (&q->symbol) != section)
5567 continue;
5569 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5571 default:
5572 break;
5573 case STT_FILE:
5574 filename = bfd_asymbol_name (&q->symbol);
5575 break;
5576 case STT_NOTYPE:
5577 case STT_FUNC:
5578 if (q->symbol.section == section
5579 && q->symbol.value >= low_func
5580 && q->symbol.value <= offset)
5582 func = (asymbol *) q;
5583 low_func = q->symbol.value;
5585 break;
5589 if (func == NULL)
5590 return false;
5592 if (filename_ptr)
5593 *filename_ptr = filename;
5594 if (functionname_ptr)
5595 *functionname_ptr = bfd_asymbol_name (func);
5597 return true;
5600 /* Find the nearest line to a particular section and offset,
5601 for error reporting. */
5603 boolean
5604 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5605 filename_ptr, functionname_ptr, line_ptr)
5606 bfd *abfd;
5607 asection *section;
5608 asymbol **symbols;
5609 bfd_vma offset;
5610 const char **filename_ptr;
5611 const char **functionname_ptr;
5612 unsigned int *line_ptr;
5614 boolean found;
5616 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5617 filename_ptr, functionname_ptr,
5618 line_ptr))
5620 if (!*functionname_ptr)
5621 elf_find_function (abfd, section, symbols, offset,
5622 *filename_ptr ? NULL : filename_ptr,
5623 functionname_ptr);
5625 return true;
5628 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5629 filename_ptr, functionname_ptr,
5630 line_ptr, 0,
5631 &elf_tdata (abfd)->dwarf2_find_line_info))
5633 if (!*functionname_ptr)
5634 elf_find_function (abfd, section, symbols, offset,
5635 *filename_ptr ? NULL : filename_ptr,
5636 functionname_ptr);
5638 return true;
5641 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5642 &found, filename_ptr,
5643 functionname_ptr, line_ptr,
5644 &elf_tdata (abfd)->line_info))
5645 return false;
5646 if (found)
5647 return true;
5649 if (symbols == NULL)
5650 return false;
5652 if (! elf_find_function (abfd, section, symbols, offset,
5653 filename_ptr, functionname_ptr))
5654 return false;
5656 *line_ptr = 0;
5657 return true;
5661 _bfd_elf_sizeof_headers (abfd, reloc)
5662 bfd *abfd;
5663 boolean reloc;
5665 int ret;
5667 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5668 if (! reloc)
5669 ret += get_program_header_size (abfd);
5670 return ret;
5673 boolean
5674 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5675 bfd *abfd;
5676 sec_ptr section;
5677 PTR location;
5678 file_ptr offset;
5679 bfd_size_type count;
5681 Elf_Internal_Shdr *hdr;
5682 bfd_signed_vma pos;
5684 if (! abfd->output_has_begun
5685 && ! _bfd_elf_compute_section_file_positions
5686 (abfd, (struct bfd_link_info *) NULL))
5687 return false;
5689 hdr = &elf_section_data (section)->this_hdr;
5690 pos = hdr->sh_offset + offset;
5691 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5692 || bfd_bwrite (location, count, abfd) != count)
5693 return false;
5695 return true;
5698 void
5699 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5700 bfd *abfd ATTRIBUTE_UNUSED;
5701 arelent *cache_ptr ATTRIBUTE_UNUSED;
5702 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5704 abort ();
5707 #if 0
5708 void
5709 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5710 bfd *abfd;
5711 arelent *cache_ptr;
5712 Elf_Internal_Rel *dst;
5714 abort ();
5716 #endif
5718 /* Try to convert a non-ELF reloc into an ELF one. */
5720 boolean
5721 _bfd_elf_validate_reloc (abfd, areloc)
5722 bfd *abfd;
5723 arelent *areloc;
5725 /* Check whether we really have an ELF howto. */
5727 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5729 bfd_reloc_code_real_type code;
5730 reloc_howto_type *howto;
5732 /* Alien reloc: Try to determine its type to replace it with an
5733 equivalent ELF reloc. */
5735 if (areloc->howto->pc_relative)
5737 switch (areloc->howto->bitsize)
5739 case 8:
5740 code = BFD_RELOC_8_PCREL;
5741 break;
5742 case 12:
5743 code = BFD_RELOC_12_PCREL;
5744 break;
5745 case 16:
5746 code = BFD_RELOC_16_PCREL;
5747 break;
5748 case 24:
5749 code = BFD_RELOC_24_PCREL;
5750 break;
5751 case 32:
5752 code = BFD_RELOC_32_PCREL;
5753 break;
5754 case 64:
5755 code = BFD_RELOC_64_PCREL;
5756 break;
5757 default:
5758 goto fail;
5761 howto = bfd_reloc_type_lookup (abfd, code);
5763 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5765 if (howto->pcrel_offset)
5766 areloc->addend += areloc->address;
5767 else
5768 areloc->addend -= areloc->address; /* addend is unsigned!! */
5771 else
5773 switch (areloc->howto->bitsize)
5775 case 8:
5776 code = BFD_RELOC_8;
5777 break;
5778 case 14:
5779 code = BFD_RELOC_14;
5780 break;
5781 case 16:
5782 code = BFD_RELOC_16;
5783 break;
5784 case 26:
5785 code = BFD_RELOC_26;
5786 break;
5787 case 32:
5788 code = BFD_RELOC_32;
5789 break;
5790 case 64:
5791 code = BFD_RELOC_64;
5792 break;
5793 default:
5794 goto fail;
5797 howto = bfd_reloc_type_lookup (abfd, code);
5800 if (howto)
5801 areloc->howto = howto;
5802 else
5803 goto fail;
5806 return true;
5808 fail:
5809 (*_bfd_error_handler)
5810 (_("%s: unsupported relocation type %s"),
5811 bfd_archive_filename (abfd), areloc->howto->name);
5812 bfd_set_error (bfd_error_bad_value);
5813 return false;
5816 boolean
5817 _bfd_elf_close_and_cleanup (abfd)
5818 bfd *abfd;
5820 if (bfd_get_format (abfd) == bfd_object)
5822 if (elf_shstrtab (abfd) != NULL)
5823 _bfd_elf_strtab_free (elf_shstrtab (abfd));
5826 return _bfd_generic_close_and_cleanup (abfd);
5829 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5830 in the relocation's offset. Thus we cannot allow any sort of sanity
5831 range-checking to interfere. There is nothing else to do in processing
5832 this reloc. */
5834 bfd_reloc_status_type
5835 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5836 bfd *abfd ATTRIBUTE_UNUSED;
5837 arelent *re ATTRIBUTE_UNUSED;
5838 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5839 PTR data ATTRIBUTE_UNUSED;
5840 asection *is ATTRIBUTE_UNUSED;
5841 bfd *obfd ATTRIBUTE_UNUSED;
5842 char **errmsg ATTRIBUTE_UNUSED;
5844 return bfd_reloc_ok;
5847 /* Elf core file support. Much of this only works on native
5848 toolchains, since we rely on knowing the
5849 machine-dependent procfs structure in order to pick
5850 out details about the corefile. */
5852 #ifdef HAVE_SYS_PROCFS_H
5853 # include <sys/procfs.h>
5854 #endif
5856 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5858 static int
5859 elfcore_make_pid (abfd)
5860 bfd *abfd;
5862 return ((elf_tdata (abfd)->core_lwpid << 16)
5863 + (elf_tdata (abfd)->core_pid));
5866 /* If there isn't a section called NAME, make one, using
5867 data from SECT. Note, this function will generate a
5868 reference to NAME, so you shouldn't deallocate or
5869 overwrite it. */
5871 static boolean
5872 elfcore_maybe_make_sect (abfd, name, sect)
5873 bfd *abfd;
5874 char *name;
5875 asection *sect;
5877 asection *sect2;
5879 if (bfd_get_section_by_name (abfd, name) != NULL)
5880 return true;
5882 sect2 = bfd_make_section (abfd, name);
5883 if (sect2 == NULL)
5884 return false;
5886 sect2->_raw_size = sect->_raw_size;
5887 sect2->filepos = sect->filepos;
5888 sect2->flags = sect->flags;
5889 sect2->alignment_power = sect->alignment_power;
5890 return true;
5893 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5894 actually creates up to two pseudosections:
5895 - For the single-threaded case, a section named NAME, unless
5896 such a section already exists.
5897 - For the multi-threaded case, a section named "NAME/PID", where
5898 PID is elfcore_make_pid (abfd).
5899 Both pseudosections have identical contents. */
5900 boolean
5901 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5902 bfd *abfd;
5903 char *name;
5904 size_t size;
5905 ufile_ptr filepos;
5907 char buf[100];
5908 char *threaded_name;
5909 asection *sect;
5911 /* Build the section name. */
5913 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5914 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
5915 if (threaded_name == NULL)
5916 return false;
5917 strcpy (threaded_name, buf);
5919 sect = bfd_make_section (abfd, threaded_name);
5920 if (sect == NULL)
5921 return false;
5922 sect->_raw_size = size;
5923 sect->filepos = filepos;
5924 sect->flags = SEC_HAS_CONTENTS;
5925 sect->alignment_power = 2;
5927 return elfcore_maybe_make_sect (abfd, name, sect);
5930 /* prstatus_t exists on:
5931 solaris 2.5+
5932 linux 2.[01] + glibc
5933 unixware 4.2
5936 #if defined (HAVE_PRSTATUS_T)
5937 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5939 static boolean
5940 elfcore_grok_prstatus (abfd, note)
5941 bfd *abfd;
5942 Elf_Internal_Note *note;
5944 size_t raw_size;
5945 int offset;
5947 if (note->descsz == sizeof (prstatus_t))
5949 prstatus_t prstat;
5951 raw_size = sizeof (prstat.pr_reg);
5952 offset = offsetof (prstatus_t, pr_reg);
5953 memcpy (&prstat, note->descdata, sizeof (prstat));
5955 /* Do not overwrite the core signal if it
5956 has already been set by another thread. */
5957 if (elf_tdata (abfd)->core_signal == 0)
5958 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5959 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5961 /* pr_who exists on:
5962 solaris 2.5+
5963 unixware 4.2
5964 pr_who doesn't exist on:
5965 linux 2.[01]
5967 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5968 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5969 #endif
5971 #if defined (HAVE_PRSTATUS32_T)
5972 else if (note->descsz == sizeof (prstatus32_t))
5974 /* 64-bit host, 32-bit corefile */
5975 prstatus32_t prstat;
5977 raw_size = sizeof (prstat.pr_reg);
5978 offset = offsetof (prstatus32_t, pr_reg);
5979 memcpy (&prstat, note->descdata, sizeof (prstat));
5981 /* Do not overwrite the core signal if it
5982 has already been set by another thread. */
5983 if (elf_tdata (abfd)->core_signal == 0)
5984 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5985 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5987 /* pr_who exists on:
5988 solaris 2.5+
5989 unixware 4.2
5990 pr_who doesn't exist on:
5991 linux 2.[01]
5993 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5994 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5995 #endif
5997 #endif /* HAVE_PRSTATUS32_T */
5998 else
6000 /* Fail - we don't know how to handle any other
6001 note size (ie. data object type). */
6002 return true;
6005 /* Make a ".reg/999" section and a ".reg" section. */
6006 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6007 raw_size, note->descpos + offset);
6009 #endif /* defined (HAVE_PRSTATUS_T) */
6011 /* Create a pseudosection containing the exact contents of NOTE. */
6012 static boolean
6013 elfcore_make_note_pseudosection (abfd, name, note)
6014 bfd *abfd;
6015 char *name;
6016 Elf_Internal_Note *note;
6018 return _bfd_elfcore_make_pseudosection (abfd, name,
6019 note->descsz, note->descpos);
6022 /* There isn't a consistent prfpregset_t across platforms,
6023 but it doesn't matter, because we don't have to pick this
6024 data structure apart. */
6026 static boolean
6027 elfcore_grok_prfpreg (abfd, note)
6028 bfd *abfd;
6029 Elf_Internal_Note *note;
6031 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6034 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6035 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6036 literally. */
6038 static boolean
6039 elfcore_grok_prxfpreg (abfd, note)
6040 bfd *abfd;
6041 Elf_Internal_Note *note;
6043 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6046 #if defined (HAVE_PRPSINFO_T)
6047 typedef prpsinfo_t elfcore_psinfo_t;
6048 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6049 typedef prpsinfo32_t elfcore_psinfo32_t;
6050 #endif
6051 #endif
6053 #if defined (HAVE_PSINFO_T)
6054 typedef psinfo_t elfcore_psinfo_t;
6055 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6056 typedef psinfo32_t elfcore_psinfo32_t;
6057 #endif
6058 #endif
6060 /* return a malloc'ed copy of a string at START which is at
6061 most MAX bytes long, possibly without a terminating '\0'.
6062 the copy will always have a terminating '\0'. */
6064 char *
6065 _bfd_elfcore_strndup (abfd, start, max)
6066 bfd *abfd;
6067 char *start;
6068 size_t max;
6070 char *dups;
6071 char *end = memchr (start, '\0', max);
6072 size_t len;
6074 if (end == NULL)
6075 len = max;
6076 else
6077 len = end - start;
6079 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6080 if (dups == NULL)
6081 return NULL;
6083 memcpy (dups, start, len);
6084 dups[len] = '\0';
6086 return dups;
6089 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6090 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
6092 static boolean
6093 elfcore_grok_psinfo (abfd, note)
6094 bfd *abfd;
6095 Elf_Internal_Note *note;
6097 if (note->descsz == sizeof (elfcore_psinfo_t))
6099 elfcore_psinfo_t psinfo;
6101 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6103 elf_tdata (abfd)->core_program
6104 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6105 sizeof (psinfo.pr_fname));
6107 elf_tdata (abfd)->core_command
6108 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6109 sizeof (psinfo.pr_psargs));
6111 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6112 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6114 /* 64-bit host, 32-bit corefile */
6115 elfcore_psinfo32_t psinfo;
6117 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6119 elf_tdata (abfd)->core_program
6120 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6121 sizeof (psinfo.pr_fname));
6123 elf_tdata (abfd)->core_command
6124 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6125 sizeof (psinfo.pr_psargs));
6127 #endif
6129 else
6131 /* Fail - we don't know how to handle any other
6132 note size (ie. data object type). */
6133 return true;
6136 /* Note that for some reason, a spurious space is tacked
6137 onto the end of the args in some (at least one anyway)
6138 implementations, so strip it off if it exists. */
6141 char *command = elf_tdata (abfd)->core_command;
6142 int n = strlen (command);
6144 if (0 < n && command[n - 1] == ' ')
6145 command[n - 1] = '\0';
6148 return true;
6150 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6152 #if defined (HAVE_PSTATUS_T)
6153 static boolean
6154 elfcore_grok_pstatus (abfd, note)
6155 bfd *abfd;
6156 Elf_Internal_Note *note;
6158 if (note->descsz == sizeof (pstatus_t)
6159 #if defined (HAVE_PXSTATUS_T)
6160 || note->descsz == sizeof (pxstatus_t)
6161 #endif
6164 pstatus_t pstat;
6166 memcpy (&pstat, note->descdata, sizeof (pstat));
6168 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6170 #if defined (HAVE_PSTATUS32_T)
6171 else if (note->descsz == sizeof (pstatus32_t))
6173 /* 64-bit host, 32-bit corefile */
6174 pstatus32_t pstat;
6176 memcpy (&pstat, note->descdata, sizeof (pstat));
6178 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6180 #endif
6181 /* Could grab some more details from the "representative"
6182 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6183 NT_LWPSTATUS note, presumably. */
6185 return true;
6187 #endif /* defined (HAVE_PSTATUS_T) */
6189 #if defined (HAVE_LWPSTATUS_T)
6190 static boolean
6191 elfcore_grok_lwpstatus (abfd, note)
6192 bfd *abfd;
6193 Elf_Internal_Note *note;
6195 lwpstatus_t lwpstat;
6196 char buf[100];
6197 char *name;
6198 asection *sect;
6200 if (note->descsz != sizeof (lwpstat)
6201 #if defined (HAVE_LWPXSTATUS_T)
6202 && note->descsz != sizeof (lwpxstatus_t)
6203 #endif
6205 return true;
6207 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6209 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6210 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6212 /* Make a ".reg/999" section. */
6214 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6215 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6216 if (name == NULL)
6217 return false;
6218 strcpy (name, buf);
6220 sect = bfd_make_section (abfd, name);
6221 if (sect == NULL)
6222 return false;
6224 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6225 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6226 sect->filepos = note->descpos
6227 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6228 #endif
6230 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6231 sect->_raw_size = sizeof (lwpstat.pr_reg);
6232 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6233 #endif
6235 sect->flags = SEC_HAS_CONTENTS;
6236 sect->alignment_power = 2;
6238 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6239 return false;
6241 /* Make a ".reg2/999" section */
6243 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6244 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6245 if (name == NULL)
6246 return false;
6247 strcpy (name, buf);
6249 sect = bfd_make_section (abfd, name);
6250 if (sect == NULL)
6251 return false;
6253 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6254 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6255 sect->filepos = note->descpos
6256 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6257 #endif
6259 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6260 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6261 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6262 #endif
6264 sect->flags = SEC_HAS_CONTENTS;
6265 sect->alignment_power = 2;
6267 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6269 #endif /* defined (HAVE_LWPSTATUS_T) */
6271 #if defined (HAVE_WIN32_PSTATUS_T)
6272 static boolean
6273 elfcore_grok_win32pstatus (abfd, note)
6274 bfd *abfd;
6275 Elf_Internal_Note *note;
6277 char buf[30];
6278 char *name;
6279 asection *sect;
6280 win32_pstatus_t pstatus;
6282 if (note->descsz < sizeof (pstatus))
6283 return true;
6285 memcpy (&pstatus, note->descdata, note->descsz);
6287 switch (pstatus.data_type)
6289 case NOTE_INFO_PROCESS:
6290 /* FIXME: need to add ->core_command. */
6291 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6292 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6293 break;
6295 case NOTE_INFO_THREAD:
6296 /* Make a ".reg/999" section. */
6297 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6299 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6300 if (name == NULL)
6301 return false;
6303 strcpy (name, buf);
6305 sect = bfd_make_section (abfd, name);
6306 if (sect == NULL)
6307 return false;
6309 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6310 sect->filepos = (note->descpos
6311 + offsetof (struct win32_pstatus,
6312 data.thread_info.thread_context));
6313 sect->flags = SEC_HAS_CONTENTS;
6314 sect->alignment_power = 2;
6316 if (pstatus.data.thread_info.is_active_thread)
6317 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6318 return false;
6319 break;
6321 case NOTE_INFO_MODULE:
6322 /* Make a ".module/xxxxxxxx" section. */
6323 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6325 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6326 if (name == NULL)
6327 return false;
6329 strcpy (name, buf);
6331 sect = bfd_make_section (abfd, name);
6333 if (sect == NULL)
6334 return false;
6336 sect->_raw_size = note->descsz;
6337 sect->filepos = note->descpos;
6338 sect->flags = SEC_HAS_CONTENTS;
6339 sect->alignment_power = 2;
6340 break;
6342 default:
6343 return true;
6346 return true;
6348 #endif /* HAVE_WIN32_PSTATUS_T */
6350 static boolean
6351 elfcore_grok_note (abfd, note)
6352 bfd *abfd;
6353 Elf_Internal_Note *note;
6355 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6357 switch (note->type)
6359 default:
6360 return true;
6362 case NT_PRSTATUS:
6363 if (bed->elf_backend_grok_prstatus)
6364 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6365 return true;
6366 #if defined (HAVE_PRSTATUS_T)
6367 return elfcore_grok_prstatus (abfd, note);
6368 #else
6369 return true;
6370 #endif
6372 #if defined (HAVE_PSTATUS_T)
6373 case NT_PSTATUS:
6374 return elfcore_grok_pstatus (abfd, note);
6375 #endif
6377 #if defined (HAVE_LWPSTATUS_T)
6378 case NT_LWPSTATUS:
6379 return elfcore_grok_lwpstatus (abfd, note);
6380 #endif
6382 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6383 return elfcore_grok_prfpreg (abfd, note);
6385 #if defined (HAVE_WIN32_PSTATUS_T)
6386 case NT_WIN32PSTATUS:
6387 return elfcore_grok_win32pstatus (abfd, note);
6388 #endif
6390 case NT_PRXFPREG: /* Linux SSE extension */
6391 if (note->namesz == 5
6392 && ! strcmp (note->namedata, "LINUX"))
6393 return elfcore_grok_prxfpreg (abfd, note);
6394 else
6395 return true;
6397 case NT_PRPSINFO:
6398 case NT_PSINFO:
6399 if (bed->elf_backend_grok_psinfo)
6400 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6401 return true;
6402 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6403 return elfcore_grok_psinfo (abfd, note);
6404 #else
6405 return true;
6406 #endif
6410 static boolean
6411 elfcore_netbsd_get_lwpid (note, lwpidp)
6412 Elf_Internal_Note *note;
6413 int *lwpidp;
6415 char *cp;
6417 cp = strchr (note->namedata, '@');
6418 if (cp != NULL)
6420 *lwpidp = atoi(cp);
6421 return true;
6423 return false;
6426 static boolean
6427 elfcore_grok_netbsd_procinfo (abfd, note)
6428 bfd *abfd;
6429 Elf_Internal_Note *note;
6432 /* Signal number at offset 0x08. */
6433 elf_tdata (abfd)->core_signal
6434 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6436 /* Process ID at offset 0x50. */
6437 elf_tdata (abfd)->core_pid
6438 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6440 /* Command name at 0x7c (max 32 bytes, including nul). */
6441 elf_tdata (abfd)->core_command
6442 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6444 return true;
6447 static boolean
6448 elfcore_grok_netbsd_note (abfd, note)
6449 bfd *abfd;
6450 Elf_Internal_Note *note;
6452 int lwp;
6454 if (elfcore_netbsd_get_lwpid (note, &lwp))
6455 elf_tdata (abfd)->core_lwpid = lwp;
6457 if (note->type == NT_NETBSDCORE_PROCINFO)
6459 /* NetBSD-specific core "procinfo". Note that we expect to
6460 find this note before any of the others, which is fine,
6461 since the kernel writes this note out first when it
6462 creates a core file. */
6464 return elfcore_grok_netbsd_procinfo (abfd, note);
6467 /* As of Jan 2002 there are no other machine-independent notes
6468 defined for NetBSD core files. If the note type is less
6469 than the start of the machine-dependent note types, we don't
6470 understand it. */
6472 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6473 return true;
6476 switch (bfd_get_arch (abfd))
6478 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6479 PT_GETFPREGS == mach+2. */
6481 case bfd_arch_alpha:
6482 case bfd_arch_sparc:
6483 switch (note->type)
6485 case NT_NETBSDCORE_FIRSTMACH+0:
6486 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6488 case NT_NETBSDCORE_FIRSTMACH+2:
6489 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6491 default:
6492 return true;
6495 /* On all other arch's, PT_GETREGS == mach+1 and
6496 PT_GETFPREGS == mach+3. */
6498 default:
6499 switch (note->type)
6501 case NT_NETBSDCORE_FIRSTMACH+1:
6502 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6504 case NT_NETBSDCORE_FIRSTMACH+3:
6505 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6507 default:
6508 return true;
6511 /* NOTREACHED */
6514 /* Function: elfcore_write_note
6516 Inputs:
6517 buffer to hold note
6518 name of note
6519 type of note
6520 data for note
6521 size of data for note
6523 Return:
6524 End of buffer containing note. */
6526 char *
6527 elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6528 bfd *abfd;
6529 char *buf;
6530 int *bufsiz;
6531 char *name;
6532 int type;
6533 void *input;
6534 int size;
6536 Elf_External_Note *xnp;
6537 int namesz = strlen (name);
6538 int newspace = BFD_ALIGN (sizeof (Elf_External_Note) + size + namesz - 1, 4);
6539 char *p, *dest;
6541 p = realloc (buf, *bufsiz + newspace);
6542 dest = p + *bufsiz;
6543 *bufsiz += newspace;
6544 xnp = (Elf_External_Note *) dest;
6545 H_PUT_32 (abfd, namesz, xnp->namesz);
6546 H_PUT_32 (abfd, size, xnp->descsz);
6547 H_PUT_32 (abfd, type, xnp->type);
6548 strcpy (xnp->name, name);
6549 memcpy (xnp->name + BFD_ALIGN (namesz, 4), input, size);
6550 return p;
6553 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6554 char *
6555 elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6556 bfd *abfd;
6557 char *buf;
6558 int *bufsiz;
6559 char *fname;
6560 char *psargs;
6562 int note_type;
6563 char *note_name = "CORE";
6565 #if defined (HAVE_PSINFO_T)
6566 psinfo_t data;
6567 note_type = NT_PSINFO;
6568 #else
6569 prpsinfo_t data;
6570 note_type = NT_PRPSINFO;
6571 #endif
6573 memset (&data, 0, sizeof (data));
6574 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
6575 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
6576 return elfcore_write_note (abfd, buf, bufsiz,
6577 note_name, note_type, &data, sizeof (data));
6579 #endif /* PSINFO_T or PRPSINFO_T */
6581 #if defined (HAVE_PRSTATUS_T)
6582 char *
6583 elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6584 bfd *abfd;
6585 char *buf;
6586 int *bufsiz;
6587 long pid;
6588 int cursig;
6589 void *gregs;
6591 prstatus_t prstat;
6592 char *note_name = "CORE";
6594 memset (&prstat, 0, sizeof (prstat));
6595 prstat.pr_pid = pid;
6596 prstat.pr_cursig = cursig;
6597 memcpy (prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
6598 return elfcore_write_note (abfd, buf, bufsiz,
6599 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
6601 #endif /* HAVE_PRSTATUS_T */
6603 #if defined (HAVE_PSTATUS_T)
6604 char *
6605 elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6606 bfd *abfd;
6607 char *buf;
6608 int *bufsiz;
6609 long pid;
6610 int cursig;
6611 void *gregs;
6613 pstatus_t pstat;
6614 char *note_name = "CORE";
6616 memset (&pstat, 0, sizeof (prstat));
6617 pstat.pr_pid = pid;
6618 memcpy (pstat.pr_reg, gregs, sizeof (pstat.pr_reg));
6619 return elfcore_write_note (abfd, buf, bufsiz,
6620 note_name, NT_PSTATUS, &pstat, sizeof (pstat));
6622 #endif /* HAVE_PSTATUS_T */
6624 char *
6625 elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
6626 bfd *abfd;
6627 char *buf;
6628 int *bufsiz;
6629 void *fpregs;
6630 int size;
6632 char *note_name = "CORE";
6633 return elfcore_write_note (abfd, buf, bufsiz,
6634 note_name, NT_FPREGSET, fpregs, size);
6637 char *
6638 elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
6639 bfd *abfd;
6640 char *buf;
6641 int *bufsiz;
6642 void *xfpregs;
6643 int size;
6645 char *note_name = "LINUX";
6646 return elfcore_write_note (abfd, buf, bufsiz,
6647 note_name, NT_PRXFPREG, xfpregs, size);
6650 static boolean
6651 elfcore_read_notes (abfd, offset, size)
6652 bfd *abfd;
6653 file_ptr offset;
6654 bfd_size_type size;
6656 char *buf;
6657 char *p;
6659 if (size <= 0)
6660 return true;
6662 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
6663 return false;
6665 buf = bfd_malloc (size);
6666 if (buf == NULL)
6667 return false;
6669 if (bfd_bread (buf, size, abfd) != size)
6671 error:
6672 free (buf);
6673 return false;
6676 p = buf;
6677 while (p < buf + size)
6679 /* FIXME: bad alignment assumption. */
6680 Elf_External_Note *xnp = (Elf_External_Note *) p;
6681 Elf_Internal_Note in;
6683 in.type = H_GET_32 (abfd, xnp->type);
6685 in.namesz = H_GET_32 (abfd, xnp->namesz);
6686 in.namedata = xnp->name;
6688 in.descsz = H_GET_32 (abfd, xnp->descsz);
6689 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
6690 in.descpos = offset + (in.descdata - buf);
6692 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
6694 if (! elfcore_grok_netbsd_note (abfd, &in))
6695 goto error;
6697 else
6699 if (! elfcore_grok_note (abfd, &in))
6700 goto error;
6703 p = in.descdata + BFD_ALIGN (in.descsz, 4);
6706 free (buf);
6707 return true;
6710 /* Providing external access to the ELF program header table. */
6712 /* Return an upper bound on the number of bytes required to store a
6713 copy of ABFD's program header table entries. Return -1 if an error
6714 occurs; bfd_get_error will return an appropriate code. */
6716 long
6717 bfd_get_elf_phdr_upper_bound (abfd)
6718 bfd *abfd;
6720 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6722 bfd_set_error (bfd_error_wrong_format);
6723 return -1;
6726 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
6729 /* Copy ABFD's program header table entries to *PHDRS. The entries
6730 will be stored as an array of Elf_Internal_Phdr structures, as
6731 defined in include/elf/internal.h. To find out how large the
6732 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6734 Return the number of program header table entries read, or -1 if an
6735 error occurs; bfd_get_error will return an appropriate code. */
6738 bfd_get_elf_phdrs (abfd, phdrs)
6739 bfd *abfd;
6740 void *phdrs;
6742 int num_phdrs;
6744 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6746 bfd_set_error (bfd_error_wrong_format);
6747 return -1;
6750 num_phdrs = elf_elfheader (abfd)->e_phnum;
6751 memcpy (phdrs, elf_tdata (abfd)->phdr,
6752 num_phdrs * sizeof (Elf_Internal_Phdr));
6754 return num_phdrs;
6757 void
6758 _bfd_elf_sprintf_vma (abfd, buf, value)
6759 bfd *abfd ATTRIBUTE_UNUSED;
6760 char *buf;
6761 bfd_vma value;
6763 #ifdef BFD64
6764 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6766 i_ehdrp = elf_elfheader (abfd);
6767 if (i_ehdrp == NULL)
6768 sprintf_vma (buf, value);
6769 else
6771 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6773 #if BFD_HOST_64BIT_LONG
6774 sprintf (buf, "%016lx", value);
6775 #else
6776 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6777 _bfd_int64_low (value));
6778 #endif
6780 else
6781 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6783 #else
6784 sprintf_vma (buf, value);
6785 #endif
6788 void
6789 _bfd_elf_fprintf_vma (abfd, stream, value)
6790 bfd *abfd ATTRIBUTE_UNUSED;
6791 PTR stream;
6792 bfd_vma value;
6794 #ifdef BFD64
6795 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6797 i_ehdrp = elf_elfheader (abfd);
6798 if (i_ehdrp == NULL)
6799 fprintf_vma ((FILE *) stream, value);
6800 else
6802 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6804 #if BFD_HOST_64BIT_LONG
6805 fprintf ((FILE *) stream, "%016lx", value);
6806 #else
6807 fprintf ((FILE *) stream, "%08lx%08lx",
6808 _bfd_int64_high (value), _bfd_int64_low (value));
6809 #endif
6811 else
6812 fprintf ((FILE *) stream, "%08lx",
6813 (unsigned long) (value & 0xffffffff));
6815 #else
6816 fprintf_vma ((FILE *) stream, value);
6817 #endif
6820 enum elf_reloc_type_class
6821 _bfd_elf_reloc_type_class (rela)
6822 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
6824 return reloc_class_normal;
6827 /* For RELA architectures, return what the relocation value for
6828 relocation against a local symbol. */
6830 bfd_vma
6831 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
6832 bfd *abfd;
6833 Elf_Internal_Sym *sym;
6834 asection *sec;
6835 Elf_Internal_Rela *rel;
6837 bfd_vma relocation;
6839 relocation = (sec->output_section->vma
6840 + sec->output_offset
6841 + sym->st_value);
6842 if ((sec->flags & SEC_MERGE)
6843 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
6844 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6846 asection *msec;
6848 msec = sec;
6849 rel->r_addend =
6850 _bfd_merged_section_offset (abfd, &msec,
6851 elf_section_data (sec)->sec_info,
6852 sym->st_value + rel->r_addend,
6853 (bfd_vma) 0)
6854 - relocation;
6855 rel->r_addend += msec->output_section->vma + msec->output_offset;
6857 return relocation;
6860 bfd_vma
6861 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
6862 bfd *abfd;
6863 Elf_Internal_Sym *sym;
6864 asection **psec;
6865 bfd_vma addend;
6867 asection *sec = *psec;
6869 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
6870 return sym->st_value + addend;
6872 return _bfd_merged_section_offset (abfd, psec,
6873 elf_section_data (sec)->sec_info,
6874 sym->st_value + addend, (bfd_vma) 0);
6877 bfd_vma
6878 _bfd_elf_section_offset (abfd, info, sec, offset)
6879 bfd *abfd;
6880 struct bfd_link_info *info;
6881 asection *sec;
6882 bfd_vma offset;
6884 struct bfd_elf_section_data *sec_data;
6886 sec_data = elf_section_data (sec);
6887 switch (sec_data->sec_info_type)
6889 case ELF_INFO_TYPE_STABS:
6890 return _bfd_stab_section_offset
6891 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
6892 offset);
6893 case ELF_INFO_TYPE_EH_FRAME:
6894 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
6895 default:
6896 return offset;