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