* config/tc-s390.c (s390_force_relocation): Removed.
[binutils.git] / bfd / elf.c
blob4e2d7ae8d99a34d069e6109a9b36a95ce3d67822
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"
44 static INLINE struct elf_segment_map *make_mapping
45 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
46 static boolean map_sections_to_segments PARAMS ((bfd *));
47 static int elf_sort_sections PARAMS ((const PTR, const PTR));
48 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
49 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
50 static boolean prep_headers PARAMS ((bfd *));
51 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
52 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
53 static char *elf_read PARAMS ((bfd *, long, unsigned int));
54 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
55 static boolean assign_section_numbers PARAMS ((bfd *));
56 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
57 static boolean elf_map_symbols PARAMS ((bfd *));
58 static bfd_size_type get_program_header_size PARAMS ((bfd *));
59 static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
60 static boolean elf_find_function PARAMS ((bfd *, asection *,
61 asymbol **,
62 bfd_vma, const char **,
63 const char **));
65 /* Swap version information in and out. The version information is
66 currently size independent. If that ever changes, this code will
67 need to move into elfcode.h. */
69 /* Swap in a Verdef structure. */
71 void
72 _bfd_elf_swap_verdef_in (abfd, src, dst)
73 bfd *abfd;
74 const Elf_External_Verdef *src;
75 Elf_Internal_Verdef *dst;
77 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
78 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
79 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
80 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
81 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
82 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
83 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
86 /* Swap out a Verdef structure. */
88 void
89 _bfd_elf_swap_verdef_out (abfd, src, dst)
90 bfd *abfd;
91 const Elf_Internal_Verdef *src;
92 Elf_External_Verdef *dst;
94 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
95 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
96 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
97 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
98 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
99 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
100 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
103 /* Swap in a Verdaux structure. */
105 void
106 _bfd_elf_swap_verdaux_in (abfd, src, dst)
107 bfd *abfd;
108 const Elf_External_Verdaux *src;
109 Elf_Internal_Verdaux *dst;
111 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
112 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
115 /* Swap out a Verdaux structure. */
117 void
118 _bfd_elf_swap_verdaux_out (abfd, src, dst)
119 bfd *abfd;
120 const Elf_Internal_Verdaux *src;
121 Elf_External_Verdaux *dst;
123 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
124 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
127 /* Swap in a Verneed structure. */
129 void
130 _bfd_elf_swap_verneed_in (abfd, src, dst)
131 bfd *abfd;
132 const Elf_External_Verneed *src;
133 Elf_Internal_Verneed *dst;
135 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
136 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
137 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
138 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
139 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
142 /* Swap out a Verneed structure. */
144 void
145 _bfd_elf_swap_verneed_out (abfd, src, dst)
146 bfd *abfd;
147 const Elf_Internal_Verneed *src;
148 Elf_External_Verneed *dst;
150 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
151 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
152 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
153 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
154 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
157 /* Swap in a Vernaux structure. */
159 void
160 _bfd_elf_swap_vernaux_in (abfd, src, dst)
161 bfd *abfd;
162 const Elf_External_Vernaux *src;
163 Elf_Internal_Vernaux *dst;
165 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
166 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
167 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
168 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
169 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
172 /* Swap out a Vernaux structure. */
174 void
175 _bfd_elf_swap_vernaux_out (abfd, src, dst)
176 bfd *abfd;
177 const Elf_Internal_Vernaux *src;
178 Elf_External_Vernaux *dst;
180 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
181 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
182 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
183 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
184 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
187 /* Swap in a Versym structure. */
189 void
190 _bfd_elf_swap_versym_in (abfd, src, dst)
191 bfd *abfd;
192 const Elf_External_Versym *src;
193 Elf_Internal_Versym *dst;
195 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
198 /* Swap out a Versym structure. */
200 void
201 _bfd_elf_swap_versym_out (abfd, src, dst)
202 bfd *abfd;
203 const Elf_Internal_Versym *src;
204 Elf_External_Versym *dst;
206 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
209 /* Standard ELF hash function. Do not change this function; you will
210 cause invalid hash tables to be generated. */
212 unsigned long
213 bfd_elf_hash (namearg)
214 const char *namearg;
216 const unsigned char *name = (const unsigned char *) namearg;
217 unsigned long h = 0;
218 unsigned long g;
219 int ch;
221 while ((ch = *name++) != '\0')
223 h = (h << 4) + ch;
224 if ((g = (h & 0xf0000000)) != 0)
226 h ^= g >> 24;
227 /* The ELF ABI says `h &= ~g', but this is equivalent in
228 this case and on some machines one insn instead of two. */
229 h ^= g;
232 return h;
235 /* Read a specified number of bytes at a specified offset in an ELF
236 file, into a newly allocated buffer, and return a pointer to the
237 buffer. */
239 static char *
240 elf_read (abfd, offset, size)
241 bfd *abfd;
242 long offset;
243 unsigned int size;
245 char *buf;
247 if ((buf = bfd_alloc (abfd, size)) == NULL)
248 return NULL;
249 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
250 return NULL;
251 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
253 if (bfd_get_error () != bfd_error_system_call)
254 bfd_set_error (bfd_error_file_truncated);
255 return NULL;
257 return buf;
260 boolean
261 bfd_elf_mkobject (abfd)
262 bfd *abfd;
264 /* This just does initialization. */
265 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
266 elf_tdata (abfd) = (struct elf_obj_tdata *)
267 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
268 if (elf_tdata (abfd) == 0)
269 return false;
270 /* Since everything is done at close time, do we need any
271 initialization? */
273 return true;
276 boolean
277 bfd_elf_mkcorefile (abfd)
278 bfd *abfd;
280 /* I think this can be done just like an object file. */
281 return bfd_elf_mkobject (abfd);
284 char *
285 bfd_elf_get_str_section (abfd, shindex)
286 bfd *abfd;
287 unsigned int shindex;
289 Elf_Internal_Shdr **i_shdrp;
290 char *shstrtab = NULL;
291 unsigned int offset;
292 unsigned int shstrtabsize;
294 i_shdrp = elf_elfsections (abfd);
295 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
296 return 0;
298 shstrtab = (char *) i_shdrp[shindex]->contents;
299 if (shstrtab == NULL)
301 /* No cached one, attempt to read, and cache what we read. */
302 offset = i_shdrp[shindex]->sh_offset;
303 shstrtabsize = i_shdrp[shindex]->sh_size;
304 shstrtab = elf_read (abfd, offset, shstrtabsize);
305 i_shdrp[shindex]->contents = (PTR) shstrtab;
307 return shstrtab;
310 char *
311 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
312 bfd *abfd;
313 unsigned int shindex;
314 unsigned int strindex;
316 Elf_Internal_Shdr *hdr;
318 if (strindex == 0)
319 return "";
321 hdr = elf_elfsections (abfd)[shindex];
323 if (hdr->contents == NULL
324 && bfd_elf_get_str_section (abfd, shindex) == NULL)
325 return NULL;
327 if (strindex >= hdr->sh_size)
329 (*_bfd_error_handler)
330 (_("%s: invalid string offset %u >= %lu for section `%s'"),
331 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
332 ((shindex == elf_elfheader(abfd)->e_shstrndx
333 && strindex == hdr->sh_name)
334 ? ".shstrtab"
335 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
336 return "";
339 return ((char *) hdr->contents) + strindex;
342 /* Make a BFD section from an ELF section. We store a pointer to the
343 BFD section in the bfd_section field of the header. */
345 boolean
346 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
347 bfd *abfd;
348 Elf_Internal_Shdr *hdr;
349 const char *name;
351 asection *newsect;
352 flagword flags;
353 struct elf_backend_data *bed;
355 if (hdr->bfd_section != NULL)
357 BFD_ASSERT (strcmp (name,
358 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
359 return true;
362 newsect = bfd_make_section_anyway (abfd, name);
363 if (newsect == NULL)
364 return false;
366 newsect->filepos = hdr->sh_offset;
368 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
369 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
370 || ! bfd_set_section_alignment (abfd, newsect,
371 bfd_log2 (hdr->sh_addralign)))
372 return false;
374 flags = SEC_NO_FLAGS;
375 if (hdr->sh_type != SHT_NOBITS)
376 flags |= SEC_HAS_CONTENTS;
377 if ((hdr->sh_flags & SHF_ALLOC) != 0)
379 flags |= SEC_ALLOC;
380 if (hdr->sh_type != SHT_NOBITS)
381 flags |= SEC_LOAD;
383 if ((hdr->sh_flags & SHF_WRITE) == 0)
384 flags |= SEC_READONLY;
385 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
386 flags |= SEC_CODE;
387 else if ((flags & SEC_LOAD) != 0)
388 flags |= SEC_DATA;
389 if ((hdr->sh_flags & SHF_MERGE) != 0)
391 flags |= SEC_MERGE;
392 newsect->entsize = hdr->sh_entsize;
393 if ((hdr->sh_flags & SHF_STRINGS) != 0)
394 flags |= SEC_STRINGS;
397 /* The debugging sections appear to be recognized only by name, not
398 any sort of flag. */
400 static const char *debug_sec_names [] =
402 ".debug",
403 ".gnu.linkonce.wi.",
404 ".line",
405 ".stab"
407 int i;
409 for (i = sizeof (debug_sec_names) / sizeof (debug_sec_names[0]); i--;)
410 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0)
411 break;
413 if (i >= 0)
414 flags |= SEC_DEBUGGING;
417 /* As a GNU extension, if the name begins with .gnu.linkonce, we
418 only link a single copy of the section. This is used to support
419 g++. g++ will emit each template expansion in its own section.
420 The symbols will be defined as weak, so that multiple definitions
421 are permitted. The GNU linker extension is to actually discard
422 all but one of the sections. */
423 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
424 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
426 bed = get_elf_backend_data (abfd);
427 if (bed->elf_backend_section_flags)
428 if (! bed->elf_backend_section_flags (&flags, hdr))
429 return false;
431 if (! bfd_set_section_flags (abfd, newsect, flags))
432 return false;
434 if ((flags & SEC_ALLOC) != 0)
436 Elf_Internal_Phdr *phdr;
437 unsigned int i;
439 /* Look through the phdrs to see if we need to adjust the lma.
440 If all the p_paddr fields are zero, we ignore them, since
441 some ELF linkers produce such output. */
442 phdr = elf_tdata (abfd)->phdr;
443 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
445 if (phdr->p_paddr != 0)
446 break;
448 if (i < elf_elfheader (abfd)->e_phnum)
450 phdr = elf_tdata (abfd)->phdr;
451 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
453 if (phdr->p_type == PT_LOAD
454 && phdr->p_vaddr != phdr->p_paddr
455 && phdr->p_vaddr <= hdr->sh_addr
456 && (phdr->p_vaddr + phdr->p_memsz
457 >= hdr->sh_addr + hdr->sh_size)
458 && ((flags & SEC_LOAD) == 0
459 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
460 && (phdr->p_offset + phdr->p_filesz
461 >= hdr->sh_offset + hdr->sh_size))))
463 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
464 break;
470 hdr->bfd_section = newsect;
471 elf_section_data (newsect)->this_hdr = *hdr;
473 return true;
477 INTERNAL_FUNCTION
478 bfd_elf_find_section
480 SYNOPSIS
481 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
483 DESCRIPTION
484 Helper functions for GDB to locate the string tables.
485 Since BFD hides string tables from callers, GDB needs to use an
486 internal hook to find them. Sun's .stabstr, in particular,
487 isn't even pointed to by the .stab section, so ordinary
488 mechanisms wouldn't work to find it, even if we had some.
491 struct elf_internal_shdr *
492 bfd_elf_find_section (abfd, name)
493 bfd *abfd;
494 char *name;
496 Elf_Internal_Shdr **i_shdrp;
497 char *shstrtab;
498 unsigned int max;
499 unsigned int i;
501 i_shdrp = elf_elfsections (abfd);
502 if (i_shdrp != NULL)
504 shstrtab = bfd_elf_get_str_section
505 (abfd, elf_elfheader (abfd)->e_shstrndx);
506 if (shstrtab != NULL)
508 max = elf_elfheader (abfd)->e_shnum;
509 for (i = 1; i < max; i++)
510 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
511 return i_shdrp[i];
514 return 0;
517 const char *const bfd_elf_section_type_names[] = {
518 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
519 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
520 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
523 /* ELF relocs are against symbols. If we are producing relocateable
524 output, and the reloc is against an external symbol, and nothing
525 has given us any additional addend, the resulting reloc will also
526 be against the same symbol. In such a case, we don't want to
527 change anything about the way the reloc is handled, since it will
528 all be done at final link time. Rather than put special case code
529 into bfd_perform_relocation, all the reloc types use this howto
530 function. It just short circuits the reloc if producing
531 relocateable output against an external symbol. */
533 bfd_reloc_status_type
534 bfd_elf_generic_reloc (abfd,
535 reloc_entry,
536 symbol,
537 data,
538 input_section,
539 output_bfd,
540 error_message)
541 bfd *abfd ATTRIBUTE_UNUSED;
542 arelent *reloc_entry;
543 asymbol *symbol;
544 PTR data ATTRIBUTE_UNUSED;
545 asection *input_section;
546 bfd *output_bfd;
547 char **error_message ATTRIBUTE_UNUSED;
549 if (output_bfd != (bfd *) NULL
550 && (symbol->flags & BSF_SECTION_SYM) == 0
551 && (! reloc_entry->howto->partial_inplace
552 || reloc_entry->addend == 0))
554 reloc_entry->address += input_section->output_offset;
555 return bfd_reloc_ok;
558 return bfd_reloc_continue;
561 /* Finish SHF_MERGE section merging. */
563 boolean
564 _bfd_elf_merge_sections (abfd, info)
565 bfd *abfd;
566 struct bfd_link_info *info;
568 if (elf_hash_table (info)->merge_info)
569 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info);
570 return true;
573 /* Print out the program headers. */
575 boolean
576 _bfd_elf_print_private_bfd_data (abfd, farg)
577 bfd *abfd;
578 PTR farg;
580 FILE *f = (FILE *) farg;
581 Elf_Internal_Phdr *p;
582 asection *s;
583 bfd_byte *dynbuf = NULL;
585 p = elf_tdata (abfd)->phdr;
586 if (p != NULL)
588 unsigned int i, c;
590 fprintf (f, _("\nProgram Header:\n"));
591 c = elf_elfheader (abfd)->e_phnum;
592 for (i = 0; i < c; i++, p++)
594 const char *s;
595 char buf[20];
597 switch (p->p_type)
599 case PT_NULL: s = "NULL"; break;
600 case PT_LOAD: s = "LOAD"; break;
601 case PT_DYNAMIC: s = "DYNAMIC"; break;
602 case PT_INTERP: s = "INTERP"; break;
603 case PT_NOTE: s = "NOTE"; break;
604 case PT_SHLIB: s = "SHLIB"; break;
605 case PT_PHDR: s = "PHDR"; break;
606 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
608 fprintf (f, "%8s off 0x", s);
609 fprintf_vma (f, p->p_offset);
610 fprintf (f, " vaddr 0x");
611 fprintf_vma (f, p->p_vaddr);
612 fprintf (f, " paddr 0x");
613 fprintf_vma (f, p->p_paddr);
614 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
615 fprintf (f, " filesz 0x");
616 fprintf_vma (f, p->p_filesz);
617 fprintf (f, " memsz 0x");
618 fprintf_vma (f, p->p_memsz);
619 fprintf (f, " flags %c%c%c",
620 (p->p_flags & PF_R) != 0 ? 'r' : '-',
621 (p->p_flags & PF_W) != 0 ? 'w' : '-',
622 (p->p_flags & PF_X) != 0 ? 'x' : '-');
623 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
624 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
625 fprintf (f, "\n");
629 s = bfd_get_section_by_name (abfd, ".dynamic");
630 if (s != NULL)
632 int elfsec;
633 unsigned long link;
634 bfd_byte *extdyn, *extdynend;
635 size_t extdynsize;
636 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
638 fprintf (f, _("\nDynamic Section:\n"));
640 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
641 if (dynbuf == NULL)
642 goto error_return;
643 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
644 s->_raw_size))
645 goto error_return;
647 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
648 if (elfsec == -1)
649 goto error_return;
650 link = elf_elfsections (abfd)[elfsec]->sh_link;
652 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
653 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
655 extdyn = dynbuf;
656 extdynend = extdyn + s->_raw_size;
657 for (; extdyn < extdynend; extdyn += extdynsize)
659 Elf_Internal_Dyn dyn;
660 const char *name;
661 char ab[20];
662 boolean stringp;
664 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
666 if (dyn.d_tag == DT_NULL)
667 break;
669 stringp = false;
670 switch (dyn.d_tag)
672 default:
673 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
674 name = ab;
675 break;
677 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
678 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
679 case DT_PLTGOT: name = "PLTGOT"; break;
680 case DT_HASH: name = "HASH"; break;
681 case DT_STRTAB: name = "STRTAB"; break;
682 case DT_SYMTAB: name = "SYMTAB"; break;
683 case DT_RELA: name = "RELA"; break;
684 case DT_RELASZ: name = "RELASZ"; break;
685 case DT_RELAENT: name = "RELAENT"; break;
686 case DT_STRSZ: name = "STRSZ"; break;
687 case DT_SYMENT: name = "SYMENT"; break;
688 case DT_INIT: name = "INIT"; break;
689 case DT_FINI: name = "FINI"; break;
690 case DT_SONAME: name = "SONAME"; stringp = true; break;
691 case DT_RPATH: name = "RPATH"; stringp = true; break;
692 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
693 case DT_REL: name = "REL"; break;
694 case DT_RELSZ: name = "RELSZ"; break;
695 case DT_RELENT: name = "RELENT"; break;
696 case DT_PLTREL: name = "PLTREL"; break;
697 case DT_DEBUG: name = "DEBUG"; break;
698 case DT_TEXTREL: name = "TEXTREL"; break;
699 case DT_JMPREL: name = "JMPREL"; break;
700 case DT_BIND_NOW: name = "BIND_NOW"; break;
701 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
702 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
703 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
704 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
705 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
706 case DT_FLAGS: name = "FLAGS"; break;
707 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
708 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
709 case DT_CHECKSUM: name = "CHECKSUM"; break;
710 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
711 case DT_MOVEENT: name = "MOVEENT"; break;
712 case DT_MOVESZ: name = "MOVESZ"; break;
713 case DT_FEATURE: name = "FEATURE"; break;
714 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
715 case DT_SYMINSZ: name = "SYMINSZ"; break;
716 case DT_SYMINENT: name = "SYMINENT"; break;
717 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
718 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
719 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
720 case DT_PLTPAD: name = "PLTPAD"; break;
721 case DT_MOVETAB: name = "MOVETAB"; break;
722 case DT_SYMINFO: name = "SYMINFO"; break;
723 case DT_RELACOUNT: name = "RELACOUNT"; break;
724 case DT_RELCOUNT: name = "RELCOUNT"; break;
725 case DT_FLAGS_1: name = "FLAGS_1"; break;
726 case DT_VERSYM: name = "VERSYM"; break;
727 case DT_VERDEF: name = "VERDEF"; break;
728 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
729 case DT_VERNEED: name = "VERNEED"; break;
730 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
731 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
732 case DT_USED: name = "USED"; break;
733 case DT_FILTER: name = "FILTER"; stringp = true; break;
736 fprintf (f, " %-11s ", name);
737 if (! stringp)
738 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
739 else
741 const char *string;
743 string = bfd_elf_string_from_elf_section (abfd, link,
744 dyn.d_un.d_val);
745 if (string == NULL)
746 goto error_return;
747 fprintf (f, "%s", string);
749 fprintf (f, "\n");
752 free (dynbuf);
753 dynbuf = NULL;
756 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
757 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
759 if (! _bfd_elf_slurp_version_tables (abfd))
760 return false;
763 if (elf_dynverdef (abfd) != 0)
765 Elf_Internal_Verdef *t;
767 fprintf (f, _("\nVersion definitions:\n"));
768 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
770 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
771 t->vd_flags, t->vd_hash, t->vd_nodename);
772 if (t->vd_auxptr->vda_nextptr != NULL)
774 Elf_Internal_Verdaux *a;
776 fprintf (f, "\t");
777 for (a = t->vd_auxptr->vda_nextptr;
778 a != NULL;
779 a = a->vda_nextptr)
780 fprintf (f, "%s ", a->vda_nodename);
781 fprintf (f, "\n");
786 if (elf_dynverref (abfd) != 0)
788 Elf_Internal_Verneed *t;
790 fprintf (f, _("\nVersion References:\n"));
791 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
793 Elf_Internal_Vernaux *a;
795 fprintf (f, _(" required from %s:\n"), t->vn_filename);
796 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
797 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
798 a->vna_flags, a->vna_other, a->vna_nodename);
802 return true;
804 error_return:
805 if (dynbuf != NULL)
806 free (dynbuf);
807 return false;
810 /* Display ELF-specific fields of a symbol. */
812 void
813 bfd_elf_print_symbol (abfd, filep, symbol, how)
814 bfd *abfd;
815 PTR filep;
816 asymbol *symbol;
817 bfd_print_symbol_type how;
819 FILE *file = (FILE *) filep;
820 switch (how)
822 case bfd_print_symbol_name:
823 fprintf (file, "%s", symbol->name);
824 break;
825 case bfd_print_symbol_more:
826 fprintf (file, "elf ");
827 fprintf_vma (file, symbol->value);
828 fprintf (file, " %lx", (long) symbol->flags);
829 break;
830 case bfd_print_symbol_all:
832 const char *section_name;
833 const char *name = NULL;
834 struct elf_backend_data *bed;
835 unsigned char st_other;
837 section_name = symbol->section ? symbol->section->name : "(*none*)";
839 bed = get_elf_backend_data (abfd);
840 if (bed->elf_backend_print_symbol_all)
841 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
843 if (name == NULL)
845 name = symbol->name;
846 bfd_print_symbol_vandf ((PTR) file, symbol);
849 fprintf (file, " %s\t", section_name);
850 /* Print the "other" value for a symbol. For common symbols,
851 we've already printed the size; now print the alignment.
852 For other symbols, we have no specified alignment, and
853 we've printed the address; now print the size. */
854 fprintf_vma (file,
855 (bfd_is_com_section (symbol->section)
856 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
857 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
859 /* If we have version information, print it. */
860 if (elf_tdata (abfd)->dynversym_section != 0
861 && (elf_tdata (abfd)->dynverdef_section != 0
862 || elf_tdata (abfd)->dynverref_section != 0))
864 unsigned int vernum;
865 const char *version_string;
867 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
869 if (vernum == 0)
870 version_string = "";
871 else if (vernum == 1)
872 version_string = "Base";
873 else if (vernum <= elf_tdata (abfd)->cverdefs)
874 version_string =
875 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
876 else
878 Elf_Internal_Verneed *t;
880 version_string = "";
881 for (t = elf_tdata (abfd)->verref;
882 t != NULL;
883 t = t->vn_nextref)
885 Elf_Internal_Vernaux *a;
887 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
889 if (a->vna_other == vernum)
891 version_string = a->vna_nodename;
892 break;
898 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
899 fprintf (file, " %-11s", version_string);
900 else
902 int i;
904 fprintf (file, " (%s)", version_string);
905 for (i = 10 - strlen (version_string); i > 0; --i)
906 putc (' ', file);
910 /* If the st_other field is not zero, print it. */
911 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
913 switch (st_other)
915 case 0: break;
916 case STV_INTERNAL: fprintf (file, " .internal"); break;
917 case STV_HIDDEN: fprintf (file, " .hidden"); break;
918 case STV_PROTECTED: fprintf (file, " .protected"); break;
919 default:
920 /* Some other non-defined flags are also present, so print
921 everything hex. */
922 fprintf (file, " 0x%02x", (unsigned int) st_other);
925 fprintf (file, " %s", name);
927 break;
931 /* Create an entry in an ELF linker hash table. */
933 struct bfd_hash_entry *
934 _bfd_elf_link_hash_newfunc (entry, table, string)
935 struct bfd_hash_entry *entry;
936 struct bfd_hash_table *table;
937 const char *string;
939 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
941 /* Allocate the structure if it has not already been allocated by a
942 subclass. */
943 if (ret == (struct elf_link_hash_entry *) NULL)
944 ret = ((struct elf_link_hash_entry *)
945 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
946 if (ret == (struct elf_link_hash_entry *) NULL)
947 return (struct bfd_hash_entry *) ret;
949 /* Call the allocation method of the superclass. */
950 ret = ((struct elf_link_hash_entry *)
951 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
952 table, string));
953 if (ret != (struct elf_link_hash_entry *) NULL)
955 /* Set local fields. */
956 ret->indx = -1;
957 ret->size = 0;
958 ret->dynindx = -1;
959 ret->dynstr_index = 0;
960 ret->weakdef = NULL;
961 ret->got.offset = (bfd_vma) -1;
962 ret->plt.offset = (bfd_vma) -1;
963 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
964 ret->verinfo.verdef = NULL;
965 ret->vtable_entries_used = NULL;
966 ret->vtable_entries_size = 0;
967 ret->vtable_parent = NULL;
968 ret->type = STT_NOTYPE;
969 ret->other = 0;
970 /* Assume that we have been called by a non-ELF symbol reader.
971 This flag is then reset by the code which reads an ELF input
972 file. This ensures that a symbol created by a non-ELF symbol
973 reader will have the flag set correctly. */
974 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
977 return (struct bfd_hash_entry *) ret;
980 /* Copy data from an indirect symbol to its direct symbol, hiding the
981 old indirect symbol. */
983 void
984 _bfd_elf_link_hash_copy_indirect (dir, ind)
985 struct elf_link_hash_entry *dir, *ind;
987 /* Copy down any references that we may have already seen to the
988 symbol which just became indirect. */
990 dir->elf_link_hash_flags |=
991 (ind->elf_link_hash_flags
992 & (ELF_LINK_HASH_REF_DYNAMIC
993 | ELF_LINK_HASH_REF_REGULAR
994 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
995 | ELF_LINK_NON_GOT_REF));
997 /* Copy over the global and procedure linkage table offset entries.
998 These may have been already set up by a check_relocs routine. */
999 if (dir->got.offset == (bfd_vma) -1)
1001 dir->got.offset = ind->got.offset;
1002 ind->got.offset = (bfd_vma) -1;
1004 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
1006 if (dir->plt.offset == (bfd_vma) -1)
1008 dir->plt.offset = ind->plt.offset;
1009 ind->plt.offset = (bfd_vma) -1;
1011 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
1013 if (dir->dynindx == -1)
1015 dir->dynindx = ind->dynindx;
1016 dir->dynstr_index = ind->dynstr_index;
1017 ind->dynindx = -1;
1018 ind->dynstr_index = 0;
1020 BFD_ASSERT (ind->dynindx == -1);
1023 void
1024 _bfd_elf_link_hash_hide_symbol (info, h)
1025 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1026 struct elf_link_hash_entry *h;
1028 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1029 h->plt.offset = (bfd_vma) -1;
1030 if ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0)
1031 h->dynindx = -1;
1034 /* Initialize an ELF linker hash table. */
1036 boolean
1037 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1038 struct elf_link_hash_table *table;
1039 bfd *abfd;
1040 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1041 struct bfd_hash_table *,
1042 const char *));
1044 table->dynamic_sections_created = false;
1045 table->dynobj = NULL;
1046 /* The first dynamic symbol is a dummy. */
1047 table->dynsymcount = 1;
1048 table->dynstr = NULL;
1049 table->bucketcount = 0;
1050 table->needed = NULL;
1051 table->runpath = NULL;
1052 table->hgot = NULL;
1053 table->stab_info = NULL;
1054 table->merge_info = NULL;
1055 table->dynlocal = NULL;
1056 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1059 /* Create an ELF linker hash table. */
1061 struct bfd_link_hash_table *
1062 _bfd_elf_link_hash_table_create (abfd)
1063 bfd *abfd;
1065 struct elf_link_hash_table *ret;
1067 ret = ((struct elf_link_hash_table *)
1068 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
1069 if (ret == (struct elf_link_hash_table *) NULL)
1070 return NULL;
1072 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1074 bfd_release (abfd, ret);
1075 return NULL;
1078 return &ret->root;
1081 /* This is a hook for the ELF emulation code in the generic linker to
1082 tell the backend linker what file name to use for the DT_NEEDED
1083 entry for a dynamic object. The generic linker passes name as an
1084 empty string to indicate that no DT_NEEDED entry should be made. */
1086 void
1087 bfd_elf_set_dt_needed_name (abfd, name)
1088 bfd *abfd;
1089 const char *name;
1091 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1092 && bfd_get_format (abfd) == bfd_object)
1093 elf_dt_name (abfd) = name;
1096 void
1097 bfd_elf_set_dt_needed_soname (abfd, name)
1098 bfd *abfd;
1099 const char *name;
1101 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1102 && bfd_get_format (abfd) == bfd_object)
1103 elf_dt_soname (abfd) = name;
1106 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1107 the linker ELF emulation code. */
1109 struct bfd_link_needed_list *
1110 bfd_elf_get_needed_list (abfd, info)
1111 bfd *abfd ATTRIBUTE_UNUSED;
1112 struct bfd_link_info *info;
1114 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1115 return NULL;
1116 return elf_hash_table (info)->needed;
1119 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1120 hook for the linker ELF emulation code. */
1122 struct bfd_link_needed_list *
1123 bfd_elf_get_runpath_list (abfd, info)
1124 bfd *abfd ATTRIBUTE_UNUSED;
1125 struct bfd_link_info *info;
1127 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1128 return NULL;
1129 return elf_hash_table (info)->runpath;
1132 /* Get the name actually used for a dynamic object for a link. This
1133 is the SONAME entry if there is one. Otherwise, it is the string
1134 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1136 const char *
1137 bfd_elf_get_dt_soname (abfd)
1138 bfd *abfd;
1140 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1141 && bfd_get_format (abfd) == bfd_object)
1142 return elf_dt_name (abfd);
1143 return NULL;
1146 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1147 the ELF linker emulation code. */
1149 boolean
1150 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1151 bfd *abfd;
1152 struct bfd_link_needed_list **pneeded;
1154 asection *s;
1155 bfd_byte *dynbuf = NULL;
1156 int elfsec;
1157 unsigned long link;
1158 bfd_byte *extdyn, *extdynend;
1159 size_t extdynsize;
1160 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1162 *pneeded = NULL;
1164 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1165 || bfd_get_format (abfd) != bfd_object)
1166 return true;
1168 s = bfd_get_section_by_name (abfd, ".dynamic");
1169 if (s == NULL || s->_raw_size == 0)
1170 return true;
1172 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1173 if (dynbuf == NULL)
1174 goto error_return;
1176 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1177 s->_raw_size))
1178 goto error_return;
1180 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1181 if (elfsec == -1)
1182 goto error_return;
1184 link = elf_elfsections (abfd)[elfsec]->sh_link;
1186 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1187 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1189 extdyn = dynbuf;
1190 extdynend = extdyn + s->_raw_size;
1191 for (; extdyn < extdynend; extdyn += extdynsize)
1193 Elf_Internal_Dyn dyn;
1195 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1197 if (dyn.d_tag == DT_NULL)
1198 break;
1200 if (dyn.d_tag == DT_NEEDED)
1202 const char *string;
1203 struct bfd_link_needed_list *l;
1205 string = bfd_elf_string_from_elf_section (abfd, link,
1206 dyn.d_un.d_val);
1207 if (string == NULL)
1208 goto error_return;
1210 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1211 if (l == NULL)
1212 goto error_return;
1214 l->by = abfd;
1215 l->name = string;
1216 l->next = *pneeded;
1217 *pneeded = l;
1221 free (dynbuf);
1223 return true;
1225 error_return:
1226 if (dynbuf != NULL)
1227 free (dynbuf);
1228 return false;
1231 /* Allocate an ELF string table--force the first byte to be zero. */
1233 struct bfd_strtab_hash *
1234 _bfd_elf_stringtab_init ()
1236 struct bfd_strtab_hash *ret;
1238 ret = _bfd_stringtab_init ();
1239 if (ret != NULL)
1241 bfd_size_type loc;
1243 loc = _bfd_stringtab_add (ret, "", true, false);
1244 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1245 if (loc == (bfd_size_type) -1)
1247 _bfd_stringtab_free (ret);
1248 ret = NULL;
1251 return ret;
1254 /* ELF .o/exec file reading */
1256 /* Create a new bfd section from an ELF section header. */
1258 boolean
1259 bfd_section_from_shdr (abfd, shindex)
1260 bfd *abfd;
1261 unsigned int shindex;
1263 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1264 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1265 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1266 char *name;
1268 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1270 switch (hdr->sh_type)
1272 case SHT_NULL:
1273 /* Inactive section. Throw it away. */
1274 return true;
1276 case SHT_PROGBITS: /* Normal section with contents. */
1277 case SHT_DYNAMIC: /* Dynamic linking information. */
1278 case SHT_NOBITS: /* .bss section. */
1279 case SHT_HASH: /* .hash section. */
1280 case SHT_NOTE: /* .note section. */
1281 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1283 case SHT_SYMTAB: /* A symbol table */
1284 if (elf_onesymtab (abfd) == shindex)
1285 return true;
1287 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1288 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1289 elf_onesymtab (abfd) = shindex;
1290 elf_tdata (abfd)->symtab_hdr = *hdr;
1291 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1292 abfd->flags |= HAS_SYMS;
1294 /* Sometimes a shared object will map in the symbol table. If
1295 SHF_ALLOC is set, and this is a shared object, then we also
1296 treat this section as a BFD section. We can not base the
1297 decision purely on SHF_ALLOC, because that flag is sometimes
1298 set in a relocateable object file, which would confuse the
1299 linker. */
1300 if ((hdr->sh_flags & SHF_ALLOC) != 0
1301 && (abfd->flags & DYNAMIC) != 0
1302 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1303 return false;
1305 return true;
1307 case SHT_DYNSYM: /* A dynamic symbol table */
1308 if (elf_dynsymtab (abfd) == shindex)
1309 return true;
1311 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1312 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1313 elf_dynsymtab (abfd) = shindex;
1314 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1315 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1316 abfd->flags |= HAS_SYMS;
1318 /* Besides being a symbol table, we also treat this as a regular
1319 section, so that objcopy can handle it. */
1320 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1322 case SHT_STRTAB: /* A string table */
1323 if (hdr->bfd_section != NULL)
1324 return true;
1325 if (ehdr->e_shstrndx == shindex)
1327 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1328 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1329 return true;
1332 unsigned int i;
1334 for (i = 1; i < ehdr->e_shnum; i++)
1336 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1337 if (hdr2->sh_link == shindex)
1339 if (! bfd_section_from_shdr (abfd, i))
1340 return false;
1341 if (elf_onesymtab (abfd) == i)
1343 elf_tdata (abfd)->strtab_hdr = *hdr;
1344 elf_elfsections (abfd)[shindex] =
1345 &elf_tdata (abfd)->strtab_hdr;
1346 return true;
1348 if (elf_dynsymtab (abfd) == i)
1350 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1351 elf_elfsections (abfd)[shindex] = hdr =
1352 &elf_tdata (abfd)->dynstrtab_hdr;
1353 /* We also treat this as a regular section, so
1354 that objcopy can handle it. */
1355 break;
1357 #if 0 /* Not handling other string tables specially right now. */
1358 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1359 /* We have a strtab for some random other section. */
1360 newsect = (asection *) hdr2->bfd_section;
1361 if (!newsect)
1362 break;
1363 hdr->bfd_section = newsect;
1364 hdr2 = &elf_section_data (newsect)->str_hdr;
1365 *hdr2 = *hdr;
1366 elf_elfsections (abfd)[shindex] = hdr2;
1367 #endif
1372 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1374 case SHT_REL:
1375 case SHT_RELA:
1376 /* *These* do a lot of work -- but build no sections! */
1378 asection *target_sect;
1379 Elf_Internal_Shdr *hdr2;
1381 /* Check for a bogus link to avoid crashing. */
1382 if (hdr->sh_link >= ehdr->e_shnum)
1384 ((*_bfd_error_handler)
1385 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1386 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1387 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1390 /* For some incomprehensible reason Oracle distributes
1391 libraries for Solaris in which some of the objects have
1392 bogus sh_link fields. It would be nice if we could just
1393 reject them, but, unfortunately, some people need to use
1394 them. We scan through the section headers; if we find only
1395 one suitable symbol table, we clobber the sh_link to point
1396 to it. I hope this doesn't break anything. */
1397 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1398 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1400 int scan;
1401 int found;
1403 found = 0;
1404 for (scan = 1; scan < ehdr->e_shnum; scan++)
1406 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1407 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1409 if (found != 0)
1411 found = 0;
1412 break;
1414 found = scan;
1417 if (found != 0)
1418 hdr->sh_link = found;
1421 /* Get the symbol table. */
1422 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1423 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1424 return false;
1426 /* If this reloc section does not use the main symbol table we
1427 don't treat it as a reloc section. BFD can't adequately
1428 represent such a section, so at least for now, we don't
1429 try. We just present it as a normal section. We also
1430 can't use it as a reloc section if it points to the null
1431 section. */
1432 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1433 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1435 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1436 return false;
1437 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1438 if (target_sect == NULL)
1439 return false;
1441 if ((target_sect->flags & SEC_RELOC) == 0
1442 || target_sect->reloc_count == 0)
1443 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1444 else
1446 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1447 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1448 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1450 *hdr2 = *hdr;
1451 elf_elfsections (abfd)[shindex] = hdr2;
1452 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
1453 target_sect->flags |= SEC_RELOC;
1454 target_sect->relocation = NULL;
1455 target_sect->rel_filepos = hdr->sh_offset;
1456 /* In the section to which the relocations apply, mark whether
1457 its relocations are of the REL or RELA variety. */
1458 if (hdr->sh_size != 0)
1459 elf_section_data (target_sect)->use_rela_p
1460 = (hdr->sh_type == SHT_RELA);
1461 abfd->flags |= HAS_RELOC;
1462 return true;
1464 break;
1466 case SHT_GNU_verdef:
1467 elf_dynverdef (abfd) = shindex;
1468 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1469 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1470 break;
1472 case SHT_GNU_versym:
1473 elf_dynversym (abfd) = shindex;
1474 elf_tdata (abfd)->dynversym_hdr = *hdr;
1475 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1476 break;
1478 case SHT_GNU_verneed:
1479 elf_dynverref (abfd) = shindex;
1480 elf_tdata (abfd)->dynverref_hdr = *hdr;
1481 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1482 break;
1484 case SHT_SHLIB:
1485 return true;
1487 default:
1488 /* Check for any processor-specific section types. */
1490 if (bed->elf_backend_section_from_shdr)
1491 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1493 break;
1496 return true;
1499 /* Given an ELF section number, retrieve the corresponding BFD
1500 section. */
1502 asection *
1503 bfd_section_from_elf_index (abfd, index)
1504 bfd *abfd;
1505 unsigned int index;
1507 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1508 if (index >= elf_elfheader (abfd)->e_shnum)
1509 return NULL;
1510 return elf_elfsections (abfd)[index]->bfd_section;
1513 boolean
1514 _bfd_elf_new_section_hook (abfd, sec)
1515 bfd *abfd;
1516 asection *sec;
1518 struct bfd_elf_section_data *sdata;
1520 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1521 if (!sdata)
1522 return false;
1523 sec->used_by_bfd = (PTR) sdata;
1525 /* Indicate whether or not this section should use RELA relocations. */
1526 sdata->use_rela_p
1527 = get_elf_backend_data (abfd)->default_use_rela_p;
1529 return true;
1532 /* Create a new bfd section from an ELF program header.
1534 Since program segments have no names, we generate a synthetic name
1535 of the form segment<NUM>, where NUM is generally the index in the
1536 program header table. For segments that are split (see below) we
1537 generate the names segment<NUM>a and segment<NUM>b.
1539 Note that some program segments may have a file size that is different than
1540 (less than) the memory size. All this means is that at execution the
1541 system must allocate the amount of memory specified by the memory size,
1542 but only initialize it with the first "file size" bytes read from the
1543 file. This would occur for example, with program segments consisting
1544 of combined data+bss.
1546 To handle the above situation, this routine generates TWO bfd sections
1547 for the single program segment. The first has the length specified by
1548 the file size of the segment, and the second has the length specified
1549 by the difference between the two sizes. In effect, the segment is split
1550 into it's initialized and uninitialized parts.
1554 boolean
1555 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1556 bfd *abfd;
1557 Elf_Internal_Phdr *hdr;
1558 int index;
1559 const char *typename;
1561 asection *newsect;
1562 char *name;
1563 char namebuf[64];
1564 int split;
1566 split = ((hdr->p_memsz > 0)
1567 && (hdr->p_filesz > 0)
1568 && (hdr->p_memsz > hdr->p_filesz));
1569 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1570 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1571 if (!name)
1572 return false;
1573 strcpy (name, namebuf);
1574 newsect = bfd_make_section (abfd, name);
1575 if (newsect == NULL)
1576 return false;
1577 newsect->vma = hdr->p_vaddr;
1578 newsect->lma = hdr->p_paddr;
1579 newsect->_raw_size = hdr->p_filesz;
1580 newsect->filepos = hdr->p_offset;
1581 newsect->flags |= SEC_HAS_CONTENTS;
1582 if (hdr->p_type == PT_LOAD)
1584 newsect->flags |= SEC_ALLOC;
1585 newsect->flags |= SEC_LOAD;
1586 if (hdr->p_flags & PF_X)
1588 /* FIXME: all we known is that it has execute PERMISSION,
1589 may be data. */
1590 newsect->flags |= SEC_CODE;
1593 if (!(hdr->p_flags & PF_W))
1595 newsect->flags |= SEC_READONLY;
1598 if (split)
1600 sprintf (namebuf, "%s%db", typename, index);
1601 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1602 if (!name)
1603 return false;
1604 strcpy (name, namebuf);
1605 newsect = bfd_make_section (abfd, name);
1606 if (newsect == NULL)
1607 return false;
1608 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1609 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1610 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1611 if (hdr->p_type == PT_LOAD)
1613 newsect->flags |= SEC_ALLOC;
1614 if (hdr->p_flags & PF_X)
1615 newsect->flags |= SEC_CODE;
1617 if (!(hdr->p_flags & PF_W))
1618 newsect->flags |= SEC_READONLY;
1621 return true;
1624 boolean
1625 bfd_section_from_phdr (abfd, hdr, index)
1626 bfd *abfd;
1627 Elf_Internal_Phdr *hdr;
1628 int index;
1630 struct elf_backend_data *bed;
1632 switch (hdr->p_type)
1634 case PT_NULL:
1635 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1637 case PT_LOAD:
1638 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1640 case PT_DYNAMIC:
1641 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1643 case PT_INTERP:
1644 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1646 case PT_NOTE:
1647 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1648 return false;
1649 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1650 return false;
1651 return true;
1653 case PT_SHLIB:
1654 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1656 case PT_PHDR:
1657 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1659 default:
1660 /* Check for any processor-specific program segment types.
1661 If no handler for them, default to making "segment" sections. */
1662 bed = get_elf_backend_data (abfd);
1663 if (bed->elf_backend_section_from_phdr)
1664 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1665 else
1666 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1670 /* Initialize REL_HDR, the section-header for new section, containing
1671 relocations against ASECT. If USE_RELA_P is true, we use RELA
1672 relocations; otherwise, we use REL relocations. */
1674 boolean
1675 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1676 bfd *abfd;
1677 Elf_Internal_Shdr *rel_hdr;
1678 asection *asect;
1679 boolean use_rela_p;
1681 char *name;
1682 struct elf_backend_data *bed;
1684 bed = get_elf_backend_data (abfd);
1685 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1686 if (name == NULL)
1687 return false;
1688 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1689 rel_hdr->sh_name =
1690 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1691 true, false);
1692 if (rel_hdr->sh_name == (unsigned int) -1)
1693 return false;
1694 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1695 rel_hdr->sh_entsize = (use_rela_p
1696 ? bed->s->sizeof_rela
1697 : bed->s->sizeof_rel);
1698 rel_hdr->sh_addralign = bed->s->file_align;
1699 rel_hdr->sh_flags = 0;
1700 rel_hdr->sh_addr = 0;
1701 rel_hdr->sh_size = 0;
1702 rel_hdr->sh_offset = 0;
1704 return true;
1707 /* Set up an ELF internal section header for a section. */
1709 static void
1710 elf_fake_sections (abfd, asect, failedptrarg)
1711 bfd *abfd;
1712 asection *asect;
1713 PTR failedptrarg;
1715 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1716 boolean *failedptr = (boolean *) failedptrarg;
1717 Elf_Internal_Shdr *this_hdr;
1719 if (*failedptr)
1721 /* We already failed; just get out of the bfd_map_over_sections
1722 loop. */
1723 return;
1726 this_hdr = &elf_section_data (asect)->this_hdr;
1728 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1729 asect->name,
1730 true, false);
1731 if (this_hdr->sh_name == (unsigned long) -1)
1733 *failedptr = true;
1734 return;
1737 this_hdr->sh_flags = 0;
1739 if ((asect->flags & SEC_ALLOC) != 0
1740 || asect->user_set_vma)
1741 this_hdr->sh_addr = asect->vma;
1742 else
1743 this_hdr->sh_addr = 0;
1745 this_hdr->sh_offset = 0;
1746 this_hdr->sh_size = asect->_raw_size;
1747 this_hdr->sh_link = 0;
1748 this_hdr->sh_addralign = 1 << asect->alignment_power;
1749 /* The sh_entsize and sh_info fields may have been set already by
1750 copy_private_section_data. */
1752 this_hdr->bfd_section = asect;
1753 this_hdr->contents = NULL;
1755 /* FIXME: This should not be based on section names. */
1756 if (strcmp (asect->name, ".dynstr") == 0)
1757 this_hdr->sh_type = SHT_STRTAB;
1758 else if (strcmp (asect->name, ".hash") == 0)
1760 this_hdr->sh_type = SHT_HASH;
1761 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1763 else if (strcmp (asect->name, ".dynsym") == 0)
1765 this_hdr->sh_type = SHT_DYNSYM;
1766 this_hdr->sh_entsize = bed->s->sizeof_sym;
1768 else if (strcmp (asect->name, ".dynamic") == 0)
1770 this_hdr->sh_type = SHT_DYNAMIC;
1771 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1773 else if (strncmp (asect->name, ".rela", 5) == 0
1774 && get_elf_backend_data (abfd)->may_use_rela_p)
1776 this_hdr->sh_type = SHT_RELA;
1777 this_hdr->sh_entsize = bed->s->sizeof_rela;
1779 else if (strncmp (asect->name, ".rel", 4) == 0
1780 && get_elf_backend_data (abfd)->may_use_rel_p)
1782 this_hdr->sh_type = SHT_REL;
1783 this_hdr->sh_entsize = bed->s->sizeof_rel;
1785 else if (strncmp (asect->name, ".note", 5) == 0)
1786 this_hdr->sh_type = SHT_NOTE;
1787 else if (strncmp (asect->name, ".stab", 5) == 0
1788 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1789 this_hdr->sh_type = SHT_STRTAB;
1790 else if (strcmp (asect->name, ".gnu.version") == 0)
1792 this_hdr->sh_type = SHT_GNU_versym;
1793 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1795 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1797 this_hdr->sh_type = SHT_GNU_verdef;
1798 this_hdr->sh_entsize = 0;
1799 /* objcopy or strip will copy over sh_info, but may not set
1800 cverdefs. The linker will set cverdefs, but sh_info will be
1801 zero. */
1802 if (this_hdr->sh_info == 0)
1803 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1804 else
1805 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1806 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1808 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1810 this_hdr->sh_type = SHT_GNU_verneed;
1811 this_hdr->sh_entsize = 0;
1812 /* objcopy or strip will copy over sh_info, but may not set
1813 cverrefs. The linker will set cverrefs, but sh_info will be
1814 zero. */
1815 if (this_hdr->sh_info == 0)
1816 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1817 else
1818 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1819 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1821 else if ((asect->flags & SEC_ALLOC) != 0
1822 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
1823 this_hdr->sh_type = SHT_NOBITS;
1824 else
1825 this_hdr->sh_type = SHT_PROGBITS;
1827 if ((asect->flags & SEC_ALLOC) != 0)
1828 this_hdr->sh_flags |= SHF_ALLOC;
1829 if ((asect->flags & SEC_READONLY) == 0)
1830 this_hdr->sh_flags |= SHF_WRITE;
1831 if ((asect->flags & SEC_CODE) != 0)
1832 this_hdr->sh_flags |= SHF_EXECINSTR;
1833 if ((asect->flags & SEC_MERGE) != 0)
1835 this_hdr->sh_flags |= SHF_MERGE;
1836 this_hdr->sh_entsize = asect->entsize;
1837 if ((asect->flags & SEC_STRINGS) != 0)
1838 this_hdr->sh_flags |= SHF_STRINGS;
1841 /* Check for processor-specific section types. */
1842 if (bed->elf_backend_fake_sections)
1843 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1845 /* If the section has relocs, set up a section header for the
1846 SHT_REL[A] section. If two relocation sections are required for
1847 this section, it is up to the processor-specific back-end to
1848 create the other. */
1849 if ((asect->flags & SEC_RELOC) != 0
1850 && !_bfd_elf_init_reloc_shdr (abfd,
1851 &elf_section_data (asect)->rel_hdr,
1852 asect,
1853 elf_section_data (asect)->use_rela_p))
1854 *failedptr = true;
1857 /* Assign all ELF section numbers. The dummy first section is handled here
1858 too. The link/info pointers for the standard section types are filled
1859 in here too, while we're at it. */
1861 static boolean
1862 assign_section_numbers (abfd)
1863 bfd *abfd;
1865 struct elf_obj_tdata *t = elf_tdata (abfd);
1866 asection *sec;
1867 unsigned int section_number;
1868 Elf_Internal_Shdr **i_shdrp;
1870 section_number = 1;
1872 for (sec = abfd->sections; sec; sec = sec->next)
1874 struct bfd_elf_section_data *d = elf_section_data (sec);
1876 d->this_idx = section_number++;
1877 if ((sec->flags & SEC_RELOC) == 0)
1878 d->rel_idx = 0;
1879 else
1880 d->rel_idx = section_number++;
1882 if (d->rel_hdr2)
1883 d->rel_idx2 = section_number++;
1884 else
1885 d->rel_idx2 = 0;
1888 t->shstrtab_section = section_number++;
1889 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1890 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1892 if (bfd_get_symcount (abfd) > 0)
1894 t->symtab_section = section_number++;
1895 t->strtab_section = section_number++;
1898 elf_elfheader (abfd)->e_shnum = section_number;
1900 /* Set up the list of section header pointers, in agreement with the
1901 indices. */
1902 i_shdrp = ((Elf_Internal_Shdr **)
1903 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1904 if (i_shdrp == NULL)
1905 return false;
1907 i_shdrp[0] = ((Elf_Internal_Shdr *)
1908 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1909 if (i_shdrp[0] == NULL)
1911 bfd_release (abfd, i_shdrp);
1912 return false;
1914 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1916 elf_elfsections (abfd) = i_shdrp;
1918 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1919 if (bfd_get_symcount (abfd) > 0)
1921 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1922 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1923 t->symtab_hdr.sh_link = t->strtab_section;
1925 for (sec = abfd->sections; sec; sec = sec->next)
1927 struct bfd_elf_section_data *d = elf_section_data (sec);
1928 asection *s;
1929 const char *name;
1931 i_shdrp[d->this_idx] = &d->this_hdr;
1932 if (d->rel_idx != 0)
1933 i_shdrp[d->rel_idx] = &d->rel_hdr;
1934 if (d->rel_idx2 != 0)
1935 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1937 /* Fill in the sh_link and sh_info fields while we're at it. */
1939 /* sh_link of a reloc section is the section index of the symbol
1940 table. sh_info is the section index of the section to which
1941 the relocation entries apply. */
1942 if (d->rel_idx != 0)
1944 d->rel_hdr.sh_link = t->symtab_section;
1945 d->rel_hdr.sh_info = d->this_idx;
1947 if (d->rel_idx2 != 0)
1949 d->rel_hdr2->sh_link = t->symtab_section;
1950 d->rel_hdr2->sh_info = d->this_idx;
1953 switch (d->this_hdr.sh_type)
1955 case SHT_REL:
1956 case SHT_RELA:
1957 /* A reloc section which we are treating as a normal BFD
1958 section. sh_link is the section index of the symbol
1959 table. sh_info is the section index of the section to
1960 which the relocation entries apply. We assume that an
1961 allocated reloc section uses the dynamic symbol table.
1962 FIXME: How can we be sure? */
1963 s = bfd_get_section_by_name (abfd, ".dynsym");
1964 if (s != NULL)
1965 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1967 /* We look up the section the relocs apply to by name. */
1968 name = sec->name;
1969 if (d->this_hdr.sh_type == SHT_REL)
1970 name += 4;
1971 else
1972 name += 5;
1973 s = bfd_get_section_by_name (abfd, name);
1974 if (s != NULL)
1975 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1976 break;
1978 case SHT_STRTAB:
1979 /* We assume that a section named .stab*str is a stabs
1980 string section. We look for a section with the same name
1981 but without the trailing ``str'', and set its sh_link
1982 field to point to this section. */
1983 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1984 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1986 size_t len;
1987 char *alc;
1989 len = strlen (sec->name);
1990 alc = (char *) bfd_malloc (len - 2);
1991 if (alc == NULL)
1992 return false;
1993 strncpy (alc, sec->name, len - 3);
1994 alc[len - 3] = '\0';
1995 s = bfd_get_section_by_name (abfd, alc);
1996 free (alc);
1997 if (s != NULL)
1999 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
2001 /* This is a .stab section. */
2002 elf_section_data (s)->this_hdr.sh_entsize =
2003 4 + 2 * bfd_get_arch_size (abfd) / 8;
2006 break;
2008 case SHT_DYNAMIC:
2009 case SHT_DYNSYM:
2010 case SHT_GNU_verneed:
2011 case SHT_GNU_verdef:
2012 /* sh_link is the section header index of the string table
2013 used for the dynamic entries, or the symbol table, or the
2014 version strings. */
2015 s = bfd_get_section_by_name (abfd, ".dynstr");
2016 if (s != NULL)
2017 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2018 break;
2020 case SHT_HASH:
2021 case SHT_GNU_versym:
2022 /* sh_link is the section header index of the symbol table
2023 this hash table or version table is for. */
2024 s = bfd_get_section_by_name (abfd, ".dynsym");
2025 if (s != NULL)
2026 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2027 break;
2031 return true;
2034 /* Map symbol from it's internal number to the external number, moving
2035 all local symbols to be at the head of the list. */
2037 static INLINE int
2038 sym_is_global (abfd, sym)
2039 bfd *abfd;
2040 asymbol *sym;
2042 /* If the backend has a special mapping, use it. */
2043 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2044 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2045 (abfd, sym));
2047 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2048 || bfd_is_und_section (bfd_get_section (sym))
2049 || bfd_is_com_section (bfd_get_section (sym)));
2052 static boolean
2053 elf_map_symbols (abfd)
2054 bfd *abfd;
2056 int symcount = bfd_get_symcount (abfd);
2057 asymbol **syms = bfd_get_outsymbols (abfd);
2058 asymbol **sect_syms;
2059 int num_locals = 0;
2060 int num_globals = 0;
2061 int num_locals2 = 0;
2062 int num_globals2 = 0;
2063 int max_index = 0;
2064 int num_sections = 0;
2065 int idx;
2066 asection *asect;
2067 asymbol **new_syms;
2068 asymbol *sym;
2070 #ifdef DEBUG
2071 fprintf (stderr, "elf_map_symbols\n");
2072 fflush (stderr);
2073 #endif
2075 /* Add a section symbol for each BFD section. FIXME: Is this really
2076 necessary? */
2077 for (asect = abfd->sections; asect; asect = asect->next)
2079 if (max_index < asect->index)
2080 max_index = asect->index;
2083 max_index++;
2084 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2085 if (sect_syms == NULL)
2086 return false;
2087 elf_section_syms (abfd) = sect_syms;
2089 for (idx = 0; idx < symcount; idx++)
2091 sym = syms[idx];
2093 if ((sym->flags & BSF_SECTION_SYM) != 0
2094 && sym->value == 0)
2096 asection *sec;
2098 sec = sym->section;
2100 if (sec->owner != NULL)
2102 if (sec->owner != abfd)
2104 if (sec->output_offset != 0)
2105 continue;
2107 sec = sec->output_section;
2109 /* Empty sections in the input files may have had a section
2110 symbol created for them. (See the comment near the end of
2111 _bfd_generic_link_output_symbols in linker.c). If the linker
2112 script discards such sections then we will reach this point.
2113 Since we know that we cannot avoid this case, we detect it
2114 and skip the abort and the assignment to the sect_syms array.
2115 To reproduce this particular case try running the linker
2116 testsuite test ld-scripts/weak.exp for an ELF port that uses
2117 the generic linker. */
2118 if (sec->owner == NULL)
2119 continue;
2121 BFD_ASSERT (sec->owner == abfd);
2123 sect_syms[sec->index] = syms[idx];
2128 for (asect = abfd->sections; asect; asect = asect->next)
2130 if (sect_syms[asect->index] != NULL)
2131 continue;
2133 sym = bfd_make_empty_symbol (abfd);
2134 if (sym == NULL)
2135 return false;
2136 sym->the_bfd = abfd;
2137 sym->name = asect->name;
2138 sym->value = 0;
2139 /* Set the flags to 0 to indicate that this one was newly added. */
2140 sym->flags = 0;
2141 sym->section = asect;
2142 sect_syms[asect->index] = sym;
2143 num_sections++;
2144 #ifdef DEBUG
2145 fprintf (stderr,
2146 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2147 asect->name, (long) asect->vma, asect->index, (long) asect);
2148 #endif
2151 /* Classify all of the symbols. */
2152 for (idx = 0; idx < symcount; idx++)
2154 if (!sym_is_global (abfd, syms[idx]))
2155 num_locals++;
2156 else
2157 num_globals++;
2159 for (asect = abfd->sections; asect; asect = asect->next)
2161 if (sect_syms[asect->index] != NULL
2162 && sect_syms[asect->index]->flags == 0)
2164 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2165 if (!sym_is_global (abfd, sect_syms[asect->index]))
2166 num_locals++;
2167 else
2168 num_globals++;
2169 sect_syms[asect->index]->flags = 0;
2173 /* Now sort the symbols so the local symbols are first. */
2174 new_syms = ((asymbol **)
2175 bfd_alloc (abfd,
2176 (num_locals + num_globals) * sizeof (asymbol *)));
2177 if (new_syms == NULL)
2178 return false;
2180 for (idx = 0; idx < symcount; idx++)
2182 asymbol *sym = syms[idx];
2183 int i;
2185 if (!sym_is_global (abfd, sym))
2186 i = num_locals2++;
2187 else
2188 i = num_locals + num_globals2++;
2189 new_syms[i] = sym;
2190 sym->udata.i = i + 1;
2192 for (asect = abfd->sections; asect; asect = asect->next)
2194 if (sect_syms[asect->index] != NULL
2195 && sect_syms[asect->index]->flags == 0)
2197 asymbol *sym = sect_syms[asect->index];
2198 int i;
2200 sym->flags = BSF_SECTION_SYM;
2201 if (!sym_is_global (abfd, sym))
2202 i = num_locals2++;
2203 else
2204 i = num_locals + num_globals2++;
2205 new_syms[i] = sym;
2206 sym->udata.i = i + 1;
2210 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2212 elf_num_locals (abfd) = num_locals;
2213 elf_num_globals (abfd) = num_globals;
2214 return true;
2217 /* Align to the maximum file alignment that could be required for any
2218 ELF data structure. */
2220 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2221 static INLINE file_ptr
2222 align_file_position (off, align)
2223 file_ptr off;
2224 int align;
2226 return (off + align - 1) & ~(align - 1);
2229 /* Assign a file position to a section, optionally aligning to the
2230 required section alignment. */
2232 INLINE file_ptr
2233 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2234 Elf_Internal_Shdr *i_shdrp;
2235 file_ptr offset;
2236 boolean align;
2238 if (align)
2240 unsigned int al;
2242 al = i_shdrp->sh_addralign;
2243 if (al > 1)
2244 offset = BFD_ALIGN (offset, al);
2246 i_shdrp->sh_offset = offset;
2247 if (i_shdrp->bfd_section != NULL)
2248 i_shdrp->bfd_section->filepos = offset;
2249 if (i_shdrp->sh_type != SHT_NOBITS)
2250 offset += i_shdrp->sh_size;
2251 return offset;
2254 /* Compute the file positions we are going to put the sections at, and
2255 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2256 is not NULL, this is being called by the ELF backend linker. */
2258 boolean
2259 _bfd_elf_compute_section_file_positions (abfd, link_info)
2260 bfd *abfd;
2261 struct bfd_link_info *link_info;
2263 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2264 boolean failed;
2265 struct bfd_strtab_hash *strtab;
2266 Elf_Internal_Shdr *shstrtab_hdr;
2268 if (abfd->output_has_begun)
2269 return true;
2271 /* Do any elf backend specific processing first. */
2272 if (bed->elf_backend_begin_write_processing)
2273 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2275 if (! prep_headers (abfd))
2276 return false;
2278 /* Post process the headers if necessary. */
2279 if (bed->elf_backend_post_process_headers)
2280 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2282 failed = false;
2283 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2284 if (failed)
2285 return false;
2287 if (!assign_section_numbers (abfd))
2288 return false;
2290 /* The backend linker builds symbol table information itself. */
2291 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2293 /* Non-zero if doing a relocatable link. */
2294 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2296 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2297 return false;
2300 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2301 /* sh_name was set in prep_headers. */
2302 shstrtab_hdr->sh_type = SHT_STRTAB;
2303 shstrtab_hdr->sh_flags = 0;
2304 shstrtab_hdr->sh_addr = 0;
2305 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2306 shstrtab_hdr->sh_entsize = 0;
2307 shstrtab_hdr->sh_link = 0;
2308 shstrtab_hdr->sh_info = 0;
2309 /* sh_offset is set in assign_file_positions_except_relocs. */
2310 shstrtab_hdr->sh_addralign = 1;
2312 if (!assign_file_positions_except_relocs (abfd))
2313 return false;
2315 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2317 file_ptr off;
2318 Elf_Internal_Shdr *hdr;
2320 off = elf_tdata (abfd)->next_file_pos;
2322 hdr = &elf_tdata (abfd)->symtab_hdr;
2323 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2325 hdr = &elf_tdata (abfd)->strtab_hdr;
2326 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2328 elf_tdata (abfd)->next_file_pos = off;
2330 /* Now that we know where the .strtab section goes, write it
2331 out. */
2332 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2333 || ! _bfd_stringtab_emit (abfd, strtab))
2334 return false;
2335 _bfd_stringtab_free (strtab);
2338 abfd->output_has_begun = true;
2340 return true;
2343 /* Create a mapping from a set of sections to a program segment. */
2345 static INLINE struct elf_segment_map *
2346 make_mapping (abfd, sections, from, to, phdr)
2347 bfd *abfd;
2348 asection **sections;
2349 unsigned int from;
2350 unsigned int to;
2351 boolean phdr;
2353 struct elf_segment_map *m;
2354 unsigned int i;
2355 asection **hdrpp;
2357 m = ((struct elf_segment_map *)
2358 bfd_zalloc (abfd,
2359 (sizeof (struct elf_segment_map)
2360 + (to - from - 1) * sizeof (asection *))));
2361 if (m == NULL)
2362 return NULL;
2363 m->next = NULL;
2364 m->p_type = PT_LOAD;
2365 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2366 m->sections[i - from] = *hdrpp;
2367 m->count = to - from;
2369 if (from == 0 && phdr)
2371 /* Include the headers in the first PT_LOAD segment. */
2372 m->includes_filehdr = 1;
2373 m->includes_phdrs = 1;
2376 return m;
2379 /* Set up a mapping from BFD sections to program segments. */
2381 static boolean
2382 map_sections_to_segments (abfd)
2383 bfd *abfd;
2385 asection **sections = NULL;
2386 asection *s;
2387 unsigned int i;
2388 unsigned int count;
2389 struct elf_segment_map *mfirst;
2390 struct elf_segment_map **pm;
2391 struct elf_segment_map *m;
2392 asection *last_hdr;
2393 unsigned int phdr_index;
2394 bfd_vma maxpagesize;
2395 asection **hdrpp;
2396 boolean phdr_in_segment = true;
2397 boolean writable;
2398 asection *dynsec;
2400 if (elf_tdata (abfd)->segment_map != NULL)
2401 return true;
2403 if (bfd_count_sections (abfd) == 0)
2404 return true;
2406 /* Select the allocated sections, and sort them. */
2408 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2409 * sizeof (asection *));
2410 if (sections == NULL)
2411 goto error_return;
2413 i = 0;
2414 for (s = abfd->sections; s != NULL; s = s->next)
2416 if ((s->flags & SEC_ALLOC) != 0)
2418 sections[i] = s;
2419 ++i;
2422 BFD_ASSERT (i <= bfd_count_sections (abfd));
2423 count = i;
2425 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2427 /* Build the mapping. */
2429 mfirst = NULL;
2430 pm = &mfirst;
2432 /* If we have a .interp section, then create a PT_PHDR segment for
2433 the program headers and a PT_INTERP segment for the .interp
2434 section. */
2435 s = bfd_get_section_by_name (abfd, ".interp");
2436 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2438 m = ((struct elf_segment_map *)
2439 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2440 if (m == NULL)
2441 goto error_return;
2442 m->next = NULL;
2443 m->p_type = PT_PHDR;
2444 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2445 m->p_flags = PF_R | PF_X;
2446 m->p_flags_valid = 1;
2447 m->includes_phdrs = 1;
2449 *pm = m;
2450 pm = &m->next;
2452 m = ((struct elf_segment_map *)
2453 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2454 if (m == NULL)
2455 goto error_return;
2456 m->next = NULL;
2457 m->p_type = PT_INTERP;
2458 m->count = 1;
2459 m->sections[0] = s;
2461 *pm = m;
2462 pm = &m->next;
2465 /* Look through the sections. We put sections in the same program
2466 segment when the start of the second section can be placed within
2467 a few bytes of the end of the first section. */
2468 last_hdr = NULL;
2469 phdr_index = 0;
2470 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2471 writable = false;
2472 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2473 if (dynsec != NULL
2474 && (dynsec->flags & SEC_LOAD) == 0)
2475 dynsec = NULL;
2477 /* Deal with -Ttext or something similar such that the first section
2478 is not adjacent to the program headers. This is an
2479 approximation, since at this point we don't know exactly how many
2480 program headers we will need. */
2481 if (count > 0)
2483 bfd_size_type phdr_size;
2485 phdr_size = elf_tdata (abfd)->program_header_size;
2486 if (phdr_size == 0)
2487 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2488 if ((abfd->flags & D_PAGED) == 0
2489 || sections[0]->lma < phdr_size
2490 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2491 phdr_in_segment = false;
2494 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2496 asection *hdr;
2497 boolean new_segment;
2499 hdr = *hdrpp;
2501 /* See if this section and the last one will fit in the same
2502 segment. */
2504 if (last_hdr == NULL)
2506 /* If we don't have a segment yet, then we don't need a new
2507 one (we build the last one after this loop). */
2508 new_segment = false;
2510 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2512 /* If this section has a different relation between the
2513 virtual address and the load address, then we need a new
2514 segment. */
2515 new_segment = true;
2517 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2518 < BFD_ALIGN (hdr->lma, maxpagesize))
2520 /* If putting this section in this segment would force us to
2521 skip a page in the segment, then we need a new segment. */
2522 new_segment = true;
2524 else if ((last_hdr->flags & SEC_LOAD) == 0
2525 && (hdr->flags & SEC_LOAD) != 0)
2527 /* We don't want to put a loadable section after a
2528 nonloadable section in the same segment. */
2529 new_segment = true;
2531 else if ((abfd->flags & D_PAGED) == 0)
2533 /* If the file is not demand paged, which means that we
2534 don't require the sections to be correctly aligned in the
2535 file, then there is no other reason for a new segment. */
2536 new_segment = false;
2538 else if (! writable
2539 && (hdr->flags & SEC_READONLY) == 0
2540 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2541 == hdr->lma))
2543 /* We don't want to put a writable section in a read only
2544 segment, unless they are on the same page in memory
2545 anyhow. We already know that the last section does not
2546 bring us past the current section on the page, so the
2547 only case in which the new section is not on the same
2548 page as the previous section is when the previous section
2549 ends precisely on a page boundary. */
2550 new_segment = true;
2552 else
2554 /* Otherwise, we can use the same segment. */
2555 new_segment = false;
2558 if (! new_segment)
2560 if ((hdr->flags & SEC_READONLY) == 0)
2561 writable = true;
2562 last_hdr = hdr;
2563 continue;
2566 /* We need a new program segment. We must create a new program
2567 header holding all the sections from phdr_index until hdr. */
2569 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2570 if (m == NULL)
2571 goto error_return;
2573 *pm = m;
2574 pm = &m->next;
2576 if ((hdr->flags & SEC_READONLY) == 0)
2577 writable = true;
2578 else
2579 writable = false;
2581 last_hdr = hdr;
2582 phdr_index = i;
2583 phdr_in_segment = false;
2586 /* Create a final PT_LOAD program segment. */
2587 if (last_hdr != NULL)
2589 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2590 if (m == NULL)
2591 goto error_return;
2593 *pm = m;
2594 pm = &m->next;
2597 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2598 if (dynsec != NULL)
2600 m = ((struct elf_segment_map *)
2601 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2602 if (m == NULL)
2603 goto error_return;
2604 m->next = NULL;
2605 m->p_type = PT_DYNAMIC;
2606 m->count = 1;
2607 m->sections[0] = dynsec;
2609 *pm = m;
2610 pm = &m->next;
2613 /* For each loadable .note section, add a PT_NOTE segment. We don't
2614 use bfd_get_section_by_name, because if we link together
2615 nonloadable .note sections and loadable .note sections, we will
2616 generate two .note sections in the output file. FIXME: Using
2617 names for section types is bogus anyhow. */
2618 for (s = abfd->sections; s != NULL; s = s->next)
2620 if ((s->flags & SEC_LOAD) != 0
2621 && strncmp (s->name, ".note", 5) == 0)
2623 m = ((struct elf_segment_map *)
2624 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2625 if (m == NULL)
2626 goto error_return;
2627 m->next = NULL;
2628 m->p_type = PT_NOTE;
2629 m->count = 1;
2630 m->sections[0] = s;
2632 *pm = m;
2633 pm = &m->next;
2637 free (sections);
2638 sections = NULL;
2640 elf_tdata (abfd)->segment_map = mfirst;
2641 return true;
2643 error_return:
2644 if (sections != NULL)
2645 free (sections);
2646 return false;
2649 /* Sort sections by address. */
2651 static int
2652 elf_sort_sections (arg1, arg2)
2653 const PTR arg1;
2654 const PTR arg2;
2656 const asection *sec1 = *(const asection **) arg1;
2657 const asection *sec2 = *(const asection **) arg2;
2659 /* Sort by LMA first, since this is the address used to
2660 place the section into a segment. */
2661 if (sec1->lma < sec2->lma)
2662 return -1;
2663 else if (sec1->lma > sec2->lma)
2664 return 1;
2666 /* Then sort by VMA. Normally the LMA and the VMA will be
2667 the same, and this will do nothing. */
2668 if (sec1->vma < sec2->vma)
2669 return -1;
2670 else if (sec1->vma > sec2->vma)
2671 return 1;
2673 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2675 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2677 if (TOEND (sec1))
2679 if (TOEND (sec2))
2680 return sec1->target_index - sec2->target_index;
2681 else
2682 return 1;
2685 if (TOEND (sec2))
2686 return -1;
2688 #undef TOEND
2690 /* Sort by size, to put zero sized sections before others at the
2691 same address. */
2693 if (sec1->_raw_size < sec2->_raw_size)
2694 return -1;
2695 if (sec1->_raw_size > sec2->_raw_size)
2696 return 1;
2698 return sec1->target_index - sec2->target_index;
2701 /* Assign file positions to the sections based on the mapping from
2702 sections to segments. This function also sets up some fields in
2703 the file header, and writes out the program headers. */
2705 static boolean
2706 assign_file_positions_for_segments (abfd)
2707 bfd *abfd;
2709 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2710 unsigned int count;
2711 struct elf_segment_map *m;
2712 unsigned int alloc;
2713 Elf_Internal_Phdr *phdrs;
2714 file_ptr off, voff;
2715 bfd_vma filehdr_vaddr, filehdr_paddr;
2716 bfd_vma phdrs_vaddr, phdrs_paddr;
2717 Elf_Internal_Phdr *p;
2719 if (elf_tdata (abfd)->segment_map == NULL)
2721 if (! map_sections_to_segments (abfd))
2722 return false;
2725 if (bed->elf_backend_modify_segment_map)
2727 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2728 return false;
2731 count = 0;
2732 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2733 ++count;
2735 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2736 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2737 elf_elfheader (abfd)->e_phnum = count;
2739 if (count == 0)
2740 return true;
2742 /* If we already counted the number of program segments, make sure
2743 that we allocated enough space. This happens when SIZEOF_HEADERS
2744 is used in a linker script. */
2745 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2746 if (alloc != 0 && count > alloc)
2748 ((*_bfd_error_handler)
2749 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2750 bfd_get_filename (abfd), alloc, count));
2751 bfd_set_error (bfd_error_bad_value);
2752 return false;
2755 if (alloc == 0)
2756 alloc = count;
2758 phdrs = ((Elf_Internal_Phdr *)
2759 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2760 if (phdrs == NULL)
2761 return false;
2763 off = bed->s->sizeof_ehdr;
2764 off += alloc * bed->s->sizeof_phdr;
2766 filehdr_vaddr = 0;
2767 filehdr_paddr = 0;
2768 phdrs_vaddr = 0;
2769 phdrs_paddr = 0;
2771 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2772 m != NULL;
2773 m = m->next, p++)
2775 unsigned int i;
2776 asection **secpp;
2778 /* If elf_segment_map is not from map_sections_to_segments, the
2779 sections may not be correctly ordered. */
2780 if (m->count > 0)
2781 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2782 elf_sort_sections);
2784 p->p_type = m->p_type;
2785 p->p_flags = m->p_flags;
2787 if (p->p_type == PT_LOAD
2788 && m->count > 0
2789 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2791 if ((abfd->flags & D_PAGED) != 0)
2792 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2793 else
2795 bfd_size_type align;
2797 align = 0;
2798 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2800 bfd_size_type secalign;
2802 secalign = bfd_get_section_alignment (abfd, *secpp);
2803 if (secalign > align)
2804 align = secalign;
2807 off += (m->sections[0]->vma - off) % (1 << align);
2811 if (m->count == 0)
2812 p->p_vaddr = 0;
2813 else
2814 p->p_vaddr = m->sections[0]->vma;
2816 if (m->p_paddr_valid)
2817 p->p_paddr = m->p_paddr;
2818 else if (m->count == 0)
2819 p->p_paddr = 0;
2820 else
2821 p->p_paddr = m->sections[0]->lma;
2823 if (p->p_type == PT_LOAD
2824 && (abfd->flags & D_PAGED) != 0)
2825 p->p_align = bed->maxpagesize;
2826 else if (m->count == 0)
2827 p->p_align = bed->s->file_align;
2828 else
2829 p->p_align = 0;
2831 p->p_offset = 0;
2832 p->p_filesz = 0;
2833 p->p_memsz = 0;
2835 if (m->includes_filehdr)
2837 if (! m->p_flags_valid)
2838 p->p_flags |= PF_R;
2839 p->p_offset = 0;
2840 p->p_filesz = bed->s->sizeof_ehdr;
2841 p->p_memsz = bed->s->sizeof_ehdr;
2842 if (m->count > 0)
2844 BFD_ASSERT (p->p_type == PT_LOAD);
2846 if (p->p_vaddr < (bfd_vma) off)
2848 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2849 bfd_get_filename (abfd));
2850 bfd_set_error (bfd_error_bad_value);
2851 return false;
2854 p->p_vaddr -= off;
2855 if (! m->p_paddr_valid)
2856 p->p_paddr -= off;
2858 if (p->p_type == PT_LOAD)
2860 filehdr_vaddr = p->p_vaddr;
2861 filehdr_paddr = p->p_paddr;
2865 if (m->includes_phdrs)
2867 if (! m->p_flags_valid)
2868 p->p_flags |= PF_R;
2870 if (m->includes_filehdr)
2872 if (p->p_type == PT_LOAD)
2874 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2875 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2878 else
2880 p->p_offset = bed->s->sizeof_ehdr;
2882 if (m->count > 0)
2884 BFD_ASSERT (p->p_type == PT_LOAD);
2885 p->p_vaddr -= off - p->p_offset;
2886 if (! m->p_paddr_valid)
2887 p->p_paddr -= off - p->p_offset;
2890 if (p->p_type == PT_LOAD)
2892 phdrs_vaddr = p->p_vaddr;
2893 phdrs_paddr = p->p_paddr;
2895 else
2896 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2899 p->p_filesz += alloc * bed->s->sizeof_phdr;
2900 p->p_memsz += alloc * bed->s->sizeof_phdr;
2903 if (p->p_type == PT_LOAD
2904 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2906 if (! m->includes_filehdr && ! m->includes_phdrs)
2907 p->p_offset = off;
2908 else
2910 file_ptr adjust;
2912 adjust = off - (p->p_offset + p->p_filesz);
2913 p->p_filesz += adjust;
2914 p->p_memsz += adjust;
2918 voff = off;
2920 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2922 asection *sec;
2923 flagword flags;
2924 bfd_size_type align;
2926 sec = *secpp;
2927 flags = sec->flags;
2928 align = 1 << bfd_get_section_alignment (abfd, sec);
2930 /* The section may have artificial alignment forced by a
2931 link script. Notice this case by the gap between the
2932 cumulative phdr vma and the section's vma. */
2933 if (p->p_vaddr + p->p_memsz < sec->vma)
2935 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2937 p->p_memsz += adjust;
2938 off += adjust;
2939 voff += adjust;
2940 if ((flags & SEC_LOAD) != 0)
2941 p->p_filesz += adjust;
2944 if (p->p_type == PT_LOAD)
2946 bfd_signed_vma adjust;
2948 if ((flags & SEC_LOAD) != 0)
2950 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2951 if (adjust < 0)
2952 adjust = 0;
2954 else if ((flags & SEC_ALLOC) != 0)
2956 /* The section VMA must equal the file position
2957 modulo the page size. FIXME: I'm not sure if
2958 this adjustment is really necessary. We used to
2959 not have the SEC_LOAD case just above, and then
2960 this was necessary, but now I'm not sure. */
2961 if ((abfd->flags & D_PAGED) != 0)
2962 adjust = (sec->vma - voff) % bed->maxpagesize;
2963 else
2964 adjust = (sec->vma - voff) % align;
2966 else
2967 adjust = 0;
2969 if (adjust != 0)
2971 if (i == 0)
2973 (* _bfd_error_handler)
2974 (_("Error: First section in segment (%s) starts at 0x%x"),
2975 bfd_section_name (abfd, sec), sec->lma);
2976 (* _bfd_error_handler)
2977 (_(" whereas segment starts at 0x%x"),
2978 p->p_paddr);
2980 return false;
2982 p->p_memsz += adjust;
2983 off += adjust;
2984 voff += adjust;
2985 if ((flags & SEC_LOAD) != 0)
2986 p->p_filesz += adjust;
2989 sec->filepos = off;
2991 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2992 used in a linker script we may have a section with
2993 SEC_LOAD clear but which is supposed to have
2994 contents. */
2995 if ((flags & SEC_LOAD) != 0
2996 || (flags & SEC_HAS_CONTENTS) != 0)
2997 off += sec->_raw_size;
2999 if ((flags & SEC_ALLOC) != 0)
3000 voff += sec->_raw_size;
3003 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3005 /* The actual "note" segment has i == 0.
3006 This is the one that actually contains everything. */
3007 if (i == 0)
3009 sec->filepos = off;
3010 p->p_filesz = sec->_raw_size;
3011 off += sec->_raw_size;
3012 voff = off;
3014 else
3016 /* Fake sections -- don't need to be written. */
3017 sec->filepos = 0;
3018 sec->_raw_size = 0;
3019 flags = sec->flags = 0;
3021 p->p_memsz = 0;
3022 p->p_align = 1;
3024 else
3026 p->p_memsz += sec->_raw_size;
3028 if ((flags & SEC_LOAD) != 0)
3029 p->p_filesz += sec->_raw_size;
3031 if (align > p->p_align
3032 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3033 p->p_align = align;
3036 if (! m->p_flags_valid)
3038 p->p_flags |= PF_R;
3039 if ((flags & SEC_CODE) != 0)
3040 p->p_flags |= PF_X;
3041 if ((flags & SEC_READONLY) == 0)
3042 p->p_flags |= PF_W;
3047 /* Now that we have set the section file positions, we can set up
3048 the file positions for the non PT_LOAD segments. */
3049 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3050 m != NULL;
3051 m = m->next, p++)
3053 if (p->p_type != PT_LOAD && m->count > 0)
3055 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3056 p->p_offset = m->sections[0]->filepos;
3058 if (m->count == 0)
3060 if (m->includes_filehdr)
3062 p->p_vaddr = filehdr_vaddr;
3063 if (! m->p_paddr_valid)
3064 p->p_paddr = filehdr_paddr;
3066 else if (m->includes_phdrs)
3068 p->p_vaddr = phdrs_vaddr;
3069 if (! m->p_paddr_valid)
3070 p->p_paddr = phdrs_paddr;
3075 /* Clear out any program headers we allocated but did not use. */
3076 for (; count < alloc; count++, p++)
3078 memset (p, 0, sizeof *p);
3079 p->p_type = PT_NULL;
3082 elf_tdata (abfd)->phdr = phdrs;
3084 elf_tdata (abfd)->next_file_pos = off;
3086 /* Write out the program headers. */
3087 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3088 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3089 return false;
3091 return true;
3094 /* Get the size of the program header.
3096 If this is called by the linker before any of the section VMA's are set, it
3097 can't calculate the correct value for a strange memory layout. This only
3098 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3099 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3100 data segment (exclusive of .interp and .dynamic).
3102 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3103 will be two segments. */
3105 static bfd_size_type
3106 get_program_header_size (abfd)
3107 bfd *abfd;
3109 size_t segs;
3110 asection *s;
3111 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3113 /* We can't return a different result each time we're called. */
3114 if (elf_tdata (abfd)->program_header_size != 0)
3115 return elf_tdata (abfd)->program_header_size;
3117 if (elf_tdata (abfd)->segment_map != NULL)
3119 struct elf_segment_map *m;
3121 segs = 0;
3122 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3123 ++segs;
3124 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3125 return elf_tdata (abfd)->program_header_size;
3128 /* Assume we will need exactly two PT_LOAD segments: one for text
3129 and one for data. */
3130 segs = 2;
3132 s = bfd_get_section_by_name (abfd, ".interp");
3133 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3135 /* If we have a loadable interpreter section, we need a
3136 PT_INTERP segment. In this case, assume we also need a
3137 PT_PHDR segment, although that may not be true for all
3138 targets. */
3139 segs += 2;
3142 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3144 /* We need a PT_DYNAMIC segment. */
3145 ++segs;
3148 for (s = abfd->sections; s != NULL; s = s->next)
3150 if ((s->flags & SEC_LOAD) != 0
3151 && strncmp (s->name, ".note", 5) == 0)
3153 /* We need a PT_NOTE segment. */
3154 ++segs;
3158 /* Let the backend count up any program headers it might need. */
3159 if (bed->elf_backend_additional_program_headers)
3161 int a;
3163 a = (*bed->elf_backend_additional_program_headers) (abfd);
3164 if (a == -1)
3165 abort ();
3166 segs += a;
3169 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3170 return elf_tdata (abfd)->program_header_size;
3173 /* Work out the file positions of all the sections. This is called by
3174 _bfd_elf_compute_section_file_positions. All the section sizes and
3175 VMAs must be known before this is called.
3177 We do not consider reloc sections at this point, unless they form
3178 part of the loadable image. Reloc sections are assigned file
3179 positions in assign_file_positions_for_relocs, which is called by
3180 write_object_contents and final_link.
3182 We also don't set the positions of the .symtab and .strtab here. */
3184 static boolean
3185 assign_file_positions_except_relocs (abfd)
3186 bfd *abfd;
3188 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3189 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3190 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3191 file_ptr off;
3192 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3194 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3195 && bfd_get_format (abfd) != bfd_core)
3197 Elf_Internal_Shdr **hdrpp;
3198 unsigned int i;
3200 /* Start after the ELF header. */
3201 off = i_ehdrp->e_ehsize;
3203 /* We are not creating an executable, which means that we are
3204 not creating a program header, and that the actual order of
3205 the sections in the file is unimportant. */
3206 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3208 Elf_Internal_Shdr *hdr;
3210 hdr = *hdrpp;
3211 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3213 hdr->sh_offset = -1;
3214 continue;
3216 if (i == tdata->symtab_section
3217 || i == tdata->strtab_section)
3219 hdr->sh_offset = -1;
3220 continue;
3223 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3226 else
3228 unsigned int i;
3229 Elf_Internal_Shdr **hdrpp;
3231 /* Assign file positions for the loaded sections based on the
3232 assignment of sections to segments. */
3233 if (! assign_file_positions_for_segments (abfd))
3234 return false;
3236 /* Assign file positions for the other sections. */
3238 off = elf_tdata (abfd)->next_file_pos;
3239 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3241 Elf_Internal_Shdr *hdr;
3243 hdr = *hdrpp;
3244 if (hdr->bfd_section != NULL
3245 && hdr->bfd_section->filepos != 0)
3246 hdr->sh_offset = hdr->bfd_section->filepos;
3247 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3249 ((*_bfd_error_handler)
3250 (_("%s: warning: allocated section `%s' not in segment"),
3251 bfd_get_filename (abfd),
3252 (hdr->bfd_section == NULL
3253 ? "*unknown*"
3254 : hdr->bfd_section->name)));
3255 if ((abfd->flags & D_PAGED) != 0)
3256 off += (hdr->sh_addr - off) % bed->maxpagesize;
3257 else
3258 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3259 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3260 false);
3262 else if (hdr->sh_type == SHT_REL
3263 || hdr->sh_type == SHT_RELA
3264 || hdr == i_shdrpp[tdata->symtab_section]
3265 || hdr == i_shdrpp[tdata->strtab_section])
3266 hdr->sh_offset = -1;
3267 else
3268 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3272 /* Place the section headers. */
3273 off = align_file_position (off, bed->s->file_align);
3274 i_ehdrp->e_shoff = off;
3275 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3277 elf_tdata (abfd)->next_file_pos = off;
3279 return true;
3282 static boolean
3283 prep_headers (abfd)
3284 bfd *abfd;
3286 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3287 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3288 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3289 int count;
3290 struct bfd_strtab_hash *shstrtab;
3291 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3293 i_ehdrp = elf_elfheader (abfd);
3294 i_shdrp = elf_elfsections (abfd);
3296 shstrtab = _bfd_elf_stringtab_init ();
3297 if (shstrtab == NULL)
3298 return false;
3300 elf_shstrtab (abfd) = shstrtab;
3302 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3303 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3304 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3305 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3307 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3308 i_ehdrp->e_ident[EI_DATA] =
3309 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3310 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3312 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3313 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3315 for (count = EI_PAD; count < EI_NIDENT; count++)
3316 i_ehdrp->e_ident[count] = 0;
3318 if ((abfd->flags & DYNAMIC) != 0)
3319 i_ehdrp->e_type = ET_DYN;
3320 else if ((abfd->flags & EXEC_P) != 0)
3321 i_ehdrp->e_type = ET_EXEC;
3322 else if (bfd_get_format (abfd) == bfd_core)
3323 i_ehdrp->e_type = ET_CORE;
3324 else
3325 i_ehdrp->e_type = ET_REL;
3327 switch (bfd_get_arch (abfd))
3329 case bfd_arch_unknown:
3330 i_ehdrp->e_machine = EM_NONE;
3331 break;
3332 case bfd_arch_sparc:
3333 if (bfd_get_arch_size (abfd) == 64)
3334 i_ehdrp->e_machine = EM_SPARCV9;
3335 else
3336 i_ehdrp->e_machine = EM_SPARC;
3337 break;
3338 case bfd_arch_i370:
3339 i_ehdrp->e_machine = EM_S370;
3340 break;
3341 case bfd_arch_i386:
3342 if (bfd_get_arch_size (abfd) == 64)
3343 i_ehdrp->e_machine = EM_X86_64;
3344 else
3345 i_ehdrp->e_machine = EM_386;
3346 break;
3347 case bfd_arch_ia64:
3348 i_ehdrp->e_machine = EM_IA_64;
3349 break;
3350 case bfd_arch_m68hc11:
3351 i_ehdrp->e_machine = EM_68HC11;
3352 break;
3353 case bfd_arch_m68hc12:
3354 i_ehdrp->e_machine = EM_68HC12;
3355 break;
3356 case bfd_arch_s390:
3357 i_ehdrp->e_machine = EM_S390;
3358 break;
3359 case bfd_arch_m68k:
3360 i_ehdrp->e_machine = EM_68K;
3361 break;
3362 case bfd_arch_m88k:
3363 i_ehdrp->e_machine = EM_88K;
3364 break;
3365 case bfd_arch_i860:
3366 i_ehdrp->e_machine = EM_860;
3367 break;
3368 case bfd_arch_i960:
3369 i_ehdrp->e_machine = EM_960;
3370 break;
3371 case bfd_arch_mips: /* MIPS Rxxxx */
3372 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3373 break;
3374 case bfd_arch_hppa:
3375 i_ehdrp->e_machine = EM_PARISC;
3376 break;
3377 case bfd_arch_powerpc:
3378 i_ehdrp->e_machine = EM_PPC;
3379 break;
3380 case bfd_arch_alpha:
3381 i_ehdrp->e_machine = EM_ALPHA;
3382 break;
3383 case bfd_arch_sh:
3384 i_ehdrp->e_machine = EM_SH;
3385 break;
3386 case bfd_arch_d10v:
3387 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3388 break;
3389 case bfd_arch_d30v:
3390 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3391 break;
3392 case bfd_arch_fr30:
3393 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3394 break;
3395 case bfd_arch_mcore:
3396 i_ehdrp->e_machine = EM_MCORE;
3397 break;
3398 case bfd_arch_avr:
3399 i_ehdrp->e_machine = EM_AVR;
3400 break;
3401 case bfd_arch_v850:
3402 switch (bfd_get_mach (abfd))
3404 default:
3405 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3407 break;
3408 case bfd_arch_arc:
3409 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3410 break;
3411 case bfd_arch_arm:
3412 i_ehdrp->e_machine = EM_ARM;
3413 break;
3414 case bfd_arch_m32r:
3415 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3416 break;
3417 case bfd_arch_mn10200:
3418 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3419 break;
3420 case bfd_arch_mn10300:
3421 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3422 break;
3423 case bfd_arch_pj:
3424 i_ehdrp->e_machine = EM_PJ;
3425 break;
3426 case bfd_arch_cris:
3427 i_ehdrp->e_machine = EM_CRIS;
3428 break;
3429 case bfd_arch_openrisc:
3430 i_ehdrp->e_machine = EM_OPENRISC;
3431 break;
3432 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3433 default:
3434 i_ehdrp->e_machine = EM_NONE;
3436 i_ehdrp->e_version = bed->s->ev_current;
3437 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3439 /* No program header, for now. */
3440 i_ehdrp->e_phoff = 0;
3441 i_ehdrp->e_phentsize = 0;
3442 i_ehdrp->e_phnum = 0;
3444 /* Each bfd section is section header entry. */
3445 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3446 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3448 /* If we're building an executable, we'll need a program header table. */
3449 if (abfd->flags & EXEC_P)
3451 /* It all happens later. */
3452 #if 0
3453 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3455 /* elf_build_phdrs() returns a (NULL-terminated) array of
3456 Elf_Internal_Phdrs. */
3457 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3458 i_ehdrp->e_phoff = outbase;
3459 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3460 #endif
3462 else
3464 i_ehdrp->e_phentsize = 0;
3465 i_phdrp = 0;
3466 i_ehdrp->e_phoff = 0;
3469 elf_tdata (abfd)->symtab_hdr.sh_name =
3470 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3471 elf_tdata (abfd)->strtab_hdr.sh_name =
3472 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3473 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3474 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3475 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3476 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3477 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3478 return false;
3480 return true;
3483 /* Assign file positions for all the reloc sections which are not part
3484 of the loadable file image. */
3486 void
3487 _bfd_elf_assign_file_positions_for_relocs (abfd)
3488 bfd *abfd;
3490 file_ptr off;
3491 unsigned int i;
3492 Elf_Internal_Shdr **shdrpp;
3494 off = elf_tdata (abfd)->next_file_pos;
3496 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3497 i < elf_elfheader (abfd)->e_shnum;
3498 i++, shdrpp++)
3500 Elf_Internal_Shdr *shdrp;
3502 shdrp = *shdrpp;
3503 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3504 && shdrp->sh_offset == -1)
3505 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3508 elf_tdata (abfd)->next_file_pos = off;
3511 boolean
3512 _bfd_elf_write_object_contents (abfd)
3513 bfd *abfd;
3515 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3516 Elf_Internal_Ehdr *i_ehdrp;
3517 Elf_Internal_Shdr **i_shdrp;
3518 boolean failed;
3519 unsigned int count;
3521 if (! abfd->output_has_begun
3522 && ! _bfd_elf_compute_section_file_positions
3523 (abfd, (struct bfd_link_info *) NULL))
3524 return false;
3526 i_shdrp = elf_elfsections (abfd);
3527 i_ehdrp = elf_elfheader (abfd);
3529 failed = false;
3530 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3531 if (failed)
3532 return false;
3534 _bfd_elf_assign_file_positions_for_relocs (abfd);
3536 /* After writing the headers, we need to write the sections too... */
3537 for (count = 1; count < i_ehdrp->e_shnum; count++)
3539 if (bed->elf_backend_section_processing)
3540 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3541 if (i_shdrp[count]->contents)
3543 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3544 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3545 1, abfd)
3546 != i_shdrp[count]->sh_size))
3547 return false;
3551 /* Write out the section header names. */
3552 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3553 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3554 return false;
3556 if (bed->elf_backend_final_write_processing)
3557 (*bed->elf_backend_final_write_processing) (abfd,
3558 elf_tdata (abfd)->linker);
3560 return bed->s->write_shdrs_and_ehdr (abfd);
3563 boolean
3564 _bfd_elf_write_corefile_contents (abfd)
3565 bfd *abfd;
3567 /* Hopefully this can be done just like an object file. */
3568 return _bfd_elf_write_object_contents (abfd);
3571 /* Given a section, search the header to find them. */
3574 _bfd_elf_section_from_bfd_section (abfd, asect)
3575 bfd *abfd;
3576 struct sec *asect;
3578 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3579 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3580 int index;
3581 Elf_Internal_Shdr *hdr;
3582 int maxindex = elf_elfheader (abfd)->e_shnum;
3584 for (index = 0; index < maxindex; index++)
3586 hdr = i_shdrp[index];
3587 if (hdr->bfd_section == asect)
3588 return index;
3591 if (bed->elf_backend_section_from_bfd_section)
3593 for (index = 0; index < maxindex; index++)
3595 int retval;
3597 hdr = i_shdrp[index];
3598 retval = index;
3599 if ((*bed->elf_backend_section_from_bfd_section)
3600 (abfd, hdr, asect, &retval))
3601 return retval;
3605 if (bfd_is_abs_section (asect))
3606 return SHN_ABS;
3607 if (bfd_is_com_section (asect))
3608 return SHN_COMMON;
3609 if (bfd_is_und_section (asect))
3610 return SHN_UNDEF;
3612 bfd_set_error (bfd_error_nonrepresentable_section);
3614 return -1;
3617 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3618 on error. */
3621 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3622 bfd *abfd;
3623 asymbol **asym_ptr_ptr;
3625 asymbol *asym_ptr = *asym_ptr_ptr;
3626 int idx;
3627 flagword flags = asym_ptr->flags;
3629 /* When gas creates relocations against local labels, it creates its
3630 own symbol for the section, but does put the symbol into the
3631 symbol chain, so udata is 0. When the linker is generating
3632 relocatable output, this section symbol may be for one of the
3633 input sections rather than the output section. */
3634 if (asym_ptr->udata.i == 0
3635 && (flags & BSF_SECTION_SYM)
3636 && asym_ptr->section)
3638 int indx;
3640 if (asym_ptr->section->output_section != NULL)
3641 indx = asym_ptr->section->output_section->index;
3642 else
3643 indx = asym_ptr->section->index;
3644 if (elf_section_syms (abfd)[indx])
3645 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3648 idx = asym_ptr->udata.i;
3650 if (idx == 0)
3652 /* This case can occur when using --strip-symbol on a symbol
3653 which is used in a relocation entry. */
3654 (*_bfd_error_handler)
3655 (_("%s: symbol `%s' required but not present"),
3656 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3657 bfd_set_error (bfd_error_no_symbols);
3658 return -1;
3661 #if DEBUG & 4
3663 fprintf (stderr,
3664 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3665 (long) asym_ptr, asym_ptr->name, idx, flags,
3666 elf_symbol_flags (flags));
3667 fflush (stderr);
3669 #endif
3671 return idx;
3674 /* Copy private BFD data. This copies any program header information. */
3676 static boolean
3677 copy_private_bfd_data (ibfd, obfd)
3678 bfd *ibfd;
3679 bfd *obfd;
3681 Elf_Internal_Ehdr * iehdr;
3682 struct elf_segment_map * map;
3683 struct elf_segment_map * map_first;
3684 struct elf_segment_map ** pointer_to_map;
3685 Elf_Internal_Phdr * segment;
3686 asection * section;
3687 unsigned int i;
3688 unsigned int num_segments;
3689 boolean phdr_included = false;
3690 bfd_vma maxpagesize;
3691 struct elf_segment_map * phdr_adjust_seg = NULL;
3692 unsigned int phdr_adjust_num = 0;
3694 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3695 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3696 return true;
3698 if (elf_tdata (ibfd)->phdr == NULL)
3699 return true;
3701 iehdr = elf_elfheader (ibfd);
3703 map_first = NULL;
3704 pointer_to_map = &map_first;
3706 num_segments = elf_elfheader (ibfd)->e_phnum;
3707 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3709 /* Returns the end address of the segment + 1. */
3710 #define SEGMENT_END(segment, start) \
3711 (start + (segment->p_memsz > segment->p_filesz \
3712 ? segment->p_memsz : segment->p_filesz))
3714 /* Returns true if the given section is contained within
3715 the given segment. VMA addresses are compared. */
3716 #define IS_CONTAINED_BY_VMA(section, segment) \
3717 (section->vma >= segment->p_vaddr \
3718 && (section->vma + section->_raw_size) \
3719 <= (SEGMENT_END (segment, segment->p_vaddr)))
3721 /* Returns true if the given section is contained within
3722 the given segment. LMA addresses are compared. */
3723 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3724 (section->lma >= base \
3725 && (section->lma + section->_raw_size) \
3726 <= SEGMENT_END (segment, base))
3728 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3729 #define IS_COREFILE_NOTE(p, s) \
3730 (p->p_type == PT_NOTE \
3731 && bfd_get_format (ibfd) == bfd_core \
3732 && s->vma == 0 && s->lma == 0 \
3733 && (bfd_vma) s->filepos >= p->p_offset \
3734 && (bfd_vma) s->filepos + s->_raw_size \
3735 <= p->p_offset + p->p_filesz)
3737 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3738 linker, which generates a PT_INTERP section with p_vaddr and
3739 p_memsz set to 0. */
3740 #define IS_SOLARIS_PT_INTERP(p, s) \
3741 ( p->p_vaddr == 0 \
3742 && p->p_filesz > 0 \
3743 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3744 && s->_raw_size > 0 \
3745 && (bfd_vma) s->filepos >= p->p_offset \
3746 && ((bfd_vma) s->filepos + s->_raw_size \
3747 <= p->p_offset + p->p_filesz))
3749 /* Decide if the given section should be included in the given segment.
3750 A section will be included if:
3751 1. It is within the address space of the segment,
3752 2. It is an allocated segment,
3753 3. There is an output section associated with it,
3754 4. The section has not already been allocated to a previous segment. */
3755 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3756 ((((IS_CONTAINED_BY_VMA (section, segment) \
3757 || IS_SOLARIS_PT_INTERP (segment, section)) \
3758 && (section->flags & SEC_ALLOC) != 0) \
3759 || IS_COREFILE_NOTE (segment, section)) \
3760 && section->output_section != NULL \
3761 && section->segment_mark == false)
3763 /* Returns true iff seg1 starts after the end of seg2. */
3764 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3765 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3767 /* Returns true iff seg1 and seg2 overlap. */
3768 #define SEGMENT_OVERLAPS(seg1, seg2) \
3769 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3771 /* Initialise the segment mark field. */
3772 for (section = ibfd->sections; section != NULL; section = section->next)
3773 section->segment_mark = false;
3775 /* Scan through the segments specified in the program header
3776 of the input BFD. For this first scan we look for overlaps
3777 in the loadable segments. These can be created by wierd
3778 parameters to objcopy. */
3779 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3780 i < num_segments;
3781 i++, segment++)
3783 unsigned int j;
3784 Elf_Internal_Phdr *segment2;
3786 if (segment->p_type != PT_LOAD)
3787 continue;
3789 /* Determine if this segment overlaps any previous segments. */
3790 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3792 bfd_signed_vma extra_length;
3794 if (segment2->p_type != PT_LOAD
3795 || ! SEGMENT_OVERLAPS (segment, segment2))
3796 continue;
3798 /* Merge the two segments together. */
3799 if (segment2->p_vaddr < segment->p_vaddr)
3801 /* Extend SEGMENT2 to include SEGMENT and then delete
3802 SEGMENT. */
3803 extra_length =
3804 SEGMENT_END (segment, segment->p_vaddr)
3805 - SEGMENT_END (segment2, segment2->p_vaddr);
3807 if (extra_length > 0)
3809 segment2->p_memsz += extra_length;
3810 segment2->p_filesz += extra_length;
3813 segment->p_type = PT_NULL;
3815 /* Since we have deleted P we must restart the outer loop. */
3816 i = 0;
3817 segment = elf_tdata (ibfd)->phdr;
3818 break;
3820 else
3822 /* Extend SEGMENT to include SEGMENT2 and then delete
3823 SEGMENT2. */
3824 extra_length =
3825 SEGMENT_END (segment2, segment2->p_vaddr)
3826 - SEGMENT_END (segment, segment->p_vaddr);
3828 if (extra_length > 0)
3830 segment->p_memsz += extra_length;
3831 segment->p_filesz += extra_length;
3834 segment2->p_type = PT_NULL;
3839 /* The second scan attempts to assign sections to segments. */
3840 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3841 i < num_segments;
3842 i ++, segment ++)
3844 unsigned int section_count;
3845 asection ** sections;
3846 asection * output_section;
3847 unsigned int isec;
3848 bfd_vma matching_lma;
3849 bfd_vma suggested_lma;
3850 unsigned int j;
3852 if (segment->p_type == PT_NULL)
3853 continue;
3855 /* Compute how many sections might be placed into this segment. */
3856 section_count = 0;
3857 for (section = ibfd->sections; section != NULL; section = section->next)
3858 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3859 ++section_count;
3861 /* Allocate a segment map big enough to contain all of the
3862 sections we have selected. */
3863 map = ((struct elf_segment_map *)
3864 bfd_alloc (obfd,
3865 (sizeof (struct elf_segment_map)
3866 + ((size_t) section_count - 1) * sizeof (asection *))));
3867 if (map == NULL)
3868 return false;
3870 /* Initialise the fields of the segment map. Default to
3871 using the physical address of the segment in the input BFD. */
3872 map->next = NULL;
3873 map->p_type = segment->p_type;
3874 map->p_flags = segment->p_flags;
3875 map->p_flags_valid = 1;
3876 map->p_paddr = segment->p_paddr;
3877 map->p_paddr_valid = 1;
3879 /* Determine if this segment contains the ELF file header
3880 and if it contains the program headers themselves. */
3881 map->includes_filehdr = (segment->p_offset == 0
3882 && segment->p_filesz >= iehdr->e_ehsize);
3884 map->includes_phdrs = 0;
3886 if (! phdr_included || segment->p_type != PT_LOAD)
3888 map->includes_phdrs =
3889 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3890 && (segment->p_offset + segment->p_filesz
3891 >= ((bfd_vma) iehdr->e_phoff
3892 + iehdr->e_phnum * iehdr->e_phentsize)));
3894 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3895 phdr_included = true;
3898 if (section_count == 0)
3900 /* Special segments, such as the PT_PHDR segment, may contain
3901 no sections, but ordinary, loadable segments should contain
3902 something. */
3903 if (segment->p_type == PT_LOAD)
3904 _bfd_error_handler
3905 (_("%s: warning: Empty loadable segment detected\n"),
3906 bfd_get_filename (ibfd));
3908 map->count = 0;
3909 *pointer_to_map = map;
3910 pointer_to_map = &map->next;
3912 continue;
3915 /* Now scan the sections in the input BFD again and attempt
3916 to add their corresponding output sections to the segment map.
3917 The problem here is how to handle an output section which has
3918 been moved (ie had its LMA changed). There are four possibilities:
3920 1. None of the sections have been moved.
3921 In this case we can continue to use the segment LMA from the
3922 input BFD.
3924 2. All of the sections have been moved by the same amount.
3925 In this case we can change the segment's LMA to match the LMA
3926 of the first section.
3928 3. Some of the sections have been moved, others have not.
3929 In this case those sections which have not been moved can be
3930 placed in the current segment which will have to have its size,
3931 and possibly its LMA changed, and a new segment or segments will
3932 have to be created to contain the other sections.
3934 4. The sections have been moved, but not be the same amount.
3935 In this case we can change the segment's LMA to match the LMA
3936 of the first section and we will have to create a new segment
3937 or segments to contain the other sections.
3939 In order to save time, we allocate an array to hold the section
3940 pointers that we are interested in. As these sections get assigned
3941 to a segment, they are removed from this array. */
3943 sections = (asection **) bfd_malloc
3944 (sizeof (asection *) * section_count);
3945 if (sections == NULL)
3946 return false;
3948 /* Step One: Scan for segment vs section LMA conflicts.
3949 Also add the sections to the section array allocated above.
3950 Also add the sections to the current segment. In the common
3951 case, where the sections have not been moved, this means that
3952 we have completely filled the segment, and there is nothing
3953 more to do. */
3954 isec = 0;
3955 matching_lma = 0;
3956 suggested_lma = 0;
3958 for (j = 0, section = ibfd->sections;
3959 section != NULL;
3960 section = section->next)
3962 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3964 output_section = section->output_section;
3966 sections[j ++] = section;
3968 /* The Solaris native linker always sets p_paddr to 0.
3969 We try to catch that case here, and set it to the
3970 correct value. */
3971 if (segment->p_paddr == 0
3972 && segment->p_vaddr != 0
3973 && isec == 0
3974 && output_section->lma != 0
3975 && (output_section->vma == (segment->p_vaddr
3976 + (map->includes_filehdr
3977 ? iehdr->e_ehsize
3978 : 0)
3979 + (map->includes_phdrs
3980 ? iehdr->e_phnum * iehdr->e_phentsize
3981 : 0))))
3982 map->p_paddr = segment->p_vaddr;
3984 /* Match up the physical address of the segment with the
3985 LMA address of the output section. */
3986 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3987 || IS_COREFILE_NOTE (segment, section))
3989 if (matching_lma == 0)
3990 matching_lma = output_section->lma;
3992 /* We assume that if the section fits within the segment
3993 then it does not overlap any other section within that
3994 segment. */
3995 map->sections[isec ++] = output_section;
3997 else if (suggested_lma == 0)
3998 suggested_lma = output_section->lma;
4002 BFD_ASSERT (j == section_count);
4004 /* Step Two: Adjust the physical address of the current segment,
4005 if necessary. */
4006 if (isec == section_count)
4008 /* All of the sections fitted within the segment as currently
4009 specified. This is the default case. Add the segment to
4010 the list of built segments and carry on to process the next
4011 program header in the input BFD. */
4012 map->count = section_count;
4013 *pointer_to_map = map;
4014 pointer_to_map = &map->next;
4016 free (sections);
4017 continue;
4019 else
4021 if (matching_lma != 0)
4023 /* At least one section fits inside the current segment.
4024 Keep it, but modify its physical address to match the
4025 LMA of the first section that fitted. */
4026 map->p_paddr = matching_lma;
4028 else
4030 /* None of the sections fitted inside the current segment.
4031 Change the current segment's physical address to match
4032 the LMA of the first section. */
4033 map->p_paddr = suggested_lma;
4036 /* Offset the segment physical address from the lma
4037 to allow for space taken up by elf headers. */
4038 if (map->includes_filehdr)
4039 map->p_paddr -= iehdr->e_ehsize;
4041 if (map->includes_phdrs)
4043 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4045 /* iehdr->e_phnum is just an estimate of the number
4046 of program headers that we will need. Make a note
4047 here of the number we used and the segment we chose
4048 to hold these headers, so that we can adjust the
4049 offset when we know the correct value. */
4050 phdr_adjust_num = iehdr->e_phnum;
4051 phdr_adjust_seg = map;
4055 /* Step Three: Loop over the sections again, this time assigning
4056 those that fit to the current segment and remvoing them from the
4057 sections array; but making sure not to leave large gaps. Once all
4058 possible sections have been assigned to the current segment it is
4059 added to the list of built segments and if sections still remain
4060 to be assigned, a new segment is constructed before repeating
4061 the loop. */
4062 isec = 0;
4065 map->count = 0;
4066 suggested_lma = 0;
4068 /* Fill the current segment with sections that fit. */
4069 for (j = 0; j < section_count; j++)
4071 section = sections[j];
4073 if (section == NULL)
4074 continue;
4076 output_section = section->output_section;
4078 BFD_ASSERT (output_section != NULL);
4080 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4081 || IS_COREFILE_NOTE (segment, section))
4083 if (map->count == 0)
4085 /* If the first section in a segment does not start at
4086 the beginning of the segment, then something is
4087 wrong. */
4088 if (output_section->lma !=
4089 (map->p_paddr
4090 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4091 + (map->includes_phdrs
4092 ? iehdr->e_phnum * iehdr->e_phentsize
4093 : 0)))
4094 abort ();
4096 else
4098 asection * prev_sec;
4100 prev_sec = map->sections[map->count - 1];
4102 /* If the gap between the end of the previous section
4103 and the start of this section is more than
4104 maxpagesize then we need to start a new segment. */
4105 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4106 < BFD_ALIGN (output_section->lma, maxpagesize))
4107 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4109 if (suggested_lma == 0)
4110 suggested_lma = output_section->lma;
4112 continue;
4116 map->sections[map->count++] = output_section;
4117 ++isec;
4118 sections[j] = NULL;
4119 section->segment_mark = true;
4121 else if (suggested_lma == 0)
4122 suggested_lma = output_section->lma;
4125 BFD_ASSERT (map->count > 0);
4127 /* Add the current segment to the list of built segments. */
4128 *pointer_to_map = map;
4129 pointer_to_map = &map->next;
4131 if (isec < section_count)
4133 /* We still have not allocated all of the sections to
4134 segments. Create a new segment here, initialise it
4135 and carry on looping. */
4136 map = ((struct elf_segment_map *)
4137 bfd_alloc (obfd,
4138 (sizeof (struct elf_segment_map)
4139 + ((size_t) section_count - 1)
4140 * sizeof (asection *))));
4141 if (map == NULL)
4142 return false;
4144 /* Initialise the fields of the segment map. Set the physical
4145 physical address to the LMA of the first section that has
4146 not yet been assigned. */
4147 map->next = NULL;
4148 map->p_type = segment->p_type;
4149 map->p_flags = segment->p_flags;
4150 map->p_flags_valid = 1;
4151 map->p_paddr = suggested_lma;
4152 map->p_paddr_valid = 1;
4153 map->includes_filehdr = 0;
4154 map->includes_phdrs = 0;
4157 while (isec < section_count);
4159 free (sections);
4162 /* The Solaris linker creates program headers in which all the
4163 p_paddr fields are zero. When we try to objcopy or strip such a
4164 file, we get confused. Check for this case, and if we find it
4165 reset the p_paddr_valid fields. */
4166 for (map = map_first; map != NULL; map = map->next)
4167 if (map->p_paddr != 0)
4168 break;
4169 if (map == NULL)
4171 for (map = map_first; map != NULL; map = map->next)
4172 map->p_paddr_valid = 0;
4175 elf_tdata (obfd)->segment_map = map_first;
4177 /* If we had to estimate the number of program headers that were
4178 going to be needed, then check our estimate know and adjust
4179 the offset if necessary. */
4180 if (phdr_adjust_seg != NULL)
4182 unsigned int count;
4184 for (count = 0, map = map_first; map != NULL; map = map->next)
4185 count++;
4187 if (count > phdr_adjust_num)
4188 phdr_adjust_seg->p_paddr
4189 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4192 #if 0
4193 /* Final Step: Sort the segments into ascending order of physical
4194 address. */
4195 if (map_first != NULL)
4197 struct elf_segment_map *prev;
4199 prev = map_first;
4200 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4202 /* Yes I know - its a bubble sort.... */
4203 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4205 /* Swap map and map->next. */
4206 prev->next = map->next;
4207 map->next = map->next->next;
4208 prev->next->next = map;
4210 /* Restart loop. */
4211 map = map_first;
4215 #endif
4217 #undef SEGMENT_END
4218 #undef IS_CONTAINED_BY_VMA
4219 #undef IS_CONTAINED_BY_LMA
4220 #undef IS_COREFILE_NOTE
4221 #undef IS_SOLARIS_PT_INTERP
4222 #undef INCLUDE_SECTION_IN_SEGMENT
4223 #undef SEGMENT_AFTER_SEGMENT
4224 #undef SEGMENT_OVERLAPS
4225 return true;
4228 /* Copy private section information. This copies over the entsize
4229 field, and sometimes the info field. */
4231 boolean
4232 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4233 bfd *ibfd;
4234 asection *isec;
4235 bfd *obfd;
4236 asection *osec;
4238 Elf_Internal_Shdr *ihdr, *ohdr;
4240 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4241 || obfd->xvec->flavour != bfd_target_elf_flavour)
4242 return true;
4244 /* Copy over private BFD data if it has not already been copied.
4245 This must be done here, rather than in the copy_private_bfd_data
4246 entry point, because the latter is called after the section
4247 contents have been set, which means that the program headers have
4248 already been worked out. */
4249 if (elf_tdata (obfd)->segment_map == NULL
4250 && elf_tdata (ibfd)->phdr != NULL)
4252 asection *s;
4254 /* Only set up the segments if there are no more SEC_ALLOC
4255 sections. FIXME: This won't do the right thing if objcopy is
4256 used to remove the last SEC_ALLOC section, since objcopy
4257 won't call this routine in that case. */
4258 for (s = isec->next; s != NULL; s = s->next)
4259 if ((s->flags & SEC_ALLOC) != 0)
4260 break;
4261 if (s == NULL)
4263 if (! copy_private_bfd_data (ibfd, obfd))
4264 return false;
4268 ihdr = &elf_section_data (isec)->this_hdr;
4269 ohdr = &elf_section_data (osec)->this_hdr;
4271 ohdr->sh_entsize = ihdr->sh_entsize;
4273 if (ihdr->sh_type == SHT_SYMTAB
4274 || ihdr->sh_type == SHT_DYNSYM
4275 || ihdr->sh_type == SHT_GNU_verneed
4276 || ihdr->sh_type == SHT_GNU_verdef)
4277 ohdr->sh_info = ihdr->sh_info;
4279 elf_section_data (osec)->use_rela_p
4280 = elf_section_data (isec)->use_rela_p;
4282 return true;
4285 /* Copy private symbol information. If this symbol is in a section
4286 which we did not map into a BFD section, try to map the section
4287 index correctly. We use special macro definitions for the mapped
4288 section indices; these definitions are interpreted by the
4289 swap_out_syms function. */
4291 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4292 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4293 #define MAP_STRTAB (SHN_LORESERVE - 3)
4294 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4296 boolean
4297 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4298 bfd *ibfd;
4299 asymbol *isymarg;
4300 bfd *obfd;
4301 asymbol *osymarg;
4303 elf_symbol_type *isym, *osym;
4305 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4306 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4307 return true;
4309 isym = elf_symbol_from (ibfd, isymarg);
4310 osym = elf_symbol_from (obfd, osymarg);
4312 if (isym != NULL
4313 && osym != NULL
4314 && bfd_is_abs_section (isym->symbol.section))
4316 unsigned int shndx;
4318 shndx = isym->internal_elf_sym.st_shndx;
4319 if (shndx == elf_onesymtab (ibfd))
4320 shndx = MAP_ONESYMTAB;
4321 else if (shndx == elf_dynsymtab (ibfd))
4322 shndx = MAP_DYNSYMTAB;
4323 else if (shndx == elf_tdata (ibfd)->strtab_section)
4324 shndx = MAP_STRTAB;
4325 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4326 shndx = MAP_SHSTRTAB;
4327 osym->internal_elf_sym.st_shndx = shndx;
4330 return true;
4333 /* Swap out the symbols. */
4335 static boolean
4336 swap_out_syms (abfd, sttp, relocatable_p)
4337 bfd *abfd;
4338 struct bfd_strtab_hash **sttp;
4339 int relocatable_p;
4341 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4343 if (!elf_map_symbols (abfd))
4344 return false;
4346 /* Dump out the symtabs. */
4348 int symcount = bfd_get_symcount (abfd);
4349 asymbol **syms = bfd_get_outsymbols (abfd);
4350 struct bfd_strtab_hash *stt;
4351 Elf_Internal_Shdr *symtab_hdr;
4352 Elf_Internal_Shdr *symstrtab_hdr;
4353 char *outbound_syms;
4354 int idx;
4356 stt = _bfd_elf_stringtab_init ();
4357 if (stt == NULL)
4358 return false;
4360 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4361 symtab_hdr->sh_type = SHT_SYMTAB;
4362 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4363 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4364 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4365 symtab_hdr->sh_addralign = bed->s->file_align;
4367 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4368 symstrtab_hdr->sh_type = SHT_STRTAB;
4370 outbound_syms = bfd_alloc (abfd,
4371 (1 + symcount) * bed->s->sizeof_sym);
4372 if (outbound_syms == NULL)
4373 return false;
4374 symtab_hdr->contents = (PTR) outbound_syms;
4376 /* now generate the data (for "contents") */
4378 /* Fill in zeroth symbol and swap it out. */
4379 Elf_Internal_Sym sym;
4380 sym.st_name = 0;
4381 sym.st_value = 0;
4382 sym.st_size = 0;
4383 sym.st_info = 0;
4384 sym.st_other = 0;
4385 sym.st_shndx = SHN_UNDEF;
4386 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4387 outbound_syms += bed->s->sizeof_sym;
4389 for (idx = 0; idx < symcount; idx++)
4391 Elf_Internal_Sym sym;
4392 bfd_vma value = syms[idx]->value;
4393 elf_symbol_type *type_ptr;
4394 flagword flags = syms[idx]->flags;
4395 int type;
4397 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4399 /* Local section symbols have no name. */
4400 sym.st_name = 0;
4402 else
4404 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4405 syms[idx]->name,
4406 true, false);
4407 if (sym.st_name == (unsigned long) -1)
4408 return false;
4411 type_ptr = elf_symbol_from (abfd, syms[idx]);
4413 if ((flags & BSF_SECTION_SYM) == 0
4414 && bfd_is_com_section (syms[idx]->section))
4416 /* ELF common symbols put the alignment into the `value' field,
4417 and the size into the `size' field. This is backwards from
4418 how BFD handles it, so reverse it here. */
4419 sym.st_size = value;
4420 if (type_ptr == NULL
4421 || type_ptr->internal_elf_sym.st_value == 0)
4422 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4423 else
4424 sym.st_value = type_ptr->internal_elf_sym.st_value;
4425 sym.st_shndx = _bfd_elf_section_from_bfd_section
4426 (abfd, syms[idx]->section);
4428 else
4430 asection *sec = syms[idx]->section;
4431 int shndx;
4433 if (sec->output_section)
4435 value += sec->output_offset;
4436 sec = sec->output_section;
4438 /* Don't add in the section vma for relocatable output. */
4439 if (! relocatable_p)
4440 value += sec->vma;
4441 sym.st_value = value;
4442 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4444 if (bfd_is_abs_section (sec)
4445 && type_ptr != NULL
4446 && type_ptr->internal_elf_sym.st_shndx != 0)
4448 /* This symbol is in a real ELF section which we did
4449 not create as a BFD section. Undo the mapping done
4450 by copy_private_symbol_data. */
4451 shndx = type_ptr->internal_elf_sym.st_shndx;
4452 switch (shndx)
4454 case MAP_ONESYMTAB:
4455 shndx = elf_onesymtab (abfd);
4456 break;
4457 case MAP_DYNSYMTAB:
4458 shndx = elf_dynsymtab (abfd);
4459 break;
4460 case MAP_STRTAB:
4461 shndx = elf_tdata (abfd)->strtab_section;
4462 break;
4463 case MAP_SHSTRTAB:
4464 shndx = elf_tdata (abfd)->shstrtab_section;
4465 break;
4466 default:
4467 break;
4470 else
4472 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4474 if (shndx == -1)
4476 asection *sec2;
4478 /* Writing this would be a hell of a lot easier if
4479 we had some decent documentation on bfd, and
4480 knew what to expect of the library, and what to
4481 demand of applications. For example, it
4482 appears that `objcopy' might not set the
4483 section of a symbol to be a section that is
4484 actually in the output file. */
4485 sec2 = bfd_get_section_by_name (abfd, sec->name);
4486 BFD_ASSERT (sec2 != 0);
4487 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4488 BFD_ASSERT (shndx != -1);
4492 sym.st_shndx = shndx;
4495 if ((flags & BSF_FUNCTION) != 0)
4496 type = STT_FUNC;
4497 else if ((flags & BSF_OBJECT) != 0)
4498 type = STT_OBJECT;
4499 else
4500 type = STT_NOTYPE;
4502 /* Processor-specific types */
4503 if (type_ptr != NULL
4504 && bed->elf_backend_get_symbol_type)
4505 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4507 if (flags & BSF_SECTION_SYM)
4509 if (flags & BSF_GLOBAL)
4510 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
4511 else
4512 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4514 else if (bfd_is_com_section (syms[idx]->section))
4515 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4516 else if (bfd_is_und_section (syms[idx]->section))
4517 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4518 ? STB_WEAK
4519 : STB_GLOBAL),
4520 type);
4521 else if (flags & BSF_FILE)
4522 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4523 else
4525 int bind = STB_LOCAL;
4527 if (flags & BSF_LOCAL)
4528 bind = STB_LOCAL;
4529 else if (flags & BSF_WEAK)
4530 bind = STB_WEAK;
4531 else if (flags & BSF_GLOBAL)
4532 bind = STB_GLOBAL;
4534 sym.st_info = ELF_ST_INFO (bind, type);
4537 if (type_ptr != NULL)
4538 sym.st_other = type_ptr->internal_elf_sym.st_other;
4539 else
4540 sym.st_other = 0;
4542 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4543 outbound_syms += bed->s->sizeof_sym;
4546 *sttp = stt;
4547 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4548 symstrtab_hdr->sh_type = SHT_STRTAB;
4550 symstrtab_hdr->sh_flags = 0;
4551 symstrtab_hdr->sh_addr = 0;
4552 symstrtab_hdr->sh_entsize = 0;
4553 symstrtab_hdr->sh_link = 0;
4554 symstrtab_hdr->sh_info = 0;
4555 symstrtab_hdr->sh_addralign = 1;
4558 return true;
4561 /* Return the number of bytes required to hold the symtab vector.
4563 Note that we base it on the count plus 1, since we will null terminate
4564 the vector allocated based on this size. However, the ELF symbol table
4565 always has a dummy entry as symbol #0, so it ends up even. */
4567 long
4568 _bfd_elf_get_symtab_upper_bound (abfd)
4569 bfd *abfd;
4571 long symcount;
4572 long symtab_size;
4573 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4575 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4576 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4578 return symtab_size;
4581 long
4582 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4583 bfd *abfd;
4585 long symcount;
4586 long symtab_size;
4587 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4589 if (elf_dynsymtab (abfd) == 0)
4591 bfd_set_error (bfd_error_invalid_operation);
4592 return -1;
4595 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4596 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4598 return symtab_size;
4601 long
4602 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4603 bfd *abfd ATTRIBUTE_UNUSED;
4604 sec_ptr asect;
4606 return (asect->reloc_count + 1) * sizeof (arelent *);
4609 /* Canonicalize the relocs. */
4611 long
4612 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4613 bfd *abfd;
4614 sec_ptr section;
4615 arelent **relptr;
4616 asymbol **symbols;
4618 arelent *tblptr;
4619 unsigned int i;
4621 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4622 section,
4623 symbols,
4624 false))
4625 return -1;
4627 tblptr = section->relocation;
4628 for (i = 0; i < section->reloc_count; i++)
4629 *relptr++ = tblptr++;
4631 *relptr = NULL;
4633 return section->reloc_count;
4636 long
4637 _bfd_elf_get_symtab (abfd, alocation)
4638 bfd *abfd;
4639 asymbol **alocation;
4641 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4642 (abfd, alocation, false);
4644 if (symcount >= 0)
4645 bfd_get_symcount (abfd) = symcount;
4646 return symcount;
4649 long
4650 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4651 bfd *abfd;
4652 asymbol **alocation;
4654 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4655 (abfd, alocation, true);
4658 /* Return the size required for the dynamic reloc entries. Any
4659 section that was actually installed in the BFD, and has type
4660 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4661 considered to be a dynamic reloc section. */
4663 long
4664 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4665 bfd *abfd;
4667 long ret;
4668 asection *s;
4670 if (elf_dynsymtab (abfd) == 0)
4672 bfd_set_error (bfd_error_invalid_operation);
4673 return -1;
4676 ret = sizeof (arelent *);
4677 for (s = abfd->sections; s != NULL; s = s->next)
4678 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4679 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4680 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4681 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4682 * sizeof (arelent *));
4684 return ret;
4687 /* Canonicalize the dynamic relocation entries. Note that we return
4688 the dynamic relocations as a single block, although they are
4689 actually associated with particular sections; the interface, which
4690 was designed for SunOS style shared libraries, expects that there
4691 is only one set of dynamic relocs. Any section that was actually
4692 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4693 the dynamic symbol table, is considered to be a dynamic reloc
4694 section. */
4696 long
4697 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4698 bfd *abfd;
4699 arelent **storage;
4700 asymbol **syms;
4702 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4703 asection *s;
4704 long ret;
4706 if (elf_dynsymtab (abfd) == 0)
4708 bfd_set_error (bfd_error_invalid_operation);
4709 return -1;
4712 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4713 ret = 0;
4714 for (s = abfd->sections; s != NULL; s = s->next)
4716 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4717 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4718 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4720 arelent *p;
4721 long count, i;
4723 if (! (*slurp_relocs) (abfd, s, syms, true))
4724 return -1;
4725 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4726 p = s->relocation;
4727 for (i = 0; i < count; i++)
4728 *storage++ = p++;
4729 ret += count;
4733 *storage = NULL;
4735 return ret;
4738 /* Read in the version information. */
4740 boolean
4741 _bfd_elf_slurp_version_tables (abfd)
4742 bfd *abfd;
4744 bfd_byte *contents = NULL;
4746 if (elf_dynverdef (abfd) != 0)
4748 Elf_Internal_Shdr *hdr;
4749 Elf_External_Verdef *everdef;
4750 Elf_Internal_Verdef *iverdef;
4751 Elf_Internal_Verdef *iverdefarr;
4752 Elf_Internal_Verdef iverdefmem;
4753 unsigned int i;
4754 unsigned int maxidx;
4756 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4758 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4759 if (contents == NULL)
4760 goto error_return;
4761 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4762 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4763 goto error_return;
4765 /* We know the number of entries in the section but not the maximum
4766 index. Therefore we have to run through all entries and find
4767 the maximum. */
4768 everdef = (Elf_External_Verdef *) contents;
4769 maxidx = 0;
4770 for (i = 0; i < hdr->sh_info; ++i)
4772 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4774 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
4775 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
4777 everdef = ((Elf_External_Verdef *)
4778 ((bfd_byte *) everdef + iverdefmem.vd_next));
4781 elf_tdata (abfd)->verdef =
4782 ((Elf_Internal_Verdef *)
4783 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4784 if (elf_tdata (abfd)->verdef == NULL)
4785 goto error_return;
4787 elf_tdata (abfd)->cverdefs = maxidx;
4789 everdef = (Elf_External_Verdef *) contents;
4790 iverdefarr = elf_tdata (abfd)->verdef;
4791 for (i = 0; i < hdr->sh_info; i++)
4793 Elf_External_Verdaux *everdaux;
4794 Elf_Internal_Verdaux *iverdaux;
4795 unsigned int j;
4797 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4799 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4800 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4802 iverdef->vd_bfd = abfd;
4804 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4805 bfd_alloc (abfd,
4806 (iverdef->vd_cnt
4807 * sizeof (Elf_Internal_Verdaux))));
4808 if (iverdef->vd_auxptr == NULL)
4809 goto error_return;
4811 everdaux = ((Elf_External_Verdaux *)
4812 ((bfd_byte *) everdef + iverdef->vd_aux));
4813 iverdaux = iverdef->vd_auxptr;
4814 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4816 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4818 iverdaux->vda_nodename =
4819 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4820 iverdaux->vda_name);
4821 if (iverdaux->vda_nodename == NULL)
4822 goto error_return;
4824 if (j + 1 < iverdef->vd_cnt)
4825 iverdaux->vda_nextptr = iverdaux + 1;
4826 else
4827 iverdaux->vda_nextptr = NULL;
4829 everdaux = ((Elf_External_Verdaux *)
4830 ((bfd_byte *) everdaux + iverdaux->vda_next));
4833 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4835 if (i + 1 < hdr->sh_info)
4836 iverdef->vd_nextdef = iverdef + 1;
4837 else
4838 iverdef->vd_nextdef = NULL;
4840 everdef = ((Elf_External_Verdef *)
4841 ((bfd_byte *) everdef + iverdef->vd_next));
4844 free (contents);
4845 contents = NULL;
4848 if (elf_dynverref (abfd) != 0)
4850 Elf_Internal_Shdr *hdr;
4851 Elf_External_Verneed *everneed;
4852 Elf_Internal_Verneed *iverneed;
4853 unsigned int i;
4855 hdr = &elf_tdata (abfd)->dynverref_hdr;
4857 elf_tdata (abfd)->verref =
4858 ((Elf_Internal_Verneed *)
4859 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4860 if (elf_tdata (abfd)->verref == NULL)
4861 goto error_return;
4863 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4865 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4866 if (contents == NULL)
4867 goto error_return;
4868 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4869 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4870 goto error_return;
4872 everneed = (Elf_External_Verneed *) contents;
4873 iverneed = elf_tdata (abfd)->verref;
4874 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4876 Elf_External_Vernaux *evernaux;
4877 Elf_Internal_Vernaux *ivernaux;
4878 unsigned int j;
4880 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4882 iverneed->vn_bfd = abfd;
4884 iverneed->vn_filename =
4885 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4886 iverneed->vn_file);
4887 if (iverneed->vn_filename == NULL)
4888 goto error_return;
4890 iverneed->vn_auxptr =
4891 ((Elf_Internal_Vernaux *)
4892 bfd_alloc (abfd,
4893 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4895 evernaux = ((Elf_External_Vernaux *)
4896 ((bfd_byte *) everneed + iverneed->vn_aux));
4897 ivernaux = iverneed->vn_auxptr;
4898 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4900 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4902 ivernaux->vna_nodename =
4903 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4904 ivernaux->vna_name);
4905 if (ivernaux->vna_nodename == NULL)
4906 goto error_return;
4908 if (j + 1 < iverneed->vn_cnt)
4909 ivernaux->vna_nextptr = ivernaux + 1;
4910 else
4911 ivernaux->vna_nextptr = NULL;
4913 evernaux = ((Elf_External_Vernaux *)
4914 ((bfd_byte *) evernaux + ivernaux->vna_next));
4917 if (i + 1 < hdr->sh_info)
4918 iverneed->vn_nextref = iverneed + 1;
4919 else
4920 iverneed->vn_nextref = NULL;
4922 everneed = ((Elf_External_Verneed *)
4923 ((bfd_byte *) everneed + iverneed->vn_next));
4926 free (contents);
4927 contents = NULL;
4930 return true;
4932 error_return:
4933 if (contents == NULL)
4934 free (contents);
4935 return false;
4938 asymbol *
4939 _bfd_elf_make_empty_symbol (abfd)
4940 bfd *abfd;
4942 elf_symbol_type *newsym;
4944 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4945 if (!newsym)
4946 return NULL;
4947 else
4949 newsym->symbol.the_bfd = abfd;
4950 return &newsym->symbol;
4954 void
4955 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4956 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4957 asymbol *symbol;
4958 symbol_info *ret;
4960 bfd_symbol_info (symbol, ret);
4963 /* Return whether a symbol name implies a local symbol. Most targets
4964 use this function for the is_local_label_name entry point, but some
4965 override it. */
4967 boolean
4968 _bfd_elf_is_local_label_name (abfd, name)
4969 bfd *abfd ATTRIBUTE_UNUSED;
4970 const char *name;
4972 /* Normal local symbols start with ``.L''. */
4973 if (name[0] == '.' && name[1] == 'L')
4974 return true;
4976 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4977 DWARF debugging symbols starting with ``..''. */
4978 if (name[0] == '.' && name[1] == '.')
4979 return true;
4981 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4982 emitting DWARF debugging output. I suspect this is actually a
4983 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4984 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4985 underscore to be emitted on some ELF targets). For ease of use,
4986 we treat such symbols as local. */
4987 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4988 return true;
4990 return false;
4993 alent *
4994 _bfd_elf_get_lineno (ignore_abfd, symbol)
4995 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4996 asymbol *symbol ATTRIBUTE_UNUSED;
4998 abort ();
4999 return NULL;
5002 boolean
5003 _bfd_elf_set_arch_mach (abfd, arch, machine)
5004 bfd *abfd;
5005 enum bfd_architecture arch;
5006 unsigned long machine;
5008 /* If this isn't the right architecture for this backend, and this
5009 isn't the generic backend, fail. */
5010 if (arch != get_elf_backend_data (abfd)->arch
5011 && arch != bfd_arch_unknown
5012 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5013 return false;
5015 return bfd_default_set_arch_mach (abfd, arch, machine);
5018 /* Find the function to a particular section and offset,
5019 for error reporting. */
5021 static boolean
5022 elf_find_function (abfd, section, symbols, offset,
5023 filename_ptr, functionname_ptr)
5024 bfd *abfd ATTRIBUTE_UNUSED;
5025 asection *section;
5026 asymbol **symbols;
5027 bfd_vma offset;
5028 const char **filename_ptr;
5029 const char **functionname_ptr;
5031 const char *filename;
5032 asymbol *func;
5033 bfd_vma low_func;
5034 asymbol **p;
5036 filename = NULL;
5037 func = NULL;
5038 low_func = 0;
5040 for (p = symbols; *p != NULL; p++)
5042 elf_symbol_type *q;
5044 q = (elf_symbol_type *) *p;
5046 if (bfd_get_section (&q->symbol) != section)
5047 continue;
5049 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5051 default:
5052 break;
5053 case STT_FILE:
5054 filename = bfd_asymbol_name (&q->symbol);
5055 break;
5056 case STT_NOTYPE:
5057 case STT_FUNC:
5058 if (q->symbol.section == section
5059 && q->symbol.value >= low_func
5060 && q->symbol.value <= offset)
5062 func = (asymbol *) q;
5063 low_func = q->symbol.value;
5065 break;
5069 if (func == NULL)
5070 return false;
5072 if (filename_ptr)
5073 *filename_ptr = filename;
5074 if (functionname_ptr)
5075 *functionname_ptr = bfd_asymbol_name (func);
5077 return true;
5080 /* Find the nearest line to a particular section and offset,
5081 for error reporting. */
5083 boolean
5084 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5085 filename_ptr, functionname_ptr, line_ptr)
5086 bfd *abfd;
5087 asection *section;
5088 asymbol **symbols;
5089 bfd_vma offset;
5090 const char **filename_ptr;
5091 const char **functionname_ptr;
5092 unsigned int *line_ptr;
5094 boolean found;
5096 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5097 filename_ptr, functionname_ptr,
5098 line_ptr))
5100 if (!*functionname_ptr)
5101 elf_find_function (abfd, section, symbols, offset,
5102 *filename_ptr ? NULL : filename_ptr,
5103 functionname_ptr);
5105 return true;
5108 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5109 filename_ptr, functionname_ptr,
5110 line_ptr, 0,
5111 &elf_tdata (abfd)->dwarf2_find_line_info))
5113 if (!*functionname_ptr)
5114 elf_find_function (abfd, section, symbols, offset,
5115 *filename_ptr ? NULL : filename_ptr,
5116 functionname_ptr);
5118 return true;
5121 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5122 &found, filename_ptr,
5123 functionname_ptr, line_ptr,
5124 &elf_tdata (abfd)->line_info))
5125 return false;
5126 if (found)
5127 return true;
5129 if (symbols == NULL)
5130 return false;
5132 if (! elf_find_function (abfd, section, symbols, offset,
5133 filename_ptr, functionname_ptr))
5134 return false;
5136 *line_ptr = 0;
5137 return true;
5141 _bfd_elf_sizeof_headers (abfd, reloc)
5142 bfd *abfd;
5143 boolean reloc;
5145 int ret;
5147 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5148 if (! reloc)
5149 ret += get_program_header_size (abfd);
5150 return ret;
5153 boolean
5154 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5155 bfd *abfd;
5156 sec_ptr section;
5157 PTR location;
5158 file_ptr offset;
5159 bfd_size_type count;
5161 Elf_Internal_Shdr *hdr;
5163 if (! abfd->output_has_begun
5164 && ! _bfd_elf_compute_section_file_positions
5165 (abfd, (struct bfd_link_info *) NULL))
5166 return false;
5168 hdr = &elf_section_data (section)->this_hdr;
5170 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5171 return false;
5172 if (bfd_write (location, 1, count, abfd) != count)
5173 return false;
5175 return true;
5178 void
5179 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5180 bfd *abfd ATTRIBUTE_UNUSED;
5181 arelent *cache_ptr ATTRIBUTE_UNUSED;
5182 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5184 abort ();
5187 #if 0
5188 void
5189 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5190 bfd *abfd;
5191 arelent *cache_ptr;
5192 Elf_Internal_Rel *dst;
5194 abort ();
5196 #endif
5198 /* Try to convert a non-ELF reloc into an ELF one. */
5200 boolean
5201 _bfd_elf_validate_reloc (abfd, areloc)
5202 bfd *abfd;
5203 arelent *areloc;
5205 /* Check whether we really have an ELF howto. */
5207 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5209 bfd_reloc_code_real_type code;
5210 reloc_howto_type *howto;
5212 /* Alien reloc: Try to determine its type to replace it with an
5213 equivalent ELF reloc. */
5215 if (areloc->howto->pc_relative)
5217 switch (areloc->howto->bitsize)
5219 case 8:
5220 code = BFD_RELOC_8_PCREL;
5221 break;
5222 case 12:
5223 code = BFD_RELOC_12_PCREL;
5224 break;
5225 case 16:
5226 code = BFD_RELOC_16_PCREL;
5227 break;
5228 case 24:
5229 code = BFD_RELOC_24_PCREL;
5230 break;
5231 case 32:
5232 code = BFD_RELOC_32_PCREL;
5233 break;
5234 case 64:
5235 code = BFD_RELOC_64_PCREL;
5236 break;
5237 default:
5238 goto fail;
5241 howto = bfd_reloc_type_lookup (abfd, code);
5243 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5245 if (howto->pcrel_offset)
5246 areloc->addend += areloc->address;
5247 else
5248 areloc->addend -= areloc->address; /* addend is unsigned!! */
5251 else
5253 switch (areloc->howto->bitsize)
5255 case 8:
5256 code = BFD_RELOC_8;
5257 break;
5258 case 14:
5259 code = BFD_RELOC_14;
5260 break;
5261 case 16:
5262 code = BFD_RELOC_16;
5263 break;
5264 case 26:
5265 code = BFD_RELOC_26;
5266 break;
5267 case 32:
5268 code = BFD_RELOC_32;
5269 break;
5270 case 64:
5271 code = BFD_RELOC_64;
5272 break;
5273 default:
5274 goto fail;
5277 howto = bfd_reloc_type_lookup (abfd, code);
5280 if (howto)
5281 areloc->howto = howto;
5282 else
5283 goto fail;
5286 return true;
5288 fail:
5289 (*_bfd_error_handler)
5290 (_("%s: unsupported relocation type %s"),
5291 bfd_get_filename (abfd), areloc->howto->name);
5292 bfd_set_error (bfd_error_bad_value);
5293 return false;
5296 boolean
5297 _bfd_elf_close_and_cleanup (abfd)
5298 bfd *abfd;
5300 if (bfd_get_format (abfd) == bfd_object)
5302 if (elf_shstrtab (abfd) != NULL)
5303 _bfd_stringtab_free (elf_shstrtab (abfd));
5306 return _bfd_generic_close_and_cleanup (abfd);
5309 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5310 in the relocation's offset. Thus we cannot allow any sort of sanity
5311 range-checking to interfere. There is nothing else to do in processing
5312 this reloc. */
5314 bfd_reloc_status_type
5315 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5316 bfd *abfd ATTRIBUTE_UNUSED;
5317 arelent *re ATTRIBUTE_UNUSED;
5318 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5319 PTR data ATTRIBUTE_UNUSED;
5320 asection *is ATTRIBUTE_UNUSED;
5321 bfd *obfd ATTRIBUTE_UNUSED;
5322 char **errmsg ATTRIBUTE_UNUSED;
5324 return bfd_reloc_ok;
5327 /* Elf core file support. Much of this only works on native
5328 toolchains, since we rely on knowing the
5329 machine-dependent procfs structure in order to pick
5330 out details about the corefile. */
5332 #ifdef HAVE_SYS_PROCFS_H
5333 # include <sys/procfs.h>
5334 #endif
5336 /* Define offsetof for those systems which lack it. */
5338 #ifndef offsetof
5339 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5340 #endif
5342 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5344 static int
5345 elfcore_make_pid (abfd)
5346 bfd *abfd;
5348 return ((elf_tdata (abfd)->core_lwpid << 16)
5349 + (elf_tdata (abfd)->core_pid));
5352 /* If there isn't a section called NAME, make one, using
5353 data from SECT. Note, this function will generate a
5354 reference to NAME, so you shouldn't deallocate or
5355 overwrite it. */
5357 static boolean
5358 elfcore_maybe_make_sect (abfd, name, sect)
5359 bfd *abfd;
5360 char *name;
5361 asection *sect;
5363 asection *sect2;
5365 if (bfd_get_section_by_name (abfd, name) != NULL)
5366 return true;
5368 sect2 = bfd_make_section (abfd, name);
5369 if (sect2 == NULL)
5370 return false;
5372 sect2->_raw_size = sect->_raw_size;
5373 sect2->filepos = sect->filepos;
5374 sect2->flags = sect->flags;
5375 sect2->alignment_power = sect->alignment_power;
5376 return true;
5379 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5380 actually creates up to two pseudosections:
5381 - For the single-threaded case, a section named NAME, unless
5382 such a section already exists.
5383 - For the multi-threaded case, a section named "NAME/PID", where
5384 PID is elfcore_make_pid (abfd).
5385 Both pseudosections have identical contents. */
5386 boolean
5387 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5388 bfd *abfd;
5389 char *name;
5390 int size;
5391 int filepos;
5393 char buf[100];
5394 char *threaded_name;
5395 asection *sect;
5397 /* Build the section name. */
5399 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5400 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5401 if (threaded_name == NULL)
5402 return false;
5403 strcpy (threaded_name, buf);
5405 sect = bfd_make_section (abfd, threaded_name);
5406 if (sect == NULL)
5407 return false;
5408 sect->_raw_size = size;
5409 sect->filepos = filepos;
5410 sect->flags = SEC_HAS_CONTENTS;
5411 sect->alignment_power = 2;
5413 return elfcore_maybe_make_sect (abfd, name, sect);
5416 /* prstatus_t exists on:
5417 solaris 2.5+
5418 linux 2.[01] + glibc
5419 unixware 4.2
5422 #if defined (HAVE_PRSTATUS_T)
5423 static boolean
5424 elfcore_grok_prstatus (abfd, note)
5425 bfd *abfd;
5426 Elf_Internal_Note *note;
5428 int raw_size;
5429 int offset;
5431 if (note->descsz == sizeof (prstatus_t))
5433 prstatus_t prstat;
5435 raw_size = sizeof (prstat.pr_reg);
5436 offset = offsetof (prstatus_t, pr_reg);
5437 memcpy (&prstat, note->descdata, sizeof (prstat));
5439 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5440 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5442 /* pr_who exists on:
5443 solaris 2.5+
5444 unixware 4.2
5445 pr_who doesn't exist on:
5446 linux 2.[01]
5448 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5449 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5450 #endif
5452 #if defined (HAVE_PRSTATUS32_T)
5453 else if (note->descsz == sizeof (prstatus32_t))
5455 /* 64-bit host, 32-bit corefile */
5456 prstatus32_t prstat;
5458 raw_size = sizeof (prstat.pr_reg);
5459 offset = offsetof (prstatus32_t, pr_reg);
5460 memcpy (&prstat, note->descdata, sizeof (prstat));
5462 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5463 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5465 /* pr_who exists on:
5466 solaris 2.5+
5467 unixware 4.2
5468 pr_who doesn't exist on:
5469 linux 2.[01]
5471 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5472 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5473 #endif
5475 #endif /* HAVE_PRSTATUS32_T */
5476 else
5478 /* Fail - we don't know how to handle any other
5479 note size (ie. data object type). */
5480 return true;
5483 /* Make a ".reg/999" section and a ".reg" section. */
5484 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5485 raw_size, note->descpos + offset);
5487 #endif /* defined (HAVE_PRSTATUS_T) */
5489 /* Create a pseudosection containing the exact contents of NOTE. */
5490 static boolean
5491 elfcore_make_note_pseudosection (abfd, name, note)
5492 bfd *abfd;
5493 char *name;
5494 Elf_Internal_Note *note;
5496 return _bfd_elfcore_make_pseudosection (abfd, name,
5497 note->descsz, note->descpos);
5500 /* There isn't a consistent prfpregset_t across platforms,
5501 but it doesn't matter, because we don't have to pick this
5502 data structure apart. */
5504 static boolean
5505 elfcore_grok_prfpreg (abfd, note)
5506 bfd *abfd;
5507 Elf_Internal_Note *note;
5509 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5512 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5513 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5514 literally. */
5516 static boolean
5517 elfcore_grok_prxfpreg (abfd, note)
5518 bfd *abfd;
5519 Elf_Internal_Note *note;
5521 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5524 #if defined (HAVE_PRPSINFO_T)
5525 typedef prpsinfo_t elfcore_psinfo_t;
5526 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5527 typedef prpsinfo32_t elfcore_psinfo32_t;
5528 #endif
5529 #endif
5531 #if defined (HAVE_PSINFO_T)
5532 typedef psinfo_t elfcore_psinfo_t;
5533 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5534 typedef psinfo32_t elfcore_psinfo32_t;
5535 #endif
5536 #endif
5538 /* return a malloc'ed copy of a string at START which is at
5539 most MAX bytes long, possibly without a terminating '\0'.
5540 the copy will always have a terminating '\0'. */
5542 char *
5543 _bfd_elfcore_strndup (abfd, start, max)
5544 bfd *abfd;
5545 char *start;
5546 int max;
5548 char *dup;
5549 char *end = memchr (start, '\0', max);
5550 int len;
5552 if (end == NULL)
5553 len = max;
5554 else
5555 len = end - start;
5557 dup = bfd_alloc (abfd, len + 1);
5558 if (dup == NULL)
5559 return NULL;
5561 memcpy (dup, start, len);
5562 dup[len] = '\0';
5564 return dup;
5567 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5569 static boolean
5570 elfcore_grok_psinfo (abfd, note)
5571 bfd *abfd;
5572 Elf_Internal_Note *note;
5574 if (note->descsz == sizeof (elfcore_psinfo_t))
5576 elfcore_psinfo_t psinfo;
5578 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5580 elf_tdata (abfd)->core_program
5581 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5582 sizeof (psinfo.pr_fname));
5584 elf_tdata (abfd)->core_command
5585 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5586 sizeof (psinfo.pr_psargs));
5588 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5589 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5591 /* 64-bit host, 32-bit corefile */
5592 elfcore_psinfo32_t psinfo;
5594 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5596 elf_tdata (abfd)->core_program
5597 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5598 sizeof (psinfo.pr_fname));
5600 elf_tdata (abfd)->core_command
5601 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5602 sizeof (psinfo.pr_psargs));
5604 #endif
5606 else
5608 /* Fail - we don't know how to handle any other
5609 note size (ie. data object type). */
5610 return true;
5613 /* Note that for some reason, a spurious space is tacked
5614 onto the end of the args in some (at least one anyway)
5615 implementations, so strip it off if it exists. */
5618 char *command = elf_tdata (abfd)->core_command;
5619 int n = strlen (command);
5621 if (0 < n && command[n - 1] == ' ')
5622 command[n - 1] = '\0';
5625 return true;
5627 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5629 #if defined (HAVE_PSTATUS_T)
5630 static boolean
5631 elfcore_grok_pstatus (abfd, note)
5632 bfd *abfd;
5633 Elf_Internal_Note *note;
5635 if (note->descsz == sizeof (pstatus_t)
5636 #if defined (HAVE_PXSTATUS_T)
5637 || note->descsz == sizeof (pxstatus_t)
5638 #endif
5641 pstatus_t pstat;
5643 memcpy (&pstat, note->descdata, sizeof (pstat));
5645 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5647 #if defined (HAVE_PSTATUS32_T)
5648 else if (note->descsz == sizeof (pstatus32_t))
5650 /* 64-bit host, 32-bit corefile */
5651 pstatus32_t pstat;
5653 memcpy (&pstat, note->descdata, sizeof (pstat));
5655 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5657 #endif
5658 /* Could grab some more details from the "representative"
5659 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5660 NT_LWPSTATUS note, presumably. */
5662 return true;
5664 #endif /* defined (HAVE_PSTATUS_T) */
5666 #if defined (HAVE_LWPSTATUS_T)
5667 static boolean
5668 elfcore_grok_lwpstatus (abfd, note)
5669 bfd *abfd;
5670 Elf_Internal_Note *note;
5672 lwpstatus_t lwpstat;
5673 char buf[100];
5674 char *name;
5675 asection *sect;
5677 if (note->descsz != sizeof (lwpstat)
5678 #if defined (HAVE_LWPXSTATUS_T)
5679 && note->descsz != sizeof (lwpxstatus_t)
5680 #endif
5682 return true;
5684 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5686 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5687 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5689 /* Make a ".reg/999" section. */
5691 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5692 name = bfd_alloc (abfd, strlen (buf) + 1);
5693 if (name == NULL)
5694 return false;
5695 strcpy (name, buf);
5697 sect = bfd_make_section (abfd, name);
5698 if (sect == NULL)
5699 return false;
5701 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5702 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5703 sect->filepos = note->descpos
5704 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5705 #endif
5707 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5708 sect->_raw_size = sizeof (lwpstat.pr_reg);
5709 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5710 #endif
5712 sect->flags = SEC_HAS_CONTENTS;
5713 sect->alignment_power = 2;
5715 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5716 return false;
5718 /* Make a ".reg2/999" section */
5720 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5721 name = bfd_alloc (abfd, strlen (buf) + 1);
5722 if (name == NULL)
5723 return false;
5724 strcpy (name, buf);
5726 sect = bfd_make_section (abfd, name);
5727 if (sect == NULL)
5728 return false;
5730 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5731 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5732 sect->filepos = note->descpos
5733 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5734 #endif
5736 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5737 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5738 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5739 #endif
5741 sect->flags = SEC_HAS_CONTENTS;
5742 sect->alignment_power = 2;
5744 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
5746 #endif /* defined (HAVE_LWPSTATUS_T) */
5748 #if defined (HAVE_WIN32_PSTATUS_T)
5749 static boolean
5750 elfcore_grok_win32pstatus (abfd, note)
5751 bfd *abfd;
5752 Elf_Internal_Note *note;
5754 char buf[30];
5755 char *name;
5756 asection *sect;
5757 win32_pstatus_t pstatus;
5759 if (note->descsz < sizeof (pstatus))
5760 return true;
5762 memcpy (&pstatus, note->descdata, note->descsz);
5764 switch (pstatus.data_type)
5766 case NOTE_INFO_PROCESS:
5767 /* FIXME: need to add ->core_command. */
5768 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5769 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5770 break;
5772 case NOTE_INFO_THREAD:
5773 /* Make a ".reg/999" section. */
5774 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5776 name = bfd_alloc (abfd, strlen (buf) + 1);
5777 if (name == NULL)
5778 return false;
5780 strcpy (name, buf);
5782 sect = bfd_make_section (abfd, name);
5783 if (sect == NULL)
5784 return false;
5786 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5787 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5788 data.thread_info.thread_context);
5789 sect->flags = SEC_HAS_CONTENTS;
5790 sect->alignment_power = 2;
5792 if (pstatus.data.thread_info.is_active_thread)
5793 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5794 return false;
5795 break;
5797 case NOTE_INFO_MODULE:
5798 /* Make a ".module/xxxxxxxx" section. */
5799 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5801 name = bfd_alloc (abfd, strlen (buf) + 1);
5802 if (name == NULL)
5803 return false;
5805 strcpy (name, buf);
5807 sect = bfd_make_section (abfd, name);
5809 if (sect == NULL)
5810 return false;
5812 sect->_raw_size = note->descsz;
5813 sect->filepos = note->descpos;
5814 sect->flags = SEC_HAS_CONTENTS;
5815 sect->alignment_power = 2;
5816 break;
5818 default:
5819 return true;
5822 return true;
5824 #endif /* HAVE_WIN32_PSTATUS_T */
5826 static boolean
5827 elfcore_grok_note (abfd, note)
5828 bfd *abfd;
5829 Elf_Internal_Note *note;
5831 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5833 switch (note->type)
5835 default:
5836 return true;
5838 case NT_PRSTATUS:
5839 if (bed->elf_backend_grok_prstatus)
5840 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
5841 return true;
5842 #if defined (HAVE_PRSTATUS_T)
5843 return elfcore_grok_prstatus (abfd, note);
5844 #else
5845 return true;
5846 #endif
5848 #if defined (HAVE_PSTATUS_T)
5849 case NT_PSTATUS:
5850 return elfcore_grok_pstatus (abfd, note);
5851 #endif
5853 #if defined (HAVE_LWPSTATUS_T)
5854 case NT_LWPSTATUS:
5855 return elfcore_grok_lwpstatus (abfd, note);
5856 #endif
5858 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5859 return elfcore_grok_prfpreg (abfd, note);
5861 #if defined (HAVE_WIN32_PSTATUS_T)
5862 case NT_WIN32PSTATUS:
5863 return elfcore_grok_win32pstatus (abfd, note);
5864 #endif
5866 case NT_PRXFPREG: /* Linux SSE extension */
5867 if (note->namesz == 5
5868 && ! strcmp (note->namedata, "LINUX"))
5869 return elfcore_grok_prxfpreg (abfd, note);
5870 else
5871 return true;
5873 case NT_PRPSINFO:
5874 case NT_PSINFO:
5875 if (bed->elf_backend_grok_psinfo)
5876 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
5877 return true;
5878 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5879 return elfcore_grok_psinfo (abfd, note);
5880 #else
5881 return true;
5882 #endif
5886 static boolean
5887 elfcore_read_notes (abfd, offset, size)
5888 bfd *abfd;
5889 bfd_vma offset;
5890 bfd_vma size;
5892 char *buf;
5893 char *p;
5895 if (size <= 0)
5896 return true;
5898 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5899 return false;
5901 buf = bfd_malloc ((size_t) size);
5902 if (buf == NULL)
5903 return false;
5905 if (bfd_read (buf, size, 1, abfd) != size)
5907 error:
5908 free (buf);
5909 return false;
5912 p = buf;
5913 while (p < buf + size)
5915 /* FIXME: bad alignment assumption. */
5916 Elf_External_Note *xnp = (Elf_External_Note *) p;
5917 Elf_Internal_Note in;
5919 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5921 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5922 in.namedata = xnp->name;
5924 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5925 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5926 in.descpos = offset + (in.descdata - buf);
5928 if (! elfcore_grok_note (abfd, &in))
5929 goto error;
5931 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5934 free (buf);
5935 return true;
5938 /* Providing external access to the ELF program header table. */
5940 /* Return an upper bound on the number of bytes required to store a
5941 copy of ABFD's program header table entries. Return -1 if an error
5942 occurs; bfd_get_error will return an appropriate code. */
5944 long
5945 bfd_get_elf_phdr_upper_bound (abfd)
5946 bfd *abfd;
5948 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5950 bfd_set_error (bfd_error_wrong_format);
5951 return -1;
5954 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
5957 /* Copy ABFD's program header table entries to *PHDRS. The entries
5958 will be stored as an array of Elf_Internal_Phdr structures, as
5959 defined in include/elf/internal.h. To find out how large the
5960 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5962 Return the number of program header table entries read, or -1 if an
5963 error occurs; bfd_get_error will return an appropriate code. */
5966 bfd_get_elf_phdrs (abfd, phdrs)
5967 bfd *abfd;
5968 void *phdrs;
5970 int num_phdrs;
5972 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5974 bfd_set_error (bfd_error_wrong_format);
5975 return -1;
5978 num_phdrs = elf_elfheader (abfd)->e_phnum;
5979 memcpy (phdrs, elf_tdata (abfd)->phdr,
5980 num_phdrs * sizeof (Elf_Internal_Phdr));
5982 return num_phdrs;