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