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