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