Support for more than 64k ELF sections.
[binutils.git] / bfd / elf.c
blobcf13ad60afe57894b5f4c0e7f160a5f5a573103e
1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
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 elf_fake_sections PARAMS ((bfd *, asection *, PTR));
57 static void set_group_contents PARAMS ((bfd *, asection *, PTR));
58 static boolean assign_section_numbers PARAMS ((bfd *));
59 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
60 static boolean elf_map_symbols PARAMS ((bfd *));
61 static bfd_size_type get_program_header_size PARAMS ((bfd *));
62 static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
63 static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
64 bfd_vma, const char **,
65 const char **));
66 static int elfcore_make_pid PARAMS ((bfd *));
67 static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
68 static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
69 Elf_Internal_Note *));
70 static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
71 static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
72 static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
74 /* Swap version information in and out. The version information is
75 currently size independent. If that ever changes, this code will
76 need to move into elfcode.h. */
78 /* Swap in a Verdef structure. */
80 void
81 _bfd_elf_swap_verdef_in (abfd, src, dst)
82 bfd *abfd;
83 const Elf_External_Verdef *src;
84 Elf_Internal_Verdef *dst;
86 dst->vd_version = H_GET_16 (abfd, src->vd_version);
87 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
88 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
89 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
90 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
91 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
92 dst->vd_next = H_GET_32 (abfd, src->vd_next);
95 /* Swap out a Verdef structure. */
97 void
98 _bfd_elf_swap_verdef_out (abfd, src, dst)
99 bfd *abfd;
100 const Elf_Internal_Verdef *src;
101 Elf_External_Verdef *dst;
103 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
104 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
105 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
106 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
107 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
108 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
109 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
112 /* Swap in a Verdaux structure. */
114 void
115 _bfd_elf_swap_verdaux_in (abfd, src, dst)
116 bfd *abfd;
117 const Elf_External_Verdaux *src;
118 Elf_Internal_Verdaux *dst;
120 dst->vda_name = H_GET_32 (abfd, src->vda_name);
121 dst->vda_next = H_GET_32 (abfd, src->vda_next);
124 /* Swap out a Verdaux structure. */
126 void
127 _bfd_elf_swap_verdaux_out (abfd, src, dst)
128 bfd *abfd;
129 const Elf_Internal_Verdaux *src;
130 Elf_External_Verdaux *dst;
132 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
133 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
136 /* Swap in a Verneed structure. */
138 void
139 _bfd_elf_swap_verneed_in (abfd, src, dst)
140 bfd *abfd;
141 const Elf_External_Verneed *src;
142 Elf_Internal_Verneed *dst;
144 dst->vn_version = H_GET_16 (abfd, src->vn_version);
145 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
146 dst->vn_file = H_GET_32 (abfd, src->vn_file);
147 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
148 dst->vn_next = H_GET_32 (abfd, src->vn_next);
151 /* Swap out a Verneed structure. */
153 void
154 _bfd_elf_swap_verneed_out (abfd, src, dst)
155 bfd *abfd;
156 const Elf_Internal_Verneed *src;
157 Elf_External_Verneed *dst;
159 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
160 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
161 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
162 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
163 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
166 /* Swap in a Vernaux structure. */
168 void
169 _bfd_elf_swap_vernaux_in (abfd, src, dst)
170 bfd *abfd;
171 const Elf_External_Vernaux *src;
172 Elf_Internal_Vernaux *dst;
174 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
175 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
176 dst->vna_other = H_GET_16 (abfd, src->vna_other);
177 dst->vna_name = H_GET_32 (abfd, src->vna_name);
178 dst->vna_next = H_GET_32 (abfd, src->vna_next);
181 /* Swap out a Vernaux structure. */
183 void
184 _bfd_elf_swap_vernaux_out (abfd, src, dst)
185 bfd *abfd;
186 const Elf_Internal_Vernaux *src;
187 Elf_External_Vernaux *dst;
189 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
190 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
191 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
192 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
193 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
196 /* Swap in a Versym structure. */
198 void
199 _bfd_elf_swap_versym_in (abfd, src, dst)
200 bfd *abfd;
201 const Elf_External_Versym *src;
202 Elf_Internal_Versym *dst;
204 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
207 /* Swap out a Versym structure. */
209 void
210 _bfd_elf_swap_versym_out (abfd, src, dst)
211 bfd *abfd;
212 const Elf_Internal_Versym *src;
213 Elf_External_Versym *dst;
215 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
218 /* Standard ELF hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
221 unsigned long
222 bfd_elf_hash (namearg)
223 const char *namearg;
225 const unsigned char *name = (const unsigned char *) namearg;
226 unsigned long h = 0;
227 unsigned long g;
228 int ch;
230 while ((ch = *name++) != '\0')
232 h = (h << 4) + ch;
233 if ((g = (h & 0xf0000000)) != 0)
235 h ^= g >> 24;
236 /* The ELF ABI says `h &= ~g', but this is equivalent in
237 this case and on some machines one insn instead of two. */
238 h ^= g;
241 return h;
244 /* Read a specified number of bytes at a specified offset in an ELF
245 file, into a newly allocated buffer, and return a pointer to the
246 buffer. */
248 static char *
249 elf_read (abfd, offset, size)
250 bfd *abfd;
251 file_ptr offset;
252 bfd_size_type size;
254 char *buf;
256 if ((buf = bfd_alloc (abfd, size)) == NULL)
257 return NULL;
258 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
259 return NULL;
260 if (bfd_bread ((PTR) buf, size, abfd) != size)
262 if (bfd_get_error () != bfd_error_system_call)
263 bfd_set_error (bfd_error_file_truncated);
264 return NULL;
266 return buf;
269 boolean
270 bfd_elf_mkobject (abfd)
271 bfd *abfd;
273 /* This just does initialization. */
274 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
275 bfd_size_type amt = sizeof (struct elf_obj_tdata);
276 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
277 if (elf_tdata (abfd) == 0)
278 return false;
279 /* Since everything is done at close time, do we need any
280 initialization? */
282 return true;
285 boolean
286 bfd_elf_mkcorefile (abfd)
287 bfd *abfd;
289 /* I think this can be done just like an object file. */
290 return bfd_elf_mkobject (abfd);
293 char *
294 bfd_elf_get_str_section (abfd, shindex)
295 bfd *abfd;
296 unsigned int shindex;
298 Elf_Internal_Shdr **i_shdrp;
299 char *shstrtab = NULL;
300 file_ptr offset;
301 bfd_size_type shstrtabsize;
303 i_shdrp = elf_elfsections (abfd);
304 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
305 return 0;
307 shstrtab = (char *) i_shdrp[shindex]->contents;
308 if (shstrtab == NULL)
310 /* No cached one, attempt to read, and cache what we read. */
311 offset = i_shdrp[shindex]->sh_offset;
312 shstrtabsize = i_shdrp[shindex]->sh_size;
313 shstrtab = elf_read (abfd, offset, shstrtabsize);
314 i_shdrp[shindex]->contents = (PTR) shstrtab;
316 return shstrtab;
319 char *
320 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
321 bfd *abfd;
322 unsigned int shindex;
323 unsigned int strindex;
325 Elf_Internal_Shdr *hdr;
327 if (strindex == 0)
328 return "";
330 hdr = elf_elfsections (abfd)[shindex];
332 if (hdr->contents == NULL
333 && bfd_elf_get_str_section (abfd, shindex) == NULL)
334 return NULL;
336 if (strindex >= hdr->sh_size)
338 (*_bfd_error_handler)
339 (_("%s: invalid string offset %u >= %lu for section `%s'"),
340 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
341 ((shindex == elf_elfheader(abfd)->e_shstrndx
342 && strindex == hdr->sh_name)
343 ? ".shstrtab"
344 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
345 return "";
348 return ((char *) hdr->contents) + strindex;
351 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
352 sections. The first element is the flags, the rest are section
353 pointers. */
355 typedef union elf_internal_group {
356 Elf_Internal_Shdr *shdr;
357 unsigned int flags;
358 } Elf_Internal_Group;
360 /* Set next_in_group list pointer, and group name for NEWSECT. */
362 static boolean
363 setup_group (abfd, hdr, newsect)
364 bfd *abfd;
365 Elf_Internal_Shdr *hdr;
366 asection *newsect;
368 unsigned int num_group = elf_tdata (abfd)->num_group;
370 /* If num_group is zero, read in all SHT_GROUP sections. The count
371 is set to -1 if there are no SHT_GROUP sections. */
372 if (num_group == 0)
374 unsigned int i, shnum;
376 /* First count the number of groups. If we have a SHT_GROUP
377 section with just a flag word (ie. sh_size is 4), ignore it. */
378 shnum = elf_numsections (abfd);
379 num_group = 0;
380 for (i = 0; i < shnum; i++)
382 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
383 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
384 num_group += 1;
387 if (num_group == 0)
388 num_group = (unsigned) -1;
389 elf_tdata (abfd)->num_group = num_group;
391 if (num_group > 0)
393 /* We keep a list of elf section headers for group sections,
394 so we can find them quickly. */
395 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
396 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
397 if (elf_tdata (abfd)->group_sect_ptr == NULL)
398 return false;
400 num_group = 0;
401 for (i = 0; i < shnum; i++)
403 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
404 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
406 unsigned char *src;
407 Elf_Internal_Group *dest;
409 /* Add to list of sections. */
410 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
411 num_group += 1;
413 /* Read the raw contents. */
414 BFD_ASSERT (sizeof (*dest) >= 4);
415 amt = shdr->sh_size * sizeof (*dest) / 4;
416 shdr->contents = bfd_alloc (abfd, amt);
417 if (shdr->contents == NULL
418 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
419 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
420 != shdr->sh_size))
421 return false;
423 /* Translate raw contents, a flag word followed by an
424 array of elf section indices all in target byte order,
425 to the flag word followed by an array of elf section
426 pointers. */
427 src = shdr->contents + shdr->sh_size;
428 dest = (Elf_Internal_Group *) (shdr->contents + amt);
429 while (1)
431 unsigned int idx;
433 src -= 4;
434 --dest;
435 idx = H_GET_32 (abfd, src);
436 if (src == shdr->contents)
438 dest->flags = idx;
439 break;
441 if (idx >= shnum)
443 ((*_bfd_error_handler)
444 (_("%s: invalid SHT_GROUP entry"),
445 bfd_archive_filename (abfd)));
446 idx = 0;
448 dest->shdr = elf_elfsections (abfd)[idx];
455 if (num_group != (unsigned) -1)
457 unsigned int i;
459 for (i = 0; i < num_group; i++)
461 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
462 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
463 unsigned int n_elt = shdr->sh_size / 4;
465 /* Look through this group's sections to see if current
466 section is a member. */
467 while (--n_elt != 0)
468 if ((++idx)->shdr == hdr)
470 asection *s = NULL;
472 /* We are a member of this group. Go looking through
473 other members to see if any others are linked via
474 next_in_group. */
475 idx = (Elf_Internal_Group *) shdr->contents;
476 n_elt = shdr->sh_size / 4;
477 while (--n_elt != 0)
478 if ((s = (++idx)->shdr->bfd_section) != NULL
479 && elf_next_in_group (s) != NULL)
480 break;
481 if (n_elt != 0)
483 /* Snarf the group name from other member, and
484 insert current section in circular list. */
485 elf_group_name (newsect) = elf_group_name (s);
486 elf_next_in_group (newsect) = elf_next_in_group (s);
487 elf_next_in_group (s) = newsect;
489 else
491 struct elf_backend_data *bed;
492 file_ptr pos;
493 unsigned char ename[4];
494 unsigned long iname;
495 const char *gname;
497 /* Humbug. Get the name from the group signature
498 symbol. Why isn't the signature just a string?
499 Fortunately, the name index is at the same
500 place in the external symbol for both 32 and 64
501 bit ELF. */
502 bed = get_elf_backend_data (abfd);
503 pos = elf_tdata (abfd)->symtab_hdr.sh_offset;
504 pos += shdr->sh_info * bed->s->sizeof_sym;
505 if (bfd_seek (abfd, pos, SEEK_SET) != 0
506 || bfd_bread (ename, (bfd_size_type) 4, abfd) != 4)
507 return false;
508 iname = H_GET_32 (abfd, ename);
509 gname = elf_string_from_elf_strtab (abfd, iname);
510 elf_group_name (newsect) = gname;
512 /* Start a circular list with one element. */
513 elf_next_in_group (newsect) = newsect;
515 if (shdr->bfd_section != NULL)
516 elf_next_in_group (shdr->bfd_section) = newsect;
517 i = num_group - 1;
518 break;
523 if (elf_group_name (newsect) == NULL)
525 (*_bfd_error_handler) (_("%s: no group info for section %s"),
526 bfd_archive_filename (abfd), newsect->name);
528 return true;
531 /* Make a BFD section from an ELF section. We store a pointer to the
532 BFD section in the bfd_section field of the header. */
534 boolean
535 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
536 bfd *abfd;
537 Elf_Internal_Shdr *hdr;
538 const char *name;
540 asection *newsect;
541 flagword flags;
542 struct elf_backend_data *bed;
544 if (hdr->bfd_section != NULL)
546 BFD_ASSERT (strcmp (name,
547 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
548 return true;
551 newsect = bfd_make_section_anyway (abfd, name);
552 if (newsect == NULL)
553 return false;
555 newsect->filepos = hdr->sh_offset;
557 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
558 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
559 || ! bfd_set_section_alignment (abfd, newsect,
560 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
561 return false;
563 flags = SEC_NO_FLAGS;
564 if (hdr->sh_type != SHT_NOBITS)
565 flags |= SEC_HAS_CONTENTS;
566 if (hdr->sh_type == SHT_GROUP)
567 flags |= SEC_GROUP | SEC_EXCLUDE;
568 if ((hdr->sh_flags & SHF_ALLOC) != 0)
570 flags |= SEC_ALLOC;
571 if (hdr->sh_type != SHT_NOBITS)
572 flags |= SEC_LOAD;
574 if ((hdr->sh_flags & SHF_WRITE) == 0)
575 flags |= SEC_READONLY;
576 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
577 flags |= SEC_CODE;
578 else if ((flags & SEC_LOAD) != 0)
579 flags |= SEC_DATA;
580 if ((hdr->sh_flags & SHF_MERGE) != 0)
582 flags |= SEC_MERGE;
583 newsect->entsize = hdr->sh_entsize;
584 if ((hdr->sh_flags & SHF_STRINGS) != 0)
585 flags |= SEC_STRINGS;
587 if (hdr->sh_flags & SHF_GROUP)
588 if (!setup_group (abfd, hdr, newsect))
589 return false;
591 /* The debugging sections appear to be recognized only by name, not
592 any sort of flag. */
594 static const char *debug_sec_names [] =
596 ".debug",
597 ".gnu.linkonce.wi.",
598 ".line",
599 ".stab"
601 int i;
603 for (i = ARRAY_SIZE (debug_sec_names); i--;)
604 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
605 break;
607 if (i >= 0)
608 flags |= SEC_DEBUGGING;
611 /* As a GNU extension, if the name begins with .gnu.linkonce, we
612 only link a single copy of the section. This is used to support
613 g++. g++ will emit each template expansion in its own section.
614 The symbols will be defined as weak, so that multiple definitions
615 are permitted. The GNU linker extension is to actually discard
616 all but one of the sections. */
617 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
618 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
620 bed = get_elf_backend_data (abfd);
621 if (bed->elf_backend_section_flags)
622 if (! bed->elf_backend_section_flags (&flags, hdr))
623 return false;
625 if (! bfd_set_section_flags (abfd, newsect, flags))
626 return false;
628 if ((flags & SEC_ALLOC) != 0)
630 Elf_Internal_Phdr *phdr;
631 unsigned int i;
633 /* Look through the phdrs to see if we need to adjust the lma.
634 If all the p_paddr fields are zero, we ignore them, since
635 some ELF linkers produce such output. */
636 phdr = elf_tdata (abfd)->phdr;
637 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
639 if (phdr->p_paddr != 0)
640 break;
642 if (i < elf_elfheader (abfd)->e_phnum)
644 phdr = elf_tdata (abfd)->phdr;
645 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
647 /* This section is part of this segment if its file
648 offset plus size lies within the segment's memory
649 span and, if the section is loaded, the extent of the
650 loaded data lies within the extent of the segment.
651 If the p_paddr field is not set, we don't alter the
652 LMA. */
653 if (phdr->p_type == PT_LOAD
654 && phdr->p_paddr
655 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
656 && (hdr->sh_offset + hdr->sh_size
657 <= phdr->p_offset + phdr->p_memsz)
658 && ((flags & SEC_LOAD) == 0
659 || (phdr->p_offset + phdr->p_filesz
660 >= hdr->sh_offset + hdr->sh_size)))
662 /* We used to do a relative adjustment here, but
663 that doesn't work if the segment is packed with
664 code from multiple VMAs. Instead we calculate
665 the LMA absoultely, based on the LMA of the
666 segment (it is assumed that the segment will
667 contain sections with contiguous LMAs, even if
668 the VMAs are not). */
669 newsect->lma = phdr->p_paddr
670 + hdr->sh_offset - phdr->p_offset;
671 break;
677 hdr->bfd_section = newsect;
678 elf_section_data (newsect)->this_hdr = *hdr;
680 return true;
684 INTERNAL_FUNCTION
685 bfd_elf_find_section
687 SYNOPSIS
688 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
690 DESCRIPTION
691 Helper functions for GDB to locate the string tables.
692 Since BFD hides string tables from callers, GDB needs to use an
693 internal hook to find them. Sun's .stabstr, in particular,
694 isn't even pointed to by the .stab section, so ordinary
695 mechanisms wouldn't work to find it, even if we had some.
698 struct elf_internal_shdr *
699 bfd_elf_find_section (abfd, name)
700 bfd *abfd;
701 char *name;
703 Elf_Internal_Shdr **i_shdrp;
704 char *shstrtab;
705 unsigned int max;
706 unsigned int i;
708 i_shdrp = elf_elfsections (abfd);
709 if (i_shdrp != NULL)
711 shstrtab = bfd_elf_get_str_section (abfd,
712 elf_elfheader (abfd)->e_shstrndx);
713 if (shstrtab != NULL)
715 max = elf_numsections (abfd);
716 for (i = 1; i < max; i++)
717 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
718 return i_shdrp[i];
721 return 0;
724 const char *const bfd_elf_section_type_names[] = {
725 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
726 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
727 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
730 /* ELF relocs are against symbols. If we are producing relocateable
731 output, and the reloc is against an external symbol, and nothing
732 has given us any additional addend, the resulting reloc will also
733 be against the same symbol. In such a case, we don't want to
734 change anything about the way the reloc is handled, since it will
735 all be done at final link time. Rather than put special case code
736 into bfd_perform_relocation, all the reloc types use this howto
737 function. It just short circuits the reloc if producing
738 relocateable output against an external symbol. */
740 bfd_reloc_status_type
741 bfd_elf_generic_reloc (abfd,
742 reloc_entry,
743 symbol,
744 data,
745 input_section,
746 output_bfd,
747 error_message)
748 bfd *abfd ATTRIBUTE_UNUSED;
749 arelent *reloc_entry;
750 asymbol *symbol;
751 PTR data ATTRIBUTE_UNUSED;
752 asection *input_section;
753 bfd *output_bfd;
754 char **error_message ATTRIBUTE_UNUSED;
756 if (output_bfd != (bfd *) NULL
757 && (symbol->flags & BSF_SECTION_SYM) == 0
758 && (! reloc_entry->howto->partial_inplace
759 || reloc_entry->addend == 0))
761 reloc_entry->address += input_section->output_offset;
762 return bfd_reloc_ok;
765 return bfd_reloc_continue;
768 /* Finish SHF_MERGE section merging. */
770 boolean
771 _bfd_elf_merge_sections (abfd, info)
772 bfd *abfd;
773 struct bfd_link_info *info;
775 if (!is_elf_hash_table (info))
776 return false;
777 if (elf_hash_table (info)->merge_info)
778 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info);
779 return true;
782 /* Print out the program headers. */
784 boolean
785 _bfd_elf_print_private_bfd_data (abfd, farg)
786 bfd *abfd;
787 PTR farg;
789 FILE *f = (FILE *) farg;
790 Elf_Internal_Phdr *p;
791 asection *s;
792 bfd_byte *dynbuf = NULL;
794 p = elf_tdata (abfd)->phdr;
795 if (p != NULL)
797 unsigned int i, c;
799 fprintf (f, _("\nProgram Header:\n"));
800 c = elf_elfheader (abfd)->e_phnum;
801 for (i = 0; i < c; i++, p++)
803 const char *pt;
804 char buf[20];
806 switch (p->p_type)
808 case PT_NULL: pt = "NULL"; break;
809 case PT_LOAD: pt = "LOAD"; break;
810 case PT_DYNAMIC: pt = "DYNAMIC"; break;
811 case PT_INTERP: pt = "INTERP"; break;
812 case PT_NOTE: pt = "NOTE"; break;
813 case PT_SHLIB: pt = "SHLIB"; break;
814 case PT_PHDR: pt = "PHDR"; break;
815 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
816 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
818 fprintf (f, "%8s off 0x", pt);
819 bfd_fprintf_vma (abfd, f, p->p_offset);
820 fprintf (f, " vaddr 0x");
821 bfd_fprintf_vma (abfd, f, p->p_vaddr);
822 fprintf (f, " paddr 0x");
823 bfd_fprintf_vma (abfd, f, p->p_paddr);
824 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
825 fprintf (f, " filesz 0x");
826 bfd_fprintf_vma (abfd, f, p->p_filesz);
827 fprintf (f, " memsz 0x");
828 bfd_fprintf_vma (abfd, f, p->p_memsz);
829 fprintf (f, " flags %c%c%c",
830 (p->p_flags & PF_R) != 0 ? 'r' : '-',
831 (p->p_flags & PF_W) != 0 ? 'w' : '-',
832 (p->p_flags & PF_X) != 0 ? 'x' : '-');
833 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
834 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
835 fprintf (f, "\n");
839 s = bfd_get_section_by_name (abfd, ".dynamic");
840 if (s != NULL)
842 int elfsec;
843 unsigned long shlink;
844 bfd_byte *extdyn, *extdynend;
845 size_t extdynsize;
846 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
848 fprintf (f, _("\nDynamic Section:\n"));
850 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
851 if (dynbuf == NULL)
852 goto error_return;
853 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
854 s->_raw_size))
855 goto error_return;
857 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
858 if (elfsec == -1)
859 goto error_return;
860 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
862 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
863 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
865 extdyn = dynbuf;
866 extdynend = extdyn + s->_raw_size;
867 for (; extdyn < extdynend; extdyn += extdynsize)
869 Elf_Internal_Dyn dyn;
870 const char *name;
871 char ab[20];
872 boolean stringp;
874 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
876 if (dyn.d_tag == DT_NULL)
877 break;
879 stringp = false;
880 switch (dyn.d_tag)
882 default:
883 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
884 name = ab;
885 break;
887 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
888 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
889 case DT_PLTGOT: name = "PLTGOT"; break;
890 case DT_HASH: name = "HASH"; break;
891 case DT_STRTAB: name = "STRTAB"; break;
892 case DT_SYMTAB: name = "SYMTAB"; break;
893 case DT_RELA: name = "RELA"; break;
894 case DT_RELASZ: name = "RELASZ"; break;
895 case DT_RELAENT: name = "RELAENT"; break;
896 case DT_STRSZ: name = "STRSZ"; break;
897 case DT_SYMENT: name = "SYMENT"; break;
898 case DT_INIT: name = "INIT"; break;
899 case DT_FINI: name = "FINI"; break;
900 case DT_SONAME: name = "SONAME"; stringp = true; break;
901 case DT_RPATH: name = "RPATH"; stringp = true; break;
902 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
903 case DT_REL: name = "REL"; break;
904 case DT_RELSZ: name = "RELSZ"; break;
905 case DT_RELENT: name = "RELENT"; break;
906 case DT_PLTREL: name = "PLTREL"; break;
907 case DT_DEBUG: name = "DEBUG"; break;
908 case DT_TEXTREL: name = "TEXTREL"; break;
909 case DT_JMPREL: name = "JMPREL"; break;
910 case DT_BIND_NOW: name = "BIND_NOW"; break;
911 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
912 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
913 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
914 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
915 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
916 case DT_FLAGS: name = "FLAGS"; break;
917 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
918 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
919 case DT_CHECKSUM: name = "CHECKSUM"; break;
920 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
921 case DT_MOVEENT: name = "MOVEENT"; break;
922 case DT_MOVESZ: name = "MOVESZ"; break;
923 case DT_FEATURE: name = "FEATURE"; break;
924 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
925 case DT_SYMINSZ: name = "SYMINSZ"; break;
926 case DT_SYMINENT: name = "SYMINENT"; break;
927 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
928 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
929 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
930 case DT_PLTPAD: name = "PLTPAD"; break;
931 case DT_MOVETAB: name = "MOVETAB"; break;
932 case DT_SYMINFO: name = "SYMINFO"; break;
933 case DT_RELACOUNT: name = "RELACOUNT"; break;
934 case DT_RELCOUNT: name = "RELCOUNT"; break;
935 case DT_FLAGS_1: name = "FLAGS_1"; break;
936 case DT_VERSYM: name = "VERSYM"; break;
937 case DT_VERDEF: name = "VERDEF"; break;
938 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
939 case DT_VERNEED: name = "VERNEED"; break;
940 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
941 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
942 case DT_USED: name = "USED"; break;
943 case DT_FILTER: name = "FILTER"; stringp = true; break;
946 fprintf (f, " %-11s ", name);
947 if (! stringp)
948 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
949 else
951 const char *string;
952 unsigned int tagv = dyn.d_un.d_val;
954 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
955 if (string == NULL)
956 goto error_return;
957 fprintf (f, "%s", string);
959 fprintf (f, "\n");
962 free (dynbuf);
963 dynbuf = NULL;
966 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
967 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
969 if (! _bfd_elf_slurp_version_tables (abfd))
970 return false;
973 if (elf_dynverdef (abfd) != 0)
975 Elf_Internal_Verdef *t;
977 fprintf (f, _("\nVersion definitions:\n"));
978 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
980 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
981 t->vd_flags, t->vd_hash, t->vd_nodename);
982 if (t->vd_auxptr->vda_nextptr != NULL)
984 Elf_Internal_Verdaux *a;
986 fprintf (f, "\t");
987 for (a = t->vd_auxptr->vda_nextptr;
988 a != NULL;
989 a = a->vda_nextptr)
990 fprintf (f, "%s ", a->vda_nodename);
991 fprintf (f, "\n");
996 if (elf_dynverref (abfd) != 0)
998 Elf_Internal_Verneed *t;
1000 fprintf (f, _("\nVersion References:\n"));
1001 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1003 Elf_Internal_Vernaux *a;
1005 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1006 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1007 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1008 a->vna_flags, a->vna_other, a->vna_nodename);
1012 return true;
1014 error_return:
1015 if (dynbuf != NULL)
1016 free (dynbuf);
1017 return false;
1020 /* Display ELF-specific fields of a symbol. */
1022 void
1023 bfd_elf_print_symbol (abfd, filep, symbol, how)
1024 bfd *abfd;
1025 PTR filep;
1026 asymbol *symbol;
1027 bfd_print_symbol_type how;
1029 FILE *file = (FILE *) filep;
1030 switch (how)
1032 case bfd_print_symbol_name:
1033 fprintf (file, "%s", symbol->name);
1034 break;
1035 case bfd_print_symbol_more:
1036 fprintf (file, "elf ");
1037 bfd_fprintf_vma (abfd, file, symbol->value);
1038 fprintf (file, " %lx", (long) symbol->flags);
1039 break;
1040 case bfd_print_symbol_all:
1042 const char *section_name;
1043 const char *name = NULL;
1044 struct elf_backend_data *bed;
1045 unsigned char st_other;
1046 bfd_vma val;
1048 section_name = symbol->section ? symbol->section->name : "(*none*)";
1050 bed = get_elf_backend_data (abfd);
1051 if (bed->elf_backend_print_symbol_all)
1052 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1054 if (name == NULL)
1056 name = symbol->name;
1057 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1060 fprintf (file, " %s\t", section_name);
1061 /* Print the "other" value for a symbol. For common symbols,
1062 we've already printed the size; now print the alignment.
1063 For other symbols, we have no specified alignment, and
1064 we've printed the address; now print the size. */
1065 if (bfd_is_com_section (symbol->section))
1066 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1067 else
1068 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1069 bfd_fprintf_vma (abfd, file, val);
1071 /* If we have version information, print it. */
1072 if (elf_tdata (abfd)->dynversym_section != 0
1073 && (elf_tdata (abfd)->dynverdef_section != 0
1074 || elf_tdata (abfd)->dynverref_section != 0))
1076 unsigned int vernum;
1077 const char *version_string;
1079 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1081 if (vernum == 0)
1082 version_string = "";
1083 else if (vernum == 1)
1084 version_string = "Base";
1085 else if (vernum <= elf_tdata (abfd)->cverdefs)
1086 version_string =
1087 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1088 else
1090 Elf_Internal_Verneed *t;
1092 version_string = "";
1093 for (t = elf_tdata (abfd)->verref;
1094 t != NULL;
1095 t = t->vn_nextref)
1097 Elf_Internal_Vernaux *a;
1099 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1101 if (a->vna_other == vernum)
1103 version_string = a->vna_nodename;
1104 break;
1110 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1111 fprintf (file, " %-11s", version_string);
1112 else
1114 int i;
1116 fprintf (file, " (%s)", version_string);
1117 for (i = 10 - strlen (version_string); i > 0; --i)
1118 putc (' ', file);
1122 /* If the st_other field is not zero, print it. */
1123 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1125 switch (st_other)
1127 case 0: break;
1128 case STV_INTERNAL: fprintf (file, " .internal"); break;
1129 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1130 case STV_PROTECTED: fprintf (file, " .protected"); break;
1131 default:
1132 /* Some other non-defined flags are also present, so print
1133 everything hex. */
1134 fprintf (file, " 0x%02x", (unsigned int) st_other);
1137 fprintf (file, " %s", name);
1139 break;
1143 /* Create an entry in an ELF linker hash table. */
1145 struct bfd_hash_entry *
1146 _bfd_elf_link_hash_newfunc (entry, table, string)
1147 struct bfd_hash_entry *entry;
1148 struct bfd_hash_table *table;
1149 const char *string;
1151 /* Allocate the structure if it has not already been allocated by a
1152 subclass. */
1153 if (entry == NULL)
1155 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1156 if (entry == NULL)
1157 return entry;
1160 /* Call the allocation method of the superclass. */
1161 entry = _bfd_link_hash_newfunc (entry, table, string);
1162 if (entry != NULL)
1164 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1165 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1167 /* Set local fields. */
1168 ret->indx = -1;
1169 ret->size = 0;
1170 ret->dynindx = -1;
1171 ret->dynstr_index = 0;
1172 ret->weakdef = NULL;
1173 ret->got.refcount = htab->init_refcount;
1174 ret->plt.refcount = htab->init_refcount;
1175 ret->linker_section_pointer = NULL;
1176 ret->verinfo.verdef = NULL;
1177 ret->vtable_entries_used = NULL;
1178 ret->vtable_entries_size = 0;
1179 ret->vtable_parent = NULL;
1180 ret->type = STT_NOTYPE;
1181 ret->other = 0;
1182 /* Assume that we have been called by a non-ELF symbol reader.
1183 This flag is then reset by the code which reads an ELF input
1184 file. This ensures that a symbol created by a non-ELF symbol
1185 reader will have the flag set correctly. */
1186 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1189 return entry;
1192 /* Copy data from an indirect symbol to its direct symbol, hiding the
1193 old indirect symbol. Also used for copying flags to a weakdef. */
1195 void
1196 _bfd_elf_link_hash_copy_indirect (dir, ind)
1197 struct elf_link_hash_entry *dir, *ind;
1199 bfd_signed_vma tmp;
1201 /* Copy down any references that we may have already seen to the
1202 symbol which just became indirect. */
1204 dir->elf_link_hash_flags |=
1205 (ind->elf_link_hash_flags
1206 & (ELF_LINK_HASH_REF_DYNAMIC
1207 | ELF_LINK_HASH_REF_REGULAR
1208 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1209 | ELF_LINK_NON_GOT_REF));
1211 if (ind->root.type != bfd_link_hash_indirect)
1212 return;
1214 /* Copy over the global and procedure linkage table refcount entries.
1215 These may have been already set up by a check_relocs routine. */
1216 tmp = dir->got.refcount;
1217 if (tmp <= 0)
1219 dir->got.refcount = ind->got.refcount;
1220 ind->got.refcount = tmp;
1222 else
1223 BFD_ASSERT (ind->got.refcount <= 0);
1225 tmp = dir->plt.refcount;
1226 if (tmp <= 0)
1228 dir->plt.refcount = ind->plt.refcount;
1229 ind->plt.refcount = tmp;
1231 else
1232 BFD_ASSERT (ind->plt.refcount <= 0);
1234 if (dir->dynindx == -1)
1236 dir->dynindx = ind->dynindx;
1237 dir->dynstr_index = ind->dynstr_index;
1238 ind->dynindx = -1;
1239 ind->dynstr_index = 0;
1241 else
1242 BFD_ASSERT (ind->dynindx == -1);
1245 void
1246 _bfd_elf_link_hash_hide_symbol (info, h)
1247 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1248 struct elf_link_hash_entry *h;
1250 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1251 h->plt.offset = (bfd_vma) -1;
1252 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1253 h->dynindx = -1;
1256 /* Initialize an ELF linker hash table. */
1258 boolean
1259 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1260 struct elf_link_hash_table *table;
1261 bfd *abfd;
1262 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1263 struct bfd_hash_table *,
1264 const char *));
1266 boolean ret;
1268 table->dynamic_sections_created = false;
1269 table->dynobj = NULL;
1270 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
1271 /* The first dynamic symbol is a dummy. */
1272 table->dynsymcount = 1;
1273 table->dynstr = NULL;
1274 table->bucketcount = 0;
1275 table->needed = NULL;
1276 table->runpath = NULL;
1277 table->hgot = NULL;
1278 table->stab_info = NULL;
1279 table->merge_info = NULL;
1280 table->dynlocal = NULL;
1281 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1282 table->root.type = bfd_link_elf_hash_table;
1284 return ret;
1287 /* Create an ELF linker hash table. */
1289 struct bfd_link_hash_table *
1290 _bfd_elf_link_hash_table_create (abfd)
1291 bfd *abfd;
1293 struct elf_link_hash_table *ret;
1294 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1296 ret = (struct elf_link_hash_table *) bfd_alloc (abfd, amt);
1297 if (ret == (struct elf_link_hash_table *) NULL)
1298 return NULL;
1300 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1302 bfd_release (abfd, ret);
1303 return NULL;
1306 return &ret->root;
1309 /* This is a hook for the ELF emulation code in the generic linker to
1310 tell the backend linker what file name to use for the DT_NEEDED
1311 entry for a dynamic object. The generic linker passes name as an
1312 empty string to indicate that no DT_NEEDED entry should be made. */
1314 void
1315 bfd_elf_set_dt_needed_name (abfd, name)
1316 bfd *abfd;
1317 const char *name;
1319 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1320 && bfd_get_format (abfd) == bfd_object)
1321 elf_dt_name (abfd) = name;
1324 void
1325 bfd_elf_set_dt_needed_soname (abfd, name)
1326 bfd *abfd;
1327 const char *name;
1329 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1330 && bfd_get_format (abfd) == bfd_object)
1331 elf_dt_soname (abfd) = name;
1334 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1335 the linker ELF emulation code. */
1337 struct bfd_link_needed_list *
1338 bfd_elf_get_needed_list (abfd, info)
1339 bfd *abfd ATTRIBUTE_UNUSED;
1340 struct bfd_link_info *info;
1342 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1343 return NULL;
1344 return elf_hash_table (info)->needed;
1347 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1348 hook for the linker ELF emulation code. */
1350 struct bfd_link_needed_list *
1351 bfd_elf_get_runpath_list (abfd, info)
1352 bfd *abfd ATTRIBUTE_UNUSED;
1353 struct bfd_link_info *info;
1355 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1356 return NULL;
1357 return elf_hash_table (info)->runpath;
1360 /* Get the name actually used for a dynamic object for a link. This
1361 is the SONAME entry if there is one. Otherwise, it is the string
1362 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1364 const char *
1365 bfd_elf_get_dt_soname (abfd)
1366 bfd *abfd;
1368 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1369 && bfd_get_format (abfd) == bfd_object)
1370 return elf_dt_name (abfd);
1371 return NULL;
1374 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1375 the ELF linker emulation code. */
1377 boolean
1378 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1379 bfd *abfd;
1380 struct bfd_link_needed_list **pneeded;
1382 asection *s;
1383 bfd_byte *dynbuf = NULL;
1384 int elfsec;
1385 unsigned long shlink;
1386 bfd_byte *extdyn, *extdynend;
1387 size_t extdynsize;
1388 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1390 *pneeded = NULL;
1392 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1393 || bfd_get_format (abfd) != bfd_object)
1394 return true;
1396 s = bfd_get_section_by_name (abfd, ".dynamic");
1397 if (s == NULL || s->_raw_size == 0)
1398 return true;
1400 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1401 if (dynbuf == NULL)
1402 goto error_return;
1404 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1405 s->_raw_size))
1406 goto error_return;
1408 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1409 if (elfsec == -1)
1410 goto error_return;
1412 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1414 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1415 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1417 extdyn = dynbuf;
1418 extdynend = extdyn + s->_raw_size;
1419 for (; extdyn < extdynend; extdyn += extdynsize)
1421 Elf_Internal_Dyn dyn;
1423 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1425 if (dyn.d_tag == DT_NULL)
1426 break;
1428 if (dyn.d_tag == DT_NEEDED)
1430 const char *string;
1431 struct bfd_link_needed_list *l;
1432 unsigned int tagv = dyn.d_un.d_val;
1433 bfd_size_type amt;
1435 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1436 if (string == NULL)
1437 goto error_return;
1439 amt = sizeof *l;
1440 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1441 if (l == NULL)
1442 goto error_return;
1444 l->by = abfd;
1445 l->name = string;
1446 l->next = *pneeded;
1447 *pneeded = l;
1451 free (dynbuf);
1453 return true;
1455 error_return:
1456 if (dynbuf != NULL)
1457 free (dynbuf);
1458 return false;
1461 /* Allocate an ELF string table--force the first byte to be zero. */
1463 struct bfd_strtab_hash *
1464 _bfd_elf_stringtab_init ()
1466 struct bfd_strtab_hash *ret;
1468 ret = _bfd_stringtab_init ();
1469 if (ret != NULL)
1471 bfd_size_type loc;
1473 loc = _bfd_stringtab_add (ret, "", true, false);
1474 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1475 if (loc == (bfd_size_type) -1)
1477 _bfd_stringtab_free (ret);
1478 ret = NULL;
1481 return ret;
1484 /* ELF .o/exec file reading */
1486 /* Create a new bfd section from an ELF section header. */
1488 boolean
1489 bfd_section_from_shdr (abfd, shindex)
1490 bfd *abfd;
1491 unsigned int shindex;
1493 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1494 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1495 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1496 char *name;
1498 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1500 switch (hdr->sh_type)
1502 case SHT_NULL:
1503 /* Inactive section. Throw it away. */
1504 return true;
1506 case SHT_PROGBITS: /* Normal section with contents. */
1507 case SHT_DYNAMIC: /* Dynamic linking information. */
1508 case SHT_NOBITS: /* .bss section. */
1509 case SHT_HASH: /* .hash section. */
1510 case SHT_NOTE: /* .note section. */
1511 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1513 case SHT_SYMTAB: /* A symbol table */
1514 if (elf_onesymtab (abfd) == shindex)
1515 return true;
1517 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1518 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1519 elf_onesymtab (abfd) = shindex;
1520 elf_tdata (abfd)->symtab_hdr = *hdr;
1521 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1522 abfd->flags |= HAS_SYMS;
1524 /* Sometimes a shared object will map in the symbol table. If
1525 SHF_ALLOC is set, and this is a shared object, then we also
1526 treat this section as a BFD section. We can not base the
1527 decision purely on SHF_ALLOC, because that flag is sometimes
1528 set in a relocateable object file, which would confuse the
1529 linker. */
1530 if ((hdr->sh_flags & SHF_ALLOC) != 0
1531 && (abfd->flags & DYNAMIC) != 0
1532 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1533 return false;
1535 return true;
1537 case SHT_DYNSYM: /* A dynamic symbol table */
1538 if (elf_dynsymtab (abfd) == shindex)
1539 return true;
1541 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1542 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1543 elf_dynsymtab (abfd) = shindex;
1544 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1545 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1546 abfd->flags |= HAS_SYMS;
1548 /* Besides being a symbol table, we also treat this as a regular
1549 section, so that objcopy can handle it. */
1550 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1552 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1553 if (elf_symtab_shndx (abfd) == shindex)
1554 return true;
1556 /* Get the associated symbol table. */
1557 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1558 || hdr->sh_link != elf_onesymtab (abfd))
1559 return false;
1561 elf_symtab_shndx (abfd) = shindex;
1562 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1563 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1564 return true;
1566 case SHT_STRTAB: /* A string table */
1567 if (hdr->bfd_section != NULL)
1568 return true;
1569 if (ehdr->e_shstrndx == shindex)
1571 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1572 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1573 return true;
1576 unsigned int i, num_sec;
1578 num_sec = elf_numsections (abfd);
1579 for (i = 1; i < num_sec; i++)
1581 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1582 if (hdr2->sh_link == shindex)
1584 if (! bfd_section_from_shdr (abfd, i))
1585 return false;
1586 if (elf_onesymtab (abfd) == i)
1588 elf_tdata (abfd)->strtab_hdr = *hdr;
1589 elf_elfsections (abfd)[shindex] =
1590 &elf_tdata (abfd)->strtab_hdr;
1591 return true;
1593 if (elf_dynsymtab (abfd) == i)
1595 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1596 elf_elfsections (abfd)[shindex] = hdr =
1597 &elf_tdata (abfd)->dynstrtab_hdr;
1598 /* We also treat this as a regular section, so
1599 that objcopy can handle it. */
1600 break;
1602 #if 0 /* Not handling other string tables specially right now. */
1603 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1604 /* We have a strtab for some random other section. */
1605 newsect = (asection *) hdr2->bfd_section;
1606 if (!newsect)
1607 break;
1608 hdr->bfd_section = newsect;
1609 hdr2 = &elf_section_data (newsect)->str_hdr;
1610 *hdr2 = *hdr;
1611 elf_elfsections (abfd)[shindex] = hdr2;
1612 #endif
1617 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1619 case SHT_REL:
1620 case SHT_RELA:
1621 /* *These* do a lot of work -- but build no sections! */
1623 asection *target_sect;
1624 Elf_Internal_Shdr *hdr2;
1625 unsigned int num_sec = elf_numsections (abfd);
1627 /* Check for a bogus link to avoid crashing. */
1628 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1629 || hdr->sh_link >= num_sec)
1631 ((*_bfd_error_handler)
1632 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1633 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1634 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1637 /* For some incomprehensible reason Oracle distributes
1638 libraries for Solaris in which some of the objects have
1639 bogus sh_link fields. It would be nice if we could just
1640 reject them, but, unfortunately, some people need to use
1641 them. We scan through the section headers; if we find only
1642 one suitable symbol table, we clobber the sh_link to point
1643 to it. I hope this doesn't break anything. */
1644 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1645 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1647 unsigned int scan;
1648 int found;
1650 found = 0;
1651 for (scan = 1; scan < num_sec; scan++)
1653 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1654 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1656 if (found != 0)
1658 found = 0;
1659 break;
1661 found = scan;
1664 if (found != 0)
1665 hdr->sh_link = found;
1668 /* Get the symbol table. */
1669 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1670 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1671 return false;
1673 /* If this reloc section does not use the main symbol table we
1674 don't treat it as a reloc section. BFD can't adequately
1675 represent such a section, so at least for now, we don't
1676 try. We just present it as a normal section. We also
1677 can't use it as a reloc section if it points to the null
1678 section. */
1679 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1680 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1682 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1683 return false;
1684 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1685 if (target_sect == NULL)
1686 return false;
1688 if ((target_sect->flags & SEC_RELOC) == 0
1689 || target_sect->reloc_count == 0)
1690 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1691 else
1693 bfd_size_type amt;
1694 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1695 amt = sizeof (*hdr2);
1696 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1697 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1699 *hdr2 = *hdr;
1700 elf_elfsections (abfd)[shindex] = hdr2;
1701 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1702 target_sect->flags |= SEC_RELOC;
1703 target_sect->relocation = NULL;
1704 target_sect->rel_filepos = hdr->sh_offset;
1705 /* In the section to which the relocations apply, mark whether
1706 its relocations are of the REL or RELA variety. */
1707 if (hdr->sh_size != 0)
1708 elf_section_data (target_sect)->use_rela_p
1709 = (hdr->sh_type == SHT_RELA);
1710 abfd->flags |= HAS_RELOC;
1711 return true;
1713 break;
1715 case SHT_GNU_verdef:
1716 elf_dynverdef (abfd) = shindex;
1717 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1718 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1719 break;
1721 case SHT_GNU_versym:
1722 elf_dynversym (abfd) = shindex;
1723 elf_tdata (abfd)->dynversym_hdr = *hdr;
1724 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1725 break;
1727 case SHT_GNU_verneed:
1728 elf_dynverref (abfd) = shindex;
1729 elf_tdata (abfd)->dynverref_hdr = *hdr;
1730 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1731 break;
1733 case SHT_SHLIB:
1734 return true;
1736 case SHT_GROUP:
1737 /* Make a section for objcopy and relocatable links. */
1738 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1739 return false;
1740 if (hdr->contents != NULL)
1742 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1743 unsigned int n_elt = hdr->sh_size / 4;
1744 asection *s;
1746 while (--n_elt != 0)
1747 if ((s = (++idx)->shdr->bfd_section) != NULL
1748 && elf_next_in_group (s) != NULL)
1750 elf_next_in_group (hdr->bfd_section) = s;
1751 break;
1754 break;
1756 default:
1757 /* Check for any processor-specific section types. */
1759 if (bed->elf_backend_section_from_shdr)
1760 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1762 break;
1765 return true;
1768 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1769 Return SEC for sections that have no elf section, and NULL on error. */
1771 asection *
1772 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
1773 bfd *abfd;
1774 struct sym_sec_cache *cache;
1775 asection *sec;
1776 unsigned long r_symndx;
1778 unsigned char esym_shndx[4];
1779 unsigned int isym_shndx;
1780 Elf_Internal_Shdr *symtab_hdr;
1781 file_ptr pos;
1782 bfd_size_type amt;
1783 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1785 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1786 return cache->sec[ent];
1788 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1789 pos = symtab_hdr->sh_offset;
1790 if (get_elf_backend_data (abfd)->s->sizeof_sym
1791 == sizeof (Elf64_External_Sym))
1793 pos += r_symndx * sizeof (Elf64_External_Sym);
1794 pos += offsetof (Elf64_External_Sym, st_shndx);
1795 amt = sizeof (((Elf64_External_Sym *) 0)->st_shndx);
1797 else
1799 pos += r_symndx * sizeof (Elf32_External_Sym);
1800 pos += offsetof (Elf32_External_Sym, st_shndx);
1801 amt = sizeof (((Elf32_External_Sym *) 0)->st_shndx);
1803 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1804 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1805 return NULL;
1806 isym_shndx = H_GET_16 (abfd, esym_shndx);
1808 if (isym_shndx == SHN_XINDEX)
1810 Elf_Internal_Shdr *shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1811 if (shndx_hdr->sh_size != 0)
1813 pos = shndx_hdr->sh_offset;
1814 pos += r_symndx * sizeof (Elf_External_Sym_Shndx);
1815 amt = sizeof (Elf_External_Sym_Shndx);
1816 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1817 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1818 return NULL;
1819 isym_shndx = H_GET_32 (abfd, esym_shndx);
1823 if (cache->abfd != abfd)
1825 memset (cache->indx, -1, sizeof (cache->indx));
1826 cache->abfd = abfd;
1828 cache->indx[ent] = r_symndx;
1829 cache->sec[ent] = sec;
1830 if (isym_shndx < SHN_LORESERVE || isym_shndx > SHN_HIRESERVE)
1832 asection *s;
1833 s = bfd_section_from_elf_index (abfd, isym_shndx);
1834 if (s != NULL)
1835 cache->sec[ent] = s;
1837 return cache->sec[ent];
1840 /* Given an ELF section number, retrieve the corresponding BFD
1841 section. */
1843 asection *
1844 bfd_section_from_elf_index (abfd, index)
1845 bfd *abfd;
1846 unsigned int index;
1848 if (index >= elf_numsections (abfd))
1849 return NULL;
1850 return elf_elfsections (abfd)[index]->bfd_section;
1853 boolean
1854 _bfd_elf_new_section_hook (abfd, sec)
1855 bfd *abfd;
1856 asection *sec;
1858 struct bfd_elf_section_data *sdata;
1859 bfd_size_type amt = sizeof (*sdata);
1861 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
1862 if (!sdata)
1863 return false;
1864 sec->used_by_bfd = (PTR) sdata;
1866 /* Indicate whether or not this section should use RELA relocations. */
1867 sdata->use_rela_p
1868 = get_elf_backend_data (abfd)->default_use_rela_p;
1870 return true;
1873 /* Create a new bfd section from an ELF program header.
1875 Since program segments have no names, we generate a synthetic name
1876 of the form segment<NUM>, where NUM is generally the index in the
1877 program header table. For segments that are split (see below) we
1878 generate the names segment<NUM>a and segment<NUM>b.
1880 Note that some program segments may have a file size that is different than
1881 (less than) the memory size. All this means is that at execution the
1882 system must allocate the amount of memory specified by the memory size,
1883 but only initialize it with the first "file size" bytes read from the
1884 file. This would occur for example, with program segments consisting
1885 of combined data+bss.
1887 To handle the above situation, this routine generates TWO bfd sections
1888 for the single program segment. The first has the length specified by
1889 the file size of the segment, and the second has the length specified
1890 by the difference between the two sizes. In effect, the segment is split
1891 into it's initialized and uninitialized parts.
1895 boolean
1896 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1897 bfd *abfd;
1898 Elf_Internal_Phdr *hdr;
1899 int index;
1900 const char *typename;
1902 asection *newsect;
1903 char *name;
1904 char namebuf[64];
1905 int split;
1907 split = ((hdr->p_memsz > 0)
1908 && (hdr->p_filesz > 0)
1909 && (hdr->p_memsz > hdr->p_filesz));
1910 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1911 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1912 if (!name)
1913 return false;
1914 strcpy (name, namebuf);
1915 newsect = bfd_make_section (abfd, name);
1916 if (newsect == NULL)
1917 return false;
1918 newsect->vma = hdr->p_vaddr;
1919 newsect->lma = hdr->p_paddr;
1920 newsect->_raw_size = hdr->p_filesz;
1921 newsect->filepos = hdr->p_offset;
1922 newsect->flags |= SEC_HAS_CONTENTS;
1923 if (hdr->p_type == PT_LOAD)
1925 newsect->flags |= SEC_ALLOC;
1926 newsect->flags |= SEC_LOAD;
1927 if (hdr->p_flags & PF_X)
1929 /* FIXME: all we known is that it has execute PERMISSION,
1930 may be data. */
1931 newsect->flags |= SEC_CODE;
1934 if (!(hdr->p_flags & PF_W))
1936 newsect->flags |= SEC_READONLY;
1939 if (split)
1941 sprintf (namebuf, "%s%db", typename, index);
1942 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1943 if (!name)
1944 return false;
1945 strcpy (name, namebuf);
1946 newsect = bfd_make_section (abfd, name);
1947 if (newsect == NULL)
1948 return false;
1949 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1950 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1951 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1952 if (hdr->p_type == PT_LOAD)
1954 newsect->flags |= SEC_ALLOC;
1955 if (hdr->p_flags & PF_X)
1956 newsect->flags |= SEC_CODE;
1958 if (!(hdr->p_flags & PF_W))
1959 newsect->flags |= SEC_READONLY;
1962 return true;
1965 boolean
1966 bfd_section_from_phdr (abfd, hdr, index)
1967 bfd *abfd;
1968 Elf_Internal_Phdr *hdr;
1969 int index;
1971 struct elf_backend_data *bed;
1973 switch (hdr->p_type)
1975 case PT_NULL:
1976 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1978 case PT_LOAD:
1979 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1981 case PT_DYNAMIC:
1982 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1984 case PT_INTERP:
1985 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1987 case PT_NOTE:
1988 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1989 return false;
1990 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
1991 return false;
1992 return true;
1994 case PT_SHLIB:
1995 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1997 case PT_PHDR:
1998 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2000 default:
2001 /* Check for any processor-specific program segment types.
2002 If no handler for them, default to making "segment" sections. */
2003 bed = get_elf_backend_data (abfd);
2004 if (bed->elf_backend_section_from_phdr)
2005 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2006 else
2007 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2011 /* Initialize REL_HDR, the section-header for new section, containing
2012 relocations against ASECT. If USE_RELA_P is true, we use RELA
2013 relocations; otherwise, we use REL relocations. */
2015 boolean
2016 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2017 bfd *abfd;
2018 Elf_Internal_Shdr *rel_hdr;
2019 asection *asect;
2020 boolean use_rela_p;
2022 char *name;
2023 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2024 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2026 name = bfd_alloc (abfd, amt);
2027 if (name == NULL)
2028 return false;
2029 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2030 rel_hdr->sh_name =
2031 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2032 false);
2033 if (rel_hdr->sh_name == (unsigned int) -1)
2034 return false;
2035 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2036 rel_hdr->sh_entsize = (use_rela_p
2037 ? bed->s->sizeof_rela
2038 : bed->s->sizeof_rel);
2039 rel_hdr->sh_addralign = bed->s->file_align;
2040 rel_hdr->sh_flags = 0;
2041 rel_hdr->sh_addr = 0;
2042 rel_hdr->sh_size = 0;
2043 rel_hdr->sh_offset = 0;
2045 return true;
2048 /* Set up an ELF internal section header for a section. */
2050 static void
2051 elf_fake_sections (abfd, asect, failedptrarg)
2052 bfd *abfd;
2053 asection *asect;
2054 PTR failedptrarg;
2056 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2057 boolean *failedptr = (boolean *) failedptrarg;
2058 Elf_Internal_Shdr *this_hdr;
2060 if (*failedptr)
2062 /* We already failed; just get out of the bfd_map_over_sections
2063 loop. */
2064 return;
2067 this_hdr = &elf_section_data (asect)->this_hdr;
2069 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2070 asect->name, false);
2071 if (this_hdr->sh_name == (unsigned long) -1)
2073 *failedptr = true;
2074 return;
2077 this_hdr->sh_flags = 0;
2079 if ((asect->flags & SEC_ALLOC) != 0
2080 || asect->user_set_vma)
2081 this_hdr->sh_addr = asect->vma;
2082 else
2083 this_hdr->sh_addr = 0;
2085 this_hdr->sh_offset = 0;
2086 this_hdr->sh_size = asect->_raw_size;
2087 this_hdr->sh_link = 0;
2088 this_hdr->sh_addralign = 1 << asect->alignment_power;
2089 /* The sh_entsize and sh_info fields may have been set already by
2090 copy_private_section_data. */
2092 this_hdr->bfd_section = asect;
2093 this_hdr->contents = NULL;
2095 /* FIXME: This should not be based on section names. */
2096 if (strcmp (asect->name, ".dynstr") == 0)
2097 this_hdr->sh_type = SHT_STRTAB;
2098 else if (strcmp (asect->name, ".hash") == 0)
2100 this_hdr->sh_type = SHT_HASH;
2101 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2103 else if (strcmp (asect->name, ".dynsym") == 0)
2105 this_hdr->sh_type = SHT_DYNSYM;
2106 this_hdr->sh_entsize = bed->s->sizeof_sym;
2108 else if (strcmp (asect->name, ".dynamic") == 0)
2110 this_hdr->sh_type = SHT_DYNAMIC;
2111 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2113 else if (strncmp (asect->name, ".rela", 5) == 0
2114 && get_elf_backend_data (abfd)->may_use_rela_p)
2116 this_hdr->sh_type = SHT_RELA;
2117 this_hdr->sh_entsize = bed->s->sizeof_rela;
2119 else if (strncmp (asect->name, ".rel", 4) == 0
2120 && get_elf_backend_data (abfd)->may_use_rel_p)
2122 this_hdr->sh_type = SHT_REL;
2123 this_hdr->sh_entsize = bed->s->sizeof_rel;
2125 else if (strncmp (asect->name, ".note", 5) == 0)
2126 this_hdr->sh_type = SHT_NOTE;
2127 else if (strncmp (asect->name, ".stab", 5) == 0
2128 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2129 this_hdr->sh_type = SHT_STRTAB;
2130 else if (strcmp (asect->name, ".gnu.version") == 0)
2132 this_hdr->sh_type = SHT_GNU_versym;
2133 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2135 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2137 this_hdr->sh_type = SHT_GNU_verdef;
2138 this_hdr->sh_entsize = 0;
2139 /* objcopy or strip will copy over sh_info, but may not set
2140 cverdefs. The linker will set cverdefs, but sh_info will be
2141 zero. */
2142 if (this_hdr->sh_info == 0)
2143 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2144 else
2145 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2146 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2148 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2150 this_hdr->sh_type = SHT_GNU_verneed;
2151 this_hdr->sh_entsize = 0;
2152 /* objcopy or strip will copy over sh_info, but may not set
2153 cverrefs. The linker will set cverrefs, but sh_info will be
2154 zero. */
2155 if (this_hdr->sh_info == 0)
2156 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2157 else
2158 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2159 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2161 else if ((asect->flags & SEC_GROUP) != 0)
2163 this_hdr->sh_type = SHT_GROUP;
2164 this_hdr->sh_entsize = 4;
2166 else if ((asect->flags & SEC_ALLOC) != 0
2167 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
2168 this_hdr->sh_type = SHT_NOBITS;
2169 else
2170 this_hdr->sh_type = SHT_PROGBITS;
2172 if ((asect->flags & SEC_ALLOC) != 0)
2173 this_hdr->sh_flags |= SHF_ALLOC;
2174 if ((asect->flags & SEC_READONLY) == 0)
2175 this_hdr->sh_flags |= SHF_WRITE;
2176 if ((asect->flags & SEC_CODE) != 0)
2177 this_hdr->sh_flags |= SHF_EXECINSTR;
2178 if ((asect->flags & SEC_MERGE) != 0)
2180 this_hdr->sh_flags |= SHF_MERGE;
2181 this_hdr->sh_entsize = asect->entsize;
2182 if ((asect->flags & SEC_STRINGS) != 0)
2183 this_hdr->sh_flags |= SHF_STRINGS;
2185 if (elf_group_name (asect) != NULL)
2186 this_hdr->sh_flags |= SHF_GROUP;
2188 /* Check for processor-specific section types. */
2189 if (bed->elf_backend_fake_sections)
2190 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
2192 /* If the section has relocs, set up a section header for the
2193 SHT_REL[A] section. If two relocation sections are required for
2194 this section, it is up to the processor-specific back-end to
2195 create the other. */
2196 if ((asect->flags & SEC_RELOC) != 0
2197 && !_bfd_elf_init_reloc_shdr (abfd,
2198 &elf_section_data (asect)->rel_hdr,
2199 asect,
2200 elf_section_data (asect)->use_rela_p))
2201 *failedptr = true;
2204 /* Fill in the contents of a SHT_GROUP section. */
2206 static void
2207 set_group_contents (abfd, sec, failedptrarg)
2208 bfd *abfd;
2209 asection *sec;
2210 PTR failedptrarg ATTRIBUTE_UNUSED;
2212 boolean *failedptr = (boolean *) failedptrarg;
2213 unsigned long symindx;
2214 asection *elt;
2215 unsigned char *loc;
2216 struct bfd_link_order *l;
2218 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2219 || *failedptr)
2220 return;
2222 /* If called from the assembler, swap_out_syms will have set up
2223 elf_section_syms; If called for "ld -r", the symbols won't yet
2224 be mapped, so emulate elf_bfd_final_link. */
2225 if (elf_section_syms (abfd) != NULL)
2226 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2227 else
2228 symindx = elf_section_data (sec)->this_idx;
2229 elf_section_data (sec)->this_hdr.sh_info = symindx;
2231 /* Nor will the contents be allocated for "ld -r". */
2232 if (sec->contents == NULL)
2234 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2235 if (sec->contents == NULL)
2237 *failedptr = true;
2238 return;
2242 loc = sec->contents + sec->_raw_size;
2244 /* Get the pointer to the first section in the group that we
2245 squirreled away here. */
2246 elt = elf_next_in_group (sec);
2248 /* First element is a flag word. Rest of section is elf section
2249 indices for all the sections of the group. Write them backwards
2250 just to keep the group in the same order as given in .section
2251 directives, not that it matters. */
2252 while (elt != NULL)
2254 loc -= 4;
2255 H_PUT_32 (abfd, elf_section_data (elt)->this_idx, loc);
2256 elt = elf_next_in_group (elt);
2259 /* If this is a relocatable link, then the above did nothing because
2260 SEC is the output section. Look through the input sections
2261 instead. */
2262 for (l = sec->link_order_head; l != NULL; l = l->next)
2263 if (l->type == bfd_indirect_link_order
2264 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2267 loc -= 4;
2268 H_PUT_32 (abfd,
2269 elf_section_data (elt->output_section)->this_idx, loc);
2270 elt = elf_next_in_group (elt);
2271 /* During a relocatable link, the lists are circular. */
2273 while (elt != elf_next_in_group (l->u.indirect.section));
2275 loc -= 4;
2276 H_PUT_32 (abfd, 0, loc);
2278 BFD_ASSERT (loc == sec->contents);
2281 /* Assign all ELF section numbers. The dummy first section is handled here
2282 too. The link/info pointers for the standard section types are filled
2283 in here too, while we're at it. */
2285 static boolean
2286 assign_section_numbers (abfd)
2287 bfd *abfd;
2289 struct elf_obj_tdata *t = elf_tdata (abfd);
2290 asection *sec;
2291 unsigned int section_number, secn;
2292 Elf_Internal_Shdr **i_shdrp;
2293 bfd_size_type amt;
2295 section_number = 1;
2297 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2299 for (sec = abfd->sections; sec; sec = sec->next)
2301 struct bfd_elf_section_data *d = elf_section_data (sec);
2303 if (section_number == SHN_LORESERVE)
2304 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2305 d->this_idx = section_number++;
2306 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2307 if ((sec->flags & SEC_RELOC) == 0)
2308 d->rel_idx = 0;
2309 else
2311 if (section_number == SHN_LORESERVE)
2312 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2313 d->rel_idx = section_number++;
2314 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2317 if (d->rel_hdr2)
2319 if (section_number == SHN_LORESERVE)
2320 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2321 d->rel_idx2 = section_number++;
2322 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2324 else
2325 d->rel_idx2 = 0;
2328 if (section_number == SHN_LORESERVE)
2329 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2330 t->shstrtab_section = section_number++;
2331 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2332 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2334 if (bfd_get_symcount (abfd) > 0)
2336 if (section_number == SHN_LORESERVE)
2337 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2338 t->symtab_section = section_number++;
2339 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2340 if (section_number > SHN_LORESERVE - 2)
2342 if (section_number == SHN_LORESERVE)
2343 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2344 t->symtab_shndx_section = section_number++;
2345 t->symtab_shndx_hdr.sh_name
2346 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2347 ".symtab_shndx", false);
2348 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2349 return false;
2351 if (section_number == SHN_LORESERVE)
2352 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2353 t->strtab_section = section_number++;
2354 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2357 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2358 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2360 elf_numsections (abfd) = section_number;
2361 elf_elfheader (abfd)->e_shnum = section_number;
2362 if (section_number > SHN_LORESERVE)
2363 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2365 /* Set up the list of section header pointers, in agreement with the
2366 indices. */
2367 amt = section_number * sizeof (Elf_Internal_Shdr *);
2368 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
2369 if (i_shdrp == NULL)
2370 return false;
2372 amt = sizeof (Elf_Internal_Shdr);
2373 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2374 if (i_shdrp[0] == NULL)
2376 bfd_release (abfd, i_shdrp);
2377 return false;
2379 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2381 elf_elfsections (abfd) = i_shdrp;
2383 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2384 if (bfd_get_symcount (abfd) > 0)
2386 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2387 if (elf_numsections (abfd) > SHN_LORESERVE)
2389 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2390 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2392 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2393 t->symtab_hdr.sh_link = t->strtab_section;
2395 for (sec = abfd->sections; sec; sec = sec->next)
2397 struct bfd_elf_section_data *d = elf_section_data (sec);
2398 asection *s;
2399 const char *name;
2401 i_shdrp[d->this_idx] = &d->this_hdr;
2402 if (d->rel_idx != 0)
2403 i_shdrp[d->rel_idx] = &d->rel_hdr;
2404 if (d->rel_idx2 != 0)
2405 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2407 /* Fill in the sh_link and sh_info fields while we're at it. */
2409 /* sh_link of a reloc section is the section index of the symbol
2410 table. sh_info is the section index of the section to which
2411 the relocation entries apply. */
2412 if (d->rel_idx != 0)
2414 d->rel_hdr.sh_link = t->symtab_section;
2415 d->rel_hdr.sh_info = d->this_idx;
2417 if (d->rel_idx2 != 0)
2419 d->rel_hdr2->sh_link = t->symtab_section;
2420 d->rel_hdr2->sh_info = d->this_idx;
2423 switch (d->this_hdr.sh_type)
2425 case SHT_REL:
2426 case SHT_RELA:
2427 /* A reloc section which we are treating as a normal BFD
2428 section. sh_link is the section index of the symbol
2429 table. sh_info is the section index of the section to
2430 which the relocation entries apply. We assume that an
2431 allocated reloc section uses the dynamic symbol table.
2432 FIXME: How can we be sure? */
2433 s = bfd_get_section_by_name (abfd, ".dynsym");
2434 if (s != NULL)
2435 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2437 /* We look up the section the relocs apply to by name. */
2438 name = sec->name;
2439 if (d->this_hdr.sh_type == SHT_REL)
2440 name += 4;
2441 else
2442 name += 5;
2443 s = bfd_get_section_by_name (abfd, name);
2444 if (s != NULL)
2445 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2446 break;
2448 case SHT_STRTAB:
2449 /* We assume that a section named .stab*str is a stabs
2450 string section. We look for a section with the same name
2451 but without the trailing ``str'', and set its sh_link
2452 field to point to this section. */
2453 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2454 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2456 size_t len;
2457 char *alc;
2459 len = strlen (sec->name);
2460 alc = (char *) bfd_malloc ((bfd_size_type) len - 2);
2461 if (alc == NULL)
2462 return false;
2463 strncpy (alc, sec->name, len - 3);
2464 alc[len - 3] = '\0';
2465 s = bfd_get_section_by_name (abfd, alc);
2466 free (alc);
2467 if (s != NULL)
2469 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2471 /* This is a .stab section. */
2472 elf_section_data (s)->this_hdr.sh_entsize =
2473 4 + 2 * bfd_get_arch_size (abfd) / 8;
2476 break;
2478 case SHT_DYNAMIC:
2479 case SHT_DYNSYM:
2480 case SHT_GNU_verneed:
2481 case SHT_GNU_verdef:
2482 /* sh_link is the section header index of the string table
2483 used for the dynamic entries, or the symbol table, or the
2484 version strings. */
2485 s = bfd_get_section_by_name (abfd, ".dynstr");
2486 if (s != NULL)
2487 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2488 break;
2490 case SHT_HASH:
2491 case SHT_GNU_versym:
2492 /* sh_link is the section header index of the symbol table
2493 this hash table or version table is for. */
2494 s = bfd_get_section_by_name (abfd, ".dynsym");
2495 if (s != NULL)
2496 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2497 break;
2499 case SHT_GROUP:
2500 d->this_hdr.sh_link = t->symtab_section;
2504 for (secn = 1; secn < section_number; ++secn)
2505 if (i_shdrp[secn] == NULL)
2506 i_shdrp[secn] = i_shdrp[0];
2507 else
2508 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2509 i_shdrp[secn]->sh_name);
2510 return true;
2513 /* Map symbol from it's internal number to the external number, moving
2514 all local symbols to be at the head of the list. */
2516 static INLINE int
2517 sym_is_global (abfd, sym)
2518 bfd *abfd;
2519 asymbol *sym;
2521 /* If the backend has a special mapping, use it. */
2522 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2523 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2524 (abfd, sym));
2526 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2527 || bfd_is_und_section (bfd_get_section (sym))
2528 || bfd_is_com_section (bfd_get_section (sym)));
2531 static boolean
2532 elf_map_symbols (abfd)
2533 bfd *abfd;
2535 unsigned int symcount = bfd_get_symcount (abfd);
2536 asymbol **syms = bfd_get_outsymbols (abfd);
2537 asymbol **sect_syms;
2538 unsigned int num_locals = 0;
2539 unsigned int num_globals = 0;
2540 unsigned int num_locals2 = 0;
2541 unsigned int num_globals2 = 0;
2542 int max_index = 0;
2543 unsigned int idx;
2544 asection *asect;
2545 asymbol **new_syms;
2546 bfd_size_type amt;
2548 #ifdef DEBUG
2549 fprintf (stderr, "elf_map_symbols\n");
2550 fflush (stderr);
2551 #endif
2553 for (asect = abfd->sections; asect; asect = asect->next)
2555 if (max_index < asect->index)
2556 max_index = asect->index;
2559 max_index++;
2560 amt = max_index * sizeof (asymbol *);
2561 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2562 if (sect_syms == NULL)
2563 return false;
2564 elf_section_syms (abfd) = sect_syms;
2565 elf_num_section_syms (abfd) = max_index;
2567 /* Init sect_syms entries for any section symbols we have already
2568 decided to output. */
2569 for (idx = 0; idx < symcount; idx++)
2571 asymbol *sym = syms[idx];
2573 if ((sym->flags & BSF_SECTION_SYM) != 0
2574 && sym->value == 0)
2576 asection *sec;
2578 sec = sym->section;
2580 if (sec->owner != NULL)
2582 if (sec->owner != abfd)
2584 if (sec->output_offset != 0)
2585 continue;
2587 sec = sec->output_section;
2589 /* Empty sections in the input files may have had a
2590 section symbol created for them. (See the comment
2591 near the end of _bfd_generic_link_output_symbols in
2592 linker.c). If the linker script discards such
2593 sections then we will reach this point. Since we know
2594 that we cannot avoid this case, we detect it and skip
2595 the abort and the assignment to the sect_syms array.
2596 To reproduce this particular case try running the
2597 linker testsuite test ld-scripts/weak.exp for an ELF
2598 port that uses the generic linker. */
2599 if (sec->owner == NULL)
2600 continue;
2602 BFD_ASSERT (sec->owner == abfd);
2604 sect_syms[sec->index] = syms[idx];
2609 /* Classify all of the symbols. */
2610 for (idx = 0; idx < symcount; idx++)
2612 if (!sym_is_global (abfd, syms[idx]))
2613 num_locals++;
2614 else
2615 num_globals++;
2618 /* We will be adding a section symbol for each BFD section. Most normal
2619 sections will already have a section symbol in outsymbols, but
2620 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2621 at least in that case. */
2622 for (asect = abfd->sections; asect; asect = asect->next)
2624 if (sect_syms[asect->index] == NULL)
2626 if (!sym_is_global (abfd, asect->symbol))
2627 num_locals++;
2628 else
2629 num_globals++;
2633 /* Now sort the symbols so the local symbols are first. */
2634 amt = (num_locals + num_globals) * sizeof (asymbol *);
2635 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2637 if (new_syms == NULL)
2638 return false;
2640 for (idx = 0; idx < symcount; idx++)
2642 asymbol *sym = syms[idx];
2643 unsigned int i;
2645 if (!sym_is_global (abfd, sym))
2646 i = num_locals2++;
2647 else
2648 i = num_locals + num_globals2++;
2649 new_syms[i] = sym;
2650 sym->udata.i = i + 1;
2652 for (asect = abfd->sections; asect; asect = asect->next)
2654 if (sect_syms[asect->index] == NULL)
2656 asymbol *sym = asect->symbol;
2657 unsigned int i;
2659 sect_syms[asect->index] = sym;
2660 if (!sym_is_global (abfd, sym))
2661 i = num_locals2++;
2662 else
2663 i = num_locals + num_globals2++;
2664 new_syms[i] = sym;
2665 sym->udata.i = i + 1;
2669 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2671 elf_num_locals (abfd) = num_locals;
2672 elf_num_globals (abfd) = num_globals;
2673 return true;
2676 /* Align to the maximum file alignment that could be required for any
2677 ELF data structure. */
2679 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2680 static INLINE file_ptr
2681 align_file_position (off, align)
2682 file_ptr off;
2683 int align;
2685 return (off + align - 1) & ~(align - 1);
2688 /* Assign a file position to a section, optionally aligning to the
2689 required section alignment. */
2691 INLINE file_ptr
2692 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2693 Elf_Internal_Shdr *i_shdrp;
2694 file_ptr offset;
2695 boolean align;
2697 if (align)
2699 unsigned int al;
2701 al = i_shdrp->sh_addralign;
2702 if (al > 1)
2703 offset = BFD_ALIGN (offset, al);
2705 i_shdrp->sh_offset = offset;
2706 if (i_shdrp->bfd_section != NULL)
2707 i_shdrp->bfd_section->filepos = offset;
2708 if (i_shdrp->sh_type != SHT_NOBITS)
2709 offset += i_shdrp->sh_size;
2710 return offset;
2713 /* Compute the file positions we are going to put the sections at, and
2714 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2715 is not NULL, this is being called by the ELF backend linker. */
2717 boolean
2718 _bfd_elf_compute_section_file_positions (abfd, link_info)
2719 bfd *abfd;
2720 struct bfd_link_info *link_info;
2722 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2723 boolean failed;
2724 struct bfd_strtab_hash *strtab;
2725 Elf_Internal_Shdr *shstrtab_hdr;
2727 if (abfd->output_has_begun)
2728 return true;
2730 /* Do any elf backend specific processing first. */
2731 if (bed->elf_backend_begin_write_processing)
2732 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2734 if (! prep_headers (abfd))
2735 return false;
2737 /* Post process the headers if necessary. */
2738 if (bed->elf_backend_post_process_headers)
2739 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2741 failed = false;
2742 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2743 if (failed)
2744 return false;
2746 if (!assign_section_numbers (abfd))
2747 return false;
2749 /* The backend linker builds symbol table information itself. */
2750 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2752 /* Non-zero if doing a relocatable link. */
2753 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2755 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2756 return false;
2759 if (link_info == NULL || link_info->relocateable)
2761 bfd_map_over_sections (abfd, set_group_contents, &failed);
2762 if (failed)
2763 return false;
2766 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2767 /* sh_name was set in prep_headers. */
2768 shstrtab_hdr->sh_type = SHT_STRTAB;
2769 shstrtab_hdr->sh_flags = 0;
2770 shstrtab_hdr->sh_addr = 0;
2771 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2772 shstrtab_hdr->sh_entsize = 0;
2773 shstrtab_hdr->sh_link = 0;
2774 shstrtab_hdr->sh_info = 0;
2775 /* sh_offset is set in assign_file_positions_except_relocs. */
2776 shstrtab_hdr->sh_addralign = 1;
2778 if (!assign_file_positions_except_relocs (abfd))
2779 return false;
2781 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2783 file_ptr off;
2784 Elf_Internal_Shdr *hdr;
2786 off = elf_tdata (abfd)->next_file_pos;
2788 hdr = &elf_tdata (abfd)->symtab_hdr;
2789 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2791 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2792 if (hdr->sh_size != 0)
2793 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2795 hdr = &elf_tdata (abfd)->strtab_hdr;
2796 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2798 elf_tdata (abfd)->next_file_pos = off;
2800 /* Now that we know where the .strtab section goes, write it
2801 out. */
2802 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2803 || ! _bfd_stringtab_emit (abfd, strtab))
2804 return false;
2805 _bfd_stringtab_free (strtab);
2808 abfd->output_has_begun = true;
2810 return true;
2813 /* Create a mapping from a set of sections to a program segment. */
2815 static INLINE struct elf_segment_map *
2816 make_mapping (abfd, sections, from, to, phdr)
2817 bfd *abfd;
2818 asection **sections;
2819 unsigned int from;
2820 unsigned int to;
2821 boolean phdr;
2823 struct elf_segment_map *m;
2824 unsigned int i;
2825 asection **hdrpp;
2826 bfd_size_type amt;
2828 amt = sizeof (struct elf_segment_map);
2829 amt += (to - from - 1) * sizeof (asection *);
2830 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2831 if (m == NULL)
2832 return NULL;
2833 m->next = NULL;
2834 m->p_type = PT_LOAD;
2835 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2836 m->sections[i - from] = *hdrpp;
2837 m->count = to - from;
2839 if (from == 0 && phdr)
2841 /* Include the headers in the first PT_LOAD segment. */
2842 m->includes_filehdr = 1;
2843 m->includes_phdrs = 1;
2846 return m;
2849 /* Set up a mapping from BFD sections to program segments. */
2851 static boolean
2852 map_sections_to_segments (abfd)
2853 bfd *abfd;
2855 asection **sections = NULL;
2856 asection *s;
2857 unsigned int i;
2858 unsigned int count;
2859 struct elf_segment_map *mfirst;
2860 struct elf_segment_map **pm;
2861 struct elf_segment_map *m;
2862 asection *last_hdr;
2863 unsigned int phdr_index;
2864 bfd_vma maxpagesize;
2865 asection **hdrpp;
2866 boolean phdr_in_segment = true;
2867 boolean writable;
2868 asection *dynsec, *eh_frame_hdr;
2869 bfd_size_type amt;
2871 if (elf_tdata (abfd)->segment_map != NULL)
2872 return true;
2874 if (bfd_count_sections (abfd) == 0)
2875 return true;
2877 /* Select the allocated sections, and sort them. */
2879 amt = bfd_count_sections (abfd) * sizeof (asection *);
2880 sections = (asection **) bfd_malloc (amt);
2881 if (sections == NULL)
2882 goto error_return;
2884 i = 0;
2885 for (s = abfd->sections; s != NULL; s = s->next)
2887 if ((s->flags & SEC_ALLOC) != 0)
2889 sections[i] = s;
2890 ++i;
2893 BFD_ASSERT (i <= bfd_count_sections (abfd));
2894 count = i;
2896 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2898 /* Build the mapping. */
2900 mfirst = NULL;
2901 pm = &mfirst;
2903 /* If we have a .interp section, then create a PT_PHDR segment for
2904 the program headers and a PT_INTERP segment for the .interp
2905 section. */
2906 s = bfd_get_section_by_name (abfd, ".interp");
2907 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2909 amt = sizeof (struct elf_segment_map);
2910 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2911 if (m == NULL)
2912 goto error_return;
2913 m->next = NULL;
2914 m->p_type = PT_PHDR;
2915 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2916 m->p_flags = PF_R | PF_X;
2917 m->p_flags_valid = 1;
2918 m->includes_phdrs = 1;
2920 *pm = m;
2921 pm = &m->next;
2923 amt = sizeof (struct elf_segment_map);
2924 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2925 if (m == NULL)
2926 goto error_return;
2927 m->next = NULL;
2928 m->p_type = PT_INTERP;
2929 m->count = 1;
2930 m->sections[0] = s;
2932 *pm = m;
2933 pm = &m->next;
2936 /* Look through the sections. We put sections in the same program
2937 segment when the start of the second section can be placed within
2938 a few bytes of the end of the first section. */
2939 last_hdr = NULL;
2940 phdr_index = 0;
2941 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2942 writable = false;
2943 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2944 if (dynsec != NULL
2945 && (dynsec->flags & SEC_LOAD) == 0)
2946 dynsec = NULL;
2948 /* Deal with -Ttext or something similar such that the first section
2949 is not adjacent to the program headers. This is an
2950 approximation, since at this point we don't know exactly how many
2951 program headers we will need. */
2952 if (count > 0)
2954 bfd_size_type phdr_size;
2956 phdr_size = elf_tdata (abfd)->program_header_size;
2957 if (phdr_size == 0)
2958 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2959 if ((abfd->flags & D_PAGED) == 0
2960 || sections[0]->lma < phdr_size
2961 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2962 phdr_in_segment = false;
2965 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2967 asection *hdr;
2968 boolean new_segment;
2970 hdr = *hdrpp;
2972 /* See if this section and the last one will fit in the same
2973 segment. */
2975 if (last_hdr == NULL)
2977 /* If we don't have a segment yet, then we don't need a new
2978 one (we build the last one after this loop). */
2979 new_segment = false;
2981 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2983 /* If this section has a different relation between the
2984 virtual address and the load address, then we need a new
2985 segment. */
2986 new_segment = true;
2988 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2989 < BFD_ALIGN (hdr->lma, maxpagesize))
2991 /* If putting this section in this segment would force us to
2992 skip a page in the segment, then we need a new segment. */
2993 new_segment = true;
2995 else if ((last_hdr->flags & SEC_LOAD) == 0
2996 && (hdr->flags & SEC_LOAD) != 0)
2998 /* We don't want to put a loadable section after a
2999 nonloadable section in the same segment. */
3000 new_segment = true;
3002 else if ((abfd->flags & D_PAGED) == 0)
3004 /* If the file is not demand paged, which means that we
3005 don't require the sections to be correctly aligned in the
3006 file, then there is no other reason for a new segment. */
3007 new_segment = false;
3009 else if (! writable
3010 && (hdr->flags & SEC_READONLY) == 0
3011 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3012 == hdr->lma))
3014 /* We don't want to put a writable section in a read only
3015 segment, unless they are on the same page in memory
3016 anyhow. We already know that the last section does not
3017 bring us past the current section on the page, so the
3018 only case in which the new section is not on the same
3019 page as the previous section is when the previous section
3020 ends precisely on a page boundary. */
3021 new_segment = true;
3023 else
3025 /* Otherwise, we can use the same segment. */
3026 new_segment = false;
3029 if (! new_segment)
3031 if ((hdr->flags & SEC_READONLY) == 0)
3032 writable = true;
3033 last_hdr = hdr;
3034 continue;
3037 /* We need a new program segment. We must create a new program
3038 header holding all the sections from phdr_index until hdr. */
3040 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3041 if (m == NULL)
3042 goto error_return;
3044 *pm = m;
3045 pm = &m->next;
3047 if ((hdr->flags & SEC_READONLY) == 0)
3048 writable = true;
3049 else
3050 writable = false;
3052 last_hdr = hdr;
3053 phdr_index = i;
3054 phdr_in_segment = false;
3057 /* Create a final PT_LOAD program segment. */
3058 if (last_hdr != NULL)
3060 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3061 if (m == NULL)
3062 goto error_return;
3064 *pm = m;
3065 pm = &m->next;
3068 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3069 if (dynsec != NULL)
3071 amt = sizeof (struct elf_segment_map);
3072 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3073 if (m == NULL)
3074 goto error_return;
3075 m->next = NULL;
3076 m->p_type = PT_DYNAMIC;
3077 m->count = 1;
3078 m->sections[0] = dynsec;
3080 *pm = m;
3081 pm = &m->next;
3084 /* For each loadable .note section, add a PT_NOTE segment. We don't
3085 use bfd_get_section_by_name, because if we link together
3086 nonloadable .note sections and loadable .note sections, we will
3087 generate two .note sections in the output file. FIXME: Using
3088 names for section types is bogus anyhow. */
3089 for (s = abfd->sections; s != NULL; s = s->next)
3091 if ((s->flags & SEC_LOAD) != 0
3092 && strncmp (s->name, ".note", 5) == 0)
3094 amt = sizeof (struct elf_segment_map);
3095 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3096 if (m == NULL)
3097 goto error_return;
3098 m->next = NULL;
3099 m->p_type = PT_NOTE;
3100 m->count = 1;
3101 m->sections[0] = s;
3103 *pm = m;
3104 pm = &m->next;
3108 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3109 segment. */
3110 eh_frame_hdr = NULL;
3111 if (elf_tdata (abfd)->eh_frame_hdr)
3112 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
3113 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3115 amt = sizeof (struct elf_segment_map);
3116 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3117 if (m == NULL)
3118 goto error_return;
3119 m->next = NULL;
3120 m->p_type = PT_GNU_EH_FRAME;
3121 m->count = 1;
3122 m->sections[0] = eh_frame_hdr;
3124 *pm = m;
3125 pm = &m->next;
3128 free (sections);
3129 sections = NULL;
3131 elf_tdata (abfd)->segment_map = mfirst;
3132 return true;
3134 error_return:
3135 if (sections != NULL)
3136 free (sections);
3137 return false;
3140 /* Sort sections by address. */
3142 static int
3143 elf_sort_sections (arg1, arg2)
3144 const PTR arg1;
3145 const PTR arg2;
3147 const asection *sec1 = *(const asection **) arg1;
3148 const asection *sec2 = *(const asection **) arg2;
3150 /* Sort by LMA first, since this is the address used to
3151 place the section into a segment. */
3152 if (sec1->lma < sec2->lma)
3153 return -1;
3154 else if (sec1->lma > sec2->lma)
3155 return 1;
3157 /* Then sort by VMA. Normally the LMA and the VMA will be
3158 the same, and this will do nothing. */
3159 if (sec1->vma < sec2->vma)
3160 return -1;
3161 else if (sec1->vma > sec2->vma)
3162 return 1;
3164 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3166 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3168 if (TOEND (sec1))
3170 if (TOEND (sec2))
3172 /* If the indicies are the same, do not return 0
3173 here, but continue to try the next comparison. */
3174 if (sec1->target_index - sec2->target_index != 0)
3175 return sec1->target_index - sec2->target_index;
3177 else
3178 return 1;
3180 else if (TOEND (sec2))
3181 return -1;
3183 #undef TOEND
3185 /* Sort by size, to put zero sized sections
3186 before others at the same address. */
3188 if (sec1->_raw_size < sec2->_raw_size)
3189 return -1;
3190 if (sec1->_raw_size > sec2->_raw_size)
3191 return 1;
3193 return sec1->target_index - sec2->target_index;
3196 /* Assign file positions to the sections based on the mapping from
3197 sections to segments. This function also sets up some fields in
3198 the file header, and writes out the program headers. */
3200 static boolean
3201 assign_file_positions_for_segments (abfd)
3202 bfd *abfd;
3204 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3205 unsigned int count;
3206 struct elf_segment_map *m;
3207 unsigned int alloc;
3208 Elf_Internal_Phdr *phdrs;
3209 file_ptr off, voff;
3210 bfd_vma filehdr_vaddr, filehdr_paddr;
3211 bfd_vma phdrs_vaddr, phdrs_paddr;
3212 Elf_Internal_Phdr *p;
3213 bfd_size_type amt;
3215 if (elf_tdata (abfd)->segment_map == NULL)
3217 if (! map_sections_to_segments (abfd))
3218 return false;
3221 if (bed->elf_backend_modify_segment_map)
3223 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3224 return false;
3227 count = 0;
3228 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3229 ++count;
3231 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3232 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3233 elf_elfheader (abfd)->e_phnum = count;
3235 if (count == 0)
3236 return true;
3238 /* If we already counted the number of program segments, make sure
3239 that we allocated enough space. This happens when SIZEOF_HEADERS
3240 is used in a linker script. */
3241 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3242 if (alloc != 0 && count > alloc)
3244 ((*_bfd_error_handler)
3245 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3246 bfd_get_filename (abfd), alloc, count));
3247 bfd_set_error (bfd_error_bad_value);
3248 return false;
3251 if (alloc == 0)
3252 alloc = count;
3254 amt = alloc * sizeof (Elf_Internal_Phdr);
3255 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3256 if (phdrs == NULL)
3257 return false;
3259 off = bed->s->sizeof_ehdr;
3260 off += alloc * bed->s->sizeof_phdr;
3262 filehdr_vaddr = 0;
3263 filehdr_paddr = 0;
3264 phdrs_vaddr = 0;
3265 phdrs_paddr = 0;
3267 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3268 m != NULL;
3269 m = m->next, p++)
3271 unsigned int i;
3272 asection **secpp;
3274 /* If elf_segment_map is not from map_sections_to_segments, the
3275 sections may not be correctly ordered. */
3276 if (m->count > 0)
3277 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3278 elf_sort_sections);
3280 p->p_type = m->p_type;
3281 p->p_flags = m->p_flags;
3283 if (p->p_type == PT_LOAD
3284 && m->count > 0
3285 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3287 if ((abfd->flags & D_PAGED) != 0)
3288 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3289 else
3291 bfd_size_type align;
3293 align = 0;
3294 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3296 bfd_size_type secalign;
3298 secalign = bfd_get_section_alignment (abfd, *secpp);
3299 if (secalign > align)
3300 align = secalign;
3303 off += (m->sections[0]->vma - off) % (1 << align);
3307 if (m->count == 0)
3308 p->p_vaddr = 0;
3309 else
3310 p->p_vaddr = m->sections[0]->vma;
3312 if (m->p_paddr_valid)
3313 p->p_paddr = m->p_paddr;
3314 else if (m->count == 0)
3315 p->p_paddr = 0;
3316 else
3317 p->p_paddr = m->sections[0]->lma;
3319 if (p->p_type == PT_LOAD
3320 && (abfd->flags & D_PAGED) != 0)
3321 p->p_align = bed->maxpagesize;
3322 else if (m->count == 0)
3323 p->p_align = bed->s->file_align;
3324 else
3325 p->p_align = 0;
3327 p->p_offset = 0;
3328 p->p_filesz = 0;
3329 p->p_memsz = 0;
3331 if (m->includes_filehdr)
3333 if (! m->p_flags_valid)
3334 p->p_flags |= PF_R;
3335 p->p_offset = 0;
3336 p->p_filesz = bed->s->sizeof_ehdr;
3337 p->p_memsz = bed->s->sizeof_ehdr;
3338 if (m->count > 0)
3340 BFD_ASSERT (p->p_type == PT_LOAD);
3342 if (p->p_vaddr < (bfd_vma) off)
3344 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3345 bfd_get_filename (abfd));
3346 bfd_set_error (bfd_error_bad_value);
3347 return false;
3350 p->p_vaddr -= off;
3351 if (! m->p_paddr_valid)
3352 p->p_paddr -= off;
3354 if (p->p_type == PT_LOAD)
3356 filehdr_vaddr = p->p_vaddr;
3357 filehdr_paddr = p->p_paddr;
3361 if (m->includes_phdrs)
3363 if (! m->p_flags_valid)
3364 p->p_flags |= PF_R;
3366 if (m->includes_filehdr)
3368 if (p->p_type == PT_LOAD)
3370 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3371 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3374 else
3376 p->p_offset = bed->s->sizeof_ehdr;
3378 if (m->count > 0)
3380 BFD_ASSERT (p->p_type == PT_LOAD);
3381 p->p_vaddr -= off - p->p_offset;
3382 if (! m->p_paddr_valid)
3383 p->p_paddr -= off - p->p_offset;
3386 if (p->p_type == PT_LOAD)
3388 phdrs_vaddr = p->p_vaddr;
3389 phdrs_paddr = p->p_paddr;
3391 else
3392 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3395 p->p_filesz += alloc * bed->s->sizeof_phdr;
3396 p->p_memsz += alloc * bed->s->sizeof_phdr;
3399 if (p->p_type == PT_LOAD
3400 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3402 if (! m->includes_filehdr && ! m->includes_phdrs)
3403 p->p_offset = off;
3404 else
3406 file_ptr adjust;
3408 adjust = off - (p->p_offset + p->p_filesz);
3409 p->p_filesz += adjust;
3410 p->p_memsz += adjust;
3414 voff = off;
3416 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3418 asection *sec;
3419 flagword flags;
3420 bfd_size_type align;
3422 sec = *secpp;
3423 flags = sec->flags;
3424 align = 1 << bfd_get_section_alignment (abfd, sec);
3426 /* The section may have artificial alignment forced by a
3427 link script. Notice this case by the gap between the
3428 cumulative phdr lma and the section's lma. */
3429 if (p->p_paddr + p->p_memsz < sec->lma)
3431 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3433 p->p_memsz += adjust;
3434 off += adjust;
3435 voff += adjust;
3436 if ((flags & SEC_LOAD) != 0)
3437 p->p_filesz += adjust;
3440 if (p->p_type == PT_LOAD)
3442 bfd_signed_vma adjust;
3444 if ((flags & SEC_LOAD) != 0)
3446 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3447 if (adjust < 0)
3448 adjust = 0;
3450 else if ((flags & SEC_ALLOC) != 0)
3452 /* The section VMA must equal the file position
3453 modulo the page size. FIXME: I'm not sure if
3454 this adjustment is really necessary. We used to
3455 not have the SEC_LOAD case just above, and then
3456 this was necessary, but now I'm not sure. */
3457 if ((abfd->flags & D_PAGED) != 0)
3458 adjust = (sec->vma - voff) % bed->maxpagesize;
3459 else
3460 adjust = (sec->vma - voff) % align;
3462 else
3463 adjust = 0;
3465 if (adjust != 0)
3467 if (i == 0)
3469 (* _bfd_error_handler) (_("\
3470 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3471 bfd_section_name (abfd, sec),
3472 sec->lma,
3473 p->p_paddr);
3474 return false;
3476 p->p_memsz += adjust;
3477 off += adjust;
3478 voff += adjust;
3479 if ((flags & SEC_LOAD) != 0)
3480 p->p_filesz += adjust;
3483 sec->filepos = off;
3485 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3486 used in a linker script we may have a section with
3487 SEC_LOAD clear but which is supposed to have
3488 contents. */
3489 if ((flags & SEC_LOAD) != 0
3490 || (flags & SEC_HAS_CONTENTS) != 0)
3491 off += sec->_raw_size;
3493 if ((flags & SEC_ALLOC) != 0)
3494 voff += sec->_raw_size;
3497 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3499 /* The actual "note" segment has i == 0.
3500 This is the one that actually contains everything. */
3501 if (i == 0)
3503 sec->filepos = off;
3504 p->p_filesz = sec->_raw_size;
3505 off += sec->_raw_size;
3506 voff = off;
3508 else
3510 /* Fake sections -- don't need to be written. */
3511 sec->filepos = 0;
3512 sec->_raw_size = 0;
3513 flags = sec->flags = 0;
3515 p->p_memsz = 0;
3516 p->p_align = 1;
3518 else
3520 p->p_memsz += sec->_raw_size;
3522 if ((flags & SEC_LOAD) != 0)
3523 p->p_filesz += sec->_raw_size;
3525 if (align > p->p_align
3526 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3527 p->p_align = align;
3530 if (! m->p_flags_valid)
3532 p->p_flags |= PF_R;
3533 if ((flags & SEC_CODE) != 0)
3534 p->p_flags |= PF_X;
3535 if ((flags & SEC_READONLY) == 0)
3536 p->p_flags |= PF_W;
3541 /* Now that we have set the section file positions, we can set up
3542 the file positions for the non PT_LOAD segments. */
3543 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3544 m != NULL;
3545 m = m->next, p++)
3547 if (p->p_type != PT_LOAD && m->count > 0)
3549 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3550 p->p_offset = m->sections[0]->filepos;
3552 if (m->count == 0)
3554 if (m->includes_filehdr)
3556 p->p_vaddr = filehdr_vaddr;
3557 if (! m->p_paddr_valid)
3558 p->p_paddr = filehdr_paddr;
3560 else if (m->includes_phdrs)
3562 p->p_vaddr = phdrs_vaddr;
3563 if (! m->p_paddr_valid)
3564 p->p_paddr = phdrs_paddr;
3569 /* Clear out any program headers we allocated but did not use. */
3570 for (; count < alloc; count++, p++)
3572 memset (p, 0, sizeof *p);
3573 p->p_type = PT_NULL;
3576 elf_tdata (abfd)->phdr = phdrs;
3578 elf_tdata (abfd)->next_file_pos = off;
3580 /* Write out the program headers. */
3581 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
3582 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3583 return false;
3585 return true;
3588 /* Get the size of the program header.
3590 If this is called by the linker before any of the section VMA's are set, it
3591 can't calculate the correct value for a strange memory layout. This only
3592 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3593 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3594 data segment (exclusive of .interp and .dynamic).
3596 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3597 will be two segments. */
3599 static bfd_size_type
3600 get_program_header_size (abfd)
3601 bfd *abfd;
3603 size_t segs;
3604 asection *s;
3605 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3607 /* We can't return a different result each time we're called. */
3608 if (elf_tdata (abfd)->program_header_size != 0)
3609 return elf_tdata (abfd)->program_header_size;
3611 if (elf_tdata (abfd)->segment_map != NULL)
3613 struct elf_segment_map *m;
3615 segs = 0;
3616 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3617 ++segs;
3618 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3619 return elf_tdata (abfd)->program_header_size;
3622 /* Assume we will need exactly two PT_LOAD segments: one for text
3623 and one for data. */
3624 segs = 2;
3626 s = bfd_get_section_by_name (abfd, ".interp");
3627 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3629 /* If we have a loadable interpreter section, we need a
3630 PT_INTERP segment. In this case, assume we also need a
3631 PT_PHDR segment, although that may not be true for all
3632 targets. */
3633 segs += 2;
3636 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3638 /* We need a PT_DYNAMIC segment. */
3639 ++segs;
3642 if (elf_tdata (abfd)->eh_frame_hdr
3643 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
3645 /* We need a PT_GNU_EH_FRAME segment. */
3646 ++segs;
3649 for (s = abfd->sections; s != NULL; s = s->next)
3651 if ((s->flags & SEC_LOAD) != 0
3652 && strncmp (s->name, ".note", 5) == 0)
3654 /* We need a PT_NOTE segment. */
3655 ++segs;
3659 /* Let the backend count up any program headers it might need. */
3660 if (bed->elf_backend_additional_program_headers)
3662 int a;
3664 a = (*bed->elf_backend_additional_program_headers) (abfd);
3665 if (a == -1)
3666 abort ();
3667 segs += a;
3670 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3671 return elf_tdata (abfd)->program_header_size;
3674 /* Work out the file positions of all the sections. This is called by
3675 _bfd_elf_compute_section_file_positions. All the section sizes and
3676 VMAs must be known before this is called.
3678 We do not consider reloc sections at this point, unless they form
3679 part of the loadable image. Reloc sections are assigned file
3680 positions in assign_file_positions_for_relocs, which is called by
3681 write_object_contents and final_link.
3683 We also don't set the positions of the .symtab and .strtab here. */
3685 static boolean
3686 assign_file_positions_except_relocs (abfd)
3687 bfd *abfd;
3689 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3690 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3691 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3692 unsigned int num_sec = elf_numsections (abfd);
3693 file_ptr off;
3694 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3696 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3697 && bfd_get_format (abfd) != bfd_core)
3699 Elf_Internal_Shdr **hdrpp;
3700 unsigned int i;
3702 /* Start after the ELF header. */
3703 off = i_ehdrp->e_ehsize;
3705 /* We are not creating an executable, which means that we are
3706 not creating a program header, and that the actual order of
3707 the sections in the file is unimportant. */
3708 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3710 Elf_Internal_Shdr *hdr;
3712 hdr = *hdrpp;
3713 if (hdr->sh_type == SHT_REL
3714 || hdr->sh_type == SHT_RELA
3715 || i == tdata->symtab_section
3716 || i == tdata->symtab_shndx_section
3717 || i == tdata->strtab_section)
3719 hdr->sh_offset = -1;
3721 else
3722 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3724 if (i == SHN_LORESERVE - 1)
3726 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3727 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3731 else
3733 unsigned int i;
3734 Elf_Internal_Shdr **hdrpp;
3736 /* Assign file positions for the loaded sections based on the
3737 assignment of sections to segments. */
3738 if (! assign_file_positions_for_segments (abfd))
3739 return false;
3741 /* Assign file positions for the other sections. */
3743 off = elf_tdata (abfd)->next_file_pos;
3744 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3746 Elf_Internal_Shdr *hdr;
3748 hdr = *hdrpp;
3749 if (hdr->bfd_section != NULL
3750 && hdr->bfd_section->filepos != 0)
3751 hdr->sh_offset = hdr->bfd_section->filepos;
3752 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3754 ((*_bfd_error_handler)
3755 (_("%s: warning: allocated section `%s' not in segment"),
3756 bfd_get_filename (abfd),
3757 (hdr->bfd_section == NULL
3758 ? "*unknown*"
3759 : hdr->bfd_section->name)));
3760 if ((abfd->flags & D_PAGED) != 0)
3761 off += (hdr->sh_addr - off) % bed->maxpagesize;
3762 else
3763 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3764 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3765 false);
3767 else if (hdr->sh_type == SHT_REL
3768 || hdr->sh_type == SHT_RELA
3769 || hdr == i_shdrpp[tdata->symtab_section]
3770 || hdr == i_shdrpp[tdata->symtab_shndx_section]
3771 || hdr == i_shdrpp[tdata->strtab_section])
3772 hdr->sh_offset = -1;
3773 else
3774 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3776 if (i == SHN_LORESERVE - 1)
3778 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3779 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3784 /* Place the section headers. */
3785 off = align_file_position (off, bed->s->file_align);
3786 i_ehdrp->e_shoff = off;
3787 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3789 elf_tdata (abfd)->next_file_pos = off;
3791 return true;
3794 static boolean
3795 prep_headers (abfd)
3796 bfd *abfd;
3798 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3799 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3800 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3801 int count;
3802 struct elf_strtab_hash *shstrtab;
3803 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3805 i_ehdrp = elf_elfheader (abfd);
3806 i_shdrp = elf_elfsections (abfd);
3808 shstrtab = _bfd_elf_strtab_init ();
3809 if (shstrtab == NULL)
3810 return false;
3812 elf_shstrtab (abfd) = shstrtab;
3814 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3815 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3816 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3817 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3819 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3820 i_ehdrp->e_ident[EI_DATA] =
3821 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3822 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3824 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3825 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3827 for (count = EI_PAD; count < EI_NIDENT; count++)
3828 i_ehdrp->e_ident[count] = 0;
3830 if ((abfd->flags & DYNAMIC) != 0)
3831 i_ehdrp->e_type = ET_DYN;
3832 else if ((abfd->flags & EXEC_P) != 0)
3833 i_ehdrp->e_type = ET_EXEC;
3834 else if (bfd_get_format (abfd) == bfd_core)
3835 i_ehdrp->e_type = ET_CORE;
3836 else
3837 i_ehdrp->e_type = ET_REL;
3839 switch (bfd_get_arch (abfd))
3841 case bfd_arch_unknown:
3842 i_ehdrp->e_machine = EM_NONE;
3843 break;
3845 /* There used to be a long list of cases here, each one setting
3846 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3847 in the corresponding bfd definition. To avoid duplication,
3848 the switch was removed. Machines that need special handling
3849 can generally do it in elf_backend_final_write_processing(),
3850 unless they need the information earlier than the final write.
3851 Such need can generally be supplied by replacing the tests for
3852 e_machine with the conditions used to determine it. */
3853 default:
3854 if (get_elf_backend_data (abfd) != NULL)
3855 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
3856 else
3857 i_ehdrp->e_machine = EM_NONE;
3860 i_ehdrp->e_version = bed->s->ev_current;
3861 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3863 /* No program header, for now. */
3864 i_ehdrp->e_phoff = 0;
3865 i_ehdrp->e_phentsize = 0;
3866 i_ehdrp->e_phnum = 0;
3868 /* Each bfd section is section header entry. */
3869 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3870 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3872 /* If we're building an executable, we'll need a program header table. */
3873 if (abfd->flags & EXEC_P)
3875 /* It all happens later. */
3876 #if 0
3877 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3879 /* elf_build_phdrs() returns a (NULL-terminated) array of
3880 Elf_Internal_Phdrs. */
3881 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3882 i_ehdrp->e_phoff = outbase;
3883 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3884 #endif
3886 else
3888 i_ehdrp->e_phentsize = 0;
3889 i_phdrp = 0;
3890 i_ehdrp->e_phoff = 0;
3893 elf_tdata (abfd)->symtab_hdr.sh_name =
3894 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
3895 elf_tdata (abfd)->strtab_hdr.sh_name =
3896 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
3897 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3898 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
3899 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3900 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3901 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3902 return false;
3904 return true;
3907 /* Assign file positions for all the reloc sections which are not part
3908 of the loadable file image. */
3910 void
3911 _bfd_elf_assign_file_positions_for_relocs (abfd)
3912 bfd *abfd;
3914 file_ptr off;
3915 unsigned int i, num_sec;
3916 Elf_Internal_Shdr **shdrpp;
3918 off = elf_tdata (abfd)->next_file_pos;
3920 num_sec = elf_numsections (abfd);
3921 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
3923 Elf_Internal_Shdr *shdrp;
3925 shdrp = *shdrpp;
3926 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3927 && shdrp->sh_offset == -1)
3928 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3931 elf_tdata (abfd)->next_file_pos = off;
3934 boolean
3935 _bfd_elf_write_object_contents (abfd)
3936 bfd *abfd;
3938 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3939 Elf_Internal_Ehdr *i_ehdrp;
3940 Elf_Internal_Shdr **i_shdrp;
3941 boolean failed;
3942 unsigned int count, num_sec;
3944 if (! abfd->output_has_begun
3945 && ! _bfd_elf_compute_section_file_positions
3946 (abfd, (struct bfd_link_info *) NULL))
3947 return false;
3949 i_shdrp = elf_elfsections (abfd);
3950 i_ehdrp = elf_elfheader (abfd);
3952 failed = false;
3953 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3954 if (failed)
3955 return false;
3957 _bfd_elf_assign_file_positions_for_relocs (abfd);
3959 /* After writing the headers, we need to write the sections too... */
3960 num_sec = elf_numsections (abfd);
3961 for (count = 1; count < num_sec; count++)
3963 if (bed->elf_backend_section_processing)
3964 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3965 if (i_shdrp[count]->contents)
3967 bfd_size_type amt = i_shdrp[count]->sh_size;
3969 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3970 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
3971 return false;
3973 if (count == SHN_LORESERVE - 1)
3974 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3977 /* Write out the section header names. */
3978 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3979 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
3980 return false;
3982 if (bed->elf_backend_final_write_processing)
3983 (*bed->elf_backend_final_write_processing) (abfd,
3984 elf_tdata (abfd)->linker);
3986 return bed->s->write_shdrs_and_ehdr (abfd);
3989 boolean
3990 _bfd_elf_write_corefile_contents (abfd)
3991 bfd *abfd;
3993 /* Hopefully this can be done just like an object file. */
3994 return _bfd_elf_write_object_contents (abfd);
3997 /* Given a section, search the header to find them. */
4000 _bfd_elf_section_from_bfd_section (abfd, asect)
4001 bfd *abfd;
4002 struct sec *asect;
4004 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4005 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4006 int index;
4007 Elf_Internal_Shdr *hdr;
4008 int maxindex = elf_numsections (abfd);
4010 if (elf_section_data (asect) != NULL
4011 && elf_section_data (asect)->this_idx != 0)
4012 return elf_section_data (asect)->this_idx;
4014 if (bfd_is_abs_section (asect))
4015 return SHN_ABS;
4016 if (bfd_is_com_section (asect))
4017 return SHN_COMMON;
4018 if (bfd_is_und_section (asect))
4019 return SHN_UNDEF;
4021 for (index = 1; index < maxindex; index++)
4023 hdr = i_shdrp[index];
4024 if (hdr != NULL && hdr->bfd_section == asect)
4025 return index;
4028 if (bed->elf_backend_section_from_bfd_section)
4030 for (index = 0; index < maxindex; index++)
4032 int retval;
4034 hdr = i_shdrp[index];
4035 if (hdr == NULL)
4036 continue;
4038 retval = index;
4039 if ((*bed->elf_backend_section_from_bfd_section)
4040 (abfd, hdr, asect, &retval))
4041 return retval;
4045 bfd_set_error (bfd_error_nonrepresentable_section);
4047 return SHN_BAD;
4050 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4051 on error. */
4054 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4055 bfd *abfd;
4056 asymbol **asym_ptr_ptr;
4058 asymbol *asym_ptr = *asym_ptr_ptr;
4059 int idx;
4060 flagword flags = asym_ptr->flags;
4062 /* When gas creates relocations against local labels, it creates its
4063 own symbol for the section, but does put the symbol into the
4064 symbol chain, so udata is 0. When the linker is generating
4065 relocatable output, this section symbol may be for one of the
4066 input sections rather than the output section. */
4067 if (asym_ptr->udata.i == 0
4068 && (flags & BSF_SECTION_SYM)
4069 && asym_ptr->section)
4071 int indx;
4073 if (asym_ptr->section->output_section != NULL)
4074 indx = asym_ptr->section->output_section->index;
4075 else
4076 indx = asym_ptr->section->index;
4077 if (indx < elf_num_section_syms (abfd)
4078 && elf_section_syms (abfd)[indx] != NULL)
4079 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4082 idx = asym_ptr->udata.i;
4084 if (idx == 0)
4086 /* This case can occur when using --strip-symbol on a symbol
4087 which is used in a relocation entry. */
4088 (*_bfd_error_handler)
4089 (_("%s: symbol `%s' required but not present"),
4090 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4091 bfd_set_error (bfd_error_no_symbols);
4092 return -1;
4095 #if DEBUG & 4
4097 fprintf (stderr,
4098 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
4099 (long) asym_ptr, asym_ptr->name, idx, flags,
4100 elf_symbol_flags (flags));
4101 fflush (stderr);
4103 #endif
4105 return idx;
4108 /* Copy private BFD data. This copies any program header information. */
4110 static boolean
4111 copy_private_bfd_data (ibfd, obfd)
4112 bfd *ibfd;
4113 bfd *obfd;
4115 Elf_Internal_Ehdr * iehdr;
4116 struct elf_segment_map * map;
4117 struct elf_segment_map * map_first;
4118 struct elf_segment_map ** pointer_to_map;
4119 Elf_Internal_Phdr * segment;
4120 asection * section;
4121 unsigned int i;
4122 unsigned int num_segments;
4123 boolean phdr_included = false;
4124 bfd_vma maxpagesize;
4125 struct elf_segment_map * phdr_adjust_seg = NULL;
4126 unsigned int phdr_adjust_num = 0;
4128 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4129 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4130 return true;
4132 if (elf_tdata (ibfd)->phdr == NULL)
4133 return true;
4135 iehdr = elf_elfheader (ibfd);
4137 map_first = NULL;
4138 pointer_to_map = &map_first;
4140 num_segments = elf_elfheader (ibfd)->e_phnum;
4141 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4143 /* Returns the end address of the segment + 1. */
4144 #define SEGMENT_END(segment, start) \
4145 (start + (segment->p_memsz > segment->p_filesz \
4146 ? segment->p_memsz : segment->p_filesz))
4148 /* Returns true if the given section is contained within
4149 the given segment. VMA addresses are compared. */
4150 #define IS_CONTAINED_BY_VMA(section, segment) \
4151 (section->vma >= segment->p_vaddr \
4152 && (section->vma + section->_raw_size) \
4153 <= (SEGMENT_END (segment, segment->p_vaddr)))
4155 /* Returns true if the given section is contained within
4156 the given segment. LMA addresses are compared. */
4157 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4158 (section->lma >= base \
4159 && (section->lma + section->_raw_size) \
4160 <= SEGMENT_END (segment, base))
4162 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4163 #define IS_COREFILE_NOTE(p, s) \
4164 (p->p_type == PT_NOTE \
4165 && bfd_get_format (ibfd) == bfd_core \
4166 && s->vma == 0 && s->lma == 0 \
4167 && (bfd_vma) s->filepos >= p->p_offset \
4168 && (bfd_vma) s->filepos + s->_raw_size \
4169 <= p->p_offset + p->p_filesz)
4171 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4172 linker, which generates a PT_INTERP section with p_vaddr and
4173 p_memsz set to 0. */
4174 #define IS_SOLARIS_PT_INTERP(p, s) \
4175 ( p->p_vaddr == 0 \
4176 && p->p_filesz > 0 \
4177 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4178 && s->_raw_size > 0 \
4179 && (bfd_vma) s->filepos >= p->p_offset \
4180 && ((bfd_vma) s->filepos + s->_raw_size \
4181 <= p->p_offset + p->p_filesz))
4183 /* Decide if the given section should be included in the given segment.
4184 A section will be included if:
4185 1. It is within the address space of the segment -- we use the LMA
4186 if that is set for the segment and the VMA otherwise,
4187 2. It is an allocated segment,
4188 3. There is an output section associated with it,
4189 4. The section has not already been allocated to a previous segment. */
4190 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4191 (((((segment->p_paddr \
4192 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4193 : IS_CONTAINED_BY_VMA (section, segment)) \
4194 || IS_SOLARIS_PT_INTERP (segment, section)) \
4195 && (section->flags & SEC_ALLOC) != 0) \
4196 || IS_COREFILE_NOTE (segment, section)) \
4197 && section->output_section != NULL \
4198 && section->segment_mark == false)
4200 /* Returns true iff seg1 starts after the end of seg2. */
4201 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4202 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4204 /* Returns true iff seg1 and seg2 overlap. */
4205 #define SEGMENT_OVERLAPS(seg1, seg2) \
4206 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4208 /* Initialise the segment mark field. */
4209 for (section = ibfd->sections; section != NULL; section = section->next)
4210 section->segment_mark = false;
4212 /* Scan through the segments specified in the program header
4213 of the input BFD. For this first scan we look for overlaps
4214 in the loadable segments. These can be created by weird
4215 parameters to objcopy. */
4216 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4217 i < num_segments;
4218 i++, segment++)
4220 unsigned int j;
4221 Elf_Internal_Phdr *segment2;
4223 if (segment->p_type != PT_LOAD)
4224 continue;
4226 /* Determine if this segment overlaps any previous segments. */
4227 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4229 bfd_signed_vma extra_length;
4231 if (segment2->p_type != PT_LOAD
4232 || ! SEGMENT_OVERLAPS (segment, segment2))
4233 continue;
4235 /* Merge the two segments together. */
4236 if (segment2->p_vaddr < segment->p_vaddr)
4238 /* Extend SEGMENT2 to include SEGMENT and then delete
4239 SEGMENT. */
4240 extra_length =
4241 SEGMENT_END (segment, segment->p_vaddr)
4242 - SEGMENT_END (segment2, segment2->p_vaddr);
4244 if (extra_length > 0)
4246 segment2->p_memsz += extra_length;
4247 segment2->p_filesz += extra_length;
4250 segment->p_type = PT_NULL;
4252 /* Since we have deleted P we must restart the outer loop. */
4253 i = 0;
4254 segment = elf_tdata (ibfd)->phdr;
4255 break;
4257 else
4259 /* Extend SEGMENT to include SEGMENT2 and then delete
4260 SEGMENT2. */
4261 extra_length =
4262 SEGMENT_END (segment2, segment2->p_vaddr)
4263 - SEGMENT_END (segment, segment->p_vaddr);
4265 if (extra_length > 0)
4267 segment->p_memsz += extra_length;
4268 segment->p_filesz += extra_length;
4271 segment2->p_type = PT_NULL;
4276 /* The second scan attempts to assign sections to segments. */
4277 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4278 i < num_segments;
4279 i ++, segment ++)
4281 unsigned int section_count;
4282 asection ** sections;
4283 asection * output_section;
4284 unsigned int isec;
4285 bfd_vma matching_lma;
4286 bfd_vma suggested_lma;
4287 unsigned int j;
4288 bfd_size_type amt;
4290 if (segment->p_type == PT_NULL)
4291 continue;
4293 /* Compute how many sections might be placed into this segment. */
4294 section_count = 0;
4295 for (section = ibfd->sections; section != NULL; section = section->next)
4296 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4297 ++section_count;
4299 /* Allocate a segment map big enough to contain all of the
4300 sections we have selected. */
4301 amt = sizeof (struct elf_segment_map);
4302 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4303 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4304 if (map == NULL)
4305 return false;
4307 /* Initialise the fields of the segment map. Default to
4308 using the physical address of the segment in the input BFD. */
4309 map->next = NULL;
4310 map->p_type = segment->p_type;
4311 map->p_flags = segment->p_flags;
4312 map->p_flags_valid = 1;
4313 map->p_paddr = segment->p_paddr;
4314 map->p_paddr_valid = 1;
4316 /* Determine if this segment contains the ELF file header
4317 and if it contains the program headers themselves. */
4318 map->includes_filehdr = (segment->p_offset == 0
4319 && segment->p_filesz >= iehdr->e_ehsize);
4321 map->includes_phdrs = 0;
4323 if (! phdr_included || segment->p_type != PT_LOAD)
4325 map->includes_phdrs =
4326 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4327 && (segment->p_offset + segment->p_filesz
4328 >= ((bfd_vma) iehdr->e_phoff
4329 + iehdr->e_phnum * iehdr->e_phentsize)));
4331 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4332 phdr_included = true;
4335 if (section_count == 0)
4337 /* Special segments, such as the PT_PHDR segment, may contain
4338 no sections, but ordinary, loadable segments should contain
4339 something. */
4340 if (segment->p_type == PT_LOAD)
4341 _bfd_error_handler
4342 (_("%s: warning: Empty loadable segment detected\n"),
4343 bfd_archive_filename (ibfd));
4345 map->count = 0;
4346 *pointer_to_map = map;
4347 pointer_to_map = &map->next;
4349 continue;
4352 /* Now scan the sections in the input BFD again and attempt
4353 to add their corresponding output sections to the segment map.
4354 The problem here is how to handle an output section which has
4355 been moved (ie had its LMA changed). There are four possibilities:
4357 1. None of the sections have been moved.
4358 In this case we can continue to use the segment LMA from the
4359 input BFD.
4361 2. All of the sections have been moved by the same amount.
4362 In this case we can change the segment's LMA to match the LMA
4363 of the first section.
4365 3. Some of the sections have been moved, others have not.
4366 In this case those sections which have not been moved can be
4367 placed in the current segment which will have to have its size,
4368 and possibly its LMA changed, and a new segment or segments will
4369 have to be created to contain the other sections.
4371 4. The sections have been moved, but not be the same amount.
4372 In this case we can change the segment's LMA to match the LMA
4373 of the first section and we will have to create a new segment
4374 or segments to contain the other sections.
4376 In order to save time, we allocate an array to hold the section
4377 pointers that we are interested in. As these sections get assigned
4378 to a segment, they are removed from this array. */
4380 amt = (bfd_size_type) section_count * sizeof (asection *);
4381 sections = (asection **) bfd_malloc (amt);
4382 if (sections == NULL)
4383 return false;
4385 /* Step One: Scan for segment vs section LMA conflicts.
4386 Also add the sections to the section array allocated above.
4387 Also add the sections to the current segment. In the common
4388 case, where the sections have not been moved, this means that
4389 we have completely filled the segment, and there is nothing
4390 more to do. */
4391 isec = 0;
4392 matching_lma = 0;
4393 suggested_lma = 0;
4395 for (j = 0, section = ibfd->sections;
4396 section != NULL;
4397 section = section->next)
4399 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4401 output_section = section->output_section;
4403 sections[j ++] = section;
4405 /* The Solaris native linker always sets p_paddr to 0.
4406 We try to catch that case here, and set it to the
4407 correct value. */
4408 if (segment->p_paddr == 0
4409 && segment->p_vaddr != 0
4410 && isec == 0
4411 && output_section->lma != 0
4412 && (output_section->vma == (segment->p_vaddr
4413 + (map->includes_filehdr
4414 ? iehdr->e_ehsize
4415 : 0)
4416 + (map->includes_phdrs
4417 ? (iehdr->e_phnum
4418 * iehdr->e_phentsize)
4419 : 0))))
4420 map->p_paddr = segment->p_vaddr;
4422 /* Match up the physical address of the segment with the
4423 LMA address of the output section. */
4424 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4425 || IS_COREFILE_NOTE (segment, section))
4427 if (matching_lma == 0)
4428 matching_lma = output_section->lma;
4430 /* We assume that if the section fits within the segment
4431 then it does not overlap any other section within that
4432 segment. */
4433 map->sections[isec ++] = output_section;
4435 else if (suggested_lma == 0)
4436 suggested_lma = output_section->lma;
4440 BFD_ASSERT (j == section_count);
4442 /* Step Two: Adjust the physical address of the current segment,
4443 if necessary. */
4444 if (isec == section_count)
4446 /* All of the sections fitted within the segment as currently
4447 specified. This is the default case. Add the segment to
4448 the list of built segments and carry on to process the next
4449 program header in the input BFD. */
4450 map->count = section_count;
4451 *pointer_to_map = map;
4452 pointer_to_map = &map->next;
4454 free (sections);
4455 continue;
4457 else
4459 if (matching_lma != 0)
4461 /* At least one section fits inside the current segment.
4462 Keep it, but modify its physical address to match the
4463 LMA of the first section that fitted. */
4464 map->p_paddr = matching_lma;
4466 else
4468 /* None of the sections fitted inside the current segment.
4469 Change the current segment's physical address to match
4470 the LMA of the first section. */
4471 map->p_paddr = suggested_lma;
4474 /* Offset the segment physical address from the lma
4475 to allow for space taken up by elf headers. */
4476 if (map->includes_filehdr)
4477 map->p_paddr -= iehdr->e_ehsize;
4479 if (map->includes_phdrs)
4481 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4483 /* iehdr->e_phnum is just an estimate of the number
4484 of program headers that we will need. Make a note
4485 here of the number we used and the segment we chose
4486 to hold these headers, so that we can adjust the
4487 offset when we know the correct value. */
4488 phdr_adjust_num = iehdr->e_phnum;
4489 phdr_adjust_seg = map;
4493 /* Step Three: Loop over the sections again, this time assigning
4494 those that fit to the current segment and remvoing them from the
4495 sections array; but making sure not to leave large gaps. Once all
4496 possible sections have been assigned to the current segment it is
4497 added to the list of built segments and if sections still remain
4498 to be assigned, a new segment is constructed before repeating
4499 the loop. */
4500 isec = 0;
4503 map->count = 0;
4504 suggested_lma = 0;
4506 /* Fill the current segment with sections that fit. */
4507 for (j = 0; j < section_count; j++)
4509 section = sections[j];
4511 if (section == NULL)
4512 continue;
4514 output_section = section->output_section;
4516 BFD_ASSERT (output_section != NULL);
4518 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4519 || IS_COREFILE_NOTE (segment, section))
4521 if (map->count == 0)
4523 /* If the first section in a segment does not start at
4524 the beginning of the segment, then something is
4525 wrong. */
4526 if (output_section->lma !=
4527 (map->p_paddr
4528 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4529 + (map->includes_phdrs
4530 ? iehdr->e_phnum * iehdr->e_phentsize
4531 : 0)))
4532 abort ();
4534 else
4536 asection * prev_sec;
4538 prev_sec = map->sections[map->count - 1];
4540 /* If the gap between the end of the previous section
4541 and the start of this section is more than
4542 maxpagesize then we need to start a new segment. */
4543 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4544 maxpagesize)
4545 < BFD_ALIGN (output_section->lma, maxpagesize))
4546 || ((prev_sec->lma + prev_sec->_raw_size)
4547 > output_section->lma))
4549 if (suggested_lma == 0)
4550 suggested_lma = output_section->lma;
4552 continue;
4556 map->sections[map->count++] = output_section;
4557 ++isec;
4558 sections[j] = NULL;
4559 section->segment_mark = true;
4561 else if (suggested_lma == 0)
4562 suggested_lma = output_section->lma;
4565 BFD_ASSERT (map->count > 0);
4567 /* Add the current segment to the list of built segments. */
4568 *pointer_to_map = map;
4569 pointer_to_map = &map->next;
4571 if (isec < section_count)
4573 /* We still have not allocated all of the sections to
4574 segments. Create a new segment here, initialise it
4575 and carry on looping. */
4576 amt = sizeof (struct elf_segment_map);
4577 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4578 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4579 if (map == NULL)
4580 return false;
4582 /* Initialise the fields of the segment map. Set the physical
4583 physical address to the LMA of the first section that has
4584 not yet been assigned. */
4585 map->next = NULL;
4586 map->p_type = segment->p_type;
4587 map->p_flags = segment->p_flags;
4588 map->p_flags_valid = 1;
4589 map->p_paddr = suggested_lma;
4590 map->p_paddr_valid = 1;
4591 map->includes_filehdr = 0;
4592 map->includes_phdrs = 0;
4595 while (isec < section_count);
4597 free (sections);
4600 /* The Solaris linker creates program headers in which all the
4601 p_paddr fields are zero. When we try to objcopy or strip such a
4602 file, we get confused. Check for this case, and if we find it
4603 reset the p_paddr_valid fields. */
4604 for (map = map_first; map != NULL; map = map->next)
4605 if (map->p_paddr != 0)
4606 break;
4607 if (map == NULL)
4609 for (map = map_first; map != NULL; map = map->next)
4610 map->p_paddr_valid = 0;
4613 elf_tdata (obfd)->segment_map = map_first;
4615 /* If we had to estimate the number of program headers that were
4616 going to be needed, then check our estimate now and adjust
4617 the offset if necessary. */
4618 if (phdr_adjust_seg != NULL)
4620 unsigned int count;
4622 for (count = 0, map = map_first; map != NULL; map = map->next)
4623 count++;
4625 if (count > phdr_adjust_num)
4626 phdr_adjust_seg->p_paddr
4627 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4630 #if 0
4631 /* Final Step: Sort the segments into ascending order of physical
4632 address. */
4633 if (map_first != NULL)
4635 struct elf_segment_map *prev;
4637 prev = map_first;
4638 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4640 /* Yes I know - its a bubble sort.... */
4641 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4643 /* Swap map and map->next. */
4644 prev->next = map->next;
4645 map->next = map->next->next;
4646 prev->next->next = map;
4648 /* Restart loop. */
4649 map = map_first;
4653 #endif
4655 #undef SEGMENT_END
4656 #undef IS_CONTAINED_BY_VMA
4657 #undef IS_CONTAINED_BY_LMA
4658 #undef IS_COREFILE_NOTE
4659 #undef IS_SOLARIS_PT_INTERP
4660 #undef INCLUDE_SECTION_IN_SEGMENT
4661 #undef SEGMENT_AFTER_SEGMENT
4662 #undef SEGMENT_OVERLAPS
4663 return true;
4666 /* Copy private section information. This copies over the entsize
4667 field, and sometimes the info field. */
4669 boolean
4670 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4671 bfd *ibfd;
4672 asection *isec;
4673 bfd *obfd;
4674 asection *osec;
4676 Elf_Internal_Shdr *ihdr, *ohdr;
4678 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4679 || obfd->xvec->flavour != bfd_target_elf_flavour)
4680 return true;
4682 /* Copy over private BFD data if it has not already been copied.
4683 This must be done here, rather than in the copy_private_bfd_data
4684 entry point, because the latter is called after the section
4685 contents have been set, which means that the program headers have
4686 already been worked out. */
4687 if (elf_tdata (obfd)->segment_map == NULL
4688 && elf_tdata (ibfd)->phdr != NULL)
4690 asection *s;
4692 /* Only set up the segments if there are no more SEC_ALLOC
4693 sections. FIXME: This won't do the right thing if objcopy is
4694 used to remove the last SEC_ALLOC section, since objcopy
4695 won't call this routine in that case. */
4696 for (s = isec->next; s != NULL; s = s->next)
4697 if ((s->flags & SEC_ALLOC) != 0)
4698 break;
4699 if (s == NULL)
4701 if (! copy_private_bfd_data (ibfd, obfd))
4702 return false;
4706 ihdr = &elf_section_data (isec)->this_hdr;
4707 ohdr = &elf_section_data (osec)->this_hdr;
4709 ohdr->sh_entsize = ihdr->sh_entsize;
4711 if (ihdr->sh_type == SHT_SYMTAB
4712 || ihdr->sh_type == SHT_DYNSYM
4713 || ihdr->sh_type == SHT_GNU_verneed
4714 || ihdr->sh_type == SHT_GNU_verdef)
4715 ohdr->sh_info = ihdr->sh_info;
4717 elf_section_data (osec)->use_rela_p
4718 = elf_section_data (isec)->use_rela_p;
4720 return true;
4723 /* Copy private symbol information. If this symbol is in a section
4724 which we did not map into a BFD section, try to map the section
4725 index correctly. We use special macro definitions for the mapped
4726 section indices; these definitions are interpreted by the
4727 swap_out_syms function. */
4729 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4730 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4731 #define MAP_STRTAB (SHN_HIOS + 3)
4732 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4733 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4735 boolean
4736 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4737 bfd *ibfd;
4738 asymbol *isymarg;
4739 bfd *obfd;
4740 asymbol *osymarg;
4742 elf_symbol_type *isym, *osym;
4744 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4745 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4746 return true;
4748 isym = elf_symbol_from (ibfd, isymarg);
4749 osym = elf_symbol_from (obfd, osymarg);
4751 if (isym != NULL
4752 && osym != NULL
4753 && bfd_is_abs_section (isym->symbol.section))
4755 unsigned int shndx;
4757 shndx = isym->internal_elf_sym.st_shndx;
4758 if (shndx == elf_onesymtab (ibfd))
4759 shndx = MAP_ONESYMTAB;
4760 else if (shndx == elf_dynsymtab (ibfd))
4761 shndx = MAP_DYNSYMTAB;
4762 else if (shndx == elf_tdata (ibfd)->strtab_section)
4763 shndx = MAP_STRTAB;
4764 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4765 shndx = MAP_SHSTRTAB;
4766 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
4767 shndx = MAP_SYM_SHNDX;
4768 osym->internal_elf_sym.st_shndx = shndx;
4771 return true;
4774 /* Swap out the symbols. */
4776 static boolean
4777 swap_out_syms (abfd, sttp, relocatable_p)
4778 bfd *abfd;
4779 struct bfd_strtab_hash **sttp;
4780 int relocatable_p;
4782 struct elf_backend_data *bed;
4783 int symcount;
4784 asymbol **syms;
4785 struct bfd_strtab_hash *stt;
4786 Elf_Internal_Shdr *symtab_hdr;
4787 Elf_Internal_Shdr *symtab_shndx_hdr;
4788 Elf_Internal_Shdr *symstrtab_hdr;
4789 char *outbound_syms;
4790 char *outbound_shndx;
4791 int idx;
4792 bfd_size_type amt;
4794 if (!elf_map_symbols (abfd))
4795 return false;
4797 /* Dump out the symtabs. */
4798 stt = _bfd_elf_stringtab_init ();
4799 if (stt == NULL)
4800 return false;
4802 bed = get_elf_backend_data (abfd);
4803 symcount = bfd_get_symcount (abfd);
4804 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4805 symtab_hdr->sh_type = SHT_SYMTAB;
4806 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4807 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4808 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4809 symtab_hdr->sh_addralign = bed->s->file_align;
4811 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4812 symstrtab_hdr->sh_type = SHT_STRTAB;
4814 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
4815 outbound_syms = bfd_alloc (abfd, amt);
4816 if (outbound_syms == NULL)
4817 return false;
4818 symtab_hdr->contents = (PTR) outbound_syms;
4820 outbound_shndx = NULL;
4821 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
4822 if (symtab_shndx_hdr->sh_name != 0)
4824 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
4825 outbound_shndx = bfd_alloc (abfd, amt);
4826 if (outbound_shndx == NULL)
4827 return false;
4828 memset (outbound_shndx, 0, (unsigned long) amt);
4829 symtab_shndx_hdr->contents = outbound_shndx;
4830 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
4831 symtab_shndx_hdr->sh_size = amt;
4832 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
4833 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
4836 /* now generate the data (for "contents") */
4838 /* Fill in zeroth symbol and swap it out. */
4839 Elf_Internal_Sym sym;
4840 sym.st_name = 0;
4841 sym.st_value = 0;
4842 sym.st_size = 0;
4843 sym.st_info = 0;
4844 sym.st_other = 0;
4845 sym.st_shndx = SHN_UNDEF;
4846 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
4847 outbound_syms += bed->s->sizeof_sym;
4848 if (outbound_shndx != NULL)
4849 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
4852 syms = bfd_get_outsymbols (abfd);
4853 for (idx = 0; idx < symcount; idx++)
4855 Elf_Internal_Sym sym;
4856 bfd_vma value = syms[idx]->value;
4857 elf_symbol_type *type_ptr;
4858 flagword flags = syms[idx]->flags;
4859 int type;
4861 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4863 /* Local section symbols have no name. */
4864 sym.st_name = 0;
4866 else
4868 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4869 syms[idx]->name,
4870 true, false);
4871 if (sym.st_name == (unsigned long) -1)
4872 return false;
4875 type_ptr = elf_symbol_from (abfd, syms[idx]);
4877 if ((flags & BSF_SECTION_SYM) == 0
4878 && bfd_is_com_section (syms[idx]->section))
4880 /* ELF common symbols put the alignment into the `value' field,
4881 and the size into the `size' field. This is backwards from
4882 how BFD handles it, so reverse it here. */
4883 sym.st_size = value;
4884 if (type_ptr == NULL
4885 || type_ptr->internal_elf_sym.st_value == 0)
4886 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4887 else
4888 sym.st_value = type_ptr->internal_elf_sym.st_value;
4889 sym.st_shndx = _bfd_elf_section_from_bfd_section
4890 (abfd, syms[idx]->section);
4892 else
4894 asection *sec = syms[idx]->section;
4895 int shndx;
4897 if (sec->output_section)
4899 value += sec->output_offset;
4900 sec = sec->output_section;
4902 /* Don't add in the section vma for relocatable output. */
4903 if (! relocatable_p)
4904 value += sec->vma;
4905 sym.st_value = value;
4906 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4908 if (bfd_is_abs_section (sec)
4909 && type_ptr != NULL
4910 && type_ptr->internal_elf_sym.st_shndx != 0)
4912 /* This symbol is in a real ELF section which we did
4913 not create as a BFD section. Undo the mapping done
4914 by copy_private_symbol_data. */
4915 shndx = type_ptr->internal_elf_sym.st_shndx;
4916 switch (shndx)
4918 case MAP_ONESYMTAB:
4919 shndx = elf_onesymtab (abfd);
4920 break;
4921 case MAP_DYNSYMTAB:
4922 shndx = elf_dynsymtab (abfd);
4923 break;
4924 case MAP_STRTAB:
4925 shndx = elf_tdata (abfd)->strtab_section;
4926 break;
4927 case MAP_SHSTRTAB:
4928 shndx = elf_tdata (abfd)->shstrtab_section;
4929 break;
4930 case MAP_SYM_SHNDX:
4931 shndx = elf_tdata (abfd)->symtab_shndx_section;
4932 break;
4933 default:
4934 break;
4937 else
4939 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4941 if (shndx == -1)
4943 asection *sec2;
4945 /* Writing this would be a hell of a lot easier if
4946 we had some decent documentation on bfd, and
4947 knew what to expect of the library, and what to
4948 demand of applications. For example, it
4949 appears that `objcopy' might not set the
4950 section of a symbol to be a section that is
4951 actually in the output file. */
4952 sec2 = bfd_get_section_by_name (abfd, sec->name);
4953 BFD_ASSERT (sec2 != 0);
4954 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4955 BFD_ASSERT (shndx != -1);
4959 sym.st_shndx = shndx;
4962 if ((flags & BSF_FUNCTION) != 0)
4963 type = STT_FUNC;
4964 else if ((flags & BSF_OBJECT) != 0)
4965 type = STT_OBJECT;
4966 else
4967 type = STT_NOTYPE;
4969 /* Processor-specific types */
4970 if (type_ptr != NULL
4971 && bed->elf_backend_get_symbol_type)
4972 type = ((*bed->elf_backend_get_symbol_type)
4973 (&type_ptr->internal_elf_sym, type));
4975 if (flags & BSF_SECTION_SYM)
4977 if (flags & BSF_GLOBAL)
4978 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
4979 else
4980 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4982 else if (bfd_is_com_section (syms[idx]->section))
4983 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4984 else if (bfd_is_und_section (syms[idx]->section))
4985 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4986 ? STB_WEAK
4987 : STB_GLOBAL),
4988 type);
4989 else if (flags & BSF_FILE)
4990 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4991 else
4993 int bind = STB_LOCAL;
4995 if (flags & BSF_LOCAL)
4996 bind = STB_LOCAL;
4997 else if (flags & BSF_WEAK)
4998 bind = STB_WEAK;
4999 else if (flags & BSF_GLOBAL)
5000 bind = STB_GLOBAL;
5002 sym.st_info = ELF_ST_INFO (bind, type);
5005 if (type_ptr != NULL)
5006 sym.st_other = type_ptr->internal_elf_sym.st_other;
5007 else
5008 sym.st_other = 0;
5010 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5011 outbound_syms += bed->s->sizeof_sym;
5012 if (outbound_shndx != NULL)
5013 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5016 *sttp = stt;
5017 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5018 symstrtab_hdr->sh_type = SHT_STRTAB;
5020 symstrtab_hdr->sh_flags = 0;
5021 symstrtab_hdr->sh_addr = 0;
5022 symstrtab_hdr->sh_entsize = 0;
5023 symstrtab_hdr->sh_link = 0;
5024 symstrtab_hdr->sh_info = 0;
5025 symstrtab_hdr->sh_addralign = 1;
5027 return true;
5030 /* Return the number of bytes required to hold the symtab vector.
5032 Note that we base it on the count plus 1, since we will null terminate
5033 the vector allocated based on this size. However, the ELF symbol table
5034 always has a dummy entry as symbol #0, so it ends up even. */
5036 long
5037 _bfd_elf_get_symtab_upper_bound (abfd)
5038 bfd *abfd;
5040 long symcount;
5041 long symtab_size;
5042 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5044 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5045 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5047 return symtab_size;
5050 long
5051 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5052 bfd *abfd;
5054 long symcount;
5055 long symtab_size;
5056 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5058 if (elf_dynsymtab (abfd) == 0)
5060 bfd_set_error (bfd_error_invalid_operation);
5061 return -1;
5064 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5065 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5067 return symtab_size;
5070 long
5071 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5072 bfd *abfd ATTRIBUTE_UNUSED;
5073 sec_ptr asect;
5075 return (asect->reloc_count + 1) * sizeof (arelent *);
5078 /* Canonicalize the relocs. */
5080 long
5081 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5082 bfd *abfd;
5083 sec_ptr section;
5084 arelent **relptr;
5085 asymbol **symbols;
5087 arelent *tblptr;
5088 unsigned int i;
5089 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5091 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
5092 return -1;
5094 tblptr = section->relocation;
5095 for (i = 0; i < section->reloc_count; i++)
5096 *relptr++ = tblptr++;
5098 *relptr = NULL;
5100 return section->reloc_count;
5103 long
5104 _bfd_elf_get_symtab (abfd, alocation)
5105 bfd *abfd;
5106 asymbol **alocation;
5108 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5109 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
5111 if (symcount >= 0)
5112 bfd_get_symcount (abfd) = symcount;
5113 return symcount;
5116 long
5117 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5118 bfd *abfd;
5119 asymbol **alocation;
5121 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5122 return bed->s->slurp_symbol_table (abfd, alocation, true);
5125 /* Return the size required for the dynamic reloc entries. Any
5126 section that was actually installed in the BFD, and has type
5127 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5128 considered to be a dynamic reloc section. */
5130 long
5131 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5132 bfd *abfd;
5134 long ret;
5135 asection *s;
5137 if (elf_dynsymtab (abfd) == 0)
5139 bfd_set_error (bfd_error_invalid_operation);
5140 return -1;
5143 ret = sizeof (arelent *);
5144 for (s = abfd->sections; s != NULL; s = s->next)
5145 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5146 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5147 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5148 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5149 * sizeof (arelent *));
5151 return ret;
5154 /* Canonicalize the dynamic relocation entries. Note that we return
5155 the dynamic relocations as a single block, although they are
5156 actually associated with particular sections; the interface, which
5157 was designed for SunOS style shared libraries, expects that there
5158 is only one set of dynamic relocs. Any section that was actually
5159 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5160 the dynamic symbol table, is considered to be a dynamic reloc
5161 section. */
5163 long
5164 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5165 bfd *abfd;
5166 arelent **storage;
5167 asymbol **syms;
5169 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5170 asection *s;
5171 long ret;
5173 if (elf_dynsymtab (abfd) == 0)
5175 bfd_set_error (bfd_error_invalid_operation);
5176 return -1;
5179 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5180 ret = 0;
5181 for (s = abfd->sections; s != NULL; s = s->next)
5183 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5184 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5185 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5187 arelent *p;
5188 long count, i;
5190 if (! (*slurp_relocs) (abfd, s, syms, true))
5191 return -1;
5192 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5193 p = s->relocation;
5194 for (i = 0; i < count; i++)
5195 *storage++ = p++;
5196 ret += count;
5200 *storage = NULL;
5202 return ret;
5205 /* Read in the version information. */
5207 boolean
5208 _bfd_elf_slurp_version_tables (abfd)
5209 bfd *abfd;
5211 bfd_byte *contents = NULL;
5212 bfd_size_type amt;
5214 if (elf_dynverdef (abfd) != 0)
5216 Elf_Internal_Shdr *hdr;
5217 Elf_External_Verdef *everdef;
5218 Elf_Internal_Verdef *iverdef;
5219 Elf_Internal_Verdef *iverdefarr;
5220 Elf_Internal_Verdef iverdefmem;
5221 unsigned int i;
5222 unsigned int maxidx;
5224 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5226 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5227 if (contents == NULL)
5228 goto error_return;
5229 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5230 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5231 goto error_return;
5233 /* We know the number of entries in the section but not the maximum
5234 index. Therefore we have to run through all entries and find
5235 the maximum. */
5236 everdef = (Elf_External_Verdef *) contents;
5237 maxidx = 0;
5238 for (i = 0; i < hdr->sh_info; ++i)
5240 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5242 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5243 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5245 everdef = ((Elf_External_Verdef *)
5246 ((bfd_byte *) everdef + iverdefmem.vd_next));
5249 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5250 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5251 if (elf_tdata (abfd)->verdef == NULL)
5252 goto error_return;
5254 elf_tdata (abfd)->cverdefs = maxidx;
5256 everdef = (Elf_External_Verdef *) contents;
5257 iverdefarr = elf_tdata (abfd)->verdef;
5258 for (i = 0; i < hdr->sh_info; i++)
5260 Elf_External_Verdaux *everdaux;
5261 Elf_Internal_Verdaux *iverdaux;
5262 unsigned int j;
5264 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5266 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5267 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5269 iverdef->vd_bfd = abfd;
5271 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5272 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5273 if (iverdef->vd_auxptr == NULL)
5274 goto error_return;
5276 everdaux = ((Elf_External_Verdaux *)
5277 ((bfd_byte *) everdef + iverdef->vd_aux));
5278 iverdaux = iverdef->vd_auxptr;
5279 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5281 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5283 iverdaux->vda_nodename =
5284 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5285 iverdaux->vda_name);
5286 if (iverdaux->vda_nodename == NULL)
5287 goto error_return;
5289 if (j + 1 < iverdef->vd_cnt)
5290 iverdaux->vda_nextptr = iverdaux + 1;
5291 else
5292 iverdaux->vda_nextptr = NULL;
5294 everdaux = ((Elf_External_Verdaux *)
5295 ((bfd_byte *) everdaux + iverdaux->vda_next));
5298 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5300 if (i + 1 < hdr->sh_info)
5301 iverdef->vd_nextdef = iverdef + 1;
5302 else
5303 iverdef->vd_nextdef = NULL;
5305 everdef = ((Elf_External_Verdef *)
5306 ((bfd_byte *) everdef + iverdef->vd_next));
5309 free (contents);
5310 contents = NULL;
5313 if (elf_dynverref (abfd) != 0)
5315 Elf_Internal_Shdr *hdr;
5316 Elf_External_Verneed *everneed;
5317 Elf_Internal_Verneed *iverneed;
5318 unsigned int i;
5320 hdr = &elf_tdata (abfd)->dynverref_hdr;
5322 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5323 elf_tdata (abfd)->verref =
5324 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5325 if (elf_tdata (abfd)->verref == NULL)
5326 goto error_return;
5328 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5330 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5331 if (contents == NULL)
5332 goto error_return;
5333 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5334 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5335 goto error_return;
5337 everneed = (Elf_External_Verneed *) contents;
5338 iverneed = elf_tdata (abfd)->verref;
5339 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5341 Elf_External_Vernaux *evernaux;
5342 Elf_Internal_Vernaux *ivernaux;
5343 unsigned int j;
5345 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5347 iverneed->vn_bfd = abfd;
5349 iverneed->vn_filename =
5350 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5351 iverneed->vn_file);
5352 if (iverneed->vn_filename == NULL)
5353 goto error_return;
5355 amt = iverneed->vn_cnt;
5356 amt *= sizeof (Elf_Internal_Vernaux);
5357 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5359 evernaux = ((Elf_External_Vernaux *)
5360 ((bfd_byte *) everneed + iverneed->vn_aux));
5361 ivernaux = iverneed->vn_auxptr;
5362 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5364 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5366 ivernaux->vna_nodename =
5367 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5368 ivernaux->vna_name);
5369 if (ivernaux->vna_nodename == NULL)
5370 goto error_return;
5372 if (j + 1 < iverneed->vn_cnt)
5373 ivernaux->vna_nextptr = ivernaux + 1;
5374 else
5375 ivernaux->vna_nextptr = NULL;
5377 evernaux = ((Elf_External_Vernaux *)
5378 ((bfd_byte *) evernaux + ivernaux->vna_next));
5381 if (i + 1 < hdr->sh_info)
5382 iverneed->vn_nextref = iverneed + 1;
5383 else
5384 iverneed->vn_nextref = NULL;
5386 everneed = ((Elf_External_Verneed *)
5387 ((bfd_byte *) everneed + iverneed->vn_next));
5390 free (contents);
5391 contents = NULL;
5394 return true;
5396 error_return:
5397 if (contents == NULL)
5398 free (contents);
5399 return false;
5402 asymbol *
5403 _bfd_elf_make_empty_symbol (abfd)
5404 bfd *abfd;
5406 elf_symbol_type *newsym;
5407 bfd_size_type amt = sizeof (elf_symbol_type);
5409 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5410 if (!newsym)
5411 return NULL;
5412 else
5414 newsym->symbol.the_bfd = abfd;
5415 return &newsym->symbol;
5419 void
5420 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5421 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5422 asymbol *symbol;
5423 symbol_info *ret;
5425 bfd_symbol_info (symbol, ret);
5428 /* Return whether a symbol name implies a local symbol. Most targets
5429 use this function for the is_local_label_name entry point, but some
5430 override it. */
5432 boolean
5433 _bfd_elf_is_local_label_name (abfd, name)
5434 bfd *abfd ATTRIBUTE_UNUSED;
5435 const char *name;
5437 /* Normal local symbols start with ``.L''. */
5438 if (name[0] == '.' && name[1] == 'L')
5439 return true;
5441 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5442 DWARF debugging symbols starting with ``..''. */
5443 if (name[0] == '.' && name[1] == '.')
5444 return true;
5446 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5447 emitting DWARF debugging output. I suspect this is actually a
5448 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5449 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5450 underscore to be emitted on some ELF targets). For ease of use,
5451 we treat such symbols as local. */
5452 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5453 return true;
5455 return false;
5458 alent *
5459 _bfd_elf_get_lineno (ignore_abfd, symbol)
5460 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5461 asymbol *symbol ATTRIBUTE_UNUSED;
5463 abort ();
5464 return NULL;
5467 boolean
5468 _bfd_elf_set_arch_mach (abfd, arch, machine)
5469 bfd *abfd;
5470 enum bfd_architecture arch;
5471 unsigned long machine;
5473 /* If this isn't the right architecture for this backend, and this
5474 isn't the generic backend, fail. */
5475 if (arch != get_elf_backend_data (abfd)->arch
5476 && arch != bfd_arch_unknown
5477 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5478 return false;
5480 return bfd_default_set_arch_mach (abfd, arch, machine);
5483 /* Find the function to a particular section and offset,
5484 for error reporting. */
5486 static boolean
5487 elf_find_function (abfd, section, symbols, offset,
5488 filename_ptr, functionname_ptr)
5489 bfd *abfd ATTRIBUTE_UNUSED;
5490 asection *section;
5491 asymbol **symbols;
5492 bfd_vma offset;
5493 const char **filename_ptr;
5494 const char **functionname_ptr;
5496 const char *filename;
5497 asymbol *func;
5498 bfd_vma low_func;
5499 asymbol **p;
5501 filename = NULL;
5502 func = NULL;
5503 low_func = 0;
5505 for (p = symbols; *p != NULL; p++)
5507 elf_symbol_type *q;
5509 q = (elf_symbol_type *) *p;
5511 if (bfd_get_section (&q->symbol) != section)
5512 continue;
5514 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5516 default:
5517 break;
5518 case STT_FILE:
5519 filename = bfd_asymbol_name (&q->symbol);
5520 break;
5521 case STT_NOTYPE:
5522 case STT_FUNC:
5523 if (q->symbol.section == section
5524 && q->symbol.value >= low_func
5525 && q->symbol.value <= offset)
5527 func = (asymbol *) q;
5528 low_func = q->symbol.value;
5530 break;
5534 if (func == NULL)
5535 return false;
5537 if (filename_ptr)
5538 *filename_ptr = filename;
5539 if (functionname_ptr)
5540 *functionname_ptr = bfd_asymbol_name (func);
5542 return true;
5545 /* Find the nearest line to a particular section and offset,
5546 for error reporting. */
5548 boolean
5549 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5550 filename_ptr, functionname_ptr, line_ptr)
5551 bfd *abfd;
5552 asection *section;
5553 asymbol **symbols;
5554 bfd_vma offset;
5555 const char **filename_ptr;
5556 const char **functionname_ptr;
5557 unsigned int *line_ptr;
5559 boolean found;
5561 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5562 filename_ptr, functionname_ptr,
5563 line_ptr))
5565 if (!*functionname_ptr)
5566 elf_find_function (abfd, section, symbols, offset,
5567 *filename_ptr ? NULL : filename_ptr,
5568 functionname_ptr);
5570 return true;
5573 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5574 filename_ptr, functionname_ptr,
5575 line_ptr, 0,
5576 &elf_tdata (abfd)->dwarf2_find_line_info))
5578 if (!*functionname_ptr)
5579 elf_find_function (abfd, section, symbols, offset,
5580 *filename_ptr ? NULL : filename_ptr,
5581 functionname_ptr);
5583 return true;
5586 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5587 &found, filename_ptr,
5588 functionname_ptr, line_ptr,
5589 &elf_tdata (abfd)->line_info))
5590 return false;
5591 if (found)
5592 return true;
5594 if (symbols == NULL)
5595 return false;
5597 if (! elf_find_function (abfd, section, symbols, offset,
5598 filename_ptr, functionname_ptr))
5599 return false;
5601 *line_ptr = 0;
5602 return true;
5606 _bfd_elf_sizeof_headers (abfd, reloc)
5607 bfd *abfd;
5608 boolean reloc;
5610 int ret;
5612 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5613 if (! reloc)
5614 ret += get_program_header_size (abfd);
5615 return ret;
5618 boolean
5619 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5620 bfd *abfd;
5621 sec_ptr section;
5622 PTR location;
5623 file_ptr offset;
5624 bfd_size_type count;
5626 Elf_Internal_Shdr *hdr;
5627 bfd_signed_vma pos;
5629 if (! abfd->output_has_begun
5630 && ! _bfd_elf_compute_section_file_positions
5631 (abfd, (struct bfd_link_info *) NULL))
5632 return false;
5634 hdr = &elf_section_data (section)->this_hdr;
5635 pos = hdr->sh_offset + offset;
5636 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5637 || bfd_bwrite (location, count, abfd) != count)
5638 return false;
5640 return true;
5643 void
5644 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5645 bfd *abfd ATTRIBUTE_UNUSED;
5646 arelent *cache_ptr ATTRIBUTE_UNUSED;
5647 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5649 abort ();
5652 #if 0
5653 void
5654 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5655 bfd *abfd;
5656 arelent *cache_ptr;
5657 Elf_Internal_Rel *dst;
5659 abort ();
5661 #endif
5663 /* Try to convert a non-ELF reloc into an ELF one. */
5665 boolean
5666 _bfd_elf_validate_reloc (abfd, areloc)
5667 bfd *abfd;
5668 arelent *areloc;
5670 /* Check whether we really have an ELF howto. */
5672 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5674 bfd_reloc_code_real_type code;
5675 reloc_howto_type *howto;
5677 /* Alien reloc: Try to determine its type to replace it with an
5678 equivalent ELF reloc. */
5680 if (areloc->howto->pc_relative)
5682 switch (areloc->howto->bitsize)
5684 case 8:
5685 code = BFD_RELOC_8_PCREL;
5686 break;
5687 case 12:
5688 code = BFD_RELOC_12_PCREL;
5689 break;
5690 case 16:
5691 code = BFD_RELOC_16_PCREL;
5692 break;
5693 case 24:
5694 code = BFD_RELOC_24_PCREL;
5695 break;
5696 case 32:
5697 code = BFD_RELOC_32_PCREL;
5698 break;
5699 case 64:
5700 code = BFD_RELOC_64_PCREL;
5701 break;
5702 default:
5703 goto fail;
5706 howto = bfd_reloc_type_lookup (abfd, code);
5708 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5710 if (howto->pcrel_offset)
5711 areloc->addend += areloc->address;
5712 else
5713 areloc->addend -= areloc->address; /* addend is unsigned!! */
5716 else
5718 switch (areloc->howto->bitsize)
5720 case 8:
5721 code = BFD_RELOC_8;
5722 break;
5723 case 14:
5724 code = BFD_RELOC_14;
5725 break;
5726 case 16:
5727 code = BFD_RELOC_16;
5728 break;
5729 case 26:
5730 code = BFD_RELOC_26;
5731 break;
5732 case 32:
5733 code = BFD_RELOC_32;
5734 break;
5735 case 64:
5736 code = BFD_RELOC_64;
5737 break;
5738 default:
5739 goto fail;
5742 howto = bfd_reloc_type_lookup (abfd, code);
5745 if (howto)
5746 areloc->howto = howto;
5747 else
5748 goto fail;
5751 return true;
5753 fail:
5754 (*_bfd_error_handler)
5755 (_("%s: unsupported relocation type %s"),
5756 bfd_archive_filename (abfd), areloc->howto->name);
5757 bfd_set_error (bfd_error_bad_value);
5758 return false;
5761 boolean
5762 _bfd_elf_close_and_cleanup (abfd)
5763 bfd *abfd;
5765 if (bfd_get_format (abfd) == bfd_object)
5767 if (elf_shstrtab (abfd) != NULL)
5768 _bfd_elf_strtab_free (elf_shstrtab (abfd));
5771 return _bfd_generic_close_and_cleanup (abfd);
5774 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5775 in the relocation's offset. Thus we cannot allow any sort of sanity
5776 range-checking to interfere. There is nothing else to do in processing
5777 this reloc. */
5779 bfd_reloc_status_type
5780 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5781 bfd *abfd ATTRIBUTE_UNUSED;
5782 arelent *re ATTRIBUTE_UNUSED;
5783 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5784 PTR data ATTRIBUTE_UNUSED;
5785 asection *is ATTRIBUTE_UNUSED;
5786 bfd *obfd ATTRIBUTE_UNUSED;
5787 char **errmsg ATTRIBUTE_UNUSED;
5789 return bfd_reloc_ok;
5792 /* Elf core file support. Much of this only works on native
5793 toolchains, since we rely on knowing the
5794 machine-dependent procfs structure in order to pick
5795 out details about the corefile. */
5797 #ifdef HAVE_SYS_PROCFS_H
5798 # include <sys/procfs.h>
5799 #endif
5801 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5803 static int
5804 elfcore_make_pid (abfd)
5805 bfd *abfd;
5807 return ((elf_tdata (abfd)->core_lwpid << 16)
5808 + (elf_tdata (abfd)->core_pid));
5811 /* If there isn't a section called NAME, make one, using
5812 data from SECT. Note, this function will generate a
5813 reference to NAME, so you shouldn't deallocate or
5814 overwrite it. */
5816 static boolean
5817 elfcore_maybe_make_sect (abfd, name, sect)
5818 bfd *abfd;
5819 char *name;
5820 asection *sect;
5822 asection *sect2;
5824 if (bfd_get_section_by_name (abfd, name) != NULL)
5825 return true;
5827 sect2 = bfd_make_section (abfd, name);
5828 if (sect2 == NULL)
5829 return false;
5831 sect2->_raw_size = sect->_raw_size;
5832 sect2->filepos = sect->filepos;
5833 sect2->flags = sect->flags;
5834 sect2->alignment_power = sect->alignment_power;
5835 return true;
5838 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5839 actually creates up to two pseudosections:
5840 - For the single-threaded case, a section named NAME, unless
5841 such a section already exists.
5842 - For the multi-threaded case, a section named "NAME/PID", where
5843 PID is elfcore_make_pid (abfd).
5844 Both pseudosections have identical contents. */
5845 boolean
5846 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5847 bfd *abfd;
5848 char *name;
5849 size_t size;
5850 ufile_ptr filepos;
5852 char buf[100];
5853 char *threaded_name;
5854 asection *sect;
5856 /* Build the section name. */
5858 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5859 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
5860 if (threaded_name == NULL)
5861 return false;
5862 strcpy (threaded_name, buf);
5864 sect = bfd_make_section (abfd, threaded_name);
5865 if (sect == NULL)
5866 return false;
5867 sect->_raw_size = size;
5868 sect->filepos = filepos;
5869 sect->flags = SEC_HAS_CONTENTS;
5870 sect->alignment_power = 2;
5872 return elfcore_maybe_make_sect (abfd, name, sect);
5875 /* prstatus_t exists on:
5876 solaris 2.5+
5877 linux 2.[01] + glibc
5878 unixware 4.2
5881 #if defined (HAVE_PRSTATUS_T)
5882 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5884 static boolean
5885 elfcore_grok_prstatus (abfd, note)
5886 bfd *abfd;
5887 Elf_Internal_Note *note;
5889 size_t raw_size;
5890 int offset;
5892 if (note->descsz == sizeof (prstatus_t))
5894 prstatus_t prstat;
5896 raw_size = sizeof (prstat.pr_reg);
5897 offset = offsetof (prstatus_t, pr_reg);
5898 memcpy (&prstat, note->descdata, sizeof (prstat));
5900 /* Do not overwrite the core signal if it
5901 has already been set by another thread. */
5902 if (elf_tdata (abfd)->core_signal == 0)
5903 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5904 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5906 /* pr_who exists on:
5907 solaris 2.5+
5908 unixware 4.2
5909 pr_who doesn't exist on:
5910 linux 2.[01]
5912 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5913 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5914 #endif
5916 #if defined (HAVE_PRSTATUS32_T)
5917 else if (note->descsz == sizeof (prstatus32_t))
5919 /* 64-bit host, 32-bit corefile */
5920 prstatus32_t prstat;
5922 raw_size = sizeof (prstat.pr_reg);
5923 offset = offsetof (prstatus32_t, pr_reg);
5924 memcpy (&prstat, note->descdata, sizeof (prstat));
5926 /* Do not overwrite the core signal if it
5927 has already been set by another thread. */
5928 if (elf_tdata (abfd)->core_signal == 0)
5929 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5930 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5932 /* pr_who exists on:
5933 solaris 2.5+
5934 unixware 4.2
5935 pr_who doesn't exist on:
5936 linux 2.[01]
5938 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5939 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5940 #endif
5942 #endif /* HAVE_PRSTATUS32_T */
5943 else
5945 /* Fail - we don't know how to handle any other
5946 note size (ie. data object type). */
5947 return true;
5950 /* Make a ".reg/999" section and a ".reg" section. */
5951 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5952 raw_size, note->descpos + offset);
5954 #endif /* defined (HAVE_PRSTATUS_T) */
5956 /* Create a pseudosection containing the exact contents of NOTE. */
5957 static boolean
5958 elfcore_make_note_pseudosection (abfd, name, note)
5959 bfd *abfd;
5960 char *name;
5961 Elf_Internal_Note *note;
5963 return _bfd_elfcore_make_pseudosection (abfd, name,
5964 note->descsz, note->descpos);
5967 /* There isn't a consistent prfpregset_t across platforms,
5968 but it doesn't matter, because we don't have to pick this
5969 data structure apart. */
5971 static boolean
5972 elfcore_grok_prfpreg (abfd, note)
5973 bfd *abfd;
5974 Elf_Internal_Note *note;
5976 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5979 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5980 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5981 literally. */
5983 static boolean
5984 elfcore_grok_prxfpreg (abfd, note)
5985 bfd *abfd;
5986 Elf_Internal_Note *note;
5988 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5991 #if defined (HAVE_PRPSINFO_T)
5992 typedef prpsinfo_t elfcore_psinfo_t;
5993 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5994 typedef prpsinfo32_t elfcore_psinfo32_t;
5995 #endif
5996 #endif
5998 #if defined (HAVE_PSINFO_T)
5999 typedef psinfo_t elfcore_psinfo_t;
6000 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6001 typedef psinfo32_t elfcore_psinfo32_t;
6002 #endif
6003 #endif
6005 /* return a malloc'ed copy of a string at START which is at
6006 most MAX bytes long, possibly without a terminating '\0'.
6007 the copy will always have a terminating '\0'. */
6009 char *
6010 _bfd_elfcore_strndup (abfd, start, max)
6011 bfd *abfd;
6012 char *start;
6013 size_t max;
6015 char *dups;
6016 char *end = memchr (start, '\0', max);
6017 size_t len;
6019 if (end == NULL)
6020 len = max;
6021 else
6022 len = end - start;
6024 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6025 if (dups == NULL)
6026 return NULL;
6028 memcpy (dups, start, len);
6029 dups[len] = '\0';
6031 return dups;
6034 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6035 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
6037 static boolean
6038 elfcore_grok_psinfo (abfd, note)
6039 bfd *abfd;
6040 Elf_Internal_Note *note;
6042 if (note->descsz == sizeof (elfcore_psinfo_t))
6044 elfcore_psinfo_t psinfo;
6046 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6048 elf_tdata (abfd)->core_program
6049 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6050 sizeof (psinfo.pr_fname));
6052 elf_tdata (abfd)->core_command
6053 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6054 sizeof (psinfo.pr_psargs));
6056 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6057 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6059 /* 64-bit host, 32-bit corefile */
6060 elfcore_psinfo32_t psinfo;
6062 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6064 elf_tdata (abfd)->core_program
6065 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6066 sizeof (psinfo.pr_fname));
6068 elf_tdata (abfd)->core_command
6069 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6070 sizeof (psinfo.pr_psargs));
6072 #endif
6074 else
6076 /* Fail - we don't know how to handle any other
6077 note size (ie. data object type). */
6078 return true;
6081 /* Note that for some reason, a spurious space is tacked
6082 onto the end of the args in some (at least one anyway)
6083 implementations, so strip it off if it exists. */
6086 char *command = elf_tdata (abfd)->core_command;
6087 int n = strlen (command);
6089 if (0 < n && command[n - 1] == ' ')
6090 command[n - 1] = '\0';
6093 return true;
6095 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6097 #if defined (HAVE_PSTATUS_T)
6098 static boolean
6099 elfcore_grok_pstatus (abfd, note)
6100 bfd *abfd;
6101 Elf_Internal_Note *note;
6103 if (note->descsz == sizeof (pstatus_t)
6104 #if defined (HAVE_PXSTATUS_T)
6105 || note->descsz == sizeof (pxstatus_t)
6106 #endif
6109 pstatus_t pstat;
6111 memcpy (&pstat, note->descdata, sizeof (pstat));
6113 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6115 #if defined (HAVE_PSTATUS32_T)
6116 else if (note->descsz == sizeof (pstatus32_t))
6118 /* 64-bit host, 32-bit corefile */
6119 pstatus32_t pstat;
6121 memcpy (&pstat, note->descdata, sizeof (pstat));
6123 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6125 #endif
6126 /* Could grab some more details from the "representative"
6127 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6128 NT_LWPSTATUS note, presumably. */
6130 return true;
6132 #endif /* defined (HAVE_PSTATUS_T) */
6134 #if defined (HAVE_LWPSTATUS_T)
6135 static boolean
6136 elfcore_grok_lwpstatus (abfd, note)
6137 bfd *abfd;
6138 Elf_Internal_Note *note;
6140 lwpstatus_t lwpstat;
6141 char buf[100];
6142 char *name;
6143 asection *sect;
6145 if (note->descsz != sizeof (lwpstat)
6146 #if defined (HAVE_LWPXSTATUS_T)
6147 && note->descsz != sizeof (lwpxstatus_t)
6148 #endif
6150 return true;
6152 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6154 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6155 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6157 /* Make a ".reg/999" section. */
6159 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6160 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6161 if (name == NULL)
6162 return false;
6163 strcpy (name, buf);
6165 sect = bfd_make_section (abfd, name);
6166 if (sect == NULL)
6167 return false;
6169 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6170 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6171 sect->filepos = note->descpos
6172 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6173 #endif
6175 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6176 sect->_raw_size = sizeof (lwpstat.pr_reg);
6177 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6178 #endif
6180 sect->flags = SEC_HAS_CONTENTS;
6181 sect->alignment_power = 2;
6183 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6184 return false;
6186 /* Make a ".reg2/999" section */
6188 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6189 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6190 if (name == NULL)
6191 return false;
6192 strcpy (name, buf);
6194 sect = bfd_make_section (abfd, name);
6195 if (sect == NULL)
6196 return false;
6198 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6199 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6200 sect->filepos = note->descpos
6201 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6202 #endif
6204 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6205 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6206 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6207 #endif
6209 sect->flags = SEC_HAS_CONTENTS;
6210 sect->alignment_power = 2;
6212 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6214 #endif /* defined (HAVE_LWPSTATUS_T) */
6216 #if defined (HAVE_WIN32_PSTATUS_T)
6217 static boolean
6218 elfcore_grok_win32pstatus (abfd, note)
6219 bfd *abfd;
6220 Elf_Internal_Note *note;
6222 char buf[30];
6223 char *name;
6224 asection *sect;
6225 win32_pstatus_t pstatus;
6227 if (note->descsz < sizeof (pstatus))
6228 return true;
6230 memcpy (&pstatus, note->descdata, note->descsz);
6232 switch (pstatus.data_type)
6234 case NOTE_INFO_PROCESS:
6235 /* FIXME: need to add ->core_command. */
6236 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6237 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6238 break;
6240 case NOTE_INFO_THREAD:
6241 /* Make a ".reg/999" section. */
6242 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6244 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6245 if (name == NULL)
6246 return false;
6248 strcpy (name, buf);
6250 sect = bfd_make_section (abfd, name);
6251 if (sect == NULL)
6252 return false;
6254 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6255 sect->filepos = (note->descpos
6256 + offsetof (struct win32_pstatus,
6257 data.thread_info.thread_context));
6258 sect->flags = SEC_HAS_CONTENTS;
6259 sect->alignment_power = 2;
6261 if (pstatus.data.thread_info.is_active_thread)
6262 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6263 return false;
6264 break;
6266 case NOTE_INFO_MODULE:
6267 /* Make a ".module/xxxxxxxx" section. */
6268 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6270 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6271 if (name == NULL)
6272 return false;
6274 strcpy (name, buf);
6276 sect = bfd_make_section (abfd, name);
6278 if (sect == NULL)
6279 return false;
6281 sect->_raw_size = note->descsz;
6282 sect->filepos = note->descpos;
6283 sect->flags = SEC_HAS_CONTENTS;
6284 sect->alignment_power = 2;
6285 break;
6287 default:
6288 return true;
6291 return true;
6293 #endif /* HAVE_WIN32_PSTATUS_T */
6295 static boolean
6296 elfcore_grok_note (abfd, note)
6297 bfd *abfd;
6298 Elf_Internal_Note *note;
6300 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6302 switch (note->type)
6304 default:
6305 return true;
6307 case NT_PRSTATUS:
6308 if (bed->elf_backend_grok_prstatus)
6309 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6310 return true;
6311 #if defined (HAVE_PRSTATUS_T)
6312 return elfcore_grok_prstatus (abfd, note);
6313 #else
6314 return true;
6315 #endif
6317 #if defined (HAVE_PSTATUS_T)
6318 case NT_PSTATUS:
6319 return elfcore_grok_pstatus (abfd, note);
6320 #endif
6322 #if defined (HAVE_LWPSTATUS_T)
6323 case NT_LWPSTATUS:
6324 return elfcore_grok_lwpstatus (abfd, note);
6325 #endif
6327 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6328 return elfcore_grok_prfpreg (abfd, note);
6330 #if defined (HAVE_WIN32_PSTATUS_T)
6331 case NT_WIN32PSTATUS:
6332 return elfcore_grok_win32pstatus (abfd, note);
6333 #endif
6335 case NT_PRXFPREG: /* Linux SSE extension */
6336 if (note->namesz == 5
6337 && ! strcmp (note->namedata, "LINUX"))
6338 return elfcore_grok_prxfpreg (abfd, note);
6339 else
6340 return true;
6342 case NT_PRPSINFO:
6343 case NT_PSINFO:
6344 if (bed->elf_backend_grok_psinfo)
6345 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6346 return true;
6347 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6348 return elfcore_grok_psinfo (abfd, note);
6349 #else
6350 return true;
6351 #endif
6355 static boolean
6356 elfcore_read_notes (abfd, offset, size)
6357 bfd *abfd;
6358 file_ptr offset;
6359 bfd_size_type size;
6361 char *buf;
6362 char *p;
6364 if (size <= 0)
6365 return true;
6367 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
6368 return false;
6370 buf = bfd_malloc (size);
6371 if (buf == NULL)
6372 return false;
6374 if (bfd_bread (buf, size, abfd) != size)
6376 error:
6377 free (buf);
6378 return false;
6381 p = buf;
6382 while (p < buf + size)
6384 /* FIXME: bad alignment assumption. */
6385 Elf_External_Note *xnp = (Elf_External_Note *) p;
6386 Elf_Internal_Note in;
6388 in.type = H_GET_32 (abfd, xnp->type);
6390 in.namesz = H_GET_32 (abfd, xnp->namesz);
6391 in.namedata = xnp->name;
6393 in.descsz = H_GET_32 (abfd, xnp->descsz);
6394 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
6395 in.descpos = offset + (in.descdata - buf);
6397 if (! elfcore_grok_note (abfd, &in))
6398 goto error;
6400 p = in.descdata + BFD_ALIGN (in.descsz, 4);
6403 free (buf);
6404 return true;
6407 /* Providing external access to the ELF program header table. */
6409 /* Return an upper bound on the number of bytes required to store a
6410 copy of ABFD's program header table entries. Return -1 if an error
6411 occurs; bfd_get_error will return an appropriate code. */
6413 long
6414 bfd_get_elf_phdr_upper_bound (abfd)
6415 bfd *abfd;
6417 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6419 bfd_set_error (bfd_error_wrong_format);
6420 return -1;
6423 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
6426 /* Copy ABFD's program header table entries to *PHDRS. The entries
6427 will be stored as an array of Elf_Internal_Phdr structures, as
6428 defined in include/elf/internal.h. To find out how large the
6429 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6431 Return the number of program header table entries read, or -1 if an
6432 error occurs; bfd_get_error will return an appropriate code. */
6435 bfd_get_elf_phdrs (abfd, phdrs)
6436 bfd *abfd;
6437 void *phdrs;
6439 int num_phdrs;
6441 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6443 bfd_set_error (bfd_error_wrong_format);
6444 return -1;
6447 num_phdrs = elf_elfheader (abfd)->e_phnum;
6448 memcpy (phdrs, elf_tdata (abfd)->phdr,
6449 num_phdrs * sizeof (Elf_Internal_Phdr));
6451 return num_phdrs;
6454 void
6455 _bfd_elf_sprintf_vma (abfd, buf, value)
6456 bfd *abfd ATTRIBUTE_UNUSED;
6457 char *buf;
6458 bfd_vma value;
6460 #ifdef BFD64
6461 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6463 i_ehdrp = elf_elfheader (abfd);
6464 if (i_ehdrp == NULL)
6465 sprintf_vma (buf, value);
6466 else
6468 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6470 #if BFD_HOST_64BIT_LONG
6471 sprintf (buf, "%016lx", value);
6472 #else
6473 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6474 _bfd_int64_low (value));
6475 #endif
6477 else
6478 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6480 #else
6481 sprintf_vma (buf, value);
6482 #endif
6485 void
6486 _bfd_elf_fprintf_vma (abfd, stream, value)
6487 bfd *abfd ATTRIBUTE_UNUSED;
6488 PTR stream;
6489 bfd_vma value;
6491 #ifdef BFD64
6492 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6494 i_ehdrp = elf_elfheader (abfd);
6495 if (i_ehdrp == NULL)
6496 fprintf_vma ((FILE *) stream, value);
6497 else
6499 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6501 #if BFD_HOST_64BIT_LONG
6502 fprintf ((FILE *) stream, "%016lx", value);
6503 #else
6504 fprintf ((FILE *) stream, "%08lx%08lx",
6505 _bfd_int64_high (value), _bfd_int64_low (value));
6506 #endif
6508 else
6509 fprintf ((FILE *) stream, "%08lx",
6510 (unsigned long) (value & 0xffffffff));
6512 #else
6513 fprintf_vma ((FILE *) stream, value);
6514 #endif
6517 enum elf_reloc_type_class
6518 _bfd_elf_reloc_type_class (rela)
6519 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
6521 return reloc_class_normal;
6524 /* For RELA architectures, return what the relocation value for
6525 relocation against a local symbol. */
6527 bfd_vma
6528 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
6529 bfd *abfd;
6530 Elf_Internal_Sym *sym;
6531 asection *sec;
6532 Elf_Internal_Rela *rel;
6534 bfd_vma relocation;
6536 relocation = (sec->output_section->vma
6537 + sec->output_offset
6538 + sym->st_value);
6539 if ((sec->flags & SEC_MERGE)
6540 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
6541 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6543 asection *msec;
6545 msec = sec;
6546 rel->r_addend =
6547 _bfd_merged_section_offset (abfd, &msec,
6548 elf_section_data (sec)->sec_info,
6549 sym->st_value + rel->r_addend,
6550 (bfd_vma) 0)
6551 - relocation;
6552 rel->r_addend += msec->output_section->vma + msec->output_offset;
6554 return relocation;
6557 bfd_vma
6558 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
6559 bfd *abfd;
6560 Elf_Internal_Sym *sym;
6561 asection **psec;
6562 bfd_vma addend;
6564 asection *sec = *psec;
6566 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
6567 return sym->st_value + addend;
6569 return _bfd_merged_section_offset (abfd, psec,
6570 elf_section_data (sec)->sec_info,
6571 sym->st_value + addend, (bfd_vma) 0);
6574 bfd_vma
6575 _bfd_elf_section_offset (abfd, info, sec, offset)
6576 bfd *abfd;
6577 struct bfd_link_info *info;
6578 asection *sec;
6579 bfd_vma offset;
6581 struct bfd_elf_section_data *sec_data;
6583 sec_data = elf_section_data (sec);
6584 switch (sec_data->sec_info_type)
6586 case ELF_INFO_TYPE_STABS:
6587 return _bfd_stab_section_offset
6588 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
6589 offset);
6590 case ELF_INFO_TYPE_EH_FRAME:
6591 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
6592 default:
6593 return offset;