bfd/
[binutils.git] / bfd / elf.c
blob92a546d3c55a1c7c5f41dd8233f148c7c87a7160
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 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3417 default:
3418 i_ehdrp->e_machine = EM_NONE;
3420 i_ehdrp->e_version = bed->s->ev_current;
3421 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3423 /* No program header, for now. */
3424 i_ehdrp->e_phoff = 0;
3425 i_ehdrp->e_phentsize = 0;
3426 i_ehdrp->e_phnum = 0;
3428 /* Each bfd section is section header entry. */
3429 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3430 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3432 /* If we're building an executable, we'll need a program header table. */
3433 if (abfd->flags & EXEC_P)
3435 /* It all happens later. */
3436 #if 0
3437 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3439 /* elf_build_phdrs() returns a (NULL-terminated) array of
3440 Elf_Internal_Phdrs. */
3441 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3442 i_ehdrp->e_phoff = outbase;
3443 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3444 #endif
3446 else
3448 i_ehdrp->e_phentsize = 0;
3449 i_phdrp = 0;
3450 i_ehdrp->e_phoff = 0;
3453 elf_tdata (abfd)->symtab_hdr.sh_name =
3454 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3455 elf_tdata (abfd)->strtab_hdr.sh_name =
3456 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3457 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3458 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3459 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3460 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3461 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3462 return false;
3464 return true;
3467 /* Assign file positions for all the reloc sections which are not part
3468 of the loadable file image. */
3470 void
3471 _bfd_elf_assign_file_positions_for_relocs (abfd)
3472 bfd *abfd;
3474 file_ptr off;
3475 unsigned int i;
3476 Elf_Internal_Shdr **shdrpp;
3478 off = elf_tdata (abfd)->next_file_pos;
3480 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3481 i < elf_elfheader (abfd)->e_shnum;
3482 i++, shdrpp++)
3484 Elf_Internal_Shdr *shdrp;
3486 shdrp = *shdrpp;
3487 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3488 && shdrp->sh_offset == -1)
3489 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3492 elf_tdata (abfd)->next_file_pos = off;
3495 boolean
3496 _bfd_elf_write_object_contents (abfd)
3497 bfd *abfd;
3499 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3500 Elf_Internal_Ehdr *i_ehdrp;
3501 Elf_Internal_Shdr **i_shdrp;
3502 boolean failed;
3503 unsigned int count;
3505 if (! abfd->output_has_begun
3506 && ! _bfd_elf_compute_section_file_positions
3507 (abfd, (struct bfd_link_info *) NULL))
3508 return false;
3510 i_shdrp = elf_elfsections (abfd);
3511 i_ehdrp = elf_elfheader (abfd);
3513 failed = false;
3514 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3515 if (failed)
3516 return false;
3518 _bfd_elf_assign_file_positions_for_relocs (abfd);
3520 /* After writing the headers, we need to write the sections too... */
3521 for (count = 1; count < i_ehdrp->e_shnum; count++)
3523 if (bed->elf_backend_section_processing)
3524 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3525 if (i_shdrp[count]->contents)
3527 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3528 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3529 1, abfd)
3530 != i_shdrp[count]->sh_size))
3531 return false;
3535 /* Write out the section header names. */
3536 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3537 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3538 return false;
3540 if (bed->elf_backend_final_write_processing)
3541 (*bed->elf_backend_final_write_processing) (abfd,
3542 elf_tdata (abfd)->linker);
3544 return bed->s->write_shdrs_and_ehdr (abfd);
3547 boolean
3548 _bfd_elf_write_corefile_contents (abfd)
3549 bfd *abfd;
3551 /* Hopefully this can be done just like an object file. */
3552 return _bfd_elf_write_object_contents (abfd);
3555 /* Given a section, search the header to find them. */
3558 _bfd_elf_section_from_bfd_section (abfd, asect)
3559 bfd *abfd;
3560 struct sec *asect;
3562 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3563 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3564 int index;
3565 Elf_Internal_Shdr *hdr;
3566 int maxindex = elf_elfheader (abfd)->e_shnum;
3568 for (index = 0; index < maxindex; index++)
3570 hdr = i_shdrp[index];
3571 if (hdr->bfd_section == asect)
3572 return index;
3575 if (bed->elf_backend_section_from_bfd_section)
3577 for (index = 0; index < maxindex; index++)
3579 int retval;
3581 hdr = i_shdrp[index];
3582 retval = index;
3583 if ((*bed->elf_backend_section_from_bfd_section)
3584 (abfd, hdr, asect, &retval))
3585 return retval;
3589 if (bfd_is_abs_section (asect))
3590 return SHN_ABS;
3591 if (bfd_is_com_section (asect))
3592 return SHN_COMMON;
3593 if (bfd_is_und_section (asect))
3594 return SHN_UNDEF;
3596 bfd_set_error (bfd_error_nonrepresentable_section);
3598 return -1;
3601 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3602 on error. */
3605 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3606 bfd *abfd;
3607 asymbol **asym_ptr_ptr;
3609 asymbol *asym_ptr = *asym_ptr_ptr;
3610 int idx;
3611 flagword flags = asym_ptr->flags;
3613 /* When gas creates relocations against local labels, it creates its
3614 own symbol for the section, but does put the symbol into the
3615 symbol chain, so udata is 0. When the linker is generating
3616 relocatable output, this section symbol may be for one of the
3617 input sections rather than the output section. */
3618 if (asym_ptr->udata.i == 0
3619 && (flags & BSF_SECTION_SYM)
3620 && asym_ptr->section)
3622 int indx;
3624 if (asym_ptr->section->output_section != NULL)
3625 indx = asym_ptr->section->output_section->index;
3626 else
3627 indx = asym_ptr->section->index;
3628 if (elf_section_syms (abfd)[indx])
3629 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3632 idx = asym_ptr->udata.i;
3634 if (idx == 0)
3636 /* This case can occur when using --strip-symbol on a symbol
3637 which is used in a relocation entry. */
3638 (*_bfd_error_handler)
3639 (_("%s: symbol `%s' required but not present"),
3640 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3641 bfd_set_error (bfd_error_no_symbols);
3642 return -1;
3645 #if DEBUG & 4
3647 fprintf (stderr,
3648 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3649 (long) asym_ptr, asym_ptr->name, idx, flags,
3650 elf_symbol_flags (flags));
3651 fflush (stderr);
3653 #endif
3655 return idx;
3658 /* Copy private BFD data. This copies any program header information. */
3660 static boolean
3661 copy_private_bfd_data (ibfd, obfd)
3662 bfd *ibfd;
3663 bfd *obfd;
3665 Elf_Internal_Ehdr * iehdr;
3666 struct elf_segment_map * map;
3667 struct elf_segment_map * map_first;
3668 struct elf_segment_map ** pointer_to_map;
3669 Elf_Internal_Phdr * segment;
3670 asection * section;
3671 unsigned int i;
3672 unsigned int num_segments;
3673 boolean phdr_included = false;
3674 bfd_vma maxpagesize;
3675 struct elf_segment_map * phdr_adjust_seg = NULL;
3676 unsigned int phdr_adjust_num = 0;
3678 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3679 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3680 return true;
3682 if (elf_tdata (ibfd)->phdr == NULL)
3683 return true;
3685 iehdr = elf_elfheader (ibfd);
3687 map_first = NULL;
3688 pointer_to_map = &map_first;
3690 num_segments = elf_elfheader (ibfd)->e_phnum;
3691 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3693 /* Returns the end address of the segment + 1. */
3694 #define SEGMENT_END(segment, start) \
3695 (start + (segment->p_memsz > segment->p_filesz \
3696 ? segment->p_memsz : segment->p_filesz))
3698 /* Returns true if the given section is contained within
3699 the given segment. VMA addresses are compared. */
3700 #define IS_CONTAINED_BY_VMA(section, segment) \
3701 (section->vma >= segment->p_vaddr \
3702 && (section->vma + section->_raw_size) \
3703 <= (SEGMENT_END (segment, segment->p_vaddr)))
3705 /* Returns true if the given section is contained within
3706 the given segment. LMA addresses are compared. */
3707 #define IS_CONTAINED_BY_LMA(section, segment, base) \
3708 (section->lma >= base \
3709 && (section->lma + section->_raw_size) \
3710 <= SEGMENT_END (segment, base))
3712 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3713 #define IS_COREFILE_NOTE(p, s) \
3714 (p->p_type == PT_NOTE \
3715 && bfd_get_format (ibfd) == bfd_core \
3716 && s->vma == 0 && s->lma == 0 \
3717 && (bfd_vma) s->filepos >= p->p_offset \
3718 && (bfd_vma) s->filepos + s->_raw_size \
3719 <= p->p_offset + p->p_filesz)
3721 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3722 linker, which generates a PT_INTERP section with p_vaddr and
3723 p_memsz set to 0. */
3724 #define IS_SOLARIS_PT_INTERP(p, s) \
3725 ( p->p_vaddr == 0 \
3726 && p->p_filesz > 0 \
3727 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3728 && s->_raw_size > 0 \
3729 && (bfd_vma) s->filepos >= p->p_offset \
3730 && ((bfd_vma) s->filepos + s->_raw_size \
3731 <= p->p_offset + p->p_filesz))
3733 /* Decide if the given section should be included in the given segment.
3734 A section will be included if:
3735 1. It is within the address space of the segment,
3736 2. It is an allocated segment,
3737 3. There is an output section associated with it,
3738 4. The section has not already been allocated to a previous segment. */
3739 #define INCLUDE_SECTION_IN_SEGMENT(section, segment) \
3740 ((((IS_CONTAINED_BY_VMA (section, segment) \
3741 || IS_SOLARIS_PT_INTERP (segment, section)) \
3742 && (section->flags & SEC_ALLOC) != 0) \
3743 || IS_COREFILE_NOTE (segment, section)) \
3744 && section->output_section != NULL \
3745 && section->segment_mark == false)
3747 /* Returns true iff seg1 starts after the end of seg2. */
3748 #define SEGMENT_AFTER_SEGMENT(seg1, seg2) \
3749 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr))
3751 /* Returns true iff seg1 and seg2 overlap. */
3752 #define SEGMENT_OVERLAPS(seg1, seg2) \
3753 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) || SEGMENT_AFTER_SEGMENT (seg2, seg1)))
3755 /* Initialise the segment mark field. */
3756 for (section = ibfd->sections; section != NULL; section = section->next)
3757 section->segment_mark = false;
3759 /* Scan through the segments specified in the program header
3760 of the input BFD. For this first scan we look for overlaps
3761 in the loadable segments. These can be created by wierd
3762 parameters to objcopy. */
3763 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3764 i < num_segments;
3765 i++, segment++)
3767 unsigned int j;
3768 Elf_Internal_Phdr *segment2;
3770 if (segment->p_type != PT_LOAD)
3771 continue;
3773 /* Determine if this segment overlaps any previous segments. */
3774 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++)
3776 bfd_signed_vma extra_length;
3778 if (segment2->p_type != PT_LOAD
3779 || ! SEGMENT_OVERLAPS (segment, segment2))
3780 continue;
3782 /* Merge the two segments together. */
3783 if (segment2->p_vaddr < segment->p_vaddr)
3785 /* Extend SEGMENT2 to include SEGMENT and then delete
3786 SEGMENT. */
3787 extra_length =
3788 SEGMENT_END (segment, segment->p_vaddr)
3789 - SEGMENT_END (segment2, segment2->p_vaddr);
3791 if (extra_length > 0)
3793 segment2->p_memsz += extra_length;
3794 segment2->p_filesz += extra_length;
3797 segment->p_type = PT_NULL;
3799 /* Since we have deleted P we must restart the outer loop. */
3800 i = 0;
3801 segment = elf_tdata (ibfd)->phdr;
3802 break;
3804 else
3806 /* Extend SEGMENT to include SEGMENT2 and then delete
3807 SEGMENT2. */
3808 extra_length =
3809 SEGMENT_END (segment2, segment2->p_vaddr)
3810 - SEGMENT_END (segment, segment->p_vaddr);
3812 if (extra_length > 0)
3814 segment->p_memsz += extra_length;
3815 segment->p_filesz += extra_length;
3818 segment2->p_type = PT_NULL;
3823 /* The second scan attempts to assign sections to segments. */
3824 for (i = 0, segment = elf_tdata (ibfd)->phdr;
3825 i < num_segments;
3826 i ++, segment ++)
3828 unsigned int section_count;
3829 asection ** sections;
3830 asection * output_section;
3831 unsigned int isec;
3832 bfd_vma matching_lma;
3833 bfd_vma suggested_lma;
3834 unsigned int j;
3836 if (segment->p_type == PT_NULL)
3837 continue;
3839 /* Compute how many sections might be placed into this segment. */
3840 section_count = 0;
3841 for (section = ibfd->sections; section != NULL; section = section->next)
3842 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3843 ++section_count;
3845 /* Allocate a segment map big enough to contain all of the
3846 sections we have selected. */
3847 map = ((struct elf_segment_map *)
3848 bfd_alloc (obfd,
3849 (sizeof (struct elf_segment_map)
3850 + ((size_t) section_count - 1) * sizeof (asection *))));
3851 if (map == NULL)
3852 return false;
3854 /* Initialise the fields of the segment map. Default to
3855 using the physical address of the segment in the input BFD. */
3856 map->next = NULL;
3857 map->p_type = segment->p_type;
3858 map->p_flags = segment->p_flags;
3859 map->p_flags_valid = 1;
3860 map->p_paddr = segment->p_paddr;
3861 map->p_paddr_valid = 1;
3863 /* Determine if this segment contains the ELF file header
3864 and if it contains the program headers themselves. */
3865 map->includes_filehdr = (segment->p_offset == 0
3866 && segment->p_filesz >= iehdr->e_ehsize);
3868 map->includes_phdrs = 0;
3870 if (! phdr_included || segment->p_type != PT_LOAD)
3872 map->includes_phdrs =
3873 (segment->p_offset <= (bfd_vma) iehdr->e_phoff
3874 && (segment->p_offset + segment->p_filesz
3875 >= ((bfd_vma) iehdr->e_phoff
3876 + iehdr->e_phnum * iehdr->e_phentsize)));
3878 if (segment->p_type == PT_LOAD && map->includes_phdrs)
3879 phdr_included = true;
3882 if (section_count == 0)
3884 /* Special segments, such as the PT_PHDR segment, may contain
3885 no sections, but ordinary, loadable segments should contain
3886 something. */
3887 if (segment->p_type == PT_LOAD)
3888 _bfd_error_handler
3889 (_("%s: warning: Empty loadable segment detected\n"),
3890 bfd_get_filename (ibfd));
3892 map->count = 0;
3893 *pointer_to_map = map;
3894 pointer_to_map = &map->next;
3896 continue;
3899 /* Now scan the sections in the input BFD again and attempt
3900 to add their corresponding output sections to the segment map.
3901 The problem here is how to handle an output section which has
3902 been moved (ie had its LMA changed). There are four possibilities:
3904 1. None of the sections have been moved.
3905 In this case we can continue to use the segment LMA from the
3906 input BFD.
3908 2. All of the sections have been moved by the same amount.
3909 In this case we can change the segment's LMA to match the LMA
3910 of the first section.
3912 3. Some of the sections have been moved, others have not.
3913 In this case those sections which have not been moved can be
3914 placed in the current segment which will have to have its size,
3915 and possibly its LMA changed, and a new segment or segments will
3916 have to be created to contain the other sections.
3918 4. The sections have been moved, but not be the same amount.
3919 In this case we can change the segment's LMA to match the LMA
3920 of the first section and we will have to create a new segment
3921 or segments to contain the other sections.
3923 In order to save time, we allocate an array to hold the section
3924 pointers that we are interested in. As these sections get assigned
3925 to a segment, they are removed from this array. */
3927 sections = (asection **) bfd_malloc
3928 (sizeof (asection *) * section_count);
3929 if (sections == NULL)
3930 return false;
3932 /* Step One: Scan for segment vs section LMA conflicts.
3933 Also add the sections to the section array allocated above.
3934 Also add the sections to the current segment. In the common
3935 case, where the sections have not been moved, this means that
3936 we have completely filled the segment, and there is nothing
3937 more to do. */
3938 isec = 0;
3939 matching_lma = 0;
3940 suggested_lma = 0;
3942 for (j = 0, section = ibfd->sections;
3943 section != NULL;
3944 section = section->next)
3946 if (INCLUDE_SECTION_IN_SEGMENT (section, segment))
3948 output_section = section->output_section;
3950 sections[j ++] = section;
3952 /* The Solaris native linker always sets p_paddr to 0.
3953 We try to catch that case here, and set it to the
3954 correct value. */
3955 if (segment->p_paddr == 0
3956 && segment->p_vaddr != 0
3957 && isec == 0
3958 && output_section->lma != 0
3959 && (output_section->vma == (segment->p_vaddr
3960 + (map->includes_filehdr
3961 ? iehdr->e_ehsize
3962 : 0)
3963 + (map->includes_phdrs
3964 ? iehdr->e_phnum * iehdr->e_phentsize
3965 : 0))))
3966 map->p_paddr = segment->p_vaddr;
3968 /* Match up the physical address of the segment with the
3969 LMA address of the output section. */
3970 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
3971 || IS_COREFILE_NOTE (segment, section))
3973 if (matching_lma == 0)
3974 matching_lma = output_section->lma;
3976 /* We assume that if the section fits within the segment
3977 then it does not overlap any other section within that
3978 segment. */
3979 map->sections[isec ++] = output_section;
3981 else if (suggested_lma == 0)
3982 suggested_lma = output_section->lma;
3986 BFD_ASSERT (j == section_count);
3988 /* Step Two: Adjust the physical address of the current segment,
3989 if necessary. */
3990 if (isec == section_count)
3992 /* All of the sections fitted within the segment as currently
3993 specified. This is the default case. Add the segment to
3994 the list of built segments and carry on to process the next
3995 program header in the input BFD. */
3996 map->count = section_count;
3997 *pointer_to_map = map;
3998 pointer_to_map = &map->next;
4000 free (sections);
4001 continue;
4003 else
4005 if (matching_lma != 0)
4007 /* At least one section fits inside the current segment.
4008 Keep it, but modify its physical address to match the
4009 LMA of the first section that fitted. */
4010 map->p_paddr = matching_lma;
4012 else
4014 /* None of the sections fitted inside the current segment.
4015 Change the current segment's physical address to match
4016 the LMA of the first section. */
4017 map->p_paddr = suggested_lma;
4020 /* Offset the segment physical address from the lma
4021 to allow for space taken up by elf headers. */
4022 if (map->includes_filehdr)
4023 map->p_paddr -= iehdr->e_ehsize;
4025 if (map->includes_phdrs)
4027 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
4029 /* iehdr->e_phnum is just an estimate of the number
4030 of program headers that we will need. Make a note
4031 here of the number we used and the segment we chose
4032 to hold these headers, so that we can adjust the
4033 offset when we know the correct value. */
4034 phdr_adjust_num = iehdr->e_phnum;
4035 phdr_adjust_seg = map;
4039 /* Step Three: Loop over the sections again, this time assigning
4040 those that fit to the current segment and remvoing them from the
4041 sections array; but making sure not to leave large gaps. Once all
4042 possible sections have been assigned to the current segment it is
4043 added to the list of built segments and if sections still remain
4044 to be assigned, a new segment is constructed before repeating
4045 the loop. */
4046 isec = 0;
4049 map->count = 0;
4050 suggested_lma = 0;
4052 /* Fill the current segment with sections that fit. */
4053 for (j = 0; j < section_count; j++)
4055 section = sections[j];
4057 if (section == NULL)
4058 continue;
4060 output_section = section->output_section;
4062 BFD_ASSERT (output_section != NULL);
4064 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr)
4065 || IS_COREFILE_NOTE (segment, section))
4067 if (map->count == 0)
4069 /* If the first section in a segment does not start at
4070 the beginning of the segment, then something is
4071 wrong. */
4072 if (output_section->lma !=
4073 (map->p_paddr
4074 + (map->includes_filehdr ? iehdr->e_ehsize : 0)
4075 + (map->includes_phdrs
4076 ? iehdr->e_phnum * iehdr->e_phentsize
4077 : 0)))
4078 abort ();
4080 else
4082 asection * prev_sec;
4084 prev_sec = map->sections[map->count - 1];
4086 /* If the gap between the end of the previous section
4087 and the start of this section is more than
4088 maxpagesize then we need to start a new segment. */
4089 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
4090 < BFD_ALIGN (output_section->lma, maxpagesize))
4091 || ((prev_sec->lma + prev_sec->_raw_size) > output_section->lma))
4093 if (suggested_lma == 0)
4094 suggested_lma = output_section->lma;
4096 continue;
4100 map->sections[map->count++] = output_section;
4101 ++isec;
4102 sections[j] = NULL;
4103 section->segment_mark = true;
4105 else if (suggested_lma == 0)
4106 suggested_lma = output_section->lma;
4109 BFD_ASSERT (map->count > 0);
4111 /* Add the current segment to the list of built segments. */
4112 *pointer_to_map = map;
4113 pointer_to_map = &map->next;
4115 if (isec < section_count)
4117 /* We still have not allocated all of the sections to
4118 segments. Create a new segment here, initialise it
4119 and carry on looping. */
4120 map = ((struct elf_segment_map *)
4121 bfd_alloc (obfd,
4122 (sizeof (struct elf_segment_map)
4123 + ((size_t) section_count - 1)
4124 * sizeof (asection *))));
4125 if (map == NULL)
4126 return false;
4128 /* Initialise the fields of the segment map. Set the physical
4129 physical address to the LMA of the first section that has
4130 not yet been assigned. */
4131 map->next = NULL;
4132 map->p_type = segment->p_type;
4133 map->p_flags = segment->p_flags;
4134 map->p_flags_valid = 1;
4135 map->p_paddr = suggested_lma;
4136 map->p_paddr_valid = 1;
4137 map->includes_filehdr = 0;
4138 map->includes_phdrs = 0;
4141 while (isec < section_count);
4143 free (sections);
4146 /* The Solaris linker creates program headers in which all the
4147 p_paddr fields are zero. When we try to objcopy or strip such a
4148 file, we get confused. Check for this case, and if we find it
4149 reset the p_paddr_valid fields. */
4150 for (map = map_first; map != NULL; map = map->next)
4151 if (map->p_paddr != 0)
4152 break;
4153 if (map == NULL)
4155 for (map = map_first; map != NULL; map = map->next)
4156 map->p_paddr_valid = 0;
4159 elf_tdata (obfd)->segment_map = map_first;
4161 /* If we had to estimate the number of program headers that were
4162 going to be needed, then check our estimate know and adjust
4163 the offset if necessary. */
4164 if (phdr_adjust_seg != NULL)
4166 unsigned int count;
4168 for (count = 0, map = map_first; map != NULL; map = map->next)
4169 count++;
4171 if (count > phdr_adjust_num)
4172 phdr_adjust_seg->p_paddr
4173 -= (count - phdr_adjust_num) * iehdr->e_phentsize;
4176 #if 0
4177 /* Final Step: Sort the segments into ascending order of physical
4178 address. */
4179 if (map_first != NULL)
4181 struct elf_segment_map *prev;
4183 prev = map_first;
4184 for (map = map_first->next; map != NULL; prev = map, map = map->next)
4186 /* Yes I know - its a bubble sort.... */
4187 if (map->next != NULL && (map->next->p_paddr < map->p_paddr))
4189 /* Swap map and map->next. */
4190 prev->next = map->next;
4191 map->next = map->next->next;
4192 prev->next->next = map;
4194 /* Restart loop. */
4195 map = map_first;
4199 #endif
4201 #undef SEGMENT_END
4202 #undef IS_CONTAINED_BY_VMA
4203 #undef IS_CONTAINED_BY_LMA
4204 #undef IS_COREFILE_NOTE
4205 #undef IS_SOLARIS_PT_INTERP
4206 #undef INCLUDE_SECTION_IN_SEGMENT
4207 #undef SEGMENT_AFTER_SEGMENT
4208 #undef SEGMENT_OVERLAPS
4209 return true;
4212 /* Copy private section information. This copies over the entsize
4213 field, and sometimes the info field. */
4215 boolean
4216 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
4217 bfd *ibfd;
4218 asection *isec;
4219 bfd *obfd;
4220 asection *osec;
4222 Elf_Internal_Shdr *ihdr, *ohdr;
4224 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4225 || obfd->xvec->flavour != bfd_target_elf_flavour)
4226 return true;
4228 /* Copy over private BFD data if it has not already been copied.
4229 This must be done here, rather than in the copy_private_bfd_data
4230 entry point, because the latter is called after the section
4231 contents have been set, which means that the program headers have
4232 already been worked out. */
4233 if (elf_tdata (obfd)->segment_map == NULL
4234 && elf_tdata (ibfd)->phdr != NULL)
4236 asection *s;
4238 /* Only set up the segments if there are no more SEC_ALLOC
4239 sections. FIXME: This won't do the right thing if objcopy is
4240 used to remove the last SEC_ALLOC section, since objcopy
4241 won't call this routine in that case. */
4242 for (s = isec->next; s != NULL; s = s->next)
4243 if ((s->flags & SEC_ALLOC) != 0)
4244 break;
4245 if (s == NULL)
4247 if (! copy_private_bfd_data (ibfd, obfd))
4248 return false;
4252 ihdr = &elf_section_data (isec)->this_hdr;
4253 ohdr = &elf_section_data (osec)->this_hdr;
4255 ohdr->sh_entsize = ihdr->sh_entsize;
4257 if (ihdr->sh_type == SHT_SYMTAB
4258 || ihdr->sh_type == SHT_DYNSYM
4259 || ihdr->sh_type == SHT_GNU_verneed
4260 || ihdr->sh_type == SHT_GNU_verdef)
4261 ohdr->sh_info = ihdr->sh_info;
4263 elf_section_data (osec)->use_rela_p
4264 = elf_section_data (isec)->use_rela_p;
4266 return true;
4269 /* Copy private symbol information. If this symbol is in a section
4270 which we did not map into a BFD section, try to map the section
4271 index correctly. We use special macro definitions for the mapped
4272 section indices; these definitions are interpreted by the
4273 swap_out_syms function. */
4275 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4276 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4277 #define MAP_STRTAB (SHN_LORESERVE - 3)
4278 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4280 boolean
4281 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4282 bfd *ibfd;
4283 asymbol *isymarg;
4284 bfd *obfd;
4285 asymbol *osymarg;
4287 elf_symbol_type *isym, *osym;
4289 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4290 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4291 return true;
4293 isym = elf_symbol_from (ibfd, isymarg);
4294 osym = elf_symbol_from (obfd, osymarg);
4296 if (isym != NULL
4297 && osym != NULL
4298 && bfd_is_abs_section (isym->symbol.section))
4300 unsigned int shndx;
4302 shndx = isym->internal_elf_sym.st_shndx;
4303 if (shndx == elf_onesymtab (ibfd))
4304 shndx = MAP_ONESYMTAB;
4305 else if (shndx == elf_dynsymtab (ibfd))
4306 shndx = MAP_DYNSYMTAB;
4307 else if (shndx == elf_tdata (ibfd)->strtab_section)
4308 shndx = MAP_STRTAB;
4309 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4310 shndx = MAP_SHSTRTAB;
4311 osym->internal_elf_sym.st_shndx = shndx;
4314 return true;
4317 /* Swap out the symbols. */
4319 static boolean
4320 swap_out_syms (abfd, sttp, relocatable_p)
4321 bfd *abfd;
4322 struct bfd_strtab_hash **sttp;
4323 int relocatable_p;
4325 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4327 if (!elf_map_symbols (abfd))
4328 return false;
4330 /* Dump out the symtabs. */
4332 int symcount = bfd_get_symcount (abfd);
4333 asymbol **syms = bfd_get_outsymbols (abfd);
4334 struct bfd_strtab_hash *stt;
4335 Elf_Internal_Shdr *symtab_hdr;
4336 Elf_Internal_Shdr *symstrtab_hdr;
4337 char *outbound_syms;
4338 int idx;
4340 stt = _bfd_elf_stringtab_init ();
4341 if (stt == NULL)
4342 return false;
4344 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4345 symtab_hdr->sh_type = SHT_SYMTAB;
4346 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4347 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4348 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4349 symtab_hdr->sh_addralign = bed->s->file_align;
4351 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4352 symstrtab_hdr->sh_type = SHT_STRTAB;
4354 outbound_syms = bfd_alloc (abfd,
4355 (1 + symcount) * bed->s->sizeof_sym);
4356 if (outbound_syms == NULL)
4357 return false;
4358 symtab_hdr->contents = (PTR) outbound_syms;
4360 /* now generate the data (for "contents") */
4362 /* Fill in zeroth symbol and swap it out. */
4363 Elf_Internal_Sym sym;
4364 sym.st_name = 0;
4365 sym.st_value = 0;
4366 sym.st_size = 0;
4367 sym.st_info = 0;
4368 sym.st_other = 0;
4369 sym.st_shndx = SHN_UNDEF;
4370 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4371 outbound_syms += bed->s->sizeof_sym;
4373 for (idx = 0; idx < symcount; idx++)
4375 Elf_Internal_Sym sym;
4376 bfd_vma value = syms[idx]->value;
4377 elf_symbol_type *type_ptr;
4378 flagword flags = syms[idx]->flags;
4379 int type;
4381 if ((flags & BSF_SECTION_SYM) != 0)
4383 /* Section symbols have no name. */
4384 sym.st_name = 0;
4386 else
4388 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4389 syms[idx]->name,
4390 true, false);
4391 if (sym.st_name == (unsigned long) -1)
4392 return false;
4395 type_ptr = elf_symbol_from (abfd, syms[idx]);
4397 if ((flags & BSF_SECTION_SYM) == 0
4398 && bfd_is_com_section (syms[idx]->section))
4400 /* ELF common symbols put the alignment into the `value' field,
4401 and the size into the `size' field. This is backwards from
4402 how BFD handles it, so reverse it here. */
4403 sym.st_size = value;
4404 if (type_ptr == NULL
4405 || type_ptr->internal_elf_sym.st_value == 0)
4406 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4407 else
4408 sym.st_value = type_ptr->internal_elf_sym.st_value;
4409 sym.st_shndx = _bfd_elf_section_from_bfd_section
4410 (abfd, syms[idx]->section);
4412 else
4414 asection *sec = syms[idx]->section;
4415 int shndx;
4417 if (sec->output_section)
4419 value += sec->output_offset;
4420 sec = sec->output_section;
4422 /* Don't add in the section vma for relocatable output. */
4423 if (! relocatable_p)
4424 value += sec->vma;
4425 sym.st_value = value;
4426 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4428 if (bfd_is_abs_section (sec)
4429 && type_ptr != NULL
4430 && type_ptr->internal_elf_sym.st_shndx != 0)
4432 /* This symbol is in a real ELF section which we did
4433 not create as a BFD section. Undo the mapping done
4434 by copy_private_symbol_data. */
4435 shndx = type_ptr->internal_elf_sym.st_shndx;
4436 switch (shndx)
4438 case MAP_ONESYMTAB:
4439 shndx = elf_onesymtab (abfd);
4440 break;
4441 case MAP_DYNSYMTAB:
4442 shndx = elf_dynsymtab (abfd);
4443 break;
4444 case MAP_STRTAB:
4445 shndx = elf_tdata (abfd)->strtab_section;
4446 break;
4447 case MAP_SHSTRTAB:
4448 shndx = elf_tdata (abfd)->shstrtab_section;
4449 break;
4450 default:
4451 break;
4454 else
4456 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4458 if (shndx == -1)
4460 asection *sec2;
4462 /* Writing this would be a hell of a lot easier if
4463 we had some decent documentation on bfd, and
4464 knew what to expect of the library, and what to
4465 demand of applications. For example, it
4466 appears that `objcopy' might not set the
4467 section of a symbol to be a section that is
4468 actually in the output file. */
4469 sec2 = bfd_get_section_by_name (abfd, sec->name);
4470 BFD_ASSERT (sec2 != 0);
4471 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4472 BFD_ASSERT (shndx != -1);
4476 sym.st_shndx = shndx;
4479 if ((flags & BSF_FUNCTION) != 0)
4480 type = STT_FUNC;
4481 else if ((flags & BSF_OBJECT) != 0)
4482 type = STT_OBJECT;
4483 else
4484 type = STT_NOTYPE;
4486 /* Processor-specific types */
4487 if (type_ptr != NULL
4488 && bed->elf_backend_get_symbol_type)
4489 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4491 if (flags & BSF_SECTION_SYM)
4492 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4493 else if (bfd_is_com_section (syms[idx]->section))
4494 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4495 else if (bfd_is_und_section (syms[idx]->section))
4496 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4497 ? STB_WEAK
4498 : STB_GLOBAL),
4499 type);
4500 else if (flags & BSF_FILE)
4501 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4502 else
4504 int bind = STB_LOCAL;
4506 if (flags & BSF_LOCAL)
4507 bind = STB_LOCAL;
4508 else if (flags & BSF_WEAK)
4509 bind = STB_WEAK;
4510 else if (flags & BSF_GLOBAL)
4511 bind = STB_GLOBAL;
4513 sym.st_info = ELF_ST_INFO (bind, type);
4516 if (type_ptr != NULL)
4517 sym.st_other = type_ptr->internal_elf_sym.st_other;
4518 else
4519 sym.st_other = 0;
4521 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4522 outbound_syms += bed->s->sizeof_sym;
4525 *sttp = stt;
4526 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4527 symstrtab_hdr->sh_type = SHT_STRTAB;
4529 symstrtab_hdr->sh_flags = 0;
4530 symstrtab_hdr->sh_addr = 0;
4531 symstrtab_hdr->sh_entsize = 0;
4532 symstrtab_hdr->sh_link = 0;
4533 symstrtab_hdr->sh_info = 0;
4534 symstrtab_hdr->sh_addralign = 1;
4537 return true;
4540 /* Return the number of bytes required to hold the symtab vector.
4542 Note that we base it on the count plus 1, since we will null terminate
4543 the vector allocated based on this size. However, the ELF symbol table
4544 always has a dummy entry as symbol #0, so it ends up even. */
4546 long
4547 _bfd_elf_get_symtab_upper_bound (abfd)
4548 bfd *abfd;
4550 long symcount;
4551 long symtab_size;
4552 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4554 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4555 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4557 return symtab_size;
4560 long
4561 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4562 bfd *abfd;
4564 long symcount;
4565 long symtab_size;
4566 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4568 if (elf_dynsymtab (abfd) == 0)
4570 bfd_set_error (bfd_error_invalid_operation);
4571 return -1;
4574 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4575 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4577 return symtab_size;
4580 long
4581 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4582 bfd *abfd ATTRIBUTE_UNUSED;
4583 sec_ptr asect;
4585 return (asect->reloc_count + 1) * sizeof (arelent *);
4588 /* Canonicalize the relocs. */
4590 long
4591 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4592 bfd *abfd;
4593 sec_ptr section;
4594 arelent **relptr;
4595 asymbol **symbols;
4597 arelent *tblptr;
4598 unsigned int i;
4600 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4601 section,
4602 symbols,
4603 false))
4604 return -1;
4606 tblptr = section->relocation;
4607 for (i = 0; i < section->reloc_count; i++)
4608 *relptr++ = tblptr++;
4610 *relptr = NULL;
4612 return section->reloc_count;
4615 long
4616 _bfd_elf_get_symtab (abfd, alocation)
4617 bfd *abfd;
4618 asymbol **alocation;
4620 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4621 (abfd, alocation, false);
4623 if (symcount >= 0)
4624 bfd_get_symcount (abfd) = symcount;
4625 return symcount;
4628 long
4629 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4630 bfd *abfd;
4631 asymbol **alocation;
4633 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4634 (abfd, alocation, true);
4637 /* Return the size required for the dynamic reloc entries. Any
4638 section that was actually installed in the BFD, and has type
4639 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4640 considered to be a dynamic reloc section. */
4642 long
4643 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4644 bfd *abfd;
4646 long ret;
4647 asection *s;
4649 if (elf_dynsymtab (abfd) == 0)
4651 bfd_set_error (bfd_error_invalid_operation);
4652 return -1;
4655 ret = sizeof (arelent *);
4656 for (s = abfd->sections; s != NULL; s = s->next)
4657 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4658 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4659 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4660 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4661 * sizeof (arelent *));
4663 return ret;
4666 /* Canonicalize the dynamic relocation entries. Note that we return
4667 the dynamic relocations as a single block, although they are
4668 actually associated with particular sections; the interface, which
4669 was designed for SunOS style shared libraries, expects that there
4670 is only one set of dynamic relocs. Any section that was actually
4671 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4672 the dynamic symbol table, is considered to be a dynamic reloc
4673 section. */
4675 long
4676 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4677 bfd *abfd;
4678 arelent **storage;
4679 asymbol **syms;
4681 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4682 asection *s;
4683 long ret;
4685 if (elf_dynsymtab (abfd) == 0)
4687 bfd_set_error (bfd_error_invalid_operation);
4688 return -1;
4691 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4692 ret = 0;
4693 for (s = abfd->sections; s != NULL; s = s->next)
4695 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4696 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4697 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4699 arelent *p;
4700 long count, i;
4702 if (! (*slurp_relocs) (abfd, s, syms, true))
4703 return -1;
4704 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4705 p = s->relocation;
4706 for (i = 0; i < count; i++)
4707 *storage++ = p++;
4708 ret += count;
4712 *storage = NULL;
4714 return ret;
4717 /* Read in the version information. */
4719 boolean
4720 _bfd_elf_slurp_version_tables (abfd)
4721 bfd *abfd;
4723 bfd_byte *contents = NULL;
4725 if (elf_dynverdef (abfd) != 0)
4727 Elf_Internal_Shdr *hdr;
4728 Elf_External_Verdef *everdef;
4729 Elf_Internal_Verdef *iverdef;
4730 Elf_Internal_Verdef *iverdefarr;
4731 Elf_Internal_Verdef iverdefmem;
4732 unsigned int i;
4733 int maxidx;
4735 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4737 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4738 if (contents == NULL)
4739 goto error_return;
4740 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4741 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4742 goto error_return;
4744 /* We know the number of entries in the section but not the maximum
4745 index. Therefore we have to run through all entries and find
4746 the maximum. */
4747 everdef = (Elf_External_Verdef *) contents;
4748 maxidx = 0;
4749 for (i = 0; i < hdr->sh_info; ++i)
4751 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4753 if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
4754 maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
4756 everdef = ((Elf_External_Verdef *)
4757 ((bfd_byte *) everdef + iverdefmem.vd_next));
4760 elf_tdata (abfd)->verdef =
4761 ((Elf_Internal_Verdef *)
4762 bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
4763 if (elf_tdata (abfd)->verdef == NULL)
4764 goto error_return;
4766 elf_tdata (abfd)->cverdefs = maxidx;
4768 everdef = (Elf_External_Verdef *) contents;
4769 iverdefarr = elf_tdata (abfd)->verdef;
4770 for (i = 0; i < hdr->sh_info; i++)
4772 Elf_External_Verdaux *everdaux;
4773 Elf_Internal_Verdaux *iverdaux;
4774 unsigned int j;
4776 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
4778 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
4779 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
4781 iverdef->vd_bfd = abfd;
4783 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4784 bfd_alloc (abfd,
4785 (iverdef->vd_cnt
4786 * sizeof (Elf_Internal_Verdaux))));
4787 if (iverdef->vd_auxptr == NULL)
4788 goto error_return;
4790 everdaux = ((Elf_External_Verdaux *)
4791 ((bfd_byte *) everdef + iverdef->vd_aux));
4792 iverdaux = iverdef->vd_auxptr;
4793 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4795 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4797 iverdaux->vda_nodename =
4798 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4799 iverdaux->vda_name);
4800 if (iverdaux->vda_nodename == NULL)
4801 goto error_return;
4803 if (j + 1 < iverdef->vd_cnt)
4804 iverdaux->vda_nextptr = iverdaux + 1;
4805 else
4806 iverdaux->vda_nextptr = NULL;
4808 everdaux = ((Elf_External_Verdaux *)
4809 ((bfd_byte *) everdaux + iverdaux->vda_next));
4812 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4814 if (i + 1 < hdr->sh_info)
4815 iverdef->vd_nextdef = iverdef + 1;
4816 else
4817 iverdef->vd_nextdef = NULL;
4819 everdef = ((Elf_External_Verdef *)
4820 ((bfd_byte *) everdef + iverdef->vd_next));
4823 free (contents);
4824 contents = NULL;
4827 if (elf_dynverref (abfd) != 0)
4829 Elf_Internal_Shdr *hdr;
4830 Elf_External_Verneed *everneed;
4831 Elf_Internal_Verneed *iverneed;
4832 unsigned int i;
4834 hdr = &elf_tdata (abfd)->dynverref_hdr;
4836 elf_tdata (abfd)->verref =
4837 ((Elf_Internal_Verneed *)
4838 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4839 if (elf_tdata (abfd)->verref == NULL)
4840 goto error_return;
4842 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4844 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4845 if (contents == NULL)
4846 goto error_return;
4847 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4848 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4849 goto error_return;
4851 everneed = (Elf_External_Verneed *) contents;
4852 iverneed = elf_tdata (abfd)->verref;
4853 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4855 Elf_External_Vernaux *evernaux;
4856 Elf_Internal_Vernaux *ivernaux;
4857 unsigned int j;
4859 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4861 iverneed->vn_bfd = abfd;
4863 iverneed->vn_filename =
4864 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4865 iverneed->vn_file);
4866 if (iverneed->vn_filename == NULL)
4867 goto error_return;
4869 iverneed->vn_auxptr =
4870 ((Elf_Internal_Vernaux *)
4871 bfd_alloc (abfd,
4872 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4874 evernaux = ((Elf_External_Vernaux *)
4875 ((bfd_byte *) everneed + iverneed->vn_aux));
4876 ivernaux = iverneed->vn_auxptr;
4877 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4879 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4881 ivernaux->vna_nodename =
4882 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4883 ivernaux->vna_name);
4884 if (ivernaux->vna_nodename == NULL)
4885 goto error_return;
4887 if (j + 1 < iverneed->vn_cnt)
4888 ivernaux->vna_nextptr = ivernaux + 1;
4889 else
4890 ivernaux->vna_nextptr = NULL;
4892 evernaux = ((Elf_External_Vernaux *)
4893 ((bfd_byte *) evernaux + ivernaux->vna_next));
4896 if (i + 1 < hdr->sh_info)
4897 iverneed->vn_nextref = iverneed + 1;
4898 else
4899 iverneed->vn_nextref = NULL;
4901 everneed = ((Elf_External_Verneed *)
4902 ((bfd_byte *) everneed + iverneed->vn_next));
4905 free (contents);
4906 contents = NULL;
4909 return true;
4911 error_return:
4912 if (contents == NULL)
4913 free (contents);
4914 return false;
4917 asymbol *
4918 _bfd_elf_make_empty_symbol (abfd)
4919 bfd *abfd;
4921 elf_symbol_type *newsym;
4923 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4924 if (!newsym)
4925 return NULL;
4926 else
4928 newsym->symbol.the_bfd = abfd;
4929 return &newsym->symbol;
4933 void
4934 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4935 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4936 asymbol *symbol;
4937 symbol_info *ret;
4939 bfd_symbol_info (symbol, ret);
4942 /* Return whether a symbol name implies a local symbol. Most targets
4943 use this function for the is_local_label_name entry point, but some
4944 override it. */
4946 boolean
4947 _bfd_elf_is_local_label_name (abfd, name)
4948 bfd *abfd ATTRIBUTE_UNUSED;
4949 const char *name;
4951 /* Normal local symbols start with ``.L''. */
4952 if (name[0] == '.' && name[1] == 'L')
4953 return true;
4955 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4956 DWARF debugging symbols starting with ``..''. */
4957 if (name[0] == '.' && name[1] == '.')
4958 return true;
4960 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4961 emitting DWARF debugging output. I suspect this is actually a
4962 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4963 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4964 underscore to be emitted on some ELF targets). For ease of use,
4965 we treat such symbols as local. */
4966 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4967 return true;
4969 return false;
4972 alent *
4973 _bfd_elf_get_lineno (ignore_abfd, symbol)
4974 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4975 asymbol *symbol ATTRIBUTE_UNUSED;
4977 abort ();
4978 return NULL;
4981 boolean
4982 _bfd_elf_set_arch_mach (abfd, arch, machine)
4983 bfd *abfd;
4984 enum bfd_architecture arch;
4985 unsigned long machine;
4987 /* If this isn't the right architecture for this backend, and this
4988 isn't the generic backend, fail. */
4989 if (arch != get_elf_backend_data (abfd)->arch
4990 && arch != bfd_arch_unknown
4991 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4992 return false;
4994 return bfd_default_set_arch_mach (abfd, arch, machine);
4997 /* Find the function to a particular section and offset,
4998 for error reporting. */
5000 static boolean
5001 elf_find_function (abfd, section, symbols, offset,
5002 filename_ptr, functionname_ptr)
5003 bfd *abfd ATTRIBUTE_UNUSED;
5004 asection *section;
5005 asymbol **symbols;
5006 bfd_vma offset;
5007 CONST char **filename_ptr;
5008 CONST char **functionname_ptr;
5010 const char *filename;
5011 asymbol *func;
5012 bfd_vma low_func;
5013 asymbol **p;
5015 filename = NULL;
5016 func = NULL;
5017 low_func = 0;
5019 for (p = symbols; *p != NULL; p++)
5021 elf_symbol_type *q;
5023 q = (elf_symbol_type *) *p;
5025 if (bfd_get_section (&q->symbol) != section)
5026 continue;
5028 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
5030 default:
5031 break;
5032 case STT_FILE:
5033 filename = bfd_asymbol_name (&q->symbol);
5034 break;
5035 case STT_NOTYPE:
5036 case STT_FUNC:
5037 if (q->symbol.section == section
5038 && q->symbol.value >= low_func
5039 && q->symbol.value <= offset)
5041 func = (asymbol *) q;
5042 low_func = q->symbol.value;
5044 break;
5048 if (func == NULL)
5049 return false;
5051 if (filename_ptr)
5052 *filename_ptr = filename;
5053 if (functionname_ptr)
5054 *functionname_ptr = bfd_asymbol_name (func);
5056 return true;
5059 /* Find the nearest line to a particular section and offset,
5060 for error reporting. */
5062 boolean
5063 _bfd_elf_find_nearest_line (abfd, section, symbols, offset,
5064 filename_ptr, functionname_ptr, line_ptr)
5065 bfd *abfd;
5066 asection *section;
5067 asymbol **symbols;
5068 bfd_vma offset;
5069 CONST char **filename_ptr;
5070 CONST char **functionname_ptr;
5071 unsigned int *line_ptr;
5073 boolean found;
5075 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
5076 filename_ptr, functionname_ptr,
5077 line_ptr))
5079 if (!*functionname_ptr)
5080 elf_find_function (abfd, section, symbols, offset,
5081 *filename_ptr ? NULL : filename_ptr,
5082 functionname_ptr);
5084 return true;
5087 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
5088 filename_ptr, functionname_ptr,
5089 line_ptr, 0,
5090 &elf_tdata (abfd)->dwarf2_find_line_info))
5092 if (!*functionname_ptr)
5093 elf_find_function (abfd, section, symbols, offset,
5094 *filename_ptr ? NULL : filename_ptr,
5095 functionname_ptr);
5097 return true;
5100 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
5101 &found, filename_ptr,
5102 functionname_ptr, line_ptr,
5103 &elf_tdata (abfd)->line_info))
5104 return false;
5105 if (found)
5106 return true;
5108 if (symbols == NULL)
5109 return false;
5111 if (! elf_find_function (abfd, section, symbols, offset,
5112 filename_ptr, functionname_ptr))
5113 return false;
5115 *line_ptr = 0;
5116 return true;
5120 _bfd_elf_sizeof_headers (abfd, reloc)
5121 bfd *abfd;
5122 boolean reloc;
5124 int ret;
5126 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
5127 if (! reloc)
5128 ret += get_program_header_size (abfd);
5129 return ret;
5132 boolean
5133 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
5134 bfd *abfd;
5135 sec_ptr section;
5136 PTR location;
5137 file_ptr offset;
5138 bfd_size_type count;
5140 Elf_Internal_Shdr *hdr;
5142 if (! abfd->output_has_begun
5143 && ! _bfd_elf_compute_section_file_positions
5144 (abfd, (struct bfd_link_info *) NULL))
5145 return false;
5147 hdr = &elf_section_data (section)->this_hdr;
5149 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
5150 return false;
5151 if (bfd_write (location, 1, count, abfd) != count)
5152 return false;
5154 return true;
5157 void
5158 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
5159 bfd *abfd ATTRIBUTE_UNUSED;
5160 arelent *cache_ptr ATTRIBUTE_UNUSED;
5161 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
5163 abort ();
5166 #if 0
5167 void
5168 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
5169 bfd *abfd;
5170 arelent *cache_ptr;
5171 Elf_Internal_Rel *dst;
5173 abort ();
5175 #endif
5177 /* Try to convert a non-ELF reloc into an ELF one. */
5179 boolean
5180 _bfd_elf_validate_reloc (abfd, areloc)
5181 bfd *abfd;
5182 arelent *areloc;
5184 /* Check whether we really have an ELF howto. */
5186 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
5188 bfd_reloc_code_real_type code;
5189 reloc_howto_type *howto;
5191 /* Alien reloc: Try to determine its type to replace it with an
5192 equivalent ELF reloc. */
5194 if (areloc->howto->pc_relative)
5196 switch (areloc->howto->bitsize)
5198 case 8:
5199 code = BFD_RELOC_8_PCREL;
5200 break;
5201 case 12:
5202 code = BFD_RELOC_12_PCREL;
5203 break;
5204 case 16:
5205 code = BFD_RELOC_16_PCREL;
5206 break;
5207 case 24:
5208 code = BFD_RELOC_24_PCREL;
5209 break;
5210 case 32:
5211 code = BFD_RELOC_32_PCREL;
5212 break;
5213 case 64:
5214 code = BFD_RELOC_64_PCREL;
5215 break;
5216 default:
5217 goto fail;
5220 howto = bfd_reloc_type_lookup (abfd, code);
5222 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
5224 if (howto->pcrel_offset)
5225 areloc->addend += areloc->address;
5226 else
5227 areloc->addend -= areloc->address; /* addend is unsigned!! */
5230 else
5232 switch (areloc->howto->bitsize)
5234 case 8:
5235 code = BFD_RELOC_8;
5236 break;
5237 case 14:
5238 code = BFD_RELOC_14;
5239 break;
5240 case 16:
5241 code = BFD_RELOC_16;
5242 break;
5243 case 26:
5244 code = BFD_RELOC_26;
5245 break;
5246 case 32:
5247 code = BFD_RELOC_32;
5248 break;
5249 case 64:
5250 code = BFD_RELOC_64;
5251 break;
5252 default:
5253 goto fail;
5256 howto = bfd_reloc_type_lookup (abfd, code);
5259 if (howto)
5260 areloc->howto = howto;
5261 else
5262 goto fail;
5265 return true;
5267 fail:
5268 (*_bfd_error_handler)
5269 (_("%s: unsupported relocation type %s"),
5270 bfd_get_filename (abfd), areloc->howto->name);
5271 bfd_set_error (bfd_error_bad_value);
5272 return false;
5275 boolean
5276 _bfd_elf_close_and_cleanup (abfd)
5277 bfd *abfd;
5279 if (bfd_get_format (abfd) == bfd_object)
5281 if (elf_shstrtab (abfd) != NULL)
5282 _bfd_stringtab_free (elf_shstrtab (abfd));
5285 return _bfd_generic_close_and_cleanup (abfd);
5288 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5289 in the relocation's offset. Thus we cannot allow any sort of sanity
5290 range-checking to interfere. There is nothing else to do in processing
5291 this reloc. */
5293 bfd_reloc_status_type
5294 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5295 bfd *abfd ATTRIBUTE_UNUSED;
5296 arelent *re ATTRIBUTE_UNUSED;
5297 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5298 PTR data ATTRIBUTE_UNUSED;
5299 asection *is ATTRIBUTE_UNUSED;
5300 bfd *obfd ATTRIBUTE_UNUSED;
5301 char **errmsg ATTRIBUTE_UNUSED;
5303 return bfd_reloc_ok;
5306 /* Elf core file support. Much of this only works on native
5307 toolchains, since we rely on knowing the
5308 machine-dependent procfs structure in order to pick
5309 out details about the corefile. */
5311 #ifdef HAVE_SYS_PROCFS_H
5312 # include <sys/procfs.h>
5313 #endif
5315 /* Define offsetof for those systems which lack it. */
5317 #ifndef offsetof
5318 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5319 #endif
5321 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5323 static int
5324 elfcore_make_pid (abfd)
5325 bfd *abfd;
5327 return ((elf_tdata (abfd)->core_lwpid << 16)
5328 + (elf_tdata (abfd)->core_pid));
5331 /* If there isn't a section called NAME, make one, using
5332 data from SECT. Note, this function will generate a
5333 reference to NAME, so you shouldn't deallocate or
5334 overwrite it. */
5336 static boolean
5337 elfcore_maybe_make_sect (abfd, name, sect)
5338 bfd *abfd;
5339 char *name;
5340 asection *sect;
5342 asection *sect2;
5344 if (bfd_get_section_by_name (abfd, name) != NULL)
5345 return true;
5347 sect2 = bfd_make_section (abfd, name);
5348 if (sect2 == NULL)
5349 return false;
5351 sect2->_raw_size = sect->_raw_size;
5352 sect2->filepos = sect->filepos;
5353 sect2->flags = sect->flags;
5354 sect2->alignment_power = sect->alignment_power;
5355 return true;
5358 /* prstatus_t exists on:
5359 solaris 2.5+
5360 linux 2.[01] + glibc
5361 unixware 4.2
5364 #if defined (HAVE_PRSTATUS_T)
5365 static boolean
5366 elfcore_grok_prstatus (abfd, note)
5367 bfd *abfd;
5368 Elf_Internal_Note *note;
5370 char buf[100];
5371 char *name;
5372 asection *sect;
5373 int raw_size;
5374 int offset;
5376 if (note->descsz == sizeof (prstatus_t))
5378 prstatus_t prstat;
5380 raw_size = sizeof (prstat.pr_reg);
5381 offset = offsetof (prstatus_t, pr_reg);
5382 memcpy (&prstat, note->descdata, sizeof (prstat));
5384 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5385 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5387 /* pr_who exists on:
5388 solaris 2.5+
5389 unixware 4.2
5390 pr_who doesn't exist on:
5391 linux 2.[01]
5393 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5394 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5395 #endif
5397 #if defined (HAVE_PRSTATUS32_T)
5398 else if (note->descsz == sizeof (prstatus32_t))
5400 /* 64-bit host, 32-bit corefile */
5401 prstatus32_t prstat;
5403 raw_size = sizeof (prstat.pr_reg);
5404 offset = offsetof (prstatus32_t, pr_reg);
5405 memcpy (&prstat, note->descdata, sizeof (prstat));
5407 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5408 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5410 /* pr_who exists on:
5411 solaris 2.5+
5412 unixware 4.2
5413 pr_who doesn't exist on:
5414 linux 2.[01]
5416 #if defined (HAVE_PRSTATUS32_T_PR_WHO)
5417 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5418 #endif
5420 #endif /* HAVE_PRSTATUS32_T */
5421 else
5423 /* Fail - we don't know how to handle any other
5424 note size (ie. data object type). */
5425 return true;
5428 /* Make a ".reg/999" section. */
5430 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5431 name = bfd_alloc (abfd, strlen (buf) + 1);
5432 if (name == NULL)
5433 return false;
5434 strcpy (name, buf);
5436 sect = bfd_make_section (abfd, name);
5437 if (sect == NULL)
5438 return false;
5440 sect->_raw_size = raw_size;
5441 sect->filepos = note->descpos + offset;
5443 sect->flags = SEC_HAS_CONTENTS;
5444 sect->alignment_power = 2;
5446 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5447 return false;
5449 return true;
5451 #endif /* defined (HAVE_PRSTATUS_T) */
5453 /* Create a pseudosection containing the exact contents of NOTE. This
5454 actually creates up to two pseudosections:
5455 - For the single-threaded case, a section named NAME, unless
5456 such a section already exists.
5457 - For the multi-threaded case, a section named "NAME/PID", where
5458 PID is elfcore_make_pid (abfd).
5459 Both pseudosections have identical contents: the contents of NOTE. */
5461 static boolean
5462 elfcore_make_note_pseudosection (abfd, name, note)
5463 bfd *abfd;
5464 char *name;
5465 Elf_Internal_Note *note;
5467 char buf[100];
5468 char *threaded_name;
5469 asection *sect;
5471 /* Build the section name. */
5473 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5474 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5475 if (threaded_name == NULL)
5476 return false;
5477 strcpy (threaded_name, buf);
5479 sect = bfd_make_section (abfd, threaded_name);
5480 if (sect == NULL)
5481 return false;
5482 sect->_raw_size = note->descsz;
5483 sect->filepos = note->descpos;
5484 sect->flags = SEC_HAS_CONTENTS;
5485 sect->alignment_power = 2;
5487 if (! elfcore_maybe_make_sect (abfd, name, sect))
5488 return false;
5490 return true;
5493 /* There isn't a consistent prfpregset_t across platforms,
5494 but it doesn't matter, because we don't have to pick this
5495 data structure apart. */
5497 static boolean
5498 elfcore_grok_prfpreg (abfd, note)
5499 bfd *abfd;
5500 Elf_Internal_Note *note;
5502 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5505 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5506 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5507 literally. */
5509 static boolean
5510 elfcore_grok_prxfpreg (abfd, note)
5511 bfd *abfd;
5512 Elf_Internal_Note *note;
5514 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5517 #if defined (HAVE_PRPSINFO_T)
5518 typedef prpsinfo_t elfcore_psinfo_t;
5519 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */
5520 typedef prpsinfo32_t elfcore_psinfo32_t;
5521 #endif
5522 #endif
5524 #if defined (HAVE_PSINFO_T)
5525 typedef psinfo_t elfcore_psinfo_t;
5526 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */
5527 typedef psinfo32_t elfcore_psinfo32_t;
5528 #endif
5529 #endif
5531 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5533 /* return a malloc'ed copy of a string at START which is at
5534 most MAX bytes long, possibly without a terminating '\0'.
5535 the copy will always have a terminating '\0'. */
5537 static char*
5538 elfcore_strndup (abfd, start, max)
5539 bfd *abfd;
5540 char *start;
5541 int max;
5543 char *dup;
5544 char *end = memchr (start, '\0', max);
5545 int len;
5547 if (end == NULL)
5548 len = max;
5549 else
5550 len = end - start;
5552 dup = bfd_alloc (abfd, len + 1);
5553 if (dup == NULL)
5554 return NULL;
5556 memcpy (dup, start, len);
5557 dup[len] = '\0';
5559 return dup;
5562 static boolean
5563 elfcore_grok_psinfo (abfd, note)
5564 bfd *abfd;
5565 Elf_Internal_Note *note;
5567 if (note->descsz == sizeof (elfcore_psinfo_t))
5569 elfcore_psinfo_t psinfo;
5571 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5573 elf_tdata (abfd)->core_program
5574 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5576 elf_tdata (abfd)->core_command
5577 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5579 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T)
5580 else if (note->descsz == sizeof (elfcore_psinfo32_t))
5582 /* 64-bit host, 32-bit corefile */
5583 elfcore_psinfo32_t psinfo;
5585 memcpy (&psinfo, note->descdata, sizeof (psinfo));
5587 elf_tdata (abfd)->core_program
5588 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5590 elf_tdata (abfd)->core_command
5591 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5593 #endif
5595 else
5597 /* Fail - we don't know how to handle any other
5598 note size (ie. data object type). */
5599 return true;
5602 /* Note that for some reason, a spurious space is tacked
5603 onto the end of the args in some (at least one anyway)
5604 implementations, so strip it off if it exists. */
5607 char *command = elf_tdata (abfd)->core_command;
5608 int n = strlen (command);
5610 if (0 < n && command[n - 1] == ' ')
5611 command[n - 1] = '\0';
5614 return true;
5616 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5618 #if defined (HAVE_PSTATUS_T)
5619 static boolean
5620 elfcore_grok_pstatus (abfd, note)
5621 bfd *abfd;
5622 Elf_Internal_Note *note;
5624 if (note->descsz == sizeof (pstatus_t)
5625 #if defined (HAVE_PXSTATUS_T)
5626 || note->descsz == sizeof (pxstatus_t)
5627 #endif
5630 pstatus_t pstat;
5632 memcpy (&pstat, note->descdata, sizeof (pstat));
5634 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5636 #if defined (HAVE_PSTATUS32_T)
5637 else if (note->descsz == sizeof (pstatus32_t))
5639 /* 64-bit host, 32-bit corefile */
5640 pstatus32_t pstat;
5642 memcpy (&pstat, note->descdata, sizeof (pstat));
5644 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5646 #endif
5647 /* Could grab some more details from the "representative"
5648 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5649 NT_LWPSTATUS note, presumably. */
5651 return true;
5653 #endif /* defined (HAVE_PSTATUS_T) */
5655 #if defined (HAVE_LWPSTATUS_T)
5656 static boolean
5657 elfcore_grok_lwpstatus (abfd, note)
5658 bfd *abfd;
5659 Elf_Internal_Note *note;
5661 lwpstatus_t lwpstat;
5662 char buf[100];
5663 char *name;
5664 asection *sect;
5666 if (note->descsz != sizeof (lwpstat)
5667 #if defined (HAVE_LWPXSTATUS_T)
5668 && note->descsz != sizeof (lwpxstatus_t)
5669 #endif
5671 return true;
5673 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5675 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5676 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5678 /* Make a ".reg/999" section. */
5680 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5681 name = bfd_alloc (abfd, strlen (buf) + 1);
5682 if (name == NULL)
5683 return false;
5684 strcpy (name, buf);
5686 sect = bfd_make_section (abfd, name);
5687 if (sect == NULL)
5688 return false;
5690 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5691 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5692 sect->filepos = note->descpos
5693 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5694 #endif
5696 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5697 sect->_raw_size = sizeof (lwpstat.pr_reg);
5698 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5699 #endif
5701 sect->flags = SEC_HAS_CONTENTS;
5702 sect->alignment_power = 2;
5704 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5705 return false;
5707 /* Make a ".reg2/999" section */
5709 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5710 name = bfd_alloc (abfd, strlen (buf) + 1);
5711 if (name == NULL)
5712 return false;
5713 strcpy (name, buf);
5715 sect = bfd_make_section (abfd, name);
5716 if (sect == NULL)
5717 return false;
5719 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5720 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5721 sect->filepos = note->descpos
5722 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5723 #endif
5725 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5726 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5727 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5728 #endif
5730 sect->flags = SEC_HAS_CONTENTS;
5731 sect->alignment_power = 2;
5733 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5734 return false;
5736 return true;
5738 #endif /* defined (HAVE_LWPSTATUS_T) */
5740 #if defined (HAVE_WIN32_PSTATUS_T)
5741 static boolean
5742 elfcore_grok_win32pstatus (abfd, note)
5743 bfd *abfd;
5744 Elf_Internal_Note *note;
5746 char buf[30];
5747 char *name;
5748 asection *sect;
5749 win32_pstatus_t pstatus;
5751 if (note->descsz < sizeof (pstatus))
5752 return true;
5754 memcpy (&pstatus, note->descdata, note->descsz);
5756 switch (pstatus.data_type)
5758 case NOTE_INFO_PROCESS:
5759 /* FIXME: need to add ->core_command. */
5760 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5761 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5762 break;
5764 case NOTE_INFO_THREAD:
5765 /* Make a ".reg/999" section. */
5766 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5768 name = bfd_alloc (abfd, strlen (buf) + 1);
5769 if (name == NULL)
5770 return false;
5772 strcpy (name, buf);
5774 sect = bfd_make_section (abfd, name);
5775 if (sect == NULL)
5776 return false;
5778 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5779 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5780 data.thread_info.thread_context);
5781 sect->flags = SEC_HAS_CONTENTS;
5782 sect->alignment_power = 2;
5784 if (pstatus.data.thread_info.is_active_thread)
5785 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5786 return false;
5787 break;
5789 case NOTE_INFO_MODULE:
5790 /* Make a ".module/xxxxxxxx" section. */
5791 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address);
5793 name = bfd_alloc (abfd, strlen (buf) + 1);
5794 if (name == NULL)
5795 return false;
5797 strcpy (name, buf);
5799 sect = bfd_make_section (abfd, name);
5801 if (sect == NULL)
5802 return false;
5804 sect->_raw_size = note->descsz;
5805 sect->filepos = note->descpos;
5806 sect->flags = SEC_HAS_CONTENTS;
5807 sect->alignment_power = 2;
5808 break;
5810 default:
5811 return true;
5814 return true;
5816 #endif /* HAVE_WIN32_PSTATUS_T */
5818 static boolean
5819 elfcore_grok_note (abfd, note)
5820 bfd *abfd;
5821 Elf_Internal_Note *note;
5823 switch (note->type)
5825 default:
5826 return true;
5828 #if defined (HAVE_PRSTATUS_T)
5829 case NT_PRSTATUS:
5830 return elfcore_grok_prstatus (abfd, note);
5831 #endif
5833 #if defined (HAVE_PSTATUS_T)
5834 case NT_PSTATUS:
5835 return elfcore_grok_pstatus (abfd, note);
5836 #endif
5838 #if defined (HAVE_LWPSTATUS_T)
5839 case NT_LWPSTATUS:
5840 return elfcore_grok_lwpstatus (abfd, note);
5841 #endif
5843 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5844 return elfcore_grok_prfpreg (abfd, note);
5846 #if defined (HAVE_WIN32_PSTATUS_T)
5847 case NT_WIN32PSTATUS:
5848 return elfcore_grok_win32pstatus (abfd, note);
5849 #endif
5851 case NT_PRXFPREG: /* Linux SSE extension */
5852 if (note->namesz == 5
5853 && ! strcmp (note->namedata, "LINUX"))
5854 return elfcore_grok_prxfpreg (abfd, note);
5855 else
5856 return true;
5858 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5859 case NT_PRPSINFO:
5860 case NT_PSINFO:
5861 return elfcore_grok_psinfo (abfd, note);
5862 #endif
5866 static boolean
5867 elfcore_read_notes (abfd, offset, size)
5868 bfd *abfd;
5869 bfd_vma offset;
5870 bfd_vma size;
5872 char *buf;
5873 char *p;
5875 if (size <= 0)
5876 return true;
5878 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5879 return false;
5881 buf = bfd_malloc ((size_t) size);
5882 if (buf == NULL)
5883 return false;
5885 if (bfd_read (buf, size, 1, abfd) != size)
5887 error:
5888 free (buf);
5889 return false;
5892 p = buf;
5893 while (p < buf + size)
5895 /* FIXME: bad alignment assumption. */
5896 Elf_External_Note *xnp = (Elf_External_Note *) p;
5897 Elf_Internal_Note in;
5899 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5901 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5902 in.namedata = xnp->name;
5904 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5905 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5906 in.descpos = offset + (in.descdata - buf);
5908 if (! elfcore_grok_note (abfd, &in))
5909 goto error;
5911 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5914 free (buf);
5915 return true;
5918 /* FIXME: This function is now unnecessary. Callers can just call
5919 bfd_section_from_phdr directly. */
5921 boolean
5922 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5923 bfd *abfd;
5924 Elf_Internal_Phdr* phdr;
5925 int sec_num;
5927 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5928 return false;
5930 return true;
5933 /* Providing external access to the ELF program header table. */
5935 /* Return an upper bound on the number of bytes required to store a
5936 copy of ABFD's program header table entries. Return -1 if an error
5937 occurs; bfd_get_error will return an appropriate code. */
5939 long
5940 bfd_get_elf_phdr_upper_bound (abfd)
5941 bfd *abfd;
5943 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5945 bfd_set_error (bfd_error_wrong_format);
5946 return -1;
5949 return (elf_elfheader (abfd)->e_phnum
5950 * sizeof (Elf_Internal_Phdr));
5953 /* Copy ABFD's program header table entries to *PHDRS. The entries
5954 will be stored as an array of Elf_Internal_Phdr structures, as
5955 defined in include/elf/internal.h. To find out how large the
5956 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5958 Return the number of program header table entries read, or -1 if an
5959 error occurs; bfd_get_error will return an appropriate code. */
5962 bfd_get_elf_phdrs (abfd, phdrs)
5963 bfd *abfd;
5964 void *phdrs;
5966 int num_phdrs;
5968 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5970 bfd_set_error (bfd_error_wrong_format);
5971 return -1;
5974 num_phdrs = elf_elfheader (abfd)->e_phnum;
5975 memcpy (phdrs, elf_tdata (abfd)->phdr,
5976 num_phdrs * sizeof (Elf_Internal_Phdr));
5978 return num_phdrs;