2001-11-16 Michael Snyder <msnyder@redhat.com>
[binutils.git] / bfd / elf.c
blob81923d176dd8f655ecc2cf8ab4f6e86c9178741b
1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 SECTION
24 ELF backends
26 BFD support for ELF formats is being worked on.
27 Currently, the best supported back ends are for sparc and i386
28 (running svr4 or Solaris 2).
30 Documentation of the internals of the support code still needs
31 to be written. The code is changing quickly enough that we
32 haven't bothered yet.
35 /* For sparc64-cross-sparc32. */
36 #define _SYSCALL32
37 #include "bfd.h"
38 #include "sysdep.h"
39 #include "bfdlink.h"
40 #include "libbfd.h"
41 #define ARCH_SIZE 0
42 #include "elf-bfd.h"
43 #include "libiberty.h"
45 static INLINE struct elf_segment_map *make_mapping
46 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
47 static boolean map_sections_to_segments PARAMS ((bfd *));
48 static int elf_sort_sections PARAMS ((const PTR, const PTR));
49 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
50 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
51 static boolean prep_headers PARAMS ((bfd *));
52 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
53 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
54 static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type));
55 static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
56 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
57 static void set_group_contents PARAMS ((bfd *, asection *, PTR));
58 static boolean assign_section_numbers PARAMS ((bfd *));
59 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
60 static boolean elf_map_symbols PARAMS ((bfd *));
61 static bfd_size_type get_program_header_size PARAMS ((bfd *));
62 static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type));
63 static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **,
64 bfd_vma, const char **,
65 const char **));
66 static int elfcore_make_pid PARAMS ((bfd *));
67 static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *));
68 static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *,
69 Elf_Internal_Note *));
70 static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *));
71 static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *));
72 static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *));
74 /* Swap version information in and out. The version information is
75 currently size independent. If that ever changes, this code will
76 need to move into elfcode.h. */
78 /* Swap in a Verdef structure. */
80 void
81 _bfd_elf_swap_verdef_in (abfd, src, dst)
82 bfd *abfd;
83 const Elf_External_Verdef *src;
84 Elf_Internal_Verdef *dst;
86 dst->vd_version = H_GET_16 (abfd, src->vd_version);
87 dst->vd_flags = H_GET_16 (abfd, src->vd_flags);
88 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx);
89 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt);
90 dst->vd_hash = H_GET_32 (abfd, src->vd_hash);
91 dst->vd_aux = H_GET_32 (abfd, src->vd_aux);
92 dst->vd_next = H_GET_32 (abfd, src->vd_next);
95 /* Swap out a Verdef structure. */
97 void
98 _bfd_elf_swap_verdef_out (abfd, src, dst)
99 bfd *abfd;
100 const Elf_Internal_Verdef *src;
101 Elf_External_Verdef *dst;
103 H_PUT_16 (abfd, src->vd_version, dst->vd_version);
104 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags);
105 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx);
106 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt);
107 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash);
108 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux);
109 H_PUT_32 (abfd, src->vd_next, dst->vd_next);
112 /* Swap in a Verdaux structure. */
114 void
115 _bfd_elf_swap_verdaux_in (abfd, src, dst)
116 bfd *abfd;
117 const Elf_External_Verdaux *src;
118 Elf_Internal_Verdaux *dst;
120 dst->vda_name = H_GET_32 (abfd, src->vda_name);
121 dst->vda_next = H_GET_32 (abfd, src->vda_next);
124 /* Swap out a Verdaux structure. */
126 void
127 _bfd_elf_swap_verdaux_out (abfd, src, dst)
128 bfd *abfd;
129 const Elf_Internal_Verdaux *src;
130 Elf_External_Verdaux *dst;
132 H_PUT_32 (abfd, src->vda_name, dst->vda_name);
133 H_PUT_32 (abfd, src->vda_next, dst->vda_next);
136 /* Swap in a Verneed structure. */
138 void
139 _bfd_elf_swap_verneed_in (abfd, src, dst)
140 bfd *abfd;
141 const Elf_External_Verneed *src;
142 Elf_Internal_Verneed *dst;
144 dst->vn_version = H_GET_16 (abfd, src->vn_version);
145 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt);
146 dst->vn_file = H_GET_32 (abfd, src->vn_file);
147 dst->vn_aux = H_GET_32 (abfd, src->vn_aux);
148 dst->vn_next = H_GET_32 (abfd, src->vn_next);
151 /* Swap out a Verneed structure. */
153 void
154 _bfd_elf_swap_verneed_out (abfd, src, dst)
155 bfd *abfd;
156 const Elf_Internal_Verneed *src;
157 Elf_External_Verneed *dst;
159 H_PUT_16 (abfd, src->vn_version, dst->vn_version);
160 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt);
161 H_PUT_32 (abfd, src->vn_file, dst->vn_file);
162 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux);
163 H_PUT_32 (abfd, src->vn_next, dst->vn_next);
166 /* Swap in a Vernaux structure. */
168 void
169 _bfd_elf_swap_vernaux_in (abfd, src, dst)
170 bfd *abfd;
171 const Elf_External_Vernaux *src;
172 Elf_Internal_Vernaux *dst;
174 dst->vna_hash = H_GET_32 (abfd, src->vna_hash);
175 dst->vna_flags = H_GET_16 (abfd, src->vna_flags);
176 dst->vna_other = H_GET_16 (abfd, src->vna_other);
177 dst->vna_name = H_GET_32 (abfd, src->vna_name);
178 dst->vna_next = H_GET_32 (abfd, src->vna_next);
181 /* Swap out a Vernaux structure. */
183 void
184 _bfd_elf_swap_vernaux_out (abfd, src, dst)
185 bfd *abfd;
186 const Elf_Internal_Vernaux *src;
187 Elf_External_Vernaux *dst;
189 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash);
190 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags);
191 H_PUT_16 (abfd, src->vna_other, dst->vna_other);
192 H_PUT_32 (abfd, src->vna_name, dst->vna_name);
193 H_PUT_32 (abfd, src->vna_next, dst->vna_next);
196 /* Swap in a Versym structure. */
198 void
199 _bfd_elf_swap_versym_in (abfd, src, dst)
200 bfd *abfd;
201 const Elf_External_Versym *src;
202 Elf_Internal_Versym *dst;
204 dst->vs_vers = H_GET_16 (abfd, src->vs_vers);
207 /* Swap out a Versym structure. */
209 void
210 _bfd_elf_swap_versym_out (abfd, src, dst)
211 bfd *abfd;
212 const Elf_Internal_Versym *src;
213 Elf_External_Versym *dst;
215 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers);
218 /* Standard ELF hash function. Do not change this function; you will
219 cause invalid hash tables to be generated. */
221 unsigned long
222 bfd_elf_hash (namearg)
223 const char *namearg;
225 const unsigned char *name = (const unsigned char *) namearg;
226 unsigned long h = 0;
227 unsigned long g;
228 int ch;
230 while ((ch = *name++) != '\0')
232 h = (h << 4) + ch;
233 if ((g = (h & 0xf0000000)) != 0)
235 h ^= g >> 24;
236 /* The ELF ABI says `h &= ~g', but this is equivalent in
237 this case and on some machines one insn instead of two. */
238 h ^= g;
241 return h;
244 /* Read a specified number of bytes at a specified offset in an ELF
245 file, into a newly allocated buffer, and return a pointer to the
246 buffer. */
248 static char *
249 elf_read (abfd, offset, size)
250 bfd *abfd;
251 file_ptr offset;
252 bfd_size_type size;
254 char *buf;
256 if ((buf = bfd_alloc (abfd, size)) == NULL)
257 return NULL;
258 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
259 return NULL;
260 if (bfd_bread ((PTR) buf, size, abfd) != size)
262 if (bfd_get_error () != bfd_error_system_call)
263 bfd_set_error (bfd_error_file_truncated);
264 return NULL;
266 return buf;
269 boolean
270 bfd_elf_mkobject (abfd)
271 bfd *abfd;
273 /* This just does initialization. */
274 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
275 bfd_size_type amt = sizeof (struct elf_obj_tdata);
276 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt);
277 if (elf_tdata (abfd) == 0)
278 return false;
279 /* Since everything is done at close time, do we need any
280 initialization? */
282 return true;
285 boolean
286 bfd_elf_mkcorefile (abfd)
287 bfd *abfd;
289 /* I think this can be done just like an object file. */
290 return bfd_elf_mkobject (abfd);
293 char *
294 bfd_elf_get_str_section (abfd, shindex)
295 bfd *abfd;
296 unsigned int shindex;
298 Elf_Internal_Shdr **i_shdrp;
299 char *shstrtab = NULL;
300 file_ptr offset;
301 bfd_size_type shstrtabsize;
303 i_shdrp = elf_elfsections (abfd);
304 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
305 return 0;
307 shstrtab = (char *) i_shdrp[shindex]->contents;
308 if (shstrtab == NULL)
310 /* No cached one, attempt to read, and cache what we read. */
311 offset = i_shdrp[shindex]->sh_offset;
312 shstrtabsize = i_shdrp[shindex]->sh_size;
313 shstrtab = elf_read (abfd, offset, shstrtabsize);
314 i_shdrp[shindex]->contents = (PTR) shstrtab;
316 return shstrtab;
319 char *
320 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
321 bfd *abfd;
322 unsigned int shindex;
323 unsigned int strindex;
325 Elf_Internal_Shdr *hdr;
327 if (strindex == 0)
328 return "";
330 hdr = elf_elfsections (abfd)[shindex];
332 if (hdr->contents == NULL
333 && bfd_elf_get_str_section (abfd, shindex) == NULL)
334 return NULL;
336 if (strindex >= hdr->sh_size)
338 (*_bfd_error_handler)
339 (_("%s: invalid string offset %u >= %lu for section `%s'"),
340 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size,
341 ((shindex == elf_elfheader(abfd)->e_shstrndx
342 && strindex == hdr->sh_name)
343 ? ".shstrtab"
344 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
345 return "";
348 return ((char *) hdr->contents) + strindex;
351 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP
352 sections. The first element is the flags, the rest are section
353 pointers. */
355 typedef union elf_internal_group {
356 Elf_Internal_Shdr *shdr;
357 unsigned int flags;
358 } Elf_Internal_Group;
360 /* Set next_in_group list pointer, and group name for NEWSECT. */
362 static boolean
363 setup_group (abfd, hdr, newsect)
364 bfd *abfd;
365 Elf_Internal_Shdr *hdr;
366 asection *newsect;
368 unsigned int num_group = elf_tdata (abfd)->num_group;
370 /* If num_group is zero, read in all SHT_GROUP sections. The count
371 is set to -1 if there are no SHT_GROUP sections. */
372 if (num_group == 0)
374 unsigned int i, shnum;
376 /* First count the number of groups. If we have a SHT_GROUP
377 section with just a flag word (ie. sh_size is 4), ignore it. */
378 shnum = elf_elfheader (abfd)->e_shnum;
379 num_group = 0;
380 for (i = 0; i < shnum; i++)
382 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
383 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
384 num_group += 1;
387 if (num_group == 0)
388 num_group = (unsigned) -1;
389 elf_tdata (abfd)->num_group = num_group;
391 if (num_group > 0)
393 /* We keep a list of elf section headers for group sections,
394 so we can find them quickly. */
395 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *);
396 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt);
397 if (elf_tdata (abfd)->group_sect_ptr == NULL)
398 return false;
400 num_group = 0;
401 for (i = 0; i < shnum; i++)
403 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i];
404 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8)
406 unsigned char *src;
407 Elf_Internal_Group *dest;
409 /* Add to list of sections. */
410 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr;
411 num_group += 1;
413 /* Read the raw contents. */
414 BFD_ASSERT (sizeof (*dest) >= 4);
415 amt = shdr->sh_size * sizeof (*dest) / 4;
416 shdr->contents = bfd_alloc (abfd, amt);
417 if (shdr->contents == NULL
418 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0
419 || (bfd_bread (shdr->contents, shdr->sh_size, abfd)
420 != shdr->sh_size))
421 return false;
423 /* Translate raw contents, a flag word followed by an
424 array of elf section indices all in target byte order,
425 to the flag word followed by an array of elf section
426 pointers. */
427 src = shdr->contents + shdr->sh_size;
428 dest = (Elf_Internal_Group *) (shdr->contents + amt);
429 while (1)
431 unsigned int idx;
433 src -= 4;
434 --dest;
435 idx = H_GET_32 (abfd, src);
436 if (src == shdr->contents)
438 dest->flags = idx;
439 break;
441 if (idx >= shnum)
443 ((*_bfd_error_handler)
444 (_("%s: invalid SHT_GROUP entry"),
445 bfd_archive_filename (abfd)));
446 idx = 0;
448 dest->shdr = elf_elfsections (abfd)[idx];
455 if (num_group != (unsigned) -1)
457 unsigned int i;
459 for (i = 0; i < num_group; i++)
461 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i];
462 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents;
463 unsigned int n_elt = shdr->sh_size / 4;
465 /* Look through this group's sections to see if current
466 section is a member. */
467 while (--n_elt != 0)
468 if ((++idx)->shdr == hdr)
470 asection *s = NULL;
472 /* We are a member of this group. Go looking through
473 other members to see if any others are linked via
474 next_in_group. */
475 idx = (Elf_Internal_Group *) shdr->contents;
476 n_elt = shdr->sh_size / 4;
477 while (--n_elt != 0)
478 if ((s = (++idx)->shdr->bfd_section) != NULL
479 && elf_next_in_group (s) != NULL)
480 break;
481 if (n_elt != 0)
483 /* Snarf the group name from other member, and
484 insert current section in circular list. */
485 elf_group_name (newsect) = elf_group_name (s);
486 elf_next_in_group (newsect) = elf_next_in_group (s);
487 elf_next_in_group (s) = newsect;
489 else
491 struct elf_backend_data *bed;
492 file_ptr pos;
493 unsigned char ename[4];
494 unsigned long iname;
495 const char *gname;
497 /* Humbug. Get the name from the group signature
498 symbol. Why isn't the signature just a string?
499 Fortunately, the name index is at the same
500 place in the external symbol for both 32 and 64
501 bit ELF. */
502 bed = get_elf_backend_data (abfd);
503 pos = elf_tdata (abfd)->symtab_hdr.sh_offset;
504 pos += shdr->sh_info * bed->s->sizeof_sym;
505 if (bfd_seek (abfd, pos, SEEK_SET) != 0
506 || bfd_bread (ename, (bfd_size_type) 4, abfd) != 4)
507 return false;
508 iname = H_GET_32 (abfd, ename);
509 gname = elf_string_from_elf_strtab (abfd, iname);
510 elf_group_name (newsect) = gname;
512 /* Start a circular list with one element. */
513 elf_next_in_group (newsect) = newsect;
515 if (shdr->bfd_section != NULL)
516 elf_next_in_group (shdr->bfd_section) = newsect;
517 i = num_group - 1;
518 break;
523 if (elf_group_name (newsect) == NULL)
525 (*_bfd_error_handler) (_("%s: no group info for section %s"),
526 bfd_archive_filename (abfd), newsect->name);
528 return true;
531 /* Make a BFD section from an ELF section. We store a pointer to the
532 BFD section in the bfd_section field of the header. */
534 boolean
535 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
536 bfd *abfd;
537 Elf_Internal_Shdr *hdr;
538 const char *name;
540 asection *newsect;
541 flagword flags;
542 struct elf_backend_data *bed;
544 if (hdr->bfd_section != NULL)
546 BFD_ASSERT (strcmp (name,
547 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
548 return true;
551 newsect = bfd_make_section_anyway (abfd, name);
552 if (newsect == NULL)
553 return false;
555 newsect->filepos = hdr->sh_offset;
557 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
558 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
559 || ! bfd_set_section_alignment (abfd, newsect,
560 bfd_log2 ((bfd_vma) hdr->sh_addralign)))
561 return false;
563 flags = SEC_NO_FLAGS;
564 if (hdr->sh_type != SHT_NOBITS)
565 flags |= SEC_HAS_CONTENTS;
566 if (hdr->sh_type == SHT_GROUP)
567 flags |= SEC_GROUP | SEC_EXCLUDE;
568 if ((hdr->sh_flags & SHF_ALLOC) != 0)
570 flags |= SEC_ALLOC;
571 if (hdr->sh_type != SHT_NOBITS)
572 flags |= SEC_LOAD;
574 if ((hdr->sh_flags & SHF_WRITE) == 0)
575 flags |= SEC_READONLY;
576 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
577 flags |= SEC_CODE;
578 else if ((flags & SEC_LOAD) != 0)
579 flags |= SEC_DATA;
580 if ((hdr->sh_flags & SHF_MERGE) != 0)
582 flags |= SEC_MERGE;
583 newsect->entsize = hdr->sh_entsize;
584 if ((hdr->sh_flags & SHF_STRINGS) != 0)
585 flags |= SEC_STRINGS;
587 if (hdr->sh_flags & SHF_GROUP)
588 if (!setup_group (abfd, hdr, newsect))
589 return false;
591 /* The debugging sections appear to be recognized only by name, not
592 any sort of flag. */
594 static const char *debug_sec_names [] =
596 ".debug",
597 ".gnu.linkonce.wi.",
598 ".line",
599 ".stab"
601 int i;
603 for (i = ARRAY_SIZE (debug_sec_names); i--;)
604 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
605 break;
607 if (i >= 0)
608 flags |= SEC_DEBUGGING;
611 /* As a GNU extension, if the name begins with .gnu.linkonce, we
612 only link a single copy of the section. This is used to support
613 g++. g++ will emit each template expansion in its own section.
614 The symbols will be defined as weak, so that multiple definitions
615 are permitted. The GNU linker extension is to actually discard
616 all but one of the sections. */
617 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
618 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
620 bed = get_elf_backend_data (abfd);
621 if (bed->elf_backend_section_flags)
622 if (! bed->elf_backend_section_flags (&flags, hdr))
623 return false;
625 if (! bfd_set_section_flags (abfd, newsect, flags))
626 return false;
628 if ((flags & SEC_ALLOC) != 0)
630 Elf_Internal_Phdr *phdr;
631 unsigned int i;
633 /* Look through the phdrs to see if we need to adjust the lma.
634 If all the p_paddr fields are zero, we ignore them, since
635 some ELF linkers produce such output. */
636 phdr = elf_tdata (abfd)->phdr;
637 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
639 if (phdr->p_paddr != 0)
640 break;
642 if (i < elf_elfheader (abfd)->e_phnum)
644 phdr = elf_tdata (abfd)->phdr;
645 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
647 /* This section is part of this segment if its file
648 offset plus size lies within the segment's memory
649 span and, if the section is loaded, the extent of the
650 loaded data lies within the extent of the segment.
651 If the p_paddr field is not set, we don't alter the
652 LMA. */
653 if (phdr->p_type == PT_LOAD
654 && phdr->p_paddr
655 && (bfd_vma) hdr->sh_offset >= phdr->p_offset
656 && (hdr->sh_offset + hdr->sh_size
657 <= phdr->p_offset + phdr->p_memsz)
658 && ((flags & SEC_LOAD) == 0
659 || (phdr->p_offset + phdr->p_filesz
660 >= hdr->sh_offset + hdr->sh_size)))
662 /* We used to do a relative adjustment here, but
663 that doesn't work if the segment is packed with
664 code from multiple VMAs. Instead we calculate
665 the LMA absoultely, based on the LMA of the
666 segment (it is assumed that the segment will
667 contain sections with contiguous LMAs, even if
668 the VMAs are not). */
669 newsect->lma = phdr->p_paddr
670 + hdr->sh_offset - phdr->p_offset;
671 break;
677 hdr->bfd_section = newsect;
678 elf_section_data (newsect)->this_hdr = *hdr;
680 return true;
684 INTERNAL_FUNCTION
685 bfd_elf_find_section
687 SYNOPSIS
688 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
690 DESCRIPTION
691 Helper functions for GDB to locate the string tables.
692 Since BFD hides string tables from callers, GDB needs to use an
693 internal hook to find them. Sun's .stabstr, in particular,
694 isn't even pointed to by the .stab section, so ordinary
695 mechanisms wouldn't work to find it, even if we had some.
698 struct elf_internal_shdr *
699 bfd_elf_find_section (abfd, name)
700 bfd *abfd;
701 char *name;
703 Elf_Internal_Shdr **i_shdrp;
704 char *shstrtab;
705 unsigned int max;
706 unsigned int i;
708 i_shdrp = elf_elfsections (abfd);
709 if (i_shdrp != NULL)
711 shstrtab = bfd_elf_get_str_section
712 (abfd, elf_elfheader (abfd)->e_shstrndx);
713 if (shstrtab != NULL)
715 max = elf_elfheader (abfd)->e_shnum;
716 for (i = 1; i < max; i++)
717 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
718 return i_shdrp[i];
721 return 0;
724 const char *const bfd_elf_section_type_names[] = {
725 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
726 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
727 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
730 /* ELF relocs are against symbols. If we are producing relocateable
731 output, and the reloc is against an external symbol, and nothing
732 has given us any additional addend, the resulting reloc will also
733 be against the same symbol. In such a case, we don't want to
734 change anything about the way the reloc is handled, since it will
735 all be done at final link time. Rather than put special case code
736 into bfd_perform_relocation, all the reloc types use this howto
737 function. It just short circuits the reloc if producing
738 relocateable output against an external symbol. */
740 bfd_reloc_status_type
741 bfd_elf_generic_reloc (abfd,
742 reloc_entry,
743 symbol,
744 data,
745 input_section,
746 output_bfd,
747 error_message)
748 bfd *abfd ATTRIBUTE_UNUSED;
749 arelent *reloc_entry;
750 asymbol *symbol;
751 PTR data ATTRIBUTE_UNUSED;
752 asection *input_section;
753 bfd *output_bfd;
754 char **error_message ATTRIBUTE_UNUSED;
756 if (output_bfd != (bfd *) NULL
757 && (symbol->flags & BSF_SECTION_SYM) == 0
758 && (! reloc_entry->howto->partial_inplace
759 || reloc_entry->addend == 0))
761 reloc_entry->address += input_section->output_offset;
762 return bfd_reloc_ok;
765 return bfd_reloc_continue;
768 /* Finish SHF_MERGE section merging. */
770 boolean
771 _bfd_elf_merge_sections (abfd, info)
772 bfd *abfd;
773 struct bfd_link_info *info;
775 if (!is_elf_hash_table (info))
776 return false;
777 if (elf_hash_table (info)->merge_info)
778 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info);
779 return true;
782 /* Print out the program headers. */
784 boolean
785 _bfd_elf_print_private_bfd_data (abfd, farg)
786 bfd *abfd;
787 PTR farg;
789 FILE *f = (FILE *) farg;
790 Elf_Internal_Phdr *p;
791 asection *s;
792 bfd_byte *dynbuf = NULL;
794 p = elf_tdata (abfd)->phdr;
795 if (p != NULL)
797 unsigned int i, c;
799 fprintf (f, _("\nProgram Header:\n"));
800 c = elf_elfheader (abfd)->e_phnum;
801 for (i = 0; i < c; i++, p++)
803 const char *pt;
804 char buf[20];
806 switch (p->p_type)
808 case PT_NULL: pt = "NULL"; break;
809 case PT_LOAD: pt = "LOAD"; break;
810 case PT_DYNAMIC: pt = "DYNAMIC"; break;
811 case PT_INTERP: pt = "INTERP"; break;
812 case PT_NOTE: pt = "NOTE"; break;
813 case PT_SHLIB: pt = "SHLIB"; break;
814 case PT_PHDR: pt = "PHDR"; break;
815 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break;
817 fprintf (f, "%8s off 0x", pt);
818 bfd_fprintf_vma (abfd, f, p->p_offset);
819 fprintf (f, " vaddr 0x");
820 bfd_fprintf_vma (abfd, f, p->p_vaddr);
821 fprintf (f, " paddr 0x");
822 bfd_fprintf_vma (abfd, f, p->p_paddr);
823 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
824 fprintf (f, " filesz 0x");
825 bfd_fprintf_vma (abfd, f, p->p_filesz);
826 fprintf (f, " memsz 0x");
827 bfd_fprintf_vma (abfd, f, p->p_memsz);
828 fprintf (f, " flags %c%c%c",
829 (p->p_flags & PF_R) != 0 ? 'r' : '-',
830 (p->p_flags & PF_W) != 0 ? 'w' : '-',
831 (p->p_flags & PF_X) != 0 ? 'x' : '-');
832 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0)
833 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X));
834 fprintf (f, "\n");
838 s = bfd_get_section_by_name (abfd, ".dynamic");
839 if (s != NULL)
841 int elfsec;
842 unsigned long shlink;
843 bfd_byte *extdyn, *extdynend;
844 size_t extdynsize;
845 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
847 fprintf (f, _("\nDynamic Section:\n"));
849 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
850 if (dynbuf == NULL)
851 goto error_return;
852 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
853 s->_raw_size))
854 goto error_return;
856 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
857 if (elfsec == -1)
858 goto error_return;
859 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
861 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
862 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
864 extdyn = dynbuf;
865 extdynend = extdyn + s->_raw_size;
866 for (; extdyn < extdynend; extdyn += extdynsize)
868 Elf_Internal_Dyn dyn;
869 const char *name;
870 char ab[20];
871 boolean stringp;
873 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
875 if (dyn.d_tag == DT_NULL)
876 break;
878 stringp = false;
879 switch (dyn.d_tag)
881 default:
882 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
883 name = ab;
884 break;
886 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
887 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
888 case DT_PLTGOT: name = "PLTGOT"; break;
889 case DT_HASH: name = "HASH"; break;
890 case DT_STRTAB: name = "STRTAB"; break;
891 case DT_SYMTAB: name = "SYMTAB"; break;
892 case DT_RELA: name = "RELA"; break;
893 case DT_RELASZ: name = "RELASZ"; break;
894 case DT_RELAENT: name = "RELAENT"; break;
895 case DT_STRSZ: name = "STRSZ"; break;
896 case DT_SYMENT: name = "SYMENT"; break;
897 case DT_INIT: name = "INIT"; break;
898 case DT_FINI: name = "FINI"; break;
899 case DT_SONAME: name = "SONAME"; stringp = true; break;
900 case DT_RPATH: name = "RPATH"; stringp = true; break;
901 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
902 case DT_REL: name = "REL"; break;
903 case DT_RELSZ: name = "RELSZ"; break;
904 case DT_RELENT: name = "RELENT"; break;
905 case DT_PLTREL: name = "PLTREL"; break;
906 case DT_DEBUG: name = "DEBUG"; break;
907 case DT_TEXTREL: name = "TEXTREL"; break;
908 case DT_JMPREL: name = "JMPREL"; break;
909 case DT_BIND_NOW: name = "BIND_NOW"; break;
910 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
911 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
912 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
913 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
914 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
915 case DT_FLAGS: name = "FLAGS"; break;
916 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
917 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
918 case DT_CHECKSUM: name = "CHECKSUM"; break;
919 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
920 case DT_MOVEENT: name = "MOVEENT"; break;
921 case DT_MOVESZ: name = "MOVESZ"; break;
922 case DT_FEATURE: name = "FEATURE"; break;
923 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
924 case DT_SYMINSZ: name = "SYMINSZ"; break;
925 case DT_SYMINENT: name = "SYMINENT"; break;
926 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
927 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
928 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
929 case DT_PLTPAD: name = "PLTPAD"; break;
930 case DT_MOVETAB: name = "MOVETAB"; break;
931 case DT_SYMINFO: name = "SYMINFO"; break;
932 case DT_RELACOUNT: name = "RELACOUNT"; break;
933 case DT_RELCOUNT: name = "RELCOUNT"; break;
934 case DT_FLAGS_1: name = "FLAGS_1"; break;
935 case DT_VERSYM: name = "VERSYM"; break;
936 case DT_VERDEF: name = "VERDEF"; break;
937 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
938 case DT_VERNEED: name = "VERNEED"; break;
939 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
940 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
941 case DT_USED: name = "USED"; break;
942 case DT_FILTER: name = "FILTER"; stringp = true; break;
945 fprintf (f, " %-11s ", name);
946 if (! stringp)
947 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
948 else
950 const char *string;
951 unsigned int tagv = dyn.d_un.d_val;
953 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
954 if (string == NULL)
955 goto error_return;
956 fprintf (f, "%s", string);
958 fprintf (f, "\n");
961 free (dynbuf);
962 dynbuf = NULL;
965 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
966 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
968 if (! _bfd_elf_slurp_version_tables (abfd))
969 return false;
972 if (elf_dynverdef (abfd) != 0)
974 Elf_Internal_Verdef *t;
976 fprintf (f, _("\nVersion definitions:\n"));
977 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
979 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
980 t->vd_flags, t->vd_hash, t->vd_nodename);
981 if (t->vd_auxptr->vda_nextptr != NULL)
983 Elf_Internal_Verdaux *a;
985 fprintf (f, "\t");
986 for (a = t->vd_auxptr->vda_nextptr;
987 a != NULL;
988 a = a->vda_nextptr)
989 fprintf (f, "%s ", a->vda_nodename);
990 fprintf (f, "\n");
995 if (elf_dynverref (abfd) != 0)
997 Elf_Internal_Verneed *t;
999 fprintf (f, _("\nVersion References:\n"));
1000 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
1002 Elf_Internal_Vernaux *a;
1004 fprintf (f, _(" required from %s:\n"), t->vn_filename);
1005 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1006 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
1007 a->vna_flags, a->vna_other, a->vna_nodename);
1011 return true;
1013 error_return:
1014 if (dynbuf != NULL)
1015 free (dynbuf);
1016 return false;
1019 /* Display ELF-specific fields of a symbol. */
1021 void
1022 bfd_elf_print_symbol (abfd, filep, symbol, how)
1023 bfd *abfd;
1024 PTR filep;
1025 asymbol *symbol;
1026 bfd_print_symbol_type how;
1028 FILE *file = (FILE *) filep;
1029 switch (how)
1031 case bfd_print_symbol_name:
1032 fprintf (file, "%s", symbol->name);
1033 break;
1034 case bfd_print_symbol_more:
1035 fprintf (file, "elf ");
1036 bfd_fprintf_vma (abfd, file, symbol->value);
1037 fprintf (file, " %lx", (long) symbol->flags);
1038 break;
1039 case bfd_print_symbol_all:
1041 const char *section_name;
1042 const char *name = NULL;
1043 struct elf_backend_data *bed;
1044 unsigned char st_other;
1045 bfd_vma val;
1047 section_name = symbol->section ? symbol->section->name : "(*none*)";
1049 bed = get_elf_backend_data (abfd);
1050 if (bed->elf_backend_print_symbol_all)
1051 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
1053 if (name == NULL)
1055 name = symbol->name;
1056 bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
1059 fprintf (file, " %s\t", section_name);
1060 /* Print the "other" value for a symbol. For common symbols,
1061 we've already printed the size; now print the alignment.
1062 For other symbols, we have no specified alignment, and
1063 we've printed the address; now print the size. */
1064 if (bfd_is_com_section (symbol->section))
1065 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
1066 else
1067 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size;
1068 bfd_fprintf_vma (abfd, file, val);
1070 /* If we have version information, print it. */
1071 if (elf_tdata (abfd)->dynversym_section != 0
1072 && (elf_tdata (abfd)->dynverdef_section != 0
1073 || elf_tdata (abfd)->dynverref_section != 0))
1075 unsigned int vernum;
1076 const char *version_string;
1078 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
1080 if (vernum == 0)
1081 version_string = "";
1082 else if (vernum == 1)
1083 version_string = "Base";
1084 else if (vernum <= elf_tdata (abfd)->cverdefs)
1085 version_string =
1086 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
1087 else
1089 Elf_Internal_Verneed *t;
1091 version_string = "";
1092 for (t = elf_tdata (abfd)->verref;
1093 t != NULL;
1094 t = t->vn_nextref)
1096 Elf_Internal_Vernaux *a;
1098 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
1100 if (a->vna_other == vernum)
1102 version_string = a->vna_nodename;
1103 break;
1109 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
1110 fprintf (file, " %-11s", version_string);
1111 else
1113 int i;
1115 fprintf (file, " (%s)", version_string);
1116 for (i = 10 - strlen (version_string); i > 0; --i)
1117 putc (' ', file);
1121 /* If the st_other field is not zero, print it. */
1122 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
1124 switch (st_other)
1126 case 0: break;
1127 case STV_INTERNAL: fprintf (file, " .internal"); break;
1128 case STV_HIDDEN: fprintf (file, " .hidden"); break;
1129 case STV_PROTECTED: fprintf (file, " .protected"); break;
1130 default:
1131 /* Some other non-defined flags are also present, so print
1132 everything hex. */
1133 fprintf (file, " 0x%02x", (unsigned int) st_other);
1136 fprintf (file, " %s", name);
1138 break;
1142 /* Create an entry in an ELF linker hash table. */
1144 struct bfd_hash_entry *
1145 _bfd_elf_link_hash_newfunc (entry, table, string)
1146 struct bfd_hash_entry *entry;
1147 struct bfd_hash_table *table;
1148 const char *string;
1150 /* Allocate the structure if it has not already been allocated by a
1151 subclass. */
1152 if (entry == NULL)
1154 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry));
1155 if (entry == NULL)
1156 return entry;
1159 /* Call the allocation method of the superclass. */
1160 entry = _bfd_link_hash_newfunc (entry, table, string);
1161 if (entry != NULL)
1163 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
1164 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table;
1166 /* Set local fields. */
1167 ret->indx = -1;
1168 ret->size = 0;
1169 ret->dynindx = -1;
1170 ret->dynstr_index = 0;
1171 ret->weakdef = NULL;
1172 ret->got.refcount = htab->init_refcount;
1173 ret->plt.refcount = htab->init_refcount;
1174 ret->linker_section_pointer = NULL;
1175 ret->verinfo.verdef = NULL;
1176 ret->vtable_entries_used = NULL;
1177 ret->vtable_entries_size = 0;
1178 ret->vtable_parent = NULL;
1179 ret->type = STT_NOTYPE;
1180 ret->other = 0;
1181 /* Assume that we have been called by a non-ELF symbol reader.
1182 This flag is then reset by the code which reads an ELF input
1183 file. This ensures that a symbol created by a non-ELF symbol
1184 reader will have the flag set correctly. */
1185 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
1188 return entry;
1191 /* Copy data from an indirect symbol to its direct symbol, hiding the
1192 old indirect symbol. Also used for copying flags to a weakdef. */
1194 void
1195 _bfd_elf_link_hash_copy_indirect (dir, ind)
1196 struct elf_link_hash_entry *dir, *ind;
1198 bfd_signed_vma tmp;
1200 /* Copy down any references that we may have already seen to the
1201 symbol which just became indirect. */
1203 dir->elf_link_hash_flags |=
1204 (ind->elf_link_hash_flags
1205 & (ELF_LINK_HASH_REF_DYNAMIC
1206 | ELF_LINK_HASH_REF_REGULAR
1207 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
1208 | ELF_LINK_NON_GOT_REF));
1210 if (ind->root.type != bfd_link_hash_indirect)
1211 return;
1213 /* Copy over the global and procedure linkage table refcount entries.
1214 These may have been already set up by a check_relocs routine. */
1215 tmp = dir->got.refcount;
1216 if (tmp <= 0)
1218 dir->got.refcount = ind->got.refcount;
1219 ind->got.refcount = tmp;
1221 else
1222 BFD_ASSERT (ind->got.refcount <= 0);
1224 tmp = dir->plt.refcount;
1225 if (tmp <= 0)
1227 dir->plt.refcount = ind->plt.refcount;
1228 ind->plt.refcount = tmp;
1230 else
1231 BFD_ASSERT (ind->plt.refcount <= 0);
1233 if (dir->dynindx == -1)
1235 dir->dynindx = ind->dynindx;
1236 dir->dynstr_index = ind->dynstr_index;
1237 ind->dynindx = -1;
1238 ind->dynstr_index = 0;
1240 else
1241 BFD_ASSERT (ind->dynindx == -1);
1244 void
1245 _bfd_elf_link_hash_hide_symbol (info, h)
1246 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1247 struct elf_link_hash_entry *h;
1249 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1250 h->plt.offset = (bfd_vma) -1;
1251 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1252 h->dynindx = -1;
1255 /* Initialize an ELF linker hash table. */
1257 boolean
1258 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1259 struct elf_link_hash_table *table;
1260 bfd *abfd;
1261 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1262 struct bfd_hash_table *,
1263 const char *));
1265 boolean ret;
1267 table->dynamic_sections_created = false;
1268 table->dynobj = NULL;
1269 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1;
1270 /* The first dynamic symbol is a dummy. */
1271 table->dynsymcount = 1;
1272 table->dynstr = NULL;
1273 table->bucketcount = 0;
1274 table->needed = NULL;
1275 table->runpath = NULL;
1276 table->hgot = NULL;
1277 table->stab_info = NULL;
1278 table->merge_info = NULL;
1279 table->dynlocal = NULL;
1280 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc);
1281 table->root.type = bfd_link_elf_hash_table;
1283 return ret;
1286 /* Create an ELF linker hash table. */
1288 struct bfd_link_hash_table *
1289 _bfd_elf_link_hash_table_create (abfd)
1290 bfd *abfd;
1292 struct elf_link_hash_table *ret;
1293 bfd_size_type amt = sizeof (struct elf_link_hash_table);
1295 ret = (struct elf_link_hash_table *) bfd_alloc (abfd, amt);
1296 if (ret == (struct elf_link_hash_table *) NULL)
1297 return NULL;
1299 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1301 bfd_release (abfd, ret);
1302 return NULL;
1305 return &ret->root;
1308 /* This is a hook for the ELF emulation code in the generic linker to
1309 tell the backend linker what file name to use for the DT_NEEDED
1310 entry for a dynamic object. The generic linker passes name as an
1311 empty string to indicate that no DT_NEEDED entry should be made. */
1313 void
1314 bfd_elf_set_dt_needed_name (abfd, name)
1315 bfd *abfd;
1316 const char *name;
1318 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1319 && bfd_get_format (abfd) == bfd_object)
1320 elf_dt_name (abfd) = name;
1323 void
1324 bfd_elf_set_dt_needed_soname (abfd, name)
1325 bfd *abfd;
1326 const char *name;
1328 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1329 && bfd_get_format (abfd) == bfd_object)
1330 elf_dt_soname (abfd) = name;
1333 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1334 the linker ELF emulation code. */
1336 struct bfd_link_needed_list *
1337 bfd_elf_get_needed_list (abfd, info)
1338 bfd *abfd ATTRIBUTE_UNUSED;
1339 struct bfd_link_info *info;
1341 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1342 return NULL;
1343 return elf_hash_table (info)->needed;
1346 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1347 hook for the linker ELF emulation code. */
1349 struct bfd_link_needed_list *
1350 bfd_elf_get_runpath_list (abfd, info)
1351 bfd *abfd ATTRIBUTE_UNUSED;
1352 struct bfd_link_info *info;
1354 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1355 return NULL;
1356 return elf_hash_table (info)->runpath;
1359 /* Get the name actually used for a dynamic object for a link. This
1360 is the SONAME entry if there is one. Otherwise, it is the string
1361 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1363 const char *
1364 bfd_elf_get_dt_soname (abfd)
1365 bfd *abfd;
1367 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1368 && bfd_get_format (abfd) == bfd_object)
1369 return elf_dt_name (abfd);
1370 return NULL;
1373 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1374 the ELF linker emulation code. */
1376 boolean
1377 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1378 bfd *abfd;
1379 struct bfd_link_needed_list **pneeded;
1381 asection *s;
1382 bfd_byte *dynbuf = NULL;
1383 int elfsec;
1384 unsigned long shlink;
1385 bfd_byte *extdyn, *extdynend;
1386 size_t extdynsize;
1387 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1389 *pneeded = NULL;
1391 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1392 || bfd_get_format (abfd) != bfd_object)
1393 return true;
1395 s = bfd_get_section_by_name (abfd, ".dynamic");
1396 if (s == NULL || s->_raw_size == 0)
1397 return true;
1399 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1400 if (dynbuf == NULL)
1401 goto error_return;
1403 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1404 s->_raw_size))
1405 goto error_return;
1407 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1408 if (elfsec == -1)
1409 goto error_return;
1411 shlink = elf_elfsections (abfd)[elfsec]->sh_link;
1413 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1414 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1416 extdyn = dynbuf;
1417 extdynend = extdyn + s->_raw_size;
1418 for (; extdyn < extdynend; extdyn += extdynsize)
1420 Elf_Internal_Dyn dyn;
1422 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1424 if (dyn.d_tag == DT_NULL)
1425 break;
1427 if (dyn.d_tag == DT_NEEDED)
1429 const char *string;
1430 struct bfd_link_needed_list *l;
1431 unsigned int tagv = dyn.d_un.d_val;
1432 bfd_size_type amt;
1434 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv);
1435 if (string == NULL)
1436 goto error_return;
1438 amt = sizeof *l;
1439 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt);
1440 if (l == NULL)
1441 goto error_return;
1443 l->by = abfd;
1444 l->name = string;
1445 l->next = *pneeded;
1446 *pneeded = l;
1450 free (dynbuf);
1452 return true;
1454 error_return:
1455 if (dynbuf != NULL)
1456 free (dynbuf);
1457 return false;
1460 /* Allocate an ELF string table--force the first byte to be zero. */
1462 struct bfd_strtab_hash *
1463 _bfd_elf_stringtab_init ()
1465 struct bfd_strtab_hash *ret;
1467 ret = _bfd_stringtab_init ();
1468 if (ret != NULL)
1470 bfd_size_type loc;
1472 loc = _bfd_stringtab_add (ret, "", true, false);
1473 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1474 if (loc == (bfd_size_type) -1)
1476 _bfd_stringtab_free (ret);
1477 ret = NULL;
1480 return ret;
1483 /* ELF .o/exec file reading */
1485 /* Create a new bfd section from an ELF section header. */
1487 boolean
1488 bfd_section_from_shdr (abfd, shindex)
1489 bfd *abfd;
1490 unsigned int shindex;
1492 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1493 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1494 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1495 char *name;
1497 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1499 switch (hdr->sh_type)
1501 case SHT_NULL:
1502 /* Inactive section. Throw it away. */
1503 return true;
1505 case SHT_PROGBITS: /* Normal section with contents. */
1506 case SHT_DYNAMIC: /* Dynamic linking information. */
1507 case SHT_NOBITS: /* .bss section. */
1508 case SHT_HASH: /* .hash section. */
1509 case SHT_NOTE: /* .note section. */
1510 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1512 case SHT_SYMTAB: /* A symbol table */
1513 if (elf_onesymtab (abfd) == shindex)
1514 return true;
1516 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1517 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1518 elf_onesymtab (abfd) = shindex;
1519 elf_tdata (abfd)->symtab_hdr = *hdr;
1520 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1521 abfd->flags |= HAS_SYMS;
1523 /* Sometimes a shared object will map in the symbol table. If
1524 SHF_ALLOC is set, and this is a shared object, then we also
1525 treat this section as a BFD section. We can not base the
1526 decision purely on SHF_ALLOC, because that flag is sometimes
1527 set in a relocateable object file, which would confuse the
1528 linker. */
1529 if ((hdr->sh_flags & SHF_ALLOC) != 0
1530 && (abfd->flags & DYNAMIC) != 0
1531 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1532 return false;
1534 return true;
1536 case SHT_DYNSYM: /* A dynamic symbol table */
1537 if (elf_dynsymtab (abfd) == shindex)
1538 return true;
1540 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1541 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1542 elf_dynsymtab (abfd) = shindex;
1543 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1544 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1545 abfd->flags |= HAS_SYMS;
1547 /* Besides being a symbol table, we also treat this as a regular
1548 section, so that objcopy can handle it. */
1549 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1551 case SHT_STRTAB: /* A string table */
1552 if (hdr->bfd_section != NULL)
1553 return true;
1554 if (ehdr->e_shstrndx == shindex)
1556 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1557 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1558 return true;
1561 unsigned int i;
1563 for (i = 1; i < ehdr->e_shnum; i++)
1565 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1566 if (hdr2->sh_link == shindex)
1568 if (! bfd_section_from_shdr (abfd, i))
1569 return false;
1570 if (elf_onesymtab (abfd) == i)
1572 elf_tdata (abfd)->strtab_hdr = *hdr;
1573 elf_elfsections (abfd)[shindex] =
1574 &elf_tdata (abfd)->strtab_hdr;
1575 return true;
1577 if (elf_dynsymtab (abfd) == i)
1579 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1580 elf_elfsections (abfd)[shindex] = hdr =
1581 &elf_tdata (abfd)->dynstrtab_hdr;
1582 /* We also treat this as a regular section, so
1583 that objcopy can handle it. */
1584 break;
1586 #if 0 /* Not handling other string tables specially right now. */
1587 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1588 /* We have a strtab for some random other section. */
1589 newsect = (asection *) hdr2->bfd_section;
1590 if (!newsect)
1591 break;
1592 hdr->bfd_section = newsect;
1593 hdr2 = &elf_section_data (newsect)->str_hdr;
1594 *hdr2 = *hdr;
1595 elf_elfsections (abfd)[shindex] = hdr2;
1596 #endif
1601 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1603 case SHT_REL:
1604 case SHT_RELA:
1605 /* *These* do a lot of work -- but build no sections! */
1607 asection *target_sect;
1608 Elf_Internal_Shdr *hdr2;
1610 /* Check for a bogus link to avoid crashing. */
1611 if (hdr->sh_link >= ehdr->e_shnum)
1613 ((*_bfd_error_handler)
1614 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1615 bfd_archive_filename (abfd), hdr->sh_link, name, shindex));
1616 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1619 /* For some incomprehensible reason Oracle distributes
1620 libraries for Solaris in which some of the objects have
1621 bogus sh_link fields. It would be nice if we could just
1622 reject them, but, unfortunately, some people need to use
1623 them. We scan through the section headers; if we find only
1624 one suitable symbol table, we clobber the sh_link to point
1625 to it. I hope this doesn't break anything. */
1626 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1627 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1629 int scan;
1630 int found;
1632 found = 0;
1633 for (scan = 1; scan < ehdr->e_shnum; scan++)
1635 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1636 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1638 if (found != 0)
1640 found = 0;
1641 break;
1643 found = scan;
1646 if (found != 0)
1647 hdr->sh_link = found;
1650 /* Get the symbol table. */
1651 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1652 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1653 return false;
1655 /* If this reloc section does not use the main symbol table we
1656 don't treat it as a reloc section. BFD can't adequately
1657 represent such a section, so at least for now, we don't
1658 try. We just present it as a normal section. We also
1659 can't use it as a reloc section if it points to the null
1660 section. */
1661 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1662 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1664 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1665 return false;
1666 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1667 if (target_sect == NULL)
1668 return false;
1670 if ((target_sect->flags & SEC_RELOC) == 0
1671 || target_sect->reloc_count == 0)
1672 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1673 else
1675 bfd_size_type amt;
1676 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1677 amt = sizeof (*hdr2);
1678 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
1679 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1681 *hdr2 = *hdr;
1682 elf_elfsections (abfd)[shindex] = hdr2;
1683 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1684 target_sect->flags |= SEC_RELOC;
1685 target_sect->relocation = NULL;
1686 target_sect->rel_filepos = hdr->sh_offset;
1687 /* In the section to which the relocations apply, mark whether
1688 its relocations are of the REL or RELA variety. */
1689 if (hdr->sh_size != 0)
1690 elf_section_data (target_sect)->use_rela_p
1691 = (hdr->sh_type == SHT_RELA);
1692 abfd->flags |= HAS_RELOC;
1693 return true;
1695 break;
1697 case SHT_GNU_verdef:
1698 elf_dynverdef (abfd) = shindex;
1699 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1700 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1701 break;
1703 case SHT_GNU_versym:
1704 elf_dynversym (abfd) = shindex;
1705 elf_tdata (abfd)->dynversym_hdr = *hdr;
1706 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1707 break;
1709 case SHT_GNU_verneed:
1710 elf_dynverref (abfd) = shindex;
1711 elf_tdata (abfd)->dynverref_hdr = *hdr;
1712 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1713 break;
1715 case SHT_SHLIB:
1716 return true;
1718 case SHT_GROUP:
1719 /* Make a section for objcopy and relocatable links. */
1720 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name))
1721 return false;
1722 if (hdr->contents != NULL)
1724 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents;
1725 unsigned int n_elt = hdr->sh_size / 4;
1726 asection *s;
1728 while (--n_elt != 0)
1729 if ((s = (++idx)->shdr->bfd_section) != NULL
1730 && elf_next_in_group (s) != NULL)
1732 elf_next_in_group (hdr->bfd_section) = s;
1733 break;
1736 break;
1738 default:
1739 /* Check for any processor-specific section types. */
1741 if (bed->elf_backend_section_from_shdr)
1742 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1744 break;
1747 return true;
1750 /* Return the section for the local symbol specified by ABFD, R_SYMNDX.
1751 Return SEC for sections that have no elf section, and NULL on error. */
1753 asection *
1754 bfd_section_from_r_symndx (abfd, cache, sec, r_symndx)
1755 bfd *abfd;
1756 struct sym_sec_cache *cache;
1757 asection *sec;
1758 unsigned long r_symndx;
1760 unsigned char esym_shndx[2];
1761 unsigned int isym_shndx;
1762 Elf_Internal_Shdr *symtab_hdr;
1763 file_ptr pos;
1764 bfd_size_type amt;
1765 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE;
1767 if (cache->abfd == abfd && cache->indx[ent] == r_symndx)
1768 return cache->sec[ent];
1770 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1771 pos = symtab_hdr->sh_offset;
1772 if (get_elf_backend_data (abfd)->s->sizeof_sym
1773 == sizeof (Elf64_External_Sym))
1775 pos += r_symndx * sizeof (Elf64_External_Sym);
1776 pos += offsetof (Elf64_External_Sym, st_shndx);
1778 else
1780 pos += r_symndx * sizeof (Elf32_External_Sym);
1781 pos += offsetof (Elf32_External_Sym, st_shndx);
1783 amt = sizeof (esym_shndx);
1784 if (bfd_seek (abfd, pos, SEEK_SET) != 0
1785 || bfd_bread ((PTR) esym_shndx, amt, abfd) != amt)
1786 return NULL;
1787 isym_shndx = H_GET_16 (abfd, esym_shndx);
1789 if (cache->abfd != abfd)
1791 memset (cache->indx, -1, sizeof (cache->indx));
1792 cache->abfd = abfd;
1794 cache->indx[ent] = r_symndx;
1795 cache->sec[ent] = sec;
1796 if (isym_shndx > 0 && isym_shndx < SHN_LORESERVE)
1798 asection *s;
1799 s = bfd_section_from_elf_index (abfd, isym_shndx);
1800 if (s != NULL)
1801 cache->sec[ent] = s;
1803 return cache->sec[ent];
1806 /* Given an ELF section number, retrieve the corresponding BFD
1807 section. */
1809 asection *
1810 bfd_section_from_elf_index (abfd, index)
1811 bfd *abfd;
1812 unsigned int index;
1814 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1815 if (index >= elf_elfheader (abfd)->e_shnum)
1816 return NULL;
1817 return elf_elfsections (abfd)[index]->bfd_section;
1820 boolean
1821 _bfd_elf_new_section_hook (abfd, sec)
1822 bfd *abfd;
1823 asection *sec;
1825 struct bfd_elf_section_data *sdata;
1826 bfd_size_type amt = sizeof (*sdata);
1828 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt);
1829 if (!sdata)
1830 return false;
1831 sec->used_by_bfd = (PTR) sdata;
1833 /* Indicate whether or not this section should use RELA relocations. */
1834 sdata->use_rela_p
1835 = get_elf_backend_data (abfd)->default_use_rela_p;
1837 return true;
1840 /* Create a new bfd section from an ELF program header.
1842 Since program segments have no names, we generate a synthetic name
1843 of the form segment<NUM>, where NUM is generally the index in the
1844 program header table. For segments that are split (see below) we
1845 generate the names segment<NUM>a and segment<NUM>b.
1847 Note that some program segments may have a file size that is different than
1848 (less than) the memory size. All this means is that at execution the
1849 system must allocate the amount of memory specified by the memory size,
1850 but only initialize it with the first "file size" bytes read from the
1851 file. This would occur for example, with program segments consisting
1852 of combined data+bss.
1854 To handle the above situation, this routine generates TWO bfd sections
1855 for the single program segment. The first has the length specified by
1856 the file size of the segment, and the second has the length specified
1857 by the difference between the two sizes. In effect, the segment is split
1858 into it's initialized and uninitialized parts.
1862 boolean
1863 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1864 bfd *abfd;
1865 Elf_Internal_Phdr *hdr;
1866 int index;
1867 const char *typename;
1869 asection *newsect;
1870 char *name;
1871 char namebuf[64];
1872 int split;
1874 split = ((hdr->p_memsz > 0)
1875 && (hdr->p_filesz > 0)
1876 && (hdr->p_memsz > hdr->p_filesz));
1877 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1878 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1879 if (!name)
1880 return false;
1881 strcpy (name, namebuf);
1882 newsect = bfd_make_section (abfd, name);
1883 if (newsect == NULL)
1884 return false;
1885 newsect->vma = hdr->p_vaddr;
1886 newsect->lma = hdr->p_paddr;
1887 newsect->_raw_size = hdr->p_filesz;
1888 newsect->filepos = hdr->p_offset;
1889 newsect->flags |= SEC_HAS_CONTENTS;
1890 if (hdr->p_type == PT_LOAD)
1892 newsect->flags |= SEC_ALLOC;
1893 newsect->flags |= SEC_LOAD;
1894 if (hdr->p_flags & PF_X)
1896 /* FIXME: all we known is that it has execute PERMISSION,
1897 may be data. */
1898 newsect->flags |= SEC_CODE;
1901 if (!(hdr->p_flags & PF_W))
1903 newsect->flags |= SEC_READONLY;
1906 if (split)
1908 sprintf (namebuf, "%s%db", typename, index);
1909 name = bfd_alloc (abfd, (bfd_size_type) strlen (namebuf) + 1);
1910 if (!name)
1911 return false;
1912 strcpy (name, namebuf);
1913 newsect = bfd_make_section (abfd, name);
1914 if (newsect == NULL)
1915 return false;
1916 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1917 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1918 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1919 if (hdr->p_type == PT_LOAD)
1921 newsect->flags |= SEC_ALLOC;
1922 if (hdr->p_flags & PF_X)
1923 newsect->flags |= SEC_CODE;
1925 if (!(hdr->p_flags & PF_W))
1926 newsect->flags |= SEC_READONLY;
1929 return true;
1932 boolean
1933 bfd_section_from_phdr (abfd, hdr, index)
1934 bfd *abfd;
1935 Elf_Internal_Phdr *hdr;
1936 int index;
1938 struct elf_backend_data *bed;
1940 switch (hdr->p_type)
1942 case PT_NULL:
1943 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1945 case PT_LOAD:
1946 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1948 case PT_DYNAMIC:
1949 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1951 case PT_INTERP:
1952 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1954 case PT_NOTE:
1955 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1956 return false;
1957 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz))
1958 return false;
1959 return true;
1961 case PT_SHLIB:
1962 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1964 case PT_PHDR:
1965 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1967 default:
1968 /* Check for any processor-specific program segment types.
1969 If no handler for them, default to making "segment" sections. */
1970 bed = get_elf_backend_data (abfd);
1971 if (bed->elf_backend_section_from_phdr)
1972 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1973 else
1974 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1978 /* Initialize REL_HDR, the section-header for new section, containing
1979 relocations against ASECT. If USE_RELA_P is true, we use RELA
1980 relocations; otherwise, we use REL relocations. */
1982 boolean
1983 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1984 bfd *abfd;
1985 Elf_Internal_Shdr *rel_hdr;
1986 asection *asect;
1987 boolean use_rela_p;
1989 char *name;
1990 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1991 bfd_size_type amt = sizeof ".rela" + strlen (asect->name);
1993 name = bfd_alloc (abfd, amt);
1994 if (name == NULL)
1995 return false;
1996 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1997 rel_hdr->sh_name =
1998 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name,
1999 false);
2000 if (rel_hdr->sh_name == (unsigned int) -1)
2001 return false;
2002 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
2003 rel_hdr->sh_entsize = (use_rela_p
2004 ? bed->s->sizeof_rela
2005 : bed->s->sizeof_rel);
2006 rel_hdr->sh_addralign = bed->s->file_align;
2007 rel_hdr->sh_flags = 0;
2008 rel_hdr->sh_addr = 0;
2009 rel_hdr->sh_size = 0;
2010 rel_hdr->sh_offset = 0;
2012 return true;
2015 /* Set up an ELF internal section header for a section. */
2017 static void
2018 elf_fake_sections (abfd, asect, failedptrarg)
2019 bfd *abfd;
2020 asection *asect;
2021 PTR failedptrarg;
2023 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2024 boolean *failedptr = (boolean *) failedptrarg;
2025 Elf_Internal_Shdr *this_hdr;
2027 if (*failedptr)
2029 /* We already failed; just get out of the bfd_map_over_sections
2030 loop. */
2031 return;
2034 this_hdr = &elf_section_data (asect)->this_hdr;
2036 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd),
2037 asect->name, false);
2038 if (this_hdr->sh_name == (unsigned long) -1)
2040 *failedptr = true;
2041 return;
2044 this_hdr->sh_flags = 0;
2046 if ((asect->flags & SEC_ALLOC) != 0
2047 || asect->user_set_vma)
2048 this_hdr->sh_addr = asect->vma;
2049 else
2050 this_hdr->sh_addr = 0;
2052 this_hdr->sh_offset = 0;
2053 this_hdr->sh_size = asect->_raw_size;
2054 this_hdr->sh_link = 0;
2055 this_hdr->sh_addralign = 1 << asect->alignment_power;
2056 /* The sh_entsize and sh_info fields may have been set already by
2057 copy_private_section_data. */
2059 this_hdr->bfd_section = asect;
2060 this_hdr->contents = NULL;
2062 /* FIXME: This should not be based on section names. */
2063 if (strcmp (asect->name, ".dynstr") == 0)
2064 this_hdr->sh_type = SHT_STRTAB;
2065 else if (strcmp (asect->name, ".hash") == 0)
2067 this_hdr->sh_type = SHT_HASH;
2068 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
2070 else if (strcmp (asect->name, ".dynsym") == 0)
2072 this_hdr->sh_type = SHT_DYNSYM;
2073 this_hdr->sh_entsize = bed->s->sizeof_sym;
2075 else if (strcmp (asect->name, ".dynamic") == 0)
2077 this_hdr->sh_type = SHT_DYNAMIC;
2078 this_hdr->sh_entsize = bed->s->sizeof_dyn;
2080 else if (strncmp (asect->name, ".rela", 5) == 0
2081 && get_elf_backend_data (abfd)->may_use_rela_p)
2083 this_hdr->sh_type = SHT_RELA;
2084 this_hdr->sh_entsize = bed->s->sizeof_rela;
2086 else if (strncmp (asect->name, ".rel", 4) == 0
2087 && get_elf_backend_data (abfd)->may_use_rel_p)
2089 this_hdr->sh_type = SHT_REL;
2090 this_hdr->sh_entsize = bed->s->sizeof_rel;
2092 else if (strncmp (asect->name, ".note", 5) == 0)
2093 this_hdr->sh_type = SHT_NOTE;
2094 else if (strncmp (asect->name, ".stab", 5) == 0
2095 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
2096 this_hdr->sh_type = SHT_STRTAB;
2097 else if (strcmp (asect->name, ".gnu.version") == 0)
2099 this_hdr->sh_type = SHT_GNU_versym;
2100 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
2102 else if (strcmp (asect->name, ".gnu.version_d") == 0)
2104 this_hdr->sh_type = SHT_GNU_verdef;
2105 this_hdr->sh_entsize = 0;
2106 /* objcopy or strip will copy over sh_info, but may not set
2107 cverdefs. The linker will set cverdefs, but sh_info will be
2108 zero. */
2109 if (this_hdr->sh_info == 0)
2110 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
2111 else
2112 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
2113 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
2115 else if (strcmp (asect->name, ".gnu.version_r") == 0)
2117 this_hdr->sh_type = SHT_GNU_verneed;
2118 this_hdr->sh_entsize = 0;
2119 /* objcopy or strip will copy over sh_info, but may not set
2120 cverrefs. The linker will set cverrefs, but sh_info will be
2121 zero. */
2122 if (this_hdr->sh_info == 0)
2123 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
2124 else
2125 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
2126 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
2128 else if ((asect->flags & SEC_GROUP) != 0)
2130 this_hdr->sh_type = SHT_GROUP;
2131 this_hdr->sh_entsize = 4;
2133 else if ((asect->flags & SEC_ALLOC) != 0
2134 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
2135 this_hdr->sh_type = SHT_NOBITS;
2136 else
2137 this_hdr->sh_type = SHT_PROGBITS;
2139 if ((asect->flags & SEC_ALLOC) != 0)
2140 this_hdr->sh_flags |= SHF_ALLOC;
2141 if ((asect->flags & SEC_READONLY) == 0)
2142 this_hdr->sh_flags |= SHF_WRITE;
2143 if ((asect->flags & SEC_CODE) != 0)
2144 this_hdr->sh_flags |= SHF_EXECINSTR;
2145 if ((asect->flags & SEC_MERGE) != 0)
2147 this_hdr->sh_flags |= SHF_MERGE;
2148 this_hdr->sh_entsize = asect->entsize;
2149 if ((asect->flags & SEC_STRINGS) != 0)
2150 this_hdr->sh_flags |= SHF_STRINGS;
2152 if (elf_group_name (asect) != NULL)
2153 this_hdr->sh_flags |= SHF_GROUP;
2155 /* Check for processor-specific section types. */
2156 if (bed->elf_backend_fake_sections)
2157 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
2159 /* If the section has relocs, set up a section header for the
2160 SHT_REL[A] section. If two relocation sections are required for
2161 this section, it is up to the processor-specific back-end to
2162 create the other. */
2163 if ((asect->flags & SEC_RELOC) != 0
2164 && !_bfd_elf_init_reloc_shdr (abfd,
2165 &elf_section_data (asect)->rel_hdr,
2166 asect,
2167 elf_section_data (asect)->use_rela_p))
2168 *failedptr = true;
2171 /* Fill in the contents of a SHT_GROUP section. */
2173 static void
2174 set_group_contents (abfd, sec, failedptrarg)
2175 bfd *abfd;
2176 asection *sec;
2177 PTR failedptrarg ATTRIBUTE_UNUSED;
2179 boolean *failedptr = (boolean *) failedptrarg;
2180 unsigned long symindx;
2181 asection *elt;
2182 unsigned char *loc;
2183 struct bfd_link_order *l;
2185 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP
2186 || *failedptr)
2187 return;
2189 /* If called from the assembler, swap_out_syms will have set up
2190 elf_section_syms; If called for "ld -r", the symbols won't yet
2191 be mapped, so emulate elf_bfd_final_link. */
2192 if (elf_section_syms (abfd) != NULL)
2193 symindx = elf_section_syms (abfd)[sec->index]->udata.i;
2194 else
2195 symindx = elf_section_data (sec)->this_idx;
2196 elf_section_data (sec)->this_hdr.sh_info = symindx;
2198 /* Nor will the contents be allocated for "ld -r". */
2199 if (sec->contents == NULL)
2201 sec->contents = bfd_alloc (abfd, sec->_raw_size);
2202 if (sec->contents == NULL)
2204 *failedptr = true;
2205 return;
2209 loc = sec->contents + sec->_raw_size;
2211 /* Get the pointer to the first section in the group that we
2212 squirreled away here. */
2213 elt = elf_next_in_group (sec);
2215 /* First element is a flag word. Rest of section is elf section
2216 indices for all the sections of the group. Write them backwards
2217 just to keep the group in the same order as given in .section
2218 directives, not that it matters. */
2219 while (elt != NULL)
2221 loc -= 4;
2222 H_PUT_32 (abfd, elf_section_data (elt)->this_idx, loc);
2223 elt = elf_next_in_group (elt);
2226 /* If this is a relocatable link, then the above did nothing because
2227 SEC is the output section. Look through the input sections
2228 instead. */
2229 for (l = sec->link_order_head; l != NULL; l = l->next)
2230 if (l->type == bfd_indirect_link_order
2231 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL)
2234 loc -= 4;
2235 H_PUT_32 (abfd,
2236 elf_section_data (elt->output_section)->this_idx, loc);
2237 elt = elf_next_in_group (elt);
2238 /* During a relocatable link, the lists are circular. */
2240 while (elt != elf_next_in_group (l->u.indirect.section));
2242 loc -= 4;
2243 H_PUT_32 (abfd, 0, loc);
2245 BFD_ASSERT (loc == sec->contents);
2248 /* Assign all ELF section numbers. The dummy first section is handled here
2249 too. The link/info pointers for the standard section types are filled
2250 in here too, while we're at it. */
2252 static boolean
2253 assign_section_numbers (abfd)
2254 bfd *abfd;
2256 struct elf_obj_tdata *t = elf_tdata (abfd);
2257 asection *sec;
2258 unsigned int section_number, secn;
2259 Elf_Internal_Shdr **i_shdrp;
2260 bfd_size_type amt;
2262 section_number = 1;
2264 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd));
2266 for (sec = abfd->sections; sec; sec = sec->next)
2268 struct bfd_elf_section_data *d = elf_section_data (sec);
2270 d->this_idx = section_number++;
2271 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name);
2272 if ((sec->flags & SEC_RELOC) == 0)
2273 d->rel_idx = 0;
2274 else
2276 d->rel_idx = section_number++;
2277 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name);
2280 if (d->rel_hdr2)
2282 d->rel_idx2 = section_number++;
2283 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name);
2285 else
2286 d->rel_idx2 = 0;
2289 t->shstrtab_section = section_number++;
2290 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name);
2291 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
2293 if (bfd_get_symcount (abfd) > 0)
2295 t->symtab_section = section_number++;
2296 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name);
2297 t->strtab_section = section_number++;
2298 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name);
2301 _bfd_elf_strtab_finalize (elf_shstrtab (abfd));
2302 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2303 elf_elfheader (abfd)->e_shnum = section_number;
2305 /* Set up the list of section header pointers, in agreement with the
2306 indices. */
2307 amt = section_number * sizeof (Elf_Internal_Shdr *);
2308 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
2309 if (i_shdrp == NULL)
2310 return false;
2312 amt = sizeof (Elf_Internal_Shdr);
2313 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
2314 if (i_shdrp[0] == NULL)
2316 bfd_release (abfd, i_shdrp);
2317 return false;
2319 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
2321 elf_elfsections (abfd) = i_shdrp;
2323 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
2324 if (bfd_get_symcount (abfd) > 0)
2326 i_shdrp[t->symtab_section] = &t->symtab_hdr;
2327 i_shdrp[t->strtab_section] = &t->strtab_hdr;
2328 t->symtab_hdr.sh_link = t->strtab_section;
2330 for (sec = abfd->sections; sec; sec = sec->next)
2332 struct bfd_elf_section_data *d = elf_section_data (sec);
2333 asection *s;
2334 const char *name;
2336 i_shdrp[d->this_idx] = &d->this_hdr;
2337 if (d->rel_idx != 0)
2338 i_shdrp[d->rel_idx] = &d->rel_hdr;
2339 if (d->rel_idx2 != 0)
2340 i_shdrp[d->rel_idx2] = d->rel_hdr2;
2342 /* Fill in the sh_link and sh_info fields while we're at it. */
2344 /* sh_link of a reloc section is the section index of the symbol
2345 table. sh_info is the section index of the section to which
2346 the relocation entries apply. */
2347 if (d->rel_idx != 0)
2349 d->rel_hdr.sh_link = t->symtab_section;
2350 d->rel_hdr.sh_info = d->this_idx;
2352 if (d->rel_idx2 != 0)
2354 d->rel_hdr2->sh_link = t->symtab_section;
2355 d->rel_hdr2->sh_info = d->this_idx;
2358 switch (d->this_hdr.sh_type)
2360 case SHT_REL:
2361 case SHT_RELA:
2362 /* A reloc section which we are treating as a normal BFD
2363 section. sh_link is the section index of the symbol
2364 table. sh_info is the section index of the section to
2365 which the relocation entries apply. We assume that an
2366 allocated reloc section uses the dynamic symbol table.
2367 FIXME: How can we be sure? */
2368 s = bfd_get_section_by_name (abfd, ".dynsym");
2369 if (s != NULL)
2370 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2372 /* We look up the section the relocs apply to by name. */
2373 name = sec->name;
2374 if (d->this_hdr.sh_type == SHT_REL)
2375 name += 4;
2376 else
2377 name += 5;
2378 s = bfd_get_section_by_name (abfd, name);
2379 if (s != NULL)
2380 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
2381 break;
2383 case SHT_STRTAB:
2384 /* We assume that a section named .stab*str is a stabs
2385 string section. We look for a section with the same name
2386 but without the trailing ``str'', and set its sh_link
2387 field to point to this section. */
2388 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
2389 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
2391 size_t len;
2392 char *alc;
2394 len = strlen (sec->name);
2395 alc = (char *) bfd_malloc ((bfd_size_type) len - 2);
2396 if (alc == NULL)
2397 return false;
2398 strncpy (alc, sec->name, len - 3);
2399 alc[len - 3] = '\0';
2400 s = bfd_get_section_by_name (abfd, alc);
2401 free (alc);
2402 if (s != NULL)
2404 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2406 /* This is a .stab section. */
2407 elf_section_data (s)->this_hdr.sh_entsize =
2408 4 + 2 * bfd_get_arch_size (abfd) / 8;
2411 break;
2413 case SHT_DYNAMIC:
2414 case SHT_DYNSYM:
2415 case SHT_GNU_verneed:
2416 case SHT_GNU_verdef:
2417 /* sh_link is the section header index of the string table
2418 used for the dynamic entries, or the symbol table, or the
2419 version strings. */
2420 s = bfd_get_section_by_name (abfd, ".dynstr");
2421 if (s != NULL)
2422 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2423 break;
2425 case SHT_HASH:
2426 case SHT_GNU_versym:
2427 /* sh_link is the section header index of the symbol table
2428 this hash table or version table is for. */
2429 s = bfd_get_section_by_name (abfd, ".dynsym");
2430 if (s != NULL)
2431 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2432 break;
2434 case SHT_GROUP:
2435 d->this_hdr.sh_link = t->symtab_section;
2439 for (secn = 1; secn < section_number; ++secn)
2440 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd),
2441 i_shdrp[secn]->sh_name);
2443 return true;
2446 /* Map symbol from it's internal number to the external number, moving
2447 all local symbols to be at the head of the list. */
2449 static INLINE int
2450 sym_is_global (abfd, sym)
2451 bfd *abfd;
2452 asymbol *sym;
2454 /* If the backend has a special mapping, use it. */
2455 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2456 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2457 (abfd, sym));
2459 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2460 || bfd_is_und_section (bfd_get_section (sym))
2461 || bfd_is_com_section (bfd_get_section (sym)));
2464 static boolean
2465 elf_map_symbols (abfd)
2466 bfd *abfd;
2468 unsigned int symcount = bfd_get_symcount (abfd);
2469 asymbol **syms = bfd_get_outsymbols (abfd);
2470 asymbol **sect_syms;
2471 unsigned int num_locals = 0;
2472 unsigned int num_globals = 0;
2473 unsigned int num_locals2 = 0;
2474 unsigned int num_globals2 = 0;
2475 int max_index = 0;
2476 unsigned int idx;
2477 asection *asect;
2478 asymbol **new_syms;
2479 bfd_size_type amt;
2481 #ifdef DEBUG
2482 fprintf (stderr, "elf_map_symbols\n");
2483 fflush (stderr);
2484 #endif
2486 for (asect = abfd->sections; asect; asect = asect->next)
2488 if (max_index < asect->index)
2489 max_index = asect->index;
2492 max_index++;
2493 amt = max_index * sizeof (asymbol *);
2494 sect_syms = (asymbol **) bfd_zalloc (abfd, amt);
2495 if (sect_syms == NULL)
2496 return false;
2497 elf_section_syms (abfd) = sect_syms;
2498 elf_num_section_syms (abfd) = max_index;
2500 /* Init sect_syms entries for any section symbols we have already
2501 decided to output. */
2502 for (idx = 0; idx < symcount; idx++)
2504 asymbol *sym = syms[idx];
2506 if ((sym->flags & BSF_SECTION_SYM) != 0
2507 && sym->value == 0)
2509 asection *sec;
2511 sec = sym->section;
2513 if (sec->owner != NULL)
2515 if (sec->owner != abfd)
2517 if (sec->output_offset != 0)
2518 continue;
2520 sec = sec->output_section;
2522 /* Empty sections in the input files may have had a
2523 section symbol created for them. (See the comment
2524 near the end of _bfd_generic_link_output_symbols in
2525 linker.c). If the linker script discards such
2526 sections then we will reach this point. Since we know
2527 that we cannot avoid this case, we detect it and skip
2528 the abort and the assignment to the sect_syms array.
2529 To reproduce this particular case try running the
2530 linker testsuite test ld-scripts/weak.exp for an ELF
2531 port that uses the generic linker. */
2532 if (sec->owner == NULL)
2533 continue;
2535 BFD_ASSERT (sec->owner == abfd);
2537 sect_syms[sec->index] = syms[idx];
2542 /* Classify all of the symbols. */
2543 for (idx = 0; idx < symcount; idx++)
2545 if (!sym_is_global (abfd, syms[idx]))
2546 num_locals++;
2547 else
2548 num_globals++;
2551 /* We will be adding a section symbol for each BFD section. Most normal
2552 sections will already have a section symbol in outsymbols, but
2553 eg. SHT_GROUP sections will not, and we need the section symbol mapped
2554 at least in that case. */
2555 for (asect = abfd->sections; asect; asect = asect->next)
2557 if (sect_syms[asect->index] == NULL)
2559 if (!sym_is_global (abfd, asect->symbol))
2560 num_locals++;
2561 else
2562 num_globals++;
2566 /* Now sort the symbols so the local symbols are first. */
2567 amt = (num_locals + num_globals) * sizeof (asymbol *);
2568 new_syms = (asymbol **) bfd_alloc (abfd, amt);
2570 if (new_syms == NULL)
2571 return false;
2573 for (idx = 0; idx < symcount; idx++)
2575 asymbol *sym = syms[idx];
2576 unsigned int i;
2578 if (!sym_is_global (abfd, sym))
2579 i = num_locals2++;
2580 else
2581 i = num_locals + num_globals2++;
2582 new_syms[i] = sym;
2583 sym->udata.i = i + 1;
2585 for (asect = abfd->sections; asect; asect = asect->next)
2587 if (sect_syms[asect->index] == NULL)
2589 asymbol *sym = asect->symbol;
2590 unsigned int i;
2592 sect_syms[asect->index] = sym;
2593 if (!sym_is_global (abfd, sym))
2594 i = num_locals2++;
2595 else
2596 i = num_locals + num_globals2++;
2597 new_syms[i] = sym;
2598 sym->udata.i = i + 1;
2602 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2604 elf_num_locals (abfd) = num_locals;
2605 elf_num_globals (abfd) = num_globals;
2606 return true;
2609 /* Align to the maximum file alignment that could be required for any
2610 ELF data structure. */
2612 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2613 static INLINE file_ptr
2614 align_file_position (off, align)
2615 file_ptr off;
2616 int align;
2618 return (off + align - 1) & ~(align - 1);
2621 /* Assign a file position to a section, optionally aligning to the
2622 required section alignment. */
2624 INLINE file_ptr
2625 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2626 Elf_Internal_Shdr *i_shdrp;
2627 file_ptr offset;
2628 boolean align;
2630 if (align)
2632 unsigned int al;
2634 al = i_shdrp->sh_addralign;
2635 if (al > 1)
2636 offset = BFD_ALIGN (offset, al);
2638 i_shdrp->sh_offset = offset;
2639 if (i_shdrp->bfd_section != NULL)
2640 i_shdrp->bfd_section->filepos = offset;
2641 if (i_shdrp->sh_type != SHT_NOBITS)
2642 offset += i_shdrp->sh_size;
2643 return offset;
2646 /* Compute the file positions we are going to put the sections at, and
2647 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2648 is not NULL, this is being called by the ELF backend linker. */
2650 boolean
2651 _bfd_elf_compute_section_file_positions (abfd, link_info)
2652 bfd *abfd;
2653 struct bfd_link_info *link_info;
2655 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2656 boolean failed;
2657 struct bfd_strtab_hash *strtab;
2658 Elf_Internal_Shdr *shstrtab_hdr;
2660 if (abfd->output_has_begun)
2661 return true;
2663 /* Do any elf backend specific processing first. */
2664 if (bed->elf_backend_begin_write_processing)
2665 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2667 if (! prep_headers (abfd))
2668 return false;
2670 /* Post process the headers if necessary. */
2671 if (bed->elf_backend_post_process_headers)
2672 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2674 failed = false;
2675 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2676 if (failed)
2677 return false;
2679 if (!assign_section_numbers (abfd))
2680 return false;
2682 /* The backend linker builds symbol table information itself. */
2683 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2685 /* Non-zero if doing a relocatable link. */
2686 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2688 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2689 return false;
2692 if (link_info == NULL || link_info->relocateable)
2694 bfd_map_over_sections (abfd, set_group_contents, &failed);
2695 if (failed)
2696 return false;
2699 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2700 /* sh_name was set in prep_headers. */
2701 shstrtab_hdr->sh_type = SHT_STRTAB;
2702 shstrtab_hdr->sh_flags = 0;
2703 shstrtab_hdr->sh_addr = 0;
2704 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd));
2705 shstrtab_hdr->sh_entsize = 0;
2706 shstrtab_hdr->sh_link = 0;
2707 shstrtab_hdr->sh_info = 0;
2708 /* sh_offset is set in assign_file_positions_except_relocs. */
2709 shstrtab_hdr->sh_addralign = 1;
2711 if (!assign_file_positions_except_relocs (abfd))
2712 return false;
2714 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2716 file_ptr off;
2717 Elf_Internal_Shdr *hdr;
2719 off = elf_tdata (abfd)->next_file_pos;
2721 hdr = &elf_tdata (abfd)->symtab_hdr;
2722 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2724 hdr = &elf_tdata (abfd)->strtab_hdr;
2725 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2727 elf_tdata (abfd)->next_file_pos = off;
2729 /* Now that we know where the .strtab section goes, write it
2730 out. */
2731 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2732 || ! _bfd_stringtab_emit (abfd, strtab))
2733 return false;
2734 _bfd_stringtab_free (strtab);
2737 abfd->output_has_begun = true;
2739 return true;
2742 /* Create a mapping from a set of sections to a program segment. */
2744 static INLINE struct elf_segment_map *
2745 make_mapping (abfd, sections, from, to, phdr)
2746 bfd *abfd;
2747 asection **sections;
2748 unsigned int from;
2749 unsigned int to;
2750 boolean phdr;
2752 struct elf_segment_map *m;
2753 unsigned int i;
2754 asection **hdrpp;
2755 bfd_size_type amt;
2757 amt = sizeof (struct elf_segment_map);
2758 amt += (to - from - 1) * sizeof (asection *);
2759 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2760 if (m == NULL)
2761 return NULL;
2762 m->next = NULL;
2763 m->p_type = PT_LOAD;
2764 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2765 m->sections[i - from] = *hdrpp;
2766 m->count = to - from;
2768 if (from == 0 && phdr)
2770 /* Include the headers in the first PT_LOAD segment. */
2771 m->includes_filehdr = 1;
2772 m->includes_phdrs = 1;
2775 return m;
2778 /* Set up a mapping from BFD sections to program segments. */
2780 static boolean
2781 map_sections_to_segments (abfd)
2782 bfd *abfd;
2784 asection **sections = NULL;
2785 asection *s;
2786 unsigned int i;
2787 unsigned int count;
2788 struct elf_segment_map *mfirst;
2789 struct elf_segment_map **pm;
2790 struct elf_segment_map *m;
2791 asection *last_hdr;
2792 unsigned int phdr_index;
2793 bfd_vma maxpagesize;
2794 asection **hdrpp;
2795 boolean phdr_in_segment = true;
2796 boolean writable;
2797 asection *dynsec;
2798 bfd_size_type amt;
2800 if (elf_tdata (abfd)->segment_map != NULL)
2801 return true;
2803 if (bfd_count_sections (abfd) == 0)
2804 return true;
2806 /* Select the allocated sections, and sort them. */
2808 amt = bfd_count_sections (abfd) * sizeof (asection *);
2809 sections = (asection **) bfd_malloc (amt);
2810 if (sections == NULL)
2811 goto error_return;
2813 i = 0;
2814 for (s = abfd->sections; s != NULL; s = s->next)
2816 if ((s->flags & SEC_ALLOC) != 0)
2818 sections[i] = s;
2819 ++i;
2822 BFD_ASSERT (i <= bfd_count_sections (abfd));
2823 count = i;
2825 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2827 /* Build the mapping. */
2829 mfirst = NULL;
2830 pm = &mfirst;
2832 /* If we have a .interp section, then create a PT_PHDR segment for
2833 the program headers and a PT_INTERP segment for the .interp
2834 section. */
2835 s = bfd_get_section_by_name (abfd, ".interp");
2836 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2838 amt = sizeof (struct elf_segment_map);
2839 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2840 if (m == NULL)
2841 goto error_return;
2842 m->next = NULL;
2843 m->p_type = PT_PHDR;
2844 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2845 m->p_flags = PF_R | PF_X;
2846 m->p_flags_valid = 1;
2847 m->includes_phdrs = 1;
2849 *pm = m;
2850 pm = &m->next;
2852 amt = sizeof (struct elf_segment_map);
2853 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
2854 if (m == NULL)
2855 goto error_return;
2856 m->next = NULL;
2857 m->p_type = PT_INTERP;
2858 m->count = 1;
2859 m->sections[0] = s;
2861 *pm = m;
2862 pm = &m->next;
2865 /* Look through the sections. We put sections in the same program
2866 segment when the start of the second section can be placed within
2867 a few bytes of the end of the first section. */
2868 last_hdr = NULL;
2869 phdr_index = 0;
2870 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2871 writable = false;
2872 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2873 if (dynsec != NULL
2874 && (dynsec->flags & SEC_LOAD) == 0)
2875 dynsec = NULL;
2877 /* Deal with -Ttext or something similar such that the first section
2878 is not adjacent to the program headers. This is an
2879 approximation, since at this point we don't know exactly how many
2880 program headers we will need. */
2881 if (count > 0)
2883 bfd_size_type phdr_size;
2885 phdr_size = elf_tdata (abfd)->program_header_size;
2886 if (phdr_size == 0)
2887 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2888 if ((abfd->flags & D_PAGED) == 0
2889 || sections[0]->lma < phdr_size
2890 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2891 phdr_in_segment = false;
2894 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2896 asection *hdr;
2897 boolean new_segment;
2899 hdr = *hdrpp;
2901 /* See if this section and the last one will fit in the same
2902 segment. */
2904 if (last_hdr == NULL)
2906 /* If we don't have a segment yet, then we don't need a new
2907 one (we build the last one after this loop). */
2908 new_segment = false;
2910 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2912 /* If this section has a different relation between the
2913 virtual address and the load address, then we need a new
2914 segment. */
2915 new_segment = true;
2917 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2918 < BFD_ALIGN (hdr->lma, maxpagesize))
2920 /* If putting this section in this segment would force us to
2921 skip a page in the segment, then we need a new segment. */
2922 new_segment = true;
2924 else if ((last_hdr->flags & SEC_LOAD) == 0
2925 && (hdr->flags & SEC_LOAD) != 0)
2927 /* We don't want to put a loadable section after a
2928 nonloadable section in the same segment. */
2929 new_segment = true;
2931 else if ((abfd->flags & D_PAGED) == 0)
2933 /* If the file is not demand paged, which means that we
2934 don't require the sections to be correctly aligned in the
2935 file, then there is no other reason for a new segment. */
2936 new_segment = false;
2938 else if (! writable
2939 && (hdr->flags & SEC_READONLY) == 0
2940 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2941 == hdr->lma))
2943 /* We don't want to put a writable section in a read only
2944 segment, unless they are on the same page in memory
2945 anyhow. We already know that the last section does not
2946 bring us past the current section on the page, so the
2947 only case in which the new section is not on the same
2948 page as the previous section is when the previous section
2949 ends precisely on a page boundary. */
2950 new_segment = true;
2952 else
2954 /* Otherwise, we can use the same segment. */
2955 new_segment = false;
2958 if (! new_segment)
2960 if ((hdr->flags & SEC_READONLY) == 0)
2961 writable = true;
2962 last_hdr = hdr;
2963 continue;
2966 /* We need a new program segment. We must create a new program
2967 header holding all the sections from phdr_index until hdr. */
2969 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2970 if (m == NULL)
2971 goto error_return;
2973 *pm = m;
2974 pm = &m->next;
2976 if ((hdr->flags & SEC_READONLY) == 0)
2977 writable = true;
2978 else
2979 writable = false;
2981 last_hdr = hdr;
2982 phdr_index = i;
2983 phdr_in_segment = false;
2986 /* Create a final PT_LOAD program segment. */
2987 if (last_hdr != NULL)
2989 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2990 if (m == NULL)
2991 goto error_return;
2993 *pm = m;
2994 pm = &m->next;
2997 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2998 if (dynsec != NULL)
3000 amt = sizeof (struct elf_segment_map);
3001 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3002 if (m == NULL)
3003 goto error_return;
3004 m->next = NULL;
3005 m->p_type = PT_DYNAMIC;
3006 m->count = 1;
3007 m->sections[0] = dynsec;
3009 *pm = m;
3010 pm = &m->next;
3013 /* For each loadable .note section, add a PT_NOTE segment. We don't
3014 use bfd_get_section_by_name, because if we link together
3015 nonloadable .note sections and loadable .note sections, we will
3016 generate two .note sections in the output file. FIXME: Using
3017 names for section types is bogus anyhow. */
3018 for (s = abfd->sections; s != NULL; s = s->next)
3020 if ((s->flags & SEC_LOAD) != 0
3021 && strncmp (s->name, ".note", 5) == 0)
3023 amt = sizeof (struct elf_segment_map);
3024 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
3025 if (m == NULL)
3026 goto error_return;
3027 m->next = NULL;
3028 m->p_type = PT_NOTE;
3029 m->count = 1;
3030 m->sections[0] = s;
3032 *pm = m;
3033 pm = &m->next;
3037 free (sections);
3038 sections = NULL;
3040 elf_tdata (abfd)->segment_map = mfirst;
3041 return true;
3043 error_return:
3044 if (sections != NULL)
3045 free (sections);
3046 return false;
3049 /* Sort sections by address. */
3051 static int
3052 elf_sort_sections (arg1, arg2)
3053 const PTR arg1;
3054 const PTR arg2;
3056 const asection *sec1 = *(const asection **) arg1;
3057 const asection *sec2 = *(const asection **) arg2;
3059 /* Sort by LMA first, since this is the address used to
3060 place the section into a segment. */
3061 if (sec1->lma < sec2->lma)
3062 return -1;
3063 else if (sec1->lma > sec2->lma)
3064 return 1;
3066 /* Then sort by VMA. Normally the LMA and the VMA will be
3067 the same, and this will do nothing. */
3068 if (sec1->vma < sec2->vma)
3069 return -1;
3070 else if (sec1->vma > sec2->vma)
3071 return 1;
3073 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
3075 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
3077 if (TOEND (sec1))
3079 if (TOEND (sec2))
3081 /* If the indicies are the same, do not return 0
3082 here, but continue to try the next comparison. */
3083 if (sec1->target_index - sec2->target_index != 0)
3084 return sec1->target_index - sec2->target_index;
3086 else
3087 return 1;
3089 else if (TOEND (sec2))
3090 return -1;
3092 #undef TOEND
3094 /* Sort by size, to put zero sized sections
3095 before others at the same address. */
3097 if (sec1->_raw_size < sec2->_raw_size)
3098 return -1;
3099 if (sec1->_raw_size > sec2->_raw_size)
3100 return 1;
3102 return sec1->target_index - sec2->target_index;
3105 /* Assign file positions to the sections based on the mapping from
3106 sections to segments. This function also sets up some fields in
3107 the file header, and writes out the program headers. */
3109 static boolean
3110 assign_file_positions_for_segments (abfd)
3111 bfd *abfd;
3113 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
3114 unsigned int count;
3115 struct elf_segment_map *m;
3116 unsigned int alloc;
3117 Elf_Internal_Phdr *phdrs;
3118 file_ptr off, voff;
3119 bfd_vma filehdr_vaddr, filehdr_paddr;
3120 bfd_vma phdrs_vaddr, phdrs_paddr;
3121 Elf_Internal_Phdr *p;
3122 bfd_size_type amt;
3124 if (elf_tdata (abfd)->segment_map == NULL)
3126 if (! map_sections_to_segments (abfd))
3127 return false;
3130 if (bed->elf_backend_modify_segment_map)
3132 if (! (*bed->elf_backend_modify_segment_map) (abfd))
3133 return false;
3136 count = 0;
3137 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3138 ++count;
3140 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
3141 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
3142 elf_elfheader (abfd)->e_phnum = count;
3144 if (count == 0)
3145 return true;
3147 /* If we already counted the number of program segments, make sure
3148 that we allocated enough space. This happens when SIZEOF_HEADERS
3149 is used in a linker script. */
3150 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
3151 if (alloc != 0 && count > alloc)
3153 ((*_bfd_error_handler)
3154 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
3155 bfd_get_filename (abfd), alloc, count));
3156 bfd_set_error (bfd_error_bad_value);
3157 return false;
3160 if (alloc == 0)
3161 alloc = count;
3163 amt = alloc * sizeof (Elf_Internal_Phdr);
3164 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
3165 if (phdrs == NULL)
3166 return false;
3168 off = bed->s->sizeof_ehdr;
3169 off += alloc * bed->s->sizeof_phdr;
3171 filehdr_vaddr = 0;
3172 filehdr_paddr = 0;
3173 phdrs_vaddr = 0;
3174 phdrs_paddr = 0;
3176 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3177 m != NULL;
3178 m = m->next, p++)
3180 unsigned int i;
3181 asection **secpp;
3183 /* If elf_segment_map is not from map_sections_to_segments, the
3184 sections may not be correctly ordered. */
3185 if (m->count > 0)
3186 qsort (m->sections, (size_t) m->count, sizeof (asection *),
3187 elf_sort_sections);
3189 p->p_type = m->p_type;
3190 p->p_flags = m->p_flags;
3192 if (p->p_type == PT_LOAD
3193 && m->count > 0
3194 && (m->sections[0]->flags & SEC_ALLOC) != 0)
3196 if ((abfd->flags & D_PAGED) != 0)
3197 off += (m->sections[0]->vma - off) % bed->maxpagesize;
3198 else
3200 bfd_size_type align;
3202 align = 0;
3203 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3205 bfd_size_type secalign;
3207 secalign = bfd_get_section_alignment (abfd, *secpp);
3208 if (secalign > align)
3209 align = secalign;
3212 off += (m->sections[0]->vma - off) % (1 << align);
3216 if (m->count == 0)
3217 p->p_vaddr = 0;
3218 else
3219 p->p_vaddr = m->sections[0]->vma;
3221 if (m->p_paddr_valid)
3222 p->p_paddr = m->p_paddr;
3223 else if (m->count == 0)
3224 p->p_paddr = 0;
3225 else
3226 p->p_paddr = m->sections[0]->lma;
3228 if (p->p_type == PT_LOAD
3229 && (abfd->flags & D_PAGED) != 0)
3230 p->p_align = bed->maxpagesize;
3231 else if (m->count == 0)
3232 p->p_align = bed->s->file_align;
3233 else
3234 p->p_align = 0;
3236 p->p_offset = 0;
3237 p->p_filesz = 0;
3238 p->p_memsz = 0;
3240 if (m->includes_filehdr)
3242 if (! m->p_flags_valid)
3243 p->p_flags |= PF_R;
3244 p->p_offset = 0;
3245 p->p_filesz = bed->s->sizeof_ehdr;
3246 p->p_memsz = bed->s->sizeof_ehdr;
3247 if (m->count > 0)
3249 BFD_ASSERT (p->p_type == PT_LOAD);
3251 if (p->p_vaddr < (bfd_vma) off)
3253 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
3254 bfd_get_filename (abfd));
3255 bfd_set_error (bfd_error_bad_value);
3256 return false;
3259 p->p_vaddr -= off;
3260 if (! m->p_paddr_valid)
3261 p->p_paddr -= off;
3263 if (p->p_type == PT_LOAD)
3265 filehdr_vaddr = p->p_vaddr;
3266 filehdr_paddr = p->p_paddr;
3270 if (m->includes_phdrs)
3272 if (! m->p_flags_valid)
3273 p->p_flags |= PF_R;
3275 if (m->includes_filehdr)
3277 if (p->p_type == PT_LOAD)
3279 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
3280 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
3283 else
3285 p->p_offset = bed->s->sizeof_ehdr;
3287 if (m->count > 0)
3289 BFD_ASSERT (p->p_type == PT_LOAD);
3290 p->p_vaddr -= off - p->p_offset;
3291 if (! m->p_paddr_valid)
3292 p->p_paddr -= off - p->p_offset;
3295 if (p->p_type == PT_LOAD)
3297 phdrs_vaddr = p->p_vaddr;
3298 phdrs_paddr = p->p_paddr;
3300 else
3301 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
3304 p->p_filesz += alloc * bed->s->sizeof_phdr;
3305 p->p_memsz += alloc * bed->s->sizeof_phdr;
3308 if (p->p_type == PT_LOAD
3309 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
3311 if (! m->includes_filehdr && ! m->includes_phdrs)
3312 p->p_offset = off;
3313 else
3315 file_ptr adjust;
3317 adjust = off - (p->p_offset + p->p_filesz);
3318 p->p_filesz += adjust;
3319 p->p_memsz += adjust;
3323 voff = off;
3325 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
3327 asection *sec;
3328 flagword flags;
3329 bfd_size_type align;
3331 sec = *secpp;
3332 flags = sec->flags;
3333 align = 1 << bfd_get_section_alignment (abfd, sec);
3335 /* The section may have artificial alignment forced by a
3336 link script. Notice this case by the gap between the
3337 cumulative phdr lma and the section's lma. */
3338 if (p->p_paddr + p->p_memsz < sec->lma)
3340 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz);
3342 p->p_memsz += adjust;
3343 off += adjust;
3344 voff += adjust;
3345 if ((flags & SEC_LOAD) != 0)
3346 p->p_filesz += adjust;
3349 if (p->p_type == PT_LOAD)
3351 bfd_signed_vma adjust;
3353 if ((flags & SEC_LOAD) != 0)
3355 adjust = sec->lma - (p->p_paddr + p->p_memsz);
3356 if (adjust < 0)
3357 adjust = 0;
3359 else if ((flags & SEC_ALLOC) != 0)
3361 /* The section VMA must equal the file position
3362 modulo the page size. FIXME: I'm not sure if
3363 this adjustment is really necessary. We used to
3364 not have the SEC_LOAD case just above, and then
3365 this was necessary, but now I'm not sure. */
3366 if ((abfd->flags & D_PAGED) != 0)
3367 adjust = (sec->vma - voff) % bed->maxpagesize;
3368 else
3369 adjust = (sec->vma - voff) % align;
3371 else
3372 adjust = 0;
3374 if (adjust != 0)
3376 if (i == 0)
3378 (* _bfd_error_handler)
3379 (_("Error: First section in segment (%s) starts at 0x%x"),
3380 bfd_section_name (abfd, sec), sec->lma);
3381 (* _bfd_error_handler)
3382 (_(" whereas segment starts at 0x%x"),
3383 p->p_paddr);
3385 return false;
3387 p->p_memsz += adjust;
3388 off += adjust;
3389 voff += adjust;
3390 if ((flags & SEC_LOAD) != 0)
3391 p->p_filesz += adjust;
3394 sec->filepos = off;
3396 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
3397 used in a linker script we may have a section with
3398 SEC_LOAD clear but which is supposed to have
3399 contents. */
3400 if ((flags & SEC_LOAD) != 0
3401 || (flags & SEC_HAS_CONTENTS) != 0)
3402 off += sec->_raw_size;
3404 if ((flags & SEC_ALLOC) != 0)
3405 voff += sec->_raw_size;
3408 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3410 /* The actual "note" segment has i == 0.
3411 This is the one that actually contains everything. */
3412 if (i == 0)
3414 sec->filepos = off;
3415 p->p_filesz = sec->_raw_size;
3416 off += sec->_raw_size;
3417 voff = off;
3419 else
3421 /* Fake sections -- don't need to be written. */
3422 sec->filepos = 0;
3423 sec->_raw_size = 0;
3424 flags = sec->flags = 0;
3426 p->p_memsz = 0;
3427 p->p_align = 1;
3429 else
3431 p->p_memsz += sec->_raw_size;
3433 if ((flags & SEC_LOAD) != 0)
3434 p->p_filesz += sec->_raw_size;
3436 if (align > p->p_align
3437 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3438 p->p_align = align;
3441 if (! m->p_flags_valid)
3443 p->p_flags |= PF_R;
3444 if ((flags & SEC_CODE) != 0)
3445 p->p_flags |= PF_X;
3446 if ((flags & SEC_READONLY) == 0)
3447 p->p_flags |= PF_W;
3452 /* Now that we have set the section file positions, we can set up
3453 the file positions for the non PT_LOAD segments. */
3454 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3455 m != NULL;
3456 m = m->next, p++)
3458 if (p->p_type != PT_LOAD && m->count > 0)
3460 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3461 p->p_offset = m->sections[0]->filepos;
3463 if (m->count == 0)
3465 if (m->includes_filehdr)
3467 p->p_vaddr = filehdr_vaddr;
3468 if (! m->p_paddr_valid)
3469 p->p_paddr = filehdr_paddr;
3471 else if (m->includes_phdrs)
3473 p->p_vaddr = phdrs_vaddr;
3474 if (! m->p_paddr_valid)
3475 p->p_paddr = phdrs_paddr;
3480 /* Clear out any program headers we allocated but did not use. */
3481 for (; count < alloc; count++, p++)
3483 memset (p, 0, sizeof *p);
3484 p->p_type = PT_NULL;
3487 elf_tdata (abfd)->phdr = phdrs;
3489 elf_tdata (abfd)->next_file_pos = off;
3491 /* Write out the program headers. */
3492 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0
3493 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3494 return false;
3496 return true;
3499 /* Get the size of the program header.
3501 If this is called by the linker before any of the section VMA's are set, it
3502 can't calculate the correct value for a strange memory layout. This only
3503 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3504 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3505 data segment (exclusive of .interp and .dynamic).
3507 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3508 will be two segments. */
3510 static bfd_size_type
3511 get_program_header_size (abfd)
3512 bfd *abfd;
3514 size_t segs;
3515 asection *s;
3516 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3518 /* We can't return a different result each time we're called. */
3519 if (elf_tdata (abfd)->program_header_size != 0)
3520 return elf_tdata (abfd)->program_header_size;
3522 if (elf_tdata (abfd)->segment_map != NULL)
3524 struct elf_segment_map *m;
3526 segs = 0;
3527 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3528 ++segs;
3529 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3530 return elf_tdata (abfd)->program_header_size;
3533 /* Assume we will need exactly two PT_LOAD segments: one for text
3534 and one for data. */
3535 segs = 2;
3537 s = bfd_get_section_by_name (abfd, ".interp");
3538 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3540 /* If we have a loadable interpreter section, we need a
3541 PT_INTERP segment. In this case, assume we also need a
3542 PT_PHDR segment, although that may not be true for all
3543 targets. */
3544 segs += 2;
3547 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3549 /* We need a PT_DYNAMIC segment. */
3550 ++segs;
3553 for (s = abfd->sections; s != NULL; s = s->next)
3555 if ((s->flags & SEC_LOAD) != 0
3556 && strncmp (s->name, ".note", 5) == 0)
3558 /* We need a PT_NOTE segment. */
3559 ++segs;
3563 /* Let the backend count up any program headers it might need. */
3564 if (bed->elf_backend_additional_program_headers)
3566 int a;
3568 a = (*bed->elf_backend_additional_program_headers) (abfd);
3569 if (a == -1)
3570 abort ();
3571 segs += a;
3574 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3575 return elf_tdata (abfd)->program_header_size;
3578 /* Work out the file positions of all the sections. This is called by
3579 _bfd_elf_compute_section_file_positions. All the section sizes and
3580 VMAs must be known before this is called.
3582 We do not consider reloc sections at this point, unless they form
3583 part of the loadable image. Reloc sections are assigned file
3584 positions in assign_file_positions_for_relocs, which is called by
3585 write_object_contents and final_link.
3587 We also don't set the positions of the .symtab and .strtab here. */
3589 static boolean
3590 assign_file_positions_except_relocs (abfd)
3591 bfd *abfd;
3593 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3594 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3595 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3596 file_ptr off;
3597 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3599 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3600 && bfd_get_format (abfd) != bfd_core)
3602 Elf_Internal_Shdr **hdrpp;
3603 unsigned int i;
3605 /* Start after the ELF header. */
3606 off = i_ehdrp->e_ehsize;
3608 /* We are not creating an executable, which means that we are
3609 not creating a program header, and that the actual order of
3610 the sections in the file is unimportant. */
3611 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3613 Elf_Internal_Shdr *hdr;
3615 hdr = *hdrpp;
3616 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3618 hdr->sh_offset = -1;
3619 continue;
3621 if (i == tdata->symtab_section
3622 || i == tdata->strtab_section)
3624 hdr->sh_offset = -1;
3625 continue;
3628 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3631 else
3633 unsigned int i;
3634 Elf_Internal_Shdr **hdrpp;
3636 /* Assign file positions for the loaded sections based on the
3637 assignment of sections to segments. */
3638 if (! assign_file_positions_for_segments (abfd))
3639 return false;
3641 /* Assign file positions for the other sections. */
3643 off = elf_tdata (abfd)->next_file_pos;
3644 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3646 Elf_Internal_Shdr *hdr;
3648 hdr = *hdrpp;
3649 if (hdr->bfd_section != NULL
3650 && hdr->bfd_section->filepos != 0)
3651 hdr->sh_offset = hdr->bfd_section->filepos;
3652 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3654 ((*_bfd_error_handler)
3655 (_("%s: warning: allocated section `%s' not in segment"),
3656 bfd_get_filename (abfd),
3657 (hdr->bfd_section == NULL
3658 ? "*unknown*"
3659 : hdr->bfd_section->name)));
3660 if ((abfd->flags & D_PAGED) != 0)
3661 off += (hdr->sh_addr - off) % bed->maxpagesize;
3662 else
3663 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3664 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3665 false);
3667 else if (hdr->sh_type == SHT_REL
3668 || hdr->sh_type == SHT_RELA
3669 || hdr == i_shdrpp[tdata->symtab_section]
3670 || hdr == i_shdrpp[tdata->strtab_section])
3671 hdr->sh_offset = -1;
3672 else
3673 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3677 /* Place the section headers. */
3678 off = align_file_position (off, bed->s->file_align);
3679 i_ehdrp->e_shoff = off;
3680 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3682 elf_tdata (abfd)->next_file_pos = off;
3684 return true;
3687 static boolean
3688 prep_headers (abfd)
3689 bfd *abfd;
3691 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3692 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3693 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3694 int count;
3695 struct elf_strtab_hash *shstrtab;
3696 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3698 i_ehdrp = elf_elfheader (abfd);
3699 i_shdrp = elf_elfsections (abfd);
3701 shstrtab = _bfd_elf_strtab_init ();
3702 if (shstrtab == NULL)
3703 return false;
3705 elf_shstrtab (abfd) = shstrtab;
3707 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3708 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3709 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3710 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3712 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3713 i_ehdrp->e_ident[EI_DATA] =
3714 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3715 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3717 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3718 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3720 for (count = EI_PAD; count < EI_NIDENT; count++)
3721 i_ehdrp->e_ident[count] = 0;
3723 if ((abfd->flags & DYNAMIC) != 0)
3724 i_ehdrp->e_type = ET_DYN;
3725 else if ((abfd->flags & EXEC_P) != 0)
3726 i_ehdrp->e_type = ET_EXEC;
3727 else if (bfd_get_format (abfd) == bfd_core)
3728 i_ehdrp->e_type = ET_CORE;
3729 else
3730 i_ehdrp->e_type = ET_REL;
3732 switch (bfd_get_arch (abfd))
3734 case bfd_arch_unknown:
3735 i_ehdrp->e_machine = EM_NONE;
3736 break;
3738 /* There used to be a long list of cases here, each one setting
3739 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE
3740 in the corresponding bfd definition. To avoid duplication,
3741 the switch was removed. Machines that need special handling
3742 can generally do it in elf_backend_final_write_processing(),
3743 unless they need the information earlier than the final write.
3744 Such need can generally be supplied by replacing the tests for
3745 e_machine with the conditions used to determine it. */
3746 default:
3747 if (get_elf_backend_data (abfd) != NULL)
3748 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code;
3749 else
3750 i_ehdrp->e_machine = EM_NONE;
3753 i_ehdrp->e_version = bed->s->ev_current;
3754 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3756 /* No program header, for now. */
3757 i_ehdrp->e_phoff = 0;
3758 i_ehdrp->e_phentsize = 0;
3759 i_ehdrp->e_phnum = 0;
3761 /* Each bfd section is section header entry. */
3762 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3763 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3765 /* If we're building an executable, we'll need a program header table. */
3766 if (abfd->flags & EXEC_P)
3768 /* It all happens later. */
3769 #if 0
3770 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3772 /* elf_build_phdrs() returns a (NULL-terminated) array of
3773 Elf_Internal_Phdrs. */
3774 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3775 i_ehdrp->e_phoff = outbase;
3776 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3777 #endif
3779 else
3781 i_ehdrp->e_phentsize = 0;
3782 i_phdrp = 0;
3783 i_ehdrp->e_phoff = 0;
3786 elf_tdata (abfd)->symtab_hdr.sh_name =
3787 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false);
3788 elf_tdata (abfd)->strtab_hdr.sh_name =
3789 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false);
3790 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3791 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false);
3792 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3793 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3794 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3795 return false;
3797 return true;
3800 /* Assign file positions for all the reloc sections which are not part
3801 of the loadable file image. */
3803 void
3804 _bfd_elf_assign_file_positions_for_relocs (abfd)
3805 bfd *abfd;
3807 file_ptr off;
3808 unsigned int i;
3809 Elf_Internal_Shdr **shdrpp;
3811 off = elf_tdata (abfd)->next_file_pos;
3813 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3814 i < elf_elfheader (abfd)->e_shnum;
3815 i++, shdrpp++)
3817 Elf_Internal_Shdr *shdrp;
3819 shdrp = *shdrpp;
3820 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3821 && shdrp->sh_offset == -1)
3822 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3825 elf_tdata (abfd)->next_file_pos = off;
3828 boolean
3829 _bfd_elf_write_object_contents (abfd)
3830 bfd *abfd;
3832 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3833 Elf_Internal_Ehdr *i_ehdrp;
3834 Elf_Internal_Shdr **i_shdrp;
3835 boolean failed;
3836 unsigned int count;
3838 if (! abfd->output_has_begun
3839 && ! _bfd_elf_compute_section_file_positions
3840 (abfd, (struct bfd_link_info *) NULL))
3841 return false;
3843 i_shdrp = elf_elfsections (abfd);
3844 i_ehdrp = elf_elfheader (abfd);
3846 failed = false;
3847 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3848 if (failed)
3849 return false;
3851 _bfd_elf_assign_file_positions_for_relocs (abfd);
3853 /* After writing the headers, we need to write the sections too... */
3854 for (count = 1; count < i_ehdrp->e_shnum; count++)
3856 if (bed->elf_backend_section_processing)
3857 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3858 if (i_shdrp[count]->contents)
3860 bfd_size_type amt = i_shdrp[count]->sh_size;
3862 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3863 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt)
3864 return false;
3868 /* Write out the section header names. */
3869 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3870 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))
3871 return false;
3873 if (bed->elf_backend_final_write_processing)
3874 (*bed->elf_backend_final_write_processing) (abfd,
3875 elf_tdata (abfd)->linker);
3877 return bed->s->write_shdrs_and_ehdr (abfd);
3880 boolean
3881 _bfd_elf_write_corefile_contents (abfd)
3882 bfd *abfd;
3884 /* Hopefully this can be done just like an object file. */
3885 return _bfd_elf_write_object_contents (abfd);
3888 /* Given a section, search the header to find them. */
3891 _bfd_elf_section_from_bfd_section (abfd, asect)
3892 bfd *abfd;
3893 struct sec *asect;
3895 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3896 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3897 int index;
3898 Elf_Internal_Shdr *hdr;
3899 int maxindex = elf_elfheader (abfd)->e_shnum;
3901 for (index = 0; index < maxindex; index++)
3903 hdr = i_shdrp[index];
3904 if (hdr->bfd_section == asect)
3905 return index;
3908 if (bed->elf_backend_section_from_bfd_section)
3910 for (index = 0; index < maxindex; index++)
3912 int retval;
3914 hdr = i_shdrp[index];
3915 retval = index;
3916 if ((*bed->elf_backend_section_from_bfd_section)
3917 (abfd, hdr, asect, &retval))
3918 return retval;
3922 if (bfd_is_abs_section (asect))
3923 return SHN_ABS;
3924 if (bfd_is_com_section (asect))
3925 return SHN_COMMON;
3926 if (bfd_is_und_section (asect))
3927 return SHN_UNDEF;
3929 bfd_set_error (bfd_error_nonrepresentable_section);
3931 return -1;
3934 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3935 on error. */
3938 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3939 bfd *abfd;
3940 asymbol **asym_ptr_ptr;
3942 asymbol *asym_ptr = *asym_ptr_ptr;
3943 int idx;
3944 flagword flags = asym_ptr->flags;
3946 /* When gas creates relocations against local labels, it creates its
3947 own symbol for the section, but does put the symbol into the
3948 symbol chain, so udata is 0. When the linker is generating
3949 relocatable output, this section symbol may be for one of the
3950 input sections rather than the output section. */
3951 if (asym_ptr->udata.i == 0
3952 && (flags & BSF_SECTION_SYM)
3953 && asym_ptr->section)
3955 int indx;
3957 if (asym_ptr->section->output_section != NULL)
3958 indx = asym_ptr->section->output_section->index;
3959 else
3960 indx = asym_ptr->section->index;
3961 if (indx < elf_num_section_syms (abfd)
3962 && elf_section_syms (abfd)[indx] != NULL)
3963 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3966 idx = asym_ptr->udata.i;
3968 if (idx == 0)
3970 /* This case can occur when using --strip-symbol on a symbol
3971 which is used in a relocation entry. */
3972 (*_bfd_error_handler)
3973 (_("%s: symbol `%s' required but not present"),
3974 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr));
3975 bfd_set_error (bfd_error_no_symbols);
3976 return -1;
3979 #if DEBUG & 4
3981 fprintf (stderr,
3982 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3983 (long) asym_ptr, asym_ptr->name, idx, flags,
3984 elf_symbol_flags (flags));
3985 fflush (stderr);
3987 #endif
3989 return idx;
3992 /* Copy private BFD data. This copies any program header information. */
3994 static boolean
3995 copy_private_bfd_data (ibfd, obfd)
3996 bfd *ibfd;
3997 bfd *obfd;
3999 Elf_Internal_Ehdr * iehdr;
4000 struct elf_segment_map * map;
4001 struct elf_segment_map * map_first;
4002 struct elf_segment_map ** pointer_to_map;
4003 Elf_Internal_Phdr * segment;
4004 asection * section;
4005 unsigned int i;
4006 unsigned int num_segments;
4007 boolean phdr_included = false;
4008 bfd_vma maxpagesize;
4009 struct elf_segment_map * phdr_adjust_seg = NULL;
4010 unsigned int phdr_adjust_num = 0;
4012 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4013 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4014 return true;
4016 if (elf_tdata (ibfd)->phdr == NULL)
4017 return true;
4019 iehdr = elf_elfheader (ibfd);
4021 map_first = NULL;
4022 pointer_to_map = &map_first;
4024 num_segments = elf_elfheader (ibfd)->e_phnum;
4025 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
4027 /* Returns the end address of the segment + 1. */
4028 #define SEGMENT_END(segment, start) \
4029 (start + (segment->p_memsz > segment->p_filesz \
4030 ? segment->p_memsz : segment->p_filesz))
4032 /* Returns true if the given section is contained within
4033 the given segment. VMA addresses are compared. */
4034 #define IS_CONTAINED_BY_VMA(section, segment) \
4035 (section->vma >= segment->p_vaddr \
4036 && (section->vma + section->_raw_size) \
4037 <= (SEGMENT_END (segment, segment->p_vaddr)))
4039 /* Returns true if the given section is contained within
4040 the given segment. LMA addresses are compared. */
4041 #define IS_CONTAINED_BY_LMA(section, segment, base) \
4042 (section->lma >= base \
4043 && (section->lma + section->_raw_size) \
4044 <= SEGMENT_END (segment, base))
4046 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
4047 #define IS_COREFILE_NOTE(p, s) \
4048 (p->p_type == PT_NOTE \
4049 && bfd_get_format (ibfd) == bfd_core \
4050 && s->vma == 0 && s->lma == 0 \
4051 && (bfd_vma) s->filepos >= p->p_offset \
4052 && (bfd_vma) s->filepos + s->_raw_size \
4053 <= p->p_offset + p->p_filesz)
4055 /* The complicated case when p_vaddr is 0 is to handle the Solaris
4056 linker, which generates a PT_INTERP section with p_vaddr and
4057 p_memsz set to 0. */
4058 #define IS_SOLARIS_PT_INTERP(p, s) \
4059 ( p->p_vaddr == 0 \
4060 && p->p_filesz > 0 \
4061 && (s->flags & SEC_HAS_CONTENTS) != 0 \
4062 && s->_raw_size > 0 \
4063 && (bfd_vma) s->filepos >= p->p_offset \
4064 && ((bfd_vma) s->filepos + s->_raw_size \
4065 <= p->p_offset + p->p_filesz))
4067 /* Decide if the given section should be included in the given segment.
4068 A section will be included if:
4069 1. It is within the address space of the segment -- we use the LMA
4070 if that is set for the segment and the VMA otherwise,
4071 2. It is an allocated segment,
4072 3. There is an output section associated with it,
4073 4. The section has not already been allocated to a previous segment. */
4074 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
4075 (((((segment->p_paddr \
4076 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \
4077 : IS_CONTAINED_BY_VMA (section, segment)) \
4078 || IS_SOLARIS_PT_INTERP (segment, section)) \
4079 && (section->flags & SEC_ALLOC) != 0) \
4080 || IS_COREFILE_NOTE (segment, section)) \
4081 && section->output_section != NULL \
4082 && section->segment_mark == false)
4084 /* Returns true iff seg1 starts after the end of seg2. */
4085 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
4086 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
4088 /* Returns true iff seg1 and seg2 overlap. */
4089 #define SEGMENT_OVERLAPS(seg1, seg2) \
4090 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
4092 /* Initialise the segment mark field. */
4093 for (section = ibfd->sections; section != NULL; section = section->next)
4094 section->segment_mark = false;
4096 /* Scan through the segments specified in the program header
4097 of the input BFD. For this first scan we look for overlaps
4098 in the loadable segments. These can be created by wierd
4099 parameters to objcopy. */
4100 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4101 i < num_segments;
4102 i++, segment++)
4104 unsigned int j;
4105 Elf_Internal_Phdr *segment2;
4107 if (segment->p_type != PT_LOAD)
4108 continue;
4110 /* Determine if this segment overlaps any previous segments. */
4111 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
4113 bfd_signed_vma extra_length;
4115 if (segment2->p_type != PT_LOAD
4116 || ! SEGMENT_OVERLAPS (segment, segment2))
4117 continue;
4119 /* Merge the two segments together. */
4120 if (segment2->p_vaddr < segment->p_vaddr)
4122 /* Extend SEGMENT2 to include SEGMENT and then delete
4123 SEGMENT. */
4124 extra_length =
4125 SEGMENT_END (segment, segment->p_vaddr)
4126 - SEGMENT_END (segment2, segment2->p_vaddr);
4128 if (extra_length > 0)
4130 segment2->p_memsz += extra_length;
4131 segment2->p_filesz += extra_length;
4134 segment->p_type = PT_NULL;
4136 /* Since we have deleted P we must restart the outer loop. */
4137 i = 0;
4138 segment = elf_tdata (ibfd)->phdr;
4139 break;
4141 else
4143 /* Extend SEGMENT to include SEGMENT2 and then delete
4144 SEGMENT2. */
4145 extra_length =
4146 SEGMENT_END (segment2, segment2->p_vaddr)
4147 - SEGMENT_END (segment, segment->p_vaddr);
4149 if (extra_length > 0)
4151 segment->p_memsz += extra_length;
4152 segment->p_filesz += extra_length;
4155 segment2->p_type = PT_NULL;
4160 /* The second scan attempts to assign sections to segments. */
4161 for (i = 0, segment = elf_tdata (ibfd)->phdr;
4162 i < num_segments;
4163 i ++, segment ++)
4165 unsigned int section_count;
4166 asection ** sections;
4167 asection * output_section;
4168 unsigned int isec;
4169 bfd_vma matching_lma;
4170 bfd_vma suggested_lma;
4171 unsigned int j;
4172 bfd_size_type amt;
4174 if (segment->p_type == PT_NULL)
4175 continue;
4177 /* Compute how many sections might be placed into this segment. */
4178 section_count = 0;
4179 for (section = ibfd->sections; section != NULL; section = section->next)
4180 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4181 ++section_count;
4183 /* Allocate a segment map big enough to contain all of the
4184 sections we have selected. */
4185 amt = sizeof (struct elf_segment_map);
4186 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4187 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4188 if (map == NULL)
4189 return false;
4191 /* Initialise the fields of the segment map. Default to
4192 using the physical address of the segment in the input BFD. */
4193 map->next = NULL;
4194 map->p_type = segment->p_type;
4195 map->p_flags = segment->p_flags;
4196 map->p_flags_valid = 1;
4197 map->p_paddr = segment->p_paddr;
4198 map->p_paddr_valid = 1;
4200 /* Determine if this segment contains the ELF file header
4201 and if it contains the program headers themselves. */
4202 map->includes_filehdr = (segment->p_offset == 0
4203 && segment->p_filesz >= iehdr->e_ehsize);
4205 map->includes_phdrs = 0;
4207 if (! phdr_included || segment->p_type != PT_LOAD)
4209 map->includes_phdrs =
4210 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
4211 && (segment->p_offset + segment->p_filesz
4212 >= ((bfd_vma) iehdr->e_phoff
4213 + iehdr->e_phnum * iehdr->e_phentsize)));
4215 if (segment->p_type == PT_LOAD && map->includes_phdrs)
4216 phdr_included = true;
4219 if (section_count == 0)
4221 /* Special segments, such as the PT_PHDR segment, may contain
4222 no sections, but ordinary, loadable segments should contain
4223 something. */
4224 if (segment->p_type == PT_LOAD)
4225 _bfd_error_handler
4226 (_("%s: warning: Empty loadable segment detected\n"),
4227 bfd_archive_filename (ibfd));
4229 map->count = 0;
4230 *pointer_to_map = map;
4231 pointer_to_map = &map->next;
4233 continue;
4236 /* Now scan the sections in the input BFD again and attempt
4237 to add their corresponding output sections to the segment map.
4238 The problem here is how to handle an output section which has
4239 been moved (ie had its LMA changed). There are four possibilities:
4241 1. None of the sections have been moved.
4242 In this case we can continue to use the segment LMA from the
4243 input BFD.
4245 2. All of the sections have been moved by the same amount.
4246 In this case we can change the segment's LMA to match the LMA
4247 of the first section.
4249 3. Some of the sections have been moved, others have not.
4250 In this case those sections which have not been moved can be
4251 placed in the current segment which will have to have its size,
4252 and possibly its LMA changed, and a new segment or segments will
4253 have to be created to contain the other sections.
4255 4. The sections have been moved, but not be the same amount.
4256 In this case we can change the segment's LMA to match the LMA
4257 of the first section and we will have to create a new segment
4258 or segments to contain the other sections.
4260 In order to save time, we allocate an array to hold the section
4261 pointers that we are interested in. As these sections get assigned
4262 to a segment, they are removed from this array. */
4264 amt = (bfd_size_type) section_count * sizeof (asection *);
4265 sections = (asection **) bfd_malloc (amt);
4266 if (sections == NULL)
4267 return false;
4269 /* Step One: Scan for segment vs section LMA conflicts.
4270 Also add the sections to the section array allocated above.
4271 Also add the sections to the current segment. In the common
4272 case, where the sections have not been moved, this means that
4273 we have completely filled the segment, and there is nothing
4274 more to do. */
4275 isec = 0;
4276 matching_lma = 0;
4277 suggested_lma = 0;
4279 for (j = 0, section = ibfd->sections;
4280 section != NULL;
4281 section = section->next)
4283 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
4285 output_section = section->output_section;
4287 sections[j ++] = section;
4289 /* The Solaris native linker always sets p_paddr to 0.
4290 We try to catch that case here, and set it to the
4291 correct value. */
4292 if (segment->p_paddr == 0
4293 && segment->p_vaddr != 0
4294 && isec == 0
4295 && output_section->lma != 0
4296 && (output_section->vma == (segment->p_vaddr
4297 + (map->includes_filehdr
4298 ? iehdr->e_ehsize
4299 : 0)
4300 + (map->includes_phdrs
4301 ? (iehdr->e_phnum
4302 * iehdr->e_phentsize)
4303 : 0))))
4304 map->p_paddr = segment->p_vaddr;
4306 /* Match up the physical address of the segment with the
4307 LMA address of the output section. */
4308 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4309 || IS_COREFILE_NOTE (segment, section))
4311 if (matching_lma == 0)
4312 matching_lma = output_section->lma;
4314 /* We assume that if the section fits within the segment
4315 then it does not overlap any other section within that
4316 segment. */
4317 map->sections[isec ++] = output_section;
4319 else if (suggested_lma == 0)
4320 suggested_lma = output_section->lma;
4324 BFD_ASSERT (j == section_count);
4326 /* Step Two: Adjust the physical address of the current segment,
4327 if necessary. */
4328 if (isec == section_count)
4330 /* All of the sections fitted within the segment as currently
4331 specified. This is the default case. Add the segment to
4332 the list of built segments and carry on to process the next
4333 program header in the input BFD. */
4334 map->count = section_count;
4335 *pointer_to_map = map;
4336 pointer_to_map = &map->next;
4338 free (sections);
4339 continue;
4341 else
4343 if (matching_lma != 0)
4345 /* At least one section fits inside the current segment.
4346 Keep it, but modify its physical address to match the
4347 LMA of the first section that fitted. */
4348 map->p_paddr = matching_lma;
4350 else
4352 /* None of the sections fitted inside the current segment.
4353 Change the current segment's physical address to match
4354 the LMA of the first section. */
4355 map->p_paddr = suggested_lma;
4358 /* Offset the segment physical address from the lma
4359 to allow for space taken up by elf headers. */
4360 if (map->includes_filehdr)
4361 map->p_paddr -= iehdr->e_ehsize;
4363 if (map->includes_phdrs)
4365 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4367 /* iehdr->e_phnum is just an estimate of the number
4368 of program headers that we will need. Make a note
4369 here of the number we used and the segment we chose
4370 to hold these headers, so that we can adjust the
4371 offset when we know the correct value. */
4372 phdr_adjust_num = iehdr->e_phnum;
4373 phdr_adjust_seg = map;
4377 /* Step Three: Loop over the sections again, this time assigning
4378 those that fit to the current segment and remvoing them from the
4379 sections array; but making sure not to leave large gaps. Once all
4380 possible sections have been assigned to the current segment it is
4381 added to the list of built segments and if sections still remain
4382 to be assigned, a new segment is constructed before repeating
4383 the loop. */
4384 isec = 0;
4387 map->count = 0;
4388 suggested_lma = 0;
4390 /* Fill the current segment with sections that fit. */
4391 for (j = 0; j < section_count; j++)
4393 section = sections[j];
4395 if (section == NULL)
4396 continue;
4398 output_section = section->output_section;
4400 BFD_ASSERT (output_section != NULL);
4402 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4403 || IS_COREFILE_NOTE (segment, section))
4405 if (map->count == 0)
4407 /* If the first section in a segment does not start at
4408 the beginning of the segment, then something is
4409 wrong. */
4410 if (output_section->lma !=
4411 (map->p_paddr
4412 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4413 + (map->includes_phdrs
4414 ? iehdr->e_phnum * iehdr->e_phentsize
4415 : 0)))
4416 abort ();
4418 else
4420 asection * prev_sec;
4422 prev_sec = map->sections[map->count - 1];
4424 /* If the gap between the end of the previous section
4425 and the start of this section is more than
4426 maxpagesize then we need to start a new segment. */
4427 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size,
4428 maxpagesize)
4429 < BFD_ALIGN (output_section->lma, maxpagesize))
4430 || ((prev_sec->lma + prev_sec->_raw_size)
4431 > output_section->lma))
4433 if (suggested_lma == 0)
4434 suggested_lma = output_section->lma;
4436 continue;
4440 map->sections[map->count++] = output_section;
4441 ++isec;
4442 sections[j] = NULL;
4443 section->segment_mark = true;
4445 else if (suggested_lma == 0)
4446 suggested_lma = output_section->lma;
4449 BFD_ASSERT (map->count > 0);
4451 /* Add the current segment to the list of built segments. */
4452 *pointer_to_map = map;
4453 pointer_to_map = &map->next;
4455 if (isec < section_count)
4457 /* We still have not allocated all of the sections to
4458 segments. Create a new segment here, initialise it
4459 and carry on looping. */
4460 amt = sizeof (struct elf_segment_map);
4461 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *);
4462 map = (struct elf_segment_map *) bfd_alloc (obfd, amt);
4463 if (map == NULL)
4464 return false;
4466 /* Initialise the fields of the segment map. Set the physical
4467 physical address to the LMA of the first section that has
4468 not yet been assigned. */
4469 map->next = NULL;
4470 map->p_type = segment->p_type;
4471 map->p_flags = segment->p_flags;
4472 map->p_flags_valid = 1;
4473 map->p_paddr = suggested_lma;
4474 map->p_paddr_valid = 1;
4475 map->includes_filehdr = 0;
4476 map->includes_phdrs = 0;
4479 while (isec < section_count);
4481 free (sections);
4484 /* The Solaris linker creates program headers in which all the
4485 p_paddr fields are zero. When we try to objcopy or strip such a
4486 file, we get confused. Check for this case, and if we find it
4487 reset the p_paddr_valid fields. */
4488 for (map = map_first; map != NULL; map = map->next)
4489 if (map->p_paddr != 0)
4490 break;
4491 if (map == NULL)
4493 for (map = map_first; map != NULL; map = map->next)
4494 map->p_paddr_valid = 0;
4497 elf_tdata (obfd)->segment_map = map_first;
4499 /* If we had to estimate the number of program headers that were
4500 going to be needed, then check our estimate know and adjust
4501 the offset if necessary. */
4502 if (phdr_adjust_seg != NULL)
4504 unsigned int count;
4506 for (count = 0, map = map_first; map != NULL; map = map->next)
4507 count++;
4509 if (count > phdr_adjust_num)
4510 phdr_adjust_seg->p_paddr
4511 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4514 #if 0
4515 /* Final Step: Sort the segments into ascending order of physical
4516 address. */
4517 if (map_first != NULL)
4519 struct elf_segment_map *prev;
4521 prev = map_first;
4522 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4524 /* Yes I know - its a bubble sort.... */
4525 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4527 /* Swap map and map->next. */
4528 prev->next = map->next;
4529 map->next = map->next->next;
4530 prev->next->next = map;
4532 /* Restart loop. */
4533 map = map_first;
4537 #endif
4539 #undef SEGMENT_END
4540 #undef IS_CONTAINED_BY_VMA
4541 #undef IS_CONTAINED_BY_LMA
4542 #undef IS_COREFILE_NOTE
4543 #undef IS_SOLARIS_PT_INTERP
4544 #undef INCLUDE_SECTION_IN_SEGMENT
4545 #undef SEGMENT_AFTER_SEGMENT
4546 #undef SEGMENT_OVERLAPS
4547 return true;
4550 /* Copy private section information. This copies over the entsize
4551 field, and sometimes the info field. */
4553 boolean
4554 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4555 bfd *ibfd;
4556 asection *isec;
4557 bfd *obfd;
4558 asection *osec;
4560 Elf_Internal_Shdr *ihdr, *ohdr;
4562 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4563 || obfd->xvec->flavour != bfd_target_elf_flavour)
4564 return true;
4566 /* Copy over private BFD data if it has not already been copied.
4567 This must be done here, rather than in the copy_private_bfd_data
4568 entry point, because the latter is called after the section
4569 contents have been set, which means that the program headers have
4570 already been worked out. */
4571 if (elf_tdata (obfd)->segment_map == NULL
4572 && elf_tdata (ibfd)->phdr != NULL)
4574 asection *s;
4576 /* Only set up the segments if there are no more SEC_ALLOC
4577 sections. FIXME: This won't do the right thing if objcopy is
4578 used to remove the last SEC_ALLOC section, since objcopy
4579 won't call this routine in that case. */
4580 for (s = isec->next; s != NULL; s = s->next)
4581 if ((s->flags & SEC_ALLOC) != 0)
4582 break;
4583 if (s == NULL)
4585 if (! copy_private_bfd_data (ibfd, obfd))
4586 return false;
4590 ihdr = &elf_section_data (isec)->this_hdr;
4591 ohdr = &elf_section_data (osec)->this_hdr;
4593 ohdr->sh_entsize = ihdr->sh_entsize;
4595 if (ihdr->sh_type == SHT_SYMTAB
4596 || ihdr->sh_type == SHT_DYNSYM
4597 || ihdr->sh_type == SHT_GNU_verneed
4598 || ihdr->sh_type == SHT_GNU_verdef)
4599 ohdr->sh_info = ihdr->sh_info;
4601 elf_section_data (osec)->use_rela_p
4602 = elf_section_data (isec)->use_rela_p;
4604 return true;
4607 /* Copy private symbol information. If this symbol is in a section
4608 which we did not map into a BFD section, try to map the section
4609 index correctly. We use special macro definitions for the mapped
4610 section indices; these definitions are interpreted by the
4611 swap_out_syms function. */
4613 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4614 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4615 #define MAP_STRTAB (SHN_LORESERVE - 3)
4616 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4618 boolean
4619 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4620 bfd *ibfd;
4621 asymbol *isymarg;
4622 bfd *obfd;
4623 asymbol *osymarg;
4625 elf_symbol_type *isym, *osym;
4627 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4628 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4629 return true;
4631 isym = elf_symbol_from (ibfd, isymarg);
4632 osym = elf_symbol_from (obfd, osymarg);
4634 if (isym != NULL
4635 && osym != NULL
4636 && bfd_is_abs_section (isym->symbol.section))
4638 unsigned int shndx;
4640 shndx = isym->internal_elf_sym.st_shndx;
4641 if (shndx == elf_onesymtab (ibfd))
4642 shndx = MAP_ONESYMTAB;
4643 else if (shndx == elf_dynsymtab (ibfd))
4644 shndx = MAP_DYNSYMTAB;
4645 else if (shndx == elf_tdata (ibfd)->strtab_section)
4646 shndx = MAP_STRTAB;
4647 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4648 shndx = MAP_SHSTRTAB;
4649 osym->internal_elf_sym.st_shndx = shndx;
4652 return true;
4655 /* Swap out the symbols. */
4657 static boolean
4658 swap_out_syms (abfd, sttp, relocatable_p)
4659 bfd *abfd;
4660 struct bfd_strtab_hash **sttp;
4661 int relocatable_p;
4663 struct elf_backend_data *bed;
4664 int symcount;
4665 asymbol **syms;
4666 struct bfd_strtab_hash *stt;
4667 Elf_Internal_Shdr *symtab_hdr;
4668 Elf_Internal_Shdr *symstrtab_hdr;
4669 char *outbound_syms;
4670 int idx;
4671 bfd_size_type amt;
4673 if (!elf_map_symbols (abfd))
4674 return false;
4676 /* Dump out the symtabs. */
4677 stt = _bfd_elf_stringtab_init ();
4678 if (stt == NULL)
4679 return false;
4681 bed = get_elf_backend_data (abfd);
4682 symcount = bfd_get_symcount (abfd);
4683 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4684 symtab_hdr->sh_type = SHT_SYMTAB;
4685 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4686 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4687 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4688 symtab_hdr->sh_addralign = bed->s->file_align;
4690 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4691 symstrtab_hdr->sh_type = SHT_STRTAB;
4693 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym;
4694 outbound_syms = bfd_alloc (abfd, amt);
4695 if (outbound_syms == NULL)
4696 return false;
4697 symtab_hdr->contents = (PTR) outbound_syms;
4699 /* now generate the data (for "contents") */
4701 /* Fill in zeroth symbol and swap it out. */
4702 Elf_Internal_Sym sym;
4703 sym.st_name = 0;
4704 sym.st_value = 0;
4705 sym.st_size = 0;
4706 sym.st_info = 0;
4707 sym.st_other = 0;
4708 sym.st_shndx = SHN_UNDEF;
4709 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4710 outbound_syms += bed->s->sizeof_sym;
4713 syms = bfd_get_outsymbols (abfd);
4714 for (idx = 0; idx < symcount; idx++)
4716 Elf_Internal_Sym sym;
4717 bfd_vma value = syms[idx]->value;
4718 elf_symbol_type *type_ptr;
4719 flagword flags = syms[idx]->flags;
4720 int type;
4722 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4724 /* Local section symbols have no name. */
4725 sym.st_name = 0;
4727 else
4729 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4730 syms[idx]->name,
4731 true, false);
4732 if (sym.st_name == (unsigned long) -1)
4733 return false;
4736 type_ptr = elf_symbol_from (abfd, syms[idx]);
4738 if ((flags & BSF_SECTION_SYM) == 0
4739 && bfd_is_com_section (syms[idx]->section))
4741 /* ELF common symbols put the alignment into the `value' field,
4742 and the size into the `size' field. This is backwards from
4743 how BFD handles it, so reverse it here. */
4744 sym.st_size = value;
4745 if (type_ptr == NULL
4746 || type_ptr->internal_elf_sym.st_value == 0)
4747 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4748 else
4749 sym.st_value = type_ptr->internal_elf_sym.st_value;
4750 sym.st_shndx = _bfd_elf_section_from_bfd_section
4751 (abfd, syms[idx]->section);
4753 else
4755 asection *sec = syms[idx]->section;
4756 int shndx;
4758 if (sec->output_section)
4760 value += sec->output_offset;
4761 sec = sec->output_section;
4763 /* Don't add in the section vma for relocatable output. */
4764 if (! relocatable_p)
4765 value += sec->vma;
4766 sym.st_value = value;
4767 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4769 if (bfd_is_abs_section (sec)
4770 && type_ptr != NULL
4771 && type_ptr->internal_elf_sym.st_shndx != 0)
4773 /* This symbol is in a real ELF section which we did
4774 not create as a BFD section. Undo the mapping done
4775 by copy_private_symbol_data. */
4776 shndx = type_ptr->internal_elf_sym.st_shndx;
4777 switch (shndx)
4779 case MAP_ONESYMTAB:
4780 shndx = elf_onesymtab (abfd);
4781 break;
4782 case MAP_DYNSYMTAB:
4783 shndx = elf_dynsymtab (abfd);
4784 break;
4785 case MAP_STRTAB:
4786 shndx = elf_tdata (abfd)->strtab_section;
4787 break;
4788 case MAP_SHSTRTAB:
4789 shndx = elf_tdata (abfd)->shstrtab_section;
4790 break;
4791 default:
4792 break;
4795 else
4797 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4799 if (shndx == -1)
4801 asection *sec2;
4803 /* Writing this would be a hell of a lot easier if
4804 we had some decent documentation on bfd, and
4805 knew what to expect of the library, and what to
4806 demand of applications. For example, it
4807 appears that `objcopy' might not set the
4808 section of a symbol to be a section that is
4809 actually in the output file. */
4810 sec2 = bfd_get_section_by_name (abfd, sec->name);
4811 BFD_ASSERT (sec2 != 0);
4812 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4813 BFD_ASSERT (shndx != -1);
4817 sym.st_shndx = shndx;
4820 if ((flags & BSF_FUNCTION) != 0)
4821 type = STT_FUNC;
4822 else if ((flags & BSF_OBJECT) != 0)
4823 type = STT_OBJECT;
4824 else
4825 type = STT_NOTYPE;
4827 /* Processor-specific types */
4828 if (type_ptr != NULL
4829 && bed->elf_backend_get_symbol_type)
4830 type = ((*bed->elf_backend_get_symbol_type)
4831 (&type_ptr->internal_elf_sym, type));
4833 if (flags & BSF_SECTION_SYM)
4835 if (flags & BSF_GLOBAL)
4836 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
4837 else
4838 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4840 else if (bfd_is_com_section (syms[idx]->section))
4841 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4842 else if (bfd_is_und_section (syms[idx]->section))
4843 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4844 ? STB_WEAK
4845 : STB_GLOBAL),
4846 type);
4847 else if (flags & BSF_FILE)
4848 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4849 else
4851 int bind = STB_LOCAL;
4853 if (flags & BSF_LOCAL)
4854 bind = STB_LOCAL;
4855 else if (flags & BSF_WEAK)
4856 bind = STB_WEAK;
4857 else if (flags & BSF_GLOBAL)
4858 bind = STB_GLOBAL;
4860 sym.st_info = ELF_ST_INFO (bind, type);
4863 if (type_ptr != NULL)
4864 sym.st_other = type_ptr->internal_elf_sym.st_other;
4865 else
4866 sym.st_other = 0;
4868 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4869 outbound_syms += bed->s->sizeof_sym;
4872 *sttp = stt;
4873 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4874 symstrtab_hdr->sh_type = SHT_STRTAB;
4876 symstrtab_hdr->sh_flags = 0;
4877 symstrtab_hdr->sh_addr = 0;
4878 symstrtab_hdr->sh_entsize = 0;
4879 symstrtab_hdr->sh_link = 0;
4880 symstrtab_hdr->sh_info = 0;
4881 symstrtab_hdr->sh_addralign = 1;
4883 return true;
4886 /* Return the number of bytes required to hold the symtab vector.
4888 Note that we base it on the count plus 1, since we will null terminate
4889 the vector allocated based on this size. However, the ELF symbol table
4890 always has a dummy entry as symbol #0, so it ends up even. */
4892 long
4893 _bfd_elf_get_symtab_upper_bound (abfd)
4894 bfd *abfd;
4896 long symcount;
4897 long symtab_size;
4898 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4900 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4901 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4903 return symtab_size;
4906 long
4907 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4908 bfd *abfd;
4910 long symcount;
4911 long symtab_size;
4912 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4914 if (elf_dynsymtab (abfd) == 0)
4916 bfd_set_error (bfd_error_invalid_operation);
4917 return -1;
4920 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4921 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4923 return symtab_size;
4926 long
4927 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4928 bfd *abfd ATTRIBUTE_UNUSED;
4929 sec_ptr asect;
4931 return (asect->reloc_count + 1) * sizeof (arelent *);
4934 /* Canonicalize the relocs. */
4936 long
4937 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4938 bfd *abfd;
4939 sec_ptr section;
4940 arelent **relptr;
4941 asymbol **symbols;
4943 arelent *tblptr;
4944 unsigned int i;
4945 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4947 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false))
4948 return -1;
4950 tblptr = section->relocation;
4951 for (i = 0; i < section->reloc_count; i++)
4952 *relptr++ = tblptr++;
4954 *relptr = NULL;
4956 return section->reloc_count;
4959 long
4960 _bfd_elf_get_symtab (abfd, alocation)
4961 bfd *abfd;
4962 asymbol **alocation;
4964 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4965 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false);
4967 if (symcount >= 0)
4968 bfd_get_symcount (abfd) = symcount;
4969 return symcount;
4972 long
4973 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4974 bfd *abfd;
4975 asymbol **alocation;
4977 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4978 return bed->s->slurp_symbol_table (abfd, alocation, true);
4981 /* Return the size required for the dynamic reloc entries. Any
4982 section that was actually installed in the BFD, and has type
4983 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4984 considered to be a dynamic reloc section. */
4986 long
4987 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4988 bfd *abfd;
4990 long ret;
4991 asection *s;
4993 if (elf_dynsymtab (abfd) == 0)
4995 bfd_set_error (bfd_error_invalid_operation);
4996 return -1;
4999 ret = sizeof (arelent *);
5000 for (s = abfd->sections; s != NULL; s = s->next)
5001 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5002 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5003 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5004 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
5005 * sizeof (arelent *));
5007 return ret;
5010 /* Canonicalize the dynamic relocation entries. Note that we return
5011 the dynamic relocations as a single block, although they are
5012 actually associated with particular sections; the interface, which
5013 was designed for SunOS style shared libraries, expects that there
5014 is only one set of dynamic relocs. Any section that was actually
5015 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
5016 the dynamic symbol table, is considered to be a dynamic reloc
5017 section. */
5019 long
5020 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
5021 bfd *abfd;
5022 arelent **storage;
5023 asymbol **syms;
5025 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
5026 asection *s;
5027 long ret;
5029 if (elf_dynsymtab (abfd) == 0)
5031 bfd_set_error (bfd_error_invalid_operation);
5032 return -1;
5035 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
5036 ret = 0;
5037 for (s = abfd->sections; s != NULL; s = s->next)
5039 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
5040 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
5041 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
5043 arelent *p;
5044 long count, i;
5046 if (! (*slurp_relocs) (abfd, s, syms, true))
5047 return -1;
5048 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
5049 p = s->relocation;
5050 for (i = 0; i < count; i++)
5051 *storage++ = p++;
5052 ret += count;
5056 *storage = NULL;
5058 return ret;
5061 /* Read in the version information. */
5063 boolean
5064 _bfd_elf_slurp_version_tables (abfd)
5065 bfd *abfd;
5067 bfd_byte *contents = NULL;
5068 bfd_size_type amt;
5070 if (elf_dynverdef (abfd) != 0)
5072 Elf_Internal_Shdr *hdr;
5073 Elf_External_Verdef *everdef;
5074 Elf_Internal_Verdef *iverdef;
5075 Elf_Internal_Verdef *iverdefarr;
5076 Elf_Internal_Verdef iverdefmem;
5077 unsigned int i;
5078 unsigned int maxidx;
5080 hdr = &elf_tdata (abfd)->dynverdef_hdr;
5082 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5083 if (contents == NULL)
5084 goto error_return;
5085 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5086 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5087 goto error_return;
5089 /* We know the number of entries in the section but not the maximum
5090 index. Therefore we have to run through all entries and find
5091 the maximum. */
5092 everdef = (Elf_External_Verdef *) contents;
5093 maxidx = 0;
5094 for (i = 0; i < hdr->sh_info; ++i)
5096 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5098 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
5099 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
5101 everdef = ((Elf_External_Verdef *)
5102 ((bfd_byte *) everdef + iverdefmem.vd_next));
5105 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef);
5106 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt);
5107 if (elf_tdata (abfd)->verdef == NULL)
5108 goto error_return;
5110 elf_tdata (abfd)->cverdefs = maxidx;
5112 everdef = (Elf_External_Verdef *) contents;
5113 iverdefarr = elf_tdata (abfd)->verdef;
5114 for (i = 0; i < hdr->sh_info; i++)
5116 Elf_External_Verdaux *everdaux;
5117 Elf_Internal_Verdaux *iverdaux;
5118 unsigned int j;
5120 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
5122 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
5123 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
5125 iverdef->vd_bfd = abfd;
5127 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux);
5128 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt);
5129 if (iverdef->vd_auxptr == NULL)
5130 goto error_return;
5132 everdaux = ((Elf_External_Verdaux *)
5133 ((bfd_byte *) everdef + iverdef->vd_aux));
5134 iverdaux = iverdef->vd_auxptr;
5135 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
5137 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
5139 iverdaux->vda_nodename =
5140 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5141 iverdaux->vda_name);
5142 if (iverdaux->vda_nodename == NULL)
5143 goto error_return;
5145 if (j + 1 < iverdef->vd_cnt)
5146 iverdaux->vda_nextptr = iverdaux + 1;
5147 else
5148 iverdaux->vda_nextptr = NULL;
5150 everdaux = ((Elf_External_Verdaux *)
5151 ((bfd_byte *) everdaux + iverdaux->vda_next));
5154 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
5156 if (i + 1 < hdr->sh_info)
5157 iverdef->vd_nextdef = iverdef + 1;
5158 else
5159 iverdef->vd_nextdef = NULL;
5161 everdef = ((Elf_External_Verdef *)
5162 ((bfd_byte *) everdef + iverdef->vd_next));
5165 free (contents);
5166 contents = NULL;
5169 if (elf_dynverref (abfd) != 0)
5171 Elf_Internal_Shdr *hdr;
5172 Elf_External_Verneed *everneed;
5173 Elf_Internal_Verneed *iverneed;
5174 unsigned int i;
5176 hdr = &elf_tdata (abfd)->dynverref_hdr;
5178 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed);
5179 elf_tdata (abfd)->verref =
5180 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt);
5181 if (elf_tdata (abfd)->verref == NULL)
5182 goto error_return;
5184 elf_tdata (abfd)->cverrefs = hdr->sh_info;
5186 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
5187 if (contents == NULL)
5188 goto error_return;
5189 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
5190 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size)
5191 goto error_return;
5193 everneed = (Elf_External_Verneed *) contents;
5194 iverneed = elf_tdata (abfd)->verref;
5195 for (i = 0; i < hdr->sh_info; i++, iverneed++)
5197 Elf_External_Vernaux *evernaux;
5198 Elf_Internal_Vernaux *ivernaux;
5199 unsigned int j;
5201 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
5203 iverneed->vn_bfd = abfd;
5205 iverneed->vn_filename =
5206 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5207 iverneed->vn_file);
5208 if (iverneed->vn_filename == NULL)
5209 goto error_return;
5211 amt = iverneed->vn_cnt;
5212 amt *= sizeof (Elf_Internal_Vernaux);
5213 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt);
5215 evernaux = ((Elf_External_Vernaux *)
5216 ((bfd_byte *) everneed + iverneed->vn_aux));
5217 ivernaux = iverneed->vn_auxptr;
5218 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
5220 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
5222 ivernaux->vna_nodename =
5223 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
5224 ivernaux->vna_name);
5225 if (ivernaux->vna_nodename == NULL)
5226 goto error_return;
5228 if (j + 1 < iverneed->vn_cnt)
5229 ivernaux->vna_nextptr = ivernaux + 1;
5230 else
5231 ivernaux->vna_nextptr = NULL;
5233 evernaux = ((Elf_External_Vernaux *)
5234 ((bfd_byte *) evernaux + ivernaux->vna_next));
5237 if (i + 1 < hdr->sh_info)
5238 iverneed->vn_nextref = iverneed + 1;
5239 else
5240 iverneed->vn_nextref = NULL;
5242 everneed = ((Elf_External_Verneed *)
5243 ((bfd_byte *) everneed + iverneed->vn_next));
5246 free (contents);
5247 contents = NULL;
5250 return true;
5252 error_return:
5253 if (contents == NULL)
5254 free (contents);
5255 return false;
5258 asymbol *
5259 _bfd_elf_make_empty_symbol (abfd)
5260 bfd *abfd;
5262 elf_symbol_type *newsym;
5263 bfd_size_type amt = sizeof (elf_symbol_type);
5265 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt);
5266 if (!newsym)
5267 return NULL;
5268 else
5270 newsym->symbol.the_bfd = abfd;
5271 return &newsym->symbol;
5275 void
5276 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
5277 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5278 asymbol *symbol;
5279 symbol_info *ret;
5281 bfd_symbol_info (symbol, ret);
5284 /* Return whether a symbol name implies a local symbol. Most targets
5285 use this function for the is_local_label_name entry point, but some
5286 override it. */
5288 boolean
5289 _bfd_elf_is_local_label_name (abfd, name)
5290 bfd *abfd ATTRIBUTE_UNUSED;
5291 const char *name;
5293 /* Normal local symbols start with ``.L''. */
5294 if (name[0] == '.' && name[1] == 'L')
5295 return true;
5297 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
5298 DWARF debugging symbols starting with ``..''. */
5299 if (name[0] == '.' && name[1] == '.')
5300 return true;
5302 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
5303 emitting DWARF debugging output. I suspect this is actually a
5304 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
5305 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
5306 underscore to be emitted on some ELF targets). For ease of use,
5307 we treat such symbols as local. */
5308 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
5309 return true;
5311 return false;
5314 alent *
5315 _bfd_elf_get_lineno (ignore_abfd, symbol)
5316 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5317 asymbol *symbol ATTRIBUTE_UNUSED;
5319 abort ();
5320 return NULL;
5323 boolean
5324 _bfd_elf_set_arch_mach (abfd, arch, machine)
5325 bfd *abfd;
5326 enum bfd_architecture arch;
5327 unsigned long machine;
5329 /* If this isn't the right architecture for this backend, and this
5330 isn't the generic backend, fail. */
5331 if (arch != get_elf_backend_data (abfd)->arch
5332 && arch != bfd_arch_unknown
5333 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5334 return false;
5336 return bfd_default_set_arch_mach (abfd, arch, machine);
5339 /* Find the function to a particular section and offset,
5340 for error reporting. */
5342 static boolean
5343 elf_find_function (abfd, section, symbols, offset,
5344 filename_ptr, functionname_ptr)
5345 bfd *abfd ATTRIBUTE_UNUSED;
5346 asection *section;
5347 asymbol **symbols;
5348 bfd_vma offset;
5349 const char **filename_ptr;
5350 const char **functionname_ptr;
5352 const char *filename;
5353 asymbol *func;
5354 bfd_vma low_func;
5355 asymbol **p;
5357 filename = NULL;
5358 func = NULL;
5359 low_func = 0;
5361 for (p = symbols; *p != NULL; p++)
5363 elf_symbol_type *q;
5365 q = (elf_symbol_type *) *p;
5367 if (bfd_get_section (&q->symbol) != section)
5368 continue;
5370 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5372 default:
5373 break;
5374 case STT_FILE:
5375 filename = bfd_asymbol_name (&q->symbol);
5376 break;
5377 case STT_NOTYPE:
5378 case STT_FUNC:
5379 if (q->symbol.section == section
5380 && q->symbol.value >= low_func
5381 && q->symbol.value <= offset)
5383 func = (asymbol *) q;
5384 low_func = q->symbol.value;
5386 break;
5390 if (func == NULL)
5391 return false;
5393 if (filename_ptr)
5394 *filename_ptr = filename;
5395 if (functionname_ptr)
5396 *functionname_ptr = bfd_asymbol_name (func);
5398 return true;
5401 /* Find the nearest line to a particular section and offset,
5402 for error reporting. */
5404 boolean
5405 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5406 filename_ptr, functionname_ptr, line_ptr)
5407 bfd *abfd;
5408 asection *section;
5409 asymbol **symbols;
5410 bfd_vma offset;
5411 const char **filename_ptr;
5412 const char **functionname_ptr;
5413 unsigned int *line_ptr;
5415 boolean found;
5417 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5418 filename_ptr, functionname_ptr,
5419 line_ptr))
5421 if (!*functionname_ptr)
5422 elf_find_function (abfd, section, symbols, offset,
5423 *filename_ptr ? NULL : filename_ptr,
5424 functionname_ptr);
5426 return true;
5429 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5430 filename_ptr, functionname_ptr,
5431 line_ptr, 0,
5432 &elf_tdata (abfd)->dwarf2_find_line_info))
5434 if (!*functionname_ptr)
5435 elf_find_function (abfd, section, symbols, offset,
5436 *filename_ptr ? NULL : filename_ptr,
5437 functionname_ptr);
5439 return true;
5442 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5443 &found, filename_ptr,
5444 functionname_ptr, line_ptr,
5445 &elf_tdata (abfd)->line_info))
5446 return false;
5447 if (found)
5448 return true;
5450 if (symbols == NULL)
5451 return false;
5453 if (! elf_find_function (abfd, section, symbols, offset,
5454 filename_ptr, functionname_ptr))
5455 return false;
5457 *line_ptr = 0;
5458 return true;
5462 _bfd_elf_sizeof_headers (abfd, reloc)
5463 bfd *abfd;
5464 boolean reloc;
5466 int ret;
5468 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5469 if (! reloc)
5470 ret += get_program_header_size (abfd);
5471 return ret;
5474 boolean
5475 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5476 bfd *abfd;
5477 sec_ptr section;
5478 PTR location;
5479 file_ptr offset;
5480 bfd_size_type count;
5482 Elf_Internal_Shdr *hdr;
5483 bfd_signed_vma pos;
5485 if (! abfd->output_has_begun
5486 && ! _bfd_elf_compute_section_file_positions
5487 (abfd, (struct bfd_link_info *) NULL))
5488 return false;
5490 hdr = &elf_section_data (section)->this_hdr;
5491 pos = hdr->sh_offset + offset;
5492 if (bfd_seek (abfd, pos, SEEK_SET) != 0
5493 || bfd_bwrite (location, count, abfd) != count)
5494 return false;
5496 return true;
5499 void
5500 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5501 bfd *abfd ATTRIBUTE_UNUSED;
5502 arelent *cache_ptr ATTRIBUTE_UNUSED;
5503 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5505 abort ();
5508 #if 0
5509 void
5510 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5511 bfd *abfd;
5512 arelent *cache_ptr;
5513 Elf_Internal_Rel *dst;
5515 abort ();
5517 #endif
5519 /* Try to convert a non-ELF reloc into an ELF one. */
5521 boolean
5522 _bfd_elf_validate_reloc (abfd, areloc)
5523 bfd *abfd;
5524 arelent *areloc;
5526 /* Check whether we really have an ELF howto. */
5528 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5530 bfd_reloc_code_real_type code;
5531 reloc_howto_type *howto;
5533 /* Alien reloc: Try to determine its type to replace it with an
5534 equivalent ELF reloc. */
5536 if (areloc->howto->pc_relative)
5538 switch (areloc->howto->bitsize)
5540 case 8:
5541 code = BFD_RELOC_8_PCREL;
5542 break;
5543 case 12:
5544 code = BFD_RELOC_12_PCREL;
5545 break;
5546 case 16:
5547 code = BFD_RELOC_16_PCREL;
5548 break;
5549 case 24:
5550 code = BFD_RELOC_24_PCREL;
5551 break;
5552 case 32:
5553 code = BFD_RELOC_32_PCREL;
5554 break;
5555 case 64:
5556 code = BFD_RELOC_64_PCREL;
5557 break;
5558 default:
5559 goto fail;
5562 howto = bfd_reloc_type_lookup (abfd, code);
5564 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5566 if (howto->pcrel_offset)
5567 areloc->addend += areloc->address;
5568 else
5569 areloc->addend -= areloc->address; /* addend is unsigned!! */
5572 else
5574 switch (areloc->howto->bitsize)
5576 case 8:
5577 code = BFD_RELOC_8;
5578 break;
5579 case 14:
5580 code = BFD_RELOC_14;
5581 break;
5582 case 16:
5583 code = BFD_RELOC_16;
5584 break;
5585 case 26:
5586 code = BFD_RELOC_26;
5587 break;
5588 case 32:
5589 code = BFD_RELOC_32;
5590 break;
5591 case 64:
5592 code = BFD_RELOC_64;
5593 break;
5594 default:
5595 goto fail;
5598 howto = bfd_reloc_type_lookup (abfd, code);
5601 if (howto)
5602 areloc->howto = howto;
5603 else
5604 goto fail;
5607 return true;
5609 fail:
5610 (*_bfd_error_handler)
5611 (_("%s: unsupported relocation type %s"),
5612 bfd_archive_filename (abfd), areloc->howto->name);
5613 bfd_set_error (bfd_error_bad_value);
5614 return false;
5617 boolean
5618 _bfd_elf_close_and_cleanup (abfd)
5619 bfd *abfd;
5621 if (bfd_get_format (abfd) == bfd_object)
5623 if (elf_shstrtab (abfd) != NULL)
5624 _bfd_elf_strtab_free (elf_shstrtab (abfd));
5627 return _bfd_generic_close_and_cleanup (abfd);
5630 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5631 in the relocation's offset. Thus we cannot allow any sort of sanity
5632 range-checking to interfere. There is nothing else to do in processing
5633 this reloc. */
5635 bfd_reloc_status_type
5636 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5637 bfd *abfd ATTRIBUTE_UNUSED;
5638 arelent *re ATTRIBUTE_UNUSED;
5639 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5640 PTR data ATTRIBUTE_UNUSED;
5641 asection *is ATTRIBUTE_UNUSED;
5642 bfd *obfd ATTRIBUTE_UNUSED;
5643 char **errmsg ATTRIBUTE_UNUSED;
5645 return bfd_reloc_ok;
5648 /* Elf core file support. Much of this only works on native
5649 toolchains, since we rely on knowing the
5650 machine-dependent procfs structure in order to pick
5651 out details about the corefile. */
5653 #ifdef HAVE_SYS_PROCFS_H
5654 # include <sys/procfs.h>
5655 #endif
5657 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5659 static int
5660 elfcore_make_pid (abfd)
5661 bfd *abfd;
5663 return ((elf_tdata (abfd)->core_lwpid << 16)
5664 + (elf_tdata (abfd)->core_pid));
5667 /* If there isn't a section called NAME, make one, using
5668 data from SECT. Note, this function will generate a
5669 reference to NAME, so you shouldn't deallocate or
5670 overwrite it. */
5672 static boolean
5673 elfcore_maybe_make_sect (abfd, name, sect)
5674 bfd *abfd;
5675 char *name;
5676 asection *sect;
5678 asection *sect2;
5680 if (bfd_get_section_by_name (abfd, name) != NULL)
5681 return true;
5683 sect2 = bfd_make_section (abfd, name);
5684 if (sect2 == NULL)
5685 return false;
5687 sect2->_raw_size = sect->_raw_size;
5688 sect2->filepos = sect->filepos;
5689 sect2->flags = sect->flags;
5690 sect2->alignment_power = sect->alignment_power;
5691 return true;
5694 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5695 actually creates up to two pseudosections:
5696 - For the single-threaded case, a section named NAME, unless
5697 such a section already exists.
5698 - For the multi-threaded case, a section named "NAME/PID", where
5699 PID is elfcore_make_pid (abfd).
5700 Both pseudosections have identical contents. */
5701 boolean
5702 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5703 bfd *abfd;
5704 char *name;
5705 size_t size;
5706 ufile_ptr filepos;
5708 char buf[100];
5709 char *threaded_name;
5710 asection *sect;
5712 /* Build the section name. */
5714 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5715 threaded_name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
5716 if (threaded_name == NULL)
5717 return false;
5718 strcpy (threaded_name, buf);
5720 sect = bfd_make_section (abfd, threaded_name);
5721 if (sect == NULL)
5722 return false;
5723 sect->_raw_size = size;
5724 sect->filepos = filepos;
5725 sect->flags = SEC_HAS_CONTENTS;
5726 sect->alignment_power = 2;
5728 return elfcore_maybe_make_sect (abfd, name, sect);
5731 /* prstatus_t exists on:
5732 solaris 2.5+
5733 linux 2.[01] + glibc
5734 unixware 4.2
5737 #if defined (HAVE_PRSTATUS_T)
5738 static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *));
5740 static boolean
5741 elfcore_grok_prstatus (abfd, note)
5742 bfd *abfd;
5743 Elf_Internal_Note *note;
5745 size_t raw_size;
5746 int offset;
5748 if (note->descsz == sizeof (prstatus_t))
5750 prstatus_t prstat;
5752 raw_size = sizeof (prstat.pr_reg);
5753 offset = offsetof (prstatus_t, pr_reg);
5754 memcpy (&prstat, note->descdata, sizeof (prstat));
5756 /* Do not overwrite the core signal if it
5757 has already been set by another thread. */
5758 if (elf_tdata (abfd)->core_signal == 0)
5759 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5760 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5762 /* pr_who exists on:
5763 solaris 2.5+
5764 unixware 4.2
5765 pr_who doesn't exist on:
5766 linux 2.[01]
5768 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5769 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5770 #endif
5772 #if defined (HAVE_PRSTATUS32_T)
5773 else if (note->descsz == sizeof (prstatus32_t))
5775 /* 64-bit host, 32-bit corefile */
5776 prstatus32_t prstat;
5778 raw_size = sizeof (prstat.pr_reg);
5779 offset = offsetof (prstatus32_t, pr_reg);
5780 memcpy (&prstat, note->descdata, sizeof (prstat));
5782 /* Do not overwrite the core signal if it
5783 has already been set by another thread. */
5784 if (elf_tdata (abfd)->core_signal == 0)
5785 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5786 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5788 /* pr_who exists on:
5789 solaris 2.5+
5790 unixware 4.2
5791 pr_who doesn't exist on:
5792 linux 2.[01]
5794 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5795 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5796 #endif
5798 #endif /* HAVE_PRSTATUS32_T */
5799 else
5801 /* Fail - we don't know how to handle any other
5802 note size (ie. data object type). */
5803 return true;
5806 /* Make a ".reg/999" section and a ".reg" section. */
5807 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5808 raw_size, note->descpos + offset);
5810 #endif /* defined (HAVE_PRSTATUS_T) */
5812 /* Create a pseudosection containing the exact contents of NOTE. */
5813 static boolean
5814 elfcore_make_note_pseudosection (abfd, name, note)
5815 bfd *abfd;
5816 char *name;
5817 Elf_Internal_Note *note;
5819 return _bfd_elfcore_make_pseudosection (abfd, name,
5820 note->descsz, note->descpos);
5823 /* There isn't a consistent prfpregset_t across platforms,
5824 but it doesn't matter, because we don't have to pick this
5825 data structure apart. */
5827 static boolean
5828 elfcore_grok_prfpreg (abfd, note)
5829 bfd *abfd;
5830 Elf_Internal_Note *note;
5832 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5835 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5836 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5837 literally. */
5839 static boolean
5840 elfcore_grok_prxfpreg (abfd, note)
5841 bfd *abfd;
5842 Elf_Internal_Note *note;
5844 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5847 #if defined (HAVE_PRPSINFO_T)
5848 typedef prpsinfo_t elfcore_psinfo_t;
5849 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5850 typedef prpsinfo32_t elfcore_psinfo32_t;
5851 #endif
5852 #endif
5854 #if defined (HAVE_PSINFO_T)
5855 typedef psinfo_t elfcore_psinfo_t;
5856 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5857 typedef psinfo32_t elfcore_psinfo32_t;
5858 #endif
5859 #endif
5861 /* return a malloc'ed copy of a string at START which is at
5862 most MAX bytes long, possibly without a terminating '\0'.
5863 the copy will always have a terminating '\0'. */
5865 char *
5866 _bfd_elfcore_strndup (abfd, start, max)
5867 bfd *abfd;
5868 char *start;
5869 size_t max;
5871 char *dups;
5872 char *end = memchr (start, '\0', max);
5873 size_t len;
5875 if (end == NULL)
5876 len = max;
5877 else
5878 len = end - start;
5880 dups = bfd_alloc (abfd, (bfd_size_type) len + 1);
5881 if (dups == NULL)
5882 return NULL;
5884 memcpy (dups, start, len);
5885 dups[len] = '\0';
5887 return dups;
5890 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5891 static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *));
5893 static boolean
5894 elfcore_grok_psinfo (abfd, note)
5895 bfd *abfd;
5896 Elf_Internal_Note *note;
5898 if (note->descsz == sizeof (elfcore_psinfo_t))
5900 elfcore_psinfo_t psinfo;
5902 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5904 elf_tdata (abfd)->core_program
5905 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5906 sizeof (psinfo.pr_fname));
5908 elf_tdata (abfd)->core_command
5909 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5910 sizeof (psinfo.pr_psargs));
5912 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5913 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5915 /* 64-bit host, 32-bit corefile */
5916 elfcore_psinfo32_t psinfo;
5918 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5920 elf_tdata (abfd)->core_program
5921 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5922 sizeof (psinfo.pr_fname));
5924 elf_tdata (abfd)->core_command
5925 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5926 sizeof (psinfo.pr_psargs));
5928 #endif
5930 else
5932 /* Fail - we don't know how to handle any other
5933 note size (ie. data object type). */
5934 return true;
5937 /* Note that for some reason, a spurious space is tacked
5938 onto the end of the args in some (at least one anyway)
5939 implementations, so strip it off if it exists. */
5942 char *command = elf_tdata (abfd)->core_command;
5943 int n = strlen (command);
5945 if (0 < n && command[n - 1] == ' ')
5946 command[n - 1] = '\0';
5949 return true;
5951 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5953 #if defined (HAVE_PSTATUS_T)
5954 static boolean
5955 elfcore_grok_pstatus (abfd, note)
5956 bfd *abfd;
5957 Elf_Internal_Note *note;
5959 if (note->descsz == sizeof (pstatus_t)
5960 #if defined (HAVE_PXSTATUS_T)
5961 || note->descsz == sizeof (pxstatus_t)
5962 #endif
5965 pstatus_t pstat;
5967 memcpy (&pstat, note->descdata, sizeof (pstat));
5969 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5971 #if defined (HAVE_PSTATUS32_T)
5972 else if (note->descsz == sizeof (pstatus32_t))
5974 /* 64-bit host, 32-bit corefile */
5975 pstatus32_t pstat;
5977 memcpy (&pstat, note->descdata, sizeof (pstat));
5979 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5981 #endif
5982 /* Could grab some more details from the "representative"
5983 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5984 NT_LWPSTATUS note, presumably. */
5986 return true;
5988 #endif /* defined (HAVE_PSTATUS_T) */
5990 #if defined (HAVE_LWPSTATUS_T)
5991 static boolean
5992 elfcore_grok_lwpstatus (abfd, note)
5993 bfd *abfd;
5994 Elf_Internal_Note *note;
5996 lwpstatus_t lwpstat;
5997 char buf[100];
5998 char *name;
5999 asection *sect;
6001 if (note->descsz != sizeof (lwpstat)
6002 #if defined (HAVE_LWPXSTATUS_T)
6003 && note->descsz != sizeof (lwpxstatus_t)
6004 #endif
6006 return true;
6008 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
6010 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
6011 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
6013 /* Make a ".reg/999" section. */
6015 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
6016 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6017 if (name == NULL)
6018 return false;
6019 strcpy (name, buf);
6021 sect = bfd_make_section (abfd, name);
6022 if (sect == NULL)
6023 return false;
6025 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6026 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
6027 sect->filepos = note->descpos
6028 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
6029 #endif
6031 #if defined (HAVE_LWPSTATUS_T_PR_REG)
6032 sect->_raw_size = sizeof (lwpstat.pr_reg);
6033 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
6034 #endif
6036 sect->flags = SEC_HAS_CONTENTS;
6037 sect->alignment_power = 2;
6039 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
6040 return false;
6042 /* Make a ".reg2/999" section */
6044 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
6045 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6046 if (name == NULL)
6047 return false;
6048 strcpy (name, buf);
6050 sect = bfd_make_section (abfd, name);
6051 if (sect == NULL)
6052 return false;
6054 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
6055 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
6056 sect->filepos = note->descpos
6057 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
6058 #endif
6060 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
6061 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
6062 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
6063 #endif
6065 sect->flags = SEC_HAS_CONTENTS;
6066 sect->alignment_power = 2;
6068 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
6070 #endif /* defined (HAVE_LWPSTATUS_T) */
6072 #if defined (HAVE_WIN32_PSTATUS_T)
6073 static boolean
6074 elfcore_grok_win32pstatus (abfd, note)
6075 bfd *abfd;
6076 Elf_Internal_Note *note;
6078 char buf[30];
6079 char *name;
6080 asection *sect;
6081 win32_pstatus_t pstatus;
6083 if (note->descsz < sizeof (pstatus))
6084 return true;
6086 memcpy (&pstatus, note->descdata, note->descsz);
6088 switch (pstatus.data_type)
6090 case NOTE_INFO_PROCESS:
6091 /* FIXME: need to add ->core_command. */
6092 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
6093 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
6094 break;
6096 case NOTE_INFO_THREAD:
6097 /* Make a ".reg/999" section. */
6098 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
6100 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6101 if (name == NULL)
6102 return false;
6104 strcpy (name, buf);
6106 sect = bfd_make_section (abfd, name);
6107 if (sect == NULL)
6108 return false;
6110 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
6111 sect->filepos = (note->descpos
6112 + offsetof (struct win32_pstatus,
6113 data.thread_info.thread_context));
6114 sect->flags = SEC_HAS_CONTENTS;
6115 sect->alignment_power = 2;
6117 if (pstatus.data.thread_info.is_active_thread)
6118 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
6119 return false;
6120 break;
6122 case NOTE_INFO_MODULE:
6123 /* Make a ".module/xxxxxxxx" section. */
6124 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
6126 name = bfd_alloc (abfd, (bfd_size_type) strlen (buf) + 1);
6127 if (name == NULL)
6128 return false;
6130 strcpy (name, buf);
6132 sect = bfd_make_section (abfd, name);
6134 if (sect == NULL)
6135 return false;
6137 sect->_raw_size = note->descsz;
6138 sect->filepos = note->descpos;
6139 sect->flags = SEC_HAS_CONTENTS;
6140 sect->alignment_power = 2;
6141 break;
6143 default:
6144 return true;
6147 return true;
6149 #endif /* HAVE_WIN32_PSTATUS_T */
6151 static boolean
6152 elfcore_grok_note (abfd, note)
6153 bfd *abfd;
6154 Elf_Internal_Note *note;
6156 struct elf_backend_data *bed = get_elf_backend_data (abfd);
6158 switch (note->type)
6160 default:
6161 return true;
6163 case NT_PRSTATUS:
6164 if (bed->elf_backend_grok_prstatus)
6165 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
6166 return true;
6167 #if defined (HAVE_PRSTATUS_T)
6168 return elfcore_grok_prstatus (abfd, note);
6169 #else
6170 return true;
6171 #endif
6173 #if defined (HAVE_PSTATUS_T)
6174 case NT_PSTATUS:
6175 return elfcore_grok_pstatus (abfd, note);
6176 #endif
6178 #if defined (HAVE_LWPSTATUS_T)
6179 case NT_LWPSTATUS:
6180 return elfcore_grok_lwpstatus (abfd, note);
6181 #endif
6183 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
6184 return elfcore_grok_prfpreg (abfd, note);
6186 #if defined (HAVE_WIN32_PSTATUS_T)
6187 case NT_WIN32PSTATUS:
6188 return elfcore_grok_win32pstatus (abfd, note);
6189 #endif
6191 case NT_PRXFPREG: /* Linux SSE extension */
6192 if (note->namesz == 5
6193 && ! strcmp (note->namedata, "LINUX"))
6194 return elfcore_grok_prxfpreg (abfd, note);
6195 else
6196 return true;
6198 case NT_PRPSINFO:
6199 case NT_PSINFO:
6200 if (bed->elf_backend_grok_psinfo)
6201 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
6202 return true;
6203 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
6204 return elfcore_grok_psinfo (abfd, note);
6205 #else
6206 return true;
6207 #endif
6211 static boolean
6212 elfcore_read_notes (abfd, offset, size)
6213 bfd *abfd;
6214 file_ptr offset;
6215 bfd_size_type size;
6217 char *buf;
6218 char *p;
6220 if (size <= 0)
6221 return true;
6223 if (bfd_seek (abfd, offset, SEEK_SET) != 0)
6224 return false;
6226 buf = bfd_malloc (size);
6227 if (buf == NULL)
6228 return false;
6230 if (bfd_bread (buf, size, abfd) != size)
6232 error:
6233 free (buf);
6234 return false;
6237 p = buf;
6238 while (p < buf + size)
6240 /* FIXME: bad alignment assumption. */
6241 Elf_External_Note *xnp = (Elf_External_Note *) p;
6242 Elf_Internal_Note in;
6244 in.type = H_GET_32 (abfd, xnp->type);
6246 in.namesz = H_GET_32 (abfd, xnp->namesz);
6247 in.namedata = xnp->name;
6249 in.descsz = H_GET_32 (abfd, xnp->descsz);
6250 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
6251 in.descpos = offset + (in.descdata - buf);
6253 if (! elfcore_grok_note (abfd, &in))
6254 goto error;
6256 p = in.descdata + BFD_ALIGN (in.descsz, 4);
6259 free (buf);
6260 return true;
6263 /* Providing external access to the ELF program header table. */
6265 /* Return an upper bound on the number of bytes required to store a
6266 copy of ABFD's program header table entries. Return -1 if an error
6267 occurs; bfd_get_error will return an appropriate code. */
6269 long
6270 bfd_get_elf_phdr_upper_bound (abfd)
6271 bfd *abfd;
6273 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6275 bfd_set_error (bfd_error_wrong_format);
6276 return -1;
6279 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
6282 /* Copy ABFD's program header table entries to *PHDRS. The entries
6283 will be stored as an array of Elf_Internal_Phdr structures, as
6284 defined in include/elf/internal.h. To find out how large the
6285 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
6287 Return the number of program header table entries read, or -1 if an
6288 error occurs; bfd_get_error will return an appropriate code. */
6291 bfd_get_elf_phdrs (abfd, phdrs)
6292 bfd *abfd;
6293 void *phdrs;
6295 int num_phdrs;
6297 if (abfd->xvec->flavour != bfd_target_elf_flavour)
6299 bfd_set_error (bfd_error_wrong_format);
6300 return -1;
6303 num_phdrs = elf_elfheader (abfd)->e_phnum;
6304 memcpy (phdrs, elf_tdata (abfd)->phdr,
6305 num_phdrs * sizeof (Elf_Internal_Phdr));
6307 return num_phdrs;
6310 void
6311 _bfd_elf_sprintf_vma (abfd, buf, value)
6312 bfd *abfd ATTRIBUTE_UNUSED;
6313 char *buf;
6314 bfd_vma value;
6316 #ifdef BFD64
6317 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6319 i_ehdrp = elf_elfheader (abfd);
6320 if (i_ehdrp == NULL)
6321 sprintf_vma (buf, value);
6322 else
6324 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6326 #if BFD_HOST_64BIT_LONG
6327 sprintf (buf, "%016lx", value);
6328 #else
6329 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value),
6330 _bfd_int64_low (value));
6331 #endif
6333 else
6334 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff));
6336 #else
6337 sprintf_vma (buf, value);
6338 #endif
6341 void
6342 _bfd_elf_fprintf_vma (abfd, stream, value)
6343 bfd *abfd ATTRIBUTE_UNUSED;
6344 PTR stream;
6345 bfd_vma value;
6347 #ifdef BFD64
6348 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
6350 i_ehdrp = elf_elfheader (abfd);
6351 if (i_ehdrp == NULL)
6352 fprintf_vma ((FILE *) stream, value);
6353 else
6355 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
6357 #if BFD_HOST_64BIT_LONG
6358 fprintf ((FILE *) stream, "%016lx", value);
6359 #else
6360 fprintf ((FILE *) stream, "%08lx%08lx",
6361 _bfd_int64_high (value), _bfd_int64_low (value));
6362 #endif
6364 else
6365 fprintf ((FILE *) stream, "%08lx",
6366 (unsigned long) (value & 0xffffffff));
6368 #else
6369 fprintf_vma ((FILE *) stream, value);
6370 #endif
6373 enum elf_reloc_type_class
6374 _bfd_elf_reloc_type_class (rela)
6375 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED;
6377 return reloc_class_normal;
6380 /* For RELA architectures, return what the relocation value for
6381 relocation against a local symbol. */
6383 bfd_vma
6384 _bfd_elf_rela_local_sym (abfd, sym, sec, rel)
6385 bfd *abfd;
6386 Elf_Internal_Sym *sym;
6387 asection *sec;
6388 Elf_Internal_Rela *rel;
6390 bfd_vma relocation;
6392 relocation = (sec->output_section->vma
6393 + sec->output_offset
6394 + sym->st_value);
6395 if ((sec->flags & SEC_MERGE)
6396 && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
6398 asection *msec;
6400 msec = sec;
6401 rel->r_addend =
6402 _bfd_merged_section_offset (abfd, &msec,
6403 elf_section_data (sec)->merge_info,
6404 sym->st_value + rel->r_addend,
6405 (bfd_vma) 0)
6406 - relocation;
6407 rel->r_addend += msec->output_section->vma + msec->output_offset;
6409 return relocation;