b10c170ecae66c6496563c595b63c926cedfb6bf
[binutils-gdb.git] / bfd / elf.c
1 /* ELF support for BFD.
2 Copyright (C) 1991 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".
7
8 This file is part of BFD, the Binary File Descriptor library.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23
24
25 /****************************************
26
27 WARNING
28
29 This is only a partial ELF implementation,
30 incorporating only those parts that are
31 required to get gdb up and running. It is
32 expected that it will be expanded to a full
33 ELF implementation at some future date.
34
35 Unimplemented stubs call abort() to ensure
36 that they get proper attention if they are
37 ever called. The stubs are here since
38 this version was hacked from the COFF
39 version, and thus they will probably
40 go away or get expanded appropriately in a
41 future version.
42
43 fnf@cygnus.com
44
45 *****************************************/
46
47
48 /* Problems and other issues to resolve.
49
50 (1) BFD expects there to be some fixed number of "sections" in
51 the object file. I.E. there is a "section_count" variable in the
52 bfd structure which contains the number of sections. However, ELF
53 supports multiple "views" of a file. In particular, with current
54 implementations, executable files typically have two tables, a
55 program header table and a section header table, both of which
56 partition the executable.
57
58 In ELF-speak, the "linking view" of the file uses the section header
59 table to access "sections" within the file, and the "execution view"
60 uses the program header table to access "segments" within the file.
61 "Segments" typically may contain all the data from one or more
62 "sections".
63
64 Note that the section header table is optional in ELF executables,
65 but it is this information that is most useful to gdb. If the
66 section header table is missing, then gdb should probably try
67 to make do with the program header table. (FIXME)
68
69 */
70
71 #include <ansidecl.h>
72 #include <sysdep.h>
73 #include "bfd.h"
74 #include "libbfd.h"
75 #include "obstack.h"
76 #include "elf-common.h"
77 #include "elf-internal.h"
78 #include "elf-external.h"
79
80 /* Forward data declarations */
81 extern bfd_target elf_little_vec, elf_big_vec;
82
83 /* Translate an ELF header in external format into an ELF header in internal
84 format. */
85
86 static void
87 DEFUN(bfd_swap_ehdr_in,(abfd, src, dst),
88 bfd *abfd AND
89 Elf_External_Ehdr *src AND
90 Elf_Internal_Ehdr *dst)
91 {
92 bcopy (src -> e_ident, dst -> e_ident, EI_NIDENT);
93 dst -> e_type = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_type);
94 dst -> e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_machine);
95 dst -> e_version = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_version);
96 dst -> e_entry = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_entry);
97 dst -> e_phoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_phoff);
98 dst -> e_shoff = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_shoff);
99 dst -> e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> e_flags);
100 dst -> e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_ehsize);
101 dst -> e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phentsize);
102 dst -> e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_phnum);
103 dst -> e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shentsize);
104 dst -> e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shnum);
105 dst -> e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src -> e_shstrndx);
106 }
107
108
109 /* Translate an ELF section header table entry in external format into an
110 ELF section header table entry in internal format. */
111
112 static void
113 DEFUN(bfd_swap_shdr_in,(abfd, src, dst),
114 bfd *abfd AND
115 Elf_External_Shdr *src AND
116 Elf_Internal_Shdr *dst)
117 {
118 dst -> sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_name);
119 dst -> sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_type);
120 dst -> sh_flags = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_flags);
121 dst -> sh_addr = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_addr);
122 dst -> sh_offset = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_offset);
123 dst -> sh_size = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_size);
124 dst -> sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_link);
125 dst -> sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_info);
126 dst -> sh_addralign = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_addralign);
127 dst -> sh_entsize = bfd_h_get_32 (abfd, (bfd_byte *) src -> sh_entsize);
128 }
129
130
131 /* Create a new bfd section from an ELF section header. */
132
133 static boolean
134 DEFUN(bfd_section_from_shdr, (abfd, hdr, shstrtab),
135 bfd *abfd AND
136 Elf_Internal_Shdr *hdr AND
137 char *shstrtab)
138 {
139 asection *newsect;
140 char *name;
141
142 name = hdr -> sh_name ? shstrtab + hdr -> sh_name : "unnamed";
143 newsect = bfd_make_section (abfd, name);
144 newsect -> vma = hdr -> sh_addr;
145 newsect -> size = hdr -> sh_size;
146 if (!(hdr -> sh_type == SHT_NOBITS))
147 {
148 newsect -> filepos = hdr -> sh_offset;
149 newsect -> flags |= SEC_HAS_CONTENTS;
150 }
151 if (hdr -> sh_flags & SHF_ALLOC)
152 {
153 newsect -> flags |= SEC_ALLOC;
154 if (hdr -> sh_type != SHT_NOBITS)
155 {
156 newsect -> flags |= SEC_LOAD;
157 }
158 }
159 if (!(hdr -> sh_flags & SHF_WRITE))
160 {
161 newsect -> flags |= SEC_READONLY;
162 }
163 if (hdr -> sh_flags & SHF_EXECINSTR)
164 {
165 newsect -> flags |= SEC_CODE; /* FIXME: may only contain SOME code */
166 }
167 if (hdr -> sh_type == SHT_SYMTAB)
168 {
169 abfd -> flags |= HAS_SYMS;
170 }
171
172 return (true);
173 }
174
175 /* Begin processing a given object.
176
177 First we validate the file by reading in the ELF header and checking
178 the magic number.
179
180 */
181
182 static bfd_target *
183 DEFUN (elf_object_p, (abfd), bfd *abfd)
184 {
185 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */
186 Elf_Internal_Ehdr i_ehdr; /* Elf file header, internal form */
187 Elf_External_Shdr *x_shdr; /* Section header table, external form */
188 Elf_Internal_Shdr *i_shdr; /* Section header table, internal form */
189 int shindex;
190 char *shstrtab; /* Internal copy of section header stringtab */
191 int shstrtabsize; /* Size of section header string table */
192
193 /* Read in the ELF header in external format. */
194
195 if (bfd_read ((PTR) &x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr))
196 {
197 bfd_error = system_call_error;
198 return (NULL);
199 }
200
201 /* Now check to see if we have a valid ELF file, and one that BFD can
202 make use of. The magic number must match, the address size ('class')
203 and byte-swapping must match our XVEC entry, and it must have a
204 section header table (FIXME: See comments re sections at top of this
205 file). */
206
207 if (x_ehdr.e_ident[EI_MAG0] != ELFMAG0 ||
208 x_ehdr.e_ident[EI_MAG1] != ELFMAG1 ||
209 x_ehdr.e_ident[EI_MAG2] != ELFMAG2 ||
210 x_ehdr.e_ident[EI_MAG3] != ELFMAG3)
211 {
212 wrong:
213 bfd_error = wrong_format;
214 return (NULL);
215 }
216
217 /* FIXME, Check EI_VERSION here ! */
218
219 switch (x_ehdr.e_ident[EI_CLASS]) {
220 case ELFCLASSNONE: /* address size not specified */
221 goto wrong; /* No support if can't tell address size */
222 case ELFCLASS32: /* 32-bit addresses */
223 break;
224 case ELFCLASS64: /* 64-bit addresses */
225 goto wrong; /* FIXME: 64 bits not yet supported */
226 default:
227 goto wrong; /* No support if unknown address class */
228 }
229
230 /* Switch xvec to match the specified byte order. */
231 switch (x_ehdr.e_ident[EI_DATA]) {
232 case ELFDATA2MSB: /* Big-endian */
233 abfd->xvec = &elf_big_vec;
234 break;
235 case ELFDATA2LSB: /* Little-endian */
236 abfd->xvec = &elf_little_vec;
237 case ELFDATANONE: /* No data encoding specified */
238 default: /* Unknown data encoding specified */
239 goto wrong;
240 }
241
242 /* Now that we know the byte order, swap in the rest of the header */
243 bfd_swap_ehdr_in (abfd, &x_ehdr, &i_ehdr);
244 if (x_ehdr.e_shoff == 0)
245 goto wrong;
246
247 if (i_ehdr.e_type == ET_EXEC || i_ehdr.e_type == ET_DYN)
248 {
249 abfd -> flags |= EXEC_P;
250 }
251
252 /* Allocate space for copies of the section header table in external
253 and internal form, seek to the section header table in the file,
254 read it in, and convert it to internal form. As a simple sanity
255 check, verify that the what BFD thinks is the size of each section
256 header table entry actually matches the size recorded in the file. */
257
258 if (i_ehdr.e_shentsize != sizeof (*x_shdr))
259 goto wrong;
260 if ((x_shdr = (Elf_External_Shdr *)
261 bfd_alloc (abfd, sizeof (*x_shdr) * i_ehdr.e_shnum)) == NULL)
262 {
263 bfd_error = no_memory;
264 return (NULL);
265 }
266 if ((i_shdr = (Elf_Internal_Shdr *)
267 bfd_alloc (abfd, sizeof (*i_shdr) * i_ehdr.e_shnum)) == NULL)
268 {
269 bfd_error = no_memory;
270 return (NULL);
271 }
272 if (bfd_seek (abfd, i_ehdr.e_shoff, SEEK_SET) == -1)
273 {
274 bfd_error = system_call_error;
275 return (NULL);
276 }
277 for (shindex = 0; shindex < i_ehdr.e_shnum; shindex++)
278 {
279 if (bfd_read ((PTR) (x_shdr + shindex), sizeof (*x_shdr), 1, abfd)
280 != sizeof (*x_shdr))
281 {
282 bfd_error = system_call_error;
283 return (NULL);
284 }
285 bfd_swap_shdr_in (abfd, x_shdr + shindex, i_shdr + shindex);
286 }
287
288 /* Read in the string table containing the names of the sections. We
289 will need the base pointer to this table later. */
290
291 shstrtabsize = i_shdr[i_ehdr.e_shstrndx].sh_size;
292 if ((shstrtab = bfd_alloc (abfd, shstrtabsize)) == NULL)
293 {
294 bfd_error = no_memory;
295 return (NULL);
296 }
297 if (bfd_seek (abfd, i_shdr[i_ehdr.e_shstrndx].sh_offset, SEEK_SET) == -1)
298 {
299 bfd_error = system_call_error;
300 return (NULL);
301 }
302 if (bfd_read ((PTR) shstrtab, shstrtabsize, 1, abfd) != shstrtabsize)
303 {
304 bfd_error = system_call_error;
305 return (NULL);
306 }
307
308 /* Once all of the section headers have been read and converted, we
309 can start processing them. */
310
311 for (shindex = 0; shindex < i_ehdr.e_shnum; shindex++)
312 {
313 bfd_section_from_shdr (abfd, i_shdr + shindex, shstrtab);
314 }
315
316 return (abfd->xvec);
317 }
318
319 static boolean
320 DEFUN (elf_mkobject, (abfd), bfd *abfd)
321 {
322 fprintf (stderr, "elf_mkobject unimplemented\n");
323 fflush (stderr);
324 abort ();
325 return (false);
326 }
327
328 static boolean
329 DEFUN (elf_write_object_contents, (abfd), bfd *abfd)
330 {
331 fprintf (stderr, "elf_write_object_contents unimplemented\n");
332 fflush (stderr);
333 abort ();
334 return (false);
335 }
336
337 static boolean
338 DEFUN (elf_set_section_contents, (abfd, section, location, offset, count),
339 bfd *abfd AND
340 sec_ptr section AND
341 PTR location AND
342 file_ptr offset AND
343 bfd_size_type count)
344 {
345 fprintf (stderr, "elf_set_section_contents unimplemented\n");
346 fflush (stderr);
347 abort ();
348 return (false);
349 }
350
351 #define elf_new_section_hook _bfd_dummy_new_section_hook
352 #define elf_core_file_failing_command _bfd_dummy_core_file_failing_command
353 #define elf_core_file_failing_signal _bfd_dummy_core_file_failing_signal
354 #define elf_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
355 #define elf_slurp_armap bfd_false
356 #define elf_slurp_extended_name_table _bfd_slurp_extended_name_table
357 #define elf_truncate_arname bfd_dont_truncate_arname
358 #define elf_openr_next_archived_file bfd_generic_openr_next_archived_file
359 #define elf_generic_stat_arch_elt bfd_generic_stat_arch_elt
360 #define elf_get_section_contents bfd_generic_get_section_contents
361 #define elf_close_and_cleanup bfd_generic_close_and_cleanup
362
363 #define elf_bfd_debug_info_start bfd_void
364 #define elf_bfd_debug_info_end bfd_void
365 #define elf_bfd_debug_info_accumulate (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
366
367 #define elf_write_armap bfd_false
368
369
370 static unsigned int
371 elf_get_symtab_upper_bound(abfd)
372 bfd *abfd;
373 {
374 fprintf (stderr, "elf_get_symtab_upper_bound unimplemented\n");
375 fflush (stderr);
376 abort ();
377 return (0);
378 }
379
380 static unsigned int
381 elf_get_reloc_upper_bound (abfd, asect)
382 bfd *abfd;
383 sec_ptr asect;
384 {
385 fprintf (stderr, "elf_get_reloc_upper_bound unimplemented\n");
386 fflush (stderr);
387 abort ();
388 return (0);
389 }
390
391 static unsigned int
392 elf_canonicalize_reloc (abfd, section, relptr, symbols)
393 bfd *abfd;
394 sec_ptr section;
395 arelent **relptr;
396 asymbol **symbols;
397 {
398 fprintf (stderr, "elf_canonicalize_reloc unimplemented\n");
399 fflush (stderr);
400 abort ();
401 return (0);
402 }
403
404 static unsigned int
405 elf_get_symtab (abfd, alocation)
406 bfd *abfd;
407 asymbol **alocation;
408 {
409 fprintf (stderr, "elf_get_symtab unimplemented\n");
410 fflush (stderr);
411 abort ();
412 return (0);
413 }
414
415 static asymbol *
416 elf_make_empty_symbol(abfd)
417 bfd *abfd;
418 {
419 fprintf (stderr, "elf_make_empty_symbol unimplemented\n");
420 fflush (stderr);
421 abort ();
422 return (NULL);
423 }
424
425 static void
426 DEFUN (elf_print_symbol,(ignore_abfd, filep, symbol, how),
427 bfd *ignore_abfd AND
428 PTR filep AND
429 asymbol *symbol AND
430 bfd_print_symbol_enum_type how)
431 {
432 fprintf (stderr, "elf_print_symbol unimplemented\n");
433 fflush (stderr);
434 abort ();
435 }
436
437 static alent *
438 DEFUN (elf_get_lineno,(ignore_abfd, symbol),
439 bfd *ignore_abfd AND
440 asymbol *symbol)
441 {
442 fprintf (stderr, "elf_get_lineno unimplemented\n");
443 fflush (stderr);
444 abort ();
445 return (NULL);
446 }
447
448 static boolean
449 DEFUN (elf_set_arch_mach,(abfd, arch, machine),
450 bfd *abfd AND
451 enum bfd_architecture arch AND
452 unsigned long machine)
453 {
454 fprintf (stderr, "elf_set_arch_mach unimplemented\n");
455 fflush (stderr);
456 /* Allow any architecture to be supported by the elf backend */
457 return bfd_default_set_arch_mach(abfd, arch, machine);
458 }
459
460 static boolean
461 DEFUN (elf_find_nearest_line,(abfd,
462 section,
463 symbols,
464 offset,
465 filename_ptr,
466 functionname_ptr,
467 line_ptr),
468 bfd *abfd AND
469 asection *section AND
470 asymbol **symbols AND
471 bfd_vma offset AND
472 CONST char **filename_ptr AND
473 CONST char **functionname_ptr AND
474 unsigned int *line_ptr)
475 {
476 fprintf (stderr, "elf_find_nearest_line unimplemented\n");
477 fflush (stderr);
478 abort ();
479 return (false);
480 }
481
482 static int
483 DEFUN (elf_sizeof_headers, (abfd, reloc),
484 bfd *abfd AND
485 boolean reloc)
486 {
487 fprintf (stderr, "elf_sizeof_headers unimplemented\n");
488 fflush (stderr);
489 abort ();
490 return (0);
491 }
492
493 /* This structure contains everything that BFD knows about a target.
494 It includes things like its byte order, name, what routines to call
495 to do various operations, etc. Every BFD points to a target structure
496 with its "xvec" member.
497
498 There are two such structures here: one for big-endian machines and
499 one for little-endian machines. */
500
501 bfd_target elf_big_vec =
502 {
503 /* name: identify kind of target */
504 "elf-big",
505
506 /* flavour: general indication about file */
507 bfd_target_elf_flavour_enum,
508
509 /* byteorder_big_p: data is big endian */
510 true,
511
512 /* header_byteorder_big_p: header is also big endian */
513 true,
514
515 /* object_flags: mask of all file flags */
516 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
517 DYNAMIC | WP_TEXT),
518
519 /* section_flags: mask of all section flags */
520 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
521 SEC_DATA),
522
523 /* ar_pad_char: pad character for filenames within an archive header
524 FIXME: this really has nothing to do with ELF, this is a characteristic
525 of the archiver and/or os and should be independently tunable */
526 '/',
527
528 /* ar_max_namelen: maximum number of characters in an archive header
529 FIXME: this really has nothing to do with ELF, this is a characteristic
530 of the archiver and should be independently tunable. This value is
531 a WAG (wild a** guess) */
532 15,
533
534 /* align_power_min: minimum alignment restriction for any section
535 FIXME: this value may be target machine dependent */
536 3,
537
538 /* Routines to byte-swap various sized integers from the data sections */
539 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
540
541 /* Routines to byte-swap various sized integers from the file headers */
542 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
543
544 /* bfd_check_format: check the format of a file being read */
545 { _bfd_dummy_target,
546 elf_object_p,
547 bfd_generic_archive_p,
548 _bfd_dummy_target
549 },
550
551 /* bfd_set_format: set the format of a file being written */
552 { bfd_false,
553 elf_mkobject,
554 _bfd_generic_mkarchive,
555 bfd_false
556 },
557
558 /* bfd_write_contents: write cached information into a file being written */
559 { bfd_false,
560 elf_write_object_contents,
561 _bfd_write_archive_contents,
562 bfd_false
563 },
564
565 /* Initialize a jump table with the standard macro. All names start
566 with "elf" */
567 JUMP_TABLE(elf),
568
569 /* SWAP_TABLE */
570 NULL, NULL, NULL
571 };
572
573 bfd_target elf_little_vec =
574 {
575 /* name: identify kind of target */
576 "elf-little",
577
578 /* flavour: general indication about file */
579 bfd_target_elf_flavour_enum,
580
581 /* byteorder_big_p: data is big endian */
582 false, /* Nope -- this one's little endian */
583
584 /* header_byteorder_big_p: header is also big endian */
585 false, /* Nope -- this one's little endian */
586
587 /* object_flags: mask of all file flags */
588 (HAS_RELOC | EXEC_P | HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS |
589 DYNAMIC | WP_TEXT),
590
591 /* section_flags: mask of all section flags */
592 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_READONLY |
593 SEC_DATA),
594
595 /* ar_pad_char: pad character for filenames within an archive header
596 FIXME: this really has nothing to do with ELF, this is a characteristic
597 of the archiver and/or os and should be independently tunable */
598 '/',
599
600 /* ar_max_namelen: maximum number of characters in an archive header
601 FIXME: this really has nothing to do with ELF, this is a characteristic
602 of the archiver and should be independently tunable. This value is
603 a WAG (wild a** guess) */
604 15,
605
606 /* align_power_min: minimum alignment restriction for any section
607 FIXME: this value may be target machine dependent */
608 3,
609
610 /* Routines to byte-swap various sized integers from the data sections */
611 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
612
613 /* Routines to byte-swap various sized integers from the file headers */
614 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16,
615
616 /* bfd_check_format: check the format of a file being read */
617 { _bfd_dummy_target,
618 elf_object_p,
619 bfd_generic_archive_p,
620 _bfd_dummy_target
621 },
622
623 /* bfd_set_format: set the format of a file being written */
624 { bfd_false,
625 elf_mkobject,
626 _bfd_generic_mkarchive,
627 bfd_false
628 },
629
630 /* bfd_write_contents: write cached information into a file being written */
631 { bfd_false,
632 elf_write_object_contents,
633 _bfd_write_archive_contents,
634 bfd_false
635 },
636
637 /* Initialize a jump table with the standard macro. All names start
638 with "elf" */
639 JUMP_TABLE(elf),
640
641 /* SWAP_TABLE */
642 NULL, NULL, NULL
643 };