1 /* ELF executable support for BFD.
2 Copyright 1991, 1992, 1993 Free Software Foundation, Inc.
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.
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.
16 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
32 /* Problems and other issues to resolve.
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.
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
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)
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
60 #include <string.h> /* For strrchr and friends */
70 /* Renaming structures, typedefs, macros and functions to be size-specific. */
71 #define Elf_External_Ehdr NAME(Elf,External_Ehdr)
72 #define Elf_External_Sym NAME(Elf,External_Sym)
73 #define Elf_External_Shdr NAME(Elf,External_Shdr)
74 #define Elf_External_Phdr NAME(Elf,External_Phdr)
75 #define Elf_External_Rel NAME(Elf,External_Rel)
76 #define Elf_External_Rela NAME(Elf,External_Rela)
78 #define elf_symbol_type NAME(elf,symbol_type)
80 #define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
81 #define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
82 #define elf_core_file_matches_executable_p NAME(bfd_elf,core_file_matches_executable_p)
83 #define elf_object_p NAME(bfd_elf,object_p)
84 #define elf_core_file_p NAME(bfd_elf,core_file_p)
85 #define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
86 #define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
87 #define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
88 #define elf_get_symtab NAME(bfd_elf,get_symtab)
89 #define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
90 #define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
91 #define elf_print_symbol NAME(bfd_elf,print_symbol)
92 #define elf_get_lineno NAME(bfd_elf,get_lineno)
93 #define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
94 #define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
95 #define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
96 #define elf_set_section_contents NAME(bfd_elf,set_section_contents)
97 #define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
98 #define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
99 #define elf_new_section_hook NAME(bfd_elf,new_section_hook)
100 #define write_relocs NAME(bfd_elf,_write_relocs)
103 #define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
104 #define ELF_R_SYM(X) ELF64_R_SYM(X)
105 #define ELFCLASS ELFCLASS64
108 #define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
109 #define ELF_R_SYM(X) ELF32_R_SYM(X)
110 #define ELFCLASS ELFCLASS32
113 static int shstrtab_length_fixed;
115 struct elf_sect_data {
120 /* Forward declarations of static functions */
122 static struct sec * section_from_elf_index PARAMS ((bfd *, int));
124 static int elf_section_from_bfd_section PARAMS ((bfd *, struct sec *));
126 static boolean elf_slurp_symbol_table PARAMS ((bfd *, asymbol **));
128 static int elf_symbol_from_bfd_symbol PARAMS ((bfd *,
129 struct symbol_cache_entry **));
131 static void elf_map_symbols PARAMS ((bfd *));
132 static void swap_out_syms PARAMS ((bfd *));
135 static void elf_debug_section PARAMS ((char *, int, Elf_Internal_Shdr *));
136 static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
139 #define elf_string_from_elf_strtab(abfd,strindex) \
140 elf_string_from_elf_section(abfd,elf_elfheader(abfd)->e_shstrndx,strindex)
143 /* Structure swapping routines */
145 /* Should perhaps use put_offset, put_word, etc. For now, the two versions
146 can be handled by explicitly specifying 32 bits or "the long type". */
148 #define put_word bfd_h_put_64
149 #define get_word bfd_h_get_64
152 #define put_word bfd_h_put_32
153 #define get_word bfd_h_get_32
156 /* Translate an ELF symbol in external format into an ELF symbol in internal
160 DEFUN (elf_swap_symbol_in, (abfd, src, dst),
162 Elf_External_Sym * src AND
163 Elf_Internal_Sym * dst)
165 dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
166 dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
167 dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
168 dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
169 dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
170 dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
173 /* Translate an ELF symbol in internal format into an ELF symbol in external
177 DEFUN (elf_swap_symbol_out, (abfd, src, dst),
179 Elf_Internal_Sym * src AND
180 Elf_External_Sym * dst)
182 bfd_h_put_32 (abfd, src->st_name, dst->st_name);
183 put_word (abfd, src->st_value, dst->st_value);
184 put_word (abfd, src->st_size, dst->st_size);
185 bfd_h_put_8 (abfd, src->st_info, dst->st_info);
186 bfd_h_put_8 (abfd, src->st_other, dst->st_other);
187 bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
191 /* Translate an ELF file header in external format into an ELF file header in
195 DEFUN (elf_swap_ehdr_in, (abfd, src, dst),
197 Elf_External_Ehdr * src AND
198 Elf_Internal_Ehdr * dst)
200 memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
201 dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);
202 dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);
203 dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);
204 dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
205 dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
206 dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);
207 dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);
208 dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);
209 dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);
210 dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);
211 dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);
212 dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);
213 dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);
216 /* Translate an ELF file header in internal format into an ELF file header in
220 DEFUN (elf_swap_ehdr_out, (abfd, src, dst),
222 Elf_Internal_Ehdr * src AND
223 Elf_External_Ehdr * dst)
225 memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
226 /* note that all elements of dst are *arrays of unsigned char* already... */
227 bfd_h_put_16 (abfd, src->e_type, dst->e_type);
228 bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
229 bfd_h_put_32 (abfd, src->e_version, dst->e_version);
230 put_word (abfd, src->e_entry, dst->e_entry);
231 put_word (abfd, src->e_phoff, dst->e_phoff);
232 put_word (abfd, src->e_shoff, dst->e_shoff);
233 bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
234 bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
235 bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
236 bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
237 bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
238 bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
239 bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
243 /* Translate an ELF section header table entry in external format into an
244 ELF section header table entry in internal format. */
247 DEFUN (elf_swap_shdr_in, (abfd, src, dst),
249 Elf_External_Shdr * src AND
250 Elf_Internal_Shdr * dst)
252 dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
253 dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
254 dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);
255 dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
256 dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
257 dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);
258 dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
259 dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
260 dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);
261 dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
262 /* we haven't done any processing on it yet, so... */
263 dst->rawdata = (void *) 0;
266 /* Translate an ELF section header table entry in internal format into an
267 ELF section header table entry in external format. */
270 DEFUN (elf_swap_shdr_out, (abfd, src, dst),
272 Elf_Internal_Shdr * src AND
273 Elf_External_Shdr * dst)
275 /* note that all elements of dst are *arrays of unsigned char* already... */
276 bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
277 bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
278 put_word (abfd, src->sh_flags, dst->sh_flags);
279 put_word (abfd, src->sh_addr, dst->sh_addr);
280 put_word (abfd, src->sh_offset, dst->sh_offset);
281 put_word (abfd, src->sh_size, dst->sh_size);
282 bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
283 bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
284 put_word (abfd, src->sh_addralign, dst->sh_addralign);
285 put_word (abfd, src->sh_entsize, dst->sh_entsize);
289 /* Translate an ELF program header table entry in external format into an
290 ELF program header table entry in internal format. */
293 DEFUN (elf_swap_phdr_in, (abfd, src, dst),
295 Elf_External_Phdr * src AND
296 Elf_Internal_Phdr * dst)
298 dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
299 dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
300 dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
301 dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
302 dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
303 dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
304 dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
305 dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
309 DEFUN (elf_swap_phdr_out, (abfd, src, dst),
311 Elf_Internal_Phdr * src AND
312 Elf_External_Phdr * dst)
314 /* note that all elements of dst are *arrays of unsigned char* already... */
315 bfd_h_put_32 (abfd, src->p_type, dst->p_type);
316 put_word (abfd, src->p_offset, dst->p_offset);
317 put_word (abfd, src->p_vaddr, dst->p_vaddr);
318 put_word (abfd, src->p_paddr, dst->p_paddr);
319 put_word (abfd, src->p_filesz, dst->p_filesz);
320 put_word (abfd, src->p_memsz, dst->p_memsz);
321 bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
322 put_word (abfd, src->p_align, dst->p_align);
325 /* Translate an ELF reloc from external format to internal format. */
327 DEFUN (elf_swap_reloc_in, (abfd, src, dst),
329 Elf_External_Rel * src AND
330 Elf_Internal_Rel * dst)
332 dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
333 dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
337 DEFUN (elf_swap_reloca_in, (abfd, src, dst),
339 Elf_External_Rela * src AND
340 Elf_Internal_Rela * dst)
342 dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
343 dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
344 dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend);
347 /* Translate an ELF reloc from internal format to external format. */
349 DEFUN (elf_swap_reloc_out, (abfd, src, dst),
351 Elf_Internal_Rel * src AND
352 Elf_External_Rel * dst)
354 put_word (abfd, src->r_offset, dst->r_offset);
355 put_word (abfd, src->r_info, dst->r_info);
359 DEFUN (elf_swap_reloca_out, (abfd, src, dst),
361 Elf_Internal_Rela * src AND
362 Elf_External_Rela * dst)
364 put_word (abfd, src->r_offset, dst->r_offset);
365 put_word (abfd, src->r_info, dst->r_info);
366 put_word (abfd, src->r_addend, dst->r_addend);
371 /* String table creation/manipulation routines */
373 static struct strtab *
374 DEFUN (bfd_new_strtab, (abfd),
379 ss = (struct strtab *) bfd_xmalloc (sizeof (struct strtab));
380 ss->tab = bfd_xmalloc (1);
381 BFD_ASSERT (ss->tab != 0);
390 DEFUN (bfd_add_to_strtab, (abfd, ss, str),
392 struct strtab *ss AND
395 /* should search first, but for now: */
396 /* include the trailing NUL */
397 int ln = strlen (str) + 1;
399 /* should this be using obstacks? */
400 ss->tab = realloc (ss->tab, ss->length + ln);
402 BFD_ASSERT (ss->tab != 0);
403 strcpy (ss->tab + ss->length, str);
407 return ss->length - ln;
411 DEFUN (bfd_add_2_to_strtab, (abfd, ss, str, str2),
413 struct strtab *ss AND
417 /* should search first, but for now: */
418 /* include the trailing NUL */
419 int ln = strlen (str) + strlen (str2) + 1;
421 /* should this be using obstacks? */
423 ss->tab = realloc (ss->tab, ss->length + ln);
425 ss->tab = bfd_xmalloc (ln);
427 BFD_ASSERT (ss->tab != 0);
428 strcpy (ss->tab + ss->length, str);
429 strcpy (ss->tab + ss->length + strlen (str), str2);
433 return ss->length - ln;
437 /* ELF .o/exec file reading */
439 /* Create a new bfd section from an ELF section header. */
442 DEFUN (bfd_section_from_shdr, (abfd, shindex),
444 unsigned int shindex)
446 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex];
447 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd);
451 name = elf_string_from_elf_strtab (abfd, hdr->sh_name);
453 switch (hdr->sh_type)
457 /* inactive section. Throw it away. */
461 /* Bits that get saved. This one is real. */
464 newsect = bfd_make_section (abfd, name);
467 newsect->filepos = hdr->sh_offset; /* so we can read back the bits */
468 newsect->flags |= SEC_HAS_CONTENTS;
469 newsect->vma = hdr->sh_addr;
470 newsect->_raw_size = hdr->sh_size;
471 newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
473 if (hdr->sh_flags & SHF_ALLOC)
475 newsect->flags |= SEC_ALLOC;
476 newsect->flags |= SEC_LOAD;
479 if (!(hdr->sh_flags & SHF_WRITE))
480 newsect->flags |= SEC_READONLY;
482 if (hdr->sh_flags & SHF_EXECINSTR)
483 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
485 newsect->flags |= SEC_DATA;
487 hdr->rawdata = (void *) newsect;
490 hdr->rawdata = (void *) bfd_get_section_by_name (abfd, name);
495 /* Bits that get saved. This one is real. */
498 newsect = bfd_make_section (abfd, name);
501 newsect->vma = hdr->sh_addr;
502 newsect->_raw_size = hdr->sh_size;
503 newsect->filepos = hdr->sh_offset; /* fake */
504 newsect->alignment_power = bfd_log2 (hdr->sh_addralign);
505 if (hdr->sh_flags & SHF_ALLOC)
506 newsect->flags |= SEC_ALLOC;
508 if (!(hdr->sh_flags & SHF_WRITE))
509 newsect->flags |= SEC_READONLY;
511 if (hdr->sh_flags & SHF_EXECINSTR)
512 newsect->flags |= SEC_CODE; /* FIXME: may only contain SOME code */
514 newsect->flags |= SEC_DATA;
516 hdr->rawdata = (void *) newsect;
521 case SHT_SYMTAB: /* A symbol table */
522 if (elf_onesymtab (abfd) == shindex)
525 BFD_ASSERT (hdr->sh_entsize == sizeof (Elf_External_Sym));
526 BFD_ASSERT (elf_onesymtab (abfd) == 0);
527 elf_onesymtab (abfd) = shindex;
528 elf_tdata(abfd)->symtab_hdr = *hdr;
529 elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->symtab_hdr;
530 abfd->flags |= HAS_SYMS;
533 case SHT_STRTAB: /* A string table */
536 if (ehdr->e_shstrndx == shindex)
538 elf_tdata(abfd)->shstrtab_hdr = *hdr;
539 elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->shstrtab_hdr;
540 hdr->rawdata = (PTR) &elf_tdata(abfd)->shstrtab_hdr;
546 for (i = 1; i < ehdr->e_shnum; i++)
548 Elf_Internal_Shdr *hdr2 = elf_elfsections(abfd)[i];
549 if (hdr2->sh_link == shindex)
551 bfd_section_from_shdr (abfd, i);
552 if (elf_onesymtab (abfd) == i)
554 elf_tdata(abfd)->strtab_hdr = *hdr;
555 elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->strtab_hdr;
558 #if 0 /* Not handling other string tables specially right now. */
559 hdr2 = elf_elfsections(abfd)[i]; /* in case it moved */
560 /* We have a strtab for some random other section. */
561 newsect = (asection *) hdr2->rawdata;
564 hdr->rawdata = (PTR) newsect;
565 hdr2 = &elf_section_data (newsect)->str_hdr;
567 elf_elfsections(abfd)[shindex] = hdr2;
573 newsect = bfd_make_section (abfd, name);
576 newsect->flags = SEC_HAS_CONTENTS;
577 hdr->rawdata = (PTR) newsect;
578 newsect->_raw_size = hdr->sh_size;
579 newsect->alignment_power = 0;
582 if (hdr->sh_flags & SHF_ALLOC)
583 newsect->flags |= SEC_ALLOC|SEC_LOAD;
584 if (!(hdr->sh_flags & SHF_WRITE))
585 newsect->flags |= SEC_READONLY;
586 if (hdr->sh_flags & SHF_EXECINSTR)
587 newsect->flags |= SEC_CODE;
589 newsect->flags |= SEC_DATA;
596 /* *These* do a lot of work -- but build no sections!
597 The spec says there can be multiple strtabs, but only one symtab,
598 but there can be lots of REL* sections. */
599 /* FIXME: The above statement is wrong! There are typically at least
600 two symbol tables in a dynamically linked executable, ".dynsym"
601 which is the dynamic linkage symbol table and ".symtab", which is
602 the "traditional" symbol table. -fnf */
605 asection *target_sect;
606 Elf_Internal_Shdr *hdr2;
607 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
609 /* Don't allow REL relocations on a machine that uses RELA and
611 /* @@ Actually, the generic ABI does suggest that both might be
612 used in one file. But the four ABI Processor Supplements I
613 have access to right now all specify that only one is used on
614 each of those architectures. It's conceivable that, e.g., a
615 bunch of absolute 32-bit relocs might be more compact in REL
616 form even on a RELA machine... */
617 BFD_ASSERT (!(use_rela_p && (hdr->sh_type == SHT_REL)));
618 BFD_ASSERT (!(!use_rela_p && (hdr->sh_type == SHT_RELA)));
619 BFD_ASSERT (hdr->sh_entsize ==
621 ? sizeof (Elf_External_Rela)
622 : sizeof (Elf_External_Rel)));
624 bfd_section_from_shdr (abfd, hdr->sh_info); /* target */
625 bfd_section_from_shdr (abfd, hdr->sh_link); /* symbol table */
626 target_sect = section_from_elf_index (abfd, hdr->sh_info);
627 if (target_sect == NULL)
630 hdr2 = &elf_section_data (target_sect)->rel_hdr;
632 elf_elfsections(abfd)[shindex] = hdr2;
633 target_sect->reloc_count = hdr->sh_size / hdr->sh_entsize;
634 target_sect->flags |= SEC_RELOC;
635 target_sect->relocation = 0;
636 target_sect->rel_filepos = hdr->sh_offset;
637 abfd->flags |= HAS_RELOC;
644 case SHT_DYNSYM: /* could treat this like symtab... */
646 fprintf (stderr, "Dynamic Linking sections not yet supported.\n");
653 fprintf (stderr, "Note Sections not yet supported.\n");
660 fprintf (stderr, "SHLIB Sections not supported (and non conforming.)\n");
672 DEFUN (elf_new_section_hook, (abfd, sec),
676 struct bfd_elf_section_data *sdata;
678 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata));
679 sec->used_by_bfd = (PTR) sdata;
680 memset (sdata, 0, sizeof (*sdata));
684 /* Create a new bfd section from an ELF program header.
686 Since program segments have no names, we generate a synthetic name
687 of the form segment<NUM>, where NUM is generally the index in the
688 program header table. For segments that are split (see below) we
689 generate the names segment<NUM>a and segment<NUM>b.
691 Note that some program segments may have a file size that is different than
692 (less than) the memory size. All this means is that at execution the
693 system must allocate the amount of memory specified by the memory size,
694 but only initialize it with the first "file size" bytes read from the
695 file. This would occur for example, with program segments consisting
696 of combined data+bss.
698 To handle the above situation, this routine generates TWO bfd sections
699 for the single program segment. The first has the length specified by
700 the file size of the segment, and the second has the length specified
701 by the difference between the two sizes. In effect, the segment is split
702 into it's initialized and uninitialized parts.
707 DEFUN (bfd_section_from_phdr, (abfd, hdr, index),
709 Elf_Internal_Phdr * hdr AND
717 split = ((hdr->p_memsz > 0) &&
718 (hdr->p_filesz > 0) &&
719 (hdr->p_memsz > hdr->p_filesz));
720 sprintf (namebuf, split ? "segment%da" : "segment%d", index);
721 name = bfd_alloc (abfd, strlen (namebuf) + 1);
722 strcpy (name, namebuf);
723 newsect = bfd_make_section (abfd, name);
724 newsect->vma = hdr->p_vaddr;
725 newsect->_raw_size = hdr->p_filesz;
726 newsect->filepos = hdr->p_offset;
727 newsect->flags |= SEC_HAS_CONTENTS;
728 if (hdr->p_type == PT_LOAD)
730 newsect->flags |= SEC_ALLOC;
731 newsect->flags |= SEC_LOAD;
732 if (hdr->p_flags & PF_X)
734 /* FIXME: all we known is that it has execute PERMISSION,
736 newsect->flags |= SEC_CODE;
739 if (!(hdr->p_flags & PF_W))
741 newsect->flags |= SEC_READONLY;
746 sprintf (namebuf, "segment%db", index);
747 name = bfd_alloc (abfd, strlen (namebuf) + 1);
748 strcpy (name, namebuf);
749 newsect = bfd_make_section (abfd, name);
750 newsect->vma = hdr->p_vaddr + hdr->p_filesz;
751 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz;
752 if (hdr->p_type == PT_LOAD)
754 newsect->flags |= SEC_ALLOC;
755 if (hdr->p_flags & PF_X)
756 newsect->flags |= SEC_CODE;
758 if (!(hdr->p_flags & PF_W))
759 newsect->flags |= SEC_READONLY;
765 /* Begin processing a given object.
767 First we validate the file by reading in the ELF header and checking
770 static INLINE boolean
771 DEFUN (elf_file_p, (x_ehdrp), Elf_External_Ehdr * x_ehdrp)
773 return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
774 && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
775 && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
776 && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
780 DEFUN (elf_object_p, (abfd), bfd * abfd)
782 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
783 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
784 Elf_External_Shdr x_shdr; /* Section header table entry, external form */
785 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
787 char *shstrtab; /* Internal copy of section header stringtab */
788 struct elf_backend_data *ebd; /* Use to get ELF_ARCH stored in xvec */
790 /* Read in the ELF header in external format. */
792 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
794 bfd_error = system_call_error;
798 /* Now check to see if we have a valid ELF file, and one that BFD can
799 make use of. The magic number must match, the address size ('class')
800 and byte-swapping must match our XVEC entry, and it must have a
801 section header table (FIXME: See comments re sections at top of this
804 if (elf_file_p (&x_ehdr) == false)
807 bfd_error = wrong_format;
811 if (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT)
814 if (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)
817 /* Switch xvec to match the specified byte order. */
818 switch (x_ehdr.e_ident[EI_DATA])
820 case ELFDATA2MSB: /* Big-endian */
821 if (!abfd->xvec->header_byteorder_big_p)
824 case ELFDATA2LSB: /* Little-endian */
825 if (abfd->xvec->header_byteorder_big_p)
828 case ELFDATANONE: /* No data encoding specified */
829 default: /* Unknown data encoding specified */
833 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
834 the tdata pointer in the bfd. */
836 if (NULL == (elf_tdata (abfd) = (struct elf_obj_tdata *)
837 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata))))
839 bfd_error = no_memory;
843 /* FIXME: Any `wrong' exits below here will leak memory (tdata). */
845 /* Now that we know the byte order, swap in the rest of the header */
846 i_ehdrp = elf_elfheader (abfd);
847 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
849 elf_debug_file (i_ehdrp);
852 /* If there is no section header table, we're hosed. */
853 if (i_ehdrp->e_shoff == 0)
856 if (i_ehdrp->e_type == ET_EXEC || i_ehdrp->e_type == ET_DYN)
857 abfd->flags |= EXEC_P;
859 /* Retrieve the architecture information from the xvec and verify
860 that it matches the machine info stored in the ELF header.
861 This allows us to resolve ambiguous formats that might not
862 otherwise be distinguishable. */
864 ebd = get_elf_backend_data (abfd);
866 /* Perhaps the elf architecture value should be another field in the
867 elf backend data? If you change this to work that way, make sure
868 that you still get bfd_arch_unknown for unknown architecture types,
869 and that it still gets accepted by the `generic' elf target. */
872 enum bfd_architecture arch = bfd_arch_unknown;
874 for (i = 0; i < bfd_elf_arch_map_size; i++)
876 if (bfd_elf_arch_map[i].elf_arch == i_ehdrp->e_machine)
878 arch = bfd_elf_arch_map[i].bfd_arch;
882 /* start-sanitize-v9 */
883 if (i_ehdrp->e_machine == EM_SPARC64)
884 arch = bfd_arch_sparc;
885 /* end-sanitize-v9 */
886 if (ebd->arch != arch)
888 bfd_default_set_arch_mach (abfd, arch, 0);
891 /* Allocate space for a copy of the section header table in
892 internal form, seek to the section header table in the file,
893 read it in, and convert it to internal form. As a simple sanity
894 check, verify that the what BFD thinks is the size of each section
895 header table entry actually matches the size recorded in the file. */
897 if (i_ehdrp->e_shentsize != sizeof (x_shdr))
899 i_shdrp = (Elf_Internal_Shdr *)
900 bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum);
901 elf_elfsections (abfd) =
902 (Elf_Internal_Shdr **) bfd_alloc (abfd, sizeof (i_shdrp) * i_ehdrp->e_shnum);
903 if (!i_shdrp || !elf_elfsections(abfd))
905 bfd_error = no_memory;
908 if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) == -1)
910 bfd_error = system_call_error;
913 for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
915 if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd)
918 bfd_error = system_call_error;
921 elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
922 elf_elfsections(abfd)[shindex] = i_shdrp + shindex;
924 if (i_ehdrp->e_shstrndx)
926 bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx);
930 for (shindex = i_ehdrp->e_shnum - 1; shindex >= 0; shindex--)
932 if (!strcmp (elf_string_from_elf_strtab (abfd,
933 i_shdrp[shindex].sh_name),
936 elf_tdata(abfd)->strtab_hdr = i_shdrp[shindex];
937 elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->strtab_hdr;
939 else if (!strcmp (elf_string_from_elf_strtab (abfd,
940 i_shdrp[shindex].sh_name),
943 elf_tdata(abfd)->symtab_hdr = i_shdrp[shindex];
944 elf_elfsections(abfd)[shindex] = &elf_tdata(abfd)->symtab_hdr;
945 elf_onesymtab (abfd) = shindex;
950 /* Read in the string table containing the names of the sections. We
951 will need the base pointer to this table later. */
952 /* We read this inline now, so that we don't have to go through
953 bfd_section_from_shdr with it (since this particular strtab is
954 used to find all of the ELF section names.) */
956 shstrtab = elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
960 /* Once all of the section headers have been read and converted, we
961 can start processing them. Note that the first section header is
962 a dummy placeholder entry, so we ignore it.
964 We also watch for the symbol table section and remember the file
965 offset and section size for both the symbol table section and the
966 associated string table section. */
968 for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
970 bfd_section_from_shdr (abfd, shindex);
973 /* Remember the entry point specified in the ELF file header. */
975 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
981 /* ELF .o/exec file writing */
983 /* Create a new ELF section from a bfd section. */
987 DEFUN (bfd_shdr_from_section, (abfd, hdr, shstrtab, indx),
989 Elf_Internal_Shdr * hdr AND
990 struct strtab *shstrtab AND
996 sect = abfd->sections;
997 for (ndx = indx; --ndx;)
1001 hdr[indx].sh_name = bfd_add_to_strtab (abfd, shstrtab,
1002 bfd_section_name (abfd, sect));
1003 hdr[indx].sh_addr = sect->vma;
1004 hdr[indx].sh_size = sect->_raw_size;
1005 hdr[indx].sh_addralign = 1 << sect->alignment_power;
1006 hdr[indx].sh_flags = 0;
1007 /* these need to be preserved on */
1008 hdr[indx].sh_link = 0;
1009 hdr[indx].sh_info = 0;
1010 hdr[indx].sh_entsize = 0;
1012 hdr[indx].sh_type = 0;
1013 if (sect->flags & SEC_RELOC)
1015 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1016 hdr[indx].sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1019 if (sect->flags & SEC_HAS_CONTENTS)
1021 hdr[indx].sh_offset = sect->filepos;
1022 hdr[indx].sh_size = sect->_raw_size;
1024 if (sect->flags & SEC_ALLOC)
1026 hdr[indx].sh_flags |= SHF_ALLOC;
1027 if (sect->flags & SEC_LOAD)
1029 /* do something with sh_type ? */
1032 if (!(sect->flags & SEC_READONLY))
1033 hdr[indx].sh_flags |= SHF_WRITE;
1035 if (sect->flags & SEC_CODE)
1036 hdr[indx].sh_flags |= SHF_EXECINSTR;
1043 Takes a bfd and a symbol, returns a pointer to the elf specific area
1044 of the symbol if there is one.
1046 static INLINE elf_symbol_type *
1047 DEFUN (elf_symbol_from, (ignore_abfd, symbol),
1048 bfd * ignore_abfd AND
1051 if (symbol->the_bfd->xvec->flavour != bfd_target_elf_flavour)
1054 if (symbol->the_bfd->tdata.elf_obj_data == (struct elf_obj_tdata *) NULL)
1057 return (elf_symbol_type *) symbol;
1061 Create ELF output from BFD sections.
1063 Essentially, just create the section header and forget about the program
1069 DEFUN (elf_make_sections, (abfd, asect, obj),
1071 asection * asect AND
1074 /* most of what is in bfd_shdr_from_section goes in here... */
1075 /* and all of these sections generate at *least* one ELF section. */
1078 Elf_Internal_Shdr *this_hdr;
1079 this_hdr = &elf_section_data (asect)->this_hdr;
1081 this_hdr->sh_addr = asect->vma;
1082 this_hdr->sh_size = asect->_raw_size;
1083 /* contents already set by elf_set_section_contents */
1085 if (asect->flags & SEC_RELOC)
1087 /* emit a reloc section, and thus strtab and symtab... */
1088 Elf_Internal_Shdr *rela_hdr;
1089 Elf_External_Rela *outbound_relocas;
1090 Elf_External_Rel *outbound_relocs;
1091 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1093 rela_hdr = &elf_section_data (asect)->rel_hdr;
1095 /* orelocation has the data, reloc_count has the count... */
1098 rela_hdr->sh_type = SHT_RELA;
1099 rela_hdr->sh_entsize = sizeof (Elf_External_Rela);
1102 /* REL relocations */
1104 rela_hdr->sh_type = SHT_REL;
1105 rela_hdr->sh_entsize = sizeof (Elf_External_Rel);
1107 rela_hdr->sh_flags = 0;
1108 rela_hdr->sh_addr = 0;
1109 rela_hdr->sh_offset = 0;
1110 rela_hdr->sh_addralign = 0;
1113 if (asect->flags & SEC_ALLOC)
1115 this_hdr->sh_flags |= SHF_ALLOC;
1116 if (asect->flags & SEC_LOAD)
1118 /* @@ Do something with sh_type? */
1121 if (!(asect->flags & SEC_READONLY))
1122 this_hdr->sh_flags |= SHF_WRITE;
1124 if (asect->flags & SEC_CODE)
1125 this_hdr->sh_flags |= SHF_EXECINSTR;
1129 write_relocs (abfd, sec, xxx)
1134 Elf_Internal_Shdr *rela_hdr;
1135 Elf_External_Rela *outbound_relocas;
1136 Elf_External_Rel *outbound_relocs;
1138 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1139 asymbol *last_sym = 0;
1142 if ((sec->flags & SEC_RELOC) == 0)
1144 /* Flags are sometimes inconsistent. */
1145 if (sec->reloc_count == 0)
1148 rela_hdr = &elf_section_data (sec)->rel_hdr;
1150 rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
1151 rela_hdr->contents = (void *) bfd_alloc (abfd, rela_hdr->sh_size);
1153 /* orelocation has the data, reloc_count has the count... */
1156 outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
1158 for (idx = 0; idx < sec->reloc_count; idx++)
1160 Elf_Internal_Rela dst_rela;
1161 Elf_External_Rela *src_rela;
1166 ptr = sec->orelocation[idx];
1167 src_rela = outbound_relocas + idx;
1168 if (!(abfd->flags & EXEC_P))
1169 dst_rela.r_offset = ptr->address - sec->vma;
1171 dst_rela.r_offset = ptr->address;
1173 sym = *ptr->sym_ptr_ptr;
1174 if (sym == last_sym)
1179 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1181 dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1183 dst_rela.r_addend = ptr->addend;
1184 elf_swap_reloca_out (abfd, &dst_rela, src_rela);
1188 /* REL relocations */
1190 outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
1192 for (idx = 0; idx < sec->reloc_count; idx++)
1194 Elf_Internal_Rel dst_rel;
1195 Elf_External_Rel *src_rel;
1200 ptr = sec->orelocation[idx];
1201 sym = *ptr->sym_ptr_ptr;
1202 src_rel = outbound_relocs + idx;
1203 if (!(abfd->flags & EXEC_P))
1204 dst_rel.r_offset = ptr->address - sec->vma;
1206 dst_rel.r_offset = ptr->address;
1208 if (sym == last_sym)
1213 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
1215 dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
1217 elf_swap_reloc_out (abfd, &dst_rel, src_rel);
1219 /* Update the addend -- FIXME add 64 bit support. */
1220 bfd_put_32 (abfd, ptr->addend,
1221 (unsigned char *) (elf_section_data (sec)->this_hdr.contents)
1222 + dst_rel.r_offset);
1228 fix_up_strtabs (abfd, asect, obj)
1233 Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr;
1234 int this_idx = elf_section_data(asect)->this_idx;
1236 /* @@ Check flags! */
1237 if (!strncmp (asect->name, ".stab", 5)
1238 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
1240 size_t len = strlen (asect->name) + 1;
1241 char *s = (char *) alloca (len);
1242 strcpy (s, asect->name);
1244 asect = bfd_get_section_by_name (abfd, s);
1247 elf_section_data(asect)->this_hdr.sh_link = this_idx;
1249 /* @@ Assuming 32 bits! */
1250 this_hdr->sh_entsize = 0xc;
1255 DEFUN (elf_fake_sections, (abfd, asect, obj),
1257 asection * asect AND
1260 /* most of what is in bfd_shdr_from_section goes in here... */
1261 /* and all of these sections generate at *least* one ELF section. */
1263 Elf_Internal_Shdr *this_hdr;
1264 this_hdr = &elf_section_data (asect)->this_hdr;
1266 bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name);
1267 /* We need to log the type *now* so that elf_section_from_bfd_section
1268 can find us... have to set rawdata too. */
1269 this_hdr->rawdata = (void *) asect;
1270 this_hdr->sh_addralign = 1 << asect->alignment_power;
1271 if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1272 this_hdr->sh_type = SHT_PROGBITS;
1273 /* @@ Select conditions correctly! */
1274 else if (!strcmp (asect->name, ".bss"))
1275 this_hdr->sh_type = SHT_NOBITS;
1277 /* what *do* we put here? */
1278 this_hdr->sh_type = SHT_PROGBITS;
1280 this_hdr->sh_flags = 0;
1281 this_hdr->sh_addr = 0;
1282 this_hdr->sh_size = 0;
1283 this_hdr->sh_entsize = 0;
1284 this_hdr->sh_info = 0;
1285 this_hdr->sh_link = 0;
1286 this_hdr->sh_offset = 0;
1290 /* Emit a strtab and symtab, and possibly a reloc section. */
1291 Elf_Internal_Shdr *rela_hdr;
1292 Elf_Internal_Shdr *symstrtab_hdr;
1294 /* Note that only one symtab is used, so just remember it
1297 if (asect->flags & SEC_RELOC)
1299 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
1301 rela_hdr = &elf_section_data (asect)->rel_hdr;
1303 bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
1304 use_rela_p ? ".rela" : ".rel",
1306 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1307 rela_hdr->sh_entsize = (use_rela_p
1308 ? sizeof (Elf_External_Rela)
1309 : sizeof (Elf_External_Rel));
1311 rela_hdr->sh_flags = 0;
1312 rela_hdr->sh_addr = 0;
1313 rela_hdr->sh_size = 0;
1314 rela_hdr->sh_offset = 0;
1315 rela_hdr->sh_addralign = 0;
1319 if (asect->flags & SEC_ALLOC)
1321 this_hdr->sh_flags |= SHF_ALLOC;
1322 if (asect->flags & SEC_LOAD)
1324 /* @@ Do something with sh_type? */
1327 if (!(asect->flags & SEC_READONLY))
1328 this_hdr->sh_flags |= SHF_WRITE;
1329 if (asect->flags & SEC_CODE)
1330 this_hdr->sh_flags |= SHF_EXECINSTR;
1335 xxxINTERNAL_FUNCTION
1339 struct elf_internal_shdr *bfd_elf_locate_sh (bfd *abfd,
1340 struct strtab *strtab,
1341 struct elf_internal_shdr *shdrp,
1345 Helper function to locate an ELF section header given the
1346 name of a BFD section.
1349 static struct elfNAME (internal_shdr) *
1350 DEFUN (elf_locate_sh, (abfd, strtab, shdrp, name),
1352 struct strtab *strtab AND
1353 struct elfNAME (internal_shdr) *shdrp AND
1356 Elf_Internal_Shdr *gotit = NULL;
1359 if (shdrp != NULL && strtab != NULL)
1361 max = elf_elfheader (abfd)->e_shnum;
1362 for (i = 1; i < max; i++)
1364 if (!strcmp (strtab->tab + shdrp[i].sh_name, name))
1373 /* Map symbol from it's internal number to the external number, moving
1374 all local symbols to be at the head of the list. */
1380 if (sym->flags & BSF_GLOBAL)
1382 if (sym->flags & BSF_LOCAL)
1386 if (sym->section == &bfd_und_section)
1388 if (bfd_is_com_section (sym->section))
1390 if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE))
1396 DEFUN (elf_map_symbols, (abfd), bfd * abfd)
1398 int symcount = bfd_get_symcount (abfd);
1399 asymbol **syms = bfd_get_outsymbols (abfd);
1401 int num_globals = 0;
1402 int num_locals2 = 0;
1403 int num_globals2 = 0;
1404 int num_sections = 0;
1410 fprintf (stderr, "elf_map_symbols\n");
1414 /* Add local symbols for each allocated section
1415 FIXME -- we should only put out symbols for sections that
1416 are actually relocated against. */
1417 for (asect = abfd->sections; asect; asect = asect->next)
1419 if (/*asect->flags & (SEC_LOAD | SEC_DATA | SEC_CODE)*/1)
1426 syms = (asymbol **) bfd_realloc (abfd, syms,
1427 ((symcount + num_sections + 1)
1428 * sizeof (asymbol *)));
1430 syms = (asymbol **) bfd_alloc (abfd,
1431 (num_sections + 1) * sizeof (asymbol *));
1433 for (asect = abfd->sections; asect; asect = asect->next)
1435 if (/* asect->flags & (SEC_LOAD | SEC_DATA | SEC_CODE) */ 1)
1437 asymbol *sym = syms[symcount++] = bfd_make_empty_symbol (abfd);
1438 sym->the_bfd = abfd;
1439 sym->name = asect->name;
1440 sym->value = asect->vma;
1441 sym->flags = BSF_SECTION_SYM;
1442 sym->section = asect;
1446 syms[symcount] = (asymbol *) 0;
1447 bfd_set_symtab (abfd, syms, symcount);
1450 elf_symtab_map (abfd) = symtab_map
1451 = (int *) bfd_alloc (abfd, symcount * sizeof (int *));
1453 /* Identify and classify all of the symbols. */
1454 for (idx = 0; idx < symcount; idx++)
1456 if (!sym_is_global (syms[idx]))
1462 /* Now provide mapping information. Add +1 for skipping over the
1464 for (idx = 0; idx < symcount; idx++)
1466 if (!sym_is_global (syms[idx]))
1467 symtab_map[idx] = 1 + num_locals2++;
1469 symtab_map[idx] = 1 + num_locals + num_globals2++;
1472 elf_num_locals (abfd) = num_locals;
1473 elf_num_globals (abfd) = num_globals;
1476 static void assign_section_numbers ();
1477 static void assign_file_positions_except_relocs ();
1480 DEFUN (elf_compute_section_file_positions, (abfd), bfd * abfd)
1482 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1483 Elf_Internal_Shdr *i_shdrp; /* Section header table, internal form */
1484 struct strtab *shstrtab;
1485 int count, maxsections;
1487 bfd_map_over_sections (abfd, elf_fake_sections, 0);
1489 assign_section_numbers (abfd);
1491 bfd_map_over_sections (abfd, elf_make_sections, 0);
1493 bfd_map_over_sections (abfd, fix_up_strtabs, 0); /* .stab/.stabstr &c */
1495 swap_out_syms (abfd);
1497 assign_file_positions_except_relocs (abfd);
1503 DEFUN (elf_write_phdrs, (abfd, i_ehdrp, i_phdrp, phdr_cnt),
1505 Elf_Internal_Ehdr * i_ehdrp AND
1506 Elf_Internal_Phdr * i_phdrp AND
1507 Elf32_Half phdr_cnt)
1509 /* first program header entry goes after the file header */
1510 int outbase = i_ehdrp->e_phoff;
1512 Elf_External_Phdr x_phdr;
1514 for (i = 0; i < phdr_cnt; i++)
1516 elf_swap_phdr_out (abfd, i_phdrp + i, &x_phdr);
1517 bfd_seek (abfd, outbase, SEEK_SET);
1518 bfd_write ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd);
1519 outbase += sizeof (x_phdr);
1526 static Elf_Internal_Phdr *
1527 DEFUN (elf_build_phdrs, (abfd, i_ehdrp, i_shdrp, phdr_cnt),
1529 Elf_Internal_Ehdr * i_ehdrp AND
1530 Elf_Internal_Shdr * i_shdrp AND
1531 Elf32_Half * phdr_cnt)
1533 Elf_Internal_Phdr *phdr_buf;
1536 1. The program header table is *not* loaded as part
1537 of the memory image of the program. If this
1538 changes later, the PT_PHDR entry must come first.
1539 2. there is currently no support for program header
1540 entries of type PT_PHDR, PT_DYNAMIC, PT_INTERP,
1543 /* A. Figure out how many program header table entries are needed
1544 1. PT_LOAD for the text segment
1545 2. PT_LOAD for the data segment
1546 Then, reserve space for one more pointer. This will be NULL
1547 to indicate the end of the program header table. */
1549 #ifdef PHDRS_INCLUDED
1552 /* XXX right now, execve() expects exactly 3 PT entries on HPPA-OSF. */
1556 phdr_buf = (Elf_Internal_Phdr *) bfd_xmalloc (((*phdr_cnt) + 1)
1558 sizeof (Elf_Internal_Phdr));
1561 #ifdef PHDRS_INCLUDED
1562 /* B. Fill in the PT_PHDR entry. */
1567 /* C. Fill in the PT_LOAD entry for the text segment. */
1569 phdr_buf[idx].p_type = PT_LOAD;
1571 /* get virtual/physical address from .text section */
1572 phdr_buf[idx].p_vaddr = bfd_get_section_by_name (abfd, ".text")->vma;
1573 phdr_buf[idx].p_paddr = 0; /* XXX */
1575 /* Ultimately, we would like the size of the .text load
1576 segment to be the sum of the following sections:
1577 the program header table itself
1588 But, right now, it will be the sum of the following sections:
1593 static char *CONST ld_sect_names[] =
1594 {".text", ".rodata", NULL};
1598 for (i = 0; ld_sect_names[i]; i++)
1600 asection *asect = bfd_get_section_by_name (abfd,
1604 ld_size += bfd_section_size (abfd, asect);
1606 phdr_buf[idx].p_filesz = ld_size;
1607 /* XXX: need to fix this */
1608 phdr_buf[idx].p_memsz = ld_size;
1610 phdr_buf[idx].p_flags = PF_R + PF_X;
1611 phdr_buf[idx].p_align =
1612 bfd_get_section_by_name (abfd, ".text")->alignment_power;
1616 /* D. Fill in the PT_LOAD entry for the data segment. */
1618 phdr_buf[idx].p_type = PT_LOAD;
1620 /* get virtual/physical address from .data section */
1621 phdr_buf[idx].p_vaddr = bfd_get_section_by_name (abfd, ".data")->vma;
1622 phdr_buf[idx].p_paddr = 0; /* XXX */
1624 /* Ultimately, we would like the size of the data load segment
1625 to be the sum of the following sections:
1626 the PT_DYNAMIC program header table entry
1632 But, right now, it will be the sum of the following sections:
1636 static char *CONST ld_sect_names[] =
1641 for (i = 0; ld_sect_names[i]; i++)
1643 asection *asect = bfd_get_section_by_name (abfd,
1647 ld_size += bfd_section_size (abfd, asect);
1649 phdr_buf[idx].p_filesz = ld_size;
1650 /* XXX: need to fix this */
1651 phdr_buf[idx].p_memsz = ld_size;
1653 phdr_buf[idx].p_flags = PF_R + PF_W + PF_X;
1654 phdr_buf[idx].p_align
1655 = bfd_get_section_by_name (abfd, ".data")->alignment_power;
1659 /* E. Fill in the PT_LOAD entry for the bss segment. */
1661 phdr_buf[idx].p_type = PT_LOAD;
1663 /* get virtual/physical address from .data section */
1664 phdr_buf[idx].p_vaddr = bfd_get_section_by_name (abfd, ".bss")->vma;
1665 phdr_buf[idx].p_paddr = 0; /* XXX */
1668 static char *CONST ld_sect_names[] =
1673 for (i = 0; ld_sect_names[i]; i++)
1675 asection *asect = bfd_get_section_by_name (abfd,
1679 ld_size += bfd_section_size (abfd, asect);
1681 phdr_buf[idx].p_filesz = 0;
1682 /* XXX: need to fix this */
1683 phdr_buf[idx].p_memsz = ld_size;
1685 phdr_buf[idx].p_flags = PF_R + PF_W + PF_X;
1686 phdr_buf[idx].p_align
1687 = bfd_get_section_by_name (abfd, ".bss")->alignment_power;
1691 /* F. Set up the "end of program header table" sentinel. */
1693 memset ((char *) (phdr_buf + idx), 0, sizeof (Elf_Internal_Phdr));
1696 BFD_ASSERT (idx - 1 == *phdr_cnt);
1702 static const Elf_Internal_Shdr null_shdr;
1704 /* Assign all ELF section numbers. The dummy first section is handled here
1705 too. The link/info pointers for the standard section types are filled
1706 in here too, while we're at it. (Link pointers for .stab sections are
1707 not filled in here.) */
1709 assign_section_numbers (abfd)
1712 struct elf_obj_tdata *t = elf_tdata (abfd);
1714 int section_number = 1;
1716 Elf_Internal_Shdr **i_shdrp;
1718 t->shstrtab_hdr.sh_size = elf_shstrtab(abfd)->length;
1719 t->shstrtab_hdr.contents = (void *) elf_shstrtab(abfd)->tab;
1720 shstrtab_length_fixed = 1;
1722 t->shstrtab_section = section_number++;
1723 elf_elfheader(abfd)->e_shstrndx = t->shstrtab_section;
1726 t->symtab_section = section_number++;
1727 t->strtab_section = section_number++;
1728 t->symtab_hdr.sh_link = t->strtab_section;
1730 for (sec = abfd->sections; sec; sec = sec->next)
1732 struct bfd_elf_section_data *d = elf_section_data (sec);
1733 d->this_idx = section_number++;
1734 if (sec->flags & SEC_RELOC)
1736 d->rel_idx = section_number++;
1737 d->rel_hdr.sh_link = t->symtab_section;
1738 d->rel_hdr.sh_info = d->this_idx;
1742 /* No handling for per-section string tables currently. */
1744 elf_elfheader(abfd)->e_shnum = section_number;
1746 /* Set up the list of section header pointers, in agreement with the
1748 i_shdrp = (Elf_Internal_Shdr **)
1749 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *));
1750 elf_elfsections(abfd) = i_shdrp;
1751 for (i = 0; i < section_number; i++)
1754 i_shdrp[0] = (Elf_Internal_Shdr *) &null_shdr;
1755 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1758 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1759 i_shdrp[t->strtab_section] = &t->strtab_hdr;
1761 for (sec = abfd->sections; sec; sec = sec->next)
1763 struct bfd_elf_section_data *d = elf_section_data (sec);
1764 i_shdrp[d->this_idx] = &d->this_hdr;
1766 i_shdrp[d->rel_idx] = &d->rel_hdr;
1768 /* Make sure we got everything.... */
1769 for (i = 0; i < section_number; i++)
1770 if (i_shdrp[i] == 0)
1774 static INLINE file_ptr
1775 assign_file_position_for_section (i_shdrp, offset)
1776 Elf_Internal_Shdr *i_shdrp;
1779 i_shdrp->sh_offset = offset;
1780 if (i_shdrp->sh_type != SHT_NOBITS)
1781 offset += i_shdrp->sh_size;
1785 static INLINE file_ptr
1786 assign_file_positions_for_symtab_and_strtabs (abfd, off)
1790 struct elf_obj_tdata *t = elf_tdata (abfd);
1792 off = assign_file_position_for_section (&t->shstrtab_hdr, off);
1793 off = assign_file_position_for_section (&t->symtab_hdr, off);
1794 off = assign_file_position_for_section (&t->strtab_hdr, off);
1799 bfd_vma low, mem_size;
1803 struct seg_info *next;
1807 map_program_segments (abfd)
1810 Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
1811 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1812 Elf_Internal_Shdr *i_shdrp;
1813 Elf_Internal_Phdr *phdr;
1816 file_ptr lowest_offset = 0;
1817 struct seg_info *seg = 0;
1819 done = (char *) alloca (i_ehdrp->e_shnum);
1820 memset (done, 0, i_ehdrp->e_shnum);
1821 for (i = 0; i < i_ehdrp->e_shnum; i++)
1823 i_shdrp = i_shdrpp[i];
1824 /* If it's going to be mapped in, it's been assigned a position. */
1825 if (i_shdrp->sh_offset + 1 == 0)
1827 /* Well, not really, but we won't process it here. */
1831 if (i_shdrp->sh_offset < lowest_offset
1832 || lowest_offset == 0)
1833 lowest_offset = i_shdrp->sh_offset;
1834 /* Only interested in PROGBITS or NOBITS for generating segments. */
1835 switch (i_shdrp->sh_type)
1848 bfd_vma lowest_vma = -1, high;
1853 for (i = 1; i < i_ehdrp->e_shnum; i++)
1855 i_shdrp = i_shdrpp[i];
1856 if (!done[i] && i_shdrp->sh_addr < lowest_vma)
1858 lowest_vma = i_shdrp->sh_addr;
1864 /* So now we know the lowest vma of any unassigned sections; start
1868 s = (struct seg_info *) bfd_alloc (abfd, sizeof (struct seg_info));
1872 seg->low = lowest_vma;
1873 i_shdrp = i_shdrpp[low_sec];
1874 seg->start_pos = i_shdrp->sh_offset;
1875 seg->sh_flags = i_shdrp->sh_flags;
1876 done[low_sec] = 1, n_left--;
1877 mem_size = i_shdrp->sh_size;
1878 high = lowest_vma + i_shdrp->sh_size;
1880 if (i_shdrp->sh_type == SHT_PROGBITS)
1881 file_size = i_shdrp->sh_size;
1883 for (i = 0; i < i_ehdrp->e_shnum; i++)
1887 if (file_size != mem_size)
1891 i_shdrp = i_shdrpp[i];
1892 /* position of next byte on disk */
1893 f1 = seg->start_pos + file_size;
1894 if (i_shdrp->sh_type == SHT_PROGBITS)
1896 if (i_shdrp->sh_offset - f1 != i_shdrp->sh_addr - high)
1899 else /* sh_type == NOBITS */
1901 /* If the section in question has no contents in the disk
1902 file, we really don't care where it supposedly starts.
1903 But we don't want to bother merging it into this segment
1904 if it doesn't start on this memory page. */
1905 bfd_vma page1, page2;
1906 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1908 /* page number in address space of current end of seg */
1909 page1 = (high - 1 + maxpagesize - 1) / maxpagesize;
1910 /* page number in address space of start of this section */
1911 page2 = (i_shdrp->sh_addr + maxpagesize - 1) / maxpagesize;
1916 done[i] = 1, n_left--;
1917 if (i_shdrp->sh_type == SHT_PROGBITS)
1918 file_size = i_shdrp->sh_offset + i_shdrp->sh_size - seg->start_pos;
1919 mem_size = i_shdrp->sh_addr + i_shdrp->sh_size - seg->low;
1920 high = i_shdrp->sh_addr + i_shdrp->sh_size;
1923 seg->file_size = file_size;
1924 seg->mem_size = mem_size;
1926 /* Now do something with the list of segments we've built up. */
1928 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1933 for (s = seg; s; s = s->next)
1937 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
1938 sz = sizeof (Elf_External_Phdr) * n_segs;
1939 if (i_ehdrp->e_ehsize + sz <= lowest_offset)
1940 i_ehdrp->e_phoff = i_ehdrp->e_ehsize;
1943 i_ehdrp->e_phoff = elf_tdata (abfd)->next_file_pos;
1944 elf_tdata (abfd)->next_file_pos += sz;
1946 phdr = (Elf_Internal_Phdr*) bfd_alloc (abfd,
1947 n_segs * sizeof (Elf_Internal_Phdr));
1948 elf_tdata (abfd)->phdr = phdr;
1951 phdr->p_type = PT_LOAD; /* only type we really support so far */
1952 phdr->p_offset = seg->start_pos;
1953 phdr->p_vaddr = seg->low;
1955 phdr->p_filesz = seg->file_size;
1956 phdr->p_memsz = seg->mem_size;
1957 phdr->p_flags = PF_R;
1958 phdr->p_align = maxpagesize; /* ? */
1959 if (seg->sh_flags & SHF_WRITE)
1960 phdr->p_flags |= PF_W;
1961 if (seg->sh_flags & SHF_EXECINSTR)
1962 phdr->p_flags |= PF_X;
1966 i_ehdrp->e_phnum = n_segs;
1968 elf_write_phdrs (abfd, i_ehdrp, elf_tdata (abfd)->phdr, i_ehdrp->e_phnum);
1972 assign_file_positions_except_relocs (abfd)
1975 /* For now, we ignore the possibility of having program segments, which
1976 may require some alignment in the file. That'll require padding, and
1977 some interesting calculations to optimize file space usage.
1979 Also, since the application may change the list of relocations for
1980 a given section, we don't figure them in here. We'll put them at the
1981 end of the file, at positions computed during bfd_close.
1983 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1984 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
1988 Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
1989 Elf_Internal_Shdr *i_shdrp;
1990 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1991 int exec_p = (abfd->flags & EXEC_P) != 0;
1993 /* Everything starts after the ELF file header. */
1994 off = i_ehdrp->e_ehsize;
1998 /* Section headers. */
1999 i_ehdrp->e_shoff = off;
2000 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2002 off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
2004 for (i = 0; i < i_ehdrp->e_shnum; i++)
2006 i_shdrp = i_shdrpp[i];
2007 if (i_shdrp->sh_type == SHT_REL || i_shdrp->sh_type == SHT_RELA)
2009 i_shdrp->sh_offset = -1;
2014 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
2015 if (maxpagesize == 0)
2016 maxpagesize = 1; /* make the arithmetic work */
2017 /* This isn't necessarily going to give the best packing, if the
2018 segments require padding between them, but since that isn't
2019 usually the case, this'll do. */
2020 if ((i_shdrp->sh_flags & SHF_ALLOC) == 0)
2022 i_shdrp->sh_offset = -1;
2025 /* Blindly assume that the segments are ordered optimally. With
2026 the default LD script, they will be. */
2028 /* need big unsigned type */
2030 addtl_off = i_shdrp->sh_addr - off;
2031 addtl_off = addtl_off % maxpagesize;
2037 if (i_shdrp->sh_type == SHT_NOBITS)
2040 i_shdrp->sh_offset = off;
2041 if (off % maxpagesize != 0)
2042 off2 = maxpagesize - (off % maxpagesize);
2043 if (off2 > i_shdrp->sh_size)
2044 off2 = i_shdrp->sh_size;
2048 off = assign_file_position_for_section (i_shdrp, off);
2050 && get_elf_backend_data(abfd)->maxpagesize > 1
2051 && i_shdrp->sh_type == SHT_PROGBITS
2052 && (i_shdrp->sh_flags & SHF_ALLOC)
2053 && (i_shdrp->sh_offset - i_shdrp->sh_addr) % get_elf_backend_data(abfd)->maxpagesize != 0)
2058 elf_tdata (abfd)->next_file_pos = off;
2059 map_program_segments (abfd);
2060 off = elf_tdata (abfd)->next_file_pos;
2062 /* Section headers. */
2063 i_ehdrp->e_shoff = off;
2064 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
2066 off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
2068 for (i = 0; i < i_ehdrp->e_shnum; i++)
2070 i_shdrp = i_shdrpp[i];
2071 if (i_shdrp->sh_offset + 1 == 0
2072 && i_shdrp->sh_type != SHT_REL
2073 && i_shdrp->sh_type != SHT_RELA)
2074 off = assign_file_position_for_section (i_shdrp, off);
2077 elf_tdata (abfd)->next_file_pos = off;
2084 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
2085 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2086 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2087 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
2088 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2092 struct strtab *shstrtab;
2094 i_ehdrp = elf_elfheader (abfd);
2095 i_shdrp = elf_elfsections (abfd);
2097 shstrtab = bfd_new_strtab (abfd);
2098 elf_shstrtab (abfd) = shstrtab;
2100 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2101 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2102 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2103 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
2105 i_ehdrp->e_ident[EI_CLASS] = ELFCLASS;
2106 i_ehdrp->e_ident[EI_DATA] =
2107 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2108 i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
2110 for (count = EI_PAD; count < EI_NIDENT; count++)
2111 i_ehdrp->e_ident[count] = 0;
2113 i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL;
2114 switch (bfd_get_arch (abfd))
2116 case bfd_arch_unknown:
2117 i_ehdrp->e_machine = EM_NONE;
2119 case bfd_arch_sparc:
2120 i_ehdrp->e_machine = EM_SPARC;
2121 /* start-sanitize-v9 */
2123 i_ehdrp->e_machine = EM_SPARC64;
2125 /* end-sanitize-v9 */
2128 i_ehdrp->e_machine = EM_386;
2131 i_ehdrp->e_machine = EM_68K;
2134 i_ehdrp->e_machine = EM_88K;
2137 i_ehdrp->e_machine = EM_860;
2139 case bfd_arch_mips: /* MIPS Rxxxx */
2140 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2143 i_ehdrp->e_machine = EM_HPPA;
2145 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2147 i_ehdrp->e_machine = EM_NONE;
2149 i_ehdrp->e_version = EV_CURRENT;
2150 i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr);
2152 /* no program header, for now. */
2153 i_ehdrp->e_phoff = 0;
2154 i_ehdrp->e_phentsize = 0;
2155 i_ehdrp->e_phnum = 0;
2157 /* each bfd section is section header entry */
2158 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2159 i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
2161 /* if we're building an executable, we'll need a program header table */
2162 if (abfd->flags & EXEC_P)
2164 /* it all happens later */
2166 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
2168 /* elf_build_phdrs() returns a (NULL-terminated) array of
2169 Elf_Internal_Phdrs */
2170 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2171 i_ehdrp->e_phoff = outbase;
2172 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2177 i_ehdrp->e_phentsize = 0;
2179 i_ehdrp->e_phoff = 0;
2182 elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2184 elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2186 elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2192 swap_out_syms (abfd)
2195 struct strtab *shstrtab = elf_shstrtab (abfd);
2197 elf_map_symbols (abfd);
2199 /* Dump out the symtabs. */
2201 int symcount = bfd_get_symcount (abfd);
2202 asymbol **syms = bfd_get_outsymbols (abfd);
2203 struct strtab *stt = bfd_new_strtab (abfd);
2204 Elf_Internal_Shdr *symtab_hdr;
2205 Elf_Internal_Shdr *symstrtab_hdr;
2206 Elf_External_Sym *outbound_syms;
2209 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2210 symtab_hdr->sh_type = SHT_SYMTAB;
2211 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
2212 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2213 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
2215 /* see assert in elf_fake_sections that supports this: */
2216 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2217 symstrtab_hdr->sh_type = SHT_STRTAB;
2219 outbound_syms = (Elf_External_Sym *)
2220 bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym));
2221 /* now generate the data (for "contents") */
2223 /* Fill in zeroth symbol and swap it out. */
2224 Elf_Internal_Sym sym;
2230 sym.st_shndx = SHN_UNDEF;
2231 elf_swap_symbol_out (abfd, &sym, outbound_syms);
2233 for (idx = 0; idx < symcount; idx++)
2235 Elf_Internal_Sym sym;
2236 bfd_vma value = syms[idx]->value;
2238 if (syms[idx]->flags & BSF_SECTION_SYM)
2239 /* Section symbols have no names. */
2242 sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
2244 if (bfd_is_com_section (syms[idx]->section))
2246 /* ELF common symbols put the alignment into the `value' field,
2247 and the size into the `size' field. This is backwards from
2248 how BFD handles it, so reverse it here. */
2249 sym.st_size = value;
2250 /* Should retrieve this from somewhere... */
2252 sym.st_shndx = SHN_COMMON;
2256 asection *sec = syms[idx]->section;
2257 elf_symbol_type *type_ptr;
2260 if (sec->output_section)
2262 value += sec->output_offset;
2263 sec = sec->output_section;
2266 sym.st_value = value;
2267 type_ptr = elf_symbol_from (abfd, syms[idx]);
2268 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
2269 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec);
2273 /* Writing this would be a hell of a lot easier if we had
2274 some decent documentation on bfd, and knew what to expect
2275 of the library, and what to demand of applications. For
2276 example, it appears that `objcopy' might not set the
2277 section of a symbol to be a section that is actually in
2279 sec2 = bfd_get_section_by_name (abfd, sec->name);
2281 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2);
2282 assert (shndx != -1);
2286 if (bfd_is_com_section (syms[idx]->section))
2287 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE);
2288 else if (syms[idx]->section == &bfd_und_section)
2289 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE);
2290 else if (syms[idx]->flags & BSF_WEAK)
2291 sym.st_info = ELF_ST_INFO (STB_WEAK, STT_OBJECT);
2292 else if (syms[idx]->flags & BSF_SECTION_SYM)
2293 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2294 else if (syms[idx]->flags & BSF_FILE)
2295 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
2296 else if (syms[idx]->flags & (BSF_GLOBAL | BSF_EXPORT))
2298 if (syms[idx]->flags & BSF_FUNCTION)
2299 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_FUNC);
2301 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
2303 else if (syms[idx]->flags & BSF_LOCAL)
2305 if (syms[idx]->flags & BSF_FUNCTION)
2306 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
2308 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_OBJECT);
2311 /* Default to local if flag isn't set at all. */
2312 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_OBJECT);
2315 elf_swap_symbol_out (abfd, &sym,
2316 outbound_syms + elf_symtab_map (abfd)[idx]);
2319 symtab_hdr->contents = (PTR) outbound_syms;
2320 symstrtab_hdr->contents = (PTR) stt->tab;
2321 symstrtab_hdr->sh_size = stt->length;
2322 symstrtab_hdr->sh_type = SHT_STRTAB;
2324 symstrtab_hdr->sh_flags = 0;
2325 symstrtab_hdr->sh_addr = 0;
2326 symstrtab_hdr->sh_entsize = 0;
2327 symstrtab_hdr->sh_link = 0;
2328 symstrtab_hdr->sh_info = 0;
2329 symstrtab_hdr->sh_addralign = 0;
2330 symstrtab_hdr->size = 0;
2333 /* put the strtab out too... */
2335 Elf_Internal_Shdr *this_hdr;
2337 this_hdr = &elf_tdata(abfd)->shstrtab_hdr;
2338 this_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
2339 this_hdr->sh_size = elf_shstrtab (abfd)->length;
2340 this_hdr->sh_type = SHT_STRTAB;
2341 this_hdr->sh_flags = 0;
2342 this_hdr->sh_addr = 0;
2343 this_hdr->sh_entsize = 0;
2344 this_hdr->sh_addralign = 0;
2350 write_shdrs_and_ehdr (abfd)
2353 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
2354 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
2355 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
2356 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
2357 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
2361 struct strtab *shstrtab;
2363 i_ehdrp = elf_elfheader (abfd);
2364 i_shdrp = elf_elfsections (abfd);
2365 shstrtab = elf_shstrtab (abfd);
2367 /* swap the header before spitting it out... */
2370 elf_debug_file (i_ehdrp);
2372 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
2373 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
2374 bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd);
2376 /* at this point we've concocted all the ELF sections... */
2377 x_shdrp = (Elf_External_Shdr *)
2378 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
2381 bfd_error = no_memory;
2385 for (count = 0; count < i_ehdrp->e_shnum; count++)
2388 elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
2391 elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
2393 bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET);
2394 bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd);
2395 /* need to dump the string table too... */
2401 assign_file_positions_for_relocs (abfd)
2404 file_ptr off = elf_tdata(abfd)->next_file_pos;
2406 Elf_Internal_Shdr **shdrpp = elf_elfsections (abfd);
2407 Elf_Internal_Shdr *shdrp;
2408 for (i = 0; i < elf_elfheader(abfd)->e_shnum; i++)
2411 if (shdrp->sh_type != SHT_REL && shdrp->sh_type != SHT_RELA)
2413 off = assign_file_position_for_section (shdrp, off);
2415 elf_tdata(abfd)->next_file_pos = off;
2419 DEFUN (NAME(bfd_elf,write_object_contents), (abfd), bfd * abfd)
2421 Elf_Internal_Ehdr *i_ehdrp;
2422 Elf_Internal_Shdr **i_shdrp;
2425 if (abfd->output_has_begun == false)
2427 prep_headers (abfd);
2428 elf_compute_section_file_positions (abfd);
2429 abfd->output_has_begun = true;
2432 i_shdrp = elf_elfsections (abfd);
2433 i_ehdrp = elf_elfheader (abfd);
2435 bfd_map_over_sections (abfd, write_relocs, (PTR) 0);
2436 assign_file_positions_for_relocs (abfd);
2438 /* After writing the headers, we need to write the sections too... */
2439 for (count = 0; count < i_ehdrp->e_shnum; count++)
2440 if (i_shdrp[count]->contents)
2442 bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET);
2443 bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, 1, abfd);
2445 return write_shdrs_and_ehdr (abfd);
2448 /* Given an index of a section, retrieve a pointer to it. Note
2449 that for our purposes, sections are indexed by {1, 2, ...} with
2450 0 being an illegal index. */
2452 /* In the original, each ELF section went into exactly one BFD
2453 section. This doesn't really make sense, so we need a real mapping.
2454 The mapping has to hide in the Elf_Internal_Shdr since asection
2455 doesn't have anything like a tdata field... */
2458 DEFUN (section_from_elf_index, (abfd, index),
2462 /* @@ Is bfd_com_section really correct in all the places it could
2463 be returned from this routine? */
2465 if (index == SHN_ABS)
2466 return &bfd_com_section; /* not abs? */
2467 if (index == SHN_COMMON)
2468 return &bfd_com_section;
2470 if (index > elf_elfheader (abfd)->e_shnum)
2474 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index];
2476 switch (hdr->sh_type)
2478 /* ELF sections that map to BFD sections */
2482 bfd_section_from_shdr (abfd, index);
2483 return (struct sec *) hdr->rawdata;
2486 return (struct sec *) &bfd_abs_section;
2491 /* given a section, search the header to find them... */
2493 DEFUN (elf_section_from_bfd_section, (abfd, asect),
2497 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2499 Elf_Internal_Shdr *hdr;
2500 int maxindex = elf_elfheader (abfd)->e_shnum;
2502 if (asect == &bfd_abs_section)
2504 if (asect == &bfd_com_section)
2506 if (asect == &bfd_und_section)
2509 for (index = 0; index < maxindex; index++)
2511 hdr = i_shdrp[index];
2512 switch (hdr->sh_type)
2514 /* ELF sections that map to BFD sections */
2519 if (((struct sec *) (hdr->rawdata)) == asect)
2530 /* given a symbol, return the bfd index for that symbol. */
2532 DEFUN (elf_symbol_from_bfd_symbol, (abfd, asym_ptr_ptr),
2534 struct symbol_cache_entry **asym_ptr_ptr)
2536 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
2537 CONST char *name = asym_ptr->name;
2539 int symcount = bfd_get_symcount (abfd);
2540 asymbol **syms = bfd_get_outsymbols (abfd);
2542 /* FIXME -- there has to be a better way than linear search. */
2543 for (idx = 0; idx < symcount; idx++)
2545 if (syms[idx] == asym_ptr
2546 || (name == syms[idx]->name && name)
2547 || ((asym_ptr->flags & BSF_SECTION_SYM)
2548 && (syms[idx]->flags & BSF_SECTION_SYM)
2549 && asym_ptr->section == syms[idx]->section))
2553 if (idx >= symcount)
2556 fprintf (stderr, "bfd app err: can't find sym `%s' in symtab\n",
2560 idx = elf_symtab_map (abfd)[idx];
2564 flagword flags = asym_ptr->flags;
2567 "elfsym<-bfdsym %.8lx `%s' sec=%s symnum=%d {",
2568 (long) asym_ptr, asym_ptr->name, asym_ptr->section->name, idx);
2570 if (flags == BSF_NO_FLAGS)
2571 fprintf (stderr, " none");
2573 if (flags & BSF_LOCAL)
2574 fprintf (stderr, " local");
2576 if (flags & BSF_GLOBAL)
2577 fprintf (stderr, " global");
2579 if (flags & BSF_EXPORT)
2580 fprintf (stderr, " export");
2582 if (flags & BSF_DEBUGGING)
2583 fprintf (stderr, " debugging");
2585 if (flags & BSF_KEEP)
2586 fprintf (stderr, " keep");
2588 if (flags & BSF_KEEP_G)
2589 fprintf (stderr, " keep_g");
2591 if (flags & BSF_WEAK)
2592 fprintf (stderr, " weak");
2594 if (flags & BSF_SECTION_SYM)
2595 fprintf (stderr, " section_sym");
2597 if (flags & BSF_OLD_COMMON)
2598 fprintf (stderr, " old_common");
2600 if (flags & BSF_NOT_AT_END)
2601 fprintf (stderr, " not_at_end");
2603 if (flags & BSF_CONSTRUCTOR)
2604 fprintf (stderr, " constructor");
2606 if (flags & BSF_WARNING)
2607 fprintf (stderr, " warning");
2609 if (flags & BSF_INDIRECT)
2610 fprintf (stderr, " indirect");
2612 if (flags & BSF_FILE)
2613 fprintf (stderr, " file");
2615 if (flags & BSF_FUNCTION)
2616 fprintf (stderr, " function");
2618 fputs (" }\n", stderr);
2627 DEFUN (elf_slurp_symbol_table, (abfd, symptrs),
2629 asymbol ** symptrs) /* Buffer for generated bfd symbols */
2631 Elf_Internal_Shdr *hdr = &elf_tdata(abfd)->symtab_hdr;
2632 int symcount; /* Number of external ELF symbols */
2634 elf_symbol_type *sym; /* Pointer to current bfd symbol */
2635 elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
2636 Elf_Internal_Sym i_sym;
2637 Elf_External_Sym *x_symp;
2639 /* this is only valid because there is only one symtab... */
2640 /* FIXME: This is incorrect, there may also be a dynamic symbol
2641 table which is a subset of the full symbol table. We either need
2642 to be prepared to read both (and merge them) or ensure that we
2643 only read the full symbol table. Currently we only get called to
2644 read the full symbol table. -fnf */
2645 if (bfd_get_outsymbols (abfd) != NULL)
2650 /* Read each raw ELF symbol, converting from external ELF form to
2651 internal ELF form, and then using the information to create a
2652 canonical bfd symbol table entry.
2654 Note that we allocate the initial bfd canonical symbol buffer
2655 based on a one-to-one mapping of the ELF symbols to canonical
2656 symbols. We actually use all the ELF symbols, so there will be no
2657 space left over at the end. When we have all the symbols, we
2658 build the caller's pointer vector. */
2660 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
2662 bfd_error = system_call_error;
2666 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2667 symbase = (elf_symbol_type *) bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type));
2670 /* Temporarily allocate room for the raw ELF symbols. */
2671 x_symp = (Elf_External_Sym *) bfd_xmalloc (symcount * sizeof (Elf_External_Sym));
2673 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
2674 != symcount * sizeof (Elf_External_Sym))
2676 free ((PTR) x_symp);
2677 bfd_error = system_call_error;
2680 /* Skip first symbol, which is a null dummy. */
2681 for (i = 1; i < symcount; i++)
2683 elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
2684 memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
2685 memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
2686 sym->symbol.the_bfd = abfd;
2688 sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link,
2691 sym->symbol.value = i_sym.st_value;
2693 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
2695 sym->symbol.section = section_from_elf_index (abfd, i_sym.st_shndx);
2697 else if (i_sym.st_shndx == SHN_ABS)
2699 sym->symbol.section = &bfd_abs_section;
2701 else if (i_sym.st_shndx == SHN_COMMON)
2703 sym->symbol.section = &bfd_com_section;
2704 /* Elf puts the alignment into the `value' field, and the size
2705 into the `size' field. BFD wants to see the size in the
2706 value field, and doesn't care (at the moment) about the
2708 sym->symbol.value = i_sym.st_size;
2710 else if (i_sym.st_shndx == SHN_UNDEF)
2712 sym->symbol.section = &bfd_und_section;
2715 sym->symbol.section = &bfd_abs_section;
2717 sym->symbol.value -= sym->symbol.section->vma;
2719 switch (ELF_ST_BIND (i_sym.st_info))
2722 sym->symbol.flags |= BSF_LOCAL;
2725 sym->symbol.flags |= (BSF_GLOBAL | BSF_EXPORT);
2728 sym->symbol.flags |= BSF_WEAK;
2732 switch (ELF_ST_TYPE (i_sym.st_info))
2735 sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
2738 sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
2741 sym->symbol.flags |= BSF_FUNCTION;
2745 /* Is this a definition of $global$? If so, keep it because it will be
2746 needd if any relocations are performed. */
2747 if (!strcmp (sym->symbol.name, "$global$")
2748 && sym->symbol.section != &bfd_und_section)
2750 /* @@ Why is this referring to backend data and not a field of
2752 struct elf_backend_data *be_data = (struct elf_backend_data *) abfd->xvec->backend_data;
2754 be_data->global_sym = (PTR) sym;
2759 /* We rely on the zalloc to clear out the final symbol entry. */
2761 /* obj_raw_syms macro uses a cast... */
2762 elf_tdata (abfd)->raw_syms = (PTR) x_symp;
2764 bfd_get_symcount (abfd) = symcount = sym - symbase;
2766 /* Fill in the user's symbol pointer vector if needed. */
2770 while (symcount-- > 0)
2772 *symptrs++ = &sym->symbol;
2775 *symptrs = 0; /* Final null pointer */
2781 /* Return the number of bytes required to hold the symtab vector.
2783 Note that we base it on the count plus 1, since we will null terminate
2784 the vector allocated based on this size. However, the ELF symbol table
2785 always has a dummy entry as symbol #0, so it ends up even. */
2788 DEFUN (elf_get_symtab_upper_bound, (abfd), bfd * abfd)
2790 unsigned int symcount;
2791 unsigned int symtab_size = 0;
2793 Elf_Internal_Shdr *hdr = &elf_tdata(abfd)->symtab_hdr;
2794 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
2795 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol));
2801 This function return the number of bytes required to store the
2802 relocation information associated with section <<sect>>
2803 attached to bfd <<abfd>>
2807 elf_get_reloc_upper_bound (abfd, asect)
2811 if (asect->flags & SEC_RELOC)
2813 /* either rel or rela */
2814 return elf_section_data(asect)->rel_hdr.sh_size;
2821 DEFUN (elf_slurp_reloca_table, (abfd, asect, symbols),
2826 Elf_External_Rela *native_relocs;
2827 arelent *reloc_cache;
2832 if (asect->relocation)
2834 if (asect->reloc_count == 0)
2836 if (asect->flags & SEC_CONSTRUCTOR)
2839 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
2840 native_relocs = (Elf_External_Rela *)
2841 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela));
2842 bfd_read ((PTR) native_relocs,
2843 sizeof (Elf_External_Rela), asect->reloc_count, abfd);
2845 reloc_cache = (arelent *)
2846 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2850 bfd_error = no_memory;
2854 for (idx = 0; idx < asect->reloc_count; idx++)
2856 #ifdef RELOC_PROCESSING
2857 Elf_Internal_Rela dst;
2858 Elf_External_Rela *src;
2860 cache_ptr = reloc_cache + idx;
2861 src = native_relocs + idx;
2862 elf_swap_reloca_in (abfd, src, &dst);
2864 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
2866 Elf_Internal_Rela dst;
2867 Elf_External_Rela *src;
2869 cache_ptr = reloc_cache + idx;
2870 src = native_relocs + idx;
2872 elf_swap_reloca_in (abfd, src, &dst);
2874 if (asect->flags & SEC_RELOC)
2876 /* relocatable, so the offset is off of the section */
2877 cache_ptr->address = dst.r_offset + asect->vma;
2881 /* non-relocatable, so the offset a virtual address */
2882 cache_ptr->address = dst.r_offset;
2884 /* ELF_R_SYM(dst.r_info) is the symbol table offset; subtract 1
2885 because the first entry is NULL. */
2886 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
2888 /* Is it an ELF section symbol? If so, translate it into a
2889 BFD section symbol. */
2890 asymbol *s = *(cache_ptr->sym_ptr_ptr);
2891 if (s->flags & BSF_SECTION_SYM)
2892 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
2894 cache_ptr->addend = dst.r_addend;
2896 /* Fill in the cache_ptr->howto field from dst.r_type */
2898 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2899 (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst);
2904 asect->relocation = reloc_cache;
2910 elf_debug_section (str, num, hdr)
2913 Elf_Internal_Shdr *hdr;
2915 fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
2917 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2918 (long) hdr->sh_name,
2919 (long) hdr->sh_type,
2920 (long) hdr->sh_flags);
2922 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2923 (long) hdr->sh_addr,
2924 (long) hdr->sh_offset,
2925 (long) hdr->sh_size);
2927 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2928 (long) hdr->sh_link,
2929 (long) hdr->sh_info,
2930 (long) hdr->sh_addralign);
2931 fprintf (stderr, "sh_entsize = %ld\n",
2932 (long) hdr->sh_entsize);
2933 fprintf (stderr, "rawdata = 0x%.8lx\n", (long) hdr->rawdata);
2934 fprintf (stderr, "contents = 0x%.8lx\n", (long) hdr->contents);
2935 fprintf (stderr, "size = %ld\n", (long) hdr->size);
2940 elf_debug_file (ehdrp)
2941 Elf_Internal_Ehdr *ehdrp;
2943 fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry);
2944 fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff);
2945 fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum);
2946 fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize);
2947 fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff);
2948 fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum);
2949 fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize);
2954 DEFUN (elf_slurp_reloc_table, (abfd, asect, symbols),
2959 Elf_External_Rel *native_relocs;
2960 arelent *reloc_cache;
2962 Elf_Internal_Shdr *data_hdr;
2963 ElfNAME (Off) data_off;
2964 ElfNAME (Word) data_max;
2965 char buf[4]; /* FIXME -- might be elf64 */
2969 if (asect->relocation)
2971 if (asect->reloc_count == 0)
2973 if (asect->flags & SEC_CONSTRUCTOR)
2976 bfd_seek (abfd, asect->rel_filepos, SEEK_SET);
2977 native_relocs = (Elf_External_Rel *)
2978 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel));
2979 bfd_read ((PTR) native_relocs,
2980 sizeof (Elf_External_Rel), asect->reloc_count, abfd);
2982 reloc_cache = (arelent *)
2983 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2987 bfd_error = no_memory;
2991 /* Get the offset of the start of the segment we are relocating to read in
2992 the implicit addend. */
2993 data_hdr = &elf_section_data(asect)->this_hdr;
2994 data_off = data_hdr->sh_offset;
2995 data_max = data_hdr->sh_size - sizeof (buf) + 1;
2998 elf_debug_section ("data section", -1, data_hdr);
3001 for (idx = 0; idx < asect->reloc_count; idx++)
3003 #ifdef RELOC_PROCESSING
3004 Elf_Internal_Rel dst;
3005 Elf_External_Rel *src;
3007 cache_ptr = reloc_cache + idx;
3008 src = native_relocs + idx;
3009 elf_swap_reloc_in (abfd, src, &dst);
3011 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
3013 Elf_Internal_Rel dst;
3014 Elf_External_Rel *src;
3016 cache_ptr = reloc_cache + idx;
3017 src = native_relocs + idx;
3019 elf_swap_reloc_in (abfd, src, &dst);
3021 if (asect->flags & SEC_RELOC)
3023 /* relocatable, so the offset is off of the section */
3024 cache_ptr->address = dst.r_offset + asect->vma;
3028 /* non-relocatable, so the offset a virtual address */
3029 cache_ptr->address = dst.r_offset;
3031 /* ELF_R_SYM(dst.r_info) is the symbol table offset...
3032 -1 is to skip the dummy symbol table entry */
3033 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
3034 BFD_ASSERT (dst.r_offset <= data_max);
3035 if (bfd_seek (abfd, data_off + dst.r_offset, SEEK_SET) != 0
3036 || bfd_read ((PTR) buf, sizeof (buf), 1, abfd) != sizeof (buf))
3038 bfd_error = system_call_error;
3042 cache_ptr->addend = (*abfd->xvec->bfd_getx_signed_32) ((bfd_byte *) buf);
3044 /* Fill in the cache_ptr->howto field from dst.r_type */
3046 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
3047 (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst);
3052 asect->relocation = reloc_cache;
3057 elf_canonicalize_reloc (abfd, section, relptr, symbols)
3063 arelent *tblptr = section->relocation;
3064 unsigned int count = 0;
3065 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
3067 /* snarfed from coffcode.h */
3069 elf_slurp_reloca_table (abfd, section, symbols);
3071 elf_slurp_reloc_table (abfd, section, symbols);
3073 tblptr = section->relocation;
3077 for (; count++ < section->reloc_count;)
3078 *relptr++ = tblptr++;
3081 return section->reloc_count;
3085 DEFUN (elf_get_symtab, (abfd, alocation),
3087 asymbol ** alocation)
3090 if (!elf_slurp_symbol_table (abfd, alocation))
3093 return bfd_get_symcount (abfd);
3097 DEFUN (elf_make_empty_symbol, (abfd),
3100 elf_symbol_type *newsym;
3102 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3105 bfd_error = no_memory;
3110 newsym->symbol.the_bfd = abfd;
3111 return &newsym->symbol;
3116 DEFUN (elf_get_symbol_info, (ignore_abfd, symbol, ret),
3117 bfd * ignore_abfd AND
3118 asymbol * symbol AND
3121 bfd_symbol_info (symbol, ret);
3125 DEFUN (elf_print_symbol, (ignore_abfd, filep, symbol, how),
3126 bfd * ignore_abfd AND
3128 asymbol * symbol AND
3129 bfd_print_symbol_type how)
3131 FILE *file = (FILE *) filep;
3134 case bfd_print_symbol_name:
3135 fprintf (file, "%s", symbol->name);
3137 case bfd_print_symbol_more:
3138 fprintf (file, "elf ");
3139 fprintf_vma (file, symbol->value);
3140 fprintf (file, " %lx", (long) symbol->flags);
3142 case bfd_print_symbol_all:
3144 CONST char *section_name;
3145 section_name = symbol->section ? symbol->section->name : "(*none*)";
3146 bfd_print_symbol_vandf ((PTR) file, symbol);
3147 fprintf (file, " %s\t%s",
3157 DEFUN (elf_get_lineno, (ignore_abfd, symbol),
3158 bfd * ignore_abfd AND
3161 fprintf (stderr, "elf_get_lineno unimplemented\n");
3168 DEFUN (elf_set_arch_mach, (abfd, arch, machine),
3170 enum bfd_architecture arch AND
3171 unsigned long machine)
3173 /* Allow any architecture to be supported by the elf backend */
3176 case bfd_arch_unknown: /* EM_NONE */
3177 case bfd_arch_sparc: /* EM_SPARC */
3178 case bfd_arch_i386: /* EM_386 */
3179 case bfd_arch_m68k: /* EM_68K */
3180 case bfd_arch_m88k: /* EM_88K */
3181 case bfd_arch_i860: /* EM_860 */
3182 case bfd_arch_mips: /* EM_MIPS (MIPS R3000) */
3183 case bfd_arch_hppa: /* EM_HPPA (HP PA_RISC) */
3184 return bfd_default_set_arch_mach (abfd, arch, machine);
3191 DEFUN (elf_find_nearest_line, (abfd,
3199 asection * section AND
3200 asymbol ** symbols AND
3202 CONST char **filename_ptr AND
3203 CONST char **functionname_ptr AND
3204 unsigned int *line_ptr)
3210 DEFUN (elf_sizeof_headers, (abfd, reloc),
3214 fprintf (stderr, "elf_sizeof_headers unimplemented\n");
3221 DEFUN (elf_set_section_contents, (abfd, section, location, offset, count),
3226 bfd_size_type count)
3228 Elf_Internal_Shdr *hdr;
3230 if (abfd->output_has_begun == false) /* set by bfd.c handler? */
3232 /* do setup calculations (FIXME) */
3233 prep_headers (abfd);
3234 elf_compute_section_file_positions (abfd);
3235 abfd->output_has_begun = true;
3238 hdr = &elf_section_data(section)->this_hdr;
3240 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3242 if (bfd_write (location, 1, count, abfd) != count)
3249 DEFUN (elf_no_info_to_howto, (abfd, cache_ptr, dst),
3251 arelent * cache_ptr AND
3252 Elf_Internal_Rela * dst)
3254 fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n");
3260 DEFUN (elf_no_info_to_howto_rel, (abfd, cache_ptr, dst),
3262 arelent * cache_ptr AND
3263 Elf_Internal_Rel * dst)
3265 fprintf (stderr, "elf REL relocation support for target machine unimplemented\n");
3271 /* Core file support */
3273 #ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3274 #include <sys/procfs.h>
3276 #define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3277 #define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3278 #define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3284 DEFUN (bfd_prstatus, (abfd, descdata, descsz, filepos),
3291 prstatus_t *status = (prstatus_t *) 0;
3293 if (descsz == sizeof (prstatus_t))
3295 newsect = bfd_make_section (abfd, ".reg");
3296 newsect->_raw_size = sizeof (status->pr_reg);
3297 newsect->filepos = filepos + (long) &status->pr_reg;
3298 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3299 newsect->alignment_power = 2;
3300 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3302 memcpy (core_prstatus (abfd), descdata, descsz);
3307 /* Stash a copy of the prpsinfo structure away for future use. */
3310 DEFUN (bfd_prpsinfo, (abfd, descdata, descsz, filepos),
3318 if (descsz == sizeof (prpsinfo_t))
3320 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3322 memcpy (core_prpsinfo (abfd), descdata, descsz);
3328 DEFUN (bfd_fpregset, (abfd, descdata, descsz, filepos),
3336 newsect = bfd_make_section (abfd, ".reg2");
3337 newsect->_raw_size = descsz;
3338 newsect->filepos = filepos;
3339 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3340 newsect->alignment_power = 2;
3343 #endif /* HAVE_PROCFS */
3345 /* Return a pointer to the args (including the command name) that were
3346 seen by the program that generated the core dump. Note that for
3347 some reason, a spurious space is tacked onto the end of the args
3348 in some (at least one anyway) implementations, so strip it off if
3352 DEFUN (elf_core_file_failing_command, (abfd),
3356 if (core_prpsinfo (abfd))
3358 prpsinfo_t *p = core_prpsinfo (abfd);
3359 char *scan = p->pr_psargs;
3364 if ((scan > p->pr_psargs) && (*scan == ' '))
3368 return p->pr_psargs;
3374 /* Return the number of the signal that caused the core dump. Presumably,
3375 since we have a core file, we got a signal of some kind, so don't bother
3376 checking the other process status fields, just return the signal number.
3380 DEFUN (elf_core_file_failing_signal, (abfd),
3384 if (core_prstatus (abfd))
3386 return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
3392 /* Check to see if the core file could reasonably be expected to have
3393 come for the current executable file. Note that by default we return
3394 true unless we find something that indicates that there might be a
3399 DEFUN (elf_core_file_matches_executable_p, (core_bfd, exec_bfd),
3408 /* First, xvecs must match since both are ELF files for the same target. */
3410 if (core_bfd->xvec != exec_bfd->xvec)
3412 bfd_error = system_call_error;
3418 /* If no prpsinfo, just return true. Otherwise, grab the last component
3419 of the exec'd pathname from the prpsinfo. */
3421 if (core_prpsinfo (core_bfd))
3423 corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname);
3430 /* Find the last component of the executable pathname. */
3432 if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
3438 execname = (char *) exec_bfd->filename;
3441 /* See if they match */
3443 return strcmp (execname, corename) ? false : true;
3449 #endif /* HAVE_PROCFS */
3452 /* ELF core files contain a segment of type PT_NOTE, that holds much of
3453 the information that would normally be available from the /proc interface
3454 for the process, at the time the process dumped core. Currently this
3455 includes copies of the prstatus, prpsinfo, and fpregset structures.
3457 Since these structures are potentially machine dependent in size and
3458 ordering, bfd provides two levels of support for them. The first level,
3459 available on all machines since it does not require that the host
3460 have /proc support or the relevant include files, is to create a bfd
3461 section for each of the prstatus, prpsinfo, and fpregset structures,
3462 without any interpretation of their contents. With just this support,
3463 the bfd client will have to interpret the structures itself. Even with
3464 /proc support, it might want these full structures for it's own reasons.
3466 In the second level of support, where HAVE_PROCFS is defined, bfd will
3467 pick apart the structures to gather some additional information that
3468 clients may want, such as the general register set, the name of the
3469 exec'ed file and its arguments, the signal (if any) that caused the
3475 DEFUN (elf_corefile_note, (abfd, hdr),
3477 Elf_Internal_Phdr * hdr)
3479 Elf_External_Note *x_note_p; /* Elf note, external form */
3480 Elf_Internal_Note i_note; /* Elf note, internal form */
3481 char *buf = NULL; /* Entire note segment contents */
3482 char *namedata; /* Name portion of the note */
3483 char *descdata; /* Descriptor portion of the note */
3484 char *sectname; /* Name to use for new section */
3485 long filepos; /* File offset to descriptor data */
3488 if (hdr->p_filesz > 0
3489 && (buf = (char *) bfd_xmalloc (hdr->p_filesz)) != NULL
3490 && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
3491 && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
3493 x_note_p = (Elf_External_Note *) buf;
3494 while ((char *) x_note_p < (buf + hdr->p_filesz))
3496 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
3497 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
3498 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
3499 namedata = x_note_p->name;
3500 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
3501 filepos = hdr->p_offset + (descdata - buf);
3502 switch (i_note.type)
3505 /* process descdata as prstatus info */
3506 bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
3507 sectname = ".prstatus";
3510 /* process descdata as fpregset info */
3511 bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
3512 sectname = ".fpregset";
3515 /* process descdata as prpsinfo */
3516 bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
3517 sectname = ".prpsinfo";
3520 /* Unknown descriptor, just ignore it. */
3524 if (sectname != NULL)
3526 newsect = bfd_make_section (abfd, sectname);
3527 newsect->_raw_size = i_note.descsz;
3528 newsect->filepos = filepos;
3529 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3530 newsect->alignment_power = 2;
3532 x_note_p = (Elf_External_Note *)
3533 (descdata + BFD_ALIGN (i_note.descsz, 4));
3544 /* Core files are simply standard ELF formatted files that partition
3545 the file using the execution view of the file (program header table)
3546 rather than the linking view. In fact, there is no section header
3547 table in a core file.
3549 The process status information (including the contents of the general
3550 register set) and the floating point register set are stored in a
3551 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3552 that allow standard bfd access to the general registers (.reg) and the
3553 floating point registers (.reg2).
3558 DEFUN (elf_core_file_p, (abfd), bfd * abfd)
3560 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
3561 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3562 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
3563 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
3564 unsigned int phindex;
3566 /* Read in the ELF header in external format. */
3568 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
3570 bfd_error = system_call_error;
3574 /* Now check to see if we have a valid ELF file, and one that BFD can
3575 make use of. The magic number must match, the address size ('class')
3576 and byte-swapping must match our XVEC entry, and it must have a
3577 program header table (FIXME: See comments re segments at top of this
3580 if (elf_file_p (&x_ehdr) == false)
3583 bfd_error = wrong_format;
3587 /* FIXME, Check EI_VERSION here ! */
3591 int desired_address_size = ELFCLASS32;
3594 int desired_address_size = ELFCLASS64;
3597 if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
3601 /* Switch xvec to match the specified byte order. */
3602 switch (x_ehdr.e_ident[EI_DATA])
3604 case ELFDATA2MSB: /* Big-endian */
3605 if (abfd->xvec->byteorder_big_p == false)
3608 case ELFDATA2LSB: /* Little-endian */
3609 if (abfd->xvec->byteorder_big_p == true)
3612 case ELFDATANONE: /* No data encoding specified */
3613 default: /* Unknown data encoding specified */
3617 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3618 the tdata pointer in the bfd. */
3621 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
3622 if (elf_tdata (abfd) == NULL)
3624 bfd_error = no_memory;
3628 /* FIXME, `wrong' returns from this point onward, leak memory. */
3630 /* Now that we know the byte order, swap in the rest of the header */
3631 i_ehdrp = elf_elfheader (abfd);
3632 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
3634 elf_debug_file (i_ehdrp);
3637 /* If there is no program header, or the type is not a core file, then
3639 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
3642 /* Allocate space for a copy of the program header table in
3643 internal form, seek to the program header table in the file,
3644 read it in, and convert it to internal form. As a simple sanity
3645 check, verify that the what BFD thinks is the size of each program
3646 header table entry actually matches the size recorded in the file. */
3648 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
3650 i_phdrp = (Elf_Internal_Phdr *)
3651 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
3654 bfd_error = no_memory;
3657 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
3659 bfd_error = system_call_error;
3662 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3664 if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
3667 bfd_error = system_call_error;
3670 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
3673 /* Once all of the program headers have been read and converted, we
3674 can start processing them. */
3676 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3678 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
3679 if ((i_phdrp + phindex)->p_type == PT_NOTE)
3681 elf_corefile_note (abfd, i_phdrp + phindex);
3685 /* Remember the entry point specified in the ELF file header. */
3687 bfd_get_start_address (abfd) = i_ehdrp->e_entry;