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 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 **));
49 static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *));
51 /* Standard ELF hash function. Do not change this function; you will
52 cause invalid hash tables to be generated. (Well, you would if this
53 were being used yet.) */
56 CONST unsigned char *name;
62 while ((ch = *name++) != '\0')
65 if ((g = (h & 0xf0000000)) != 0)
74 /* Read a specified number of bytes at a specified offset in an ELF
75 file, into a newly allocated buffer, and return a pointer to the
79 elf_read (abfd, offset, size)
86 if ((buf = bfd_alloc (abfd, size)) == NULL)
88 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
90 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
92 if (bfd_get_error () != bfd_error_system_call)
93 bfd_set_error (bfd_error_file_truncated);
103 /* this just does initialization */
104 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
105 elf_tdata (abfd) = (struct elf_obj_tdata *)
106 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
107 if (elf_tdata (abfd) == 0)
109 /* since everything is done at close time, do we need any
116 bfd_elf_get_str_section (abfd, shindex)
118 unsigned int shindex;
120 Elf_Internal_Shdr **i_shdrp;
121 char *shstrtab = NULL;
123 unsigned int shstrtabsize;
125 i_shdrp = elf_elfsections (abfd);
126 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
129 shstrtab = (char *) i_shdrp[shindex]->contents;
130 if (shstrtab == NULL)
132 /* No cached one, attempt to read, and cache what we read. */
133 offset = i_shdrp[shindex]->sh_offset;
134 shstrtabsize = i_shdrp[shindex]->sh_size;
135 shstrtab = elf_read (abfd, offset, shstrtabsize);
136 i_shdrp[shindex]->contents = (PTR) shstrtab;
142 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
144 unsigned int shindex;
145 unsigned int strindex;
147 Elf_Internal_Shdr *hdr;
152 hdr = elf_elfsections (abfd)[shindex];
154 if (hdr->contents == NULL
155 && bfd_elf_get_str_section (abfd, shindex) == NULL)
158 return ((char *) hdr->contents) + strindex;
161 /* Make a BFD section from an ELF section. We store a pointer to the
162 BFD section in the bfd_section field of the header. */
165 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
167 Elf_Internal_Shdr *hdr;
173 if (hdr->bfd_section != NULL)
175 BFD_ASSERT (strcmp (name,
176 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
180 newsect = bfd_make_section_anyway (abfd, name);
184 newsect->filepos = hdr->sh_offset;
186 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
187 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
188 || ! bfd_set_section_alignment (abfd, newsect,
189 bfd_log2 (hdr->sh_addralign)))
192 flags = SEC_NO_FLAGS;
193 if (hdr->sh_type != SHT_NOBITS)
194 flags |= SEC_HAS_CONTENTS;
195 if ((hdr->sh_flags & SHF_ALLOC) != 0)
198 if (hdr->sh_type != SHT_NOBITS)
201 if ((hdr->sh_flags & SHF_WRITE) == 0)
202 flags |= SEC_READONLY;
203 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
205 else if ((flags & SEC_LOAD) != 0)
208 /* The debugging sections appear to be recognized only by name, not
210 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
211 || strncmp (name, ".line", sizeof ".line" - 1) == 0
212 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
213 flags |= SEC_DEBUGGING;
215 if (! bfd_set_section_flags (abfd, newsect, flags))
218 if ((flags & SEC_ALLOC) != 0)
220 Elf_Internal_Phdr *phdr;
223 /* Look through the phdrs to see if we need to adjust the lma. */
224 phdr = elf_tdata (abfd)->phdr;
225 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
227 if (phdr->p_type == PT_LOAD
228 && phdr->p_paddr != 0
229 && phdr->p_vaddr != phdr->p_paddr
230 && phdr->p_vaddr <= hdr->sh_addr
231 && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size)
233 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
239 hdr->bfd_section = newsect;
240 elf_section_data (newsect)->this_hdr = *hdr;
250 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
253 Helper functions for GDB to locate the string tables.
254 Since BFD hides string tables from callers, GDB needs to use an
255 internal hook to find them. Sun's .stabstr, in particular,
256 isn't even pointed to by the .stab section, so ordinary
257 mechanisms wouldn't work to find it, even if we had some.
260 struct elf_internal_shdr *
261 bfd_elf_find_section (abfd, name)
265 Elf_Internal_Shdr **i_shdrp;
270 i_shdrp = elf_elfsections (abfd);
273 shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
274 if (shstrtab != NULL)
276 max = elf_elfheader (abfd)->e_shnum;
277 for (i = 1; i < max; i++)
278 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
285 const char *const bfd_elf_section_type_names[] = {
286 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
287 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
288 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
291 /* ELF relocs are against symbols. If we are producing relocateable
292 output, and the reloc is against an external symbol, and nothing
293 has given us any additional addend, the resulting reloc will also
294 be against the same symbol. In such a case, we don't want to
295 change anything about the way the reloc is handled, since it will
296 all be done at final link time. Rather than put special case code
297 into bfd_perform_relocation, all the reloc types use this howto
298 function. It just short circuits the reloc if producing
299 relocateable output against an external symbol. */
302 bfd_reloc_status_type
303 bfd_elf_generic_reloc (abfd,
311 arelent *reloc_entry;
314 asection *input_section;
316 char **error_message;
318 if (output_bfd != (bfd *) NULL
319 && (symbol->flags & BSF_SECTION_SYM) == 0
320 && (! reloc_entry->howto->partial_inplace
321 || reloc_entry->addend == 0))
323 reloc_entry->address += input_section->output_offset;
327 return bfd_reloc_continue;
330 /* Print out the program headers. */
333 _bfd_elf_print_private_bfd_data (abfd, farg)
337 FILE *f = (FILE *) farg;
338 Elf_Internal_Phdr *p;
340 bfd_byte *dynbuf = NULL;
342 p = elf_tdata (abfd)->phdr;
347 fprintf (f, "\nProgram Header:\n");
348 c = elf_elfheader (abfd)->e_phnum;
349 for (i = 0; i < c; i++, p++)
356 case PT_NULL: s = "NULL"; break;
357 case PT_LOAD: s = "LOAD"; break;
358 case PT_DYNAMIC: s = "DYNAMIC"; break;
359 case PT_INTERP: s = "INTERP"; break;
360 case PT_NOTE: s = "NOTE"; break;
361 case PT_SHLIB: s = "SHLIB"; break;
362 case PT_PHDR: s = "PHDR"; break;
363 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
365 fprintf (f, "%8s off 0x", s);
366 fprintf_vma (f, p->p_offset);
367 fprintf (f, " vaddr 0x");
368 fprintf_vma (f, p->p_vaddr);
369 fprintf (f, " paddr 0x");
370 fprintf_vma (f, p->p_paddr);
371 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
372 fprintf (f, " filesz 0x");
373 fprintf_vma (f, p->p_filesz);
374 fprintf (f, " memsz 0x");
375 fprintf_vma (f, p->p_memsz);
376 fprintf (f, " flags %c%c%c",
377 (p->p_flags & PF_R) != 0 ? 'r' : '-',
378 (p->p_flags & PF_W) != 0 ? 'w' : '-',
379 (p->p_flags & PF_X) != 0 ? 'x' : '-');
380 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
381 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
386 s = bfd_get_section_by_name (abfd, ".dynamic");
391 bfd_byte *extdyn, *extdynend;
393 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *));
395 fprintf (f, "\nDynamic Section:\n");
397 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size);
400 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0,
404 elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
407 link = elf_elfsections (abfd)[elfsec]->sh_link;
409 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
410 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
413 extdynend = extdyn + s->_raw_size;
414 for (; extdyn < extdynend; extdyn += extdynsize)
416 Elf_Internal_Dyn dyn;
421 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn);
423 if (dyn.d_tag == DT_NULL)
430 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag);
434 case DT_NEEDED: name = "NEEDED"; stringp = true; break;
435 case DT_PLTRELSZ: name = "PLTRELSZ"; break;
436 case DT_PLTGOT: name = "PLTGOT"; break;
437 case DT_HASH: name = "HASH"; break;
438 case DT_STRTAB: name = "STRTAB"; break;
439 case DT_SYMTAB: name = "SYMTAB"; break;
440 case DT_RELA: name = "RELA"; break;
441 case DT_RELASZ: name = "RELASZ"; break;
442 case DT_RELAENT: name = "RELAENT"; break;
443 case DT_STRSZ: name = "STRSZ"; break;
444 case DT_SYMENT: name = "SYMENT"; break;
445 case DT_INIT: name = "INIT"; break;
446 case DT_FINI: name = "FINI"; break;
447 case DT_SONAME: name = "SONAME"; stringp = true; break;
448 case DT_RPATH: name = "RPATH"; stringp = true; break;
449 case DT_SYMBOLIC: name = "SYMBOLIC"; break;
450 case DT_REL: name = "REL"; break;
451 case DT_RELSZ: name = "RELSZ"; break;
452 case DT_RELENT: name = "RELENT"; break;
453 case DT_PLTREL: name = "PLTREL"; break;
454 case DT_DEBUG: name = "DEBUG"; break;
455 case DT_TEXTREL: name = "TEXTREL"; break;
456 case DT_JMPREL: name = "JMPREL"; break;
459 fprintf (f, " %-11s ", name);
461 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val);
466 string = bfd_elf_string_from_elf_section (abfd, link,
470 fprintf (f, "%s", string);
487 /* Display ELF-specific fields of a symbol. */
489 bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
493 bfd_print_symbol_type how;
495 FILE *file = (FILE *) filep;
498 case bfd_print_symbol_name:
499 fprintf (file, "%s", symbol->name);
501 case bfd_print_symbol_more:
502 fprintf (file, "elf ");
503 fprintf_vma (file, symbol->value);
504 fprintf (file, " %lx", (long) symbol->flags);
506 case bfd_print_symbol_all:
508 CONST char *section_name;
509 section_name = symbol->section ? symbol->section->name : "(*none*)";
510 bfd_print_symbol_vandf ((PTR) file, symbol);
511 fprintf (file, " %s\t", section_name);
512 /* Print the "other" value for a symbol. For common symbols,
513 we've already printed the size; now print the alignment.
514 For other symbols, we have no specified alignment, and
515 we've printed the address; now print the size. */
517 (bfd_is_com_section (symbol->section)
518 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
519 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
520 fprintf (file, " %s", symbol->name);
526 /* Create an entry in an ELF linker hash table. */
528 struct bfd_hash_entry *
529 _bfd_elf_link_hash_newfunc (entry, table, string)
530 struct bfd_hash_entry *entry;
531 struct bfd_hash_table *table;
534 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
536 /* Allocate the structure if it has not already been allocated by a
538 if (ret == (struct elf_link_hash_entry *) NULL)
539 ret = ((struct elf_link_hash_entry *)
540 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
541 if (ret == (struct elf_link_hash_entry *) NULL)
542 return (struct bfd_hash_entry *) ret;
544 /* Call the allocation method of the superclass. */
545 ret = ((struct elf_link_hash_entry *)
546 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
548 if (ret != (struct elf_link_hash_entry *) NULL)
550 /* Set local fields. */
554 ret->dynstr_index = 0;
556 ret->got_offset = (bfd_vma) -1;
557 ret->plt_offset = (bfd_vma) -1;
558 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0;
559 ret->type = STT_NOTYPE;
560 /* Assume that we have been called by a non-ELF symbol reader.
561 This flag is then reset by the code which reads an ELF input
562 file. This ensures that a symbol created by a non-ELF symbol
563 reader will have the flag set correctly. */
564 ret->elf_link_hash_flags = ELF_LINK_NON_ELF;
567 return (struct bfd_hash_entry *) ret;
570 /* Initialize an ELF linker hash table. */
573 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
574 struct elf_link_hash_table *table;
576 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
577 struct bfd_hash_table *,
580 table->dynamic_sections_created = false;
581 table->dynobj = NULL;
582 /* The first dynamic symbol is a dummy. */
583 table->dynsymcount = 1;
584 table->dynstr = NULL;
585 table->bucketcount = 0;
586 table->needed = NULL;
588 table->stab_info = NULL;
589 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
592 /* Create an ELF linker hash table. */
594 struct bfd_link_hash_table *
595 _bfd_elf_link_hash_table_create (abfd)
598 struct elf_link_hash_table *ret;
600 ret = ((struct elf_link_hash_table *)
601 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
602 if (ret == (struct elf_link_hash_table *) NULL)
605 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
607 bfd_release (abfd, ret);
614 /* This is a hook for the ELF emulation code in the generic linker to
615 tell the backend linker what file name to use for the DT_NEEDED
616 entry for a dynamic object. The generic linker passes name as an
617 empty string to indicate that no DT_NEEDED entry should be made. */
620 bfd_elf_set_dt_needed_name (abfd, name)
624 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
625 && bfd_get_format (abfd) == bfd_object)
626 elf_dt_name (abfd) = name;
629 /* Get the list of DT_NEEDED entries for a link. This is a hook for
630 the ELF emulation code. */
632 struct bfd_link_needed_list *
633 bfd_elf_get_needed_list (abfd, info)
635 struct bfd_link_info *info;
637 if (info->hash->creator->flavour != bfd_target_elf_flavour)
639 return elf_hash_table (info)->needed;
642 /* Get the name actually used for a dynamic object for a link. This
643 is the SONAME entry if there is one. Otherwise, it is the string
644 passed to bfd_elf_set_dt_needed_name, or it is the filename. */
647 bfd_elf_get_dt_soname (abfd)
650 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
651 && bfd_get_format (abfd) == bfd_object)
652 return elf_dt_name (abfd);
656 /* Allocate an ELF string table--force the first byte to be zero. */
658 struct bfd_strtab_hash *
659 _bfd_elf_stringtab_init ()
661 struct bfd_strtab_hash *ret;
663 ret = _bfd_stringtab_init ();
668 loc = _bfd_stringtab_add (ret, "", true, false);
669 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
670 if (loc == (bfd_size_type) -1)
672 _bfd_stringtab_free (ret);
679 /* ELF .o/exec file reading */
681 /* Create a new bfd section from an ELF section header. */
684 bfd_section_from_shdr (abfd, shindex)
686 unsigned int shindex;
688 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
689 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
690 struct elf_backend_data *bed = get_elf_backend_data (abfd);
693 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
695 switch (hdr->sh_type)
698 /* Inactive section. Throw it away. */
701 case SHT_PROGBITS: /* Normal section with contents. */
702 case SHT_DYNAMIC: /* Dynamic linking information. */
703 case SHT_NOBITS: /* .bss section. */
704 case SHT_HASH: /* .hash section. */
705 case SHT_NOTE: /* .note section. */
706 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
708 case SHT_SYMTAB: /* A symbol table */
709 if (elf_onesymtab (abfd) == shindex)
712 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
713 BFD_ASSERT (elf_onesymtab (abfd) == 0);
714 elf_onesymtab (abfd) = shindex;
715 elf_tdata (abfd)->symtab_hdr = *hdr;
716 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
717 abfd->flags |= HAS_SYMS;
719 /* Sometimes a shared object will map in the symbol table. If
720 SHF_ALLOC is set, and this is a shared object, then we also
721 treat this section as a BFD section. We can not base the
722 decision purely on SHF_ALLOC, because that flag is sometimes
723 set in a relocateable object file, which would confuse the
725 if ((hdr->sh_flags & SHF_ALLOC) != 0
726 && (abfd->flags & DYNAMIC) != 0
727 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
732 case SHT_DYNSYM: /* A dynamic symbol table */
733 if (elf_dynsymtab (abfd) == shindex)
736 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
737 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
738 elf_dynsymtab (abfd) = shindex;
739 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
740 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
741 abfd->flags |= HAS_SYMS;
743 /* Besides being a symbol table, we also treat this as a regular
744 section, so that objcopy can handle it. */
745 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
747 case SHT_STRTAB: /* A string table */
748 if (hdr->bfd_section != NULL)
750 if (ehdr->e_shstrndx == shindex)
752 elf_tdata (abfd)->shstrtab_hdr = *hdr;
753 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
759 for (i = 1; i < ehdr->e_shnum; i++)
761 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
762 if (hdr2->sh_link == shindex)
764 if (! bfd_section_from_shdr (abfd, i))
766 if (elf_onesymtab (abfd) == i)
768 elf_tdata (abfd)->strtab_hdr = *hdr;
769 elf_elfsections (abfd)[shindex] =
770 &elf_tdata (abfd)->strtab_hdr;
773 if (elf_dynsymtab (abfd) == i)
775 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
776 elf_elfsections (abfd)[shindex] = hdr =
777 &elf_tdata (abfd)->dynstrtab_hdr;
778 /* We also treat this as a regular section, so
779 that objcopy can handle it. */
782 #if 0 /* Not handling other string tables specially right now. */
783 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
784 /* We have a strtab for some random other section. */
785 newsect = (asection *) hdr2->bfd_section;
788 hdr->bfd_section = newsect;
789 hdr2 = &elf_section_data (newsect)->str_hdr;
791 elf_elfsections (abfd)[shindex] = hdr2;
797 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
801 /* *These* do a lot of work -- but build no sections! */
803 asection *target_sect;
804 Elf_Internal_Shdr *hdr2;
806 /* For some incomprehensible reason Oracle distributes
807 libraries for Solaris in which some of the objects have
808 bogus sh_link fields. It would be nice if we could just
809 reject them, but, unfortunately, some people need to use
810 them. We scan through the section headers; if we find only
811 one suitable symbol table, we clobber the sh_link to point
812 to it. I hope this doesn't break anything. */
813 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
814 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
820 for (scan = 1; scan < ehdr->e_shnum; scan++)
822 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
823 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
834 hdr->sh_link = found;
837 /* Get the symbol table. */
838 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
839 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
842 /* If this reloc section does not use the main symbol table we
843 don't treat it as a reloc section. BFD can't adequately
844 represent such a section, so at least for now, we don't
845 try. We just present it as a normal section. */
846 if (hdr->sh_link != elf_onesymtab (abfd))
847 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
849 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
851 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
852 if (target_sect == NULL)
855 if ((target_sect->flags & SEC_RELOC) == 0
856 || target_sect->reloc_count == 0)
857 hdr2 = &elf_section_data (target_sect)->rel_hdr;
860 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL);
861 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2));
862 elf_section_data (target_sect)->rel_hdr2 = hdr2;
865 elf_elfsections (abfd)[shindex] = hdr2;
866 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
867 target_sect->flags |= SEC_RELOC;
868 target_sect->relocation = NULL;
869 target_sect->rel_filepos = hdr->sh_offset;
870 abfd->flags |= HAS_RELOC;
879 /* Check for any processor-specific section types. */
881 if (bed->elf_backend_section_from_shdr)
882 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
890 /* Given an ELF section number, retrieve the corresponding BFD
894 bfd_section_from_elf_index (abfd, index)
898 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
899 if (index >= elf_elfheader (abfd)->e_shnum)
901 return elf_elfsections (abfd)[index]->bfd_section;
905 _bfd_elf_new_section_hook (abfd, sec)
909 struct bfd_elf_section_data *sdata;
911 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
914 sec->used_by_bfd = (PTR) sdata;
915 memset (sdata, 0, sizeof (*sdata));
919 /* Create a new bfd section from an ELF program header.
921 Since program segments have no names, we generate a synthetic name
922 of the form segment<NUM>, where NUM is generally the index in the
923 program header table. For segments that are split (see below) we
924 generate the names segment<NUM>a and segment<NUM>b.
926 Note that some program segments may have a file size that is different than
927 (less than) the memory size. All this means is that at execution the
928 system must allocate the amount of memory specified by the memory size,
929 but only initialize it with the first "file size" bytes read from the
930 file. This would occur for example, with program segments consisting
931 of combined data+bss.
933 To handle the above situation, this routine generates TWO bfd sections
934 for the single program segment. The first has the length specified by
935 the file size of the segment, and the second has the length specified
936 by the difference between the two sizes. In effect, the segment is split
937 into it's initialized and uninitialized parts.
942 bfd_section_from_phdr (abfd, hdr, index)
944 Elf_Internal_Phdr *hdr;
952 split = ((hdr->p_memsz > 0) &&
953 (hdr->p_filesz > 0) &&
954 (hdr->p_memsz > hdr->p_filesz));
955 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
956 name = bfd_alloc (abfd, strlen (namebuf) + 1);
959 strcpy (name, namebuf);
960 newsect = bfd_make_section (abfd, name);
963 newsect->vma = hdr->p_vaddr;
964 newsect->lma = hdr->p_paddr;
965 newsect->_raw_size = hdr->p_filesz;
966 newsect->filepos = hdr->p_offset;
967 newsect->flags |= SEC_HAS_CONTENTS;
968 if (hdr->p_type == PT_LOAD)
970 newsect->flags |= SEC_ALLOC;
971 newsect->flags |= SEC_LOAD;
972 if (hdr->p_flags & PF_X)
974 /* FIXME: all we known is that it has execute PERMISSION,
976 newsect->flags |= SEC_CODE;
979 if (!(hdr->p_flags & PF_W))
981 newsect->flags |= SEC_READONLY;
986 sprintf (namebuf, "segment%db", index);
987 name = bfd_alloc (abfd, strlen (namebuf) + 1);
990 strcpy (name, namebuf);
991 newsect = bfd_make_section (abfd, name);
994 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
995 newsect->lma = hdr->p_paddr + hdr->p_filesz;
996 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
997 if (hdr->p_type == PT_LOAD)
999 newsect->flags |= SEC_ALLOC;
1000 if (hdr->p_flags & PF_X)
1001 newsect->flags |= SEC_CODE;
1003 if (!(hdr->p_flags & PF_W))
1004 newsect->flags |= SEC_READONLY;
1010 /* Set up an ELF internal section header for a section. */
1014 elf_fake_sections (abfd, asect, failedptrarg)
1019 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1020 boolean *failedptr = (boolean *) failedptrarg;
1021 Elf_Internal_Shdr *this_hdr;
1025 /* We already failed; just get out of the bfd_map_over_sections
1030 this_hdr = &elf_section_data (asect)->this_hdr;
1032 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
1035 if (this_hdr->sh_name == (unsigned long) -1)
1041 this_hdr->sh_flags = 0;
1043 if ((asect->flags & SEC_ALLOC) != 0
1044 || asect->user_set_vma)
1045 this_hdr->sh_addr = asect->vma;
1047 this_hdr->sh_addr = 0;
1049 this_hdr->sh_offset = 0;
1050 this_hdr->sh_size = asect->_raw_size;
1051 this_hdr->sh_link = 0;
1052 this_hdr->sh_addralign = 1 << asect->alignment_power;
1053 /* The sh_entsize and sh_info fields may have been set already by
1054 copy_private_section_data. */
1056 this_hdr->bfd_section = asect;
1057 this_hdr->contents = NULL;
1059 /* FIXME: This should not be based on section names. */
1060 if (strcmp (asect->name, ".dynstr") == 0)
1061 this_hdr->sh_type = SHT_STRTAB;
1062 else if (strcmp (asect->name, ".hash") == 0)
1064 this_hdr->sh_type = SHT_HASH;
1065 this_hdr->sh_entsize = bed->s->arch_size / 8;
1067 else if (strcmp (asect->name, ".dynsym") == 0)
1069 this_hdr->sh_type = SHT_DYNSYM;
1070 this_hdr->sh_entsize = bed->s->sizeof_sym;
1072 else if (strcmp (asect->name, ".dynamic") == 0)
1074 this_hdr->sh_type = SHT_DYNAMIC;
1075 this_hdr->sh_entsize = bed->s->sizeof_dyn;
1077 else if (strncmp (asect->name, ".rela", 5) == 0
1078 && get_elf_backend_data (abfd)->use_rela_p)
1080 this_hdr->sh_type = SHT_RELA;
1081 this_hdr->sh_entsize = bed->s->sizeof_rela;
1083 else if (strncmp (asect->name, ".rel", 4) == 0
1084 && ! get_elf_backend_data (abfd)->use_rela_p)
1086 this_hdr->sh_type = SHT_REL;
1087 this_hdr->sh_entsize = bed->s->sizeof_rel;
1089 else if (strcmp (asect->name, ".note") == 0)
1090 this_hdr->sh_type = SHT_NOTE;
1091 else if (strncmp (asect->name, ".stab", 5) == 0
1092 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
1093 this_hdr->sh_type = SHT_STRTAB;
1094 else if ((asect->flags & SEC_ALLOC) != 0
1095 && (asect->flags & SEC_LOAD) != 0)
1096 this_hdr->sh_type = SHT_PROGBITS;
1097 else if ((asect->flags & SEC_ALLOC) != 0
1098 && ((asect->flags & SEC_LOAD) == 0))
1099 this_hdr->sh_type = SHT_NOBITS;
1103 this_hdr->sh_type = SHT_PROGBITS;
1106 if ((asect->flags & SEC_ALLOC) != 0)
1107 this_hdr->sh_flags |= SHF_ALLOC;
1108 if ((asect->flags & SEC_READONLY) == 0)
1109 this_hdr->sh_flags |= SHF_WRITE;
1110 if ((asect->flags & SEC_CODE) != 0)
1111 this_hdr->sh_flags |= SHF_EXECINSTR;
1113 /* Check for processor-specific section types. */
1115 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1117 if (bed->elf_backend_fake_sections)
1118 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1121 /* If the section has relocs, set up a section header for the
1122 SHT_REL[A] section. */
1123 if ((asect->flags & SEC_RELOC) != 0)
1125 Elf_Internal_Shdr *rela_hdr;
1126 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1129 rela_hdr = &elf_section_data (asect)->rel_hdr;
1130 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1136 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1138 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1140 if (rela_hdr->sh_name == (unsigned int) -1)
1145 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1146 rela_hdr->sh_entsize = (use_rela_p
1147 ? bed->s->sizeof_rela
1148 : bed->s->sizeof_rel);
1149 rela_hdr->sh_addralign = bed->s->file_align;
1150 rela_hdr->sh_flags = 0;
1151 rela_hdr->sh_addr = 0;
1152 rela_hdr->sh_size = 0;
1153 rela_hdr->sh_offset = 0;
1157 /* Assign all ELF section numbers. The dummy first section is handled here
1158 too. The link/info pointers for the standard section types are filled
1159 in here too, while we're at it. */
1162 assign_section_numbers (abfd)
1165 struct elf_obj_tdata *t = elf_tdata (abfd);
1167 unsigned int section_number;
1168 Elf_Internal_Shdr **i_shdrp;
1169 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1173 for (sec = abfd->sections; sec; sec = sec->next)
1175 struct bfd_elf_section_data *d = elf_section_data (sec);
1177 d->this_idx = section_number++;
1178 if ((sec->flags & SEC_RELOC) == 0)
1181 d->rel_idx = section_number++;
1184 t->shstrtab_section = section_number++;
1185 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1186 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1188 if (abfd->symcount > 0)
1190 t->symtab_section = section_number++;
1191 t->strtab_section = section_number++;
1194 elf_elfheader (abfd)->e_shnum = section_number;
1196 /* Set up the list of section header pointers, in agreement with the
1198 i_shdrp = ((Elf_Internal_Shdr **)
1199 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1200 if (i_shdrp == NULL)
1203 i_shdrp[0] = ((Elf_Internal_Shdr *)
1204 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1205 if (i_shdrp[0] == NULL)
1207 bfd_release (abfd, i_shdrp);
1210 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1212 elf_elfsections (abfd) = i_shdrp;
1214 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1215 if (abfd->symcount > 0)
1217 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1218 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1219 t->symtab_hdr.sh_link = t->strtab_section;
1221 for (sec = abfd->sections; sec; sec = sec->next)
1223 struct bfd_elf_section_data *d = elf_section_data (sec);
1227 i_shdrp[d->this_idx] = &d->this_hdr;
1228 if (d->rel_idx != 0)
1229 i_shdrp[d->rel_idx] = &d->rel_hdr;
1231 /* Fill in the sh_link and sh_info fields while we're at it. */
1233 /* sh_link of a reloc section is the section index of the symbol
1234 table. sh_info is the section index of the section to which
1235 the relocation entries apply. */
1236 if (d->rel_idx != 0)
1238 d->rel_hdr.sh_link = t->symtab_section;
1239 d->rel_hdr.sh_info = d->this_idx;
1242 switch (d->this_hdr.sh_type)
1246 /* A reloc section which we are treating as a normal BFD
1247 section. sh_link is the section index of the symbol
1248 table. sh_info is the section index of the section to
1249 which the relocation entries apply. We assume that an
1250 allocated reloc section uses the dynamic symbol table.
1251 FIXME: How can we be sure? */
1252 s = bfd_get_section_by_name (abfd, ".dynsym");
1254 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1256 /* We look up the section the relocs apply to by name. */
1258 if (d->this_hdr.sh_type == SHT_REL)
1262 s = bfd_get_section_by_name (abfd, name);
1264 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1268 /* We assume that a section named .stab*str is a stabs
1269 string section. We look for a section with the same name
1270 but without the trailing ``str'', and set its sh_link
1271 field to point to this section. */
1272 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1273 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1278 len = strlen (sec->name);
1279 alc = (char *) bfd_malloc (len - 2);
1282 strncpy (alc, sec->name, len - 3);
1283 alc[len - 3] = '\0';
1284 s = bfd_get_section_by_name (abfd, alc);
1288 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1290 /* This is a .stab section. */
1291 elf_section_data (s)->this_hdr.sh_entsize =
1292 4 + 2 * (bed->s->arch_size / 8);
1299 /* sh_link is the section header index of the string table
1300 used for the dynamic entries or symbol table. */
1301 s = bfd_get_section_by_name (abfd, ".dynstr");
1303 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1307 /* sh_link is the section header index of the symbol table
1308 this hash table is for. */
1309 s = bfd_get_section_by_name (abfd, ".dynsym");
1311 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1319 /* Map symbol from it's internal number to the external number, moving
1320 all local symbols to be at the head of the list. */
1323 sym_is_global (abfd, sym)
1327 /* If the backend has a special mapping, use it. */
1328 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1329 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1332 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1333 || bfd_is_und_section (bfd_get_section (sym))
1334 || bfd_is_com_section (bfd_get_section (sym)));
1338 elf_map_symbols (abfd)
1341 int symcount = bfd_get_symcount (abfd);
1342 asymbol **syms = bfd_get_outsymbols (abfd);
1343 asymbol **sect_syms;
1345 int num_globals = 0;
1346 int num_locals2 = 0;
1347 int num_globals2 = 0;
1349 int num_sections = 0;
1355 fprintf (stderr, "elf_map_symbols\n");
1359 /* Add a section symbol for each BFD section. FIXME: Is this really
1361 for (asect = abfd->sections; asect; asect = asect->next)
1363 if (max_index < asect->index)
1364 max_index = asect->index;
1368 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1369 if (sect_syms == NULL)
1371 elf_section_syms (abfd) = sect_syms;
1373 for (idx = 0; idx < symcount; idx++)
1375 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1376 && (syms[idx]->value + syms[idx]->section->vma) == 0)
1380 sec = syms[idx]->section;
1381 if (sec->owner != NULL)
1383 if (sec->owner != abfd)
1385 if (sec->output_offset != 0)
1387 sec = sec->output_section;
1388 BFD_ASSERT (sec->owner == abfd);
1390 sect_syms[sec->index] = syms[idx];
1395 for (asect = abfd->sections; asect; asect = asect->next)
1399 if (sect_syms[asect->index] != NULL)
1402 sym = bfd_make_empty_symbol (abfd);
1405 sym->the_bfd = abfd;
1406 sym->name = asect->name;
1408 /* Set the flags to 0 to indicate that this one was newly added. */
1410 sym->section = asect;
1411 sect_syms[asect->index] = sym;
1415 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1416 asect->name, (long) asect->vma, asect->index, (long) asect);
1420 /* Classify all of the symbols. */
1421 for (idx = 0; idx < symcount; idx++)
1423 if (!sym_is_global (abfd, syms[idx]))
1428 for (asect = abfd->sections; asect; asect = asect->next)
1430 if (sect_syms[asect->index] != NULL
1431 && sect_syms[asect->index]->flags == 0)
1433 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1434 if (!sym_is_global (abfd, sect_syms[asect->index]))
1438 sect_syms[asect->index]->flags = 0;
1442 /* Now sort the symbols so the local symbols are first. */
1443 new_syms = ((asymbol **)
1445 (num_locals + num_globals) * sizeof (asymbol *)));
1446 if (new_syms == NULL)
1449 for (idx = 0; idx < symcount; idx++)
1451 asymbol *sym = syms[idx];
1454 if (!sym_is_global (abfd, sym))
1457 i = num_locals + num_globals2++;
1459 sym->udata.i = i + 1;
1461 for (asect = abfd->sections; asect; asect = asect->next)
1463 if (sect_syms[asect->index] != NULL
1464 && sect_syms[asect->index]->flags == 0)
1466 asymbol *sym = sect_syms[asect->index];
1469 sym->flags = BSF_SECTION_SYM;
1470 if (!sym_is_global (abfd, sym))
1473 i = num_locals + num_globals2++;
1475 sym->udata.i = i + 1;
1479 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1481 elf_num_locals (abfd) = num_locals;
1482 elf_num_globals (abfd) = num_globals;
1486 /* Align to the maximum file alignment that could be required for any
1487 ELF data structure. */
1489 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1490 static INLINE file_ptr
1491 align_file_position (off, align)
1495 return (off + align - 1) & ~(align - 1);
1498 /* Assign a file position to a section, optionally aligning to the
1499 required section alignment. */
1502 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1503 Elf_Internal_Shdr *i_shdrp;
1511 al = i_shdrp->sh_addralign;
1513 offset = BFD_ALIGN (offset, al);
1515 i_shdrp->sh_offset = offset;
1516 if (i_shdrp->bfd_section != NULL)
1517 i_shdrp->bfd_section->filepos = offset;
1518 if (i_shdrp->sh_type != SHT_NOBITS)
1519 offset += i_shdrp->sh_size;
1523 /* Compute the file positions we are going to put the sections at, and
1524 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1525 is not NULL, this is being called by the ELF backend linker. */
1528 _bfd_elf_compute_section_file_positions (abfd, link_info)
1530 struct bfd_link_info *link_info;
1532 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1534 struct bfd_strtab_hash *strtab;
1535 Elf_Internal_Shdr *shstrtab_hdr;
1537 if (abfd->output_has_begun)
1540 /* Do any elf backend specific processing first. */
1541 if (bed->elf_backend_begin_write_processing)
1542 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1544 if (! prep_headers (abfd))
1548 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1552 if (!assign_section_numbers (abfd))
1555 /* The backend linker builds symbol table information itself. */
1556 if (link_info == NULL && abfd->symcount > 0)
1558 if (! swap_out_syms (abfd, &strtab))
1562 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1563 /* sh_name was set in prep_headers. */
1564 shstrtab_hdr->sh_type = SHT_STRTAB;
1565 shstrtab_hdr->sh_flags = 0;
1566 shstrtab_hdr->sh_addr = 0;
1567 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1568 shstrtab_hdr->sh_entsize = 0;
1569 shstrtab_hdr->sh_link = 0;
1570 shstrtab_hdr->sh_info = 0;
1571 /* sh_offset is set in assign_file_positions_except_relocs. */
1572 shstrtab_hdr->sh_addralign = 1;
1574 if (!assign_file_positions_except_relocs (abfd))
1577 if (link_info == NULL && abfd->symcount > 0)
1580 Elf_Internal_Shdr *hdr;
1582 off = elf_tdata (abfd)->next_file_pos;
1584 hdr = &elf_tdata (abfd)->symtab_hdr;
1585 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1587 hdr = &elf_tdata (abfd)->strtab_hdr;
1588 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1590 elf_tdata (abfd)->next_file_pos = off;
1592 /* Now that we know where the .strtab section goes, write it
1594 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
1595 || ! _bfd_stringtab_emit (abfd, strtab))
1597 _bfd_stringtab_free (strtab);
1600 abfd->output_has_begun = true;
1605 /* Create a mapping from a set of sections to a program segment. */
1607 static INLINE struct elf_segment_map *
1608 make_mapping (abfd, sections, from, to, phdr)
1610 asection **sections;
1615 struct elf_segment_map *m;
1619 m = ((struct elf_segment_map *)
1621 (sizeof (struct elf_segment_map)
1622 + (to - from - 1) * sizeof (asection *))));
1626 m->p_type = PT_LOAD;
1627 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1628 m->sections[i - from] = *hdrpp;
1629 m->count = to - from;
1631 if (from == 0 && phdr)
1633 /* Include the headers in the first PT_LOAD segment. */
1634 m->includes_filehdr = 1;
1635 m->includes_phdrs = 1;
1641 /* Set up a mapping from BFD sections to program segments. */
1644 map_sections_to_segments (abfd)
1647 asection **sections = NULL;
1651 struct elf_segment_map *mfirst;
1652 struct elf_segment_map **pm;
1653 struct elf_segment_map *m;
1655 unsigned int phdr_index;
1656 bfd_vma maxpagesize;
1658 boolean phdr_in_section = true;
1662 if (elf_tdata (abfd)->segment_map != NULL)
1665 if (bfd_count_sections (abfd) == 0)
1668 /* Select the allocated sections, and sort them. */
1670 sections = (asection **) bfd_malloc (bfd_count_sections (abfd)
1671 * sizeof (asection *));
1672 if (sections == NULL)
1676 for (s = abfd->sections; s != NULL; s = s->next)
1678 if ((s->flags & SEC_ALLOC) != 0)
1684 BFD_ASSERT (i <= bfd_count_sections (abfd));
1687 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
1689 /* Build the mapping. */
1694 /* If we have a .interp section, then create a PT_PHDR segment for
1695 the program headers and a PT_INTERP segment for the .interp
1697 s = bfd_get_section_by_name (abfd, ".interp");
1698 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1700 m = ((struct elf_segment_map *)
1701 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1705 m->p_type = PT_PHDR;
1706 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1707 m->p_flags = PF_R | PF_X;
1708 m->p_flags_valid = 1;
1709 m->includes_phdrs = 1;
1714 m = ((struct elf_segment_map *)
1715 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1719 m->p_type = PT_INTERP;
1727 /* Look through the sections. We put sections in the same program
1728 segment when the start of the second section can be placed within
1729 a few bytes of the end of the first section. */
1732 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1734 dynsec = bfd_get_section_by_name (abfd, ".dynamic");
1736 && (dynsec->flags & SEC_LOAD) == 0)
1739 /* Deal with -Ttext or something similar such that the first section
1740 is not adjacent to the program headers. This is an
1741 approximation, since at this point we don't know exactly how many
1742 program headers we will need. */
1745 bfd_size_type phdr_size;
1747 phdr_size = elf_tdata (abfd)->program_header_size;
1749 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr;
1750 if ((abfd->flags & D_PAGED) == 0
1751 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize)
1752 phdr_in_section = false;
1755 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
1758 boolean new_segment;
1762 /* See if this section and the last one will fit in the same
1765 if (last_hdr == NULL)
1767 /* If we don't have a segment yet, then we don't need a new
1768 one (we build the last one after this loop). */
1769 new_segment = false;
1771 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma)
1773 /* If this section has a different relation between the
1774 virtual address and the load address, then we need a new
1778 else if ((abfd->flags & D_PAGED) == 0)
1780 /* If the file is not demand paged, which means that we
1781 don't require the sections to be correctly aligned in the
1782 file, then there is no other reason for a new segment. */
1783 new_segment = false;
1785 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1788 /* If putting this section in this segment would force us to
1789 skip a page in the segment, then we need a new segment. */
1792 else if ((last_hdr->flags & SEC_LOAD) == 0
1793 && (hdr->flags & SEC_LOAD) != 0)
1795 /* We don't want to put a loadable section after a
1796 nonloadable section in the same segment. */
1800 && (hdr->flags & SEC_READONLY) == 0
1801 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1804 /* We don't want to put a writable section in a read only
1805 segment, unless they are on the same page in memory
1806 anyhow. We already know that the last section does not
1807 bring us past the current section on the page, so the
1808 only case in which the new section is not on the same
1809 page as the previous section is when the previous section
1810 ends precisely on a page boundary. */
1815 /* Otherwise, we can use the same segment. */
1816 new_segment = false;
1821 if ((hdr->flags & SEC_READONLY) == 0)
1827 /* We need a new program segment. We must create a new program
1828 header holding all the sections from phdr_index until hdr. */
1830 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
1837 if ((hdr->flags & SEC_READONLY) == 0)
1844 phdr_in_section = false;
1847 /* Create a final PT_LOAD program segment. */
1848 if (last_hdr != NULL)
1850 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section);
1858 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
1861 m = ((struct elf_segment_map *)
1862 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1866 m->p_type = PT_DYNAMIC;
1868 m->sections[0] = dynsec;
1877 elf_tdata (abfd)->segment_map = mfirst;
1881 if (sections != NULL)
1886 /* Sort sections by VMA. */
1889 elf_sort_sections (arg1, arg2)
1893 const asection *sec1 = *(const asection **) arg1;
1894 const asection *sec2 = *(const asection **) arg2;
1896 if (sec1->vma < sec2->vma)
1898 else if (sec1->vma > sec2->vma)
1901 /* Sort by LMA. Normally the LMA and the VMA will be the same, and
1902 this will do nothing. */
1903 if (sec1->lma < sec2->lma)
1905 else if (sec1->lma > sec2->lma)
1908 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
1910 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1914 return sec1->target_index - sec2->target_index;
1923 /* Sort by size, to put zero sized sections before others at the
1926 if (sec1->_raw_size < sec2->_raw_size)
1928 if (sec1->_raw_size > sec2->_raw_size)
1931 return sec1->target_index - sec2->target_index;
1934 /* Assign file positions to the sections based on the mapping from
1935 sections to segments. This function also sets up some fields in
1936 the file header, and writes out the program headers. */
1939 assign_file_positions_for_segments (abfd)
1942 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1944 struct elf_segment_map *m;
1946 Elf_Internal_Phdr *phdrs;
1948 bfd_vma filehdr_vaddr, filehdr_paddr;
1949 bfd_vma phdrs_vaddr, phdrs_paddr;
1950 Elf_Internal_Phdr *p;
1952 if (elf_tdata (abfd)->segment_map == NULL)
1954 if (! map_sections_to_segments (abfd))
1958 if (bed->elf_backend_modify_segment_map)
1960 if (! (*bed->elf_backend_modify_segment_map) (abfd))
1965 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1968 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
1969 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
1970 elf_elfheader (abfd)->e_phnum = count;
1975 /* If we already counted the number of program segments, make sure
1976 that we allocated enough space. This happens when SIZEOF_HEADERS
1977 is used in a linker script. */
1978 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
1979 if (alloc != 0 && count > alloc)
1981 ((*_bfd_error_handler)
1982 ("%s: Not enough room for program headers (allocated %u, need %u)",
1983 bfd_get_filename (abfd), alloc, count));
1984 bfd_set_error (bfd_error_bad_value);
1991 phdrs = ((Elf_Internal_Phdr *)
1992 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
1996 off = bed->s->sizeof_ehdr;
1997 off += alloc * bed->s->sizeof_phdr;
2003 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2010 /* If elf_segment_map is not from map_sections_to_segments, the
2011 sections may not be correctly ordered. */
2013 qsort (m->sections, (size_t) m->count, sizeof (asection *),
2016 p->p_type = m->p_type;
2018 if (m->p_flags_valid)
2019 p->p_flags = m->p_flags;
2023 if (p->p_type == PT_LOAD
2025 && (m->sections[0]->flags & SEC_LOAD) != 0)
2027 if ((abfd->flags & D_PAGED) != 0)
2028 off += (m->sections[0]->vma - off) % bed->maxpagesize;
2030 off += ((m->sections[0]->vma - off)
2031 % (1 << bfd_get_section_alignment (abfd, m->sections[0])));
2037 p->p_vaddr = m->sections[0]->vma;
2039 if (m->p_paddr_valid)
2040 p->p_paddr = m->p_paddr;
2041 else if (m->count == 0)
2044 p->p_paddr = m->sections[0]->lma;
2046 if (p->p_type == PT_LOAD
2047 && (abfd->flags & D_PAGED) != 0)
2048 p->p_align = bed->maxpagesize;
2049 else if (m->count == 0)
2050 p->p_align = bed->s->file_align;
2058 if (m->includes_filehdr)
2060 if (! m->p_flags_valid)
2063 p->p_filesz = bed->s->sizeof_ehdr;
2064 p->p_memsz = bed->s->sizeof_ehdr;
2067 BFD_ASSERT (p->p_type == PT_LOAD);
2069 if (! m->p_paddr_valid)
2072 if (p->p_type == PT_LOAD)
2074 filehdr_vaddr = p->p_vaddr;
2075 filehdr_paddr = p->p_paddr;
2079 if (m->includes_phdrs)
2081 if (! m->p_flags_valid)
2083 if (m->includes_filehdr)
2085 if (p->p_type == PT_LOAD)
2087 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
2088 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
2093 p->p_offset = bed->s->sizeof_ehdr;
2096 BFD_ASSERT (p->p_type == PT_LOAD);
2097 p->p_vaddr -= off - p->p_offset;
2098 if (! m->p_paddr_valid)
2099 p->p_paddr -= off - p->p_offset;
2101 if (p->p_type == PT_LOAD)
2103 phdrs_vaddr = p->p_vaddr;
2104 phdrs_paddr = p->p_paddr;
2107 p->p_filesz += alloc * bed->s->sizeof_phdr;
2108 p->p_memsz += alloc * bed->s->sizeof_phdr;
2111 if (p->p_type == PT_LOAD)
2113 if (! m->includes_filehdr && ! m->includes_phdrs)
2119 adjust = off - (p->p_offset + p->p_filesz);
2120 p->p_filesz += adjust;
2121 p->p_memsz += adjust;
2126 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
2130 bfd_size_type align;
2134 align = 1 << bfd_get_section_alignment (abfd, sec);
2136 if (p->p_type == PT_LOAD)
2140 /* The section VMA must equal the file position modulo
2142 if ((flags & SEC_ALLOC) != 0)
2144 if ((abfd->flags & D_PAGED) != 0)
2145 adjust = (sec->vma - voff) % bed->maxpagesize;
2147 adjust = (sec->vma - voff) % align;
2152 p->p_memsz += adjust;
2155 if ((flags & SEC_LOAD) != 0)
2156 p->p_filesz += adjust;
2162 if ((flags & SEC_LOAD) != 0)
2163 off += sec->_raw_size;
2164 if ((flags & SEC_ALLOC) != 0)
2165 voff += sec->_raw_size;
2168 p->p_memsz += sec->_raw_size;
2170 if ((flags & SEC_LOAD) != 0)
2171 p->p_filesz += sec->_raw_size;
2173 if (align > p->p_align)
2176 if (! m->p_flags_valid)
2179 if ((flags & SEC_CODE) != 0)
2181 if ((flags & SEC_READONLY) == 0)
2187 /* Now that we have set the section file positions, we can set up
2188 the file positions for the non PT_LOAD segments. */
2189 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
2193 if (p->p_type != PT_LOAD && m->count > 0)
2195 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
2196 p->p_offset = m->sections[0]->filepos;
2200 if (m->includes_filehdr)
2202 p->p_vaddr = filehdr_vaddr;
2203 if (! m->p_paddr_valid)
2204 p->p_paddr = filehdr_paddr;
2206 else if (m->includes_phdrs)
2208 p->p_vaddr = phdrs_vaddr;
2209 if (! m->p_paddr_valid)
2210 p->p_paddr = phdrs_paddr;
2215 /* Clear out any program headers we allocated but did not use. */
2216 for (; count < alloc; count++, p++)
2218 memset (p, 0, sizeof *p);
2219 p->p_type = PT_NULL;
2222 elf_tdata (abfd)->phdr = phdrs;
2224 elf_tdata (abfd)->next_file_pos = off;
2226 /* Write out the program headers. */
2227 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2228 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2234 /* Get the size of the program header.
2236 If this is called by the linker before any of the section VMA's are set, it
2237 can't calculate the correct value for a strange memory layout. This only
2238 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2239 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2240 data segment (exclusive of .interp and .dynamic).
2242 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2243 will be two segments. */
2245 static bfd_size_type
2246 get_program_header_size (abfd)
2251 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2253 /* We can't return a different result each time we're called. */
2254 if (elf_tdata (abfd)->program_header_size != 0)
2255 return elf_tdata (abfd)->program_header_size;
2257 if (elf_tdata (abfd)->segment_map != NULL)
2259 struct elf_segment_map *m;
2262 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2264 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2265 return elf_tdata (abfd)->program_header_size;
2268 /* Assume we will need exactly two PT_LOAD segments: one for text
2269 and one for data. */
2272 s = bfd_get_section_by_name (abfd, ".interp");
2273 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2275 /* If we have a loadable interpreter section, we need a
2276 PT_INTERP segment. In this case, assume we also need a
2277 PT_PHDR segment, although that may not be true for all
2282 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2284 /* We need a PT_DYNAMIC segment. */
2288 /* Let the backend count up any program headers it might need. */
2289 if (bed->elf_backend_additional_program_headers)
2293 a = (*bed->elf_backend_additional_program_headers) (abfd);
2299 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2300 return elf_tdata (abfd)->program_header_size;
2303 /* Work out the file positions of all the sections. This is called by
2304 _bfd_elf_compute_section_file_positions. All the section sizes and
2305 VMAs must be known before this is called.
2307 We do not consider reloc sections at this point, unless they form
2308 part of the loadable image. Reloc sections are assigned file
2309 positions in assign_file_positions_for_relocs, which is called by
2310 write_object_contents and final_link.
2312 We also don't set the positions of the .symtab and .strtab here. */
2315 assign_file_positions_except_relocs (abfd)
2318 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2319 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2320 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2322 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2324 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2326 Elf_Internal_Shdr **hdrpp;
2329 /* Start after the ELF header. */
2330 off = i_ehdrp->e_ehsize;
2332 /* We are not creating an executable, which means that we are
2333 not creating a program header, and that the actual order of
2334 the sections in the file is unimportant. */
2335 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2337 Elf_Internal_Shdr *hdr;
2340 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2342 hdr->sh_offset = -1;
2345 if (i == tdata->symtab_section
2346 || i == tdata->strtab_section)
2348 hdr->sh_offset = -1;
2352 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2358 Elf_Internal_Shdr **hdrpp;
2360 /* Assign file positions for the loaded sections based on the
2361 assignment of sections to segments. */
2362 if (! assign_file_positions_for_segments (abfd))
2365 /* Assign file positions for the other sections. */
2367 off = elf_tdata (abfd)->next_file_pos;
2368 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2370 Elf_Internal_Shdr *hdr;
2373 if (hdr->bfd_section != NULL
2374 && hdr->bfd_section->filepos != 0)
2375 hdr->sh_offset = hdr->bfd_section->filepos;
2376 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2378 ((*_bfd_error_handler)
2379 ("%s: warning: allocated section `%s' not in segment",
2380 bfd_get_filename (abfd),
2381 (hdr->bfd_section == NULL
2383 : hdr->bfd_section->name)));
2384 if ((abfd->flags & D_PAGED) != 0)
2385 off += (hdr->sh_addr - off) % bed->maxpagesize;
2387 off += (hdr->sh_addr - off) % hdr->sh_addralign;
2388 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2391 else if (hdr->sh_type == SHT_REL
2392 || hdr->sh_type == SHT_RELA
2393 || hdr == i_shdrpp[tdata->symtab_section]
2394 || hdr == i_shdrpp[tdata->strtab_section])
2395 hdr->sh_offset = -1;
2397 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2401 /* Place the section headers. */
2402 off = align_file_position (off, bed->s->file_align);
2403 i_ehdrp->e_shoff = off;
2404 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2406 elf_tdata (abfd)->next_file_pos = off;
2415 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2416 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2417 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2419 struct bfd_strtab_hash *shstrtab;
2420 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2422 i_ehdrp = elf_elfheader (abfd);
2423 i_shdrp = elf_elfsections (abfd);
2425 shstrtab = _bfd_elf_stringtab_init ();
2426 if (shstrtab == NULL)
2429 elf_shstrtab (abfd) = shstrtab;
2431 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2432 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2433 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2434 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2436 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2437 i_ehdrp->e_ident[EI_DATA] =
2438 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
2439 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2441 for (count = EI_PAD; count < EI_NIDENT; count++)
2442 i_ehdrp->e_ident[count] = 0;
2444 if ((abfd->flags & DYNAMIC) != 0)
2445 i_ehdrp->e_type = ET_DYN;
2446 else if ((abfd->flags & EXEC_P) != 0)
2447 i_ehdrp->e_type = ET_EXEC;
2449 i_ehdrp->e_type = ET_REL;
2451 switch (bfd_get_arch (abfd))
2453 case bfd_arch_unknown:
2454 i_ehdrp->e_machine = EM_NONE;
2456 case bfd_arch_sparc:
2457 if (bed->s->arch_size == 64)
2458 i_ehdrp->e_machine = EM_SPARC64;
2460 i_ehdrp->e_machine = EM_SPARC;
2463 i_ehdrp->e_machine = EM_386;
2466 i_ehdrp->e_machine = EM_68K;
2469 i_ehdrp->e_machine = EM_88K;
2472 i_ehdrp->e_machine = EM_860;
2474 case bfd_arch_mips: /* MIPS Rxxxx */
2475 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2478 i_ehdrp->e_machine = EM_PARISC;
2480 case bfd_arch_powerpc:
2481 i_ehdrp->e_machine = EM_PPC;
2483 case bfd_arch_alpha:
2484 i_ehdrp->e_machine = EM_ALPHA;
2486 /* start-sanitize-d10v */
2488 i_ehdrp->e_machine = EM_CYGNUS_D10V;
2490 /* end-sanitize-d10v */
2491 /* start-sanitize-arc */
2493 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2495 /* end-sanitize-arc */
2496 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2498 i_ehdrp->e_machine = EM_NONE;
2500 i_ehdrp->e_version = bed->s->ev_current;
2501 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2503 /* no program header, for now. */
2504 i_ehdrp->e_phoff = 0;
2505 i_ehdrp->e_phentsize = 0;
2506 i_ehdrp->e_phnum = 0;
2508 /* each bfd section is section header entry */
2509 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2510 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2512 /* if we're building an executable, we'll need a program header table */
2513 if (abfd->flags & EXEC_P)
2515 /* it all happens later */
2517 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2519 /* elf_build_phdrs() returns a (NULL-terminated) array of
2520 Elf_Internal_Phdrs */
2521 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2522 i_ehdrp->e_phoff = outbase;
2523 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2528 i_ehdrp->e_phentsize = 0;
2530 i_ehdrp->e_phoff = 0;
2533 elf_tdata (abfd)->symtab_hdr.sh_name =
2534 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2535 elf_tdata (abfd)->strtab_hdr.sh_name =
2536 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2537 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2538 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2539 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2540 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2541 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2547 /* Assign file positions for all the reloc sections which are not part
2548 of the loadable file image. */
2551 _bfd_elf_assign_file_positions_for_relocs (abfd)
2556 Elf_Internal_Shdr **shdrpp;
2558 off = elf_tdata (abfd)->next_file_pos;
2560 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2561 i < elf_elfheader (abfd)->e_shnum;
2564 Elf_Internal_Shdr *shdrp;
2567 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2568 && shdrp->sh_offset == -1)
2569 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
2572 elf_tdata (abfd)->next_file_pos = off;
2576 _bfd_elf_write_object_contents (abfd)
2579 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2580 Elf_Internal_Ehdr *i_ehdrp;
2581 Elf_Internal_Shdr **i_shdrp;
2585 if (! abfd->output_has_begun
2586 && ! _bfd_elf_compute_section_file_positions (abfd,
2587 (struct bfd_link_info *) NULL))
2590 i_shdrp = elf_elfsections (abfd);
2591 i_ehdrp = elf_elfheader (abfd);
2594 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2597 _bfd_elf_assign_file_positions_for_relocs (abfd);
2599 /* After writing the headers, we need to write the sections too... */
2600 for (count = 1; count < i_ehdrp->e_shnum; count++)
2602 if (bed->elf_backend_section_processing)
2603 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2604 if (i_shdrp[count]->contents)
2606 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2607 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2609 != i_shdrp[count]->sh_size))
2614 /* Write out the section header names. */
2615 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2616 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2619 if (bed->elf_backend_final_write_processing)
2620 (*bed->elf_backend_final_write_processing) (abfd,
2621 elf_tdata (abfd)->linker);
2623 return bed->s->write_shdrs_and_ehdr (abfd);
2626 /* given a section, search the header to find them... */
2628 _bfd_elf_section_from_bfd_section (abfd, asect)
2632 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2633 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2635 Elf_Internal_Shdr *hdr;
2636 int maxindex = elf_elfheader (abfd)->e_shnum;
2638 for (index = 0; index < maxindex; index++)
2640 hdr = i_shdrp[index];
2641 if (hdr->bfd_section == asect)
2645 if (bed->elf_backend_section_from_bfd_section)
2647 for (index = 0; index < maxindex; index++)
2651 hdr = i_shdrp[index];
2653 if ((*bed->elf_backend_section_from_bfd_section)
2654 (abfd, hdr, asect, &retval))
2659 if (bfd_is_abs_section (asect))
2661 if (bfd_is_com_section (asect))
2663 if (bfd_is_und_section (asect))
2669 /* Given a BFD symbol, return the index in the ELF symbol table, or -1
2673 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2675 asymbol **asym_ptr_ptr;
2677 asymbol *asym_ptr = *asym_ptr_ptr;
2679 flagword flags = asym_ptr->flags;
2681 /* When gas creates relocations against local labels, it creates its
2682 own symbol for the section, but does put the symbol into the
2683 symbol chain, so udata is 0. When the linker is generating
2684 relocatable output, this section symbol may be for one of the
2685 input sections rather than the output section. */
2686 if (asym_ptr->udata.i == 0
2687 && (flags & BSF_SECTION_SYM)
2688 && asym_ptr->section)
2692 if (asym_ptr->section->output_section != NULL)
2693 indx = asym_ptr->section->output_section->index;
2695 indx = asym_ptr->section->index;
2696 if (elf_section_syms (abfd)[indx])
2697 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
2700 idx = asym_ptr->udata.i;
2704 /* This case can occur when using --strip-symbol on a symbol
2705 which is used in a relocation entry. */
2706 (*_bfd_error_handler)
2707 ("%s: symbol `%s' required but not present",
2708 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr));
2709 bfd_set_error (bfd_error_no_symbols);
2716 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2717 (long) asym_ptr, asym_ptr->name, idx, flags,
2718 elf_symbol_flags (flags));
2726 /* Copy private BFD data. This copies any program header information. */
2729 copy_private_bfd_data (ibfd, obfd)
2733 Elf_Internal_Ehdr *iehdr;
2734 struct elf_segment_map *mfirst;
2735 struct elf_segment_map **pm;
2736 Elf_Internal_Phdr *p;
2739 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2740 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2743 if (elf_tdata (ibfd)->phdr == NULL)
2746 iehdr = elf_elfheader (ibfd);
2751 c = elf_elfheader (ibfd)->e_phnum;
2752 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
2756 struct elf_segment_map *m;
2761 /* The complicated case when p_vaddr is 0 is to handle the
2762 Solaris linker, which generates a PT_INTERP section with
2763 p_vaddr and p_memsz set to 0. */
2764 for (s = ibfd->sections; s != NULL; s = s->next)
2765 if (((s->vma >= p->p_vaddr
2766 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2767 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2770 && (s->flags & SEC_HAS_CONTENTS) != 0
2771 && (bfd_vma) s->filepos >= p->p_offset
2772 && ((bfd_vma) s->filepos + s->_raw_size
2773 <= p->p_offset + p->p_filesz)))
2774 && (s->flags & SEC_ALLOC) != 0
2775 && s->output_section != NULL)
2778 m = ((struct elf_segment_map *)
2780 (sizeof (struct elf_segment_map)
2781 + (csecs - 1) * sizeof (asection *))));
2786 m->p_type = p->p_type;
2787 m->p_flags = p->p_flags;
2788 m->p_flags_valid = 1;
2789 m->p_paddr = p->p_paddr;
2790 m->p_paddr_valid = 1;
2792 m->includes_filehdr = (p->p_offset == 0
2793 && p->p_filesz >= iehdr->e_ehsize);
2795 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
2796 && (p->p_offset + p->p_filesz
2797 >= ((bfd_vma) iehdr->e_phoff
2798 + iehdr->e_phnum * iehdr->e_phentsize)));
2801 for (s = ibfd->sections; s != NULL; s = s->next)
2803 if (((s->vma >= p->p_vaddr
2804 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2805 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2808 && (s->flags & SEC_HAS_CONTENTS) != 0
2809 && (bfd_vma) s->filepos >= p->p_offset
2810 && ((bfd_vma) s->filepos + s->_raw_size
2811 <= p->p_offset + p->p_filesz)))
2812 && (s->flags & SEC_ALLOC) != 0
2813 && s->output_section != NULL)
2815 m->sections[isec] = s->output_section;
2819 BFD_ASSERT (isec == csecs);
2826 elf_tdata (obfd)->segment_map = mfirst;
2831 /* Copy private section information. This copies over the entsize
2832 field, and sometimes the info field. */
2835 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
2841 Elf_Internal_Shdr *ihdr, *ohdr;
2843 if (ibfd->xvec->flavour != bfd_target_elf_flavour
2844 || obfd->xvec->flavour != bfd_target_elf_flavour)
2847 /* Copy over private BFD data if it has not already been copied.
2848 This must be done here, rather than in the copy_private_bfd_data
2849 entry point, because the latter is called after the section
2850 contents have been set, which means that the program headers have
2851 already been worked out. */
2852 if (elf_tdata (obfd)->segment_map == NULL
2853 && elf_tdata (ibfd)->phdr != NULL)
2857 /* Only set up the segments when all the sections have been set
2859 for (s = ibfd->sections; s != NULL; s = s->next)
2860 if (s->output_section == NULL)
2864 if (! copy_private_bfd_data (ibfd, obfd))
2869 ihdr = &elf_section_data (isec)->this_hdr;
2870 ohdr = &elf_section_data (osec)->this_hdr;
2872 ohdr->sh_entsize = ihdr->sh_entsize;
2874 if (ihdr->sh_type == SHT_SYMTAB
2875 || ihdr->sh_type == SHT_DYNSYM)
2876 ohdr->sh_info = ihdr->sh_info;
2881 /* Copy private symbol information. If this symbol is in a section
2882 which we did not map into a BFD section, try to map the section
2883 index correctly. We use special macro definitions for the mapped
2884 section indices; these definitions are interpreted by the
2885 swap_out_syms function. */
2887 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2888 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2889 #define MAP_STRTAB (SHN_LORESERVE - 3)
2890 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2893 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
2899 elf_symbol_type *isym, *osym;
2901 isym = elf_symbol_from (ibfd, isymarg);
2902 osym = elf_symbol_from (obfd, osymarg);
2906 && bfd_is_abs_section (isym->symbol.section))
2910 shndx = isym->internal_elf_sym.st_shndx;
2911 if (shndx == elf_onesymtab (ibfd))
2912 shndx = MAP_ONESYMTAB;
2913 else if (shndx == elf_dynsymtab (ibfd))
2914 shndx = MAP_DYNSYMTAB;
2915 else if (shndx == elf_tdata (ibfd)->strtab_section)
2917 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
2918 shndx = MAP_SHSTRTAB;
2919 osym->internal_elf_sym.st_shndx = shndx;
2925 /* Swap out the symbols. */
2928 swap_out_syms (abfd, sttp)
2930 struct bfd_strtab_hash **sttp;
2932 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2934 if (!elf_map_symbols (abfd))
2937 /* Dump out the symtabs. */
2939 int symcount = bfd_get_symcount (abfd);
2940 asymbol **syms = bfd_get_outsymbols (abfd);
2941 struct bfd_strtab_hash *stt;
2942 Elf_Internal_Shdr *symtab_hdr;
2943 Elf_Internal_Shdr *symstrtab_hdr;
2944 char *outbound_syms;
2947 stt = _bfd_elf_stringtab_init ();
2951 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2952 symtab_hdr->sh_type = SHT_SYMTAB;
2953 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
2954 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2955 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2956 symtab_hdr->sh_addralign = bed->s->file_align;
2958 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2959 symstrtab_hdr->sh_type = SHT_STRTAB;
2961 outbound_syms = bfd_alloc (abfd,
2962 (1 + symcount) * bed->s->sizeof_sym);
2963 if (outbound_syms == NULL)
2965 symtab_hdr->contents = (PTR) outbound_syms;
2967 /* now generate the data (for "contents") */
2969 /* Fill in zeroth symbol and swap it out. */
2970 Elf_Internal_Sym sym;
2976 sym.st_shndx = SHN_UNDEF;
2977 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
2978 outbound_syms += bed->s->sizeof_sym;
2980 for (idx = 0; idx < symcount; idx++)
2982 Elf_Internal_Sym sym;
2983 bfd_vma value = syms[idx]->value;
2984 elf_symbol_type *type_ptr;
2985 flagword flags = syms[idx]->flags;
2988 if (flags & BSF_SECTION_SYM)
2989 /* Section symbols have no names. */
2993 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
2996 if (sym.st_name == (unsigned long) -1)
3000 type_ptr = elf_symbol_from (abfd, syms[idx]);
3002 if (bfd_is_com_section (syms[idx]->section))
3004 /* ELF common symbols put the alignment into the `value' field,
3005 and the size into the `size' field. This is backwards from
3006 how BFD handles it, so reverse it here. */
3007 sym.st_size = value;
3008 if (type_ptr == NULL
3009 || type_ptr->internal_elf_sym.st_value == 0)
3010 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
3012 sym.st_value = type_ptr->internal_elf_sym.st_value;
3013 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
3014 syms[idx]->section);
3018 asection *sec = syms[idx]->section;
3021 if (sec->output_section)
3023 value += sec->output_offset;
3024 sec = sec->output_section;
3027 sym.st_value = value;
3028 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
3030 if (bfd_is_abs_section (sec)
3032 && type_ptr->internal_elf_sym.st_shndx != 0)
3034 /* This symbol is in a real ELF section which we did
3035 not create as a BFD section. Undo the mapping done
3036 by copy_private_symbol_data. */
3037 shndx = type_ptr->internal_elf_sym.st_shndx;
3041 shndx = elf_onesymtab (abfd);
3044 shndx = elf_dynsymtab (abfd);
3047 shndx = elf_tdata (abfd)->strtab_section;
3050 shndx = elf_tdata (abfd)->shstrtab_section;
3058 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3064 /* Writing this would be a hell of a lot easier if
3065 we had some decent documentation on bfd, and
3066 knew what to expect of the library, and what to
3067 demand of applications. For example, it
3068 appears that `objcopy' might not set the
3069 section of a symbol to be a section that is
3070 actually in the output file. */
3071 sec2 = bfd_get_section_by_name (abfd, sec->name);
3072 BFD_ASSERT (sec2 != 0);
3073 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
3074 BFD_ASSERT (shndx != -1);
3078 sym.st_shndx = shndx;
3081 if ((flags & BSF_FUNCTION) != 0)
3083 else if ((flags & BSF_OBJECT) != 0)
3088 if (bfd_is_com_section (syms[idx]->section))
3089 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type);
3090 else if (bfd_is_und_section (syms[idx]->section))
3091 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
3095 else if (flags & BSF_SECTION_SYM)
3096 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
3097 else if (flags & BSF_FILE)
3098 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
3101 int bind = STB_LOCAL;
3103 if (flags & BSF_LOCAL)
3105 else if (flags & BSF_WEAK)
3107 else if (flags & BSF_GLOBAL)
3110 sym.st_info = ELF_ST_INFO (bind, type);
3114 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
3115 outbound_syms += bed->s->sizeof_sym;
3119 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
3120 symstrtab_hdr->sh_type = SHT_STRTAB;
3122 symstrtab_hdr->sh_flags = 0;
3123 symstrtab_hdr->sh_addr = 0;
3124 symstrtab_hdr->sh_entsize = 0;
3125 symstrtab_hdr->sh_link = 0;
3126 symstrtab_hdr->sh_info = 0;
3127 symstrtab_hdr->sh_addralign = 1;
3133 /* Return the number of bytes required to hold the symtab vector.
3135 Note that we base it on the count plus 1, since we will null terminate
3136 the vector allocated based on this size. However, the ELF symbol table
3137 always has a dummy entry as symbol #0, so it ends up even. */
3140 _bfd_elf_get_symtab_upper_bound (abfd)
3145 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
3147 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3148 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3154 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
3159 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
3161 if (elf_dynsymtab (abfd) == 0)
3163 bfd_set_error (bfd_error_invalid_operation);
3167 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
3168 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
3174 _bfd_elf_get_reloc_upper_bound (abfd, asect)
3178 return (asect->reloc_count + 1) * sizeof (arelent *);
3181 /* Canonicalize the relocs. */
3184 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
3193 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols))
3196 tblptr = section->relocation;
3197 for (i = 0; i < section->reloc_count; i++)
3198 *relptr++ = tblptr++;
3202 return section->reloc_count;
3206 _bfd_elf_get_symtab (abfd, alocation)
3208 asymbol **alocation;
3210 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
3213 bfd_get_symcount (abfd) = symcount;
3218 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
3220 asymbol **alocation;
3222 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
3226 _bfd_elf_make_empty_symbol (abfd)
3229 elf_symbol_type *newsym;
3231 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3236 newsym->symbol.the_bfd = abfd;
3237 return &newsym->symbol;
3242 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
3247 bfd_symbol_info (symbol, ret);
3251 _bfd_elf_get_lineno (ignore_abfd, symbol)
3260 _bfd_elf_set_arch_mach (abfd, arch, machine)
3262 enum bfd_architecture arch;
3263 unsigned long machine;
3265 /* If this isn't the right architecture for this backend, and this
3266 isn't the generic backend, fail. */
3267 if (arch != get_elf_backend_data (abfd)->arch
3268 && arch != bfd_arch_unknown
3269 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3272 return bfd_default_set_arch_mach (abfd, arch, machine);
3275 /* Find the nearest line to a particular section and offset, for error
3279 _bfd_elf_find_nearest_line (abfd,
3290 CONST char **filename_ptr;
3291 CONST char **functionname_ptr;
3292 unsigned int *line_ptr;
3295 const char *filename;
3300 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
3301 &found, filename_ptr,
3302 functionname_ptr, line_ptr,
3303 &elf_tdata (abfd)->line_info))
3308 if (symbols == NULL)
3315 for (p = symbols; *p != NULL; p++)
3319 q = (elf_symbol_type *) *p;
3321 if (bfd_get_section (&q->symbol) != section)
3324 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3329 filename = bfd_asymbol_name (&q->symbol);
3332 if (q->symbol.section == section
3333 && q->symbol.value >= low_func
3334 && q->symbol.value <= offset)
3336 func = (asymbol *) q;
3337 low_func = q->symbol.value;
3346 *filename_ptr = filename;
3347 *functionname_ptr = bfd_asymbol_name (func);
3353 _bfd_elf_sizeof_headers (abfd, reloc)
3359 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
3361 ret += get_program_header_size (abfd);
3366 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
3371 bfd_size_type count;
3373 Elf_Internal_Shdr *hdr;
3375 if (! abfd->output_has_begun
3376 && ! _bfd_elf_compute_section_file_positions (abfd,
3377 (struct bfd_link_info *) NULL))
3380 hdr = &elf_section_data (section)->this_hdr;
3382 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3384 if (bfd_write (location, 1, count, abfd) != count)
3391 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
3394 Elf_Internal_Rela *dst;
3401 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3404 Elf_Internal_Rel *dst;
3410 /* Try to convert a non-ELF reloc into an ELF one. */
3413 _bfd_elf_validate_reloc (abfd, areloc)
3417 /* Check whether we really have an ELF howto. */
3419 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec)
3421 bfd_reloc_code_real_type code;
3422 reloc_howto_type *howto;
3424 /* Alien reloc: Try to determine its type to replace it with an
3425 equivalent ELF reloc. */
3427 if (areloc->howto->pc_relative)
3429 switch (areloc->howto->bitsize)
3432 code = BFD_RELOC_8_PCREL;
3435 code = BFD_RELOC_12_PCREL;
3438 code = BFD_RELOC_16_PCREL;
3441 code = BFD_RELOC_24_PCREL;
3444 code = BFD_RELOC_32_PCREL;
3447 code = BFD_RELOC_64_PCREL;
3453 howto = bfd_reloc_type_lookup (abfd, code);
3455 if (areloc->howto->pcrel_offset != howto->pcrel_offset)
3457 if (howto->pcrel_offset)
3458 areloc->addend += areloc->address;
3460 areloc->addend -= areloc->address; /* addend is unsigned!! */
3465 switch (areloc->howto->bitsize)
3471 code = BFD_RELOC_14;
3474 code = BFD_RELOC_16;
3477 code = BFD_RELOC_26;
3480 code = BFD_RELOC_32;
3483 code = BFD_RELOC_64;
3489 howto = bfd_reloc_type_lookup (abfd, code);
3493 areloc->howto = howto;
3501 (*_bfd_error_handler)
3502 ("%s: unsupported relocation type %s",
3503 bfd_get_filename (abfd), areloc->howto->name);
3504 bfd_set_error (bfd_error_bad_value);