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