]>
Commit | Line | Data |
---|---|---|
81187b54 KR |
1 | /* ELF executable support for BFD. |
2 | Copyright 1991, 1992, 1993 Free Software Foundation, Inc. | |
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. | |
13 | ||
14 | This file is part of BFD, the Binary File Descriptor library. | |
15 | ||
16 | This program is free software; you can redistribute it and/or modify | |
17 | it under the terms of the GNU General Public License as published by | |
18 | the Free Software Foundation; either version 2 of the License, or | |
19 | (at your option) any later version. | |
20 | ||
21 | This program is distributed in the hope that it will be useful, | |
22 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
24 | GNU General Public License for more details. | |
25 | ||
26 | You should have received a copy of the GNU General Public License | |
27 | along with this program; if not, write to the Free Software | |
28 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
29 | ||
30 | ||
31 | /**************************************** | |
32 | ||
33 | WARNING | |
34 | ||
35 | This is only a partial ELF implementation, | |
36 | incorporating only those parts that are | |
37 | required to get gdb up and running. It is | |
38 | expected that it will be expanded to a full | |
39 | ELF implementation at some future date. | |
40 | ||
41 | Unimplemented stubs call abort() to ensure | |
42 | that they get proper attention if they are | |
43 | ever called. The stubs are here since | |
44 | this version was hacked from the COFF | |
45 | version, and thus they will probably | |
46 | go away or get expanded appropriately in a | |
47 | future version. | |
48 | ||
49 | [email protected] | |
50 | ||
51 | *****************************************/ | |
52 | ||
53 | ||
54 | /* Problems and other issues to resolve. | |
55 | ||
56 | (1) BFD expects there to be some fixed number of "sections" in | |
57 | the object file. I.E. there is a "section_count" variable in the | |
58 | bfd structure which contains the number of sections. However, ELF | |
59 | supports multiple "views" of a file. In particular, with current | |
60 | implementations, executable files typically have two tables, a | |
61 | program header table and a section header table, both of which | |
62 | partition the executable. | |
63 | ||
64 | In ELF-speak, the "linking view" of the file uses the section header | |
65 | table to access "sections" within the file, and the "execution view" | |
66 | uses the program header table to access "segments" within the file. | |
67 | "Segments" typically may contain all the data from one or more | |
68 | "sections". | |
69 | ||
70 | Note that the section header table is optional in ELF executables, | |
71 | but it is this information that is most useful to gdb. If the | |
72 | section header table is missing, then gdb should probably try | |
73 | to make do with the program header table. (FIXME) | |
74 | ||
75 | */ | |
76 | ||
286a4427 | 77 | #include <string.h> /* For strrchr and friends */ |
81187b54 KR |
78 | #include "bfd.h" |
79 | #include "sysdep.h" | |
80 | #include "libbfd.h" | |
81 | #include "libelf.h" | |
82 | ||
83 | #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */ | |
84 | #include <sys/procfs.h> | |
85 | #else | |
86 | #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */ | |
87 | #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */ | |
88 | #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */ | |
89 | #endif | |
90 | ||
91 | /* Forward declarations of static functions */ | |
92 | ||
93 | static char * | |
94 | elf_read PARAMS ((bfd *, long, int)); | |
95 | ||
96 | static struct sec * | |
97 | section_from_elf_index PARAMS ((bfd *, int)); | |
98 | ||
99 | static int | |
100 | elf_section_from_bfd_section PARAMS ((bfd *, struct sec *)); | |
101 | ||
102 | static boolean | |
103 | elf_slurp_symbol_table PARAMS ((bfd *, asymbol **)); | |
104 | ||
105 | static char * | |
106 | elf_get_str_section PARAMS ((bfd *, unsigned int)); | |
107 | ||
81187b54 KR |
108 | /* Some private data is stashed away for future use using the tdata pointer |
109 | in the bfd structure. */ | |
110 | ||
111 | struct elf_obj_tdata | |
112 | { | |
113 | Elf_Internal_Ehdr elf_header[1]; /* Actual data, but ref like ptr */ | |
114 | Elf_Internal_Shdr *elf_sect_ptr; | |
115 | struct strtab *strtab_ptr; | |
116 | int symtab_section; | |
117 | void *prstatus; /* The raw /proc prstatus structure */ | |
118 | void *prpsinfo; /* The raw /proc prpsinfo structure */ | |
fb6e80d5 KR |
119 | Elf_External_Sym *raw_syms; |
120 | Elf_Internal_Sym *internal_syms; | |
121 | elf_symbol_type *symbols; | |
81187b54 KR |
122 | }; |
123 | ||
124 | #define elf_tdata(bfd) ((bfd) -> tdata.elf_obj_data) | |
125 | #define elf_elfheader(bfd) (elf_tdata(bfd) -> elf_header) | |
126 | #define elf_elfsections(bfd) (elf_tdata(bfd) -> elf_sect_ptr) | |
127 | #define elf_shstrtab(bfd) (elf_tdata(bfd) -> strtab_ptr) | |
128 | #define elf_onesymtab(bfd) (elf_tdata(bfd) -> symtab_section) | |
129 | #define core_prpsinfo(bfd) (elf_tdata(bfd) -> prpsinfo) | |
130 | #define core_prstatus(bfd) (elf_tdata(bfd) -> prstatus) | |
fb6e80d5 KR |
131 | #define obj_symbols(bfd) (elf_tdata(bfd) -> symbols) |
132 | #define obj_raw_syms(bfd) (elf_tdata(bfd) -> raw_syms) | |
133 | #define obj_internal_syms(bfd) (elf_tdata(bfd) -> internal_syms) | |
81187b54 KR |
134 | |
135 | /* Translate an ELF symbol in external format into an ELF symbol in internal | |
136 | format. */ | |
137 | ||
138 | static void | |
139 | DEFUN(elf_swap_symbol_in,(abfd, src, dst), | |
140 | bfd *abfd AND | |
141 | Elf_External_Sym *src AND | |
142 | Elf_Internal_Sym *dst) | |
143 | { | |
144 | dst -> st_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_name); | |
145 | dst -> st_value = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_value); | |
146 | dst -> st_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> st_size); | |
147 | dst -> st_info = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_info); | |
148 | dst -> st_other = bfd_h_get_8 (abfd, (bfd_byte *) src -> st_other); | |
149 | dst -> st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> st_shndx); | |
150 | } | |
151 | ||
152 | /* Translate an ELF symbol in internal format into an ELF symbol in external | |
153 | format. */ | |
154 | ||
155 | static void | |
156 | DEFUN(elf_swap_symbol_out,(abfd, src, dst), | |
157 | bfd *abfd AND | |
158 | Elf_Internal_Sym *src AND | |
159 | Elf_External_Sym *dst) | |
160 | { | |
161 | bfd_h_put_32 (abfd, src->st_name, dst->st_name); | |
162 | bfd_h_put_32 (abfd, src->st_value, dst->st_value); | |
163 | bfd_h_put_32 (abfd, src->st_size, dst->st_size); | |
164 | bfd_h_put_8 (abfd, src->st_info, dst->st_info); | |
165 | bfd_h_put_8 (abfd, src->st_other, dst->st_other); | |
166 | bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx); | |
167 | } | |
168 | ||
169 | ||
170 | /* Translate an ELF file header in external format into an ELF file header in | |
171 | internal format. */ | |
172 | ||
173 | static void | |
174 | DEFUN(elf_swap_ehdr_in,(abfd, src, dst), | |
175 | bfd *abfd AND | |
176 | Elf_External_Ehdr *src AND | |
177 | Elf_Internal_Ehdr *dst) | |
178 | { | |
179 | memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT); | |
180 | dst -> e_type = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_type); | |
181 | dst -> e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_machine); | |
182 | dst -> e_version = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_version); | |
183 | dst -> e_entry = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_entry); | |
184 | dst -> e_phoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_phoff); | |
185 | dst -> e_shoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_shoff); | |
186 | dst -> e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_flags); | |
187 | dst -> e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_ehsize); | |
188 | dst -> e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phentsize); | |
189 | dst -> e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phnum); | |
190 | dst -> e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shentsize); | |
191 | dst -> e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shnum); | |
192 | dst -> e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shstrndx); | |
193 | } | |
194 | ||
195 | /* Translate an ELF file header in internal format into an ELF file header in | |
196 | external format. */ | |
197 | ||
198 | static void | |
199 | DEFUN(elf_swap_ehdr_out,(abfd, src, dst), | |
200 | bfd *abfd AND | |
201 | Elf_Internal_Ehdr *src AND | |
202 | Elf_External_Ehdr *dst) | |
203 | { | |
204 | memcpy (dst -> e_ident, src -> e_ident, EI_NIDENT); | |
205 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
206 | bfd_h_put_16 (abfd, src->e_type, dst->e_type); | |
207 | bfd_h_put_16 (abfd, src->e_machine, dst->e_machine); | |
208 | bfd_h_put_32 (abfd, src->e_version, dst->e_version); | |
209 | bfd_h_put_32 (abfd, src->e_entry, dst->e_entry); | |
210 | bfd_h_put_32 (abfd, src->e_phoff, dst->e_phoff); | |
211 | bfd_h_put_32 (abfd, src->e_shoff, dst->e_shoff); | |
212 | bfd_h_put_32 (abfd, src->e_flags, dst->e_flags); | |
213 | bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize); | |
214 | bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize); | |
215 | bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum); | |
216 | bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize); | |
217 | bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum); | |
218 | bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx); | |
219 | } | |
220 | ||
221 | ||
222 | /* Translate an ELF section header table entry in external format into an | |
223 | ELF section header table entry in internal format. */ | |
224 | ||
225 | static void | |
226 | DEFUN(elf_swap_shdr_in,(abfd, src, dst), | |
227 | bfd *abfd AND | |
228 | Elf_External_Shdr *src AND | |
229 | Elf_Internal_Shdr *dst) | |
230 | { | |
231 | dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name); | |
232 | dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type); | |
233 | dst->sh_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_flags); | |
234 | dst->sh_addr = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_addr); | |
235 | dst->sh_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_offset); | |
236 | dst->sh_size = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_size); | |
237 | dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link); | |
238 | dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info); | |
239 | dst->sh_addralign = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_addralign); | |
240 | dst->sh_entsize = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_entsize); | |
241 | /* we haven't done any processing on it yet, so... */ | |
242 | dst->rawdata = (void*)0; | |
243 | } | |
244 | ||
245 | /* Translate an ELF section header table entry in internal format into an | |
246 | ELF section header table entry in external format. */ | |
247 | ||
248 | static void | |
249 | DEFUN(elf_swap_shdr_out,(abfd, src, dst), | |
250 | bfd *abfd AND | |
251 | Elf_Internal_Shdr *src AND | |
252 | Elf_External_Shdr *dst) | |
253 | { | |
254 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
255 | bfd_h_put_32 (abfd, src->sh_name, dst->sh_name); | |
256 | bfd_h_put_32 (abfd, src->sh_type, dst->sh_type); | |
257 | bfd_h_put_32 (abfd, src->sh_flags, dst->sh_flags); | |
258 | bfd_h_put_32 (abfd, src->sh_addr, dst->sh_addr); | |
259 | bfd_h_put_32 (abfd, src->sh_offset, dst->sh_offset); | |
260 | bfd_h_put_32 (abfd, src->sh_size, dst->sh_size); | |
261 | bfd_h_put_32 (abfd, src->sh_link, dst->sh_link); | |
262 | bfd_h_put_32 (abfd, src->sh_info, dst->sh_info); | |
263 | bfd_h_put_32 (abfd, src->sh_addralign, dst->sh_addralign); | |
264 | bfd_h_put_32 (abfd, src->sh_entsize, dst->sh_entsize); | |
265 | } | |
266 | ||
267 | ||
268 | /* Translate an ELF program header table entry in external format into an | |
269 | ELF program header table entry in internal format. */ | |
270 | ||
271 | static void | |
272 | DEFUN(elf_swap_phdr_in,(abfd, src, dst), | |
273 | bfd *abfd AND | |
274 | Elf_External_Phdr *src AND | |
275 | Elf_Internal_Phdr *dst) | |
276 | { | |
277 | dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type); | |
278 | dst->p_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->p_offset); | |
279 | dst->p_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) src->p_vaddr); | |
280 | dst->p_paddr = bfd_h_get_32 (abfd, (bfd_byte *) src->p_paddr); | |
281 | dst->p_filesz = bfd_h_get_32 (abfd, (bfd_byte *) src->p_filesz); | |
282 | dst->p_memsz = bfd_h_get_32 (abfd, (bfd_byte *) src->p_memsz); | |
283 | dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags); | |
284 | dst->p_align = bfd_h_get_32 (abfd, (bfd_byte *) src->p_align); | |
285 | } | |
286 | ||
fb6e80d5 KR |
287 | /* ... */ |
288 | ||
289 | static void | |
290 | DEFUN(elf_swap_phdr_out,(abfd, src, dst), | |
291 | bfd *abfd AND | |
292 | Elf_Internal_Phdr *src AND | |
293 | Elf_External_Phdr *dst) | |
294 | { | |
295 | /* note that all elements of dst are *arrays of unsigned char* already... */ | |
296 | bfd_h_put_32 (abfd, src->p_type, dst->p_type); | |
297 | bfd_h_put_32 (abfd, src->p_offset, dst->p_offset); | |
298 | bfd_h_put_32 (abfd, src->p_vaddr, dst->p_vaddr); | |
299 | bfd_h_put_32 (abfd, src->p_paddr, dst->p_paddr); | |
300 | bfd_h_put_32 (abfd, src->p_filesz, dst->p_filesz); | |
301 | bfd_h_put_32 (abfd, src->p_memsz, dst->p_memsz); | |
302 | bfd_h_put_32 (abfd, src->p_flags, dst->p_flags); | |
303 | bfd_h_put_32 (abfd, src->p_align, dst->p_align); | |
304 | } | |
81187b54 KR |
305 | |
306 | /* Translate an ELF reloc from external format to internal format. */ | |
307 | static void | |
308 | DEFUN(elf_swap_reloc_in,(abfd, src, dst), | |
309 | bfd *abfd AND | |
310 | Elf_External_Rel *src AND | |
311 | Elf_Internal_Rel *dst) | |
312 | { | |
313 | dst->r_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->r_offset); | |
314 | dst->r_info = bfd_h_get_32 (abfd, (bfd_byte *) src->r_info); | |
315 | } | |
316 | ||
317 | static void | |
318 | DEFUN(elf_swap_reloca_in,(abfd, src, dst), | |
319 | bfd *abfd AND | |
320 | Elf_External_Rela *src AND | |
321 | Elf_Internal_Rela *dst) | |
322 | { | |
323 | dst->r_offset = bfd_h_get_32 (abfd, (bfd_byte *) src->r_offset); | |
324 | dst->r_info = bfd_h_get_32 (abfd, (bfd_byte *) src->r_info); | |
325 | dst->r_addend = bfd_h_get_32 (abfd, (bfd_byte *) src->r_addend); | |
326 | } | |
327 | ||
328 | /* Translate an ELF reloc from internal format to external format. */ | |
329 | static void | |
330 | DEFUN(elf_swap_reloc_out,(abfd, src, dst), | |
331 | bfd *abfd AND | |
332 | Elf_Internal_Rel *src AND | |
333 | Elf_External_Rel *dst) | |
334 | { | |
335 | bfd_h_put_32 (abfd, src->r_offset, dst->r_offset); | |
336 | bfd_h_put_32 (abfd, src->r_info, dst->r_info); | |
337 | } | |
338 | ||
339 | static void | |
340 | DEFUN(elf_swap_reloca_out,(abfd, src, dst), | |
341 | bfd *abfd AND | |
342 | Elf_Internal_Rela *src AND | |
343 | Elf_External_Rela *dst) | |
344 | { | |
345 | bfd_h_put_32 (abfd, src->r_offset, dst->r_offset); | |
346 | bfd_h_put_32 (abfd, src->r_info, dst->r_info); | |
347 | bfd_h_put_32 (abfd, src->r_addend, dst->r_addend); | |
348 | } | |
349 | ||
350 | /* | |
351 | INTERNAL_FUNCTION | |
352 | bfd_elf_find_section | |
353 | ||
354 | SYNOPSIS | |
355 | struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); | |
356 | ||
357 | DESCRIPTION | |
358 | Helper functions for GDB to locate the string tables. | |
359 | Since BFD hides string tables from callers, GDB needs to use an | |
360 | internal hook to find them. Sun's .stabstr, in particular, | |
361 | isn't even pointed to by the .stab section, so ordinary | |
362 | mechanisms wouldn't work to find it, even if we had some. | |
363 | */ | |
364 | ||
365 | struct elf_internal_shdr * | |
366 | DEFUN(bfd_elf_find_section, (abfd, name), | |
367 | bfd *abfd AND | |
368 | char *name) | |
369 | { | |
370 | Elf_Internal_Shdr *i_shdrp; | |
371 | Elf_Internal_Shdr *gotit = NULL; | |
372 | char *shstrtab; | |
373 | unsigned int max; | |
374 | unsigned int i; | |
375 | ||
376 | i_shdrp = elf_elfsections (abfd); | |
377 | if (i_shdrp != NULL) | |
378 | { | |
379 | shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx); | |
380 | if (shstrtab != NULL) | |
381 | { | |
382 | max = elf_elfheader (abfd)->e_shnum; | |
383 | for (i = 1; i < max; i++) | |
384 | { | |
385 | if (!strcmp (&shstrtab[i_shdrp[i].sh_name], name)) | |
386 | { | |
387 | gotit = &i_shdrp[i]; | |
388 | } | |
389 | } | |
390 | } | |
391 | } | |
392 | return (gotit); | |
393 | } | |
394 | ||
395 | /* End of GDB support. */ | |
396 | ||
397 | static char * | |
398 | DEFUN(elf_get_str_section, (abfd, shindex), | |
399 | bfd *abfd AND | |
400 | unsigned int shindex) | |
401 | { | |
402 | Elf_Internal_Shdr *i_shdrp; | |
403 | char *shstrtab = NULL; | |
404 | unsigned int offset; | |
405 | unsigned int shstrtabsize; | |
406 | ||
407 | i_shdrp = elf_elfsections (abfd); | |
408 | if (i_shdrp != NULL) | |
409 | { | |
410 | shstrtab = i_shdrp[shindex].rawdata; | |
411 | if (shstrtab == NULL) | |
412 | { | |
413 | /* No cached one, attempt to read, and cache what we read. */ | |
414 | offset = i_shdrp[shindex].sh_offset; | |
415 | shstrtabsize = i_shdrp[shindex].sh_size; | |
416 | shstrtab = elf_read (abfd, offset, shstrtabsize); | |
417 | i_shdrp[shindex].rawdata = (void*) shstrtab; | |
418 | } | |
419 | } | |
420 | return (shstrtab); | |
421 | } | |
422 | ||
423 | static char * | |
424 | DEFUN(elf_string_from_elf_section, (abfd, shindex, strindex), | |
425 | bfd *abfd AND | |
426 | unsigned int shindex AND | |
427 | unsigned int strindex) | |
428 | { | |
429 | Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd); | |
430 | Elf_Internal_Shdr *hdr = i_shdrp + shindex; | |
431 | ||
432 | if (! hdr->rawdata) | |
433 | { | |
434 | if (elf_get_str_section (abfd, shindex) == NULL) | |
435 | { | |
436 | return NULL; | |
437 | } | |
438 | } | |
439 | return ((char*)hdr->rawdata)+strindex; | |
440 | } | |
441 | ||
442 | #define elf_string_from_elf_strtab(abfd, strindex) \ | |
443 | elf_string_from_elf_section (abfd, elf_elfheader(abfd)->e_shstrndx, strindex) | |
444 | ||
445 | /* Create a new bfd section from an ELF section header. */ | |
446 | ||
447 | static boolean | |
448 | DEFUN(bfd_section_from_shdr, (abfd, shindex), | |
449 | bfd *abfd AND | |
450 | unsigned int shindex) | |
451 | { | |
452 | Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd); | |
453 | Elf_Internal_Shdr *hdr = i_shdrp + shindex; | |
454 | asection *newsect; | |
455 | char *name; | |
456 | ||
fb6e80d5 | 457 | name = hdr->sh_name ? elf_string_from_elf_strtab (abfd, hdr->sh_name) : ""; |
81187b54 KR |
458 | |
459 | switch(hdr->sh_type) { | |
460 | ||
461 | case SHT_NULL: | |
462 | /* inactive section. Throw it away. */ | |
463 | return true; | |
464 | ||
465 | case SHT_PROGBITS: | |
81187b54 KR |
466 | /* Bits that get saved. This one is real. */ |
467 | if (! hdr->rawdata ) | |
468 | { | |
469 | newsect = bfd_make_section (abfd, name); | |
286a4427 | 470 | if (newsect != NULL) |
81187b54 | 471 | { |
286a4427 FF |
472 | newsect->vma = hdr->sh_addr; |
473 | newsect->_raw_size = hdr->sh_size; | |
474 | newsect->filepos = hdr->sh_offset; /* so we can read back the bits */ | |
475 | newsect->flags |= SEC_HAS_CONTENTS; | |
476 | ||
477 | if (hdr->sh_flags & SHF_ALLOC) | |
478 | { | |
479 | newsect->flags |= SEC_ALLOC; | |
fb6e80d5 | 480 | newsect->flags |= SEC_LOAD; |
286a4427 FF |
481 | } |
482 | ||
483 | if (!(hdr->sh_flags & SHF_WRITE)) | |
484 | newsect->flags |= SEC_READONLY; | |
485 | ||
486 | if (hdr->sh_flags & SHF_EXECINSTR) | |
487 | newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */ | |
488 | else | |
489 | newsect->flags |= SEC_DATA; | |
490 | ||
491 | hdr->rawdata = (void*)newsect; | |
81187b54 | 492 | } |
81187b54 KR |
493 | } |
494 | return true; | |
fb6e80d5 KR |
495 | |
496 | case SHT_NOBITS: | |
497 | /* Bits that get saved. This one is real. */ | |
498 | if (! hdr->rawdata ) | |
499 | { | |
500 | newsect = bfd_make_section (abfd, name); | |
501 | if (newsect != NULL) | |
502 | { | |
503 | newsect->vma = hdr->sh_addr; | |
504 | newsect->_raw_size = hdr->sh_size; | |
505 | newsect->filepos = hdr->sh_offset; /* fake */ | |
506 | if (hdr->sh_flags & SHF_ALLOC) | |
507 | newsect->flags |= SEC_ALLOC; | |
508 | ||
509 | if (!(hdr->sh_flags & SHF_WRITE)) | |
510 | newsect->flags |= SEC_READONLY; | |
511 | ||
512 | if (hdr->sh_flags & SHF_EXECINSTR) | |
513 | newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */ | |
514 | else | |
515 | newsect->flags |= SEC_DATA; | |
516 | ||
517 | hdr->rawdata = (void*)newsect; | |
518 | } | |
519 | } | |
520 | return true; | |
81187b54 KR |
521 | |
522 | case SHT_SYMTAB: /* A symbol table */ | |
523 | BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym)); | |
524 | elf_onesymtab (abfd) = shindex; | |
525 | abfd->flags |= HAS_SYMS; | |
526 | return true; | |
527 | ||
528 | case SHT_STRTAB: /* A string table */ | |
529 | return true; | |
530 | ||
531 | case SHT_REL: | |
532 | case SHT_RELA: | |
533 | /* *these* do a lot of work -- but build no sections! */ | |
534 | /* the spec says there can be multiple strtabs, but only one symtab */ | |
535 | /* but there can be lots of REL* sections. */ | |
536 | /* FIXME: The above statement is wrong! There are typically at least | |
537 | two symbol tables in a dynamically linked executable, ".dynsym" | |
538 | which is the dynamic linkage symbol table and ".symtab", which is | |
539 | the "traditional" symbol table. -fnf */ | |
540 | ||
541 | { | |
542 | asection *target_sect; | |
543 | ||
544 | bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */ | |
545 | bfd_section_from_shdr (abfd, hdr->sh_info); /* target */ | |
546 | target_sect = section_from_elf_index (abfd, hdr->sh_info); | |
547 | if (target_sect == NULL) | |
548 | return false; | |
549 | ||
550 | #if 0 | |
551 | /* FIXME: We are only prepared to read one symbol table, so | |
552 | do NOT read the dynamic symbol table since it is only a | |
553 | subset of the full symbol table. Also see comment above. -fnf */ | |
554 | if (!elf_slurp_symbol_table(abfd, i_shdrp + hdr->sh_link)) | |
555 | return false; | |
556 | #endif | |
557 | ||
558 | target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize; | |
559 | target_sect->flags |= SEC_RELOC; | |
560 | target_sect->relocation = 0; | |
561 | target_sect->rel_filepos = hdr->sh_offset; | |
562 | return true; | |
563 | } | |
564 | break; | |
565 | ||
566 | case SHT_HASH: | |
567 | case SHT_DYNAMIC: | |
568 | case SHT_DYNSYM: /* could treat this like symtab... */ | |
569 | #if 0 | |
570 | fprintf(stderr, "Dynamic Linking sections not yet supported.\n"); | |
571 | abort (); | |
572 | #endif | |
573 | break; | |
574 | ||
575 | case SHT_NOTE: | |
576 | #if 0 | |
577 | fprintf(stderr, "Note Sections not yet supported.\n"); | |
578 | abort (); | |
579 | #endif | |
580 | break; | |
581 | ||
582 | case SHT_SHLIB: | |
583 | #if 0 | |
584 | fprintf(stderr, "SHLIB Sections not supported (and non conforming.)\n"); | |
585 | #endif | |
586 | return true; | |
587 | ||
588 | default: | |
589 | break; | |
590 | } | |
591 | ||
592 | return (true); | |
593 | } | |
594 | ||
595 | ||
596 | ||
597 | ||
598 | struct strtab { | |
599 | char *tab; | |
600 | int nentries; | |
601 | int length; | |
602 | }; | |
603 | ||
604 | ||
605 | static struct strtab * | |
606 | DEFUN(bfd_new_strtab, (abfd), | |
607 | bfd *abfd) | |
608 | { | |
609 | struct strtab *ss; | |
610 | ||
611 | ss = (struct strtab *) bfd_xmalloc(sizeof(struct strtab)); | |
612 | ss->tab = bfd_xmalloc(1); | |
613 | BFD_ASSERT(ss->tab != 0); | |
614 | *ss->tab = 0; | |
615 | ss->nentries = 0; | |
616 | ss->length = 1; | |
617 | ||
618 | return ss; | |
619 | } | |
620 | ||
621 | static int | |
622 | DEFUN(bfd_add_to_strtab, (abfd, ss, str), | |
623 | bfd *abfd AND | |
624 | struct strtab *ss AND | |
625 | CONST char *str) | |
626 | { | |
627 | /* should search first, but for now: */ | |
628 | /* include the trailing NUL */ | |
629 | int ln = strlen(str)+1; | |
630 | ||
631 | /* should this be using obstacks? */ | |
632 | ss->tab = realloc(ss->tab, ss->length + ln); | |
633 | ||
634 | BFD_ASSERT(ss->tab != 0); | |
635 | strcpy(ss->tab + ss->length, str); | |
636 | ss->nentries++; | |
637 | ss->length += ln; | |
638 | ||
639 | return ss->length - ln; | |
640 | } | |
641 | ||
642 | static int | |
643 | DEFUN(bfd_add_2_to_strtab, (abfd, ss, str, str2), | |
644 | bfd *abfd AND | |
645 | struct strtab *ss AND | |
646 | char *str AND | |
647 | CONST char *str2) | |
648 | { | |
649 | /* should search first, but for now: */ | |
650 | /* include the trailing NUL */ | |
651 | int ln = strlen(str)+strlen(str2)+1; | |
652 | ||
653 | /* should this be using obstacks? */ | |
654 | if (ss->length) | |
655 | ss->tab = realloc(ss->tab, ss->length + ln); | |
656 | else | |
657 | ss->tab = bfd_xmalloc(ln); | |
658 | ||
659 | BFD_ASSERT(ss->tab != 0); | |
660 | strcpy(ss->tab + ss->length, str); | |
661 | strcpy(ss->tab + ss->length + strlen(str), str2); | |
662 | ss->nentries++; | |
663 | ss->length += ln; | |
664 | ||
665 | return ss->length - ln; | |
666 | } | |
667 | ||
668 | /* Create a new ELF section from a bfd section. */ | |
669 | ||
670 | static boolean | |
671 | DEFUN(bfd_shdr_from_section, (abfd, hdr, shstrtab, indx), | |
672 | bfd *abfd AND | |
673 | Elf_Internal_Shdr *hdr AND | |
674 | struct strtab *shstrtab AND | |
675 | int indx) | |
676 | { | |
677 | asection *sect; | |
678 | int ndx; | |
679 | ||
680 | /* figure out out to write the section name from the bfd section name. MWE */ | |
681 | ||
682 | sect = abfd->sections; | |
683 | for (ndx = indx; --ndx; ) | |
684 | { | |
685 | sect = sect->next; | |
686 | } | |
687 | hdr[indx].sh_name = bfd_add_to_strtab(abfd, shstrtab, | |
688 | bfd_section_name(abfd, sect)); | |
689 | hdr[indx].sh_addr = sect->vma; | |
690 | hdr[indx].sh_size = sect->_raw_size; | |
691 | hdr[indx].sh_flags = 0; | |
692 | /* these need to be preserved on */ | |
693 | hdr[indx].sh_link = 0; | |
694 | hdr[indx].sh_info = 0; | |
695 | hdr[indx].sh_addralign = 0; | |
696 | hdr[indx].sh_entsize = 0; | |
697 | ||
698 | hdr[indx].sh_type = 0; | |
699 | if (sect->flags & SEC_RELOC) { | |
700 | hdr[indx].sh_type = SHT_RELA; /* FIXME -- sparc specific */ | |
701 | } | |
702 | ||
703 | if (sect->flags & SEC_HAS_CONTENTS) | |
704 | { | |
705 | hdr[indx].sh_offset = sect->filepos; | |
706 | hdr[indx].sh_size = sect->_raw_size; | |
707 | } | |
708 | if (sect->flags & SEC_ALLOC) | |
709 | { | |
710 | hdr[indx].sh_flags |= SHF_ALLOC; | |
711 | if (sect->flags & SEC_LOAD) | |
712 | { | |
713 | /* do something with sh_type ? */ | |
714 | } | |
715 | } | |
716 | if (!(sect->flags & SEC_READONLY)) | |
717 | hdr[indx].sh_flags |= SHF_WRITE; | |
718 | ||
719 | if (sect->flags & SEC_CODE) | |
720 | hdr[indx].sh_flags |= SHF_EXECINSTR; | |
721 | ||
722 | return (true); | |
723 | } | |
724 | ||
725 | /* Create a new bfd section from an ELF program header. | |
726 | ||
727 | Since program segments have no names, we generate a synthetic name | |
728 | of the form segment<NUM>, where NUM is generally the index in the | |
729 | program header table. For segments that are split (see below) we | |
730 | generate the names segment<NUM>a and segment<NUM>b. | |
731 | ||
732 | Note that some program segments may have a file size that is different than | |
733 | (less than) the memory size. All this means is that at execution the | |
734 | system must allocate the amount of memory specified by the memory size, | |
735 | but only initialize it with the first "file size" bytes read from the | |
736 | file. This would occur for example, with program segments consisting | |
737 | of combined data+bss. | |
738 | ||
739 | To handle the above situation, this routine generates TWO bfd sections | |
740 | for the single program segment. The first has the length specified by | |
741 | the file size of the segment, and the second has the length specified | |
742 | by the difference between the two sizes. In effect, the segment is split | |
743 | into it's initialized and uninitialized parts. | |
744 | ||
745 | */ | |
746 | ||
747 | static boolean | |
748 | DEFUN(bfd_section_from_phdr, (abfd, hdr, index), | |
749 | bfd *abfd AND | |
750 | Elf_Internal_Phdr *hdr AND | |
751 | int index) | |
752 | { | |
753 | asection *newsect; | |
754 | char *name; | |
755 | char namebuf[64]; | |
756 | int split; | |
757 | ||
758 | split = ((hdr -> p_memsz > 0) && | |
759 | (hdr -> p_filesz > 0) && | |
760 | (hdr -> p_memsz > hdr -> p_filesz)); | |
761 | sprintf (namebuf, split ? "segment%da" : "segment%d", index); | |
762 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
763 | strcpy (name, namebuf); | |
764 | newsect = bfd_make_section (abfd, name); | |
765 | newsect -> vma = hdr -> p_vaddr; | |
766 | newsect -> _raw_size = hdr -> p_filesz; | |
767 | newsect -> filepos = hdr -> p_offset; | |
768 | newsect -> flags |= SEC_HAS_CONTENTS; | |
769 | if (hdr -> p_type == PT_LOAD) | |
770 | { | |
771 | newsect -> flags |= SEC_ALLOC; | |
772 | newsect -> flags |= SEC_LOAD; | |
773 | if (hdr -> p_flags & PF_X) | |
774 | { | |
775 | /* FIXME: all we known is that it has execute PERMISSION, | |
776 | may be data. */ | |
777 | newsect -> flags |= SEC_CODE; | |
778 | } | |
779 | } | |
780 | if (!(hdr -> p_flags & PF_W)) | |
781 | { | |
782 | newsect -> flags |= SEC_READONLY; | |
783 | } | |
784 | ||
785 | if (split) | |
786 | { | |
787 | sprintf (namebuf, "segment%db", index); | |
788 | name = bfd_alloc (abfd, strlen (namebuf) + 1); | |
789 | strcpy (name, namebuf); | |
790 | newsect = bfd_make_section (abfd, name); | |
791 | newsect -> vma = hdr -> p_vaddr + hdr -> p_filesz; | |
792 | newsect -> _raw_size = hdr -> p_memsz - hdr -> p_filesz; | |
793 | if (hdr -> p_type == PT_LOAD) | |
794 | { | |
795 | newsect -> flags |= SEC_ALLOC; | |
796 | if (hdr -> p_flags & PF_X) | |
797 | newsect -> flags |= SEC_CODE; | |
798 | } | |
799 | if (!(hdr -> p_flags & PF_W)) | |
800 | newsect -> flags |= SEC_READONLY; | |
801 | } | |
802 | ||
803 | return (true); | |
804 | } | |
805 | ||
806 | #ifdef HAVE_PROCFS | |
807 | ||
808 | static void | |
809 | DEFUN(bfd_prstatus,(abfd, descdata, descsz, filepos), | |
810 | bfd *abfd AND | |
811 | char *descdata AND | |
812 | int descsz AND | |
813 | long filepos) | |
814 | { | |
815 | asection *newsect; | |
816 | prstatus_t *status = (prstatus_t *)0; | |
817 | ||
818 | if (descsz == sizeof (prstatus_t)) | |
819 | { | |
820 | newsect = bfd_make_section (abfd, ".reg"); | |
821 | newsect -> _raw_size = sizeof (status->pr_reg); | |
822 | newsect -> filepos = filepos + (long) &status->pr_reg; | |
823 | newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
824 | newsect -> alignment_power = 2; | |
825 | if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL) | |
826 | { | |
827 | memcpy (core_prstatus (abfd), descdata, descsz); | |
828 | } | |
829 | } | |
830 | } | |
831 | ||
832 | /* Stash a copy of the prpsinfo structure away for future use. */ | |
833 | ||
834 | static void | |
835 | DEFUN(bfd_prpsinfo,(abfd, descdata, descsz, filepos), | |
836 | bfd *abfd AND | |
837 | char *descdata AND | |
838 | int descsz AND | |
839 | long filepos) | |
840 | { | |
841 | asection *newsect; | |
842 | ||
843 | if (descsz == sizeof (prpsinfo_t)) | |
844 | { | |
845 | if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL) | |
846 | { | |
847 | memcpy (core_prpsinfo (abfd), descdata, descsz); | |
848 | } | |
849 | } | |
850 | } | |
851 | ||
852 | static void | |
853 | DEFUN(bfd_fpregset,(abfd, descdata, descsz, filepos), | |
854 | bfd *abfd AND | |
855 | char *descdata AND | |
856 | int descsz AND | |
857 | long filepos) | |
858 | { | |
859 | asection *newsect; | |
860 | ||
861 | newsect = bfd_make_section (abfd, ".reg2"); | |
862 | newsect -> _raw_size = descsz; | |
863 | newsect -> filepos = filepos; | |
864 | newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
865 | newsect -> alignment_power = 2; | |
866 | } | |
867 | ||
868 | #endif /* HAVE_PROCFS */ | |
869 | ||
870 | /* Return a pointer to the args (including the command name) that were | |
871 | seen by the program that generated the core dump. Note that for | |
872 | some reason, a spurious space is tacked onto the end of the args | |
873 | in some (at least one anyway) implementations, so strip it off if | |
874 | it exists. */ | |
875 | ||
876 | char * | |
877 | DEFUN(elf_core_file_failing_command, (abfd), | |
878 | bfd *abfd) | |
879 | { | |
880 | #ifdef HAVE_PROCFS | |
881 | if (core_prpsinfo (abfd)) | |
882 | { | |
883 | prpsinfo_t *p = core_prpsinfo (abfd); | |
884 | char *scan = p -> pr_psargs; | |
885 | while (*scan++) {;} | |
886 | scan -= 2; | |
887 | if ((scan > p -> pr_psargs) && (*scan == ' ')) | |
888 | { | |
889 | *scan = '\000'; | |
890 | } | |
891 | return (p -> pr_psargs); | |
892 | } | |
893 | #endif | |
894 | return (NULL); | |
895 | } | |
896 | ||
897 | /* Return the number of the signal that caused the core dump. Presumably, | |
898 | since we have a core file, we got a signal of some kind, so don't bother | |
899 | checking the other process status fields, just return the signal number. | |
900 | */ | |
901 | ||
902 | int | |
903 | DEFUN(elf_core_file_failing_signal, (abfd), | |
904 | bfd *abfd) | |
905 | { | |
906 | #ifdef HAVE_PROCFS | |
907 | if (core_prstatus (abfd)) | |
908 | { | |
909 | return (((prstatus_t *)(core_prstatus (abfd))) -> pr_cursig); | |
910 | } | |
911 | #endif | |
912 | return (-1); | |
913 | } | |
914 | ||
915 | /* Check to see if the core file could reasonably be expected to have | |
916 | come for the current executable file. Note that by default we return | |
917 | true unless we find something that indicates that there might be a | |
918 | problem. | |
919 | */ | |
920 | ||
921 | boolean | |
922 | DEFUN(elf_core_file_matches_executable_p, (core_bfd, exec_bfd), | |
923 | bfd *core_bfd AND | |
924 | bfd *exec_bfd) | |
925 | { | |
926 | #ifdef HAVE_PROCFS | |
927 | char *corename; | |
928 | char *execname; | |
929 | #endif | |
930 | ||
931 | /* First, xvecs must match since both are ELF files for the same target. */ | |
932 | ||
933 | if (core_bfd->xvec != exec_bfd->xvec) | |
934 | { | |
935 | bfd_error = system_call_error; | |
936 | return (false); | |
937 | } | |
938 | ||
939 | #ifdef HAVE_PROCFS | |
940 | ||
941 | /* If no prpsinfo, just return true. Otherwise, grab the last component | |
942 | of the exec'd pathname from the prpsinfo. */ | |
943 | ||
944 | if (core_prpsinfo (core_bfd)) | |
945 | { | |
946 | corename = (((struct prpsinfo *) core_prpsinfo (core_bfd)) -> pr_fname); | |
947 | } | |
948 | else | |
949 | { | |
950 | return (true); | |
951 | } | |
952 | ||
953 | /* Find the last component of the executable pathname. */ | |
954 | ||
955 | if ((execname = strrchr (exec_bfd -> filename, '/')) != NULL) | |
956 | { | |
957 | execname++; | |
958 | } | |
959 | else | |
960 | { | |
961 | execname = (char *) exec_bfd -> filename; | |
962 | } | |
963 | ||
964 | /* See if they match */ | |
965 | ||
966 | return (strcmp (execname, corename) ? false : true); | |
967 | ||
968 | #else | |
969 | ||
970 | return (true); | |
971 | ||
972 | #endif /* HAVE_PROCFS */ | |
973 | } | |
974 | ||
975 | /* ELF core files contain a segment of type PT_NOTE, that holds much of | |
976 | the information that would normally be available from the /proc interface | |
977 | for the process, at the time the process dumped core. Currently this | |
978 | includes copies of the prstatus, prpsinfo, and fpregset structures. | |
979 | ||
980 | Since these structures are potentially machine dependent in size and | |
981 | ordering, bfd provides two levels of support for them. The first level, | |
982 | available on all machines since it does not require that the host | |
983 | have /proc support or the relevant include files, is to create a bfd | |
984 | section for each of the prstatus, prpsinfo, and fpregset structures, | |
985 | without any interpretation of their contents. With just this support, | |
986 | the bfd client will have to interpret the structures itself. Even with | |
987 | /proc support, it might want these full structures for it's own reasons. | |
988 | ||
989 | In the second level of support, where HAVE_PROCFS is defined, bfd will | |
990 | pick apart the structures to gather some additional information that | |
991 | clients may want, such as the general register set, the name of the | |
992 | exec'ed file and its arguments, the signal (if any) that caused the | |
993 | core dump, etc. | |
994 | ||
995 | */ | |
996 | ||
997 | static boolean | |
998 | DEFUN(elf_corefile_note, (abfd, hdr), | |
999 | bfd *abfd AND | |
1000 | Elf_Internal_Phdr *hdr) | |
1001 | { | |
1002 | Elf_External_Note *x_note_p; /* Elf note, external form */ | |
1003 | Elf_Internal_Note i_note; /* Elf note, internal form */ | |
1004 | char *buf = NULL; /* Entire note segment contents */ | |
1005 | char *namedata; /* Name portion of the note */ | |
1006 | char *descdata; /* Descriptor portion of the note */ | |
1007 | char *sectname; /* Name to use for new section */ | |
1008 | long filepos; /* File offset to descriptor data */ | |
1009 | asection *newsect; | |
1010 | ||
1011 | if (hdr -> p_filesz > 0 | |
1012 | && (buf = (char *) bfd_xmalloc (hdr -> p_filesz)) != NULL | |
1013 | && bfd_seek (abfd, hdr -> p_offset, SEEK_SET) != -1 | |
1014 | && bfd_read ((PTR) buf, hdr -> p_filesz, 1, abfd) == hdr -> p_filesz) | |
1015 | { | |
1016 | x_note_p = (Elf_External_Note *) buf; | |
1017 | while ((char *) x_note_p < (buf + hdr -> p_filesz)) | |
1018 | { | |
1019 | i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> namesz); | |
1020 | i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> descsz); | |
1021 | i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p -> type); | |
1022 | namedata = x_note_p -> name; | |
1023 | descdata = namedata + BFD_ALIGN (i_note.namesz, 4); | |
1024 | filepos = hdr -> p_offset + (descdata - buf); | |
1025 | switch (i_note.type) { | |
1026 | case NT_PRSTATUS: | |
1027 | /* process descdata as prstatus info */ | |
1028 | bfd_prstatus (abfd, descdata, i_note.descsz, filepos); | |
1029 | sectname = ".prstatus"; | |
1030 | break; | |
1031 | case NT_FPREGSET: | |
1032 | /* process descdata as fpregset info */ | |
1033 | bfd_fpregset (abfd, descdata, i_note.descsz, filepos); | |
1034 | sectname = ".fpregset"; | |
1035 | break; | |
1036 | case NT_PRPSINFO: | |
1037 | /* process descdata as prpsinfo */ | |
1038 | bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos); | |
1039 | sectname = ".prpsinfo"; | |
1040 | break; | |
1041 | default: | |
1042 | /* Unknown descriptor, just ignore it. */ | |
1043 | sectname = NULL; | |
1044 | break; | |
1045 | } | |
1046 | if (sectname != NULL) | |
1047 | { | |
1048 | newsect = bfd_make_section (abfd, sectname); | |
1049 | newsect -> _raw_size = i_note.descsz; | |
1050 | newsect -> filepos = filepos; | |
1051 | newsect -> flags = SEC_ALLOC | SEC_HAS_CONTENTS; | |
1052 | newsect -> alignment_power = 2; | |
1053 | } | |
1054 | x_note_p = (Elf_External_Note *) | |
1055 | (descdata + BFD_ALIGN (i_note.descsz, 4)); | |
1056 | } | |
1057 | } | |
1058 | if (buf != NULL) | |
1059 | { | |
1060 | free (buf); | |
1061 | } | |
1062 | return true; | |
1063 | ||
1064 | } | |
1065 | ||
1066 | ||
1067 | /* Read a specified number of bytes at a specified offset in an ELF | |
1068 | file, into a newly allocated buffer, and return a pointer to the | |
1069 | buffer. */ | |
1070 | ||
1071 | static char * | |
1072 | DEFUN(elf_read, (abfd, offset, size), | |
1073 | bfd *abfd AND | |
1074 | long offset AND | |
1075 | int size) | |
1076 | { | |
1077 | char *buf; | |
1078 | ||
1079 | if ((buf = bfd_alloc (abfd, size)) == NULL) | |
1080 | { | |
1081 | bfd_error = no_memory; | |
1082 | return (NULL); | |
1083 | } | |
1084 | if (bfd_seek (abfd, offset, SEEK_SET) == -1) | |
1085 | { | |
1086 | bfd_error = system_call_error; | |
1087 | return (NULL); | |
1088 | } | |
1089 | if (bfd_read ((PTR) buf, size, 1, abfd) != size) | |
1090 | { | |
1091 | bfd_error = system_call_error; | |
1092 | return (NULL); | |
1093 | } | |
1094 | return (buf); | |
1095 | } | |
1096 | ||
1097 | /* Begin processing a given object. | |
1098 | ||
1099 | First we validate the file by reading in the ELF header and checking | |
1100 | the magic number. | |
1101 | ||
1102 | */ | |
1103 | ||
fb6e80d5 KR |
1104 | static boolean |
1105 | DEFUN (elf_file_p, (x_ehdrp), Elf_External_Ehdr *x_ehdrp) | |
1106 | { | |
1107 | return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0) | |
1108 | && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1) | |
1109 | && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2) | |
1110 | && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3)); | |
1111 | } | |
1112 | ||
81187b54 KR |
1113 | bfd_target * |
1114 | DEFUN (elf_object_p, (abfd), bfd *abfd) | |
1115 | { | |
1116 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ | |
1117 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
1118 | Elf_External_Shdr x_shdr; /* Section header table entry, external form */ | |
1119 | Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */ | |
1120 | int shindex; | |
1121 | char *shstrtab; /* Internal copy of section header stringtab */ | |
286a4427 | 1122 | struct elf_backend_data *ebd; /* Use to get ELF_ARCH stored in xvec */ |
81187b54 KR |
1123 | |
1124 | /* Read in the ELF header in external format. */ | |
1125 | ||
1126 | if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
1127 | { | |
1128 | bfd_error = system_call_error; | |
1129 | return (NULL); | |
1130 | } | |
1131 | ||
1132 | /* Now check to see if we have a valid ELF file, and one that BFD can | |
1133 | make use of. The magic number must match, the address size ('class') | |
1134 | and byte-swapping must match our XVEC entry, and it must have a | |
1135 | section header table (FIXME: See comments re sections at top of this | |
1136 | file). */ | |
1137 | ||
fb6e80d5 | 1138 | if (elf_file_p (&x_ehdr) == false) |
81187b54 | 1139 | { |
fb6e80d5 | 1140 | wrong: |
81187b54 KR |
1141 | bfd_error = wrong_format; |
1142 | return (NULL); | |
1143 | } | |
1144 | ||
1145 | /* FIXME, Check EI_VERSION here ! */ | |
1146 | ||
1147 | switch (x_ehdr.e_ident[EI_CLASS]) | |
1148 | { | |
1149 | case ELFCLASSNONE: /* address size not specified */ | |
1150 | goto wrong; /* No support if can't tell address size */ | |
1151 | case ELFCLASS32: /* 32-bit addresses */ | |
1152 | break; | |
1153 | case ELFCLASS64: /* 64-bit addresses */ | |
1154 | goto wrong; /* FIXME: 64 bits not yet supported */ | |
1155 | default: | |
1156 | goto wrong; /* No support if unknown address class */ | |
1157 | } | |
1158 | ||
1159 | /* Switch xvec to match the specified byte order. */ | |
1160 | switch (x_ehdr.e_ident[EI_DATA]) | |
1161 | { | |
1162 | case ELFDATA2MSB: /* Big-endian */ | |
1163 | if (!abfd->xvec->header_byteorder_big_p) | |
1164 | goto wrong; | |
1165 | break; | |
1166 | case ELFDATA2LSB: /* Little-endian */ | |
1167 | if (abfd->xvec->header_byteorder_big_p) | |
1168 | goto wrong; | |
1169 | break; | |
1170 | case ELFDATANONE: /* No data encoding specified */ | |
1171 | default: /* Unknown data encoding specified */ | |
1172 | goto wrong; | |
1173 | } | |
1174 | ||
1175 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to | |
1176 | the tdata pointer in the bfd. */ | |
1177 | ||
1178 | if (NULL == (elf_tdata (abfd) = (struct elf_obj_tdata *) | |
1179 | bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)))) | |
1180 | { | |
1181 | bfd_error = no_memory; | |
1182 | return (NULL); | |
1183 | } | |
1184 | ||
1185 | /* FIXME: Any `wrong' exits below here will leak memory (tdata). */ | |
1186 | ||
1187 | /* Now that we know the byte order, swap in the rest of the header */ | |
1188 | i_ehdrp = elf_elfheader (abfd); | |
1189 | elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); | |
1190 | ||
1191 | /* If there is no section header table, we're hosed. */ | |
1192 | if (i_ehdrp->e_shoff == 0) | |
1193 | goto wrong; | |
1194 | ||
1195 | if (i_ehdrp->e_type == ET_EXEC || i_ehdrp->e_type == ET_DYN) | |
1196 | abfd -> flags |= EXEC_P; | |
1197 | ||
286a4427 FF |
1198 | /* Retrieve the architecture information from the xvec and verify |
1199 | that it matches the machine info stored in the ELF header. | |
1200 | This allows us to resolve ambiguous formats that might not | |
1201 | otherwise be distinguishable. */ | |
1202 | ||
1203 | ebd = (struct elf_backend_data *) (abfd->xvec->backend_data); | |
81187b54 KR |
1204 | switch (i_ehdrp->e_machine) |
1205 | { | |
1206 | case EM_NONE: | |
1207 | case EM_M32: /* or should this be bfd_arch_obscure? */ | |
286a4427 FF |
1208 | if (ebd -> arch != bfd_arch_unknown) |
1209 | goto wrong; | |
81187b54 KR |
1210 | bfd_default_set_arch_mach(abfd, bfd_arch_unknown, 0); |
1211 | break; | |
1212 | case EM_SPARC: | |
286a4427 FF |
1213 | if (ebd -> arch != bfd_arch_sparc) |
1214 | goto wrong; | |
81187b54 KR |
1215 | bfd_default_set_arch_mach(abfd, bfd_arch_sparc, 0); |
1216 | break; | |
1217 | case EM_386: | |
286a4427 FF |
1218 | if (ebd -> arch != bfd_arch_i386) |
1219 | goto wrong; | |
81187b54 KR |
1220 | bfd_default_set_arch_mach(abfd, bfd_arch_i386, 0); |
1221 | break; | |
1222 | case EM_68K: | |
286a4427 FF |
1223 | if (ebd -> arch != bfd_arch_m68k) |
1224 | goto wrong; | |
81187b54 KR |
1225 | bfd_default_set_arch_mach(abfd, bfd_arch_m68k, 0); |
1226 | break; | |
1227 | case EM_88K: | |
286a4427 FF |
1228 | if (ebd -> arch != bfd_arch_m88k) |
1229 | goto wrong; | |
81187b54 KR |
1230 | bfd_default_set_arch_mach(abfd, bfd_arch_m88k, 0); |
1231 | break; | |
1232 | case EM_860: | |
286a4427 FF |
1233 | if (ebd -> arch != bfd_arch_i860) |
1234 | goto wrong; | |
81187b54 KR |
1235 | bfd_default_set_arch_mach(abfd, bfd_arch_i860, 0); |
1236 | break; | |
1237 | case EM_MIPS: | |
286a4427 FF |
1238 | if (ebd -> arch != bfd_arch_mips) |
1239 | goto wrong; | |
81187b54 KR |
1240 | bfd_default_set_arch_mach(abfd, bfd_arch_mips, 0); |
1241 | break; | |
fb6e80d5 KR |
1242 | case EM_HPPA: |
1243 | if (ebd -> arch != bfd_arch_hppa) | |
1244 | goto wrong; | |
1245 | bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 0); | |
1246 | break; | |
81187b54 KR |
1247 | default: |
1248 | goto wrong; | |
1249 | } | |
1250 | ||
1251 | /* Allocate space for a copy of the section header table in | |
1252 | internal form, seek to the section header table in the file, | |
1253 | read it in, and convert it to internal form. As a simple sanity | |
1254 | check, verify that the what BFD thinks is the size of each section | |
1255 | header table entry actually matches the size recorded in the file. */ | |
1256 | ||
1257 | if (i_ehdrp->e_shentsize != sizeof (x_shdr)) | |
1258 | goto wrong; | |
1259 | i_shdrp = (Elf_Internal_Shdr *) | |
1260 | bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum); | |
1261 | if (! i_shdrp) | |
1262 | { | |
1263 | bfd_error = no_memory; | |
1264 | return (NULL); | |
1265 | } | |
1266 | if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1) | |
1267 | { | |
1268 | bfd_error = system_call_error; | |
1269 | return (NULL); | |
1270 | } | |
1271 | for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++) | |
1272 | { | |
1273 | if (bfd_read ((PTR) &x_shdr, sizeof x_shdr, 1, abfd) | |
1274 | != sizeof (x_shdr)) | |
1275 | { | |
1276 | bfd_error = system_call_error; | |
1277 | return (NULL); | |
1278 | } | |
1279 | elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex); | |
1280 | } | |
1281 | ||
1282 | elf_elfsections (abfd) = i_shdrp; | |
1283 | ||
1284 | /* Read in the string table containing the names of the sections. We | |
1285 | will need the base pointer to this table later. */ | |
1286 | /* We read this inline now, so that we don't have to go through | |
1287 | bfd_section_from_shdr with it (since this particular strtab is | |
1288 | used to find all of the ELF section names.) */ | |
1289 | ||
1290 | shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx); | |
1291 | if (! shstrtab) | |
1292 | return (NULL); | |
1293 | ||
1294 | /* Once all of the section headers have been read and converted, we | |
1295 | can start processing them. Note that the first section header is | |
1296 | a dummy placeholder entry, so we ignore it. | |
1297 | ||
1298 | We also watch for the symbol table section and remember the file | |
1299 | offset and section size for both the symbol table section and the | |
1300 | associated string table section. */ | |
1301 | ||
1302 | for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++) | |
1303 | { | |
1304 | bfd_section_from_shdr (abfd, shindex); | |
1305 | } | |
1306 | ||
1307 | /* Remember the entry point specified in the ELF file header. */ | |
1308 | ||
1309 | bfd_get_start_address (abfd) = i_ehdrp->e_entry; | |
1310 | ||
1311 | return (abfd->xvec); | |
1312 | } | |
1313 | ||
fb6e80d5 KR |
1314 | /* |
1315 | Takes a bfd and a symbol, returns a pointer to the elf specific area | |
1316 | of the symbol if there is one. | |
1317 | */ | |
1318 | static elf_symbol_type * | |
1319 | DEFUN(elf_symbol_from,(ignore_abfd, symbol), | |
1320 | bfd *ignore_abfd AND | |
1321 | asymbol *symbol) | |
1322 | { | |
1323 | if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour) | |
1324 | return (elf_symbol_type *)NULL; | |
1325 | ||
1326 | if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *)NULL) | |
1327 | return (elf_symbol_type *)NULL; | |
1328 | ||
1329 | return (elf_symbol_type *) symbol; | |
1330 | } | |
1331 | ||
81187b54 KR |
1332 | /* Core files are simply standard ELF formatted files that partition |
1333 | the file using the execution view of the file (program header table) | |
1334 | rather than the linking view. In fact, there is no section header | |
1335 | table in a core file. | |
1336 | ||
1337 | The process status information (including the contents of the general | |
1338 | register set) and the floating point register set are stored in a | |
1339 | segment of type PT_NOTE. We handcraft a couple of extra bfd sections | |
1340 | that allow standard bfd access to the general registers (.reg) and the | |
1341 | floating point registers (.reg2). | |
1342 | ||
1343 | */ | |
1344 | ||
1345 | bfd_target * | |
1346 | DEFUN (elf_core_file_p, (abfd), bfd *abfd) | |
1347 | { | |
1348 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ | |
1349 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
1350 | Elf_External_Phdr x_phdr; /* Program header table entry, external form */ | |
1351 | Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */ | |
1352 | unsigned int phindex; | |
1353 | ||
1354 | /* Read in the ELF header in external format. */ | |
1355 | ||
1356 | if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) | |
1357 | { | |
1358 | bfd_error = system_call_error; | |
1359 | return (NULL); | |
1360 | } | |
1361 | ||
1362 | /* Now check to see if we have a valid ELF file, and one that BFD can | |
1363 | make use of. The magic number must match, the address size ('class') | |
1364 | and byte-swapping must match our XVEC entry, and it must have a | |
1365 | program header table (FIXME: See comments re segments at top of this | |
1366 | file). */ | |
1367 | ||
fb6e80d5 | 1368 | if (elf_file_p (&x_ehdr) == false) |
81187b54 | 1369 | { |
fb6e80d5 | 1370 | wrong: |
81187b54 KR |
1371 | bfd_error = wrong_format; |
1372 | return (NULL); | |
1373 | } | |
1374 | ||
1375 | /* FIXME, Check EI_VERSION here ! */ | |
1376 | ||
1377 | switch (x_ehdr.e_ident[EI_CLASS]) | |
1378 | { | |
1379 | case ELFCLASSNONE: /* address size not specified */ | |
1380 | goto wrong; /* No support if can't tell address size */ | |
1381 | case ELFCLASS32: /* 32-bit addresses */ | |
1382 | break; | |
1383 | case ELFCLASS64: /* 64-bit addresses */ | |
1384 | goto wrong; /* FIXME: 64 bits not yet supported */ | |
1385 | default: | |
1386 | goto wrong; /* No support if unknown address class */ | |
1387 | } | |
1388 | ||
1389 | /* Switch xvec to match the specified byte order. */ | |
1390 | switch (x_ehdr.e_ident[EI_DATA]) | |
1391 | { | |
1392 | case ELFDATA2MSB: /* Big-endian */ | |
1393 | if (abfd->xvec->byteorder_big_p == false) | |
1394 | goto wrong; | |
1395 | break; | |
1396 | case ELFDATA2LSB: /* Little-endian */ | |
1397 | if (abfd->xvec->byteorder_big_p == true) | |
1398 | goto wrong; | |
1399 | break; | |
1400 | case ELFDATANONE: /* No data encoding specified */ | |
1401 | default: /* Unknown data encoding specified */ | |
1402 | goto wrong; | |
1403 | } | |
1404 | ||
1405 | /* Allocate an instance of the elf_obj_tdata structure and hook it up to | |
1406 | the tdata pointer in the bfd. */ | |
1407 | ||
1408 | elf_tdata (abfd) = | |
1409 | (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); | |
1410 | if (elf_tdata (abfd) == NULL) | |
1411 | { | |
1412 | bfd_error = no_memory; | |
1413 | return (NULL); | |
1414 | } | |
1415 | ||
1416 | /* FIXME, `wrong' returns from this point onward, leak memory. */ | |
1417 | ||
1418 | /* Now that we know the byte order, swap in the rest of the header */ | |
1419 | i_ehdrp = elf_elfheader (abfd); | |
1420 | elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); | |
1421 | ||
1422 | /* If there is no program header, or the type is not a core file, then | |
1423 | we are hosed. */ | |
1424 | if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE) | |
1425 | goto wrong; | |
1426 | ||
1427 | /* Allocate space for a copy of the program header table in | |
1428 | internal form, seek to the program header table in the file, | |
1429 | read it in, and convert it to internal form. As a simple sanity | |
1430 | check, verify that the what BFD thinks is the size of each program | |
1431 | header table entry actually matches the size recorded in the file. */ | |
1432 | ||
1433 | if (i_ehdrp->e_phentsize != sizeof (x_phdr)) | |
1434 | goto wrong; | |
1435 | i_phdrp = (Elf_Internal_Phdr *) | |
1436 | bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum); | |
1437 | if (! i_phdrp) | |
1438 | { | |
1439 | bfd_error = no_memory; | |
1440 | return (NULL); | |
1441 | } | |
1442 | if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1) | |
1443 | { | |
1444 | bfd_error = system_call_error; | |
1445 | return (NULL); | |
1446 | } | |
1447 | for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++) | |
1448 | { | |
1449 | if (bfd_read ((PTR) &x_phdr, sizeof (x_phdr), 1, abfd) | |
1450 | != sizeof (x_phdr)) | |
1451 | { | |
1452 | bfd_error = system_call_error; | |
1453 | return (NULL); | |
1454 | } | |
1455 | elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex); | |
1456 | } | |
1457 | ||
1458 | /* Once all of the program headers have been read and converted, we | |
1459 | can start processing them. */ | |
1460 | ||
1461 | for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++) | |
1462 | { | |
1463 | bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex); | |
1464 | if ((i_phdrp + phindex) -> p_type == PT_NOTE) | |
1465 | { | |
1466 | elf_corefile_note (abfd, i_phdrp + phindex); | |
1467 | } | |
1468 | } | |
1469 | ||
1470 | /* Remember the entry point specified in the ELF file header. */ | |
1471 | ||
1472 | bfd_get_start_address (abfd) = i_ehdrp->e_entry; | |
1473 | ||
1474 | return (abfd->xvec); | |
1475 | } | |
1476 | ||
1477 | boolean | |
1478 | DEFUN (elf_mkobject, (abfd), bfd *abfd) | |
1479 | { | |
1480 | /* this just does initialization */ | |
1481 | /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */ | |
1482 | elf_tdata(abfd) = (struct elf_obj_tdata *) | |
1483 | bfd_zalloc (abfd, sizeof(struct elf_obj_tdata)); | |
1484 | if (elf_tdata(abfd) == 0) { | |
1485 | bfd_error = no_memory; | |
1486 | return false; | |
1487 | } | |
1488 | /* since everything is done at close time, do we need any | |
1489 | initialization? */ | |
1490 | ||
1491 | return (true); | |
1492 | } | |
1493 | ||
1494 | /* | |
1495 | Create ELF output from BFD sections. | |
1496 | ||
1497 | Essentially, just create the section header and forget about the program | |
1498 | header for now. | |
1499 | ||
1500 | */ | |
1501 | ||
1502 | /* lacking nested functions and nested types, set up for mapping over | |
1503 | BFD sections to produce ELF sections */ | |
1504 | ||
1505 | typedef struct { | |
1506 | Elf_Internal_Ehdr *i_ehdr; | |
1507 | Elf_Internal_Shdr *i_shdrp; | |
1508 | struct strtab *shstrtab; | |
1509 | int symtab_section; | |
1510 | } elf_sect_thunk; | |
1511 | ||
fb6e80d5 KR |
1512 | static int |
1513 | elf_idx_of_sym(abfd, sym) | |
1514 | bfd *abfd; | |
1515 | asymbol *sym; | |
1516 | { | |
1517 | int i; | |
1518 | for ( i = 0; i < abfd->symcount; i++ ) | |
1519 | { | |
1520 | if ( sym == (asymbol *)abfd->outsymbols[i] ) | |
1521 | { | |
1522 | /* sanity check */ | |
1523 | BFD_ASSERT( (strcmp(sym->name, abfd->outsymbols[i]->name) == 0) | |
1524 | || (strlen(sym->name) == 0) ); | |
1525 | return i+1; | |
1526 | } | |
1527 | } | |
1528 | return 0; | |
1529 | } | |
81187b54 KR |
1530 | |
1531 | static void | |
1532 | DEFUN (elf_make_sections, (abfd, asect, obj), | |
1533 | bfd *abfd AND | |
1534 | asection *asect AND | |
1535 | PTR obj) | |
1536 | { | |
1537 | elf_sect_thunk *thunk = (elf_sect_thunk*)obj; | |
1538 | /* most of what is in bfd_shdr_from_section goes in here... */ | |
1539 | /* and all of these sections generate at *least* one ELF section. */ | |
1540 | int this_section; | |
1541 | int idx; | |
1542 | ||
1543 | /* check if we're making a PROGBITS section... */ | |
1544 | /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */ | |
1545 | /* this was too strict... what *do* we want to check here? */ | |
fb6e80d5 | 1546 | if (1) |
81187b54 KR |
1547 | { |
1548 | Elf_Internal_Shdr *this_hdr; | |
1549 | this_section = elf_section_from_bfd_section (abfd, asect); | |
1550 | this_hdr = &thunk->i_shdrp[this_section]; | |
1551 | ||
1552 | this_hdr->sh_addr = asect->vma; | |
1553 | this_hdr->sh_size = asect->_raw_size; | |
1554 | /* contents already set by elf_set_section_contents */ | |
1555 | ||
1556 | if (asect->flags & SEC_RELOC) | |
1557 | { | |
1558 | /* emit a reloc section, and thus strtab and symtab... */ | |
1559 | Elf_Internal_Shdr *rela_hdr; | |
1560 | Elf_Internal_Shdr *symtab_hdr; | |
1561 | Elf_External_Rela *outbound_relocs; | |
1562 | int rela_section; | |
1563 | ||
1564 | symtab_hdr = &thunk->i_shdrp[thunk->symtab_section]; | |
1565 | ||
1566 | if (thunk->symtab_section == this_section + 1) | |
1567 | rela_section = thunk->symtab_section + 2; /* symtab + symstrtab */ | |
1568 | else | |
1569 | rela_section = this_section + 1; | |
1570 | rela_hdr = &thunk->i_shdrp[rela_section]; | |
1571 | rela_hdr->sh_type = SHT_RELA; | |
1572 | rela_hdr->sh_link = thunk->symtab_section; | |
1573 | rela_hdr->sh_info = this_section; | |
1574 | rela_hdr->sh_entsize = sizeof (Elf_External_Rela); | |
1575 | /* orelocation has the data, reloc_count has the count... */ | |
1576 | rela_hdr->sh_size = rela_hdr->sh_entsize * asect->reloc_count; | |
1577 | outbound_relocs = (Elf_External_Rela *) | |
1578 | bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela)); | |
1579 | for (idx = 0; idx < asect->reloc_count; idx++) | |
1580 | { | |
1581 | Elf_Internal_Rela dst; | |
1582 | arelent *ptr; | |
1583 | Elf_External_Rela *src; | |
fb6e80d5 | 1584 | |
81187b54 KR |
1585 | ptr = asect->orelocation[idx]; |
1586 | src = outbound_relocs + idx; | |
1587 | if (asect->flags & SEC_RELOC) | |
1588 | dst.r_offset = ptr->address - asect->vma; | |
1589 | else | |
1590 | dst.r_offset = ptr->address; | |
1591 | ||
fb6e80d5 KR |
1592 | /* @@ This assumes the symbols were written (or will be |
1593 | written) in the same order that they appear in | |
1594 | abfd->outsymbols. */ | |
1595 | if (ptr->sym_ptr_ptr && ptr->sym_ptr_ptr[0]) | |
1596 | dst.r_info = ELF_R_INFO (elf_idx_of_sym (abfd, | |
1597 | ptr->sym_ptr_ptr[0]), | |
1598 | ptr->howto->type); | |
1599 | else | |
1600 | dst.r_info = ELF_R_INFO (STN_UNDEF, ptr->howto->type); | |
81187b54 KR |
1601 | |
1602 | dst.r_addend = ptr->addend; | |
1603 | elf_swap_reloca_out(abfd, &dst, src); | |
1604 | } | |
1605 | rela_hdr->contents = (void*)outbound_relocs; | |
1606 | } | |
fb6e80d5 KR |
1607 | if (asect->flags & SEC_ALLOC) |
1608 | { | |
1609 | this_hdr->sh_flags |= SHF_ALLOC; | |
1610 | if (asect->flags & SEC_LOAD) | |
1611 | { | |
1612 | /* @@ Do something with sh_type? */ | |
1613 | } | |
1614 | } | |
1615 | if (!(asect->flags & SEC_READONLY)) | |
1616 | this_hdr->sh_flags |= SHF_WRITE; | |
1617 | ||
1618 | if (asect->flags & SEC_CODE) | |
1619 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
81187b54 KR |
1620 | } |
1621 | } | |
1622 | ||
1623 | static void | |
1624 | DEFUN (elf_fake_sections, (abfd, asect, obj), | |
1625 | bfd *abfd AND | |
1626 | asection *asect AND | |
1627 | PTR obj) | |
1628 | { | |
1629 | elf_sect_thunk *thunk = (elf_sect_thunk*)obj; | |
1630 | /* most of what is in bfd_shdr_from_section goes in here... */ | |
1631 | /* and all of these sections generate at *least* one ELF section. */ | |
1632 | int this_section; | |
fb6e80d5 | 1633 | |
81187b54 KR |
1634 | /* check if we're making a PROGBITS section... */ |
1635 | /* if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) */ | |
1636 | /* this was too strict... what *do* we want to check here? */ | |
fb6e80d5 | 1637 | if (1) |
81187b54 KR |
1638 | { |
1639 | Elf_Internal_Shdr *this_hdr; | |
1640 | this_section = thunk->i_ehdr->e_shnum++; | |
1641 | this_hdr = &thunk->i_shdrp[this_section]; | |
1642 | this_hdr->sh_name = | |
1643 | bfd_add_to_strtab (abfd, thunk->shstrtab, asect->name); | |
1644 | /* we need to log the type *now* so that elf_section_from_bfd_section | |
1645 | can find us... have to set rawdata too. */ | |
1646 | this_hdr->rawdata = (void*)asect; | |
1647 | if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD)) | |
1648 | this_hdr->sh_type = SHT_PROGBITS; | |
fb6e80d5 KR |
1649 | /* @@ Select conditions correctly! */ |
1650 | else if (!strcmp (asect->name, ".bss")) | |
1651 | this_hdr->sh_type = SHT_NOBITS; | |
81187b54 KR |
1652 | else |
1653 | /* what *do* we put here? */ | |
1654 | this_hdr->sh_type = SHT_PROGBITS; | |
1655 | ||
1656 | ||
fb6e80d5 KR |
1657 | { |
1658 | /* Emit a strtab and symtab, and possibly a reloc section. */ | |
1659 | Elf_Internal_Shdr *rela_hdr; | |
1660 | Elf_Internal_Shdr *symtab_hdr; | |
1661 | Elf_Internal_Shdr *symstrtab_hdr; | |
1662 | int rela_section; | |
1663 | int symstrtab_section; | |
1664 | ||
1665 | /* Note that only one symtab is used, so just remember it | |
1666 | for now. */ | |
1667 | if (! thunk->symtab_section) | |
1668 | { | |
1669 | thunk->symtab_section = thunk->i_ehdr->e_shnum++; | |
81187b54 | 1670 | symtab_hdr = &thunk->i_shdrp[thunk->symtab_section]; |
fb6e80d5 KR |
1671 | symtab_hdr->sh_name = |
1672 | bfd_add_to_strtab (abfd, thunk->shstrtab, ".symtab"); | |
1673 | symtab_hdr->sh_type = SHT_SYMTAB; | |
1674 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
1675 | ||
1676 | symstrtab_section = thunk->i_ehdr->e_shnum++; | |
1677 | BFD_ASSERT(symstrtab_section == thunk->symtab_section+1); | |
1678 | symstrtab_hdr = &thunk->i_shdrp[symstrtab_section]; | |
1679 | symtab_hdr->sh_link = symstrtab_section; | |
1680 | symstrtab_hdr->sh_name = | |
1681 | bfd_add_to_strtab (abfd, thunk->shstrtab, ".strtab"); | |
1682 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
1683 | ||
1684 | symtab_hdr->contents = 0; | |
1685 | symstrtab_hdr->contents = 0; | |
1686 | symstrtab_hdr->sh_size = 0; | |
1687 | } | |
1688 | else | |
1689 | symtab_hdr = &thunk->i_shdrp[thunk->symtab_section]; | |
81187b54 | 1690 | |
fb6e80d5 KR |
1691 | if (asect->flags & SEC_RELOC) |
1692 | { | |
1693 | rela_section = thunk->i_ehdr->e_shnum++; | |
1694 | rela_hdr = &thunk->i_shdrp[rela_section]; | |
1695 | rela_hdr->sh_name = | |
1696 | bfd_add_2_to_strtab (abfd, thunk->shstrtab, ".rela", | |
1697 | asect->name); | |
1698 | rela_hdr->sh_type = SHT_RELA; | |
1699 | rela_hdr->sh_link = thunk->symtab_section; | |
1700 | rela_hdr->sh_info = this_section; | |
1701 | rela_hdr->sh_entsize = sizeof (Elf_External_Rela); | |
1702 | } | |
1703 | } | |
1704 | if (asect->flags & SEC_ALLOC) | |
1705 | { | |
1706 | this_hdr->sh_flags |= SHF_ALLOC; | |
1707 | if (asect->flags & SEC_LOAD) | |
1708 | { | |
1709 | /* @@ Do something with sh_type? */ | |
1710 | } | |
81187b54 | 1711 | } |
fb6e80d5 KR |
1712 | if (!(asect->flags & SEC_READONLY)) |
1713 | this_hdr->sh_flags |= SHF_WRITE; | |
1714 | if (asect->flags & SEC_CODE) | |
1715 | this_hdr->sh_flags |= SHF_EXECINSTR; | |
81187b54 KR |
1716 | } |
1717 | } | |
1718 | ||
1719 | ||
1720 | static boolean | |
1721 | DEFUN (elf_compute_section_file_positions, (abfd), bfd *abfd) | |
1722 | { | |
1723 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
1724 | Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */ | |
1725 | struct strtab *shstrtab; | |
1726 | int count, maxsections; | |
1727 | elf_sect_thunk est; | |
1728 | ||
1729 | if (! elf_shstrtab (abfd)) { | |
1730 | i_ehdrp = elf_elfheader (abfd); /* build new header in tdata memory */ | |
1731 | shstrtab = bfd_new_strtab(abfd); | |
1732 | ||
1733 | i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; | |
1734 | i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; | |
1735 | i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; | |
1736 | i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; | |
1737 | ||
1738 | i_ehdrp->e_ident[EI_CLASS] = ELFCLASS32; /* FIXME: find out from bfd */ | |
1739 | i_ehdrp->e_ident[EI_DATA] = | |
1740 | abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB; | |
1741 | i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT; | |
1742 | ||
1743 | for(count = EI_PAD; count < EI_NIDENT; count ++) | |
1744 | i_ehdrp->e_ident[count] = 0; | |
1745 | ||
1746 | i_ehdrp->e_type = (abfd->flags & EXEC_P)? ET_EXEC : ET_REL; | |
1747 | switch(bfd_get_arch(abfd)) | |
1748 | { | |
1749 | case bfd_arch_unknown: | |
1750 | i_ehdrp->e_machine = EM_NONE; | |
1751 | break; | |
1752 | case bfd_arch_sparc: | |
1753 | i_ehdrp->e_machine = EM_SPARC; | |
1754 | break; | |
1755 | case bfd_arch_i386: | |
1756 | i_ehdrp->e_machine = EM_386; | |
1757 | break; | |
1758 | case bfd_arch_m68k: | |
1759 | i_ehdrp->e_machine = EM_68K; | |
1760 | break; | |
1761 | case bfd_arch_m88k: | |
1762 | i_ehdrp->e_machine = EM_88K; | |
1763 | break; | |
1764 | case bfd_arch_i860: | |
1765 | i_ehdrp->e_machine = EM_860; | |
1766 | break; | |
1767 | case bfd_arch_mips: /* MIPS Rxxxx */ | |
1768 | i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ | |
1769 | break; | |
fb6e80d5 KR |
1770 | case bfd_arch_hppa: |
1771 | i_ehdrp->e_machine = EM_HPPA; | |
1772 | break; | |
81187b54 KR |
1773 | /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ |
1774 | default: | |
1775 | i_ehdrp->e_machine = EM_NONE; | |
1776 | } | |
1777 | i_ehdrp->e_version = EV_CURRENT; | |
1778 | i_ehdrp->e_ehsize = sizeof(Elf_External_Ehdr); | |
1779 | ||
1780 | /* no program header, for now. */ | |
1781 | i_ehdrp->e_phoff = 0; | |
1782 | i_ehdrp->e_phentsize = 0; | |
1783 | i_ehdrp->e_phnum = 0; | |
1784 | ||
1785 | /* each bfd section is section header entry */ | |
1786 | i_ehdrp->e_entry = bfd_get_start_address (abfd); | |
1787 | i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr); | |
1788 | ||
1789 | /* figure at most each section can have a rel, strtab, symtab */ | |
1790 | maxsections = 4*bfd_count_sections(abfd)+2; | |
1791 | ||
1792 | i_ehdrp->e_shoff = i_ehdrp->e_ehsize; | |
1793 | ||
1794 | /* and we'll just have to fix up the offsets later. */ | |
1795 | /* outbase += i_ehdr.e_shentsize * i_ehdr.e_shnum; */ | |
1796 | ||
1797 | i_shdrp = (Elf_Internal_Shdr *) | |
1798 | bfd_alloc (abfd, sizeof (*i_shdrp) * maxsections); | |
1799 | if (! i_shdrp) | |
1800 | { | |
1801 | bfd_error = no_memory; | |
1802 | return (false); | |
1803 | } | |
1804 | for (count=0; count < maxsections; count++) | |
1805 | { | |
1806 | i_shdrp[count].rawdata = 0; | |
1807 | i_shdrp[count].contents = 0; | |
1808 | } | |
1809 | ||
1810 | ||
1811 | i_shdrp[0].sh_name = 0; | |
1812 | i_shdrp[0].sh_type = SHT_NULL; | |
1813 | i_shdrp[0].sh_flags = 0; | |
1814 | i_shdrp[0].sh_addr = 0; | |
1815 | i_shdrp[0].sh_offset = 0; | |
1816 | i_shdrp[0].sh_size = 0; | |
1817 | i_shdrp[0].sh_link = SHN_UNDEF; | |
1818 | i_shdrp[0].sh_info = 0; | |
1819 | i_shdrp[0].sh_addralign = 0; | |
1820 | i_shdrp[0].sh_entsize = 0; | |
1821 | ||
1822 | i_ehdrp->e_shnum = 1; | |
1823 | ||
1824 | elf_elfsections (abfd) = i_shdrp; | |
1825 | elf_shstrtab (abfd) = shstrtab; | |
1826 | } | |
1827 | est.i_ehdr = elf_elfheader(abfd); | |
1828 | est.i_shdrp = elf_elfsections(abfd); | |
1829 | est.shstrtab = elf_shstrtab(abfd); | |
1830 | est.symtab_section = 0; /* elf_fake_sections fils it in */ | |
1831 | ||
1832 | bfd_map_over_sections(abfd, elf_fake_sections, &est); | |
1833 | elf_onesymtab (abfd) = est.symtab_section; | |
1834 | return (true); | |
1835 | } | |
1836 | ||
fb6e80d5 KR |
1837 | static boolean |
1838 | DEFUN (elf_write_phdrs, (abfd, i_ehdrp, i_phdrp, phdr_cnt), | |
1839 | bfd *abfd AND | |
1840 | Elf_Internal_Ehdr *i_ehdrp AND | |
1841 | Elf_Internal_Phdr *i_phdrp AND | |
1842 | Elf_Half phdr_cnt) | |
1843 | { | |
1844 | /* first program header entry goes after the file header */ | |
1845 | int outbase = i_ehdrp->e_ehsize; | |
1846 | int i; | |
1847 | Elf_External_Phdr x_phdr; | |
1848 | ||
1849 | for ( i = 0; i < phdr_cnt; i++ ) { | |
1850 | elf_swap_phdr_out(abfd, i_phdrp + i, &x_phdr); | |
1851 | bfd_seek(abfd, outbase, SEEK_SET); | |
1852 | bfd_write( (PTR)&x_phdr, sizeof(x_phdr), 1, abfd); | |
1853 | outbase += sizeof(x_phdr); | |
1854 | } | |
1855 | ||
1856 | return true; | |
1857 | } | |
1858 | ||
1859 | static Elf_Internal_Phdr * | |
1860 | DEFUN (elf_build_phdrs, (abfd, i_ehdrp, i_shdrp, phdr_cnt), | |
1861 | bfd *abfd AND | |
1862 | Elf_Internal_Ehdr *i_ehdrp AND | |
1863 | Elf_Internal_Shdr *i_shdrp AND | |
1864 | Elf_Half *phdr_cnt) | |
1865 | { | |
1866 | Elf_Internal_Phdr *phdr_buf; | |
1867 | int idx; | |
1868 | /* | |
1869 | NOTES: | |
1870 | 1. The program header table is *not* loaded as part | |
1871 | of the memory image of the program. If this | |
1872 | changes later, the PT_PHDR entry must come first. | |
1873 | 2. there is currently no support for program header | |
1874 | entries of type PT_PHDR, PT_DYNAMIC, PT_INTERP, | |
1875 | or PT_SHLIB. | |
1876 | */ | |
1877 | ||
1878 | /* A. Figure out how many program header table entries are needed */ | |
1879 | /* 1. PT_LOAD for the text segment */ | |
1880 | /* 2. PT_LOAD for the data segment */ | |
1881 | /* Then, reserve space for one more pointer. This will be NULL */ | |
1882 | /* to indicate the end of the program header table. */ | |
1883 | ||
1884 | #ifdef PHDRS_INCLUDED | |
1885 | *phdr_cnt = 4; | |
1886 | #else | |
1887 | *phdr_cnt = 3; /* XXX right now, execve() expects exactly 3 PT entries */ | |
1888 | #endif | |
1889 | ||
1890 | phdr_buf = (Elf_Internal_Phdr *)bfd_xmalloc( ((*phdr_cnt) + 1) | |
1891 | * | |
1892 | sizeof(Elf_Internal_Phdr)); | |
1893 | ||
1894 | idx = 0; | |
1895 | #ifdef PHDRS_INCLUDED | |
1896 | /* B. Fill in the PT_PHDR entry. */ | |
1897 | ||
1898 | idx++; | |
1899 | #endif | |
1900 | ||
1901 | /* C. Fill in the PT_LOAD entry for the text segment. */ | |
1902 | ||
1903 | phdr_buf[idx].p_type = PT_LOAD; | |
1904 | ||
1905 | /* get virtual/physical address from .text section */ | |
1906 | phdr_buf[idx].p_vaddr = bfd_get_section_by_name(abfd,".text")->vma; | |
1907 | phdr_buf[idx].p_paddr = 0; /* XXX */ | |
1908 | ||
1909 | /* Ultimately, we would like the size of the .text load */ | |
1910 | /* segment to be the sum of the following sections: */ | |
1911 | /* the program header table itself */ | |
1912 | /* .interp */ | |
1913 | /* .hash */ | |
1914 | /* .dynsym */ | |
1915 | /* .dynstr */ | |
1916 | /* .rela.bss */ | |
1917 | /* .rela.plt */ | |
1918 | /* .init */ | |
1919 | /* .text */ | |
1920 | /* .fini */ | |
1921 | /* .rodata */ | |
1922 | /* But, right now, it will be the sum of the following */ | |
1923 | /* sections: */ | |
1924 | /* .text */ | |
1925 | /* .rodata */ | |
1926 | ||
1927 | { | |
1928 | static char *CONST ld_sect_names[] = | |
1929 | { ".text", ".rodata", NULL }; | |
1930 | int i; | |
1931 | int ld_size = 0; | |
1932 | ||
1933 | for ( i = 0; ld_sect_names[i]; i++ ) { | |
1934 | asection *asect = bfd_get_section_by_name(abfd, | |
1935 | ld_sect_names[i]); | |
1936 | ||
1937 | if ( asect ) | |
1938 | ld_size += bfd_section_size(abfd, asect); | |
1939 | } | |
1940 | phdr_buf[idx].p_filesz = ld_size; | |
1941 | /* XXX: need to fix this */ | |
1942 | phdr_buf[idx].p_memsz = ld_size; | |
1943 | } | |
1944 | phdr_buf[idx].p_flags = PF_R + PF_X; | |
1945 | phdr_buf[idx].p_align | |
1946 | = bfd_get_section_by_name(abfd,".text")->alignment_power; | |
1947 | ||
1948 | idx++; | |
1949 | ||
1950 | /* D. Fill in the PT_LOAD entry for the data segment. */ | |
1951 | ||
1952 | phdr_buf[idx].p_type = PT_LOAD; | |
1953 | ||
1954 | /* get virtual/physical address from .data section */ | |
1955 | phdr_buf[idx].p_vaddr = bfd_get_section_by_name(abfd,".data")->vma; | |
1956 | phdr_buf[idx].p_paddr = 0; /* XXX */ | |
1957 | ||
1958 | /* Ultimately, we would like the size of the data load segment */ | |
1959 | /* to be the sum of the following sections: */ | |
1960 | /* the PT_DYNAMIC program header table entry */ | |
1961 | /* .plt */ | |
1962 | /* .data */ | |
1963 | /* .data1 */ | |
1964 | /* .got */ | |
1965 | /* .dynamic */ | |
1966 | /* But, right now, it will be the sum of the following */ | |
1967 | /* sections: */ | |
1968 | /* .data */ | |
1969 | ||
1970 | { | |
1971 | static char *CONST ld_sect_names[] = | |
1972 | { ".data", NULL }; | |
1973 | int i; | |
1974 | int ld_size = 0; | |
1975 | ||
1976 | for ( i = 0; ld_sect_names[i]; i++ ) { | |
1977 | asection *asect = bfd_get_section_by_name(abfd, | |
1978 | ld_sect_names[i]); | |
1979 | ||
1980 | if ( asect ) | |
1981 | ld_size += bfd_section_size(abfd, asect); | |
1982 | } | |
1983 | phdr_buf[idx].p_filesz = ld_size; | |
1984 | /* XXX: need to fix this */ | |
1985 | phdr_buf[idx].p_memsz = ld_size; | |
1986 | } | |
1987 | phdr_buf[idx].p_flags = PF_R + PF_W + PF_X; | |
1988 | phdr_buf[idx].p_align | |
1989 | = bfd_get_section_by_name(abfd,".data")->alignment_power; | |
1990 | ||
1991 | idx++; | |
1992 | ||
1993 | /* E. Fill in the PT_LOAD entry for the bss segment. */ | |
1994 | ||
1995 | phdr_buf[idx].p_type = PT_LOAD; | |
1996 | ||
1997 | /* get virtual/physical address from .data section */ | |
1998 | phdr_buf[idx].p_vaddr = bfd_get_section_by_name(abfd,".bss")->vma; | |
1999 | phdr_buf[idx].p_paddr = 0; /* XXX */ | |
2000 | ||
2001 | { | |
2002 | static char *CONST ld_sect_names[] = | |
2003 | { ".bss", NULL }; | |
2004 | int i; | |
2005 | int ld_size = 0; | |
2006 | ||
2007 | for ( i = 0; ld_sect_names[i]; i++ ) { | |
2008 | asection *asect = bfd_get_section_by_name(abfd, | |
2009 | ld_sect_names[i]); | |
2010 | ||
2011 | if ( asect ) | |
2012 | ld_size += bfd_section_size(abfd, asect); | |
2013 | } | |
2014 | phdr_buf[idx].p_filesz = 0; | |
2015 | /* XXX: need to fix this */ | |
2016 | phdr_buf[idx].p_memsz = ld_size; | |
2017 | } | |
2018 | phdr_buf[idx].p_flags = PF_R + PF_W + PF_X; | |
2019 | phdr_buf[idx].p_align | |
2020 | = bfd_get_section_by_name(abfd,".bss")->alignment_power; | |
2021 | ||
2022 | idx++; | |
2023 | ||
2024 | /* F. Set up the "end of program header table" sentinel. */ | |
2025 | ||
2026 | bzero((char *)(phdr_buf+idx),sizeof(Elf_Internal_Phdr)); | |
2027 | idx++; | |
2028 | ||
2029 | BFD_ASSERT(idx - 1 == *phdr_cnt); | |
2030 | ||
2031 | return phdr_buf; | |
2032 | } | |
2033 | ||
81187b54 KR |
2034 | boolean |
2035 | DEFUN (elf_write_object_contents, (abfd), bfd *abfd) | |
2036 | { | |
2037 | Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ | |
2038 | Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ | |
fb6e80d5 KR |
2039 | Elf_External_Phdr *x_phdrp; /* Program header table, external form */ |
2040 | Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */ | |
81187b54 KR |
2041 | Elf_External_Shdr *x_shdrp; /* Section header table, external form */ |
2042 | Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */ | |
2043 | asection *nsect; | |
2044 | elf_sect_thunk est; | |
2045 | ||
2046 | int outbase = 0; | |
2047 | int count; | |
fb6e80d5 | 2048 | int scnt; |
81187b54 KR |
2049 | struct strtab *shstrtab; |
2050 | ||
fb6e80d5 KR |
2051 | if(abfd->output_has_begun == false) |
2052 | { | |
2053 | elf_compute_section_file_positions(abfd); | |
2054 | abfd->output_has_begun = true; | |
2055 | } | |
81187b54 KR |
2056 | |
2057 | i_ehdrp = elf_elfheader (abfd); | |
2058 | i_shdrp = elf_elfsections (abfd); | |
2059 | shstrtab = elf_shstrtab (abfd); | |
2060 | ||
2061 | est.i_ehdr = i_ehdrp; | |
2062 | est.i_shdrp = i_shdrp; | |
2063 | est.shstrtab = shstrtab; | |
2064 | est.symtab_section = elf_onesymtab (abfd); /* filled in by elf_fake */ | |
2065 | ||
2066 | bfd_map_over_sections(abfd, elf_make_sections, &est); | |
2067 | ||
fb6e80d5 | 2068 | /* Dump out the symtabs. */ |
81187b54 KR |
2069 | { |
2070 | int symcount = bfd_get_symcount (abfd); | |
2071 | asymbol ** syms = bfd_get_outsymbols (abfd); | |
2072 | struct strtab * stt = bfd_new_strtab (abfd); | |
2073 | Elf_Internal_Shdr *symtab_hdr; | |
2074 | Elf_Internal_Shdr *symstrtab_hdr; | |
2075 | int symstrtab_section; | |
2076 | Elf_External_Sym *outbound_syms; | |
2077 | int idx; | |
2078 | ||
2079 | symtab_hdr = &i_shdrp[est.symtab_section]; | |
2080 | symtab_hdr->sh_type = SHT_SYMTAB; | |
2081 | symtab_hdr->sh_entsize = sizeof (Elf_External_Sym); | |
fb6e80d5 | 2082 | symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); |
81187b54 KR |
2083 | |
2084 | /* see assert in elf_fake_sections that supports this: */ | |
2085 | symstrtab_section = est.symtab_section+1; | |
2086 | symstrtab_hdr = &i_shdrp[symstrtab_section]; | |
2087 | symtab_hdr->sh_link = symstrtab_section; | |
2088 | symstrtab_hdr->sh_type = SHT_STRTAB; | |
2089 | ||
2090 | outbound_syms = (Elf_External_Sym*) | |
2091 | bfd_alloc(abfd, (1+symcount) * sizeof(Elf_External_Sym)); | |
2092 | /* now generate the data (for "contents") */ | |
2093 | for (idx = 0; idx < symcount; idx++) | |
2094 | { | |
2095 | Elf_Internal_Sym sym; | |
fb6e80d5 KR |
2096 | bfd_vma value = syms[idx]->value; |
2097 | ||
81187b54 | 2098 | sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name); |
fb6e80d5 KR |
2099 | |
2100 | value += syms[idx]->section->output_section->vma | |
2101 | + syms[idx]->section->output_offset; | |
2102 | sym.st_value = value; | |
2103 | ||
2104 | sym.st_size = (Elf_Word)(elf_symbol_from(abfd, syms[idx]))->internal_elf_sym.st_size; | |
2105 | ||
81187b54 KR |
2106 | if (syms[idx]->flags & BSF_WEAK) |
2107 | sym.st_info = ELF_ST_INFO(STB_WEAK, STT_OBJECT); | |
fb6e80d5 KR |
2108 | else if (syms[idx]->flags & BSF_LOCAL) { |
2109 | if ( syms[idx]->flags & BSF_FUNCTION ) | |
2110 | sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_FUNC); | |
2111 | else | |
2112 | sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_OBJECT); | |
2113 | } | |
2114 | else if (syms[idx]->flags & BSF_GLOBAL) { | |
2115 | if ( syms[idx]->flags & BSF_FUNCTION ) | |
2116 | sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC); | |
2117 | else | |
2118 | sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT); | |
2119 | } | |
2120 | else if (syms[idx]->flags & BSF_EXPORT) { | |
2121 | if ( syms[idx]->flags & BSF_FUNCTION ) | |
2122 | sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC); | |
2123 | else | |
2124 | sym.st_info = ELF_ST_INFO(STB_GLOBAL, STT_OBJECT); | |
2125 | } | |
81187b54 KR |
2126 | else if (syms[idx]->flags & BSF_SECTION_SYM) |
2127 | sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_SECTION); | |
2128 | else if (syms[idx]->flags & BSF_FILE) | |
2129 | sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_FILE); | |
fb6e80d5 KR |
2130 | else |
2131 | sym.st_info = ELF_ST_INFO(STB_LOCAL, STT_OBJECT); | |
81187b54 KR |
2132 | |
2133 | sym.st_other = 0; | |
2134 | if (syms[idx]->section) | |
2135 | sym.st_shndx = | |
2136 | elf_section_from_bfd_section(abfd, | |
2137 | syms[idx]->section->output_section); | |
2138 | else | |
2139 | sym.st_shndx = SHN_UNDEF; | |
2140 | ||
2141 | elf_swap_symbol_out (abfd, &sym, outbound_syms+idx+1); | |
2142 | } | |
2143 | { | |
2144 | /* fill in 0th symbol */ | |
2145 | Elf_Internal_Sym sym; | |
2146 | sym.st_name = 0; | |
2147 | sym.st_value = 0; | |
2148 | sym.st_size = 0; | |
2149 | sym.st_info = 0; | |
2150 | sym.st_other = 0; | |
2151 | sym.st_shndx = SHN_UNDEF; | |
2152 | elf_swap_symbol_out (abfd, &sym, outbound_syms); | |
2153 | } | |
2154 | symtab_hdr->contents = (void*)outbound_syms; | |
2155 | symstrtab_hdr->contents = (void*)stt->tab; | |
2156 | symstrtab_hdr->sh_size = stt->length; | |
fb6e80d5 | 2157 | symstrtab_hdr->sh_type = SHT_STRTAB; |
81187b54 KR |
2158 | } |
2159 | ||
2160 | /* put the strtab out too... */ | |
2161 | { | |
2162 | Elf_Internal_Shdr *this_hdr; | |
2163 | int this_section; | |
2164 | ||
2165 | this_section = i_ehdrp->e_shnum++; | |
2166 | i_ehdrp->e_shstrndx = this_section; | |
2167 | this_hdr = &i_shdrp[this_section]; | |
2168 | this_hdr->sh_name = bfd_add_to_strtab (abfd, shstrtab, ".shstrtab"); | |
2169 | this_hdr->sh_type = SHT_STRTAB; | |
2170 | this_hdr->sh_size = shstrtab->length; | |
fb6e80d5 | 2171 | this_hdr->sh_type = SHT_STRTAB; |
81187b54 KR |
2172 | this_hdr->contents = (void*)shstrtab->tab; |
2173 | } | |
2174 | ||
2175 | outbase = i_ehdrp->e_ehsize; | |
2176 | ||
fb6e80d5 KR |
2177 | /* if we're building an executable, we'll need a program header table */ |
2178 | if (abfd->flags & EXEC_P) | |
2179 | { | |
2180 | i_ehdrp->e_phentsize = sizeof(Elf_External_Phdr); | |
2181 | ||
2182 | /* elf_build_phdrs() returns a (NULL-terminated) array of | |
2183 | Elf_Internal_Phdrs */ | |
2184 | i_phdrp = elf_build_phdrs(abfd,i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); | |
2185 | i_ehdrp->e_phoff = i_ehdrp->e_ehsize; | |
2186 | i_ehdrp->e_shoff = i_ehdrp->e_phoff + (i_ehdrp->e_phentsize | |
2187 | * i_ehdrp->e_phnum); | |
2188 | } | |
2189 | ||
81187b54 KR |
2190 | /* swap the header before spitting it out... */ |
2191 | elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr); | |
2192 | bfd_seek (abfd, (file_ptr) 0, SEEK_SET); | |
2193 | bfd_write ((PTR) &x_ehdr, sizeof(x_ehdr), 1, abfd); | |
2194 | ||
fb6e80d5 | 2195 | outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; |
81187b54 KR |
2196 | outbase += i_ehdrp->e_shentsize * i_ehdrp->e_shnum; |
2197 | ||
2198 | /* now we fix up the offsets... */ | |
2199 | for (count = 1; count < i_ehdrp->e_shnum; count ++) | |
2200 | { | |
2201 | i_shdrp[count].sh_offset = outbase; | |
2202 | outbase += i_shdrp[count].sh_size; | |
2203 | } | |
2204 | ||
fb6e80d5 KR |
2205 | /* If we're building an executable, fixup the program header table |
2206 | offsets. | |
2207 | ||
2208 | @@ For now, assume that the entries are in a fixed order: text, | |
2209 | data, bss. FIXME */ | |
2210 | ||
2211 | if ( abfd->flags & EXEC_P ) | |
2212 | { | |
2213 | static char *CONST section_name[] = { ".text", ".data", ".bss" }; | |
2214 | ||
2215 | for ( count = 0; count < 3; count ++ ) | |
2216 | { | |
2217 | asection *asect = bfd_get_section_by_name(abfd, section_name[count]); | |
2218 | int sh_idx = elf_section_from_bfd_section(abfd, asect); | |
2219 | ||
2220 | i_phdrp[count].p_offset = i_shdrp[sh_idx].sh_offset; | |
2221 | } | |
2222 | ||
2223 | /* write out the program header table entries */ | |
2224 | elf_write_phdrs(abfd, i_ehdrp, i_phdrp, i_ehdrp->e_phnum); | |
2225 | } | |
2226 | ||
81187b54 KR |
2227 | /* at this point we've concocted all the ELF sections... */ |
2228 | x_shdrp = (Elf_External_Shdr *) | |
2229 | bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum)); | |
2230 | if (! x_shdrp) | |
2231 | { | |
2232 | bfd_error = no_memory; | |
2233 | return (false); | |
2234 | } | |
2235 | ||
fb6e80d5 | 2236 | for (count = 0, scnt = 0; count < i_ehdrp->e_shnum; count++) |
81187b54 | 2237 | { |
fb6e80d5 KR |
2238 | elf_swap_shdr_out (abfd, i_shdrp+count, x_shdrp+scnt); |
2239 | scnt++; | |
81187b54 KR |
2240 | } |
2241 | bfd_write ((PTR) x_shdrp, sizeof(*x_shdrp), i_ehdrp->e_shnum, abfd); | |
2242 | /* need to dump the string table too... */ | |
2243 | ||
2244 | /* after writing the headers, we need to write the sections too... */ | |
2245 | nsect = abfd->sections; | |
2246 | for (count = 0; count < i_ehdrp->e_shnum; count ++) | |
2247 | { | |
2248 | if(i_shdrp[count].contents) | |
2249 | { | |
2250 | bfd_seek (abfd, i_shdrp[count].sh_offset, SEEK_SET); | |
2251 | bfd_write (i_shdrp[count].contents, i_shdrp[count].sh_size, 1, abfd); | |
2252 | } | |
2253 | } | |
2254 | ||
81187b54 | 2255 | return true; |
81187b54 KR |
2256 | } |
2257 | ||
2258 | /* Given an index of a section, retrieve a pointer to it. Note | |
2259 | that for our purposes, sections are indexed by {1, 2, ...} with | |
2260 | 0 being an illegal index. */ | |
2261 | ||
2262 | /* In the original, each ELF section went into exactly one BFD | |
2263 | section. This doesn't really make sense, so we need a real mapping. | |
2264 | The mapping has to hide in the Elf_Internal_Shdr since asection | |
2265 | doesn't have anything like a tdata field... */ | |
2266 | ||
2267 | static struct sec * | |
2268 | DEFUN (section_from_elf_index, (abfd, index), | |
2269 | bfd *abfd AND | |
2270 | int index) | |
2271 | { | |
fb6e80d5 KR |
2272 | /* @@ Is bfd_com_section really correct in all the places it could |
2273 | be returned from this routine? */ | |
81187b54 | 2274 | |
fb6e80d5 KR |
2275 | if (index == SHN_ABS) |
2276 | return &bfd_com_section; | |
2277 | if (index == SHN_COMMON) | |
2278 | return &bfd_com_section; | |
81187b54 | 2279 | |
fb6e80d5 KR |
2280 | { |
2281 | Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd); | |
2282 | Elf_Internal_Shdr *hdr = i_shdrp + index; | |
2283 | ||
2284 | switch (hdr->sh_type) | |
2285 | { | |
2286 | /* ELF sections that map to BFD sections */ | |
2287 | case SHT_PROGBITS: | |
2288 | case SHT_NOBITS: | |
2289 | if (! hdr->rawdata) | |
2290 | bfd_section_from_shdr (abfd, index); | |
2291 | return (struct sec *) hdr->rawdata; | |
2292 | ||
2293 | default: | |
2294 | return (struct sec *) &bfd_abs_section; | |
2295 | } | |
2296 | } | |
81187b54 KR |
2297 | } |
2298 | ||
2299 | /* given a section, search the header to find them... */ | |
2300 | static int | |
2301 | DEFUN (elf_section_from_bfd_section, (abfd, asect), | |
2302 | bfd *abfd AND | |
2303 | struct sec *asect) | |
2304 | { | |
2305 | Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd); | |
2306 | int index; | |
2307 | Elf_Internal_Shdr *hdr; | |
2308 | int maxindex = elf_elfheader (abfd)->e_shnum; | |
fb6e80d5 KR |
2309 | |
2310 | if (asect == &bfd_abs_section) | |
2311 | return SHN_ABS; | |
2312 | if (asect == &bfd_com_section) | |
2313 | return SHN_COMMON; | |
2314 | ||
81187b54 KR |
2315 | for(index = 0; index < maxindex; index++) { |
2316 | hdr = &i_shdrp[index]; | |
2317 | switch (hdr->sh_type) | |
2318 | { | |
2319 | /* ELF sections that map to BFD sections */ | |
2320 | case SHT_PROGBITS: | |
2321 | case SHT_NOBITS: | |
2322 | if (hdr->rawdata) | |
2323 | { | |
2324 | if (((struct sec *)(hdr->rawdata)) == asect) | |
2325 | return index; | |
2326 | } | |
2327 | break; | |
2328 | default: | |
2329 | break; | |
2330 | } | |
2331 | } | |
2332 | return 0; | |
2333 | } | |
2334 | ||
2335 | static boolean | |
2336 | DEFUN (elf_slurp_symbol_table, (abfd, symptrs), | |
2337 | bfd *abfd AND | |
2338 | asymbol **symptrs) /* Buffer for generated bfd symbols */ | |
2339 | { | |
2340 | Elf_Internal_Shdr *i_shdrp = elf_elfsections (abfd); | |
2341 | Elf_Internal_Shdr *hdr = i_shdrp + elf_onesymtab (abfd); | |
2342 | int symcount; /* Number of external ELF symbols */ | |
2343 | int i; | |
fb6e80d5 KR |
2344 | elf_symbol_type *sym; /* Pointer to current bfd symbol */ |
2345 | elf_symbol_type *symbase; /* Buffer for generated bfd symbols */ | |
81187b54 KR |
2346 | Elf_Internal_Sym i_sym; |
2347 | Elf_External_Sym *x_symp; | |
2348 | ||
2349 | /* this is only valid because there is only one symtab... */ | |
2350 | /* FIXME: This is incorrect, there may also be a dynamic symbol | |
2351 | table which is a subset of the full symbol table. We either need | |
2352 | to be prepared to read both (and merge them) or ensure that we | |
2353 | only read the full symbol table. Currently we only get called to | |
2354 | read the full symbol table. -fnf */ | |
2355 | if (bfd_get_outsymbols (abfd) != NULL) | |
2356 | { | |
2357 | return (true); | |
2358 | } | |
2359 | ||
2360 | /* Read each raw ELF symbol, converting from external ELF form to | |
2361 | internal ELF form, and then using the information to create a | |
2362 | canonical bfd symbol table entry. | |
2363 | ||
2364 | Note that we allocate the initial bfd canonical symbol buffer | |
2365 | based on a one-to-one mapping of the ELF symbols to canonical | |
2366 | symbols. We actually use all the ELF symbols, so there will be no | |
2367 | space left over at the end. When we have all the symbols, we | |
2368 | build the caller's pointer vector. */ | |
2369 | ||
2370 | if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) | |
2371 | { | |
2372 | bfd_error = system_call_error; | |
2373 | return (false); | |
2374 | } | |
2375 | ||
2376 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); | |
fb6e80d5 | 2377 | symbase = (elf_symbol_type *) bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)); |
81187b54 KR |
2378 | sym = symbase; |
2379 | ||
2380 | /* Temporarily allocate room for the raw ELF symbols. */ | |
2381 | x_symp = (Elf_External_Sym *) bfd_xmalloc (symcount * sizeof (Elf_External_Sym)); | |
2382 | ||
2383 | if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd) | |
2384 | != symcount * sizeof (Elf_External_Sym)) | |
2385 | { | |
2386 | free ((PTR)x_symp); | |
2387 | bfd_error = system_call_error; | |
2388 | return (false); | |
2389 | } | |
2390 | /* Skip first symbol, which is a null dummy. */ | |
2391 | for (i = 1; i < symcount; i++) | |
2392 | { | |
2393 | elf_swap_symbol_in (abfd, x_symp + i, &i_sym); | |
fb6e80d5 KR |
2394 | memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym)); |
2395 | memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym)); | |
2396 | sym->symbol.the_bfd = abfd; | |
81187b54 | 2397 | if (i_sym.st_name > 0) |
fb6e80d5 KR |
2398 | sym->symbol.name = elf_string_from_elf_section(abfd, hdr->sh_link, |
2399 | i_sym.st_name); | |
81187b54 | 2400 | else |
fb6e80d5 KR |
2401 | sym->symbol.name = ""; /* perhaps should include the number? */ |
2402 | ||
2403 | sym->symbol.value = i_sym.st_value; | |
2404 | /* FIXME -- this is almost certainly bogus. It's from Pace | |
2405 | Willisson's hasty Solaris support, to pass the sizes of | |
2406 | object files or functions down into GDB via the back door, to | |
2407 | circumvent some other kludge in how Sun hacked stabs. -- | |
2408 | [email protected] */ | |
2409 | /* XXX size is now stored via a pointer in an elf_symbol_type */ | |
2410 | /* sym ->symbol.udata = (PTR)i_sym.st_size; */ | |
2411 | /* FIXME -- end of bogosity. */ | |
81187b54 KR |
2412 | if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV) |
2413 | { | |
fb6e80d5 | 2414 | sym->symbol.section = section_from_elf_index (abfd, i_sym.st_shndx); |
81187b54 KR |
2415 | } |
2416 | else if (i_sym.st_shndx == SHN_ABS) | |
2417 | { | |
fb6e80d5 | 2418 | sym->symbol.section = &bfd_abs_section; |
81187b54 KR |
2419 | } |
2420 | else if (i_sym.st_shndx == SHN_COMMON) | |
2421 | { | |
fb6e80d5 | 2422 | sym->symbol.section = &bfd_com_section; |
81187b54 KR |
2423 | } |
2424 | else if (i_sym.st_shndx == SHN_UNDEF) | |
2425 | { | |
fb6e80d5 | 2426 | sym->symbol.section = &bfd_und_section; |
81187b54 KR |
2427 | } |
2428 | else | |
fb6e80d5 | 2429 | sym->symbol.section = &bfd_abs_section; |
81187b54 KR |
2430 | |
2431 | switch (ELF_ST_BIND (i_sym.st_info)) | |
2432 | { | |
2433 | case STB_LOCAL: | |
fb6e80d5 | 2434 | sym->symbol.flags |= BSF_LOCAL; |
81187b54 KR |
2435 | break; |
2436 | case STB_GLOBAL: | |
fb6e80d5 | 2437 | sym->symbol.flags |= (BSF_GLOBAL | BSF_EXPORT); |
81187b54 KR |
2438 | break; |
2439 | case STB_WEAK: | |
fb6e80d5 | 2440 | sym->symbol.flags |= BSF_WEAK; |
81187b54 KR |
2441 | break; |
2442 | } | |
2443 | ||
2444 | switch (ELF_ST_TYPE (i_sym.st_info)) | |
2445 | { | |
2446 | case STT_SECTION: | |
fb6e80d5 | 2447 | sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING; |
81187b54 KR |
2448 | break; |
2449 | case STT_FILE: | |
fb6e80d5 KR |
2450 | sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING; |
2451 | break; | |
2452 | case STT_FUNC: | |
2453 | sym->symbol.flags |= BSF_FUNCTION; | |
81187b54 KR |
2454 | break; |
2455 | } | |
fb6e80d5 KR |
2456 | /* Is this a definition of $global$? If so, keep it because it will be |
2457 | needd if any relocations are performed. */ | |
2458 | if (!strcmp (sym->symbol.name, "$global$") | |
2459 | && sym->symbol.section != &bfd_und_section) | |
2460 | { | |
2461 | /* @@ Why is this referring to backend data and not a field of | |
2462 | abfd? FIXME */ | |
2463 | struct elf_backend_data *be_data = (struct elf_backend_data *) abfd->xvec->backend_data; | |
2464 | ||
2465 | be_data->global_sym = sym; | |
2466 | } | |
81187b54 KR |
2467 | sym++; |
2468 | } | |
2469 | ||
2470 | /* We rely on the zalloc to clear out the final symbol entry. */ | |
2471 | ||
fb6e80d5 | 2472 | obj_raw_syms (abfd) = x_symp; |
81187b54 KR |
2473 | |
2474 | bfd_get_symcount(abfd) = symcount = sym - symbase; | |
2475 | ||
2476 | /* Fill in the user's symbol pointer vector if needed. */ | |
2477 | if (symptrs) | |
2478 | { | |
2479 | sym = symbase; | |
2480 | while (symcount-- > 0) | |
2481 | { | |
fb6e80d5 KR |
2482 | *symptrs++ = &sym->symbol; |
2483 | sym++; | |
81187b54 KR |
2484 | } |
2485 | *symptrs = 0; /* Final null pointer */ | |
2486 | } | |
2487 | ||
2488 | return (true); | |
2489 | } | |
2490 | ||
2491 | /* Return the number of bytes required to hold the symtab vector. | |
2492 | ||
2493 | Note that we base it on the count plus 1, since we will null terminate | |
2494 | the vector allocated based on this size. However, the ELF symbol table | |
2495 | always has a dummy entry as symbol #0, so it ends up even. */ | |
2496 | ||
2497 | unsigned int | |
2498 | DEFUN (elf_get_symtab_upper_bound, (abfd), bfd *abfd) | |
2499 | { | |
2500 | unsigned int symcount; | |
2501 | unsigned int symtab_size = 0; | |
2502 | Elf_Internal_Shdr *i_shdrp; | |
2503 | Elf_Internal_Shdr *hdr; | |
2504 | ||
2505 | i_shdrp = elf_elfsections (abfd); | |
2506 | if (i_shdrp != NULL) | |
2507 | { | |
2508 | hdr = i_shdrp + elf_onesymtab (abfd); | |
2509 | symcount = hdr->sh_size / sizeof (Elf_External_Sym); | |
2510 | symtab_size = (symcount - 1 + 1) * (sizeof (asymbol)); | |
2511 | } | |
2512 | return (symtab_size); | |
2513 | } | |
2514 | ||
2515 | /* | |
2516 | This function return the number of bytes required to store the | |
2517 | relocation information associated with section <<sect>> | |
2518 | attached to bfd <<abfd>> | |
2519 | ||
2520 | */ | |
2521 | unsigned int | |
2522 | elf_get_reloc_upper_bound (abfd, asect) | |
2523 | bfd *abfd; | |
2524 | sec_ptr asect; | |
2525 | { | |
2526 | if (asect->flags & SEC_RELOC) | |
2527 | { | |
2528 | /* either rel or rela */ | |
2529 | return asect->_raw_size; | |
2530 | } | |
2531 | else | |
2532 | return (0); | |
2533 | } | |
2534 | ||
2535 | static boolean | |
2536 | DEFUN(elf_slurp_reloca_table,(abfd, asect, symbols), | |
2537 | bfd *abfd AND | |
2538 | sec_ptr asect AND | |
2539 | asymbol **symbols) | |
2540 | { | |
2541 | Elf_External_Rela *native_relocs; | |
2542 | arelent *reloc_cache; | |
2543 | arelent *cache_ptr; | |
2544 | ||
2545 | unsigned int idx; | |
2546 | ||
2547 | if (asect->relocation) | |
2548 | return true; | |
2549 | if (asect->reloc_count == 0) | |
2550 | return true; | |
2551 | if (asect->flags & SEC_CONSTRUCTOR) | |
2552 | return true; | |
2553 | ||
2554 | bfd_seek (abfd, asect->rel_filepos, SEEK_SET); | |
2555 | native_relocs = (Elf_External_Rela *) | |
2556 | bfd_alloc(abfd, asect->reloc_count * sizeof(Elf_External_Rela)); | |
2557 | bfd_read ((PTR) native_relocs, | |
2558 | sizeof(Elf_External_Rela), asect->reloc_count, abfd); | |
2559 | ||
2560 | reloc_cache = (arelent *) | |
2561 | bfd_alloc(abfd, (size_t) (asect->reloc_count * sizeof(arelent))); | |
2562 | ||
2563 | if (! reloc_cache) { | |
2564 | bfd_error = no_memory; | |
2565 | return false; | |
2566 | } | |
2567 | ||
2568 | for (idx = 0; idx < asect->reloc_count; idx ++) | |
2569 | { | |
2570 | #ifdef RELOC_PROCESSING | |
2571 | /* sparc, 68k, 88k, 860 use rela only. */ | |
2572 | /* 386 and we32000 use rel only... fix it for them later. */ | |
2573 | Elf_Internal_Rela dst; | |
2574 | Elf_External_Rela *src; | |
2575 | ||
2576 | cache_ptr = reloc_cache + idx; | |
2577 | src = native_relocs + idx; | |
2578 | elf_swap_reloca_in(abfd, src, &dst); | |
2579 | ||
2580 | RELOC_PROCESSING(cache_ptr, &dst, symbols, abfd, asect); | |
2581 | #else | |
2582 | Elf_Internal_Rela dst; | |
2583 | Elf_External_Rela *src; | |
2584 | ||
2585 | cache_ptr = reloc_cache + idx; | |
2586 | src = native_relocs + idx; | |
2587 | ||
2588 | elf_swap_reloca_in(abfd, src, &dst); | |
2589 | ||
2590 | if(asect->flags & SEC_RELOC) | |
2591 | { | |
2592 | /* relocatable, so the offset is off of the section */ | |
2593 | cache_ptr->address = dst.r_offset + asect->vma; | |
2594 | } | |
2595 | else | |
2596 | { | |
2597 | /* non-relocatable, so the offset a virtual address */ | |
2598 | cache_ptr->address = dst.r_offset; | |
2599 | } | |
fb6e80d5 KR |
2600 | /* ELF_R_SYM(dst.r_info) is the symbol table offset; subtract 1 |
2601 | because the first entry is NULL. */ | |
2602 | cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM(dst.r_info) - 1; | |
81187b54 KR |
2603 | cache_ptr->addend = dst.r_addend; |
2604 | ||
2605 | /* Fill in the cache_ptr->howto field from dst.r_type */ | |
2606 | { | |
2607 | struct elf_backend_data *ebd; | |
2608 | ebd = (struct elf_backend_data *) (abfd->xvec->backend_data); | |
2609 | (*ebd->elf_info_to_howto)(abfd, cache_ptr, &dst); | |
2610 | } | |
2611 | #endif | |
2612 | } | |
2613 | ||
2614 | asect->relocation = reloc_cache; | |
2615 | return true; | |
2616 | } | |
2617 | ||
2618 | ||
2619 | unsigned int | |
2620 | elf_canonicalize_reloc (abfd, section, relptr, symbols) | |
2621 | bfd *abfd; | |
2622 | sec_ptr section; | |
2623 | arelent **relptr; | |
2624 | asymbol **symbols; | |
2625 | { | |
2626 | arelent *tblptr = section->relocation; | |
2627 | unsigned int count = 0; | |
2628 | ||
2629 | /* snarfed from coffcode.h */ | |
2630 | /* FIXME: this could be reloc... */ | |
2631 | elf_slurp_reloca_table(abfd, section, symbols); | |
2632 | ||
2633 | tblptr = section->relocation; | |
2634 | if (!tblptr) | |
2635 | return 0; | |
2636 | ||
2637 | for (; count++ < section->reloc_count;) | |
2638 | *relptr++ = tblptr++; | |
2639 | ||
2640 | *relptr = 0; | |
2641 | return section->reloc_count; | |
2642 | } | |
2643 | ||
2644 | unsigned int | |
2645 | DEFUN (elf_get_symtab, (abfd, alocation), | |
2646 | bfd *abfd AND | |
2647 | asymbol **alocation) | |
2648 | { | |
2649 | ||
2650 | if (!elf_slurp_symbol_table (abfd, alocation)) | |
2651 | return (0); | |
2652 | else | |
2653 | return (bfd_get_symcount (abfd)); | |
2654 | } | |
2655 | ||
2656 | asymbol * | |
2657 | DEFUN (elf_make_empty_symbol, (abfd), | |
2658 | bfd *abfd) | |
2659 | { | |
2660 | elf_symbol_type *newsym; | |
2661 | ||
2662 | newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); | |
2663 | if (! newsym) | |
2664 | { | |
2665 | bfd_error = no_memory; | |
2666 | return (NULL); | |
2667 | } | |
2668 | else | |
2669 | { | |
2670 | newsym -> symbol.the_bfd = abfd; | |
2671 | return (&newsym -> symbol); | |
2672 | } | |
2673 | } | |
2674 | ||
2675 | void | |
2676 | DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how), | |
2677 | bfd *ignore_abfd AND | |
2678 | PTR filep AND | |
2679 | asymbol *symbol AND | |
2680 | bfd_print_symbol_type how) | |
2681 | { | |
2682 | FILE *file = (FILE *)filep; | |
2683 | switch (how) | |
2684 | { | |
2685 | case bfd_print_symbol_name: | |
2686 | fprintf(file, "%s", symbol->name); | |
2687 | break; | |
2688 | case bfd_print_symbol_more: | |
2689 | fprintf(file, "elf %lx %lx", | |
2690 | symbol->value, | |
2691 | symbol->flags); | |
2692 | break; | |
2693 | case bfd_print_symbol_nm: | |
2694 | case bfd_print_symbol_all: | |
2695 | { | |
2696 | CONST char *section_name; | |
2697 | section_name = symbol->section? symbol->section->name : "(*none*)"; | |
2698 | bfd_print_symbol_vandf((PTR) file, symbol); | |
2699 | fprintf(file, " %s\t%s", | |
2700 | section_name, | |
2701 | symbol->name); | |
2702 | } | |
2703 | break; | |
2704 | } | |
2705 | ||
2706 | } | |
2707 | ||
2708 | alent * | |
2709 | DEFUN (elf_get_lineno,(ignore_abfd, symbol), | |
2710 | bfd *ignore_abfd AND | |
2711 | asymbol *symbol) | |
2712 | { | |
2713 | fprintf (stderr, "elf_get_lineno unimplemented\n"); | |
2714 | fflush (stderr); | |
2715 | abort (); | |
2716 | return (NULL); | |
2717 | } | |
2718 | ||
2719 | boolean | |
2720 | DEFUN (elf_set_arch_mach,(abfd, arch, machine), | |
2721 | bfd *abfd AND | |
2722 | enum bfd_architecture arch AND | |
2723 | unsigned long machine) | |
2724 | { | |
2725 | /* Allow any architecture to be supported by the elf backend */ | |
2726 | switch(arch) | |
2727 | { | |
2728 | case bfd_arch_unknown: /* EM_NONE */ | |
2729 | case bfd_arch_sparc: /* EM_SPARC */ | |
2730 | case bfd_arch_i386: /* EM_386 */ | |
2731 | case bfd_arch_m68k: /* EM_68K */ | |
2732 | case bfd_arch_m88k: /* EM_88K */ | |
2733 | case bfd_arch_i860: /* EM_860 */ | |
2734 | case bfd_arch_mips: /* EM_MIPS (MIPS R3000) */ | |
fb6e80d5 | 2735 | case bfd_arch_hppa: /* EM_HPPA (HP PA_RISC) */ |
81187b54 KR |
2736 | return bfd_default_set_arch_mach(abfd, arch, machine); |
2737 | default: | |
2738 | return false; | |
2739 | } | |
2740 | } | |
2741 | ||
2742 | boolean | |
2743 | DEFUN (elf_find_nearest_line,(abfd, | |
2744 | section, | |
2745 | symbols, | |
2746 | offset, | |
2747 | filename_ptr, | |
2748 | functionname_ptr, | |
2749 | line_ptr), | |
2750 | bfd *abfd AND | |
2751 | asection *section AND | |
2752 | asymbol **symbols AND | |
2753 | bfd_vma offset AND | |
2754 | CONST char **filename_ptr AND | |
2755 | CONST char **functionname_ptr AND | |
2756 | unsigned int *line_ptr) | |
2757 | { | |
fb6e80d5 | 2758 | return false; |
81187b54 KR |
2759 | } |
2760 | ||
2761 | int | |
2762 | DEFUN (elf_sizeof_headers, (abfd, reloc), | |
2763 | bfd *abfd AND | |
2764 | boolean reloc) | |
2765 | { | |
2766 | fprintf (stderr, "elf_sizeof_headers unimplemented\n"); | |
2767 | fflush (stderr); | |
2768 | abort (); | |
2769 | return (0); | |
2770 | } | |
2771 | ||
2772 | boolean | |
2773 | DEFUN(elf_set_section_contents, (abfd, section, location, offset, count), | |
2774 | bfd *abfd AND | |
2775 | sec_ptr section AND | |
2776 | PTR location AND | |
2777 | file_ptr offset AND | |
2778 | bfd_size_type count) | |
2779 | { | |
2780 | int dest_sect; | |
2781 | void *contents; | |
2782 | if (abfd->output_has_begun == false) /* set by bfd.c handler? */ | |
2783 | { | |
2784 | /* do setup calculations (FIXME) */ | |
2785 | elf_compute_section_file_positions(abfd); | |
fb6e80d5 | 2786 | abfd->output_has_begun = true; |
81187b54 | 2787 | } |
fb6e80d5 | 2788 | |
81187b54 KR |
2789 | dest_sect = elf_section_from_bfd_section(abfd, section); |
2790 | if(!dest_sect) | |
2791 | return false; | |
2792 | ||
fb6e80d5 KR |
2793 | if (bfd_seek (abfd, elf_elfsections(abfd)[dest_sect].sh_offset + offset, SEEK_SET) == -1) |
2794 | return false; | |
2795 | if (bfd_write (location, 1, count, abfd) != count) | |
2796 | return false; | |
81187b54 KR |
2797 | return true; |
2798 | } | |
2799 | ||
2800 | void | |
2801 | DEFUN (elf_no_info_to_howto, (abfd, cache_ptr, dst), | |
2802 | bfd *abfd AND | |
2803 | arelent *cache_ptr AND | |
2804 | Elf_Internal_Rela *dst) | |
2805 | { | |
2806 | abort (); | |
2807 | } |