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