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