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