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