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