Add field ``name'' to floatformat.
[binutils.git] / bfd / elf.c
blob76c85e112195a5ad6a043f90aabb2193b1eae478
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 table->dynlocal = NULL;
981 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
984 /* Create an ELF linker hash table. */
986 struct bfd_link_hash_table *
987 _bfd_elf_link_hash_table_create (abfd)
988 bfd *abfd;
990 struct elf_link_hash_table *ret;
992 ret = ((struct elf_link_hash_table *)
993 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
994 if (ret == (struct elf_link_hash_table *) NULL)
995 return NULL;
997 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
999 bfd_release (abfd, ret);
1000 return NULL;
1003 return &ret->root;
1006 /* This is a hook for the ELF emulation code in the generic linker to
1007 tell the backend linker what file name to use for the DT_NEEDED
1008 entry for a dynamic object. The generic linker passes name as an
1009 empty string to indicate that no DT_NEEDED entry should be made. */
1011 void
1012 bfd_elf_set_dt_needed_name (abfd, name)
1013 bfd *abfd;
1014 const char *name;
1016 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1017 && bfd_get_format (abfd) == bfd_object)
1018 elf_dt_name (abfd) = name;
1021 /* Get the list of DT_NEEDED entries for a link. This is a hook for
1022 the linker ELF emulation code. */
1024 struct bfd_link_needed_list *
1025 bfd_elf_get_needed_list (abfd, info)
1026 bfd *abfd ATTRIBUTE_UNUSED;
1027 struct bfd_link_info *info;
1029 if (info->hash->creator->flavour != bfd_target_elf_flavour)
1030 return NULL;
1031 return elf_hash_table (info)->needed;
1034 /* Get the name actually used for a dynamic object for a link. This
1035 is the SONAME entry if there is one. Otherwise, it is the string
1036 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
1038 const char *
1039 bfd_elf_get_dt_soname (abfd)
1040 bfd *abfd;
1042 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
1043 && bfd_get_format (abfd) == bfd_object)
1044 return elf_dt_name (abfd);
1045 return NULL;
1048 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for
1049 the ELF linker emulation code. */
1051 boolean
1052 bfd_elf_get_bfd_needed_list (abfd, pneeded)
1053 bfd *abfd;
1054 struct bfd_link_needed_list **pneeded;
1056 asection *s;
1057 bfd_byte *dynbuf = NULL;
1058 int elfsec;
1059 unsigned long link;
1060 bfd_byte *extdyn, *extdynend;
1061 size_t extdynsize;
1062 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
1064 *pneeded = NULL;
1066 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour
1067 || bfd_get_format (abfd) != bfd_object)
1068 return true;
1070 s = bfd_get_section_by_name (abfd, ".dynamic");
1071 if (s == NULL || s->_raw_size == 0)
1072 return true;
1074 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
1075 if (dynbuf == NULL)
1076 goto error_return;
1078 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
1079 s->_raw_size))
1080 goto error_return;
1082 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
1083 if (elfsec == -1)
1084 goto error_return;
1086 link = elf_elfsections (abfd)[elfsec]->sh_link;
1088 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
1089 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
1091 extdyn = dynbuf;
1092 extdynend = extdyn + s->_raw_size;
1093 for (; extdyn < extdynend; extdyn += extdynsize)
1095 Elf_Internal_Dyn dyn;
1097 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
1099 if (dyn.d_tag == DT_NULL)
1100 break;
1102 if (dyn.d_tag == DT_NEEDED)
1104 const char *string;
1105 struct bfd_link_needed_list *l;
1107 string = bfd_elf_string_from_elf_section (abfd, link,
1108 dyn.d_un.d_val);
1109 if (string == NULL)
1110 goto error_return;
1112 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l);
1113 if (l == NULL)
1114 goto error_return;
1116 l->by = abfd;
1117 l->name = string;
1118 l->next = *pneeded;
1119 *pneeded = l;
1123 free (dynbuf);
1125 return true;
1127 error_return:
1128 if (dynbuf != NULL)
1129 free (dynbuf);
1130 return false;
1133 /* Allocate an ELF string table--force the first byte to be zero. */
1135 struct bfd_strtab_hash *
1136 _bfd_elf_stringtab_init ()
1138 struct bfd_strtab_hash *ret;
1140 ret = _bfd_stringtab_init ();
1141 if (ret != NULL)
1143 bfd_size_type loc;
1145 loc = _bfd_stringtab_add (ret, "", true, false);
1146 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
1147 if (loc == (bfd_size_type) -1)
1149 _bfd_stringtab_free (ret);
1150 ret = NULL;
1153 return ret;
1156 /* ELF .o/exec file reading */
1158 /* Create a new bfd section from an ELF section header. */
1160 boolean
1161 bfd_section_from_shdr (abfd, shindex)
1162 bfd *abfd;
1163 unsigned int shindex;
1165 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
1166 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
1167 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1168 char *name;
1170 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
1172 switch (hdr->sh_type)
1174 case SHT_NULL:
1175 /* Inactive section. Throw it away. */
1176 return true;
1178 case SHT_PROGBITS: /* Normal section with contents. */
1179 case SHT_DYNAMIC: /* Dynamic linking information. */
1180 case SHT_NOBITS: /* .bss section. */
1181 case SHT_HASH: /* .hash section. */
1182 case SHT_NOTE: /* .note section. */
1183 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1185 case SHT_SYMTAB: /* A symbol table */
1186 if (elf_onesymtab (abfd) == shindex)
1187 return true;
1189 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1190 BFD_ASSERT (elf_onesymtab (abfd) == 0);
1191 elf_onesymtab (abfd) = shindex;
1192 elf_tdata (abfd)->symtab_hdr = *hdr;
1193 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
1194 abfd->flags |= HAS_SYMS;
1196 /* Sometimes a shared object will map in the symbol table. If
1197 SHF_ALLOC is set, and this is a shared object, then we also
1198 treat this section as a BFD section. We can not base the
1199 decision purely on SHF_ALLOC, because that flag is sometimes
1200 set in a relocateable object file, which would confuse the
1201 linker. */
1202 if ((hdr->sh_flags & SHF_ALLOC) != 0
1203 && (abfd->flags & DYNAMIC) != 0
1204 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
1205 return false;
1207 return true;
1209 case SHT_DYNSYM: /* A dynamic symbol table */
1210 if (elf_dynsymtab (abfd) == shindex)
1211 return true;
1213 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
1214 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
1215 elf_dynsymtab (abfd) = shindex;
1216 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
1217 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1218 abfd->flags |= HAS_SYMS;
1220 /* Besides being a symbol table, we also treat this as a regular
1221 section, so that objcopy can handle it. */
1222 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1224 case SHT_STRTAB: /* A string table */
1225 if (hdr->bfd_section != NULL)
1226 return true;
1227 if (ehdr->e_shstrndx == shindex)
1229 elf_tdata (abfd)->shstrtab_hdr = *hdr;
1230 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
1231 return true;
1234 unsigned int i;
1236 for (i = 1; i < ehdr->e_shnum; i++)
1238 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
1239 if (hdr2->sh_link == shindex)
1241 if (! bfd_section_from_shdr (abfd, i))
1242 return false;
1243 if (elf_onesymtab (abfd) == i)
1245 elf_tdata (abfd)->strtab_hdr = *hdr;
1246 elf_elfsections (abfd)[shindex] =
1247 &elf_tdata (abfd)->strtab_hdr;
1248 return true;
1250 if (elf_dynsymtab (abfd) == i)
1252 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
1253 elf_elfsections (abfd)[shindex] = hdr =
1254 &elf_tdata (abfd)->dynstrtab_hdr;
1255 /* We also treat this as a regular section, so
1256 that objcopy can handle it. */
1257 break;
1259 #if 0 /* Not handling other string tables specially right now. */
1260 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
1261 /* We have a strtab for some random other section. */
1262 newsect = (asection *) hdr2->bfd_section;
1263 if (!newsect)
1264 break;
1265 hdr->bfd_section = newsect;
1266 hdr2 = &elf_section_data (newsect)->str_hdr;
1267 *hdr2 = *hdr;
1268 elf_elfsections (abfd)[shindex] = hdr2;
1269 #endif
1274 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1276 case SHT_REL:
1277 case SHT_RELA:
1278 /* *These* do a lot of work -- but build no sections! */
1280 asection *target_sect;
1281 Elf_Internal_Shdr *hdr2;
1283 /* Check for a bogus link to avoid crashing. */
1284 if (hdr->sh_link >= ehdr->e_shnum)
1286 ((*_bfd_error_handler)
1287 (_("%s: invalid link %lu for reloc section %s (index %u)"),
1288 bfd_get_filename (abfd), hdr->sh_link, name, shindex));
1289 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1292 /* For some incomprehensible reason Oracle distributes
1293 libraries for Solaris in which some of the objects have
1294 bogus sh_link fields. It would be nice if we could just
1295 reject them, but, unfortunately, some people need to use
1296 them. We scan through the section headers; if we find only
1297 one suitable symbol table, we clobber the sh_link to point
1298 to it. I hope this doesn't break anything. */
1299 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
1300 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
1302 int scan;
1303 int found;
1305 found = 0;
1306 for (scan = 1; scan < ehdr->e_shnum; scan++)
1308 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
1309 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
1311 if (found != 0)
1313 found = 0;
1314 break;
1316 found = scan;
1319 if (found != 0)
1320 hdr->sh_link = found;
1323 /* Get the symbol table. */
1324 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
1325 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
1326 return false;
1328 /* If this reloc section does not use the main symbol table we
1329 don't treat it as a reloc section. BFD can't adequately
1330 represent such a section, so at least for now, we don't
1331 try. We just present it as a normal section. */
1332 if (hdr->sh_link != elf_onesymtab (abfd))
1333 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1335 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
1336 return false;
1337 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
1338 if (target_sect == NULL)
1339 return false;
1341 if ((target_sect->flags & SEC_RELOC) == 0
1342 || target_sect->reloc_count == 0)
1343 hdr2 = &elf_section_data (target_sect)->rel_hdr;
1344 else
1346 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
1347 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
1348 elf_section_data (target_sect)->rel_hdr2 = hdr2;
1350 *hdr2 = *hdr;
1351 elf_elfsections (abfd)[shindex] = hdr2;
1352 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
1353 target_sect->flags |= SEC_RELOC;
1354 target_sect->relocation = NULL;
1355 target_sect->rel_filepos = hdr->sh_offset;
1356 /* In the section to which the relocations apply, mark whether
1357 its relocations are of the REL or RELA variety. */
1358 if (hdr->sh_size != 0)
1359 elf_section_data (target_sect)->use_rela_p
1360 = (hdr->sh_type == SHT_RELA);
1361 abfd->flags |= HAS_RELOC;
1362 return true;
1364 break;
1366 case SHT_GNU_verdef:
1367 elf_dynverdef (abfd) = shindex;
1368 elf_tdata (abfd)->dynverdef_hdr = *hdr;
1369 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1370 break;
1372 case SHT_GNU_versym:
1373 elf_dynversym (abfd) = shindex;
1374 elf_tdata (abfd)->dynversym_hdr = *hdr;
1375 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1376 break;
1378 case SHT_GNU_verneed:
1379 elf_dynverref (abfd) = shindex;
1380 elf_tdata (abfd)->dynverref_hdr = *hdr;
1381 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
1382 break;
1384 case SHT_SHLIB:
1385 return true;
1387 default:
1388 /* Check for any processor-specific section types. */
1390 if (bed->elf_backend_section_from_shdr)
1391 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
1393 break;
1396 return true;
1399 /* Given an ELF section number, retrieve the corresponding BFD
1400 section. */
1402 asection *
1403 bfd_section_from_elf_index (abfd, index)
1404 bfd *abfd;
1405 unsigned int index;
1407 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
1408 if (index >= elf_elfheader (abfd)->e_shnum)
1409 return NULL;
1410 return elf_elfsections (abfd)[index]->bfd_section;
1413 boolean
1414 _bfd_elf_new_section_hook (abfd, sec)
1415 bfd *abfd;
1416 asection *sec;
1418 struct bfd_elf_section_data *sdata;
1420 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, sizeof (*sdata));
1421 if (!sdata)
1422 return false;
1423 sec->used_by_bfd = (PTR) sdata;
1425 /* Indicate whether or not this section should use RELA relocations. */
1426 sdata->use_rela_p
1427 = get_elf_backend_data (abfd)->default_use_rela_p;
1429 return true;
1432 /* Create a new bfd section from an ELF program header.
1434 Since program segments have no names, we generate a synthetic name
1435 of the form segment<NUM>, where NUM is generally the index in the
1436 program header table. For segments that are split (see below) we
1437 generate the names segment<NUM>a and segment<NUM>b.
1439 Note that some program segments may have a file size that is different than
1440 (less than) the memory size. All this means is that at execution the
1441 system must allocate the amount of memory specified by the memory size,
1442 but only initialize it with the first "file size" bytes read from the
1443 file. This would occur for example, with program segments consisting
1444 of combined data+bss.
1446 To handle the above situation, this routine generates TWO bfd sections
1447 for the single program segment. The first has the length specified by
1448 the file size of the segment, and the second has the length specified
1449 by the difference between the two sizes. In effect, the segment is split
1450 into it's initialized and uninitialized parts.
1454 boolean
1455 _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename)
1456 bfd *abfd;
1457 Elf_Internal_Phdr *hdr;
1458 int index;
1459 const char *typename;
1461 asection *newsect;
1462 char *name;
1463 char namebuf[64];
1464 int split;
1466 split = ((hdr->p_memsz > 0)
1467 && (hdr->p_filesz > 0)
1468 && (hdr->p_memsz > hdr->p_filesz));
1469 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : "");
1470 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1471 if (!name)
1472 return false;
1473 strcpy (name, namebuf);
1474 newsect = bfd_make_section (abfd, name);
1475 if (newsect == NULL)
1476 return false;
1477 newsect->vma = hdr->p_vaddr;
1478 newsect->lma = hdr->p_paddr;
1479 newsect->_raw_size = hdr->p_filesz;
1480 newsect->filepos = hdr->p_offset;
1481 newsect->flags |= SEC_HAS_CONTENTS;
1482 if (hdr->p_type == PT_LOAD)
1484 newsect->flags |= SEC_ALLOC;
1485 newsect->flags |= SEC_LOAD;
1486 if (hdr->p_flags & PF_X)
1488 /* FIXME: all we known is that it has execute PERMISSION,
1489 may be data. */
1490 newsect->flags |= SEC_CODE;
1493 if (!(hdr->p_flags & PF_W))
1495 newsect->flags |= SEC_READONLY;
1498 if (split)
1500 sprintf (namebuf, "%s%db", typename, index);
1501 name = bfd_alloc (abfd, strlen (namebuf) + 1);
1502 if (!name)
1503 return false;
1504 strcpy (name, namebuf);
1505 newsect = bfd_make_section (abfd, name);
1506 if (newsect == NULL)
1507 return false;
1508 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
1509 newsect->lma = hdr->p_paddr + hdr->p_filesz;
1510 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
1511 if (hdr->p_type == PT_LOAD)
1513 newsect->flags |= SEC_ALLOC;
1514 if (hdr->p_flags & PF_X)
1515 newsect->flags |= SEC_CODE;
1517 if (!(hdr->p_flags & PF_W))
1518 newsect->flags |= SEC_READONLY;
1521 return true;
1524 boolean
1525 bfd_section_from_phdr (abfd, hdr, index)
1526 bfd *abfd;
1527 Elf_Internal_Phdr *hdr;
1528 int index;
1530 struct elf_backend_data *bed;
1532 switch (hdr->p_type)
1534 case PT_NULL:
1535 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null");
1537 case PT_LOAD:
1538 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load");
1540 case PT_DYNAMIC:
1541 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic");
1543 case PT_INTERP:
1544 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp");
1546 case PT_NOTE:
1547 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note"))
1548 return false;
1549 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz))
1550 return false;
1551 return true;
1553 case PT_SHLIB:
1554 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib");
1556 case PT_PHDR:
1557 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr");
1559 default:
1560 /* Check for any processor-specific program segment types.
1561 If no handler for them, default to making "segment" sections. */
1562 bed = get_elf_backend_data (abfd);
1563 if (bed->elf_backend_section_from_phdr)
1564 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index);
1565 else
1566 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment");
1570 /* Initialize REL_HDR, the section-header for new section, containing
1571 relocations against ASECT. If USE_RELA_P is true, we use RELA
1572 relocations; otherwise, we use REL relocations. */
1574 boolean
1575 _bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p)
1576 bfd *abfd;
1577 Elf_Internal_Shdr *rel_hdr;
1578 asection *asect;
1579 boolean use_rela_p;
1581 char *name;
1582 struct elf_backend_data *bed;
1584 bed = get_elf_backend_data (abfd);
1585 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1586 if (name == NULL)
1587 return false;
1588 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1589 rel_hdr->sh_name =
1590 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1591 true, false);
1592 if (rel_hdr->sh_name == (unsigned int) -1)
1593 return false;
1594 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1595 rel_hdr->sh_entsize = (use_rela_p
1596 ? bed->s->sizeof_rela
1597 : bed->s->sizeof_rel);
1598 rel_hdr->sh_addralign = bed->s->file_align;
1599 rel_hdr->sh_flags = 0;
1600 rel_hdr->sh_addr = 0;
1601 rel_hdr->sh_size = 0;
1602 rel_hdr->sh_offset = 0;
1604 return true;
1607 /* Set up an ELF internal section header for a section. */
1609 /*ARGSUSED*/
1610 static void
1611 elf_fake_sections (abfd, asect, failedptrarg)
1612 bfd *abfd;
1613 asection *asect;
1614 PTR failedptrarg;
1616 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1617 boolean *failedptr = (boolean *) failedptrarg;
1618 Elf_Internal_Shdr *this_hdr;
1620 if (*failedptr)
1622 /* We already failed; just get out of the bfd_map_over_sections
1623 loop. */
1624 return;
1627 this_hdr = &elf_section_data (asect)->this_hdr;
1629 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1630 asect->name,
1631 true, false);
1632 if (this_hdr->sh_name == (unsigned long) -1)
1634 *failedptr = true;
1635 return;
1638 this_hdr->sh_flags = 0;
1640 if ((asect->flags & SEC_ALLOC) != 0
1641 || asect->user_set_vma)
1642 this_hdr->sh_addr = asect->vma;
1643 else
1644 this_hdr->sh_addr = 0;
1646 this_hdr->sh_offset = 0;
1647 this_hdr->sh_size = asect->_raw_size;
1648 this_hdr->sh_link = 0;
1649 this_hdr->sh_addralign = 1 << asect->alignment_power;
1650 /* The sh_entsize and sh_info fields may have been set already by
1651 copy_private_section_data. */
1653 this_hdr->bfd_section = asect;
1654 this_hdr->contents = NULL;
1656 /* FIXME: This should not be based on section names. */
1657 if (strcmp (asect->name, ".dynstr") == 0)
1658 this_hdr->sh_type = SHT_STRTAB;
1659 else if (strcmp (asect->name, ".hash") == 0)
1661 this_hdr->sh_type = SHT_HASH;
1662 this_hdr->sh_entsize = bed->s->sizeof_hash_entry;
1664 else if (strcmp (asect->name, ".dynsym") == 0)
1666 this_hdr->sh_type = SHT_DYNSYM;
1667 this_hdr->sh_entsize = bed->s->sizeof_sym;
1669 else if (strcmp (asect->name, ".dynamic") == 0)
1671 this_hdr->sh_type = SHT_DYNAMIC;
1672 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1674 else if (strncmp (asect->name, ".rela", 5) == 0
1675 && get_elf_backend_data (abfd)->may_use_rela_p)
1677 this_hdr->sh_type = SHT_RELA;
1678 this_hdr->sh_entsize = bed->s->sizeof_rela;
1680 else if (strncmp (asect->name, ".rel", 4) == 0
1681 && get_elf_backend_data (abfd)->may_use_rel_p)
1683 this_hdr->sh_type = SHT_REL;
1684 this_hdr->sh_entsize = bed->s->sizeof_rel;
1686 else if (strncmp (asect->name, ".note", 5) == 0)
1687 this_hdr->sh_type = SHT_NOTE;
1688 else if (strncmp (asect->name, ".stab", 5) == 0
1689 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1690 this_hdr->sh_type = SHT_STRTAB;
1691 else if (strcmp (asect->name, ".gnu.version") == 0)
1693 this_hdr->sh_type = SHT_GNU_versym;
1694 this_hdr->sh_entsize = sizeof (Elf_External_Versym);
1696 else if (strcmp (asect->name, ".gnu.version_d") == 0)
1698 this_hdr->sh_type = SHT_GNU_verdef;
1699 this_hdr->sh_entsize = 0;
1700 /* objcopy or strip will copy over sh_info, but may not set
1701 cverdefs. The linker will set cverdefs, but sh_info will be
1702 zero. */
1703 if (this_hdr->sh_info == 0)
1704 this_hdr->sh_info = elf_tdata (abfd)->cverdefs;
1705 else
1706 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0
1707 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs);
1709 else if (strcmp (asect->name, ".gnu.version_r") == 0)
1711 this_hdr->sh_type = SHT_GNU_verneed;
1712 this_hdr->sh_entsize = 0;
1713 /* objcopy or strip will copy over sh_info, but may not set
1714 cverrefs. The linker will set cverrefs, but sh_info will be
1715 zero. */
1716 if (this_hdr->sh_info == 0)
1717 this_hdr->sh_info = elf_tdata (abfd)->cverrefs;
1718 else
1719 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0
1720 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs);
1722 else if ((asect->flags & SEC_ALLOC) != 0
1723 && (asect->flags & SEC_LOAD) != 0)
1724 this_hdr->sh_type = SHT_PROGBITS;
1725 else if ((asect->flags & SEC_ALLOC) != 0
1726 && ((asect->flags & SEC_LOAD) == 0))
1727 this_hdr->sh_type = SHT_NOBITS;
1728 else
1730 /* Who knows? */
1731 this_hdr->sh_type = SHT_PROGBITS;
1734 if ((asect->flags & SEC_ALLOC) != 0)
1735 this_hdr->sh_flags |= SHF_ALLOC;
1736 if ((asect->flags & SEC_READONLY) == 0)
1737 this_hdr->sh_flags |= SHF_WRITE;
1738 if ((asect->flags & SEC_CODE) != 0)
1739 this_hdr->sh_flags |= SHF_EXECINSTR;
1741 /* Check for processor-specific section types. */
1742 if (bed->elf_backend_fake_sections)
1743 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1745 /* If the section has relocs, set up a section header for the
1746 SHT_REL[A] section. If two relocation sections are required for
1747 this section, it is up to the processor-specific back-end to
1748 create the other. */
1749 if ((asect->flags & SEC_RELOC) != 0
1750 && !_bfd_elf_init_reloc_shdr (abfd,
1751 &elf_section_data (asect)->rel_hdr,
1752 asect,
1753 elf_section_data (asect)->use_rela_p))
1754 *failedptr = true;
1757 /* Get elf arch size (32 / 64).
1758 Returns -1 if not elf. */
1761 bfd_elf_get_arch_size (abfd)
1762 bfd *abfd;
1764 if (abfd->xvec->flavour != bfd_target_elf_flavour)
1766 bfd_set_error (bfd_error_wrong_format);
1767 return -1;
1770 return (get_elf_backend_data (abfd))->s->arch_size;
1773 /* Assign all ELF section numbers. The dummy first section is handled here
1774 too. The link/info pointers for the standard section types are filled
1775 in here too, while we're at it. */
1777 static boolean
1778 assign_section_numbers (abfd)
1779 bfd *abfd;
1781 struct elf_obj_tdata *t = elf_tdata (abfd);
1782 asection *sec;
1783 unsigned int section_number;
1784 Elf_Internal_Shdr **i_shdrp;
1786 section_number = 1;
1788 for (sec = abfd->sections; sec; sec = sec->next)
1790 struct bfd_elf_section_data *d = elf_section_data (sec);
1792 d->this_idx = section_number++;
1793 if ((sec->flags & SEC_RELOC) == 0)
1794 d->rel_idx = 0;
1795 else
1796 d->rel_idx = section_number++;
1798 if (d->rel_hdr2)
1799 d->rel_idx2 = section_number++;
1800 else
1801 d->rel_idx2 = 0;
1804 t->shstrtab_section = section_number++;
1805 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1806 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1808 if (bfd_get_symcount (abfd) > 0)
1810 t->symtab_section = section_number++;
1811 t->strtab_section = section_number++;
1814 elf_elfheader (abfd)->e_shnum = section_number;
1816 /* Set up the list of section header pointers, in agreement with the
1817 indices. */
1818 i_shdrp = ((Elf_Internal_Shdr **)
1819 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1820 if (i_shdrp == NULL)
1821 return false;
1823 i_shdrp[0] = ((Elf_Internal_Shdr *)
1824 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1825 if (i_shdrp[0] == NULL)
1827 bfd_release (abfd, i_shdrp);
1828 return false;
1830 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1832 elf_elfsections (abfd) = i_shdrp;
1834 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1835 if (bfd_get_symcount (abfd) > 0)
1837 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1838 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1839 t->symtab_hdr.sh_link = t->strtab_section;
1841 for (sec = abfd->sections; sec; sec = sec->next)
1843 struct bfd_elf_section_data *d = elf_section_data (sec);
1844 asection *s;
1845 const char *name;
1847 i_shdrp[d->this_idx] = &d->this_hdr;
1848 if (d->rel_idx != 0)
1849 i_shdrp[d->rel_idx] = &d->rel_hdr;
1850 if (d->rel_idx2 != 0)
1851 i_shdrp[d->rel_idx2] = d->rel_hdr2;
1853 /* Fill in the sh_link and sh_info fields while we're at it. */
1855 /* sh_link of a reloc section is the section index of the symbol
1856 table. sh_info is the section index of the section to which
1857 the relocation entries apply. */
1858 if (d->rel_idx != 0)
1860 d->rel_hdr.sh_link = t->symtab_section;
1861 d->rel_hdr.sh_info = d->this_idx;
1863 if (d->rel_idx2 != 0)
1865 d->rel_hdr2->sh_link = t->symtab_section;
1866 d->rel_hdr2->sh_info = d->this_idx;
1869 switch (d->this_hdr.sh_type)
1871 case SHT_REL:
1872 case SHT_RELA:
1873 /* A reloc section which we are treating as a normal BFD
1874 section. sh_link is the section index of the symbol
1875 table. sh_info is the section index of the section to
1876 which the relocation entries apply. We assume that an
1877 allocated reloc section uses the dynamic symbol table.
1878 FIXME: How can we be sure? */
1879 s = bfd_get_section_by_name (abfd, ".dynsym");
1880 if (s != NULL)
1881 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1883 /* We look up the section the relocs apply to by name. */
1884 name = sec->name;
1885 if (d->this_hdr.sh_type == SHT_REL)
1886 name += 4;
1887 else
1888 name += 5;
1889 s = bfd_get_section_by_name (abfd, name);
1890 if (s != NULL)
1891 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1892 break;
1894 case SHT_STRTAB:
1895 /* We assume that a section named .stab*str is a stabs
1896 string section. We look for a section with the same name
1897 but without the trailing ``str'', and set its sh_link
1898 field to point to this section. */
1899 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1900 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1902 size_t len;
1903 char *alc;
1905 len = strlen (sec->name);
1906 alc = (char *) bfd_malloc (len - 2);
1907 if (alc == NULL)
1908 return false;
1909 strncpy (alc, sec->name, len - 3);
1910 alc[len - 3] = '\0';
1911 s = bfd_get_section_by_name (abfd, alc);
1912 free (alc);
1913 if (s != NULL)
1915 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1917 /* This is a .stab section. */
1918 elf_section_data (s)->this_hdr.sh_entsize =
1919 4 + 2 * bfd_elf_get_arch_size (abfd) / 8;
1922 break;
1924 case SHT_DYNAMIC:
1925 case SHT_DYNSYM:
1926 case SHT_GNU_verneed:
1927 case SHT_GNU_verdef:
1928 /* sh_link is the section header index of the string table
1929 used for the dynamic entries, or the symbol table, or the
1930 version strings. */
1931 s = bfd_get_section_by_name (abfd, ".dynstr");
1932 if (s != NULL)
1933 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1934 break;
1936 case SHT_HASH:
1937 case SHT_GNU_versym:
1938 /* sh_link is the section header index of the symbol table
1939 this hash table or version table is for. */
1940 s = bfd_get_section_by_name (abfd, ".dynsym");
1941 if (s != NULL)
1942 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1943 break;
1947 return true;
1950 /* Map symbol from it's internal number to the external number, moving
1951 all local symbols to be at the head of the list. */
1953 static INLINE int
1954 sym_is_global (abfd, sym)
1955 bfd *abfd;
1956 asymbol *sym;
1958 /* If the backend has a special mapping, use it. */
1959 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1960 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1961 (abfd, sym));
1963 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1964 || bfd_is_und_section (bfd_get_section (sym))
1965 || bfd_is_com_section (bfd_get_section (sym)));
1968 static boolean
1969 elf_map_symbols (abfd)
1970 bfd *abfd;
1972 int symcount = bfd_get_symcount (abfd);
1973 asymbol **syms = bfd_get_outsymbols (abfd);
1974 asymbol **sect_syms;
1975 int num_locals = 0;
1976 int num_globals = 0;
1977 int num_locals2 = 0;
1978 int num_globals2 = 0;
1979 int max_index = 0;
1980 int num_sections = 0;
1981 int idx;
1982 asection *asect;
1983 asymbol **new_syms;
1984 asymbol *sym;
1986 #ifdef DEBUG
1987 fprintf (stderr, "elf_map_symbols\n");
1988 fflush (stderr);
1989 #endif
1991 /* Add a section symbol for each BFD section. FIXME: Is this really
1992 necessary? */
1993 for (asect = abfd->sections; asect; asect = asect->next)
1995 if (max_index < asect->index)
1996 max_index = asect->index;
1999 max_index++;
2000 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
2001 if (sect_syms == NULL)
2002 return false;
2003 elf_section_syms (abfd) = sect_syms;
2005 for (idx = 0; idx < symcount; idx++)
2007 sym = syms[idx];
2009 if ((sym->flags & BSF_SECTION_SYM) != 0
2010 && sym->value == 0)
2012 asection *sec;
2014 sec = sym->section;
2016 if (sec->owner != NULL)
2018 if (sec->owner != abfd)
2020 if (sec->output_offset != 0)
2021 continue;
2023 sec = sec->output_section;
2025 /* Empty sections in the input files may have had a section
2026 symbol created for them. (See the comment near the end of
2027 _bfd_generic_link_output_symbols in linker.c). If the linker
2028 script discards such sections then we will reach this point.
2029 Since we know that we cannot avoid this case, we detect it
2030 and skip the abort and the assignment to the sect_syms array.
2031 To reproduce this particular case try running the linker
2032 testsuite test ld-scripts/weak.exp for an ELF port that uses
2033 the generic linker. */
2034 if (sec->owner == NULL)
2035 continue;
2037 BFD_ASSERT (sec->owner == abfd);
2039 sect_syms[sec->index] = syms[idx];
2044 for (asect = abfd->sections; asect; asect = asect->next)
2046 if (sect_syms[asect->index] != NULL)
2047 continue;
2049 sym = bfd_make_empty_symbol (abfd);
2050 if (sym == NULL)
2051 return false;
2052 sym->the_bfd = abfd;
2053 sym->name = asect->name;
2054 sym->value = 0;
2055 /* Set the flags to 0 to indicate that this one was newly added. */
2056 sym->flags = 0;
2057 sym->section = asect;
2058 sect_syms[asect->index] = sym;
2059 num_sections++;
2060 #ifdef DEBUG
2061 fprintf (stderr,
2062 _("creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n"),
2063 asect->name, (long) asect->vma, asect->index, (long) asect);
2064 #endif
2067 /* Classify all of the symbols. */
2068 for (idx = 0; idx < symcount; idx++)
2070 if (!sym_is_global (abfd, syms[idx]))
2071 num_locals++;
2072 else
2073 num_globals++;
2075 for (asect = abfd->sections; asect; asect = asect->next)
2077 if (sect_syms[asect->index] != NULL
2078 && sect_syms[asect->index]->flags == 0)
2080 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
2081 if (!sym_is_global (abfd, sect_syms[asect->index]))
2082 num_locals++;
2083 else
2084 num_globals++;
2085 sect_syms[asect->index]->flags = 0;
2089 /* Now sort the symbols so the local symbols are first. */
2090 new_syms = ((asymbol **)
2091 bfd_alloc (abfd,
2092 (num_locals + num_globals) * sizeof (asymbol *)));
2093 if (new_syms == NULL)
2094 return false;
2096 for (idx = 0; idx < symcount; idx++)
2098 asymbol *sym = syms[idx];
2099 int i;
2101 if (!sym_is_global (abfd, sym))
2102 i = num_locals2++;
2103 else
2104 i = num_locals + num_globals2++;
2105 new_syms[i] = sym;
2106 sym->udata.i = i + 1;
2108 for (asect = abfd->sections; asect; asect = asect->next)
2110 if (sect_syms[asect->index] != NULL
2111 && sect_syms[asect->index]->flags == 0)
2113 asymbol *sym = sect_syms[asect->index];
2114 int i;
2116 sym->flags = BSF_SECTION_SYM;
2117 if (!sym_is_global (abfd, sym))
2118 i = num_locals2++;
2119 else
2120 i = num_locals + num_globals2++;
2121 new_syms[i] = sym;
2122 sym->udata.i = i + 1;
2126 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
2128 elf_num_locals (abfd) = num_locals;
2129 elf_num_globals (abfd) = num_globals;
2130 return true;
2133 /* Align to the maximum file alignment that could be required for any
2134 ELF data structure. */
2136 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
2137 static INLINE file_ptr
2138 align_file_position (off, align)
2139 file_ptr off;
2140 int align;
2142 return (off + align - 1) & ~(align - 1);
2145 /* Assign a file position to a section, optionally aligning to the
2146 required section alignment. */
2148 INLINE file_ptr
2149 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
2150 Elf_Internal_Shdr *i_shdrp;
2151 file_ptr offset;
2152 boolean align;
2154 if (align)
2156 unsigned int al;
2158 al = i_shdrp->sh_addralign;
2159 if (al > 1)
2160 offset = BFD_ALIGN (offset, al);
2162 i_shdrp->sh_offset = offset;
2163 if (i_shdrp->bfd_section != NULL)
2164 i_shdrp->bfd_section->filepos = offset;
2165 if (i_shdrp->sh_type != SHT_NOBITS)
2166 offset += i_shdrp->sh_size;
2167 return offset;
2170 /* Compute the file positions we are going to put the sections at, and
2171 otherwise prepare to begin writing out the ELF file. If LINK_INFO
2172 is not NULL, this is being called by the ELF backend linker. */
2174 boolean
2175 _bfd_elf_compute_section_file_positions (abfd, link_info)
2176 bfd *abfd;
2177 struct bfd_link_info *link_info;
2179 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2180 boolean failed;
2181 struct bfd_strtab_hash *strtab;
2182 Elf_Internal_Shdr *shstrtab_hdr;
2184 if (abfd->output_has_begun)
2185 return true;
2187 /* Do any elf backend specific processing first. */
2188 if (bed->elf_backend_begin_write_processing)
2189 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
2191 if (! prep_headers (abfd))
2192 return false;
2194 /* Post process the headers if necessary. */
2195 if (bed->elf_backend_post_process_headers)
2196 (*bed->elf_backend_post_process_headers) (abfd, link_info);
2198 failed = false;
2199 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
2200 if (failed)
2201 return false;
2203 if (!assign_section_numbers (abfd))
2204 return false;
2206 /* The backend linker builds symbol table information itself. */
2207 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2209 /* Non-zero if doing a relocatable link. */
2210 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC));
2212 if (! swap_out_syms (abfd, &strtab, relocatable_p))
2213 return false;
2216 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
2217 /* sh_name was set in prep_headers. */
2218 shstrtab_hdr->sh_type = SHT_STRTAB;
2219 shstrtab_hdr->sh_flags = 0;
2220 shstrtab_hdr->sh_addr = 0;
2221 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
2222 shstrtab_hdr->sh_entsize = 0;
2223 shstrtab_hdr->sh_link = 0;
2224 shstrtab_hdr->sh_info = 0;
2225 /* sh_offset is set in assign_file_positions_except_relocs. */
2226 shstrtab_hdr->sh_addralign = 1;
2228 if (!assign_file_positions_except_relocs (abfd))
2229 return false;
2231 if (link_info == NULL && bfd_get_symcount (abfd) > 0)
2233 file_ptr off;
2234 Elf_Internal_Shdr *hdr;
2236 off = elf_tdata (abfd)->next_file_pos;
2238 hdr = &elf_tdata (abfd)->symtab_hdr;
2239 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2241 hdr = &elf_tdata (abfd)->strtab_hdr;
2242 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2244 elf_tdata (abfd)->next_file_pos = off;
2246 /* Now that we know where the .strtab section goes, write it
2247 out. */
2248 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
2249 || ! _bfd_stringtab_emit (abfd, strtab))
2250 return false;
2251 _bfd_stringtab_free (strtab);
2254 abfd->output_has_begun = true;
2256 return true;
2259 /* Create a mapping from a set of sections to a program segment. */
2261 static INLINE struct elf_segment_map *
2262 make_mapping (abfd, sections, from, to, phdr)
2263 bfd *abfd;
2264 asection **sections;
2265 unsigned int from;
2266 unsigned int to;
2267 boolean phdr;
2269 struct elf_segment_map *m;
2270 unsigned int i;
2271 asection **hdrpp;
2273 m = ((struct elf_segment_map *)
2274 bfd_zalloc (abfd,
2275 (sizeof (struct elf_segment_map)
2276 + (to - from - 1) * sizeof (asection *))));
2277 if (m == NULL)
2278 return NULL;
2279 m->next = NULL;
2280 m->p_type = PT_LOAD;
2281 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
2282 m->sections[i - from] = *hdrpp;
2283 m->count = to - from;
2285 if (from == 0 && phdr)
2287 /* Include the headers in the first PT_LOAD segment. */
2288 m->includes_filehdr = 1;
2289 m->includes_phdrs = 1;
2292 return m;
2295 /* Set up a mapping from BFD sections to program segments. */
2297 static boolean
2298 map_sections_to_segments (abfd)
2299 bfd *abfd;
2301 asection **sections = NULL;
2302 asection *s;
2303 unsigned int i;
2304 unsigned int count;
2305 struct elf_segment_map *mfirst;
2306 struct elf_segment_map **pm;
2307 struct elf_segment_map *m;
2308 asection *last_hdr;
2309 unsigned int phdr_index;
2310 bfd_vma maxpagesize;
2311 asection **hdrpp;
2312 boolean phdr_in_segment = true;
2313 boolean writable;
2314 asection *dynsec;
2316 if (elf_tdata (abfd)->segment_map != NULL)
2317 return true;
2319 if (bfd_count_sections (abfd) == 0)
2320 return true;
2322 /* Select the allocated sections, and sort them. */
2324 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
2325 * sizeof (asection *));
2326 if (sections == NULL)
2327 goto error_return;
2329 i = 0;
2330 for (s = abfd->sections; s != NULL; s = s->next)
2332 if ((s->flags & SEC_ALLOC) != 0)
2334 sections[i] = s;
2335 ++i;
2338 BFD_ASSERT (i <= bfd_count_sections (abfd));
2339 count = i;
2341 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
2343 /* Build the mapping. */
2345 mfirst = NULL;
2346 pm = &mfirst;
2348 /* If we have a .interp section, then create a PT_PHDR segment for
2349 the program headers and a PT_INTERP segment for the .interp
2350 section. */
2351 s = bfd_get_section_by_name (abfd, ".interp");
2352 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2354 m = ((struct elf_segment_map *)
2355 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2356 if (m == NULL)
2357 goto error_return;
2358 m->next = NULL;
2359 m->p_type = PT_PHDR;
2360 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
2361 m->p_flags = PF_R | PF_X;
2362 m->p_flags_valid = 1;
2363 m->includes_phdrs = 1;
2365 *pm = m;
2366 pm = &m->next;
2368 m = ((struct elf_segment_map *)
2369 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2370 if (m == NULL)
2371 goto error_return;
2372 m->next = NULL;
2373 m->p_type = PT_INTERP;
2374 m->count = 1;
2375 m->sections[0] = s;
2377 *pm = m;
2378 pm = &m->next;
2381 /* Look through the sections. We put sections in the same program
2382 segment when the start of the second section can be placed within
2383 a few bytes of the end of the first section. */
2384 last_hdr = NULL;
2385 phdr_index = 0;
2386 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2387 writable = false;
2388 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
2389 if (dynsec != NULL
2390 && (dynsec->flags & SEC_LOAD) == 0)
2391 dynsec = NULL;
2393 /* Deal with -Ttext or something similar such that the first section
2394 is not adjacent to the program headers. This is an
2395 approximation, since at this point we don't know exactly how many
2396 program headers we will need. */
2397 if (count > 0)
2399 bfd_size_type phdr_size;
2401 phdr_size = elf_tdata (abfd)->program_header_size;
2402 if (phdr_size == 0)
2403 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
2404 if ((abfd->flags & D_PAGED) == 0
2405 || sections[0]->lma < phdr_size
2406 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
2407 phdr_in_segment = false;
2410 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
2412 asection *hdr;
2413 boolean new_segment;
2415 hdr = *hdrpp;
2417 /* See if this section and the last one will fit in the same
2418 segment. */
2420 if (last_hdr == NULL)
2422 /* If we don't have a segment yet, then we don't need a new
2423 one (we build the last one after this loop). */
2424 new_segment = false;
2426 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
2428 /* If this section has a different relation between the
2429 virtual address and the load address, then we need a new
2430 segment. */
2431 new_segment = true;
2433 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2434 < BFD_ALIGN (hdr->lma, maxpagesize))
2436 /* If putting this section in this segment would force us to
2437 skip a page in the segment, then we need a new segment. */
2438 new_segment = true;
2440 else if ((last_hdr->flags & SEC_LOAD) == 0
2441 && (hdr->flags & SEC_LOAD) != 0)
2443 /* We don't want to put a loadable section after a
2444 nonloadable section in the same segment. */
2445 new_segment = true;
2447 else if ((abfd->flags & D_PAGED) == 0)
2449 /* If the file is not demand paged, which means that we
2450 don't require the sections to be correctly aligned in the
2451 file, then there is no other reason for a new segment. */
2452 new_segment = false;
2454 else if (! writable
2455 && (hdr->flags & SEC_READONLY) == 0
2456 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
2457 == hdr->lma))
2459 /* We don't want to put a writable section in a read only
2460 segment, unless they are on the same page in memory
2461 anyhow. We already know that the last section does not
2462 bring us past the current section on the page, so the
2463 only case in which the new section is not on the same
2464 page as the previous section is when the previous section
2465 ends precisely on a page boundary. */
2466 new_segment = true;
2468 else
2470 /* Otherwise, we can use the same segment. */
2471 new_segment = false;
2474 if (! new_segment)
2476 if ((hdr->flags & SEC_READONLY) == 0)
2477 writable = true;
2478 last_hdr = hdr;
2479 continue;
2482 /* We need a new program segment. We must create a new program
2483 header holding all the sections from phdr_index until hdr. */
2485 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2486 if (m == NULL)
2487 goto error_return;
2489 *pm = m;
2490 pm = &m->next;
2492 if ((hdr->flags & SEC_READONLY) == 0)
2493 writable = true;
2494 else
2495 writable = false;
2497 last_hdr = hdr;
2498 phdr_index = i;
2499 phdr_in_segment = false;
2502 /* Create a final PT_LOAD program segment. */
2503 if (last_hdr != NULL)
2505 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment);
2506 if (m == NULL)
2507 goto error_return;
2509 *pm = m;
2510 pm = &m->next;
2513 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
2514 if (dynsec != NULL)
2516 m = ((struct elf_segment_map *)
2517 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2518 if (m == NULL)
2519 goto error_return;
2520 m->next = NULL;
2521 m->p_type = PT_DYNAMIC;
2522 m->count = 1;
2523 m->sections[0] = dynsec;
2525 *pm = m;
2526 pm = &m->next;
2529 /* For each loadable .note section, add a PT_NOTE segment. We don't
2530 use bfd_get_section_by_name, because if we link together
2531 nonloadable .note sections and loadable .note sections, we will
2532 generate two .note sections in the output file. FIXME: Using
2533 names for section types is bogus anyhow. */
2534 for (s = abfd->sections; s != NULL; s = s->next)
2536 if ((s->flags & SEC_LOAD) != 0
2537 && strncmp (s->name, ".note", 5) == 0)
2539 m = ((struct elf_segment_map *)
2540 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
2541 if (m == NULL)
2542 goto error_return;
2543 m->next = NULL;
2544 m->p_type = PT_NOTE;
2545 m->count = 1;
2546 m->sections[0] = s;
2548 *pm = m;
2549 pm = &m->next;
2553 free (sections);
2554 sections = NULL;
2556 elf_tdata (abfd)->segment_map = mfirst;
2557 return true;
2559 error_return:
2560 if (sections != NULL)
2561 free (sections);
2562 return false;
2565 /* Sort sections by address. */
2567 static int
2568 elf_sort_sections (arg1, arg2)
2569 const PTR arg1;
2570 const PTR arg2;
2572 const asection *sec1 = *(const asection **) arg1;
2573 const asection *sec2 = *(const asection **) arg2;
2575 /* Sort by LMA first, since this is the address used to
2576 place the section into a segment. */
2577 if (sec1->lma < sec2->lma)
2578 return -1;
2579 else if (sec1->lma > sec2->lma)
2580 return 1;
2582 /* Then sort by VMA. Normally the LMA and the VMA will be
2583 the same, and this will do nothing. */
2584 if (sec1->vma < sec2->vma)
2585 return -1;
2586 else if (sec1->vma > sec2->vma)
2587 return 1;
2589 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
2591 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
2593 if (TOEND (sec1))
2595 if (TOEND (sec2))
2596 return sec1->target_index - sec2->target_index;
2597 else
2598 return 1;
2601 if (TOEND (sec2))
2602 return -1;
2604 #undef TOEND
2606 /* Sort by size, to put zero sized sections before others at the
2607 same address. */
2609 if (sec1->_raw_size < sec2->_raw_size)
2610 return -1;
2611 if (sec1->_raw_size > sec2->_raw_size)
2612 return 1;
2614 return sec1->target_index - sec2->target_index;
2617 /* Assign file positions to the sections based on the mapping from
2618 sections to segments. This function also sets up some fields in
2619 the file header, and writes out the program headers. */
2621 static boolean
2622 assign_file_positions_for_segments (abfd)
2623 bfd *abfd;
2625 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
2626 unsigned int count;
2627 struct elf_segment_map *m;
2628 unsigned int alloc;
2629 Elf_Internal_Phdr *phdrs;
2630 file_ptr off, voff;
2631 bfd_vma filehdr_vaddr, filehdr_paddr;
2632 bfd_vma phdrs_vaddr, phdrs_paddr;
2633 Elf_Internal_Phdr *p;
2635 if (elf_tdata (abfd)->segment_map == NULL)
2637 if (! map_sections_to_segments (abfd))
2638 return false;
2641 if (bed->elf_backend_modify_segment_map)
2643 if (! (*bed->elf_backend_modify_segment_map) (abfd))
2644 return false;
2647 count = 0;
2648 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2649 ++count;
2651 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
2652 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
2653 elf_elfheader (abfd)->e_phnum = count;
2655 if (count == 0)
2656 return true;
2658 /* If we already counted the number of program segments, make sure
2659 that we allocated enough space. This happens when SIZEOF_HEADERS
2660 is used in a linker script. */
2661 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
2662 if (alloc != 0 && count > alloc)
2664 ((*_bfd_error_handler)
2665 (_("%s: Not enough room for program headers (allocated %u, need %u)"),
2666 bfd_get_filename (abfd), alloc, count));
2667 bfd_set_error (bfd_error_bad_value);
2668 return false;
2671 if (alloc == 0)
2672 alloc = count;
2674 phdrs = ((Elf_Internal_Phdr *)
2675 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
2676 if (phdrs == NULL)
2677 return false;
2679 off = bed->s->sizeof_ehdr;
2680 off += alloc * bed->s->sizeof_phdr;
2682 filehdr_vaddr = 0;
2683 filehdr_paddr = 0;
2684 phdrs_vaddr = 0;
2685 phdrs_paddr = 0;
2687 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2688 m != NULL;
2689 m = m->next, p++)
2691 unsigned int i;
2692 asection **secpp;
2694 /* If elf_segment_map is not from map_sections_to_segments, the
2695 sections may not be correctly ordered. */
2696 if (m->count > 0)
2697 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2698 elf_sort_sections);
2700 p->p_type = m->p_type;
2701 p->p_flags = m->p_flags;
2703 if (p->p_type == PT_LOAD
2704 && m->count > 0
2705 && (m->sections[0]->flags & SEC_ALLOC) != 0)
2707 if ((abfd->flags & D_PAGED) != 0)
2708 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2709 else
2711 bfd_size_type align;
2713 align = 0;
2714 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2716 bfd_size_type secalign;
2718 secalign = bfd_get_section_alignment (abfd, *secpp);
2719 if (secalign > align)
2720 align = secalign;
2723 off += (m->sections[0]->vma - off) % (1 << align);
2727 if (m->count == 0)
2728 p->p_vaddr = 0;
2729 else
2730 p->p_vaddr = m->sections[0]->vma;
2732 if (m->p_paddr_valid)
2733 p->p_paddr = m->p_paddr;
2734 else if (m->count == 0)
2735 p->p_paddr = 0;
2736 else
2737 p->p_paddr = m->sections[0]->lma;
2739 if (p->p_type == PT_LOAD
2740 && (abfd->flags & D_PAGED) != 0)
2741 p->p_align = bed->maxpagesize;
2742 else if (m->count == 0)
2743 p->p_align = bed->s->file_align;
2744 else
2745 p->p_align = 0;
2747 p->p_offset = 0;
2748 p->p_filesz = 0;
2749 p->p_memsz = 0;
2751 if (m->includes_filehdr)
2753 if (! m->p_flags_valid)
2754 p->p_flags |= PF_R;
2755 p->p_offset = 0;
2756 p->p_filesz = bed->s->sizeof_ehdr;
2757 p->p_memsz = bed->s->sizeof_ehdr;
2758 if (m->count > 0)
2760 BFD_ASSERT (p->p_type == PT_LOAD);
2762 if (p->p_vaddr < (bfd_vma) off)
2764 _bfd_error_handler (_("%s: Not enough room for program headers, try linking with -N"),
2765 bfd_get_filename (abfd));
2766 bfd_set_error (bfd_error_bad_value);
2767 return false;
2770 p->p_vaddr -= off;
2771 if (! m->p_paddr_valid)
2772 p->p_paddr -= off;
2774 if (p->p_type == PT_LOAD)
2776 filehdr_vaddr = p->p_vaddr;
2777 filehdr_paddr = p->p_paddr;
2781 if (m->includes_phdrs)
2783 if (! m->p_flags_valid)
2784 p->p_flags |= PF_R;
2786 if (m->includes_filehdr)
2788 if (p->p_type == PT_LOAD)
2790 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2791 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2794 else
2796 p->p_offset = bed->s->sizeof_ehdr;
2798 if (m->count > 0)
2800 BFD_ASSERT (p->p_type == PT_LOAD);
2801 p->p_vaddr -= off - p->p_offset;
2802 if (! m->p_paddr_valid)
2803 p->p_paddr -= off - p->p_offset;
2806 if (p->p_type == PT_LOAD)
2808 phdrs_vaddr = p->p_vaddr;
2809 phdrs_paddr = p->p_paddr;
2811 else
2812 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr;
2815 p->p_filesz += alloc * bed->s->sizeof_phdr;
2816 p->p_memsz += alloc * bed->s->sizeof_phdr;
2819 if (p->p_type == PT_LOAD
2820 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core))
2822 if (! m->includes_filehdr && ! m->includes_phdrs)
2823 p->p_offset = off;
2824 else
2826 file_ptr adjust;
2828 adjust = off - (p->p_offset + p->p_filesz);
2829 p->p_filesz += adjust;
2830 p->p_memsz += adjust;
2834 voff = off;
2836 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2838 asection *sec;
2839 flagword flags;
2840 bfd_size_type align;
2842 sec = *secpp;
2843 flags = sec->flags;
2844 align = 1 << bfd_get_section_alignment (abfd, sec);
2846 /* The section may have artificial alignment forced by a
2847 link script. Notice this case by the gap between the
2848 cumulative phdr vma and the section's vma. */
2849 if (p->p_vaddr + p->p_memsz < sec->vma)
2851 bfd_vma adjust = sec->vma - (p->p_vaddr + p->p_memsz);
2853 p->p_memsz += adjust;
2854 off += adjust;
2855 voff += adjust;
2856 if ((flags & SEC_LOAD) != 0)
2857 p->p_filesz += adjust;
2860 if (p->p_type == PT_LOAD)
2862 bfd_signed_vma adjust;
2864 if ((flags & SEC_LOAD) != 0)
2866 adjust = sec->lma - (p->p_paddr + p->p_memsz);
2867 if (adjust < 0)
2868 adjust = 0;
2870 else if ((flags & SEC_ALLOC) != 0)
2872 /* The section VMA must equal the file position
2873 modulo the page size. FIXME: I'm not sure if
2874 this adjustment is really necessary. We used to
2875 not have the SEC_LOAD case just above, and then
2876 this was necessary, but now I'm not sure. */
2877 if ((abfd->flags & D_PAGED) != 0)
2878 adjust = (sec->vma - voff) % bed->maxpagesize;
2879 else
2880 adjust = (sec->vma - voff) % align;
2882 else
2883 adjust = 0;
2885 if (adjust != 0)
2887 if (i == 0)
2889 (* _bfd_error_handler)
2890 (_("Error: First section in segment (%s) starts at 0x%x"),
2891 bfd_section_name (abfd, sec), sec->lma);
2892 (* _bfd_error_handler)
2893 (_(" whereas segment starts at 0x%x"),
2894 p->p_paddr);
2896 return false;
2898 p->p_memsz += adjust;
2899 off += adjust;
2900 voff += adjust;
2901 if ((flags & SEC_LOAD) != 0)
2902 p->p_filesz += adjust;
2905 sec->filepos = off;
2907 /* We check SEC_HAS_CONTENTS here because if NOLOAD is
2908 used in a linker script we may have a section with
2909 SEC_LOAD clear but which is supposed to have
2910 contents. */
2911 if ((flags & SEC_LOAD) != 0
2912 || (flags & SEC_HAS_CONTENTS) != 0)
2913 off += sec->_raw_size;
2915 if ((flags & SEC_ALLOC) != 0)
2916 voff += sec->_raw_size;
2919 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
2921 if (i == 0) /* the actual "note" segment */
2922 { /* this one actually contains everything. */
2923 sec->filepos = off;
2924 p->p_filesz = sec->_raw_size;
2925 off += sec->_raw_size;
2926 voff = off;
2928 else /* fake sections -- don't need to be written */
2930 sec->filepos = 0;
2931 sec->_raw_size = 0;
2932 flags = sec->flags = 0; /* no contents */
2934 p->p_memsz = 0;
2935 p->p_align = 1;
2937 else
2939 p->p_memsz += sec->_raw_size;
2941 if ((flags & SEC_LOAD) != 0)
2942 p->p_filesz += sec->_raw_size;
2944 if (align > p->p_align
2945 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0))
2946 p->p_align = align;
2949 if (! m->p_flags_valid)
2951 p->p_flags |= PF_R;
2952 if ((flags & SEC_CODE) != 0)
2953 p->p_flags |= PF_X;
2954 if ((flags & SEC_READONLY) == 0)
2955 p->p_flags |= PF_W;
2960 /* Now that we have set the section file positions, we can set up
2961 the file positions for the non PT_LOAD segments. */
2962 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2963 m != NULL;
2964 m = m->next, p++)
2966 if (p->p_type != PT_LOAD && m->count > 0)
2968 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2969 p->p_offset = m->sections[0]->filepos;
2971 if (m->count == 0)
2973 if (m->includes_filehdr)
2975 p->p_vaddr = filehdr_vaddr;
2976 if (! m->p_paddr_valid)
2977 p->p_paddr = filehdr_paddr;
2979 else if (m->includes_phdrs)
2981 p->p_vaddr = phdrs_vaddr;
2982 if (! m->p_paddr_valid)
2983 p->p_paddr = phdrs_paddr;
2988 /* Clear out any program headers we allocated but did not use. */
2989 for (; count < alloc; count++, p++)
2991 memset (p, 0, sizeof *p);
2992 p->p_type = PT_NULL;
2995 elf_tdata (abfd)->phdr = phdrs;
2997 elf_tdata (abfd)->next_file_pos = off;
2999 /* Write out the program headers. */
3000 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
3001 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
3002 return false;
3004 return true;
3007 /* Get the size of the program header.
3009 If this is called by the linker before any of the section VMA's are set, it
3010 can't calculate the correct value for a strange memory layout. This only
3011 happens when SIZEOF_HEADERS is used in a linker script. In this case,
3012 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
3013 data segment (exclusive of .interp and .dynamic).
3015 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
3016 will be two segments. */
3018 static bfd_size_type
3019 get_program_header_size (abfd)
3020 bfd *abfd;
3022 size_t segs;
3023 asection *s;
3024 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3026 /* We can't return a different result each time we're called. */
3027 if (elf_tdata (abfd)->program_header_size != 0)
3028 return elf_tdata (abfd)->program_header_size;
3030 if (elf_tdata (abfd)->segment_map != NULL)
3032 struct elf_segment_map *m;
3034 segs = 0;
3035 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
3036 ++segs;
3037 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3038 return elf_tdata (abfd)->program_header_size;
3041 /* Assume we will need exactly two PT_LOAD segments: one for text
3042 and one for data. */
3043 segs = 2;
3045 s = bfd_get_section_by_name (abfd, ".interp");
3046 if (s != NULL && (s->flags & SEC_LOAD) != 0)
3048 /* If we have a loadable interpreter section, we need a
3049 PT_INTERP segment. In this case, assume we also need a
3050 PT_PHDR segment, although that may not be true for all
3051 targets. */
3052 segs += 2;
3055 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
3057 /* We need a PT_DYNAMIC segment. */
3058 ++segs;
3061 for (s = abfd->sections; s != NULL; s = s->next)
3063 if ((s->flags & SEC_LOAD) != 0
3064 && strncmp (s->name, ".note", 5) == 0)
3066 /* We need a PT_NOTE segment. */
3067 ++segs;
3071 /* Let the backend count up any program headers it might need. */
3072 if (bed->elf_backend_additional_program_headers)
3074 int a;
3076 a = (*bed->elf_backend_additional_program_headers) (abfd);
3077 if (a == -1)
3078 abort ();
3079 segs += a;
3082 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
3083 return elf_tdata (abfd)->program_header_size;
3086 /* Work out the file positions of all the sections. This is called by
3087 _bfd_elf_compute_section_file_positions. All the section sizes and
3088 VMAs must be known before this is called.
3090 We do not consider reloc sections at this point, unless they form
3091 part of the loadable image. Reloc sections are assigned file
3092 positions in assign_file_positions_for_relocs, which is called by
3093 write_object_contents and final_link.
3095 We also don't set the positions of the .symtab and .strtab here. */
3097 static boolean
3098 assign_file_positions_except_relocs (abfd)
3099 bfd *abfd;
3101 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
3102 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
3103 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
3104 file_ptr off;
3105 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3107 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0
3108 && bfd_get_format (abfd) != bfd_core)
3110 Elf_Internal_Shdr **hdrpp;
3111 unsigned int i;
3113 /* Start after the ELF header. */
3114 off = i_ehdrp->e_ehsize;
3116 /* We are not creating an executable, which means that we are
3117 not creating a program header, and that the actual order of
3118 the sections in the file is unimportant. */
3119 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3121 Elf_Internal_Shdr *hdr;
3123 hdr = *hdrpp;
3124 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
3126 hdr->sh_offset = -1;
3127 continue;
3129 if (i == tdata->symtab_section
3130 || i == tdata->strtab_section)
3132 hdr->sh_offset = -1;
3133 continue;
3136 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3139 else
3141 unsigned int i;
3142 Elf_Internal_Shdr **hdrpp;
3144 /* Assign file positions for the loaded sections based on the
3145 assignment of sections to segments. */
3146 if (! assign_file_positions_for_segments (abfd))
3147 return false;
3149 /* Assign file positions for the other sections. */
3151 off = elf_tdata (abfd)->next_file_pos;
3152 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
3154 Elf_Internal_Shdr *hdr;
3156 hdr = *hdrpp;
3157 if (hdr->bfd_section != NULL
3158 && hdr->bfd_section->filepos != 0)
3159 hdr->sh_offset = hdr->bfd_section->filepos;
3160 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
3162 ((*_bfd_error_handler)
3163 (_("%s: warning: allocated section `%s' not in segment"),
3164 bfd_get_filename (abfd),
3165 (hdr->bfd_section == NULL
3166 ? "*unknown*"
3167 : hdr->bfd_section->name)));
3168 if ((abfd->flags & D_PAGED) != 0)
3169 off += (hdr->sh_addr - off) % bed->maxpagesize;
3170 else
3171 off += (hdr->sh_addr - off) % hdr->sh_addralign;
3172 off = _bfd_elf_assign_file_position_for_section (hdr, off,
3173 false);
3175 else if (hdr->sh_type == SHT_REL
3176 || hdr->sh_type == SHT_RELA
3177 || hdr == i_shdrpp[tdata->symtab_section]
3178 || hdr == i_shdrpp[tdata->strtab_section])
3179 hdr->sh_offset = -1;
3180 else
3181 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
3185 /* Place the section headers. */
3186 off = align_file_position (off, bed->s->file_align);
3187 i_ehdrp->e_shoff = off;
3188 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
3190 elf_tdata (abfd)->next_file_pos = off;
3192 return true;
3195 static boolean
3196 prep_headers (abfd)
3197 bfd *abfd;
3199 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3200 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
3201 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
3202 int count;
3203 struct bfd_strtab_hash *shstrtab;
3204 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3206 i_ehdrp = elf_elfheader (abfd);
3207 i_shdrp = elf_elfsections (abfd);
3209 shstrtab = _bfd_elf_stringtab_init ();
3210 if (shstrtab == NULL)
3211 return false;
3213 elf_shstrtab (abfd) = shstrtab;
3215 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
3216 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
3217 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
3218 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
3220 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
3221 i_ehdrp->e_ident[EI_DATA] =
3222 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
3223 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
3225 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
3226 i_ehdrp->e_ident[EI_ABIVERSION] = 0;
3228 for (count = EI_PAD; count < EI_NIDENT; count++)
3229 i_ehdrp->e_ident[count] = 0;
3231 if ((abfd->flags & DYNAMIC) != 0)
3232 i_ehdrp->e_type = ET_DYN;
3233 else if ((abfd->flags & EXEC_P) != 0)
3234 i_ehdrp->e_type = ET_EXEC;
3235 else if (bfd_get_format (abfd) == bfd_core)
3236 i_ehdrp->e_type = ET_CORE;
3237 else
3238 i_ehdrp->e_type = ET_REL;
3240 switch (bfd_get_arch (abfd))
3242 case bfd_arch_unknown:
3243 i_ehdrp->e_machine = EM_NONE;
3244 break;
3245 case bfd_arch_sparc:
3246 if (bfd_elf_get_arch_size (abfd) == 64)
3247 i_ehdrp->e_machine = EM_SPARCV9;
3248 else
3249 i_ehdrp->e_machine = EM_SPARC;
3250 break;
3251 case bfd_arch_i370:
3252 i_ehdrp->e_machine = EM_S370;
3253 break;
3254 case bfd_arch_i386:
3255 i_ehdrp->e_machine = EM_386;
3256 break;
3257 case bfd_arch_ia64:
3258 i_ehdrp->e_machine = EM_IA_64;
3259 break;
3260 case bfd_arch_m68k:
3261 i_ehdrp->e_machine = EM_68K;
3262 break;
3263 case bfd_arch_m88k:
3264 i_ehdrp->e_machine = EM_88K;
3265 break;
3266 case bfd_arch_i860:
3267 i_ehdrp->e_machine = EM_860;
3268 break;
3269 case bfd_arch_i960:
3270 i_ehdrp->e_machine = EM_960;
3271 break;
3272 case bfd_arch_mips: /* MIPS Rxxxx */
3273 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
3274 break;
3275 case bfd_arch_hppa:
3276 i_ehdrp->e_machine = EM_PARISC;
3277 break;
3278 case bfd_arch_powerpc:
3279 i_ehdrp->e_machine = EM_PPC;
3280 break;
3281 case bfd_arch_alpha:
3282 i_ehdrp->e_machine = EM_ALPHA;
3283 break;
3284 case bfd_arch_sh:
3285 i_ehdrp->e_machine = EM_SH;
3286 break;
3287 case bfd_arch_d10v:
3288 i_ehdrp->e_machine = EM_CYGNUS_D10V;
3289 break;
3290 case bfd_arch_d30v:
3291 i_ehdrp->e_machine = EM_CYGNUS_D30V;
3292 break;
3293 case bfd_arch_fr30:
3294 i_ehdrp->e_machine = EM_CYGNUS_FR30;
3295 break;
3296 case bfd_arch_mcore:
3297 i_ehdrp->e_machine = EM_MCORE;
3298 break;
3299 case bfd_arch_avr:
3300 i_ehdrp->e_machine = EM_AVR;
3301 break;
3302 case bfd_arch_v850:
3303 switch (bfd_get_mach (abfd))
3305 default:
3306 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break;
3308 break;
3309 case bfd_arch_arc:
3310 i_ehdrp->e_machine = EM_CYGNUS_ARC;
3311 break;
3312 case bfd_arch_arm:
3313 i_ehdrp->e_machine = EM_ARM;
3314 break;
3315 case bfd_arch_m32r:
3316 i_ehdrp->e_machine = EM_CYGNUS_M32R;
3317 break;
3318 case bfd_arch_mn10200:
3319 i_ehdrp->e_machine = EM_CYGNUS_MN10200;
3320 break;
3321 case bfd_arch_mn10300:
3322 i_ehdrp->e_machine = EM_CYGNUS_MN10300;
3323 break;
3324 case bfd_arch_pj:
3325 i_ehdrp->e_machine = EM_PJ;
3326 break;
3327 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
3328 default:
3329 i_ehdrp->e_machine = EM_NONE;
3331 i_ehdrp->e_version = bed->s->ev_current;
3332 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
3334 /* no program header, for now. */
3335 i_ehdrp->e_phoff = 0;
3336 i_ehdrp->e_phentsize = 0;
3337 i_ehdrp->e_phnum = 0;
3339 /* each bfd section is section header entry */
3340 i_ehdrp->e_entry = bfd_get_start_address (abfd);
3341 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
3343 /* if we're building an executable, we'll need a program header table */
3344 if (abfd->flags & EXEC_P)
3346 /* it all happens later */
3347 #if 0
3348 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
3350 /* elf_build_phdrs() returns a (NULL-terminated) array of
3351 Elf_Internal_Phdrs */
3352 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
3353 i_ehdrp->e_phoff = outbase;
3354 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
3355 #endif
3357 else
3359 i_ehdrp->e_phentsize = 0;
3360 i_phdrp = 0;
3361 i_ehdrp->e_phoff = 0;
3364 elf_tdata (abfd)->symtab_hdr.sh_name =
3365 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
3366 elf_tdata (abfd)->strtab_hdr.sh_name =
3367 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
3368 elf_tdata (abfd)->shstrtab_hdr.sh_name =
3369 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
3370 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3371 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
3372 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
3373 return false;
3375 return true;
3378 /* Assign file positions for all the reloc sections which are not part
3379 of the loadable file image. */
3381 void
3382 _bfd_elf_assign_file_positions_for_relocs (abfd)
3383 bfd *abfd;
3385 file_ptr off;
3386 unsigned int i;
3387 Elf_Internal_Shdr **shdrpp;
3389 off = elf_tdata (abfd)->next_file_pos;
3391 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
3392 i < elf_elfheader (abfd)->e_shnum;
3393 i++, shdrpp++)
3395 Elf_Internal_Shdr *shdrp;
3397 shdrp = *shdrpp;
3398 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
3399 && shdrp->sh_offset == -1)
3400 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
3403 elf_tdata (abfd)->next_file_pos = off;
3406 boolean
3407 _bfd_elf_write_object_contents (abfd)
3408 bfd *abfd;
3410 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3411 Elf_Internal_Ehdr *i_ehdrp;
3412 Elf_Internal_Shdr **i_shdrp;
3413 boolean failed;
3414 unsigned int count;
3416 if (! abfd->output_has_begun
3417 && ! _bfd_elf_compute_section_file_positions
3418 (abfd, (struct bfd_link_info *) NULL))
3419 return false;
3421 i_shdrp = elf_elfsections (abfd);
3422 i_ehdrp = elf_elfheader (abfd);
3424 failed = false;
3425 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
3426 if (failed)
3427 return false;
3429 _bfd_elf_assign_file_positions_for_relocs (abfd);
3431 /* After writing the headers, we need to write the sections too... */
3432 for (count = 1; count < i_ehdrp->e_shnum; count++)
3434 if (bed->elf_backend_section_processing)
3435 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
3436 if (i_shdrp[count]->contents)
3438 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
3439 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
3440 1, abfd)
3441 != i_shdrp[count]->sh_size))
3442 return false;
3446 /* Write out the section header names. */
3447 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
3448 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
3449 return false;
3451 if (bed->elf_backend_final_write_processing)
3452 (*bed->elf_backend_final_write_processing) (abfd,
3453 elf_tdata (abfd)->linker);
3455 return bed->s->write_shdrs_and_ehdr (abfd);
3458 boolean
3459 _bfd_elf_write_corefile_contents (abfd)
3460 bfd *abfd;
3462 /* Hopefully this can be done just like an object file. */
3463 return _bfd_elf_write_object_contents (abfd);
3465 /* given a section, search the header to find them... */
3467 _bfd_elf_section_from_bfd_section (abfd, asect)
3468 bfd *abfd;
3469 struct sec *asect;
3471 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3472 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
3473 int index;
3474 Elf_Internal_Shdr *hdr;
3475 int maxindex = elf_elfheader (abfd)->e_shnum;
3477 for (index = 0; index < maxindex; index++)
3479 hdr = i_shdrp[index];
3480 if (hdr->bfd_section == asect)
3481 return index;
3484 if (bed->elf_backend_section_from_bfd_section)
3486 for (index = 0; index < maxindex; index++)
3488 int retval;
3490 hdr = i_shdrp[index];
3491 retval = index;
3492 if ((*bed->elf_backend_section_from_bfd_section)
3493 (abfd, hdr, asect, &retval))
3494 return retval;
3498 if (bfd_is_abs_section (asect))
3499 return SHN_ABS;
3500 if (bfd_is_com_section (asect))
3501 return SHN_COMMON;
3502 if (bfd_is_und_section (asect))
3503 return SHN_UNDEF;
3505 bfd_set_error (bfd_error_nonrepresentable_section);
3507 return -1;
3510 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
3511 on error. */
3514 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
3515 bfd *abfd;
3516 asymbol **asym_ptr_ptr;
3518 asymbol *asym_ptr = *asym_ptr_ptr;
3519 int idx;
3520 flagword flags = asym_ptr->flags;
3522 /* When gas creates relocations against local labels, it creates its
3523 own symbol for the section, but does put the symbol into the
3524 symbol chain, so udata is 0. When the linker is generating
3525 relocatable output, this section symbol may be for one of the
3526 input sections rather than the output section. */
3527 if (asym_ptr->udata.i == 0
3528 && (flags & BSF_SECTION_SYM)
3529 && asym_ptr->section)
3531 int indx;
3533 if (asym_ptr->section->output_section != NULL)
3534 indx = asym_ptr->section->output_section->index;
3535 else
3536 indx = asym_ptr->section->index;
3537 if (elf_section_syms (abfd)[indx])
3538 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
3541 idx = asym_ptr->udata.i;
3543 if (idx == 0)
3545 /* This case can occur when using --strip-symbol on a symbol
3546 which is used in a relocation entry. */
3547 (*_bfd_error_handler)
3548 (_("%s: symbol `%s' required but not present"),
3549 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
3550 bfd_set_error (bfd_error_no_symbols);
3551 return -1;
3554 #if DEBUG & 4
3556 fprintf (stderr,
3557 _("elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n"),
3558 (long) asym_ptr, asym_ptr->name, idx, flags,
3559 elf_symbol_flags (flags));
3560 fflush (stderr);
3562 #endif
3564 return idx;
3567 /* Copy private BFD data. This copies any program header information. */
3569 static boolean
3570 copy_private_bfd_data (ibfd, obfd)
3571 bfd *ibfd;
3572 bfd *obfd;
3574 Elf_Internal_Ehdr *iehdr;
3575 struct elf_segment_map *mfirst;
3576 struct elf_segment_map **pm;
3577 struct elf_segment_map *m;
3578 Elf_Internal_Phdr *p;
3579 unsigned int i;
3580 unsigned int num_segments;
3581 boolean phdr_included = false;
3583 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3584 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3585 return true;
3587 if (elf_tdata (ibfd)->phdr == NULL)
3588 return true;
3590 iehdr = elf_elfheader (ibfd);
3592 mfirst = NULL;
3593 pm = &mfirst;
3595 num_segments = elf_elfheader (ibfd)->e_phnum;
3597 #define IS_CONTAINED_BY(addr, len, bottom, phdr) \
3598 ((addr) >= (bottom) \
3599 && ( ((addr) + (len)) <= ((bottom) + (phdr)->p_memsz) \
3600 || ((addr) + (len)) <= ((bottom) + (phdr)->p_filesz)))
3602 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */
3604 #define IS_COREFILE_NOTE(p, s) \
3605 (p->p_type == PT_NOTE \
3606 && bfd_get_format (ibfd) == bfd_core \
3607 && s->vma == 0 && s->lma == 0 \
3608 && (bfd_vma) s->filepos >= p->p_offset \
3609 && (bfd_vma) s->filepos + s->_raw_size \
3610 <= p->p_offset + p->p_filesz)
3612 /* The complicated case when p_vaddr is 0 is to handle the Solaris
3613 linker, which generates a PT_INTERP section with p_vaddr and
3614 p_memsz set to 0. */
3616 #define IS_SOLARIS_PT_INTERP(p, s) \
3617 (p->p_vaddr == 0 \
3618 && p->p_filesz > 0 \
3619 && (s->flags & SEC_HAS_CONTENTS) != 0 \
3620 && s->_raw_size > 0 \
3621 && (bfd_vma) s->filepos >= p->p_offset \
3622 && ((bfd_vma) s->filepos + s->_raw_size \
3623 <= p->p_offset + p->p_filesz))
3625 /* Scan through the segments specified in the program header
3626 of the input BFD. */
3627 for (i = 0, p = elf_tdata (ibfd)->phdr; i < num_segments; i++, p++)
3629 unsigned int csecs;
3630 asection *s;
3631 asection **sections;
3632 asection *os;
3633 unsigned int isec;
3634 bfd_vma matching_lma;
3635 bfd_vma suggested_lma;
3636 unsigned int j;
3638 /* For each section in the input BFD, decide if it should be
3639 included in the current segment. A section will be included
3640 if it is within the address space of the segment, and it is
3641 an allocated segment, and there is an output section
3642 associated with it. */
3643 csecs = 0;
3644 for (s = ibfd->sections; s != NULL; s = s->next)
3645 if (s->output_section != NULL)
3647 if ((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3648 || IS_SOLARIS_PT_INTERP (p, s))
3649 && (s->flags & SEC_ALLOC) != 0)
3650 ++csecs;
3651 else if (IS_COREFILE_NOTE (p, s))
3652 ++csecs;
3655 /* Allocate a segment map big enough to contain all of the
3656 sections we have selected. */
3657 m = ((struct elf_segment_map *)
3658 bfd_alloc (obfd,
3659 (sizeof (struct elf_segment_map)
3660 + ((size_t) csecs - 1) * sizeof (asection *))));
3661 if (m == NULL)
3662 return false;
3664 /* Initialise the fields of the segment map. Default to
3665 using the physical address of the segment in the input BFD. */
3666 m->next = NULL;
3667 m->p_type = p->p_type;
3668 m->p_flags = p->p_flags;
3669 m->p_flags_valid = 1;
3670 m->p_paddr = p->p_paddr;
3671 m->p_paddr_valid = 1;
3673 /* Determine if this segment contains the ELF file header
3674 and if it contains the program headers themselves. */
3675 m->includes_filehdr = (p->p_offset == 0
3676 && p->p_filesz >= iehdr->e_ehsize);
3678 m->includes_phdrs = 0;
3680 if (! phdr_included || p->p_type != PT_LOAD)
3682 m->includes_phdrs =
3683 (p->p_offset <= (bfd_vma) iehdr->e_phoff
3684 && (p->p_offset + p->p_filesz
3685 >= ((bfd_vma) iehdr->e_phoff
3686 + iehdr->e_phnum * iehdr->e_phentsize)));
3687 if (p->p_type == PT_LOAD && m->includes_phdrs)
3688 phdr_included = true;
3691 if (csecs == 0)
3693 /* Special segments, such as the PT_PHDR segment, may contain
3694 no sections, but ordinary, loadable segments should contain
3695 something. */
3697 if (p->p_type == PT_LOAD)
3698 _bfd_error_handler
3699 (_("%s: warning: Empty loadable segment detected\n"),
3700 bfd_get_filename (ibfd));
3702 m->count = 0;
3703 *pm = m;
3704 pm = &m->next;
3706 continue;
3709 /* Now scan the sections in the input BFD again and attempt
3710 to add their corresponding output sections to the segment map.
3711 The problem here is how to handle an output section which has
3712 been moved (ie had its LMA changed). There are four possibilities:
3714 1. None of the sections have been moved.
3715 In this case we can continue to use the segment LMA from the
3716 input BFD.
3718 2. All of the sections have been moved by the same amount.
3719 In this case we can change the segment's LMA to match the LMA
3720 of the first section.
3722 3. Some of the sections have been moved, others have not.
3723 In this case those sections which have not been moved can be
3724 placed in the current segment which will have to have its size,
3725 and possibly its LMA changed, and a new segment or segments will
3726 have to be created to contain the other sections.
3728 4. The sections have been moved, but not be the same amount.
3729 In this case we can change the segment's LMA to match the LMA
3730 of the first section and we will have to create a new segment
3731 or segments to contain the other sections.
3733 In order to save time, we allocate an array to hold the section
3734 pointers that we are interested in. As these sections get assigned
3735 to a segment, they are removed from this array. */
3737 sections = (asection **) bfd_malloc (sizeof (asection *) * csecs);
3738 if (sections == NULL)
3739 return false;
3741 /* Step One: Scan for segment vs section LMA conflicts.
3742 Also add the sections to the section array allocated above.
3743 Also add the sections to the current segment. In the common
3744 case, where the sections have not been moved, this means that
3745 we have completely filled the segment, and there is nothing
3746 more to do. */
3748 isec = 0;
3749 matching_lma = 0;
3750 suggested_lma = 0;
3752 for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
3754 os = s->output_section;
3756 if ((((IS_CONTAINED_BY (s->vma, s->_raw_size, p->p_vaddr, p)
3757 || IS_SOLARIS_PT_INTERP (p, s))
3758 && (s->flags & SEC_ALLOC) != 0)
3759 || IS_COREFILE_NOTE (p, s))
3760 && os != NULL)
3762 sections[j++] = s;
3764 /* The Solaris native linker always sets p_paddr to 0.
3765 We try to catch that case here, and set it to the
3766 correct value. */
3767 if (p->p_paddr == 0
3768 && p->p_vaddr != 0
3769 && isec == 0
3770 && os->lma != 0
3771 && (os->vma == (p->p_vaddr
3772 + (m->includes_filehdr
3773 ? iehdr->e_ehsize
3774 : 0)
3775 + (m->includes_phdrs
3776 ? iehdr->e_phnum * iehdr->e_phentsize
3777 : 0))))
3778 m->p_paddr = p->p_vaddr;
3780 /* Match up the physical address of the segment with the
3781 LMA address of the output section. */
3782 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3783 || IS_COREFILE_NOTE (p, s))
3785 if (matching_lma == 0)
3786 matching_lma = os->lma;
3788 /* We assume that if the section fits within the segment
3789 that it does not overlap any other section within that
3790 segment. */
3791 m->sections[isec++] = os;
3793 else if (suggested_lma == 0)
3794 suggested_lma = os->lma;
3798 BFD_ASSERT (j == csecs);
3800 /* Step Two: Adjust the physical address of the current segment,
3801 if necessary. */
3802 if (isec == csecs)
3804 /* All of the sections fitted within the segment as currently
3805 specified. This is the default case. Add the segment to
3806 the list of built segments and carry on to process the next
3807 program header in the input BFD. */
3808 m->count = csecs;
3809 *pm = m;
3810 pm = &m->next;
3812 free (sections);
3813 continue;
3815 else
3817 if (matching_lma != 0)
3819 /* At least one section fits inside the current segment.
3820 Keep it, but modify its physical address to match the
3821 LMA of the first section that fitted. */
3823 m->p_paddr = matching_lma;
3825 else
3827 /* None of the sections fitted inside the current segment.
3828 Change the current segment's physical address to match
3829 the LMA of the first section. */
3831 m->p_paddr = suggested_lma;
3834 /* Offset the segment physical address from the lma to allow
3835 for space taken up by elf headers. */
3836 if (m->includes_filehdr)
3837 m->p_paddr -= iehdr->e_ehsize;
3839 if (m->includes_phdrs)
3840 m->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
3843 /* Step Three: Loop over the sections again, this time assigning
3844 those that fit to the current segment and remvoing them from the
3845 sections array; but making sure not to leave large gaps. Once all
3846 possible sections have been assigned to the current segment it is
3847 added to the list of built segments and if sections still remain
3848 to be assigned, a new segment is constructed before repeating
3849 the loop. */
3850 isec = 0;
3853 m->count = 0;
3854 suggested_lma = 0;
3856 /* Fill the current segment with sections that fit. */
3857 for (j = 0; j < csecs; j++)
3859 s = sections[j];
3861 if (s == NULL)
3862 continue;
3864 os = s->output_section;
3866 if (IS_CONTAINED_BY (os->lma, os->_raw_size, m->p_paddr, p)
3867 || IS_COREFILE_NOTE (p, s))
3869 if (m->count == 0)
3871 /* If the first section in a segment does not start at
3872 the beginning of the segment, then something is wrong. */
3873 if (os->lma != (m->p_paddr
3874 + (m->includes_filehdr
3875 ? iehdr->e_ehsize : 0)
3876 + (m->includes_phdrs
3877 ? iehdr->e_phnum * iehdr->e_phentsize
3878 : 0)))
3879 abort ();
3881 else
3883 asection * prev_sec;
3884 bfd_vma maxpagesize;
3886 prev_sec = m->sections[m->count - 1];
3887 maxpagesize = get_elf_backend_data (obfd)->maxpagesize;
3889 /* If the gap between the end of the previous section
3890 and the start of this section is more than maxpagesize
3891 then we need to start a new segment. */
3892 if (BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, maxpagesize)
3893 < BFD_ALIGN (os->lma, maxpagesize))
3895 if (suggested_lma == 0)
3896 suggested_lma = os->lma;
3898 continue;
3902 m->sections[m->count++] = os;
3903 ++isec;
3904 sections[j] = NULL;
3906 else if (suggested_lma == 0)
3907 suggested_lma = os->lma;
3910 BFD_ASSERT (m->count > 0);
3912 /* Add the current segment to the list of built segments. */
3913 *pm = m;
3914 pm = &m->next;
3916 if (isec < csecs)
3918 /* We still have not allocated all of the sections to
3919 segments. Create a new segment here, initialise it
3920 and carry on looping. */
3922 m = ((struct elf_segment_map *)
3923 bfd_alloc (obfd,
3924 (sizeof (struct elf_segment_map)
3925 + ((size_t) csecs - 1) * sizeof (asection *))));
3926 if (m == NULL)
3927 return false;
3929 /* Initialise the fields of the segment map. Set the physical
3930 physical address to the LMA of the first section that has
3931 not yet been assigned. */
3933 m->next = NULL;
3934 m->p_type = p->p_type;
3935 m->p_flags = p->p_flags;
3936 m->p_flags_valid = 1;
3937 m->p_paddr = suggested_lma;
3938 m->p_paddr_valid = 1;
3939 m->includes_filehdr = 0;
3940 m->includes_phdrs = 0;
3943 while (isec < csecs);
3945 free (sections);
3948 /* The Solaris linker creates program headers in which all the
3949 p_paddr fields are zero. When we try to objcopy or strip such a
3950 file, we get confused. Check for this case, and if we find it
3951 reset the p_paddr_valid fields. */
3952 for (m = mfirst; m != NULL; m = m->next)
3953 if (m->p_paddr != 0)
3954 break;
3955 if (m == NULL)
3957 for (m = mfirst; m != NULL; m = m->next)
3958 m->p_paddr_valid = 0;
3961 elf_tdata (obfd)->segment_map = mfirst;
3963 #if 0
3964 /* Final Step: Sort the segments into ascending order of physical address. */
3965 if (mfirst != NULL)
3967 struct elf_segment_map* prev;
3969 prev = mfirst;
3970 for (m = mfirst->next; m != NULL; prev = m, m = m->next)
3972 /* Yes I know - its a bubble sort....*/
3973 if (m->next != NULL && (m->next->p_paddr < m->p_paddr))
3975 /* swap m and m->next */
3976 prev->next = m->next;
3977 m->next = m->next->next;
3978 prev->next->next = m;
3980 /* restart loop. */
3981 m = mfirst;
3985 #endif
3987 #undef IS_CONTAINED_BY
3988 #undef IS_SOLARIS_PT_INTERP
3989 #undef IS_COREFILE_NOTE
3990 return true;
3993 /* Copy private section information. This copies over the entsize
3994 field, and sometimes the info field. */
3996 boolean
3997 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
3998 bfd *ibfd;
3999 asection *isec;
4000 bfd *obfd;
4001 asection *osec;
4003 Elf_Internal_Shdr *ihdr, *ohdr;
4005 if (ibfd->xvec->flavour != bfd_target_elf_flavour
4006 || obfd->xvec->flavour != bfd_target_elf_flavour)
4007 return true;
4009 /* Copy over private BFD data if it has not already been copied.
4010 This must be done here, rather than in the copy_private_bfd_data
4011 entry point, because the latter is called after the section
4012 contents have been set, which means that the program headers have
4013 already been worked out. */
4014 if (elf_tdata (obfd)->segment_map == NULL
4015 && elf_tdata (ibfd)->phdr != NULL)
4017 asection *s;
4019 /* Only set up the segments if there are no more SEC_ALLOC
4020 sections. FIXME: This won't do the right thing if objcopy is
4021 used to remove the last SEC_ALLOC section, since objcopy
4022 won't call this routine in that case. */
4023 for (s = isec->next; s != NULL; s = s->next)
4024 if ((s->flags & SEC_ALLOC) != 0)
4025 break;
4026 if (s == NULL)
4028 if (! copy_private_bfd_data (ibfd, obfd))
4029 return false;
4033 ihdr = &elf_section_data (isec)->this_hdr;
4034 ohdr = &elf_section_data (osec)->this_hdr;
4036 ohdr->sh_entsize = ihdr->sh_entsize;
4038 if (ihdr->sh_type == SHT_SYMTAB
4039 || ihdr->sh_type == SHT_DYNSYM
4040 || ihdr->sh_type == SHT_GNU_verneed
4041 || ihdr->sh_type == SHT_GNU_verdef)
4042 ohdr->sh_info = ihdr->sh_info;
4044 elf_section_data (osec)->use_rela_p
4045 = elf_section_data (isec)->use_rela_p;
4047 return true;
4050 /* Copy private symbol information. If this symbol is in a section
4051 which we did not map into a BFD section, try to map the section
4052 index correctly. We use special macro definitions for the mapped
4053 section indices; these definitions are interpreted by the
4054 swap_out_syms function. */
4056 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
4057 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
4058 #define MAP_STRTAB (SHN_LORESERVE - 3)
4059 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
4061 boolean
4062 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
4063 bfd *ibfd;
4064 asymbol *isymarg;
4065 bfd *obfd;
4066 asymbol *osymarg;
4068 elf_symbol_type *isym, *osym;
4070 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4071 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4072 return true;
4074 isym = elf_symbol_from (ibfd, isymarg);
4075 osym = elf_symbol_from (obfd, osymarg);
4077 if (isym != NULL
4078 && osym != NULL
4079 && bfd_is_abs_section (isym->symbol.section))
4081 unsigned int shndx;
4083 shndx = isym->internal_elf_sym.st_shndx;
4084 if (shndx == elf_onesymtab (ibfd))
4085 shndx = MAP_ONESYMTAB;
4086 else if (shndx == elf_dynsymtab (ibfd))
4087 shndx = MAP_DYNSYMTAB;
4088 else if (shndx == elf_tdata (ibfd)->strtab_section)
4089 shndx = MAP_STRTAB;
4090 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
4091 shndx = MAP_SHSTRTAB;
4092 osym->internal_elf_sym.st_shndx = shndx;
4095 return true;
4098 /* Swap out the symbols. */
4100 static boolean
4101 swap_out_syms (abfd, sttp, relocatable_p)
4102 bfd *abfd;
4103 struct bfd_strtab_hash **sttp;
4104 int relocatable_p;
4106 struct elf_backend_data *bed = get_elf_backend_data (abfd);
4108 if (!elf_map_symbols (abfd))
4109 return false;
4111 /* Dump out the symtabs. */
4113 int symcount = bfd_get_symcount (abfd);
4114 asymbol **syms = bfd_get_outsymbols (abfd);
4115 struct bfd_strtab_hash *stt;
4116 Elf_Internal_Shdr *symtab_hdr;
4117 Elf_Internal_Shdr *symstrtab_hdr;
4118 char *outbound_syms;
4119 int idx;
4121 stt = _bfd_elf_stringtab_init ();
4122 if (stt == NULL)
4123 return false;
4125 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
4126 symtab_hdr->sh_type = SHT_SYMTAB;
4127 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
4128 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
4129 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
4130 symtab_hdr->sh_addralign = bed->s->file_align;
4132 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
4133 symstrtab_hdr->sh_type = SHT_STRTAB;
4135 outbound_syms = bfd_alloc (abfd,
4136 (1 + symcount) * bed->s->sizeof_sym);
4137 if (outbound_syms == NULL)
4138 return false;
4139 symtab_hdr->contents = (PTR) outbound_syms;
4141 /* now generate the data (for "contents") */
4143 /* Fill in zeroth symbol and swap it out. */
4144 Elf_Internal_Sym sym;
4145 sym.st_name = 0;
4146 sym.st_value = 0;
4147 sym.st_size = 0;
4148 sym.st_info = 0;
4149 sym.st_other = 0;
4150 sym.st_shndx = SHN_UNDEF;
4151 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4152 outbound_syms += bed->s->sizeof_sym;
4154 for (idx = 0; idx < symcount; idx++)
4156 Elf_Internal_Sym sym;
4157 bfd_vma value = syms[idx]->value;
4158 elf_symbol_type *type_ptr;
4159 flagword flags = syms[idx]->flags;
4160 int type;
4162 if (flags & BSF_SECTION_SYM)
4163 /* Section symbols have no names. */
4164 sym.st_name = 0;
4165 else
4167 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
4168 syms[idx]->name,
4169 true, false);
4170 if (sym.st_name == (unsigned long) -1)
4171 return false;
4174 type_ptr = elf_symbol_from (abfd, syms[idx]);
4176 if ((flags & BSF_SECTION_SYM) == 0
4177 && bfd_is_com_section (syms[idx]->section))
4179 /* ELF common symbols put the alignment into the `value' field,
4180 and the size into the `size' field. This is backwards from
4181 how BFD handles it, so reverse it here. */
4182 sym.st_size = value;
4183 if (type_ptr == NULL
4184 || type_ptr->internal_elf_sym.st_value == 0)
4185 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
4186 else
4187 sym.st_value = type_ptr->internal_elf_sym.st_value;
4188 sym.st_shndx = _bfd_elf_section_from_bfd_section
4189 (abfd, syms[idx]->section);
4191 else
4193 asection *sec = syms[idx]->section;
4194 int shndx;
4196 if (sec->output_section)
4198 value += sec->output_offset;
4199 sec = sec->output_section;
4201 /* Don't add in the section vma for relocatable output. */
4202 if (! relocatable_p)
4203 value += sec->vma;
4204 sym.st_value = value;
4205 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
4207 if (bfd_is_abs_section (sec)
4208 && type_ptr != NULL
4209 && type_ptr->internal_elf_sym.st_shndx != 0)
4211 /* This symbol is in a real ELF section which we did
4212 not create as a BFD section. Undo the mapping done
4213 by copy_private_symbol_data. */
4214 shndx = type_ptr->internal_elf_sym.st_shndx;
4215 switch (shndx)
4217 case MAP_ONESYMTAB:
4218 shndx = elf_onesymtab (abfd);
4219 break;
4220 case MAP_DYNSYMTAB:
4221 shndx = elf_dynsymtab (abfd);
4222 break;
4223 case MAP_STRTAB:
4224 shndx = elf_tdata (abfd)->strtab_section;
4225 break;
4226 case MAP_SHSTRTAB:
4227 shndx = elf_tdata (abfd)->shstrtab_section;
4228 break;
4229 default:
4230 break;
4233 else
4235 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
4237 if (shndx == -1)
4239 asection *sec2;
4241 /* Writing this would be a hell of a lot easier if
4242 we had some decent documentation on bfd, and
4243 knew what to expect of the library, and what to
4244 demand of applications. For example, it
4245 appears that `objcopy' might not set the
4246 section of a symbol to be a section that is
4247 actually in the output file. */
4248 sec2 = bfd_get_section_by_name (abfd, sec->name);
4249 BFD_ASSERT (sec2 != 0);
4250 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
4251 BFD_ASSERT (shndx != -1);
4255 sym.st_shndx = shndx;
4258 if ((flags & BSF_FUNCTION) != 0)
4259 type = STT_FUNC;
4260 else if ((flags & BSF_OBJECT) != 0)
4261 type = STT_OBJECT;
4262 else
4263 type = STT_NOTYPE;
4265 /* Processor-specific types */
4266 if (type_ptr != NULL
4267 && bed->elf_backend_get_symbol_type)
4268 type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
4270 if (flags & BSF_SECTION_SYM)
4271 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
4272 else if (bfd_is_com_section (syms[idx]->section))
4273 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
4274 else if (bfd_is_und_section (syms[idx]->section))
4275 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
4276 ? STB_WEAK
4277 : STB_GLOBAL),
4278 type);
4279 else if (flags & BSF_FILE)
4280 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
4281 else
4283 int bind = STB_LOCAL;
4285 if (flags & BSF_LOCAL)
4286 bind = STB_LOCAL;
4287 else if (flags & BSF_WEAK)
4288 bind = STB_WEAK;
4289 else if (flags & BSF_GLOBAL)
4290 bind = STB_GLOBAL;
4292 sym.st_info = ELF_ST_INFO (bind, type);
4295 if (type_ptr != NULL)
4296 sym.st_other = type_ptr->internal_elf_sym.st_other;
4297 else
4298 sym.st_other = 0;
4300 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
4301 outbound_syms += bed->s->sizeof_sym;
4304 *sttp = stt;
4305 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
4306 symstrtab_hdr->sh_type = SHT_STRTAB;
4308 symstrtab_hdr->sh_flags = 0;
4309 symstrtab_hdr->sh_addr = 0;
4310 symstrtab_hdr->sh_entsize = 0;
4311 symstrtab_hdr->sh_link = 0;
4312 symstrtab_hdr->sh_info = 0;
4313 symstrtab_hdr->sh_addralign = 1;
4316 return true;
4319 /* Return the number of bytes required to hold the symtab vector.
4321 Note that we base it on the count plus 1, since we will null terminate
4322 the vector allocated based on this size. However, the ELF symbol table
4323 always has a dummy entry as symbol #0, so it ends up even. */
4325 long
4326 _bfd_elf_get_symtab_upper_bound (abfd)
4327 bfd *abfd;
4329 long symcount;
4330 long symtab_size;
4331 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
4333 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4334 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4336 return symtab_size;
4339 long
4340 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
4341 bfd *abfd;
4343 long symcount;
4344 long symtab_size;
4345 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
4347 if (elf_dynsymtab (abfd) == 0)
4349 bfd_set_error (bfd_error_invalid_operation);
4350 return -1;
4353 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
4354 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
4356 return symtab_size;
4359 long
4360 _bfd_elf_get_reloc_upper_bound (abfd, asect)
4361 bfd *abfd ATTRIBUTE_UNUSED;
4362 sec_ptr asect;
4364 return (asect->reloc_count + 1) * sizeof (arelent *);
4367 /* Canonicalize the relocs. */
4369 long
4370 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
4371 bfd *abfd;
4372 sec_ptr section;
4373 arelent **relptr;
4374 asymbol **symbols;
4376 arelent *tblptr;
4377 unsigned int i;
4379 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd,
4380 section,
4381 symbols,
4382 false))
4383 return -1;
4385 tblptr = section->relocation;
4386 for (i = 0; i < section->reloc_count; i++)
4387 *relptr++ = tblptr++;
4389 *relptr = NULL;
4391 return section->reloc_count;
4394 long
4395 _bfd_elf_get_symtab (abfd, alocation)
4396 bfd *abfd;
4397 asymbol **alocation;
4399 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table
4400 (abfd, alocation, false);
4402 if (symcount >= 0)
4403 bfd_get_symcount (abfd) = symcount;
4404 return symcount;
4407 long
4408 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
4409 bfd *abfd;
4410 asymbol **alocation;
4412 return get_elf_backend_data (abfd)->s->slurp_symbol_table
4413 (abfd, alocation, true);
4416 /* Return the size required for the dynamic reloc entries. Any
4417 section that was actually installed in the BFD, and has type
4418 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is
4419 considered to be a dynamic reloc section. */
4421 long
4422 _bfd_elf_get_dynamic_reloc_upper_bound (abfd)
4423 bfd *abfd;
4425 long ret;
4426 asection *s;
4428 if (elf_dynsymtab (abfd) == 0)
4430 bfd_set_error (bfd_error_invalid_operation);
4431 return -1;
4434 ret = sizeof (arelent *);
4435 for (s = abfd->sections; s != NULL; s = s->next)
4436 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4437 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4438 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4439 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize)
4440 * sizeof (arelent *));
4442 return ret;
4445 /* Canonicalize the dynamic relocation entries. Note that we return
4446 the dynamic relocations as a single block, although they are
4447 actually associated with particular sections; the interface, which
4448 was designed for SunOS style shared libraries, expects that there
4449 is only one set of dynamic relocs. Any section that was actually
4450 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
4451 the dynamic symbol table, is considered to be a dynamic reloc
4452 section. */
4454 long
4455 _bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms)
4456 bfd *abfd;
4457 arelent **storage;
4458 asymbol **syms;
4460 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean));
4461 asection *s;
4462 long ret;
4464 if (elf_dynsymtab (abfd) == 0)
4466 bfd_set_error (bfd_error_invalid_operation);
4467 return -1;
4470 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
4471 ret = 0;
4472 for (s = abfd->sections; s != NULL; s = s->next)
4474 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
4475 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL
4476 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
4478 arelent *p;
4479 long count, i;
4481 if (! (*slurp_relocs) (abfd, s, syms, true))
4482 return -1;
4483 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize;
4484 p = s->relocation;
4485 for (i = 0; i < count; i++)
4486 *storage++ = p++;
4487 ret += count;
4491 *storage = NULL;
4493 return ret;
4496 /* Read in the version information. */
4498 boolean
4499 _bfd_elf_slurp_version_tables (abfd)
4500 bfd *abfd;
4502 bfd_byte *contents = NULL;
4504 if (elf_dynverdef (abfd) != 0)
4506 Elf_Internal_Shdr *hdr;
4507 Elf_External_Verdef *everdef;
4508 Elf_Internal_Verdef *iverdef;
4509 unsigned int i;
4511 hdr = &elf_tdata (abfd)->dynverdef_hdr;
4513 elf_tdata (abfd)->verdef =
4514 ((Elf_Internal_Verdef *)
4515 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
4516 if (elf_tdata (abfd)->verdef == NULL)
4517 goto error_return;
4519 elf_tdata (abfd)->cverdefs = hdr->sh_info;
4521 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4522 if (contents == NULL)
4523 goto error_return;
4524 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4525 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4526 goto error_return;
4528 everdef = (Elf_External_Verdef *) contents;
4529 iverdef = elf_tdata (abfd)->verdef;
4530 for (i = 0; i < hdr->sh_info; i++, iverdef++)
4532 Elf_External_Verdaux *everdaux;
4533 Elf_Internal_Verdaux *iverdaux;
4534 unsigned int j;
4536 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
4538 iverdef->vd_bfd = abfd;
4540 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *)
4541 bfd_alloc (abfd,
4542 (iverdef->vd_cnt
4543 * sizeof (Elf_Internal_Verdaux))));
4544 if (iverdef->vd_auxptr == NULL)
4545 goto error_return;
4547 everdaux = ((Elf_External_Verdaux *)
4548 ((bfd_byte *) everdef + iverdef->vd_aux));
4549 iverdaux = iverdef->vd_auxptr;
4550 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++)
4552 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux);
4554 iverdaux->vda_nodename =
4555 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4556 iverdaux->vda_name);
4557 if (iverdaux->vda_nodename == NULL)
4558 goto error_return;
4560 if (j + 1 < iverdef->vd_cnt)
4561 iverdaux->vda_nextptr = iverdaux + 1;
4562 else
4563 iverdaux->vda_nextptr = NULL;
4565 everdaux = ((Elf_External_Verdaux *)
4566 ((bfd_byte *) everdaux + iverdaux->vda_next));
4569 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename;
4571 if (i + 1 < hdr->sh_info)
4572 iverdef->vd_nextdef = iverdef + 1;
4573 else
4574 iverdef->vd_nextdef = NULL;
4576 everdef = ((Elf_External_Verdef *)
4577 ((bfd_byte *) everdef + iverdef->vd_next));
4580 free (contents);
4581 contents = NULL;
4584 if (elf_dynverref (abfd) != 0)
4586 Elf_Internal_Shdr *hdr;
4587 Elf_External_Verneed *everneed;
4588 Elf_Internal_Verneed *iverneed;
4589 unsigned int i;
4591 hdr = &elf_tdata (abfd)->dynverref_hdr;
4593 elf_tdata (abfd)->verref =
4594 ((Elf_Internal_Verneed *)
4595 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed)));
4596 if (elf_tdata (abfd)->verref == NULL)
4597 goto error_return;
4599 elf_tdata (abfd)->cverrefs = hdr->sh_info;
4601 contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
4602 if (contents == NULL)
4603 goto error_return;
4604 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
4605 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
4606 goto error_return;
4608 everneed = (Elf_External_Verneed *) contents;
4609 iverneed = elf_tdata (abfd)->verref;
4610 for (i = 0; i < hdr->sh_info; i++, iverneed++)
4612 Elf_External_Vernaux *evernaux;
4613 Elf_Internal_Vernaux *ivernaux;
4614 unsigned int j;
4616 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed);
4618 iverneed->vn_bfd = abfd;
4620 iverneed->vn_filename =
4621 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4622 iverneed->vn_file);
4623 if (iverneed->vn_filename == NULL)
4624 goto error_return;
4626 iverneed->vn_auxptr =
4627 ((Elf_Internal_Vernaux *)
4628 bfd_alloc (abfd,
4629 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux)));
4631 evernaux = ((Elf_External_Vernaux *)
4632 ((bfd_byte *) everneed + iverneed->vn_aux));
4633 ivernaux = iverneed->vn_auxptr;
4634 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++)
4636 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux);
4638 ivernaux->vna_nodename =
4639 bfd_elf_string_from_elf_section (abfd, hdr->sh_link,
4640 ivernaux->vna_name);
4641 if (ivernaux->vna_nodename == NULL)
4642 goto error_return;
4644 if (j + 1 < iverneed->vn_cnt)
4645 ivernaux->vna_nextptr = ivernaux + 1;
4646 else
4647 ivernaux->vna_nextptr = NULL;
4649 evernaux = ((Elf_External_Vernaux *)
4650 ((bfd_byte *) evernaux + ivernaux->vna_next));
4653 if (i + 1 < hdr->sh_info)
4654 iverneed->vn_nextref = iverneed + 1;
4655 else
4656 iverneed->vn_nextref = NULL;
4658 everneed = ((Elf_External_Verneed *)
4659 ((bfd_byte *) everneed + iverneed->vn_next));
4662 free (contents);
4663 contents = NULL;
4666 return true;
4668 error_return:
4669 if (contents == NULL)
4670 free (contents);
4671 return false;
4674 asymbol *
4675 _bfd_elf_make_empty_symbol (abfd)
4676 bfd *abfd;
4678 elf_symbol_type *newsym;
4680 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
4681 if (!newsym)
4682 return NULL;
4683 else
4685 newsym->symbol.the_bfd = abfd;
4686 return &newsym->symbol;
4690 void
4691 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
4692 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4693 asymbol *symbol;
4694 symbol_info *ret;
4696 bfd_symbol_info (symbol, ret);
4699 /* Return whether a symbol name implies a local symbol. Most targets
4700 use this function for the is_local_label_name entry point, but some
4701 override it. */
4703 boolean
4704 _bfd_elf_is_local_label_name (abfd, name)
4705 bfd *abfd ATTRIBUTE_UNUSED;
4706 const char *name;
4708 /* Normal local symbols start with ``.L''. */
4709 if (name[0] == '.' && name[1] == 'L')
4710 return true;
4712 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate
4713 DWARF debugging symbols starting with ``..''. */
4714 if (name[0] == '.' && name[1] == '.')
4715 return true;
4717 /* gcc will sometimes generate symbols beginning with ``_.L_'' when
4718 emitting DWARF debugging output. I suspect this is actually a
4719 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call
4720 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading
4721 underscore to be emitted on some ELF targets). For ease of use,
4722 we treat such symbols as local. */
4723 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
4724 return true;
4726 return false;
4729 alent *
4730 _bfd_elf_get_lineno (ignore_abfd, symbol)
4731 bfd *ignore_abfd ATTRIBUTE_UNUSED;
4732 asymbol *symbol ATTRIBUTE_UNUSED;
4734 abort ();
4735 return NULL;
4738 boolean
4739 _bfd_elf_set_arch_mach (abfd, arch, machine)
4740 bfd *abfd;
4741 enum bfd_architecture arch;
4742 unsigned long machine;
4744 /* If this isn't the right architecture for this backend, and this
4745 isn't the generic backend, fail. */
4746 if (arch != get_elf_backend_data (abfd)->arch
4747 && arch != bfd_arch_unknown
4748 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
4749 return false;
4751 return bfd_default_set_arch_mach (abfd, arch, machine);
4754 /* Find the nearest line to a particular section and offset, for error
4755 reporting. */
4757 boolean
4758 _bfd_elf_find_nearest_line (abfd,
4759 section,
4760 symbols,
4761 offset,
4762 filename_ptr,
4763 functionname_ptr,
4764 line_ptr)
4765 bfd *abfd;
4766 asection *section;
4767 asymbol **symbols;
4768 bfd_vma offset;
4769 CONST char **filename_ptr;
4770 CONST char **functionname_ptr;
4771 unsigned int *line_ptr;
4773 boolean found;
4774 const char *filename;
4775 asymbol *func;
4776 bfd_vma low_func;
4777 asymbol **p;
4779 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset,
4780 filename_ptr, functionname_ptr,
4781 line_ptr))
4782 return true;
4784 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
4785 filename_ptr, functionname_ptr,
4786 line_ptr, 0))
4787 return true;
4789 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
4790 &found, filename_ptr,
4791 functionname_ptr, line_ptr,
4792 &elf_tdata (abfd)->line_info))
4793 return false;
4794 if (found)
4795 return true;
4797 if (symbols == NULL)
4798 return false;
4800 filename = NULL;
4801 func = NULL;
4802 low_func = 0;
4804 for (p = symbols; *p != NULL; p++)
4806 elf_symbol_type *q;
4808 q = (elf_symbol_type *) *p;
4810 if (bfd_get_section (&q->symbol) != section)
4811 continue;
4813 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
4815 default:
4816 break;
4817 case STT_FILE:
4818 filename = bfd_asymbol_name (&q->symbol);
4819 break;
4820 case STT_NOTYPE:
4821 case STT_FUNC:
4822 if (q->symbol.section == section
4823 && q->symbol.value >= low_func
4824 && q->symbol.value <= offset)
4826 func = (asymbol *) q;
4827 low_func = q->symbol.value;
4829 break;
4833 if (func == NULL)
4834 return false;
4836 *filename_ptr = filename;
4837 *functionname_ptr = bfd_asymbol_name (func);
4838 *line_ptr = 0;
4839 return true;
4843 _bfd_elf_sizeof_headers (abfd, reloc)
4844 bfd *abfd;
4845 boolean reloc;
4847 int ret;
4849 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
4850 if (! reloc)
4851 ret += get_program_header_size (abfd);
4852 return ret;
4855 boolean
4856 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
4857 bfd *abfd;
4858 sec_ptr section;
4859 PTR location;
4860 file_ptr offset;
4861 bfd_size_type count;
4863 Elf_Internal_Shdr *hdr;
4865 if (! abfd->output_has_begun
4866 && ! _bfd_elf_compute_section_file_positions
4867 (abfd, (struct bfd_link_info *) NULL))
4868 return false;
4870 hdr = &elf_section_data (section)->this_hdr;
4872 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
4873 return false;
4874 if (bfd_write (location, 1, count, abfd) != count)
4875 return false;
4877 return true;
4880 void
4881 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
4882 bfd *abfd ATTRIBUTE_UNUSED;
4883 arelent *cache_ptr ATTRIBUTE_UNUSED;
4884 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED;
4886 abort ();
4889 #if 0
4890 void
4891 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
4892 bfd *abfd;
4893 arelent *cache_ptr;
4894 Elf_Internal_Rel *dst;
4896 abort ();
4898 #endif
4900 /* Try to convert a non-ELF reloc into an ELF one. */
4902 boolean
4903 _bfd_elf_validate_reloc (abfd, areloc)
4904 bfd *abfd;
4905 arelent *areloc;
4907 /* Check whether we really have an ELF howto. */
4909 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
4911 bfd_reloc_code_real_type code;
4912 reloc_howto_type *howto;
4914 /* Alien reloc: Try to determine its type to replace it with an
4915 equivalent ELF reloc. */
4917 if (areloc->howto->pc_relative)
4919 switch (areloc->howto->bitsize)
4921 case 8:
4922 code = BFD_RELOC_8_PCREL;
4923 break;
4924 case 12:
4925 code = BFD_RELOC_12_PCREL;
4926 break;
4927 case 16:
4928 code = BFD_RELOC_16_PCREL;
4929 break;
4930 case 24:
4931 code = BFD_RELOC_24_PCREL;
4932 break;
4933 case 32:
4934 code = BFD_RELOC_32_PCREL;
4935 break;
4936 case 64:
4937 code = BFD_RELOC_64_PCREL;
4938 break;
4939 default:
4940 goto fail;
4943 howto = bfd_reloc_type_lookup (abfd, code);
4945 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
4947 if (howto->pcrel_offset)
4948 areloc->addend += areloc->address;
4949 else
4950 areloc->addend -= areloc->address; /* addend is unsigned!! */
4953 else
4955 switch (areloc->howto->bitsize)
4957 case 8:
4958 code = BFD_RELOC_8;
4959 break;
4960 case 14:
4961 code = BFD_RELOC_14;
4962 break;
4963 case 16:
4964 code = BFD_RELOC_16;
4965 break;
4966 case 26:
4967 code = BFD_RELOC_26;
4968 break;
4969 case 32:
4970 code = BFD_RELOC_32;
4971 break;
4972 case 64:
4973 code = BFD_RELOC_64;
4974 break;
4975 default:
4976 goto fail;
4979 howto = bfd_reloc_type_lookup (abfd, code);
4982 if (howto)
4983 areloc->howto = howto;
4984 else
4985 goto fail;
4988 return true;
4990 fail:
4991 (*_bfd_error_handler)
4992 (_("%s: unsupported relocation type %s"),
4993 bfd_get_filename (abfd), areloc->howto->name);
4994 bfd_set_error (bfd_error_bad_value);
4995 return false;
4998 boolean
4999 _bfd_elf_close_and_cleanup (abfd)
5000 bfd *abfd;
5002 if (bfd_get_format (abfd) == bfd_object)
5004 if (elf_shstrtab (abfd) != NULL)
5005 _bfd_stringtab_free (elf_shstrtab (abfd));
5008 return _bfd_generic_close_and_cleanup (abfd);
5011 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY
5012 in the relocation's offset. Thus we cannot allow any sort of sanity
5013 range-checking to interfere. There is nothing else to do in processing
5014 this reloc. */
5016 bfd_reloc_status_type
5017 _bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg)
5018 bfd *abfd ATTRIBUTE_UNUSED;
5019 arelent *re ATTRIBUTE_UNUSED;
5020 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED;
5021 PTR data ATTRIBUTE_UNUSED;
5022 asection *is ATTRIBUTE_UNUSED;
5023 bfd *obfd ATTRIBUTE_UNUSED;
5024 char **errmsg ATTRIBUTE_UNUSED;
5026 return bfd_reloc_ok;
5030 /* Elf core file support. Much of this only works on native
5031 toolchains, since we rely on knowing the
5032 machine-dependent procfs structure in order to pick
5033 out details about the corefile. */
5035 #ifdef HAVE_SYS_PROCFS_H
5036 # include <sys/procfs.h>
5037 #endif
5040 /* Define offsetof for those systems which lack it. */
5042 #ifndef offsetof
5043 # define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
5044 #endif
5047 /* FIXME: this is kinda wrong, but it's what gdb wants. */
5049 static int
5050 elfcore_make_pid (abfd)
5051 bfd* abfd;
5053 return ((elf_tdata (abfd)->core_lwpid << 16)
5054 + (elf_tdata (abfd)->core_pid));
5058 /* If there isn't a section called NAME, make one, using
5059 data from SECT. Note, this function will generate a
5060 reference to NAME, so you shouldn't deallocate or
5061 overwrite it. */
5063 static boolean
5064 elfcore_maybe_make_sect (abfd, name, sect)
5065 bfd* abfd;
5066 char* name;
5067 asection* sect;
5069 asection* sect2;
5071 if (bfd_get_section_by_name (abfd, name) != NULL)
5072 return true;
5074 sect2 = bfd_make_section (abfd, name);
5075 if (sect2 == NULL)
5076 return false;
5078 sect2->_raw_size = sect->_raw_size;
5079 sect2->filepos = sect->filepos;
5080 sect2->flags = sect->flags;
5081 sect2->alignment_power = sect->alignment_power;
5082 return true;
5086 /* prstatus_t exists on:
5087 solaris 2.[567]
5088 linux 2.[01] + glibc
5089 unixware 4.2
5092 #if defined (HAVE_PRSTATUS_T)
5093 static boolean
5094 elfcore_grok_prstatus (abfd, note)
5095 bfd* abfd;
5096 Elf_Internal_Note* note;
5098 prstatus_t prstat;
5099 char buf[100];
5100 char* name;
5101 asection* sect;
5103 if (note->descsz != sizeof (prstat))
5104 return true;
5106 memcpy (&prstat, note->descdata, sizeof (prstat));
5108 elf_tdata (abfd)->core_signal = prstat.pr_cursig;
5109 elf_tdata (abfd)->core_pid = prstat.pr_pid;
5111 /* pr_who exists on:
5112 solaris 2.[567]
5113 unixware 4.2
5114 pr_who doesn't exist on:
5115 linux 2.[01]
5117 #if defined (HAVE_PRSTATUS_T_PR_WHO)
5118 elf_tdata (abfd)->core_lwpid = prstat.pr_who;
5119 #endif
5121 /* Make a ".reg/999" section. */
5123 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5124 name = bfd_alloc (abfd, strlen (buf) + 1);
5125 if (name == NULL)
5126 return false;
5127 strcpy (name, buf);
5129 sect = bfd_make_section (abfd, name);
5130 if (sect == NULL)
5131 return false;
5132 sect->_raw_size = sizeof (prstat.pr_reg);
5133 sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
5134 sect->flags = SEC_HAS_CONTENTS;
5135 sect->alignment_power = 2;
5137 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5138 return false;
5140 return true;
5142 #endif /* defined (HAVE_PRSTATUS_T) */
5145 /* Create a pseudosection containing the exact contents of NOTE. This
5146 actually creates up to two pseudosections:
5147 - For the single-threaded case, a section named NAME, unless
5148 such a section already exists.
5149 - For the multi-threaded case, a section named "NAME/PID", where
5150 PID is elfcore_make_pid (abfd).
5151 Both pseudosections have identical contents: the contents of NOTE. */
5153 static boolean
5154 elfcore_make_note_pseudosection (abfd, name, note)
5155 bfd* abfd;
5156 char *name;
5157 Elf_Internal_Note* note;
5159 char buf[100];
5160 char *threaded_name;
5161 asection* sect;
5163 /* Build the section name. */
5165 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd));
5166 threaded_name = bfd_alloc (abfd, strlen (buf) + 1);
5167 if (threaded_name == NULL)
5168 return false;
5169 strcpy (threaded_name, buf);
5171 sect = bfd_make_section (abfd, threaded_name);
5172 if (sect == NULL)
5173 return false;
5174 sect->_raw_size = note->descsz;
5175 sect->filepos = note->descpos;
5176 sect->flags = SEC_HAS_CONTENTS;
5177 sect->alignment_power = 2;
5179 if (! elfcore_maybe_make_sect (abfd, name, sect))
5180 return false;
5182 return true;
5186 /* There isn't a consistent prfpregset_t across platforms,
5187 but it doesn't matter, because we don't have to pick this
5188 data structure apart. */
5189 static boolean
5190 elfcore_grok_prfpreg (abfd, note)
5191 bfd* abfd;
5192 Elf_Internal_Note* note;
5194 return elfcore_make_note_pseudosection (abfd, ".reg2", note);
5198 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note
5199 type of 5 (NT_PRXFPREG). Just include the whole note's contents
5200 literally. */
5201 static boolean
5202 elfcore_grok_prxfpreg (abfd, note)
5203 bfd* abfd;
5204 Elf_Internal_Note* note;
5206 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note);
5210 #if defined (HAVE_PRPSINFO_T)
5211 # define elfcore_psinfo_t prpsinfo_t
5212 #endif
5214 #if defined (HAVE_PSINFO_T)
5215 # define elfcore_psinfo_t psinfo_t
5216 #endif
5219 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5221 /* return a malloc'ed copy of a string at START which is at
5222 most MAX bytes long, possibly without a terminating '\0'.
5223 the copy will always have a terminating '\0'. */
5225 static char*
5226 elfcore_strndup (abfd, start, max)
5227 bfd* abfd;
5228 char* start;
5229 int max;
5231 char* dup;
5232 char* end = memchr (start, '\0', max);
5233 int len;
5235 if (end == NULL)
5236 len = max;
5237 else
5238 len = end - start;
5240 dup = bfd_alloc (abfd, len + 1);
5241 if (dup == NULL)
5242 return NULL;
5244 memcpy (dup, start, len);
5245 dup[len] = '\0';
5247 return dup;
5250 static boolean
5251 elfcore_grok_psinfo (abfd, note)
5252 bfd* abfd;
5253 Elf_Internal_Note* note;
5255 elfcore_psinfo_t psinfo;
5257 if (note->descsz != sizeof (elfcore_psinfo_t))
5258 return true;
5260 memcpy (&psinfo, note->descdata, note->descsz);
5262 elf_tdata (abfd)->core_program
5263 = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
5265 elf_tdata (abfd)->core_command
5266 = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
5268 /* Note that for some reason, a spurious space is tacked
5269 onto the end of the args in some (at least one anyway)
5270 implementations, so strip it off if it exists. */
5273 char* command = elf_tdata (abfd)->core_command;
5274 int n = strlen (command);
5276 if (0 < n && command[n - 1] == ' ')
5277 command[n - 1] = '\0';
5280 return true;
5282 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */
5285 #if defined (HAVE_PSTATUS_T)
5286 static boolean
5287 elfcore_grok_pstatus (abfd, note)
5288 bfd* abfd;
5289 Elf_Internal_Note* note;
5291 pstatus_t pstat;
5293 if (note->descsz != sizeof (pstat))
5294 return true;
5296 memcpy (&pstat, note->descdata, sizeof (pstat));
5298 elf_tdata (abfd)->core_pid = pstat.pr_pid;
5300 /* Could grab some more details from the "representative"
5301 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
5302 NT_LWPSTATUS note, presumably. */
5304 return true;
5306 #endif /* defined (HAVE_PSTATUS_T) */
5309 #if defined (HAVE_LWPSTATUS_T)
5310 static boolean
5311 elfcore_grok_lwpstatus (abfd, note)
5312 bfd* abfd;
5313 Elf_Internal_Note* note;
5315 lwpstatus_t lwpstat;
5316 char buf[100];
5317 char* name;
5318 asection* sect;
5320 if (note->descsz != sizeof (lwpstat))
5321 return true;
5323 memcpy (&lwpstat, note->descdata, sizeof (lwpstat));
5325 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid;
5326 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig;
5328 /* Make a ".reg/999" section. */
5330 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd));
5331 name = bfd_alloc (abfd, strlen (buf) + 1);
5332 if (name == NULL)
5333 return false;
5334 strcpy (name, buf);
5336 sect = bfd_make_section (abfd, name);
5337 if (sect == NULL)
5338 return false;
5340 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5341 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs);
5342 sect->filepos = note->descpos
5343 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs);
5344 #endif
5346 #if defined (HAVE_LWPSTATUS_T_PR_REG)
5347 sect->_raw_size = sizeof (lwpstat.pr_reg);
5348 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg);
5349 #endif
5351 sect->flags = SEC_HAS_CONTENTS;
5352 sect->alignment_power = 2;
5354 if (!elfcore_maybe_make_sect (abfd, ".reg", sect))
5355 return false;
5357 /* Make a ".reg2/999" section */
5359 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd));
5360 name = bfd_alloc (abfd, strlen (buf) + 1);
5361 if (name == NULL)
5362 return false;
5363 strcpy (name, buf);
5365 sect = bfd_make_section (abfd, name);
5366 if (sect == NULL)
5367 return false;
5369 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT)
5370 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs);
5371 sect->filepos = note->descpos
5372 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs);
5373 #endif
5375 #if defined (HAVE_LWPSTATUS_T_PR_FPREG)
5376 sect->_raw_size = sizeof (lwpstat.pr_fpreg);
5377 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg);
5378 #endif
5380 sect->flags = SEC_HAS_CONTENTS;
5381 sect->alignment_power = 2;
5383 if (!elfcore_maybe_make_sect (abfd, ".reg2", sect))
5384 return false;
5386 return true;
5388 #endif /* defined (HAVE_LWPSTATUS_T) */
5390 #if defined (HAVE_WIN32_PSTATUS_T)
5391 static boolean
5392 elfcore_grok_win32pstatus (abfd, note)
5393 bfd * abfd;
5394 Elf_Internal_Note * note;
5396 char buf[30];
5397 char * name;
5398 asection * sect;
5399 win32_pstatus_t pstatus;
5401 if (note->descsz < sizeof (pstatus))
5402 return true;
5404 memcpy (& pstatus, note->descdata, note->descsz);
5406 switch (pstatus.data_type)
5408 case NOTE_INFO_PROCESS:
5409 /* FIXME: need to add ->core_command. */
5410 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal;
5411 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid;
5412 break ;
5414 case NOTE_INFO_THREAD:
5415 /* Make a ".reg/999" section. */
5416 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid);
5418 name = bfd_alloc (abfd, strlen (buf) + 1);
5419 if (name == NULL)
5420 return false;
5422 strcpy (name, buf);
5424 sect = bfd_make_section (abfd, name);
5425 if (sect == NULL)
5426 return false;
5428 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context);
5429 sect->filepos = note->descpos + offsetof (struct win32_pstatus,
5430 data.thread_info.thread_context);
5431 sect->flags = SEC_HAS_CONTENTS;
5432 sect->alignment_power = 2;
5434 if (pstatus.data.thread_info.is_active_thread)
5435 if (! elfcore_maybe_make_sect (abfd, ".reg", sect))
5436 return false;
5437 break;
5439 case NOTE_INFO_MODULE:
5440 /* Make a ".module/xxxxxxxx" section. */
5441 sprintf (buf, ".module/%08x" , pstatus.data.module_info.base_address);
5443 name = bfd_alloc (abfd, strlen (buf) + 1);
5444 if (name == NULL)
5445 return false;
5447 strcpy (name, buf);
5449 sect = bfd_make_section (abfd, name);
5451 if (sect == NULL)
5452 return false;
5454 sect->_raw_size = note->descsz;
5455 sect->filepos = note->descpos;
5456 sect->flags = SEC_HAS_CONTENTS;
5457 sect->alignment_power = 2;
5458 break;
5460 default:
5461 return true;
5464 return true;
5466 #endif /* HAVE_WIN32_PSTATUS_T */
5468 static boolean
5469 elfcore_grok_note (abfd, note)
5470 bfd* abfd;
5471 Elf_Internal_Note* note;
5473 switch (note->type)
5475 default:
5476 return true;
5478 #if defined (HAVE_PRSTATUS_T)
5479 case NT_PRSTATUS:
5480 return elfcore_grok_prstatus (abfd, note);
5481 #endif
5483 #if defined (HAVE_PSTATUS_T)
5484 case NT_PSTATUS:
5485 return elfcore_grok_pstatus (abfd, note);
5486 #endif
5488 #if defined (HAVE_LWPSTATUS_T)
5489 case NT_LWPSTATUS:
5490 return elfcore_grok_lwpstatus (abfd, note);
5491 #endif
5493 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */
5494 return elfcore_grok_prfpreg (abfd, note);
5496 #if defined (HAVE_WIN32_PSTATUS_T)
5497 case NT_WIN32PSTATUS:
5498 return elfcore_grok_win32pstatus (abfd, note);
5499 #endif
5501 case NT_PRXFPREG: /* Linux SSE extension */
5502 if (note->namesz == 5
5503 && ! strcmp (note->namedata, "LINUX"))
5504 return elfcore_grok_prxfpreg (abfd, note);
5505 else
5506 return true;
5508 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T)
5509 case NT_PRPSINFO:
5510 case NT_PSINFO:
5511 return elfcore_grok_psinfo (abfd, note);
5512 #endif
5517 static boolean
5518 elfcore_read_notes (abfd, offset, size)
5519 bfd* abfd;
5520 bfd_vma offset;
5521 bfd_vma size;
5523 char* buf;
5524 char* p;
5526 if (size <= 0)
5527 return true;
5529 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
5530 return false;
5532 buf = bfd_malloc ((size_t) size);
5533 if (buf == NULL)
5534 return false;
5536 if (bfd_read (buf, size, 1, abfd) != size)
5538 error:
5539 free (buf);
5540 return false;
5543 p = buf;
5544 while (p < buf + size)
5546 /* FIXME: bad alignment assumption. */
5547 Elf_External_Note* xnp = (Elf_External_Note*) p;
5548 Elf_Internal_Note in;
5550 in.type = bfd_h_get_32 (abfd, (bfd_byte *) xnp->type);
5552 in.namesz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->namesz);
5553 in.namedata = xnp->name;
5555 in.descsz = bfd_h_get_32 (abfd, (bfd_byte *) xnp->descsz);
5556 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4);
5557 in.descpos = offset + (in.descdata - buf);
5559 if (! elfcore_grok_note (abfd, &in))
5560 goto error;
5562 p = in.descdata + BFD_ALIGN (in.descsz, 4);
5565 free (buf);
5566 return true;
5570 /* FIXME: This function is now unnecessary. Callers can just call
5571 bfd_section_from_phdr directly. */
5573 boolean
5574 _bfd_elfcore_section_from_phdr (abfd, phdr, sec_num)
5575 bfd* abfd;
5576 Elf_Internal_Phdr* phdr;
5577 int sec_num;
5579 if (! bfd_section_from_phdr (abfd, phdr, sec_num))
5580 return false;
5582 return true;
5587 /* Providing external access to the ELF program header table. */
5589 /* Return an upper bound on the number of bytes required to store a
5590 copy of ABFD's program header table entries. Return -1 if an error
5591 occurs; bfd_get_error will return an appropriate code. */
5592 long
5593 bfd_get_elf_phdr_upper_bound (abfd)
5594 bfd *abfd;
5596 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5598 bfd_set_error (bfd_error_wrong_format);
5599 return -1;
5602 return (elf_elfheader (abfd)->e_phnum
5603 * sizeof (Elf_Internal_Phdr));
5607 /* Copy ABFD's program header table entries to *PHDRS. The entries
5608 will be stored as an array of Elf_Internal_Phdr structures, as
5609 defined in include/elf/internal.h. To find out how large the
5610 buffer needs to be, call bfd_get_elf_phdr_upper_bound.
5612 Return the number of program header table entries read, or -1 if an
5613 error occurs; bfd_get_error will return an appropriate code. */
5615 bfd_get_elf_phdrs (abfd, phdrs)
5616 bfd *abfd;
5617 void *phdrs;
5619 int num_phdrs;
5621 if (abfd->xvec->flavour != bfd_target_elf_flavour)
5623 bfd_set_error (bfd_error_wrong_format);
5624 return -1;
5627 num_phdrs = elf_elfheader (abfd)->e_phnum;
5628 memcpy (phdrs, elf_tdata (abfd)->phdr,
5629 num_phdrs * sizeof (Elf_Internal_Phdr));
5631 return num_phdrs;