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