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