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