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