Home | History | Annotate | Line # | Download | only in bfd
      1   1.1  christos /* ELF executable support for BFD.
      2  1.11  christos    Copyright (C) 1991-2024 Free Software Foundation, Inc.
      3   1.1  christos 
      4   1.1  christos    Written by Fred Fish @ Cygnus Support, from information published
      5   1.1  christos    in "UNIX System V Release 4, Programmers Guide: ANSI C and
      6   1.1  christos    Programming Support Tools".  Sufficient support for gdb.
      7   1.1  christos 
      8   1.1  christos    Rewritten by Mark Eichin @ Cygnus Support, from information
      9   1.1  christos    published in "System V Application Binary Interface", chapters 4
     10   1.1  christos    and 5, as well as the various "Processor Supplement" documents
     11   1.1  christos    derived from it. Added support for assembler and other object file
     12   1.1  christos    utilities.  Further work done by Ken Raeburn (Cygnus Support), Michael
     13   1.1  christos    Meissner (Open Software Foundation), and Peter Hoogenboom (University
     14   1.1  christos    of Utah) to finish and extend this.
     15   1.1  christos 
     16   1.1  christos    This file is part of BFD, the Binary File Descriptor library.
     17   1.1  christos 
     18   1.1  christos    This program is free software; you can redistribute it and/or modify
     19   1.1  christos    it under the terms of the GNU General Public License as published by
     20   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     21   1.1  christos    (at your option) any later version.
     22   1.1  christos 
     23   1.1  christos    This program is distributed in the hope that it will be useful,
     24   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     25   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     26   1.1  christos    GNU General Public License for more details.
     27   1.1  christos 
     28   1.1  christos    You should have received a copy of the GNU General Public License
     29   1.1  christos    along with this program; if not, write to the Free Software
     30   1.1  christos    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     31   1.1  christos    MA 02110-1301, USA.  */
     32   1.1  christos 
     33   1.1  christos 
     34   1.1  christos /* Problems and other issues to resolve.
     35   1.1  christos 
     36   1.1  christos    (1)	BFD expects there to be some fixed number of "sections" in
     37   1.1  christos 	the object file.  I.E. there is a "section_count" variable in the
     38   1.1  christos 	bfd structure which contains the number of sections.  However, ELF
     39   1.1  christos 	supports multiple "views" of a file.  In particular, with current
     40   1.1  christos 	implementations, executable files typically have two tables, a
     41   1.1  christos 	program header table and a section header table, both of which
     42   1.1  christos 	partition the executable.
     43   1.1  christos 
     44   1.1  christos 	In ELF-speak, the "linking view" of the file uses the section header
     45   1.1  christos 	table to access "sections" within the file, and the "execution view"
     46   1.1  christos 	uses the program header table to access "segments" within the file.
     47   1.1  christos 	"Segments" typically may contain all the data from one or more
     48   1.1  christos 	"sections".
     49   1.1  christos 
     50   1.1  christos 	Note that the section header table is optional in ELF executables,
     51   1.1  christos 	but it is this information that is most useful to gdb.  If the
     52   1.1  christos 	section header table is missing, then gdb should probably try
     53   1.1  christos 	to make do with the program header table.  (FIXME)
     54   1.1  christos 
     55   1.1  christos    (2)  The code in this file is compiled twice, once in 32-bit mode and
     56   1.1  christos 	once in 64-bit mode.  More of it should be made size-independent
     57   1.1  christos 	and moved into elf.c.
     58   1.1  christos 
     59   1.1  christos    (3)	ELF section symbols are handled rather sloppily now.  This should
     60   1.1  christos 	be cleaned up, and ELF section symbols reconciled with BFD section
     61   1.1  christos 	symbols.
     62   1.1  christos 
     63   1.1  christos    (4)  We need a published spec for 64-bit ELF.  We've got some stuff here
     64   1.1  christos 	that we're using for SPARC V9 64-bit chips, but don't assume that
     65   1.1  christos 	it's cast in stone.
     66   1.1  christos  */
     67   1.1  christos 
     68   1.1  christos #include "sysdep.h"
     69   1.1  christos #include "bfd.h"
     70   1.1  christos #include "libiberty.h"
     71   1.1  christos #include "bfdlink.h"
     72   1.1  christos #include "libbfd.h"
     73   1.1  christos #include "elf-bfd.h"
     74   1.1  christos #include "libiberty.h"
     75   1.1  christos 
     76   1.1  christos /* Renaming structures, typedefs, macros and functions to be size-specific.  */
     77   1.1  christos #define Elf_External_Ehdr	NAME(Elf,External_Ehdr)
     78   1.1  christos #define Elf_External_Sym	NAME(Elf,External_Sym)
     79   1.1  christos #define Elf_External_Shdr	NAME(Elf,External_Shdr)
     80   1.1  christos #define Elf_External_Phdr	NAME(Elf,External_Phdr)
     81   1.1  christos #define Elf_External_Rel	NAME(Elf,External_Rel)
     82   1.1  christos #define Elf_External_Rela	NAME(Elf,External_Rela)
     83   1.1  christos #define Elf_External_Dyn	NAME(Elf,External_Dyn)
     84   1.1  christos 
     85   1.1  christos #define elf_core_file_failing_command	NAME(bfd_elf,core_file_failing_command)
     86   1.1  christos #define elf_core_file_failing_signal	NAME(bfd_elf,core_file_failing_signal)
     87   1.1  christos #define elf_core_file_matches_executable_p \
     88   1.1  christos   NAME(bfd_elf,core_file_matches_executable_p)
     89   1.1  christos #define elf_core_file_pid		NAME(bfd_elf,core_file_pid)
     90   1.1  christos #define elf_object_p			NAME(bfd_elf,object_p)
     91   1.1  christos #define elf_core_file_p			NAME(bfd_elf,core_file_p)
     92   1.1  christos #define elf_get_symtab_upper_bound	NAME(bfd_elf,get_symtab_upper_bound)
     93   1.1  christos #define elf_get_dynamic_symtab_upper_bound \
     94   1.1  christos   NAME(bfd_elf,get_dynamic_symtab_upper_bound)
     95   1.1  christos #define elf_swap_reloc_in		NAME(bfd_elf,swap_reloc_in)
     96   1.1  christos #define elf_swap_reloca_in		NAME(bfd_elf,swap_reloca_in)
     97   1.1  christos #define elf_swap_reloc_out		NAME(bfd_elf,swap_reloc_out)
     98   1.1  christos #define elf_swap_reloca_out		NAME(bfd_elf,swap_reloca_out)
     99   1.1  christos #define elf_swap_symbol_in		NAME(bfd_elf,swap_symbol_in)
    100   1.1  christos #define elf_swap_symbol_out		NAME(bfd_elf,swap_symbol_out)
    101   1.1  christos #define elf_swap_phdr_in		NAME(bfd_elf,swap_phdr_in)
    102   1.1  christos #define elf_swap_phdr_out		NAME(bfd_elf,swap_phdr_out)
    103   1.1  christos #define elf_swap_dyn_in			NAME(bfd_elf,swap_dyn_in)
    104   1.1  christos #define elf_swap_dyn_out		NAME(bfd_elf,swap_dyn_out)
    105   1.1  christos #define elf_get_reloc_upper_bound	NAME(bfd_elf,get_reloc_upper_bound)
    106   1.1  christos #define elf_canonicalize_reloc		NAME(bfd_elf,canonicalize_reloc)
    107   1.1  christos #define elf_slurp_symbol_table		NAME(bfd_elf,slurp_symbol_table)
    108   1.1  christos #define elf_canonicalize_symtab		NAME(bfd_elf,canonicalize_symtab)
    109   1.1  christos #define elf_canonicalize_dynamic_symtab \
    110   1.1  christos   NAME(bfd_elf,canonicalize_dynamic_symtab)
    111   1.1  christos #define elf_get_synthetic_symtab \
    112   1.1  christos   NAME(bfd_elf,get_synthetic_symtab)
    113   1.1  christos #define elf_make_empty_symbol		NAME(bfd_elf,make_empty_symbol)
    114   1.1  christos #define elf_get_symbol_info		NAME(bfd_elf,get_symbol_info)
    115   1.1  christos #define elf_get_lineno			NAME(bfd_elf,get_lineno)
    116   1.1  christos #define elf_set_arch_mach		NAME(bfd_elf,set_arch_mach)
    117   1.1  christos #define elf_find_nearest_line		NAME(bfd_elf,find_nearest_line)
    118   1.1  christos #define elf_sizeof_headers		NAME(bfd_elf,sizeof_headers)
    119   1.1  christos #define elf_set_section_contents	NAME(bfd_elf,set_section_contents)
    120   1.1  christos #define elf_no_info_to_howto		NAME(bfd_elf,no_info_to_howto)
    121   1.1  christos #define elf_no_info_to_howto_rel	NAME(bfd_elf,no_info_to_howto_rel)
    122   1.1  christos #define elf_find_section		NAME(bfd_elf,find_section)
    123   1.1  christos #define elf_write_shdrs_and_ehdr	NAME(bfd_elf,write_shdrs_and_ehdr)
    124   1.1  christos #define elf_write_out_phdrs		NAME(bfd_elf,write_out_phdrs)
    125   1.1  christos #define elf_checksum_contents		NAME(bfd_elf,checksum_contents)
    126   1.1  christos #define elf_write_relocs		NAME(bfd_elf,write_relocs)
    127   1.1  christos #define elf_slurp_reloc_table		NAME(bfd_elf,slurp_reloc_table)
    128   1.1  christos 
    129   1.1  christos #if ARCH_SIZE == 64
    130   1.1  christos #define ELF_R_INFO(X,Y)	ELF64_R_INFO(X,Y)
    131   1.1  christos #define ELF_R_SYM(X)	ELF64_R_SYM(X)
    132   1.1  christos #define ELF_R_TYPE(X)	ELF64_R_TYPE(X)
    133   1.1  christos #define ELFCLASS	ELFCLASS64
    134   1.1  christos #define FILE_ALIGN	8
    135   1.1  christos #define LOG_FILE_ALIGN	3
    136   1.1  christos #endif
    137   1.1  christos #if ARCH_SIZE == 32
    138   1.1  christos #define ELF_R_INFO(X,Y)	ELF32_R_INFO(X,Y)
    139   1.1  christos #define ELF_R_SYM(X)	ELF32_R_SYM(X)
    140   1.1  christos #define ELF_R_TYPE(X)	ELF32_R_TYPE(X)
    141   1.1  christos #define ELFCLASS	ELFCLASS32
    142   1.1  christos #define FILE_ALIGN	4
    143   1.1  christos #define LOG_FILE_ALIGN	2
    144   1.1  christos #endif
    145   1.1  christos 
    146   1.1  christos #if DEBUG & 2
    147   1.1  christos static void elf_debug_section (int, Elf_Internal_Shdr *);
    148   1.1  christos #endif
    149   1.1  christos #if DEBUG & 1
    150   1.1  christos static void elf_debug_file (Elf_Internal_Ehdr *);
    151   1.1  christos #endif
    152   1.1  christos 
    153   1.1  christos /* Structure swapping routines */
    155   1.1  christos 
    156   1.1  christos /* Should perhaps use put_offset, put_word, etc.  For now, the two versions
    157   1.1  christos    can be handled by explicitly specifying 32 bits or "the long type".  */
    158   1.1  christos #if ARCH_SIZE == 64
    159   1.1  christos #define H_PUT_WORD		H_PUT_64
    160   1.1  christos #define H_PUT_SIGNED_WORD	H_PUT_S64
    161   1.1  christos #define H_GET_WORD		H_GET_64
    162   1.1  christos #define H_GET_SIGNED_WORD	H_GET_S64
    163   1.1  christos #endif
    164   1.1  christos #if ARCH_SIZE == 32
    165   1.1  christos #define H_PUT_WORD		H_PUT_32
    166   1.1  christos #define H_PUT_SIGNED_WORD	H_PUT_S32
    167   1.1  christos #define H_GET_WORD		H_GET_32
    168   1.1  christos #define H_GET_SIGNED_WORD	H_GET_S32
    169   1.1  christos #endif
    170   1.1  christos 
    171   1.1  christos /* Translate an ELF symbol in external format into an ELF symbol in internal
    172   1.1  christos    format.  */
    173  1.10  christos 
    174   1.1  christos bool
    175   1.1  christos elf_swap_symbol_in (bfd *abfd,
    176   1.1  christos 		    const void *psrc,
    177   1.1  christos 		    const void *pshn,
    178   1.1  christos 		    Elf_Internal_Sym *dst)
    179   1.1  christos {
    180   1.1  christos   const Elf_External_Sym *src = (const Elf_External_Sym *) psrc;
    181   1.1  christos   const Elf_External_Sym_Shndx *shndx = (const Elf_External_Sym_Shndx *) pshn;
    182   1.1  christos   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    183   1.1  christos 
    184   1.1  christos   dst->st_name = H_GET_32 (abfd, src->st_name);
    185   1.1  christos   if (signed_vma)
    186   1.1  christos     dst->st_value = H_GET_SIGNED_WORD (abfd, src->st_value);
    187   1.1  christos   else
    188   1.1  christos     dst->st_value = H_GET_WORD (abfd, src->st_value);
    189   1.1  christos   dst->st_size = H_GET_WORD (abfd, src->st_size);
    190   1.1  christos   dst->st_info = H_GET_8 (abfd, src->st_info);
    191   1.1  christos   dst->st_other = H_GET_8 (abfd, src->st_other);
    192   1.1  christos   dst->st_shndx = H_GET_16 (abfd, src->st_shndx);
    193   1.1  christos   if (dst->st_shndx == (SHN_XINDEX & 0xffff))
    194   1.1  christos     {
    195  1.10  christos       if (shndx == NULL)
    196   1.1  christos 	return false;
    197   1.1  christos       dst->st_shndx = H_GET_32 (abfd, shndx->est_shndx);
    198   1.1  christos     }
    199   1.1  christos   else if (dst->st_shndx >= (SHN_LORESERVE & 0xffff))
    200   1.1  christos     dst->st_shndx += SHN_LORESERVE - (SHN_LORESERVE & 0xffff);
    201  1.10  christos   dst->st_target_internal = 0;
    202   1.1  christos   return true;
    203   1.1  christos }
    204   1.1  christos 
    205   1.1  christos /* Translate an ELF symbol in internal format into an ELF symbol in external
    206   1.1  christos    format.  */
    207   1.1  christos 
    208   1.1  christos void
    209   1.1  christos elf_swap_symbol_out (bfd *abfd,
    210   1.1  christos 		     const Elf_Internal_Sym *src,
    211   1.1  christos 		     void *cdst,
    212   1.1  christos 		     void *shndx)
    213   1.1  christos {
    214   1.1  christos   unsigned int tmp;
    215   1.1  christos   Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
    216   1.1  christos   H_PUT_32 (abfd, src->st_name, dst->st_name);
    217   1.1  christos   H_PUT_WORD (abfd, src->st_value, dst->st_value);
    218   1.1  christos   H_PUT_WORD (abfd, src->st_size, dst->st_size);
    219   1.1  christos   H_PUT_8 (abfd, src->st_info, dst->st_info);
    220   1.1  christos   H_PUT_8 (abfd, src->st_other, dst->st_other);
    221   1.1  christos   tmp = src->st_shndx;
    222   1.1  christos   if (tmp >= (SHN_LORESERVE & 0xffff) && tmp < SHN_LORESERVE)
    223   1.1  christos     {
    224   1.1  christos       if (shndx == NULL)
    225   1.1  christos 	abort ();
    226   1.1  christos       H_PUT_32 (abfd, tmp, shndx);
    227   1.1  christos       tmp = SHN_XINDEX & 0xffff;
    228   1.1  christos     }
    229   1.1  christos   H_PUT_16 (abfd, tmp, dst->st_shndx);
    230   1.1  christos }
    231   1.1  christos 
    232   1.1  christos /* Translate an ELF file header in external format into an ELF file header in
    233   1.1  christos    internal format.  */
    234   1.1  christos 
    235   1.1  christos static void
    236   1.1  christos elf_swap_ehdr_in (bfd *abfd,
    237   1.1  christos 		  const Elf_External_Ehdr *src,
    238   1.1  christos 		  Elf_Internal_Ehdr *dst)
    239   1.1  christos {
    240   1.1  christos   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    241   1.1  christos   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
    242   1.1  christos   dst->e_type = H_GET_16 (abfd, src->e_type);
    243   1.1  christos   dst->e_machine = H_GET_16 (abfd, src->e_machine);
    244   1.1  christos   dst->e_version = H_GET_32 (abfd, src->e_version);
    245   1.1  christos   if (signed_vma)
    246   1.1  christos     dst->e_entry = H_GET_SIGNED_WORD (abfd, src->e_entry);
    247   1.1  christos   else
    248   1.1  christos     dst->e_entry = H_GET_WORD (abfd, src->e_entry);
    249   1.1  christos   dst->e_phoff = H_GET_WORD (abfd, src->e_phoff);
    250   1.1  christos   dst->e_shoff = H_GET_WORD (abfd, src->e_shoff);
    251   1.1  christos   dst->e_flags = H_GET_32 (abfd, src->e_flags);
    252   1.1  christos   dst->e_ehsize = H_GET_16 (abfd, src->e_ehsize);
    253   1.1  christos   dst->e_phentsize = H_GET_16 (abfd, src->e_phentsize);
    254   1.1  christos   dst->e_phnum = H_GET_16 (abfd, src->e_phnum);
    255   1.1  christos   dst->e_shentsize = H_GET_16 (abfd, src->e_shentsize);
    256   1.1  christos   dst->e_shnum = H_GET_16 (abfd, src->e_shnum);
    257   1.1  christos   dst->e_shstrndx = H_GET_16 (abfd, src->e_shstrndx);
    258   1.1  christos }
    259   1.1  christos 
    260   1.1  christos /* Translate an ELF file header in internal format into an ELF file header in
    261   1.1  christos    external format.  */
    262   1.1  christos 
    263   1.1  christos static void
    264   1.1  christos elf_swap_ehdr_out (bfd *abfd,
    265   1.1  christos 		   const Elf_Internal_Ehdr *src,
    266   1.1  christos 		   Elf_External_Ehdr *dst)
    267   1.1  christos {
    268   1.1  christos   unsigned int tmp;
    269  1.11  christos   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    270   1.1  christos   bool no_section_header = (abfd->flags & BFD_NO_SECTION_HEADER) != 0;
    271   1.1  christos   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
    272   1.1  christos   /* note that all elements of dst are *arrays of unsigned char* already...  */
    273   1.1  christos   H_PUT_16 (abfd, src->e_type, dst->e_type);
    274   1.1  christos   H_PUT_16 (abfd, src->e_machine, dst->e_machine);
    275   1.1  christos   H_PUT_32 (abfd, src->e_version, dst->e_version);
    276   1.1  christos   if (signed_vma)
    277   1.1  christos     H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
    278   1.1  christos   else
    279   1.1  christos     H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
    280  1.11  christos   H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
    281  1.11  christos   if (no_section_header)
    282  1.11  christos     H_PUT_WORD (abfd, 0, dst->e_shoff);
    283  1.11  christos   else
    284   1.1  christos     H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
    285   1.1  christos   H_PUT_32 (abfd, src->e_flags, dst->e_flags);
    286   1.1  christos   H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
    287   1.1  christos   H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
    288   1.1  christos   tmp = src->e_phnum;
    289   1.1  christos   if (tmp > PN_XNUM)
    290   1.1  christos     tmp = PN_XNUM;
    291  1.11  christos   H_PUT_16 (abfd, tmp, dst->e_phnum);
    292  1.11  christos   if (no_section_header)
    293  1.11  christos     {
    294  1.11  christos       H_PUT_16 (abfd, 0, dst->e_shentsize);
    295  1.11  christos       H_PUT_16 (abfd, 0, dst->e_shnum);
    296  1.11  christos       H_PUT_16 (abfd, 0, dst->e_shstrndx);
    297  1.11  christos     }
    298  1.11  christos   else
    299  1.11  christos     {
    300  1.11  christos       H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
    301  1.11  christos       tmp = src->e_shnum;
    302  1.11  christos       if (tmp >= (SHN_LORESERVE & 0xffff))
    303  1.11  christos 	tmp = SHN_UNDEF;
    304  1.11  christos       H_PUT_16 (abfd, tmp, dst->e_shnum);
    305  1.11  christos       tmp = src->e_shstrndx;
    306  1.11  christos       if (tmp >= (SHN_LORESERVE & 0xffff))
    307  1.11  christos 	tmp = SHN_XINDEX & 0xffff;
    308  1.11  christos       H_PUT_16 (abfd, tmp, dst->e_shstrndx);
    309   1.1  christos     }
    310   1.1  christos }
    311   1.1  christos 
    312   1.1  christos /* Translate an ELF section header table entry in external format into an
    313   1.1  christos    ELF section header table entry in internal format.  */
    314   1.1  christos 
    315   1.1  christos static void
    316   1.1  christos elf_swap_shdr_in (bfd *abfd,
    317   1.1  christos 		  const Elf_External_Shdr *src,
    318   1.1  christos 		  Elf_Internal_Shdr *dst)
    319   1.1  christos {
    320   1.1  christos   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    321   1.1  christos 
    322   1.1  christos   dst->sh_name = H_GET_32 (abfd, src->sh_name);
    323   1.1  christos   dst->sh_type = H_GET_32 (abfd, src->sh_type);
    324   1.1  christos   dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
    325   1.1  christos   if (signed_vma)
    326   1.1  christos     dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
    327   1.1  christos   else
    328   1.1  christos     dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
    329   1.1  christos   dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
    330   1.8  christos   dst->sh_size = H_GET_WORD (abfd, src->sh_size);
    331   1.8  christos   /* PR 23657.  Check for invalid section size, in sections with contents.
    332   1.8  christos      Note - we do not set an error value here because the contents
    333   1.9  christos      of this particular section might not be needed by the consumer.  */
    334   1.9  christos   if (dst->sh_type != SHT_NOBITS)
    335   1.9  christos     {
    336   1.9  christos       ufile_ptr filesize = bfd_get_file_size (abfd);
    337   1.9  christos 
    338   1.9  christos       if (filesize != 0
    339  1.10  christos 	  && ((ufile_ptr) dst->sh_offset > filesize
    340  1.10  christos 	      || dst->sh_size > filesize - dst->sh_offset)
    341   1.9  christos 	  && !abfd->read_only)
    342   1.9  christos 	{
    343   1.9  christos 	  _bfd_error_handler (_("warning: %pB has a section "
    344  1.10  christos 				"extending past end of file"), abfd);
    345   1.9  christos 	  abfd->read_only = 1;
    346   1.9  christos 	}
    347   1.1  christos     }
    348   1.1  christos   dst->sh_link = H_GET_32 (abfd, src->sh_link);
    349   1.1  christos   dst->sh_info = H_GET_32 (abfd, src->sh_info);
    350   1.1  christos   dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
    351   1.1  christos   dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
    352   1.1  christos   dst->bfd_section = NULL;
    353   1.1  christos   dst->contents = NULL;
    354   1.1  christos }
    355   1.1  christos 
    356   1.1  christos /* Translate an ELF section header table entry in internal format into an
    357   1.1  christos    ELF section header table entry in external format.  */
    358   1.1  christos 
    359   1.1  christos static void
    360   1.1  christos elf_swap_shdr_out (bfd *abfd,
    361   1.1  christos 		   const Elf_Internal_Shdr *src,
    362   1.1  christos 		   Elf_External_Shdr *dst)
    363   1.1  christos {
    364   1.1  christos   /* note that all elements of dst are *arrays of unsigned char* already...  */
    365   1.1  christos   H_PUT_32 (abfd, src->sh_name, dst->sh_name);
    366   1.1  christos   H_PUT_32 (abfd, src->sh_type, dst->sh_type);
    367   1.1  christos   H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
    368   1.1  christos   H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
    369   1.1  christos   H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
    370   1.1  christos   H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
    371   1.1  christos   H_PUT_32 (abfd, src->sh_link, dst->sh_link);
    372   1.1  christos   H_PUT_32 (abfd, src->sh_info, dst->sh_info);
    373   1.1  christos   H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
    374   1.1  christos   H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
    375   1.1  christos }
    376   1.1  christos 
    377   1.1  christos /* Translate an ELF program header table entry in external format into an
    378   1.1  christos    ELF program header table entry in internal format.  */
    379   1.1  christos 
    380   1.1  christos void
    381   1.1  christos elf_swap_phdr_in (bfd *abfd,
    382   1.1  christos 		  const Elf_External_Phdr *src,
    383   1.1  christos 		  Elf_Internal_Phdr *dst)
    384   1.1  christos {
    385   1.1  christos   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    386   1.1  christos 
    387   1.1  christos   dst->p_type = H_GET_32 (abfd, src->p_type);
    388   1.1  christos   dst->p_flags = H_GET_32 (abfd, src->p_flags);
    389   1.1  christos   dst->p_offset = H_GET_WORD (abfd, src->p_offset);
    390   1.1  christos   if (signed_vma)
    391   1.1  christos     {
    392   1.1  christos       dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
    393   1.1  christos       dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
    394   1.1  christos     }
    395   1.1  christos   else
    396   1.1  christos     {
    397   1.1  christos       dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
    398   1.1  christos       dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
    399   1.1  christos     }
    400   1.1  christos   dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
    401   1.1  christos   dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
    402   1.1  christos   dst->p_align = H_GET_WORD (abfd, src->p_align);
    403   1.1  christos }
    404   1.1  christos 
    405   1.1  christos void
    406   1.1  christos elf_swap_phdr_out (bfd *abfd,
    407   1.1  christos 		   const Elf_Internal_Phdr *src,
    408   1.1  christos 		   Elf_External_Phdr *dst)
    409   1.1  christos {
    410   1.1  christos   const struct elf_backend_data *bed;
    411   1.1  christos   bfd_vma p_paddr;
    412   1.1  christos 
    413   1.1  christos   bed = get_elf_backend_data (abfd);
    414   1.1  christos   p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
    415   1.1  christos 
    416   1.1  christos   /* note that all elements of dst are *arrays of unsigned char* already...  */
    417   1.1  christos   H_PUT_32 (abfd, src->p_type, dst->p_type);
    418   1.1  christos   H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
    419   1.1  christos   H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
    420   1.1  christos   H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
    421   1.1  christos   H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
    422   1.1  christos   H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
    423   1.1  christos   H_PUT_32 (abfd, src->p_flags, dst->p_flags);
    424   1.1  christos   H_PUT_WORD (abfd, src->p_align, dst->p_align);
    425   1.1  christos }
    426   1.1  christos 
    427   1.1  christos /* Translate an ELF reloc from external format to internal format.  */
    428   1.1  christos void
    429   1.1  christos elf_swap_reloc_in (bfd *abfd,
    430   1.1  christos 		   const bfd_byte *s,
    431   1.1  christos 		   Elf_Internal_Rela *dst)
    432   1.1  christos {
    433   1.1  christos   const Elf_External_Rel *src = (const Elf_External_Rel *) s;
    434   1.1  christos   dst->r_offset = H_GET_WORD (abfd, src->r_offset);
    435   1.1  christos   dst->r_info = H_GET_WORD (abfd, src->r_info);
    436   1.1  christos   dst->r_addend = 0;
    437   1.1  christos }
    438   1.1  christos 
    439   1.1  christos void
    440   1.1  christos elf_swap_reloca_in (bfd *abfd,
    441   1.1  christos 		    const bfd_byte *s,
    442   1.1  christos 		    Elf_Internal_Rela *dst)
    443   1.1  christos {
    444   1.1  christos   const Elf_External_Rela *src = (const Elf_External_Rela *) s;
    445   1.1  christos   dst->r_offset = H_GET_WORD (abfd, src->r_offset);
    446   1.1  christos   dst->r_info = H_GET_WORD (abfd, src->r_info);
    447   1.1  christos   dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
    448   1.1  christos }
    449   1.1  christos 
    450   1.1  christos /* Translate an ELF reloc from internal format to external format.  */
    451   1.1  christos void
    452   1.1  christos elf_swap_reloc_out (bfd *abfd,
    453   1.1  christos 		    const Elf_Internal_Rela *src,
    454   1.1  christos 		    bfd_byte *d)
    455   1.1  christos {
    456   1.1  christos   Elf_External_Rel *dst = (Elf_External_Rel *) d;
    457   1.1  christos   H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
    458   1.1  christos   H_PUT_WORD (abfd, src->r_info, dst->r_info);
    459   1.1  christos }
    460   1.1  christos 
    461   1.1  christos void
    462   1.1  christos elf_swap_reloca_out (bfd *abfd,
    463   1.1  christos 		     const Elf_Internal_Rela *src,
    464   1.1  christos 		     bfd_byte *d)
    465   1.1  christos {
    466   1.1  christos   Elf_External_Rela *dst = (Elf_External_Rela *) d;
    467   1.1  christos   H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
    468   1.1  christos   H_PUT_WORD (abfd, src->r_info, dst->r_info);
    469   1.1  christos   H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
    470   1.1  christos }
    471   1.1  christos 
    472   1.1  christos void
    473   1.1  christos elf_swap_dyn_in (bfd *abfd,
    474   1.1  christos 		 const void *p,
    475   1.1  christos 		 Elf_Internal_Dyn *dst)
    476   1.1  christos {
    477   1.1  christos   const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
    478   1.1  christos 
    479   1.1  christos   dst->d_tag = H_GET_WORD (abfd, src->d_tag);
    480   1.1  christos   dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
    481   1.1  christos }
    482   1.1  christos 
    483   1.1  christos void
    484   1.1  christos elf_swap_dyn_out (bfd *abfd,
    485   1.1  christos 		  const Elf_Internal_Dyn *src,
    486   1.1  christos 		  void *p)
    487   1.1  christos {
    488   1.1  christos   Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
    489   1.1  christos 
    490   1.1  christos   H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
    491   1.1  christos   H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
    492   1.1  christos }
    493   1.1  christos 
    494   1.1  christos /* ELF .o/exec file reading */
    496   1.1  christos 
    497   1.1  christos /* Begin processing a given object.
    498   1.1  christos 
    499   1.1  christos    First we validate the file by reading in the ELF header and checking
    500  1.10  christos    the magic number.  */
    501   1.1  christos 
    502   1.1  christos static inline bool
    503   1.1  christos elf_file_p (Elf_External_Ehdr *x_ehdrp)
    504   1.1  christos {
    505   1.1  christos   return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
    506   1.1  christos 	  && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
    507   1.1  christos 	  && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
    508   1.1  christos 	  && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
    509   1.1  christos }
    510   1.1  christos 
    511   1.1  christos /* Check to see if the file associated with ABFD matches the target vector
    512   1.1  christos    that ABFD points to.
    513   1.1  christos 
    514   1.1  christos    Note that we may be called several times with the same ABFD, but different
    515   1.1  christos    target vectors, most of which will not match.  We have to avoid leaving
    516   1.1  christos    any side effects in ABFD, or any data it points to (like tdata), if the
    517   1.9  christos    file does not match the target vector.  */
    518   1.1  christos 
    519   1.1  christos bfd_cleanup
    520   1.1  christos elf_object_p (bfd *abfd)
    521   1.1  christos {
    522   1.1  christos   Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
    523   1.1  christos   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form */
    524   1.1  christos   Elf_External_Shdr x_shdr;	/* Section header table entry, external form */
    525   1.1  christos   Elf_Internal_Shdr i_shdr;
    526   1.1  christos   Elf_Internal_Shdr *i_shdrp;	/* Section header table, internal form */
    527   1.1  christos   unsigned int shindex;
    528   1.1  christos   const struct elf_backend_data *ebd;
    529   1.1  christos   asection *s;
    530   1.1  christos   const bfd_target *target;
    531   1.1  christos 
    532  1.11  christos   /* Read in the ELF header in external format.  */
    533   1.1  christos 
    534   1.1  christos   if (bfd_read (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
    535   1.1  christos     {
    536   1.1  christos       if (bfd_get_error () != bfd_error_system_call)
    537   1.1  christos 	goto got_wrong_format_error;
    538   1.1  christos       else
    539   1.1  christos 	goto got_no_match;
    540   1.1  christos     }
    541   1.1  christos 
    542   1.1  christos   /* Now check to see if we have a valid ELF file, and one that BFD can
    543   1.1  christos      make use of.  The magic number must match, the address size ('class')
    544   1.1  christos      and byte-swapping must match our XVEC entry, and it must have a
    545   1.1  christos      section header table (FIXME: See comments re sections at top of this
    546   1.1  christos      file).  */
    547   1.1  christos 
    548   1.1  christos   if (! elf_file_p (&x_ehdr)
    549   1.1  christos       || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
    550   1.1  christos       || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
    551   1.1  christos     goto got_wrong_format_error;
    552   1.1  christos 
    553   1.1  christos   /* Check that file's byte order matches xvec's */
    554   1.1  christos   switch (x_ehdr.e_ident[EI_DATA])
    555   1.1  christos     {
    556   1.1  christos     case ELFDATA2MSB:		/* Big-endian */
    557   1.1  christos       if (! bfd_header_big_endian (abfd))
    558   1.1  christos 	goto got_wrong_format_error;
    559   1.1  christos       break;
    560   1.1  christos     case ELFDATA2LSB:		/* Little-endian */
    561   1.1  christos       if (! bfd_header_little_endian (abfd))
    562   1.1  christos 	goto got_wrong_format_error;
    563   1.1  christos       break;
    564   1.1  christos     case ELFDATANONE:		/* No data encoding specified */
    565   1.1  christos     default:			/* Unknown data encoding specified */
    566   1.1  christos       goto got_wrong_format_error;
    567   1.1  christos     }
    568   1.1  christos 
    569   1.1  christos   target = abfd->xvec;
    570   1.1  christos 
    571   1.1  christos   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
    572   1.1  christos      the tdata pointer in the bfd.  */
    573   1.1  christos 
    574   1.1  christos   if (! (*target->_bfd_set_format[bfd_object]) (abfd))
    575   1.1  christos     goto got_no_match;
    576   1.1  christos 
    577   1.1  christos   /* Now that we know the byte order, swap in the rest of the header */
    578   1.1  christos   i_ehdrp = elf_elfheader (abfd);
    579   1.1  christos   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
    580   1.1  christos #if DEBUG & 1
    581   1.1  christos   elf_debug_file (i_ehdrp);
    582   1.1  christos #endif
    583   1.1  christos 
    584   1.1  christos   /* Reject ET_CORE (header indicates core file, not object file) */
    585   1.1  christos   if (i_ehdrp->e_type == ET_CORE)
    586   1.1  christos     goto got_wrong_format_error;
    587   1.1  christos 
    588   1.9  christos   /* If this is a relocatable file and there is no section header
    589   1.1  christos      table, then we're hosed.  */
    590   1.1  christos   if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_type == ET_REL)
    591   1.1  christos     goto got_wrong_format_error;
    592   1.1  christos 
    593   1.1  christos   /* As a simple sanity check, verify that what BFD thinks is the
    594   1.1  christos      size of each section header table entry actually matches the size
    595   1.1  christos      recorded in the file, but only if there are any sections.  */
    596   1.1  christos   if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
    597   1.1  christos     goto got_wrong_format_error;
    598   1.9  christos 
    599   1.1  christos   /* Further sanity check.  */
    600   1.1  christos   if (i_ehdrp->e_shoff < sizeof (x_ehdr) && i_ehdrp->e_shnum != 0)
    601   1.1  christos     goto got_wrong_format_error;
    602   1.1  christos 
    603   1.1  christos   ebd = get_elf_backend_data (abfd);
    604   1.1  christos   if (ebd->s->arch_size != ARCH_SIZE)
    605   1.1  christos     goto got_wrong_format_error;
    606   1.1  christos 
    607   1.1  christos   /* Check that the ELF e_machine field matches what this particular
    608   1.1  christos      BFD format expects.  */
    609   1.1  christos   if (ebd->elf_machine_code != i_ehdrp->e_machine
    610   1.1  christos       && (ebd->elf_machine_alt1 == 0
    611   1.1  christos 	  || i_ehdrp->e_machine != ebd->elf_machine_alt1)
    612   1.1  christos       && (ebd->elf_machine_alt2 == 0
    613   1.1  christos 	  || i_ehdrp->e_machine != ebd->elf_machine_alt2)
    614   1.1  christos       && ebd->elf_machine_code != EM_NONE)
    615   1.1  christos     goto got_wrong_format_error;
    616   1.1  christos 
    617   1.1  christos   if (i_ehdrp->e_type == ET_EXEC)
    618   1.1  christos     abfd->flags |= EXEC_P;
    619   1.1  christos   else if (i_ehdrp->e_type == ET_DYN)
    620   1.1  christos     abfd->flags |= DYNAMIC;
    621   1.1  christos 
    622   1.1  christos   if (i_ehdrp->e_phnum > 0)
    623   1.1  christos     abfd->flags |= D_PAGED;
    624   1.1  christos 
    625   1.1  christos   if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
    626   1.1  christos     {
    627   1.1  christos       /* It's OK if this fails for the generic target.  */
    628   1.1  christos       if (ebd->elf_machine_code != EM_NONE)
    629   1.1  christos 	goto got_no_match;
    630   1.1  christos     }
    631   1.1  christos 
    632   1.1  christos   if (ebd->elf_machine_code != EM_NONE
    633   1.1  christos       && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
    634   1.1  christos       && ebd->elf_osabi != ELFOSABI_NONE)
    635   1.9  christos     goto got_wrong_format_error;
    636   1.1  christos 
    637   1.6  christos   if (i_ehdrp->e_shoff >= sizeof (x_ehdr))
    638   1.1  christos     {
    639   1.1  christos       file_ptr where = (file_ptr) i_ehdrp->e_shoff;
    640   1.6  christos 
    641   1.1  christos       /* Seek to the section header table in the file.  */
    642   1.1  christos       if (bfd_seek (abfd, where, SEEK_SET) != 0)
    643   1.1  christos 	goto got_no_match;
    644   1.1  christos 
    645  1.11  christos       /* Read the first section header at index 0, and convert to internal
    646   1.1  christos 	 form.  */
    647   1.1  christos       if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
    648   1.1  christos 	goto got_no_match;
    649   1.1  christos       elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
    650   1.1  christos 
    651   1.1  christos       /* If the section count is zero, the actual count is in the first
    652   1.1  christos 	 section header.  */
    653   1.1  christos       if (i_ehdrp->e_shnum == SHN_UNDEF)
    654   1.1  christos 	{
    655   1.1  christos 	  i_ehdrp->e_shnum = i_shdr.sh_size;
    656   1.1  christos 	  if (i_ehdrp->e_shnum >= SHN_LORESERVE
    657   1.1  christos 	      || i_ehdrp->e_shnum != i_shdr.sh_size
    658   1.1  christos 	      || i_ehdrp->e_shnum  == 0)
    659   1.1  christos 	    goto got_wrong_format_error;
    660   1.1  christos 	}
    661   1.1  christos 
    662   1.1  christos       /* And similarly for the string table index.  */
    663   1.1  christos       if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
    664   1.1  christos 	{
    665   1.1  christos 	  i_ehdrp->e_shstrndx = i_shdr.sh_link;
    666   1.1  christos 	  if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
    667   1.1  christos 	    goto got_wrong_format_error;
    668   1.1  christos 	}
    669   1.1  christos 
    670   1.1  christos       /* And program headers.  */
    671   1.1  christos       if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
    672   1.1  christos 	{
    673   1.1  christos 	  i_ehdrp->e_phnum = i_shdr.sh_info;
    674   1.1  christos 	  if (i_ehdrp->e_phnum != i_shdr.sh_info)
    675   1.1  christos 	    goto got_wrong_format_error;
    676   1.1  christos 	}
    677   1.1  christos 
    678   1.1  christos       /* Sanity check that we can read all of the section headers.
    679   1.1  christos 	 It ought to be good enough to just read the last one.  */
    680   1.1  christos       if (i_ehdrp->e_shnum != 1)
    681   1.1  christos 	{
    682   1.1  christos 	  /* Check that we don't have a totally silly number of sections.  */
    683   1.1  christos 	  if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
    684   1.1  christos 	      || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
    685   1.1  christos 	    goto got_wrong_format_error;
    686   1.1  christos 
    687   1.1  christos 	  where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
    688   1.1  christos 	  if ((bfd_size_type) where <= i_ehdrp->e_shoff)
    689   1.6  christos 	    goto got_wrong_format_error;
    690   1.1  christos 
    691  1.11  christos 	  if (bfd_seek (abfd, where, SEEK_SET) != 0)
    692   1.1  christos 	    goto got_no_match;
    693   1.1  christos 	  if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
    694   1.1  christos 	    goto got_no_match;
    695   1.1  christos 
    696   1.6  christos 	  /* Back to where we were.  */
    697   1.1  christos 	  where = i_ehdrp->e_shoff + sizeof (x_shdr);
    698   1.1  christos 	  if (bfd_seek (abfd, where, SEEK_SET) != 0)
    699   1.1  christos 	    goto got_no_match;
    700   1.1  christos 	}
    701   1.1  christos     }
    702   1.1  christos 
    703   1.1  christos   /* Allocate space for a copy of the section header table in
    704   1.1  christos      internal form.  */
    705   1.1  christos   if (i_ehdrp->e_shnum != 0)
    706   1.1  christos     {
    707   1.9  christos       Elf_Internal_Shdr *shdrp;
    708   1.1  christos       unsigned int num_sec;
    709   1.9  christos       size_t amt;
    710   1.6  christos 
    711   1.1  christos       if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*i_shdrp), &amt))
    712   1.1  christos 	goto got_wrong_format_error;
    713   1.1  christos       i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
    714   1.1  christos       if (!i_shdrp)
    715   1.1  christos 	goto got_no_match;
    716   1.9  christos       num_sec = i_ehdrp->e_shnum;
    717   1.9  christos       elf_numsections (abfd) = num_sec;
    718   1.1  christos       if (_bfd_mul_overflow (num_sec, sizeof (i_shdrp), &amt))
    719   1.1  christos 	goto got_wrong_format_error;
    720   1.1  christos       elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
    721  1.10  christos       if (!elf_elfsections (abfd))
    722  1.10  christos 	goto got_no_match;
    723  1.10  christos       elf_tdata (abfd)->being_created = bfd_zalloc (abfd, num_sec);
    724   1.1  christos       if (!elf_tdata (abfd)->being_created)
    725   1.1  christos 	goto got_no_match;
    726   1.1  christos 
    727   1.1  christos       memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
    728   1.1  christos       for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
    729   1.1  christos 	elf_elfsections (abfd)[shindex] = shdrp++;
    730   1.1  christos 
    731   1.1  christos       /* Read in the rest of the section header table and convert it
    732   1.1  christos 	 to internal form.  */
    733  1.11  christos       for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
    734   1.1  christos 	{
    735   1.1  christos 	  if (bfd_read (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
    736   1.1  christos 	    goto got_no_match;
    737   1.1  christos 	  elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
    738   1.1  christos 
    739   1.1  christos 	  /* Sanity check sh_link and sh_info.  */
    740   1.1  christos 	  if (i_shdrp[shindex].sh_link >= num_sec)
    741   1.1  christos 	    {
    742   1.1  christos 	      /* PR 10478: Accept Solaris binaries with a sh_link
    743   1.1  christos 		 field set to SHN_BEFORE or SHN_AFTER.  */
    744   1.1  christos 	      switch (ebd->elf_machine_code)
    745   1.5  christos 		{
    746   1.1  christos 		case EM_386:
    747   1.1  christos 		case EM_IAMCU:
    748   1.1  christos 		case EM_X86_64:
    749   1.1  christos 		case EM_OLD_SPARCV9:
    750   1.1  christos 		case EM_SPARC32PLUS:
    751   1.1  christos 		case EM_SPARCV9:
    752   1.1  christos 		case EM_SPARC:
    753   1.1  christos 		  if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
    754   1.1  christos 		      || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
    755   1.1  christos 		    break;
    756   1.1  christos 		  /* Otherwise fall through.  */
    757   1.1  christos 		default:
    758   1.1  christos 		  goto got_wrong_format_error;
    759   1.1  christos 		}
    760   1.1  christos 	    }
    761   1.1  christos 
    762   1.1  christos 	  if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
    763   1.1  christos 	       || i_shdrp[shindex].sh_type == SHT_RELA
    764   1.1  christos 	       || i_shdrp[shindex].sh_type == SHT_REL)
    765   1.1  christos 	      && i_shdrp[shindex].sh_info >= num_sec)
    766   1.1  christos 	    goto got_wrong_format_error;
    767   1.1  christos 
    768   1.1  christos 	  /* If the section is loaded, but not page aligned, clear
    769   1.1  christos 	     D_PAGED.  */
    770   1.1  christos 	  if (i_shdrp[shindex].sh_size != 0
    771   1.1  christos 	      && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
    772   1.1  christos 	      && i_shdrp[shindex].sh_type != SHT_NOBITS
    773   1.1  christos 	      && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
    774   1.1  christos 		   % ebd->minpagesize)
    775   1.1  christos 		  != 0))
    776   1.1  christos 	    abfd->flags &= ~D_PAGED;
    777   1.9  christos 	}
    778   1.9  christos 
    779   1.1  christos       if (i_ehdrp->e_shstrndx >= elf_numsections (abfd)
    780   1.1  christos 	  || i_shdrp[i_ehdrp->e_shstrndx].sh_type != SHT_STRTAB)
    781   1.1  christos 	{
    782   1.1  christos 	  /* PR 2257:
    783   1.1  christos 	     We used to just goto got_wrong_format_error here
    784   1.1  christos 	     but there are binaries in existance for which this test
    785   1.1  christos 	     will prevent the binutils from working with them at all.
    786   1.1  christos 	     So we are kind, and reset the string index value to 0
    787  1.10  christos 	     so that at least some processing can be done.  */
    788  1.10  christos 	  i_ehdrp->e_shstrndx = SHN_UNDEF;
    789  1.10  christos 	  if (!abfd->read_only)
    790  1.10  christos 	    {
    791  1.10  christos 	      _bfd_error_handler
    792  1.10  christos 		(_("warning: %pB has a corrupt string table index"), abfd);
    793   1.1  christos 	      abfd->read_only = 1;
    794   1.1  christos 	    }
    795   1.1  christos 	}
    796   1.1  christos     }
    797   1.1  christos   else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
    798   1.1  christos     goto got_wrong_format_error;
    799   1.1  christos 
    800   1.1  christos   /* Read in the program headers.  */
    801   1.1  christos   if (i_ehdrp->e_phnum == 0)
    802   1.1  christos     elf_tdata (abfd)->phdr = NULL;
    803   1.1  christos   else
    804   1.1  christos     {
    805   1.9  christos       Elf_Internal_Phdr *i_phdr;
    806   1.9  christos       unsigned int i;
    807   1.1  christos       ufile_ptr filesize;
    808   1.8  christos       size_t amt;
    809   1.8  christos 
    810   1.9  christos       /* Check for a corrupt input file with an impossibly large number
    811   1.9  christos 	 of program headers.  */
    812   1.9  christos       filesize = bfd_get_file_size (abfd);
    813   1.9  christos       if (filesize != 0
    814   1.9  christos 	  && i_ehdrp->e_phnum > filesize / sizeof (Elf_External_Phdr))
    815   1.9  christos 	goto got_wrong_format_error;
    816   1.9  christos       if (_bfd_mul_overflow (i_ehdrp->e_phnum, sizeof (*i_phdr), &amt))
    817   1.9  christos 	goto got_wrong_format_error;
    818   1.1  christos       elf_tdata (abfd)->phdr
    819   1.1  christos 	= (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
    820  1.11  christos       if (elf_tdata (abfd)->phdr == NULL)
    821   1.1  christos 	goto got_no_match;
    822  1.11  christos       if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
    823   1.1  christos 	goto got_no_match;
    824   1.1  christos       bool eu_strip_broken_phdrs = false;
    825   1.1  christos       i_phdr = elf_tdata (abfd)->phdr;
    826   1.1  christos       for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
    827   1.1  christos 	{
    828  1.11  christos 	  Elf_External_Phdr x_phdr;
    829   1.1  christos 
    830   1.1  christos 	  if (bfd_read (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
    831   1.9  christos 	    goto got_no_match;
    832   1.9  christos 	  elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
    833   1.9  christos 	  /* Too much code in BFD relies on alignment being a power of
    834   1.9  christos 	     two, as required by the ELF spec.  */
    835  1.10  christos 	  if (i_phdr->p_align != (i_phdr->p_align & -i_phdr->p_align))
    836  1.10  christos 	    {
    837  1.10  christos 	      i_phdr->p_align &= -i_phdr->p_align;
    838  1.10  christos 	      if (!abfd->read_only)
    839  1.10  christos 		{
    840  1.10  christos 		  _bfd_error_handler (_("warning: %pB has a program header "
    841  1.10  christos 					"with invalid alignment"), abfd);
    842   1.9  christos 		  abfd->read_only = 1;
    843  1.11  christos 		}
    844  1.11  christos 	    }
    845  1.11  christos 	  /* Detect eu-strip -f debug files, which have program
    846  1.11  christos 	     headers that describe the original file.  */
    847  1.11  christos 	  if (i_phdr->p_filesz != 0
    848  1.11  christos 	      && (i_phdr->p_filesz > filesize
    849  1.11  christos 		  || i_phdr->p_offset > filesize - i_phdr->p_filesz))
    850  1.11  christos 	    eu_strip_broken_phdrs = true;
    851  1.11  christos 	}
    852  1.11  christos       if (!eu_strip_broken_phdrs
    853  1.11  christos 	  && i_ehdrp->e_shoff == 0
    854  1.11  christos 	  && i_ehdrp->e_shstrndx == 0)
    855  1.11  christos 	{
    856  1.11  christos 	  /* Try to reconstruct dynamic symbol table from PT_DYNAMIC
    857  1.11  christos 	     segment if there is no section header.  */
    858  1.11  christos 	  i_phdr = elf_tdata (abfd)->phdr;
    859  1.11  christos 	  for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
    860  1.11  christos 	    if (i_phdr->p_type == PT_DYNAMIC)
    861  1.11  christos 	      {
    862  1.11  christos 		if (i_phdr->p_filesz != 0
    863  1.11  christos 		    && !_bfd_elf_get_dynamic_symbols (abfd, i_phdr,
    864  1.11  christos 						      elf_tdata (abfd)->phdr,
    865  1.11  christos 						      i_ehdrp->e_phnum,
    866  1.11  christos 						      filesize))
    867  1.11  christos 		  goto got_no_match;
    868   1.1  christos 		break;
    869   1.1  christos 	      }
    870   1.1  christos 	}
    871   1.9  christos     }
    872   1.1  christos 
    873   1.1  christos   if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff >= sizeof (x_ehdr))
    874   1.1  christos     {
    875   1.1  christos       unsigned int num_sec;
    876   1.1  christos 
    877   1.1  christos       /* Once all of the section headers have been read and converted, we
    878   1.1  christos 	 can start processing them.  Note that the first section header is
    879   1.1  christos 	 a dummy placeholder entry, so we ignore it.  */
    880   1.1  christos       num_sec = elf_numsections (abfd);
    881   1.1  christos       for (shindex = 1; shindex < num_sec; shindex++)
    882   1.1  christos 	if (!bfd_section_from_shdr (abfd, shindex))
    883   1.1  christos 	  goto got_no_match;
    884   1.1  christos 
    885   1.1  christos       /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
    886   1.1  christos       if (! _bfd_elf_setup_sections (abfd))
    887   1.1  christos 	goto got_wrong_format_error;
    888   1.1  christos     }
    889   1.1  christos 
    890   1.1  christos   /* Let the backend double check the format and override global
    891   1.1  christos      information.  */
    892   1.1  christos   if (ebd->elf_backend_object_p)
    893   1.1  christos     {
    894   1.1  christos       if (! (*ebd->elf_backend_object_p) (abfd))
    895   1.1  christos 	goto got_wrong_format_error;
    896   1.1  christos     }
    897   1.1  christos 
    898   1.1  christos   /* Remember the entry point specified in the ELF file header.  */
    899   1.1  christos   bfd_set_start_address (abfd, i_ehdrp->e_entry);
    900   1.1  christos 
    901   1.1  christos   /* If we have created any reloc sections that are associated with
    902   1.1  christos      debugging sections, mark the reloc sections as debugging as well.  */
    903   1.1  christos   for (s = abfd->sections; s != NULL; s = s->next)
    904   1.1  christos     {
    905   1.1  christos       if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
    906   1.1  christos 	   || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
    907   1.1  christos 	  && elf_section_data (s)->this_hdr.sh_info > 0)
    908   1.1  christos 	{
    909   1.1  christos 	  unsigned long targ_index;
    910   1.1  christos 	  asection *targ_sec;
    911   1.1  christos 
    912   1.1  christos 	  targ_index = elf_section_data (s)->this_hdr.sh_info;
    913   1.1  christos 	  targ_sec = bfd_section_from_elf_index (abfd, targ_index);
    914   1.1  christos 	  if (targ_sec != NULL
    915   1.1  christos 	      && (targ_sec->flags & SEC_DEBUGGING) != 0)
    916   1.1  christos 	    s->flags |= SEC_DEBUGGING;
    917   1.9  christos 	}
    918   1.1  christos     }
    919   1.1  christos   return _bfd_no_cleanup;
    920   1.1  christos 
    921   1.1  christos  got_wrong_format_error:
    922   1.1  christos   bfd_set_error (bfd_error_wrong_format);
    923   1.1  christos 
    924   1.1  christos  got_no_match:
    925   1.1  christos   return NULL;
    926   1.1  christos }
    927   1.1  christos 
    928   1.1  christos /* ELF .o/exec file writing */
    930   1.1  christos 
    931   1.1  christos /* Write out the relocs.  */
    932   1.1  christos 
    933   1.9  christos void
    934  1.10  christos elf_write_relocs (bfd *abfd, asection *sec, void *data)
    935   1.1  christos {
    936   1.1  christos   const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
    937   1.1  christos   bool *failedp = (bool *) data;
    938   1.1  christos   Elf_Internal_Shdr *rela_hdr;
    939   1.1  christos   bfd_vma addr_offset;
    940   1.1  christos   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
    941   1.1  christos   size_t extsize;
    942   1.1  christos   bfd_byte *dst_rela;
    943   1.9  christos   unsigned int idx;
    944   1.1  christos   asymbol *last_sym;
    945   1.1  christos   int last_sym_idx;
    946   1.1  christos   size_t amt;
    947   1.1  christos 
    948   1.1  christos   /* If we have already failed, don't do anything.  */
    949   1.1  christos   if (*failedp)
    950   1.1  christos     return;
    951   1.1  christos 
    952   1.1  christos   if ((sec->flags & SEC_RELOC) == 0)
    953   1.1  christos     return;
    954   1.1  christos 
    955   1.1  christos   /* The linker backend writes the relocs out itself, and sets the
    956   1.1  christos      reloc_count field to zero to inhibit writing them here.  Also,
    957   1.1  christos      sometimes the SEC_RELOC flag gets set even when there aren't any
    958   1.1  christos      relocs.  */
    959   1.1  christos   if (sec->reloc_count == 0)
    960   1.1  christos     return;
    961   1.1  christos 
    962   1.1  christos   /* If we have opened an existing file for update, reloc_count may be
    963   1.1  christos      set even though we are not linking.  In that case we have nothing
    964   1.1  christos      to do.  */
    965   1.1  christos   if (sec->orelocation == NULL)
    966   1.1  christos     return;
    967   1.1  christos 
    968   1.1  christos   rela_hdr = elf_section_data (sec)->rela.hdr;
    969   1.1  christos   if (rela_hdr == NULL)
    970   1.9  christos     rela_hdr = elf_section_data (sec)->rel.hdr;
    971   1.9  christos 
    972   1.1  christos   rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
    973   1.9  christos   if (_bfd_mul_overflow (sec->reloc_count, rela_hdr->sh_entsize, &amt)
    974  1.10  christos       || (rela_hdr->contents = bfd_alloc (abfd, amt)) == NULL)
    975   1.1  christos     {
    976   1.1  christos       bfd_set_error (bfd_error_no_memory);
    977   1.1  christos       *failedp = true;
    978   1.1  christos       return;
    979   1.1  christos     }
    980   1.1  christos 
    981   1.1  christos   /* Figure out whether the relocations are RELA or REL relocations.  */
    982   1.1  christos   if (rela_hdr->sh_type == SHT_RELA)
    983   1.1  christos     {
    984   1.1  christos       swap_out = elf_swap_reloca_out;
    985   1.1  christos       extsize = sizeof (Elf_External_Rela);
    986   1.1  christos     }
    987   1.1  christos   else if (rela_hdr->sh_type == SHT_REL)
    988   1.1  christos     {
    989   1.1  christos       swap_out = elf_swap_reloc_out;
    990   1.1  christos       extsize = sizeof (Elf_External_Rel);
    991   1.1  christos     }
    992   1.1  christos   else
    993   1.1  christos     /* Every relocation section should be either an SHT_RELA or an
    994   1.1  christos        SHT_REL section.  */
    995   1.1  christos     abort ();
    996   1.1  christos 
    997   1.1  christos   /* The address of an ELF reloc is section relative for an object
    998   1.1  christos      file, and absolute for an executable file or shared library.
    999   1.1  christos      The address of a BFD reloc is always section relative.  */
   1000   1.1  christos   addr_offset = 0;
   1001   1.1  christos   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   1002   1.1  christos     addr_offset = sec->vma;
   1003   1.1  christos 
   1004   1.1  christos   /* orelocation has the data, reloc_count has the count...  */
   1005   1.1  christos   last_sym = 0;
   1006   1.1  christos   last_sym_idx = 0;
   1007   1.1  christos   dst_rela = rela_hdr->contents;
   1008   1.1  christos 
   1009   1.1  christos   for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
   1010   1.1  christos     {
   1011   1.1  christos       Elf_Internal_Rela src_rela;
   1012   1.1  christos       arelent *ptr;
   1013   1.1  christos       asymbol *sym;
   1014   1.1  christos       int n;
   1015   1.1  christos 
   1016   1.1  christos       ptr = sec->orelocation[idx];
   1017  1.11  christos       sym = *ptr->sym_ptr_ptr;
   1018  1.11  christos       if (sym == last_sym)
   1019  1.11  christos 	n = last_sym_idx;
   1020  1.11  christos       /* If the relocation is against an absolute symbol whoes value is
   1021  1.11  christos 	 zero, then the symbol can be dropped, simplifying the reloc.
   1022  1.11  christos 	 PR 31106: Except for complex relocations where the symbols
   1023  1.11  christos 	 itself might be significant.  */
   1024   1.1  christos       else if (bfd_is_abs_section (sym->section)
   1025   1.1  christos 	       && sym->value == 0
   1026   1.1  christos 	       && (sym->flags & BSF_RELC) == 0)
   1027   1.1  christos 	n = STN_UNDEF;
   1028   1.1  christos       else
   1029   1.1  christos 	{
   1030   1.1  christos 	  last_sym = sym;
   1031  1.10  christos 	  n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
   1032   1.1  christos 	  if (n < 0)
   1033   1.1  christos 	    {
   1034   1.1  christos 	      *failedp = true;
   1035   1.1  christos 	      return;
   1036   1.1  christos 	    }
   1037   1.1  christos 	  last_sym_idx = n;
   1038   1.1  christos 	}
   1039   1.1  christos 
   1040   1.1  christos       if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
   1041  1.10  christos 	  && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
   1042   1.1  christos 	  && ! _bfd_elf_validate_reloc (abfd, ptr))
   1043   1.1  christos 	{
   1044   1.1  christos 	  *failedp = true;
   1045   1.8  christos 	  return;
   1046   1.8  christos 	}
   1047  1.10  christos 
   1048   1.8  christos       if (ptr->howto == NULL)
   1049   1.8  christos 	{
   1050   1.8  christos 	  *failedp = true;
   1051  1.10  christos 	  return;
   1052  1.10  christos 	}
   1053  1.10  christos 
   1054  1.10  christos #if defined(BFD64) && ARCH_SIZE == 32
   1055  1.10  christos       if (rela_hdr->sh_type == SHT_RELA
   1056  1.10  christos 	  && ptr->howto->bitsize > 32
   1057  1.10  christos 	  && ptr->addend - INT32_MIN > UINT32_MAX)
   1058  1.10  christos 	{
   1059  1.10  christos 	  _bfd_error_handler (_("%pB: %pA+%" PRIx64 ": "
   1060  1.10  christos 				"relocation addend %" PRIx64 " too large"),
   1061  1.10  christos 			      abfd, sec, (uint64_t) ptr->address,
   1062  1.10  christos 			      (uint64_t) ptr->addend);
   1063  1.10  christos 	  *failedp = true;
   1064  1.10  christos 	  bfd_set_error (bfd_error_bad_value);
   1065   1.1  christos 	}
   1066   1.1  christos #endif
   1067   1.1  christos 
   1068   1.1  christos       src_rela.r_offset = ptr->address + addr_offset;
   1069   1.1  christos       src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
   1070   1.9  christos       src_rela.r_addend = ptr->addend;
   1071   1.9  christos       (*swap_out) (abfd, &src_rela, dst_rela);
   1072   1.9  christos     }
   1073   1.9  christos 
   1074  1.10  christos   if (elf_section_data (sec)->has_secondary_relocs
   1075   1.9  christos       && !bed->write_secondary_relocs (abfd, sec))
   1076   1.9  christos     {
   1077   1.1  christos       *failedp = true;
   1078   1.1  christos       return;
   1079   1.1  christos     }
   1080   1.1  christos }
   1081   1.1  christos 
   1082   1.1  christos /* Write out the program headers.  */
   1083   1.1  christos 
   1084   1.1  christos int
   1085   1.1  christos elf_write_out_phdrs (bfd *abfd,
   1086   1.1  christos 		     const Elf_Internal_Phdr *phdr,
   1087   1.1  christos 		     unsigned int count)
   1088   1.1  christos {
   1089   1.7  christos   while (count--)
   1090   1.1  christos     {
   1091  1.11  christos       Elf_External_Phdr extphdr;
   1092   1.1  christos 
   1093   1.1  christos       elf_swap_phdr_out (abfd, phdr, &extphdr);
   1094   1.1  christos       if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), abfd)
   1095   1.1  christos 	  != sizeof (Elf_External_Phdr))
   1096   1.1  christos 	return -1;
   1097   1.1  christos       phdr++;
   1098   1.1  christos     }
   1099   1.1  christos   return 0;
   1100   1.1  christos }
   1101  1.10  christos 
   1102   1.1  christos /* Write out the section headers and the ELF file header.  */
   1103   1.1  christos 
   1104   1.1  christos bool
   1105   1.1  christos elf_write_shdrs_and_ehdr (bfd *abfd)
   1106   1.1  christos {
   1107   1.1  christos   Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
   1108   1.1  christos   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form */
   1109   1.9  christos   Elf_External_Shdr *x_shdrp;	/* Section header table, external form */
   1110   1.1  christos   Elf_Internal_Shdr **i_shdrp;	/* Section header table, internal form */
   1111   1.1  christos   unsigned int count;
   1112   1.1  christos   size_t amt;
   1113   1.1  christos 
   1114   1.1  christos   i_ehdrp = elf_elfheader (abfd);
   1115   1.1  christos   i_shdrp = elf_elfsections (abfd);
   1116   1.1  christos 
   1117   1.1  christos   /* swap the header before spitting it out...  */
   1118   1.1  christos 
   1119   1.1  christos #if DEBUG & 1
   1120   1.1  christos   elf_debug_file (i_ehdrp);
   1121  1.11  christos #endif
   1122  1.11  christos   elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
   1123  1.10  christos   amt = sizeof (x_ehdr);
   1124   1.1  christos   if (bfd_seek (abfd, 0, SEEK_SET) != 0
   1125  1.11  christos       || bfd_write (&x_ehdr, amt, abfd) != amt)
   1126  1.11  christos     return false;
   1127  1.11  christos 
   1128   1.1  christos   if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0)
   1129   1.1  christos     return true;
   1130   1.1  christos 
   1131   1.1  christos   /* Some fields in the first section header handle overflow of ehdr
   1132   1.1  christos      fields.  */
   1133   1.1  christos   if (i_ehdrp->e_phnum >= PN_XNUM)
   1134   1.1  christos     i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
   1135   1.1  christos   if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
   1136   1.1  christos     i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
   1137   1.1  christos   if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
   1138   1.9  christos     i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
   1139   1.9  christos 
   1140   1.9  christos   /* at this point we've concocted all the ELF sections...  */
   1141  1.10  christos   if (_bfd_mul_overflow (i_ehdrp->e_shnum, sizeof (*x_shdrp), &amt))
   1142   1.9  christos     {
   1143   1.1  christos       bfd_set_error (bfd_error_no_memory);
   1144   1.1  christos       return false;
   1145  1.10  christos     }
   1146   1.1  christos   x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
   1147   1.1  christos   if (!x_shdrp)
   1148   1.1  christos     return false;
   1149   1.1  christos 
   1150   1.1  christos   for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
   1151   1.1  christos     {
   1152   1.1  christos #if DEBUG & 2
   1153   1.1  christos       elf_debug_section (count, *i_shdrp);
   1154   1.9  christos #endif
   1155  1.11  christos       elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
   1156  1.11  christos     }
   1157  1.10  christos   amt = (bfd_size_type) i_ehdrp->e_shnum * sizeof (*x_shdrp);
   1158   1.1  christos   if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0
   1159   1.1  christos       || bfd_write (x_shdrp, amt, abfd) != amt)
   1160   1.1  christos     return false;
   1161  1.10  christos 
   1162   1.1  christos   /* need to dump the string table too...  */
   1163   1.1  christos 
   1164  1.10  christos   return true;
   1165   1.1  christos }
   1166   1.1  christos 
   1167   1.1  christos bool
   1168   1.1  christos elf_checksum_contents (bfd *abfd,
   1169   1.1  christos 		       void (*process) (const void *, size_t, void *),
   1170   1.1  christos 		       void *arg)
   1171   1.1  christos {
   1172   1.1  christos   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   1173   1.1  christos   Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
   1174   1.1  christos   Elf_Internal_Phdr *i_phdrp = elf_tdata (abfd)->phdr;
   1175   1.1  christos   unsigned int count, num;
   1176   1.1  christos 
   1177   1.1  christos   {
   1178   1.1  christos     Elf_External_Ehdr x_ehdr;
   1179   1.1  christos     Elf_Internal_Ehdr i_ehdr;
   1180   1.1  christos 
   1181   1.1  christos     i_ehdr = *i_ehdrp;
   1182   1.1  christos     i_ehdr.e_phoff = i_ehdr.e_shoff = 0;
   1183   1.1  christos     elf_swap_ehdr_out (abfd, &i_ehdr, &x_ehdr);
   1184   1.1  christos     (*process) (&x_ehdr, sizeof x_ehdr, arg);
   1185   1.1  christos   }
   1186   1.1  christos 
   1187   1.1  christos   num = i_ehdrp->e_phnum;
   1188   1.1  christos   for (count = 0; count < num; count++)
   1189   1.1  christos     {
   1190   1.1  christos       Elf_External_Phdr x_phdr;
   1191   1.1  christos       elf_swap_phdr_out (abfd, &i_phdrp[count], &x_phdr);
   1192   1.1  christos       (*process) (&x_phdr, sizeof x_phdr, arg);
   1193   1.1  christos     }
   1194   1.1  christos 
   1195   1.1  christos   num = elf_numsections (abfd);
   1196   1.1  christos   for (count = 0; count < num; count++)
   1197   1.1  christos     {
   1198  1.11  christos       Elf_Internal_Shdr i_shdr;
   1199   1.1  christos       Elf_External_Shdr x_shdr;
   1200   1.1  christos       bfd_byte *contents, *free_contents;
   1201   1.1  christos       asection *sec = NULL;
   1202   1.1  christos 
   1203   1.1  christos       i_shdr = *i_shdrp[count];
   1204   1.1  christos       i_shdr.sh_offset = 0;
   1205   1.1  christos 
   1206   1.1  christos       elf_swap_shdr_out (abfd, &i_shdr, &x_shdr);
   1207   1.1  christos       (*process) (&x_shdr, sizeof x_shdr, arg);
   1208   1.1  christos 
   1209   1.1  christos       /* Process the section's contents, if it has some.
   1210   1.1  christos 	 PR ld/12451: Read them in if necessary.  */
   1211   1.1  christos       if (i_shdr.sh_type == SHT_NOBITS)
   1212   1.1  christos 	continue;
   1213   1.1  christos       free_contents = NULL;
   1214   1.1  christos       contents = i_shdr.contents;
   1215   1.1  christos       if (contents == NULL)
   1216   1.1  christos 	{
   1217   1.1  christos 	  sec = bfd_section_from_elf_index (abfd, count);
   1218   1.1  christos 	  if (sec != NULL)
   1219   1.1  christos 	    {
   1220   1.1  christos 	      contents = sec->contents;
   1221   1.1  christos 	      if (contents == NULL)
   1222  1.11  christos 		{
   1223   1.1  christos 		  /* Force rereading from file.  */
   1224   1.1  christos 		  sec->flags &= ~SEC_IN_MEMORY;
   1225   1.1  christos 		  if (!_bfd_elf_mmap_section_contents (abfd, sec, &free_contents))
   1226   1.1  christos 		    continue;
   1227   1.1  christos 		  contents = free_contents;
   1228   1.1  christos 		}
   1229   1.1  christos 	    }
   1230   1.1  christos 	}
   1231  1.11  christos       if (contents != NULL)
   1232   1.1  christos 	{
   1233   1.1  christos 	  (*process) (contents, i_shdr.sh_size, arg);
   1234   1.1  christos 	  _bfd_elf_munmap_section_contents (sec, free_contents);
   1235  1.10  christos 	}
   1236   1.1  christos     }
   1237   1.1  christos 
   1238   1.1  christos   return true;
   1239  1.10  christos }
   1240   1.1  christos 
   1241   1.1  christos long
   1242   1.1  christos elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bool dynamic)
   1243   1.1  christos {
   1244   1.1  christos   Elf_Internal_Shdr *hdr;
   1245   1.1  christos   Elf_Internal_Shdr *verhdr;
   1246   1.1  christos   unsigned long symcount;	/* Number of external ELF symbols */
   1247   1.1  christos   elf_symbol_type *sym;		/* Pointer to current bfd symbol */
   1248   1.1  christos   elf_symbol_type *symbase;	/* Buffer for generated bfd symbols */
   1249   1.1  christos   Elf_Internal_Sym *isym;
   1250   1.1  christos   Elf_Internal_Sym *isymend;
   1251   1.1  christos   Elf_Internal_Sym *isymbuf = NULL;
   1252   1.9  christos   Elf_External_Versym *xver;
   1253   1.1  christos   Elf_External_Versym *xverbuf = NULL;
   1254   1.1  christos   const struct elf_backend_data *ebd;
   1255   1.1  christos   size_t amt;
   1256   1.1  christos 
   1257   1.1  christos   /* Read each raw ELF symbol, converting from external ELF form to
   1258   1.1  christos      internal ELF form, and then using the information to create a
   1259   1.1  christos      canonical bfd symbol table entry.
   1260   1.1  christos 
   1261   1.1  christos      Note that we allocate the initial bfd canonical symbol buffer
   1262   1.1  christos      based on a one-to-one mapping of the ELF symbols to canonical
   1263  1.11  christos      symbols.  We actually use all the ELF symbols, so there will be no
   1264   1.1  christos      space left over at the end.  When we have all the symbols, we
   1265   1.1  christos      build the caller's pointer vector.  */
   1266   1.1  christos   ebd = get_elf_backend_data (abfd);
   1267   1.1  christos 
   1268   1.1  christos   if (! dynamic)
   1269  1.11  christos     {
   1270   1.1  christos       hdr = &elf_tdata (abfd)->symtab_hdr;
   1271   1.1  christos       verhdr = NULL;
   1272   1.1  christos       symcount = hdr->sh_size / ebd->s->sizeof_sym;
   1273   1.1  christos     }
   1274   1.1  christos   else
   1275   1.1  christos     {
   1276   1.1  christos       hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   1277   1.1  christos       if (elf_dynversym (abfd) == 0)
   1278   1.1  christos 	verhdr = NULL;
   1279   1.1  christos       else
   1280   1.1  christos 	verhdr = &elf_tdata (abfd)->dynversym_hdr;
   1281  1.11  christos       if ((elf_dynverdef (abfd) != 0
   1282  1.11  christos 	   && elf_tdata (abfd)->verdef == NULL)
   1283  1.11  christos 	  || (elf_dynverref (abfd) != 0
   1284   1.1  christos 	      && elf_tdata (abfd)->verref == NULL)
   1285  1.10  christos 	  || elf_tdata (abfd)->dt_verdef != NULL
   1286   1.1  christos 	  || elf_tdata (abfd)->dt_verneed != NULL)
   1287   1.1  christos 	{
   1288  1.11  christos 	  if (!_bfd_elf_slurp_version_tables (abfd, false))
   1289  1.11  christos 	    return -1;
   1290   1.1  christos 	}
   1291   1.1  christos 
   1292  1.11  christos       symcount = elf_tdata (abfd)->dt_symtab_count;
   1293  1.11  christos     }
   1294  1.11  christos 
   1295   1.1  christos   if (symcount == 0)
   1296   1.1  christos     symcount = hdr->sh_size / sizeof (Elf_External_Sym);
   1297   1.1  christos 
   1298   1.1  christos   if (symcount == 0)
   1299  1.11  christos     sym = symbase = NULL;
   1300  1.11  christos   else
   1301   1.1  christos     {
   1302   1.1  christos       size_t i;
   1303   1.1  christos 
   1304   1.1  christos       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
   1305   1.1  christos 				      NULL, NULL, NULL);
   1306   1.9  christos       if (isymbuf == NULL)
   1307   1.9  christos 	return -1;
   1308   1.9  christos 
   1309   1.9  christos       if (_bfd_mul_overflow (symcount, sizeof (elf_symbol_type), &amt))
   1310   1.9  christos 	{
   1311   1.1  christos 	  bfd_set_error (bfd_error_file_too_big);
   1312   1.1  christos 	  goto error_return;
   1313   1.1  christos 	}
   1314   1.1  christos       symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
   1315   1.1  christos       if (symbase == (elf_symbol_type *) NULL)
   1316   1.1  christos 	goto error_return;
   1317   1.1  christos 
   1318   1.1  christos       /* Read the raw ELF version symbol information.  */
   1319   1.7  christos       if (verhdr != NULL
   1320   1.7  christos 	  && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
   1321   1.8  christos 	{
   1322   1.8  christos 	  _bfd_error_handler
   1323   1.7  christos 	    /* xgettext:c-format */
   1324   1.8  christos 	    (_("%pB: version count (%" PRId64 ")"
   1325   1.1  christos 	       " does not match symbol count (%ld)"),
   1326   1.1  christos 	     abfd,
   1327   1.1  christos 	     (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
   1328   1.1  christos 	     symcount);
   1329   1.1  christos 
   1330   1.1  christos 	  /* Slurp in the symbols without the version information,
   1331   1.1  christos 	     since that is more helpful than just quitting.  */
   1332   1.1  christos 	  verhdr = NULL;
   1333   1.1  christos 	}
   1334   1.1  christos 
   1335   1.1  christos       if (verhdr != NULL)
   1336   1.9  christos 	{
   1337   1.9  christos 	  if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
   1338   1.1  christos 	    goto error_return;
   1339   1.1  christos 	  xverbuf = (Elf_External_Versym *)
   1340   1.1  christos 	    _bfd_malloc_and_read (abfd, verhdr->sh_size, verhdr->sh_size);
   1341   1.1  christos 	  if (xverbuf == NULL && verhdr->sh_size != 0)
   1342   1.1  christos 	    goto error_return;
   1343   1.1  christos 	}
   1344   1.1  christos 
   1345   1.1  christos       /* Skip first symbol, which is a null dummy.  */
   1346   1.1  christos       xver = xverbuf;
   1347  1.11  christos       if (xver != NULL)
   1348  1.11  christos 	++xver;
   1349  1.11  christos       isymend = isymbuf + symcount;
   1350   1.1  christos       for (isym = isymbuf + 1, sym = symbase, i = 1;
   1351   1.1  christos 	   isym < isymend;
   1352   1.3  christos 	   isym++, sym++, i++)
   1353   1.1  christos 	{
   1354  1.11  christos 	  memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
   1355  1.11  christos 
   1356  1.11  christos 	  sym->symbol.the_bfd = abfd;
   1357  1.11  christos 	  if (elf_use_dt_symtab_p (abfd))
   1358  1.11  christos 	    sym->symbol.name = (elf_tdata (abfd)->dt_strtab
   1359   1.1  christos 				+ isym->st_name);
   1360   1.1  christos 	  else
   1361   1.1  christos 	    sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
   1362   1.1  christos 	  sym->symbol.value = isym->st_value;
   1363   1.1  christos 
   1364   1.1  christos 	  if (isym->st_shndx == SHN_UNDEF)
   1365   1.1  christos 	    {
   1366   1.1  christos 	      sym->symbol.section = bfd_und_section_ptr;
   1367   1.1  christos 	    }
   1368   1.1  christos 	  else if (isym->st_shndx == SHN_ABS)
   1369   1.1  christos 	    {
   1370   1.1  christos 	      sym->symbol.section = bfd_abs_section_ptr;
   1371   1.1  christos 	    }
   1372   1.1  christos 	  else if (isym->st_shndx == SHN_COMMON)
   1373   1.1  christos 	    {
   1374   1.1  christos 	      sym->symbol.section = bfd_com_section_ptr;
   1375   1.1  christos 	      if ((abfd->flags & BFD_PLUGIN) != 0)
   1376   1.1  christos 		{
   1377   1.1  christos 		  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   1378   1.1  christos 
   1379   1.1  christos 		  if (xc == NULL)
   1380   1.1  christos 		    {
   1381   1.1  christos 		      flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   1382   1.1  christos 					| SEC_EXCLUDE);
   1383   1.1  christos 		      xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
   1384   1.1  christos 		      if (xc == NULL)
   1385   1.1  christos 			goto error_return;
   1386   1.1  christos 		    }
   1387   1.1  christos 		  sym->symbol.section = xc;
   1388   1.1  christos 		}
   1389   1.1  christos 	      /* Elf puts the alignment into the `value' field, and
   1390   1.1  christos 		 the size into the `size' field.  BFD wants to see the
   1391   1.1  christos 		 size in the value field, and doesn't care (at the
   1392  1.11  christos 		 moment) about the alignment.  */
   1393  1.11  christos 	      sym->symbol.value = isym->st_size;
   1394  1.11  christos 	    }
   1395  1.11  christos 	  else if (elf_use_dt_symtab_p (abfd))
   1396  1.11  christos 	    {
   1397  1.11  christos 	      asection *sec;
   1398  1.11  christos 	      sec = _bfd_elf_get_section_from_dynamic_symbol (abfd,
   1399  1.11  christos 							      isym);
   1400  1.11  christos 	      if (sec == NULL)
   1401   1.1  christos 		goto error_return;
   1402   1.1  christos 	      sym->symbol.section = sec;
   1403   1.1  christos 	    }
   1404   1.1  christos 	  else
   1405   1.1  christos 	    {
   1406   1.1  christos 	      sym->symbol.section
   1407   1.1  christos 		= bfd_section_from_elf_index (abfd, isym->st_shndx);
   1408   1.1  christos 	      if (sym->symbol.section == NULL)
   1409   1.9  christos 		{
   1410   1.9  christos 		  /* This symbol is in a section for which we did not
   1411   1.9  christos 		     create a BFD section.  Just use bfd_abs_section,
   1412   1.9  christos 		     although it is wrong.  FIXME.  Note - there is
   1413   1.1  christos 		     code in elf.c:swap_out_syms that calls
   1414   1.1  christos 		     symbol_section_index() in the elf backend for
   1415   1.1  christos 		     cases like this.  */
   1416   1.1  christos 		  sym->symbol.section = bfd_abs_section_ptr;
   1417   1.1  christos 		}
   1418   1.1  christos 	    }
   1419   1.1  christos 
   1420   1.1  christos 	  /* If this is a relocatable file, then the symbol value is
   1421   1.1  christos 	     already section relative.  */
   1422   1.1  christos 	  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   1423   1.1  christos 	    sym->symbol.value -= sym->symbol.section->vma;
   1424   1.1  christos 
   1425   1.1  christos 	  switch (ELF_ST_BIND (isym->st_info))
   1426   1.1  christos 	    {
   1427   1.1  christos 	    case STB_LOCAL:
   1428   1.1  christos 	      sym->symbol.flags |= BSF_LOCAL;
   1429   1.1  christos 	      break;
   1430   1.1  christos 	    case STB_GLOBAL:
   1431   1.1  christos 	      if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
   1432   1.1  christos 		sym->symbol.flags |= BSF_GLOBAL;
   1433   1.1  christos 	      break;
   1434   1.1  christos 	    case STB_WEAK:
   1435   1.1  christos 	      sym->symbol.flags |= BSF_WEAK;
   1436   1.1  christos 	      break;
   1437   1.1  christos 	    case STB_GNU_UNIQUE:
   1438   1.1  christos 	      sym->symbol.flags |= BSF_GNU_UNIQUE;
   1439   1.1  christos 	      break;
   1440   1.1  christos 	    }
   1441   1.1  christos 
   1442  1.10  christos 	  switch (ELF_ST_TYPE (isym->st_info))
   1443  1.10  christos 	    {
   1444  1.10  christos 	    case STT_SECTION:
   1445  1.10  christos 	      /* Mark the input section symbol as used since it may be
   1446  1.10  christos 	         used for relocation and section group.
   1447  1.10  christos 		 NB: BSF_SECTION_SYM_USED is ignored by linker and may
   1448  1.10  christos 		 be cleared by objcopy for non-relocatable inputs.  */
   1449   1.1  christos 	      sym->symbol.flags |= (BSF_SECTION_SYM
   1450   1.1  christos 				    | BSF_DEBUGGING
   1451   1.1  christos 				    | BSF_SECTION_SYM_USED);
   1452   1.1  christos 	      break;
   1453   1.1  christos 	    case STT_FILE:
   1454   1.1  christos 	      sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
   1455   1.1  christos 	      break;
   1456   1.1  christos 	    case STT_FUNC:
   1457   1.1  christos 	      sym->symbol.flags |= BSF_FUNCTION;
   1458   1.1  christos 	      break;
   1459   1.6  christos 	    case STT_COMMON:
   1460   1.1  christos 	      /* FIXME: Do we have to put the size field into the value field
   1461   1.1  christos 		 as we do with symbols in SHN_COMMON sections (see above) ?  */
   1462   1.1  christos 	      sym->symbol.flags |= BSF_ELF_COMMON;
   1463   1.1  christos 	      /* Fall through.  */
   1464   1.1  christos 	    case STT_OBJECT:
   1465   1.1  christos 	      sym->symbol.flags |= BSF_OBJECT;
   1466   1.1  christos 	      break;
   1467   1.1  christos 	    case STT_TLS:
   1468   1.1  christos 	      sym->symbol.flags |= BSF_THREAD_LOCAL;
   1469   1.1  christos 	      break;
   1470   1.1  christos 	    case STT_RELC:
   1471   1.1  christos 	      sym->symbol.flags |= BSF_RELC;
   1472   1.1  christos 	      break;
   1473   1.1  christos 	    case STT_SRELC:
   1474   1.1  christos 	      sym->symbol.flags |= BSF_SRELC;
   1475   1.1  christos 	      break;
   1476   1.1  christos 	    case STT_GNU_IFUNC:
   1477   1.1  christos 	      sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
   1478   1.1  christos 	      break;
   1479   1.1  christos 	    }
   1480   1.1  christos 
   1481  1.11  christos 	  if (dynamic)
   1482  1.11  christos 	    sym->symbol.flags |= BSF_DYNAMIC;
   1483  1.11  christos 
   1484  1.11  christos 	  if (elf_tdata (abfd)->dt_versym)
   1485   1.1  christos 	    sym->version = bfd_get_16 (abfd,
   1486   1.1  christos 				       elf_tdata (abfd)->dt_versym + 2 * i);
   1487   1.1  christos 	  else if (xver != NULL)
   1488   1.1  christos 	    {
   1489   1.1  christos 	      Elf_Internal_Versym iversym;
   1490   1.1  christos 
   1491   1.1  christos 	      _bfd_elf_swap_versym_in (abfd, xver, &iversym);
   1492   1.1  christos 	      sym->version = iversym.vs_vers;
   1493   1.1  christos 	      xver++;
   1494   1.1  christos 	    }
   1495   1.1  christos 
   1496   1.1  christos 	  /* Do some backend-specific processing on this symbol.  */
   1497   1.1  christos 	  if (ebd->elf_backend_symbol_processing)
   1498   1.1  christos 	    (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
   1499   1.1  christos 	}
   1500   1.1  christos     }
   1501   1.1  christos 
   1502   1.1  christos   /* Do some backend-specific processing on this symbol table.  */
   1503   1.1  christos   if (ebd->elf_backend_symbol_table_processing)
   1504   1.1  christos     (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
   1505   1.1  christos 
   1506   1.1  christos   /* We rely on the zalloc to clear out the final symbol entry.  */
   1507   1.1  christos 
   1508   1.1  christos   symcount = sym - symbase;
   1509   1.1  christos 
   1510   1.1  christos   /* Fill in the user's symbol pointer vector if needed.  */
   1511   1.1  christos   if (symptrs)
   1512   1.1  christos     {
   1513   1.1  christos       long l = symcount;
   1514   1.1  christos 
   1515   1.1  christos       sym = symbase;
   1516   1.1  christos       while (l-- > 0)
   1517   1.1  christos 	{
   1518   1.1  christos 	  *symptrs++ = &sym->symbol;
   1519   1.1  christos 	  sym++;
   1520   1.1  christos 	}
   1521   1.9  christos       *symptrs = 0;		/* Final null pointer */
   1522  1.11  christos     }
   1523  1.11  christos 
   1524   1.1  christos   free (xverbuf);
   1525   1.1  christos   if (hdr->contents != (unsigned char *) isymbuf
   1526   1.1  christos       && !elf_use_dt_symtab_p (abfd))
   1527   1.9  christos     free (isymbuf);
   1528   1.9  christos   return symcount;
   1529  1.11  christos 
   1530  1.11  christos  error_return:
   1531   1.1  christos   free (xverbuf);
   1532   1.1  christos   if (hdr->contents != (unsigned char *) isymbuf
   1533   1.1  christos       && !elf_use_dt_symtab_p (abfd))
   1534   1.1  christos     free (isymbuf);
   1535   1.1  christos   return -1;
   1536   1.1  christos }
   1537   1.1  christos 
   1538  1.10  christos /* Read relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
   1539   1.1  christos    them.  */
   1540   1.1  christos 
   1541   1.1  christos static bool
   1542   1.1  christos elf_slurp_reloc_table_from_section (bfd *abfd,
   1543   1.1  christos 				    asection *asect,
   1544   1.1  christos 				    Elf_Internal_Shdr *rel_hdr,
   1545  1.10  christos 				    bfd_size_type reloc_count,
   1546   1.1  christos 				    arelent *relents,
   1547   1.1  christos 				    asymbol **symbols,
   1548   1.1  christos 				    bool dynamic)
   1549   1.1  christos {
   1550   1.1  christos   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   1551   1.1  christos   void *allocated = NULL;
   1552   1.1  christos   bfd_byte *native_relocs;
   1553   1.1  christos   arelent *relent;
   1554   1.1  christos   unsigned int i;
   1555   1.9  christos   int entsize;
   1556  1.10  christos   unsigned int symcount;
   1557   1.9  christos 
   1558   1.1  christos   if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0)
   1559  1.10  christos     return false;
   1560   1.1  christos   allocated = _bfd_malloc_and_read (abfd, rel_hdr->sh_size, rel_hdr->sh_size);
   1561   1.1  christos   if (allocated == NULL)
   1562   1.1  christos     return false;
   1563   1.1  christos 
   1564   1.1  christos   native_relocs = (bfd_byte *) allocated;
   1565   1.1  christos 
   1566   1.1  christos   entsize = rel_hdr->sh_entsize;
   1567   1.1  christos   BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
   1568   1.1  christos 	      || entsize == sizeof (Elf_External_Rela));
   1569   1.1  christos 
   1570   1.1  christos   if (dynamic)
   1571   1.1  christos     symcount = bfd_get_dynamic_symcount (abfd);
   1572   1.1  christos   else
   1573   1.1  christos     symcount = bfd_get_symcount (abfd);
   1574   1.1  christos 
   1575   1.1  christos   for (i = 0, relent = relents;
   1576  1.10  christos        i < reloc_count;
   1577   1.1  christos        i++, relent++, native_relocs += entsize)
   1578   1.1  christos     {
   1579   1.1  christos       bool res;
   1580   1.1  christos       Elf_Internal_Rela rela;
   1581   1.1  christos 
   1582   1.1  christos       if (entsize == sizeof (Elf_External_Rela))
   1583   1.1  christos 	elf_swap_reloca_in (abfd, native_relocs, &rela);
   1584   1.1  christos       else
   1585   1.1  christos 	elf_swap_reloc_in (abfd, native_relocs, &rela);
   1586   1.1  christos 
   1587   1.1  christos       /* The address of an ELF reloc is section relative for an object
   1588   1.1  christos 	 file, and absolute for an executable file or shared library.
   1589   1.1  christos 	 The address of a normal BFD reloc is always section relative,
   1590   1.1  christos 	 and the address of a dynamic reloc is absolute..  */
   1591   1.1  christos       if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
   1592   1.1  christos 	relent->address = rela.r_offset;
   1593   1.1  christos       else
   1594   1.8  christos 	relent->address = rela.r_offset - asect->vma;
   1595   1.8  christos 
   1596  1.12  christos       if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
   1597   1.1  christos 	/* FIXME: This and the error case below mean that we have a
   1598   1.1  christos 	   symbol on relocs that is not elf_symbol_type.  */
   1599   1.7  christos 	relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   1600   1.7  christos       else if (ELF_R_SYM (rela.r_info) > symcount)
   1601   1.8  christos 	{
   1602   1.8  christos 	  _bfd_error_handler
   1603   1.8  christos 	    /* xgettext:c-format */
   1604  1.12  christos 	    (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
   1605   1.1  christos 	     abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
   1606   1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   1607   1.1  christos 	  relent->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
   1608   1.1  christos 	}
   1609   1.1  christos       else
   1610   1.1  christos 	{
   1611   1.1  christos 	  asymbol **ps;
   1612   1.1  christos 
   1613   1.1  christos 	  ps = symbols + ELF_R_SYM (rela.r_info) - 1;
   1614   1.1  christos 
   1615   1.1  christos 	  relent->sym_ptr_ptr = ps;
   1616   1.1  christos 	}
   1617  1.12  christos 
   1618   1.1  christos       relent->addend = rela.r_addend;
   1619  1.12  christos 
   1620  1.12  christos       res = false;
   1621   1.8  christos       if ((entsize == sizeof (Elf_External_Rela)
   1622  1.12  christos 	   || ebd->elf_info_to_howto_rel == NULL)
   1623   1.8  christos 	  && ebd->elf_info_to_howto != NULL)
   1624   1.8  christos 	res = ebd->elf_info_to_howto (abfd, relent, &rela);
   1625   1.8  christos       else if (ebd->elf_info_to_howto_rel != NULL)
   1626   1.8  christos 	res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
   1627   1.1  christos 
   1628   1.1  christos       if (! res || relent->howto == NULL)
   1629   1.9  christos 	goto error_return;
   1630  1.10  christos     }
   1631   1.1  christos 
   1632   1.1  christos   free (allocated);
   1633   1.9  christos   return true;
   1634  1.10  christos 
   1635   1.1  christos  error_return:
   1636   1.1  christos   free (allocated);
   1637   1.1  christos   return false;
   1638   1.1  christos }
   1639  1.10  christos 
   1640   1.1  christos /* Read in and swap the external relocs.  */
   1641   1.1  christos 
   1642   1.1  christos bool
   1643  1.10  christos elf_slurp_reloc_table (bfd *abfd,
   1644   1.1  christos 		       asection *asect,
   1645   1.9  christos 		       asymbol **symbols,
   1646   1.1  christos 		       bool dynamic)
   1647   1.1  christos {
   1648   1.1  christos   const struct elf_backend_data * const bed = get_elf_backend_data (abfd);
   1649   1.1  christos   struct bfd_elf_section_data * const d = elf_section_data (asect);
   1650   1.1  christos   Elf_Internal_Shdr *rel_hdr;
   1651   1.1  christos   Elf_Internal_Shdr *rel_hdr2;
   1652   1.9  christos   bfd_size_type reloc_count;
   1653   1.1  christos   bfd_size_type reloc_count2;
   1654   1.1  christos   arelent *relents;
   1655  1.10  christos   size_t amt;
   1656   1.1  christos 
   1657   1.1  christos   if (asect->relocation != NULL)
   1658   1.1  christos     return true;
   1659   1.1  christos 
   1660   1.1  christos   if (! dynamic)
   1661  1.10  christos     {
   1662   1.1  christos       if ((asect->flags & SEC_RELOC) == 0
   1663   1.1  christos 	  || asect->reloc_count == 0)
   1664   1.1  christos 	return true;
   1665   1.1  christos 
   1666   1.1  christos       rel_hdr = d->rel.hdr;
   1667   1.1  christos       reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
   1668   1.3  christos       rel_hdr2 = d->rela.hdr;
   1669   1.3  christos       reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
   1670  1.10  christos 
   1671   1.1  christos       /* PR 17512: file: 0b4f81b7.  */
   1672   1.1  christos       if (asect->reloc_count != reloc_count + reloc_count2)
   1673   1.1  christos 	return false;
   1674   1.1  christos       BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
   1675   1.1  christos 		  || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
   1676   1.1  christos 
   1677   1.1  christos     }
   1678   1.1  christos   else
   1679   1.1  christos     {
   1680   1.1  christos       /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
   1681   1.1  christos 	 case because relocations against this section may use the
   1682  1.10  christos 	 dynamic symbol table, and in that case bfd_section_from_shdr
   1683   1.1  christos 	 in elf.c does not update the RELOC_COUNT.  */
   1684   1.1  christos       if (asect->size == 0)
   1685   1.1  christos 	return true;
   1686   1.1  christos 
   1687   1.1  christos       rel_hdr = &d->this_hdr;
   1688   1.1  christos       reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
   1689   1.1  christos       rel_hdr2 = NULL;
   1690   1.9  christos       reloc_count2 = 0;
   1691   1.9  christos     }
   1692   1.9  christos 
   1693  1.10  christos   if (_bfd_mul_overflow (reloc_count + reloc_count2, sizeof (arelent), &amt))
   1694   1.9  christos     {
   1695   1.1  christos       bfd_set_error (bfd_error_file_too_big);
   1696   1.1  christos       return false;
   1697  1.10  christos     }
   1698   1.1  christos   relents = (arelent *) bfd_alloc (abfd, amt);
   1699   1.1  christos   if (relents == NULL)
   1700   1.1  christos     return false;
   1701   1.1  christos 
   1702   1.1  christos   if (rel_hdr
   1703   1.1  christos       && !elf_slurp_reloc_table_from_section (abfd, asect,
   1704  1.10  christos 					      rel_hdr, reloc_count,
   1705   1.1  christos 					      relents,
   1706   1.1  christos 					      symbols, dynamic))
   1707   1.1  christos     return false;
   1708   1.1  christos 
   1709   1.1  christos   if (rel_hdr2
   1710   1.1  christos       && !elf_slurp_reloc_table_from_section (abfd, asect,
   1711  1.10  christos 					      rel_hdr2, reloc_count2,
   1712   1.1  christos 					      relents + reloc_count,
   1713  1.10  christos 					      symbols, dynamic))
   1714  1.10  christos     return false;
   1715   1.9  christos 
   1716   1.1  christos   if (!bed->slurp_secondary_relocs (abfd, asect, symbols, dynamic))
   1717  1.10  christos     return false;
   1718   1.1  christos 
   1719   1.1  christos   asect->relocation = relents;
   1720   1.1  christos   return true;
   1721   1.1  christos }
   1722   1.1  christos 
   1723   1.1  christos #if DEBUG & 2
   1724   1.1  christos static void
   1725   1.1  christos elf_debug_section (int num, Elf_Internal_Shdr *hdr)
   1726   1.1  christos {
   1727   1.1  christos   fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
   1728   1.1  christos 	   hdr->bfd_section != NULL ? hdr->bfd_section->name : "",
   1729   1.1  christos 	   (long) hdr);
   1730   1.1  christos   fprintf (stderr,
   1731   1.1  christos 	   "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
   1732   1.1  christos 	   (long) hdr->sh_name,
   1733   1.1  christos 	   (long) hdr->sh_type,
   1734   1.1  christos 	   (long) hdr->sh_flags);
   1735   1.1  christos   fprintf (stderr,
   1736   1.1  christos 	   "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
   1737   1.1  christos 	   (long) hdr->sh_addr,
   1738   1.1  christos 	   (long) hdr->sh_offset,
   1739   1.1  christos 	   (long) hdr->sh_size);
   1740   1.1  christos   fprintf (stderr,
   1741   1.1  christos 	   "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
   1742   1.1  christos 	   (long) hdr->sh_link,
   1743   1.1  christos 	   (long) hdr->sh_info,
   1744   1.1  christos 	   (long) hdr->sh_addralign);
   1745   1.1  christos   fprintf (stderr, "sh_entsize   = %ld\n",
   1746   1.1  christos 	   (long) hdr->sh_entsize);
   1747   1.1  christos   fflush (stderr);
   1748   1.1  christos }
   1749   1.1  christos #endif
   1750   1.1  christos 
   1751   1.1  christos #if DEBUG & 1
   1752   1.1  christos static void
   1753   1.1  christos elf_debug_file (Elf_Internal_Ehdr *ehdrp)
   1754   1.1  christos {
   1755   1.1  christos   fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
   1756   1.1  christos   fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
   1757   1.1  christos   fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
   1758   1.1  christos   fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
   1759   1.1  christos   fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
   1760   1.1  christos   fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
   1761   1.1  christos   fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
   1762   1.1  christos }
   1763   1.3  christos #endif
   1764   1.3  christos 
   1765   1.3  christos /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   1767   1.3  christos    reconstruct an ELF file by reading the segments out of remote
   1768   1.3  christos    memory based on the ELF file header at EHDR_VMA and the ELF program
   1769   1.3  christos    headers it points to.  If non-zero, SIZE is the known extent of the
   1770   1.3  christos    object.  If not null, *LOADBASEP is filled in with the difference
   1771   1.3  christos    between the VMAs from which the segments were read, and the VMAs
   1772   1.3  christos    the file headers (and hence BFD's idea of each section's VMA) put
   1773   1.3  christos    them at.
   1774   1.3  christos 
   1775   1.3  christos    The function TARGET_READ_MEMORY is called to copy LEN bytes from
   1776   1.1  christos    the remote memory at target address VMA into the local buffer at
   1777   1.1  christos    MYADDR; it should return zero on success or an `errno' code on
   1778   1.1  christos    failure.  TEMPL must be a BFD for a target with the word size and
   1779   1.1  christos    byte order found in the remote memory.  */
   1780   1.9  christos 
   1781   1.9  christos bfd *
   1782   1.9  christos NAME(_bfd_elf,bfd_from_remote_memory)
   1783   1.1  christos   (bfd *templ,
   1784   1.9  christos    bfd_vma ehdr_vma    /* Bytes.  */,
   1785   1.1  christos    bfd_size_type size  /* Octets.  */,
   1786   1.1  christos    bfd_vma *loadbasep  /* Bytes.  */,
   1787   1.1  christos    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   1788   1.1  christos                           /* (Bytes  ,           , octets       ).  */
   1789   1.3  christos {
   1790   1.1  christos   Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
   1791   1.1  christos   Elf_Internal_Ehdr i_ehdr;	/* Elf file header, internal form */
   1792   1.1  christos   Elf_External_Phdr *x_phdrs;
   1793   1.1  christos   Elf_Internal_Phdr *i_phdrs, *last_phdr, *first_phdr;
   1794   1.1  christos   bfd *nbfd;
   1795   1.3  christos   struct bfd_in_memory *bim;
   1796   1.3  christos   bfd_byte *contents;
   1797   1.9  christos   int err;
   1798   1.9  christos   unsigned int i;
   1799   1.9  christos   bfd_vma high_offset;
   1800   1.1  christos   bfd_vma shdr_end;
   1801   1.1  christos   bfd_vma loadbase;  /* Bytes.  */
   1802   1.1  christos   size_t amt;
   1803   1.1  christos   unsigned int opb = bfd_octets_per_byte (templ, NULL);
   1804   1.1  christos 
   1805   1.1  christos   /* Read in the ELF header in external format.  */
   1806   1.1  christos   err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr);
   1807   1.1  christos   if (err)
   1808   1.1  christos     {
   1809   1.1  christos       bfd_set_error (bfd_error_system_call);
   1810   1.1  christos       errno = err;
   1811   1.1  christos       return NULL;
   1812   1.1  christos     }
   1813   1.1  christos 
   1814   1.1  christos   /* Now check to see if we have a valid ELF file, and one that BFD can
   1815   1.1  christos      make use of.  The magic number must match, the address size ('class')
   1816   1.1  christos      and byte-swapping must match our XVEC entry.  */
   1817   1.1  christos 
   1818   1.1  christos   if (! elf_file_p (&x_ehdr)
   1819   1.1  christos       || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
   1820   1.1  christos       || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
   1821   1.1  christos     {
   1822   1.1  christos       bfd_set_error (bfd_error_wrong_format);
   1823   1.1  christos       return NULL;
   1824   1.1  christos     }
   1825   1.1  christos 
   1826   1.1  christos   /* Check that file's byte order matches xvec's */
   1827   1.1  christos   switch (x_ehdr.e_ident[EI_DATA])
   1828   1.1  christos     {
   1829   1.1  christos     case ELFDATA2MSB:		/* Big-endian */
   1830   1.1  christos       if (! bfd_header_big_endian (templ))
   1831   1.1  christos 	{
   1832   1.1  christos 	  bfd_set_error (bfd_error_wrong_format);
   1833   1.1  christos 	  return NULL;
   1834   1.1  christos 	}
   1835   1.1  christos       break;
   1836   1.1  christos     case ELFDATA2LSB:		/* Little-endian */
   1837   1.1  christos       if (! bfd_header_little_endian (templ))
   1838   1.1  christos 	{
   1839   1.1  christos 	  bfd_set_error (bfd_error_wrong_format);
   1840   1.1  christos 	  return NULL;
   1841   1.1  christos 	}
   1842   1.1  christos       break;
   1843   1.1  christos     case ELFDATANONE:		/* No data encoding specified */
   1844   1.1  christos     default:			/* Unknown data encoding specified */
   1845   1.1  christos       bfd_set_error (bfd_error_wrong_format);
   1846   1.1  christos       return NULL;
   1847   1.1  christos     }
   1848   1.1  christos 
   1849   1.1  christos   elf_swap_ehdr_in (templ, &x_ehdr, &i_ehdr);
   1850   1.1  christos 
   1851   1.1  christos   /* The file header tells where to find the program headers.
   1852   1.1  christos      These are what we use to actually choose what to read.  */
   1853   1.1  christos 
   1854   1.1  christos   if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
   1855   1.1  christos     {
   1856   1.9  christos       bfd_set_error (bfd_error_wrong_format);
   1857   1.9  christos       return NULL;
   1858   1.9  christos     }
   1859   1.9  christos 
   1860   1.9  christos   if (_bfd_mul_overflow (i_ehdr.e_phnum,
   1861   1.9  christos 			 sizeof (*x_phdrs) + sizeof (*i_phdrs), &amt))
   1862   1.9  christos     {
   1863   1.1  christos       bfd_set_error (bfd_error_file_too_big);
   1864   1.3  christos       return NULL;
   1865   1.1  christos     }
   1866   1.1  christos   x_phdrs = (Elf_External_Phdr *) bfd_malloc (amt);
   1867   1.1  christos   if (x_phdrs == NULL)
   1868   1.1  christos     return NULL;
   1869   1.1  christos   err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs,
   1870   1.1  christos 			    i_ehdr.e_phnum * sizeof x_phdrs[0]);
   1871   1.1  christos   if (err)
   1872   1.1  christos     {
   1873   1.1  christos       free (x_phdrs);
   1874   1.1  christos       bfd_set_error (bfd_error_system_call);
   1875   1.1  christos       errno = err;
   1876   1.3  christos       return NULL;
   1877   1.3  christos     }
   1878   1.3  christos   i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum];
   1879   1.1  christos 
   1880   1.1  christos   high_offset = 0;
   1881   1.1  christos   loadbase = 0;
   1882   1.1  christos   first_phdr = NULL;
   1883   1.1  christos   last_phdr = NULL;
   1884   1.1  christos   for (i = 0; i < i_ehdr.e_phnum; ++i)
   1885   1.3  christos     {
   1886   1.3  christos       elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]);
   1887   1.3  christos       if (i_phdrs[i].p_type == PT_LOAD)
   1888   1.1  christos 	{
   1889   1.3  christos 	  bfd_vma segment_end = i_phdrs[i].p_offset + i_phdrs[i].p_filesz;
   1890   1.3  christos 
   1891   1.1  christos 	  if (segment_end > high_offset)
   1892   1.1  christos 	    {
   1893   1.3  christos 	      high_offset = segment_end;
   1894   1.3  christos 	      last_phdr = &i_phdrs[i];
   1895   1.3  christos 	    }
   1896   1.3  christos 
   1897   1.9  christos 	  /* If this program header covers offset zero, where the file
   1898   1.9  christos 	     header sits, then we can figure out the loadbase.  */
   1899   1.3  christos 	  if (first_phdr == NULL)
   1900   1.3  christos 	    {
   1901   1.3  christos 	      bfd_vma p_offset = i_phdrs[i].p_offset;  /* Octets.  */
   1902   1.9  christos 	      bfd_vma p_vaddr = i_phdrs[i].p_vaddr;    /* Octets.  */
   1903   1.9  christos 
   1904   1.3  christos 	      if (i_phdrs[i].p_align > 1)
   1905   1.3  christos 		{
   1906   1.3  christos 		  p_offset &= -(i_phdrs[i].p_align * opb);
   1907   1.9  christos 		  p_vaddr &= -(i_phdrs[i].p_align * opb);
   1908   1.3  christos 		}
   1909   1.3  christos 	      if (p_offset == 0)
   1910   1.3  christos 		{
   1911   1.1  christos 		  loadbase = ehdr_vma - p_vaddr / opb;
   1912   1.1  christos 		  first_phdr = &i_phdrs[i];
   1913   1.3  christos 		}
   1914   1.1  christos 	    }
   1915   1.1  christos 	}
   1916   1.1  christos     }
   1917   1.1  christos   if (high_offset == 0)
   1918   1.1  christos     {
   1919   1.1  christos       /* There were no PT_LOAD segments, so we don't have anything to read.  */
   1920   1.1  christos       free (x_phdrs);
   1921   1.3  christos       bfd_set_error (bfd_error_wrong_format);
   1922   1.3  christos       return NULL;
   1923   1.3  christos     }
   1924   1.3  christos 
   1925   1.3  christos   shdr_end = 0;
   1926   1.3  christos   if (i_ehdr.e_shoff != 0 && i_ehdr.e_shnum != 0 && i_ehdr.e_shentsize != 0)
   1927   1.3  christos     {
   1928   1.3  christos       shdr_end = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
   1929   1.3  christos 
   1930   1.3  christos       if (last_phdr->p_filesz != last_phdr->p_memsz)
   1931   1.3  christos 	{
   1932   1.3  christos 	  /* If the last PT_LOAD header has a bss area then ld.so will
   1933   1.3  christos 	     have cleared anything past p_filesz, zapping the section
   1934   1.3  christos 	     headers.  */
   1935   1.3  christos 	}
   1936   1.3  christos       else if (size >= shdr_end)
   1937   1.3  christos 	high_offset = size;
   1938   1.3  christos       else
   1939   1.3  christos 	{
   1940   1.3  christos 	  bfd_vma page_size = get_elf_backend_data (templ)->minpagesize;
   1941   1.3  christos 	  bfd_vma segment_end = last_phdr->p_offset + last_phdr->p_filesz;
   1942   1.3  christos 
   1943   1.3  christos 	  /* Assume we loaded full pages, allowing us to sometimes see
   1944   1.3  christos 	     section headers.  */
   1945   1.3  christos 	  if (page_size > 1 && shdr_end > segment_end)
   1946   1.3  christos 	    {
   1947   1.3  christos 	      bfd_vma page_end = (segment_end + page_size - 1) & -page_size;
   1948   1.3  christos 
   1949   1.3  christos 	      if (page_end >= shdr_end)
   1950   1.1  christos 		/* Whee, section headers covered.  */
   1951   1.1  christos 		high_offset = shdr_end;
   1952   1.1  christos 	    }
   1953   1.3  christos 	}
   1954   1.1  christos     }
   1955   1.1  christos 
   1956   1.1  christos   /* Now we know the size of the whole image we want read in.  */
   1957   1.1  christos   contents = (bfd_byte *) bfd_zmalloc (high_offset);
   1958   1.1  christos   if (contents == NULL)
   1959   1.1  christos     {
   1960   1.1  christos       free (x_phdrs);
   1961   1.1  christos       return NULL;
   1962   1.1  christos     }
   1963   1.9  christos 
   1964   1.9  christos   for (i = 0; i < i_ehdr.e_phnum; ++i)
   1965   1.9  christos     if (i_phdrs[i].p_type == PT_LOAD)
   1966   1.3  christos       {
   1967   1.3  christos 	bfd_vma start = i_phdrs[i].p_offset;         /* Octets.  */
   1968   1.3  christos 	bfd_vma end = start + i_phdrs[i].p_filesz;   /* Octets.  */
   1969   1.3  christos 	bfd_vma vaddr = i_phdrs[i].p_vaddr;          /* Octets.  */
   1970   1.3  christos 
   1971   1.3  christos 	/* Extend the beginning of the first pt_load to cover file
   1972   1.3  christos 	   header and program headers, if we proved earlier that its
   1973   1.3  christos 	   aligned offset is 0.  */
   1974   1.3  christos 	if (first_phdr == &i_phdrs[i])
   1975   1.3  christos 	  {
   1976   1.3  christos 	    vaddr -= start;
   1977   1.3  christos 	    start = 0;
   1978   1.9  christos 	  }
   1979   1.1  christos 	/* Extend the end of the last pt_load to cover section headers.  */
   1980   1.1  christos 	if (last_phdr == &i_phdrs[i])
   1981   1.1  christos 	  end = high_offset;
   1982   1.1  christos 	err = target_read_memory (loadbase + vaddr / opb,
   1983   1.1  christos 				  contents + start, end - start);
   1984   1.1  christos 	if (err)
   1985   1.1  christos 	  {
   1986   1.1  christos 	    free (x_phdrs);
   1987   1.1  christos 	    free (contents);
   1988   1.1  christos 	    bfd_set_error (bfd_error_system_call);
   1989   1.1  christos 	    errno = err;
   1990   1.1  christos 	    return NULL;
   1991   1.1  christos 	  }
   1992   1.1  christos       }
   1993   1.3  christos   free (x_phdrs);
   1994   1.1  christos 
   1995   1.1  christos   /* If the segments visible in memory didn't include the section headers,
   1996   1.1  christos      then clear them from the file header.  */
   1997   1.1  christos   if (high_offset < shdr_end)
   1998   1.1  christos     {
   1999   1.1  christos       memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff);
   2000   1.1  christos       memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum);
   2001   1.1  christos       memset (&x_ehdr.e_shstrndx, 0, sizeof x_ehdr.e_shstrndx);
   2002   1.1  christos     }
   2003   1.1  christos 
   2004   1.1  christos   /* This will normally have been in the first PT_LOAD segment.  But it
   2005   1.1  christos      conceivably could be missing, and we might have just changed it.  */
   2006   1.1  christos   memcpy (contents, &x_ehdr, sizeof x_ehdr);
   2007   1.1  christos 
   2008   1.1  christos   /* Now we have a memory image of the ELF file contents.  Make a BFD.  */
   2009   1.1  christos   bim = (struct bfd_in_memory *) bfd_malloc (sizeof (struct bfd_in_memory));
   2010   1.1  christos   if (bim == NULL)
   2011   1.1  christos     {
   2012   1.9  christos       free (contents);
   2013   1.9  christos       return NULL;
   2014   1.1  christos     }
   2015   1.1  christos   nbfd = _bfd_new_bfd ();
   2016   1.1  christos   if (nbfd == NULL
   2017   1.1  christos       || !bfd_set_filename (nbfd, "<in-memory>"))
   2018   1.1  christos     {
   2019   1.1  christos       free (bim);
   2020   1.3  christos       free (contents);
   2021   1.1  christos       return NULL;
   2022   1.1  christos     }
   2023   1.1  christos   nbfd->xvec = templ->xvec;
   2024   1.1  christos   bim->size = high_offset;
   2025   1.1  christos   bim->buffer = contents;
   2026   1.1  christos   nbfd->iostream = bim;
   2027   1.1  christos   nbfd->flags = BFD_IN_MEMORY;
   2028  1.10  christos   nbfd->iovec = &_bfd_memory_iovec;
   2029   1.1  christos   nbfd->origin = 0;
   2030   1.1  christos   nbfd->direction = read_direction;
   2031   1.1  christos   nbfd->mtime = time (NULL);
   2032   1.1  christos   nbfd->mtime_set = true;
   2033   1.1  christos 
   2034   1.1  christos   if (loadbasep)
   2035   1.1  christos     *loadbasep = loadbase;
   2036   1.1  christos   return nbfd;
   2037   1.1  christos }
   2038   1.1  christos 
   2039   1.1  christos /* Function for ELF_R_INFO.  */
   2040   1.1  christos 
   2041   1.1  christos bfd_vma
   2042   1.1  christos NAME(elf,r_info) (bfd_vma sym, bfd_vma type)
   2043   1.1  christos {
   2044   1.1  christos   return ELF_R_INFO (sym, type);
   2045   1.1  christos }
   2046   1.1  christos 
   2047   1.1  christos /* Function for ELF_R_SYM.  */
   2048   1.1  christos 
   2049   1.1  christos bfd_vma
   2050   1.1  christos NAME(elf,r_sym) (bfd_vma r_info)
   2051   1.1  christos {
   2052   1.1  christos   return ELF_R_SYM (r_info);
   2053   1.1  christos }
   2054   1.1  christos 
   2055   1.1  christos #include "elfcore.h"
   2057   1.1  christos 
   2058   1.1  christos /* Size-dependent data and functions.  */
   2060   1.1  christos const struct elf_size_info NAME(_bfd_elf,size_info) = {
   2061   1.1  christos   sizeof (Elf_External_Ehdr),
   2062   1.1  christos   sizeof (Elf_External_Phdr),
   2063   1.1  christos   sizeof (Elf_External_Shdr),
   2064   1.1  christos   sizeof (Elf_External_Rel),
   2065   1.1  christos   sizeof (Elf_External_Rela),
   2066   1.1  christos   sizeof (Elf_External_Sym),
   2067   1.1  christos   sizeof (Elf_External_Dyn),
   2068   1.1  christos   sizeof (Elf_External_Note),
   2069   1.1  christos   4,
   2070   1.1  christos   1,
   2071   1.1  christos   ARCH_SIZE, LOG_FILE_ALIGN,
   2072   1.1  christos   ELFCLASS, EV_CURRENT,
   2073   1.1  christos   elf_write_out_phdrs,
   2074   1.1  christos   elf_write_shdrs_and_ehdr,
   2075   1.1  christos   elf_checksum_contents,
   2076   1.1  christos   elf_write_relocs,
   2077   1.1  christos   elf_swap_symbol_in,
   2078   1.1  christos   elf_swap_symbol_out,
   2079   1.1  christos   elf_slurp_reloc_table,
   2080   1.1  christos   elf_slurp_symbol_table,
   2081   1.1  christos   elf_swap_dyn_in,
   2082                   elf_swap_dyn_out,
   2083                   elf_swap_reloc_in,
   2084                   elf_swap_reloc_out,
   2085                   elf_swap_reloca_in,
   2086                   elf_swap_reloca_out
   2087                 };
   2088