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