* output-file.c (output_file_create): Don't try to open using
[binutils.git] / bfd / elf.c
blob8dc2f3145447665a38122bfab488fbb92d7ef3dd
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))
2681 /* If the indicies are the same, do not return 0
2682 here, but continue to try the next comparison. */
2683 if (sec1->target_index - sec2->target_index != 0)
2684 return sec1->target_index - sec2->target_index;
2686 else
2687 return 1;
2689 else if (TOEND (sec2))
2690 return -1;
2692 #undef TOEND
2694 /* Sort by size, to put zero sized sections
2695 before others at the same address. */
2697 if (sec1->_raw_size < sec2->_raw_size)
2698 return -1;
2699 if (sec1->_raw_size > sec2->_raw_size)
2700 return 1;
2702 return sec1->target_index - sec2->target_index;
2705 /* Assign file positions to the sections based on the mapping from
2706 sections to segments. This function also sets up some fields in
2707 the file header, and writes out the program headers. */
2709 static boolean
2710 assign_file_positions_for_segments (abfd)
2711 bfd *abfd;
2713 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2714 unsigned int count;
2715 struct elf_segment_map *m;
2716 unsigned int alloc;
2717 Elf_Internal_Phdr *phdrs;
2718 file_ptr off, voff;
2719 bfd_vma filehdr_vaddr, filehdr_paddr;
2720 bfd_vma phdrs_vaddr, phdrs_paddr;
2721 Elf_Internal_Phdr *p;
2723 if (elf_tdata (abfd)->segment_map == NULL)
2725 if (! map_sections_to_segments (abfd))
2726 return false;
2729 if (bed->elf_backend_modify_segment_map)
2731 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2732 return false;
2735 count = 0;
2736 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2737 ++count;
2739 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2740 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2741 elf_elfheader (abfd)->e_phnum = count;
2743 if (count == 0)
2744 return true;
2746 /* If we already counted the number of program segments, make sure
2747 that we allocated enough space. This happens when SIZEOF_HEADERS
2748 is used in a linker script. */
2749 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2750 if (alloc != 0 && count > alloc)
2752 ((*_bfd_error_handler)
2753 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2754 bfd_get_filename (abfd), alloc, count));
2755 bfd_set_error (bfd_error_bad_value);
2756 return false;
2759 if (alloc == 0)
2760 alloc = count;
2762 phdrs = ((Elf_Internal_Phdr *)
2763 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2764 if (phdrs == NULL)
2765 return false;
2767 off = bed->s->sizeof_ehdr;
2768 off += alloc * bed->s->sizeof_phdr;
2770 filehdr_vaddr = 0;
2771 filehdr_paddr = 0;
2772 phdrs_vaddr = 0;
2773 phdrs_paddr = 0;
2775 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2776 m != NULL;
2777 m = m->next, p++)
2779 unsigned int i;
2780 asection **secpp;
2782 /* If elf_segment_map is not from map_sections_to_segments, the
2783 sections may not be correctly ordered. */
2784 if (m->count > 0)
2785 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2786 elf_sort_sections);
2788 p->p_type = m->p_type;
2789 p->p_flags = m->p_flags;
2791 if (p->p_type == PT_LOAD
2792 && m->count > 0
2793 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2795 if ((abfd->flags & D_PAGED) != 0)
2796 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2797 else
2799 bfd_size_type align;
2801 align = 0;
2802 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2804 bfd_size_type secalign;
2806 secalign = bfd_get_section_alignment (abfd, *secpp);
2807 if (secalign > align)
2808 align = secalign;
2811 off += (m->sections[0]->vma - off) % (1 << align);
2815 if (m->count == 0)
2816 p->p_vaddr = 0;
2817 else
2818 p->p_vaddr = m->sections[0]->vma;
2820 if (m->p_paddr_valid)
2821 p->p_paddr = m->p_paddr;
2822 else if (m->count == 0)
2823 p->p_paddr = 0;
2824 else
2825 p->p_paddr = m->sections[0]->lma;
2827 if (p->p_type == PT_LOAD
2828 && (abfd->flags & D_PAGED) != 0)
2829 p->p_align = bed->maxpagesize;
2830 else if (m->count == 0)
2831 p->p_align = bed->s->file_align;
2832 else
2833 p->p_align = 0;
2835 p->p_offset = 0;
2836 p->p_filesz = 0;
2837 p->p_memsz = 0;
2839 if (m->includes_filehdr)
2841 if (! m->p_flags_valid)
2842 p->p_flags |= PF_R;
2843 p->p_offset = 0;
2844 p->p_filesz = bed->s->sizeof_ehdr;
2845 p->p_memsz = bed->s->sizeof_ehdr;
2846 if (m->count > 0)
2848 BFD_ASSERT (p->p_type == PT_LOAD);
2850 if (p->p_vaddr < (bfd_vma) off)
2852 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2853 bfd_get_filename (abfd));
2854 bfd_set_error (bfd_error_bad_value);
2855 return false;
2858 p->p_vaddr -= off;
2859 if (! m->p_paddr_valid)
2860 p->p_paddr -= off;
2862 if (p->p_type == PT_LOAD)
2864 filehdr_vaddr = p->p_vaddr;
2865 filehdr_paddr = p->p_paddr;
2869 if (m->includes_phdrs)
2871 if (! m->p_flags_valid)
2872 p->p_flags |= PF_R;
2874 if (m->includes_filehdr)
2876 if (p->p_type == PT_LOAD)
2878 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2879 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2882 else
2884 p->p_offset = bed->s->sizeof_ehdr;
2886 if (m->count > 0)
2888 BFD_ASSERT (p->p_type == PT_LOAD);
2889 p->p_vaddr -= off - p->p_offset;
2890 if (! m->p_paddr_valid)
2891 p->p_paddr -= off - p->p_offset;
2894 if (p->p_type == PT_LOAD)
2896 phdrs_vaddr = p->p_vaddr;
2897 phdrs_paddr = p->p_paddr;
2899 else
2900 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2903 p->p_filesz += alloc * bed->s->sizeof_phdr;
2904 p->p_memsz += alloc * bed->s->sizeof_phdr;
2907 if (p->p_type == PT_LOAD
2908 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2910 if (! m->includes_filehdr && ! m->includes_phdrs)
2911 p->p_offset = off;
2912 else
2914 file_ptr adjust;
2916 adjust = off - (p->p_offset + p->p_filesz);
2917 p->p_filesz += adjust;
2918 p->p_memsz += adjust;
2922 voff = off;
2924 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2926 asection *sec;
2927 flagword flags;
2928 bfd_size_type align;
2930 sec = *secpp;
2931 flags = sec->flags;
2932 align = 1 << bfd_get_section_alignment (abfd, sec);
2934 /* The section may have artificial alignment forced by a
2935 link script. Notice this case by the gap between the
2936 cumulative phdr vma and the section's vma. */
2937 if (p->p_vaddr + p->p_memsz < sec->vma)
2939 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2941 p->p_memsz += adjust;
2942 off += adjust;
2943 voff += adjust;
2944 if ((flags & SEC_LOAD) != 0)
2945 p->p_filesz += adjust;
2948 if (p->p_type == PT_LOAD)
2950 bfd_signed_vma adjust;
2952 if ((flags & SEC_LOAD) != 0)
2954 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2955 if (adjust < 0)
2956 adjust = 0;
2958 else if ((flags & SEC_ALLOC) != 0)
2960 /* The section VMA must equal the file position
2961 modulo the page size. FIXME: I'm not sure if
2962 this adjustment is really necessary. We used to
2963 not have the SEC_LOAD case just above, and then
2964 this was necessary, but now I'm not sure. */
2965 if ((abfd->flags & D_PAGED) != 0)
2966 adjust = (sec->vma - voff) % bed->maxpagesize;
2967 else
2968 adjust = (sec->vma - voff) % align;
2970 else
2971 adjust = 0;
2973 if (adjust != 0)
2975 if (i == 0)
2977 (* _bfd_error_handler)
2978 (_("Error: First section in segment (%s) starts at 0x%x"),
2979 bfd_section_name (abfd, sec), sec->lma);
2980 (* _bfd_error_handler)
2981 (_(" whereas segment starts at 0x%x"),
2982 p->p_paddr);
2984 return false;
2986 p->p_memsz += adjust;
2987 off += adjust;
2988 voff += adjust;
2989 if ((flags & SEC_LOAD) != 0)
2990 p->p_filesz += adjust;
2993 sec->filepos = off;
2995 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2996 used in a linker script we may have a section with
2997 SEC_LOAD clear but which is supposed to have
2998 contents. */
2999 if ((flags & SEC_LOAD) != 0
3000 || (flags & SEC_HAS_CONTENTS) != 0)
3001 off += sec->_raw_size;
3003 if ((flags & SEC_ALLOC) != 0)
3004 voff += sec->_raw_size;
3007 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
3009 /* The actual "note" segment has i == 0.
3010 This is the one that actually contains everything. */
3011 if (i == 0)
3013 sec->filepos = off;
3014 p->p_filesz = sec->_raw_size;
3015 off += sec->_raw_size;
3016 voff = off;
3018 else
3020 /* Fake sections -- don't need to be written. */
3021 sec->filepos = 0;
3022 sec->_raw_size = 0;
3023 flags = sec->flags = 0;
3025 p->p_memsz = 0;
3026 p->p_align = 1;
3028 else
3030 p->p_memsz += sec->_raw_size;
3032 if ((flags & SEC_LOAD) != 0)
3033 p->p_filesz += sec->_raw_size;
3035 if (align > p->p_align
3036 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3037 p->p_align = align;
3040 if (! m->p_flags_valid)
3042 p->p_flags |= PF_R;
3043 if ((flags & SEC_CODE) != 0)
3044 p->p_flags |= PF_X;
3045 if ((flags & SEC_READONLY) == 0)
3046 p->p_flags |= PF_W;
3051 /* Now that we have set the section file positions, we can set up
3052 the file positions for the non PT_LOAD segments. */
3053 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3054 m != NULL;
3055 m = m->next, p++)
3057 if (p->p_type != PT_LOAD && m->count > 0)
3059 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3060 p->p_offset = m->sections[0]->filepos;
3062 if (m->count == 0)
3064 if (m->includes_filehdr)
3066 p->p_vaddr = filehdr_vaddr;
3067 if (! m->p_paddr_valid)
3068 p->p_paddr = filehdr_paddr;
3070 else if (m->includes_phdrs)
3072 p->p_vaddr = phdrs_vaddr;
3073 if (! m->p_paddr_valid)
3074 p->p_paddr = phdrs_paddr;
3079 /* Clear out any program headers we allocated but did not use. */
3080 for (; count < alloc; count++, p++)
3082 memset (p, 0, sizeof *p);
3083 p->p_type = PT_NULL;
3086 elf_tdata (abfd)->phdr = phdrs;
3088 elf_tdata (abfd)->next_file_pos = off;
3090 /* Write out the program headers. */
3091 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3092 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3093 return false;
3095 return true;
3098 /* Get the size of the program header.
3100 If this is called by the linker before any of the section VMA's are set, it
3101 can't calculate the correct value for a strange memory layout. This only
3102 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3103 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3104 data segment (exclusive of .interp and .dynamic).
3106 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3107 will be two segments. */
3109 static bfd_size_type
3110 get_program_header_size (abfd)
3111 bfd *abfd;
3113 size_t segs;
3114 asection *s;
3115 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3117 /* We can't return a different result each time we're called. */
3118 if (elf_tdata (abfd)->program_header_size != 0)
3119 return elf_tdata (abfd)->program_header_size;
3121 if (elf_tdata (abfd)->segment_map != NULL)
3123 struct elf_segment_map *m;
3125 segs = 0;
3126 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3127 ++segs;
3128 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3129 return elf_tdata (abfd)->program_header_size;
3132 /* Assume we will need exactly two PT_LOAD segments: one for text
3133 and one for data. */
3134 segs = 2;
3136 s = bfd_get_section_by_name (abfd, ".interp");
3137 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3139 /* If we have a loadable interpreter section, we need a
3140 PT_INTERP segment. In this case, assume we also need a
3141 PT_PHDR segment, although that may not be true for all
3142 targets. */
3143 segs += 2;
3146 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3148 /* We need a PT_DYNAMIC segment. */
3149 ++segs;
3152 for (s = abfd->sections; s != NULL; s = s->next)
3154 if ((s->flags & SEC_LOAD) != 0
3155 && strncmp (s->name, ".note", 5) == 0)
3157 /* We need a PT_NOTE segment. */
3158 ++segs;
3162 /* Let the backend count up any program headers it might need. */
3163 if (bed->elf_backend_additional_program_headers)
3165 int a;
3167 a = (*bed->elf_backend_additional_program_headers) (abfd);
3168 if (a == -1)
3169 abort ();
3170 segs += a;
3173 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3174 return elf_tdata (abfd)->program_header_size;
3177 /* Work out the file positions of all the sections. This is called by
3178 _bfd_elf_compute_section_file_positions. All the section sizes and
3179 VMAs must be known before this is called.
3181 We do not consider reloc sections at this point, unless they form
3182 part of the loadable image. Reloc sections are assigned file
3183 positions in assign_file_positions_for_relocs, which is called by
3184 write_object_contents and final_link.
3186 We also don't set the positions of the .symtab and .strtab here. */
3188 static boolean
3189 assign_file_positions_except_relocs (abfd)
3190 bfd *abfd;
3192 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3193 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3194 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3195 file_ptr off;
3196 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3198 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3199 && bfd_get_format (abfd) != bfd_core)
3201 Elf_Internal_Shdr **hdrpp;
3202 unsigned int i;
3204 /* Start after the ELF header. */
3205 off = i_ehdrp->e_ehsize;
3207 /* We are not creating an executable, which means that we are
3208 not creating a program header, and that the actual order of
3209 the sections in the file is unimportant. */
3210 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3212 Elf_Internal_Shdr *hdr;
3214 hdr = *hdrpp;
3215 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3217 hdr->sh_offset = -1;
3218 continue;
3220 if (i == tdata->symtab_section
3221 || i == tdata->strtab_section)
3223 hdr->sh_offset = -1;
3224 continue;
3227 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3230 else
3232 unsigned int i;
3233 Elf_Internal_Shdr **hdrpp;
3235 /* Assign file positions for the loaded sections based on the
3236 assignment of sections to segments. */
3237 if (! assign_file_positions_for_segments (abfd))
3238 return false;
3240 /* Assign file positions for the other sections. */
3242 off = elf_tdata (abfd)->next_file_pos;
3243 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3245 Elf_Internal_Shdr *hdr;
3247 hdr = *hdrpp;
3248 if (hdr->bfd_section != NULL
3249 && hdr->bfd_section->filepos != 0)
3250 hdr->sh_offset = hdr->bfd_section->filepos;
3251 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3253 ((*_bfd_error_handler)
3254 (_("%s: warning: allocated section `%s' not in segment"),
3255 bfd_get_filename (abfd),
3256 (hdr->bfd_section == NULL
3257 ? "*unknown*"
3258 : hdr->bfd_section->name)));
3259 if ((abfd->flags & D_PAGED) != 0)
3260 off += (hdr->sh_addr - off) % bed->maxpagesize;
3261 else
3262 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3263 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3264 false);
3266 else if (hdr->sh_type == SHT_REL
3267 || hdr->sh_type == SHT_RELA
3268 || hdr == i_shdrpp[tdata->symtab_section]
3269 || hdr == i_shdrpp[tdata->strtab_section])
3270 hdr->sh_offset = -1;
3271 else
3272 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3276 /* Place the section headers. */
3277 off = align_file_position (off, bed->s->file_align);
3278 i_ehdrp->e_shoff = off;
3279 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3281 elf_tdata (abfd)->next_file_pos = off;
3283 return true;
3286 static boolean
3287 prep_headers (abfd)
3288 bfd *abfd;
3290 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3291 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3292 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3293 int count;
3294 struct bfd_strtab_hash *shstrtab;
3295 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3297 i_ehdrp = elf_elfheader (abfd);
3298 i_shdrp = elf_elfsections (abfd);
3300 shstrtab = _bfd_elf_stringtab_init ();
3301 if (shstrtab == NULL)
3302 return false;
3304 elf_shstrtab (abfd) = shstrtab;
3306 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3307 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3308 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3309 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3311 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3312 i_ehdrp->e_ident[EI_DATA] =
3313 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3314 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3316 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3317 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3319 for (count = EI_PAD; count < EI_NIDENT; count++)
3320 i_ehdrp->e_ident[count] = 0;
3322 if ((abfd->flags & DYNAMIC) != 0)
3323 i_ehdrp->e_type = ET_DYN;
3324 else if ((abfd->flags & EXEC_P) != 0)
3325 i_ehdrp->e_type = ET_EXEC;
3326 else if (bfd_get_format (abfd) == bfd_core)
3327 i_ehdrp->e_type = ET_CORE;
3328 else
3329 i_ehdrp->e_type = ET_REL;
3331 switch (bfd_get_arch (abfd))
3333 case bfd_arch_unknown:
3334 i_ehdrp->e_machine = EM_NONE;
3335 break;
3336 case bfd_arch_sparc:
3337 if (bfd_get_arch_size (abfd) == 64)
3338 i_ehdrp->e_machine = EM_SPARCV9;
3339 else
3340 i_ehdrp->e_machine = EM_SPARC;
3341 break;
3342 case bfd_arch_i370:
3343 i_ehdrp->e_machine = EM_S370;
3344 break;
3345 case bfd_arch_i386:
3346 if (bfd_get_arch_size (abfd) == 64)
3347 i_ehdrp->e_machine = EM_X86_64;
3348 else
3349 i_ehdrp->e_machine = EM_386;
3350 break;
3351 case bfd_arch_ia64:
3352 i_ehdrp->e_machine = EM_IA_64;
3353 break;
3354 case bfd_arch_m68hc11:
3355 i_ehdrp->e_machine = EM_68HC11;
3356 break;
3357 case bfd_arch_m68hc12:
3358 i_ehdrp->e_machine = EM_68HC12;
3359 break;
3360 case bfd_arch_s390:
3361 i_ehdrp->e_machine = EM_S390;
3362 break;
3363 case bfd_arch_m68k:
3364 i_ehdrp->e_machine = EM_68K;
3365 break;
3366 case bfd_arch_m88k:
3367 i_ehdrp->e_machine = EM_88K;
3368 break;
3369 case bfd_arch_i860:
3370 i_ehdrp->e_machine = EM_860;
3371 break;
3372 case bfd_arch_i960:
3373 i_ehdrp->e_machine = EM_960;
3374 break;
3375 case bfd_arch_mips: /* MIPS Rxxxx */
3376 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3377 break;
3378 case bfd_arch_hppa:
3379 i_ehdrp->e_machine = EM_PARISC;
3380 break;
3381 case bfd_arch_powerpc:
3382 i_ehdrp->e_machine = EM_PPC;
3383 break;
3384 case bfd_arch_alpha:
3385 i_ehdrp->e_machine = EM_ALPHA;
3386 break;
3387 case bfd_arch_sh:
3388 i_ehdrp->e_machine = EM_SH;
3389 break;
3390 case bfd_arch_d10v:
3391 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3392 break;
3393 case bfd_arch_d30v:
3394 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3395 break;
3396 case bfd_arch_fr30:
3397 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3398 break;
3399 case bfd_arch_mcore:
3400 i_ehdrp->e_machine = EM_MCORE;
3401 break;
3402 case bfd_arch_avr:
3403 i_ehdrp->e_machine = EM_AVR;
3404 break;
3405 case bfd_arch_v850:
3406 switch (bfd_get_mach (abfd))
3408 default:
3409 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3411 break;
3412 case bfd_arch_arc:
3413 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3414 break;
3415 case bfd_arch_arm:
3416 i_ehdrp->e_machine = EM_ARM;
3417 break;
3418 case bfd_arch_m32r:
3419 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3420 break;
3421 case bfd_arch_mn10200:
3422 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3423 break;
3424 case bfd_arch_mn10300:
3425 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3426 break;
3427 case bfd_arch_pj:
3428 i_ehdrp->e_machine = EM_PJ;
3429 break;
3430 case bfd_arch_cris:
3431 i_ehdrp->e_machine = EM_CRIS;
3432 break;
3433 case bfd_arch_openrisc:
3434 i_ehdrp->e_machine = EM_OPENRISC;
3435 break;
3436 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3437 default:
3438 i_ehdrp->e_machine = EM_NONE;
3440 i_ehdrp->e_version = bed->s->ev_current;
3441 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3443 /* No program header, for now. */
3444 i_ehdrp->e_phoff = 0;
3445 i_ehdrp->e_phentsize = 0;
3446 i_ehdrp->e_phnum = 0;
3448 /* Each bfd section is section header entry. */
3449 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3450 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3452 /* If we're building an executable, we'll need a program header table. */
3453 if (abfd->flags & EXEC_P)
3455 /* It all happens later. */
3456 #if 0
3457 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3459 /* elf_build_phdrs() returns a (NULL-terminated) array of
3460 Elf_Internal_Phdrs. */
3461 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3462 i_ehdrp->e_phoff = outbase;
3463 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3464 #endif
3466 else
3468 i_ehdrp->e_phentsize = 0;
3469 i_phdrp = 0;
3470 i_ehdrp->e_phoff = 0;
3473 elf_tdata (abfd)->symtab_hdr.sh_name =
3474 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3475 elf_tdata (abfd)->strtab_hdr.sh_name =
3476 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3477 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3478 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3479 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3480 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3481 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3482 return false;
3484 return true;
3487 /* Assign file positions for all the reloc sections which are not part
3488 of the loadable file image. */
3490 void
3491 _bfd_elf_assign_file_positions_for_relocs (abfd)
3492 bfd *abfd;
3494 file_ptr off;
3495 unsigned int i;
3496 Elf_Internal_Shdr **shdrpp;
3498 off = elf_tdata (abfd)->next_file_pos;
3500 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3501 i < elf_elfheader (abfd)->e_shnum;
3502 i++, shdrpp++)
3504 Elf_Internal_Shdr *shdrp;
3506 shdrp = *shdrpp;
3507 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3508 && shdrp->sh_offset == -1)
3509 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3512 elf_tdata (abfd)->next_file_pos = off;
3515 boolean
3516 _bfd_elf_write_object_contents (abfd)
3517 bfd *abfd;
3519 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3520 Elf_Internal_Ehdr *i_ehdrp;
3521 Elf_Internal_Shdr **i_shdrp;
3522 boolean failed;
3523 unsigned int count;
3525 if (! abfd->output_has_begun
3526 && ! _bfd_elf_compute_section_file_positions
3527 (abfd, (struct bfd_link_info *) NULL))
3528 return false;
3530 i_shdrp = elf_elfsections (abfd);
3531 i_ehdrp = elf_elfheader (abfd);
3533 failed = false;
3534 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3535 if (failed)
3536 return false;
3538 _bfd_elf_assign_file_positions_for_relocs (abfd);
3540 /* After writing the headers, we need to write the sections too... */
3541 for (count = 1; count < i_ehdrp->e_shnum; count++)
3543 if (bed->elf_backend_section_processing)
3544 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3545 if (i_shdrp[count]->contents)
3547 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3548 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3549 1, abfd)
3550 != i_shdrp[count]->sh_size))
3551 return false;
3555 /* Write out the section header names. */
3556 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3557 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3558 return false;
3560 if (bed->elf_backend_final_write_processing)
3561 (*bed->elf_backend_final_write_processing) (abfd,
3562 elf_tdata (abfd)->linker);
3564 return bed->s->write_shdrs_and_ehdr (abfd);
3567 boolean
3568 _bfd_elf_write_corefile_contents (abfd)
3569 bfd *abfd;
3571 /* Hopefully this can be done just like an object file. */
3572 return _bfd_elf_write_object_contents (abfd);
3575 /* Given a section, search the header to find them. */
3578 _bfd_elf_section_from_bfd_section (abfd, asect)
3579 bfd *abfd;
3580 struct sec *asect;
3582 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3583 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3584 int index;
3585 Elf_Internal_Shdr *hdr;
3586 int maxindex = elf_elfheader (abfd)->e_shnum;
3588 for (index = 0; index < maxindex; index++)
3590 hdr = i_shdrp[index];
3591 if (hdr->bfd_section == asect)
3592 return index;
3595 if (bed->elf_backend_section_from_bfd_section)
3597 for (index = 0; index < maxindex; index++)
3599 int retval;
3601 hdr = i_shdrp[index];
3602 retval = index;
3603 if ((*bed->elf_backend_section_from_bfd_section)
3604 (abfd, hdr, asect, &retval))
3605 return retval;
3609 if (bfd_is_abs_section (asect))
3610 return SHN_ABS;
3611 if (bfd_is_com_section (asect))
3612 return SHN_COMMON;
3613 if (bfd_is_und_section (asect))
3614 return SHN_UNDEF;
3616 bfd_set_error (bfd_error_nonrepresentable_section);
3618 return -1;
3621 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3622 on error. */
3625 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3626 bfd *abfd;
3627 asymbol **asym_ptr_ptr;
3629 asymbol *asym_ptr = *asym_ptr_ptr;
3630 int idx;
3631 flagword flags = asym_ptr->flags;
3633 /* When gas creates relocations against local labels, it creates its
3634 own symbol for the section, but does put the symbol into the
3635 symbol chain, so udata is 0. When the linker is generating
3636 relocatable output, this section symbol may be for one of the
3637 input sections rather than the output section. */
3638 if (asym_ptr->udata.i == 0
3639 && (flags & BSF_SECTION_SYM)
3640 && asym_ptr->section)
3642 int indx;
3644 if (asym_ptr->section->output_section != NULL)
3645 indx = asym_ptr->section->output_section->index;
3646 else
3647 indx = asym_ptr->section->index;
3648 if (elf_section_syms (abfd)[indx])
3649 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3652 idx = asym_ptr->udata.i;
3654 if (idx == 0)
3656 /* This case can occur when using --strip-symbol on a symbol
3657 which is used in a relocation entry. */
3658 (*_bfd_error_handler)
3659 (_("%s: symbol `%s' required but not present"),
3660 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3661 bfd_set_error (bfd_error_no_symbols);
3662 return -1;
3665 #if DEBUG & 4
3667 fprintf (stderr,
3668 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3669 (long) asym_ptr, asym_ptr->name, idx, flags,
3670 elf_symbol_flags (flags));
3671 fflush (stderr);
3673 #endif
3675 return idx;
3678 /* Copy private BFD data. This copies any program header information. */
3680 static boolean
3681 copy_private_bfd_data (ibfd, obfd)
3682 bfd *ibfd;
3683 bfd *obfd;
3685 Elf_Internal_Ehdr * iehdr;
3686 struct elf_segment_map * map;
3687 struct elf_segment_map * map_first;
3688 struct elf_segment_map ** pointer_to_map;
3689 Elf_Internal_Phdr * segment;
3690 asection * section;
3691 unsigned int i;
3692 unsigned int num_segments;
3693 boolean phdr_included = false;
3694 bfd_vma maxpagesize;
3695 struct elf_segment_map * phdr_adjust_seg = NULL;
3696 unsigned int phdr_adjust_num = 0;
3698 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3699 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3700 return true;
3702 if (elf_tdata (ibfd)->phdr == NULL)
3703 return true;
3705 iehdr = elf_elfheader (ibfd);
3707 map_first = NULL;
3708 pointer_to_map = &map_first;
3710 num_segments = elf_elfheader (ibfd)->e_phnum;
3711 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3713 /* Returns the end address of the segment + 1. */
3714 #define SEGMENT_END(segment, start) \
3715 (start + (segment->p_memsz > segment->p_filesz \
3716 ? segment->p_memsz : segment->p_filesz))
3718 /* Returns true if the given section is contained within
3719 the given segment. VMA addresses are compared. */
3720 #define IS_CONTAINED_BY_VMA(section, segment) \
3721 (section->vma >= segment->p_vaddr \
3722 && (section->vma + section->_raw_size) \
3723 <= (SEGMENT_END (segment, segment->p_vaddr)))
3725 /* Returns true if the given section is contained within
3726 the given segment. LMA addresses are compared. */
3727 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3728 (section->lma >= base \
3729 && (section->lma + section->_raw_size) \
3730 <= SEGMENT_END (segment, base))
3732 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3733 #define IS_COREFILE_NOTE(p, s) \
3734 (p->p_type == PT_NOTE \
3735 && bfd_get_format (ibfd) == bfd_core \
3736 && s->vma == 0 && s->lma == 0 \
3737 && (bfd_vma) s->filepos >= p->p_offset \
3738 && (bfd_vma) s->filepos + s->_raw_size \
3739 <= p->p_offset + p->p_filesz)
3741 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3742 linker, which generates a PT_INTERP section with p_vaddr and
3743 p_memsz set to 0. */
3744 #define IS_SOLARIS_PT_INTERP(p, s) \
3745 ( p->p_vaddr == 0 \
3746 && p->p_filesz > 0 \
3747 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3748 && s->_raw_size > 0 \
3749 && (bfd_vma) s->filepos >= p->p_offset \
3750 && ((bfd_vma) s->filepos + s->_raw_size \
3751 <= p->p_offset + p->p_filesz))
3753 /* Decide if the given section should be included in the given segment.
3754 A section will be included if:
3755 1. It is within the address space of the segment,
3756 2. It is an allocated segment,
3757 3. There is an output section associated with it,
3758 4. The section has not already been allocated to a previous segment. */
3759 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3760 ((((IS_CONTAINED_BY_VMA (section, segment) \
3761 || IS_SOLARIS_PT_INTERP (segment, section)) \
3762 && (section->flags & SEC_ALLOC) != 0) \
3763 || IS_COREFILE_NOTE (segment, section)) \
3764 && section->output_section != NULL \
3765 && section->segment_mark == false)
3767 /* Returns true iff seg1 starts after the end of seg2. */
3768 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3769 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3771 /* Returns true iff seg1 and seg2 overlap. */
3772 #define SEGMENT_OVERLAPS(seg1, seg2) \
3773 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3775 /* Initialise the segment mark field. */
3776 for (section = ibfd->sections; section != NULL; section = section->next)
3777 section->segment_mark = false;
3779 /* Scan through the segments specified in the program header
3780 of the input BFD. For this first scan we look for overlaps
3781 in the loadable segments. These can be created by wierd
3782 parameters to objcopy. */
3783 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3784 i < num_segments;
3785 i++, segment++)
3787 unsigned int j;
3788 Elf_Internal_Phdr *segment2;
3790 if (segment->p_type != PT_LOAD)
3791 continue;
3793 /* Determine if this segment overlaps any previous segments. */
3794 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3796 bfd_signed_vma extra_length;
3798 if (segment2->p_type != PT_LOAD
3799 || ! SEGMENT_OVERLAPS (segment, segment2))
3800 continue;
3802 /* Merge the two segments together. */
3803 if (segment2->p_vaddr < segment->p_vaddr)
3805 /* Extend SEGMENT2 to include SEGMENT and then delete
3806 SEGMENT. */
3807 extra_length =
3808 SEGMENT_END (segment, segment->p_vaddr)
3809 - SEGMENT_END (segment2, segment2->p_vaddr);
3811 if (extra_length > 0)
3813 segment2->p_memsz += extra_length;
3814 segment2->p_filesz += extra_length;
3817 segment->p_type = PT_NULL;
3819 /* Since we have deleted P we must restart the outer loop. */
3820 i = 0;
3821 segment = elf_tdata (ibfd)->phdr;
3822 break;
3824 else
3826 /* Extend SEGMENT to include SEGMENT2 and then delete
3827 SEGMENT2. */
3828 extra_length =
3829 SEGMENT_END (segment2, segment2->p_vaddr)
3830 - SEGMENT_END (segment, segment->p_vaddr);
3832 if (extra_length > 0)
3834 segment->p_memsz += extra_length;
3835 segment->p_filesz += extra_length;
3838 segment2->p_type = PT_NULL;
3843 /* The second scan attempts to assign sections to segments. */
3844 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3845 i < num_segments;
3846 i ++, segment ++)
3848 unsigned int section_count;
3849 asection ** sections;
3850 asection * output_section;
3851 unsigned int isec;
3852 bfd_vma matching_lma;
3853 bfd_vma suggested_lma;
3854 unsigned int j;
3856 if (segment->p_type == PT_NULL)
3857 continue;
3859 /* Compute how many sections might be placed into this segment. */
3860 section_count = 0;
3861 for (section = ibfd->sections; section != NULL; section = section->next)
3862 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3863 ++section_count;
3865 /* Allocate a segment map big enough to contain all of the
3866 sections we have selected. */
3867 map = ((struct elf_segment_map *)
3868 bfd_alloc (obfd,
3869 (sizeof (struct elf_segment_map)
3870 + ((size_t) section_count - 1) * sizeof (asection *))));
3871 if (map == NULL)
3872 return false;
3874 /* Initialise the fields of the segment map. Default to
3875 using the physical address of the segment in the input BFD. */
3876 map->next = NULL;
3877 map->p_type = segment->p_type;
3878 map->p_flags = segment->p_flags;
3879 map->p_flags_valid = 1;
3880 map->p_paddr = segment->p_paddr;
3881 map->p_paddr_valid = 1;
3883 /* Determine if this segment contains the ELF file header
3884 and if it contains the program headers themselves. */
3885 map->includes_filehdr = (segment->p_offset == 0
3886 && segment->p_filesz >= iehdr->e_ehsize);
3888 map->includes_phdrs = 0;
3890 if (! phdr_included || segment->p_type != PT_LOAD)
3892 map->includes_phdrs =
3893 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3894 && (segment->p_offset + segment->p_filesz
3895 >= ((bfd_vma) iehdr->e_phoff
3896 + iehdr->e_phnum * iehdr->e_phentsize)));
3898 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3899 phdr_included = true;
3902 if (section_count == 0)
3904 /* Special segments, such as the PT_PHDR segment, may contain
3905 no sections, but ordinary, loadable segments should contain
3906 something. */
3907 if (segment->p_type == PT_LOAD)
3908 _bfd_error_handler
3909 (_("%s: warning: Empty loadable segment detected\n"),
3910 bfd_get_filename (ibfd));
3912 map->count = 0;
3913 *pointer_to_map = map;
3914 pointer_to_map = &map->next;
3916 continue;
3919 /* Now scan the sections in the input BFD again and attempt
3920 to add their corresponding output sections to the segment map.
3921 The problem here is how to handle an output section which has
3922 been moved (ie had its LMA changed). There are four possibilities:
3924 1. None of the sections have been moved.
3925 In this case we can continue to use the segment LMA from the
3926 input BFD.
3928 2. All of the sections have been moved by the same amount.
3929 In this case we can change the segment's LMA to match the LMA
3930 of the first section.
3932 3. Some of the sections have been moved, others have not.
3933 In this case those sections which have not been moved can be
3934 placed in the current segment which will have to have its size,
3935 and possibly its LMA changed, and a new segment or segments will
3936 have to be created to contain the other sections.
3938 4. The sections have been moved, but not be the same amount.
3939 In this case we can change the segment's LMA to match the LMA
3940 of the first section and we will have to create a new segment
3941 or segments to contain the other sections.
3943 In order to save time, we allocate an array to hold the section
3944 pointers that we are interested in. As these sections get assigned
3945 to a segment, they are removed from this array. */
3947 sections = (asection **) bfd_malloc
3948 (sizeof (asection *) * section_count);
3949 if (sections == NULL)
3950 return false;
3952 /* Step One: Scan for segment vs section LMA conflicts.
3953 Also add the sections to the section array allocated above.
3954 Also add the sections to the current segment. In the common
3955 case, where the sections have not been moved, this means that
3956 we have completely filled the segment, and there is nothing
3957 more to do. */
3958 isec = 0;
3959 matching_lma = 0;
3960 suggested_lma = 0;
3962 for (j = 0, section = ibfd->sections;
3963 section != NULL;
3964 section = section->next)
3966 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3968 output_section = section->output_section;
3970 sections[j ++] = section;
3972 /* The Solaris native linker always sets p_paddr to 0.
3973 We try to catch that case here, and set it to the
3974 correct value. */
3975 if (segment->p_paddr == 0
3976 && segment->p_vaddr != 0
3977 && isec == 0
3978 && output_section->lma != 0
3979 && (output_section->vma == (segment->p_vaddr
3980 + (map->includes_filehdr
3981 ? iehdr->e_ehsize
3982 : 0)
3983 + (map->includes_phdrs
3984 ? iehdr->e_phnum * iehdr->e_phentsize
3985 : 0))))
3986 map->p_paddr = segment->p_vaddr;
3988 /* Match up the physical address of the segment with the
3989 LMA address of the output section. */
3990 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3991 || IS_COREFILE_NOTE (segment, section))
3993 if (matching_lma == 0)
3994 matching_lma = output_section->lma;
3996 /* We assume that if the section fits within the segment
3997 then it does not overlap any other section within that
3998 segment. */
3999 map->sections[isec ++] = output_section;
4001 else if (suggested_lma == 0)
4002 suggested_lma = output_section->lma;
4006 BFD_ASSERT (j == section_count);
4008 /* Step Two: Adjust the physical address of the current segment,
4009 if necessary. */
4010 if (isec == section_count)
4012 /* All of the sections fitted within the segment as currently
4013 specified. This is the default case. Add the segment to
4014 the list of built segments and carry on to process the next
4015 program header in the input BFD. */
4016 map->count = section_count;
4017 *pointer_to_map = map;
4018 pointer_to_map = &map->next;
4020 free (sections);
4021 continue;
4023 else
4025 if (matching_lma != 0)
4027 /* At least one section fits inside the current segment.
4028 Keep it, but modify its physical address to match the
4029 LMA of the first section that fitted. */
4030 map->p_paddr = matching_lma;
4032 else
4034 /* None of the sections fitted inside the current segment.
4035 Change the current segment's physical address to match
4036 the LMA of the first section. */
4037 map->p_paddr = suggested_lma;
4040 /* Offset the segment physical address from the lma
4041 to allow for space taken up by elf headers. */
4042 if (map->includes_filehdr)
4043 map->p_paddr -= iehdr->e_ehsize;
4045 if (map->includes_phdrs)
4047 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4049 /* iehdr->e_phnum is just an estimate of the number
4050 of program headers that we will need. Make a note
4051 here of the number we used and the segment we chose
4052 to hold these headers, so that we can adjust the
4053 offset when we know the correct value. */
4054 phdr_adjust_num = iehdr->e_phnum;
4055 phdr_adjust_seg = map;
4059 /* Step Three: Loop over the sections again, this time assigning
4060 those that fit to the current segment and remvoing them from the
4061 sections array; but making sure not to leave large gaps. Once all
4062 possible sections have been assigned to the current segment it is
4063 added to the list of built segments and if sections still remain
4064 to be assigned, a new segment is constructed before repeating
4065 the loop. */
4066 isec = 0;
4069 map->count = 0;
4070 suggested_lma = 0;
4072 /* Fill the current segment with sections that fit. */
4073 for (j = 0; j < section_count; j++)
4075 section = sections[j];
4077 if (section == NULL)
4078 continue;
4080 output_section = section->output_section;
4082 BFD_ASSERT (output_section != NULL);
4084 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4085 || IS_COREFILE_NOTE (segment, section))
4087 if (map->count == 0)
4089 /* If the first section in a segment does not start at
4090 the beginning of the segment, then something is
4091 wrong. */
4092 if (output_section->lma !=
4093 (map->p_paddr
4094 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4095 + (map->includes_phdrs
4096 ? iehdr->e_phnum * iehdr->e_phentsize
4097 : 0)))
4098 abort ();
4100 else
4102 asection * prev_sec;
4104 prev_sec = map->sections[map->count - 1];
4106 /* If the gap between the end of the previous section
4107 and the start of this section is more than
4108 maxpagesize then we need to start a new segment. */
4109 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4110 < BFD_ALIGN (output_section->lma, maxpagesize))
4111 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4113 if (suggested_lma == 0)
4114 suggested_lma = output_section->lma;
4116 continue;
4120 map->sections[map->count++] = output_section;
4121 ++isec;
4122 sections[j] = NULL;
4123 section->segment_mark = true;
4125 else if (suggested_lma == 0)
4126 suggested_lma = output_section->lma;
4129 BFD_ASSERT (map->count > 0);
4131 /* Add the current segment to the list of built segments. */
4132 *pointer_to_map = map;
4133 pointer_to_map = &map->next;
4135 if (isec < section_count)
4137 /* We still have not allocated all of the sections to
4138 segments. Create a new segment here, initialise it
4139 and carry on looping. */
4140 map = ((struct elf_segment_map *)
4141 bfd_alloc (obfd,
4142 (sizeof (struct elf_segment_map)
4143 + ((size_t) section_count - 1)
4144 * sizeof (asection *))));
4145 if (map == NULL)
4146 return false;
4148 /* Initialise the fields of the segment map. Set the physical
4149 physical address to the LMA of the first section that has
4150 not yet been assigned. */
4151 map->next = NULL;
4152 map->p_type = segment->p_type;
4153 map->p_flags = segment->p_flags;
4154 map->p_flags_valid = 1;
4155 map->p_paddr = suggested_lma;
4156 map->p_paddr_valid = 1;
4157 map->includes_filehdr = 0;
4158 map->includes_phdrs = 0;
4161 while (isec < section_count);
4163 free (sections);
4166 /* The Solaris linker creates program headers in which all the
4167 p_paddr fields are zero. When we try to objcopy or strip such a
4168 file, we get confused. Check for this case, and if we find it
4169 reset the p_paddr_valid fields. */
4170 for (map = map_first; map != NULL; map = map->next)
4171 if (map->p_paddr != 0)
4172 break;
4173 if (map == NULL)
4175 for (map = map_first; map != NULL; map = map->next)
4176 map->p_paddr_valid = 0;
4179 elf_tdata (obfd)->segment_map = map_first;
4181 /* If we had to estimate the number of program headers that were
4182 going to be needed, then check our estimate know and adjust
4183 the offset if necessary. */
4184 if (phdr_adjust_seg != NULL)
4186 unsigned int count;
4188 for (count = 0, map = map_first; map != NULL; map = map->next)
4189 count++;
4191 if (count > phdr_adjust_num)
4192 phdr_adjust_seg->p_paddr
4193 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4196 #if 0
4197 /* Final Step: Sort the segments into ascending order of physical
4198 address. */
4199 if (map_first != NULL)
4201 struct elf_segment_map *prev;
4203 prev = map_first;
4204 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4206 /* Yes I know - its a bubble sort.... */
4207 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4209 /* Swap map and map->next. */
4210 prev->next = map->next;
4211 map->next = map->next->next;
4212 prev->next->next = map;
4214 /* Restart loop. */
4215 map = map_first;
4219 #endif
4221 #undef SEGMENT_END
4222 #undef IS_CONTAINED_BY_VMA
4223 #undef IS_CONTAINED_BY_LMA
4224 #undef IS_COREFILE_NOTE
4225 #undef IS_SOLARIS_PT_INTERP
4226 #undef INCLUDE_SECTION_IN_SEGMENT
4227 #undef SEGMENT_AFTER_SEGMENT
4228 #undef SEGMENT_OVERLAPS
4229 return true;
4232 /* Copy private section information. This copies over the entsize
4233 field, and sometimes the info field. */
4235 boolean
4236 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4237 bfd *ibfd;
4238 asection *isec;
4239 bfd *obfd;
4240 asection *osec;
4242 Elf_Internal_Shdr *ihdr, *ohdr;
4244 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4245 || obfd->xvec->flavour != bfd_target_elf_flavour)
4246 return true;
4248 /* Copy over private BFD data if it has not already been copied.
4249 This must be done here, rather than in the copy_private_bfd_data
4250 entry point, because the latter is called after the section
4251 contents have been set, which means that the program headers have
4252 already been worked out. */
4253 if (elf_tdata (obfd)->segment_map == NULL
4254 && elf_tdata (ibfd)->phdr != NULL)
4256 asection *s;
4258 /* Only set up the segments if there are no more SEC_ALLOC
4259 sections. FIXME: This won't do the right thing if objcopy is
4260 used to remove the last SEC_ALLOC section, since objcopy
4261 won't call this routine in that case. */
4262 for (s = isec->next; s != NULL; s = s->next)
4263 if ((s->flags & SEC_ALLOC) != 0)
4264 break;
4265 if (s == NULL)
4267 if (! copy_private_bfd_data (ibfd, obfd))
4268 return false;
4272 ihdr = &elf_section_data (isec)->this_hdr;
4273 ohdr = &elf_section_data (osec)->this_hdr;
4275 ohdr->sh_entsize = ihdr->sh_entsize;
4277 if (ihdr->sh_type == SHT_SYMTAB
4278 || ihdr->sh_type == SHT_DYNSYM
4279 || ihdr->sh_type == SHT_GNU_verneed
4280 || ihdr->sh_type == SHT_GNU_verdef)
4281 ohdr->sh_info = ihdr->sh_info;
4283 elf_section_data (osec)->use_rela_p
4284 = elf_section_data (isec)->use_rela_p;
4286 return true;
4289 /* Copy private symbol information. If this symbol is in a section
4290 which we did not map into a BFD section, try to map the section
4291 index correctly. We use special macro definitions for the mapped
4292 section indices; these definitions are interpreted by the
4293 swap_out_syms function. */
4295 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4296 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4297 #define MAP_STRTAB (SHN_LORESERVE - 3)
4298 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4300 boolean
4301 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4302 bfd *ibfd;
4303 asymbol *isymarg;
4304 bfd *obfd;
4305 asymbol *osymarg;
4307 elf_symbol_type *isym, *osym;
4309 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4310 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4311 return true;
4313 isym = elf_symbol_from (ibfd, isymarg);
4314 osym = elf_symbol_from (obfd, osymarg);
4316 if (isym != NULL
4317 && osym != NULL
4318 && bfd_is_abs_section (isym->symbol.section))
4320 unsigned int shndx;
4322 shndx = isym->internal_elf_sym.st_shndx;
4323 if (shndx == elf_onesymtab (ibfd))
4324 shndx = MAP_ONESYMTAB;
4325 else if (shndx == elf_dynsymtab (ibfd))
4326 shndx = MAP_DYNSYMTAB;
4327 else if (shndx == elf_tdata (ibfd)->strtab_section)
4328 shndx = MAP_STRTAB;
4329 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4330 shndx = MAP_SHSTRTAB;
4331 osym->internal_elf_sym.st_shndx = shndx;
4334 return true;
4337 /* Swap out the symbols. */
4339 static boolean
4340 swap_out_syms (abfd, sttp, relocatable_p)
4341 bfd *abfd;
4342 struct bfd_strtab_hash **sttp;
4343 int relocatable_p;
4345 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4347 if (!elf_map_symbols (abfd))
4348 return false;
4350 /* Dump out the symtabs. */
4352 int symcount = bfd_get_symcount (abfd);
4353 asymbol **syms = bfd_get_outsymbols (abfd);
4354 struct bfd_strtab_hash *stt;
4355 Elf_Internal_Shdr *symtab_hdr;
4356 Elf_Internal_Shdr *symstrtab_hdr;
4357 char *outbound_syms;
4358 int idx;
4360 stt = _bfd_elf_stringtab_init ();
4361 if (stt == NULL)
4362 return false;
4364 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4365 symtab_hdr->sh_type = SHT_SYMTAB;
4366 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4367 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4368 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4369 symtab_hdr->sh_addralign = bed->s->file_align;
4371 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4372 symstrtab_hdr->sh_type = SHT_STRTAB;
4374 outbound_syms = bfd_alloc (abfd,
4375 (1 + symcount) * bed->s->sizeof_sym);
4376 if (outbound_syms == NULL)
4377 return false;
4378 symtab_hdr->contents = (PTR) outbound_syms;
4380 /* now generate the data (for "contents") */
4382 /* Fill in zeroth symbol and swap it out. */
4383 Elf_Internal_Sym sym;
4384 sym.st_name = 0;
4385 sym.st_value = 0;
4386 sym.st_size = 0;
4387 sym.st_info = 0;
4388 sym.st_other = 0;
4389 sym.st_shndx = SHN_UNDEF;
4390 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4391 outbound_syms += bed->s->sizeof_sym;
4393 for (idx = 0; idx < symcount; idx++)
4395 Elf_Internal_Sym sym;
4396 bfd_vma value = syms[idx]->value;
4397 elf_symbol_type *type_ptr;
4398 flagword flags = syms[idx]->flags;
4399 int type;
4401 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM)
4403 /* Local section symbols have no name. */
4404 sym.st_name = 0;
4406 else
4408 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4409 syms[idx]->name,
4410 true, false);
4411 if (sym.st_name == (unsigned long) -1)
4412 return false;
4415 type_ptr = elf_symbol_from (abfd, syms[idx]);
4417 if ((flags & BSF_SECTION_SYM) == 0
4418 && bfd_is_com_section (syms[idx]->section))
4420 /* ELF common symbols put the alignment into the `value' field,
4421 and the size into the `size' field. This is backwards from
4422 how BFD handles it, so reverse it here. */
4423 sym.st_size = value;
4424 if (type_ptr == NULL
4425 || type_ptr->internal_elf_sym.st_value == 0)
4426 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4427 else
4428 sym.st_value = type_ptr->internal_elf_sym.st_value;
4429 sym.st_shndx = _bfd_elf_section_from_bfd_section
4430 (abfd, syms[idx]->section);
4432 else
4434 asection *sec = syms[idx]->section;
4435 int shndx;
4437 if (sec->output_section)
4439 value += sec->output_offset;
4440 sec = sec->output_section;
4442 /* Don't add in the section vma for relocatable output. */
4443 if (! relocatable_p)
4444 value += sec->vma;
4445 sym.st_value = value;
4446 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4448 if (bfd_is_abs_section (sec)
4449 && type_ptr != NULL
4450 && type_ptr->internal_elf_sym.st_shndx != 0)
4452 /* This symbol is in a real ELF section which we did
4453 not create as a BFD section. Undo the mapping done
4454 by copy_private_symbol_data. */
4455 shndx = type_ptr->internal_elf_sym.st_shndx;
4456 switch (shndx)
4458 case MAP_ONESYMTAB:
4459 shndx = elf_onesymtab (abfd);
4460 break;
4461 case MAP_DYNSYMTAB:
4462 shndx = elf_dynsymtab (abfd);
4463 break;
4464 case MAP_STRTAB:
4465 shndx = elf_tdata (abfd)->strtab_section;
4466 break;
4467 case MAP_SHSTRTAB:
4468 shndx = elf_tdata (abfd)->shstrtab_section;
4469 break;
4470 default:
4471 break;
4474 else
4476 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4478 if (shndx == -1)
4480 asection *sec2;
4482 /* Writing this would be a hell of a lot easier if
4483 we had some decent documentation on bfd, and
4484 knew what to expect of the library, and what to
4485 demand of applications. For example, it
4486 appears that `objcopy' might not set the
4487 section of a symbol to be a section that is
4488 actually in the output file. */
4489 sec2 = bfd_get_section_by_name (abfd, sec->name);
4490 BFD_ASSERT (sec2 != 0);
4491 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4492 BFD_ASSERT (shndx != -1);
4496 sym.st_shndx = shndx;
4499 if ((flags & BSF_FUNCTION) != 0)
4500 type = STT_FUNC;
4501 else if ((flags & BSF_OBJECT) != 0)
4502 type = STT_OBJECT;
4503 else
4504 type = STT_NOTYPE;
4506 /* Processor-specific types */
4507 if (type_ptr != NULL
4508 && bed->elf_backend_get_symbol_type)
4509 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4511 if (flags & BSF_SECTION_SYM)
4513 if (flags & BSF_GLOBAL)
4514 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION);
4515 else
4516 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4518 else if (bfd_is_com_section (syms[idx]->section))
4519 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4520 else if (bfd_is_und_section (syms[idx]->section))
4521 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4522 ? STB_WEAK
4523 : STB_GLOBAL),
4524 type);
4525 else if (flags & BSF_FILE)
4526 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4527 else
4529 int bind = STB_LOCAL;
4531 if (flags & BSF_LOCAL)
4532 bind = STB_LOCAL;
4533 else if (flags & BSF_WEAK)
4534 bind = STB_WEAK;
4535 else if (flags & BSF_GLOBAL)
4536 bind = STB_GLOBAL;
4538 sym.st_info = ELF_ST_INFO (bind, type);
4541 if (type_ptr != NULL)
4542 sym.st_other = type_ptr->internal_elf_sym.st_other;
4543 else
4544 sym.st_other = 0;
4546 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4547 outbound_syms += bed->s->sizeof_sym;
4550 *sttp = stt;
4551 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4552 symstrtab_hdr->sh_type = SHT_STRTAB;
4554 symstrtab_hdr->sh_flags = 0;
4555 symstrtab_hdr->sh_addr = 0;
4556 symstrtab_hdr->sh_entsize = 0;
4557 symstrtab_hdr->sh_link = 0;
4558 symstrtab_hdr->sh_info = 0;
4559 symstrtab_hdr->sh_addralign = 1;
4562 return true;
4565 /* Return the number of bytes required to hold the symtab vector.
4567 Note that we base it on the count plus 1, since we will null terminate
4568 the vector allocated based on this size. However, the ELF symbol table
4569 always has a dummy entry as symbol #0, so it ends up even. */
4571 long
4572 _bfd_elf_get_symtab_upper_bound (abfd)
4573 bfd *abfd;
4575 long symcount;
4576 long symtab_size;
4577 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4579 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4580 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4582 return symtab_size;
4585 long
4586 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4587 bfd *abfd;
4589 long symcount;
4590 long symtab_size;
4591 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4593 if (elf_dynsymtab (abfd) == 0)
4595 bfd_set_error (bfd_error_invalid_operation);
4596 return -1;
4599 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4600 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4602 return symtab_size;
4605 long
4606 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4607 bfd *abfd ATTRIBUTE_UNUSED;
4608 sec_ptr asect;
4610 return (asect->reloc_count + 1) * sizeof (arelent *);
4613 /* Canonicalize the relocs. */
4615 long
4616 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4617 bfd *abfd;
4618 sec_ptr section;
4619 arelent **relptr;
4620 asymbol **symbols;
4622 arelent *tblptr;
4623 unsigned int i;
4625 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4626 section,
4627 symbols,
4628 false))
4629 return -1;
4631 tblptr = section->relocation;
4632 for (i = 0; i < section->reloc_count; i++)
4633 *relptr++ = tblptr++;
4635 *relptr = NULL;
4637 return section->reloc_count;
4640 long
4641 _bfd_elf_get_symtab (abfd, alocation)
4642 bfd *abfd;
4643 asymbol **alocation;
4645 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4646 (abfd, alocation, false);
4648 if (symcount >= 0)
4649 bfd_get_symcount (abfd) = symcount;
4650 return symcount;
4653 long
4654 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4655 bfd *abfd;
4656 asymbol **alocation;
4658 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4659 (abfd, alocation, true);
4662 /* Return the size required for the dynamic reloc entries. Any
4663 section that was actually installed in the BFD, and has type
4664 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4665 considered to be a dynamic reloc section. */
4667 long
4668 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4669 bfd *abfd;
4671 long ret;
4672 asection *s;
4674 if (elf_dynsymtab (abfd) == 0)
4676 bfd_set_error (bfd_error_invalid_operation);
4677 return -1;
4680 ret = sizeof (arelent *);
4681 for (s = abfd->sections; s != NULL; s = s->next)
4682 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4683 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4684 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4685 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4686 * sizeof (arelent *));
4688 return ret;
4691 /* Canonicalize the dynamic relocation entries. Note that we return
4692 the dynamic relocations as a single block, although they are
4693 actually associated with particular sections; the interface, which
4694 was designed for SunOS style shared libraries, expects that there
4695 is only one set of dynamic relocs. Any section that was actually
4696 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4697 the dynamic symbol table, is considered to be a dynamic reloc
4698 section. */
4700 long
4701 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4702 bfd *abfd;
4703 arelent **storage;
4704 asymbol **syms;
4706 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4707 asection *s;
4708 long ret;
4710 if (elf_dynsymtab (abfd) == 0)
4712 bfd_set_error (bfd_error_invalid_operation);
4713 return -1;
4716 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4717 ret = 0;
4718 for (s = abfd->sections; s != NULL; s = s->next)
4720 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4721 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4722 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4724 arelent *p;
4725 long count, i;
4727 if (! (*slurp_relocs) (abfd, s, syms, true))
4728 return -1;
4729 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4730 p = s->relocation;
4731 for (i = 0; i < count; i++)
4732 *storage++ = p++;
4733 ret += count;
4737 *storage = NULL;
4739 return ret;
4742 /* Read in the version information. */
4744 boolean
4745 _bfd_elf_slurp_version_tables (abfd)
4746 bfd *abfd;
4748 bfd_byte *contents = NULL;
4750 if (elf_dynverdef (abfd) != 0)
4752 Elf_Internal_Shdr *hdr;
4753 Elf_External_Verdef *everdef;
4754 Elf_Internal_Verdef *iverdef;
4755 Elf_Internal_Verdef *iverdefarr;
4756 Elf_Internal_Verdef iverdefmem;
4757 unsigned int i;
4758 unsigned int maxidx;
4760 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4762 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4763 if (contents == NULL)
4764 goto error_return;
4765 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4766 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4767 goto error_return;
4769 /* We know the number of entries in the section but not the maximum
4770 index. Therefore we have to run through all entries and find
4771 the maximum. */
4772 everdef = (Elf_External_Verdef *) contents;
4773 maxidx = 0;
4774 for (i = 0; i < hdr->sh_info; ++i)
4776 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4778 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx)
4779 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION);
4781 everdef = ((Elf_External_Verdef *)
4782 ((bfd_byte *) everdef + iverdefmem.vd_next));
4785 elf_tdata (abfd)->verdef =
4786 ((Elf_Internal_Verdef *)
4787 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4788 if (elf_tdata (abfd)->verdef == NULL)
4789 goto error_return;
4791 elf_tdata (abfd)->cverdefs = maxidx;
4793 everdef = (Elf_External_Verdef *) contents;
4794 iverdefarr = elf_tdata (abfd)->verdef;
4795 for (i = 0; i < hdr->sh_info; i++)
4797 Elf_External_Verdaux *everdaux;
4798 Elf_Internal_Verdaux *iverdaux;
4799 unsigned int j;
4801 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4803 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4804 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4806 iverdef->vd_bfd = abfd;
4808 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4809 bfd_alloc (abfd,
4810 (iverdef->vd_cnt
4811 * sizeof (Elf_Internal_Verdaux))));
4812 if (iverdef->vd_auxptr == NULL)
4813 goto error_return;
4815 everdaux = ((Elf_External_Verdaux *)
4816 ((bfd_byte *) everdef + iverdef->vd_aux));
4817 iverdaux = iverdef->vd_auxptr;
4818 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4820 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4822 iverdaux->vda_nodename =
4823 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4824 iverdaux->vda_name);
4825 if (iverdaux->vda_nodename == NULL)
4826 goto error_return;
4828 if (j + 1 < iverdef->vd_cnt)
4829 iverdaux->vda_nextptr = iverdaux + 1;
4830 else
4831 iverdaux->vda_nextptr = NULL;
4833 everdaux = ((Elf_External_Verdaux *)
4834 ((bfd_byte *) everdaux + iverdaux->vda_next));
4837 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4839 if (i + 1 < hdr->sh_info)
4840 iverdef->vd_nextdef = iverdef + 1;
4841 else
4842 iverdef->vd_nextdef = NULL;
4844 everdef = ((Elf_External_Verdef *)
4845 ((bfd_byte *) everdef + iverdef->vd_next));
4848 free (contents);
4849 contents = NULL;
4852 if (elf_dynverref (abfd) != 0)
4854 Elf_Internal_Shdr *hdr;
4855 Elf_External_Verneed *everneed;
4856 Elf_Internal_Verneed *iverneed;
4857 unsigned int i;
4859 hdr = &elf_tdata (abfd)->dynverref_hdr;
4861 elf_tdata (abfd)->verref =
4862 ((Elf_Internal_Verneed *)
4863 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4864 if (elf_tdata (abfd)->verref == NULL)
4865 goto error_return;
4867 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4869 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4870 if (contents == NULL)
4871 goto error_return;
4872 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4873 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4874 goto error_return;
4876 everneed = (Elf_External_Verneed *) contents;
4877 iverneed = elf_tdata (abfd)->verref;
4878 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4880 Elf_External_Vernaux *evernaux;
4881 Elf_Internal_Vernaux *ivernaux;
4882 unsigned int j;
4884 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4886 iverneed->vn_bfd = abfd;
4888 iverneed->vn_filename =
4889 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4890 iverneed->vn_file);
4891 if (iverneed->vn_filename == NULL)
4892 goto error_return;
4894 iverneed->vn_auxptr =
4895 ((Elf_Internal_Vernaux *)
4896 bfd_alloc (abfd,
4897 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4899 evernaux = ((Elf_External_Vernaux *)
4900 ((bfd_byte *) everneed + iverneed->vn_aux));
4901 ivernaux = iverneed->vn_auxptr;
4902 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4904 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4906 ivernaux->vna_nodename =
4907 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4908 ivernaux->vna_name);
4909 if (ivernaux->vna_nodename == NULL)
4910 goto error_return;
4912 if (j + 1 < iverneed->vn_cnt)
4913 ivernaux->vna_nextptr = ivernaux + 1;
4914 else
4915 ivernaux->vna_nextptr = NULL;
4917 evernaux = ((Elf_External_Vernaux *)
4918 ((bfd_byte *) evernaux + ivernaux->vna_next));
4921 if (i + 1 < hdr->sh_info)
4922 iverneed->vn_nextref = iverneed + 1;
4923 else
4924 iverneed->vn_nextref = NULL;
4926 everneed = ((Elf_External_Verneed *)
4927 ((bfd_byte *) everneed + iverneed->vn_next));
4930 free (contents);
4931 contents = NULL;
4934 return true;
4936 error_return:
4937 if (contents == NULL)
4938 free (contents);
4939 return false;
4942 asymbol *
4943 _bfd_elf_make_empty_symbol (abfd)
4944 bfd *abfd;
4946 elf_symbol_type *newsym;
4948 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4949 if (!newsym)
4950 return NULL;
4951 else
4953 newsym->symbol.the_bfd = abfd;
4954 return &newsym->symbol;
4958 void
4959 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4960 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4961 asymbol *symbol;
4962 symbol_info *ret;
4964 bfd_symbol_info (symbol, ret);
4967 /* Return whether a symbol name implies a local symbol. Most targets
4968 use this function for the is_local_label_name entry point, but some
4969 override it. */
4971 boolean
4972 _bfd_elf_is_local_label_name (abfd, name)
4973 bfd *abfd ATTRIBUTE_UNUSED;
4974 const char *name;
4976 /* Normal local symbols start with ``.L''. */
4977 if (name[0] == '.' && name[1] == 'L')
4978 return true;
4980 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4981 DWARF debugging symbols starting with ``..''. */
4982 if (name[0] == '.' && name[1] == '.')
4983 return true;
4985 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4986 emitting DWARF debugging output. I suspect this is actually a
4987 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4988 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4989 underscore to be emitted on some ELF targets). For ease of use,
4990 we treat such symbols as local. */
4991 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4992 return true;
4994 return false;
4997 alent *
4998 _bfd_elf_get_lineno (ignore_abfd, symbol)
4999 bfd *ignore_abfd ATTRIBUTE_UNUSED;
5000 asymbol *symbol ATTRIBUTE_UNUSED;
5002 abort ();
5003 return NULL;
5006 boolean
5007 _bfd_elf_set_arch_mach (abfd, arch, machine)
5008 bfd *abfd;
5009 enum bfd_architecture arch;
5010 unsigned long machine;
5012 /* If this isn't the right architecture for this backend, and this
5013 isn't the generic backend, fail. */
5014 if (arch != get_elf_backend_data (abfd)->arch
5015 && arch != bfd_arch_unknown
5016 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
5017 return false;
5019 return bfd_default_set_arch_mach (abfd, arch, machine);
5022 /* Find the function to a particular section and offset,
5023 for error reporting. */
5025 static boolean
5026 elf_find_function (abfd, section, symbols, offset,
5027 filename_ptr, functionname_ptr)
5028 bfd *abfd ATTRIBUTE_UNUSED;
5029 asection *section;
5030 asymbol **symbols;
5031 bfd_vma offset;
5032 const char **filename_ptr;
5033 const char **functionname_ptr;
5035 const char *filename;
5036 asymbol *func;
5037 bfd_vma low_func;
5038 asymbol **p;
5040 filename = NULL;
5041 func = NULL;
5042 low_func = 0;
5044 for (p = symbols; *p != NULL; p++)
5046 elf_symbol_type *q;
5048 q = (elf_symbol_type *) *p;
5050 if (bfd_get_section (&q->symbol) != section)
5051 continue;
5053 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5055 default:
5056 break;
5057 case STT_FILE:
5058 filename = bfd_asymbol_name (&q->symbol);
5059 break;
5060 case STT_NOTYPE:
5061 case STT_FUNC:
5062 if (q->symbol.section == section
5063 && q->symbol.value >= low_func
5064 && q->symbol.value <= offset)
5066 func = (asymbol *) q;
5067 low_func = q->symbol.value;
5069 break;
5073 if (func == NULL)
5074 return false;
5076 if (filename_ptr)
5077 *filename_ptr = filename;
5078 if (functionname_ptr)
5079 *functionname_ptr = bfd_asymbol_name (func);
5081 return true;
5084 /* Find the nearest line to a particular section and offset,
5085 for error reporting. */
5087 boolean
5088 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5089 filename_ptr, functionname_ptr, line_ptr)
5090 bfd *abfd;
5091 asection *section;
5092 asymbol **symbols;
5093 bfd_vma offset;
5094 const char **filename_ptr;
5095 const char **functionname_ptr;
5096 unsigned int *line_ptr;
5098 boolean found;
5100 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5101 filename_ptr, functionname_ptr,
5102 line_ptr))
5104 if (!*functionname_ptr)
5105 elf_find_function (abfd, section, symbols, offset,
5106 *filename_ptr ? NULL : filename_ptr,
5107 functionname_ptr);
5109 return true;
5112 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5113 filename_ptr, functionname_ptr,
5114 line_ptr, 0,
5115 &elf_tdata (abfd)->dwarf2_find_line_info))
5117 if (!*functionname_ptr)
5118 elf_find_function (abfd, section, symbols, offset,
5119 *filename_ptr ? NULL : filename_ptr,
5120 functionname_ptr);
5122 return true;
5125 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5126 &found, filename_ptr,
5127 functionname_ptr, line_ptr,
5128 &elf_tdata (abfd)->line_info))
5129 return false;
5130 if (found)
5131 return true;
5133 if (symbols == NULL)
5134 return false;
5136 if (! elf_find_function (abfd, section, symbols, offset,
5137 filename_ptr, functionname_ptr))
5138 return false;
5140 *line_ptr = 0;
5141 return true;
5145 _bfd_elf_sizeof_headers (abfd, reloc)
5146 bfd *abfd;
5147 boolean reloc;
5149 int ret;
5151 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5152 if (! reloc)
5153 ret += get_program_header_size (abfd);
5154 return ret;
5157 boolean
5158 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5159 bfd *abfd;
5160 sec_ptr section;
5161 PTR location;
5162 file_ptr offset;
5163 bfd_size_type count;
5165 Elf_Internal_Shdr *hdr;
5167 if (! abfd->output_has_begun
5168 && ! _bfd_elf_compute_section_file_positions
5169 (abfd, (struct bfd_link_info *) NULL))
5170 return false;
5172 hdr = &elf_section_data (section)->this_hdr;
5174 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5175 return false;
5176 if (bfd_write (location, 1, count, abfd) != count)
5177 return false;
5179 return true;
5182 void
5183 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5184 bfd *abfd ATTRIBUTE_UNUSED;
5185 arelent *cache_ptr ATTRIBUTE_UNUSED;
5186 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5188 abort ();
5191 #if 0
5192 void
5193 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5194 bfd *abfd;
5195 arelent *cache_ptr;
5196 Elf_Internal_Rel *dst;
5198 abort ();
5200 #endif
5202 /* Try to convert a non-ELF reloc into an ELF one. */
5204 boolean
5205 _bfd_elf_validate_reloc (abfd, areloc)
5206 bfd *abfd;
5207 arelent *areloc;
5209 /* Check whether we really have an ELF howto. */
5211 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5213 bfd_reloc_code_real_type code;
5214 reloc_howto_type *howto;
5216 /* Alien reloc: Try to determine its type to replace it with an
5217 equivalent ELF reloc. */
5219 if (areloc->howto->pc_relative)
5221 switch (areloc->howto->bitsize)
5223 case 8:
5224 code = BFD_RELOC_8_PCREL;
5225 break;
5226 case 12:
5227 code = BFD_RELOC_12_PCREL;
5228 break;
5229 case 16:
5230 code = BFD_RELOC_16_PCREL;
5231 break;
5232 case 24:
5233 code = BFD_RELOC_24_PCREL;
5234 break;
5235 case 32:
5236 code = BFD_RELOC_32_PCREL;
5237 break;
5238 case 64:
5239 code = BFD_RELOC_64_PCREL;
5240 break;
5241 default:
5242 goto fail;
5245 howto = bfd_reloc_type_lookup (abfd, code);
5247 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5249 if (howto->pcrel_offset)
5250 areloc->addend += areloc->address;
5251 else
5252 areloc->addend -= areloc->address; /* addend is unsigned!! */
5255 else
5257 switch (areloc->howto->bitsize)
5259 case 8:
5260 code = BFD_RELOC_8;
5261 break;
5262 case 14:
5263 code = BFD_RELOC_14;
5264 break;
5265 case 16:
5266 code = BFD_RELOC_16;
5267 break;
5268 case 26:
5269 code = BFD_RELOC_26;
5270 break;
5271 case 32:
5272 code = BFD_RELOC_32;
5273 break;
5274 case 64:
5275 code = BFD_RELOC_64;
5276 break;
5277 default:
5278 goto fail;
5281 howto = bfd_reloc_type_lookup (abfd, code);
5284 if (howto)
5285 areloc->howto = howto;
5286 else
5287 goto fail;
5290 return true;
5292 fail:
5293 (*_bfd_error_handler)
5294 (_("%s: unsupported relocation type %s"),
5295 bfd_get_filename (abfd), areloc->howto->name);
5296 bfd_set_error (bfd_error_bad_value);
5297 return false;
5300 boolean
5301 _bfd_elf_close_and_cleanup (abfd)
5302 bfd *abfd;
5304 if (bfd_get_format (abfd) == bfd_object)
5306 if (elf_shstrtab (abfd) != NULL)
5307 _bfd_stringtab_free (elf_shstrtab (abfd));
5310 return _bfd_generic_close_and_cleanup (abfd);
5313 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5314 in the relocation's offset. Thus we cannot allow any sort of sanity
5315 range-checking to interfere. There is nothing else to do in processing
5316 this reloc. */
5318 bfd_reloc_status_type
5319 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5320 bfd *abfd ATTRIBUTE_UNUSED;
5321 arelent *re ATTRIBUTE_UNUSED;
5322 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5323 PTR data ATTRIBUTE_UNUSED;
5324 asection *is ATTRIBUTE_UNUSED;
5325 bfd *obfd ATTRIBUTE_UNUSED;
5326 char **errmsg ATTRIBUTE_UNUSED;
5328 return bfd_reloc_ok;
5331 /* Elf core file support. Much of this only works on native
5332 toolchains, since we rely on knowing the
5333 machine-dependent procfs structure in order to pick
5334 out details about the corefile. */
5336 #ifdef HAVE_SYS_PROCFS_H
5337 # include <sys/procfs.h>
5338 #endif
5340 /* Define offsetof for those systems which lack it. */
5342 #ifndef offsetof
5343 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5344 #endif
5346 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5348 static int
5349 elfcore_make_pid (abfd)
5350 bfd *abfd;
5352 return ((elf_tdata (abfd)->core_lwpid << 16)
5353 + (elf_tdata (abfd)->core_pid));
5356 /* If there isn't a section called NAME, make one, using
5357 data from SECT. Note, this function will generate a
5358 reference to NAME, so you shouldn't deallocate or
5359 overwrite it. */
5361 static boolean
5362 elfcore_maybe_make_sect (abfd, name, sect)
5363 bfd *abfd;
5364 char *name;
5365 asection *sect;
5367 asection *sect2;
5369 if (bfd_get_section_by_name (abfd, name) != NULL)
5370 return true;
5372 sect2 = bfd_make_section (abfd, name);
5373 if (sect2 == NULL)
5374 return false;
5376 sect2->_raw_size = sect->_raw_size;
5377 sect2->filepos = sect->filepos;
5378 sect2->flags = sect->flags;
5379 sect2->alignment_power = sect->alignment_power;
5380 return true;
5383 /* Create a pseudosection containing SIZE bytes at FILEPOS. This
5384 actually creates up to two pseudosections:
5385 - For the single-threaded case, a section named NAME, unless
5386 such a section already exists.
5387 - For the multi-threaded case, a section named "NAME/PID", where
5388 PID is elfcore_make_pid (abfd).
5389 Both pseudosections have identical contents. */
5390 boolean
5391 _bfd_elfcore_make_pseudosection (abfd, name, size, filepos)
5392 bfd *abfd;
5393 char *name;
5394 int size;
5395 int filepos;
5397 char buf[100];
5398 char *threaded_name;
5399 asection *sect;
5401 /* Build the section name. */
5403 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5404 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5405 if (threaded_name == NULL)
5406 return false;
5407 strcpy (threaded_name, buf);
5409 sect = bfd_make_section (abfd, threaded_name);
5410 if (sect == NULL)
5411 return false;
5412 sect->_raw_size = size;
5413 sect->filepos = filepos;
5414 sect->flags = SEC_HAS_CONTENTS;
5415 sect->alignment_power = 2;
5417 return elfcore_maybe_make_sect (abfd, name, sect);
5420 /* prstatus_t exists on:
5421 solaris 2.5+
5422 linux 2.[01] + glibc
5423 unixware 4.2
5426 #if defined (HAVE_PRSTATUS_T)
5427 static boolean
5428 elfcore_grok_prstatus (abfd, note)
5429 bfd *abfd;
5430 Elf_Internal_Note *note;
5432 int raw_size;
5433 int offset;
5435 if (note->descsz == sizeof (prstatus_t))
5437 prstatus_t prstat;
5439 raw_size = sizeof (prstat.pr_reg);
5440 offset = offsetof (prstatus_t, pr_reg);
5441 memcpy (&prstat, note->descdata, sizeof (prstat));
5443 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5444 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5446 /* pr_who exists on:
5447 solaris 2.5+
5448 unixware 4.2
5449 pr_who doesn't exist on:
5450 linux 2.[01]
5452 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5453 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5454 #endif
5456 #if defined (HAVE_PRSTATUS32_T)
5457 else if (note->descsz == sizeof (prstatus32_t))
5459 /* 64-bit host, 32-bit corefile */
5460 prstatus32_t prstat;
5462 raw_size = sizeof (prstat.pr_reg);
5463 offset = offsetof (prstatus32_t, pr_reg);
5464 memcpy (&prstat, note->descdata, sizeof (prstat));
5466 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5467 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5469 /* pr_who exists on:
5470 solaris 2.5+
5471 unixware 4.2
5472 pr_who doesn't exist on:
5473 linux 2.[01]
5475 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5476 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5477 #endif
5479 #endif /* HAVE_PRSTATUS32_T */
5480 else
5482 /* Fail - we don't know how to handle any other
5483 note size (ie. data object type). */
5484 return true;
5487 /* Make a ".reg/999" section and a ".reg" section. */
5488 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5489 raw_size, note->descpos + offset);
5491 #endif /* defined (HAVE_PRSTATUS_T) */
5493 /* Create a pseudosection containing the exact contents of NOTE. */
5494 static boolean
5495 elfcore_make_note_pseudosection (abfd, name, note)
5496 bfd *abfd;
5497 char *name;
5498 Elf_Internal_Note *note;
5500 return _bfd_elfcore_make_pseudosection (abfd, name,
5501 note->descsz, note->descpos);
5504 /* There isn't a consistent prfpregset_t across platforms,
5505 but it doesn't matter, because we don't have to pick this
5506 data structure apart. */
5508 static boolean
5509 elfcore_grok_prfpreg (abfd, note)
5510 bfd *abfd;
5511 Elf_Internal_Note *note;
5513 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5516 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5517 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5518 literally. */
5520 static boolean
5521 elfcore_grok_prxfpreg (abfd, note)
5522 bfd *abfd;
5523 Elf_Internal_Note *note;
5525 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5528 #if defined (HAVE_PRPSINFO_T)
5529 typedef prpsinfo_t elfcore_psinfo_t;
5530 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5531 typedef prpsinfo32_t elfcore_psinfo32_t;
5532 #endif
5533 #endif
5535 #if defined (HAVE_PSINFO_T)
5536 typedef psinfo_t elfcore_psinfo_t;
5537 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5538 typedef psinfo32_t elfcore_psinfo32_t;
5539 #endif
5540 #endif
5542 /* return a malloc'ed copy of a string at START which is at
5543 most MAX bytes long, possibly without a terminating '\0'.
5544 the copy will always have a terminating '\0'. */
5546 char *
5547 _bfd_elfcore_strndup (abfd, start, max)
5548 bfd *abfd;
5549 char *start;
5550 int max;
5552 char *dup;
5553 char *end = memchr (start, '\0', max);
5554 int len;
5556 if (end == NULL)
5557 len = max;
5558 else
5559 len = end - start;
5561 dup = bfd_alloc (abfd, len + 1);
5562 if (dup == NULL)
5563 return NULL;
5565 memcpy (dup, start, len);
5566 dup[len] = '\0';
5568 return dup;
5571 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5573 static boolean
5574 elfcore_grok_psinfo (abfd, note)
5575 bfd *abfd;
5576 Elf_Internal_Note *note;
5578 if (note->descsz == sizeof (elfcore_psinfo_t))
5580 elfcore_psinfo_t psinfo;
5582 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5584 elf_tdata (abfd)->core_program
5585 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5586 sizeof (psinfo.pr_fname));
5588 elf_tdata (abfd)->core_command
5589 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5590 sizeof (psinfo.pr_psargs));
5592 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5593 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5595 /* 64-bit host, 32-bit corefile */
5596 elfcore_psinfo32_t psinfo;
5598 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5600 elf_tdata (abfd)->core_program
5601 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname,
5602 sizeof (psinfo.pr_fname));
5604 elf_tdata (abfd)->core_command
5605 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs,
5606 sizeof (psinfo.pr_psargs));
5608 #endif
5610 else
5612 /* Fail - we don't know how to handle any other
5613 note size (ie. data object type). */
5614 return true;
5617 /* Note that for some reason, a spurious space is tacked
5618 onto the end of the args in some (at least one anyway)
5619 implementations, so strip it off if it exists. */
5622 char *command = elf_tdata (abfd)->core_command;
5623 int n = strlen (command);
5625 if (0 < n && command[n - 1] == ' ')
5626 command[n - 1] = '\0';
5629 return true;
5631 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5633 #if defined (HAVE_PSTATUS_T)
5634 static boolean
5635 elfcore_grok_pstatus (abfd, note)
5636 bfd *abfd;
5637 Elf_Internal_Note *note;
5639 if (note->descsz == sizeof (pstatus_t)
5640 #if defined (HAVE_PXSTATUS_T)
5641 || note->descsz == sizeof (pxstatus_t)
5642 #endif
5645 pstatus_t pstat;
5647 memcpy (&pstat, note->descdata, sizeof (pstat));
5649 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5651 #if defined (HAVE_PSTATUS32_T)
5652 else if (note->descsz == sizeof (pstatus32_t))
5654 /* 64-bit host, 32-bit corefile */
5655 pstatus32_t pstat;
5657 memcpy (&pstat, note->descdata, sizeof (pstat));
5659 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5661 #endif
5662 /* Could grab some more details from the "representative"
5663 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5664 NT_LWPSTATUS note, presumably. */
5666 return true;
5668 #endif /* defined (HAVE_PSTATUS_T) */
5670 #if defined (HAVE_LWPSTATUS_T)
5671 static boolean
5672 elfcore_grok_lwpstatus (abfd, note)
5673 bfd *abfd;
5674 Elf_Internal_Note *note;
5676 lwpstatus_t lwpstat;
5677 char buf[100];
5678 char *name;
5679 asection *sect;
5681 if (note->descsz != sizeof (lwpstat)
5682 #if defined (HAVE_LWPXSTATUS_T)
5683 && note->descsz != sizeof (lwpxstatus_t)
5684 #endif
5686 return true;
5688 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5690 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5691 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5693 /* Make a ".reg/999" section. */
5695 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5696 name = bfd_alloc (abfd, strlen (buf) + 1);
5697 if (name == NULL)
5698 return false;
5699 strcpy (name, buf);
5701 sect = bfd_make_section (abfd, name);
5702 if (sect == NULL)
5703 return false;
5705 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5706 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5707 sect->filepos = note->descpos
5708 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5709 #endif
5711 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5712 sect->_raw_size = sizeof (lwpstat.pr_reg);
5713 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5714 #endif
5716 sect->flags = SEC_HAS_CONTENTS;
5717 sect->alignment_power = 2;
5719 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5720 return false;
5722 /* Make a ".reg2/999" section */
5724 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5725 name = bfd_alloc (abfd, strlen (buf) + 1);
5726 if (name == NULL)
5727 return false;
5728 strcpy (name, buf);
5730 sect = bfd_make_section (abfd, name);
5731 if (sect == NULL)
5732 return false;
5734 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5735 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5736 sect->filepos = note->descpos
5737 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5738 #endif
5740 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5741 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5742 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5743 #endif
5745 sect->flags = SEC_HAS_CONTENTS;
5746 sect->alignment_power = 2;
5748 return elfcore_maybe_make_sect (abfd, ".reg2", sect);
5750 #endif /* defined (HAVE_LWPSTATUS_T) */
5752 #if defined (HAVE_WIN32_PSTATUS_T)
5753 static boolean
5754 elfcore_grok_win32pstatus (abfd, note)
5755 bfd *abfd;
5756 Elf_Internal_Note *note;
5758 char buf[30];
5759 char *name;
5760 asection *sect;
5761 win32_pstatus_t pstatus;
5763 if (note->descsz < sizeof (pstatus))
5764 return true;
5766 memcpy (&pstatus, note->descdata, note->descsz);
5768 switch (pstatus.data_type)
5770 case NOTE_INFO_PROCESS:
5771 /* FIXME: need to add ->core_command. */
5772 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5773 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5774 break;
5776 case NOTE_INFO_THREAD:
5777 /* Make a ".reg/999" section. */
5778 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5780 name = bfd_alloc (abfd, strlen (buf) + 1);
5781 if (name == NULL)
5782 return false;
5784 strcpy (name, buf);
5786 sect = bfd_make_section (abfd, name);
5787 if (sect == NULL)
5788 return false;
5790 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5791 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5792 data.thread_info.thread_context);
5793 sect->flags = SEC_HAS_CONTENTS;
5794 sect->alignment_power = 2;
5796 if (pstatus.data.thread_info.is_active_thread)
5797 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5798 return false;
5799 break;
5801 case NOTE_INFO_MODULE:
5802 /* Make a ".module/xxxxxxxx" section. */
5803 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5805 name = bfd_alloc (abfd, strlen (buf) + 1);
5806 if (name == NULL)
5807 return false;
5809 strcpy (name, buf);
5811 sect = bfd_make_section (abfd, name);
5813 if (sect == NULL)
5814 return false;
5816 sect->_raw_size = note->descsz;
5817 sect->filepos = note->descpos;
5818 sect->flags = SEC_HAS_CONTENTS;
5819 sect->alignment_power = 2;
5820 break;
5822 default:
5823 return true;
5826 return true;
5828 #endif /* HAVE_WIN32_PSTATUS_T */
5830 static boolean
5831 elfcore_grok_note (abfd, note)
5832 bfd *abfd;
5833 Elf_Internal_Note *note;
5835 struct elf_backend_data *bed = get_elf_backend_data (abfd);
5837 switch (note->type)
5839 default:
5840 return true;
5842 case NT_PRSTATUS:
5843 if (bed->elf_backend_grok_prstatus)
5844 if ((*bed->elf_backend_grok_prstatus) (abfd, note))
5845 return true;
5846 #if defined (HAVE_PRSTATUS_T)
5847 return elfcore_grok_prstatus (abfd, note);
5848 #else
5849 return true;
5850 #endif
5852 #if defined (HAVE_PSTATUS_T)
5853 case NT_PSTATUS:
5854 return elfcore_grok_pstatus (abfd, note);
5855 #endif
5857 #if defined (HAVE_LWPSTATUS_T)
5858 case NT_LWPSTATUS:
5859 return elfcore_grok_lwpstatus (abfd, note);
5860 #endif
5862 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5863 return elfcore_grok_prfpreg (abfd, note);
5865 #if defined (HAVE_WIN32_PSTATUS_T)
5866 case NT_WIN32PSTATUS:
5867 return elfcore_grok_win32pstatus (abfd, note);
5868 #endif
5870 case NT_PRXFPREG: /* Linux SSE extension */
5871 if (note->namesz == 5
5872 && ! strcmp (note->namedata, "LINUX"))
5873 return elfcore_grok_prxfpreg (abfd, note);
5874 else
5875 return true;
5877 case NT_PRPSINFO:
5878 case NT_PSINFO:
5879 if (bed->elf_backend_grok_psinfo)
5880 if ((*bed->elf_backend_grok_psinfo) (abfd, note))
5881 return true;
5882 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5883 return elfcore_grok_psinfo (abfd, note);
5884 #else
5885 return true;
5886 #endif
5890 static boolean
5891 elfcore_read_notes (abfd, offset, size)
5892 bfd *abfd;
5893 bfd_vma offset;
5894 bfd_vma size;
5896 char *buf;
5897 char *p;
5899 if (size <= 0)
5900 return true;
5902 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5903 return false;
5905 buf = bfd_malloc ((size_t) size);
5906 if (buf == NULL)
5907 return false;
5909 if (bfd_read (buf, size, 1, abfd) != size)
5911 error:
5912 free (buf);
5913 return false;
5916 p = buf;
5917 while (p < buf + size)
5919 /* FIXME: bad alignment assumption. */
5920 Elf_External_Note *xnp = (Elf_External_Note *) p;
5921 Elf_Internal_Note in;
5923 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5925 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5926 in.namedata = xnp->name;
5928 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5929 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5930 in.descpos = offset + (in.descdata - buf);
5932 if (! elfcore_grok_note (abfd, &in))
5933 goto error;
5935 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5938 free (buf);
5939 return true;
5942 /* Providing external access to the ELF program header table. */
5944 /* Return an upper bound on the number of bytes required to store a
5945 copy of ABFD's program header table entries. Return -1 if an error
5946 occurs; bfd_get_error will return an appropriate code. */
5948 long
5949 bfd_get_elf_phdr_upper_bound (abfd)
5950 bfd *abfd;
5952 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5954 bfd_set_error (bfd_error_wrong_format);
5955 return -1;
5958 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr);
5961 /* Copy ABFD's program header table entries to *PHDRS. The entries
5962 will be stored as an array of Elf_Internal_Phdr structures, as
5963 defined in include/elf/internal.h. To find out how large the
5964 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5966 Return the number of program header table entries read, or -1 if an
5967 error occurs; bfd_get_error will return an appropriate code. */
5970 bfd_get_elf_phdrs (abfd, phdrs)
5971 bfd *abfd;
5972 void *phdrs;
5974 int num_phdrs;
5976 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5978 bfd_set_error (bfd_error_wrong_format);
5979 return -1;
5982 num_phdrs = elf_elfheader (abfd)->e_phnum;
5983 memcpy (phdrs, elf_tdata (abfd)->phdr,
5984 num_phdrs * sizeof (Elf_Internal_Phdr));
5986 return num_phdrs;