* configure: Unknown options are fatal again.
[deliverable/binutils-gdb.git] / bfd / elfcode.h
CommitLineData
244ffee7 1/* ELF executable support for BFD.
b9d5cdf0 2 Copyright 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
244ffee7
JK
3
4 Written by Fred Fish @ Cygnus Support, from information published
5 in "UNIX System V Release 4, Programmers Guide: ANSI C and
6 Programming Support Tools". Sufficient support for gdb.
7
8 Rewritten by Mark Eichin @ Cygnus Support, from information
9 published in "System V Application Binary Interface", chapters 4
10 and 5, as well as the various "Processor Supplement" documents
11 derived from it. Added support for assembler and other object file
12 utilities. Further work done by Ken Raeburn (Cygnus Support), Michael
13 Meissner (Open Software Foundation), and Peter Hoogenboom (University
14 of Utah) to finish and extend this.
15
16This file is part of BFD, the Binary File Descriptor library.
17
18This program is free software; you can redistribute it and/or modify
19it under the terms of the GNU General Public License as published by
20the Free Software Foundation; either version 2 of the License, or
21(at your option) any later version.
22
23This program is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26GNU General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with this program; if not, write to the Free Software
30Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
31
244ffee7
JK
32/* Problems and other issues to resolve.
33
34 (1) BFD expects there to be some fixed number of "sections" in
35 the object file. I.E. there is a "section_count" variable in the
36 bfd structure which contains the number of sections. However, ELF
37 supports multiple "views" of a file. In particular, with current
38 implementations, executable files typically have two tables, a
39 program header table and a section header table, both of which
40 partition the executable.
41
42 In ELF-speak, the "linking view" of the file uses the section header
43 table to access "sections" within the file, and the "execution view"
44 uses the program header table to access "segments" within the file.
45 "Segments" typically may contain all the data from one or more
46 "sections".
47
48 Note that the section header table is optional in ELF executables,
49 but it is this information that is most useful to gdb. If the
50 section header table is missing, then gdb should probably try
51 to make do with the program header table. (FIXME)
52
6a3eb9b6
KR
53 (2) The code in this file is compiled twice, once in 32-bit mode and
54 once in 64-bit mode. More of it should be made size-independent
55 and moved into elf.c.
56
d24928c0
KR
57 (3) ELF section symbols are handled rather sloppily now. This should
58 be cleaned up, and ELF section symbols reconciled with BFD section
59 symbols.
60 */
244ffee7
JK
61
62#include <string.h> /* For strrchr and friends */
63#include "bfd.h"
64#include "sysdep.h"
65#include "libbfd.h"
66#include "libelf.h"
67
32090b8e 68/* Renaming structures, typedefs, macros and functions to be size-specific. */
244ffee7 69#define Elf_External_Ehdr NAME(Elf,External_Ehdr)
244ffee7 70#define Elf_External_Sym NAME(Elf,External_Sym)
244ffee7 71#define Elf_External_Shdr NAME(Elf,External_Shdr)
244ffee7 72#define Elf_External_Phdr NAME(Elf,External_Phdr)
244ffee7
JK
73#define Elf_External_Rel NAME(Elf,External_Rel)
74#define Elf_External_Rela NAME(Elf,External_Rela)
244ffee7 75
244ffee7
JK
76#define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command)
77#define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal)
78#define elf_core_file_matches_executable_p NAME(bfd_elf,core_file_matches_executable_p)
79#define elf_object_p NAME(bfd_elf,object_p)
80#define elf_core_file_p NAME(bfd_elf,core_file_p)
244ffee7
JK
81#define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound)
82#define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound)
83#define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc)
84#define elf_get_symtab NAME(bfd_elf,get_symtab)
85#define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol)
86#define elf_get_symbol_info NAME(bfd_elf,get_symbol_info)
87#define elf_print_symbol NAME(bfd_elf,print_symbol)
88#define elf_get_lineno NAME(bfd_elf,get_lineno)
89#define elf_set_arch_mach NAME(bfd_elf,set_arch_mach)
90#define elf_find_nearest_line NAME(bfd_elf,find_nearest_line)
91#define elf_sizeof_headers NAME(bfd_elf,sizeof_headers)
92#define elf_set_section_contents NAME(bfd_elf,set_section_contents)
93#define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto)
94#define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel)
fce36137 95#define elf_new_section_hook NAME(bfd_elf,new_section_hook)
32090b8e 96#define write_relocs NAME(bfd_elf,_write_relocs)
f035cc47 97#define elf_find_section NAME(bfd_elf,find_section)
244ffee7 98
6a3eb9b6
KR
99#if ARCH_SIZE == 64
100#define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y)
101#define ELF_R_SYM(X) ELF64_R_SYM(X)
32090b8e 102#define ELFCLASS ELFCLASS64
f035cc47 103#define FILE_ALIGN 8
6a3eb9b6
KR
104#endif
105#if ARCH_SIZE == 32
106#define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y)
107#define ELF_R_SYM(X) ELF32_R_SYM(X)
32090b8e 108#define ELFCLASS ELFCLASS32
f035cc47 109#define FILE_ALIGN 4
244ffee7
JK
110#endif
111
32090b8e
KR
112static int shstrtab_length_fixed;
113
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
KR
1173 sym = *ptr->sym_ptr_ptr;
1174 if (sym == last_sym)
1175 n = last_sym_idx;
1176 else
32090b8e 1177 {
300adb31
KR
1178 last_sym = sym;
1179 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
32090b8e 1180 }
300adb31
KR
1181 dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type);
1182
1183 dst_rela.r_addend = ptr->addend;
1184 elf_swap_reloca_out (abfd, &dst_rela, src_rela);
244ffee7 1185 }
300adb31
KR
1186 }
1187 else
1188 /* REL relocations */
1189 {
1190 outbound_relocs = (Elf_External_Rel *) rela_hdr->contents;
1191
1192 for (idx = 0; idx < sec->reloc_count; idx++)
32090b8e 1193 {
300adb31
KR
1194 Elf_Internal_Rel dst_rel;
1195 Elf_External_Rel *src_rel;
1196 arelent *ptr;
1197 int n;
1198 asymbol *sym;
1199
1200 ptr = sec->orelocation[idx];
1201 sym = *ptr->sym_ptr_ptr;
1202 src_rel = outbound_relocs + idx;
1203 if (!(abfd->flags & EXEC_P))
1204 dst_rel.r_offset = ptr->address - sec->vma;
1205 else
1206 dst_rel.r_offset = ptr->address;
244ffee7 1207
300adb31
KR
1208 if (sym == last_sym)
1209 n = last_sym_idx;
1210 else
32090b8e 1211 {
300adb31
KR
1212 last_sym = sym;
1213 last_sym_idx = n = elf_symbol_from_bfd_symbol (abfd, &sym);
32090b8e 1214 }
300adb31
KR
1215 dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type);
1216
1217 elf_swap_reloc_out (abfd, &dst_rel, src_rel);
32090b8e 1218 }
300adb31 1219 }
32090b8e 1220}
244ffee7 1221
32090b8e
KR
1222static void
1223fix_up_strtabs (abfd, asect, obj)
1224 bfd *abfd;
1225 asection *asect;
1226 PTR obj;
1227{
1228 Elf_Internal_Shdr *this_hdr = &elf_section_data (asect)->this_hdr;
1c6042ee 1229 int this_idx = elf_section_data (asect)->this_idx;
244ffee7 1230
32090b8e
KR
1231 /* @@ Check flags! */
1232 if (!strncmp (asect->name, ".stab", 5)
1233 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
1234 {
1235 size_t len = strlen (asect->name) + 1;
80425e6c
JK
1236 char *s = (char *) malloc (len);
1237 if (s == NULL)
1238 /* FIXME: Should deal more gracefully with errors. */
1239 abort ();
32090b8e
KR
1240 strcpy (s, asect->name);
1241 s[len - 4] = 0;
1242 asect = bfd_get_section_by_name (abfd, s);
80425e6c 1243 free (s);
32090b8e
KR
1244 if (!asect)
1245 abort ();
1c6042ee 1246 elf_section_data (asect)->this_hdr.sh_link = this_idx;
32090b8e 1247 /* @@ Assuming 32 bits! */
1c6042ee 1248 elf_section_data (asect)->this_hdr.sh_entsize = 0xc;
01383fb4
KR
1249
1250 this_hdr->sh_type = SHT_STRTAB;
244ffee7 1251 }
32090b8e 1252}
244ffee7 1253
32090b8e 1254static void
1c6042ee
ILT
1255elf_fake_sections (abfd, asect, obj)
1256 bfd *abfd;
1257 asection *asect;
1258 PTR obj;
32090b8e
KR
1259{
1260 /* most of what is in bfd_shdr_from_section goes in here... */
1261 /* and all of these sections generate at *least* one ELF section. */
244ffee7 1262
32090b8e
KR
1263 Elf_Internal_Shdr *this_hdr;
1264 this_hdr = &elf_section_data (asect)->this_hdr;
1265 this_hdr->sh_name =
1266 bfd_add_to_strtab (abfd, elf_shstrtab (abfd), asect->name);
1267 /* We need to log the type *now* so that elf_section_from_bfd_section
1268 can find us... have to set rawdata too. */
1269 this_hdr->rawdata = (void *) asect;
1270 this_hdr->sh_addralign = 1 << asect->alignment_power;
1271 if ((asect->flags & SEC_ALLOC) && (asect->flags & SEC_LOAD))
1272 this_hdr->sh_type = SHT_PROGBITS;
e621c5cc
ILT
1273 else if ((asect->flags & SEC_ALLOC) && ((asect->flags & SEC_LOAD) == 0))
1274 {
6c35a16d
ILT
1275 BFD_ASSERT (strcmp (asect->name, ".bss") == 0
1276 || strcmp (asect->name, ".sbss") == 0);
e621c5cc
ILT
1277 this_hdr->sh_type = SHT_NOBITS;
1278 }
1279 /* FIXME I am not sure how to detect a .note section from the flags
1280 word of an `asection'. */
1281 else if (!strcmp (asect->name, ".note"))
1282 this_hdr->sh_type = SHT_NOTE;
32090b8e 1283 else
32090b8e
KR
1284 this_hdr->sh_type = SHT_PROGBITS;
1285
1286 this_hdr->sh_flags = 0;
1287 this_hdr->sh_addr = 0;
1288 this_hdr->sh_size = 0;
1289 this_hdr->sh_entsize = 0;
1290 this_hdr->sh_info = 0;
1291 this_hdr->sh_link = 0;
1292 this_hdr->sh_offset = 0;
1293 this_hdr->size = 0;
244ffee7 1294
f035cc47
ILT
1295 /* Now, check for processor-specific section types. */
1296 {
1297 struct elf_backend_data *bed = get_elf_backend_data (abfd);
1298
1299 if (bed->elf_backend_fake_sections)
1300 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect);
1301 }
1302
32090b8e
KR
1303 {
1304 /* Emit a strtab and symtab, and possibly a reloc section. */
1305 Elf_Internal_Shdr *rela_hdr;
244ffee7 1306
32090b8e
KR
1307 /* Note that only one symtab is used, so just remember it
1308 for now. */
244ffee7 1309
300adb31 1310 if (asect->flags & SEC_RELOC)
32090b8e
KR
1311 {
1312 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
244ffee7 1313
32090b8e
KR
1314 rela_hdr = &elf_section_data (asect)->rel_hdr;
1315 rela_hdr->sh_name =
1316 bfd_add_2_to_strtab (abfd, elf_shstrtab (abfd),
1317 use_rela_p ? ".rela" : ".rel",
1318 asect->name);
1319 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL;
1320 rela_hdr->sh_entsize = (use_rela_p
1321 ? sizeof (Elf_External_Rela)
1322 : sizeof (Elf_External_Rel));
1323
1324 rela_hdr->sh_flags = 0;
1325 rela_hdr->sh_addr = 0;
1326 rela_hdr->sh_size = 0;
1327 rela_hdr->sh_offset = 0;
062189c6
ILT
1328
1329 /* FIXME: Systems I've checked use an alignment of 4, but some
1330 systems may use a different alignment. */
1331 rela_hdr->sh_addralign = 4;
1332
32090b8e
KR
1333 rela_hdr->size = 0;
1334 }
1335 }
1336 if (asect->flags & SEC_ALLOC)
1337 {
1338 this_hdr->sh_flags |= SHF_ALLOC;
1339 if (asect->flags & SEC_LOAD)
1340 {
1341 /* @@ Do something with sh_type? */
1342 }
1343 }
1344 if (!(asect->flags & SEC_READONLY))
1345 this_hdr->sh_flags |= SHF_WRITE;
1346 if (asect->flags & SEC_CODE)
1347 this_hdr->sh_flags |= SHF_EXECINSTR;
244ffee7
JK
1348}
1349
32090b8e
KR
1350/* Map symbol from it's internal number to the external number, moving
1351 all local symbols to be at the head of the list. */
244ffee7 1352
32090b8e 1353static INLINE int
062189c6
ILT
1354sym_is_global (abfd, sym)
1355 bfd *abfd;
32090b8e
KR
1356 asymbol *sym;
1357{
062189c6
ILT
1358 /* If the backend has a special mapping, use it. */
1359 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1360 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global)
1361 (abfd, sym));
1362
d24928c0 1363 if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
244ffee7 1364 {
32090b8e
KR
1365 if (sym->flags & BSF_LOCAL)
1366 abort ();
1367 return 1;
244ffee7 1368 }
d24928c0
KR
1369 if (sym->section == 0)
1370 {
1371 /* Is this valid? */
1372 abort ();
1373
1374 return 1;
1375 }
32090b8e
KR
1376 if (sym->section == &bfd_und_section)
1377 return 1;
1378 if (bfd_is_com_section (sym->section))
1379 return 1;
1380 if (sym->flags & (BSF_LOCAL | BSF_SECTION_SYM | BSF_FILE))
1381 return 0;
1382 return 0;
1383}
244ffee7 1384
9783e04a 1385static boolean
1c6042ee
ILT
1386elf_map_symbols (abfd)
1387 bfd *abfd;
32090b8e
KR
1388{
1389 int symcount = bfd_get_symcount (abfd);
1390 asymbol **syms = bfd_get_outsymbols (abfd);
d24928c0 1391 asymbol **sect_syms;
32090b8e
KR
1392 int num_locals = 0;
1393 int num_globals = 0;
1394 int num_locals2 = 0;
1395 int num_globals2 = 0;
d24928c0 1396 int max_index = 0;
32090b8e 1397 int num_sections = 0;
d24928c0 1398 Elf_Sym_Extra *sym_extra;
32090b8e
KR
1399 int idx;
1400 asection *asect;
6a3eb9b6 1401
32090b8e
KR
1402#ifdef DEBUG
1403 fprintf (stderr, "elf_map_symbols\n");
1404 fflush (stderr);
1405#endif
244ffee7 1406
e621c5cc
ILT
1407 /* Add local symbols for each section for which there are relocs.
1408 FIXME: How can we tell which sections have relocs at this point?
1409 Will reloc_count always be accurate? Actually, I think most ELF
1410 targets create section symbols for all sections anyhow. */
32090b8e 1411 for (asect = abfd->sections; asect; asect = asect->next)
244ffee7 1412 {
d24928c0
KR
1413 if (max_index < asect->index)
1414 max_index = asect->index;
244ffee7
JK
1415 }
1416
d24928c0
KR
1417 max_index++;
1418 elf_num_section_syms (abfd) = max_index;
1419 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *));
1420 elf_section_syms (abfd) = sect_syms;
1421
5e829a34 1422 if (sect_syms == 0)
9783e04a 1423 {
d1ad85a6 1424 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1425 return false;
1426 }
d24928c0
KR
1427
1428 for (asect = abfd->sections; asect; asect = asect->next)
e621c5cc
ILT
1429 {
1430 asymbol *sym = bfd_make_empty_symbol (abfd);
9783e04a
DM
1431 if (!sym)
1432 {
d1ad85a6 1433 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1434 return false;
1435 }
e621c5cc
ILT
1436 sym->the_bfd = abfd;
1437 sym->name = asect->name;
1438 sym->value = asect->vma;
1439 sym->flags = BSF_SECTION_SYM;
1440 sym->section = asect;
1441 sect_syms[asect->index] = sym;
1442 num_sections++;
d24928c0 1443#ifdef DEBUG
e621c5cc
ILT
1444 fprintf (stderr,
1445 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n",
1446 asect->name, (long) asect->vma, asect->index, (long) asect);
d24928c0 1447#endif
e621c5cc 1448 }
d24928c0 1449
32090b8e 1450 if (num_sections)
244ffee7 1451 {
32090b8e
KR
1452 if (syms)
1453 syms = (asymbol **) bfd_realloc (abfd, syms,
1454 ((symcount + num_sections + 1)
1455 * sizeof (asymbol *)));
1456 else
1457 syms = (asymbol **) bfd_alloc (abfd,
1c6042ee 1458 (num_sections + 1) * sizeof (asymbol *));
9783e04a
DM
1459 if (!syms)
1460 {
d1ad85a6 1461 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1462 return false;
1463 }
244ffee7 1464
32090b8e
KR
1465 for (asect = abfd->sections; asect; asect = asect->next)
1466 {
d24928c0
KR
1467 if (sect_syms[asect->index])
1468 syms[symcount++] = sect_syms[asect->index];
32090b8e 1469 }
244ffee7 1470
32090b8e
KR
1471 syms[symcount] = (asymbol *) 0;
1472 bfd_set_symtab (abfd, syms, symcount);
1473 }
244ffee7 1474
d24928c0
KR
1475 elf_sym_extra (abfd) = sym_extra
1476 = (Elf_Sym_Extra *) bfd_alloc (abfd, symcount * sizeof (Elf_Sym_Extra));
9783e04a
DM
1477 if (!sym_extra)
1478 {
d1ad85a6 1479 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1480 return false;
1481 }
244ffee7 1482
32090b8e
KR
1483 /* Identify and classify all of the symbols. */
1484 for (idx = 0; idx < symcount; idx++)
244ffee7 1485 {
062189c6 1486 if (!sym_is_global (abfd, syms[idx]))
32090b8e
KR
1487 num_locals++;
1488 else
1489 num_globals++;
244ffee7 1490 }
32090b8e
KR
1491
1492 /* Now provide mapping information. Add +1 for skipping over the
1493 dummy symbol. */
1494 for (idx = 0; idx < symcount; idx++)
244ffee7 1495 {
1c6042ee 1496 syms[idx]->udata = (PTR) & sym_extra[idx];
062189c6 1497 if (!sym_is_global (abfd, syms[idx]))
d24928c0 1498 sym_extra[idx].elf_sym_num = 1 + num_locals2++;
32090b8e 1499 else
d24928c0 1500 sym_extra[idx].elf_sym_num = 1 + num_locals + num_globals2++;
244ffee7
JK
1501 }
1502
32090b8e
KR
1503 elf_num_locals (abfd) = num_locals;
1504 elf_num_globals (abfd) = num_globals;
9783e04a 1505 return true;
32090b8e 1506}
244ffee7 1507
9783e04a
DM
1508static boolean assign_section_numbers ();
1509static boolean assign_file_positions_except_relocs ();
244ffee7 1510
32090b8e 1511static boolean
1c6042ee
ILT
1512elf_compute_section_file_positions (abfd)
1513 bfd *abfd;
32090b8e 1514{
32090b8e 1515 bfd_map_over_sections (abfd, elf_fake_sections, 0);
244ffee7 1516
9783e04a
DM
1517 if (!assign_section_numbers (abfd))
1518 return false;
244ffee7 1519
32090b8e 1520 bfd_map_over_sections (abfd, elf_make_sections, 0);
244ffee7 1521
1c6042ee 1522 bfd_map_over_sections (abfd, fix_up_strtabs, 0); /* .stab/.stabstr &c */
244ffee7 1523
b9d5cdf0
DM
1524 if (swap_out_syms (abfd) == false)
1525 return false;
244ffee7 1526
9783e04a
DM
1527 if (!assign_file_positions_except_relocs (abfd))
1528 return false;
32090b8e
KR
1529
1530 return true;
1531}
1532
1533static boolean
1c6042ee
ILT
1534elf_write_phdrs (abfd, i_ehdrp, i_phdrp, phdr_cnt)
1535 bfd *abfd;
1536 Elf_Internal_Ehdr *i_ehdrp;
1537 Elf_Internal_Phdr *i_phdrp;
1538 unsigned short phdr_cnt;
244ffee7 1539{
32090b8e 1540 /* first program header entry goes after the file header */
300adb31 1541 int outbase = i_ehdrp->e_phoff;
68241b2b 1542 unsigned int i;
32090b8e
KR
1543 Elf_External_Phdr x_phdr;
1544
1545 for (i = 0; i < phdr_cnt; i++)
244ffee7 1546 {
32090b8e 1547 elf_swap_phdr_out (abfd, i_phdrp + i, &x_phdr);
4002f18a
ILT
1548 if (bfd_seek (abfd, outbase, SEEK_SET) != 0
1549 || (bfd_write ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
1550 != sizeof (x_phdr)))
1551 return false;
32090b8e 1552 outbase += sizeof (x_phdr);
244ffee7 1553 }
32090b8e
KR
1554
1555 return true;
244ffee7
JK
1556}
1557
32090b8e
KR
1558static const Elf_Internal_Shdr null_shdr;
1559
1560/* Assign all ELF section numbers. The dummy first section is handled here
1561 too. The link/info pointers for the standard section types are filled
1562 in here too, while we're at it. (Link pointers for .stab sections are
1563 not filled in here.) */
9783e04a 1564static boolean
32090b8e 1565assign_section_numbers (abfd)
fce36137 1566 bfd *abfd;
fce36137 1567{
32090b8e
KR
1568 struct elf_obj_tdata *t = elf_tdata (abfd);
1569 asection *sec;
1570 int section_number = 1;
1571 int i;
1572 Elf_Internal_Shdr **i_shdrp;
244ffee7 1573
1c6042ee
ILT
1574 t->shstrtab_hdr.sh_size = elf_shstrtab (abfd)->length;
1575 t->shstrtab_hdr.contents = (void *) elf_shstrtab (abfd)->tab;
32090b8e 1576 shstrtab_length_fixed = 1;
244ffee7 1577
32090b8e 1578 t->shstrtab_section = section_number++;
1c6042ee 1579 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section;
32090b8e
KR
1580 if (abfd->symcount)
1581 {
1582 t->symtab_section = section_number++;
1583 t->strtab_section = section_number++;
1584 t->symtab_hdr.sh_link = t->strtab_section;
1585 }
1586 for (sec = abfd->sections; sec; sec = sec->next)
1587 {
1588 struct bfd_elf_section_data *d = elf_section_data (sec);
1589 d->this_idx = section_number++;
300adb31 1590 if (sec->flags & SEC_RELOC)
fce36137 1591 {
32090b8e
KR
1592 d->rel_idx = section_number++;
1593 d->rel_hdr.sh_link = t->symtab_section;
1594 d->rel_hdr.sh_info = d->this_idx;
244ffee7 1595 }
fce36137 1596 else
32090b8e
KR
1597 d->rel_idx = 0;
1598 /* No handling for per-section string tables currently. */
1599 }
1c6042ee 1600 elf_elfheader (abfd)->e_shnum = section_number;
32090b8e
KR
1601
1602 /* Set up the list of section header pointers, in agreement with the
1603 indices. */
300adb31
KR
1604 i_shdrp = (Elf_Internal_Shdr **)
1605 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *));
9783e04a
DM
1606 if (!i_shdrp)
1607 {
d1ad85a6 1608 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1609 return false;
1610 }
1c6042ee 1611 elf_elfsections (abfd) = i_shdrp;
32090b8e
KR
1612 for (i = 0; i < section_number; i++)
1613 i_shdrp[i] = 0;
1614
1c6042ee 1615 i_shdrp[0] = (Elf_Internal_Shdr *) & null_shdr;
32090b8e
KR
1616 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr;
1617 if (abfd->symcount)
1618 {
1619 i_shdrp[t->symtab_section] = &t->symtab_hdr;
1620 i_shdrp[t->strtab_section] = &t->strtab_hdr;
244ffee7 1621 }
32090b8e
KR
1622 for (sec = abfd->sections; sec; sec = sec->next)
1623 {
1624 struct bfd_elf_section_data *d = elf_section_data (sec);
1625 i_shdrp[d->this_idx] = &d->this_hdr;
1626 if (d->rel_idx)
1627 i_shdrp[d->rel_idx] = &d->rel_hdr;
1628 }
1629 /* Make sure we got everything.... */
1630 for (i = 0; i < section_number; i++)
1631 if (i_shdrp[i] == 0)
1632 abort ();
9783e04a 1633 return true;
32090b8e
KR
1634}
1635
1636static INLINE file_ptr
1637assign_file_position_for_section (i_shdrp, offset)
1638 Elf_Internal_Shdr *i_shdrp;
1639 file_ptr offset;
1640{
f035cc47
ILT
1641 int align;
1642
1643 if (i_shdrp->sh_addralign != 0)
1644 align = i_shdrp->sh_addralign;
1645 else
1646 align = 1;
1647 i_shdrp->sh_offset = offset = BFD_ALIGN (offset, align);
7b8106b4
ILT
1648 if (i_shdrp->rawdata != NULL)
1649 ((asection *) i_shdrp->rawdata)->filepos = offset;
300adb31
KR
1650 if (i_shdrp->sh_type != SHT_NOBITS)
1651 offset += i_shdrp->sh_size;
32090b8e 1652 return offset;
244ffee7
JK
1653}
1654
01383fb4
KR
1655static INLINE file_ptr
1656align_file_position (off)
1657 file_ptr off;
1658{
f035cc47 1659 return (off + FILE_ALIGN - 1) & ~(FILE_ALIGN - 1);
01383fb4
KR
1660}
1661
300adb31
KR
1662static INLINE file_ptr
1663assign_file_positions_for_symtab_and_strtabs (abfd, off)
1664 bfd *abfd;
1665 file_ptr off;
1666{
1667 struct elf_obj_tdata *t = elf_tdata (abfd);
1668
01383fb4 1669 off = align_file_position (off);
300adb31 1670 off = assign_file_position_for_section (&t->symtab_hdr, off);
01383fb4 1671 off = assign_file_position_for_section (&t->shstrtab_hdr, off);
300adb31
KR
1672 off = assign_file_position_for_section (&t->strtab_hdr, off);
1673 return off;
1674}
1675
1c6042ee
ILT
1676struct seg_info
1677{
300adb31
KR
1678 bfd_vma low, mem_size;
1679 file_ptr file_size;
1680 int start_pos;
1681 int sh_flags;
1682 struct seg_info *next;
1683};
1684
9783e04a 1685static boolean
300adb31
KR
1686map_program_segments (abfd)
1687 bfd *abfd;
1688{
1689 Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
1690 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
1691 Elf_Internal_Shdr *i_shdrp;
1692 Elf_Internal_Phdr *phdr;
80425e6c 1693 char *done = NULL;
68241b2b 1694 unsigned int i, n_left = 0;
300adb31 1695 file_ptr lowest_offset = 0;
2f3189e7 1696 struct seg_info *seg = NULL;
300adb31 1697
80425e6c 1698 done = (char *) malloc (i_ehdrp->e_shnum);
25057836 1699 if (done == NULL && i_ehdrp->e_shnum != 0)
80425e6c
JK
1700 {
1701 bfd_set_error (bfd_error_no_memory);
1702 goto error_return;
1703 }
300adb31 1704 memset (done, 0, i_ehdrp->e_shnum);
062189c6 1705 for (i = 1; i < i_ehdrp->e_shnum; i++)
300adb31
KR
1706 {
1707 i_shdrp = i_shdrpp[i];
1708 /* If it's going to be mapped in, it's been assigned a position. */
1709 if (i_shdrp->sh_offset + 1 == 0)
1710 {
1711 /* Well, not really, but we won't process it here. */
1712 done[i] = 1;
1713 continue;
1714 }
1715 if (i_shdrp->sh_offset < lowest_offset
1716 || lowest_offset == 0)
1717 lowest_offset = i_shdrp->sh_offset;
1718 /* Only interested in PROGBITS or NOBITS for generating segments. */
1719 switch (i_shdrp->sh_type)
1720 {
1721 case SHT_PROGBITS:
1722 case SHT_NOBITS:
1723 break;
1724 default:
1725 done[i] = 1;
1726 }
1727 if (!done[i])
1728 n_left++;
1729 }
1730 while (n_left)
1731 {
1732 bfd_vma lowest_vma = -1, high;
1733 int low_sec = 0;
1734 int mem_size;
1735 int file_size = 0;
2f3189e7
ILT
1736 struct seg_info *snew;
1737 struct seg_info **s_ptr;
300adb31
KR
1738
1739 for (i = 1; i < i_ehdrp->e_shnum; i++)
1740 {
1741 i_shdrp = i_shdrpp[i];
1742 if (!done[i] && i_shdrp->sh_addr < lowest_vma)
1743 {
1744 lowest_vma = i_shdrp->sh_addr;
1745 low_sec = i;
1746 }
1747 }
1748 if (low_sec == 0)
1749 abort ();
1750 /* So now we know the lowest vma of any unassigned sections; start
1751 a segment there. */
2f3189e7
ILT
1752 snew = (struct seg_info *) bfd_alloc (abfd, sizeof (struct seg_info));
1753 if (!snew)
1754 {
1755 bfd_set_error (bfd_error_no_memory);
80425e6c 1756 goto error_return;
2f3189e7
ILT
1757 }
1758 s_ptr = &seg;
1759 while (*s_ptr != (struct seg_info *) NULL)
1760 s_ptr = &(*s_ptr)->next;
1761 *s_ptr = snew;
1762 snew->next = NULL;
1763 snew->low = lowest_vma;
300adb31 1764 i_shdrp = i_shdrpp[low_sec];
2f3189e7
ILT
1765 snew->start_pos = i_shdrp->sh_offset;
1766 snew->sh_flags = i_shdrp->sh_flags;
300adb31
KR
1767 done[low_sec] = 1, n_left--;
1768 mem_size = i_shdrp->sh_size;
1769 high = lowest_vma + i_shdrp->sh_size;
1770
1771 if (i_shdrp->sh_type == SHT_PROGBITS)
1772 file_size = i_shdrp->sh_size;
1773
062189c6 1774 for (i = 1; i < i_ehdrp->e_shnum; i++)
300adb31
KR
1775 {
1776 file_ptr f1;
1777
300adb31
KR
1778 if (done[i])
1779 continue;
1780 i_shdrp = i_shdrpp[i];
1781 /* position of next byte on disk */
2f3189e7 1782 f1 = snew->start_pos + file_size;
300adb31
KR
1783 if (i_shdrp->sh_type == SHT_PROGBITS)
1784 {
1785 if (i_shdrp->sh_offset - f1 != i_shdrp->sh_addr - high)
1786 continue;
6c35a16d
ILT
1787 if (file_size != mem_size)
1788 break;
300adb31 1789 }
1c6042ee
ILT
1790 else
1791 /* sh_type == NOBITS */
300adb31
KR
1792 {
1793 /* If the section in question has no contents in the disk
1794 file, we really don't care where it supposedly starts.
1795 But we don't want to bother merging it into this segment
1796 if it doesn't start on this memory page. */
1797 bfd_vma page1, page2;
1798 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1799
2f3189e7 1800 /* page number in address space of current end of snew */
300adb31
KR
1801 page1 = (high - 1 + maxpagesize - 1) / maxpagesize;
1802 /* page number in address space of start of this section */
1803 page2 = (i_shdrp->sh_addr + maxpagesize - 1) / maxpagesize;
1804
1805 if (page1 != page2)
1806 continue;
1807 }
1808 done[i] = 1, n_left--;
1809 if (i_shdrp->sh_type == SHT_PROGBITS)
2f3189e7
ILT
1810 file_size = i_shdrp->sh_offset + i_shdrp->sh_size - snew->start_pos;
1811 mem_size = i_shdrp->sh_addr + i_shdrp->sh_size - snew->low;
300adb31
KR
1812 high = i_shdrp->sh_addr + i_shdrp->sh_size;
1813 i = 0;
1814 }
2f3189e7
ILT
1815 snew->file_size = file_size;
1816 snew->mem_size = mem_size;
300adb31
KR
1817 }
1818 /* Now do something with the list of segments we've built up. */
1819 {
1820 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
1821 struct seg_info *s;
1822 int n_segs = 0;
1823 int sz;
1824
1825 for (s = seg; s; s = s->next)
1826 {
1827 n_segs++;
1828 }
1829 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
1830 sz = sizeof (Elf_External_Phdr) * n_segs;
01383fb4
KR
1831 if (align_file_position (i_ehdrp->e_ehsize) + sz <= lowest_offset)
1832 i_ehdrp->e_phoff = align_file_position (i_ehdrp->e_ehsize);
300adb31
KR
1833 else
1834 {
01383fb4
KR
1835 i_ehdrp->e_phoff = align_file_position (elf_tdata (abfd)->next_file_pos);
1836 elf_tdata (abfd)->next_file_pos = i_ehdrp->e_phoff + sz;
300adb31 1837 }
1c6042ee
ILT
1838 phdr = (Elf_Internal_Phdr *) bfd_alloc (abfd,
1839 n_segs * sizeof (Elf_Internal_Phdr));
9783e04a
DM
1840 if (!phdr)
1841 {
d1ad85a6 1842 bfd_set_error (bfd_error_no_memory);
1c6042ee 1843 abort (); /* FIXME */
9783e04a 1844 }
300adb31
KR
1845 elf_tdata (abfd)->phdr = phdr;
1846 while (seg)
1847 {
1848 phdr->p_type = PT_LOAD; /* only type we really support so far */
1849 phdr->p_offset = seg->start_pos;
1850 phdr->p_vaddr = seg->low;
1851 phdr->p_paddr = 0;
1852 phdr->p_filesz = seg->file_size;
1853 phdr->p_memsz = seg->mem_size;
1854 phdr->p_flags = PF_R;
1c6042ee 1855 phdr->p_align = maxpagesize; /* ? */
300adb31 1856 if (seg->sh_flags & SHF_WRITE)
e621c5cc
ILT
1857 /* SysVr4 ELF docs say "data segments normally have read, write,
1858 and execute permissions." */
1859 phdr->p_flags |= (PF_W | PF_X);
300adb31
KR
1860 if (seg->sh_flags & SHF_EXECINSTR)
1861 phdr->p_flags |= PF_X;
1862 phdr++;
1863 seg = seg->next;
1864 }
1865 i_ehdrp->e_phnum = n_segs;
1866 }
1867 elf_write_phdrs (abfd, i_ehdrp, elf_tdata (abfd)->phdr, i_ehdrp->e_phnum);
80425e6c
JK
1868 if (done != NULL)
1869 free (done);
9783e04a 1870 return true;
1c6042ee 1871error_return:
80425e6c
JK
1872 if (done != NULL)
1873 free (done);
1874 return false;
300adb31
KR
1875}
1876
9783e04a 1877static boolean
32090b8e
KR
1878assign_file_positions_except_relocs (abfd)
1879 bfd *abfd;
244ffee7 1880{
32090b8e
KR
1881 /* For now, we ignore the possibility of having program segments, which
1882 may require some alignment in the file. That'll require padding, and
1883 some interesting calculations to optimize file space usage.
244ffee7 1884
32090b8e
KR
1885 Also, since the application may change the list of relocations for
1886 a given section, we don't figure them in here. We'll put them at the
1887 end of the file, at positions computed during bfd_close.
244ffee7 1888
300adb31
KR
1889 The order, for now: <ehdr> <shdr> <sec1> <sec2> <sec3> ... <rel1> ...
1890 or: <ehdr> <phdr> <sec1> <sec2> ... <shdr> <rel1> ... */
32090b8e 1891
062189c6 1892 struct elf_obj_tdata *t = elf_tdata (abfd);
32090b8e 1893 file_ptr off;
68241b2b 1894 unsigned int i;
32090b8e
KR
1895 Elf_Internal_Shdr **i_shdrpp = elf_elfsections (abfd);
1896 Elf_Internal_Shdr *i_shdrp;
1897 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd);
300adb31 1898 int exec_p = (abfd->flags & EXEC_P) != 0;
6c35a16d 1899 bfd_vma maxpagesize = get_elf_backend_data (abfd)->maxpagesize;
32090b8e 1900
300adb31 1901 /* Everything starts after the ELF file header. */
32090b8e 1902 off = i_ehdrp->e_ehsize;
300adb31
KR
1903
1904 if (!exec_p)
1905 {
1906 /* Section headers. */
01383fb4 1907 off = align_file_position (off);
300adb31
KR
1908 i_ehdrp->e_shoff = off;
1909 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
300adb31
KR
1910 off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
1911 }
062189c6 1912 for (i = 1; i < i_ehdrp->e_shnum; i++)
32090b8e 1913 {
062189c6
ILT
1914 /* The symtab and strtab sections are placed by
1915 assign_file_positions_for_symtab_and_strtabs. */
1916 if (i == t->symtab_section
1917 || i == t->strtab_section
1918 || i == t->shstrtab_section)
1919 continue;
1920
32090b8e
KR
1921 i_shdrp = i_shdrpp[i];
1922 if (i_shdrp->sh_type == SHT_REL || i_shdrp->sh_type == SHT_RELA)
244ffee7 1923 {
32090b8e
KR
1924 i_shdrp->sh_offset = -1;
1925 continue;
244ffee7 1926 }
300adb31
KR
1927 if (exec_p)
1928 {
300adb31
KR
1929 if (maxpagesize == 0)
1930 maxpagesize = 1; /* make the arithmetic work */
1931 /* This isn't necessarily going to give the best packing, if the
1932 segments require padding between them, but since that isn't
1933 usually the case, this'll do. */
1934 if ((i_shdrp->sh_flags & SHF_ALLOC) == 0)
1935 {
1936 i_shdrp->sh_offset = -1;
1937 continue;
1938 }
1939 /* Blindly assume that the segments are ordered optimally. With
1940 the default LD script, they will be. */
6c35a16d 1941 if (i_shdrp->sh_type != SHT_NOBITS)
300adb31 1942 {
6c35a16d
ILT
1943 /* need big unsigned type */
1944 bfd_vma addtl_off;
1945 addtl_off = i_shdrp->sh_addr - off;
1946 addtl_off = addtl_off % maxpagesize;
1947 if (addtl_off)
1948 {
1949 off += addtl_off;
1950 }
300adb31
KR
1951 }
1952 }
32090b8e 1953 off = assign_file_position_for_section (i_shdrp, off);
01383fb4 1954
6c35a16d
ILT
1955 if (exec_p
1956 && i_shdrp->sh_type == SHT_NOBITS
1957 && (i == i_ehdrp->e_shnum
1958 || i_shdrpp[i + 1]->sh_type != SHT_NOBITS))
1959 {
1960 /* Skip to the next page to ensure that when the file is
1961 loaded the bss section is loaded with zeroes. I don't
1962 know if this is required on all platforms, but it
1963 shouldn't really hurt. */
1964 off = BFD_ALIGN (off, maxpagesize);
1965 }
1966
300adb31 1967 if (exec_p
1c6042ee 1968 && get_elf_backend_data (abfd)->maxpagesize > 1
300adb31
KR
1969 && i_shdrp->sh_type == SHT_PROGBITS
1970 && (i_shdrp->sh_flags & SHF_ALLOC)
01383fb4 1971 && (i_shdrp->sh_offset - i_shdrp->sh_addr) % get_elf_backend_data (abfd)->maxpagesize != 0)
300adb31
KR
1972 abort ();
1973 }
1974 if (exec_p)
1975 {
1976 elf_tdata (abfd)->next_file_pos = off;
9783e04a
DM
1977 if (!map_program_segments (abfd))
1978 return false;
300adb31
KR
1979 off = elf_tdata (abfd)->next_file_pos;
1980
1981 /* Section headers. */
01383fb4 1982 off = align_file_position (off);
300adb31
KR
1983 i_ehdrp->e_shoff = off;
1984 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize;
1985
1986 off = assign_file_positions_for_symtab_and_strtabs (abfd, off);
1987
062189c6 1988 for (i = 1; i < i_ehdrp->e_shnum; i++)
300adb31
KR
1989 {
1990 i_shdrp = i_shdrpp[i];
1991 if (i_shdrp->sh_offset + 1 == 0
1992 && i_shdrp->sh_type != SHT_REL
1993 && i_shdrp->sh_type != SHT_RELA)
1994 off = assign_file_position_for_section (i_shdrp, off);
1995 }
244ffee7 1996 }
32090b8e 1997 elf_tdata (abfd)->next_file_pos = off;
9783e04a 1998 return true;
244ffee7
JK
1999}
2000
32090b8e
KR
2001static boolean
2002prep_headers (abfd)
2003 bfd *abfd;
2004{
32090b8e 2005 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
1c6042ee 2006 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */
32090b8e 2007 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
32090b8e 2008 int count;
32090b8e 2009 struct strtab *shstrtab;
244ffee7 2010
32090b8e
KR
2011 i_ehdrp = elf_elfheader (abfd);
2012 i_shdrp = elf_elfsections (abfd);
244ffee7 2013
32090b8e 2014 shstrtab = bfd_new_strtab (abfd);
b9d5cdf0
DM
2015 if (!shstrtab)
2016 return false;
1c6042ee 2017
32090b8e 2018 elf_shstrtab (abfd) = shstrtab;
244ffee7 2019
32090b8e
KR
2020 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0;
2021 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1;
2022 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2;
2023 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3;
244ffee7 2024
32090b8e
KR
2025 i_ehdrp->e_ident[EI_CLASS] = ELFCLASS;
2026 i_ehdrp->e_ident[EI_DATA] =
2027 abfd->xvec->byteorder_big_p ? ELFDATA2MSB : ELFDATA2LSB;
2028 i_ehdrp->e_ident[EI_VERSION] = EV_CURRENT;
244ffee7 2029
32090b8e
KR
2030 for (count = EI_PAD; count < EI_NIDENT; count++)
2031 i_ehdrp->e_ident[count] = 0;
244ffee7 2032
32090b8e
KR
2033 i_ehdrp->e_type = (abfd->flags & EXEC_P) ? ET_EXEC : ET_REL;
2034 switch (bfd_get_arch (abfd))
fce36137 2035 {
32090b8e
KR
2036 case bfd_arch_unknown:
2037 i_ehdrp->e_machine = EM_NONE;
2038 break;
2039 case bfd_arch_sparc:
2040 i_ehdrp->e_machine = EM_SPARC;
2041 /* start-sanitize-v9 */
2042#if ARCH_SIZE == 64
2043 i_ehdrp->e_machine = EM_SPARC64;
2044#endif
2045 /* end-sanitize-v9 */
2046 break;
2047 case bfd_arch_i386:
2048 i_ehdrp->e_machine = EM_386;
2049 break;
2050 case bfd_arch_m68k:
2051 i_ehdrp->e_machine = EM_68K;
2052 break;
2053 case bfd_arch_m88k:
2054 i_ehdrp->e_machine = EM_88K;
2055 break;
2056 case bfd_arch_i860:
2057 i_ehdrp->e_machine = EM_860;
2058 break;
2059 case bfd_arch_mips: /* MIPS Rxxxx */
2060 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */
2061 break;
2062 case bfd_arch_hppa:
2063 i_ehdrp->e_machine = EM_HPPA;
2064 break;
99ec1f66
ILT
2065 case bfd_arch_powerpc:
2066 i_ehdrp->e_machine = EM_CYGNUS_POWERPC;
2067 break;
32090b8e
KR
2068 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */
2069 default:
2070 i_ehdrp->e_machine = EM_NONE;
fce36137 2071 }
32090b8e
KR
2072 i_ehdrp->e_version = EV_CURRENT;
2073 i_ehdrp->e_ehsize = sizeof (Elf_External_Ehdr);
244ffee7 2074
32090b8e
KR
2075 /* no program header, for now. */
2076 i_ehdrp->e_phoff = 0;
2077 i_ehdrp->e_phentsize = 0;
2078 i_ehdrp->e_phnum = 0;
244ffee7 2079
32090b8e
KR
2080 /* each bfd section is section header entry */
2081 i_ehdrp->e_entry = bfd_get_start_address (abfd);
2082 i_ehdrp->e_shentsize = sizeof (Elf_External_Shdr);
244ffee7 2083
32090b8e
KR
2084 /* if we're building an executable, we'll need a program header table */
2085 if (abfd->flags & EXEC_P)
244ffee7 2086 {
300adb31 2087 /* it all happens later */
32090b8e
KR
2088#if 0
2089 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr);
244ffee7 2090
32090b8e
KR
2091 /* elf_build_phdrs() returns a (NULL-terminated) array of
2092 Elf_Internal_Phdrs */
2093 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum);
2094 i_ehdrp->e_phoff = outbase;
2095 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum;
2096#endif
244ffee7 2097 }
32090b8e 2098 else
244ffee7 2099 {
32090b8e
KR
2100 i_ehdrp->e_phentsize = 0;
2101 i_phdrp = 0;
2102 i_ehdrp->e_phoff = 0;
244ffee7
JK
2103 }
2104
32090b8e
KR
2105 elf_tdata (abfd)->symtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2106 ".symtab");
2107 elf_tdata (abfd)->strtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2108 ".strtab");
2109 elf_tdata (abfd)->shstrtab_hdr.sh_name = bfd_add_to_strtab (abfd, shstrtab,
2110 ".shstrtab");
f035cc47 2111 return true;
244ffee7
JK
2112}
2113
b9d5cdf0 2114static boolean
32090b8e
KR
2115swap_out_syms (abfd)
2116 bfd *abfd;
244ffee7 2117{
9783e04a
DM
2118 if (!elf_map_symbols (abfd))
2119 return false;
244ffee7 2120
32090b8e
KR
2121 /* Dump out the symtabs. */
2122 {
2123 int symcount = bfd_get_symcount (abfd);
2124 asymbol **syms = bfd_get_outsymbols (abfd);
2125 struct strtab *stt = bfd_new_strtab (abfd);
2126 Elf_Internal_Shdr *symtab_hdr;
2127 Elf_Internal_Shdr *symstrtab_hdr;
2128 Elf_External_Sym *outbound_syms;
2129 int idx;
244ffee7 2130
b9d5cdf0
DM
2131 if (!stt)
2132 return false;
32090b8e
KR
2133 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2134 symtab_hdr->sh_type = SHT_SYMTAB;
2135 symtab_hdr->sh_entsize = sizeof (Elf_External_Sym);
2136 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1);
2137 symtab_hdr->sh_info = elf_num_locals (abfd) + 1;
244ffee7 2138
062189c6
ILT
2139 /* FIXME: Systems I've checked use 4 byte alignment for .symtab,
2140 but it is possible that there are systems which use a different
2141 alignment. */
2142 symtab_hdr->sh_addralign = 4;
2143
32090b8e
KR
2144 /* see assert in elf_fake_sections that supports this: */
2145 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr;
2146 symstrtab_hdr->sh_type = SHT_STRTAB;
244ffee7 2147
32090b8e
KR
2148 outbound_syms = (Elf_External_Sym *)
2149 bfd_alloc (abfd, (1 + symcount) * sizeof (Elf_External_Sym));
9783e04a
DM
2150 if (!outbound_syms)
2151 {
d1ad85a6 2152 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
2153 return false;
2154 }
32090b8e
KR
2155 /* now generate the data (for "contents") */
2156 {
2157 /* Fill in zeroth symbol and swap it out. */
2158 Elf_Internal_Sym sym;
2159 sym.st_name = 0;
2160 sym.st_value = 0;
2161 sym.st_size = 0;
2162 sym.st_info = 0;
2163 sym.st_other = 0;
2164 sym.st_shndx = SHN_UNDEF;
2165 elf_swap_symbol_out (abfd, &sym, outbound_syms);
244ffee7 2166 }
32090b8e
KR
2167 for (idx = 0; idx < symcount; idx++)
2168 {
2169 Elf_Internal_Sym sym;
2170 bfd_vma value = syms[idx]->value;
244ffee7 2171
32090b8e
KR
2172 if (syms[idx]->flags & BSF_SECTION_SYM)
2173 /* Section symbols have no names. */
2174 sym.st_name = 0;
2175 else
2176 sym.st_name = bfd_add_to_strtab (abfd, stt, syms[idx]->name);
244ffee7 2177
32090b8e 2178 if (bfd_is_com_section (syms[idx]->section))
244ffee7 2179 {
32090b8e
KR
2180 /* ELF common symbols put the alignment into the `value' field,
2181 and the size into the `size' field. This is backwards from
2182 how BFD handles it, so reverse it here. */
2183 sym.st_size = value;
2184 /* Should retrieve this from somewhere... */
2185 sym.st_value = 16;
d4fb8fce
ILT
2186 sym.st_shndx = elf_section_from_bfd_section (abfd,
2187 syms[idx]->section);
244ffee7
JK
2188 }
2189 else
2190 {
32090b8e 2191 asection *sec = syms[idx]->section;
e74034d8 2192 elf_symbol_type *type_ptr;
32090b8e 2193 int shndx;
244ffee7 2194
32090b8e
KR
2195 if (sec->output_section)
2196 {
2197 value += sec->output_offset;
2198 sec = sec->output_section;
2199 }
2200 value += sec->vma;
2201 sym.st_value = value;
e74034d8
KR
2202 type_ptr = elf_symbol_from (abfd, syms[idx]);
2203 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0;
32090b8e
KR
2204 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec);
2205 if (shndx == -1)
2206 {
2207 asection *sec2;
2208 /* Writing this would be a hell of a lot easier if we had
2209 some decent documentation on bfd, and knew what to expect
2210 of the library, and what to demand of applications. For
2211 example, it appears that `objcopy' might not set the
2212 section of a symbol to be a section that is actually in
2213 the output file. */
2214 sec2 = bfd_get_section_by_name (abfd, sec->name);
850584ad 2215 BFD_ASSERT (sec2 != 0);
32090b8e 2216 sym.st_shndx = shndx = elf_section_from_bfd_section (abfd, sec2);
850584ad 2217 BFD_ASSERT (shndx != -1);
32090b8e
KR
2218 }
2219 }
244ffee7 2220
32090b8e 2221 if (bfd_is_com_section (syms[idx]->section))
38a5f510 2222 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_OBJECT);
32090b8e
KR
2223 else if (syms[idx]->section == &bfd_und_section)
2224 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_NOTYPE);
32090b8e
KR
2225 else if (syms[idx]->flags & BSF_SECTION_SYM)
2226 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION);
2227 else if (syms[idx]->flags & BSF_FILE)
2228 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
d24928c0 2229 else
32090b8e 2230 {
d24928c0
KR
2231 int bind = STB_LOCAL;
2232 int type = STT_OBJECT;
2233 unsigned int flags = syms[idx]->flags;
2234
2235 if (flags & BSF_LOCAL)
2236 bind = STB_LOCAL;
2237 else if (flags & BSF_WEAK)
2238 bind = STB_WEAK;
2239 else if (flags & BSF_GLOBAL)
2240 bind = STB_GLOBAL;
2241
2242 if (flags & BSF_FUNCTION)
2243 type = STT_FUNC;
2244
2245 sym.st_info = ELF_ST_INFO (bind, type);
32090b8e 2246 }
244ffee7 2247
32090b8e
KR
2248 sym.st_other = 0;
2249 elf_swap_symbol_out (abfd, &sym,
d24928c0
KR
2250 (outbound_syms
2251 + elf_sym_extra (abfd)[idx].elf_sym_num));
32090b8e
KR
2252 }
2253
2254 symtab_hdr->contents = (PTR) outbound_syms;
2255 symstrtab_hdr->contents = (PTR) stt->tab;
2256 symstrtab_hdr->sh_size = stt->length;
2257 symstrtab_hdr->sh_type = SHT_STRTAB;
2258
2259 symstrtab_hdr->sh_flags = 0;
2260 symstrtab_hdr->sh_addr = 0;
2261 symstrtab_hdr->sh_entsize = 0;
2262 symstrtab_hdr->sh_link = 0;
2263 symstrtab_hdr->sh_info = 0;
062189c6 2264 symstrtab_hdr->sh_addralign = 1;
32090b8e
KR
2265 symstrtab_hdr->size = 0;
2266 }
2267
2268 /* put the strtab out too... */
2269 {
2270 Elf_Internal_Shdr *this_hdr;
2271
1c6042ee 2272 this_hdr = &elf_tdata (abfd)->shstrtab_hdr;
32090b8e
KR
2273 this_hdr->contents = (PTR) elf_shstrtab (abfd)->tab;
2274 this_hdr->sh_size = elf_shstrtab (abfd)->length;
2275 this_hdr->sh_type = SHT_STRTAB;
2276 this_hdr->sh_flags = 0;
2277 this_hdr->sh_addr = 0;
2278 this_hdr->sh_entsize = 0;
062189c6 2279 this_hdr->sh_addralign = 1;
32090b8e
KR
2280 this_hdr->size = 0;
2281 }
b9d5cdf0 2282 return true;
244ffee7
JK
2283}
2284
32090b8e
KR
2285static boolean
2286write_shdrs_and_ehdr (abfd)
2287 bfd *abfd;
244ffee7 2288{
32090b8e
KR
2289 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
2290 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
32090b8e
KR
2291 Elf_External_Shdr *x_shdrp; /* Section header table, external form */
2292 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */
68241b2b 2293 unsigned int count;
32090b8e 2294 struct strtab *shstrtab;
244ffee7 2295
32090b8e
KR
2296 i_ehdrp = elf_elfheader (abfd);
2297 i_shdrp = elf_elfsections (abfd);
2298 shstrtab = elf_shstrtab (abfd);
2299
2300 /* swap the header before spitting it out... */
2301
2302#if DEBUG & 1
2303 elf_debug_file (i_ehdrp);
244ffee7 2304#endif
32090b8e 2305 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr);
4002f18a
ILT
2306 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
2307 || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd)
2308 != sizeof (x_ehdr)))
2309 return false;
244ffee7 2310
32090b8e
KR
2311 /* at this point we've concocted all the ELF sections... */
2312 x_shdrp = (Elf_External_Shdr *)
2313 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum));
2314 if (!x_shdrp)
2315 {
d1ad85a6 2316 bfd_set_error (bfd_error_no_memory);
32090b8e
KR
2317 return false;
2318 }
2319
2320 for (count = 0; count < i_ehdrp->e_shnum; count++)
2321 {
2322#if DEBUG & 2
2323 elf_debug_section (shstrtab->tab + i_shdrp[count]->sh_name, count,
2324 i_shdrp[count]);
244ffee7 2325#endif
32090b8e
KR
2326 elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count);
2327 }
4002f18a
ILT
2328 if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0
2329 || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd)
d909628b 2330 != sizeof (*x_shdrp) * i_ehdrp->e_shnum))
4002f18a
ILT
2331 return false;
2332
32090b8e 2333 /* need to dump the string table too... */
244ffee7 2334
32090b8e
KR
2335 return true;
2336}
244ffee7 2337
32090b8e
KR
2338static void
2339assign_file_positions_for_relocs (abfd)
2340 bfd *abfd;
2341{
1c6042ee 2342 file_ptr off = elf_tdata (abfd)->next_file_pos;
68241b2b 2343 unsigned int i;
32090b8e
KR
2344 Elf_Internal_Shdr **shdrpp = elf_elfsections (abfd);
2345 Elf_Internal_Shdr *shdrp;
1c6042ee 2346 for (i = 1; i < elf_elfheader (abfd)->e_shnum; i++)
32090b8e
KR
2347 {
2348 shdrp = shdrpp[i];
2349 if (shdrp->sh_type != SHT_REL && shdrp->sh_type != SHT_RELA)
2350 continue;
01383fb4 2351 off = align_file_position (off);
32090b8e
KR
2352 off = assign_file_position_for_section (shdrp, off);
2353 }
1c6042ee 2354 elf_tdata (abfd)->next_file_pos = off;
32090b8e 2355}
244ffee7 2356
32090b8e 2357boolean
1c6042ee
ILT
2358NAME(bfd_elf,write_object_contents) (abfd)
2359 bfd *abfd;
32090b8e 2360{
062189c6 2361 struct elf_backend_data *bed = get_elf_backend_data (abfd);
32090b8e
KR
2362 Elf_Internal_Ehdr *i_ehdrp;
2363 Elf_Internal_Shdr **i_shdrp;
68241b2b 2364 unsigned int count;
244ffee7 2365
38a5f510
ILT
2366 /* We don't know how to write dynamic objects. Specifically, we
2367 don't know how to construct the program header. */
2368 if ((abfd->flags & DYNAMIC) != 0)
2369 {
2370 fprintf (stderr, "Writing ELF dynamic objects is not supported\n");
d1ad85a6 2371 bfd_set_error (bfd_error_wrong_format);
38a5f510
ILT
2372 return false;
2373 }
2374
32090b8e
KR
2375 if (abfd->output_has_begun == false)
2376 {
99a6c761
JL
2377 /* Do any elf backend specific processing first. */
2378 if (bed->elf_backend_begin_write_processing)
2379 (*bed->elf_backend_begin_write_processing) (abfd);
2380
5e829a34
JL
2381 if (prep_headers (abfd) == false)
2382 return false;
2383 if (elf_compute_section_file_positions (abfd) == false)
2384 return false;
32090b8e
KR
2385 abfd->output_has_begun = true;
2386 }
244ffee7 2387
32090b8e
KR
2388 i_shdrp = elf_elfsections (abfd);
2389 i_ehdrp = elf_elfheader (abfd);
244ffee7 2390
32090b8e 2391 bfd_map_over_sections (abfd, write_relocs, (PTR) 0);
32090b8e 2392 assign_file_positions_for_relocs (abfd);
244ffee7 2393
32090b8e 2394 /* After writing the headers, we need to write the sections too... */
062189c6 2395 for (count = 1; count < i_ehdrp->e_shnum; count++)
e621c5cc 2396 {
e621c5cc
ILT
2397 if (bed->elf_backend_section_processing)
2398 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]);
2399 if (i_shdrp[count]->contents)
2400 {
4002f18a
ILT
2401 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0
2402 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size,
2403 1, abfd)
2404 != i_shdrp[count]->sh_size))
2405 return false;
e621c5cc
ILT
2406 }
2407 }
062189c6
ILT
2408
2409 if (bed->elf_backend_final_write_processing)
2410 (*bed->elf_backend_final_write_processing) (abfd);
2411
32090b8e
KR
2412 return write_shdrs_and_ehdr (abfd);
2413}
244ffee7 2414
32090b8e
KR
2415/* Given an index of a section, retrieve a pointer to it. Note
2416 that for our purposes, sections are indexed by {1, 2, ...} with
2417 0 being an illegal index. */
244ffee7 2418
32090b8e
KR
2419/* In the original, each ELF section went into exactly one BFD
2420 section. This doesn't really make sense, so we need a real mapping.
2421 The mapping has to hide in the Elf_Internal_Shdr since asection
2422 doesn't have anything like a tdata field... */
244ffee7 2423
32090b8e 2424static struct sec *
1c6042ee
ILT
2425section_from_elf_index (abfd, index)
2426 bfd *abfd;
2427 unsigned int index;
32090b8e
KR
2428{
2429 /* @@ Is bfd_com_section really correct in all the places it could
2430 be returned from this routine? */
244ffee7 2431
32090b8e
KR
2432 if (index == SHN_ABS)
2433 return &bfd_com_section; /* not abs? */
2434 if (index == SHN_COMMON)
2435 return &bfd_com_section;
244ffee7 2436
32090b8e
KR
2437 if (index > elf_elfheader (abfd)->e_shnum)
2438 return 0;
244ffee7
JK
2439
2440 {
32090b8e 2441 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[index];
244ffee7 2442
32090b8e 2443 switch (hdr->sh_type)
244ffee7 2444 {
32090b8e
KR
2445 /* ELF sections that map to BFD sections */
2446 case SHT_PROGBITS:
2447 case SHT_NOBITS:
2448 if (!hdr->rawdata)
2449 bfd_section_from_shdr (abfd, index);
2450 return (struct sec *) hdr->rawdata;
244ffee7 2451
32090b8e
KR
2452 default:
2453 return (struct sec *) &bfd_abs_section;
244ffee7 2454 }
244ffee7 2455 }
32090b8e 2456}
244ffee7 2457
32090b8e
KR
2458/* given a section, search the header to find them... */
2459static int
1c6042ee
ILT
2460elf_section_from_bfd_section (abfd, asect)
2461 bfd *abfd;
2462 struct sec *asect;
32090b8e
KR
2463{
2464 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd);
2465 int index;
2466 Elf_Internal_Shdr *hdr;
2467 int maxindex = elf_elfheader (abfd)->e_shnum;
244ffee7 2468
32090b8e
KR
2469 if (asect == &bfd_abs_section)
2470 return SHN_ABS;
2471 if (asect == &bfd_com_section)
2472 return SHN_COMMON;
2473 if (asect == &bfd_und_section)
2474 return SHN_UNDEF;
244ffee7 2475
32090b8e
KR
2476 for (index = 0; index < maxindex; index++)
2477 {
2478 hdr = i_shdrp[index];
2479 switch (hdr->sh_type)
2480 {
2481 /* ELF sections that map to BFD sections */
2482 case SHT_PROGBITS:
2483 case SHT_NOBITS:
e621c5cc 2484 case SHT_NOTE:
32090b8e
KR
2485 if (hdr->rawdata)
2486 {
2487 if (((struct sec *) (hdr->rawdata)) == asect)
2488 return index;
2489 }
2490 break;
01383fb4
KR
2491
2492 case SHT_STRTAB:
2493 /* fix_up_strtabs will generate STRTAB sections with names
2494 of .stab*str. */
2495 if (!strncmp (asect->name, ".stab", 5)
2496 && !strcmp ("str", asect->name + strlen (asect->name) - 3))
2497 {
2498 if (hdr->rawdata)
2499 {
2500 if (((struct sec *) (hdr->rawdata)) == asect)
2501 return index;
2502 }
2503 break;
2504 }
2505 /* FALL THROUGH */
32090b8e 2506 default:
e621c5cc
ILT
2507 {
2508 struct elf_backend_data *bed = get_elf_backend_data (abfd);
2509
2510 if (bed->elf_backend_section_from_bfd_section)
f035cc47
ILT
2511 {
2512 int retval;
2513
2514 retval = index;
2515 if ((*bed->elf_backend_section_from_bfd_section)
2516 (abfd, hdr, asect, &retval))
2517 return retval;
2518 }
e621c5cc 2519 }
32090b8e
KR
2520 break;
2521 }
2522 }
2523 return -1;
2524}
244ffee7 2525
32090b8e
KR
2526/* given a symbol, return the bfd index for that symbol. */
2527static int
1c6042ee
ILT
2528elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr)
2529 bfd *abfd;
2530 struct symbol_cache_entry **asym_ptr_ptr;
32090b8e
KR
2531{
2532 struct symbol_cache_entry *asym_ptr = *asym_ptr_ptr;
32090b8e 2533 int idx;
d24928c0 2534 flagword flags = asym_ptr->flags;
32090b8e 2535
d24928c0
KR
2536 /* When gas creates relocations against local labels, it creates its
2537 own symbol for the section, but does put the symbol into the
e621c5cc
ILT
2538 symbol chain, so udata is 0. When the linker is generating
2539 relocatable output, this section symbol may be for one of the
2540 input sections rather than the output section. */
d24928c0
KR
2541 if (asym_ptr->udata == (PTR) 0
2542 && (flags & BSF_SECTION_SYM)
e621c5cc
ILT
2543 && asym_ptr->section)
2544 {
2545 int indx;
2546
2547 if (asym_ptr->section->output_section != NULL)
2548 indx = asym_ptr->section->output_section->index;
2549 else
2550 indx = asym_ptr->section->index;
2551 if (elf_section_syms (abfd)[indx])
2552 asym_ptr->udata = elf_section_syms (abfd)[indx]->udata;
01383fb4 2553 }
e621c5cc 2554
d24928c0 2555 if (asym_ptr->udata)
1c6042ee 2556 idx = ((Elf_Sym_Extra *) asym_ptr->udata)->elf_sym_num;
d24928c0 2557 else
32090b8e 2558 {
32090b8e
KR
2559 abort ();
2560 }
244ffee7 2561
32090b8e 2562#if DEBUG & 4
244ffee7 2563 {
244ffee7 2564
32090b8e 2565 fprintf (stderr,
d24928c0 2566 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx %s\n",
1c6042ee 2567 (long) asym_ptr, asym_ptr->name, idx, flags, elf_symbol_flags (flags));
32090b8e
KR
2568 fflush (stderr);
2569 }
2570#endif
2571
2572 return idx;
2573}
2574
2575static boolean
1c6042ee
ILT
2576elf_slurp_symbol_table (abfd, symptrs)
2577 bfd *abfd;
2578 asymbol **symptrs; /* Buffer for generated bfd symbols */
32090b8e 2579{
1c6042ee 2580 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
7d8aaf36 2581 long symcount; /* Number of external ELF symbols */
32090b8e
KR
2582 elf_symbol_type *sym; /* Pointer to current bfd symbol */
2583 elf_symbol_type *symbase; /* Buffer for generated bfd symbols */
2584 Elf_Internal_Sym i_sym;
80425e6c 2585 Elf_External_Sym *x_symp = NULL;
32090b8e
KR
2586
2587 /* this is only valid because there is only one symtab... */
2588 /* FIXME: This is incorrect, there may also be a dynamic symbol
2589 table which is a subset of the full symbol table. We either need
2590 to be prepared to read both (and merge them) or ensure that we
2591 only read the full symbol table. Currently we only get called to
2592 read the full symbol table. -fnf */
244ffee7 2593
32090b8e
KR
2594 /* Read each raw ELF symbol, converting from external ELF form to
2595 internal ELF form, and then using the information to create a
2596 canonical bfd symbol table entry.
244ffee7 2597
32090b8e
KR
2598 Note that we allocate the initial bfd canonical symbol buffer
2599 based on a one-to-one mapping of the ELF symbols to canonical
2600 symbols. We actually use all the ELF symbols, so there will be no
2601 space left over at the end. When we have all the symbols, we
2602 build the caller's pointer vector. */
244ffee7 2603
32090b8e 2604 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
25057836 2605 return false;
244ffee7 2606
32090b8e 2607 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
244ffee7 2608
7d8aaf36
ILT
2609 if (symcount == 0)
2610 sym = symbase = NULL;
2611 else
244ffee7 2612 {
7d8aaf36 2613 long i;
244ffee7 2614
7d8aaf36 2615 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1)
25057836 2616 return false;
7d8aaf36
ILT
2617
2618 symbase = ((elf_symbol_type *)
2619 bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type)));
2620 if (symbase == (elf_symbol_type *) NULL)
32090b8e 2621 {
7d8aaf36
ILT
2622 bfd_set_error (bfd_error_no_memory);
2623 return false;
32090b8e 2624 }
7d8aaf36
ILT
2625 sym = symbase;
2626
2627 /* Temporarily allocate room for the raw ELF symbols. */
2628 x_symp = ((Elf_External_Sym *)
80425e6c 2629 malloc (symcount * sizeof (Elf_External_Sym)));
25057836 2630 if (x_symp == NULL && symcount != 0)
80425e6c
JK
2631 {
2632 bfd_set_error (bfd_error_no_memory);
2633 goto error_return;
2634 }
7d8aaf36
ILT
2635
2636 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd)
2637 != symcount * sizeof (Elf_External_Sym))
25057836 2638 goto error_return;
7d8aaf36
ILT
2639 /* Skip first symbol, which is a null dummy. */
2640 for (i = 1; i < symcount; i++)
32090b8e 2641 {
7d8aaf36
ILT
2642 elf_swap_symbol_in (abfd, x_symp + i, &i_sym);
2643 memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym));
2644#ifdef ELF_KEEP_EXTSYM
2645 memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym));
2646#endif
2647 sym->symbol.the_bfd = abfd;
244ffee7 2648
7d8aaf36
ILT
2649 sym->symbol.name = elf_string_from_elf_section (abfd, hdr->sh_link,
2650 i_sym.st_name);
244ffee7 2651
7d8aaf36 2652 sym->symbol.value = i_sym.st_value;
244ffee7 2653
7d8aaf36
ILT
2654 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERV)
2655 {
2656 sym->symbol.section = section_from_elf_index (abfd,
2657 i_sym.st_shndx);
2658 }
2659 else if (i_sym.st_shndx == SHN_ABS)
2660 {
2661 sym->symbol.section = &bfd_abs_section;
2662 }
2663 else if (i_sym.st_shndx == SHN_COMMON)
2664 {
2665 sym->symbol.section = &bfd_com_section;
2666 /* Elf puts the alignment into the `value' field, and
2667 the size into the `size' field. BFD wants to see the
2668 size in the value field, and doesn't care (at the
2669 moment) about the alignment. */
2670 sym->symbol.value = i_sym.st_size;
2671 }
2672 else if (i_sym.st_shndx == SHN_UNDEF)
2673 {
2674 sym->symbol.section = &bfd_und_section;
2675 }
2676 else
2677 sym->symbol.section = &bfd_abs_section;
300adb31 2678
7d8aaf36 2679 sym->symbol.value -= sym->symbol.section->vma;
244ffee7 2680
7d8aaf36
ILT
2681 switch (ELF_ST_BIND (i_sym.st_info))
2682 {
2683 case STB_LOCAL:
2684 sym->symbol.flags |= BSF_LOCAL;
2685 break;
2686 case STB_GLOBAL:
2687 sym->symbol.flags |= BSF_GLOBAL;
2688 break;
2689 case STB_WEAK:
2690 sym->symbol.flags |= BSF_WEAK;
2691 break;
2692 }
2693
2694 switch (ELF_ST_TYPE (i_sym.st_info))
2695 {
2696 case STT_SECTION:
2697 sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING;
2698 break;
2699 case STT_FILE:
2700 sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING;
2701 break;
2702 case STT_FUNC:
2703 sym->symbol.flags |= BSF_FUNCTION;
2704 break;
2705 }
2706
2707 /* Do some backend-specific processing on this symbol. */
2708 {
2709 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2710 if (ebd->elf_backend_symbol_processing)
2711 (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol);
2712 }
2713
2714 sym++;
2715 }
244ffee7
JK
2716 }
2717
e621c5cc
ILT
2718 /* Do some backend-specific processing on this symbol table. */
2719 {
2720 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2721 if (ebd->elf_backend_symbol_table_processing)
2722 (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount);
2723 }
244ffee7 2724
e621c5cc 2725 /* We rely on the zalloc to clear out the final symbol entry. */
244ffee7 2726
32090b8e
KR
2727 bfd_get_symcount (abfd) = symcount = sym - symbase;
2728
2729 /* Fill in the user's symbol pointer vector if needed. */
2730 if (symptrs)
244ffee7 2731 {
32090b8e
KR
2732 sym = symbase;
2733 while (symcount-- > 0)
244ffee7 2734 {
32090b8e
KR
2735 *symptrs++ = &sym->symbol;
2736 sym++;
244ffee7 2737 }
32090b8e 2738 *symptrs = 0; /* Final null pointer */
244ffee7
JK
2739 }
2740
80425e6c
JK
2741 if (x_symp != NULL)
2742 free (x_symp);
244ffee7 2743 return true;
1c6042ee 2744error_return:
80425e6c
JK
2745 if (x_symp != NULL)
2746 free (x_symp);
2747 return false;
244ffee7
JK
2748}
2749
32090b8e 2750/* Return the number of bytes required to hold the symtab vector.
244ffee7 2751
32090b8e
KR
2752 Note that we base it on the count plus 1, since we will null terminate
2753 the vector allocated based on this size. However, the ELF symbol table
2754 always has a dummy entry as symbol #0, so it ends up even. */
244ffee7 2755
326e32d7 2756long
1c6042ee
ILT
2757elf_get_symtab_upper_bound (abfd)
2758 bfd *abfd;
244ffee7 2759{
326e32d7
ILT
2760 long symcount;
2761 long symtab_size;
1c6042ee 2762 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr;
326e32d7 2763
32090b8e 2764 symcount = hdr->sh_size / sizeof (Elf_External_Sym);
d6439785 2765 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *));
244ffee7 2766
32090b8e
KR
2767 return symtab_size;
2768}
244ffee7 2769
32090b8e
KR
2770/*
2771 This function return the number of bytes required to store the
2772 relocation information associated with section <<sect>>
2773 attached to bfd <<abfd>>
244ffee7 2774
32090b8e 2775*/
326e32d7 2776long
32090b8e
KR
2777elf_get_reloc_upper_bound (abfd, asect)
2778 bfd *abfd;
2779 sec_ptr asect;
2780{
2781 if (asect->flags & SEC_RELOC)
2782 {
2783 /* either rel or rela */
1c6042ee 2784 return elf_section_data (asect)->rel_hdr.sh_size;
32090b8e
KR
2785 }
2786 else
2787 return 0;
244ffee7
JK
2788}
2789
32090b8e 2790static boolean
1c6042ee
ILT
2791elf_slurp_reloca_table (abfd, asect, symbols)
2792 bfd *abfd;
2793 sec_ptr asect;
2794 asymbol **symbols;
244ffee7 2795{
32090b8e
KR
2796 Elf_External_Rela *native_relocs;
2797 arelent *reloc_cache;
2798 arelent *cache_ptr;
244ffee7 2799
32090b8e 2800 unsigned int idx;
244ffee7 2801
32090b8e
KR
2802 if (asect->relocation)
2803 return true;
2804 if (asect->reloc_count == 0)
2805 return true;
2806 if (asect->flags & SEC_CONSTRUCTOR)
2807 return true;
244ffee7 2808
4002f18a
ILT
2809 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2810 return false;
32090b8e
KR
2811 native_relocs = (Elf_External_Rela *)
2812 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rela));
9783e04a 2813 if (!native_relocs)
9783e04a 2814 {
d1ad85a6 2815 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
2816 return false;
2817 }
4002f18a
ILT
2818 if (bfd_read ((PTR) native_relocs,
2819 sizeof (Elf_External_Rela), asect->reloc_count, abfd)
2820 != sizeof (Elf_External_Rela) * asect->reloc_count)
2821 return false;
244ffee7 2822
32090b8e
KR
2823 reloc_cache = (arelent *)
2824 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2825
2826 if (!reloc_cache)
6a3eb9b6 2827 {
d1ad85a6 2828 bfd_set_error (bfd_error_no_memory);
32090b8e 2829 return false;
6a3eb9b6 2830 }
244ffee7 2831
32090b8e
KR
2832 for (idx = 0; idx < asect->reloc_count; idx++)
2833 {
32090b8e
KR
2834 Elf_Internal_Rela dst;
2835 Elf_External_Rela *src;
244ffee7 2836
32090b8e
KR
2837 cache_ptr = reloc_cache + idx;
2838 src = native_relocs + idx;
2839 elf_swap_reloca_in (abfd, src, &dst);
244ffee7 2840
d24928c0 2841#ifdef RELOC_PROCESSING
32090b8e
KR
2842 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
2843#else
32090b8e
KR
2844 if (asect->flags & SEC_RELOC)
2845 {
2846 /* relocatable, so the offset is off of the section */
2847 cache_ptr->address = dst.r_offset + asect->vma;
2848 }
2849 else
2850 {
2851 /* non-relocatable, so the offset a virtual address */
2852 cache_ptr->address = dst.r_offset;
2853 }
7b8106b4
ILT
2854
2855 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
2856 of zero points to the dummy symbol, which was not read into
2857 the symbol table SYMBOLS. */
2858 if (ELF_R_SYM (dst.r_info) == 0)
2859 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2860 else
2861 {
2862 asymbol *s;
2863
2864 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
2865
2866 /* Translate any ELF section symbol into a BFD section
2867 symbol. */
2868 s = *(cache_ptr->sym_ptr_ptr);
2869 if (s->flags & BSF_SECTION_SYM)
2870 {
2871 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
2872 s = *cache_ptr->sym_ptr_ptr;
2873 if (s->name == 0 || s->name[0] == 0)
2874 abort ();
2875 }
2876 }
32090b8e 2877 cache_ptr->addend = dst.r_addend;
244ffee7 2878
32090b8e
KR
2879 /* Fill in the cache_ptr->howto field from dst.r_type */
2880 {
2881 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
2882 (*ebd->elf_info_to_howto) (abfd, cache_ptr, &dst);
2883 }
2884#endif
2885 }
244ffee7 2886
32090b8e
KR
2887 asect->relocation = reloc_cache;
2888 return true;
2889}
238ac6ec 2890
32090b8e
KR
2891#ifdef DEBUG
2892static void
2893elf_debug_section (str, num, hdr)
2894 char *str;
2895 int num;
2896 Elf_Internal_Shdr *hdr;
2897{
2898 fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, str, (long) hdr);
2899 fprintf (stderr,
2900 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n",
2901 (long) hdr->sh_name,
2902 (long) hdr->sh_type,
2903 (long) hdr->sh_flags);
2904 fprintf (stderr,
2905 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n",
2906 (long) hdr->sh_addr,
2907 (long) hdr->sh_offset,
2908 (long) hdr->sh_size);
2909 fprintf (stderr,
2910 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n",
2911 (long) hdr->sh_link,
2912 (long) hdr->sh_info,
2913 (long) hdr->sh_addralign);
2914 fprintf (stderr, "sh_entsize = %ld\n",
2915 (long) hdr->sh_entsize);
2916 fprintf (stderr, "rawdata = 0x%.8lx\n", (long) hdr->rawdata);
2917 fprintf (stderr, "contents = 0x%.8lx\n", (long) hdr->contents);
2918 fprintf (stderr, "size = %ld\n", (long) hdr->size);
2919 fflush (stderr);
2920}
244ffee7 2921
32090b8e
KR
2922static void
2923elf_debug_file (ehdrp)
2924 Elf_Internal_Ehdr *ehdrp;
2925{
2926 fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry);
2927 fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff);
2928 fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum);
2929 fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize);
2930 fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff);
2931 fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum);
2932 fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize);
244ffee7 2933}
32090b8e 2934#endif
244ffee7
JK
2935
2936static boolean
1c6042ee
ILT
2937elf_slurp_reloc_table (abfd, asect, symbols)
2938 bfd *abfd;
2939 sec_ptr asect;
2940 asymbol **symbols;
244ffee7 2941{
32090b8e
KR
2942 Elf_External_Rel *native_relocs;
2943 arelent *reloc_cache;
2944 arelent *cache_ptr;
2945 Elf_Internal_Shdr *data_hdr;
25677b5b
PS
2946 bfd_vma data_off;
2947 unsigned long data_max;
32090b8e 2948 char buf[4]; /* FIXME -- might be elf64 */
244ffee7 2949
32090b8e 2950 unsigned int idx;
244ffee7 2951
32090b8e
KR
2952 if (asect->relocation)
2953 return true;
2954 if (asect->reloc_count == 0)
2955 return true;
2956 if (asect->flags & SEC_CONSTRUCTOR)
2957 return true;
244ffee7 2958
4002f18a
ILT
2959 if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
2960 return false;
32090b8e
KR
2961 native_relocs = (Elf_External_Rel *)
2962 bfd_alloc (abfd, asect->reloc_count * sizeof (Elf_External_Rel));
9783e04a
DM
2963 if (!native_relocs)
2964 {
d1ad85a6 2965 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
2966 return false;
2967 }
4002f18a
ILT
2968 if (bfd_read ((PTR) native_relocs,
2969 sizeof (Elf_External_Rel), asect->reloc_count, abfd)
2970 != sizeof (Elf_External_Rel) * asect->reloc_count)
2971 return false;
244ffee7 2972
32090b8e
KR
2973 reloc_cache = (arelent *)
2974 bfd_alloc (abfd, (size_t) (asect->reloc_count * sizeof (arelent)));
2975
2976 if (!reloc_cache)
244ffee7 2977 {
d1ad85a6 2978 bfd_set_error (bfd_error_no_memory);
244ffee7
JK
2979 return false;
2980 }
2981
32090b8e
KR
2982 /* Get the offset of the start of the segment we are relocating to read in
2983 the implicit addend. */
1c6042ee 2984 data_hdr = &elf_section_data (asect)->this_hdr;
32090b8e
KR
2985 data_off = data_hdr->sh_offset;
2986 data_max = data_hdr->sh_size - sizeof (buf) + 1;
244ffee7 2987
32090b8e
KR
2988#if DEBUG & 2
2989 elf_debug_section ("data section", -1, data_hdr);
2990#endif
244ffee7 2991
32090b8e 2992 for (idx = 0; idx < asect->reloc_count; idx++)
244ffee7 2993 {
32090b8e
KR
2994#ifdef RELOC_PROCESSING
2995 Elf_Internal_Rel dst;
2996 Elf_External_Rel *src;
244ffee7 2997
32090b8e
KR
2998 cache_ptr = reloc_cache + idx;
2999 src = native_relocs + idx;
3000 elf_swap_reloc_in (abfd, src, &dst);
244ffee7 3001
32090b8e
KR
3002 RELOC_PROCESSING (cache_ptr, &dst, symbols, abfd, asect);
3003#else
3004 Elf_Internal_Rel dst;
3005 Elf_External_Rel *src;
6a3eb9b6 3006
32090b8e
KR
3007 cache_ptr = reloc_cache + idx;
3008 src = native_relocs + idx;
3009
3010 elf_swap_reloc_in (abfd, src, &dst);
3011
3012 if (asect->flags & SEC_RELOC)
244ffee7 3013 {
32090b8e
KR
3014 /* relocatable, so the offset is off of the section */
3015 cache_ptr->address = dst.r_offset + asect->vma;
244ffee7 3016 }
32090b8e 3017 else
244ffee7 3018 {
32090b8e
KR
3019 /* non-relocatable, so the offset a virtual address */
3020 cache_ptr->address = dst.r_offset;
244ffee7 3021 }
7b8106b4
ILT
3022
3023 /* ELF_R_SYM(dst.r_info) is the symbol table offset. An offset
3024 of zero points to the dummy symbol, which was not read into
3025 the symbol table SYMBOLS. */
3026 if (ELF_R_SYM (dst.r_info) == 0)
3027 cache_ptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
3028 else
3029 {
3030 asymbol *s;
3031
3032 cache_ptr->sym_ptr_ptr = symbols + ELF_R_SYM (dst.r_info) - 1;
3033
3034 /* Translate any ELF section symbol into a BFD section
3035 symbol. */
3036 s = *(cache_ptr->sym_ptr_ptr);
3037 if (s->flags & BSF_SECTION_SYM)
3038 {
3039 cache_ptr->sym_ptr_ptr = s->section->symbol_ptr_ptr;
3040 s = *cache_ptr->sym_ptr_ptr;
3041 if (s->name == 0 || s->name[0] == 0)
3042 abort ();
3043 }
3044 }
32090b8e 3045 BFD_ASSERT (dst.r_offset <= data_max);
d24928c0 3046 cache_ptr->addend = 0;
244ffee7 3047
32090b8e
KR
3048 /* Fill in the cache_ptr->howto field from dst.r_type */
3049 {
3050 struct elf_backend_data *ebd = get_elf_backend_data (abfd);
3051 (*ebd->elf_info_to_howto_rel) (abfd, cache_ptr, &dst);
3052 }
3053#endif
3054 }
244ffee7 3055
32090b8e
KR
3056 asect->relocation = reloc_cache;
3057 return true;
3058}
244ffee7 3059
326e32d7 3060long
32090b8e
KR
3061elf_canonicalize_reloc (abfd, section, relptr, symbols)
3062 bfd *abfd;
3063 sec_ptr section;
3064 arelent **relptr;
3065 asymbol **symbols;
3066{
3067 arelent *tblptr = section->relocation;
3068 unsigned int count = 0;
3069 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p;
3070
3071 /* snarfed from coffcode.h */
3072 if (use_rela_p)
326e32d7
ILT
3073 {
3074 if (! elf_slurp_reloca_table (abfd, section, symbols))
3075 return -1;
3076 }
32090b8e 3077 else
326e32d7
ILT
3078 {
3079 if (! elf_slurp_reloc_table (abfd, section, symbols))
3080 return -1;
3081 }
32090b8e
KR
3082
3083 tblptr = section->relocation;
32090b8e
KR
3084
3085 for (; count++ < section->reloc_count;)
3086 *relptr++ = tblptr++;
3087
3088 *relptr = 0;
3089 return section->reloc_count;
3090}
3091
326e32d7 3092long
1c6042ee
ILT
3093elf_get_symtab (abfd, alocation)
3094 bfd *abfd;
3095 asymbol **alocation;
32090b8e 3096{
32090b8e 3097 if (!elf_slurp_symbol_table (abfd, alocation))
326e32d7
ILT
3098 return -1;
3099
3100 return bfd_get_symcount (abfd);
32090b8e
KR
3101}
3102
3103asymbol *
1c6042ee
ILT
3104elf_make_empty_symbol (abfd)
3105 bfd *abfd;
32090b8e
KR
3106{
3107 elf_symbol_type *newsym;
3108
3109 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type));
3110 if (!newsym)
3111 {
d1ad85a6 3112 bfd_set_error (bfd_error_no_memory);
32090b8e
KR
3113 return NULL;
3114 }
3115 else
3116 {
3117 newsym->symbol.the_bfd = abfd;
3118 return &newsym->symbol;
244ffee7 3119 }
32090b8e 3120}
244ffee7 3121
32090b8e 3122void
1c6042ee
ILT
3123elf_get_symbol_info (ignore_abfd, symbol, ret)
3124 bfd *ignore_abfd;
3125 asymbol *symbol;
3126 symbol_info *ret;
32090b8e
KR
3127{
3128 bfd_symbol_info (symbol, ret);
3129}
244ffee7 3130
32090b8e 3131void
1c6042ee
ILT
3132elf_print_symbol (ignore_abfd, filep, symbol, how)
3133 bfd *ignore_abfd;
3134 PTR filep;
3135 asymbol *symbol;
3136 bfd_print_symbol_type how;
32090b8e
KR
3137{
3138 FILE *file = (FILE *) filep;
3139 switch (how)
3140 {
3141 case bfd_print_symbol_name:
3142 fprintf (file, "%s", symbol->name);
3143 break;
3144 case bfd_print_symbol_more:
3145 fprintf (file, "elf ");
3146 fprintf_vma (file, symbol->value);
3147 fprintf (file, " %lx", (long) symbol->flags);
3148 break;
3149 case bfd_print_symbol_all:
3150 {
3151 CONST char *section_name;
3152 section_name = symbol->section ? symbol->section->name : "(*none*)";
3153 bfd_print_symbol_vandf ((PTR) file, symbol);
3154 fprintf (file, " %s\t%s",
3155 section_name,
3156 symbol->name);
3157 }
3158 break;
3159 }
244ffee7 3160
32090b8e 3161}
244ffee7 3162
32090b8e 3163alent *
1c6042ee
ILT
3164elf_get_lineno (ignore_abfd, symbol)
3165 bfd *ignore_abfd;
3166 asymbol *symbol;
32090b8e
KR
3167{
3168 fprintf (stderr, "elf_get_lineno unimplemented\n");
3169 fflush (stderr);
3170 BFD_FAIL ();
3171 return NULL;
3172}
3173
3174boolean
1c6042ee
ILT
3175elf_set_arch_mach (abfd, arch, machine)
3176 bfd *abfd;
3177 enum bfd_architecture arch;
3178 unsigned long machine;
32090b8e
KR
3179{
3180 /* Allow any architecture to be supported by the elf backend */
3181 switch (arch)
244ffee7 3182 {
32090b8e
KR
3183 case bfd_arch_unknown: /* EM_NONE */
3184 case bfd_arch_sparc: /* EM_SPARC */
1c6042ee
ILT
3185 case bfd_arch_i386: /* EM_386 */
3186 case bfd_arch_m68k: /* EM_68K */
3187 case bfd_arch_m88k: /* EM_88K */
3188 case bfd_arch_i860: /* EM_860 */
3189 case bfd_arch_mips: /* EM_MIPS (MIPS R3000) */
3190 case bfd_arch_hppa: /* EM_HPPA (HP PA_RISC) */
99ec1f66 3191 case bfd_arch_powerpc: /* EM_CYGNUS_POWERPC */
32090b8e
KR
3192 return bfd_default_set_arch_mach (abfd, arch, machine);
3193 default:
3194 return false;
244ffee7 3195 }
32090b8e 3196}
244ffee7 3197
32090b8e 3198boolean
1c6042ee
ILT
3199elf_find_nearest_line (abfd,
3200 section,
3201 symbols,
3202 offset,
3203 filename_ptr,
3204 functionname_ptr,
3205 line_ptr)
3206 bfd *abfd;
3207 asection *section;
3208 asymbol **symbols;
3209 bfd_vma offset;
3210 CONST char **filename_ptr;
3211 CONST char **functionname_ptr;
3212 unsigned int *line_ptr;
32090b8e
KR
3213{
3214 return false;
244ffee7
JK
3215}
3216
32090b8e 3217int
1c6042ee
ILT
3218elf_sizeof_headers (abfd, reloc)
3219 bfd *abfd;
3220 boolean reloc;
32090b8e
KR
3221{
3222 fprintf (stderr, "elf_sizeof_headers unimplemented\n");
3223 fflush (stderr);
3224 BFD_FAIL ();
3225 return 0;
3226}
244ffee7 3227
32090b8e 3228boolean
1c6042ee
ILT
3229elf_set_section_contents (abfd, section, location, offset, count)
3230 bfd *abfd;
3231 sec_ptr section;
3232 PTR location;
3233 file_ptr offset;
3234 bfd_size_type count;
244ffee7 3235{
244ffee7
JK
3236 Elf_Internal_Shdr *hdr;
3237
32090b8e 3238 if (abfd->output_has_begun == false) /* set by bfd.c handler? */
244ffee7 3239 {
99a6c761
JL
3240 struct elf_backend_data *bed = get_elf_backend_data (abfd);
3241
3242 /* Do any elf backend specific processing first. */
3243 if (bed->elf_backend_begin_write_processing)
3244 (*bed->elf_backend_begin_write_processing) (abfd);
3245
32090b8e 3246 /* do setup calculations (FIXME) */
5e829a34
JL
3247 if (prep_headers (abfd) == false)
3248 return false;
3249 if (elf_compute_section_file_positions (abfd) == false)
3250 return false;
32090b8e 3251 abfd->output_has_begun = true;
244ffee7 3252 }
244ffee7 3253
1c6042ee 3254 hdr = &elf_section_data (section)->this_hdr;
244ffee7 3255
32090b8e
KR
3256 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1)
3257 return false;
3258 if (bfd_write (location, 1, count, abfd) != count)
3259 return false;
3260
3261 return true;
3262}
3263
3264void
1c6042ee
ILT
3265elf_no_info_to_howto (abfd, cache_ptr, dst)
3266 bfd *abfd;
3267 arelent *cache_ptr;
3268 Elf_Internal_Rela *dst;
244ffee7 3269{
32090b8e
KR
3270 fprintf (stderr, "elf RELA relocation support for target machine unimplemented\n");
3271 fflush (stderr);
3272 BFD_FAIL ();
244ffee7
JK
3273}
3274
32090b8e 3275void
1c6042ee
ILT
3276elf_no_info_to_howto_rel (abfd, cache_ptr, dst)
3277 bfd *abfd;
3278 arelent *cache_ptr;
3279 Elf_Internal_Rel *dst;
244ffee7 3280{
32090b8e
KR
3281 fprintf (stderr, "elf REL relocation support for target machine unimplemented\n");
3282 fflush (stderr);
3283 BFD_FAIL ();
3284}
32090b8e 3285\f
1c6042ee 3286
32090b8e 3287/* Core file support */
244ffee7 3288
32090b8e
KR
3289#ifdef HAVE_PROCFS /* Some core file support requires host /proc files */
3290#include <sys/procfs.h>
3291#else
3292#define bfd_prstatus(abfd, descdata, descsz, filepos) /* Define away */
3293#define bfd_fpregset(abfd, descdata, descsz, filepos) /* Define away */
3294#define bfd_prpsinfo(abfd, descdata, descsz, filepos) /* Define away */
3295#endif
244ffee7 3296
32090b8e 3297#ifdef HAVE_PROCFS
244ffee7 3298
32090b8e 3299static void
1c6042ee
ILT
3300bfd_prstatus (abfd, descdata, descsz, filepos)
3301 bfd *abfd;
3302 char *descdata;
3303 int descsz;
3304 long filepos;
32090b8e
KR
3305{
3306 asection *newsect;
3307 prstatus_t *status = (prstatus_t *) 0;
244ffee7 3308
32090b8e 3309 if (descsz == sizeof (prstatus_t))
244ffee7 3310 {
32090b8e
KR
3311 newsect = bfd_make_section (abfd, ".reg");
3312 newsect->_raw_size = sizeof (status->pr_reg);
3313 newsect->filepos = filepos + (long) &status->pr_reg;
3314 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3315 newsect->alignment_power = 2;
3316 if ((core_prstatus (abfd) = bfd_alloc (abfd, descsz)) != NULL)
3317 {
3318 memcpy (core_prstatus (abfd), descdata, descsz);
3319 }
244ffee7 3320 }
32090b8e 3321}
244ffee7 3322
32090b8e 3323/* Stash a copy of the prpsinfo structure away for future use. */
244ffee7 3324
32090b8e 3325static void
1c6042ee
ILT
3326bfd_prpsinfo (abfd, descdata, descsz, filepos)
3327 bfd *abfd;
3328 char *descdata;
3329 int descsz;
3330 long filepos;
32090b8e
KR
3331{
3332 asection *newsect;
244ffee7 3333
32090b8e
KR
3334 if (descsz == sizeof (prpsinfo_t))
3335 {
3336 if ((core_prpsinfo (abfd) = bfd_alloc (abfd, descsz)) != NULL)
244ffee7 3337 {
32090b8e 3338 memcpy (core_prpsinfo (abfd), descdata, descsz);
244ffee7 3339 }
244ffee7 3340 }
244ffee7
JK
3341}
3342
244ffee7 3343static void
1c6042ee
ILT
3344bfd_fpregset (abfd, descdata, descsz, filepos)
3345 bfd *abfd;
3346 char *descdata;
3347 int descsz;
3348 long filepos;
244ffee7 3349{
32090b8e 3350 asection *newsect;
244ffee7 3351
32090b8e
KR
3352 newsect = bfd_make_section (abfd, ".reg2");
3353 newsect->_raw_size = descsz;
3354 newsect->filepos = filepos;
3355 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3356 newsect->alignment_power = 2;
6a3eb9b6 3357}
244ffee7 3358
32090b8e
KR
3359#endif /* HAVE_PROCFS */
3360
3361/* Return a pointer to the args (including the command name) that were
3362 seen by the program that generated the core dump. Note that for
3363 some reason, a spurious space is tacked onto the end of the args
3364 in some (at least one anyway) implementations, so strip it off if
3365 it exists. */
3366
3367char *
1c6042ee
ILT
3368elf_core_file_failing_command (abfd)
3369 bfd *abfd;
244ffee7 3370{
32090b8e
KR
3371#ifdef HAVE_PROCFS
3372 if (core_prpsinfo (abfd))
3373 {
3374 prpsinfo_t *p = core_prpsinfo (abfd);
3375 char *scan = p->pr_psargs;
3376 while (*scan++)
3377 {;
3378 }
3379 scan -= 2;
3380 if ((scan > p->pr_psargs) && (*scan == ' '))
3381 {
3382 *scan = '\000';
3383 }
3384 return p->pr_psargs;
3385 }
3386#endif
3387 return NULL;
3388}
244ffee7 3389
32090b8e
KR
3390/* Return the number of the signal that caused the core dump. Presumably,
3391 since we have a core file, we got a signal of some kind, so don't bother
3392 checking the other process status fields, just return the signal number.
3393 */
244ffee7 3394
32090b8e 3395int
1c6042ee
ILT
3396elf_core_file_failing_signal (abfd)
3397 bfd *abfd;
32090b8e
KR
3398{
3399#ifdef HAVE_PROCFS
3400 if (core_prstatus (abfd))
3401 {
3402 return ((prstatus_t *) (core_prstatus (abfd)))->pr_cursig;
3403 }
3404#endif
3405 return -1;
3406}
244ffee7 3407
32090b8e
KR
3408/* Check to see if the core file could reasonably be expected to have
3409 come for the current executable file. Note that by default we return
3410 true unless we find something that indicates that there might be a
3411 problem.
3412 */
244ffee7 3413
32090b8e 3414boolean
1c6042ee
ILT
3415elf_core_file_matches_executable_p (core_bfd, exec_bfd)
3416 bfd *core_bfd;
3417 bfd *exec_bfd;
32090b8e
KR
3418{
3419#ifdef HAVE_PROCFS
3420 char *corename;
3421 char *execname;
3422#endif
244ffee7 3423
32090b8e
KR
3424 /* First, xvecs must match since both are ELF files for the same target. */
3425
3426 if (core_bfd->xvec != exec_bfd->xvec)
244ffee7 3427 {
d1ad85a6 3428 bfd_set_error (bfd_error_system_call);
244ffee7
JK
3429 return false;
3430 }
3431
32090b8e 3432#ifdef HAVE_PROCFS
244ffee7 3433
32090b8e
KR
3434 /* If no prpsinfo, just return true. Otherwise, grab the last component
3435 of the exec'd pathname from the prpsinfo. */
244ffee7 3436
32090b8e 3437 if (core_prpsinfo (core_bfd))
244ffee7 3438 {
32090b8e
KR
3439 corename = (((struct prpsinfo *) core_prpsinfo (core_bfd))->pr_fname);
3440 }
3441 else
3442 {
3443 return true;
3444 }
244ffee7 3445
32090b8e 3446 /* Find the last component of the executable pathname. */
244ffee7 3447
32090b8e
KR
3448 if ((execname = strrchr (exec_bfd->filename, '/')) != NULL)
3449 {
3450 execname++;
3451 }
3452 else
3453 {
3454 execname = (char *) exec_bfd->filename;
3455 }
244ffee7 3456
32090b8e 3457 /* See if they match */
244ffee7 3458
32090b8e 3459 return strcmp (execname, corename) ? false : true;
244ffee7 3460
32090b8e 3461#else
244ffee7 3462
244ffee7 3463 return true;
244ffee7 3464
32090b8e
KR
3465#endif /* HAVE_PROCFS */
3466}
244ffee7 3467
32090b8e
KR
3468/* ELF core files contain a segment of type PT_NOTE, that holds much of
3469 the information that would normally be available from the /proc interface
3470 for the process, at the time the process dumped core. Currently this
3471 includes copies of the prstatus, prpsinfo, and fpregset structures.
244ffee7 3472
32090b8e
KR
3473 Since these structures are potentially machine dependent in size and
3474 ordering, bfd provides two levels of support for them. The first level,
3475 available on all machines since it does not require that the host
3476 have /proc support or the relevant include files, is to create a bfd
3477 section for each of the prstatus, prpsinfo, and fpregset structures,
3478 without any interpretation of their contents. With just this support,
3479 the bfd client will have to interpret the structures itself. Even with
3480 /proc support, it might want these full structures for it's own reasons.
244ffee7 3481
32090b8e
KR
3482 In the second level of support, where HAVE_PROCFS is defined, bfd will
3483 pick apart the structures to gather some additional information that
3484 clients may want, such as the general register set, the name of the
3485 exec'ed file and its arguments, the signal (if any) that caused the
3486 core dump, etc.
244ffee7 3487
32090b8e 3488 */
244ffee7 3489
32090b8e 3490static boolean
1c6042ee
ILT
3491elf_corefile_note (abfd, hdr)
3492 bfd *abfd;
3493 Elf_Internal_Phdr *hdr;
244ffee7 3494{
32090b8e
KR
3495 Elf_External_Note *x_note_p; /* Elf note, external form */
3496 Elf_Internal_Note i_note; /* Elf note, internal form */
3497 char *buf = NULL; /* Entire note segment contents */
3498 char *namedata; /* Name portion of the note */
3499 char *descdata; /* Descriptor portion of the note */
3500 char *sectname; /* Name to use for new section */
3501 long filepos; /* File offset to descriptor data */
3502 asection *newsect;
3503
3504 if (hdr->p_filesz > 0
b9d5cdf0 3505 && (buf = (char *) malloc (hdr->p_filesz)) != NULL
32090b8e
KR
3506 && bfd_seek (abfd, hdr->p_offset, SEEK_SET) != -1
3507 && bfd_read ((PTR) buf, hdr->p_filesz, 1, abfd) == hdr->p_filesz)
3508 {
3509 x_note_p = (Elf_External_Note *) buf;
3510 while ((char *) x_note_p < (buf + hdr->p_filesz))
3511 {
3512 i_note.namesz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->namesz);
3513 i_note.descsz = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->descsz);
3514 i_note.type = bfd_h_get_32 (abfd, (bfd_byte *) x_note_p->type);
3515 namedata = x_note_p->name;
3516 descdata = namedata + BFD_ALIGN (i_note.namesz, 4);
3517 filepos = hdr->p_offset + (descdata - buf);
3518 switch (i_note.type)
3519 {
3520 case NT_PRSTATUS:
3521 /* process descdata as prstatus info */
3522 bfd_prstatus (abfd, descdata, i_note.descsz, filepos);
3523 sectname = ".prstatus";
3524 break;
3525 case NT_FPREGSET:
3526 /* process descdata as fpregset info */
3527 bfd_fpregset (abfd, descdata, i_note.descsz, filepos);
3528 sectname = ".fpregset";
3529 break;
3530 case NT_PRPSINFO:
3531 /* process descdata as prpsinfo */
3532 bfd_prpsinfo (abfd, descdata, i_note.descsz, filepos);
3533 sectname = ".prpsinfo";
3534 break;
3535 default:
3536 /* Unknown descriptor, just ignore it. */
3537 sectname = NULL;
3538 break;
3539 }
3540 if (sectname != NULL)
3541 {
3542 newsect = bfd_make_section (abfd, sectname);
3543 newsect->_raw_size = i_note.descsz;
3544 newsect->filepos = filepos;
3545 newsect->flags = SEC_ALLOC | SEC_HAS_CONTENTS;
3546 newsect->alignment_power = 2;
3547 }
3548 x_note_p = (Elf_External_Note *)
3549 (descdata + BFD_ALIGN (i_note.descsz, 4));
3550 }
3551 }
3552 if (buf != NULL)
3553 {
3554 free (buf);
3555 }
b9d5cdf0
DM
3556 else if (hdr->p_filesz > 0)
3557 {
d1ad85a6 3558 bfd_set_error (bfd_error_no_memory);
b9d5cdf0
DM
3559 return false;
3560 }
32090b8e 3561 return true;
244ffee7 3562
244ffee7
JK
3563}
3564
32090b8e
KR
3565/* Core files are simply standard ELF formatted files that partition
3566 the file using the execution view of the file (program header table)
3567 rather than the linking view. In fact, there is no section header
3568 table in a core file.
3569
3570 The process status information (including the contents of the general
3571 register set) and the floating point register set are stored in a
3572 segment of type PT_NOTE. We handcraft a couple of extra bfd sections
3573 that allow standard bfd access to the general registers (.reg) and the
3574 floating point registers (.reg2).
3575
3576 */
3577
3578bfd_target *
1c6042ee
ILT
3579elf_core_file_p (abfd)
3580 bfd *abfd;
244ffee7 3581{
32090b8e
KR
3582 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
3583 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */
3584 Elf_External_Phdr x_phdr; /* Program header table entry, external form */
3585 Elf_Internal_Phdr *i_phdrp; /* Program header table, internal form */
3586 unsigned int phindex;
d6439785 3587 struct elf_backend_data *ebd;
244ffee7 3588
32090b8e
KR
3589 /* Read in the ELF header in external format. */
3590
3591 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
244ffee7 3592 {
25057836
JL
3593 if (bfd_get_error () != bfd_error_system_call)
3594 bfd_set_error (bfd_error_wrong_format);
244ffee7
JK
3595 return NULL;
3596 }
32090b8e
KR
3597
3598 /* Now check to see if we have a valid ELF file, and one that BFD can
3599 make use of. The magic number must match, the address size ('class')
3600 and byte-swapping must match our XVEC entry, and it must have a
3601 program header table (FIXME: See comments re segments at top of this
3602 file). */
3603
3604 if (elf_file_p (&x_ehdr) == false)
244ffee7 3605 {
32090b8e 3606 wrong:
d1ad85a6 3607 bfd_set_error (bfd_error_wrong_format);
32090b8e 3608 return NULL;
244ffee7 3609 }
244ffee7 3610
32090b8e 3611 /* FIXME, Check EI_VERSION here ! */
244ffee7 3612
32090b8e
KR
3613 {
3614#if ARCH_SIZE == 32
3615 int desired_address_size = ELFCLASS32;
3616#endif
3617#if ARCH_SIZE == 64
3618 int desired_address_size = ELFCLASS64;
3619#endif
3620
3621 if (x_ehdr.e_ident[EI_CLASS] != desired_address_size)
3622 goto wrong;
3623 }
3624
3625 /* Switch xvec to match the specified byte order. */
3626 switch (x_ehdr.e_ident[EI_DATA])
244ffee7 3627 {
32090b8e
KR
3628 case ELFDATA2MSB: /* Big-endian */
3629 if (abfd->xvec->byteorder_big_p == false)
3630 goto wrong;
244ffee7 3631 break;
32090b8e
KR
3632 case ELFDATA2LSB: /* Little-endian */
3633 if (abfd->xvec->byteorder_big_p == true)
3634 goto wrong;
244ffee7 3635 break;
32090b8e
KR
3636 case ELFDATANONE: /* No data encoding specified */
3637 default: /* Unknown data encoding specified */
3638 goto wrong;
244ffee7
JK
3639 }
3640
32090b8e
KR
3641 /* Allocate an instance of the elf_obj_tdata structure and hook it up to
3642 the tdata pointer in the bfd. */
244ffee7 3643
32090b8e
KR
3644 elf_tdata (abfd) =
3645 (struct elf_obj_tdata *) bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
3646 if (elf_tdata (abfd) == NULL)
244ffee7 3647 {
d1ad85a6 3648 bfd_set_error (bfd_error_no_memory);
32090b8e 3649 return NULL;
244ffee7 3650 }
244ffee7 3651
32090b8e 3652 /* FIXME, `wrong' returns from this point onward, leak memory. */
244ffee7 3653
32090b8e
KR
3654 /* Now that we know the byte order, swap in the rest of the header */
3655 i_ehdrp = elf_elfheader (abfd);
3656 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp);
3657#if DEBUG & 1
3658 elf_debug_file (i_ehdrp);
3659#endif
244ffee7 3660
d6439785
JL
3661 ebd = get_elf_backend_data (abfd);
3662
3663 /* Check that the ELF e_machine field matches what this particular
3664 BFD format expects. */
3665 if (ebd->elf_machine_code != i_ehdrp->e_machine)
3666 {
3667 bfd_target **target_ptr;
3668
3669 if (ebd->elf_machine_code != EM_NONE)
3670 goto wrong;
3671
3672 /* This is the generic ELF target. Let it match any ELF target
3673 for which we do not have a specific backend. */
3674 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++)
3675 {
3676 struct elf_backend_data *back;
3677
3678 if ((*target_ptr)->flavour != bfd_target_elf_flavour)
3679 continue;
3680 back = (struct elf_backend_data *) (*target_ptr)->backend_data;
3681 if (back->elf_machine_code == i_ehdrp->e_machine)
3682 {
3683 /* target_ptr is an ELF backend which matches this
3684 object file, so reject the generic ELF target. */
3685 goto wrong;
3686 }
3687 }
3688 }
3689
32090b8e
KR
3690 /* If there is no program header, or the type is not a core file, then
3691 we are hosed. */
3692 if (i_ehdrp->e_phoff == 0 || i_ehdrp->e_type != ET_CORE)
3693 goto wrong;
244ffee7 3694
32090b8e
KR
3695 /* Allocate space for a copy of the program header table in
3696 internal form, seek to the program header table in the file,
3697 read it in, and convert it to internal form. As a simple sanity
3698 check, verify that the what BFD thinks is the size of each program
3699 header table entry actually matches the size recorded in the file. */
3700
3701 if (i_ehdrp->e_phentsize != sizeof (x_phdr))
3702 goto wrong;
3703 i_phdrp = (Elf_Internal_Phdr *)
3704 bfd_alloc (abfd, sizeof (*i_phdrp) * i_ehdrp->e_phnum);
3705 if (!i_phdrp)
244ffee7 3706 {
d1ad85a6 3707 bfd_set_error (bfd_error_no_memory);
32090b8e
KR
3708 return NULL;
3709 }
3710 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) == -1)
25057836 3711 return NULL;
32090b8e
KR
3712 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3713 {
3714 if (bfd_read ((PTR) & x_phdr, sizeof (x_phdr), 1, abfd)
3715 != sizeof (x_phdr))
25057836 3716 return NULL;
32090b8e 3717 elf_swap_phdr_in (abfd, &x_phdr, i_phdrp + phindex);
244ffee7
JK
3718 }
3719
32090b8e
KR
3720 /* Once all of the program headers have been read and converted, we
3721 can start processing them. */
244ffee7 3722
32090b8e
KR
3723 for (phindex = 0; phindex < i_ehdrp->e_phnum; phindex++)
3724 {
3725 bfd_section_from_phdr (abfd, i_phdrp + phindex, phindex);
3726 if ((i_phdrp + phindex)->p_type == PT_NOTE)
3727 {
3728 elf_corefile_note (abfd, i_phdrp + phindex);
3729 }
3730 }
244ffee7 3731
32090b8e 3732 /* Remember the entry point specified in the ELF file header. */
244ffee7 3733
32090b8e 3734 bfd_get_start_address (abfd) = i_ehdrp->e_entry;
244ffee7 3735
32090b8e 3736 return abfd->xvec;
244ffee7 3737}
This page took 0.253586 seconds and 4 git commands to generate.