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 **));
49 /* Standard ELF hash function. Do not change this function; you will
50 cause invalid hash tables to be generated. (Well, you would if this
51 were being used yet.) */
54 CONST unsigned char *name;
60 while ((ch = *name++) != '\0')
63 if ((g = (h & 0xf0000000)) != 0)
72 /* Read a specified number of bytes at a specified offset in an ELF
73 file, into a newly allocated buffer, and return a pointer to the
77 elf_read (abfd, offset, size)
84 if ((buf = bfd_alloc (abfd, size)) == NULL)
86 bfd_set_error (bfd_error_no_memory);
89 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
91 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
93 if (bfd_get_error () != bfd_error_system_call)
94 bfd_set_error (bfd_error_file_truncated);
104 /* this just does initialization */
105 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
106 elf_tdata (abfd) = (struct elf_obj_tdata *)
107 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
108 if (elf_tdata (abfd) == 0)
110 bfd_set_error (bfd_error_no_memory);
113 /* since everything is done at close time, do we need any
120 bfd_elf_get_str_section (abfd, shindex)
122 unsigned int shindex;
124 Elf_Internal_Shdr **i_shdrp;
125 char *shstrtab = NULL;
127 unsigned int shstrtabsize;
129 i_shdrp = elf_elfsections (abfd);
130 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
133 shstrtab = (char *) i_shdrp[shindex]->contents;
134 if (shstrtab == NULL)
136 /* No cached one, attempt to read, and cache what we read. */
137 offset = i_shdrp[shindex]->sh_offset;
138 shstrtabsize = i_shdrp[shindex]->sh_size;
139 shstrtab = elf_read (abfd, offset, shstrtabsize);
140 i_shdrp[shindex]->contents = (PTR) shstrtab;
146 bfd_elf_string_from_elf_section (abfd, shindex, strindex)
148 unsigned int shindex;
149 unsigned int strindex;
151 Elf_Internal_Shdr *hdr;
156 hdr = elf_elfsections (abfd)[shindex];
158 if (hdr->contents == NULL
159 && bfd_elf_get_str_section (abfd, shindex) == NULL)
162 return ((char *) hdr->contents) + strindex;
165 /* Make a BFD section from an ELF section. We store a pointer to the
166 BFD section in the bfd_section field of the header. */
169 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
171 Elf_Internal_Shdr *hdr;
177 if (hdr->bfd_section != NULL)
179 BFD_ASSERT (strcmp (name,
180 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
184 newsect = bfd_make_section_anyway (abfd, name);
188 newsect->filepos = hdr->sh_offset;
190 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
191 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
192 || ! bfd_set_section_alignment (abfd, newsect,
193 bfd_log2 (hdr->sh_addralign)))
196 flags = SEC_NO_FLAGS;
197 if (hdr->sh_type != SHT_NOBITS)
198 flags |= SEC_HAS_CONTENTS;
199 if ((hdr->sh_flags & SHF_ALLOC) != 0)
202 if (hdr->sh_type != SHT_NOBITS)
205 if ((hdr->sh_flags & SHF_WRITE) == 0)
206 flags |= SEC_READONLY;
207 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
209 else if ((flags & SEC_LOAD) != 0)
212 /* The debugging sections appear to be recognized only by name, not
214 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
215 || strncmp (name, ".line", sizeof ".line" - 1) == 0
216 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
217 flags |= SEC_DEBUGGING;
219 if (! bfd_set_section_flags (abfd, newsect, flags))
222 if ((flags & SEC_ALLOC) != 0)
224 Elf_Internal_Phdr *phdr;
227 /* Look through the phdrs to see if we need to adjust the lma. */
228 phdr = elf_tdata (abfd)->phdr;
229 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
231 if (phdr->p_type == PT_LOAD
232 && phdr->p_vaddr != phdr->p_paddr
233 && phdr->p_vaddr <= hdr->sh_addr
234 && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size)
236 newsect->lma += phdr->p_paddr - phdr->p_vaddr;
242 hdr->bfd_section = newsect;
243 elf_section_data (newsect)->this_hdr = *hdr;
253 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
256 Helper functions for GDB to locate the string tables.
257 Since BFD hides string tables from callers, GDB needs to use an
258 internal hook to find them. Sun's .stabstr, in particular,
259 isn't even pointed to by the .stab section, so ordinary
260 mechanisms wouldn't work to find it, even if we had some.
263 struct elf_internal_shdr *
264 bfd_elf_find_section (abfd, name)
268 Elf_Internal_Shdr **i_shdrp;
273 i_shdrp = elf_elfsections (abfd);
276 shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
277 if (shstrtab != NULL)
279 max = elf_elfheader (abfd)->e_shnum;
280 for (i = 1; i < max; i++)
281 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
288 const char *const bfd_elf_section_type_names[] = {
289 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
290 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
291 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
294 /* ELF relocs are against symbols. If we are producing relocateable
295 output, and the reloc is against an external symbol, and nothing
296 has given us any additional addend, the resulting reloc will also
297 be against the same symbol. In such a case, we don't want to
298 change anything about the way the reloc is handled, since it will
299 all be done at final link time. Rather than put special case code
300 into bfd_perform_relocation, all the reloc types use this howto
301 function. It just short circuits the reloc if producing
302 relocateable output against an external symbol. */
305 bfd_reloc_status_type
306 bfd_elf_generic_reloc (abfd,
314 arelent *reloc_entry;
317 asection *input_section;
319 char **error_message;
321 if (output_bfd != (bfd *) NULL
322 && (symbol->flags & BSF_SECTION_SYM) == 0
323 && (! reloc_entry->howto->partial_inplace
324 || reloc_entry->addend == 0))
326 reloc_entry->address += input_section->output_offset;
330 return bfd_reloc_continue;
333 /* Print out the program headers. */
336 _bfd_elf_print_private_bfd_data (abfd, farg)
340 FILE *f = (FILE *) farg;
341 Elf_Internal_Phdr *p;
344 p = elf_tdata (abfd)->phdr;
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));
388 /* Display ELF-specific fields of a symbol. */
390 bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
394 bfd_print_symbol_type how;
396 FILE *file = (FILE *) filep;
399 case bfd_print_symbol_name:
400 fprintf (file, "%s", symbol->name);
402 case bfd_print_symbol_more:
403 fprintf (file, "elf ");
404 fprintf_vma (file, symbol->value);
405 fprintf (file, " %lx", (long) symbol->flags);
407 case bfd_print_symbol_all:
409 CONST char *section_name;
410 section_name = symbol->section ? symbol->section->name : "(*none*)";
411 bfd_print_symbol_vandf ((PTR) file, symbol);
412 fprintf (file, " %s\t", section_name);
413 /* Print the "other" value for a symbol. For common symbols,
414 we've already printed the size; now print the alignment.
415 For other symbols, we have no specified alignment, and
416 we've printed the address; now print the size. */
418 (bfd_is_com_section (symbol->section)
419 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
420 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
421 fprintf (file, " %s", symbol->name);
427 /* Create an entry in an ELF linker hash table. */
429 struct bfd_hash_entry *
430 _bfd_elf_link_hash_newfunc (entry, table, string)
431 struct bfd_hash_entry *entry;
432 struct bfd_hash_table *table;
435 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
437 /* Allocate the structure if it has not already been allocated by a
439 if (ret == (struct elf_link_hash_entry *) NULL)
440 ret = ((struct elf_link_hash_entry *)
441 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
442 if (ret == (struct elf_link_hash_entry *) NULL)
444 bfd_set_error (bfd_error_no_memory);
445 return (struct bfd_hash_entry *) ret;
448 /* Call the allocation method of the superclass. */
449 ret = ((struct elf_link_hash_entry *)
450 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
452 if (ret != (struct elf_link_hash_entry *) NULL)
454 /* Set local fields. */
458 ret->dynstr_index = 0;
460 ret->got_offset = (bfd_vma) -1;
461 ret->plt_offset = (bfd_vma) -1;
462 ret->type = STT_NOTYPE;
463 ret->elf_link_hash_flags = 0;
466 return (struct bfd_hash_entry *) ret;
469 /* Initialize an ELF linker hash table. */
472 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
473 struct elf_link_hash_table *table;
475 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
476 struct bfd_hash_table *,
479 table->dynamic_sections_created = false;
480 table->dynobj = NULL;
481 /* The first dynamic symbol is a dummy. */
482 table->dynsymcount = 1;
483 table->dynstr = NULL;
484 table->bucketcount = 0;
485 table->needed = NULL;
486 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
489 /* Create an ELF linker hash table. */
491 struct bfd_link_hash_table *
492 _bfd_elf_link_hash_table_create (abfd)
495 struct elf_link_hash_table *ret;
497 ret = ((struct elf_link_hash_table *)
498 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
499 if (ret == (struct elf_link_hash_table *) NULL)
501 bfd_set_error (bfd_error_no_memory);
505 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
507 bfd_release (abfd, ret);
514 /* This is a hook for the ELF emulation code in the generic linker to
515 tell the backend linker what file name to use for the DT_NEEDED
516 entry for a dynamic object. The generic linker passes name as an
517 empty string to indicate that no DT_NEEDED entry should be made. */
520 bfd_elf_set_dt_needed_name (abfd, name)
524 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
525 elf_dt_needed_name (abfd) = name;
528 /* Get the list of DT_NEEDED entries for a link. */
530 struct bfd_link_needed_list *
531 bfd_elf_get_needed_list (abfd, info)
533 struct bfd_link_info *info;
535 if (info->hash->creator->flavour != bfd_target_elf_flavour)
537 return elf_hash_table (info)->needed;
540 /* Allocate an ELF string table--force the first byte to be zero. */
542 struct bfd_strtab_hash *
543 _bfd_elf_stringtab_init ()
545 struct bfd_strtab_hash *ret;
547 ret = _bfd_stringtab_init ();
552 loc = _bfd_stringtab_add (ret, "", true, false);
553 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1);
554 if (loc == (bfd_size_type) -1)
556 _bfd_stringtab_free (ret);
563 /* ELF .o/exec file reading */
565 /* Create a new bfd section from an ELF section header. */
568 bfd_section_from_shdr (abfd, shindex)
570 unsigned int shindex;
572 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
573 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
574 struct elf_backend_data *bed = get_elf_backend_data (abfd);
577 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
579 switch (hdr->sh_type)
582 /* Inactive section. Throw it away. */
585 case SHT_PROGBITS: /* Normal section with contents. */
586 case SHT_DYNAMIC: /* Dynamic linking information. */
587 case SHT_NOBITS: /* .bss section. */
588 case SHT_HASH: /* .hash section. */
589 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
591 case SHT_SYMTAB: /* A symbol table */
592 if (elf_onesymtab (abfd) == shindex)
595 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
596 BFD_ASSERT (elf_onesymtab (abfd) == 0);
597 elf_onesymtab (abfd) = shindex;
598 elf_tdata (abfd)->symtab_hdr = *hdr;
599 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr;
600 abfd->flags |= HAS_SYMS;
602 /* Sometimes a shared object will map in the symbol table. If
603 SHF_ALLOC is set, and this is a shared object, then we also
604 treat this section as a BFD section. We can not base the
605 decision purely on SHF_ALLOC, because that flag is sometimes
606 set in a relocateable object file, which would confuse the
608 if ((hdr->sh_flags & SHF_ALLOC) != 0
609 && (abfd->flags & DYNAMIC) != 0
610 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
615 case SHT_DYNSYM: /* A dynamic symbol table */
616 if (elf_dynsymtab (abfd) == shindex)
619 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym);
620 BFD_ASSERT (elf_dynsymtab (abfd) == 0);
621 elf_dynsymtab (abfd) = shindex;
622 elf_tdata (abfd)->dynsymtab_hdr = *hdr;
623 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr;
624 abfd->flags |= HAS_SYMS;
626 /* Besides being a symbol table, we also treat this as a regular
627 section, so that objcopy can handle it. */
628 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
630 case SHT_STRTAB: /* A string table */
631 if (hdr->bfd_section != NULL)
633 if (ehdr->e_shstrndx == shindex)
635 elf_tdata (abfd)->shstrtab_hdr = *hdr;
636 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr;
642 for (i = 1; i < ehdr->e_shnum; i++)
644 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i];
645 if (hdr2->sh_link == shindex)
647 if (! bfd_section_from_shdr (abfd, i))
649 if (elf_onesymtab (abfd) == i)
651 elf_tdata (abfd)->strtab_hdr = *hdr;
652 elf_elfsections (abfd)[shindex] =
653 &elf_tdata (abfd)->strtab_hdr;
656 if (elf_dynsymtab (abfd) == i)
658 elf_tdata (abfd)->dynstrtab_hdr = *hdr;
659 elf_elfsections (abfd)[shindex] = hdr =
660 &elf_tdata (abfd)->dynstrtab_hdr;
661 /* We also treat this as a regular section, so
662 that objcopy can handle it. */
665 #if 0 /* Not handling other string tables specially right now. */
666 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */
667 /* We have a strtab for some random other section. */
668 newsect = (asection *) hdr2->bfd_section;
671 hdr->bfd_section = newsect;
672 hdr2 = &elf_section_data (newsect)->str_hdr;
674 elf_elfsections (abfd)[shindex] = hdr2;
680 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
684 /* *These* do a lot of work -- but build no sections! */
686 asection *target_sect;
687 Elf_Internal_Shdr *hdr2;
688 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
690 /* For some incomprehensible reason Oracle distributes
691 libraries for Solaris in which some of the objects have
692 bogus sh_link fields. It would be nice if we could just
693 reject them, but, unfortunately, some people need to use
694 them. We scan through the section headers; if we find only
695 one suitable symbol table, we clobber the sh_link to point
696 to it. I hope this doesn't break anything. */
697 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB
698 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM)
704 for (scan = 1; scan < ehdr->e_shnum; scan++)
706 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB
707 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM)
718 hdr->sh_link = found;
721 /* Get the symbol table. */
722 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB
723 && ! bfd_section_from_shdr (abfd, hdr->sh_link))
726 /* If this reloc section does not use the main symbol table we
727 don't treat it as a reloc section. BFD can't adequately
728 represent such a section, so at least for now, we don't
729 try. We just present it as a normal section. */
730 if (hdr->sh_link != elf_onesymtab (abfd))
731 return _bfd_elf_make_section_from_shdr (abfd, hdr, name);
733 /* Don't allow REL relocations on a machine that uses RELA and
735 /* @@ Actually, the generic ABI does suggest that both might be
736 used in one file. But the four ABI Processor Supplements I
737 have access to right now all specify that only one is used on
738 each of those architectures. It's conceivable that, e.g., a
739 bunch of absolute 32-bit relocs might be more compact in REL
740 form even on a RELA machine... */
741 BFD_ASSERT (use_rela_p
742 ? (hdr->sh_type == SHT_RELA
743 && hdr->sh_entsize == bed->s->sizeof_rela)
744 : (hdr->sh_type == SHT_REL
745 && hdr->sh_entsize == bed->s->sizeof_rel));
747 if (! bfd_section_from_shdr (abfd, hdr->sh_info))
749 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info);
750 if (target_sect == NULL)
753 hdr2 = &elf_section_data (target_sect)->rel_hdr;
755 elf_elfsections (abfd)[shindex] = hdr2;
756 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
757 target_sect->flags |= SEC_RELOC;
758 target_sect->relocation = NULL;
759 target_sect->rel_filepos = hdr->sh_offset;
760 abfd->flags |= HAS_RELOC;
772 /* Check for any processor-specific section types. */
774 if (bed->elf_backend_section_from_shdr)
775 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
783 /* Given an ELF section number, retrieve the corresponding BFD
787 bfd_section_from_elf_index (abfd, index)
791 BFD_ASSERT (index > 0 && index < SHN_LORESERVE);
792 if (index >= elf_elfheader (abfd)->e_shnum)
794 return elf_elfsections (abfd)[index]->bfd_section;
798 _bfd_elf_new_section_hook (abfd, sec)
802 struct bfd_elf_section_data *sdata;
804 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
807 bfd_set_error (bfd_error_no_memory);
810 sec->used_by_bfd = (PTR) sdata;
811 memset (sdata, 0, sizeof (*sdata));
815 /* Create a new bfd section from an ELF program header.
817 Since program segments have no names, we generate a synthetic name
818 of the form segment<NUM>, where NUM is generally the index in the
819 program header table. For segments that are split (see below) we
820 generate the names segment<NUM>a and segment<NUM>b.
822 Note that some program segments may have a file size that is different than
823 (less than) the memory size. All this means is that at execution the
824 system must allocate the amount of memory specified by the memory size,
825 but only initialize it with the first "file size" bytes read from the
826 file. This would occur for example, with program segments consisting
827 of combined data+bss.
829 To handle the above situation, this routine generates TWO bfd sections
830 for the single program segment. The first has the length specified by
831 the file size of the segment, and the second has the length specified
832 by the difference between the two sizes. In effect, the segment is split
833 into it's initialized and uninitialized parts.
838 bfd_section_from_phdr (abfd, hdr, index)
840 Elf_Internal_Phdr *hdr;
848 split = ((hdr->p_memsz > 0) &&
849 (hdr->p_filesz > 0) &&
850 (hdr->p_memsz > hdr->p_filesz));
851 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
852 name = bfd_alloc (abfd, strlen (namebuf) + 1);
855 bfd_set_error (bfd_error_no_memory);
858 strcpy (name, namebuf);
859 newsect = bfd_make_section (abfd, name);
862 newsect->vma = hdr->p_vaddr;
863 newsect->lma = hdr->p_paddr;
864 newsect->_raw_size = hdr->p_filesz;
865 newsect->filepos = hdr->p_offset;
866 newsect->flags |= SEC_HAS_CONTENTS;
867 if (hdr->p_type == PT_LOAD)
869 newsect->flags |= SEC_ALLOC;
870 newsect->flags |= SEC_LOAD;
871 if (hdr->p_flags & PF_X)
873 /* FIXME: all we known is that it has execute PERMISSION,
875 newsect->flags |= SEC_CODE;
878 if (!(hdr->p_flags & PF_W))
880 newsect->flags |= SEC_READONLY;
885 sprintf (namebuf, "segment%db", index);
886 name = bfd_alloc (abfd, strlen (namebuf) + 1);
889 bfd_set_error (bfd_error_no_memory);
892 strcpy (name, namebuf);
893 newsect = bfd_make_section (abfd, name);
896 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
897 newsect->lma = hdr->p_paddr + hdr->p_filesz;
898 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
899 if (hdr->p_type == PT_LOAD)
901 newsect->flags |= SEC_ALLOC;
902 if (hdr->p_flags & PF_X)
903 newsect->flags |= SEC_CODE;
905 if (!(hdr->p_flags & PF_W))
906 newsect->flags |= SEC_READONLY;
912 /* Set up an ELF internal section header for a section. */
916 elf_fake_sections (abfd, asect, failedptrarg)
921 struct elf_backend_data *bed = get_elf_backend_data (abfd);
922 boolean *failedptr = (boolean *) failedptrarg;
923 Elf_Internal_Shdr *this_hdr;
927 /* We already failed; just get out of the bfd_map_over_sections
932 this_hdr = &elf_section_data (asect)->this_hdr;
934 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd),
937 if (this_hdr->sh_name == (unsigned long) -1)
943 this_hdr->sh_flags = 0;
945 if ((asect->flags & SEC_ALLOC) != 0)
946 this_hdr->sh_addr = asect->vma;
948 this_hdr->sh_addr = 0;
950 this_hdr->sh_offset = 0;
951 this_hdr->sh_size = asect->_raw_size;
952 this_hdr->sh_link = 0;
953 this_hdr->sh_addralign = 1 << asect->alignment_power;
954 /* The sh_entsize and sh_info fields may have been set already by
955 copy_private_section_data. */
957 this_hdr->bfd_section = asect;
958 this_hdr->contents = NULL;
960 /* FIXME: This should not be based on section names. */
961 if (strcmp (asect->name, ".dynstr") == 0)
962 this_hdr->sh_type = SHT_STRTAB;
963 else if (strcmp (asect->name, ".hash") == 0)
965 this_hdr->sh_type = SHT_HASH;
966 this_hdr->sh_entsize = bed->s->arch_size / 8;
968 else if (strcmp (asect->name, ".dynsym") == 0)
970 this_hdr->sh_type = SHT_DYNSYM;
971 this_hdr->sh_entsize = bed->s->sizeof_sym;
973 else if (strcmp (asect->name, ".dynamic") == 0)
975 this_hdr->sh_type = SHT_DYNAMIC;
976 this_hdr->sh_entsize = bed->s->sizeof_dyn;
978 else if (strncmp (asect->name, ".rela", 5) == 0
979 && get_elf_backend_data (abfd)->use_rela_p)
981 this_hdr->sh_type = SHT_RELA;
982 this_hdr->sh_entsize = bed->s->sizeof_rela;
984 else if (strncmp (asect->name, ".rel", 4) == 0
985 && ! get_elf_backend_data (abfd)->use_rela_p)
987 this_hdr->sh_type = SHT_REL;
988 this_hdr->sh_entsize = bed->s->sizeof_rel;
990 else if (strcmp (asect->name, ".note") == 0)
991 this_hdr->sh_type = SHT_NOTE;
992 else if (strncmp (asect->name, ".stab", 5) == 0
993 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0)
994 this_hdr->sh_type = SHT_STRTAB;
995 else if ((asect->flags & SEC_ALLOC) != 0
996 && (asect->flags & SEC_LOAD) != 0)
997 this_hdr->sh_type = SHT_PROGBITS;
998 else if ((asect->flags & SEC_ALLOC) != 0
999 && ((asect->flags & SEC_LOAD) == 0))
1000 this_hdr->sh_type = SHT_NOBITS;
1004 this_hdr->sh_type = SHT_PROGBITS;
1007 if ((asect->flags & SEC_ALLOC) != 0)
1008 this_hdr->sh_flags |= SHF_ALLOC;
1009 if ((asect->flags & SEC_READONLY) == 0)
1010 this_hdr->sh_flags |= SHF_WRITE;
1011 if ((asect->flags & SEC_CODE) != 0)
1012 this_hdr->sh_flags |= SHF_EXECINSTR;
1014 /* Check for processor-specific section types. */
1016 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1018 if (bed->elf_backend_fake_sections)
1019 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1022 /* If the section has relocs, set up a section header for the
1023 SHT_REL[A] section. */
1024 if ((asect->flags & SEC_RELOC) != 0)
1026 Elf_Internal_Shdr *rela_hdr;
1027 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1030 rela_hdr = &elf_section_data (asect)->rel_hdr;
1031 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name));
1034 bfd_set_error (bfd_error_no_memory);
1038 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name);
1040 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name,
1042 if (rela_hdr->sh_name == (unsigned int) -1)
1047 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1048 rela_hdr->sh_entsize = (use_rela_p
1049 ? bed->s->sizeof_rela
1050 : bed->s->sizeof_rel);
1051 rela_hdr->sh_addralign = bed->s->file_align;
1052 rela_hdr->sh_flags = 0;
1053 rela_hdr->sh_addr = 0;
1054 rela_hdr->sh_size = 0;
1055 rela_hdr->sh_offset = 0;
1059 /* Assign all ELF section numbers. The dummy first section is handled here
1060 too. The link/info pointers for the standard section types are filled
1061 in here too, while we're at it. */
1064 assign_section_numbers (abfd)
1067 struct elf_obj_tdata *t = elf_tdata (abfd);
1069 unsigned int section_number;
1070 Elf_Internal_Shdr **i_shdrp;
1071 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1075 for (sec = abfd->sections; sec; sec = sec->next)
1077 struct bfd_elf_section_data *d = elf_section_data (sec);
1079 d->this_idx = section_number++;
1080 if ((sec->flags & SEC_RELOC) == 0)
1083 d->rel_idx = section_number++;
1086 t->shstrtab_section = section_number++;
1087 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
1088 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1090 if (abfd->symcount > 0)
1092 t->symtab_section = section_number++;
1093 t->strtab_section = section_number++;
1096 elf_elfheader (abfd)->e_shnum = section_number;
1098 /* Set up the list of section header pointers, in agreement with the
1100 i_shdrp = ((Elf_Internal_Shdr **)
1101 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *)));
1102 if (i_shdrp == NULL)
1104 bfd_set_error (bfd_error_no_memory);
1108 i_shdrp[0] = ((Elf_Internal_Shdr *)
1109 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr)));
1110 if (i_shdrp[0] == NULL)
1112 bfd_release (abfd, i_shdrp);
1113 bfd_set_error (bfd_error_no_memory);
1116 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr));
1118 elf_elfsections (abfd) = i_shdrp;
1120 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1121 if (abfd->symcount > 0)
1123 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1124 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1125 t->symtab_hdr.sh_link = t->strtab_section;
1127 for (sec = abfd->sections; sec; sec = sec->next)
1129 struct bfd_elf_section_data *d = elf_section_data (sec);
1133 i_shdrp[d->this_idx] = &d->this_hdr;
1134 if (d->rel_idx != 0)
1135 i_shdrp[d->rel_idx] = &d->rel_hdr;
1137 /* Fill in the sh_link and sh_info fields while we're at it. */
1139 /* sh_link of a reloc section is the section index of the symbol
1140 table. sh_info is the section index of the section to which
1141 the relocation entries apply. */
1142 if (d->rel_idx != 0)
1144 d->rel_hdr.sh_link = t->symtab_section;
1145 d->rel_hdr.sh_info = d->this_idx;
1148 switch (d->this_hdr.sh_type)
1152 /* A reloc section which we are treating as a normal BFD
1153 section. sh_link is the section index of the symbol
1154 table. sh_info is the section index of the section to
1155 which the relocation entries apply. We assume that an
1156 allocated reloc section uses the dynamic symbol table.
1157 FIXME: How can we be sure? */
1158 s = bfd_get_section_by_name (abfd, ".dynsym");
1160 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1162 /* We look up the section the relocs apply to by name. */
1164 if (d->this_hdr.sh_type == SHT_REL)
1168 s = bfd_get_section_by_name (abfd, name);
1170 d->this_hdr.sh_info = elf_section_data (s)->this_idx;
1174 /* We assume that a section named .stab*str is a stabs
1175 string section. We look for a section with the same name
1176 but without the trailing ``str'', and set its sh_link
1177 field to point to this section. */
1178 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0
1179 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0)
1184 len = strlen (sec->name);
1185 alc = (char *) malloc (len - 2);
1188 bfd_set_error (bfd_error_no_memory);
1191 strncpy (alc, sec->name, len - 3);
1192 alc[len - 3] = '\0';
1193 s = bfd_get_section_by_name (abfd, alc);
1197 elf_section_data (s)->this_hdr.sh_link = d->this_idx;
1199 /* This is a .stab section. */
1200 elf_section_data (s)->this_hdr.sh_entsize =
1201 4 + 2 * (bed->s->arch_size / 8);
1208 /* sh_link is the section header index of the string table
1209 used for the dynamic entries or symbol table. */
1210 s = bfd_get_section_by_name (abfd, ".dynstr");
1212 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1216 /* sh_link is the section header index of the symbol table
1217 this hash table is for. */
1218 s = bfd_get_section_by_name (abfd, ".dynsym");
1220 d->this_hdr.sh_link = elf_section_data (s)->this_idx;
1228 /* Map symbol from it's internal number to the external number, moving
1229 all local symbols to be at the head of the list. */
1232 sym_is_global (abfd, sym)
1236 /* If the backend has a special mapping, use it. */
1237 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1238 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1241 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0
1242 || bfd_is_und_section (bfd_get_section (sym))
1243 || bfd_is_com_section (bfd_get_section (sym)));
1247 elf_map_symbols (abfd)
1250 int symcount = bfd_get_symcount (abfd);
1251 asymbol **syms = bfd_get_outsymbols (abfd);
1252 asymbol **sect_syms;
1254 int num_globals = 0;
1255 int num_locals2 = 0;
1256 int num_globals2 = 0;
1258 int num_sections = 0;
1264 fprintf (stderr, "elf_map_symbols\n");
1268 /* Add a section symbol for each BFD section. FIXME: Is this really
1270 for (asect = abfd->sections; asect; asect = asect->next)
1272 if (max_index < asect->index)
1273 max_index = asect->index;
1277 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1278 if (sect_syms == NULL)
1280 bfd_set_error (bfd_error_no_memory);
1283 elf_section_syms (abfd) = sect_syms;
1285 for (idx = 0; idx < symcount; idx++)
1287 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0
1288 && (syms[idx]->value + syms[idx]->section->vma) == 0)
1292 sec = syms[idx]->section;
1293 if (sec->owner != NULL)
1295 if (sec->owner != abfd)
1297 if (sec->output_offset != 0)
1299 sec = sec->output_section;
1300 BFD_ASSERT (sec->owner == abfd);
1302 sect_syms[sec->index] = syms[idx];
1307 for (asect = abfd->sections; asect; asect = asect->next)
1311 if (sect_syms[asect->index] != NULL)
1314 sym = bfd_make_empty_symbol (abfd);
1317 sym->the_bfd = abfd;
1318 sym->name = asect->name;
1320 /* Set the flags to 0 to indicate that this one was newly added. */
1322 sym->section = asect;
1323 sect_syms[asect->index] = sym;
1327 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1328 asect->name, (long) asect->vma, asect->index, (long) asect);
1332 /* Classify all of the symbols. */
1333 for (idx = 0; idx < symcount; idx++)
1335 if (!sym_is_global (abfd, syms[idx]))
1340 for (asect = abfd->sections; asect; asect = asect->next)
1342 if (sect_syms[asect->index] != NULL
1343 && sect_syms[asect->index]->flags == 0)
1345 sect_syms[asect->index]->flags = BSF_SECTION_SYM;
1346 if (!sym_is_global (abfd, sect_syms[asect->index]))
1350 sect_syms[asect->index]->flags = 0;
1354 /* Now sort the symbols so the local symbols are first. */
1355 new_syms = ((asymbol **)
1357 (num_locals + num_globals) * sizeof (asymbol *)));
1358 if (new_syms == NULL)
1360 bfd_set_error (bfd_error_no_memory);
1364 for (idx = 0; idx < symcount; idx++)
1366 asymbol *sym = syms[idx];
1369 if (!sym_is_global (abfd, sym))
1372 i = num_locals + num_globals2++;
1374 sym->udata.i = i + 1;
1376 for (asect = abfd->sections; asect; asect = asect->next)
1378 if (sect_syms[asect->index] != NULL
1379 && sect_syms[asect->index]->flags == 0)
1381 asymbol *sym = sect_syms[asect->index];
1384 sym->flags = BSF_SECTION_SYM;
1385 if (!sym_is_global (abfd, sym))
1388 i = num_locals + num_globals2++;
1390 sym->udata.i = i + 1;
1394 bfd_set_symtab (abfd, new_syms, num_locals + num_globals);
1396 elf_num_locals (abfd) = num_locals;
1397 elf_num_globals (abfd) = num_globals;
1401 /* Align to the maximum file alignment that could be required for any
1402 ELF data structure. */
1404 static INLINE file_ptr align_file_position PARAMS ((file_ptr, int));
1405 static INLINE file_ptr
1406 align_file_position (off, align)
1410 return (off + align - 1) & ~(align - 1);
1413 /* Assign a file position to a section, optionally aligning to the
1414 required section alignment. */
1417 _bfd_elf_assign_file_position_for_section (i_shdrp, offset, align)
1418 Elf_Internal_Shdr *i_shdrp;
1426 al = i_shdrp->sh_addralign;
1428 offset = BFD_ALIGN (offset, al);
1430 i_shdrp->sh_offset = offset;
1431 if (i_shdrp->bfd_section != NULL)
1432 i_shdrp->bfd_section->filepos = offset;
1433 if (i_shdrp->sh_type != SHT_NOBITS)
1434 offset += i_shdrp->sh_size;
1438 /* Compute the file positions we are going to put the sections at, and
1439 otherwise prepare to begin writing out the ELF file. If LINK_INFO
1440 is not NULL, this is being called by the ELF backend linker. */
1443 _bfd_elf_compute_section_file_positions (abfd, link_info)
1445 struct bfd_link_info *link_info;
1447 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1449 struct bfd_strtab_hash *strtab;
1450 Elf_Internal_Shdr *shstrtab_hdr;
1452 if (abfd->output_has_begun)
1455 /* Do any elf backend specific processing first. */
1456 if (bed->elf_backend_begin_write_processing)
1457 (*bed->elf_backend_begin_write_processing) (abfd, link_info);
1459 if (! prep_headers (abfd))
1463 bfd_map_over_sections (abfd, elf_fake_sections, &failed);
1467 if (!assign_section_numbers (abfd))
1470 /* The backend linker builds symbol table information itself. */
1471 if (link_info == NULL && abfd->symcount > 0)
1473 if (! swap_out_syms (abfd, &strtab))
1477 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr;
1478 /* sh_name was set in prep_headers. */
1479 shstrtab_hdr->sh_type = SHT_STRTAB;
1480 shstrtab_hdr->sh_flags = 0;
1481 shstrtab_hdr->sh_addr = 0;
1482 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd));
1483 shstrtab_hdr->sh_entsize = 0;
1484 shstrtab_hdr->sh_link = 0;
1485 shstrtab_hdr->sh_info = 0;
1486 /* sh_offset is set in assign_file_positions_except_relocs. */
1487 shstrtab_hdr->sh_addralign = 1;
1489 if (!assign_file_positions_except_relocs (abfd))
1492 if (link_info == NULL && abfd->symcount > 0)
1495 Elf_Internal_Shdr *hdr;
1497 off = elf_tdata (abfd)->next_file_pos;
1499 hdr = &elf_tdata (abfd)->symtab_hdr;
1500 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1502 hdr = &elf_tdata (abfd)->strtab_hdr;
1503 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
1505 elf_tdata (abfd)->next_file_pos = off;
1507 /* Now that we know where the .strtab section goes, write it
1509 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0
1510 || ! _bfd_stringtab_emit (abfd, strtab))
1512 _bfd_stringtab_free (strtab);
1515 abfd->output_has_begun = true;
1520 /* Create a mapping from a set of sections to a program segment. */
1522 static INLINE struct elf_segment_map *
1523 make_mapping (abfd, sections, from, to)
1525 asection **sections;
1529 struct elf_segment_map *m;
1533 m = ((struct elf_segment_map *)
1535 (sizeof (struct elf_segment_map)
1536 + (to - from - 1) * sizeof (asection *))));
1539 bfd_set_error (bfd_error_no_memory);
1543 m->p_type = PT_LOAD;
1544 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++)
1545 m->sections[i - from] = *hdrpp;
1546 m->count = to - from;
1551 /* Set up a mapping from BFD sections to program segments. */
1554 map_sections_to_segments (abfd)
1557 asection **sections = NULL;
1561 struct elf_segment_map *mfirst;
1562 struct elf_segment_map **pm;
1563 struct elf_segment_map *m;
1565 unsigned int phdr_index;
1566 bfd_vma maxpagesize;
1569 if (elf_tdata (abfd)->segment_map != NULL)
1572 if (bfd_count_sections (abfd) == 0)
1575 /* Select the allocated sections, and sort them. */
1577 sections = (asection **) malloc (bfd_count_sections (abfd)
1578 * sizeof (asection *));
1579 if (sections == NULL)
1581 bfd_set_error (bfd_error_no_memory);
1586 for (s = abfd->sections; s != NULL; s = s->next)
1588 if ((s->flags & SEC_ALLOC) != 0)
1594 BFD_ASSERT (i <= bfd_count_sections (abfd));
1597 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections);
1599 /* Build the mapping. */
1604 /* If we have a .interp section, then create a PT_PHDR segment for
1605 the program headers and a PT_INTERP segment for the .interp
1607 s = bfd_get_section_by_name (abfd, ".interp");
1608 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1610 m = ((struct elf_segment_map *)
1611 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1614 bfd_set_error (bfd_error_no_memory);
1618 m->p_type = PT_PHDR;
1619 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */
1620 m->p_flags = PF_R | PF_X;
1621 m->p_flags_valid = 1;
1626 m = ((struct elf_segment_map *)
1627 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1630 bfd_set_error (bfd_error_no_memory);
1634 m->p_type = PT_INTERP;
1642 /* Look through the sections. We put sections in the same program
1643 segment when the start of the second section can be placed within
1644 a few bytes of the end of the first section. */
1647 maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1648 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++)
1654 /* See if this section and the last one will fit in the same
1656 if (last_hdr == NULL
1657 || ((BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize)
1659 && ((last_hdr->flags & SEC_LOAD) != 0
1660 || (hdr->flags & SEC_LOAD) == 0)))
1666 /* This section won't fit in the program segment. We must
1667 create a new program header holding all the sections from
1668 phdr_index until hdr. */
1670 m = make_mapping (abfd, sections, phdr_index, i);
1681 /* Create a final PT_LOAD program segment. */
1682 if (last_hdr != NULL)
1684 m = make_mapping (abfd, sections, phdr_index, i);
1692 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */
1693 s = bfd_get_section_by_name (abfd, ".dynamic");
1694 if (s != NULL && (s->flags & SEC_LOAD) != 0)
1696 m = ((struct elf_segment_map *)
1697 bfd_zalloc (abfd, sizeof (struct elf_segment_map)));
1700 bfd_set_error (bfd_error_no_memory);
1704 m->p_type = PT_DYNAMIC;
1715 elf_tdata (abfd)->segment_map = mfirst;
1719 if (sections != NULL)
1724 /* Sort sections by VMA. */
1727 elf_sort_sections (arg1, arg2)
1731 const asection *sec1 = *(const asection **) arg1;
1732 const asection *sec2 = *(const asection **) arg2;
1734 if (sec1->vma < sec2->vma)
1736 else if (sec1->vma > sec2->vma)
1739 /* Put !SEC_LOAD sections after SEC_LOAD ones. */
1741 #define TOEND(x) (((x)->flags & SEC_LOAD) == 0)
1745 return sec1->target_index - sec2->target_index;
1754 /* Sort by size, to put zero sized sections before others at the
1757 if (sec1->_raw_size < sec2->_raw_size)
1759 if (sec1->_raw_size > sec2->_raw_size)
1762 return sec1->target_index - sec2->target_index;
1765 /* Assign file positions to the sections based on the mapping from
1766 sections to segments. This function also sets up some fields in
1767 the file header, and writes out the program headers. */
1770 assign_file_positions_for_segments (abfd)
1773 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
1775 struct elf_segment_map *m;
1777 Elf_Internal_Phdr *phdrs;
1780 Elf_Internal_Phdr *p;
1782 if (elf_tdata (abfd)->segment_map == NULL)
1784 if (! map_sections_to_segments (abfd))
1789 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
1792 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr;
1793 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr;
1794 elf_elfheader (abfd)->e_phnum = count;
1799 /* Let the backend count up any program headers it might need. */
1800 if (bed->elf_backend_create_program_headers)
1801 count = ((*bed->elf_backend_create_program_headers)
1802 (abfd, (Elf_Internal_Phdr *) NULL, count));
1804 /* If we already counted the number of program segments, make sure
1805 that we allocated enough space. This happens when SIZEOF_HEADERS
1806 is used in a linker script. */
1807 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr;
1808 if (alloc != 0 && count > alloc)
1810 ((*_bfd_error_handler)
1811 ("%s: Not enough room for program headers (allocated %u, need %u)",
1812 bfd_get_filename (abfd), alloc, count));
1813 bfd_set_error (bfd_error_bad_value);
1820 phdrs = ((Elf_Internal_Phdr *)
1821 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr)));
1824 bfd_set_error (bfd_error_no_memory);
1828 off = bed->s->sizeof_ehdr;
1829 off += alloc * bed->s->sizeof_phdr;
1832 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
1840 p->p_type = m->p_type;
1842 if (m->p_flags_valid)
1843 p->p_flags = m->p_flags;
1848 p->p_vaddr = m->sections[0]->vma;
1850 if (m->p_paddr_valid)
1851 p->p_paddr = m->p_paddr;
1852 else if (m->count == 0)
1855 p->p_paddr = m->sections[0]->lma;
1857 if (p->p_type == PT_LOAD)
1858 p->p_align = bed->maxpagesize;
1859 else if (m->count == 0)
1860 p->p_align = bed->s->file_align;
1868 if (p->p_type == PT_LOAD)
1874 struct elf_segment_map *mi;
1875 Elf_Internal_Phdr *pi;
1876 Elf_Internal_Phdr *pi_phdr;
1878 /* This is the first PT_LOAD segment. If there is a
1879 PT_INTERP segment, adjust the offset of this segment
1880 to include the program headers and the file header. */
1882 for (mi = elf_tdata (abfd)->segment_map, pi = phdrs;
1884 mi = mi->next, pi++)
1886 if (mi->p_type == PT_INTERP)
1895 if (mi->p_type == PT_PHDR)
1899 /* Set up the PT_PHDR addresses. */
1900 if (pi_phdr != NULL)
1902 pi_phdr->p_vaddr = p->p_vaddr + bed->s->sizeof_ehdr;
1903 pi_phdr->p_paddr = p->p_paddr + bed->s->sizeof_ehdr;
1910 if (! m->p_flags_valid)
1912 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++)
1916 bfd_size_type align;
1921 if (p->p_type == PT_LOAD)
1925 /* The section VMA must equal the file position modulo
1927 adjust = (sec->vma - off) % bed->maxpagesize;
1930 if (i == 0 && ! adjusted)
1931 p->p_offset += adjust;
1934 p->p_memsz += adjust;
1935 if ((flags & SEC_LOAD) != 0)
1936 p->p_filesz += adjust;
1943 if ((flags & SEC_LOAD) != 0)
1944 off += sec->_raw_size;
1947 p->p_memsz += sec->_raw_size;
1949 if ((flags & SEC_LOAD) != 0)
1950 p->p_filesz += sec->_raw_size;
1952 align = 1 << bfd_get_section_alignment (abfd, sec);
1953 if (align > p->p_align)
1956 if (! m->p_flags_valid)
1958 if ((flags & SEC_CODE) != 0)
1960 if ((flags & SEC_READONLY) == 0)
1966 /* Now that we have set the section file positions, we can set up
1967 the file positions for the non PT_LOAD segments. */
1968 for (m = elf_tdata (abfd)->segment_map, p = phdrs;
1972 if (p->p_type != PT_LOAD && m->count > 0)
1973 p->p_offset = m->sections[0]->filepos;
1974 if (p->p_type == PT_PHDR)
1976 p->p_offset = bed->s->sizeof_ehdr;
1977 p->p_filesz = count * bed->s->sizeof_phdr;
1978 p->p_memsz = p->p_filesz;
1982 /* Let the backend set up any program headers it might need. */
1983 if (bed->elf_backend_create_program_headers)
1984 count = ((*bed->elf_backend_create_program_headers)
1985 (abfd, phdrs, count));
1987 /* Clear out any program headers we allocated but did not use. */
1988 for (; count < alloc; count++, p++)
1990 memset (p, 0, sizeof *p);
1991 p->p_type = PT_NULL;
1994 elf_tdata (abfd)->phdr = phdrs;
1996 elf_tdata (abfd)->next_file_pos = off;
1998 /* Write out the program headers. */
1999 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0
2000 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0)
2006 /* Get the size of the program header.
2008 If this is called by the linker before any of the section VMA's are set, it
2009 can't calculate the correct value for a strange memory layout. This only
2010 happens when SIZEOF_HEADERS is used in a linker script. In this case,
2011 SORTED_HDRS is NULL and we assume the normal scenario of one text and one
2012 data segment (exclusive of .interp and .dynamic).
2014 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there
2015 will be two segments. */
2017 static bfd_size_type
2018 get_program_header_size (abfd)
2023 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2025 /* We can't return a different result each time we're called. */
2026 if (elf_tdata (abfd)->program_header_size != 0)
2027 return elf_tdata (abfd)->program_header_size;
2029 /* Assume we will need exactly two PT_LOAD segments: one for text
2030 and one for data. */
2033 s = bfd_get_section_by_name (abfd, ".interp");
2034 if (s != NULL && (s->flags & SEC_LOAD) != 0)
2036 /* If we have a loadable interpreter section, we need a
2037 PT_INTERP segment. In this case, assume we also need a
2038 PT_PHDR segment, although that may not be true for all
2043 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL)
2045 /* We need a PT_DYNAMIC segment. */
2049 /* Let the backend count up any program headers it might need. */
2050 if (bed->elf_backend_create_program_headers)
2051 segs = ((*bed->elf_backend_create_program_headers)
2052 (abfd, (Elf_Internal_Phdr *) NULL, segs));
2054 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr;
2055 return elf_tdata (abfd)->program_header_size;
2058 /* Work out the file positions of all the sections. This is called by
2059 _bfd_elf_compute_section_file_positions. All the section sizes and
2060 VMAs must be known before this is called.
2062 We do not consider reloc sections at this point, unless they form
2063 part of the loadable image. Reloc sections are assigned file
2064 positions in assign_file_positions_for_relocs, which is called by
2065 write_object_contents and final_link.
2067 We also don't set the positions of the .symtab and .strtab here. */
2070 assign_file_positions_except_relocs (abfd)
2073 struct elf_obj_tdata * const tdata = elf_tdata (abfd);
2074 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd);
2075 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd);
2077 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2079 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2081 Elf_Internal_Shdr **hdrpp;
2084 /* Start after the ELF header. */
2085 off = i_ehdrp->e_ehsize;
2087 /* We are not creating an executable, which means that we are
2088 not creating a program header, and that the actual order of
2089 the sections in the file is unimportant. */
2090 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2092 Elf_Internal_Shdr *hdr;
2095 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA)
2097 hdr->sh_offset = -1;
2100 if (i == tdata->symtab_section
2101 || i == tdata->strtab_section)
2103 hdr->sh_offset = -1;
2107 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2113 Elf_Internal_Shdr **hdrpp;
2115 /* Assign file positions for the loaded sections based on the
2116 assignment of sections to segments. */
2117 if (! assign_file_positions_for_segments (abfd))
2120 /* Assign file positions for the other sections. */
2122 off = elf_tdata (abfd)->next_file_pos;
2123 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++)
2125 Elf_Internal_Shdr *hdr;
2128 if (hdr->bfd_section != NULL
2129 && hdr->bfd_section->filepos != 0)
2130 hdr->sh_offset = hdr->bfd_section->filepos;
2131 else if ((hdr->sh_flags & SHF_ALLOC) != 0)
2133 ((*_bfd_error_handler)
2134 ("%s: warning: allocated section `%s' not in segment",
2135 bfd_get_filename (abfd),
2136 (hdr->bfd_section == NULL
2138 : hdr->bfd_section->name)));
2139 off += (hdr->sh_addr - off) % bed->maxpagesize;
2140 off = _bfd_elf_assign_file_position_for_section (hdr, off,
2143 else if (hdr->sh_type == SHT_REL
2144 || hdr->sh_type == SHT_RELA
2145 || hdr == i_shdrpp[tdata->symtab_section]
2146 || hdr == i_shdrpp[tdata->strtab_section])
2147 hdr->sh_offset = -1;
2149 off = _bfd_elf_assign_file_position_for_section (hdr, off, true);
2153 /* Place the section headers. */
2154 off = align_file_position (off, bed->s->file_align);
2155 i_ehdrp->e_shoff = off;
2156 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2158 elf_tdata (abfd)->next_file_pos = off;
2167 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2168 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2169 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2171 struct bfd_strtab_hash *shstrtab;
2172 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2174 i_ehdrp = elf_elfheader (abfd);
2175 i_shdrp = elf_elfsections (abfd);
2177 shstrtab = _bfd_elf_stringtab_init ();
2178 if (shstrtab == NULL)
2181 elf_shstrtab (abfd) = shstrtab;
2183 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2184 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2185 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2186 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2188 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass;
2189 i_ehdrp->e_ident[EI_DATA] =
2190 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2191 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
2193 for (count = EI_PAD; count < EI_NIDENT; count++)
2194 i_ehdrp->e_ident[count] = 0;
2196 if ((abfd->flags & DYNAMIC) != 0)
2197 i_ehdrp->e_type = ET_DYN;
2198 else if ((abfd->flags & EXEC_P) != 0)
2199 i_ehdrp->e_type = ET_EXEC;
2201 i_ehdrp->e_type = ET_REL;
2203 switch (bfd_get_arch (abfd))
2205 case bfd_arch_unknown:
2206 i_ehdrp->e_machine = EM_NONE;
2208 case bfd_arch_sparc:
2209 if (bed->s->arch_size == 64)
2210 i_ehdrp->e_machine = EM_SPARC64;
2212 i_ehdrp->e_machine = EM_SPARC;
2215 i_ehdrp->e_machine = EM_386;
2218 i_ehdrp->e_machine = EM_68K;
2221 i_ehdrp->e_machine = EM_88K;
2224 i_ehdrp->e_machine = EM_860;
2226 case bfd_arch_mips: /* MIPS Rxxxx */
2227 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2230 i_ehdrp->e_machine = EM_PARISC;
2232 case bfd_arch_powerpc:
2233 i_ehdrp->e_machine = EM_PPC;
2235 /* start-sanitize-arc */
2237 i_ehdrp->e_machine = EM_CYGNUS_ARC;
2239 /* end-sanitize-arc */
2240 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2242 i_ehdrp->e_machine = EM_NONE;
2244 i_ehdrp->e_version = bed->s->ev_current;
2245 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr;
2247 /* no program header, for now. */
2248 i_ehdrp->e_phoff = 0;
2249 i_ehdrp->e_phentsize = 0;
2250 i_ehdrp->e_phnum = 0;
2252 /* each bfd section is section header entry */
2253 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2254 i_ehdrp->e_shentsize = bed->s->sizeof_shdr;
2256 /* if we're building an executable, we'll need a program header table */
2257 if (abfd->flags & EXEC_P)
2259 /* it all happens later */
2261 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2263 /* elf_build_phdrs() returns a (NULL-terminated) array of
2264 Elf_Internal_Phdrs */
2265 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2266 i_ehdrp->e_phoff = outbase;
2267 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2272 i_ehdrp->e_phentsize = 0;
2274 i_ehdrp->e_phoff = 0;
2277 elf_tdata (abfd)->symtab_hdr.sh_name =
2278 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false);
2279 elf_tdata (abfd)->strtab_hdr.sh_name =
2280 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false);
2281 elf_tdata (abfd)->shstrtab_hdr.sh_name =
2282 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false);
2283 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2284 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1
2285 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1)
2291 /* Assign file positions for all the reloc sections which are not part
2292 of the loadable file image. */
2295 _bfd_elf_assign_file_positions_for_relocs (abfd)
2300 Elf_Internal_Shdr **shdrpp;
2302 off = elf_tdata (abfd)->next_file_pos;
2304 for (i = 1, shdrpp = elf_elfsections (abfd) + 1;
2305 i < elf_elfheader (abfd)->e_shnum;
2308 Elf_Internal_Shdr *shdrp;
2311 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA)
2312 && shdrp->sh_offset == -1)
2313 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true);
2316 elf_tdata (abfd)->next_file_pos = off;
2320 _bfd_elf_write_object_contents (abfd)
2323 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2324 Elf_Internal_Ehdr *i_ehdrp;
2325 Elf_Internal_Shdr **i_shdrp;
2329 if (! abfd->output_has_begun
2330 && ! _bfd_elf_compute_section_file_positions (abfd,
2331 (struct bfd_link_info *) NULL))
2334 i_shdrp = elf_elfsections (abfd);
2335 i_ehdrp = elf_elfheader (abfd);
2338 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed);
2341 _bfd_elf_assign_file_positions_for_relocs (abfd);
2343 /* After writing the headers, we need to write the sections too... */
2344 for (count = 1; count < i_ehdrp->e_shnum; count++)
2346 if (bed->elf_backend_section_processing)
2347 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2348 if (i_shdrp[count]->contents)
2350 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2351 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2353 != i_shdrp[count]->sh_size))
2358 /* Write out the section header names. */
2359 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0
2360 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd)))
2363 if (bed->elf_backend_final_write_processing)
2364 (*bed->elf_backend_final_write_processing) (abfd,
2365 elf_tdata (abfd)->linker);
2367 return bed->s->write_shdrs_and_ehdr (abfd);
2370 /* given a section, search the header to find them... */
2372 _bfd_elf_section_from_bfd_section (abfd, asect)
2376 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2377 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2379 Elf_Internal_Shdr *hdr;
2380 int maxindex = elf_elfheader (abfd)->e_shnum;
2382 for (index = 0; index < maxindex; index++)
2384 hdr = i_shdrp[index];
2385 if (hdr->bfd_section == asect)
2389 if (bed->elf_backend_section_from_bfd_section)
2391 for (index = 0; index < maxindex; index++)
2395 hdr = i_shdrp[index];
2397 if ((*bed->elf_backend_section_from_bfd_section)
2398 (abfd, hdr, asect, &retval))
2403 if (bfd_is_abs_section (asect))
2405 if (bfd_is_com_section (asect))
2407 if (bfd_is_und_section (asect))
2413 /* given a symbol, return the bfd index for that symbol. */
2415 _bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2417 struct symbol_cache_entry **asym_ptr_ptr;
2419 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2421 flagword flags = asym_ptr->flags;
2423 /* When gas creates relocations against local labels, it creates its
2424 own symbol for the section, but does put the symbol into the
2425 symbol chain, so udata is 0. When the linker is generating
2426 relocatable output, this section symbol may be for one of the
2427 input sections rather than the output section. */
2428 if (asym_ptr->udata.i == 0
2429 && (flags & BSF_SECTION_SYM)
2430 && asym_ptr->section)
2434 if (asym_ptr->section->output_section != NULL)
2435 indx = asym_ptr->section->output_section->index;
2437 indx = asym_ptr->section->index;
2438 if (elf_section_syms (abfd)[indx])
2439 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i;
2442 idx = asym_ptr->udata.i;
2443 BFD_ASSERT (idx != 0);
2448 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n",
2449 (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
2457 /* Copy private section information. This copies over the entsize
2458 field, and sometimes the info field. */
2461 _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec)
2467 Elf_Internal_Shdr *ihdr, *ohdr;
2469 if (ibfd->xvec->flavour != bfd_target_elf_flavour
2470 || obfd->xvec->flavour != bfd_target_elf_flavour)
2473 ihdr = &elf_section_data (isec)->this_hdr;
2474 ohdr = &elf_section_data (osec)->this_hdr;
2476 ohdr->sh_entsize = ihdr->sh_entsize;
2478 if (ihdr->sh_type == SHT_SYMTAB
2479 || ihdr->sh_type == SHT_DYNSYM)
2480 ohdr->sh_info = ihdr->sh_info;
2485 /* Copy private symbol information. If this symbol is in a section
2486 which we did not map into a BFD section, try to map the section
2487 index correctly. We use special macro definitions for the mapped
2488 section indices; these definitions are interpreted by the
2489 swap_out_syms function. */
2491 #define MAP_ONESYMTAB (SHN_LORESERVE - 1)
2492 #define MAP_DYNSYMTAB (SHN_LORESERVE - 2)
2493 #define MAP_STRTAB (SHN_LORESERVE - 3)
2494 #define MAP_SHSTRTAB (SHN_LORESERVE - 4)
2497 _bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg)
2503 elf_symbol_type *isym, *osym;
2505 isym = elf_symbol_from (ibfd, isymarg);
2506 osym = elf_symbol_from (obfd, osymarg);
2510 && bfd_is_abs_section (isym->symbol.section))
2514 shndx = isym->internal_elf_sym.st_shndx;
2515 if (shndx == elf_onesymtab (ibfd))
2516 shndx = MAP_ONESYMTAB;
2517 else if (shndx == elf_dynsymtab (ibfd))
2518 shndx = MAP_DYNSYMTAB;
2519 else if (shndx == elf_tdata (ibfd)->strtab_section)
2521 else if (shndx == elf_tdata (ibfd)->shstrtab_section)
2522 shndx = MAP_SHSTRTAB;
2523 osym->internal_elf_sym.st_shndx = shndx;
2529 /* Swap out the symbols. */
2532 swap_out_syms (abfd, sttp)
2534 struct bfd_strtab_hash **sttp;
2536 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2538 if (!elf_map_symbols (abfd))
2541 /* Dump out the symtabs. */
2543 int symcount = bfd_get_symcount (abfd);
2544 asymbol **syms = bfd_get_outsymbols (abfd);
2545 struct bfd_strtab_hash *stt;
2546 Elf_Internal_Shdr *symtab_hdr;
2547 Elf_Internal_Shdr *symstrtab_hdr;
2548 char *outbound_syms;
2551 stt = _bfd_elf_stringtab_init ();
2555 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2556 symtab_hdr->sh_type = SHT_SYMTAB;
2557 symtab_hdr->sh_entsize = bed->s->sizeof_sym;
2558 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2559 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2560 symtab_hdr->sh_addralign = bed->s->file_align;
2562 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2563 symstrtab_hdr->sh_type = SHT_STRTAB;
2565 outbound_syms = bfd_alloc (abfd,
2566 (1 + symcount) * bed->s->sizeof_sym);
2567 if (outbound_syms == NULL)
2569 bfd_set_error (bfd_error_no_memory);
2572 symtab_hdr->contents = (PTR) outbound_syms;
2574 /* now generate the data (for "contents") */
2576 /* Fill in zeroth symbol and swap it out. */
2577 Elf_Internal_Sym sym;
2583 sym.st_shndx = SHN_UNDEF;
2584 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
2585 outbound_syms += bed->s->sizeof_sym;
2587 for (idx = 0; idx < symcount; idx++)
2589 Elf_Internal_Sym sym;
2590 bfd_vma value = syms[idx]->value;
2591 elf_symbol_type *type_ptr;
2592 flagword flags = syms[idx]->flags;
2594 if (flags & BSF_SECTION_SYM)
2595 /* Section symbols have no names. */
2599 sym.st_name = (unsigned long) _bfd_stringtab_add (stt,
2602 if (sym.st_name == (unsigned long) -1)
2606 type_ptr = elf_symbol_from (abfd, syms[idx]);
2608 if (bfd_is_com_section (syms[idx]->section))
2610 /* ELF common symbols put the alignment into the `value' field,
2611 and the size into the `size' field. This is backwards from
2612 how BFD handles it, so reverse it here. */
2613 sym.st_size = value;
2614 if (type_ptr == NULL
2615 || type_ptr->internal_elf_sym.st_value == 0)
2616 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value));
2618 sym.st_value = type_ptr->internal_elf_sym.st_value;
2619 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd,
2620 syms[idx]->section);
2624 asection *sec = syms[idx]->section;
2627 if (sec->output_section)
2629 value += sec->output_offset;
2630 sec = sec->output_section;
2633 sym.st_value = value;
2634 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2636 if (bfd_is_abs_section (sec)
2638 && type_ptr->internal_elf_sym.st_shndx != 0)
2640 /* This symbol is in a real ELF section which we did
2641 not create as a BFD section. Undo the mapping done
2642 by copy_private_symbol_data. */
2643 shndx = type_ptr->internal_elf_sym.st_shndx;
2647 shndx = elf_onesymtab (abfd);
2650 shndx = elf_dynsymtab (abfd);
2653 shndx = elf_tdata (abfd)->strtab_section;
2656 shndx = elf_tdata (abfd)->shstrtab_section;
2664 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2670 /* Writing this would be a hell of a lot easier if
2671 we had some decent documentation on bfd, and
2672 knew what to expect of the library, and what to
2673 demand of applications. For example, it
2674 appears that `objcopy' might not set the
2675 section of a symbol to be a section that is
2676 actually in the output file. */
2677 sec2 = bfd_get_section_by_name (abfd, sec->name);
2678 BFD_ASSERT (sec2 != 0);
2679 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2);
2680 BFD_ASSERT (shndx != -1);
2684 sym.st_shndx = shndx;
2687 if (bfd_is_com_section (syms[idx]->section))
2688 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
2689 else if (bfd_is_und_section (syms[idx]->section))
2690 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK)
2693 ((flags & BSF_FUNCTION)
2696 else if (flags & BSF_SECTION_SYM)
2697 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2698 else if (flags & BSF_FILE)
2699 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2702 int bind = STB_LOCAL;
2703 int type = STT_OBJECT;
2705 if (flags & BSF_LOCAL)
2707 else if (flags & BSF_WEAK)
2709 else if (flags & BSF_GLOBAL)
2712 if (flags & BSF_FUNCTION)
2715 sym.st_info = ELF_ST_INFO (bind, type);
2719 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms);
2720 outbound_syms += bed->s->sizeof_sym;
2724 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt);
2725 symstrtab_hdr->sh_type = SHT_STRTAB;
2727 symstrtab_hdr->sh_flags = 0;
2728 symstrtab_hdr->sh_addr = 0;
2729 symstrtab_hdr->sh_entsize = 0;
2730 symstrtab_hdr->sh_link = 0;
2731 symstrtab_hdr->sh_info = 0;
2732 symstrtab_hdr->sh_addralign = 1;
2738 /* Return the number of bytes required to hold the symtab vector.
2740 Note that we base it on the count plus 1, since we will null terminate
2741 the vector allocated based on this size. However, the ELF symbol table
2742 always has a dummy entry as symbol #0, so it ends up even. */
2745 _bfd_elf_get_symtab_upper_bound (abfd)
2750 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
2752 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2753 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2759 _bfd_elf_get_dynamic_symtab_upper_bound (abfd)
2764 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr;
2766 if (elf_dynsymtab (abfd) == 0)
2768 bfd_set_error (bfd_error_invalid_operation);
2772 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym;
2773 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
2779 _bfd_elf_get_reloc_upper_bound (abfd, asect)
2783 return (asect->reloc_count + 1) * sizeof (arelent *);
2786 /* Canonicalize the relocs. */
2789 _bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols)
2798 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, section, symbols))
2801 tblptr = section->relocation;
2802 for (i = 0; i < section->reloc_count; i++)
2803 *relptr++ = tblptr++;
2807 return section->reloc_count;
2811 _bfd_elf_get_symtab (abfd, alocation)
2813 asymbol **alocation;
2815 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false);
2818 bfd_get_symcount (abfd) = symcount;
2823 _bfd_elf_canonicalize_dynamic_symtab (abfd, alocation)
2825 asymbol **alocation;
2827 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true);
2831 _bfd_elf_make_empty_symbol (abfd)
2834 elf_symbol_type *newsym;
2836 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
2839 bfd_set_error (bfd_error_no_memory);
2844 newsym->symbol.the_bfd = abfd;
2845 return &newsym->symbol;
2850 _bfd_elf_get_symbol_info (ignore_abfd, symbol, ret)
2855 bfd_symbol_info (symbol, ret);
2859 _bfd_elf_get_lineno (ignore_abfd, symbol)
2868 _bfd_elf_set_arch_mach (abfd, arch, machine)
2870 enum bfd_architecture arch;
2871 unsigned long machine;
2873 /* If this isn't the right architecture for this backend, and this
2874 isn't the generic backend, fail. */
2875 if (arch != get_elf_backend_data (abfd)->arch
2876 && arch != bfd_arch_unknown
2877 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown)
2880 return bfd_default_set_arch_mach (abfd, arch, machine);
2883 /* Find the nearest line to a particular section and offset, for error
2887 _bfd_elf_find_nearest_line (abfd,
2898 CONST char **filename_ptr;
2899 CONST char **functionname_ptr;
2900 unsigned int *line_ptr;
2902 const char *filename;
2906 if (symbols == NULL)
2912 for (p = symbols; *p != NULL; p++)
2916 q = (elf_symbol_type *) *p;
2918 if (bfd_get_section (&q->symbol) != section)
2921 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info))
2926 filename = bfd_asymbol_name (&q->symbol);
2930 || q->symbol.value <= offset)
2931 func = (asymbol *) q;
2939 *filename_ptr = filename;
2940 *functionname_ptr = bfd_asymbol_name (func);
2946 _bfd_elf_sizeof_headers (abfd, reloc)
2952 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr;
2954 ret += get_program_header_size (abfd);
2959 _bfd_elf_set_section_contents (abfd, section, location, offset, count)
2964 bfd_size_type count;
2966 Elf_Internal_Shdr *hdr;
2968 if (! abfd->output_has_begun
2969 && ! _bfd_elf_compute_section_file_positions (abfd,
2970 (struct bfd_link_info *) NULL))
2973 hdr = &elf_section_data (section)->this_hdr;
2975 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
2977 if (bfd_write (location, 1, count, abfd) != count)
2984 _bfd_elf_no_info_to_howto (abfd, cache_ptr, dst)
2987 Elf_Internal_Rela *dst;
2994 _bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
2997 Elf_Internal_Rel *dst;