1 /* ELF executable support for BFD.
2 Copyright 1993 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));
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;
340 p = elf_tdata (abfd)->phdr;
344 c = elf_elfheader (abfd)->e_phnum;
345 for (i = 0; i < c; i++, p++)
352 case PT_NULL: s = "NULL"; break;
353 case PT_LOAD: s = "LOAD"; break;
354 case PT_DYNAMIC: s = "DYNAMIC"; break;
355 case PT_INTERP: s = "INTERP"; break;
356 case PT_NOTE: s = "NOTE"; break;
357 case PT_SHLIB: s = "SHLIB"; break;
358 case PT_PHDR: s = "PHDR"; break;
359 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break;
361 fprintf (f, "%8s off 0x", s);
362 fprintf_vma (f, p->p_offset);
363 fprintf (f, " vaddr 0x");
364 fprintf_vma (f, p->p_vaddr);
365 fprintf (f, " paddr 0x");
366 fprintf_vma (f, p->p_paddr);
367 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align));
368 fprintf (f, " filesz 0x");
369 fprintf_vma (f, p->p_filesz);
370 fprintf (f, " memsz 0x");
371 fprintf_vma (f, p->p_memsz);
372 fprintf (f, " flags %c%c%c",
373 (p->p_flags & PF_R) != 0 ? 'r' : '-',
374 (p->p_flags & PF_W) != 0 ? 'w' : '-',
375 (p->p_flags & PF_X) != 0 ? 'x' : '-');
376 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0)
377 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X));
384 /* Display ELF-specific fields of a symbol. */
386 bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
390 bfd_print_symbol_type how;
392 FILE *file = (FILE *) filep;
395 case bfd_print_symbol_name:
396 fprintf (file, "%s", symbol->name);
398 case bfd_print_symbol_more:
399 fprintf (file, "elf ");
400 fprintf_vma (file, symbol->value);
401 fprintf (file, " %lx", (long) symbol->flags);
403 case bfd_print_symbol_all:
405 CONST char *section_name;
406 section_name = symbol->section ? symbol->section->name : "(*none*)";
407 bfd_print_symbol_vandf ((PTR) file, symbol);
408 fprintf (file, " %s\t", section_name);
409 /* Print the "other" value for a symbol. For common symbols,
410 we've already printed the size; now print the alignment.
411 For other symbols, we have no specified alignment, and
412 we've printed the address; now print the size. */
414 (bfd_is_com_section (symbol->section)
415 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
416 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
417 fprintf (file, " %s", symbol->name);
423 /* Create an entry in an ELF linker hash table. */
425 struct bfd_hash_entry *
426 _bfd_elf_link_hash_newfunc (entry, table, string)
427 struct bfd_hash_entry *entry;
428 struct bfd_hash_table *table;
431 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
433 /* Allocate the structure if it has not already been allocated by a
435 if (ret == (struct elf_link_hash_entry *) NULL)
436 ret = ((struct elf_link_hash_entry *)
437 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
438 if (ret == (struct elf_link_hash_entry *) NULL)
439 return (struct bfd_hash_entry *) ret;
441 /* Call the allocation method of the superclass. */
442 ret = ((struct elf_link_hash_entry *)
443 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
445 if (ret != (struct elf_link_hash_entry *) NULL)
447 /* Set local fields. */
451 ret->dynstr_index = 0;
453 ret->got_offset = (bfd_vma) -1;
454 ret->plt_offset = (bfd_vma) -1;
455 ret->type = STT_NOTYPE;
456 ret->elf_link_hash_flags = 0;
459 return (struct bfd_hash_entry *) ret;
462 /* Initialize an ELF linker hash table. */
465 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
466 struct elf_link_hash_table *table;
468 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
469 struct bfd_hash_table *,
472 table->dynamic_sections_created = false;
473 table->dynobj = NULL;
474 /* The first dynamic symbol is a dummy. */
475 table->dynsymcount = 1;
476 table->dynstr = NULL;
477 table->bucketcount = 0;
478 table->needed = NULL;
479 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
482 /* Create an ELF linker hash table. */
484 struct bfd_link_hash_table *
485 _bfd_elf_link_hash_table_create (abfd)
488 struct elf_link_hash_table *ret;
490 ret = ((struct elf_link_hash_table *)
491 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
492 if (ret == (struct elf_link_hash_table *) NULL)
495 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
497 bfd_release (abfd, ret);
504 /* This is a hook for the ELF emulation code in the generic linker to
505 tell the backend linker what file name to use for the DT_NEEDED
506 entry for a dynamic object. The generic linker passes name as an
507 empty string to indicate that no DT_NEEDED entry should be made. */
510 bfd_elf_set_dt_needed_name (abfd, name)
514 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
515 elf_dt_needed_name (abfd) = name;
518 /* Get the list of DT_NEEDED entries for a link. */
520 struct bfd_link_needed_list *
521 bfd_elf_get_needed_list (abfd, info)
523 struct bfd_link_info *info;
525 if (info->hash->creator->flavour != bfd_target_elf_flavour)
527 return elf_hash_table (info)->needed;
530 /* Allocate an ELF string table--force the first byte to be zero. */
532 struct bfd_strtab_hash *
533 _bfd_elf_stringtab_init ()
535 struct bfd_strtab_hash *ret;
537 ret = _bfd_stringtab_init ();
542 loc = _bfd_stringtab_add (ret, "", true, false);
543 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
544 if (loc == (bfd_size_type) -1)
546 _bfd_stringtab_free (ret);
553 /* ELF .o/exec file reading */
555 /* Create a new bfd section from an ELF section header. */
558 bfd_section_from_shdr (abfd, shindex)
560 unsigned int shindex;
562 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
563 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
564 struct elf_backend_data *bed = get_elf_backend_data (abfd);
567 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
569 switch (hdr->sh_type)
572 /* Inactive section. Throw it away. */
575 case SHT_PROGBITS: /* Normal section with contents. */
576 case SHT_DYNAMIC: /* Dynamic linking information. */
577 case SHT_NOBITS: /* .bss section. */
578 case SHT_HASH: /* .hash section. */
579 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
581 case SHT_SYMTAB: /* A symbol table */
582 if (elf_onesymtab (abfd) == shindex)
585 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
586 BFD_ASSERT (elf_onesymtab (abfd) == 0);
587 elf_onesymtab (abfd) = shindex;
588 elf_tdata (abfd)->symtab_hdr = *hdr;
589 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
590 abfd->flags |= HAS_SYMS;
592 /* Sometimes a shared object will map in the symbol table. If
593 SHF_ALLOC is set, and this is a shared object, then we also
594 treat this section as a BFD section. We can not base the
595 decision purely on SHF_ALLOC, because that flag is sometimes
596 set in a relocateable object file, which would confuse the
598 if ((hdr->sh_flags & SHF_ALLOC) != 0
599 && (abfd->flags & DYNAMIC) != 0
600 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
605 case SHT_DYNSYM: /* A dynamic symbol table */
606 if (elf_dynsymtab (abfd) == shindex)
609 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
610 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
611 elf_dynsymtab (abfd) = shindex;
612 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
613 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
614 abfd->flags |= HAS_SYMS;
616 /* Besides being a symbol table, we also treat this as a regular
617 section, so that objcopy can handle it. */
618 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
620 case SHT_STRTAB: /* A string table */
621 if (hdr->bfd_section != NULL)
623 if (ehdr->e_shstrndx == shindex)
625 elf_tdata (abfd)->shstrtab_hdr = *hdr;
626 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
632 for (i = 1; i < ehdr->e_shnum; i++)
634 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
635 if (hdr2->sh_link == shindex)
637 if (! bfd_section_from_shdr (abfd, i))
639 if (elf_onesymtab (abfd) == i)
641 elf_tdata (abfd)->strtab_hdr = *hdr;
642 elf_elfsections (abfd)[shindex] =
643 &elf_tdata (abfd)->strtab_hdr;
646 if (elf_dynsymtab (abfd) == i)
648 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
649 elf_elfsections (abfd)[shindex] = hdr =
650 &elf_tdata (abfd)->dynstrtab_hdr;
651 /* We also treat this as a regular section, so
652 that objcopy can handle it. */
655 #if 0 /* Not handling other string tables specially right now. */
656 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
657 /* We have a strtab for some random other section. */
658 newsect = (asection *) hdr2->bfd_section;
661 hdr->bfd_section = newsect;
662 hdr2 = &elf_section_data (newsect)->str_hdr;
664 elf_elfsections (abfd)[shindex] = hdr2;
670 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
674 /* *These* do a lot of work -- but build no sections! */
676 asection *target_sect;
677 Elf_Internal_Shdr *hdr2;
678 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
680 /* For some incomprehensible reason Oracle distributes
681 libraries for Solaris in which some of the objects have
682 bogus sh_link fields. It would be nice if we could just
683 reject them, but, unfortunately, some people need to use
684 them. We scan through the section headers; if we find only
685 one suitable symbol table, we clobber the sh_link to point
686 to it. I hope this doesn't break anything. */
687 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
688 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
694 for (scan = 1; scan < ehdr->e_shnum; scan++)
696 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
697 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
708 hdr->sh_link = found;
711 /* Get the symbol table. */
712 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
713 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
716 /* If this reloc section does not use the main symbol table we
717 don't treat it as a reloc section. BFD can't adequately
718 represent such a section, so at least for now, we don't
719 try. We just present it as a normal section. */
720 if (hdr->sh_link != elf_onesymtab (abfd))
721 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
723 /* Don't allow REL relocations on a machine that uses RELA and
725 /* @@ Actually, the generic ABI does suggest that both might be
726 used in one file. But the four ABI Processor Supplements I
727 have access to right now all specify that only one is used on
728 each of those architectures. It's conceivable that, e.g., a
729 bunch of absolute 32-bit relocs might be more compact in REL
730 form even on a RELA machine... */
731 BFD_ASSERT (use_rela_p
732 ? (hdr->sh_type == SHT_RELA
733 && hdr->sh_entsize == bed->s->sizeof_rela)
734 : (hdr->sh_type == SHT_REL
735 && hdr->sh_entsize == bed->s->sizeof_rel));
737 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
739 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
740 if (target_sect == NULL)
743 hdr2 = &elf_section_data (target_sect)->rel_hdr;
745 elf_elfsections (abfd)[shindex] = hdr2;
746 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
747 target_sect->flags |= SEC_RELOC;
748 target_sect->relocation = NULL;
749 target_sect->rel_filepos = hdr->sh_offset;
750 abfd->flags |= HAS_RELOC;
762 /* Check for any processor-specific section types. */
764 if (bed->elf_backend_section_from_shdr)
765 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
773 /* Given an ELF section number, retrieve the corresponding BFD
777 bfd_section_from_elf_index (abfd, index)
781 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
782 if (index >= elf_elfheader (abfd)->e_shnum)
784 return elf_elfsections (abfd)[index]->bfd_section;
788 _bfd_elf_new_section_hook (abfd, sec)
792 struct bfd_elf_section_data *sdata;
794 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
797 sec->used_by_bfd = (PTR) sdata;
798 memset (sdata, 0, sizeof (*sdata));
802 /* Create a new bfd section from an ELF program header.
804 Since program segments have no names, we generate a synthetic name
805 of the form segment<NUM>, where NUM is generally the index in the
806 program header table. For segments that are split (see below) we
807 generate the names segment<NUM>a and segment<NUM>b.
809 Note that some program segments may have a file size that is different than
810 (less than) the memory size. All this means is that at execution the
811 system must allocate the amount of memory specified by the memory size,
812 but only initialize it with the first "file size" bytes read from the
813 file. This would occur for example, with program segments consisting
814 of combined data+bss.
816 To handle the above situation, this routine generates TWO bfd sections
817 for the single program segment. The first has the length specified by
818 the file size of the segment, and the second has the length specified
819 by the difference between the two sizes. In effect, the segment is split
820 into it's initialized and uninitialized parts.
825 bfd_section_from_phdr (abfd, hdr, index)
827 Elf_Internal_Phdr *hdr;
835 split = ((hdr->p_memsz > 0) &&
836 (hdr->p_filesz > 0) &&
837 (hdr->p_memsz > hdr->p_filesz));
838 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
839 name = bfd_alloc (abfd, strlen (namebuf) + 1);
842 strcpy (name, namebuf);
843 newsect = bfd_make_section (abfd, name);
846 newsect->vma = hdr->p_vaddr;
847 newsect->lma = hdr->p_paddr;
848 newsect->_raw_size = hdr->p_filesz;
849 newsect->filepos = hdr->p_offset;
850 newsect->flags |= SEC_HAS_CONTENTS;
851 if (hdr->p_type == PT_LOAD)
853 newsect->flags |= SEC_ALLOC;
854 newsect->flags |= SEC_LOAD;
855 if (hdr->p_flags & PF_X)
857 /* FIXME: all we known is that it has execute PERMISSION,
859 newsect->flags |= SEC_CODE;
862 if (!(hdr->p_flags & PF_W))
864 newsect->flags |= SEC_READONLY;
869 sprintf (namebuf, "segment%db", index);
870 name = bfd_alloc (abfd, strlen (namebuf) + 1);
873 strcpy (name, namebuf);
874 newsect = bfd_make_section (abfd, name);
877 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
878 newsect->lma = hdr->p_paddr + hdr->p_filesz;
879 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
880 if (hdr->p_type == PT_LOAD)
882 newsect->flags |= SEC_ALLOC;
883 if (hdr->p_flags & PF_X)
884 newsect->flags |= SEC_CODE;
886 if (!(hdr->p_flags & PF_W))
887 newsect->flags |= SEC_READONLY;
893 /* Set up an ELF internal section header for a section. */
897 elf_fake_sections (abfd, asect, failedptrarg)
902 struct elf_backend_data *bed = get_elf_backend_data (abfd);
903 boolean *failedptr = (boolean *) failedptrarg;
904 Elf_Internal_Shdr *this_hdr;
908 /* We already failed; just get out of the bfd_map_over_sections
913 this_hdr = &elf_section_data (asect)->this_hdr;
915 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
918 if (this_hdr->sh_name == (unsigned long) -1)
924 this_hdr->sh_flags = 0;
926 if ((asect->flags & SEC_ALLOC) != 0)
927 this_hdr->sh_addr = asect->vma;
929 this_hdr->sh_addr = 0;
931 this_hdr->sh_offset = 0;
932 this_hdr->sh_size = asect->_raw_size;
933 this_hdr->sh_link = 0;
934 this_hdr->sh_addralign = 1 << asect->alignment_power;
935 /* The sh_entsize and sh_info fields may have been set already by
936 copy_private_section_data. */
938 this_hdr->bfd_section = asect;
939 this_hdr->contents = NULL;
941 /* FIXME: This should not be based on section names. */
942 if (strcmp (asect->name, ".dynstr") == 0)
943 this_hdr->sh_type = SHT_STRTAB;
944 else if (strcmp (asect->name, ".hash") == 0)
946 this_hdr->sh_type = SHT_HASH;
947 this_hdr->sh_entsize = bed->s->arch_size / 8;
949 else if (strcmp (asect->name, ".dynsym") == 0)
951 this_hdr->sh_type = SHT_DYNSYM;
952 this_hdr->sh_entsize = bed->s->sizeof_sym;
954 else if (strcmp (asect->name, ".dynamic") == 0)
956 this_hdr->sh_type = SHT_DYNAMIC;
957 this_hdr->sh_entsize = bed->s->sizeof_dyn;
959 else if (strncmp (asect->name, ".rela", 5) == 0
960 && get_elf_backend_data (abfd)->use_rela_p)
962 this_hdr->sh_type = SHT_RELA;
963 this_hdr->sh_entsize = bed->s->sizeof_rela;
965 else if (strncmp (asect->name, ".rel", 4) == 0
966 && ! get_elf_backend_data (abfd)->use_rela_p)
968 this_hdr->sh_type = SHT_REL;
969 this_hdr->sh_entsize = bed->s->sizeof_rel;
971 else if (strcmp (asect->name, ".note") == 0)
972 this_hdr->sh_type = SHT_NOTE;
973 else if (strncmp (asect->name, ".stab", 5) == 0
974 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
975 this_hdr->sh_type = SHT_STRTAB;
976 else if ((asect->flags & SEC_ALLOC) != 0
977 && (asect->flags & SEC_LOAD) != 0)
978 this_hdr->sh_type = SHT_PROGBITS;
979 else if ((asect->flags & SEC_ALLOC) != 0
980 && ((asect->flags & SEC_LOAD) == 0))
981 this_hdr->sh_type = SHT_NOBITS;
985 this_hdr->sh_type = SHT_PROGBITS;
988 if ((asect->flags & SEC_ALLOC) != 0)
989 this_hdr->sh_flags |= SHF_ALLOC;
990 if ((asect->flags & SEC_READONLY) == 0)
991 this_hdr->sh_flags |= SHF_WRITE;
992 if ((asect->flags & SEC_CODE) != 0)
993 this_hdr->sh_flags |= SHF_EXECINSTR;
995 /* Check for processor-specific section types. */
997 struct elf_backend_data *bed = get_elf_backend_data (abfd);
999 if (bed->elf_backend_fake_sections)
1000 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1003 /* If the section has relocs, set up a section header for the
1004 SHT_REL[A] section. */
1005 if ((asect->flags & SEC_RELOC) != 0)
1007 Elf_Internal_Shdr *rela_hdr;
1008 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1011 rela_hdr = &elf_section_data (asect)->rel_hdr;
1012 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1018 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1020 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1022 if (rela_hdr->sh_name == (unsigned int) -1)
1027 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1028 rela_hdr->sh_entsize = (use_rela_p
1029 ? bed->s->sizeof_rela
1030 : bed->s->sizeof_rel);
1031 rela_hdr->sh_addralign = bed->s->file_align;
1032 rela_hdr->sh_flags = 0;
1033 rela_hdr->sh_addr = 0;
1034 rela_hdr->sh_size = 0;
1035 rela_hdr->sh_offset = 0;
1039 /* Assign all ELF section numbers. The dummy first section is handled here
1040 too. The link/info pointers for the standard section types are filled
1041 in here too, while we're at it. */
1044 assign_section_numbers (abfd)
1047 struct elf_obj_tdata *t = elf_tdata (abfd);
1049 unsigned int section_number;
1050 Elf_Internal_Shdr **i_shdrp;
1051 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1055 for (sec = abfd->sections; sec; sec = sec->next)
1057 struct bfd_elf_section_data *d = elf_section_data (sec);
1059 d->this_idx = section_number++;
1060 if ((sec->flags & SEC_RELOC) == 0)
1063 d->rel_idx = section_number++;
1066 t->shstrtab_section = section_number++;
1067 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1068 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1070 if (abfd->symcount > 0)
1072 t->symtab_section = section_number++;
1073 t->strtab_section = section_number++;
1076 elf_elfheader (abfd)->e_shnum = section_number;
1078 /* Set up the list of section header pointers, in agreement with the
1080 i_shdrp = ((Elf_Internal_Shdr **)
1081 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1082 if (i_shdrp == NULL)
1085 i_shdrp[0] = ((Elf_Internal_Shdr *)
1086 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1087 if (i_shdrp[0] == NULL)
1089 bfd_release (abfd, i_shdrp);
1092 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1094 elf_elfsections (abfd) = i_shdrp;
1096 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1097 if (abfd->symcount > 0)
1099 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1100 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1101 t->symtab_hdr.sh_link = t->strtab_section;
1103 for (sec = abfd->sections; sec; sec = sec->next)
1105 struct bfd_elf_section_data *d = elf_section_data (sec);
1109 i_shdrp[d->this_idx] = &d->this_hdr;
1110 if (d->rel_idx != 0)
1111 i_shdrp[d->rel_idx] = &d->rel_hdr;
1113 /* Fill in the sh_link and sh_info fields while we're at it. */
1115 /* sh_link of a reloc section is the section index of the symbol
1116 table. sh_info is the section index of the section to which
1117 the relocation entries apply. */
1118 if (d->rel_idx != 0)
1120 d->rel_hdr.sh_link = t->symtab_section;
1121 d->rel_hdr.sh_info = d->this_idx;
1124 switch (d->this_hdr.sh_type)
1128 /* A reloc section which we are treating as a normal BFD
1129 section. sh_link is the section index of the symbol
1130 table. sh_info is the section index of the section to
1131 which the relocation entries apply. We assume that an
1132 allocated reloc section uses the dynamic symbol table.
1133 FIXME: How can we be sure? */
1134 s = bfd_get_section_by_name (abfd, ".dynsym");
1136 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1138 /* We look up the section the relocs apply to by name. */
1140 if (d->this_hdr.sh_type == SHT_REL)
1144 s = bfd_get_section_by_name (abfd, name);
1146 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1150 /* We assume that a section named .stab*str is a stabs
1151 string section. We look for a section with the same name
1152 but without the trailing ``str'', and set its sh_link
1153 field to point to this section. */
1154 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1155 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1160 len = strlen (sec->name);
1161 alc = (char *) malloc (len - 2);
1164 bfd_set_error (bfd_error_no_memory);
1167 strncpy (alc, sec->name, len - 3);
1168 alc[len - 3] = '\0';
1169 s = bfd_get_section_by_name (abfd, alc);
1173 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1175 /* This is a .stab section. */
1176 elf_section_data (s)->this_hdr.sh_entsize =
1177 4 + 2 * (bed->s->arch_size / 8);
1184 /* sh_link is the section header index of the string table
1185 used for the dynamic entries or symbol table. */
1186 s = bfd_get_section_by_name (abfd, ".dynstr");
1188 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1192 /* sh_link is the section header index of the symbol table
1193 this hash table is for. */
1194 s = bfd_get_section_by_name (abfd, ".dynsym");
1196 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1204 /* Map symbol from it's internal number to the external number, moving
1205 all local symbols to be at the head of the list. */
1208 sym_is_global (abfd, sym)
1212 /* If the backend has a special mapping, use it. */
1213 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1214 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1217 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1218 || bfd_is_und_section (bfd_get_section (sym))
1219 || bfd_is_com_section (bfd_get_section (sym)));
1223 elf_map_symbols (abfd)
1226 int symcount = bfd_get_symcount (abfd);
1227 asymbol **syms = bfd_get_outsymbols (abfd);
1228 asymbol **sect_syms;
1230 int num_globals = 0;
1231 int num_locals2 = 0;
1232 int num_globals2 = 0;
1234 int num_sections = 0;
1240 fprintf (stderr, "elf_map_symbols\n");
1244 /* Add a section symbol for each BFD section. FIXME: Is this really
1246 for (asect = abfd->sections; asect; asect = asect->next)
1248 if (max_index < asect->index)
1249 max_index = asect->index;
1253 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1254 if (sect_syms == NULL)
1256 elf_section_syms (abfd) = sect_syms;
1258 for (idx = 0; idx < symcount; idx++)
1260 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1261 && (syms[idx]->value + syms[idx]->section->vma) == 0)
1265 sec = syms[idx]->section;
1266 if (sec->owner != NULL)
1268 if (sec->owner != abfd)
1270 if (sec->output_offset != 0)
1272 sec = sec->output_section;
1273 BFD_ASSERT (sec->owner == abfd);
1275 sect_syms[sec->index] = syms[idx];
1280 for (asect = abfd->sections; asect; asect = asect->next)
1284 if (sect_syms[asect->index] != NULL)
1287 sym = bfd_make_empty_symbol (abfd);
1290 sym->the_bfd = abfd;
1291 sym->name = asect->name;
1293 /* Set the flags to 0 to indicate that this one was newly added. */
1295 sym->section = asect;
1296 sect_syms[asect->index] = sym;
1300 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1301 asect->name, (long) asect->vma, asect->index, (long) asect);
1305 /* Classify all of the symbols. */
1306 for (idx = 0; idx < symcount; idx++)
1308 if (!sym_is_global (abfd, syms[idx]))
1313 for (asect = abfd->sections; asect; asect = asect->next)
1315 if (sect_syms[asect->index] != NULL
1316 && sect_syms[asect->index]->flags == 0)
1318 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1319 if (!sym_is_global (abfd, sect_syms[asect->index]))
1323 sect_syms[asect->index]->flags = 0;
1327 /* Now sort the symbols so the local symbols are first. */
1328 new_syms = ((asymbol **)
1330 (num_locals + num_globals) * sizeof (asymbol *)));
1331 if (new_syms == NULL)
1334 for (idx = 0; idx < symcount; idx++)
1336 asymbol *sym = syms[idx];
1339 if (!sym_is_global (abfd, sym))
1342 i = num_locals + num_globals2++;
1344 sym->udata.i = i + 1;
1346 for (asect = abfd->sections; asect; asect = asect->next)
1348 if (sect_syms[asect->index] != NULL
1349 && sect_syms[asect->index]->flags == 0)
1351 asymbol *sym = sect_syms[asect->index];
1354 sym->flags = BSF_SECTION_SYM;
1355 if (!sym_is_global (abfd, sym))
1358 i = num_locals + num_globals2++;
1360 sym->udata.i = i + 1;
1364 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1366 elf_num_locals (abfd) = num_locals;
1367 elf_num_globals (abfd) = num_globals;
1371 /* Align to the maximum file alignment that could be required for any
1372 ELF data structure. */
1374 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1375 static INLINE file_ptr
1376 align_file_position (off, align)
1380 return (off + align - 1) & ~(align - 1);
1383 /* Assign a file position to a section, optionally aligning to the
1384 required section alignment. */
1387 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1388 Elf_Internal_Shdr *i_shdrp;
1396 al = i_shdrp->sh_addralign;
1398 offset = BFD_ALIGN (offset, al);
1400 i_shdrp->sh_offset = offset;
1401 if (i_shdrp->bfd_section != NULL)
1402 i_shdrp->bfd_section->filepos = offset;
1403 if (i_shdrp->sh_type != SHT_NOBITS)
1404 offset += i_shdrp->sh_size;
1408 /* Compute the file positions we are going to put the sections at, and
1409 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1410 is not NULL, this is being called by the ELF backend linker. */
1413 _bfd_elf_compute_section_file_positions (abfd, link_info)
1415 struct bfd_link_info *link_info;
1417 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1419 struct bfd_strtab_hash *strtab;
1420 Elf_Internal_Shdr *shstrtab_hdr;
1422 if (abfd->output_has_begun)
1425 /* Do any elf backend specific processing first. */
1426 if (bed->elf_backend_begin_write_processing)
1427 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1429 if (! prep_headers (abfd))
1433 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1437 if (!assign_section_numbers (abfd))
1440 /* The backend linker builds symbol table information itself. */
1441 if (link_info == NULL && abfd->symcount > 0)
1443 if (! swap_out_syms (abfd, &strtab))
1447 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1448 /* sh_name was set in prep_headers. */
1449 shstrtab_hdr->sh_type = SHT_STRTAB;
1450 shstrtab_hdr->sh_flags = 0;
1451 shstrtab_hdr->sh_addr = 0;
1452 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1453 shstrtab_hdr->sh_entsize = 0;
1454 shstrtab_hdr->sh_link = 0;
1455 shstrtab_hdr->sh_info = 0;
1456 /* sh_offset is set in assign_file_positions_except_relocs. */
1457 shstrtab_hdr->sh_addralign = 1;
1459 if (!assign_file_positions_except_relocs (abfd))
1462 if (link_info == NULL && abfd->symcount > 0)
1465 Elf_Internal_Shdr *hdr;
1467 off = elf_tdata (abfd)->next_file_pos;
1469 hdr = &elf_tdata (abfd)->symtab_hdr;
1470 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1472 hdr = &elf_tdata (abfd)->strtab_hdr;
1473 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1475 elf_tdata (abfd)->next_file_pos = off;
1477 /* Now that we know where the .strtab section goes, write it
1479 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
1480 || ! _bfd_stringtab_emit (abfd, strtab))
1482 _bfd_stringtab_free (strtab);
1485 abfd->output_has_begun = true;
1490 /* Create a mapping from a set of sections to a program segment. */
1492 static INLINE struct elf_segment_map *
1493 make_mapping (abfd, sections, from, to)
1495 asection **sections;
1499 struct elf_segment_map *m;
1503 m = ((struct elf_segment_map *)
1505 (sizeof (struct elf_segment_map)
1506 + (to - from - 1) * sizeof (asection *))));
1510 m->p_type = PT_LOAD;
1511 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1512 m->sections[i - from] = *hdrpp;
1513 m->count = to - from;
1517 /* Include the headers in the first PT_LOAD segment. */
1518 m->includes_filehdr = 1;
1519 m->includes_phdrs = 1;
1525 /* Set up a mapping from BFD sections to program segments. */
1528 map_sections_to_segments (abfd)
1531 asection **sections = NULL;
1535 struct elf_segment_map *mfirst;
1536 struct elf_segment_map **pm;
1537 struct elf_segment_map *m;
1539 unsigned int phdr_index;
1540 bfd_vma maxpagesize;
1543 if (elf_tdata (abfd)->segment_map != NULL)
1546 if (bfd_count_sections (abfd) == 0)
1549 /* Select the allocated sections, and sort them. */
1551 sections = (asection **) malloc (bfd_count_sections (abfd)
1552 * sizeof (asection *));
1553 if (sections == NULL)
1555 bfd_set_error (bfd_error_no_memory);
1560 for (s = abfd->sections; s != NULL; s = s->next)
1562 if ((s->flags & SEC_ALLOC) != 0)
1568 BFD_ASSERT (i <= bfd_count_sections (abfd));
1571 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
1573 /* Build the mapping. */
1578 /* If we have a .interp section, then create a PT_PHDR segment for
1579 the program headers and a PT_INTERP segment for the .interp
1581 s = bfd_get_section_by_name (abfd, ".interp");
1582 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1584 m = ((struct elf_segment_map *)
1585 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1589 m->p_type = PT_PHDR;
1590 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1591 m->p_flags = PF_R | PF_X;
1592 m->p_flags_valid = 1;
1593 m->includes_phdrs = 1;
1598 m = ((struct elf_segment_map *)
1599 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1603 m->p_type = PT_INTERP;
1611 /* Look through the sections. We put sections in the same program
1612 segment when the start of the second section can be placed within
1613 a few bytes of the end of the first section. */
1616 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1617 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
1623 /* See if this section and the last one will fit in the same
1625 if (last_hdr == NULL
1626 || ((BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1628 && ((last_hdr->flags & SEC_LOAD) != 0
1629 || (hdr->flags & SEC_LOAD) == 0)))
1635 /* This section won't fit in the program segment. We must
1636 create a new program header holding all the sections from
1637 phdr_index until hdr. */
1639 m = make_mapping (abfd, sections, phdr_index, i);
1650 /* Create a final PT_LOAD program segment. */
1651 if (last_hdr != NULL)
1653 m = make_mapping (abfd, sections, phdr_index, i);
1661 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
1662 s = bfd_get_section_by_name (abfd, ".dynamic");
1663 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1665 m = ((struct elf_segment_map *)
1666 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1670 m->p_type = PT_DYNAMIC;
1681 elf_tdata (abfd)->segment_map = mfirst;
1685 if (sections != NULL)
1690 /* Sort sections by VMA. */
1693 elf_sort_sections (arg1, arg2)
1697 const asection *sec1 = *(const asection **) arg1;
1698 const asection *sec2 = *(const asection **) arg2;
1700 if (sec1->vma < sec2->vma)
1702 else if (sec1->vma > sec2->vma)
1705 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
1707 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1711 return sec1->target_index - sec2->target_index;
1720 /* Sort by size, to put zero sized sections before others at the
1723 if (sec1->_raw_size < sec2->_raw_size)
1725 if (sec1->_raw_size > sec2->_raw_size)
1728 return sec1->target_index - sec2->target_index;
1731 /* Assign file positions to the sections based on the mapping from
1732 sections to segments. This function also sets up some fields in
1733 the file header, and writes out the program headers. */
1736 assign_file_positions_for_segments (abfd)
1739 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1741 struct elf_segment_map *m;
1743 Elf_Internal_Phdr *phdrs;
1745 bfd_vma filehdr_vaddr, filehdr_paddr;
1746 bfd_vma phdrs_vaddr, phdrs_paddr;
1747 Elf_Internal_Phdr *p;
1749 if (elf_tdata (abfd)->segment_map == NULL)
1751 if (! map_sections_to_segments (abfd))
1756 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1759 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
1760 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
1761 elf_elfheader (abfd)->e_phnum = count;
1766 /* Let the backend count up any program headers it might need. */
1767 if (bed->elf_backend_create_program_headers)
1768 count = ((*bed->elf_backend_create_program_headers)
1769 (abfd, (Elf_Internal_Phdr *) NULL, count));
1771 /* If we already counted the number of program segments, make sure
1772 that we allocated enough space. This happens when SIZEOF_HEADERS
1773 is used in a linker script. */
1774 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
1775 if (alloc != 0 && count > alloc)
1777 ((*_bfd_error_handler)
1778 ("%s: Not enough room for program headers (allocated %u, need %u)",
1779 bfd_get_filename (abfd), alloc, count));
1780 bfd_set_error (bfd_error_bad_value);
1787 phdrs = ((Elf_Internal_Phdr *)
1788 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
1792 off = bed->s->sizeof_ehdr;
1793 off += alloc * bed->s->sizeof_phdr;
1799 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
1806 p->p_type = m->p_type;
1808 if (m->p_flags_valid)
1809 p->p_flags = m->p_flags;
1811 if (p->p_type == PT_LOAD && m->count > 0)
1812 off += (m->sections[0]->vma - off) % bed->maxpagesize;
1817 p->p_vaddr = m->sections[0]->vma;
1819 if (m->p_paddr_valid)
1820 p->p_paddr = m->p_paddr;
1821 else if (m->count == 0)
1824 p->p_paddr = m->sections[0]->lma;
1826 if (p->p_type == PT_LOAD)
1827 p->p_align = bed->maxpagesize;
1828 else if (m->count == 0)
1829 p->p_align = bed->s->file_align;
1837 if (m->includes_filehdr)
1840 p->p_filesz = bed->s->sizeof_ehdr;
1841 p->p_memsz = bed->s->sizeof_ehdr;
1844 BFD_ASSERT (p->p_type == PT_LOAD);
1846 if (! m->p_paddr_valid)
1849 if (p->p_type == PT_LOAD)
1851 filehdr_vaddr = p->p_vaddr;
1852 filehdr_paddr = p->p_paddr;
1856 if (m->includes_phdrs)
1858 if (m->includes_filehdr)
1860 if (p->p_type == PT_LOAD)
1862 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
1863 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr;
1868 p->p_offset = bed->s->sizeof_ehdr;
1871 BFD_ASSERT (p->p_type == PT_LOAD);
1872 p->p_vaddr -= off - p->p_offset;
1873 if (! m->p_paddr_valid)
1874 p->p_paddr -= off - p->p_offset;
1876 if (p->p_type == PT_LOAD)
1878 phdrs_vaddr = p->p_vaddr;
1879 phdrs_paddr = p->p_paddr;
1882 p->p_filesz += alloc * bed->s->sizeof_phdr;
1883 p->p_memsz += alloc * bed->s->sizeof_phdr;
1886 if (p->p_type == PT_LOAD)
1888 if (! m->includes_filehdr && ! m->includes_phdrs)
1894 adjust = off - (p->p_offset + p->p_filesz);
1895 p->p_filesz += adjust;
1896 p->p_memsz += adjust;
1900 if (! m->p_flags_valid)
1902 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
1906 bfd_size_type align;
1911 if (p->p_type == PT_LOAD)
1915 /* The section VMA must equal the file position modulo
1917 adjust = (sec->vma - off) % bed->maxpagesize;
1922 p->p_memsz += adjust;
1923 if ((flags & SEC_LOAD) != 0)
1924 p->p_filesz += adjust;
1930 if ((flags & SEC_LOAD) != 0)
1931 off += sec->_raw_size;
1934 p->p_memsz += sec->_raw_size;
1936 if ((flags & SEC_LOAD) != 0)
1937 p->p_filesz += sec->_raw_size;
1939 align = 1 << bfd_get_section_alignment (abfd, sec);
1940 if (align > p->p_align)
1943 if (! m->p_flags_valid)
1945 if ((flags & SEC_CODE) != 0)
1947 if ((flags & SEC_READONLY) == 0)
1953 /* Now that we have set the section file positions, we can set up
1954 the file positions for the non PT_LOAD segments. */
1955 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
1959 if (p->p_type != PT_LOAD && m->count > 0)
1961 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs);
1962 p->p_offset = m->sections[0]->filepos;
1966 if (m->includes_filehdr)
1968 p->p_vaddr = filehdr_vaddr;
1969 if (! m->p_paddr_valid)
1970 p->p_paddr = filehdr_paddr;
1972 else if (m->includes_phdrs)
1974 p->p_vaddr = phdrs_vaddr;
1975 if (! m->p_paddr_valid)
1976 p->p_paddr = phdrs_paddr;
1981 /* Let the backend set up any program headers it might need. */
1982 if (bed->elf_backend_create_program_headers)
1983 count = ((*bed->elf_backend_create_program_headers)
1984 (abfd, phdrs, count));
1986 /* Clear out any program headers we allocated but did not use. */
1987 for (; count < alloc; count++, p++)
1989 memset (p, 0, sizeof *p);
1990 p->p_type = PT_NULL;
1993 elf_tdata (abfd)->phdr = phdrs;
1995 elf_tdata (abfd)->next_file_pos = off;
1997 /* Write out the program headers. */
1998 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
1999 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2005 /* Get the size of the program header.
2007 If this is called by the linker before any of the section VMA's are set, it
2008 can't calculate the correct value for a strange memory layout. This only
2009 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2010 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2011 data segment (exclusive of .interp and .dynamic).
2013 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2014 will be two segments. */
2016 static bfd_size_type
2017 get_program_header_size (abfd)
2022 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2024 /* We can't return a different result each time we're called. */
2025 if (elf_tdata (abfd)->program_header_size != 0)
2026 return elf_tdata (abfd)->program_header_size;
2028 /* Assume we will need exactly two PT_LOAD segments: one for text
2029 and one for data. */
2032 s = bfd_get_section_by_name (abfd, ".interp");
2033 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2035 /* If we have a loadable interpreter section, we need a
2036 PT_INTERP segment. In this case, assume we also need a
2037 PT_PHDR segment, although that may not be true for all
2042 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2044 /* We need a PT_DYNAMIC segment. */
2048 /* Let the backend count up any program headers it might need. */
2049 if (bed->elf_backend_create_program_headers)
2050 segs = ((*bed->elf_backend_create_program_headers)
2051 (abfd, (Elf_Internal_Phdr *) NULL, segs));
2053 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2054 return elf_tdata (abfd)->program_header_size;
2057 /* Work out the file positions of all the sections. This is called by
2058 _bfd_elf_compute_section_file_positions. All the section sizes and
2059 VMAs must be known before this is called.
2061 We do not consider reloc sections at this point, unless they form
2062 part of the loadable image. Reloc sections are assigned file
2063 positions in assign_file_positions_for_relocs, which is called by
2064 write_object_contents and final_link.
2066 We also don't set the positions of the .symtab and .strtab here. */
2069 assign_file_positions_except_relocs (abfd)
2072 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2073 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2074 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2076 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2078 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2080 Elf_Internal_Shdr **hdrpp;
2083 /* Start after the ELF header. */
2084 off = i_ehdrp->e_ehsize;
2086 /* We are not creating an executable, which means that we are
2087 not creating a program header, and that the actual order of
2088 the sections in the file is unimportant. */
2089 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2091 Elf_Internal_Shdr *hdr;
2094 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2096 hdr->sh_offset = -1;
2099 if (i == tdata->symtab_section
2100 || i == tdata->strtab_section)
2102 hdr->sh_offset = -1;
2106 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2112 Elf_Internal_Shdr **hdrpp;
2114 /* Assign file positions for the loaded sections based on the
2115 assignment of sections to segments. */
2116 if (! assign_file_positions_for_segments (abfd))
2119 /* Assign file positions for the other sections. */
2121 off = elf_tdata (abfd)->next_file_pos;
2122 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2124 Elf_Internal_Shdr *hdr;
2127 if (hdr->bfd_section != NULL
2128 && hdr->bfd_section->filepos != 0)
2129 hdr->sh_offset = hdr->bfd_section->filepos;
2130 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2132 ((*_bfd_error_handler)
2133 ("%s: warning: allocated section `%s' not in segment",
2134 bfd_get_filename (abfd),
2135 (hdr->bfd_section == NULL
2137 : hdr->bfd_section->name)));
2138 off += (hdr->sh_addr - off) % bed->maxpagesize;
2139 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2142 else if (hdr->sh_type == SHT_REL
2143 || hdr->sh_type == SHT_RELA
2144 || hdr == i_shdrpp[tdata->symtab_section]
2145 || hdr == i_shdrpp[tdata->strtab_section])
2146 hdr->sh_offset = -1;
2148 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2152 /* Place the section headers. */
2153 off = align_file_position (off, bed->s->file_align);
2154 i_ehdrp->e_shoff = off;
2155 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2157 elf_tdata (abfd)->next_file_pos = off;
2166 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2167 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2168 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2170 struct bfd_strtab_hash *shstrtab;
2171 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2173 i_ehdrp = elf_elfheader (abfd);
2174 i_shdrp = elf_elfsections (abfd);
2176 shstrtab = _bfd_elf_stringtab_init ();
2177 if (shstrtab == NULL)
2180 elf_shstrtab (abfd) = shstrtab;
2182 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2183 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2184 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2185 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2187 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2188 i_ehdrp->e_ident[EI_DATA] =
2189 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2190 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2192 for (count = EI_PAD; count < EI_NIDENT; count++)
2193 i_ehdrp->e_ident[count] = 0;
2195 if ((abfd->flags & DYNAMIC) != 0)
2196 i_ehdrp->e_type = ET_DYN;
2197 else if ((abfd->flags & EXEC_P) != 0)
2198 i_ehdrp->e_type = ET_EXEC;
2200 i_ehdrp->e_type = ET_REL;
2202 switch (bfd_get_arch (abfd))
2204 case bfd_arch_unknown:
2205 i_ehdrp->e_machine = EM_NONE;
2207 case bfd_arch_sparc:
2208 if (bed->s->arch_size == 64)
2209 i_ehdrp->e_machine = EM_SPARC64;
2211 i_ehdrp->e_machine = EM_SPARC;
2214 i_ehdrp->e_machine = EM_386;
2217 i_ehdrp->e_machine = EM_68K;
2220 i_ehdrp->e_machine = EM_88K;
2223 i_ehdrp->e_machine = EM_860;
2225 case bfd_arch_mips: /* MIPS Rxxxx */
2226 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2229 i_ehdrp->e_machine = EM_PARISC;
2231 case bfd_arch_powerpc:
2232 i_ehdrp->e_machine = EM_PPC;
2234 /* start-sanitize-arc */
2236 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2238 /* end-sanitize-arc */
2239 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2241 i_ehdrp->e_machine = EM_NONE;
2243 i_ehdrp->e_version = bed->s->ev_current;
2244 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2246 /* no program header, for now. */
2247 i_ehdrp->e_phoff = 0;
2248 i_ehdrp->e_phentsize = 0;
2249 i_ehdrp->e_phnum = 0;
2251 /* each bfd section is section header entry */
2252 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2253 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2255 /* if we're building an executable, we'll need a program header table */
2256 if (abfd->flags & EXEC_P)
2258 /* it all happens later */
2260 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2262 /* elf_build_phdrs() returns a (NULL-terminated) array of
2263 Elf_Internal_Phdrs */
2264 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2265 i_ehdrp->e_phoff = outbase;
2266 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2271 i_ehdrp->e_phentsize = 0;
2273 i_ehdrp->e_phoff = 0;
2276 elf_tdata (abfd)->symtab_hdr.sh_name =
2277 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2278 elf_tdata (abfd)->strtab_hdr.sh_name =
2279 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2280 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2281 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2282 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2283 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2284 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2290 /* Assign file positions for all the reloc sections which are not part
2291 of the loadable file image. */
2294 _bfd_elf_assign_file_positions_for_relocs (abfd)
2299 Elf_Internal_Shdr **shdrpp;
2301 off = elf_tdata (abfd)->next_file_pos;
2303 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2304 i < elf_elfheader (abfd)->e_shnum;
2307 Elf_Internal_Shdr *shdrp;
2310 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2311 && shdrp->sh_offset == -1)
2312 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
2315 elf_tdata (abfd)->next_file_pos = off;
2319 _bfd_elf_write_object_contents (abfd)
2322 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2323 Elf_Internal_Ehdr *i_ehdrp;
2324 Elf_Internal_Shdr **i_shdrp;
2328 if (! abfd->output_has_begun
2329 && ! _bfd_elf_compute_section_file_positions (abfd,
2330 (struct bfd_link_info *) NULL))
2333 i_shdrp = elf_elfsections (abfd);
2334 i_ehdrp = elf_elfheader (abfd);
2337 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2340 _bfd_elf_assign_file_positions_for_relocs (abfd);
2342 /* After writing the headers, we need to write the sections too... */
2343 for (count = 1; count < i_ehdrp->e_shnum; count++)
2345 if (bed->elf_backend_section_processing)
2346 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2347 if (i_shdrp[count]->contents)
2349 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2350 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2352 != i_shdrp[count]->sh_size))
2357 /* Write out the section header names. */
2358 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2359 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2362 if (bed->elf_backend_final_write_processing)
2363 (*bed->elf_backend_final_write_processing) (abfd,
2364 elf_tdata (abfd)->linker);
2366 return bed->s->write_shdrs_and_ehdr (abfd);
2369 /* given a section, search the header to find them... */
2371 _bfd_elf_section_from_bfd_section (abfd, asect)
2375 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2376 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2378 Elf_Internal_Shdr *hdr;
2379 int maxindex = elf_elfheader (abfd)->e_shnum;
2381 for (index = 0; index < maxindex; index++)
2383 hdr = i_shdrp[index];
2384 if (hdr->bfd_section == asect)
2388 if (bed->elf_backend_section_from_bfd_section)
2390 for (index = 0; index < maxindex; index++)
2394 hdr = i_shdrp[index];
2396 if ((*bed->elf_backend_section_from_bfd_section)
2397 (abfd, hdr, asect, &retval))
2402 if (bfd_is_abs_section (asect))
2404 if (bfd_is_com_section (asect))
2406 if (bfd_is_und_section (asect))
2412 /* given a symbol, return the bfd index for that symbol. */
2414 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2416 struct symbol_cache_entry **asym_ptr_ptr;
2418 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2420 flagword flags = asym_ptr->flags;
2422 /* When gas creates relocations against local labels, it creates its
2423 own symbol for the section, but does put the symbol into the
2424 symbol chain, so udata is 0. When the linker is generating
2425 relocatable output, this section symbol may be for one of the
2426 input sections rather than the output section. */
2427 if (asym_ptr->udata.i == 0
2428 && (flags & BSF_SECTION_SYM)
2429 && asym_ptr->section)
2433 if (asym_ptr->section->output_section != NULL)
2434 indx = asym_ptr->section->output_section->index;
2436 indx = asym_ptr->section->index;
2437 if (elf_section_syms (abfd)[indx])
2438 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
2441 idx = asym_ptr->udata.i;
2442 BFD_ASSERT (idx != 0);
2447 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2448 (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
2456 /* Copy private BFD data. This copies any program header information. */
2459 copy_private_bfd_data (ibfd, obfd)
2463 Elf_Internal_Ehdr *iehdr;
2464 struct elf_segment_map *mfirst;
2465 struct elf_segment_map **pm;
2466 Elf_Internal_Phdr *p;
2469 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2470 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2473 if (elf_tdata (ibfd)->phdr == NULL)
2476 iehdr = elf_elfheader (ibfd);
2481 c = elf_elfheader (ibfd)->e_phnum;
2482 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++)
2486 struct elf_segment_map *m;
2491 /* The complicated case when p_vaddr is 0 is to handle the
2492 Solaris linker, which generates a PT_INTERP section with
2493 p_vaddr and p_memsz set to 0. */
2494 for (s = ibfd->sections; s != NULL; s = s->next)
2495 if (((s->vma >= p->p_vaddr
2496 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2497 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2500 && (s->flags & SEC_HAS_CONTENTS) != 0
2501 && (bfd_vma) s->filepos >= p->p_offset
2502 && ((bfd_vma) s->filepos + s->_raw_size
2503 <= p->p_offset + p->p_filesz)))
2504 && s->output_section != NULL)
2507 m = ((struct elf_segment_map *)
2509 (sizeof (struct elf_segment_map)
2510 + (csecs - 1) * sizeof (asection *))));
2515 m->p_type = p->p_type;
2516 m->p_flags = p->p_flags;
2517 m->p_flags_valid = 1;
2518 m->p_paddr = p->p_paddr;
2519 m->p_paddr_valid = 1;
2521 m->includes_filehdr = (p->p_offset == 0
2522 && p->p_filesz >= iehdr->e_ehsize);
2524 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff
2525 && (p->p_offset + p->p_filesz
2526 >= ((bfd_vma) iehdr->e_phoff
2527 + iehdr->e_phnum * iehdr->e_phentsize)));
2530 for (s = ibfd->sections; s != NULL; s = s->next)
2532 if (((s->vma >= p->p_vaddr
2533 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz
2534 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz))
2537 && (s->flags & SEC_HAS_CONTENTS) != 0
2538 && (bfd_vma) s->filepos >= p->p_offset
2539 && ((bfd_vma) s->filepos + s->_raw_size
2540 <= p->p_offset + p->p_filesz)))
2541 && s->output_section != NULL)
2543 m->sections[isec] = s->output_section;
2547 BFD_ASSERT (isec == csecs);
2549 qsort (m->sections, (size_t) csecs, sizeof (asection *),
2557 elf_tdata (obfd)->segment_map = mfirst;
2562 /* Copy private section information. This copies over the entsize
2563 field, and sometimes the info field. */
2566 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
2572 Elf_Internal_Shdr *ihdr, *ohdr;
2574 if (ibfd->xvec->flavour != bfd_target_elf_flavour
2575 || obfd->xvec->flavour != bfd_target_elf_flavour)
2578 /* Copy over private BFD data if it has not already been copied.
2579 This must be done here, rather than in the copy_private_bfd_data
2580 entry point, because the latter is called after the section
2581 contents have been set, which means that the program headers have
2582 already been worked out. */
2583 if (elf_tdata (obfd)->segment_map == NULL
2584 && elf_tdata (ibfd)->phdr != NULL)
2588 /* Only set up the segments when all the sections have been set
2590 for (s = ibfd->sections; s != NULL; s = s->next)
2591 if (s->output_section == NULL)
2595 if (! copy_private_bfd_data (ibfd, obfd))
2600 ihdr = &elf_section_data (isec)->this_hdr;
2601 ohdr = &elf_section_data (osec)->this_hdr;
2603 ohdr->sh_entsize = ihdr->sh_entsize;
2605 if (ihdr->sh_type == SHT_SYMTAB
2606 || ihdr->sh_type == SHT_DYNSYM)
2607 ohdr->sh_info = ihdr->sh_info;
2612 /* Copy private symbol information. If this symbol is in a section
2613 which we did not map into a BFD section, try to map the section
2614 index correctly. We use special macro definitions for the mapped
2615 section indices; these definitions are interpreted by the
2616 swap_out_syms function. */
2618 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2619 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2620 #define MAP_STRTAB (SHN_LORESERVE - 3)
2621 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2624 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
2630 elf_symbol_type *isym, *osym;
2632 isym = elf_symbol_from (ibfd, isymarg);
2633 osym = elf_symbol_from (obfd, osymarg);
2637 && bfd_is_abs_section (isym->symbol.section))
2641 shndx = isym->internal_elf_sym.st_shndx;
2642 if (shndx == elf_onesymtab (ibfd))
2643 shndx = MAP_ONESYMTAB;
2644 else if (shndx == elf_dynsymtab (ibfd))
2645 shndx = MAP_DYNSYMTAB;
2646 else if (shndx == elf_tdata (ibfd)->strtab_section)
2648 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
2649 shndx = MAP_SHSTRTAB;
2650 osym->internal_elf_sym.st_shndx = shndx;
2656 /* Swap out the symbols. */
2659 swap_out_syms (abfd, sttp)
2661 struct bfd_strtab_hash **sttp;
2663 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2665 if (!elf_map_symbols (abfd))
2668 /* Dump out the symtabs. */
2670 int symcount = bfd_get_symcount (abfd);
2671 asymbol **syms = bfd_get_outsymbols (abfd);
2672 struct bfd_strtab_hash *stt;
2673 Elf_Internal_Shdr *symtab_hdr;
2674 Elf_Internal_Shdr *symstrtab_hdr;
2675 char *outbound_syms;
2678 stt = _bfd_elf_stringtab_init ();
2682 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2683 symtab_hdr->sh_type = SHT_SYMTAB;
2684 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
2685 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2686 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2687 symtab_hdr->sh_addralign = bed->s->file_align;
2689 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2690 symstrtab_hdr->sh_type = SHT_STRTAB;
2692 outbound_syms = bfd_alloc (abfd,
2693 (1 + symcount) * bed->s->sizeof_sym);
2694 if (outbound_syms == NULL)
2696 symtab_hdr->contents = (PTR) outbound_syms;
2698 /* now generate the data (for "contents") */
2700 /* Fill in zeroth symbol and swap it out. */
2701 Elf_Internal_Sym sym;
2707 sym.st_shndx = SHN_UNDEF;
2708 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
2709 outbound_syms += bed->s->sizeof_sym;
2711 for (idx = 0; idx < symcount; idx++)
2713 Elf_Internal_Sym sym;
2714 bfd_vma value = syms[idx]->value;
2715 elf_symbol_type *type_ptr;
2716 flagword flags = syms[idx]->flags;
2718 if (flags & BSF_SECTION_SYM)
2719 /* Section symbols have no names. */
2723 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
2726 if (sym.st_name == (unsigned long) -1)
2730 type_ptr = elf_symbol_from (abfd, syms[idx]);
2732 if (bfd_is_com_section (syms[idx]->section))
2734 /* ELF common symbols put the alignment into the `value' field,
2735 and the size into the `size' field. This is backwards from
2736 how BFD handles it, so reverse it here. */
2737 sym.st_size = value;
2738 if (type_ptr == NULL
2739 || type_ptr->internal_elf_sym.st_value == 0)
2740 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
2742 sym.st_value = type_ptr->internal_elf_sym.st_value;
2743 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
2744 syms[idx]->section);
2748 asection *sec = syms[idx]->section;
2751 if (sec->output_section)
2753 value += sec->output_offset;
2754 sec = sec->output_section;
2757 sym.st_value = value;
2758 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2760 if (bfd_is_abs_section (sec)
2762 && type_ptr->internal_elf_sym.st_shndx != 0)
2764 /* This symbol is in a real ELF section which we did
2765 not create as a BFD section. Undo the mapping done
2766 by copy_private_symbol_data. */
2767 shndx = type_ptr->internal_elf_sym.st_shndx;
2771 shndx = elf_onesymtab (abfd);
2774 shndx = elf_dynsymtab (abfd);
2777 shndx = elf_tdata (abfd)->strtab_section;
2780 shndx = elf_tdata (abfd)->shstrtab_section;
2788 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2794 /* Writing this would be a hell of a lot easier if
2795 we had some decent documentation on bfd, and
2796 knew what to expect of the library, and what to
2797 demand of applications. For example, it
2798 appears that `objcopy' might not set the
2799 section of a symbol to be a section that is
2800 actually in the output file. */
2801 sec2 = bfd_get_section_by_name (abfd, sec->name);
2802 BFD_ASSERT (sec2 != 0);
2803 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
2804 BFD_ASSERT (shndx != -1);
2808 sym.st_shndx = shndx;
2811 if (bfd_is_com_section (syms[idx]->section))
2812 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
2813 else if (bfd_is_und_section (syms[idx]->section))
2814 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
2817 ((flags & BSF_FUNCTION)
2820 else if (flags & BSF_SECTION_SYM)
2821 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2822 else if (flags & BSF_FILE)
2823 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2826 int bind = STB_LOCAL;
2827 int type = STT_OBJECT;
2829 if (flags & BSF_LOCAL)
2831 else if (flags & BSF_WEAK)
2833 else if (flags & BSF_GLOBAL)
2836 if (flags & BSF_FUNCTION)
2839 sym.st_info = ELF_ST_INFO (bind, type);
2843 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
2844 outbound_syms += bed->s->sizeof_sym;
2848 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
2849 symstrtab_hdr->sh_type = SHT_STRTAB;
2851 symstrtab_hdr->sh_flags = 0;
2852 symstrtab_hdr->sh_addr = 0;
2853 symstrtab_hdr->sh_entsize = 0;
2854 symstrtab_hdr->sh_link = 0;
2855 symstrtab_hdr->sh_info = 0;
2856 symstrtab_hdr->sh_addralign = 1;
2862 /* Return the number of bytes required to hold the symtab vector.
2864 Note that we base it on the count plus 1, since we will null terminate
2865 the vector allocated based on this size. However, the ELF symbol table
2866 always has a dummy entry as symbol #0, so it ends up even. */
2869 _bfd_elf_get_symtab_upper_bound (abfd)
2874 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
2876 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2877 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2883 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
2888 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2890 if (elf_dynsymtab (abfd) == 0)
2892 bfd_set_error (bfd_error_invalid_operation);
2896 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2897 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2903 _bfd_elf_get_reloc_upper_bound (abfd, asect)
2907 return (asect->reloc_count + 1) * sizeof (arelent *);
2910 /* Canonicalize the relocs. */
2913 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
2922 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols))
2925 tblptr = section->relocation;
2926 for (i = 0; i < section->reloc_count; i++)
2927 *relptr++ = tblptr++;
2931 return section->reloc_count;
2935 _bfd_elf_get_symtab (abfd, alocation)
2937 asymbol **alocation;
2939 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
2942 bfd_get_symcount (abfd) = symcount;
2947 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
2949 asymbol **alocation;
2951 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
2955 _bfd_elf_make_empty_symbol (abfd)
2958 elf_symbol_type *newsym;
2960 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
2965 newsym->symbol.the_bfd = abfd;
2966 return &newsym->symbol;
2971 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
2976 bfd_symbol_info (symbol, ret);
2980 _bfd_elf_get_lineno (ignore_abfd, symbol)
2989 _bfd_elf_set_arch_mach (abfd, arch, machine)
2991 enum bfd_architecture arch;
2992 unsigned long machine;
2994 /* If this isn't the right architecture for this backend, and this
2995 isn't the generic backend, fail. */
2996 if (arch != get_elf_backend_data (abfd)->arch
2997 && arch != bfd_arch_unknown
2998 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
3001 return bfd_default_set_arch_mach (abfd, arch, machine);
3004 /* Find the nearest line to a particular section and offset, for error
3008 _bfd_elf_find_nearest_line (abfd,
3019 CONST char **filename_ptr;
3020 CONST char **functionname_ptr;
3021 unsigned int *line_ptr;
3023 const char *filename;
3027 if (symbols == NULL)
3033 for (p = symbols; *p != NULL; p++)
3037 q = (elf_symbol_type *) *p;
3039 if (bfd_get_section (&q->symbol) != section)
3042 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
3047 filename = bfd_asymbol_name (&q->symbol);
3051 || q->symbol.value <= offset)
3052 func = (asymbol *) q;
3060 *filename_ptr = filename;
3061 *functionname_ptr = bfd_asymbol_name (func);
3067 _bfd_elf_sizeof_headers (abfd, reloc)
3073 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
3075 ret += get_program_header_size (abfd);
3080 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
3085 bfd_size_type count;
3087 Elf_Internal_Shdr *hdr;
3089 if (! abfd->output_has_begun
3090 && ! _bfd_elf_compute_section_file_positions (abfd,
3091 (struct bfd_link_info *) NULL))
3094 hdr = &elf_section_data (section)->this_hdr;
3096 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3098 if (bfd_write (location, 1, count, abfd) != count)
3105 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
3108 Elf_Internal_Rela *dst;
3115 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3118 Elf_Internal_Rel *dst;