Commit | Line | Data |
---|---|---|
244ffee7 | 1 | /* ELF executable support for BFD. |
b9d5cdf0 | 2 | Copyright 1991, 1992, 1993, 1994 Free Software Foundation, Inc. |
244ffee7 JK |
3 | |
4 | Written by Fred Fish @ Cygnus Support, from information published | |
5 | in "UNIX System V Release 4, Programmers Guide: ANSI C and | |
6 | Programming Support Tools". Sufficient support for gdb. | |
7 | ||
8 | Rewritten by Mark Eichin @ Cygnus Support, from information | |
9 | published in "System V Application Binary Interface", chapters 4 | |
10 | and 5, as well as the various "Processor Supplement" documents | |
11 | derived from it. Added support for assembler and other object file | |
12 | utilities. Further work done by Ken Raeburn (Cygnus Support), Michael | |
13 | Meissner (Open Software Foundation), and Peter Hoogenboom (University | |
14 | of Utah) to finish and extend this. | |
15 | ||
16 | This file is part of BFD, the Binary File Descriptor library. | |
17 | ||
18 | This program is free software; you can redistribute it and/or modify | |
19 | it under the terms of the GNU General Public License as published by | |
20 | the Free Software Foundation; either version 2 of the License, or | |
21 | (at your option) any later version. | |
22 | ||
23 | This program is distributed in the hope that it will be useful, | |
24 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
26 | GNU General Public License for more details. | |
27 | ||
28 | You should have received a copy of the GNU General Public License | |
29 | along with this program; if not, write to the Free Software | |
30 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
31 | ||
244ffee7 JK |
32 | /* Problems and other issues to resolve. |
33 | ||
34 | (1) BFD expects there to be some fixed number of "sections" in | |
35 | the object file. I.E. there is a "section_count" variable in the | |
36 | bfd structure which contains the number of sections. However, ELF | |
37 | supports multiple "views" of a file. In particular, with current | |
38 | implementations, executable files typically have two tables, a | |
39 | program header table and a section header table, both of which | |
40 | partition the executable. | |
41 | ||
42 | In ELF-speak, the "linking view" of the file uses the section header | |
43 | table to access "sections" within the file, and the "execution view" | |
44 | uses the program header table to access "segments" within the file. | |
45 | "Segments" typically may contain all the data from one or more | |
46 | "sections". | |
47 | ||
48 | Note that the section header table is optional in ELF executables, | |
49 | but it is this information that is most useful to gdb. If the | |
50 | section header table is missing, then gdb should probably try | |
51 | to make do with the program header table. (FIXME) | |
52 | ||
6a3eb9b6 KR |
53 | (2) The code in this file is compiled twice, once in 32-bit mode and |
54 | once in 64-bit mode. More of it should be made size-independent | |
55 | and moved into elf.c. | |
56 | ||
d24928c0 KR |
57 | (3) ELF section symbols are handled rather sloppily now. This should |
58 | be cleaned up, and ELF section symbols reconciled with BFD section | |
59 | symbols. | |
60 | */ | |
244ffee7 JK |
61 | |
62 | #include <string.h> /* For strrchr and friends */ | |
63 | #include "bfd.h" | |
64 | #include "sysdep.h" | |
6ec3bb6a | 65 | #include "bfdlink.h" |
244ffee7 JK |
66 | #include "libbfd.h" |
67 | #include "libelf.h" | |
68 | ||
32090b8e | 69 | /* Renaming structures, typedefs, macros and functions to be size-specific. */ |
244ffee7 | 70 | #define Elf_External_Ehdr NAME(Elf,External_Ehdr) |
244ffee7 | 71 | #define Elf_External_Sym NAME(Elf,External_Sym) |
244ffee7 | 72 | #define Elf_External_Shdr NAME(Elf,External_Shdr) |
244ffee7 | 73 | #define Elf_External_Phdr NAME(Elf,External_Phdr) |
244ffee7 JK |
74 | #define Elf_External_Rel NAME(Elf,External_Rel) |
75 | #define Elf_External_Rela NAME(Elf,External_Rela) | |
244ffee7 | 76 | |
244ffee7 JK |
77 | #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command) |
78 | #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal) | |
cb71adf1 PS |
79 | #define elf_core_file_matches_executable_p \ |
80 | NAME(bfd_elf,core_file_matches_executable_p) | |
244ffee7 JK |
81 | #define elf_object_p NAME(bfd_elf,object_p) |
82 | #define elf_core_file_p NAME(bfd_elf,core_file_p) | |
244ffee7 | 83 | #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound) |
cb71adf1 PS |
84 | #define elf_get_dynamic_symtab_upper_bound \ |
85 | NAME(bfd_elf,get_dynamic_symtab_upper_bound) | |
244ffee7 JK |
86 | #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound) |
87 | #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc) | |
88 | #define elf_get_symtab NAME(bfd_elf,get_symtab) | |
cb71adf1 PS |
89 | #define elf_canonicalize_dynamic_symtab \ |
90 | NAME(bfd_elf,canonicalize_dynamic_symtab) | |
244ffee7 JK |
91 | #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol) |
92 | #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info) | |
93 | #define elf_print_symbol NAME(bfd_elf,print_symbol) | |
94 | #define elf_get_lineno NAME(bfd_elf,get_lineno) | |
95 | #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach) | |
96 | #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line) | |
97 | #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers) | |
98 | #define elf_set_section_contents NAME(bfd_elf,set_section_contents) | |
99 | #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto) | |
100 | #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel) | |
fce36137 | 101 | #define elf_new_section_hook NAME(bfd_elf,new_section_hook) |
32090b8e | 102 | #define write_relocs NAME(bfd_elf,_write_relocs) |
f035cc47 | 103 | #define elf_find_section NAME(bfd_elf,find_section) |
6ec3bb6a ILT |
104 | #define elf_bfd_link_add_symbols NAME(bfd_elf,bfd_link_add_symbols) |
105 | #define elf_bfd_final_link NAME(bfd_elf,bfd_final_link) | |
244ffee7 | 106 | |
6a3eb9b6 KR |
107 | #if ARCH_SIZE == 64 |
108 | #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y) | |
109 | #define ELF_R_SYM(X) ELF64_R_SYM(X) | |
6ec3bb6a | 110 | #define ELF_R_TYPE(X) ELF64_R_TYPE(X) |
32090b8e | 111 | #define ELFCLASS ELFCLASS64 |
f035cc47 | 112 | #define FILE_ALIGN 8 |
6a3eb9b6 KR |
113 | #endif |
114 | #if ARCH_SIZE == 32 | |
115 | #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y) | |
116 | #define ELF_R_SYM(X) ELF32_R_SYM(X) | |
6ec3bb6a | 117 | #define ELF_R_TYPE(X) ELF32_R_TYPE(X) |
32090b8e | 118 | #define ELFCLASS ELFCLASS32 |
f035cc47 | 119 | #define FILE_ALIGN 4 |
244ffee7 JK |
120 | #endif |
121 | ||
244ffee7 JK |
122 | /* Forward declarations of static functions */ |
123 | ||
6ec3bb6a ILT |
124 | static void elf_swap_reloc_in |
125 | PARAMS ((bfd *, Elf_External_Rel *, Elf_Internal_Rel *)); | |
126 | static void elf_swap_reloc_out | |
127 | PARAMS ((bfd *, Elf_Internal_Rel *, Elf_External_Rel *)); | |
128 | static void elf_swap_reloca_in | |
129 | PARAMS ((bfd *, Elf_External_Rela *, Elf_Internal_Rela *)); | |
130 | static void elf_swap_reloca_out | |
131 | PARAMS ((bfd *, Elf_Internal_Rela *, Elf_External_Rela *)); | |
132 | ||
133 | static unsigned long bfd_add_to_strtab | |
134 | PARAMS ((bfd *, struct strtab *, const char *)); | |
2e03ce18 | 135 | static asection *section_from_elf_index PARAMS ((bfd *, unsigned int)); |
244ffee7 JK |
136 | |
137 | static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *)); | |
138 | ||
cb71adf1 | 139 | static long elf_slurp_symbol_table PARAMS ((bfd *, asymbol **, boolean)); |
244ffee7 | 140 | |
244ffee7 | 141 | static int elf_symbol_from_bfd_symbol PARAMS ((bfd *, |
1c6042ee | 142 | struct symbol_cache_entry **)); |
244ffee7 | 143 | |
6ec3bb6a ILT |
144 | static boolean elf_compute_section_file_positions |
145 | PARAMS ((bfd *, struct bfd_link_info *)); | |
146 | static boolean prep_headers PARAMS ((bfd *)); | |
147 | static boolean assign_section_numbers PARAMS ((bfd *)); | |
148 | static boolean assign_file_positions_except_relocs PARAMS ((bfd *, boolean)); | |
149 | static INLINE file_ptr assign_file_positions_for_symtab_and_strtabs | |
150 | PARAMS ((bfd *, file_ptr, boolean)); | |
151 | ||
9783e04a | 152 | static boolean elf_map_symbols PARAMS ((bfd *)); |
b9d5cdf0 | 153 | static boolean swap_out_syms PARAMS ((bfd *)); |
244ffee7 | 154 | |
2e03ce18 ILT |
155 | static boolean bfd_section_from_shdr PARAMS ((bfd *, unsigned int shindex)); |
156 | ||
6a3eb9b6 KR |
157 | #ifdef DEBUG |
158 | static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *)); | |
159 | static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *)); | |
160 | #endif | |
238ac6ec | 161 | |
32090b8e KR |
162 | #define elf_string_from_elf_strtab(abfd,strindex) \ |
163 | elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex) | |
32090b8e | 164 | \f |
1c6042ee | 165 | |
32090b8e KR |
166 | /* Structure swapping routines */ |
167 | ||
6a3eb9b6 KR |
168 | /* Should perhaps use put_offset, put_word, etc. For now, the two versions |
169 | can be handled by explicitly specifying 32 bits or "the long type". */ | |
238ac6ec KR |
170 | #if ARCH_SIZE == 64 |
171 | #define put_word bfd_h_put_64 | |
172 | #define get_word bfd_h_get_64 | |
173 | #endif | |
174 | #if ARCH_SIZE == 32 | |
175 | #define put_word bfd_h_put_32 | |
176 | #define get_word bfd_h_get_32 | |
177 | #endif | |
178 | ||
244ffee7 JK |
179 | /* Translate an ELF symbol in external format into an ELF symbol in internal |
180 | format. */ | |
181 | ||
182 | static void | |
1c6042ee ILT |
183 | elf_swap_symbol_in (abfd, src, dst) |
184 | bfd *abfd; | |
185 | Elf_External_Sym *src; | |
186 | Elf_Internal_Sym *dst; | |
244ffee7 JK |
187 | { |
188 | dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name); | |
238ac6ec KR |
189 | dst->st_value = get_word (abfd, (bfd_byte *) src->st_value); |
190 | dst->st_size = get_word (abfd, (bfd_byte *) src->st_size); | |
244ffee7 JK |
191 | dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info); |
192 | dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other); | |
193 | dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx); | |
194 | } | |
195 | ||
196 | /* Translate an ELF symbol in internal format into an ELF symbol in external | |
197 | format. */ | |
198 | ||
199 | static void | |
1c6042ee ILT |
200 | elf_swap_symbol_out (abfd, src, dst) |
201 | bfd *abfd; | |
202 | Elf_Internal_Sym *src; | |
203 | Elf_External_Sym *dst; | |
244ffee7 JK |
204 | { |
205 | bfd_h_put_32 (abfd, src->st_name, dst->st_name); | |
238ac6ec KR |
206 | put_word (abfd, src->st_value, dst->st_value); |
207 | put_word (abfd, src->st_size, dst->st_size); | |
244ffee7 JK |
208 | bfd_h_put_8 (abfd, src->st_info, dst->st_info); |
209 | bfd_h_put_8 (abfd, src->st_other, dst->st_other); | |
210 | bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx); | |
211 | } | |
212 | ||
213 | ||
214 | /* Translate an ELF file header in external format into an ELF file header in | |
215 | internal format. */ | |
216 | ||
217 | static void | |
1c6042ee ILT |
218 | elf_swap_ehdr_in (abfd, src, dst) |
219 | bfd *abfd; | |
220 | Elf_External_Ehdr *src; | |
221 | Elf_Internal_Ehdr *dst; | |
244ffee7 JK |
222 | { |
223 | memcpy (dst->e_ident, src->e_ident, EI_NIDENT); | |
224 | dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type); | |
225 | dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine); | |
226 | dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version); | |
238ac6ec KR |
227 | dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry); |
228 | dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff); | |
229 | dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff); | |
244ffee7 JK |
230 | dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags); |
231 | dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize); | |
232 | dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize); | |
233 | dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum); | |
234 | dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize); | |
235 | dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum); | |
236 | dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx); | |
237 | } | |
238 | ||
239 | /* Translate an ELF file header in internal format into an ELF file header in | |
240 | external format. */ | |
241 | ||
242 | static void | |
1c6042ee ILT |
243 | elf_swap_ehdr_out (abfd, src, dst) |
244 | bfd *abfd; | |
245 | Elf_Internal_Ehdr *src; | |
246 | Elf_External_Ehdr *dst; | |
244ffee7 JK |
247 | { |
248 | memcpy (dst->e_ident, src->e_ident, EI_NIDENT); | |
249 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
250 | bfd_h_put_16 (abfd, src->e_type, dst->e_type); | |
251 | bfd_h_put_16 (abfd, src->e_machine, dst->e_machine); | |
252 | bfd_h_put_32 (abfd, src->e_version, dst->e_version); | |
238ac6ec KR |
253 | put_word (abfd, src->e_entry, dst->e_entry); |
254 | put_word (abfd, src->e_phoff, dst->e_phoff); | |
255 | put_word (abfd, src->e_shoff, dst->e_shoff); | |
244ffee7 JK |
256 | bfd_h_put_32 (abfd, src->e_flags, dst->e_flags); |
257 | bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize); | |
258 | bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize); | |
259 | bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum); | |
260 | bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize); | |
261 | bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum); | |
262 | bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx); | |
263 | } | |
264 | ||
265 | ||
266 | /* Translate an ELF section header table entry in external format into an | |
267 | ELF section header table entry in internal format. */ | |
268 | ||
269 | static void | |
1c6042ee ILT |
270 | elf_swap_shdr_in (abfd, src, dst) |
271 | bfd *abfd; | |
272 | Elf_External_Shdr *src; | |
273 | Elf_Internal_Shdr *dst; | |
244ffee7 JK |
274 | { |
275 | dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name); | |
276 | dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type); | |
238ac6ec KR |
277 | dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags); |
278 | dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr); | |
279 | dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset); | |
280 | dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size); | |
244ffee7 JK |
281 | dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link); |
282 | dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info); | |
238ac6ec KR |
283 | dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign); |
284 | dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize); | |
244ffee7 JK |
285 | /* we haven't done any processing on it yet, so... */ |
286 | dst->rawdata = (void *) 0; | |
287 | } | |
288 | ||
289 | /* Translate an ELF section header table entry in internal format into an | |
290 | ELF section header table entry in external format. */ | |
291 | ||
292 | static void | |
1c6042ee ILT |
293 | elf_swap_shdr_out (abfd, src, dst) |
294 | bfd *abfd; | |
295 | Elf_Internal_Shdr *src; | |
296 | Elf_External_Shdr *dst; | |
244ffee7 JK |
297 | { |
298 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
299 | bfd_h_put_32 (abfd, src->sh_name, dst->sh_name); | |
300 | bfd_h_put_32 (abfd, src->sh_type, dst->sh_type); | |
238ac6ec KR |
301 | put_word (abfd, src->sh_flags, dst->sh_flags); |
302 | put_word (abfd, src->sh_addr, dst->sh_addr); | |
303 | put_word (abfd, src->sh_offset, dst->sh_offset); | |
304 | put_word (abfd, src->sh_size, dst->sh_size); | |
244ffee7 JK |
305 | bfd_h_put_32 (abfd, src->sh_link, dst->sh_link); |
306 | bfd_h_put_32 (abfd, src->sh_info, dst->sh_info); | |
238ac6ec KR |
307 | put_word (abfd, src->sh_addralign, dst->sh_addralign); |
308 | put_word (abfd, src->sh_entsize, dst->sh_entsize); | |
244ffee7 JK |
309 | } |
310 | ||
311 | ||
312 | /* Translate an ELF program header table entry in external format into an | |
313 | ELF program header table entry in internal format. */ | |
314 | ||
315 | static void | |
1c6042ee ILT |
316 | elf_swap_phdr_in (abfd, src, dst) |
317 | bfd *abfd; | |
318 | Elf_External_Phdr *src; | |
319 | Elf_Internal_Phdr *dst; | |
244ffee7 JK |
320 | { |
321 | dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type); | |
244ffee7 | 322 | dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags); |
238ac6ec KR |
323 | dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset); |
324 | dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr); | |
325 | dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr); | |
326 | dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz); | |
327 | dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz); | |
328 | dst->p_align = get_word (abfd, (bfd_byte *) src->p_align); | |
244ffee7 JK |
329 | } |
330 | ||
244ffee7 | 331 | static void |
1c6042ee ILT |
332 | elf_swap_phdr_out (abfd, src, dst) |
333 | bfd *abfd; | |
334 | Elf_Internal_Phdr *src; | |
335 | Elf_External_Phdr *dst; | |
244ffee7 JK |
336 | { |
337 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
338 | bfd_h_put_32 (abfd, src->p_type, dst->p_type); | |
94dbb655 KR |
339 | put_word (abfd, src->p_offset, dst->p_offset); |
340 | put_word (abfd, src->p_vaddr, dst->p_vaddr); | |
341 | put_word (abfd, src->p_paddr, dst->p_paddr); | |
342 | put_word (abfd, src->p_filesz, dst->p_filesz); | |
343 | put_word (abfd, src->p_memsz, dst->p_memsz); | |
244ffee7 | 344 | bfd_h_put_32 (abfd, src->p_flags, dst->p_flags); |
94dbb655 | 345 | put_word (abfd, src->p_align, dst->p_align); |
244ffee7 JK |
346 | } |
347 | ||
348 | /* Translate an ELF reloc from external format to internal format. */ | |
32090b8e | 349 | static INLINE void |
1c6042ee ILT |
350 | elf_swap_reloc_in (abfd, src, dst) |
351 | bfd *abfd; | |
352 | Elf_External_Rel *src; | |
353 | Elf_Internal_Rel *dst; | |
244ffee7 | 354 | { |
94dbb655 KR |
355 | dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset); |
356 | dst->r_info = get_word (abfd, (bfd_byte *) src->r_info); | |
244ffee7 JK |
357 | } |
358 | ||
32090b8e | 359 | static INLINE void |
1c6042ee ILT |
360 | elf_swap_reloca_in (abfd, src, dst) |
361 | bfd *abfd; | |
362 | Elf_External_Rela *src; | |
363 | Elf_Internal_Rela *dst; | |
244ffee7 | 364 | { |
94dbb655 KR |
365 | dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset); |
366 | dst->r_info = get_word (abfd, (bfd_byte *) src->r_info); | |
367 | dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend); | |
244ffee7 JK |
368 | } |
369 | ||
370 | /* Translate an ELF reloc from internal format to external format. */ | |
32090b8e | 371 | static INLINE void |
1c6042ee ILT |
372 | elf_swap_reloc_out (abfd, src, dst) |
373 | bfd *abfd; | |
374 | Elf_Internal_Rel *src; | |
375 | Elf_External_Rel *dst; | |
244ffee7 | 376 | { |
94dbb655 KR |
377 | put_word (abfd, src->r_offset, dst->r_offset); |
378 | put_word (abfd, src->r_info, dst->r_info); | |
244ffee7 JK |
379 | } |
380 | ||
32090b8e | 381 | static INLINE void |
1c6042ee ILT |
382 | elf_swap_reloca_out (abfd, src, dst) |
383 | bfd *abfd; | |
384 | Elf_Internal_Rela *src; | |
385 | Elf_External_Rela *dst; | |
244ffee7 | 386 | { |
94dbb655 KR |
387 | put_word (abfd, src->r_offset, dst->r_offset); |
388 | put_word (abfd, src->r_info, dst->r_info); | |
389 | put_word (abfd, src->r_addend, dst->r_addend); | |
244ffee7 | 390 | } |
32090b8e KR |
391 | \f |
392 | ||
1c6042ee | 393 | |
32090b8e KR |
394 | /* String table creation/manipulation routines */ |
395 | ||
396 | static struct strtab * | |
1c6042ee ILT |
397 | bfd_new_strtab (abfd) |
398 | bfd *abfd; | |
32090b8e KR |
399 | { |
400 | struct strtab *ss; | |
401 | ||
b9d5cdf0 DM |
402 | ss = (struct strtab *) malloc (sizeof (struct strtab)); |
403 | if (!ss) | |
404 | { | |
d1ad85a6 | 405 | bfd_set_error (bfd_error_no_memory); |
b9d5cdf0 DM |
406 | return NULL; |
407 | } | |
408 | ss->tab = malloc (1); | |
409 | if (!ss->tab) | |
410 | { | |
d1ad85a6 | 411 | bfd_set_error (bfd_error_no_memory); |
b9d5cdf0 DM |
412 | return NULL; |
413 | } | |
32090b8e KR |
414 | *ss->tab = 0; |
415 | ss->nentries = 0; | |
416 | ss->length = 1; | |
244ffee7 | 417 | |
32090b8e KR |
418 | return ss; |
419 | } | |
420 | ||
6ec3bb6a | 421 | static unsigned long |
1c6042ee ILT |
422 | bfd_add_to_strtab (abfd, ss, str) |
423 | bfd *abfd; | |
424 | struct strtab *ss; | |
6ec3bb6a | 425 | const char *str; |
32090b8e KR |
426 | { |
427 | /* should search first, but for now: */ | |
428 | /* include the trailing NUL */ | |
429 | int ln = strlen (str) + 1; | |
430 | ||
6ec3bb6a ILT |
431 | /* FIXME: This is slow. Also, we could combine this with the a.out |
432 | string table building and use a hash table, although it might not | |
433 | be worth it since ELF symbols don't include debugging information | |
434 | and thus have much less overlap. */ | |
32090b8e | 435 | ss->tab = realloc (ss->tab, ss->length + ln); |
6ec3bb6a ILT |
436 | if (ss->tab == NULL) |
437 | { | |
438 | bfd_set_error (bfd_error_no_memory); | |
439 | return (unsigned long) -1; | |
440 | } | |
32090b8e | 441 | |
32090b8e KR |
442 | strcpy (ss->tab + ss->length, str); |
443 | ss->nentries++; | |
444 | ss->length += ln; | |
445 | ||
446 | return ss->length - ln; | |
447 | } | |
448 | ||
449 | static int | |
1c6042ee ILT |
450 | bfd_add_2_to_strtab (abfd, ss, str, str2) |
451 | bfd *abfd; | |
452 | struct strtab *ss; | |
453 | char *str; | |
454 | CONST char *str2; | |
244ffee7 | 455 | { |
32090b8e KR |
456 | /* should search first, but for now: */ |
457 | /* include the trailing NUL */ | |
458 | int ln = strlen (str) + strlen (str2) + 1; | |
459 | ||
460 | /* should this be using obstacks? */ | |
461 | if (ss->length) | |
462 | ss->tab = realloc (ss->tab, ss->length + ln); | |
463 | else | |
b9d5cdf0 | 464 | ss->tab = malloc (ln); |
32090b8e | 465 | |
9783e04a | 466 | BFD_ASSERT (ss->tab != 0); /* FIXME */ |
32090b8e KR |
467 | strcpy (ss->tab + ss->length, str); |
468 | strcpy (ss->tab + ss->length + strlen (str), str2); | |
469 | ss->nentries++; | |
470 | ss->length += ln; | |
471 | ||
472 | return ss->length - ln; | |
244ffee7 | 473 | } |
32090b8e | 474 | \f |
1c6042ee | 475 | |
32090b8e KR |
476 | /* ELF .o/exec file reading */ |
477 | ||
478 | /* Create a new bfd section from an ELF section header. */ | |
479 | ||
244ffee7 | 480 | static boolean |
1c6042ee ILT |
481 | bfd_section_from_shdr (abfd, shindex) |
482 | bfd *abfd; | |
483 | unsigned int shindex; | |
244ffee7 | 484 | { |
32090b8e KR |
485 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; |
486 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
244ffee7 JK |
487 | asection *newsect; |
488 | char *name; | |
489 | ||
490 | name = elf_string_from_elf_strtab (abfd, hdr->sh_name); | |
491 | ||
492 | switch (hdr->sh_type) | |
493 | { | |
494 | ||
495 | case SHT_NULL: | |
496 | /* inactive section. Throw it away. */ | |
497 | return true; | |
498 | ||
499 | case SHT_PROGBITS: | |
25677b5b | 500 | case SHT_DYNAMIC: |
244ffee7 | 501 | /* Bits that get saved. This one is real. */ |
2e03ce18 | 502 | if (hdr->rawdata == NULL) |
244ffee7 | 503 | { |
2e03ce18 ILT |
504 | newsect = bfd_make_section_anyway (abfd, name); |
505 | if (newsect == NULL) | |
506 | return false; | |
244ffee7 | 507 | |
2e03ce18 ILT |
508 | newsect->filepos = hdr->sh_offset; |
509 | newsect->flags |= SEC_HAS_CONTENTS; | |
510 | newsect->vma = hdr->sh_addr; | |
511 | newsect->_raw_size = hdr->sh_size; | |
512 | newsect->alignment_power = bfd_log2 (hdr->sh_addralign); | |
244ffee7 | 513 | |
2e03ce18 ILT |
514 | if (hdr->sh_flags & SHF_ALLOC) |
515 | { | |
516 | newsect->flags |= SEC_ALLOC; | |
517 | newsect->flags |= SEC_LOAD; | |
518 | } | |
244ffee7 | 519 | |
2e03ce18 ILT |
520 | if (!(hdr->sh_flags & SHF_WRITE)) |
521 | newsect->flags |= SEC_READONLY; | |
244ffee7 | 522 | |
2e03ce18 ILT |
523 | if (hdr->sh_flags & SHF_EXECINSTR) |
524 | newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */ | |
525 | else if (newsect->flags & SEC_ALLOC) | |
526 | newsect->flags |= SEC_DATA; | |
d6e5f950 | 527 | |
2e03ce18 ILT |
528 | /* The debugging sections appear to recognized only by name, |
529 | not any sort of flag. */ | |
530 | if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0 | |
531 | || strncmp (name, ".line", sizeof ".line" - 1) == 0 | |
532 | || strncmp (name, ".stab", sizeof ".stab" - 1) == 0) | |
533 | newsect->flags |= SEC_DEBUGGING; | |
534 | ||
535 | hdr->rawdata = (PTR) newsect; | |
244ffee7 JK |
536 | } |
537 | return true; | |
538 | ||
539 | case SHT_NOBITS: | |
540 | /* Bits that get saved. This one is real. */ | |
2e03ce18 | 541 | if (hdr->rawdata == NULL) |
244ffee7 | 542 | { |
2e03ce18 ILT |
543 | newsect = bfd_make_section_anyway (abfd, name); |
544 | if (newsect == NULL) | |
545 | return false; | |
546 | ||
547 | newsect->vma = hdr->sh_addr; | |
548 | newsect->_raw_size = hdr->sh_size; | |
549 | newsect->filepos = hdr->sh_offset; /* fake */ | |
550 | newsect->alignment_power = bfd_log2 (hdr->sh_addralign); | |
551 | if (hdr->sh_flags & SHF_ALLOC) | |
552 | newsect->flags |= SEC_ALLOC; | |
553 | ||
554 | if (!(hdr->sh_flags & SHF_WRITE)) | |
555 | newsect->flags |= SEC_READONLY; | |
556 | ||
557 | /* FIXME: This section is empty. Does it really make sense | |
558 | to set SEC_CODE for it? */ | |
559 | if (hdr->sh_flags & SHF_EXECINSTR) | |
560 | newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */ | |
561 | ||
562 | hdr->rawdata = (PTR) newsect; | |
244ffee7 JK |
563 | } |
564 | return true; | |
565 | ||
566 | case SHT_SYMTAB: /* A symbol table */ | |
32090b8e KR |
567 | if (elf_onesymtab (abfd) == shindex) |
568 | return true; | |
569 | ||
244ffee7 | 570 | BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym)); |
32090b8e | 571 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
244ffee7 | 572 | elf_onesymtab (abfd) = shindex; |
1c6042ee ILT |
573 | elf_tdata (abfd)->symtab_hdr = *hdr; |
574 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr; | |
244ffee7 JK |
575 | abfd->flags |= HAS_SYMS; |
576 | return true; | |
577 | ||
cb71adf1 PS |
578 | case SHT_DYNSYM: /* A dynamic symbol table */ |
579 | if (elf_dynsymtab (abfd) == shindex) | |
580 | return true; | |
581 | ||
582 | BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym)); | |
583 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
584 | elf_dynsymtab (abfd) = shindex; | |
585 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
586 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynsymtab_hdr; | |
587 | abfd->flags |= HAS_SYMS; | |
588 | return true; | |
589 | ||
244ffee7 | 590 | case SHT_STRTAB: /* A string table */ |
32090b8e | 591 | if (hdr->rawdata) |
fce36137 | 592 | return true; |
32090b8e KR |
593 | if (ehdr->e_shstrndx == shindex) |
594 | { | |
1c6042ee ILT |
595 | elf_tdata (abfd)->shstrtab_hdr = *hdr; |
596 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
597 | hdr->rawdata = (PTR) & elf_tdata (abfd)->shstrtab_hdr; | |
32090b8e KR |
598 | return true; |
599 | } | |
600 | { | |
68241b2b | 601 | unsigned int i; |
fce36137 | 602 | |
32090b8e KR |
603 | for (i = 1; i < ehdr->e_shnum; i++) |
604 | { | |
1c6042ee | 605 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; |
32090b8e KR |
606 | if (hdr2->sh_link == shindex) |
607 | { | |
2e03ce18 ILT |
608 | if (! bfd_section_from_shdr (abfd, i)) |
609 | return false; | |
32090b8e KR |
610 | if (elf_onesymtab (abfd) == i) |
611 | { | |
1c6042ee ILT |
612 | elf_tdata (abfd)->strtab_hdr = *hdr; |
613 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; | |
32090b8e KR |
614 | return true; |
615 | } | |
cb71adf1 PS |
616 | if (elf_dynsymtab (abfd) == i) |
617 | { | |
618 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
619 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynstrtab_hdr; | |
620 | return true; | |
621 | } | |
2e03ce18 | 622 | #if 0 /* Not handling other string tables specially right now. */ |
1c6042ee | 623 | hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ |
32090b8e KR |
624 | /* We have a strtab for some random other section. */ |
625 | newsect = (asection *) hdr2->rawdata; | |
626 | if (!newsect) | |
627 | break; | |
628 | hdr->rawdata = (PTR) newsect; | |
629 | hdr2 = &elf_section_data (newsect)->str_hdr; | |
630 | *hdr2 = *hdr; | |
1c6042ee | 631 | elf_elfsections (abfd)[shindex] = hdr2; |
32090b8e KR |
632 | #endif |
633 | } | |
634 | } | |
635 | } | |
636 | ||
2e03ce18 ILT |
637 | newsect = bfd_make_section_anyway (abfd, name); |
638 | if (newsect == NULL) | |
639 | return false; | |
32090b8e | 640 | |
2e03ce18 ILT |
641 | newsect->flags = SEC_HAS_CONTENTS; |
642 | hdr->rawdata = (PTR) newsect; | |
643 | newsect->_raw_size = hdr->sh_size; | |
644 | newsect->alignment_power = bfd_log2 (hdr->sh_addralign); | |
645 | newsect->vma = hdr->sh_addr; | |
646 | newsect->filepos = hdr->sh_offset; | |
01383fb4 | 647 | |
2e03ce18 ILT |
648 | if (hdr->sh_flags & SHF_ALLOC) |
649 | newsect->flags |= SEC_ALLOC | SEC_LOAD; | |
650 | if (!(hdr->sh_flags & SHF_WRITE)) | |
651 | newsect->flags |= SEC_READONLY; | |
652 | if (hdr->sh_flags & SHF_EXECINSTR) | |
653 | newsect->flags |= SEC_CODE; | |
654 | else if (newsect->flags & SEC_ALLOC) | |
655 | newsect->flags |= SEC_DATA; | |
656 | ||
657 | /* Check for debugging string tables. */ | |
658 | if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0 | |
659 | || strncmp (name, ".stab", sizeof ".stab" - 1) == 0) | |
660 | newsect->flags |= SEC_DEBUGGING; | |
fce36137 | 661 | |
244ffee7 JK |
662 | return true; |
663 | ||
664 | case SHT_REL: | |
665 | case SHT_RELA: | |
32090b8e KR |
666 | /* *These* do a lot of work -- but build no sections! |
667 | The spec says there can be multiple strtabs, but only one symtab, | |
668 | but there can be lots of REL* sections. */ | |
244ffee7 | 669 | /* FIXME: The above statement is wrong! There are typically at least |
32090b8e KR |
670 | two symbol tables in a dynamically linked executable, ".dynsym" |
671 | which is the dynamic linkage symbol table and ".symtab", which is | |
672 | the "traditional" symbol table. -fnf */ | |
244ffee7 JK |
673 | |
674 | { | |
675 | asection *target_sect; | |
32090b8e | 676 | Elf_Internal_Shdr *hdr2; |
244ffee7 JK |
677 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; |
678 | ||
679 | /* Don't allow REL relocations on a machine that uses RELA and | |
680 | vice versa. */ | |
681 | /* @@ Actually, the generic ABI does suggest that both might be | |
682 | used in one file. But the four ABI Processor Supplements I | |
683 | have access to right now all specify that only one is used on | |
684 | each of those architectures. It's conceivable that, e.g., a | |
685 | bunch of absolute 32-bit relocs might be more compact in REL | |
686 | form even on a RELA machine... */ | |
687 | BFD_ASSERT (!(use_rela_p && (hdr->sh_type == SHT_REL))); | |
688 | BFD_ASSERT (!(!use_rela_p && (hdr->sh_type == SHT_RELA))); | |
689 | BFD_ASSERT (hdr->sh_entsize == | |
690 | (use_rela_p | |
6a3eb9b6 KR |
691 | ? sizeof (Elf_External_Rela) |
692 | : sizeof (Elf_External_Rel))); | |
244ffee7 | 693 | |
2e03ce18 ILT |
694 | if (! bfd_section_from_shdr (abfd, hdr->sh_info) /* target */ |
695 | || ! bfd_section_from_shdr (abfd, hdr->sh_link)) /* symbol table */ | |
696 | return false; | |
244ffee7 | 697 | target_sect = section_from_elf_index (abfd, hdr->sh_info); |
062189c6 ILT |
698 | if (target_sect == NULL |
699 | || elf_section_data (target_sect) == NULL) | |
244ffee7 JK |
700 | return false; |
701 | ||
32090b8e KR |
702 | hdr2 = &elf_section_data (target_sect)->rel_hdr; |
703 | *hdr2 = *hdr; | |
1c6042ee | 704 | elf_elfsections (abfd)[shindex] = hdr2; |
244ffee7 JK |
705 | target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize; |
706 | target_sect->flags |= SEC_RELOC; | |
707 | target_sect->relocation = 0; | |
708 | target_sect->rel_filepos = hdr->sh_offset; | |
32090b8e | 709 | abfd->flags |= HAS_RELOC; |
244ffee7 JK |
710 | return true; |
711 | } | |
712 | break; | |
713 | ||
714 | case SHT_HASH: | |
244ffee7 JK |
715 | #if 0 |
716 | fprintf (stderr, "Dynamic Linking sections not yet supported.\n"); | |
717 | BFD_FAIL (); | |
718 | #endif | |
719 | break; | |
720 | ||
721 | case SHT_NOTE: | |
722 | #if 0 | |
723 | fprintf (stderr, "Note Sections not yet supported.\n"); | |
724 | BFD_FAIL (); | |
725 | #endif | |
726 | break; | |
727 | ||
728 | case SHT_SHLIB: | |
729 | #if 0 | |
730 | fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n"); | |
731 | #endif | |
732 | return true; | |
733 | ||
734 | default: | |
e621c5cc ILT |
735 | /* Check for any processor-specific section types. */ |
736 | { | |
737 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
738 | ||
739 | if (bed->elf_backend_section_from_shdr) | |
740 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
741 | } | |
244ffee7 JK |
742 | break; |
743 | } | |
744 | ||
745 | return true; | |
746 | } | |
747 | ||
fce36137 | 748 | boolean |
1c6042ee ILT |
749 | elf_new_section_hook (abfd, sec) |
750 | bfd *abfd | |
751 | ; | |
752 | asection *sec; | |
fce36137 | 753 | { |
32090b8e | 754 | struct bfd_elf_section_data *sdata; |
300adb31 KR |
755 | |
756 | sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata)); | |
9783e04a DM |
757 | if (!sdata) |
758 | { | |
d1ad85a6 | 759 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
760 | return false; |
761 | } | |
300adb31 | 762 | sec->used_by_bfd = (PTR) sdata; |
32090b8e | 763 | memset (sdata, 0, sizeof (*sdata)); |
244ffee7 JK |
764 | return true; |
765 | } | |
766 | ||
767 | /* Create a new bfd section from an ELF program header. | |
768 | ||
769 | Since program segments have no names, we generate a synthetic name | |
770 | of the form segment<NUM>, where NUM is generally the index in the | |
771 | program header table. For segments that are split (see below) we | |
772 | generate the names segment<NUM>a and segment<NUM>b. | |
773 | ||
774 | Note that some program segments may have a file size that is different than | |
775 | (less than) the memory size. All this means is that at execution the | |
776 | system must allocate the amount of memory specified by the memory size, | |
777 | but only initialize it with the first "file size" bytes read from the | |
778 | file. This would occur for example, with program segments consisting | |
779 | of combined data+bss. | |
780 | ||
781 | To handle the above situation, this routine generates TWO bfd sections | |
782 | for the single program segment. The first has the length specified by | |
783 | the file size of the segment, and the second has the length specified | |
784 | by the difference between the two sizes. In effect, the segment is split | |
785 | into it's initialized and uninitialized parts. | |
786 | ||
787 | */ | |
788 | ||
789 | static boolean | |
1c6042ee ILT |
790 | bfd_section_from_phdr (abfd, hdr, index) |
791 | bfd *abfd; | |
792 | Elf_Internal_Phdr *hdr; | |
793 | int index; | |
244ffee7 JK |
794 | { |
795 | asection *newsect; | |
796 | char *name; | |
797 | char namebuf[64]; | |
798 | int split; | |
799 | ||
800 | split = ((hdr->p_memsz > 0) && | |
801 | (hdr->p_filesz > 0) && | |
802 | (hdr->p_memsz > hdr->p_filesz)); | |
803 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
804 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
9783e04a DM |
805 | if (!name) |
806 | { | |
d1ad85a6 | 807 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
808 | return false; |
809 | } | |
244ffee7 JK |
810 | strcpy (name, namebuf); |
811 | newsect = bfd_make_section (abfd, name); | |
2e03ce18 ILT |
812 | if (newsect == NULL) |
813 | return false; | |
244ffee7 JK |
814 | newsect->vma = hdr->p_vaddr; |
815 | newsect->_raw_size = hdr->p_filesz; | |
816 | newsect->filepos = hdr->p_offset; | |
817 | newsect->flags |= SEC_HAS_CONTENTS; | |
818 | if (hdr->p_type == PT_LOAD) | |
819 | { | |
820 | newsect->flags |= SEC_ALLOC; | |
821 | newsect->flags |= SEC_LOAD; | |
822 | if (hdr->p_flags & PF_X) | |
823 | { | |
824 | /* FIXME: all we known is that it has execute PERMISSION, | |
825 | may be data. */ | |
826 | newsect->flags |= SEC_CODE; | |
827 | } | |
828 | } | |
829 | if (!(hdr->p_flags & PF_W)) | |
830 | { | |
831 | newsect->flags |= SEC_READONLY; | |
832 | } | |
833 | ||
834 | if (split) | |
835 | { | |
836 | sprintf (namebuf, "segment%db", index); | |
837 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
9783e04a DM |
838 | if (!name) |
839 | { | |
d1ad85a6 | 840 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
841 | return false; |
842 | } | |
244ffee7 JK |
843 | strcpy (name, namebuf); |
844 | newsect = bfd_make_section (abfd, name); | |
2e03ce18 ILT |
845 | if (newsect == NULL) |
846 | return false; | |
244ffee7 JK |
847 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
848 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; | |
849 | if (hdr->p_type == PT_LOAD) | |
850 | { | |
851 | newsect->flags |= SEC_ALLOC; | |
852 | if (hdr->p_flags & PF_X) | |
853 | newsect->flags |= SEC_CODE; | |
854 | } | |
855 | if (!(hdr->p_flags & PF_W)) | |
856 | newsect->flags |= SEC_READONLY; | |
857 | } | |
858 | ||
859 | return true; | |
860 | } | |
861 | ||
32090b8e | 862 | /* Begin processing a given object. |
244ffee7 | 863 | |
32090b8e KR |
864 | First we validate the file by reading in the ELF header and checking |
865 | the magic number. */ | |
866 | ||
867 | static INLINE boolean | |
1c6042ee ILT |
868 | elf_file_p (x_ehdrp) |
869 | Elf_External_Ehdr *x_ehdrp; | |
244ffee7 | 870 | { |
32090b8e KR |
871 | return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0) |
872 | && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1) | |
873 | && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2) | |
874 | && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3)); | |
875 | } | |
244ffee7 | 876 | |
d24928c0 KR |
877 | /* Check to see if the file associated with ABFD matches the target vector |
878 | that ABFD points to. | |
879 | ||
880 | Note that we may be called several times with the same ABFD, but different | |
881 | target vectors, most of which will not match. We have to avoid leaving | |
882 | any side effects in ABFD, or any data it points to (like tdata), if the | |
6ec3bb6a | 883 | file does not match the target vector. */ |
d24928c0 | 884 | |
32090b8e | 885 | bfd_target * |
1c6042ee ILT |
886 | elf_object_p (abfd) |
887 | bfd *abfd; | |
244ffee7 | 888 | { |
32090b8e KR |
889 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
890 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
891 | Elf_External_Shdr x_shdr; /* Section header table entry, external form */ | |
6ec3bb6a | 892 | Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */ |
68241b2b | 893 | unsigned int shindex; |
32090b8e | 894 | char *shstrtab; /* Internal copy of section header stringtab */ |
062189c6 | 895 | struct elf_backend_data *ebd; |
d24928c0 | 896 | struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd); |
6ec3bb6a | 897 | struct elf_obj_tdata *new_tdata = NULL; |
244ffee7 | 898 | |
32090b8e KR |
899 | /* Read in the ELF header in external format. */ |
900 | ||
901 | if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
25057836 JL |
902 | { |
903 | if (bfd_get_error () != bfd_error_system_call) | |
904 | goto got_wrong_format_error; | |
905 | else | |
906 | goto got_no_match; | |
907 | } | |
244ffee7 | 908 | |
32090b8e KR |
909 | /* Now check to see if we have a valid ELF file, and one that BFD can |
910 | make use of. The magic number must match, the address size ('class') | |
911 | and byte-swapping must match our XVEC entry, and it must have a | |
912 | section header table (FIXME: See comments re sections at top of this | |
913 | file). */ | |
244ffee7 | 914 | |
d24928c0 KR |
915 | if ((elf_file_p (&x_ehdr) == false) || |
916 | (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) || | |
917 | (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)) | |
918 | goto got_wrong_format_error; | |
244ffee7 | 919 | |
d24928c0 | 920 | /* Check that file's byte order matches xvec's */ |
32090b8e | 921 | switch (x_ehdr.e_ident[EI_DATA]) |
244ffee7 | 922 | { |
32090b8e KR |
923 | case ELFDATA2MSB: /* Big-endian */ |
924 | if (!abfd->xvec->header_byteorder_big_p) | |
d24928c0 | 925 | goto got_wrong_format_error; |
32090b8e KR |
926 | break; |
927 | case ELFDATA2LSB: /* Little-endian */ | |
928 | if (abfd->xvec->header_byteorder_big_p) | |
d24928c0 | 929 | goto got_wrong_format_error; |
32090b8e KR |
930 | break; |
931 | case ELFDATANONE: /* No data encoding specified */ | |
932 | default: /* Unknown data encoding specified */ | |
d24928c0 | 933 | goto got_wrong_format_error; |
244ffee7 | 934 | } |
244ffee7 | 935 | |
32090b8e | 936 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to |
6ec3bb6a | 937 | the tdata pointer in the bfd. */ |
244ffee7 | 938 | |
6ec3bb6a ILT |
939 | new_tdata = ((struct elf_obj_tdata *) |
940 | bfd_zalloc (abfd, sizeof (struct elf_obj_tdata))); | |
941 | if (new_tdata == NULL) | |
d24928c0 | 942 | goto got_no_memory_error; |
6ec3bb6a | 943 | elf_tdata (abfd) = new_tdata; |
244ffee7 | 944 | |
32090b8e KR |
945 | /* Now that we know the byte order, swap in the rest of the header */ |
946 | i_ehdrp = elf_elfheader (abfd); | |
947 | elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); | |
948 | #if DEBUG & 1 | |
949 | elf_debug_file (i_ehdrp); | |
244ffee7 JK |
950 | #endif |
951 | ||
32090b8e KR |
952 | /* If there is no section header table, we're hosed. */ |
953 | if (i_ehdrp->e_shoff == 0) | |
d24928c0 | 954 | goto got_wrong_format_error; |
244ffee7 | 955 | |
062189c6 ILT |
956 | /* As a simple sanity check, verify that the what BFD thinks is the |
957 | size of each section header table entry actually matches the size | |
958 | recorded in the file. */ | |
959 | if (i_ehdrp->e_shentsize != sizeof (x_shdr)) | |
960 | goto got_wrong_format_error; | |
961 | ||
962 | ebd = get_elf_backend_data (abfd); | |
963 | ||
964 | /* Check that the ELF e_machine field matches what this particular | |
965 | BFD format expects. */ | |
966 | if (ebd->elf_machine_code != i_ehdrp->e_machine) | |
967 | { | |
968 | bfd_target **target_ptr; | |
969 | ||
970 | if (ebd->elf_machine_code != EM_NONE) | |
971 | goto got_wrong_format_error; | |
972 | ||
973 | /* This is the generic ELF target. Let it match any ELF target | |
974 | for which we do not have a specific backend. */ | |
f4bd7a8f | 975 | for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) |
062189c6 ILT |
976 | { |
977 | struct elf_backend_data *back; | |
978 | ||
979 | if ((*target_ptr)->flavour != bfd_target_elf_flavour) | |
980 | continue; | |
981 | back = (struct elf_backend_data *) (*target_ptr)->backend_data; | |
982 | if (back->elf_machine_code == i_ehdrp->e_machine) | |
983 | { | |
984 | /* target_ptr is an ELF backend which matches this | |
985 | object file, so reject the generic ELF target. */ | |
986 | goto got_wrong_format_error; | |
987 | } | |
988 | } | |
989 | } | |
990 | ||
062189c6 ILT |
991 | /* Set the flags and architecture before calling the backend so that |
992 | it can override them. */ | |
7b8106b4 | 993 | if (i_ehdrp->e_type == ET_EXEC) |
32090b8e | 994 | abfd->flags |= EXEC_P; |
7b8106b4 ILT |
995 | else if (i_ehdrp->e_type == ET_DYN) |
996 | abfd->flags |= DYNAMIC; | |
244ffee7 | 997 | |
6ec3bb6a ILT |
998 | if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)) |
999 | goto got_no_match; | |
32090b8e | 1000 | |
062189c6 ILT |
1001 | /* Remember the entry point specified in the ELF file header. */ |
1002 | bfd_get_start_address (abfd) = i_ehdrp->e_entry; | |
32090b8e | 1003 | |
062189c6 ILT |
1004 | /* Let the backend double check the format and override global |
1005 | information. */ | |
1006 | if (ebd->elf_backend_object_p) | |
1007 | { | |
1008 | if ((*ebd->elf_backend_object_p) (abfd) == false) | |
1009 | goto got_wrong_format_error; | |
1010 | } | |
1c6042ee | 1011 | |
32090b8e KR |
1012 | /* Allocate space for a copy of the section header table in |
1013 | internal form, seek to the section header table in the file, | |
062189c6 | 1014 | read it in, and convert it to internal form. */ |
6ec3bb6a ILT |
1015 | i_shdrp = ((Elf_Internal_Shdr *) |
1016 | bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum)); | |
1017 | elf_elfsections (abfd) = ((Elf_Internal_Shdr **) | |
1018 | bfd_alloc (abfd, | |
1019 | sizeof (i_shdrp) * i_ehdrp->e_shnum)); | |
1c6042ee | 1020 | if (!i_shdrp || !elf_elfsections (abfd)) |
d24928c0 | 1021 | goto got_no_memory_error; |
6ec3bb6a | 1022 | if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0) |
25057836 | 1023 | goto got_no_match; |
32090b8e | 1024 | for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++) |
244ffee7 | 1025 | { |
d24928c0 | 1026 | if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr)) |
25057836 | 1027 | goto got_no_match; |
32090b8e | 1028 | elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex); |
1c6042ee | 1029 | elf_elfsections (abfd)[shindex] = i_shdrp + shindex; |
38a5f510 ILT |
1030 | |
1031 | /* If this is a .dynamic section, mark the object file as being | |
1032 | dynamically linked. */ | |
1033 | if (i_shdrp[shindex].sh_type == SHT_DYNAMIC) | |
1034 | abfd->flags |= DYNAMIC; | |
244ffee7 | 1035 | } |
32090b8e | 1036 | if (i_ehdrp->e_shstrndx) |
244ffee7 | 1037 | { |
2e03ce18 ILT |
1038 | if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx)) |
1039 | goto got_no_match; | |
244ffee7 JK |
1040 | } |
1041 | ||
32090b8e KR |
1042 | /* Read in the string table containing the names of the sections. We |
1043 | will need the base pointer to this table later. */ | |
1044 | /* We read this inline now, so that we don't have to go through | |
1045 | bfd_section_from_shdr with it (since this particular strtab is | |
1046 | used to find all of the ELF section names.) */ | |
244ffee7 | 1047 | |
32090b8e KR |
1048 | shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx); |
1049 | if (!shstrtab) | |
6ec3bb6a | 1050 | goto got_no_match; |
244ffee7 | 1051 | |
32090b8e KR |
1052 | /* Once all of the section headers have been read and converted, we |
1053 | can start processing them. Note that the first section header is | |
6ec3bb6a | 1054 | a dummy placeholder entry, so we ignore it. */ |
244ffee7 | 1055 | |
32090b8e KR |
1056 | for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++) |
1057 | { | |
2e03ce18 ILT |
1058 | if (! bfd_section_from_shdr (abfd, shindex)) |
1059 | goto got_no_match; | |
32090b8e | 1060 | } |
244ffee7 | 1061 | |
d24928c0 KR |
1062 | return (abfd->xvec); |
1063 | ||
1c6042ee | 1064 | got_wrong_format_error: |
d1ad85a6 | 1065 | bfd_set_error (bfd_error_wrong_format); |
d24928c0 | 1066 | goto got_no_match; |
1c6042ee | 1067 | got_no_memory_error: |
d1ad85a6 | 1068 | bfd_set_error (bfd_error_no_memory); |
d24928c0 | 1069 | goto got_no_match; |
1c6042ee | 1070 | got_no_match: |
6ec3bb6a ILT |
1071 | if (new_tdata != NULL |
1072 | && new_tdata->elf_sect_ptr != NULL) | |
1073 | bfd_release (abfd, new_tdata->elf_sect_ptr); | |
1074 | if (i_shdrp != NULL) | |
1075 | bfd_release (abfd, i_shdrp); | |
1076 | if (new_tdata != NULL) | |
1077 | bfd_release (abfd, new_tdata); | |
d24928c0 KR |
1078 | elf_tdata (abfd) = preserved_tdata; |
1079 | return (NULL); | |
32090b8e | 1080 | } |
32090b8e | 1081 | \f |
1c6042ee | 1082 | |
32090b8e KR |
1083 | /* ELF .o/exec file writing */ |
1084 | ||
d24928c0 KR |
1085 | /* Takes a bfd and a symbol, returns a pointer to the elf specific area |
1086 | of the symbol if there is one. */ | |
32090b8e | 1087 | static INLINE elf_symbol_type * |
1c6042ee ILT |
1088 | elf_symbol_from (ignore_abfd, symbol) |
1089 | bfd *ignore_abfd; | |
1090 | asymbol *symbol; | |
244ffee7 | 1091 | { |
32090b8e KR |
1092 | if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour) |
1093 | return 0; | |
1094 | ||
1095 | if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL) | |
1096 | return 0; | |
1097 | ||
1098 | return (elf_symbol_type *) symbol; | |
244ffee7 JK |
1099 | } |
1100 | ||
d24928c0 | 1101 | /* Create ELF output from BFD sections. |
244ffee7 | 1102 | |
d24928c0 KR |
1103 | Essentially, just create the section header and forget about the program |
1104 | header for now. */ | |
244ffee7 | 1105 | |
32090b8e | 1106 | static void |
1c6042ee ILT |
1107 | elf_make_sections (abfd, asect, obj) |
1108 | bfd *abfd; | |
1109 | asection *asect; | |
1110 | PTR obj; | |
32090b8e KR |
1111 | { |
1112 | /* most of what is in bfd_shdr_from_section goes in here... */ | |
1113 | /* and all of these sections generate at *least* one ELF section. */ | |
32090b8e KR |
1114 | Elf_Internal_Shdr *this_hdr; |
1115 | this_hdr = &elf_section_data (asect)->this_hdr; | |
244ffee7 | 1116 | |
32090b8e KR |
1117 | this_hdr->sh_addr = asect->vma; |
1118 | this_hdr->sh_size = asect->_raw_size; | |
1119 | /* contents already set by elf_set_section_contents */ | |
244ffee7 | 1120 | |
300adb31 | 1121 | if (asect->flags & SEC_RELOC) |
244ffee7 | 1122 | { |
32090b8e KR |
1123 | /* emit a reloc section, and thus strtab and symtab... */ |
1124 | Elf_Internal_Shdr *rela_hdr; | |
32090b8e | 1125 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; |
244ffee7 | 1126 | |
32090b8e | 1127 | rela_hdr = &elf_section_data (asect)->rel_hdr; |
244ffee7 | 1128 | |
32090b8e KR |
1129 | /* orelocation has the data, reloc_count has the count... */ |
1130 | if (use_rela_p) | |
1131 | { | |
1132 | rela_hdr->sh_type = SHT_RELA; | |
1133 | rela_hdr->sh_entsize = sizeof (Elf_External_Rela); | |
1134 | } | |
1135 | else | |
1136 | /* REL relocations */ | |
1137 | { | |
1138 | rela_hdr->sh_type = SHT_REL; | |
1139 | rela_hdr->sh_entsize = sizeof (Elf_External_Rel); | |
1140 | } | |
1141 | rela_hdr->sh_flags = 0; | |
1142 | rela_hdr->sh_addr = 0; | |
1143 | rela_hdr->sh_offset = 0; | |
062189c6 ILT |
1144 | |
1145 | /* FIXME: Systems I've checked use an alignment of 4, but it is | |
1146 | possible that some systems use a different alignment. */ | |
1147 | rela_hdr->sh_addralign = 4; | |
1148 | ||
32090b8e KR |
1149 | rela_hdr->size = 0; |
1150 | } | |
1151 | if (asect->flags & SEC_ALLOC) | |
244ffee7 | 1152 | { |
32090b8e KR |
1153 | this_hdr->sh_flags |= SHF_ALLOC; |
1154 | if (asect->flags & SEC_LOAD) | |
1155 | { | |
1156 | /* @@ Do something with sh_type? */ | |
1157 | } | |
244ffee7 | 1158 | } |
f035cc47 ILT |
1159 | else |
1160 | { | |
1161 | /* If this section is not part of the program image during | |
1162 | execution, leave the address fields at 0. */ | |
1163 | this_hdr->sh_addr = 0; | |
1164 | asect->vma = 0; | |
1165 | } | |
32090b8e KR |
1166 | if (!(asect->flags & SEC_READONLY)) |
1167 | this_hdr->sh_flags |= SHF_WRITE; | |
244ffee7 | 1168 | |
32090b8e KR |
1169 | if (asect->flags & SEC_CODE) |
1170 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
1171 | } | |
244ffee7 | 1172 | |
32090b8e KR |
1173 | void |
1174 | write_relocs (abfd, sec, xxx) | |
1175 | bfd *abfd; | |
1176 | asection *sec; | |
1177 | PTR xxx; | |
1178 | { | |
1179 | Elf_Internal_Shdr *rela_hdr; | |
1180 | Elf_External_Rela *outbound_relocas; | |
1181 | Elf_External_Rel *outbound_relocs; | |
1182 | int idx; | |
1183 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
300adb31 | 1184 | asymbol *last_sym = 0; |
38a5f510 | 1185 | int last_sym_idx = 9999999; /* should always be written before use */ |
244ffee7 | 1186 | |
32090b8e KR |
1187 | if ((sec->flags & SEC_RELOC) == 0) |
1188 | return; | |
6ec3bb6a ILT |
1189 | |
1190 | /* The linker backend writes the relocs out itself, and sets the | |
1191 | reloc_count field to zero to inhibit writing them here. Also, | |
1192 | sometimes the SEC_RELOC flag gets set even when there aren't any | |
1193 | relocs. */ | |
32090b8e KR |
1194 | if (sec->reloc_count == 0) |
1195 | return; | |
244ffee7 | 1196 | |
32090b8e | 1197 | rela_hdr = &elf_section_data (sec)->rel_hdr; |
244ffee7 | 1198 | |
32090b8e KR |
1199 | rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count; |
1200 | rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size); | |
9783e04a DM |
1201 | if (!rela_hdr->contents) |
1202 | { | |
d1ad85a6 | 1203 | bfd_set_error (bfd_error_no_memory); |
1c6042ee | 1204 | abort (); /* FIXME */ |
9783e04a | 1205 | } |
244ffee7 | 1206 | |
32090b8e | 1207 | /* orelocation has the data, reloc_count has the count... */ |
300adb31 KR |
1208 | if (use_rela_p) |
1209 | { | |
1210 | outbound_relocas = (Elf_External_Rela *) rela_hdr->contents; | |
1211 | ||
1212 | for (idx = 0; idx < sec->reloc_count; idx++) | |
32090b8e | 1213 | { |
300adb31 KR |
1214 | Elf_Internal_Rela dst_rela; |
1215 | Elf_External_Rela *src_rela; | |
1216 | arelent *ptr; | |
1217 | asymbol *sym; | |
1218 | int n; | |
1219 | ||
1220 | ptr = sec->orelocation[idx]; | |
1221 | src_rela = outbound_relocas + idx; | |
1222 | if (!(abfd->flags & EXEC_P)) | |
1223 | dst_rela.r_offset = ptr->address - sec->vma; | |
1224 | else | |
1225 | dst_rela.r_offset = ptr->address; | |
6a3eb9b6 | 1226 | |
300adb31 KR |
1227 | sym = *ptr->sym_ptr_ptr; |
1228 | if (sym == last_sym) | |
1229 | n = last_sym_idx; | |
1230 | else | |
32090b8e | 1231 | { |
300adb31 KR |
1232 | last_sym = sym; |
1233 | last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym); | |
32090b8e | 1234 | } |
300adb31 KR |
1235 | dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type); |
1236 | ||
1237 | dst_rela.r_addend = ptr->addend; | |
1238 | elf_swap_reloca_out (abfd, &dst_rela, src_rela); | |
244ffee7 | 1239 | } |
300adb31 KR |
1240 | } |
1241 | else | |
1242 | /* REL relocations */ | |
1243 | { | |
1244 | outbound_relocs = (Elf_External_Rel *) rela_hdr->contents; | |
1245 | ||
1246 | for (idx = 0; idx < sec->reloc_count; idx++) | |
32090b8e | 1247 | { |
300adb31 KR |
1248 | Elf_Internal_Rel dst_rel; |
1249 | Elf_External_Rel *src_rel; | |
1250 | arelent *ptr; | |
1251 | int n; | |
1252 | asymbol *sym; | |
1253 | ||
1254 | ptr = sec->orelocation[idx]; | |
1255 | sym = *ptr->sym_ptr_ptr; | |
1256 | src_rel = outbound_relocs + idx; | |
1257 | if (!(abfd->flags & EXEC_P)) | |
1258 | dst_rel.r_offset = ptr->address - sec->vma; | |
1259 | else | |
1260 | dst_rel.r_offset = ptr->address; | |
244ffee7 | 1261 | |
300adb31 KR |
1262 | if (sym == last_sym) |
1263 | n = last_sym_idx; | |
1264 | else | |
32090b8e | 1265 | { |
300adb31 KR |
1266 | last_sym = sym; |
1267 | last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym); | |
32090b8e | 1268 | } |
300adb31 KR |
1269 | dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type); |
1270 | ||
1271 | elf_swap_reloc_out (abfd, &dst_rel, src_rel); | |
32090b8e | 1272 | } |
300adb31 | 1273 | } |
32090b8e | 1274 | } |
244ffee7 | 1275 | |
32090b8e KR |
1276 | static void |
1277 | fix_up_strtabs (abfd, asect, obj) | |
1278 | bfd *abfd; | |
1279 | asection *asect; | |
1280 | PTR obj; | |
1281 | { | |
1282 | Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr; | |
1c6042ee | 1283 | int this_idx = elf_section_data (asect)->this_idx; |
244ffee7 | 1284 | |
32090b8e KR |
1285 | /* @@ Check flags! */ |
1286 | if (!strncmp (asect->name, ".stab", 5) | |
1287 | && !strcmp ("str", asect->name + strlen (asect->name) - 3)) | |
1288 | { | |
1289 | size_t len = strlen (asect->name) + 1; | |
80425e6c JK |
1290 | char *s = (char *) malloc (len); |
1291 | if (s == NULL) | |
1292 | /* FIXME: Should deal more gracefully with errors. */ | |
1293 | abort (); | |
32090b8e KR |
1294 | strcpy (s, asect->name); |
1295 | s[len - 4] = 0; | |
1296 | asect = bfd_get_section_by_name (abfd, s); | |
80425e6c | 1297 | free (s); |
32090b8e KR |
1298 | if (!asect) |
1299 | abort (); | |
1c6042ee | 1300 | elf_section_data (asect)->this_hdr.sh_link = this_idx; |
32090b8e | 1301 | /* @@ Assuming 32 bits! */ |
1c6042ee | 1302 | elf_section_data (asect)->this_hdr.sh_entsize = 0xc; |
01383fb4 KR |
1303 | |
1304 | this_hdr->sh_type = SHT_STRTAB; | |
244ffee7 | 1305 | } |
32090b8e | 1306 | } |
244ffee7 | 1307 | |
32090b8e | 1308 | static void |
1c6042ee ILT |
1309 | elf_fake_sections (abfd, asect, obj) |
1310 | bfd *abfd; | |
1311 | asection *asect; | |
1312 | PTR obj; | |
32090b8e KR |
1313 | { |
1314 | /* most of what is in bfd_shdr_from_section goes in here... */ | |
1315 | /* and all of these sections generate at *least* one ELF section. */ | |
244ffee7 | 1316 | |
32090b8e KR |
1317 | Elf_Internal_Shdr *this_hdr; |
1318 | this_hdr = &elf_section_data (asect)->this_hdr; | |
1319 | this_hdr->sh_name = | |
1320 | bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name); | |
6ec3bb6a ILT |
1321 | if (this_hdr->sh_name == (unsigned long) -1) |
1322 | abort (); /* FIXME */ | |
32090b8e KR |
1323 | /* We need to log the type *now* so that elf_section_from_bfd_section |
1324 | can find us... have to set rawdata too. */ | |
1325 | this_hdr->rawdata = (void *) asect; | |
1326 | this_hdr->sh_addralign = 1 << asect->alignment_power; | |
1327 | if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) | |
1328 | this_hdr->sh_type = SHT_PROGBITS; | |
e621c5cc ILT |
1329 | else if ((asect->flags & SEC_ALLOC) && ((asect->flags & SEC_LOAD) == 0)) |
1330 | { | |
6c35a16d ILT |
1331 | BFD_ASSERT (strcmp (asect->name, ".bss") == 0 |
1332 | || strcmp (asect->name, ".sbss") == 0); | |
e621c5cc ILT |
1333 | this_hdr->sh_type = SHT_NOBITS; |
1334 | } | |
1335 | /* FIXME I am not sure how to detect a .note section from the flags | |
1336 | word of an `asection'. */ | |
1337 | else if (!strcmp (asect->name, ".note")) | |
1338 | this_hdr->sh_type = SHT_NOTE; | |
32090b8e | 1339 | else |
32090b8e KR |
1340 | this_hdr->sh_type = SHT_PROGBITS; |
1341 | ||
1342 | this_hdr->sh_flags = 0; | |
1343 | this_hdr->sh_addr = 0; | |
1344 | this_hdr->sh_size = 0; | |
1345 | this_hdr->sh_entsize = 0; | |
1346 | this_hdr->sh_info = 0; | |
1347 | this_hdr->sh_link = 0; | |
1348 | this_hdr->sh_offset = 0; | |
1349 | this_hdr->size = 0; | |
244ffee7 | 1350 | |
f035cc47 ILT |
1351 | /* Now, check for processor-specific section types. */ |
1352 | { | |
1353 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1354 | ||
1355 | if (bed->elf_backend_fake_sections) | |
1356 | (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect); | |
1357 | } | |
1358 | ||
32090b8e KR |
1359 | { |
1360 | /* Emit a strtab and symtab, and possibly a reloc section. */ | |
1361 | Elf_Internal_Shdr *rela_hdr; | |
244ffee7 | 1362 | |
32090b8e KR |
1363 | /* Note that only one symtab is used, so just remember it |
1364 | for now. */ | |
244ffee7 | 1365 | |
300adb31 | 1366 | if (asect->flags & SEC_RELOC) |
32090b8e KR |
1367 | { |
1368 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
244ffee7 | 1369 | |
32090b8e KR |
1370 | rela_hdr = &elf_section_data (asect)->rel_hdr; |
1371 | rela_hdr->sh_name = | |
1372 | bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd), | |
1373 | use_rela_p ? ".rela" : ".rel", | |
1374 | asect->name); | |
1375 | rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; | |
1376 | rela_hdr->sh_entsize = (use_rela_p | |
1377 | ? sizeof (Elf_External_Rela) | |
1378 | : sizeof (Elf_External_Rel)); | |
1379 | ||
1380 | rela_hdr->sh_flags = 0; | |
1381 | rela_hdr->sh_addr = 0; | |
1382 | rela_hdr->sh_size = 0; | |
1383 | rela_hdr->sh_offset = 0; | |
062189c6 ILT |
1384 | |
1385 | /* FIXME: Systems I've checked use an alignment of 4, but some | |
1386 | systems may use a different alignment. */ | |
1387 | rela_hdr->sh_addralign = 4; | |
1388 | ||
32090b8e KR |
1389 | rela_hdr->size = 0; |
1390 | } | |
1391 | } | |
1392 | if (asect->flags & SEC_ALLOC) | |
1393 | { | |
1394 | this_hdr->sh_flags |= SHF_ALLOC; | |
1395 | if (asect->flags & SEC_LOAD) | |
1396 | { | |
1397 | /* @@ Do something with sh_type? */ | |
1398 | } | |
1399 | } | |
1400 | if (!(asect->flags & SEC_READONLY)) | |
1401 | this_hdr->sh_flags |= SHF_WRITE; | |
1402 | if (asect->flags & SEC_CODE) | |
1403 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
244ffee7 JK |
1404 | } |
1405 | ||
32090b8e KR |
1406 | /* Map symbol from it's internal number to the external number, moving |
1407 | all local symbols to be at the head of the list. */ | |
244ffee7 | 1408 | |
32090b8e | 1409 | static INLINE int |
062189c6 ILT |
1410 | sym_is_global (abfd, sym) |
1411 | bfd *abfd; | |
32090b8e KR |
1412 | asymbol *sym; |
1413 | { | |
062189c6 ILT |
1414 | /* If the backend has a special mapping, use it. */ |
1415 | if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1416 | return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1417 | (abfd, sym)); | |
1418 | ||
d24928c0 | 1419 | if (sym->flags & (BSF_GLOBAL | BSF_WEAK)) |
244ffee7 | 1420 | { |
32090b8e KR |
1421 | if (sym->flags & BSF_LOCAL) |
1422 | abort (); | |
1423 | return 1; | |
244ffee7 | 1424 | } |
d24928c0 KR |
1425 | if (sym->section == 0) |
1426 | { | |
1427 | /* Is this valid? */ | |
1428 | abort (); | |
1429 | ||
1430 | return 1; | |
1431 | } | |
32090b8e KR |
1432 | if (sym->section == &bfd_und_section) |
1433 | return 1; | |
1434 | if (bfd_is_com_section (sym->section)) | |
1435 | return 1; | |
1436 | if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE)) | |
1437 | return 0; | |
1438 | return 0; | |
1439 | } | |
244ffee7 | 1440 | |
9783e04a | 1441 | static boolean |
1c6042ee ILT |
1442 | elf_map_symbols (abfd) |
1443 | bfd *abfd; | |
32090b8e KR |
1444 | { |
1445 | int symcount = bfd_get_symcount (abfd); | |
1446 | asymbol **syms = bfd_get_outsymbols (abfd); | |
d24928c0 | 1447 | asymbol **sect_syms; |
32090b8e KR |
1448 | int num_locals = 0; |
1449 | int num_globals = 0; | |
1450 | int num_locals2 = 0; | |
1451 | int num_globals2 = 0; | |
d24928c0 | 1452 | int max_index = 0; |
32090b8e | 1453 | int num_sections = 0; |
d24928c0 | 1454 | Elf_Sym_Extra *sym_extra; |
32090b8e KR |
1455 | int idx; |
1456 | asection *asect; | |
6a3eb9b6 | 1457 | |
32090b8e KR |
1458 | #ifdef DEBUG |
1459 | fprintf (stderr, "elf_map_symbols\n"); | |
1460 | fflush (stderr); | |
1461 | #endif | |
244ffee7 | 1462 | |
e621c5cc ILT |
1463 | /* Add local symbols for each section for which there are relocs. |
1464 | FIXME: How can we tell which sections have relocs at this point? | |
1465 | Will reloc_count always be accurate? Actually, I think most ELF | |
1466 | targets create section symbols for all sections anyhow. */ | |
32090b8e | 1467 | for (asect = abfd->sections; asect; asect = asect->next) |
244ffee7 | 1468 | { |
d24928c0 KR |
1469 | if (max_index < asect->index) |
1470 | max_index = asect->index; | |
244ffee7 JK |
1471 | } |
1472 | ||
d24928c0 KR |
1473 | max_index++; |
1474 | elf_num_section_syms (abfd) = max_index; | |
1475 | sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *)); | |
1476 | elf_section_syms (abfd) = sect_syms; | |
1477 | ||
5e829a34 | 1478 | if (sect_syms == 0) |
9783e04a | 1479 | { |
d1ad85a6 | 1480 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1481 | return false; |
1482 | } | |
d24928c0 KR |
1483 | |
1484 | for (asect = abfd->sections; asect; asect = asect->next) | |
e621c5cc ILT |
1485 | { |
1486 | asymbol *sym = bfd_make_empty_symbol (abfd); | |
9783e04a DM |
1487 | if (!sym) |
1488 | { | |
d1ad85a6 | 1489 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1490 | return false; |
1491 | } | |
e621c5cc ILT |
1492 | sym->the_bfd = abfd; |
1493 | sym->name = asect->name; | |
1494 | sym->value = asect->vma; | |
1495 | sym->flags = BSF_SECTION_SYM; | |
1496 | sym->section = asect; | |
1497 | sect_syms[asect->index] = sym; | |
1498 | num_sections++; | |
d24928c0 | 1499 | #ifdef DEBUG |
e621c5cc ILT |
1500 | fprintf (stderr, |
1501 | "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n", | |
1502 | asect->name, (long) asect->vma, asect->index, (long) asect); | |
d24928c0 | 1503 | #endif |
e621c5cc | 1504 | } |
d24928c0 | 1505 | |
32090b8e | 1506 | if (num_sections) |
244ffee7 | 1507 | { |
32090b8e KR |
1508 | if (syms) |
1509 | syms = (asymbol **) bfd_realloc (abfd, syms, | |
1510 | ((symcount + num_sections + 1) | |
1511 | * sizeof (asymbol *))); | |
1512 | else | |
1513 | syms = (asymbol **) bfd_alloc (abfd, | |
1c6042ee | 1514 | (num_sections + 1) * sizeof (asymbol *)); |
9783e04a DM |
1515 | if (!syms) |
1516 | { | |
d1ad85a6 | 1517 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1518 | return false; |
1519 | } | |
244ffee7 | 1520 | |
32090b8e KR |
1521 | for (asect = abfd->sections; asect; asect = asect->next) |
1522 | { | |
d24928c0 KR |
1523 | if (sect_syms[asect->index]) |
1524 | syms[symcount++] = sect_syms[asect->index]; | |
32090b8e | 1525 | } |
244ffee7 | 1526 | |
32090b8e KR |
1527 | syms[symcount] = (asymbol *) 0; |
1528 | bfd_set_symtab (abfd, syms, symcount); | |
1529 | } | |
244ffee7 | 1530 | |
d24928c0 KR |
1531 | elf_sym_extra (abfd) = sym_extra |
1532 | = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra)); | |
9783e04a DM |
1533 | if (!sym_extra) |
1534 | { | |
d1ad85a6 | 1535 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1536 | return false; |
1537 | } | |
244ffee7 | 1538 | |
32090b8e KR |
1539 | /* Identify and classify all of the symbols. */ |
1540 | for (idx = 0; idx < symcount; idx++) | |
244ffee7 | 1541 | { |
062189c6 | 1542 | if (!sym_is_global (abfd, syms[idx])) |
32090b8e KR |
1543 | num_locals++; |
1544 | else | |
1545 | num_globals++; | |
244ffee7 | 1546 | } |
32090b8e KR |
1547 | |
1548 | /* Now provide mapping information. Add +1 for skipping over the | |
1549 | dummy symbol. */ | |
1550 | for (idx = 0; idx < symcount; idx++) | |
244ffee7 | 1551 | { |
1c6042ee | 1552 | syms[idx]->udata = (PTR) & sym_extra[idx]; |
062189c6 | 1553 | if (!sym_is_global (abfd, syms[idx])) |
d24928c0 | 1554 | sym_extra[idx].elf_sym_num = 1 + num_locals2++; |
32090b8e | 1555 | else |
d24928c0 | 1556 | sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++; |
244ffee7 JK |
1557 | } |
1558 | ||
32090b8e KR |
1559 | elf_num_locals (abfd) = num_locals; |
1560 | elf_num_globals (abfd) = num_globals; | |
9783e04a | 1561 | return true; |
32090b8e | 1562 | } |
244ffee7 | 1563 | |
6ec3bb6a ILT |
1564 | /* Compute the file positions we are going to put the sections at, and |
1565 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
1566 | is not NULL, this is being called by the ELF backend linker. */ | |
244ffee7 | 1567 | |
32090b8e | 1568 | static boolean |
6ec3bb6a | 1569 | elf_compute_section_file_positions (abfd, link_info) |
1c6042ee | 1570 | bfd *abfd; |
6ec3bb6a | 1571 | struct bfd_link_info *link_info; |
32090b8e | 1572 | { |
6ec3bb6a ILT |
1573 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
1574 | Elf_Internal_Shdr *shstrtab_hdr; | |
1575 | ||
1576 | if (abfd->output_has_begun) | |
1577 | return true; | |
1578 | ||
1579 | /* Do any elf backend specific processing first. */ | |
1580 | if (bed->elf_backend_begin_write_processing) | |
1581 | (*bed->elf_backend_begin_write_processing) (abfd); | |
1582 | ||
1583 | if (! prep_headers (abfd)) | |
1584 | return false; | |
1585 | ||
32090b8e | 1586 | bfd_map_over_sections (abfd, elf_fake_sections, 0); |
244ffee7 | 1587 | |
9783e04a DM |
1588 | if (!assign_section_numbers (abfd)) |
1589 | return false; | |
244ffee7 | 1590 | |
32090b8e | 1591 | bfd_map_over_sections (abfd, elf_make_sections, 0); |
244ffee7 | 1592 | |
1c6042ee | 1593 | bfd_map_over_sections (abfd, fix_up_strtabs, 0); /* .stab/.stabstr &c */ |
244ffee7 | 1594 | |
6ec3bb6a ILT |
1595 | /* The backend linker builds symbol table information itself. */ |
1596 | if (link_info == NULL) | |
1597 | { | |
1598 | if (! swap_out_syms (abfd)) | |
1599 | return false; | |
1600 | } | |
244ffee7 | 1601 | |
6ec3bb6a ILT |
1602 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
1603 | /* sh_name was set in prep_headers. */ | |
1604 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
1605 | shstrtab_hdr->sh_flags = 0; | |
1606 | shstrtab_hdr->sh_addr = 0; | |
1607 | shstrtab_hdr->sh_size = elf_shstrtab (abfd)->length; | |
1608 | shstrtab_hdr->sh_entsize = 0; | |
1609 | shstrtab_hdr->sh_link = 0; | |
1610 | shstrtab_hdr->sh_info = 0; | |
1611 | /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs. */ | |
1612 | shstrtab_hdr->sh_addralign = 1; | |
1613 | shstrtab_hdr->contents = (PTR) elf_shstrtab (abfd)->tab; | |
1614 | ||
1615 | if (!assign_file_positions_except_relocs (abfd, | |
1616 | link_info == NULL ? true : false)) | |
9783e04a | 1617 | return false; |
32090b8e | 1618 | |
6ec3bb6a ILT |
1619 | abfd->output_has_begun = true; |
1620 | ||
32090b8e KR |
1621 | return true; |
1622 | } | |
1623 | ||
1624 | static boolean | |
1c6042ee ILT |
1625 | elf_write_phdrs (abfd, i_ehdrp, i_phdrp, phdr_cnt) |
1626 | bfd *abfd; | |
1627 | Elf_Internal_Ehdr *i_ehdrp; | |
1628 | Elf_Internal_Phdr *i_phdrp; | |
1629 | unsigned short phdr_cnt; | |
244ffee7 | 1630 | { |
32090b8e | 1631 | /* first program header entry goes after the file header */ |
300adb31 | 1632 | int outbase = i_ehdrp->e_phoff; |
68241b2b | 1633 | unsigned int i; |
32090b8e KR |
1634 | Elf_External_Phdr x_phdr; |
1635 | ||
1636 | for (i = 0; i < phdr_cnt; i++) | |
244ffee7 | 1637 | { |
32090b8e | 1638 | elf_swap_phdr_out (abfd, i_phdrp + i, &x_phdr); |
4002f18a ILT |
1639 | if (bfd_seek (abfd, outbase, SEEK_SET) != 0 |
1640 | || (bfd_write ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd) | |
1641 | != sizeof (x_phdr))) | |
1642 | return false; | |
32090b8e | 1643 | outbase += sizeof (x_phdr); |
244ffee7 | 1644 | } |
32090b8e KR |
1645 | |
1646 | return true; | |
244ffee7 JK |
1647 | } |
1648 | ||
32090b8e KR |
1649 | /* Assign all ELF section numbers. The dummy first section is handled here |
1650 | too. The link/info pointers for the standard section types are filled | |
1651 | in here too, while we're at it. (Link pointers for .stab sections are | |
1652 | not filled in here.) */ | |
9783e04a | 1653 | static boolean |
32090b8e | 1654 | assign_section_numbers (abfd) |
fce36137 | 1655 | bfd *abfd; |
fce36137 | 1656 | { |
32090b8e KR |
1657 | struct elf_obj_tdata *t = elf_tdata (abfd); |
1658 | asection *sec; | |
1659 | int section_number = 1; | |
1660 | int i; | |
1661 | Elf_Internal_Shdr **i_shdrp; | |
244ffee7 | 1662 | |
1c6042ee ILT |
1663 | t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length; |
1664 | t->shstrtab_hdr.contents = (void *) elf_shstrtab (abfd)->tab; | |
244ffee7 | 1665 | |
32090b8e | 1666 | t->shstrtab_section = section_number++; |
1c6042ee | 1667 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; |
32090b8e KR |
1668 | if (abfd->symcount) |
1669 | { | |
1670 | t->symtab_section = section_number++; | |
1671 | t->strtab_section = section_number++; | |
1672 | t->symtab_hdr.sh_link = t->strtab_section; | |
1673 | } | |
1674 | for (sec = abfd->sections; sec; sec = sec->next) | |
1675 | { | |
1676 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1677 | d->this_idx = section_number++; | |
300adb31 | 1678 | if (sec->flags & SEC_RELOC) |
fce36137 | 1679 | { |
32090b8e KR |
1680 | d->rel_idx = section_number++; |
1681 | d->rel_hdr.sh_link = t->symtab_section; | |
1682 | d->rel_hdr.sh_info = d->this_idx; | |
244ffee7 | 1683 | } |
fce36137 | 1684 | else |
32090b8e KR |
1685 | d->rel_idx = 0; |
1686 | /* No handling for per-section string tables currently. */ | |
1687 | } | |
1c6042ee | 1688 | elf_elfheader (abfd)->e_shnum = section_number; |
32090b8e KR |
1689 | |
1690 | /* Set up the list of section header pointers, in agreement with the | |
1691 | indices. */ | |
6ec3bb6a ILT |
1692 | i_shdrp = ((Elf_Internal_Shdr **) |
1693 | bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *))); | |
1694 | if (i_shdrp == NULL) | |
1695 | { | |
1696 | bfd_set_error (bfd_error_no_memory); | |
1697 | return false; | |
1698 | } | |
1699 | i_shdrp[0] = ((Elf_Internal_Shdr *) | |
1700 | bfd_alloc (abfd, sizeof (Elf_Internal_Shdr))); | |
1701 | if (i_shdrp[0] == NULL) | |
9783e04a | 1702 | { |
6ec3bb6a | 1703 | bfd_release (abfd, i_shdrp); |
d1ad85a6 | 1704 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1705 | return false; |
1706 | } | |
1c6042ee | 1707 | elf_elfsections (abfd) = i_shdrp; |
6ec3bb6a ILT |
1708 | for (i = 1; i < section_number; i++) |
1709 | i_shdrp[i] = NULL; | |
1710 | memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr)); | |
32090b8e | 1711 | |
32090b8e KR |
1712 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; |
1713 | if (abfd->symcount) | |
1714 | { | |
1715 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
1716 | i_shdrp[t->strtab_section] = &t->strtab_hdr; | |
244ffee7 | 1717 | } |
32090b8e KR |
1718 | for (sec = abfd->sections; sec; sec = sec->next) |
1719 | { | |
1720 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1721 | i_shdrp[d->this_idx] = &d->this_hdr; | |
1722 | if (d->rel_idx) | |
1723 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
1724 | } | |
1725 | /* Make sure we got everything.... */ | |
1726 | for (i = 0; i < section_number; i++) | |
6ec3bb6a | 1727 | if (i_shdrp[i] == NULL) |
32090b8e | 1728 | abort (); |
9783e04a | 1729 | return true; |
32090b8e KR |
1730 | } |
1731 | ||
1732 | static INLINE file_ptr | |
1733 | assign_file_position_for_section (i_shdrp, offset) | |
1734 | Elf_Internal_Shdr *i_shdrp; | |
1735 | file_ptr offset; | |
1736 | { | |
f035cc47 ILT |
1737 | int align; |
1738 | ||
1739 | if (i_shdrp->sh_addralign != 0) | |
1740 | align = i_shdrp->sh_addralign; | |
1741 | else | |
1742 | align = 1; | |
1743 | i_shdrp->sh_offset = offset = BFD_ALIGN (offset, align); | |
7b8106b4 ILT |
1744 | if (i_shdrp->rawdata != NULL) |
1745 | ((asection *) i_shdrp->rawdata)->filepos = offset; | |
300adb31 KR |
1746 | if (i_shdrp->sh_type != SHT_NOBITS) |
1747 | offset += i_shdrp->sh_size; | |
32090b8e | 1748 | return offset; |
244ffee7 JK |
1749 | } |
1750 | ||
01383fb4 KR |
1751 | static INLINE file_ptr |
1752 | align_file_position (off) | |
1753 | file_ptr off; | |
1754 | { | |
f035cc47 | 1755 | return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1); |
01383fb4 KR |
1756 | } |
1757 | ||
300adb31 | 1758 | static INLINE file_ptr |
6ec3bb6a | 1759 | assign_file_positions_for_symtab_and_strtabs (abfd, off, dosyms) |
300adb31 KR |
1760 | bfd *abfd; |
1761 | file_ptr off; | |
6ec3bb6a | 1762 | boolean dosyms; |
300adb31 KR |
1763 | { |
1764 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
1765 | ||
01383fb4 | 1766 | off = align_file_position (off); |
6ec3bb6a ILT |
1767 | if (dosyms) |
1768 | off = assign_file_position_for_section (&t->symtab_hdr, off); | |
01383fb4 | 1769 | off = assign_file_position_for_section (&t->shstrtab_hdr, off); |
6ec3bb6a ILT |
1770 | if (dosyms) |
1771 | off = assign_file_position_for_section (&t->strtab_hdr, off); | |
300adb31 KR |
1772 | return off; |
1773 | } | |
1774 | ||
1c6042ee ILT |
1775 | struct seg_info |
1776 | { | |
300adb31 KR |
1777 | bfd_vma low, mem_size; |
1778 | file_ptr file_size; | |
1779 | int start_pos; | |
1780 | int sh_flags; | |
1781 | struct seg_info *next; | |
1782 | }; | |
1783 | ||
9783e04a | 1784 | static boolean |
300adb31 KR |
1785 | map_program_segments (abfd) |
1786 | bfd *abfd; | |
1787 | { | |
1788 | Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd); | |
1789 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
1790 | Elf_Internal_Shdr *i_shdrp; | |
1791 | Elf_Internal_Phdr *phdr; | |
80425e6c | 1792 | char *done = NULL; |
68241b2b | 1793 | unsigned int i, n_left = 0; |
300adb31 | 1794 | file_ptr lowest_offset = 0; |
2f3189e7 | 1795 | struct seg_info *seg = NULL; |
300adb31 | 1796 | |
80425e6c | 1797 | done = (char *) malloc (i_ehdrp->e_shnum); |
25057836 | 1798 | if (done == NULL && i_ehdrp->e_shnum != 0) |
80425e6c JK |
1799 | { |
1800 | bfd_set_error (bfd_error_no_memory); | |
1801 | goto error_return; | |
1802 | } | |
300adb31 | 1803 | memset (done, 0, i_ehdrp->e_shnum); |
062189c6 | 1804 | for (i = 1; i < i_ehdrp->e_shnum; i++) |
300adb31 KR |
1805 | { |
1806 | i_shdrp = i_shdrpp[i]; | |
1807 | /* If it's going to be mapped in, it's been assigned a position. */ | |
1808 | if (i_shdrp->sh_offset + 1 == 0) | |
1809 | { | |
1810 | /* Well, not really, but we won't process it here. */ | |
1811 | done[i] = 1; | |
1812 | continue; | |
1813 | } | |
1814 | if (i_shdrp->sh_offset < lowest_offset | |
1815 | || lowest_offset == 0) | |
1816 | lowest_offset = i_shdrp->sh_offset; | |
1817 | /* Only interested in PROGBITS or NOBITS for generating segments. */ | |
1818 | switch (i_shdrp->sh_type) | |
1819 | { | |
1820 | case SHT_PROGBITS: | |
1821 | case SHT_NOBITS: | |
1822 | break; | |
1823 | default: | |
1824 | done[i] = 1; | |
1825 | } | |
1826 | if (!done[i]) | |
1827 | n_left++; | |
1828 | } | |
1829 | while (n_left) | |
1830 | { | |
1831 | bfd_vma lowest_vma = -1, high; | |
1832 | int low_sec = 0; | |
1833 | int mem_size; | |
1834 | int file_size = 0; | |
2f3189e7 ILT |
1835 | struct seg_info *snew; |
1836 | struct seg_info **s_ptr; | |
300adb31 KR |
1837 | |
1838 | for (i = 1; i < i_ehdrp->e_shnum; i++) | |
1839 | { | |
1840 | i_shdrp = i_shdrpp[i]; | |
1841 | if (!done[i] && i_shdrp->sh_addr < lowest_vma) | |
1842 | { | |
1843 | lowest_vma = i_shdrp->sh_addr; | |
1844 | low_sec = i; | |
1845 | } | |
1846 | } | |
1847 | if (low_sec == 0) | |
1848 | abort (); | |
1849 | /* So now we know the lowest vma of any unassigned sections; start | |
1850 | a segment there. */ | |
2f3189e7 ILT |
1851 | snew = (struct seg_info *) bfd_alloc (abfd, sizeof (struct seg_info)); |
1852 | if (!snew) | |
1853 | { | |
1854 | bfd_set_error (bfd_error_no_memory); | |
80425e6c | 1855 | goto error_return; |
2f3189e7 ILT |
1856 | } |
1857 | s_ptr = &seg; | |
1858 | while (*s_ptr != (struct seg_info *) NULL) | |
1859 | s_ptr = &(*s_ptr)->next; | |
1860 | *s_ptr = snew; | |
1861 | snew->next = NULL; | |
1862 | snew->low = lowest_vma; | |
300adb31 | 1863 | i_shdrp = i_shdrpp[low_sec]; |
2f3189e7 ILT |
1864 | snew->start_pos = i_shdrp->sh_offset; |
1865 | snew->sh_flags = i_shdrp->sh_flags; | |
300adb31 KR |
1866 | done[low_sec] = 1, n_left--; |
1867 | mem_size = i_shdrp->sh_size; | |
1868 | high = lowest_vma + i_shdrp->sh_size; | |
1869 | ||
1870 | if (i_shdrp->sh_type == SHT_PROGBITS) | |
1871 | file_size = i_shdrp->sh_size; | |
1872 | ||
062189c6 | 1873 | for (i = 1; i < i_ehdrp->e_shnum; i++) |
300adb31 KR |
1874 | { |
1875 | file_ptr f1; | |
1876 | ||
300adb31 KR |
1877 | if (done[i]) |
1878 | continue; | |
1879 | i_shdrp = i_shdrpp[i]; | |
1880 | /* position of next byte on disk */ | |
2f3189e7 | 1881 | f1 = snew->start_pos + file_size; |
300adb31 KR |
1882 | if (i_shdrp->sh_type == SHT_PROGBITS) |
1883 | { | |
1884 | if (i_shdrp->sh_offset - f1 != i_shdrp->sh_addr - high) | |
1885 | continue; | |
6c35a16d ILT |
1886 | if (file_size != mem_size) |
1887 | break; | |
300adb31 | 1888 | } |
1c6042ee ILT |
1889 | else |
1890 | /* sh_type == NOBITS */ | |
300adb31 KR |
1891 | { |
1892 | /* If the section in question has no contents in the disk | |
1893 | file, we really don't care where it supposedly starts. | |
1894 | But we don't want to bother merging it into this segment | |
1895 | if it doesn't start on this memory page. */ | |
1896 | bfd_vma page1, page2; | |
1897 | bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
1898 | ||
2f3189e7 | 1899 | /* page number in address space of current end of snew */ |
300adb31 KR |
1900 | page1 = (high - 1 + maxpagesize - 1) / maxpagesize; |
1901 | /* page number in address space of start of this section */ | |
1902 | page2 = (i_shdrp->sh_addr + maxpagesize - 1) / maxpagesize; | |
1903 | ||
1904 | if (page1 != page2) | |
1905 | continue; | |
1906 | } | |
1907 | done[i] = 1, n_left--; | |
1908 | if (i_shdrp->sh_type == SHT_PROGBITS) | |
2f3189e7 ILT |
1909 | file_size = i_shdrp->sh_offset + i_shdrp->sh_size - snew->start_pos; |
1910 | mem_size = i_shdrp->sh_addr + i_shdrp->sh_size - snew->low; | |
300adb31 KR |
1911 | high = i_shdrp->sh_addr + i_shdrp->sh_size; |
1912 | i = 0; | |
1913 | } | |
2f3189e7 ILT |
1914 | snew->file_size = file_size; |
1915 | snew->mem_size = mem_size; | |
300adb31 KR |
1916 | } |
1917 | /* Now do something with the list of segments we've built up. */ | |
1918 | { | |
1919 | bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
1920 | struct seg_info *s; | |
1921 | int n_segs = 0; | |
1922 | int sz; | |
1923 | ||
1924 | for (s = seg; s; s = s->next) | |
1925 | { | |
1926 | n_segs++; | |
1927 | } | |
1928 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
1929 | sz = sizeof (Elf_External_Phdr) * n_segs; | |
01383fb4 KR |
1930 | if (align_file_position (i_ehdrp->e_ehsize) + sz <= lowest_offset) |
1931 | i_ehdrp->e_phoff = align_file_position (i_ehdrp->e_ehsize); | |
300adb31 KR |
1932 | else |
1933 | { | |
01383fb4 KR |
1934 | i_ehdrp->e_phoff = align_file_position (elf_tdata (abfd)->next_file_pos); |
1935 | elf_tdata (abfd)->next_file_pos = i_ehdrp->e_phoff + sz; | |
300adb31 | 1936 | } |
1c6042ee ILT |
1937 | phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd, |
1938 | n_segs * sizeof (Elf_Internal_Phdr)); | |
9783e04a DM |
1939 | if (!phdr) |
1940 | { | |
d1ad85a6 | 1941 | bfd_set_error (bfd_error_no_memory); |
1c6042ee | 1942 | abort (); /* FIXME */ |
9783e04a | 1943 | } |
300adb31 KR |
1944 | elf_tdata (abfd)->phdr = phdr; |
1945 | while (seg) | |
1946 | { | |
1947 | phdr->p_type = PT_LOAD; /* only type we really support so far */ | |
1948 | phdr->p_offset = seg->start_pos; | |
1949 | phdr->p_vaddr = seg->low; | |
1950 | phdr->p_paddr = 0; | |
1951 | phdr->p_filesz = seg->file_size; | |
1952 | phdr->p_memsz = seg->mem_size; | |
1953 | phdr->p_flags = PF_R; | |
1c6042ee | 1954 | phdr->p_align = maxpagesize; /* ? */ |
300adb31 | 1955 | if (seg->sh_flags & SHF_WRITE) |
e621c5cc ILT |
1956 | /* SysVr4 ELF docs say "data segments normally have read, write, |
1957 | and execute permissions." */ | |
1958 | phdr->p_flags |= (PF_W | PF_X); | |
300adb31 KR |
1959 | if (seg->sh_flags & SHF_EXECINSTR) |
1960 | phdr->p_flags |= PF_X; | |
1961 | phdr++; | |
1962 | seg = seg->next; | |
1963 | } | |
1964 | i_ehdrp->e_phnum = n_segs; | |
1965 | } | |
1966 | elf_write_phdrs (abfd, i_ehdrp, elf_tdata (abfd)->phdr, i_ehdrp->e_phnum); | |
80425e6c JK |
1967 | if (done != NULL) |
1968 | free (done); | |
9783e04a | 1969 | return true; |
1c6042ee | 1970 | error_return: |
80425e6c JK |
1971 | if (done != NULL) |
1972 | free (done); | |
1973 | return false; | |
300adb31 KR |
1974 | } |
1975 | ||
9783e04a | 1976 | static boolean |
6ec3bb6a | 1977 | assign_file_positions_except_relocs (abfd, dosyms) |
32090b8e | 1978 | bfd *abfd; |
6ec3bb6a | 1979 | boolean dosyms; |
244ffee7 | 1980 | { |
32090b8e KR |
1981 | /* For now, we ignore the possibility of having program segments, which |
1982 | may require some alignment in the file. That'll require padding, and | |
1983 | some interesting calculations to optimize file space usage. | |
244ffee7 | 1984 | |
32090b8e KR |
1985 | Also, since the application may change the list of relocations for |
1986 | a given section, we don't figure them in here. We'll put them at the | |
1987 | end of the file, at positions computed during bfd_close. | |
244ffee7 | 1988 | |
300adb31 KR |
1989 | The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ... |
1990 | or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */ | |
32090b8e | 1991 | |
062189c6 | 1992 | struct elf_obj_tdata *t = elf_tdata (abfd); |
32090b8e | 1993 | file_ptr off; |
68241b2b | 1994 | unsigned int i; |
32090b8e KR |
1995 | Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd); |
1996 | Elf_Internal_Shdr *i_shdrp; | |
1997 | Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); | |
300adb31 | 1998 | int exec_p = (abfd->flags & EXEC_P) != 0; |
6c35a16d | 1999 | bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize; |
32090b8e | 2000 | |
300adb31 | 2001 | /* Everything starts after the ELF file header. */ |
32090b8e | 2002 | off = i_ehdrp->e_ehsize; |
300adb31 KR |
2003 | |
2004 | if (!exec_p) | |
2005 | { | |
2006 | /* Section headers. */ | |
01383fb4 | 2007 | off = align_file_position (off); |
300adb31 KR |
2008 | i_ehdrp->e_shoff = off; |
2009 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
6ec3bb6a | 2010 | off = assign_file_positions_for_symtab_and_strtabs (abfd, off, dosyms); |
300adb31 | 2011 | } |
062189c6 | 2012 | for (i = 1; i < i_ehdrp->e_shnum; i++) |
32090b8e | 2013 | { |
062189c6 ILT |
2014 | /* The symtab and strtab sections are placed by |
2015 | assign_file_positions_for_symtab_and_strtabs. */ | |
2016 | if (i == t->symtab_section | |
2017 | || i == t->strtab_section | |
2018 | || i == t->shstrtab_section) | |
2019 | continue; | |
2020 | ||
32090b8e KR |
2021 | i_shdrp = i_shdrpp[i]; |
2022 | if (i_shdrp->sh_type == SHT_REL || i_shdrp->sh_type == SHT_RELA) | |
244ffee7 | 2023 | { |
32090b8e KR |
2024 | i_shdrp->sh_offset = -1; |
2025 | continue; | |
244ffee7 | 2026 | } |
300adb31 KR |
2027 | if (exec_p) |
2028 | { | |
300adb31 KR |
2029 | if (maxpagesize == 0) |
2030 | maxpagesize = 1; /* make the arithmetic work */ | |
2031 | /* This isn't necessarily going to give the best packing, if the | |
2032 | segments require padding between them, but since that isn't | |
2033 | usually the case, this'll do. */ | |
2034 | if ((i_shdrp->sh_flags & SHF_ALLOC) == 0) | |
2035 | { | |
2036 | i_shdrp->sh_offset = -1; | |
2037 | continue; | |
2038 | } | |
2039 | /* Blindly assume that the segments are ordered optimally. With | |
2040 | the default LD script, they will be. */ | |
6c35a16d | 2041 | if (i_shdrp->sh_type != SHT_NOBITS) |
300adb31 | 2042 | { |
6c35a16d ILT |
2043 | /* need big unsigned type */ |
2044 | bfd_vma addtl_off; | |
2045 | addtl_off = i_shdrp->sh_addr - off; | |
2046 | addtl_off = addtl_off % maxpagesize; | |
2047 | if (addtl_off) | |
2048 | { | |
2049 | off += addtl_off; | |
2050 | } | |
300adb31 KR |
2051 | } |
2052 | } | |
32090b8e | 2053 | off = assign_file_position_for_section (i_shdrp, off); |
01383fb4 | 2054 | |
300adb31 | 2055 | if (exec_p |
bf08d1e2 | 2056 | && (abfd->flags & D_PAGED) != 0 |
1c6042ee | 2057 | && get_elf_backend_data (abfd)->maxpagesize > 1 |
300adb31 KR |
2058 | && i_shdrp->sh_type == SHT_PROGBITS |
2059 | && (i_shdrp->sh_flags & SHF_ALLOC) | |
01383fb4 | 2060 | && (i_shdrp->sh_offset - i_shdrp->sh_addr) % get_elf_backend_data (abfd)->maxpagesize != 0) |
300adb31 KR |
2061 | abort (); |
2062 | } | |
2063 | if (exec_p) | |
2064 | { | |
2065 | elf_tdata (abfd)->next_file_pos = off; | |
9783e04a DM |
2066 | if (!map_program_segments (abfd)) |
2067 | return false; | |
300adb31 KR |
2068 | off = elf_tdata (abfd)->next_file_pos; |
2069 | ||
2070 | /* Section headers. */ | |
01383fb4 | 2071 | off = align_file_position (off); |
300adb31 KR |
2072 | i_ehdrp->e_shoff = off; |
2073 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
2074 | ||
6ec3bb6a | 2075 | off = assign_file_positions_for_symtab_and_strtabs (abfd, off, dosyms); |
300adb31 | 2076 | |
062189c6 | 2077 | for (i = 1; i < i_ehdrp->e_shnum; i++) |
300adb31 KR |
2078 | { |
2079 | i_shdrp = i_shdrpp[i]; | |
2080 | if (i_shdrp->sh_offset + 1 == 0 | |
2081 | && i_shdrp->sh_type != SHT_REL | |
2082 | && i_shdrp->sh_type != SHT_RELA) | |
2083 | off = assign_file_position_for_section (i_shdrp, off); | |
2084 | } | |
244ffee7 | 2085 | } |
32090b8e | 2086 | elf_tdata (abfd)->next_file_pos = off; |
9783e04a | 2087 | return true; |
244ffee7 JK |
2088 | } |
2089 | ||
32090b8e KR |
2090 | static boolean |
2091 | prep_headers (abfd) | |
2092 | bfd *abfd; | |
2093 | { | |
32090b8e | 2094 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ |
1c6042ee | 2095 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ |
32090b8e | 2096 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ |
32090b8e | 2097 | int count; |
32090b8e | 2098 | struct strtab *shstrtab; |
244ffee7 | 2099 | |
32090b8e KR |
2100 | i_ehdrp = elf_elfheader (abfd); |
2101 | i_shdrp = elf_elfsections (abfd); | |
244ffee7 | 2102 | |
32090b8e | 2103 | shstrtab = bfd_new_strtab (abfd); |
b9d5cdf0 DM |
2104 | if (!shstrtab) |
2105 | return false; | |
1c6042ee | 2106 | |
32090b8e | 2107 | elf_shstrtab (abfd) = shstrtab; |
244ffee7 | 2108 | |
32090b8e KR |
2109 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; |
2110 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
2111 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
2112 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
244ffee7 | 2113 | |
32090b8e KR |
2114 | i_ehdrp->e_ident[EI_CLASS] = ELFCLASS; |
2115 | i_ehdrp->e_ident[EI_DATA] = | |
2116 | abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB; | |
2117 | i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT; | |
244ffee7 | 2118 | |
32090b8e KR |
2119 | for (count = EI_PAD; count < EI_NIDENT; count++) |
2120 | i_ehdrp->e_ident[count] = 0; | |
244ffee7 | 2121 | |
32090b8e KR |
2122 | i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL; |
2123 | switch (bfd_get_arch (abfd)) | |
fce36137 | 2124 | { |
32090b8e KR |
2125 | case bfd_arch_unknown: |
2126 | i_ehdrp->e_machine = EM_NONE; | |
2127 | break; | |
2128 | case bfd_arch_sparc: | |
2129 | i_ehdrp->e_machine = EM_SPARC; | |
2130 | /* start-sanitize-v9 */ | |
2131 | #if ARCH_SIZE == 64 | |
2132 | i_ehdrp->e_machine = EM_SPARC64; | |
2133 | #endif | |
2134 | /* end-sanitize-v9 */ | |
2135 | break; | |
2136 | case bfd_arch_i386: | |
2137 | i_ehdrp->e_machine = EM_386; | |
2138 | break; | |
2139 | case bfd_arch_m68k: | |
2140 | i_ehdrp->e_machine = EM_68K; | |
2141 | break; | |
2142 | case bfd_arch_m88k: | |
2143 | i_ehdrp->e_machine = EM_88K; | |
2144 | break; | |
2145 | case bfd_arch_i860: | |
2146 | i_ehdrp->e_machine = EM_860; | |
2147 | break; | |
2148 | case bfd_arch_mips: /* MIPS Rxxxx */ | |
2149 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
2150 | break; | |
2151 | case bfd_arch_hppa: | |
459ae909 | 2152 | i_ehdrp->e_machine = EM_PARISC; |
32090b8e | 2153 | break; |
99ec1f66 ILT |
2154 | case bfd_arch_powerpc: |
2155 | i_ehdrp->e_machine = EM_CYGNUS_POWERPC; | |
2156 | break; | |
32090b8e KR |
2157 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ |
2158 | default: | |
2159 | i_ehdrp->e_machine = EM_NONE; | |
fce36137 | 2160 | } |
32090b8e KR |
2161 | i_ehdrp->e_version = EV_CURRENT; |
2162 | i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr); | |
244ffee7 | 2163 | |
32090b8e KR |
2164 | /* no program header, for now. */ |
2165 | i_ehdrp->e_phoff = 0; | |
2166 | i_ehdrp->e_phentsize = 0; | |
2167 | i_ehdrp->e_phnum = 0; | |
244ffee7 | 2168 | |
32090b8e KR |
2169 | /* each bfd section is section header entry */ |
2170 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
2171 | i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr); | |
244ffee7 | 2172 | |
32090b8e KR |
2173 | /* if we're building an executable, we'll need a program header table */ |
2174 | if (abfd->flags & EXEC_P) | |
244ffee7 | 2175 | { |
300adb31 | 2176 | /* it all happens later */ |
32090b8e KR |
2177 | #if 0 |
2178 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
244ffee7 | 2179 | |
32090b8e KR |
2180 | /* elf_build_phdrs() returns a (NULL-terminated) array of |
2181 | Elf_Internal_Phdrs */ | |
2182 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
2183 | i_ehdrp->e_phoff = outbase; | |
2184 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
2185 | #endif | |
244ffee7 | 2186 | } |
32090b8e | 2187 | else |
244ffee7 | 2188 | { |
32090b8e KR |
2189 | i_ehdrp->e_phentsize = 0; |
2190 | i_phdrp = 0; | |
2191 | i_ehdrp->e_phoff = 0; | |
244ffee7 JK |
2192 | } |
2193 | ||
32090b8e KR |
2194 | elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab, |
2195 | ".symtab"); | |
2196 | elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab, | |
2197 | ".strtab"); | |
2198 | elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab, | |
2199 | ".shstrtab"); | |
6ec3bb6a ILT |
2200 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
2201 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2202 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
2203 | return false; | |
2204 | ||
f035cc47 | 2205 | return true; |
244ffee7 JK |
2206 | } |
2207 | ||
b9d5cdf0 | 2208 | static boolean |
32090b8e KR |
2209 | swap_out_syms (abfd) |
2210 | bfd *abfd; | |
244ffee7 | 2211 | { |
9783e04a DM |
2212 | if (!elf_map_symbols (abfd)) |
2213 | return false; | |
244ffee7 | 2214 | |
32090b8e KR |
2215 | /* Dump out the symtabs. */ |
2216 | { | |
2217 | int symcount = bfd_get_symcount (abfd); | |
2218 | asymbol **syms = bfd_get_outsymbols (abfd); | |
2219 | struct strtab *stt = bfd_new_strtab (abfd); | |
2220 | Elf_Internal_Shdr *symtab_hdr; | |
2221 | Elf_Internal_Shdr *symstrtab_hdr; | |
2222 | Elf_External_Sym *outbound_syms; | |
2223 | int idx; | |
244ffee7 | 2224 | |
b9d5cdf0 DM |
2225 | if (!stt) |
2226 | return false; | |
32090b8e KR |
2227 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2228 | symtab_hdr->sh_type = SHT_SYMTAB; | |
2229 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
2230 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
2231 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
244ffee7 | 2232 | |
062189c6 ILT |
2233 | /* FIXME: Systems I've checked use 4 byte alignment for .symtab, |
2234 | but it is possible that there are systems which use a different | |
2235 | alignment. */ | |
2236 | symtab_hdr->sh_addralign = 4; | |
2237 | ||
32090b8e KR |
2238 | /* see assert in elf_fake_sections that supports this: */ |
2239 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
2240 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
244ffee7 | 2241 | |
32090b8e KR |
2242 | outbound_syms = (Elf_External_Sym *) |
2243 | bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym)); | |
9783e04a DM |
2244 | if (!outbound_syms) |
2245 | { | |
d1ad85a6 | 2246 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2247 | return false; |
2248 | } | |
32090b8e KR |
2249 | /* now generate the data (for "contents") */ |
2250 | { | |
2251 | /* Fill in zeroth symbol and swap it out. */ | |
2252 | Elf_Internal_Sym sym; | |
2253 | sym.st_name = 0; | |
2254 | sym.st_value = 0; | |
2255 | sym.st_size = 0; | |
2256 | sym.st_info = 0; | |
2257 | sym.st_other = 0; | |
2258 | sym.st_shndx = SHN_UNDEF; | |
2259 | elf_swap_symbol_out (abfd, &sym, outbound_syms); | |
244ffee7 | 2260 | } |
32090b8e KR |
2261 | for (idx = 0; idx < symcount; idx++) |
2262 | { | |
2263 | Elf_Internal_Sym sym; | |
2264 | bfd_vma value = syms[idx]->value; | |
244ffee7 | 2265 | |
32090b8e KR |
2266 | if (syms[idx]->flags & BSF_SECTION_SYM) |
2267 | /* Section symbols have no names. */ | |
2268 | sym.st_name = 0; | |
2269 | else | |
6ec3bb6a ILT |
2270 | { |
2271 | sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name); | |
2272 | if (sym.st_name == (unsigned long) -1) | |
2273 | return false; | |
2274 | } | |
244ffee7 | 2275 | |
32090b8e | 2276 | if (bfd_is_com_section (syms[idx]->section)) |
244ffee7 | 2277 | { |
32090b8e KR |
2278 | /* ELF common symbols put the alignment into the `value' field, |
2279 | and the size into the `size' field. This is backwards from | |
2280 | how BFD handles it, so reverse it here. */ | |
2281 | sym.st_size = value; | |
2282 | /* Should retrieve this from somewhere... */ | |
2283 | sym.st_value = 16; | |
d4fb8fce ILT |
2284 | sym.st_shndx = elf_section_from_bfd_section (abfd, |
2285 | syms[idx]->section); | |
244ffee7 JK |
2286 | } |
2287 | else | |
2288 | { | |
32090b8e | 2289 | asection *sec = syms[idx]->section; |
e74034d8 | 2290 | elf_symbol_type *type_ptr; |
32090b8e | 2291 | int shndx; |
244ffee7 | 2292 | |
32090b8e KR |
2293 | if (sec->output_section) |
2294 | { | |
2295 | value += sec->output_offset; | |
2296 | sec = sec->output_section; | |
2297 | } | |
2298 | value += sec->vma; | |
2299 | sym.st_value = value; | |
e74034d8 KR |
2300 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
2301 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; | |
32090b8e KR |
2302 | sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec); |
2303 | if (shndx == -1) | |
2304 | { | |
2305 | asection *sec2; | |
2306 | /* Writing this would be a hell of a lot easier if we had | |
2307 | some decent documentation on bfd, and knew what to expect | |
2308 | of the library, and what to demand of applications. For | |
2309 | example, it appears that `objcopy' might not set the | |
2310 | section of a symbol to be a section that is actually in | |
2311 | the output file. */ | |
2312 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
850584ad | 2313 | BFD_ASSERT (sec2 != 0); |
32090b8e | 2314 | sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2); |
850584ad | 2315 | BFD_ASSERT (shndx != -1); |
32090b8e KR |
2316 | } |
2317 | } | |
244ffee7 | 2318 | |
32090b8e | 2319 | if (bfd_is_com_section (syms[idx]->section)) |
38a5f510 | 2320 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT); |
32090b8e KR |
2321 | else if (syms[idx]->section == &bfd_und_section) |
2322 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE); | |
32090b8e KR |
2323 | else if (syms[idx]->flags & BSF_SECTION_SYM) |
2324 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
2325 | else if (syms[idx]->flags & BSF_FILE) | |
2326 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
d24928c0 | 2327 | else |
32090b8e | 2328 | { |
d24928c0 KR |
2329 | int bind = STB_LOCAL; |
2330 | int type = STT_OBJECT; | |
2331 | unsigned int flags = syms[idx]->flags; | |
2332 | ||
2333 | if (flags & BSF_LOCAL) | |
2334 | bind = STB_LOCAL; | |
2335 | else if (flags & BSF_WEAK) | |
2336 | bind = STB_WEAK; | |
2337 | else if (flags & BSF_GLOBAL) | |
2338 | bind = STB_GLOBAL; | |
2339 | ||
2340 | if (flags & BSF_FUNCTION) | |
2341 | type = STT_FUNC; | |
2342 | ||
2343 | sym.st_info = ELF_ST_INFO (bind, type); | |
32090b8e | 2344 | } |
244ffee7 | 2345 | |
32090b8e KR |
2346 | sym.st_other = 0; |
2347 | elf_swap_symbol_out (abfd, &sym, | |
d24928c0 KR |
2348 | (outbound_syms |
2349 | + elf_sym_extra (abfd)[idx].elf_sym_num)); | |
32090b8e KR |
2350 | } |
2351 | ||
2352 | symtab_hdr->contents = (PTR) outbound_syms; | |
2353 | symstrtab_hdr->contents = (PTR) stt->tab; | |
2354 | symstrtab_hdr->sh_size = stt->length; | |
2355 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
2356 | ||
2357 | symstrtab_hdr->sh_flags = 0; | |
2358 | symstrtab_hdr->sh_addr = 0; | |
2359 | symstrtab_hdr->sh_entsize = 0; | |
2360 | symstrtab_hdr->sh_link = 0; | |
2361 | symstrtab_hdr->sh_info = 0; | |
062189c6 | 2362 | symstrtab_hdr->sh_addralign = 1; |
32090b8e KR |
2363 | symstrtab_hdr->size = 0; |
2364 | } | |
2365 | ||
b9d5cdf0 | 2366 | return true; |
244ffee7 JK |
2367 | } |
2368 | ||
32090b8e KR |
2369 | static boolean |
2370 | write_shdrs_and_ehdr (abfd) | |
2371 | bfd *abfd; | |
244ffee7 | 2372 | { |
32090b8e KR |
2373 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
2374 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
32090b8e KR |
2375 | Elf_External_Shdr *x_shdrp; /* Section header table, external form */ |
2376 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
68241b2b | 2377 | unsigned int count; |
32090b8e | 2378 | struct strtab *shstrtab; |
244ffee7 | 2379 | |
32090b8e KR |
2380 | i_ehdrp = elf_elfheader (abfd); |
2381 | i_shdrp = elf_elfsections (abfd); | |
2382 | shstrtab = elf_shstrtab (abfd); | |
2383 | ||
2384 | /* swap the header before spitting it out... */ | |
2385 | ||
2386 | #if DEBUG & 1 | |
2387 | elf_debug_file (i_ehdrp); | |
244ffee7 | 2388 | #endif |
32090b8e | 2389 | elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr); |
4002f18a ILT |
2390 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 |
2391 | || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) | |
2392 | != sizeof (x_ehdr))) | |
2393 | return false; | |
244ffee7 | 2394 | |
32090b8e KR |
2395 | /* at this point we've concocted all the ELF sections... */ |
2396 | x_shdrp = (Elf_External_Shdr *) | |
2397 | bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum)); | |
2398 | if (!x_shdrp) | |
2399 | { | |
d1ad85a6 | 2400 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
2401 | return false; |
2402 | } | |
2403 | ||
2404 | for (count = 0; count < i_ehdrp->e_shnum; count++) | |
2405 | { | |
2406 | #if DEBUG & 2 | |
2407 | elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count, | |
2408 | i_shdrp[count]); | |
244ffee7 | 2409 | #endif |
32090b8e KR |
2410 | elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count); |
2411 | } | |
4002f18a ILT |
2412 | if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0 |
2413 | || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd) | |
d909628b | 2414 | != sizeof (*x_shdrp) * i_ehdrp->e_shnum)) |
4002f18a ILT |
2415 | return false; |
2416 | ||
32090b8e | 2417 | /* need to dump the string table too... */ |
244ffee7 | 2418 | |
32090b8e KR |
2419 | return true; |
2420 | } | |
244ffee7 | 2421 | |
32090b8e KR |
2422 | static void |
2423 | assign_file_positions_for_relocs (abfd) | |
2424 | bfd *abfd; | |
2425 | { | |
1c6042ee | 2426 | file_ptr off = elf_tdata (abfd)->next_file_pos; |
68241b2b | 2427 | unsigned int i; |
32090b8e KR |
2428 | Elf_Internal_Shdr **shdrpp = elf_elfsections (abfd); |
2429 | Elf_Internal_Shdr *shdrp; | |
1c6042ee | 2430 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) |
32090b8e KR |
2431 | { |
2432 | shdrp = shdrpp[i]; | |
2433 | if (shdrp->sh_type != SHT_REL && shdrp->sh_type != SHT_RELA) | |
2434 | continue; | |
01383fb4 | 2435 | off = align_file_position (off); |
32090b8e KR |
2436 | off = assign_file_position_for_section (shdrp, off); |
2437 | } | |
1c6042ee | 2438 | elf_tdata (abfd)->next_file_pos = off; |
32090b8e | 2439 | } |
244ffee7 | 2440 | |
32090b8e | 2441 | boolean |
1c6042ee ILT |
2442 | NAME(bfd_elf,write_object_contents) (abfd) |
2443 | bfd *abfd; | |
32090b8e | 2444 | { |
062189c6 | 2445 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
32090b8e KR |
2446 | Elf_Internal_Ehdr *i_ehdrp; |
2447 | Elf_Internal_Shdr **i_shdrp; | |
68241b2b | 2448 | unsigned int count; |
244ffee7 | 2449 | |
38a5f510 ILT |
2450 | /* We don't know how to write dynamic objects. Specifically, we |
2451 | don't know how to construct the program header. */ | |
2452 | if ((abfd->flags & DYNAMIC) != 0) | |
2453 | { | |
2454 | fprintf (stderr, "Writing ELF dynamic objects is not supported\n"); | |
d1ad85a6 | 2455 | bfd_set_error (bfd_error_wrong_format); |
38a5f510 ILT |
2456 | return false; |
2457 | } | |
2458 | ||
6ec3bb6a ILT |
2459 | if (! abfd->output_has_begun |
2460 | && ! elf_compute_section_file_positions (abfd, | |
2461 | (struct bfd_link_info *) NULL)) | |
2462 | return false; | |
244ffee7 | 2463 | |
32090b8e KR |
2464 | i_shdrp = elf_elfsections (abfd); |
2465 | i_ehdrp = elf_elfheader (abfd); | |
244ffee7 | 2466 | |
32090b8e | 2467 | bfd_map_over_sections (abfd, write_relocs, (PTR) 0); |
32090b8e | 2468 | assign_file_positions_for_relocs (abfd); |
244ffee7 | 2469 | |
32090b8e | 2470 | /* After writing the headers, we need to write the sections too... */ |
062189c6 | 2471 | for (count = 1; count < i_ehdrp->e_shnum; count++) |
e621c5cc | 2472 | { |
e621c5cc ILT |
2473 | if (bed->elf_backend_section_processing) |
2474 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
2475 | if (i_shdrp[count]->contents) | |
2476 | { | |
4002f18a ILT |
2477 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
2478 | || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, | |
2479 | 1, abfd) | |
2480 | != i_shdrp[count]->sh_size)) | |
2481 | return false; | |
e621c5cc ILT |
2482 | } |
2483 | } | |
062189c6 ILT |
2484 | |
2485 | if (bed->elf_backend_final_write_processing) | |
2486 | (*bed->elf_backend_final_write_processing) (abfd); | |
2487 | ||
32090b8e KR |
2488 | return write_shdrs_and_ehdr (abfd); |
2489 | } | |
244ffee7 | 2490 | |
32090b8e KR |
2491 | /* Given an index of a section, retrieve a pointer to it. Note |
2492 | that for our purposes, sections are indexed by {1, 2, ...} with | |
2493 | 0 being an illegal index. */ | |
244ffee7 | 2494 | |
32090b8e KR |
2495 | /* In the original, each ELF section went into exactly one BFD |
2496 | section. This doesn't really make sense, so we need a real mapping. | |
2497 | The mapping has to hide in the Elf_Internal_Shdr since asection | |
2498 | doesn't have anything like a tdata field... */ | |
244ffee7 | 2499 | |
2e03ce18 | 2500 | static asection * |
1c6042ee ILT |
2501 | section_from_elf_index (abfd, index) |
2502 | bfd *abfd; | |
2503 | unsigned int index; | |
32090b8e KR |
2504 | { |
2505 | /* @@ Is bfd_com_section really correct in all the places it could | |
2506 | be returned from this routine? */ | |
244ffee7 | 2507 | |
32090b8e KR |
2508 | if (index == SHN_ABS) |
2509 | return &bfd_com_section; /* not abs? */ | |
2510 | if (index == SHN_COMMON) | |
2511 | return &bfd_com_section; | |
244ffee7 | 2512 | |
32090b8e | 2513 | if (index > elf_elfheader (abfd)->e_shnum) |
2e03ce18 | 2514 | return NULL; |
244ffee7 JK |
2515 | |
2516 | { | |
32090b8e | 2517 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index]; |
244ffee7 | 2518 | |
32090b8e | 2519 | switch (hdr->sh_type) |
244ffee7 | 2520 | { |
32090b8e KR |
2521 | /* ELF sections that map to BFD sections */ |
2522 | case SHT_PROGBITS: | |
2523 | case SHT_NOBITS: | |
2e03ce18 ILT |
2524 | if (hdr->rawdata == NULL) |
2525 | { | |
2526 | if (! bfd_section_from_shdr (abfd, index)) | |
2527 | return NULL; | |
2528 | } | |
32090b8e | 2529 | return (struct sec *) hdr->rawdata; |
244ffee7 | 2530 | |
32090b8e KR |
2531 | default: |
2532 | return (struct sec *) &bfd_abs_section; | |
244ffee7 | 2533 | } |
244ffee7 | 2534 | } |
32090b8e | 2535 | } |
244ffee7 | 2536 | |
32090b8e KR |
2537 | /* given a section, search the header to find them... */ |
2538 | static int | |
1c6042ee ILT |
2539 | elf_section_from_bfd_section (abfd, asect) |
2540 | bfd *abfd; | |
2541 | struct sec *asect; | |
32090b8e KR |
2542 | { |
2543 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); | |
2544 | int index; | |
2545 | Elf_Internal_Shdr *hdr; | |
2546 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
244ffee7 | 2547 | |
32090b8e KR |
2548 | if (asect == &bfd_abs_section) |
2549 | return SHN_ABS; | |
2550 | if (asect == &bfd_com_section) | |
2551 | return SHN_COMMON; | |
2552 | if (asect == &bfd_und_section) | |
2553 | return SHN_UNDEF; | |
244ffee7 | 2554 | |
32090b8e KR |
2555 | for (index = 0; index < maxindex; index++) |
2556 | { | |
2557 | hdr = i_shdrp[index]; | |
2558 | switch (hdr->sh_type) | |
2559 | { | |
2560 | /* ELF sections that map to BFD sections */ | |
2561 | case SHT_PROGBITS: | |
2562 | case SHT_NOBITS: | |
e621c5cc | 2563 | case SHT_NOTE: |
32090b8e KR |
2564 | if (hdr->rawdata) |
2565 | { | |
2566 | if (((struct sec *) (hdr->rawdata)) == asect) | |
2567 | return index; | |
2568 | } | |
2569 | break; | |
01383fb4 KR |
2570 | |
2571 | case SHT_STRTAB: | |
2572 | /* fix_up_strtabs will generate STRTAB sections with names | |
2573 | of .stab*str. */ | |
2574 | if (!strncmp (asect->name, ".stab", 5) | |
2575 | && !strcmp ("str", asect->name + strlen (asect->name) - 3)) | |
2576 | { | |
2577 | if (hdr->rawdata) | |
2578 | { | |
2579 | if (((struct sec *) (hdr->rawdata)) == asect) | |
2580 | return index; | |
2581 | } | |
2582 | break; | |
2583 | } | |
2584 | /* FALL THROUGH */ | |
32090b8e | 2585 | default: |
e621c5cc ILT |
2586 | { |
2587 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
2588 | ||
2589 | if (bed->elf_backend_section_from_bfd_section) | |
f035cc47 ILT |
2590 | { |
2591 | int retval; | |
2592 | ||
2593 | retval = index; | |
2594 | if ((*bed->elf_backend_section_from_bfd_section) | |
2595 | (abfd, hdr, asect, &retval)) | |
2596 | return retval; | |
2597 | } | |
e621c5cc | 2598 | } |
32090b8e KR |
2599 | break; |
2600 | } | |
2601 | } | |
2602 | return -1; | |
2603 | } | |
244ffee7 | 2604 | |
32090b8e KR |
2605 | /* given a symbol, return the bfd index for that symbol. */ |
2606 | static int | |
1c6042ee ILT |
2607 | elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) |
2608 | bfd *abfd; | |
2609 | struct symbol_cache_entry **asym_ptr_ptr; | |
32090b8e KR |
2610 | { |
2611 | struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr; | |
32090b8e | 2612 | int idx; |
d24928c0 | 2613 | flagword flags = asym_ptr->flags; |
32090b8e | 2614 | |
d24928c0 KR |
2615 | /* When gas creates relocations against local labels, it creates its |
2616 | own symbol for the section, but does put the symbol into the | |
e621c5cc ILT |
2617 | symbol chain, so udata is 0. When the linker is generating |
2618 | relocatable output, this section symbol may be for one of the | |
2619 | input sections rather than the output section. */ | |
d24928c0 KR |
2620 | if (asym_ptr->udata == (PTR) 0 |
2621 | && (flags & BSF_SECTION_SYM) | |
e621c5cc ILT |
2622 | && asym_ptr->section) |
2623 | { | |
2624 | int indx; | |
2625 | ||
2626 | if (asym_ptr->section->output_section != NULL) | |
2627 | indx = asym_ptr->section->output_section->index; | |
2628 | else | |
2629 | indx = asym_ptr->section->index; | |
2630 | if (elf_section_syms (abfd)[indx]) | |
2631 | asym_ptr->udata = elf_section_syms (abfd)[indx]->udata; | |
01383fb4 | 2632 | } |
e621c5cc | 2633 | |
d24928c0 | 2634 | if (asym_ptr->udata) |
1c6042ee | 2635 | idx = ((Elf_Sym_Extra *) asym_ptr->udata)->elf_sym_num; |
d24928c0 | 2636 | else |
32090b8e | 2637 | { |
32090b8e KR |
2638 | abort (); |
2639 | } | |
244ffee7 | 2640 | |
32090b8e | 2641 | #if DEBUG & 4 |
244ffee7 | 2642 | { |
244ffee7 | 2643 | |
32090b8e | 2644 | fprintf (stderr, |
d24928c0 | 2645 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n", |
1c6042ee | 2646 | (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags)); |
32090b8e KR |
2647 | fflush (stderr); |
2648 | } | |
2649 | #endif | |
2650 | ||
2651 | return idx; | |
2652 | } | |
2653 | ||
cb71adf1 PS |
2654 | static long |
2655 | elf_slurp_symbol_table (abfd, symptrs, dynamic) | |
1c6042ee ILT |
2656 | bfd *abfd; |
2657 | asymbol **symptrs; /* Buffer for generated bfd symbols */ | |
cb71adf1 | 2658 | boolean dynamic; |
32090b8e | 2659 | { |
cb71adf1 | 2660 | Elf_Internal_Shdr *hdr; |
7d8aaf36 | 2661 | long symcount; /* Number of external ELF symbols */ |
32090b8e KR |
2662 | elf_symbol_type *sym; /* Pointer to current bfd symbol */ |
2663 | elf_symbol_type *symbase; /* Buffer for generated bfd symbols */ | |
2664 | Elf_Internal_Sym i_sym; | |
80425e6c | 2665 | Elf_External_Sym *x_symp = NULL; |
32090b8e | 2666 | |
32090b8e KR |
2667 | /* Read each raw ELF symbol, converting from external ELF form to |
2668 | internal ELF form, and then using the information to create a | |
2669 | canonical bfd symbol table entry. | |
244ffee7 | 2670 | |
32090b8e KR |
2671 | Note that we allocate the initial bfd canonical symbol buffer |
2672 | based on a one-to-one mapping of the ELF symbols to canonical | |
2673 | symbols. We actually use all the ELF symbols, so there will be no | |
2674 | space left over at the end. When we have all the symbols, we | |
2675 | build the caller's pointer vector. */ | |
244ffee7 | 2676 | |
cb71adf1 PS |
2677 | if (dynamic) |
2678 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2679 | else | |
2680 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
32090b8e | 2681 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) |
cb71adf1 | 2682 | return -1; |
244ffee7 | 2683 | |
32090b8e | 2684 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
244ffee7 | 2685 | |
7d8aaf36 ILT |
2686 | if (symcount == 0) |
2687 | sym = symbase = NULL; | |
2688 | else | |
244ffee7 | 2689 | { |
7d8aaf36 | 2690 | long i; |
244ffee7 | 2691 | |
7d8aaf36 | 2692 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) |
cb71adf1 | 2693 | return -1; |
7d8aaf36 ILT |
2694 | |
2695 | symbase = ((elf_symbol_type *) | |
2696 | bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type))); | |
2697 | if (symbase == (elf_symbol_type *) NULL) | |
32090b8e | 2698 | { |
7d8aaf36 | 2699 | bfd_set_error (bfd_error_no_memory); |
cb71adf1 | 2700 | return -1; |
32090b8e | 2701 | } |
7d8aaf36 ILT |
2702 | sym = symbase; |
2703 | ||
2704 | /* Temporarily allocate room for the raw ELF symbols. */ | |
2705 | x_symp = ((Elf_External_Sym *) | |
80425e6c | 2706 | malloc (symcount * sizeof (Elf_External_Sym))); |
25057836 | 2707 | if (x_symp == NULL && symcount != 0) |
80425e6c JK |
2708 | { |
2709 | bfd_set_error (bfd_error_no_memory); | |
2710 | goto error_return; | |
2711 | } | |
7d8aaf36 ILT |
2712 | |
2713 | if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd) | |
2714 | != symcount * sizeof (Elf_External_Sym)) | |
25057836 | 2715 | goto error_return; |
7d8aaf36 ILT |
2716 | /* Skip first symbol, which is a null dummy. */ |
2717 | for (i = 1; i < symcount; i++) | |
32090b8e | 2718 | { |
7d8aaf36 ILT |
2719 | elf_swap_symbol_in (abfd, x_symp + i, &i_sym); |
2720 | memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym)); | |
2721 | #ifdef ELF_KEEP_EXTSYM | |
2722 | memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym)); | |
2723 | #endif | |
2724 | sym->symbol.the_bfd = abfd; | |
244ffee7 | 2725 | |
7d8aaf36 ILT |
2726 | sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link, |
2727 | i_sym.st_name); | |
244ffee7 | 2728 | |
7d8aaf36 | 2729 | sym->symbol.value = i_sym.st_value; |
244ffee7 | 2730 | |
6ec3bb6a | 2731 | if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE) |
7d8aaf36 ILT |
2732 | { |
2733 | sym->symbol.section = section_from_elf_index (abfd, | |
2734 | i_sym.st_shndx); | |
2e03ce18 ILT |
2735 | if (sym->symbol.section == NULL) |
2736 | goto error_return; | |
7d8aaf36 ILT |
2737 | } |
2738 | else if (i_sym.st_shndx == SHN_ABS) | |
2739 | { | |
2740 | sym->symbol.section = &bfd_abs_section; | |
2741 | } | |
2742 | else if (i_sym.st_shndx == SHN_COMMON) | |
2743 | { | |
2744 | sym->symbol.section = &bfd_com_section; | |
2745 | /* Elf puts the alignment into the `value' field, and | |
2746 | the size into the `size' field. BFD wants to see the | |
2747 | size in the value field, and doesn't care (at the | |
2748 | moment) about the alignment. */ | |
2749 | sym->symbol.value = i_sym.st_size; | |
2750 | } | |
2751 | else if (i_sym.st_shndx == SHN_UNDEF) | |
2752 | { | |
2753 | sym->symbol.section = &bfd_und_section; | |
2754 | } | |
2755 | else | |
2756 | sym->symbol.section = &bfd_abs_section; | |
300adb31 | 2757 | |
7d8aaf36 | 2758 | sym->symbol.value -= sym->symbol.section->vma; |
244ffee7 | 2759 | |
7d8aaf36 ILT |
2760 | switch (ELF_ST_BIND (i_sym.st_info)) |
2761 | { | |
2762 | case STB_LOCAL: | |
2763 | sym->symbol.flags |= BSF_LOCAL; | |
2764 | break; | |
2765 | case STB_GLOBAL: | |
2766 | sym->symbol.flags |= BSF_GLOBAL; | |
2767 | break; | |
2768 | case STB_WEAK: | |
2769 | sym->symbol.flags |= BSF_WEAK; | |
2770 | break; | |
2771 | } | |
2772 | ||
2773 | switch (ELF_ST_TYPE (i_sym.st_info)) | |
2774 | { | |
2775 | case STT_SECTION: | |
2776 | sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING; | |
2777 | break; | |
2778 | case STT_FILE: | |
2779 | sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING; | |
2780 | break; | |
2781 | case STT_FUNC: | |
2782 | sym->symbol.flags |= BSF_FUNCTION; | |
2783 | break; | |
2784 | } | |
2785 | ||
cb71adf1 PS |
2786 | if (dynamic) |
2787 | sym->symbol.flags |= BSF_DYNAMIC; | |
2788 | ||
7d8aaf36 ILT |
2789 | /* Do some backend-specific processing on this symbol. */ |
2790 | { | |
2791 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
2792 | if (ebd->elf_backend_symbol_processing) | |
2793 | (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol); | |
2794 | } | |
2795 | ||
2796 | sym++; | |
2797 | } | |
244ffee7 JK |
2798 | } |
2799 | ||
e621c5cc ILT |
2800 | /* Do some backend-specific processing on this symbol table. */ |
2801 | { | |
2802 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
2803 | if (ebd->elf_backend_symbol_table_processing) | |
2804 | (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount); | |
2805 | } | |
244ffee7 | 2806 | |
e621c5cc | 2807 | /* We rely on the zalloc to clear out the final symbol entry. */ |
244ffee7 | 2808 | |
cb71adf1 | 2809 | symcount = sym - symbase; |
32090b8e KR |
2810 | |
2811 | /* Fill in the user's symbol pointer vector if needed. */ | |
2812 | if (symptrs) | |
244ffee7 | 2813 | { |
cb71adf1 PS |
2814 | long l = symcount; |
2815 | ||
32090b8e | 2816 | sym = symbase; |
cb71adf1 | 2817 | while (l-- > 0) |
244ffee7 | 2818 | { |
32090b8e KR |
2819 | *symptrs++ = &sym->symbol; |
2820 | sym++; | |
244ffee7 | 2821 | } |
32090b8e | 2822 | *symptrs = 0; /* Final null pointer */ |
244ffee7 JK |
2823 | } |
2824 | ||
80425e6c JK |
2825 | if (x_symp != NULL) |
2826 | free (x_symp); | |
cb71adf1 | 2827 | return symcount; |
1c6042ee | 2828 | error_return: |
80425e6c JK |
2829 | if (x_symp != NULL) |
2830 | free (x_symp); | |
cb71adf1 | 2831 | return -1; |
244ffee7 JK |
2832 | } |
2833 | ||
32090b8e | 2834 | /* Return the number of bytes required to hold the symtab vector. |
244ffee7 | 2835 | |
32090b8e KR |
2836 | Note that we base it on the count plus 1, since we will null terminate |
2837 | the vector allocated based on this size. However, the ELF symbol table | |
2838 | always has a dummy entry as symbol #0, so it ends up even. */ | |
244ffee7 | 2839 | |
326e32d7 | 2840 | long |
1c6042ee ILT |
2841 | elf_get_symtab_upper_bound (abfd) |
2842 | bfd *abfd; | |
244ffee7 | 2843 | { |
326e32d7 ILT |
2844 | long symcount; |
2845 | long symtab_size; | |
1c6042ee | 2846 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; |
326e32d7 | 2847 | |
32090b8e | 2848 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
d6439785 | 2849 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); |
244ffee7 | 2850 | |
32090b8e KR |
2851 | return symtab_size; |
2852 | } | |
244ffee7 | 2853 | |
cb71adf1 PS |
2854 | long |
2855 | elf_get_dynamic_symtab_upper_bound (abfd) | |
2856 | bfd *abfd; | |
2857 | { | |
2858 | long symcount; | |
2859 | long symtab_size; | |
2860 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2861 | ||
2862 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); | |
2863 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
2864 | ||
2865 | return symtab_size; | |
2866 | } | |
2867 | ||
32090b8e KR |
2868 | /* |
2869 | This function return the number of bytes required to store the | |
2870 | relocation information associated with section <<sect>> | |
2871 | attached to bfd <<abfd>> | |
244ffee7 | 2872 | |
32090b8e | 2873 | */ |
326e32d7 | 2874 | long |
32090b8e KR |
2875 | elf_get_reloc_upper_bound (abfd, asect) |
2876 | bfd *abfd; | |
2877 | sec_ptr asect; | |
2878 | { | |
2879 | if (asect->flags & SEC_RELOC) | |
2880 | { | |
2881 | /* either rel or rela */ | |
1c6042ee | 2882 | return elf_section_data (asect)->rel_hdr.sh_size; |
32090b8e KR |
2883 | } |
2884 | else | |
2885 | return 0; | |
244ffee7 JK |
2886 | } |
2887 | ||
32090b8e | 2888 | static boolean |
1c6042ee ILT |
2889 | elf_slurp_reloca_table (abfd, asect, symbols) |
2890 | bfd *abfd; | |
2891 | sec_ptr asect; | |
2892 | asymbol **symbols; | |
244ffee7 | 2893 | { |
32090b8e KR |
2894 | Elf_External_Rela *native_relocs; |
2895 | arelent *reloc_cache; | |
2896 | arelent *cache_ptr; | |
244ffee7 | 2897 | |
32090b8e | 2898 | unsigned int idx; |
244ffee7 | 2899 | |
32090b8e KR |
2900 | if (asect->relocation) |
2901 | return true; | |
2902 | if (asect->reloc_count == 0) | |
2903 | return true; | |
2904 | if (asect->flags & SEC_CONSTRUCTOR) | |
2905 | return true; | |
244ffee7 | 2906 | |
4002f18a ILT |
2907 | if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0) |
2908 | return false; | |
32090b8e KR |
2909 | native_relocs = (Elf_External_Rela *) |
2910 | bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela)); | |
9783e04a | 2911 | if (!native_relocs) |
9783e04a | 2912 | { |
d1ad85a6 | 2913 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2914 | return false; |
2915 | } | |
4002f18a ILT |
2916 | if (bfd_read ((PTR) native_relocs, |
2917 | sizeof (Elf_External_Rela), asect->reloc_count, abfd) | |
2918 | != sizeof (Elf_External_Rela) * asect->reloc_count) | |
2919 | return false; | |
244ffee7 | 2920 | |
32090b8e KR |
2921 | reloc_cache = (arelent *) |
2922 | bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent))); | |
2923 | ||
2924 | if (!reloc_cache) | |
6a3eb9b6 | 2925 | { |
d1ad85a6 | 2926 | bfd_set_error (bfd_error_no_memory); |
32090b8e | 2927 | return false; |
6a3eb9b6 | 2928 | } |
244ffee7 | 2929 | |
32090b8e KR |
2930 | for (idx = 0; idx < asect->reloc_count; idx++) |
2931 | { | |
32090b8e KR |
2932 | Elf_Internal_Rela dst; |
2933 | Elf_External_Rela *src; | |
244ffee7 | 2934 | |
32090b8e KR |
2935 | cache_ptr = reloc_cache + idx; |
2936 | src = native_relocs + idx; | |
2937 | elf_swap_reloca_in (abfd, src, &dst); | |
244ffee7 | 2938 | |
d24928c0 | 2939 | #ifdef RELOC_PROCESSING |
32090b8e KR |
2940 | RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect); |
2941 | #else | |
32090b8e KR |
2942 | if (asect->flags & SEC_RELOC) |
2943 | { | |
2944 | /* relocatable, so the offset is off of the section */ | |
2945 | cache_ptr->address = dst.r_offset + asect->vma; | |
2946 | } | |
2947 | else | |
2948 | { | |
2949 | /* non-relocatable, so the offset a virtual address */ | |
2950 | cache_ptr->address = dst.r_offset; | |
2951 | } | |
7b8106b4 ILT |
2952 | |
2953 | /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset | |
2954 | of zero points to the dummy symbol, which was not read into | |
2955 | the symbol table SYMBOLS. */ | |
2956 | if (ELF_R_SYM (dst.r_info) == 0) | |
2957 | cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; | |
2958 | else | |
2959 | { | |
2960 | asymbol *s; | |
2961 | ||
2962 | cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1; | |
2963 | ||
2964 | /* Translate any ELF section symbol into a BFD section | |
2965 | symbol. */ | |
2966 | s = *(cache_ptr->sym_ptr_ptr); | |
2967 | if (s->flags & BSF_SECTION_SYM) | |
2968 | { | |
2969 | cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr; | |
2970 | s = *cache_ptr->sym_ptr_ptr; | |
2971 | if (s->name == 0 || s->name[0] == 0) | |
2972 | abort (); | |
2973 | } | |
2974 | } | |
32090b8e | 2975 | cache_ptr->addend = dst.r_addend; |
244ffee7 | 2976 | |
32090b8e KR |
2977 | /* Fill in the cache_ptr->howto field from dst.r_type */ |
2978 | { | |
2979 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
2980 | (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst); | |
2981 | } | |
2982 | #endif | |
2983 | } | |
244ffee7 | 2984 | |
32090b8e KR |
2985 | asect->relocation = reloc_cache; |
2986 | return true; | |
2987 | } | |
238ac6ec | 2988 | |
32090b8e KR |
2989 | #ifdef DEBUG |
2990 | static void | |
2991 | elf_debug_section (str, num, hdr) | |
2992 | char *str; | |
2993 | int num; | |
2994 | Elf_Internal_Shdr *hdr; | |
2995 | { | |
2996 | fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr); | |
2997 | fprintf (stderr, | |
2998 | "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n", | |
2999 | (long) hdr->sh_name, | |
3000 | (long) hdr->sh_type, | |
3001 | (long) hdr->sh_flags); | |
3002 | fprintf (stderr, | |
3003 | "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n", | |
3004 | (long) hdr->sh_addr, | |
3005 | (long) hdr->sh_offset, | |
3006 | (long) hdr->sh_size); | |
3007 | fprintf (stderr, | |
3008 | "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n", | |
3009 | (long) hdr->sh_link, | |
3010 | (long) hdr->sh_info, | |
3011 | (long) hdr->sh_addralign); | |
3012 | fprintf (stderr, "sh_entsize = %ld\n", | |
3013 | (long) hdr->sh_entsize); | |
3014 | fprintf (stderr, "rawdata = 0x%.8lx\n", (long) hdr->rawdata); | |
3015 | fprintf (stderr, "contents = 0x%.8lx\n", (long) hdr->contents); | |
3016 | fprintf (stderr, "size = %ld\n", (long) hdr->size); | |
3017 | fflush (stderr); | |
3018 | } | |
244ffee7 | 3019 | |
32090b8e KR |
3020 | static void |
3021 | elf_debug_file (ehdrp) | |
3022 | Elf_Internal_Ehdr *ehdrp; | |
3023 | { | |
3024 | fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry); | |
3025 | fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff); | |
3026 | fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum); | |
3027 | fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize); | |
3028 | fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff); | |
3029 | fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum); | |
3030 | fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize); | |
244ffee7 | 3031 | } |
32090b8e | 3032 | #endif |
244ffee7 JK |
3033 | |
3034 | static boolean | |
1c6042ee ILT |
3035 | elf_slurp_reloc_table (abfd, asect, symbols) |
3036 | bfd *abfd; | |
3037 | sec_ptr asect; | |
3038 | asymbol **symbols; | |
244ffee7 | 3039 | { |
32090b8e KR |
3040 | Elf_External_Rel *native_relocs; |
3041 | arelent *reloc_cache; | |
3042 | arelent *cache_ptr; | |
3043 | Elf_Internal_Shdr *data_hdr; | |
25677b5b PS |
3044 | bfd_vma data_off; |
3045 | unsigned long data_max; | |
32090b8e | 3046 | char buf[4]; /* FIXME -- might be elf64 */ |
244ffee7 | 3047 | |
32090b8e | 3048 | unsigned int idx; |
244ffee7 | 3049 | |
32090b8e KR |
3050 | if (asect->relocation) |
3051 | return true; | |
3052 | if (asect->reloc_count == 0) | |
3053 | return true; | |
3054 | if (asect->flags & SEC_CONSTRUCTOR) | |
3055 | return true; | |
244ffee7 | 3056 | |
4002f18a ILT |
3057 | if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0) |
3058 | return false; | |
32090b8e KR |
3059 | native_relocs = (Elf_External_Rel *) |
3060 | bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel)); | |
9783e04a DM |
3061 | if (!native_relocs) |
3062 | { | |
d1ad85a6 | 3063 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
3064 | return false; |
3065 | } | |
4002f18a ILT |
3066 | if (bfd_read ((PTR) native_relocs, |
3067 | sizeof (Elf_External_Rel), asect->reloc_count, abfd) | |
3068 | != sizeof (Elf_External_Rel) * asect->reloc_count) | |
3069 | return false; | |
244ffee7 | 3070 | |
32090b8e KR |
3071 | reloc_cache = (arelent *) |
3072 | bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent))); | |
3073 | ||
3074 | if (!reloc_cache) | |
244ffee7 | 3075 | { |
d1ad85a6 | 3076 | bfd_set_error (bfd_error_no_memory); |
244ffee7 JK |
3077 | return false; |
3078 | } | |
3079 | ||
32090b8e KR |
3080 | /* Get the offset of the start of the segment we are relocating to read in |
3081 | the implicit addend. */ | |
1c6042ee | 3082 | data_hdr = &elf_section_data (asect)->this_hdr; |
32090b8e KR |
3083 | data_off = data_hdr->sh_offset; |
3084 | data_max = data_hdr->sh_size - sizeof (buf) + 1; | |
244ffee7 | 3085 | |
32090b8e KR |
3086 | #if DEBUG & 2 |
3087 | elf_debug_section ("data section", -1, data_hdr); | |
3088 | #endif | |
244ffee7 | 3089 | |
32090b8e | 3090 | for (idx = 0; idx < asect->reloc_count; idx++) |
244ffee7 | 3091 | { |
32090b8e KR |
3092 | #ifdef RELOC_PROCESSING |
3093 | Elf_Internal_Rel dst; | |
3094 | Elf_External_Rel *src; | |
244ffee7 | 3095 | |
32090b8e KR |
3096 | cache_ptr = reloc_cache + idx; |
3097 | src = native_relocs + idx; | |
3098 | elf_swap_reloc_in (abfd, src, &dst); | |
244ffee7 | 3099 | |
32090b8e KR |
3100 | RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect); |
3101 | #else | |
3102 | Elf_Internal_Rel dst; | |
3103 | Elf_External_Rel *src; | |
6a3eb9b6 | 3104 | |
32090b8e KR |
3105 | cache_ptr = reloc_cache + idx; |
3106 | src = native_relocs + idx; | |
3107 | ||
3108 | elf_swap_reloc_in (abfd, src, &dst); | |
3109 | ||
3110 | if (asect->flags & SEC_RELOC) | |
244ffee7 | 3111 | { |
32090b8e KR |
3112 | /* relocatable, so the offset is off of the section */ |
3113 | cache_ptr->address = dst.r_offset + asect->vma; | |
244ffee7 | 3114 | } |
32090b8e | 3115 | else |
244ffee7 | 3116 | { |
32090b8e KR |
3117 | /* non-relocatable, so the offset a virtual address */ |
3118 | cache_ptr->address = dst.r_offset; | |
244ffee7 | 3119 | } |
7b8106b4 ILT |
3120 | |
3121 | /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset | |
3122 | of zero points to the dummy symbol, which was not read into | |
3123 | the symbol table SYMBOLS. */ | |
3124 | if (ELF_R_SYM (dst.r_info) == 0) | |
3125 | cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr; | |
3126 | else | |
3127 | { | |
3128 | asymbol *s; | |
3129 | ||
3130 | cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1; | |
3131 | ||
3132 | /* Translate any ELF section symbol into a BFD section | |
3133 | symbol. */ | |
3134 | s = *(cache_ptr->sym_ptr_ptr); | |
3135 | if (s->flags & BSF_SECTION_SYM) | |
3136 | { | |
3137 | cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr; | |
3138 | s = *cache_ptr->sym_ptr_ptr; | |
3139 | if (s->name == 0 || s->name[0] == 0) | |
3140 | abort (); | |
3141 | } | |
3142 | } | |
32090b8e | 3143 | BFD_ASSERT (dst.r_offset <= data_max); |
d24928c0 | 3144 | cache_ptr->addend = 0; |
244ffee7 | 3145 | |
32090b8e KR |
3146 | /* Fill in the cache_ptr->howto field from dst.r_type */ |
3147 | { | |
3148 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
3149 | (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst); | |
3150 | } | |
3151 | #endif | |
3152 | } | |
244ffee7 | 3153 | |
32090b8e KR |
3154 | asect->relocation = reloc_cache; |
3155 | return true; | |
3156 | } | |
244ffee7 | 3157 | |
326e32d7 | 3158 | long |
32090b8e KR |
3159 | elf_canonicalize_reloc (abfd, section, relptr, symbols) |
3160 | bfd *abfd; | |
3161 | sec_ptr section; | |
3162 | arelent **relptr; | |
3163 | asymbol **symbols; | |
3164 | { | |
3165 | arelent *tblptr = section->relocation; | |
3166 | unsigned int count = 0; | |
3167 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
3168 | ||
3169 | /* snarfed from coffcode.h */ | |
3170 | if (use_rela_p) | |
326e32d7 ILT |
3171 | { |
3172 | if (! elf_slurp_reloca_table (abfd, section, symbols)) | |
3173 | return -1; | |
3174 | } | |
32090b8e | 3175 | else |
326e32d7 ILT |
3176 | { |
3177 | if (! elf_slurp_reloc_table (abfd, section, symbols)) | |
3178 | return -1; | |
3179 | } | |
32090b8e KR |
3180 | |
3181 | tblptr = section->relocation; | |
32090b8e KR |
3182 | |
3183 | for (; count++ < section->reloc_count;) | |
3184 | *relptr++ = tblptr++; | |
3185 | ||
3186 | *relptr = 0; | |
3187 | return section->reloc_count; | |
3188 | } | |
3189 | ||
326e32d7 | 3190 | long |
1c6042ee ILT |
3191 | elf_get_symtab (abfd, alocation) |
3192 | bfd *abfd; | |
3193 | asymbol **alocation; | |
32090b8e | 3194 | { |
cb71adf1 PS |
3195 | long symcount = elf_slurp_symbol_table (abfd, alocation, false); |
3196 | ||
3197 | if (symcount >= 0) | |
3198 | bfd_get_symcount (abfd) = symcount; | |
3199 | return symcount; | |
3200 | } | |
326e32d7 | 3201 | |
cb71adf1 PS |
3202 | long |
3203 | elf_canonicalize_dynamic_symtab (abfd, alocation) | |
3204 | bfd *abfd; | |
3205 | asymbol **alocation; | |
3206 | { | |
3207 | return elf_slurp_symbol_table (abfd, alocation, true); | |
32090b8e KR |
3208 | } |
3209 | ||
3210 | asymbol * | |
1c6042ee ILT |
3211 | elf_make_empty_symbol (abfd) |
3212 | bfd *abfd; | |
32090b8e KR |
3213 | { |
3214 | elf_symbol_type *newsym; | |
3215 | ||
3216 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
3217 | if (!newsym) | |
3218 | { | |
d1ad85a6 | 3219 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
3220 | return NULL; |
3221 | } | |
3222 | else | |
3223 | { | |
3224 | newsym->symbol.the_bfd = abfd; | |
3225 | return &newsym->symbol; | |
244ffee7 | 3226 | } |
32090b8e | 3227 | } |
244ffee7 | 3228 | |
32090b8e | 3229 | void |
1c6042ee ILT |
3230 | elf_get_symbol_info (ignore_abfd, symbol, ret) |
3231 | bfd *ignore_abfd; | |
3232 | asymbol *symbol; | |
3233 | symbol_info *ret; | |
32090b8e KR |
3234 | { |
3235 | bfd_symbol_info (symbol, ret); | |
3236 | } | |
244ffee7 | 3237 | |
32090b8e | 3238 | void |
1c6042ee ILT |
3239 | elf_print_symbol (ignore_abfd, filep, symbol, how) |
3240 | bfd *ignore_abfd; | |
3241 | PTR filep; | |
3242 | asymbol *symbol; | |
3243 | bfd_print_symbol_type how; | |
32090b8e KR |
3244 | { |
3245 | FILE *file = (FILE *) filep; | |
3246 | switch (how) | |
3247 | { | |
3248 | case bfd_print_symbol_name: | |
3249 | fprintf (file, "%s", symbol->name); | |
3250 | break; | |
3251 | case bfd_print_symbol_more: | |
3252 | fprintf (file, "elf "); | |
3253 | fprintf_vma (file, symbol->value); | |
3254 | fprintf (file, " %lx", (long) symbol->flags); | |
3255 | break; | |
3256 | case bfd_print_symbol_all: | |
3257 | { | |
3258 | CONST char *section_name; | |
3259 | section_name = symbol->section ? symbol->section->name : "(*none*)"; | |
3260 | bfd_print_symbol_vandf ((PTR) file, symbol); | |
3261 | fprintf (file, " %s\t%s", | |
3262 | section_name, | |
3263 | symbol->name); | |
3264 | } | |
3265 | break; | |
3266 | } | |
244ffee7 | 3267 | |
32090b8e | 3268 | } |
244ffee7 | 3269 | |
32090b8e | 3270 | alent * |
1c6042ee ILT |
3271 | elf_get_lineno (ignore_abfd, symbol) |
3272 | bfd *ignore_abfd; | |
3273 | asymbol *symbol; | |
32090b8e KR |
3274 | { |
3275 | fprintf (stderr, "elf_get_lineno unimplemented\n"); | |
3276 | fflush (stderr); | |
3277 | BFD_FAIL (); | |
3278 | return NULL; | |
3279 | } | |
3280 | ||
3281 | boolean | |
1c6042ee ILT |
3282 | elf_set_arch_mach (abfd, arch, machine) |
3283 | bfd *abfd; | |
3284 | enum bfd_architecture arch; | |
3285 | unsigned long machine; | |
32090b8e | 3286 | { |
80a903c9 ILT |
3287 | /* If this isn't the right architecture for this backend, and this |
3288 | isn't the generic backend, fail. */ | |
3289 | if (arch != get_elf_backend_data (abfd)->arch | |
3290 | && arch != bfd_arch_unknown | |
3291 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
3292 | return false; | |
3293 | ||
3294 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
32090b8e | 3295 | } |
244ffee7 | 3296 | |
32090b8e | 3297 | boolean |
1c6042ee ILT |
3298 | elf_find_nearest_line (abfd, |
3299 | section, | |
3300 | symbols, | |
3301 | offset, | |
3302 | filename_ptr, | |
3303 | functionname_ptr, | |
3304 | line_ptr) | |
3305 | bfd *abfd; | |
3306 | asection *section; | |
3307 | asymbol **symbols; | |
3308 | bfd_vma offset; | |
3309 | CONST char **filename_ptr; | |
3310 | CONST char **functionname_ptr; | |
3311 | unsigned int *line_ptr; | |
32090b8e KR |
3312 | { |
3313 | return false; | |
244ffee7 JK |
3314 | } |
3315 | ||
32090b8e | 3316 | int |
1c6042ee ILT |
3317 | elf_sizeof_headers (abfd, reloc) |
3318 | bfd *abfd; | |
3319 | boolean reloc; | |
32090b8e KR |
3320 | { |
3321 | fprintf (stderr, "elf_sizeof_headers unimplemented\n"); | |
3322 | fflush (stderr); | |
3323 | BFD_FAIL (); | |
3324 | return 0; | |
3325 | } | |
244ffee7 | 3326 | |
32090b8e | 3327 | boolean |
1c6042ee ILT |
3328 | elf_set_section_contents (abfd, section, location, offset, count) |
3329 | bfd *abfd; | |
3330 | sec_ptr section; | |
3331 | PTR location; | |
3332 | file_ptr offset; | |
3333 | bfd_size_type count; | |
244ffee7 | 3334 | { |
244ffee7 JK |
3335 | Elf_Internal_Shdr *hdr; |
3336 | ||
6ec3bb6a ILT |
3337 | if (! abfd->output_has_begun |
3338 | && ! elf_compute_section_file_positions (abfd, | |
3339 | (struct bfd_link_info *) NULL)) | |
3340 | return false; | |
244ffee7 | 3341 | |
1c6042ee | 3342 | hdr = &elf_section_data (section)->this_hdr; |
244ffee7 | 3343 | |
32090b8e KR |
3344 | if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) |
3345 | return false; | |
3346 | if (bfd_write (location, 1, count, abfd) != count) | |
3347 | return false; | |
3348 | ||
3349 | return true; | |
3350 | } | |
3351 | ||
3352 | void | |
1c6042ee ILT |
3353 | elf_no_info_to_howto (abfd, cache_ptr, dst) |
3354 | bfd *abfd; | |
3355 | arelent *cache_ptr; | |
3356 | Elf_Internal_Rela *dst; | |
244ffee7 | 3357 | { |
32090b8e KR |
3358 | fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n"); |
3359 | fflush (stderr); | |
3360 | BFD_FAIL (); | |
244ffee7 JK |
3361 | } |
3362 | ||
32090b8e | 3363 | void |
1c6042ee ILT |
3364 | elf_no_info_to_howto_rel (abfd, cache_ptr, dst) |
3365 | bfd *abfd; | |
3366 | arelent *cache_ptr; | |
3367 | Elf_Internal_Rel *dst; | |
244ffee7 | 3368 | { |
32090b8e KR |
3369 | fprintf (stderr, "elf REL relocation support for target machine unimplemented\n"); |
3370 | fflush (stderr); | |
3371 | BFD_FAIL (); | |
3372 | } | |
32090b8e | 3373 | \f |
1c6042ee | 3374 | |
32090b8e | 3375 | /* Core file support */ |
244ffee7 | 3376 | |
32090b8e KR |
3377 | #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */ |
3378 | #include <sys/procfs.h> | |
3379 | #else | |
2e03ce18 ILT |
3380 | #define bfd_prstatus(abfd, descdata, descsz, filepos) true |
3381 | #define bfd_fpregset(abfd, descdata, descsz, filepos) true | |
3382 | #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true | |
32090b8e | 3383 | #endif |
244ffee7 | 3384 | |
32090b8e | 3385 | #ifdef HAVE_PROCFS |
244ffee7 | 3386 | |
2e03ce18 | 3387 | static boolean |
1c6042ee ILT |
3388 | bfd_prstatus (abfd, descdata, descsz, filepos) |
3389 | bfd *abfd; | |
3390 | char *descdata; | |
3391 | int descsz; | |
3392 | long filepos; | |
32090b8e KR |
3393 | { |
3394 | asection *newsect; | |
3395 | prstatus_t *status = (prstatus_t *) 0; | |
244ffee7 | 3396 | |
32090b8e | 3397 | if (descsz == sizeof (prstatus_t)) |
244ffee7 | 3398 | { |
32090b8e | 3399 | newsect = bfd_make_section (abfd, ".reg"); |
2e03ce18 ILT |
3400 | if (newsect == NULL) |
3401 | return false; | |
32090b8e KR |
3402 | newsect->_raw_size = sizeof (status->pr_reg); |
3403 | newsect->filepos = filepos + (long) &status->pr_reg; | |
3404 | newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
3405 | newsect->alignment_power = 2; | |
3406 | if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL) | |
3407 | { | |
3408 | memcpy (core_prstatus (abfd), descdata, descsz); | |
3409 | } | |
244ffee7 | 3410 | } |
2e03ce18 | 3411 | return true; |
32090b8e | 3412 | } |
244ffee7 | 3413 | |
32090b8e | 3414 | /* Stash a copy of the prpsinfo structure away for future use. */ |
244ffee7 | 3415 | |
2e03ce18 | 3416 | static boolean |
1c6042ee ILT |
3417 | bfd_prpsinfo (abfd, descdata, descsz, filepos) |
3418 | bfd *abfd; | |
3419 | char *descdata; | |
3420 | int descsz; | |
3421 | long filepos; | |
32090b8e | 3422 | { |
32090b8e KR |
3423 | if (descsz == sizeof (prpsinfo_t)) |
3424 | { | |
2e03ce18 | 3425 | if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) == NULL) |
244ffee7 | 3426 | { |
2e03ce18 ILT |
3427 | bfd_set_error (bfd_error_no_memory); |
3428 | return false; | |
244ffee7 | 3429 | } |
2e03ce18 | 3430 | memcpy (core_prpsinfo (abfd), descdata, descsz); |
244ffee7 | 3431 | } |
2e03ce18 | 3432 | return true; |
244ffee7 JK |
3433 | } |
3434 | ||
2e03ce18 | 3435 | static boolean |
1c6042ee ILT |
3436 | bfd_fpregset (abfd, descdata, descsz, filepos) |
3437 | bfd *abfd; | |
3438 | char *descdata; | |
3439 | int descsz; | |
3440 | long filepos; | |
244ffee7 | 3441 | { |
32090b8e | 3442 | asection *newsect; |
244ffee7 | 3443 | |
32090b8e | 3444 | newsect = bfd_make_section (abfd, ".reg2"); |
2e03ce18 ILT |
3445 | if (newsect == NULL) |
3446 | return false; | |
32090b8e KR |
3447 | newsect->_raw_size = descsz; |
3448 | newsect->filepos = filepos; | |
3449 | newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
3450 | newsect->alignment_power = 2; | |
2e03ce18 | 3451 | return true; |
6a3eb9b6 | 3452 | } |
244ffee7 | 3453 | |
32090b8e KR |
3454 | #endif /* HAVE_PROCFS */ |
3455 | ||
3456 | /* Return a pointer to the args (including the command name) that were | |
3457 | seen by the program that generated the core dump. Note that for | |
3458 | some reason, a spurious space is tacked onto the end of the args | |
3459 | in some (at least one anyway) implementations, so strip it off if | |
3460 | it exists. */ | |
3461 | ||
3462 | char * | |
1c6042ee ILT |
3463 | elf_core_file_failing_command (abfd) |
3464 | bfd *abfd; | |
244ffee7 | 3465 | { |
32090b8e KR |
3466 | #ifdef HAVE_PROCFS |
3467 | if (core_prpsinfo (abfd)) | |
3468 | { | |
3469 | prpsinfo_t *p = core_prpsinfo (abfd); | |
3470 | char *scan = p->pr_psargs; | |
3471 | while (*scan++) | |
3472 | {; | |
3473 | } | |
3474 | scan -= 2; | |
3475 | if ((scan > p->pr_psargs) && (*scan == ' ')) | |
3476 | { | |
3477 | *scan = '\000'; | |
3478 | } | |
3479 | return p->pr_psargs; | |
3480 | } | |
3481 | #endif | |
3482 | return NULL; | |
3483 | } | |
244ffee7 | 3484 | |
32090b8e KR |
3485 | /* Return the number of the signal that caused the core dump. Presumably, |
3486 | since we have a core file, we got a signal of some kind, so don't bother | |
3487 | checking the other process status fields, just return the signal number. | |
3488 | */ | |
244ffee7 | 3489 | |
32090b8e | 3490 | int |
1c6042ee ILT |
3491 | elf_core_file_failing_signal (abfd) |
3492 | bfd *abfd; | |
32090b8e KR |
3493 | { |
3494 | #ifdef HAVE_PROCFS | |
3495 | if (core_prstatus (abfd)) | |
3496 | { | |
3497 | return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig; | |
3498 | } | |
3499 | #endif | |
3500 | return -1; | |
3501 | } | |
244ffee7 | 3502 | |
32090b8e KR |
3503 | /* Check to see if the core file could reasonably be expected to have |
3504 | come for the current executable file. Note that by default we return | |
3505 | true unless we find something that indicates that there might be a | |
3506 | problem. | |
3507 | */ | |
244ffee7 | 3508 | |
32090b8e | 3509 | boolean |
1c6042ee ILT |
3510 | elf_core_file_matches_executable_p (core_bfd, exec_bfd) |
3511 | bfd *core_bfd; | |
3512 | bfd *exec_bfd; | |
32090b8e KR |
3513 | { |
3514 | #ifdef HAVE_PROCFS | |
3515 | char *corename; | |
3516 | char *execname; | |
3517 | #endif | |
244ffee7 | 3518 | |
32090b8e KR |
3519 | /* First, xvecs must match since both are ELF files for the same target. */ |
3520 | ||
3521 | if (core_bfd->xvec != exec_bfd->xvec) | |
244ffee7 | 3522 | { |
d1ad85a6 | 3523 | bfd_set_error (bfd_error_system_call); |
244ffee7 JK |
3524 | return false; |
3525 | } | |
3526 | ||
32090b8e | 3527 | #ifdef HAVE_PROCFS |
244ffee7 | 3528 | |
32090b8e KR |
3529 | /* If no prpsinfo, just return true. Otherwise, grab the last component |
3530 | of the exec'd pathname from the prpsinfo. */ | |
244ffee7 | 3531 | |
32090b8e | 3532 | if (core_prpsinfo (core_bfd)) |
244ffee7 | 3533 | { |
32090b8e KR |
3534 | corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname); |
3535 | } | |
3536 | else | |
3537 | { | |
3538 | return true; | |
3539 | } | |
244ffee7 | 3540 | |
32090b8e | 3541 | /* Find the last component of the executable pathname. */ |
244ffee7 | 3542 | |
32090b8e KR |
3543 | if ((execname = strrchr (exec_bfd->filename, '/')) != NULL) |
3544 | { | |
3545 | execname++; | |
3546 | } | |
3547 | else | |
3548 | { | |
3549 | execname = (char *) exec_bfd->filename; | |
3550 | } | |
244ffee7 | 3551 | |
32090b8e | 3552 | /* See if they match */ |
244ffee7 | 3553 | |
32090b8e | 3554 | return strcmp (execname, corename) ? false : true; |
244ffee7 | 3555 | |
32090b8e | 3556 | #else |
244ffee7 | 3557 | |
244ffee7 | 3558 | return true; |
244ffee7 | 3559 | |
32090b8e KR |
3560 | #endif /* HAVE_PROCFS */ |
3561 | } | |
244ffee7 | 3562 | |
32090b8e KR |
3563 | /* ELF core files contain a segment of type PT_NOTE, that holds much of |
3564 | the information that would normally be available from the /proc interface | |
3565 | for the process, at the time the process dumped core. Currently this | |
3566 | includes copies of the prstatus, prpsinfo, and fpregset structures. | |
244ffee7 | 3567 | |
32090b8e KR |
3568 | Since these structures are potentially machine dependent in size and |
3569 | ordering, bfd provides two levels of support for them. The first level, | |
3570 | available on all machines since it does not require that the host | |
3571 | have /proc support or the relevant include files, is to create a bfd | |
3572 | section for each of the prstatus, prpsinfo, and fpregset structures, | |
3573 | without any interpretation of their contents. With just this support, | |
3574 | the bfd client will have to interpret the structures itself. Even with | |
3575 | /proc support, it might want these full structures for it's own reasons. | |
244ffee7 | 3576 | |
32090b8e KR |
3577 | In the second level of support, where HAVE_PROCFS is defined, bfd will |
3578 | pick apart the structures to gather some additional information that | |
3579 | clients may want, such as the general register set, the name of the | |
3580 | exec'ed file and its arguments, the signal (if any) that caused the | |
3581 | core dump, etc. | |
244ffee7 | 3582 | |
32090b8e | 3583 | */ |
244ffee7 | 3584 | |
32090b8e | 3585 | static boolean |
1c6042ee ILT |
3586 | elf_corefile_note (abfd, hdr) |
3587 | bfd *abfd; | |
3588 | Elf_Internal_Phdr *hdr; | |
244ffee7 | 3589 | { |
32090b8e KR |
3590 | Elf_External_Note *x_note_p; /* Elf note, external form */ |
3591 | Elf_Internal_Note i_note; /* Elf note, internal form */ | |
3592 | char *buf = NULL; /* Entire note segment contents */ | |
3593 | char *namedata; /* Name portion of the note */ | |
3594 | char *descdata; /* Descriptor portion of the note */ | |
3595 | char *sectname; /* Name to use for new section */ | |
3596 | long filepos; /* File offset to descriptor data */ | |
3597 | asection *newsect; | |
3598 | ||
3599 | if (hdr->p_filesz > 0 | |
b9d5cdf0 | 3600 | && (buf = (char *) malloc (hdr->p_filesz)) != NULL |
32090b8e KR |
3601 | && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1 |
3602 | && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz) | |
3603 | { | |
3604 | x_note_p = (Elf_External_Note *) buf; | |
3605 | while ((char *) x_note_p < (buf + hdr->p_filesz)) | |
3606 | { | |
3607 | i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz); | |
3608 | i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz); | |
3609 | i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type); | |
3610 | namedata = x_note_p->name; | |
3611 | descdata = namedata + BFD_ALIGN (i_note.namesz, 4); | |
3612 | filepos = hdr->p_offset + (descdata - buf); | |
3613 | switch (i_note.type) | |
3614 | { | |
3615 | case NT_PRSTATUS: | |
3616 | /* process descdata as prstatus info */ | |
2e03ce18 ILT |
3617 | if (! bfd_prstatus (abfd, descdata, i_note.descsz, filepos)) |
3618 | return false; | |
32090b8e KR |
3619 | sectname = ".prstatus"; |
3620 | break; | |
3621 | case NT_FPREGSET: | |
3622 | /* process descdata as fpregset info */ | |
2e03ce18 ILT |
3623 | if (! bfd_fpregset (abfd, descdata, i_note.descsz, filepos)) |
3624 | return false; | |
32090b8e KR |
3625 | sectname = ".fpregset"; |
3626 | break; | |
3627 | case NT_PRPSINFO: | |
3628 | /* process descdata as prpsinfo */ | |
2e03ce18 ILT |
3629 | if (! bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos)) |
3630 | return false; | |
32090b8e KR |
3631 | sectname = ".prpsinfo"; |
3632 | break; | |
3633 | default: | |
3634 | /* Unknown descriptor, just ignore it. */ | |
3635 | sectname = NULL; | |
3636 | break; | |
3637 | } | |
3638 | if (sectname != NULL) | |
3639 | { | |
3640 | newsect = bfd_make_section (abfd, sectname); | |
2e03ce18 ILT |
3641 | if (newsect == NULL) |
3642 | return false; | |
32090b8e KR |
3643 | newsect->_raw_size = i_note.descsz; |
3644 | newsect->filepos = filepos; | |
3645 | newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
3646 | newsect->alignment_power = 2; | |
3647 | } | |
3648 | x_note_p = (Elf_External_Note *) | |
3649 | (descdata + BFD_ALIGN (i_note.descsz, 4)); | |
3650 | } | |
3651 | } | |
3652 | if (buf != NULL) | |
3653 | { | |
3654 | free (buf); | |
3655 | } | |
b9d5cdf0 DM |
3656 | else if (hdr->p_filesz > 0) |
3657 | { | |
d1ad85a6 | 3658 | bfd_set_error (bfd_error_no_memory); |
b9d5cdf0 DM |
3659 | return false; |
3660 | } | |
32090b8e | 3661 | return true; |
244ffee7 | 3662 | |
244ffee7 JK |
3663 | } |
3664 | ||
32090b8e KR |
3665 | /* Core files are simply standard ELF formatted files that partition |
3666 | the file using the execution view of the file (program header table) | |
3667 | rather than the linking view. In fact, there is no section header | |
3668 | table in a core file. | |
3669 | ||
3670 | The process status information (including the contents of the general | |
3671 | register set) and the floating point register set are stored in a | |
3672 | segment of type PT_NOTE. We handcraft a couple of extra bfd sections | |
3673 | that allow standard bfd access to the general registers (.reg) and the | |
3674 | floating point registers (.reg2). | |
3675 | ||
3676 | */ | |
3677 | ||
3678 | bfd_target * | |
1c6042ee ILT |
3679 | elf_core_file_p (abfd) |
3680 | bfd *abfd; | |
244ffee7 | 3681 | { |
32090b8e KR |
3682 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
3683 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
3684 | Elf_External_Phdr x_phdr; /* Program header table entry, external form */ | |
3685 | Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */ | |
3686 | unsigned int phindex; | |
d6439785 | 3687 | struct elf_backend_data *ebd; |
244ffee7 | 3688 | |
32090b8e KR |
3689 | /* Read in the ELF header in external format. */ |
3690 | ||
3691 | if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
244ffee7 | 3692 | { |
25057836 JL |
3693 | if (bfd_get_error () != bfd_error_system_call) |
3694 | bfd_set_error (bfd_error_wrong_format); | |
244ffee7 JK |
3695 | return NULL; |
3696 | } | |
32090b8e KR |
3697 | |
3698 | /* Now check to see if we have a valid ELF file, and one that BFD can | |
3699 | make use of. The magic number must match, the address size ('class') | |
3700 | and byte-swapping must match our XVEC entry, and it must have a | |
3701 | program header table (FIXME: See comments re segments at top of this | |
3702 | file). */ | |
3703 | ||
3704 | if (elf_file_p (&x_ehdr) == false) | |
244ffee7 | 3705 | { |
32090b8e | 3706 | wrong: |
d1ad85a6 | 3707 | bfd_set_error (bfd_error_wrong_format); |
32090b8e | 3708 | return NULL; |
244ffee7 | 3709 | } |
244ffee7 | 3710 | |
32090b8e | 3711 | /* FIXME, Check EI_VERSION here ! */ |
244ffee7 | 3712 | |
32090b8e KR |
3713 | { |
3714 | #if ARCH_SIZE == 32 | |
3715 | int desired_address_size = ELFCLASS32; | |
3716 | #endif | |
3717 | #if ARCH_SIZE == 64 | |
3718 | int desired_address_size = ELFCLASS64; | |
3719 | #endif | |
3720 | ||
3721 | if (x_ehdr.e_ident[EI_CLASS] != desired_address_size) | |
3722 | goto wrong; | |
3723 | } | |
3724 | ||
3725 | /* Switch xvec to match the specified byte order. */ | |
3726 | switch (x_ehdr.e_ident[EI_DATA]) | |
244ffee7 | 3727 | { |
32090b8e KR |
3728 | case ELFDATA2MSB: /* Big-endian */ |
3729 | if (abfd->xvec->byteorder_big_p == false) | |
3730 | goto wrong; | |
244ffee7 | 3731 | break; |
32090b8e KR |
3732 | case ELFDATA2LSB: /* Little-endian */ |
3733 | if (abfd->xvec->byteorder_big_p == true) | |
3734 | goto wrong; | |
244ffee7 | 3735 | break; |
32090b8e KR |
3736 | case ELFDATANONE: /* No data encoding specified */ |
3737 | default: /* Unknown data encoding specified */ | |
3738 | goto wrong; | |
244ffee7 JK |
3739 | } |
3740 | ||
32090b8e KR |
3741 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to |
3742 | the tdata pointer in the bfd. */ | |
244ffee7 | 3743 | |
32090b8e KR |
3744 | elf_tdata (abfd) = |
3745 | (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); | |
3746 | if (elf_tdata (abfd) == NULL) | |
244ffee7 | 3747 | { |
d1ad85a6 | 3748 | bfd_set_error (bfd_error_no_memory); |
32090b8e | 3749 | return NULL; |
244ffee7 | 3750 | } |
244ffee7 | 3751 | |
32090b8e | 3752 | /* FIXME, `wrong' returns from this point onward, leak memory. */ |
244ffee7 | 3753 | |
32090b8e KR |
3754 | /* Now that we know the byte order, swap in the rest of the header */ |
3755 | i_ehdrp = elf_elfheader (abfd); | |
3756 | elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); | |
3757 | #if DEBUG & 1 | |
3758 | elf_debug_file (i_ehdrp); | |
3759 | #endif | |
244ffee7 | 3760 | |
d6439785 JL |
3761 | ebd = get_elf_backend_data (abfd); |
3762 | ||
3763 | /* Check that the ELF e_machine field matches what this particular | |
3764 | BFD format expects. */ | |
3765 | if (ebd->elf_machine_code != i_ehdrp->e_machine) | |
3766 | { | |
3767 | bfd_target **target_ptr; | |
3768 | ||
3769 | if (ebd->elf_machine_code != EM_NONE) | |
3770 | goto wrong; | |
3771 | ||
3772 | /* This is the generic ELF target. Let it match any ELF target | |
3773 | for which we do not have a specific backend. */ | |
3774 | for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) | |
3775 | { | |
3776 | struct elf_backend_data *back; | |
3777 | ||
3778 | if ((*target_ptr)->flavour != bfd_target_elf_flavour) | |
3779 | continue; | |
3780 | back = (struct elf_backend_data *) (*target_ptr)->backend_data; | |
3781 | if (back->elf_machine_code == i_ehdrp->e_machine) | |
3782 | { | |
3783 | /* target_ptr is an ELF backend which matches this | |
3784 | object file, so reject the generic ELF target. */ | |
3785 | goto wrong; | |
3786 | } | |
3787 | } | |
3788 | } | |
3789 | ||
32090b8e KR |
3790 | /* If there is no program header, or the type is not a core file, then |
3791 | we are hosed. */ | |
3792 | if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE) | |
3793 | goto wrong; | |
244ffee7 | 3794 | |
32090b8e KR |
3795 | /* Allocate space for a copy of the program header table in |
3796 | internal form, seek to the program header table in the file, | |
3797 | read it in, and convert it to internal form. As a simple sanity | |
3798 | check, verify that the what BFD thinks is the size of each program | |
3799 | header table entry actually matches the size recorded in the file. */ | |
3800 | ||
3801 | if (i_ehdrp->e_phentsize != sizeof (x_phdr)) | |
3802 | goto wrong; | |
3803 | i_phdrp = (Elf_Internal_Phdr *) | |
3804 | bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum); | |
3805 | if (!i_phdrp) | |
244ffee7 | 3806 | { |
d1ad85a6 | 3807 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
3808 | return NULL; |
3809 | } | |
3810 | if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1) | |
25057836 | 3811 | return NULL; |
32090b8e KR |
3812 | for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++) |
3813 | { | |
3814 | if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd) | |
3815 | != sizeof (x_phdr)) | |
25057836 | 3816 | return NULL; |
32090b8e | 3817 | elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex); |
244ffee7 JK |
3818 | } |
3819 | ||
32090b8e KR |
3820 | /* Once all of the program headers have been read and converted, we |
3821 | can start processing them. */ | |
244ffee7 | 3822 | |
32090b8e KR |
3823 | for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++) |
3824 | { | |
3825 | bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex); | |
3826 | if ((i_phdrp + phindex)->p_type == PT_NOTE) | |
3827 | { | |
2e03ce18 ILT |
3828 | if (! elf_corefile_note (abfd, i_phdrp + phindex)) |
3829 | return NULL; | |
32090b8e KR |
3830 | } |
3831 | } | |
244ffee7 | 3832 | |
32090b8e | 3833 | /* Remember the entry point specified in the ELF file header. */ |
244ffee7 | 3834 | |
32090b8e | 3835 | bfd_get_start_address (abfd) = i_ehdrp->e_entry; |
244ffee7 | 3836 | |
32090b8e | 3837 | return abfd->xvec; |
244ffee7 | 3838 | } |
6ec3bb6a ILT |
3839 | \f |
3840 | /* ELF linker code. */ | |
3841 | ||
3842 | static boolean elf_link_add_object_symbols | |
3843 | PARAMS ((bfd *, struct bfd_link_info *)); | |
3844 | static boolean elf_link_add_archive_symbols | |
3845 | PARAMS ((bfd *, struct bfd_link_info *)); | |
3846 | ||
3847 | /* Given an ELF BFD, add symbols to the global hash table as | |
3848 | appropriate. */ | |
3849 | ||
3850 | boolean | |
3851 | elf_bfd_link_add_symbols (abfd, info) | |
3852 | bfd *abfd; | |
3853 | struct bfd_link_info *info; | |
3854 | { | |
3855 | switch (bfd_get_format (abfd)) | |
3856 | { | |
3857 | case bfd_object: | |
3858 | return elf_link_add_object_symbols (abfd, info); | |
3859 | case bfd_archive: | |
3860 | return elf_link_add_archive_symbols (abfd, info); | |
3861 | default: | |
3862 | bfd_set_error (bfd_error_wrong_format); | |
3863 | return false; | |
3864 | } | |
3865 | } | |
3866 | ||
3867 | /* Add symbols from an ELF archive file to the linker hash table. We | |
3868 | don't use _bfd_generic_link_add_archive_symbols because of a | |
3869 | problem which arises on UnixWare. The UnixWare libc.so is an | |
3870 | archive which includes an entry libc.so.1 which defines a bunch of | |
3871 | symbols. The libc.so archive also includes a number of other | |
3872 | object files, which also define symbols, some of which are the same | |
3873 | as those defined in libc.so.1. Correct linking requires that we | |
3874 | consider each object file in turn, and include it if it defines any | |
3875 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
3876 | this; it looks through the list of undefined symbols, and includes | |
3877 | any object file which defines them. When this algorithm is used on | |
3878 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
3879 | bunch of symbols. This means that some of the other objects in the | |
3880 | archive are not included in the link, which is incorrect since they | |
3881 | precede libc.so.1 in the archive. | |
3882 | ||
3883 | Fortunately, ELF archive handling is simpler than that done by | |
3884 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
3885 | oddities. In ELF, if we find a symbol in the archive map, and the | |
3886 | symbol is currently undefined, we know that we must pull in that | |
3887 | object file. | |
3888 | ||
3889 | Unfortunately, we do have to make multiple passes over the symbol | |
3890 | table until nothing further is resolved. */ | |
3891 | ||
3892 | static boolean | |
3893 | elf_link_add_archive_symbols (abfd, info) | |
3894 | bfd *abfd; | |
3895 | struct bfd_link_info *info; | |
3896 | { | |
3897 | symindex c; | |
3898 | boolean *defined = NULL; | |
3899 | boolean *included = NULL; | |
3900 | carsym *symdefs; | |
3901 | boolean loop; | |
3902 | ||
3903 | if (! bfd_has_map (abfd)) | |
3904 | { | |
3905 | bfd_set_error (bfd_error_no_symbols); | |
3906 | return false; | |
3907 | } | |
3908 | ||
3909 | /* Keep track of all symbols we know to be already defined, and all | |
3910 | files we know to be already included. This is to speed up the | |
3911 | second and subsequent passes. */ | |
3912 | c = bfd_ardata (abfd)->symdef_count; | |
3913 | if (c == 0) | |
3914 | return true; | |
3915 | defined = (boolean *) malloc (c * sizeof (boolean)); | |
3916 | included = (boolean *) malloc (c * sizeof (boolean)); | |
3917 | if (defined == (boolean *) NULL || included == (boolean *) NULL) | |
3918 | { | |
3919 | bfd_set_error (bfd_error_no_memory); | |
3920 | goto error_return; | |
3921 | } | |
3922 | memset (defined, 0, c * sizeof (boolean)); | |
3923 | memset (included, 0, c * sizeof (boolean)); | |
3924 | ||
3925 | symdefs = bfd_ardata (abfd)->symdefs; | |
3926 | ||
3927 | do | |
3928 | { | |
3929 | file_ptr last; | |
3930 | symindex i; | |
3931 | carsym *symdef; | |
3932 | carsym *symdefend; | |
3933 | ||
3934 | loop = false; | |
3935 | last = -1; | |
3936 | ||
3937 | symdef = symdefs; | |
3938 | symdefend = symdef + c; | |
3939 | for (i = 0; symdef < symdefend; symdef++, i++) | |
3940 | { | |
3941 | struct elf_link_hash_entry *h; | |
3942 | bfd *element; | |
3943 | struct bfd_link_hash_entry *undefs_tail; | |
3944 | symindex mark; | |
3945 | ||
3946 | if (defined[i] || included[i]) | |
3947 | continue; | |
3948 | if (symdef->file_offset == last) | |
3949 | { | |
3950 | included[i] = true; | |
3951 | continue; | |
3952 | } | |
3953 | ||
3954 | h = elf_link_hash_lookup (elf_hash_table (info), symdef->name, | |
3955 | false, false, false); | |
3956 | if (h == (struct elf_link_hash_entry *) NULL) | |
3957 | continue; | |
3958 | if (h->root.type != bfd_link_hash_undefined) | |
3959 | { | |
3960 | defined[i] = true; | |
3961 | continue; | |
3962 | } | |
3963 | ||
3964 | /* We need to include this archive member. */ | |
3965 | ||
3966 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3967 | if (element == (bfd *) NULL) | |
3968 | goto error_return; | |
3969 | ||
3970 | if (! bfd_check_format (element, bfd_object)) | |
3971 | goto error_return; | |
3972 | ||
3973 | /* Doublecheck that we have not included this object | |
3974 | already--it should be impossible, but there may be | |
3975 | something wrong with the archive. */ | |
3976 | if (element->archive_pass != 0) | |
3977 | { | |
3978 | bfd_set_error (bfd_error_bad_value); | |
3979 | goto error_return; | |
3980 | } | |
3981 | element->archive_pass = 1; | |
3982 | ||
3983 | undefs_tail = info->hash->undefs_tail; | |
3984 | ||
3985 | if (! (*info->callbacks->add_archive_element) (info, element, | |
3986 | symdef->name)) | |
3987 | goto error_return; | |
3988 | if (! elf_link_add_object_symbols (element, info)) | |
3989 | goto error_return; | |
3990 | ||
3991 | /* If there are any new undefined symbols, we need to make | |
3992 | another pass through the archive in order to see whether | |
3993 | they can be defined. FIXME: This isn't perfect, because | |
3994 | common symbols wind up on undefs_tail and because an | |
3995 | undefined symbol which is defined later on in this pass | |
3996 | does not require another pass. This isn't a bug, but it | |
3997 | does make the code less efficient than it could be. */ | |
3998 | if (undefs_tail != info->hash->undefs_tail) | |
3999 | loop = true; | |
4000 | ||
4001 | /* Look backward to mark all symbols from this object file | |
4002 | which we have already seen in this pass. */ | |
4003 | mark = i; | |
4004 | do | |
4005 | { | |
4006 | included[mark] = true; | |
4007 | if (mark == 0) | |
4008 | break; | |
4009 | --mark; | |
4010 | } | |
4011 | while (symdefs[mark].file_offset == symdef->file_offset); | |
4012 | ||
4013 | /* We mark subsequent symbols from this object file as we go | |
4014 | on through the loop. */ | |
4015 | last = symdef->file_offset; | |
4016 | } | |
4017 | } | |
4018 | while (loop); | |
4019 | ||
4020 | free (defined); | |
4021 | free (included); | |
4022 | ||
4023 | return true; | |
4024 | ||
4025 | error_return: | |
4026 | if (defined != (boolean *) NULL) | |
4027 | free (defined); | |
4028 | if (included != (boolean *) NULL) | |
4029 | free (included); | |
4030 | return false; | |
4031 | } | |
4032 | ||
4033 | /* Add symbols from an ELF object file to the linker hash table. */ | |
4034 | ||
4035 | static boolean | |
4036 | elf_link_add_object_symbols (abfd, info) | |
4037 | bfd *abfd; | |
4038 | struct bfd_link_info *info; | |
4039 | { | |
4040 | boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *, | |
4041 | const Elf_Internal_Sym *, | |
4042 | const char **, flagword *, | |
4043 | asection **, bfd_vma *)); | |
4044 | boolean collect; | |
4045 | Elf_Internal_Shdr *hdr; | |
4046 | size_t symcount; | |
4047 | size_t extsymcount; | |
4048 | Elf_External_Sym *buf = NULL; | |
4049 | struct elf_link_hash_entry **sym_hash; | |
4050 | Elf_External_Sym *esym; | |
4051 | Elf_External_Sym *esymend; | |
4052 | ||
4053 | add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook; | |
4054 | collect = get_elf_backend_data (abfd)->collect; | |
4055 | ||
4056 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
4057 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); | |
4058 | ||
4059 | /* The sh_info field of the symtab header tells us where the | |
4060 | external symbols start. We don't care about the local symbols at | |
4061 | this point. */ | |
4062 | extsymcount = symcount - hdr->sh_info; | |
4063 | ||
4064 | buf = (Elf_External_Sym *) malloc (extsymcount * sizeof (Elf_External_Sym)); | |
4065 | if (buf == NULL && extsymcount != 0) | |
4066 | { | |
4067 | bfd_set_error (bfd_error_no_memory); | |
4068 | goto error_return; | |
4069 | } | |
4070 | ||
4071 | /* We keep hash table entry for each external symbol. */ | |
4072 | sym_hash = ((struct elf_link_hash_entry **) | |
4073 | bfd_alloc (abfd, | |
4074 | extsymcount * sizeof (struct elf_link_hash_entry *))); | |
4075 | if (sym_hash == NULL) | |
4076 | { | |
4077 | bfd_set_error (bfd_error_no_memory); | |
4078 | goto error_return; | |
4079 | } | |
4080 | elf_sym_hashes (abfd) = sym_hash; | |
4081 | ||
4082 | if (bfd_seek (abfd, | |
4083 | hdr->sh_offset + hdr->sh_info * sizeof (Elf_External_Sym), | |
4084 | SEEK_SET) != 0 | |
4085 | || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd) | |
4086 | != extsymcount * sizeof (Elf_External_Sym))) | |
4087 | goto error_return; | |
4088 | ||
4089 | esymend = buf + extsymcount; | |
4090 | for (esym = buf; esym < esymend; esym++, sym_hash++) | |
4091 | { | |
4092 | Elf_Internal_Sym sym; | |
4093 | int bind; | |
4094 | bfd_vma value; | |
4095 | asection *sec; | |
4096 | flagword flags; | |
4097 | const char *name; | |
4098 | ||
4099 | elf_swap_symbol_in (abfd, esym, &sym); | |
4100 | ||
4101 | flags = BSF_NO_FLAGS; | |
4102 | sec = NULL; | |
4103 | value = sym.st_value; | |
4104 | *sym_hash = NULL; | |
4105 | ||
4106 | bind = ELF_ST_BIND (sym.st_info); | |
4107 | if (bind == STB_LOCAL) | |
4108 | { | |
4109 | /* This should be impossible, since ELF requires that all | |
4110 | global symbols follow all local symbols, and that sh_info | |
4111 | point to the first global symbol. */ | |
4112 | bfd_set_error (bfd_error_bad_value); | |
4113 | goto error_return; | |
4114 | } | |
4115 | else if (bind == STB_GLOBAL) | |
4116 | flags = BSF_GLOBAL; | |
4117 | else if (bind == STB_WEAK) | |
4118 | flags = BSF_WEAK; | |
4119 | else | |
4120 | { | |
4121 | /* Leave it up to the processor backend. */ | |
4122 | } | |
4123 | ||
4124 | if (sym.st_shndx == SHN_UNDEF) | |
4125 | sec = &bfd_und_section; | |
4126 | else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE) | |
4127 | { | |
4128 | sec = section_from_elf_index (abfd, sym.st_shndx); | |
4129 | if (sec == NULL) | |
4130 | goto error_return; | |
4131 | value -= sec->vma; | |
4132 | } | |
4133 | else if (sym.st_shndx == SHN_ABS) | |
4134 | sec = &bfd_abs_section; | |
4135 | else if (sym.st_shndx == SHN_COMMON) | |
4136 | { | |
4137 | sec = &bfd_com_section; | |
4138 | /* What ELF calls the size we call the value. What ELF | |
4139 | calls the value we call the alignment. */ | |
4140 | value = sym.st_size; | |
4141 | } | |
4142 | else | |
4143 | { | |
4144 | /* Leave it up to the processor backend. */ | |
4145 | } | |
4146 | ||
4147 | name = elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name); | |
4148 | if (name == (const char *) NULL) | |
4149 | goto error_return; | |
4150 | ||
4151 | if (add_symbol_hook) | |
4152 | { | |
4153 | if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec, | |
4154 | &value)) | |
4155 | goto error_return; | |
4156 | ||
4157 | /* The hook function sets the name to NULL if this symbol | |
4158 | should be skipped for some reason. */ | |
4159 | if (name == (const char *) NULL) | |
4160 | continue; | |
4161 | } | |
4162 | ||
4163 | /* Sanity check that all possibilities were handled. */ | |
4164 | if (flags == BSF_NO_FLAGS || sec == (asection *) NULL) | |
4165 | { | |
4166 | bfd_set_error (bfd_error_bad_value); | |
4167 | goto error_return; | |
4168 | } | |
4169 | ||
4170 | if (! (_bfd_generic_link_add_one_symbol | |
4171 | (info, abfd, name, flags, sec, value, (const char *) NULL, | |
4172 | false, collect, (struct bfd_link_hash_entry **) sym_hash))) | |
4173 | goto error_return; | |
4174 | ||
4175 | if (info->hash->creator->flavour == bfd_target_elf_flavour) | |
4176 | { | |
4177 | /* Remember the symbol size, type and alignment. */ | |
4178 | if (sym.st_size != 0) | |
4179 | { | |
4180 | /* FIXME: We should probably somehow give a warning if | |
4181 | the symbol size changes. */ | |
4182 | (*sym_hash)->size = sym.st_size; | |
4183 | } | |
4184 | if (sym.st_shndx == SHN_COMMON | |
4185 | && sym.st_value > (*sym_hash)->align) | |
4186 | (*sym_hash)->align = sym.st_value; | |
4187 | if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE) | |
4188 | { | |
4189 | /* FIXME: We should probably somehow give a warning if | |
4190 | the symbol type changes. */ | |
4191 | (*sym_hash)->type = ELF_ST_TYPE (sym.st_info); | |
4192 | } | |
4193 | } | |
4194 | } | |
4195 | ||
4196 | if (buf != NULL) | |
4197 | free (buf); | |
4198 | ||
4199 | return true; | |
4200 | ||
4201 | error_return: | |
4202 | if (buf != NULL) | |
4203 | free (buf); | |
4204 | return false; | |
4205 | } | |
4206 | \f | |
4207 | /* Final phase of ELF linker. */ | |
4208 | ||
4209 | /* A structure we use to avoid passing large numbers of arguments. */ | |
4210 | ||
4211 | struct elf_final_link_info | |
4212 | { | |
4213 | /* General link information. */ | |
4214 | struct bfd_link_info *info; | |
4215 | /* Output BFD. */ | |
4216 | bfd *output_bfd; | |
4217 | /* Symbol string table. */ | |
4218 | struct strtab *symstrtab; | |
4219 | /* Buffer large enough to hold contents of any section. */ | |
4220 | bfd_byte *contents; | |
4221 | /* Buffer large enough to hold external relocs of any section. */ | |
4222 | PTR external_relocs; | |
4223 | /* Buffer large enough to hold internal relocs of any section. */ | |
4224 | Elf_Internal_Rela *internal_relocs; | |
4225 | /* Buffer large enough to hold external local symbols of any input | |
4226 | BFD. */ | |
4227 | Elf_External_Sym *external_syms; | |
4228 | /* Buffer large enough to hold internal local symbols of any input | |
4229 | BFD. */ | |
4230 | Elf_Internal_Sym *internal_syms; | |
4231 | /* Array large enough to hold a symbol index for each local symbol | |
4232 | of any input BFD. */ | |
4233 | long *indices; | |
4234 | /* Array large enough to hold a section pointer for each local | |
4235 | symbol of any input BFD. */ | |
4236 | asection **sections; | |
4237 | /* Buffer to hold swapped out symbols. */ | |
4238 | Elf_External_Sym *symbuf; | |
4239 | /* Number of swapped out symbols in buffer. */ | |
4240 | size_t symbuf_count; | |
4241 | /* Number of symbols which fit in symbuf. */ | |
4242 | size_t symbuf_size; | |
4243 | }; | |
4244 | ||
4245 | static boolean elf_link_output_sym | |
4246 | PARAMS ((struct elf_final_link_info *, const char *, Elf_Internal_Sym *)); | |
4247 | static boolean elf_link_flush_output_syms | |
4248 | PARAMS ((struct elf_final_link_info *)); | |
4249 | static boolean elf_link_output_extsym | |
4250 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
4251 | static boolean elf_link_input_bfd | |
4252 | PARAMS ((struct elf_final_link_info *, bfd *)); | |
4253 | static boolean elf_reloc_link_order | |
4254 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
4255 | struct bfd_link_order *)); | |
4256 | ||
4257 | /* Do the final step of an ELF link. */ | |
4258 | ||
4259 | boolean | |
4260 | elf_bfd_final_link (abfd, info) | |
4261 | bfd *abfd; | |
4262 | struct bfd_link_info *info; | |
4263 | { | |
4264 | struct elf_final_link_info finfo; | |
4265 | register asection *o; | |
4266 | register struct bfd_link_order *p; | |
4267 | register bfd *sub; | |
4268 | size_t max_contents_size; | |
4269 | size_t max_external_reloc_size; | |
4270 | size_t max_internal_reloc_count; | |
4271 | size_t max_sym_count; | |
4272 | file_ptr off; | |
4273 | Elf_Internal_Sym elfsym; | |
4274 | Elf_Internal_Shdr *symtab_hdr; | |
4275 | Elf_Internal_Shdr *symstrtab_hdr; | |
4276 | ||
4277 | finfo.info = info; | |
4278 | finfo.output_bfd = abfd; | |
4279 | finfo.symstrtab = bfd_new_strtab (abfd); | |
4280 | if (finfo.symstrtab == NULL) | |
4281 | return false; | |
4282 | finfo.contents = NULL; | |
4283 | finfo.external_relocs = NULL; | |
4284 | finfo.internal_relocs = NULL; | |
4285 | finfo.external_syms = NULL; | |
4286 | finfo.internal_syms = NULL; | |
4287 | finfo.indices = NULL; | |
4288 | finfo.sections = NULL; | |
4289 | finfo.symbuf = NULL; | |
4290 | finfo.symbuf_count = 0; | |
4291 | ||
4292 | /* Count up the number of relocations we will output for each output | |
4293 | section, so that we know the sizes of the reloc sections. We | |
4294 | also figure out some maximum sizes. */ | |
4295 | max_contents_size = 0; | |
4296 | max_external_reloc_size = 0; | |
4297 | max_internal_reloc_count = 0; | |
4298 | max_sym_count = 0; | |
4299 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
4300 | { | |
4301 | o->reloc_count = 0; | |
4302 | ||
4303 | for (p = o->link_order_head; p != NULL; p = p->next) | |
4304 | { | |
4305 | if (p->type == bfd_section_reloc_link_order | |
4306 | || p->type == bfd_symbol_reloc_link_order) | |
4307 | ++o->reloc_count; | |
4308 | else if (p->type == bfd_indirect_link_order) | |
4309 | { | |
4310 | asection *sec; | |
4311 | ||
4312 | sec = p->u.indirect.section; | |
4313 | ||
4314 | if (info->relocateable) | |
4315 | o->reloc_count += sec->reloc_count; | |
4316 | ||
4317 | if (sec->_raw_size > max_contents_size) | |
4318 | max_contents_size = sec->_raw_size; | |
4319 | if (sec->_cooked_size > max_contents_size) | |
4320 | max_contents_size = sec->_cooked_size; | |
4321 | ||
4322 | /* We are interested in just local symbols, not all | |
4323 | symbols. */ | |
4324 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour | |
4325 | && (elf_tdata (sec->owner)->symtab_hdr.sh_info | |
4326 | > max_sym_count)) | |
4327 | max_sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
4328 | ||
4329 | if ((sec->flags & SEC_RELOC) != 0) | |
4330 | { | |
4331 | size_t ext_size; | |
4332 | ||
4333 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; | |
4334 | if (ext_size > max_external_reloc_size) | |
4335 | max_external_reloc_size = ext_size; | |
4336 | if (sec->reloc_count > max_internal_reloc_count) | |
4337 | max_internal_reloc_count = sec->reloc_count; | |
4338 | } | |
4339 | } | |
4340 | } | |
4341 | ||
4342 | if (o->reloc_count > 0) | |
4343 | o->flags |= SEC_RELOC; | |
4344 | else | |
4345 | { | |
4346 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
4347 | set it (this is probably a bug) and if it is set | |
4348 | assign_section_numbers will create a reloc section. */ | |
4349 | o->flags &=~ SEC_RELOC; | |
4350 | } | |
4351 | } | |
4352 | ||
4353 | /* Figure out the file positions for everything but the symbol table | |
4354 | and the relocs. We set symcount to force assign_section_numbers | |
4355 | to create a symbol table. */ | |
4356 | abfd->symcount = info->strip == strip_all ? 0 : 1; | |
4357 | BFD_ASSERT (! abfd->output_has_begun); | |
4358 | if (! elf_compute_section_file_positions (abfd, info)) | |
4359 | goto error_return; | |
4360 | ||
4361 | /* That created the reloc sections. Set their sizes, and assign | |
4362 | them file positions, and allocate some buffers. */ | |
4363 | for (o = abfd->sections; o != NULL; o = o->next) | |
4364 | { | |
4365 | if ((o->flags & SEC_RELOC) != 0) | |
4366 | { | |
4367 | Elf_Internal_Shdr *rel_hdr; | |
4368 | register struct elf_link_hash_entry **p, **pend; | |
4369 | ||
4370 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
4371 | ||
4372 | rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count; | |
4373 | ||
4374 | /* The contents field must last into write_object_contents, | |
4375 | so we allocate it with bfd_alloc rather than malloc. */ | |
4376 | rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size); | |
4377 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) | |
4378 | { | |
4379 | bfd_set_error (bfd_error_no_memory); | |
4380 | goto error_return; | |
4381 | } | |
4382 | ||
4383 | p = ((struct elf_link_hash_entry **) | |
4384 | malloc (o->reloc_count | |
4385 | * sizeof (struct elf_link_hash_entry *))); | |
4386 | if (p == NULL && o->reloc_count != 0) | |
4387 | { | |
4388 | bfd_set_error (bfd_error_no_memory); | |
4389 | goto error_return; | |
4390 | } | |
4391 | elf_section_data (o)->rel_hashes = p; | |
4392 | pend = p + o->reloc_count; | |
4393 | for (; p < pend; p++) | |
4394 | *p = NULL; | |
4395 | ||
4396 | /* Use the reloc_count field as an index when outputting the | |
4397 | relocs. */ | |
4398 | o->reloc_count = 0; | |
4399 | } | |
4400 | } | |
4401 | ||
4402 | assign_file_positions_for_relocs (abfd); | |
4403 | ||
4404 | /* We have now assigned file positions for all the sections except | |
4405 | .symtab and .strtab. We start the .symtab section at the current | |
4406 | file position, and write directly to it. We build the .strtab | |
4407 | section in memory. When we add .dynsym support, we will build | |
4408 | that in memory as well (.dynsym is smaller than .symtab). */ | |
4409 | abfd->symcount = 0; | |
4410 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
4411 | /* sh_name is set in prep_headers. */ | |
4412 | symtab_hdr->sh_type = SHT_SYMTAB; | |
4413 | symtab_hdr->sh_flags = 0; | |
4414 | symtab_hdr->sh_addr = 0; | |
4415 | symtab_hdr->sh_size = 0; | |
4416 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
4417 | /* sh_link is set in assign_section_numbers. */ | |
4418 | /* sh_info is set below. */ | |
4419 | /* sh_offset is set just below. */ | |
4420 | symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */ | |
4421 | ||
4422 | off = elf_tdata (abfd)->next_file_pos; | |
4423 | off = align_file_position (off); | |
4424 | off = assign_file_position_for_section (symtab_hdr, off); | |
4425 | ||
4426 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
4427 | incorrect. We do not yet know the size of the .symtab section. | |
4428 | We correct next_file_pos below, after we do know the size. */ | |
4429 | ||
4430 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
4431 | continuously seeking to the right position in the file. */ | |
4432 | if (! info->keep_memory || max_sym_count < 20) | |
4433 | finfo.symbuf_size = 20; | |
4434 | else | |
4435 | finfo.symbuf_size = max_sym_count; | |
4436 | finfo.symbuf = ((Elf_External_Sym *) | |
4437 | malloc (finfo.symbuf_size * sizeof (Elf_External_Sym))); | |
4438 | if (finfo.symbuf == NULL) | |
4439 | { | |
4440 | bfd_set_error (bfd_error_no_memory); | |
4441 | goto error_return; | |
4442 | } | |
4443 | ||
4444 | /* Start writing out the symbol table. The first symbol is always a | |
4445 | dummy symbol. */ | |
4446 | elfsym.st_value = 0; | |
4447 | elfsym.st_size = 0; | |
4448 | elfsym.st_info = 0; | |
4449 | elfsym.st_other = 0; | |
4450 | elfsym.st_shndx = SHN_UNDEF; | |
4451 | if (! elf_link_output_sym (&finfo, (const char *) NULL, &elfsym)) | |
4452 | goto error_return; | |
4453 | ||
4454 | #if 0 | |
4455 | /* Some standard ELF linkers do this, but we don't because it causes | |
4456 | bootstrap comparison failures. */ | |
4457 | /* Output a file symbol for the output file as the second symbol. | |
4458 | We output this even if we are discarding local symbols, although | |
4459 | I'm not sure if this is correct. */ | |
4460 | elfsym.st_value = 0; | |
4461 | elfsym.st_size = 0; | |
4462 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
4463 | elfsym.st_other = 0; | |
4464 | elfsym.st_shndx = SHN_ABS; | |
4465 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), &elfsym)) | |
4466 | goto error_return; | |
4467 | #endif | |
4468 | ||
4469 | /* Output a symbol for each section. We output these even if we are | |
4470 | discarding local symbols, since they are used for relocs. These | |
4471 | symbols have no names. We store the index of each one in the | |
4472 | index field of the section, so that we can find it again when | |
4473 | outputting relocs. */ | |
4474 | elfsym.st_value = 0; | |
4475 | elfsym.st_size = 0; | |
4476 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
4477 | elfsym.st_other = 0; | |
4478 | for (o = abfd->sections; o != NULL; o = o->next) | |
4479 | { | |
4480 | o->index = abfd->symcount; | |
4481 | elfsym.st_shndx = elf_section_from_bfd_section (abfd, o); | |
4482 | if (elfsym.st_shndx == (unsigned long) -1) | |
4483 | goto error_return; | |
4484 | if (! elf_link_output_sym (&finfo, (const char *) NULL, &elfsym)) | |
4485 | goto error_return; | |
4486 | } | |
4487 | ||
4488 | /* Allocate some memory to hold information read in from the input | |
4489 | files. */ | |
4490 | finfo.contents = (bfd_byte *) malloc (max_contents_size); | |
4491 | finfo.external_relocs = (PTR) malloc (max_external_reloc_size); | |
4492 | finfo.internal_relocs = ((Elf_Internal_Rela *) | |
4493 | malloc (max_internal_reloc_count | |
4494 | * sizeof (Elf_Internal_Rela))); | |
4495 | finfo.external_syms = ((Elf_External_Sym *) | |
4496 | malloc (max_sym_count * sizeof (Elf_External_Sym))); | |
4497 | finfo.internal_syms = ((Elf_Internal_Sym *) | |
4498 | malloc (max_sym_count * sizeof (Elf_Internal_Sym))); | |
4499 | finfo.indices = (long *) malloc (max_sym_count * sizeof (long)); | |
4500 | finfo.sections = (asection **) malloc (max_sym_count * sizeof (asection *)); | |
4501 | if ((finfo.contents == NULL && max_contents_size != 0) | |
4502 | || (finfo.external_relocs == NULL && max_external_reloc_size != 0) | |
4503 | || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0) | |
4504 | || (finfo.external_syms == NULL && max_sym_count != 0) | |
4505 | || (finfo.internal_syms == NULL && max_sym_count != 0) | |
4506 | || (finfo.indices == NULL && max_sym_count != 0) | |
4507 | || (finfo.sections == NULL && max_sym_count != 0)) | |
4508 | { | |
4509 | bfd_set_error (bfd_error_no_memory); | |
4510 | goto error_return; | |
4511 | } | |
4512 | ||
4513 | /* Since ELF permits relocations to be against local symbols, we | |
4514 | must have the local symbols available when we do the relocations. | |
4515 | Since we would rather only read the local symbols once, and we | |
4516 | would rather not keep them in memory, we handle all the | |
4517 | relocations for a single input file at the same time. | |
4518 | ||
4519 | Unfortunately, there is no way to know the total number of local | |
4520 | symbols until we have seen all of them, and the local symbol | |
4521 | indices precede the global symbol indices. This means that when | |
4522 | we are generating relocateable output, and we see a reloc against | |
4523 | a global symbol, we can not know the symbol index until we have | |
4524 | finished examining all the local symbols to see which ones we are | |
4525 | going to output. To deal with this, we keep the relocations in | |
4526 | memory, and don't output them until the end of the link. This is | |
4527 | an unfortunate waste of memory, but I don't see a good way around | |
4528 | it. Fortunately, it only happens when performing a relocateable | |
4529 | link, which is not the common case. FIXME: If keep_memory is set | |
4530 | we could write the relocs out and then read them again; I don't | |
4531 | know how bad the memory loss will be. */ | |
4532 | ||
4533 | for (sub = info->input_bfds; sub != NULL; sub = sub->next) | |
4534 | sub->output_has_begun = false; | |
4535 | for (o = abfd->sections; o != NULL; o = o->next) | |
4536 | { | |
4537 | for (p = o->link_order_head; p != NULL; p = p->next) | |
4538 | { | |
4539 | if (p->type == bfd_indirect_link_order | |
4540 | && (bfd_get_flavour (p->u.indirect.section->owner) | |
4541 | == bfd_target_elf_flavour)) | |
4542 | { | |
4543 | sub = p->u.indirect.section->owner; | |
4544 | if (! sub->output_has_begun) | |
4545 | { | |
4546 | if (! elf_link_input_bfd (&finfo, sub)) | |
4547 | goto error_return; | |
4548 | sub->output_has_begun = true; | |
4549 | } | |
4550 | } | |
4551 | else if (p->type == bfd_section_reloc_link_order | |
4552 | || p->type == bfd_symbol_reloc_link_order) | |
4553 | { | |
4554 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
4555 | goto error_return; | |
4556 | } | |
4557 | else | |
4558 | { | |
4559 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
4560 | goto error_return; | |
4561 | } | |
4562 | } | |
4563 | } | |
4564 | ||
4565 | /* That wrote out all the local symbols. Finish up the symbol table | |
4566 | with the global symbols. */ | |
4567 | ||
4568 | /* The sh_info field records the index of the first non local | |
4569 | symbol. */ | |
4570 | symtab_hdr->sh_info = abfd->symcount; | |
4571 | ||
4572 | /* We get the global symbols from the hash table. */ | |
4573 | if (info->strip != strip_all) | |
4574 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, | |
4575 | (PTR) &finfo); | |
4576 | ||
4577 | /* Flush all symbols to the file. */ | |
4578 | if (! elf_link_flush_output_syms (&finfo)) | |
4579 | return false; | |
4580 | ||
4581 | /* Now we know the size of the symtab section. */ | |
4582 | off += symtab_hdr->sh_size; | |
4583 | ||
4584 | /* Finish up the symbol string table (.strtab) section. */ | |
4585 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; | |
4586 | /* sh_name was set in prep_headers. */ | |
4587 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
4588 | symstrtab_hdr->sh_flags = 0; | |
4589 | symstrtab_hdr->sh_addr = 0; | |
4590 | symstrtab_hdr->sh_size = finfo.symstrtab->length; | |
4591 | symstrtab_hdr->sh_entsize = 0; | |
4592 | symstrtab_hdr->sh_link = 0; | |
4593 | symstrtab_hdr->sh_info = 0; | |
4594 | /* sh_offset is set just below. */ | |
4595 | symstrtab_hdr->sh_addralign = 1; | |
4596 | symstrtab_hdr->contents = (PTR) finfo.symstrtab->tab; | |
4597 | ||
4598 | off = assign_file_position_for_section (symstrtab_hdr, off); | |
4599 | elf_tdata (abfd)->next_file_pos = off; | |
4600 | ||
4601 | /* Adjust the relocs to have the correct symbol indices. */ | |
4602 | for (o = abfd->sections; o != NULL; o = o->next) | |
4603 | { | |
4604 | struct elf_link_hash_entry **rel_hash; | |
4605 | Elf_Internal_Shdr *rel_hdr; | |
4606 | size_t i; | |
4607 | ||
4608 | if ((o->flags & SEC_RELOC) == 0) | |
4609 | continue; | |
4610 | ||
4611 | rel_hash = elf_section_data (o)->rel_hashes; | |
4612 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
4613 | for (i = 0; i < o->reloc_count; i++, rel_hash++) | |
4614 | { | |
4615 | if (*rel_hash == NULL) | |
4616 | continue; | |
4617 | ||
4618 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
4619 | ||
4620 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
4621 | { | |
4622 | Elf_External_Rel *erel; | |
4623 | Elf_Internal_Rel irel; | |
4624 | ||
4625 | erel = (Elf_External_Rel *) rel_hdr->contents + i; | |
4626 | elf_swap_reloc_in (abfd, erel, &irel); | |
4627 | irel.r_info = ELF_R_INFO ((*rel_hash)->indx, | |
4628 | ELF_R_TYPE (irel.r_info)); | |
4629 | elf_swap_reloc_out (abfd, &irel, erel); | |
4630 | } | |
4631 | else | |
4632 | { | |
4633 | Elf_External_Rela *erela; | |
4634 | Elf_Internal_Rela irela; | |
4635 | ||
4636 | BFD_ASSERT (rel_hdr->sh_entsize | |
4637 | == sizeof (Elf_External_Rela)); | |
4638 | ||
4639 | erela = (Elf_External_Rela *) rel_hdr->contents + i; | |
4640 | elf_swap_reloca_in (abfd, erela, &irela); | |
4641 | irela.r_info = ELF_R_INFO ((*rel_hash)->indx, | |
4642 | ELF_R_TYPE (irela.r_info)); | |
4643 | elf_swap_reloca_out (abfd, &irela, erela); | |
4644 | } | |
4645 | } | |
4646 | ||
4647 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
4648 | trying to swap the relocs out itself. */ | |
4649 | o->reloc_count = 0; | |
4650 | } | |
4651 | ||
4652 | if (finfo.contents != NULL) | |
4653 | free (finfo.contents); | |
4654 | if (finfo.external_relocs != NULL) | |
4655 | free (finfo.external_relocs); | |
4656 | if (finfo.internal_relocs != NULL) | |
4657 | free (finfo.internal_relocs); | |
4658 | if (finfo.external_syms != NULL) | |
4659 | free (finfo.external_syms); | |
4660 | if (finfo.internal_syms != NULL) | |
4661 | free (finfo.internal_syms); | |
4662 | if (finfo.indices != NULL) | |
4663 | free (finfo.indices); | |
4664 | if (finfo.sections != NULL) | |
4665 | free (finfo.sections); | |
4666 | if (finfo.symbuf != NULL) | |
4667 | free (finfo.symbuf); | |
4668 | for (o = abfd->sections; o != NULL; o = o->next) | |
4669 | { | |
4670 | if ((o->flags & SEC_RELOC) != 0 | |
4671 | && elf_section_data (o)->rel_hashes != NULL) | |
4672 | free (elf_section_data (o)->rel_hashes); | |
4673 | } | |
4674 | ||
4675 | return true; | |
4676 | ||
4677 | error_return: | |
4678 | if (finfo.contents != NULL) | |
4679 | free (finfo.contents); | |
4680 | if (finfo.external_relocs != NULL) | |
4681 | free (finfo.external_relocs); | |
4682 | if (finfo.internal_relocs != NULL) | |
4683 | free (finfo.internal_relocs); | |
4684 | if (finfo.external_syms != NULL) | |
4685 | free (finfo.external_syms); | |
4686 | if (finfo.internal_syms != NULL) | |
4687 | free (finfo.internal_syms); | |
4688 | if (finfo.indices != NULL) | |
4689 | free (finfo.indices); | |
4690 | if (finfo.sections != NULL) | |
4691 | free (finfo.sections); | |
4692 | if (finfo.symbuf != NULL) | |
4693 | free (finfo.symbuf); | |
4694 | for (o = abfd->sections; o != NULL; o = o->next) | |
4695 | { | |
4696 | if ((o->flags & SEC_RELOC) != 0 | |
4697 | && elf_section_data (o)->rel_hashes != NULL) | |
4698 | free (elf_section_data (o)->rel_hashes); | |
4699 | } | |
4700 | ||
4701 | return false; | |
4702 | } | |
4703 | ||
4704 | /* Add a symbol to the output symbol table. */ | |
4705 | ||
4706 | static boolean | |
4707 | elf_link_output_sym (finfo, name, elfsym) | |
4708 | struct elf_final_link_info *finfo; | |
4709 | const char *name; | |
4710 | Elf_Internal_Sym *elfsym; | |
4711 | { | |
4712 | if (name == (const char *) NULL || *name == '\0') | |
4713 | elfsym->st_name = 0; | |
4714 | else | |
4715 | { | |
4716 | elfsym->st_name = bfd_add_to_strtab (finfo->output_bfd, | |
4717 | finfo->symstrtab, name); | |
4718 | if (elfsym->st_name == (unsigned long) -1) | |
4719 | return false; | |
4720 | } | |
4721 | ||
4722 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
4723 | { | |
4724 | if (! elf_link_flush_output_syms (finfo)) | |
4725 | return false; | |
4726 | } | |
4727 | ||
4728 | elf_swap_symbol_out (finfo->output_bfd, elfsym, | |
4729 | finfo->symbuf + finfo->symbuf_count); | |
4730 | ++finfo->symbuf_count; | |
4731 | ||
4732 | ++finfo->output_bfd->symcount; | |
4733 | ||
4734 | return true; | |
4735 | } | |
4736 | ||
4737 | /* Flush the output symbols to the file. */ | |
4738 | ||
4739 | static boolean | |
4740 | elf_link_flush_output_syms (finfo) | |
4741 | struct elf_final_link_info *finfo; | |
4742 | { | |
4743 | Elf_Internal_Shdr *symtab; | |
4744 | ||
4745 | symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
4746 | ||
4747 | if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size, | |
4748 | SEEK_SET) != 0 | |
4749 | || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count, | |
4750 | sizeof (Elf_External_Sym), finfo->output_bfd) | |
4751 | != finfo->symbuf_count * sizeof (Elf_External_Sym))) | |
4752 | return false; | |
4753 | ||
4754 | symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym); | |
4755 | ||
4756 | finfo->symbuf_count = 0; | |
4757 | ||
4758 | return true; | |
4759 | } | |
4760 | ||
4761 | /* Add an external symbol to the symbol table. This is called from | |
4762 | the hash table traversal routine. */ | |
4763 | ||
4764 | static boolean | |
4765 | elf_link_output_extsym (h, data) | |
4766 | struct elf_link_hash_entry *h; | |
4767 | PTR data; | |
4768 | { | |
4769 | struct elf_final_link_info *finfo = (struct elf_final_link_info *) data; | |
4770 | Elf_Internal_Sym sym; | |
4771 | ||
4772 | /* h->indx is set to -2 if this symbol is used by a reloc. */ | |
4773 | if (h->indx == -1 | |
4774 | && finfo->info->strip == strip_some | |
4775 | && bfd_hash_lookup (finfo->info->keep_hash, h->root.root.string, | |
4776 | false, false) == NULL) | |
4777 | return true; | |
4778 | ||
4779 | sym.st_value = 0; | |
4780 | sym.st_size = h->size; | |
4781 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
4782 | sym.st_other = 0; | |
4783 | ||
4784 | switch (h->root.type) | |
4785 | { | |
4786 | default: | |
4787 | case bfd_link_hash_new: | |
4788 | abort (); | |
4789 | return false; | |
4790 | ||
4791 | case bfd_link_hash_undefined: | |
4792 | sym.st_shndx = SHN_UNDEF; | |
4793 | break; | |
4794 | ||
4795 | case bfd_link_hash_weak: | |
4796 | sym.st_shndx = SHN_UNDEF; | |
4797 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); | |
4798 | break; | |
4799 | ||
4800 | case bfd_link_hash_defined: | |
4801 | { | |
4802 | asection *sec; | |
4803 | ||
4804 | sec = h->root.u.def.section; | |
4805 | sym.st_shndx = elf_section_from_bfd_section (finfo->output_bfd, | |
4806 | sec->output_section); | |
4807 | if (sym.st_shndx == (unsigned long) -1) | |
4808 | { | |
4809 | /* FIXME: No way to handle errors. */ | |
4810 | abort (); | |
4811 | } | |
4812 | ||
4813 | /* ELF symbols in relocateable files are section relative, but | |
4814 | in nonrelocateable files they are virtual addresses. */ | |
4815 | sym.st_value = h->root.u.def.value + sec->output_offset; | |
4816 | if (! finfo->info->relocateable) | |
4817 | sym.st_value += sec->output_section->vma; | |
4818 | } | |
4819 | break; | |
4820 | ||
4821 | case bfd_link_hash_common: | |
4822 | sym.st_shndx = SHN_COMMON; | |
4823 | if (h->align == 0) | |
4824 | sym.st_value = 1; | |
4825 | else | |
4826 | sym.st_value = h->align; | |
4827 | break; | |
4828 | ||
4829 | case bfd_link_hash_indirect: | |
4830 | case bfd_link_hash_warning: | |
4831 | /* I have no idea how these should be handled. */ | |
4832 | return true; | |
4833 | } | |
4834 | ||
4835 | h->indx = finfo->output_bfd->symcount; | |
4836 | ||
4837 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym)) | |
4838 | { | |
4839 | /* FIXME: No way to return error. */ | |
4840 | abort (); | |
4841 | } | |
4842 | ||
4843 | return true; | |
4844 | } | |
4845 | ||
4846 | /* Link an input file into the linker output file. This function | |
4847 | handles all the sections and relocations of the input file at once. | |
4848 | This is so that we only have to read the local symbols once, and | |
4849 | don't have to keep them in memory. */ | |
4850 | ||
4851 | static boolean | |
4852 | elf_link_input_bfd (finfo, input_bfd) | |
4853 | struct elf_final_link_info *finfo; | |
4854 | bfd *input_bfd; | |
4855 | { | |
4856 | boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *, | |
4857 | bfd *, asection *, bfd_byte *, | |
4858 | Elf_Internal_Rela *, | |
4859 | Elf_Internal_Sym *, | |
4860 | asection **)); | |
4861 | bfd *output_bfd; | |
4862 | Elf_Internal_Shdr *symtab_hdr; | |
4863 | Elf_External_Sym *esym; | |
4864 | Elf_External_Sym *esymend; | |
4865 | Elf_Internal_Sym *isym; | |
4866 | long *pindex; | |
4867 | asection **ppsection; | |
4868 | asection *o; | |
4869 | ||
4870 | output_bfd = finfo->output_bfd; | |
4871 | relocate_section = | |
4872 | get_elf_backend_data (output_bfd)->elf_backend_relocate_section; | |
4873 | ||
4874 | /* Read the local symbols. */ | |
4875 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
4876 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
4877 | || (bfd_read (finfo->external_syms, sizeof (Elf_External_Sym), | |
4878 | symtab_hdr->sh_info, input_bfd) | |
4879 | != symtab_hdr->sh_info * sizeof (Elf_External_Sym))) | |
4880 | return false; | |
4881 | ||
4882 | /* Swap in the local symbols and write out the ones which we know | |
4883 | are going into the output file. */ | |
4884 | esym = finfo->external_syms; | |
4885 | esymend = esym + symtab_hdr->sh_info; | |
4886 | isym = finfo->internal_syms; | |
4887 | pindex = finfo->indices; | |
4888 | ppsection = finfo->sections; | |
4889 | for (; esym < esymend; esym++, isym++, pindex++, ppsection++) | |
4890 | { | |
4891 | asection *isec; | |
4892 | const char *name; | |
4893 | bfd_vma oldval; | |
4894 | ||
4895 | elf_swap_symbol_in (input_bfd, esym, isym); | |
4896 | *pindex = -1; | |
4897 | ||
4898 | if (isym->st_shndx == SHN_UNDEF) | |
4899 | isec = &bfd_und_section; | |
4900 | else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE) | |
4901 | { | |
4902 | isec = section_from_elf_index (input_bfd, isym->st_shndx); | |
4903 | if (isec == NULL) | |
4904 | return false; | |
4905 | } | |
4906 | else if (isym->st_shndx == SHN_ABS) | |
4907 | isec = &bfd_abs_section; | |
4908 | else if (isym->st_shndx == SHN_COMMON) | |
4909 | isec = &bfd_com_section; | |
4910 | else | |
4911 | { | |
4912 | /* Who knows? */ | |
4913 | isec = NULL; | |
4914 | } | |
4915 | ||
4916 | *ppsection = isec; | |
4917 | ||
4918 | /* Don't output the first, undefined, symbol. */ | |
4919 | if (esym == finfo->external_syms) | |
4920 | continue; | |
4921 | ||
4922 | /* If we are stripping all symbols, we don't want to output this | |
4923 | one. */ | |
4924 | if (finfo->info->strip == strip_all) | |
4925 | continue; | |
4926 | ||
4927 | /* We never output section symbols. Instead, we use the section | |
4928 | symbol of the corresponding section in the output file. */ | |
4929 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
4930 | continue; | |
4931 | ||
4932 | /* If we are discarding all local symbols, we don't want to | |
4933 | output this one. If we are generating a relocateable output | |
4934 | file, then some of the local symbols may be required by | |
4935 | relocs; we output them below as we discover that they are | |
4936 | needed. */ | |
4937 | if (finfo->info->discard == discard_all) | |
4938 | continue; | |
4939 | ||
4940 | /* Get the name of the symbol. */ | |
4941 | name = elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
4942 | isym->st_name); | |
4943 | if (name == NULL) | |
4944 | return false; | |
4945 | ||
4946 | /* See if we are discarding symbols with this name. */ | |
4947 | if ((finfo->info->strip == strip_some | |
4948 | && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false) | |
4949 | == NULL)) | |
4950 | || (finfo->info->discard == discard_l | |
4951 | && strncmp (name, finfo->info->lprefix, | |
4952 | finfo->info->lprefix_len) == 0)) | |
4953 | continue; | |
4954 | ||
4955 | /* If we get here, we are going to output this symbol. */ | |
4956 | ||
4957 | /* Adjust the section index for the output file. */ | |
4958 | isym->st_shndx = elf_section_from_bfd_section (output_bfd, | |
4959 | isec->output_section); | |
4960 | if (isym->st_shndx == (unsigned long) -1) | |
4961 | return false; | |
4962 | ||
4963 | *pindex = output_bfd->symcount; | |
4964 | ||
4965 | /* ELF symbols in relocateable files are section relative, but | |
4966 | in executable files they are virtual addresses. Note that | |
4967 | this code assumes that all ELF sections have an associated | |
4968 | BFD section with a reasonable value for output_offset; below | |
4969 | we assume that they also have a reasonable value for | |
4970 | output_section. Any special sections must be set up to meet | |
4971 | these requirements. */ | |
4972 | oldval = isym->st_value; | |
4973 | isym->st_value += isec->output_offset; | |
4974 | if (! finfo->info->relocateable) | |
4975 | isym->st_value += isec->output_section->vma; | |
4976 | ||
4977 | if (! elf_link_output_sym (finfo, name, isym)) | |
4978 | return false; | |
4979 | ||
4980 | /* Restore the old value for reloc handling. */ | |
4981 | isym->st_value = oldval; | |
4982 | } | |
4983 | ||
4984 | /* Relocate the contents of each section. */ | |
4985 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
4986 | { | |
4987 | Elf_Internal_Shdr *input_rel_hdr; | |
4988 | ||
4989 | if ((o->flags & SEC_HAS_CONTENTS) == 0) | |
4990 | continue; | |
4991 | ||
4992 | /* Read the contents of the section. */ | |
4993 | if (! bfd_get_section_contents (input_bfd, o, finfo->contents, | |
4994 | (file_ptr) 0, o->_raw_size)) | |
4995 | return false; | |
4996 | ||
4997 | if ((o->flags & SEC_RELOC) != 0) | |
4998 | { | |
4999 | /* Read in the relocs. */ | |
5000 | input_rel_hdr = &elf_section_data (o)->rel_hdr; | |
5001 | if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0 | |
5002 | || bfd_read (finfo->external_relocs, 1, input_rel_hdr->sh_size, | |
5003 | input_bfd) != input_rel_hdr->sh_size) | |
5004 | return false; | |
5005 | ||
5006 | /* Swap in the relocs. For convenience, we always produce | |
5007 | an Elf_Internal_Rela array; if the relocs are Rel, we set | |
5008 | the addend to 0. */ | |
5009 | if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
5010 | { | |
5011 | Elf_External_Rel *erel; | |
5012 | Elf_External_Rel *erelend; | |
5013 | Elf_Internal_Rela *irela; | |
5014 | ||
5015 | erel = (Elf_External_Rel *) finfo->external_relocs; | |
5016 | erelend = erel + o->reloc_count; | |
5017 | irela = finfo->internal_relocs; | |
5018 | for (; erel < erelend; erel++, irela++) | |
5019 | { | |
5020 | Elf_Internal_Rel irel; | |
5021 | ||
5022 | elf_swap_reloc_in (input_bfd, erel, &irel); | |
5023 | irela->r_offset = irel.r_offset; | |
5024 | irela->r_info = irel.r_info; | |
5025 | irela->r_addend = 0; | |
5026 | } | |
5027 | } | |
5028 | else | |
5029 | { | |
5030 | Elf_External_Rela *erela; | |
5031 | Elf_External_Rela *erelaend; | |
5032 | Elf_Internal_Rela *irela; | |
5033 | ||
5034 | BFD_ASSERT (input_rel_hdr->sh_entsize | |
5035 | == sizeof (Elf_External_Rela)); | |
5036 | ||
5037 | erela = (Elf_External_Rela *) finfo->external_relocs; | |
5038 | erelaend = erela + o->reloc_count; | |
5039 | irela = finfo->internal_relocs; | |
5040 | for (; erela < erelaend; erela++, irela++) | |
5041 | elf_swap_reloca_in (input_bfd, erela, irela); | |
5042 | } | |
5043 | ||
5044 | /* Relocate the section by invoking a back end routine. | |
5045 | ||
5046 | The back end routine is responsible for adjusting the | |
5047 | section contents as necessary, and (if using Rela relocs | |
5048 | and generating a relocateable output file) adjusting the | |
5049 | reloc addend as necessary. | |
5050 | ||
5051 | The back end routine does not have to worry about setting | |
5052 | the reloc address or the reloc symbol index. | |
5053 | ||
5054 | The back end routine is given a pointer to the swapped in | |
5055 | internal symbols, and can access the hash table entries | |
5056 | for the external symbols via elf_sym_hashes (input_bfd). | |
5057 | ||
5058 | When generating relocateable output, the back end routine | |
5059 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
5060 | output symbol is going to be a section symbol | |
5061 | corresponding to the output section, which will require | |
5062 | the addend to be adjusted. */ | |
5063 | ||
5064 | if (! (*relocate_section) (output_bfd, finfo->info, | |
5065 | input_bfd, o, | |
5066 | finfo->contents, | |
5067 | finfo->internal_relocs, | |
5068 | finfo->internal_syms, | |
5069 | finfo->sections)) | |
5070 | return false; | |
5071 | ||
5072 | if (finfo->info->relocateable) | |
5073 | { | |
5074 | Elf_Internal_Rela *irela; | |
5075 | Elf_Internal_Rela *irelaend; | |
5076 | struct elf_link_hash_entry **rel_hash; | |
5077 | Elf_Internal_Shdr *output_rel_hdr; | |
5078 | ||
5079 | /* Adjust the reloc addresses and symbol indices. */ | |
5080 | ||
5081 | irela = finfo->internal_relocs; | |
5082 | irelaend = irela + o->reloc_count; | |
5083 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
5084 | + o->output_section->reloc_count); | |
5085 | for (; irela < irelaend; irela++, rel_hash++) | |
5086 | { | |
5087 | long r_symndx; | |
5088 | Elf_Internal_Sym *isym; | |
5089 | asection *sec; | |
5090 | ||
5091 | irela->r_offset += o->output_offset; | |
5092 | ||
5093 | r_symndx = ELF_R_SYM (irela->r_info); | |
5094 | ||
5095 | if (r_symndx == 0) | |
5096 | continue; | |
5097 | ||
5098 | if (r_symndx >= symtab_hdr->sh_info) | |
5099 | { | |
5100 | long indx; | |
5101 | ||
5102 | /* This is a reloc against a global symbol. We | |
5103 | have not yet output all the local symbols, so | |
5104 | we do not know the symbol index of any global | |
5105 | symbol. We set the rel_hash entry for this | |
5106 | reloc to point to the global hash table entry | |
5107 | for this symbol. The symbol index is then | |
5108 | set at the end of elf_bfd_final_link. */ | |
5109 | indx = r_symndx - symtab_hdr->sh_info; | |
5110 | *rel_hash = elf_sym_hashes (input_bfd)[indx]; | |
5111 | ||
5112 | /* Setting the index to -2 tells | |
5113 | elf_link_output_extsym that this symbol is | |
5114 | used by a reloc. */ | |
5115 | BFD_ASSERT ((*rel_hash)->indx < 0); | |
5116 | (*rel_hash)->indx = -2; | |
5117 | ||
5118 | continue; | |
5119 | } | |
5120 | ||
5121 | /* This is a reloc against a local symbol. */ | |
5122 | ||
5123 | *rel_hash = NULL; | |
5124 | isym = finfo->internal_syms + r_symndx; | |
5125 | sec = finfo->sections[r_symndx]; | |
5126 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
5127 | { | |
5128 | /* I suppose the backend ought to fill in the | |
5129 | section of any STT_SECTION symbol against a | |
5130 | processor specific section. */ | |
5131 | if (sec == NULL | |
5132 | || sec == &bfd_und_section | |
5133 | || sec == &bfd_com_section) | |
5134 | { | |
5135 | bfd_set_error (bfd_error_bad_value); | |
5136 | return false; | |
5137 | } | |
5138 | else if (sec == &bfd_abs_section) | |
5139 | r_symndx = 0; | |
5140 | else | |
5141 | r_symndx = sec->output_section->index; | |
5142 | } | |
5143 | else | |
5144 | { | |
5145 | if (finfo->indices[r_symndx] == -1) | |
5146 | { | |
5147 | unsigned long link; | |
5148 | const char *name; | |
5149 | asection *osec; | |
5150 | ||
5151 | if (finfo->info->strip == strip_all) | |
5152 | { | |
5153 | /* You can't do ld -r -s. */ | |
5154 | bfd_set_error (bfd_error_invalid_operation); | |
5155 | return false; | |
5156 | } | |
5157 | ||
5158 | /* This symbol was skipped earlier, but | |
5159 | since it is needed by a reloc, we | |
5160 | must output it now. */ | |
5161 | link = symtab_hdr->sh_link; | |
5162 | name = elf_string_from_elf_section (input_bfd, | |
5163 | link, | |
5164 | isym->st_name); | |
5165 | if (name == NULL) | |
5166 | return false; | |
5167 | ||
5168 | osec = sec->output_section; | |
5169 | isym->st_shndx = | |
5170 | elf_section_from_bfd_section (output_bfd, | |
5171 | osec); | |
5172 | if (isym->st_shndx == (unsigned long) -1) | |
5173 | return false; | |
5174 | ||
5175 | isym->st_value += sec->output_offset; | |
5176 | if (! finfo->info->relocateable) | |
5177 | isym->st_value += osec->vma; | |
5178 | ||
5179 | finfo->indices[r_symndx] = output_bfd->symcount; | |
5180 | ||
5181 | if (! elf_link_output_sym (finfo, name, isym)) | |
5182 | return false; | |
5183 | } | |
5184 | ||
5185 | r_symndx = finfo->indices[r_symndx]; | |
5186 | } | |
5187 | ||
5188 | irela->r_info = ELF_R_INFO (r_symndx, | |
5189 | ELF_R_TYPE (irela->r_info)); | |
5190 | } | |
5191 | ||
5192 | /* Swap out the relocs. */ | |
5193 | output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr; | |
5194 | BFD_ASSERT (output_rel_hdr->sh_entsize | |
5195 | == input_rel_hdr->sh_entsize); | |
5196 | irela = finfo->internal_relocs; | |
5197 | irelaend = irela + o->reloc_count; | |
5198 | if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
5199 | { | |
5200 | Elf_External_Rel *erel; | |
5201 | ||
5202 | erel = ((Elf_External_Rel *) output_rel_hdr->contents | |
5203 | + o->output_section->reloc_count); | |
5204 | for (; irela < irelaend; irela++, erel++) | |
5205 | { | |
5206 | Elf_Internal_Rel irel; | |
5207 | ||
5208 | irel.r_offset = irela->r_offset; | |
5209 | irel.r_info = irela->r_info; | |
5210 | BFD_ASSERT (irela->r_addend == 0); | |
5211 | elf_swap_reloc_out (output_bfd, &irel, erel); | |
5212 | } | |
5213 | } | |
5214 | else | |
5215 | { | |
5216 | Elf_External_Rela *erela; | |
5217 | ||
5218 | BFD_ASSERT (input_rel_hdr->sh_entsize | |
5219 | == sizeof (Elf_External_Rela)); | |
5220 | erela = ((Elf_External_Rela *) output_rel_hdr->contents | |
5221 | + o->output_section->reloc_count); | |
5222 | for (; irela < irelaend; irela++, erela++) | |
5223 | elf_swap_reloca_out (output_bfd, irela, erela); | |
5224 | } | |
5225 | ||
5226 | o->output_section->reloc_count += o->reloc_count; | |
5227 | } | |
5228 | } | |
5229 | ||
5230 | /* Write out the modified section contents. */ | |
5231 | if (! bfd_set_section_contents (output_bfd, o->output_section, | |
5232 | finfo->contents, o->output_offset, | |
5233 | (o->_cooked_size != 0 | |
5234 | ? o->_cooked_size | |
5235 | : o->_raw_size))) | |
5236 | return false; | |
5237 | } | |
5238 | ||
5239 | return true; | |
5240 | } | |
5241 | ||
5242 | /* Generate a reloc when linking an ELF file. This is a reloc | |
5243 | requested by the linker, and does come from any input file. This | |
5244 | is used to build constructor and destructor tables when linking | |
5245 | with -Ur. */ | |
5246 | ||
5247 | static boolean | |
5248 | elf_reloc_link_order (output_bfd, info, output_section, link_order) | |
5249 | bfd *output_bfd; | |
5250 | struct bfd_link_info *info; | |
5251 | asection *output_section; | |
5252 | struct bfd_link_order *link_order; | |
5253 | { | |
5254 | const reloc_howto_type *howto; | |
5255 | long indx; | |
5256 | bfd_vma offset; | |
5257 | struct elf_link_hash_entry **rel_hash_ptr; | |
5258 | Elf_Internal_Shdr *rel_hdr; | |
5259 | ||
5260 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
5261 | if (howto == NULL) | |
5262 | { | |
5263 | bfd_set_error (bfd_error_bad_value); | |
5264 | return false; | |
5265 | } | |
5266 | ||
5267 | /* If this is an inplace reloc, we must write the addend into the | |
5268 | object file. */ | |
5269 | if (howto->partial_inplace | |
5270 | && link_order->u.reloc.p->addend != 0) | |
5271 | { | |
5272 | bfd_size_type size; | |
5273 | bfd_reloc_status_type rstat; | |
5274 | bfd_byte *buf; | |
5275 | boolean ok; | |
5276 | ||
5277 | size = bfd_get_reloc_size (howto); | |
5278 | buf = (bfd_byte *) bfd_zmalloc (size); | |
5279 | if (buf == (bfd_byte *) NULL) | |
5280 | { | |
5281 | bfd_set_error (bfd_error_no_memory); | |
5282 | return false; | |
5283 | } | |
5284 | rstat = _bfd_relocate_contents (howto, output_bfd, | |
5285 | link_order->u.reloc.p->addend, buf); | |
5286 | switch (rstat) | |
5287 | { | |
5288 | case bfd_reloc_ok: | |
5289 | break; | |
5290 | default: | |
5291 | case bfd_reloc_outofrange: | |
5292 | abort (); | |
5293 | case bfd_reloc_overflow: | |
5294 | if (! ((*info->callbacks->reloc_overflow) | |
5295 | (info, | |
5296 | (link_order->type == bfd_section_reloc_link_order | |
5297 | ? bfd_section_name (output_bfd, | |
5298 | link_order->u.reloc.p->u.section) | |
5299 | : link_order->u.reloc.p->u.name), | |
5300 | howto->name, link_order->u.reloc.p->addend, | |
5301 | (bfd *) NULL, (asection *) NULL, (bfd_vma) 0))) | |
5302 | { | |
5303 | free (buf); | |
5304 | return false; | |
5305 | } | |
5306 | break; | |
5307 | } | |
5308 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, | |
5309 | (file_ptr) link_order->offset, size); | |
5310 | free (buf); | |
5311 | if (! ok) | |
5312 | return false; | |
5313 | } | |
5314 | ||
5315 | /* Figure out the symbol index. */ | |
5316 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
5317 | + output_section->reloc_count); | |
5318 | if (link_order->type == bfd_section_reloc_link_order) | |
5319 | { | |
5320 | indx = link_order->u.reloc.p->u.section->index; | |
5321 | *rel_hash_ptr = NULL; | |
5322 | } | |
5323 | else | |
5324 | { | |
5325 | struct elf_link_hash_entry *h; | |
5326 | ||
5327 | h = elf_link_hash_lookup (elf_hash_table (info), | |
5328 | link_order->u.reloc.p->u.name, | |
5329 | false, false, true); | |
5330 | if (h != NULL) | |
5331 | { | |
5332 | /* Setting the index to -2 tells elf_link_output_extsym that | |
5333 | this symbol is used by a reloc. */ | |
5334 | h->indx = -2; | |
5335 | *rel_hash_ptr = h; | |
5336 | indx = 0; | |
5337 | } | |
5338 | else | |
5339 | { | |
5340 | if (! ((*info->callbacks->unattached_reloc) | |
5341 | (info, link_order->u.reloc.p->u.name, (bfd *) NULL, | |
5342 | (asection *) NULL, (bfd_vma) 0))) | |
5343 | return false; | |
5344 | indx = 0; | |
5345 | } | |
5346 | } | |
5347 | ||
5348 | /* The address of a reloc is relative to the section in a | |
5349 | relocateable file, and is a virtual address in an executable | |
5350 | file. */ | |
5351 | offset = link_order->offset; | |
5352 | if (! info->relocateable) | |
5353 | offset += output_section->vma; | |
5354 | ||
5355 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
5356 | ||
5357 | if (rel_hdr->sh_type == SHT_REL) | |
5358 | { | |
5359 | Elf_Internal_Rel irel; | |
5360 | Elf_External_Rel *erel; | |
5361 | ||
5362 | irel.r_offset = offset; | |
5363 | irel.r_info = ELF_R_INFO (indx, howto->type); | |
5364 | erel = ((Elf_External_Rel *) rel_hdr->contents | |
5365 | + output_section->reloc_count); | |
5366 | elf_swap_reloc_out (output_bfd, &irel, erel); | |
5367 | } | |
5368 | else | |
5369 | { | |
5370 | Elf_Internal_Rela irela; | |
5371 | Elf_External_Rela *erela; | |
5372 | ||
5373 | irela.r_offset = offset; | |
5374 | irela.r_info = ELF_R_INFO (indx, howto->type); | |
5375 | irela.r_addend = link_order->u.reloc.p->addend; | |
5376 | erela = ((Elf_External_Rela *) rel_hdr->contents | |
5377 | + output_section->reloc_count); | |
5378 | elf_swap_reloca_out (output_bfd, &irela, erela); | |
5379 | } | |
5380 | ||
5381 | ++output_section->reloc_count; | |
5382 | ||
5383 | return true; | |
5384 | } |