2000-05-04 Michael Snyder <msnyder@seadog.cygnus.com>
[binutils.git] / bfd / elf.c
blob4c8846385282916068a7201c381851ced0a06dde
1 /* ELF executable support for BFD.
2 Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 SECTION
23 ELF backends
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
31 haven't bothered yet.
34 #include "bfd.h"
35 #include "sysdep.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #define ARCH_SIZE 0
39 #include "elf-bfd.h"
41 static INLINE struct elf_segment_map *make_mapping
42 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
43 static boolean map_sections_to_segments PARAMS ((bfd *));
44 static int elf_sort_sections PARAMS ((const PTR, const PTR));
45 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
46 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
47 static boolean prep_headers PARAMS ((bfd *));
48 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int));
49 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
50 static char *elf_read PARAMS ((bfd *, long, unsigned int));
51 static void elf_fake_sections PARAMS ((bfd *, asection *, PTR));
52 static boolean assign_section_numbers PARAMS ((bfd *));
53 static INLINE int sym_is_global PARAMS ((bfd *, asymbol *));
54 static boolean elf_map_symbols PARAMS ((bfd *));
55 static bfd_size_type get_program_header_size PARAMS ((bfd *));
56 static boolean elfcore_read_notes PARAMS ((bfd *, bfd_vma, bfd_vma));
58 /* Swap version information in and out. The version information is
59 currently size independent. If that ever changes, this code will
60 need to move into elfcode.h. */
62 /* Swap in a Verdef structure. */
64 void
65 _bfd_elf_swap_verdef_in (abfd, src, dst)
66 bfd *abfd;
67 const Elf_External_Verdef *src;
68 Elf_Internal_Verdef *dst;
70 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version);
71 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags);
72 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx);
73 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt);
74 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash);
75 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux);
76 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next);
79 /* Swap out a Verdef structure. */
81 void
82 _bfd_elf_swap_verdef_out (abfd, src, dst)
83 bfd *abfd;
84 const Elf_Internal_Verdef *src;
85 Elf_External_Verdef *dst;
87 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version);
88 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags);
89 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx);
90 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt);
91 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash);
92 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux);
93 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next);
96 /* Swap in a Verdaux structure. */
98 void
99 _bfd_elf_swap_verdaux_in (abfd, src, dst)
100 bfd *abfd;
101 const Elf_External_Verdaux *src;
102 Elf_Internal_Verdaux *dst;
104 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name);
105 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next);
108 /* Swap out a Verdaux structure. */
110 void
111 _bfd_elf_swap_verdaux_out (abfd, src, dst)
112 bfd *abfd;
113 const Elf_Internal_Verdaux *src;
114 Elf_External_Verdaux *dst;
116 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name);
117 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next);
120 /* Swap in a Verneed structure. */
122 void
123 _bfd_elf_swap_verneed_in (abfd, src, dst)
124 bfd *abfd;
125 const Elf_External_Verneed *src;
126 Elf_Internal_Verneed *dst;
128 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version);
129 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt);
130 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file);
131 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux);
132 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next);
135 /* Swap out a Verneed structure. */
137 void
138 _bfd_elf_swap_verneed_out (abfd, src, dst)
139 bfd *abfd;
140 const Elf_Internal_Verneed *src;
141 Elf_External_Verneed *dst;
143 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version);
144 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt);
145 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file);
146 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux);
147 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next);
150 /* Swap in a Vernaux structure. */
152 void
153 _bfd_elf_swap_vernaux_in (abfd, src, dst)
154 bfd *abfd;
155 const Elf_External_Vernaux *src;
156 Elf_Internal_Vernaux *dst;
158 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash);
159 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags);
160 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other);
161 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name);
162 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next);
165 /* Swap out a Vernaux structure. */
167 void
168 _bfd_elf_swap_vernaux_out (abfd, src, dst)
169 bfd *abfd;
170 const Elf_Internal_Vernaux *src;
171 Elf_External_Vernaux *dst;
173 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash);
174 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags);
175 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other);
176 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name);
177 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next);
180 /* Swap in a Versym structure. */
182 void
183 _bfd_elf_swap_versym_in (abfd, src, dst)
184 bfd *abfd;
185 const Elf_External_Versym *src;
186 Elf_Internal_Versym *dst;
188 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers);
191 /* Swap out a Versym structure. */
193 void
194 _bfd_elf_swap_versym_out (abfd, src, dst)
195 bfd *abfd;
196 const Elf_Internal_Versym *src;
197 Elf_External_Versym *dst;
199 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers);
202 /* Standard ELF hash function. Do not change this function; you will
203 cause invalid hash tables to be generated. */
205 unsigned long
206 bfd_elf_hash (namearg)
207 const char *namearg;
209 const unsigned char *name = (const unsigned char *) namearg;
210 unsigned long h = 0;
211 unsigned long g;
212 int ch;
214 while ((ch = *name++) != '\0')
216 h = (h << 4) + ch;
217 if ((g = (h & 0xf0000000)) != 0)
219 h ^= g >> 24;
220 /* The ELF ABI says `h &= ~g', but this is equivalent in
221 this case and on some machines one insn instead of two. */
222 h ^= g;
225 return h;
228 /* Read a specified number of bytes at a specified offset in an ELF
229 file, into a newly allocated buffer, and return a pointer to the
230 buffer. */
232 static char *
233 elf_read (abfd, offset, size)
234 bfd * abfd;
235 long offset;
236 unsigned int size;
238 char *buf;
240 if ((buf = bfd_alloc (abfd, size)) == NULL)
241 return NULL;
242 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
243 return NULL;
244 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
246 if (bfd_get_error () != bfd_error_system_call)
247 bfd_set_error (bfd_error_file_truncated);
248 return NULL;
250 return buf;
253 boolean
254 bfd_elf_mkobject (abfd)
255 bfd * abfd;
257 /* this just does initialization */
258 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
259 elf_tdata (abfd) = (struct elf_obj_tdata *)
260 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
261 if (elf_tdata (abfd) == 0)
262 return false;
263 /* since everything is done at close time, do we need any
264 initialization? */
266 return true;
269 boolean
270 bfd_elf_mkcorefile (abfd)
271 bfd * abfd;
273 /* I think this can be done just like an object file. */
274 return bfd_elf_mkobject (abfd);
277 char *
278 bfd_elf_get_str_section (abfd, shindex)
279 bfd * abfd;
280 unsigned int shindex;
282 Elf_Internal_Shdr **i_shdrp;
283 char *shstrtab = NULL;
284 unsigned int offset;
285 unsigned int shstrtabsize;
287 i_shdrp = elf_elfsections (abfd);
288 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
289 return 0;
291 shstrtab = (char *) i_shdrp[shindex]->contents;
292 if (shstrtab == NULL)
294 /* No cached one, attempt to read, and cache what we read. */
295 offset = i_shdrp[shindex]->sh_offset;
296 shstrtabsize = i_shdrp[shindex]->sh_size;
297 shstrtab = elf_read (abfd, offset, shstrtabsize);
298 i_shdrp[shindex]->contents = (PTR) shstrtab;
300 return shstrtab;
303 char *
304 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
305 bfd * abfd;
306 unsigned int shindex;
307 unsigned int strindex;
309 Elf_Internal_Shdr *hdr;
311 if (strindex == 0)
312 return "";
314 hdr = elf_elfsections (abfd)[shindex];
316 if (hdr->contents == NULL
317 && bfd_elf_get_str_section (abfd, shindex) == NULL)
318 return NULL;
320 if (strindex >= hdr->sh_size)
322 (*_bfd_error_handler)
323 (_("%s: invalid string offset %u >= %lu for section `%s'"),
324 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size,
325 ((shindex == elf_elfheader(abfd)->e_shstrndx
326 && strindex == hdr->sh_name)
327 ? ".shstrtab"
328 : elf_string_from_elf_strtab (abfd, hdr->sh_name)));
329 return "";
332 return ((char *) hdr->contents) + strindex;
335 /* Make a BFD section from an ELF section. We store a pointer to the
336 BFD section in the bfd_section field of the header. */
338 boolean
339 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
340 bfd *abfd;
341 Elf_Internal_Shdr *hdr;
342 const char *name;
344 asection *newsect;
345 flagword flags;
347 if (hdr->bfd_section != NULL)
349 BFD_ASSERT (strcmp (name,
350 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
351 return true;
354 newsect = bfd_make_section_anyway (abfd, name);
355 if (newsect == NULL)
356 return false;
358 newsect->filepos = hdr->sh_offset;
360 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
361 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
362 || ! bfd_set_section_alignment (abfd, newsect,
363 bfd_log2 (hdr->sh_addralign)))
364 return false;
366 flags = SEC_NO_FLAGS;
367 if (hdr->sh_type != SHT_NOBITS)
368 flags |= SEC_HAS_CONTENTS;
369 if ((hdr->sh_flags & SHF_ALLOC) != 0)
371 flags |= SEC_ALLOC;
372 if (hdr->sh_type != SHT_NOBITS)
373 flags |= SEC_LOAD;
375 if ((hdr->sh_flags & SHF_WRITE) == 0)
376 flags |= SEC_READONLY;
377 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
378 flags |= SEC_CODE;
379 else if ((flags & SEC_LOAD) != 0)
380 flags |= SEC_DATA;
382 /* The debugging sections appear to be recognized only by name, not
383 any sort of flag. */
384 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
385 || strncmp (name, ".line", sizeof ".line" - 1) == 0
386 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
387 flags |= SEC_DEBUGGING;
389 /* As a GNU extension, if the name begins with .gnu.linkonce, we
390 only link a single copy of the section. This is used to support
391 g++. g++ will emit each template expansion in its own section.
392 The symbols will be defined as weak, so that multiple definitions
393 are permitted. The GNU linker extension is to actually discard
394 all but one of the sections. */
395 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
396 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
398 if (! bfd_set_section_flags (abfd, newsect, flags))
399 return false;
401 if ((flags & SEC_ALLOC) != 0)
403 Elf_Internal_Phdr *phdr;
404 unsigned int i;
406 /* Look through the phdrs to see if we need to adjust the lma.
407 If all the p_paddr fields are zero, we ignore them, since
408 some ELF linkers produce such output. */
409 phdr = elf_tdata (abfd)->phdr;
410 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
412 if (phdr->p_paddr != 0)
413 break;
415 if (i < elf_elfheader (abfd)->e_phnum)
417 phdr = elf_tdata (abfd)->phdr;
418 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
420 if (phdr->p_type == PT_LOAD
421 && phdr->p_vaddr != phdr->p_paddr
422 && phdr->p_vaddr <= hdr->sh_addr
423 && (phdr->p_vaddr + phdr->p_memsz
424 >= hdr->sh_addr + hdr->sh_size)
425 && ((flags & SEC_LOAD) == 0
426 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset
427 && (phdr->p_offset + phdr->p_filesz
428 >= hdr->sh_offset + hdr->sh_size))))
430 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
431 break;
437 hdr->bfd_section = newsect;
438 elf_section_data (newsect)->this_hdr = *hdr;
440 return true;
444 INTERNAL_FUNCTION
445 bfd_elf_find_section
447 SYNOPSIS
448 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
450 DESCRIPTION
451 Helper functions for GDB to locate the string tables.
452 Since BFD hides string tables from callers, GDB needs to use an
453 internal hook to find them. Sun's .stabstr, in particular,
454 isn't even pointed to by the .stab section, so ordinary
455 mechanisms wouldn't work to find it, even if we had some.
458 struct elf_internal_shdr *
459 bfd_elf_find_section (abfd, name)
460 bfd * abfd;
461 char *name;
463 Elf_Internal_Shdr **i_shdrp;
464 char *shstrtab;
465 unsigned int max;
466 unsigned int i;
468 i_shdrp = elf_elfsections (abfd);
469 if (i_shdrp != NULL)
471 shstrtab = bfd_elf_get_str_section
472 (abfd, elf_elfheader (abfd)->e_shstrndx);
473 if (shstrtab != NULL)
475 max = elf_elfheader (abfd)->e_shnum;
476 for (i = 1; i < max; i++)
477 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
478 return i_shdrp[i];
481 return 0;
484 const char *const bfd_elf_section_type_names[] = {
485 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
486 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
487 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
490 /* ELF relocs are against symbols. If we are producing relocateable
491 output, and the reloc is against an external symbol, and nothing
492 has given us any additional addend, the resulting reloc will also
493 be against the same symbol. In such a case, we don't want to
494 change anything about the way the reloc is handled, since it will
495 all be done at final link time. Rather than put special case code
496 into bfd_perform_relocation, all the reloc types use this howto
497 function. It just short circuits the reloc if producing
498 relocateable output against an external symbol. */
500 /*ARGSUSED*/
501 bfd_reloc_status_type
502 bfd_elf_generic_reloc (abfd,
503 reloc_entry,
504 symbol,
505 data,
506 input_section,
507 output_bfd,
508 error_message)
509 bfd *abfd ATTRIBUTE_UNUSED;
510 arelent *reloc_entry;
511 asymbol *symbol;
512 PTR data ATTRIBUTE_UNUSED;
513 asection *input_section;
514 bfd *output_bfd;
515 char **error_message ATTRIBUTE_UNUSED;
517 if (output_bfd != (bfd *) NULL
518 && (symbol->flags & BSF_SECTION_SYM) == 0
519 && (! reloc_entry->howto->partial_inplace
520 || reloc_entry->addend == 0))
522 reloc_entry->address += input_section->output_offset;
523 return bfd_reloc_ok;
526 return bfd_reloc_continue;
529 /* Print out the program headers. */
531 boolean
532 _bfd_elf_print_private_bfd_data (abfd, farg)
533 bfd *abfd;
534 PTR farg;
536 FILE *f = (FILE *) farg;
537 Elf_Internal_Phdr *p;
538 asection *s;
539 bfd_byte *dynbuf = NULL;
541 p = elf_tdata (abfd)->phdr;
542 if (p != NULL)
544 unsigned int i, c;
546 fprintf (f, _("\nProgram Header:\n"));
547 c = elf_elfheader (abfd)->e_phnum;
548 for (i = 0; i < c; i++, p++)
550 const char *s;
551 char buf[20];
553 switch (p->p_type)
555 case PT_NULL: s = "NULL"; break;
556 case PT_LOAD: s = "LOAD"; break;
557 case PT_DYNAMIC: s = "DYNAMIC"; break;
558 case PT_INTERP: s = "INTERP"; break;
559 case PT_NOTE: s = "NOTE"; break;
560 case PT_SHLIB: s = "SHLIB"; break;
561 case PT_PHDR: s = "PHDR"; break;
562 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
564 fprintf (f, "%8s off 0x", s);
565 fprintf_vma (f, p->p_offset);
566 fprintf (f, " vaddr 0x");
567 fprintf_vma (f, p->p_vaddr);
568 fprintf (f, " paddr 0x");
569 fprintf_vma (f, p->p_paddr);
570 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
571 fprintf (f, " filesz 0x");
572 fprintf_vma (f, p->p_filesz);
573 fprintf (f, " memsz 0x");
574 fprintf_vma (f, p->p_memsz);
575 fprintf (f, " flags %c%c%c",
576 (p->p_flags & PF_R) != 0 ? 'r' : '-',
577 (p->p_flags & PF_W) != 0 ? 'w' : '-',
578 (p->p_flags & PF_X) != 0 ? 'x' : '-');
579 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
580 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
581 fprintf (f, "\n");
585 s = bfd_get_section_by_name (abfd, ".dynamic");
586 if (s != NULL)
588 int elfsec;
589 unsigned long link;
590 bfd_byte *extdyn, *extdynend;
591 size_t extdynsize;
592 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
594 fprintf (f, _("\nDynamic Section:\n"));
596 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
597 if (dynbuf == NULL)
598 goto error_return;
599 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
600 s->_raw_size))
601 goto error_return;
603 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
604 if (elfsec == -1)
605 goto error_return;
606 link = elf_elfsections (abfd)[elfsec]->sh_link;
608 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
609 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
611 extdyn = dynbuf;
612 extdynend = extdyn + s->_raw_size;
613 for (; extdyn < extdynend; extdyn += extdynsize)
615 Elf_Internal_Dyn dyn;
616 const char *name;
617 char ab[20];
618 boolean stringp;
620 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
622 if (dyn.d_tag == DT_NULL)
623 break;
625 stringp = false;
626 switch (dyn.d_tag)
628 default:
629 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
630 name = ab;
631 break;
633 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
634 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
635 case DT_PLTGOT: name = "PLTGOT"; break;
636 case DT_HASH: name = "HASH"; break;
637 case DT_STRTAB: name = "STRTAB"; break;
638 case DT_SYMTAB: name = "SYMTAB"; break;
639 case DT_RELA: name = "RELA"; break;
640 case DT_RELASZ: name = "RELASZ"; break;
641 case DT_RELAENT: name = "RELAENT"; break;
642 case DT_STRSZ: name = "STRSZ"; break;
643 case DT_SYMENT: name = "SYMENT"; break;
644 case DT_INIT: name = "INIT"; break;
645 case DT_FINI: name = "FINI"; break;
646 case DT_SONAME: name = "SONAME"; stringp = true; break;
647 case DT_RPATH: name = "RPATH"; stringp = true; break;
648 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
649 case DT_REL: name = "REL"; break;
650 case DT_RELSZ: name = "RELSZ"; break;
651 case DT_RELENT: name = "RELENT"; break;
652 case DT_PLTREL: name = "PLTREL"; break;
653 case DT_DEBUG: name = "DEBUG"; break;
654 case DT_TEXTREL: name = "TEXTREL"; break;
655 case DT_JMPREL: name = "JMPREL"; break;
656 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break;
657 case DT_FILTER: name = "FILTER"; stringp = true; break;
658 case DT_VERSYM: name = "VERSYM"; break;
659 case DT_VERDEF: name = "VERDEF"; break;
660 case DT_VERDEFNUM: name = "VERDEFNUM"; break;
661 case DT_VERNEED: name = "VERNEED"; break;
662 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break;
665 fprintf (f, " %-11s ", name);
666 if (! stringp)
667 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
668 else
670 const char *string;
672 string = bfd_elf_string_from_elf_section (abfd, link,
673 dyn.d_un.d_val);
674 if (string == NULL)
675 goto error_return;
676 fprintf (f, "%s", string);
678 fprintf (f, "\n");
681 free (dynbuf);
682 dynbuf = NULL;
685 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL)
686 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL))
688 if (! _bfd_elf_slurp_version_tables (abfd))
689 return false;
692 if (elf_dynverdef (abfd) != 0)
694 Elf_Internal_Verdef *t;
696 fprintf (f, _("\nVersion definitions:\n"));
697 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef)
699 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx,
700 t->vd_flags, t->vd_hash, t->vd_nodename);
701 if (t->vd_auxptr->vda_nextptr != NULL)
703 Elf_Internal_Verdaux *a;
705 fprintf (f, "\t");
706 for (a = t->vd_auxptr->vda_nextptr;
707 a != NULL;
708 a = a->vda_nextptr)
709 fprintf (f, "%s ", a->vda_nodename);
710 fprintf (f, "\n");
715 if (elf_dynverref (abfd) != 0)
717 Elf_Internal_Verneed *t;
719 fprintf (f, _("\nVersion References:\n"));
720 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref)
722 Elf_Internal_Vernaux *a;
724 fprintf (f, _(" required from %s:\n"), t->vn_filename);
725 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
726 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash,
727 a->vna_flags, a->vna_other, a->vna_nodename);
731 return true;
733 error_return:
734 if (dynbuf != NULL)
735 free (dynbuf);
736 return false;
739 /* Display ELF-specific fields of a symbol. */
741 void
742 bfd_elf_print_symbol (abfd, filep, symbol, how)
743 bfd *abfd;
744 PTR filep;
745 asymbol *symbol;
746 bfd_print_symbol_type how;
748 FILE *file = (FILE *) filep;
749 switch (how)
751 case bfd_print_symbol_name:
752 fprintf (file, "%s", symbol->name);
753 break;
754 case bfd_print_symbol_more:
755 fprintf (file, "elf ");
756 fprintf_vma (file, symbol->value);
757 fprintf (file, " %lx", (long) symbol->flags);
758 break;
759 case bfd_print_symbol_all:
761 CONST char *section_name;
762 CONST char *name = NULL;
763 struct elf_backend_data *bed;
764 unsigned char st_other;
766 section_name = symbol->section ? symbol->section->name : "(*none*)";
768 bed = get_elf_backend_data (abfd);
769 if (bed->elf_backend_print_symbol_all)
770 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol);
772 if (name == NULL)
774 name = symbol->name;
775 bfd_print_symbol_vandf ((PTR) file, symbol);
778 fprintf (file, " %s\t", section_name);
779 /* Print the "other" value for a symbol. For common symbols,
780 we've already printed the size; now print the alignment.
781 For other symbols, we have no specified alignment, and
782 we've printed the address; now print the size. */
783 fprintf_vma (file,
784 (bfd_is_com_section (symbol->section)
785 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
786 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
788 /* If we have version information, print it. */
789 if (elf_tdata (abfd)->dynversym_section != 0
790 && (elf_tdata (abfd)->dynverdef_section != 0
791 || elf_tdata (abfd)->dynverref_section != 0))
793 unsigned int vernum;
794 const char *version_string;
796 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION;
798 if (vernum == 0)
799 version_string = "";
800 else if (vernum == 1)
801 version_string = "Base";
802 else if (vernum <= elf_tdata (abfd)->cverdefs)
803 version_string =
804 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
805 else
807 Elf_Internal_Verneed *t;
809 version_string = "";
810 for (t = elf_tdata (abfd)->verref;
811 t != NULL;
812 t = t->vn_nextref)
814 Elf_Internal_Vernaux *a;
816 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
818 if (a->vna_other == vernum)
820 version_string = a->vna_nodename;
821 break;
827 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0)
828 fprintf (file, " %-11s", version_string);
829 else
831 int i;
833 fprintf (file, " (%s)", version_string);
834 for (i = 10 - strlen (version_string); i > 0; --i)
835 putc (' ', file);
839 /* If the st_other field is not zero, print it. */
840 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other;
842 switch (st_other)
844 case 0: break;
845 case STV_INTERNAL: fprintf (file, " .internal"); break;
846 case STV_HIDDEN: fprintf (file, " .hidden"); break;
847 case STV_PROTECTED: fprintf (file, " .protected"); break;
848 default:
849 /* Some other non-defined flags are also present, so print
850 everything hex. */
851 fprintf (file, " 0x%02x", (unsigned int) st_other);
854 fprintf (file, " %s", name);
856 break;
860 /* Create an entry in an ELF linker hash table. */
862 struct bfd_hash_entry *
863 _bfd_elf_link_hash_newfunc (entry, table, string)
864 struct bfd_hash_entry *entry;
865 struct bfd_hash_table *table;
866 const char *string;
868 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
870 /* Allocate the structure if it has not already been allocated by a
871 subclass. */
872 if (ret == (struct elf_link_hash_entry *) NULL)
873 ret = ((struct elf_link_hash_entry *)
874 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
875 if (ret == (struct elf_link_hash_entry *) NULL)
876 return (struct bfd_hash_entry *) ret;
878 /* Call the allocation method of the superclass. */
879 ret = ((struct elf_link_hash_entry *)
880 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
881 table, string));
882 if (ret != (struct elf_link_hash_entry *) NULL)
884 /* Set local fields. */
885 ret->indx = -1;
886 ret->size = 0;
887 ret->dynindx = -1;
888 ret->dynstr_index = 0;
889 ret->weakdef = NULL;
890 ret->got.offset = (bfd_vma) -1;
891 ret->plt.offset = (bfd_vma) -1;
892 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
893 ret->verinfo.verdef = NULL;
894 ret->vtable_entries_used = NULL;
895 ret->vtable_entries_size = 0;
896 ret->vtable_parent = NULL;
897 ret->type = STT_NOTYPE;
898 ret->other = 0;
899 /* Assume that we have been called by a non-ELF symbol reader.
900 This flag is then reset by the code which reads an ELF input
901 file. This ensures that a symbol created by a non-ELF symbol
902 reader will have the flag set correctly. */
903 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
906 return (struct bfd_hash_entry *) ret;
909 /* Copy data from an indirect symbol to its direct symbol, hiding the
910 old indirect symbol. */
912 void
913 _bfd_elf_link_hash_copy_indirect (dir, ind)
914 struct elf_link_hash_entry *dir, *ind;
916 /* Copy down any references that we may have already seen to the
917 symbol which just became indirect. */
919 dir->elf_link_hash_flags |=
920 (ind->elf_link_hash_flags
921 & (ELF_LINK_HASH_REF_DYNAMIC
922 | ELF_LINK_HASH_REF_REGULAR
923 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
924 | ELF_LINK_NON_GOT_REF));
926 /* Copy over the global and procedure linkage table offset entries.
927 These may have been already set up by a check_relocs routine. */
928 if (dir->got.offset == (bfd_vma) -1)
930 dir->got.offset = ind->got.offset;
931 ind->got.offset = (bfd_vma) -1;
933 BFD_ASSERT (ind->got.offset == (bfd_vma) -1);
935 if (dir->plt.offset == (bfd_vma) -1)
937 dir->plt.offset = ind->plt.offset;
938 ind->plt.offset = (bfd_vma) -1;
940 BFD_ASSERT (ind->plt.offset == (bfd_vma) -1);
942 if (dir->dynindx == -1)
944 dir->dynindx = ind->dynindx;
945 dir->dynstr_index = ind->dynstr_index;
946 ind->dynindx = -1;
947 ind->dynstr_index = 0;
949 BFD_ASSERT (ind->dynindx == -1);
952 void
953 _bfd_elf_link_hash_hide_symbol(h)
954 struct elf_link_hash_entry *h;
956 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
957 h->dynindx = -1;
958 h->plt.offset = (bfd_vma) -1;
961 /* Initialize an ELF linker hash table. */
963 boolean
964 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
965 struct elf_link_hash_table *table;
966 bfd *abfd;
967 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
968 struct bfd_hash_table *,
969 const char *));
971 table->dynamic_sections_created = false;
972 table->dynobj = NULL;
973 /* The first dynamic symbol is a dummy. */
974 table->dynsymcount = 1;
975 table->dynstr = NULL;
976 table->bucketcount = 0;
977 table->needed = NULL;
978 table->hgot = NULL;
979 table->stab_info = NULL;
980 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
983 /* Create an ELF linker hash table. */
985 struct bfd_link_hash_table *
986 _bfd_elf_link_hash_table_create (abfd)
987 bfd *abfd;
989 struct elf_link_hash_table *ret;
991 ret = ((struct elf_link_hash_table *)
992 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
993 if (ret == (struct elf_link_hash_table *) NULL)
994 return NULL;
996 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
998 bfd_release (abfd, ret);
999 return NULL;
1002 return &ret->root;
1005 /* This is a hook for the ELF emulation code in the generic linker to
1006 tell the backend linker what file name to use for the DT_NEEDED
1007 entry for a dynamic object. The generic linker passes name as an
1008 empty string to indicate that no DT_NEEDED entry should be made. */
1010 void
1011 bfd_elf_set_dt_needed_name (abfd, name)
1012 bfd *abfd;
1013 const char *name;
1015 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1016 && bfd_get_format (abfd) == bfd_object)
1017 elf_dt_name (abfd) = name;
1020 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1021 the linker ELF emulation code. */
1023 struct bfd_link_needed_list *
1024 bfd_elf_get_needed_list (abfd, info)
1025 bfd *abfd ATTRIBUTE_UNUSED;
1026 struct bfd_link_info *info;
1028 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1029 return NULL;
1030 return elf_hash_table (info)->needed;
1033 /* Get the name actually used for a dynamic object for a link. This
1034 is the SONAME entry if there is one. Otherwise, it is the string
1035 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1037 const char *
1038 bfd_elf_get_dt_soname (abfd)
1039 bfd *abfd;
1041 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1042 && bfd_get_format (abfd) == bfd_object)
1043 return elf_dt_name (abfd);
1044 return NULL;
1047 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1048 the ELF linker emulation code. */
1050 boolean
1051 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1052 bfd *abfd;
1053 struct bfd_link_needed_list **pneeded;
1055 asection *s;
1056 bfd_byte *dynbuf = NULL;
1057 int elfsec;
1058 unsigned long link;
1059 bfd_byte *extdyn, *extdynend;
1060 size_t extdynsize;
1061 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1063 *pneeded = NULL;
1065 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1066 || bfd_get_format (abfd) != bfd_object)
1067 return true;
1069 s = bfd_get_section_by_name (abfd, ".dynamic");
1070 if (s == NULL || s->_raw_size == 0)
1071 return true;
1073 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1074 if (dynbuf == NULL)
1075 goto error_return;
1077 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1078 s->_raw_size))
1079 goto error_return;
1081 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1082 if (elfsec == -1)
1083 goto error_return;
1085 link = elf_elfsections (abfd)[elfsec]->sh_link;
1087 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1088 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1090 extdyn = dynbuf;
1091 extdynend = extdyn + s->_raw_size;
1092 for (; extdyn < extdynend; extdyn += extdynsize)
1094 Elf_Internal_Dyn dyn;
1096 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1098 if (dyn.d_tag == DT_NULL)
1099 break;
1101 if (dyn.d_tag == DT_NEEDED)
1103 const char *string;
1104 struct bfd_link_needed_list *l;
1106 string = bfd_elf_string_from_elf_section (abfd, link,
1107 dyn.d_un.d_val);
1108 if (string == NULL)
1109 goto error_return;
1111 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1112 if (l == NULL)
1113 goto error_return;
1115 l->by = abfd;
1116 l->name = string;
1117 l->next = *pneeded;
1118 *pneeded = l;
1122 free (dynbuf);
1124 return true;
1126 error_return:
1127 if (dynbuf != NULL)
1128 free (dynbuf);
1129 return false;
1132 /* Allocate an ELF string table--force the first byte to be zero. */
1134 struct bfd_strtab_hash *
1135 _bfd_elf_stringtab_init ()
1137 struct bfd_strtab_hash *ret;
1139 ret = _bfd_stringtab_init ();
1140 if (ret != NULL)
1142 bfd_size_type loc;
1144 loc = _bfd_stringtab_add (ret, "", true, false);
1145 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1146 if (loc == (bfd_size_type) -1)
1148 _bfd_stringtab_free (ret);
1149 ret = NULL;
1152 return ret;
1155 /* ELF .o/exec file reading */
1157 /* Create a new bfd section from an ELF section header. */
1159 boolean
1160 bfd_section_from_shdr (abfd, shindex)
1161 bfd *abfd;
1162 unsigned int shindex;
1164 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1165 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1166 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1167 char *name;
1169 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1171 switch (hdr->sh_type)
1173 case SHT_NULL:
1174 /* Inactive section. Throw it away. */
1175 return true;
1177 case SHT_PROGBITS: /* Normal section with contents. */
1178 case SHT_DYNAMIC: /* Dynamic linking information. */
1179 case SHT_NOBITS: /* .bss section. */
1180 case SHT_HASH: /* .hash section. */
1181 case SHT_NOTE: /* .note section. */
1182 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1184 case SHT_SYMTAB: /* A symbol table */
1185 if (elf_onesymtab (abfd) == shindex)
1186 return true;
1188 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1189 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1190 elf_onesymtab (abfd) = shindex;
1191 elf_tdata (abfd)->symtab_hdr = *hdr;
1192 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1193 abfd->flags |= HAS_SYMS;
1195 /* Sometimes a shared object will map in the symbol table. If
1196 SHF_ALLOC is set, and this is a shared object, then we also
1197 treat this section as a BFD section. We can not base the
1198 decision purely on SHF_ALLOC, because that flag is sometimes
1199 set in a relocateable object file, which would confuse the
1200 linker. */
1201 if ((hdr->sh_flags & SHF_ALLOC) != 0
1202 && (abfd->flags & DYNAMIC) != 0
1203 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1204 return false;
1206 return true;
1208 case SHT_DYNSYM: /* A dynamic symbol table */
1209 if (elf_dynsymtab (abfd) == shindex)
1210 return true;
1212 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1213 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1214 elf_dynsymtab (abfd) = shindex;
1215 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1216 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1217 abfd->flags |= HAS_SYMS;
1219 /* Besides being a symbol table, we also treat this as a regular
1220 section, so that objcopy can handle it. */
1221 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1223 case SHT_STRTAB: /* A string table */
1224 if (hdr->bfd_section != NULL)
1225 return true;
1226 if (ehdr->e_shstrndx == shindex)
1228 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1229 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1230 return true;
1233 unsigned int i;
1235 for (i = 1; i < ehdr->e_shnum; i++)
1237 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1238 if (hdr2->sh_link == shindex)
1240 if (! bfd_section_from_shdr (abfd, i))
1241 return false;
1242 if (elf_onesymtab (abfd) == i)
1244 elf_tdata (abfd)->strtab_hdr = *hdr;
1245 elf_elfsections (abfd)[shindex] =
1246 &elf_tdata (abfd)->strtab_hdr;
1247 return true;
1249 if (elf_dynsymtab (abfd) == i)
1251 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1252 elf_elfsections (abfd)[shindex] = hdr =
1253 &elf_tdata (abfd)->dynstrtab_hdr;
1254 /* We also treat this as a regular section, so
1255 that objcopy can handle it. */
1256 break;
1258 #if 0 /* Not handling other string tables specially right now. */
1259 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1260 /* We have a strtab for some random other section. */
1261 newsect = (asection *) hdr2->bfd_section;
1262 if (!newsect)
1263 break;
1264 hdr->bfd_section = newsect;
1265 hdr2 = &elf_section_data (newsect)->str_hdr;
1266 *hdr2 = *hdr;
1267 elf_elfsections (abfd)[shindex] = hdr2;
1268 #endif
1273 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1275 case SHT_REL:
1276 case SHT_RELA:
1277 /* *These* do a lot of work -- but build no sections! */
1279 asection *target_sect;
1280 Elf_Internal_Shdr *hdr2;
1282 /* Check for a bogus link to avoid crashing. */
1283 if (hdr->sh_link >= ehdr->e_shnum)
1285 ((*_bfd_error_handler)
1286 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1287 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1288 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1291 /* For some incomprehensible reason Oracle distributes
1292 libraries for Solaris in which some of the objects have
1293 bogus sh_link fields. It would be nice if we could just
1294 reject them, but, unfortunately, some people need to use
1295 them. We scan through the section headers; if we find only
1296 one suitable symbol table, we clobber the sh_link to point
1297 to it. I hope this doesn't break anything. */
1298 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1299 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1301 int scan;
1302 int found;
1304 found = 0;
1305 for (scan = 1; scan < ehdr->e_shnum; scan++)
1307 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1308 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1310 if (found != 0)
1312 found = 0;
1313 break;
1315 found = scan;
1318 if (found != 0)
1319 hdr->sh_link = found;
1322 /* Get the symbol table. */
1323 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1324 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1325 return false;
1327 /* If this reloc section does not use the main symbol table we
1328 don't treat it as a reloc section. BFD can't adequately
1329 represent such a section, so at least for now, we don't
1330 try. We just present it as a normal section. */
1331 if (hdr->sh_link != elf_onesymtab (abfd))
1332 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1334 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1335 return false;
1336 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1337 if (target_sect == NULL)
1338 return false;
1340 if ((target_sect->flags & SEC_RELOC) == 0
1341 || target_sect->reloc_count == 0)
1342 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1343 else
1345 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1346 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1347 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1349 *hdr2 = *hdr;
1350 elf_elfsections (abfd)[shindex] = hdr2;
1351 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1352 target_sect->flags |= SEC_RELOC;
1353 target_sect->relocation = NULL;
1354 target_sect->rel_filepos = hdr->sh_offset;
1355 /* In the section to which the relocations apply, mark whether
1356 its relocations are of the REL or RELA variety. */
1357 elf_section_data (target_sect)->use_rela_p
1358 = (hdr->sh_type == SHT_RELA);
1359 abfd->flags |= HAS_RELOC;
1360 return true;
1362 break;
1364 case SHT_GNU_verdef:
1365 elf_dynverdef (abfd) = shindex;
1366 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1367 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1368 break;
1370 case SHT_GNU_versym:
1371 elf_dynversym (abfd) = shindex;
1372 elf_tdata (abfd)->dynversym_hdr = *hdr;
1373 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1374 break;
1376 case SHT_GNU_verneed:
1377 elf_dynverref (abfd) = shindex;
1378 elf_tdata (abfd)->dynverref_hdr = *hdr;
1379 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1380 break;
1382 case SHT_SHLIB:
1383 return true;
1385 default:
1386 /* Check for any processor-specific section types. */
1388 if (bed->elf_backend_section_from_shdr)
1389 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1391 break;
1394 return true;
1397 /* Given an ELF section number, retrieve the corresponding BFD
1398 section. */
1400 asection *
1401 bfd_section_from_elf_index (abfd, index)
1402 bfd *abfd;
1403 unsigned int index;
1405 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1406 if (index >= elf_elfheader (abfd)->e_shnum)
1407 return NULL;
1408 return elf_elfsections (abfd)[index]->bfd_section;
1411 boolean
1412 _bfd_elf_new_section_hook (abfd, sec)
1413 bfd *abfd;
1414 asection *sec;
1416 struct bfd_elf_section_data *sdata;
1418 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1419 if (!sdata)
1420 return false;
1421 sec->used_by_bfd = (PTR) sdata;
1423 /* Indicate whether or not this section should use RELA relocations. */
1424 sdata->use_rela_p
1425 = get_elf_backend_data (abfd)->default_use_rela_p;
1427 return true;
1430 /* Create a new bfd section from an ELF program header.
1432 Since program segments have no names, we generate a synthetic name
1433 of the form segment<NUM>, where NUM is generally the index in the
1434 program header table. For segments that are split (see below) we
1435 generate the names segment<NUM>a and segment<NUM>b.
1437 Note that some program segments may have a file size that is different than
1438 (less than) the memory size. All this means is that at execution the
1439 system must allocate the amount of memory specified by the memory size,
1440 but only initialize it with the first "file size" bytes read from the
1441 file. This would occur for example, with program segments consisting
1442 of combined data+bss.
1444 To handle the above situation, this routine generates TWO bfd sections
1445 for the single program segment. The first has the length specified by
1446 the file size of the segment, and the second has the length specified
1447 by the difference between the two sizes. In effect, the segment is split
1448 into it's initialized and uninitialized parts.
1452 boolean
1453 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1454 bfd *abfd;
1455 Elf_Internal_Phdr *hdr;
1456 int index;
1457 const char *typename;
1459 asection *newsect;
1460 char *name;
1461 char namebuf[64];
1462 int split;
1464 split = ((hdr->p_memsz > 0)
1465 && (hdr->p_filesz > 0)
1466 && (hdr->p_memsz > hdr->p_filesz));
1467 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1468 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1469 if (!name)
1470 return false;
1471 strcpy (name, namebuf);
1472 newsect = bfd_make_section (abfd, name);
1473 if (newsect == NULL)
1474 return false;
1475 newsect->vma = hdr->p_vaddr;
1476 newsect->lma = hdr->p_paddr;
1477 newsect->_raw_size = hdr->p_filesz;
1478 newsect->filepos = hdr->p_offset;
1479 newsect->flags |= SEC_HAS_CONTENTS;
1480 if (hdr->p_type == PT_LOAD)
1482 newsect->flags |= SEC_ALLOC;
1483 newsect->flags |= SEC_LOAD;
1484 if (hdr->p_flags & PF_X)
1486 /* FIXME: all we known is that it has execute PERMISSION,
1487 may be data. */
1488 newsect->flags |= SEC_CODE;
1491 if (!(hdr->p_flags & PF_W))
1493 newsect->flags |= SEC_READONLY;
1496 if (split)
1498 sprintf (namebuf, "%s%db", typename, index);
1499 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1500 if (!name)
1501 return false;
1502 strcpy (name, namebuf);
1503 newsect = bfd_make_section (abfd, name);
1504 if (newsect == NULL)
1505 return false;
1506 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1507 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1508 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1509 if (hdr->p_type == PT_LOAD)
1511 newsect->flags |= SEC_ALLOC;
1512 if (hdr->p_flags & PF_X)
1513 newsect->flags |= SEC_CODE;
1515 if (!(hdr->p_flags & PF_W))
1516 newsect->flags |= SEC_READONLY;
1519 return true;
1522 boolean
1523 bfd_section_from_phdr (abfd, hdr, index)
1524 bfd *abfd;
1525 Elf_Internal_Phdr *hdr;
1526 int index;
1528 struct elf_backend_data *bed;
1530 switch (hdr->p_type)
1532 case PT_NULL:
1533 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1535 case PT_LOAD:
1536 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1538 case PT_DYNAMIC:
1539 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1541 case PT_INTERP:
1542 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1544 case PT_NOTE:
1545 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1546 return false;
1547 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1548 return false;
1549 return true;
1551 case PT_SHLIB:
1552 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1554 case PT_PHDR:
1555 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1557 default:
1558 /* Check for any processor-specific program segment types.
1559 If no handler for them, default to making "segment" sections. */
1560 bed = get_elf_backend_data (abfd);
1561 if (bed->elf_backend_section_from_phdr)
1562 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1563 else
1564 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1568 /* Initialize REL_HDR, the section-header for new section, containing
1569 relocations against ASECT. If USE_RELA_P is true, we use RELA
1570 relocations; otherwise, we use REL relocations. */
1572 boolean
1573 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1574 bfd *abfd;
1575 Elf_Internal_Shdr *rel_hdr;
1576 asection *asect;
1577 boolean use_rela_p;
1579 char *name;
1580 struct elf_backend_data *bed;
1582 bed = get_elf_backend_data (abfd);
1583 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1584 if (name == NULL)
1585 return false;
1586 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1587 rel_hdr->sh_name =
1588 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1589 true, false);
1590 if (rel_hdr->sh_name == (unsigned int) -1)
1591 return false;
1592 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1593 rel_hdr->sh_entsize = (use_rela_p
1594 ? bed->s->sizeof_rela
1595 : bed->s->sizeof_rel);
1596 rel_hdr->sh_addralign = bed->s->file_align;
1597 rel_hdr->sh_flags = 0;
1598 rel_hdr->sh_addr = 0;
1599 rel_hdr->sh_size = 0;
1600 rel_hdr->sh_offset = 0;
1602 return true;
1605 /* Set up an ELF internal section header for a section. */
1607 /*ARGSUSED*/
1608 static void
1609 elf_fake_sections (abfd, asect, failedptrarg)
1610 bfd *abfd;
1611 asection *asect;
1612 PTR failedptrarg;
1614 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1615 boolean *failedptr = (boolean *) failedptrarg;
1616 Elf_Internal_Shdr *this_hdr;
1618 if (*failedptr)
1620 /* We already failed; just get out of the bfd_map_over_sections
1621 loop. */
1622 return;
1625 this_hdr = &elf_section_data (asect)->this_hdr;
1627 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1628 asect->name,
1629 true, false);
1630 if (this_hdr->sh_name == (unsigned long) -1)
1632 *failedptr = true;
1633 return;
1636 this_hdr->sh_flags = 0;
1638 if ((asect->flags & SEC_ALLOC) != 0
1639 || asect->user_set_vma)
1640 this_hdr->sh_addr = asect->vma;
1641 else
1642 this_hdr->sh_addr = 0;
1644 this_hdr->sh_offset = 0;
1645 this_hdr->sh_size = asect->_raw_size;
1646 this_hdr->sh_link = 0;
1647 this_hdr->sh_addralign = 1 << asect->alignment_power;
1648 /* The sh_entsize and sh_info fields may have been set already by
1649 copy_private_section_data. */
1651 this_hdr->bfd_section = asect;
1652 this_hdr->contents = NULL;
1654 /* FIXME: This should not be based on section names. */
1655 if (strcmp (asect->name, ".dynstr") == 0)
1656 this_hdr->sh_type = SHT_STRTAB;
1657 else if (strcmp (asect->name, ".hash") == 0)
1659 this_hdr->sh_type = SHT_HASH;
1660 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1662 else if (strcmp (asect->name, ".dynsym") == 0)
1664 this_hdr->sh_type = SHT_DYNSYM;
1665 this_hdr->sh_entsize = bed->s->sizeof_sym;
1667 else if (strcmp (asect->name, ".dynamic") == 0)
1669 this_hdr->sh_type = SHT_DYNAMIC;
1670 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1672 else if (strncmp (asect->name, ".rela", 5) == 0
1673 && get_elf_backend_data (abfd)->may_use_rela_p)
1675 this_hdr->sh_type = SHT_RELA;
1676 this_hdr->sh_entsize = bed->s->sizeof_rela;
1678 else if (strncmp (asect->name, ".rel", 4) == 0
1679 && get_elf_backend_data (abfd)->may_use_rel_p)
1681 this_hdr->sh_type = SHT_REL;
1682 this_hdr->sh_entsize = bed->s->sizeof_rel;
1684 else if (strncmp (asect->name, ".note", 5) == 0)
1685 this_hdr->sh_type = SHT_NOTE;
1686 else if (strncmp (asect->name, ".stab", 5) == 0
1687 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1688 this_hdr->sh_type = SHT_STRTAB;
1689 else if (strcmp (asect->name, ".gnu.version") == 0)
1691 this_hdr->sh_type = SHT_GNU_versym;
1692 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1694 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1696 this_hdr->sh_type = SHT_GNU_verdef;
1697 this_hdr->sh_entsize = 0;
1698 /* objcopy or strip will copy over sh_info, but may not set
1699 cverdefs. The linker will set cverdefs, but sh_info will be
1700 zero. */
1701 if (this_hdr->sh_info == 0)
1702 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1703 else
1704 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1705 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1707 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1709 this_hdr->sh_type = SHT_GNU_verneed;
1710 this_hdr->sh_entsize = 0;
1711 /* objcopy or strip will copy over sh_info, but may not set
1712 cverrefs. The linker will set cverrefs, but sh_info will be
1713 zero. */
1714 if (this_hdr->sh_info == 0)
1715 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1716 else
1717 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1718 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1720 else if ((asect->flags & SEC_ALLOC) != 0
1721 && (asect->flags & SEC_LOAD) != 0)
1722 this_hdr->sh_type = SHT_PROGBITS;
1723 else if ((asect->flags & SEC_ALLOC) != 0
1724 && ((asect->flags & SEC_LOAD) == 0))
1725 this_hdr->sh_type = SHT_NOBITS;
1726 else
1728 /* Who knows? */
1729 this_hdr->sh_type = SHT_PROGBITS;
1732 if ((asect->flags & SEC_ALLOC) != 0)
1733 this_hdr->sh_flags |= SHF_ALLOC;
1734 if ((asect->flags & SEC_READONLY) == 0)
1735 this_hdr->sh_flags |= SHF_WRITE;
1736 if ((asect->flags & SEC_CODE) != 0)
1737 this_hdr->sh_flags |= SHF_EXECINSTR;
1739 /* Check for processor-specific section types. */
1740 if (bed->elf_backend_fake_sections)
1741 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1743 /* If the section has relocs, set up a section header for the
1744 SHT_REL[A] section. If two relocation sections are required for
1745 this section, it is up to the processor-specific back-end to
1746 create the other. */
1747 if ((asect->flags & SEC_RELOC) != 0
1748 && !_bfd_elf_init_reloc_shdr (abfd,
1749 &elf_section_data (asect)->rel_hdr,
1750 asect,
1751 elf_section_data (asect)->use_rela_p))
1752 *failedptr = true;
1755 /* Get elf arch size (32 / 64).
1756 Returns -1 if not elf. */
1759 bfd_elf_get_arch_size (abfd)
1760 bfd *abfd;
1762 if (abfd->xvec->flavour != bfd_target_elf_flavour)
1764 bfd_set_error (bfd_error_wrong_format);
1765 return -1;
1768 return (get_elf_backend_data (abfd))->s->arch_size;
1771 /* Assign all ELF section numbers. The dummy first section is handled here
1772 too. The link/info pointers for the standard section types are filled
1773 in here too, while we're at it. */
1775 static boolean
1776 assign_section_numbers (abfd)
1777 bfd *abfd;
1779 struct elf_obj_tdata *t = elf_tdata (abfd);
1780 asection *sec;
1781 unsigned int section_number;
1782 Elf_Internal_Shdr **i_shdrp;
1784 section_number = 1;
1786 for (sec = abfd->sections; sec; sec = sec->next)
1788 struct bfd_elf_section_data *d = elf_section_data (sec);
1790 d->this_idx = section_number++;
1791 if ((sec->flags & SEC_RELOC) == 0)
1792 d->rel_idx = 0;
1793 else
1794 d->rel_idx = section_number++;
1796 if (d->rel_hdr2)
1797 d->rel_idx2 = section_number++;
1798 else
1799 d->rel_idx2 = 0;
1802 t->shstrtab_section = section_number++;
1803 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1804 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1806 if (bfd_get_symcount (abfd) > 0)
1808 t->symtab_section = section_number++;
1809 t->strtab_section = section_number++;
1812 elf_elfheader (abfd)->e_shnum = section_number;
1814 /* Set up the list of section header pointers, in agreement with the
1815 indices. */
1816 i_shdrp = ((Elf_Internal_Shdr **)
1817 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1818 if (i_shdrp == NULL)
1819 return false;
1821 i_shdrp[0] = ((Elf_Internal_Shdr *)
1822 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1823 if (i_shdrp[0] == NULL)
1825 bfd_release (abfd, i_shdrp);
1826 return false;
1828 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1830 elf_elfsections (abfd) = i_shdrp;
1832 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1833 if (bfd_get_symcount (abfd) > 0)
1835 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1836 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1837 t->symtab_hdr.sh_link = t->strtab_section;
1839 for (sec = abfd->sections; sec; sec = sec->next)
1841 struct bfd_elf_section_data *d = elf_section_data (sec);
1842 asection *s;
1843 const char *name;
1845 i_shdrp[d->this_idx] = &d->this_hdr;
1846 if (d->rel_idx != 0)
1847 i_shdrp[d->rel_idx] = &d->rel_hdr;
1848 if (d->rel_idx2 != 0)
1849 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1851 /* Fill in the sh_link and sh_info fields while we're at it. */
1853 /* sh_link of a reloc section is the section index of the symbol
1854 table. sh_info is the section index of the section to which
1855 the relocation entries apply. */
1856 if (d->rel_idx != 0)
1858 d->rel_hdr.sh_link = t->symtab_section;
1859 d->rel_hdr.sh_info = d->this_idx;
1861 if (d->rel_idx2 != 0)
1863 d->rel_hdr2->sh_link = t->symtab_section;
1864 d->rel_hdr2->sh_info = d->this_idx;
1867 switch (d->this_hdr.sh_type)
1869 case SHT_REL:
1870 case SHT_RELA:
1871 /* A reloc section which we are treating as a normal BFD
1872 section. sh_link is the section index of the symbol
1873 table. sh_info is the section index of the section to
1874 which the relocation entries apply. We assume that an
1875 allocated reloc section uses the dynamic symbol table.
1876 FIXME: How can we be sure? */
1877 s = bfd_get_section_by_name (abfd, ".dynsym");
1878 if (s != NULL)
1879 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1881 /* We look up the section the relocs apply to by name. */
1882 name = sec->name;
1883 if (d->this_hdr.sh_type == SHT_REL)
1884 name += 4;
1885 else
1886 name += 5;
1887 s = bfd_get_section_by_name (abfd, name);
1888 if (s != NULL)
1889 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1890 break;
1892 case SHT_STRTAB:
1893 /* We assume that a section named .stab*str is a stabs
1894 string section. We look for a section with the same name
1895 but without the trailing ``str'', and set its sh_link
1896 field to point to this section. */
1897 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1898 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1900 size_t len;
1901 char *alc;
1903 len = strlen (sec->name);
1904 alc = (char *) bfd_malloc (len - 2);
1905 if (alc == NULL)
1906 return false;
1907 strncpy (alc, sec->name, len - 3);
1908 alc[len - 3] = '\0';
1909 s = bfd_get_section_by_name (abfd, alc);
1910 free (alc);
1911 if (s != NULL)
1913 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1915 /* This is a .stab section. */
1916 elf_section_data (s)->this_hdr.sh_entsize =
1917 4 + 2 * bfd_elf_get_arch_size (abfd) / 8;
1920 break;
1922 case SHT_DYNAMIC:
1923 case SHT_DYNSYM:
1924 case SHT_GNU_verneed:
1925 case SHT_GNU_verdef:
1926 /* sh_link is the section header index of the string table
1927 used for the dynamic entries, or the symbol table, or the
1928 version strings. */
1929 s = bfd_get_section_by_name (abfd, ".dynstr");
1930 if (s != NULL)
1931 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1932 break;
1934 case SHT_HASH:
1935 case SHT_GNU_versym:
1936 /* sh_link is the section header index of the symbol table
1937 this hash table or version table is for. */
1938 s = bfd_get_section_by_name (abfd, ".dynsym");
1939 if (s != NULL)
1940 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1941 break;
1945 return true;
1948 /* Map symbol from it's internal number to the external number, moving
1949 all local symbols to be at the head of the list. */
1951 static INLINE int
1952 sym_is_global (abfd, sym)
1953 bfd *abfd;
1954 asymbol *sym;
1956 /* If the backend has a special mapping, use it. */
1957 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1958 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1959 (abfd, sym));
1961 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1962 || bfd_is_und_section (bfd_get_section (sym))
1963 || bfd_is_com_section (bfd_get_section (sym)));
1966 static boolean
1967 elf_map_symbols (abfd)
1968 bfd *abfd;
1970 int symcount = bfd_get_symcount (abfd);
1971 asymbol **syms = bfd_get_outsymbols (abfd);
1972 asymbol **sect_syms;
1973 int num_locals = 0;
1974 int num_globals = 0;
1975 int num_locals2 = 0;
1976 int num_globals2 = 0;
1977 int max_index = 0;
1978 int num_sections = 0;
1979 int idx;
1980 asection *asect;
1981 asymbol **new_syms;
1982 asymbol *sym;
1984 #ifdef DEBUG
1985 fprintf (stderr, "elf_map_symbols\n");
1986 fflush (stderr);
1987 #endif
1989 /* Add a section symbol for each BFD section. FIXME: Is this really
1990 necessary? */
1991 for (asect = abfd->sections; asect; asect = asect->next)
1993 if (max_index < asect->index)
1994 max_index = asect->index;
1997 max_index++;
1998 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1999 if (sect_syms == NULL)
2000 return false;
2001 elf_section_syms (abfd) = sect_syms;
2003 for (idx = 0; idx < symcount; idx++)
2005 sym = syms[idx];
2007 if ((sym->flags & BSF_SECTION_SYM) != 0
2008 && sym->value == 0)
2010 asection *sec;
2012 sec = sym->section;
2014 if (sec->owner != NULL)
2016 if (sec->owner != abfd)
2018 if (sec->output_offset != 0)
2019 continue;
2021 sec = sec->output_section;
2023 /* Empty sections in the input files may have had a section
2024 symbol created for them. (See the comment near the end of
2025 _bfd_generic_link_output_symbols in linker.c). If the linker
2026 script discards such sections then we will reach this point.
2027 Since we know that we cannot avoid this case, we detect it
2028 and skip the abort and the assignment to the sect_syms array.
2029 To reproduce this particular case try running the linker
2030 testsuite test ld-scripts/weak.exp for an ELF port that uses
2031 the generic linker. */
2032 if (sec->owner == NULL)
2033 continue;
2035 BFD_ASSERT (sec->owner == abfd);
2037 sect_syms[sec->index] = syms[idx];
2042 for (asect = abfd->sections; asect; asect = asect->next)
2044 if (sect_syms[asect->index] != NULL)
2045 continue;
2047 sym = bfd_make_empty_symbol (abfd);
2048 if (sym == NULL)
2049 return false;
2050 sym->the_bfd = abfd;
2051 sym->name = asect->name;
2052 sym->value = 0;
2053 /* Set the flags to 0 to indicate that this one was newly added. */
2054 sym->flags = 0;
2055 sym->section = asect;
2056 sect_syms[asect->index] = sym;
2057 num_sections++;
2058 #ifdef DEBUG
2059 fprintf (stderr,
2060 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2061 asect->name, (long) asect->vma, asect->index, (long) asect);
2062 #endif
2065 /* Classify all of the symbols. */
2066 for (idx = 0; idx < symcount; idx++)
2068 if (!sym_is_global (abfd, syms[idx]))
2069 num_locals++;
2070 else
2071 num_globals++;
2073 for (asect = abfd->sections; asect; asect = asect->next)
2075 if (sect_syms[asect->index] != NULL
2076 && sect_syms[asect->index]->flags == 0)
2078 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2079 if (!sym_is_global (abfd, sect_syms[asect->index]))
2080 num_locals++;
2081 else
2082 num_globals++;
2083 sect_syms[asect->index]->flags = 0;
2087 /* Now sort the symbols so the local symbols are first. */
2088 new_syms = ((asymbol **)
2089 bfd_alloc (abfd,
2090 (num_locals + num_globals) * sizeof (asymbol *)));
2091 if (new_syms == NULL)
2092 return false;
2094 for (idx = 0; idx < symcount; idx++)
2096 asymbol *sym = syms[idx];
2097 int i;
2099 if (!sym_is_global (abfd, sym))
2100 i = num_locals2++;
2101 else
2102 i = num_locals + num_globals2++;
2103 new_syms[i] = sym;
2104 sym->udata.i = i + 1;
2106 for (asect = abfd->sections; asect; asect = asect->next)
2108 if (sect_syms[asect->index] != NULL
2109 && sect_syms[asect->index]->flags == 0)
2111 asymbol *sym = sect_syms[asect->index];
2112 int i;
2114 sym->flags = BSF_SECTION_SYM;
2115 if (!sym_is_global (abfd, sym))
2116 i = num_locals2++;
2117 else
2118 i = num_locals + num_globals2++;
2119 new_syms[i] = sym;
2120 sym->udata.i = i + 1;
2124 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2126 elf_num_locals (abfd) = num_locals;
2127 elf_num_globals (abfd) = num_globals;
2128 return true;
2131 /* Align to the maximum file alignment that could be required for any
2132 ELF data structure. */
2134 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2135 static INLINE file_ptr
2136 align_file_position (off, align)
2137 file_ptr off;
2138 int align;
2140 return (off + align - 1) & ~(align - 1);
2143 /* Assign a file position to a section, optionally aligning to the
2144 required section alignment. */
2146 INLINE file_ptr
2147 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2148 Elf_Internal_Shdr *i_shdrp;
2149 file_ptr offset;
2150 boolean align;
2152 if (align)
2154 unsigned int al;
2156 al = i_shdrp->sh_addralign;
2157 if (al > 1)
2158 offset = BFD_ALIGN (offset, al);
2160 i_shdrp->sh_offset = offset;
2161 if (i_shdrp->bfd_section != NULL)
2162 i_shdrp->bfd_section->filepos = offset;
2163 if (i_shdrp->sh_type != SHT_NOBITS)
2164 offset += i_shdrp->sh_size;
2165 return offset;
2168 /* Compute the file positions we are going to put the sections at, and
2169 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2170 is not NULL, this is being called by the ELF backend linker. */
2172 boolean
2173 _bfd_elf_compute_section_file_positions (abfd, link_info)
2174 bfd *abfd;
2175 struct bfd_link_info *link_info;
2177 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2178 boolean failed;
2179 struct bfd_strtab_hash *strtab;
2180 Elf_Internal_Shdr *shstrtab_hdr;
2182 if (abfd->output_has_begun)
2183 return true;
2185 /* Do any elf backend specific processing first. */
2186 if (bed->elf_backend_begin_write_processing)
2187 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2189 if (! prep_headers (abfd))
2190 return false;
2192 /* Post process the headers if necessary. */
2193 if (bed->elf_backend_post_process_headers)
2194 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2196 failed = false;
2197 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2198 if (failed)
2199 return false;
2201 if (!assign_section_numbers (abfd))
2202 return false;
2204 /* The backend linker builds symbol table information itself. */
2205 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2207 /* Non-zero if doing a relocatable link. */
2208 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2210 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2211 return false;
2214 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2215 /* sh_name was set in prep_headers. */
2216 shstrtab_hdr->sh_type = SHT_STRTAB;
2217 shstrtab_hdr->sh_flags = 0;
2218 shstrtab_hdr->sh_addr = 0;
2219 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2220 shstrtab_hdr->sh_entsize = 0;
2221 shstrtab_hdr->sh_link = 0;
2222 shstrtab_hdr->sh_info = 0;
2223 /* sh_offset is set in assign_file_positions_except_relocs. */
2224 shstrtab_hdr->sh_addralign = 1;
2226 if (!assign_file_positions_except_relocs (abfd))
2227 return false;
2229 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2231 file_ptr off;
2232 Elf_Internal_Shdr *hdr;
2234 off = elf_tdata (abfd)->next_file_pos;
2236 hdr = &elf_tdata (abfd)->symtab_hdr;
2237 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2239 hdr = &elf_tdata (abfd)->strtab_hdr;
2240 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2242 elf_tdata (abfd)->next_file_pos = off;
2244 /* Now that we know where the .strtab section goes, write it
2245 out. */
2246 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2247 || ! _bfd_stringtab_emit (abfd, strtab))
2248 return false;
2249 _bfd_stringtab_free (strtab);
2252 abfd->output_has_begun = true;
2254 return true;
2257 /* Create a mapping from a set of sections to a program segment. */
2259 static INLINE struct elf_segment_map *
2260 make_mapping (abfd, sections, from, to, phdr)
2261 bfd *abfd;
2262 asection **sections;
2263 unsigned int from;
2264 unsigned int to;
2265 boolean phdr;
2267 struct elf_segment_map *m;
2268 unsigned int i;
2269 asection **hdrpp;
2271 m = ((struct elf_segment_map *)
2272 bfd_zalloc (abfd,
2273 (sizeof (struct elf_segment_map)
2274 + (to - from - 1) * sizeof (asection *))));
2275 if (m == NULL)
2276 return NULL;
2277 m->next = NULL;
2278 m->p_type = PT_LOAD;
2279 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2280 m->sections[i - from] = *hdrpp;
2281 m->count = to - from;
2283 if (from == 0 && phdr)
2285 /* Include the headers in the first PT_LOAD segment. */
2286 m->includes_filehdr = 1;
2287 m->includes_phdrs = 1;
2290 return m;
2293 /* Set up a mapping from BFD sections to program segments. */
2295 static boolean
2296 map_sections_to_segments (abfd)
2297 bfd *abfd;
2299 asection **sections = NULL;
2300 asection *s;
2301 unsigned int i;
2302 unsigned int count;
2303 struct elf_segment_map *mfirst;
2304 struct elf_segment_map **pm;
2305 struct elf_segment_map *m;
2306 asection *last_hdr;
2307 unsigned int phdr_index;
2308 bfd_vma maxpagesize;
2309 asection **hdrpp;
2310 boolean phdr_in_segment = true;
2311 boolean writable;
2312 asection *dynsec;
2314 if (elf_tdata (abfd)->segment_map != NULL)
2315 return true;
2317 if (bfd_count_sections (abfd) == 0)
2318 return true;
2320 /* Select the allocated sections, and sort them. */
2322 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2323 * sizeof (asection *));
2324 if (sections == NULL)
2325 goto error_return;
2327 i = 0;
2328 for (s = abfd->sections; s != NULL; s = s->next)
2330 if ((s->flags & SEC_ALLOC) != 0)
2332 sections[i] = s;
2333 ++i;
2336 BFD_ASSERT (i <= bfd_count_sections (abfd));
2337 count = i;
2339 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2341 /* Build the mapping. */
2343 mfirst = NULL;
2344 pm = &mfirst;
2346 /* If we have a .interp section, then create a PT_PHDR segment for
2347 the program headers and a PT_INTERP segment for the .interp
2348 section. */
2349 s = bfd_get_section_by_name (abfd, ".interp");
2350 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2352 m = ((struct elf_segment_map *)
2353 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2354 if (m == NULL)
2355 goto error_return;
2356 m->next = NULL;
2357 m->p_type = PT_PHDR;
2358 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2359 m->p_flags = PF_R | PF_X;
2360 m->p_flags_valid = 1;
2361 m->includes_phdrs = 1;
2363 *pm = m;
2364 pm = &m->next;
2366 m = ((struct elf_segment_map *)
2367 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2368 if (m == NULL)
2369 goto error_return;
2370 m->next = NULL;
2371 m->p_type = PT_INTERP;
2372 m->count = 1;
2373 m->sections[0] = s;
2375 *pm = m;
2376 pm = &m->next;
2379 /* Look through the sections. We put sections in the same program
2380 segment when the start of the second section can be placed within
2381 a few bytes of the end of the first section. */
2382 last_hdr = NULL;
2383 phdr_index = 0;
2384 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2385 writable = false;
2386 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2387 if (dynsec != NULL
2388 && (dynsec->flags & SEC_LOAD) == 0)
2389 dynsec = NULL;
2391 /* Deal with -Ttext or something similar such that the first section
2392 is not adjacent to the program headers. This is an
2393 approximation, since at this point we don't know exactly how many
2394 program headers we will need. */
2395 if (count > 0)
2397 bfd_size_type phdr_size;
2399 phdr_size = elf_tdata (abfd)->program_header_size;
2400 if (phdr_size == 0)
2401 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2402 if ((abfd->flags & D_PAGED) == 0
2403 || sections[0]->lma < phdr_size
2404 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2405 phdr_in_segment = false;
2408 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2410 asection *hdr;
2411 boolean new_segment;
2413 hdr = *hdrpp;
2415 /* See if this section and the last one will fit in the same
2416 segment. */
2418 if (last_hdr == NULL)
2420 /* If we don't have a segment yet, then we don't need a new
2421 one (we build the last one after this loop). */
2422 new_segment = false;
2424 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2426 /* If this section has a different relation between the
2427 virtual address and the load address, then we need a new
2428 segment. */
2429 new_segment = true;
2431 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2432 < BFD_ALIGN (hdr->lma, maxpagesize))
2434 /* If putting this section in this segment would force us to
2435 skip a page in the segment, then we need a new segment. */
2436 new_segment = true;
2438 else if ((last_hdr->flags & SEC_LOAD) == 0
2439 && (hdr->flags & SEC_LOAD) != 0)
2441 /* We don't want to put a loadable section after a
2442 nonloadable section in the same segment. */
2443 new_segment = true;
2445 else if ((abfd->flags & D_PAGED) == 0)
2447 /* If the file is not demand paged, which means that we
2448 don't require the sections to be correctly aligned in the
2449 file, then there is no other reason for a new segment. */
2450 new_segment = false;
2452 else if (! writable
2453 && (hdr->flags & SEC_READONLY) == 0
2454 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2455 == hdr->lma))
2457 /* We don't want to put a writable section in a read only
2458 segment, unless they are on the same page in memory
2459 anyhow. We already know that the last section does not
2460 bring us past the current section on the page, so the
2461 only case in which the new section is not on the same
2462 page as the previous section is when the previous section
2463 ends precisely on a page boundary. */
2464 new_segment = true;
2466 else
2468 /* Otherwise, we can use the same segment. */
2469 new_segment = false;
2472 if (! new_segment)
2474 if ((hdr->flags & SEC_READONLY) == 0)
2475 writable = true;
2476 last_hdr = hdr;
2477 continue;
2480 /* We need a new program segment. We must create a new program
2481 header holding all the sections from phdr_index until hdr. */
2483 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2484 if (m == NULL)
2485 goto error_return;
2487 *pm = m;
2488 pm = &m->next;
2490 if ((hdr->flags & SEC_READONLY) == 0)
2491 writable = true;
2492 else
2493 writable = false;
2495 last_hdr = hdr;
2496 phdr_index = i;
2497 phdr_in_segment = false;
2500 /* Create a final PT_LOAD program segment. */
2501 if (last_hdr != NULL)
2503 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2504 if (m == NULL)
2505 goto error_return;
2507 *pm = m;
2508 pm = &m->next;
2511 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2512 if (dynsec != NULL)
2514 m = ((struct elf_segment_map *)
2515 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2516 if (m == NULL)
2517 goto error_return;
2518 m->next = NULL;
2519 m->p_type = PT_DYNAMIC;
2520 m->count = 1;
2521 m->sections[0] = dynsec;
2523 *pm = m;
2524 pm = &m->next;
2527 /* For each loadable .note section, add a PT_NOTE segment. We don't
2528 use bfd_get_section_by_name, because if we link together
2529 nonloadable .note sections and loadable .note sections, we will
2530 generate two .note sections in the output file. FIXME: Using
2531 names for section types is bogus anyhow. */
2532 for (s = abfd->sections; s != NULL; s = s->next)
2534 if ((s->flags & SEC_LOAD) != 0
2535 && strncmp (s->name, ".note", 5) == 0)
2537 m = ((struct elf_segment_map *)
2538 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2539 if (m == NULL)
2540 goto error_return;
2541 m->next = NULL;
2542 m->p_type = PT_NOTE;
2543 m->count = 1;
2544 m->sections[0] = s;
2546 *pm = m;
2547 pm = &m->next;
2551 free (sections);
2552 sections = NULL;
2554 elf_tdata (abfd)->segment_map = mfirst;
2555 return true;
2557 error_return:
2558 if (sections != NULL)
2559 free (sections);
2560 return false;
2563 /* Sort sections by address. */
2565 static int
2566 elf_sort_sections (arg1, arg2)
2567 const PTR arg1;
2568 const PTR arg2;
2570 const asection *sec1 = *(const asection **) arg1;
2571 const asection *sec2 = *(const asection **) arg2;
2573 /* Sort by LMA first, since this is the address used to
2574 place the section into a segment. */
2575 if (sec1->lma < sec2->lma)
2576 return -1;
2577 else if (sec1->lma > sec2->lma)
2578 return 1;
2580 /* Then sort by VMA. Normally the LMA and the VMA will be
2581 the same, and this will do nothing. */
2582 if (sec1->vma < sec2->vma)
2583 return -1;
2584 else if (sec1->vma > sec2->vma)
2585 return 1;
2587 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2589 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2591 if (TOEND (sec1))
2593 if (TOEND (sec2))
2594 return sec1->target_index - sec2->target_index;
2595 else
2596 return 1;
2599 if (TOEND (sec2))
2600 return -1;
2602 #undef TOEND
2604 /* Sort by size, to put zero sized sections before others at the
2605 same address. */
2607 if (sec1->_raw_size < sec2->_raw_size)
2608 return -1;
2609 if (sec1->_raw_size > sec2->_raw_size)
2610 return 1;
2612 return sec1->target_index - sec2->target_index;
2615 /* Assign file positions to the sections based on the mapping from
2616 sections to segments. This function also sets up some fields in
2617 the file header, and writes out the program headers. */
2619 static boolean
2620 assign_file_positions_for_segments (abfd)
2621 bfd *abfd;
2623 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2624 unsigned int count;
2625 struct elf_segment_map *m;
2626 unsigned int alloc;
2627 Elf_Internal_Phdr *phdrs;
2628 file_ptr off, voff;
2629 bfd_vma filehdr_vaddr, filehdr_paddr;
2630 bfd_vma phdrs_vaddr, phdrs_paddr;
2631 Elf_Internal_Phdr *p;
2633 if (elf_tdata (abfd)->segment_map == NULL)
2635 if (! map_sections_to_segments (abfd))
2636 return false;
2639 if (bed->elf_backend_modify_segment_map)
2641 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2642 return false;
2645 count = 0;
2646 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2647 ++count;
2649 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2650 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2651 elf_elfheader (abfd)->e_phnum = count;
2653 if (count == 0)
2654 return true;
2656 /* If we already counted the number of program segments, make sure
2657 that we allocated enough space. This happens when SIZEOF_HEADERS
2658 is used in a linker script. */
2659 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2660 if (alloc != 0 && count > alloc)
2662 ((*_bfd_error_handler)
2663 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2664 bfd_get_filename (abfd), alloc, count));
2665 bfd_set_error (bfd_error_bad_value);
2666 return false;
2669 if (alloc == 0)
2670 alloc = count;
2672 phdrs = ((Elf_Internal_Phdr *)
2673 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2674 if (phdrs == NULL)
2675 return false;
2677 off = bed->s->sizeof_ehdr;
2678 off += alloc * bed->s->sizeof_phdr;
2680 filehdr_vaddr = 0;
2681 filehdr_paddr = 0;
2682 phdrs_vaddr = 0;
2683 phdrs_paddr = 0;
2685 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2686 m != NULL;
2687 m = m->next, p++)
2689 unsigned int i;
2690 asection **secpp;
2692 /* If elf_segment_map is not from map_sections_to_segments, the
2693 sections may not be correctly ordered. */
2694 if (m->count > 0)
2695 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2696 elf_sort_sections);
2698 p->p_type = m->p_type;
2699 p->p_flags = m->p_flags;
2701 if (p->p_type == PT_LOAD
2702 && m->count > 0
2703 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2705 if ((abfd->flags & D_PAGED) != 0)
2706 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2707 else
2709 bfd_size_type align;
2711 align = 0;
2712 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2714 bfd_size_type secalign;
2716 secalign = bfd_get_section_alignment (abfd, *secpp);
2717 if (secalign > align)
2718 align = secalign;
2721 off += (m->sections[0]->vma - off) % (1 << align);
2725 if (m->count == 0)
2726 p->p_vaddr = 0;
2727 else
2728 p->p_vaddr = m->sections[0]->vma;
2730 if (m->p_paddr_valid)
2731 p->p_paddr = m->p_paddr;
2732 else if (m->count == 0)
2733 p->p_paddr = 0;
2734 else
2735 p->p_paddr = m->sections[0]->lma;
2737 if (p->p_type == PT_LOAD
2738 && (abfd->flags & D_PAGED) != 0)
2739 p->p_align = bed->maxpagesize;
2740 else if (m->count == 0)
2741 p->p_align = bed->s->file_align;
2742 else
2743 p->p_align = 0;
2745 p->p_offset = 0;
2746 p->p_filesz = 0;
2747 p->p_memsz = 0;
2749 if (m->includes_filehdr)
2751 if (! m->p_flags_valid)
2752 p->p_flags |= PF_R;
2753 p->p_offset = 0;
2754 p->p_filesz = bed->s->sizeof_ehdr;
2755 p->p_memsz = bed->s->sizeof_ehdr;
2756 if (m->count > 0)
2758 BFD_ASSERT (p->p_type == PT_LOAD);
2760 if (p->p_vaddr < (bfd_vma) off)
2762 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2763 bfd_get_filename (abfd));
2764 bfd_set_error (bfd_error_bad_value);
2765 return false;
2768 p->p_vaddr -= off;
2769 if (! m->p_paddr_valid)
2770 p->p_paddr -= off;
2772 if (p->p_type == PT_LOAD)
2774 filehdr_vaddr = p->p_vaddr;
2775 filehdr_paddr = p->p_paddr;
2779 if (m->includes_phdrs)
2781 if (! m->p_flags_valid)
2782 p->p_flags |= PF_R;
2784 if (m->includes_filehdr)
2786 if (p->p_type == PT_LOAD)
2788 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2789 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2792 else
2794 p->p_offset = bed->s->sizeof_ehdr;
2796 if (m->count > 0)
2798 BFD_ASSERT (p->p_type == PT_LOAD);
2799 p->p_vaddr -= off - p->p_offset;
2800 if (! m->p_paddr_valid)
2801 p->p_paddr -= off - p->p_offset;
2804 if (p->p_type == PT_LOAD)
2806 phdrs_vaddr = p->p_vaddr;
2807 phdrs_paddr = p->p_paddr;
2809 else
2810 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2813 p->p_filesz += alloc * bed->s->sizeof_phdr;
2814 p->p_memsz += alloc * bed->s->sizeof_phdr;
2817 if (p->p_type == PT_LOAD
2818 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2820 if (! m->includes_filehdr && ! m->includes_phdrs)
2821 p->p_offset = off;
2822 else
2824 file_ptr adjust;
2826 adjust = off - (p->p_offset + p->p_filesz);
2827 p->p_filesz += adjust;
2828 p->p_memsz += adjust;
2832 voff = off;
2834 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2836 asection *sec;
2837 flagword flags;
2838 bfd_size_type align;
2840 sec = *secpp;
2841 flags = sec->flags;
2842 align = 1 << bfd_get_section_alignment (abfd, sec);
2844 /* The section may have artificial alignment forced by a
2845 link script. Notice this case by the gap between the
2846 cumulative phdr vma and the section's vma. */
2847 if (p->p_vaddr + p->p_memsz < sec->vma)
2849 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2851 p->p_memsz += adjust;
2852 off += adjust;
2853 voff += adjust;
2854 if ((flags & SEC_LOAD) != 0)
2855 p->p_filesz += adjust;
2858 if (p->p_type == PT_LOAD)
2860 bfd_signed_vma adjust;
2862 if ((flags & SEC_LOAD) != 0)
2864 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2865 if (adjust < 0)
2866 adjust = 0;
2868 else if ((flags & SEC_ALLOC) != 0)
2870 /* The section VMA must equal the file position
2871 modulo the page size. FIXME: I'm not sure if
2872 this adjustment is really necessary. We used to
2873 not have the SEC_LOAD case just above, and then
2874 this was necessary, but now I'm not sure. */
2875 if ((abfd->flags & D_PAGED) != 0)
2876 adjust = (sec->vma - voff) % bed->maxpagesize;
2877 else
2878 adjust = (sec->vma - voff) % align;
2880 else
2881 adjust = 0;
2883 if (adjust != 0)
2885 if (i == 0)
2887 (* _bfd_error_handler)
2888 (_("Error: First section in segment (%s) starts at 0x%x"),
2889 bfd_section_name (abfd, sec), sec->lma);
2890 (* _bfd_error_handler)
2891 (_(" whereas segment starts at 0x%x"),
2892 p->p_paddr);
2894 return false;
2896 p->p_memsz += adjust;
2897 off += adjust;
2898 voff += adjust;
2899 if ((flags & SEC_LOAD) != 0)
2900 p->p_filesz += adjust;
2903 sec->filepos = off;
2905 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2906 used in a linker script we may have a section with
2907 SEC_LOAD clear but which is supposed to have
2908 contents. */
2909 if ((flags & SEC_LOAD) != 0
2910 || (flags & SEC_HAS_CONTENTS) != 0)
2911 off += sec->_raw_size;
2913 if ((flags & SEC_ALLOC) != 0)
2914 voff += sec->_raw_size;
2917 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2919 if (i == 0) /* the actual "note" segment */
2920 { /* this one actually contains everything. */
2921 sec->filepos = off;
2922 p->p_filesz = sec->_raw_size;
2923 off += sec->_raw_size;
2924 voff = off;
2926 else /* fake sections -- don't need to be written */
2928 sec->filepos = 0;
2929 sec->_raw_size = 0;
2930 flags = sec->flags = 0; /* no contents */
2932 p->p_memsz = 0;
2933 p->p_align = 1;
2935 else
2937 p->p_memsz += sec->_raw_size;
2939 if ((flags & SEC_LOAD) != 0)
2940 p->p_filesz += sec->_raw_size;
2942 if (align > p->p_align
2943 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2944 p->p_align = align;
2947 if (! m->p_flags_valid)
2949 p->p_flags |= PF_R;
2950 if ((flags & SEC_CODE) != 0)
2951 p->p_flags |= PF_X;
2952 if ((flags & SEC_READONLY) == 0)
2953 p->p_flags |= PF_W;
2958 /* Now that we have set the section file positions, we can set up
2959 the file positions for the non PT_LOAD segments. */
2960 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2961 m != NULL;
2962 m = m->next, p++)
2964 if (p->p_type != PT_LOAD && m->count > 0)
2966 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2967 p->p_offset = m->sections[0]->filepos;
2969 if (m->count == 0)
2971 if (m->includes_filehdr)
2973 p->p_vaddr = filehdr_vaddr;
2974 if (! m->p_paddr_valid)
2975 p->p_paddr = filehdr_paddr;
2977 else if (m->includes_phdrs)
2979 p->p_vaddr = phdrs_vaddr;
2980 if (! m->p_paddr_valid)
2981 p->p_paddr = phdrs_paddr;
2986 /* Clear out any program headers we allocated but did not use. */
2987 for (; count < alloc; count++, p++)
2989 memset (p, 0, sizeof *p);
2990 p->p_type = PT_NULL;
2993 elf_tdata (abfd)->phdr = phdrs;
2995 elf_tdata (abfd)->next_file_pos = off;
2997 /* Write out the program headers. */
2998 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2999 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3000 return false;
3002 return true;
3005 /* Get the size of the program header.
3007 If this is called by the linker before any of the section VMA's are set, it
3008 can't calculate the correct value for a strange memory layout. This only
3009 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3010 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3011 data segment (exclusive of .interp and .dynamic).
3013 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3014 will be two segments. */
3016 static bfd_size_type
3017 get_program_header_size (abfd)
3018 bfd *abfd;
3020 size_t segs;
3021 asection *s;
3022 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3024 /* We can't return a different result each time we're called. */
3025 if (elf_tdata (abfd)->program_header_size != 0)
3026 return elf_tdata (abfd)->program_header_size;
3028 if (elf_tdata (abfd)->segment_map != NULL)
3030 struct elf_segment_map *m;
3032 segs = 0;
3033 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3034 ++segs;
3035 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3036 return elf_tdata (abfd)->program_header_size;
3039 /* Assume we will need exactly two PT_LOAD segments: one for text
3040 and one for data. */
3041 segs = 2;
3043 s = bfd_get_section_by_name (abfd, ".interp");
3044 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3046 /* If we have a loadable interpreter section, we need a
3047 PT_INTERP segment. In this case, assume we also need a
3048 PT_PHDR segment, although that may not be true for all
3049 targets. */
3050 segs += 2;
3053 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3055 /* We need a PT_DYNAMIC segment. */
3056 ++segs;
3059 for (s = abfd->sections; s != NULL; s = s->next)
3061 if ((s->flags & SEC_LOAD) != 0
3062 && strncmp (s->name, ".note", 5) == 0)
3064 /* We need a PT_NOTE segment. */
3065 ++segs;
3069 /* Let the backend count up any program headers it might need. */
3070 if (bed->elf_backend_additional_program_headers)
3072 int a;
3074 a = (*bed->elf_backend_additional_program_headers) (abfd);
3075 if (a == -1)
3076 abort ();
3077 segs += a;
3080 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3081 return elf_tdata (abfd)->program_header_size;
3084 /* Work out the file positions of all the sections. This is called by
3085 _bfd_elf_compute_section_file_positions. All the section sizes and
3086 VMAs must be known before this is called.
3088 We do not consider reloc sections at this point, unless they form
3089 part of the loadable image. Reloc sections are assigned file
3090 positions in assign_file_positions_for_relocs, which is called by
3091 write_object_contents and final_link.
3093 We also don't set the positions of the .symtab and .strtab here. */
3095 static boolean
3096 assign_file_positions_except_relocs (abfd)
3097 bfd *abfd;
3099 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3100 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3101 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3102 file_ptr off;
3103 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3105 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3106 && bfd_get_format (abfd) != bfd_core)
3108 Elf_Internal_Shdr **hdrpp;
3109 unsigned int i;
3111 /* Start after the ELF header. */
3112 off = i_ehdrp->e_ehsize;
3114 /* We are not creating an executable, which means that we are
3115 not creating a program header, and that the actual order of
3116 the sections in the file is unimportant. */
3117 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3119 Elf_Internal_Shdr *hdr;
3121 hdr = *hdrpp;
3122 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3124 hdr->sh_offset = -1;
3125 continue;
3127 if (i == tdata->symtab_section
3128 || i == tdata->strtab_section)
3130 hdr->sh_offset = -1;
3131 continue;
3134 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3137 else
3139 unsigned int i;
3140 Elf_Internal_Shdr **hdrpp;
3142 /* Assign file positions for the loaded sections based on the
3143 assignment of sections to segments. */
3144 if (! assign_file_positions_for_segments (abfd))
3145 return false;
3147 /* Assign file positions for the other sections. */
3149 off = elf_tdata (abfd)->next_file_pos;
3150 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3152 Elf_Internal_Shdr *hdr;
3154 hdr = *hdrpp;
3155 if (hdr->bfd_section != NULL
3156 && hdr->bfd_section->filepos != 0)
3157 hdr->sh_offset = hdr->bfd_section->filepos;
3158 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3160 ((*_bfd_error_handler)
3161 (_("%s: warning: allocated section `%s' not in segment"),
3162 bfd_get_filename (abfd),
3163 (hdr->bfd_section == NULL
3164 ? "*unknown*"
3165 : hdr->bfd_section->name)));
3166 if ((abfd->flags & D_PAGED) != 0)
3167 off += (hdr->sh_addr - off) % bed->maxpagesize;
3168 else
3169 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3170 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3171 false);
3173 else if (hdr->sh_type == SHT_REL
3174 || hdr->sh_type == SHT_RELA
3175 || hdr == i_shdrpp[tdata->symtab_section]
3176 || hdr == i_shdrpp[tdata->strtab_section])
3177 hdr->sh_offset = -1;
3178 else
3179 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3183 /* Place the section headers. */
3184 off = align_file_position (off, bed->s->file_align);
3185 i_ehdrp->e_shoff = off;
3186 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3188 elf_tdata (abfd)->next_file_pos = off;
3190 return true;
3193 static boolean
3194 prep_headers (abfd)
3195 bfd *abfd;
3197 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3198 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3199 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3200 int count;
3201 struct bfd_strtab_hash *shstrtab;
3202 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3204 i_ehdrp = elf_elfheader (abfd);
3205 i_shdrp = elf_elfsections (abfd);
3207 shstrtab = _bfd_elf_stringtab_init ();
3208 if (shstrtab == NULL)
3209 return false;
3211 elf_shstrtab (abfd) = shstrtab;
3213 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3214 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3215 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3216 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3218 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3219 i_ehdrp->e_ident[EI_DATA] =
3220 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3221 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3223 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3224 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3226 for (count = EI_PAD; count < EI_NIDENT; count++)
3227 i_ehdrp->e_ident[count] = 0;
3229 if ((abfd->flags & DYNAMIC) != 0)
3230 i_ehdrp->e_type = ET_DYN;
3231 else if ((abfd->flags & EXEC_P) != 0)
3232 i_ehdrp->e_type = ET_EXEC;
3233 else if (bfd_get_format (abfd) == bfd_core)
3234 i_ehdrp->e_type = ET_CORE;
3235 else
3236 i_ehdrp->e_type = ET_REL;
3238 switch (bfd_get_arch (abfd))
3240 case bfd_arch_unknown:
3241 i_ehdrp->e_machine = EM_NONE;
3242 break;
3243 case bfd_arch_sparc:
3244 if (bfd_elf_get_arch_size (abfd) == 64)
3245 i_ehdrp->e_machine = EM_SPARCV9;
3246 else
3247 i_ehdrp->e_machine = EM_SPARC;
3248 break;
3249 case bfd_arch_i370:
3250 i_ehdrp->e_machine = EM_S370;
3251 break;
3252 case bfd_arch_i386:
3253 i_ehdrp->e_machine = EM_386;
3254 break;
3255 case bfd_arch_ia64:
3256 i_ehdrp->e_machine = EM_IA_64;
3257 break;
3258 case bfd_arch_m68k:
3259 i_ehdrp->e_machine = EM_68K;
3260 break;
3261 case bfd_arch_m88k:
3262 i_ehdrp->e_machine = EM_88K;
3263 break;
3264 case bfd_arch_i860:
3265 i_ehdrp->e_machine = EM_860;
3266 break;
3267 case bfd_arch_i960:
3268 i_ehdrp->e_machine = EM_960;
3269 break;
3270 case bfd_arch_mips: /* MIPS Rxxxx */
3271 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3272 break;
3273 case bfd_arch_hppa:
3274 i_ehdrp->e_machine = EM_PARISC;
3275 break;
3276 case bfd_arch_powerpc:
3277 i_ehdrp->e_machine = EM_PPC;
3278 break;
3279 case bfd_arch_alpha:
3280 i_ehdrp->e_machine = EM_ALPHA;
3281 break;
3282 case bfd_arch_sh:
3283 i_ehdrp->e_machine = EM_SH;
3284 break;
3285 case bfd_arch_d10v:
3286 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3287 break;
3288 case bfd_arch_d30v:
3289 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3290 break;
3291 case bfd_arch_fr30:
3292 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3293 break;
3294 case bfd_arch_mcore:
3295 i_ehdrp->e_machine = EM_MCORE;
3296 break;
3297 case bfd_arch_avr:
3298 i_ehdrp->e_machine = EM_AVR;
3299 break;
3300 case bfd_arch_v850:
3301 switch (bfd_get_mach (abfd))
3303 default:
3304 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3306 break;
3307 case bfd_arch_arc:
3308 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3309 break;
3310 case bfd_arch_arm:
3311 i_ehdrp->e_machine = EM_ARM;
3312 break;
3313 case bfd_arch_m32r:
3314 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3315 break;
3316 case bfd_arch_mn10200:
3317 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3318 break;
3319 case bfd_arch_mn10300:
3320 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3321 break;
3322 case bfd_arch_pj:
3323 i_ehdrp->e_machine = EM_PJ;
3324 break;
3325 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3326 default:
3327 i_ehdrp->e_machine = EM_NONE;
3329 i_ehdrp->e_version = bed->s->ev_current;
3330 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3332 /* no program header, for now. */
3333 i_ehdrp->e_phoff = 0;
3334 i_ehdrp->e_phentsize = 0;
3335 i_ehdrp->e_phnum = 0;
3337 /* each bfd section is section header entry */
3338 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3339 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3341 /* if we're building an executable, we'll need a program header table */
3342 if (abfd->flags & EXEC_P)
3344 /* it all happens later */
3345 #if 0
3346 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3348 /* elf_build_phdrs() returns a (NULL-terminated) array of
3349 Elf_Internal_Phdrs */
3350 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3351 i_ehdrp->e_phoff = outbase;
3352 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3353 #endif
3355 else
3357 i_ehdrp->e_phentsize = 0;
3358 i_phdrp = 0;
3359 i_ehdrp->e_phoff = 0;
3362 elf_tdata (abfd)->symtab_hdr.sh_name =
3363 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3364 elf_tdata (abfd)->strtab_hdr.sh_name =
3365 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3366 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3367 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3368 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3369 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3370 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3371 return false;
3373 return true;
3376 /* Assign file positions for all the reloc sections which are not part
3377 of the loadable file image. */
3379 void
3380 _bfd_elf_assign_file_positions_for_relocs (abfd)
3381 bfd *abfd;
3383 file_ptr off;
3384 unsigned int i;
3385 Elf_Internal_Shdr **shdrpp;
3387 off = elf_tdata (abfd)->next_file_pos;
3389 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3390 i < elf_elfheader (abfd)->e_shnum;
3391 i++, shdrpp++)
3393 Elf_Internal_Shdr *shdrp;
3395 shdrp = *shdrpp;
3396 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3397 && shdrp->sh_offset == -1)
3398 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3401 elf_tdata (abfd)->next_file_pos = off;
3404 boolean
3405 _bfd_elf_write_object_contents (abfd)
3406 bfd *abfd;
3408 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3409 Elf_Internal_Ehdr *i_ehdrp;
3410 Elf_Internal_Shdr **i_shdrp;
3411 boolean failed;
3412 unsigned int count;
3414 if (! abfd->output_has_begun
3415 && ! _bfd_elf_compute_section_file_positions
3416 (abfd, (struct bfd_link_info *) NULL))
3417 return false;
3419 i_shdrp = elf_elfsections (abfd);
3420 i_ehdrp = elf_elfheader (abfd);
3422 failed = false;
3423 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3424 if (failed)
3425 return false;
3427 _bfd_elf_assign_file_positions_for_relocs (abfd);
3429 /* After writing the headers, we need to write the sections too... */
3430 for (count = 1; count < i_ehdrp->e_shnum; count++)
3432 if (bed->elf_backend_section_processing)
3433 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3434 if (i_shdrp[count]->contents)
3436 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3437 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3438 1, abfd)
3439 != i_shdrp[count]->sh_size))
3440 return false;
3444 /* Write out the section header names. */
3445 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3446 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3447 return false;
3449 if (bed->elf_backend_final_write_processing)
3450 (*bed->elf_backend_final_write_processing) (abfd,
3451 elf_tdata (abfd)->linker);
3453 return bed->s->write_shdrs_and_ehdr (abfd);
3456 boolean
3457 _bfd_elf_write_corefile_contents (abfd)
3458 bfd *abfd;
3460 /* Hopefully this can be done just like an object file. */
3461 return _bfd_elf_write_object_contents (abfd);
3463 /* given a section, search the header to find them... */
3465 _bfd_elf_section_from_bfd_section (abfd, asect)
3466 bfd *abfd;
3467 struct sec *asect;
3469 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3470 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3471 int index;
3472 Elf_Internal_Shdr *hdr;
3473 int maxindex = elf_elfheader (abfd)->e_shnum;
3475 for (index = 0; index < maxindex; index++)
3477 hdr = i_shdrp[index];
3478 if (hdr->bfd_section == asect)
3479 return index;
3482 if (bed->elf_backend_section_from_bfd_section)
3484 for (index = 0; index < maxindex; index++)
3486 int retval;
3488 hdr = i_shdrp[index];
3489 retval = index;
3490 if ((*bed->elf_backend_section_from_bfd_section)
3491 (abfd, hdr, asect, &retval))
3492 return retval;
3496 if (bfd_is_abs_section (asect))
3497 return SHN_ABS;
3498 if (bfd_is_com_section (asect))
3499 return SHN_COMMON;
3500 if (bfd_is_und_section (asect))
3501 return SHN_UNDEF;
3503 bfd_set_error (bfd_error_nonrepresentable_section);
3505 return -1;
3508 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3509 on error. */
3512 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3513 bfd *abfd;
3514 asymbol **asym_ptr_ptr;
3516 asymbol *asym_ptr = *asym_ptr_ptr;
3517 int idx;
3518 flagword flags = asym_ptr->flags;
3520 /* When gas creates relocations against local labels, it creates its
3521 own symbol for the section, but does put the symbol into the
3522 symbol chain, so udata is 0. When the linker is generating
3523 relocatable output, this section symbol may be for one of the
3524 input sections rather than the output section. */
3525 if (asym_ptr->udata.i == 0
3526 && (flags & BSF_SECTION_SYM)
3527 && asym_ptr->section)
3529 int indx;
3531 if (asym_ptr->section->output_section != NULL)
3532 indx = asym_ptr->section->output_section->index;
3533 else
3534 indx = asym_ptr->section->index;
3535 if (elf_section_syms (abfd)[indx])
3536 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3539 idx = asym_ptr->udata.i;
3541 if (idx == 0)
3543 /* This case can occur when using --strip-symbol on a symbol
3544 which is used in a relocation entry. */
3545 (*_bfd_error_handler)
3546 (_("%s: symbol `%s' required but not present"),
3547 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3548 bfd_set_error (bfd_error_no_symbols);
3549 return -1;
3552 #if DEBUG & 4
3554 fprintf (stderr,
3555 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3556 (long) asym_ptr, asym_ptr->name, idx, flags,
3557 elf_symbol_flags (flags));
3558 fflush (stderr);
3560 #endif
3562 return idx;
3565 /* Copy private BFD data. This copies any program header information. */
3567 static boolean
3568 copy_private_bfd_data (ibfd, obfd)
3569 bfd *ibfd;
3570 bfd *obfd;
3572 Elf_Internal_Ehdr *iehdr;
3573 struct elf_segment_map *mfirst;
3574 struct elf_segment_map **pm;
3575 struct elf_segment_map *m;
3576 Elf_Internal_Phdr *p;
3577 unsigned int i;
3578 unsigned int num_segments;
3579 boolean phdr_included = false;
3581 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3582 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3583 return true;
3585 if (elf_tdata (ibfd)->phdr == NULL)
3586 return true;
3588 iehdr = elf_elfheader (ibfd);
3590 mfirst = NULL;
3591 pm = &mfirst;
3593 num_segments = elf_elfheader (ibfd)->e_phnum;
3595 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3596 ((addr) >= (bottom) \
3597 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3598 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3600 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3602 #define IS_COREFILE_NOTE(p, s) \
3603 (p->p_type == PT_NOTE \
3604 && bfd_get_format (ibfd) == bfd_core \
3605 && s->vma == 0 && s->lma == 0 \
3606 && (bfd_vma) s->filepos >= p->p_offset \
3607 && (bfd_vma) s->filepos + s->_raw_size \
3608 <= p->p_offset + p->p_filesz)
3610 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3611 linker, which generates a PT_INTERP section with p_vaddr and
3612 p_memsz set to 0. */
3614 #define IS_SOLARIS_PT_INTERP(p, s) \
3615 (p->p_vaddr == 0 \
3616 && p->p_filesz > 0 \
3617 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3618 && s->_raw_size > 0 \
3619 && (bfd_vma) s->filepos >= p->p_offset \
3620 && ((bfd_vma) s->filepos + s->_raw_size \
3621 <= p->p_offset + p->p_filesz))
3623 /* Scan through the segments specified in the program header
3624 of the input BFD. */
3625 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3627 unsigned int csecs;
3628 asection *s;
3629 asection **sections;
3630 asection *os;
3631 unsigned int isec;
3632 bfd_vma matching_lma;
3633 bfd_vma suggested_lma;
3634 unsigned int j;
3636 /* For each section in the input BFD, decide if it should be
3637 included in the current segment. A section will be included
3638 if it is within the address space of the segment, and it is
3639 an allocated segment, and there is an output section
3640 associated with it. */
3641 csecs = 0;
3642 for (s = ibfd->sections; s != NULL; s = s->next)
3643 if (s->output_section != NULL)
3645 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3646 || IS_SOLARIS_PT_INTERP (p, s))
3647 && (s->flags & SEC_ALLOC) != 0)
3648 ++csecs;
3649 else if (IS_COREFILE_NOTE (p, s))
3650 ++csecs;
3653 /* Allocate a segment map big enough to contain all of the
3654 sections we have selected. */
3655 m = ((struct elf_segment_map *)
3656 bfd_alloc (obfd,
3657 (sizeof (struct elf_segment_map)
3658 + ((size_t) csecs - 1) * sizeof (asection *))));
3659 if (m == NULL)
3660 return false;
3662 /* Initialise the fields of the segment map. Default to
3663 using the physical address of the segment in the input BFD. */
3664 m->next = NULL;
3665 m->p_type = p->p_type;
3666 m->p_flags = p->p_flags;
3667 m->p_flags_valid = 1;
3668 m->p_paddr = p->p_paddr;
3669 m->p_paddr_valid = 1;
3671 /* Determine if this segment contains the ELF file header
3672 and if it contains the program headers themselves. */
3673 m->includes_filehdr = (p->p_offset == 0
3674 && p->p_filesz >= iehdr->e_ehsize);
3676 m->includes_phdrs = 0;
3678 if (! phdr_included || p->p_type != PT_LOAD)
3680 m->includes_phdrs =
3681 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3682 && (p->p_offset + p->p_filesz
3683 >= ((bfd_vma) iehdr->e_phoff
3684 + iehdr->e_phnum * iehdr->e_phentsize)));
3685 if (p->p_type == PT_LOAD && m->includes_phdrs)
3686 phdr_included = true;
3689 if (csecs == 0)
3691 /* Special segments, such as the PT_PHDR segment, may contain
3692 no sections, but ordinary, loadable segments should contain
3693 something. */
3695 if (p->p_type == PT_LOAD)
3696 _bfd_error_handler
3697 (_("%s: warning: Empty loadable segment detected\n"),
3698 bfd_get_filename (ibfd));
3700 m->count = 0;
3701 *pm = m;
3702 pm = &m->next;
3704 continue;
3707 /* Now scan the sections in the input BFD again and attempt
3708 to add their corresponding output sections to the segment map.
3709 The problem here is how to handle an output section which has
3710 been moved (ie had its LMA changed). There are four possibilities:
3712 1. None of the sections have been moved.
3713 In this case we can continue to use the segment LMA from the
3714 input BFD.
3716 2. All of the sections have been moved by the same amount.
3717 In this case we can change the segment's LMA to match the LMA
3718 of the first section.
3720 3. Some of the sections have been moved, others have not.
3721 In this case those sections which have not been moved can be
3722 placed in the current segment which will have to have its size,
3723 and possibly its LMA changed, and a new segment or segments will
3724 have to be created to contain the other sections.
3726 4. The sections have been moved, but not be the same amount.
3727 In this case we can change the segment's LMA to match the LMA
3728 of the first section and we will have to create a new segment
3729 or segments to contain the other sections.
3731 In order to save time, we allocate an array to hold the section
3732 pointers that we are interested in. As these sections get assigned
3733 to a segment, they are removed from this array. */
3735 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3736 if (sections == NULL)
3737 return false;
3739 /* Step One: Scan for segment vs section LMA conflicts.
3740 Also add the sections to the section array allocated above.
3741 Also add the sections to the current segment. In the common
3742 case, where the sections have not been moved, this means that
3743 we have completely filled the segment, and there is nothing
3744 more to do. */
3746 isec = 0;
3747 matching_lma = false;
3748 suggested_lma = 0;
3750 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3752 os = s->output_section;
3754 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3755 || IS_SOLARIS_PT_INTERP (p, s))
3756 && (s->flags & SEC_ALLOC) != 0)
3757 || IS_COREFILE_NOTE (p, s))
3758 && os != NULL)
3760 sections[j++] = s;
3762 /* The Solaris native linker always sets p_paddr to 0.
3763 We try to catch that case here, and set it to the
3764 correct value. */
3765 if (p->p_paddr == 0
3766 && p->p_vaddr != 0
3767 && isec == 0
3768 && os->lma != 0
3769 && (os->vma == (p->p_vaddr
3770 + (m->includes_filehdr
3771 ? iehdr->e_ehsize
3772 : 0)
3773 + (m->includes_phdrs
3774 ? iehdr->e_phnum * iehdr->e_phentsize
3775 : 0))))
3776 m->p_paddr = p->p_vaddr;
3778 /* Match up the physical address of the segment with the
3779 LMA address of the output section. */
3780 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3781 || IS_COREFILE_NOTE (p, s))
3783 if (matching_lma == 0)
3784 matching_lma = os->lma;
3786 /* We assume that if the section fits within the segment
3787 that it does not overlap any other section within that
3788 segment. */
3789 m->sections[isec++] = os;
3791 else if (suggested_lma == 0)
3792 suggested_lma = os->lma;
3796 BFD_ASSERT (j == csecs);
3798 /* Step Two: Adjust the physical address of the current segment,
3799 if necessary. */
3800 if (isec == csecs)
3802 /* All of the sections fitted within the segment as currently
3803 specified. This is the default case. Add the segment to
3804 the list of built segments and carry on to process the next
3805 program header in the input BFD. */
3806 m->count = csecs;
3807 *pm = m;
3808 pm = &m->next;
3810 free (sections);
3811 continue;
3813 else if (matching_lma != 0)
3815 /* At least one section fits inside the current segment.
3816 Keep it, but modify its physical address to match the
3817 LMA of the first section that fitted. */
3819 m->p_paddr = matching_lma;
3821 else
3823 /* None of the sections fitted inside the current segment.
3824 Change the current segment's physical address to match
3825 the LMA of the first section. */
3827 m->p_paddr = suggested_lma;
3830 /* Step Three: Loop over the sections again, this time assigning
3831 those that fit to the current segment and remvoing them from the
3832 sections array; but making sure not to leave large gaps. Once all
3833 possible sections have been assigned to the current segment it is
3834 added to the list of built segments and if sections still remain
3835 to be assigned, a new segment is constructed before repeating
3836 the loop. */
3837 isec = 0;
3840 m->count = 0;
3841 suggested_lma = 0;
3843 /* Fill the current segment with sections that fit. */
3844 for (j = 0; j < csecs; j++)
3846 s = sections[j];
3848 if (s == NULL)
3849 continue;
3851 os = s->output_section;
3853 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3854 || IS_COREFILE_NOTE (p, s))
3856 if (m->count == 0)
3858 /* If the first section in a segment does not start at
3859 the beginning of the segment, then something is wrong. */
3860 if (os->lma != m->p_paddr)
3861 abort ();
3863 else
3865 asection * prev_sec;
3866 bfd_vma maxpagesize;
3868 prev_sec = m->sections[m->count - 1];
3869 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3871 /* If the gap between the end of the previous section
3872 and the start of this section is more than maxpagesize
3873 then we need to start a new segment. */
3874 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3875 < BFD_ALIGN (os->lma, maxpagesize))
3877 if (suggested_lma == 0)
3878 suggested_lma = os->lma;
3880 continue;
3884 m->sections[m->count++] = os;
3885 ++isec;
3886 sections[j] = NULL;
3888 else if (suggested_lma == 0)
3889 suggested_lma = os->lma;
3892 BFD_ASSERT (m->count > 0);
3894 /* Add the current segment to the list of built segments. */
3895 *pm = m;
3896 pm = &m->next;
3898 if (isec < csecs)
3900 /* We still have not allocated all of the sections to
3901 segments. Create a new segment here, initialise it
3902 and carry on looping. */
3904 m = ((struct elf_segment_map *)
3905 bfd_alloc (obfd,
3906 (sizeof (struct elf_segment_map)
3907 + ((size_t) csecs - 1) * sizeof (asection *))));
3908 if (m == NULL)
3909 return false;
3911 /* Initialise the fields of the segment map. Set the physical
3912 physical address to the LMA of the first section that has
3913 not yet been assigned. */
3915 m->next = NULL;
3916 m->p_type = p->p_type;
3917 m->p_flags = p->p_flags;
3918 m->p_flags_valid = 1;
3919 m->p_paddr = suggested_lma;
3920 m->p_paddr_valid = 1;
3921 m->includes_filehdr = 0;
3922 m->includes_phdrs = 0;
3925 while (isec < csecs);
3927 free (sections);
3930 /* The Solaris linker creates program headers in which all the
3931 p_paddr fields are zero. When we try to objcopy or strip such a
3932 file, we get confused. Check for this case, and if we find it
3933 reset the p_paddr_valid fields. */
3934 for (m = mfirst; m != NULL; m = m->next)
3935 if (m->p_paddr != 0)
3936 break;
3937 if (m == NULL)
3939 for (m = mfirst; m != NULL; m = m->next)
3940 m->p_paddr_valid = 0;
3943 elf_tdata (obfd)->segment_map = mfirst;
3945 #if 0
3946 /* Final Step: Sort the segments into ascending order of physical address. */
3947 if (mfirst != NULL)
3949 struct elf_segment_map* prev;
3951 prev = mfirst;
3952 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3954 /* Yes I know - its a bubble sort....*/
3955 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3957 /* swap m and m->next */
3958 prev->next = m->next;
3959 m->next = m->next->next;
3960 prev->next->next = m;
3962 /* restart loop. */
3963 m = mfirst;
3967 #endif
3969 #undef IS_CONTAINED_BY
3970 #undef IS_SOLARIS_PT_INTERP
3971 #undef IS_COREFILE_NOTE
3972 return true;
3975 /* Copy private section information. This copies over the entsize
3976 field, and sometimes the info field. */
3978 boolean
3979 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3980 bfd *ibfd;
3981 asection *isec;
3982 bfd *obfd;
3983 asection *osec;
3985 Elf_Internal_Shdr *ihdr, *ohdr;
3987 if (ibfd->xvec->flavour != bfd_target_elf_flavour
3988 || obfd->xvec->flavour != bfd_target_elf_flavour)
3989 return true;
3991 /* Copy over private BFD data if it has not already been copied.
3992 This must be done here, rather than in the copy_private_bfd_data
3993 entry point, because the latter is called after the section
3994 contents have been set, which means that the program headers have
3995 already been worked out. */
3996 if (elf_tdata (obfd)->segment_map == NULL
3997 && elf_tdata (ibfd)->phdr != NULL)
3999 asection *s;
4001 /* Only set up the segments if there are no more SEC_ALLOC
4002 sections. FIXME: This won't do the right thing if objcopy is
4003 used to remove the last SEC_ALLOC section, since objcopy
4004 won't call this routine in that case. */
4005 for (s = isec->next; s != NULL; s = s->next)
4006 if ((s->flags & SEC_ALLOC) != 0)
4007 break;
4008 if (s == NULL)
4010 if (! copy_private_bfd_data (ibfd, obfd))
4011 return false;
4015 ihdr = &elf_section_data (isec)->this_hdr;
4016 ohdr = &elf_section_data (osec)->this_hdr;
4018 ohdr->sh_entsize = ihdr->sh_entsize;
4020 if (ihdr->sh_type == SHT_SYMTAB
4021 || ihdr->sh_type == SHT_DYNSYM
4022 || ihdr->sh_type == SHT_GNU_verneed
4023 || ihdr->sh_type == SHT_GNU_verdef)
4024 ohdr->sh_info = ihdr->sh_info;
4026 elf_section_data (osec)->use_rela_p
4027 = elf_section_data (isec)->use_rela_p;
4029 return true;
4032 /* Copy private symbol information. If this symbol is in a section
4033 which we did not map into a BFD section, try to map the section
4034 index correctly. We use special macro definitions for the mapped
4035 section indices; these definitions are interpreted by the
4036 swap_out_syms function. */
4038 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4039 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4040 #define MAP_STRTAB (SHN_LORESERVE - 3)
4041 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4043 boolean
4044 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4045 bfd *ibfd;
4046 asymbol *isymarg;
4047 bfd *obfd;
4048 asymbol *osymarg;
4050 elf_symbol_type *isym, *osym;
4052 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4053 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4054 return true;
4056 isym = elf_symbol_from (ibfd, isymarg);
4057 osym = elf_symbol_from (obfd, osymarg);
4059 if (isym != NULL
4060 && osym != NULL
4061 && bfd_is_abs_section (isym->symbol.section))
4063 unsigned int shndx;
4065 shndx = isym->internal_elf_sym.st_shndx;
4066 if (shndx == elf_onesymtab (ibfd))
4067 shndx = MAP_ONESYMTAB;
4068 else if (shndx == elf_dynsymtab (ibfd))
4069 shndx = MAP_DYNSYMTAB;
4070 else if (shndx == elf_tdata (ibfd)->strtab_section)
4071 shndx = MAP_STRTAB;
4072 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4073 shndx = MAP_SHSTRTAB;
4074 osym->internal_elf_sym.st_shndx = shndx;
4077 return true;
4080 /* Swap out the symbols. */
4082 static boolean
4083 swap_out_syms (abfd, sttp, relocatable_p)
4084 bfd *abfd;
4085 struct bfd_strtab_hash **sttp;
4086 int relocatable_p;
4088 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4090 if (!elf_map_symbols (abfd))
4091 return false;
4093 /* Dump out the symtabs. */
4095 int symcount = bfd_get_symcount (abfd);
4096 asymbol **syms = bfd_get_outsymbols (abfd);
4097 struct bfd_strtab_hash *stt;
4098 Elf_Internal_Shdr *symtab_hdr;
4099 Elf_Internal_Shdr *symstrtab_hdr;
4100 char *outbound_syms;
4101 int idx;
4103 stt = _bfd_elf_stringtab_init ();
4104 if (stt == NULL)
4105 return false;
4107 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4108 symtab_hdr->sh_type = SHT_SYMTAB;
4109 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4110 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4111 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4112 symtab_hdr->sh_addralign = bed->s->file_align;
4114 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4115 symstrtab_hdr->sh_type = SHT_STRTAB;
4117 outbound_syms = bfd_alloc (abfd,
4118 (1 + symcount) * bed->s->sizeof_sym);
4119 if (outbound_syms == NULL)
4120 return false;
4121 symtab_hdr->contents = (PTR) outbound_syms;
4123 /* now generate the data (for "contents") */
4125 /* Fill in zeroth symbol and swap it out. */
4126 Elf_Internal_Sym sym;
4127 sym.st_name = 0;
4128 sym.st_value = 0;
4129 sym.st_size = 0;
4130 sym.st_info = 0;
4131 sym.st_other = 0;
4132 sym.st_shndx = SHN_UNDEF;
4133 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4134 outbound_syms += bed->s->sizeof_sym;
4136 for (idx = 0; idx < symcount; idx++)
4138 Elf_Internal_Sym sym;
4139 bfd_vma value = syms[idx]->value;
4140 elf_symbol_type *type_ptr;
4141 flagword flags = syms[idx]->flags;
4142 int type;
4144 if (flags & BSF_SECTION_SYM)
4145 /* Section symbols have no names. */
4146 sym.st_name = 0;
4147 else
4149 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4150 syms[idx]->name,
4151 true, false);
4152 if (sym.st_name == (unsigned long) -1)
4153 return false;
4156 type_ptr = elf_symbol_from (abfd, syms[idx]);
4158 if ((flags & BSF_SECTION_SYM) == 0
4159 && bfd_is_com_section (syms[idx]->section))
4161 /* ELF common symbols put the alignment into the `value' field,
4162 and the size into the `size' field. This is backwards from
4163 how BFD handles it, so reverse it here. */
4164 sym.st_size = value;
4165 if (type_ptr == NULL
4166 || type_ptr->internal_elf_sym.st_value == 0)
4167 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4168 else
4169 sym.st_value = type_ptr->internal_elf_sym.st_value;
4170 sym.st_shndx = _bfd_elf_section_from_bfd_section
4171 (abfd, syms[idx]->section);
4173 else
4175 asection *sec = syms[idx]->section;
4176 int shndx;
4178 if (sec->output_section)
4180 value += sec->output_offset;
4181 sec = sec->output_section;
4183 /* Don't add in the section vma for relocatable output. */
4184 if (! relocatable_p)
4185 value += sec->vma;
4186 sym.st_value = value;
4187 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4189 if (bfd_is_abs_section (sec)
4190 && type_ptr != NULL
4191 && type_ptr->internal_elf_sym.st_shndx != 0)
4193 /* This symbol is in a real ELF section which we did
4194 not create as a BFD section. Undo the mapping done
4195 by copy_private_symbol_data. */
4196 shndx = type_ptr->internal_elf_sym.st_shndx;
4197 switch (shndx)
4199 case MAP_ONESYMTAB:
4200 shndx = elf_onesymtab (abfd);
4201 break;
4202 case MAP_DYNSYMTAB:
4203 shndx = elf_dynsymtab (abfd);
4204 break;
4205 case MAP_STRTAB:
4206 shndx = elf_tdata (abfd)->strtab_section;
4207 break;
4208 case MAP_SHSTRTAB:
4209 shndx = elf_tdata (abfd)->shstrtab_section;
4210 break;
4211 default:
4212 break;
4215 else
4217 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4219 if (shndx == -1)
4221 asection *sec2;
4223 /* Writing this would be a hell of a lot easier if
4224 we had some decent documentation on bfd, and
4225 knew what to expect of the library, and what to
4226 demand of applications. For example, it
4227 appears that `objcopy' might not set the
4228 section of a symbol to be a section that is
4229 actually in the output file. */
4230 sec2 = bfd_get_section_by_name (abfd, sec->name);
4231 BFD_ASSERT (sec2 != 0);
4232 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4233 BFD_ASSERT (shndx != -1);
4237 sym.st_shndx = shndx;
4240 if ((flags & BSF_FUNCTION) != 0)
4241 type = STT_FUNC;
4242 else if ((flags & BSF_OBJECT) != 0)
4243 type = STT_OBJECT;
4244 else
4245 type = STT_NOTYPE;
4247 /* Processor-specific types */
4248 if (type_ptr != NULL
4249 && bed->elf_backend_get_symbol_type)
4250 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4252 if (flags & BSF_SECTION_SYM)
4253 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4254 else if (bfd_is_com_section (syms[idx]->section))
4255 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4256 else if (bfd_is_und_section (syms[idx]->section))
4257 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4258 ? STB_WEAK
4259 : STB_GLOBAL),
4260 type);
4261 else if (flags & BSF_FILE)
4262 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4263 else
4265 int bind = STB_LOCAL;
4267 if (flags & BSF_LOCAL)
4268 bind = STB_LOCAL;
4269 else if (flags & BSF_WEAK)
4270 bind = STB_WEAK;
4271 else if (flags & BSF_GLOBAL)
4272 bind = STB_GLOBAL;
4274 sym.st_info = ELF_ST_INFO (bind, type);
4277 if (type_ptr != NULL)
4278 sym.st_other = type_ptr->internal_elf_sym.st_other;
4279 else
4280 sym.st_other = 0;
4282 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4283 outbound_syms += bed->s->sizeof_sym;
4286 *sttp = stt;
4287 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4288 symstrtab_hdr->sh_type = SHT_STRTAB;
4290 symstrtab_hdr->sh_flags = 0;
4291 symstrtab_hdr->sh_addr = 0;
4292 symstrtab_hdr->sh_entsize = 0;
4293 symstrtab_hdr->sh_link = 0;
4294 symstrtab_hdr->sh_info = 0;
4295 symstrtab_hdr->sh_addralign = 1;
4298 return true;
4301 /* Return the number of bytes required to hold the symtab vector.
4303 Note that we base it on the count plus 1, since we will null terminate
4304 the vector allocated based on this size. However, the ELF symbol table
4305 always has a dummy entry as symbol #0, so it ends up even. */
4307 long
4308 _bfd_elf_get_symtab_upper_bound (abfd)
4309 bfd *abfd;
4311 long symcount;
4312 long symtab_size;
4313 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4315 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4316 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4318 return symtab_size;
4321 long
4322 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4323 bfd *abfd;
4325 long symcount;
4326 long symtab_size;
4327 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4329 if (elf_dynsymtab (abfd) == 0)
4331 bfd_set_error (bfd_error_invalid_operation);
4332 return -1;
4335 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4336 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4338 return symtab_size;
4341 long
4342 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4343 bfd *abfd ATTRIBUTE_UNUSED;
4344 sec_ptr asect;
4346 return (asect->reloc_count + 1) * sizeof (arelent *);
4349 /* Canonicalize the relocs. */
4351 long
4352 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4353 bfd *abfd;
4354 sec_ptr section;
4355 arelent **relptr;
4356 asymbol **symbols;
4358 arelent *tblptr;
4359 unsigned int i;
4361 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4362 section,
4363 symbols,
4364 false))
4365 return -1;
4367 tblptr = section->relocation;
4368 for (i = 0; i < section->reloc_count; i++)
4369 *relptr++ = tblptr++;
4371 *relptr = NULL;
4373 return section->reloc_count;
4376 long
4377 _bfd_elf_get_symtab (abfd, alocation)
4378 bfd *abfd;
4379 asymbol **alocation;
4381 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4382 (abfd, alocation, false);
4384 if (symcount >= 0)
4385 bfd_get_symcount (abfd) = symcount;
4386 return symcount;
4389 long
4390 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4391 bfd *abfd;
4392 asymbol **alocation;
4394 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4395 (abfd, alocation, true);
4398 /* Return the size required for the dynamic reloc entries. Any
4399 section that was actually installed in the BFD, and has type
4400 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4401 considered to be a dynamic reloc section. */
4403 long
4404 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4405 bfd *abfd;
4407 long ret;
4408 asection *s;
4410 if (elf_dynsymtab (abfd) == 0)
4412 bfd_set_error (bfd_error_invalid_operation);
4413 return -1;
4416 ret = sizeof (arelent *);
4417 for (s = abfd->sections; s != NULL; s = s->next)
4418 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4419 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4420 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4421 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4422 * sizeof (arelent *));
4424 return ret;
4427 /* Canonicalize the dynamic relocation entries. Note that we return
4428 the dynamic relocations as a single block, although they are
4429 actually associated with particular sections; the interface, which
4430 was designed for SunOS style shared libraries, expects that there
4431 is only one set of dynamic relocs. Any section that was actually
4432 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4433 the dynamic symbol table, is considered to be a dynamic reloc
4434 section. */
4436 long
4437 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4438 bfd *abfd;
4439 arelent **storage;
4440 asymbol **syms;
4442 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4443 asection *s;
4444 long ret;
4446 if (elf_dynsymtab (abfd) == 0)
4448 bfd_set_error (bfd_error_invalid_operation);
4449 return -1;
4452 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4453 ret = 0;
4454 for (s = abfd->sections; s != NULL; s = s->next)
4456 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4457 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4458 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4460 arelent *p;
4461 long count, i;
4463 if (! (*slurp_relocs) (abfd, s, syms, true))
4464 return -1;
4465 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4466 p = s->relocation;
4467 for (i = 0; i < count; i++)
4468 *storage++ = p++;
4469 ret += count;
4473 *storage = NULL;
4475 return ret;
4478 /* Read in the version information. */
4480 boolean
4481 _bfd_elf_slurp_version_tables (abfd)
4482 bfd *abfd;
4484 bfd_byte *contents = NULL;
4486 if (elf_dynverdef (abfd) != 0)
4488 Elf_Internal_Shdr *hdr;
4489 Elf_External_Verdef *everdef;
4490 Elf_Internal_Verdef *iverdef;
4491 unsigned int i;
4493 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4495 elf_tdata (abfd)->verdef =
4496 ((Elf_Internal_Verdef *)
4497 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4498 if (elf_tdata (abfd)->verdef == NULL)
4499 goto error_return;
4501 elf_tdata (abfd)->cverdefs = hdr->sh_info;
4503 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4504 if (contents == NULL)
4505 goto error_return;
4506 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4507 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4508 goto error_return;
4510 everdef = (Elf_External_Verdef *) contents;
4511 iverdef = elf_tdata (abfd)->verdef;
4512 for (i = 0; i < hdr->sh_info; i++, iverdef++)
4514 Elf_External_Verdaux *everdaux;
4515 Elf_Internal_Verdaux *iverdaux;
4516 unsigned int j;
4518 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4520 iverdef->vd_bfd = abfd;
4522 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4523 bfd_alloc (abfd,
4524 (iverdef->vd_cnt
4525 * sizeof (Elf_Internal_Verdaux))));
4526 if (iverdef->vd_auxptr == NULL)
4527 goto error_return;
4529 everdaux = ((Elf_External_Verdaux *)
4530 ((bfd_byte *) everdef + iverdef->vd_aux));
4531 iverdaux = iverdef->vd_auxptr;
4532 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4534 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4536 iverdaux->vda_nodename =
4537 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4538 iverdaux->vda_name);
4539 if (iverdaux->vda_nodename == NULL)
4540 goto error_return;
4542 if (j + 1 < iverdef->vd_cnt)
4543 iverdaux->vda_nextptr = iverdaux + 1;
4544 else
4545 iverdaux->vda_nextptr = NULL;
4547 everdaux = ((Elf_External_Verdaux *)
4548 ((bfd_byte *) everdaux + iverdaux->vda_next));
4551 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4553 if (i + 1 < hdr->sh_info)
4554 iverdef->vd_nextdef = iverdef + 1;
4555 else
4556 iverdef->vd_nextdef = NULL;
4558 everdef = ((Elf_External_Verdef *)
4559 ((bfd_byte *) everdef + iverdef->vd_next));
4562 free (contents);
4563 contents = NULL;
4566 if (elf_dynverref (abfd) != 0)
4568 Elf_Internal_Shdr *hdr;
4569 Elf_External_Verneed *everneed;
4570 Elf_Internal_Verneed *iverneed;
4571 unsigned int i;
4573 hdr = &elf_tdata (abfd)->dynverref_hdr;
4575 elf_tdata (abfd)->verref =
4576 ((Elf_Internal_Verneed *)
4577 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4578 if (elf_tdata (abfd)->verref == NULL)
4579 goto error_return;
4581 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4583 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4584 if (contents == NULL)
4585 goto error_return;
4586 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4587 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4588 goto error_return;
4590 everneed = (Elf_External_Verneed *) contents;
4591 iverneed = elf_tdata (abfd)->verref;
4592 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4594 Elf_External_Vernaux *evernaux;
4595 Elf_Internal_Vernaux *ivernaux;
4596 unsigned int j;
4598 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4600 iverneed->vn_bfd = abfd;
4602 iverneed->vn_filename =
4603 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4604 iverneed->vn_file);
4605 if (iverneed->vn_filename == NULL)
4606 goto error_return;
4608 iverneed->vn_auxptr =
4609 ((Elf_Internal_Vernaux *)
4610 bfd_alloc (abfd,
4611 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4613 evernaux = ((Elf_External_Vernaux *)
4614 ((bfd_byte *) everneed + iverneed->vn_aux));
4615 ivernaux = iverneed->vn_auxptr;
4616 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4618 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4620 ivernaux->vna_nodename =
4621 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4622 ivernaux->vna_name);
4623 if (ivernaux->vna_nodename == NULL)
4624 goto error_return;
4626 if (j + 1 < iverneed->vn_cnt)
4627 ivernaux->vna_nextptr = ivernaux + 1;
4628 else
4629 ivernaux->vna_nextptr = NULL;
4631 evernaux = ((Elf_External_Vernaux *)
4632 ((bfd_byte *) evernaux + ivernaux->vna_next));
4635 if (i + 1 < hdr->sh_info)
4636 iverneed->vn_nextref = iverneed + 1;
4637 else
4638 iverneed->vn_nextref = NULL;
4640 everneed = ((Elf_External_Verneed *)
4641 ((bfd_byte *) everneed + iverneed->vn_next));
4644 free (contents);
4645 contents = NULL;
4648 return true;
4650 error_return:
4651 if (contents == NULL)
4652 free (contents);
4653 return false;
4656 asymbol *
4657 _bfd_elf_make_empty_symbol (abfd)
4658 bfd *abfd;
4660 elf_symbol_type *newsym;
4662 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4663 if (!newsym)
4664 return NULL;
4665 else
4667 newsym->symbol.the_bfd = abfd;
4668 return &newsym->symbol;
4672 void
4673 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4674 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4675 asymbol *symbol;
4676 symbol_info *ret;
4678 bfd_symbol_info (symbol, ret);
4681 /* Return whether a symbol name implies a local symbol. Most targets
4682 use this function for the is_local_label_name entry point, but some
4683 override it. */
4685 boolean
4686 _bfd_elf_is_local_label_name (abfd, name)
4687 bfd *abfd ATTRIBUTE_UNUSED;
4688 const char *name;
4690 /* Normal local symbols start with ``.L''. */
4691 if (name[0] == '.' && name[1] == 'L')
4692 return true;
4694 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4695 DWARF debugging symbols starting with ``..''. */
4696 if (name[0] == '.' && name[1] == '.')
4697 return true;
4699 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4700 emitting DWARF debugging output. I suspect this is actually a
4701 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4702 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4703 underscore to be emitted on some ELF targets). For ease of use,
4704 we treat such symbols as local. */
4705 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4706 return true;
4708 return false;
4711 alent *
4712 _bfd_elf_get_lineno (ignore_abfd, symbol)
4713 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4714 asymbol *symbol ATTRIBUTE_UNUSED;
4716 abort ();
4717 return NULL;
4720 boolean
4721 _bfd_elf_set_arch_mach (abfd, arch, machine)
4722 bfd *abfd;
4723 enum bfd_architecture arch;
4724 unsigned long machine;
4726 /* If this isn't the right architecture for this backend, and this
4727 isn't the generic backend, fail. */
4728 if (arch != get_elf_backend_data (abfd)->arch
4729 && arch != bfd_arch_unknown
4730 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4731 return false;
4733 return bfd_default_set_arch_mach (abfd, arch, machine);
4736 /* Find the nearest line to a particular section and offset, for error
4737 reporting. */
4739 boolean
4740 _bfd_elf_find_nearest_line (abfd,
4741 section,
4742 symbols,
4743 offset,
4744 filename_ptr,
4745 functionname_ptr,
4746 line_ptr)
4747 bfd *abfd;
4748 asection *section;
4749 asymbol **symbols;
4750 bfd_vma offset;
4751 CONST char **filename_ptr;
4752 CONST char **functionname_ptr;
4753 unsigned int *line_ptr;
4755 boolean found;
4756 const char *filename;
4757 asymbol *func;
4758 bfd_vma low_func;
4759 asymbol **p;
4761 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4762 filename_ptr, functionname_ptr,
4763 line_ptr))
4764 return true;
4766 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4767 filename_ptr, functionname_ptr,
4768 line_ptr, 0))
4769 return true;
4771 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4772 &found, filename_ptr,
4773 functionname_ptr, line_ptr,
4774 &elf_tdata (abfd)->line_info))
4775 return false;
4776 if (found)
4777 return true;
4779 if (symbols == NULL)
4780 return false;
4782 filename = NULL;
4783 func = NULL;
4784 low_func = 0;
4786 for (p = symbols; *p != NULL; p++)
4788 elf_symbol_type *q;
4790 q = (elf_symbol_type *) *p;
4792 if (bfd_get_section (&q->symbol) != section)
4793 continue;
4795 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4797 default:
4798 break;
4799 case STT_FILE:
4800 filename = bfd_asymbol_name (&q->symbol);
4801 break;
4802 case STT_NOTYPE:
4803 case STT_FUNC:
4804 if (q->symbol.section == section
4805 && q->symbol.value >= low_func
4806 && q->symbol.value <= offset)
4808 func = (asymbol *) q;
4809 low_func = q->symbol.value;
4811 break;
4815 if (func == NULL)
4816 return false;
4818 *filename_ptr = filename;
4819 *functionname_ptr = bfd_asymbol_name (func);
4820 *line_ptr = 0;
4821 return true;
4825 _bfd_elf_sizeof_headers (abfd, reloc)
4826 bfd *abfd;
4827 boolean reloc;
4829 int ret;
4831 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4832 if (! reloc)
4833 ret += get_program_header_size (abfd);
4834 return ret;
4837 boolean
4838 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4839 bfd *abfd;
4840 sec_ptr section;
4841 PTR location;
4842 file_ptr offset;
4843 bfd_size_type count;
4845 Elf_Internal_Shdr *hdr;
4847 if (! abfd->output_has_begun
4848 && ! _bfd_elf_compute_section_file_positions
4849 (abfd, (struct bfd_link_info *) NULL))
4850 return false;
4852 hdr = &elf_section_data (section)->this_hdr;
4854 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4855 return false;
4856 if (bfd_write (location, 1, count, abfd) != count)
4857 return false;
4859 return true;
4862 void
4863 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4864 bfd *abfd ATTRIBUTE_UNUSED;
4865 arelent *cache_ptr ATTRIBUTE_UNUSED;
4866 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
4868 abort ();
4871 #if 0
4872 void
4873 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4874 bfd *abfd;
4875 arelent *cache_ptr;
4876 Elf_Internal_Rel *dst;
4878 abort ();
4880 #endif
4882 /* Try to convert a non-ELF reloc into an ELF one. */
4884 boolean
4885 _bfd_elf_validate_reloc (abfd, areloc)
4886 bfd *abfd;
4887 arelent *areloc;
4889 /* Check whether we really have an ELF howto. */
4891 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4893 bfd_reloc_code_real_type code;
4894 reloc_howto_type *howto;
4896 /* Alien reloc: Try to determine its type to replace it with an
4897 equivalent ELF reloc. */
4899 if (areloc->howto->pc_relative)
4901 switch (areloc->howto->bitsize)
4903 case 8:
4904 code = BFD_RELOC_8_PCREL;
4905 break;
4906 case 12:
4907 code = BFD_RELOC_12_PCREL;
4908 break;
4909 case 16:
4910 code = BFD_RELOC_16_PCREL;
4911 break;
4912 case 24:
4913 code = BFD_RELOC_24_PCREL;
4914 break;
4915 case 32:
4916 code = BFD_RELOC_32_PCREL;
4917 break;
4918 case 64:
4919 code = BFD_RELOC_64_PCREL;
4920 break;
4921 default:
4922 goto fail;
4925 howto = bfd_reloc_type_lookup (abfd, code);
4927 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4929 if (howto->pcrel_offset)
4930 areloc->addend += areloc->address;
4931 else
4932 areloc->addend -= areloc->address; /* addend is unsigned!! */
4935 else
4937 switch (areloc->howto->bitsize)
4939 case 8:
4940 code = BFD_RELOC_8;
4941 break;
4942 case 14:
4943 code = BFD_RELOC_14;
4944 break;
4945 case 16:
4946 code = BFD_RELOC_16;
4947 break;
4948 case 26:
4949 code = BFD_RELOC_26;
4950 break;
4951 case 32:
4952 code = BFD_RELOC_32;
4953 break;
4954 case 64:
4955 code = BFD_RELOC_64;
4956 break;
4957 default:
4958 goto fail;
4961 howto = bfd_reloc_type_lookup (abfd, code);
4964 if (howto)
4965 areloc->howto = howto;
4966 else
4967 goto fail;
4970 return true;
4972 fail:
4973 (*_bfd_error_handler)
4974 (_("%s: unsupported relocation type %s"),
4975 bfd_get_filename (abfd), areloc->howto->name);
4976 bfd_set_error (bfd_error_bad_value);
4977 return false;
4980 boolean
4981 _bfd_elf_close_and_cleanup (abfd)
4982 bfd *abfd;
4984 if (bfd_get_format (abfd) == bfd_object)
4986 if (elf_shstrtab (abfd) != NULL)
4987 _bfd_stringtab_free (elf_shstrtab (abfd));
4990 return _bfd_generic_close_and_cleanup (abfd);
4993 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
4994 in the relocation's offset. Thus we cannot allow any sort of sanity
4995 range-checking to interfere. There is nothing else to do in processing
4996 this reloc. */
4998 bfd_reloc_status_type
4999 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5000 bfd *abfd ATTRIBUTE_UNUSED;
5001 arelent *re ATTRIBUTE_UNUSED;
5002 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5003 PTR data ATTRIBUTE_UNUSED;
5004 asection *is ATTRIBUTE_UNUSED;
5005 bfd *obfd ATTRIBUTE_UNUSED;
5006 char **errmsg ATTRIBUTE_UNUSED;
5008 return bfd_reloc_ok;
5012 /* Elf core file support. Much of this only works on native
5013 toolchains, since we rely on knowing the
5014 machine-dependent procfs structure in order to pick
5015 out details about the corefile. */
5017 #ifdef HAVE_SYS_PROCFS_H
5018 # include <sys/procfs.h>
5019 #endif
5022 /* Define offsetof for those systems which lack it. */
5024 #ifndef offsetof
5025 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5026 #endif
5029 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5031 static int
5032 elfcore_make_pid (abfd)
5033 bfd* abfd;
5035 return ((elf_tdata (abfd)->core_lwpid << 16)
5036 + (elf_tdata (abfd)->core_pid));
5040 /* If there isn't a section called NAME, make one, using
5041 data from SECT. Note, this function will generate a
5042 reference to NAME, so you shouldn't deallocate or
5043 overwrite it. */
5045 static boolean
5046 elfcore_maybe_make_sect (abfd, name, sect)
5047 bfd* abfd;
5048 char* name;
5049 asection* sect;
5051 asection* sect2;
5053 if (bfd_get_section_by_name (abfd, name) != NULL)
5054 return true;
5056 sect2 = bfd_make_section (abfd, name);
5057 if (sect2 == NULL)
5058 return false;
5060 sect2->_raw_size = sect->_raw_size;
5061 sect2->filepos = sect->filepos;
5062 sect2->flags = sect->flags;
5063 sect2->alignment_power = sect->alignment_power;
5064 return true;
5068 /* prstatus_t exists on:
5069 solaris 2.[567]
5070 linux 2.[01] + glibc
5071 unixware 4.2
5074 #if defined (HAVE_PRSTATUS_T)
5075 static boolean
5076 elfcore_grok_prstatus (abfd, note)
5077 bfd* abfd;
5078 Elf_Internal_Note* note;
5080 prstatus_t prstat;
5081 char buf[100];
5082 char* name;
5083 asection* sect;
5085 if (note->descsz != sizeof (prstat))
5086 return true;
5088 memcpy (&prstat, note->descdata, sizeof (prstat));
5090 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5091 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5093 /* pr_who exists on:
5094 solaris 2.[567]
5095 unixware 4.2
5096 pr_who doesn't exist on:
5097 linux 2.[01]
5099 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5100 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5101 #endif
5103 /* Make a ".reg/999" section. */
5105 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5106 name = bfd_alloc (abfd, strlen (buf) + 1);
5107 if (name == NULL)
5108 return false;
5109 strcpy (name, buf);
5111 sect = bfd_make_section (abfd, name);
5112 if (sect == NULL)
5113 return false;
5114 sect->_raw_size = sizeof (prstat.pr_reg);
5115 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
5116 sect->flags = SEC_HAS_CONTENTS;
5117 sect->alignment_power = 2;
5119 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5120 return false;
5122 return true;
5124 #endif /* defined (HAVE_PRSTATUS_T) */
5127 /* Create a pseudosection containing the exact contents of NOTE. This
5128 actually creates up to two pseudosections:
5129 - For the single-threaded case, a section named NAME, unless
5130 such a section already exists.
5131 - For the multi-threaded case, a section named "NAME/PID", where
5132 PID is elfcore_make_pid (abfd).
5133 Both pseudosections have identical contents: the contents of NOTE. */
5135 static boolean
5136 elfcore_make_note_pseudosection (abfd, name, note)
5137 bfd* abfd;
5138 char *name;
5139 Elf_Internal_Note* note;
5141 char buf[100];
5142 char *threaded_name;
5143 asection* sect;
5145 /* Build the section name. */
5147 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5148 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5149 if (threaded_name == NULL)
5150 return false;
5151 strcpy (threaded_name, buf);
5153 sect = bfd_make_section (abfd, threaded_name);
5154 if (sect == NULL)
5155 return false;
5156 sect->_raw_size = note->descsz;
5157 sect->filepos = note->descpos;
5158 sect->flags = SEC_HAS_CONTENTS;
5159 sect->alignment_power = 2;
5161 if (! elfcore_maybe_make_sect (abfd, name, sect))
5162 return false;
5164 return true;
5168 /* There isn't a consistent prfpregset_t across platforms,
5169 but it doesn't matter, because we don't have to pick this
5170 data structure apart. */
5171 static boolean
5172 elfcore_grok_prfpreg (abfd, note)
5173 bfd* abfd;
5174 Elf_Internal_Note* note;
5176 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5180 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5181 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5182 literally. */
5183 static boolean
5184 elfcore_grok_prxfpreg (abfd, note)
5185 bfd* abfd;
5186 Elf_Internal_Note* note;
5188 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5192 #if defined (HAVE_PRPSINFO_T)
5193 # define elfcore_psinfo_t prpsinfo_t
5194 #endif
5196 #if defined (HAVE_PSINFO_T)
5197 # define elfcore_psinfo_t psinfo_t
5198 #endif
5201 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5203 /* return a malloc'ed copy of a string at START which is at
5204 most MAX bytes long, possibly without a terminating '\0'.
5205 the copy will always have a terminating '\0'. */
5207 static char*
5208 elfcore_strndup (abfd, start, max)
5209 bfd* abfd;
5210 char* start;
5211 int max;
5213 char* dup;
5214 char* end = memchr (start, '\0', max);
5215 int len;
5217 if (end == NULL)
5218 len = max;
5219 else
5220 len = end - start;
5222 dup = bfd_alloc (abfd, len + 1);
5223 if (dup == NULL)
5224 return NULL;
5226 memcpy (dup, start, len);
5227 dup[len] = '\0';
5229 return dup;
5232 static boolean
5233 elfcore_grok_psinfo (abfd, note)
5234 bfd* abfd;
5235 Elf_Internal_Note* note;
5237 elfcore_psinfo_t psinfo;
5239 if (note->descsz != sizeof (elfcore_psinfo_t))
5240 return true;
5242 memcpy (&psinfo, note->descdata, note->descsz);
5244 elf_tdata (abfd)->core_program
5245 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5247 elf_tdata (abfd)->core_command
5248 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5250 /* Note that for some reason, a spurious space is tacked
5251 onto the end of the args in some (at least one anyway)
5252 implementations, so strip it off if it exists. */
5255 char* command = elf_tdata (abfd)->core_command;
5256 int n = strlen (command);
5258 if (0 < n && command[n - 1] == ' ')
5259 command[n - 1] = '\0';
5262 return true;
5264 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5267 #if defined (HAVE_PSTATUS_T)
5268 static boolean
5269 elfcore_grok_pstatus (abfd, note)
5270 bfd* abfd;
5271 Elf_Internal_Note* note;
5273 pstatus_t pstat;
5275 if (note->descsz != sizeof (pstat))
5276 return true;
5278 memcpy (&pstat, note->descdata, sizeof (pstat));
5280 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5282 /* Could grab some more details from the "representative"
5283 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5284 NT_LWPSTATUS note, presumably. */
5286 return true;
5288 #endif /* defined (HAVE_PSTATUS_T) */
5291 #if defined (HAVE_LWPSTATUS_T)
5292 static boolean
5293 elfcore_grok_lwpstatus (abfd, note)
5294 bfd* abfd;
5295 Elf_Internal_Note* note;
5297 lwpstatus_t lwpstat;
5298 char buf[100];
5299 char* name;
5300 asection* sect;
5302 if (note->descsz != sizeof (lwpstat))
5303 return true;
5305 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5307 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5308 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5310 /* Make a ".reg/999" section. */
5312 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5313 name = bfd_alloc (abfd, strlen (buf) + 1);
5314 if (name == NULL)
5315 return false;
5316 strcpy (name, buf);
5318 sect = bfd_make_section (abfd, name);
5319 if (sect == NULL)
5320 return false;
5322 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5323 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5324 sect->filepos = note->descpos
5325 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5326 #endif
5328 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5329 sect->_raw_size = sizeof (lwpstat.pr_reg);
5330 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5331 #endif
5333 sect->flags = SEC_HAS_CONTENTS;
5334 sect->alignment_power = 2;
5336 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5337 return false;
5339 /* Make a ".reg2/999" section */
5341 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5342 name = bfd_alloc (abfd, strlen (buf) + 1);
5343 if (name == NULL)
5344 return false;
5345 strcpy (name, buf);
5347 sect = bfd_make_section (abfd, name);
5348 if (sect == NULL)
5349 return false;
5351 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5352 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5353 sect->filepos = note->descpos
5354 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5355 #endif
5357 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5358 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5359 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5360 #endif
5362 sect->flags = SEC_HAS_CONTENTS;
5363 sect->alignment_power = 2;
5365 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5366 return false;
5368 return true;
5370 #endif /* defined (HAVE_LWPSTATUS_T) */
5372 #if defined (HAVE_WIN32_PSTATUS_T)
5373 static boolean
5374 elfcore_grok_win32pstatus (abfd, note)
5375 bfd * abfd;
5376 Elf_Internal_Note * note;
5378 char buf[30];
5379 char * name;
5380 asection * sect;
5381 win32_pstatus_t pstatus;
5383 if (note->descsz < sizeof (pstatus))
5384 return true;
5386 memcpy (& pstatus, note->descdata, note->descsz);
5388 switch (pstatus.data_type)
5390 case NOTE_INFO_PROCESS:
5391 /* FIXME: need to add ->core_command. */
5392 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5393 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5394 break ;
5396 case NOTE_INFO_THREAD:
5397 /* Make a ".reg/999" section. */
5398 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5400 name = bfd_alloc (abfd, strlen (buf) + 1);
5401 if (name == NULL)
5402 return false;
5404 strcpy (name, buf);
5406 sect = bfd_make_section (abfd, name);
5407 if (sect == NULL)
5408 return false;
5410 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5411 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5412 data.thread_info.thread_context);
5413 sect->flags = SEC_HAS_CONTENTS;
5414 sect->alignment_power = 2;
5416 if (pstatus.data.thread_info.is_active_thread)
5417 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5418 return false;
5419 break;
5421 case NOTE_INFO_MODULE:
5422 /* Make a ".module/xxxxxxxx" section. */
5423 sprintf (buf, ".module/%08x" , pstatus.data.module_info.base_address);
5425 name = bfd_alloc (abfd, strlen (buf) + 1);
5426 if (name == NULL)
5427 return false;
5429 strcpy (name, buf);
5431 sect = bfd_make_section (abfd, name);
5433 if (sect == NULL)
5434 return false;
5436 sect->_raw_size = note->descsz;
5437 sect->filepos = note->descpos;
5438 sect->flags = SEC_HAS_CONTENTS;
5439 sect->alignment_power = 2;
5440 break;
5442 default:
5443 return true;
5446 return true;
5448 #endif /* HAVE_WIN32_PSTATUS_T */
5450 static boolean
5451 elfcore_grok_note (abfd, note)
5452 bfd* abfd;
5453 Elf_Internal_Note* note;
5455 switch (note->type)
5457 default:
5458 return true;
5460 #if defined (HAVE_PRSTATUS_T)
5461 case NT_PRSTATUS:
5462 return elfcore_grok_prstatus (abfd, note);
5463 #endif
5465 #if defined (HAVE_PSTATUS_T)
5466 case NT_PSTATUS:
5467 return elfcore_grok_pstatus (abfd, note);
5468 #endif
5470 #if defined (HAVE_LWPSTATUS_T)
5471 case NT_LWPSTATUS:
5472 return elfcore_grok_lwpstatus (abfd, note);
5473 #endif
5475 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5476 return elfcore_grok_prfpreg (abfd, note);
5478 #if defined (HAVE_WIN32_PSTATUS_T)
5479 case NT_WIN32PSTATUS:
5480 return elfcore_grok_win32pstatus (abfd, note);
5481 #endif
5483 case NT_PRXFPREG: /* Linux SSE extension */
5484 if (note->namesz == 5
5485 && ! strcmp (note->namedata, "LINUX"))
5486 return elfcore_grok_prxfpreg (abfd, note);
5487 else
5488 return true;
5490 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5491 case NT_PRPSINFO:
5492 case NT_PSINFO:
5493 return elfcore_grok_psinfo (abfd, note);
5494 #endif
5499 static boolean
5500 elfcore_read_notes (abfd, offset, size)
5501 bfd* abfd;
5502 bfd_vma offset;
5503 bfd_vma size;
5505 char* buf;
5506 char* p;
5508 if (size <= 0)
5509 return true;
5511 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5512 return false;
5514 buf = bfd_malloc ((size_t) size);
5515 if (buf == NULL)
5516 return false;
5518 if (bfd_read (buf, size, 1, abfd) != size)
5520 error:
5521 free (buf);
5522 return false;
5525 p = buf;
5526 while (p < buf + size)
5528 /* FIXME: bad alignment assumption. */
5529 Elf_External_Note* xnp = (Elf_External_Note*) p;
5530 Elf_Internal_Note in;
5532 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5534 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5535 in.namedata = xnp->name;
5537 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5538 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5539 in.descpos = offset + (in.descdata - buf);
5541 if (! elfcore_grok_note (abfd, &in))
5542 goto error;
5544 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5547 free (buf);
5548 return true;
5552 /* FIXME: This function is now unnecessary. Callers can just call
5553 bfd_section_from_phdr directly. */
5555 boolean
5556 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5557 bfd* abfd;
5558 Elf_Internal_Phdr* phdr;
5559 int sec_num;
5561 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5562 return false;
5564 return true;
5569 /* Providing external access to the ELF program header table. */
5571 /* Return an upper bound on the number of bytes required to store a
5572 copy of ABFD's program header table entries. Return -1 if an error
5573 occurs; bfd_get_error will return an appropriate code. */
5574 long
5575 bfd_get_elf_phdr_upper_bound (abfd)
5576 bfd *abfd;
5578 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5580 bfd_set_error (bfd_error_wrong_format);
5581 return -1;
5584 return (elf_elfheader (abfd)->e_phnum
5585 * sizeof (Elf_Internal_Phdr));
5589 /* Copy ABFD's program header table entries to *PHDRS. The entries
5590 will be stored as an array of Elf_Internal_Phdr structures, as
5591 defined in include/elf/internal.h. To find out how large the
5592 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5594 Return the number of program header table entries read, or -1 if an
5595 error occurs; bfd_get_error will return an appropriate code. */
5597 bfd_get_elf_phdrs (abfd, phdrs)
5598 bfd *abfd;
5599 void *phdrs;
5601 int num_phdrs;
5603 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5605 bfd_set_error (bfd_error_wrong_format);
5606 return -1;
5609 num_phdrs = elf_elfheader (abfd)->e_phnum;
5610 memcpy (phdrs, elf_tdata (abfd)->phdr,
5611 num_phdrs * sizeof (Elf_Internal_Phdr));
5613 return num_phdrs;