OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [oc/] [gdb-5.0/] [bfd/] [elfcode.h] - Blame information for rev 104

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.