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