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