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