Add OpenRISC support
[binutils.git] / bfd / elf.c
blob26ce9d3b0a7ff743d0914ed306e469cdf5247486
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 /* Print out the program headers. */
563 boolean
564 _bfd_elf_print_private_bfd_data (abfd, farg)
565 bfd *abfd;
566 PTR farg;
568 FILE *f = (FILE *) farg;
569 Elf_Internal_Phdr *p;
570 asection *s;
571 bfd_byte *dynbuf = NULL;
573 p = elf_tdata (abfd)->phdr;
574 if (p != NULL)
576 unsigned int i, c;
578 fprintf (f, _("\nProgram Header:\n"));
579 c = elf_elfheader (abfd)->e_phnum;
580 for (i = 0; i < c; i++, p++)
582 const char *s;
583 char buf[20];
585 switch (p->p_type)
587 case PT_NULL: s = "NULL"; break;
588 case PT_LOAD: s = "LOAD"; break;
589 case PT_DYNAMIC: s = "DYNAMIC"; break;
590 case PT_INTERP: s = "INTERP"; break;
591 case PT_NOTE: s = "NOTE"; break;
592 case PT_SHLIB: s = "SHLIB"; break;
593 case PT_PHDR: s = "PHDR"; break;
594 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
596 fprintf (f, "%8s off 0x", s);
597 fprintf_vma (f, p->p_offset);
598 fprintf (f, " vaddr 0x");
599 fprintf_vma (f, p->p_vaddr);
600 fprintf (f, " paddr 0x");
601 fprintf_vma (f, p->p_paddr);
602 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
603 fprintf (f, " filesz 0x");
604 fprintf_vma (f, p->p_filesz);
605 fprintf (f, " memsz 0x");
606 fprintf_vma (f, p->p_memsz);
607 fprintf (f, " flags %c%c%c",
608 (p->p_flags & PF_R) != 0 ? 'r' : '-',
609 (p->p_flags & PF_W) != 0 ? 'w' : '-',
610 (p->p_flags & PF_X) != 0 ? 'x' : '-');
611 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
612 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
613 fprintf (f, "\n");
617 s = bfd_get_section_by_name (abfd, ".dynamic");
618 if (s != NULL)
620 int elfsec;
621 unsigned long link;
622 bfd_byte *extdyn, *extdynend;
623 size_t extdynsize;
624 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
626 fprintf (f, _("\nDynamic Section:\n"));
628 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
629 if (dynbuf == NULL)
630 goto error_return;
631 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
632 s->_raw_size))
633 goto error_return;
635 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
636 if (elfsec == -1)
637 goto error_return;
638 link = elf_elfsections (abfd)[elfsec]->sh_link;
640 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
641 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
643 extdyn = dynbuf;
644 extdynend = extdyn + s->_raw_size;
645 for (; extdyn < extdynend; extdyn += extdynsize)
647 Elf_Internal_Dyn dyn;
648 const char *name;
649 char ab[20];
650 boolean stringp;
652 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
654 if (dyn.d_tag == DT_NULL)
655 break;
657 stringp = false;
658 switch (dyn.d_tag)
660 default:
661 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
662 name = ab;
663 break;
665 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
666 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
667 case DT_PLTGOT: name = "PLTGOT"; break;
668 case DT_HASH: name = "HASH"; break;
669 case DT_STRTAB: name = "STRTAB"; break;
670 case DT_SYMTAB: name = "SYMTAB"; break;
671 case DT_RELA: name = "RELA"; break;
672 case DT_RELASZ: name = "RELASZ"; break;
673 case DT_RELAENT: name = "RELAENT"; break;
674 case DT_STRSZ: name = "STRSZ"; break;
675 case DT_SYMENT: name = "SYMENT"; break;
676 case DT_INIT: name = "INIT"; break;
677 case DT_FINI: name = "FINI"; break;
678 case DT_SONAME: name = "SONAME"; stringp = true; break;
679 case DT_RPATH: name = "RPATH"; stringp = true; break;
680 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
681 case DT_REL: name = "REL"; break;
682 case DT_RELSZ: name = "RELSZ"; break;
683 case DT_RELENT: name = "RELENT"; break;
684 case DT_PLTREL: name = "PLTREL"; break;
685 case DT_DEBUG: name = "DEBUG"; break;
686 case DT_TEXTREL: name = "TEXTREL"; break;
687 case DT_JMPREL: name = "JMPREL"; break;
688 case DT_BIND_NOW: name = "BIND_NOW"; break;
689 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break;
690 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break;
691 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break;
692 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break;
693 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break;
694 case DT_FLAGS: name = "FLAGS"; break;
695 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break;
696 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break;
697 case DT_CHECKSUM: name = "CHECKSUM"; break;
698 case DT_PLTPADSZ: name = "PLTPADSZ"; break;
699 case DT_MOVEENT: name = "MOVEENT"; break;
700 case DT_MOVESZ: name = "MOVESZ"; break;
701 case DT_FEATURE: name = "FEATURE"; break;
702 case DT_POSFLAG_1: name = "POSFLAG_1"; break;
703 case DT_SYMINSZ: name = "SYMINSZ"; break;
704 case DT_SYMINENT: name = "SYMINENT"; break;
705 case DT_CONFIG: name = "CONFIG"; stringp = true; break;
706 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break;
707 case DT_AUDIT: name = "AUDIT"; stringp = true; break;
708 case DT_PLTPAD: name = "PLTPAD"; break;
709 case DT_MOVETAB: name = "MOVETAB"; break;
710 case DT_SYMINFO: name = "SYMINFO"; break;
711 case DT_RELACOUNT: name = "RELACOUNT"; break;
712 case DT_RELCOUNT: name = "RELCOUNT"; break;
713 case DT_FLAGS_1: name = "FLAGS_1"; break;
714 case DT_VERSYM: name = "VERSYM"; break;
715 case DT_VERDEF: name = "VERDEF"; break;
716 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
717 case DT_VERNEED: name = "VERNEED"; break;
718 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
719 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
720 case DT_USED: name = "USED"; break;
721 case DT_FILTER: name = "FILTER"; stringp = true; break;
724 fprintf (f, " %-11s ", name);
725 if (! stringp)
726 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
727 else
729 const char *string;
731 string = bfd_elf_string_from_elf_section (abfd, link,
732 dyn.d_un.d_val);
733 if (string == NULL)
734 goto error_return;
735 fprintf (f, "%s", string);
737 fprintf (f, "\n");
740 free (dynbuf);
741 dynbuf = NULL;
744 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
745 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
747 if (! _bfd_elf_slurp_version_tables (abfd))
748 return false;
751 if (elf_dynverdef (abfd) != 0)
753 Elf_Internal_Verdef *t;
755 fprintf (f, _("\nVersion definitions:\n"));
756 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
758 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
759 t->vd_flags, t->vd_hash, t->vd_nodename);
760 if (t->vd_auxptr->vda_nextptr != NULL)
762 Elf_Internal_Verdaux *a;
764 fprintf (f, "\t");
765 for (a = t->vd_auxptr->vda_nextptr;
766 a != NULL;
767 a = a->vda_nextptr)
768 fprintf (f, "%s ", a->vda_nodename);
769 fprintf (f, "\n");
774 if (elf_dynverref (abfd) != 0)
776 Elf_Internal_Verneed *t;
778 fprintf (f, _("\nVersion References:\n"));
779 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
781 Elf_Internal_Vernaux *a;
783 fprintf (f, _(" required from %s:\n"), t->vn_filename);
784 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
785 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
786 a->vna_flags, a->vna_other, a->vna_nodename);
790 return true;
792 error_return:
793 if (dynbuf != NULL)
794 free (dynbuf);
795 return false;
798 /* Display ELF-specific fields of a symbol. */
800 void
801 bfd_elf_print_symbol (abfd, filep, symbol, how)
802 bfd *abfd;
803 PTR filep;
804 asymbol *symbol;
805 bfd_print_symbol_type how;
807 FILE *file = (FILE *) filep;
808 switch (how)
810 case bfd_print_symbol_name:
811 fprintf (file, "%s", symbol->name);
812 break;
813 case bfd_print_symbol_more:
814 fprintf (file, "elf ");
815 fprintf_vma (file, symbol->value);
816 fprintf (file, " %lx", (long) symbol->flags);
817 break;
818 case bfd_print_symbol_all:
820 CONST char *section_name;
821 CONST char *name = NULL;
822 struct elf_backend_data *bed;
823 unsigned char st_other;
825 section_name = symbol->section ? symbol->section->name : "(*none*)";
827 bed = get_elf_backend_data (abfd);
828 if (bed->elf_backend_print_symbol_all)
829 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
831 if (name == NULL)
833 name = symbol->name;
834 bfd_print_symbol_vandf ((PTR) file, symbol);
837 fprintf (file, " %s\t", section_name);
838 /* Print the "other" value for a symbol. For common symbols,
839 we've already printed the size; now print the alignment.
840 For other symbols, we have no specified alignment, and
841 we've printed the address; now print the size. */
842 fprintf_vma (file,
843 (bfd_is_com_section (symbol->section)
844 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
845 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
847 /* If we have version information, print it. */
848 if (elf_tdata (abfd)->dynversym_section != 0
849 && (elf_tdata (abfd)->dynverdef_section != 0
850 || elf_tdata (abfd)->dynverref_section != 0))
852 unsigned int vernum;
853 const char *version_string;
855 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
857 if (vernum == 0)
858 version_string = "";
859 else if (vernum == 1)
860 version_string = "Base";
861 else if (vernum <= elf_tdata (abfd)->cverdefs)
862 version_string =
863 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
864 else
866 Elf_Internal_Verneed *t;
868 version_string = "";
869 for (t = elf_tdata (abfd)->verref;
870 t != NULL;
871 t = t->vn_nextref)
873 Elf_Internal_Vernaux *a;
875 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
877 if (a->vna_other == vernum)
879 version_string = a->vna_nodename;
880 break;
886 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
887 fprintf (file, " %-11s", version_string);
888 else
890 int i;
892 fprintf (file, " (%s)", version_string);
893 for (i = 10 - strlen (version_string); i > 0; --i)
894 putc (' ', file);
898 /* If the st_other field is not zero, print it. */
899 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
901 switch (st_other)
903 case 0: break;
904 case STV_INTERNAL: fprintf (file, " .internal"); break;
905 case STV_HIDDEN: fprintf (file, " .hidden"); break;
906 case STV_PROTECTED: fprintf (file, " .protected"); break;
907 default:
908 /* Some other non-defined flags are also present, so print
909 everything hex. */
910 fprintf (file, " 0x%02x", (unsigned int) st_other);
913 fprintf (file, " %s", name);
915 break;
919 /* Create an entry in an ELF linker hash table. */
921 struct bfd_hash_entry *
922 _bfd_elf_link_hash_newfunc (entry, table, string)
923 struct bfd_hash_entry *entry;
924 struct bfd_hash_table *table;
925 const char *string;
927 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
929 /* Allocate the structure if it has not already been allocated by a
930 subclass. */
931 if (ret == (struct elf_link_hash_entry *) NULL)
932 ret = ((struct elf_link_hash_entry *)
933 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
934 if (ret == (struct elf_link_hash_entry *) NULL)
935 return (struct bfd_hash_entry *) ret;
937 /* Call the allocation method of the superclass. */
938 ret = ((struct elf_link_hash_entry *)
939 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
940 table, string));
941 if (ret != (struct elf_link_hash_entry *) NULL)
943 /* Set local fields. */
944 ret->indx = -1;
945 ret->size = 0;
946 ret->dynindx = -1;
947 ret->dynstr_index = 0;
948 ret->weakdef = NULL;
949 ret->got.offset = (bfd_vma) -1;
950 ret->plt.offset = (bfd_vma) -1;
951 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
952 ret->verinfo.verdef = NULL;
953 ret->vtable_entries_used = NULL;
954 ret->vtable_entries_size = 0;
955 ret->vtable_parent = NULL;
956 ret->type = STT_NOTYPE;
957 ret->other = 0;
958 /* Assume that we have been called by a non-ELF symbol reader.
959 This flag is then reset by the code which reads an ELF input
960 file. This ensures that a symbol created by a non-ELF symbol
961 reader will have the flag set correctly. */
962 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
965 return (struct bfd_hash_entry *) ret;
968 /* Copy data from an indirect symbol to its direct symbol, hiding the
969 old indirect symbol. */
971 void
972 _bfd_elf_link_hash_copy_indirect (dir, ind)
973 struct elf_link_hash_entry *dir, *ind;
975 /* Copy down any references that we may have already seen to the
976 symbol which just became indirect. */
978 dir->elf_link_hash_flags |=
979 (ind->elf_link_hash_flags
980 & (ELF_LINK_HASH_REF_DYNAMIC
981 | ELF_LINK_HASH_REF_REGULAR
982 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
983 | ELF_LINK_NON_GOT_REF));
985 /* Copy over the global and procedure linkage table offset entries.
986 These may have been already set up by a check_relocs routine. */
987 if (dir->got.offset == (bfd_vma) -1)
989 dir->got.offset = ind->got.offset;
990 ind->got.offset = (bfd_vma) -1;
992 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
994 if (dir->plt.offset == (bfd_vma) -1)
996 dir->plt.offset = ind->plt.offset;
997 ind->plt.offset = (bfd_vma) -1;
999 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
1001 if (dir->dynindx == -1)
1003 dir->dynindx = ind->dynindx;
1004 dir->dynstr_index = ind->dynstr_index;
1005 ind->dynindx = -1;
1006 ind->dynstr_index = 0;
1008 BFD_ASSERT (ind->dynindx == -1);
1011 void
1012 _bfd_elf_link_hash_hide_symbol (info, h)
1013 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1014 struct elf_link_hash_entry *h;
1016 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1017 h->dynindx = -1;
1018 h->plt.offset = (bfd_vma) -1;
1021 /* Initialize an ELF linker hash table. */
1023 boolean
1024 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
1025 struct elf_link_hash_table *table;
1026 bfd *abfd;
1027 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
1028 struct bfd_hash_table *,
1029 const char *));
1031 table->dynamic_sections_created = false;
1032 table->dynobj = NULL;
1033 /* The first dynamic symbol is a dummy. */
1034 table->dynsymcount = 1;
1035 table->dynstr = NULL;
1036 table->bucketcount = 0;
1037 table->needed = NULL;
1038 table->runpath = NULL;
1039 table->hgot = NULL;
1040 table->stab_info = NULL;
1041 table->merge_info = NULL;
1042 table->dynlocal = NULL;
1043 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
1046 /* Create an ELF linker hash table. */
1048 struct bfd_link_hash_table *
1049 _bfd_elf_link_hash_table_create (abfd)
1050 bfd *abfd;
1052 struct elf_link_hash_table *ret;
1054 ret = ((struct elf_link_hash_table *)
1055 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
1056 if (ret == (struct elf_link_hash_table *) NULL)
1057 return NULL;
1059 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
1061 bfd_release (abfd, ret);
1062 return NULL;
1065 return &ret->root;
1068 /* This is a hook for the ELF emulation code in the generic linker to
1069 tell the backend linker what file name to use for the DT_NEEDED
1070 entry for a dynamic object. The generic linker passes name as an
1071 empty string to indicate that no DT_NEEDED entry should be made. */
1073 void
1074 bfd_elf_set_dt_needed_name (abfd, name)
1075 bfd *abfd;
1076 const char *name;
1078 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1079 && bfd_get_format (abfd) == bfd_object)
1080 elf_dt_name (abfd) = name;
1083 void
1084 bfd_elf_set_dt_needed_soname (abfd, name)
1085 bfd *abfd;
1086 const char *name;
1088 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1089 && bfd_get_format (abfd) == bfd_object)
1090 elf_dt_soname (abfd) = name;
1093 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1094 the linker ELF emulation code. */
1096 struct bfd_link_needed_list *
1097 bfd_elf_get_needed_list (abfd, info)
1098 bfd *abfd ATTRIBUTE_UNUSED;
1099 struct bfd_link_info *info;
1101 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1102 return NULL;
1103 return elf_hash_table (info)->needed;
1106 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a
1107 hook for the linker ELF emulation code. */
1109 struct bfd_link_needed_list *
1110 bfd_elf_get_runpath_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)->runpath;
1119 /* Get the name actually used for a dynamic object for a link. This
1120 is the SONAME entry if there is one. Otherwise, it is the string
1121 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1123 const char *
1124 bfd_elf_get_dt_soname (abfd)
1125 bfd *abfd;
1127 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1128 && bfd_get_format (abfd) == bfd_object)
1129 return elf_dt_name (abfd);
1130 return NULL;
1133 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1134 the ELF linker emulation code. */
1136 boolean
1137 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1138 bfd *abfd;
1139 struct bfd_link_needed_list **pneeded;
1141 asection *s;
1142 bfd_byte *dynbuf = NULL;
1143 int elfsec;
1144 unsigned long link;
1145 bfd_byte *extdyn, *extdynend;
1146 size_t extdynsize;
1147 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1149 *pneeded = NULL;
1151 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1152 || bfd_get_format (abfd) != bfd_object)
1153 return true;
1155 s = bfd_get_section_by_name (abfd, ".dynamic");
1156 if (s == NULL || s->_raw_size == 0)
1157 return true;
1159 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1160 if (dynbuf == NULL)
1161 goto error_return;
1163 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1164 s->_raw_size))
1165 goto error_return;
1167 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1168 if (elfsec == -1)
1169 goto error_return;
1171 link = elf_elfsections (abfd)[elfsec]->sh_link;
1173 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1174 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1176 extdyn = dynbuf;
1177 extdynend = extdyn + s->_raw_size;
1178 for (; extdyn < extdynend; extdyn += extdynsize)
1180 Elf_Internal_Dyn dyn;
1182 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1184 if (dyn.d_tag == DT_NULL)
1185 break;
1187 if (dyn.d_tag == DT_NEEDED)
1189 const char *string;
1190 struct bfd_link_needed_list *l;
1192 string = bfd_elf_string_from_elf_section (abfd, link,
1193 dyn.d_un.d_val);
1194 if (string == NULL)
1195 goto error_return;
1197 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1198 if (l == NULL)
1199 goto error_return;
1201 l->by = abfd;
1202 l->name = string;
1203 l->next = *pneeded;
1204 *pneeded = l;
1208 free (dynbuf);
1210 return true;
1212 error_return:
1213 if (dynbuf != NULL)
1214 free (dynbuf);
1215 return false;
1218 /* Allocate an ELF string table--force the first byte to be zero. */
1220 struct bfd_strtab_hash *
1221 _bfd_elf_stringtab_init ()
1223 struct bfd_strtab_hash *ret;
1225 ret = _bfd_stringtab_init ();
1226 if (ret != NULL)
1228 bfd_size_type loc;
1230 loc = _bfd_stringtab_add (ret, "", true, false);
1231 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1232 if (loc == (bfd_size_type) -1)
1234 _bfd_stringtab_free (ret);
1235 ret = NULL;
1238 return ret;
1241 /* ELF .o/exec file reading */
1243 /* Create a new bfd section from an ELF section header. */
1245 boolean
1246 bfd_section_from_shdr (abfd, shindex)
1247 bfd *abfd;
1248 unsigned int shindex;
1250 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1251 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1252 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1253 char *name;
1255 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1257 switch (hdr->sh_type)
1259 case SHT_NULL:
1260 /* Inactive section. Throw it away. */
1261 return true;
1263 case SHT_PROGBITS: /* Normal section with contents. */
1264 case SHT_DYNAMIC: /* Dynamic linking information. */
1265 case SHT_NOBITS: /* .bss section. */
1266 case SHT_HASH: /* .hash section. */
1267 case SHT_NOTE: /* .note section. */
1268 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1270 case SHT_SYMTAB: /* A symbol table */
1271 if (elf_onesymtab (abfd) == shindex)
1272 return true;
1274 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1275 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1276 elf_onesymtab (abfd) = shindex;
1277 elf_tdata (abfd)->symtab_hdr = *hdr;
1278 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1279 abfd->flags |= HAS_SYMS;
1281 /* Sometimes a shared object will map in the symbol table. If
1282 SHF_ALLOC is set, and this is a shared object, then we also
1283 treat this section as a BFD section. We can not base the
1284 decision purely on SHF_ALLOC, because that flag is sometimes
1285 set in a relocateable object file, which would confuse the
1286 linker. */
1287 if ((hdr->sh_flags & SHF_ALLOC) != 0
1288 && (abfd->flags & DYNAMIC) != 0
1289 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1290 return false;
1292 return true;
1294 case SHT_DYNSYM: /* A dynamic symbol table */
1295 if (elf_dynsymtab (abfd) == shindex)
1296 return true;
1298 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1299 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1300 elf_dynsymtab (abfd) = shindex;
1301 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1302 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1303 abfd->flags |= HAS_SYMS;
1305 /* Besides being a symbol table, we also treat this as a regular
1306 section, so that objcopy can handle it. */
1307 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1309 case SHT_STRTAB: /* A string table */
1310 if (hdr->bfd_section != NULL)
1311 return true;
1312 if (ehdr->e_shstrndx == shindex)
1314 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1315 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1316 return true;
1319 unsigned int i;
1321 for (i = 1; i < ehdr->e_shnum; i++)
1323 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1324 if (hdr2->sh_link == shindex)
1326 if (! bfd_section_from_shdr (abfd, i))
1327 return false;
1328 if (elf_onesymtab (abfd) == i)
1330 elf_tdata (abfd)->strtab_hdr = *hdr;
1331 elf_elfsections (abfd)[shindex] =
1332 &elf_tdata (abfd)->strtab_hdr;
1333 return true;
1335 if (elf_dynsymtab (abfd) == i)
1337 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1338 elf_elfsections (abfd)[shindex] = hdr =
1339 &elf_tdata (abfd)->dynstrtab_hdr;
1340 /* We also treat this as a regular section, so
1341 that objcopy can handle it. */
1342 break;
1344 #if 0 /* Not handling other string tables specially right now. */
1345 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1346 /* We have a strtab for some random other section. */
1347 newsect = (asection *) hdr2->bfd_section;
1348 if (!newsect)
1349 break;
1350 hdr->bfd_section = newsect;
1351 hdr2 = &elf_section_data (newsect)->str_hdr;
1352 *hdr2 = *hdr;
1353 elf_elfsections (abfd)[shindex] = hdr2;
1354 #endif
1359 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1361 case SHT_REL:
1362 case SHT_RELA:
1363 /* *These* do a lot of work -- but build no sections! */
1365 asection *target_sect;
1366 Elf_Internal_Shdr *hdr2;
1368 /* Check for a bogus link to avoid crashing. */
1369 if (hdr->sh_link >= ehdr->e_shnum)
1371 ((*_bfd_error_handler)
1372 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1373 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1374 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1377 /* For some incomprehensible reason Oracle distributes
1378 libraries for Solaris in which some of the objects have
1379 bogus sh_link fields. It would be nice if we could just
1380 reject them, but, unfortunately, some people need to use
1381 them. We scan through the section headers; if we find only
1382 one suitable symbol table, we clobber the sh_link to point
1383 to it. I hope this doesn't break anything. */
1384 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1385 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1387 int scan;
1388 int found;
1390 found = 0;
1391 for (scan = 1; scan < ehdr->e_shnum; scan++)
1393 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1394 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1396 if (found != 0)
1398 found = 0;
1399 break;
1401 found = scan;
1404 if (found != 0)
1405 hdr->sh_link = found;
1408 /* Get the symbol table. */
1409 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1410 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1411 return false;
1413 /* If this reloc section does not use the main symbol table we
1414 don't treat it as a reloc section. BFD can't adequately
1415 represent such a section, so at least for now, we don't
1416 try. We just present it as a normal section. We also
1417 can't use it as a reloc section if it points to the null
1418 section. */
1419 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF)
1420 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1422 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1423 return false;
1424 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1425 if (target_sect == NULL)
1426 return false;
1428 if ((target_sect->flags & SEC_RELOC) == 0
1429 || target_sect->reloc_count == 0)
1430 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1431 else
1433 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1434 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1435 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1437 *hdr2 = *hdr;
1438 elf_elfsections (abfd)[shindex] = hdr2;
1439 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1440 target_sect->flags |= SEC_RELOC;
1441 target_sect->relocation = NULL;
1442 target_sect->rel_filepos = hdr->sh_offset;
1443 /* In the section to which the relocations apply, mark whether
1444 its relocations are of the REL or RELA variety. */
1445 if (hdr->sh_size != 0)
1446 elf_section_data (target_sect)->use_rela_p
1447 = (hdr->sh_type == SHT_RELA);
1448 abfd->flags |= HAS_RELOC;
1449 return true;
1451 break;
1453 case SHT_GNU_verdef:
1454 elf_dynverdef (abfd) = shindex;
1455 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1456 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1457 break;
1459 case SHT_GNU_versym:
1460 elf_dynversym (abfd) = shindex;
1461 elf_tdata (abfd)->dynversym_hdr = *hdr;
1462 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1463 break;
1465 case SHT_GNU_verneed:
1466 elf_dynverref (abfd) = shindex;
1467 elf_tdata (abfd)->dynverref_hdr = *hdr;
1468 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1469 break;
1471 case SHT_SHLIB:
1472 return true;
1474 default:
1475 /* Check for any processor-specific section types. */
1477 if (bed->elf_backend_section_from_shdr)
1478 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1480 break;
1483 return true;
1486 /* Given an ELF section number, retrieve the corresponding BFD
1487 section. */
1489 asection *
1490 bfd_section_from_elf_index (abfd, index)
1491 bfd *abfd;
1492 unsigned int index;
1494 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1495 if (index >= elf_elfheader (abfd)->e_shnum)
1496 return NULL;
1497 return elf_elfsections (abfd)[index]->bfd_section;
1500 boolean
1501 _bfd_elf_new_section_hook (abfd, sec)
1502 bfd *abfd;
1503 asection *sec;
1505 struct bfd_elf_section_data *sdata;
1507 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1508 if (!sdata)
1509 return false;
1510 sec->used_by_bfd = (PTR) sdata;
1512 /* Indicate whether or not this section should use RELA relocations. */
1513 sdata->use_rela_p
1514 = get_elf_backend_data (abfd)->default_use_rela_p;
1516 return true;
1519 /* Create a new bfd section from an ELF program header.
1521 Since program segments have no names, we generate a synthetic name
1522 of the form segment<NUM>, where NUM is generally the index in the
1523 program header table. For segments that are split (see below) we
1524 generate the names segment<NUM>a and segment<NUM>b.
1526 Note that some program segments may have a file size that is different than
1527 (less than) the memory size. All this means is that at execution the
1528 system must allocate the amount of memory specified by the memory size,
1529 but only initialize it with the first "file size" bytes read from the
1530 file. This would occur for example, with program segments consisting
1531 of combined data+bss.
1533 To handle the above situation, this routine generates TWO bfd sections
1534 for the single program segment. The first has the length specified by
1535 the file size of the segment, and the second has the length specified
1536 by the difference between the two sizes. In effect, the segment is split
1537 into it's initialized and uninitialized parts.
1541 boolean
1542 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1543 bfd *abfd;
1544 Elf_Internal_Phdr *hdr;
1545 int index;
1546 const char *typename;
1548 asection *newsect;
1549 char *name;
1550 char namebuf[64];
1551 int split;
1553 split = ((hdr->p_memsz > 0)
1554 && (hdr->p_filesz > 0)
1555 && (hdr->p_memsz > hdr->p_filesz));
1556 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1557 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1558 if (!name)
1559 return false;
1560 strcpy (name, namebuf);
1561 newsect = bfd_make_section (abfd, name);
1562 if (newsect == NULL)
1563 return false;
1564 newsect->vma = hdr->p_vaddr;
1565 newsect->lma = hdr->p_paddr;
1566 newsect->_raw_size = hdr->p_filesz;
1567 newsect->filepos = hdr->p_offset;
1568 newsect->flags |= SEC_HAS_CONTENTS;
1569 if (hdr->p_type == PT_LOAD)
1571 newsect->flags |= SEC_ALLOC;
1572 newsect->flags |= SEC_LOAD;
1573 if (hdr->p_flags & PF_X)
1575 /* FIXME: all we known is that it has execute PERMISSION,
1576 may be data. */
1577 newsect->flags |= SEC_CODE;
1580 if (!(hdr->p_flags & PF_W))
1582 newsect->flags |= SEC_READONLY;
1585 if (split)
1587 sprintf (namebuf, "%s%db", typename, index);
1588 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1589 if (!name)
1590 return false;
1591 strcpy (name, namebuf);
1592 newsect = bfd_make_section (abfd, name);
1593 if (newsect == NULL)
1594 return false;
1595 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1596 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1597 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1598 if (hdr->p_type == PT_LOAD)
1600 newsect->flags |= SEC_ALLOC;
1601 if (hdr->p_flags & PF_X)
1602 newsect->flags |= SEC_CODE;
1604 if (!(hdr->p_flags & PF_W))
1605 newsect->flags |= SEC_READONLY;
1608 return true;
1611 boolean
1612 bfd_section_from_phdr (abfd, hdr, index)
1613 bfd *abfd;
1614 Elf_Internal_Phdr *hdr;
1615 int index;
1617 struct elf_backend_data *bed;
1619 switch (hdr->p_type)
1621 case PT_NULL:
1622 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1624 case PT_LOAD:
1625 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1627 case PT_DYNAMIC:
1628 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1630 case PT_INTERP:
1631 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1633 case PT_NOTE:
1634 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1635 return false;
1636 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1637 return false;
1638 return true;
1640 case PT_SHLIB:
1641 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1643 case PT_PHDR:
1644 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1646 default:
1647 /* Check for any processor-specific program segment types.
1648 If no handler for them, default to making "segment" sections. */
1649 bed = get_elf_backend_data (abfd);
1650 if (bed->elf_backend_section_from_phdr)
1651 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1652 else
1653 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1657 /* Initialize REL_HDR, the section-header for new section, containing
1658 relocations against ASECT. If USE_RELA_P is true, we use RELA
1659 relocations; otherwise, we use REL relocations. */
1661 boolean
1662 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1663 bfd *abfd;
1664 Elf_Internal_Shdr *rel_hdr;
1665 asection *asect;
1666 boolean use_rela_p;
1668 char *name;
1669 struct elf_backend_data *bed;
1671 bed = get_elf_backend_data (abfd);
1672 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1673 if (name == NULL)
1674 return false;
1675 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1676 rel_hdr->sh_name =
1677 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1678 true, false);
1679 if (rel_hdr->sh_name == (unsigned int) -1)
1680 return false;
1681 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1682 rel_hdr->sh_entsize = (use_rela_p
1683 ? bed->s->sizeof_rela
1684 : bed->s->sizeof_rel);
1685 rel_hdr->sh_addralign = bed->s->file_align;
1686 rel_hdr->sh_flags = 0;
1687 rel_hdr->sh_addr = 0;
1688 rel_hdr->sh_size = 0;
1689 rel_hdr->sh_offset = 0;
1691 return true;
1694 /* Set up an ELF internal section header for a section. */
1696 static void
1697 elf_fake_sections (abfd, asect, failedptrarg)
1698 bfd *abfd;
1699 asection *asect;
1700 PTR failedptrarg;
1702 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1703 boolean *failedptr = (boolean *) failedptrarg;
1704 Elf_Internal_Shdr *this_hdr;
1706 if (*failedptr)
1708 /* We already failed; just get out of the bfd_map_over_sections
1709 loop. */
1710 return;
1713 this_hdr = &elf_section_data (asect)->this_hdr;
1715 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1716 asect->name,
1717 true, false);
1718 if (this_hdr->sh_name == (unsigned long) -1)
1720 *failedptr = true;
1721 return;
1724 this_hdr->sh_flags = 0;
1726 if ((asect->flags & SEC_ALLOC) != 0
1727 || asect->user_set_vma)
1728 this_hdr->sh_addr = asect->vma;
1729 else
1730 this_hdr->sh_addr = 0;
1732 this_hdr->sh_offset = 0;
1733 this_hdr->sh_size = asect->_raw_size;
1734 this_hdr->sh_link = 0;
1735 this_hdr->sh_addralign = 1 << asect->alignment_power;
1736 /* The sh_entsize and sh_info fields may have been set already by
1737 copy_private_section_data. */
1739 this_hdr->bfd_section = asect;
1740 this_hdr->contents = NULL;
1742 /* FIXME: This should not be based on section names. */
1743 if (strcmp (asect->name, ".dynstr") == 0)
1744 this_hdr->sh_type = SHT_STRTAB;
1745 else if (strcmp (asect->name, ".hash") == 0)
1747 this_hdr->sh_type = SHT_HASH;
1748 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1750 else if (strcmp (asect->name, ".dynsym") == 0)
1752 this_hdr->sh_type = SHT_DYNSYM;
1753 this_hdr->sh_entsize = bed->s->sizeof_sym;
1755 else if (strcmp (asect->name, ".dynamic") == 0)
1757 this_hdr->sh_type = SHT_DYNAMIC;
1758 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1760 else if (strncmp (asect->name, ".rela", 5) == 0
1761 && get_elf_backend_data (abfd)->may_use_rela_p)
1763 this_hdr->sh_type = SHT_RELA;
1764 this_hdr->sh_entsize = bed->s->sizeof_rela;
1766 else if (strncmp (asect->name, ".rel", 4) == 0
1767 && get_elf_backend_data (abfd)->may_use_rel_p)
1769 this_hdr->sh_type = SHT_REL;
1770 this_hdr->sh_entsize = bed->s->sizeof_rel;
1772 else if (strncmp (asect->name, ".note", 5) == 0)
1773 this_hdr->sh_type = SHT_NOTE;
1774 else if (strncmp (asect->name, ".stab", 5) == 0
1775 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1776 this_hdr->sh_type = SHT_STRTAB;
1777 else if (strcmp (asect->name, ".gnu.version") == 0)
1779 this_hdr->sh_type = SHT_GNU_versym;
1780 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1782 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1784 this_hdr->sh_type = SHT_GNU_verdef;
1785 this_hdr->sh_entsize = 0;
1786 /* objcopy or strip will copy over sh_info, but may not set
1787 cverdefs. The linker will set cverdefs, but sh_info will be
1788 zero. */
1789 if (this_hdr->sh_info == 0)
1790 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1791 else
1792 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1793 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1795 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1797 this_hdr->sh_type = SHT_GNU_verneed;
1798 this_hdr->sh_entsize = 0;
1799 /* objcopy or strip will copy over sh_info, but may not set
1800 cverrefs. The linker will set cverrefs, but sh_info will be
1801 zero. */
1802 if (this_hdr->sh_info == 0)
1803 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1804 else
1805 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1806 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1808 else if ((asect->flags & SEC_ALLOC) != 0
1809 && ((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0))
1810 this_hdr->sh_type = SHT_NOBITS;
1811 else
1812 this_hdr->sh_type = SHT_PROGBITS;
1814 if ((asect->flags & SEC_ALLOC) != 0)
1815 this_hdr->sh_flags |= SHF_ALLOC;
1816 if ((asect->flags & SEC_READONLY) == 0)
1817 this_hdr->sh_flags |= SHF_WRITE;
1818 if ((asect->flags & SEC_CODE) != 0)
1819 this_hdr->sh_flags |= SHF_EXECINSTR;
1820 if ((asect->flags & SEC_MERGE) != 0)
1822 this_hdr->sh_flags |= SHF_MERGE;
1823 this_hdr->sh_entsize = asect->entsize;
1824 if ((asect->flags & SEC_STRINGS) != 0)
1825 this_hdr->sh_flags |= SHF_STRINGS;
1828 /* Check for processor-specific section types. */
1829 if (bed->elf_backend_fake_sections)
1830 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1832 /* If the section has relocs, set up a section header for the
1833 SHT_REL[A] section. If two relocation sections are required for
1834 this section, it is up to the processor-specific back-end to
1835 create the other. */
1836 if ((asect->flags & SEC_RELOC) != 0
1837 && !_bfd_elf_init_reloc_shdr (abfd,
1838 &elf_section_data (asect)->rel_hdr,
1839 asect,
1840 elf_section_data (asect)->use_rela_p))
1841 *failedptr = true;
1844 /* Assign all ELF section numbers. The dummy first section is handled here
1845 too. The link/info pointers for the standard section types are filled
1846 in here too, while we're at it. */
1848 static boolean
1849 assign_section_numbers (abfd)
1850 bfd *abfd;
1852 struct elf_obj_tdata *t = elf_tdata (abfd);
1853 asection *sec;
1854 unsigned int section_number;
1855 Elf_Internal_Shdr **i_shdrp;
1857 section_number = 1;
1859 for (sec = abfd->sections; sec; sec = sec->next)
1861 struct bfd_elf_section_data *d = elf_section_data (sec);
1863 d->this_idx = section_number++;
1864 if ((sec->flags & SEC_RELOC) == 0)
1865 d->rel_idx = 0;
1866 else
1867 d->rel_idx = section_number++;
1869 if (d->rel_hdr2)
1870 d->rel_idx2 = section_number++;
1871 else
1872 d->rel_idx2 = 0;
1875 t->shstrtab_section = section_number++;
1876 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1877 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1879 if (bfd_get_symcount (abfd) > 0)
1881 t->symtab_section = section_number++;
1882 t->strtab_section = section_number++;
1885 elf_elfheader (abfd)->e_shnum = section_number;
1887 /* Set up the list of section header pointers, in agreement with the
1888 indices. */
1889 i_shdrp = ((Elf_Internal_Shdr **)
1890 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1891 if (i_shdrp == NULL)
1892 return false;
1894 i_shdrp[0] = ((Elf_Internal_Shdr *)
1895 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1896 if (i_shdrp[0] == NULL)
1898 bfd_release (abfd, i_shdrp);
1899 return false;
1901 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1903 elf_elfsections (abfd) = i_shdrp;
1905 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1906 if (bfd_get_symcount (abfd) > 0)
1908 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1909 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1910 t->symtab_hdr.sh_link = t->strtab_section;
1912 for (sec = abfd->sections; sec; sec = sec->next)
1914 struct bfd_elf_section_data *d = elf_section_data (sec);
1915 asection *s;
1916 const char *name;
1918 i_shdrp[d->this_idx] = &d->this_hdr;
1919 if (d->rel_idx != 0)
1920 i_shdrp[d->rel_idx] = &d->rel_hdr;
1921 if (d->rel_idx2 != 0)
1922 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1924 /* Fill in the sh_link and sh_info fields while we're at it. */
1926 /* sh_link of a reloc section is the section index of the symbol
1927 table. sh_info is the section index of the section to which
1928 the relocation entries apply. */
1929 if (d->rel_idx != 0)
1931 d->rel_hdr.sh_link = t->symtab_section;
1932 d->rel_hdr.sh_info = d->this_idx;
1934 if (d->rel_idx2 != 0)
1936 d->rel_hdr2->sh_link = t->symtab_section;
1937 d->rel_hdr2->sh_info = d->this_idx;
1940 switch (d->this_hdr.sh_type)
1942 case SHT_REL:
1943 case SHT_RELA:
1944 /* A reloc section which we are treating as a normal BFD
1945 section. sh_link is the section index of the symbol
1946 table. sh_info is the section index of the section to
1947 which the relocation entries apply. We assume that an
1948 allocated reloc section uses the dynamic symbol table.
1949 FIXME: How can we be sure? */
1950 s = bfd_get_section_by_name (abfd, ".dynsym");
1951 if (s != NULL)
1952 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1954 /* We look up the section the relocs apply to by name. */
1955 name = sec->name;
1956 if (d->this_hdr.sh_type == SHT_REL)
1957 name += 4;
1958 else
1959 name += 5;
1960 s = bfd_get_section_by_name (abfd, name);
1961 if (s != NULL)
1962 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1963 break;
1965 case SHT_STRTAB:
1966 /* We assume that a section named .stab*str is a stabs
1967 string section. We look for a section with the same name
1968 but without the trailing ``str'', and set its sh_link
1969 field to point to this section. */
1970 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1971 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1973 size_t len;
1974 char *alc;
1976 len = strlen (sec->name);
1977 alc = (char *) bfd_malloc (len - 2);
1978 if (alc == NULL)
1979 return false;
1980 strncpy (alc, sec->name, len - 3);
1981 alc[len - 3] = '\0';
1982 s = bfd_get_section_by_name (abfd, alc);
1983 free (alc);
1984 if (s != NULL)
1986 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1988 /* This is a .stab section. */
1989 elf_section_data (s)->this_hdr.sh_entsize =
1990 4 + 2 * bfd_get_arch_size (abfd) / 8;
1993 break;
1995 case SHT_DYNAMIC:
1996 case SHT_DYNSYM:
1997 case SHT_GNU_verneed:
1998 case SHT_GNU_verdef:
1999 /* sh_link is the section header index of the string table
2000 used for the dynamic entries, or the symbol table, or the
2001 version strings. */
2002 s = bfd_get_section_by_name (abfd, ".dynstr");
2003 if (s != NULL)
2004 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2005 break;
2007 case SHT_HASH:
2008 case SHT_GNU_versym:
2009 /* sh_link is the section header index of the symbol table
2010 this hash table or version table is for. */
2011 s = bfd_get_section_by_name (abfd, ".dynsym");
2012 if (s != NULL)
2013 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
2014 break;
2018 return true;
2021 /* Map symbol from it's internal number to the external number, moving
2022 all local symbols to be at the head of the list. */
2024 static INLINE int
2025 sym_is_global (abfd, sym)
2026 bfd *abfd;
2027 asymbol *sym;
2029 /* If the backend has a special mapping, use it. */
2030 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2031 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
2032 (abfd, sym));
2034 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
2035 || bfd_is_und_section (bfd_get_section (sym))
2036 || bfd_is_com_section (bfd_get_section (sym)));
2039 static boolean
2040 elf_map_symbols (abfd)
2041 bfd *abfd;
2043 int symcount = bfd_get_symcount (abfd);
2044 asymbol **syms = bfd_get_outsymbols (abfd);
2045 asymbol **sect_syms;
2046 int num_locals = 0;
2047 int num_globals = 0;
2048 int num_locals2 = 0;
2049 int num_globals2 = 0;
2050 int max_index = 0;
2051 int num_sections = 0;
2052 int idx;
2053 asection *asect;
2054 asymbol **new_syms;
2055 asymbol *sym;
2057 #ifdef DEBUG
2058 fprintf (stderr, "elf_map_symbols\n");
2059 fflush (stderr);
2060 #endif
2062 /* Add a section symbol for each BFD section. FIXME: Is this really
2063 necessary? */
2064 for (asect = abfd->sections; asect; asect = asect->next)
2066 if (max_index < asect->index)
2067 max_index = asect->index;
2070 max_index++;
2071 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2072 if (sect_syms == NULL)
2073 return false;
2074 elf_section_syms (abfd) = sect_syms;
2076 for (idx = 0; idx < symcount; idx++)
2078 sym = syms[idx];
2080 if ((sym->flags & BSF_SECTION_SYM) != 0
2081 && sym->value == 0)
2083 asection *sec;
2085 sec = sym->section;
2087 if (sec->owner != NULL)
2089 if (sec->owner != abfd)
2091 if (sec->output_offset != 0)
2092 continue;
2094 sec = sec->output_section;
2096 /* Empty sections in the input files may have had a section
2097 symbol created for them. (See the comment near the end of
2098 _bfd_generic_link_output_symbols in linker.c). If the linker
2099 script discards such sections then we will reach this point.
2100 Since we know that we cannot avoid this case, we detect it
2101 and skip the abort and the assignment to the sect_syms array.
2102 To reproduce this particular case try running the linker
2103 testsuite test ld-scripts/weak.exp for an ELF port that uses
2104 the generic linker. */
2105 if (sec->owner == NULL)
2106 continue;
2108 BFD_ASSERT (sec->owner == abfd);
2110 sect_syms[sec->index] = syms[idx];
2115 for (asect = abfd->sections; asect; asect = asect->next)
2117 if (sect_syms[asect->index] != NULL)
2118 continue;
2120 sym = bfd_make_empty_symbol (abfd);
2121 if (sym == NULL)
2122 return false;
2123 sym->the_bfd = abfd;
2124 sym->name = asect->name;
2125 sym->value = 0;
2126 /* Set the flags to 0 to indicate that this one was newly added. */
2127 sym->flags = 0;
2128 sym->section = asect;
2129 sect_syms[asect->index] = sym;
2130 num_sections++;
2131 #ifdef DEBUG
2132 fprintf (stderr,
2133 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2134 asect->name, (long) asect->vma, asect->index, (long) asect);
2135 #endif
2138 /* Classify all of the symbols. */
2139 for (idx = 0; idx < symcount; idx++)
2141 if (!sym_is_global (abfd, syms[idx]))
2142 num_locals++;
2143 else
2144 num_globals++;
2146 for (asect = abfd->sections; asect; asect = asect->next)
2148 if (sect_syms[asect->index] != NULL
2149 && sect_syms[asect->index]->flags == 0)
2151 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2152 if (!sym_is_global (abfd, sect_syms[asect->index]))
2153 num_locals++;
2154 else
2155 num_globals++;
2156 sect_syms[asect->index]->flags = 0;
2160 /* Now sort the symbols so the local symbols are first. */
2161 new_syms = ((asymbol **)
2162 bfd_alloc (abfd,
2163 (num_locals + num_globals) * sizeof (asymbol *)));
2164 if (new_syms == NULL)
2165 return false;
2167 for (idx = 0; idx < symcount; idx++)
2169 asymbol *sym = syms[idx];
2170 int i;
2172 if (!sym_is_global (abfd, sym))
2173 i = num_locals2++;
2174 else
2175 i = num_locals + num_globals2++;
2176 new_syms[i] = sym;
2177 sym->udata.i = i + 1;
2179 for (asect = abfd->sections; asect; asect = asect->next)
2181 if (sect_syms[asect->index] != NULL
2182 && sect_syms[asect->index]->flags == 0)
2184 asymbol *sym = sect_syms[asect->index];
2185 int i;
2187 sym->flags = BSF_SECTION_SYM;
2188 if (!sym_is_global (abfd, sym))
2189 i = num_locals2++;
2190 else
2191 i = num_locals + num_globals2++;
2192 new_syms[i] = sym;
2193 sym->udata.i = i + 1;
2197 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2199 elf_num_locals (abfd) = num_locals;
2200 elf_num_globals (abfd) = num_globals;
2201 return true;
2204 /* Align to the maximum file alignment that could be required for any
2205 ELF data structure. */
2207 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2208 static INLINE file_ptr
2209 align_file_position (off, align)
2210 file_ptr off;
2211 int align;
2213 return (off + align - 1) & ~(align - 1);
2216 /* Assign a file position to a section, optionally aligning to the
2217 required section alignment. */
2219 INLINE file_ptr
2220 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2221 Elf_Internal_Shdr *i_shdrp;
2222 file_ptr offset;
2223 boolean align;
2225 if (align)
2227 unsigned int al;
2229 al = i_shdrp->sh_addralign;
2230 if (al > 1)
2231 offset = BFD_ALIGN (offset, al);
2233 i_shdrp->sh_offset = offset;
2234 if (i_shdrp->bfd_section != NULL)
2235 i_shdrp->bfd_section->filepos = offset;
2236 if (i_shdrp->sh_type != SHT_NOBITS)
2237 offset += i_shdrp->sh_size;
2238 return offset;
2241 /* Compute the file positions we are going to put the sections at, and
2242 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2243 is not NULL, this is being called by the ELF backend linker. */
2245 boolean
2246 _bfd_elf_compute_section_file_positions (abfd, link_info)
2247 bfd *abfd;
2248 struct bfd_link_info *link_info;
2250 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2251 boolean failed;
2252 struct bfd_strtab_hash *strtab;
2253 Elf_Internal_Shdr *shstrtab_hdr;
2255 if (abfd->output_has_begun)
2256 return true;
2258 /* Do any elf backend specific processing first. */
2259 if (bed->elf_backend_begin_write_processing)
2260 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2262 if (! prep_headers (abfd))
2263 return false;
2265 /* Post process the headers if necessary. */
2266 if (bed->elf_backend_post_process_headers)
2267 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2269 failed = false;
2270 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2271 if (failed)
2272 return false;
2274 if (!assign_section_numbers (abfd))
2275 return false;
2277 /* The backend linker builds symbol table information itself. */
2278 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2280 /* Non-zero if doing a relocatable link. */
2281 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2283 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2284 return false;
2287 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2288 /* sh_name was set in prep_headers. */
2289 shstrtab_hdr->sh_type = SHT_STRTAB;
2290 shstrtab_hdr->sh_flags = 0;
2291 shstrtab_hdr->sh_addr = 0;
2292 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2293 shstrtab_hdr->sh_entsize = 0;
2294 shstrtab_hdr->sh_link = 0;
2295 shstrtab_hdr->sh_info = 0;
2296 /* sh_offset is set in assign_file_positions_except_relocs. */
2297 shstrtab_hdr->sh_addralign = 1;
2299 if (!assign_file_positions_except_relocs (abfd))
2300 return false;
2302 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2304 file_ptr off;
2305 Elf_Internal_Shdr *hdr;
2307 off = elf_tdata (abfd)->next_file_pos;
2309 hdr = &elf_tdata (abfd)->symtab_hdr;
2310 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2312 hdr = &elf_tdata (abfd)->strtab_hdr;
2313 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2315 elf_tdata (abfd)->next_file_pos = off;
2317 /* Now that we know where the .strtab section goes, write it
2318 out. */
2319 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2320 || ! _bfd_stringtab_emit (abfd, strtab))
2321 return false;
2322 _bfd_stringtab_free (strtab);
2325 abfd->output_has_begun = true;
2327 return true;
2330 /* Create a mapping from a set of sections to a program segment. */
2332 static INLINE struct elf_segment_map *
2333 make_mapping (abfd, sections, from, to, phdr)
2334 bfd *abfd;
2335 asection **sections;
2336 unsigned int from;
2337 unsigned int to;
2338 boolean phdr;
2340 struct elf_segment_map *m;
2341 unsigned int i;
2342 asection **hdrpp;
2344 m = ((struct elf_segment_map *)
2345 bfd_zalloc (abfd,
2346 (sizeof (struct elf_segment_map)
2347 + (to - from - 1) * sizeof (asection *))));
2348 if (m == NULL)
2349 return NULL;
2350 m->next = NULL;
2351 m->p_type = PT_LOAD;
2352 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2353 m->sections[i - from] = *hdrpp;
2354 m->count = to - from;
2356 if (from == 0 && phdr)
2358 /* Include the headers in the first PT_LOAD segment. */
2359 m->includes_filehdr = 1;
2360 m->includes_phdrs = 1;
2363 return m;
2366 /* Set up a mapping from BFD sections to program segments. */
2368 static boolean
2369 map_sections_to_segments (abfd)
2370 bfd *abfd;
2372 asection **sections = NULL;
2373 asection *s;
2374 unsigned int i;
2375 unsigned int count;
2376 struct elf_segment_map *mfirst;
2377 struct elf_segment_map **pm;
2378 struct elf_segment_map *m;
2379 asection *last_hdr;
2380 unsigned int phdr_index;
2381 bfd_vma maxpagesize;
2382 asection **hdrpp;
2383 boolean phdr_in_segment = true;
2384 boolean writable;
2385 asection *dynsec;
2387 if (elf_tdata (abfd)->segment_map != NULL)
2388 return true;
2390 if (bfd_count_sections (abfd) == 0)
2391 return true;
2393 /* Select the allocated sections, and sort them. */
2395 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2396 * sizeof (asection *));
2397 if (sections == NULL)
2398 goto error_return;
2400 i = 0;
2401 for (s = abfd->sections; s != NULL; s = s->next)
2403 if ((s->flags & SEC_ALLOC) != 0)
2405 sections[i] = s;
2406 ++i;
2409 BFD_ASSERT (i <= bfd_count_sections (abfd));
2410 count = i;
2412 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2414 /* Build the mapping. */
2416 mfirst = NULL;
2417 pm = &mfirst;
2419 /* If we have a .interp section, then create a PT_PHDR segment for
2420 the program headers and a PT_INTERP segment for the .interp
2421 section. */
2422 s = bfd_get_section_by_name (abfd, ".interp");
2423 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2425 m = ((struct elf_segment_map *)
2426 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2427 if (m == NULL)
2428 goto error_return;
2429 m->next = NULL;
2430 m->p_type = PT_PHDR;
2431 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2432 m->p_flags = PF_R | PF_X;
2433 m->p_flags_valid = 1;
2434 m->includes_phdrs = 1;
2436 *pm = m;
2437 pm = &m->next;
2439 m = ((struct elf_segment_map *)
2440 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2441 if (m == NULL)
2442 goto error_return;
2443 m->next = NULL;
2444 m->p_type = PT_INTERP;
2445 m->count = 1;
2446 m->sections[0] = s;
2448 *pm = m;
2449 pm = &m->next;
2452 /* Look through the sections. We put sections in the same program
2453 segment when the start of the second section can be placed within
2454 a few bytes of the end of the first section. */
2455 last_hdr = NULL;
2456 phdr_index = 0;
2457 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2458 writable = false;
2459 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2460 if (dynsec != NULL
2461 && (dynsec->flags & SEC_LOAD) == 0)
2462 dynsec = NULL;
2464 /* Deal with -Ttext or something similar such that the first section
2465 is not adjacent to the program headers. This is an
2466 approximation, since at this point we don't know exactly how many
2467 program headers we will need. */
2468 if (count > 0)
2470 bfd_size_type phdr_size;
2472 phdr_size = elf_tdata (abfd)->program_header_size;
2473 if (phdr_size == 0)
2474 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2475 if ((abfd->flags & D_PAGED) == 0
2476 || sections[0]->lma < phdr_size
2477 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2478 phdr_in_segment = false;
2481 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2483 asection *hdr;
2484 boolean new_segment;
2486 hdr = *hdrpp;
2488 /* See if this section and the last one will fit in the same
2489 segment. */
2491 if (last_hdr == NULL)
2493 /* If we don't have a segment yet, then we don't need a new
2494 one (we build the last one after this loop). */
2495 new_segment = false;
2497 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2499 /* If this section has a different relation between the
2500 virtual address and the load address, then we need a new
2501 segment. */
2502 new_segment = true;
2504 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2505 < BFD_ALIGN (hdr->lma, maxpagesize))
2507 /* If putting this section in this segment would force us to
2508 skip a page in the segment, then we need a new segment. */
2509 new_segment = true;
2511 else if ((last_hdr->flags & SEC_LOAD) == 0
2512 && (hdr->flags & SEC_LOAD) != 0)
2514 /* We don't want to put a loadable section after a
2515 nonloadable section in the same segment. */
2516 new_segment = true;
2518 else if ((abfd->flags & D_PAGED) == 0)
2520 /* If the file is not demand paged, which means that we
2521 don't require the sections to be correctly aligned in the
2522 file, then there is no other reason for a new segment. */
2523 new_segment = false;
2525 else if (! writable
2526 && (hdr->flags & SEC_READONLY) == 0
2527 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2528 == hdr->lma))
2530 /* We don't want to put a writable section in a read only
2531 segment, unless they are on the same page in memory
2532 anyhow. We already know that the last section does not
2533 bring us past the current section on the page, so the
2534 only case in which the new section is not on the same
2535 page as the previous section is when the previous section
2536 ends precisely on a page boundary. */
2537 new_segment = true;
2539 else
2541 /* Otherwise, we can use the same segment. */
2542 new_segment = false;
2545 if (! new_segment)
2547 if ((hdr->flags & SEC_READONLY) == 0)
2548 writable = true;
2549 last_hdr = hdr;
2550 continue;
2553 /* We need a new program segment. We must create a new program
2554 header holding all the sections from phdr_index until hdr. */
2556 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2557 if (m == NULL)
2558 goto error_return;
2560 *pm = m;
2561 pm = &m->next;
2563 if ((hdr->flags & SEC_READONLY) == 0)
2564 writable = true;
2565 else
2566 writable = false;
2568 last_hdr = hdr;
2569 phdr_index = i;
2570 phdr_in_segment = false;
2573 /* Create a final PT_LOAD program segment. */
2574 if (last_hdr != NULL)
2576 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2577 if (m == NULL)
2578 goto error_return;
2580 *pm = m;
2581 pm = &m->next;
2584 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2585 if (dynsec != NULL)
2587 m = ((struct elf_segment_map *)
2588 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2589 if (m == NULL)
2590 goto error_return;
2591 m->next = NULL;
2592 m->p_type = PT_DYNAMIC;
2593 m->count = 1;
2594 m->sections[0] = dynsec;
2596 *pm = m;
2597 pm = &m->next;
2600 /* For each loadable .note section, add a PT_NOTE segment. We don't
2601 use bfd_get_section_by_name, because if we link together
2602 nonloadable .note sections and loadable .note sections, we will
2603 generate two .note sections in the output file. FIXME: Using
2604 names for section types is bogus anyhow. */
2605 for (s = abfd->sections; s != NULL; s = s->next)
2607 if ((s->flags & SEC_LOAD) != 0
2608 && strncmp (s->name, ".note", 5) == 0)
2610 m = ((struct elf_segment_map *)
2611 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2612 if (m == NULL)
2613 goto error_return;
2614 m->next = NULL;
2615 m->p_type = PT_NOTE;
2616 m->count = 1;
2617 m->sections[0] = s;
2619 *pm = m;
2620 pm = &m->next;
2624 free (sections);
2625 sections = NULL;
2627 elf_tdata (abfd)->segment_map = mfirst;
2628 return true;
2630 error_return:
2631 if (sections != NULL)
2632 free (sections);
2633 return false;
2636 /* Sort sections by address. */
2638 static int
2639 elf_sort_sections (arg1, arg2)
2640 const PTR arg1;
2641 const PTR arg2;
2643 const asection *sec1 = *(const asection **) arg1;
2644 const asection *sec2 = *(const asection **) arg2;
2646 /* Sort by LMA first, since this is the address used to
2647 place the section into a segment. */
2648 if (sec1->lma < sec2->lma)
2649 return -1;
2650 else if (sec1->lma > sec2->lma)
2651 return 1;
2653 /* Then sort by VMA. Normally the LMA and the VMA will be
2654 the same, and this will do nothing. */
2655 if (sec1->vma < sec2->vma)
2656 return -1;
2657 else if (sec1->vma > sec2->vma)
2658 return 1;
2660 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2662 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2664 if (TOEND (sec1))
2666 if (TOEND (sec2))
2667 return sec1->target_index - sec2->target_index;
2668 else
2669 return 1;
2672 if (TOEND (sec2))
2673 return -1;
2675 #undef TOEND
2677 /* Sort by size, to put zero sized sections before others at the
2678 same address. */
2680 if (sec1->_raw_size < sec2->_raw_size)
2681 return -1;
2682 if (sec1->_raw_size > sec2->_raw_size)
2683 return 1;
2685 return sec1->target_index - sec2->target_index;
2688 /* Assign file positions to the sections based on the mapping from
2689 sections to segments. This function also sets up some fields in
2690 the file header, and writes out the program headers. */
2692 static boolean
2693 assign_file_positions_for_segments (abfd)
2694 bfd *abfd;
2696 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2697 unsigned int count;
2698 struct elf_segment_map *m;
2699 unsigned int alloc;
2700 Elf_Internal_Phdr *phdrs;
2701 file_ptr off, voff;
2702 bfd_vma filehdr_vaddr, filehdr_paddr;
2703 bfd_vma phdrs_vaddr, phdrs_paddr;
2704 Elf_Internal_Phdr *p;
2706 if (elf_tdata (abfd)->segment_map == NULL)
2708 if (! map_sections_to_segments (abfd))
2709 return false;
2712 if (bed->elf_backend_modify_segment_map)
2714 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2715 return false;
2718 count = 0;
2719 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2720 ++count;
2722 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2723 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2724 elf_elfheader (abfd)->e_phnum = count;
2726 if (count == 0)
2727 return true;
2729 /* If we already counted the number of program segments, make sure
2730 that we allocated enough space. This happens when SIZEOF_HEADERS
2731 is used in a linker script. */
2732 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2733 if (alloc != 0 && count > alloc)
2735 ((*_bfd_error_handler)
2736 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2737 bfd_get_filename (abfd), alloc, count));
2738 bfd_set_error (bfd_error_bad_value);
2739 return false;
2742 if (alloc == 0)
2743 alloc = count;
2745 phdrs = ((Elf_Internal_Phdr *)
2746 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2747 if (phdrs == NULL)
2748 return false;
2750 off = bed->s->sizeof_ehdr;
2751 off += alloc * bed->s->sizeof_phdr;
2753 filehdr_vaddr = 0;
2754 filehdr_paddr = 0;
2755 phdrs_vaddr = 0;
2756 phdrs_paddr = 0;
2758 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2759 m != NULL;
2760 m = m->next, p++)
2762 unsigned int i;
2763 asection **secpp;
2765 /* If elf_segment_map is not from map_sections_to_segments, the
2766 sections may not be correctly ordered. */
2767 if (m->count > 0)
2768 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2769 elf_sort_sections);
2771 p->p_type = m->p_type;
2772 p->p_flags = m->p_flags;
2774 if (p->p_type == PT_LOAD
2775 && m->count > 0
2776 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2778 if ((abfd->flags & D_PAGED) != 0)
2779 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2780 else
2782 bfd_size_type align;
2784 align = 0;
2785 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2787 bfd_size_type secalign;
2789 secalign = bfd_get_section_alignment (abfd, *secpp);
2790 if (secalign > align)
2791 align = secalign;
2794 off += (m->sections[0]->vma - off) % (1 << align);
2798 if (m->count == 0)
2799 p->p_vaddr = 0;
2800 else
2801 p->p_vaddr = m->sections[0]->vma;
2803 if (m->p_paddr_valid)
2804 p->p_paddr = m->p_paddr;
2805 else if (m->count == 0)
2806 p->p_paddr = 0;
2807 else
2808 p->p_paddr = m->sections[0]->lma;
2810 if (p->p_type == PT_LOAD
2811 && (abfd->flags & D_PAGED) != 0)
2812 p->p_align = bed->maxpagesize;
2813 else if (m->count == 0)
2814 p->p_align = bed->s->file_align;
2815 else
2816 p->p_align = 0;
2818 p->p_offset = 0;
2819 p->p_filesz = 0;
2820 p->p_memsz = 0;
2822 if (m->includes_filehdr)
2824 if (! m->p_flags_valid)
2825 p->p_flags |= PF_R;
2826 p->p_offset = 0;
2827 p->p_filesz = bed->s->sizeof_ehdr;
2828 p->p_memsz = bed->s->sizeof_ehdr;
2829 if (m->count > 0)
2831 BFD_ASSERT (p->p_type == PT_LOAD);
2833 if (p->p_vaddr < (bfd_vma) off)
2835 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2836 bfd_get_filename (abfd));
2837 bfd_set_error (bfd_error_bad_value);
2838 return false;
2841 p->p_vaddr -= off;
2842 if (! m->p_paddr_valid)
2843 p->p_paddr -= off;
2845 if (p->p_type == PT_LOAD)
2847 filehdr_vaddr = p->p_vaddr;
2848 filehdr_paddr = p->p_paddr;
2852 if (m->includes_phdrs)
2854 if (! m->p_flags_valid)
2855 p->p_flags |= PF_R;
2857 if (m->includes_filehdr)
2859 if (p->p_type == PT_LOAD)
2861 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2862 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2865 else
2867 p->p_offset = bed->s->sizeof_ehdr;
2869 if (m->count > 0)
2871 BFD_ASSERT (p->p_type == PT_LOAD);
2872 p->p_vaddr -= off - p->p_offset;
2873 if (! m->p_paddr_valid)
2874 p->p_paddr -= off - p->p_offset;
2877 if (p->p_type == PT_LOAD)
2879 phdrs_vaddr = p->p_vaddr;
2880 phdrs_paddr = p->p_paddr;
2882 else
2883 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2886 p->p_filesz += alloc * bed->s->sizeof_phdr;
2887 p->p_memsz += alloc * bed->s->sizeof_phdr;
2890 if (p->p_type == PT_LOAD
2891 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2893 if (! m->includes_filehdr && ! m->includes_phdrs)
2894 p->p_offset = off;
2895 else
2897 file_ptr adjust;
2899 adjust = off - (p->p_offset + p->p_filesz);
2900 p->p_filesz += adjust;
2901 p->p_memsz += adjust;
2905 voff = off;
2907 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2909 asection *sec;
2910 flagword flags;
2911 bfd_size_type align;
2913 sec = *secpp;
2914 flags = sec->flags;
2915 align = 1 << bfd_get_section_alignment (abfd, sec);
2917 /* The section may have artificial alignment forced by a
2918 link script. Notice this case by the gap between the
2919 cumulative phdr vma and the section's vma. */
2920 if (p->p_vaddr + p->p_memsz < sec->vma)
2922 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2924 p->p_memsz += adjust;
2925 off += adjust;
2926 voff += adjust;
2927 if ((flags & SEC_LOAD) != 0)
2928 p->p_filesz += adjust;
2931 if (p->p_type == PT_LOAD)
2933 bfd_signed_vma adjust;
2935 if ((flags & SEC_LOAD) != 0)
2937 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2938 if (adjust < 0)
2939 adjust = 0;
2941 else if ((flags & SEC_ALLOC) != 0)
2943 /* The section VMA must equal the file position
2944 modulo the page size. FIXME: I'm not sure if
2945 this adjustment is really necessary. We used to
2946 not have the SEC_LOAD case just above, and then
2947 this was necessary, but now I'm not sure. */
2948 if ((abfd->flags & D_PAGED) != 0)
2949 adjust = (sec->vma - voff) % bed->maxpagesize;
2950 else
2951 adjust = (sec->vma - voff) % align;
2953 else
2954 adjust = 0;
2956 if (adjust != 0)
2958 if (i == 0)
2960 (* _bfd_error_handler)
2961 (_("Error: First section in segment (%s) starts at 0x%x"),
2962 bfd_section_name (abfd, sec), sec->lma);
2963 (* _bfd_error_handler)
2964 (_(" whereas segment starts at 0x%x"),
2965 p->p_paddr);
2967 return false;
2969 p->p_memsz += adjust;
2970 off += adjust;
2971 voff += adjust;
2972 if ((flags & SEC_LOAD) != 0)
2973 p->p_filesz += adjust;
2976 sec->filepos = off;
2978 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2979 used in a linker script we may have a section with
2980 SEC_LOAD clear but which is supposed to have
2981 contents. */
2982 if ((flags & SEC_LOAD) != 0
2983 || (flags & SEC_HAS_CONTENTS) != 0)
2984 off += sec->_raw_size;
2986 if ((flags & SEC_ALLOC) != 0)
2987 voff += sec->_raw_size;
2990 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2992 /* The actual "note" segment has i == 0.
2993 This is the one that actually contains everything. */
2994 if (i == 0)
2996 sec->filepos = off;
2997 p->p_filesz = sec->_raw_size;
2998 off += sec->_raw_size;
2999 voff = off;
3001 else
3003 /* Fake sections -- don't need to be written. */
3004 sec->filepos = 0;
3005 sec->_raw_size = 0;
3006 flags = sec->flags = 0;
3008 p->p_memsz = 0;
3009 p->p_align = 1;
3011 else
3013 p->p_memsz += sec->_raw_size;
3015 if ((flags & SEC_LOAD) != 0)
3016 p->p_filesz += sec->_raw_size;
3018 if (align > p->p_align
3019 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
3020 p->p_align = align;
3023 if (! m->p_flags_valid)
3025 p->p_flags |= PF_R;
3026 if ((flags & SEC_CODE) != 0)
3027 p->p_flags |= PF_X;
3028 if ((flags & SEC_READONLY) == 0)
3029 p->p_flags |= PF_W;
3034 /* Now that we have set the section file positions, we can set up
3035 the file positions for the non PT_LOAD segments. */
3036 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
3037 m != NULL;
3038 m = m->next, p++)
3040 if (p->p_type != PT_LOAD && m->count > 0)
3042 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
3043 p->p_offset = m->sections[0]->filepos;
3045 if (m->count == 0)
3047 if (m->includes_filehdr)
3049 p->p_vaddr = filehdr_vaddr;
3050 if (! m->p_paddr_valid)
3051 p->p_paddr = filehdr_paddr;
3053 else if (m->includes_phdrs)
3055 p->p_vaddr = phdrs_vaddr;
3056 if (! m->p_paddr_valid)
3057 p->p_paddr = phdrs_paddr;
3062 /* Clear out any program headers we allocated but did not use. */
3063 for (; count < alloc; count++, p++)
3065 memset (p, 0, sizeof *p);
3066 p->p_type = PT_NULL;
3069 elf_tdata (abfd)->phdr = phdrs;
3071 elf_tdata (abfd)->next_file_pos = off;
3073 /* Write out the program headers. */
3074 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3075 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3076 return false;
3078 return true;
3081 /* Get the size of the program header.
3083 If this is called by the linker before any of the section VMA's are set, it
3084 can't calculate the correct value for a strange memory layout. This only
3085 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3086 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3087 data segment (exclusive of .interp and .dynamic).
3089 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3090 will be two segments. */
3092 static bfd_size_type
3093 get_program_header_size (abfd)
3094 bfd *abfd;
3096 size_t segs;
3097 asection *s;
3098 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3100 /* We can't return a different result each time we're called. */
3101 if (elf_tdata (abfd)->program_header_size != 0)
3102 return elf_tdata (abfd)->program_header_size;
3104 if (elf_tdata (abfd)->segment_map != NULL)
3106 struct elf_segment_map *m;
3108 segs = 0;
3109 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3110 ++segs;
3111 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3112 return elf_tdata (abfd)->program_header_size;
3115 /* Assume we will need exactly two PT_LOAD segments: one for text
3116 and one for data. */
3117 segs = 2;
3119 s = bfd_get_section_by_name (abfd, ".interp");
3120 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3122 /* If we have a loadable interpreter section, we need a
3123 PT_INTERP segment. In this case, assume we also need a
3124 PT_PHDR segment, although that may not be true for all
3125 targets. */
3126 segs += 2;
3129 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3131 /* We need a PT_DYNAMIC segment. */
3132 ++segs;
3135 for (s = abfd->sections; s != NULL; s = s->next)
3137 if ((s->flags & SEC_LOAD) != 0
3138 && strncmp (s->name, ".note", 5) == 0)
3140 /* We need a PT_NOTE segment. */
3141 ++segs;
3145 /* Let the backend count up any program headers it might need. */
3146 if (bed->elf_backend_additional_program_headers)
3148 int a;
3150 a = (*bed->elf_backend_additional_program_headers) (abfd);
3151 if (a == -1)
3152 abort ();
3153 segs += a;
3156 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3157 return elf_tdata (abfd)->program_header_size;
3160 /* Work out the file positions of all the sections. This is called by
3161 _bfd_elf_compute_section_file_positions. All the section sizes and
3162 VMAs must be known before this is called.
3164 We do not consider reloc sections at this point, unless they form
3165 part of the loadable image. Reloc sections are assigned file
3166 positions in assign_file_positions_for_relocs, which is called by
3167 write_object_contents and final_link.
3169 We also don't set the positions of the .symtab and .strtab here. */
3171 static boolean
3172 assign_file_positions_except_relocs (abfd)
3173 bfd *abfd;
3175 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3176 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3177 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3178 file_ptr off;
3179 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3181 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3182 && bfd_get_format (abfd) != bfd_core)
3184 Elf_Internal_Shdr **hdrpp;
3185 unsigned int i;
3187 /* Start after the ELF header. */
3188 off = i_ehdrp->e_ehsize;
3190 /* We are not creating an executable, which means that we are
3191 not creating a program header, and that the actual order of
3192 the sections in the file is unimportant. */
3193 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3195 Elf_Internal_Shdr *hdr;
3197 hdr = *hdrpp;
3198 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3200 hdr->sh_offset = -1;
3201 continue;
3203 if (i == tdata->symtab_section
3204 || i == tdata->strtab_section)
3206 hdr->sh_offset = -1;
3207 continue;
3210 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3213 else
3215 unsigned int i;
3216 Elf_Internal_Shdr **hdrpp;
3218 /* Assign file positions for the loaded sections based on the
3219 assignment of sections to segments. */
3220 if (! assign_file_positions_for_segments (abfd))
3221 return false;
3223 /* Assign file positions for the other sections. */
3225 off = elf_tdata (abfd)->next_file_pos;
3226 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3228 Elf_Internal_Shdr *hdr;
3230 hdr = *hdrpp;
3231 if (hdr->bfd_section != NULL
3232 && hdr->bfd_section->filepos != 0)
3233 hdr->sh_offset = hdr->bfd_section->filepos;
3234 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3236 ((*_bfd_error_handler)
3237 (_("%s: warning: allocated section `%s' not in segment"),
3238 bfd_get_filename (abfd),
3239 (hdr->bfd_section == NULL
3240 ? "*unknown*"
3241 : hdr->bfd_section->name)));
3242 if ((abfd->flags & D_PAGED) != 0)
3243 off += (hdr->sh_addr - off) % bed->maxpagesize;
3244 else
3245 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3246 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3247 false);
3249 else if (hdr->sh_type == SHT_REL
3250 || hdr->sh_type == SHT_RELA
3251 || hdr == i_shdrpp[tdata->symtab_section]
3252 || hdr == i_shdrpp[tdata->strtab_section])
3253 hdr->sh_offset = -1;
3254 else
3255 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3259 /* Place the section headers. */
3260 off = align_file_position (off, bed->s->file_align);
3261 i_ehdrp->e_shoff = off;
3262 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3264 elf_tdata (abfd)->next_file_pos = off;
3266 return true;
3269 static boolean
3270 prep_headers (abfd)
3271 bfd *abfd;
3273 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3274 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3275 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3276 int count;
3277 struct bfd_strtab_hash *shstrtab;
3278 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3280 i_ehdrp = elf_elfheader (abfd);
3281 i_shdrp = elf_elfsections (abfd);
3283 shstrtab = _bfd_elf_stringtab_init ();
3284 if (shstrtab == NULL)
3285 return false;
3287 elf_shstrtab (abfd) = shstrtab;
3289 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3290 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3291 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3292 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3294 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3295 i_ehdrp->e_ident[EI_DATA] =
3296 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3297 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3299 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3300 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3302 for (count = EI_PAD; count < EI_NIDENT; count++)
3303 i_ehdrp->e_ident[count] = 0;
3305 if ((abfd->flags & DYNAMIC) != 0)
3306 i_ehdrp->e_type = ET_DYN;
3307 else if ((abfd->flags & EXEC_P) != 0)
3308 i_ehdrp->e_type = ET_EXEC;
3309 else if (bfd_get_format (abfd) == bfd_core)
3310 i_ehdrp->e_type = ET_CORE;
3311 else
3312 i_ehdrp->e_type = ET_REL;
3314 switch (bfd_get_arch (abfd))
3316 case bfd_arch_unknown:
3317 i_ehdrp->e_machine = EM_NONE;
3318 break;
3319 case bfd_arch_sparc:
3320 if (bfd_get_arch_size (abfd) == 64)
3321 i_ehdrp->e_machine = EM_SPARCV9;
3322 else
3323 i_ehdrp->e_machine = EM_SPARC;
3324 break;
3325 case bfd_arch_i370:
3326 i_ehdrp->e_machine = EM_S370;
3327 break;
3328 case bfd_arch_i386:
3329 if (bfd_get_arch_size (abfd) == 64)
3330 i_ehdrp->e_machine = EM_X86_64;
3331 else
3332 i_ehdrp->e_machine = EM_386;
3333 break;
3334 case bfd_arch_ia64:
3335 i_ehdrp->e_machine = EM_IA_64;
3336 break;
3337 case bfd_arch_m68hc11:
3338 i_ehdrp->e_machine = EM_68HC11;
3339 break;
3340 case bfd_arch_m68hc12:
3341 i_ehdrp->e_machine = EM_68HC12;
3342 break;
3343 case bfd_arch_s390:
3344 i_ehdrp->e_machine = EM_S390;
3345 break;
3346 case bfd_arch_m68k:
3347 i_ehdrp->e_machine = EM_68K;
3348 break;
3349 case bfd_arch_m88k:
3350 i_ehdrp->e_machine = EM_88K;
3351 break;
3352 case bfd_arch_i860:
3353 i_ehdrp->e_machine = EM_860;
3354 break;
3355 case bfd_arch_i960:
3356 i_ehdrp->e_machine = EM_960;
3357 break;
3358 case bfd_arch_mips: /* MIPS Rxxxx */
3359 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3360 break;
3361 case bfd_arch_hppa:
3362 i_ehdrp->e_machine = EM_PARISC;
3363 break;
3364 case bfd_arch_powerpc:
3365 i_ehdrp->e_machine = EM_PPC;
3366 break;
3367 case bfd_arch_alpha:
3368 i_ehdrp->e_machine = EM_ALPHA;
3369 break;
3370 case bfd_arch_sh:
3371 i_ehdrp->e_machine = EM_SH;
3372 break;
3373 case bfd_arch_d10v:
3374 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3375 break;
3376 case bfd_arch_d30v:
3377 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3378 break;
3379 case bfd_arch_fr30:
3380 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3381 break;
3382 case bfd_arch_mcore:
3383 i_ehdrp->e_machine = EM_MCORE;
3384 break;
3385 case bfd_arch_avr:
3386 i_ehdrp->e_machine = EM_AVR;
3387 break;
3388 case bfd_arch_v850:
3389 switch (bfd_get_mach (abfd))
3391 default:
3392 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3394 break;
3395 case bfd_arch_arc:
3396 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3397 break;
3398 case bfd_arch_arm:
3399 i_ehdrp->e_machine = EM_ARM;
3400 break;
3401 case bfd_arch_m32r:
3402 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3403 break;
3404 case bfd_arch_mn10200:
3405 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3406 break;
3407 case bfd_arch_mn10300:
3408 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3409 break;
3410 case bfd_arch_pj:
3411 i_ehdrp->e_machine = EM_PJ;
3412 break;
3413 case bfd_arch_cris:
3414 i_ehdrp->e_machine = EM_CRIS;
3415 break;
3416 case bfd_arch_openrisc:
3417 i_ehdrp->e_machine = EM_OPENRISC;
3418 break;
3419 /* Also note that EM_M32, AT&T WE32100 is unknown to bfd. */
3420 default:
3421 i_ehdrp->e_machine = EM_NONE;
3423 i_ehdrp->e_version = bed->s->ev_current;
3424 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3426 /* No program header, for now. */
3427 i_ehdrp->e_phoff = 0;
3428 i_ehdrp->e_phentsize = 0;
3429 i_ehdrp->e_phnum = 0;
3431 /* Each bfd section is section header entry. */
3432 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3433 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3435 /* If we're building an executable, we'll need a program header table. */
3436 if (abfd->flags & EXEC_P)
3438 /* It all happens later. */
3439 #if 0
3440 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3442 /* elf_build_phdrs() returns a (NULL-terminated) array of
3443 Elf_Internal_Phdrs. */
3444 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3445 i_ehdrp->e_phoff = outbase;
3446 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3447 #endif
3449 else
3451 i_ehdrp->e_phentsize = 0;
3452 i_phdrp = 0;
3453 i_ehdrp->e_phoff = 0;
3456 elf_tdata (abfd)->symtab_hdr.sh_name =
3457 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3458 elf_tdata (abfd)->strtab_hdr.sh_name =
3459 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3460 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3461 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3462 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3463 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3464 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3465 return false;
3467 return true;
3470 /* Assign file positions for all the reloc sections which are not part
3471 of the loadable file image. */
3473 void
3474 _bfd_elf_assign_file_positions_for_relocs (abfd)
3475 bfd *abfd;
3477 file_ptr off;
3478 unsigned int i;
3479 Elf_Internal_Shdr **shdrpp;
3481 off = elf_tdata (abfd)->next_file_pos;
3483 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3484 i < elf_elfheader (abfd)->e_shnum;
3485 i++, shdrpp++)
3487 Elf_Internal_Shdr *shdrp;
3489 shdrp = *shdrpp;
3490 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3491 && shdrp->sh_offset == -1)
3492 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3495 elf_tdata (abfd)->next_file_pos = off;
3498 boolean
3499 _bfd_elf_write_object_contents (abfd)
3500 bfd *abfd;
3502 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3503 Elf_Internal_Ehdr *i_ehdrp;
3504 Elf_Internal_Shdr **i_shdrp;
3505 boolean failed;
3506 unsigned int count;
3508 if (! abfd->output_has_begun
3509 && ! _bfd_elf_compute_section_file_positions
3510 (abfd, (struct bfd_link_info *) NULL))
3511 return false;
3513 i_shdrp = elf_elfsections (abfd);
3514 i_ehdrp = elf_elfheader (abfd);
3516 failed = false;
3517 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3518 if (failed)
3519 return false;
3521 _bfd_elf_assign_file_positions_for_relocs (abfd);
3523 /* After writing the headers, we need to write the sections too... */
3524 for (count = 1; count < i_ehdrp->e_shnum; count++)
3526 if (bed->elf_backend_section_processing)
3527 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3528 if (i_shdrp[count]->contents)
3530 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3531 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3532 1, abfd)
3533 != i_shdrp[count]->sh_size))
3534 return false;
3538 /* Write out the section header names. */
3539 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3540 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3541 return false;
3543 if (bed->elf_backend_final_write_processing)
3544 (*bed->elf_backend_final_write_processing) (abfd,
3545 elf_tdata (abfd)->linker);
3547 return bed->s->write_shdrs_and_ehdr (abfd);
3550 boolean
3551 _bfd_elf_write_corefile_contents (abfd)
3552 bfd *abfd;
3554 /* Hopefully this can be done just like an object file. */
3555 return _bfd_elf_write_object_contents (abfd);
3558 /* Given a section, search the header to find them. */
3561 _bfd_elf_section_from_bfd_section (abfd, asect)
3562 bfd *abfd;
3563 struct sec *asect;
3565 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3566 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3567 int index;
3568 Elf_Internal_Shdr *hdr;
3569 int maxindex = elf_elfheader (abfd)->e_shnum;
3571 for (index = 0; index < maxindex; index++)
3573 hdr = i_shdrp[index];
3574 if (hdr->bfd_section == asect)
3575 return index;
3578 if (bed->elf_backend_section_from_bfd_section)
3580 for (index = 0; index < maxindex; index++)
3582 int retval;
3584 hdr = i_shdrp[index];
3585 retval = index;
3586 if ((*bed->elf_backend_section_from_bfd_section)
3587 (abfd, hdr, asect, &retval))
3588 return retval;
3592 if (bfd_is_abs_section (asect))
3593 return SHN_ABS;
3594 if (bfd_is_com_section (asect))
3595 return SHN_COMMON;
3596 if (bfd_is_und_section (asect))
3597 return SHN_UNDEF;
3599 bfd_set_error (bfd_error_nonrepresentable_section);
3601 return -1;
3604 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3605 on error. */
3608 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3609 bfd *abfd;
3610 asymbol **asym_ptr_ptr;
3612 asymbol *asym_ptr = *asym_ptr_ptr;
3613 int idx;
3614 flagword flags = asym_ptr->flags;
3616 /* When gas creates relocations against local labels, it creates its
3617 own symbol for the section, but does put the symbol into the
3618 symbol chain, so udata is 0. When the linker is generating
3619 relocatable output, this section symbol may be for one of the
3620 input sections rather than the output section. */
3621 if (asym_ptr->udata.i == 0
3622 && (flags & BSF_SECTION_SYM)
3623 && asym_ptr->section)
3625 int indx;
3627 if (asym_ptr->section->output_section != NULL)
3628 indx = asym_ptr->section->output_section->index;
3629 else
3630 indx = asym_ptr->section->index;
3631 if (elf_section_syms (abfd)[indx])
3632 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3635 idx = asym_ptr->udata.i;
3637 if (idx == 0)
3639 /* This case can occur when using --strip-symbol on a symbol
3640 which is used in a relocation entry. */
3641 (*_bfd_error_handler)
3642 (_("%s: symbol `%s' required but not present"),
3643 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3644 bfd_set_error (bfd_error_no_symbols);
3645 return -1;
3648 #if DEBUG & 4
3650 fprintf (stderr,
3651 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3652 (long) asym_ptr, asym_ptr->name, idx, flags,
3653 elf_symbol_flags (flags));
3654 fflush (stderr);
3656 #endif
3658 return idx;
3661 /* Copy private BFD data. This copies any program header information. */
3663 static boolean
3664 copy_private_bfd_data (ibfd, obfd)
3665 bfd *ibfd;
3666 bfd *obfd;
3668 Elf_Internal_Ehdr * iehdr;
3669 struct elf_segment_map * map;
3670 struct elf_segment_map * map_first;
3671 struct elf_segment_map ** pointer_to_map;
3672 Elf_Internal_Phdr * segment;
3673 asection * section;
3674 unsigned int i;
3675 unsigned int num_segments;
3676 boolean phdr_included = false;
3677 bfd_vma maxpagesize;
3678 struct elf_segment_map * phdr_adjust_seg = NULL;
3679 unsigned int phdr_adjust_num = 0;
3681 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3682 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3683 return true;
3685 if (elf_tdata (ibfd)->phdr == NULL)
3686 return true;
3688 iehdr = elf_elfheader (ibfd);
3690 map_first = NULL;
3691 pointer_to_map = &map_first;
3693 num_segments = elf_elfheader (ibfd)->e_phnum;
3694 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3696 /* Returns the end address of the segment + 1. */
3697 #define SEGMENT_END(segment, start) \
3698 (start + (segment->p_memsz > segment->p_filesz \
3699 ? segment->p_memsz : segment->p_filesz))
3701 /* Returns true if the given section is contained within
3702 the given segment. VMA addresses are compared. */
3703 #define IS_CONTAINED_BY_VMA(section, segment) \
3704 (section->vma >= segment->p_vaddr \
3705 && (section->vma + section->_raw_size) \
3706 <= (SEGMENT_END (segment, segment->p_vaddr)))
3708 /* Returns true if the given section is contained within
3709 the given segment. LMA addresses are compared. */
3710 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3711 (section->lma >= base \
3712 && (section->lma + section->_raw_size) \
3713 <= SEGMENT_END (segment, base))
3715 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3716 #define IS_COREFILE_NOTE(p, s) \
3717 (p->p_type == PT_NOTE \
3718 && bfd_get_format (ibfd) == bfd_core \
3719 && s->vma == 0 && s->lma == 0 \
3720 && (bfd_vma) s->filepos >= p->p_offset \
3721 && (bfd_vma) s->filepos + s->_raw_size \
3722 <= p->p_offset + p->p_filesz)
3724 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3725 linker, which generates a PT_INTERP section with p_vaddr and
3726 p_memsz set to 0. */
3727 #define IS_SOLARIS_PT_INTERP(p, s) \
3728 ( p->p_vaddr == 0 \
3729 && p->p_filesz > 0 \
3730 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3731 && s->_raw_size > 0 \
3732 && (bfd_vma) s->filepos >= p->p_offset \
3733 && ((bfd_vma) s->filepos + s->_raw_size \
3734 <= p->p_offset + p->p_filesz))
3736 /* Decide if the given section should be included in the given segment.
3737 A section will be included if:
3738 1. It is within the address space of the segment,
3739 2. It is an allocated segment,
3740 3. There is an output section associated with it,
3741 4. The section has not already been allocated to a previous segment. */
3742 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3743 ((((IS_CONTAINED_BY_VMA (section, segment) \
3744 || IS_SOLARIS_PT_INTERP (segment, section)) \
3745 && (section->flags & SEC_ALLOC) != 0) \
3746 || IS_COREFILE_NOTE (segment, section)) \
3747 && section->output_section != NULL \
3748 && section->segment_mark == false)
3750 /* Returns true iff seg1 starts after the end of seg2. */
3751 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3752 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3754 /* Returns true iff seg1 and seg2 overlap. */
3755 #define SEGMENT_OVERLAPS(seg1, seg2) \
3756 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3758 /* Initialise the segment mark field. */
3759 for (section = ibfd->sections; section != NULL; section = section->next)
3760 section->segment_mark = false;
3762 /* Scan through the segments specified in the program header
3763 of the input BFD. For this first scan we look for overlaps
3764 in the loadable segments. These can be created by wierd
3765 parameters to objcopy. */
3766 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3767 i < num_segments;
3768 i++, segment++)
3770 unsigned int j;
3771 Elf_Internal_Phdr *segment2;
3773 if (segment->p_type != PT_LOAD)
3774 continue;
3776 /* Determine if this segment overlaps any previous segments. */
3777 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3779 bfd_signed_vma extra_length;
3781 if (segment2->p_type != PT_LOAD
3782 || ! SEGMENT_OVERLAPS (segment, segment2))
3783 continue;
3785 /* Merge the two segments together. */
3786 if (segment2->p_vaddr < segment->p_vaddr)
3788 /* Extend SEGMENT2 to include SEGMENT and then delete
3789 SEGMENT. */
3790 extra_length =
3791 SEGMENT_END (segment, segment->p_vaddr)
3792 - SEGMENT_END (segment2, segment2->p_vaddr);
3794 if (extra_length > 0)
3796 segment2->p_memsz += extra_length;
3797 segment2->p_filesz += extra_length;
3800 segment->p_type = PT_NULL;
3802 /* Since we have deleted P we must restart the outer loop. */
3803 i = 0;
3804 segment = elf_tdata (ibfd)->phdr;
3805 break;
3807 else
3809 /* Extend SEGMENT to include SEGMENT2 and then delete
3810 SEGMENT2. */
3811 extra_length =
3812 SEGMENT_END (segment2, segment2->p_vaddr)
3813 - SEGMENT_END (segment, segment->p_vaddr);
3815 if (extra_length > 0)
3817 segment->p_memsz += extra_length;
3818 segment->p_filesz += extra_length;
3821 segment2->p_type = PT_NULL;
3826 /* The second scan attempts to assign sections to segments. */
3827 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3828 i < num_segments;
3829 i ++, segment ++)
3831 unsigned int section_count;
3832 asection ** sections;
3833 asection * output_section;
3834 unsigned int isec;
3835 bfd_vma matching_lma;
3836 bfd_vma suggested_lma;
3837 unsigned int j;
3839 if (segment->p_type == PT_NULL)
3840 continue;
3842 /* Compute how many sections might be placed into this segment. */
3843 section_count = 0;
3844 for (section = ibfd->sections; section != NULL; section = section->next)
3845 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3846 ++section_count;
3848 /* Allocate a segment map big enough to contain all of the
3849 sections we have selected. */
3850 map = ((struct elf_segment_map *)
3851 bfd_alloc (obfd,
3852 (sizeof (struct elf_segment_map)
3853 + ((size_t) section_count - 1) * sizeof (asection *))));
3854 if (map == NULL)
3855 return false;
3857 /* Initialise the fields of the segment map. Default to
3858 using the physical address of the segment in the input BFD. */
3859 map->next = NULL;
3860 map->p_type = segment->p_type;
3861 map->p_flags = segment->p_flags;
3862 map->p_flags_valid = 1;
3863 map->p_paddr = segment->p_paddr;
3864 map->p_paddr_valid = 1;
3866 /* Determine if this segment contains the ELF file header
3867 and if it contains the program headers themselves. */
3868 map->includes_filehdr = (segment->p_offset == 0
3869 && segment->p_filesz >= iehdr->e_ehsize);
3871 map->includes_phdrs = 0;
3873 if (! phdr_included || segment->p_type != PT_LOAD)
3875 map->includes_phdrs =
3876 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3877 && (segment->p_offset + segment->p_filesz
3878 >= ((bfd_vma) iehdr->e_phoff
3879 + iehdr->e_phnum * iehdr->e_phentsize)));
3881 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3882 phdr_included = true;
3885 if (section_count == 0)
3887 /* Special segments, such as the PT_PHDR segment, may contain
3888 no sections, but ordinary, loadable segments should contain
3889 something. */
3890 if (segment->p_type == PT_LOAD)
3891 _bfd_error_handler
3892 (_("%s: warning: Empty loadable segment detected\n"),
3893 bfd_get_filename (ibfd));
3895 map->count = 0;
3896 *pointer_to_map = map;
3897 pointer_to_map = &map->next;
3899 continue;
3902 /* Now scan the sections in the input BFD again and attempt
3903 to add their corresponding output sections to the segment map.
3904 The problem here is how to handle an output section which has
3905 been moved (ie had its LMA changed). There are four possibilities:
3907 1. None of the sections have been moved.
3908 In this case we can continue to use the segment LMA from the
3909 input BFD.
3911 2. All of the sections have been moved by the same amount.
3912 In this case we can change the segment's LMA to match the LMA
3913 of the first section.
3915 3. Some of the sections have been moved, others have not.
3916 In this case those sections which have not been moved can be
3917 placed in the current segment which will have to have its size,
3918 and possibly its LMA changed, and a new segment or segments will
3919 have to be created to contain the other sections.
3921 4. The sections have been moved, but not be the same amount.
3922 In this case we can change the segment's LMA to match the LMA
3923 of the first section and we will have to create a new segment
3924 or segments to contain the other sections.
3926 In order to save time, we allocate an array to hold the section
3927 pointers that we are interested in. As these sections get assigned
3928 to a segment, they are removed from this array. */
3930 sections = (asection **) bfd_malloc
3931 (sizeof (asection *) * section_count);
3932 if (sections == NULL)
3933 return false;
3935 /* Step One: Scan for segment vs section LMA conflicts.
3936 Also add the sections to the section array allocated above.
3937 Also add the sections to the current segment. In the common
3938 case, where the sections have not been moved, this means that
3939 we have completely filled the segment, and there is nothing
3940 more to do. */
3941 isec = 0;
3942 matching_lma = 0;
3943 suggested_lma = 0;
3945 for (j = 0, section = ibfd->sections;
3946 section != NULL;
3947 section = section->next)
3949 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3951 output_section = section->output_section;
3953 sections[j ++] = section;
3955 /* The Solaris native linker always sets p_paddr to 0.
3956 We try to catch that case here, and set it to the
3957 correct value. */
3958 if (segment->p_paddr == 0
3959 && segment->p_vaddr != 0
3960 && isec == 0
3961 && output_section->lma != 0
3962 && (output_section->vma == (segment->p_vaddr
3963 + (map->includes_filehdr
3964 ? iehdr->e_ehsize
3965 : 0)
3966 + (map->includes_phdrs
3967 ? iehdr->e_phnum * iehdr->e_phentsize
3968 : 0))))
3969 map->p_paddr = segment->p_vaddr;
3971 /* Match up the physical address of the segment with the
3972 LMA address of the output section. */
3973 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3974 || IS_COREFILE_NOTE (segment, section))
3976 if (matching_lma == 0)
3977 matching_lma = output_section->lma;
3979 /* We assume that if the section fits within the segment
3980 then it does not overlap any other section within that
3981 segment. */
3982 map->sections[isec ++] = output_section;
3984 else if (suggested_lma == 0)
3985 suggested_lma = output_section->lma;
3989 BFD_ASSERT (j == section_count);
3991 /* Step Two: Adjust the physical address of the current segment,
3992 if necessary. */
3993 if (isec == section_count)
3995 /* All of the sections fitted within the segment as currently
3996 specified. This is the default case. Add the segment to
3997 the list of built segments and carry on to process the next
3998 program header in the input BFD. */
3999 map->count = section_count;
4000 *pointer_to_map = map;
4001 pointer_to_map = &map->next;
4003 free (sections);
4004 continue;
4006 else
4008 if (matching_lma != 0)
4010 /* At least one section fits inside the current segment.
4011 Keep it, but modify its physical address to match the
4012 LMA of the first section that fitted. */
4013 map->p_paddr = matching_lma;
4015 else
4017 /* None of the sections fitted inside the current segment.
4018 Change the current segment's physical address to match
4019 the LMA of the first section. */
4020 map->p_paddr = suggested_lma;
4023 /* Offset the segment physical address from the lma
4024 to allow for space taken up by elf headers. */
4025 if (map->includes_filehdr)
4026 map->p_paddr -= iehdr->e_ehsize;
4028 if (map->includes_phdrs)
4030 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4032 /* iehdr->e_phnum is just an estimate of the number
4033 of program headers that we will need. Make a note
4034 here of the number we used and the segment we chose
4035 to hold these headers, so that we can adjust the
4036 offset when we know the correct value. */
4037 phdr_adjust_num = iehdr->e_phnum;
4038 phdr_adjust_seg = map;
4042 /* Step Three: Loop over the sections again, this time assigning
4043 those that fit to the current segment and remvoing them from the
4044 sections array; but making sure not to leave large gaps. Once all
4045 possible sections have been assigned to the current segment it is
4046 added to the list of built segments and if sections still remain
4047 to be assigned, a new segment is constructed before repeating
4048 the loop. */
4049 isec = 0;
4052 map->count = 0;
4053 suggested_lma = 0;
4055 /* Fill the current segment with sections that fit. */
4056 for (j = 0; j < section_count; j++)
4058 section = sections[j];
4060 if (section == NULL)
4061 continue;
4063 output_section = section->output_section;
4065 BFD_ASSERT (output_section != NULL);
4067 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4068 || IS_COREFILE_NOTE (segment, section))
4070 if (map->count == 0)
4072 /* If the first section in a segment does not start at
4073 the beginning of the segment, then something is
4074 wrong. */
4075 if (output_section->lma !=
4076 (map->p_paddr
4077 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4078 + (map->includes_phdrs
4079 ? iehdr->e_phnum * iehdr->e_phentsize
4080 : 0)))
4081 abort ();
4083 else
4085 asection * prev_sec;
4087 prev_sec = map->sections[map->count - 1];
4089 /* If the gap between the end of the previous section
4090 and the start of this section is more than
4091 maxpagesize then we need to start a new segment. */
4092 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4093 < BFD_ALIGN (output_section->lma, maxpagesize))
4094 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4096 if (suggested_lma == 0)
4097 suggested_lma = output_section->lma;
4099 continue;
4103 map->sections[map->count++] = output_section;
4104 ++isec;
4105 sections[j] = NULL;
4106 section->segment_mark = true;
4108 else if (suggested_lma == 0)
4109 suggested_lma = output_section->lma;
4112 BFD_ASSERT (map->count > 0);
4114 /* Add the current segment to the list of built segments. */
4115 *pointer_to_map = map;
4116 pointer_to_map = &map->next;
4118 if (isec < section_count)
4120 /* We still have not allocated all of the sections to
4121 segments. Create a new segment here, initialise it
4122 and carry on looping. */
4123 map = ((struct elf_segment_map *)
4124 bfd_alloc (obfd,
4125 (sizeof (struct elf_segment_map)
4126 + ((size_t) section_count - 1)
4127 * sizeof (asection *))));
4128 if (map == NULL)
4129 return false;
4131 /* Initialise the fields of the segment map. Set the physical
4132 physical address to the LMA of the first section that has
4133 not yet been assigned. */
4134 map->next = NULL;
4135 map->p_type = segment->p_type;
4136 map->p_flags = segment->p_flags;
4137 map->p_flags_valid = 1;
4138 map->p_paddr = suggested_lma;
4139 map->p_paddr_valid = 1;
4140 map->includes_filehdr = 0;
4141 map->includes_phdrs = 0;
4144 while (isec < section_count);
4146 free (sections);
4149 /* The Solaris linker creates program headers in which all the
4150 p_paddr fields are zero. When we try to objcopy or strip such a
4151 file, we get confused. Check for this case, and if we find it
4152 reset the p_paddr_valid fields. */
4153 for (map = map_first; map != NULL; map = map->next)
4154 if (map->p_paddr != 0)
4155 break;
4156 if (map == NULL)
4158 for (map = map_first; map != NULL; map = map->next)
4159 map->p_paddr_valid = 0;
4162 elf_tdata (obfd)->segment_map = map_first;
4164 /* If we had to estimate the number of program headers that were
4165 going to be needed, then check our estimate know and adjust
4166 the offset if necessary. */
4167 if (phdr_adjust_seg != NULL)
4169 unsigned int count;
4171 for (count = 0, map = map_first; map != NULL; map = map->next)
4172 count++;
4174 if (count > phdr_adjust_num)
4175 phdr_adjust_seg->p_paddr
4176 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4179 #if 0
4180 /* Final Step: Sort the segments into ascending order of physical
4181 address. */
4182 if (map_first != NULL)
4184 struct elf_segment_map *prev;
4186 prev = map_first;
4187 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4189 /* Yes I know - its a bubble sort.... */
4190 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4192 /* Swap map and map->next. */
4193 prev->next = map->next;
4194 map->next = map->next->next;
4195 prev->next->next = map;
4197 /* Restart loop. */
4198 map = map_first;
4202 #endif
4204 #undef SEGMENT_END
4205 #undef IS_CONTAINED_BY_VMA
4206 #undef IS_CONTAINED_BY_LMA
4207 #undef IS_COREFILE_NOTE
4208 #undef IS_SOLARIS_PT_INTERP
4209 #undef INCLUDE_SECTION_IN_SEGMENT
4210 #undef SEGMENT_AFTER_SEGMENT
4211 #undef SEGMENT_OVERLAPS
4212 return true;
4215 /* Copy private section information. This copies over the entsize
4216 field, and sometimes the info field. */
4218 boolean
4219 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4220 bfd *ibfd;
4221 asection *isec;
4222 bfd *obfd;
4223 asection *osec;
4225 Elf_Internal_Shdr *ihdr, *ohdr;
4227 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4228 || obfd->xvec->flavour != bfd_target_elf_flavour)
4229 return true;
4231 /* Copy over private BFD data if it has not already been copied.
4232 This must be done here, rather than in the copy_private_bfd_data
4233 entry point, because the latter is called after the section
4234 contents have been set, which means that the program headers have
4235 already been worked out. */
4236 if (elf_tdata (obfd)->segment_map == NULL
4237 && elf_tdata (ibfd)->phdr != NULL)
4239 asection *s;
4241 /* Only set up the segments if there are no more SEC_ALLOC
4242 sections. FIXME: This won't do the right thing if objcopy is
4243 used to remove the last SEC_ALLOC section, since objcopy
4244 won't call this routine in that case. */
4245 for (s = isec->next; s != NULL; s = s->next)
4246 if ((s->flags & SEC_ALLOC) != 0)
4247 break;
4248 if (s == NULL)
4250 if (! copy_private_bfd_data (ibfd, obfd))
4251 return false;
4255 ihdr = &elf_section_data (isec)->this_hdr;
4256 ohdr = &elf_section_data (osec)->this_hdr;
4258 ohdr->sh_entsize = ihdr->sh_entsize;
4260 if (ihdr->sh_type == SHT_SYMTAB
4261 || ihdr->sh_type == SHT_DYNSYM
4262 || ihdr->sh_type == SHT_GNU_verneed
4263 || ihdr->sh_type == SHT_GNU_verdef)
4264 ohdr->sh_info = ihdr->sh_info;
4266 elf_section_data (osec)->use_rela_p
4267 = elf_section_data (isec)->use_rela_p;
4269 return true;
4272 /* Copy private symbol information. If this symbol is in a section
4273 which we did not map into a BFD section, try to map the section
4274 index correctly. We use special macro definitions for the mapped
4275 section indices; these definitions are interpreted by the
4276 swap_out_syms function. */
4278 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4279 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4280 #define MAP_STRTAB (SHN_LORESERVE - 3)
4281 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4283 boolean
4284 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4285 bfd *ibfd;
4286 asymbol *isymarg;
4287 bfd *obfd;
4288 asymbol *osymarg;
4290 elf_symbol_type *isym, *osym;
4292 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4293 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4294 return true;
4296 isym = elf_symbol_from (ibfd, isymarg);
4297 osym = elf_symbol_from (obfd, osymarg);
4299 if (isym != NULL
4300 && osym != NULL
4301 && bfd_is_abs_section (isym->symbol.section))
4303 unsigned int shndx;
4305 shndx = isym->internal_elf_sym.st_shndx;
4306 if (shndx == elf_onesymtab (ibfd))
4307 shndx = MAP_ONESYMTAB;
4308 else if (shndx == elf_dynsymtab (ibfd))
4309 shndx = MAP_DYNSYMTAB;
4310 else if (shndx == elf_tdata (ibfd)->strtab_section)
4311 shndx = MAP_STRTAB;
4312 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4313 shndx = MAP_SHSTRTAB;
4314 osym->internal_elf_sym.st_shndx = shndx;
4317 return true;
4320 /* Swap out the symbols. */
4322 static boolean
4323 swap_out_syms (abfd, sttp, relocatable_p)
4324 bfd *abfd;
4325 struct bfd_strtab_hash **sttp;
4326 int relocatable_p;
4328 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4330 if (!elf_map_symbols (abfd))
4331 return false;
4333 /* Dump out the symtabs. */
4335 int symcount = bfd_get_symcount (abfd);
4336 asymbol **syms = bfd_get_outsymbols (abfd);
4337 struct bfd_strtab_hash *stt;
4338 Elf_Internal_Shdr *symtab_hdr;
4339 Elf_Internal_Shdr *symstrtab_hdr;
4340 char *outbound_syms;
4341 int idx;
4343 stt = _bfd_elf_stringtab_init ();
4344 if (stt == NULL)
4345 return false;
4347 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4348 symtab_hdr->sh_type = SHT_SYMTAB;
4349 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4350 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4351 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4352 symtab_hdr->sh_addralign = bed->s->file_align;
4354 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4355 symstrtab_hdr->sh_type = SHT_STRTAB;
4357 outbound_syms = bfd_alloc (abfd,
4358 (1 + symcount) * bed->s->sizeof_sym);
4359 if (outbound_syms == NULL)
4360 return false;
4361 symtab_hdr->contents = (PTR) outbound_syms;
4363 /* now generate the data (for "contents") */
4365 /* Fill in zeroth symbol and swap it out. */
4366 Elf_Internal_Sym sym;
4367 sym.st_name = 0;
4368 sym.st_value = 0;
4369 sym.st_size = 0;
4370 sym.st_info = 0;
4371 sym.st_other = 0;
4372 sym.st_shndx = SHN_UNDEF;
4373 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4374 outbound_syms += bed->s->sizeof_sym;
4376 for (idx = 0; idx < symcount; idx++)
4378 Elf_Internal_Sym sym;
4379 bfd_vma value = syms[idx]->value;
4380 elf_symbol_type *type_ptr;
4381 flagword flags = syms[idx]->flags;
4382 int type;
4384 if ((flags & BSF_SECTION_SYM) != 0)
4386 /* Section symbols have no name. */
4387 sym.st_name = 0;
4389 else
4391 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4392 syms[idx]->name,
4393 true, false);
4394 if (sym.st_name == (unsigned long) -1)
4395 return false;
4398 type_ptr = elf_symbol_from (abfd, syms[idx]);
4400 if ((flags & BSF_SECTION_SYM) == 0
4401 && bfd_is_com_section (syms[idx]->section))
4403 /* ELF common symbols put the alignment into the `value' field,
4404 and the size into the `size' field. This is backwards from
4405 how BFD handles it, so reverse it here. */
4406 sym.st_size = value;
4407 if (type_ptr == NULL
4408 || type_ptr->internal_elf_sym.st_value == 0)
4409 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4410 else
4411 sym.st_value = type_ptr->internal_elf_sym.st_value;
4412 sym.st_shndx = _bfd_elf_section_from_bfd_section
4413 (abfd, syms[idx]->section);
4415 else
4417 asection *sec = syms[idx]->section;
4418 int shndx;
4420 if (sec->output_section)
4422 value += sec->output_offset;
4423 sec = sec->output_section;
4425 /* Don't add in the section vma for relocatable output. */
4426 if (! relocatable_p)
4427 value += sec->vma;
4428 sym.st_value = value;
4429 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4431 if (bfd_is_abs_section (sec)
4432 && type_ptr != NULL
4433 && type_ptr->internal_elf_sym.st_shndx != 0)
4435 /* This symbol is in a real ELF section which we did
4436 not create as a BFD section. Undo the mapping done
4437 by copy_private_symbol_data. */
4438 shndx = type_ptr->internal_elf_sym.st_shndx;
4439 switch (shndx)
4441 case MAP_ONESYMTAB:
4442 shndx = elf_onesymtab (abfd);
4443 break;
4444 case MAP_DYNSYMTAB:
4445 shndx = elf_dynsymtab (abfd);
4446 break;
4447 case MAP_STRTAB:
4448 shndx = elf_tdata (abfd)->strtab_section;
4449 break;
4450 case MAP_SHSTRTAB:
4451 shndx = elf_tdata (abfd)->shstrtab_section;
4452 break;
4453 default:
4454 break;
4457 else
4459 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4461 if (shndx == -1)
4463 asection *sec2;
4465 /* Writing this would be a hell of a lot easier if
4466 we had some decent documentation on bfd, and
4467 knew what to expect of the library, and what to
4468 demand of applications. For example, it
4469 appears that `objcopy' might not set the
4470 section of a symbol to be a section that is
4471 actually in the output file. */
4472 sec2 = bfd_get_section_by_name (abfd, sec->name);
4473 BFD_ASSERT (sec2 != 0);
4474 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4475 BFD_ASSERT (shndx != -1);
4479 sym.st_shndx = shndx;
4482 if ((flags & BSF_FUNCTION) != 0)
4483 type = STT_FUNC;
4484 else if ((flags & BSF_OBJECT) != 0)
4485 type = STT_OBJECT;
4486 else
4487 type = STT_NOTYPE;
4489 /* Processor-specific types */
4490 if (type_ptr != NULL
4491 && bed->elf_backend_get_symbol_type)
4492 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4494 if (flags & BSF_SECTION_SYM)
4495 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4496 else if (bfd_is_com_section (syms[idx]->section))
4497 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4498 else if (bfd_is_und_section (syms[idx]->section))
4499 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4500 ? STB_WEAK
4501 : STB_GLOBAL),
4502 type);
4503 else if (flags & BSF_FILE)
4504 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4505 else
4507 int bind = STB_LOCAL;
4509 if (flags & BSF_LOCAL)
4510 bind = STB_LOCAL;
4511 else if (flags & BSF_WEAK)
4512 bind = STB_WEAK;
4513 else if (flags & BSF_GLOBAL)
4514 bind = STB_GLOBAL;
4516 sym.st_info = ELF_ST_INFO (bind, type);
4519 if (type_ptr != NULL)
4520 sym.st_other = type_ptr->internal_elf_sym.st_other;
4521 else
4522 sym.st_other = 0;
4524 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4525 outbound_syms += bed->s->sizeof_sym;
4528 *sttp = stt;
4529 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4530 symstrtab_hdr->sh_type = SHT_STRTAB;
4532 symstrtab_hdr->sh_flags = 0;
4533 symstrtab_hdr->sh_addr = 0;
4534 symstrtab_hdr->sh_entsize = 0;
4535 symstrtab_hdr->sh_link = 0;
4536 symstrtab_hdr->sh_info = 0;
4537 symstrtab_hdr->sh_addralign = 1;
4540 return true;
4543 /* Return the number of bytes required to hold the symtab vector.
4545 Note that we base it on the count plus 1, since we will null terminate
4546 the vector allocated based on this size. However, the ELF symbol table
4547 always has a dummy entry as symbol #0, so it ends up even. */
4549 long
4550 _bfd_elf_get_symtab_upper_bound (abfd)
4551 bfd *abfd;
4553 long symcount;
4554 long symtab_size;
4555 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4557 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4558 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4560 return symtab_size;
4563 long
4564 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4565 bfd *abfd;
4567 long symcount;
4568 long symtab_size;
4569 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4571 if (elf_dynsymtab (abfd) == 0)
4573 bfd_set_error (bfd_error_invalid_operation);
4574 return -1;
4577 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4578 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4580 return symtab_size;
4583 long
4584 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4585 bfd *abfd ATTRIBUTE_UNUSED;
4586 sec_ptr asect;
4588 return (asect->reloc_count + 1) * sizeof (arelent *);
4591 /* Canonicalize the relocs. */
4593 long
4594 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4595 bfd *abfd;
4596 sec_ptr section;
4597 arelent **relptr;
4598 asymbol **symbols;
4600 arelent *tblptr;
4601 unsigned int i;
4603 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4604 section,
4605 symbols,
4606 false))
4607 return -1;
4609 tblptr = section->relocation;
4610 for (i = 0; i < section->reloc_count; i++)
4611 *relptr++ = tblptr++;
4613 *relptr = NULL;
4615 return section->reloc_count;
4618 long
4619 _bfd_elf_get_symtab (abfd, alocation)
4620 bfd *abfd;
4621 asymbol **alocation;
4623 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4624 (abfd, alocation, false);
4626 if (symcount >= 0)
4627 bfd_get_symcount (abfd) = symcount;
4628 return symcount;
4631 long
4632 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4633 bfd *abfd;
4634 asymbol **alocation;
4636 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4637 (abfd, alocation, true);
4640 /* Return the size required for the dynamic reloc entries. Any
4641 section that was actually installed in the BFD, and has type
4642 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4643 considered to be a dynamic reloc section. */
4645 long
4646 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4647 bfd *abfd;
4649 long ret;
4650 asection *s;
4652 if (elf_dynsymtab (abfd) == 0)
4654 bfd_set_error (bfd_error_invalid_operation);
4655 return -1;
4658 ret = sizeof (arelent *);
4659 for (s = abfd->sections; s != NULL; s = s->next)
4660 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4661 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4662 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4663 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4664 * sizeof (arelent *));
4666 return ret;
4669 /* Canonicalize the dynamic relocation entries. Note that we return
4670 the dynamic relocations as a single block, although they are
4671 actually associated with particular sections; the interface, which
4672 was designed for SunOS style shared libraries, expects that there
4673 is only one set of dynamic relocs. Any section that was actually
4674 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4675 the dynamic symbol table, is considered to be a dynamic reloc
4676 section. */
4678 long
4679 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4680 bfd *abfd;
4681 arelent **storage;
4682 asymbol **syms;
4684 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4685 asection *s;
4686 long ret;
4688 if (elf_dynsymtab (abfd) == 0)
4690 bfd_set_error (bfd_error_invalid_operation);
4691 return -1;
4694 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4695 ret = 0;
4696 for (s = abfd->sections; s != NULL; s = s->next)
4698 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4699 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4700 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4702 arelent *p;
4703 long count, i;
4705 if (! (*slurp_relocs) (abfd, s, syms, true))
4706 return -1;
4707 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4708 p = s->relocation;
4709 for (i = 0; i < count; i++)
4710 *storage++ = p++;
4711 ret += count;
4715 *storage = NULL;
4717 return ret;
4720 /* Read in the version information. */
4722 boolean
4723 _bfd_elf_slurp_version_tables (abfd)
4724 bfd *abfd;
4726 bfd_byte *contents = NULL;
4728 if (elf_dynverdef (abfd) != 0)
4730 Elf_Internal_Shdr *hdr;
4731 Elf_External_Verdef *everdef;
4732 Elf_Internal_Verdef *iverdef;
4733 Elf_Internal_Verdef *iverdefarr;
4734 Elf_Internal_Verdef iverdefmem;
4735 unsigned int i;
4736 int maxidx;
4738 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4740 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4741 if (contents == NULL)
4742 goto error_return;
4743 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4744 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4745 goto error_return;
4747 /* We know the number of entries in the section but not the maximum
4748 index. Therefore we have to run through all entries and find
4749 the maximum. */
4750 everdef = (Elf_External_Verdef *) contents;
4751 maxidx = 0;
4752 for (i = 0; i < hdr->sh_info; ++i)
4754 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4756 if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
4757 maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
4759 everdef = ((Elf_External_Verdef *)
4760 ((bfd_byte *) everdef + iverdefmem.vd_next));
4763 elf_tdata (abfd)->verdef =
4764 ((Elf_Internal_Verdef *)
4765 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4766 if (elf_tdata (abfd)->verdef == NULL)
4767 goto error_return;
4769 elf_tdata (abfd)->cverdefs = maxidx;
4771 everdef = (Elf_External_Verdef *) contents;
4772 iverdefarr = elf_tdata (abfd)->verdef;
4773 for (i = 0; i < hdr->sh_info; i++)
4775 Elf_External_Verdaux *everdaux;
4776 Elf_Internal_Verdaux *iverdaux;
4777 unsigned int j;
4779 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4781 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4782 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4784 iverdef->vd_bfd = abfd;
4786 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4787 bfd_alloc (abfd,
4788 (iverdef->vd_cnt
4789 * sizeof (Elf_Internal_Verdaux))));
4790 if (iverdef->vd_auxptr == NULL)
4791 goto error_return;
4793 everdaux = ((Elf_External_Verdaux *)
4794 ((bfd_byte *) everdef + iverdef->vd_aux));
4795 iverdaux = iverdef->vd_auxptr;
4796 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4798 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4800 iverdaux->vda_nodename =
4801 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4802 iverdaux->vda_name);
4803 if (iverdaux->vda_nodename == NULL)
4804 goto error_return;
4806 if (j + 1 < iverdef->vd_cnt)
4807 iverdaux->vda_nextptr = iverdaux + 1;
4808 else
4809 iverdaux->vda_nextptr = NULL;
4811 everdaux = ((Elf_External_Verdaux *)
4812 ((bfd_byte *) everdaux + iverdaux->vda_next));
4815 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4817 if (i + 1 < hdr->sh_info)
4818 iverdef->vd_nextdef = iverdef + 1;
4819 else
4820 iverdef->vd_nextdef = NULL;
4822 everdef = ((Elf_External_Verdef *)
4823 ((bfd_byte *) everdef + iverdef->vd_next));
4826 free (contents);
4827 contents = NULL;
4830 if (elf_dynverref (abfd) != 0)
4832 Elf_Internal_Shdr *hdr;
4833 Elf_External_Verneed *everneed;
4834 Elf_Internal_Verneed *iverneed;
4835 unsigned int i;
4837 hdr = &elf_tdata (abfd)->dynverref_hdr;
4839 elf_tdata (abfd)->verref =
4840 ((Elf_Internal_Verneed *)
4841 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4842 if (elf_tdata (abfd)->verref == NULL)
4843 goto error_return;
4845 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4847 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4848 if (contents == NULL)
4849 goto error_return;
4850 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4851 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4852 goto error_return;
4854 everneed = (Elf_External_Verneed *) contents;
4855 iverneed = elf_tdata (abfd)->verref;
4856 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4858 Elf_External_Vernaux *evernaux;
4859 Elf_Internal_Vernaux *ivernaux;
4860 unsigned int j;
4862 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4864 iverneed->vn_bfd = abfd;
4866 iverneed->vn_filename =
4867 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4868 iverneed->vn_file);
4869 if (iverneed->vn_filename == NULL)
4870 goto error_return;
4872 iverneed->vn_auxptr =
4873 ((Elf_Internal_Vernaux *)
4874 bfd_alloc (abfd,
4875 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4877 evernaux = ((Elf_External_Vernaux *)
4878 ((bfd_byte *) everneed + iverneed->vn_aux));
4879 ivernaux = iverneed->vn_auxptr;
4880 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4882 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4884 ivernaux->vna_nodename =
4885 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4886 ivernaux->vna_name);
4887 if (ivernaux->vna_nodename == NULL)
4888 goto error_return;
4890 if (j + 1 < iverneed->vn_cnt)
4891 ivernaux->vna_nextptr = ivernaux + 1;
4892 else
4893 ivernaux->vna_nextptr = NULL;
4895 evernaux = ((Elf_External_Vernaux *)
4896 ((bfd_byte *) evernaux + ivernaux->vna_next));
4899 if (i + 1 < hdr->sh_info)
4900 iverneed->vn_nextref = iverneed + 1;
4901 else
4902 iverneed->vn_nextref = NULL;
4904 everneed = ((Elf_External_Verneed *)
4905 ((bfd_byte *) everneed + iverneed->vn_next));
4908 free (contents);
4909 contents = NULL;
4912 return true;
4914 error_return:
4915 if (contents == NULL)
4916 free (contents);
4917 return false;
4920 asymbol *
4921 _bfd_elf_make_empty_symbol (abfd)
4922 bfd *abfd;
4924 elf_symbol_type *newsym;
4926 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4927 if (!newsym)
4928 return NULL;
4929 else
4931 newsym->symbol.the_bfd = abfd;
4932 return &newsym->symbol;
4936 void
4937 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4938 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4939 asymbol *symbol;
4940 symbol_info *ret;
4942 bfd_symbol_info (symbol, ret);
4945 /* Return whether a symbol name implies a local symbol. Most targets
4946 use this function for the is_local_label_name entry point, but some
4947 override it. */
4949 boolean
4950 _bfd_elf_is_local_label_name (abfd, name)
4951 bfd *abfd ATTRIBUTE_UNUSED;
4952 const char *name;
4954 /* Normal local symbols start with ``.L''. */
4955 if (name[0] == '.' && name[1] == 'L')
4956 return true;
4958 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4959 DWARF debugging symbols starting with ``..''. */
4960 if (name[0] == '.' && name[1] == '.')
4961 return true;
4963 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4964 emitting DWARF debugging output. I suspect this is actually a
4965 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4966 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4967 underscore to be emitted on some ELF targets). For ease of use,
4968 we treat such symbols as local. */
4969 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4970 return true;
4972 return false;
4975 alent *
4976 _bfd_elf_get_lineno (ignore_abfd, symbol)
4977 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4978 asymbol *symbol ATTRIBUTE_UNUSED;
4980 abort ();
4981 return NULL;
4984 boolean
4985 _bfd_elf_set_arch_mach (abfd, arch, machine)
4986 bfd *abfd;
4987 enum bfd_architecture arch;
4988 unsigned long machine;
4990 /* If this isn't the right architecture for this backend, and this
4991 isn't the generic backend, fail. */
4992 if (arch != get_elf_backend_data (abfd)->arch
4993 && arch != bfd_arch_unknown
4994 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4995 return false;
4997 return bfd_default_set_arch_mach (abfd, arch, machine);
5000 /* Find the function to a particular section and offset,
5001 for error reporting. */
5003 static boolean
5004 elf_find_function (abfd, section, symbols, offset,
5005 filename_ptr, functionname_ptr)
5006 bfd *abfd ATTRIBUTE_UNUSED;
5007 asection *section;
5008 asymbol **symbols;
5009 bfd_vma offset;
5010 CONST char **filename_ptr;
5011 CONST char **functionname_ptr;
5013 const char *filename;
5014 asymbol *func;
5015 bfd_vma low_func;
5016 asymbol **p;
5018 filename = NULL;
5019 func = NULL;
5020 low_func = 0;
5022 for (p = symbols; *p != NULL; p++)
5024 elf_symbol_type *q;
5026 q = (elf_symbol_type *) *p;
5028 if (bfd_get_section (&q->symbol) != section)
5029 continue;
5031 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5033 default:
5034 break;
5035 case STT_FILE:
5036 filename = bfd_asymbol_name (&q->symbol);
5037 break;
5038 case STT_NOTYPE:
5039 case STT_FUNC:
5040 if (q->symbol.section == section
5041 && q->symbol.value >= low_func
5042 && q->symbol.value <= offset)
5044 func = (asymbol *) q;
5045 low_func = q->symbol.value;
5047 break;
5051 if (func == NULL)
5052 return false;
5054 if (filename_ptr)
5055 *filename_ptr = filename;
5056 if (functionname_ptr)
5057 *functionname_ptr = bfd_asymbol_name (func);
5059 return true;
5062 /* Find the nearest line to a particular section and offset,
5063 for error reporting. */
5065 boolean
5066 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5067 filename_ptr, functionname_ptr, line_ptr)
5068 bfd *abfd;
5069 asection *section;
5070 asymbol **symbols;
5071 bfd_vma offset;
5072 CONST char **filename_ptr;
5073 CONST char **functionname_ptr;
5074 unsigned int *line_ptr;
5076 boolean found;
5078 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5079 filename_ptr, functionname_ptr,
5080 line_ptr))
5082 if (!*functionname_ptr)
5083 elf_find_function (abfd, section, symbols, offset,
5084 *filename_ptr ? NULL : filename_ptr,
5085 functionname_ptr);
5087 return true;
5090 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5091 filename_ptr, functionname_ptr,
5092 line_ptr, 0,
5093 &elf_tdata (abfd)->dwarf2_find_line_info))
5095 if (!*functionname_ptr)
5096 elf_find_function (abfd, section, symbols, offset,
5097 *filename_ptr ? NULL : filename_ptr,
5098 functionname_ptr);
5100 return true;
5103 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5104 &found, filename_ptr,
5105 functionname_ptr, line_ptr,
5106 &elf_tdata (abfd)->line_info))
5107 return false;
5108 if (found)
5109 return true;
5111 if (symbols == NULL)
5112 return false;
5114 if (! elf_find_function (abfd, section, symbols, offset,
5115 filename_ptr, functionname_ptr))
5116 return false;
5118 *line_ptr = 0;
5119 return true;
5123 _bfd_elf_sizeof_headers (abfd, reloc)
5124 bfd *abfd;
5125 boolean reloc;
5127 int ret;
5129 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5130 if (! reloc)
5131 ret += get_program_header_size (abfd);
5132 return ret;
5135 boolean
5136 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5137 bfd *abfd;
5138 sec_ptr section;
5139 PTR location;
5140 file_ptr offset;
5141 bfd_size_type count;
5143 Elf_Internal_Shdr *hdr;
5145 if (! abfd->output_has_begun
5146 && ! _bfd_elf_compute_section_file_positions
5147 (abfd, (struct bfd_link_info *) NULL))
5148 return false;
5150 hdr = &elf_section_data (section)->this_hdr;
5152 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5153 return false;
5154 if (bfd_write (location, 1, count, abfd) != count)
5155 return false;
5157 return true;
5160 void
5161 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5162 bfd *abfd ATTRIBUTE_UNUSED;
5163 arelent *cache_ptr ATTRIBUTE_UNUSED;
5164 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5166 abort ();
5169 #if 0
5170 void
5171 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5172 bfd *abfd;
5173 arelent *cache_ptr;
5174 Elf_Internal_Rel *dst;
5176 abort ();
5178 #endif
5180 /* Try to convert a non-ELF reloc into an ELF one. */
5182 boolean
5183 _bfd_elf_validate_reloc (abfd, areloc)
5184 bfd *abfd;
5185 arelent *areloc;
5187 /* Check whether we really have an ELF howto. */
5189 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5191 bfd_reloc_code_real_type code;
5192 reloc_howto_type *howto;
5194 /* Alien reloc: Try to determine its type to replace it with an
5195 equivalent ELF reloc. */
5197 if (areloc->howto->pc_relative)
5199 switch (areloc->howto->bitsize)
5201 case 8:
5202 code = BFD_RELOC_8_PCREL;
5203 break;
5204 case 12:
5205 code = BFD_RELOC_12_PCREL;
5206 break;
5207 case 16:
5208 code = BFD_RELOC_16_PCREL;
5209 break;
5210 case 24:
5211 code = BFD_RELOC_24_PCREL;
5212 break;
5213 case 32:
5214 code = BFD_RELOC_32_PCREL;
5215 break;
5216 case 64:
5217 code = BFD_RELOC_64_PCREL;
5218 break;
5219 default:
5220 goto fail;
5223 howto = bfd_reloc_type_lookup (abfd, code);
5225 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5227 if (howto->pcrel_offset)
5228 areloc->addend += areloc->address;
5229 else
5230 areloc->addend -= areloc->address; /* addend is unsigned!! */
5233 else
5235 switch (areloc->howto->bitsize)
5237 case 8:
5238 code = BFD_RELOC_8;
5239 break;
5240 case 14:
5241 code = BFD_RELOC_14;
5242 break;
5243 case 16:
5244 code = BFD_RELOC_16;
5245 break;
5246 case 26:
5247 code = BFD_RELOC_26;
5248 break;
5249 case 32:
5250 code = BFD_RELOC_32;
5251 break;
5252 case 64:
5253 code = BFD_RELOC_64;
5254 break;
5255 default:
5256 goto fail;
5259 howto = bfd_reloc_type_lookup (abfd, code);
5262 if (howto)
5263 areloc->howto = howto;
5264 else
5265 goto fail;
5268 return true;
5270 fail:
5271 (*_bfd_error_handler)
5272 (_("%s: unsupported relocation type %s"),
5273 bfd_get_filename (abfd), areloc->howto->name);
5274 bfd_set_error (bfd_error_bad_value);
5275 return false;
5278 boolean
5279 _bfd_elf_close_and_cleanup (abfd)
5280 bfd *abfd;
5282 if (bfd_get_format (abfd) == bfd_object)
5284 if (elf_shstrtab (abfd) != NULL)
5285 _bfd_stringtab_free (elf_shstrtab (abfd));
5288 return _bfd_generic_close_and_cleanup (abfd);
5291 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5292 in the relocation's offset. Thus we cannot allow any sort of sanity
5293 range-checking to interfere. There is nothing else to do in processing
5294 this reloc. */
5296 bfd_reloc_status_type
5297 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5298 bfd *abfd ATTRIBUTE_UNUSED;
5299 arelent *re ATTRIBUTE_UNUSED;
5300 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5301 PTR data ATTRIBUTE_UNUSED;
5302 asection *is ATTRIBUTE_UNUSED;
5303 bfd *obfd ATTRIBUTE_UNUSED;
5304 char **errmsg ATTRIBUTE_UNUSED;
5306 return bfd_reloc_ok;
5309 /* Elf core file support. Much of this only works on native
5310 toolchains, since we rely on knowing the
5311 machine-dependent procfs structure in order to pick
5312 out details about the corefile. */
5314 #ifdef HAVE_SYS_PROCFS_H
5315 # include <sys/procfs.h>
5316 #endif
5318 /* Define offsetof for those systems which lack it. */
5320 #ifndef offsetof
5321 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5322 #endif
5324 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5326 static int
5327 elfcore_make_pid (abfd)
5328 bfd *abfd;
5330 return ((elf_tdata (abfd)->core_lwpid << 16)
5331 + (elf_tdata (abfd)->core_pid));
5334 /* If there isn't a section called NAME, make one, using
5335 data from SECT. Note, this function will generate a
5336 reference to NAME, so you shouldn't deallocate or
5337 overwrite it. */
5339 static boolean
5340 elfcore_maybe_make_sect (abfd, name, sect)
5341 bfd *abfd;
5342 char *name;
5343 asection *sect;
5345 asection *sect2;
5347 if (bfd_get_section_by_name (abfd, name) != NULL)
5348 return true;
5350 sect2 = bfd_make_section (abfd, name);
5351 if (sect2 == NULL)
5352 return false;
5354 sect2->_raw_size = sect->_raw_size;
5355 sect2->filepos = sect->filepos;
5356 sect2->flags = sect->flags;
5357 sect2->alignment_power = sect->alignment_power;
5358 return true;
5361 /* prstatus_t exists on:
5362 solaris 2.5+
5363 linux 2.[01] + glibc
5364 unixware 4.2
5367 #if defined (HAVE_PRSTATUS_T)
5368 static boolean
5369 elfcore_grok_prstatus (abfd, note)
5370 bfd *abfd;
5371 Elf_Internal_Note *note;
5373 char buf[100];
5374 char *name;
5375 asection *sect;
5376 int raw_size;
5377 int offset;
5379 if (note->descsz == sizeof (prstatus_t))
5381 prstatus_t prstat;
5383 raw_size = sizeof (prstat.pr_reg);
5384 offset = offsetof (prstatus_t, pr_reg);
5385 memcpy (&prstat, note->descdata, sizeof (prstat));
5387 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5388 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5390 /* pr_who exists on:
5391 solaris 2.5+
5392 unixware 4.2
5393 pr_who doesn't exist on:
5394 linux 2.[01]
5396 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5397 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5398 #endif
5400 #if defined (HAVE_PRSTATUS32_T)
5401 else if (note->descsz == sizeof (prstatus32_t))
5403 /* 64-bit host, 32-bit corefile */
5404 prstatus32_t prstat;
5406 raw_size = sizeof (prstat.pr_reg);
5407 offset = offsetof (prstatus32_t, pr_reg);
5408 memcpy (&prstat, note->descdata, sizeof (prstat));
5410 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5411 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5413 /* pr_who exists on:
5414 solaris 2.5+
5415 unixware 4.2
5416 pr_who doesn't exist on:
5417 linux 2.[01]
5419 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5420 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5421 #endif
5423 #endif /* HAVE_PRSTATUS32_T */
5424 else
5426 /* Fail - we don't know how to handle any other
5427 note size (ie. data object type). */
5428 return true;
5431 /* Make a ".reg/999" section. */
5433 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5434 name = bfd_alloc (abfd, strlen (buf) + 1);
5435 if (name == NULL)
5436 return false;
5437 strcpy (name, buf);
5439 sect = bfd_make_section (abfd, name);
5440 if (sect == NULL)
5441 return false;
5443 sect->_raw_size = raw_size;
5444 sect->filepos = note->descpos + offset;
5446 sect->flags = SEC_HAS_CONTENTS;
5447 sect->alignment_power = 2;
5449 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5450 return false;
5452 return true;
5454 #endif /* defined (HAVE_PRSTATUS_T) */
5456 /* Create a pseudosection containing the exact contents of NOTE. This
5457 actually creates up to two pseudosections:
5458 - For the single-threaded case, a section named NAME, unless
5459 such a section already exists.
5460 - For the multi-threaded case, a section named "NAME/PID", where
5461 PID is elfcore_make_pid (abfd).
5462 Both pseudosections have identical contents: the contents of NOTE. */
5464 static boolean
5465 elfcore_make_note_pseudosection (abfd, name, note)
5466 bfd *abfd;
5467 char *name;
5468 Elf_Internal_Note *note;
5470 char buf[100];
5471 char *threaded_name;
5472 asection *sect;
5474 /* Build the section name. */
5476 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5477 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5478 if (threaded_name == NULL)
5479 return false;
5480 strcpy (threaded_name, buf);
5482 sect = bfd_make_section (abfd, threaded_name);
5483 if (sect == NULL)
5484 return false;
5485 sect->_raw_size = note->descsz;
5486 sect->filepos = note->descpos;
5487 sect->flags = SEC_HAS_CONTENTS;
5488 sect->alignment_power = 2;
5490 if (! elfcore_maybe_make_sect (abfd, name, sect))
5491 return false;
5493 return true;
5496 /* There isn't a consistent prfpregset_t across platforms,
5497 but it doesn't matter, because we don't have to pick this
5498 data structure apart. */
5500 static boolean
5501 elfcore_grok_prfpreg (abfd, note)
5502 bfd *abfd;
5503 Elf_Internal_Note *note;
5505 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5508 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5509 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5510 literally. */
5512 static boolean
5513 elfcore_grok_prxfpreg (abfd, note)
5514 bfd *abfd;
5515 Elf_Internal_Note *note;
5517 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5520 #if defined (HAVE_PRPSINFO_T)
5521 typedef prpsinfo_t elfcore_psinfo_t;
5522 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5523 typedef prpsinfo32_t elfcore_psinfo32_t;
5524 #endif
5525 #endif
5527 #if defined (HAVE_PSINFO_T)
5528 typedef psinfo_t elfcore_psinfo_t;
5529 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5530 typedef psinfo32_t elfcore_psinfo32_t;
5531 #endif
5532 #endif
5534 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5536 /* return a malloc'ed copy of a string at START which is at
5537 most MAX bytes long, possibly without a terminating '\0'.
5538 the copy will always have a terminating '\0'. */
5540 static char*
5541 elfcore_strndup (abfd, start, max)
5542 bfd *abfd;
5543 char *start;
5544 int max;
5546 char *dup;
5547 char *end = memchr (start, '\0', max);
5548 int len;
5550 if (end == NULL)
5551 len = max;
5552 else
5553 len = end - start;
5555 dup = bfd_alloc (abfd, len + 1);
5556 if (dup == NULL)
5557 return NULL;
5559 memcpy (dup, start, len);
5560 dup[len] = '\0';
5562 return dup;
5565 static boolean
5566 elfcore_grok_psinfo (abfd, note)
5567 bfd *abfd;
5568 Elf_Internal_Note *note;
5570 if (note->descsz == sizeof (elfcore_psinfo_t))
5572 elfcore_psinfo_t psinfo;
5574 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5576 elf_tdata (abfd)->core_program
5577 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5579 elf_tdata (abfd)->core_command
5580 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5582 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5583 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5585 /* 64-bit host, 32-bit corefile */
5586 elfcore_psinfo32_t psinfo;
5588 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5590 elf_tdata (abfd)->core_program
5591 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5593 elf_tdata (abfd)->core_command
5594 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5596 #endif
5598 else
5600 /* Fail - we don't know how to handle any other
5601 note size (ie. data object type). */
5602 return true;
5605 /* Note that for some reason, a spurious space is tacked
5606 onto the end of the args in some (at least one anyway)
5607 implementations, so strip it off if it exists. */
5610 char *command = elf_tdata (abfd)->core_command;
5611 int n = strlen (command);
5613 if (0 < n && command[n - 1] == ' ')
5614 command[n - 1] = '\0';
5617 return true;
5619 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5621 #if defined (HAVE_PSTATUS_T)
5622 static boolean
5623 elfcore_grok_pstatus (abfd, note)
5624 bfd *abfd;
5625 Elf_Internal_Note *note;
5627 if (note->descsz == sizeof (pstatus_t)
5628 #if defined (HAVE_PXSTATUS_T)
5629 || note->descsz == sizeof (pxstatus_t)
5630 #endif
5633 pstatus_t pstat;
5635 memcpy (&pstat, note->descdata, sizeof (pstat));
5637 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5639 #if defined (HAVE_PSTATUS32_T)
5640 else if (note->descsz == sizeof (pstatus32_t))
5642 /* 64-bit host, 32-bit corefile */
5643 pstatus32_t pstat;
5645 memcpy (&pstat, note->descdata, sizeof (pstat));
5647 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5649 #endif
5650 /* Could grab some more details from the "representative"
5651 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5652 NT_LWPSTATUS note, presumably. */
5654 return true;
5656 #endif /* defined (HAVE_PSTATUS_T) */
5658 #if defined (HAVE_LWPSTATUS_T)
5659 static boolean
5660 elfcore_grok_lwpstatus (abfd, note)
5661 bfd *abfd;
5662 Elf_Internal_Note *note;
5664 lwpstatus_t lwpstat;
5665 char buf[100];
5666 char *name;
5667 asection *sect;
5669 if (note->descsz != sizeof (lwpstat)
5670 #if defined (HAVE_LWPXSTATUS_T)
5671 && note->descsz != sizeof (lwpxstatus_t)
5672 #endif
5674 return true;
5676 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5678 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5679 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5681 /* Make a ".reg/999" section. */
5683 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5684 name = bfd_alloc (abfd, strlen (buf) + 1);
5685 if (name == NULL)
5686 return false;
5687 strcpy (name, buf);
5689 sect = bfd_make_section (abfd, name);
5690 if (sect == NULL)
5691 return false;
5693 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5694 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5695 sect->filepos = note->descpos
5696 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5697 #endif
5699 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5700 sect->_raw_size = sizeof (lwpstat.pr_reg);
5701 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5702 #endif
5704 sect->flags = SEC_HAS_CONTENTS;
5705 sect->alignment_power = 2;
5707 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5708 return false;
5710 /* Make a ".reg2/999" section */
5712 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5713 name = bfd_alloc (abfd, strlen (buf) + 1);
5714 if (name == NULL)
5715 return false;
5716 strcpy (name, buf);
5718 sect = bfd_make_section (abfd, name);
5719 if (sect == NULL)
5720 return false;
5722 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5723 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5724 sect->filepos = note->descpos
5725 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5726 #endif
5728 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5729 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5730 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5731 #endif
5733 sect->flags = SEC_HAS_CONTENTS;
5734 sect->alignment_power = 2;
5736 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5737 return false;
5739 return true;
5741 #endif /* defined (HAVE_LWPSTATUS_T) */
5743 #if defined (HAVE_WIN32_PSTATUS_T)
5744 static boolean
5745 elfcore_grok_win32pstatus (abfd, note)
5746 bfd *abfd;
5747 Elf_Internal_Note *note;
5749 char buf[30];
5750 char *name;
5751 asection *sect;
5752 win32_pstatus_t pstatus;
5754 if (note->descsz < sizeof (pstatus))
5755 return true;
5757 memcpy (&pstatus, note->descdata, note->descsz);
5759 switch (pstatus.data_type)
5761 case NOTE_INFO_PROCESS:
5762 /* FIXME: need to add ->core_command. */
5763 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5764 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5765 break;
5767 case NOTE_INFO_THREAD:
5768 /* Make a ".reg/999" section. */
5769 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5771 name = bfd_alloc (abfd, strlen (buf) + 1);
5772 if (name == NULL)
5773 return false;
5775 strcpy (name, buf);
5777 sect = bfd_make_section (abfd, name);
5778 if (sect == NULL)
5779 return false;
5781 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5782 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5783 data.thread_info.thread_context);
5784 sect->flags = SEC_HAS_CONTENTS;
5785 sect->alignment_power = 2;
5787 if (pstatus.data.thread_info.is_active_thread)
5788 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5789 return false;
5790 break;
5792 case NOTE_INFO_MODULE:
5793 /* Make a ".module/xxxxxxxx" section. */
5794 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5796 name = bfd_alloc (abfd, strlen (buf) + 1);
5797 if (name == NULL)
5798 return false;
5800 strcpy (name, buf);
5802 sect = bfd_make_section (abfd, name);
5804 if (sect == NULL)
5805 return false;
5807 sect->_raw_size = note->descsz;
5808 sect->filepos = note->descpos;
5809 sect->flags = SEC_HAS_CONTENTS;
5810 sect->alignment_power = 2;
5811 break;
5813 default:
5814 return true;
5817 return true;
5819 #endif /* HAVE_WIN32_PSTATUS_T */
5821 static boolean
5822 elfcore_grok_note (abfd, note)
5823 bfd *abfd;
5824 Elf_Internal_Note *note;
5826 switch (note->type)
5828 default:
5829 return true;
5831 #if defined (HAVE_PRSTATUS_T)
5832 case NT_PRSTATUS:
5833 return elfcore_grok_prstatus (abfd, note);
5834 #endif
5836 #if defined (HAVE_PSTATUS_T)
5837 case NT_PSTATUS:
5838 return elfcore_grok_pstatus (abfd, note);
5839 #endif
5841 #if defined (HAVE_LWPSTATUS_T)
5842 case NT_LWPSTATUS:
5843 return elfcore_grok_lwpstatus (abfd, note);
5844 #endif
5846 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5847 return elfcore_grok_prfpreg (abfd, note);
5849 #if defined (HAVE_WIN32_PSTATUS_T)
5850 case NT_WIN32PSTATUS:
5851 return elfcore_grok_win32pstatus (abfd, note);
5852 #endif
5854 case NT_PRXFPREG: /* Linux SSE extension */
5855 if (note->namesz == 5
5856 && ! strcmp (note->namedata, "LINUX"))
5857 return elfcore_grok_prxfpreg (abfd, note);
5858 else
5859 return true;
5861 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5862 case NT_PRPSINFO:
5863 case NT_PSINFO:
5864 return elfcore_grok_psinfo (abfd, note);
5865 #endif
5869 static boolean
5870 elfcore_read_notes (abfd, offset, size)
5871 bfd *abfd;
5872 bfd_vma offset;
5873 bfd_vma size;
5875 char *buf;
5876 char *p;
5878 if (size <= 0)
5879 return true;
5881 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5882 return false;
5884 buf = bfd_malloc ((size_t) size);
5885 if (buf == NULL)
5886 return false;
5888 if (bfd_read (buf, size, 1, abfd) != size)
5890 error:
5891 free (buf);
5892 return false;
5895 p = buf;
5896 while (p < buf + size)
5898 /* FIXME: bad alignment assumption. */
5899 Elf_External_Note *xnp = (Elf_External_Note *) p;
5900 Elf_Internal_Note in;
5902 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5904 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5905 in.namedata = xnp->name;
5907 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5908 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5909 in.descpos = offset + (in.descdata - buf);
5911 if (! elfcore_grok_note (abfd, &in))
5912 goto error;
5914 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5917 free (buf);
5918 return true;
5921 /* FIXME: This function is now unnecessary. Callers can just call
5922 bfd_section_from_phdr directly. */
5924 boolean
5925 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5926 bfd *abfd;
5927 Elf_Internal_Phdr* phdr;
5928 int sec_num;
5930 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5931 return false;
5933 return true;
5936 /* Providing external access to the ELF program header table. */
5938 /* Return an upper bound on the number of bytes required to store a
5939 copy of ABFD's program header table entries. Return -1 if an error
5940 occurs; bfd_get_error will return an appropriate code. */
5942 long
5943 bfd_get_elf_phdr_upper_bound (abfd)
5944 bfd *abfd;
5946 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5948 bfd_set_error (bfd_error_wrong_format);
5949 return -1;
5952 return (elf_elfheader (abfd)->e_phnum
5953 * sizeof (Elf_Internal_Phdr));
5956 /* Copy ABFD's program header table entries to *PHDRS. The entries
5957 will be stored as an array of Elf_Internal_Phdr structures, as
5958 defined in include/elf/internal.h. To find out how large the
5959 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5961 Return the number of program header table entries read, or -1 if an
5962 error occurs; bfd_get_error will return an appropriate code. */
5965 bfd_get_elf_phdrs (abfd, phdrs)
5966 bfd *abfd;
5967 void *phdrs;
5969 int num_phdrs;
5971 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5973 bfd_set_error (bfd_error_wrong_format);
5974 return -1;
5977 num_phdrs = elf_elfheader (abfd)->e_phnum;
5978 memcpy (phdrs, elf_tdata (abfd)->phdr,
5979 num_phdrs * sizeof (Elf_Internal_Phdr));
5981 return num_phdrs;