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