home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / bfd / elfcode.h < prev    next >
C/C++ Source or Header  |  1994-02-03  |  104KB  |  3,534 lines

  1. /* ELF executable support for BFD.
  2.    Copyright 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4.    Written by Fred Fish @ Cygnus Support, from information published
  5.    in "UNIX System V Release 4, Programmers Guide: ANSI C and
  6.    Programming Support Tools".  Sufficient support for gdb.
  7.  
  8.    Rewritten by Mark Eichin @ Cygnus Support, from information
  9.    published in "System V Application Binary Interface", chapters 4
  10.    and 5, as well as the various "Processor Supplement" documents
  11.    derived from it. Added support for assembler and other object file
  12.    utilities.  Further work done by Ken Raeburn (Cygnus Support), Michael
  13.    Meissner (Open Software Foundation), and Peter Hoogenboom (University
  14.    of Utah) to finish and extend this.
  15.  
  16. This file is part of BFD, the Binary File Descriptor library.
  17.  
  18. This program is free software; you can redistribute it and/or modify
  19. it under the terms of the GNU General Public License as published by
  20. the Free Software Foundation; either version 2 of the License, or
  21. (at your option) any later version.
  22.  
  23. This program is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. GNU General Public License for more details.
  27.  
  28. You should have received a copy of the GNU General Public License
  29. along with this program; if not, write to the Free Software
  30. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  31.  
  32. /* Problems and other issues to resolve.
  33.  
  34.    (1)    BFD expects there to be some fixed number of "sections" in
  35.         the object file.  I.E. there is a "section_count" variable in the
  36.     bfd structure which contains the number of sections.  However, ELF
  37.     supports multiple "views" of a file.  In particular, with current
  38.     implementations, executable files typically have two tables, a
  39.     program header table and a section header table, both of which
  40.     partition the executable.
  41.  
  42.     In ELF-speak, the "linking view" of the file uses the section header
  43.     table to access "sections" within the file, and the "execution view"
  44.     uses the program header table to access "segments" within the file.
  45.     "Segments" typically may contain all the data from one or more
  46.     "sections".
  47.  
  48.     Note that the section header table is optional in ELF executables,
  49.     but it is this information that is most useful to gdb.  If the
  50.     section header table is missing, then gdb should probably try
  51.     to make do with the program header table.  (FIXME)
  52.  
  53.    (2)  The code in this file is compiled twice, once in 32-bit mode and
  54.     once in 64-bit mode.  More of it should be made size-independent
  55.     and moved into elf.c.
  56.  
  57.    (3)    ELF section symbols are handled rather sloppily now.  This should
  58.     be cleaned up, and ELF section symbols reconciled with BFD section
  59.     symbols.
  60.  */
  61.  
  62. #include <assert.h>
  63. #include <string.h>        /* For strrchr and friends */
  64. #include "bfd.h"
  65. #include "sysdep.h"
  66. #include "libbfd.h"
  67. #include "libelf.h"
  68.  
  69. #ifndef alloca
  70. PTR alloca ();
  71. #endif
  72.  
  73. /* Renaming structures, typedefs, macros and functions to be size-specific.  */
  74. #define Elf_External_Ehdr    NAME(Elf,External_Ehdr)
  75. #define Elf_External_Sym    NAME(Elf,External_Sym)
  76. #define Elf_External_Shdr    NAME(Elf,External_Shdr)
  77. #define Elf_External_Phdr    NAME(Elf,External_Phdr)
  78. #define Elf_External_Rel    NAME(Elf,External_Rel)
  79. #define Elf_External_Rela    NAME(Elf,External_Rela)
  80.  
  81. #define elf_core_file_failing_command    NAME(bfd_elf,core_file_failing_command)
  82. #define elf_core_file_failing_signal    NAME(bfd_elf,core_file_failing_signal)
  83. #define elf_core_file_matches_executable_p NAME(bfd_elf,core_file_matches_executable_p)
  84. #define elf_object_p            NAME(bfd_elf,object_p)
  85. #define elf_core_file_p            NAME(bfd_elf,core_file_p)
  86. #define elf_get_symtab_upper_bound    NAME(bfd_elf,get_symtab_upper_bound)
  87. #define elf_get_reloc_upper_bound    NAME(bfd_elf,get_reloc_upper_bound)
  88. #define elf_canonicalize_reloc        NAME(bfd_elf,canonicalize_reloc)
  89. #define elf_get_symtab            NAME(bfd_elf,get_symtab)
  90. #define elf_make_empty_symbol        NAME(bfd_elf,make_empty_symbol)
  91. #define elf_get_symbol_info        NAME(bfd_elf,get_symbol_info)
  92. #define elf_print_symbol        NAME(bfd_elf,print_symbol)
  93. #define elf_get_lineno            NAME(bfd_elf,get_lineno)
  94. #define elf_set_arch_mach        NAME(bfd_elf,set_arch_mach)
  95. #define elf_find_nearest_line        NAME(bfd_elf,find_nearest_line)
  96. #define elf_sizeof_headers        NAME(bfd_elf,sizeof_headers)
  97. #define elf_set_section_contents    NAME(bfd_elf,set_section_contents)
  98. #define elf_no_info_to_howto        NAME(bfd_elf,no_info_to_howto)
  99. #define elf_no_info_to_howto_rel    NAME(bfd_elf,no_info_to_howto_rel)
  100. #define elf_new_section_hook        NAME(bfd_elf,new_section_hook)
  101. #define write_relocs            NAME(bfd_elf,_write_relocs)
  102. #define elf_find_section        NAME(bfd_elf,find_section)
  103.  
  104. #if ARCH_SIZE == 64
  105. #define ELF_R_INFO(X,Y)    ELF64_R_INFO(X,Y)
  106. #define ELF_R_SYM(X)    ELF64_R_SYM(X)
  107. #define ELFCLASS    ELFCLASS64
  108. #define FILE_ALIGN    8
  109. #endif
  110. #if ARCH_SIZE == 32
  111. #define ELF_R_INFO(X,Y)    ELF32_R_INFO(X,Y)
  112. #define ELF_R_SYM(X)    ELF32_R_SYM(X)
  113. #define ELFCLASS    ELFCLASS32
  114. #define FILE_ALIGN    4
  115. #endif
  116.  
  117. static int shstrtab_length_fixed;
  118.  
  119. struct elf_sect_data {
  120.   int reloc_sec;
  121.   /* more? */
  122. };
  123.  
  124. /* Forward declarations of static functions */
  125.  
  126. static struct sec * section_from_elf_index PARAMS ((bfd *, int));
  127.  
  128. static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
  129.  
  130. static boolean elf_slurp_symbol_table PARAMS ((bfd *, asymbol **));
  131.  
  132. static int elf_symbol_from_bfd_symbol PARAMS ((bfd *,
  133.                            struct symbol_cache_entry **));
  134.  
  135. static void elf_map_symbols PARAMS ((bfd *));
  136. static void swap_out_syms PARAMS ((bfd *));
  137.  
  138. #ifdef DEBUG
  139. static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *));
  140. static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
  141. #endif
  142.  
  143. #define elf_string_from_elf_strtab(abfd,strindex) \
  144.      elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
  145.  
  146.  
  147. /* Structure swapping routines */
  148.  
  149. /* Should perhaps use put_offset, put_word, etc.  For now, the two versions
  150.    can be handled by explicitly specifying 32 bits or "the long type".  */
  151. #if ARCH_SIZE == 64
  152. #define put_word    bfd_h_put_64
  153. #define get_word    bfd_h_get_64
  154. #endif
  155. #if ARCH_SIZE == 32
  156. #define put_word    bfd_h_put_32
  157. #define get_word    bfd_h_get_32
  158. #endif
  159.  
  160. /* Translate an ELF symbol in external format into an ELF symbol in internal
  161.    format. */
  162.  
  163. static void
  164. DEFUN (elf_swap_symbol_in, (abfd, src, dst),
  165.        bfd * abfd AND
  166.        Elf_External_Sym * src AND
  167.        Elf_Internal_Sym * dst)
  168. {
  169.   dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
  170.   dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
  171.   dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
  172.   dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
  173.   dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
  174.   dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
  175. }
  176.  
  177. /* Translate an ELF symbol in internal format into an ELF symbol in external
  178.    format. */
  179.  
  180. static void
  181. DEFUN (elf_swap_symbol_out, (abfd, src, dst),
  182.        bfd * abfd AND
  183.        Elf_Internal_Sym * src AND
  184.        Elf_External_Sym * dst)
  185. {
  186.   bfd_h_put_32 (abfd, src->st_name, dst->st_name);
  187.   put_word (abfd, src->st_value, dst->st_value);
  188.   put_word (abfd, src->st_size, dst->st_size);
  189.   bfd_h_put_8 (abfd, src->st_info, dst->st_info);
  190.   bfd_h_put_8 (abfd, src->st_other, dst->st_other);
  191.   bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
  192. }
  193.  
  194.  
  195. /* Translate an ELF file header in external format into an ELF file header in
  196.    internal format. */
  197.  
  198. static void
  199. DEFUN (elf_swap_ehdr_in, (abfd, src, dst),
  200.        bfd * abfd AND
  201.        Elf_External_Ehdr * src AND
  202.        Elf_Internal_Ehdr * dst)
  203. {
  204.   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
  205.   dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);
  206.   dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);
  207.   dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);
  208.   dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
  209.   dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
  210.   dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);
  211.   dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);
  212.   dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);
  213.   dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);
  214.   dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);
  215.   dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);
  216.   dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);
  217.   dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);
  218. }
  219.  
  220. /* Translate an ELF file header in internal format into an ELF file header in
  221.    external format. */
  222.  
  223. static void
  224. DEFUN (elf_swap_ehdr_out, (abfd, src, dst),
  225.        bfd * abfd AND
  226.        Elf_Internal_Ehdr * src AND
  227.        Elf_External_Ehdr * dst)
  228. {
  229.   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
  230.   /* note that all elements of dst are *arrays of unsigned char* already... */
  231.   bfd_h_put_16 (abfd, src->e_type, dst->e_type);
  232.   bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
  233.   bfd_h_put_32 (abfd, src->e_version, dst->e_version);
  234.   put_word (abfd, src->e_entry, dst->e_entry);
  235.   put_word (abfd, src->e_phoff, dst->e_phoff);
  236.   put_word (abfd, src->e_shoff, dst->e_shoff);
  237.   bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
  238.   bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
  239.   bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
  240.   bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
  241.   bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
  242.   bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
  243.   bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
  244. }
  245.  
  246.  
  247. /* Translate an ELF section header table entry in external format into an
  248.    ELF section header table entry in internal format. */
  249.  
  250. static void
  251. DEFUN (elf_swap_shdr_in, (abfd, src, dst),
  252.        bfd * abfd AND
  253.        Elf_External_Shdr * src AND
  254.        Elf_Internal_Shdr * dst)
  255. {
  256.   dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
  257.   dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
  258.   dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);
  259.   dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
  260.   dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
  261.   dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);
  262.   dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
  263.   dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
  264.   dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);
  265.   dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
  266.   /* we haven't done any processing on it yet, so... */
  267.   dst->rawdata = (void *) 0;
  268. }
  269.  
  270. /* Translate an ELF section header table entry in internal format into an
  271.    ELF section header table entry in external format. */
  272.  
  273. static void
  274. DEFUN (elf_swap_shdr_out, (abfd, src, dst),
  275.        bfd * abfd AND
  276.        Elf_Internal_Shdr * src AND
  277.        Elf_External_Shdr * dst)
  278. {
  279.   /* note that all elements of dst are *arrays of unsigned char* already... */
  280.   bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
  281.   bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
  282.   put_word (abfd, src->sh_flags, dst->sh_flags);
  283.   put_word (abfd, src->sh_addr, dst->sh_addr);
  284.   put_word (abfd, src->sh_offset, dst->sh_offset);
  285.   put_word (abfd, src->sh_size, dst->sh_size);
  286.   bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
  287.   bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
  288.   put_word (abfd, src->sh_addralign, dst->sh_addralign);
  289.   put_word (abfd, src->sh_entsize, dst->sh_entsize);
  290. }
  291.  
  292.  
  293. /* Translate an ELF program header table entry in external format into an
  294.    ELF program header table entry in internal format. */
  295.  
  296. static void
  297. DEFUN (elf_swap_phdr_in, (abfd, src, dst),
  298.        bfd * abfd AND
  299.        Elf_External_Phdr * src AND
  300.        Elf_Internal_Phdr * dst)
  301. {
  302.   dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
  303.   dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
  304.   dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
  305.   dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
  306.   dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
  307.   dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
  308.   dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
  309.   dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
  310. }
  311.  
  312. static void
  313. DEFUN (elf_swap_phdr_out, (abfd, src, dst),
  314.        bfd * abfd AND
  315.        Elf_Internal_Phdr * src AND
  316.        Elf_External_Phdr * dst)
  317. {
  318.   /* note that all elements of dst are *arrays of unsigned char* already... */
  319.   bfd_h_put_32 (abfd, src->p_type, dst->p_type);
  320.   put_word (abfd, src->p_offset, dst->p_offset);
  321.   put_word (abfd, src->p_vaddr, dst->p_vaddr);
  322.   put_word (abfd, src->p_paddr, dst->p_paddr);
  323.   put_word (abfd, src->p_filesz, dst->p_filesz);
  324.   put_word (abfd, src->p_memsz, dst->p_memsz);
  325.   bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
  326.   put_word (abfd, src->p_align, dst->p_align);
  327. }
  328.  
  329. /* Translate an ELF reloc from external format to internal format. */
  330. static INLINE void
  331. DEFUN (elf_swap_reloc_in, (abfd, src, dst),
  332.        bfd * abfd AND
  333.        Elf_External_Rel * src AND
  334.        Elf_Internal_Rel * dst)
  335. {
  336.   dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
  337.   dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
  338. }
  339.  
  340. static INLINE void
  341. DEFUN (elf_swap_reloca_in, (abfd, src, dst),
  342.        bfd * abfd AND
  343.        Elf_External_Rela * src AND
  344.        Elf_Internal_Rela * dst)
  345. {
  346.   dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
  347.   dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
  348.   dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend);
  349. }
  350.  
  351. /* Translate an ELF reloc from internal format to external format. */
  352. static INLINE void
  353. DEFUN (elf_swap_reloc_out, (abfd, src, dst),
  354.        bfd * abfd AND
  355.        Elf_Internal_Rel * src AND
  356.        Elf_External_Rel * dst)
  357. {
  358.   put_word (abfd, src->r_offset, dst->r_offset);
  359.   put_word (abfd, src->r_info, dst->r_info);
  360. }
  361.  
  362. static INLINE void
  363. DEFUN (elf_swap_reloca_out, (abfd, src, dst),
  364.        bfd * abfd AND
  365.        Elf_Internal_Rela * src AND
  366.        Elf_External_Rela * dst)
  367. {
  368.   put_word (abfd, src->r_offset, dst->r_offset);
  369.   put_word (abfd, src->r_info, dst->r_info);
  370.   put_word (abfd, src->r_addend, dst->r_addend);
  371. }
  372.  
  373.  
  374.  
  375. /* String table creation/manipulation routines */
  376.  
  377. static struct strtab *
  378. DEFUN (bfd_new_strtab, (abfd),
  379.        bfd * abfd)
  380. {
  381.   struct strtab *ss;
  382.  
  383.   ss = (struct strtab *) bfd_xmalloc (sizeof (struct strtab));
  384.   ss->tab = bfd_xmalloc (1);
  385.   BFD_ASSERT (ss->tab != 0);
  386.   *ss->tab = 0;
  387.   ss->nentries = 0;
  388.   ss->length = 1;
  389.  
  390.   return ss;
  391. }
  392.  
  393. static int
  394. DEFUN (bfd_add_to_strtab, (abfd, ss, str),
  395.        bfd * abfd AND
  396.        struct strtab *ss AND
  397.        CONST char *str)
  398. {
  399.   /* should search first, but for now: */
  400.   /* include the trailing NUL */
  401.   int ln = strlen (str) + 1;
  402.  
  403.   /* should this be using obstacks? */
  404.   ss->tab = realloc (ss->tab, ss->length + ln);
  405.  
  406.   BFD_ASSERT (ss->tab != 0);
  407.   strcpy (ss->tab + ss->length, str);
  408.   ss->nentries++;
  409.   ss->length += ln;
  410.  
  411.   return ss->length - ln;
  412. }
  413.  
  414. static int
  415. DEFUN (bfd_add_2_to_strtab, (abfd, ss, str, str2),
  416.        bfd * abfd AND
  417.        struct strtab *ss AND
  418.        char *str AND
  419.        CONST char *str2)
  420. {
  421.   /* should search first, but for now: */
  422.   /* include the trailing NUL */
  423.   int ln = strlen (str) + strlen (str2) + 1;
  424.  
  425.   /* should this be using obstacks? */
  426.   if (ss->length)
  427.     ss->tab = realloc (ss->tab, ss->length + ln);
  428.   else
  429.     ss->tab = bfd_xmalloc (ln);
  430.  
  431.   BFD_ASSERT (ss->tab != 0);
  432.   strcpy (ss->tab + ss->length, str);
  433.   strcpy (ss->tab + ss->length + strlen (str), str2);
  434.   ss->nentries++;
  435.   ss->length += ln;
  436.  
  437.   return ss->length - ln;
  438. }
  439.  
  440.  
  441. /* ELF .o/exec file reading */
  442.  
  443. /* Create a new bfd section from an ELF section header. */
  444.  
  445. static boolean
  446. DEFUN (bfd_section_from_shdr, (abfd, shindex),
  447.        bfd * abfd AND
  448.        unsigned int shindex)
  449. {
  450.   Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
  451.   Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
  452.   asection *newsect;
  453.   char *name;
  454.  
  455.   name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
  456.  
  457.   switch (hdr->sh_type)
  458.     {
  459.  
  460.     case SHT_NULL:
  461.       /* inactive section. Throw it away. */
  462.       return true;
  463.  
  464.     case SHT_PROGBITS:
  465.       /* Bits that get saved. This one is real. */
  466.       if (!hdr->rawdata)
  467.     {
  468.       newsect = bfd_make_section (abfd, name);
  469.       if (newsect != NULL)
  470.         {
  471.           newsect->filepos = hdr->sh_offset; /* so we can read back the bits */
  472.           newsect->flags |= SEC_HAS_CONTENTS;
  473.           newsect->vma = hdr->sh_addr;
  474.           newsect->_raw_size = hdr->sh_size;
  475.           newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
  476.  
  477.           if (hdr->sh_flags & SHF_ALLOC)
  478.         {
  479.           newsect->flags |= SEC_ALLOC;
  480.           newsect->flags |= SEC_LOAD;
  481.         }
  482.  
  483.           if (!(hdr->sh_flags & SHF_WRITE))
  484.         newsect->flags |= SEC_READONLY;
  485.  
  486.           if (hdr->sh_flags & SHF_EXECINSTR)
  487.         newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
  488.           else if (newsect->flags & SEC_ALLOC)
  489.         newsect->flags |= SEC_DATA;
  490.  
  491.           /* The debugging sections appear to recognized only by
  492.          name.  */
  493.           if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
  494.           || strncmp (name, ".line", sizeof ".line" - 1) == 0
  495.           || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
  496.         newsect->flags |= SEC_DEBUGGING;
  497.  
  498.           hdr->rawdata = (void *) newsect;
  499.         }
  500.       else
  501.         hdr->rawdata = (void *) bfd_get_section_by_name (abfd, name);
  502.     }
  503.       return true;
  504.  
  505.     case SHT_NOBITS:
  506.       /* Bits that get saved. This one is real. */
  507.       if (!hdr->rawdata)
  508.     {
  509.       newsect = bfd_make_section (abfd, name);
  510.       if (newsect != NULL)
  511.         {
  512.           newsect->vma = hdr->sh_addr;
  513.           newsect->_raw_size = hdr->sh_size;
  514.           newsect->filepos = hdr->sh_offset;    /* fake */
  515.           newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
  516.           if (hdr->sh_flags & SHF_ALLOC)
  517.         newsect->flags |= SEC_ALLOC;
  518.  
  519.           if (!(hdr->sh_flags & SHF_WRITE))
  520.         newsect->flags |= SEC_READONLY;
  521.  
  522.           /* FIXME: This section is empty.  Does it really make
  523.          sense to set SEC_CODE for it?  */
  524.           if (hdr->sh_flags & SHF_EXECINSTR)
  525.         newsect->flags |= SEC_CODE;    /* FIXME: may only contain SOME code */
  526.  
  527.           hdr->rawdata = (void *) newsect;
  528.         }
  529.     }
  530.       return true;
  531.  
  532.     case SHT_SYMTAB:        /* A symbol table */
  533.       if (elf_onesymtab (abfd) == shindex)
  534.     return true;
  535.  
  536.       BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
  537.       BFD_ASSERT (elf_onesymtab (abfd) == 0);
  538.       elf_onesymtab (abfd) = shindex;
  539.       elf_tdata(abfd)->symtab_hdr = *hdr;
  540.       elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->symtab_hdr;
  541.       abfd->flags |= HAS_SYMS;
  542.       return true;
  543.  
  544.     case SHT_STRTAB:        /* A string table */
  545.       if (hdr->rawdata)
  546.     return true;
  547.       if (ehdr->e_shstrndx == shindex)
  548.     {
  549.       elf_tdata(abfd)->shstrtab_hdr = *hdr;
  550.       elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->shstrtab_hdr;
  551.       hdr->rawdata = (PTR) &elf_tdata(abfd)->shstrtab_hdr;
  552.       return true;
  553.     }
  554.       {
  555.     int i;
  556.  
  557.     for (i = 1; i < ehdr->e_shnum; i++)
  558.       {
  559.         Elf_Internal_Shdr *hdr2 = elf_elfsections(abfd)[i];
  560.         if (hdr2->sh_link == shindex)
  561.           {
  562.         bfd_section_from_shdr (abfd, i);
  563.         if (elf_onesymtab (abfd) == i)
  564.           {
  565.             elf_tdata(abfd)->strtab_hdr = *hdr;
  566.             elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->strtab_hdr;
  567.             return true;
  568.           }
  569. #if 0 /* Not handling other string tables specially right now.  */
  570.         hdr2 = elf_elfsections(abfd)[i]; /* in case it moved */
  571.         /* We have a strtab for some random other section.  */
  572.         newsect = (asection *) hdr2->rawdata;
  573.         if (!newsect)
  574.           break;
  575.         hdr->rawdata = (PTR) newsect;
  576.         hdr2 = &elf_section_data (newsect)->str_hdr;
  577.         *hdr2 = *hdr;
  578.         elf_elfsections(abfd)[shindex] = hdr2;
  579. #endif
  580.           }
  581.       }
  582.       }
  583.  
  584.       newsect = bfd_make_section (abfd, name);
  585.       if (newsect)
  586.     {
  587.       newsect->flags = SEC_HAS_CONTENTS;
  588.       hdr->rawdata = (PTR) newsect;
  589.       newsect->_raw_size = hdr->sh_size;
  590.       newsect->alignment_power = 0;
  591.       newsect->vma = 0;
  592.       newsect->filepos = hdr->sh_offset;
  593.  
  594.       if (hdr->sh_flags & SHF_ALLOC)
  595.         newsect->flags |= SEC_ALLOC|SEC_LOAD;
  596.       if (!(hdr->sh_flags & SHF_WRITE))
  597.         newsect->flags |= SEC_READONLY;
  598.       if (hdr->sh_flags & SHF_EXECINSTR)
  599.         newsect->flags |= SEC_CODE;
  600.       else if (newsect->flags & SEC_ALLOC)
  601.         newsect->flags |= SEC_DATA;
  602.  
  603.       /* Check for debugging string tables.  */
  604.       if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
  605.           || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
  606.         newsect->flags |= SEC_DEBUGGING;
  607.     }
  608.  
  609.       return true;
  610.  
  611.     case SHT_REL:
  612.     case SHT_RELA:
  613.       /* *These* do a lot of work -- but build no sections!
  614.      The spec says there can be multiple strtabs, but only one symtab,
  615.      but there can be lots of REL* sections. */
  616.       /* FIXME:  The above statement is wrong!  There are typically at least
  617.      two symbol tables in a dynamically linked executable, ".dynsym"
  618.      which is the dynamic linkage symbol table and ".symtab", which is
  619.      the "traditional" symbol table.  -fnf */
  620.  
  621.       {
  622.     asection *target_sect;
  623.     Elf_Internal_Shdr *hdr2;
  624.     int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
  625.  
  626.     /* Don't allow REL relocations on a machine that uses RELA and
  627.        vice versa.  */
  628.     /* @@ Actually, the generic ABI does suggest that both might be
  629.        used in one file.  But the four ABI Processor Supplements I
  630.        have access to right now all specify that only one is used on
  631.        each of those architectures.  It's conceivable that, e.g., a
  632.        bunch of absolute 32-bit relocs might be more compact in REL
  633.        form even on a RELA machine...  */
  634.     BFD_ASSERT (!(use_rela_p && (hdr->sh_type == SHT_REL)));
  635.     BFD_ASSERT (!(!use_rela_p && (hdr->sh_type == SHT_RELA)));
  636.     BFD_ASSERT (hdr->sh_entsize ==
  637.             (use_rela_p
  638.              ? sizeof (Elf_External_Rela)
  639.              : sizeof (Elf_External_Rel)));
  640.  
  641.     bfd_section_from_shdr (abfd, hdr->sh_info);    /* target */
  642.     bfd_section_from_shdr (abfd, hdr->sh_link);    /* symbol table */
  643.     target_sect = section_from_elf_index (abfd, hdr->sh_info);
  644.     if (target_sect == NULL
  645.         || elf_section_data (target_sect) == NULL)
  646.       return false;
  647.  
  648.     hdr2 = &elf_section_data (target_sect)->rel_hdr;
  649.     *hdr2 = *hdr;
  650.     elf_elfsections(abfd)[shindex] = hdr2;
  651.     target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
  652.     target_sect->flags |= SEC_RELOC;
  653.     target_sect->relocation = 0;
  654.     target_sect->rel_filepos = hdr->sh_offset;
  655.     abfd->flags |= HAS_RELOC;
  656.     return true;
  657.       }
  658.       break;
  659.  
  660.     case SHT_HASH:
  661.     case SHT_DYNAMIC:
  662.     case SHT_DYNSYM:        /* could treat this like symtab... */
  663. #if 0
  664.       fprintf (stderr, "Dynamic Linking sections not yet supported.\n");
  665.       BFD_FAIL ();
  666. #endif
  667.       break;
  668.  
  669.     case SHT_NOTE:
  670. #if 0
  671.       fprintf (stderr, "Note Sections not yet supported.\n");
  672.       BFD_FAIL ();
  673. #endif
  674.       break;
  675.  
  676.     case SHT_SHLIB:
  677. #if 0
  678.       fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n");
  679. #endif
  680.       return true;
  681.  
  682.     default:
  683.       /* Check for any processor-specific section types.  */
  684.       {
  685.     struct elf_backend_data *bed = get_elf_backend_data (abfd);
  686.  
  687.     if (bed->elf_backend_section_from_shdr)
  688.       (*bed->elf_backend_section_from_shdr) (abfd, hdr, name);
  689.       }
  690.       break;
  691.     }
  692.  
  693.   return true;
  694. }
  695.  
  696. boolean
  697. DEFUN (elf_new_section_hook, (abfd, sec),
  698.        bfd *abfd
  699.        AND asection *sec)
  700. {
  701.   struct bfd_elf_section_data *sdata;
  702.  
  703.   sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
  704.   sec->used_by_bfd = (PTR) sdata;
  705.   memset (sdata, 0, sizeof (*sdata));
  706.   return true;
  707. }
  708.  
  709. /* Create a new bfd section from an ELF program header.
  710.  
  711.    Since program segments have no names, we generate a synthetic name
  712.    of the form segment<NUM>, where NUM is generally the index in the
  713.    program header table.  For segments that are split (see below) we
  714.    generate the names segment<NUM>a and segment<NUM>b.
  715.  
  716.    Note that some program segments may have a file size that is different than
  717.    (less than) the memory size.  All this means is that at execution the
  718.    system must allocate the amount of memory specified by the memory size,
  719.    but only initialize it with the first "file size" bytes read from the
  720.    file.  This would occur for example, with program segments consisting
  721.    of combined data+bss.
  722.  
  723.    To handle the above situation, this routine generates TWO bfd sections
  724.    for the single program segment.  The first has the length specified by
  725.    the file size of the segment, and the second has the length specified
  726.    by the difference between the two sizes.  In effect, the segment is split
  727.    into it's initialized and uninitialized parts.
  728.  
  729.  */
  730.  
  731. static boolean
  732. DEFUN (bfd_section_from_phdr, (abfd, hdr, index),
  733.        bfd * abfd AND
  734.        Elf_Internal_Phdr * hdr AND
  735.        int index)
  736. {
  737.   asection *newsect;
  738.   char *name;
  739.   char namebuf[64];
  740.   int split;
  741.  
  742.   split = ((hdr->p_memsz > 0) &&
  743.        (hdr->p_filesz > 0) &&
  744.        (hdr->p_memsz > hdr->p_filesz));
  745.   sprintf (namebuf, split ? "segment%da" : "segment%d", index);
  746.   name = bfd_alloc (abfd, strlen (namebuf) + 1);
  747.   strcpy (name, namebuf);
  748.   newsect = bfd_make_section (abfd, name);
  749.   newsect->vma = hdr->p_vaddr;
  750.   newsect->_raw_size = hdr->p_filesz;
  751.   newsect->filepos = hdr->p_offset;
  752.   newsect->flags |= SEC_HAS_CONTENTS;
  753.   if (hdr->p_type == PT_LOAD)
  754.     {
  755.       newsect->flags |= SEC_ALLOC;
  756.       newsect->flags |= SEC_LOAD;
  757.       if (hdr->p_flags & PF_X)
  758.     {
  759.       /* FIXME: all we known is that it has execute PERMISSION,
  760.          may be data. */
  761.       newsect->flags |= SEC_CODE;
  762.     }
  763.     }
  764.   if (!(hdr->p_flags & PF_W))
  765.     {
  766.       newsect->flags |= SEC_READONLY;
  767.     }
  768.  
  769.   if (split)
  770.     {
  771.       sprintf (namebuf, "segment%db", index);
  772.       name = bfd_alloc (abfd, strlen (namebuf) + 1);
  773.       strcpy (name, namebuf);
  774.       newsect = bfd_make_section (abfd, name);
  775.       newsect->vma = hdr->p_vaddr + hdr->p_filesz;
  776.       newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
  777.       if (hdr->p_type == PT_LOAD)
  778.     {
  779.       newsect->flags |= SEC_ALLOC;
  780.       if (hdr->p_flags & PF_X)
  781.         newsect->flags |= SEC_CODE;
  782.     }
  783.       if (!(hdr->p_flags & PF_W))
  784.     newsect->flags |= SEC_READONLY;
  785.     }
  786.  
  787.   return true;
  788. }
  789.  
  790. /* Begin processing a given object.
  791.  
  792.    First we validate the file by reading in the ELF header and checking
  793.    the magic number.  */
  794.  
  795. static INLINE boolean
  796. DEFUN (elf_file_p, (x_ehdrp), Elf_External_Ehdr * x_ehdrp)
  797. {
  798.   return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
  799.       && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
  800.       && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
  801.       && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
  802. }
  803.  
  804. /* Check to see if the file associated with ABFD matches the target vector
  805.    that ABFD points to.
  806.  
  807.    Note that we may be called several times with the same ABFD, but different
  808.    target vectors, most of which will not match.  We have to avoid leaving
  809.    any side effects in ABFD, or any data it points to (like tdata), if the
  810.    file does not match the target vector.
  811.  
  812.    FIXME:  There is memory leak if we are called more than once with the same
  813.    ABFD, and that bfd already has tdata allocated, since we allocate more tdata
  814.    and the old tdata is orphaned.  Since it's in the bfd obstack, there isn't
  815.    much we can do about this except possibly rewrite the code.  There are
  816.    also other bfd_allocs that may be the source of memory leaks as well. */
  817.  
  818. bfd_target *
  819. DEFUN (elf_object_p, (abfd), bfd * abfd)
  820. {
  821.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  822.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  823.   Elf_External_Shdr x_shdr;    /* Section header table entry, external form */
  824.   Elf_Internal_Shdr *i_shdrp;    /* Section header table, internal form */
  825.   int shindex;
  826.   char *shstrtab;        /* Internal copy of section header stringtab */
  827.   struct elf_backend_data *ebd;
  828.   struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
  829.  
  830.   /* Read in the ELF header in external format.  */
  831.  
  832.   if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
  833.     goto got_system_call_error;
  834.  
  835.   /* Now check to see if we have a valid ELF file, and one that BFD can
  836.      make use of.  The magic number must match, the address size ('class')
  837.      and byte-swapping must match our XVEC entry, and it must have a
  838.      section header table (FIXME: See comments re sections at top of this
  839.      file). */
  840.  
  841.   if ((elf_file_p (&x_ehdr) == false) ||
  842.       (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
  843.       (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
  844.     goto got_wrong_format_error;
  845.  
  846.   /* Check that file's byte order matches xvec's */
  847.   switch (x_ehdr.e_ident[EI_DATA])
  848.     {
  849.     case ELFDATA2MSB:        /* Big-endian */
  850.       if (!abfd->xvec->header_byteorder_big_p)
  851.     goto got_wrong_format_error;
  852.       break;
  853.     case ELFDATA2LSB:        /* Little-endian */
  854.       if (abfd->xvec->header_byteorder_big_p)
  855.     goto got_wrong_format_error;
  856.       break;
  857.     case ELFDATANONE:        /* No data encoding specified */
  858.     default:            /* Unknown data encoding specified */
  859.       goto got_wrong_format_error;
  860.     }
  861.  
  862.   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
  863.      the tdata pointer in the bfd.  FIXME:  memory leak, see above. */
  864.  
  865.   elf_tdata (abfd) =
  866.     (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
  867.   if (elf_tdata (abfd) == NULL)
  868.     goto got_no_memory_error;
  869.  
  870.   /* Now that we know the byte order, swap in the rest of the header */
  871.   i_ehdrp = elf_elfheader (abfd);
  872.   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
  873. #if DEBUG & 1
  874.   elf_debug_file (i_ehdrp);
  875. #endif
  876.  
  877.   /* If there is no section header table, we're hosed. */
  878.   if (i_ehdrp->e_shoff == 0)
  879.     goto got_wrong_format_error;
  880.  
  881.   /* As a simple sanity check, verify that the what BFD thinks is the
  882.      size of each section header table entry actually matches the size
  883.      recorded in the file. */
  884.   if (i_ehdrp->e_shentsize != sizeof (x_shdr))
  885.     goto got_wrong_format_error;
  886.  
  887.   ebd = get_elf_backend_data (abfd);
  888.  
  889.   /* Check that the ELF e_machine field matches what this particular
  890.      BFD format expects.  */
  891.   if (ebd->elf_machine_code != i_ehdrp->e_machine)
  892.     {
  893.       bfd_target **target_ptr;
  894.  
  895.       if (ebd->elf_machine_code != EM_NONE)
  896.     goto got_wrong_format_error;
  897.  
  898.       /* This is the generic ELF target.  Let it match any ELF target
  899.      for which we do not have a specific backend.  */
  900.       for (target_ptr = target_vector; *target_ptr != NULL; target_ptr++)
  901.     {
  902.       struct elf_backend_data *back;
  903.  
  904.       if ((*target_ptr)->flavour != bfd_target_elf_flavour)
  905.         continue;
  906.       back = (struct elf_backend_data *) (*target_ptr)->backend_data;
  907.       if (back->elf_machine_code == i_ehdrp->e_machine)
  908.         {
  909.           /* target_ptr is an ELF backend which matches this
  910.          object file, so reject the generic ELF target.  */
  911.           goto got_wrong_format_error;
  912.         }
  913.     }
  914.     }
  915.  
  916.  
  917.   /* Set the flags and architecture before calling the backend so that
  918.      it can override them.  */
  919.   if (i_ehdrp->e_type == ET_EXEC)
  920.     abfd->flags |= EXEC_P;
  921.   else if (i_ehdrp->e_type == ET_DYN)
  922.     abfd->flags |= DYNAMIC;
  923.  
  924.   bfd_default_set_arch_mach (abfd, ebd->arch, 0);
  925.  
  926.   /* Remember the entry point specified in the ELF file header. */
  927.   bfd_get_start_address (abfd) = i_ehdrp->e_entry;
  928.  
  929.   /* Let the backend double check the format and override global
  930.      information.  */
  931.   if (ebd->elf_backend_object_p)
  932.     {
  933.       if ((*ebd->elf_backend_object_p) (abfd) == false)
  934.     goto got_wrong_format_error;
  935.     }
  936.     
  937.   /* Allocate space for a copy of the section header table in
  938.      internal form, seek to the section header table in the file,
  939.      read it in, and convert it to internal form.  */
  940.   i_shdrp = (Elf_Internal_Shdr *)
  941.     bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum);
  942.   elf_elfsections (abfd) =
  943.     (Elf_Internal_Shdr **) bfd_alloc (abfd, sizeof (i_shdrp) * i_ehdrp->e_shnum);
  944.   if (!i_shdrp || !elf_elfsections(abfd))
  945.     goto got_no_memory_error;
  946.   if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1)
  947.     goto got_system_call_error;
  948.   for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
  949.     {
  950.       if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
  951.     goto got_system_call_error;
  952.       elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
  953.       elf_elfsections(abfd)[shindex] = i_shdrp + shindex;
  954.     }
  955.   if (i_ehdrp->e_shstrndx)
  956.     {
  957.       bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx);
  958.     }
  959.  
  960. #if 0
  961.   for (shindex = i_ehdrp->e_shnum - 1; shindex >= 0; shindex--)
  962.     {
  963.       if (!strcmp (elf_string_from_elf_strtab (abfd,
  964.                            i_shdrp[shindex].sh_name),
  965.            ".strtab"))
  966.     {
  967.       elf_tdata(abfd)->strtab_hdr = i_shdrp[shindex];
  968.       elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->strtab_hdr;
  969.     }
  970.       else if (!strcmp (elf_string_from_elf_strtab (abfd,
  971.                             i_shdrp[shindex].sh_name),
  972.             ".symtab"))
  973.     {
  974.       elf_tdata(abfd)->symtab_hdr = i_shdrp[shindex];
  975.       elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->symtab_hdr;
  976.       elf_onesymtab (abfd) = shindex;
  977.     }
  978.     }
  979. #endif
  980.  
  981.   /* Read in the string table containing the names of the sections.  We
  982.      will need the base pointer to this table later. */
  983.   /* We read this inline now, so that we don't have to go through
  984.      bfd_section_from_shdr with it (since this particular strtab is
  985.      used to find all of the ELF section names.) */
  986.  
  987.   shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
  988.   if (!shstrtab)
  989.     goto got_wrong_format_error;
  990.  
  991.   /* Once all of the section headers have been read and converted, we
  992.      can start processing them.  Note that the first section header is
  993.      a dummy placeholder entry, so we ignore it.
  994.  
  995.      We also watch for the symbol table section and remember the file
  996.      offset and section size for both the symbol table section and the
  997.      associated string table section. */
  998.  
  999.   for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
  1000.     {
  1001.       bfd_section_from_shdr (abfd, shindex);
  1002.     }
  1003.  
  1004.   return (abfd->xvec);
  1005.  
  1006.   /* If we are going to use goto's to avoid duplicating error setting
  1007.      and return(NULL) code, then this at least makes it more maintainable. */
  1008.  
  1009.  got_system_call_error:
  1010.   bfd_error = system_call_error;
  1011.   goto got_no_match;
  1012.  got_wrong_format_error:
  1013.   bfd_error = wrong_format;
  1014.   goto got_no_match;
  1015.  got_no_memory_error:
  1016.   bfd_error = no_memory;
  1017.   goto got_no_match;
  1018.  got_no_match:
  1019.   elf_tdata (abfd) = preserved_tdata;
  1020.   return (NULL);
  1021. }
  1022.  
  1023.  
  1024. /* ELF .o/exec file writing */
  1025.  
  1026. /* Takes a bfd and a symbol, returns a pointer to the elf specific area
  1027.    of the symbol if there is one.  */
  1028. static INLINE elf_symbol_type *
  1029. DEFUN (elf_symbol_from, (ignore_abfd, symbol),
  1030.        bfd * ignore_abfd AND
  1031.        asymbol * symbol)
  1032. {
  1033.   if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour)
  1034.     return 0;
  1035.  
  1036.   if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL)
  1037.     return 0;
  1038.  
  1039.   return (elf_symbol_type *) symbol;
  1040. }
  1041.  
  1042. /* Create ELF output from BFD sections.
  1043.  
  1044.    Essentially, just create the section header and forget about the program
  1045.    header for now.  */
  1046.  
  1047. static void
  1048. DEFUN (elf_make_sections, (abfd, asect, obj),
  1049.        bfd * abfd AND
  1050.        asection * asect AND
  1051.        PTR obj)
  1052. {
  1053.   /* most of what is in bfd_shdr_from_section goes in here... */
  1054.   /* and all of these sections generate at *least* one ELF section. */
  1055.   Elf_Internal_Shdr *this_hdr;
  1056.   this_hdr = &elf_section_data (asect)->this_hdr;
  1057.  
  1058.   this_hdr->sh_addr = asect->vma;
  1059.   this_hdr->sh_size = asect->_raw_size;
  1060.   /* contents already set by elf_set_section_contents */
  1061.  
  1062.   if (asect->flags & SEC_RELOC)
  1063.     {
  1064.       /* emit a reloc section, and thus strtab and symtab... */
  1065.       Elf_Internal_Shdr *rela_hdr;
  1066.       int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
  1067.  
  1068.       rela_hdr = &elf_section_data (asect)->rel_hdr;
  1069.  
  1070.       /* orelocation has the data, reloc_count has the count... */
  1071.       if (use_rela_p)
  1072.     {
  1073.       rela_hdr->sh_type = SHT_RELA;
  1074.       rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
  1075.     }
  1076.       else
  1077.     /* REL relocations */
  1078.     {
  1079.       rela_hdr->sh_type = SHT_REL;
  1080.       rela_hdr->sh_entsize = sizeof (Elf_External_Rel);
  1081.     }
  1082.       rela_hdr->sh_flags = 0;
  1083.       rela_hdr->sh_addr = 0;
  1084.       rela_hdr->sh_offset = 0;
  1085.  
  1086.       /* FIXME: Systems I've checked use an alignment of 4, but it is
  1087.      possible that some systems use a different alignment.  */
  1088.       rela_hdr->sh_addralign = 4;
  1089.  
  1090.       rela_hdr->size = 0;
  1091.     }
  1092.   if (asect->flags & SEC_ALLOC)
  1093.     {
  1094.       this_hdr->sh_flags |= SHF_ALLOC;
  1095.       if (asect->flags & SEC_LOAD)
  1096.     {
  1097.       /* @@ Do something with sh_type? */
  1098.     }
  1099.     }
  1100.   else
  1101.     {
  1102.       /* If this section is not part of the program image during
  1103.      execution, leave the address fields at 0.  */
  1104.       this_hdr->sh_addr = 0;
  1105.       asect->vma = 0;
  1106.     }
  1107.   if (!(asect->flags & SEC_READONLY))
  1108.     this_hdr->sh_flags |= SHF_WRITE;
  1109.  
  1110.   if (asect->flags & SEC_CODE)
  1111.     this_hdr->sh_flags |= SHF_EXECINSTR;
  1112. }
  1113.  
  1114. void
  1115. write_relocs (abfd, sec, xxx)
  1116.      bfd *abfd;
  1117.      asection *sec;
  1118.      PTR xxx;
  1119. {
  1120.   Elf_Internal_Shdr *rela_hdr;
  1121.   Elf_External_Rela *outbound_relocas;
  1122.   Elf_External_Rel *outbound_relocs;
  1123.   int idx;
  1124.   int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
  1125.   asymbol *last_sym = 0;
  1126.   int last_sym_idx = 9999999;    /* should always be written before use */
  1127.  
  1128.   if ((sec->flags & SEC_RELOC) == 0)
  1129.     return;
  1130.   /* Flags are sometimes inconsistent.  */
  1131.   if (sec->reloc_count == 0)
  1132.     return;
  1133.  
  1134.   rela_hdr = &elf_section_data (sec)->rel_hdr;
  1135.  
  1136.   rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
  1137.   rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size);
  1138.  
  1139.   /* orelocation has the data, reloc_count has the count... */
  1140.   if (use_rela_p)
  1141.     {
  1142.       outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
  1143.  
  1144.       for (idx = 0; idx < sec->reloc_count; idx++)
  1145.     {
  1146.       Elf_Internal_Rela dst_rela;
  1147.       Elf_External_Rela *src_rela;
  1148.       arelent *ptr;
  1149.       asymbol *sym;
  1150.       int n;
  1151.  
  1152.       ptr = sec->orelocation[idx];
  1153.       src_rela = outbound_relocas + idx;
  1154.       if (!(abfd->flags & EXEC_P))
  1155.         dst_rela.r_offset = ptr->address - sec->vma;
  1156.       else
  1157.         dst_rela.r_offset = ptr->address;
  1158.  
  1159.       sym = *ptr->sym_ptr_ptr;
  1160.       if (sym == last_sym)
  1161.         n = last_sym_idx;
  1162.       else
  1163.         {
  1164.           last_sym = sym;
  1165.           last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
  1166.         }
  1167.       dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
  1168.  
  1169.       dst_rela.r_addend = ptr->addend;
  1170.       elf_swap_reloca_out (abfd, &dst_rela, src_rela);
  1171.     }
  1172.     }
  1173.   else
  1174.     /* REL relocations */
  1175.     {
  1176.       outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
  1177.  
  1178.       for (idx = 0; idx < sec->reloc_count; idx++)
  1179.     {
  1180.       Elf_Internal_Rel dst_rel;
  1181.       Elf_External_Rel *src_rel;
  1182.       arelent *ptr;
  1183.       int n;
  1184.       asymbol *sym;
  1185.  
  1186.       ptr = sec->orelocation[idx];
  1187.       sym = *ptr->sym_ptr_ptr;
  1188.       src_rel = outbound_relocs + idx;
  1189.       if (!(abfd->flags & EXEC_P))
  1190.         dst_rel.r_offset = ptr->address - sec->vma;
  1191.       else
  1192.         dst_rel.r_offset = ptr->address;
  1193.  
  1194.       if (sym == last_sym)
  1195.         n = last_sym_idx;
  1196.       else
  1197.         {
  1198.           last_sym = sym;
  1199.           last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
  1200.         }
  1201.       dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
  1202.  
  1203.       elf_swap_reloc_out (abfd, &dst_rel, src_rel);
  1204.     }
  1205.     }
  1206. }
  1207.  
  1208. static void
  1209. fix_up_strtabs (abfd, asect, obj)
  1210.      bfd *abfd;
  1211.      asection *asect;
  1212.      PTR obj;
  1213. {
  1214.   Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr;
  1215.   int this_idx = elf_section_data(asect)->this_idx;
  1216.  
  1217.   /* @@ Check flags!  */
  1218.   if (!strncmp (asect->name, ".stab", 5)
  1219.       && !strcmp ("str", asect->name + strlen (asect->name) - 3))
  1220.     {
  1221.       size_t len = strlen (asect->name) + 1;
  1222.       char *s = (char *) alloca (len);
  1223.       strcpy (s, asect->name);
  1224.       s[len - 4] = 0;
  1225.       asect = bfd_get_section_by_name (abfd, s);
  1226.       if (!asect)
  1227.     abort ();
  1228.       elf_section_data(asect)->this_hdr.sh_link = this_idx;
  1229.       /* @@ Assuming 32 bits!  */
  1230.       elf_section_data(asect)->this_hdr.sh_entsize = 0xc;
  1231.  
  1232.       this_hdr->sh_type = SHT_STRTAB;
  1233.     }
  1234. }
  1235.  
  1236. static void
  1237. DEFUN (elf_fake_sections, (abfd, asect, obj),
  1238.        bfd * abfd AND
  1239.        asection * asect AND
  1240.        PTR obj)
  1241. {
  1242.   /* most of what is in bfd_shdr_from_section goes in here... */
  1243.   /* and all of these sections generate at *least* one ELF section. */
  1244.  
  1245.   Elf_Internal_Shdr *this_hdr;
  1246.   this_hdr = &elf_section_data (asect)->this_hdr;
  1247.   this_hdr->sh_name =
  1248.     bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name);
  1249.   /* We need to log the type *now* so that elf_section_from_bfd_section
  1250.      can find us... have to set rawdata too. */
  1251.   this_hdr->rawdata = (void *) asect;
  1252.   this_hdr->sh_addralign = 1 << asect->alignment_power;
  1253.   if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
  1254.     this_hdr->sh_type = SHT_PROGBITS;
  1255.   else if ((asect->flags & SEC_ALLOC) && ((asect->flags & SEC_LOAD) == 0))
  1256.     {
  1257.       BFD_ASSERT (strcmp (asect->name, ".bss") == 0
  1258.           || strcmp (asect->name, ".sbss") == 0);
  1259.       this_hdr->sh_type = SHT_NOBITS;
  1260.     }
  1261.   /* FIXME I am not sure how to detect a .note section from the flags
  1262.      word of an `asection'.  */
  1263.   else if (!strcmp (asect->name, ".note"))
  1264.     this_hdr->sh_type = SHT_NOTE;
  1265.   else
  1266.     this_hdr->sh_type = SHT_PROGBITS;
  1267.  
  1268.   this_hdr->sh_flags = 0;
  1269.   this_hdr->sh_addr = 0;
  1270.   this_hdr->sh_size = 0;
  1271.   this_hdr->sh_entsize = 0;
  1272.   this_hdr->sh_info = 0;
  1273.   this_hdr->sh_link = 0;
  1274.   this_hdr->sh_offset = 0;
  1275.   this_hdr->size = 0;
  1276.  
  1277.   /* Now, check for processor-specific section types.  */
  1278.   {
  1279.     struct elf_backend_data *bed = get_elf_backend_data (abfd);
  1280.  
  1281.     if (bed->elf_backend_fake_sections)
  1282.       (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
  1283.   }
  1284.  
  1285.   {
  1286.     /* Emit a strtab and symtab, and possibly a reloc section.  */
  1287.     Elf_Internal_Shdr *rela_hdr;
  1288.  
  1289.     /* Note that only one symtab is used, so just remember it
  1290.        for now.  */
  1291.  
  1292.     if (asect->flags & SEC_RELOC)
  1293.       {
  1294.     int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
  1295.  
  1296.     rela_hdr = &elf_section_data (asect)->rel_hdr;
  1297.     rela_hdr->sh_name =
  1298.       bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
  1299.                    use_rela_p ? ".rela" : ".rel",
  1300.                    asect->name);
  1301.     rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
  1302.     rela_hdr->sh_entsize = (use_rela_p
  1303.                 ? sizeof (Elf_External_Rela)
  1304.                 : sizeof (Elf_External_Rel));
  1305.  
  1306.     rela_hdr->sh_flags = 0;
  1307.     rela_hdr->sh_addr = 0;
  1308.     rela_hdr->sh_size = 0;
  1309.     rela_hdr->sh_offset = 0;
  1310.  
  1311.     /* FIXME: Systems I've checked use an alignment of 4, but some
  1312.        systems may use a different alignment.  */
  1313.     rela_hdr->sh_addralign = 4;
  1314.  
  1315.     rela_hdr->size = 0;
  1316.       }
  1317.   }
  1318.   if (asect->flags & SEC_ALLOC)
  1319.     {
  1320.       this_hdr->sh_flags |= SHF_ALLOC;
  1321.       if (asect->flags & SEC_LOAD)
  1322.     {
  1323.       /* @@ Do something with sh_type?  */
  1324.     }
  1325.     }
  1326.   if (!(asect->flags & SEC_READONLY))
  1327.     this_hdr->sh_flags |= SHF_WRITE;
  1328.   if (asect->flags & SEC_CODE)
  1329.     this_hdr->sh_flags |= SHF_EXECINSTR;
  1330. }
  1331.  
  1332. /* Map symbol from it's internal number to the external number, moving
  1333.    all local symbols to be at the head of the list.  */
  1334.  
  1335. static INLINE int
  1336. sym_is_global (abfd, sym)
  1337.      bfd *abfd;
  1338.      asymbol *sym;
  1339. {
  1340.   /* If the backend has a special mapping, use it.  */
  1341.   if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
  1342.     return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
  1343.         (abfd, sym));
  1344.  
  1345.   if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
  1346.     {
  1347.       if (sym->flags & BSF_LOCAL)
  1348.     abort ();
  1349.       return 1;
  1350.     }
  1351.   if (sym->section == 0)
  1352.     {
  1353.       /* Is this valid?  */
  1354.       abort ();
  1355.  
  1356.       return 1;
  1357.     }
  1358.   if (sym->section == &bfd_und_section)
  1359.     return 1;
  1360.   if (bfd_is_com_section (sym->section))
  1361.     return 1;
  1362.   if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE))
  1363.     return 0;
  1364.   return 0;
  1365. }
  1366.  
  1367. static void
  1368. DEFUN (elf_map_symbols, (abfd), bfd * abfd)
  1369. {
  1370.   int symcount = bfd_get_symcount (abfd);
  1371.   asymbol **syms = bfd_get_outsymbols (abfd);
  1372.   asymbol **sect_syms;
  1373.   int num_locals = 0;
  1374.   int num_globals = 0;
  1375.   int num_locals2 = 0;
  1376.   int num_globals2 = 0;
  1377.   int max_index = 0;
  1378.   int num_sections = 0;
  1379.   Elf_Sym_Extra *sym_extra;
  1380.   int idx;
  1381.   asection *asect;
  1382.  
  1383. #ifdef DEBUG
  1384.   fprintf (stderr, "elf_map_symbols\n");
  1385.   fflush (stderr);
  1386. #endif
  1387.  
  1388.   /* Add local symbols for each section for which there are relocs.
  1389.      FIXME: How can we tell which sections have relocs at this point?
  1390.      Will reloc_count always be accurate?  Actually, I think most ELF
  1391.      targets create section symbols for all sections anyhow.  */
  1392.   for (asect = abfd->sections; asect; asect = asect->next)
  1393.     {
  1394.       if (max_index < asect->index)
  1395.     max_index = asect->index;
  1396.     }
  1397.  
  1398.   max_index++;
  1399.   elf_num_section_syms (abfd) = max_index;
  1400.   sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
  1401.   elf_section_syms (abfd) = sect_syms;
  1402.  
  1403.   BFD_ASSERT (sect_syms != 0);
  1404.  
  1405.   for (asect = abfd->sections; asect; asect = asect->next)
  1406.     {
  1407.       asymbol *sym = bfd_make_empty_symbol (abfd);
  1408.       sym->the_bfd = abfd;
  1409.       sym->name = asect->name;
  1410.       sym->value = asect->vma;
  1411.       sym->flags = BSF_SECTION_SYM;
  1412.       sym->section = asect;
  1413.       sect_syms[asect->index] = sym;
  1414.       num_sections++;
  1415. #ifdef DEBUG
  1416.       fprintf (stderr,
  1417.            "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
  1418.            asect->name, (long) asect->vma, asect->index, (long) asect);
  1419. #endif
  1420.     }
  1421.  
  1422.   if (num_sections)
  1423.     {
  1424.       if (syms)
  1425.     syms = (asymbol **) bfd_realloc (abfd, syms,
  1426.                      ((symcount + num_sections + 1)
  1427.                       * sizeof (asymbol *)));
  1428.       else
  1429.     syms = (asymbol **) bfd_alloc (abfd,
  1430.                        (num_sections + 1) * sizeof (asymbol *));
  1431.  
  1432.       for (asect = abfd->sections; asect; asect = asect->next)
  1433.     {
  1434.       if (sect_syms[asect->index])
  1435.         syms[symcount++] = sect_syms[asect->index];
  1436.     }
  1437.  
  1438.       syms[symcount] = (asymbol *) 0;
  1439.       bfd_set_symtab (abfd, syms, symcount);
  1440.     }
  1441.  
  1442.   elf_sym_extra (abfd) = sym_extra
  1443.     = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra));
  1444.  
  1445.   /* Identify and classify all of the symbols.  */
  1446.   for (idx = 0; idx < symcount; idx++)
  1447.     {
  1448.       if (!sym_is_global (abfd, syms[idx]))
  1449.     num_locals++;
  1450.       else
  1451.     num_globals++;
  1452.     }
  1453.  
  1454.   /* Now provide mapping information.  Add +1 for skipping over the
  1455.      dummy symbol.  */
  1456.   for (idx = 0; idx < symcount; idx++)
  1457.     {
  1458.       syms[idx]->udata = (PTR) &sym_extra[idx];
  1459.       if (!sym_is_global (abfd, syms[idx]))
  1460.     sym_extra[idx].elf_sym_num = 1 + num_locals2++;
  1461.       else
  1462.     sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++;
  1463.     }
  1464.  
  1465.   elf_num_locals (abfd) = num_locals;
  1466.   elf_num_globals (abfd) = num_globals;
  1467. }
  1468.  
  1469. static void assign_section_numbers ();
  1470. static void assign_file_positions_except_relocs ();
  1471.  
  1472. static boolean
  1473. DEFUN (elf_compute_section_file_positions, (abfd), bfd * abfd)
  1474. {
  1475.   bfd_map_over_sections (abfd, elf_fake_sections, 0);
  1476.  
  1477.   assign_section_numbers (abfd);
  1478.  
  1479.   bfd_map_over_sections (abfd, elf_make_sections, 0);
  1480.  
  1481.   bfd_map_over_sections (abfd, fix_up_strtabs, 0); /* .stab/.stabstr &c */
  1482.  
  1483.   swap_out_syms (abfd);
  1484.  
  1485.   assign_file_positions_except_relocs (abfd);
  1486.  
  1487.   return true;
  1488. }
  1489.  
  1490. static boolean
  1491. DEFUN (elf_write_phdrs, (abfd, i_ehdrp, i_phdrp, phdr_cnt),
  1492.        bfd * abfd AND
  1493.        Elf_Internal_Ehdr * i_ehdrp AND
  1494.        Elf_Internal_Phdr * i_phdrp AND
  1495.        Elf32_Half phdr_cnt)
  1496. {
  1497.   /* first program header entry goes after the file header */
  1498.   int outbase = i_ehdrp->e_phoff;
  1499.   int i;
  1500.   Elf_External_Phdr x_phdr;
  1501.  
  1502.   for (i = 0; i < phdr_cnt; i++)
  1503.     {
  1504.       elf_swap_phdr_out (abfd, i_phdrp + i, &x_phdr);
  1505.       bfd_seek (abfd, outbase, SEEK_SET);
  1506.       bfd_write ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd);
  1507.       outbase += sizeof (x_phdr);
  1508.     }
  1509.  
  1510.   return true;
  1511. }
  1512.  
  1513. static const Elf_Internal_Shdr null_shdr;
  1514.  
  1515. /* Assign all ELF section numbers.  The dummy first section is handled here
  1516.    too.  The link/info pointers for the standard section types are filled
  1517.    in here too, while we're at it.  (Link pointers for .stab sections are
  1518.    not filled in here.)  */
  1519. static void
  1520. assign_section_numbers (abfd)
  1521.      bfd *abfd;
  1522. {
  1523.   struct elf_obj_tdata *t = elf_tdata (abfd);
  1524.   asection *sec;
  1525.   int section_number = 1;
  1526.   int i;
  1527.   Elf_Internal_Shdr **i_shdrp;
  1528.  
  1529.   t->shstrtab_hdr.sh_size = elf_shstrtab(abfd)->length;
  1530.   t->shstrtab_hdr.contents = (void *) elf_shstrtab(abfd)->tab;
  1531.   shstrtab_length_fixed = 1;
  1532.  
  1533.   t->shstrtab_section = section_number++;
  1534.   elf_elfheader(abfd)->e_shstrndx = t->shstrtab_section;
  1535.   if (abfd->symcount)
  1536.     {
  1537.       t->symtab_section = section_number++;
  1538.       t->strtab_section = section_number++;
  1539.       t->symtab_hdr.sh_link = t->strtab_section;
  1540.     }
  1541.   for (sec = abfd->sections; sec; sec = sec->next)
  1542.     {
  1543.       struct bfd_elf_section_data *d = elf_section_data (sec);
  1544.       d->this_idx = section_number++;
  1545.       if (sec->flags & SEC_RELOC)
  1546.     {
  1547.       d->rel_idx = section_number++;
  1548.       d->rel_hdr.sh_link = t->symtab_section;
  1549.       d->rel_hdr.sh_info = d->this_idx;
  1550.     }
  1551.       else
  1552.     d->rel_idx = 0;
  1553.       /* No handling for per-section string tables currently.  */
  1554.     }
  1555.   elf_elfheader(abfd)->e_shnum = section_number;
  1556.  
  1557.   /* Set up the list of section header pointers, in agreement with the
  1558.      indices.  */
  1559.   i_shdrp = (Elf_Internal_Shdr **)
  1560.     bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *));
  1561.   elf_elfsections(abfd) = i_shdrp;
  1562.   for (i = 0; i < section_number; i++)
  1563.     i_shdrp[i] = 0;
  1564.  
  1565.   i_shdrp[0] = (Elf_Internal_Shdr *) &null_shdr;
  1566.   i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
  1567.   if (abfd->symcount)
  1568.     {
  1569.       i_shdrp[t->symtab_section] = &t->symtab_hdr;
  1570.       i_shdrp[t->strtab_section] = &t->strtab_hdr;
  1571.     }
  1572.   for (sec = abfd->sections; sec; sec = sec->next)
  1573.     {
  1574.       struct bfd_elf_section_data *d = elf_section_data (sec);
  1575.       i_shdrp[d->this_idx] = &d->this_hdr;
  1576.       if (d->rel_idx)
  1577.     i_shdrp[d->rel_idx] = &d->rel_hdr;
  1578.     }
  1579.   /* Make sure we got everything.... */
  1580.   for (i = 0; i < section_number; i++)
  1581.     if (i_shdrp[i] == 0)
  1582.       abort ();
  1583. }
  1584.  
  1585. static INLINE file_ptr
  1586. assign_file_position_for_section (i_shdrp, offset)
  1587.      Elf_Internal_Shdr *i_shdrp;
  1588.      file_ptr offset;
  1589. {
  1590.   int align;
  1591.  
  1592.   if (i_shdrp->sh_addralign != 0)
  1593.     align = i_shdrp->sh_addralign;
  1594.   else
  1595.     align = 1;
  1596.   i_shdrp->sh_offset = offset = BFD_ALIGN (offset, align);
  1597.   if (i_shdrp->rawdata != NULL)
  1598.     ((asection *) i_shdrp->rawdata)->filepos = offset;
  1599.   if (i_shdrp->sh_type != SHT_NOBITS)
  1600.     offset += i_shdrp->sh_size;
  1601.   return offset;
  1602. }
  1603.  
  1604. static INLINE file_ptr
  1605. align_file_position (off)
  1606.      file_ptr off;
  1607. {
  1608.   return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1);
  1609. }
  1610.  
  1611. static INLINE file_ptr
  1612. assign_file_positions_for_symtab_and_strtabs (abfd, off)
  1613.      bfd *abfd;
  1614.      file_ptr off;
  1615. {
  1616.   struct elf_obj_tdata *t = elf_tdata (abfd);
  1617.  
  1618.   off = align_file_position (off);
  1619.   off = assign_file_position_for_section (&t->symtab_hdr, off);
  1620.   off = assign_file_position_for_section (&t->shstrtab_hdr, off);
  1621.   off = assign_file_position_for_section (&t->strtab_hdr, off);
  1622.   return off;
  1623. }
  1624.  
  1625. struct seg_info {
  1626.   bfd_vma low, mem_size;
  1627.   file_ptr file_size;
  1628.   int start_pos;
  1629.   int sh_flags;
  1630.   struct seg_info *next;
  1631. };
  1632.  
  1633. static void
  1634. map_program_segments (abfd)
  1635.      bfd *abfd;
  1636. {
  1637.   Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
  1638.   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
  1639.   Elf_Internal_Shdr *i_shdrp;
  1640.   Elf_Internal_Phdr *phdr;
  1641.   char *done;
  1642.   int i, n_left = 0;
  1643.   file_ptr lowest_offset = 0;
  1644.   struct seg_info *seg = 0;
  1645.  
  1646.   done = (char *) alloca (i_ehdrp->e_shnum);
  1647.   memset (done, 0, i_ehdrp->e_shnum);
  1648.   for (i = 1; i < i_ehdrp->e_shnum; i++)
  1649.     {
  1650.       i_shdrp = i_shdrpp[i];
  1651.       /* If it's going to be mapped in, it's been assigned a position.  */
  1652.       if (i_shdrp->sh_offset + 1 == 0)
  1653.     {
  1654.       /* Well, not really, but we won't process it here.   */
  1655.       done[i] = 1;
  1656.       continue;
  1657.     }
  1658.       if (i_shdrp->sh_offset < lowest_offset
  1659.       || lowest_offset == 0)
  1660.     lowest_offset = i_shdrp->sh_offset;
  1661.       /* Only interested in PROGBITS or NOBITS for generating segments.  */
  1662.       switch (i_shdrp->sh_type)
  1663.     {
  1664.     case SHT_PROGBITS:
  1665.     case SHT_NOBITS:
  1666.       break;
  1667.     default:
  1668.       done[i] = 1;
  1669.     }
  1670.       if (!done[i])
  1671.     n_left++;
  1672.     }
  1673.   while (n_left)
  1674.     {
  1675.       bfd_vma lowest_vma = -1, high;
  1676.       int low_sec = 0;
  1677.       int mem_size;
  1678.       int file_size = 0;
  1679.  
  1680.       for (i = 1; i < i_ehdrp->e_shnum; i++)
  1681.     {
  1682.       i_shdrp = i_shdrpp[i];
  1683.       if (!done[i] && i_shdrp->sh_addr < lowest_vma)
  1684.         {
  1685.           lowest_vma = i_shdrp->sh_addr;
  1686.           low_sec = i;
  1687.         }
  1688.     }
  1689.       if (low_sec == 0)
  1690.     abort ();
  1691.       /* So now we know the lowest vma of any unassigned sections; start
  1692.      a segment there.  */
  1693.       {
  1694.     struct seg_info *s;
  1695.     s = (struct seg_info *) bfd_alloc (abfd, sizeof (struct seg_info));
  1696.     s->next = seg;
  1697.     seg = s;
  1698.       }
  1699.       seg->low = lowest_vma;
  1700.       i_shdrp = i_shdrpp[low_sec];
  1701.       seg->start_pos = i_shdrp->sh_offset;
  1702.       seg->sh_flags = i_shdrp->sh_flags;
  1703.       done[low_sec] = 1, n_left--;
  1704.       mem_size = i_shdrp->sh_size;
  1705.       high = lowest_vma + i_shdrp->sh_size;
  1706.  
  1707.       if (i_shdrp->sh_type == SHT_PROGBITS)
  1708.     file_size = i_shdrp->sh_size;
  1709.  
  1710.       for (i = 1; i < i_ehdrp->e_shnum; i++)
  1711.     {
  1712.       file_ptr f1;
  1713.  
  1714.       if (done[i])
  1715.         continue;
  1716.       i_shdrp = i_shdrpp[i];
  1717.       /* position of next byte on disk */
  1718.       f1 = seg->start_pos + file_size;
  1719.       if (i_shdrp->sh_type == SHT_PROGBITS)
  1720.         {
  1721.           if (i_shdrp->sh_offset - f1 != i_shdrp->sh_addr - high)
  1722.         continue;
  1723.           if (file_size != mem_size)
  1724.         break;
  1725.         }
  1726.       else /* sh_type == NOBITS */
  1727.         {
  1728.           /* If the section in question has no contents in the disk
  1729.          file, we really don't care where it supposedly starts.
  1730.          But we don't want to bother merging it into this segment
  1731.          if it doesn't start on this memory page.  */
  1732.           bfd_vma page1, page2;
  1733.           bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
  1734.  
  1735.           /* page number in address space of current end of seg */
  1736.           page1 = (high - 1 + maxpagesize - 1) / maxpagesize;
  1737.           /* page number in address space of start of this section */
  1738.           page2 = (i_shdrp->sh_addr + maxpagesize - 1) / maxpagesize;
  1739.  
  1740.           if (page1 != page2)
  1741.         continue;
  1742.         }
  1743.       done[i] = 1, n_left--;
  1744.       if (i_shdrp->sh_type == SHT_PROGBITS)
  1745.         file_size = i_shdrp->sh_offset + i_shdrp->sh_size - seg->start_pos;
  1746.       mem_size = i_shdrp->sh_addr + i_shdrp->sh_size - seg->low;
  1747.       high = i_shdrp->sh_addr + i_shdrp->sh_size;
  1748.       i = 0;
  1749.     }
  1750.       seg->file_size = file_size;
  1751.       seg->mem_size = mem_size;
  1752.     }
  1753.   /* Now do something with the list of segments we've built up.  */
  1754.   {
  1755.     bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
  1756.     struct seg_info *s;
  1757.     int n_segs = 0;
  1758.     int sz;
  1759.  
  1760.     for (s = seg; s; s = s->next)
  1761.       {
  1762.     n_segs++;
  1763.       }
  1764.     i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
  1765.     sz = sizeof (Elf_External_Phdr) * n_segs;
  1766.     if (align_file_position (i_ehdrp->e_ehsize) + sz <= lowest_offset)
  1767.       i_ehdrp->e_phoff = align_file_position (i_ehdrp->e_ehsize);
  1768.     else
  1769.       {
  1770.     i_ehdrp->e_phoff = align_file_position (elf_tdata (abfd)->next_file_pos);
  1771.     elf_tdata (abfd)->next_file_pos = i_ehdrp->e_phoff + sz;
  1772.       }
  1773.     phdr = (Elf_Internal_Phdr*) bfd_alloc (abfd,
  1774.                        n_segs * sizeof (Elf_Internal_Phdr));
  1775.     elf_tdata (abfd)->phdr = phdr;
  1776.     while (seg)
  1777.       {
  1778.     phdr->p_type = PT_LOAD;    /* only type we really support so far */
  1779.     phdr->p_offset = seg->start_pos;
  1780.     phdr->p_vaddr = seg->low;
  1781.     phdr->p_paddr = 0;
  1782.     phdr->p_filesz = seg->file_size;
  1783.     phdr->p_memsz = seg->mem_size;
  1784.     phdr->p_flags = PF_R;
  1785.     phdr->p_align = maxpagesize; /* ? */
  1786.     if (seg->sh_flags & SHF_WRITE)
  1787.       /* SysVr4 ELF docs say "data segments normally have read, write,
  1788.          and execute permissions."  */
  1789.       phdr->p_flags |= (PF_W | PF_X);
  1790.     if (seg->sh_flags & SHF_EXECINSTR)
  1791.       phdr->p_flags |= PF_X;
  1792.     phdr++;
  1793.     seg = seg->next;
  1794.       }
  1795.     i_ehdrp->e_phnum = n_segs;
  1796.   }
  1797.   elf_write_phdrs (abfd, i_ehdrp, elf_tdata (abfd)->phdr, i_ehdrp->e_phnum);
  1798. }
  1799.  
  1800. static void
  1801. assign_file_positions_except_relocs (abfd)
  1802.      bfd *abfd;
  1803. {
  1804.   /* For now, we ignore the possibility of having program segments, which
  1805.      may require some alignment in the file.  That'll require padding, and
  1806.      some interesting calculations to optimize file space usage.
  1807.  
  1808.      Also, since the application may change the list of relocations for
  1809.      a given section, we don't figure them in here.  We'll put them at the
  1810.      end of the file, at positions computed during bfd_close.
  1811.  
  1812.      The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
  1813.      or:                 <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
  1814.  
  1815.   struct elf_obj_tdata *t = elf_tdata (abfd);
  1816.   file_ptr off;
  1817.   int i;
  1818.   Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
  1819.   Elf_Internal_Shdr *i_shdrp;
  1820.   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
  1821.   int exec_p = (abfd->flags & EXEC_P) != 0;
  1822.   bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
  1823.  
  1824.   /* Everything starts after the ELF file header.  */
  1825.   off = i_ehdrp->e_ehsize;
  1826.  
  1827.   if (!exec_p)
  1828.     {
  1829.       /* Section headers.  */
  1830.       off = align_file_position (off);
  1831.       i_ehdrp->e_shoff = off;
  1832.       off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
  1833.       off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
  1834.     }
  1835.   for (i = 1; i < i_ehdrp->e_shnum; i++)
  1836.     {
  1837.       /* The symtab and strtab sections are placed by
  1838.      assign_file_positions_for_symtab_and_strtabs.  */
  1839.       if (i == t->symtab_section
  1840.       || i == t->strtab_section
  1841.       || i == t->shstrtab_section)
  1842.     continue;
  1843.  
  1844.       i_shdrp = i_shdrpp[i];
  1845.       if (i_shdrp->sh_type == SHT_REL || i_shdrp->sh_type == SHT_RELA)
  1846.     {
  1847.       i_shdrp->sh_offset = -1;
  1848.       continue;
  1849.     }
  1850.       if (exec_p)
  1851.     {
  1852.       if (maxpagesize == 0)
  1853.         maxpagesize = 1;    /* make the arithmetic work */
  1854.       /* This isn't necessarily going to give the best packing, if the
  1855.          segments require padding between them, but since that isn't
  1856.          usually the case, this'll do.  */
  1857.       if ((i_shdrp->sh_flags & SHF_ALLOC) == 0)
  1858.         {
  1859.           i_shdrp->sh_offset = -1;
  1860.           continue;
  1861.         }
  1862.       /* Blindly assume that the segments are ordered optimally.  With
  1863.          the default LD script, they will be.  */
  1864.       if (i_shdrp->sh_type != SHT_NOBITS)
  1865.         {
  1866.           /* need big unsigned type */
  1867.           bfd_vma addtl_off;
  1868.           addtl_off = i_shdrp->sh_addr - off;
  1869.           addtl_off = addtl_off % maxpagesize;
  1870.           if (addtl_off)
  1871.         {
  1872.           off += addtl_off;
  1873.         }
  1874.         }
  1875.     }
  1876.       off = assign_file_position_for_section (i_shdrp, off);
  1877.  
  1878.       if (exec_p
  1879.       && i_shdrp->sh_type == SHT_NOBITS
  1880.       && (i == i_ehdrp->e_shnum
  1881.           || i_shdrpp[i + 1]->sh_type != SHT_NOBITS))
  1882.     {
  1883.       /* Skip to the next page to ensure that when the file is
  1884.          loaded the bss section is loaded with zeroes.  I don't
  1885.          know if this is required on all platforms, but it
  1886.          shouldn't really hurt.  */
  1887.       off = BFD_ALIGN (off, maxpagesize);
  1888.     }
  1889.  
  1890.       if (exec_p
  1891.       && get_elf_backend_data(abfd)->maxpagesize > 1
  1892.       && i_shdrp->sh_type == SHT_PROGBITS
  1893.       && (i_shdrp->sh_flags & SHF_ALLOC)
  1894.       && (i_shdrp->sh_offset - i_shdrp->sh_addr) % get_elf_backend_data (abfd)->maxpagesize != 0)
  1895.     abort ();
  1896.     }
  1897.   if (exec_p)
  1898.     {
  1899.       elf_tdata (abfd)->next_file_pos = off;
  1900.       map_program_segments (abfd);
  1901.       off = elf_tdata (abfd)->next_file_pos;
  1902.  
  1903.       /* Section headers.  */
  1904.       off = align_file_position (off);
  1905.       i_ehdrp->e_shoff = off;
  1906.       off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
  1907.  
  1908.       off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
  1909.  
  1910.       for (i = 1; i < i_ehdrp->e_shnum; i++)
  1911.     {
  1912.       i_shdrp = i_shdrpp[i];
  1913.       if (i_shdrp->sh_offset + 1 == 0
  1914.           && i_shdrp->sh_type != SHT_REL
  1915.           && i_shdrp->sh_type != SHT_RELA)
  1916.         off = assign_file_position_for_section (i_shdrp, off);
  1917.     }
  1918.     }
  1919.   elf_tdata (abfd)->next_file_pos = off;
  1920. }
  1921.  
  1922. static boolean
  1923. prep_headers (abfd)
  1924.      bfd *abfd;
  1925. {
  1926.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  1927.   Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
  1928.   Elf_Internal_Shdr **i_shdrp;    /* Section header table, internal form */
  1929.   int count;
  1930.   struct strtab *shstrtab;
  1931.  
  1932.   i_ehdrp = elf_elfheader (abfd);
  1933.   i_shdrp = elf_elfsections (abfd);
  1934.  
  1935.   shstrtab = bfd_new_strtab (abfd);
  1936.   elf_shstrtab (abfd) = shstrtab;
  1937.  
  1938.   i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
  1939.   i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
  1940.   i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
  1941.   i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
  1942.  
  1943.   i_ehdrp->e_ident[EI_CLASS] = ELFCLASS;
  1944.   i_ehdrp->e_ident[EI_DATA] =
  1945.     abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
  1946.   i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
  1947.  
  1948.   for (count = EI_PAD; count < EI_NIDENT; count++)
  1949.     i_ehdrp->e_ident[count] = 0;
  1950.  
  1951.   i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL;
  1952.   switch (bfd_get_arch (abfd))
  1953.     {
  1954.     case bfd_arch_unknown:
  1955.       i_ehdrp->e_machine = EM_NONE;
  1956.       break;
  1957.     case bfd_arch_sparc:
  1958.       i_ehdrp->e_machine = EM_SPARC;
  1959.       break;
  1960.     case bfd_arch_i386:
  1961.       i_ehdrp->e_machine = EM_386;
  1962.       break;
  1963.     case bfd_arch_m68k:
  1964.       i_ehdrp->e_machine = EM_68K;
  1965.       break;
  1966.     case bfd_arch_m88k:
  1967.       i_ehdrp->e_machine = EM_88K;
  1968.       break;
  1969.     case bfd_arch_i860:
  1970.       i_ehdrp->e_machine = EM_860;
  1971.       break;
  1972.     case bfd_arch_mips:    /* MIPS Rxxxx */
  1973.       i_ehdrp->e_machine = EM_MIPS;    /* only MIPS R3000 */
  1974.       break;
  1975.     case bfd_arch_hppa:
  1976.       i_ehdrp->e_machine = EM_HPPA;
  1977.       break;
  1978.       /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
  1979.     default:
  1980.       i_ehdrp->e_machine = EM_NONE;
  1981.     }
  1982.   i_ehdrp->e_version = EV_CURRENT;
  1983.   i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr);
  1984.  
  1985.   /* no program header, for now. */
  1986.   i_ehdrp->e_phoff = 0;
  1987.   i_ehdrp->e_phentsize = 0;
  1988.   i_ehdrp->e_phnum = 0;
  1989.  
  1990.   /* each bfd section is section header entry */
  1991.   i_ehdrp->e_entry = bfd_get_start_address (abfd);
  1992.   i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
  1993.  
  1994.   /* if we're building an executable, we'll need a program header table */
  1995.   if (abfd->flags & EXEC_P)
  1996.     {
  1997.       /* it all happens later */
  1998. #if 0
  1999.       i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
  2000.  
  2001.       /* elf_build_phdrs() returns a (NULL-terminated) array of
  2002.      Elf_Internal_Phdrs */
  2003.       i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
  2004.       i_ehdrp->e_phoff = outbase;
  2005.       outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
  2006. #endif
  2007.     }
  2008.   else
  2009.     {
  2010.       i_ehdrp->e_phentsize = 0;
  2011.       i_phdrp = 0;
  2012.       i_ehdrp->e_phoff = 0;
  2013.     }
  2014.  
  2015.   elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
  2016.                                 ".symtab");
  2017.   elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
  2018.                                 ".strtab");
  2019.   elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
  2020.                                   ".shstrtab");
  2021.   return true;
  2022. }
  2023.  
  2024. static void
  2025. swap_out_syms (abfd)
  2026.      bfd *abfd;
  2027. {
  2028.   elf_map_symbols (abfd);
  2029.  
  2030.   /* Dump out the symtabs. */
  2031.   {
  2032.     int symcount = bfd_get_symcount (abfd);
  2033.     asymbol **syms = bfd_get_outsymbols (abfd);
  2034.     struct strtab *stt = bfd_new_strtab (abfd);
  2035.     Elf_Internal_Shdr *symtab_hdr;
  2036.     Elf_Internal_Shdr *symstrtab_hdr;
  2037.     Elf_External_Sym *outbound_syms;
  2038.     int idx;
  2039.  
  2040.     symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2041.     symtab_hdr->sh_type = SHT_SYMTAB;
  2042.     symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
  2043.     symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
  2044.     symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
  2045.  
  2046.     /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
  2047.        but it is possible that there are systems which use a different
  2048.        alignment.  */
  2049.     symtab_hdr->sh_addralign = 4;
  2050.  
  2051.     /* see assert in elf_fake_sections that supports this: */
  2052.     symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
  2053.     symstrtab_hdr->sh_type = SHT_STRTAB;
  2054.  
  2055.     outbound_syms = (Elf_External_Sym *)
  2056.       bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym));
  2057.     /* now generate the data (for "contents") */
  2058.     {
  2059.       /* Fill in zeroth symbol and swap it out.  */
  2060.       Elf_Internal_Sym sym;
  2061.       sym.st_name = 0;
  2062.       sym.st_value = 0;
  2063.       sym.st_size = 0;
  2064.       sym.st_info = 0;
  2065.       sym.st_other = 0;
  2066.       sym.st_shndx = SHN_UNDEF;
  2067.       elf_swap_symbol_out (abfd, &sym, outbound_syms);
  2068.     }
  2069.     for (idx = 0; idx < symcount; idx++)
  2070.       {
  2071.     Elf_Internal_Sym sym;
  2072.     bfd_vma value = syms[idx]->value;
  2073.  
  2074.     if (syms[idx]->flags & BSF_SECTION_SYM)
  2075.       /* Section symbols have no names.  */
  2076.       sym.st_name = 0;
  2077.     else
  2078.       sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
  2079.  
  2080.     if (bfd_is_com_section (syms[idx]->section))
  2081.       {
  2082.         /* ELF common symbols put the alignment into the `value' field,
  2083.            and the size into the `size' field.  This is backwards from
  2084.            how BFD handles it, so reverse it here.  */
  2085.         sym.st_size = value;
  2086.         /* Should retrieve this from somewhere... */
  2087.         sym.st_value = 16;
  2088.         sym.st_shndx = SHN_COMMON;
  2089.       }
  2090.     else
  2091.       {
  2092.         asection *sec = syms[idx]->section;
  2093.         elf_symbol_type *type_ptr;
  2094.         int shndx;
  2095.  
  2096.         if (sec->output_section)
  2097.           {
  2098.         value += sec->output_offset;
  2099.         sec = sec->output_section;
  2100.           }
  2101.         value += sec->vma;
  2102.         sym.st_value = value;
  2103.         type_ptr = elf_symbol_from (abfd, syms[idx]);
  2104.         sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
  2105.         sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec);
  2106.         if (shndx == -1)
  2107.           {
  2108.         asection *sec2;
  2109.         /* Writing this would be a hell of a lot easier if we had
  2110.            some decent documentation on bfd, and knew what to expect
  2111.            of the library, and what to demand of applications.  For
  2112.            example, it appears that `objcopy' might not set the
  2113.            section of a symbol to be a section that is actually in
  2114.            the output file.  */
  2115.         sec2 = bfd_get_section_by_name (abfd, sec->name);
  2116.         assert (sec2 != 0);
  2117.         sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2);
  2118.         assert (shndx != -1);
  2119.           }
  2120.       }
  2121.  
  2122.     if (bfd_is_com_section (syms[idx]->section))
  2123.       sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
  2124.     else if (syms[idx]->section == &bfd_und_section)
  2125.       sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE);
  2126.     else if (syms[idx]->flags & BSF_SECTION_SYM)
  2127.       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
  2128.     else if (syms[idx]->flags & BSF_FILE)
  2129.       sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
  2130.     else
  2131.       {
  2132.         int bind = STB_LOCAL;
  2133.         int type = STT_OBJECT;
  2134.         unsigned int flags = syms[idx]->flags;
  2135.  
  2136.         if (flags & BSF_LOCAL)
  2137.           bind = STB_LOCAL;
  2138.         else if (flags & BSF_WEAK)
  2139.           bind = STB_WEAK;
  2140.         else if (flags & BSF_GLOBAL)
  2141.           bind = STB_GLOBAL;
  2142.  
  2143.         if (flags & BSF_FUNCTION)
  2144.           type = STT_FUNC;
  2145.  
  2146.         sym.st_info = ELF_ST_INFO (bind, type);
  2147.       }
  2148.  
  2149.     sym.st_other = 0;
  2150.     elf_swap_symbol_out (abfd, &sym,
  2151.                  (outbound_syms
  2152.                   + elf_sym_extra (abfd)[idx].elf_sym_num));
  2153.       }
  2154.  
  2155.     symtab_hdr->contents = (PTR) outbound_syms;
  2156.     symstrtab_hdr->contents = (PTR) stt->tab;
  2157.     symstrtab_hdr->sh_size = stt->length;
  2158.     symstrtab_hdr->sh_type = SHT_STRTAB;
  2159.  
  2160.     symstrtab_hdr->sh_flags = 0;
  2161.     symstrtab_hdr->sh_addr = 0;
  2162.     symstrtab_hdr->sh_entsize = 0;
  2163.     symstrtab_hdr->sh_link = 0;
  2164.     symstrtab_hdr->sh_info = 0;
  2165.     symstrtab_hdr->sh_addralign = 1;
  2166.     symstrtab_hdr->size = 0;
  2167.   }
  2168.  
  2169.   /* put the strtab out too... */
  2170.   {
  2171.     Elf_Internal_Shdr *this_hdr;
  2172.  
  2173.     this_hdr = &elf_tdata(abfd)->shstrtab_hdr;
  2174.     this_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
  2175.     this_hdr->sh_size = elf_shstrtab (abfd)->length;
  2176.     this_hdr->sh_type = SHT_STRTAB;
  2177.     this_hdr->sh_flags = 0;
  2178.     this_hdr->sh_addr = 0;
  2179.     this_hdr->sh_entsize = 0;
  2180.     this_hdr->sh_addralign = 1;
  2181.     this_hdr->size = 0;
  2182.   }
  2183. }
  2184.  
  2185. static boolean
  2186. write_shdrs_and_ehdr (abfd)
  2187.      bfd *abfd;
  2188. {
  2189.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  2190.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  2191.   Elf_External_Shdr *x_shdrp;    /* Section header table, external form */
  2192.   Elf_Internal_Shdr **i_shdrp;    /* Section header table, internal form */
  2193.   int count;
  2194.   struct strtab *shstrtab;
  2195.  
  2196.   i_ehdrp = elf_elfheader (abfd);
  2197.   i_shdrp = elf_elfsections (abfd);
  2198.   shstrtab = elf_shstrtab (abfd);
  2199.  
  2200.   /* swap the header before spitting it out... */
  2201.  
  2202. #if DEBUG & 1
  2203.   elf_debug_file (i_ehdrp);
  2204. #endif
  2205.   elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
  2206.   bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
  2207.   bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd);
  2208.  
  2209.   /* at this point we've concocted all the ELF sections... */
  2210.   x_shdrp = (Elf_External_Shdr *)
  2211.     bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
  2212.   if (!x_shdrp)
  2213.     {
  2214.       bfd_error = no_memory;
  2215.       return false;
  2216.     }
  2217.  
  2218.   for (count = 0; count < i_ehdrp->e_shnum; count++)
  2219.     {
  2220. #if DEBUG & 2
  2221.       elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
  2222.              i_shdrp[count]);
  2223. #endif
  2224.       elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
  2225.     }
  2226.   bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET);
  2227.   bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd);
  2228.   /* need to dump the string table too... */
  2229.  
  2230.   return true;
  2231. }
  2232.  
  2233. static void
  2234. assign_file_positions_for_relocs (abfd)
  2235.      bfd *abfd;
  2236. {
  2237.   file_ptr off = elf_tdata(abfd)->next_file_pos;
  2238.   int i;
  2239.   Elf_Internal_Shdr **shdrpp = elf_elfsections (abfd);
  2240.   Elf_Internal_Shdr *shdrp;
  2241.   for (i = 1; i < elf_elfheader(abfd)->e_shnum; i++)
  2242.     {
  2243.       shdrp = shdrpp[i];
  2244.       if (shdrp->sh_type != SHT_REL && shdrp->sh_type != SHT_RELA)
  2245.     continue;
  2246.       off = align_file_position (off);
  2247.       off = assign_file_position_for_section (shdrp, off);
  2248.     }
  2249.   elf_tdata(abfd)->next_file_pos = off;
  2250. }
  2251.  
  2252. boolean
  2253. DEFUN (NAME(bfd_elf,write_object_contents), (abfd), bfd * abfd)
  2254. {
  2255.   struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2256.   Elf_Internal_Ehdr *i_ehdrp;
  2257.   Elf_Internal_Shdr **i_shdrp;
  2258.   int count;
  2259.  
  2260.   if (abfd->output_has_begun == false)
  2261.     {
  2262.       prep_headers (abfd);
  2263.       elf_compute_section_file_positions (abfd);
  2264.       abfd->output_has_begun = true;
  2265.     }
  2266.  
  2267.   i_shdrp = elf_elfsections (abfd);
  2268.   i_ehdrp = elf_elfheader (abfd);
  2269.  
  2270.   bfd_map_over_sections (abfd, write_relocs, (PTR) 0);
  2271.   assign_file_positions_for_relocs (abfd);
  2272.  
  2273.   /* After writing the headers, we need to write the sections too... */
  2274.   for (count = 1; count < i_ehdrp->e_shnum; count++)
  2275.     {
  2276.       if (bed->elf_backend_section_processing)
  2277.     (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
  2278.       if (i_shdrp[count]->contents)
  2279.     {
  2280.       bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET);
  2281.       bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, 1,
  2282.              abfd);
  2283.     }
  2284.     }
  2285.  
  2286.   if (bed->elf_backend_final_write_processing)
  2287.     (*bed->elf_backend_final_write_processing) (abfd);
  2288.  
  2289.   return write_shdrs_and_ehdr (abfd);
  2290. }
  2291.  
  2292. /* Given an index of a section, retrieve a pointer to it.  Note
  2293.    that for our purposes, sections are indexed by {1, 2, ...} with
  2294.    0 being an illegal index. */
  2295.  
  2296. /* In the original, each ELF section went into exactly one BFD
  2297.    section. This doesn't really make sense, so we need a real mapping.
  2298.    The mapping has to hide in the Elf_Internal_Shdr since asection
  2299.    doesn't have anything like a tdata field... */
  2300.  
  2301. static struct sec *
  2302. DEFUN (section_from_elf_index, (abfd, index),
  2303.        bfd * abfd AND
  2304.        int index)
  2305. {
  2306.   /* @@ Is bfd_com_section really correct in all the places it could
  2307.      be returned from this routine?  */
  2308.  
  2309.   if (index == SHN_ABS)
  2310.     return &bfd_com_section;    /* not abs? */
  2311.   if (index == SHN_COMMON)
  2312.     return &bfd_com_section;
  2313.  
  2314.   if (index > elf_elfheader (abfd)->e_shnum)
  2315.     return 0;
  2316.  
  2317.   {
  2318.     Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index];
  2319.  
  2320.     switch (hdr->sh_type)
  2321.       {
  2322.     /* ELF sections that map to BFD sections */
  2323.       case SHT_PROGBITS:
  2324.       case SHT_NOBITS:
  2325.     if (!hdr->rawdata)
  2326.       bfd_section_from_shdr (abfd, index);
  2327.     return (struct sec *) hdr->rawdata;
  2328.  
  2329.       default:
  2330.     return (struct sec *) &bfd_abs_section;
  2331.       }
  2332.   }
  2333. }
  2334.  
  2335. /* given a section, search the header to find them... */
  2336. static int
  2337. DEFUN (elf_section_from_bfd_section, (abfd, asect),
  2338.        bfd * abfd AND
  2339.        struct sec *asect)
  2340. {
  2341.   Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
  2342.   int index;
  2343.   Elf_Internal_Shdr *hdr;
  2344.   int maxindex = elf_elfheader (abfd)->e_shnum;
  2345.  
  2346.   if (asect == &bfd_abs_section)
  2347.     return SHN_ABS;
  2348.   if (asect == &bfd_com_section)
  2349.     return SHN_COMMON;
  2350.   if (asect == &bfd_und_section)
  2351.     return SHN_UNDEF;
  2352.  
  2353.   for (index = 0; index < maxindex; index++)
  2354.     {
  2355.       hdr = i_shdrp[index];
  2356.       switch (hdr->sh_type)
  2357.     {
  2358.       /* ELF sections that map to BFD sections */
  2359.     case SHT_PROGBITS:
  2360.     case SHT_NOBITS:
  2361.     case SHT_NOTE:
  2362.       if (hdr->rawdata)
  2363.         {
  2364.           if (((struct sec *) (hdr->rawdata)) == asect)
  2365.         return index;
  2366.         }
  2367.       break;
  2368.  
  2369.     case SHT_STRTAB:
  2370.       /* fix_up_strtabs will generate STRTAB sections with names
  2371.          of .stab*str.  */
  2372.       if (!strncmp (asect->name, ".stab", 5)
  2373.           && !strcmp ("str", asect->name + strlen (asect->name) - 3))
  2374.         {
  2375.           if (hdr->rawdata)
  2376.         {
  2377.           if (((struct sec *) (hdr->rawdata)) == asect)
  2378.             return index;
  2379.         }
  2380.           break;
  2381.         }
  2382.       /* FALL THROUGH */
  2383.     default:
  2384.       {
  2385.         struct elf_backend_data *bed = get_elf_backend_data (abfd);
  2386.  
  2387.         if (bed->elf_backend_section_from_bfd_section)
  2388.           {
  2389.         int retval;
  2390.  
  2391.         retval = index;
  2392.         if ((*bed->elf_backend_section_from_bfd_section)
  2393.             (abfd, hdr, asect, &retval))
  2394.           return retval;
  2395.           }
  2396.       }
  2397.       break;
  2398.     }
  2399.     }
  2400.   return -1;
  2401. }
  2402.  
  2403. /* given a symbol, return the bfd index for that symbol.  */
  2404. static int
  2405. DEFUN (elf_symbol_from_bfd_symbol, (abfd, asym_ptr_ptr),
  2406.        bfd * abfd AND
  2407.        struct symbol_cache_entry **asym_ptr_ptr)
  2408. {
  2409.   struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
  2410.   int idx;
  2411.   flagword flags = asym_ptr->flags;
  2412.  
  2413.   /* When gas creates relocations against local labels, it creates its
  2414.      own symbol for the section, but does put the symbol into the
  2415.      symbol chain, so udata is 0.  When the linker is generating
  2416.      relocatable output, this section symbol may be for one of the
  2417.      input sections rather than the output section.  */
  2418.   if (asym_ptr->udata == (PTR) 0
  2419.       && (flags & BSF_SECTION_SYM)
  2420.       && asym_ptr->section)
  2421.     {
  2422.       int indx;
  2423.  
  2424.       if (asym_ptr->section->output_section != NULL)
  2425.     indx = asym_ptr->section->output_section->index;
  2426.       else
  2427.     indx = asym_ptr->section->index;
  2428.       if (elf_section_syms (abfd)[indx])
  2429.     asym_ptr->udata = elf_section_syms (abfd)[indx]->udata;
  2430.     }
  2431.  
  2432.   if (asym_ptr->udata)
  2433.     idx = ((Elf_Sym_Extra *)asym_ptr->udata)->elf_sym_num;
  2434.   else
  2435.     {
  2436.       abort ();
  2437.     }
  2438.  
  2439. #if DEBUG & 4
  2440.   {
  2441.  
  2442.     fprintf (stderr,
  2443.          "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
  2444.          (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
  2445.     fflush (stderr);
  2446.   }
  2447. #endif
  2448.  
  2449.   return idx;
  2450. }
  2451.  
  2452. static boolean
  2453. DEFUN (elf_slurp_symbol_table, (abfd, symptrs),
  2454.        bfd * abfd AND
  2455.        asymbol ** symptrs)    /* Buffer for generated bfd symbols */
  2456. {
  2457.   Elf_Internal_Shdr *hdr = &elf_tdata(abfd)->symtab_hdr;
  2458.   int symcount;            /* Number of external ELF symbols */
  2459.   int i;
  2460.   elf_symbol_type *sym;        /* Pointer to current bfd symbol */
  2461.   elf_symbol_type *symbase;    /* Buffer for generated bfd symbols */
  2462.   Elf_Internal_Sym i_sym;
  2463.   Elf_External_Sym *x_symp;
  2464.  
  2465.   /* this is only valid because there is only one symtab... */
  2466.   /* FIXME:  This is incorrect, there may also be a dynamic symbol
  2467.      table which is a subset of the full symbol table.  We either need
  2468.      to be prepared to read both (and merge them) or ensure that we
  2469.      only read the full symbol table.  Currently we only get called to
  2470.      read the full symbol table.  -fnf */
  2471.  
  2472.   /* Read each raw ELF symbol, converting from external ELF form to
  2473.      internal ELF form, and then using the information to create a
  2474.      canonical bfd symbol table entry.
  2475.  
  2476.      Note that we allocate the initial bfd canonical symbol buffer
  2477.      based on a one-to-one mapping of the ELF symbols to canonical
  2478.      symbols.  We actually use all the ELF symbols, so there will be no
  2479.      space left over at the end.  When we have all the symbols, we
  2480.      build the caller's pointer vector. */
  2481.  
  2482.   if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
  2483.     {
  2484.       bfd_error = system_call_error;
  2485.       return false;
  2486.     }
  2487.  
  2488.   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  2489.   symbase = (elf_symbol_type *) bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type));
  2490.   sym = symbase;
  2491.  
  2492.   /* Temporarily allocate room for the raw ELF symbols.  */
  2493.   x_symp = (Elf_External_Sym *) bfd_xmalloc (symcount * sizeof (Elf_External_Sym));
  2494.  
  2495.   if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
  2496.       != symcount * sizeof (Elf_External_Sym))
  2497.     {
  2498.       free ((PTR) x_symp);
  2499.       bfd_error = system_call_error;
  2500.       return false;
  2501.     }
  2502.   /* Skip first symbol, which is a null dummy.  */
  2503.   for (i = 1; i < symcount; i++)
  2504.     {
  2505.       elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
  2506.       memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
  2507. #ifdef ELF_KEEP_EXTSYM
  2508.       memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
  2509. #endif
  2510.       sym->symbol.the_bfd = abfd;
  2511.  
  2512.       sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link,
  2513.                               i_sym.st_name);
  2514.  
  2515.       sym->symbol.value = i_sym.st_value;
  2516.  
  2517.       if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
  2518.     {
  2519.       sym->symbol.section = section_from_elf_index (abfd, i_sym.st_shndx);
  2520.     }
  2521.       else if (i_sym.st_shndx == SHN_ABS)
  2522.     {
  2523.       sym->symbol.section = &bfd_abs_section;
  2524.     }
  2525.       else if (i_sym.st_shndx == SHN_COMMON)
  2526.     {
  2527.       sym->symbol.section = &bfd_com_section;
  2528.       /* Elf puts the alignment into the `value' field, and the size
  2529.          into the `size' field.  BFD wants to see the size in the
  2530.          value field, and doesn't care (at the moment) about the
  2531.          alignment.  */
  2532.       sym->symbol.value = i_sym.st_size;
  2533.     }
  2534.       else if (i_sym.st_shndx == SHN_UNDEF)
  2535.     {
  2536.       sym->symbol.section = &bfd_und_section;
  2537.     }
  2538.       else
  2539.     sym->symbol.section = &bfd_abs_section;
  2540.  
  2541.       sym->symbol.value -= sym->symbol.section->vma;
  2542.  
  2543.       switch (ELF_ST_BIND (i_sym.st_info))
  2544.     {
  2545.     case STB_LOCAL:
  2546.       sym->symbol.flags |= BSF_LOCAL;
  2547.       break;
  2548.     case STB_GLOBAL:
  2549.       sym->symbol.flags |= BSF_GLOBAL;
  2550.       break;
  2551.     case STB_WEAK:
  2552.       sym->symbol.flags |= BSF_WEAK;
  2553.       break;
  2554.     }
  2555.  
  2556.       switch (ELF_ST_TYPE (i_sym.st_info))
  2557.     {
  2558.     case STT_SECTION:
  2559.       sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
  2560.       break;
  2561.     case STT_FILE:
  2562.       sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
  2563.       break;
  2564.     case STT_FUNC:
  2565.       sym->symbol.flags |= BSF_FUNCTION;
  2566.       break;
  2567.     }
  2568.  
  2569.       /* Do some backend-specific processing on this symbol.  */
  2570.       {
  2571.     struct elf_backend_data *ebd = get_elf_backend_data (abfd);
  2572.     if (ebd->elf_backend_symbol_processing)
  2573.       (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
  2574.       }
  2575.  
  2576.       sym++;
  2577.     }
  2578.  
  2579.   /* Do some backend-specific processing on this symbol table.  */
  2580.   {
  2581.     struct elf_backend_data *ebd = get_elf_backend_data (abfd);
  2582.     if (ebd->elf_backend_symbol_table_processing)
  2583.       (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
  2584.   }
  2585.  
  2586.   /* We rely on the zalloc to clear out the final symbol entry.  */
  2587.  
  2588.   bfd_get_symcount (abfd) = symcount = sym - symbase;
  2589.  
  2590.   /* Fill in the user's symbol pointer vector if needed.  */
  2591.   if (symptrs)
  2592.     {
  2593.       sym = symbase;
  2594.       while (symcount-- > 0)
  2595.     {
  2596.       *symptrs++ = &sym->symbol;
  2597.       sym++;
  2598.     }
  2599.       *symptrs = 0;        /* Final null pointer */
  2600.     }
  2601.  
  2602.   free ((PTR) x_symp);
  2603.   return true;
  2604. }
  2605.  
  2606. /* Return the number of bytes required to hold the symtab vector.
  2607.  
  2608.    Note that we base it on the count plus 1, since we will null terminate
  2609.    the vector allocated based on this size.  However, the ELF symbol table
  2610.    always has a dummy entry as symbol #0, so it ends up even.  */
  2611.  
  2612. unsigned int
  2613. DEFUN (elf_get_symtab_upper_bound, (abfd), bfd * abfd)
  2614. {
  2615.   unsigned int symcount;
  2616.   unsigned int symtab_size = 0;
  2617.  
  2618.   Elf_Internal_Shdr *hdr = &elf_tdata(abfd)->symtab_hdr;
  2619.   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
  2620.   symtab_size = (symcount - 1 + 1) * (sizeof (asymbol));
  2621.  
  2622.   return symtab_size;
  2623. }
  2624.  
  2625. /*
  2626.     This function return the number of bytes required to store the
  2627.     relocation information associated with section <<sect>>
  2628.     attached to bfd <<abfd>>
  2629.  
  2630. */
  2631. unsigned int
  2632. elf_get_reloc_upper_bound (abfd, asect)
  2633.      bfd *abfd;
  2634.      sec_ptr asect;
  2635. {
  2636.   if (asect->flags & SEC_RELOC)
  2637.     {
  2638.       /* either rel or rela */
  2639.       return elf_section_data(asect)->rel_hdr.sh_size;
  2640.     }
  2641.   else
  2642.     return 0;
  2643. }
  2644.  
  2645. static boolean
  2646. DEFUN (elf_slurp_reloca_table, (abfd, asect, symbols),
  2647.        bfd * abfd AND
  2648.        sec_ptr asect AND
  2649.        asymbol ** symbols)
  2650. {
  2651.   Elf_External_Rela *native_relocs;
  2652.   arelent *reloc_cache;
  2653.   arelent *cache_ptr;
  2654.  
  2655.   unsigned int idx;
  2656.  
  2657.   if (asect->relocation)
  2658.     return true;
  2659.   if (asect->reloc_count == 0)
  2660.     return true;
  2661.   if (asect->flags & SEC_CONSTRUCTOR)
  2662.     return true;
  2663.  
  2664.   bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
  2665.   native_relocs = (Elf_External_Rela *)
  2666.     bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela));
  2667.   bfd_read ((PTR) native_relocs,
  2668.         sizeof (Elf_External_Rela), asect->reloc_count, abfd);
  2669.  
  2670.   reloc_cache = (arelent *)
  2671.     bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
  2672.  
  2673.   if (!reloc_cache)
  2674.     {
  2675.       bfd_error = no_memory;
  2676.       return false;
  2677.     }
  2678.  
  2679.   for (idx = 0; idx < asect->reloc_count; idx++)
  2680.     {
  2681.       Elf_Internal_Rela dst;
  2682.       Elf_External_Rela *src;
  2683.  
  2684.       cache_ptr = reloc_cache + idx;
  2685.       src = native_relocs + idx;
  2686.       elf_swap_reloca_in (abfd, src, &dst);
  2687.  
  2688. #ifdef RELOC_PROCESSING
  2689.       RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
  2690. #else
  2691.       if (asect->flags & SEC_RELOC)
  2692.     {
  2693.       /* relocatable, so the offset is off of the section */
  2694.       cache_ptr->address = dst.r_offset + asect->vma;
  2695.     }
  2696.       else
  2697.     {
  2698.       /* non-relocatable, so the offset a virtual address */
  2699.       cache_ptr->address = dst.r_offset;
  2700.     }
  2701.  
  2702.       /* ELF_R_SYM(dst.r_info) is the symbol table offset.  An offset
  2703.      of zero points to the dummy symbol, which was not read into
  2704.      the symbol table SYMBOLS.  */
  2705.       if (ELF_R_SYM (dst.r_info) == 0)
  2706.     cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  2707.       else
  2708.     {
  2709.       asymbol *s;
  2710.  
  2711.       cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
  2712.  
  2713.       /* Translate any ELF section symbol into a BFD section
  2714.          symbol.  */
  2715.       s = *(cache_ptr->sym_ptr_ptr);
  2716.       if (s->flags & BSF_SECTION_SYM)
  2717.         {
  2718.           cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
  2719.           s = *cache_ptr->sym_ptr_ptr;
  2720.           if (s->name == 0 || s->name[0] == 0)
  2721.         abort ();
  2722.         }
  2723.     }
  2724.       cache_ptr->addend = dst.r_addend;
  2725.  
  2726.       /* Fill in the cache_ptr->howto field from dst.r_type */
  2727.       {
  2728.     struct elf_backend_data *ebd = get_elf_backend_data (abfd);
  2729.     (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst);
  2730.       }
  2731. #endif
  2732.     }
  2733.  
  2734.   asect->relocation = reloc_cache;
  2735.   return true;
  2736. }
  2737.  
  2738. #ifdef DEBUG
  2739. static void
  2740. elf_debug_section (str, num, hdr)
  2741.      char *str;
  2742.      int num;
  2743.      Elf_Internal_Shdr *hdr;
  2744. {
  2745.   fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
  2746.   fprintf (stderr,
  2747.        "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
  2748.        (long) hdr->sh_name,
  2749.        (long) hdr->sh_type,
  2750.        (long) hdr->sh_flags);
  2751.   fprintf (stderr,
  2752.        "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
  2753.        (long) hdr->sh_addr,
  2754.        (long) hdr->sh_offset,
  2755.        (long) hdr->sh_size);
  2756.   fprintf (stderr,
  2757.        "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
  2758.        (long) hdr->sh_link,
  2759.        (long) hdr->sh_info,
  2760.        (long) hdr->sh_addralign);
  2761.   fprintf (stderr, "sh_entsize   = %ld\n",
  2762.        (long) hdr->sh_entsize);
  2763.   fprintf (stderr, "rawdata      = 0x%.8lx\n", (long) hdr->rawdata);
  2764.   fprintf (stderr, "contents     = 0x%.8lx\n", (long) hdr->contents);
  2765.   fprintf (stderr, "size         = %ld\n", (long) hdr->size);
  2766.   fflush (stderr);
  2767. }
  2768.  
  2769. static void
  2770. elf_debug_file (ehdrp)
  2771.      Elf_Internal_Ehdr *ehdrp;
  2772. {
  2773.   fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
  2774.   fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
  2775.   fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
  2776.   fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
  2777.   fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
  2778.   fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
  2779.   fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
  2780. }
  2781. #endif
  2782.  
  2783. static boolean
  2784. DEFUN (elf_slurp_reloc_table, (abfd, asect, symbols),
  2785.        bfd * abfd AND
  2786.        sec_ptr asect AND
  2787.        asymbol ** symbols)
  2788. {
  2789.   Elf_External_Rel *native_relocs;
  2790.   arelent *reloc_cache;
  2791.   arelent *cache_ptr;
  2792.   Elf_Internal_Shdr *data_hdr;
  2793.   ElfNAME (Off) data_off;
  2794.   ElfNAME (Word) data_max;
  2795.   char buf[4];            /* FIXME -- might be elf64 */
  2796.  
  2797.   unsigned int idx;
  2798.  
  2799.   if (asect->relocation)
  2800.     return true;
  2801.   if (asect->reloc_count == 0)
  2802.     return true;
  2803.   if (asect->flags & SEC_CONSTRUCTOR)
  2804.     return true;
  2805.  
  2806.   bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
  2807.   native_relocs = (Elf_External_Rel *)
  2808.     bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel));
  2809.   bfd_read ((PTR) native_relocs,
  2810.         sizeof (Elf_External_Rel), asect->reloc_count, abfd);
  2811.  
  2812.   reloc_cache = (arelent *)
  2813.     bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
  2814.  
  2815.   if (!reloc_cache)
  2816.     {
  2817.       bfd_error = no_memory;
  2818.       return false;
  2819.     }
  2820.  
  2821.   /* Get the offset of the start of the segment we are relocating to read in
  2822.      the implicit addend.  */
  2823.   data_hdr = &elf_section_data(asect)->this_hdr;
  2824.   data_off = data_hdr->sh_offset;
  2825.   data_max = data_hdr->sh_size - sizeof (buf) + 1;
  2826.  
  2827. #if DEBUG & 2
  2828.   elf_debug_section ("data section", -1, data_hdr);
  2829. #endif
  2830.  
  2831.   for (idx = 0; idx < asect->reloc_count; idx++)
  2832.     {
  2833. #ifdef RELOC_PROCESSING
  2834.       Elf_Internal_Rel dst;
  2835.       Elf_External_Rel *src;
  2836.  
  2837.       cache_ptr = reloc_cache + idx;
  2838.       src = native_relocs + idx;
  2839.       elf_swap_reloc_in (abfd, src, &dst);
  2840.  
  2841.       RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
  2842. #else
  2843.       Elf_Internal_Rel dst;
  2844.       Elf_External_Rel *src;
  2845.  
  2846.       cache_ptr = reloc_cache + idx;
  2847.       src = native_relocs + idx;
  2848.  
  2849.       elf_swap_reloc_in (abfd, src, &dst);
  2850.  
  2851.       if (asect->flags & SEC_RELOC)
  2852.     {
  2853.       /* relocatable, so the offset is off of the section */
  2854.       cache_ptr->address = dst.r_offset + asect->vma;
  2855.     }
  2856.       else
  2857.     {
  2858.       /* non-relocatable, so the offset a virtual address */
  2859.       cache_ptr->address = dst.r_offset;
  2860.     }
  2861.  
  2862.       /* ELF_R_SYM(dst.r_info) is the symbol table offset.  An offset
  2863.      of zero points to the dummy symbol, which was not read into
  2864.      the symbol table SYMBOLS.  */
  2865.       if (ELF_R_SYM (dst.r_info) == 0)
  2866.     cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  2867.       else
  2868.     {
  2869.       asymbol *s;
  2870.  
  2871.       cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
  2872.  
  2873.       /* Translate any ELF section symbol into a BFD section
  2874.          symbol.  */
  2875.       s = *(cache_ptr->sym_ptr_ptr);
  2876.       if (s->flags & BSF_SECTION_SYM)
  2877.         {
  2878.           cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
  2879.           s = *cache_ptr->sym_ptr_ptr;
  2880.           if (s->name == 0 || s->name[0] == 0)
  2881.         abort ();
  2882.         }
  2883.     }
  2884.       BFD_ASSERT (dst.r_offset <= data_max);
  2885.       cache_ptr->addend = 0;
  2886.  
  2887.       /* Fill in the cache_ptr->howto field from dst.r_type */
  2888.       {
  2889.     struct elf_backend_data *ebd = get_elf_backend_data (abfd);
  2890.     (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst);
  2891.       }
  2892. #endif
  2893.     }
  2894.  
  2895.   asect->relocation = reloc_cache;
  2896.   return true;
  2897. }
  2898.  
  2899. unsigned int
  2900. elf_canonicalize_reloc (abfd, section, relptr, symbols)
  2901.      bfd *abfd;
  2902.      sec_ptr section;
  2903.      arelent **relptr;
  2904.      asymbol **symbols;
  2905. {
  2906.   arelent *tblptr = section->relocation;
  2907.   unsigned int count = 0;
  2908.   int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
  2909.  
  2910.   /* snarfed from coffcode.h */
  2911.   if (use_rela_p)
  2912.     elf_slurp_reloca_table (abfd, section, symbols);
  2913.   else
  2914.     elf_slurp_reloc_table (abfd, section, symbols);
  2915.  
  2916.   tblptr = section->relocation;
  2917.   if (!tblptr)
  2918.     return 0;
  2919.  
  2920.   for (; count++ < section->reloc_count;)
  2921.     *relptr++ = tblptr++;
  2922.  
  2923.   *relptr = 0;
  2924.   return section->reloc_count;
  2925. }
  2926.  
  2927. unsigned int
  2928. DEFUN (elf_get_symtab, (abfd, alocation),
  2929.        bfd * abfd AND
  2930.        asymbol ** alocation)
  2931. {
  2932.  
  2933.   if (!elf_slurp_symbol_table (abfd, alocation))
  2934.     return 0;
  2935.   else
  2936.     return bfd_get_symcount (abfd);
  2937. }
  2938.  
  2939. asymbol *
  2940. DEFUN (elf_make_empty_symbol, (abfd),
  2941.        bfd * abfd)
  2942. {
  2943.   elf_symbol_type *newsym;
  2944.  
  2945.   newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
  2946.   if (!newsym)
  2947.     {
  2948.       bfd_error = no_memory;
  2949.       return NULL;
  2950.     }
  2951.   else
  2952.     {
  2953.       newsym->symbol.the_bfd = abfd;
  2954.       return &newsym->symbol;
  2955.     }
  2956. }
  2957.  
  2958. void
  2959. DEFUN (elf_get_symbol_info, (ignore_abfd, symbol, ret),
  2960.        bfd * ignore_abfd AND
  2961.        asymbol * symbol AND
  2962.        symbol_info * ret)
  2963. {
  2964.   bfd_symbol_info (symbol, ret);
  2965. }
  2966.  
  2967. void
  2968. DEFUN (elf_print_symbol, (ignore_abfd, filep, symbol, how),
  2969.        bfd * ignore_abfd AND
  2970.        PTR filep AND
  2971.        asymbol * symbol AND
  2972.        bfd_print_symbol_type how)
  2973. {
  2974.   FILE *file = (FILE *) filep;
  2975.   switch (how)
  2976.     {
  2977.     case bfd_print_symbol_name:
  2978.       fprintf (file, "%s", symbol->name);
  2979.       break;
  2980.     case bfd_print_symbol_more:
  2981.       fprintf (file, "elf ");
  2982.       fprintf_vma (file, symbol->value);
  2983.       fprintf (file, " %lx", (long) symbol->flags);
  2984.       break;
  2985.     case bfd_print_symbol_all:
  2986.       {
  2987.     CONST char *section_name;
  2988.     section_name = symbol->section ? symbol->section->name : "(*none*)";
  2989.     bfd_print_symbol_vandf ((PTR) file, symbol);
  2990.     fprintf (file, " %s\t%s",
  2991.          section_name,
  2992.          symbol->name);
  2993.       }
  2994.       break;
  2995.     }
  2996.  
  2997. }
  2998.  
  2999. alent *
  3000. DEFUN (elf_get_lineno, (ignore_abfd, symbol),
  3001.        bfd * ignore_abfd AND
  3002.        asymbol * symbol)
  3003. {
  3004.   fprintf (stderr, "elf_get_lineno unimplemented\n");
  3005.   fflush (stderr);
  3006.   BFD_FAIL ();
  3007.   return NULL;
  3008. }
  3009.  
  3010. boolean
  3011. DEFUN (elf_set_arch_mach, (abfd, arch, machine),
  3012.        bfd * abfd AND
  3013.        enum bfd_architecture arch AND
  3014.        unsigned long machine)
  3015. {
  3016.   /* Allow any architecture to be supported by the elf backend */
  3017.   switch (arch)
  3018.     {
  3019.     case bfd_arch_unknown:    /* EM_NONE */
  3020.     case bfd_arch_sparc:    /* EM_SPARC */
  3021.     case bfd_arch_i386:    /* EM_386 */
  3022.     case bfd_arch_m68k:    /* EM_68K */
  3023.     case bfd_arch_m88k:    /* EM_88K */
  3024.     case bfd_arch_i860:    /* EM_860 */
  3025.     case bfd_arch_mips:    /* EM_MIPS (MIPS R3000) */
  3026.     case bfd_arch_hppa:    /* EM_HPPA (HP PA_RISC) */
  3027.       return bfd_default_set_arch_mach (abfd, arch, machine);
  3028.     default:
  3029.       return false;
  3030.     }
  3031. }
  3032.  
  3033. boolean
  3034. DEFUN (elf_find_nearest_line, (abfd,
  3035.                    section,
  3036.                    symbols,
  3037.                    offset,
  3038.                    filename_ptr,
  3039.                    functionname_ptr,
  3040.                    line_ptr),
  3041.        bfd * abfd AND
  3042.        asection * section AND
  3043.        asymbol ** symbols AND
  3044.        bfd_vma offset AND
  3045.        CONST char **filename_ptr AND
  3046.        CONST char **functionname_ptr AND
  3047.        unsigned int *line_ptr)
  3048. {
  3049.   return false;
  3050. }
  3051.  
  3052. int
  3053. DEFUN (elf_sizeof_headers, (abfd, reloc),
  3054.        bfd * abfd AND
  3055.        boolean reloc)
  3056. {
  3057.   fprintf (stderr, "elf_sizeof_headers unimplemented\n");
  3058.   fflush (stderr);
  3059.   BFD_FAIL ();
  3060.   return 0;
  3061. }
  3062.  
  3063. boolean
  3064. DEFUN (elf_set_section_contents, (abfd, section, location, offset, count),
  3065.        bfd * abfd AND
  3066.        sec_ptr section AND
  3067.        PTR location AND
  3068.        file_ptr offset AND
  3069.        bfd_size_type count)
  3070. {
  3071.   Elf_Internal_Shdr *hdr;
  3072.  
  3073.   if (abfd->output_has_begun == false)    /* set by bfd.c handler? */
  3074.     {
  3075.       /* do setup calculations (FIXME) */
  3076.       prep_headers (abfd);
  3077.       elf_compute_section_file_positions (abfd);
  3078.       abfd->output_has_begun = true;
  3079.     }
  3080.  
  3081.   hdr = &elf_section_data(section)->this_hdr;
  3082.  
  3083.   if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
  3084.     return false;
  3085.   if (bfd_write (location, 1, count, abfd) != count)
  3086.     return false;
  3087.  
  3088.   return true;
  3089. }
  3090.  
  3091. void
  3092. DEFUN (elf_no_info_to_howto, (abfd, cache_ptr, dst),
  3093.        bfd * abfd AND
  3094.        arelent * cache_ptr AND
  3095.        Elf_Internal_Rela * dst)
  3096. {
  3097.   fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n");
  3098.   fflush (stderr);
  3099.   BFD_FAIL ();
  3100. }
  3101.  
  3102. void
  3103. DEFUN (elf_no_info_to_howto_rel, (abfd, cache_ptr, dst),
  3104.        bfd * abfd AND
  3105.        arelent * cache_ptr AND
  3106.        Elf_Internal_Rel * dst)
  3107. {
  3108.   fprintf (stderr, "elf REL relocation support for target machine unimplemented\n");
  3109.   fflush (stderr);
  3110.   BFD_FAIL ();
  3111. }
  3112.  
  3113.  
  3114. /* Core file support */
  3115.  
  3116. #ifdef HAVE_PROCFS        /* Some core file support requires host /proc files */
  3117. #include <sys/procfs.h>
  3118. #else
  3119. #define bfd_prstatus(abfd, descdata, descsz, filepos)    /* Define away */
  3120. #define bfd_fpregset(abfd, descdata, descsz, filepos)    /* Define away */
  3121. #define bfd_prpsinfo(abfd, descdata, descsz, filepos)    /* Define away */
  3122. #endif
  3123.  
  3124. #ifdef HAVE_PROCFS
  3125.  
  3126. static void
  3127. DEFUN (bfd_prstatus, (abfd, descdata, descsz, filepos),
  3128.        bfd * abfd AND
  3129.        char *descdata AND
  3130.        int descsz AND
  3131.        long filepos)
  3132. {
  3133.   asection *newsect;
  3134.   prstatus_t *status = (prstatus_t *) 0;
  3135.  
  3136.   if (descsz == sizeof (prstatus_t))
  3137.     {
  3138.       newsect = bfd_make_section (abfd, ".reg");
  3139.       newsect->_raw_size = sizeof (status->pr_reg);
  3140.       newsect->filepos = filepos + (long) &status->pr_reg;
  3141.       newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  3142.       newsect->alignment_power = 2;
  3143.       if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
  3144.     {
  3145.       memcpy (core_prstatus (abfd), descdata, descsz);
  3146.     }
  3147.     }
  3148. }
  3149.  
  3150. /* Stash a copy of the prpsinfo structure away for future use. */
  3151.  
  3152. static void
  3153. DEFUN (bfd_prpsinfo, (abfd, descdata, descsz, filepos),
  3154.        bfd * abfd AND
  3155.        char *descdata AND
  3156.        int descsz AND
  3157.        long filepos)
  3158. {
  3159.   asection *newsect;
  3160.  
  3161.   if (descsz == sizeof (prpsinfo_t))
  3162.     {
  3163.       if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
  3164.     {
  3165.       memcpy (core_prpsinfo (abfd), descdata, descsz);
  3166.     }
  3167.     }
  3168. }
  3169.  
  3170. static void
  3171. DEFUN (bfd_fpregset, (abfd, descdata, descsz, filepos),
  3172.        bfd * abfd AND
  3173.        char *descdata AND
  3174.        int descsz AND
  3175.        long filepos)
  3176. {
  3177.   asection *newsect;
  3178.  
  3179.   newsect = bfd_make_section (abfd, ".reg2");
  3180.   newsect->_raw_size = descsz;
  3181.   newsect->filepos = filepos;
  3182.   newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  3183.   newsect->alignment_power = 2;
  3184. }
  3185.  
  3186. #endif /* HAVE_PROCFS */
  3187.  
  3188. /* Return a pointer to the args (including the command name) that were
  3189.    seen by the program that generated the core dump.  Note that for
  3190.    some reason, a spurious space is tacked onto the end of the args
  3191.    in some (at least one anyway) implementations, so strip it off if
  3192.    it exists. */
  3193.  
  3194. char *
  3195. DEFUN (elf_core_file_failing_command, (abfd),
  3196.        bfd * abfd)
  3197. {
  3198. #ifdef HAVE_PROCFS
  3199.   if (core_prpsinfo (abfd))
  3200.     {
  3201.       prpsinfo_t *p = core_prpsinfo (abfd);
  3202.       char *scan = p->pr_psargs;
  3203.       while (*scan++)
  3204.     {;
  3205.     }
  3206.       scan -= 2;
  3207.       if ((scan > p->pr_psargs) && (*scan == ' '))
  3208.     {
  3209.       *scan = '\000';
  3210.     }
  3211.       return p->pr_psargs;
  3212.     }
  3213. #endif
  3214.   return NULL;
  3215. }
  3216.  
  3217. /* Return the number of the signal that caused the core dump.  Presumably,
  3218.    since we have a core file, we got a signal of some kind, so don't bother
  3219.    checking the other process status fields, just return the signal number.
  3220.    */
  3221.  
  3222. int
  3223. DEFUN (elf_core_file_failing_signal, (abfd),
  3224.        bfd * abfd)
  3225. {
  3226. #ifdef HAVE_PROCFS
  3227.   if (core_prstatus (abfd))
  3228.     {
  3229.       return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
  3230.     }
  3231. #endif
  3232.   return -1;
  3233. }
  3234.  
  3235. /* Check to see if the core file could reasonably be expected to have
  3236.    come for the current executable file.  Note that by default we return
  3237.    true unless we find something that indicates that there might be a
  3238.    problem.
  3239.    */
  3240.  
  3241. boolean
  3242. DEFUN (elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
  3243.        bfd * core_bfd AND
  3244.        bfd * exec_bfd)
  3245. {
  3246. #ifdef HAVE_PROCFS
  3247.   char *corename;
  3248.   char *execname;
  3249. #endif
  3250.  
  3251.   /* First, xvecs must match since both are ELF files for the same target. */
  3252.  
  3253.   if (core_bfd->xvec != exec_bfd->xvec)
  3254.     {
  3255.       bfd_error = system_call_error;
  3256.       return false;
  3257.     }
  3258.  
  3259. #ifdef HAVE_PROCFS
  3260.  
  3261.   /* If no prpsinfo, just return true.  Otherwise, grab the last component
  3262.      of the exec'd pathname from the prpsinfo. */
  3263.  
  3264.   if (core_prpsinfo (core_bfd))
  3265.     {
  3266.       corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname);
  3267.     }
  3268.   else
  3269.     {
  3270.       return true;
  3271.     }
  3272.  
  3273.   /* Find the last component of the executable pathname. */
  3274.  
  3275.   if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
  3276.     {
  3277.       execname++;
  3278.     }
  3279.   else
  3280.     {
  3281.       execname = (char *) exec_bfd->filename;
  3282.     }
  3283.  
  3284.   /* See if they match */
  3285.  
  3286.   return strcmp (execname, corename) ? false : true;
  3287.  
  3288. #else
  3289.  
  3290.   return true;
  3291.  
  3292. #endif /* HAVE_PROCFS */
  3293. }
  3294.  
  3295. /* ELF core files contain a segment of type PT_NOTE, that holds much of
  3296.    the information that would normally be available from the /proc interface
  3297.    for the process, at the time the process dumped core.  Currently this
  3298.    includes copies of the prstatus, prpsinfo, and fpregset structures.
  3299.  
  3300.    Since these structures are potentially machine dependent in size and
  3301.    ordering, bfd provides two levels of support for them.  The first level,
  3302.    available on all machines since it does not require that the host
  3303.    have /proc support or the relevant include files, is to create a bfd
  3304.    section for each of the prstatus, prpsinfo, and fpregset structures,
  3305.    without any interpretation of their contents.  With just this support,
  3306.    the bfd client will have to interpret the structures itself.  Even with
  3307.    /proc support, it might want these full structures for it's own reasons.
  3308.  
  3309.    In the second level of support, where HAVE_PROCFS is defined, bfd will
  3310.    pick apart the structures to gather some additional information that
  3311.    clients may want, such as the general register set, the name of the
  3312.    exec'ed file and its arguments, the signal (if any) that caused the
  3313.    core dump, etc.
  3314.  
  3315.    */
  3316.  
  3317. static boolean
  3318. DEFUN (elf_corefile_note, (abfd, hdr),
  3319.        bfd * abfd AND
  3320.        Elf_Internal_Phdr * hdr)
  3321. {
  3322.   Elf_External_Note *x_note_p;    /* Elf note, external form */
  3323.   Elf_Internal_Note i_note;    /* Elf note, internal form */
  3324.   char *buf = NULL;        /* Entire note segment contents */
  3325.   char *namedata;        /* Name portion of the note */
  3326.   char *descdata;        /* Descriptor portion of the note */
  3327.   char *sectname;        /* Name to use for new section */
  3328.   long filepos;            /* File offset to descriptor data */
  3329.   asection *newsect;
  3330.  
  3331.   if (hdr->p_filesz > 0
  3332.       && (buf = (char *) bfd_xmalloc (hdr->p_filesz)) != NULL
  3333.       && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
  3334.       && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
  3335.     {
  3336.       x_note_p = (Elf_External_Note *) buf;
  3337.       while ((char *) x_note_p < (buf + hdr->p_filesz))
  3338.     {
  3339.       i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
  3340.       i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
  3341.       i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
  3342.       namedata = x_note_p->name;
  3343.       descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
  3344.       filepos = hdr->p_offset + (descdata - buf);
  3345.       switch (i_note.type)
  3346.         {
  3347.         case NT_PRSTATUS:
  3348.           /* process descdata as prstatus info */
  3349.           bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
  3350.           sectname = ".prstatus";
  3351.           break;
  3352.         case NT_FPREGSET:
  3353.           /* process descdata as fpregset info */
  3354.           bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
  3355.           sectname = ".fpregset";
  3356.           break;
  3357.         case NT_PRPSINFO:
  3358.           /* process descdata as prpsinfo */
  3359.           bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
  3360.           sectname = ".prpsinfo";
  3361.           break;
  3362.         default:
  3363.           /* Unknown descriptor, just ignore it. */
  3364.           sectname = NULL;
  3365.           break;
  3366.         }
  3367.       if (sectname != NULL)
  3368.         {
  3369.           newsect = bfd_make_section (abfd, sectname);
  3370.           newsect->_raw_size = i_note.descsz;
  3371.           newsect->filepos = filepos;
  3372.           newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
  3373.           newsect->alignment_power = 2;
  3374.         }
  3375.       x_note_p = (Elf_External_Note *)
  3376.         (descdata + BFD_ALIGN (i_note.descsz, 4));
  3377.     }
  3378.     }
  3379.   if (buf != NULL)
  3380.     {
  3381.       free (buf);
  3382.     }
  3383.   return true;
  3384.  
  3385. }
  3386.  
  3387. /*  Core files are simply standard ELF formatted files that partition
  3388.     the file using the execution view of the file (program header table)
  3389.     rather than the linking view.  In fact, there is no section header
  3390.     table in a core file.
  3391.  
  3392.     The process status information (including the contents of the general
  3393.     register set) and the floating point register set are stored in a
  3394.     segment of type PT_NOTE.  We handcraft a couple of extra bfd sections
  3395.     that allow standard bfd access to the general registers (.reg) and the
  3396.     floating point registers (.reg2).
  3397.  
  3398.  */
  3399.  
  3400. bfd_target *
  3401. DEFUN (elf_core_file_p, (abfd), bfd * abfd)
  3402. {
  3403.   Elf_External_Ehdr x_ehdr;    /* Elf file header, external form */
  3404.   Elf_Internal_Ehdr *i_ehdrp;    /* Elf file header, internal form */
  3405.   Elf_External_Phdr x_phdr;    /* Program header table entry, external form */
  3406.   Elf_Internal_Phdr *i_phdrp;    /* Program header table, internal form */
  3407.   unsigned int phindex;
  3408.  
  3409.   /* Read in the ELF header in external format.  */
  3410.  
  3411.   if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
  3412.     {
  3413.       bfd_error = system_call_error;
  3414.       return NULL;
  3415.     }
  3416.  
  3417.   /* Now check to see if we have a valid ELF file, and one that BFD can
  3418.      make use of.  The magic number must match, the address size ('class')
  3419.      and byte-swapping must match our XVEC entry, and it must have a
  3420.      program header table (FIXME: See comments re segments at top of this
  3421.      file). */
  3422.  
  3423.   if (elf_file_p (&x_ehdr) == false)
  3424.     {
  3425.     wrong:
  3426.       bfd_error = wrong_format;
  3427.       return NULL;
  3428.     }
  3429.  
  3430.   /* FIXME, Check EI_VERSION here !  */
  3431.  
  3432.   {
  3433. #if ARCH_SIZE == 32
  3434.     int desired_address_size = ELFCLASS32;
  3435. #endif
  3436. #if ARCH_SIZE == 64
  3437.     int desired_address_size = ELFCLASS64;
  3438. #endif
  3439.  
  3440.     if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
  3441.       goto wrong;
  3442.   }
  3443.  
  3444.   /* Switch xvec to match the specified byte order.  */
  3445.   switch (x_ehdr.e_ident[EI_DATA])
  3446.     {
  3447.     case ELFDATA2MSB:        /* Big-endian */
  3448.       if (abfd->xvec->byteorder_big_p == false)
  3449.     goto wrong;
  3450.       break;
  3451.     case ELFDATA2LSB:        /* Little-endian */
  3452.       if (abfd->xvec->byteorder_big_p == true)
  3453.     goto wrong;
  3454.       break;
  3455.     case ELFDATANONE:        /* No data encoding specified */
  3456.     default:            /* Unknown data encoding specified */
  3457.       goto wrong;
  3458.     }
  3459.  
  3460.   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
  3461.      the tdata pointer in the bfd. */
  3462.  
  3463.   elf_tdata (abfd) =
  3464.     (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
  3465.   if (elf_tdata (abfd) == NULL)
  3466.     {
  3467.       bfd_error = no_memory;
  3468.       return NULL;
  3469.     }
  3470.  
  3471.   /* FIXME, `wrong' returns from this point onward, leak memory.  */
  3472.  
  3473.   /* Now that we know the byte order, swap in the rest of the header */
  3474.   i_ehdrp = elf_elfheader (abfd);
  3475.   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
  3476. #if DEBUG & 1
  3477.   elf_debug_file (i_ehdrp);
  3478. #endif
  3479.  
  3480.   /* If there is no program header, or the type is not a core file, then
  3481.      we are hosed. */
  3482.   if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
  3483.     goto wrong;
  3484.  
  3485.   /* Allocate space for a copy of the program header table in
  3486.      internal form, seek to the program header table in the file,
  3487.      read it in, and convert it to internal form.  As a simple sanity
  3488.      check, verify that the what BFD thinks is the size of each program
  3489.      header table entry actually matches the size recorded in the file. */
  3490.  
  3491.   if (i_ehdrp->e_phentsize != sizeof (x_phdr))
  3492.     goto wrong;
  3493.   i_phdrp = (Elf_Internal_Phdr *)
  3494.     bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
  3495.   if (!i_phdrp)
  3496.     {
  3497.       bfd_error = no_memory;
  3498.       return NULL;
  3499.     }
  3500.   if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
  3501.     {
  3502.       bfd_error = system_call_error;
  3503.       return NULL;
  3504.     }
  3505.   for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
  3506.     {
  3507.       if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
  3508.       != sizeof (x_phdr))
  3509.     {
  3510.       bfd_error = system_call_error;
  3511.       return NULL;
  3512.     }
  3513.       elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
  3514.     }
  3515.  
  3516.   /* Once all of the program headers have been read and converted, we
  3517.      can start processing them. */
  3518.  
  3519.   for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
  3520.     {
  3521.       bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
  3522.       if ((i_phdrp + phindex)->p_type == PT_NOTE)
  3523.     {
  3524.       elf_corefile_note (abfd, i_phdrp + phindex);
  3525.     }
  3526.     }
  3527.  
  3528.   /* Remember the entry point specified in the ELF file header. */
  3529.  
  3530.   bfd_get_start_address (abfd) = i_ehdrp->e_entry;
  3531.  
  3532.   return abfd->xvec;
  3533. }
  3534.