1 /* ELF executable support for BFD.
2 Copyright 1993, 1994, 1995, 1996 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. */
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
41 static INLINE struct elf_segment_map *make_mapping
42 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean));
43 static int elf_sort_sections PARAMS ((const PTR, const PTR));
44 static boolean assign_file_positions_for_segments PARAMS ((bfd *));
45 static boolean assign_file_positions_except_relocs PARAMS ((bfd *));
46 static boolean prep_headers PARAMS ((bfd *));
47 static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **));
48 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
50 /* Standard ELF hash function. Do not change this function; you will
51 cause invalid hash tables to be generated. (Well, you would if this
52 were being used yet.) */
55 CONST unsigned char *name;
61 while ((ch = *name++) != '\0')
64 if ((g = (h & 0xf0000000)) != 0)
73 /* Read a specified number of bytes at a specified offset in an ELF
74 file, into a newly allocated buffer, and return a pointer to the
78 elf_read (abfd, offset, size)
85 if ((buf = bfd_alloc (abfd, size)) == NULL)
87 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
89 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
91 if (bfd_get_error () != bfd_error_system_call)
92 bfd_set_error (bfd_error_file_truncated);
102 /* this just does initialization */
103 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
104 elf_tdata (abfd) = (struct elf_obj_tdata *)
105 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
106 if (elf_tdata (abfd) == 0)
108 /* since everything is done at close time, do we need any
115 bfd_elf_get_str_section (abfd, shindex)
117 unsigned int shindex;
119 Elf_Internal_Shdr **i_shdrp;
120 char *shstrtab = NULL;
122 unsigned int shstrtabsize;
124 i_shdrp = elf_elfsections (abfd);
125 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
128 shstrtab = (char *) i_shdrp[shindex]->contents;
129 if (shstrtab == NULL)
131 /* No cached one, attempt to read, and cache what we read. */
132 offset = i_shdrp[shindex]->sh_offset;
133 shstrtabsize = i_shdrp[shindex]->sh_size;
134 shstrtab = elf_read (abfd, offset, shstrtabsize);
135 i_shdrp[shindex]->contents = (PTR) shstrtab;
141 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
143 unsigned int shindex;
144 unsigned int strindex;
146 Elf_Internal_Shdr *hdr;
151 hdr = elf_elfsections (abfd)[shindex];
153 if (hdr->contents == NULL
154 && bfd_elf_get_str_section (abfd, shindex) == NULL)
157 return ((char *) hdr->contents) + strindex;
160 /* Make a BFD section from an ELF section. We store a pointer to the
161 BFD section in the bfd_section field of the header. */
164 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
166 Elf_Internal_Shdr *hdr;
172 if (hdr->bfd_section != NULL)
174 BFD_ASSERT (strcmp (name,
175 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
179 newsect = bfd_make_section_anyway (abfd, name);
183 newsect->filepos = hdr->sh_offset;
185 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
186 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
187 || ! bfd_set_section_alignment (abfd, newsect,
188 bfd_log2 (hdr->sh_addralign)))
191 flags = SEC_NO_FLAGS;
192 if (hdr->sh_type != SHT_NOBITS)
193 flags |= SEC_HAS_CONTENTS;
194 if ((hdr->sh_flags & SHF_ALLOC) != 0)
197 if (hdr->sh_type != SHT_NOBITS)
200 if ((hdr->sh_flags & SHF_WRITE) == 0)
201 flags |= SEC_READONLY;
202 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
204 else if ((flags & SEC_LOAD) != 0)
207 /* The debugging sections appear to be recognized only by name, not
209 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
210 || strncmp (name, ".line", sizeof ".line" - 1) == 0
211 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
212 flags |= SEC_DEBUGGING;
214 if (! bfd_set_section_flags (abfd, newsect, flags))
217 if ((flags & SEC_ALLOC) != 0)
219 Elf_Internal_Phdr *phdr;
222 /* Look through the phdrs to see if we need to adjust the lma. */
223 phdr = elf_tdata (abfd)->phdr;
224 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
226 if (phdr->p_type == PT_LOAD
227 && phdr->p_paddr != 0
228 && phdr->p_vaddr != phdr->p_paddr
229 && phdr->p_vaddr <= hdr->sh_addr
230 && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size)
232 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
238 hdr->bfd_section = newsect;
239 elf_section_data (newsect)->this_hdr = *hdr;
249 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
252 Helper functions for GDB to locate the string tables.
253 Since BFD hides string tables from callers, GDB needs to use an
254 internal hook to find them. Sun's .stabstr, in particular,
255 isn't even pointed to by the .stab section, so ordinary
256 mechanisms wouldn't work to find it, even if we had some.
259 struct elf_internal_shdr *
260 bfd_elf_find_section (abfd, name)
264 Elf_Internal_Shdr **i_shdrp;
269 i_shdrp = elf_elfsections (abfd);
272 shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
273 if (shstrtab != NULL)
275 max = elf_elfheader (abfd)->e_shnum;
276 for (i = 1; i < max; i++)
277 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
284 const char *const bfd_elf_section_type_names[] = {
285 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
286 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
287 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
290 /* ELF relocs are against symbols. If we are producing relocateable
291 output, and the reloc is against an external symbol, and nothing
292 has given us any additional addend, the resulting reloc will also
293 be against the same symbol. In such a case, we don't want to
294 change anything about the way the reloc is handled, since it will
295 all be done at final link time. Rather than put special case code
296 into bfd_perform_relocation, all the reloc types use this howto
297 function. It just short circuits the reloc if producing
298 relocateable output against an external symbol. */
301 bfd_reloc_status_type
302 bfd_elf_generic_reloc (abfd,
310 arelent *reloc_entry;
313 asection *input_section;
315 char **error_message;
317 if (output_bfd != (bfd *) NULL
318 && (symbol->flags & BSF_SECTION_SYM) == 0
319 && (! reloc_entry->howto->partial_inplace
320 || reloc_entry->addend == 0))
322 reloc_entry->address += input_section->output_offset;
326 return bfd_reloc_continue;
329 /* Print out the program headers. */
332 _bfd_elf_print_private_bfd_data (abfd, farg)
336 FILE *f = (FILE *) farg;
337 Elf_Internal_Phdr *p;
339 bfd_byte *dynbuf = NULL;
341 p = elf_tdata (abfd)->phdr;
346 fprintf (f, "\nProgram Header:\n");
347 c = elf_elfheader (abfd)->e_phnum;
348 for (i = 0; i < c; i++, p++)
355 case PT_NULL: s = "NULL"; break;
356 case PT_LOAD: s = "LOAD"; break;
357 case PT_DYNAMIC: s = "DYNAMIC"; break;
358 case PT_INTERP: s = "INTERP"; break;
359 case PT_NOTE: s = "NOTE"; break;
360 case PT_SHLIB: s = "SHLIB"; break;
361 case PT_PHDR: s = "PHDR"; break;
362 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
364 fprintf (f, "%8s off 0x", s);
365 fprintf_vma (f, p->p_offset);
366 fprintf (f, " vaddr 0x");
367 fprintf_vma (f, p->p_vaddr);
368 fprintf (f, " paddr 0x");
369 fprintf_vma (f, p->p_paddr);
370 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
371 fprintf (f, " filesz 0x");
372 fprintf_vma (f, p->p_filesz);
373 fprintf (f, " memsz 0x");
374 fprintf_vma (f, p->p_memsz);
375 fprintf (f, " flags %c%c%c",
376 (p->p_flags & PF_R) != 0 ? 'r' : '-',
377 (p->p_flags & PF_W) != 0 ? 'w' : '-',
378 (p->p_flags & PF_X) != 0 ? 'x' : '-');
379 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
380 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
385 s = bfd_get_section_by_name (abfd, ".dynamic");
390 bfd_byte *extdyn, *extdynend;
392 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
394 fprintf (f, "\nDynamic Section:\n");
396 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
399 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
403 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
406 link = elf_elfsections (abfd)[elfsec]->sh_link;
408 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
409 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
412 extdynend = extdyn + s->_raw_size;
413 for (; extdyn < extdynend; extdyn += extdynsize)
415 Elf_Internal_Dyn dyn;
420 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
422 if (dyn.d_tag == DT_NULL)
429 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
433 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
434 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
435 case DT_PLTGOT: name = "PLTGOT"; break;
436 case DT_HASH: name = "HASH"; break;
437 case DT_STRTAB: name = "STRTAB"; break;
438 case DT_SYMTAB: name = "SYMTAB"; break;
439 case DT_RELA: name = "RELA"; break;
440 case DT_RELASZ: name = "RELASZ"; break;
441 case DT_RELAENT: name = "RELAENT"; break;
442 case DT_STRSZ: name = "STRSZ"; break;
443 case DT_SYMENT: name = "SYMENT"; break;
444 case DT_INIT: name = "INIT"; break;
445 case DT_FINI: name = "FINI"; break;
446 case DT_SONAME: name = "SONAME"; stringp = true; break;
447 case DT_RPATH: name = "RPATH"; stringp = true; break;
448 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
449 case DT_REL: name = "REL"; break;
450 case DT_RELSZ: name = "RELSZ"; break;
451 case DT_RELENT: name = "RELENT"; break;
452 case DT_PLTREL: name = "PLTREL"; break;
453 case DT_DEBUG: name = "DEBUG"; break;
454 case DT_TEXTREL: name = "TEXTREL"; break;
455 case DT_JMPREL: name = "JMPREL"; break;
458 fprintf (f, " %-11s ", name);
460 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
465 string = bfd_elf_string_from_elf_section (abfd, link,
469 fprintf (f, "%s", string);
486 /* Display ELF-specific fields of a symbol. */
488 bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
492 bfd_print_symbol_type how;
494 FILE *file = (FILE *) filep;
497 case bfd_print_symbol_name:
498 fprintf (file, "%s", symbol->name);
500 case bfd_print_symbol_more:
501 fprintf (file, "elf ");
502 fprintf_vma (file, symbol->value);
503 fprintf (file, " %lx", (long) symbol->flags);
505 case bfd_print_symbol_all:
507 CONST char *section_name;
508 section_name = symbol->section ? symbol->section->name : "(*none*)";
509 bfd_print_symbol_vandf ((PTR) file, symbol);
510 fprintf (file, " %s\t", section_name);
511 /* Print the "other" value for a symbol. For common symbols,
512 we've already printed the size; now print the alignment.
513 For other symbols, we have no specified alignment, and
514 we've printed the address; now print the size. */
516 (bfd_is_com_section (symbol->section)
517 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
518 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
519 fprintf (file, " %s", symbol->name);
525 /* Create an entry in an ELF linker hash table. */
527 struct bfd_hash_entry *
528 _bfd_elf_link_hash_newfunc (entry, table, string)
529 struct bfd_hash_entry *entry;
530 struct bfd_hash_table *table;
533 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
535 /* Allocate the structure if it has not already been allocated by a
537 if (ret == (struct elf_link_hash_entry *) NULL)
538 ret = ((struct elf_link_hash_entry *)
539 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
540 if (ret == (struct elf_link_hash_entry *) NULL)
541 return (struct bfd_hash_entry *) ret;
543 /* Call the allocation method of the superclass. */
544 ret = ((struct elf_link_hash_entry *)
545 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
547 if (ret != (struct elf_link_hash_entry *) NULL)
549 /* Set local fields. */
553 ret->dynstr_index = 0;
555 ret->got_offset = (bfd_vma) -1;
556 ret->plt_offset = (bfd_vma) -1;
557 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
558 ret->type = STT_NOTYPE;
559 /* Assume that we have been called by a non-ELF symbol reader.
560 This flag is then reset by the code which reads an ELF input
561 file. This ensures that a symbol created by a non-ELF symbol
562 reader will have the flag set correctly. */
563 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
566 return (struct bfd_hash_entry *) ret;
569 /* Initialize an ELF linker hash table. */
572 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
573 struct elf_link_hash_table *table;
575 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
576 struct bfd_hash_table *,
579 table->dynamic_sections_created = false;
580 table->dynobj = NULL;
581 /* The first dynamic symbol is a dummy. */
582 table->dynsymcount = 1;
583 table->dynstr = NULL;
584 table->bucketcount = 0;
585 table->needed = NULL;
586 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
589 /* Create an ELF linker hash table. */
591 struct bfd_link_hash_table *
592 _bfd_elf_link_hash_table_create (abfd)
595 struct elf_link_hash_table *ret;
597 ret = ((struct elf_link_hash_table *)
598 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
599 if (ret == (struct elf_link_hash_table *) NULL)
602 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
604 bfd_release (abfd, ret);
611 /* This is a hook for the ELF emulation code in the generic linker to
612 tell the backend linker what file name to use for the DT_NEEDED
613 entry for a dynamic object. The generic linker passes name as an
614 empty string to indicate that no DT_NEEDED entry should be made. */
617 bfd_elf_set_dt_needed_name (abfd, name)
621 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
622 elf_dt_needed_name (abfd) = name;
625 /* Get the list of DT_NEEDED entries for a link. */
627 struct bfd_link_needed_list *
628 bfd_elf_get_needed_list (abfd, info)
630 struct bfd_link_info *info;
632 if (info->hash->creator->flavour != bfd_target_elf_flavour)
634 return elf_hash_table (info)->needed;
637 /* Allocate an ELF string table--force the first byte to be zero. */
639 struct bfd_strtab_hash *
640 _bfd_elf_stringtab_init ()
642 struct bfd_strtab_hash *ret;
644 ret = _bfd_stringtab_init ();
649 loc = _bfd_stringtab_add (ret, "", true, false);
650 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
651 if (loc == (bfd_size_type) -1)
653 _bfd_stringtab_free (ret);
660 /* ELF .o/exec file reading */
662 /* Create a new bfd section from an ELF section header. */
665 bfd_section_from_shdr (abfd, shindex)
667 unsigned int shindex;
669 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
670 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
671 struct elf_backend_data *bed = get_elf_backend_data (abfd);
674 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
676 switch (hdr->sh_type)
679 /* Inactive section. Throw it away. */
682 case SHT_PROGBITS: /* Normal section with contents. */
683 case SHT_DYNAMIC: /* Dynamic linking information. */
684 case SHT_NOBITS: /* .bss section. */
685 case SHT_HASH: /* .hash section. */
686 case SHT_NOTE: /* .note section. */
687 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
689 case SHT_SYMTAB: /* A symbol table */
690 if (elf_onesymtab (abfd) == shindex)
693 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
694 BFD_ASSERT (elf_onesymtab (abfd) == 0);
695 elf_onesymtab (abfd) = shindex;
696 elf_tdata (abfd)->symtab_hdr = *hdr;
697 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
698 abfd->flags |= HAS_SYMS;
700 /* Sometimes a shared object will map in the symbol table. If
701 SHF_ALLOC is set, and this is a shared object, then we also
702 treat this section as a BFD section. We can not base the
703 decision purely on SHF_ALLOC, because that flag is sometimes
704 set in a relocateable object file, which would confuse the
706 if ((hdr->sh_flags & SHF_ALLOC) != 0
707 && (abfd->flags & DYNAMIC) != 0
708 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
713 case SHT_DYNSYM: /* A dynamic symbol table */
714 if (elf_dynsymtab (abfd) == shindex)
717 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
718 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
719 elf_dynsymtab (abfd) = shindex;
720 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
721 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
722 abfd->flags |= HAS_SYMS;
724 /* Besides being a symbol table, we also treat this as a regular
725 section, so that objcopy can handle it. */
726 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
728 case SHT_STRTAB: /* A string table */
729 if (hdr->bfd_section != NULL)
731 if (ehdr->e_shstrndx == shindex)
733 elf_tdata (abfd)->shstrtab_hdr = *hdr;
734 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
740 for (i = 1; i < ehdr->e_shnum; i++)
742 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
743 if (hdr2->sh_link == shindex)
745 if (! bfd_section_from_shdr (abfd, i))
747 if (elf_onesymtab (abfd) == i)
749 elf_tdata (abfd)->strtab_hdr = *hdr;
750 elf_elfsections (abfd)[shindex] =
751 &elf_tdata (abfd)->strtab_hdr;
754 if (elf_dynsymtab (abfd) == i)
756 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
757 elf_elfsections (abfd)[shindex] = hdr =
758 &elf_tdata (abfd)->dynstrtab_hdr;
759 /* We also treat this as a regular section, so
760 that objcopy can handle it. */
763 #if 0 /* Not handling other string tables specially right now. */
764 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
765 /* We have a strtab for some random other section. */
766 newsect = (asection *) hdr2->bfd_section;
769 hdr->bfd_section = newsect;
770 hdr2 = &elf_section_data (newsect)->str_hdr;
772 elf_elfsections (abfd)[shindex] = hdr2;
778 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
782 /* *These* do a lot of work -- but build no sections! */
784 asection *target_sect;
785 Elf_Internal_Shdr *hdr2;
786 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
788 /* For some incomprehensible reason Oracle distributes
789 libraries for Solaris in which some of the objects have
790 bogus sh_link fields. It would be nice if we could just
791 reject them, but, unfortunately, some people need to use
792 them. We scan through the section headers; if we find only
793 one suitable symbol table, we clobber the sh_link to point
794 to it. I hope this doesn't break anything. */
795 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
796 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
802 for (scan = 1; scan < ehdr->e_shnum; scan++)
804 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
805 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
816 hdr->sh_link = found;
819 /* Get the symbol table. */
820 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
821 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
824 /* If this reloc section does not use the main symbol table we
825 don't treat it as a reloc section. BFD can't adequately
826 represent such a section, so at least for now, we don't
827 try. We just present it as a normal section. */
828 if (hdr->sh_link != elf_onesymtab (abfd))
829 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
831 /* Don't allow REL relocations on a machine that uses RELA and
833 /* @@ Actually, the generic ABI does suggest that both might be
834 used in one file. But the four ABI Processor Supplements I
835 have access to right now all specify that only one is used on
836 each of those architectures. It's conceivable that, e.g., a
837 bunch of absolute 32-bit relocs might be more compact in REL
838 form even on a RELA machine... */
839 BFD_ASSERT (use_rela_p
840 ? (hdr->sh_type == SHT_RELA
841 && hdr->sh_entsize == bed->s->sizeof_rela)
842 : (hdr->sh_type == SHT_REL
843 && hdr->sh_entsize == bed->s->sizeof_rel));
845 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
847 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
848 if (target_sect == NULL)
851 hdr2 = &elf_section_data (target_sect)->rel_hdr;
853 elf_elfsections (abfd)[shindex] = hdr2;
854 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
855 target_sect->flags |= SEC_RELOC;
856 target_sect->relocation = NULL;
857 target_sect->rel_filepos = hdr->sh_offset;
858 abfd->flags |= HAS_RELOC;
867 /* Check for any processor-specific section types. */
869 if (bed->elf_backend_section_from_shdr)
870 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
878 /* Given an ELF section number, retrieve the corresponding BFD
882 bfd_section_from_elf_index (abfd, index)
886 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
887 if (index >= elf_elfheader (abfd)->e_shnum)
889 return elf_elfsections (abfd)[index]->bfd_section;
893 _bfd_elf_new_section_hook (abfd, sec)
897 struct bfd_elf_section_data *sdata;
899 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
902 sec->used_by_bfd = (PTR) sdata;
903 memset (sdata, 0, sizeof (*sdata));
907 /* Create a new bfd section from an ELF program header.
909 Since program segments have no names, we generate a synthetic name
910 of the form segment<NUM>, where NUM is generally the index in the
911 program header table. For segments that are split (see below) we
912 generate the names segment<NUM>a and segment<NUM>b.
914 Note that some program segments may have a file size that is different than
915 (less than) the memory size. All this means is that at execution the
916 system must allocate the amount of memory specified by the memory size,
917 but only initialize it with the first "file size" bytes read from the
918 file. This would occur for example, with program segments consisting
919 of combined data+bss.
921 To handle the above situation, this routine generates TWO bfd sections
922 for the single program segment. The first has the length specified by
923 the file size of the segment, and the second has the length specified
924 by the difference between the two sizes. In effect, the segment is split
925 into it's initialized and uninitialized parts.
930 bfd_section_from_phdr (abfd, hdr, index)
932 Elf_Internal_Phdr *hdr;
940 split = ((hdr->p_memsz > 0) &&
941 (hdr->p_filesz > 0) &&
942 (hdr->p_memsz > hdr->p_filesz));
943 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
944 name = bfd_alloc (abfd, strlen (namebuf) + 1);
947 strcpy (name, namebuf);
948 newsect = bfd_make_section (abfd, name);
951 newsect->vma = hdr->p_vaddr;
952 newsect->lma = hdr->p_paddr;
953 newsect->_raw_size = hdr->p_filesz;
954 newsect->filepos = hdr->p_offset;
955 newsect->flags |= SEC_HAS_CONTENTS;
956 if (hdr->p_type == PT_LOAD)
958 newsect->flags |= SEC_ALLOC;
959 newsect->flags |= SEC_LOAD;
960 if (hdr->p_flags & PF_X)
962 /* FIXME: all we known is that it has execute PERMISSION,
964 newsect->flags |= SEC_CODE;
967 if (!(hdr->p_flags & PF_W))
969 newsect->flags |= SEC_READONLY;
974 sprintf (namebuf, "segment%db", index);
975 name = bfd_alloc (abfd, strlen (namebuf) + 1);
978 strcpy (name, namebuf);
979 newsect = bfd_make_section (abfd, name);
982 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
983 newsect->lma = hdr->p_paddr + hdr->p_filesz;
984 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
985 if (hdr->p_type == PT_LOAD)
987 newsect->flags |= SEC_ALLOC;
988 if (hdr->p_flags & PF_X)
989 newsect->flags |= SEC_CODE;
991 if (!(hdr->p_flags & PF_W))
992 newsect->flags |= SEC_READONLY;
998 /* Set up an ELF internal section header for a section. */
1002 elf_fake_sections (abfd, asect, failedptrarg)
1007 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1008 boolean *failedptr = (boolean *) failedptrarg;
1009 Elf_Internal_Shdr *this_hdr;
1013 /* We already failed; just get out of the bfd_map_over_sections
1018 this_hdr = &elf_section_data (asect)->this_hdr;
1020 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1023 if (this_hdr->sh_name == (unsigned long) -1)
1029 this_hdr->sh_flags = 0;
1031 if ((asect->flags & SEC_ALLOC) != 0)
1032 this_hdr->sh_addr = asect->vma;
1034 this_hdr->sh_addr = 0;
1036 this_hdr->sh_offset = 0;
1037 this_hdr->sh_size = asect->_raw_size;
1038 this_hdr->sh_link = 0;
1039 this_hdr->sh_addralign = 1 << asect->alignment_power;
1040 /* The sh_entsize and sh_info fields may have been set already by
1041 copy_private_section_data. */
1043 this_hdr->bfd_section = asect;
1044 this_hdr->contents = NULL;
1046 /* FIXME: This should not be based on section names. */
1047 if (strcmp (asect->name, ".dynstr") == 0)
1048 this_hdr->sh_type = SHT_STRTAB;
1049 else if (strcmp (asect->name, ".hash") == 0)
1051 this_hdr->sh_type = SHT_HASH;
1052 this_hdr->sh_entsize = bed->s->arch_size / 8;
1054 else if (strcmp (asect->name, ".dynsym") == 0)
1056 this_hdr->sh_type = SHT_DYNSYM;
1057 this_hdr->sh_entsize = bed->s->sizeof_sym;
1059 else if (strcmp (asect->name, ".dynamic") == 0)
1061 this_hdr->sh_type = SHT_DYNAMIC;
1062 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1064 else if (strncmp (asect->name, ".rela", 5) == 0
1065 && get_elf_backend_data (abfd)->use_rela_p)
1067 this_hdr->sh_type = SHT_RELA;
1068 this_hdr->sh_entsize = bed->s->sizeof_rela;
1070 else if (strncmp (asect->name, ".rel", 4) == 0
1071 && ! get_elf_backend_data (abfd)->use_rela_p)
1073 this_hdr->sh_type = SHT_REL;
1074 this_hdr->sh_entsize = bed->s->sizeof_rel;
1076 else if (strcmp (asect->name, ".note") == 0)
1077 this_hdr->sh_type = SHT_NOTE;
1078 else if (strncmp (asect->name, ".stab", 5) == 0
1079 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1080 this_hdr->sh_type = SHT_STRTAB;
1081 else if ((asect->flags & SEC_ALLOC) != 0
1082 && (asect->flags & SEC_LOAD) != 0)
1083 this_hdr->sh_type = SHT_PROGBITS;
1084 else if ((asect->flags & SEC_ALLOC) != 0
1085 && ((asect->flags & SEC_LOAD) == 0))
1086 this_hdr->sh_type = SHT_NOBITS;
1090 this_hdr->sh_type = SHT_PROGBITS;
1093 if ((asect->flags & SEC_ALLOC) != 0)
1094 this_hdr->sh_flags |= SHF_ALLOC;
1095 if ((asect->flags & SEC_READONLY) == 0)
1096 this_hdr->sh_flags |= SHF_WRITE;
1097 if ((asect->flags & SEC_CODE) != 0)
1098 this_hdr->sh_flags |= SHF_EXECINSTR;
1100 /* Check for processor-specific section types. */
1102 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1104 if (bed->elf_backend_fake_sections)
1105 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1108 /* If the section has relocs, set up a section header for the
1109 SHT_REL[A] section. */
1110 if ((asect->flags & SEC_RELOC) != 0)
1112 Elf_Internal_Shdr *rela_hdr;
1113 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1116 rela_hdr = &elf_section_data (asect)->rel_hdr;
1117 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1123 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1125 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1127 if (rela_hdr->sh_name == (unsigned int) -1)
1132 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1133 rela_hdr->sh_entsize = (use_rela_p
1134 ? bed->s->sizeof_rela
1135 : bed->s->sizeof_rel);
1136 rela_hdr->sh_addralign = bed->s->file_align;
1137 rela_hdr->sh_flags = 0;
1138 rela_hdr->sh_addr = 0;
1139 rela_hdr->sh_size = 0;
1140 rela_hdr->sh_offset = 0;
1144 /* Assign all ELF section numbers. The dummy first section is handled here
1145 too. The link/info pointers for the standard section types are filled
1146 in here too, while we're at it. */
1149 assign_section_numbers (abfd)
1152 struct elf_obj_tdata *t = elf_tdata (abfd);
1154 unsigned int section_number;
1155 Elf_Internal_Shdr **i_shdrp;
1156 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1160 for (sec = abfd->sections; sec; sec = sec->next)
1162 struct bfd_elf_section_data *d = elf_section_data (sec);
1164 d->this_idx = section_number++;
1165 if ((sec->flags & SEC_RELOC) == 0)
1168 d->rel_idx = section_number++;
1171 t->shstrtab_section = section_number++;
1172 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1173 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1175 if (abfd->symcount > 0)
1177 t->symtab_section = section_number++;
1178 t->strtab_section = section_number++;
1181 elf_elfheader (abfd)->e_shnum = section_number;
1183 /* Set up the list of section header pointers, in agreement with the
1185 i_shdrp = ((Elf_Internal_Shdr **)
1186 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1187 if (i_shdrp == NULL)
1190 i_shdrp[0] = ((Elf_Internal_Shdr *)
1191 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1192 if (i_shdrp[0] == NULL)
1194 bfd_release (abfd, i_shdrp);
1197 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1199 elf_elfsections (abfd) = i_shdrp;
1201 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1202 if (abfd->symcount > 0)
1204 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1205 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1206 t->symtab_hdr.sh_link = t->strtab_section;
1208 for (sec = abfd->sections; sec; sec = sec->next)
1210 struct bfd_elf_section_data *d = elf_section_data (sec);
1214 i_shdrp[d->this_idx] = &d->this_hdr;
1215 if (d->rel_idx != 0)
1216 i_shdrp[d->rel_idx] = &d->rel_hdr;
1218 /* Fill in the sh_link and sh_info fields while we're at it. */
1220 /* sh_link of a reloc section is the section index of the symbol
1221 table. sh_info is the section index of the section to which
1222 the relocation entries apply. */
1223 if (d->rel_idx != 0)
1225 d->rel_hdr.sh_link = t->symtab_section;
1226 d->rel_hdr.sh_info = d->this_idx;
1229 switch (d->this_hdr.sh_type)
1233 /* A reloc section which we are treating as a normal BFD
1234 section. sh_link is the section index of the symbol
1235 table. sh_info is the section index of the section to
1236 which the relocation entries apply. We assume that an
1237 allocated reloc section uses the dynamic symbol table.
1238 FIXME: How can we be sure? */
1239 s = bfd_get_section_by_name (abfd, ".dynsym");
1241 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1243 /* We look up the section the relocs apply to by name. */
1245 if (d->this_hdr.sh_type == SHT_REL)
1249 s = bfd_get_section_by_name (abfd, name);
1251 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1255 /* We assume that a section named .stab*str is a stabs
1256 string section. We look for a section with the same name
1257 but without the trailing ``str'', and set its sh_link
1258 field to point to this section. */
1259 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1260 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1265 len = strlen (sec->name);
1266 alc = (char *) bfd_malloc (len - 2);
1269 strncpy (alc, sec->name, len - 3);
1270 alc[len - 3] = '\0';
1271 s = bfd_get_section_by_name (abfd, alc);
1275 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1277 /* This is a .stab section. */
1278 elf_section_data (s)->this_hdr.sh_entsize =
1279 4 + 2 * (bed->s->arch_size / 8);
1286 /* sh_link is the section header index of the string table
1287 used for the dynamic entries or symbol table. */
1288 s = bfd_get_section_by_name (abfd, ".dynstr");
1290 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1294 /* sh_link is the section header index of the symbol table
1295 this hash table is for. */
1296 s = bfd_get_section_by_name (abfd, ".dynsym");
1298 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1306 /* Map symbol from it's internal number to the external number, moving
1307 all local symbols to be at the head of the list. */
1310 sym_is_global (abfd, sym)
1314 /* If the backend has a special mapping, use it. */
1315 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1316 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1319 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1320 || bfd_is_und_section (bfd_get_section (sym))
1321 || bfd_is_com_section (bfd_get_section (sym)));
1325 elf_map_symbols (abfd)
1328 int symcount = bfd_get_symcount (abfd);
1329 asymbol **syms = bfd_get_outsymbols (abfd);
1330 asymbol **sect_syms;
1332 int num_globals = 0;
1333 int num_locals2 = 0;
1334 int num_globals2 = 0;
1336 int num_sections = 0;
1342 fprintf (stderr, "elf_map_symbols\n");
1346 /* Add a section symbol for each BFD section. FIXME: Is this really
1348 for (asect = abfd->sections; asect; asect = asect->next)
1350 if (max_index < asect->index)
1351 max_index = asect->index;
1355 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1356 if (sect_syms == NULL)
1358 elf_section_syms (abfd) = sect_syms;
1360 for (idx = 0; idx < symcount; idx++)
1362 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1363 && (syms[idx]->value + syms[idx]->section->vma) == 0)
1367 sec = syms[idx]->section;
1368 if (sec->owner != NULL)
1370 if (sec->owner != abfd)
1372 if (sec->output_offset != 0)
1374 sec = sec->output_section;
1375 BFD_ASSERT (sec->owner == abfd);
1377 sect_syms[sec->index] = syms[idx];
1382 for (asect = abfd->sections; asect; asect = asect->next)
1386 if (sect_syms[asect->index] != NULL)
1389 sym = bfd_make_empty_symbol (abfd);
1392 sym->the_bfd = abfd;
1393 sym->name = asect->name;
1395 /* Set the flags to 0 to indicate that this one was newly added. */
1397 sym->section = asect;
1398 sect_syms[asect->index] = sym;
1402 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1403 asect->name, (long) asect->vma, asect->index, (long) asect);
1407 /* Classify all of the symbols. */
1408 for (idx = 0; idx < symcount; idx++)
1410 if (!sym_is_global (abfd, syms[idx]))
1415 for (asect = abfd->sections; asect; asect = asect->next)
1417 if (sect_syms[asect->index] != NULL
1418 && sect_syms[asect->index]->flags == 0)
1420 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1421 if (!sym_is_global (abfd, sect_syms[asect->index]))
1425 sect_syms[asect->index]->flags = 0;
1429 /* Now sort the symbols so the local symbols are first. */
1430 new_syms = ((asymbol **)
1432 (num_locals + num_globals) * sizeof (asymbol *)));
1433 if (new_syms == NULL)
1436 for (idx = 0; idx < symcount; idx++)
1438 asymbol *sym = syms[idx];
1441 if (!sym_is_global (abfd, sym))
1444 i = num_locals + num_globals2++;
1446 sym->udata.i = i + 1;
1448 for (asect = abfd->sections; asect; asect = asect->next)
1450 if (sect_syms[asect->index] != NULL
1451 && sect_syms[asect->index]->flags == 0)
1453 asymbol *sym = sect_syms[asect->index];
1456 sym->flags = BSF_SECTION_SYM;
1457 if (!sym_is_global (abfd, sym))
1460 i = num_locals + num_globals2++;
1462 sym->udata.i = i + 1;
1466 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1468 elf_num_locals (abfd) = num_locals;
1469 elf_num_globals (abfd) = num_globals;
1473 /* Align to the maximum file alignment that could be required for any
1474 ELF data structure. */
1476 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1477 static INLINE file_ptr
1478 align_file_position (off, align)
1482 return (off + align - 1) & ~(align - 1);
1485 /* Assign a file position to a section, optionally aligning to the
1486 required section alignment. */
1489 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1490 Elf_Internal_Shdr *i_shdrp;
1498 al = i_shdrp->sh_addralign;
1500 offset = BFD_ALIGN (offset, al);
1502 i_shdrp->sh_offset = offset;
1503 if (i_shdrp->bfd_section != NULL)
1504 i_shdrp->bfd_section->filepos = offset;
1505 if (i_shdrp->sh_type != SHT_NOBITS)
1506 offset += i_shdrp->sh_size;
1510 /* Compute the file positions we are going to put the sections at, and
1511 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1512 is not NULL, this is being called by the ELF backend linker. */
1515 _bfd_elf_compute_section_file_positions (abfd, link_info)
1517 struct bfd_link_info *link_info;
1519 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1521 struct bfd_strtab_hash *strtab;
1522 Elf_Internal_Shdr *shstrtab_hdr;
1524 if (abfd->output_has_begun)
1527 /* Do any elf backend specific processing first. */
1528 if (bed->elf_backend_begin_write_processing)
1529 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1531 if (! prep_headers (abfd))
1535 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1539 if (!assign_section_numbers (abfd))
1542 /* The backend linker builds symbol table information itself. */
1543 if (link_info == NULL && abfd->symcount > 0)
1545 if (! swap_out_syms (abfd, &strtab))
1549 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1550 /* sh_name was set in prep_headers. */
1551 shstrtab_hdr->sh_type = SHT_STRTAB;
1552 shstrtab_hdr->sh_flags = 0;
1553 shstrtab_hdr->sh_addr = 0;
1554 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1555 shstrtab_hdr->sh_entsize = 0;
1556 shstrtab_hdr->sh_link = 0;
1557 shstrtab_hdr->sh_info = 0;
1558 /* sh_offset is set in assign_file_positions_except_relocs. */
1559 shstrtab_hdr->sh_addralign = 1;
1561 if (!assign_file_positions_except_relocs (abfd))
1564 if (link_info == NULL && abfd->symcount > 0)
1567 Elf_Internal_Shdr *hdr;
1569 off = elf_tdata (abfd)->next_file_pos;
1571 hdr = &elf_tdata (abfd)->symtab_hdr;
1572 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1574 hdr = &elf_tdata (abfd)->strtab_hdr;
1575 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1577 elf_tdata (abfd)->next_file_pos = off;
1579 /* Now that we know where the .strtab section goes, write it
1581 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
1582 || ! _bfd_stringtab_emit (abfd, strtab))
1584 _bfd_stringtab_free (strtab);
1587 abfd->output_has_begun = true;
1592 /* Create a mapping from a set of sections to a program segment. */
1594 static INLINE struct elf_segment_map *
1595 make_mapping (abfd, sections, from, to, phdr)
1597 asection **sections;
1602 struct elf_segment_map *m;
1606 m = ((struct elf_segment_map *)
1608 (sizeof (struct elf_segment_map)
1609 + (to - from - 1) * sizeof (asection *))));
1613 m->p_type = PT_LOAD;
1614 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1615 m->sections[i - from] = *hdrpp;
1616 m->count = to - from;
1618 if (from == 0 && phdr)
1620 /* Include the headers in the first PT_LOAD segment. */
1621 m->includes_filehdr = 1;
1622 m->includes_phdrs = 1;
1628 /* Set up a mapping from BFD sections to program segments. */
1631 map_sections_to_segments (abfd)
1634 asection **sections = NULL;
1638 struct elf_segment_map *mfirst;
1639 struct elf_segment_map **pm;
1640 struct elf_segment_map *m;
1642 unsigned int phdr_index;
1643 bfd_vma maxpagesize;
1645 boolean phdr_in_section = true;
1649 if (elf_tdata (abfd)->segment_map != NULL)
1652 if (bfd_count_sections (abfd) == 0)
1655 /* Select the allocated sections, and sort them. */
1657 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
1658 * sizeof (asection *));
1659 if (sections == NULL)
1663 for (s = abfd->sections; s != NULL; s = s->next)
1665 if ((s->flags & SEC_ALLOC) != 0)
1671 BFD_ASSERT (i <= bfd_count_sections (abfd));
1674 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
1676 /* Build the mapping. */
1681 /* If we have a .interp section, then create a PT_PHDR segment for
1682 the program headers and a PT_INTERP segment for the .interp
1684 s = bfd_get_section_by_name (abfd, ".interp");
1685 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1687 m = ((struct elf_segment_map *)
1688 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1692 m->p_type = PT_PHDR;
1693 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1694 m->p_flags = PF_R | PF_X;
1695 m->p_flags_valid = 1;
1696 m->includes_phdrs = 1;
1701 m = ((struct elf_segment_map *)
1702 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1706 m->p_type = PT_INTERP;
1714 /* Look through the sections. We put sections in the same program
1715 segment when the start of the second section can be placed within
1716 a few bytes of the end of the first section. */
1719 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1721 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
1723 && (dynsec->flags & SEC_LOAD) == 0)
1726 /* Deal with -Ttext or something similar such that the
1727 first section is not adjacent to the program headers. */
1729 && ((sections[0]->lma % maxpagesize) <
1730 (elf_tdata (abfd)->program_header_size % maxpagesize)))
1731 phdr_in_section = false;
1733 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
1739 /* See if this section and the last one will fit in the same
1740 segment. Don't put a loadable section after a non-loadable
1741 section. If we are building a dynamic executable, don't put
1742 a writable section in a read only segment (we don't do this
1743 for a non-dynamic executable because some people prefer to
1744 have only one program segment; anybody can use PHDRS in their
1745 linker script to control what happens anyhow). */
1746 if (last_hdr == NULL
1747 || ((BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1749 && ((last_hdr->flags & SEC_LOAD) != 0
1750 || (hdr->flags & SEC_LOAD) == 0)
1753 || (hdr->flags & SEC_READONLY) != 0)))
1759 /* This section won't fit in the program segment. We must
1760 create a new program header holding all the sections from
1761 phdr_index until hdr. */
1763 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
1770 if ((hdr->flags & SEC_READONLY) == 0)
1775 phdr_in_section = false;
1778 /* Create a final PT_LOAD program segment. */
1779 if (last_hdr != NULL)
1781 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
1789 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
1792 m = ((struct elf_segment_map *)
1793 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1797 m->p_type = PT_DYNAMIC;
1799 m->sections[0] = dynsec;
1808 elf_tdata (abfd)->segment_map = mfirst;
1812 if (sections != NULL)
1817 /* Sort sections by VMA. */
1820 elf_sort_sections (arg1, arg2)
1824 const asection *sec1 = *(const asection **) arg1;
1825 const asection *sec2 = *(const asection **) arg2;
1827 if (sec1->vma < sec2->vma)
1829 else if (sec1->vma > sec2->vma)
1832 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
1834 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1838 return sec1->target_index - sec2->target_index;
1847 /* Sort by size, to put zero sized sections before others at the
1850 if (sec1->_raw_size < sec2->_raw_size)
1852 if (sec1->_raw_size > sec2->_raw_size)
1855 return sec1->target_index - sec2->target_index;
1858 /* Assign file positions to the sections based on the mapping from
1859 sections to segments. This function also sets up some fields in
1860 the file header, and writes out the program headers. */
1863 assign_file_positions_for_segments (abfd)
1866 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1868 struct elf_segment_map *m;
1870 Elf_Internal_Phdr *phdrs;
1872 bfd_vma filehdr_vaddr, filehdr_paddr;
1873 bfd_vma phdrs_vaddr, phdrs_paddr;
1874 Elf_Internal_Phdr *p;
1876 if (elf_tdata (abfd)->segment_map == NULL)
1878 if (! map_sections_to_segments (abfd))
1882 if (bed->elf_backend_modify_segment_map)
1884 if (! (*bed->elf_backend_modify_segment_map) (abfd))
1889 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1892 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
1893 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
1894 elf_elfheader (abfd)->e_phnum = count;
1899 /* If we already counted the number of program segments, make sure
1900 that we allocated enough space. This happens when SIZEOF_HEADERS
1901 is used in a linker script. */
1902 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
1903 if (alloc != 0 && count > alloc)
1905 ((*_bfd_error_handler)
1906 ("%s: Not enough room for program headers (allocated %u, need %u)",
1907 bfd_get_filename (abfd), alloc, count));
1908 bfd_set_error (bfd_error_bad_value);
1915 phdrs = ((Elf_Internal_Phdr *)
1916 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
1920 off = bed->s->sizeof_ehdr;
1921 off += alloc * bed->s->sizeof_phdr;
1927 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
1934 /* If elf_segment_map is not from map_sections_to_segments, the
1935 sections may not be correctly ordered. */
1937 qsort (m->sections, (size_t) m->count, sizeof (asection *),
1940 p->p_type = m->p_type;
1942 if (m->p_flags_valid)
1943 p->p_flags = m->p_flags;
1947 if (p->p_type == PT_LOAD
1949 && (m->sections[0]->flags & SEC_LOAD) != 0)
1950 off += (m->sections[0]->vma - off) % bed->maxpagesize;
1955 p->p_vaddr = m->sections[0]->vma;
1957 if (m->p_paddr_valid)
1958 p->p_paddr = m->p_paddr;
1959 else if (m->count == 0)
1962 p->p_paddr = m->sections[0]->lma;
1964 if (p->p_type == PT_LOAD)
1965 p->p_align = bed->maxpagesize;
1966 else if (m->count == 0)
1967 p->p_align = bed->s->file_align;
1975 if (m->includes_filehdr)
1977 if (! m->p_flags_valid)
1980 p->p_filesz = bed->s->sizeof_ehdr;
1981 p->p_memsz = bed->s->sizeof_ehdr;
1984 BFD_ASSERT (p->p_type == PT_LOAD);
1986 if (! m->p_paddr_valid)
1989 if (p->p_type == PT_LOAD)
1991 filehdr_vaddr = p->p_vaddr;
1992 filehdr_paddr = p->p_paddr;
1996 if (m->includes_phdrs)
1998 if (! m->p_flags_valid)
2000 if (m->includes_filehdr)
2002 if (p->p_type == PT_LOAD)
2004 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2005 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2010 p->p_offset = bed->s->sizeof_ehdr;
2013 BFD_ASSERT (p->p_type == PT_LOAD);
2014 p->p_vaddr -= off - p->p_offset;
2015 if (! m->p_paddr_valid)
2016 p->p_paddr -= off - p->p_offset;
2018 if (p->p_type == PT_LOAD)
2020 phdrs_vaddr = p->p_vaddr;
2021 phdrs_paddr = p->p_paddr;
2024 p->p_filesz += alloc * bed->s->sizeof_phdr;
2025 p->p_memsz += alloc * bed->s->sizeof_phdr;
2028 if (p->p_type == PT_LOAD)
2030 if (! m->includes_filehdr && ! m->includes_phdrs)
2036 adjust = off - (p->p_offset + p->p_filesz);
2037 p->p_filesz += adjust;
2038 p->p_memsz += adjust;
2042 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2046 bfd_size_type align;
2051 if (p->p_type == PT_LOAD)
2055 /* The section VMA must equal the file position modulo
2057 if ((flags & SEC_LOAD) != 0)
2059 adjust = (sec->vma - off) % bed->maxpagesize;
2064 p->p_memsz += adjust;
2065 if ((flags & SEC_LOAD) != 0)
2066 p->p_filesz += adjust;
2073 if ((flags & SEC_LOAD) != 0)
2074 off += sec->_raw_size;
2077 p->p_memsz += sec->_raw_size;
2079 if ((flags & SEC_LOAD) != 0)
2080 p->p_filesz += sec->_raw_size;
2082 align = 1 << bfd_get_section_alignment (abfd, sec);
2083 if (align > p->p_align)
2086 if (! m->p_flags_valid)
2089 if ((flags & SEC_CODE) != 0)
2091 if ((flags & SEC_READONLY) == 0)
2097 /* Now that we have set the section file positions, we can set up
2098 the file positions for the non PT_LOAD segments. */
2099 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2103 if (p->p_type != PT_LOAD && m->count > 0)
2105 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2106 p->p_offset = m->sections[0]->filepos;
2110 if (m->includes_filehdr)
2112 p->p_vaddr = filehdr_vaddr;
2113 if (! m->p_paddr_valid)
2114 p->p_paddr = filehdr_paddr;
2116 else if (m->includes_phdrs)
2118 p->p_vaddr = phdrs_vaddr;
2119 if (! m->p_paddr_valid)
2120 p->p_paddr = phdrs_paddr;
2125 /* Clear out any program headers we allocated but did not use. */
2126 for (; count < alloc; count++, p++)
2128 memset (p, 0, sizeof *p);
2129 p->p_type = PT_NULL;
2132 elf_tdata (abfd)->phdr = phdrs;
2134 elf_tdata (abfd)->next_file_pos = off;
2136 /* Write out the program headers. */
2137 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2138 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2144 /* Get the size of the program header.
2146 If this is called by the linker before any of the section VMA's are set, it
2147 can't calculate the correct value for a strange memory layout. This only
2148 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2149 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2150 data segment (exclusive of .interp and .dynamic).
2152 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2153 will be two segments. */
2155 static bfd_size_type
2156 get_program_header_size (abfd)
2161 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2163 /* We can't return a different result each time we're called. */
2164 if (elf_tdata (abfd)->program_header_size != 0)
2165 return elf_tdata (abfd)->program_header_size;
2167 if (elf_tdata (abfd)->segment_map != NULL)
2169 struct elf_segment_map *m;
2172 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2174 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2175 return elf_tdata (abfd)->program_header_size;
2178 /* Assume we will need exactly two PT_LOAD segments: one for text
2179 and one for data. */
2182 s = bfd_get_section_by_name (abfd, ".interp");
2183 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2185 /* If we have a loadable interpreter section, we need a
2186 PT_INTERP segment. In this case, assume we also need a
2187 PT_PHDR segment, although that may not be true for all
2192 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2194 /* We need a PT_DYNAMIC segment. */
2198 /* Let the backend count up any program headers it might need. */
2199 if (bed->elf_backend_additional_program_headers)
2203 a = (*bed->elf_backend_additional_program_headers) (abfd);
2209 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2210 return elf_tdata (abfd)->program_header_size;
2213 /* Work out the file positions of all the sections. This is called by
2214 _bfd_elf_compute_section_file_positions. All the section sizes and
2215 VMAs must be known before this is called.
2217 We do not consider reloc sections at this point, unless they form
2218 part of the loadable image. Reloc sections are assigned file
2219 positions in assign_file_positions_for_relocs, which is called by
2220 write_object_contents and final_link.
2222 We also don't set the positions of the .symtab and .strtab here. */
2225 assign_file_positions_except_relocs (abfd)
2228 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2229 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2230 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2232 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2234 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2236 Elf_Internal_Shdr **hdrpp;
2239 /* Start after the ELF header. */
2240 off = i_ehdrp->e_ehsize;
2242 /* We are not creating an executable, which means that we are
2243 not creating a program header, and that the actual order of
2244 the sections in the file is unimportant. */
2245 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2247 Elf_Internal_Shdr *hdr;
2250 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2252 hdr->sh_offset = -1;
2255 if (i == tdata->symtab_section
2256 || i == tdata->strtab_section)
2258 hdr->sh_offset = -1;
2262 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2268 Elf_Internal_Shdr **hdrpp;
2270 /* Assign file positions for the loaded sections based on the
2271 assignment of sections to segments. */
2272 if (! assign_file_positions_for_segments (abfd))
2275 /* Assign file positions for the other sections. */
2277 off = elf_tdata (abfd)->next_file_pos;
2278 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2280 Elf_Internal_Shdr *hdr;
2283 if (hdr->bfd_section != NULL
2284 && hdr->bfd_section->filepos != 0)
2285 hdr->sh_offset = hdr->bfd_section->filepos;
2286 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2288 ((*_bfd_error_handler)
2289 ("%s: warning: allocated section `%s' not in segment",
2290 bfd_get_filename (abfd),
2291 (hdr->bfd_section == NULL
2293 : hdr->bfd_section->name)));
2294 off += (hdr->sh_addr - off) % bed->maxpagesize;
2295 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2298 else if (hdr->sh_type == SHT_REL
2299 || hdr->sh_type == SHT_RELA
2300 || hdr == i_shdrpp[tdata->symtab_section]
2301 || hdr == i_shdrpp[tdata->strtab_section])
2302 hdr->sh_offset = -1;
2304 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2308 /* Place the section headers. */
2309 off = align_file_position (off, bed->s->file_align);
2310 i_ehdrp->e_shoff = off;
2311 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2313 elf_tdata (abfd)->next_file_pos = off;
2322 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2323 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2324 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2326 struct bfd_strtab_hash *shstrtab;
2327 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2329 i_ehdrp = elf_elfheader (abfd);
2330 i_shdrp = elf_elfsections (abfd);
2332 shstrtab = _bfd_elf_stringtab_init ();
2333 if (shstrtab == NULL)
2336 elf_shstrtab (abfd) = shstrtab;
2338 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2339 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2340 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2341 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2343 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2344 i_ehdrp->e_ident[EI_DATA] =
2345 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
2346 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2348 for (count = EI_PAD; count < EI_NIDENT; count++)
2349 i_ehdrp->e_ident[count] = 0;
2351 if ((abfd->flags & DYNAMIC) != 0)
2352 i_ehdrp->e_type = ET_DYN;
2353 else if ((abfd->flags & EXEC_P) != 0)
2354 i_ehdrp->e_type = ET_EXEC;
2356 i_ehdrp->e_type = ET_REL;
2358 switch (bfd_get_arch (abfd))
2360 case bfd_arch_unknown:
2361 i_ehdrp->e_machine = EM_NONE;
2363 case bfd_arch_sparc:
2364 if (bed->s->arch_size == 64)
2365 i_ehdrp->e_machine = EM_SPARC64;
2367 i_ehdrp->e_machine = EM_SPARC;
2370 i_ehdrp->e_machine = EM_386;
2373 i_ehdrp->e_machine = EM_68K;
2376 i_ehdrp->e_machine = EM_88K;
2379 i_ehdrp->e_machine = EM_860;
2381 case bfd_arch_mips: /* MIPS Rxxxx */
2382 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2385 i_ehdrp->e_machine = EM_PARISC;
2387 case bfd_arch_powerpc:
2388 i_ehdrp->e_machine = EM_PPC;
2390 /* start-sanitize-arc */
2392 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2394 /* end-sanitize-arc */
2395 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2397 i_ehdrp->e_machine = EM_NONE;
2399 i_ehdrp->e_version = bed->s->ev_current;
2400 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2402 /* no program header, for now. */
2403 i_ehdrp->e_phoff = 0;
2404 i_ehdrp->e_phentsize = 0;
2405 i_ehdrp->e_phnum = 0;
2407 /* each bfd section is section header entry */
2408 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2409 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2411 /* if we're building an executable, we'll need a program header table */
2412 if (abfd->flags & EXEC_P)
2414 /* it all happens later */
2416 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2418 /* elf_build_phdrs() returns a (NULL-terminated) array of
2419 Elf_Internal_Phdrs */
2420 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2421 i_ehdrp->e_phoff = outbase;
2422 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2427 i_ehdrp->e_phentsize = 0;
2429 i_ehdrp->e_phoff = 0;
2432 elf_tdata (abfd)->symtab_hdr.sh_name =
2433 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2434 elf_tdata (abfd)->strtab_hdr.sh_name =
2435 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2436 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2437 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2438 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2439 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2440 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2446 /* Assign file positions for all the reloc sections which are not part
2447 of the loadable file image. */
2450 _bfd_elf_assign_file_positions_for_relocs (abfd)
2455 Elf_Internal_Shdr **shdrpp;
2457 off = elf_tdata (abfd)->next_file_pos;
2459 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2460 i < elf_elfheader (abfd)->e_shnum;
2463 Elf_Internal_Shdr *shdrp;
2466 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2467 && shdrp->sh_offset == -1)
2468 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
2471 elf_tdata (abfd)->next_file_pos = off;
2475 _bfd_elf_write_object_contents (abfd)
2478 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2479 Elf_Internal_Ehdr *i_ehdrp;
2480 Elf_Internal_Shdr **i_shdrp;
2484 if (! abfd->output_has_begun
2485 && ! _bfd_elf_compute_section_file_positions (abfd,
2486 (struct bfd_link_info *) NULL))
2489 i_shdrp = elf_elfsections (abfd);
2490 i_ehdrp = elf_elfheader (abfd);
2493 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2496 _bfd_elf_assign_file_positions_for_relocs (abfd);
2498 /* After writing the headers, we need to write the sections too... */
2499 for (count = 1; count < i_ehdrp->e_shnum; count++)
2501 if (bed->elf_backend_section_processing)
2502 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2503 if (i_shdrp[count]->contents)
2505 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2506 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2508 != i_shdrp[count]->sh_size))
2513 /* Write out the section header names. */
2514 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2515 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2518 if (bed->elf_backend_final_write_processing)
2519 (*bed->elf_backend_final_write_processing) (abfd,
2520 elf_tdata (abfd)->linker);
2522 return bed->s->write_shdrs_and_ehdr (abfd);
2525 /* given a section, search the header to find them... */
2527 _bfd_elf_section_from_bfd_section (abfd, asect)
2531 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2532 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2534 Elf_Internal_Shdr *hdr;
2535 int maxindex = elf_elfheader (abfd)->e_shnum;
2537 for (index = 0; index < maxindex; index++)
2539 hdr = i_shdrp[index];
2540 if (hdr->bfd_section == asect)
2544 if (bed->elf_backend_section_from_bfd_section)
2546 for (index = 0; index < maxindex; index++)
2550 hdr = i_shdrp[index];
2552 if ((*bed->elf_backend_section_from_bfd_section)
2553 (abfd, hdr, asect, &retval))
2558 if (bfd_is_abs_section (asect))
2560 if (bfd_is_com_section (asect))
2562 if (bfd_is_und_section (asect))
2568 /* given a symbol, return the bfd index for that symbol. */
2570 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2572 struct symbol_cache_entry **asym_ptr_ptr;
2574 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2576 flagword flags = asym_ptr->flags;
2578 /* When gas creates relocations against local labels, it creates its
2579 own symbol for the section, but does put the symbol into the
2580 symbol chain, so udata is 0. When the linker is generating
2581 relocatable output, this section symbol may be for one of the
2582 input sections rather than the output section. */
2583 if (asym_ptr->udata.i == 0
2584 && (flags & BSF_SECTION_SYM)
2585 && asym_ptr->section)
2589 if (asym_ptr->section->output_section != NULL)
2590 indx = asym_ptr->section->output_section->index;
2592 indx = asym_ptr->section->index;
2593 if (elf_section_syms (abfd)[indx])
2594 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
2597 idx = asym_ptr->udata.i;
2598 BFD_ASSERT (idx != 0);
2603 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2604 (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
2612 /* Copy private BFD data. This copies any program header information. */
2615 copy_private_bfd_data (ibfd, obfd)
2619 Elf_Internal_Ehdr *iehdr;
2620 struct elf_segment_map *mfirst;
2621 struct elf_segment_map **pm;
2622 Elf_Internal_Phdr *p;
2625 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2626 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2629 if (elf_tdata (ibfd)->phdr == NULL)
2632 iehdr = elf_elfheader (ibfd);
2637 c = elf_elfheader (ibfd)->e_phnum;
2638 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
2642 struct elf_segment_map *m;
2647 /* The complicated case when p_vaddr is 0 is to handle the
2648 Solaris linker, which generates a PT_INTERP section with
2649 p_vaddr and p_memsz set to 0. */
2650 for (s = ibfd->sections; s != NULL; s = s->next)
2651 if (((s->vma >= p->p_vaddr
2652 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2653 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2656 && (s->flags & SEC_HAS_CONTENTS) != 0
2657 && (bfd_vma) s->filepos >= p->p_offset
2658 && ((bfd_vma) s->filepos + s->_raw_size
2659 <= p->p_offset + p->p_filesz)))
2660 && (s->flags & SEC_ALLOC) != 0
2661 && s->output_section != NULL)
2664 m = ((struct elf_segment_map *)
2666 (sizeof (struct elf_segment_map)
2667 + (csecs - 1) * sizeof (asection *))));
2672 m->p_type = p->p_type;
2673 m->p_flags = p->p_flags;
2674 m->p_flags_valid = 1;
2675 m->p_paddr = p->p_paddr;
2676 m->p_paddr_valid = 1;
2678 m->includes_filehdr = (p->p_offset == 0
2679 && p->p_filesz >= iehdr->e_ehsize);
2681 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
2682 && (p->p_offset + p->p_filesz
2683 >= ((bfd_vma) iehdr->e_phoff
2684 + iehdr->e_phnum * iehdr->e_phentsize)));
2687 for (s = ibfd->sections; s != NULL; s = s->next)
2689 if (((s->vma >= p->p_vaddr
2690 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2691 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2694 && (s->flags & SEC_HAS_CONTENTS) != 0
2695 && (bfd_vma) s->filepos >= p->p_offset
2696 && ((bfd_vma) s->filepos + s->_raw_size
2697 <= p->p_offset + p->p_filesz)))
2698 && (s->flags & SEC_ALLOC) != 0
2699 && s->output_section != NULL)
2701 m->sections[isec] = s->output_section;
2705 BFD_ASSERT (isec == csecs);
2712 elf_tdata (obfd)->segment_map = mfirst;
2717 /* Copy private section information. This copies over the entsize
2718 field, and sometimes the info field. */
2721 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
2727 Elf_Internal_Shdr *ihdr, *ohdr;
2729 if (ibfd->xvec->flavour != bfd_target_elf_flavour
2730 || obfd->xvec->flavour != bfd_target_elf_flavour)
2733 /* Copy over private BFD data if it has not already been copied.
2734 This must be done here, rather than in the copy_private_bfd_data
2735 entry point, because the latter is called after the section
2736 contents have been set, which means that the program headers have
2737 already been worked out. */
2738 if (elf_tdata (obfd)->segment_map == NULL
2739 && elf_tdata (ibfd)->phdr != NULL)
2743 /* Only set up the segments when all the sections have been set
2745 for (s = ibfd->sections; s != NULL; s = s->next)
2746 if (s->output_section == NULL)
2750 if (! copy_private_bfd_data (ibfd, obfd))
2755 ihdr = &elf_section_data (isec)->this_hdr;
2756 ohdr = &elf_section_data (osec)->this_hdr;
2758 ohdr->sh_entsize = ihdr->sh_entsize;
2760 if (ihdr->sh_type == SHT_SYMTAB
2761 || ihdr->sh_type == SHT_DYNSYM)
2762 ohdr->sh_info = ihdr->sh_info;
2767 /* Copy private symbol information. If this symbol is in a section
2768 which we did not map into a BFD section, try to map the section
2769 index correctly. We use special macro definitions for the mapped
2770 section indices; these definitions are interpreted by the
2771 swap_out_syms function. */
2773 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2774 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2775 #define MAP_STRTAB (SHN_LORESERVE - 3)
2776 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2779 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
2785 elf_symbol_type *isym, *osym;
2787 isym = elf_symbol_from (ibfd, isymarg);
2788 osym = elf_symbol_from (obfd, osymarg);
2792 && bfd_is_abs_section (isym->symbol.section))
2796 shndx = isym->internal_elf_sym.st_shndx;
2797 if (shndx == elf_onesymtab (ibfd))
2798 shndx = MAP_ONESYMTAB;
2799 else if (shndx == elf_dynsymtab (ibfd))
2800 shndx = MAP_DYNSYMTAB;
2801 else if (shndx == elf_tdata (ibfd)->strtab_section)
2803 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
2804 shndx = MAP_SHSTRTAB;
2805 osym->internal_elf_sym.st_shndx = shndx;
2811 /* Swap out the symbols. */
2814 swap_out_syms (abfd, sttp)
2816 struct bfd_strtab_hash **sttp;
2818 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2820 if (!elf_map_symbols (abfd))
2823 /* Dump out the symtabs. */
2825 int symcount = bfd_get_symcount (abfd);
2826 asymbol **syms = bfd_get_outsymbols (abfd);
2827 struct bfd_strtab_hash *stt;
2828 Elf_Internal_Shdr *symtab_hdr;
2829 Elf_Internal_Shdr *symstrtab_hdr;
2830 char *outbound_syms;
2833 stt = _bfd_elf_stringtab_init ();
2837 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2838 symtab_hdr->sh_type = SHT_SYMTAB;
2839 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
2840 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2841 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2842 symtab_hdr->sh_addralign = bed->s->file_align;
2844 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2845 symstrtab_hdr->sh_type = SHT_STRTAB;
2847 outbound_syms = bfd_alloc (abfd,
2848 (1 + symcount) * bed->s->sizeof_sym);
2849 if (outbound_syms == NULL)
2851 symtab_hdr->contents = (PTR) outbound_syms;
2853 /* now generate the data (for "contents") */
2855 /* Fill in zeroth symbol and swap it out. */
2856 Elf_Internal_Sym sym;
2862 sym.st_shndx = SHN_UNDEF;
2863 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
2864 outbound_syms += bed->s->sizeof_sym;
2866 for (idx = 0; idx < symcount; idx++)
2868 Elf_Internal_Sym sym;
2869 bfd_vma value = syms[idx]->value;
2870 elf_symbol_type *type_ptr;
2871 flagword flags = syms[idx]->flags;
2874 if (flags & BSF_SECTION_SYM)
2875 /* Section symbols have no names. */
2879 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
2882 if (sym.st_name == (unsigned long) -1)
2886 type_ptr = elf_symbol_from (abfd, syms[idx]);
2888 if (bfd_is_com_section (syms[idx]->section))
2890 /* ELF common symbols put the alignment into the `value' field,
2891 and the size into the `size' field. This is backwards from
2892 how BFD handles it, so reverse it here. */
2893 sym.st_size = value;
2894 if (type_ptr == NULL
2895 || type_ptr->internal_elf_sym.st_value == 0)
2896 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
2898 sym.st_value = type_ptr->internal_elf_sym.st_value;
2899 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
2900 syms[idx]->section);
2904 asection *sec = syms[idx]->section;
2907 if (sec->output_section)
2909 value += sec->output_offset;
2910 sec = sec->output_section;
2913 sym.st_value = value;
2914 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2916 if (bfd_is_abs_section (sec)
2918 && type_ptr->internal_elf_sym.st_shndx != 0)
2920 /* This symbol is in a real ELF section which we did
2921 not create as a BFD section. Undo the mapping done
2922 by copy_private_symbol_data. */
2923 shndx = type_ptr->internal_elf_sym.st_shndx;
2927 shndx = elf_onesymtab (abfd);
2930 shndx = elf_dynsymtab (abfd);
2933 shndx = elf_tdata (abfd)->strtab_section;
2936 shndx = elf_tdata (abfd)->shstrtab_section;
2944 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2950 /* Writing this would be a hell of a lot easier if
2951 we had some decent documentation on bfd, and
2952 knew what to expect of the library, and what to
2953 demand of applications. For example, it
2954 appears that `objcopy' might not set the
2955 section of a symbol to be a section that is
2956 actually in the output file. */
2957 sec2 = bfd_get_section_by_name (abfd, sec->name);
2958 BFD_ASSERT (sec2 != 0);
2959 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
2960 BFD_ASSERT (shndx != -1);
2964 sym.st_shndx = shndx;
2967 if ((flags & BSF_FUNCTION) != 0)
2969 else if ((flags & BSF_OBJECT) != 0)
2974 if (bfd_is_com_section (syms[idx]->section))
2975 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
2976 else if (bfd_is_und_section (syms[idx]->section))
2977 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
2981 else if (flags & BSF_SECTION_SYM)
2982 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2983 else if (flags & BSF_FILE)
2984 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2987 int bind = STB_LOCAL;
2989 if (flags & BSF_LOCAL)
2991 else if (flags & BSF_WEAK)
2993 else if (flags & BSF_GLOBAL)
2996 sym.st_info = ELF_ST_INFO (bind, type);
3000 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3001 outbound_syms += bed->s->sizeof_sym;
3005 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
3006 symstrtab_hdr->sh_type = SHT_STRTAB;
3008 symstrtab_hdr->sh_flags = 0;
3009 symstrtab_hdr->sh_addr = 0;
3010 symstrtab_hdr->sh_entsize = 0;
3011 symstrtab_hdr->sh_link = 0;
3012 symstrtab_hdr->sh_info = 0;
3013 symstrtab_hdr->sh_addralign = 1;
3019 /* Return the number of bytes required to hold the symtab vector.
3021 Note that we base it on the count plus 1, since we will null terminate
3022 the vector allocated based on this size. However, the ELF symbol table
3023 always has a dummy entry as symbol #0, so it ends up even. */
3026 _bfd_elf_get_symtab_upper_bound (abfd)
3031 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
3033 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3034 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3040 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
3045 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3047 if (elf_dynsymtab (abfd) == 0)
3049 bfd_set_error (bfd_error_invalid_operation);
3053 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3054 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3060 _bfd_elf_get_reloc_upper_bound (abfd, asect)
3064 return (asect->reloc_count + 1) * sizeof (arelent *);
3067 /* Canonicalize the relocs. */
3070 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
3079 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols))
3082 tblptr = section->relocation;
3083 for (i = 0; i < section->reloc_count; i++)
3084 *relptr++ = tblptr++;
3088 return section->reloc_count;
3092 _bfd_elf_get_symtab (abfd, alocation)
3094 asymbol **alocation;
3096 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
3099 bfd_get_symcount (abfd) = symcount;
3104 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
3106 asymbol **alocation;
3108 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
3112 _bfd_elf_make_empty_symbol (abfd)
3115 elf_symbol_type *newsym;
3117 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3122 newsym->symbol.the_bfd = abfd;
3123 return &newsym->symbol;
3128 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
3133 bfd_symbol_info (symbol, ret);
3137 _bfd_elf_get_lineno (ignore_abfd, symbol)
3146 _bfd_elf_set_arch_mach (abfd, arch, machine)
3148 enum bfd_architecture arch;
3149 unsigned long machine;
3151 /* If this isn't the right architecture for this backend, and this
3152 isn't the generic backend, fail. */
3153 if (arch != get_elf_backend_data (abfd)->arch
3154 && arch != bfd_arch_unknown
3155 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3158 return bfd_default_set_arch_mach (abfd, arch, machine);
3161 /* Find the nearest line to a particular section and offset, for error
3165 _bfd_elf_find_nearest_line (abfd,
3176 CONST char **filename_ptr;
3177 CONST char **functionname_ptr;
3178 unsigned int *line_ptr;
3181 const char *filename;
3186 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3187 &found, filename_ptr,
3188 functionname_ptr, line_ptr,
3189 &elf_tdata (abfd)->line_info))
3194 if (symbols == NULL)
3201 for (p = symbols; *p != NULL; p++)
3205 q = (elf_symbol_type *) *p;
3207 if (bfd_get_section (&q->symbol) != section)
3210 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3215 filename = bfd_asymbol_name (&q->symbol);
3218 if (q->symbol.section == section
3219 && q->symbol.value >= low_func
3220 && q->symbol.value <= offset)
3222 func = (asymbol *) q;
3223 low_func = q->symbol.value;
3232 *filename_ptr = filename;
3233 *functionname_ptr = bfd_asymbol_name (func);
3239 _bfd_elf_sizeof_headers (abfd, reloc)
3245 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
3247 ret += get_program_header_size (abfd);
3252 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
3257 bfd_size_type count;
3259 Elf_Internal_Shdr *hdr;
3261 if (! abfd->output_has_begun
3262 && ! _bfd_elf_compute_section_file_positions (abfd,
3263 (struct bfd_link_info *) NULL))
3266 hdr = &elf_section_data (section)->this_hdr;
3268 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3270 if (bfd_write (location, 1, count, abfd) != count)
3277 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
3280 Elf_Internal_Rela *dst;
3287 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3290 Elf_Internal_Rel *dst;