elfcode.h revision 77298
1146572Sharti/* ELF executable support for BFD.
2146572Sharti   Copyright 1991, 92, 93, 94, 95, 96, 97, 98, 1999, 2000 Free
3146572Sharti   Software Foundation, Inc.
4146572Sharti
5146572Sharti   Written by Fred Fish @ Cygnus Support, from information published
6146572Sharti   in "UNIX System V Release 4, Programmers Guide: ANSI C and
7146572Sharti   Programming Support Tools".  Sufficient support for gdb.
8146572Sharti
9146572Sharti   Rewritten by Mark Eichin @ Cygnus Support, from information
10146572Sharti   published in "System V Application Binary Interface", chapters 4
11146572Sharti   and 5, as well as the various "Processor Supplement" documents
12146572Sharti   derived from it. Added support for assembler and other object file
13146572Sharti   utilities.  Further work done by Ken Raeburn (Cygnus Support), Michael
14146572Sharti   Meissner (Open Software Foundation), and Peter Hoogenboom (University
15146572Sharti   of Utah) to finish and extend this.
16146572Sharti
17146572ShartiThis file is part of BFD, the Binary File Descriptor library.
18146572Sharti
19146572ShartiThis program is free software; you can redistribute it and/or modify
20146572Shartiit under the terms of the GNU General Public License as published by
21146572Shartithe Free Software Foundation; either version 2 of the License, or
22146572Sharti(at your option) any later version.
23146572Sharti
24146572ShartiThis program is distributed in the hope that it will be useful,
25146572Shartibut WITHOUT ANY WARRANTY; without even the implied warranty of
26146572ShartiMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27146572ShartiGNU General Public License for more details.
28146572Sharti
29146572ShartiYou should have received a copy of the GNU General Public License
30146572Shartialong with this program; if not, write to the Free Software
31146572ShartiFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
32146572Sharti
33146572Sharti/* Problems and other issues to resolve.
34146572Sharti
35146572Sharti   (1)	BFD expects there to be some fixed number of "sections" in
36146572Sharti        the object file.  I.E. there is a "section_count" variable in the
37146572Sharti	bfd structure which contains the number of sections.  However, ELF
38146572Sharti	supports multiple "views" of a file.  In particular, with current
39146572Sharti	implementations, executable files typically have two tables, a
40146572Sharti	program header table and a section header table, both of which
41146572Sharti	partition the executable.
42146572Sharti
43146572Sharti	In ELF-speak, the "linking view" of the file uses the section header
44146572Sharti	table to access "sections" within the file, and the "execution view"
45146572Sharti	uses the program header table to access "segments" within the file.
46146572Sharti	"Segments" typically may contain all the data from one or more
47146572Sharti	"sections".
48146572Sharti
49146572Sharti	Note that the section header table is optional in ELF executables,
50146572Sharti	but it is this information that is most useful to gdb.  If the
51146572Sharti	section header table is missing, then gdb should probably try
52146572Sharti	to make do with the program header table.  (FIXME)
53146572Sharti
54146572Sharti   (2)  The code in this file is compiled twice, once in 32-bit mode and
55146572Sharti	once in 64-bit mode.  More of it should be made size-independent
56146572Sharti	and moved into elf.c.
57146572Sharti
58146572Sharti   (3)	ELF section symbols are handled rather sloppily now.  This should
59146572Sharti	be cleaned up, and ELF section symbols reconciled with BFD section
60146572Sharti	symbols.
61146572Sharti
62146572Sharti   (4)  We need a published spec for 64-bit ELF.  We've got some stuff here
63146572Sharti	that we're using for SPARC V9 64-bit chips, but don't assume that
64146572Sharti	it's cast in stone.
65146572Sharti */
66146572Sharti
67146572Sharti#include "bfd.h"
68146572Sharti#include "sysdep.h"
69146572Sharti#include "bfdlink.h"
70146572Sharti#include "libbfd.h"
71146572Sharti#include "elf-bfd.h"
72146572Sharti
73146572Sharti/* Renaming structures, typedefs, macros and functions to be size-specific.  */
74146572Sharti#define Elf_External_Ehdr	NAME(Elf,External_Ehdr)
75146572Sharti#define Elf_External_Sym	NAME(Elf,External_Sym)
76146572Sharti#define Elf_External_Shdr	NAME(Elf,External_Shdr)
77146572Sharti#define Elf_External_Phdr	NAME(Elf,External_Phdr)
78146572Sharti#define Elf_External_Rel	NAME(Elf,External_Rel)
79146572Sharti#define Elf_External_Rela	NAME(Elf,External_Rela)
80146572Sharti#define Elf_External_Dyn	NAME(Elf,External_Dyn)
81146572Sharti
82146572Sharti#define elf_core_file_failing_command	NAME(bfd_elf,core_file_failing_command)
83146572Sharti#define elf_core_file_failing_signal	NAME(bfd_elf,core_file_failing_signal)
84146572Sharti#define elf_core_file_matches_executable_p \
85146572Sharti  NAME(bfd_elf,core_file_matches_executable_p)
86146572Sharti#define elf_object_p			NAME(bfd_elf,object_p)
87146572Sharti#define elf_core_file_p			NAME(bfd_elf,core_file_p)
88146572Sharti#define elf_get_symtab_upper_bound	NAME(bfd_elf,get_symtab_upper_bound)
89146572Sharti#define elf_get_dynamic_symtab_upper_bound \
90146572Sharti  NAME(bfd_elf,get_dynamic_symtab_upper_bound)
91146572Sharti#define elf_swap_reloc_in		NAME(bfd_elf,swap_reloc_in)
92146572Sharti#define elf_swap_reloca_in		NAME(bfd_elf,swap_reloca_in)
93146572Sharti#define elf_swap_reloc_out		NAME(bfd_elf,swap_reloc_out)
94146572Sharti#define elf_swap_reloca_out		NAME(bfd_elf,swap_reloca_out)
95146572Sharti#define elf_swap_symbol_in		NAME(bfd_elf,swap_symbol_in)
96146572Sharti#define elf_swap_symbol_out		NAME(bfd_elf,swap_symbol_out)
97146572Sharti#define elf_swap_phdr_in		NAME(bfd_elf,swap_phdr_in)
98146572Sharti#define elf_swap_phdr_out		NAME(bfd_elf,swap_phdr_out)
99146572Sharti#define elf_swap_dyn_in			NAME(bfd_elf,swap_dyn_in)
100146572Sharti#define elf_swap_dyn_out		NAME(bfd_elf,swap_dyn_out)
101146572Sharti#define elf_get_reloc_upper_bound	NAME(bfd_elf,get_reloc_upper_bound)
102146572Sharti#define elf_canonicalize_reloc		NAME(bfd_elf,canonicalize_reloc)
103146572Sharti#define elf_slurp_symbol_table		NAME(bfd_elf,slurp_symbol_table)
104146572Sharti#define elf_get_symtab			NAME(bfd_elf,get_symtab)
105146572Sharti#define elf_canonicalize_dynamic_symtab \
106146572Sharti  NAME(bfd_elf,canonicalize_dynamic_symtab)
107146572Sharti#define elf_make_empty_symbol		NAME(bfd_elf,make_empty_symbol)
108146572Sharti#define elf_get_symbol_info		NAME(bfd_elf,get_symbol_info)
109146572Sharti#define elf_get_lineno			NAME(bfd_elf,get_lineno)
110146572Sharti#define elf_set_arch_mach		NAME(bfd_elf,set_arch_mach)
111#define elf_find_nearest_line		NAME(bfd_elf,find_nearest_line)
112#define elf_sizeof_headers		NAME(bfd_elf,sizeof_headers)
113#define elf_set_section_contents	NAME(bfd_elf,set_section_contents)
114#define elf_no_info_to_howto		NAME(bfd_elf,no_info_to_howto)
115#define elf_no_info_to_howto_rel	NAME(bfd_elf,no_info_to_howto_rel)
116#define elf_find_section		NAME(bfd_elf,find_section)
117#define elf_bfd_link_add_symbols	NAME(bfd_elf,bfd_link_add_symbols)
118#define elf_add_dynamic_entry		NAME(bfd_elf,add_dynamic_entry)
119#define elf_write_shdrs_and_ehdr	NAME(bfd_elf,write_shdrs_and_ehdr)
120#define elf_write_out_phdrs		NAME(bfd_elf,write_out_phdrs)
121#define elf_write_relocs		NAME(bfd_elf,write_relocs)
122#define elf_slurp_reloc_table		NAME(bfd_elf,slurp_reloc_table)
123#define elf_link_create_dynamic_sections \
124  NAME(bfd_elf,link_create_dynamic_sections)
125#define elf_link_record_dynamic_symbol  _bfd_elf_link_record_dynamic_symbol
126#define elf_bfd_final_link		NAME(bfd_elf,bfd_final_link)
127#define elf_create_pointer_linker_section NAME(bfd_elf,create_pointer_linker_section)
128#define elf_finish_pointer_linker_section NAME(bfd_elf,finish_pointer_linker_section)
129#define elf_gc_sections			NAME(_bfd_elf,gc_sections)
130#define elf_gc_common_finalize_got_offsets \
131  NAME(_bfd_elf,gc_common_finalize_got_offsets)
132#define elf_gc_common_final_link	NAME(_bfd_elf,gc_common_final_link)
133#define elf_gc_record_vtinherit		NAME(_bfd_elf,gc_record_vtinherit)
134#define elf_gc_record_vtentry		NAME(_bfd_elf,gc_record_vtentry)
135#define elf_link_record_local_dynamic_symbol \
136  NAME(_bfd_elf,link_record_local_dynamic_symbol)
137
138#if ARCH_SIZE == 64
139#define ELF_R_INFO(X,Y)	ELF64_R_INFO(X,Y)
140#define ELF_R_SYM(X)	ELF64_R_SYM(X)
141#define ELF_R_TYPE(X)	ELF64_R_TYPE(X)
142#define ELFCLASS	ELFCLASS64
143#define FILE_ALIGN	8
144#define LOG_FILE_ALIGN	3
145#endif
146#if ARCH_SIZE == 32
147#define ELF_R_INFO(X,Y)	ELF32_R_INFO(X,Y)
148#define ELF_R_SYM(X)	ELF32_R_SYM(X)
149#define ELF_R_TYPE(X)	ELF32_R_TYPE(X)
150#define ELFCLASS	ELFCLASS32
151#define FILE_ALIGN	4
152#define LOG_FILE_ALIGN	2
153#endif
154
155/* Static functions */
156
157static void elf_swap_ehdr_in
158  PARAMS ((bfd *, const Elf_External_Ehdr *, Elf_Internal_Ehdr *));
159static void elf_swap_ehdr_out
160  PARAMS ((bfd *, const Elf_Internal_Ehdr *, Elf_External_Ehdr *));
161static void elf_swap_shdr_in
162  PARAMS ((bfd *, const Elf_External_Shdr *, Elf_Internal_Shdr *));
163static void elf_swap_shdr_out
164  PARAMS ((bfd *, const Elf_Internal_Shdr *, Elf_External_Shdr *));
165
166#define elf_stringtab_init _bfd_elf_stringtab_init
167
168#define section_from_elf_index bfd_section_from_elf_index
169
170static boolean elf_slurp_reloc_table_from_section
171  PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, bfd_size_type,
172	   arelent *, asymbol **, boolean));
173
174static boolean elf_file_p PARAMS ((Elf_External_Ehdr *));
175
176#ifdef DEBUG
177static void elf_debug_section PARAMS ((int, Elf_Internal_Shdr *));
178static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *));
179static char *elf_symbol_flags PARAMS ((flagword));
180#endif
181
182/* Structure swapping routines */
183
184/* Should perhaps use put_offset, put_word, etc.  For now, the two versions
185   can be handled by explicitly specifying 32 bits or "the long type".  */
186#if ARCH_SIZE == 64
187#define put_word	bfd_h_put_64
188#define put_signed_word	bfd_h_put_signed_64
189#define get_word	bfd_h_get_64
190#define get_signed_word	bfd_h_get_signed_64
191#endif
192#if ARCH_SIZE == 32
193#define put_word	bfd_h_put_32
194#define put_signed_word	bfd_h_put_signed_32
195#define get_word	bfd_h_get_32
196#define get_signed_word	bfd_h_get_signed_32
197#endif
198
199/* Translate an ELF symbol in external format into an ELF symbol in internal
200   format.  */
201
202void
203elf_swap_symbol_in (abfd, src, dst)
204     bfd *abfd;
205     const Elf_External_Sym *src;
206     Elf_Internal_Sym *dst;
207{
208  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
209
210  dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name);
211  if (signed_vma)
212    dst->st_value = get_signed_word (abfd, (bfd_byte *) src->st_value);
213  else
214    dst->st_value = get_word (abfd, (bfd_byte *) src->st_value);
215  dst->st_size = get_word (abfd, (bfd_byte *) src->st_size);
216  dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info);
217  dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other);
218  dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx);
219}
220
221/* Translate an ELF symbol in internal format into an ELF symbol in external
222   format.  */
223
224void
225elf_swap_symbol_out (abfd, src, cdst)
226     bfd *abfd;
227     const Elf_Internal_Sym *src;
228     PTR cdst;
229{
230  Elf_External_Sym *dst = (Elf_External_Sym *) cdst;
231  bfd_h_put_32 (abfd, src->st_name, dst->st_name);
232  put_word (abfd, src->st_value, dst->st_value);
233  put_word (abfd, src->st_size, dst->st_size);
234  bfd_h_put_8 (abfd, src->st_info, dst->st_info);
235  bfd_h_put_8 (abfd, src->st_other, dst->st_other);
236  bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx);
237}
238
239/* Translate an ELF file header in external format into an ELF file header in
240   internal format.  */
241
242static void
243elf_swap_ehdr_in (abfd, src, dst)
244     bfd *abfd;
245     const Elf_External_Ehdr *src;
246     Elf_Internal_Ehdr *dst;
247{
248  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
249  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
250  dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type);
251  dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine);
252  dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version);
253  if (signed_vma)
254    dst->e_entry = get_signed_word (abfd, (bfd_byte *) src->e_entry);
255  else
256    dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry);
257  dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff);
258  dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff);
259  dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags);
260  dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize);
261  dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize);
262  dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum);
263  dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize);
264  dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum);
265  dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx);
266}
267
268/* Translate an ELF file header in internal format into an ELF file header in
269   external format.  */
270
271static void
272elf_swap_ehdr_out (abfd, src, dst)
273     bfd *abfd;
274     const Elf_Internal_Ehdr *src;
275     Elf_External_Ehdr *dst;
276{
277  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
278  memcpy (dst->e_ident, src->e_ident, EI_NIDENT);
279  /* note that all elements of dst are *arrays of unsigned char* already...  */
280  bfd_h_put_16 (abfd, src->e_type, dst->e_type);
281  bfd_h_put_16 (abfd, src->e_machine, dst->e_machine);
282  bfd_h_put_32 (abfd, src->e_version, dst->e_version);
283  if (signed_vma)
284    put_signed_word (abfd, src->e_entry, dst->e_entry);
285  else
286    put_word (abfd, src->e_entry, dst->e_entry);
287  put_word (abfd, src->e_phoff, dst->e_phoff);
288  put_word (abfd, src->e_shoff, dst->e_shoff);
289  bfd_h_put_32 (abfd, src->e_flags, dst->e_flags);
290  bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize);
291  bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize);
292  bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum);
293  bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize);
294  bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum);
295  bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx);
296}
297
298/* Translate an ELF section header table entry in external format into an
299   ELF section header table entry in internal format.  */
300
301static void
302elf_swap_shdr_in (abfd, src, dst)
303     bfd *abfd;
304     const Elf_External_Shdr *src;
305     Elf_Internal_Shdr *dst;
306{
307  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
308
309  dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name);
310  dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type);
311  dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags);
312  if (signed_vma)
313    dst->sh_addr = get_signed_word (abfd, (bfd_byte *) src->sh_addr);
314  else
315    dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr);
316  dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset);
317  dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size);
318  dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link);
319  dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info);
320  dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign);
321  dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize);
322  dst->bfd_section = NULL;
323  dst->contents = NULL;
324}
325
326/* Translate an ELF section header table entry in internal format into an
327   ELF section header table entry in external format.  */
328
329static void
330elf_swap_shdr_out (abfd, src, dst)
331     bfd *abfd;
332     const Elf_Internal_Shdr *src;
333     Elf_External_Shdr *dst;
334{
335  /* note that all elements of dst are *arrays of unsigned char* already...  */
336  bfd_h_put_32 (abfd, src->sh_name, dst->sh_name);
337  bfd_h_put_32 (abfd, src->sh_type, dst->sh_type);
338  put_word (abfd, src->sh_flags, dst->sh_flags);
339  put_word (abfd, src->sh_addr, dst->sh_addr);
340  put_word (abfd, src->sh_offset, dst->sh_offset);
341  put_word (abfd, src->sh_size, dst->sh_size);
342  bfd_h_put_32 (abfd, src->sh_link, dst->sh_link);
343  bfd_h_put_32 (abfd, src->sh_info, dst->sh_info);
344  put_word (abfd, src->sh_addralign, dst->sh_addralign);
345  put_word (abfd, src->sh_entsize, dst->sh_entsize);
346}
347
348/* Translate an ELF program header table entry in external format into an
349   ELF program header table entry in internal format.  */
350
351void
352elf_swap_phdr_in (abfd, src, dst)
353     bfd *abfd;
354     const Elf_External_Phdr *src;
355     Elf_Internal_Phdr *dst;
356{
357  int signed_vma = get_elf_backend_data (abfd)->sign_extend_vma;
358
359  dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type);
360  dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags);
361  dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset);
362  if (signed_vma)
363    {
364      dst->p_vaddr = get_signed_word (abfd, (bfd_byte *) src->p_vaddr);
365      dst->p_paddr = get_signed_word (abfd, (bfd_byte *) src->p_paddr);
366    }
367  else
368    {
369      dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr);
370      dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr);
371    }
372  dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz);
373  dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz);
374  dst->p_align = get_word (abfd, (bfd_byte *) src->p_align);
375}
376
377void
378elf_swap_phdr_out (abfd, src, dst)
379     bfd *abfd;
380     const Elf_Internal_Phdr *src;
381     Elf_External_Phdr *dst;
382{
383  /* note that all elements of dst are *arrays of unsigned char* already...  */
384  bfd_h_put_32 (abfd, src->p_type, dst->p_type);
385  put_word (abfd, src->p_offset, dst->p_offset);
386  put_word (abfd, src->p_vaddr, dst->p_vaddr);
387  put_word (abfd, src->p_paddr, dst->p_paddr);
388  put_word (abfd, src->p_filesz, dst->p_filesz);
389  put_word (abfd, src->p_memsz, dst->p_memsz);
390  bfd_h_put_32 (abfd, src->p_flags, dst->p_flags);
391  put_word (abfd, src->p_align, dst->p_align);
392}
393
394/* Translate an ELF reloc from external format to internal format.  */
395INLINE void
396elf_swap_reloc_in (abfd, src, dst)
397     bfd *abfd;
398     const Elf_External_Rel *src;
399     Elf_Internal_Rel *dst;
400{
401  dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
402  dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
403}
404
405INLINE void
406elf_swap_reloca_in (abfd, src, dst)
407     bfd *abfd;
408     const Elf_External_Rela *src;
409     Elf_Internal_Rela *dst;
410{
411  dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset);
412  dst->r_info = get_word (abfd, (bfd_byte *) src->r_info);
413  dst->r_addend = get_signed_word (abfd, (bfd_byte *) src->r_addend);
414}
415
416/* Translate an ELF reloc from internal format to external format.  */
417INLINE void
418elf_swap_reloc_out (abfd, src, dst)
419     bfd *abfd;
420     const Elf_Internal_Rel *src;
421     Elf_External_Rel *dst;
422{
423  put_word (abfd, src->r_offset, dst->r_offset);
424  put_word (abfd, src->r_info, dst->r_info);
425}
426
427INLINE void
428elf_swap_reloca_out (abfd, src, dst)
429     bfd *abfd;
430     const Elf_Internal_Rela *src;
431     Elf_External_Rela *dst;
432{
433  put_word (abfd, src->r_offset, dst->r_offset);
434  put_word (abfd, src->r_info, dst->r_info);
435  put_signed_word (abfd, src->r_addend, dst->r_addend);
436}
437
438INLINE void
439elf_swap_dyn_in (abfd, p, dst)
440     bfd *abfd;
441     const PTR p;
442     Elf_Internal_Dyn *dst;
443{
444  const Elf_External_Dyn *src = (const Elf_External_Dyn *) p;
445
446  dst->d_tag = get_word (abfd, src->d_tag);
447  dst->d_un.d_val = get_word (abfd, src->d_un.d_val);
448}
449
450INLINE void
451elf_swap_dyn_out (abfd, src, p)
452     bfd *abfd;
453     const Elf_Internal_Dyn *src;
454     PTR p;
455{
456  Elf_External_Dyn *dst = (Elf_External_Dyn *) p;
457
458  put_word (abfd, src->d_tag, dst->d_tag);
459  put_word (abfd, src->d_un.d_val, dst->d_un.d_val);
460}
461
462/* ELF .o/exec file reading */
463
464/* Begin processing a given object.
465
466   First we validate the file by reading in the ELF header and checking
467   the magic number.  */
468
469static INLINE boolean
470elf_file_p (x_ehdrp)
471     Elf_External_Ehdr *x_ehdrp;
472{
473  return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0)
474	  && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1)
475	  && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2)
476	  && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3));
477}
478
479/* Check to see if the file associated with ABFD matches the target vector
480   that ABFD points to.
481
482   Note that we may be called several times with the same ABFD, but different
483   target vectors, most of which will not match.  We have to avoid leaving
484   any side effects in ABFD, or any data it points to (like tdata), if the
485   file does not match the target vector.  */
486
487const bfd_target *
488elf_object_p (abfd)
489     bfd *abfd;
490{
491  Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
492  Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form */
493  Elf_External_Shdr x_shdr;	/* Section header table entry, external form */
494  Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */
495  unsigned int shindex;
496  char *shstrtab;		/* Internal copy of section header stringtab */
497  struct elf_backend_data *ebd;
498  struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd);
499  struct sec *preserved_sections = abfd->sections;
500  unsigned int preserved_section_count = abfd->section_count;
501  enum bfd_architecture previous_arch = bfd_get_arch (abfd);
502  unsigned long previous_mach = bfd_get_mach (abfd);
503  struct elf_obj_tdata *new_tdata = NULL;
504  asection *s;
505
506  /* Clear section information, since there might be a recognized bfd that
507     we now check if we can replace, and we don't want to append to it.  */
508  abfd->sections = NULL;
509  abfd->section_count = 0;
510
511  /* Read in the ELF header in external format.  */
512
513  if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
514    {
515      if (bfd_get_error () != bfd_error_system_call)
516	goto got_wrong_format_error;
517      else
518	goto got_no_match;
519    }
520
521  /* Now check to see if we have a valid ELF file, and one that BFD can
522     make use of.  The magic number must match, the address size ('class')
523     and byte-swapping must match our XVEC entry, and it must have a
524     section header table (FIXME: See comments re sections at top of this
525     file).  */
526
527  if ((elf_file_p (&x_ehdr) == false) ||
528      (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) ||
529      (x_ehdr.e_ident[EI_CLASS] != ELFCLASS))
530    goto got_wrong_format_error;
531
532  /* Check that file's byte order matches xvec's */
533  switch (x_ehdr.e_ident[EI_DATA])
534    {
535    case ELFDATA2MSB:		/* Big-endian */
536      if (! bfd_header_big_endian (abfd))
537	goto got_wrong_format_error;
538      break;
539    case ELFDATA2LSB:		/* Little-endian */
540      if (! bfd_header_little_endian (abfd))
541	goto got_wrong_format_error;
542      break;
543    case ELFDATANONE:		/* No data encoding specified */
544    default:			/* Unknown data encoding specified */
545      goto got_wrong_format_error;
546    }
547
548  /* Allocate an instance of the elf_obj_tdata structure and hook it up to
549     the tdata pointer in the bfd.  */
550
551  new_tdata = ((struct elf_obj_tdata *)
552	       bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)));
553  if (new_tdata == NULL)
554    goto got_no_match;
555  elf_tdata (abfd) = new_tdata;
556
557  /* Now that we know the byte order, swap in the rest of the header */
558  i_ehdrp = elf_elfheader (abfd);
559  elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
560#if DEBUG & 1
561  elf_debug_file (i_ehdrp);
562#endif
563
564  /* Reject ET_CORE (header indicates core file, not object file) */
565  if (i_ehdrp->e_type == ET_CORE)
566    goto got_wrong_format_error;
567
568  /* If there is no section header table, we're hosed.  */
569  if (i_ehdrp->e_shoff == 0)
570    goto got_wrong_format_error;
571
572  /* As a simple sanity check, verify that the what BFD thinks is the
573     size of each section header table entry actually matches the size
574     recorded in the file.  */
575  if (i_ehdrp->e_shentsize != sizeof (x_shdr))
576    goto got_wrong_format_error;
577
578  ebd = get_elf_backend_data (abfd);
579
580  /* Check that the ELF e_machine field matches what this particular
581     BFD format expects.  */
582  if (ebd->elf_machine_code != i_ehdrp->e_machine
583      && (ebd->elf_machine_alt1 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt1)
584      && (ebd->elf_machine_alt2 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt2))
585    {
586      const bfd_target * const *target_ptr;
587
588      if (ebd->elf_machine_code != EM_NONE)
589	goto got_wrong_format_error;
590
591      /* This is the generic ELF target.  Let it match any ELF target
592	 for which we do not have a specific backend.  */
593      for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
594	{
595	  struct elf_backend_data *back;
596
597	  if ((*target_ptr)->flavour != bfd_target_elf_flavour)
598	    continue;
599	  back = (struct elf_backend_data *) (*target_ptr)->backend_data;
600	  if (back->elf_machine_code == i_ehdrp->e_machine
601	      || (back->elf_machine_alt1 != 0
602		  && back->elf_machine_alt1 == i_ehdrp->e_machine)
603	      || (back->elf_machine_alt2 != 0
604		  && back->elf_machine_alt2 == i_ehdrp->e_machine))
605	    {
606	      /* target_ptr is an ELF backend which matches this
607		 object file, so reject the generic ELF target.  */
608	      goto got_wrong_format_error;
609	    }
610	}
611    }
612
613  if (i_ehdrp->e_type == ET_EXEC)
614    abfd->flags |= EXEC_P;
615  else if (i_ehdrp->e_type == ET_DYN)
616    abfd->flags |= DYNAMIC;
617
618  if (i_ehdrp->e_phnum > 0)
619    abfd->flags |= D_PAGED;
620
621  if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0))
622    {
623      /* It's OK if this fails for the generic target.  */
624      if (ebd->elf_machine_code != EM_NONE)
625	goto got_no_match;
626    }
627
628  /* Remember the entry point specified in the ELF file header.  */
629  bfd_set_start_address (abfd, i_ehdrp->e_entry);
630
631  /* Allocate space for a copy of the section header table in
632     internal form, seek to the section header table in the file,
633     read it in, and convert it to internal form.  */
634  i_shdrp = ((Elf_Internal_Shdr *)
635	     bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum));
636  elf_elfsections (abfd) = ((Elf_Internal_Shdr **)
637			    bfd_alloc (abfd,
638				       sizeof (i_shdrp) * i_ehdrp->e_shnum));
639  if (!i_shdrp || !elf_elfsections (abfd))
640    goto got_no_match;
641  if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0)
642    goto got_no_match;
643  for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++)
644    {
645      if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr))
646	goto got_no_match;
647      elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex);
648      elf_elfsections (abfd)[shindex] = i_shdrp + shindex;
649
650      /* If the section is loaded, but not page aligned, clear
651         D_PAGED.  */
652      if ((i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0
653	  && i_shdrp[shindex].sh_type != SHT_NOBITS
654	  && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset)
655	       % ebd->maxpagesize)
656	      != 0))
657	abfd->flags &= ~D_PAGED;
658    }
659  if (i_ehdrp->e_shstrndx)
660    {
661      if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx))
662	goto got_no_match;
663    }
664
665  /* Read in the program headers.  */
666  if (i_ehdrp->e_phnum == 0)
667    elf_tdata (abfd)->phdr = NULL;
668  else
669    {
670      Elf_Internal_Phdr *i_phdr;
671      unsigned int i;
672
673      elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *)
674				bfd_alloc (abfd,
675					   (i_ehdrp->e_phnum
676					    * sizeof (Elf_Internal_Phdr))));
677      if (elf_tdata (abfd)->phdr == NULL)
678	goto got_no_match;
679      if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0)
680	goto got_no_match;
681      i_phdr = elf_tdata (abfd)->phdr;
682      for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++)
683	{
684	  Elf_External_Phdr x_phdr;
685
686	  if (bfd_read ((PTR) &x_phdr, sizeof x_phdr, 1, abfd)
687	      != sizeof x_phdr)
688	    goto got_no_match;
689	  elf_swap_phdr_in (abfd, &x_phdr, i_phdr);
690	}
691    }
692
693  /* Read in the string table containing the names of the sections.  We
694     will need the base pointer to this table later.  */
695  /* We read this inline now, so that we don't have to go through
696     bfd_section_from_shdr with it (since this particular strtab is
697     used to find all of the ELF section names.) */
698
699  shstrtab = bfd_elf_get_str_section (abfd, i_ehdrp->e_shstrndx);
700  if (!shstrtab)
701    goto got_no_match;
702
703  /* Once all of the section headers have been read and converted, we
704     can start processing them.  Note that the first section header is
705     a dummy placeholder entry, so we ignore it.  */
706
707  for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++)
708    {
709      if (! bfd_section_from_shdr (abfd, shindex))
710	goto got_no_match;
711    }
712
713  /* Let the backend double check the format and override global
714     information.  */
715  if (ebd->elf_backend_object_p)
716    {
717      if ((*ebd->elf_backend_object_p) (abfd) == false)
718	goto got_wrong_format_error;
719    }
720
721  /* If we have created any reloc sections that are associated with
722     debugging sections, mark the reloc sections as debugging as well.  */
723  for (s = abfd->sections; s != NULL; s = s->next)
724    {
725      if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL
726	   || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)
727	  && elf_section_data (s)->this_hdr.sh_info > 0)
728	{
729	  unsigned long targ_index;
730	  asection *targ_sec;
731
732	  targ_index = elf_section_data (s)->this_hdr.sh_info;
733	  targ_sec = bfd_section_from_elf_index (abfd, targ_index);
734	  if (targ_sec != NULL
735	      && (targ_sec->flags & SEC_DEBUGGING) != 0)
736	    s->flags |= SEC_DEBUGGING;
737	}
738    }
739
740  return (abfd->xvec);
741
742 got_wrong_format_error:
743  /* There is way too much undoing of half-known state here.  The caller,
744     bfd_check_format_matches, really shouldn't iterate on live bfd's to
745     check match/no-match like it does.  We have to rely on that a call to
746     bfd_default_set_arch_mach with the previously known mach, undoes what
747     was done by the first bfd_default_set_arch_mach (with mach 0) here.
748     For this to work, only elf-data and the mach may be changed by the
749     target-specific elf_backend_object_p function.  Note that saving the
750     whole bfd here and restoring it would be even worse; the first thing
751     you notice is that the cached bfd file position gets out of sync.  */
752  bfd_default_set_arch_mach (abfd, previous_arch, previous_mach);
753  bfd_set_error (bfd_error_wrong_format);
754 got_no_match:
755  if (new_tdata != NULL
756      && new_tdata->elf_sect_ptr != NULL)
757    bfd_release (abfd, new_tdata->elf_sect_ptr);
758  if (i_shdrp != NULL)
759    bfd_release (abfd, i_shdrp);
760  if (new_tdata != NULL)
761    bfd_release (abfd, new_tdata);
762  elf_tdata (abfd) = preserved_tdata;
763  abfd->sections = preserved_sections;
764  abfd->section_count = preserved_section_count;
765  return (NULL);
766}
767
768/* ELF .o/exec file writing */
769
770/* Write out the relocs.  */
771
772void
773elf_write_relocs (abfd, sec, data)
774     bfd *abfd;
775     asection *sec;
776     PTR data;
777{
778  boolean *failedp = (boolean *) data;
779  Elf_Internal_Shdr *rela_hdr;
780  Elf_External_Rela *outbound_relocas;
781  Elf_External_Rel *outbound_relocs;
782  unsigned int idx;
783  int use_rela_p;
784  asymbol *last_sym = 0;
785  int last_sym_idx = 0;
786
787  /* If we have already failed, don't do anything.  */
788  if (*failedp)
789    return;
790
791  if ((sec->flags & SEC_RELOC) == 0)
792    return;
793
794  /* The linker backend writes the relocs out itself, and sets the
795     reloc_count field to zero to inhibit writing them here.  Also,
796     sometimes the SEC_RELOC flag gets set even when there aren't any
797     relocs.  */
798  if (sec->reloc_count == 0)
799    return;
800
801  rela_hdr = &elf_section_data (sec)->rel_hdr;
802
803  rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count;
804  rela_hdr->contents = (PTR) bfd_alloc (abfd, rela_hdr->sh_size);
805  if (rela_hdr->contents == NULL)
806    {
807      *failedp = true;
808      return;
809    }
810
811  /* Figure out whether the relocations are RELA or REL relocations.  */
812  if (rela_hdr->sh_type == SHT_RELA)
813    use_rela_p = true;
814  else if (rela_hdr->sh_type == SHT_REL)
815    use_rela_p = false;
816  else
817    /* Every relocation section should be either an SHT_RELA or an
818       SHT_REL section.  */
819    abort ();
820
821  /* orelocation has the data, reloc_count has the count...  */
822  if (use_rela_p)
823    {
824      outbound_relocas = (Elf_External_Rela *) rela_hdr->contents;
825
826      for (idx = 0; idx < sec->reloc_count; idx++)
827	{
828	  Elf_Internal_Rela dst_rela;
829	  Elf_External_Rela *src_rela;
830	  arelent *ptr;
831	  asymbol *sym;
832	  int n;
833
834	  ptr = sec->orelocation[idx];
835	  src_rela = outbound_relocas + idx;
836
837	  /* The address of an ELF reloc is section relative for an object
838	     file, and absolute for an executable file or shared library.
839	     The address of a BFD reloc is always section relative.  */
840	  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
841	    dst_rela.r_offset = ptr->address;
842	  else
843	    dst_rela.r_offset = ptr->address + sec->vma;
844
845	  sym = *ptr->sym_ptr_ptr;
846	  if (sym == last_sym)
847	    n = last_sym_idx;
848	  else if (bfd_is_abs_section (sym->section) && sym->value == 0)
849	    n = STN_UNDEF;
850	  else
851	    {
852	      last_sym = sym;
853	      n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
854	      if (n < 0)
855		{
856		  *failedp = true;
857		  return;
858		}
859	      last_sym_idx = n;
860	    }
861
862	  if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
863	      && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
864	      && ! _bfd_elf_validate_reloc (abfd, ptr))
865	    {
866	      *failedp = true;
867	      return;
868	    }
869
870	  dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
871
872	  dst_rela.r_addend = ptr->addend;
873	  elf_swap_reloca_out (abfd, &dst_rela, src_rela);
874	}
875    }
876  else
877    /* REL relocations */
878    {
879      outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
880
881      for (idx = 0; idx < sec->reloc_count; idx++)
882	{
883	  Elf_Internal_Rel dst_rel;
884	  Elf_External_Rel *src_rel;
885	  arelent *ptr;
886	  int n;
887	  asymbol *sym;
888
889	  ptr = sec->orelocation[idx];
890	  sym = *ptr->sym_ptr_ptr;
891	  src_rel = outbound_relocs + idx;
892
893	  /* The address of an ELF reloc is section relative for an object
894	     file, and absolute for an executable file or shared library.
895	     The address of a BFD reloc is always section relative.  */
896	  if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
897	    dst_rel.r_offset = ptr->address;
898	  else
899	    dst_rel.r_offset = ptr->address + sec->vma;
900
901	  if (sym == last_sym)
902	    n = last_sym_idx;
903	  else if (bfd_is_abs_section (sym->section) && sym->value == 0)
904	    n = STN_UNDEF;
905	  else
906	    {
907	      last_sym = sym;
908	      n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
909	      if (n < 0)
910		{
911		  *failedp = true;
912		  return;
913		}
914	      last_sym_idx = n;
915	    }
916
917	  if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
918	      && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
919	      && ! _bfd_elf_validate_reloc (abfd, ptr))
920	    {
921	      *failedp = true;
922	      return;
923	    }
924
925	  dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
926
927	  elf_swap_reloc_out (abfd, &dst_rel, src_rel);
928	}
929    }
930}
931
932/* Write out the program headers.  */
933
934int
935elf_write_out_phdrs (abfd, phdr, count)
936     bfd *abfd;
937     const Elf_Internal_Phdr *phdr;
938     int count;
939{
940  while (count--)
941    {
942      Elf_External_Phdr extphdr;
943      elf_swap_phdr_out (abfd, phdr, &extphdr);
944      if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), 1, abfd)
945	  != sizeof (Elf_External_Phdr))
946	return -1;
947      phdr++;
948    }
949  return 0;
950}
951
952/* Write out the section headers and the ELF file header.  */
953
954boolean
955elf_write_shdrs_and_ehdr (abfd)
956     bfd *abfd;
957{
958  Elf_External_Ehdr x_ehdr;	/* Elf file header, external form */
959  Elf_Internal_Ehdr *i_ehdrp;	/* Elf file header, internal form */
960  Elf_External_Shdr *x_shdrp;	/* Section header table, external form */
961  Elf_Internal_Shdr **i_shdrp;	/* Section header table, internal form */
962  unsigned int count;
963
964  i_ehdrp = elf_elfheader (abfd);
965  i_shdrp = elf_elfsections (abfd);
966
967  /* swap the header before spitting it out...  */
968
969#if DEBUG & 1
970  elf_debug_file (i_ehdrp);
971#endif
972  elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
973  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
974      || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
975	  != sizeof (x_ehdr)))
976    return false;
977
978  /* at this point we've concocted all the ELF sections...  */
979  x_shdrp = (Elf_External_Shdr *)
980    bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
981  if (!x_shdrp)
982    return false;
983
984  for (count = 0; count < i_ehdrp->e_shnum; count++)
985    {
986#if DEBUG & 2
987      elf_debug_section (count, i_shdrp[count]);
988#endif
989      elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
990    }
991  if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
992      || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
993	  != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
994    return false;
995
996  /* need to dump the string table too...  */
997
998  return true;
999}
1000
1001long
1002elf_slurp_symbol_table (abfd, symptrs, dynamic)
1003     bfd *abfd;
1004     asymbol **symptrs;		/* Buffer for generated bfd symbols */
1005     boolean dynamic;
1006{
1007  Elf_Internal_Shdr *hdr;
1008  Elf_Internal_Shdr *verhdr;
1009  unsigned long symcount;	/* Number of external ELF symbols */
1010  elf_symbol_type *sym;		/* Pointer to current bfd symbol */
1011  elf_symbol_type *symbase;	/* Buffer for generated bfd symbols */
1012  Elf_Internal_Sym i_sym;
1013  Elf_External_Sym *x_symp = NULL;
1014  Elf_External_Versym *x_versymp = NULL;
1015
1016  /* Read each raw ELF symbol, converting from external ELF form to
1017     internal ELF form, and then using the information to create a
1018     canonical bfd symbol table entry.
1019
1020     Note that we allocate the initial bfd canonical symbol buffer
1021     based on a one-to-one mapping of the ELF symbols to canonical
1022     symbols.  We actually use all the ELF symbols, so there will be no
1023     space left over at the end.  When we have all the symbols, we
1024     build the caller's pointer vector.  */
1025
1026  if (! dynamic)
1027    {
1028      hdr = &elf_tdata (abfd)->symtab_hdr;
1029      verhdr = NULL;
1030    }
1031  else
1032    {
1033      hdr = &elf_tdata (abfd)->dynsymtab_hdr;
1034      if (elf_dynversym (abfd) == 0)
1035	verhdr = NULL;
1036      else
1037	verhdr = &elf_tdata (abfd)->dynversym_hdr;
1038      if ((elf_tdata (abfd)->dynverdef_section != 0
1039	   && elf_tdata (abfd)->verdef == NULL)
1040	  || (elf_tdata (abfd)->dynverref_section != 0
1041	      && elf_tdata (abfd)->verref == NULL))
1042	{
1043	  if (! _bfd_elf_slurp_version_tables (abfd))
1044	    return -1;
1045	}
1046    }
1047
1048  if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
1049    return -1;
1050
1051  symcount = hdr->sh_size / sizeof (Elf_External_Sym);
1052
1053  if (symcount == 0)
1054    sym = symbase = NULL;
1055  else
1056    {
1057      unsigned long i;
1058
1059      if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
1060	return -1;
1061
1062      symbase = ((elf_symbol_type *)
1063		 bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
1064      if (symbase == (elf_symbol_type *) NULL)
1065	return -1;
1066      sym = symbase;
1067
1068      /* Temporarily allocate room for the raw ELF symbols.  */
1069      x_symp = ((Elf_External_Sym *)
1070		bfd_malloc (symcount * sizeof (Elf_External_Sym)));
1071      if (x_symp == NULL && symcount != 0)
1072	goto error_return;
1073
1074      if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
1075	  != symcount * sizeof (Elf_External_Sym))
1076	goto error_return;
1077
1078      /* Read the raw ELF version symbol information.  */
1079
1080      if (verhdr != NULL
1081	  && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount)
1082	{
1083	  (*_bfd_error_handler)
1084	    (_("%s: version count (%ld) does not match symbol count (%ld)"),
1085	     abfd->filename,
1086	     (long) (verhdr->sh_size / sizeof (Elf_External_Versym)),
1087	     symcount);
1088
1089	  /* Slurp in the symbols without the version information,
1090             since that is more helpful than just quitting.  */
1091	  verhdr = NULL;
1092	}
1093
1094      if (verhdr != NULL)
1095	{
1096	  if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0)
1097	    goto error_return;
1098
1099	  x_versymp = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size);
1100	  if (x_versymp == NULL && verhdr->sh_size != 0)
1101	    goto error_return;
1102
1103	  if (bfd_read ((PTR) x_versymp, 1, verhdr->sh_size, abfd)
1104	      != verhdr->sh_size)
1105	    goto error_return;
1106	}
1107
1108      /* Skip first symbol, which is a null dummy.  */
1109      for (i = 1; i < symcount; i++)
1110	{
1111	  elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
1112	  memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
1113#ifdef ELF_KEEP_EXTSYM
1114	  memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
1115#endif
1116	  sym->symbol.the_bfd = abfd;
1117
1118	  sym->symbol.name = bfd_elf_string_from_elf_section (abfd,
1119							      hdr->sh_link,
1120							      i_sym.st_name);
1121
1122	  sym->symbol.value = i_sym.st_value;
1123
1124	  if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE)
1125	    {
1126	      sym->symbol.section = section_from_elf_index (abfd,
1127							    i_sym.st_shndx);
1128	      if (sym->symbol.section == NULL)
1129		{
1130		  /* This symbol is in a section for which we did not
1131		     create a BFD section.  Just use bfd_abs_section,
1132		     although it is wrong.  FIXME.  */
1133		  sym->symbol.section = bfd_abs_section_ptr;
1134		}
1135	    }
1136	  else if (i_sym.st_shndx == SHN_ABS)
1137	    {
1138	      sym->symbol.section = bfd_abs_section_ptr;
1139	    }
1140	  else if (i_sym.st_shndx == SHN_COMMON)
1141	    {
1142	      sym->symbol.section = bfd_com_section_ptr;
1143	      /* Elf puts the alignment into the `value' field, and
1144		 the size into the `size' field.  BFD wants to see the
1145		 size in the value field, and doesn't care (at the
1146		 moment) about the alignment.  */
1147	      sym->symbol.value = i_sym.st_size;
1148	    }
1149	  else if (i_sym.st_shndx == SHN_UNDEF)
1150	    {
1151	      sym->symbol.section = bfd_und_section_ptr;
1152	    }
1153	  else
1154	    sym->symbol.section = bfd_abs_section_ptr;
1155
1156	  /* If this is a relocateable file, then the symbol value is
1157             already section relative.  */
1158	  if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
1159	    sym->symbol.value -= sym->symbol.section->vma;
1160
1161	  switch (ELF_ST_BIND (i_sym.st_info))
1162	    {
1163	    case STB_LOCAL:
1164	      sym->symbol.flags |= BSF_LOCAL;
1165	      break;
1166	    case STB_GLOBAL:
1167	      if (i_sym.st_shndx != SHN_UNDEF
1168		  && i_sym.st_shndx != SHN_COMMON)
1169		sym->symbol.flags |= BSF_GLOBAL;
1170	      break;
1171	    case STB_WEAK:
1172	      sym->symbol.flags |= BSF_WEAK;
1173	      break;
1174	    }
1175
1176	  switch (ELF_ST_TYPE (i_sym.st_info))
1177	    {
1178	    case STT_SECTION:
1179	      sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
1180	      break;
1181	    case STT_FILE:
1182	      sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
1183	      break;
1184	    case STT_FUNC:
1185	      sym->symbol.flags |= BSF_FUNCTION;
1186	      break;
1187	    case STT_OBJECT:
1188	      sym->symbol.flags |= BSF_OBJECT;
1189	      break;
1190	    }
1191
1192	  if (dynamic)
1193	    sym->symbol.flags |= BSF_DYNAMIC;
1194
1195	  if (x_versymp != NULL)
1196	    {
1197	      Elf_Internal_Versym iversym;
1198
1199	      _bfd_elf_swap_versym_in (abfd, x_versymp + i, &iversym);
1200	      sym->version = iversym.vs_vers;
1201	    }
1202
1203	  /* Do some backend-specific processing on this symbol.  */
1204	  {
1205	    struct elf_backend_data *ebd = get_elf_backend_data (abfd);
1206	    if (ebd->elf_backend_symbol_processing)
1207	      (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
1208	  }
1209
1210	  sym++;
1211	}
1212    }
1213
1214  /* Do some backend-specific processing on this symbol table.  */
1215  {
1216    struct elf_backend_data *ebd = get_elf_backend_data (abfd);
1217    if (ebd->elf_backend_symbol_table_processing)
1218      (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
1219  }
1220
1221  /* We rely on the zalloc to clear out the final symbol entry.  */
1222
1223  symcount = sym - symbase;
1224
1225  /* Fill in the user's symbol pointer vector if needed.  */
1226  if (symptrs)
1227    {
1228      long l = symcount;
1229
1230      sym = symbase;
1231      while (l-- > 0)
1232	{
1233	  *symptrs++ = &sym->symbol;
1234	  sym++;
1235	}
1236      *symptrs = 0;		/* Final null pointer */
1237    }
1238
1239  if (x_versymp != NULL)
1240    free (x_versymp);
1241  if (x_symp != NULL)
1242    free (x_symp);
1243  return symcount;
1244error_return:
1245  if (x_versymp != NULL)
1246    free (x_versymp);
1247  if (x_symp != NULL)
1248    free (x_symp);
1249  return -1;
1250}
1251
1252/* Read  relocations for ASECT from REL_HDR.  There are RELOC_COUNT of
1253   them.  */
1254
1255static boolean
1256elf_slurp_reloc_table_from_section (abfd, asect, rel_hdr, reloc_count,
1257				    relents, symbols, dynamic)
1258     bfd *abfd;
1259     asection *asect;
1260     Elf_Internal_Shdr *rel_hdr;
1261     bfd_size_type reloc_count;
1262     arelent *relents;
1263     asymbol **symbols;
1264     boolean dynamic;
1265{
1266  struct elf_backend_data * const ebd = get_elf_backend_data (abfd);
1267  PTR allocated = NULL;
1268  bfd_byte *native_relocs;
1269  arelent *relent;
1270  unsigned int i;
1271  int entsize;
1272
1273  allocated = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size);
1274  if (allocated == NULL)
1275    goto error_return;
1276
1277  if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
1278      || (bfd_read (allocated, 1, rel_hdr->sh_size, abfd)
1279	  != rel_hdr->sh_size))
1280    goto error_return;
1281
1282  native_relocs = (bfd_byte *) allocated;
1283
1284  entsize = rel_hdr->sh_entsize;
1285  BFD_ASSERT (entsize == sizeof (Elf_External_Rel)
1286	      || entsize == sizeof (Elf_External_Rela));
1287
1288  for (i = 0, relent = relents;
1289       i < reloc_count;
1290       i++, relent++, native_relocs += entsize)
1291    {
1292      Elf_Internal_Rela rela;
1293      Elf_Internal_Rel rel;
1294
1295      if (entsize == sizeof (Elf_External_Rela))
1296	elf_swap_reloca_in (abfd, (Elf_External_Rela *) native_relocs, &rela);
1297      else
1298	{
1299	  elf_swap_reloc_in (abfd, (Elf_External_Rel *) native_relocs, &rel);
1300	  rela.r_offset = rel.r_offset;
1301	  rela.r_info = rel.r_info;
1302	  rela.r_addend = 0;
1303	}
1304
1305      /* The address of an ELF reloc is section relative for an object
1306	 file, and absolute for an executable file or shared library.
1307	 The address of a normal BFD reloc is always section relative,
1308	 and the address of a dynamic reloc is absolute..  */
1309      if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
1310	relent->address = rela.r_offset;
1311      else
1312	relent->address = rela.r_offset - asect->vma;
1313
1314      if (ELF_R_SYM (rela.r_info) == 0)
1315	relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1316      else
1317	{
1318	  asymbol **ps, *s;
1319
1320	  ps = symbols + ELF_R_SYM (rela.r_info) - 1;
1321	  s = *ps;
1322
1323	  /* Canonicalize ELF section symbols.  FIXME: Why?  */
1324	  if ((s->flags & BSF_SECTION_SYM) == 0)
1325	    relent->sym_ptr_ptr = ps;
1326	  else
1327	    relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
1328	}
1329
1330      relent->addend = rela.r_addend;
1331
1332      if (entsize == sizeof (Elf_External_Rela))
1333	(*ebd->elf_info_to_howto) (abfd, relent, &rela);
1334      else
1335	(*ebd->elf_info_to_howto_rel) (abfd, relent, &rel);
1336    }
1337
1338  if (allocated != NULL)
1339    free (allocated);
1340
1341  return true;
1342
1343 error_return:
1344  if (allocated != NULL)
1345    free (allocated);
1346  return false;
1347}
1348
1349/* Read in and swap the external relocs.  */
1350
1351boolean
1352elf_slurp_reloc_table (abfd, asect, symbols, dynamic)
1353     bfd *abfd;
1354     asection *asect;
1355     asymbol **symbols;
1356     boolean dynamic;
1357{
1358  struct bfd_elf_section_data * const d = elf_section_data (asect);
1359  Elf_Internal_Shdr *rel_hdr;
1360  Elf_Internal_Shdr *rel_hdr2;
1361  bfd_size_type reloc_count;
1362  bfd_size_type reloc_count2;
1363  arelent *relents;
1364
1365  if (asect->relocation != NULL)
1366    return true;
1367
1368  if (! dynamic)
1369    {
1370      if ((asect->flags & SEC_RELOC) == 0
1371	  || asect->reloc_count == 0)
1372	return true;
1373
1374      rel_hdr = &d->rel_hdr;
1375      reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
1376      rel_hdr2 = d->rel_hdr2;
1377      reloc_count2 = (rel_hdr2
1378		      ? (rel_hdr2->sh_size / rel_hdr2->sh_entsize)
1379		      : 0);
1380
1381      BFD_ASSERT (asect->reloc_count == reloc_count + reloc_count2);
1382      BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
1383		  || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
1384
1385    }
1386  else
1387    {
1388      /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
1389	 case because relocations against this section may use the
1390	 dynamic symbol table, and in that case bfd_section_from_shdr
1391	 in elf.c does not update the RELOC_COUNT.  */
1392      if (asect->_raw_size == 0)
1393	return true;
1394
1395      rel_hdr = &d->this_hdr;
1396      reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
1397      rel_hdr2 = NULL;
1398      reloc_count2 = 0;
1399    }
1400
1401  relents = ((arelent *)
1402	     bfd_alloc (abfd,
1403			(reloc_count + reloc_count2) * sizeof (arelent)));
1404  if (relents == NULL)
1405    return false;
1406
1407  if (!elf_slurp_reloc_table_from_section (abfd, asect,
1408					   rel_hdr, reloc_count,
1409					   relents,
1410					   symbols, dynamic))
1411    return false;
1412
1413  if (rel_hdr2
1414      && !elf_slurp_reloc_table_from_section (abfd, asect,
1415					      rel_hdr2, reloc_count2,
1416					      relents + reloc_count,
1417					      symbols, dynamic))
1418    return false;
1419
1420  asect->relocation = relents;
1421  return true;
1422}
1423
1424#ifdef DEBUG
1425static void
1426elf_debug_section (num, hdr)
1427     int num;
1428     Elf_Internal_Shdr *hdr;
1429{
1430  fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num,
1431	   hdr->bfd_section != NULL ? hdr->bfd_section->name : "",
1432	   (long) hdr);
1433  fprintf (stderr,
1434	   "sh_name      = %ld\tsh_type      = %ld\tsh_flags     = %ld\n",
1435	   (long) hdr->sh_name,
1436	   (long) hdr->sh_type,
1437	   (long) hdr->sh_flags);
1438  fprintf (stderr,
1439	   "sh_addr      = %ld\tsh_offset    = %ld\tsh_size      = %ld\n",
1440	   (long) hdr->sh_addr,
1441	   (long) hdr->sh_offset,
1442	   (long) hdr->sh_size);
1443  fprintf (stderr,
1444	   "sh_link      = %ld\tsh_info      = %ld\tsh_addralign = %ld\n",
1445	   (long) hdr->sh_link,
1446	   (long) hdr->sh_info,
1447	   (long) hdr->sh_addralign);
1448  fprintf (stderr, "sh_entsize   = %ld\n",
1449	   (long) hdr->sh_entsize);
1450  fflush (stderr);
1451}
1452
1453static void
1454elf_debug_file (ehdrp)
1455     Elf_Internal_Ehdr *ehdrp;
1456{
1457  fprintf (stderr, "e_entry      = 0x%.8lx\n", (long) ehdrp->e_entry);
1458  fprintf (stderr, "e_phoff      = %ld\n", (long) ehdrp->e_phoff);
1459  fprintf (stderr, "e_phnum      = %ld\n", (long) ehdrp->e_phnum);
1460  fprintf (stderr, "e_phentsize  = %ld\n", (long) ehdrp->e_phentsize);
1461  fprintf (stderr, "e_shoff      = %ld\n", (long) ehdrp->e_shoff);
1462  fprintf (stderr, "e_shnum      = %ld\n", (long) ehdrp->e_shnum);
1463  fprintf (stderr, "e_shentsize  = %ld\n", (long) ehdrp->e_shentsize);
1464}
1465
1466static char *
1467elf_symbol_flags (flags)
1468     flagword flags;
1469{
1470  static char buffer[1024];
1471
1472  buffer[0] = '\0';
1473  if (flags & BSF_LOCAL)
1474    strcat (buffer, " local");
1475
1476  if (flags & BSF_GLOBAL)
1477    strcat (buffer, " global");
1478
1479  if (flags & BSF_DEBUGGING)
1480    strcat (buffer, " debug");
1481
1482  if (flags & BSF_FUNCTION)
1483    strcat (buffer, " function");
1484
1485  if (flags & BSF_KEEP)
1486    strcat (buffer, " keep");
1487
1488  if (flags & BSF_KEEP_G)
1489    strcat (buffer, " keep_g");
1490
1491  if (flags & BSF_WEAK)
1492    strcat (buffer, " weak");
1493
1494  if (flags & BSF_SECTION_SYM)
1495    strcat (buffer, " section-sym");
1496
1497  if (flags & BSF_OLD_COMMON)
1498    strcat (buffer, " old-common");
1499
1500  if (flags & BSF_NOT_AT_END)
1501    strcat (buffer, " not-at-end");
1502
1503  if (flags & BSF_CONSTRUCTOR)
1504    strcat (buffer, " constructor");
1505
1506  if (flags & BSF_WARNING)
1507    strcat (buffer, " warning");
1508
1509  if (flags & BSF_INDIRECT)
1510    strcat (buffer, " indirect");
1511
1512  if (flags & BSF_FILE)
1513    strcat (buffer, " file");
1514
1515  if (flags & DYNAMIC)
1516    strcat (buffer, " dynamic");
1517
1518  if (flags & ~(BSF_LOCAL
1519		| BSF_GLOBAL
1520		| BSF_DEBUGGING
1521		| BSF_FUNCTION
1522		| BSF_KEEP
1523		| BSF_KEEP_G
1524		| BSF_WEAK
1525		| BSF_SECTION_SYM
1526		| BSF_OLD_COMMON
1527		| BSF_NOT_AT_END
1528		| BSF_CONSTRUCTOR
1529		| BSF_WARNING
1530		| BSF_INDIRECT
1531		| BSF_FILE
1532		| BSF_DYNAMIC))
1533    strcat (buffer, " unknown-bits");
1534
1535  return buffer;
1536}
1537#endif
1538
1539#include "elfcore.h"
1540#include "elflink.h"
1541
1542/* Size-dependent data and functions.  */
1543const struct elf_size_info NAME(_bfd_elf,size_info) = {
1544  sizeof (Elf_External_Ehdr),
1545  sizeof (Elf_External_Phdr),
1546  sizeof (Elf_External_Shdr),
1547  sizeof (Elf_External_Rel),
1548  sizeof (Elf_External_Rela),
1549  sizeof (Elf_External_Sym),
1550  sizeof (Elf_External_Dyn),
1551  sizeof (Elf_External_Note),
1552  4,
1553  1,
1554  ARCH_SIZE, FILE_ALIGN,
1555  ELFCLASS, EV_CURRENT,
1556  elf_write_out_phdrs,
1557  elf_write_shdrs_and_ehdr,
1558  elf_write_relocs,
1559  elf_swap_symbol_out,
1560  elf_slurp_reloc_table,
1561  elf_slurp_symbol_table,
1562  elf_swap_dyn_in,
1563  elf_swap_dyn_out,
1564  NULL,
1565  NULL,
1566  NULL,
1567  NULL
1568};
1569