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