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