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