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