Home | History | Annotate | Line # | Download | only in bfd
elfcode.h revision 1.8
      1  1.1  christos /* ELF executable support for BFD.
      2  1.8  christos    Copyright (C) 1991-2019 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.1  christos 
    174  1.1  christos bfd_boolean
    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.1  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.1  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.1  christos   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    270  1.1  christos   memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
    271  1.1  christos   /* note that all elements of dst are *arrays of unsigned char* already...  */
    272  1.1  christos   H_PUT_16 (abfd, src->e_type, dst->e_type);
    273  1.1  christos   H_PUT_16 (abfd, src->e_machine, dst->e_machine);
    274  1.1  christos   H_PUT_32 (abfd, src->e_version, dst->e_version);
    275  1.1  christos   if (signed_vma)
    276  1.1  christos     H_PUT_SIGNED_WORD (abfd, src->e_entry, dst->e_entry);
    277  1.1  christos   else
    278  1.1  christos     H_PUT_WORD (abfd, src->e_entry, dst->e_entry);
    279  1.1  christos   H_PUT_WORD (abfd, src->e_phoff, dst->e_phoff);
    280  1.1  christos   H_PUT_WORD (abfd, src->e_shoff, dst->e_shoff);
    281  1.1  christos   H_PUT_32 (abfd, src->e_flags, dst->e_flags);
    282  1.1  christos   H_PUT_16 (abfd, src->e_ehsize, dst->e_ehsize);
    283  1.1  christos   H_PUT_16 (abfd, src->e_phentsize, dst->e_phentsize);
    284  1.1  christos   tmp = src->e_phnum;
    285  1.1  christos   if (tmp > PN_XNUM)
    286  1.1  christos     tmp = PN_XNUM;
    287  1.1  christos   H_PUT_16 (abfd, tmp, dst->e_phnum);
    288  1.1  christos   H_PUT_16 (abfd, src->e_shentsize, dst->e_shentsize);
    289  1.1  christos   tmp = src->e_shnum;
    290  1.1  christos   if (tmp >= (SHN_LORESERVE & 0xffff))
    291  1.1  christos     tmp = SHN_UNDEF;
    292  1.1  christos   H_PUT_16 (abfd, tmp, dst->e_shnum);
    293  1.1  christos   tmp = src->e_shstrndx;
    294  1.1  christos   if (tmp >= (SHN_LORESERVE & 0xffff))
    295  1.1  christos     tmp = SHN_XINDEX & 0xffff;
    296  1.1  christos   H_PUT_16 (abfd, tmp, dst->e_shstrndx);
    297  1.1  christos }
    298  1.1  christos 
    299  1.1  christos /* Translate an ELF section header table entry in external format into an
    300  1.1  christos    ELF section header table entry in internal format.  */
    301  1.1  christos 
    302  1.1  christos static void
    303  1.1  christos elf_swap_shdr_in (bfd *abfd,
    304  1.1  christos 		  const Elf_External_Shdr *src,
    305  1.1  christos 		  Elf_Internal_Shdr *dst)
    306  1.1  christos {
    307  1.1  christos   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    308  1.1  christos 
    309  1.1  christos   dst->sh_name = H_GET_32 (abfd, src->sh_name);
    310  1.1  christos   dst->sh_type = H_GET_32 (abfd, src->sh_type);
    311  1.1  christos   dst->sh_flags = H_GET_WORD (abfd, src->sh_flags);
    312  1.1  christos   if (signed_vma)
    313  1.1  christos     dst->sh_addr = H_GET_SIGNED_WORD (abfd, src->sh_addr);
    314  1.1  christos   else
    315  1.1  christos     dst->sh_addr = H_GET_WORD (abfd, src->sh_addr);
    316  1.1  christos   dst->sh_offset = H_GET_WORD (abfd, src->sh_offset);
    317  1.8  christos   dst->sh_size = H_GET_WORD (abfd, src->sh_size);
    318  1.8  christos   /* PR 23657.  Check for invalid section size, in sections with contents.
    319  1.8  christos      Note - we do not set an error value here because the contents
    320  1.8  christos      of this particular section might not be needed by the consumer.  */
    321  1.8  christos   if (dst->sh_type != SHT_NOBITS
    322  1.8  christos       && dst->sh_size > bfd_get_file_size (abfd))
    323  1.8  christos     _bfd_error_handler
    324  1.8  christos       (_("warning: %pB has a corrupt section with a size (%" BFD_VMA_FMT "x) larger than the file size"),
    325  1.1  christos        abfd, dst->sh_size);
    326  1.1  christos   dst->sh_link = H_GET_32 (abfd, src->sh_link);
    327  1.1  christos   dst->sh_info = H_GET_32 (abfd, src->sh_info);
    328  1.1  christos   dst->sh_addralign = H_GET_WORD (abfd, src->sh_addralign);
    329  1.1  christos   dst->sh_entsize = H_GET_WORD (abfd, src->sh_entsize);
    330  1.1  christos   dst->bfd_section = NULL;
    331  1.1  christos   dst->contents = NULL;
    332  1.1  christos }
    333  1.1  christos 
    334  1.1  christos /* Translate an ELF section header table entry in internal format into an
    335  1.1  christos    ELF section header table entry in external format.  */
    336  1.1  christos 
    337  1.1  christos static void
    338  1.1  christos elf_swap_shdr_out (bfd *abfd,
    339  1.1  christos 		   const Elf_Internal_Shdr *src,
    340  1.1  christos 		   Elf_External_Shdr *dst)
    341  1.1  christos {
    342  1.1  christos   /* note that all elements of dst are *arrays of unsigned char* already...  */
    343  1.1  christos   H_PUT_32 (abfd, src->sh_name, dst->sh_name);
    344  1.1  christos   H_PUT_32 (abfd, src->sh_type, dst->sh_type);
    345  1.1  christos   H_PUT_WORD (abfd, src->sh_flags, dst->sh_flags);
    346  1.1  christos   H_PUT_WORD (abfd, src->sh_addr, dst->sh_addr);
    347  1.1  christos   H_PUT_WORD (abfd, src->sh_offset, dst->sh_offset);
    348  1.1  christos   H_PUT_WORD (abfd, src->sh_size, dst->sh_size);
    349  1.1  christos   H_PUT_32 (abfd, src->sh_link, dst->sh_link);
    350  1.1  christos   H_PUT_32 (abfd, src->sh_info, dst->sh_info);
    351  1.1  christos   H_PUT_WORD (abfd, src->sh_addralign, dst->sh_addralign);
    352  1.1  christos   H_PUT_WORD (abfd, src->sh_entsize, dst->sh_entsize);
    353  1.1  christos }
    354  1.1  christos 
    355  1.1  christos /* Translate an ELF program header table entry in external format into an
    356  1.1  christos    ELF program header table entry in internal format.  */
    357  1.1  christos 
    358  1.1  christos void
    359  1.1  christos elf_swap_phdr_in (bfd *abfd,
    360  1.1  christos 		  const Elf_External_Phdr *src,
    361  1.1  christos 		  Elf_Internal_Phdr *dst)
    362  1.1  christos {
    363  1.1  christos   int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
    364  1.1  christos 
    365  1.1  christos   dst->p_type = H_GET_32 (abfd, src->p_type);
    366  1.1  christos   dst->p_flags = H_GET_32 (abfd, src->p_flags);
    367  1.1  christos   dst->p_offset = H_GET_WORD (abfd, src->p_offset);
    368  1.1  christos   if (signed_vma)
    369  1.1  christos     {
    370  1.1  christos       dst->p_vaddr = H_GET_SIGNED_WORD (abfd, src->p_vaddr);
    371  1.1  christos       dst->p_paddr = H_GET_SIGNED_WORD (abfd, src->p_paddr);
    372  1.1  christos     }
    373  1.1  christos   else
    374  1.1  christos     {
    375  1.1  christos       dst->p_vaddr = H_GET_WORD (abfd, src->p_vaddr);
    376  1.1  christos       dst->p_paddr = H_GET_WORD (abfd, src->p_paddr);
    377  1.1  christos     }
    378  1.1  christos   dst->p_filesz = H_GET_WORD (abfd, src->p_filesz);
    379  1.1  christos   dst->p_memsz = H_GET_WORD (abfd, src->p_memsz);
    380  1.1  christos   dst->p_align = H_GET_WORD (abfd, src->p_align);
    381  1.1  christos }
    382  1.1  christos 
    383  1.1  christos void
    384  1.1  christos elf_swap_phdr_out (bfd *abfd,
    385  1.1  christos 		   const Elf_Internal_Phdr *src,
    386  1.1  christos 		   Elf_External_Phdr *dst)
    387  1.1  christos {
    388  1.1  christos   const struct elf_backend_data *bed;
    389  1.1  christos   bfd_vma p_paddr;
    390  1.1  christos 
    391  1.1  christos   bed = get_elf_backend_data (abfd);
    392  1.1  christos   p_paddr = bed->want_p_paddr_set_to_zero ? 0 : src->p_paddr;
    393  1.1  christos 
    394  1.1  christos   /* note that all elements of dst are *arrays of unsigned char* already...  */
    395  1.1  christos   H_PUT_32 (abfd, src->p_type, dst->p_type);
    396  1.1  christos   H_PUT_WORD (abfd, src->p_offset, dst->p_offset);
    397  1.1  christos   H_PUT_WORD (abfd, src->p_vaddr, dst->p_vaddr);
    398  1.1  christos   H_PUT_WORD (abfd, p_paddr, dst->p_paddr);
    399  1.1  christos   H_PUT_WORD (abfd, src->p_filesz, dst->p_filesz);
    400  1.1  christos   H_PUT_WORD (abfd, src->p_memsz, dst->p_memsz);
    401  1.1  christos   H_PUT_32 (abfd, src->p_flags, dst->p_flags);
    402  1.1  christos   H_PUT_WORD (abfd, src->p_align, dst->p_align);
    403  1.1  christos }
    404  1.1  christos 
    405  1.1  christos /* Translate an ELF reloc from external format to internal format.  */
    406  1.1  christos void
    407  1.1  christos elf_swap_reloc_in (bfd *abfd,
    408  1.1  christos 		   const bfd_byte *s,
    409  1.1  christos 		   Elf_Internal_Rela *dst)
    410  1.1  christos {
    411  1.1  christos   const Elf_External_Rel *src = (const Elf_External_Rel *) s;
    412  1.1  christos   dst->r_offset = H_GET_WORD (abfd, src->r_offset);
    413  1.1  christos   dst->r_info = H_GET_WORD (abfd, src->r_info);
    414  1.1  christos   dst->r_addend = 0;
    415  1.1  christos }
    416  1.1  christos 
    417  1.1  christos void
    418  1.1  christos elf_swap_reloca_in (bfd *abfd,
    419  1.1  christos 		    const bfd_byte *s,
    420  1.1  christos 		    Elf_Internal_Rela *dst)
    421  1.1  christos {
    422  1.1  christos   const Elf_External_Rela *src = (const Elf_External_Rela *) s;
    423  1.1  christos   dst->r_offset = H_GET_WORD (abfd, src->r_offset);
    424  1.1  christos   dst->r_info = H_GET_WORD (abfd, src->r_info);
    425  1.1  christos   dst->r_addend = H_GET_SIGNED_WORD (abfd, src->r_addend);
    426  1.1  christos }
    427  1.1  christos 
    428  1.1  christos /* Translate an ELF reloc from internal format to external format.  */
    429  1.1  christos void
    430  1.1  christos elf_swap_reloc_out (bfd *abfd,
    431  1.1  christos 		    const Elf_Internal_Rela *src,
    432  1.1  christos 		    bfd_byte *d)
    433  1.1  christos {
    434  1.1  christos   Elf_External_Rel *dst = (Elf_External_Rel *) d;
    435  1.1  christos   H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
    436  1.1  christos   H_PUT_WORD (abfd, src->r_info, dst->r_info);
    437  1.1  christos }
    438  1.1  christos 
    439  1.1  christos void
    440  1.1  christos elf_swap_reloca_out (bfd *abfd,
    441  1.1  christos 		     const Elf_Internal_Rela *src,
    442  1.1  christos 		     bfd_byte *d)
    443  1.1  christos {
    444  1.1  christos   Elf_External_Rela *dst = (Elf_External_Rela *) d;
    445  1.1  christos   H_PUT_WORD (abfd, src->r_offset, dst->r_offset);
    446  1.1  christos   H_PUT_WORD (abfd, src->r_info, dst->r_info);
    447  1.1  christos   H_PUT_SIGNED_WORD (abfd, src->r_addend, dst->r_addend);
    448  1.1  christos }
    449  1.1  christos 
    450  1.1  christos void
    451  1.1  christos elf_swap_dyn_in (bfd *abfd,
    452  1.1  christos 		 const void *p,
    453  1.1  christos 		 Elf_Internal_Dyn *dst)
    454  1.1  christos {
    455  1.1  christos   const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
    456  1.1  christos 
    457  1.1  christos   dst->d_tag = H_GET_WORD (abfd, src->d_tag);
    458  1.1  christos   dst->d_un.d_val = H_GET_WORD (abfd, src->d_un.d_val);
    459  1.1  christos }
    460  1.1  christos 
    461  1.1  christos void
    462  1.1  christos elf_swap_dyn_out (bfd *abfd,
    463  1.1  christos 		  const Elf_Internal_Dyn *src,
    464  1.1  christos 		  void *p)
    465  1.1  christos {
    466  1.1  christos   Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
    467  1.1  christos 
    468  1.1  christos   H_PUT_WORD (abfd, src->d_tag, dst->d_tag);
    469  1.1  christos   H_PUT_WORD (abfd, src->d_un.d_val, dst->d_un.d_val);
    470  1.1  christos }
    471  1.1  christos 
    472  1.1  christos /* ELF .o/exec file reading */
    474  1.1  christos 
    475  1.1  christos /* Begin processing a given object.
    476  1.1  christos 
    477  1.1  christos    First we validate the file by reading in the ELF header and checking
    478  1.1  christos    the magic number.  */
    479  1.1  christos 
    480  1.1  christos static inline bfd_boolean
    481  1.1  christos elf_file_p (Elf_External_Ehdr *x_ehdrp)
    482  1.1  christos {
    483  1.1  christos   return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
    484  1.1  christos 	  && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
    485  1.1  christos 	  && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
    486  1.1  christos 	  && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
    487  1.1  christos }
    488  1.1  christos 
    489  1.1  christos /* Check to see if the file associated with ABFD matches the target vector
    490  1.1  christos    that ABFD points to.
    491  1.1  christos 
    492  1.1  christos    Note that we may be called several times with the same ABFD, but different
    493  1.1  christos    target vectors, most of which will not match.  We have to avoid leaving
    494  1.1  christos    any side effects in ABFD, or any data it points to (like tdata), if the
    495  1.1  christos    file does not match the target vector.  */
    496  1.1  christos 
    497  1.1  christos const bfd_target *
    498  1.1  christos elf_object_p (bfd *abfd)
    499  1.1  christos {
    500  1.1  christos   Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
    501  1.1  christos   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form */
    502  1.1  christos   Elf_External_Shdr x_shdr;	/* Section header table entry, external form */
    503  1.1  christos   Elf_Internal_Shdr i_shdr;
    504  1.1  christos   Elf_Internal_Shdr *i_shdrp;	/* Section header table, internal form */
    505  1.1  christos   unsigned int shindex;
    506  1.1  christos   const struct elf_backend_data *ebd;
    507  1.1  christos   asection *s;
    508  1.1  christos   bfd_size_type amt;
    509  1.1  christos   const bfd_target *target;
    510  1.1  christos 
    511  1.1  christos   /* Read in the ELF header in external format.  */
    512  1.1  christos 
    513  1.1  christos   if (bfd_bread (&x_ehdr, sizeof (x_ehdr), abfd) != sizeof (x_ehdr))
    514  1.1  christos     {
    515  1.1  christos       if (bfd_get_error () != bfd_error_system_call)
    516  1.1  christos 	goto got_wrong_format_error;
    517  1.1  christos       else
    518  1.1  christos 	goto got_no_match;
    519  1.1  christos     }
    520  1.1  christos 
    521  1.1  christos   /* Now check to see if we have a valid ELF file, and one that BFD can
    522  1.1  christos      make use of.  The magic number must match, the address size ('class')
    523  1.1  christos      and byte-swapping must match our XVEC entry, and it must have a
    524  1.1  christos      section header table (FIXME: See comments re sections at top of this
    525  1.1  christos      file).  */
    526  1.1  christos 
    527  1.1  christos   if (! elf_file_p (&x_ehdr)
    528  1.1  christos       || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
    529  1.1  christos       || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
    530  1.1  christos     goto got_wrong_format_error;
    531  1.1  christos 
    532  1.1  christos   /* Check that file's byte order matches xvec's */
    533  1.1  christos   switch (x_ehdr.e_ident[EI_DATA])
    534  1.1  christos     {
    535  1.1  christos     case ELFDATA2MSB:		/* Big-endian */
    536  1.1  christos       if (! bfd_header_big_endian (abfd))
    537  1.1  christos 	goto got_wrong_format_error;
    538  1.1  christos       break;
    539  1.1  christos     case ELFDATA2LSB:		/* Little-endian */
    540  1.1  christos       if (! bfd_header_little_endian (abfd))
    541  1.1  christos 	goto got_wrong_format_error;
    542  1.1  christos       break;
    543  1.1  christos     case ELFDATANONE:		/* No data encoding specified */
    544  1.1  christos     default:			/* Unknown data encoding specified */
    545  1.1  christos       goto got_wrong_format_error;
    546  1.1  christos     }
    547  1.1  christos 
    548  1.1  christos   target = abfd->xvec;
    549  1.1  christos 
    550  1.1  christos   /* Allocate an instance of the elf_obj_tdata structure and hook it up to
    551  1.1  christos      the tdata pointer in the bfd.  */
    552  1.1  christos 
    553  1.1  christos   if (! (*target->_bfd_set_format[bfd_object]) (abfd))
    554  1.1  christos     goto got_no_match;
    555  1.1  christos 
    556  1.1  christos   /* Now that we know the byte order, swap in the rest of the header */
    557  1.1  christos   i_ehdrp = elf_elfheader (abfd);
    558  1.1  christos   elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
    559  1.1  christos #if DEBUG & 1
    560  1.1  christos   elf_debug_file (i_ehdrp);
    561  1.1  christos #endif
    562  1.1  christos 
    563  1.1  christos   /* Reject ET_CORE (header indicates core file, not object file) */
    564  1.1  christos   if (i_ehdrp->e_type == ET_CORE)
    565  1.1  christos     goto got_wrong_format_error;
    566  1.1  christos 
    567  1.1  christos   /* If this is a relocatable file and there is no section header
    568  1.1  christos      table, then we're hosed.  */
    569  1.1  christos   if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_type == ET_REL)
    570  1.1  christos     goto got_wrong_format_error;
    571  1.1  christos 
    572  1.1  christos   /* As a simple sanity check, verify that what BFD thinks is the
    573  1.1  christos      size of each section header table entry actually matches the size
    574  1.1  christos      recorded in the file, but only if there are any sections.  */
    575  1.1  christos   if (i_ehdrp->e_shentsize != sizeof (x_shdr) && i_ehdrp->e_shnum != 0)
    576  1.1  christos     goto got_wrong_format_error;
    577  1.1  christos 
    578  1.1  christos   /* Further sanity check.  */
    579  1.1  christos   if (i_ehdrp->e_shoff == 0 && i_ehdrp->e_shnum != 0)
    580  1.1  christos     goto got_wrong_format_error;
    581  1.1  christos 
    582  1.1  christos   ebd = get_elf_backend_data (abfd);
    583  1.1  christos   if (ebd->s->arch_size != ARCH_SIZE)
    584  1.1  christos     goto got_wrong_format_error;
    585  1.1  christos 
    586  1.1  christos   /* Check that the ELF e_machine field matches what this particular
    587  1.1  christos      BFD format expects.  */
    588  1.1  christos   if (ebd->elf_machine_code != i_ehdrp->e_machine
    589  1.1  christos       && (ebd->elf_machine_alt1 == 0
    590  1.1  christos 	  || i_ehdrp->e_machine != ebd->elf_machine_alt1)
    591  1.1  christos       && (ebd->elf_machine_alt2 == 0
    592  1.1  christos 	  || i_ehdrp->e_machine != ebd->elf_machine_alt2)
    593  1.1  christos       && ebd->elf_machine_code != EM_NONE)
    594  1.1  christos     goto got_wrong_format_error;
    595  1.1  christos 
    596  1.1  christos   if (i_ehdrp->e_type == ET_EXEC)
    597  1.1  christos     abfd->flags |= EXEC_P;
    598  1.1  christos   else if (i_ehdrp->e_type == ET_DYN)
    599  1.1  christos     abfd->flags |= DYNAMIC;
    600  1.1  christos 
    601  1.1  christos   if (i_ehdrp->e_phnum > 0)
    602  1.1  christos     abfd->flags |= D_PAGED;
    603  1.1  christos 
    604  1.1  christos   if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
    605  1.1  christos     {
    606  1.1  christos       /* It's OK if this fails for the generic target.  */
    607  1.1  christos       if (ebd->elf_machine_code != EM_NONE)
    608  1.1  christos 	goto got_no_match;
    609  1.1  christos     }
    610  1.1  christos 
    611  1.1  christos   if (ebd->elf_machine_code != EM_NONE
    612  1.1  christos       && i_ehdrp->e_ident[EI_OSABI] != ebd->elf_osabi
    613  1.1  christos       && ebd->elf_osabi != ELFOSABI_NONE)
    614  1.1  christos     goto got_wrong_format_error;
    615  1.1  christos 
    616  1.6  christos   if (i_ehdrp->e_shoff != 0)
    617  1.1  christos     {
    618  1.1  christos       file_ptr where = (file_ptr) i_ehdrp->e_shoff;
    619  1.6  christos 
    620  1.1  christos       /* Seek to the section header table in the file.  */
    621  1.1  christos       if (bfd_seek (abfd, where, SEEK_SET) != 0)
    622  1.1  christos 	goto got_no_match;
    623  1.1  christos 
    624  1.1  christos       /* Read the first section header at index 0, and convert to internal
    625  1.1  christos 	 form.  */
    626  1.1  christos       if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
    627  1.1  christos 	goto got_no_match;
    628  1.1  christos       elf_swap_shdr_in (abfd, &x_shdr, &i_shdr);
    629  1.1  christos 
    630  1.1  christos       /* If the section count is zero, the actual count is in the first
    631  1.1  christos 	 section header.  */
    632  1.1  christos       if (i_ehdrp->e_shnum == SHN_UNDEF)
    633  1.1  christos 	{
    634  1.1  christos 	  i_ehdrp->e_shnum = i_shdr.sh_size;
    635  1.1  christos 	  if (i_ehdrp->e_shnum >= SHN_LORESERVE
    636  1.1  christos 	      || i_ehdrp->e_shnum != i_shdr.sh_size
    637  1.1  christos 	      || i_ehdrp->e_shnum  == 0)
    638  1.1  christos 	    goto got_wrong_format_error;
    639  1.1  christos 	}
    640  1.1  christos 
    641  1.1  christos       /* And similarly for the string table index.  */
    642  1.1  christos       if (i_ehdrp->e_shstrndx == (SHN_XINDEX & 0xffff))
    643  1.1  christos 	{
    644  1.1  christos 	  i_ehdrp->e_shstrndx = i_shdr.sh_link;
    645  1.1  christos 	  if (i_ehdrp->e_shstrndx != i_shdr.sh_link)
    646  1.1  christos 	    goto got_wrong_format_error;
    647  1.1  christos 	}
    648  1.1  christos 
    649  1.1  christos       /* And program headers.  */
    650  1.1  christos       if (i_ehdrp->e_phnum == PN_XNUM && i_shdr.sh_info != 0)
    651  1.1  christos 	{
    652  1.1  christos 	  i_ehdrp->e_phnum = i_shdr.sh_info;
    653  1.1  christos 	  if (i_ehdrp->e_phnum != i_shdr.sh_info)
    654  1.1  christos 	    goto got_wrong_format_error;
    655  1.1  christos 	}
    656  1.1  christos 
    657  1.1  christos       /* Sanity check that we can read all of the section headers.
    658  1.1  christos 	 It ought to be good enough to just read the last one.  */
    659  1.1  christos       if (i_ehdrp->e_shnum != 1)
    660  1.1  christos 	{
    661  1.1  christos 	  /* Check that we don't have a totally silly number of sections.  */
    662  1.1  christos 	  if (i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (x_shdr)
    663  1.1  christos 	      || i_ehdrp->e_shnum > (unsigned int) -1 / sizeof (i_shdr))
    664  1.1  christos 	    goto got_wrong_format_error;
    665  1.1  christos 
    666  1.1  christos 	  where += (i_ehdrp->e_shnum - 1) * sizeof (x_shdr);
    667  1.1  christos 	  if ((bfd_size_type) where <= i_ehdrp->e_shoff)
    668  1.6  christos 	    goto got_wrong_format_error;
    669  1.1  christos 
    670  1.1  christos 	  if (bfd_seek (abfd, where, SEEK_SET) != 0)
    671  1.1  christos 	    goto got_no_match;
    672  1.1  christos 	  if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
    673  1.1  christos 	    goto got_no_match;
    674  1.1  christos 
    675  1.6  christos 	  /* Back to where we were.  */
    676  1.1  christos 	  where = i_ehdrp->e_shoff + sizeof (x_shdr);
    677  1.1  christos 	  if (bfd_seek (abfd, where, SEEK_SET) != 0)
    678  1.1  christos 	    goto got_no_match;
    679  1.1  christos 	}
    680  1.1  christos     }
    681  1.1  christos 
    682  1.1  christos   /* Allocate space for a copy of the section header table in
    683  1.1  christos      internal form.  */
    684  1.1  christos   if (i_ehdrp->e_shnum != 0)
    685  1.1  christos     {
    686  1.1  christos       Elf_Internal_Shdr *shdrp;
    687  1.6  christos       unsigned int num_sec;
    688  1.6  christos 
    689  1.6  christos #ifndef BFD64
    690  1.6  christos       if (i_ehdrp->e_shnum > ((bfd_size_type) -1) / sizeof (*i_shdrp))
    691  1.8  christos 	goto got_wrong_format_error;
    692  1.1  christos #endif
    693  1.1  christos       amt = sizeof (*i_shdrp) * (bfd_size_type) i_ehdrp->e_shnum;
    694  1.1  christos       i_shdrp = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt);
    695  1.1  christos       if (!i_shdrp)
    696  1.1  christos 	goto got_no_match;
    697  1.1  christos       num_sec = i_ehdrp->e_shnum;
    698  1.1  christos       elf_numsections (abfd) = num_sec;
    699  1.1  christos       amt = sizeof (i_shdrp) * num_sec;
    700  1.1  christos       elf_elfsections (abfd) = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt);
    701  1.1  christos       if (!elf_elfsections (abfd))
    702  1.1  christos 	goto got_no_match;
    703  1.1  christos 
    704  1.1  christos       memcpy (i_shdrp, &i_shdr, sizeof (*i_shdrp));
    705  1.1  christos       for (shdrp = i_shdrp, shindex = 0; shindex < num_sec; shindex++)
    706  1.1  christos 	elf_elfsections (abfd)[shindex] = shdrp++;
    707  1.1  christos 
    708  1.1  christos       /* Read in the rest of the section header table and convert it
    709  1.1  christos 	 to internal form.  */
    710  1.1  christos       for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
    711  1.1  christos 	{
    712  1.1  christos 	  if (bfd_bread (&x_shdr, sizeof x_shdr, abfd) != sizeof (x_shdr))
    713  1.1  christos 	    goto got_no_match;
    714  1.1  christos 	  elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
    715  1.1  christos 
    716  1.1  christos 	  /* Sanity check sh_link and sh_info.  */
    717  1.1  christos 	  if (i_shdrp[shindex].sh_link >= num_sec)
    718  1.1  christos 	    {
    719  1.1  christos 	      /* PR 10478: Accept Solaris binaries with a sh_link
    720  1.1  christos 		 field set to SHN_BEFORE or SHN_AFTER.  */
    721  1.1  christos 	      switch (ebd->elf_machine_code)
    722  1.5  christos 		{
    723  1.1  christos 		case EM_386:
    724  1.1  christos 		case EM_IAMCU:
    725  1.1  christos 		case EM_X86_64:
    726  1.1  christos 		case EM_OLD_SPARCV9:
    727  1.1  christos 		case EM_SPARC32PLUS:
    728  1.1  christos 		case EM_SPARCV9:
    729  1.1  christos 		case EM_SPARC:
    730  1.1  christos 		  if (i_shdrp[shindex].sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */
    731  1.1  christos 		      || i_shdrp[shindex].sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */)
    732  1.1  christos 		    break;
    733  1.1  christos 		  /* Otherwise fall through.  */
    734  1.1  christos 		default:
    735  1.1  christos 		  goto got_wrong_format_error;
    736  1.1  christos 		}
    737  1.1  christos 	    }
    738  1.1  christos 
    739  1.1  christos 	  if (((i_shdrp[shindex].sh_flags & SHF_INFO_LINK)
    740  1.1  christos 	       || i_shdrp[shindex].sh_type == SHT_RELA
    741  1.1  christos 	       || i_shdrp[shindex].sh_type == SHT_REL)
    742  1.1  christos 	      && i_shdrp[shindex].sh_info >= num_sec)
    743  1.1  christos 	    goto got_wrong_format_error;
    744  1.1  christos 
    745  1.1  christos 	  /* If the section is loaded, but not page aligned, clear
    746  1.1  christos 	     D_PAGED.  */
    747  1.1  christos 	  if (i_shdrp[shindex].sh_size != 0
    748  1.1  christos 	      && (i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
    749  1.1  christos 	      && i_shdrp[shindex].sh_type != SHT_NOBITS
    750  1.1  christos 	      && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
    751  1.1  christos 		   % ebd->minpagesize)
    752  1.1  christos 		  != 0))
    753  1.1  christos 	    abfd->flags &= ~D_PAGED;
    754  1.1  christos 	}
    755  1.1  christos     }
    756  1.1  christos 
    757  1.1  christos   /* A further sanity check.  */
    758  1.1  christos   if (i_ehdrp->e_shnum != 0)
    759  1.1  christos     {
    760  1.1  christos       if (i_ehdrp->e_shstrndx >= elf_numsections (abfd))
    761  1.1  christos 	{
    762  1.1  christos 	  /* PR 2257:
    763  1.1  christos 	     We used to just goto got_wrong_format_error here
    764  1.1  christos 	     but there are binaries in existance for which this test
    765  1.1  christos 	     will prevent the binutils from working with them at all.
    766  1.1  christos 	     So we are kind, and reset the string index value to 0
    767  1.7  christos 	     so that at least some processing can be done.  */
    768  1.8  christos 	  i_ehdrp->e_shstrndx = SHN_UNDEF;
    769  1.7  christos 	  _bfd_error_handler
    770  1.1  christos 	    (_("warning: %pB has a corrupt string table index - ignoring"),
    771  1.1  christos 	     abfd);
    772  1.1  christos 	}
    773  1.1  christos     }
    774  1.1  christos   else if (i_ehdrp->e_shstrndx != SHN_UNDEF)
    775  1.1  christos     goto got_wrong_format_error;
    776  1.1  christos 
    777  1.1  christos   /* Read in the program headers.  */
    778  1.1  christos   if (i_ehdrp->e_phnum == 0)
    779  1.1  christos     elf_tdata (abfd)->phdr = NULL;
    780  1.1  christos   else
    781  1.1  christos     {
    782  1.1  christos       Elf_Internal_Phdr *i_phdr;
    783  1.6  christos       unsigned int i;
    784  1.6  christos 
    785  1.6  christos #ifndef BFD64
    786  1.6  christos       if (i_ehdrp->e_phnum > ((bfd_size_type) -1) / sizeof (*i_phdr))
    787  1.8  christos 	goto got_wrong_format_error;
    788  1.8  christos #endif
    789  1.8  christos       /* Check for a corrupt input file with an impossibly large number
    790  1.8  christos 	 of program headers.  */
    791  1.8  christos       if (bfd_get_file_size (abfd) > 0
    792  1.8  christos 	  && i_ehdrp->e_phnum > bfd_get_file_size (abfd))
    793  1.1  christos 	goto got_no_match;
    794  1.1  christos       amt = (bfd_size_type) i_ehdrp->e_phnum * sizeof (*i_phdr);
    795  1.1  christos       elf_tdata (abfd)->phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt);
    796  1.1  christos       if (elf_tdata (abfd)->phdr == NULL)
    797  1.1  christos 	goto got_no_match;
    798  1.1  christos       if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_phoff, SEEK_SET) != 0)
    799  1.1  christos 	goto got_no_match;
    800  1.1  christos       i_phdr = elf_tdata (abfd)->phdr;
    801  1.1  christos       for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
    802  1.1  christos 	{
    803  1.1  christos 	  Elf_External_Phdr x_phdr;
    804  1.1  christos 
    805  1.1  christos 	  if (bfd_bread (&x_phdr, sizeof x_phdr, abfd) != sizeof x_phdr)
    806  1.1  christos 	    goto got_no_match;
    807  1.1  christos 	  elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
    808  1.1  christos 	}
    809  1.1  christos     }
    810  1.1  christos 
    811  1.1  christos   if (i_ehdrp->e_shstrndx != 0 && i_ehdrp->e_shoff != 0)
    812  1.1  christos     {
    813  1.1  christos       unsigned int num_sec;
    814  1.1  christos 
    815  1.1  christos       /* Once all of the section headers have been read and converted, we
    816  1.1  christos 	 can start processing them.  Note that the first section header is
    817  1.1  christos 	 a dummy placeholder entry, so we ignore it.  */
    818  1.1  christos       num_sec = elf_numsections (abfd);
    819  1.1  christos       for (shindex = 1; shindex < num_sec; shindex++)
    820  1.1  christos 	if (!bfd_section_from_shdr (abfd, shindex))
    821  1.1  christos 	  goto got_no_match;
    822  1.1  christos 
    823  1.1  christos       /* Set up ELF sections for SHF_GROUP and SHF_LINK_ORDER.  */
    824  1.1  christos       if (! _bfd_elf_setup_sections (abfd))
    825  1.1  christos 	goto got_wrong_format_error;
    826  1.1  christos     }
    827  1.1  christos 
    828  1.1  christos   /* Let the backend double check the format and override global
    829  1.1  christos      information.  */
    830  1.1  christos   if (ebd->elf_backend_object_p)
    831  1.1  christos     {
    832  1.1  christos       if (! (*ebd->elf_backend_object_p) (abfd))
    833  1.1  christos 	goto got_wrong_format_error;
    834  1.1  christos     }
    835  1.1  christos 
    836  1.1  christos   /* Remember the entry point specified in the ELF file header.  */
    837  1.1  christos   bfd_set_start_address (abfd, i_ehdrp->e_entry);
    838  1.1  christos 
    839  1.1  christos   /* If we have created any reloc sections that are associated with
    840  1.1  christos      debugging sections, mark the reloc sections as debugging as well.  */
    841  1.1  christos   for (s = abfd->sections; s != NULL; s = s->next)
    842  1.1  christos     {
    843  1.1  christos       if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
    844  1.1  christos 	   || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
    845  1.1  christos 	  && elf_section_data (s)->this_hdr.sh_info > 0)
    846  1.1  christos 	{
    847  1.1  christos 	  unsigned long targ_index;
    848  1.1  christos 	  asection *targ_sec;
    849  1.1  christos 
    850  1.1  christos 	  targ_index = elf_section_data (s)->this_hdr.sh_info;
    851  1.1  christos 	  targ_sec = bfd_section_from_elf_index (abfd, targ_index);
    852  1.1  christos 	  if (targ_sec != NULL
    853  1.1  christos 	      && (targ_sec->flags & SEC_DEBUGGING) != 0)
    854  1.1  christos 	    s->flags |= SEC_DEBUGGING;
    855  1.1  christos 	}
    856  1.1  christos     }
    857  1.1  christos   return target;
    858  1.1  christos 
    859  1.1  christos  got_wrong_format_error:
    860  1.1  christos   bfd_set_error (bfd_error_wrong_format);
    861  1.1  christos 
    862  1.1  christos  got_no_match:
    863  1.1  christos   return NULL;
    864  1.1  christos }
    865  1.1  christos 
    866  1.1  christos /* ELF .o/exec file writing */
    868  1.1  christos 
    869  1.1  christos /* Write out the relocs.  */
    870  1.1  christos 
    871  1.1  christos void
    872  1.1  christos elf_write_relocs (bfd *abfd, asection *sec, void *data)
    873  1.1  christos {
    874  1.1  christos   bfd_boolean *failedp = (bfd_boolean *) data;
    875  1.1  christos   Elf_Internal_Shdr *rela_hdr;
    876  1.1  christos   bfd_vma addr_offset;
    877  1.1  christos   void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *);
    878  1.1  christos   size_t extsize;
    879  1.1  christos   bfd_byte *dst_rela;
    880  1.1  christos   unsigned int idx;
    881  1.1  christos   asymbol *last_sym;
    882  1.1  christos   int last_sym_idx;
    883  1.1  christos 
    884  1.1  christos   /* If we have already failed, don't do anything.  */
    885  1.1  christos   if (*failedp)
    886  1.1  christos     return;
    887  1.1  christos 
    888  1.1  christos   if ((sec->flags & SEC_RELOC) == 0)
    889  1.1  christos     return;
    890  1.1  christos 
    891  1.1  christos   /* The linker backend writes the relocs out itself, and sets the
    892  1.1  christos      reloc_count field to zero to inhibit writing them here.  Also,
    893  1.1  christos      sometimes the SEC_RELOC flag gets set even when there aren't any
    894  1.1  christos      relocs.  */
    895  1.1  christos   if (sec->reloc_count == 0)
    896  1.1  christos     return;
    897  1.1  christos 
    898  1.1  christos   /* If we have opened an existing file for update, reloc_count may be
    899  1.1  christos      set even though we are not linking.  In that case we have nothing
    900  1.1  christos      to do.  */
    901  1.1  christos   if (sec->orelocation == NULL)
    902  1.1  christos     return;
    903  1.1  christos 
    904  1.1  christos   rela_hdr = elf_section_data (sec)->rela.hdr;
    905  1.1  christos   if (rela_hdr == NULL)
    906  1.1  christos     rela_hdr = elf_section_data (sec)->rel.hdr;
    907  1.1  christos 
    908  1.1  christos   rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
    909  1.1  christos   rela_hdr->contents = (unsigned char *) bfd_alloc (abfd, rela_hdr->sh_size);
    910  1.1  christos   if (rela_hdr->contents == NULL)
    911  1.1  christos     {
    912  1.1  christos       *failedp = TRUE;
    913  1.1  christos       return;
    914  1.1  christos     }
    915  1.1  christos 
    916  1.1  christos   /* Figure out whether the relocations are RELA or REL relocations.  */
    917  1.1  christos   if (rela_hdr->sh_type == SHT_RELA)
    918  1.1  christos     {
    919  1.1  christos       swap_out = elf_swap_reloca_out;
    920  1.1  christos       extsize = sizeof (Elf_External_Rela);
    921  1.1  christos     }
    922  1.1  christos   else if (rela_hdr->sh_type == SHT_REL)
    923  1.1  christos     {
    924  1.1  christos       swap_out = elf_swap_reloc_out;
    925  1.1  christos       extsize = sizeof (Elf_External_Rel);
    926  1.1  christos     }
    927  1.1  christos   else
    928  1.1  christos     /* Every relocation section should be either an SHT_RELA or an
    929  1.1  christos        SHT_REL section.  */
    930  1.1  christos     abort ();
    931  1.1  christos 
    932  1.1  christos   /* The address of an ELF reloc is section relative for an object
    933  1.1  christos      file, and absolute for an executable file or shared library.
    934  1.1  christos      The address of a BFD reloc is always section relative.  */
    935  1.1  christos   addr_offset = 0;
    936  1.1  christos   if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
    937  1.1  christos     addr_offset = sec->vma;
    938  1.1  christos 
    939  1.1  christos   /* orelocation has the data, reloc_count has the count...  */
    940  1.1  christos   last_sym = 0;
    941  1.1  christos   last_sym_idx = 0;
    942  1.1  christos   dst_rela = rela_hdr->contents;
    943  1.1  christos 
    944  1.1  christos   for (idx = 0; idx < sec->reloc_count; idx++, dst_rela += extsize)
    945  1.1  christos     {
    946  1.1  christos       Elf_Internal_Rela src_rela;
    947  1.1  christos       arelent *ptr;
    948  1.1  christos       asymbol *sym;
    949  1.1  christos       int n;
    950  1.1  christos 
    951  1.1  christos       ptr = sec->orelocation[idx];
    952  1.1  christos       sym = *ptr->sym_ptr_ptr;
    953  1.1  christos       if (sym == last_sym)
    954  1.1  christos 	n = last_sym_idx;
    955  1.1  christos       else if (bfd_is_abs_section (sym->section) && sym->value == 0)
    956  1.1  christos 	n = STN_UNDEF;
    957  1.1  christos       else
    958  1.1  christos 	{
    959  1.1  christos 	  last_sym = sym;
    960  1.1  christos 	  n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
    961  1.1  christos 	  if (n < 0)
    962  1.1  christos 	    {
    963  1.1  christos 	      *failedp = TRUE;
    964  1.1  christos 	      return;
    965  1.1  christos 	    }
    966  1.1  christos 	  last_sym_idx = n;
    967  1.1  christos 	}
    968  1.1  christos 
    969  1.1  christos       if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
    970  1.1  christos 	  && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
    971  1.1  christos 	  && ! _bfd_elf_validate_reloc (abfd, ptr))
    972  1.1  christos 	{
    973  1.1  christos 	  *failedp = TRUE;
    974  1.8  christos 	  return;
    975  1.8  christos 	}
    976  1.8  christos 
    977  1.8  christos       if (ptr->howto == NULL)
    978  1.8  christos 	{
    979  1.8  christos 	  *failedp = TRUE;
    980  1.1  christos 	  return;
    981  1.1  christos 	}
    982  1.1  christos 
    983  1.1  christos       src_rela.r_offset = ptr->address + addr_offset;
    984  1.1  christos       src_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
    985  1.1  christos       src_rela.r_addend = ptr->addend;
    986  1.1  christos       (*swap_out) (abfd, &src_rela, dst_rela);
    987  1.1  christos     }
    988  1.1  christos }
    989  1.1  christos 
    990  1.1  christos /* Write out the program headers.  */
    991  1.1  christos 
    992  1.1  christos int
    993  1.1  christos elf_write_out_phdrs (bfd *abfd,
    994  1.1  christos 		     const Elf_Internal_Phdr *phdr,
    995  1.1  christos 		     unsigned int count)
    996  1.1  christos {
    997  1.7  christos   while (count--)
    998  1.1  christos     {
    999  1.1  christos       Elf_External_Phdr extphdr;
   1000  1.1  christos 
   1001  1.1  christos       elf_swap_phdr_out (abfd, phdr, &extphdr);
   1002  1.1  christos       if (bfd_bwrite (&extphdr, sizeof (Elf_External_Phdr), abfd)
   1003  1.1  christos 	  != sizeof (Elf_External_Phdr))
   1004  1.1  christos 	return -1;
   1005  1.1  christos       phdr++;
   1006  1.1  christos     }
   1007  1.1  christos   return 0;
   1008  1.1  christos }
   1009  1.1  christos 
   1010  1.1  christos /* Write out the section headers and the ELF file header.  */
   1011  1.1  christos 
   1012  1.1  christos bfd_boolean
   1013  1.1  christos elf_write_shdrs_and_ehdr (bfd *abfd)
   1014  1.1  christos {
   1015  1.1  christos   Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
   1016  1.1  christos   Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form */
   1017  1.1  christos   Elf_External_Shdr *x_shdrp;	/* Section header table, external form */
   1018  1.1  christos   Elf_Internal_Shdr **i_shdrp;	/* Section header table, internal form */
   1019  1.1  christos   unsigned int count;
   1020  1.1  christos   bfd_size_type amt;
   1021  1.1  christos 
   1022  1.1  christos   i_ehdrp = elf_elfheader (abfd);
   1023  1.1  christos   i_shdrp = elf_elfsections (abfd);
   1024  1.1  christos 
   1025  1.1  christos   /* swap the header before spitting it out...  */
   1026  1.1  christos 
   1027  1.1  christos #if DEBUG & 1
   1028  1.1  christos   elf_debug_file (i_ehdrp);
   1029  1.1  christos #endif
   1030  1.1  christos   elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
   1031  1.1  christos   amt = sizeof (x_ehdr);
   1032  1.1  christos   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
   1033  1.1  christos       || bfd_bwrite (&x_ehdr, amt, abfd) != amt)
   1034  1.1  christos     return FALSE;
   1035  1.1  christos 
   1036  1.1  christos   /* Some fields in the first section header handle overflow of ehdr
   1037  1.1  christos      fields.  */
   1038  1.1  christos   if (i_ehdrp->e_phnum >= PN_XNUM)
   1039  1.1  christos     i_shdrp[0]->sh_info = i_ehdrp->e_phnum;
   1040  1.1  christos   if (i_ehdrp->e_shnum >= (SHN_LORESERVE & 0xffff))
   1041  1.1  christos     i_shdrp[0]->sh_size = i_ehdrp->e_shnum;
   1042  1.1  christos   if (i_ehdrp->e_shstrndx >= (SHN_LORESERVE & 0xffff))
   1043  1.1  christos     i_shdrp[0]->sh_link = i_ehdrp->e_shstrndx;
   1044  1.1  christos 
   1045  1.1  christos   /* at this point we've concocted all the ELF sections...  */
   1046  1.1  christos   amt = i_ehdrp->e_shnum;
   1047  1.1  christos   amt *= sizeof (*x_shdrp);
   1048  1.1  christos   x_shdrp = (Elf_External_Shdr *) bfd_alloc (abfd, amt);
   1049  1.1  christos   if (!x_shdrp)
   1050  1.1  christos     return FALSE;
   1051  1.1  christos 
   1052  1.1  christos   for (count = 0; count < i_ehdrp->e_shnum; i_shdrp++, count++)
   1053  1.1  christos     {
   1054  1.1  christos #if DEBUG & 2
   1055  1.1  christos       elf_debug_section (count, *i_shdrp);
   1056  1.1  christos #endif
   1057  1.1  christos       elf_swap_shdr_out (abfd, *i_shdrp, x_shdrp + count);
   1058  1.1  christos     }
   1059  1.1  christos   if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
   1060  1.1  christos       || bfd_bwrite (x_shdrp, amt, abfd) != amt)
   1061  1.1  christos     return FALSE;
   1062  1.1  christos 
   1063  1.1  christos   /* need to dump the string table too...  */
   1064  1.1  christos 
   1065  1.1  christos   return TRUE;
   1066  1.1  christos }
   1067  1.1  christos 
   1068  1.1  christos bfd_boolean
   1069  1.1  christos elf_checksum_contents (bfd *abfd,
   1070  1.1  christos 		       void (*process) (const void *, size_t, void *),
   1071  1.1  christos 		       void *arg)
   1072  1.1  christos {
   1073  1.1  christos   Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
   1074  1.1  christos   Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
   1075  1.1  christos   Elf_Internal_Phdr *i_phdrp = elf_tdata (abfd)->phdr;
   1076  1.1  christos   unsigned int count, num;
   1077  1.1  christos 
   1078  1.1  christos   {
   1079  1.1  christos     Elf_External_Ehdr x_ehdr;
   1080  1.1  christos     Elf_Internal_Ehdr i_ehdr;
   1081  1.1  christos 
   1082  1.1  christos     i_ehdr = *i_ehdrp;
   1083  1.1  christos     i_ehdr.e_phoff = i_ehdr.e_shoff = 0;
   1084  1.1  christos     elf_swap_ehdr_out (abfd, &i_ehdr, &x_ehdr);
   1085  1.1  christos     (*process) (&x_ehdr, sizeof x_ehdr, arg);
   1086  1.1  christos   }
   1087  1.1  christos 
   1088  1.1  christos   num = i_ehdrp->e_phnum;
   1089  1.1  christos   for (count = 0; count < num; count++)
   1090  1.1  christos     {
   1091  1.1  christos       Elf_External_Phdr x_phdr;
   1092  1.1  christos       elf_swap_phdr_out (abfd, &i_phdrp[count], &x_phdr);
   1093  1.1  christos       (*process) (&x_phdr, sizeof x_phdr, arg);
   1094  1.1  christos     }
   1095  1.1  christos 
   1096  1.1  christos   num = elf_numsections (abfd);
   1097  1.1  christos   for (count = 0; count < num; count++)
   1098  1.1  christos     {
   1099  1.1  christos       Elf_Internal_Shdr i_shdr;
   1100  1.1  christos       Elf_External_Shdr x_shdr;
   1101  1.1  christos       bfd_byte *contents, *free_contents;
   1102  1.1  christos 
   1103  1.1  christos       i_shdr = *i_shdrp[count];
   1104  1.1  christos       i_shdr.sh_offset = 0;
   1105  1.1  christos 
   1106  1.1  christos       elf_swap_shdr_out (abfd, &i_shdr, &x_shdr);
   1107  1.1  christos       (*process) (&x_shdr, sizeof x_shdr, arg);
   1108  1.1  christos 
   1109  1.1  christos       /* Process the section's contents, if it has some.
   1110  1.1  christos 	 PR ld/12451: Read them in if necessary.  */
   1111  1.1  christos       if (i_shdr.sh_type == SHT_NOBITS)
   1112  1.1  christos 	continue;
   1113  1.1  christos       free_contents = NULL;
   1114  1.1  christos       contents = i_shdr.contents;
   1115  1.1  christos       if (contents == NULL)
   1116  1.1  christos 	{
   1117  1.1  christos 	  asection *sec;
   1118  1.1  christos 
   1119  1.1  christos 	  sec = bfd_section_from_elf_index (abfd, count);
   1120  1.1  christos 	  if (sec != NULL)
   1121  1.1  christos 	    {
   1122  1.1  christos 	      contents = sec->contents;
   1123  1.1  christos 	      if (contents == NULL)
   1124  1.1  christos 		{
   1125  1.1  christos 		  /* Force rereading from file.  */
   1126  1.1  christos 		  sec->flags &= ~SEC_IN_MEMORY;
   1127  1.1  christos 		  if (!bfd_malloc_and_get_section (abfd, sec, &free_contents))
   1128  1.1  christos 		    continue;
   1129  1.1  christos 		  contents = free_contents;
   1130  1.1  christos 		}
   1131  1.1  christos 	    }
   1132  1.1  christos 	}
   1133  1.1  christos       if (contents != NULL)
   1134  1.1  christos 	{
   1135  1.1  christos 	  (*process) (contents, i_shdr.sh_size, arg);
   1136  1.1  christos 	  if (free_contents != NULL)
   1137  1.1  christos 	    free (free_contents);
   1138  1.1  christos 	}
   1139  1.1  christos     }
   1140  1.1  christos 
   1141  1.1  christos   return TRUE;
   1142  1.1  christos }
   1143  1.1  christos 
   1144  1.1  christos long
   1145  1.1  christos elf_slurp_symbol_table (bfd *abfd, asymbol **symptrs, bfd_boolean dynamic)
   1146  1.1  christos {
   1147  1.1  christos   Elf_Internal_Shdr *hdr;
   1148  1.1  christos   Elf_Internal_Shdr *verhdr;
   1149  1.1  christos   unsigned long symcount;	/* Number of external ELF symbols */
   1150  1.1  christos   elf_symbol_type *sym;		/* Pointer to current bfd symbol */
   1151  1.1  christos   elf_symbol_type *symbase;	/* Buffer for generated bfd symbols */
   1152  1.1  christos   Elf_Internal_Sym *isym;
   1153  1.1  christos   Elf_Internal_Sym *isymend;
   1154  1.1  christos   Elf_Internal_Sym *isymbuf = NULL;
   1155  1.1  christos   Elf_External_Versym *xver;
   1156  1.1  christos   Elf_External_Versym *xverbuf = NULL;
   1157  1.1  christos   const struct elf_backend_data *ebd;
   1158  1.1  christos   bfd_size_type amt;
   1159  1.1  christos 
   1160  1.1  christos   /* Read each raw ELF symbol, converting from external ELF form to
   1161  1.1  christos      internal ELF form, and then using the information to create a
   1162  1.1  christos      canonical bfd symbol table entry.
   1163  1.1  christos 
   1164  1.1  christos      Note that we allocate the initial bfd canonical symbol buffer
   1165  1.1  christos      based on a one-to-one mapping of the ELF symbols to canonical
   1166  1.1  christos      symbols.  We actually use all the ELF symbols, so there will be no
   1167  1.1  christos      space left over at the end.  When we have all the symbols, we
   1168  1.1  christos      build the caller's pointer vector.  */
   1169  1.1  christos 
   1170  1.1  christos   if (! dynamic)
   1171  1.1  christos     {
   1172  1.1  christos       hdr = &elf_tdata (abfd)->symtab_hdr;
   1173  1.1  christos       verhdr = NULL;
   1174  1.1  christos     }
   1175  1.1  christos   else
   1176  1.1  christos     {
   1177  1.1  christos       hdr = &elf_tdata (abfd)->dynsymtab_hdr;
   1178  1.1  christos       if (elf_dynversym (abfd) == 0)
   1179  1.1  christos 	verhdr = NULL;
   1180  1.1  christos       else
   1181  1.1  christos 	verhdr = &elf_tdata (abfd)->dynversym_hdr;
   1182  1.1  christos       if ((elf_dynverdef (abfd) != 0
   1183  1.1  christos 	   && elf_tdata (abfd)->verdef == NULL)
   1184  1.1  christos 	  || (elf_dynverref (abfd) != 0
   1185  1.1  christos 	      && elf_tdata (abfd)->verref == NULL))
   1186  1.1  christos 	{
   1187  1.1  christos 	  if (!_bfd_elf_slurp_version_tables (abfd, FALSE))
   1188  1.1  christos 	    return -1;
   1189  1.1  christos 	}
   1190  1.1  christos     }
   1191  1.1  christos 
   1192  1.1  christos   ebd = get_elf_backend_data (abfd);
   1193  1.1  christos   symcount = hdr->sh_size / sizeof (Elf_External_Sym);
   1194  1.1  christos   if (symcount == 0)
   1195  1.1  christos     sym = symbase = NULL;
   1196  1.1  christos   else
   1197  1.1  christos     {
   1198  1.1  christos       isymbuf = bfd_elf_get_elf_syms (abfd, hdr, symcount, 0,
   1199  1.1  christos 				      NULL, NULL, NULL);
   1200  1.1  christos       if (isymbuf == NULL)
   1201  1.1  christos 	return -1;
   1202  1.1  christos 
   1203  1.1  christos       amt = symcount;
   1204  1.1  christos       amt *= sizeof (elf_symbol_type);
   1205  1.1  christos       symbase = (elf_symbol_type *) bfd_zalloc (abfd, amt);
   1206  1.1  christos       if (symbase == (elf_symbol_type *) NULL)
   1207  1.1  christos 	goto error_return;
   1208  1.1  christos 
   1209  1.1  christos       /* Read the raw ELF version symbol information.  */
   1210  1.7  christos       if (verhdr != NULL
   1211  1.7  christos 	  && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
   1212  1.8  christos 	{
   1213  1.8  christos 	  _bfd_error_handler
   1214  1.7  christos 	    /* xgettext:c-format */
   1215  1.8  christos 	    (_("%pB: version count (%" PRId64 ")"
   1216  1.1  christos 	       " does not match symbol count (%ld)"),
   1217  1.1  christos 	     abfd,
   1218  1.1  christos 	     (int64_t) (verhdr->sh_size / sizeof (Elf_External_Versym)),
   1219  1.1  christos 	     symcount);
   1220  1.1  christos 
   1221  1.1  christos 	  /* Slurp in the symbols without the version information,
   1222  1.1  christos 	     since that is more helpful than just quitting.  */
   1223  1.1  christos 	  verhdr = NULL;
   1224  1.1  christos 	}
   1225  1.1  christos 
   1226  1.1  christos       if (verhdr != NULL)
   1227  1.1  christos 	{
   1228  1.1  christos 	  if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
   1229  1.1  christos 	    goto error_return;
   1230  1.1  christos 
   1231  1.1  christos 	  xverbuf = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size);
   1232  1.1  christos 	  if (xverbuf == NULL && verhdr->sh_size != 0)
   1233  1.1  christos 	    goto error_return;
   1234  1.1  christos 
   1235  1.1  christos 	  if (bfd_bread (xverbuf, verhdr->sh_size, abfd) != verhdr->sh_size)
   1236  1.1  christos 	    goto error_return;
   1237  1.1  christos 	}
   1238  1.1  christos 
   1239  1.1  christos       /* Skip first symbol, which is a null dummy.  */
   1240  1.1  christos       xver = xverbuf;
   1241  1.1  christos       if (xver != NULL)
   1242  1.1  christos 	++xver;
   1243  1.1  christos       isymend = isymbuf + symcount;
   1244  1.3  christos       for (isym = isymbuf + 1, sym = symbase; isym < isymend; isym++, sym++)
   1245  1.1  christos 	{
   1246  1.1  christos 	  memcpy (&sym->internal_elf_sym, isym, sizeof (Elf_Internal_Sym));
   1247  1.1  christos 
   1248  1.1  christos 	  sym->symbol.the_bfd = abfd;
   1249  1.1  christos 	  sym->symbol.name = bfd_elf_sym_name (abfd, hdr, isym, NULL);
   1250  1.1  christos 	  sym->symbol.value = isym->st_value;
   1251  1.1  christos 
   1252  1.1  christos 	  if (isym->st_shndx == SHN_UNDEF)
   1253  1.1  christos 	    {
   1254  1.1  christos 	      sym->symbol.section = bfd_und_section_ptr;
   1255  1.1  christos 	    }
   1256  1.1  christos 	  else if (isym->st_shndx == SHN_ABS)
   1257  1.1  christos 	    {
   1258  1.1  christos 	      sym->symbol.section = bfd_abs_section_ptr;
   1259  1.1  christos 	    }
   1260  1.1  christos 	  else if (isym->st_shndx == SHN_COMMON)
   1261  1.1  christos 	    {
   1262  1.1  christos 	      sym->symbol.section = bfd_com_section_ptr;
   1263  1.1  christos 	      if ((abfd->flags & BFD_PLUGIN) != 0)
   1264  1.1  christos 		{
   1265  1.1  christos 		  asection *xc = bfd_get_section_by_name (abfd, "COMMON");
   1266  1.1  christos 
   1267  1.1  christos 		  if (xc == NULL)
   1268  1.1  christos 		    {
   1269  1.1  christos 		      flagword flags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP
   1270  1.1  christos 					| SEC_EXCLUDE);
   1271  1.1  christos 		      xc = bfd_make_section_with_flags (abfd, "COMMON", flags);
   1272  1.1  christos 		      if (xc == NULL)
   1273  1.1  christos 			goto error_return;
   1274  1.1  christos 		    }
   1275  1.1  christos 		  sym->symbol.section = xc;
   1276  1.1  christos 		}
   1277  1.1  christos 	      /* Elf puts the alignment into the `value' field, and
   1278  1.1  christos 		 the size into the `size' field.  BFD wants to see the
   1279  1.1  christos 		 size in the value field, and doesn't care (at the
   1280  1.1  christos 		 moment) about the alignment.  */
   1281  1.1  christos 	      sym->symbol.value = isym->st_size;
   1282  1.1  christos 	    }
   1283  1.1  christos 	  else
   1284  1.1  christos 	    {
   1285  1.1  christos 	      sym->symbol.section
   1286  1.1  christos 		= bfd_section_from_elf_index (abfd, isym->st_shndx);
   1287  1.1  christos 	      if (sym->symbol.section == NULL)
   1288  1.1  christos 		{
   1289  1.1  christos 		  /* This symbol is in a section for which we did not
   1290  1.1  christos 		     create a BFD section.  Just use bfd_abs_section,
   1291  1.1  christos 		     although it is wrong.  FIXME.  */
   1292  1.1  christos 		  sym->symbol.section = bfd_abs_section_ptr;
   1293  1.1  christos 		}
   1294  1.1  christos 	    }
   1295  1.1  christos 
   1296  1.1  christos 	  /* If this is a relocatable file, then the symbol value is
   1297  1.1  christos 	     already section relative.  */
   1298  1.1  christos 	  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
   1299  1.1  christos 	    sym->symbol.value -= sym->symbol.section->vma;
   1300  1.1  christos 
   1301  1.1  christos 	  switch (ELF_ST_BIND (isym->st_info))
   1302  1.1  christos 	    {
   1303  1.1  christos 	    case STB_LOCAL:
   1304  1.1  christos 	      sym->symbol.flags |= BSF_LOCAL;
   1305  1.1  christos 	      break;
   1306  1.1  christos 	    case STB_GLOBAL:
   1307  1.1  christos 	      if (isym->st_shndx != SHN_UNDEF && isym->st_shndx != SHN_COMMON)
   1308  1.1  christos 		sym->symbol.flags |= BSF_GLOBAL;
   1309  1.1  christos 	      break;
   1310  1.1  christos 	    case STB_WEAK:
   1311  1.1  christos 	      sym->symbol.flags |= BSF_WEAK;
   1312  1.1  christos 	      break;
   1313  1.1  christos 	    case STB_GNU_UNIQUE:
   1314  1.1  christos 	      sym->symbol.flags |= BSF_GNU_UNIQUE;
   1315  1.1  christos 	      break;
   1316  1.1  christos 	    }
   1317  1.1  christos 
   1318  1.1  christos 	  switch (ELF_ST_TYPE (isym->st_info))
   1319  1.1  christos 	    {
   1320  1.1  christos 	    case STT_SECTION:
   1321  1.1  christos 	      sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
   1322  1.1  christos 	      break;
   1323  1.1  christos 	    case STT_FILE:
   1324  1.1  christos 	      sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
   1325  1.1  christos 	      break;
   1326  1.1  christos 	    case STT_FUNC:
   1327  1.1  christos 	      sym->symbol.flags |= BSF_FUNCTION;
   1328  1.1  christos 	      break;
   1329  1.6  christos 	    case STT_COMMON:
   1330  1.1  christos 	      /* FIXME: Do we have to put the size field into the value field
   1331  1.1  christos 		 as we do with symbols in SHN_COMMON sections (see above) ?  */
   1332  1.1  christos 	      sym->symbol.flags |= BSF_ELF_COMMON;
   1333  1.1  christos 	      /* Fall through.  */
   1334  1.1  christos 	    case STT_OBJECT:
   1335  1.1  christos 	      sym->symbol.flags |= BSF_OBJECT;
   1336  1.1  christos 	      break;
   1337  1.1  christos 	    case STT_TLS:
   1338  1.1  christos 	      sym->symbol.flags |= BSF_THREAD_LOCAL;
   1339  1.1  christos 	      break;
   1340  1.1  christos 	    case STT_RELC:
   1341  1.1  christos 	      sym->symbol.flags |= BSF_RELC;
   1342  1.1  christos 	      break;
   1343  1.1  christos 	    case STT_SRELC:
   1344  1.1  christos 	      sym->symbol.flags |= BSF_SRELC;
   1345  1.1  christos 	      break;
   1346  1.1  christos 	    case STT_GNU_IFUNC:
   1347  1.1  christos 	      sym->symbol.flags |= BSF_GNU_INDIRECT_FUNCTION;
   1348  1.1  christos 	      break;
   1349  1.1  christos 	    }
   1350  1.1  christos 
   1351  1.1  christos 	  if (dynamic)
   1352  1.1  christos 	    sym->symbol.flags |= BSF_DYNAMIC;
   1353  1.1  christos 
   1354  1.1  christos 	  if (xver != NULL)
   1355  1.1  christos 	    {
   1356  1.1  christos 	      Elf_Internal_Versym iversym;
   1357  1.1  christos 
   1358  1.1  christos 	      _bfd_elf_swap_versym_in (abfd, xver, &iversym);
   1359  1.1  christos 	      sym->version = iversym.vs_vers;
   1360  1.1  christos 	      xver++;
   1361  1.1  christos 	    }
   1362  1.1  christos 
   1363  1.1  christos 	  /* Do some backend-specific processing on this symbol.  */
   1364  1.1  christos 	  if (ebd->elf_backend_symbol_processing)
   1365  1.1  christos 	    (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
   1366  1.1  christos 	}
   1367  1.1  christos     }
   1368  1.1  christos 
   1369  1.1  christos   /* Do some backend-specific processing on this symbol table.  */
   1370  1.1  christos   if (ebd->elf_backend_symbol_table_processing)
   1371  1.1  christos     (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
   1372  1.1  christos 
   1373  1.1  christos   /* We rely on the zalloc to clear out the final symbol entry.  */
   1374  1.1  christos 
   1375  1.1  christos   symcount = sym - symbase;
   1376  1.1  christos 
   1377  1.1  christos   /* Fill in the user's symbol pointer vector if needed.  */
   1378  1.1  christos   if (symptrs)
   1379  1.1  christos     {
   1380  1.1  christos       long l = symcount;
   1381  1.1  christos 
   1382  1.1  christos       sym = symbase;
   1383  1.1  christos       while (l-- > 0)
   1384  1.1  christos 	{
   1385  1.1  christos 	  *symptrs++ = &sym->symbol;
   1386  1.1  christos 	  sym++;
   1387  1.1  christos 	}
   1388  1.1  christos       *symptrs = 0;		/* Final null pointer */
   1389  1.1  christos     }
   1390  1.1  christos 
   1391  1.1  christos   if (xverbuf != NULL)
   1392  1.1  christos     free (xverbuf);
   1393  1.1  christos   if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
   1394  1.1  christos     free (isymbuf);
   1395  1.1  christos   return symcount;
   1396  1.1  christos 
   1397  1.1  christos error_return:
   1398  1.1  christos   if (xverbuf != NULL)
   1399  1.1  christos     free (xverbuf);
   1400  1.1  christos   if (isymbuf != NULL && hdr->contents != (unsigned char *) isymbuf)
   1401  1.1  christos     free (isymbuf);
   1402  1.1  christos   return -1;
   1403  1.1  christos }
   1404  1.1  christos 
   1405  1.1  christos /* Read relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
   1406  1.1  christos    them.  */
   1407  1.1  christos 
   1408  1.1  christos static bfd_boolean
   1409  1.1  christos elf_slurp_reloc_table_from_section (bfd *abfd,
   1410  1.1  christos 				    asection *asect,
   1411  1.1  christos 				    Elf_Internal_Shdr *rel_hdr,
   1412  1.1  christos 				    bfd_size_type reloc_count,
   1413  1.1  christos 				    arelent *relents,
   1414  1.1  christos 				    asymbol **symbols,
   1415  1.1  christos 				    bfd_boolean dynamic)
   1416  1.1  christos {
   1417  1.1  christos   const struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
   1418  1.1  christos   void *allocated = NULL;
   1419  1.1  christos   bfd_byte *native_relocs;
   1420  1.1  christos   arelent *relent;
   1421  1.1  christos   unsigned int i;
   1422  1.1  christos   int entsize;
   1423  1.1  christos   unsigned int symcount;
   1424  1.1  christos 
   1425  1.1  christos   allocated = bfd_malloc (rel_hdr->sh_size);
   1426  1.1  christos   if (allocated == NULL)
   1427  1.1  christos     goto error_return;
   1428  1.1  christos 
   1429  1.1  christos   if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
   1430  1.1  christos       || (bfd_bread (allocated, rel_hdr->sh_size, abfd)
   1431  1.1  christos 	  != rel_hdr->sh_size))
   1432  1.1  christos     goto error_return;
   1433  1.1  christos 
   1434  1.1  christos   native_relocs = (bfd_byte *) allocated;
   1435  1.1  christos 
   1436  1.1  christos   entsize = rel_hdr->sh_entsize;
   1437  1.1  christos   BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
   1438  1.1  christos 	      || entsize == sizeof (Elf_External_Rela));
   1439  1.1  christos 
   1440  1.1  christos   if (dynamic)
   1441  1.1  christos     symcount = bfd_get_dynamic_symcount (abfd);
   1442  1.1  christos   else
   1443  1.1  christos     symcount = bfd_get_symcount (abfd);
   1444  1.1  christos 
   1445  1.1  christos   for (i = 0, relent = relents;
   1446  1.8  christos        i < reloc_count;
   1447  1.1  christos        i++, relent++, native_relocs += entsize)
   1448  1.1  christos     {
   1449  1.1  christos       bfd_boolean res;
   1450  1.1  christos       Elf_Internal_Rela rela;
   1451  1.1  christos 
   1452  1.1  christos       if (entsize == sizeof (Elf_External_Rela))
   1453  1.1  christos 	elf_swap_reloca_in (abfd, native_relocs, &rela);
   1454  1.1  christos       else
   1455  1.1  christos 	elf_swap_reloc_in (abfd, native_relocs, &rela);
   1456  1.1  christos 
   1457  1.1  christos       /* The address of an ELF reloc is section relative for an object
   1458  1.1  christos 	 file, and absolute for an executable file or shared library.
   1459  1.1  christos 	 The address of a normal BFD reloc is always section relative,
   1460  1.1  christos 	 and the address of a dynamic reloc is absolute..  */
   1461  1.1  christos       if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
   1462  1.1  christos 	relent->address = rela.r_offset;
   1463  1.1  christos       else
   1464  1.8  christos 	relent->address = rela.r_offset - asect->vma;
   1465  1.8  christos 
   1466  1.1  christos       if (ELF_R_SYM (rela.r_info) == STN_UNDEF)
   1467  1.1  christos 	/* FIXME: This and the error case below mean that we have a
   1468  1.1  christos 	   symbol on relocs that is not elf_symbol_type.  */
   1469  1.7  christos 	relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
   1470  1.7  christos       else if (ELF_R_SYM (rela.r_info) > symcount)
   1471  1.8  christos 	{
   1472  1.8  christos 	  _bfd_error_handler
   1473  1.8  christos 	    /* xgettext:c-format */
   1474  1.1  christos 	    (_("%pB(%pA): relocation %d has invalid symbol index %ld"),
   1475  1.1  christos 	     abfd, asect, i, (long) ELF_R_SYM (rela.r_info));
   1476  1.1  christos 	  bfd_set_error (bfd_error_bad_value);
   1477  1.1  christos 	  relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
   1478  1.1  christos 	}
   1479  1.1  christos       else
   1480  1.1  christos 	{
   1481  1.1  christos 	  asymbol **ps;
   1482  1.1  christos 
   1483  1.1  christos 	  ps = symbols + ELF_R_SYM (rela.r_info) - 1;
   1484  1.1  christos 
   1485  1.1  christos 	  relent->sym_ptr_ptr = ps;
   1486  1.1  christos 	}
   1487  1.1  christos 
   1488  1.1  christos       relent->addend = rela.r_addend;
   1489  1.1  christos 
   1490  1.8  christos       if ((entsize == sizeof (Elf_External_Rela)
   1491  1.1  christos 	   && ebd->elf_info_to_howto != NULL)
   1492  1.8  christos 	  || ebd->elf_info_to_howto_rel == NULL)
   1493  1.8  christos 	res = ebd->elf_info_to_howto (abfd, relent, &rela);
   1494  1.8  christos       else
   1495  1.8  christos 	res = ebd->elf_info_to_howto_rel (abfd, relent, &rela);
   1496  1.1  christos 
   1497  1.1  christos       if (! res || relent->howto == NULL)
   1498  1.1  christos 	goto error_return;
   1499  1.1  christos     }
   1500  1.1  christos 
   1501  1.1  christos   if (allocated != NULL)
   1502  1.1  christos     free (allocated);
   1503  1.1  christos   return TRUE;
   1504  1.1  christos 
   1505  1.1  christos  error_return:
   1506  1.1  christos   if (allocated != NULL)
   1507  1.1  christos     free (allocated);
   1508  1.1  christos   return FALSE;
   1509  1.1  christos }
   1510  1.1  christos 
   1511  1.1  christos /* Read in and swap the external relocs.  */
   1512  1.1  christos 
   1513  1.1  christos bfd_boolean
   1514  1.1  christos elf_slurp_reloc_table (bfd *abfd,
   1515  1.1  christos 		       asection *asect,
   1516  1.1  christos 		       asymbol **symbols,
   1517  1.1  christos 		       bfd_boolean dynamic)
   1518  1.1  christos {
   1519  1.1  christos   struct bfd_elf_section_data * const d = elf_section_data (asect);
   1520  1.1  christos   Elf_Internal_Shdr *rel_hdr;
   1521  1.1  christos   Elf_Internal_Shdr *rel_hdr2;
   1522  1.1  christos   bfd_size_type reloc_count;
   1523  1.1  christos   bfd_size_type reloc_count2;
   1524  1.1  christos   arelent *relents;
   1525  1.1  christos   bfd_size_type amt;
   1526  1.1  christos 
   1527  1.1  christos   if (asect->relocation != NULL)
   1528  1.1  christos     return TRUE;
   1529  1.1  christos 
   1530  1.1  christos   if (! dynamic)
   1531  1.1  christos     {
   1532  1.1  christos       if ((asect->flags & SEC_RELOC) == 0
   1533  1.1  christos 	  || asect->reloc_count == 0)
   1534  1.1  christos 	return TRUE;
   1535  1.1  christos 
   1536  1.1  christos       rel_hdr = d->rel.hdr;
   1537  1.1  christos       reloc_count = rel_hdr ? NUM_SHDR_ENTRIES (rel_hdr) : 0;
   1538  1.3  christos       rel_hdr2 = d->rela.hdr;
   1539  1.3  christos       reloc_count2 = rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0;
   1540  1.3  christos 
   1541  1.1  christos       /* PR 17512: file: 0b4f81b7.  */
   1542  1.1  christos       if (asect->reloc_count != reloc_count + reloc_count2)
   1543  1.1  christos 	return FALSE;
   1544  1.1  christos       BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
   1545  1.1  christos 		  || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
   1546  1.1  christos 
   1547  1.1  christos     }
   1548  1.1  christos   else
   1549  1.1  christos     {
   1550  1.1  christos       /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
   1551  1.1  christos 	 case because relocations against this section may use the
   1552  1.1  christos 	 dynamic symbol table, and in that case bfd_section_from_shdr
   1553  1.1  christos 	 in elf.c does not update the RELOC_COUNT.  */
   1554  1.1  christos       if (asect->size == 0)
   1555  1.1  christos 	return TRUE;
   1556  1.1  christos 
   1557  1.1  christos       rel_hdr = &d->this_hdr;
   1558  1.1  christos       reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
   1559  1.1  christos       rel_hdr2 = NULL;
   1560  1.1  christos       reloc_count2 = 0;
   1561  1.1  christos     }
   1562  1.1  christos 
   1563  1.1  christos   amt = (reloc_count + reloc_count2) * sizeof (arelent);
   1564  1.1  christos   relents = (arelent *) bfd_alloc (abfd, amt);
   1565  1.1  christos   if (relents == NULL)
   1566  1.1  christos     return FALSE;
   1567  1.1  christos 
   1568  1.1  christos   if (rel_hdr
   1569  1.1  christos       && !elf_slurp_reloc_table_from_section (abfd, asect,
   1570  1.1  christos 					      rel_hdr, reloc_count,
   1571  1.1  christos 					      relents,
   1572  1.1  christos 					      symbols, dynamic))
   1573  1.1  christos     return FALSE;
   1574  1.1  christos 
   1575  1.1  christos   if (rel_hdr2
   1576  1.1  christos       && !elf_slurp_reloc_table_from_section (abfd, asect,
   1577  1.1  christos 					      rel_hdr2, reloc_count2,
   1578  1.1  christos 					      relents + reloc_count,
   1579  1.1  christos 					      symbols, dynamic))
   1580  1.1  christos     return FALSE;
   1581  1.1  christos 
   1582  1.1  christos   asect->relocation = relents;
   1583  1.1  christos   return TRUE;
   1584  1.1  christos }
   1585  1.1  christos 
   1586  1.1  christos #if DEBUG & 2
   1587  1.1  christos static void
   1588  1.1  christos elf_debug_section (int num, Elf_Internal_Shdr *hdr)
   1589  1.1  christos {
   1590  1.1  christos   fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
   1591  1.1  christos 	   hdr->bfd_section != NULL ? hdr->bfd_section->name : "",
   1592  1.1  christos 	   (long) hdr);
   1593  1.1  christos   fprintf (stderr,
   1594  1.1  christos 	   "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
   1595  1.1  christos 	   (long) hdr->sh_name,
   1596  1.1  christos 	   (long) hdr->sh_type,
   1597  1.1  christos 	   (long) hdr->sh_flags);
   1598  1.1  christos   fprintf (stderr,
   1599  1.1  christos 	   "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
   1600  1.1  christos 	   (long) hdr->sh_addr,
   1601  1.1  christos 	   (long) hdr->sh_offset,
   1602  1.1  christos 	   (long) hdr->sh_size);
   1603  1.1  christos   fprintf (stderr,
   1604  1.1  christos 	   "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
   1605  1.1  christos 	   (long) hdr->sh_link,
   1606  1.1  christos 	   (long) hdr->sh_info,
   1607  1.1  christos 	   (long) hdr->sh_addralign);
   1608  1.1  christos   fprintf (stderr, "sh_entsize   = %ld\n",
   1609  1.1  christos 	   (long) hdr->sh_entsize);
   1610  1.1  christos   fflush (stderr);
   1611  1.1  christos }
   1612  1.1  christos #endif
   1613  1.1  christos 
   1614  1.1  christos #if DEBUG & 1
   1615  1.1  christos static void
   1616  1.1  christos elf_debug_file (Elf_Internal_Ehdr *ehdrp)
   1617  1.1  christos {
   1618  1.1  christos   fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
   1619  1.1  christos   fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
   1620  1.1  christos   fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
   1621  1.1  christos   fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
   1622  1.1  christos   fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
   1623  1.1  christos   fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
   1624  1.1  christos   fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
   1625  1.1  christos }
   1626  1.3  christos #endif
   1627  1.3  christos 
   1628  1.3  christos /* Create a new BFD as if by bfd_openr.  Rather than opening a file,
   1630  1.3  christos    reconstruct an ELF file by reading the segments out of remote
   1631  1.3  christos    memory based on the ELF file header at EHDR_VMA and the ELF program
   1632  1.3  christos    headers it points to.  If non-zero, SIZE is the known extent of the
   1633  1.3  christos    object.  If not null, *LOADBASEP is filled in with the difference
   1634  1.3  christos    between the VMAs from which the segments were read, and the VMAs
   1635  1.3  christos    the file headers (and hence BFD's idea of each section's VMA) put
   1636  1.3  christos    them at.
   1637  1.3  christos 
   1638  1.3  christos    The function TARGET_READ_MEMORY is called to copy LEN bytes from
   1639  1.1  christos    the remote memory at target address VMA into the local buffer at
   1640  1.1  christos    MYADDR; it should return zero on success or an `errno' code on
   1641  1.1  christos    failure.  TEMPL must be a BFD for a target with the word size and
   1642  1.1  christos    byte order found in the remote memory.  */
   1643  1.1  christos 
   1644  1.3  christos bfd *
   1645  1.1  christos NAME(_bfd_elf,bfd_from_remote_memory)
   1646  1.1  christos   (bfd *templ,
   1647  1.1  christos    bfd_vma ehdr_vma,
   1648  1.1  christos    bfd_size_type size,
   1649  1.1  christos    bfd_vma *loadbasep,
   1650  1.1  christos    int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type))
   1651  1.3  christos {
   1652  1.1  christos   Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
   1653  1.1  christos   Elf_Internal_Ehdr i_ehdr;	/* Elf file header, internal form */
   1654  1.1  christos   Elf_External_Phdr *x_phdrs;
   1655  1.1  christos   Elf_Internal_Phdr *i_phdrs, *last_phdr, *first_phdr;
   1656  1.1  christos   bfd *nbfd;
   1657  1.3  christos   struct bfd_in_memory *bim;
   1658  1.3  christos   bfd_byte *contents;
   1659  1.1  christos   int err;
   1660  1.1  christos   unsigned int i;
   1661  1.1  christos   bfd_vma high_offset;
   1662  1.1  christos   bfd_vma shdr_end;
   1663  1.1  christos   bfd_vma loadbase;
   1664  1.1  christos 
   1665  1.1  christos   /* Read in the ELF header in external format.  */
   1666  1.1  christos   err = target_read_memory (ehdr_vma, (bfd_byte *) &x_ehdr, sizeof x_ehdr);
   1667  1.1  christos   if (err)
   1668  1.1  christos     {
   1669  1.1  christos       bfd_set_error (bfd_error_system_call);
   1670  1.1  christos       errno = err;
   1671  1.1  christos       return NULL;
   1672  1.1  christos     }
   1673  1.1  christos 
   1674  1.1  christos   /* Now check to see if we have a valid ELF file, and one that BFD can
   1675  1.1  christos      make use of.  The magic number must match, the address size ('class')
   1676  1.1  christos      and byte-swapping must match our XVEC entry.  */
   1677  1.1  christos 
   1678  1.1  christos   if (! elf_file_p (&x_ehdr)
   1679  1.1  christos       || x_ehdr.e_ident[EI_VERSION] != EV_CURRENT
   1680  1.1  christos       || x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
   1681  1.1  christos     {
   1682  1.1  christos       bfd_set_error (bfd_error_wrong_format);
   1683  1.1  christos       return NULL;
   1684  1.1  christos     }
   1685  1.1  christos 
   1686  1.1  christos   /* Check that file's byte order matches xvec's */
   1687  1.1  christos   switch (x_ehdr.e_ident[EI_DATA])
   1688  1.1  christos     {
   1689  1.1  christos     case ELFDATA2MSB:		/* Big-endian */
   1690  1.1  christos       if (! bfd_header_big_endian (templ))
   1691  1.1  christos 	{
   1692  1.1  christos 	  bfd_set_error (bfd_error_wrong_format);
   1693  1.1  christos 	  return NULL;
   1694  1.1  christos 	}
   1695  1.1  christos       break;
   1696  1.1  christos     case ELFDATA2LSB:		/* Little-endian */
   1697  1.1  christos       if (! bfd_header_little_endian (templ))
   1698  1.1  christos 	{
   1699  1.1  christos 	  bfd_set_error (bfd_error_wrong_format);
   1700  1.1  christos 	  return NULL;
   1701  1.1  christos 	}
   1702  1.1  christos       break;
   1703  1.1  christos     case ELFDATANONE:		/* No data encoding specified */
   1704  1.1  christos     default:			/* Unknown data encoding specified */
   1705  1.1  christos       bfd_set_error (bfd_error_wrong_format);
   1706  1.1  christos       return NULL;
   1707  1.1  christos     }
   1708  1.1  christos 
   1709  1.1  christos   elf_swap_ehdr_in (templ, &x_ehdr, &i_ehdr);
   1710  1.1  christos 
   1711  1.1  christos   /* The file header tells where to find the program headers.
   1712  1.1  christos      These are what we use to actually choose what to read.  */
   1713  1.1  christos 
   1714  1.1  christos   if (i_ehdr.e_phentsize != sizeof (Elf_External_Phdr) || i_ehdr.e_phnum == 0)
   1715  1.1  christos     {
   1716  1.1  christos       bfd_set_error (bfd_error_wrong_format);
   1717  1.1  christos       return NULL;
   1718  1.1  christos     }
   1719  1.3  christos 
   1720  1.1  christos   x_phdrs = (Elf_External_Phdr *)
   1721  1.1  christos       bfd_malloc (i_ehdr.e_phnum * (sizeof *x_phdrs + sizeof *i_phdrs));
   1722  1.1  christos   if (x_phdrs == NULL)
   1723  1.1  christos     return NULL;
   1724  1.1  christos   err = target_read_memory (ehdr_vma + i_ehdr.e_phoff, (bfd_byte *) x_phdrs,
   1725  1.1  christos 			    i_ehdr.e_phnum * sizeof x_phdrs[0]);
   1726  1.1  christos   if (err)
   1727  1.1  christos     {
   1728  1.1  christos       free (x_phdrs);
   1729  1.1  christos       bfd_set_error (bfd_error_system_call);
   1730  1.1  christos       errno = err;
   1731  1.3  christos       return NULL;
   1732  1.3  christos     }
   1733  1.3  christos   i_phdrs = (Elf_Internal_Phdr *) &x_phdrs[i_ehdr.e_phnum];
   1734  1.1  christos 
   1735  1.1  christos   high_offset = 0;
   1736  1.1  christos   loadbase = 0;
   1737  1.1  christos   first_phdr = NULL;
   1738  1.1  christos   last_phdr = NULL;
   1739  1.1  christos   for (i = 0; i < i_ehdr.e_phnum; ++i)
   1740  1.3  christos     {
   1741  1.3  christos       elf_swap_phdr_in (templ, &x_phdrs[i], &i_phdrs[i]);
   1742  1.3  christos       if (i_phdrs[i].p_type == PT_LOAD)
   1743  1.1  christos 	{
   1744  1.3  christos 	  bfd_vma segment_end = i_phdrs[i].p_offset + i_phdrs[i].p_filesz;
   1745  1.3  christos 
   1746  1.1  christos 	  if (segment_end > high_offset)
   1747  1.1  christos 	    {
   1748  1.3  christos 	      high_offset = segment_end;
   1749  1.3  christos 	      last_phdr = &i_phdrs[i];
   1750  1.3  christos 	    }
   1751  1.3  christos 
   1752  1.3  christos 	  /* If this program header covers offset zero, where the file
   1753  1.3  christos 	     header sits, then we can figure out the loadbase.  */
   1754  1.3  christos 	  if (first_phdr == NULL)
   1755  1.3  christos 	    {
   1756  1.3  christos 	      bfd_vma p_offset = i_phdrs[i].p_offset;
   1757  1.3  christos 	      bfd_vma p_vaddr = i_phdrs[i].p_vaddr;
   1758  1.3  christos 
   1759  1.3  christos 	      if (i_phdrs[i].p_align > 1)
   1760  1.3  christos 		{
   1761  1.3  christos 		  p_offset &= -i_phdrs[i].p_align;
   1762  1.3  christos 		  p_vaddr &= -i_phdrs[i].p_align;
   1763  1.3  christos 		}
   1764  1.3  christos 	      if (p_offset == 0)
   1765  1.3  christos 		{
   1766  1.1  christos 		  loadbase = ehdr_vma - p_vaddr;
   1767  1.1  christos 		  first_phdr = &i_phdrs[i];
   1768  1.3  christos 		}
   1769  1.1  christos 	    }
   1770  1.1  christos 	}
   1771  1.1  christos     }
   1772  1.1  christos   if (high_offset == 0)
   1773  1.1  christos     {
   1774  1.1  christos       /* There were no PT_LOAD segments, so we don't have anything to read.  */
   1775  1.1  christos       free (x_phdrs);
   1776  1.3  christos       bfd_set_error (bfd_error_wrong_format);
   1777  1.3  christos       return NULL;
   1778  1.3  christos     }
   1779  1.3  christos 
   1780  1.3  christos   shdr_end = 0;
   1781  1.3  christos   if (i_ehdr.e_shoff != 0 && i_ehdr.e_shnum != 0 && i_ehdr.e_shentsize != 0)
   1782  1.3  christos     {
   1783  1.3  christos       shdr_end = i_ehdr.e_shoff + i_ehdr.e_shnum * i_ehdr.e_shentsize;
   1784  1.3  christos 
   1785  1.3  christos       if (last_phdr->p_filesz != last_phdr->p_memsz)
   1786  1.3  christos 	{
   1787  1.3  christos 	  /* If the last PT_LOAD header has a bss area then ld.so will
   1788  1.3  christos 	     have cleared anything past p_filesz, zapping the section
   1789  1.3  christos 	     headers.  */
   1790  1.3  christos 	}
   1791  1.3  christos       else if (size >= shdr_end)
   1792  1.3  christos 	high_offset = size;
   1793  1.3  christos       else
   1794  1.3  christos 	{
   1795  1.3  christos 	  bfd_vma page_size = get_elf_backend_data (templ)->minpagesize;
   1796  1.3  christos 	  bfd_vma segment_end = last_phdr->p_offset + last_phdr->p_filesz;
   1797  1.3  christos 
   1798  1.3  christos 	  /* Assume we loaded full pages, allowing us to sometimes see
   1799  1.3  christos 	     section headers.  */
   1800  1.3  christos 	  if (page_size > 1 && shdr_end > segment_end)
   1801  1.3  christos 	    {
   1802  1.3  christos 	      bfd_vma page_end = (segment_end + page_size - 1) & -page_size;
   1803  1.3  christos 
   1804  1.3  christos 	      if (page_end >= shdr_end)
   1805  1.1  christos 		/* Whee, section headers covered.  */
   1806  1.1  christos 		high_offset = shdr_end;
   1807  1.1  christos 	    }
   1808  1.3  christos 	}
   1809  1.1  christos     }
   1810  1.1  christos 
   1811  1.1  christos   /* Now we know the size of the whole image we want read in.  */
   1812  1.1  christos   contents = (bfd_byte *) bfd_zmalloc (high_offset);
   1813  1.1  christos   if (contents == NULL)
   1814  1.1  christos     {
   1815  1.1  christos       free (x_phdrs);
   1816  1.1  christos       return NULL;
   1817  1.1  christos     }
   1818  1.3  christos 
   1819  1.3  christos   for (i = 0; i < i_ehdr.e_phnum; ++i)
   1820  1.3  christos     if (i_phdrs[i].p_type == PT_LOAD)
   1821  1.3  christos       {
   1822  1.3  christos 	bfd_vma start = i_phdrs[i].p_offset;
   1823  1.3  christos 	bfd_vma end = start + i_phdrs[i].p_filesz;
   1824  1.3  christos 	bfd_vma vaddr = i_phdrs[i].p_vaddr;
   1825  1.3  christos 
   1826  1.3  christos 	/* Extend the beginning of the first pt_load to cover file
   1827  1.3  christos 	   header and program headers, if we proved earlier that its
   1828  1.3  christos 	   aligned offset is 0.  */
   1829  1.3  christos 	if (first_phdr == &i_phdrs[i])
   1830  1.3  christos 	  {
   1831  1.3  christos 	    vaddr -= start;
   1832  1.3  christos 	    start = 0;
   1833  1.3  christos 	  }
   1834  1.1  christos 	/* Extend the end of the last pt_load to cover section headers.  */
   1835  1.1  christos 	if (last_phdr == &i_phdrs[i])
   1836  1.1  christos 	  end = high_offset;
   1837  1.1  christos 	err = target_read_memory (loadbase + vaddr,
   1838  1.1  christos 				  contents + start, end - start);
   1839  1.1  christos 	if (err)
   1840  1.1  christos 	  {
   1841  1.1  christos 	    free (x_phdrs);
   1842  1.1  christos 	    free (contents);
   1843  1.1  christos 	    bfd_set_error (bfd_error_system_call);
   1844  1.1  christos 	    errno = err;
   1845  1.1  christos 	    return NULL;
   1846  1.1  christos 	  }
   1847  1.1  christos       }
   1848  1.3  christos   free (x_phdrs);
   1849  1.1  christos 
   1850  1.1  christos   /* If the segments visible in memory didn't include the section headers,
   1851  1.1  christos      then clear them from the file header.  */
   1852  1.1  christos   if (high_offset < shdr_end)
   1853  1.1  christos     {
   1854  1.1  christos       memset (&x_ehdr.e_shoff, 0, sizeof x_ehdr.e_shoff);
   1855  1.1  christos       memset (&x_ehdr.e_shnum, 0, sizeof x_ehdr.e_shnum);
   1856  1.1  christos       memset (&x_ehdr.e_shstrndx, 0, sizeof x_ehdr.e_shstrndx);
   1857  1.1  christos     }
   1858  1.1  christos 
   1859  1.1  christos   /* This will normally have been in the first PT_LOAD segment.  But it
   1860  1.1  christos      conceivably could be missing, and we might have just changed it.  */
   1861  1.1  christos   memcpy (contents, &x_ehdr, sizeof x_ehdr);
   1862  1.1  christos 
   1863  1.1  christos   /* Now we have a memory image of the ELF file contents.  Make a BFD.  */
   1864  1.1  christos   bim = (struct bfd_in_memory *) bfd_malloc (sizeof (struct bfd_in_memory));
   1865  1.1  christos   if (bim == NULL)
   1866  1.1  christos     {
   1867  1.1  christos       free (contents);
   1868  1.1  christos       return NULL;
   1869  1.1  christos     }
   1870  1.1  christos   nbfd = _bfd_new_bfd ();
   1871  1.1  christos   if (nbfd == NULL)
   1872  1.1  christos     {
   1873  1.1  christos       free (bim);
   1874  1.1  christos       free (contents);
   1875  1.3  christos       return NULL;
   1876  1.1  christos     }
   1877  1.1  christos   nbfd->filename = xstrdup ("<in-memory>");
   1878  1.1  christos   nbfd->xvec = templ->xvec;
   1879  1.1  christos   bim->size = high_offset;
   1880  1.1  christos   bim->buffer = contents;
   1881  1.1  christos   nbfd->iostream = bim;
   1882  1.1  christos   nbfd->flags = BFD_IN_MEMORY;
   1883  1.1  christos   nbfd->iovec = &_bfd_memory_iovec;
   1884  1.1  christos   nbfd->origin = 0;
   1885  1.1  christos   nbfd->direction = read_direction;
   1886  1.1  christos   nbfd->mtime = time (NULL);
   1887  1.1  christos   nbfd->mtime_set = TRUE;
   1888  1.1  christos 
   1889  1.1  christos   if (loadbasep)
   1890  1.1  christos     *loadbasep = loadbase;
   1891  1.1  christos   return nbfd;
   1892  1.1  christos }
   1893  1.1  christos 
   1894  1.1  christos /* Function for ELF_R_INFO.  */
   1895  1.1  christos 
   1896  1.1  christos bfd_vma
   1897  1.1  christos NAME(elf,r_info) (bfd_vma sym, bfd_vma type)
   1898  1.1  christos {
   1899  1.1  christos   return ELF_R_INFO (sym, type);
   1900  1.1  christos }
   1901  1.1  christos 
   1902  1.1  christos /* Function for ELF_R_SYM.  */
   1903  1.1  christos 
   1904  1.1  christos bfd_vma
   1905  1.1  christos NAME(elf,r_sym) (bfd_vma r_info)
   1906  1.1  christos {
   1907  1.1  christos   return ELF_R_SYM (r_info);
   1908  1.1  christos }
   1909  1.1  christos 
   1910  1.1  christos #include "elfcore.h"
   1912  1.1  christos 
   1913  1.1  christos /* Size-dependent data and functions.  */
   1915  1.1  christos const struct elf_size_info NAME(_bfd_elf,size_info) = {
   1916  1.1  christos   sizeof (Elf_External_Ehdr),
   1917  1.1  christos   sizeof (Elf_External_Phdr),
   1918  1.1  christos   sizeof (Elf_External_Shdr),
   1919  1.1  christos   sizeof (Elf_External_Rel),
   1920  1.1  christos   sizeof (Elf_External_Rela),
   1921  1.1  christos   sizeof (Elf_External_Sym),
   1922  1.1  christos   sizeof (Elf_External_Dyn),
   1923  1.1  christos   sizeof (Elf_External_Note),
   1924  1.1  christos   4,
   1925  1.1  christos   1,
   1926  1.1  christos   ARCH_SIZE, LOG_FILE_ALIGN,
   1927  1.1  christos   ELFCLASS, EV_CURRENT,
   1928  1.1  christos   elf_write_out_phdrs,
   1929  1.1  christos   elf_write_shdrs_and_ehdr,
   1930  1.1  christos   elf_checksum_contents,
   1931  1.1  christos   elf_write_relocs,
   1932  1.1  christos   elf_swap_symbol_in,
   1933  1.1  christos   elf_swap_symbol_out,
   1934  1.1  christos   elf_slurp_reloc_table,
   1935  1.1  christos   elf_slurp_symbol_table,
   1936  1.1  christos   elf_swap_dyn_in,
   1937                  elf_swap_dyn_out,
   1938                  elf_swap_reloc_in,
   1939                  elf_swap_reloc_out,
   1940                  elf_swap_reloca_in,
   1941                  elf_swap_reloca_out
   1942                };
   1943