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. | |
5546cc7e KR |
60 | |
61 | (4) We need a published spec for 64-bit ELF. We've got some stuff here | |
62 | that we're using for SPARC V9 64-bit chips, but don't assume that | |
63 | it's cast in stone. | |
d24928c0 | 64 | */ |
244ffee7 JK |
65 | |
66 | #include <string.h> /* For strrchr and friends */ | |
67 | #include "bfd.h" | |
68 | #include "sysdep.h" | |
6ec3bb6a | 69 | #include "bfdlink.h" |
244ffee7 JK |
70 | #include "libbfd.h" |
71 | #include "libelf.h" | |
72 | ||
32090b8e | 73 | /* Renaming structures, typedefs, macros and functions to be size-specific. */ |
244ffee7 | 74 | #define Elf_External_Ehdr NAME(Elf,External_Ehdr) |
244ffee7 | 75 | #define Elf_External_Sym NAME(Elf,External_Sym) |
244ffee7 | 76 | #define Elf_External_Shdr NAME(Elf,External_Shdr) |
244ffee7 | 77 | #define Elf_External_Phdr NAME(Elf,External_Phdr) |
244ffee7 JK |
78 | #define Elf_External_Rel NAME(Elf,External_Rel) |
79 | #define Elf_External_Rela NAME(Elf,External_Rela) | |
013dec1a | 80 | #define Elf_External_Dyn NAME(Elf,External_Dyn) |
244ffee7 | 81 | |
244ffee7 JK |
82 | #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command) |
83 | #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal) | |
cb71adf1 PS |
84 | #define elf_core_file_matches_executable_p \ |
85 | NAME(bfd_elf,core_file_matches_executable_p) | |
244ffee7 JK |
86 | #define elf_object_p NAME(bfd_elf,object_p) |
87 | #define elf_core_file_p NAME(bfd_elf,core_file_p) | |
244ffee7 | 88 | #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound) |
cb71adf1 PS |
89 | #define elf_get_dynamic_symtab_upper_bound \ |
90 | NAME(bfd_elf,get_dynamic_symtab_upper_bound) | |
013dec1a ILT |
91 | #define elf_swap_reloc_in NAME(bfd_elf,swap_reloc_in) |
92 | #define elf_swap_reloca_in NAME(bfd_elf,swap_reloca_in) | |
93 | #define elf_swap_reloc_out NAME(bfd_elf,swap_reloc_out) | |
94 | #define elf_swap_reloca_out NAME(bfd_elf,swap_reloca_out) | |
71edd06d ILT |
95 | #define elf_swap_symbol_in NAME(bfd_elf,swap_symbol_in) |
96 | #define elf_swap_symbol_out NAME(bfd_elf,swap_symbol_out) | |
013dec1a ILT |
97 | #define elf_swap_dyn_in NAME(bfd_elf,swap_dyn_in) |
98 | #define elf_swap_dyn_out NAME(bfd_elf,swap_dyn_out) | |
244ffee7 JK |
99 | #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound) |
100 | #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc) | |
101 | #define elf_get_symtab NAME(bfd_elf,get_symtab) | |
cb71adf1 PS |
102 | #define elf_canonicalize_dynamic_symtab \ |
103 | NAME(bfd_elf,canonicalize_dynamic_symtab) | |
244ffee7 JK |
104 | #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol) |
105 | #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info) | |
244ffee7 JK |
106 | #define elf_get_lineno NAME(bfd_elf,get_lineno) |
107 | #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach) | |
108 | #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line) | |
109 | #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers) | |
110 | #define elf_set_section_contents NAME(bfd_elf,set_section_contents) | |
111 | #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto) | |
112 | #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel) | |
fce36137 | 113 | #define elf_new_section_hook NAME(bfd_elf,new_section_hook) |
32090b8e | 114 | #define write_relocs NAME(bfd_elf,_write_relocs) |
f035cc47 | 115 | #define elf_find_section NAME(bfd_elf,find_section) |
6ec3bb6a | 116 | #define elf_bfd_link_add_symbols NAME(bfd_elf,bfd_link_add_symbols) |
013dec1a | 117 | #define elf_add_dynamic_entry NAME(bfd_elf,add_dynamic_entry) |
374d2ef9 ILT |
118 | #define elf_link_create_dynamic_sections \ |
119 | NAME(bfd_elf,link_create_dynamic_sections) | |
120 | #define elf_link_record_dynamic_symbol \ | |
121 | NAME(bfd_elf,link_record_dynamic_symbol) | |
6ec3bb6a | 122 | #define elf_bfd_final_link NAME(bfd_elf,bfd_final_link) |
244ffee7 | 123 | |
6a3eb9b6 KR |
124 | #if ARCH_SIZE == 64 |
125 | #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y) | |
126 | #define ELF_R_SYM(X) ELF64_R_SYM(X) | |
6ec3bb6a | 127 | #define ELF_R_TYPE(X) ELF64_R_TYPE(X) |
32090b8e | 128 | #define ELFCLASS ELFCLASS64 |
f035cc47 | 129 | #define FILE_ALIGN 8 |
013dec1a | 130 | #define LOG_FILE_ALIGN 3 |
6a3eb9b6 KR |
131 | #endif |
132 | #if ARCH_SIZE == 32 | |
133 | #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y) | |
134 | #define ELF_R_SYM(X) ELF32_R_SYM(X) | |
6ec3bb6a | 135 | #define ELF_R_TYPE(X) ELF32_R_TYPE(X) |
32090b8e | 136 | #define ELFCLASS ELFCLASS32 |
f035cc47 | 137 | #define FILE_ALIGN 4 |
013dec1a | 138 | #define LOG_FILE_ALIGN 2 |
244ffee7 JK |
139 | #endif |
140 | ||
244ffee7 JK |
141 | /* Forward declarations of static functions */ |
142 | ||
eb4267a3 | 143 | static struct bfd_strtab_hash *elf_stringtab_init PARAMS ((void)); |
2e03ce18 | 144 | static asection *section_from_elf_index PARAMS ((bfd *, unsigned int)); |
244ffee7 JK |
145 | |
146 | static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *)); | |
147 | ||
cb71adf1 | 148 | static long elf_slurp_symbol_table PARAMS ((bfd *, asymbol **, boolean)); |
244ffee7 | 149 | |
ea617174 ILT |
150 | static boolean elf_slurp_reloc_table PARAMS ((bfd *, asection *, asymbol **)); |
151 | ||
244ffee7 | 152 | static int elf_symbol_from_bfd_symbol PARAMS ((bfd *, |
1c6042ee | 153 | struct symbol_cache_entry **)); |
244ffee7 | 154 | |
6ec3bb6a ILT |
155 | static boolean elf_compute_section_file_positions |
156 | PARAMS ((bfd *, struct bfd_link_info *)); | |
157 | static boolean prep_headers PARAMS ((bfd *)); | |
fa15568a | 158 | static void elf_fake_sections PARAMS ((bfd *, asection *, PTR)); |
6ec3bb6a | 159 | static boolean assign_section_numbers PARAMS ((bfd *)); |
013dec1a ILT |
160 | static file_ptr align_file_position PARAMS ((file_ptr)); |
161 | static file_ptr assign_file_position_for_section | |
162 | PARAMS ((Elf_Internal_Shdr *, file_ptr, boolean)); | |
6ec3bb6a | 163 | static boolean assign_file_positions_except_relocs PARAMS ((bfd *, boolean)); |
013dec1a ILT |
164 | static void assign_file_positions_for_relocs PARAMS ((bfd *)); |
165 | static bfd_size_type get_program_header_size PARAMS ((bfd *)); | |
166 | static file_ptr map_program_segments | |
167 | PARAMS ((bfd *, file_ptr, Elf_Internal_Shdr *, bfd_size_type)); | |
6ec3bb6a | 168 | |
9783e04a | 169 | static boolean elf_map_symbols PARAMS ((bfd *)); |
eb4267a3 | 170 | static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **)); |
244ffee7 | 171 | |
2e03ce18 ILT |
172 | static boolean bfd_section_from_shdr PARAMS ((bfd *, unsigned int shindex)); |
173 | ||
6a3eb9b6 | 174 | #ifdef DEBUG |
eb4267a3 | 175 | static void elf_debug_section PARAMS ((int, Elf_Internal_Shdr *)); |
6a3eb9b6 KR |
176 | static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *)); |
177 | #endif | |
238ac6ec | 178 | |
32090b8e KR |
179 | #define elf_string_from_elf_strtab(abfd,strindex) \ |
180 | elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex) | |
32090b8e KR |
181 | \f |
182 | /* Structure swapping routines */ | |
183 | ||
6a3eb9b6 KR |
184 | /* Should perhaps use put_offset, put_word, etc. For now, the two versions |
185 | can be handled by explicitly specifying 32 bits or "the long type". */ | |
238ac6ec KR |
186 | #if ARCH_SIZE == 64 |
187 | #define put_word bfd_h_put_64 | |
188 | #define get_word bfd_h_get_64 | |
189 | #endif | |
190 | #if ARCH_SIZE == 32 | |
191 | #define put_word bfd_h_put_32 | |
192 | #define get_word bfd_h_get_32 | |
193 | #endif | |
194 | ||
244ffee7 JK |
195 | /* Translate an ELF symbol in external format into an ELF symbol in internal |
196 | format. */ | |
197 | ||
71edd06d | 198 | void |
1c6042ee ILT |
199 | elf_swap_symbol_in (abfd, src, dst) |
200 | bfd *abfd; | |
201 | Elf_External_Sym *src; | |
202 | Elf_Internal_Sym *dst; | |
244ffee7 JK |
203 | { |
204 | dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name); | |
238ac6ec KR |
205 | dst->st_value = get_word (abfd, (bfd_byte *) src->st_value); |
206 | dst->st_size = get_word (abfd, (bfd_byte *) src->st_size); | |
244ffee7 JK |
207 | dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info); |
208 | dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other); | |
209 | dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx); | |
210 | } | |
211 | ||
212 | /* Translate an ELF symbol in internal format into an ELF symbol in external | |
213 | format. */ | |
214 | ||
71edd06d | 215 | void |
1c6042ee ILT |
216 | elf_swap_symbol_out (abfd, src, dst) |
217 | bfd *abfd; | |
218 | Elf_Internal_Sym *src; | |
219 | Elf_External_Sym *dst; | |
244ffee7 JK |
220 | { |
221 | bfd_h_put_32 (abfd, src->st_name, dst->st_name); | |
238ac6ec KR |
222 | put_word (abfd, src->st_value, dst->st_value); |
223 | put_word (abfd, src->st_size, dst->st_size); | |
244ffee7 JK |
224 | bfd_h_put_8 (abfd, src->st_info, dst->st_info); |
225 | bfd_h_put_8 (abfd, src->st_other, dst->st_other); | |
226 | bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx); | |
227 | } | |
228 | ||
229 | ||
230 | /* Translate an ELF file header in external format into an ELF file header in | |
231 | internal format. */ | |
232 | ||
233 | static void | |
1c6042ee ILT |
234 | elf_swap_ehdr_in (abfd, src, dst) |
235 | bfd *abfd; | |
236 | Elf_External_Ehdr *src; | |
237 | Elf_Internal_Ehdr *dst; | |
244ffee7 JK |
238 | { |
239 | memcpy (dst->e_ident, src->e_ident, EI_NIDENT); | |
240 | dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type); | |
241 | dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine); | |
242 | dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version); | |
238ac6ec KR |
243 | dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry); |
244 | dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff); | |
245 | dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff); | |
244ffee7 JK |
246 | dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags); |
247 | dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize); | |
248 | dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize); | |
249 | dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum); | |
250 | dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize); | |
251 | dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum); | |
252 | dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx); | |
253 | } | |
254 | ||
255 | /* Translate an ELF file header in internal format into an ELF file header in | |
256 | external format. */ | |
257 | ||
258 | static void | |
1c6042ee ILT |
259 | elf_swap_ehdr_out (abfd, src, dst) |
260 | bfd *abfd; | |
261 | Elf_Internal_Ehdr *src; | |
262 | Elf_External_Ehdr *dst; | |
244ffee7 JK |
263 | { |
264 | memcpy (dst->e_ident, src->e_ident, EI_NIDENT); | |
265 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
266 | bfd_h_put_16 (abfd, src->e_type, dst->e_type); | |
267 | bfd_h_put_16 (abfd, src->e_machine, dst->e_machine); | |
268 | bfd_h_put_32 (abfd, src->e_version, dst->e_version); | |
238ac6ec KR |
269 | put_word (abfd, src->e_entry, dst->e_entry); |
270 | put_word (abfd, src->e_phoff, dst->e_phoff); | |
271 | put_word (abfd, src->e_shoff, dst->e_shoff); | |
244ffee7 JK |
272 | bfd_h_put_32 (abfd, src->e_flags, dst->e_flags); |
273 | bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize); | |
274 | bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize); | |
275 | bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum); | |
276 | bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize); | |
277 | bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum); | |
278 | bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx); | |
279 | } | |
280 | ||
281 | ||
282 | /* Translate an ELF section header table entry in external format into an | |
283 | ELF section header table entry in internal format. */ | |
284 | ||
285 | static void | |
1c6042ee ILT |
286 | elf_swap_shdr_in (abfd, src, dst) |
287 | bfd *abfd; | |
288 | Elf_External_Shdr *src; | |
289 | Elf_Internal_Shdr *dst; | |
244ffee7 JK |
290 | { |
291 | dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name); | |
292 | dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type); | |
238ac6ec KR |
293 | dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags); |
294 | dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr); | |
295 | dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset); | |
296 | dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size); | |
244ffee7 JK |
297 | dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link); |
298 | dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info); | |
238ac6ec KR |
299 | dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign); |
300 | dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize); | |
24f13b03 ILT |
301 | dst->bfd_section = NULL; |
302 | dst->contents = NULL; | |
244ffee7 JK |
303 | } |
304 | ||
305 | /* Translate an ELF section header table entry in internal format into an | |
306 | ELF section header table entry in external format. */ | |
307 | ||
308 | static void | |
1c6042ee ILT |
309 | elf_swap_shdr_out (abfd, src, dst) |
310 | bfd *abfd; | |
311 | Elf_Internal_Shdr *src; | |
312 | Elf_External_Shdr *dst; | |
244ffee7 JK |
313 | { |
314 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
315 | bfd_h_put_32 (abfd, src->sh_name, dst->sh_name); | |
316 | bfd_h_put_32 (abfd, src->sh_type, dst->sh_type); | |
238ac6ec KR |
317 | put_word (abfd, src->sh_flags, dst->sh_flags); |
318 | put_word (abfd, src->sh_addr, dst->sh_addr); | |
319 | put_word (abfd, src->sh_offset, dst->sh_offset); | |
320 | put_word (abfd, src->sh_size, dst->sh_size); | |
244ffee7 JK |
321 | bfd_h_put_32 (abfd, src->sh_link, dst->sh_link); |
322 | bfd_h_put_32 (abfd, src->sh_info, dst->sh_info); | |
238ac6ec KR |
323 | put_word (abfd, src->sh_addralign, dst->sh_addralign); |
324 | put_word (abfd, src->sh_entsize, dst->sh_entsize); | |
244ffee7 JK |
325 | } |
326 | ||
327 | ||
328 | /* Translate an ELF program header table entry in external format into an | |
329 | ELF program header table entry in internal format. */ | |
330 | ||
331 | static void | |
1c6042ee ILT |
332 | elf_swap_phdr_in (abfd, src, dst) |
333 | bfd *abfd; | |
334 | Elf_External_Phdr *src; | |
335 | Elf_Internal_Phdr *dst; | |
244ffee7 JK |
336 | { |
337 | dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type); | |
244ffee7 | 338 | dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags); |
238ac6ec KR |
339 | dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset); |
340 | dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr); | |
341 | dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr); | |
342 | dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz); | |
343 | dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz); | |
344 | dst->p_align = get_word (abfd, (bfd_byte *) src->p_align); | |
244ffee7 JK |
345 | } |
346 | ||
244ffee7 | 347 | static void |
1c6042ee ILT |
348 | elf_swap_phdr_out (abfd, src, dst) |
349 | bfd *abfd; | |
350 | Elf_Internal_Phdr *src; | |
351 | Elf_External_Phdr *dst; | |
244ffee7 JK |
352 | { |
353 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
354 | bfd_h_put_32 (abfd, src->p_type, dst->p_type); | |
94dbb655 KR |
355 | put_word (abfd, src->p_offset, dst->p_offset); |
356 | put_word (abfd, src->p_vaddr, dst->p_vaddr); | |
357 | put_word (abfd, src->p_paddr, dst->p_paddr); | |
358 | put_word (abfd, src->p_filesz, dst->p_filesz); | |
359 | put_word (abfd, src->p_memsz, dst->p_memsz); | |
244ffee7 | 360 | bfd_h_put_32 (abfd, src->p_flags, dst->p_flags); |
94dbb655 | 361 | put_word (abfd, src->p_align, dst->p_align); |
244ffee7 JK |
362 | } |
363 | ||
364 | /* Translate an ELF reloc from external format to internal format. */ | |
013dec1a | 365 | INLINE void |
1c6042ee ILT |
366 | elf_swap_reloc_in (abfd, src, dst) |
367 | bfd *abfd; | |
368 | Elf_External_Rel *src; | |
369 | Elf_Internal_Rel *dst; | |
244ffee7 | 370 | { |
94dbb655 KR |
371 | dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset); |
372 | dst->r_info = get_word (abfd, (bfd_byte *) src->r_info); | |
244ffee7 JK |
373 | } |
374 | ||
013dec1a | 375 | INLINE void |
1c6042ee ILT |
376 | elf_swap_reloca_in (abfd, src, dst) |
377 | bfd *abfd; | |
378 | Elf_External_Rela *src; | |
379 | Elf_Internal_Rela *dst; | |
244ffee7 | 380 | { |
94dbb655 KR |
381 | dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset); |
382 | dst->r_info = get_word (abfd, (bfd_byte *) src->r_info); | |
383 | dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend); | |
244ffee7 JK |
384 | } |
385 | ||
386 | /* Translate an ELF reloc from internal format to external format. */ | |
013dec1a | 387 | INLINE void |
1c6042ee ILT |
388 | elf_swap_reloc_out (abfd, src, dst) |
389 | bfd *abfd; | |
390 | Elf_Internal_Rel *src; | |
391 | Elf_External_Rel *dst; | |
244ffee7 | 392 | { |
94dbb655 KR |
393 | put_word (abfd, src->r_offset, dst->r_offset); |
394 | put_word (abfd, src->r_info, dst->r_info); | |
244ffee7 JK |
395 | } |
396 | ||
013dec1a | 397 | INLINE void |
1c6042ee ILT |
398 | elf_swap_reloca_out (abfd, src, dst) |
399 | bfd *abfd; | |
400 | Elf_Internal_Rela *src; | |
401 | Elf_External_Rela *dst; | |
244ffee7 | 402 | { |
94dbb655 KR |
403 | put_word (abfd, src->r_offset, dst->r_offset); |
404 | put_word (abfd, src->r_info, dst->r_info); | |
405 | put_word (abfd, src->r_addend, dst->r_addend); | |
244ffee7 | 406 | } |
32090b8e | 407 | |
013dec1a ILT |
408 | INLINE void |
409 | elf_swap_dyn_in (abfd, src, dst) | |
410 | bfd *abfd; | |
411 | const Elf_External_Dyn *src; | |
412 | Elf_Internal_Dyn *dst; | |
413 | { | |
414 | dst->d_tag = get_word (abfd, src->d_tag); | |
415 | dst->d_un.d_val = get_word (abfd, src->d_un.d_val); | |
416 | } | |
1c6042ee | 417 | |
013dec1a ILT |
418 | INLINE void |
419 | elf_swap_dyn_out (abfd, src, dst) | |
420 | bfd *abfd; | |
421 | const Elf_Internal_Dyn *src; | |
422 | Elf_External_Dyn *dst; | |
423 | { | |
424 | put_word (abfd, src->d_tag, dst->d_tag); | |
425 | put_word (abfd, src->d_un.d_val, dst->d_un.d_val); | |
426 | } | |
427 | \f | |
eb4267a3 | 428 | /* Allocate an ELF string table--force the first byte to be zero. */ |
32090b8e | 429 | |
eb4267a3 ILT |
430 | static struct bfd_strtab_hash * |
431 | elf_stringtab_init () | |
32090b8e | 432 | { |
eb4267a3 | 433 | struct bfd_strtab_hash *ret; |
32090b8e | 434 | |
eb4267a3 ILT |
435 | ret = _bfd_stringtab_init (); |
436 | if (ret != NULL) | |
b9d5cdf0 | 437 | { |
eb4267a3 | 438 | bfd_size_type loc; |
244ffee7 | 439 | |
eb4267a3 ILT |
440 | loc = _bfd_stringtab_add (ret, "", true, false); |
441 | BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); | |
442 | if (loc == (bfd_size_type) -1) | |
443 | { | |
444 | _bfd_stringtab_free (ret); | |
445 | ret = NULL; | |
446 | } | |
6ec3bb6a | 447 | } |
eb4267a3 | 448 | return ret; |
244ffee7 | 449 | } |
32090b8e KR |
450 | \f |
451 | /* ELF .o/exec file reading */ | |
452 | ||
453 | /* Create a new bfd section from an ELF section header. */ | |
454 | ||
244ffee7 | 455 | static boolean |
1c6042ee ILT |
456 | bfd_section_from_shdr (abfd, shindex) |
457 | bfd *abfd; | |
458 | unsigned int shindex; | |
244ffee7 | 459 | { |
32090b8e KR |
460 | Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; |
461 | Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); | |
244ffee7 JK |
462 | char *name; |
463 | ||
464 | name = elf_string_from_elf_strtab (abfd, hdr->sh_name); | |
465 | ||
466 | switch (hdr->sh_type) | |
467 | { | |
244ffee7 | 468 | case SHT_NULL: |
497c5434 | 469 | /* Inactive section. Throw it away. */ |
244ffee7 JK |
470 | return true; |
471 | ||
497c5434 ILT |
472 | case SHT_PROGBITS: /* Normal section with contents. */ |
473 | case SHT_DYNAMIC: /* Dynamic linking information. */ | |
474 | case SHT_NOBITS: /* .bss section. */ | |
fa15568a | 475 | case SHT_HASH: /* .hash section. */ |
497c5434 | 476 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
244ffee7 JK |
477 | |
478 | case SHT_SYMTAB: /* A symbol table */ | |
32090b8e KR |
479 | if (elf_onesymtab (abfd) == shindex) |
480 | return true; | |
481 | ||
244ffee7 | 482 | BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym)); |
32090b8e | 483 | BFD_ASSERT (elf_onesymtab (abfd) == 0); |
244ffee7 | 484 | elf_onesymtab (abfd) = shindex; |
1c6042ee ILT |
485 | elf_tdata (abfd)->symtab_hdr = *hdr; |
486 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_hdr; | |
244ffee7 | 487 | abfd->flags |= HAS_SYMS; |
823609fe ILT |
488 | |
489 | /* Sometimes a shared object will map in the symbol table. If | |
490 | SHF_ALLOC is set, and this is a shared object, then we also | |
491 | treat this section as a BFD section. We can not base the | |
492 | decision purely on SHF_ALLOC, because that flag is sometimes | |
493 | set in a relocateable object file, which would confuse the | |
494 | linker. */ | |
495 | if ((hdr->sh_flags & SHF_ALLOC) != 0 | |
496 | && (abfd->flags & DYNAMIC) != 0 | |
497 | && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
498 | return false; | |
499 | ||
244ffee7 JK |
500 | return true; |
501 | ||
cb71adf1 PS |
502 | case SHT_DYNSYM: /* A dynamic symbol table */ |
503 | if (elf_dynsymtab (abfd) == shindex) | |
504 | return true; | |
505 | ||
506 | BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym)); | |
507 | BFD_ASSERT (elf_dynsymtab (abfd) == 0); | |
508 | elf_dynsymtab (abfd) = shindex; | |
509 | elf_tdata (abfd)->dynsymtab_hdr = *hdr; | |
510 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->dynsymtab_hdr; | |
511 | abfd->flags |= HAS_SYMS; | |
fa15568a ILT |
512 | |
513 | /* Besides being a symbol table, we also treat this as a regular | |
514 | section, so that objcopy can handle it. */ | |
515 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); | |
cb71adf1 | 516 | |
244ffee7 | 517 | case SHT_STRTAB: /* A string table */ |
24f13b03 | 518 | if (hdr->bfd_section != NULL) |
fce36137 | 519 | return true; |
32090b8e KR |
520 | if (ehdr->e_shstrndx == shindex) |
521 | { | |
1c6042ee ILT |
522 | elf_tdata (abfd)->shstrtab_hdr = *hdr; |
523 | elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; | |
32090b8e KR |
524 | return true; |
525 | } | |
526 | { | |
68241b2b | 527 | unsigned int i; |
fce36137 | 528 | |
32090b8e KR |
529 | for (i = 1; i < ehdr->e_shnum; i++) |
530 | { | |
1c6042ee | 531 | Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; |
32090b8e KR |
532 | if (hdr2->sh_link == shindex) |
533 | { | |
2e03ce18 ILT |
534 | if (! bfd_section_from_shdr (abfd, i)) |
535 | return false; | |
32090b8e KR |
536 | if (elf_onesymtab (abfd) == i) |
537 | { | |
1c6042ee | 538 | elf_tdata (abfd)->strtab_hdr = *hdr; |
fa15568a ILT |
539 | elf_elfsections (abfd)[shindex] = |
540 | &elf_tdata (abfd)->strtab_hdr; | |
32090b8e KR |
541 | return true; |
542 | } | |
cb71adf1 PS |
543 | if (elf_dynsymtab (abfd) == i) |
544 | { | |
545 | elf_tdata (abfd)->dynstrtab_hdr = *hdr; | |
fa15568a ILT |
546 | elf_elfsections (abfd)[shindex] = |
547 | &elf_tdata (abfd)->dynstrtab_hdr; | |
548 | /* We also treat this as a regular section, so | |
549 | that objcopy can handle it. */ | |
550 | break; | |
cb71adf1 | 551 | } |
2e03ce18 | 552 | #if 0 /* Not handling other string tables specially right now. */ |
1c6042ee | 553 | hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ |
32090b8e | 554 | /* We have a strtab for some random other section. */ |
24f13b03 | 555 | newsect = (asection *) hdr2->bfd_section; |
32090b8e KR |
556 | if (!newsect) |
557 | break; | |
24f13b03 | 558 | hdr->bfd_section = newsect; |
32090b8e KR |
559 | hdr2 = &elf_section_data (newsect)->str_hdr; |
560 | *hdr2 = *hdr; | |
1c6042ee | 561 | elf_elfsections (abfd)[shindex] = hdr2; |
32090b8e KR |
562 | #endif |
563 | } | |
564 | } | |
565 | } | |
566 | ||
497c5434 | 567 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
244ffee7 JK |
568 | |
569 | case SHT_REL: | |
570 | case SHT_RELA: | |
497c5434 | 571 | /* *These* do a lot of work -- but build no sections! */ |
244ffee7 JK |
572 | { |
573 | asection *target_sect; | |
32090b8e | 574 | Elf_Internal_Shdr *hdr2; |
244ffee7 JK |
575 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; |
576 | ||
497c5434 ILT |
577 | /* Get the symbol table. */ |
578 | if (! bfd_section_from_shdr (abfd, hdr->sh_link)) | |
579 | return false; | |
580 | ||
e6667b2b ILT |
581 | /* If this reloc section does not use the main symbol table we |
582 | don't treat it as a reloc section. BFD can't adequately | |
583 | represent such a section, so at least for now, we don't | |
584 | try. We just present it as a normal section. */ | |
585 | if (hdr->sh_link != elf_onesymtab (abfd)) | |
497c5434 ILT |
586 | return _bfd_elf_make_section_from_shdr (abfd, hdr, name); |
587 | ||
244ffee7 JK |
588 | /* Don't allow REL relocations on a machine that uses RELA and |
589 | vice versa. */ | |
590 | /* @@ Actually, the generic ABI does suggest that both might be | |
591 | used in one file. But the four ABI Processor Supplements I | |
592 | have access to right now all specify that only one is used on | |
593 | each of those architectures. It's conceivable that, e.g., a | |
594 | bunch of absolute 32-bit relocs might be more compact in REL | |
595 | form even on a RELA machine... */ | |
497c5434 ILT |
596 | BFD_ASSERT (use_rela_p |
597 | ? (hdr->sh_type == SHT_RELA | |
598 | && hdr->sh_entsize == sizeof (Elf_External_Rela)) | |
599 | : (hdr->sh_type == SHT_REL | |
600 | && hdr->sh_entsize == sizeof (Elf_External_Rel))); | |
601 | ||
602 | if (! bfd_section_from_shdr (abfd, hdr->sh_info)) | |
2e03ce18 | 603 | return false; |
244ffee7 | 604 | target_sect = section_from_elf_index (abfd, hdr->sh_info); |
24f13b03 | 605 | if (target_sect == NULL) |
244ffee7 JK |
606 | return false; |
607 | ||
32090b8e KR |
608 | hdr2 = &elf_section_data (target_sect)->rel_hdr; |
609 | *hdr2 = *hdr; | |
1c6042ee | 610 | elf_elfsections (abfd)[shindex] = hdr2; |
244ffee7 JK |
611 | target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize; |
612 | target_sect->flags |= SEC_RELOC; | |
497c5434 | 613 | target_sect->relocation = NULL; |
244ffee7 | 614 | target_sect->rel_filepos = hdr->sh_offset; |
32090b8e | 615 | abfd->flags |= HAS_RELOC; |
244ffee7 JK |
616 | return true; |
617 | } | |
618 | break; | |
619 | ||
244ffee7 JK |
620 | case SHT_NOTE: |
621 | #if 0 | |
622 | fprintf (stderr, "Note Sections not yet supported.\n"); | |
623 | BFD_FAIL (); | |
624 | #endif | |
625 | break; | |
626 | ||
627 | case SHT_SHLIB: | |
628 | #if 0 | |
629 | fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n"); | |
630 | #endif | |
631 | return true; | |
632 | ||
633 | default: | |
e621c5cc ILT |
634 | /* Check for any processor-specific section types. */ |
635 | { | |
636 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
637 | ||
638 | if (bed->elf_backend_section_from_shdr) | |
639 | (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); | |
640 | } | |
244ffee7 JK |
641 | break; |
642 | } | |
643 | ||
644 | return true; | |
645 | } | |
646 | ||
fce36137 | 647 | boolean |
1c6042ee ILT |
648 | elf_new_section_hook (abfd, sec) |
649 | bfd *abfd | |
650 | ; | |
651 | asection *sec; | |
fce36137 | 652 | { |
32090b8e | 653 | struct bfd_elf_section_data *sdata; |
300adb31 KR |
654 | |
655 | sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata)); | |
9783e04a DM |
656 | if (!sdata) |
657 | { | |
d1ad85a6 | 658 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
659 | return false; |
660 | } | |
300adb31 | 661 | sec->used_by_bfd = (PTR) sdata; |
32090b8e | 662 | memset (sdata, 0, sizeof (*sdata)); |
244ffee7 JK |
663 | return true; |
664 | } | |
665 | ||
666 | /* Create a new bfd section from an ELF program header. | |
667 | ||
668 | Since program segments have no names, we generate a synthetic name | |
669 | of the form segment<NUM>, where NUM is generally the index in the | |
670 | program header table. For segments that are split (see below) we | |
671 | generate the names segment<NUM>a and segment<NUM>b. | |
672 | ||
673 | Note that some program segments may have a file size that is different than | |
674 | (less than) the memory size. All this means is that at execution the | |
675 | system must allocate the amount of memory specified by the memory size, | |
676 | but only initialize it with the first "file size" bytes read from the | |
677 | file. This would occur for example, with program segments consisting | |
678 | of combined data+bss. | |
679 | ||
680 | To handle the above situation, this routine generates TWO bfd sections | |
681 | for the single program segment. The first has the length specified by | |
682 | the file size of the segment, and the second has the length specified | |
683 | by the difference between the two sizes. In effect, the segment is split | |
684 | into it's initialized and uninitialized parts. | |
685 | ||
686 | */ | |
687 | ||
688 | static boolean | |
1c6042ee ILT |
689 | bfd_section_from_phdr (abfd, hdr, index) |
690 | bfd *abfd; | |
691 | Elf_Internal_Phdr *hdr; | |
692 | int index; | |
244ffee7 JK |
693 | { |
694 | asection *newsect; | |
695 | char *name; | |
696 | char namebuf[64]; | |
697 | int split; | |
698 | ||
699 | split = ((hdr->p_memsz > 0) && | |
700 | (hdr->p_filesz > 0) && | |
701 | (hdr->p_memsz > hdr->p_filesz)); | |
702 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
703 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
9783e04a DM |
704 | if (!name) |
705 | { | |
d1ad85a6 | 706 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
707 | return false; |
708 | } | |
244ffee7 JK |
709 | strcpy (name, namebuf); |
710 | newsect = bfd_make_section (abfd, name); | |
2e03ce18 ILT |
711 | if (newsect == NULL) |
712 | return false; | |
244ffee7 JK |
713 | newsect->vma = hdr->p_vaddr; |
714 | newsect->_raw_size = hdr->p_filesz; | |
715 | newsect->filepos = hdr->p_offset; | |
716 | newsect->flags |= SEC_HAS_CONTENTS; | |
717 | if (hdr->p_type == PT_LOAD) | |
718 | { | |
719 | newsect->flags |= SEC_ALLOC; | |
720 | newsect->flags |= SEC_LOAD; | |
721 | if (hdr->p_flags & PF_X) | |
722 | { | |
723 | /* FIXME: all we known is that it has execute PERMISSION, | |
724 | may be data. */ | |
725 | newsect->flags |= SEC_CODE; | |
726 | } | |
727 | } | |
728 | if (!(hdr->p_flags & PF_W)) | |
729 | { | |
730 | newsect->flags |= SEC_READONLY; | |
731 | } | |
732 | ||
733 | if (split) | |
734 | { | |
735 | sprintf (namebuf, "segment%db", index); | |
736 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
9783e04a DM |
737 | if (!name) |
738 | { | |
d1ad85a6 | 739 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
740 | return false; |
741 | } | |
244ffee7 JK |
742 | strcpy (name, namebuf); |
743 | newsect = bfd_make_section (abfd, name); | |
2e03ce18 ILT |
744 | if (newsect == NULL) |
745 | return false; | |
244ffee7 JK |
746 | newsect->vma = hdr->p_vaddr + hdr->p_filesz; |
747 | newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; | |
748 | if (hdr->p_type == PT_LOAD) | |
749 | { | |
750 | newsect->flags |= SEC_ALLOC; | |
751 | if (hdr->p_flags & PF_X) | |
752 | newsect->flags |= SEC_CODE; | |
753 | } | |
754 | if (!(hdr->p_flags & PF_W)) | |
755 | newsect->flags |= SEC_READONLY; | |
756 | } | |
757 | ||
758 | return true; | |
759 | } | |
760 | ||
32090b8e | 761 | /* Begin processing a given object. |
244ffee7 | 762 | |
32090b8e KR |
763 | First we validate the file by reading in the ELF header and checking |
764 | the magic number. */ | |
765 | ||
766 | static INLINE boolean | |
1c6042ee ILT |
767 | elf_file_p (x_ehdrp) |
768 | Elf_External_Ehdr *x_ehdrp; | |
244ffee7 | 769 | { |
32090b8e KR |
770 | return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0) |
771 | && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1) | |
772 | && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2) | |
773 | && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3)); | |
774 | } | |
244ffee7 | 775 | |
d24928c0 KR |
776 | /* Check to see if the file associated with ABFD matches the target vector |
777 | that ABFD points to. | |
778 | ||
779 | Note that we may be called several times with the same ABFD, but different | |
780 | target vectors, most of which will not match. We have to avoid leaving | |
781 | any side effects in ABFD, or any data it points to (like tdata), if the | |
6ec3bb6a | 782 | file does not match the target vector. */ |
d24928c0 | 783 | |
2f3508ad | 784 | const bfd_target * |
1c6042ee ILT |
785 | elf_object_p (abfd) |
786 | bfd *abfd; | |
244ffee7 | 787 | { |
32090b8e KR |
788 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
789 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
790 | Elf_External_Shdr x_shdr; /* Section header table entry, external form */ | |
6ec3bb6a | 791 | Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */ |
68241b2b | 792 | unsigned int shindex; |
32090b8e | 793 | char *shstrtab; /* Internal copy of section header stringtab */ |
062189c6 | 794 | struct elf_backend_data *ebd; |
d24928c0 | 795 | struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd); |
6ec3bb6a | 796 | struct elf_obj_tdata *new_tdata = NULL; |
244ffee7 | 797 | |
32090b8e KR |
798 | /* Read in the ELF header in external format. */ |
799 | ||
800 | if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
25057836 JL |
801 | { |
802 | if (bfd_get_error () != bfd_error_system_call) | |
803 | goto got_wrong_format_error; | |
804 | else | |
805 | goto got_no_match; | |
806 | } | |
244ffee7 | 807 | |
32090b8e KR |
808 | /* Now check to see if we have a valid ELF file, and one that BFD can |
809 | make use of. The magic number must match, the address size ('class') | |
810 | and byte-swapping must match our XVEC entry, and it must have a | |
811 | section header table (FIXME: See comments re sections at top of this | |
812 | file). */ | |
244ffee7 | 813 | |
d24928c0 KR |
814 | if ((elf_file_p (&x_ehdr) == false) || |
815 | (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) || | |
816 | (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)) | |
817 | goto got_wrong_format_error; | |
244ffee7 | 818 | |
d24928c0 | 819 | /* Check that file's byte order matches xvec's */ |
32090b8e | 820 | switch (x_ehdr.e_ident[EI_DATA]) |
244ffee7 | 821 | { |
32090b8e KR |
822 | case ELFDATA2MSB: /* Big-endian */ |
823 | if (!abfd->xvec->header_byteorder_big_p) | |
d24928c0 | 824 | goto got_wrong_format_error; |
32090b8e KR |
825 | break; |
826 | case ELFDATA2LSB: /* Little-endian */ | |
827 | if (abfd->xvec->header_byteorder_big_p) | |
d24928c0 | 828 | goto got_wrong_format_error; |
32090b8e KR |
829 | break; |
830 | case ELFDATANONE: /* No data encoding specified */ | |
831 | default: /* Unknown data encoding specified */ | |
d24928c0 | 832 | goto got_wrong_format_error; |
244ffee7 | 833 | } |
244ffee7 | 834 | |
32090b8e | 835 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to |
6ec3bb6a | 836 | the tdata pointer in the bfd. */ |
244ffee7 | 837 | |
6ec3bb6a ILT |
838 | new_tdata = ((struct elf_obj_tdata *) |
839 | bfd_zalloc (abfd, sizeof (struct elf_obj_tdata))); | |
840 | if (new_tdata == NULL) | |
d24928c0 | 841 | goto got_no_memory_error; |
6ec3bb6a | 842 | elf_tdata (abfd) = new_tdata; |
244ffee7 | 843 | |
32090b8e KR |
844 | /* Now that we know the byte order, swap in the rest of the header */ |
845 | i_ehdrp = elf_elfheader (abfd); | |
846 | elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); | |
847 | #if DEBUG & 1 | |
848 | elf_debug_file (i_ehdrp); | |
244ffee7 JK |
849 | #endif |
850 | ||
32090b8e KR |
851 | /* If there is no section header table, we're hosed. */ |
852 | if (i_ehdrp->e_shoff == 0) | |
d24928c0 | 853 | goto got_wrong_format_error; |
244ffee7 | 854 | |
062189c6 ILT |
855 | /* As a simple sanity check, verify that the what BFD thinks is the |
856 | size of each section header table entry actually matches the size | |
857 | recorded in the file. */ | |
858 | if (i_ehdrp->e_shentsize != sizeof (x_shdr)) | |
859 | goto got_wrong_format_error; | |
860 | ||
861 | ebd = get_elf_backend_data (abfd); | |
862 | ||
863 | /* Check that the ELF e_machine field matches what this particular | |
864 | BFD format expects. */ | |
865 | if (ebd->elf_machine_code != i_ehdrp->e_machine) | |
866 | { | |
2f3508ad | 867 | const bfd_target * const *target_ptr; |
062189c6 ILT |
868 | |
869 | if (ebd->elf_machine_code != EM_NONE) | |
870 | goto got_wrong_format_error; | |
871 | ||
872 | /* This is the generic ELF target. Let it match any ELF target | |
873 | for which we do not have a specific backend. */ | |
f4bd7a8f | 874 | for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) |
062189c6 ILT |
875 | { |
876 | struct elf_backend_data *back; | |
877 | ||
878 | if ((*target_ptr)->flavour != bfd_target_elf_flavour) | |
879 | continue; | |
880 | back = (struct elf_backend_data *) (*target_ptr)->backend_data; | |
881 | if (back->elf_machine_code == i_ehdrp->e_machine) | |
882 | { | |
883 | /* target_ptr is an ELF backend which matches this | |
884 | object file, so reject the generic ELF target. */ | |
885 | goto got_wrong_format_error; | |
886 | } | |
887 | } | |
888 | } | |
889 | ||
7b8106b4 | 890 | if (i_ehdrp->e_type == ET_EXEC) |
32090b8e | 891 | abfd->flags |= EXEC_P; |
7b8106b4 ILT |
892 | else if (i_ehdrp->e_type == ET_DYN) |
893 | abfd->flags |= DYNAMIC; | |
244ffee7 | 894 | |
fa15568a ILT |
895 | if (i_ehdrp->e_phnum > 0) |
896 | abfd->flags |= D_PAGED; | |
897 | ||
6ec3bb6a ILT |
898 | if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)) |
899 | goto got_no_match; | |
32090b8e | 900 | |
062189c6 ILT |
901 | /* Remember the entry point specified in the ELF file header. */ |
902 | bfd_get_start_address (abfd) = i_ehdrp->e_entry; | |
32090b8e KR |
903 | |
904 | /* Allocate space for a copy of the section header table in | |
905 | internal form, seek to the section header table in the file, | |
062189c6 | 906 | read it in, and convert it to internal form. */ |
6ec3bb6a ILT |
907 | i_shdrp = ((Elf_Internal_Shdr *) |
908 | bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum)); | |
909 | elf_elfsections (abfd) = ((Elf_Internal_Shdr **) | |
910 | bfd_alloc (abfd, | |
911 | sizeof (i_shdrp) * i_ehdrp->e_shnum)); | |
1c6042ee | 912 | if (!i_shdrp || !elf_elfsections (abfd)) |
d24928c0 | 913 | goto got_no_memory_error; |
6ec3bb6a | 914 | if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0) |
25057836 | 915 | goto got_no_match; |
32090b8e | 916 | for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++) |
244ffee7 | 917 | { |
d24928c0 | 918 | if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr)) |
25057836 | 919 | goto got_no_match; |
32090b8e | 920 | elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex); |
1c6042ee | 921 | elf_elfsections (abfd)[shindex] = i_shdrp + shindex; |
244ffee7 | 922 | } |
32090b8e | 923 | if (i_ehdrp->e_shstrndx) |
244ffee7 | 924 | { |
2e03ce18 ILT |
925 | if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx)) |
926 | goto got_no_match; | |
244ffee7 JK |
927 | } |
928 | ||
32090b8e KR |
929 | /* Read in the string table containing the names of the sections. We |
930 | will need the base pointer to this table later. */ | |
931 | /* We read this inline now, so that we don't have to go through | |
932 | bfd_section_from_shdr with it (since this particular strtab is | |
933 | used to find all of the ELF section names.) */ | |
244ffee7 | 934 | |
32090b8e KR |
935 | shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx); |
936 | if (!shstrtab) | |
6ec3bb6a | 937 | goto got_no_match; |
244ffee7 | 938 | |
32090b8e KR |
939 | /* Once all of the section headers have been read and converted, we |
940 | can start processing them. Note that the first section header is | |
6ec3bb6a | 941 | a dummy placeholder entry, so we ignore it. */ |
244ffee7 | 942 | |
32090b8e KR |
943 | for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++) |
944 | { | |
2e03ce18 ILT |
945 | if (! bfd_section_from_shdr (abfd, shindex)) |
946 | goto got_no_match; | |
32090b8e | 947 | } |
244ffee7 | 948 | |
5315c428 ILT |
949 | /* Let the backend double check the format and override global |
950 | information. */ | |
951 | if (ebd->elf_backend_object_p) | |
952 | { | |
953 | if ((*ebd->elf_backend_object_p) (abfd) == false) | |
954 | goto got_wrong_format_error; | |
955 | } | |
956 | ||
d24928c0 KR |
957 | return (abfd->xvec); |
958 | ||
1c6042ee | 959 | got_wrong_format_error: |
d1ad85a6 | 960 | bfd_set_error (bfd_error_wrong_format); |
d24928c0 | 961 | goto got_no_match; |
1c6042ee | 962 | got_no_memory_error: |
d1ad85a6 | 963 | bfd_set_error (bfd_error_no_memory); |
d24928c0 | 964 | goto got_no_match; |
1c6042ee | 965 | got_no_match: |
6ec3bb6a ILT |
966 | if (new_tdata != NULL |
967 | && new_tdata->elf_sect_ptr != NULL) | |
968 | bfd_release (abfd, new_tdata->elf_sect_ptr); | |
969 | if (i_shdrp != NULL) | |
970 | bfd_release (abfd, i_shdrp); | |
971 | if (new_tdata != NULL) | |
972 | bfd_release (abfd, new_tdata); | |
d24928c0 KR |
973 | elf_tdata (abfd) = preserved_tdata; |
974 | return (NULL); | |
32090b8e | 975 | } |
32090b8e | 976 | \f |
1c6042ee | 977 | |
32090b8e KR |
978 | /* ELF .o/exec file writing */ |
979 | ||
d24928c0 KR |
980 | /* Takes a bfd and a symbol, returns a pointer to the elf specific area |
981 | of the symbol if there is one. */ | |
32090b8e | 982 | static INLINE elf_symbol_type * |
1c6042ee ILT |
983 | elf_symbol_from (ignore_abfd, symbol) |
984 | bfd *ignore_abfd; | |
985 | asymbol *symbol; | |
244ffee7 | 986 | { |
32090b8e KR |
987 | if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour) |
988 | return 0; | |
989 | ||
990 | if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL) | |
991 | return 0; | |
992 | ||
993 | return (elf_symbol_type *) symbol; | |
244ffee7 JK |
994 | } |
995 | ||
32090b8e KR |
996 | void |
997 | write_relocs (abfd, sec, xxx) | |
998 | bfd *abfd; | |
999 | asection *sec; | |
1000 | PTR xxx; | |
1001 | { | |
1002 | Elf_Internal_Shdr *rela_hdr; | |
1003 | Elf_External_Rela *outbound_relocas; | |
1004 | Elf_External_Rel *outbound_relocs; | |
1005 | int idx; | |
1006 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
300adb31 | 1007 | asymbol *last_sym = 0; |
38a5f510 | 1008 | int last_sym_idx = 9999999; /* should always be written before use */ |
244ffee7 | 1009 | |
32090b8e KR |
1010 | if ((sec->flags & SEC_RELOC) == 0) |
1011 | return; | |
6ec3bb6a ILT |
1012 | |
1013 | /* The linker backend writes the relocs out itself, and sets the | |
1014 | reloc_count field to zero to inhibit writing them here. Also, | |
1015 | sometimes the SEC_RELOC flag gets set even when there aren't any | |
1016 | relocs. */ | |
32090b8e KR |
1017 | if (sec->reloc_count == 0) |
1018 | return; | |
244ffee7 | 1019 | |
32090b8e | 1020 | rela_hdr = &elf_section_data (sec)->rel_hdr; |
244ffee7 | 1021 | |
32090b8e KR |
1022 | rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count; |
1023 | rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size); | |
9783e04a DM |
1024 | if (!rela_hdr->contents) |
1025 | { | |
d1ad85a6 | 1026 | bfd_set_error (bfd_error_no_memory); |
1c6042ee | 1027 | abort (); /* FIXME */ |
9783e04a | 1028 | } |
244ffee7 | 1029 | |
32090b8e | 1030 | /* orelocation has the data, reloc_count has the count... */ |
300adb31 KR |
1031 | if (use_rela_p) |
1032 | { | |
1033 | outbound_relocas = (Elf_External_Rela *) rela_hdr->contents; | |
1034 | ||
1035 | for (idx = 0; idx < sec->reloc_count; idx++) | |
32090b8e | 1036 | { |
300adb31 KR |
1037 | Elf_Internal_Rela dst_rela; |
1038 | Elf_External_Rela *src_rela; | |
1039 | arelent *ptr; | |
1040 | asymbol *sym; | |
1041 | int n; | |
1042 | ||
1043 | ptr = sec->orelocation[idx]; | |
1044 | src_rela = outbound_relocas + idx; | |
1045 | if (!(abfd->flags & EXEC_P)) | |
1046 | dst_rela.r_offset = ptr->address - sec->vma; | |
1047 | else | |
1048 | dst_rela.r_offset = ptr->address; | |
6a3eb9b6 | 1049 | |
300adb31 KR |
1050 | sym = *ptr->sym_ptr_ptr; |
1051 | if (sym == last_sym) | |
1052 | n = last_sym_idx; | |
1053 | else | |
32090b8e | 1054 | { |
300adb31 KR |
1055 | last_sym = sym; |
1056 | last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym); | |
32090b8e | 1057 | } |
300adb31 KR |
1058 | dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type); |
1059 | ||
1060 | dst_rela.r_addend = ptr->addend; | |
1061 | elf_swap_reloca_out (abfd, &dst_rela, src_rela); | |
244ffee7 | 1062 | } |
300adb31 KR |
1063 | } |
1064 | else | |
1065 | /* REL relocations */ | |
1066 | { | |
1067 | outbound_relocs = (Elf_External_Rel *) rela_hdr->contents; | |
1068 | ||
1069 | for (idx = 0; idx < sec->reloc_count; idx++) | |
32090b8e | 1070 | { |
300adb31 KR |
1071 | Elf_Internal_Rel dst_rel; |
1072 | Elf_External_Rel *src_rel; | |
1073 | arelent *ptr; | |
1074 | int n; | |
1075 | asymbol *sym; | |
1076 | ||
1077 | ptr = sec->orelocation[idx]; | |
1078 | sym = *ptr->sym_ptr_ptr; | |
1079 | src_rel = outbound_relocs + idx; | |
1080 | if (!(abfd->flags & EXEC_P)) | |
1081 | dst_rel.r_offset = ptr->address - sec->vma; | |
1082 | else | |
1083 | dst_rel.r_offset = ptr->address; | |
244ffee7 | 1084 | |
300adb31 KR |
1085 | if (sym == last_sym) |
1086 | n = last_sym_idx; | |
1087 | else | |
32090b8e | 1088 | { |
300adb31 KR |
1089 | last_sym = sym; |
1090 | last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym); | |
32090b8e | 1091 | } |
300adb31 KR |
1092 | dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type); |
1093 | ||
1094 | elf_swap_reloc_out (abfd, &dst_rel, src_rel); | |
32090b8e | 1095 | } |
300adb31 | 1096 | } |
32090b8e | 1097 | } |
244ffee7 | 1098 | |
fa15568a | 1099 | /* Set up an ELF internal section header for a section. */ |
244ffee7 | 1100 | |
fa15568a | 1101 | /*ARGSUSED*/ |
32090b8e | 1102 | static void |
eb4267a3 | 1103 | elf_fake_sections (abfd, asect, failedptrarg) |
1c6042ee ILT |
1104 | bfd *abfd; |
1105 | asection *asect; | |
eb4267a3 | 1106 | PTR failedptrarg; |
32090b8e | 1107 | { |
eb4267a3 | 1108 | boolean *failedptr = (boolean *) failedptrarg; |
32090b8e | 1109 | Elf_Internal_Shdr *this_hdr; |
fa15568a | 1110 | |
eb4267a3 ILT |
1111 | if (*failedptr) |
1112 | { | |
1113 | /* We already failed; just get out of the bfd_map_over_sections | |
1114 | loop. */ | |
1115 | return; | |
1116 | } | |
1117 | ||
32090b8e | 1118 | this_hdr = &elf_section_data (asect)->this_hdr; |
fa15568a | 1119 | |
eb4267a3 ILT |
1120 | this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd), |
1121 | asect->name, | |
1122 | true, false); | |
6ec3bb6a | 1123 | if (this_hdr->sh_name == (unsigned long) -1) |
eb4267a3 ILT |
1124 | { |
1125 | *failedptr = true; | |
1126 | return; | |
1127 | } | |
fa15568a ILT |
1128 | |
1129 | this_hdr->sh_flags = 0; | |
1130 | if ((asect->flags & SEC_ALLOC) != 0) | |
1131 | this_hdr->sh_addr = asect->vma; | |
1132 | else | |
1133 | this_hdr->sh_addr = 0; | |
1134 | this_hdr->sh_offset = 0; | |
1135 | this_hdr->sh_size = asect->_raw_size; | |
1136 | this_hdr->sh_link = 0; | |
1137 | this_hdr->sh_info = 0; | |
32090b8e | 1138 | this_hdr->sh_addralign = 1 << asect->alignment_power; |
fa15568a ILT |
1139 | this_hdr->sh_entsize = 0; |
1140 | ||
24f13b03 | 1141 | this_hdr->bfd_section = asect; |
fa15568a | 1142 | this_hdr->contents = NULL; |
013dec1a ILT |
1143 | |
1144 | /* FIXME: This should not be based on section names. */ | |
1145 | if (strcmp (asect->name, ".dynstr") == 0) | |
1146 | this_hdr->sh_type = SHT_STRTAB; | |
1147 | else if (strcmp (asect->name, ".hash") == 0) | |
fa15568a ILT |
1148 | { |
1149 | this_hdr->sh_type = SHT_HASH; | |
1150 | this_hdr->sh_entsize = ARCH_SIZE / 8; | |
1151 | } | |
013dec1a | 1152 | else if (strcmp (asect->name, ".dynsym") == 0) |
fa15568a ILT |
1153 | { |
1154 | this_hdr->sh_type = SHT_DYNSYM; | |
1155 | this_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
1156 | } | |
013dec1a | 1157 | else if (strcmp (asect->name, ".dynamic") == 0) |
fa15568a ILT |
1158 | { |
1159 | this_hdr->sh_type = SHT_DYNAMIC; | |
1160 | this_hdr->sh_entsize = sizeof (Elf_External_Dyn); | |
1161 | } | |
0822b56d ILT |
1162 | else if (strncmp (asect->name, ".rela", 5) == 0 |
1163 | && get_elf_backend_data (abfd)->use_rela_p) | |
fa15568a ILT |
1164 | { |
1165 | this_hdr->sh_type = SHT_RELA; | |
1166 | this_hdr->sh_entsize = sizeof (Elf_External_Rela); | |
1167 | } | |
0822b56d ILT |
1168 | else if (strncmp (asect->name, ".rel", 4) == 0 |
1169 | && ! get_elf_backend_data (abfd)->use_rela_p) | |
1170 | { | |
1171 | this_hdr->sh_type = SHT_REL; | |
1172 | this_hdr->sh_entsize = sizeof (Elf_External_Rel); | |
1173 | } | |
fa15568a ILT |
1174 | else if (strcmp (asect->name, ".note") == 0) |
1175 | this_hdr->sh_type = SHT_NOTE; | |
1176 | else if (strncmp (asect->name, ".stab", 5) == 0 | |
1177 | && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) | |
1178 | this_hdr->sh_type = SHT_STRTAB; | |
1179 | else if ((asect->flags & SEC_ALLOC) != 0 | |
1180 | && (asect->flags & SEC_LOAD) != 0) | |
32090b8e | 1181 | this_hdr->sh_type = SHT_PROGBITS; |
fa15568a ILT |
1182 | else if ((asect->flags & SEC_ALLOC) != 0 |
1183 | && ((asect->flags & SEC_LOAD) == 0)) | |
e621c5cc | 1184 | { |
6c35a16d ILT |
1185 | BFD_ASSERT (strcmp (asect->name, ".bss") == 0 |
1186 | || strcmp (asect->name, ".sbss") == 0); | |
e621c5cc ILT |
1187 | this_hdr->sh_type = SHT_NOBITS; |
1188 | } | |
32090b8e | 1189 | else |
fa15568a ILT |
1190 | { |
1191 | /* Who knows? */ | |
1192 | this_hdr->sh_type = SHT_PROGBITS; | |
1193 | } | |
32090b8e | 1194 | |
fa15568a ILT |
1195 | if ((asect->flags & SEC_ALLOC) != 0) |
1196 | this_hdr->sh_flags |= SHF_ALLOC; | |
1197 | if ((asect->flags & SEC_READONLY) == 0) | |
1198 | this_hdr->sh_flags |= SHF_WRITE; | |
1199 | if ((asect->flags & SEC_CODE) != 0) | |
1200 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
244ffee7 | 1201 | |
fa15568a | 1202 | /* Check for processor-specific section types. */ |
f035cc47 ILT |
1203 | { |
1204 | struct elf_backend_data *bed = get_elf_backend_data (abfd); | |
1205 | ||
1206 | if (bed->elf_backend_fake_sections) | |
1207 | (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect); | |
1208 | } | |
1209 | ||
fa15568a ILT |
1210 | /* If the section has relocs, set up a section header for the |
1211 | SHT_REL[A] section. */ | |
1212 | if ((asect->flags & SEC_RELOC) != 0) | |
1213 | { | |
1214 | Elf_Internal_Shdr *rela_hdr; | |
1215 | int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; | |
eb4267a3 | 1216 | char *name; |
244ffee7 | 1217 | |
fa15568a | 1218 | rela_hdr = &elf_section_data (asect)->rel_hdr; |
eb4267a3 ILT |
1219 | name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name)); |
1220 | if (name == NULL) | |
1221 | { | |
1222 | bfd_set_error (bfd_error_no_memory); | |
1223 | *failedptr = true; | |
1224 | return; | |
1225 | } | |
1226 | sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); | |
fa15568a | 1227 | rela_hdr->sh_name = |
eb4267a3 ILT |
1228 | (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name, |
1229 | true, false); | |
1230 | if (rela_hdr->sh_name == (unsigned int) -1) | |
1231 | { | |
1232 | *failedptr = true; | |
1233 | return; | |
1234 | } | |
fa15568a ILT |
1235 | rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; |
1236 | rela_hdr->sh_entsize = (use_rela_p | |
1237 | ? sizeof (Elf_External_Rela) | |
1238 | : sizeof (Elf_External_Rel)); | |
1239 | rela_hdr->sh_addralign = FILE_ALIGN; | |
1240 | rela_hdr->sh_flags = 0; | |
1241 | rela_hdr->sh_addr = 0; | |
1242 | rela_hdr->sh_size = 0; | |
1243 | rela_hdr->sh_offset = 0; | |
fa15568a ILT |
1244 | } |
1245 | } | |
244ffee7 | 1246 | |
fa15568a ILT |
1247 | /* Assign all ELF section numbers. The dummy first section is handled here |
1248 | too. The link/info pointers for the standard section types are filled | |
e6667b2b | 1249 | in here too, while we're at it. */ |
244ffee7 | 1250 | |
fa15568a ILT |
1251 | static boolean |
1252 | assign_section_numbers (abfd) | |
1253 | bfd *abfd; | |
1254 | { | |
1255 | struct elf_obj_tdata *t = elf_tdata (abfd); | |
1256 | asection *sec; | |
1257 | unsigned int section_number; | |
1258 | Elf_Internal_Shdr **i_shdrp; | |
1259 | ||
1260 | section_number = 1; | |
1261 | ||
fa15568a ILT |
1262 | for (sec = abfd->sections; sec; sec = sec->next) |
1263 | { | |
1264 | struct bfd_elf_section_data *d = elf_section_data (sec); | |
1265 | ||
1266 | d->this_idx = section_number++; | |
1267 | if ((sec->flags & SEC_RELOC) == 0) | |
1268 | d->rel_idx = 0; | |
1269 | else | |
1270 | d->rel_idx = section_number++; | |
1271 | } | |
1272 | ||
7c726b66 ILT |
1273 | t->shstrtab_section = section_number++; |
1274 | elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; | |
eb4267a3 | 1275 | t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); |
7c726b66 ILT |
1276 | |
1277 | if (abfd->symcount > 0) | |
1278 | { | |
1279 | t->symtab_section = section_number++; | |
1280 | t->strtab_section = section_number++; | |
1281 | } | |
1282 | ||
fa15568a ILT |
1283 | elf_elfheader (abfd)->e_shnum = section_number; |
1284 | ||
1285 | /* Set up the list of section header pointers, in agreement with the | |
1286 | indices. */ | |
1287 | i_shdrp = ((Elf_Internal_Shdr **) | |
1288 | bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *))); | |
1289 | if (i_shdrp == NULL) | |
1290 | { | |
1291 | bfd_set_error (bfd_error_no_memory); | |
1292 | return false; | |
1293 | } | |
1294 | ||
1295 | i_shdrp[0] = ((Elf_Internal_Shdr *) | |
1296 | bfd_alloc (abfd, sizeof (Elf_Internal_Shdr))); | |
1297 | if (i_shdrp[0] == NULL) | |
1298 | { | |
1299 | bfd_release (abfd, i_shdrp); | |
1300 | bfd_set_error (bfd_error_no_memory); | |
1301 | return false; | |
1302 | } | |
1303 | memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr)); | |
1304 | ||
1305 | elf_elfsections (abfd) = i_shdrp; | |
1306 | ||
1307 | i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; | |
1308 | if (abfd->symcount > 0) | |
1309 | { | |
1310 | i_shdrp[t->symtab_section] = &t->symtab_hdr; | |
1311 | i_shdrp[t->strtab_section] = &t->strtab_hdr; | |
1312 | t->symtab_hdr.sh_link = t->strtab_section; | |
1313 | } | |
1314 | for (sec = abfd->sections; sec; sec = sec->next) | |
32090b8e | 1315 | { |
fa15568a ILT |
1316 | struct bfd_elf_section_data *d = elf_section_data (sec); |
1317 | asection *s; | |
1318 | const char *name; | |
1319 | ||
1320 | i_shdrp[d->this_idx] = &d->this_hdr; | |
1321 | if (d->rel_idx != 0) | |
1322 | i_shdrp[d->rel_idx] = &d->rel_hdr; | |
1323 | ||
1324 | /* Fill in the sh_link and sh_info fields while we're at it. */ | |
1325 | ||
1326 | /* sh_link of a reloc section is the section index of the symbol | |
1327 | table. sh_info is the section index of the section to which | |
1328 | the relocation entries apply. */ | |
1329 | if (d->rel_idx != 0) | |
1330 | { | |
1331 | d->rel_hdr.sh_link = t->symtab_section; | |
1332 | d->rel_hdr.sh_info = d->this_idx; | |
1333 | } | |
1334 | ||
1335 | switch (d->this_hdr.sh_type) | |
32090b8e | 1336 | { |
fa15568a ILT |
1337 | case SHT_REL: |
1338 | case SHT_RELA: | |
1339 | /* A reloc section which we are treating as a normal BFD | |
1340 | section. sh_link is the section index of the symbol | |
1341 | table. sh_info is the section index of the section to | |
1342 | which the relocation entries apply. We assume that an | |
1343 | allocated reloc section uses the dynamic symbol table. | |
1344 | FIXME: How can we be sure? */ | |
1345 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1346 | if (s != NULL) | |
1347 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1348 | ||
1349 | /* We look up the section the relocs apply to by name. */ | |
1350 | name = sec->name; | |
1351 | if (d->this_hdr.sh_type == SHT_REL) | |
1352 | name += 4; | |
1353 | else | |
1354 | name += 5; | |
1355 | s = bfd_get_section_by_name (abfd, name); | |
1356 | if (s != NULL) | |
1357 | d->this_hdr.sh_info = elf_section_data (s)->this_idx; | |
1358 | break; | |
1359 | ||
1360 | case SHT_STRTAB: | |
1361 | /* We assume that a section named .stab*str is a stabs | |
1362 | string section. We look for a section with the same name | |
1363 | but without the trailing ``str'', and set its sh_link | |
1364 | field to point to this section. */ | |
1365 | if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 | |
1366 | && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) | |
1367 | { | |
1368 | size_t len; | |
1369 | char *alc; | |
1370 | ||
1371 | len = strlen (sec->name); | |
1372 | alc = (char *) malloc (len - 2); | |
1373 | if (alc == NULL) | |
1374 | { | |
1375 | bfd_set_error (bfd_error_no_memory); | |
1376 | return false; | |
1377 | } | |
1378 | strncpy (alc, sec->name, len - 3); | |
1379 | alc[len - 3] = '\0'; | |
1380 | s = bfd_get_section_by_name (abfd, alc); | |
1381 | free (alc); | |
1382 | if (s != NULL) | |
1383 | { | |
1384 | elf_section_data (s)->this_hdr.sh_link = d->this_idx; | |
1385 | ||
1386 | /* This is a .stab section. */ | |
1387 | elf_section_data (s)->this_hdr.sh_entsize = | |
1388 | 4 + 2 * (ARCH_SIZE / 8); | |
1389 | } | |
1390 | } | |
1391 | break; | |
1392 | ||
1393 | case SHT_DYNAMIC: | |
1394 | case SHT_DYNSYM: | |
1395 | /* sh_link is the section header index of the string table | |
1396 | used for the dynamic entries or symbol table. */ | |
1397 | s = bfd_get_section_by_name (abfd, ".dynstr"); | |
1398 | if (s != NULL) | |
1399 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1400 | break; | |
1401 | ||
1402 | case SHT_HASH: | |
1403 | /* sh_link is the section header index of the symbol table | |
1404 | this hash table is for. */ | |
1405 | s = bfd_get_section_by_name (abfd, ".dynsym"); | |
1406 | if (s != NULL) | |
1407 | d->this_hdr.sh_link = elf_section_data (s)->this_idx; | |
1408 | break; | |
32090b8e KR |
1409 | } |
1410 | } | |
fa15568a ILT |
1411 | |
1412 | return true; | |
244ffee7 JK |
1413 | } |
1414 | ||
32090b8e KR |
1415 | /* Map symbol from it's internal number to the external number, moving |
1416 | all local symbols to be at the head of the list. */ | |
244ffee7 | 1417 | |
32090b8e | 1418 | static INLINE int |
062189c6 ILT |
1419 | sym_is_global (abfd, sym) |
1420 | bfd *abfd; | |
32090b8e KR |
1421 | asymbol *sym; |
1422 | { | |
062189c6 ILT |
1423 | /* If the backend has a special mapping, use it. */ |
1424 | if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1425 | return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) | |
1426 | (abfd, sym)); | |
1427 | ||
d24928c0 | 1428 | if (sym->flags & (BSF_GLOBAL | BSF_WEAK)) |
244ffee7 | 1429 | { |
32090b8e KR |
1430 | if (sym->flags & BSF_LOCAL) |
1431 | abort (); | |
1432 | return 1; | |
244ffee7 | 1433 | } |
d24928c0 KR |
1434 | if (sym->section == 0) |
1435 | { | |
1436 | /* Is this valid? */ | |
1437 | abort (); | |
1438 | ||
1439 | return 1; | |
1440 | } | |
badd23e3 | 1441 | if (bfd_is_und_section (sym->section)) |
32090b8e KR |
1442 | return 1; |
1443 | if (bfd_is_com_section (sym->section)) | |
1444 | return 1; | |
1445 | if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE)) | |
1446 | return 0; | |
1447 | return 0; | |
1448 | } | |
244ffee7 | 1449 | |
9783e04a | 1450 | static boolean |
1c6042ee ILT |
1451 | elf_map_symbols (abfd) |
1452 | bfd *abfd; | |
32090b8e KR |
1453 | { |
1454 | int symcount = bfd_get_symcount (abfd); | |
1455 | asymbol **syms = bfd_get_outsymbols (abfd); | |
d24928c0 | 1456 | asymbol **sect_syms; |
32090b8e KR |
1457 | int num_locals = 0; |
1458 | int num_globals = 0; | |
1459 | int num_locals2 = 0; | |
1460 | int num_globals2 = 0; | |
d24928c0 | 1461 | int max_index = 0; |
32090b8e | 1462 | int num_sections = 0; |
32090b8e KR |
1463 | int idx; |
1464 | asection *asect; | |
e4a4da62 | 1465 | asymbol **new_syms; |
6a3eb9b6 | 1466 | |
32090b8e KR |
1467 | #ifdef DEBUG |
1468 | fprintf (stderr, "elf_map_symbols\n"); | |
1469 | fflush (stderr); | |
1470 | #endif | |
244ffee7 | 1471 | |
e4a4da62 ILT |
1472 | /* Add a section symbol for each BFD section. FIXME: Is this really |
1473 | necessary? */ | |
32090b8e | 1474 | for (asect = abfd->sections; asect; asect = asect->next) |
244ffee7 | 1475 | { |
d24928c0 KR |
1476 | if (max_index < asect->index) |
1477 | max_index = asect->index; | |
244ffee7 JK |
1478 | } |
1479 | ||
d24928c0 | 1480 | max_index++; |
d24928c0 | 1481 | sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *)); |
e4a4da62 | 1482 | if (sect_syms == NULL) |
9783e04a | 1483 | { |
d1ad85a6 | 1484 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1485 | return false; |
1486 | } | |
e4a4da62 | 1487 | elf_section_syms (abfd) = sect_syms; |
d24928c0 | 1488 | |
24f13b03 | 1489 | for (idx = 0; idx < symcount; idx++) |
e621c5cc | 1490 | { |
24f13b03 | 1491 | if ((syms[idx]->flags & BSF_SECTION_SYM) != 0) |
9783e04a | 1492 | { |
24f13b03 ILT |
1493 | asection *sec; |
1494 | ||
1495 | sec = syms[idx]->section; | |
e4a4da62 | 1496 | if (sec->owner != NULL) |
24f13b03 ILT |
1497 | { |
1498 | if (sec->owner != abfd) | |
1499 | { | |
1500 | sec = sec->output_section; | |
1501 | BFD_ASSERT (sec->owner == abfd); | |
1502 | } | |
1503 | sect_syms[sec->index] = syms[idx]; | |
1504 | } | |
9783e04a | 1505 | } |
24f13b03 ILT |
1506 | } |
1507 | ||
1508 | for (asect = abfd->sections; asect; asect = asect->next) | |
1509 | { | |
1510 | asymbol *sym; | |
1511 | ||
1512 | if (sect_syms[asect->index] != NULL) | |
1513 | continue; | |
1514 | ||
1515 | sym = bfd_make_empty_symbol (abfd); | |
1516 | if (sym == NULL) | |
1517 | return false; | |
e621c5cc ILT |
1518 | sym->the_bfd = abfd; |
1519 | sym->name = asect->name; | |
1520 | sym->value = asect->vma; | |
24f13b03 ILT |
1521 | /* Set the flags to 0 to indicate that this one was newly added. */ |
1522 | sym->flags = 0; | |
e621c5cc ILT |
1523 | sym->section = asect; |
1524 | sect_syms[asect->index] = sym; | |
1525 | num_sections++; | |
d24928c0 | 1526 | #ifdef DEBUG |
e621c5cc ILT |
1527 | fprintf (stderr, |
1528 | "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n", | |
1529 | asect->name, (long) asect->vma, asect->index, (long) asect); | |
d24928c0 | 1530 | #endif |
e621c5cc | 1531 | } |
d24928c0 | 1532 | |
e4a4da62 ILT |
1533 | /* Classify all of the symbols. */ |
1534 | for (idx = 0; idx < symcount; idx++) | |
244ffee7 | 1535 | { |
e4a4da62 ILT |
1536 | if (!sym_is_global (abfd, syms[idx])) |
1537 | num_locals++; | |
32090b8e | 1538 | else |
e4a4da62 ILT |
1539 | num_globals++; |
1540 | } | |
1541 | for (asect = abfd->sections; asect; asect = asect->next) | |
1542 | { | |
1543 | if (sect_syms[asect->index] != NULL | |
1544 | && sect_syms[asect->index]->flags == 0) | |
32090b8e | 1545 | { |
e4a4da62 ILT |
1546 | sect_syms[asect->index]->flags = BSF_SECTION_SYM; |
1547 | if (!sym_is_global (abfd, sect_syms[asect->index])) | |
1548 | num_locals++; | |
1549 | else | |
1550 | num_globals++; | |
1551 | sect_syms[asect->index]->flags = 0; | |
32090b8e | 1552 | } |
32090b8e | 1553 | } |
244ffee7 | 1554 | |
e4a4da62 ILT |
1555 | /* Now sort the symbols so the local symbols are first. */ |
1556 | new_syms = ((asymbol **) | |
1557 | bfd_alloc (abfd, | |
1558 | (num_locals + num_globals) * sizeof (asymbol *))); | |
1559 | if (new_syms == NULL) | |
9783e04a | 1560 | { |
d1ad85a6 | 1561 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
1562 | return false; |
1563 | } | |
244ffee7 | 1564 | |
32090b8e | 1565 | for (idx = 0; idx < symcount; idx++) |
244ffee7 | 1566 | { |
e4a4da62 ILT |
1567 | asymbol *sym = syms[idx]; |
1568 | int i; | |
1569 | ||
1570 | if (!sym_is_global (abfd, sym)) | |
1571 | i = num_locals2++; | |
32090b8e | 1572 | else |
e4a4da62 ILT |
1573 | i = num_locals + num_globals2++; |
1574 | new_syms[i] = sym; | |
1575 | sym->udata.i = i + 1; | |
244ffee7 | 1576 | } |
e4a4da62 | 1577 | for (asect = abfd->sections; asect; asect = asect->next) |
244ffee7 | 1578 | { |
e4a4da62 ILT |
1579 | if (sect_syms[asect->index] != NULL |
1580 | && sect_syms[asect->index]->flags == 0) | |
1581 | { | |
1582 | asymbol *sym = sect_syms[asect->index]; | |
1583 | int i; | |
1584 | ||
1585 | sym->flags = BSF_SECTION_SYM; | |
1586 | if (!sym_is_global (abfd, sym)) | |
1587 | i = num_locals2++; | |
1588 | else | |
1589 | i = num_locals + num_globals2++; | |
1590 | new_syms[i] = sym; | |
1591 | sym->udata.i = i + 1; | |
1592 | } | |
244ffee7 JK |
1593 | } |
1594 | ||
e4a4da62 ILT |
1595 | bfd_set_symtab (abfd, new_syms, num_locals + num_globals); |
1596 | ||
32090b8e KR |
1597 | elf_num_locals (abfd) = num_locals; |
1598 | elf_num_globals (abfd) = num_globals; | |
9783e04a | 1599 | return true; |
32090b8e | 1600 | } |
244ffee7 | 1601 | |
6ec3bb6a ILT |
1602 | /* Compute the file positions we are going to put the sections at, and |
1603 | otherwise prepare to begin writing out the ELF file. If LINK_INFO | |
1604 | is not NULL, this is being called by the ELF backend linker. */ | |
244ffee7 | 1605 | |
32090b8e | 1606 | static boolean |
6ec3bb6a | 1607 | elf_compute_section_file_positions (abfd, link_info) |
1c6042ee | 1608 | bfd *abfd; |
6ec3bb6a | 1609 | struct bfd_link_info *link_info; |
32090b8e | 1610 | { |
6ec3bb6a | 1611 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
eb4267a3 ILT |
1612 | boolean failed; |
1613 | struct bfd_strtab_hash *strtab; | |
6ec3bb6a ILT |
1614 | Elf_Internal_Shdr *shstrtab_hdr; |
1615 | ||
1616 | if (abfd->output_has_begun) | |
1617 | return true; | |
1618 | ||
1619 | /* Do any elf backend specific processing first. */ | |
1620 | if (bed->elf_backend_begin_write_processing) | |
71edd06d | 1621 | (*bed->elf_backend_begin_write_processing) (abfd, link_info); |
6ec3bb6a ILT |
1622 | |
1623 | if (! prep_headers (abfd)) | |
1624 | return false; | |
1625 | ||
eb4267a3 ILT |
1626 | failed = false; |
1627 | bfd_map_over_sections (abfd, elf_fake_sections, &failed); | |
1628 | if (failed) | |
1629 | return false; | |
244ffee7 | 1630 | |
9783e04a DM |
1631 | if (!assign_section_numbers (abfd)) |
1632 | return false; | |
244ffee7 | 1633 | |
6ec3bb6a ILT |
1634 | /* The backend linker builds symbol table information itself. */ |
1635 | if (link_info == NULL) | |
1636 | { | |
eb4267a3 | 1637 | if (! swap_out_syms (abfd, &strtab)) |
6ec3bb6a ILT |
1638 | return false; |
1639 | } | |
244ffee7 | 1640 | |
6ec3bb6a ILT |
1641 | shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; |
1642 | /* sh_name was set in prep_headers. */ | |
1643 | shstrtab_hdr->sh_type = SHT_STRTAB; | |
1644 | shstrtab_hdr->sh_flags = 0; | |
1645 | shstrtab_hdr->sh_addr = 0; | |
eb4267a3 | 1646 | shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); |
6ec3bb6a ILT |
1647 | shstrtab_hdr->sh_entsize = 0; |
1648 | shstrtab_hdr->sh_link = 0; | |
1649 | shstrtab_hdr->sh_info = 0; | |
1650 | /* sh_offset is set in assign_file_positions_for_symtabs_and_strtabs. */ | |
1651 | shstrtab_hdr->sh_addralign = 1; | |
6ec3bb6a ILT |
1652 | |
1653 | if (!assign_file_positions_except_relocs (abfd, | |
1654 | link_info == NULL ? true : false)) | |
9783e04a | 1655 | return false; |
32090b8e | 1656 | |
eb4267a3 ILT |
1657 | if (link_info == NULL) |
1658 | { | |
1659 | /* Now that we know where the .strtab section goes, write it | |
1660 | out. */ | |
1661 | if ((bfd_seek (abfd, elf_tdata (abfd)->strtab_hdr.sh_offset, SEEK_SET) | |
1662 | != 0) | |
1663 | || ! _bfd_stringtab_emit (abfd, strtab)) | |
1664 | return false; | |
1665 | _bfd_stringtab_free (strtab); | |
1666 | } | |
1667 | ||
6ec3bb6a ILT |
1668 | abfd->output_has_begun = true; |
1669 | ||
32090b8e KR |
1670 | return true; |
1671 | } | |
1672 | ||
244ffee7 | 1673 | |
013dec1a ILT |
1674 | /* Align to the maximum file alignment that could be required for any |
1675 | ELF data structure. */ | |
1676 | ||
1677 | static INLINE file_ptr | |
1678 | align_file_position (off) | |
1679 | file_ptr off; | |
1680 | { | |
1681 | return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1); | |
1682 | } | |
1683 | ||
1684 | /* Assign a file position to a section, optionally aligning to the | |
1685 | required section alignment. */ | |
1686 | ||
32090b8e | 1687 | static INLINE file_ptr |
013dec1a | 1688 | assign_file_position_for_section (i_shdrp, offset, align) |
32090b8e KR |
1689 | Elf_Internal_Shdr *i_shdrp; |
1690 | file_ptr offset; | |
013dec1a | 1691 | boolean align; |
32090b8e | 1692 | { |
013dec1a ILT |
1693 | if (align) |
1694 | { | |
1695 | unsigned int al; | |
f035cc47 | 1696 | |
013dec1a ILT |
1697 | al = i_shdrp->sh_addralign; |
1698 | if (al > 1) | |
1699 | offset = BFD_ALIGN (offset, al); | |
1700 | } | |
1701 | i_shdrp->sh_offset = offset; | |
24f13b03 ILT |
1702 | if (i_shdrp->bfd_section != NULL) |
1703 | i_shdrp->bfd_section->filepos = offset; | |
300adb31 KR |
1704 | if (i_shdrp->sh_type != SHT_NOBITS) |
1705 | offset += i_shdrp->sh_size; | |
32090b8e | 1706 | return offset; |
244ffee7 JK |
1707 | } |
1708 | ||
013dec1a ILT |
1709 | /* Get the size of the program header. This is called by the linker |
1710 | before any of the section VMA's are set, so it can't calculate the | |
1711 | correct value for a strange memory layout. */ | |
01383fb4 | 1712 | |
013dec1a ILT |
1713 | static bfd_size_type |
1714 | get_program_header_size (abfd) | |
300adb31 | 1715 | bfd *abfd; |
300adb31 | 1716 | { |
013dec1a ILT |
1717 | size_t segs; |
1718 | asection *s; | |
300adb31 | 1719 | |
013dec1a ILT |
1720 | /* Assume we will need exactly two PT_LOAD segments: one for text |
1721 | and one for data. */ | |
1722 | segs = 2; | |
1723 | ||
1724 | s = bfd_get_section_by_name (abfd, ".interp"); | |
1725 | if (s != NULL && (s->flags & SEC_LOAD) != 0) | |
1726 | { | |
1727 | /* If we have a loadable interpreter section, we need a | |
1728 | PT_INTERP segment. In this case, assume we also need a | |
1729 | PT_PHDR segment, although that may not be true for all | |
1730 | targets. */ | |
1731 | segs += 2; | |
1732 | } | |
1733 | ||
1734 | if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) | |
1735 | { | |
1736 | /* We need a PT_DYNAMIC segment. */ | |
1737 | ++segs; | |
1738 | } | |
1739 | ||
1740 | return segs * sizeof (Elf_External_Phdr); | |
300adb31 KR |
1741 | } |
1742 | ||
013dec1a ILT |
1743 | /* Create the program header. OFF is the file offset where the |
1744 | program header should be written. FIRST is the first loadable ELF | |
1745 | section. PHDR_SIZE is the size of the program header as returned | |
1746 | by get_program_header_size. */ | |
300adb31 | 1747 | |
013dec1a ILT |
1748 | static file_ptr |
1749 | map_program_segments (abfd, off, first, phdr_size) | |
300adb31 | 1750 | bfd *abfd; |
013dec1a ILT |
1751 | file_ptr off; |
1752 | Elf_Internal_Shdr *first; | |
1753 | bfd_size_type phdr_size; | |
300adb31 | 1754 | { |
6731b89c | 1755 | Elf_Internal_Phdr phdrs[10]; |
013dec1a | 1756 | unsigned int phdr_count; |
300adb31 | 1757 | Elf_Internal_Phdr *phdr; |
013dec1a ILT |
1758 | int phdr_size_adjust; |
1759 | unsigned int i; | |
1760 | Elf_Internal_Shdr **hdrpp; | |
1761 | asection *sinterp, *sdyn; | |
1762 | unsigned int last_type; | |
1763 | Elf_Internal_Ehdr *i_ehdrp; | |
1764 | ||
df9e066f | 1765 | BFD_ASSERT ((abfd->flags & (EXEC_P | DYNAMIC)) != 0); |
6731b89c ILT |
1766 | BFD_ASSERT (phdr_size / sizeof (Elf_Internal_Phdr) |
1767 | <= sizeof phdrs / sizeof (phdrs[0])); | |
013dec1a ILT |
1768 | |
1769 | phdr_count = 0; | |
1770 | phdr = phdrs; | |
1771 | ||
1772 | phdr_size_adjust = 0; | |
300adb31 | 1773 | |
013dec1a ILT |
1774 | /* If we have a loadable .interp section, we must create a PT_INTERP |
1775 | segment which must precede all PT_LOAD segments. We assume that | |
1776 | we must also create a PT_PHDR segment, although that may not be | |
1777 | true for all targets. */ | |
1778 | sinterp = bfd_get_section_by_name (abfd, ".interp"); | |
1779 | if (sinterp != NULL && (sinterp->flags & SEC_LOAD) != 0) | |
80425e6c | 1780 | { |
013dec1a ILT |
1781 | BFD_ASSERT (first != NULL); |
1782 | ||
1783 | phdr->p_type = PT_PHDR; | |
1784 | ||
1785 | phdr->p_offset = off; | |
1786 | ||
1787 | /* Account for any adjustment made because of the alignment of | |
1788 | the first loadable section. */ | |
1789 | phdr_size_adjust = (first->sh_offset - phdr_size) - off; | |
1790 | BFD_ASSERT (phdr_size_adjust >= 0 && phdr_size_adjust < 128); | |
1791 | ||
1792 | /* The program header precedes all loadable sections. This lets | |
1793 | us compute its loadable address. This depends on the linker | |
1794 | script. */ | |
1795 | phdr->p_vaddr = first->sh_addr - (phdr_size + phdr_size_adjust); | |
1796 | ||
1797 | phdr->p_paddr = 0; | |
1798 | phdr->p_filesz = phdr_size; | |
1799 | phdr->p_memsz = phdr_size; | |
1800 | ||
1801 | /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ | |
1802 | phdr->p_flags = PF_R | PF_X; | |
1803 | ||
1804 | phdr->p_align = FILE_ALIGN; | |
1805 | BFD_ASSERT ((phdr->p_vaddr - phdr->p_offset) % FILE_ALIGN == 0); | |
1806 | ||
1807 | /* Include the ELF header in the first loadable segment. */ | |
1808 | phdr_size_adjust += off; | |
1809 | ||
1810 | ++phdr_count; | |
1811 | ++phdr; | |
1812 | ||
1813 | phdr->p_type = PT_INTERP; | |
1814 | phdr->p_offset = sinterp->filepos; | |
1815 | phdr->p_vaddr = sinterp->vma; | |
1816 | phdr->p_paddr = 0; | |
1817 | phdr->p_filesz = sinterp->_raw_size; | |
1818 | phdr->p_memsz = sinterp->_raw_size; | |
1819 | phdr->p_flags = PF_R; | |
1820 | phdr->p_align = 1 << bfd_get_section_alignment (abfd, sinterp); | |
1821 | ||
1822 | ++phdr_count; | |
1823 | ++phdr; | |
80425e6c | 1824 | } |
013dec1a ILT |
1825 | |
1826 | /* Look through the sections to see how they will be divided into | |
1827 | program segments. The sections must be arranged in order by | |
1828 | sh_addr for this to work correctly. */ | |
1829 | phdr->p_type = PT_NULL; | |
1830 | last_type = SHT_PROGBITS; | |
1831 | for (i = 1, hdrpp = elf_elfsections (abfd) + 1; | |
1832 | i < elf_elfheader (abfd)->e_shnum; | |
1833 | i++, hdrpp++) | |
300adb31 | 1834 | { |
013dec1a ILT |
1835 | Elf_Internal_Shdr *hdr; |
1836 | ||
1837 | hdr = *hdrpp; | |
1838 | ||
1839 | /* Ignore any section which will not be part of the process | |
1840 | image. */ | |
1841 | if ((hdr->sh_flags & SHF_ALLOC) == 0) | |
1842 | continue; | |
1843 | ||
1844 | /* If this section fits in the segment we are constructing, add | |
1845 | it in. */ | |
1846 | if (phdr->p_type != PT_NULL | |
1847 | && (hdr->sh_offset - (phdr->p_offset + phdr->p_memsz) | |
1848 | == hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz)) | |
1849 | && (last_type != SHT_NOBITS || hdr->sh_type == SHT_NOBITS)) | |
300adb31 | 1850 | { |
013dec1a ILT |
1851 | bfd_size_type adjust; |
1852 | ||
1853 | adjust = hdr->sh_addr - (phdr->p_vaddr + phdr->p_memsz); | |
1854 | phdr->p_memsz += hdr->sh_size + adjust; | |
1855 | if (hdr->sh_type != SHT_NOBITS) | |
1856 | phdr->p_filesz += hdr->sh_size + adjust; | |
1857 | if ((hdr->sh_flags & SHF_WRITE) != 0) | |
1858 | phdr->p_flags |= PF_W; | |
1859 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1860 | phdr->p_flags |= PF_X; | |
1861 | last_type = hdr->sh_type; | |
300adb31 KR |
1862 | continue; |
1863 | } | |
300adb31 | 1864 | |
013dec1a ILT |
1865 | /* If we have a segment, move to the next one. */ |
1866 | if (phdr->p_type != PT_NULL) | |
300adb31 | 1867 | { |
013dec1a ILT |
1868 | ++phdr; |
1869 | ++phdr_count; | |
300adb31 | 1870 | } |
013dec1a ILT |
1871 | |
1872 | /* Start a new segment. */ | |
1873 | phdr->p_type = PT_LOAD; | |
1874 | phdr->p_offset = hdr->sh_offset; | |
1875 | phdr->p_vaddr = hdr->sh_addr; | |
1876 | phdr->p_paddr = 0; | |
1877 | if (hdr->sh_type == SHT_NOBITS) | |
1878 | phdr->p_filesz = 0; | |
1879 | else | |
1880 | phdr->p_filesz = hdr->sh_size; | |
1881 | phdr->p_memsz = hdr->sh_size; | |
1882 | phdr->p_flags = PF_R; | |
1883 | if ((hdr->sh_flags & SHF_WRITE) != 0) | |
1884 | phdr->p_flags |= PF_W; | |
1885 | if ((hdr->sh_flags & SHF_EXECINSTR) != 0) | |
1886 | phdr->p_flags |= PF_X; | |
1887 | phdr->p_align = get_elf_backend_data (abfd)->maxpagesize; | |
1888 | ||
1889 | if (hdr == first | |
1890 | && sinterp != NULL | |
1891 | && (sinterp->flags & SEC_LOAD) != 0) | |
2f3189e7 | 1892 | { |
013dec1a ILT |
1893 | phdr->p_offset -= phdr_size + phdr_size_adjust; |
1894 | phdr->p_vaddr -= phdr_size + phdr_size_adjust; | |
1895 | phdr->p_filesz += phdr_size + phdr_size_adjust; | |
1896 | phdr->p_memsz += phdr_size + phdr_size_adjust; | |
2f3189e7 | 1897 | } |
300adb31 | 1898 | |
013dec1a | 1899 | last_type = hdr->sh_type; |
300adb31 | 1900 | } |
300adb31 | 1901 | |
013dec1a ILT |
1902 | if (phdr->p_type != PT_NULL) |
1903 | { | |
1904 | ++phdr; | |
1905 | ++phdr_count; | |
1906 | } | |
1907 | ||
1908 | /* If we have a .dynamic section, create a PT_DYNAMIC segment. */ | |
1909 | sdyn = bfd_get_section_by_name (abfd, ".dynamic"); | |
1910 | if (sdyn != NULL && (sdyn->flags & SEC_LOAD) != 0) | |
1911 | { | |
1912 | phdr->p_type = PT_DYNAMIC; | |
1913 | phdr->p_offset = sdyn->filepos; | |
1914 | phdr->p_vaddr = sdyn->vma; | |
1915 | phdr->p_paddr = 0; | |
1916 | phdr->p_filesz = sdyn->_raw_size; | |
1917 | phdr->p_memsz = sdyn->_raw_size; | |
1918 | phdr->p_flags = PF_R; | |
1919 | if ((sdyn->flags & SEC_READONLY) == 0) | |
1920 | phdr->p_flags |= PF_W; | |
1921 | if ((sdyn->flags & SEC_CODE) != 0) | |
1922 | phdr->p_flags |= PF_X; | |
1923 | phdr->p_align = 1 << bfd_get_section_alignment (abfd, sdyn); | |
1924 | ||
1925 | ++phdr; | |
1926 | ++phdr_count; | |
1927 | } | |
1928 | ||
013dec1a | 1929 | /* Make sure the return value from get_program_header_size matches |
57b40081 ILT |
1930 | what we computed here. Actually, it's OK if we allocated too |
1931 | much space in the program header. */ | |
1932 | if (phdr_count > phdr_size / sizeof (Elf_External_Phdr)) | |
013dec1a ILT |
1933 | abort (); |
1934 | ||
1935 | /* Set up program header information. */ | |
1936 | i_ehdrp = elf_elfheader (abfd); | |
1937 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
1938 | i_ehdrp->e_phoff = off; | |
1939 | i_ehdrp->e_phnum = phdr_count; | |
1940 | ||
1941 | /* Save the program headers away. I don't think anybody uses this | |
1942 | information right now. */ | |
1943 | elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *) | |
1944 | bfd_alloc (abfd, | |
1945 | (phdr_count | |
1946 | * sizeof (Elf_Internal_Phdr)))); | |
1947 | if (elf_tdata (abfd)->phdr == NULL && phdr_count != 0) | |
1948 | { | |
1949 | bfd_set_error (bfd_error_no_memory); | |
1950 | return (file_ptr) -1; | |
1951 | } | |
1952 | memcpy (elf_tdata (abfd)->phdr, phdrs, | |
1953 | phdr_count * sizeof (Elf_Internal_Phdr)); | |
1954 | ||
1955 | /* Write out the program headers. */ | |
1956 | if (bfd_seek (abfd, off, SEEK_SET) != 0) | |
1957 | return (file_ptr) -1; | |
1958 | ||
1959 | for (i = 0, phdr = phdrs; i < phdr_count; i++, phdr++) | |
1960 | { | |
1961 | Elf_External_Phdr extphdr; | |
1962 | ||
1963 | elf_swap_phdr_out (abfd, phdr, &extphdr); | |
1964 | if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), 1, abfd) | |
1965 | != sizeof (Elf_External_Phdr)) | |
1966 | return (file_ptr) -1; | |
1967 | } | |
1968 | ||
1969 | return off + phdr_count * sizeof (Elf_External_Phdr); | |
300adb31 KR |
1970 | } |
1971 | ||
013dec1a ILT |
1972 | /* Work out the file positions of all the sections. This is called by |
1973 | elf_compute_section_file_positions. All the section sizes and VMAs | |
1974 | must be known before this is called. | |
1975 | ||
1976 | We do not consider reloc sections at this point, unless they form | |
1977 | part of the loadable image. Reloc sections are assigned file | |
1978 | positions in assign_file_positions_for_relocs, which is called by | |
1979 | write_object_contents and final_link. | |
1980 | ||
1981 | If DOSYMS is false, we do not assign file positions for the symbol | |
1982 | table or the string table. */ | |
1983 | ||
9783e04a | 1984 | static boolean |
6ec3bb6a | 1985 | assign_file_positions_except_relocs (abfd, dosyms) |
32090b8e | 1986 | bfd *abfd; |
6ec3bb6a | 1987 | boolean dosyms; |
244ffee7 | 1988 | { |
013dec1a ILT |
1989 | struct elf_obj_tdata * const tdata = elf_tdata (abfd); |
1990 | Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); | |
1991 | Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); | |
32090b8e | 1992 | file_ptr off; |
32090b8e | 1993 | |
013dec1a | 1994 | /* Start after the ELF header. */ |
32090b8e | 1995 | off = i_ehdrp->e_ehsize; |
300adb31 | 1996 | |
df9e066f | 1997 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) |
300adb31 | 1998 | { |
013dec1a ILT |
1999 | Elf_Internal_Shdr **hdrpp; |
2000 | unsigned int i; | |
062189c6 | 2001 | |
013dec1a ILT |
2002 | /* We are not creating an executable, which means that we are |
2003 | not creating a program header, and that the actual order of | |
2004 | the sections in the file is unimportant. */ | |
2005 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
244ffee7 | 2006 | { |
013dec1a ILT |
2007 | Elf_Internal_Shdr *hdr; |
2008 | ||
2009 | hdr = *hdrpp; | |
2010 | if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
300adb31 | 2011 | { |
013dec1a | 2012 | hdr->sh_offset = -1; |
300adb31 KR |
2013 | continue; |
2014 | } | |
013dec1a ILT |
2015 | if (! dosyms |
2016 | && (i == tdata->symtab_section | |
2017 | || i == tdata->strtab_section)) | |
300adb31 | 2018 | { |
013dec1a ILT |
2019 | hdr->sh_offset = -1; |
2020 | continue; | |
300adb31 | 2021 | } |
013dec1a ILT |
2022 | |
2023 | off = assign_file_position_for_section (hdr, off, true); | |
300adb31 | 2024 | } |
300adb31 | 2025 | } |
013dec1a | 2026 | else |
300adb31 | 2027 | { |
013dec1a ILT |
2028 | file_ptr phdr_off; |
2029 | bfd_size_type phdr_size; | |
2030 | bfd_vma maxpagesize; | |
2031 | Elf_Internal_Shdr **hdrpp; | |
2032 | unsigned int i; | |
2033 | Elf_Internal_Shdr *first; | |
2034 | file_ptr phdr_map; | |
2035 | ||
2036 | /* We are creating an executable. We must create a program | |
2037 | header. We can't actually create the program header until we | |
2038 | have set the file positions for the sections, but we can | |
2039 | figure out how big it is going to be. */ | |
2040 | off = align_file_position (off); | |
2041 | phdr_size = get_program_header_size (abfd); | |
2042 | if (phdr_size == (file_ptr) -1) | |
9783e04a | 2043 | return false; |
013dec1a ILT |
2044 | phdr_off = off; |
2045 | off += phdr_size; | |
2046 | ||
2047 | maxpagesize = get_elf_backend_data (abfd)->maxpagesize; | |
2048 | if (maxpagesize == 0) | |
2049 | maxpagesize = 1; | |
2050 | ||
2051 | /* FIXME: We might want to sort the sections on the sh_addr | |
2052 | field here. For now, we just assume that the linker will | |
2053 | create the sections in an appropriate order. */ | |
2054 | ||
2055 | /* Assign file positions in two passes. In the first pass, we | |
2056 | assign a file position to every section which forms part of | |
2057 | the executable image. */ | |
2058 | first = NULL; | |
2059 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
2060 | { | |
2061 | Elf_Internal_Shdr *hdr; | |
300adb31 | 2062 | |
013dec1a ILT |
2063 | hdr = *hdrpp; |
2064 | if ((hdr->sh_flags & SHF_ALLOC) == 0) | |
2065 | continue; | |
2066 | ||
2067 | if (first == NULL) | |
2068 | first = hdr; | |
2069 | ||
64e21e0f KR |
2070 | /* The section VMA must equal the file position modulo the |
2071 | page size. This is required by the program header. */ | |
2072 | off += (hdr->sh_addr - off) % maxpagesize; | |
300adb31 | 2073 | |
013dec1a ILT |
2074 | off = assign_file_position_for_section (hdr, off, false); |
2075 | } | |
300adb31 | 2076 | |
013dec1a ILT |
2077 | /* Assign file positions to all the sections which do not form |
2078 | part of the loadable image, except for the relocs. */ | |
2079 | for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) | |
300adb31 | 2080 | { |
013dec1a ILT |
2081 | Elf_Internal_Shdr *hdr; |
2082 | ||
2083 | hdr = *hdrpp; | |
2084 | if ((hdr->sh_flags & SHF_ALLOC) != 0) | |
2085 | continue; | |
2086 | if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) | |
2087 | { | |
2088 | hdr->sh_offset = -1; | |
2089 | continue; | |
2090 | } | |
2091 | if (! dosyms | |
2092 | && (i == tdata->symtab_section | |
2093 | || i == tdata->strtab_section)) | |
2094 | { | |
2095 | hdr->sh_offset = -1; | |
2096 | continue; | |
2097 | } | |
2098 | ||
2099 | off = assign_file_position_for_section (hdr, off, true); | |
300adb31 | 2100 | } |
013dec1a ILT |
2101 | |
2102 | phdr_map = map_program_segments (abfd, phdr_off, first, phdr_size); | |
2103 | if (phdr_map == (file_ptr) -1) | |
2104 | return false; | |
0d9e1397 | 2105 | BFD_ASSERT (phdr_map <= phdr_off + phdr_size); |
244ffee7 | 2106 | } |
013dec1a ILT |
2107 | |
2108 | /* Place the section headers. */ | |
2109 | off = align_file_position (off); | |
2110 | i_ehdrp->e_shoff = off; | |
2111 | off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; | |
2112 | ||
32090b8e | 2113 | elf_tdata (abfd)->next_file_pos = off; |
013dec1a | 2114 | |
9783e04a | 2115 | return true; |
244ffee7 JK |
2116 | } |
2117 | ||
32090b8e KR |
2118 | static boolean |
2119 | prep_headers (abfd) | |
2120 | bfd *abfd; | |
2121 | { | |
32090b8e | 2122 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ |
1c6042ee | 2123 | Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ |
32090b8e | 2124 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ |
32090b8e | 2125 | int count; |
eb4267a3 | 2126 | struct bfd_strtab_hash *shstrtab; |
244ffee7 | 2127 | |
32090b8e KR |
2128 | i_ehdrp = elf_elfheader (abfd); |
2129 | i_shdrp = elf_elfsections (abfd); | |
244ffee7 | 2130 | |
eb4267a3 ILT |
2131 | shstrtab = elf_stringtab_init (); |
2132 | if (shstrtab == NULL) | |
b9d5cdf0 | 2133 | return false; |
1c6042ee | 2134 | |
32090b8e | 2135 | elf_shstrtab (abfd) = shstrtab; |
244ffee7 | 2136 | |
32090b8e KR |
2137 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; |
2138 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
2139 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
2140 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
244ffee7 | 2141 | |
32090b8e KR |
2142 | i_ehdrp->e_ident[EI_CLASS] = ELFCLASS; |
2143 | i_ehdrp->e_ident[EI_DATA] = | |
2144 | abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB; | |
2145 | i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT; | |
244ffee7 | 2146 | |
32090b8e KR |
2147 | for (count = EI_PAD; count < EI_NIDENT; count++) |
2148 | i_ehdrp->e_ident[count] = 0; | |
244ffee7 | 2149 | |
8af74670 ILT |
2150 | if ((abfd->flags & DYNAMIC) != 0) |
2151 | i_ehdrp->e_type = ET_DYN; | |
2152 | else if ((abfd->flags & EXEC_P) != 0) | |
2153 | i_ehdrp->e_type = ET_EXEC; | |
2154 | else | |
2155 | i_ehdrp->e_type = ET_REL; | |
2156 | ||
32090b8e | 2157 | switch (bfd_get_arch (abfd)) |
fce36137 | 2158 | { |
32090b8e KR |
2159 | case bfd_arch_unknown: |
2160 | i_ehdrp->e_machine = EM_NONE; | |
2161 | break; | |
2162 | case bfd_arch_sparc: | |
32090b8e KR |
2163 | #if ARCH_SIZE == 64 |
2164 | i_ehdrp->e_machine = EM_SPARC64; | |
5546cc7e KR |
2165 | #else |
2166 | i_ehdrp->e_machine = EM_SPARC; | |
32090b8e | 2167 | #endif |
32090b8e KR |
2168 | break; |
2169 | case bfd_arch_i386: | |
2170 | i_ehdrp->e_machine = EM_386; | |
2171 | break; | |
2172 | case bfd_arch_m68k: | |
2173 | i_ehdrp->e_machine = EM_68K; | |
2174 | break; | |
2175 | case bfd_arch_m88k: | |
2176 | i_ehdrp->e_machine = EM_88K; | |
2177 | break; | |
2178 | case bfd_arch_i860: | |
2179 | i_ehdrp->e_machine = EM_860; | |
2180 | break; | |
2181 | case bfd_arch_mips: /* MIPS Rxxxx */ | |
2182 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
2183 | break; | |
2184 | case bfd_arch_hppa: | |
459ae909 | 2185 | i_ehdrp->e_machine = EM_PARISC; |
32090b8e | 2186 | break; |
99ec1f66 ILT |
2187 | case bfd_arch_powerpc: |
2188 | i_ehdrp->e_machine = EM_CYGNUS_POWERPC; | |
2189 | break; | |
32090b8e KR |
2190 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ |
2191 | default: | |
2192 | i_ehdrp->e_machine = EM_NONE; | |
fce36137 | 2193 | } |
32090b8e KR |
2194 | i_ehdrp->e_version = EV_CURRENT; |
2195 | i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr); | |
244ffee7 | 2196 | |
32090b8e KR |
2197 | /* no program header, for now. */ |
2198 | i_ehdrp->e_phoff = 0; | |
2199 | i_ehdrp->e_phentsize = 0; | |
2200 | i_ehdrp->e_phnum = 0; | |
244ffee7 | 2201 | |
32090b8e KR |
2202 | /* each bfd section is section header entry */ |
2203 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
2204 | i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr); | |
244ffee7 | 2205 | |
32090b8e KR |
2206 | /* if we're building an executable, we'll need a program header table */ |
2207 | if (abfd->flags & EXEC_P) | |
244ffee7 | 2208 | { |
300adb31 | 2209 | /* it all happens later */ |
32090b8e KR |
2210 | #if 0 |
2211 | i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); | |
244ffee7 | 2212 | |
32090b8e KR |
2213 | /* elf_build_phdrs() returns a (NULL-terminated) array of |
2214 | Elf_Internal_Phdrs */ | |
2215 | i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
2216 | i_ehdrp->e_phoff = outbase; | |
2217 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; | |
2218 | #endif | |
244ffee7 | 2219 | } |
32090b8e | 2220 | else |
244ffee7 | 2221 | { |
32090b8e KR |
2222 | i_ehdrp->e_phentsize = 0; |
2223 | i_phdrp = 0; | |
2224 | i_ehdrp->e_phoff = 0; | |
244ffee7 JK |
2225 | } |
2226 | ||
eb4267a3 ILT |
2227 | elf_tdata (abfd)->symtab_hdr.sh_name = |
2228 | (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false); | |
2229 | elf_tdata (abfd)->strtab_hdr.sh_name = | |
2230 | (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false); | |
2231 | elf_tdata (abfd)->shstrtab_hdr.sh_name = | |
2232 | (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false); | |
6ec3bb6a ILT |
2233 | if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 |
2234 | || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 | |
2235 | || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) | |
2236 | return false; | |
2237 | ||
f035cc47 | 2238 | return true; |
244ffee7 JK |
2239 | } |
2240 | ||
b9d5cdf0 | 2241 | static boolean |
eb4267a3 | 2242 | swap_out_syms (abfd, sttp) |
32090b8e | 2243 | bfd *abfd; |
eb4267a3 | 2244 | struct bfd_strtab_hash **sttp; |
244ffee7 | 2245 | { |
9783e04a DM |
2246 | if (!elf_map_symbols (abfd)) |
2247 | return false; | |
244ffee7 | 2248 | |
32090b8e KR |
2249 | /* Dump out the symtabs. */ |
2250 | { | |
2251 | int symcount = bfd_get_symcount (abfd); | |
2252 | asymbol **syms = bfd_get_outsymbols (abfd); | |
eb4267a3 | 2253 | struct bfd_strtab_hash *stt; |
32090b8e KR |
2254 | Elf_Internal_Shdr *symtab_hdr; |
2255 | Elf_Internal_Shdr *symstrtab_hdr; | |
2256 | Elf_External_Sym *outbound_syms; | |
2257 | int idx; | |
244ffee7 | 2258 | |
eb4267a3 ILT |
2259 | stt = elf_stringtab_init (); |
2260 | if (stt == NULL) | |
b9d5cdf0 | 2261 | return false; |
eb4267a3 | 2262 | |
32090b8e KR |
2263 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
2264 | symtab_hdr->sh_type = SHT_SYMTAB; | |
2265 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
2266 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); | |
2267 | symtab_hdr->sh_info = elf_num_locals (abfd) + 1; | |
fa15568a | 2268 | symtab_hdr->sh_addralign = FILE_ALIGN; |
244ffee7 | 2269 | |
32090b8e KR |
2270 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; |
2271 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
244ffee7 | 2272 | |
e4a4da62 ILT |
2273 | outbound_syms = ((Elf_External_Sym *) |
2274 | bfd_alloc (abfd, | |
2275 | (1 + symcount) * sizeof (Elf_External_Sym))); | |
2276 | if (outbound_syms == NULL) | |
9783e04a | 2277 | { |
d1ad85a6 | 2278 | bfd_set_error (bfd_error_no_memory); |
9783e04a DM |
2279 | return false; |
2280 | } | |
e4a4da62 ILT |
2281 | symtab_hdr->contents = (PTR) outbound_syms; |
2282 | ||
32090b8e KR |
2283 | /* now generate the data (for "contents") */ |
2284 | { | |
2285 | /* Fill in zeroth symbol and swap it out. */ | |
2286 | Elf_Internal_Sym sym; | |
2287 | sym.st_name = 0; | |
2288 | sym.st_value = 0; | |
2289 | sym.st_size = 0; | |
2290 | sym.st_info = 0; | |
2291 | sym.st_other = 0; | |
2292 | sym.st_shndx = SHN_UNDEF; | |
2293 | elf_swap_symbol_out (abfd, &sym, outbound_syms); | |
e4a4da62 | 2294 | ++outbound_syms; |
244ffee7 | 2295 | } |
32090b8e KR |
2296 | for (idx = 0; idx < symcount; idx++) |
2297 | { | |
2298 | Elf_Internal_Sym sym; | |
2299 | bfd_vma value = syms[idx]->value; | |
71edd06d | 2300 | elf_symbol_type *type_ptr; |
244ffee7 | 2301 | |
32090b8e KR |
2302 | if (syms[idx]->flags & BSF_SECTION_SYM) |
2303 | /* Section symbols have no names. */ | |
2304 | sym.st_name = 0; | |
2305 | else | |
6ec3bb6a | 2306 | { |
eb4267a3 ILT |
2307 | sym.st_name = (unsigned long) _bfd_stringtab_add (stt, |
2308 | syms[idx]->name, | |
2309 | true, false); | |
6ec3bb6a ILT |
2310 | if (sym.st_name == (unsigned long) -1) |
2311 | return false; | |
2312 | } | |
244ffee7 | 2313 | |
71edd06d ILT |
2314 | type_ptr = elf_symbol_from (abfd, syms[idx]); |
2315 | ||
32090b8e | 2316 | if (bfd_is_com_section (syms[idx]->section)) |
244ffee7 | 2317 | { |
32090b8e KR |
2318 | /* ELF common symbols put the alignment into the `value' field, |
2319 | and the size into the `size' field. This is backwards from | |
2320 | how BFD handles it, so reverse it here. */ | |
2321 | sym.st_size = value; | |
4c032270 KR |
2322 | if (type_ptr == NULL |
2323 | || type_ptr->internal_elf_sym.st_value == 0) | |
2324 | sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); | |
2325 | else | |
2326 | sym.st_value = type_ptr->internal_elf_sym.st_value; | |
d4fb8fce ILT |
2327 | sym.st_shndx = elf_section_from_bfd_section (abfd, |
2328 | syms[idx]->section); | |
244ffee7 JK |
2329 | } |
2330 | else | |
2331 | { | |
32090b8e KR |
2332 | asection *sec = syms[idx]->section; |
2333 | int shndx; | |
244ffee7 | 2334 | |
32090b8e KR |
2335 | if (sec->output_section) |
2336 | { | |
2337 | value += sec->output_offset; | |
2338 | sec = sec->output_section; | |
2339 | } | |
2340 | value += sec->vma; | |
2341 | sym.st_value = value; | |
e74034d8 | 2342 | sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; |
32090b8e KR |
2343 | sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec); |
2344 | if (shndx == -1) | |
2345 | { | |
2346 | asection *sec2; | |
2347 | /* Writing this would be a hell of a lot easier if we had | |
2348 | some decent documentation on bfd, and knew what to expect | |
2349 | of the library, and what to demand of applications. For | |
2350 | example, it appears that `objcopy' might not set the | |
2351 | section of a symbol to be a section that is actually in | |
2352 | the output file. */ | |
2353 | sec2 = bfd_get_section_by_name (abfd, sec->name); | |
850584ad | 2354 | BFD_ASSERT (sec2 != 0); |
32090b8e | 2355 | sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2); |
850584ad | 2356 | BFD_ASSERT (shndx != -1); |
32090b8e KR |
2357 | } |
2358 | } | |
244ffee7 | 2359 | |
32090b8e | 2360 | if (bfd_is_com_section (syms[idx]->section)) |
38a5f510 | 2361 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT); |
badd23e3 | 2362 | else if (bfd_is_und_section (syms[idx]->section)) |
3c9832f8 ILT |
2363 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, |
2364 | ((syms[idx]->flags & BSF_FUNCTION) | |
2365 | ? STT_FUNC | |
2366 | : STT_NOTYPE)); | |
32090b8e KR |
2367 | else if (syms[idx]->flags & BSF_SECTION_SYM) |
2368 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
2369 | else if (syms[idx]->flags & BSF_FILE) | |
2370 | sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
d24928c0 | 2371 | else |
32090b8e | 2372 | { |
d24928c0 KR |
2373 | int bind = STB_LOCAL; |
2374 | int type = STT_OBJECT; | |
2375 | unsigned int flags = syms[idx]->flags; | |
2376 | ||
2377 | if (flags & BSF_LOCAL) | |
2378 | bind = STB_LOCAL; | |
2379 | else if (flags & BSF_WEAK) | |
2380 | bind = STB_WEAK; | |
2381 | else if (flags & BSF_GLOBAL) | |
2382 | bind = STB_GLOBAL; | |
2383 | ||
2384 | if (flags & BSF_FUNCTION) | |
2385 | type = STT_FUNC; | |
2386 | ||
2387 | sym.st_info = ELF_ST_INFO (bind, type); | |
32090b8e | 2388 | } |
244ffee7 | 2389 | |
32090b8e | 2390 | sym.st_other = 0; |
e4a4da62 ILT |
2391 | elf_swap_symbol_out (abfd, &sym, outbound_syms); |
2392 | ++outbound_syms; | |
32090b8e KR |
2393 | } |
2394 | ||
eb4267a3 ILT |
2395 | *sttp = stt; |
2396 | symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); | |
32090b8e KR |
2397 | symstrtab_hdr->sh_type = SHT_STRTAB; |
2398 | ||
2399 | symstrtab_hdr->sh_flags = 0; | |
2400 | symstrtab_hdr->sh_addr = 0; | |
2401 | symstrtab_hdr->sh_entsize = 0; | |
2402 | symstrtab_hdr->sh_link = 0; | |
2403 | symstrtab_hdr->sh_info = 0; | |
062189c6 | 2404 | symstrtab_hdr->sh_addralign = 1; |
32090b8e KR |
2405 | } |
2406 | ||
b9d5cdf0 | 2407 | return true; |
244ffee7 JK |
2408 | } |
2409 | ||
32090b8e KR |
2410 | static boolean |
2411 | write_shdrs_and_ehdr (abfd) | |
2412 | bfd *abfd; | |
244ffee7 | 2413 | { |
32090b8e KR |
2414 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
2415 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
32090b8e KR |
2416 | Elf_External_Shdr *x_shdrp; /* Section header table, external form */ |
2417 | Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ | |
68241b2b | 2418 | unsigned int count; |
244ffee7 | 2419 | |
32090b8e KR |
2420 | i_ehdrp = elf_elfheader (abfd); |
2421 | i_shdrp = elf_elfsections (abfd); | |
32090b8e KR |
2422 | |
2423 | /* swap the header before spitting it out... */ | |
2424 | ||
2425 | #if DEBUG & 1 | |
2426 | elf_debug_file (i_ehdrp); | |
244ffee7 | 2427 | #endif |
32090b8e | 2428 | elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr); |
4002f18a ILT |
2429 | if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 |
2430 | || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) | |
2431 | != sizeof (x_ehdr))) | |
2432 | return false; | |
244ffee7 | 2433 | |
32090b8e KR |
2434 | /* at this point we've concocted all the ELF sections... */ |
2435 | x_shdrp = (Elf_External_Shdr *) | |
2436 | bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum)); | |
2437 | if (!x_shdrp) | |
2438 | { | |
d1ad85a6 | 2439 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
2440 | return false; |
2441 | } | |
2442 | ||
2443 | for (count = 0; count < i_ehdrp->e_shnum; count++) | |
2444 | { | |
2445 | #if DEBUG & 2 | |
eb4267a3 | 2446 | elf_debug_section (count, i_shdrp[count]); |
244ffee7 | 2447 | #endif |
32090b8e KR |
2448 | elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count); |
2449 | } | |
4002f18a ILT |
2450 | if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0 |
2451 | || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd) | |
d909628b | 2452 | != sizeof (*x_shdrp) * i_ehdrp->e_shnum)) |
4002f18a ILT |
2453 | return false; |
2454 | ||
32090b8e | 2455 | /* need to dump the string table too... */ |
244ffee7 | 2456 | |
32090b8e KR |
2457 | return true; |
2458 | } | |
244ffee7 | 2459 | |
013dec1a ILT |
2460 | /* Assign file positions for all the reloc sections which are not part |
2461 | of the loadable file image. */ | |
2462 | ||
32090b8e KR |
2463 | static void |
2464 | assign_file_positions_for_relocs (abfd) | |
2465 | bfd *abfd; | |
2466 | { | |
013dec1a | 2467 | file_ptr off; |
68241b2b | 2468 | unsigned int i; |
013dec1a ILT |
2469 | Elf_Internal_Shdr **shdrpp; |
2470 | ||
2471 | off = elf_tdata (abfd)->next_file_pos; | |
2472 | ||
2473 | for (i = 1, shdrpp = elf_elfsections (abfd) + 1; | |
2474 | i < elf_elfheader (abfd)->e_shnum; | |
2475 | i++, shdrpp++) | |
32090b8e | 2476 | { |
013dec1a ILT |
2477 | Elf_Internal_Shdr *shdrp; |
2478 | ||
2479 | shdrp = *shdrpp; | |
2480 | if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) | |
2481 | && shdrp->sh_offset == -1) | |
2482 | off = assign_file_position_for_section (shdrp, off, true); | |
32090b8e | 2483 | } |
013dec1a | 2484 | |
1c6042ee | 2485 | elf_tdata (abfd)->next_file_pos = off; |
32090b8e | 2486 | } |
244ffee7 | 2487 | |
32090b8e | 2488 | boolean |
1c6042ee ILT |
2489 | NAME(bfd_elf,write_object_contents) (abfd) |
2490 | bfd *abfd; | |
32090b8e | 2491 | { |
062189c6 | 2492 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
32090b8e KR |
2493 | Elf_Internal_Ehdr *i_ehdrp; |
2494 | Elf_Internal_Shdr **i_shdrp; | |
68241b2b | 2495 | unsigned int count; |
244ffee7 | 2496 | |
6ec3bb6a ILT |
2497 | if (! abfd->output_has_begun |
2498 | && ! elf_compute_section_file_positions (abfd, | |
2499 | (struct bfd_link_info *) NULL)) | |
2500 | return false; | |
244ffee7 | 2501 | |
32090b8e KR |
2502 | i_shdrp = elf_elfsections (abfd); |
2503 | i_ehdrp = elf_elfheader (abfd); | |
244ffee7 | 2504 | |
32090b8e | 2505 | bfd_map_over_sections (abfd, write_relocs, (PTR) 0); |
32090b8e | 2506 | assign_file_positions_for_relocs (abfd); |
244ffee7 | 2507 | |
32090b8e | 2508 | /* After writing the headers, we need to write the sections too... */ |
062189c6 | 2509 | for (count = 1; count < i_ehdrp->e_shnum; count++) |
e621c5cc | 2510 | { |
e621c5cc ILT |
2511 | if (bed->elf_backend_section_processing) |
2512 | (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); | |
2513 | if (i_shdrp[count]->contents) | |
2514 | { | |
4002f18a ILT |
2515 | if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 |
2516 | || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, | |
2517 | 1, abfd) | |
2518 | != i_shdrp[count]->sh_size)) | |
2519 | return false; | |
e621c5cc ILT |
2520 | } |
2521 | } | |
062189c6 | 2522 | |
eb4267a3 ILT |
2523 | /* Write out the section header names. */ |
2524 | if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 | |
2525 | || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd))) | |
2526 | return false; | |
2527 | ||
062189c6 | 2528 | if (bed->elf_backend_final_write_processing) |
3c9832f8 ILT |
2529 | (*bed->elf_backend_final_write_processing) (abfd, |
2530 | elf_tdata (abfd)->linker); | |
062189c6 | 2531 | |
32090b8e KR |
2532 | return write_shdrs_and_ehdr (abfd); |
2533 | } | |
244ffee7 | 2534 | |
24f13b03 ILT |
2535 | /* Given an ELF section number, retrieve the corresponding BFD |
2536 | section. */ | |
244ffee7 | 2537 | |
2e03ce18 | 2538 | static asection * |
1c6042ee ILT |
2539 | section_from_elf_index (abfd, index) |
2540 | bfd *abfd; | |
2541 | unsigned int index; | |
32090b8e | 2542 | { |
24f13b03 | 2543 | BFD_ASSERT (index > 0 && index < SHN_LORESERVE); |
e9227b42 | 2544 | if (index >= elf_elfheader (abfd)->e_shnum) |
2e03ce18 | 2545 | return NULL; |
24f13b03 | 2546 | return elf_elfsections (abfd)[index]->bfd_section; |
32090b8e | 2547 | } |
244ffee7 | 2548 | |
32090b8e KR |
2549 | /* given a section, search the header to find them... */ |
2550 | static int | |
1c6042ee ILT |
2551 | elf_section_from_bfd_section (abfd, asect) |
2552 | bfd *abfd; | |
2553 | struct sec *asect; | |
32090b8e | 2554 | { |
24f13b03 | 2555 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
32090b8e KR |
2556 | Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); |
2557 | int index; | |
2558 | Elf_Internal_Shdr *hdr; | |
2559 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
244ffee7 | 2560 | |
e4a4da62 ILT |
2561 | if (bfd_is_abs_section (asect)) |
2562 | return SHN_ABS; | |
2563 | if (bfd_is_com_section (asect)) | |
2564 | return SHN_COMMON; | |
2565 | if (bfd_is_und_section (asect)) | |
2566 | return SHN_UNDEF; | |
244ffee7 | 2567 | |
32090b8e KR |
2568 | for (index = 0; index < maxindex; index++) |
2569 | { | |
2570 | hdr = i_shdrp[index]; | |
24f13b03 ILT |
2571 | if (hdr->bfd_section == asect) |
2572 | return index; | |
e4a4da62 | 2573 | } |
e621c5cc | 2574 | |
e4a4da62 ILT |
2575 | if (bed->elf_backend_section_from_bfd_section) |
2576 | { | |
2577 | for (index = 0; index < maxindex; index++) | |
24f13b03 ILT |
2578 | { |
2579 | int retval; | |
f035cc47 | 2580 | |
e4a4da62 | 2581 | hdr = i_shdrp[index]; |
24f13b03 ILT |
2582 | retval = index; |
2583 | if ((*bed->elf_backend_section_from_bfd_section) | |
2584 | (abfd, hdr, asect, &retval)) | |
2585 | return retval; | |
32090b8e KR |
2586 | } |
2587 | } | |
24f13b03 | 2588 | |
32090b8e KR |
2589 | return -1; |
2590 | } | |
244ffee7 | 2591 | |
32090b8e KR |
2592 | /* given a symbol, return the bfd index for that symbol. */ |
2593 | static int | |
1c6042ee ILT |
2594 | elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) |
2595 | bfd *abfd; | |
2596 | struct symbol_cache_entry **asym_ptr_ptr; | |
32090b8e KR |
2597 | { |
2598 | struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr; | |
32090b8e | 2599 | int idx; |
d24928c0 | 2600 | flagword flags = asym_ptr->flags; |
32090b8e | 2601 | |
d24928c0 KR |
2602 | /* When gas creates relocations against local labels, it creates its |
2603 | own symbol for the section, but does put the symbol into the | |
e621c5cc ILT |
2604 | symbol chain, so udata is 0. When the linker is generating |
2605 | relocatable output, this section symbol may be for one of the | |
2606 | input sections rather than the output section. */ | |
e4a4da62 | 2607 | if (asym_ptr->udata.i == 0 |
d24928c0 | 2608 | && (flags & BSF_SECTION_SYM) |
e621c5cc ILT |
2609 | && asym_ptr->section) |
2610 | { | |
2611 | int indx; | |
2612 | ||
2613 | if (asym_ptr->section->output_section != NULL) | |
2614 | indx = asym_ptr->section->output_section->index; | |
2615 | else | |
2616 | indx = asym_ptr->section->index; | |
2617 | if (elf_section_syms (abfd)[indx]) | |
e4a4da62 | 2618 | asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; |
01383fb4 | 2619 | } |
e621c5cc | 2620 | |
e4a4da62 ILT |
2621 | idx = asym_ptr->udata.i; |
2622 | if (idx == 0) | |
2623 | abort (); | |
244ffee7 | 2624 | |
32090b8e | 2625 | #if DEBUG & 4 |
244ffee7 | 2626 | { |
244ffee7 | 2627 | |
32090b8e | 2628 | fprintf (stderr, |
d24928c0 | 2629 | "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n", |
1c6042ee | 2630 | (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags)); |
32090b8e KR |
2631 | fflush (stderr); |
2632 | } | |
2633 | #endif | |
2634 | ||
2635 | return idx; | |
2636 | } | |
2637 | ||
cb71adf1 PS |
2638 | static long |
2639 | elf_slurp_symbol_table (abfd, symptrs, dynamic) | |
1c6042ee ILT |
2640 | bfd *abfd; |
2641 | asymbol **symptrs; /* Buffer for generated bfd symbols */ | |
cb71adf1 | 2642 | boolean dynamic; |
32090b8e | 2643 | { |
cb71adf1 | 2644 | Elf_Internal_Shdr *hdr; |
7d8aaf36 | 2645 | long symcount; /* Number of external ELF symbols */ |
32090b8e KR |
2646 | elf_symbol_type *sym; /* Pointer to current bfd symbol */ |
2647 | elf_symbol_type *symbase; /* Buffer for generated bfd symbols */ | |
2648 | Elf_Internal_Sym i_sym; | |
80425e6c | 2649 | Elf_External_Sym *x_symp = NULL; |
32090b8e | 2650 | |
32090b8e KR |
2651 | /* Read each raw ELF symbol, converting from external ELF form to |
2652 | internal ELF form, and then using the information to create a | |
2653 | canonical bfd symbol table entry. | |
244ffee7 | 2654 | |
32090b8e KR |
2655 | Note that we allocate the initial bfd canonical symbol buffer |
2656 | based on a one-to-one mapping of the ELF symbols to canonical | |
2657 | symbols. We actually use all the ELF symbols, so there will be no | |
2658 | space left over at the end. When we have all the symbols, we | |
2659 | build the caller's pointer vector. */ | |
244ffee7 | 2660 | |
cb71adf1 PS |
2661 | if (dynamic) |
2662 | hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2663 | else | |
2664 | hdr = &elf_tdata (abfd)->symtab_hdr; | |
32090b8e | 2665 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) |
cb71adf1 | 2666 | return -1; |
244ffee7 | 2667 | |
32090b8e | 2668 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
244ffee7 | 2669 | |
7d8aaf36 ILT |
2670 | if (symcount == 0) |
2671 | sym = symbase = NULL; | |
2672 | else | |
244ffee7 | 2673 | { |
7d8aaf36 | 2674 | long i; |
244ffee7 | 2675 | |
7d8aaf36 | 2676 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) |
cb71adf1 | 2677 | return -1; |
7d8aaf36 ILT |
2678 | |
2679 | symbase = ((elf_symbol_type *) | |
2680 | bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type))); | |
2681 | if (symbase == (elf_symbol_type *) NULL) | |
32090b8e | 2682 | { |
7d8aaf36 | 2683 | bfd_set_error (bfd_error_no_memory); |
cb71adf1 | 2684 | return -1; |
32090b8e | 2685 | } |
7d8aaf36 ILT |
2686 | sym = symbase; |
2687 | ||
2688 | /* Temporarily allocate room for the raw ELF symbols. */ | |
2689 | x_symp = ((Elf_External_Sym *) | |
80425e6c | 2690 | malloc (symcount * sizeof (Elf_External_Sym))); |
25057836 | 2691 | if (x_symp == NULL && symcount != 0) |
80425e6c JK |
2692 | { |
2693 | bfd_set_error (bfd_error_no_memory); | |
2694 | goto error_return; | |
2695 | } | |
7d8aaf36 ILT |
2696 | |
2697 | if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd) | |
2698 | != symcount * sizeof (Elf_External_Sym)) | |
25057836 | 2699 | goto error_return; |
7d8aaf36 ILT |
2700 | /* Skip first symbol, which is a null dummy. */ |
2701 | for (i = 1; i < symcount; i++) | |
32090b8e | 2702 | { |
7d8aaf36 ILT |
2703 | elf_swap_symbol_in (abfd, x_symp + i, &i_sym); |
2704 | memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym)); | |
2705 | #ifdef ELF_KEEP_EXTSYM | |
2706 | memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym)); | |
2707 | #endif | |
2708 | sym->symbol.the_bfd = abfd; | |
244ffee7 | 2709 | |
7d8aaf36 ILT |
2710 | sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link, |
2711 | i_sym.st_name); | |
244ffee7 | 2712 | |
7d8aaf36 | 2713 | sym->symbol.value = i_sym.st_value; |
244ffee7 | 2714 | |
6ec3bb6a | 2715 | if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE) |
7d8aaf36 ILT |
2716 | { |
2717 | sym->symbol.section = section_from_elf_index (abfd, | |
2718 | i_sym.st_shndx); | |
2e03ce18 | 2719 | if (sym->symbol.section == NULL) |
013dec1a ILT |
2720 | { |
2721 | /* This symbol is in a section for which we did not | |
2722 | create a BFD section. Just use bfd_abs_section, | |
2723 | although it is wrong. FIXME. */ | |
badd23e3 | 2724 | sym->symbol.section = bfd_abs_section_ptr; |
013dec1a | 2725 | } |
7d8aaf36 ILT |
2726 | } |
2727 | else if (i_sym.st_shndx == SHN_ABS) | |
2728 | { | |
badd23e3 | 2729 | sym->symbol.section = bfd_abs_section_ptr; |
7d8aaf36 ILT |
2730 | } |
2731 | else if (i_sym.st_shndx == SHN_COMMON) | |
2732 | { | |
badd23e3 | 2733 | sym->symbol.section = bfd_com_section_ptr; |
7d8aaf36 ILT |
2734 | /* Elf puts the alignment into the `value' field, and |
2735 | the size into the `size' field. BFD wants to see the | |
2736 | size in the value field, and doesn't care (at the | |
2737 | moment) about the alignment. */ | |
2738 | sym->symbol.value = i_sym.st_size; | |
2739 | } | |
2740 | else if (i_sym.st_shndx == SHN_UNDEF) | |
2741 | { | |
badd23e3 | 2742 | sym->symbol.section = bfd_und_section_ptr; |
7d8aaf36 ILT |
2743 | } |
2744 | else | |
badd23e3 | 2745 | sym->symbol.section = bfd_abs_section_ptr; |
300adb31 | 2746 | |
7d8aaf36 | 2747 | sym->symbol.value -= sym->symbol.section->vma; |
244ffee7 | 2748 | |
7d8aaf36 ILT |
2749 | switch (ELF_ST_BIND (i_sym.st_info)) |
2750 | { | |
2751 | case STB_LOCAL: | |
2752 | sym->symbol.flags |= BSF_LOCAL; | |
2753 | break; | |
2754 | case STB_GLOBAL: | |
2755 | sym->symbol.flags |= BSF_GLOBAL; | |
2756 | break; | |
2757 | case STB_WEAK: | |
2758 | sym->symbol.flags |= BSF_WEAK; | |
2759 | break; | |
2760 | } | |
2761 | ||
2762 | switch (ELF_ST_TYPE (i_sym.st_info)) | |
2763 | { | |
2764 | case STT_SECTION: | |
2765 | sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING; | |
2766 | break; | |
2767 | case STT_FILE: | |
2768 | sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING; | |
2769 | break; | |
2770 | case STT_FUNC: | |
2771 | sym->symbol.flags |= BSF_FUNCTION; | |
2772 | break; | |
2773 | } | |
2774 | ||
cb71adf1 PS |
2775 | if (dynamic) |
2776 | sym->symbol.flags |= BSF_DYNAMIC; | |
2777 | ||
7d8aaf36 ILT |
2778 | /* Do some backend-specific processing on this symbol. */ |
2779 | { | |
2780 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
2781 | if (ebd->elf_backend_symbol_processing) | |
2782 | (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol); | |
2783 | } | |
2784 | ||
2785 | sym++; | |
2786 | } | |
244ffee7 JK |
2787 | } |
2788 | ||
e621c5cc ILT |
2789 | /* Do some backend-specific processing on this symbol table. */ |
2790 | { | |
2791 | struct elf_backend_data *ebd = get_elf_backend_data (abfd); | |
2792 | if (ebd->elf_backend_symbol_table_processing) | |
2793 | (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount); | |
2794 | } | |
244ffee7 | 2795 | |
e621c5cc | 2796 | /* We rely on the zalloc to clear out the final symbol entry. */ |
244ffee7 | 2797 | |
cb71adf1 | 2798 | symcount = sym - symbase; |
32090b8e KR |
2799 | |
2800 | /* Fill in the user's symbol pointer vector if needed. */ | |
2801 | if (symptrs) | |
244ffee7 | 2802 | { |
cb71adf1 PS |
2803 | long l = symcount; |
2804 | ||
32090b8e | 2805 | sym = symbase; |
cb71adf1 | 2806 | while (l-- > 0) |
244ffee7 | 2807 | { |
32090b8e KR |
2808 | *symptrs++ = &sym->symbol; |
2809 | sym++; | |
244ffee7 | 2810 | } |
32090b8e | 2811 | *symptrs = 0; /* Final null pointer */ |
244ffee7 JK |
2812 | } |
2813 | ||
80425e6c JK |
2814 | if (x_symp != NULL) |
2815 | free (x_symp); | |
cb71adf1 | 2816 | return symcount; |
1c6042ee | 2817 | error_return: |
80425e6c JK |
2818 | if (x_symp != NULL) |
2819 | free (x_symp); | |
cb71adf1 | 2820 | return -1; |
244ffee7 JK |
2821 | } |
2822 | ||
32090b8e | 2823 | /* Return the number of bytes required to hold the symtab vector. |
244ffee7 | 2824 | |
32090b8e KR |
2825 | Note that we base it on the count plus 1, since we will null terminate |
2826 | the vector allocated based on this size. However, the ELF symbol table | |
2827 | always has a dummy entry as symbol #0, so it ends up even. */ | |
244ffee7 | 2828 | |
326e32d7 | 2829 | long |
1c6042ee ILT |
2830 | elf_get_symtab_upper_bound (abfd) |
2831 | bfd *abfd; | |
244ffee7 | 2832 | { |
326e32d7 ILT |
2833 | long symcount; |
2834 | long symtab_size; | |
1c6042ee | 2835 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; |
326e32d7 | 2836 | |
32090b8e | 2837 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
d6439785 | 2838 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); |
244ffee7 | 2839 | |
32090b8e KR |
2840 | return symtab_size; |
2841 | } | |
244ffee7 | 2842 | |
cb71adf1 PS |
2843 | long |
2844 | elf_get_dynamic_symtab_upper_bound (abfd) | |
2845 | bfd *abfd; | |
2846 | { | |
2847 | long symcount; | |
2848 | long symtab_size; | |
2849 | Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; | |
2850 | ||
f9779aad ILT |
2851 | if (elf_dynsymtab (abfd) == 0) |
2852 | { | |
2853 | bfd_set_error (bfd_error_invalid_operation); | |
2854 | return -1; | |
2855 | } | |
2856 | ||
cb71adf1 PS |
2857 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); |
2858 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); | |
2859 | ||
2860 | return symtab_size; | |
2861 | } | |
2862 | ||
326e32d7 | 2863 | long |
32090b8e KR |
2864 | elf_get_reloc_upper_bound (abfd, asect) |
2865 | bfd *abfd; | |
2866 | sec_ptr asect; | |
2867 | { | |
51fc377b | 2868 | return (asect->reloc_count + 1) * sizeof (arelent *); |
244ffee7 JK |
2869 | } |
2870 | ||
ea617174 ILT |
2871 | /* Read in and swap the external relocs. */ |
2872 | ||
32090b8e | 2873 | static boolean |
ea617174 | 2874 | elf_slurp_reloc_table (abfd, asect, symbols) |
1c6042ee | 2875 | bfd *abfd; |
ea617174 | 2876 | asection *asect; |
1c6042ee | 2877 | asymbol **symbols; |
244ffee7 | 2878 | { |
ea617174 ILT |
2879 | struct elf_backend_data * const ebd = get_elf_backend_data (abfd); |
2880 | struct bfd_elf_section_data * const d = elf_section_data (asect); | |
2881 | PTR allocated = NULL; | |
2882 | bfd_byte *native_relocs; | |
2883 | arelent *relents; | |
2884 | arelent *relent; | |
2885 | unsigned int i; | |
2886 | int entsize; | |
244ffee7 | 2887 | |
d510fd89 ILT |
2888 | if (asect->relocation != NULL |
2889 | || (asect->flags & SEC_RELOC) == 0 | |
2890 | || asect->reloc_count == 0) | |
32090b8e | 2891 | return true; |
244ffee7 | 2892 | |
ea617174 ILT |
2893 | BFD_ASSERT (asect->rel_filepos == d->rel_hdr.sh_offset |
2894 | && (asect->reloc_count | |
2895 | == d->rel_hdr.sh_size / d->rel_hdr.sh_entsize)); | |
2896 | ||
374d2ef9 ILT |
2897 | allocated = (PTR) malloc (d->rel_hdr.sh_size); |
2898 | if (allocated == NULL) | |
9783e04a | 2899 | { |
374d2ef9 ILT |
2900 | bfd_set_error (bfd_error_no_memory); |
2901 | goto error_return; | |
2902 | } | |
ea617174 | 2903 | |
374d2ef9 ILT |
2904 | if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0 |
2905 | || (bfd_read (allocated, 1, d->rel_hdr.sh_size, abfd) | |
2906 | != d->rel_hdr.sh_size)) | |
2907 | goto error_return; | |
244ffee7 | 2908 | |
374d2ef9 | 2909 | native_relocs = (bfd_byte *) allocated; |
32090b8e | 2910 | |
ea617174 ILT |
2911 | relents = ((arelent *) |
2912 | bfd_alloc (abfd, asect->reloc_count * sizeof (arelent))); | |
2913 | if (relents == NULL) | |
6a3eb9b6 | 2914 | { |
d1ad85a6 | 2915 | bfd_set_error (bfd_error_no_memory); |
ea617174 | 2916 | goto error_return; |
6a3eb9b6 | 2917 | } |
244ffee7 | 2918 | |
ea617174 ILT |
2919 | entsize = d->rel_hdr.sh_entsize; |
2920 | BFD_ASSERT (entsize == sizeof (Elf_External_Rel) | |
2921 | || entsize == sizeof (Elf_External_Rela)); | |
244ffee7 | 2922 | |
ea617174 ILT |
2923 | for (i = 0, relent = relents; |
2924 | i < asect->reloc_count; | |
2925 | i++, relent++, native_relocs += entsize) | |
2926 | { | |
2927 | Elf_Internal_Rela rela; | |
2928 | Elf_Internal_Rel rel; | |
244ffee7 | 2929 | |
ea617174 ILT |
2930 | if (entsize == sizeof (Elf_External_Rela)) |
2931 | elf_swap_reloca_in (abfd, (Elf_External_Rela *) native_relocs, &rela); | |
32090b8e KR |
2932 | else |
2933 | { | |
ea617174 ILT |
2934 | elf_swap_reloc_in (abfd, (Elf_External_Rel *) native_relocs, &rel); |
2935 | rela.r_offset = rel.r_offset; | |
2936 | rela.r_info = rel.r_info; | |
2937 | rela.r_addend = 0; | |
32090b8e | 2938 | } |
7b8106b4 | 2939 | |
ea617174 ILT |
2940 | /* The address of an ELF reloc is section relative for an object |
2941 | file, and absolute for an executable file or shared library. | |
2942 | The address of a BFD reloc is always section relative. */ | |
2943 | if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) | |
2944 | relent->address = rela.r_offset; | |
2945 | else | |
2946 | relent->address = rela.r_offset - asect->vma; | |
2947 | ||
2948 | if (ELF_R_SYM (rela.r_info) == 0) | |
2949 | relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; | |
7b8106b4 ILT |
2950 | else |
2951 | { | |
ea617174 | 2952 | asymbol **ps, *s; |
7b8106b4 | 2953 | |
ea617174 ILT |
2954 | ps = symbols + ELF_R_SYM (rela.r_info) - 1; |
2955 | s = *ps; | |
7b8106b4 | 2956 | |
ea617174 ILT |
2957 | /* Canonicalize ELF section symbols. FIXME: Why? */ |
2958 | if ((s->flags & BSF_SECTION_SYM) == 0) | |
2959 | relent->sym_ptr_ptr = ps; | |
2960 | else | |
2961 | relent->sym_ptr_ptr = s->section->symbol_ptr_ptr; | |
7b8106b4 | 2962 | } |
244ffee7 | 2963 | |
ea617174 ILT |
2964 | relent->addend = rela.r_addend; |
2965 | ||
2966 | if (entsize == sizeof (Elf_External_Rela)) | |
2967 | (*ebd->elf_info_to_howto) (abfd, relent, &rela); | |
2968 | else | |
2969 | (*ebd->elf_info_to_howto_rel) (abfd, relent, &rel); | |
32090b8e | 2970 | } |
244ffee7 | 2971 | |
ea617174 ILT |
2972 | asect->relocation = relents; |
2973 | ||
2974 | if (allocated != NULL) | |
2975 | free (allocated); | |
2976 | ||
32090b8e | 2977 | return true; |
ea617174 ILT |
2978 | |
2979 | error_return: | |
2980 | if (allocated != NULL) | |
2981 | free (allocated); | |
2982 | return false; | |
32090b8e | 2983 | } |
238ac6ec | 2984 | |
32090b8e KR |
2985 | #ifdef DEBUG |
2986 | static void | |
eb4267a3 | 2987 | elf_debug_section (num, hdr) |
32090b8e KR |
2988 | int num; |
2989 | Elf_Internal_Shdr *hdr; | |
2990 | { | |
eb4267a3 ILT |
2991 | fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, |
2992 | hdr->bfd_section != NULL ? hfd->bfd_section->name : "", | |
2993 | (long) hdr); | |
32090b8e KR |
2994 | fprintf (stderr, |
2995 | "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n", | |
2996 | (long) hdr->sh_name, | |
2997 | (long) hdr->sh_type, | |
2998 | (long) hdr->sh_flags); | |
2999 | fprintf (stderr, | |
3000 | "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n", | |
3001 | (long) hdr->sh_addr, | |
3002 | (long) hdr->sh_offset, | |
3003 | (long) hdr->sh_size); | |
3004 | fprintf (stderr, | |
3005 | "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n", | |
3006 | (long) hdr->sh_link, | |
3007 | (long) hdr->sh_info, | |
3008 | (long) hdr->sh_addralign); | |
3009 | fprintf (stderr, "sh_entsize = %ld\n", | |
3010 | (long) hdr->sh_entsize); | |
32090b8e KR |
3011 | fflush (stderr); |
3012 | } | |
244ffee7 | 3013 | |
32090b8e KR |
3014 | static void |
3015 | elf_debug_file (ehdrp) | |
3016 | Elf_Internal_Ehdr *ehdrp; | |
3017 | { | |
3018 | fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry); | |
3019 | fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff); | |
3020 | fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum); | |
3021 | fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize); | |
3022 | fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff); | |
3023 | fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum); | |
3024 | fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize); | |
244ffee7 | 3025 | } |
32090b8e | 3026 | #endif |
244ffee7 | 3027 | |
ea617174 | 3028 | /* Canonicalize the relocs. */ |
244ffee7 | 3029 | |
326e32d7 | 3030 | long |
32090b8e KR |
3031 | elf_canonicalize_reloc (abfd, section, relptr, symbols) |
3032 | bfd *abfd; | |
3033 | sec_ptr section; | |
3034 | arelent **relptr; | |
3035 | asymbol **symbols; | |
3036 | { | |
ea617174 ILT |
3037 | arelent *tblptr; |
3038 | unsigned int i; | |
32090b8e | 3039 | |
ea617174 ILT |
3040 | if (! elf_slurp_reloc_table (abfd, section, symbols)) |
3041 | return -1; | |
32090b8e KR |
3042 | |
3043 | tblptr = section->relocation; | |
ea617174 | 3044 | for (i = 0; i < section->reloc_count; i++) |
32090b8e KR |
3045 | *relptr++ = tblptr++; |
3046 | ||
ea617174 ILT |
3047 | *relptr = NULL; |
3048 | ||
32090b8e KR |
3049 | return section->reloc_count; |
3050 | } | |
3051 | ||
326e32d7 | 3052 | long |
1c6042ee ILT |
3053 | elf_get_symtab (abfd, alocation) |
3054 | bfd *abfd; | |
3055 | asymbol **alocation; | |
32090b8e | 3056 | { |
cb71adf1 PS |
3057 | long symcount = elf_slurp_symbol_table (abfd, alocation, false); |
3058 | ||
3059 | if (symcount >= 0) | |
3060 | bfd_get_symcount (abfd) = symcount; | |
3061 | return symcount; | |
3062 | } | |
326e32d7 | 3063 | |
cb71adf1 PS |
3064 | long |
3065 | elf_canonicalize_dynamic_symtab (abfd, alocation) | |
3066 | bfd *abfd; | |
3067 | asymbol **alocation; | |
3068 | { | |
3069 | return elf_slurp_symbol_table (abfd, alocation, true); | |
32090b8e KR |
3070 | } |
3071 | ||
3072 | asymbol * | |
1c6042ee ILT |
3073 | elf_make_empty_symbol (abfd) |
3074 | bfd *abfd; | |
32090b8e KR |
3075 | { |
3076 | elf_symbol_type *newsym; | |
3077 | ||
3078 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
3079 | if (!newsym) | |
3080 | { | |
d1ad85a6 | 3081 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
3082 | return NULL; |
3083 | } | |
3084 | else | |
3085 | { | |
3086 | newsym->symbol.the_bfd = abfd; | |
3087 | return &newsym->symbol; | |
244ffee7 | 3088 | } |
32090b8e | 3089 | } |
244ffee7 | 3090 | |
32090b8e | 3091 | void |
1c6042ee ILT |
3092 | elf_get_symbol_info (ignore_abfd, symbol, ret) |
3093 | bfd *ignore_abfd; | |
3094 | asymbol *symbol; | |
3095 | symbol_info *ret; | |
32090b8e KR |
3096 | { |
3097 | bfd_symbol_info (symbol, ret); | |
3098 | } | |
244ffee7 | 3099 | |
32090b8e | 3100 | alent * |
1c6042ee ILT |
3101 | elf_get_lineno (ignore_abfd, symbol) |
3102 | bfd *ignore_abfd; | |
3103 | asymbol *symbol; | |
32090b8e KR |
3104 | { |
3105 | fprintf (stderr, "elf_get_lineno unimplemented\n"); | |
3106 | fflush (stderr); | |
3107 | BFD_FAIL (); | |
3108 | return NULL; | |
3109 | } | |
3110 | ||
3111 | boolean | |
1c6042ee ILT |
3112 | elf_set_arch_mach (abfd, arch, machine) |
3113 | bfd *abfd; | |
3114 | enum bfd_architecture arch; | |
3115 | unsigned long machine; | |
32090b8e | 3116 | { |
80a903c9 ILT |
3117 | /* If this isn't the right architecture for this backend, and this |
3118 | isn't the generic backend, fail. */ | |
3119 | if (arch != get_elf_backend_data (abfd)->arch | |
3120 | && arch != bfd_arch_unknown | |
3121 | && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) | |
3122 | return false; | |
3123 | ||
3124 | return bfd_default_set_arch_mach (abfd, arch, machine); | |
32090b8e | 3125 | } |
244ffee7 | 3126 | |
32090b8e | 3127 | boolean |
1c6042ee ILT |
3128 | elf_find_nearest_line (abfd, |
3129 | section, | |
3130 | symbols, | |
3131 | offset, | |
3132 | filename_ptr, | |
3133 | functionname_ptr, | |
3134 | line_ptr) | |
3135 | bfd *abfd; | |
3136 | asection *section; | |
3137 | asymbol **symbols; | |
3138 | bfd_vma offset; | |
3139 | CONST char **filename_ptr; | |
3140 | CONST char **functionname_ptr; | |
3141 | unsigned int *line_ptr; | |
32090b8e KR |
3142 | { |
3143 | return false; | |
244ffee7 JK |
3144 | } |
3145 | ||
32090b8e | 3146 | int |
1c6042ee ILT |
3147 | elf_sizeof_headers (abfd, reloc) |
3148 | bfd *abfd; | |
3149 | boolean reloc; | |
32090b8e | 3150 | { |
013dec1a ILT |
3151 | int ret; |
3152 | ||
3153 | ret = sizeof (Elf_External_Ehdr); | |
3154 | if (! reloc) | |
3155 | ret += get_program_header_size (abfd); | |
3156 | return ret; | |
32090b8e | 3157 | } |
244ffee7 | 3158 | |
32090b8e | 3159 | boolean |
1c6042ee ILT |
3160 | elf_set_section_contents (abfd, section, location, offset, count) |
3161 | bfd *abfd; | |
3162 | sec_ptr section; | |
3163 | PTR location; | |
3164 | file_ptr offset; | |
3165 | bfd_size_type count; | |
244ffee7 | 3166 | { |
244ffee7 JK |
3167 | Elf_Internal_Shdr *hdr; |
3168 | ||
6ec3bb6a ILT |
3169 | if (! abfd->output_has_begun |
3170 | && ! elf_compute_section_file_positions (abfd, | |
3171 | (struct bfd_link_info *) NULL)) | |
3172 | return false; | |
244ffee7 | 3173 | |
1c6042ee | 3174 | hdr = &elf_section_data (section)->this_hdr; |
244ffee7 | 3175 | |
32090b8e KR |
3176 | if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) |
3177 | return false; | |
3178 | if (bfd_write (location, 1, count, abfd) != count) | |
3179 | return false; | |
3180 | ||
3181 | return true; | |
3182 | } | |
3183 | ||
3184 | void | |
1c6042ee ILT |
3185 | elf_no_info_to_howto (abfd, cache_ptr, dst) |
3186 | bfd *abfd; | |
3187 | arelent *cache_ptr; | |
3188 | Elf_Internal_Rela *dst; | |
244ffee7 | 3189 | { |
32090b8e KR |
3190 | fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n"); |
3191 | fflush (stderr); | |
3192 | BFD_FAIL (); | |
244ffee7 JK |
3193 | } |
3194 | ||
32090b8e | 3195 | void |
1c6042ee ILT |
3196 | elf_no_info_to_howto_rel (abfd, cache_ptr, dst) |
3197 | bfd *abfd; | |
3198 | arelent *cache_ptr; | |
3199 | Elf_Internal_Rel *dst; | |
244ffee7 | 3200 | { |
32090b8e KR |
3201 | fprintf (stderr, "elf REL relocation support for target machine unimplemented\n"); |
3202 | fflush (stderr); | |
3203 | BFD_FAIL (); | |
3204 | } | |
32090b8e | 3205 | \f |
1c6042ee | 3206 | |
32090b8e | 3207 | /* Core file support */ |
244ffee7 | 3208 | |
32090b8e KR |
3209 | #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */ |
3210 | #include <sys/procfs.h> | |
3211 | #else | |
2e03ce18 ILT |
3212 | #define bfd_prstatus(abfd, descdata, descsz, filepos) true |
3213 | #define bfd_fpregset(abfd, descdata, descsz, filepos) true | |
3214 | #define bfd_prpsinfo(abfd, descdata, descsz, filepos) true | |
32090b8e | 3215 | #endif |
244ffee7 | 3216 | |
32090b8e | 3217 | #ifdef HAVE_PROCFS |
244ffee7 | 3218 | |
2e03ce18 | 3219 | static boolean |
1c6042ee ILT |
3220 | bfd_prstatus (abfd, descdata, descsz, filepos) |
3221 | bfd *abfd; | |
3222 | char *descdata; | |
3223 | int descsz; | |
3224 | long filepos; | |
32090b8e KR |
3225 | { |
3226 | asection *newsect; | |
3227 | prstatus_t *status = (prstatus_t *) 0; | |
244ffee7 | 3228 | |
32090b8e | 3229 | if (descsz == sizeof (prstatus_t)) |
244ffee7 | 3230 | { |
32090b8e | 3231 | newsect = bfd_make_section (abfd, ".reg"); |
2e03ce18 ILT |
3232 | if (newsect == NULL) |
3233 | return false; | |
32090b8e KR |
3234 | newsect->_raw_size = sizeof (status->pr_reg); |
3235 | newsect->filepos = filepos + (long) &status->pr_reg; | |
57a814a9 | 3236 | newsect->flags = SEC_HAS_CONTENTS; |
32090b8e KR |
3237 | newsect->alignment_power = 2; |
3238 | if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL) | |
3239 | { | |
3240 | memcpy (core_prstatus (abfd), descdata, descsz); | |
3241 | } | |
244ffee7 | 3242 | } |
2e03ce18 | 3243 | return true; |
32090b8e | 3244 | } |
244ffee7 | 3245 | |
32090b8e | 3246 | /* Stash a copy of the prpsinfo structure away for future use. */ |
244ffee7 | 3247 | |
2e03ce18 | 3248 | static boolean |
1c6042ee ILT |
3249 | bfd_prpsinfo (abfd, descdata, descsz, filepos) |
3250 | bfd *abfd; | |
3251 | char *descdata; | |
3252 | int descsz; | |
3253 | long filepos; | |
32090b8e | 3254 | { |
32090b8e KR |
3255 | if (descsz == sizeof (prpsinfo_t)) |
3256 | { | |
2e03ce18 | 3257 | if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) == NULL) |
244ffee7 | 3258 | { |
2e03ce18 ILT |
3259 | bfd_set_error (bfd_error_no_memory); |
3260 | return false; | |
244ffee7 | 3261 | } |
2e03ce18 | 3262 | memcpy (core_prpsinfo (abfd), descdata, descsz); |
244ffee7 | 3263 | } |
2e03ce18 | 3264 | return true; |
244ffee7 JK |
3265 | } |
3266 | ||
2e03ce18 | 3267 | static boolean |
1c6042ee ILT |
3268 | bfd_fpregset (abfd, descdata, descsz, filepos) |
3269 | bfd *abfd; | |
3270 | char *descdata; | |
3271 | int descsz; | |
3272 | long filepos; | |
244ffee7 | 3273 | { |
32090b8e | 3274 | asection *newsect; |
244ffee7 | 3275 | |
32090b8e | 3276 | newsect = bfd_make_section (abfd, ".reg2"); |
2e03ce18 ILT |
3277 | if (newsect == NULL) |
3278 | return false; | |
32090b8e KR |
3279 | newsect->_raw_size = descsz; |
3280 | newsect->filepos = filepos; | |
57a814a9 | 3281 | newsect->flags = SEC_HAS_CONTENTS; |
32090b8e | 3282 | newsect->alignment_power = 2; |
2e03ce18 | 3283 | return true; |
6a3eb9b6 | 3284 | } |
244ffee7 | 3285 | |
32090b8e KR |
3286 | #endif /* HAVE_PROCFS */ |
3287 | ||
3288 | /* Return a pointer to the args (including the command name) that were | |
3289 | seen by the program that generated the core dump. Note that for | |
3290 | some reason, a spurious space is tacked onto the end of the args | |
3291 | in some (at least one anyway) implementations, so strip it off if | |
3292 | it exists. */ | |
3293 | ||
3294 | char * | |
1c6042ee ILT |
3295 | elf_core_file_failing_command (abfd) |
3296 | bfd *abfd; | |
244ffee7 | 3297 | { |
32090b8e KR |
3298 | #ifdef HAVE_PROCFS |
3299 | if (core_prpsinfo (abfd)) | |
3300 | { | |
3301 | prpsinfo_t *p = core_prpsinfo (abfd); | |
3302 | char *scan = p->pr_psargs; | |
3303 | while (*scan++) | |
3304 | {; | |
3305 | } | |
3306 | scan -= 2; | |
3307 | if ((scan > p->pr_psargs) && (*scan == ' ')) | |
3308 | { | |
3309 | *scan = '\000'; | |
3310 | } | |
3311 | return p->pr_psargs; | |
3312 | } | |
3313 | #endif | |
3314 | return NULL; | |
3315 | } | |
244ffee7 | 3316 | |
32090b8e KR |
3317 | /* Return the number of the signal that caused the core dump. Presumably, |
3318 | since we have a core file, we got a signal of some kind, so don't bother | |
3319 | checking the other process status fields, just return the signal number. | |
3320 | */ | |
244ffee7 | 3321 | |
32090b8e | 3322 | int |
1c6042ee ILT |
3323 | elf_core_file_failing_signal (abfd) |
3324 | bfd *abfd; | |
32090b8e KR |
3325 | { |
3326 | #ifdef HAVE_PROCFS | |
3327 | if (core_prstatus (abfd)) | |
3328 | { | |
3329 | return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig; | |
3330 | } | |
3331 | #endif | |
3332 | return -1; | |
3333 | } | |
244ffee7 | 3334 | |
32090b8e KR |
3335 | /* Check to see if the core file could reasonably be expected to have |
3336 | come for the current executable file. Note that by default we return | |
3337 | true unless we find something that indicates that there might be a | |
3338 | problem. | |
3339 | */ | |
244ffee7 | 3340 | |
32090b8e | 3341 | boolean |
1c6042ee ILT |
3342 | elf_core_file_matches_executable_p (core_bfd, exec_bfd) |
3343 | bfd *core_bfd; | |
3344 | bfd *exec_bfd; | |
32090b8e KR |
3345 | { |
3346 | #ifdef HAVE_PROCFS | |
3347 | char *corename; | |
3348 | char *execname; | |
3349 | #endif | |
244ffee7 | 3350 | |
32090b8e KR |
3351 | /* First, xvecs must match since both are ELF files for the same target. */ |
3352 | ||
3353 | if (core_bfd->xvec != exec_bfd->xvec) | |
244ffee7 | 3354 | { |
d1ad85a6 | 3355 | bfd_set_error (bfd_error_system_call); |
244ffee7 JK |
3356 | return false; |
3357 | } | |
3358 | ||
32090b8e | 3359 | #ifdef HAVE_PROCFS |
244ffee7 | 3360 | |
32090b8e KR |
3361 | /* If no prpsinfo, just return true. Otherwise, grab the last component |
3362 | of the exec'd pathname from the prpsinfo. */ | |
244ffee7 | 3363 | |
32090b8e | 3364 | if (core_prpsinfo (core_bfd)) |
244ffee7 | 3365 | { |
32090b8e KR |
3366 | corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname); |
3367 | } | |
3368 | else | |
3369 | { | |
3370 | return true; | |
3371 | } | |
244ffee7 | 3372 | |
32090b8e | 3373 | /* Find the last component of the executable pathname. */ |
244ffee7 | 3374 | |
32090b8e KR |
3375 | if ((execname = strrchr (exec_bfd->filename, '/')) != NULL) |
3376 | { | |
3377 | execname++; | |
3378 | } | |
3379 | else | |
3380 | { | |
3381 | execname = (char *) exec_bfd->filename; | |
3382 | } | |
244ffee7 | 3383 | |
32090b8e | 3384 | /* See if they match */ |
244ffee7 | 3385 | |
32090b8e | 3386 | return strcmp (execname, corename) ? false : true; |
244ffee7 | 3387 | |
32090b8e | 3388 | #else |
244ffee7 | 3389 | |
244ffee7 | 3390 | return true; |
244ffee7 | 3391 | |
32090b8e KR |
3392 | #endif /* HAVE_PROCFS */ |
3393 | } | |
244ffee7 | 3394 | |
32090b8e KR |
3395 | /* ELF core files contain a segment of type PT_NOTE, that holds much of |
3396 | the information that would normally be available from the /proc interface | |
3397 | for the process, at the time the process dumped core. Currently this | |
3398 | includes copies of the prstatus, prpsinfo, and fpregset structures. | |
244ffee7 | 3399 | |
32090b8e KR |
3400 | Since these structures are potentially machine dependent in size and |
3401 | ordering, bfd provides two levels of support for them. The first level, | |
3402 | available on all machines since it does not require that the host | |
3403 | have /proc support or the relevant include files, is to create a bfd | |
3404 | section for each of the prstatus, prpsinfo, and fpregset structures, | |
3405 | without any interpretation of their contents. With just this support, | |
3406 | the bfd client will have to interpret the structures itself. Even with | |
3407 | /proc support, it might want these full structures for it's own reasons. | |
244ffee7 | 3408 | |
32090b8e KR |
3409 | In the second level of support, where HAVE_PROCFS is defined, bfd will |
3410 | pick apart the structures to gather some additional information that | |
3411 | clients may want, such as the general register set, the name of the | |
3412 | exec'ed file and its arguments, the signal (if any) that caused the | |
3413 | core dump, etc. | |
244ffee7 | 3414 | |
32090b8e | 3415 | */ |
244ffee7 | 3416 | |
32090b8e | 3417 | static boolean |
1c6042ee ILT |
3418 | elf_corefile_note (abfd, hdr) |
3419 | bfd *abfd; | |
3420 | Elf_Internal_Phdr *hdr; | |
244ffee7 | 3421 | { |
32090b8e KR |
3422 | Elf_External_Note *x_note_p; /* Elf note, external form */ |
3423 | Elf_Internal_Note i_note; /* Elf note, internal form */ | |
3424 | char *buf = NULL; /* Entire note segment contents */ | |
3425 | char *namedata; /* Name portion of the note */ | |
3426 | char *descdata; /* Descriptor portion of the note */ | |
3427 | char *sectname; /* Name to use for new section */ | |
3428 | long filepos; /* File offset to descriptor data */ | |
3429 | asection *newsect; | |
3430 | ||
3431 | if (hdr->p_filesz > 0 | |
b9d5cdf0 | 3432 | && (buf = (char *) malloc (hdr->p_filesz)) != NULL |
32090b8e KR |
3433 | && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1 |
3434 | && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz) | |
3435 | { | |
3436 | x_note_p = (Elf_External_Note *) buf; | |
3437 | while ((char *) x_note_p < (buf + hdr->p_filesz)) | |
3438 | { | |
3439 | i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz); | |
3440 | i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz); | |
3441 | i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type); | |
3442 | namedata = x_note_p->name; | |
3443 | descdata = namedata + BFD_ALIGN (i_note.namesz, 4); | |
3444 | filepos = hdr->p_offset + (descdata - buf); | |
3445 | switch (i_note.type) | |
3446 | { | |
3447 | case NT_PRSTATUS: | |
3448 | /* process descdata as prstatus info */ | |
2e03ce18 ILT |
3449 | if (! bfd_prstatus (abfd, descdata, i_note.descsz, filepos)) |
3450 | return false; | |
32090b8e KR |
3451 | sectname = ".prstatus"; |
3452 | break; | |
3453 | case NT_FPREGSET: | |
3454 | /* process descdata as fpregset info */ | |
2e03ce18 ILT |
3455 | if (! bfd_fpregset (abfd, descdata, i_note.descsz, filepos)) |
3456 | return false; | |
32090b8e KR |
3457 | sectname = ".fpregset"; |
3458 | break; | |
3459 | case NT_PRPSINFO: | |
3460 | /* process descdata as prpsinfo */ | |
2e03ce18 ILT |
3461 | if (! bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos)) |
3462 | return false; | |
32090b8e KR |
3463 | sectname = ".prpsinfo"; |
3464 | break; | |
3465 | default: | |
3466 | /* Unknown descriptor, just ignore it. */ | |
3467 | sectname = NULL; | |
3468 | break; | |
3469 | } | |
3470 | if (sectname != NULL) | |
3471 | { | |
3472 | newsect = bfd_make_section (abfd, sectname); | |
2e03ce18 ILT |
3473 | if (newsect == NULL) |
3474 | return false; | |
32090b8e KR |
3475 | newsect->_raw_size = i_note.descsz; |
3476 | newsect->filepos = filepos; | |
3477 | newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
3478 | newsect->alignment_power = 2; | |
3479 | } | |
3480 | x_note_p = (Elf_External_Note *) | |
3481 | (descdata + BFD_ALIGN (i_note.descsz, 4)); | |
3482 | } | |
3483 | } | |
3484 | if (buf != NULL) | |
3485 | { | |
3486 | free (buf); | |
3487 | } | |
b9d5cdf0 DM |
3488 | else if (hdr->p_filesz > 0) |
3489 | { | |
d1ad85a6 | 3490 | bfd_set_error (bfd_error_no_memory); |
b9d5cdf0 DM |
3491 | return false; |
3492 | } | |
32090b8e | 3493 | return true; |
244ffee7 | 3494 | |
244ffee7 JK |
3495 | } |
3496 | ||
32090b8e KR |
3497 | /* Core files are simply standard ELF formatted files that partition |
3498 | the file using the execution view of the file (program header table) | |
3499 | rather than the linking view. In fact, there is no section header | |
3500 | table in a core file. | |
3501 | ||
3502 | The process status information (including the contents of the general | |
3503 | register set) and the floating point register set are stored in a | |
3504 | segment of type PT_NOTE. We handcraft a couple of extra bfd sections | |
3505 | that allow standard bfd access to the general registers (.reg) and the | |
3506 | floating point registers (.reg2). | |
3507 | ||
3508 | */ | |
3509 | ||
2f3508ad | 3510 | const bfd_target * |
1c6042ee ILT |
3511 | elf_core_file_p (abfd) |
3512 | bfd *abfd; | |
244ffee7 | 3513 | { |
32090b8e KR |
3514 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ |
3515 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
3516 | Elf_External_Phdr x_phdr; /* Program header table entry, external form */ | |
3517 | Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */ | |
3518 | unsigned int phindex; | |
d6439785 | 3519 | struct elf_backend_data *ebd; |
244ffee7 | 3520 | |
32090b8e KR |
3521 | /* Read in the ELF header in external format. */ |
3522 | ||
3523 | if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
244ffee7 | 3524 | { |
25057836 JL |
3525 | if (bfd_get_error () != bfd_error_system_call) |
3526 | bfd_set_error (bfd_error_wrong_format); | |
244ffee7 JK |
3527 | return NULL; |
3528 | } | |
32090b8e KR |
3529 | |
3530 | /* Now check to see if we have a valid ELF file, and one that BFD can | |
3531 | make use of. The magic number must match, the address size ('class') | |
3532 | and byte-swapping must match our XVEC entry, and it must have a | |
3533 | program header table (FIXME: See comments re segments at top of this | |
3534 | file). */ | |
3535 | ||
3536 | if (elf_file_p (&x_ehdr) == false) | |
244ffee7 | 3537 | { |
32090b8e | 3538 | wrong: |
d1ad85a6 | 3539 | bfd_set_error (bfd_error_wrong_format); |
32090b8e | 3540 | return NULL; |
244ffee7 | 3541 | } |
244ffee7 | 3542 | |
32090b8e | 3543 | /* FIXME, Check EI_VERSION here ! */ |
244ffee7 | 3544 | |
32090b8e KR |
3545 | { |
3546 | #if ARCH_SIZE == 32 | |
3547 | int desired_address_size = ELFCLASS32; | |
3548 | #endif | |
3549 | #if ARCH_SIZE == 64 | |
3550 | int desired_address_size = ELFCLASS64; | |
3551 | #endif | |
3552 | ||
3553 | if (x_ehdr.e_ident[EI_CLASS] != desired_address_size) | |
3554 | goto wrong; | |
3555 | } | |
3556 | ||
3557 | /* Switch xvec to match the specified byte order. */ | |
3558 | switch (x_ehdr.e_ident[EI_DATA]) | |
244ffee7 | 3559 | { |
32090b8e KR |
3560 | case ELFDATA2MSB: /* Big-endian */ |
3561 | if (abfd->xvec->byteorder_big_p == false) | |
3562 | goto wrong; | |
244ffee7 | 3563 | break; |
32090b8e KR |
3564 | case ELFDATA2LSB: /* Little-endian */ |
3565 | if (abfd->xvec->byteorder_big_p == true) | |
3566 | goto wrong; | |
244ffee7 | 3567 | break; |
32090b8e KR |
3568 | case ELFDATANONE: /* No data encoding specified */ |
3569 | default: /* Unknown data encoding specified */ | |
3570 | goto wrong; | |
244ffee7 JK |
3571 | } |
3572 | ||
32090b8e KR |
3573 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to |
3574 | the tdata pointer in the bfd. */ | |
244ffee7 | 3575 | |
32090b8e KR |
3576 | elf_tdata (abfd) = |
3577 | (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); | |
3578 | if (elf_tdata (abfd) == NULL) | |
244ffee7 | 3579 | { |
d1ad85a6 | 3580 | bfd_set_error (bfd_error_no_memory); |
32090b8e | 3581 | return NULL; |
244ffee7 | 3582 | } |
244ffee7 | 3583 | |
32090b8e | 3584 | /* FIXME, `wrong' returns from this point onward, leak memory. */ |
244ffee7 | 3585 | |
32090b8e KR |
3586 | /* Now that we know the byte order, swap in the rest of the header */ |
3587 | i_ehdrp = elf_elfheader (abfd); | |
3588 | elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); | |
3589 | #if DEBUG & 1 | |
3590 | elf_debug_file (i_ehdrp); | |
3591 | #endif | |
244ffee7 | 3592 | |
d6439785 JL |
3593 | ebd = get_elf_backend_data (abfd); |
3594 | ||
3595 | /* Check that the ELF e_machine field matches what this particular | |
3596 | BFD format expects. */ | |
3597 | if (ebd->elf_machine_code != i_ehdrp->e_machine) | |
3598 | { | |
2f3508ad | 3599 | const bfd_target * const *target_ptr; |
d6439785 JL |
3600 | |
3601 | if (ebd->elf_machine_code != EM_NONE) | |
3602 | goto wrong; | |
3603 | ||
3604 | /* This is the generic ELF target. Let it match any ELF target | |
3605 | for which we do not have a specific backend. */ | |
3606 | for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) | |
3607 | { | |
3608 | struct elf_backend_data *back; | |
3609 | ||
3610 | if ((*target_ptr)->flavour != bfd_target_elf_flavour) | |
3611 | continue; | |
3612 | back = (struct elf_backend_data *) (*target_ptr)->backend_data; | |
3613 | if (back->elf_machine_code == i_ehdrp->e_machine) | |
3614 | { | |
3615 | /* target_ptr is an ELF backend which matches this | |
3616 | object file, so reject the generic ELF target. */ | |
3617 | goto wrong; | |
3618 | } | |
3619 | } | |
3620 | } | |
3621 | ||
32090b8e KR |
3622 | /* If there is no program header, or the type is not a core file, then |
3623 | we are hosed. */ | |
3624 | if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE) | |
3625 | goto wrong; | |
244ffee7 | 3626 | |
32090b8e KR |
3627 | /* Allocate space for a copy of the program header table in |
3628 | internal form, seek to the program header table in the file, | |
3629 | read it in, and convert it to internal form. As a simple sanity | |
3630 | check, verify that the what BFD thinks is the size of each program | |
3631 | header table entry actually matches the size recorded in the file. */ | |
3632 | ||
3633 | if (i_ehdrp->e_phentsize != sizeof (x_phdr)) | |
3634 | goto wrong; | |
3635 | i_phdrp = (Elf_Internal_Phdr *) | |
3636 | bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum); | |
3637 | if (!i_phdrp) | |
244ffee7 | 3638 | { |
d1ad85a6 | 3639 | bfd_set_error (bfd_error_no_memory); |
32090b8e KR |
3640 | return NULL; |
3641 | } | |
3642 | if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1) | |
25057836 | 3643 | return NULL; |
32090b8e KR |
3644 | for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++) |
3645 | { | |
3646 | if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd) | |
3647 | != sizeof (x_phdr)) | |
25057836 | 3648 | return NULL; |
32090b8e | 3649 | elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex); |
244ffee7 JK |
3650 | } |
3651 | ||
32090b8e KR |
3652 | /* Once all of the program headers have been read and converted, we |
3653 | can start processing them. */ | |
244ffee7 | 3654 | |
32090b8e KR |
3655 | for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++) |
3656 | { | |
3657 | bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex); | |
3658 | if ((i_phdrp + phindex)->p_type == PT_NOTE) | |
3659 | { | |
2e03ce18 ILT |
3660 | if (! elf_corefile_note (abfd, i_phdrp + phindex)) |
3661 | return NULL; | |
32090b8e KR |
3662 | } |
3663 | } | |
244ffee7 | 3664 | |
32090b8e | 3665 | /* Remember the entry point specified in the ELF file header. */ |
244ffee7 | 3666 | |
32090b8e | 3667 | bfd_get_start_address (abfd) = i_ehdrp->e_entry; |
244ffee7 | 3668 | |
32090b8e | 3669 | return abfd->xvec; |
244ffee7 | 3670 | } |
6ec3bb6a ILT |
3671 | \f |
3672 | /* ELF linker code. */ | |
3673 | ||
3674 | static boolean elf_link_add_object_symbols | |
3675 | PARAMS ((bfd *, struct bfd_link_info *)); | |
3676 | static boolean elf_link_add_archive_symbols | |
3677 | PARAMS ((bfd *, struct bfd_link_info *)); | |
374d2ef9 ILT |
3678 | static Elf_Internal_Rela *elf_link_read_relocs |
3679 | PARAMS ((bfd *, asection *, PTR, Elf_Internal_Rela *, boolean)); | |
013dec1a ILT |
3680 | static boolean elf_adjust_dynamic_symbol |
3681 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
6ec3bb6a ILT |
3682 | |
3683 | /* Given an ELF BFD, add symbols to the global hash table as | |
3684 | appropriate. */ | |
3685 | ||
3686 | boolean | |
3687 | elf_bfd_link_add_symbols (abfd, info) | |
3688 | bfd *abfd; | |
3689 | struct bfd_link_info *info; | |
3690 | { | |
3691 | switch (bfd_get_format (abfd)) | |
3692 | { | |
3693 | case bfd_object: | |
3694 | return elf_link_add_object_symbols (abfd, info); | |
3695 | case bfd_archive: | |
3696 | return elf_link_add_archive_symbols (abfd, info); | |
3697 | default: | |
3698 | bfd_set_error (bfd_error_wrong_format); | |
3699 | return false; | |
3700 | } | |
3701 | } | |
3702 | ||
3703 | /* Add symbols from an ELF archive file to the linker hash table. We | |
3704 | don't use _bfd_generic_link_add_archive_symbols because of a | |
3705 | problem which arises on UnixWare. The UnixWare libc.so is an | |
3706 | archive which includes an entry libc.so.1 which defines a bunch of | |
3707 | symbols. The libc.so archive also includes a number of other | |
3708 | object files, which also define symbols, some of which are the same | |
3709 | as those defined in libc.so.1. Correct linking requires that we | |
3710 | consider each object file in turn, and include it if it defines any | |
3711 | symbols we need. _bfd_generic_link_add_archive_symbols does not do | |
3712 | this; it looks through the list of undefined symbols, and includes | |
3713 | any object file which defines them. When this algorithm is used on | |
3714 | UnixWare, it winds up pulling in libc.so.1 early and defining a | |
3715 | bunch of symbols. This means that some of the other objects in the | |
3716 | archive are not included in the link, which is incorrect since they | |
3717 | precede libc.so.1 in the archive. | |
3718 | ||
3719 | Fortunately, ELF archive handling is simpler than that done by | |
3720 | _bfd_generic_link_add_archive_symbols, which has to allow for a.out | |
3721 | oddities. In ELF, if we find a symbol in the archive map, and the | |
3722 | symbol is currently undefined, we know that we must pull in that | |
3723 | object file. | |
3724 | ||
3725 | Unfortunately, we do have to make multiple passes over the symbol | |
3726 | table until nothing further is resolved. */ | |
3727 | ||
3728 | static boolean | |
3729 | elf_link_add_archive_symbols (abfd, info) | |
3730 | bfd *abfd; | |
3731 | struct bfd_link_info *info; | |
3732 | { | |
3733 | symindex c; | |
3734 | boolean *defined = NULL; | |
3735 | boolean *included = NULL; | |
3736 | carsym *symdefs; | |
3737 | boolean loop; | |
3738 | ||
3739 | if (! bfd_has_map (abfd)) | |
3740 | { | |
54f16fc4 ILT |
3741 | /* An empty archive is a special case. */ |
3742 | if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL) | |
3743 | return true; | |
6ec3bb6a ILT |
3744 | bfd_set_error (bfd_error_no_symbols); |
3745 | return false; | |
3746 | } | |
3747 | ||
3748 | /* Keep track of all symbols we know to be already defined, and all | |
3749 | files we know to be already included. This is to speed up the | |
3750 | second and subsequent passes. */ | |
3751 | c = bfd_ardata (abfd)->symdef_count; | |
3752 | if (c == 0) | |
3753 | return true; | |
3754 | defined = (boolean *) malloc (c * sizeof (boolean)); | |
3755 | included = (boolean *) malloc (c * sizeof (boolean)); | |
3756 | if (defined == (boolean *) NULL || included == (boolean *) NULL) | |
3757 | { | |
3758 | bfd_set_error (bfd_error_no_memory); | |
3759 | goto error_return; | |
3760 | } | |
3761 | memset (defined, 0, c * sizeof (boolean)); | |
3762 | memset (included, 0, c * sizeof (boolean)); | |
3763 | ||
3764 | symdefs = bfd_ardata (abfd)->symdefs; | |
3765 | ||
3766 | do | |
3767 | { | |
3768 | file_ptr last; | |
3769 | symindex i; | |
3770 | carsym *symdef; | |
3771 | carsym *symdefend; | |
3772 | ||
3773 | loop = false; | |
3774 | last = -1; | |
3775 | ||
3776 | symdef = symdefs; | |
3777 | symdefend = symdef + c; | |
3778 | for (i = 0; symdef < symdefend; symdef++, i++) | |
3779 | { | |
3780 | struct elf_link_hash_entry *h; | |
3781 | bfd *element; | |
3782 | struct bfd_link_hash_entry *undefs_tail; | |
3783 | symindex mark; | |
3784 | ||
3785 | if (defined[i] || included[i]) | |
3786 | continue; | |
3787 | if (symdef->file_offset == last) | |
3788 | { | |
3789 | included[i] = true; | |
3790 | continue; | |
3791 | } | |
3792 | ||
3793 | h = elf_link_hash_lookup (elf_hash_table (info), symdef->name, | |
3794 | false, false, false); | |
3795 | if (h == (struct elf_link_hash_entry *) NULL) | |
3796 | continue; | |
3797 | if (h->root.type != bfd_link_hash_undefined) | |
3798 | { | |
3799 | defined[i] = true; | |
3800 | continue; | |
3801 | } | |
3802 | ||
3803 | /* We need to include this archive member. */ | |
3804 | ||
3805 | element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); | |
3806 | if (element == (bfd *) NULL) | |
3807 | goto error_return; | |
3808 | ||
3809 | if (! bfd_check_format (element, bfd_object)) | |
3810 | goto error_return; | |
3811 | ||
3812 | /* Doublecheck that we have not included this object | |
3813 | already--it should be impossible, but there may be | |
3814 | something wrong with the archive. */ | |
3815 | if (element->archive_pass != 0) | |
3816 | { | |
3817 | bfd_set_error (bfd_error_bad_value); | |
3818 | goto error_return; | |
3819 | } | |
3820 | element->archive_pass = 1; | |
3821 | ||
3822 | undefs_tail = info->hash->undefs_tail; | |
3823 | ||
3824 | if (! (*info->callbacks->add_archive_element) (info, element, | |
3825 | symdef->name)) | |
3826 | goto error_return; | |
3827 | if (! elf_link_add_object_symbols (element, info)) | |
3828 | goto error_return; | |
3829 | ||
3830 | /* If there are any new undefined symbols, we need to make | |
3831 | another pass through the archive in order to see whether | |
3832 | they can be defined. FIXME: This isn't perfect, because | |
3833 | common symbols wind up on undefs_tail and because an | |
3834 | undefined symbol which is defined later on in this pass | |
3835 | does not require another pass. This isn't a bug, but it | |
3836 | does make the code less efficient than it could be. */ | |
3837 | if (undefs_tail != info->hash->undefs_tail) | |
3838 | loop = true; | |
3839 | ||
3840 | /* Look backward to mark all symbols from this object file | |
3841 | which we have already seen in this pass. */ | |
3842 | mark = i; | |
3843 | do | |
3844 | { | |
3845 | included[mark] = true; | |
3846 | if (mark == 0) | |
3847 | break; | |
3848 | --mark; | |
3849 | } | |
3850 | while (symdefs[mark].file_offset == symdef->file_offset); | |
3851 | ||
3852 | /* We mark subsequent symbols from this object file as we go | |
3853 | on through the loop. */ | |
3854 | last = symdef->file_offset; | |
3855 | } | |
3856 | } | |
3857 | while (loop); | |
3858 | ||
3859 | free (defined); | |
3860 | free (included); | |
3861 | ||
3862 | return true; | |
3863 | ||
3864 | error_return: | |
3865 | if (defined != (boolean *) NULL) | |
3866 | free (defined); | |
3867 | if (included != (boolean *) NULL) | |
3868 | free (included); | |
3869 | return false; | |
3870 | } | |
3871 | ||
013dec1a ILT |
3872 | /* Record a new dynamic symbol. We record the dynamic symbols as we |
3873 | read the input files, since we need to have a list of all of them | |
12662be4 ILT |
3874 | before we can determine the final sizes of the output sections. |
3875 | Note that we may actually call this function even though we are not | |
3876 | going to output any dynamic symbols; in some cases we know that a | |
3877 | symbol should be in the dynamic symbol table, but only if there is | |
3878 | one. */ | |
013dec1a | 3879 | |
12662be4 | 3880 | boolean |
013dec1a ILT |
3881 | elf_link_record_dynamic_symbol (info, h) |
3882 | struct bfd_link_info *info; | |
3883 | struct elf_link_hash_entry *h; | |
3884 | { | |
3885 | if (h->dynindx == -1) | |
3886 | { | |
12662be4 ILT |
3887 | struct bfd_strtab_hash *dynstr; |
3888 | ||
013dec1a ILT |
3889 | h->dynindx = elf_hash_table (info)->dynsymcount; |
3890 | ++elf_hash_table (info)->dynsymcount; | |
12662be4 ILT |
3891 | |
3892 | dynstr = elf_hash_table (info)->dynstr; | |
3893 | if (dynstr == NULL) | |
3894 | { | |
3895 | /* Create a strtab to hold the dynamic symbol names. */ | |
3896 | elf_hash_table (info)->dynstr = dynstr = elf_stringtab_init (); | |
3897 | if (dynstr == NULL) | |
3898 | return false; | |
3899 | } | |
3900 | ||
3901 | h->dynstr_index = ((unsigned long) | |
3902 | _bfd_stringtab_add (dynstr, h->root.root.string, | |
3903 | true, false)); | |
013dec1a ILT |
3904 | if (h->dynstr_index == (unsigned long) -1) |
3905 | return false; | |
3906 | } | |
3907 | ||
3908 | return true; | |
3909 | } | |
3910 | ||
6ec3bb6a ILT |
3911 | /* Add symbols from an ELF object file to the linker hash table. */ |
3912 | ||
3913 | static boolean | |
3914 | elf_link_add_object_symbols (abfd, info) | |
3915 | bfd *abfd; | |
3916 | struct bfd_link_info *info; | |
3917 | { | |
3918 | boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *, | |
3919 | const Elf_Internal_Sym *, | |
3920 | const char **, flagword *, | |
3921 | asection **, bfd_vma *)); | |
374d2ef9 ILT |
3922 | boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *, |
3923 | asection *, const Elf_Internal_Rela *)); | |
6ec3bb6a ILT |
3924 | boolean collect; |
3925 | Elf_Internal_Shdr *hdr; | |
3926 | size_t symcount; | |
3927 | size_t extsymcount; | |
5315c428 | 3928 | size_t extsymoff; |
6ec3bb6a ILT |
3929 | Elf_External_Sym *buf = NULL; |
3930 | struct elf_link_hash_entry **sym_hash; | |
013dec1a ILT |
3931 | boolean dynamic; |
3932 | Elf_External_Dyn *dynbuf = NULL; | |
3933 | struct elf_link_hash_entry *weaks; | |
6ec3bb6a ILT |
3934 | Elf_External_Sym *esym; |
3935 | Elf_External_Sym *esymend; | |
3936 | ||
3937 | add_symbol_hook = get_elf_backend_data (abfd)->elf_add_symbol_hook; | |
3938 | collect = get_elf_backend_data (abfd)->collect; | |
3939 | ||
1c640609 ILT |
3940 | /* A stripped shared library might only have a dynamic symbol table, |
3941 | not a regular symbol table. In that case we can still go ahead | |
3942 | and link using the dynamic symbol table. */ | |
3943 | if (elf_onesymtab (abfd) == 0 | |
3944 | && elf_dynsymtab (abfd) != 0) | |
3945 | { | |
3946 | elf_onesymtab (abfd) = elf_dynsymtab (abfd); | |
3947 | elf_tdata (abfd)->symtab_hdr = elf_tdata (abfd)->dynsymtab_hdr; | |
3948 | } | |
3949 | ||
6ec3bb6a ILT |
3950 | hdr = &elf_tdata (abfd)->symtab_hdr; |
3951 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); | |
3952 | ||
3953 | /* The sh_info field of the symtab header tells us where the | |
3954 | external symbols start. We don't care about the local symbols at | |
3955 | this point. */ | |
5315c428 ILT |
3956 | if (elf_bad_symtab (abfd)) |
3957 | { | |
3958 | extsymcount = symcount; | |
3959 | extsymoff = 0; | |
3960 | } | |
3961 | else | |
3962 | { | |
3963 | extsymcount = symcount - hdr->sh_info; | |
3964 | extsymoff = hdr->sh_info; | |
3965 | } | |
6ec3bb6a ILT |
3966 | |
3967 | buf = (Elf_External_Sym *) malloc (extsymcount * sizeof (Elf_External_Sym)); | |
3968 | if (buf == NULL && extsymcount != 0) | |
3969 | { | |
3970 | bfd_set_error (bfd_error_no_memory); | |
3971 | goto error_return; | |
3972 | } | |
3973 | ||
013dec1a ILT |
3974 | /* We store a pointer to the hash table entry for each external |
3975 | symbol. */ | |
6ec3bb6a ILT |
3976 | sym_hash = ((struct elf_link_hash_entry **) |
3977 | bfd_alloc (abfd, | |
3978 | extsymcount * sizeof (struct elf_link_hash_entry *))); | |
3979 | if (sym_hash == NULL) | |
3980 | { | |
3981 | bfd_set_error (bfd_error_no_memory); | |
3982 | goto error_return; | |
3983 | } | |
3984 | elf_sym_hashes (abfd) = sym_hash; | |
3985 | ||
013dec1a | 3986 | if (elf_elfheader (abfd)->e_type != ET_DYN) |
8af74670 ILT |
3987 | { |
3988 | dynamic = false; | |
3989 | ||
3990 | /* If we are creating a shared library, create all the dynamic | |
3991 | sections immediately. We need to attach them to something, | |
3992 | so we attach them to this BFD, provided it is the right | |
3993 | format. FIXME: If there are no input BFD's of the same | |
3994 | format as the output, we can't make a shared library. */ | |
3995 | if (info->shared | |
12662be4 | 3996 | && ! elf_hash_table (info)->dynamic_sections_created |
8af74670 ILT |
3997 | && abfd->xvec == info->hash->creator) |
3998 | { | |
3999 | if (! elf_link_create_dynamic_sections (abfd, info)) | |
4000 | goto error_return; | |
8af74670 ILT |
4001 | } |
4002 | } | |
013dec1a ILT |
4003 | else |
4004 | { | |
4005 | asection *s; | |
4006 | const char *name; | |
eb4267a3 | 4007 | bfd_size_type strindex; |
013dec1a ILT |
4008 | |
4009 | dynamic = true; | |
4010 | ||
374d2ef9 ILT |
4011 | /* You can't use -r against a dynamic object. Also, there's no |
4012 | hope of using a dynamic object which does not exactly match | |
4013 | the format of the output file. */ | |
013dec1a ILT |
4014 | if (info->relocateable |
4015 | || info->hash->creator != abfd->xvec) | |
4016 | { | |
4017 | bfd_set_error (bfd_error_invalid_operation); | |
4018 | goto error_return; | |
4019 | } | |
4020 | ||
4021 | /* Find the name to use in a DT_NEEDED entry that refers to this | |
4022 | object. If the object has a DT_SONAME entry, we use it. | |
c1f84521 ILT |
4023 | Otherwise, if the generic linker stuck something in |
4024 | elf_dt_needed_name, we use that. Otherwise, we just use the | |
4025 | file name. */ | |
013dec1a | 4026 | name = bfd_get_filename (abfd); |
c1f84521 ILT |
4027 | if (elf_dt_needed_name (abfd) != NULL) |
4028 | name = elf_dt_needed_name (abfd); | |
013dec1a ILT |
4029 | s = bfd_get_section_by_name (abfd, ".dynamic"); |
4030 | if (s != NULL) | |
4031 | { | |
4032 | Elf_External_Dyn *extdyn; | |
4033 | Elf_External_Dyn *extdynend; | |
4034 | ||
4035 | dynbuf = (Elf_External_Dyn *) malloc (s->_raw_size); | |
4036 | if (dynbuf == NULL) | |
4037 | { | |
4038 | bfd_set_error (bfd_error_no_memory); | |
4039 | goto error_return; | |
4040 | } | |
4041 | ||
4042 | if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, | |
4043 | (file_ptr) 0, s->_raw_size)) | |
4044 | goto error_return; | |
4045 | ||
4046 | extdyn = dynbuf; | |
4047 | extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn); | |
4048 | for (; extdyn < extdynend; extdyn++) | |
4049 | { | |
4050 | Elf_Internal_Dyn dyn; | |
4051 | ||
4052 | elf_swap_dyn_in (abfd, extdyn, &dyn); | |
4053 | if (dyn.d_tag == DT_SONAME) | |
4054 | { | |
4055 | int elfsec; | |
4056 | unsigned long link; | |
4057 | ||
4058 | elfsec = elf_section_from_bfd_section (abfd, s); | |
4059 | if (elfsec == -1) | |
4060 | goto error_return; | |
4061 | link = elf_elfsections (abfd)[elfsec]->sh_link; | |
4062 | name = elf_string_from_elf_section (abfd, link, | |
4063 | dyn.d_un.d_val); | |
4064 | if (name == NULL) | |
4065 | goto error_return; | |
4066 | ||
4067 | break; | |
4068 | } | |
4069 | } | |
4070 | ||
4071 | free (dynbuf); | |
4072 | dynbuf = NULL; | |
4073 | } | |
4074 | ||
4075 | /* We do not want to include any of the sections in a dynamic | |
4076 | object in the output file. We hack by simply clobbering the | |
4077 | list of sections in the BFD. This could be handled more | |
4078 | cleanly by, say, a new section flag; the existing | |
4079 | SEC_NEVER_LOAD flag is not the one we want, because that one | |
4080 | still implies that the section takes up space in the output | |
4081 | file. */ | |
4082 | abfd->sections = NULL; | |
4083 | ||
4084 | /* If this is the first dynamic object found in the link, create | |
12662be4 ILT |
4085 | the special sections required for dynamic linking. */ |
4086 | if (! elf_hash_table (info)->dynamic_sections_created) | |
013dec1a ILT |
4087 | { |
4088 | if (! elf_link_create_dynamic_sections (abfd, info)) | |
4089 | goto error_return; | |
013dec1a ILT |
4090 | } |
4091 | ||
4092 | /* Add a DT_NEEDED entry for this dynamic object. */ | |
eb4267a3 ILT |
4093 | strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name, |
4094 | true, false); | |
4095 | if (strindex == (bfd_size_type) -1) | |
013dec1a ILT |
4096 | goto error_return; |
4097 | if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex)) | |
4098 | goto error_return; | |
4099 | } | |
4100 | ||
6ec3bb6a | 4101 | if (bfd_seek (abfd, |
5315c428 | 4102 | hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym), |
6ec3bb6a ILT |
4103 | SEEK_SET) != 0 |
4104 | || (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd) | |
4105 | != extsymcount * sizeof (Elf_External_Sym))) | |
4106 | goto error_return; | |
4107 | ||
013dec1a ILT |
4108 | weaks = NULL; |
4109 | ||
6ec3bb6a ILT |
4110 | esymend = buf + extsymcount; |
4111 | for (esym = buf; esym < esymend; esym++, sym_hash++) | |
4112 | { | |
4113 | Elf_Internal_Sym sym; | |
4114 | int bind; | |
4115 | bfd_vma value; | |
4116 | asection *sec; | |
4117 | flagword flags; | |
4118 | const char *name; | |
013dec1a ILT |
4119 | struct elf_link_hash_entry *h = NULL; |
4120 | boolean definition; | |
6ec3bb6a ILT |
4121 | |
4122 | elf_swap_symbol_in (abfd, esym, &sym); | |
4123 | ||
4124 | flags = BSF_NO_FLAGS; | |
4125 | sec = NULL; | |
4126 | value = sym.st_value; | |
4127 | *sym_hash = NULL; | |
4128 | ||
4129 | bind = ELF_ST_BIND (sym.st_info); | |
4130 | if (bind == STB_LOCAL) | |
4131 | { | |
4132 | /* This should be impossible, since ELF requires that all | |
4133 | global symbols follow all local symbols, and that sh_info | |
5315c428 ILT |
4134 | point to the first global symbol. Unfortunatealy, Irix 5 |
4135 | screws this up. */ | |
4136 | continue; | |
6ec3bb6a ILT |
4137 | } |
4138 | else if (bind == STB_GLOBAL) | |
4139 | flags = BSF_GLOBAL; | |
4140 | else if (bind == STB_WEAK) | |
4141 | flags = BSF_WEAK; | |
4142 | else | |
4143 | { | |
4144 | /* Leave it up to the processor backend. */ | |
4145 | } | |
4146 | ||
4147 | if (sym.st_shndx == SHN_UNDEF) | |
badd23e3 | 4148 | sec = bfd_und_section_ptr; |
6ec3bb6a ILT |
4149 | else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE) |
4150 | { | |
4151 | sec = section_from_elf_index (abfd, sym.st_shndx); | |
24f13b03 ILT |
4152 | if (sec != NULL) |
4153 | value -= sec->vma; | |
4154 | else | |
4155 | sec = bfd_abs_section_ptr; | |
6ec3bb6a ILT |
4156 | } |
4157 | else if (sym.st_shndx == SHN_ABS) | |
badd23e3 | 4158 | sec = bfd_abs_section_ptr; |
6ec3bb6a ILT |
4159 | else if (sym.st_shndx == SHN_COMMON) |
4160 | { | |
badd23e3 | 4161 | sec = bfd_com_section_ptr; |
6ec3bb6a ILT |
4162 | /* What ELF calls the size we call the value. What ELF |
4163 | calls the value we call the alignment. */ | |
4164 | value = sym.st_size; | |
4165 | } | |
4166 | else | |
4167 | { | |
4168 | /* Leave it up to the processor backend. */ | |
4169 | } | |
4170 | ||
4171 | name = elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name); | |
4172 | if (name == (const char *) NULL) | |
4173 | goto error_return; | |
4174 | ||
4175 | if (add_symbol_hook) | |
4176 | { | |
4177 | if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec, | |
4178 | &value)) | |
4179 | goto error_return; | |
4180 | ||
4181 | /* The hook function sets the name to NULL if this symbol | |
4182 | should be skipped for some reason. */ | |
4183 | if (name == (const char *) NULL) | |
4184 | continue; | |
4185 | } | |
4186 | ||
4187 | /* Sanity check that all possibilities were handled. */ | |
4188 | if (flags == BSF_NO_FLAGS || sec == (asection *) NULL) | |
4189 | { | |
4190 | bfd_set_error (bfd_error_bad_value); | |
4191 | goto error_return; | |
4192 | } | |
4193 | ||
badd23e3 | 4194 | if (bfd_is_und_section (sec) |
013dec1a ILT |
4195 | || bfd_is_com_section (sec)) |
4196 | definition = false; | |
4197 | else | |
4198 | definition = true; | |
4199 | ||
4200 | if (info->hash->creator->flavour == bfd_target_elf_flavour) | |
4201 | { | |
4202 | /* We need to look up the symbol now in order to get some of | |
4203 | the dynamic object handling right. We pass the hash | |
4204 | table entry in to _bfd_generic_link_add_one_symbol so | |
4205 | that it does not have to look it up again. */ | |
4206 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
4207 | true, false, false); | |
4208 | if (h == NULL) | |
4209 | goto error_return; | |
4210 | *sym_hash = h; | |
4211 | ||
4212 | /* If we are looking at a dynamic object, and this is a | |
4213 | definition, we need to see if it has already been defined | |
4214 | by some other object. If it has, we want to use the | |
4215 | existing definition, and we do not want to report a | |
4216 | multiple symbol definition error; we do this by | |
badd23e3 | 4217 | clobbering sec to be bfd_und_section_ptr. */ |
013dec1a ILT |
4218 | if (dynamic && definition) |
4219 | { | |
4220 | if (h->root.type == bfd_link_hash_defined) | |
badd23e3 | 4221 | sec = bfd_und_section_ptr; |
013dec1a ILT |
4222 | } |
4223 | ||
4224 | /* Similarly, if we are not looking at a dynamic object, and | |
4225 | we have a definition, we want to override any definition | |
4226 | we may have from a dynamic object. Symbols from regular | |
4227 | files always take precedence over symbols from dynamic | |
4228 | objects, even if they are defined after the dynamic | |
4229 | object in the link. */ | |
4230 | if (! dynamic | |
4231 | && definition | |
4232 | && h->root.type == bfd_link_hash_defined | |
4233 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
4234 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
4235 | == bfd_target_elf_flavour) | |
4236 | && (elf_elfheader (h->root.u.def.section->owner)->e_type | |
4237 | == ET_DYN)) | |
4238 | { | |
4239 | /* Change the hash table entry to undefined, and let | |
4240 | _bfd_generic_link_add_one_symbol do the right thing | |
4241 | with the new definition. */ | |
4242 | h->root.type = bfd_link_hash_undefined; | |
4243 | h->root.u.undef.abfd = h->root.u.def.section->owner; | |
bf73e4f3 | 4244 | h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEFINED_WEAK; |
013dec1a | 4245 | } |
bf73e4f3 ILT |
4246 | |
4247 | /* If this is a weak definition which we are going to use, | |
4248 | and the symbol is currently undefined, record that the | |
4249 | definition is weak. */ | |
4250 | if (definition | |
4251 | && (flags & BSF_WEAK) != 0 | |
4252 | && ! bfd_is_und_section (sec) | |
4253 | && (h->root.type == bfd_link_hash_new | |
4254 | || h->root.type == bfd_link_hash_undefined | |
4255 | || h->root.type == bfd_link_hash_weak)) | |
4256 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEFINED_WEAK; | |
013dec1a ILT |
4257 | } |
4258 | ||
4259 | if (! (_bfd_generic_link_add_one_symbol | |
4260 | (info, abfd, name, flags, sec, value, (const char *) NULL, | |
4261 | false, collect, (struct bfd_link_hash_entry **) sym_hash))) | |
6ec3bb6a ILT |
4262 | goto error_return; |
4263 | ||
013dec1a ILT |
4264 | if (dynamic |
4265 | && definition | |
4266 | && (flags & BSF_WEAK) != 0 | |
4267 | && ELF_ST_TYPE (sym.st_info) != STT_FUNC | |
4268 | && (*sym_hash)->weakdef == NULL) | |
4269 | { | |
4270 | /* Keep a list of all weak defined non function symbols from | |
4271 | a dynamic object, using the weakdef field. Later in this | |
4272 | function we will set the weakdef field to the correct | |
4273 | value. We only put non-function symbols from dynamic | |
4274 | objects on this list, because that happens to be the only | |
4275 | time we need to know the normal symbol corresponding to a | |
4276 | weak symbol, and the information is time consuming to | |
4277 | figure out. If the weakdef field is not already NULL, | |
4278 | then this symbol was already defined by some previous | |
4279 | dynamic object, and we will be using that previous | |
4280 | definition anyhow. */ | |
4281 | ||
4282 | (*sym_hash)->weakdef = weaks; | |
4283 | weaks = *sym_hash; | |
4284 | } | |
4285 | ||
374d2ef9 | 4286 | /* Get the alignment of a common symbol. */ |
7c6da9ca ILT |
4287 | if (sym.st_shndx == SHN_COMMON |
4288 | && h->root.type == bfd_link_hash_common) | |
4289 | h->root.u.c.alignment_power = bfd_log2 (sym.st_value); | |
4290 | ||
6ec3bb6a ILT |
4291 | if (info->hash->creator->flavour == bfd_target_elf_flavour) |
4292 | { | |
013dec1a ILT |
4293 | int old_flags; |
4294 | boolean dynsym; | |
4295 | int new_flag; | |
4296 | ||
374d2ef9 | 4297 | /* Remember the symbol size and type. */ |
6ec3bb6a ILT |
4298 | if (sym.st_size != 0) |
4299 | { | |
4300 | /* FIXME: We should probably somehow give a warning if | |
4301 | the symbol size changes. */ | |
013dec1a | 4302 | h->size = sym.st_size; |
6ec3bb6a | 4303 | } |
6ec3bb6a ILT |
4304 | if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE) |
4305 | { | |
4306 | /* FIXME: We should probably somehow give a warning if | |
4307 | the symbol type changes. */ | |
013dec1a ILT |
4308 | h->type = ELF_ST_TYPE (sym.st_info); |
4309 | } | |
4310 | ||
4311 | /* Set a flag in the hash table entry indicating the type of | |
4312 | reference or definition we just found. Keep a count of | |
4313 | the number of dynamic symbols we find. A dynamic symbol | |
4314 | is one which is referenced or defined by both a regular | |
4315 | object and a shared object, or one which is referenced or | |
4316 | defined by more than one shared object. */ | |
4317 | old_flags = h->elf_link_hash_flags; | |
4318 | dynsym = false; | |
4319 | if (! dynamic) | |
4320 | { | |
4321 | if (! definition) | |
4322 | new_flag = ELF_LINK_HASH_REF_REGULAR; | |
4323 | else | |
4324 | new_flag = ELF_LINK_HASH_DEF_REGULAR; | |
374d2ef9 ILT |
4325 | if (info->shared |
4326 | || (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC | |
4327 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0) | |
013dec1a ILT |
4328 | dynsym = true; |
4329 | } | |
4330 | else | |
4331 | { | |
4332 | if (! definition) | |
4333 | new_flag = ELF_LINK_HASH_REF_DYNAMIC; | |
4334 | else | |
4335 | new_flag = ELF_LINK_HASH_DEF_DYNAMIC; | |
374d2ef9 ILT |
4336 | if ((old_flags & new_flag) != 0 |
4337 | || (old_flags & (ELF_LINK_HASH_DEF_REGULAR | |
4338 | | ELF_LINK_HASH_REF_REGULAR)) != 0) | |
4339 | dynsym = true; | |
013dec1a ILT |
4340 | } |
4341 | ||
4342 | h->elf_link_hash_flags |= new_flag; | |
4343 | if (dynsym && h->dynindx == -1) | |
4344 | { | |
4345 | if (! elf_link_record_dynamic_symbol (info, h)) | |
4346 | goto error_return; | |
4347 | } | |
4348 | } | |
4349 | } | |
4350 | ||
4351 | /* Now set the weakdefs field correctly for all the weak defined | |
4352 | symbols we found. The only way to do this is to search all the | |
4353 | symbols. Since we only need the information for non functions in | |
4354 | dynamic objects, that's the only time we actually put anything on | |
4355 | the list WEAKS. We need this information so that if a regular | |
4356 | object refers to a symbol defined weakly in a dynamic object, the | |
4357 | real symbol in the dynamic object is also put in the dynamic | |
4358 | symbols; we also must arrange for both symbols to point to the | |
4359 | same memory location. We could handle the general case of symbol | |
4360 | aliasing, but a general symbol alias can only be generated in | |
4361 | assembler code, handling it correctly would be very time | |
4362 | consuming, and other ELF linkers don't handle general aliasing | |
4363 | either. */ | |
4364 | while (weaks != NULL) | |
4365 | { | |
4366 | struct elf_link_hash_entry *hlook; | |
4367 | asection *slook; | |
4368 | bfd_vma vlook; | |
4369 | struct elf_link_hash_entry **hpp; | |
4370 | struct elf_link_hash_entry **hppend; | |
4371 | ||
4372 | hlook = weaks; | |
4373 | weaks = hlook->weakdef; | |
4374 | hlook->weakdef = NULL; | |
4375 | ||
4376 | BFD_ASSERT (hlook->root.type == bfd_link_hash_defined); | |
4377 | slook = hlook->root.u.def.section; | |
4378 | vlook = hlook->root.u.def.value; | |
4379 | ||
4380 | hpp = elf_sym_hashes (abfd); | |
4381 | hppend = hpp + extsymcount; | |
4382 | for (; hpp < hppend; hpp++) | |
4383 | { | |
4384 | struct elf_link_hash_entry *h; | |
4385 | ||
4386 | h = *hpp; | |
4387 | if (h != hlook | |
4388 | && h->root.type == bfd_link_hash_defined | |
4389 | && h->root.u.def.section == slook | |
4390 | && h->root.u.def.value == vlook) | |
4391 | { | |
4392 | hlook->weakdef = h; | |
4393 | ||
4394 | /* If the weak definition is in the list of dynamic | |
4395 | symbols, make sure the real definition is put there | |
4396 | as well. */ | |
4397 | if (hlook->dynindx != -1 | |
4398 | && h->dynindx == -1) | |
4399 | { | |
4400 | if (! elf_link_record_dynamic_symbol (info, h)) | |
4401 | goto error_return; | |
4402 | } | |
4403 | ||
4404 | break; | |
6ec3bb6a ILT |
4405 | } |
4406 | } | |
4407 | } | |
4408 | ||
4409 | if (buf != NULL) | |
374d2ef9 ILT |
4410 | { |
4411 | free (buf); | |
4412 | buf = NULL; | |
4413 | } | |
4414 | ||
4415 | /* If this object is the same format as the output object, and it is | |
4416 | not a shared library, then let the backend look through the | |
4417 | relocs. | |
4418 | ||
4419 | This is required to build global offset table entries and to | |
4420 | arrange for dynamic relocs. It is not required for the | |
4421 | particular common case of linking non PIC code, even when linking | |
4422 | against shared libraries, but unfortunately there is no way of | |
4423 | knowing whether an object file has been compiled PIC or not. | |
4424 | Looking through the relocs is not particularly time consuming. | |
4425 | The problem is that we must either (1) keep the relocs in memory, | |
4426 | which causes the linker to require additional runtime memory or | |
4427 | (2) read the relocs twice from the input file, which wastes time. | |
4428 | This would be a good case for using mmap. | |
4429 | ||
4430 | I have no idea how to handle linking PIC code into a file of a | |
4431 | different format. It probably can't be done. */ | |
4432 | check_relocs = get_elf_backend_data (abfd)->check_relocs; | |
4433 | if (! dynamic | |
4434 | && abfd->xvec == info->hash->creator | |
4435 | && check_relocs != NULL) | |
4436 | { | |
4437 | asection *o; | |
4438 | ||
4439 | for (o = abfd->sections; o != NULL; o = o->next) | |
4440 | { | |
4441 | Elf_Internal_Rela *internal_relocs; | |
4442 | boolean ok; | |
4443 | ||
4444 | if ((o->flags & SEC_RELOC) == 0 | |
4445 | || o->reloc_count == 0) | |
4446 | continue; | |
4447 | ||
4448 | /* I believe we can ignore the relocs for any section which | |
4449 | does not form part of the final process image, such as a | |
4450 | debugging section. */ | |
4451 | if ((o->flags & SEC_ALLOC) == 0) | |
4452 | continue; | |
4453 | ||
4454 | internal_relocs = elf_link_read_relocs (abfd, o, (PTR) NULL, | |
4455 | (Elf_Internal_Rela *) NULL, | |
4456 | info->keep_memory); | |
4457 | if (internal_relocs == NULL) | |
4458 | goto error_return; | |
4459 | ||
4460 | ok = (*check_relocs) (abfd, info, o, internal_relocs); | |
4461 | ||
4462 | if (! info->keep_memory) | |
4463 | free (internal_relocs); | |
4464 | ||
4465 | if (! ok) | |
4466 | goto error_return; | |
4467 | } | |
4468 | } | |
6ec3bb6a ILT |
4469 | |
4470 | return true; | |
4471 | ||
4472 | error_return: | |
4473 | if (buf != NULL) | |
4474 | free (buf); | |
013dec1a ILT |
4475 | if (dynbuf != NULL) |
4476 | free (dynbuf); | |
6ec3bb6a ILT |
4477 | return false; |
4478 | } | |
013dec1a ILT |
4479 | |
4480 | /* Create some sections which will be filled in with dynamic linking | |
12662be4 ILT |
4481 | information. ABFD is an input file which requires dynamic sections |
4482 | to be created. The dynamic sections take up virtual memory space | |
4483 | when the final executable is run, so we need to create them before | |
4484 | addresses are assigned to the output sections. We work out the | |
4485 | actual contents and size of these sections later. */ | |
013dec1a | 4486 | |
374d2ef9 | 4487 | boolean |
013dec1a ILT |
4488 | elf_link_create_dynamic_sections (abfd, info) |
4489 | bfd *abfd; | |
4490 | struct bfd_link_info *info; | |
4491 | { | |
4492 | flagword flags; | |
4493 | register asection *s; | |
4494 | struct elf_link_hash_entry *h; | |
4495 | struct elf_backend_data *bed; | |
4496 | ||
12662be4 ILT |
4497 | if (elf_hash_table (info)->dynamic_sections_created) |
4498 | return true; | |
4499 | ||
4500 | /* Make sure that all dynamic sections use the same input BFD. */ | |
4501 | if (elf_hash_table (info)->dynobj == NULL) | |
4502 | elf_hash_table (info)->dynobj = abfd; | |
4503 | else | |
4504 | abfd = elf_hash_table (info)->dynobj; | |
4505 | ||
013dec1a ILT |
4506 | /* Note that we set the SEC_IN_MEMORY flag for all of these |
4507 | sections. */ | |
4508 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
4509 | ||
8af74670 ILT |
4510 | /* A dynamically linked executable has a .interp section, but a |
4511 | shared library does not. */ | |
4512 | if (! info->shared) | |
4513 | { | |
4514 | s = bfd_make_section (abfd, ".interp"); | |
4515 | if (s == NULL | |
4516 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
4517 | return false; | |
4518 | } | |
013dec1a | 4519 | |
374d2ef9 ILT |
4520 | s = bfd_make_section (abfd, ".dynsym"); |
4521 | if (s == NULL | |
4522 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
4523 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
4524 | return false; | |
4525 | ||
374d2ef9 ILT |
4526 | s = bfd_make_section (abfd, ".dynstr"); |
4527 | if (s == NULL | |
4528 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)) | |
4529 | return false; | |
4530 | ||
4531 | /* Create a strtab to hold the dynamic symbol names. */ | |
374d2ef9 | 4532 | if (elf_hash_table (info)->dynstr == NULL) |
12662be4 ILT |
4533 | { |
4534 | elf_hash_table (info)->dynstr = elf_stringtab_init (); | |
4535 | if (elf_hash_table (info)->dynstr == NULL) | |
4536 | return false; | |
4537 | } | |
374d2ef9 | 4538 | |
013dec1a ILT |
4539 | s = bfd_make_section (abfd, ".dynamic"); |
4540 | if (s == NULL | |
4541 | || ! bfd_set_section_flags (abfd, s, flags) | |
4542 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
4543 | return false; | |
4544 | ||
4545 | /* The special symbol _DYNAMIC is always set to the start of the | |
4546 | .dynamic section. This call occurs before we have processed the | |
4547 | symbols for any dynamic object, so we don't have to worry about | |
4548 | overriding a dynamic definition. We could set _DYNAMIC in a | |
4549 | linker script, but we only want to define it if we are, in fact, | |
4550 | creating a .dynamic section. We don't want to define it if there | |
4551 | is no .dynamic section, since on some ELF platforms the start up | |
4552 | code examines it to decide how to initialize the process. */ | |
4553 | h = NULL; | |
4554 | if (! (_bfd_generic_link_add_one_symbol | |
4555 | (info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0, | |
4556 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, | |
4557 | (struct bfd_link_hash_entry **) &h))) | |
4558 | return false; | |
4559 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
99dd7754 JMD |
4560 | h->type = STT_OBJECT; |
4561 | ||
374d2ef9 ILT |
4562 | if (info->shared |
4563 | && ! elf_link_record_dynamic_symbol (info, h)) | |
013dec1a ILT |
4564 | return false; |
4565 | ||
4566 | s = bfd_make_section (abfd, ".hash"); | |
4567 | if (s == NULL | |
4568 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
4569 | || ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN)) | |
4570 | return false; | |
4571 | ||
4572 | /* Let the backend create the rest of the sections. This lets the | |
4573 | backend set the right flags. The backend will normally create | |
4574 | the .got and .plt sections. */ | |
4575 | bed = get_elf_backend_data (abfd); | |
12662be4 ILT |
4576 | if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) |
4577 | return false; | |
4578 | ||
4579 | elf_hash_table (info)->dynamic_sections_created = true; | |
4580 | ||
4581 | return true; | |
013dec1a ILT |
4582 | } |
4583 | ||
4584 | /* Add an entry to the .dynamic table. */ | |
4585 | ||
4586 | boolean | |
4587 | elf_add_dynamic_entry (info, tag, val) | |
4588 | struct bfd_link_info *info; | |
4589 | bfd_vma tag; | |
4590 | bfd_vma val; | |
4591 | { | |
4592 | Elf_Internal_Dyn dyn; | |
4593 | bfd *dynobj; | |
4594 | asection *s; | |
4595 | size_t newsize; | |
4596 | bfd_byte *newcontents; | |
4597 | ||
4598 | dynobj = elf_hash_table (info)->dynobj; | |
4599 | ||
4600 | s = bfd_get_section_by_name (dynobj, ".dynamic"); | |
4601 | BFD_ASSERT (s != NULL); | |
4602 | ||
4603 | newsize = s->_raw_size + sizeof (Elf_External_Dyn); | |
4604 | if (s->contents == NULL) | |
4605 | newcontents = (bfd_byte *) malloc (newsize); | |
4606 | else | |
4607 | newcontents = (bfd_byte *) realloc (s->contents, newsize); | |
4608 | if (newcontents == NULL) | |
4609 | { | |
4610 | bfd_set_error (bfd_error_no_memory); | |
4611 | return false; | |
4612 | } | |
4613 | ||
4614 | dyn.d_tag = tag; | |
4615 | dyn.d_un.d_val = val; | |
4616 | elf_swap_dyn_out (dynobj, &dyn, | |
4617 | (Elf_External_Dyn *) (newcontents + s->_raw_size)); | |
4618 | ||
4619 | s->_raw_size = newsize; | |
4620 | s->contents = newcontents; | |
4621 | ||
4622 | return true; | |
4623 | } | |
4624 | ||
374d2ef9 ILT |
4625 | /* Read and swap the relocs for a section. They may have been cached. |
4626 | If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are not NULL, | |
4627 | they are used as buffers to read into. They are known to be large | |
4628 | enough. If the INTERNAL_RELOCS relocs argument is NULL, the return | |
4629 | value is allocated using either malloc or bfd_alloc, according to | |
4630 | the KEEP_MEMORY argument. */ | |
4631 | ||
4632 | static Elf_Internal_Rela * | |
4633 | elf_link_read_relocs (abfd, o, external_relocs, internal_relocs, keep_memory) | |
4634 | bfd *abfd; | |
4635 | asection *o; | |
4636 | PTR external_relocs; | |
4637 | Elf_Internal_Rela *internal_relocs; | |
4638 | boolean keep_memory; | |
4639 | { | |
4640 | Elf_Internal_Shdr *rel_hdr; | |
4641 | PTR alloc1 = NULL; | |
4642 | Elf_Internal_Rela *alloc2 = NULL; | |
4643 | ||
4644 | if (elf_section_data (o)->relocs != NULL) | |
4645 | return elf_section_data (o)->relocs; | |
4646 | ||
4647 | if (o->reloc_count == 0) | |
4648 | return NULL; | |
4649 | ||
4650 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
4651 | ||
4652 | if (internal_relocs == NULL) | |
4653 | { | |
4654 | size_t size; | |
4655 | ||
4656 | size = o->reloc_count * sizeof (Elf_Internal_Rela); | |
4657 | if (keep_memory) | |
4658 | internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size); | |
4659 | else | |
4660 | internal_relocs = alloc2 = (Elf_Internal_Rela *) malloc (size); | |
4661 | if (internal_relocs == NULL) | |
4662 | { | |
4663 | bfd_set_error (bfd_error_no_memory); | |
4664 | goto error_return; | |
4665 | } | |
4666 | } | |
4667 | ||
4668 | if (external_relocs == NULL) | |
4669 | { | |
4670 | alloc1 = (PTR) malloc (rel_hdr->sh_size); | |
4671 | if (alloc1 == NULL) | |
4672 | { | |
4673 | bfd_set_error (bfd_error_no_memory); | |
4674 | goto error_return; | |
4675 | } | |
4676 | external_relocs = alloc1; | |
4677 | } | |
4678 | ||
4679 | if ((bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0) | |
4680 | || (bfd_read (external_relocs, 1, rel_hdr->sh_size, abfd) | |
4681 | != rel_hdr->sh_size)) | |
4682 | goto error_return; | |
4683 | ||
4684 | /* Swap in the relocs. For convenience, we always produce an | |
4685 | Elf_Internal_Rela array; if the relocs are Rel, we set the addend | |
4686 | to 0. */ | |
4687 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
4688 | { | |
4689 | Elf_External_Rel *erel; | |
4690 | Elf_External_Rel *erelend; | |
4691 | Elf_Internal_Rela *irela; | |
4692 | ||
4693 | erel = (Elf_External_Rel *) external_relocs; | |
4694 | erelend = erel + o->reloc_count; | |
4695 | irela = internal_relocs; | |
4696 | for (; erel < erelend; erel++, irela++) | |
4697 | { | |
4698 | Elf_Internal_Rel irel; | |
4699 | ||
4700 | elf_swap_reloc_in (abfd, erel, &irel); | |
4701 | irela->r_offset = irel.r_offset; | |
4702 | irela->r_info = irel.r_info; | |
4703 | irela->r_addend = 0; | |
4704 | } | |
4705 | } | |
4706 | else | |
4707 | { | |
4708 | Elf_External_Rela *erela; | |
4709 | Elf_External_Rela *erelaend; | |
4710 | Elf_Internal_Rela *irela; | |
4711 | ||
4712 | BFD_ASSERT (rel_hdr->sh_entsize == sizeof (Elf_External_Rela)); | |
4713 | ||
4714 | erela = (Elf_External_Rela *) external_relocs; | |
4715 | erelaend = erela + o->reloc_count; | |
4716 | irela = internal_relocs; | |
4717 | for (; erela < erelaend; erela++, irela++) | |
4718 | elf_swap_reloca_in (abfd, erela, irela); | |
4719 | } | |
4720 | ||
4721 | /* Cache the results for next time, if we can. */ | |
4722 | if (keep_memory) | |
4723 | elf_section_data (o)->relocs = internal_relocs; | |
4724 | ||
4725 | if (alloc1 != NULL) | |
4726 | free (alloc1); | |
4727 | ||
4728 | /* Don't free alloc2, since if it was allocated we are passing it | |
4729 | back (under the name of internal_relocs). */ | |
4730 | ||
4731 | return internal_relocs; | |
4732 | ||
4733 | error_return: | |
4734 | if (alloc1 != NULL) | |
4735 | free (alloc1); | |
4736 | if (alloc2 != NULL) | |
4737 | free (alloc2); | |
4738 | return NULL; | |
4739 | } | |
4740 | ||
013dec1a ILT |
4741 | /* Record an assignment to a symbol made by a linker script. We need |
4742 | this in case some dynamic object refers to this symbol. */ | |
4743 | ||
4744 | /*ARGSUSED*/ | |
4745 | boolean | |
4746 | NAME(bfd_elf,record_link_assignment) (output_bfd, info, name) | |
4747 | bfd *output_bfd; | |
4748 | struct bfd_link_info *info; | |
4749 | const char *name; | |
4750 | { | |
4751 | struct elf_link_hash_entry *h; | |
4752 | ||
99dd7754 | 4753 | h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false); |
013dec1a | 4754 | if (h == NULL) |
99dd7754 | 4755 | return false; |
013dec1a ILT |
4756 | |
4757 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
99dd7754 | 4758 | h->type = STT_OBJECT; |
013dec1a | 4759 | |
374d2ef9 ILT |
4760 | if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC |
4761 | | ELF_LINK_HASH_REF_DYNAMIC)) != 0 | |
4762 | || info->shared) | |
013dec1a ILT |
4763 | && h->dynindx == -1) |
4764 | { | |
4765 | if (! elf_link_record_dynamic_symbol (info, h)) | |
4766 | return false; | |
4767 | ||
4768 | /* If this is a weak defined symbol, and we know a corresponding | |
4769 | real symbol from the same dynamic object, make sure the real | |
4770 | symbol is also made into a dynamic symbol. */ | |
4771 | if (h->weakdef != NULL | |
4772 | && h->weakdef->dynindx == -1) | |
4773 | { | |
4774 | if (! elf_link_record_dynamic_symbol (info, h->weakdef)) | |
4775 | return false; | |
4776 | } | |
4777 | } | |
4778 | ||
4779 | return true; | |
4780 | } | |
4781 | ||
4782 | /* Array used to determine the number of hash table buckets to use | |
4783 | based on the number of symbols there are. If there are fewer than | |
4784 | 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, | |
4785 | fewer than 37 we use 17 buckets, and so forth. We never use more | |
4786 | than 521 buckets. */ | |
4787 | ||
4788 | static const size_t elf_buckets[] = | |
4789 | { | |
4790 | 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 0 | |
4791 | }; | |
4792 | ||
4793 | /* Set up the sizes and contents of the ELF dynamic sections. This is | |
4794 | called by the ELF linker emulation before_allocation routine. We | |
4795 | must set the sizes of the sections before the linker sets the | |
4796 | addresses of the various sections. */ | |
4797 | ||
4798 | boolean | |
0822b56d ILT |
4799 | NAME(bfd_elf,size_dynamic_sections) (output_bfd, soname, rpath, info, |
4800 | sinterpptr) | |
013dec1a | 4801 | bfd *output_bfd; |
0822b56d | 4802 | const char *soname; |
f9779aad | 4803 | const char *rpath; |
013dec1a | 4804 | struct bfd_link_info *info; |
7c726b66 | 4805 | asection **sinterpptr; |
013dec1a ILT |
4806 | { |
4807 | bfd *dynobj; | |
013dec1a ILT |
4808 | asection *s; |
4809 | Elf_Internal_Sym isym; | |
4810 | size_t i; | |
4811 | size_t bucketcount; | |
4812 | struct elf_backend_data *bed; | |
4813 | ||
7c726b66 ILT |
4814 | *sinterpptr = NULL; |
4815 | ||
013dec1a | 4816 | dynobj = elf_hash_table (info)->dynobj; |
013dec1a ILT |
4817 | |
4818 | /* If there were no dynamic objects in the link, there is nothing to | |
4819 | do here. */ | |
4820 | if (dynobj == NULL) | |
4821 | return true; | |
4822 | ||
12662be4 ILT |
4823 | if (elf_hash_table (info)->dynamic_sections_created) |
4824 | { | |
12662be4 ILT |
4825 | bfd_size_type strsize; |
4826 | ||
4827 | *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); | |
4828 | BFD_ASSERT (*sinterpptr != NULL || info->shared); | |
4829 | ||
12662be4 ILT |
4830 | if (soname != NULL) |
4831 | { | |
4832 | bfd_size_type indx; | |
0822b56d | 4833 | |
12662be4 ILT |
4834 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, soname, |
4835 | true, true); | |
4836 | if (indx == (bfd_size_type) -1 | |
4837 | || ! elf_add_dynamic_entry (info, DT_SONAME, indx)) | |
4838 | return false; | |
4839 | } | |
0822b56d | 4840 | |
12662be4 ILT |
4841 | if (rpath != NULL) |
4842 | { | |
4843 | bfd_size_type indx; | |
f9779aad | 4844 | |
12662be4 ILT |
4845 | indx = _bfd_stringtab_add (elf_hash_table (info)->dynstr, rpath, |
4846 | true, true); | |
4847 | if (indx == (bfd_size_type) -1 | |
4848 | || ! elf_add_dynamic_entry (info, DT_RPATH, indx)) | |
4849 | return false; | |
4850 | } | |
f9779aad | 4851 | |
12662be4 ILT |
4852 | /* Find all symbols which were defined in a dynamic object and make |
4853 | the backend pick a reasonable value for them. */ | |
4854 | elf_link_hash_traverse (elf_hash_table (info), | |
4855 | elf_adjust_dynamic_symbol, | |
4856 | (PTR) info); | |
013dec1a | 4857 | |
12662be4 ILT |
4858 | /* Add some entries to the .dynamic section. We fill in some of the |
4859 | values later, in elf_bfd_final_link, but we must add the entries | |
4860 | now so that we know the final size of the .dynamic section. */ | |
197e30e5 ILT |
4861 | if (elf_link_hash_lookup (elf_hash_table (info), "_init", false, |
4862 | false, false) != NULL) | |
12662be4 ILT |
4863 | { |
4864 | if (! elf_add_dynamic_entry (info, DT_INIT, 0)) | |
4865 | return false; | |
4866 | } | |
197e30e5 ILT |
4867 | if (elf_link_hash_lookup (elf_hash_table (info), "_fini", false, |
4868 | false, false) != NULL) | |
12662be4 ILT |
4869 | { |
4870 | if (! elf_add_dynamic_entry (info, DT_FINI, 0)) | |
4871 | return false; | |
4872 | } | |
4873 | strsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr); | |
4874 | if (! elf_add_dynamic_entry (info, DT_HASH, 0) | |
4875 | || ! elf_add_dynamic_entry (info, DT_STRTAB, 0) | |
4876 | || ! elf_add_dynamic_entry (info, DT_SYMTAB, 0) | |
4877 | || ! elf_add_dynamic_entry (info, DT_STRSZ, strsize) | |
4878 | || ! elf_add_dynamic_entry (info, DT_SYMENT, | |
4879 | sizeof (Elf_External_Sym))) | |
013dec1a ILT |
4880 | return false; |
4881 | } | |
013dec1a ILT |
4882 | |
4883 | /* The backend must work out the sizes of all the other dynamic | |
4884 | sections. */ | |
4885 | bed = get_elf_backend_data (output_bfd); | |
4886 | if (! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) | |
4887 | return false; | |
4888 | ||
12662be4 ILT |
4889 | if (elf_hash_table (info)->dynamic_sections_created) |
4890 | { | |
14cac507 ILT |
4891 | size_t dynsymcount; |
4892 | ||
4893 | /* Set the size of the .dynsym and .hash sections. We counted | |
4894 | the number of dynamic symbols in elf_link_add_object_symbols. | |
4895 | We will build the contents of .dynsym and .hash when we build | |
4896 | the final symbol table, because until then we do not know the | |
4897 | correct value to give the symbols. We built the .dynstr | |
4898 | section as we went along in elf_link_add_object_symbols. */ | |
4899 | dynsymcount = elf_hash_table (info)->dynsymcount; | |
4900 | s = bfd_get_section_by_name (dynobj, ".dynsym"); | |
4901 | BFD_ASSERT (s != NULL); | |
4902 | s->_raw_size = dynsymcount * sizeof (Elf_External_Sym); | |
4903 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
4904 | if (s->contents == NULL && s->_raw_size != 0) | |
4905 | { | |
4906 | bfd_set_error (bfd_error_no_memory); | |
4907 | return false; | |
4908 | } | |
4909 | ||
4910 | /* The first entry in .dynsym is a dummy symbol. */ | |
4911 | isym.st_value = 0; | |
4912 | isym.st_size = 0; | |
4913 | isym.st_name = 0; | |
4914 | isym.st_info = 0; | |
4915 | isym.st_other = 0; | |
4916 | isym.st_shndx = 0; | |
4917 | elf_swap_symbol_out (output_bfd, &isym, | |
4918 | (Elf_External_Sym *) s->contents); | |
4919 | ||
4920 | for (i = 0; elf_buckets[i] != 0; i++) | |
4921 | { | |
4922 | bucketcount = elf_buckets[i]; | |
4923 | if (dynsymcount < elf_buckets[i + 1]) | |
4924 | break; | |
4925 | } | |
4926 | ||
4927 | s = bfd_get_section_by_name (dynobj, ".hash"); | |
4928 | BFD_ASSERT (s != NULL); | |
4929 | s->_raw_size = (2 + bucketcount + dynsymcount) * (ARCH_SIZE / 8); | |
4930 | s->contents = (bfd_byte *) bfd_alloc (output_bfd, s->_raw_size); | |
4931 | if (s->contents == NULL) | |
4932 | { | |
4933 | bfd_set_error (bfd_error_no_memory); | |
4934 | return false; | |
4935 | } | |
4936 | memset (s->contents, 0, s->_raw_size); | |
4937 | ||
4938 | put_word (output_bfd, bucketcount, s->contents); | |
4939 | put_word (output_bfd, dynsymcount, s->contents + (ARCH_SIZE / 8)); | |
4940 | ||
4941 | elf_hash_table (info)->bucketcount = bucketcount; | |
4942 | ||
4943 | s = bfd_get_section_by_name (dynobj, ".dynstr"); | |
4944 | BFD_ASSERT (s != NULL); | |
4945 | s->_raw_size = _bfd_stringtab_size (elf_hash_table (info)->dynstr); | |
4946 | ||
12662be4 ILT |
4947 | if (! elf_add_dynamic_entry (info, DT_NULL, 0)) |
4948 | return false; | |
4949 | } | |
4950 | ||
4951 | return true; | |
013dec1a ILT |
4952 | } |
4953 | ||
4954 | /* Make the backend pick a good value for a dynamic symbol. This is | |
4955 | called via elf_link_hash_traverse, and also calls itself | |
4956 | recursively. */ | |
4957 | ||
4958 | static boolean | |
4959 | elf_adjust_dynamic_symbol (h, data) | |
4960 | struct elf_link_hash_entry *h; | |
4961 | PTR data; | |
4962 | { | |
4963 | struct bfd_link_info *info = (struct bfd_link_info *) data; | |
4964 | bfd *dynobj; | |
4965 | struct elf_backend_data *bed; | |
4966 | ||
12662be4 ILT |
4967 | /* If this symbol does not require a PLT entry, and it is not |
4968 | defined by a dynamic object, or is not referenced by a regular | |
4969 | object, ignore it. FIXME: Do we need to worry about symbols | |
4970 | which are defined by one dynamic object and referenced by another | |
4971 | one? */ | |
4972 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) == 0 | |
4973 | && ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
4974 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
4975 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0)) | |
013dec1a ILT |
4976 | return true; |
4977 | ||
4978 | /* If we've already adjusted this symbol, don't do it again. This | |
4979 | can happen via a recursive call. */ | |
4980 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0) | |
4981 | return true; | |
4982 | ||
4983 | /* Don't look at this symbol again. Note that we must set this | |
4984 | after checking the above conditions, because we may look at a | |
4985 | symbol once, decide not to do anything, and then get called | |
4986 | recursively later after REF_REGULAR is set below. */ | |
4987 | h->elf_link_hash_flags |= ELF_LINK_HASH_DYNAMIC_ADJUSTED; | |
4988 | ||
4989 | /* If this is a weak definition, and we know a real definition, and | |
4990 | the real symbol is not itself defined by a regular object file, | |
4991 | then get a good value for the real definition. We handle the | |
4992 | real symbol first, for the convenience of the backend routine. | |
4993 | ||
4994 | Note that there is a confusing case here. If the real definition | |
4995 | is defined by a regular object file, we don't get the real symbol | |
4996 | from the dynamic object, but we do get the weak symbol. If the | |
4997 | processor backend uses a COPY reloc, then if some routine in the | |
4998 | dynamic object changes the real symbol, we will not see that | |
4999 | change in the corresponding weak symbol. This is the way other | |
5000 | ELF linkers work as well, and seems to be a result of the shared | |
5001 | library model. | |
5002 | ||
5003 | I will clarify this issue. Most SVR4 shared libraries define the | |
5004 | variable _timezone and define timezone as a weak synonym. The | |
5005 | tzset call changes _timezone. If you write | |
5006 | extern int timezone; | |
5007 | int _timezone = 5; | |
5008 | int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } | |
5009 | you might expect that, since timezone is a synonym for _timezone, | |
5010 | the same number will print both times. However, if the processor | |
5011 | backend uses a COPY reloc, then actually timezone will be copied | |
5012 | into your process image, and, since you define _timezone | |
5013 | yourself, _timezone will not. Thus timezone and _timezone will | |
5014 | wind up at different memory locations. The tzset call will set | |
5015 | _timezone, leaving timezone unchanged. */ | |
5016 | ||
5017 | if (h->weakdef != NULL) | |
5018 | { | |
5019 | struct elf_link_hash_entry *weakdef; | |
5020 | ||
5021 | BFD_ASSERT (h->root.type == bfd_link_hash_defined); | |
5022 | weakdef = h->weakdef; | |
5023 | BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined); | |
5024 | BFD_ASSERT (weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC); | |
30e5ad97 | 5025 | if ((weakdef->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0) |
013dec1a | 5026 | { |
30e5ad97 ILT |
5027 | /* This symbol is defined by a regular object file, so we |
5028 | will not do anything special. Clear weakdef for the | |
5029 | convenience of the processor backend. */ | |
013dec1a ILT |
5030 | h->weakdef = NULL; |
5031 | } | |
5032 | else | |
5033 | { | |
5034 | /* There is an implicit reference by a regular object file | |
5035 | via the weak symbol. */ | |
5036 | weakdef->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR; | |
5037 | if (! elf_adjust_dynamic_symbol (weakdef, (PTR) info)) | |
5038 | return false; | |
5039 | } | |
5040 | } | |
5041 | ||
5042 | dynobj = elf_hash_table (info)->dynobj; | |
5043 | bed = get_elf_backend_data (dynobj); | |
5044 | if (! (*bed->elf_backend_adjust_dynamic_symbol) (info, h)) | |
5045 | { | |
5046 | /* FIXME: No way to return error. */ | |
5047 | abort (); | |
5048 | } | |
5049 | ||
5050 | return true; | |
5051 | } | |
6ec3bb6a ILT |
5052 | \f |
5053 | /* Final phase of ELF linker. */ | |
5054 | ||
5055 | /* A structure we use to avoid passing large numbers of arguments. */ | |
5056 | ||
5057 | struct elf_final_link_info | |
5058 | { | |
5059 | /* General link information. */ | |
5060 | struct bfd_link_info *info; | |
5061 | /* Output BFD. */ | |
5062 | bfd *output_bfd; | |
5063 | /* Symbol string table. */ | |
eb4267a3 | 5064 | struct bfd_strtab_hash *symstrtab; |
013dec1a ILT |
5065 | /* .dynsym section. */ |
5066 | asection *dynsym_sec; | |
5067 | /* .hash section. */ | |
5068 | asection *hash_sec; | |
6ec3bb6a ILT |
5069 | /* Buffer large enough to hold contents of any section. */ |
5070 | bfd_byte *contents; | |
5071 | /* Buffer large enough to hold external relocs of any section. */ | |
5072 | PTR external_relocs; | |
5073 | /* Buffer large enough to hold internal relocs of any section. */ | |
5074 | Elf_Internal_Rela *internal_relocs; | |
5075 | /* Buffer large enough to hold external local symbols of any input | |
5076 | BFD. */ | |
5077 | Elf_External_Sym *external_syms; | |
5078 | /* Buffer large enough to hold internal local symbols of any input | |
5079 | BFD. */ | |
5080 | Elf_Internal_Sym *internal_syms; | |
5081 | /* Array large enough to hold a symbol index for each local symbol | |
5082 | of any input BFD. */ | |
5083 | long *indices; | |
5084 | /* Array large enough to hold a section pointer for each local | |
5085 | symbol of any input BFD. */ | |
5086 | asection **sections; | |
5087 | /* Buffer to hold swapped out symbols. */ | |
5088 | Elf_External_Sym *symbuf; | |
5089 | /* Number of swapped out symbols in buffer. */ | |
5090 | size_t symbuf_count; | |
5091 | /* Number of symbols which fit in symbuf. */ | |
5092 | size_t symbuf_size; | |
5093 | }; | |
5094 | ||
5095 | static boolean elf_link_output_sym | |
71edd06d ILT |
5096 | PARAMS ((struct elf_final_link_info *, const char *, |
5097 | Elf_Internal_Sym *, asection *)); | |
6ec3bb6a ILT |
5098 | static boolean elf_link_flush_output_syms |
5099 | PARAMS ((struct elf_final_link_info *)); | |
5100 | static boolean elf_link_output_extsym | |
5101 | PARAMS ((struct elf_link_hash_entry *, PTR)); | |
5102 | static boolean elf_link_input_bfd | |
5103 | PARAMS ((struct elf_final_link_info *, bfd *)); | |
5104 | static boolean elf_reloc_link_order | |
5105 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
5106 | struct bfd_link_order *)); | |
5107 | ||
5108 | /* Do the final step of an ELF link. */ | |
5109 | ||
5110 | boolean | |
5111 | elf_bfd_final_link (abfd, info) | |
5112 | bfd *abfd; | |
5113 | struct bfd_link_info *info; | |
5114 | { | |
12662be4 | 5115 | boolean dynamic; |
013dec1a | 5116 | bfd *dynobj; |
6ec3bb6a ILT |
5117 | struct elf_final_link_info finfo; |
5118 | register asection *o; | |
5119 | register struct bfd_link_order *p; | |
5120 | register bfd *sub; | |
5121 | size_t max_contents_size; | |
5122 | size_t max_external_reloc_size; | |
5123 | size_t max_internal_reloc_count; | |
5124 | size_t max_sym_count; | |
5125 | file_ptr off; | |
5126 | Elf_Internal_Sym elfsym; | |
013dec1a | 5127 | unsigned int i; |
6ec3bb6a ILT |
5128 | Elf_Internal_Shdr *symtab_hdr; |
5129 | Elf_Internal_Shdr *symstrtab_hdr; | |
71edd06d | 5130 | struct elf_backend_data *bed = get_elf_backend_data (abfd); |
6ec3bb6a | 5131 | |
0ff5d3a6 | 5132 | if (info->shared) |
374d2ef9 | 5133 | abfd->flags |= DYNAMIC; |
0ff5d3a6 | 5134 | |
12662be4 | 5135 | dynamic = elf_hash_table (info)->dynamic_sections_created; |
013dec1a ILT |
5136 | dynobj = elf_hash_table (info)->dynobj; |
5137 | ||
6ec3bb6a ILT |
5138 | finfo.info = info; |
5139 | finfo.output_bfd = abfd; | |
eb4267a3 | 5140 | finfo.symstrtab = elf_stringtab_init (); |
6ec3bb6a ILT |
5141 | if (finfo.symstrtab == NULL) |
5142 | return false; | |
12662be4 | 5143 | if (! dynamic) |
013dec1a ILT |
5144 | { |
5145 | finfo.dynsym_sec = NULL; | |
5146 | finfo.hash_sec = NULL; | |
5147 | } | |
5148 | else | |
5149 | { | |
5150 | finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); | |
5151 | finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); | |
5152 | if (finfo.dynsym_sec == NULL | |
5153 | || finfo.hash_sec == NULL) | |
5154 | abort (); | |
5155 | } | |
6ec3bb6a ILT |
5156 | finfo.contents = NULL; |
5157 | finfo.external_relocs = NULL; | |
5158 | finfo.internal_relocs = NULL; | |
5159 | finfo.external_syms = NULL; | |
5160 | finfo.internal_syms = NULL; | |
5161 | finfo.indices = NULL; | |
5162 | finfo.sections = NULL; | |
5163 | finfo.symbuf = NULL; | |
5164 | finfo.symbuf_count = 0; | |
5165 | ||
5166 | /* Count up the number of relocations we will output for each output | |
5167 | section, so that we know the sizes of the reloc sections. We | |
5168 | also figure out some maximum sizes. */ | |
5169 | max_contents_size = 0; | |
5170 | max_external_reloc_size = 0; | |
5171 | max_internal_reloc_count = 0; | |
5172 | max_sym_count = 0; | |
5173 | for (o = abfd->sections; o != (asection *) NULL; o = o->next) | |
5174 | { | |
5175 | o->reloc_count = 0; | |
5176 | ||
5177 | for (p = o->link_order_head; p != NULL; p = p->next) | |
5178 | { | |
5179 | if (p->type == bfd_section_reloc_link_order | |
5180 | || p->type == bfd_symbol_reloc_link_order) | |
5181 | ++o->reloc_count; | |
5182 | else if (p->type == bfd_indirect_link_order) | |
5183 | { | |
5184 | asection *sec; | |
5185 | ||
5186 | sec = p->u.indirect.section; | |
5187 | ||
5188 | if (info->relocateable) | |
5189 | o->reloc_count += sec->reloc_count; | |
5190 | ||
5191 | if (sec->_raw_size > max_contents_size) | |
5192 | max_contents_size = sec->_raw_size; | |
5193 | if (sec->_cooked_size > max_contents_size) | |
5194 | max_contents_size = sec->_cooked_size; | |
5195 | ||
5196 | /* We are interested in just local symbols, not all | |
5197 | symbols. */ | |
5315c428 ILT |
5198 | if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour) |
5199 | { | |
5200 | size_t sym_count; | |
5201 | ||
5202 | if (elf_bad_symtab (sec->owner)) | |
5203 | sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size | |
5204 | / sizeof (Elf_External_Sym)); | |
5205 | else | |
5206 | sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; | |
5207 | ||
5208 | if (sym_count > max_sym_count) | |
5209 | max_sym_count = sym_count; | |
6ec3bb6a | 5210 | |
6c8fa8e6 ILT |
5211 | if ((sec->flags & SEC_RELOC) != 0) |
5212 | { | |
5213 | size_t ext_size; | |
6ec3bb6a | 5214 | |
6c8fa8e6 ILT |
5215 | ext_size = elf_section_data (sec)->rel_hdr.sh_size; |
5216 | if (ext_size > max_external_reloc_size) | |
5217 | max_external_reloc_size = ext_size; | |
5218 | if (sec->reloc_count > max_internal_reloc_count) | |
5219 | max_internal_reloc_count = sec->reloc_count; | |
5220 | } | |
6ec3bb6a ILT |
5221 | } |
5222 | } | |
5223 | } | |
5224 | ||
5225 | if (o->reloc_count > 0) | |
5226 | o->flags |= SEC_RELOC; | |
5227 | else | |
5228 | { | |
5229 | /* Explicitly clear the SEC_RELOC flag. The linker tends to | |
5230 | set it (this is probably a bug) and if it is set | |
5231 | assign_section_numbers will create a reloc section. */ | |
5232 | o->flags &=~ SEC_RELOC; | |
5233 | } | |
1c640609 ILT |
5234 | |
5235 | /* If the SEC_ALLOC flag is not set, force the section VMA to | |
5236 | zero. This is done in elf_fake_sections as well, but forcing | |
5237 | the VMA to 0 here will ensure that relocs against these | |
5238 | sections are handled correctly. */ | |
5239 | if ((o->flags & SEC_ALLOC) == 0) | |
5240 | o->vma = 0; | |
6ec3bb6a ILT |
5241 | } |
5242 | ||
5243 | /* Figure out the file positions for everything but the symbol table | |
5244 | and the relocs. We set symcount to force assign_section_numbers | |
5245 | to create a symbol table. */ | |
5246 | abfd->symcount = info->strip == strip_all ? 0 : 1; | |
5247 | BFD_ASSERT (! abfd->output_has_begun); | |
5248 | if (! elf_compute_section_file_positions (abfd, info)) | |
5249 | goto error_return; | |
5250 | ||
5251 | /* That created the reloc sections. Set their sizes, and assign | |
5252 | them file positions, and allocate some buffers. */ | |
5253 | for (o = abfd->sections; o != NULL; o = o->next) | |
5254 | { | |
5255 | if ((o->flags & SEC_RELOC) != 0) | |
5256 | { | |
5257 | Elf_Internal_Shdr *rel_hdr; | |
5258 | register struct elf_link_hash_entry **p, **pend; | |
5259 | ||
5260 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
5261 | ||
5262 | rel_hdr->sh_size = rel_hdr->sh_entsize * o->reloc_count; | |
5263 | ||
5264 | /* The contents field must last into write_object_contents, | |
5265 | so we allocate it with bfd_alloc rather than malloc. */ | |
5266 | rel_hdr->contents = (PTR) bfd_alloc (abfd, rel_hdr->sh_size); | |
5267 | if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) | |
5268 | { | |
5269 | bfd_set_error (bfd_error_no_memory); | |
5270 | goto error_return; | |
5271 | } | |
5272 | ||
5273 | p = ((struct elf_link_hash_entry **) | |
5274 | malloc (o->reloc_count | |
5275 | * sizeof (struct elf_link_hash_entry *))); | |
5276 | if (p == NULL && o->reloc_count != 0) | |
5277 | { | |
5278 | bfd_set_error (bfd_error_no_memory); | |
5279 | goto error_return; | |
5280 | } | |
5281 | elf_section_data (o)->rel_hashes = p; | |
5282 | pend = p + o->reloc_count; | |
5283 | for (; p < pend; p++) | |
5284 | *p = NULL; | |
5285 | ||
5286 | /* Use the reloc_count field as an index when outputting the | |
5287 | relocs. */ | |
5288 | o->reloc_count = 0; | |
5289 | } | |
5290 | } | |
5291 | ||
5292 | assign_file_positions_for_relocs (abfd); | |
5293 | ||
5294 | /* We have now assigned file positions for all the sections except | |
5295 | .symtab and .strtab. We start the .symtab section at the current | |
5296 | file position, and write directly to it. We build the .strtab | |
5297 | section in memory. When we add .dynsym support, we will build | |
5298 | that in memory as well (.dynsym is smaller than .symtab). */ | |
5299 | abfd->symcount = 0; | |
5300 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
5301 | /* sh_name is set in prep_headers. */ | |
5302 | symtab_hdr->sh_type = SHT_SYMTAB; | |
5303 | symtab_hdr->sh_flags = 0; | |
5304 | symtab_hdr->sh_addr = 0; | |
5305 | symtab_hdr->sh_size = 0; | |
5306 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
5307 | /* sh_link is set in assign_section_numbers. */ | |
5308 | /* sh_info is set below. */ | |
5309 | /* sh_offset is set just below. */ | |
5310 | symtab_hdr->sh_addralign = 4; /* FIXME: system dependent? */ | |
5311 | ||
5312 | off = elf_tdata (abfd)->next_file_pos; | |
013dec1a | 5313 | off = assign_file_position_for_section (symtab_hdr, off, true); |
6ec3bb6a ILT |
5314 | |
5315 | /* Note that at this point elf_tdata (abfd)->next_file_pos is | |
5316 | incorrect. We do not yet know the size of the .symtab section. | |
5317 | We correct next_file_pos below, after we do know the size. */ | |
5318 | ||
5319 | /* Allocate a buffer to hold swapped out symbols. This is to avoid | |
5320 | continuously seeking to the right position in the file. */ | |
5321 | if (! info->keep_memory || max_sym_count < 20) | |
5322 | finfo.symbuf_size = 20; | |
5323 | else | |
5324 | finfo.symbuf_size = max_sym_count; | |
5325 | finfo.symbuf = ((Elf_External_Sym *) | |
5326 | malloc (finfo.symbuf_size * sizeof (Elf_External_Sym))); | |
5327 | if (finfo.symbuf == NULL) | |
5328 | { | |
5329 | bfd_set_error (bfd_error_no_memory); | |
5330 | goto error_return; | |
5331 | } | |
5332 | ||
5333 | /* Start writing out the symbol table. The first symbol is always a | |
5334 | dummy symbol. */ | |
5335 | elfsym.st_value = 0; | |
5336 | elfsym.st_size = 0; | |
5337 | elfsym.st_info = 0; | |
5338 | elfsym.st_other = 0; | |
5339 | elfsym.st_shndx = SHN_UNDEF; | |
71edd06d ILT |
5340 | if (! elf_link_output_sym (&finfo, (const char *) NULL, |
5341 | &elfsym, bfd_und_section_ptr)) | |
6ec3bb6a ILT |
5342 | goto error_return; |
5343 | ||
5344 | #if 0 | |
5345 | /* Some standard ELF linkers do this, but we don't because it causes | |
5346 | bootstrap comparison failures. */ | |
5347 | /* Output a file symbol for the output file as the second symbol. | |
5348 | We output this even if we are discarding local symbols, although | |
5349 | I'm not sure if this is correct. */ | |
5350 | elfsym.st_value = 0; | |
5351 | elfsym.st_size = 0; | |
5352 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); | |
5353 | elfsym.st_other = 0; | |
5354 | elfsym.st_shndx = SHN_ABS; | |
71edd06d ILT |
5355 | if (! elf_link_output_sym (&finfo, bfd_get_filename (abfd), |
5356 | &elfsym, bfd_abs_section_ptr)) | |
6ec3bb6a ILT |
5357 | goto error_return; |
5358 | #endif | |
5359 | ||
5360 | /* Output a symbol for each section. We output these even if we are | |
5361 | discarding local symbols, since they are used for relocs. These | |
5362 | symbols have no names. We store the index of each one in the | |
5363 | index field of the section, so that we can find it again when | |
5364 | outputting relocs. */ | |
5365 | elfsym.st_value = 0; | |
5366 | elfsym.st_size = 0; | |
5367 | elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); | |
5368 | elfsym.st_other = 0; | |
013dec1a | 5369 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) |
6ec3bb6a | 5370 | { |
013dec1a | 5371 | o = section_from_elf_index (abfd, i); |
24f13b03 | 5372 | if (o != NULL) |
013dec1a ILT |
5373 | o->target_index = abfd->symcount; |
5374 | elfsym.st_shndx = i; | |
71edd06d ILT |
5375 | if (! elf_link_output_sym (&finfo, (const char *) NULL, |
5376 | &elfsym, o)) | |
6ec3bb6a ILT |
5377 | goto error_return; |
5378 | } | |
5379 | ||
5380 | /* Allocate some memory to hold information read in from the input | |
5381 | files. */ | |
5382 | finfo.contents = (bfd_byte *) malloc (max_contents_size); | |
5383 | finfo.external_relocs = (PTR) malloc (max_external_reloc_size); | |
5384 | finfo.internal_relocs = ((Elf_Internal_Rela *) | |
5385 | malloc (max_internal_reloc_count | |
5386 | * sizeof (Elf_Internal_Rela))); | |
5387 | finfo.external_syms = ((Elf_External_Sym *) | |
5388 | malloc (max_sym_count * sizeof (Elf_External_Sym))); | |
5389 | finfo.internal_syms = ((Elf_Internal_Sym *) | |
5390 | malloc (max_sym_count * sizeof (Elf_Internal_Sym))); | |
5391 | finfo.indices = (long *) malloc (max_sym_count * sizeof (long)); | |
5392 | finfo.sections = (asection **) malloc (max_sym_count * sizeof (asection *)); | |
5393 | if ((finfo.contents == NULL && max_contents_size != 0) | |
5394 | || (finfo.external_relocs == NULL && max_external_reloc_size != 0) | |
5395 | || (finfo.internal_relocs == NULL && max_internal_reloc_count != 0) | |
5396 | || (finfo.external_syms == NULL && max_sym_count != 0) | |
5397 | || (finfo.internal_syms == NULL && max_sym_count != 0) | |
5398 | || (finfo.indices == NULL && max_sym_count != 0) | |
5399 | || (finfo.sections == NULL && max_sym_count != 0)) | |
5400 | { | |
5401 | bfd_set_error (bfd_error_no_memory); | |
5402 | goto error_return; | |
5403 | } | |
5404 | ||
5405 | /* Since ELF permits relocations to be against local symbols, we | |
5406 | must have the local symbols available when we do the relocations. | |
5407 | Since we would rather only read the local symbols once, and we | |
5408 | would rather not keep them in memory, we handle all the | |
5409 | relocations for a single input file at the same time. | |
5410 | ||
5411 | Unfortunately, there is no way to know the total number of local | |
5412 | symbols until we have seen all of them, and the local symbol | |
5413 | indices precede the global symbol indices. This means that when | |
5414 | we are generating relocateable output, and we see a reloc against | |
5415 | a global symbol, we can not know the symbol index until we have | |
5416 | finished examining all the local symbols to see which ones we are | |
5417 | going to output. To deal with this, we keep the relocations in | |
5418 | memory, and don't output them until the end of the link. This is | |
5419 | an unfortunate waste of memory, but I don't see a good way around | |
5420 | it. Fortunately, it only happens when performing a relocateable | |
5421 | link, which is not the common case. FIXME: If keep_memory is set | |
5422 | we could write the relocs out and then read them again; I don't | |
5423 | know how bad the memory loss will be. */ | |
5424 | ||
5425 | for (sub = info->input_bfds; sub != NULL; sub = sub->next) | |
5426 | sub->output_has_begun = false; | |
5427 | for (o = abfd->sections; o != NULL; o = o->next) | |
5428 | { | |
5429 | for (p = o->link_order_head; p != NULL; p = p->next) | |
5430 | { | |
5431 | if (p->type == bfd_indirect_link_order | |
5432 | && (bfd_get_flavour (p->u.indirect.section->owner) | |
5433 | == bfd_target_elf_flavour)) | |
5434 | { | |
5435 | sub = p->u.indirect.section->owner; | |
5436 | if (! sub->output_has_begun) | |
5437 | { | |
5438 | if (! elf_link_input_bfd (&finfo, sub)) | |
5439 | goto error_return; | |
5440 | sub->output_has_begun = true; | |
5441 | } | |
5442 | } | |
5443 | else if (p->type == bfd_section_reloc_link_order | |
5444 | || p->type == bfd_symbol_reloc_link_order) | |
5445 | { | |
5446 | if (! elf_reloc_link_order (abfd, info, o, p)) | |
5447 | goto error_return; | |
5448 | } | |
5449 | else | |
5450 | { | |
5451 | if (! _bfd_default_link_order (abfd, info, o, p)) | |
5452 | goto error_return; | |
5453 | } | |
5454 | } | |
5455 | } | |
5456 | ||
5457 | /* That wrote out all the local symbols. Finish up the symbol table | |
5458 | with the global symbols. */ | |
5459 | ||
5460 | /* The sh_info field records the index of the first non local | |
5461 | symbol. */ | |
5462 | symtab_hdr->sh_info = abfd->symcount; | |
12662be4 | 5463 | if (dynamic) |
013dec1a | 5464 | elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 1; |
6ec3bb6a ILT |
5465 | |
5466 | /* We get the global symbols from the hash table. */ | |
013dec1a ILT |
5467 | elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, |
5468 | (PTR) &finfo); | |
6ec3bb6a ILT |
5469 | |
5470 | /* Flush all symbols to the file. */ | |
5471 | if (! elf_link_flush_output_syms (&finfo)) | |
5472 | return false; | |
5473 | ||
5474 | /* Now we know the size of the symtab section. */ | |
5475 | off += symtab_hdr->sh_size; | |
5476 | ||
eb4267a3 ILT |
5477 | /* Finish up and write out the symbol string table (.strtab) |
5478 | section. */ | |
6ec3bb6a ILT |
5479 | symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; |
5480 | /* sh_name was set in prep_headers. */ | |
5481 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
5482 | symstrtab_hdr->sh_flags = 0; | |
5483 | symstrtab_hdr->sh_addr = 0; | |
eb4267a3 | 5484 | symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); |
6ec3bb6a ILT |
5485 | symstrtab_hdr->sh_entsize = 0; |
5486 | symstrtab_hdr->sh_link = 0; | |
5487 | symstrtab_hdr->sh_info = 0; | |
5488 | /* sh_offset is set just below. */ | |
5489 | symstrtab_hdr->sh_addralign = 1; | |
6ec3bb6a | 5490 | |
013dec1a | 5491 | off = assign_file_position_for_section (symstrtab_hdr, off, true); |
6ec3bb6a ILT |
5492 | elf_tdata (abfd)->next_file_pos = off; |
5493 | ||
eb4267a3 ILT |
5494 | if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 |
5495 | || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) | |
5496 | return false; | |
5497 | ||
6ec3bb6a ILT |
5498 | /* Adjust the relocs to have the correct symbol indices. */ |
5499 | for (o = abfd->sections; o != NULL; o = o->next) | |
5500 | { | |
5501 | struct elf_link_hash_entry **rel_hash; | |
5502 | Elf_Internal_Shdr *rel_hdr; | |
6ec3bb6a ILT |
5503 | |
5504 | if ((o->flags & SEC_RELOC) == 0) | |
5505 | continue; | |
5506 | ||
5507 | rel_hash = elf_section_data (o)->rel_hashes; | |
5508 | rel_hdr = &elf_section_data (o)->rel_hdr; | |
5509 | for (i = 0; i < o->reloc_count; i++, rel_hash++) | |
5510 | { | |
5511 | if (*rel_hash == NULL) | |
5512 | continue; | |
5513 | ||
5514 | BFD_ASSERT ((*rel_hash)->indx >= 0); | |
5515 | ||
5516 | if (rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
5517 | { | |
5518 | Elf_External_Rel *erel; | |
5519 | Elf_Internal_Rel irel; | |
5520 | ||
5521 | erel = (Elf_External_Rel *) rel_hdr->contents + i; | |
5522 | elf_swap_reloc_in (abfd, erel, &irel); | |
5523 | irel.r_info = ELF_R_INFO ((*rel_hash)->indx, | |
5524 | ELF_R_TYPE (irel.r_info)); | |
5525 | elf_swap_reloc_out (abfd, &irel, erel); | |
5526 | } | |
5527 | else | |
5528 | { | |
5529 | Elf_External_Rela *erela; | |
5530 | Elf_Internal_Rela irela; | |
5531 | ||
5532 | BFD_ASSERT (rel_hdr->sh_entsize | |
5533 | == sizeof (Elf_External_Rela)); | |
5534 | ||
5535 | erela = (Elf_External_Rela *) rel_hdr->contents + i; | |
5536 | elf_swap_reloca_in (abfd, erela, &irela); | |
5537 | irela.r_info = ELF_R_INFO ((*rel_hash)->indx, | |
5538 | ELF_R_TYPE (irela.r_info)); | |
5539 | elf_swap_reloca_out (abfd, &irela, erela); | |
5540 | } | |
5541 | } | |
5542 | ||
5543 | /* Set the reloc_count field to 0 to prevent write_relocs from | |
5544 | trying to swap the relocs out itself. */ | |
5545 | o->reloc_count = 0; | |
5546 | } | |
5547 | ||
12662be4 ILT |
5548 | /* If we are linking against a dynamic object, or generating a |
5549 | shared library, finish up the dynamic linking information. */ | |
5550 | if (dynamic) | |
013dec1a ILT |
5551 | { |
5552 | Elf_External_Dyn *dyncon, *dynconend; | |
013dec1a ILT |
5553 | |
5554 | /* Fix up .dynamic entries. */ | |
5555 | o = bfd_get_section_by_name (dynobj, ".dynamic"); | |
5556 | BFD_ASSERT (o != NULL); | |
5557 | ||
5558 | dyncon = (Elf_External_Dyn *) o->contents; | |
5559 | dynconend = (Elf_External_Dyn *) (o->contents + o->_raw_size); | |
5560 | for (; dyncon < dynconend; dyncon++) | |
5561 | { | |
5562 | Elf_Internal_Dyn dyn; | |
5563 | const char *name; | |
5564 | unsigned int type; | |
5565 | ||
5566 | elf_swap_dyn_in (dynobj, dyncon, &dyn); | |
5567 | ||
5568 | switch (dyn.d_tag) | |
5569 | { | |
5570 | default: | |
5571 | break; | |
5572 | ||
197e30e5 ILT |
5573 | /* SVR4 linkers seem to set DT_INIT and DT_FINI based on |
5574 | magic _init and _fini symbols. This is pretty ugly, | |
5575 | but we are compatible. */ | |
013dec1a | 5576 | case DT_INIT: |
197e30e5 ILT |
5577 | name = "_init"; |
5578 | goto get_sym; | |
013dec1a | 5579 | case DT_FINI: |
197e30e5 ILT |
5580 | name = "_fini"; |
5581 | get_sym: | |
5582 | { | |
5583 | struct elf_link_hash_entry *h; | |
5584 | ||
5585 | h = elf_link_hash_lookup (elf_hash_table (info), name, | |
5586 | false, false, true); | |
5587 | BFD_ASSERT (h != NULL); | |
5588 | if (h->root.type == bfd_link_hash_defined) | |
5589 | { | |
5590 | dyn.d_un.d_val = h->root.u.def.value; | |
5591 | o = h->root.u.def.section; | |
5592 | if (o->output_section != NULL) | |
5593 | dyn.d_un.d_val += (o->output_section->vma | |
5594 | + o->output_offset); | |
5595 | else | |
5596 | dyn.d_un.d_val += o->vma; | |
5597 | } | |
5598 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
5599 | } | |
5600 | break; | |
5601 | ||
013dec1a ILT |
5602 | case DT_HASH: |
5603 | name = ".hash"; | |
5604 | goto get_vma; | |
5605 | case DT_STRTAB: | |
5606 | name = ".dynstr"; | |
5607 | goto get_vma; | |
5608 | case DT_SYMTAB: | |
5609 | name = ".dynsym"; | |
5610 | get_vma: | |
5611 | o = bfd_get_section_by_name (abfd, name); | |
5612 | BFD_ASSERT (o != NULL); | |
5613 | dyn.d_un.d_ptr = o->vma; | |
5614 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
5615 | break; | |
5616 | ||
5617 | case DT_REL: | |
5618 | case DT_RELA: | |
5619 | case DT_RELSZ: | |
5620 | case DT_RELASZ: | |
5621 | if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) | |
5622 | type = SHT_REL; | |
5623 | else | |
5624 | type = SHT_RELA; | |
5625 | dyn.d_un.d_val = 0; | |
5626 | for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++) | |
5627 | { | |
5628 | Elf_Internal_Shdr *hdr; | |
5629 | ||
5630 | hdr = elf_elfsections (abfd)[i]; | |
5631 | if (hdr->sh_type == type | |
5632 | && (hdr->sh_flags & SHF_ALLOC) != 0) | |
5633 | { | |
5634 | if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) | |
5635 | dyn.d_un.d_val += hdr->sh_size; | |
5636 | else | |
5637 | { | |
5638 | if (dyn.d_un.d_val == 0 | |
5639 | || hdr->sh_addr < dyn.d_un.d_val) | |
5640 | dyn.d_un.d_val = hdr->sh_addr; | |
5641 | } | |
5642 | } | |
5643 | } | |
5644 | elf_swap_dyn_out (dynobj, &dyn, dyncon); | |
5645 | break; | |
5646 | } | |
5647 | } | |
12662be4 | 5648 | } |
013dec1a | 5649 | |
12662be4 ILT |
5650 | /* If we have created any dynamic sections, then output them. */ |
5651 | if (dynobj != NULL) | |
5652 | { | |
013dec1a ILT |
5653 | if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) |
5654 | goto error_return; | |
5655 | ||
5656 | for (o = dynobj->sections; o != NULL; o = o->next) | |
5657 | { | |
12662be4 ILT |
5658 | if ((o->flags & SEC_HAS_CONTENTS) == 0 |
5659 | || o->_raw_size == 0) | |
013dec1a | 5660 | continue; |
8af74670 ILT |
5661 | if ((o->flags & SEC_IN_MEMORY) == 0) |
5662 | { | |
fb562be0 ILT |
5663 | /* At this point, we are only interested in sections |
5664 | created by elf_link_create_dynamic_sections. FIXME: | |
5665 | This test is fragile. */ | |
8af74670 ILT |
5666 | continue; |
5667 | } | |
eb4267a3 ILT |
5668 | if ((elf_section_data (o->output_section)->this_hdr.sh_type |
5669 | != SHT_STRTAB) | |
5670 | || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) | |
5671 | { | |
5672 | if (! bfd_set_section_contents (abfd, o->output_section, | |
5673 | o->contents, o->output_offset, | |
5674 | o->_raw_size)) | |
5675 | goto error_return; | |
5676 | } | |
5677 | else | |
5678 | { | |
5679 | file_ptr off; | |
5680 | ||
5681 | /* The contents of the .dynstr section are actually in a | |
5682 | stringtab. */ | |
5683 | off = elf_section_data (o->output_section)->this_hdr.sh_offset; | |
5684 | if (bfd_seek (abfd, off, SEEK_SET) != 0 | |
5685 | || ! _bfd_stringtab_emit (abfd, | |
5686 | elf_hash_table (info)->dynstr)) | |
5687 | goto error_return; | |
5688 | } | |
013dec1a ILT |
5689 | } |
5690 | } | |
5691 | ||
eb4267a3 ILT |
5692 | if (finfo.symstrtab != NULL) |
5693 | _bfd_stringtab_free (finfo.symstrtab); | |
6ec3bb6a ILT |
5694 | if (finfo.contents != NULL) |
5695 | free (finfo.contents); | |
5696 | if (finfo.external_relocs != NULL) | |
5697 | free (finfo.external_relocs); | |
5698 | if (finfo.internal_relocs != NULL) | |
5699 | free (finfo.internal_relocs); | |
5700 | if (finfo.external_syms != NULL) | |
5701 | free (finfo.external_syms); | |
5702 | if (finfo.internal_syms != NULL) | |
5703 | free (finfo.internal_syms); | |
5704 | if (finfo.indices != NULL) | |
5705 | free (finfo.indices); | |
5706 | if (finfo.sections != NULL) | |
5707 | free (finfo.sections); | |
5708 | if (finfo.symbuf != NULL) | |
5709 | free (finfo.symbuf); | |
5710 | for (o = abfd->sections; o != NULL; o = o->next) | |
5711 | { | |
5712 | if ((o->flags & SEC_RELOC) != 0 | |
5713 | && elf_section_data (o)->rel_hashes != NULL) | |
5714 | free (elf_section_data (o)->rel_hashes); | |
5715 | } | |
5716 | ||
3c9832f8 ILT |
5717 | elf_tdata (abfd)->linker = true; |
5718 | ||
6ec3bb6a ILT |
5719 | return true; |
5720 | ||
5721 | error_return: | |
eb4267a3 ILT |
5722 | if (finfo.symstrtab != NULL) |
5723 | _bfd_stringtab_free (finfo.symstrtab); | |
6ec3bb6a ILT |
5724 | if (finfo.contents != NULL) |
5725 | free (finfo.contents); | |
5726 | if (finfo.external_relocs != NULL) | |
5727 | free (finfo.external_relocs); | |
5728 | if (finfo.internal_relocs != NULL) | |
5729 | free (finfo.internal_relocs); | |
5730 | if (finfo.external_syms != NULL) | |
5731 | free (finfo.external_syms); | |
5732 | if (finfo.internal_syms != NULL) | |
5733 | free (finfo.internal_syms); | |
5734 | if (finfo.indices != NULL) | |
5735 | free (finfo.indices); | |
5736 | if (finfo.sections != NULL) | |
5737 | free (finfo.sections); | |
5738 | if (finfo.symbuf != NULL) | |
5739 | free (finfo.symbuf); | |
5740 | for (o = abfd->sections; o != NULL; o = o->next) | |
5741 | { | |
5742 | if ((o->flags & SEC_RELOC) != 0 | |
5743 | && elf_section_data (o)->rel_hashes != NULL) | |
5744 | free (elf_section_data (o)->rel_hashes); | |
5745 | } | |
5746 | ||
5747 | return false; | |
5748 | } | |
5749 | ||
5750 | /* Add a symbol to the output symbol table. */ | |
5751 | ||
5752 | static boolean | |
71edd06d | 5753 | elf_link_output_sym (finfo, name, elfsym, input_sec) |
6ec3bb6a ILT |
5754 | struct elf_final_link_info *finfo; |
5755 | const char *name; | |
5756 | Elf_Internal_Sym *elfsym; | |
71edd06d | 5757 | asection *input_sec; |
6ec3bb6a | 5758 | { |
71edd06d ILT |
5759 | boolean (*output_symbol_hook) PARAMS ((bfd *, |
5760 | struct bfd_link_info *info, | |
5761 | const char *, | |
5762 | Elf_Internal_Sym *, | |
5763 | asection *)); | |
5764 | ||
5765 | output_symbol_hook = get_elf_backend_data (finfo->output_bfd)-> | |
5766 | elf_backend_link_output_symbol_hook; | |
57a814a9 PS |
5767 | if (output_symbol_hook != NULL) |
5768 | { | |
5769 | if (! ((*output_symbol_hook) | |
5770 | (finfo->output_bfd, finfo->info, name, elfsym, input_sec))) | |
5771 | return false; | |
5772 | } | |
71edd06d | 5773 | |
6ec3bb6a ILT |
5774 | if (name == (const char *) NULL || *name == '\0') |
5775 | elfsym->st_name = 0; | |
5776 | else | |
5777 | { | |
eb4267a3 ILT |
5778 | elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, |
5779 | name, true, | |
5780 | false); | |
6ec3bb6a ILT |
5781 | if (elfsym->st_name == (unsigned long) -1) |
5782 | return false; | |
5783 | } | |
5784 | ||
5785 | if (finfo->symbuf_count >= finfo->symbuf_size) | |
5786 | { | |
5787 | if (! elf_link_flush_output_syms (finfo)) | |
5788 | return false; | |
5789 | } | |
5790 | ||
5791 | elf_swap_symbol_out (finfo->output_bfd, elfsym, | |
5792 | finfo->symbuf + finfo->symbuf_count); | |
5793 | ++finfo->symbuf_count; | |
5794 | ||
5795 | ++finfo->output_bfd->symcount; | |
5796 | ||
5797 | return true; | |
5798 | } | |
5799 | ||
5800 | /* Flush the output symbols to the file. */ | |
5801 | ||
5802 | static boolean | |
5803 | elf_link_flush_output_syms (finfo) | |
5804 | struct elf_final_link_info *finfo; | |
5805 | { | |
5806 | Elf_Internal_Shdr *symtab; | |
5807 | ||
5808 | symtab = &elf_tdata (finfo->output_bfd)->symtab_hdr; | |
5809 | ||
5810 | if (bfd_seek (finfo->output_bfd, symtab->sh_offset + symtab->sh_size, | |
5811 | SEEK_SET) != 0 | |
5812 | || (bfd_write ((PTR) finfo->symbuf, finfo->symbuf_count, | |
5813 | sizeof (Elf_External_Sym), finfo->output_bfd) | |
5814 | != finfo->symbuf_count * sizeof (Elf_External_Sym))) | |
5815 | return false; | |
5816 | ||
5817 | symtab->sh_size += finfo->symbuf_count * sizeof (Elf_External_Sym); | |
5818 | ||
5819 | finfo->symbuf_count = 0; | |
5820 | ||
5821 | return true; | |
5822 | } | |
5823 | ||
5824 | /* Add an external symbol to the symbol table. This is called from | |
5825 | the hash table traversal routine. */ | |
5826 | ||
5827 | static boolean | |
5828 | elf_link_output_extsym (h, data) | |
5829 | struct elf_link_hash_entry *h; | |
5830 | PTR data; | |
5831 | { | |
5832 | struct elf_final_link_info *finfo = (struct elf_final_link_info *) data; | |
013dec1a | 5833 | boolean strip; |
6ec3bb6a | 5834 | Elf_Internal_Sym sym; |
71edd06d | 5835 | asection *input_sec; |
6ec3bb6a | 5836 | |
013dec1a ILT |
5837 | /* We don't want to output symbols that have never been mentioned by |
5838 | a regular file, or that we have been told to strip. However, if | |
5839 | h->indx is set to -2, the symbol is used by a reloc and we must | |
5840 | output it. */ | |
5841 | if (h->indx == -2) | |
5842 | strip = false; | |
5315c428 ILT |
5843 | else if (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 |
5844 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0) | |
5845 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
013dec1a ILT |
5846 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) |
5847 | strip = true; | |
5848 | else if (finfo->info->strip == strip_all | |
5849 | || (finfo->info->strip == strip_some | |
5850 | && bfd_hash_lookup (finfo->info->keep_hash, | |
5851 | h->root.root.string, | |
5852 | false, false) == NULL)) | |
5853 | strip = true; | |
5854 | else | |
5855 | strip = false; | |
5856 | ||
5857 | /* If we're stripping it, and it's not a dynamic symbol, there's | |
5858 | nothing else to do. */ | |
5859 | if (strip && h->dynindx == -1) | |
6ec3bb6a ILT |
5860 | return true; |
5861 | ||
5862 | sym.st_value = 0; | |
5863 | sym.st_size = h->size; | |
6ec3bb6a | 5864 | sym.st_other = 0; |
bf73e4f3 | 5865 | if (h->root.type == bfd_link_hash_weak |
57b40081 | 5866 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEFINED_WEAK) != 0) |
bf73e4f3 ILT |
5867 | sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); |
5868 | else | |
5869 | sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); | |
6ec3bb6a ILT |
5870 | |
5871 | switch (h->root.type) | |
5872 | { | |
5873 | default: | |
5874 | case bfd_link_hash_new: | |
5875 | abort (); | |
5876 | return false; | |
5877 | ||
5878 | case bfd_link_hash_undefined: | |
71edd06d | 5879 | input_sec = bfd_und_section_ptr; |
6ec3bb6a ILT |
5880 | sym.st_shndx = SHN_UNDEF; |
5881 | break; | |
5882 | ||
5883 | case bfd_link_hash_weak: | |
71edd06d | 5884 | input_sec = bfd_und_section_ptr; |
6ec3bb6a | 5885 | sym.st_shndx = SHN_UNDEF; |
6ec3bb6a ILT |
5886 | break; |
5887 | ||
5888 | case bfd_link_hash_defined: | |
5889 | { | |
71edd06d ILT |
5890 | input_sec = h->root.u.def.section; |
5891 | if (input_sec->output_section != NULL) | |
6ec3bb6a | 5892 | { |
eb4267a3 ILT |
5893 | sym.st_shndx = |
5894 | elf_section_from_bfd_section (finfo->output_bfd, | |
5895 | input_sec->output_section); | |
013dec1a ILT |
5896 | if (sym.st_shndx == (unsigned short) -1) |
5897 | { | |
5898 | /* FIXME: No way to handle errors. */ | |
5899 | abort (); | |
5900 | } | |
6ec3bb6a | 5901 | |
013dec1a ILT |
5902 | /* ELF symbols in relocateable files are section relative, |
5903 | but in nonrelocateable files they are virtual | |
5904 | addresses. */ | |
71edd06d | 5905 | sym.st_value = h->root.u.def.value + input_sec->output_offset; |
013dec1a | 5906 | if (! finfo->info->relocateable) |
71edd06d | 5907 | sym.st_value += input_sec->output_section->vma; |
013dec1a ILT |
5908 | } |
5909 | else | |
5910 | { | |
197e30e5 ILT |
5911 | BFD_ASSERT ((bfd_get_flavour (input_sec->owner) |
5912 | == bfd_target_elf_flavour) | |
71edd06d | 5913 | && elf_elfheader (input_sec->owner)->e_type == ET_DYN); |
013dec1a | 5914 | sym.st_shndx = SHN_UNDEF; |
71edd06d | 5915 | input_sec = bfd_und_section_ptr; |
013dec1a | 5916 | } |
6ec3bb6a ILT |
5917 | } |
5918 | break; | |
5919 | ||
5920 | case bfd_link_hash_common: | |
71edd06d | 5921 | input_sec = bfd_com_section_ptr; |
6ec3bb6a | 5922 | sym.st_shndx = SHN_COMMON; |
7c6da9ca | 5923 | sym.st_value = 1 << h->root.u.c.alignment_power; |
6ec3bb6a ILT |
5924 | break; |
5925 | ||
5926 | case bfd_link_hash_indirect: | |
5927 | case bfd_link_hash_warning: | |
5928 | /* I have no idea how these should be handled. */ | |
5929 | return true; | |
5930 | } | |
5931 | ||
013dec1a ILT |
5932 | /* If this symbol should be put in the .dynsym section, then put it |
5933 | there now. We have already know the symbol index. We also fill | |
5934 | in the entry in the .hash section. */ | |
12662be4 ILT |
5935 | if (h->dynindx != -1 |
5936 | && elf_hash_table (finfo->info)->dynamic_sections_created) | |
013dec1a ILT |
5937 | { |
5938 | struct elf_backend_data *bed; | |
5939 | size_t bucketcount; | |
5940 | size_t bucket; | |
5941 | bfd_byte *bucketpos; | |
5942 | bfd_vma chain; | |
5943 | ||
5944 | sym.st_name = h->dynstr_index; | |
5945 | ||
5946 | /* Give the processor backend a chance to tweak the symbol | |
5947 | value, and also to finish up anything that needs to be done | |
5948 | for this symbol. */ | |
5949 | bed = get_elf_backend_data (finfo->output_bfd); | |
5950 | if (! ((*bed->elf_backend_finish_dynamic_symbol) | |
5951 | (finfo->output_bfd, finfo->info, h, &sym))) | |
5952 | { | |
5953 | /* FIXME: No way to return error. */ | |
5954 | abort (); | |
5955 | } | |
5956 | ||
5957 | elf_swap_symbol_out (finfo->output_bfd, &sym, | |
5958 | ((Elf_External_Sym *) finfo->dynsym_sec->contents | |
5959 | + h->dynindx)); | |
5960 | ||
5961 | bucketcount = elf_hash_table (finfo->info)->bucketcount; | |
12662be4 ILT |
5962 | bucket = (bfd_elf_hash ((const unsigned char *) h->root.root.string) |
5963 | % bucketcount); | |
013dec1a ILT |
5964 | bucketpos = ((bfd_byte *) finfo->hash_sec->contents |
5965 | + (bucket + 2) * (ARCH_SIZE / 8)); | |
5966 | chain = get_word (finfo->output_bfd, bucketpos); | |
5967 | put_word (finfo->output_bfd, h->dynindx, bucketpos); | |
5968 | put_word (finfo->output_bfd, chain, | |
5969 | ((bfd_byte *) finfo->hash_sec->contents | |
5970 | + (bucketcount + 2 + h->dynindx) * (ARCH_SIZE / 8))); | |
5971 | } | |
5972 | ||
5973 | /* If we're stripping it, then it was just a dynamic symbol, and | |
5974 | there's nothing else to do. */ | |
5975 | if (strip) | |
5976 | return true; | |
5977 | ||
6ec3bb6a ILT |
5978 | h->indx = finfo->output_bfd->symcount; |
5979 | ||
71edd06d | 5980 | if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec)) |
6ec3bb6a ILT |
5981 | { |
5982 | /* FIXME: No way to return error. */ | |
5983 | abort (); | |
5984 | } | |
5985 | ||
5986 | return true; | |
5987 | } | |
5988 | ||
5989 | /* Link an input file into the linker output file. This function | |
5990 | handles all the sections and relocations of the input file at once. | |
5991 | This is so that we only have to read the local symbols once, and | |
5992 | don't have to keep them in memory. */ | |
5993 | ||
5994 | static boolean | |
5995 | elf_link_input_bfd (finfo, input_bfd) | |
5996 | struct elf_final_link_info *finfo; | |
5997 | bfd *input_bfd; | |
5998 | { | |
5999 | boolean (*relocate_section) PARAMS ((bfd *, struct bfd_link_info *, | |
6000 | bfd *, asection *, bfd_byte *, | |
6001 | Elf_Internal_Rela *, | |
eb4267a3 | 6002 | Elf_Internal_Sym *, asection **)); |
6ec3bb6a ILT |
6003 | bfd *output_bfd; |
6004 | Elf_Internal_Shdr *symtab_hdr; | |
5315c428 ILT |
6005 | size_t locsymcount; |
6006 | size_t extsymoff; | |
6ec3bb6a ILT |
6007 | Elf_External_Sym *esym; |
6008 | Elf_External_Sym *esymend; | |
6009 | Elf_Internal_Sym *isym; | |
6010 | long *pindex; | |
6011 | asection **ppsection; | |
6012 | asection *o; | |
6013 | ||
6014 | output_bfd = finfo->output_bfd; | |
6015 | relocate_section = | |
6016 | get_elf_backend_data (output_bfd)->elf_backend_relocate_section; | |
6017 | ||
013dec1a ILT |
6018 | /* If this is a dynamic object, we don't want to do anything here: |
6019 | we don't want the local symbols, and we don't want the section | |
6020 | contents. */ | |
6021 | if (elf_elfheader (input_bfd)->e_type == ET_DYN) | |
6022 | return true; | |
6023 | ||
6ec3bb6a | 6024 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
5315c428 ILT |
6025 | if (elf_bad_symtab (input_bfd)) |
6026 | { | |
6027 | locsymcount = symtab_hdr->sh_size / sizeof (Elf_External_Sym); | |
6028 | extsymoff = 0; | |
6029 | } | |
6030 | else | |
6031 | { | |
6032 | locsymcount = symtab_hdr->sh_info; | |
6033 | extsymoff = symtab_hdr->sh_info; | |
6034 | } | |
6035 | ||
6036 | /* Read the local symbols. */ | |
c46b8ed7 JL |
6037 | if (locsymcount > 0 |
6038 | && (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 | |
6039 | || (bfd_read (finfo->external_syms, sizeof (Elf_External_Sym), | |
6040 | locsymcount, input_bfd) | |
6041 | != locsymcount * sizeof (Elf_External_Sym)))) | |
6ec3bb6a ILT |
6042 | return false; |
6043 | ||
6044 | /* Swap in the local symbols and write out the ones which we know | |
6045 | are going into the output file. */ | |
6046 | esym = finfo->external_syms; | |
5315c428 | 6047 | esymend = esym + locsymcount; |
6ec3bb6a ILT |
6048 | isym = finfo->internal_syms; |
6049 | pindex = finfo->indices; | |
6050 | ppsection = finfo->sections; | |
6051 | for (; esym < esymend; esym++, isym++, pindex++, ppsection++) | |
6052 | { | |
6053 | asection *isec; | |
6054 | const char *name; | |
eb4267a3 | 6055 | Elf_Internal_Sym osym; |
6ec3bb6a ILT |
6056 | |
6057 | elf_swap_symbol_in (input_bfd, esym, isym); | |
6058 | *pindex = -1; | |
6059 | ||
5315c428 ILT |
6060 | if (elf_bad_symtab (input_bfd)) |
6061 | { | |
6062 | if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) | |
6063 | { | |
6064 | *ppsection = NULL; | |
6065 | continue; | |
6066 | } | |
6067 | } | |
6068 | ||
6ec3bb6a | 6069 | if (isym->st_shndx == SHN_UNDEF) |
badd23e3 | 6070 | isec = bfd_und_section_ptr; |
6ec3bb6a | 6071 | else if (isym->st_shndx > 0 && isym->st_shndx < SHN_LORESERVE) |
24f13b03 | 6072 | isec = section_from_elf_index (input_bfd, isym->st_shndx); |
6ec3bb6a | 6073 | else if (isym->st_shndx == SHN_ABS) |
badd23e3 | 6074 | isec = bfd_abs_section_ptr; |
6ec3bb6a | 6075 | else if (isym->st_shndx == SHN_COMMON) |
badd23e3 | 6076 | isec = bfd_com_section_ptr; |
6ec3bb6a ILT |
6077 | else |
6078 | { | |
6079 | /* Who knows? */ | |
6080 | isec = NULL; | |
6081 | } | |
6082 | ||
6083 | *ppsection = isec; | |
6084 | ||
6085 | /* Don't output the first, undefined, symbol. */ | |
6086 | if (esym == finfo->external_syms) | |
6087 | continue; | |
6088 | ||
6089 | /* If we are stripping all symbols, we don't want to output this | |
6090 | one. */ | |
6091 | if (finfo->info->strip == strip_all) | |
6092 | continue; | |
6093 | ||
6094 | /* We never output section symbols. Instead, we use the section | |
6095 | symbol of the corresponding section in the output file. */ | |
6096 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6097 | continue; | |
6098 | ||
6099 | /* If we are discarding all local symbols, we don't want to | |
6100 | output this one. If we are generating a relocateable output | |
6101 | file, then some of the local symbols may be required by | |
6102 | relocs; we output them below as we discover that they are | |
6103 | needed. */ | |
6104 | if (finfo->info->discard == discard_all) | |
6105 | continue; | |
6106 | ||
6107 | /* Get the name of the symbol. */ | |
6108 | name = elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, | |
6109 | isym->st_name); | |
6110 | if (name == NULL) | |
6111 | return false; | |
6112 | ||
6113 | /* See if we are discarding symbols with this name. */ | |
6114 | if ((finfo->info->strip == strip_some | |
6115 | && (bfd_hash_lookup (finfo->info->keep_hash, name, false, false) | |
6116 | == NULL)) | |
6117 | || (finfo->info->discard == discard_l | |
6118 | && strncmp (name, finfo->info->lprefix, | |
6119 | finfo->info->lprefix_len) == 0)) | |
6120 | continue; | |
6121 | ||
6122 | /* If we get here, we are going to output this symbol. */ | |
6123 | ||
eb4267a3 ILT |
6124 | osym = *isym; |
6125 | ||
6ec3bb6a | 6126 | /* Adjust the section index for the output file. */ |
eb4267a3 ILT |
6127 | osym.st_shndx = elf_section_from_bfd_section (output_bfd, |
6128 | isec->output_section); | |
6129 | if (osym.st_shndx == (unsigned short) -1) | |
6ec3bb6a ILT |
6130 | return false; |
6131 | ||
6132 | *pindex = output_bfd->symcount; | |
6133 | ||
6134 | /* ELF symbols in relocateable files are section relative, but | |
6135 | in executable files they are virtual addresses. Note that | |
6136 | this code assumes that all ELF sections have an associated | |
6137 | BFD section with a reasonable value for output_offset; below | |
6138 | we assume that they also have a reasonable value for | |
6139 | output_section. Any special sections must be set up to meet | |
6140 | these requirements. */ | |
eb4267a3 | 6141 | osym.st_value += isec->output_offset; |
6ec3bb6a | 6142 | if (! finfo->info->relocateable) |
eb4267a3 | 6143 | osym.st_value += isec->output_section->vma; |
6ec3bb6a | 6144 | |
eb4267a3 | 6145 | if (! elf_link_output_sym (finfo, name, &osym, isec)) |
6ec3bb6a | 6146 | return false; |
6ec3bb6a ILT |
6147 | } |
6148 | ||
6149 | /* Relocate the contents of each section. */ | |
6150 | for (o = input_bfd->sections; o != NULL; o = o->next) | |
6151 | { | |
6ec3bb6a ILT |
6152 | if ((o->flags & SEC_HAS_CONTENTS) == 0) |
6153 | continue; | |
6154 | ||
8af74670 ILT |
6155 | if ((o->flags & SEC_IN_MEMORY) != 0 |
6156 | && input_bfd == elf_hash_table (finfo->info)->dynobj) | |
6157 | { | |
6158 | /* Section was created by elf_link_create_dynamic_sections. | |
6159 | FIXME: This test is fragile. */ | |
6160 | continue; | |
6161 | } | |
6162 | ||
6ec3bb6a ILT |
6163 | /* Read the contents of the section. */ |
6164 | if (! bfd_get_section_contents (input_bfd, o, finfo->contents, | |
6165 | (file_ptr) 0, o->_raw_size)) | |
6166 | return false; | |
6167 | ||
6168 | if ((o->flags & SEC_RELOC) != 0) | |
6169 | { | |
374d2ef9 ILT |
6170 | Elf_Internal_Rela *internal_relocs; |
6171 | ||
6172 | /* Get the swapped relocs. */ | |
6173 | internal_relocs = elf_link_read_relocs (input_bfd, o, | |
6174 | finfo->external_relocs, | |
6175 | finfo->internal_relocs, | |
6176 | false); | |
6177 | if (internal_relocs == NULL | |
6178 | && o->reloc_count > 0) | |
6179 | return false; | |
6ec3bb6a ILT |
6180 | |
6181 | /* Relocate the section by invoking a back end routine. | |
6182 | ||
6183 | The back end routine is responsible for adjusting the | |
6184 | section contents as necessary, and (if using Rela relocs | |
6185 | and generating a relocateable output file) adjusting the | |
6186 | reloc addend as necessary. | |
6187 | ||
6188 | The back end routine does not have to worry about setting | |
6189 | the reloc address or the reloc symbol index. | |
6190 | ||
6191 | The back end routine is given a pointer to the swapped in | |
6192 | internal symbols, and can access the hash table entries | |
6193 | for the external symbols via elf_sym_hashes (input_bfd). | |
6194 | ||
6195 | When generating relocateable output, the back end routine | |
6196 | must handle STB_LOCAL/STT_SECTION symbols specially. The | |
6197 | output symbol is going to be a section symbol | |
6198 | corresponding to the output section, which will require | |
6199 | the addend to be adjusted. */ | |
6200 | ||
6201 | if (! (*relocate_section) (output_bfd, finfo->info, | |
6202 | input_bfd, o, | |
6203 | finfo->contents, | |
374d2ef9 | 6204 | internal_relocs, |
6ec3bb6a | 6205 | finfo->internal_syms, |
eb4267a3 | 6206 | finfo->sections)) |
6ec3bb6a ILT |
6207 | return false; |
6208 | ||
6209 | if (finfo->info->relocateable) | |
6210 | { | |
6211 | Elf_Internal_Rela *irela; | |
6212 | Elf_Internal_Rela *irelaend; | |
6213 | struct elf_link_hash_entry **rel_hash; | |
374d2ef9 | 6214 | Elf_Internal_Shdr *input_rel_hdr; |
6ec3bb6a ILT |
6215 | Elf_Internal_Shdr *output_rel_hdr; |
6216 | ||
6217 | /* Adjust the reloc addresses and symbol indices. */ | |
6218 | ||
374d2ef9 | 6219 | irela = internal_relocs; |
6ec3bb6a ILT |
6220 | irelaend = irela + o->reloc_count; |
6221 | rel_hash = (elf_section_data (o->output_section)->rel_hashes | |
6222 | + o->output_section->reloc_count); | |
6223 | for (; irela < irelaend; irela++, rel_hash++) | |
6224 | { | |
6225 | long r_symndx; | |
6226 | Elf_Internal_Sym *isym; | |
6227 | asection *sec; | |
6228 | ||
6229 | irela->r_offset += o->output_offset; | |
6230 | ||
6231 | r_symndx = ELF_R_SYM (irela->r_info); | |
6232 | ||
6233 | if (r_symndx == 0) | |
6234 | continue; | |
6235 | ||
5315c428 ILT |
6236 | if (r_symndx >= locsymcount |
6237 | || (elf_bad_symtab (input_bfd) | |
6238 | && finfo->sections[r_symndx] == NULL)) | |
6ec3bb6a ILT |
6239 | { |
6240 | long indx; | |
6241 | ||
6242 | /* This is a reloc against a global symbol. We | |
6243 | have not yet output all the local symbols, so | |
6244 | we do not know the symbol index of any global | |
6245 | symbol. We set the rel_hash entry for this | |
6246 | reloc to point to the global hash table entry | |
6247 | for this symbol. The symbol index is then | |
6248 | set at the end of elf_bfd_final_link. */ | |
5315c428 | 6249 | indx = r_symndx - extsymoff; |
6ec3bb6a ILT |
6250 | *rel_hash = elf_sym_hashes (input_bfd)[indx]; |
6251 | ||
6252 | /* Setting the index to -2 tells | |
6253 | elf_link_output_extsym that this symbol is | |
6254 | used by a reloc. */ | |
6255 | BFD_ASSERT ((*rel_hash)->indx < 0); | |
6256 | (*rel_hash)->indx = -2; | |
6257 | ||
6258 | continue; | |
6259 | } | |
6260 | ||
6261 | /* This is a reloc against a local symbol. */ | |
6262 | ||
6263 | *rel_hash = NULL; | |
6264 | isym = finfo->internal_syms + r_symndx; | |
6265 | sec = finfo->sections[r_symndx]; | |
6266 | if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) | |
6267 | { | |
6268 | /* I suppose the backend ought to fill in the | |
6269 | section of any STT_SECTION symbol against a | |
6270 | processor specific section. */ | |
badd23e3 ILT |
6271 | if (sec != NULL && bfd_is_abs_section (sec)) |
6272 | r_symndx = 0; | |
6273 | else if (sec == NULL || sec->owner == NULL) | |
6ec3bb6a ILT |
6274 | { |
6275 | bfd_set_error (bfd_error_bad_value); | |
6276 | return false; | |
6277 | } | |
6ec3bb6a | 6278 | else |
013dec1a ILT |
6279 | { |
6280 | r_symndx = sec->output_section->target_index; | |
6281 | if (r_symndx == 0) | |
6282 | abort (); | |
6283 | } | |
6ec3bb6a ILT |
6284 | } |
6285 | else | |
6286 | { | |
6287 | if (finfo->indices[r_symndx] == -1) | |
6288 | { | |
6289 | unsigned long link; | |
6290 | const char *name; | |
6291 | asection *osec; | |
6292 | ||
6293 | if (finfo->info->strip == strip_all) | |
6294 | { | |
6295 | /* You can't do ld -r -s. */ | |
6296 | bfd_set_error (bfd_error_invalid_operation); | |
6297 | return false; | |
6298 | } | |
6299 | ||
6300 | /* This symbol was skipped earlier, but | |
6301 | since it is needed by a reloc, we | |
6302 | must output it now. */ | |
6303 | link = symtab_hdr->sh_link; | |
6304 | name = elf_string_from_elf_section (input_bfd, | |
6305 | link, | |
6306 | isym->st_name); | |
6307 | if (name == NULL) | |
6308 | return false; | |
6309 | ||
6310 | osec = sec->output_section; | |
6311 | isym->st_shndx = | |
6312 | elf_section_from_bfd_section (output_bfd, | |
6313 | osec); | |
013dec1a | 6314 | if (isym->st_shndx == (unsigned short) -1) |
6ec3bb6a ILT |
6315 | return false; |
6316 | ||
6317 | isym->st_value += sec->output_offset; | |
6318 | if (! finfo->info->relocateable) | |
6319 | isym->st_value += osec->vma; | |
6320 | ||
6321 | finfo->indices[r_symndx] = output_bfd->symcount; | |
6322 | ||
71edd06d | 6323 | if (! elf_link_output_sym (finfo, name, isym, sec)) |
6ec3bb6a ILT |
6324 | return false; |
6325 | } | |
6326 | ||
6327 | r_symndx = finfo->indices[r_symndx]; | |
6328 | } | |
6329 | ||
6330 | irela->r_info = ELF_R_INFO (r_symndx, | |
6331 | ELF_R_TYPE (irela->r_info)); | |
6332 | } | |
6333 | ||
6334 | /* Swap out the relocs. */ | |
374d2ef9 | 6335 | input_rel_hdr = &elf_section_data (o)->rel_hdr; |
6ec3bb6a ILT |
6336 | output_rel_hdr = &elf_section_data (o->output_section)->rel_hdr; |
6337 | BFD_ASSERT (output_rel_hdr->sh_entsize | |
6338 | == input_rel_hdr->sh_entsize); | |
374d2ef9 | 6339 | irela = internal_relocs; |
6ec3bb6a ILT |
6340 | irelaend = irela + o->reloc_count; |
6341 | if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel)) | |
6342 | { | |
6343 | Elf_External_Rel *erel; | |
6344 | ||
6345 | erel = ((Elf_External_Rel *) output_rel_hdr->contents | |
6346 | + o->output_section->reloc_count); | |
6347 | for (; irela < irelaend; irela++, erel++) | |
6348 | { | |
6349 | Elf_Internal_Rel irel; | |
6350 | ||
6351 | irel.r_offset = irela->r_offset; | |
6352 | irel.r_info = irela->r_info; | |
6353 | BFD_ASSERT (irela->r_addend == 0); | |
6354 | elf_swap_reloc_out (output_bfd, &irel, erel); | |
6355 | } | |
6356 | } | |
6357 | else | |
6358 | { | |
6359 | Elf_External_Rela *erela; | |
6360 | ||
6361 | BFD_ASSERT (input_rel_hdr->sh_entsize | |
6362 | == sizeof (Elf_External_Rela)); | |
6363 | erela = ((Elf_External_Rela *) output_rel_hdr->contents | |
6364 | + o->output_section->reloc_count); | |
6365 | for (; irela < irelaend; irela++, erela++) | |
6366 | elf_swap_reloca_out (output_bfd, irela, erela); | |
6367 | } | |
6368 | ||
6369 | o->output_section->reloc_count += o->reloc_count; | |
6370 | } | |
6371 | } | |
6372 | ||
6373 | /* Write out the modified section contents. */ | |
6374 | if (! bfd_set_section_contents (output_bfd, o->output_section, | |
6375 | finfo->contents, o->output_offset, | |
6376 | (o->_cooked_size != 0 | |
6377 | ? o->_cooked_size | |
6378 | : o->_raw_size))) | |
6379 | return false; | |
6380 | } | |
6381 | ||
6382 | return true; | |
6383 | } | |
6384 | ||
6385 | /* Generate a reloc when linking an ELF file. This is a reloc | |
6386 | requested by the linker, and does come from any input file. This | |
6387 | is used to build constructor and destructor tables when linking | |
6388 | with -Ur. */ | |
6389 | ||
6390 | static boolean | |
6391 | elf_reloc_link_order (output_bfd, info, output_section, link_order) | |
6392 | bfd *output_bfd; | |
6393 | struct bfd_link_info *info; | |
6394 | asection *output_section; | |
6395 | struct bfd_link_order *link_order; | |
6396 | { | |
6397 | const reloc_howto_type *howto; | |
6398 | long indx; | |
6399 | bfd_vma offset; | |
6400 | struct elf_link_hash_entry **rel_hash_ptr; | |
6401 | Elf_Internal_Shdr *rel_hdr; | |
6402 | ||
6403 | howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); | |
6404 | if (howto == NULL) | |
6405 | { | |
6406 | bfd_set_error (bfd_error_bad_value); | |
6407 | return false; | |
6408 | } | |
6409 | ||
6410 | /* If this is an inplace reloc, we must write the addend into the | |
6411 | object file. */ | |
6412 | if (howto->partial_inplace | |
6413 | && link_order->u.reloc.p->addend != 0) | |
6414 | { | |
6415 | bfd_size_type size; | |
6416 | bfd_reloc_status_type rstat; | |
6417 | bfd_byte *buf; | |
6418 | boolean ok; | |
6419 | ||
6420 | size = bfd_get_reloc_size (howto); | |
6421 | buf = (bfd_byte *) bfd_zmalloc (size); | |
6422 | if (buf == (bfd_byte *) NULL) | |
6423 | { | |
6424 | bfd_set_error (bfd_error_no_memory); | |
6425 | return false; | |
6426 | } | |
6427 | rstat = _bfd_relocate_contents (howto, output_bfd, | |
6428 | link_order->u.reloc.p->addend, buf); | |
6429 | switch (rstat) | |
6430 | { | |
6431 | case bfd_reloc_ok: | |
6432 | break; | |
6433 | default: | |
6434 | case bfd_reloc_outofrange: | |
6435 | abort (); | |
6436 | case bfd_reloc_overflow: | |
6437 | if (! ((*info->callbacks->reloc_overflow) | |
6438 | (info, | |
6439 | (link_order->type == bfd_section_reloc_link_order | |
6440 | ? bfd_section_name (output_bfd, | |
6441 | link_order->u.reloc.p->u.section) | |
6442 | : link_order->u.reloc.p->u.name), | |
6443 | howto->name, link_order->u.reloc.p->addend, | |
6444 | (bfd *) NULL, (asection *) NULL, (bfd_vma) 0))) | |
6445 | { | |
6446 | free (buf); | |
6447 | return false; | |
6448 | } | |
6449 | break; | |
6450 | } | |
6451 | ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf, | |
6452 | (file_ptr) link_order->offset, size); | |
6453 | free (buf); | |
6454 | if (! ok) | |
6455 | return false; | |
6456 | } | |
6457 | ||
6458 | /* Figure out the symbol index. */ | |
6459 | rel_hash_ptr = (elf_section_data (output_section)->rel_hashes | |
6460 | + output_section->reloc_count); | |
6461 | if (link_order->type == bfd_section_reloc_link_order) | |
6462 | { | |
013dec1a ILT |
6463 | indx = link_order->u.reloc.p->u.section->target_index; |
6464 | if (indx == 0) | |
6465 | abort (); | |
6ec3bb6a ILT |
6466 | *rel_hash_ptr = NULL; |
6467 | } | |
6468 | else | |
6469 | { | |
6470 | struct elf_link_hash_entry *h; | |
6471 | ||
6472 | h = elf_link_hash_lookup (elf_hash_table (info), | |
6473 | link_order->u.reloc.p->u.name, | |
6474 | false, false, true); | |
6475 | if (h != NULL) | |
6476 | { | |
6477 | /* Setting the index to -2 tells elf_link_output_extsym that | |
6478 | this symbol is used by a reloc. */ | |
6479 | h->indx = -2; | |
6480 | *rel_hash_ptr = h; | |
6481 | indx = 0; | |
6482 | } | |
6483 | else | |
6484 | { | |
6485 | if (! ((*info->callbacks->unattached_reloc) | |
6486 | (info, link_order->u.reloc.p->u.name, (bfd *) NULL, | |
6487 | (asection *) NULL, (bfd_vma) 0))) | |
6488 | return false; | |
6489 | indx = 0; | |
6490 | } | |
6491 | } | |
6492 | ||
6493 | /* The address of a reloc is relative to the section in a | |
6494 | relocateable file, and is a virtual address in an executable | |
6495 | file. */ | |
6496 | offset = link_order->offset; | |
6497 | if (! info->relocateable) | |
6498 | offset += output_section->vma; | |
6499 | ||
6500 | rel_hdr = &elf_section_data (output_section)->rel_hdr; | |
6501 | ||
6502 | if (rel_hdr->sh_type == SHT_REL) | |
6503 | { | |
6504 | Elf_Internal_Rel irel; | |
6505 | Elf_External_Rel *erel; | |
6506 | ||
6507 | irel.r_offset = offset; | |
6508 | irel.r_info = ELF_R_INFO (indx, howto->type); | |
6509 | erel = ((Elf_External_Rel *) rel_hdr->contents | |
6510 | + output_section->reloc_count); | |
6511 | elf_swap_reloc_out (output_bfd, &irel, erel); | |
6512 | } | |
6513 | else | |
6514 | { | |
6515 | Elf_Internal_Rela irela; | |
6516 | Elf_External_Rela *erela; | |
6517 | ||
6518 | irela.r_offset = offset; | |
6519 | irela.r_info = ELF_R_INFO (indx, howto->type); | |
6520 | irela.r_addend = link_order->u.reloc.p->addend; | |
6521 | erela = ((Elf_External_Rela *) rel_hdr->contents | |
6522 | + output_section->reloc_count); | |
6523 | elf_swap_reloca_out (output_bfd, &irela, erela); | |
6524 | } | |
6525 | ||
6526 | ++output_section->reloc_count; | |
6527 | ||
6528 | return true; | |
6529 | } |