* tc-arm.c (do_xsc_mia, do_xsc_mar, do_xsc_mra): Renamed from
[binutils.git] / bfd / elf.c
blob2c14de8a60fdbcda5e579804aa2475972e70031a
1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 SECTION
24 ELF backends
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet.
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "bfd.h"
38 #include "sysdep.h"
39 #include "bfdlink.h"
40 #include "libbfd.h"
41 #define ARCH_SIZE 0
42 #include "elf-bfd.h"
43 #include "libiberty.h"
45 static INLINE struct elf_segment_map *make_mapping
46 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
47 static boolean map_sections_to_segments PARAMS ((bfd *));
48 static int elf_sort_sections PARAMS ((const PTR, const PTR));
49 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
50 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
51 static boolean prep_headers PARAMS ((bfd *));
52 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
53 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
54 static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
55 static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
56 static void merge_sections_remove_hook PARAMS ((bfd *, asection *));
57 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
58 static void set_group_contents PARAMS ((bfd *, asection *, PTR));
59 static boolean assign_section_numbers PARAMS ((bfd *));
60 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
61 static boolean elf_map_symbols PARAMS ((bfd *));
62 static bfd_size_type get_program_header_size PARAMS ((bfd *));
63 static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
64 static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
65 bfd_vma, const char **,
66 const char **));
67 static int elfcore_make_pid PARAMS ((bfd *));
68 static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
69 static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
70 Elf_Internal_Note *));
71 static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
72 static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
73 static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
75 static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *));
76 static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *,
77 Elf_Internal_Note *));
78 static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *));
80 /* Swap version information in and out. The version information is
81 currently size independent. If that ever changes, this code will
82 need to move into elfcode.h. */
84 /* Swap in a Verdef structure. */
86 void
87 _bfd_elf_swap_verdef_in (abfd, src, dst)
88 bfd *abfd;
89 const Elf_External_Verdef *src;
90 Elf_Internal_Verdef *dst;
92 dst->vd_version = H_GET_16 (abfd, src->vd_version);
93 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
94 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
95 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
96 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
97 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
98 dst->vd_next = H_GET_32 (abfd, src->vd_next);
101 /* Swap out a Verdef structure. */
103 void
104 _bfd_elf_swap_verdef_out (abfd, src, dst)
105 bfd *abfd;
106 const Elf_Internal_Verdef *src;
107 Elf_External_Verdef *dst;
109 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
110 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
111 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
112 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
113 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
114 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
115 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
118 /* Swap in a Verdaux structure. */
120 void
121 _bfd_elf_swap_verdaux_in (abfd, src, dst)
122 bfd *abfd;
123 const Elf_External_Verdaux *src;
124 Elf_Internal_Verdaux *dst;
126 dst->vda_name = H_GET_32 (abfd, src->vda_name);
127 dst->vda_next = H_GET_32 (abfd, src->vda_next);
130 /* Swap out a Verdaux structure. */
132 void
133 _bfd_elf_swap_verdaux_out (abfd, src, dst)
134 bfd *abfd;
135 const Elf_Internal_Verdaux *src;
136 Elf_External_Verdaux *dst;
138 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
139 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
142 /* Swap in a Verneed structure. */
144 void
145 _bfd_elf_swap_verneed_in (abfd, src, dst)
146 bfd *abfd;
147 const Elf_External_Verneed *src;
148 Elf_Internal_Verneed *dst;
150 dst->vn_version = H_GET_16 (abfd, src->vn_version);
151 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
152 dst->vn_file = H_GET_32 (abfd, src->vn_file);
153 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
154 dst->vn_next = H_GET_32 (abfd, src->vn_next);
157 /* Swap out a Verneed structure. */
159 void
160 _bfd_elf_swap_verneed_out (abfd, src, dst)
161 bfd *abfd;
162 const Elf_Internal_Verneed *src;
163 Elf_External_Verneed *dst;
165 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
166 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
167 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
168 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
169 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
172 /* Swap in a Vernaux structure. */
174 void
175 _bfd_elf_swap_vernaux_in (abfd, src, dst)
176 bfd *abfd;
177 const Elf_External_Vernaux *src;
178 Elf_Internal_Vernaux *dst;
180 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
181 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
182 dst->vna_other = H_GET_16 (abfd, src->vna_other);
183 dst->vna_name = H_GET_32 (abfd, src->vna_name);
184 dst->vna_next = H_GET_32 (abfd, src->vna_next);
187 /* Swap out a Vernaux structure. */
189 void
190 _bfd_elf_swap_vernaux_out (abfd, src, dst)
191 bfd *abfd;
192 const Elf_Internal_Vernaux *src;
193 Elf_External_Vernaux *dst;
195 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
196 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
197 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
198 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
199 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
202 /* Swap in a Versym structure. */
204 void
205 _bfd_elf_swap_versym_in (abfd, src, dst)
206 bfd *abfd;
207 const Elf_External_Versym *src;
208 Elf_Internal_Versym *dst;
210 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
213 /* Swap out a Versym structure. */
215 void
216 _bfd_elf_swap_versym_out (abfd, src, dst)
217 bfd *abfd;
218 const Elf_Internal_Versym *src;
219 Elf_External_Versym *dst;
221 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
224 /* Standard ELF hash function. Do not change this function; you will
225 cause invalid hash tables to be generated. */
227 unsigned long
228 bfd_elf_hash (namearg)
229 const char *namearg;
231 const unsigned char *name = (const unsigned char *) namearg;
232 unsigned long h = 0;
233 unsigned long g;
234 int ch;
236 while ((ch = *name++) != '\0')
238 h = (h << 4) + ch;
239 if ((g = (h & 0xf0000000)) != 0)
241 h ^= g >> 24;
242 /* The ELF ABI says `h &= ~g', but this is equivalent in
243 this case and on some machines one insn instead of two. */
244 h ^= g;
247 return h;
250 /* Read a specified number of bytes at a specified offset in an ELF
251 file, into a newly allocated buffer, and return a pointer to the
252 buffer. */
254 static char *
255 elf_read (abfd, offset, size)
256 bfd *abfd;
257 file_ptr offset;
258 bfd_size_type size;
260 char *buf;
262 if ((buf = bfd_alloc (abfd, size)) == NULL)
263 return NULL;
264 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
265 return NULL;
266 if (bfd_bread ((PTR) buf, size, abfd) != size)
268 if (bfd_get_error () != bfd_error_system_call)
269 bfd_set_error (bfd_error_file_truncated);
270 return NULL;
272 return buf;
275 boolean
276 bfd_elf_mkobject (abfd)
277 bfd *abfd;
279 /* This just does initialization. */
280 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
281 bfd_size_type amt = sizeof (struct elf_obj_tdata);
282 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
283 if (elf_tdata (abfd) == 0)
284 return false;
285 /* Since everything is done at close time, do we need any
286 initialization? */
288 return true;
291 boolean
292 bfd_elf_mkcorefile (abfd)
293 bfd *abfd;
295 /* I think this can be done just like an object file. */
296 return bfd_elf_mkobject (abfd);
299 char *
300 bfd_elf_get_str_section (abfd, shindex)
301 bfd *abfd;
302 unsigned int shindex;
304 Elf_Internal_Shdr **i_shdrp;
305 char *shstrtab = NULL;
306 file_ptr offset;
307 bfd_size_type shstrtabsize;
309 i_shdrp = elf_elfsections (abfd);
310 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
311 return 0;
313 shstrtab = (char *) i_shdrp[shindex]->contents;
314 if (shstrtab == NULL)
316 /* No cached one, attempt to read, and cache what we read. */
317 offset = i_shdrp[shindex]->sh_offset;
318 shstrtabsize = i_shdrp[shindex]->sh_size;
319 shstrtab = elf_read (abfd, offset, shstrtabsize);
320 i_shdrp[shindex]->contents = (PTR) shstrtab;
322 return shstrtab;
325 char *
326 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
327 bfd *abfd;
328 unsigned int shindex;
329 unsigned int strindex;
331 Elf_Internal_Shdr *hdr;
333 if (strindex == 0)
334 return "";
336 hdr = elf_elfsections (abfd)[shindex];
338 if (hdr->contents == NULL
339 && bfd_elf_get_str_section (abfd, shindex) == NULL)
340 return NULL;
342 if (strindex >= hdr->sh_size)
344 (*_bfd_error_handler)
345 (_("%s: invalid string offset %u >= %lu for section `%s'"),
346 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
347 ((shindex == elf_elfheader(abfd)->e_shstrndx
348 && strindex == hdr->sh_name)
349 ? ".shstrtab"
350 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
351 return "";
354 return ((char *) hdr->contents) + strindex;
357 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
358 sections. The first element is the flags, the rest are section
359 pointers. */
361 typedef union elf_internal_group {
362 Elf_Internal_Shdr *shdr;
363 unsigned int flags;
364 } Elf_Internal_Group;
366 /* Set next_in_group list pointer, and group name for NEWSECT. */
368 static boolean
369 setup_group (abfd, hdr, newsect)
370 bfd *abfd;
371 Elf_Internal_Shdr *hdr;
372 asection *newsect;
374 unsigned int num_group = elf_tdata (abfd)->num_group;
376 /* If num_group is zero, read in all SHT_GROUP sections. The count
377 is set to -1 if there are no SHT_GROUP sections. */
378 if (num_group == 0)
380 unsigned int i, shnum;
382 /* First count the number of groups. If we have a SHT_GROUP
383 section with just a flag word (ie. sh_size is 4), ignore it. */
384 shnum = elf_numsections (abfd);
385 num_group = 0;
386 for (i = 0; i < shnum; i++)
388 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
389 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
390 num_group += 1;
393 if (num_group == 0)
394 num_group = (unsigned) -1;
395 elf_tdata (abfd)->num_group = num_group;
397 if (num_group > 0)
399 /* We keep a list of elf section headers for group sections,
400 so we can find them quickly. */
401 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
402 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
403 if (elf_tdata (abfd)->group_sect_ptr == NULL)
404 return false;
406 num_group = 0;
407 for (i = 0; i < shnum; i++)
409 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
410 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
412 unsigned char *src;
413 Elf_Internal_Group *dest;
415 /* Add to list of sections. */
416 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
417 num_group += 1;
419 /* Read the raw contents. */
420 BFD_ASSERT (sizeof (*dest) >= 4);
421 amt = shdr->sh_size * sizeof (*dest) / 4;
422 shdr->contents = bfd_alloc (abfd, amt);
423 if (shdr->contents == NULL
424 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
425 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
426 != shdr->sh_size))
427 return false;
429 /* Translate raw contents, a flag word followed by an
430 array of elf section indices all in target byte order,
431 to the flag word followed by an array of elf section
432 pointers. */
433 src = shdr->contents + shdr->sh_size;
434 dest = (Elf_Internal_Group *) (shdr->contents + amt);
435 while (1)
437 unsigned int idx;
439 src -= 4;
440 --dest;
441 idx = H_GET_32 (abfd, src);
442 if (src == shdr->contents)
444 dest->flags = idx;
445 break;
447 if (idx >= shnum)
449 ((*_bfd_error_handler)
450 (_("%s: invalid SHT_GROUP entry"),
451 bfd_archive_filename (abfd)));
452 idx = 0;
454 dest->shdr = elf_elfsections (abfd)[idx];
461 if (num_group != (unsigned) -1)
463 unsigned int i;
465 for (i = 0; i < num_group; i++)
467 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
468 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
469 unsigned int n_elt = shdr->sh_size / 4;
471 /* Look through this group's sections to see if current
472 section is a member. */
473 while (--n_elt != 0)
474 if ((++idx)->shdr == hdr)
476 asection *s = NULL;
478 /* We are a member of this group. Go looking through
479 other members to see if any others are linked via
480 next_in_group. */
481 idx = (Elf_Internal_Group *) shdr->contents;
482 n_elt = shdr->sh_size / 4;
483 while (--n_elt != 0)
484 if ((s = (++idx)->shdr->bfd_section) != NULL
485 && elf_next_in_group (s) != NULL)
486 break;
487 if (n_elt != 0)
489 /* Snarf the group name from other member, and
490 insert current section in circular list. */
491 elf_group_name (newsect) = elf_group_name (s);
492 elf_next_in_group (newsect) = elf_next_in_group (s);
493 elf_next_in_group (s) = newsect;
495 else
497 struct elf_backend_data *bed;
498 file_ptr pos;
499 unsigned char ename[4];
500 unsigned long iname;
501 const char *gname;
503 /* Humbug. Get the name from the group signature
504 symbol. Why isn't the signature just a string?
505 Fortunately, the name index is at the same
506 place in the external symbol for both 32 and 64
507 bit ELF. */
508 bed = get_elf_backend_data (abfd);
509 pos = elf_tdata (abfd)->symtab_hdr.sh_offset;
510 pos += shdr->sh_info * bed->s->sizeof_sym;
511 if (bfd_seek (abfd, pos, SEEK_SET) != 0
512 || bfd_bread (ename, (bfd_size_type) 4, abfd) != 4)
513 return false;
514 iname = H_GET_32 (abfd, ename);
515 gname = elf_string_from_elf_strtab (abfd, iname);
516 elf_group_name (newsect) = gname;
518 /* Start a circular list with one element. */
519 elf_next_in_group (newsect) = newsect;
521 if (shdr->bfd_section != NULL)
522 elf_next_in_group (shdr->bfd_section) = newsect;
523 i = num_group - 1;
524 break;
529 if (elf_group_name (newsect) == NULL)
531 (*_bfd_error_handler) (_("%s: no group info for section %s"),
532 bfd_archive_filename (abfd), newsect->name);
534 return true;
537 /* Make a BFD section from an ELF section. We store a pointer to the
538 BFD section in the bfd_section field of the header. */
540 boolean
541 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
542 bfd *abfd;
543 Elf_Internal_Shdr *hdr;
544 const char *name;
546 asection *newsect;
547 flagword flags;
548 struct elf_backend_data *bed;
550 if (hdr->bfd_section != NULL)
552 BFD_ASSERT (strcmp (name,
553 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
554 return true;
557 newsect = bfd_make_section_anyway (abfd, name);
558 if (newsect == NULL)
559 return false;
561 newsect->filepos = hdr->sh_offset;
563 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
564 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
565 || ! bfd_set_section_alignment (abfd, newsect,
566 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
567 return false;
569 flags = SEC_NO_FLAGS;
570 if (hdr->sh_type != SHT_NOBITS)
571 flags |= SEC_HAS_CONTENTS;
572 if (hdr->sh_type == SHT_GROUP)
573 flags |= SEC_GROUP | SEC_EXCLUDE;
574 if ((hdr->sh_flags & SHF_ALLOC) != 0)
576 flags |= SEC_ALLOC;
577 if (hdr->sh_type != SHT_NOBITS)
578 flags |= SEC_LOAD;
580 if ((hdr->sh_flags & SHF_WRITE) == 0)
581 flags |= SEC_READONLY;
582 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
583 flags |= SEC_CODE;
584 else if ((flags & SEC_LOAD) != 0)
585 flags |= SEC_DATA;
586 if ((hdr->sh_flags & SHF_MERGE) != 0)
588 flags |= SEC_MERGE;
589 newsect->entsize = hdr->sh_entsize;
590 if ((hdr->sh_flags & SHF_STRINGS) != 0)
591 flags |= SEC_STRINGS;
593 if (hdr->sh_flags & SHF_GROUP)
594 if (!setup_group (abfd, hdr, newsect))
595 return false;
597 /* The debugging sections appear to be recognized only by name, not
598 any sort of flag. */
600 static const char *debug_sec_names [] =
602 ".debug",
603 ".gnu.linkonce.wi.",
604 ".line",
605 ".stab"
607 int i;
609 for (i = ARRAY_SIZE (debug_sec_names); i--;)
610 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
611 break;
613 if (i >= 0)
614 flags |= SEC_DEBUGGING;
617 /* As a GNU extension, if the name begins with .gnu.linkonce, we
618 only link a single copy of the section. This is used to support
619 g++. g++ will emit each template expansion in its own section.
620 The symbols will be defined as weak, so that multiple definitions
621 are permitted. The GNU linker extension is to actually discard
622 all but one of the sections. */
623 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
624 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
626 bed = get_elf_backend_data (abfd);
627 if (bed->elf_backend_section_flags)
628 if (! bed->elf_backend_section_flags (&flags, hdr))
629 return false;
631 if (! bfd_set_section_flags (abfd, newsect, flags))
632 return false;
634 if ((flags & SEC_ALLOC) != 0)
636 Elf_Internal_Phdr *phdr;
637 unsigned int i;
639 /* Look through the phdrs to see if we need to adjust the lma.
640 If all the p_paddr fields are zero, we ignore them, since
641 some ELF linkers produce such output. */
642 phdr = elf_tdata (abfd)->phdr;
643 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
645 if (phdr->p_paddr != 0)
646 break;
648 if (i < elf_elfheader (abfd)->e_phnum)
650 phdr = elf_tdata (abfd)->phdr;
651 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
653 /* This section is part of this segment if its file
654 offset plus size lies within the segment's memory
655 span and, if the section is loaded, the extent of the
656 loaded data lies within the extent of the segment.
657 If the p_paddr field is not set, we don't alter the
658 LMA. */
659 if (phdr->p_type == PT_LOAD
660 && phdr->p_paddr
661 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
662 && (hdr->sh_offset + hdr->sh_size
663 <= phdr->p_offset + phdr->p_memsz)
664 && ((flags & SEC_LOAD) == 0
665 || (phdr->p_offset + phdr->p_filesz
666 >= hdr->sh_offset + hdr->sh_size)))
668 /* We used to do a relative adjustment here, but
669 that doesn't work if the segment is packed with
670 code from multiple VMAs. Instead we calculate
671 the LMA absoultely, based on the LMA of the
672 segment (it is assumed that the segment will
673 contain sections with contiguous LMAs, even if
674 the VMAs are not). */
675 newsect->lma = phdr->p_paddr
676 + hdr->sh_offset - phdr->p_offset;
677 break;
683 hdr->bfd_section = newsect;
684 elf_section_data (newsect)->this_hdr = *hdr;
686 return true;
690 INTERNAL_FUNCTION
691 bfd_elf_find_section
693 SYNOPSIS
694 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
696 DESCRIPTION
697 Helper functions for GDB to locate the string tables.
698 Since BFD hides string tables from callers, GDB needs to use an
699 internal hook to find them. Sun's .stabstr, in particular,
700 isn't even pointed to by the .stab section, so ordinary
701 mechanisms wouldn't work to find it, even if we had some.
704 struct elf_internal_shdr *
705 bfd_elf_find_section (abfd, name)
706 bfd *abfd;
707 char *name;
709 Elf_Internal_Shdr **i_shdrp;
710 char *shstrtab;
711 unsigned int max;
712 unsigned int i;
714 i_shdrp = elf_elfsections (abfd);
715 if (i_shdrp != NULL)
717 shstrtab = bfd_elf_get_str_section (abfd,
718 elf_elfheader (abfd)->e_shstrndx);
719 if (shstrtab != NULL)
721 max = elf_numsections (abfd);
722 for (i = 1; i < max; i++)
723 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
724 return i_shdrp[i];
727 return 0;
730 const char *const bfd_elf_section_type_names[] = {
731 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
732 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
733 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
736 /* ELF relocs are against symbols. If we are producing relocateable
737 output, and the reloc is against an external symbol, and nothing
738 has given us any additional addend, the resulting reloc will also
739 be against the same symbol. In such a case, we don't want to
740 change anything about the way the reloc is handled, since it will
741 all be done at final link time. Rather than put special case code
742 into bfd_perform_relocation, all the reloc types use this howto
743 function. It just short circuits the reloc if producing
744 relocateable output against an external symbol. */
746 bfd_reloc_status_type
747 bfd_elf_generic_reloc (abfd,
748 reloc_entry,
749 symbol,
750 data,
751 input_section,
752 output_bfd,
753 error_message)
754 bfd *abfd ATTRIBUTE_UNUSED;
755 arelent *reloc_entry;
756 asymbol *symbol;
757 PTR data ATTRIBUTE_UNUSED;
758 asection *input_section;
759 bfd *output_bfd;
760 char **error_message ATTRIBUTE_UNUSED;
762 if (output_bfd != (bfd *) NULL
763 && (symbol->flags & BSF_SECTION_SYM) == 0
764 && (! reloc_entry->howto->partial_inplace
765 || reloc_entry->addend == 0))
767 reloc_entry->address += input_section->output_offset;
768 return bfd_reloc_ok;
771 return bfd_reloc_continue;
774 /* Make sure sec_info_type is cleared if sec_info is cleared too. */
776 static void
777 merge_sections_remove_hook (abfd, sec)
778 bfd *abfd ATTRIBUTE_UNUSED;
779 asection *sec;
781 struct bfd_elf_section_data *sec_data;
783 sec_data = elf_section_data (sec);
784 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE);
785 sec_data->sec_info_type = ELF_INFO_TYPE_NONE;
788 /* Finish SHF_MERGE section merging. */
790 boolean
791 _bfd_elf_merge_sections (abfd, info)
792 bfd *abfd;
793 struct bfd_link_info *info;
795 if (!is_elf_hash_table (info))
796 return false;
797 if (elf_hash_table (info)->merge_info)
798 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info,
799 merge_sections_remove_hook);
800 return true;
803 /* Copy the program header and other data from one object module to
804 another. */
806 boolean
807 _bfd_elf_copy_private_bfd_data (ibfd, obfd)
808 bfd *ibfd;
809 bfd *obfd;
811 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
812 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
813 return true;
815 BFD_ASSERT (!elf_flags_init (obfd)
816 || (elf_elfheader (obfd)->e_flags
817 == elf_elfheader (ibfd)->e_flags));
819 elf_gp (obfd) = elf_gp (ibfd);
820 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
821 elf_flags_init (obfd) = true;
822 return true;
825 /* Print out the program headers. */
827 boolean
828 _bfd_elf_print_private_bfd_data (abfd, farg)
829 bfd *abfd;
830 PTR farg;
832 FILE *f = (FILE *) farg;
833 Elf_Internal_Phdr *p;
834 asection *s;
835 bfd_byte *dynbuf = NULL;
837 p = elf_tdata (abfd)->phdr;
838 if (p != NULL)
840 unsigned int i, c;
842 fprintf (f, _("\nProgram Header:\n"));
843 c = elf_elfheader (abfd)->e_phnum;
844 for (i = 0; i < c; i++, p++)
846 const char *pt;
847 char buf[20];
849 switch (p->p_type)
851 case PT_NULL: pt = "NULL"; break;
852 case PT_LOAD: pt = "LOAD"; break;
853 case PT_DYNAMIC: pt = "DYNAMIC"; break;
854 case PT_INTERP: pt = "INTERP"; break;
855 case PT_NOTE: pt = "NOTE"; break;
856 case PT_SHLIB: pt = "SHLIB"; break;
857 case PT_PHDR: pt = "PHDR"; break;
858 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break;
859 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
861 fprintf (f, "%8s off 0x", pt);
862 bfd_fprintf_vma (abfd, f, p->p_offset);
863 fprintf (f, " vaddr 0x");
864 bfd_fprintf_vma (abfd, f, p->p_vaddr);
865 fprintf (f, " paddr 0x");
866 bfd_fprintf_vma (abfd, f, p->p_paddr);
867 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
868 fprintf (f, " filesz 0x");
869 bfd_fprintf_vma (abfd, f, p->p_filesz);
870 fprintf (f, " memsz 0x");
871 bfd_fprintf_vma (abfd, f, p->p_memsz);
872 fprintf (f, " flags %c%c%c",
873 (p->p_flags & PF_R) != 0 ? 'r' : '-',
874 (p->p_flags & PF_W) != 0 ? 'w' : '-',
875 (p->p_flags & PF_X) != 0 ? 'x' : '-');
876 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
877 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
878 fprintf (f, "\n");
882 s = bfd_get_section_by_name (abfd, ".dynamic");
883 if (s != NULL)
885 int elfsec;
886 unsigned long shlink;
887 bfd_byte *extdyn, *extdynend;
888 size_t extdynsize;
889 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
891 fprintf (f, _("\nDynamic Section:\n"));
893 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
894 if (dynbuf == NULL)
895 goto error_return;
896 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
897 s->_raw_size))
898 goto error_return;
900 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
901 if (elfsec == -1)
902 goto error_return;
903 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
905 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
906 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
908 extdyn = dynbuf;
909 extdynend = extdyn + s->_raw_size;
910 for (; extdyn < extdynend; extdyn += extdynsize)
912 Elf_Internal_Dyn dyn;
913 const char *name;
914 char ab[20];
915 boolean stringp;
917 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
919 if (dyn.d_tag == DT_NULL)
920 break;
922 stringp = false;
923 switch (dyn.d_tag)
925 default:
926 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
927 name = ab;
928 break;
930 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
931 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
932 case DT_PLTGOT: name = "PLTGOT"; break;
933 case DT_HASH: name = "HASH"; break;
934 case DT_STRTAB: name = "STRTAB"; break;
935 case DT_SYMTAB: name = "SYMTAB"; break;
936 case DT_RELA: name = "RELA"; break;
937 case DT_RELASZ: name = "RELASZ"; break;
938 case DT_RELAENT: name = "RELAENT"; break;
939 case DT_STRSZ: name = "STRSZ"; break;
940 case DT_SYMENT: name = "SYMENT"; break;
941 case DT_INIT: name = "INIT"; break;
942 case DT_FINI: name = "FINI"; break;
943 case DT_SONAME: name = "SONAME"; stringp = true; break;
944 case DT_RPATH: name = "RPATH"; stringp = true; break;
945 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
946 case DT_REL: name = "REL"; break;
947 case DT_RELSZ: name = "RELSZ"; break;
948 case DT_RELENT: name = "RELENT"; break;
949 case DT_PLTREL: name = "PLTREL"; break;
950 case DT_DEBUG: name = "DEBUG"; break;
951 case DT_TEXTREL: name = "TEXTREL"; break;
952 case DT_JMPREL: name = "JMPREL"; break;
953 case DT_BIND_NOW: name = "BIND_NOW"; break;
954 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
955 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
956 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
957 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
958 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
959 case DT_FLAGS: name = "FLAGS"; break;
960 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
961 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
962 case DT_CHECKSUM: name = "CHECKSUM"; break;
963 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
964 case DT_MOVEENT: name = "MOVEENT"; break;
965 case DT_MOVESZ: name = "MOVESZ"; break;
966 case DT_FEATURE: name = "FEATURE"; break;
967 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
968 case DT_SYMINSZ: name = "SYMINSZ"; break;
969 case DT_SYMINENT: name = "SYMINENT"; break;
970 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
971 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
972 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
973 case DT_PLTPAD: name = "PLTPAD"; break;
974 case DT_MOVETAB: name = "MOVETAB"; break;
975 case DT_SYMINFO: name = "SYMINFO"; break;
976 case DT_RELACOUNT: name = "RELACOUNT"; break;
977 case DT_RELCOUNT: name = "RELCOUNT"; break;
978 case DT_FLAGS_1: name = "FLAGS_1"; break;
979 case DT_VERSYM: name = "VERSYM"; break;
980 case DT_VERDEF: name = "VERDEF"; break;
981 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
982 case DT_VERNEED: name = "VERNEED"; break;
983 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
984 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
985 case DT_USED: name = "USED"; break;
986 case DT_FILTER: name = "FILTER"; stringp = true; break;
989 fprintf (f, " %-11s ", name);
990 if (! stringp)
991 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
992 else
994 const char *string;
995 unsigned int tagv = dyn.d_un.d_val;
997 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
998 if (string == NULL)
999 goto error_return;
1000 fprintf (f, "%s", string);
1002 fprintf (f, "\n");
1005 free (dynbuf);
1006 dynbuf = NULL;
1009 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
1010 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
1012 if (! _bfd_elf_slurp_version_tables (abfd))
1013 return false;
1016 if (elf_dynverdef (abfd) != 0)
1018 Elf_Internal_Verdef *t;
1020 fprintf (f, _("\nVersion definitions:\n"));
1021 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
1023 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
1024 t->vd_flags, t->vd_hash, t->vd_nodename);
1025 if (t->vd_auxptr->vda_nextptr != NULL)
1027 Elf_Internal_Verdaux *a;
1029 fprintf (f, "\t");
1030 for (a = t->vd_auxptr->vda_nextptr;
1031 a != NULL;
1032 a = a->vda_nextptr)
1033 fprintf (f, "%s ", a->vda_nodename);
1034 fprintf (f, "\n");
1039 if (elf_dynverref (abfd) != 0)
1041 Elf_Internal_Verneed *t;
1043 fprintf (f, _("\nVersion References:\n"));
1044 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1046 Elf_Internal_Vernaux *a;
1048 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1049 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1050 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1051 a->vna_flags, a->vna_other, a->vna_nodename);
1055 return true;
1057 error_return:
1058 if (dynbuf != NULL)
1059 free (dynbuf);
1060 return false;
1063 /* Display ELF-specific fields of a symbol. */
1065 void
1066 bfd_elf_print_symbol (abfd, filep, symbol, how)
1067 bfd *abfd;
1068 PTR filep;
1069 asymbol *symbol;
1070 bfd_print_symbol_type how;
1072 FILE *file = (FILE *) filep;
1073 switch (how)
1075 case bfd_print_symbol_name:
1076 fprintf (file, "%s", symbol->name);
1077 break;
1078 case bfd_print_symbol_more:
1079 fprintf (file, "elf ");
1080 bfd_fprintf_vma (abfd, file, symbol->value);
1081 fprintf (file, " %lx", (long) symbol->flags);
1082 break;
1083 case bfd_print_symbol_all:
1085 const char *section_name;
1086 const char *name = NULL;
1087 struct elf_backend_data *bed;
1088 unsigned char st_other;
1089 bfd_vma val;
1091 section_name = symbol->section ? symbol->section->name : "(*none*)";
1093 bed = get_elf_backend_data (abfd);
1094 if (bed->elf_backend_print_symbol_all)
1095 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1097 if (name == NULL)
1099 name = symbol->name;
1100 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1103 fprintf (file, " %s\t", section_name);
1104 /* Print the "other" value for a symbol. For common symbols,
1105 we've already printed the size; now print the alignment.
1106 For other symbols, we have no specified alignment, and
1107 we've printed the address; now print the size. */
1108 if (bfd_is_com_section (symbol->section))
1109 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1110 else
1111 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1112 bfd_fprintf_vma (abfd, file, val);
1114 /* If we have version information, print it. */
1115 if (elf_tdata (abfd)->dynversym_section != 0
1116 && (elf_tdata (abfd)->dynverdef_section != 0
1117 || elf_tdata (abfd)->dynverref_section != 0))
1119 unsigned int vernum;
1120 const char *version_string;
1122 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1124 if (vernum == 0)
1125 version_string = "";
1126 else if (vernum == 1)
1127 version_string = "Base";
1128 else if (vernum <= elf_tdata (abfd)->cverdefs)
1129 version_string =
1130 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1131 else
1133 Elf_Internal_Verneed *t;
1135 version_string = "";
1136 for (t = elf_tdata (abfd)->verref;
1137 t != NULL;
1138 t = t->vn_nextref)
1140 Elf_Internal_Vernaux *a;
1142 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1144 if (a->vna_other == vernum)
1146 version_string = a->vna_nodename;
1147 break;
1153 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1154 fprintf (file, " %-11s", version_string);
1155 else
1157 int i;
1159 fprintf (file, " (%s)", version_string);
1160 for (i = 10 - strlen (version_string); i > 0; --i)
1161 putc (' ', file);
1165 /* If the st_other field is not zero, print it. */
1166 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1168 switch (st_other)
1170 case 0: break;
1171 case STV_INTERNAL: fprintf (file, " .internal"); break;
1172 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1173 case STV_PROTECTED: fprintf (file, " .protected"); break;
1174 default:
1175 /* Some other non-defined flags are also present, so print
1176 everything hex. */
1177 fprintf (file, " 0x%02x", (unsigned int) st_other);
1180 fprintf (file, " %s", name);
1182 break;
1186 /* Create an entry in an ELF linker hash table. */
1188 struct bfd_hash_entry *
1189 _bfd_elf_link_hash_newfunc (entry, table, string)
1190 struct bfd_hash_entry *entry;
1191 struct bfd_hash_table *table;
1192 const char *string;
1194 /* Allocate the structure if it has not already been allocated by a
1195 subclass. */
1196 if (entry == NULL)
1198 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1199 if (entry == NULL)
1200 return entry;
1203 /* Call the allocation method of the superclass. */
1204 entry = _bfd_link_hash_newfunc (entry, table, string);
1205 if (entry != NULL)
1207 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1208 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1210 /* Set local fields. */
1211 ret->indx = -1;
1212 ret->size = 0;
1213 ret->dynindx = -1;
1214 ret->dynstr_index = 0;
1215 ret->weakdef = NULL;
1216 ret->got.refcount = htab->init_refcount;
1217 ret->plt.refcount = htab->init_refcount;
1218 ret->linker_section_pointer = NULL;
1219 ret->verinfo.verdef = NULL;
1220 ret->vtable_entries_used = NULL;
1221 ret->vtable_entries_size = 0;
1222 ret->vtable_parent = NULL;
1223 ret->type = STT_NOTYPE;
1224 ret->other = 0;
1225 /* Assume that we have been called by a non-ELF symbol reader.
1226 This flag is then reset by the code which reads an ELF input
1227 file. This ensures that a symbol created by a non-ELF symbol
1228 reader will have the flag set correctly. */
1229 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1232 return entry;
1235 /* Copy data from an indirect symbol to its direct symbol, hiding the
1236 old indirect symbol. Also used for copying flags to a weakdef. */
1238 void
1239 _bfd_elf_link_hash_copy_indirect (dir, ind)
1240 struct elf_link_hash_entry *dir, *ind;
1242 bfd_signed_vma tmp;
1244 /* Copy down any references that we may have already seen to the
1245 symbol which just became indirect. */
1247 dir->elf_link_hash_flags |=
1248 (ind->elf_link_hash_flags
1249 & (ELF_LINK_HASH_REF_DYNAMIC
1250 | ELF_LINK_HASH_REF_REGULAR
1251 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1252 | ELF_LINK_NON_GOT_REF));
1254 if (ind->root.type != bfd_link_hash_indirect)
1255 return;
1257 /* Copy over the global and procedure linkage table refcount entries.
1258 These may have been already set up by a check_relocs routine. */
1259 tmp = dir->got.refcount;
1260 if (tmp <= 0)
1262 dir->got.refcount = ind->got.refcount;
1263 ind->got.refcount = tmp;
1265 else
1266 BFD_ASSERT (ind->got.refcount <= 0);
1268 tmp = dir->plt.refcount;
1269 if (tmp <= 0)
1271 dir->plt.refcount = ind->plt.refcount;
1272 ind->plt.refcount = tmp;
1274 else
1275 BFD_ASSERT (ind->plt.refcount <= 0);
1277 if (dir->dynindx == -1)
1279 dir->dynindx = ind->dynindx;
1280 dir->dynstr_index = ind->dynstr_index;
1281 ind->dynindx = -1;
1282 ind->dynstr_index = 0;
1284 else
1285 BFD_ASSERT (ind->dynindx == -1);
1288 void
1289 _bfd_elf_link_hash_hide_symbol (info, h)
1290 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1291 struct elf_link_hash_entry *h;
1293 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1294 h->plt.offset = (bfd_vma) -1;
1295 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1296 h->dynindx = -1;
1299 /* Initialize an ELF linker hash table. */
1301 boolean
1302 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1303 struct elf_link_hash_table *table;
1304 bfd *abfd;
1305 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1306 struct bfd_hash_table *,
1307 const char *));
1309 boolean ret;
1311 table->dynamic_sections_created = false;
1312 table->dynobj = NULL;
1313 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
1314 /* The first dynamic symbol is a dummy. */
1315 table->dynsymcount = 1;
1316 table->dynstr = NULL;
1317 table->bucketcount = 0;
1318 table->needed = NULL;
1319 table->runpath = NULL;
1320 table->hgot = NULL;
1321 table->stab_info = NULL;
1322 table->merge_info = NULL;
1323 table->dynlocal = NULL;
1324 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1325 table->root.type = bfd_link_elf_hash_table;
1327 return ret;
1330 /* Create an ELF linker hash table. */
1332 struct bfd_link_hash_table *
1333 _bfd_elf_link_hash_table_create (abfd)
1334 bfd *abfd;
1336 struct elf_link_hash_table *ret;
1337 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1339 ret = (struct elf_link_hash_table *) bfd_alloc (abfd, amt);
1340 if (ret == (struct elf_link_hash_table *) NULL)
1341 return NULL;
1343 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1345 bfd_release (abfd, ret);
1346 return NULL;
1349 return &ret->root;
1352 /* This is a hook for the ELF emulation code in the generic linker to
1353 tell the backend linker what file name to use for the DT_NEEDED
1354 entry for a dynamic object. The generic linker passes name as an
1355 empty string to indicate that no DT_NEEDED entry should be made. */
1357 void
1358 bfd_elf_set_dt_needed_name (abfd, name)
1359 bfd *abfd;
1360 const char *name;
1362 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1363 && bfd_get_format (abfd) == bfd_object)
1364 elf_dt_name (abfd) = name;
1367 void
1368 bfd_elf_set_dt_needed_soname (abfd, name)
1369 bfd *abfd;
1370 const char *name;
1372 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1373 && bfd_get_format (abfd) == bfd_object)
1374 elf_dt_soname (abfd) = name;
1377 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1378 the linker ELF emulation code. */
1380 struct bfd_link_needed_list *
1381 bfd_elf_get_needed_list (abfd, info)
1382 bfd *abfd ATTRIBUTE_UNUSED;
1383 struct bfd_link_info *info;
1385 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1386 return NULL;
1387 return elf_hash_table (info)->needed;
1390 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1391 hook for the linker ELF emulation code. */
1393 struct bfd_link_needed_list *
1394 bfd_elf_get_runpath_list (abfd, info)
1395 bfd *abfd ATTRIBUTE_UNUSED;
1396 struct bfd_link_info *info;
1398 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1399 return NULL;
1400 return elf_hash_table (info)->runpath;
1403 /* Get the name actually used for a dynamic object for a link. This
1404 is the SONAME entry if there is one. Otherwise, it is the string
1405 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1407 const char *
1408 bfd_elf_get_dt_soname (abfd)
1409 bfd *abfd;
1411 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1412 && bfd_get_format (abfd) == bfd_object)
1413 return elf_dt_name (abfd);
1414 return NULL;
1417 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1418 the ELF linker emulation code. */
1420 boolean
1421 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1422 bfd *abfd;
1423 struct bfd_link_needed_list **pneeded;
1425 asection *s;
1426 bfd_byte *dynbuf = NULL;
1427 int elfsec;
1428 unsigned long shlink;
1429 bfd_byte *extdyn, *extdynend;
1430 size_t extdynsize;
1431 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1433 *pneeded = NULL;
1435 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1436 || bfd_get_format (abfd) != bfd_object)
1437 return true;
1439 s = bfd_get_section_by_name (abfd, ".dynamic");
1440 if (s == NULL || s->_raw_size == 0)
1441 return true;
1443 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1444 if (dynbuf == NULL)
1445 goto error_return;
1447 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1448 s->_raw_size))
1449 goto error_return;
1451 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1452 if (elfsec == -1)
1453 goto error_return;
1455 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1457 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1458 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1460 extdyn = dynbuf;
1461 extdynend = extdyn + s->_raw_size;
1462 for (; extdyn < extdynend; extdyn += extdynsize)
1464 Elf_Internal_Dyn dyn;
1466 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1468 if (dyn.d_tag == DT_NULL)
1469 break;
1471 if (dyn.d_tag == DT_NEEDED)
1473 const char *string;
1474 struct bfd_link_needed_list *l;
1475 unsigned int tagv = dyn.d_un.d_val;
1476 bfd_size_type amt;
1478 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1479 if (string == NULL)
1480 goto error_return;
1482 amt = sizeof *l;
1483 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1484 if (l == NULL)
1485 goto error_return;
1487 l->by = abfd;
1488 l->name = string;
1489 l->next = *pneeded;
1490 *pneeded = l;
1494 free (dynbuf);
1496 return true;
1498 error_return:
1499 if (dynbuf != NULL)
1500 free (dynbuf);
1501 return false;
1504 /* Allocate an ELF string table--force the first byte to be zero. */
1506 struct bfd_strtab_hash *
1507 _bfd_elf_stringtab_init ()
1509 struct bfd_strtab_hash *ret;
1511 ret = _bfd_stringtab_init ();
1512 if (ret != NULL)
1514 bfd_size_type loc;
1516 loc = _bfd_stringtab_add (ret, "", true, false);
1517 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1518 if (loc == (bfd_size_type) -1)
1520 _bfd_stringtab_free (ret);
1521 ret = NULL;
1524 return ret;
1527 /* ELF .o/exec file reading */
1529 /* Create a new bfd section from an ELF section header. */
1531 boolean
1532 bfd_section_from_shdr (abfd, shindex)
1533 bfd *abfd;
1534 unsigned int shindex;
1536 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1537 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1538 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1539 char *name;
1541 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1543 switch (hdr->sh_type)
1545 case SHT_NULL:
1546 /* Inactive section. Throw it away. */
1547 return true;
1549 case SHT_PROGBITS: /* Normal section with contents. */
1550 case SHT_DYNAMIC: /* Dynamic linking information. */
1551 case SHT_NOBITS: /* .bss section. */
1552 case SHT_HASH: /* .hash section. */
1553 case SHT_NOTE: /* .note section. */
1554 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1556 case SHT_SYMTAB: /* A symbol table */
1557 if (elf_onesymtab (abfd) == shindex)
1558 return true;
1560 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1561 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1562 elf_onesymtab (abfd) = shindex;
1563 elf_tdata (abfd)->symtab_hdr = *hdr;
1564 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1565 abfd->flags |= HAS_SYMS;
1567 /* Sometimes a shared object will map in the symbol table. If
1568 SHF_ALLOC is set, and this is a shared object, then we also
1569 treat this section as a BFD section. We can not base the
1570 decision purely on SHF_ALLOC, because that flag is sometimes
1571 set in a relocateable object file, which would confuse the
1572 linker. */
1573 if ((hdr->sh_flags & SHF_ALLOC) != 0
1574 && (abfd->flags & DYNAMIC) != 0
1575 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1576 return false;
1578 return true;
1580 case SHT_DYNSYM: /* A dynamic symbol table */
1581 if (elf_dynsymtab (abfd) == shindex)
1582 return true;
1584 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1585 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1586 elf_dynsymtab (abfd) = shindex;
1587 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1588 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1589 abfd->flags |= HAS_SYMS;
1591 /* Besides being a symbol table, we also treat this as a regular
1592 section, so that objcopy can handle it. */
1593 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1595 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */
1596 if (elf_symtab_shndx (abfd) == shindex)
1597 return true;
1599 /* Get the associated symbol table. */
1600 if (! bfd_section_from_shdr (abfd, hdr->sh_link)
1601 || hdr->sh_link != elf_onesymtab (abfd))
1602 return false;
1604 elf_symtab_shndx (abfd) = shindex;
1605 elf_tdata (abfd)->symtab_shndx_hdr = *hdr;
1606 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr;
1607 return true;
1609 case SHT_STRTAB: /* A string table */
1610 if (hdr->bfd_section != NULL)
1611 return true;
1612 if (ehdr->e_shstrndx == shindex)
1614 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1615 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1616 return true;
1619 unsigned int i, num_sec;
1621 num_sec = elf_numsections (abfd);
1622 for (i = 1; i < num_sec; i++)
1624 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1625 if (hdr2->sh_link == shindex)
1627 if (! bfd_section_from_shdr (abfd, i))
1628 return false;
1629 if (elf_onesymtab (abfd) == i)
1631 elf_tdata (abfd)->strtab_hdr = *hdr;
1632 elf_elfsections (abfd)[shindex] =
1633 &elf_tdata (abfd)->strtab_hdr;
1634 return true;
1636 if (elf_dynsymtab (abfd) == i)
1638 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1639 elf_elfsections (abfd)[shindex] = hdr =
1640 &elf_tdata (abfd)->dynstrtab_hdr;
1641 /* We also treat this as a regular section, so
1642 that objcopy can handle it. */
1643 break;
1645 #if 0 /* Not handling other string tables specially right now. */
1646 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1647 /* We have a strtab for some random other section. */
1648 newsect = (asection *) hdr2->bfd_section;
1649 if (!newsect)
1650 break;
1651 hdr->bfd_section = newsect;
1652 hdr2 = &elf_section_data (newsect)->str_hdr;
1653 *hdr2 = *hdr;
1654 elf_elfsections (abfd)[shindex] = hdr2;
1655 #endif
1660 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1662 case SHT_REL:
1663 case SHT_RELA:
1664 /* *These* do a lot of work -- but build no sections! */
1666 asection *target_sect;
1667 Elf_Internal_Shdr *hdr2;
1668 unsigned int num_sec = elf_numsections (abfd);
1670 /* Check for a bogus link to avoid crashing. */
1671 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE)
1672 || hdr->sh_link >= num_sec)
1674 ((*_bfd_error_handler)
1675 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1676 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1677 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1680 /* For some incomprehensible reason Oracle distributes
1681 libraries for Solaris in which some of the objects have
1682 bogus sh_link fields. It would be nice if we could just
1683 reject them, but, unfortunately, some people need to use
1684 them. We scan through the section headers; if we find only
1685 one suitable symbol table, we clobber the sh_link to point
1686 to it. I hope this doesn't break anything. */
1687 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1688 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1690 unsigned int scan;
1691 int found;
1693 found = 0;
1694 for (scan = 1; scan < num_sec; scan++)
1696 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1697 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1699 if (found != 0)
1701 found = 0;
1702 break;
1704 found = scan;
1707 if (found != 0)
1708 hdr->sh_link = found;
1711 /* Get the symbol table. */
1712 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1713 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1714 return false;
1716 /* If this reloc section does not use the main symbol table we
1717 don't treat it as a reloc section. BFD can't adequately
1718 represent such a section, so at least for now, we don't
1719 try. We just present it as a normal section. We also
1720 can't use it as a reloc section if it points to the null
1721 section. */
1722 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1723 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1725 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1726 return false;
1727 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1728 if (target_sect == NULL)
1729 return false;
1731 if ((target_sect->flags & SEC_RELOC) == 0
1732 || target_sect->reloc_count == 0)
1733 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1734 else
1736 bfd_size_type amt;
1737 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1738 amt = sizeof (*hdr2);
1739 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1740 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1742 *hdr2 = *hdr;
1743 elf_elfsections (abfd)[shindex] = hdr2;
1744 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1745 target_sect->flags |= SEC_RELOC;
1746 target_sect->relocation = NULL;
1747 target_sect->rel_filepos = hdr->sh_offset;
1748 /* In the section to which the relocations apply, mark whether
1749 its relocations are of the REL or RELA variety. */
1750 if (hdr->sh_size != 0)
1751 elf_section_data (target_sect)->use_rela_p
1752 = (hdr->sh_type == SHT_RELA);
1753 abfd->flags |= HAS_RELOC;
1754 return true;
1756 break;
1758 case SHT_GNU_verdef:
1759 elf_dynverdef (abfd) = shindex;
1760 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1761 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1762 break;
1764 case SHT_GNU_versym:
1765 elf_dynversym (abfd) = shindex;
1766 elf_tdata (abfd)->dynversym_hdr = *hdr;
1767 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1768 break;
1770 case SHT_GNU_verneed:
1771 elf_dynverref (abfd) = shindex;
1772 elf_tdata (abfd)->dynverref_hdr = *hdr;
1773 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1774 break;
1776 case SHT_SHLIB:
1777 return true;
1779 case SHT_GROUP:
1780 /* Make a section for objcopy and relocatable links. */
1781 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1782 return false;
1783 if (hdr->contents != NULL)
1785 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1786 unsigned int n_elt = hdr->sh_size / 4;
1787 asection *s;
1789 while (--n_elt != 0)
1790 if ((s = (++idx)->shdr->bfd_section) != NULL
1791 && elf_next_in_group (s) != NULL)
1793 elf_next_in_group (hdr->bfd_section) = s;
1794 break;
1797 break;
1799 default:
1800 /* Check for any processor-specific section types. */
1802 if (bed->elf_backend_section_from_shdr)
1803 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1805 break;
1808 return true;
1811 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1812 Return SEC for sections that have no elf section, and NULL on error. */
1814 asection *
1815 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
1816 bfd *abfd;
1817 struct sym_sec_cache *cache;
1818 asection *sec;
1819 unsigned long r_symndx;
1821 unsigned char esym_shndx[4];
1822 unsigned int isym_shndx;
1823 Elf_Internal_Shdr *symtab_hdr;
1824 file_ptr pos;
1825 bfd_size_type amt;
1826 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1828 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1829 return cache->sec[ent];
1831 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1832 pos = symtab_hdr->sh_offset;
1833 if (get_elf_backend_data (abfd)->s->sizeof_sym
1834 == sizeof (Elf64_External_Sym))
1836 pos += r_symndx * sizeof (Elf64_External_Sym);
1837 pos += offsetof (Elf64_External_Sym, st_shndx);
1838 amt = sizeof (((Elf64_External_Sym *) 0)->st_shndx);
1840 else
1842 pos += r_symndx * sizeof (Elf32_External_Sym);
1843 pos += offsetof (Elf32_External_Sym, st_shndx);
1844 amt = sizeof (((Elf32_External_Sym *) 0)->st_shndx);
1846 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1847 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1848 return NULL;
1849 isym_shndx = H_GET_16 (abfd, esym_shndx);
1851 if (isym_shndx == SHN_XINDEX)
1853 Elf_Internal_Shdr *shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1854 if (shndx_hdr->sh_size != 0)
1856 pos = shndx_hdr->sh_offset;
1857 pos += r_symndx * sizeof (Elf_External_Sym_Shndx);
1858 amt = sizeof (Elf_External_Sym_Shndx);
1859 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1860 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1861 return NULL;
1862 isym_shndx = H_GET_32 (abfd, esym_shndx);
1866 if (cache->abfd != abfd)
1868 memset (cache->indx, -1, sizeof (cache->indx));
1869 cache->abfd = abfd;
1871 cache->indx[ent] = r_symndx;
1872 cache->sec[ent] = sec;
1873 if (isym_shndx < SHN_LORESERVE || isym_shndx > SHN_HIRESERVE)
1875 asection *s;
1876 s = bfd_section_from_elf_index (abfd, isym_shndx);
1877 if (s != NULL)
1878 cache->sec[ent] = s;
1880 return cache->sec[ent];
1883 /* Given an ELF section number, retrieve the corresponding BFD
1884 section. */
1886 asection *
1887 bfd_section_from_elf_index (abfd, index)
1888 bfd *abfd;
1889 unsigned int index;
1891 if (index >= elf_numsections (abfd))
1892 return NULL;
1893 return elf_elfsections (abfd)[index]->bfd_section;
1896 boolean
1897 _bfd_elf_new_section_hook (abfd, sec)
1898 bfd *abfd;
1899 asection *sec;
1901 struct bfd_elf_section_data *sdata;
1902 bfd_size_type amt = sizeof (*sdata);
1904 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
1905 if (!sdata)
1906 return false;
1907 sec->used_by_bfd = (PTR) sdata;
1909 /* Indicate whether or not this section should use RELA relocations. */
1910 sdata->use_rela_p
1911 = get_elf_backend_data (abfd)->default_use_rela_p;
1913 return true;
1916 /* Create a new bfd section from an ELF program header.
1918 Since program segments have no names, we generate a synthetic name
1919 of the form segment<NUM>, where NUM is generally the index in the
1920 program header table. For segments that are split (see below) we
1921 generate the names segment<NUM>a and segment<NUM>b.
1923 Note that some program segments may have a file size that is different than
1924 (less than) the memory size. All this means is that at execution the
1925 system must allocate the amount of memory specified by the memory size,
1926 but only initialize it with the first "file size" bytes read from the
1927 file. This would occur for example, with program segments consisting
1928 of combined data+bss.
1930 To handle the above situation, this routine generates TWO bfd sections
1931 for the single program segment. The first has the length specified by
1932 the file size of the segment, and the second has the length specified
1933 by the difference between the two sizes. In effect, the segment is split
1934 into it's initialized and uninitialized parts.
1938 boolean
1939 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1940 bfd *abfd;
1941 Elf_Internal_Phdr *hdr;
1942 int index;
1943 const char *typename;
1945 asection *newsect;
1946 char *name;
1947 char namebuf[64];
1948 int split;
1950 split = ((hdr->p_memsz > 0)
1951 && (hdr->p_filesz > 0)
1952 && (hdr->p_memsz > hdr->p_filesz));
1953 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1954 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1955 if (!name)
1956 return false;
1957 strcpy (name, namebuf);
1958 newsect = bfd_make_section (abfd, name);
1959 if (newsect == NULL)
1960 return false;
1961 newsect->vma = hdr->p_vaddr;
1962 newsect->lma = hdr->p_paddr;
1963 newsect->_raw_size = hdr->p_filesz;
1964 newsect->filepos = hdr->p_offset;
1965 newsect->flags |= SEC_HAS_CONTENTS;
1966 if (hdr->p_type == PT_LOAD)
1968 newsect->flags |= SEC_ALLOC;
1969 newsect->flags |= SEC_LOAD;
1970 if (hdr->p_flags & PF_X)
1972 /* FIXME: all we known is that it has execute PERMISSION,
1973 may be data. */
1974 newsect->flags |= SEC_CODE;
1977 if (!(hdr->p_flags & PF_W))
1979 newsect->flags |= SEC_READONLY;
1982 if (split)
1984 sprintf (namebuf, "%s%db", typename, index);
1985 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1986 if (!name)
1987 return false;
1988 strcpy (name, namebuf);
1989 newsect = bfd_make_section (abfd, name);
1990 if (newsect == NULL)
1991 return false;
1992 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1993 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1994 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1995 if (hdr->p_type == PT_LOAD)
1997 newsect->flags |= SEC_ALLOC;
1998 if (hdr->p_flags & PF_X)
1999 newsect->flags |= SEC_CODE;
2001 if (!(hdr->p_flags & PF_W))
2002 newsect->flags |= SEC_READONLY;
2005 return true;
2008 boolean
2009 bfd_section_from_phdr (abfd, hdr, index)
2010 bfd *abfd;
2011 Elf_Internal_Phdr *hdr;
2012 int index;
2014 struct elf_backend_data *bed;
2016 switch (hdr->p_type)
2018 case PT_NULL:
2019 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
2021 case PT_LOAD:
2022 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
2024 case PT_DYNAMIC:
2025 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
2027 case PT_INTERP:
2028 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
2030 case PT_NOTE:
2031 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
2032 return false;
2033 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
2034 return false;
2035 return true;
2037 case PT_SHLIB:
2038 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
2040 case PT_PHDR:
2041 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
2043 default:
2044 /* Check for any processor-specific program segment types.
2045 If no handler for them, default to making "segment" sections. */
2046 bed = get_elf_backend_data (abfd);
2047 if (bed->elf_backend_section_from_phdr)
2048 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
2049 else
2050 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
2054 /* Initialize REL_HDR, the section-header for new section, containing
2055 relocations against ASECT. If USE_RELA_P is true, we use RELA
2056 relocations; otherwise, we use REL relocations. */
2058 boolean
2059 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
2060 bfd *abfd;
2061 Elf_Internal_Shdr *rel_hdr;
2062 asection *asect;
2063 boolean use_rela_p;
2065 char *name;
2066 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2067 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
2069 name = bfd_alloc (abfd, amt);
2070 if (name == NULL)
2071 return false;
2072 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
2073 rel_hdr->sh_name =
2074 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
2075 false);
2076 if (rel_hdr->sh_name == (unsigned int) -1)
2077 return false;
2078 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2079 rel_hdr->sh_entsize = (use_rela_p
2080 ? bed->s->sizeof_rela
2081 : bed->s->sizeof_rel);
2082 rel_hdr->sh_addralign = bed->s->file_align;
2083 rel_hdr->sh_flags = 0;
2084 rel_hdr->sh_addr = 0;
2085 rel_hdr->sh_size = 0;
2086 rel_hdr->sh_offset = 0;
2088 return true;
2091 /* Set up an ELF internal section header for a section. */
2093 static void
2094 elf_fake_sections (abfd, asect, failedptrarg)
2095 bfd *abfd;
2096 asection *asect;
2097 PTR failedptrarg;
2099 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2100 boolean *failedptr = (boolean *) failedptrarg;
2101 Elf_Internal_Shdr *this_hdr;
2103 if (*failedptr)
2105 /* We already failed; just get out of the bfd_map_over_sections
2106 loop. */
2107 return;
2110 this_hdr = &elf_section_data (asect)->this_hdr;
2112 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2113 asect->name, false);
2114 if (this_hdr->sh_name == (unsigned long) -1)
2116 *failedptr = true;
2117 return;
2120 this_hdr->sh_flags = 0;
2122 if ((asect->flags & SEC_ALLOC) != 0
2123 || asect->user_set_vma)
2124 this_hdr->sh_addr = asect->vma;
2125 else
2126 this_hdr->sh_addr = 0;
2128 this_hdr->sh_offset = 0;
2129 this_hdr->sh_size = asect->_raw_size;
2130 this_hdr->sh_link = 0;
2131 this_hdr->sh_addralign = 1 << asect->alignment_power;
2132 /* The sh_entsize and sh_info fields may have been set already by
2133 copy_private_section_data. */
2135 this_hdr->bfd_section = asect;
2136 this_hdr->contents = NULL;
2138 /* FIXME: This should not be based on section names. */
2139 if (strcmp (asect->name, ".dynstr") == 0)
2140 this_hdr->sh_type = SHT_STRTAB;
2141 else if (strcmp (asect->name, ".hash") == 0)
2143 this_hdr->sh_type = SHT_HASH;
2144 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2146 else if (strcmp (asect->name, ".dynsym") == 0)
2148 this_hdr->sh_type = SHT_DYNSYM;
2149 this_hdr->sh_entsize = bed->s->sizeof_sym;
2151 else if (strcmp (asect->name, ".dynamic") == 0)
2153 this_hdr->sh_type = SHT_DYNAMIC;
2154 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2156 else if (strncmp (asect->name, ".rela", 5) == 0
2157 && get_elf_backend_data (abfd)->may_use_rela_p)
2159 this_hdr->sh_type = SHT_RELA;
2160 this_hdr->sh_entsize = bed->s->sizeof_rela;
2162 else if (strncmp (asect->name, ".rel", 4) == 0
2163 && get_elf_backend_data (abfd)->may_use_rel_p)
2165 this_hdr->sh_type = SHT_REL;
2166 this_hdr->sh_entsize = bed->s->sizeof_rel;
2168 else if (strncmp (asect->name, ".note", 5) == 0)
2169 this_hdr->sh_type = SHT_NOTE;
2170 else if (strncmp (asect->name, ".stab", 5) == 0
2171 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2172 this_hdr->sh_type = SHT_STRTAB;
2173 else if (strcmp (asect->name, ".gnu.version") == 0)
2175 this_hdr->sh_type = SHT_GNU_versym;
2176 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2178 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2180 this_hdr->sh_type = SHT_GNU_verdef;
2181 this_hdr->sh_entsize = 0;
2182 /* objcopy or strip will copy over sh_info, but may not set
2183 cverdefs. The linker will set cverdefs, but sh_info will be
2184 zero. */
2185 if (this_hdr->sh_info == 0)
2186 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2187 else
2188 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2189 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2191 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2193 this_hdr->sh_type = SHT_GNU_verneed;
2194 this_hdr->sh_entsize = 0;
2195 /* objcopy or strip will copy over sh_info, but may not set
2196 cverrefs. The linker will set cverrefs, but sh_info will be
2197 zero. */
2198 if (this_hdr->sh_info == 0)
2199 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2200 else
2201 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2202 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2204 else if ((asect->flags & SEC_GROUP) != 0)
2206 this_hdr->sh_type = SHT_GROUP;
2207 this_hdr->sh_entsize = 4;
2209 else if ((asect->flags & SEC_ALLOC) != 0
2210 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
2211 this_hdr->sh_type = SHT_NOBITS;
2212 else
2213 this_hdr->sh_type = SHT_PROGBITS;
2215 if ((asect->flags & SEC_ALLOC) != 0)
2216 this_hdr->sh_flags |= SHF_ALLOC;
2217 if ((asect->flags & SEC_READONLY) == 0)
2218 this_hdr->sh_flags |= SHF_WRITE;
2219 if ((asect->flags & SEC_CODE) != 0)
2220 this_hdr->sh_flags |= SHF_EXECINSTR;
2221 if ((asect->flags & SEC_MERGE) != 0)
2223 this_hdr->sh_flags |= SHF_MERGE;
2224 this_hdr->sh_entsize = asect->entsize;
2225 if ((asect->flags & SEC_STRINGS) != 0)
2226 this_hdr->sh_flags |= SHF_STRINGS;
2228 if (elf_group_name (asect) != NULL)
2229 this_hdr->sh_flags |= SHF_GROUP;
2231 /* Check for processor-specific section types. */
2232 if (bed->elf_backend_fake_sections
2233 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect))
2234 *failedptr = true;
2236 /* If the section has relocs, set up a section header for the
2237 SHT_REL[A] section. If two relocation sections are required for
2238 this section, it is up to the processor-specific back-end to
2239 create the other. */
2240 if ((asect->flags & SEC_RELOC) != 0
2241 && !_bfd_elf_init_reloc_shdr (abfd,
2242 &elf_section_data (asect)->rel_hdr,
2243 asect,
2244 elf_section_data (asect)->use_rela_p))
2245 *failedptr = true;
2248 /* Fill in the contents of a SHT_GROUP section. */
2250 static void
2251 set_group_contents (abfd, sec, failedptrarg)
2252 bfd *abfd;
2253 asection *sec;
2254 PTR failedptrarg ATTRIBUTE_UNUSED;
2256 boolean *failedptr = (boolean *) failedptrarg;
2257 unsigned long symindx;
2258 asection *elt;
2259 unsigned char *loc;
2260 struct bfd_link_order *l;
2262 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2263 || *failedptr)
2264 return;
2266 /* If called from the assembler, swap_out_syms will have set up
2267 elf_section_syms; If called for "ld -r", the symbols won't yet
2268 be mapped, so emulate elf_bfd_final_link. */
2269 if (elf_section_syms (abfd) != NULL)
2270 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2271 else
2272 symindx = elf_section_data (sec)->this_idx;
2273 elf_section_data (sec)->this_hdr.sh_info = symindx;
2275 /* Nor will the contents be allocated for "ld -r". */
2276 if (sec->contents == NULL)
2278 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2279 if (sec->contents == NULL)
2281 *failedptr = true;
2282 return;
2286 loc = sec->contents + sec->_raw_size;
2288 /* Get the pointer to the first section in the group that we
2289 squirreled away here. */
2290 elt = elf_next_in_group (sec);
2292 /* First element is a flag word. Rest of section is elf section
2293 indices for all the sections of the group. Write them backwards
2294 just to keep the group in the same order as given in .section
2295 directives, not that it matters. */
2296 while (elt != NULL)
2298 loc -= 4;
2299 H_PUT_32 (abfd, elf_section_data (elt)->this_idx, loc);
2300 elt = elf_next_in_group (elt);
2303 /* If this is a relocatable link, then the above did nothing because
2304 SEC is the output section. Look through the input sections
2305 instead. */
2306 for (l = sec->link_order_head; l != NULL; l = l->next)
2307 if (l->type == bfd_indirect_link_order
2308 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2311 loc -= 4;
2312 H_PUT_32 (abfd,
2313 elf_section_data (elt->output_section)->this_idx, loc);
2314 elt = elf_next_in_group (elt);
2315 /* During a relocatable link, the lists are circular. */
2317 while (elt != elf_next_in_group (l->u.indirect.section));
2319 loc -= 4;
2320 H_PUT_32 (abfd, 0, loc);
2322 BFD_ASSERT (loc == sec->contents);
2325 /* Assign all ELF section numbers. The dummy first section is handled here
2326 too. The link/info pointers for the standard section types are filled
2327 in here too, while we're at it. */
2329 static boolean
2330 assign_section_numbers (abfd)
2331 bfd *abfd;
2333 struct elf_obj_tdata *t = elf_tdata (abfd);
2334 asection *sec;
2335 unsigned int section_number, secn;
2336 Elf_Internal_Shdr **i_shdrp;
2337 bfd_size_type amt;
2339 section_number = 1;
2341 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2343 for (sec = abfd->sections; sec; sec = sec->next)
2345 struct bfd_elf_section_data *d = elf_section_data (sec);
2347 if (section_number == SHN_LORESERVE)
2348 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2349 d->this_idx = section_number++;
2350 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2351 if ((sec->flags & SEC_RELOC) == 0)
2352 d->rel_idx = 0;
2353 else
2355 if (section_number == SHN_LORESERVE)
2356 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2357 d->rel_idx = section_number++;
2358 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2361 if (d->rel_hdr2)
2363 if (section_number == SHN_LORESERVE)
2364 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2365 d->rel_idx2 = section_number++;
2366 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2368 else
2369 d->rel_idx2 = 0;
2372 if (section_number == SHN_LORESERVE)
2373 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2374 t->shstrtab_section = section_number++;
2375 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2376 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2378 if (bfd_get_symcount (abfd) > 0)
2380 if (section_number == SHN_LORESERVE)
2381 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2382 t->symtab_section = section_number++;
2383 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2384 if (section_number > SHN_LORESERVE - 2)
2386 if (section_number == SHN_LORESERVE)
2387 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2388 t->symtab_shndx_section = section_number++;
2389 t->symtab_shndx_hdr.sh_name
2390 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2391 ".symtab_shndx", false);
2392 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1)
2393 return false;
2395 if (section_number == SHN_LORESERVE)
2396 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE;
2397 t->strtab_section = section_number++;
2398 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2401 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2402 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2404 elf_numsections (abfd) = section_number;
2405 elf_elfheader (abfd)->e_shnum = section_number;
2406 if (section_number > SHN_LORESERVE)
2407 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE;
2409 /* Set up the list of section header pointers, in agreement with the
2410 indices. */
2411 amt = section_number * sizeof (Elf_Internal_Shdr *);
2412 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
2413 if (i_shdrp == NULL)
2414 return false;
2416 amt = sizeof (Elf_Internal_Shdr);
2417 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2418 if (i_shdrp[0] == NULL)
2420 bfd_release (abfd, i_shdrp);
2421 return false;
2423 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2425 elf_elfsections (abfd) = i_shdrp;
2427 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2428 if (bfd_get_symcount (abfd) > 0)
2430 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2431 if (elf_numsections (abfd) > SHN_LORESERVE)
2433 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr;
2434 t->symtab_shndx_hdr.sh_link = t->symtab_section;
2436 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2437 t->symtab_hdr.sh_link = t->strtab_section;
2439 for (sec = abfd->sections; sec; sec = sec->next)
2441 struct bfd_elf_section_data *d = elf_section_data (sec);
2442 asection *s;
2443 const char *name;
2445 i_shdrp[d->this_idx] = &d->this_hdr;
2446 if (d->rel_idx != 0)
2447 i_shdrp[d->rel_idx] = &d->rel_hdr;
2448 if (d->rel_idx2 != 0)
2449 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2451 /* Fill in the sh_link and sh_info fields while we're at it. */
2453 /* sh_link of a reloc section is the section index of the symbol
2454 table. sh_info is the section index of the section to which
2455 the relocation entries apply. */
2456 if (d->rel_idx != 0)
2458 d->rel_hdr.sh_link = t->symtab_section;
2459 d->rel_hdr.sh_info = d->this_idx;
2461 if (d->rel_idx2 != 0)
2463 d->rel_hdr2->sh_link = t->symtab_section;
2464 d->rel_hdr2->sh_info = d->this_idx;
2467 switch (d->this_hdr.sh_type)
2469 case SHT_REL:
2470 case SHT_RELA:
2471 /* A reloc section which we are treating as a normal BFD
2472 section. sh_link is the section index of the symbol
2473 table. sh_info is the section index of the section to
2474 which the relocation entries apply. We assume that an
2475 allocated reloc section uses the dynamic symbol table.
2476 FIXME: How can we be sure? */
2477 s = bfd_get_section_by_name (abfd, ".dynsym");
2478 if (s != NULL)
2479 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2481 /* We look up the section the relocs apply to by name. */
2482 name = sec->name;
2483 if (d->this_hdr.sh_type == SHT_REL)
2484 name += 4;
2485 else
2486 name += 5;
2487 s = bfd_get_section_by_name (abfd, name);
2488 if (s != NULL)
2489 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2490 break;
2492 case SHT_STRTAB:
2493 /* We assume that a section named .stab*str is a stabs
2494 string section. We look for a section with the same name
2495 but without the trailing ``str'', and set its sh_link
2496 field to point to this section. */
2497 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2498 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2500 size_t len;
2501 char *alc;
2503 len = strlen (sec->name);
2504 alc = (char *) bfd_malloc ((bfd_size_type) len - 2);
2505 if (alc == NULL)
2506 return false;
2507 strncpy (alc, sec->name, len - 3);
2508 alc[len - 3] = '\0';
2509 s = bfd_get_section_by_name (abfd, alc);
2510 free (alc);
2511 if (s != NULL)
2513 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2515 /* This is a .stab section. */
2516 elf_section_data (s)->this_hdr.sh_entsize =
2517 4 + 2 * bfd_get_arch_size (abfd) / 8;
2520 break;
2522 case SHT_DYNAMIC:
2523 case SHT_DYNSYM:
2524 case SHT_GNU_verneed:
2525 case SHT_GNU_verdef:
2526 /* sh_link is the section header index of the string table
2527 used for the dynamic entries, or the symbol table, or the
2528 version strings. */
2529 s = bfd_get_section_by_name (abfd, ".dynstr");
2530 if (s != NULL)
2531 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2532 break;
2534 case SHT_HASH:
2535 case SHT_GNU_versym:
2536 /* sh_link is the section header index of the symbol table
2537 this hash table or version table is for. */
2538 s = bfd_get_section_by_name (abfd, ".dynsym");
2539 if (s != NULL)
2540 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2541 break;
2543 case SHT_GROUP:
2544 d->this_hdr.sh_link = t->symtab_section;
2548 for (secn = 1; secn < section_number; ++secn)
2549 if (i_shdrp[secn] == NULL)
2550 i_shdrp[secn] = i_shdrp[0];
2551 else
2552 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2553 i_shdrp[secn]->sh_name);
2554 return true;
2557 /* Map symbol from it's internal number to the external number, moving
2558 all local symbols to be at the head of the list. */
2560 static INLINE int
2561 sym_is_global (abfd, sym)
2562 bfd *abfd;
2563 asymbol *sym;
2565 /* If the backend has a special mapping, use it. */
2566 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2567 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2568 (abfd, sym));
2570 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2571 || bfd_is_und_section (bfd_get_section (sym))
2572 || bfd_is_com_section (bfd_get_section (sym)));
2575 static boolean
2576 elf_map_symbols (abfd)
2577 bfd *abfd;
2579 unsigned int symcount = bfd_get_symcount (abfd);
2580 asymbol **syms = bfd_get_outsymbols (abfd);
2581 asymbol **sect_syms;
2582 unsigned int num_locals = 0;
2583 unsigned int num_globals = 0;
2584 unsigned int num_locals2 = 0;
2585 unsigned int num_globals2 = 0;
2586 int max_index = 0;
2587 unsigned int idx;
2588 asection *asect;
2589 asymbol **new_syms;
2590 bfd_size_type amt;
2592 #ifdef DEBUG
2593 fprintf (stderr, "elf_map_symbols\n");
2594 fflush (stderr);
2595 #endif
2597 for (asect = abfd->sections; asect; asect = asect->next)
2599 if (max_index < asect->index)
2600 max_index = asect->index;
2603 max_index++;
2604 amt = max_index * sizeof (asymbol *);
2605 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2606 if (sect_syms == NULL)
2607 return false;
2608 elf_section_syms (abfd) = sect_syms;
2609 elf_num_section_syms (abfd) = max_index;
2611 /* Init sect_syms entries for any section symbols we have already
2612 decided to output. */
2613 for (idx = 0; idx < symcount; idx++)
2615 asymbol *sym = syms[idx];
2617 if ((sym->flags & BSF_SECTION_SYM) != 0
2618 && sym->value == 0)
2620 asection *sec;
2622 sec = sym->section;
2624 if (sec->owner != NULL)
2626 if (sec->owner != abfd)
2628 if (sec->output_offset != 0)
2629 continue;
2631 sec = sec->output_section;
2633 /* Empty sections in the input files may have had a
2634 section symbol created for them. (See the comment
2635 near the end of _bfd_generic_link_output_symbols in
2636 linker.c). If the linker script discards such
2637 sections then we will reach this point. Since we know
2638 that we cannot avoid this case, we detect it and skip
2639 the abort and the assignment to the sect_syms array.
2640 To reproduce this particular case try running the
2641 linker testsuite test ld-scripts/weak.exp for an ELF
2642 port that uses the generic linker. */
2643 if (sec->owner == NULL)
2644 continue;
2646 BFD_ASSERT (sec->owner == abfd);
2648 sect_syms[sec->index] = syms[idx];
2653 /* Classify all of the symbols. */
2654 for (idx = 0; idx < symcount; idx++)
2656 if (!sym_is_global (abfd, syms[idx]))
2657 num_locals++;
2658 else
2659 num_globals++;
2662 /* We will be adding a section symbol for each BFD section. Most normal
2663 sections will already have a section symbol in outsymbols, but
2664 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2665 at least in that case. */
2666 for (asect = abfd->sections; asect; asect = asect->next)
2668 if (sect_syms[asect->index] == NULL)
2670 if (!sym_is_global (abfd, asect->symbol))
2671 num_locals++;
2672 else
2673 num_globals++;
2677 /* Now sort the symbols so the local symbols are first. */
2678 amt = (num_locals + num_globals) * sizeof (asymbol *);
2679 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2681 if (new_syms == NULL)
2682 return false;
2684 for (idx = 0; idx < symcount; idx++)
2686 asymbol *sym = syms[idx];
2687 unsigned int i;
2689 if (!sym_is_global (abfd, sym))
2690 i = num_locals2++;
2691 else
2692 i = num_locals + num_globals2++;
2693 new_syms[i] = sym;
2694 sym->udata.i = i + 1;
2696 for (asect = abfd->sections; asect; asect = asect->next)
2698 if (sect_syms[asect->index] == NULL)
2700 asymbol *sym = asect->symbol;
2701 unsigned int i;
2703 sect_syms[asect->index] = sym;
2704 if (!sym_is_global (abfd, sym))
2705 i = num_locals2++;
2706 else
2707 i = num_locals + num_globals2++;
2708 new_syms[i] = sym;
2709 sym->udata.i = i + 1;
2713 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2715 elf_num_locals (abfd) = num_locals;
2716 elf_num_globals (abfd) = num_globals;
2717 return true;
2720 /* Align to the maximum file alignment that could be required for any
2721 ELF data structure. */
2723 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2724 static INLINE file_ptr
2725 align_file_position (off, align)
2726 file_ptr off;
2727 int align;
2729 return (off + align - 1) & ~(align - 1);
2732 /* Assign a file position to a section, optionally aligning to the
2733 required section alignment. */
2735 INLINE file_ptr
2736 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2737 Elf_Internal_Shdr *i_shdrp;
2738 file_ptr offset;
2739 boolean align;
2741 if (align)
2743 unsigned int al;
2745 al = i_shdrp->sh_addralign;
2746 if (al > 1)
2747 offset = BFD_ALIGN (offset, al);
2749 i_shdrp->sh_offset = offset;
2750 if (i_shdrp->bfd_section != NULL)
2751 i_shdrp->bfd_section->filepos = offset;
2752 if (i_shdrp->sh_type != SHT_NOBITS)
2753 offset += i_shdrp->sh_size;
2754 return offset;
2757 /* Compute the file positions we are going to put the sections at, and
2758 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2759 is not NULL, this is being called by the ELF backend linker. */
2761 boolean
2762 _bfd_elf_compute_section_file_positions (abfd, link_info)
2763 bfd *abfd;
2764 struct bfd_link_info *link_info;
2766 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2767 boolean failed;
2768 struct bfd_strtab_hash *strtab;
2769 Elf_Internal_Shdr *shstrtab_hdr;
2771 if (abfd->output_has_begun)
2772 return true;
2774 /* Do any elf backend specific processing first. */
2775 if (bed->elf_backend_begin_write_processing)
2776 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2778 if (! prep_headers (abfd))
2779 return false;
2781 /* Post process the headers if necessary. */
2782 if (bed->elf_backend_post_process_headers)
2783 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2785 failed = false;
2786 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2787 if (failed)
2788 return false;
2790 if (!assign_section_numbers (abfd))
2791 return false;
2793 /* The backend linker builds symbol table information itself. */
2794 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2796 /* Non-zero if doing a relocatable link. */
2797 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2799 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2800 return false;
2803 if (link_info == NULL || link_info->relocateable)
2805 bfd_map_over_sections (abfd, set_group_contents, &failed);
2806 if (failed)
2807 return false;
2810 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2811 /* sh_name was set in prep_headers. */
2812 shstrtab_hdr->sh_type = SHT_STRTAB;
2813 shstrtab_hdr->sh_flags = 0;
2814 shstrtab_hdr->sh_addr = 0;
2815 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2816 shstrtab_hdr->sh_entsize = 0;
2817 shstrtab_hdr->sh_link = 0;
2818 shstrtab_hdr->sh_info = 0;
2819 /* sh_offset is set in assign_file_positions_except_relocs. */
2820 shstrtab_hdr->sh_addralign = 1;
2822 if (!assign_file_positions_except_relocs (abfd))
2823 return false;
2825 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2827 file_ptr off;
2828 Elf_Internal_Shdr *hdr;
2830 off = elf_tdata (abfd)->next_file_pos;
2832 hdr = &elf_tdata (abfd)->symtab_hdr;
2833 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2835 hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2836 if (hdr->sh_size != 0)
2837 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2839 hdr = &elf_tdata (abfd)->strtab_hdr;
2840 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2842 elf_tdata (abfd)->next_file_pos = off;
2844 /* Now that we know where the .strtab section goes, write it
2845 out. */
2846 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2847 || ! _bfd_stringtab_emit (abfd, strtab))
2848 return false;
2849 _bfd_stringtab_free (strtab);
2852 abfd->output_has_begun = true;
2854 return true;
2857 /* Create a mapping from a set of sections to a program segment. */
2859 static INLINE struct elf_segment_map *
2860 make_mapping (abfd, sections, from, to, phdr)
2861 bfd *abfd;
2862 asection **sections;
2863 unsigned int from;
2864 unsigned int to;
2865 boolean phdr;
2867 struct elf_segment_map *m;
2868 unsigned int i;
2869 asection **hdrpp;
2870 bfd_size_type amt;
2872 amt = sizeof (struct elf_segment_map);
2873 amt += (to - from - 1) * sizeof (asection *);
2874 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2875 if (m == NULL)
2876 return NULL;
2877 m->next = NULL;
2878 m->p_type = PT_LOAD;
2879 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2880 m->sections[i - from] = *hdrpp;
2881 m->count = to - from;
2883 if (from == 0 && phdr)
2885 /* Include the headers in the first PT_LOAD segment. */
2886 m->includes_filehdr = 1;
2887 m->includes_phdrs = 1;
2890 return m;
2893 /* Set up a mapping from BFD sections to program segments. */
2895 static boolean
2896 map_sections_to_segments (abfd)
2897 bfd *abfd;
2899 asection **sections = NULL;
2900 asection *s;
2901 unsigned int i;
2902 unsigned int count;
2903 struct elf_segment_map *mfirst;
2904 struct elf_segment_map **pm;
2905 struct elf_segment_map *m;
2906 asection *last_hdr;
2907 unsigned int phdr_index;
2908 bfd_vma maxpagesize;
2909 asection **hdrpp;
2910 boolean phdr_in_segment = true;
2911 boolean writable;
2912 asection *dynsec, *eh_frame_hdr;
2913 bfd_size_type amt;
2915 if (elf_tdata (abfd)->segment_map != NULL)
2916 return true;
2918 if (bfd_count_sections (abfd) == 0)
2919 return true;
2921 /* Select the allocated sections, and sort them. */
2923 amt = bfd_count_sections (abfd) * sizeof (asection *);
2924 sections = (asection **) bfd_malloc (amt);
2925 if (sections == NULL)
2926 goto error_return;
2928 i = 0;
2929 for (s = abfd->sections; s != NULL; s = s->next)
2931 if ((s->flags & SEC_ALLOC) != 0)
2933 sections[i] = s;
2934 ++i;
2937 BFD_ASSERT (i <= bfd_count_sections (abfd));
2938 count = i;
2940 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2942 /* Build the mapping. */
2944 mfirst = NULL;
2945 pm = &mfirst;
2947 /* If we have a .interp section, then create a PT_PHDR segment for
2948 the program headers and a PT_INTERP segment for the .interp
2949 section. */
2950 s = bfd_get_section_by_name (abfd, ".interp");
2951 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2953 amt = sizeof (struct elf_segment_map);
2954 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2955 if (m == NULL)
2956 goto error_return;
2957 m->next = NULL;
2958 m->p_type = PT_PHDR;
2959 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2960 m->p_flags = PF_R | PF_X;
2961 m->p_flags_valid = 1;
2962 m->includes_phdrs = 1;
2964 *pm = m;
2965 pm = &m->next;
2967 amt = sizeof (struct elf_segment_map);
2968 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2969 if (m == NULL)
2970 goto error_return;
2971 m->next = NULL;
2972 m->p_type = PT_INTERP;
2973 m->count = 1;
2974 m->sections[0] = s;
2976 *pm = m;
2977 pm = &m->next;
2980 /* Look through the sections. We put sections in the same program
2981 segment when the start of the second section can be placed within
2982 a few bytes of the end of the first section. */
2983 last_hdr = NULL;
2984 phdr_index = 0;
2985 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2986 writable = false;
2987 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2988 if (dynsec != NULL
2989 && (dynsec->flags & SEC_LOAD) == 0)
2990 dynsec = NULL;
2992 /* Deal with -Ttext or something similar such that the first section
2993 is not adjacent to the program headers. This is an
2994 approximation, since at this point we don't know exactly how many
2995 program headers we will need. */
2996 if (count > 0)
2998 bfd_size_type phdr_size;
3000 phdr_size = elf_tdata (abfd)->program_header_size;
3001 if (phdr_size == 0)
3002 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
3003 if ((abfd->flags & D_PAGED) == 0
3004 || sections[0]->lma < phdr_size
3005 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
3006 phdr_in_segment = false;
3009 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
3011 asection *hdr;
3012 boolean new_segment;
3014 hdr = *hdrpp;
3016 /* See if this section and the last one will fit in the same
3017 segment. */
3019 if (last_hdr == NULL)
3021 /* If we don't have a segment yet, then we don't need a new
3022 one (we build the last one after this loop). */
3023 new_segment = false;
3025 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
3027 /* If this section has a different relation between the
3028 virtual address and the load address, then we need a new
3029 segment. */
3030 new_segment = true;
3032 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3033 < BFD_ALIGN (hdr->lma, maxpagesize))
3035 /* If putting this section in this segment would force us to
3036 skip a page in the segment, then we need a new segment. */
3037 new_segment = true;
3039 else if ((last_hdr->flags & SEC_LOAD) == 0
3040 && (hdr->flags & SEC_LOAD) != 0)
3042 /* We don't want to put a loadable section after a
3043 nonloadable section in the same segment. */
3044 new_segment = true;
3046 else if ((abfd->flags & D_PAGED) == 0)
3048 /* If the file is not demand paged, which means that we
3049 don't require the sections to be correctly aligned in the
3050 file, then there is no other reason for a new segment. */
3051 new_segment = false;
3053 else if (! writable
3054 && (hdr->flags & SEC_READONLY) == 0
3055 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
3056 == hdr->lma))
3058 /* We don't want to put a writable section in a read only
3059 segment, unless they are on the same page in memory
3060 anyhow. We already know that the last section does not
3061 bring us past the current section on the page, so the
3062 only case in which the new section is not on the same
3063 page as the previous section is when the previous section
3064 ends precisely on a page boundary. */
3065 new_segment = true;
3067 else
3069 /* Otherwise, we can use the same segment. */
3070 new_segment = false;
3073 if (! new_segment)
3075 if ((hdr->flags & SEC_READONLY) == 0)
3076 writable = true;
3077 last_hdr = hdr;
3078 continue;
3081 /* We need a new program segment. We must create a new program
3082 header holding all the sections from phdr_index until hdr. */
3084 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3085 if (m == NULL)
3086 goto error_return;
3088 *pm = m;
3089 pm = &m->next;
3091 if ((hdr->flags & SEC_READONLY) == 0)
3092 writable = true;
3093 else
3094 writable = false;
3096 last_hdr = hdr;
3097 phdr_index = i;
3098 phdr_in_segment = false;
3101 /* Create a final PT_LOAD program segment. */
3102 if (last_hdr != NULL)
3104 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
3105 if (m == NULL)
3106 goto error_return;
3108 *pm = m;
3109 pm = &m->next;
3112 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
3113 if (dynsec != NULL)
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_DYNAMIC;
3121 m->count = 1;
3122 m->sections[0] = dynsec;
3124 *pm = m;
3125 pm = &m->next;
3128 /* For each loadable .note section, add a PT_NOTE segment. We don't
3129 use bfd_get_section_by_name, because if we link together
3130 nonloadable .note sections and loadable .note sections, we will
3131 generate two .note sections in the output file. FIXME: Using
3132 names for section types is bogus anyhow. */
3133 for (s = abfd->sections; s != NULL; s = s->next)
3135 if ((s->flags & SEC_LOAD) != 0
3136 && strncmp (s->name, ".note", 5) == 0)
3138 amt = sizeof (struct elf_segment_map);
3139 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3140 if (m == NULL)
3141 goto error_return;
3142 m->next = NULL;
3143 m->p_type = PT_NOTE;
3144 m->count = 1;
3145 m->sections[0] = s;
3147 *pm = m;
3148 pm = &m->next;
3152 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME
3153 segment. */
3154 eh_frame_hdr = NULL;
3155 if (elf_tdata (abfd)->eh_frame_hdr)
3156 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr");
3157 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD))
3159 amt = sizeof (struct elf_segment_map);
3160 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3161 if (m == NULL)
3162 goto error_return;
3163 m->next = NULL;
3164 m->p_type = PT_GNU_EH_FRAME;
3165 m->count = 1;
3166 m->sections[0] = eh_frame_hdr;
3168 *pm = m;
3169 pm = &m->next;
3172 free (sections);
3173 sections = NULL;
3175 elf_tdata (abfd)->segment_map = mfirst;
3176 return true;
3178 error_return:
3179 if (sections != NULL)
3180 free (sections);
3181 return false;
3184 /* Sort sections by address. */
3186 static int
3187 elf_sort_sections (arg1, arg2)
3188 const PTR arg1;
3189 const PTR arg2;
3191 const asection *sec1 = *(const asection **) arg1;
3192 const asection *sec2 = *(const asection **) arg2;
3194 /* Sort by LMA first, since this is the address used to
3195 place the section into a segment. */
3196 if (sec1->lma < sec2->lma)
3197 return -1;
3198 else if (sec1->lma > sec2->lma)
3199 return 1;
3201 /* Then sort by VMA. Normally the LMA and the VMA will be
3202 the same, and this will do nothing. */
3203 if (sec1->vma < sec2->vma)
3204 return -1;
3205 else if (sec1->vma > sec2->vma)
3206 return 1;
3208 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3210 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3212 if (TOEND (sec1))
3214 if (TOEND (sec2))
3216 /* If the indicies are the same, do not return 0
3217 here, but continue to try the next comparison. */
3218 if (sec1->target_index - sec2->target_index != 0)
3219 return sec1->target_index - sec2->target_index;
3221 else
3222 return 1;
3224 else if (TOEND (sec2))
3225 return -1;
3227 #undef TOEND
3229 /* Sort by size, to put zero sized sections
3230 before others at the same address. */
3232 if (sec1->_raw_size < sec2->_raw_size)
3233 return -1;
3234 if (sec1->_raw_size > sec2->_raw_size)
3235 return 1;
3237 return sec1->target_index - sec2->target_index;
3240 /* Assign file positions to the sections based on the mapping from
3241 sections to segments. This function also sets up some fields in
3242 the file header, and writes out the program headers. */
3244 static boolean
3245 assign_file_positions_for_segments (abfd)
3246 bfd *abfd;
3248 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3249 unsigned int count;
3250 struct elf_segment_map *m;
3251 unsigned int alloc;
3252 Elf_Internal_Phdr *phdrs;
3253 file_ptr off, voff;
3254 bfd_vma filehdr_vaddr, filehdr_paddr;
3255 bfd_vma phdrs_vaddr, phdrs_paddr;
3256 Elf_Internal_Phdr *p;
3257 bfd_size_type amt;
3259 if (elf_tdata (abfd)->segment_map == NULL)
3261 if (! map_sections_to_segments (abfd))
3262 return false;
3265 if (bed->elf_backend_modify_segment_map)
3267 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3268 return false;
3271 count = 0;
3272 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3273 ++count;
3275 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3276 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3277 elf_elfheader (abfd)->e_phnum = count;
3279 if (count == 0)
3280 return true;
3282 /* If we already counted the number of program segments, make sure
3283 that we allocated enough space. This happens when SIZEOF_HEADERS
3284 is used in a linker script. */
3285 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3286 if (alloc != 0 && count > alloc)
3288 ((*_bfd_error_handler)
3289 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3290 bfd_get_filename (abfd), alloc, count));
3291 bfd_set_error (bfd_error_bad_value);
3292 return false;
3295 if (alloc == 0)
3296 alloc = count;
3298 amt = alloc * sizeof (Elf_Internal_Phdr);
3299 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3300 if (phdrs == NULL)
3301 return false;
3303 off = bed->s->sizeof_ehdr;
3304 off += alloc * bed->s->sizeof_phdr;
3306 filehdr_vaddr = 0;
3307 filehdr_paddr = 0;
3308 phdrs_vaddr = 0;
3309 phdrs_paddr = 0;
3311 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3312 m != NULL;
3313 m = m->next, p++)
3315 unsigned int i;
3316 asection **secpp;
3318 /* If elf_segment_map is not from map_sections_to_segments, the
3319 sections may not be correctly ordered. NOTE: sorting should
3320 not be done to the PT_NOTE section of a corefile, which may
3321 contain several pseudo-sections artificially created by bfd.
3322 Sorting these pseudo-sections breaks things badly. */
3323 if (m->count > 1
3324 && !(elf_elfheader (abfd)->e_type == ET_CORE
3325 && m->p_type == PT_NOTE))
3326 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3327 elf_sort_sections);
3329 p->p_type = m->p_type;
3330 p->p_flags = m->p_flags;
3332 if (p->p_type == PT_LOAD
3333 && m->count > 0
3334 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3336 if ((abfd->flags & D_PAGED) != 0)
3337 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3338 else
3340 bfd_size_type align;
3342 align = 0;
3343 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3345 bfd_size_type secalign;
3347 secalign = bfd_get_section_alignment (abfd, *secpp);
3348 if (secalign > align)
3349 align = secalign;
3352 off += (m->sections[0]->vma - off) % (1 << align);
3356 if (m->count == 0)
3357 p->p_vaddr = 0;
3358 else
3359 p->p_vaddr = m->sections[0]->vma;
3361 if (m->p_paddr_valid)
3362 p->p_paddr = m->p_paddr;
3363 else if (m->count == 0)
3364 p->p_paddr = 0;
3365 else
3366 p->p_paddr = m->sections[0]->lma;
3368 if (p->p_type == PT_LOAD
3369 && (abfd->flags & D_PAGED) != 0)
3370 p->p_align = bed->maxpagesize;
3371 else if (m->count == 0)
3372 p->p_align = bed->s->file_align;
3373 else
3374 p->p_align = 0;
3376 p->p_offset = 0;
3377 p->p_filesz = 0;
3378 p->p_memsz = 0;
3380 if (m->includes_filehdr)
3382 if (! m->p_flags_valid)
3383 p->p_flags |= PF_R;
3384 p->p_offset = 0;
3385 p->p_filesz = bed->s->sizeof_ehdr;
3386 p->p_memsz = bed->s->sizeof_ehdr;
3387 if (m->count > 0)
3389 BFD_ASSERT (p->p_type == PT_LOAD);
3391 if (p->p_vaddr < (bfd_vma) off)
3393 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3394 bfd_get_filename (abfd));
3395 bfd_set_error (bfd_error_bad_value);
3396 return false;
3399 p->p_vaddr -= off;
3400 if (! m->p_paddr_valid)
3401 p->p_paddr -= off;
3403 if (p->p_type == PT_LOAD)
3405 filehdr_vaddr = p->p_vaddr;
3406 filehdr_paddr = p->p_paddr;
3410 if (m->includes_phdrs)
3412 if (! m->p_flags_valid)
3413 p->p_flags |= PF_R;
3415 if (m->includes_filehdr)
3417 if (p->p_type == PT_LOAD)
3419 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3420 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3423 else
3425 p->p_offset = bed->s->sizeof_ehdr;
3427 if (m->count > 0)
3429 BFD_ASSERT (p->p_type == PT_LOAD);
3430 p->p_vaddr -= off - p->p_offset;
3431 if (! m->p_paddr_valid)
3432 p->p_paddr -= off - p->p_offset;
3435 if (p->p_type == PT_LOAD)
3437 phdrs_vaddr = p->p_vaddr;
3438 phdrs_paddr = p->p_paddr;
3440 else
3441 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3444 p->p_filesz += alloc * bed->s->sizeof_phdr;
3445 p->p_memsz += alloc * bed->s->sizeof_phdr;
3448 if (p->p_type == PT_LOAD
3449 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3451 if (! m->includes_filehdr && ! m->includes_phdrs)
3452 p->p_offset = off;
3453 else
3455 file_ptr adjust;
3457 adjust = off - (p->p_offset + p->p_filesz);
3458 p->p_filesz += adjust;
3459 p->p_memsz += adjust;
3463 voff = off;
3465 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3467 asection *sec;
3468 flagword flags;
3469 bfd_size_type align;
3471 sec = *secpp;
3472 flags = sec->flags;
3473 align = 1 << bfd_get_section_alignment (abfd, sec);
3475 /* The section may have artificial alignment forced by a
3476 link script. Notice this case by the gap between the
3477 cumulative phdr lma and the section's lma. */
3478 if (p->p_paddr + p->p_memsz < sec->lma)
3480 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3482 p->p_memsz += adjust;
3483 off += adjust;
3484 voff += adjust;
3485 if ((flags & SEC_LOAD) != 0)
3486 p->p_filesz += adjust;
3489 if (p->p_type == PT_LOAD)
3491 bfd_signed_vma adjust;
3493 if ((flags & SEC_LOAD) != 0)
3495 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3496 if (adjust < 0)
3497 adjust = 0;
3499 else if ((flags & SEC_ALLOC) != 0)
3501 /* The section VMA must equal the file position
3502 modulo the page size. FIXME: I'm not sure if
3503 this adjustment is really necessary. We used to
3504 not have the SEC_LOAD case just above, and then
3505 this was necessary, but now I'm not sure. */
3506 if ((abfd->flags & D_PAGED) != 0)
3507 adjust = (sec->vma - voff) % bed->maxpagesize;
3508 else
3509 adjust = (sec->vma - voff) % align;
3511 else
3512 adjust = 0;
3514 if (adjust != 0)
3516 if (i == 0)
3518 (* _bfd_error_handler) (_("\
3519 Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"),
3520 bfd_section_name (abfd, sec),
3521 sec->lma,
3522 p->p_paddr);
3523 return false;
3525 p->p_memsz += adjust;
3526 off += adjust;
3527 voff += adjust;
3528 if ((flags & SEC_LOAD) != 0)
3529 p->p_filesz += adjust;
3532 sec->filepos = off;
3534 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3535 used in a linker script we may have a section with
3536 SEC_LOAD clear but which is supposed to have
3537 contents. */
3538 if ((flags & SEC_LOAD) != 0
3539 || (flags & SEC_HAS_CONTENTS) != 0)
3540 off += sec->_raw_size;
3542 if ((flags & SEC_ALLOC) != 0)
3543 voff += sec->_raw_size;
3546 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3548 /* The actual "note" segment has i == 0.
3549 This is the one that actually contains everything. */
3550 if (i == 0)
3552 sec->filepos = off;
3553 p->p_filesz = sec->_raw_size;
3554 off += sec->_raw_size;
3555 voff = off;
3557 else
3559 /* Fake sections -- don't need to be written. */
3560 sec->filepos = 0;
3561 sec->_raw_size = 0;
3562 flags = sec->flags = 0;
3564 p->p_memsz = 0;
3565 p->p_align = 1;
3567 else
3569 p->p_memsz += sec->_raw_size;
3571 if ((flags & SEC_LOAD) != 0)
3572 p->p_filesz += sec->_raw_size;
3574 if (align > p->p_align
3575 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3576 p->p_align = align;
3579 if (! m->p_flags_valid)
3581 p->p_flags |= PF_R;
3582 if ((flags & SEC_CODE) != 0)
3583 p->p_flags |= PF_X;
3584 if ((flags & SEC_READONLY) == 0)
3585 p->p_flags |= PF_W;
3590 /* Now that we have set the section file positions, we can set up
3591 the file positions for the non PT_LOAD segments. */
3592 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3593 m != NULL;
3594 m = m->next, p++)
3596 if (p->p_type != PT_LOAD && m->count > 0)
3598 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3599 p->p_offset = m->sections[0]->filepos;
3601 if (m->count == 0)
3603 if (m->includes_filehdr)
3605 p->p_vaddr = filehdr_vaddr;
3606 if (! m->p_paddr_valid)
3607 p->p_paddr = filehdr_paddr;
3609 else if (m->includes_phdrs)
3611 p->p_vaddr = phdrs_vaddr;
3612 if (! m->p_paddr_valid)
3613 p->p_paddr = phdrs_paddr;
3618 /* Clear out any program headers we allocated but did not use. */
3619 for (; count < alloc; count++, p++)
3621 memset (p, 0, sizeof *p);
3622 p->p_type = PT_NULL;
3625 elf_tdata (abfd)->phdr = phdrs;
3627 elf_tdata (abfd)->next_file_pos = off;
3629 /* Write out the program headers. */
3630 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
3631 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3632 return false;
3634 return true;
3637 /* Get the size of the program header.
3639 If this is called by the linker before any of the section VMA's are set, it
3640 can't calculate the correct value for a strange memory layout. This only
3641 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3642 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3643 data segment (exclusive of .interp and .dynamic).
3645 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3646 will be two segments. */
3648 static bfd_size_type
3649 get_program_header_size (abfd)
3650 bfd *abfd;
3652 size_t segs;
3653 asection *s;
3654 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3656 /* We can't return a different result each time we're called. */
3657 if (elf_tdata (abfd)->program_header_size != 0)
3658 return elf_tdata (abfd)->program_header_size;
3660 if (elf_tdata (abfd)->segment_map != NULL)
3662 struct elf_segment_map *m;
3664 segs = 0;
3665 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3666 ++segs;
3667 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3668 return elf_tdata (abfd)->program_header_size;
3671 /* Assume we will need exactly two PT_LOAD segments: one for text
3672 and one for data. */
3673 segs = 2;
3675 s = bfd_get_section_by_name (abfd, ".interp");
3676 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3678 /* If we have a loadable interpreter section, we need a
3679 PT_INTERP segment. In this case, assume we also need a
3680 PT_PHDR segment, although that may not be true for all
3681 targets. */
3682 segs += 2;
3685 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3687 /* We need a PT_DYNAMIC segment. */
3688 ++segs;
3691 if (elf_tdata (abfd)->eh_frame_hdr
3692 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL)
3694 /* We need a PT_GNU_EH_FRAME segment. */
3695 ++segs;
3698 for (s = abfd->sections; s != NULL; s = s->next)
3700 if ((s->flags & SEC_LOAD) != 0
3701 && strncmp (s->name, ".note", 5) == 0)
3703 /* We need a PT_NOTE segment. */
3704 ++segs;
3708 /* Let the backend count up any program headers it might need. */
3709 if (bed->elf_backend_additional_program_headers)
3711 int a;
3713 a = (*bed->elf_backend_additional_program_headers) (abfd);
3714 if (a == -1)
3715 abort ();
3716 segs += a;
3719 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3720 return elf_tdata (abfd)->program_header_size;
3723 /* Work out the file positions of all the sections. This is called by
3724 _bfd_elf_compute_section_file_positions. All the section sizes and
3725 VMAs must be known before this is called.
3727 We do not consider reloc sections at this point, unless they form
3728 part of the loadable image. Reloc sections are assigned file
3729 positions in assign_file_positions_for_relocs, which is called by
3730 write_object_contents and final_link.
3732 We also don't set the positions of the .symtab and .strtab here. */
3734 static boolean
3735 assign_file_positions_except_relocs (abfd)
3736 bfd *abfd;
3738 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3739 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3740 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3741 unsigned int num_sec = elf_numsections (abfd);
3742 file_ptr off;
3743 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3745 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3746 && bfd_get_format (abfd) != bfd_core)
3748 Elf_Internal_Shdr **hdrpp;
3749 unsigned int i;
3751 /* Start after the ELF header. */
3752 off = i_ehdrp->e_ehsize;
3754 /* We are not creating an executable, which means that we are
3755 not creating a program header, and that the actual order of
3756 the sections in the file is unimportant. */
3757 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3759 Elf_Internal_Shdr *hdr;
3761 hdr = *hdrpp;
3762 if (hdr->sh_type == SHT_REL
3763 || hdr->sh_type == SHT_RELA
3764 || i == tdata->symtab_section
3765 || i == tdata->symtab_shndx_section
3766 || i == tdata->strtab_section)
3768 hdr->sh_offset = -1;
3770 else
3771 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3773 if (i == SHN_LORESERVE - 1)
3775 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3776 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3780 else
3782 unsigned int i;
3783 Elf_Internal_Shdr **hdrpp;
3785 /* Assign file positions for the loaded sections based on the
3786 assignment of sections to segments. */
3787 if (! assign_file_positions_for_segments (abfd))
3788 return false;
3790 /* Assign file positions for the other sections. */
3792 off = elf_tdata (abfd)->next_file_pos;
3793 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++)
3795 Elf_Internal_Shdr *hdr;
3797 hdr = *hdrpp;
3798 if (hdr->bfd_section != NULL
3799 && hdr->bfd_section->filepos != 0)
3800 hdr->sh_offset = hdr->bfd_section->filepos;
3801 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3803 ((*_bfd_error_handler)
3804 (_("%s: warning: allocated section `%s' not in segment"),
3805 bfd_get_filename (abfd),
3806 (hdr->bfd_section == NULL
3807 ? "*unknown*"
3808 : hdr->bfd_section->name)));
3809 if ((abfd->flags & D_PAGED) != 0)
3810 off += (hdr->sh_addr - off) % bed->maxpagesize;
3811 else
3812 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3813 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3814 false);
3816 else if (hdr->sh_type == SHT_REL
3817 || hdr->sh_type == SHT_RELA
3818 || hdr == i_shdrpp[tdata->symtab_section]
3819 || hdr == i_shdrpp[tdata->symtab_shndx_section]
3820 || hdr == i_shdrpp[tdata->strtab_section])
3821 hdr->sh_offset = -1;
3822 else
3823 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3825 if (i == SHN_LORESERVE - 1)
3827 i += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3828 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE;
3833 /* Place the section headers. */
3834 off = align_file_position (off, bed->s->file_align);
3835 i_ehdrp->e_shoff = off;
3836 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3838 elf_tdata (abfd)->next_file_pos = off;
3840 return true;
3843 static boolean
3844 prep_headers (abfd)
3845 bfd *abfd;
3847 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3848 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3849 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3850 int count;
3851 struct elf_strtab_hash *shstrtab;
3852 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3854 i_ehdrp = elf_elfheader (abfd);
3855 i_shdrp = elf_elfsections (abfd);
3857 shstrtab = _bfd_elf_strtab_init ();
3858 if (shstrtab == NULL)
3859 return false;
3861 elf_shstrtab (abfd) = shstrtab;
3863 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3864 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3865 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3866 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3868 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3869 i_ehdrp->e_ident[EI_DATA] =
3870 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3871 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3873 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3874 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3876 for (count = EI_PAD; count < EI_NIDENT; count++)
3877 i_ehdrp->e_ident[count] = 0;
3879 if ((abfd->flags & DYNAMIC) != 0)
3880 i_ehdrp->e_type = ET_DYN;
3881 else if ((abfd->flags & EXEC_P) != 0)
3882 i_ehdrp->e_type = ET_EXEC;
3883 else if (bfd_get_format (abfd) == bfd_core)
3884 i_ehdrp->e_type = ET_CORE;
3885 else
3886 i_ehdrp->e_type = ET_REL;
3888 switch (bfd_get_arch (abfd))
3890 case bfd_arch_unknown:
3891 i_ehdrp->e_machine = EM_NONE;
3892 break;
3894 /* There used to be a long list of cases here, each one setting
3895 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3896 in the corresponding bfd definition. To avoid duplication,
3897 the switch was removed. Machines that need special handling
3898 can generally do it in elf_backend_final_write_processing(),
3899 unless they need the information earlier than the final write.
3900 Such need can generally be supplied by replacing the tests for
3901 e_machine with the conditions used to determine it. */
3902 default:
3903 if (get_elf_backend_data (abfd) != NULL)
3904 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
3905 else
3906 i_ehdrp->e_machine = EM_NONE;
3909 i_ehdrp->e_version = bed->s->ev_current;
3910 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3912 /* No program header, for now. */
3913 i_ehdrp->e_phoff = 0;
3914 i_ehdrp->e_phentsize = 0;
3915 i_ehdrp->e_phnum = 0;
3917 /* Each bfd section is section header entry. */
3918 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3919 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3921 /* If we're building an executable, we'll need a program header table. */
3922 if (abfd->flags & EXEC_P)
3924 /* It all happens later. */
3925 #if 0
3926 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3928 /* elf_build_phdrs() returns a (NULL-terminated) array of
3929 Elf_Internal_Phdrs. */
3930 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3931 i_ehdrp->e_phoff = outbase;
3932 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3933 #endif
3935 else
3937 i_ehdrp->e_phentsize = 0;
3938 i_phdrp = 0;
3939 i_ehdrp->e_phoff = 0;
3942 elf_tdata (abfd)->symtab_hdr.sh_name =
3943 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
3944 elf_tdata (abfd)->strtab_hdr.sh_name =
3945 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
3946 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3947 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
3948 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3949 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3950 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3951 return false;
3953 return true;
3956 /* Assign file positions for all the reloc sections which are not part
3957 of the loadable file image. */
3959 void
3960 _bfd_elf_assign_file_positions_for_relocs (abfd)
3961 bfd *abfd;
3963 file_ptr off;
3964 unsigned int i, num_sec;
3965 Elf_Internal_Shdr **shdrpp;
3967 off = elf_tdata (abfd)->next_file_pos;
3969 num_sec = elf_numsections (abfd);
3970 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++)
3972 Elf_Internal_Shdr *shdrp;
3974 shdrp = *shdrpp;
3975 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3976 && shdrp->sh_offset == -1)
3977 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3980 elf_tdata (abfd)->next_file_pos = off;
3983 boolean
3984 _bfd_elf_write_object_contents (abfd)
3985 bfd *abfd;
3987 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3988 Elf_Internal_Ehdr *i_ehdrp;
3989 Elf_Internal_Shdr **i_shdrp;
3990 boolean failed;
3991 unsigned int count, num_sec;
3993 if (! abfd->output_has_begun
3994 && ! _bfd_elf_compute_section_file_positions
3995 (abfd, (struct bfd_link_info *) NULL))
3996 return false;
3998 i_shdrp = elf_elfsections (abfd);
3999 i_ehdrp = elf_elfheader (abfd);
4001 failed = false;
4002 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
4003 if (failed)
4004 return false;
4006 _bfd_elf_assign_file_positions_for_relocs (abfd);
4008 /* After writing the headers, we need to write the sections too... */
4009 num_sec = elf_numsections (abfd);
4010 for (count = 1; count < num_sec; count++)
4012 if (bed->elf_backend_section_processing)
4013 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
4014 if (i_shdrp[count]->contents)
4016 bfd_size_type amt = i_shdrp[count]->sh_size;
4018 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
4019 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
4020 return false;
4022 if (count == SHN_LORESERVE - 1)
4023 count += SHN_HIRESERVE + 1 - SHN_LORESERVE;
4026 /* Write out the section header names. */
4027 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
4028 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
4029 return false;
4031 if (bed->elf_backend_final_write_processing)
4032 (*bed->elf_backend_final_write_processing) (abfd,
4033 elf_tdata (abfd)->linker);
4035 return bed->s->write_shdrs_and_ehdr (abfd);
4038 boolean
4039 _bfd_elf_write_corefile_contents (abfd)
4040 bfd *abfd;
4042 /* Hopefully this can be done just like an object file. */
4043 return _bfd_elf_write_object_contents (abfd);
4046 /* Given a section, search the header to find them. */
4049 _bfd_elf_section_from_bfd_section (abfd, asect)
4050 bfd *abfd;
4051 struct sec *asect;
4053 struct elf_backend_data *bed;
4054 int index;
4056 if (elf_section_data (asect) != NULL
4057 && elf_section_data (asect)->this_idx != 0)
4058 return elf_section_data (asect)->this_idx;
4060 if (bfd_is_abs_section (asect))
4061 index = SHN_ABS;
4062 else if (bfd_is_com_section (asect))
4063 index = SHN_COMMON;
4064 else if (bfd_is_und_section (asect))
4065 index = SHN_UNDEF;
4066 else
4068 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
4069 int maxindex = elf_numsections (abfd);
4071 for (index = 1; index < maxindex; index++)
4073 Elf_Internal_Shdr *hdr = i_shdrp[index];
4075 if (hdr != NULL && hdr->bfd_section == asect)
4076 return index;
4078 index = -1;
4081 bed = get_elf_backend_data (abfd);
4082 if (bed->elf_backend_section_from_bfd_section)
4084 int retval = index;
4086 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval))
4087 return retval;
4090 if (index == -1)
4091 bfd_set_error (bfd_error_nonrepresentable_section);
4093 return index;
4096 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
4097 on error. */
4100 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
4101 bfd *abfd;
4102 asymbol **asym_ptr_ptr;
4104 asymbol *asym_ptr = *asym_ptr_ptr;
4105 int idx;
4106 flagword flags = asym_ptr->flags;
4108 /* When gas creates relocations against local labels, it creates its
4109 own symbol for the section, but does put the symbol into the
4110 symbol chain, so udata is 0. When the linker is generating
4111 relocatable output, this section symbol may be for one of the
4112 input sections rather than the output section. */
4113 if (asym_ptr->udata.i == 0
4114 && (flags & BSF_SECTION_SYM)
4115 && asym_ptr->section)
4117 int indx;
4119 if (asym_ptr->section->output_section != NULL)
4120 indx = asym_ptr->section->output_section->index;
4121 else
4122 indx = asym_ptr->section->index;
4123 if (indx < elf_num_section_syms (abfd)
4124 && elf_section_syms (abfd)[indx] != NULL)
4125 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
4128 idx = asym_ptr->udata.i;
4130 if (idx == 0)
4132 /* This case can occur when using --strip-symbol on a symbol
4133 which is used in a relocation entry. */
4134 (*_bfd_error_handler)
4135 (_("%s: symbol `%s' required but not present"),
4136 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
4137 bfd_set_error (bfd_error_no_symbols);
4138 return -1;
4141 #if DEBUG & 4
4143 fprintf (stderr,
4144 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
4145 (long) asym_ptr, asym_ptr->name, idx, flags,
4146 elf_symbol_flags (flags));
4147 fflush (stderr);
4149 #endif
4151 return idx;
4154 /* Copy private BFD data. This copies any program header information. */
4156 static boolean
4157 copy_private_bfd_data (ibfd, obfd)
4158 bfd *ibfd;
4159 bfd *obfd;
4161 Elf_Internal_Ehdr * iehdr;
4162 struct elf_segment_map * map;
4163 struct elf_segment_map * map_first;
4164 struct elf_segment_map ** pointer_to_map;
4165 Elf_Internal_Phdr * segment;
4166 asection * section;
4167 unsigned int i;
4168 unsigned int num_segments;
4169 boolean phdr_included = false;
4170 bfd_vma maxpagesize;
4171 struct elf_segment_map * phdr_adjust_seg = NULL;
4172 unsigned int phdr_adjust_num = 0;
4174 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4175 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4176 return true;
4178 if (elf_tdata (ibfd)->phdr == NULL)
4179 return true;
4181 iehdr = elf_elfheader (ibfd);
4183 map_first = NULL;
4184 pointer_to_map = &map_first;
4186 num_segments = elf_elfheader (ibfd)->e_phnum;
4187 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4189 /* Returns the end address of the segment + 1. */
4190 #define SEGMENT_END(segment, start) \
4191 (start + (segment->p_memsz > segment->p_filesz \
4192 ? segment->p_memsz : segment->p_filesz))
4194 /* Returns true if the given section is contained within
4195 the given segment. VMA addresses are compared. */
4196 #define IS_CONTAINED_BY_VMA(section, segment) \
4197 (section->vma >= segment->p_vaddr \
4198 && (section->vma + section->_raw_size) \
4199 <= (SEGMENT_END (segment, segment->p_vaddr)))
4201 /* Returns true if the given section is contained within
4202 the given segment. LMA addresses are compared. */
4203 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4204 (section->lma >= base \
4205 && (section->lma + section->_raw_size) \
4206 <= SEGMENT_END (segment, base))
4208 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4209 #define IS_COREFILE_NOTE(p, s) \
4210 (p->p_type == PT_NOTE \
4211 && bfd_get_format (ibfd) == bfd_core \
4212 && s->vma == 0 && s->lma == 0 \
4213 && (bfd_vma) s->filepos >= p->p_offset \
4214 && (bfd_vma) s->filepos + s->_raw_size \
4215 <= p->p_offset + p->p_filesz)
4217 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4218 linker, which generates a PT_INTERP section with p_vaddr and
4219 p_memsz set to 0. */
4220 #define IS_SOLARIS_PT_INTERP(p, s) \
4221 ( p->p_vaddr == 0 \
4222 && p->p_filesz > 0 \
4223 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4224 && s->_raw_size > 0 \
4225 && (bfd_vma) s->filepos >= p->p_offset \
4226 && ((bfd_vma) s->filepos + s->_raw_size \
4227 <= p->p_offset + p->p_filesz))
4229 /* Decide if the given section should be included in the given segment.
4230 A section will be included if:
4231 1. It is within the address space of the segment -- we use the LMA
4232 if that is set for the segment and the VMA otherwise,
4233 2. It is an allocated segment,
4234 3. There is an output section associated with it,
4235 4. The section has not already been allocated to a previous segment. */
4236 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4237 (((((segment->p_paddr \
4238 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4239 : IS_CONTAINED_BY_VMA (section, segment)) \
4240 || IS_SOLARIS_PT_INTERP (segment, section)) \
4241 && (section->flags & SEC_ALLOC) != 0) \
4242 || IS_COREFILE_NOTE (segment, section)) \
4243 && section->output_section != NULL \
4244 && section->segment_mark == false)
4246 /* Returns true iff seg1 starts after the end of seg2. */
4247 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4248 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4250 /* Returns true iff seg1 and seg2 overlap. */
4251 #define SEGMENT_OVERLAPS(seg1, seg2) \
4252 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4254 /* Initialise the segment mark field. */
4255 for (section = ibfd->sections; section != NULL; section = section->next)
4256 section->segment_mark = false;
4258 /* Scan through the segments specified in the program header
4259 of the input BFD. For this first scan we look for overlaps
4260 in the loadable segments. These can be created by weird
4261 parameters to objcopy. */
4262 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4263 i < num_segments;
4264 i++, segment++)
4266 unsigned int j;
4267 Elf_Internal_Phdr *segment2;
4269 if (segment->p_type != PT_LOAD)
4270 continue;
4272 /* Determine if this segment overlaps any previous segments. */
4273 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4275 bfd_signed_vma extra_length;
4277 if (segment2->p_type != PT_LOAD
4278 || ! SEGMENT_OVERLAPS (segment, segment2))
4279 continue;
4281 /* Merge the two segments together. */
4282 if (segment2->p_vaddr < segment->p_vaddr)
4284 /* Extend SEGMENT2 to include SEGMENT and then delete
4285 SEGMENT. */
4286 extra_length =
4287 SEGMENT_END (segment, segment->p_vaddr)
4288 - SEGMENT_END (segment2, segment2->p_vaddr);
4290 if (extra_length > 0)
4292 segment2->p_memsz += extra_length;
4293 segment2->p_filesz += extra_length;
4296 segment->p_type = PT_NULL;
4298 /* Since we have deleted P we must restart the outer loop. */
4299 i = 0;
4300 segment = elf_tdata (ibfd)->phdr;
4301 break;
4303 else
4305 /* Extend SEGMENT to include SEGMENT2 and then delete
4306 SEGMENT2. */
4307 extra_length =
4308 SEGMENT_END (segment2, segment2->p_vaddr)
4309 - SEGMENT_END (segment, segment->p_vaddr);
4311 if (extra_length > 0)
4313 segment->p_memsz += extra_length;
4314 segment->p_filesz += extra_length;
4317 segment2->p_type = PT_NULL;
4322 /* The second scan attempts to assign sections to segments. */
4323 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4324 i < num_segments;
4325 i ++, segment ++)
4327 unsigned int section_count;
4328 asection ** sections;
4329 asection * output_section;
4330 unsigned int isec;
4331 bfd_vma matching_lma;
4332 bfd_vma suggested_lma;
4333 unsigned int j;
4334 bfd_size_type amt;
4336 if (segment->p_type == PT_NULL)
4337 continue;
4339 /* Compute how many sections might be placed into this segment. */
4340 section_count = 0;
4341 for (section = ibfd->sections; section != NULL; section = section->next)
4342 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4343 ++section_count;
4345 /* Allocate a segment map big enough to contain all of the
4346 sections we have selected. */
4347 amt = sizeof (struct elf_segment_map);
4348 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4349 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4350 if (map == NULL)
4351 return false;
4353 /* Initialise the fields of the segment map. Default to
4354 using the physical address of the segment in the input BFD. */
4355 map->next = NULL;
4356 map->p_type = segment->p_type;
4357 map->p_flags = segment->p_flags;
4358 map->p_flags_valid = 1;
4359 map->p_paddr = segment->p_paddr;
4360 map->p_paddr_valid = 1;
4362 /* Determine if this segment contains the ELF file header
4363 and if it contains the program headers themselves. */
4364 map->includes_filehdr = (segment->p_offset == 0
4365 && segment->p_filesz >= iehdr->e_ehsize);
4367 map->includes_phdrs = 0;
4369 if (! phdr_included || segment->p_type != PT_LOAD)
4371 map->includes_phdrs =
4372 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4373 && (segment->p_offset + segment->p_filesz
4374 >= ((bfd_vma) iehdr->e_phoff
4375 + iehdr->e_phnum * iehdr->e_phentsize)));
4377 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4378 phdr_included = true;
4381 if (section_count == 0)
4383 /* Special segments, such as the PT_PHDR segment, may contain
4384 no sections, but ordinary, loadable segments should contain
4385 something. */
4386 if (segment->p_type == PT_LOAD)
4387 _bfd_error_handler
4388 (_("%s: warning: Empty loadable segment detected\n"),
4389 bfd_archive_filename (ibfd));
4391 map->count = 0;
4392 *pointer_to_map = map;
4393 pointer_to_map = &map->next;
4395 continue;
4398 /* Now scan the sections in the input BFD again and attempt
4399 to add their corresponding output sections to the segment map.
4400 The problem here is how to handle an output section which has
4401 been moved (ie had its LMA changed). There are four possibilities:
4403 1. None of the sections have been moved.
4404 In this case we can continue to use the segment LMA from the
4405 input BFD.
4407 2. All of the sections have been moved by the same amount.
4408 In this case we can change the segment's LMA to match the LMA
4409 of the first section.
4411 3. Some of the sections have been moved, others have not.
4412 In this case those sections which have not been moved can be
4413 placed in the current segment which will have to have its size,
4414 and possibly its LMA changed, and a new segment or segments will
4415 have to be created to contain the other sections.
4417 4. The sections have been moved, but not be the same amount.
4418 In this case we can change the segment's LMA to match the LMA
4419 of the first section and we will have to create a new segment
4420 or segments to contain the other sections.
4422 In order to save time, we allocate an array to hold the section
4423 pointers that we are interested in. As these sections get assigned
4424 to a segment, they are removed from this array. */
4426 amt = (bfd_size_type) section_count * sizeof (asection *);
4427 sections = (asection **) bfd_malloc (amt);
4428 if (sections == NULL)
4429 return false;
4431 /* Step One: Scan for segment vs section LMA conflicts.
4432 Also add the sections to the section array allocated above.
4433 Also add the sections to the current segment. In the common
4434 case, where the sections have not been moved, this means that
4435 we have completely filled the segment, and there is nothing
4436 more to do. */
4437 isec = 0;
4438 matching_lma = 0;
4439 suggested_lma = 0;
4441 for (j = 0, section = ibfd->sections;
4442 section != NULL;
4443 section = section->next)
4445 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4447 output_section = section->output_section;
4449 sections[j ++] = section;
4451 /* The Solaris native linker always sets p_paddr to 0.
4452 We try to catch that case here, and set it to the
4453 correct value. */
4454 if (segment->p_paddr == 0
4455 && segment->p_vaddr != 0
4456 && isec == 0
4457 && output_section->lma != 0
4458 && (output_section->vma == (segment->p_vaddr
4459 + (map->includes_filehdr
4460 ? iehdr->e_ehsize
4461 : 0)
4462 + (map->includes_phdrs
4463 ? (iehdr->e_phnum
4464 * iehdr->e_phentsize)
4465 : 0))))
4466 map->p_paddr = segment->p_vaddr;
4468 /* Match up the physical address of the segment with the
4469 LMA address of the output section. */
4470 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4471 || IS_COREFILE_NOTE (segment, section))
4473 if (matching_lma == 0)
4474 matching_lma = output_section->lma;
4476 /* We assume that if the section fits within the segment
4477 then it does not overlap any other section within that
4478 segment. */
4479 map->sections[isec ++] = output_section;
4481 else if (suggested_lma == 0)
4482 suggested_lma = output_section->lma;
4486 BFD_ASSERT (j == section_count);
4488 /* Step Two: Adjust the physical address of the current segment,
4489 if necessary. */
4490 if (isec == section_count)
4492 /* All of the sections fitted within the segment as currently
4493 specified. This is the default case. Add the segment to
4494 the list of built segments and carry on to process the next
4495 program header in the input BFD. */
4496 map->count = section_count;
4497 *pointer_to_map = map;
4498 pointer_to_map = &map->next;
4500 free (sections);
4501 continue;
4503 else
4505 if (matching_lma != 0)
4507 /* At least one section fits inside the current segment.
4508 Keep it, but modify its physical address to match the
4509 LMA of the first section that fitted. */
4510 map->p_paddr = matching_lma;
4512 else
4514 /* None of the sections fitted inside the current segment.
4515 Change the current segment's physical address to match
4516 the LMA of the first section. */
4517 map->p_paddr = suggested_lma;
4520 /* Offset the segment physical address from the lma
4521 to allow for space taken up by elf headers. */
4522 if (map->includes_filehdr)
4523 map->p_paddr -= iehdr->e_ehsize;
4525 if (map->includes_phdrs)
4527 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4529 /* iehdr->e_phnum is just an estimate of the number
4530 of program headers that we will need. Make a note
4531 here of the number we used and the segment we chose
4532 to hold these headers, so that we can adjust the
4533 offset when we know the correct value. */
4534 phdr_adjust_num = iehdr->e_phnum;
4535 phdr_adjust_seg = map;
4539 /* Step Three: Loop over the sections again, this time assigning
4540 those that fit to the current segment and remvoing them from the
4541 sections array; but making sure not to leave large gaps. Once all
4542 possible sections have been assigned to the current segment it is
4543 added to the list of built segments and if sections still remain
4544 to be assigned, a new segment is constructed before repeating
4545 the loop. */
4546 isec = 0;
4549 map->count = 0;
4550 suggested_lma = 0;
4552 /* Fill the current segment with sections that fit. */
4553 for (j = 0; j < section_count; j++)
4555 section = sections[j];
4557 if (section == NULL)
4558 continue;
4560 output_section = section->output_section;
4562 BFD_ASSERT (output_section != NULL);
4564 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4565 || IS_COREFILE_NOTE (segment, section))
4567 if (map->count == 0)
4569 /* If the first section in a segment does not start at
4570 the beginning of the segment, then something is
4571 wrong. */
4572 if (output_section->lma !=
4573 (map->p_paddr
4574 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4575 + (map->includes_phdrs
4576 ? iehdr->e_phnum * iehdr->e_phentsize
4577 : 0)))
4578 abort ();
4580 else
4582 asection * prev_sec;
4584 prev_sec = map->sections[map->count - 1];
4586 /* If the gap between the end of the previous section
4587 and the start of this section is more than
4588 maxpagesize then we need to start a new segment. */
4589 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4590 maxpagesize)
4591 < BFD_ALIGN (output_section->lma, maxpagesize))
4592 || ((prev_sec->lma + prev_sec->_raw_size)
4593 > output_section->lma))
4595 if (suggested_lma == 0)
4596 suggested_lma = output_section->lma;
4598 continue;
4602 map->sections[map->count++] = output_section;
4603 ++isec;
4604 sections[j] = NULL;
4605 section->segment_mark = true;
4607 else if (suggested_lma == 0)
4608 suggested_lma = output_section->lma;
4611 BFD_ASSERT (map->count > 0);
4613 /* Add the current segment to the list of built segments. */
4614 *pointer_to_map = map;
4615 pointer_to_map = &map->next;
4617 if (isec < section_count)
4619 /* We still have not allocated all of the sections to
4620 segments. Create a new segment here, initialise it
4621 and carry on looping. */
4622 amt = sizeof (struct elf_segment_map);
4623 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4624 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4625 if (map == NULL)
4626 return false;
4628 /* Initialise the fields of the segment map. Set the physical
4629 physical address to the LMA of the first section that has
4630 not yet been assigned. */
4631 map->next = NULL;
4632 map->p_type = segment->p_type;
4633 map->p_flags = segment->p_flags;
4634 map->p_flags_valid = 1;
4635 map->p_paddr = suggested_lma;
4636 map->p_paddr_valid = 1;
4637 map->includes_filehdr = 0;
4638 map->includes_phdrs = 0;
4641 while (isec < section_count);
4643 free (sections);
4646 /* The Solaris linker creates program headers in which all the
4647 p_paddr fields are zero. When we try to objcopy or strip such a
4648 file, we get confused. Check for this case, and if we find it
4649 reset the p_paddr_valid fields. */
4650 for (map = map_first; map != NULL; map = map->next)
4651 if (map->p_paddr != 0)
4652 break;
4653 if (map == NULL)
4655 for (map = map_first; map != NULL; map = map->next)
4656 map->p_paddr_valid = 0;
4659 elf_tdata (obfd)->segment_map = map_first;
4661 /* If we had to estimate the number of program headers that were
4662 going to be needed, then check our estimate now and adjust
4663 the offset if necessary. */
4664 if (phdr_adjust_seg != NULL)
4666 unsigned int count;
4668 for (count = 0, map = map_first; map != NULL; map = map->next)
4669 count++;
4671 if (count > phdr_adjust_num)
4672 phdr_adjust_seg->p_paddr
4673 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4676 #if 0
4677 /* Final Step: Sort the segments into ascending order of physical
4678 address. */
4679 if (map_first != NULL)
4681 struct elf_segment_map *prev;
4683 prev = map_first;
4684 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4686 /* Yes I know - its a bubble sort.... */
4687 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4689 /* Swap map and map->next. */
4690 prev->next = map->next;
4691 map->next = map->next->next;
4692 prev->next->next = map;
4694 /* Restart loop. */
4695 map = map_first;
4699 #endif
4701 #undef SEGMENT_END
4702 #undef IS_CONTAINED_BY_VMA
4703 #undef IS_CONTAINED_BY_LMA
4704 #undef IS_COREFILE_NOTE
4705 #undef IS_SOLARIS_PT_INTERP
4706 #undef INCLUDE_SECTION_IN_SEGMENT
4707 #undef SEGMENT_AFTER_SEGMENT
4708 #undef SEGMENT_OVERLAPS
4709 return true;
4712 /* Copy private section information. This copies over the entsize
4713 field, and sometimes the info field. */
4715 boolean
4716 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4717 bfd *ibfd;
4718 asection *isec;
4719 bfd *obfd;
4720 asection *osec;
4722 Elf_Internal_Shdr *ihdr, *ohdr;
4724 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4725 || obfd->xvec->flavour != bfd_target_elf_flavour)
4726 return true;
4728 /* Copy over private BFD data if it has not already been copied.
4729 This must be done here, rather than in the copy_private_bfd_data
4730 entry point, because the latter is called after the section
4731 contents have been set, which means that the program headers have
4732 already been worked out. */
4733 if (elf_tdata (obfd)->segment_map == NULL
4734 && elf_tdata (ibfd)->phdr != NULL)
4736 asection *s;
4738 /* Only set up the segments if there are no more SEC_ALLOC
4739 sections. FIXME: This won't do the right thing if objcopy is
4740 used to remove the last SEC_ALLOC section, since objcopy
4741 won't call this routine in that case. */
4742 for (s = isec->next; s != NULL; s = s->next)
4743 if ((s->flags & SEC_ALLOC) != 0)
4744 break;
4745 if (s == NULL)
4747 if (! copy_private_bfd_data (ibfd, obfd))
4748 return false;
4752 ihdr = &elf_section_data (isec)->this_hdr;
4753 ohdr = &elf_section_data (osec)->this_hdr;
4755 ohdr->sh_entsize = ihdr->sh_entsize;
4757 if (ihdr->sh_type == SHT_SYMTAB
4758 || ihdr->sh_type == SHT_DYNSYM
4759 || ihdr->sh_type == SHT_GNU_verneed
4760 || ihdr->sh_type == SHT_GNU_verdef)
4761 ohdr->sh_info = ihdr->sh_info;
4763 elf_section_data (osec)->use_rela_p
4764 = elf_section_data (isec)->use_rela_p;
4766 return true;
4769 /* Copy private symbol information. If this symbol is in a section
4770 which we did not map into a BFD section, try to map the section
4771 index correctly. We use special macro definitions for the mapped
4772 section indices; these definitions are interpreted by the
4773 swap_out_syms function. */
4775 #define MAP_ONESYMTAB (SHN_HIOS + 1)
4776 #define MAP_DYNSYMTAB (SHN_HIOS + 2)
4777 #define MAP_STRTAB (SHN_HIOS + 3)
4778 #define MAP_SHSTRTAB (SHN_HIOS + 4)
4779 #define MAP_SYM_SHNDX (SHN_HIOS + 5)
4781 boolean
4782 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4783 bfd *ibfd;
4784 asymbol *isymarg;
4785 bfd *obfd;
4786 asymbol *osymarg;
4788 elf_symbol_type *isym, *osym;
4790 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4791 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4792 return true;
4794 isym = elf_symbol_from (ibfd, isymarg);
4795 osym = elf_symbol_from (obfd, osymarg);
4797 if (isym != NULL
4798 && osym != NULL
4799 && bfd_is_abs_section (isym->symbol.section))
4801 unsigned int shndx;
4803 shndx = isym->internal_elf_sym.st_shndx;
4804 if (shndx == elf_onesymtab (ibfd))
4805 shndx = MAP_ONESYMTAB;
4806 else if (shndx == elf_dynsymtab (ibfd))
4807 shndx = MAP_DYNSYMTAB;
4808 else if (shndx == elf_tdata (ibfd)->strtab_section)
4809 shndx = MAP_STRTAB;
4810 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4811 shndx = MAP_SHSTRTAB;
4812 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section)
4813 shndx = MAP_SYM_SHNDX;
4814 osym->internal_elf_sym.st_shndx = shndx;
4817 return true;
4820 /* Swap out the symbols. */
4822 static boolean
4823 swap_out_syms (abfd, sttp, relocatable_p)
4824 bfd *abfd;
4825 struct bfd_strtab_hash **sttp;
4826 int relocatable_p;
4828 struct elf_backend_data *bed;
4829 int symcount;
4830 asymbol **syms;
4831 struct bfd_strtab_hash *stt;
4832 Elf_Internal_Shdr *symtab_hdr;
4833 Elf_Internal_Shdr *symtab_shndx_hdr;
4834 Elf_Internal_Shdr *symstrtab_hdr;
4835 char *outbound_syms;
4836 char *outbound_shndx;
4837 int idx;
4838 bfd_size_type amt;
4840 if (!elf_map_symbols (abfd))
4841 return false;
4843 /* Dump out the symtabs. */
4844 stt = _bfd_elf_stringtab_init ();
4845 if (stt == NULL)
4846 return false;
4848 bed = get_elf_backend_data (abfd);
4849 symcount = bfd_get_symcount (abfd);
4850 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4851 symtab_hdr->sh_type = SHT_SYMTAB;
4852 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4853 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4854 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4855 symtab_hdr->sh_addralign = bed->s->file_align;
4857 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4858 symstrtab_hdr->sh_type = SHT_STRTAB;
4860 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
4861 outbound_syms = bfd_alloc (abfd, amt);
4862 if (outbound_syms == NULL)
4863 return false;
4864 symtab_hdr->contents = (PTR) outbound_syms;
4866 outbound_shndx = NULL;
4867 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
4868 if (symtab_shndx_hdr->sh_name != 0)
4870 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx);
4871 outbound_shndx = bfd_alloc (abfd, amt);
4872 if (outbound_shndx == NULL)
4873 return false;
4874 memset (outbound_shndx, 0, (unsigned long) amt);
4875 symtab_shndx_hdr->contents = outbound_shndx;
4876 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX;
4877 symtab_shndx_hdr->sh_size = amt;
4878 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx);
4879 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx);
4882 /* now generate the data (for "contents") */
4884 /* Fill in zeroth symbol and swap it out. */
4885 Elf_Internal_Sym sym;
4886 sym.st_name = 0;
4887 sym.st_value = 0;
4888 sym.st_size = 0;
4889 sym.st_info = 0;
4890 sym.st_other = 0;
4891 sym.st_shndx = SHN_UNDEF;
4892 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
4893 outbound_syms += bed->s->sizeof_sym;
4894 if (outbound_shndx != NULL)
4895 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
4898 syms = bfd_get_outsymbols (abfd);
4899 for (idx = 0; idx < symcount; idx++)
4901 Elf_Internal_Sym sym;
4902 bfd_vma value = syms[idx]->value;
4903 elf_symbol_type *type_ptr;
4904 flagword flags = syms[idx]->flags;
4905 int type;
4907 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4909 /* Local section symbols have no name. */
4910 sym.st_name = 0;
4912 else
4914 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4915 syms[idx]->name,
4916 true, false);
4917 if (sym.st_name == (unsigned long) -1)
4918 return false;
4921 type_ptr = elf_symbol_from (abfd, syms[idx]);
4923 if ((flags & BSF_SECTION_SYM) == 0
4924 && bfd_is_com_section (syms[idx]->section))
4926 /* ELF common symbols put the alignment into the `value' field,
4927 and the size into the `size' field. This is backwards from
4928 how BFD handles it, so reverse it here. */
4929 sym.st_size = value;
4930 if (type_ptr == NULL
4931 || type_ptr->internal_elf_sym.st_value == 0)
4932 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4933 else
4934 sym.st_value = type_ptr->internal_elf_sym.st_value;
4935 sym.st_shndx = _bfd_elf_section_from_bfd_section
4936 (abfd, syms[idx]->section);
4938 else
4940 asection *sec = syms[idx]->section;
4941 int shndx;
4943 if (sec->output_section)
4945 value += sec->output_offset;
4946 sec = sec->output_section;
4948 /* Don't add in the section vma for relocatable output. */
4949 if (! relocatable_p)
4950 value += sec->vma;
4951 sym.st_value = value;
4952 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4954 if (bfd_is_abs_section (sec)
4955 && type_ptr != NULL
4956 && type_ptr->internal_elf_sym.st_shndx != 0)
4958 /* This symbol is in a real ELF section which we did
4959 not create as a BFD section. Undo the mapping done
4960 by copy_private_symbol_data. */
4961 shndx = type_ptr->internal_elf_sym.st_shndx;
4962 switch (shndx)
4964 case MAP_ONESYMTAB:
4965 shndx = elf_onesymtab (abfd);
4966 break;
4967 case MAP_DYNSYMTAB:
4968 shndx = elf_dynsymtab (abfd);
4969 break;
4970 case MAP_STRTAB:
4971 shndx = elf_tdata (abfd)->strtab_section;
4972 break;
4973 case MAP_SHSTRTAB:
4974 shndx = elf_tdata (abfd)->shstrtab_section;
4975 break;
4976 case MAP_SYM_SHNDX:
4977 shndx = elf_tdata (abfd)->symtab_shndx_section;
4978 break;
4979 default:
4980 break;
4983 else
4985 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4987 if (shndx == -1)
4989 asection *sec2;
4991 /* Writing this would be a hell of a lot easier if
4992 we had some decent documentation on bfd, and
4993 knew what to expect of the library, and what to
4994 demand of applications. For example, it
4995 appears that `objcopy' might not set the
4996 section of a symbol to be a section that is
4997 actually in the output file. */
4998 sec2 = bfd_get_section_by_name (abfd, sec->name);
4999 BFD_ASSERT (sec2 != 0);
5000 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
5001 BFD_ASSERT (shndx != -1);
5005 sym.st_shndx = shndx;
5008 if ((flags & BSF_FUNCTION) != 0)
5009 type = STT_FUNC;
5010 else if ((flags & BSF_OBJECT) != 0)
5011 type = STT_OBJECT;
5012 else
5013 type = STT_NOTYPE;
5015 /* Processor-specific types */
5016 if (type_ptr != NULL
5017 && bed->elf_backend_get_symbol_type)
5018 type = ((*bed->elf_backend_get_symbol_type)
5019 (&type_ptr->internal_elf_sym, type));
5021 if (flags & BSF_SECTION_SYM)
5023 if (flags & BSF_GLOBAL)
5024 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
5025 else
5026 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
5028 else if (bfd_is_com_section (syms[idx]->section))
5029 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
5030 else if (bfd_is_und_section (syms[idx]->section))
5031 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
5032 ? STB_WEAK
5033 : STB_GLOBAL),
5034 type);
5035 else if (flags & BSF_FILE)
5036 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
5037 else
5039 int bind = STB_LOCAL;
5041 if (flags & BSF_LOCAL)
5042 bind = STB_LOCAL;
5043 else if (flags & BSF_WEAK)
5044 bind = STB_WEAK;
5045 else if (flags & BSF_GLOBAL)
5046 bind = STB_GLOBAL;
5048 sym.st_info = ELF_ST_INFO (bind, type);
5051 if (type_ptr != NULL)
5052 sym.st_other = type_ptr->internal_elf_sym.st_other;
5053 else
5054 sym.st_other = 0;
5056 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx);
5057 outbound_syms += bed->s->sizeof_sym;
5058 if (outbound_shndx != NULL)
5059 outbound_shndx += sizeof (Elf_External_Sym_Shndx);
5062 *sttp = stt;
5063 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
5064 symstrtab_hdr->sh_type = SHT_STRTAB;
5066 symstrtab_hdr->sh_flags = 0;
5067 symstrtab_hdr->sh_addr = 0;
5068 symstrtab_hdr->sh_entsize = 0;
5069 symstrtab_hdr->sh_link = 0;
5070 symstrtab_hdr->sh_info = 0;
5071 symstrtab_hdr->sh_addralign = 1;
5073 return true;
5076 /* Return the number of bytes required to hold the symtab vector.
5078 Note that we base it on the count plus 1, since we will null terminate
5079 the vector allocated based on this size. However, the ELF symbol table
5080 always has a dummy entry as symbol #0, so it ends up even. */
5082 long
5083 _bfd_elf_get_symtab_upper_bound (abfd)
5084 bfd *abfd;
5086 long symcount;
5087 long symtab_size;
5088 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
5090 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5091 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5093 return symtab_size;
5096 long
5097 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
5098 bfd *abfd;
5100 long symcount;
5101 long symtab_size;
5102 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
5104 if (elf_dynsymtab (abfd) == 0)
5106 bfd_set_error (bfd_error_invalid_operation);
5107 return -1;
5110 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
5111 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
5113 return symtab_size;
5116 long
5117 _bfd_elf_get_reloc_upper_bound (abfd, asect)
5118 bfd *abfd ATTRIBUTE_UNUSED;
5119 sec_ptr asect;
5121 return (asect->reloc_count + 1) * sizeof (arelent *);
5124 /* Canonicalize the relocs. */
5126 long
5127 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
5128 bfd *abfd;
5129 sec_ptr section;
5130 arelent **relptr;
5131 asymbol **symbols;
5133 arelent *tblptr;
5134 unsigned int i;
5135 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5137 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
5138 return -1;
5140 tblptr = section->relocation;
5141 for (i = 0; i < section->reloc_count; i++)
5142 *relptr++ = tblptr++;
5144 *relptr = NULL;
5146 return section->reloc_count;
5149 long
5150 _bfd_elf_get_symtab (abfd, alocation)
5151 bfd *abfd;
5152 asymbol **alocation;
5154 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5155 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
5157 if (symcount >= 0)
5158 bfd_get_symcount (abfd) = symcount;
5159 return symcount;
5162 long
5163 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
5164 bfd *abfd;
5165 asymbol **alocation;
5167 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5168 return bed->s->slurp_symbol_table (abfd, alocation, true);
5171 /* Return the size required for the dynamic reloc entries. Any
5172 section that was actually installed in the BFD, and has type
5173 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
5174 considered to be a dynamic reloc section. */
5176 long
5177 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
5178 bfd *abfd;
5180 long ret;
5181 asection *s;
5183 if (elf_dynsymtab (abfd) == 0)
5185 bfd_set_error (bfd_error_invalid_operation);
5186 return -1;
5189 ret = sizeof (arelent *);
5190 for (s = abfd->sections; s != NULL; s = s->next)
5191 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5192 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5193 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5194 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5195 * sizeof (arelent *));
5197 return ret;
5200 /* Canonicalize the dynamic relocation entries. Note that we return
5201 the dynamic relocations as a single block, although they are
5202 actually associated with particular sections; the interface, which
5203 was designed for SunOS style shared libraries, expects that there
5204 is only one set of dynamic relocs. Any section that was actually
5205 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5206 the dynamic symbol table, is considered to be a dynamic reloc
5207 section. */
5209 long
5210 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5211 bfd *abfd;
5212 arelent **storage;
5213 asymbol **syms;
5215 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5216 asection *s;
5217 long ret;
5219 if (elf_dynsymtab (abfd) == 0)
5221 bfd_set_error (bfd_error_invalid_operation);
5222 return -1;
5225 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5226 ret = 0;
5227 for (s = abfd->sections; s != NULL; s = s->next)
5229 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5230 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5231 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5233 arelent *p;
5234 long count, i;
5236 if (! (*slurp_relocs) (abfd, s, syms, true))
5237 return -1;
5238 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5239 p = s->relocation;
5240 for (i = 0; i < count; i++)
5241 *storage++ = p++;
5242 ret += count;
5246 *storage = NULL;
5248 return ret;
5251 /* Read in the version information. */
5253 boolean
5254 _bfd_elf_slurp_version_tables (abfd)
5255 bfd *abfd;
5257 bfd_byte *contents = NULL;
5258 bfd_size_type amt;
5260 if (elf_dynverdef (abfd) != 0)
5262 Elf_Internal_Shdr *hdr;
5263 Elf_External_Verdef *everdef;
5264 Elf_Internal_Verdef *iverdef;
5265 Elf_Internal_Verdef *iverdefarr;
5266 Elf_Internal_Verdef iverdefmem;
5267 unsigned int i;
5268 unsigned int maxidx;
5270 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5272 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5273 if (contents == NULL)
5274 goto error_return;
5275 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5276 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5277 goto error_return;
5279 /* We know the number of entries in the section but not the maximum
5280 index. Therefore we have to run through all entries and find
5281 the maximum. */
5282 everdef = (Elf_External_Verdef *) contents;
5283 maxidx = 0;
5284 for (i = 0; i < hdr->sh_info; ++i)
5286 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5288 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5289 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5291 everdef = ((Elf_External_Verdef *)
5292 ((bfd_byte *) everdef + iverdefmem.vd_next));
5295 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5296 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5297 if (elf_tdata (abfd)->verdef == NULL)
5298 goto error_return;
5300 elf_tdata (abfd)->cverdefs = maxidx;
5302 everdef = (Elf_External_Verdef *) contents;
5303 iverdefarr = elf_tdata (abfd)->verdef;
5304 for (i = 0; i < hdr->sh_info; i++)
5306 Elf_External_Verdaux *everdaux;
5307 Elf_Internal_Verdaux *iverdaux;
5308 unsigned int j;
5310 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5312 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5313 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5315 iverdef->vd_bfd = abfd;
5317 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5318 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5319 if (iverdef->vd_auxptr == NULL)
5320 goto error_return;
5322 everdaux = ((Elf_External_Verdaux *)
5323 ((bfd_byte *) everdef + iverdef->vd_aux));
5324 iverdaux = iverdef->vd_auxptr;
5325 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5327 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5329 iverdaux->vda_nodename =
5330 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5331 iverdaux->vda_name);
5332 if (iverdaux->vda_nodename == NULL)
5333 goto error_return;
5335 if (j + 1 < iverdef->vd_cnt)
5336 iverdaux->vda_nextptr = iverdaux + 1;
5337 else
5338 iverdaux->vda_nextptr = NULL;
5340 everdaux = ((Elf_External_Verdaux *)
5341 ((bfd_byte *) everdaux + iverdaux->vda_next));
5344 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5346 if (i + 1 < hdr->sh_info)
5347 iverdef->vd_nextdef = iverdef + 1;
5348 else
5349 iverdef->vd_nextdef = NULL;
5351 everdef = ((Elf_External_Verdef *)
5352 ((bfd_byte *) everdef + iverdef->vd_next));
5355 free (contents);
5356 contents = NULL;
5359 if (elf_dynverref (abfd) != 0)
5361 Elf_Internal_Shdr *hdr;
5362 Elf_External_Verneed *everneed;
5363 Elf_Internal_Verneed *iverneed;
5364 unsigned int i;
5366 hdr = &elf_tdata (abfd)->dynverref_hdr;
5368 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5369 elf_tdata (abfd)->verref =
5370 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5371 if (elf_tdata (abfd)->verref == NULL)
5372 goto error_return;
5374 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5376 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5377 if (contents == NULL)
5378 goto error_return;
5379 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5380 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5381 goto error_return;
5383 everneed = (Elf_External_Verneed *) contents;
5384 iverneed = elf_tdata (abfd)->verref;
5385 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5387 Elf_External_Vernaux *evernaux;
5388 Elf_Internal_Vernaux *ivernaux;
5389 unsigned int j;
5391 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5393 iverneed->vn_bfd = abfd;
5395 iverneed->vn_filename =
5396 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5397 iverneed->vn_file);
5398 if (iverneed->vn_filename == NULL)
5399 goto error_return;
5401 amt = iverneed->vn_cnt;
5402 amt *= sizeof (Elf_Internal_Vernaux);
5403 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5405 evernaux = ((Elf_External_Vernaux *)
5406 ((bfd_byte *) everneed + iverneed->vn_aux));
5407 ivernaux = iverneed->vn_auxptr;
5408 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5410 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5412 ivernaux->vna_nodename =
5413 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5414 ivernaux->vna_name);
5415 if (ivernaux->vna_nodename == NULL)
5416 goto error_return;
5418 if (j + 1 < iverneed->vn_cnt)
5419 ivernaux->vna_nextptr = ivernaux + 1;
5420 else
5421 ivernaux->vna_nextptr = NULL;
5423 evernaux = ((Elf_External_Vernaux *)
5424 ((bfd_byte *) evernaux + ivernaux->vna_next));
5427 if (i + 1 < hdr->sh_info)
5428 iverneed->vn_nextref = iverneed + 1;
5429 else
5430 iverneed->vn_nextref = NULL;
5432 everneed = ((Elf_External_Verneed *)
5433 ((bfd_byte *) everneed + iverneed->vn_next));
5436 free (contents);
5437 contents = NULL;
5440 return true;
5442 error_return:
5443 if (contents == NULL)
5444 free (contents);
5445 return false;
5448 asymbol *
5449 _bfd_elf_make_empty_symbol (abfd)
5450 bfd *abfd;
5452 elf_symbol_type *newsym;
5453 bfd_size_type amt = sizeof (elf_symbol_type);
5455 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5456 if (!newsym)
5457 return NULL;
5458 else
5460 newsym->symbol.the_bfd = abfd;
5461 return &newsym->symbol;
5465 void
5466 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5467 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5468 asymbol *symbol;
5469 symbol_info *ret;
5471 bfd_symbol_info (symbol, ret);
5474 /* Return whether a symbol name implies a local symbol. Most targets
5475 use this function for the is_local_label_name entry point, but some
5476 override it. */
5478 boolean
5479 _bfd_elf_is_local_label_name (abfd, name)
5480 bfd *abfd ATTRIBUTE_UNUSED;
5481 const char *name;
5483 /* Normal local symbols start with ``.L''. */
5484 if (name[0] == '.' && name[1] == 'L')
5485 return true;
5487 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5488 DWARF debugging symbols starting with ``..''. */
5489 if (name[0] == '.' && name[1] == '.')
5490 return true;
5492 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5493 emitting DWARF debugging output. I suspect this is actually a
5494 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5495 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5496 underscore to be emitted on some ELF targets). For ease of use,
5497 we treat such symbols as local. */
5498 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5499 return true;
5501 return false;
5504 alent *
5505 _bfd_elf_get_lineno (ignore_abfd, symbol)
5506 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5507 asymbol *symbol ATTRIBUTE_UNUSED;
5509 abort ();
5510 return NULL;
5513 boolean
5514 _bfd_elf_set_arch_mach (abfd, arch, machine)
5515 bfd *abfd;
5516 enum bfd_architecture arch;
5517 unsigned long machine;
5519 /* If this isn't the right architecture for this backend, and this
5520 isn't the generic backend, fail. */
5521 if (arch != get_elf_backend_data (abfd)->arch
5522 && arch != bfd_arch_unknown
5523 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5524 return false;
5526 return bfd_default_set_arch_mach (abfd, arch, machine);
5529 /* Find the function to a particular section and offset,
5530 for error reporting. */
5532 static boolean
5533 elf_find_function (abfd, section, symbols, offset,
5534 filename_ptr, functionname_ptr)
5535 bfd *abfd ATTRIBUTE_UNUSED;
5536 asection *section;
5537 asymbol **symbols;
5538 bfd_vma offset;
5539 const char **filename_ptr;
5540 const char **functionname_ptr;
5542 const char *filename;
5543 asymbol *func;
5544 bfd_vma low_func;
5545 asymbol **p;
5547 filename = NULL;
5548 func = NULL;
5549 low_func = 0;
5551 for (p = symbols; *p != NULL; p++)
5553 elf_symbol_type *q;
5555 q = (elf_symbol_type *) *p;
5557 if (bfd_get_section (&q->symbol) != section)
5558 continue;
5560 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5562 default:
5563 break;
5564 case STT_FILE:
5565 filename = bfd_asymbol_name (&q->symbol);
5566 break;
5567 case STT_NOTYPE:
5568 case STT_FUNC:
5569 if (q->symbol.section == section
5570 && q->symbol.value >= low_func
5571 && q->symbol.value <= offset)
5573 func = (asymbol *) q;
5574 low_func = q->symbol.value;
5576 break;
5580 if (func == NULL)
5581 return false;
5583 if (filename_ptr)
5584 *filename_ptr = filename;
5585 if (functionname_ptr)
5586 *functionname_ptr = bfd_asymbol_name (func);
5588 return true;
5591 /* Find the nearest line to a particular section and offset,
5592 for error reporting. */
5594 boolean
5595 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5596 filename_ptr, functionname_ptr, line_ptr)
5597 bfd *abfd;
5598 asection *section;
5599 asymbol **symbols;
5600 bfd_vma offset;
5601 const char **filename_ptr;
5602 const char **functionname_ptr;
5603 unsigned int *line_ptr;
5605 boolean found;
5607 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5608 filename_ptr, functionname_ptr,
5609 line_ptr))
5611 if (!*functionname_ptr)
5612 elf_find_function (abfd, section, symbols, offset,
5613 *filename_ptr ? NULL : filename_ptr,
5614 functionname_ptr);
5616 return true;
5619 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5620 filename_ptr, functionname_ptr,
5621 line_ptr, 0,
5622 &elf_tdata (abfd)->dwarf2_find_line_info))
5624 if (!*functionname_ptr)
5625 elf_find_function (abfd, section, symbols, offset,
5626 *filename_ptr ? NULL : filename_ptr,
5627 functionname_ptr);
5629 return true;
5632 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5633 &found, filename_ptr,
5634 functionname_ptr, line_ptr,
5635 &elf_tdata (abfd)->line_info))
5636 return false;
5637 if (found)
5638 return true;
5640 if (symbols == NULL)
5641 return false;
5643 if (! elf_find_function (abfd, section, symbols, offset,
5644 filename_ptr, functionname_ptr))
5645 return false;
5647 *line_ptr = 0;
5648 return true;
5652 _bfd_elf_sizeof_headers (abfd, reloc)
5653 bfd *abfd;
5654 boolean reloc;
5656 int ret;
5658 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5659 if (! reloc)
5660 ret += get_program_header_size (abfd);
5661 return ret;
5664 boolean
5665 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5666 bfd *abfd;
5667 sec_ptr section;
5668 PTR location;
5669 file_ptr offset;
5670 bfd_size_type count;
5672 Elf_Internal_Shdr *hdr;
5673 bfd_signed_vma pos;
5675 if (! abfd->output_has_begun
5676 && ! _bfd_elf_compute_section_file_positions
5677 (abfd, (struct bfd_link_info *) NULL))
5678 return false;
5680 hdr = &elf_section_data (section)->this_hdr;
5681 pos = hdr->sh_offset + offset;
5682 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5683 || bfd_bwrite (location, count, abfd) != count)
5684 return false;
5686 return true;
5689 void
5690 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5691 bfd *abfd ATTRIBUTE_UNUSED;
5692 arelent *cache_ptr ATTRIBUTE_UNUSED;
5693 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5695 abort ();
5698 #if 0
5699 void
5700 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5701 bfd *abfd;
5702 arelent *cache_ptr;
5703 Elf_Internal_Rel *dst;
5705 abort ();
5707 #endif
5709 /* Try to convert a non-ELF reloc into an ELF one. */
5711 boolean
5712 _bfd_elf_validate_reloc (abfd, areloc)
5713 bfd *abfd;
5714 arelent *areloc;
5716 /* Check whether we really have an ELF howto. */
5718 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5720 bfd_reloc_code_real_type code;
5721 reloc_howto_type *howto;
5723 /* Alien reloc: Try to determine its type to replace it with an
5724 equivalent ELF reloc. */
5726 if (areloc->howto->pc_relative)
5728 switch (areloc->howto->bitsize)
5730 case 8:
5731 code = BFD_RELOC_8_PCREL;
5732 break;
5733 case 12:
5734 code = BFD_RELOC_12_PCREL;
5735 break;
5736 case 16:
5737 code = BFD_RELOC_16_PCREL;
5738 break;
5739 case 24:
5740 code = BFD_RELOC_24_PCREL;
5741 break;
5742 case 32:
5743 code = BFD_RELOC_32_PCREL;
5744 break;
5745 case 64:
5746 code = BFD_RELOC_64_PCREL;
5747 break;
5748 default:
5749 goto fail;
5752 howto = bfd_reloc_type_lookup (abfd, code);
5754 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5756 if (howto->pcrel_offset)
5757 areloc->addend += areloc->address;
5758 else
5759 areloc->addend -= areloc->address; /* addend is unsigned!! */
5762 else
5764 switch (areloc->howto->bitsize)
5766 case 8:
5767 code = BFD_RELOC_8;
5768 break;
5769 case 14:
5770 code = BFD_RELOC_14;
5771 break;
5772 case 16:
5773 code = BFD_RELOC_16;
5774 break;
5775 case 26:
5776 code = BFD_RELOC_26;
5777 break;
5778 case 32:
5779 code = BFD_RELOC_32;
5780 break;
5781 case 64:
5782 code = BFD_RELOC_64;
5783 break;
5784 default:
5785 goto fail;
5788 howto = bfd_reloc_type_lookup (abfd, code);
5791 if (howto)
5792 areloc->howto = howto;
5793 else
5794 goto fail;
5797 return true;
5799 fail:
5800 (*_bfd_error_handler)
5801 (_("%s: unsupported relocation type %s"),
5802 bfd_archive_filename (abfd), areloc->howto->name);
5803 bfd_set_error (bfd_error_bad_value);
5804 return false;
5807 boolean
5808 _bfd_elf_close_and_cleanup (abfd)
5809 bfd *abfd;
5811 if (bfd_get_format (abfd) == bfd_object)
5813 if (elf_shstrtab (abfd) != NULL)
5814 _bfd_elf_strtab_free (elf_shstrtab (abfd));
5817 return _bfd_generic_close_and_cleanup (abfd);
5820 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5821 in the relocation's offset. Thus we cannot allow any sort of sanity
5822 range-checking to interfere. There is nothing else to do in processing
5823 this reloc. */
5825 bfd_reloc_status_type
5826 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5827 bfd *abfd ATTRIBUTE_UNUSED;
5828 arelent *re ATTRIBUTE_UNUSED;
5829 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5830 PTR data ATTRIBUTE_UNUSED;
5831 asection *is ATTRIBUTE_UNUSED;
5832 bfd *obfd ATTRIBUTE_UNUSED;
5833 char **errmsg ATTRIBUTE_UNUSED;
5835 return bfd_reloc_ok;
5838 /* Elf core file support. Much of this only works on native
5839 toolchains, since we rely on knowing the
5840 machine-dependent procfs structure in order to pick
5841 out details about the corefile. */
5843 #ifdef HAVE_SYS_PROCFS_H
5844 # include <sys/procfs.h>
5845 #endif
5847 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5849 static int
5850 elfcore_make_pid (abfd)
5851 bfd *abfd;
5853 return ((elf_tdata (abfd)->core_lwpid << 16)
5854 + (elf_tdata (abfd)->core_pid));
5857 /* If there isn't a section called NAME, make one, using
5858 data from SECT. Note, this function will generate a
5859 reference to NAME, so you shouldn't deallocate or
5860 overwrite it. */
5862 static boolean
5863 elfcore_maybe_make_sect (abfd, name, sect)
5864 bfd *abfd;
5865 char *name;
5866 asection *sect;
5868 asection *sect2;
5870 if (bfd_get_section_by_name (abfd, name) != NULL)
5871 return true;
5873 sect2 = bfd_make_section (abfd, name);
5874 if (sect2 == NULL)
5875 return false;
5877 sect2->_raw_size = sect->_raw_size;
5878 sect2->filepos = sect->filepos;
5879 sect2->flags = sect->flags;
5880 sect2->alignment_power = sect->alignment_power;
5881 return true;
5884 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5885 actually creates up to two pseudosections:
5886 - For the single-threaded case, a section named NAME, unless
5887 such a section already exists.
5888 - For the multi-threaded case, a section named "NAME/PID", where
5889 PID is elfcore_make_pid (abfd).
5890 Both pseudosections have identical contents. */
5891 boolean
5892 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5893 bfd *abfd;
5894 char *name;
5895 size_t size;
5896 ufile_ptr filepos;
5898 char buf[100];
5899 char *threaded_name;
5900 asection *sect;
5902 /* Build the section name. */
5904 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5905 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
5906 if (threaded_name == NULL)
5907 return false;
5908 strcpy (threaded_name, buf);
5910 sect = bfd_make_section (abfd, threaded_name);
5911 if (sect == NULL)
5912 return false;
5913 sect->_raw_size = size;
5914 sect->filepos = filepos;
5915 sect->flags = SEC_HAS_CONTENTS;
5916 sect->alignment_power = 2;
5918 return elfcore_maybe_make_sect (abfd, name, sect);
5921 /* prstatus_t exists on:
5922 solaris 2.5+
5923 linux 2.[01] + glibc
5924 unixware 4.2
5927 #if defined (HAVE_PRSTATUS_T)
5928 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5930 static boolean
5931 elfcore_grok_prstatus (abfd, note)
5932 bfd *abfd;
5933 Elf_Internal_Note *note;
5935 size_t raw_size;
5936 int offset;
5938 if (note->descsz == sizeof (prstatus_t))
5940 prstatus_t prstat;
5942 raw_size = sizeof (prstat.pr_reg);
5943 offset = offsetof (prstatus_t, pr_reg);
5944 memcpy (&prstat, note->descdata, sizeof (prstat));
5946 /* Do not overwrite the core signal if it
5947 has already been set by another thread. */
5948 if (elf_tdata (abfd)->core_signal == 0)
5949 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5950 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5952 /* pr_who exists on:
5953 solaris 2.5+
5954 unixware 4.2
5955 pr_who doesn't exist on:
5956 linux 2.[01]
5958 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5959 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5960 #endif
5962 #if defined (HAVE_PRSTATUS32_T)
5963 else if (note->descsz == sizeof (prstatus32_t))
5965 /* 64-bit host, 32-bit corefile */
5966 prstatus32_t prstat;
5968 raw_size = sizeof (prstat.pr_reg);
5969 offset = offsetof (prstatus32_t, pr_reg);
5970 memcpy (&prstat, note->descdata, sizeof (prstat));
5972 /* Do not overwrite the core signal if it
5973 has already been set by another thread. */
5974 if (elf_tdata (abfd)->core_signal == 0)
5975 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5976 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5978 /* pr_who exists on:
5979 solaris 2.5+
5980 unixware 4.2
5981 pr_who doesn't exist on:
5982 linux 2.[01]
5984 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5985 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5986 #endif
5988 #endif /* HAVE_PRSTATUS32_T */
5989 else
5991 /* Fail - we don't know how to handle any other
5992 note size (ie. data object type). */
5993 return true;
5996 /* Make a ".reg/999" section and a ".reg" section. */
5997 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5998 raw_size, note->descpos + offset);
6000 #endif /* defined (HAVE_PRSTATUS_T) */
6002 /* Create a pseudosection containing the exact contents of NOTE. */
6003 static boolean
6004 elfcore_make_note_pseudosection (abfd, name, note)
6005 bfd *abfd;
6006 char *name;
6007 Elf_Internal_Note *note;
6009 return _bfd_elfcore_make_pseudosection (abfd, name,
6010 note->descsz, note->descpos);
6013 /* There isn't a consistent prfpregset_t across platforms,
6014 but it doesn't matter, because we don't have to pick this
6015 data structure apart. */
6017 static boolean
6018 elfcore_grok_prfpreg (abfd, note)
6019 bfd *abfd;
6020 Elf_Internal_Note *note;
6022 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6025 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
6026 type of 5 (NT_PRXFPREG). Just include the whole note's contents
6027 literally. */
6029 static boolean
6030 elfcore_grok_prxfpreg (abfd, note)
6031 bfd *abfd;
6032 Elf_Internal_Note *note;
6034 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
6037 #if defined (HAVE_PRPSINFO_T)
6038 typedef prpsinfo_t elfcore_psinfo_t;
6039 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
6040 typedef prpsinfo32_t elfcore_psinfo32_t;
6041 #endif
6042 #endif
6044 #if defined (HAVE_PSINFO_T)
6045 typedef psinfo_t elfcore_psinfo_t;
6046 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
6047 typedef psinfo32_t elfcore_psinfo32_t;
6048 #endif
6049 #endif
6051 /* return a malloc'ed copy of a string at START which is at
6052 most MAX bytes long, possibly without a terminating '\0'.
6053 the copy will always have a terminating '\0'. */
6055 char *
6056 _bfd_elfcore_strndup (abfd, start, max)
6057 bfd *abfd;
6058 char *start;
6059 size_t max;
6061 char *dups;
6062 char *end = memchr (start, '\0', max);
6063 size_t len;
6065 if (end == NULL)
6066 len = max;
6067 else
6068 len = end - start;
6070 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
6071 if (dups == NULL)
6072 return NULL;
6074 memcpy (dups, start, len);
6075 dups[len] = '\0';
6077 return dups;
6080 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6081 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
6083 static boolean
6084 elfcore_grok_psinfo (abfd, note)
6085 bfd *abfd;
6086 Elf_Internal_Note *note;
6088 if (note->descsz == sizeof (elfcore_psinfo_t))
6090 elfcore_psinfo_t psinfo;
6092 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6094 elf_tdata (abfd)->core_program
6095 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6096 sizeof (psinfo.pr_fname));
6098 elf_tdata (abfd)->core_command
6099 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6100 sizeof (psinfo.pr_psargs));
6102 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
6103 else if (note->descsz == sizeof (elfcore_psinfo32_t))
6105 /* 64-bit host, 32-bit corefile */
6106 elfcore_psinfo32_t psinfo;
6108 memcpy (&psinfo, note->descdata, sizeof (psinfo));
6110 elf_tdata (abfd)->core_program
6111 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
6112 sizeof (psinfo.pr_fname));
6114 elf_tdata (abfd)->core_command
6115 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
6116 sizeof (psinfo.pr_psargs));
6118 #endif
6120 else
6122 /* Fail - we don't know how to handle any other
6123 note size (ie. data object type). */
6124 return true;
6127 /* Note that for some reason, a spurious space is tacked
6128 onto the end of the args in some (at least one anyway)
6129 implementations, so strip it off if it exists. */
6132 char *command = elf_tdata (abfd)->core_command;
6133 int n = strlen (command);
6135 if (0 < n && command[n - 1] == ' ')
6136 command[n - 1] = '\0';
6139 return true;
6141 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
6143 #if defined (HAVE_PSTATUS_T)
6144 static boolean
6145 elfcore_grok_pstatus (abfd, note)
6146 bfd *abfd;
6147 Elf_Internal_Note *note;
6149 if (note->descsz == sizeof (pstatus_t)
6150 #if defined (HAVE_PXSTATUS_T)
6151 || note->descsz == sizeof (pxstatus_t)
6152 #endif
6155 pstatus_t pstat;
6157 memcpy (&pstat, note->descdata, sizeof (pstat));
6159 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6161 #if defined (HAVE_PSTATUS32_T)
6162 else if (note->descsz == sizeof (pstatus32_t))
6164 /* 64-bit host, 32-bit corefile */
6165 pstatus32_t pstat;
6167 memcpy (&pstat, note->descdata, sizeof (pstat));
6169 elf_tdata (abfd)->core_pid = pstat.pr_pid;
6171 #endif
6172 /* Could grab some more details from the "representative"
6173 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
6174 NT_LWPSTATUS note, presumably. */
6176 return true;
6178 #endif /* defined (HAVE_PSTATUS_T) */
6180 #if defined (HAVE_LWPSTATUS_T)
6181 static boolean
6182 elfcore_grok_lwpstatus (abfd, note)
6183 bfd *abfd;
6184 Elf_Internal_Note *note;
6186 lwpstatus_t lwpstat;
6187 char buf[100];
6188 char *name;
6189 asection *sect;
6191 if (note->descsz != sizeof (lwpstat)
6192 #if defined (HAVE_LWPXSTATUS_T)
6193 && note->descsz != sizeof (lwpxstatus_t)
6194 #endif
6196 return true;
6198 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6200 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6201 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6203 /* Make a ".reg/999" section. */
6205 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6206 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6207 if (name == NULL)
6208 return false;
6209 strcpy (name, buf);
6211 sect = bfd_make_section (abfd, name);
6212 if (sect == NULL)
6213 return false;
6215 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6216 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6217 sect->filepos = note->descpos
6218 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6219 #endif
6221 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6222 sect->_raw_size = sizeof (lwpstat.pr_reg);
6223 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6224 #endif
6226 sect->flags = SEC_HAS_CONTENTS;
6227 sect->alignment_power = 2;
6229 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6230 return false;
6232 /* Make a ".reg2/999" section */
6234 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6235 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6236 if (name == NULL)
6237 return false;
6238 strcpy (name, buf);
6240 sect = bfd_make_section (abfd, name);
6241 if (sect == NULL)
6242 return false;
6244 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6245 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6246 sect->filepos = note->descpos
6247 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6248 #endif
6250 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6251 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6252 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6253 #endif
6255 sect->flags = SEC_HAS_CONTENTS;
6256 sect->alignment_power = 2;
6258 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6260 #endif /* defined (HAVE_LWPSTATUS_T) */
6262 #if defined (HAVE_WIN32_PSTATUS_T)
6263 static boolean
6264 elfcore_grok_win32pstatus (abfd, note)
6265 bfd *abfd;
6266 Elf_Internal_Note *note;
6268 char buf[30];
6269 char *name;
6270 asection *sect;
6271 win32_pstatus_t pstatus;
6273 if (note->descsz < sizeof (pstatus))
6274 return true;
6276 memcpy (&pstatus, note->descdata, note->descsz);
6278 switch (pstatus.data_type)
6280 case NOTE_INFO_PROCESS:
6281 /* FIXME: need to add ->core_command. */
6282 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6283 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6284 break;
6286 case NOTE_INFO_THREAD:
6287 /* Make a ".reg/999" section. */
6288 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6290 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6291 if (name == NULL)
6292 return false;
6294 strcpy (name, buf);
6296 sect = bfd_make_section (abfd, name);
6297 if (sect == NULL)
6298 return false;
6300 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6301 sect->filepos = (note->descpos
6302 + offsetof (struct win32_pstatus,
6303 data.thread_info.thread_context));
6304 sect->flags = SEC_HAS_CONTENTS;
6305 sect->alignment_power = 2;
6307 if (pstatus.data.thread_info.is_active_thread)
6308 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6309 return false;
6310 break;
6312 case NOTE_INFO_MODULE:
6313 /* Make a ".module/xxxxxxxx" section. */
6314 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6316 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6317 if (name == NULL)
6318 return false;
6320 strcpy (name, buf);
6322 sect = bfd_make_section (abfd, name);
6324 if (sect == NULL)
6325 return false;
6327 sect->_raw_size = note->descsz;
6328 sect->filepos = note->descpos;
6329 sect->flags = SEC_HAS_CONTENTS;
6330 sect->alignment_power = 2;
6331 break;
6333 default:
6334 return true;
6337 return true;
6339 #endif /* HAVE_WIN32_PSTATUS_T */
6341 static boolean
6342 elfcore_grok_note (abfd, note)
6343 bfd *abfd;
6344 Elf_Internal_Note *note;
6346 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6348 switch (note->type)
6350 default:
6351 return true;
6353 case NT_PRSTATUS:
6354 if (bed->elf_backend_grok_prstatus)
6355 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6356 return true;
6357 #if defined (HAVE_PRSTATUS_T)
6358 return elfcore_grok_prstatus (abfd, note);
6359 #else
6360 return true;
6361 #endif
6363 #if defined (HAVE_PSTATUS_T)
6364 case NT_PSTATUS:
6365 return elfcore_grok_pstatus (abfd, note);
6366 #endif
6368 #if defined (HAVE_LWPSTATUS_T)
6369 case NT_LWPSTATUS:
6370 return elfcore_grok_lwpstatus (abfd, note);
6371 #endif
6373 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6374 return elfcore_grok_prfpreg (abfd, note);
6376 #if defined (HAVE_WIN32_PSTATUS_T)
6377 case NT_WIN32PSTATUS:
6378 return elfcore_grok_win32pstatus (abfd, note);
6379 #endif
6381 case NT_PRXFPREG: /* Linux SSE extension */
6382 if (note->namesz == 5
6383 && ! strcmp (note->namedata, "LINUX"))
6384 return elfcore_grok_prxfpreg (abfd, note);
6385 else
6386 return true;
6388 case NT_PRPSINFO:
6389 case NT_PSINFO:
6390 if (bed->elf_backend_grok_psinfo)
6391 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6392 return true;
6393 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6394 return elfcore_grok_psinfo (abfd, note);
6395 #else
6396 return true;
6397 #endif
6401 static boolean
6402 elfcore_netbsd_get_lwpid (note, lwpidp)
6403 Elf_Internal_Note *note;
6404 int *lwpidp;
6406 char *cp;
6408 cp = strchr (note->namedata, '@');
6409 if (cp != NULL)
6411 *lwpidp = atoi(cp);
6412 return true;
6414 return false;
6417 static boolean
6418 elfcore_grok_netbsd_procinfo (abfd, note)
6419 bfd *abfd;
6420 Elf_Internal_Note *note;
6423 /* Signal number at offset 0x08. */
6424 elf_tdata (abfd)->core_signal
6425 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08);
6427 /* Process ID at offset 0x50. */
6428 elf_tdata (abfd)->core_pid
6429 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50);
6431 /* Command name at 0x7c (max 32 bytes, including nul). */
6432 elf_tdata (abfd)->core_command
6433 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31);
6435 return true;
6438 static boolean
6439 elfcore_grok_netbsd_note (abfd, note)
6440 bfd *abfd;
6441 Elf_Internal_Note *note;
6443 int lwp;
6445 if (elfcore_netbsd_get_lwpid (note, &lwp))
6446 elf_tdata (abfd)->core_lwpid = lwp;
6448 if (note->type == NT_NETBSDCORE_PROCINFO)
6450 /* NetBSD-specific core "procinfo". Note that we expect to
6451 find this note before any of the others, which is fine,
6452 since the kernel writes this note out first when it
6453 creates a core file. */
6455 return elfcore_grok_netbsd_procinfo (abfd, note);
6458 /* As of Jan 2002 there are no other machine-independent notes
6459 defined for NetBSD core files. If the note type is less
6460 than the start of the machine-dependent note types, we don't
6461 understand it. */
6463 if (note->type < NT_NETBSDCORE_FIRSTMACH)
6464 return true;
6467 switch (bfd_get_arch (abfd))
6469 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and
6470 PT_GETFPREGS == mach+2. */
6472 case bfd_arch_alpha:
6473 case bfd_arch_sparc:
6474 switch (note->type)
6476 case NT_NETBSDCORE_FIRSTMACH+0:
6477 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6479 case NT_NETBSDCORE_FIRSTMACH+2:
6480 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6482 default:
6483 return true;
6486 /* On all other arch's, PT_GETREGS == mach+1 and
6487 PT_GETFPREGS == mach+3. */
6489 default:
6490 switch (note->type)
6492 case NT_NETBSDCORE_FIRSTMACH+1:
6493 return elfcore_make_note_pseudosection (abfd, ".reg", note);
6495 case NT_NETBSDCORE_FIRSTMACH+3:
6496 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
6498 default:
6499 return true;
6502 /* NOTREACHED */
6505 /* Function: elfcore_write_note
6507 Inputs:
6508 buffer to hold note
6509 name of note
6510 type of note
6511 data for note
6512 size of data for note
6514 Return:
6515 End of buffer containing note. */
6517 char *
6518 elfcore_write_note (abfd, buf, bufsiz, name, type, input, size)
6519 bfd *abfd;
6520 char *buf;
6521 int *bufsiz;
6522 char *name;
6523 int type;
6524 void *input;
6525 int size;
6527 Elf_External_Note *xnp;
6528 int namesz = strlen (name);
6529 int newspace = BFD_ALIGN (sizeof (Elf_External_Note) + size + namesz - 1, 4);
6530 char *p, *dest;
6532 p = realloc (buf, *bufsiz + newspace);
6533 dest = p + *bufsiz;
6534 *bufsiz += newspace;
6535 xnp = (Elf_External_Note *) dest;
6536 H_PUT_32 (abfd, namesz, xnp->namesz);
6537 H_PUT_32 (abfd, size, xnp->descsz);
6538 H_PUT_32 (abfd, type, xnp->type);
6539 strcpy (xnp->name, name);
6540 memcpy (xnp->name + BFD_ALIGN (namesz, 4), input, size);
6541 return p;
6544 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6545 char *
6546 elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs)
6547 bfd *abfd;
6548 char *buf;
6549 int *bufsiz;
6550 char *fname;
6551 char *psargs;
6553 int note_type;
6554 char *note_name = "CORE";
6556 #if defined (HAVE_PSINFO_T)
6557 psinfo_t data;
6558 note_type = NT_PSINFO;
6559 #else
6560 prpsinfo_t data;
6561 note_type = NT_PRPSINFO;
6562 #endif
6564 memset (&data, 0, sizeof (data));
6565 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
6566 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
6567 return elfcore_write_note (abfd, buf, bufsiz,
6568 note_name, note_type, &data, sizeof (data));
6570 #endif /* PSINFO_T or PRPSINFO_T */
6572 #if defined (HAVE_PRSTATUS_T)
6573 char *
6574 elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6575 bfd *abfd;
6576 char *buf;
6577 int *bufsiz;
6578 long pid;
6579 int cursig;
6580 void *gregs;
6582 prstatus_t prstat;
6583 char *note_name = "CORE";
6585 memset (&prstat, 0, sizeof (prstat));
6586 prstat.pr_pid = pid;
6587 prstat.pr_cursig = cursig;
6588 memcpy (prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
6589 return elfcore_write_note (abfd, buf, bufsiz,
6590 note_name, NT_PRSTATUS, &prstat, sizeof (prstat));
6592 #endif /* HAVE_PRSTATUS_T */
6594 #if defined (HAVE_PSTATUS_T)
6595 char *
6596 elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs)
6597 bfd *abfd;
6598 char *buf;
6599 int *bufsiz;
6600 long pid;
6601 int cursig;
6602 void *gregs;
6604 pstatus_t pstat;
6605 char *note_name = "CORE";
6607 memset (&pstat, 0, sizeof (prstat));
6608 pstat.pr_pid = pid;
6609 memcpy (pstat.pr_reg, gregs, sizeof (pstat.pr_reg));
6610 return elfcore_write_note (abfd, buf, bufsiz,
6611 note_name, NT_PSTATUS, &pstat, sizeof (pstat));
6613 #endif /* HAVE_PSTATUS_T */
6615 char *
6616 elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size)
6617 bfd *abfd;
6618 char *buf;
6619 int *bufsiz;
6620 void *fpregs;
6621 int size;
6623 char *note_name = "CORE";
6624 return elfcore_write_note (abfd, buf, bufsiz,
6625 note_name, NT_FPREGSET, fpregs, size);
6628 char *
6629 elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size)
6630 bfd *abfd;
6631 char *buf;
6632 int *bufsiz;
6633 void *xfpregs;
6634 int size;
6636 char *note_name = "LINUX";
6637 return elfcore_write_note (abfd, buf, bufsiz,
6638 note_name, NT_PRXFPREG, xfpregs, size);
6641 static boolean
6642 elfcore_read_notes (abfd, offset, size)
6643 bfd *abfd;
6644 file_ptr offset;
6645 bfd_size_type size;
6647 char *buf;
6648 char *p;
6650 if (size <= 0)
6651 return true;
6653 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
6654 return false;
6656 buf = bfd_malloc (size);
6657 if (buf == NULL)
6658 return false;
6660 if (bfd_bread (buf, size, abfd) != size)
6662 error:
6663 free (buf);
6664 return false;
6667 p = buf;
6668 while (p < buf + size)
6670 /* FIXME: bad alignment assumption. */
6671 Elf_External_Note *xnp = (Elf_External_Note *) p;
6672 Elf_Internal_Note in;
6674 in.type = H_GET_32 (abfd, xnp->type);
6676 in.namesz = H_GET_32 (abfd, xnp->namesz);
6677 in.namedata = xnp->name;
6679 in.descsz = H_GET_32 (abfd, xnp->descsz);
6680 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
6681 in.descpos = offset + (in.descdata - buf);
6683 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0)
6685 if (! elfcore_grok_netbsd_note (abfd, &in))
6686 goto error;
6688 else
6690 if (! elfcore_grok_note (abfd, &in))
6691 goto error;
6694 p = in.descdata + BFD_ALIGN (in.descsz, 4);
6697 free (buf);
6698 return true;
6701 /* Providing external access to the ELF program header table. */
6703 /* Return an upper bound on the number of bytes required to store a
6704 copy of ABFD's program header table entries. Return -1 if an error
6705 occurs; bfd_get_error will return an appropriate code. */
6707 long
6708 bfd_get_elf_phdr_upper_bound (abfd)
6709 bfd *abfd;
6711 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6713 bfd_set_error (bfd_error_wrong_format);
6714 return -1;
6717 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
6720 /* Copy ABFD's program header table entries to *PHDRS. The entries
6721 will be stored as an array of Elf_Internal_Phdr structures, as
6722 defined in include/elf/internal.h. To find out how large the
6723 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6725 Return the number of program header table entries read, or -1 if an
6726 error occurs; bfd_get_error will return an appropriate code. */
6729 bfd_get_elf_phdrs (abfd, phdrs)
6730 bfd *abfd;
6731 void *phdrs;
6733 int num_phdrs;
6735 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6737 bfd_set_error (bfd_error_wrong_format);
6738 return -1;
6741 num_phdrs = elf_elfheader (abfd)->e_phnum;
6742 memcpy (phdrs, elf_tdata (abfd)->phdr,
6743 num_phdrs * sizeof (Elf_Internal_Phdr));
6745 return num_phdrs;
6748 void
6749 _bfd_elf_sprintf_vma (abfd, buf, value)
6750 bfd *abfd ATTRIBUTE_UNUSED;
6751 char *buf;
6752 bfd_vma value;
6754 #ifdef BFD64
6755 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6757 i_ehdrp = elf_elfheader (abfd);
6758 if (i_ehdrp == NULL)
6759 sprintf_vma (buf, value);
6760 else
6762 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6764 #if BFD_HOST_64BIT_LONG
6765 sprintf (buf, "%016lx", value);
6766 #else
6767 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6768 _bfd_int64_low (value));
6769 #endif
6771 else
6772 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6774 #else
6775 sprintf_vma (buf, value);
6776 #endif
6779 void
6780 _bfd_elf_fprintf_vma (abfd, stream, value)
6781 bfd *abfd ATTRIBUTE_UNUSED;
6782 PTR stream;
6783 bfd_vma value;
6785 #ifdef BFD64
6786 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6788 i_ehdrp = elf_elfheader (abfd);
6789 if (i_ehdrp == NULL)
6790 fprintf_vma ((FILE *) stream, value);
6791 else
6793 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6795 #if BFD_HOST_64BIT_LONG
6796 fprintf ((FILE *) stream, "%016lx", value);
6797 #else
6798 fprintf ((FILE *) stream, "%08lx%08lx",
6799 _bfd_int64_high (value), _bfd_int64_low (value));
6800 #endif
6802 else
6803 fprintf ((FILE *) stream, "%08lx",
6804 (unsigned long) (value & 0xffffffff));
6806 #else
6807 fprintf_vma ((FILE *) stream, value);
6808 #endif
6811 enum elf_reloc_type_class
6812 _bfd_elf_reloc_type_class (rela)
6813 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
6815 return reloc_class_normal;
6818 /* For RELA architectures, return what the relocation value for
6819 relocation against a local symbol. */
6821 bfd_vma
6822 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
6823 bfd *abfd;
6824 Elf_Internal_Sym *sym;
6825 asection *sec;
6826 Elf_Internal_Rela *rel;
6828 bfd_vma relocation;
6830 relocation = (sec->output_section->vma
6831 + sec->output_offset
6832 + sym->st_value);
6833 if ((sec->flags & SEC_MERGE)
6834 && ELF_ST_TYPE (sym->st_info) == STT_SECTION
6835 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE)
6837 asection *msec;
6839 msec = sec;
6840 rel->r_addend =
6841 _bfd_merged_section_offset (abfd, &msec,
6842 elf_section_data (sec)->sec_info,
6843 sym->st_value + rel->r_addend,
6844 (bfd_vma) 0)
6845 - relocation;
6846 rel->r_addend += msec->output_section->vma + msec->output_offset;
6848 return relocation;
6851 bfd_vma
6852 _bfd_elf_rel_local_sym (abfd, sym, psec, addend)
6853 bfd *abfd;
6854 Elf_Internal_Sym *sym;
6855 asection **psec;
6856 bfd_vma addend;
6858 asection *sec = *psec;
6860 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE)
6861 return sym->st_value + addend;
6863 return _bfd_merged_section_offset (abfd, psec,
6864 elf_section_data (sec)->sec_info,
6865 sym->st_value + addend, (bfd_vma) 0);
6868 bfd_vma
6869 _bfd_elf_section_offset (abfd, info, sec, offset)
6870 bfd *abfd;
6871 struct bfd_link_info *info;
6872 asection *sec;
6873 bfd_vma offset;
6875 struct bfd_elf_section_data *sec_data;
6877 sec_data = elf_section_data (sec);
6878 switch (sec_data->sec_info_type)
6880 case ELF_INFO_TYPE_STABS:
6881 return _bfd_stab_section_offset
6882 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info,
6883 offset);
6884 case ELF_INFO_TYPE_EH_FRAME:
6885 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset);
6886 default:
6887 return offset;