Add some initial 64-bit DWARF support
[binutils.git] / binutils / readelf.c
blobaf3b14b7d472f305621015307b45c139f1ee6cea
1 /* readelf.c -- display contents of an ELF format file
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
7 This file is part of GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
25 #include <assert.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <time.h>
31 #if __GNUC__ >= 2
32 /* Define BFD64 here, even if our default architecture is 32 bit ELF
33 as this will allow us to read in and parse 64bit and 32bit ELF files.
34 Only do this if we believe that the compiler can support a 64 bit
35 data type. For now we only rely on GCC being able to do this. */
36 #define BFD64
37 #endif
39 #include "bfd.h"
41 #include "elf/common.h"
42 #include "elf/external.h"
43 #include "elf/internal.h"
44 #include "elf/dwarf2.h"
46 /* The following headers use the elf/reloc-macros.h file to
47 automatically generate relocation recognition functions
48 such as elf_mips_reloc_type() */
50 #define RELOC_MACROS_GEN_FUNC
52 #include "elf/alpha.h"
53 #include "elf/arc.h"
54 #include "elf/arm.h"
55 #include "elf/avr.h"
56 #include "elf/cris.h"
57 #include "elf/d10v.h"
58 #include "elf/d30v.h"
59 #include "elf/dlx.h"
60 #include "elf/fr30.h"
61 #include "elf/frv.h"
62 #include "elf/h8.h"
63 #include "elf/hppa.h"
64 #include "elf/i386.h"
65 #include "elf/i370.h"
66 #include "elf/i860.h"
67 #include "elf/i960.h"
68 #include "elf/ia64.h"
69 #include "elf/ip2k.h"
70 #include "elf/m32r.h"
71 #include "elf/m68k.h"
72 #include "elf/m68hc11.h"
73 #include "elf/mcore.h"
74 #include "elf/mips.h"
75 #include "elf/mmix.h"
76 #include "elf/mn10200.h"
77 #include "elf/mn10300.h"
78 #include "elf/msp430.h"
79 #include "elf/or32.h"
80 #include "elf/pj.h"
81 #include "elf/ppc.h"
82 #include "elf/ppc64.h"
83 #include "elf/s390.h"
84 #include "elf/sh.h"
85 #include "elf/sparc.h"
86 #include "elf/v850.h"
87 #include "elf/vax.h"
88 #include "elf/x86-64.h"
89 #include "elf/xstormy16.h"
90 #include "elf/iq2000.h"
92 #include "bucomm.h"
93 #include "getopt.h"
95 char *program_name = "readelf";
96 unsigned long dynamic_addr;
97 bfd_size_type dynamic_size;
98 char *dynamic_strings;
99 char *string_table;
100 unsigned long string_table_length;
101 unsigned long num_dynamic_syms;
102 Elf_Internal_Sym *dynamic_symbols;
103 Elf_Internal_Syminfo *dynamic_syminfo;
104 unsigned long dynamic_syminfo_offset;
105 unsigned int dynamic_syminfo_nent;
106 char program_interpreter[64];
107 long dynamic_info[DT_JMPREL + 1];
108 long version_info[16];
109 long loadaddr = 0;
110 Elf_Internal_Ehdr elf_header;
111 Elf_Internal_Shdr *section_headers;
112 Elf_Internal_Dyn *dynamic_segment;
113 Elf_Internal_Shdr *symtab_shndx_hdr;
114 int show_name;
115 int do_dynamic;
116 int do_syms;
117 int do_reloc;
118 int do_sections;
119 int do_segments;
120 int do_unwind;
121 int do_using_dynamic;
122 int do_header;
123 int do_dump;
124 int do_version;
125 int do_wide;
126 int do_histogram;
127 int do_debugging;
128 int do_debug_info;
129 int do_debug_abbrevs;
130 int do_debug_lines;
131 int do_debug_pubnames;
132 int do_debug_aranges;
133 int do_debug_frames;
134 int do_debug_frames_interp;
135 int do_debug_macinfo;
136 int do_debug_str;
137 int do_debug_loc;
138 int do_arch;
139 int do_notes;
140 int is_32bit_elf;
142 /* A dynamic array of flags indicating which sections require dumping. */
143 char *dump_sects = NULL;
144 unsigned int num_dump_sects = 0;
146 #define HEX_DUMP (1 << 0)
147 #define DISASS_DUMP (1 << 1)
148 #define DEBUG_DUMP (1 << 2)
150 /* How to rpint a vma value. */
151 typedef enum print_mode
153 HEX,
154 DEC,
155 DEC_5,
156 UNSIGNED,
157 PREFIX_HEX,
158 FULL_HEX,
159 LONG_HEX
161 print_mode;
163 /* Forward declarations for dumb compilers. */
164 static void print_vma
165 PARAMS ((bfd_vma, print_mode));
166 static void print_symbol
167 PARAMS ((int, const char *));
168 static bfd_vma (*byte_get)
169 PARAMS ((unsigned char *, int));
170 static bfd_vma byte_get_little_endian
171 PARAMS ((unsigned char *, int));
172 static bfd_vma byte_get_big_endian
173 PARAMS ((unsigned char *, int));
174 static const char *get_mips_dynamic_type
175 PARAMS ((unsigned long));
176 static const char *get_sparc64_dynamic_type
177 PARAMS ((unsigned long));
178 static const char *get_ppc64_dynamic_type
179 PARAMS ((unsigned long));
180 static const char *get_parisc_dynamic_type
181 PARAMS ((unsigned long));
182 static const char *get_ia64_dynamic_type
183 PARAMS ((unsigned long));
184 static const char *get_dynamic_type
185 PARAMS ((unsigned long));
186 static int slurp_rela_relocs
187 PARAMS ((FILE *, unsigned long, unsigned long, Elf_Internal_Rela **,
188 unsigned long *));
189 static int slurp_rel_relocs
190 PARAMS ((FILE *, unsigned long, unsigned long, Elf_Internal_Rela **,
191 unsigned long *));
192 static int dump_relocations
193 PARAMS ((FILE *, unsigned long, unsigned long, Elf_Internal_Sym *,
194 unsigned long, char *, int));
195 static char *get_file_type
196 PARAMS ((unsigned));
197 static char *get_machine_name
198 PARAMS ((unsigned));
199 static void decode_ARM_machine_flags
200 PARAMS ((unsigned, char[]));
201 static char *get_machine_flags
202 PARAMS ((unsigned, unsigned));
203 static const char *get_mips_segment_type
204 PARAMS ((unsigned long));
205 static const char *get_parisc_segment_type
206 PARAMS ((unsigned long));
207 static const char *get_ia64_segment_type
208 PARAMS ((unsigned long));
209 static const char *get_segment_type
210 PARAMS ((unsigned long));
211 static const char *get_mips_section_type_name
212 PARAMS ((unsigned int));
213 static const char *get_parisc_section_type_name
214 PARAMS ((unsigned int));
215 static const char *get_ia64_section_type_name
216 PARAMS ((unsigned int));
217 static const char *get_section_type_name
218 PARAMS ((unsigned int));
219 static const char *get_symbol_binding
220 PARAMS ((unsigned int));
221 static const char *get_symbol_type
222 PARAMS ((unsigned int));
223 static const char *get_symbol_visibility
224 PARAMS ((unsigned int));
225 static const char *get_symbol_index_type
226 PARAMS ((unsigned int));
227 static const char *get_dynamic_flags
228 PARAMS ((bfd_vma));
229 static void usage
230 PARAMS ((void));
231 static void parse_args
232 PARAMS ((int, char **));
233 static int process_file_header
234 PARAMS ((void));
235 static int process_program_headers
236 PARAMS ((FILE *));
237 static int process_section_headers
238 PARAMS ((FILE *));
239 static int process_unwind
240 PARAMS ((FILE *));
241 static void dynamic_segment_mips_val
242 PARAMS ((Elf_Internal_Dyn *));
243 static void dynamic_segment_parisc_val
244 PARAMS ((Elf_Internal_Dyn *));
245 static void dynamic_segment_ia64_val
246 PARAMS ((Elf_Internal_Dyn *));
247 static int process_dynamic_segment
248 PARAMS ((FILE *));
249 static int process_symbol_table
250 PARAMS ((FILE *));
251 static int process_syminfo
252 PARAMS ((FILE *));
253 static int process_section_contents
254 PARAMS ((FILE *));
255 static void process_mips_fpe_exception
256 PARAMS ((int));
257 static int process_mips_specific
258 PARAMS ((FILE *));
259 static int process_file
260 PARAMS ((char *));
261 static int process_relocs
262 PARAMS ((FILE *));
263 static int process_version_sections
264 PARAMS ((FILE *));
265 static char *get_ver_flags
266 PARAMS ((unsigned int));
267 static int get_32bit_section_headers
268 PARAMS ((FILE *, unsigned int));
269 static int get_64bit_section_headers
270 PARAMS ((FILE *, unsigned int));
271 static int get_32bit_program_headers
272 PARAMS ((FILE *, Elf_Internal_Phdr *));
273 static int get_64bit_program_headers
274 PARAMS ((FILE *, Elf_Internal_Phdr *));
275 static int get_file_header
276 PARAMS ((FILE *));
277 static Elf_Internal_Sym *get_32bit_elf_symbols
278 PARAMS ((FILE *, Elf_Internal_Shdr *));
279 static Elf_Internal_Sym *get_64bit_elf_symbols
280 PARAMS ((FILE *, Elf_Internal_Shdr *));
281 static const char *get_elf_section_flags
282 PARAMS ((bfd_vma));
283 static int *get_dynamic_data
284 PARAMS ((FILE *, unsigned int));
285 static int get_32bit_dynamic_segment
286 PARAMS ((FILE *));
287 static int get_64bit_dynamic_segment
288 PARAMS ((FILE *));
289 #ifdef SUPPORT_DISASSEMBLY
290 static int disassemble_section
291 PARAMS ((Elf_Internal_Shdr *, FILE *));
292 #endif
293 static int dump_section
294 PARAMS ((Elf_Internal_Shdr *, FILE *));
295 static int display_debug_section
296 PARAMS ((Elf_Internal_Shdr *, FILE *));
297 static int display_debug_info
298 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
299 static int display_debug_not_supported
300 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
301 static int prescan_debug_info
302 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
303 static int display_debug_lines
304 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
305 static int display_debug_pubnames
306 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
307 static int display_debug_abbrev
308 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
309 static int display_debug_aranges
310 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
311 static int display_debug_frames
312 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
313 static int display_debug_macinfo
314 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
315 static int display_debug_str
316 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
317 static int display_debug_loc
318 PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
319 static unsigned char *process_abbrev_section
320 PARAMS ((unsigned char *, unsigned char *));
321 static void load_debug_str
322 PARAMS ((FILE *));
323 static void free_debug_str
324 PARAMS ((void));
325 static const char *fetch_indirect_string
326 PARAMS ((unsigned long));
327 static void load_debug_loc
328 PARAMS ((FILE *));
329 static void free_debug_loc
330 PARAMS ((void));
331 static unsigned long read_leb128
332 PARAMS ((unsigned char *, int *, int));
333 static int process_extended_line_op
334 PARAMS ((unsigned char *, int, int));
335 static void reset_state_machine
336 PARAMS ((int));
337 static char *get_TAG_name
338 PARAMS ((unsigned long));
339 static char *get_AT_name
340 PARAMS ((unsigned long));
341 static char *get_FORM_name
342 PARAMS ((unsigned long));
343 static void free_abbrevs
344 PARAMS ((void));
345 static void add_abbrev
346 PARAMS ((unsigned long, unsigned long, int));
347 static void add_abbrev_attr
348 PARAMS ((unsigned long, unsigned long));
349 static unsigned char *read_and_display_attr
350 PARAMS ((unsigned long, unsigned long, unsigned char *, unsigned long,
351 unsigned long, unsigned long, int));
352 static unsigned char *read_and_display_attr_value
353 PARAMS ((unsigned long, unsigned long, unsigned char *, unsigned long,
354 unsigned long, unsigned long, int));
355 static unsigned char *display_block
356 PARAMS ((unsigned char *, unsigned long));
357 static void decode_location_expression
358 PARAMS ((unsigned char *, unsigned int, unsigned long));
359 static void request_dump
360 PARAMS ((unsigned int, int));
361 static const char *get_elf_class
362 PARAMS ((unsigned int));
363 static const char *get_data_encoding
364 PARAMS ((unsigned int));
365 static const char *get_osabi_name
366 PARAMS ((unsigned int));
367 static int guess_is_rela
368 PARAMS ((unsigned long));
369 static const char *get_note_type
370 PARAMS ((unsigned int));
371 static const char *get_netbsd_elfcore_note_type
372 PARAMS ((unsigned int));
373 static int process_note
374 PARAMS ((Elf_Internal_Note *));
375 static int process_corefile_note_segment
376 PARAMS ((FILE *, bfd_vma, bfd_vma));
377 static int process_corefile_note_segments
378 PARAMS ((FILE *));
379 static int process_corefile_contents
380 PARAMS ((FILE *));
381 static int process_arch_specific
382 PARAMS ((FILE *));
383 static int process_gnu_liblist
384 PARAMS ((FILE *));
386 typedef int Elf32_Word;
388 #define UNKNOWN -1
390 #define SECTION_NAME(X) ((X) == NULL ? "<none>" : \
391 ((X)->sh_name >= string_table_length \
392 ? "<corrupt>" : string_table + (X)->sh_name))
394 /* Given st_shndx I, map to section_headers index. */
395 #define SECTION_HEADER_INDEX(I) \
396 ((I) < SHN_LORESERVE \
397 ? (I) \
398 : ((I) <= SHN_HIRESERVE \
399 ? 0 \
400 : (I) - (SHN_HIRESERVE + 1 - SHN_LORESERVE)))
402 /* Reverse of the above. */
403 #define SECTION_HEADER_NUM(N) \
404 ((N) < SHN_LORESERVE \
405 ? (N) \
406 : (N) + (SHN_HIRESERVE + 1 - SHN_LORESERVE))
408 #define SECTION_HEADER(I) (section_headers + SECTION_HEADER_INDEX (I))
410 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
412 #define BYTE_GET(field) byte_get (field, sizeof (field))
414 /* If we can support a 64 bit data type then BFD64 should be defined
415 and sizeof (bfd_vma) == 8. In this case when translating from an
416 external 8 byte field to an internal field, we can assume that the
417 internal field is also 8 bytes wide and so we can extract all the data.
418 If, however, BFD64 is not defined, then we must assume that the
419 internal data structure only has 4 byte wide fields that are the
420 equivalent of the 8 byte wide external counterparts, and so we must
421 truncate the data. */
422 #ifdef BFD64
423 #define BYTE_GET8(field) byte_get (field, -8)
424 #else
425 #define BYTE_GET8(field) byte_get (field, 8)
426 #endif
428 #define NUM_ELEM(array) (sizeof (array) / sizeof ((array)[0]))
430 #define GET_ELF_SYMBOLS(file, section) \
431 (is_32bit_elf ? get_32bit_elf_symbols (file, section) \
432 : get_64bit_elf_symbols (file, section))
435 static void
436 error VPARAMS ((const char *message, ...))
438 VA_OPEN (args, message);
439 VA_FIXEDARG (args, const char *, message);
441 fprintf (stderr, _("%s: Error: "), program_name);
442 vfprintf (stderr, message, args);
443 VA_CLOSE (args);
446 static void
447 warn VPARAMS ((const char *message, ...))
449 VA_OPEN (args, message);
450 VA_FIXEDARG (args, const char *, message);
452 fprintf (stderr, _("%s: Warning: "), program_name);
453 vfprintf (stderr, message, args);
454 VA_CLOSE (args);
457 static PTR get_data PARAMS ((PTR, FILE *, long, size_t, const char *));
459 static PTR
460 get_data (var, file, offset, size, reason)
461 PTR var;
462 FILE *file;
463 long offset;
464 size_t size;
465 const char *reason;
467 PTR mvar;
469 if (size == 0)
470 return NULL;
472 if (fseek (file, offset, SEEK_SET))
474 error (_("Unable to seek to %x for %s\n"), offset, reason);
475 return NULL;
478 mvar = var;
479 if (mvar == NULL)
481 mvar = (PTR) malloc (size);
483 if (mvar == NULL)
485 error (_("Out of memory allocating %d bytes for %s\n"),
486 size, reason);
487 return NULL;
491 if (fread (mvar, size, 1, file) != 1)
493 error (_("Unable to read in %d bytes of %s\n"), size, reason);
494 if (mvar != var)
495 free (mvar);
496 return NULL;
499 return mvar;
502 static bfd_vma
503 byte_get_little_endian (field, size)
504 unsigned char *field;
505 int size;
507 switch (size)
509 case 1:
510 return *field;
512 case 2:
513 return ((unsigned int) (field[0]))
514 | (((unsigned int) (field[1])) << 8);
516 #ifndef BFD64
517 case 8:
518 /* We want to extract data from an 8 byte wide field and
519 place it into a 4 byte wide field. Since this is a little
520 endian source we can just use the 4 byte extraction code. */
521 /* Fall through. */
522 #endif
523 case 4:
524 return ((unsigned long) (field[0]))
525 | (((unsigned long) (field[1])) << 8)
526 | (((unsigned long) (field[2])) << 16)
527 | (((unsigned long) (field[3])) << 24);
529 #ifdef BFD64
530 case 8:
531 case -8:
532 /* This is a special case, generated by the BYTE_GET8 macro.
533 It means that we are loading an 8 byte value from a field
534 in an external structure into an 8 byte value in a field
535 in an internal strcuture. */
536 return ((bfd_vma) (field[0]))
537 | (((bfd_vma) (field[1])) << 8)
538 | (((bfd_vma) (field[2])) << 16)
539 | (((bfd_vma) (field[3])) << 24)
540 | (((bfd_vma) (field[4])) << 32)
541 | (((bfd_vma) (field[5])) << 40)
542 | (((bfd_vma) (field[6])) << 48)
543 | (((bfd_vma) (field[7])) << 56);
544 #endif
545 default:
546 error (_("Unhandled data length: %d\n"), size);
547 abort ();
551 /* Print a VMA value. */
552 static void
553 print_vma (vma, mode)
554 bfd_vma vma;
555 print_mode mode;
557 #ifdef BFD64
558 if (is_32bit_elf)
559 #endif
561 switch (mode)
563 case FULL_HEX: printf ("0x"); /* drop through */
564 case LONG_HEX: printf ("%8.8lx", (unsigned long) vma); break;
565 case PREFIX_HEX: printf ("0x"); /* drop through */
566 case HEX: printf ("%lx", (unsigned long) vma); break;
567 case DEC: printf ("%ld", (unsigned long) vma); break;
568 case DEC_5: printf ("%5ld", (long) vma); break;
569 case UNSIGNED: printf ("%lu", (unsigned long) vma); break;
572 #ifdef BFD64
573 else
575 switch (mode)
577 case FULL_HEX:
578 printf ("0x");
579 /* drop through */
581 case LONG_HEX:
582 printf_vma (vma);
583 break;
585 case PREFIX_HEX:
586 printf ("0x");
587 /* drop through */
589 case HEX:
590 #if BFD_HOST_64BIT_LONG
591 printf ("%lx", vma);
592 #else
593 if (_bfd_int64_high (vma))
594 printf ("%lx%8.8lx", _bfd_int64_high (vma), _bfd_int64_low (vma));
595 else
596 printf ("%lx", _bfd_int64_low (vma));
597 #endif
598 break;
600 case DEC:
601 #if BFD_HOST_64BIT_LONG
602 printf ("%ld", vma);
603 #else
604 if (_bfd_int64_high (vma))
605 /* ugg */
606 printf ("++%ld", _bfd_int64_low (vma));
607 else
608 printf ("%ld", _bfd_int64_low (vma));
609 #endif
610 break;
612 case DEC_5:
613 #if BFD_HOST_64BIT_LONG
614 printf ("%5ld", vma);
615 #else
616 if (_bfd_int64_high (vma))
617 /* ugg */
618 printf ("++%ld", _bfd_int64_low (vma));
619 else
620 printf ("%5ld", _bfd_int64_low (vma));
621 #endif
622 break;
624 case UNSIGNED:
625 #if BFD_HOST_64BIT_LONG
626 printf ("%lu", vma);
627 #else
628 if (_bfd_int64_high (vma))
629 /* ugg */
630 printf ("++%lu", _bfd_int64_low (vma));
631 else
632 printf ("%lu", _bfd_int64_low (vma));
633 #endif
634 break;
637 #endif
640 /* Display a symbol on stdout. If do_wide is not true then
641 format the symbol to be at most WIDTH characters,
642 truncating as necessary. If WIDTH is negative then
643 format the string to be exactly - WIDTH characters,
644 truncating or padding as necessary. */
646 static void
647 print_symbol (width, symbol)
648 int width;
649 const char *symbol;
651 if (do_wide)
652 printf ("%s", symbol);
653 else if (width < 0)
654 printf ("%-*.*s", width, width, symbol);
655 else
656 printf ("%-.*s", width, symbol);
659 static bfd_vma
660 byte_get_big_endian (field, size)
661 unsigned char *field;
662 int size;
664 switch (size)
666 case 1:
667 return *field;
669 case 2:
670 return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
672 case 4:
673 return ((unsigned long) (field[3]))
674 | (((unsigned long) (field[2])) << 8)
675 | (((unsigned long) (field[1])) << 16)
676 | (((unsigned long) (field[0])) << 24);
678 #ifndef BFD64
679 case 8:
680 /* Although we are extracing data from an 8 byte wide field, we
681 are returning only 4 bytes of data. */
682 return ((unsigned long) (field[7]))
683 | (((unsigned long) (field[6])) << 8)
684 | (((unsigned long) (field[5])) << 16)
685 | (((unsigned long) (field[4])) << 24);
686 #else
687 case 8:
688 case -8:
689 /* This is a special case, generated by the BYTE_GET8 macro.
690 It means that we are loading an 8 byte value from a field
691 in an external structure into an 8 byte value in a field
692 in an internal strcuture. */
693 return ((bfd_vma) (field[7]))
694 | (((bfd_vma) (field[6])) << 8)
695 | (((bfd_vma) (field[5])) << 16)
696 | (((bfd_vma) (field[4])) << 24)
697 | (((bfd_vma) (field[3])) << 32)
698 | (((bfd_vma) (field[2])) << 40)
699 | (((bfd_vma) (field[1])) << 48)
700 | (((bfd_vma) (field[0])) << 56);
701 #endif
703 default:
704 error (_("Unhandled data length: %d\n"), size);
705 abort ();
709 /* Guess the relocation size commonly used by the specific machines. */
711 static int
712 guess_is_rela (e_machine)
713 unsigned long e_machine;
715 switch (e_machine)
717 /* Targets that use REL relocations. */
718 case EM_ARM:
719 case EM_386:
720 case EM_486:
721 case EM_960:
722 case EM_DLX:
723 case EM_OPENRISC:
724 case EM_OR32:
725 case EM_M32R:
726 case EM_CYGNUS_M32R:
727 case EM_D10V:
728 case EM_CYGNUS_D10V:
729 case EM_MIPS:
730 case EM_MIPS_RS3_LE:
731 return FALSE;
733 /* Targets that use RELA relocations. */
734 case EM_68K:
735 case EM_H8_300:
736 case EM_H8_300H:
737 case EM_H8S:
738 case EM_SPARC32PLUS:
739 case EM_SPARCV9:
740 case EM_SPARC:
741 case EM_PPC:
742 case EM_PPC64:
743 case EM_V850:
744 case EM_CYGNUS_V850:
745 case EM_D30V:
746 case EM_CYGNUS_D30V:
747 case EM_MN10200:
748 case EM_CYGNUS_MN10200:
749 case EM_MN10300:
750 case EM_CYGNUS_MN10300:
751 case EM_FR30:
752 case EM_CYGNUS_FR30:
753 case EM_CYGNUS_FRV:
754 case EM_SH:
755 case EM_ALPHA:
756 case EM_MCORE:
757 case EM_IA_64:
758 case EM_AVR:
759 case EM_AVR_OLD:
760 case EM_CRIS:
761 case EM_860:
762 case EM_X86_64:
763 case EM_S390:
764 case EM_S390_OLD:
765 case EM_MMIX:
766 case EM_MSP430:
767 case EM_MSP430_OLD:
768 case EM_XSTORMY16:
769 case EM_VAX:
770 case EM_IP2K:
771 case EM_IP2K_OLD:
772 case EM_IQ2000:
773 return TRUE;
775 case EM_MMA:
776 case EM_PCP:
777 case EM_NCPU:
778 case EM_NDR1:
779 case EM_STARCORE:
780 case EM_ME16:
781 case EM_ST100:
782 case EM_TINYJ:
783 case EM_FX66:
784 case EM_ST9PLUS:
785 case EM_ST7:
786 case EM_68HC16:
787 case EM_68HC11:
788 case EM_68HC08:
789 case EM_68HC05:
790 case EM_SVX:
791 case EM_ST19:
792 default:
793 warn (_("Don't know about relocations on this machine architecture\n"));
794 return FALSE;
798 static int
799 slurp_rela_relocs (file, rel_offset, rel_size, relasp, nrelasp)
800 FILE *file;
801 unsigned long rel_offset;
802 unsigned long rel_size;
803 Elf_Internal_Rela **relasp;
804 unsigned long *nrelasp;
806 Elf_Internal_Rela *relas;
807 unsigned long nrelas;
808 unsigned int i;
810 if (is_32bit_elf)
812 Elf32_External_Rela *erelas;
814 erelas = (Elf32_External_Rela *) get_data (NULL, file, rel_offset,
815 rel_size, _("relocs"));
816 if (!erelas)
817 return 0;
819 nrelas = rel_size / sizeof (Elf32_External_Rela);
821 relas = (Elf_Internal_Rela *)
822 malloc (nrelas * sizeof (Elf_Internal_Rela));
824 if (relas == NULL)
826 error(_("out of memory parsing relocs"));
827 return 0;
830 for (i = 0; i < nrelas; i++)
832 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
833 relas[i].r_info = BYTE_GET (erelas[i].r_info);
834 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
837 free (erelas);
839 else
841 Elf64_External_Rela *erelas;
843 erelas = (Elf64_External_Rela *) get_data (NULL, file, rel_offset,
844 rel_size, _("relocs"));
845 if (!erelas)
846 return 0;
848 nrelas = rel_size / sizeof (Elf64_External_Rela);
850 relas = (Elf_Internal_Rela *)
851 malloc (nrelas * sizeof (Elf_Internal_Rela));
853 if (relas == NULL)
855 error(_("out of memory parsing relocs"));
856 return 0;
859 for (i = 0; i < nrelas; i++)
861 relas[i].r_offset = BYTE_GET8 (erelas[i].r_offset);
862 relas[i].r_info = BYTE_GET8 (erelas[i].r_info);
863 relas[i].r_addend = BYTE_GET8 (erelas[i].r_addend);
866 free (erelas);
868 *relasp = relas;
869 *nrelasp = nrelas;
870 return 1;
873 static int
874 slurp_rel_relocs (file, rel_offset, rel_size, relsp, nrelsp)
875 FILE *file;
876 unsigned long rel_offset;
877 unsigned long rel_size;
878 Elf_Internal_Rela **relsp;
879 unsigned long *nrelsp;
881 Elf_Internal_Rela *rels;
882 unsigned long nrels;
883 unsigned int i;
885 if (is_32bit_elf)
887 Elf32_External_Rel *erels;
889 erels = (Elf32_External_Rel *) get_data (NULL, file, rel_offset,
890 rel_size, _("relocs"));
891 if (!erels)
892 return 0;
894 nrels = rel_size / sizeof (Elf32_External_Rel);
896 rels = (Elf_Internal_Rela *) malloc (nrels * sizeof (Elf_Internal_Rela));
898 if (rels == NULL)
900 error(_("out of memory parsing relocs"));
901 return 0;
904 for (i = 0; i < nrels; i++)
906 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
907 rels[i].r_info = BYTE_GET (erels[i].r_info);
908 rels[i].r_addend = 0;
911 free (erels);
913 else
915 Elf64_External_Rel *erels;
917 erels = (Elf64_External_Rel *) get_data (NULL, file, rel_offset,
918 rel_size, _("relocs"));
919 if (!erels)
920 return 0;
922 nrels = rel_size / sizeof (Elf64_External_Rel);
924 rels = (Elf_Internal_Rela *) malloc (nrels * sizeof (Elf_Internal_Rela));
926 if (rels == NULL)
928 error(_("out of memory parsing relocs"));
929 return 0;
932 for (i = 0; i < nrels; i++)
934 rels[i].r_offset = BYTE_GET8 (erels[i].r_offset);
935 rels[i].r_info = BYTE_GET8 (erels[i].r_info);
936 rels[i].r_addend = 0;
939 free (erels);
941 *relsp = rels;
942 *nrelsp = nrels;
943 return 1;
946 /* Display the contents of the relocation data found at the specified offset. */
948 static int
949 dump_relocations (file, rel_offset, rel_size, symtab, nsyms, strtab, is_rela)
950 FILE *file;
951 unsigned long rel_offset;
952 unsigned long rel_size;
953 Elf_Internal_Sym *symtab;
954 unsigned long nsyms;
955 char *strtab;
956 int is_rela;
958 unsigned int i;
959 Elf_Internal_Rela *rels;
962 if (is_rela == UNKNOWN)
963 is_rela = guess_is_rela (elf_header.e_machine);
965 if (is_rela)
967 if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
968 return 0;
970 else
972 if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
973 return 0;
976 if (is_32bit_elf)
978 if (is_rela)
980 if (do_wide)
981 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
982 else
983 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
985 else
987 if (do_wide)
988 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
989 else
990 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
993 else
995 if (is_rela)
997 if (do_wide)
998 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
999 else
1000 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
1002 else
1004 if (do_wide)
1005 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
1006 else
1007 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
1011 for (i = 0; i < rel_size; i++)
1013 const char *rtype;
1014 const char *rtype2 = NULL;
1015 const char *rtype3 = NULL;
1016 bfd_vma offset;
1017 bfd_vma info;
1018 bfd_vma symtab_index;
1019 bfd_vma type;
1020 bfd_vma type2 = (bfd_vma) NULL;
1021 bfd_vma type3 = (bfd_vma) NULL;
1023 offset = rels[i].r_offset;
1024 info = rels[i].r_info;
1026 if (is_32bit_elf)
1028 type = ELF32_R_TYPE (info);
1029 symtab_index = ELF32_R_SYM (info);
1031 else
1033 /* The #ifdef BFD64 below is to prevent a compile time warning.
1034 We know that if we do not have a 64 bit data type that we
1035 will never execute this code anyway. */
1036 #ifdef BFD64
1037 if (elf_header.e_machine == EM_MIPS)
1039 /* In little-endian objects, r_info isn't really a 64-bit
1040 little-endian value: it has a 32-bit little-endian
1041 symbol index followed by four individual byte fields.
1042 Reorder INFO accordingly. */
1043 if (elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
1044 info = (((info & 0xffffffff) << 32)
1045 | ((info >> 56) & 0xff)
1046 | ((info >> 40) & 0xff00)
1047 | ((info >> 24) & 0xff0000)
1048 | ((info >> 8) & 0xff000000));
1049 type = ELF64_MIPS_R_TYPE (info);
1050 type2 = ELF64_MIPS_R_TYPE2 (info);
1051 type3 = ELF64_MIPS_R_TYPE3 (info);
1053 else if (elf_header.e_machine == EM_SPARCV9)
1054 type = ELF64_R_TYPE_ID (info);
1055 else
1056 type = ELF64_R_TYPE (info);
1058 symtab_index = ELF64_R_SYM (info);
1059 #endif
1062 if (is_32bit_elf)
1064 #ifdef _bfd_int64_low
1065 printf ("%8.8lx %8.8lx ", _bfd_int64_low (offset), _bfd_int64_low (info));
1066 #else
1067 printf ("%8.8lx %8.8lx ", offset, info);
1068 #endif
1070 else
1072 #ifdef _bfd_int64_low
1073 printf (do_wide
1074 ? "%8.8lx%8.8lx %8.8lx%8.8lx "
1075 : "%4.4lx%8.8lx %4.4lx%8.8lx ",
1076 _bfd_int64_high (offset),
1077 _bfd_int64_low (offset),
1078 _bfd_int64_high (info),
1079 _bfd_int64_low (info));
1080 #else
1081 printf (do_wide
1082 ? "%16.16lx %16.16lx "
1083 : "%12.12lx %12.12lx ",
1084 offset, info);
1085 #endif
1088 switch (elf_header.e_machine)
1090 default:
1091 rtype = NULL;
1092 break;
1094 case EM_M32R:
1095 case EM_CYGNUS_M32R:
1096 rtype = elf_m32r_reloc_type (type);
1097 break;
1099 case EM_386:
1100 case EM_486:
1101 rtype = elf_i386_reloc_type (type);
1102 break;
1104 case EM_68HC11:
1105 case EM_68HC12:
1106 rtype = elf_m68hc11_reloc_type (type);
1107 break;
1109 case EM_68K:
1110 rtype = elf_m68k_reloc_type (type);
1111 break;
1113 case EM_960:
1114 rtype = elf_i960_reloc_type (type);
1115 break;
1117 case EM_AVR:
1118 case EM_AVR_OLD:
1119 rtype = elf_avr_reloc_type (type);
1120 break;
1122 case EM_OLD_SPARCV9:
1123 case EM_SPARC32PLUS:
1124 case EM_SPARCV9:
1125 case EM_SPARC:
1126 rtype = elf_sparc_reloc_type (type);
1127 break;
1129 case EM_V850:
1130 case EM_CYGNUS_V850:
1131 rtype = v850_reloc_type (type);
1132 break;
1134 case EM_D10V:
1135 case EM_CYGNUS_D10V:
1136 rtype = elf_d10v_reloc_type (type);
1137 break;
1139 case EM_D30V:
1140 case EM_CYGNUS_D30V:
1141 rtype = elf_d30v_reloc_type (type);
1142 break;
1144 case EM_DLX:
1145 rtype = elf_dlx_reloc_type (type);
1146 break;
1148 case EM_SH:
1149 rtype = elf_sh_reloc_type (type);
1150 break;
1152 case EM_MN10300:
1153 case EM_CYGNUS_MN10300:
1154 rtype = elf_mn10300_reloc_type (type);
1155 break;
1157 case EM_MN10200:
1158 case EM_CYGNUS_MN10200:
1159 rtype = elf_mn10200_reloc_type (type);
1160 break;
1162 case EM_FR30:
1163 case EM_CYGNUS_FR30:
1164 rtype = elf_fr30_reloc_type (type);
1165 break;
1167 case EM_CYGNUS_FRV:
1168 rtype = elf_frv_reloc_type (type);
1169 break;
1171 case EM_MCORE:
1172 rtype = elf_mcore_reloc_type (type);
1173 break;
1175 case EM_MMIX:
1176 rtype = elf_mmix_reloc_type (type);
1177 break;
1179 case EM_MSP430:
1180 case EM_MSP430_OLD:
1181 rtype = elf_msp430_reloc_type (type);
1182 break;
1184 case EM_PPC:
1185 rtype = elf_ppc_reloc_type (type);
1186 break;
1188 case EM_PPC64:
1189 rtype = elf_ppc64_reloc_type (type);
1190 break;
1192 case EM_MIPS:
1193 case EM_MIPS_RS3_LE:
1194 rtype = elf_mips_reloc_type (type);
1195 if (!is_32bit_elf)
1197 rtype2 = elf_mips_reloc_type (type2);
1198 rtype3 = elf_mips_reloc_type (type3);
1200 break;
1202 case EM_ALPHA:
1203 rtype = elf_alpha_reloc_type (type);
1204 break;
1206 case EM_ARM:
1207 rtype = elf_arm_reloc_type (type);
1208 break;
1210 case EM_ARC:
1211 rtype = elf_arc_reloc_type (type);
1212 break;
1214 case EM_PARISC:
1215 rtype = elf_hppa_reloc_type (type);
1216 break;
1218 case EM_H8_300:
1219 case EM_H8_300H:
1220 case EM_H8S:
1221 rtype = elf_h8_reloc_type (type);
1222 break;
1224 case EM_OPENRISC:
1225 case EM_OR32:
1226 rtype = elf_or32_reloc_type (type);
1227 break;
1229 case EM_PJ:
1230 case EM_PJ_OLD:
1231 rtype = elf_pj_reloc_type (type);
1232 break;
1233 case EM_IA_64:
1234 rtype = elf_ia64_reloc_type (type);
1235 break;
1237 case EM_CRIS:
1238 rtype = elf_cris_reloc_type (type);
1239 break;
1241 case EM_860:
1242 rtype = elf_i860_reloc_type (type);
1243 break;
1245 case EM_X86_64:
1246 rtype = elf_x86_64_reloc_type (type);
1247 break;
1249 case EM_S370:
1250 rtype = i370_reloc_type (type);
1251 break;
1253 case EM_S390_OLD:
1254 case EM_S390:
1255 rtype = elf_s390_reloc_type (type);
1256 break;
1258 case EM_XSTORMY16:
1259 rtype = elf_xstormy16_reloc_type (type);
1260 break;
1262 case EM_VAX:
1263 rtype = elf_vax_reloc_type (type);
1264 break;
1266 case EM_IP2K:
1267 case EM_IP2K_OLD:
1268 rtype = elf_ip2k_reloc_type (type);
1269 break;
1271 case EM_IQ2000:
1272 rtype = elf_iq2000_reloc_type (type);
1273 break;
1276 if (rtype == NULL)
1277 #ifdef _bfd_int64_low
1278 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type));
1279 #else
1280 printf (_("unrecognized: %-7lx"), type);
1281 #endif
1282 else
1283 printf (do_wide ? "%-21.21s" : "%-17.17s", rtype);
1285 if (symtab_index)
1287 if (symtab == NULL || symtab_index >= nsyms)
1288 printf (" bad symbol index: %08lx", (unsigned long) symtab_index);
1289 else
1291 Elf_Internal_Sym *psym;
1293 psym = symtab + symtab_index;
1295 printf (" ");
1296 print_vma (psym->st_value, LONG_HEX);
1297 printf (is_32bit_elf ? " " : " ");
1299 if (psym->st_name == 0)
1301 const char *sec_name = "<null>";
1302 char name_buf[40];
1304 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1306 bfd_vma sec_index = (bfd_vma) -1;
1308 if (psym->st_shndx < SHN_LORESERVE)
1309 sec_index = psym->st_shndx;
1310 else if (psym->st_shndx > SHN_LORESERVE)
1311 sec_index = psym->st_shndx - (SHN_HIRESERVE + 1
1312 - SHN_LORESERVE);
1314 if (sec_index != (bfd_vma) -1)
1315 sec_name = SECTION_NAME (section_headers + sec_index);
1316 else if (psym->st_shndx == SHN_ABS)
1317 sec_name = "ABS";
1318 else if (psym->st_shndx == SHN_COMMON)
1319 sec_name = "COMMON";
1320 else
1322 sprintf (name_buf, "<section 0x%x>",
1323 (unsigned int) psym->st_shndx);
1324 sec_name = name_buf;
1327 print_symbol (22, sec_name);
1329 else if (strtab == NULL)
1330 printf (_("<string table index %3ld>"), psym->st_name);
1331 else
1332 print_symbol (22, strtab + psym->st_name);
1334 if (is_rela)
1335 printf (" + %lx", (unsigned long) rels[i].r_addend);
1338 else if (is_rela)
1340 printf ("%*c", is_32bit_elf ? (do_wide ? 34 : 28) : (do_wide ? 26 : 20), ' ');
1341 print_vma (rels[i].r_addend, LONG_HEX);
1344 if (elf_header.e_machine == EM_SPARCV9
1345 && !strcmp (rtype, "R_SPARC_OLO10"))
1346 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (info));
1348 putchar ('\n');
1350 if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1352 printf (" Type2: ");
1354 if (rtype2 == NULL)
1355 #ifdef _bfd_int64_low
1356 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type2));
1357 #else
1358 printf (_("unrecognized: %-7lx"), type2);
1359 #endif
1360 else
1361 printf ("%-17.17s", rtype2);
1363 printf("\n Type3: ");
1365 if (rtype3 == NULL)
1366 #ifdef _bfd_int64_low
1367 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type3));
1368 #else
1369 printf (_("unrecognized: %-7lx"), type3);
1370 #endif
1371 else
1372 printf ("%-17.17s", rtype3);
1374 putchar ('\n');
1378 free (rels);
1380 return 1;
1383 static const char *
1384 get_mips_dynamic_type (type)
1385 unsigned long type;
1387 switch (type)
1389 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1390 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1391 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1392 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1393 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1394 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1395 case DT_MIPS_MSYM: return "MIPS_MSYM";
1396 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1397 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1398 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1399 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1400 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1401 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1402 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1403 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1404 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1405 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1406 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1407 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1408 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1409 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1410 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1411 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1412 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1413 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1414 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1415 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1416 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1417 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1418 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1419 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1420 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1421 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1422 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1423 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1424 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1425 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1426 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1427 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1428 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1429 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1430 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1431 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1432 default:
1433 return NULL;
1437 static const char *
1438 get_sparc64_dynamic_type (type)
1439 unsigned long type;
1441 switch (type)
1443 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1444 default:
1445 return NULL;
1449 static const char *
1450 get_ppc64_dynamic_type (type)
1451 unsigned long type;
1453 switch (type)
1455 case DT_PPC64_GLINK: return "PPC64_GLINK";
1456 case DT_PPC64_OPD: return "PPC64_OPD";
1457 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1458 default:
1459 return NULL;
1463 static const char *
1464 get_parisc_dynamic_type (type)
1465 unsigned long type;
1467 switch (type)
1469 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1470 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1471 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1472 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1473 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1474 case DT_HP_PREINIT: return "HP_PREINIT";
1475 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1476 case DT_HP_NEEDED: return "HP_NEEDED";
1477 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1478 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1479 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1480 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1481 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
1482 default:
1483 return NULL;
1487 static const char *
1488 get_ia64_dynamic_type (type)
1489 unsigned long type;
1491 switch (type)
1493 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
1494 default:
1495 return NULL;
1499 static const char *
1500 get_dynamic_type (type)
1501 unsigned long type;
1503 static char buff[32];
1505 switch (type)
1507 case DT_NULL: return "NULL";
1508 case DT_NEEDED: return "NEEDED";
1509 case DT_PLTRELSZ: return "PLTRELSZ";
1510 case DT_PLTGOT: return "PLTGOT";
1511 case DT_HASH: return "HASH";
1512 case DT_STRTAB: return "STRTAB";
1513 case DT_SYMTAB: return "SYMTAB";
1514 case DT_RELA: return "RELA";
1515 case DT_RELASZ: return "RELASZ";
1516 case DT_RELAENT: return "RELAENT";
1517 case DT_STRSZ: return "STRSZ";
1518 case DT_SYMENT: return "SYMENT";
1519 case DT_INIT: return "INIT";
1520 case DT_FINI: return "FINI";
1521 case DT_SONAME: return "SONAME";
1522 case DT_RPATH: return "RPATH";
1523 case DT_SYMBOLIC: return "SYMBOLIC";
1524 case DT_REL: return "REL";
1525 case DT_RELSZ: return "RELSZ";
1526 case DT_RELENT: return "RELENT";
1527 case DT_PLTREL: return "PLTREL";
1528 case DT_DEBUG: return "DEBUG";
1529 case DT_TEXTREL: return "TEXTREL";
1530 case DT_JMPREL: return "JMPREL";
1531 case DT_BIND_NOW: return "BIND_NOW";
1532 case DT_INIT_ARRAY: return "INIT_ARRAY";
1533 case DT_FINI_ARRAY: return "FINI_ARRAY";
1534 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1535 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1536 case DT_RUNPATH: return "RUNPATH";
1537 case DT_FLAGS: return "FLAGS";
1539 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1540 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1542 case DT_CHECKSUM: return "CHECKSUM";
1543 case DT_PLTPADSZ: return "PLTPADSZ";
1544 case DT_MOVEENT: return "MOVEENT";
1545 case DT_MOVESZ: return "MOVESZ";
1546 case DT_FEATURE: return "FEATURE";
1547 case DT_POSFLAG_1: return "POSFLAG_1";
1548 case DT_SYMINSZ: return "SYMINSZ";
1549 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
1551 case DT_ADDRRNGLO: return "ADDRRNGLO";
1552 case DT_CONFIG: return "CONFIG";
1553 case DT_DEPAUDIT: return "DEPAUDIT";
1554 case DT_AUDIT: return "AUDIT";
1555 case DT_PLTPAD: return "PLTPAD";
1556 case DT_MOVETAB: return "MOVETAB";
1557 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
1559 case DT_VERSYM: return "VERSYM";
1561 case DT_RELACOUNT: return "RELACOUNT";
1562 case DT_RELCOUNT: return "RELCOUNT";
1563 case DT_FLAGS_1: return "FLAGS_1";
1564 case DT_VERDEF: return "VERDEF";
1565 case DT_VERDEFNUM: return "VERDEFNUM";
1566 case DT_VERNEED: return "VERNEED";
1567 case DT_VERNEEDNUM: return "VERNEEDNUM";
1569 case DT_AUXILIARY: return "AUXILIARY";
1570 case DT_USED: return "USED";
1571 case DT_FILTER: return "FILTER";
1573 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1574 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1575 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1576 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1577 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1579 default:
1580 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1582 const char *result;
1584 switch (elf_header.e_machine)
1586 case EM_MIPS:
1587 case EM_MIPS_RS3_LE:
1588 result = get_mips_dynamic_type (type);
1589 break;
1590 case EM_SPARCV9:
1591 result = get_sparc64_dynamic_type (type);
1592 break;
1593 case EM_PPC64:
1594 result = get_ppc64_dynamic_type (type);
1595 break;
1596 case EM_IA_64:
1597 result = get_ia64_dynamic_type (type);
1598 break;
1599 default:
1600 result = NULL;
1601 break;
1604 if (result != NULL)
1605 return result;
1607 sprintf (buff, _("Processor Specific: %lx"), type);
1609 else if ((type >= DT_LOOS) && (type <= DT_HIOS))
1611 const char *result;
1613 switch (elf_header.e_machine)
1615 case EM_PARISC:
1616 result = get_parisc_dynamic_type (type);
1617 break;
1618 default:
1619 result = NULL;
1620 break;
1623 if (result != NULL)
1624 return result;
1626 sprintf (buff, _("Operating System specific: %lx"), type);
1628 else
1629 sprintf (buff, _("<unknown>: %lx"), type);
1631 return buff;
1635 static char *
1636 get_file_type (e_type)
1637 unsigned e_type;
1639 static char buff[32];
1641 switch (e_type)
1643 case ET_NONE: return _("NONE (None)");
1644 case ET_REL: return _("REL (Relocatable file)");
1645 case ET_EXEC: return _("EXEC (Executable file)");
1646 case ET_DYN: return _("DYN (Shared object file)");
1647 case ET_CORE: return _("CORE (Core file)");
1649 default:
1650 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
1651 sprintf (buff, _("Processor Specific: (%x)"), e_type);
1652 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
1653 sprintf (buff, _("OS Specific: (%x)"), e_type);
1654 else
1655 sprintf (buff, _("<unknown>: %x"), e_type);
1656 return buff;
1660 static char *
1661 get_machine_name (e_machine)
1662 unsigned e_machine;
1664 static char buff[64]; /* XXX */
1666 switch (e_machine)
1668 case EM_NONE: return _("None");
1669 case EM_M32: return "WE32100";
1670 case EM_SPARC: return "Sparc";
1671 case EM_386: return "Intel 80386";
1672 case EM_68K: return "MC68000";
1673 case EM_88K: return "MC88000";
1674 case EM_486: return "Intel 80486";
1675 case EM_860: return "Intel 80860";
1676 case EM_MIPS: return "MIPS R3000";
1677 case EM_S370: return "IBM System/370";
1678 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
1679 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
1680 case EM_PARISC: return "HPPA";
1681 case EM_PPC_OLD: return "Power PC (old)";
1682 case EM_SPARC32PLUS: return "Sparc v8+" ;
1683 case EM_960: return "Intel 90860";
1684 case EM_PPC: return "PowerPC";
1685 case EM_PPC64: return "PowerPC64";
1686 case EM_V800: return "NEC V800";
1687 case EM_FR20: return "Fujitsu FR20";
1688 case EM_RH32: return "TRW RH32";
1689 case EM_MCORE: return "MCORE";
1690 case EM_ARM: return "ARM";
1691 case EM_OLD_ALPHA: return "Digital Alpha (old)";
1692 case EM_SH: return "Hitachi SH";
1693 case EM_SPARCV9: return "Sparc v9";
1694 case EM_TRICORE: return "Siemens Tricore";
1695 case EM_ARC: return "ARC";
1696 case EM_H8_300: return "Hitachi H8/300";
1697 case EM_H8_300H: return "Hitachi H8/300H";
1698 case EM_H8S: return "Hitachi H8S";
1699 case EM_H8_500: return "Hitachi H8/500";
1700 case EM_IA_64: return "Intel IA-64";
1701 case EM_MIPS_X: return "Stanford MIPS-X";
1702 case EM_COLDFIRE: return "Motorola Coldfire";
1703 case EM_68HC12: return "Motorola M68HC12";
1704 case EM_ALPHA: return "Alpha";
1705 case EM_CYGNUS_D10V:
1706 case EM_D10V: return "d10v";
1707 case EM_CYGNUS_D30V:
1708 case EM_D30V: return "d30v";
1709 case EM_CYGNUS_M32R:
1710 case EM_M32R: return "Mitsubishi M32r";
1711 case EM_CYGNUS_V850:
1712 case EM_V850: return "NEC v850";
1713 case EM_CYGNUS_MN10300:
1714 case EM_MN10300: return "mn10300";
1715 case EM_CYGNUS_MN10200:
1716 case EM_MN10200: return "mn10200";
1717 case EM_CYGNUS_FR30:
1718 case EM_FR30: return "Fujitsu FR30";
1719 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
1720 case EM_PJ_OLD:
1721 case EM_PJ: return "picoJava";
1722 case EM_MMA: return "Fujitsu Multimedia Accelerator";
1723 case EM_PCP: return "Siemens PCP";
1724 case EM_NCPU: return "Sony nCPU embedded RISC processor";
1725 case EM_NDR1: return "Denso NDR1 microprocesspr";
1726 case EM_STARCORE: return "Motorola Star*Core processor";
1727 case EM_ME16: return "Toyota ME16 processor";
1728 case EM_ST100: return "STMicroelectronics ST100 processor";
1729 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
1730 case EM_FX66: return "Siemens FX66 microcontroller";
1731 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
1732 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
1733 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
1734 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
1735 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
1736 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
1737 case EM_SVX: return "Silicon Graphics SVx";
1738 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
1739 case EM_VAX: return "Digital VAX";
1740 case EM_AVR_OLD:
1741 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
1742 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
1743 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
1744 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
1745 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
1746 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
1747 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
1748 case EM_PRISM: return "Vitesse Prism";
1749 case EM_X86_64: return "Advanced Micro Devices X86-64";
1750 case EM_S390_OLD:
1751 case EM_S390: return "IBM S/390";
1752 case EM_XSTORMY16: return "Sanyo Xstormy16 CPU core";
1753 case EM_OPENRISC:
1754 case EM_OR32: return "OpenRISC";
1755 case EM_DLX: return "OpenDLX";
1756 case EM_IP2K_OLD:
1757 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
1758 case EM_IQ2000: return "Vitesse IQ2000";
1759 default:
1760 sprintf (buff, _("<unknown>: %x"), e_machine);
1761 return buff;
1765 static void
1766 decode_ARM_machine_flags (e_flags, buf)
1767 unsigned e_flags;
1768 char buf[];
1770 unsigned eabi;
1771 int unknown = 0;
1773 eabi = EF_ARM_EABI_VERSION (e_flags);
1774 e_flags &= ~ EF_ARM_EABIMASK;
1776 /* Handle "generic" ARM flags. */
1777 if (e_flags & EF_ARM_RELEXEC)
1779 strcat (buf, ", relocatable executable");
1780 e_flags &= ~ EF_ARM_RELEXEC;
1783 if (e_flags & EF_ARM_HASENTRY)
1785 strcat (buf, ", has entry point");
1786 e_flags &= ~ EF_ARM_HASENTRY;
1789 /* Now handle EABI specific flags. */
1790 switch (eabi)
1792 default:
1793 strcat (buf, ", <unrecognized EABI>");
1794 if (e_flags)
1795 unknown = 1;
1796 break;
1798 case EF_ARM_EABI_VER1:
1799 strcat (buf, ", Version1 EABI");
1800 while (e_flags)
1802 unsigned flag;
1804 /* Process flags one bit at a time. */
1805 flag = e_flags & - e_flags;
1806 e_flags &= ~ flag;
1808 switch (flag)
1810 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1811 strcat (buf, ", sorted symbol tables");
1812 break;
1814 default:
1815 unknown = 1;
1816 break;
1819 break;
1821 case EF_ARM_EABI_VER2:
1822 strcat (buf, ", Version2 EABI");
1823 while (e_flags)
1825 unsigned flag;
1827 /* Process flags one bit at a time. */
1828 flag = e_flags & - e_flags;
1829 e_flags &= ~ flag;
1831 switch (flag)
1833 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1834 strcat (buf, ", sorted symbol tables");
1835 break;
1837 case EF_ARM_DYNSYMSUSESEGIDX:
1838 strcat (buf, ", dynamic symbols use segment index");
1839 break;
1841 case EF_ARM_MAPSYMSFIRST:
1842 strcat (buf, ", mapping symbols precede others");
1843 break;
1845 default:
1846 unknown = 1;
1847 break;
1850 break;
1852 case EF_ARM_EABI_UNKNOWN:
1853 strcat (buf, ", GNU EABI");
1854 while (e_flags)
1856 unsigned flag;
1858 /* Process flags one bit at a time. */
1859 flag = e_flags & - e_flags;
1860 e_flags &= ~ flag;
1862 switch (flag)
1864 case EF_ARM_INTERWORK:
1865 strcat (buf, ", interworking enabled");
1866 break;
1868 case EF_ARM_APCS_26:
1869 strcat (buf, ", uses APCS/26");
1870 break;
1872 case EF_ARM_APCS_FLOAT:
1873 strcat (buf, ", uses APCS/float");
1874 break;
1876 case EF_ARM_PIC:
1877 strcat (buf, ", position independent");
1878 break;
1880 case EF_ARM_ALIGN8:
1881 strcat (buf, ", 8 bit structure alignment");
1882 break;
1884 case EF_ARM_NEW_ABI:
1885 strcat (buf, ", uses new ABI");
1886 break;
1888 case EF_ARM_OLD_ABI:
1889 strcat (buf, ", uses old ABI");
1890 break;
1892 case EF_ARM_SOFT_FLOAT:
1893 strcat (buf, ", software FP");
1894 break;
1896 case EF_ARM_MAVERICK_FLOAT:
1897 strcat (buf, ", Maverick FP");
1898 break;
1900 default:
1901 unknown = 1;
1902 break;
1907 if (unknown)
1908 strcat (buf,", <unknown>");
1911 static char *
1912 get_machine_flags (e_flags, e_machine)
1913 unsigned e_flags;
1914 unsigned e_machine;
1916 static char buf[1024];
1918 buf[0] = '\0';
1920 if (e_flags)
1922 switch (e_machine)
1924 default:
1925 break;
1927 case EM_ARM:
1928 decode_ARM_machine_flags (e_flags, buf);
1929 break;
1931 case EM_68K:
1932 if (e_flags & EF_CPU32)
1933 strcat (buf, ", cpu32");
1934 if (e_flags & EF_M68000)
1935 strcat (buf, ", m68000");
1936 break;
1938 case EM_PPC:
1939 if (e_flags & EF_PPC_EMB)
1940 strcat (buf, ", emb");
1942 if (e_flags & EF_PPC_RELOCATABLE)
1943 strcat (buf, ", relocatable");
1945 if (e_flags & EF_PPC_RELOCATABLE_LIB)
1946 strcat (buf, ", relocatable-lib");
1947 break;
1949 case EM_V850:
1950 case EM_CYGNUS_V850:
1951 switch (e_flags & EF_V850_ARCH)
1953 case E_V850E_ARCH:
1954 strcat (buf, ", v850e");
1955 break;
1956 case E_V850_ARCH:
1957 strcat (buf, ", v850");
1958 break;
1959 default:
1960 strcat (buf, ", unknown v850 architecture variant");
1961 break;
1963 break;
1965 case EM_M32R:
1966 case EM_CYGNUS_M32R:
1967 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
1968 strcat (buf, ", m32r");
1970 break;
1972 case EM_MIPS:
1973 case EM_MIPS_RS3_LE:
1974 if (e_flags & EF_MIPS_NOREORDER)
1975 strcat (buf, ", noreorder");
1977 if (e_flags & EF_MIPS_PIC)
1978 strcat (buf, ", pic");
1980 if (e_flags & EF_MIPS_CPIC)
1981 strcat (buf, ", cpic");
1983 if (e_flags & EF_MIPS_UCODE)
1984 strcat (buf, ", ugen_reserved");
1986 if (e_flags & EF_MIPS_ABI2)
1987 strcat (buf, ", abi2");
1989 if (e_flags & EF_MIPS_OPTIONS_FIRST)
1990 strcat (buf, ", odk first");
1992 if (e_flags & EF_MIPS_32BITMODE)
1993 strcat (buf, ", 32bitmode");
1995 switch ((e_flags & EF_MIPS_MACH))
1997 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
1998 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
1999 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
2000 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
2001 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
2002 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
2003 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
2004 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
2005 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
2006 case 0:
2007 /* We simply ignore the field in this case to avoid confusion:
2008 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
2009 extension. */
2010 break;
2011 default: strcat (buf, ", unknown CPU"); break;
2014 switch ((e_flags & EF_MIPS_ABI))
2016 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
2017 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
2018 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
2019 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
2020 case 0:
2021 /* We simply ignore the field in this case to avoid confusion:
2022 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
2023 This means it is likely to be an o32 file, but not for
2024 sure. */
2025 break;
2026 default: strcat (buf, ", unknown ABI"); break;
2029 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
2030 strcat (buf, ", mdmx");
2032 if (e_flags & EF_MIPS_ARCH_ASE_M16)
2033 strcat (buf, ", mips16");
2035 switch ((e_flags & EF_MIPS_ARCH))
2037 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
2038 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
2039 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
2040 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
2041 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
2042 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
2043 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
2044 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
2045 default: strcat (buf, ", unknown ISA"); break;
2048 break;
2050 case EM_SPARCV9:
2051 if (e_flags & EF_SPARC_32PLUS)
2052 strcat (buf, ", v8+");
2054 if (e_flags & EF_SPARC_SUN_US1)
2055 strcat (buf, ", ultrasparcI");
2057 if (e_flags & EF_SPARC_SUN_US3)
2058 strcat (buf, ", ultrasparcIII");
2060 if (e_flags & EF_SPARC_HAL_R1)
2061 strcat (buf, ", halr1");
2063 if (e_flags & EF_SPARC_LEDATA)
2064 strcat (buf, ", ledata");
2066 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
2067 strcat (buf, ", tso");
2069 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
2070 strcat (buf, ", pso");
2072 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
2073 strcat (buf, ", rmo");
2074 break;
2076 case EM_PARISC:
2077 switch (e_flags & EF_PARISC_ARCH)
2079 case EFA_PARISC_1_0:
2080 strcpy (buf, ", PA-RISC 1.0");
2081 break;
2082 case EFA_PARISC_1_1:
2083 strcpy (buf, ", PA-RISC 1.1");
2084 break;
2085 case EFA_PARISC_2_0:
2086 strcpy (buf, ", PA-RISC 2.0");
2087 break;
2088 default:
2089 break;
2091 if (e_flags & EF_PARISC_TRAPNIL)
2092 strcat (buf, ", trapnil");
2093 if (e_flags & EF_PARISC_EXT)
2094 strcat (buf, ", ext");
2095 if (e_flags & EF_PARISC_LSB)
2096 strcat (buf, ", lsb");
2097 if (e_flags & EF_PARISC_WIDE)
2098 strcat (buf, ", wide");
2099 if (e_flags & EF_PARISC_NO_KABP)
2100 strcat (buf, ", no kabp");
2101 if (e_flags & EF_PARISC_LAZYSWAP)
2102 strcat (buf, ", lazyswap");
2103 break;
2105 case EM_PJ:
2106 case EM_PJ_OLD:
2107 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
2108 strcat (buf, ", new calling convention");
2110 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
2111 strcat (buf, ", gnu calling convention");
2112 break;
2114 case EM_IA_64:
2115 if ((e_flags & EF_IA_64_ABI64))
2116 strcat (buf, ", 64-bit");
2117 else
2118 strcat (buf, ", 32-bit");
2119 if ((e_flags & EF_IA_64_REDUCEDFP))
2120 strcat (buf, ", reduced fp model");
2121 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
2122 strcat (buf, ", no function descriptors, constant gp");
2123 else if ((e_flags & EF_IA_64_CONS_GP))
2124 strcat (buf, ", constant gp");
2125 if ((e_flags & EF_IA_64_ABSOLUTE))
2126 strcat (buf, ", absolute");
2127 break;
2129 case EM_VAX:
2130 if ((e_flags & EF_VAX_NONPIC))
2131 strcat (buf, ", non-PIC");
2132 if ((e_flags & EF_VAX_DFLOAT))
2133 strcat (buf, ", D-Float");
2134 if ((e_flags & EF_VAX_GFLOAT))
2135 strcat (buf, ", G-Float");
2136 break;
2140 return buf;
2143 static const char *
2144 get_mips_segment_type (type)
2145 unsigned long type;
2147 switch (type)
2149 case PT_MIPS_REGINFO:
2150 return "REGINFO";
2151 case PT_MIPS_RTPROC:
2152 return "RTPROC";
2153 case PT_MIPS_OPTIONS:
2154 return "OPTIONS";
2155 default:
2156 break;
2159 return NULL;
2162 static const char *
2163 get_parisc_segment_type (type)
2164 unsigned long type;
2166 switch (type)
2168 case PT_HP_TLS: return "HP_TLS";
2169 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
2170 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
2171 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
2172 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
2173 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
2174 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
2175 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
2176 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
2177 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
2178 case PT_HP_PARALLEL: return "HP_PARALLEL";
2179 case PT_HP_FASTBIND: return "HP_FASTBIND";
2180 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
2181 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
2182 default:
2183 break;
2186 return NULL;
2189 static const char *
2190 get_ia64_segment_type (type)
2191 unsigned long type;
2193 switch (type)
2195 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
2196 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
2197 case PT_HP_TLS: return "HP_TLS";
2198 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
2199 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
2200 case PT_IA_64_HP_STACK: return "HP_STACK";
2201 default:
2202 break;
2205 return NULL;
2208 static const char *
2209 get_segment_type (p_type)
2210 unsigned long p_type;
2212 static char buff[32];
2214 switch (p_type)
2216 case PT_NULL: return "NULL";
2217 case PT_LOAD: return "LOAD";
2218 case PT_DYNAMIC: return "DYNAMIC";
2219 case PT_INTERP: return "INTERP";
2220 case PT_NOTE: return "NOTE";
2221 case PT_SHLIB: return "SHLIB";
2222 case PT_PHDR: return "PHDR";
2223 case PT_TLS: return "TLS";
2225 case PT_GNU_EH_FRAME:
2226 return "GNU_EH_FRAME";
2228 default:
2229 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
2231 const char *result;
2233 switch (elf_header.e_machine)
2235 case EM_MIPS:
2236 case EM_MIPS_RS3_LE:
2237 result = get_mips_segment_type (p_type);
2238 break;
2239 case EM_PARISC:
2240 result = get_parisc_segment_type (p_type);
2241 break;
2242 case EM_IA_64:
2243 result = get_ia64_segment_type (p_type);
2244 break;
2245 default:
2246 result = NULL;
2247 break;
2250 if (result != NULL)
2251 return result;
2253 sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
2255 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
2257 const char *result;
2259 switch (elf_header.e_machine)
2261 case EM_PARISC:
2262 result = get_parisc_segment_type (p_type);
2263 break;
2264 case EM_IA_64:
2265 result = get_ia64_segment_type (p_type);
2266 break;
2267 default:
2268 result = NULL;
2269 break;
2272 if (result != NULL)
2273 return result;
2275 sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
2277 else
2278 sprintf (buff, _("<unknown>: %lx"), p_type);
2280 return buff;
2284 static const char *
2285 get_mips_section_type_name (sh_type)
2286 unsigned int sh_type;
2288 switch (sh_type)
2290 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2291 case SHT_MIPS_MSYM: return "MIPS_MSYM";
2292 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2293 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
2294 case SHT_MIPS_UCODE: return "MIPS_UCODE";
2295 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
2296 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
2297 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
2298 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
2299 case SHT_MIPS_RELD: return "MIPS_RELD";
2300 case SHT_MIPS_IFACE: return "MIPS_IFACE";
2301 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
2302 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2303 case SHT_MIPS_SHDR: return "MIPS_SHDR";
2304 case SHT_MIPS_FDESC: return "MIPS_FDESC";
2305 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
2306 case SHT_MIPS_DENSE: return "MIPS_DENSE";
2307 case SHT_MIPS_PDESC: return "MIPS_PDESC";
2308 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
2309 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
2310 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
2311 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
2312 case SHT_MIPS_LINE: return "MIPS_LINE";
2313 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
2314 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
2315 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
2316 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
2317 case SHT_MIPS_DWARF: return "MIPS_DWARF";
2318 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
2319 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2320 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
2321 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
2322 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
2323 case SHT_MIPS_XLATE: return "MIPS_XLATE";
2324 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
2325 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
2326 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
2327 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
2328 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
2329 default:
2330 break;
2332 return NULL;
2335 static const char *
2336 get_parisc_section_type_name (sh_type)
2337 unsigned int sh_type;
2339 switch (sh_type)
2341 case SHT_PARISC_EXT: return "PARISC_EXT";
2342 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
2343 case SHT_PARISC_DOC: return "PARISC_DOC";
2344 default:
2345 break;
2347 return NULL;
2350 static const char *
2351 get_ia64_section_type_name (sh_type)
2352 unsigned int sh_type;
2354 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
2355 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
2356 return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
2358 switch (sh_type)
2360 case SHT_IA_64_EXT: return "IA_64_EXT";
2361 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
2362 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
2363 default:
2364 break;
2366 return NULL;
2369 static const char *
2370 get_section_type_name (sh_type)
2371 unsigned int sh_type;
2373 static char buff[32];
2375 switch (sh_type)
2377 case SHT_NULL: return "NULL";
2378 case SHT_PROGBITS: return "PROGBITS";
2379 case SHT_SYMTAB: return "SYMTAB";
2380 case SHT_STRTAB: return "STRTAB";
2381 case SHT_RELA: return "RELA";
2382 case SHT_HASH: return "HASH";
2383 case SHT_DYNAMIC: return "DYNAMIC";
2384 case SHT_NOTE: return "NOTE";
2385 case SHT_NOBITS: return "NOBITS";
2386 case SHT_REL: return "REL";
2387 case SHT_SHLIB: return "SHLIB";
2388 case SHT_DYNSYM: return "DYNSYM";
2389 case SHT_INIT_ARRAY: return "INIT_ARRAY";
2390 case SHT_FINI_ARRAY: return "FINI_ARRAY";
2391 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2392 case SHT_GROUP: return "GROUP";
2393 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICIES";
2394 case SHT_GNU_verdef: return "VERDEF";
2395 case SHT_GNU_verneed: return "VERNEED";
2396 case SHT_GNU_versym: return "VERSYM";
2397 case 0x6ffffff0: return "VERSYM";
2398 case 0x6ffffffc: return "VERDEF";
2399 case 0x7ffffffd: return "AUXILIARY";
2400 case 0x7fffffff: return "FILTER";
2401 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
2403 default:
2404 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
2406 const char *result;
2408 switch (elf_header.e_machine)
2410 case EM_MIPS:
2411 case EM_MIPS_RS3_LE:
2412 result = get_mips_section_type_name (sh_type);
2413 break;
2414 case EM_PARISC:
2415 result = get_parisc_section_type_name (sh_type);
2416 break;
2417 case EM_IA_64:
2418 result = get_ia64_section_type_name (sh_type);
2419 break;
2420 default:
2421 result = NULL;
2422 break;
2425 if (result != NULL)
2426 return result;
2428 sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
2430 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
2431 sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
2432 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
2433 sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
2434 else
2435 sprintf (buff, _("<unknown>: %x"), sh_type);
2437 return buff;
2441 #define OPTION_DEBUG_DUMP 512
2443 struct option options[] =
2445 {"all", no_argument, 0, 'a'},
2446 {"file-header", no_argument, 0, 'h'},
2447 {"program-headers", no_argument, 0, 'l'},
2448 {"headers", no_argument, 0, 'e'},
2449 {"histogram", no_argument, 0, 'I'},
2450 {"segments", no_argument, 0, 'l'},
2451 {"sections", no_argument, 0, 'S'},
2452 {"section-headers", no_argument, 0, 'S'},
2453 {"symbols", no_argument, 0, 's'},
2454 {"syms", no_argument, 0, 's'},
2455 {"relocs", no_argument, 0, 'r'},
2456 {"notes", no_argument, 0, 'n'},
2457 {"dynamic", no_argument, 0, 'd'},
2458 {"arch-specific", no_argument, 0, 'A'},
2459 {"version-info", no_argument, 0, 'V'},
2460 {"use-dynamic", no_argument, 0, 'D'},
2461 {"hex-dump", required_argument, 0, 'x'},
2462 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
2463 {"unwind", no_argument, 0, 'u'},
2464 #ifdef SUPPORT_DISASSEMBLY
2465 {"instruction-dump", required_argument, 0, 'i'},
2466 #endif
2468 {"version", no_argument, 0, 'v'},
2469 {"wide", no_argument, 0, 'W'},
2470 {"help", no_argument, 0, 'H'},
2471 {0, no_argument, 0, 0}
2474 static void
2475 usage ()
2477 fprintf (stdout, _("Usage: readelf <option(s)> elf-file(s)\n"));
2478 fprintf (stdout, _(" Display information about the contents of ELF format files\n"));
2479 fprintf (stdout, _(" Options are:\n\
2480 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
2481 -h --file-header Display the ELF file header\n\
2482 -l --program-headers Display the program headers\n\
2483 --segments An alias for --program-headers\n\
2484 -S --section-headers Display the sections' header\n\
2485 --sections An alias for --section-headers\n\
2486 -e --headers Equivalent to: -h -l -S\n\
2487 -s --syms Display the symbol table\n\
2488 --symbols An alias for --syms\n\
2489 -n --notes Display the core notes (if present)\n\
2490 -r --relocs Display the relocations (if present)\n\
2491 -u --unwind Display the unwind info (if present)\n\
2492 -d --dynamic Display the dynamic segment (if present)\n\
2493 -V --version-info Display the version sections (if present)\n\
2494 -A --arch-specific Display architecture specific information (if any).\n\
2495 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
2496 -x --hex-dump=<number> Dump the contents of section <number>\n\
2497 -w[liaprmfFso] or\n\
2498 --debug-dump[=line,=info,=abbrev,=pubnames,=ranges,=macro,=frames,=str,=loc]\n\
2499 Display the contents of DWARF2 debug sections\n"));
2500 #ifdef SUPPORT_DISASSEMBLY
2501 fprintf (stdout, _("\
2502 -i --instruction-dump=<number>\n\
2503 Disassemble the contents of section <number>\n"));
2504 #endif
2505 fprintf (stdout, _("\
2506 -I --histogram Display histogram of bucket list lengths\n\
2507 -W --wide Allow output width to exceed 80 characters\n\
2508 -H --help Display this information\n\
2509 -v --version Display the version number of readelf\n"));
2510 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
2512 exit (0);
2515 static void
2516 request_dump (section, type)
2517 unsigned int section;
2518 int type;
2520 if (section >= num_dump_sects)
2522 char *new_dump_sects;
2524 new_dump_sects = (char *) calloc (section + 1, 1);
2526 if (new_dump_sects == NULL)
2527 error (_("Out of memory allocating dump request table."));
2528 else
2530 /* Copy current flag settings. */
2531 memcpy (new_dump_sects, dump_sects, num_dump_sects);
2533 free (dump_sects);
2535 dump_sects = new_dump_sects;
2536 num_dump_sects = section + 1;
2540 if (dump_sects)
2541 dump_sects[section] |= type;
2543 return;
2546 static void
2547 parse_args (argc, argv)
2548 int argc;
2549 char **argv;
2551 int c;
2553 if (argc < 2)
2554 usage ();
2556 while ((c = getopt_long
2557 (argc, argv, "ersuahnldSDAIw::x:i:vVWH", options, NULL)) != EOF)
2559 char *cp;
2560 int section;
2562 switch (c)
2564 case 0:
2565 /* Long options. */
2566 break;
2567 case 'H':
2568 usage ();
2569 break;
2571 case 'a':
2572 do_syms++;
2573 do_reloc++;
2574 do_unwind++;
2575 do_dynamic++;
2576 do_header++;
2577 do_sections++;
2578 do_segments++;
2579 do_version++;
2580 do_histogram++;
2581 do_arch++;
2582 do_notes++;
2583 break;
2584 case 'e':
2585 do_header++;
2586 do_sections++;
2587 do_segments++;
2588 break;
2589 case 'A':
2590 do_arch++;
2591 break;
2592 case 'D':
2593 do_using_dynamic++;
2594 break;
2595 case 'r':
2596 do_reloc++;
2597 break;
2598 case 'u':
2599 do_unwind++;
2600 break;
2601 case 'h':
2602 do_header++;
2603 break;
2604 case 'l':
2605 do_segments++;
2606 break;
2607 case 's':
2608 do_syms++;
2609 break;
2610 case 'S':
2611 do_sections++;
2612 break;
2613 case 'd':
2614 do_dynamic++;
2615 break;
2616 case 'I':
2617 do_histogram++;
2618 break;
2619 case 'n':
2620 do_notes++;
2621 break;
2622 case 'x':
2623 do_dump++;
2624 section = strtoul (optarg, & cp, 0);
2625 if (! *cp && section >= 0)
2627 request_dump (section, HEX_DUMP);
2628 break;
2630 goto oops;
2631 case 'w':
2632 do_dump++;
2633 if (optarg == 0)
2634 do_debugging = 1;
2635 else
2637 unsigned int index = 0;
2639 do_debugging = 0;
2641 while (optarg[index])
2642 switch (optarg[index++])
2644 case 'i':
2645 case 'I':
2646 do_debug_info = 1;
2647 break;
2649 case 'a':
2650 case 'A':
2651 do_debug_abbrevs = 1;
2652 break;
2654 case 'l':
2655 case 'L':
2656 do_debug_lines = 1;
2657 break;
2659 case 'p':
2660 case 'P':
2661 do_debug_pubnames = 1;
2662 break;
2664 case 'r':
2665 case 'R':
2666 do_debug_aranges = 1;
2667 break;
2669 case 'F':
2670 do_debug_frames_interp = 1;
2671 case 'f':
2672 do_debug_frames = 1;
2673 break;
2675 case 'm':
2676 case 'M':
2677 do_debug_macinfo = 1;
2678 break;
2680 case 's':
2681 case 'S':
2682 do_debug_str = 1;
2683 break;
2685 case 'o':
2686 case 'O':
2687 do_debug_loc = 1;
2688 break;
2690 default:
2691 warn (_("Unrecognized debug option '%s'\n"), optarg);
2692 break;
2695 break;
2696 case OPTION_DEBUG_DUMP:
2697 do_dump++;
2698 if (optarg == 0)
2699 do_debugging = 1;
2700 else
2702 static const char *debug_dump_opt[]
2703 = { "line", "info", "abbrev", "pubnames", "ranges",
2704 "macro", "frames", "frames-interp", "str", "loc", NULL };
2705 unsigned int index;
2706 const char *p;
2708 do_debugging = 0;
2710 p = optarg;
2711 while (*p)
2713 for (index = 0; debug_dump_opt[index]; index++)
2715 size_t len = strlen (debug_dump_opt[index]);
2717 if (strncmp (p, debug_dump_opt[index], len) == 0
2718 && (p[len] == ',' || p[len] == '\0'))
2720 switch (p[0])
2722 case 'i':
2723 do_debug_info = 1;
2724 break;
2726 case 'a':
2727 do_debug_abbrevs = 1;
2728 break;
2730 case 'l':
2731 if (p[1] == 'i')
2732 do_debug_lines = 1;
2733 else
2734 do_debug_loc = 1;
2735 break;
2737 case 'p':
2738 do_debug_pubnames = 1;
2739 break;
2741 case 'r':
2742 do_debug_aranges = 1;
2743 break;
2745 case 'f':
2746 if (len > 6)
2747 do_debug_frames_interp = 1;
2748 do_debug_frames = 1;
2749 break;
2751 case 'm':
2752 do_debug_macinfo = 1;
2753 break;
2755 case 's':
2756 do_debug_str = 1;
2757 break;
2760 p += len;
2761 break;
2765 if (debug_dump_opt[index] == NULL)
2767 warn (_("Unrecognized debug option '%s'\n"), p);
2768 p = strchr (p, ',');
2769 if (p == NULL)
2770 break;
2773 if (*p == ',')
2774 p++;
2777 break;
2778 #ifdef SUPPORT_DISASSEMBLY
2779 case 'i':
2780 do_dump++;
2781 section = strtoul (optarg, & cp, 0);
2782 if (! *cp && section >= 0)
2784 request_dump (section, DISASS_DUMP);
2785 break;
2787 goto oops;
2788 #endif
2789 case 'v':
2790 print_version (program_name);
2791 break;
2792 case 'V':
2793 do_version++;
2794 break;
2795 case 'W':
2796 do_wide++;
2797 break;
2798 default:
2799 oops:
2800 /* xgettext:c-format */
2801 error (_("Invalid option '-%c'\n"), c);
2802 /* Drop through. */
2803 case '?':
2804 usage ();
2808 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
2809 && !do_segments && !do_header && !do_dump && !do_version
2810 && !do_histogram && !do_debugging && !do_arch && !do_notes)
2811 usage ();
2812 else if (argc < 3)
2814 warn (_("Nothing to do.\n"));
2815 usage();
2819 static const char *
2820 get_elf_class (elf_class)
2821 unsigned int elf_class;
2823 static char buff[32];
2825 switch (elf_class)
2827 case ELFCLASSNONE: return _("none");
2828 case ELFCLASS32: return "ELF32";
2829 case ELFCLASS64: return "ELF64";
2830 default:
2831 sprintf (buff, _("<unknown: %x>"), elf_class);
2832 return buff;
2836 static const char *
2837 get_data_encoding (encoding)
2838 unsigned int encoding;
2840 static char buff[32];
2842 switch (encoding)
2844 case ELFDATANONE: return _("none");
2845 case ELFDATA2LSB: return _("2's complement, little endian");
2846 case ELFDATA2MSB: return _("2's complement, big endian");
2847 default:
2848 sprintf (buff, _("<unknown: %x>"), encoding);
2849 return buff;
2853 static const char *
2854 get_osabi_name (osabi)
2855 unsigned int osabi;
2857 static char buff[32];
2859 switch (osabi)
2861 case ELFOSABI_NONE: return "UNIX - System V";
2862 case ELFOSABI_HPUX: return "UNIX - HP-UX";
2863 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
2864 case ELFOSABI_LINUX: return "UNIX - Linux";
2865 case ELFOSABI_HURD: return "GNU/Hurd";
2866 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
2867 case ELFOSABI_AIX: return "UNIX - AIX";
2868 case ELFOSABI_IRIX: return "UNIX - IRIX";
2869 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
2870 case ELFOSABI_TRU64: return "UNIX - TRU64";
2871 case ELFOSABI_MODESTO: return "Novell - Modesto";
2872 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
2873 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
2874 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
2875 case ELFOSABI_AROS: return "Amiga Research OS";
2876 case ELFOSABI_STANDALONE: return _("Standalone App");
2877 case ELFOSABI_ARM: return "ARM";
2878 default:
2879 sprintf (buff, _("<unknown: %x>"), osabi);
2880 return buff;
2884 /* Decode the data held in 'elf_header'. */
2886 static int
2887 process_file_header ()
2889 if ( elf_header.e_ident[EI_MAG0] != ELFMAG0
2890 || elf_header.e_ident[EI_MAG1] != ELFMAG1
2891 || elf_header.e_ident[EI_MAG2] != ELFMAG2
2892 || elf_header.e_ident[EI_MAG3] != ELFMAG3)
2894 error
2895 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
2896 return 0;
2899 if (do_header)
2901 int i;
2903 printf (_("ELF Header:\n"));
2904 printf (_(" Magic: "));
2905 for (i = 0; i < EI_NIDENT; i++)
2906 printf ("%2.2x ", elf_header.e_ident[i]);
2907 printf ("\n");
2908 printf (_(" Class: %s\n"),
2909 get_elf_class (elf_header.e_ident[EI_CLASS]));
2910 printf (_(" Data: %s\n"),
2911 get_data_encoding (elf_header.e_ident[EI_DATA]));
2912 printf (_(" Version: %d %s\n"),
2913 elf_header.e_ident[EI_VERSION],
2914 (elf_header.e_ident[EI_VERSION] == EV_CURRENT
2915 ? "(current)"
2916 : (elf_header.e_ident[EI_VERSION] != EV_NONE
2917 ? "<unknown: %lx>"
2918 : "")));
2919 printf (_(" OS/ABI: %s\n"),
2920 get_osabi_name (elf_header.e_ident[EI_OSABI]));
2921 printf (_(" ABI Version: %d\n"),
2922 elf_header.e_ident[EI_ABIVERSION]);
2923 printf (_(" Type: %s\n"),
2924 get_file_type (elf_header.e_type));
2925 printf (_(" Machine: %s\n"),
2926 get_machine_name (elf_header.e_machine));
2927 printf (_(" Version: 0x%lx\n"),
2928 (unsigned long) elf_header.e_version);
2930 printf (_(" Entry point address: "));
2931 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
2932 printf (_("\n Start of program headers: "));
2933 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
2934 printf (_(" (bytes into file)\n Start of section headers: "));
2935 print_vma ((bfd_vma) elf_header.e_shoff, DEC);
2936 printf (_(" (bytes into file)\n"));
2938 printf (_(" Flags: 0x%lx%s\n"),
2939 (unsigned long) elf_header.e_flags,
2940 get_machine_flags (elf_header.e_flags, elf_header.e_machine));
2941 printf (_(" Size of this header: %ld (bytes)\n"),
2942 (long) elf_header.e_ehsize);
2943 printf (_(" Size of program headers: %ld (bytes)\n"),
2944 (long) elf_header.e_phentsize);
2945 printf (_(" Number of program headers: %ld\n"),
2946 (long) elf_header.e_phnum);
2947 printf (_(" Size of section headers: %ld (bytes)\n"),
2948 (long) elf_header.e_shentsize);
2949 printf (_(" Number of section headers: %ld"),
2950 (long) elf_header.e_shnum);
2951 if (section_headers != NULL && elf_header.e_shnum == 0)
2952 printf (" (%ld)", (long) section_headers[0].sh_size);
2953 putc ('\n', stdout);
2954 printf (_(" Section header string table index: %ld"),
2955 (long) elf_header.e_shstrndx);
2956 if (section_headers != NULL && elf_header.e_shstrndx == SHN_XINDEX)
2957 printf (" (%ld)", (long) section_headers[0].sh_link);
2958 putc ('\n', stdout);
2961 if (section_headers != NULL)
2963 if (elf_header.e_shnum == 0)
2964 elf_header.e_shnum = section_headers[0].sh_size;
2965 if (elf_header.e_shstrndx == SHN_XINDEX)
2966 elf_header.e_shstrndx = section_headers[0].sh_link;
2967 free (section_headers);
2968 section_headers = NULL;
2971 return 1;
2975 static int
2976 get_32bit_program_headers (file, program_headers)
2977 FILE *file;
2978 Elf_Internal_Phdr *program_headers;
2980 Elf32_External_Phdr *phdrs;
2981 Elf32_External_Phdr *external;
2982 Elf_Internal_Phdr *internal;
2983 unsigned int i;
2985 phdrs = ((Elf32_External_Phdr *)
2986 get_data (NULL, file, elf_header.e_phoff,
2987 elf_header.e_phentsize * elf_header.e_phnum,
2988 _("program headers")));
2989 if (!phdrs)
2990 return 0;
2992 for (i = 0, internal = program_headers, external = phdrs;
2993 i < elf_header.e_phnum;
2994 i++, internal++, external++)
2996 internal->p_type = BYTE_GET (external->p_type);
2997 internal->p_offset = BYTE_GET (external->p_offset);
2998 internal->p_vaddr = BYTE_GET (external->p_vaddr);
2999 internal->p_paddr = BYTE_GET (external->p_paddr);
3000 internal->p_filesz = BYTE_GET (external->p_filesz);
3001 internal->p_memsz = BYTE_GET (external->p_memsz);
3002 internal->p_flags = BYTE_GET (external->p_flags);
3003 internal->p_align = BYTE_GET (external->p_align);
3006 free (phdrs);
3008 return 1;
3011 static int
3012 get_64bit_program_headers (file, program_headers)
3013 FILE *file;
3014 Elf_Internal_Phdr *program_headers;
3016 Elf64_External_Phdr *phdrs;
3017 Elf64_External_Phdr *external;
3018 Elf_Internal_Phdr *internal;
3019 unsigned int i;
3021 phdrs = ((Elf64_External_Phdr *)
3022 get_data (NULL, file, elf_header.e_phoff,
3023 elf_header.e_phentsize * elf_header.e_phnum,
3024 _("program headers")));
3025 if (!phdrs)
3026 return 0;
3028 for (i = 0, internal = program_headers, external = phdrs;
3029 i < elf_header.e_phnum;
3030 i++, internal++, external++)
3032 internal->p_type = BYTE_GET (external->p_type);
3033 internal->p_flags = BYTE_GET (external->p_flags);
3034 internal->p_offset = BYTE_GET8 (external->p_offset);
3035 internal->p_vaddr = BYTE_GET8 (external->p_vaddr);
3036 internal->p_paddr = BYTE_GET8 (external->p_paddr);
3037 internal->p_filesz = BYTE_GET8 (external->p_filesz);
3038 internal->p_memsz = BYTE_GET8 (external->p_memsz);
3039 internal->p_align = BYTE_GET8 (external->p_align);
3042 free (phdrs);
3044 return 1;
3047 /* Returns 1 if the program headers were loaded. */
3049 static int
3050 process_program_headers (file)
3051 FILE *file;
3053 Elf_Internal_Phdr *program_headers;
3054 Elf_Internal_Phdr *segment;
3055 unsigned int i;
3057 if (elf_header.e_phnum == 0)
3059 if (do_segments)
3060 printf (_("\nThere are no program headers in this file.\n"));
3061 return 0;
3064 if (do_segments && !do_header)
3066 printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
3067 printf (_("Entry point "));
3068 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
3069 printf (_("\nThere are %d program headers, starting at offset "),
3070 elf_header.e_phnum);
3071 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
3072 printf ("\n");
3075 program_headers = (Elf_Internal_Phdr *) malloc
3076 (elf_header.e_phnum * sizeof (Elf_Internal_Phdr));
3078 if (program_headers == NULL)
3080 error (_("Out of memory\n"));
3081 return 0;
3084 if (is_32bit_elf)
3085 i = get_32bit_program_headers (file, program_headers);
3086 else
3087 i = get_64bit_program_headers (file, program_headers);
3089 if (i == 0)
3091 free (program_headers);
3092 return 0;
3095 if (do_segments)
3097 if (elf_header.e_phnum > 1)
3098 printf (_("\nProgram Headers:\n"));
3099 else
3100 printf (_("\nProgram Headers:\n"));
3102 if (is_32bit_elf)
3103 printf
3104 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
3105 else if (do_wide)
3106 printf
3107 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
3108 else
3110 printf
3111 (_(" Type Offset VirtAddr PhysAddr\n"));
3112 printf
3113 (_(" FileSiz MemSiz Flags Align\n"));
3117 loadaddr = -1;
3118 dynamic_addr = 0;
3119 dynamic_size = 0;
3121 for (i = 0, segment = program_headers;
3122 i < elf_header.e_phnum;
3123 i++, segment++)
3125 if (do_segments)
3127 printf (" %-14.14s ", get_segment_type (segment->p_type));
3129 if (is_32bit_elf)
3131 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3132 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
3133 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
3134 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
3135 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
3136 printf ("%c%c%c ",
3137 (segment->p_flags & PF_R ? 'R' : ' '),
3138 (segment->p_flags & PF_W ? 'W' : ' '),
3139 (segment->p_flags & PF_X ? 'E' : ' '));
3140 printf ("%#lx", (unsigned long) segment->p_align);
3142 else if (do_wide)
3144 if ((unsigned long) segment->p_offset == segment->p_offset)
3145 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
3146 else
3148 print_vma (segment->p_offset, FULL_HEX);
3149 putchar (' ');
3152 print_vma (segment->p_vaddr, FULL_HEX);
3153 putchar (' ');
3154 print_vma (segment->p_paddr, FULL_HEX);
3155 putchar (' ');
3157 if ((unsigned long) segment->p_filesz == segment->p_filesz)
3158 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
3159 else
3161 print_vma (segment->p_filesz, FULL_HEX);
3162 putchar (' ');
3165 if ((unsigned long) segment->p_memsz == segment->p_memsz)
3166 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
3167 else
3169 print_vma (segment->p_offset, FULL_HEX);
3172 printf (" %c%c%c ",
3173 (segment->p_flags & PF_R ? 'R' : ' '),
3174 (segment->p_flags & PF_W ? 'W' : ' '),
3175 (segment->p_flags & PF_X ? 'E' : ' '));
3177 if ((unsigned long) segment->p_align == segment->p_align)
3178 printf ("%#lx", (unsigned long) segment->p_align);
3179 else
3181 print_vma (segment->p_align, PREFIX_HEX);
3184 else
3186 print_vma (segment->p_offset, FULL_HEX);
3187 putchar (' ');
3188 print_vma (segment->p_vaddr, FULL_HEX);
3189 putchar (' ');
3190 print_vma (segment->p_paddr, FULL_HEX);
3191 printf ("\n ");
3192 print_vma (segment->p_filesz, FULL_HEX);
3193 putchar (' ');
3194 print_vma (segment->p_memsz, FULL_HEX);
3195 printf (" %c%c%c ",
3196 (segment->p_flags & PF_R ? 'R' : ' '),
3197 (segment->p_flags & PF_W ? 'W' : ' '),
3198 (segment->p_flags & PF_X ? 'E' : ' '));
3199 print_vma (segment->p_align, HEX);
3203 switch (segment->p_type)
3205 case PT_LOAD:
3206 if (loadaddr == -1)
3208 unsigned long align_mask = -segment->p_align;
3210 if (align_mask == 0)
3211 --align_mask;
3212 loadaddr = ((segment->p_vaddr & align_mask)
3213 - (segment->p_offset & align_mask));
3215 break;
3217 case PT_DYNAMIC:
3218 if (dynamic_addr)
3219 error (_("more than one dynamic segment\n"));
3221 dynamic_addr = segment->p_offset;
3222 dynamic_size = segment->p_filesz;
3223 break;
3225 case PT_INTERP:
3226 if (fseek (file, (long) segment->p_offset, SEEK_SET))
3227 error (_("Unable to find program interpreter name\n"));
3228 else
3230 program_interpreter[0] = 0;
3231 fscanf (file, "%63s", program_interpreter);
3233 if (do_segments)
3234 printf (_("\n [Requesting program interpreter: %s]"),
3235 program_interpreter);
3237 break;
3240 if (do_segments)
3241 putc ('\n', stdout);
3244 if (loadaddr == -1)
3246 /* Very strange. */
3247 loadaddr = 0;
3250 if (do_segments && section_headers != NULL)
3252 printf (_("\n Section to Segment mapping:\n"));
3253 printf (_(" Segment Sections...\n"));
3255 assert (string_table != NULL);
3257 for (i = 0; i < elf_header.e_phnum; i++)
3259 unsigned int j;
3260 Elf_Internal_Shdr *section;
3262 segment = program_headers + i;
3263 section = section_headers;
3265 printf (" %2.2d ", i);
3267 for (j = 1; j < elf_header.e_shnum; j++, section++)
3269 if (section->sh_size > 0
3270 /* Compare allocated sections by VMA, unallocated
3271 sections by file offset. */
3272 && (section->sh_flags & SHF_ALLOC
3273 ? (section->sh_addr >= segment->p_vaddr
3274 && section->sh_addr + section->sh_size
3275 <= segment->p_vaddr + segment->p_memsz)
3276 : ((bfd_vma) section->sh_offset >= segment->p_offset
3277 && (section->sh_offset + section->sh_size
3278 <= segment->p_offset + segment->p_filesz))))
3279 printf ("%s ", SECTION_NAME (section));
3282 putc ('\n',stdout);
3286 free (program_headers);
3288 return 1;
3292 static int
3293 get_32bit_section_headers (file, num)
3294 FILE *file;
3295 unsigned int num;
3297 Elf32_External_Shdr *shdrs;
3298 Elf_Internal_Shdr *internal;
3299 unsigned int i;
3301 shdrs = ((Elf32_External_Shdr *)
3302 get_data (NULL, file, elf_header.e_shoff,
3303 elf_header.e_shentsize * num,
3304 _("section headers")));
3305 if (!shdrs)
3306 return 0;
3308 section_headers = ((Elf_Internal_Shdr *)
3309 malloc (num * sizeof (Elf_Internal_Shdr)));
3311 if (section_headers == NULL)
3313 error (_("Out of memory\n"));
3314 return 0;
3317 for (i = 0, internal = section_headers;
3318 i < num;
3319 i++, internal++)
3321 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3322 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3323 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
3324 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
3325 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3326 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
3327 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3328 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3329 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3330 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
3333 free (shdrs);
3335 return 1;
3338 static int
3339 get_64bit_section_headers (file, num)
3340 FILE *file;
3341 unsigned int num;
3343 Elf64_External_Shdr *shdrs;
3344 Elf_Internal_Shdr *internal;
3345 unsigned int i;
3347 shdrs = ((Elf64_External_Shdr *)
3348 get_data (NULL, file, elf_header.e_shoff,
3349 elf_header.e_shentsize * num,
3350 _("section headers")));
3351 if (!shdrs)
3352 return 0;
3354 section_headers = ((Elf_Internal_Shdr *)
3355 malloc (num * sizeof (Elf_Internal_Shdr)));
3357 if (section_headers == NULL)
3359 error (_("Out of memory\n"));
3360 return 0;
3363 for (i = 0, internal = section_headers;
3364 i < num;
3365 i++, internal++)
3367 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3368 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3369 internal->sh_flags = BYTE_GET8 (shdrs[i].sh_flags);
3370 internal->sh_addr = BYTE_GET8 (shdrs[i].sh_addr);
3371 internal->sh_size = BYTE_GET8 (shdrs[i].sh_size);
3372 internal->sh_entsize = BYTE_GET8 (shdrs[i].sh_entsize);
3373 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3374 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3375 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3376 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3379 free (shdrs);
3381 return 1;
3384 static Elf_Internal_Sym *
3385 get_32bit_elf_symbols (file, section)
3386 FILE *file;
3387 Elf_Internal_Shdr *section;
3389 unsigned long number;
3390 Elf32_External_Sym *esyms;
3391 Elf_External_Sym_Shndx *shndx;
3392 Elf_Internal_Sym *isyms;
3393 Elf_Internal_Sym *psym;
3394 unsigned int j;
3396 esyms = ((Elf32_External_Sym *)
3397 get_data (NULL, file, section->sh_offset,
3398 section->sh_size, _("symbols")));
3399 if (!esyms)
3400 return NULL;
3402 shndx = NULL;
3403 if (symtab_shndx_hdr != NULL
3404 && (symtab_shndx_hdr->sh_link
3405 == (unsigned long) SECTION_HEADER_NUM (section - section_headers)))
3407 shndx = ((Elf_External_Sym_Shndx *)
3408 get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3409 symtab_shndx_hdr->sh_size, _("symtab shndx")));
3410 if (!shndx)
3412 free (esyms);
3413 return NULL;
3417 number = section->sh_size / section->sh_entsize;
3418 isyms = (Elf_Internal_Sym *) malloc (number * sizeof (Elf_Internal_Sym));
3420 if (isyms == NULL)
3422 error (_("Out of memory\n"));
3423 if (shndx)
3424 free (shndx);
3425 free (esyms);
3426 return NULL;
3429 for (j = 0, psym = isyms;
3430 j < number;
3431 j++, psym++)
3433 psym->st_name = BYTE_GET (esyms[j].st_name);
3434 psym->st_value = BYTE_GET (esyms[j].st_value);
3435 psym->st_size = BYTE_GET (esyms[j].st_size);
3436 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3437 if (psym->st_shndx == SHN_XINDEX && shndx != NULL)
3438 psym->st_shndx
3439 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3440 psym->st_info = BYTE_GET (esyms[j].st_info);
3441 psym->st_other = BYTE_GET (esyms[j].st_other);
3444 if (shndx)
3445 free (shndx);
3446 free (esyms);
3448 return isyms;
3451 static Elf_Internal_Sym *
3452 get_64bit_elf_symbols (file, section)
3453 FILE *file;
3454 Elf_Internal_Shdr *section;
3456 unsigned long number;
3457 Elf64_External_Sym *esyms;
3458 Elf_External_Sym_Shndx *shndx;
3459 Elf_Internal_Sym *isyms;
3460 Elf_Internal_Sym *psym;
3461 unsigned int j;
3463 esyms = ((Elf64_External_Sym *)
3464 get_data (NULL, file, section->sh_offset,
3465 section->sh_size, _("symbols")));
3466 if (!esyms)
3467 return NULL;
3469 shndx = NULL;
3470 if (symtab_shndx_hdr != NULL
3471 && (symtab_shndx_hdr->sh_link
3472 == (unsigned long) SECTION_HEADER_NUM (section - section_headers)))
3474 shndx = ((Elf_External_Sym_Shndx *)
3475 get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3476 symtab_shndx_hdr->sh_size, _("symtab shndx")));
3477 if (!shndx)
3479 free (esyms);
3480 return NULL;
3484 number = section->sh_size / section->sh_entsize;
3485 isyms = (Elf_Internal_Sym *) malloc (number * sizeof (Elf_Internal_Sym));
3487 if (isyms == NULL)
3489 error (_("Out of memory\n"));
3490 if (shndx)
3491 free (shndx);
3492 free (esyms);
3493 return NULL;
3496 for (j = 0, psym = isyms;
3497 j < number;
3498 j++, psym++)
3500 psym->st_name = BYTE_GET (esyms[j].st_name);
3501 psym->st_info = BYTE_GET (esyms[j].st_info);
3502 psym->st_other = BYTE_GET (esyms[j].st_other);
3503 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3504 if (psym->st_shndx == SHN_XINDEX && shndx != NULL)
3505 psym->st_shndx
3506 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3507 psym->st_value = BYTE_GET8 (esyms[j].st_value);
3508 psym->st_size = BYTE_GET8 (esyms[j].st_size);
3511 if (shndx)
3512 free (shndx);
3513 free (esyms);
3515 return isyms;
3518 static const char *
3519 get_elf_section_flags (sh_flags)
3520 bfd_vma sh_flags;
3522 static char buff[32];
3524 *buff = 0;
3526 while (sh_flags)
3528 bfd_vma flag;
3530 flag = sh_flags & - sh_flags;
3531 sh_flags &= ~ flag;
3533 switch (flag)
3535 case SHF_WRITE: strcat (buff, "W"); break;
3536 case SHF_ALLOC: strcat (buff, "A"); break;
3537 case SHF_EXECINSTR: strcat (buff, "X"); break;
3538 case SHF_MERGE: strcat (buff, "M"); break;
3539 case SHF_STRINGS: strcat (buff, "S"); break;
3540 case SHF_INFO_LINK: strcat (buff, "I"); break;
3541 case SHF_LINK_ORDER: strcat (buff, "L"); break;
3542 case SHF_OS_NONCONFORMING: strcat (buff, "O"); break;
3543 case SHF_GROUP: strcat (buff, "G"); break;
3544 case SHF_TLS: strcat (buff, "T"); break;
3546 default:
3547 if (flag & SHF_MASKOS)
3549 strcat (buff, "o");
3550 sh_flags &= ~ SHF_MASKOS;
3552 else if (flag & SHF_MASKPROC)
3554 strcat (buff, "p");
3555 sh_flags &= ~ SHF_MASKPROC;
3557 else
3558 strcat (buff, "x");
3559 break;
3563 return buff;
3566 static int
3567 process_section_headers (file)
3568 FILE *file;
3570 Elf_Internal_Shdr *section;
3571 unsigned int i;
3573 section_headers = NULL;
3575 if (elf_header.e_shnum == 0)
3577 if (do_sections)
3578 printf (_("\nThere are no sections in this file.\n"));
3580 return 1;
3583 if (do_sections && !do_header)
3584 printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
3585 elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
3587 if (is_32bit_elf)
3589 if (! get_32bit_section_headers (file, elf_header.e_shnum))
3590 return 0;
3592 else if (! get_64bit_section_headers (file, elf_header.e_shnum))
3593 return 0;
3595 /* Read in the string table, so that we have names to display. */
3596 section = SECTION_HEADER (elf_header.e_shstrndx);
3598 if (section->sh_size != 0)
3600 string_table = (char *) get_data (NULL, file, section->sh_offset,
3601 section->sh_size, _("string table"));
3603 string_table_length = section->sh_size;
3606 /* Scan the sections for the dynamic symbol table
3607 and dynamic string table and debug sections. */
3608 dynamic_symbols = NULL;
3609 dynamic_strings = NULL;
3610 dynamic_syminfo = NULL;
3611 symtab_shndx_hdr = NULL;
3613 for (i = 0, section = section_headers;
3614 i < elf_header.e_shnum;
3615 i++, section++)
3617 char *name = SECTION_NAME (section);
3619 if (section->sh_type == SHT_DYNSYM)
3621 if (dynamic_symbols != NULL)
3623 error (_("File contains multiple dynamic symbol tables\n"));
3624 continue;
3627 num_dynamic_syms = section->sh_size / section->sh_entsize;
3628 dynamic_symbols = GET_ELF_SYMBOLS (file, section);
3630 else if (section->sh_type == SHT_STRTAB
3631 && strcmp (name, ".dynstr") == 0)
3633 if (dynamic_strings != NULL)
3635 error (_("File contains multiple dynamic string tables\n"));
3636 continue;
3639 dynamic_strings = (char *) get_data (NULL, file, section->sh_offset,
3640 section->sh_size,
3641 _("dynamic strings"));
3643 else if (section->sh_type == SHT_SYMTAB_SHNDX)
3645 if (symtab_shndx_hdr != NULL)
3647 error (_("File contains multiple symtab shndx tables\n"));
3648 continue;
3650 symtab_shndx_hdr = section;
3652 else if ((do_debugging || do_debug_info || do_debug_abbrevs
3653 || do_debug_lines || do_debug_pubnames || do_debug_aranges
3654 || do_debug_frames || do_debug_macinfo || do_debug_str
3655 || do_debug_loc)
3656 && strncmp (name, ".debug_", 7) == 0)
3658 name += 7;
3660 if (do_debugging
3661 || (do_debug_info && (strcmp (name, "info") == 0))
3662 || (do_debug_abbrevs && (strcmp (name, "abbrev") == 0))
3663 || (do_debug_lines && (strcmp (name, "line") == 0))
3664 || (do_debug_pubnames && (strcmp (name, "pubnames") == 0))
3665 || (do_debug_aranges && (strcmp (name, "aranges") == 0))
3666 || (do_debug_frames && (strcmp (name, "frame") == 0))
3667 || (do_debug_macinfo && (strcmp (name, "macinfo") == 0))
3668 || (do_debug_str && (strcmp (name, "str") == 0))
3669 || (do_debug_loc && (strcmp (name, "loc") == 0))
3671 request_dump (i, DEBUG_DUMP);
3673 /* linkonce section to be combined with .debug_info at link time. */
3674 else if ((do_debugging || do_debug_info)
3675 && strncmp (name, ".gnu.linkonce.wi.", 17) == 0)
3676 request_dump (i, DEBUG_DUMP);
3677 else if (do_debug_frames && strcmp (name, ".eh_frame") == 0)
3678 request_dump (i, DEBUG_DUMP);
3681 if (! do_sections)
3682 return 1;
3684 if (elf_header.e_shnum > 1)
3685 printf (_("\nSection Headers:\n"));
3686 else
3687 printf (_("\nSection Header:\n"));
3689 if (is_32bit_elf)
3690 printf
3691 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
3692 else if (do_wide)
3693 printf
3694 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
3695 else
3697 printf (_(" [Nr] Name Type Address Offset\n"));
3698 printf (_(" Size EntSize Flags Link Info Align\n"));
3701 for (i = 0, section = section_headers;
3702 i < elf_header.e_shnum;
3703 i++, section++)
3705 printf (" [%2u] %-17.17s %-15.15s ",
3706 SECTION_HEADER_NUM (i),
3707 SECTION_NAME (section),
3708 get_section_type_name (section->sh_type));
3710 if (is_32bit_elf)
3712 print_vma (section->sh_addr, LONG_HEX);
3714 printf ( " %6.6lx %6.6lx %2.2lx",
3715 (unsigned long) section->sh_offset,
3716 (unsigned long) section->sh_size,
3717 (unsigned long) section->sh_entsize);
3719 printf (" %3s ", get_elf_section_flags (section->sh_flags));
3721 printf ("%2ld %3lx %2ld\n",
3722 (unsigned long) section->sh_link,
3723 (unsigned long) section->sh_info,
3724 (unsigned long) section->sh_addralign);
3726 else if (do_wide)
3728 print_vma (section->sh_addr, LONG_HEX);
3730 if ((long) section->sh_offset == section->sh_offset)
3731 printf (" %6.6lx", (unsigned long) section->sh_offset);
3732 else
3734 putchar (' ');
3735 print_vma (section->sh_offset, LONG_HEX);
3738 if ((unsigned long) section->sh_size == section->sh_size)
3739 printf (" %6.6lx", (unsigned long) section->sh_size);
3740 else
3742 putchar (' ');
3743 print_vma (section->sh_size, LONG_HEX);
3746 if ((unsigned long) section->sh_entsize == section->sh_entsize)
3747 printf (" %2.2lx", (unsigned long) section->sh_entsize);
3748 else
3750 putchar (' ');
3751 print_vma (section->sh_entsize, LONG_HEX);
3754 printf (" %3s ", get_elf_section_flags (section->sh_flags));
3756 printf ("%2ld %3lx ",
3757 (unsigned long) section->sh_link,
3758 (unsigned long) section->sh_info);
3760 if ((unsigned long) section->sh_addralign == section->sh_addralign)
3761 printf ("%2ld\n", (unsigned long) section->sh_addralign);
3762 else
3764 print_vma (section->sh_addralign, DEC);
3765 putchar ('\n');
3768 else
3770 putchar (' ');
3771 print_vma (section->sh_addr, LONG_HEX);
3772 if ((long) section->sh_offset == section->sh_offset)
3773 printf (" %8.8lx", (unsigned long) section->sh_offset);
3774 else
3776 printf (" ");
3777 print_vma (section->sh_offset, LONG_HEX);
3779 printf ("\n ");
3780 print_vma (section->sh_size, LONG_HEX);
3781 printf (" ");
3782 print_vma (section->sh_entsize, LONG_HEX);
3784 printf (" %3s ", get_elf_section_flags (section->sh_flags));
3786 printf (" %2ld %3lx %ld\n",
3787 (unsigned long) section->sh_link,
3788 (unsigned long) section->sh_info,
3789 (unsigned long) section->sh_addralign);
3793 printf (_("Key to Flags:\n\
3794 W (write), A (alloc), X (execute), M (merge), S (strings)\n\
3795 I (info), L (link order), G (group), x (unknown)\n\
3796 O (extra OS processing required) o (OS specific), p (processor specific)\n"));
3798 return 1;
3801 /* Process the reloc section. */
3802 static int
3803 process_relocs (file)
3804 FILE *file;
3806 unsigned long rel_size;
3807 unsigned long rel_offset;
3810 if (!do_reloc)
3811 return 1;
3813 if (do_using_dynamic)
3815 int is_rela = FALSE;
3817 rel_size = 0;
3818 rel_offset = 0;
3820 if (dynamic_info[DT_REL])
3822 rel_offset = dynamic_info[DT_REL];
3823 rel_size = dynamic_info[DT_RELSZ];
3824 is_rela = FALSE;
3826 else if (dynamic_info[DT_RELA])
3828 rel_offset = dynamic_info[DT_RELA];
3829 rel_size = dynamic_info[DT_RELASZ];
3830 is_rela = TRUE;
3832 else if (dynamic_info[DT_JMPREL])
3834 rel_offset = dynamic_info[DT_JMPREL];
3835 rel_size = dynamic_info[DT_PLTRELSZ];
3837 switch (dynamic_info[DT_PLTREL])
3839 case DT_REL:
3840 is_rela = FALSE;
3841 break;
3842 case DT_RELA:
3843 is_rela = TRUE;
3844 break;
3845 default:
3846 is_rela = UNKNOWN;
3847 break;
3851 if (rel_size)
3853 printf
3854 (_("\nRelocation section at offset 0x%lx contains %ld bytes:\n"),
3855 rel_offset, rel_size);
3857 dump_relocations (file, rel_offset - loadaddr, rel_size,
3858 dynamic_symbols, num_dynamic_syms, dynamic_strings,
3859 is_rela);
3861 else
3862 printf (_("\nThere are no dynamic relocations in this file.\n"));
3864 else
3866 Elf_Internal_Shdr *section;
3867 unsigned long i;
3868 int found = 0;
3870 for (i = 0, section = section_headers;
3871 i < elf_header.e_shnum;
3872 i++, section++)
3874 if ( section->sh_type != SHT_RELA
3875 && section->sh_type != SHT_REL)
3876 continue;
3878 rel_offset = section->sh_offset;
3879 rel_size = section->sh_size;
3881 if (rel_size)
3883 Elf_Internal_Shdr *strsec;
3884 Elf_Internal_Sym *symtab;
3885 char *strtab;
3886 int is_rela;
3887 unsigned long nsyms;
3889 printf (_("\nRelocation section "));
3891 if (string_table == NULL)
3892 printf ("%d", section->sh_name);
3893 else
3894 printf (_("'%s'"), SECTION_NAME (section));
3896 printf (_(" at offset 0x%lx contains %lu entries:\n"),
3897 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
3899 symtab = NULL;
3900 strtab = NULL;
3901 nsyms = 0;
3902 if (section->sh_link)
3904 Elf_Internal_Shdr *symsec;
3906 symsec = SECTION_HEADER (section->sh_link);
3907 nsyms = symsec->sh_size / symsec->sh_entsize;
3908 symtab = GET_ELF_SYMBOLS (file, symsec);
3910 if (symtab == NULL)
3911 continue;
3913 strsec = SECTION_HEADER (symsec->sh_link);
3915 strtab = (char *) get_data (NULL, file, strsec->sh_offset,
3916 strsec->sh_size,
3917 _("string table"));
3919 is_rela = section->sh_type == SHT_RELA;
3921 dump_relocations (file, rel_offset, rel_size,
3922 symtab, nsyms, strtab, is_rela);
3924 if (strtab)
3925 free (strtab);
3926 if (symtab)
3927 free (symtab);
3929 found = 1;
3933 if (! found)
3934 printf (_("\nThere are no relocations in this file.\n"));
3937 return 1;
3940 #include "unwind-ia64.h"
3942 /* An absolute address consists of a section and an offset. If the
3943 section is NULL, the offset itself is the address, otherwise, the
3944 address equals to LOAD_ADDRESS(section) + offset. */
3946 struct absaddr
3948 unsigned short section;
3949 bfd_vma offset;
3952 struct unw_aux_info
3954 struct unw_table_entry
3956 struct absaddr start;
3957 struct absaddr end;
3958 struct absaddr info;
3960 *table; /* Unwind table. */
3961 unsigned long table_len; /* Length of unwind table. */
3962 unsigned char *info; /* Unwind info. */
3963 unsigned long info_size; /* Size of unwind info. */
3964 bfd_vma info_addr; /* starting address of unwind info. */
3965 bfd_vma seg_base; /* Starting address of segment. */
3966 Elf_Internal_Sym *symtab; /* The symbol table. */
3967 unsigned long nsyms; /* Number of symbols. */
3968 char *strtab; /* The string table. */
3969 unsigned long strtab_size; /* Size of string table. */
3972 static void find_symbol_for_address
3973 PARAMS ((struct unw_aux_info *, struct absaddr, const char **, bfd_vma *));
3974 static void dump_ia64_unwind
3975 PARAMS ((struct unw_aux_info *));
3976 static int slurp_ia64_unwind_table
3977 PARAMS ((FILE *, struct unw_aux_info *, Elf_Internal_Shdr *));
3979 static void
3980 find_symbol_for_address (aux, addr, symname, offset)
3981 struct unw_aux_info *aux;
3982 struct absaddr addr;
3983 const char **symname;
3984 bfd_vma *offset;
3986 bfd_vma dist = (bfd_vma) 0x100000;
3987 Elf_Internal_Sym *sym, *best = NULL;
3988 unsigned long i;
3990 for (i = 0, sym = aux->symtab; i < aux->nsyms; ++i, ++sym)
3992 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
3993 && sym->st_name != 0
3994 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
3995 && addr.offset >= sym->st_value
3996 && addr.offset - sym->st_value < dist)
3998 best = sym;
3999 dist = addr.offset - sym->st_value;
4000 if (!dist)
4001 break;
4004 if (best)
4006 *symname = (best->st_name >= aux->strtab_size
4007 ? "<corrupt>" : aux->strtab + best->st_name);
4008 *offset = dist;
4009 return;
4011 *symname = NULL;
4012 *offset = addr.offset;
4015 static void
4016 dump_ia64_unwind (aux)
4017 struct unw_aux_info *aux;
4019 bfd_vma addr_size;
4020 struct unw_table_entry *tp;
4021 int in_body;
4023 addr_size = is_32bit_elf ? 4 : 8;
4025 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
4027 bfd_vma stamp;
4028 bfd_vma offset;
4029 const unsigned char *dp;
4030 const unsigned char *head;
4031 const char *procname;
4033 find_symbol_for_address (aux, tp->start, &procname, &offset);
4035 fputs ("\n<", stdout);
4037 if (procname)
4039 fputs (procname, stdout);
4041 if (offset)
4042 printf ("+%lx", (unsigned long) offset);
4045 fputs (">: [", stdout);
4046 print_vma (tp->start.offset, PREFIX_HEX);
4047 fputc ('-', stdout);
4048 print_vma (tp->end.offset, PREFIX_HEX);
4049 printf ("], info at +0x%lx\n",
4050 (unsigned long) (tp->info.offset - aux->seg_base));
4052 head = aux->info + (tp->info.offset - aux->info_addr);
4053 stamp = BYTE_GET8 ((unsigned char *) head);
4055 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
4056 (unsigned) UNW_VER (stamp),
4057 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
4058 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
4059 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
4060 (unsigned long) (addr_size * UNW_LENGTH (stamp)));
4062 if (UNW_VER (stamp) != 1)
4064 printf ("\tUnknown version.\n");
4065 continue;
4068 in_body = 0;
4069 for (dp = head + 8; dp < head + 8 + addr_size * UNW_LENGTH (stamp);)
4070 dp = unw_decode (dp, in_body, & in_body);
4074 static int
4075 slurp_ia64_unwind_table (file, aux, sec)
4076 FILE *file;
4077 struct unw_aux_info *aux;
4078 Elf_Internal_Shdr *sec;
4080 unsigned long size, addr_size, nrelas, i;
4081 Elf_Internal_Phdr *prog_hdrs, *seg;
4082 struct unw_table_entry *tep;
4083 Elf_Internal_Shdr *relsec;
4084 Elf_Internal_Rela *rela, *rp;
4085 unsigned char *table, *tp;
4086 Elf_Internal_Sym *sym;
4087 const char *relname;
4088 int result;
4090 addr_size = is_32bit_elf ? 4 : 8;
4092 /* First, find the starting address of the segment that includes
4093 this section: */
4095 if (elf_header.e_phnum)
4097 prog_hdrs = (Elf_Internal_Phdr *)
4098 xmalloc (elf_header.e_phnum * sizeof (Elf_Internal_Phdr));
4100 if (is_32bit_elf)
4101 result = get_32bit_program_headers (file, prog_hdrs);
4102 else
4103 result = get_64bit_program_headers (file, prog_hdrs);
4105 if (!result)
4107 free (prog_hdrs);
4108 return 0;
4111 for (seg = prog_hdrs; seg < prog_hdrs + elf_header.e_phnum; ++seg)
4113 if (seg->p_type != PT_LOAD)
4114 continue;
4116 if (sec->sh_addr >= seg->p_vaddr
4117 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
4119 aux->seg_base = seg->p_vaddr;
4120 break;
4124 free (prog_hdrs);
4127 /* Second, build the unwind table from the contents of the unwind section: */
4128 size = sec->sh_size;
4129 table = (char *) get_data (NULL, file, sec->sh_offset,
4130 size, _("unwind table"));
4131 if (!table)
4132 return 0;
4134 tep = aux->table = xmalloc (size / (3 * addr_size) * sizeof (aux->table[0]));
4135 for (tp = table; tp < table + size; tp += 3 * addr_size, ++tep)
4137 tep->start.section = SHN_UNDEF;
4138 tep->end.section = SHN_UNDEF;
4139 tep->info.section = SHN_UNDEF;
4140 if (is_32bit_elf)
4142 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
4143 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
4144 tep->info.offset = byte_get ((unsigned char *) tp + 8, 4);
4146 else
4148 tep->start.offset = BYTE_GET8 ((unsigned char *) tp + 0);
4149 tep->end.offset = BYTE_GET8 ((unsigned char *) tp + 8);
4150 tep->info.offset = BYTE_GET8 ((unsigned char *) tp + 16);
4152 tep->start.offset += aux->seg_base;
4153 tep->end.offset += aux->seg_base;
4154 tep->info.offset += aux->seg_base;
4156 free (table);
4158 /* Third, apply any relocations to the unwind table: */
4160 for (relsec = section_headers;
4161 relsec < section_headers + elf_header.e_shnum;
4162 ++relsec)
4164 if (relsec->sh_type != SHT_RELA
4165 || SECTION_HEADER (relsec->sh_info) != sec)
4166 continue;
4168 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
4169 & rela, & nrelas))
4170 return 0;
4172 for (rp = rela; rp < rela + nrelas; ++rp)
4174 if (is_32bit_elf)
4176 relname = elf_ia64_reloc_type (ELF32_R_TYPE (rp->r_info));
4177 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
4179 if (ELF32_ST_TYPE (sym->st_info) != STT_SECTION)
4181 warn (_("Skipping unexpected symbol type %u\n"),
4182 ELF32_ST_TYPE (sym->st_info));
4183 continue;
4186 else
4188 relname = elf_ia64_reloc_type (ELF64_R_TYPE (rp->r_info));
4189 sym = aux->symtab + ELF64_R_SYM (rp->r_info);
4191 if (ELF64_ST_TYPE (sym->st_info) != STT_SECTION)
4193 warn (_("Skipping unexpected symbol type %u\n"),
4194 ELF64_ST_TYPE (sym->st_info));
4195 continue;
4199 if (strncmp (relname, "R_IA64_SEGREL", 13) != 0)
4201 warn (_("Skipping unexpected relocation type %s\n"), relname);
4202 continue;
4205 i = rp->r_offset / (3 * addr_size);
4207 switch (rp->r_offset/addr_size % 3)
4209 case 0:
4210 aux->table[i].start.section = sym->st_shndx;
4211 aux->table[i].start.offset += rp->r_addend;
4212 break;
4213 case 1:
4214 aux->table[i].end.section = sym->st_shndx;
4215 aux->table[i].end.offset += rp->r_addend;
4216 break;
4217 case 2:
4218 aux->table[i].info.section = sym->st_shndx;
4219 aux->table[i].info.offset += rp->r_addend;
4220 break;
4221 default:
4222 break;
4226 free (rela);
4229 aux->table_len = size / (3 * addr_size);
4230 return 1;
4233 static int
4234 process_unwind (file)
4235 FILE *file;
4237 Elf_Internal_Shdr *sec, *unwsec = NULL, *strsec;
4238 unsigned long i, addr_size, unwcount = 0, unwstart = 0;
4239 struct unw_aux_info aux;
4241 if (!do_unwind)
4242 return 1;
4244 if (elf_header.e_machine != EM_IA_64)
4246 printf (_("\nThere are no unwind sections in this file.\n"));
4247 return 1;
4250 memset (& aux, 0, sizeof (aux));
4252 addr_size = is_32bit_elf ? 4 : 8;
4254 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
4256 if (sec->sh_type == SHT_SYMTAB)
4258 aux.nsyms = sec->sh_size / sec->sh_entsize;
4259 aux.symtab = GET_ELF_SYMBOLS (file, sec);
4261 strsec = SECTION_HEADER (sec->sh_link);
4262 aux.strtab_size = strsec->sh_size;
4263 aux.strtab = (char *) get_data (NULL, file, strsec->sh_offset,
4264 aux.strtab_size, _("string table"));
4266 else if (sec->sh_type == SHT_IA_64_UNWIND)
4267 unwcount++;
4270 if (!unwcount)
4271 printf (_("\nThere are no unwind sections in this file.\n"));
4273 while (unwcount-- > 0)
4275 char *suffix;
4276 size_t len, len2;
4278 for (i = unwstart, sec = section_headers + unwstart;
4279 i < elf_header.e_shnum; ++i, ++sec)
4280 if (sec->sh_type == SHT_IA_64_UNWIND)
4282 unwsec = sec;
4283 break;
4286 unwstart = i + 1;
4287 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
4289 if (strncmp (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once,
4290 len) == 0)
4292 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO */
4293 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
4294 suffix = SECTION_NAME (unwsec) + len;
4295 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
4296 ++i, ++sec)
4297 if (strncmp (SECTION_NAME (sec),
4298 ELF_STRING_ia64_unwind_info_once, len2) == 0
4299 && strcmp (SECTION_NAME (sec) + len2, suffix) == 0)
4300 break;
4302 else
4304 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
4305 .IA_64.unwind or BAR -> .IA_64.unwind_info */
4306 len = sizeof (ELF_STRING_ia64_unwind) - 1;
4307 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
4308 suffix = "";
4309 if (strncmp (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind,
4310 len) == 0)
4311 suffix = SECTION_NAME (unwsec) + len;
4312 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
4313 ++i, ++sec)
4314 if (strncmp (SECTION_NAME (sec),
4315 ELF_STRING_ia64_unwind_info, len2) == 0
4316 && strcmp (SECTION_NAME (sec) + len2, suffix) == 0)
4317 break;
4320 if (i == elf_header.e_shnum)
4322 printf (_("\nCould not find unwind info section for "));
4324 if (string_table == NULL)
4325 printf ("%d", unwsec->sh_name);
4326 else
4327 printf (_("'%s'"), SECTION_NAME (unwsec));
4329 else
4331 aux.info_size = sec->sh_size;
4332 aux.info_addr = sec->sh_addr;
4333 aux.info = (char *) get_data (NULL, file, sec->sh_offset,
4334 aux.info_size, _("unwind info"));
4336 printf (_("\nUnwind section "));
4338 if (string_table == NULL)
4339 printf ("%d", unwsec->sh_name);
4340 else
4341 printf (_("'%s'"), SECTION_NAME (unwsec));
4343 printf (_(" at offset 0x%lx contains %lu entries:\n"),
4344 (unsigned long) unwsec->sh_offset,
4345 (unsigned long) (unwsec->sh_size / (3 * addr_size)));
4347 (void) slurp_ia64_unwind_table (file, & aux, unwsec);
4349 if (aux.table_len > 0)
4350 dump_ia64_unwind (& aux);
4352 if (aux.table)
4353 free ((char *) aux.table);
4354 if (aux.info)
4355 free ((char *) aux.info);
4356 aux.table = NULL;
4357 aux.info = NULL;
4361 if (aux.symtab)
4362 free (aux.symtab);
4363 if (aux.strtab)
4364 free ((char *) aux.strtab);
4366 return 1;
4369 static void
4370 dynamic_segment_mips_val (entry)
4371 Elf_Internal_Dyn *entry;
4373 switch (entry->d_tag)
4375 case DT_MIPS_FLAGS:
4376 if (entry->d_un.d_val == 0)
4377 printf ("NONE\n");
4378 else
4380 static const char * opts[] =
4382 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
4383 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
4384 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
4385 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
4386 "RLD_ORDER_SAFE"
4388 unsigned int cnt;
4389 int first = 1;
4390 for (cnt = 0; cnt < NUM_ELEM (opts); ++cnt)
4391 if (entry->d_un.d_val & (1 << cnt))
4393 printf ("%s%s", first ? "" : " ", opts[cnt]);
4394 first = 0;
4396 puts ("");
4398 break;
4400 case DT_MIPS_IVERSION:
4401 if (dynamic_strings != NULL)
4402 printf ("Interface Version: %s\n",
4403 dynamic_strings + entry->d_un.d_val);
4404 else
4405 printf ("%ld\n", (long) entry->d_un.d_ptr);
4406 break;
4408 case DT_MIPS_TIME_STAMP:
4410 char timebuf[20];
4411 struct tm *tmp;
4413 time_t time = entry->d_un.d_val;
4414 tmp = gmtime (&time);
4415 sprintf (timebuf, "%04u-%02u-%02uT%02u:%02u:%02u",
4416 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
4417 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
4418 printf ("Time Stamp: %s\n", timebuf);
4420 break;
4422 case DT_MIPS_RLD_VERSION:
4423 case DT_MIPS_LOCAL_GOTNO:
4424 case DT_MIPS_CONFLICTNO:
4425 case DT_MIPS_LIBLISTNO:
4426 case DT_MIPS_SYMTABNO:
4427 case DT_MIPS_UNREFEXTNO:
4428 case DT_MIPS_HIPAGENO:
4429 case DT_MIPS_DELTA_CLASS_NO:
4430 case DT_MIPS_DELTA_INSTANCE_NO:
4431 case DT_MIPS_DELTA_RELOC_NO:
4432 case DT_MIPS_DELTA_SYM_NO:
4433 case DT_MIPS_DELTA_CLASSSYM_NO:
4434 case DT_MIPS_COMPACT_SIZE:
4435 printf ("%ld\n", (long) entry->d_un.d_ptr);
4436 break;
4438 default:
4439 printf ("%#lx\n", (long) entry->d_un.d_ptr);
4444 static void
4445 dynamic_segment_parisc_val (entry)
4446 Elf_Internal_Dyn *entry;
4448 switch (entry->d_tag)
4450 case DT_HP_DLD_FLAGS:
4452 static struct
4454 long int bit;
4455 const char *str;
4457 flags[] =
4459 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
4460 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
4461 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
4462 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
4463 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
4464 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
4465 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
4466 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
4467 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
4468 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
4469 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" }
4471 int first = 1;
4472 size_t cnt;
4473 bfd_vma val = entry->d_un.d_val;
4475 for (cnt = 0; cnt < sizeof (flags) / sizeof (flags[0]); ++cnt)
4476 if (val & flags[cnt].bit)
4478 if (! first)
4479 putchar (' ');
4480 fputs (flags[cnt].str, stdout);
4481 first = 0;
4482 val ^= flags[cnt].bit;
4485 if (val != 0 || first)
4487 if (! first)
4488 putchar (' ');
4489 print_vma (val, HEX);
4492 break;
4494 default:
4495 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
4496 break;
4498 putchar ('\n');
4501 static void
4502 dynamic_segment_ia64_val (entry)
4503 Elf_Internal_Dyn *entry;
4505 switch (entry->d_tag)
4507 case DT_IA_64_PLT_RESERVE:
4508 /* First 3 bytes reserved. */
4509 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
4510 printf (" -- ");
4511 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
4512 printf ("\n");
4516 static int
4517 get_32bit_dynamic_segment (file)
4518 FILE *file;
4520 Elf32_External_Dyn *edyn;
4521 Elf_Internal_Dyn *entry;
4522 bfd_size_type i;
4524 edyn = (Elf32_External_Dyn *) get_data (NULL, file, dynamic_addr,
4525 dynamic_size, _("dynamic segment"));
4526 if (!edyn)
4527 return 0;
4529 /* SGI's ELF has more than one section in the DYNAMIC segment. Determine
4530 how large this .dynamic is now. We can do this even before the byte
4531 swapping since the DT_NULL tag is recognizable. */
4532 dynamic_size = 0;
4533 while (*(Elf32_Word *) edyn[dynamic_size++].d_tag != DT_NULL)
4536 dynamic_segment = (Elf_Internal_Dyn *)
4537 malloc (dynamic_size * sizeof (Elf_Internal_Dyn));
4539 if (dynamic_segment == NULL)
4541 error (_("Out of memory\n"));
4542 free (edyn);
4543 return 0;
4546 for (i = 0, entry = dynamic_segment;
4547 i < dynamic_size;
4548 i++, entry++)
4550 entry->d_tag = BYTE_GET (edyn[i].d_tag);
4551 entry->d_un.d_val = BYTE_GET (edyn[i].d_un.d_val);
4554 free (edyn);
4556 return 1;
4559 static int
4560 get_64bit_dynamic_segment (file)
4561 FILE *file;
4563 Elf64_External_Dyn *edyn;
4564 Elf_Internal_Dyn *entry;
4565 bfd_size_type i;
4567 edyn = (Elf64_External_Dyn *) get_data (NULL, file, dynamic_addr,
4568 dynamic_size, _("dynamic segment"));
4569 if (!edyn)
4570 return 0;
4572 /* SGI's ELF has more than one section in the DYNAMIC segment. Determine
4573 how large this .dynamic is now. We can do this even before the byte
4574 swapping since the DT_NULL tag is recognizable. */
4575 dynamic_size = 0;
4576 while (*(bfd_vma *) edyn[dynamic_size++].d_tag != DT_NULL)
4579 dynamic_segment = (Elf_Internal_Dyn *)
4580 malloc (dynamic_size * sizeof (Elf_Internal_Dyn));
4582 if (dynamic_segment == NULL)
4584 error (_("Out of memory\n"));
4585 free (edyn);
4586 return 0;
4589 for (i = 0, entry = dynamic_segment;
4590 i < dynamic_size;
4591 i++, entry++)
4593 entry->d_tag = BYTE_GET8 (edyn[i].d_tag);
4594 entry->d_un.d_val = BYTE_GET8 (edyn[i].d_un.d_val);
4597 free (edyn);
4599 return 1;
4602 static const char *
4603 get_dynamic_flags (flags)
4604 bfd_vma flags;
4606 static char buff[128];
4607 char *p = buff;
4609 *p = '\0';
4610 while (flags)
4612 bfd_vma flag;
4614 flag = flags & - flags;
4615 flags &= ~ flag;
4617 if (p != buff)
4618 *p++ = ' ';
4620 switch (flag)
4622 case DF_ORIGIN: strcpy (p, "ORIGIN"); break;
4623 case DF_SYMBOLIC: strcpy (p, "SYMBOLIC"); break;
4624 case DF_TEXTREL: strcpy (p, "TEXTREL"); break;
4625 case DF_BIND_NOW: strcpy (p, "BIND_NOW"); break;
4626 case DF_STATIC_TLS: strcpy (p, "STATIC_TLS"); break;
4627 default: strcpy (p, "unknown"); break;
4630 p = strchr (p, '\0');
4632 return buff;
4635 /* Parse and display the contents of the dynamic segment. */
4636 static int
4637 process_dynamic_segment (file)
4638 FILE *file;
4640 Elf_Internal_Dyn *entry;
4641 bfd_size_type i;
4643 if (dynamic_size == 0)
4645 if (do_dynamic)
4646 printf (_("\nThere is no dynamic segment in this file.\n"));
4648 return 1;
4651 if (is_32bit_elf)
4653 if (! get_32bit_dynamic_segment (file))
4654 return 0;
4656 else if (! get_64bit_dynamic_segment (file))
4657 return 0;
4659 /* Find the appropriate symbol table. */
4660 if (dynamic_symbols == NULL)
4662 for (i = 0, entry = dynamic_segment;
4663 i < dynamic_size;
4664 ++i, ++entry)
4666 Elf_Internal_Shdr section;
4668 if (entry->d_tag != DT_SYMTAB)
4669 continue;
4671 dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
4673 /* Since we do not know how big the symbol table is,
4674 we default to reading in the entire file (!) and
4675 processing that. This is overkill, I know, but it
4676 should work. */
4677 section.sh_offset = entry->d_un.d_val - loadaddr;
4679 if (fseek (file, 0, SEEK_END))
4680 error (_("Unable to seek to end of file!"));
4682 section.sh_size = ftell (file) - section.sh_offset;
4683 if (is_32bit_elf)
4684 section.sh_entsize = sizeof (Elf32_External_Sym);
4685 else
4686 section.sh_entsize = sizeof (Elf64_External_Sym);
4688 num_dynamic_syms = section.sh_size / section.sh_entsize;
4689 if (num_dynamic_syms < 1)
4691 error (_("Unable to determine the number of symbols to load\n"));
4692 continue;
4695 dynamic_symbols = GET_ELF_SYMBOLS (file, &section);
4699 /* Similarly find a string table. */
4700 if (dynamic_strings == NULL)
4702 for (i = 0, entry = dynamic_segment;
4703 i < dynamic_size;
4704 ++i, ++entry)
4706 unsigned long offset;
4707 long str_tab_len;
4709 if (entry->d_tag != DT_STRTAB)
4710 continue;
4712 dynamic_info[DT_STRTAB] = entry->d_un.d_val;
4714 /* Since we do not know how big the string table is,
4715 we default to reading in the entire file (!) and
4716 processing that. This is overkill, I know, but it
4717 should work. */
4719 offset = entry->d_un.d_val - loadaddr;
4720 if (fseek (file, 0, SEEK_END))
4721 error (_("Unable to seek to end of file\n"));
4722 str_tab_len = ftell (file) - offset;
4724 if (str_tab_len < 1)
4726 error
4727 (_("Unable to determine the length of the dynamic string table\n"));
4728 continue;
4731 dynamic_strings = (char *) get_data (NULL, file, offset, str_tab_len,
4732 _("dynamic string table"));
4733 break;
4737 /* And find the syminfo section if available. */
4738 if (dynamic_syminfo == NULL)
4740 unsigned long syminsz = 0;
4742 for (i = 0, entry = dynamic_segment;
4743 i < dynamic_size;
4744 ++i, ++entry)
4746 if (entry->d_tag == DT_SYMINENT)
4748 /* Note: these braces are necessary to avoid a syntax
4749 error from the SunOS4 C compiler. */
4750 assert (sizeof (Elf_External_Syminfo) == entry->d_un.d_val);
4752 else if (entry->d_tag == DT_SYMINSZ)
4753 syminsz = entry->d_un.d_val;
4754 else if (entry->d_tag == DT_SYMINFO)
4755 dynamic_syminfo_offset = entry->d_un.d_val - loadaddr;
4758 if (dynamic_syminfo_offset != 0 && syminsz != 0)
4760 Elf_External_Syminfo *extsyminfo;
4761 Elf_Internal_Syminfo *syminfo;
4763 /* There is a syminfo section. Read the data. */
4764 extsyminfo = ((Elf_External_Syminfo *)
4765 get_data (NULL, file, dynamic_syminfo_offset,
4766 syminsz, _("symbol information")));
4767 if (!extsyminfo)
4768 return 0;
4770 dynamic_syminfo = (Elf_Internal_Syminfo *) malloc (syminsz);
4771 if (dynamic_syminfo == NULL)
4773 error (_("Out of memory\n"));
4774 return 0;
4777 dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
4778 for (i = 0, syminfo = dynamic_syminfo; i < dynamic_syminfo_nent;
4779 ++i, ++syminfo)
4781 syminfo->si_boundto = BYTE_GET (extsyminfo[i].si_boundto);
4782 syminfo->si_flags = BYTE_GET (extsyminfo[i].si_flags);
4785 free (extsyminfo);
4789 if (do_dynamic && dynamic_addr)
4790 printf (_("\nDynamic segment at offset 0x%lx contains %ld entries:\n"),
4791 dynamic_addr, (long) dynamic_size);
4792 if (do_dynamic)
4793 printf (_(" Tag Type Name/Value\n"));
4795 for (i = 0, entry = dynamic_segment;
4796 i < dynamic_size;
4797 i++, entry++)
4799 if (do_dynamic)
4801 const char *dtype;
4803 putchar (' ');
4804 print_vma (entry->d_tag, FULL_HEX);
4805 dtype = get_dynamic_type (entry->d_tag);
4806 printf (" (%s)%*s", dtype,
4807 ((is_32bit_elf ? 27 : 19)
4808 - (int) strlen (dtype)),
4809 " ");
4812 switch (entry->d_tag)
4814 case DT_FLAGS:
4815 if (do_dynamic)
4816 puts (get_dynamic_flags (entry->d_un.d_val));
4817 break;
4819 case DT_AUXILIARY:
4820 case DT_FILTER:
4821 case DT_CONFIG:
4822 case DT_DEPAUDIT:
4823 case DT_AUDIT:
4824 if (do_dynamic)
4826 switch (entry->d_tag)
4828 case DT_AUXILIARY:
4829 printf (_("Auxiliary library"));
4830 break;
4832 case DT_FILTER:
4833 printf (_("Filter library"));
4834 break;
4836 case DT_CONFIG:
4837 printf (_("Configuration file"));
4838 break;
4840 case DT_DEPAUDIT:
4841 printf (_("Dependency audit library"));
4842 break;
4844 case DT_AUDIT:
4845 printf (_("Audit library"));
4846 break;
4849 if (dynamic_strings)
4850 printf (": [%s]\n", dynamic_strings + entry->d_un.d_val);
4851 else
4853 printf (": ");
4854 print_vma (entry->d_un.d_val, PREFIX_HEX);
4855 putchar ('\n');
4858 break;
4860 case DT_FEATURE:
4861 if (do_dynamic)
4863 printf (_("Flags:"));
4865 if (entry->d_un.d_val == 0)
4866 printf (_(" None\n"));
4867 else
4869 unsigned long int val = entry->d_un.d_val;
4871 if (val & DTF_1_PARINIT)
4873 printf (" PARINIT");
4874 val ^= DTF_1_PARINIT;
4876 if (val & DTF_1_CONFEXP)
4878 printf (" CONFEXP");
4879 val ^= DTF_1_CONFEXP;
4881 if (val != 0)
4882 printf (" %lx", val);
4883 puts ("");
4886 break;
4888 case DT_POSFLAG_1:
4889 if (do_dynamic)
4891 printf (_("Flags:"));
4893 if (entry->d_un.d_val == 0)
4894 printf (_(" None\n"));
4895 else
4897 unsigned long int val = entry->d_un.d_val;
4899 if (val & DF_P1_LAZYLOAD)
4901 printf (" LAZYLOAD");
4902 val ^= DF_P1_LAZYLOAD;
4904 if (val & DF_P1_GROUPPERM)
4906 printf (" GROUPPERM");
4907 val ^= DF_P1_GROUPPERM;
4909 if (val != 0)
4910 printf (" %lx", val);
4911 puts ("");
4914 break;
4916 case DT_FLAGS_1:
4917 if (do_dynamic)
4919 printf (_("Flags:"));
4920 if (entry->d_un.d_val == 0)
4921 printf (_(" None\n"));
4922 else
4924 unsigned long int val = entry->d_un.d_val;
4926 if (val & DF_1_NOW)
4928 printf (" NOW");
4929 val ^= DF_1_NOW;
4931 if (val & DF_1_GLOBAL)
4933 printf (" GLOBAL");
4934 val ^= DF_1_GLOBAL;
4936 if (val & DF_1_GROUP)
4938 printf (" GROUP");
4939 val ^= DF_1_GROUP;
4941 if (val & DF_1_NODELETE)
4943 printf (" NODELETE");
4944 val ^= DF_1_NODELETE;
4946 if (val & DF_1_LOADFLTR)
4948 printf (" LOADFLTR");
4949 val ^= DF_1_LOADFLTR;
4951 if (val & DF_1_INITFIRST)
4953 printf (" INITFIRST");
4954 val ^= DF_1_INITFIRST;
4956 if (val & DF_1_NOOPEN)
4958 printf (" NOOPEN");
4959 val ^= DF_1_NOOPEN;
4961 if (val & DF_1_ORIGIN)
4963 printf (" ORIGIN");
4964 val ^= DF_1_ORIGIN;
4966 if (val & DF_1_DIRECT)
4968 printf (" DIRECT");
4969 val ^= DF_1_DIRECT;
4971 if (val & DF_1_TRANS)
4973 printf (" TRANS");
4974 val ^= DF_1_TRANS;
4976 if (val & DF_1_INTERPOSE)
4978 printf (" INTERPOSE");
4979 val ^= DF_1_INTERPOSE;
4981 if (val & DF_1_NODEFLIB)
4983 printf (" NODEFLIB");
4984 val ^= DF_1_NODEFLIB;
4986 if (val & DF_1_NODUMP)
4988 printf (" NODUMP");
4989 val ^= DF_1_NODUMP;
4991 if (val & DF_1_CONLFAT)
4993 printf (" CONLFAT");
4994 val ^= DF_1_CONLFAT;
4996 if (val != 0)
4997 printf (" %lx", val);
4998 puts ("");
5001 break;
5003 case DT_PLTREL:
5004 if (do_dynamic)
5005 puts (get_dynamic_type (entry->d_un.d_val));
5006 break;
5008 case DT_NULL :
5009 case DT_NEEDED :
5010 case DT_PLTGOT :
5011 case DT_HASH :
5012 case DT_STRTAB :
5013 case DT_SYMTAB :
5014 case DT_RELA :
5015 case DT_INIT :
5016 case DT_FINI :
5017 case DT_SONAME :
5018 case DT_RPATH :
5019 case DT_SYMBOLIC:
5020 case DT_REL :
5021 case DT_DEBUG :
5022 case DT_TEXTREL :
5023 case DT_JMPREL :
5024 case DT_RUNPATH :
5025 dynamic_info[entry->d_tag] = entry->d_un.d_val;
5027 if (do_dynamic)
5029 char *name;
5031 if (dynamic_strings == NULL)
5032 name = NULL;
5033 else
5034 name = dynamic_strings + entry->d_un.d_val;
5036 if (name)
5038 switch (entry->d_tag)
5040 case DT_NEEDED:
5041 printf (_("Shared library: [%s]"), name);
5043 if (strcmp (name, program_interpreter) == 0)
5044 printf (_(" program interpreter"));
5045 break;
5047 case DT_SONAME:
5048 printf (_("Library soname: [%s]"), name);
5049 break;
5051 case DT_RPATH:
5052 printf (_("Library rpath: [%s]"), name);
5053 break;
5055 case DT_RUNPATH:
5056 printf (_("Library runpath: [%s]"), name);
5057 break;
5059 default:
5060 print_vma (entry->d_un.d_val, PREFIX_HEX);
5061 break;
5064 else
5065 print_vma (entry->d_un.d_val, PREFIX_HEX);
5067 putchar ('\n');
5069 break;
5071 case DT_PLTRELSZ:
5072 case DT_RELASZ :
5073 case DT_STRSZ :
5074 case DT_RELSZ :
5075 case DT_RELAENT :
5076 case DT_SYMENT :
5077 case DT_RELENT :
5078 case DT_PLTPADSZ:
5079 case DT_MOVEENT :
5080 case DT_MOVESZ :
5081 case DT_INIT_ARRAYSZ:
5082 case DT_FINI_ARRAYSZ:
5083 case DT_GNU_CONFLICTSZ:
5084 case DT_GNU_LIBLISTSZ:
5085 if (do_dynamic)
5087 print_vma (entry->d_un.d_val, UNSIGNED);
5088 printf (" (bytes)\n");
5090 break;
5092 case DT_VERDEFNUM:
5093 case DT_VERNEEDNUM:
5094 case DT_RELACOUNT:
5095 case DT_RELCOUNT:
5096 if (do_dynamic)
5098 print_vma (entry->d_un.d_val, UNSIGNED);
5099 putchar ('\n');
5101 break;
5103 case DT_SYMINSZ:
5104 case DT_SYMINENT:
5105 case DT_SYMINFO:
5106 case DT_USED:
5107 case DT_INIT_ARRAY:
5108 case DT_FINI_ARRAY:
5109 if (do_dynamic)
5111 if (dynamic_strings != NULL && entry->d_tag == DT_USED)
5113 char *name;
5115 name = dynamic_strings + entry->d_un.d_val;
5117 if (*name)
5119 printf (_("Not needed object: [%s]\n"), name);
5120 break;
5124 print_vma (entry->d_un.d_val, PREFIX_HEX);
5125 putchar ('\n');
5127 break;
5129 case DT_BIND_NOW:
5130 /* The value of this entry is ignored. */
5131 if (do_dynamic)
5132 putchar ('\n');
5133 break;
5135 case DT_GNU_PRELINKED:
5136 if (do_dynamic)
5138 struct tm *tmp;
5139 time_t time = entry->d_un.d_val;
5141 tmp = gmtime (&time);
5142 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
5143 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
5144 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
5147 break;
5149 default:
5150 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
5151 version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
5152 entry->d_un.d_val;
5154 if (do_dynamic)
5156 switch (elf_header.e_machine)
5158 case EM_MIPS:
5159 case EM_MIPS_RS3_LE:
5160 dynamic_segment_mips_val (entry);
5161 break;
5162 case EM_PARISC:
5163 dynamic_segment_parisc_val (entry);
5164 break;
5165 case EM_IA_64:
5166 dynamic_segment_ia64_val (entry);
5167 break;
5168 default:
5169 print_vma (entry->d_un.d_val, PREFIX_HEX);
5170 putchar ('\n');
5173 break;
5177 return 1;
5180 static char *
5181 get_ver_flags (flags)
5182 unsigned int flags;
5184 static char buff[32];
5186 buff[0] = 0;
5188 if (flags == 0)
5189 return _("none");
5191 if (flags & VER_FLG_BASE)
5192 strcat (buff, "BASE ");
5194 if (flags & VER_FLG_WEAK)
5196 if (flags & VER_FLG_BASE)
5197 strcat (buff, "| ");
5199 strcat (buff, "WEAK ");
5202 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK))
5203 strcat (buff, "| <unknown>");
5205 return buff;
5208 /* Display the contents of the version sections. */
5209 static int
5210 process_version_sections (file)
5211 FILE *file;
5213 Elf_Internal_Shdr *section;
5214 unsigned i;
5215 int found = 0;
5217 if (! do_version)
5218 return 1;
5220 for (i = 0, section = section_headers;
5221 i < elf_header.e_shnum;
5222 i++, section++)
5224 switch (section->sh_type)
5226 case SHT_GNU_verdef:
5228 Elf_External_Verdef *edefs;
5229 unsigned int idx;
5230 unsigned int cnt;
5232 found = 1;
5234 printf
5235 (_("\nVersion definition section '%s' contains %ld entries:\n"),
5236 SECTION_NAME (section), section->sh_info);
5238 printf (_(" Addr: 0x"));
5239 printf_vma (section->sh_addr);
5240 printf (_(" Offset: %#08lx Link: %lx (%s)\n"),
5241 (unsigned long) section->sh_offset, section->sh_link,
5242 SECTION_NAME (SECTION_HEADER (section->sh_link)));
5244 edefs = ((Elf_External_Verdef *)
5245 get_data (NULL, file, section->sh_offset,
5246 section->sh_size,
5247 _("version definition section")));
5248 if (!edefs)
5249 break;
5251 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
5253 char *vstart;
5254 Elf_External_Verdef *edef;
5255 Elf_Internal_Verdef ent;
5256 Elf_External_Verdaux *eaux;
5257 Elf_Internal_Verdaux aux;
5258 int j;
5259 int isum;
5261 vstart = ((char *) edefs) + idx;
5263 edef = (Elf_External_Verdef *) vstart;
5265 ent.vd_version = BYTE_GET (edef->vd_version);
5266 ent.vd_flags = BYTE_GET (edef->vd_flags);
5267 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
5268 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
5269 ent.vd_hash = BYTE_GET (edef->vd_hash);
5270 ent.vd_aux = BYTE_GET (edef->vd_aux);
5271 ent.vd_next = BYTE_GET (edef->vd_next);
5273 printf (_(" %#06x: Rev: %d Flags: %s"),
5274 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
5276 printf (_(" Index: %d Cnt: %d "),
5277 ent.vd_ndx, ent.vd_cnt);
5279 vstart += ent.vd_aux;
5281 eaux = (Elf_External_Verdaux *) vstart;
5283 aux.vda_name = BYTE_GET (eaux->vda_name);
5284 aux.vda_next = BYTE_GET (eaux->vda_next);
5286 if (dynamic_strings)
5287 printf (_("Name: %s\n"), dynamic_strings + aux.vda_name);
5288 else
5289 printf (_("Name index: %ld\n"), aux.vda_name);
5291 isum = idx + ent.vd_aux;
5293 for (j = 1; j < ent.vd_cnt; j++)
5295 isum += aux.vda_next;
5296 vstart += aux.vda_next;
5298 eaux = (Elf_External_Verdaux *) vstart;
5300 aux.vda_name = BYTE_GET (eaux->vda_name);
5301 aux.vda_next = BYTE_GET (eaux->vda_next);
5303 if (dynamic_strings)
5304 printf (_(" %#06x: Parent %d: %s\n"),
5305 isum, j, dynamic_strings + aux.vda_name);
5306 else
5307 printf (_(" %#06x: Parent %d, name index: %ld\n"),
5308 isum, j, aux.vda_name);
5311 idx += ent.vd_next;
5314 free (edefs);
5316 break;
5318 case SHT_GNU_verneed:
5320 Elf_External_Verneed *eneed;
5321 unsigned int idx;
5322 unsigned int cnt;
5324 found = 1;
5326 printf (_("\nVersion needs section '%s' contains %ld entries:\n"),
5327 SECTION_NAME (section), section->sh_info);
5329 printf (_(" Addr: 0x"));
5330 printf_vma (section->sh_addr);
5331 printf (_(" Offset: %#08lx Link to section: %ld (%s)\n"),
5332 (unsigned long) section->sh_offset, section->sh_link,
5333 SECTION_NAME (SECTION_HEADER (section->sh_link)));
5335 eneed = ((Elf_External_Verneed *)
5336 get_data (NULL, file, section->sh_offset,
5337 section->sh_size, _("version need section")));
5338 if (!eneed)
5339 break;
5341 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
5343 Elf_External_Verneed *entry;
5344 Elf_Internal_Verneed ent;
5345 int j;
5346 int isum;
5347 char *vstart;
5349 vstart = ((char *) eneed) + idx;
5351 entry = (Elf_External_Verneed *) vstart;
5353 ent.vn_version = BYTE_GET (entry->vn_version);
5354 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
5355 ent.vn_file = BYTE_GET (entry->vn_file);
5356 ent.vn_aux = BYTE_GET (entry->vn_aux);
5357 ent.vn_next = BYTE_GET (entry->vn_next);
5359 printf (_(" %#06x: Version: %d"), idx, ent.vn_version);
5361 if (dynamic_strings)
5362 printf (_(" File: %s"), dynamic_strings + ent.vn_file);
5363 else
5364 printf (_(" File: %lx"), ent.vn_file);
5366 printf (_(" Cnt: %d\n"), ent.vn_cnt);
5368 vstart += ent.vn_aux;
5370 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
5372 Elf_External_Vernaux *eaux;
5373 Elf_Internal_Vernaux aux;
5375 eaux = (Elf_External_Vernaux *) vstart;
5377 aux.vna_hash = BYTE_GET (eaux->vna_hash);
5378 aux.vna_flags = BYTE_GET (eaux->vna_flags);
5379 aux.vna_other = BYTE_GET (eaux->vna_other);
5380 aux.vna_name = BYTE_GET (eaux->vna_name);
5381 aux.vna_next = BYTE_GET (eaux->vna_next);
5383 if (dynamic_strings)
5384 printf (_(" %#06x: Name: %s"),
5385 isum, dynamic_strings + aux.vna_name);
5386 else
5387 printf (_(" %#06x: Name index: %lx"),
5388 isum, aux.vna_name);
5390 printf (_(" Flags: %s Version: %d\n"),
5391 get_ver_flags (aux.vna_flags), aux.vna_other);
5393 isum += aux.vna_next;
5394 vstart += aux.vna_next;
5397 idx += ent.vn_next;
5400 free (eneed);
5402 break;
5404 case SHT_GNU_versym:
5406 Elf_Internal_Shdr *link_section;
5407 int total;
5408 int cnt;
5409 unsigned char *edata;
5410 unsigned short *data;
5411 char *strtab;
5412 Elf_Internal_Sym *symbols;
5413 Elf_Internal_Shdr *string_sec;
5415 link_section = SECTION_HEADER (section->sh_link);
5416 total = section->sh_size / section->sh_entsize;
5418 found = 1;
5420 symbols = GET_ELF_SYMBOLS (file, link_section);
5422 string_sec = SECTION_HEADER (link_section->sh_link);
5424 strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
5425 string_sec->sh_size,
5426 _("version string table"));
5427 if (!strtab)
5428 break;
5430 printf (_("\nVersion symbols section '%s' contains %d entries:\n"),
5431 SECTION_NAME (section), total);
5433 printf (_(" Addr: "));
5434 printf_vma (section->sh_addr);
5435 printf (_(" Offset: %#08lx Link: %lx (%s)\n"),
5436 (unsigned long) section->sh_offset, section->sh_link,
5437 SECTION_NAME (link_section));
5439 edata =
5440 ((unsigned char *)
5441 get_data (NULL, file,
5442 version_info[DT_VERSIONTAGIDX (DT_VERSYM)] - loadaddr,
5443 total * sizeof (short), _("version symbol data")));
5444 if (!edata)
5446 free (strtab);
5447 break;
5450 data = (unsigned short *) malloc (total * sizeof (short));
5452 for (cnt = total; cnt --;)
5453 data[cnt] = byte_get (edata + cnt * sizeof (short),
5454 sizeof (short));
5456 free (edata);
5458 for (cnt = 0; cnt < total; cnt += 4)
5460 int j, nn;
5461 int check_def, check_need;
5462 char *name;
5464 printf (" %03x:", cnt);
5466 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
5467 switch (data[cnt + j])
5469 case 0:
5470 fputs (_(" 0 (*local*) "), stdout);
5471 break;
5473 case 1:
5474 fputs (_(" 1 (*global*) "), stdout);
5475 break;
5477 default:
5478 nn = printf ("%4x%c", data[cnt + j] & 0x7fff,
5479 data[cnt + j] & 0x8000 ? 'h' : ' ');
5481 check_def = 1;
5482 check_need = 1;
5483 if (SECTION_HEADER (symbols[cnt + j].st_shndx)->sh_type
5484 != SHT_NOBITS)
5486 if (symbols[cnt + j].st_shndx == SHN_UNDEF)
5487 check_def = 0;
5488 else
5489 check_need = 0;
5492 if (check_need
5493 && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
5495 Elf_Internal_Verneed ivn;
5496 unsigned long offset;
5498 offset = version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
5499 - loadaddr;
5503 Elf_Internal_Vernaux ivna;
5504 Elf_External_Verneed evn;
5505 Elf_External_Vernaux evna;
5506 unsigned long a_off;
5508 get_data (&evn, file, offset, sizeof (evn),
5509 _("version need"));
5511 ivn.vn_aux = BYTE_GET (evn.vn_aux);
5512 ivn.vn_next = BYTE_GET (evn.vn_next);
5514 a_off = offset + ivn.vn_aux;
5518 get_data (&evna, file, a_off, sizeof (evna),
5519 _("version need aux (2)"));
5521 ivna.vna_next = BYTE_GET (evna.vna_next);
5522 ivna.vna_other = BYTE_GET (evna.vna_other);
5524 a_off += ivna.vna_next;
5526 while (ivna.vna_other != data[cnt + j]
5527 && ivna.vna_next != 0);
5529 if (ivna.vna_other == data[cnt + j])
5531 ivna.vna_name = BYTE_GET (evna.vna_name);
5533 name = strtab + ivna.vna_name;
5534 nn += printf ("(%s%-*s",
5535 name,
5536 12 - (int) strlen (name),
5537 ")");
5538 check_def = 0;
5539 break;
5542 offset += ivn.vn_next;
5544 while (ivn.vn_next);
5547 if (check_def && data[cnt + j] != 0x8001
5548 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
5550 Elf_Internal_Verdef ivd;
5551 Elf_External_Verdef evd;
5552 unsigned long offset;
5554 offset = (version_info[DT_VERSIONTAGIDX (DT_VERDEF)]
5555 - loadaddr);
5559 get_data (&evd, file, offset, sizeof (evd),
5560 _("version def"));
5562 ivd.vd_next = BYTE_GET (evd.vd_next);
5563 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
5565 offset += ivd.vd_next;
5567 while (ivd.vd_ndx != (data[cnt + j] & 0x7fff)
5568 && ivd.vd_next != 0);
5570 if (ivd.vd_ndx == (data[cnt + j] & 0x7fff))
5572 Elf_External_Verdaux evda;
5573 Elf_Internal_Verdaux ivda;
5575 ivd.vd_aux = BYTE_GET (evd.vd_aux);
5577 get_data (&evda, file,
5578 offset - ivd.vd_next + ivd.vd_aux,
5579 sizeof (evda), _("version def aux"));
5581 ivda.vda_name = BYTE_GET (evda.vda_name);
5583 name = strtab + ivda.vda_name;
5584 nn += printf ("(%s%-*s",
5585 name,
5586 12 - (int) strlen (name),
5587 ")");
5591 if (nn < 18)
5592 printf ("%*c", 18 - nn, ' ');
5595 putchar ('\n');
5598 free (data);
5599 free (strtab);
5600 free (symbols);
5602 break;
5604 default:
5605 break;
5609 if (! found)
5610 printf (_("\nNo version information found in this file.\n"));
5612 return 1;
5615 static const char *
5616 get_symbol_binding (binding)
5617 unsigned int binding;
5619 static char buff[32];
5621 switch (binding)
5623 case STB_LOCAL: return "LOCAL";
5624 case STB_GLOBAL: return "GLOBAL";
5625 case STB_WEAK: return "WEAK";
5626 default:
5627 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
5628 sprintf (buff, _("<processor specific>: %d"), binding);
5629 else if (binding >= STB_LOOS && binding <= STB_HIOS)
5630 sprintf (buff, _("<OS specific>: %d"), binding);
5631 else
5632 sprintf (buff, _("<unknown>: %d"), binding);
5633 return buff;
5637 static const char *
5638 get_symbol_type (type)
5639 unsigned int type;
5641 static char buff[32];
5643 switch (type)
5645 case STT_NOTYPE: return "NOTYPE";
5646 case STT_OBJECT: return "OBJECT";
5647 case STT_FUNC: return "FUNC";
5648 case STT_SECTION: return "SECTION";
5649 case STT_FILE: return "FILE";
5650 case STT_COMMON: return "COMMON";
5651 case STT_TLS: return "TLS";
5652 default:
5653 if (type >= STT_LOPROC && type <= STT_HIPROC)
5655 if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
5656 return "THUMB_FUNC";
5658 if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
5659 return "REGISTER";
5661 if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
5662 return "PARISC_MILLI";
5664 sprintf (buff, _("<processor specific>: %d"), type);
5666 else if (type >= STT_LOOS && type <= STT_HIOS)
5668 if (elf_header.e_machine == EM_PARISC)
5670 if (type == STT_HP_OPAQUE)
5671 return "HP_OPAQUE";
5672 if (type == STT_HP_STUB)
5673 return "HP_STUB";
5676 sprintf (buff, _("<OS specific>: %d"), type);
5678 else
5679 sprintf (buff, _("<unknown>: %d"), type);
5680 return buff;
5684 static const char *
5685 get_symbol_visibility (visibility)
5686 unsigned int visibility;
5688 switch (visibility)
5690 case STV_DEFAULT: return "DEFAULT";
5691 case STV_INTERNAL: return "INTERNAL";
5692 case STV_HIDDEN: return "HIDDEN";
5693 case STV_PROTECTED: return "PROTECTED";
5694 default: abort ();
5698 static const char *
5699 get_symbol_index_type (type)
5700 unsigned int type;
5702 static char buff[32];
5704 switch (type)
5706 case SHN_UNDEF: return "UND";
5707 case SHN_ABS: return "ABS";
5708 case SHN_COMMON: return "COM";
5709 default:
5710 if (type >= SHN_LOPROC && type <= SHN_HIPROC)
5711 sprintf (buff, "PRC[0x%04x]", type);
5712 else if (type >= SHN_LOOS && type <= SHN_HIOS)
5713 sprintf (buff, "OS [0x%04x]", type);
5714 else if (type >= SHN_LORESERVE && type <= SHN_HIRESERVE)
5715 sprintf (buff, "RSV[0x%04x]", type);
5716 else
5717 sprintf (buff, "%3d", type);
5718 break;
5721 return buff;
5724 static int *
5725 get_dynamic_data (file, number)
5726 FILE *file;
5727 unsigned int number;
5729 unsigned char *e_data;
5730 int *i_data;
5732 e_data = (unsigned char *) malloc (number * 4);
5734 if (e_data == NULL)
5736 error (_("Out of memory\n"));
5737 return NULL;
5740 if (fread (e_data, 4, number, file) != number)
5742 error (_("Unable to read in dynamic data\n"));
5743 return NULL;
5746 i_data = (int *) malloc (number * sizeof (*i_data));
5748 if (i_data == NULL)
5750 error (_("Out of memory\n"));
5751 free (e_data);
5752 return NULL;
5755 while (number--)
5756 i_data[number] = byte_get (e_data + number * 4, 4);
5758 free (e_data);
5760 return i_data;
5763 /* Dump the symbol table. */
5764 static int
5765 process_symbol_table (file)
5766 FILE *file;
5768 Elf_Internal_Shdr *section;
5769 unsigned char nb[4];
5770 unsigned char nc[4];
5771 int nbuckets = 0;
5772 int nchains = 0;
5773 int *buckets = NULL;
5774 int *chains = NULL;
5776 if (! do_syms && !do_histogram)
5777 return 1;
5779 if (dynamic_info[DT_HASH] && ((do_using_dynamic && dynamic_strings != NULL)
5780 || do_histogram))
5782 if (fseek (file, dynamic_info[DT_HASH] - loadaddr, SEEK_SET))
5784 error (_("Unable to seek to start of dynamic information"));
5785 return 0;
5788 if (fread (nb, sizeof (nb), 1, file) != 1)
5790 error (_("Failed to read in number of buckets\n"));
5791 return 0;
5794 if (fread (nc, sizeof (nc), 1, file) != 1)
5796 error (_("Failed to read in number of chains\n"));
5797 return 0;
5800 nbuckets = byte_get (nb, 4);
5801 nchains = byte_get (nc, 4);
5803 buckets = get_dynamic_data (file, nbuckets);
5804 chains = get_dynamic_data (file, nchains);
5806 if (buckets == NULL || chains == NULL)
5807 return 0;
5810 if (do_syms
5811 && dynamic_info[DT_HASH] && do_using_dynamic && dynamic_strings != NULL)
5813 int hn;
5814 int si;
5816 printf (_("\nSymbol table for image:\n"));
5817 if (is_32bit_elf)
5818 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
5819 else
5820 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
5822 for (hn = 0; hn < nbuckets; hn++)
5824 if (! buckets[hn])
5825 continue;
5827 for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
5829 Elf_Internal_Sym *psym;
5831 psym = dynamic_symbols + si;
5833 printf (" %3d %3d: ", si, hn);
5834 print_vma (psym->st_value, LONG_HEX);
5835 putchar (' ' );
5836 print_vma (psym->st_size, DEC_5);
5838 printf (" %6s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
5839 printf (" %6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
5840 printf (" %3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
5841 printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
5842 print_symbol (25, dynamic_strings + psym->st_name);
5843 putchar ('\n');
5847 else if (do_syms && !do_using_dynamic)
5849 unsigned int i;
5851 for (i = 0, section = section_headers;
5852 i < elf_header.e_shnum;
5853 i++, section++)
5855 unsigned int si;
5856 char *strtab;
5857 Elf_Internal_Sym *symtab;
5858 Elf_Internal_Sym *psym;
5861 if ( section->sh_type != SHT_SYMTAB
5862 && section->sh_type != SHT_DYNSYM)
5863 continue;
5865 printf (_("\nSymbol table '%s' contains %lu entries:\n"),
5866 SECTION_NAME (section),
5867 (unsigned long) (section->sh_size / section->sh_entsize));
5868 if (is_32bit_elf)
5869 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
5870 else
5871 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
5873 symtab = GET_ELF_SYMBOLS (file, section);
5874 if (symtab == NULL)
5875 continue;
5877 if (section->sh_link == elf_header.e_shstrndx)
5878 strtab = string_table;
5879 else
5881 Elf_Internal_Shdr *string_sec;
5883 string_sec = SECTION_HEADER (section->sh_link);
5885 strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
5886 string_sec->sh_size,
5887 _("string table"));
5890 for (si = 0, psym = symtab;
5891 si < section->sh_size / section->sh_entsize;
5892 si++, psym++)
5894 printf ("%6d: ", si);
5895 print_vma (psym->st_value, LONG_HEX);
5896 putchar (' ');
5897 print_vma (psym->st_size, DEC_5);
5898 printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
5899 printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
5900 printf (" %-3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
5901 printf (" %4s ", get_symbol_index_type (psym->st_shndx));
5902 print_symbol (25, strtab + psym->st_name);
5904 if (section->sh_type == SHT_DYNSYM &&
5905 version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
5907 unsigned char data[2];
5908 unsigned short vers_data;
5909 unsigned long offset;
5910 int is_nobits;
5911 int check_def;
5913 offset = version_info[DT_VERSIONTAGIDX (DT_VERSYM)]
5914 - loadaddr;
5916 get_data (&data, file, offset + si * sizeof (vers_data),
5917 sizeof (data), _("version data"));
5919 vers_data = byte_get (data, 2);
5921 is_nobits = (SECTION_HEADER (psym->st_shndx)->sh_type
5922 == SHT_NOBITS);
5924 check_def = (psym->st_shndx != SHN_UNDEF);
5926 if ((vers_data & 0x8000) || vers_data > 1)
5928 if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
5929 && (is_nobits || ! check_def))
5931 Elf_External_Verneed evn;
5932 Elf_Internal_Verneed ivn;
5933 Elf_Internal_Vernaux ivna;
5935 /* We must test both. */
5936 offset = (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
5937 - loadaddr);
5941 unsigned long vna_off;
5943 get_data (&evn, file, offset, sizeof (evn),
5944 _("version need"));
5946 ivn.vn_aux = BYTE_GET (evn.vn_aux);
5947 ivn.vn_next = BYTE_GET (evn.vn_next);
5949 vna_off = offset + ivn.vn_aux;
5953 Elf_External_Vernaux evna;
5955 get_data (&evna, file, vna_off,
5956 sizeof (evna),
5957 _("version need aux (3)"));
5959 ivna.vna_other = BYTE_GET (evna.vna_other);
5960 ivna.vna_next = BYTE_GET (evna.vna_next);
5961 ivna.vna_name = BYTE_GET (evna.vna_name);
5963 vna_off += ivna.vna_next;
5965 while (ivna.vna_other != vers_data
5966 && ivna.vna_next != 0);
5968 if (ivna.vna_other == vers_data)
5969 break;
5971 offset += ivn.vn_next;
5973 while (ivn.vn_next != 0);
5975 if (ivna.vna_other == vers_data)
5977 printf ("@%s (%d)",
5978 strtab + ivna.vna_name, ivna.vna_other);
5979 check_def = 0;
5981 else if (! is_nobits)
5982 error (_("bad dynamic symbol"));
5983 else
5984 check_def = 1;
5987 if (check_def)
5989 if (vers_data != 0x8001
5990 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
5992 Elf_Internal_Verdef ivd;
5993 Elf_Internal_Verdaux ivda;
5994 Elf_External_Verdaux evda;
5995 unsigned long offset;
5997 offset
5998 = (version_info[DT_VERSIONTAGIDX (DT_VERDEF)]
5999 - loadaddr);
6003 Elf_External_Verdef evd;
6005 get_data (&evd, file, offset, sizeof (evd),
6006 _("version def"));
6008 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
6009 ivd.vd_aux = BYTE_GET (evd.vd_aux);
6010 ivd.vd_next = BYTE_GET (evd.vd_next);
6012 offset += ivd.vd_next;
6014 while (ivd.vd_ndx != (vers_data & 0x7fff)
6015 && ivd.vd_next != 0);
6017 offset -= ivd.vd_next;
6018 offset += ivd.vd_aux;
6020 get_data (&evda, file, offset, sizeof (evda),
6021 _("version def aux"));
6023 ivda.vda_name = BYTE_GET (evda.vda_name);
6025 if (psym->st_name != ivda.vda_name)
6026 printf ((vers_data & 0x8000)
6027 ? "@%s" : "@@%s",
6028 strtab + ivda.vda_name);
6034 putchar ('\n');
6037 free (symtab);
6038 if (strtab != string_table)
6039 free (strtab);
6042 else if (do_syms)
6043 printf
6044 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
6046 if (do_histogram && buckets != NULL)
6048 int *lengths;
6049 int *counts;
6050 int hn;
6051 int si;
6052 int maxlength = 0;
6053 int nzero_counts = 0;
6054 int nsyms = 0;
6056 printf (_("\nHistogram for bucket list length (total of %d buckets):\n"),
6057 nbuckets);
6058 printf (_(" Length Number %% of total Coverage\n"));
6060 lengths = (int *) calloc (nbuckets, sizeof (int));
6061 if (lengths == NULL)
6063 error (_("Out of memory"));
6064 return 0;
6066 for (hn = 0; hn < nbuckets; ++hn)
6068 if (! buckets[hn])
6069 continue;
6071 for (si = buckets[hn]; si > 0 && si < nchains; si = chains[si])
6073 ++nsyms;
6074 if (maxlength < ++lengths[hn])
6075 ++maxlength;
6079 counts = (int *) calloc (maxlength + 1, sizeof (int));
6080 if (counts == NULL)
6082 error (_("Out of memory"));
6083 return 0;
6086 for (hn = 0; hn < nbuckets; ++hn)
6087 ++counts[lengths[hn]];
6089 if (nbuckets > 0)
6091 printf (" 0 %-10d (%5.1f%%)\n",
6092 counts[0], (counts[0] * 100.0) / nbuckets);
6093 for (si = 1; si <= maxlength; ++si)
6095 nzero_counts += counts[si] * si;
6096 printf ("%7d %-10d (%5.1f%%) %5.1f%%\n",
6097 si, counts[si], (counts[si] * 100.0) / nbuckets,
6098 (nzero_counts * 100.0) / nsyms);
6102 free (counts);
6103 free (lengths);
6106 if (buckets != NULL)
6108 free (buckets);
6109 free (chains);
6112 return 1;
6115 static int
6116 process_syminfo (file)
6117 FILE *file ATTRIBUTE_UNUSED;
6119 unsigned int i;
6121 if (dynamic_syminfo == NULL
6122 || !do_dynamic)
6123 /* No syminfo, this is ok. */
6124 return 1;
6126 /* There better should be a dynamic symbol section. */
6127 if (dynamic_symbols == NULL || dynamic_strings == NULL)
6128 return 0;
6130 if (dynamic_addr)
6131 printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
6132 dynamic_syminfo_offset, dynamic_syminfo_nent);
6134 printf (_(" Num: Name BoundTo Flags\n"));
6135 for (i = 0; i < dynamic_syminfo_nent; ++i)
6137 unsigned short int flags = dynamic_syminfo[i].si_flags;
6139 printf ("%4d: ", i);
6140 print_symbol (30, dynamic_strings + dynamic_symbols[i].st_name);
6141 putchar (' ');
6143 switch (dynamic_syminfo[i].si_boundto)
6145 case SYMINFO_BT_SELF:
6146 fputs ("SELF ", stdout);
6147 break;
6148 case SYMINFO_BT_PARENT:
6149 fputs ("PARENT ", stdout);
6150 break;
6151 default:
6152 if (dynamic_syminfo[i].si_boundto > 0
6153 && dynamic_syminfo[i].si_boundto < dynamic_size)
6155 print_symbol (10,
6156 dynamic_strings
6157 + (dynamic_segment
6158 [dynamic_syminfo[i].si_boundto].d_un.d_val));
6159 putchar (' ' );
6161 else
6162 printf ("%-10d ", dynamic_syminfo[i].si_boundto);
6163 break;
6166 if (flags & SYMINFO_FLG_DIRECT)
6167 printf (" DIRECT");
6168 if (flags & SYMINFO_FLG_PASSTHRU)
6169 printf (" PASSTHRU");
6170 if (flags & SYMINFO_FLG_COPY)
6171 printf (" COPY");
6172 if (flags & SYMINFO_FLG_LAZYLOAD)
6173 printf (" LAZYLOAD");
6175 puts ("");
6178 return 1;
6181 #ifdef SUPPORT_DISASSEMBLY
6182 static void
6183 disassemble_section (section, file)
6184 Elf_Internal_Shdr *section;
6185 FILE *file;
6187 printf (_("\nAssembly dump of section %s\n"),
6188 SECTION_NAME (section));
6190 /* XXX -- to be done --- XXX */
6192 return 1;
6194 #endif
6196 static int
6197 dump_section (section, file)
6198 Elf_Internal_Shdr *section;
6199 FILE *file;
6201 bfd_size_type bytes;
6202 bfd_vma addr;
6203 unsigned char *data;
6204 unsigned char *start;
6206 bytes = section->sh_size;
6208 if (bytes == 0)
6210 printf (_("\nSection '%s' has no data to dump.\n"),
6211 SECTION_NAME (section));
6212 return 0;
6214 else
6215 printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section));
6217 addr = section->sh_addr;
6219 start = (unsigned char *) get_data (NULL, file, section->sh_offset, bytes,
6220 _("section data"));
6221 if (!start)
6222 return 0;
6224 data = start;
6226 while (bytes)
6228 int j;
6229 int k;
6230 int lbytes;
6232 lbytes = (bytes > 16 ? 16 : bytes);
6234 printf (" 0x%8.8lx ", (unsigned long) addr);
6236 switch (elf_header.e_ident[EI_DATA])
6238 default:
6239 case ELFDATA2LSB:
6240 for (j = 15; j >= 0; j --)
6242 if (j < lbytes)
6243 printf ("%2.2x", data[j]);
6244 else
6245 printf (" ");
6247 if (!(j & 0x3))
6248 printf (" ");
6250 break;
6252 case ELFDATA2MSB:
6253 for (j = 0; j < 16; j++)
6255 if (j < lbytes)
6256 printf ("%2.2x", data[j]);
6257 else
6258 printf (" ");
6260 if ((j & 3) == 3)
6261 printf (" ");
6263 break;
6266 for (j = 0; j < lbytes; j++)
6268 k = data[j];
6269 if (k >= ' ' && k < 0x80)
6270 printf ("%c", k);
6271 else
6272 printf (".");
6275 putchar ('\n');
6277 data += lbytes;
6278 addr += lbytes;
6279 bytes -= lbytes;
6282 free (start);
6284 return 1;
6288 static unsigned long int
6289 read_leb128 (data, length_return, sign)
6290 unsigned char *data;
6291 int *length_return;
6292 int sign;
6294 unsigned long int result = 0;
6295 unsigned int num_read = 0;
6296 int shift = 0;
6297 unsigned char byte;
6301 byte = *data++;
6302 num_read++;
6304 result |= (byte & 0x7f) << shift;
6306 shift += 7;
6309 while (byte & 0x80);
6311 if (length_return != NULL)
6312 *length_return = num_read;
6314 if (sign && (shift < 32) && (byte & 0x40))
6315 result |= -1 << shift;
6317 return result;
6320 typedef struct State_Machine_Registers
6322 unsigned long address;
6323 unsigned int file;
6324 unsigned int line;
6325 unsigned int column;
6326 int is_stmt;
6327 int basic_block;
6328 int end_sequence;
6329 /* This variable hold the number of the last entry seen
6330 in the File Table. */
6331 unsigned int last_file_entry;
6332 } SMR;
6334 static SMR state_machine_regs;
6336 static void
6337 reset_state_machine (is_stmt)
6338 int is_stmt;
6340 state_machine_regs.address = 0;
6341 state_machine_regs.file = 1;
6342 state_machine_regs.line = 1;
6343 state_machine_regs.column = 0;
6344 state_machine_regs.is_stmt = is_stmt;
6345 state_machine_regs.basic_block = 0;
6346 state_machine_regs.end_sequence = 0;
6347 state_machine_regs.last_file_entry = 0;
6350 /* Handled an extend line op. Returns true if this is the end
6351 of sequence. */
6352 static int
6353 process_extended_line_op (data, is_stmt, pointer_size)
6354 unsigned char *data;
6355 int is_stmt;
6356 int pointer_size;
6358 unsigned char op_code;
6359 int bytes_read;
6360 unsigned int len;
6361 unsigned char *name;
6362 unsigned long adr;
6364 len = read_leb128 (data, & bytes_read, 0);
6365 data += bytes_read;
6367 if (len == 0)
6369 warn (_("badly formed extended line op encountered!\n"));
6370 return bytes_read;
6373 len += bytes_read;
6374 op_code = *data++;
6376 printf (_(" Extended opcode %d: "), op_code);
6378 switch (op_code)
6380 case DW_LNE_end_sequence:
6381 printf (_("End of Sequence\n\n"));
6382 reset_state_machine (is_stmt);
6383 break;
6385 case DW_LNE_set_address:
6386 adr = byte_get (data, pointer_size);
6387 printf (_("set Address to 0x%lx\n"), adr);
6388 state_machine_regs.address = adr;
6389 break;
6391 case DW_LNE_define_file:
6392 printf (_(" define new File Table entry\n"));
6393 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
6395 printf (_(" %d\t"), ++state_machine_regs.last_file_entry);
6396 name = data;
6397 data += strlen ((char *) data) + 1;
6398 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6399 data += bytes_read;
6400 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6401 data += bytes_read;
6402 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6403 printf (_("%s\n\n"), name);
6404 break;
6406 default:
6407 printf (_("UNKNOWN: length %d\n"), len - bytes_read);
6408 break;
6411 return len;
6414 /* Size of pointers in the .debug_line section. This information is not
6415 really present in that section. It's obtained before dumping the debug
6416 sections by doing some pre-scan of the .debug_info section. */
6417 static int debug_line_pointer_size = 4;
6419 static int
6420 display_debug_lines (section, start, file)
6421 Elf_Internal_Shdr *section;
6422 unsigned char * start;
6423 FILE *file ATTRIBUTE_UNUSED;
6425 unsigned char *hdrptr;
6426 DWARF2_Internal_LineInfo info;
6427 unsigned char *standard_opcodes;
6428 unsigned char *data = start;
6429 unsigned char *end = start + section->sh_size;
6430 unsigned char *end_of_sequence;
6431 int i;
6432 int offset_size;
6433 int initial_length_size;
6435 printf (_("\nDump of debug contents of section %s:\n\n"),
6436 SECTION_NAME (section));
6438 while (data < end)
6440 hdrptr = data;
6442 /* Check the length of the block. */
6443 info.li_length = byte_get (hdrptr, 4);
6444 hdrptr += 4;
6446 if (info.li_length == 0xffffffff)
6448 /* This section is 64-bit DWARF 3. */
6449 info.li_length = byte_get (hdrptr, 8);
6450 hdrptr += 8;
6451 offset_size = 8;
6452 initial_length_size = 12;
6454 else
6456 offset_size = 4;
6457 initial_length_size = 4;
6460 if (info.li_length + initial_length_size > section->sh_size)
6462 warn
6463 (_("The line info appears to be corrupt - the section is too small\n"));
6464 return 0;
6467 /* Check its version number. */
6468 info.li_version = byte_get (hdrptr, 2);
6469 hdrptr += 2;
6470 if (info.li_version != 2 && info.li_version != 3)
6472 warn (_("Only DWARF version 2 and 3 line info is currently supported.\n"));
6473 return 0;
6476 info.li_prologue_length = byte_get (hdrptr, offset_size);
6477 hdrptr += offset_size;
6478 info.li_min_insn_length = byte_get (hdrptr, 1);
6479 hdrptr++;
6480 info.li_default_is_stmt = byte_get (hdrptr, 1);
6481 hdrptr++;
6482 info.li_line_base = byte_get (hdrptr, 1);
6483 hdrptr++;
6484 info.li_line_range = byte_get (hdrptr, 1);
6485 hdrptr++;
6486 info.li_opcode_base = byte_get (hdrptr, 1);
6487 hdrptr++;
6489 /* Sign extend the line base field. */
6490 info.li_line_base <<= 24;
6491 info.li_line_base >>= 24;
6493 printf (_(" Length: %ld\n"), info.li_length);
6494 printf (_(" DWARF Version: %d\n"), info.li_version);
6495 printf (_(" Prologue Length: %d\n"), info.li_prologue_length);
6496 printf (_(" Minimum Instruction Length: %d\n"), info.li_min_insn_length);
6497 printf (_(" Initial value of 'is_stmt': %d\n"), info.li_default_is_stmt);
6498 printf (_(" Line Base: %d\n"), info.li_line_base);
6499 printf (_(" Line Range: %d\n"), info.li_line_range);
6500 printf (_(" Opcode Base: %d\n"), info.li_opcode_base);
6502 end_of_sequence = data + info.li_length + initial_length_size;
6504 reset_state_machine (info.li_default_is_stmt);
6506 /* Display the contents of the Opcodes table. */
6507 standard_opcodes = hdrptr;
6509 printf (_("\n Opcodes:\n"));
6511 for (i = 1; i < info.li_opcode_base; i++)
6512 printf (_(" Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
6514 /* Display the contents of the Directory table. */
6515 data = standard_opcodes + info.li_opcode_base - 1;
6517 if (*data == 0)
6518 printf (_("\n The Directory Table is empty.\n"));
6519 else
6521 printf (_("\n The Directory Table:\n"));
6523 while (*data != 0)
6525 printf (_(" %s\n"), data);
6527 data += strlen ((char *) data) + 1;
6531 /* Skip the NUL at the end of the table. */
6532 data++;
6534 /* Display the contents of the File Name table. */
6535 if (*data == 0)
6536 printf (_("\n The File Name Table is empty.\n"));
6537 else
6539 printf (_("\n The File Name Table:\n"));
6540 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
6542 while (*data != 0)
6544 unsigned char *name;
6545 int bytes_read;
6547 printf (_(" %d\t"), ++state_machine_regs.last_file_entry);
6548 name = data;
6550 data += strlen ((char *) data) + 1;
6552 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6553 data += bytes_read;
6554 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6555 data += bytes_read;
6556 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6557 data += bytes_read;
6558 printf (_("%s\n"), name);
6562 /* Skip the NUL at the end of the table. */
6563 data++;
6565 /* Now display the statements. */
6566 printf (_("\n Line Number Statements:\n"));
6569 while (data < end_of_sequence)
6571 unsigned char op_code;
6572 int adv;
6573 int bytes_read;
6575 op_code = *data++;
6577 if (op_code >= info.li_opcode_base)
6579 op_code -= info.li_opcode_base;
6580 adv = (op_code / info.li_line_range) * info.li_min_insn_length;
6581 state_machine_regs.address += adv;
6582 printf (_(" Special opcode %d: advance Address by %d to 0x%lx"),
6583 op_code, adv, state_machine_regs.address);
6584 adv = (op_code % info.li_line_range) + info.li_line_base;
6585 state_machine_regs.line += adv;
6586 printf (_(" and Line by %d to %d\n"),
6587 adv, state_machine_regs.line);
6589 else switch (op_code)
6591 case DW_LNS_extended_op:
6592 data += process_extended_line_op (data, info.li_default_is_stmt,
6593 debug_line_pointer_size);
6594 break;
6596 case DW_LNS_copy:
6597 printf (_(" Copy\n"));
6598 break;
6600 case DW_LNS_advance_pc:
6601 adv = info.li_min_insn_length * read_leb128 (data, & bytes_read, 0);
6602 data += bytes_read;
6603 state_machine_regs.address += adv;
6604 printf (_(" Advance PC by %d to %lx\n"), adv,
6605 state_machine_regs.address);
6606 break;
6608 case DW_LNS_advance_line:
6609 adv = read_leb128 (data, & bytes_read, 1);
6610 data += bytes_read;
6611 state_machine_regs.line += adv;
6612 printf (_(" Advance Line by %d to %d\n"), adv,
6613 state_machine_regs.line);
6614 break;
6616 case DW_LNS_set_file:
6617 adv = read_leb128 (data, & bytes_read, 0);
6618 data += bytes_read;
6619 printf (_(" Set File Name to entry %d in the File Name Table\n"),
6620 adv);
6621 state_machine_regs.file = adv;
6622 break;
6624 case DW_LNS_set_column:
6625 adv = read_leb128 (data, & bytes_read, 0);
6626 data += bytes_read;
6627 printf (_(" Set column to %d\n"), adv);
6628 state_machine_regs.column = adv;
6629 break;
6631 case DW_LNS_negate_stmt:
6632 adv = state_machine_regs.is_stmt;
6633 adv = ! adv;
6634 printf (_(" Set is_stmt to %d\n"), adv);
6635 state_machine_regs.is_stmt = adv;
6636 break;
6638 case DW_LNS_set_basic_block:
6639 printf (_(" Set basic block\n"));
6640 state_machine_regs.basic_block = 1;
6641 break;
6643 case DW_LNS_const_add_pc:
6644 adv = (((255 - info.li_opcode_base) / info.li_line_range)
6645 * info.li_min_insn_length);
6646 state_machine_regs.address += adv;
6647 printf (_(" Advance PC by constant %d to 0x%lx\n"), adv,
6648 state_machine_regs.address);
6649 break;
6651 case DW_LNS_fixed_advance_pc:
6652 adv = byte_get (data, 2);
6653 data += 2;
6654 state_machine_regs.address += adv;
6655 printf (_(" Advance PC by fixed size amount %d to 0x%lx\n"),
6656 adv, state_machine_regs.address);
6657 break;
6659 case DW_LNS_set_prologue_end:
6660 printf (_(" Set prologue_end to true\n"));
6661 break;
6663 case DW_LNS_set_epilogue_begin:
6664 printf (_(" Set epilogue_begin to true\n"));
6665 break;
6667 case DW_LNS_set_isa:
6668 adv = read_leb128 (data, & bytes_read, 0);
6669 data += bytes_read;
6670 printf (_(" Set ISA to %d\n"), adv);
6671 break;
6673 default:
6674 printf (_(" Unknown opcode %d with operands: "), op_code);
6676 int i;
6677 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
6679 printf ("0x%lx%s", read_leb128 (data, &bytes_read, 0),
6680 i == 1 ? "" : ", ");
6681 data += bytes_read;
6683 putchar ('\n');
6685 break;
6688 putchar ('\n');
6691 return 1;
6694 static int
6695 display_debug_pubnames (section, start, file)
6696 Elf_Internal_Shdr *section;
6697 unsigned char *start;
6698 FILE *file ATTRIBUTE_UNUSED;
6700 DWARF2_Internal_PubNames pubnames;
6701 unsigned char *end;
6703 end = start + section->sh_size;
6705 printf (_("Contents of the %s section:\n\n"), SECTION_NAME (section));
6707 while (start < end)
6709 unsigned char *data;
6710 unsigned long offset;
6711 int offset_size, initial_length_size;
6713 data = start;
6715 pubnames.pn_length = byte_get (data, 4);
6716 data += 4;
6717 if (pubnames.pn_length == 0xffffffff)
6719 pubnames.pn_length = byte_get (data, 8);
6720 data += 8;
6721 offset_size = 8;
6722 initial_length_size = 12;
6724 else
6726 offset_size = 4;
6727 initial_length_size = 4;
6730 pubnames.pn_version = byte_get (data, 2);
6731 data += 2;
6732 pubnames.pn_offset = byte_get (data, offset_size);
6733 data += offset_size;
6734 pubnames.pn_size = byte_get (data, offset_size);
6735 data += offset_size;
6737 start += pubnames.pn_length + initial_length_size;
6739 if (pubnames.pn_version != 2 && pubnames.pn_version != 3)
6741 static int warned = 0;
6743 if (! warned)
6745 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
6746 warned = 1;
6749 continue;
6752 printf (_(" Length: %ld\n"),
6753 pubnames.pn_length);
6754 printf (_(" Version: %d\n"),
6755 pubnames.pn_version);
6756 printf (_(" Offset into .debug_info section: %ld\n"),
6757 pubnames.pn_offset);
6758 printf (_(" Size of area in .debug_info section: %ld\n"),
6759 pubnames.pn_size);
6761 printf (_("\n Offset\tName\n"));
6765 offset = byte_get (data, offset_size);
6767 if (offset != 0)
6769 data += offset_size;
6770 printf (" %ld\t\t%s\n", offset, data);
6771 data += strlen ((char *) data) + 1;
6774 while (offset != 0);
6777 printf ("\n");
6778 return 1;
6781 static char *
6782 get_TAG_name (tag)
6783 unsigned long tag;
6785 switch (tag)
6787 case DW_TAG_padding: return "DW_TAG_padding";
6788 case DW_TAG_array_type: return "DW_TAG_array_type";
6789 case DW_TAG_class_type: return "DW_TAG_class_type";
6790 case DW_TAG_entry_point: return "DW_TAG_entry_point";
6791 case DW_TAG_enumeration_type: return "DW_TAG_enumeration_type";
6792 case DW_TAG_formal_parameter: return "DW_TAG_formal_parameter";
6793 case DW_TAG_imported_declaration: return "DW_TAG_imported_declaration";
6794 case DW_TAG_label: return "DW_TAG_label";
6795 case DW_TAG_lexical_block: return "DW_TAG_lexical_block";
6796 case DW_TAG_member: return "DW_TAG_member";
6797 case DW_TAG_pointer_type: return "DW_TAG_pointer_type";
6798 case DW_TAG_reference_type: return "DW_TAG_reference_type";
6799 case DW_TAG_compile_unit: return "DW_TAG_compile_unit";
6800 case DW_TAG_string_type: return "DW_TAG_string_type";
6801 case DW_TAG_structure_type: return "DW_TAG_structure_type";
6802 case DW_TAG_subroutine_type: return "DW_TAG_subroutine_type";
6803 case DW_TAG_typedef: return "DW_TAG_typedef";
6804 case DW_TAG_union_type: return "DW_TAG_union_type";
6805 case DW_TAG_unspecified_parameters: return "DW_TAG_unspecified_parameters";
6806 case DW_TAG_variant: return "DW_TAG_variant";
6807 case DW_TAG_common_block: return "DW_TAG_common_block";
6808 case DW_TAG_common_inclusion: return "DW_TAG_common_inclusion";
6809 case DW_TAG_inheritance: return "DW_TAG_inheritance";
6810 case DW_TAG_inlined_subroutine: return "DW_TAG_inlined_subroutine";
6811 case DW_TAG_module: return "DW_TAG_module";
6812 case DW_TAG_ptr_to_member_type: return "DW_TAG_ptr_to_member_type";
6813 case DW_TAG_set_type: return "DW_TAG_set_type";
6814 case DW_TAG_subrange_type: return "DW_TAG_subrange_type";
6815 case DW_TAG_with_stmt: return "DW_TAG_with_stmt";
6816 case DW_TAG_access_declaration: return "DW_TAG_access_declaration";
6817 case DW_TAG_base_type: return "DW_TAG_base_type";
6818 case DW_TAG_catch_block: return "DW_TAG_catch_block";
6819 case DW_TAG_const_type: return "DW_TAG_const_type";
6820 case DW_TAG_constant: return "DW_TAG_constant";
6821 case DW_TAG_enumerator: return "DW_TAG_enumerator";
6822 case DW_TAG_file_type: return "DW_TAG_file_type";
6823 case DW_TAG_friend: return "DW_TAG_friend";
6824 case DW_TAG_namelist: return "DW_TAG_namelist";
6825 case DW_TAG_namelist_item: return "DW_TAG_namelist_item";
6826 case DW_TAG_packed_type: return "DW_TAG_packed_type";
6827 case DW_TAG_subprogram: return "DW_TAG_subprogram";
6828 case DW_TAG_template_type_param: return "DW_TAG_template_type_param";
6829 case DW_TAG_template_value_param: return "DW_TAG_template_value_param";
6830 case DW_TAG_thrown_type: return "DW_TAG_thrown_type";
6831 case DW_TAG_try_block: return "DW_TAG_try_block";
6832 case DW_TAG_variant_part: return "DW_TAG_variant_part";
6833 case DW_TAG_variable: return "DW_TAG_variable";
6834 case DW_TAG_volatile_type: return "DW_TAG_volatile_type";
6835 case DW_TAG_MIPS_loop: return "DW_TAG_MIPS_loop";
6836 case DW_TAG_format_label: return "DW_TAG_format_label";
6837 case DW_TAG_function_template: return "DW_TAG_function_template";
6838 case DW_TAG_class_template: return "DW_TAG_class_template";
6839 /* DWARF 2.1 values. */
6840 case DW_TAG_dwarf_procedure: return "DW_TAG_dwarf_procedure";
6841 case DW_TAG_restrict_type: return "DW_TAG_restrict_type";
6842 case DW_TAG_interface_type: return "DW_TAG_interface_type";
6843 case DW_TAG_namespace: return "DW_TAG_namespace";
6844 case DW_TAG_imported_module: return "DW_TAG_imported_module";
6845 case DW_TAG_unspecified_type: return "DW_TAG_unspecified_type";
6846 case DW_TAG_partial_unit: return "DW_TAG_partial_unit";
6847 case DW_TAG_imported_unit: return "DW_TAG_imported_unit";
6848 /* UPC values. */
6849 case DW_TAG_upc_shared_type: return "DW_TAG_upc_shared_type";
6850 case DW_TAG_upc_strict_type: return "DW_TAG_upc_strict_type";
6851 case DW_TAG_upc_relaxed_type: return "DW_TAG_upc_relaxed_type";
6852 default:
6854 static char buffer[100];
6856 sprintf (buffer, _("Unknown TAG value: %lx"), tag);
6857 return buffer;
6862 static char *
6863 get_AT_name (attribute)
6864 unsigned long attribute;
6866 switch (attribute)
6868 case DW_AT_sibling: return "DW_AT_sibling";
6869 case DW_AT_location: return "DW_AT_location";
6870 case DW_AT_name: return "DW_AT_name";
6871 case DW_AT_ordering: return "DW_AT_ordering";
6872 case DW_AT_subscr_data: return "DW_AT_subscr_data";
6873 case DW_AT_byte_size: return "DW_AT_byte_size";
6874 case DW_AT_bit_offset: return "DW_AT_bit_offset";
6875 case DW_AT_bit_size: return "DW_AT_bit_size";
6876 case DW_AT_element_list: return "DW_AT_element_list";
6877 case DW_AT_stmt_list: return "DW_AT_stmt_list";
6878 case DW_AT_low_pc: return "DW_AT_low_pc";
6879 case DW_AT_high_pc: return "DW_AT_high_pc";
6880 case DW_AT_language: return "DW_AT_language";
6881 case DW_AT_member: return "DW_AT_member";
6882 case DW_AT_discr: return "DW_AT_discr";
6883 case DW_AT_discr_value: return "DW_AT_discr_value";
6884 case DW_AT_visibility: return "DW_AT_visibility";
6885 case DW_AT_import: return "DW_AT_import";
6886 case DW_AT_string_length: return "DW_AT_string_length";
6887 case DW_AT_common_reference: return "DW_AT_common_reference";
6888 case DW_AT_comp_dir: return "DW_AT_comp_dir";
6889 case DW_AT_const_value: return "DW_AT_const_value";
6890 case DW_AT_containing_type: return "DW_AT_containing_type";
6891 case DW_AT_default_value: return "DW_AT_default_value";
6892 case DW_AT_inline: return "DW_AT_inline";
6893 case DW_AT_is_optional: return "DW_AT_is_optional";
6894 case DW_AT_lower_bound: return "DW_AT_lower_bound";
6895 case DW_AT_producer: return "DW_AT_producer";
6896 case DW_AT_prototyped: return "DW_AT_prototyped";
6897 case DW_AT_return_addr: return "DW_AT_return_addr";
6898 case DW_AT_start_scope: return "DW_AT_start_scope";
6899 case DW_AT_stride_size: return "DW_AT_stride_size";
6900 case DW_AT_upper_bound: return "DW_AT_upper_bound";
6901 case DW_AT_abstract_origin: return "DW_AT_abstract_origin";
6902 case DW_AT_accessibility: return "DW_AT_accessibility";
6903 case DW_AT_address_class: return "DW_AT_address_class";
6904 case DW_AT_artificial: return "DW_AT_artificial";
6905 case DW_AT_base_types: return "DW_AT_base_types";
6906 case DW_AT_calling_convention: return "DW_AT_calling_convention";
6907 case DW_AT_count: return "DW_AT_count";
6908 case DW_AT_data_member_location: return "DW_AT_data_member_location";
6909 case DW_AT_decl_column: return "DW_AT_decl_column";
6910 case DW_AT_decl_file: return "DW_AT_decl_file";
6911 case DW_AT_decl_line: return "DW_AT_decl_line";
6912 case DW_AT_declaration: return "DW_AT_declaration";
6913 case DW_AT_discr_list: return "DW_AT_discr_list";
6914 case DW_AT_encoding: return "DW_AT_encoding";
6915 case DW_AT_external: return "DW_AT_external";
6916 case DW_AT_frame_base: return "DW_AT_frame_base";
6917 case DW_AT_friend: return "DW_AT_friend";
6918 case DW_AT_identifier_case: return "DW_AT_identifier_case";
6919 case DW_AT_macro_info: return "DW_AT_macro_info";
6920 case DW_AT_namelist_items: return "DW_AT_namelist_items";
6921 case DW_AT_priority: return "DW_AT_priority";
6922 case DW_AT_segment: return "DW_AT_segment";
6923 case DW_AT_specification: return "DW_AT_specification";
6924 case DW_AT_static_link: return "DW_AT_static_link";
6925 case DW_AT_type: return "DW_AT_type";
6926 case DW_AT_use_location: return "DW_AT_use_location";
6927 case DW_AT_variable_parameter: return "DW_AT_variable_parameter";
6928 case DW_AT_virtuality: return "DW_AT_virtuality";
6929 case DW_AT_vtable_elem_location: return "DW_AT_vtable_elem_location";
6930 /* DWARF 2.1 values. */
6931 case DW_AT_allocated: return "DW_AT_allocated";
6932 case DW_AT_associated: return "DW_AT_associated";
6933 case DW_AT_data_location: return "DW_AT_data_location";
6934 case DW_AT_stride: return "DW_AT_stride";
6935 case DW_AT_entry_pc: return "DW_AT_entry_pc";
6936 case DW_AT_use_UTF8: return "DW_AT_use_UTF8";
6937 case DW_AT_extension: return "DW_AT_extension";
6938 case DW_AT_ranges: return "DW_AT_ranges";
6939 case DW_AT_trampoline: return "DW_AT_trampoline";
6940 case DW_AT_call_column: return "DW_AT_call_column";
6941 case DW_AT_call_file: return "DW_AT_call_file";
6942 case DW_AT_call_line: return "DW_AT_call_line";
6943 /* SGI/MIPS extensions. */
6944 case DW_AT_MIPS_fde: return "DW_AT_MIPS_fde";
6945 case DW_AT_MIPS_loop_begin: return "DW_AT_MIPS_loop_begin";
6946 case DW_AT_MIPS_tail_loop_begin: return "DW_AT_MIPS_tail_loop_begin";
6947 case DW_AT_MIPS_epilog_begin: return "DW_AT_MIPS_epilog_begin";
6948 case DW_AT_MIPS_loop_unroll_factor: return "DW_AT_MIPS_loop_unroll_factor";
6949 case DW_AT_MIPS_software_pipeline_depth:
6950 return "DW_AT_MIPS_software_pipeline_depth";
6951 case DW_AT_MIPS_linkage_name: return "DW_AT_MIPS_linkage_name";
6952 case DW_AT_MIPS_stride: return "DW_AT_MIPS_stride";
6953 case DW_AT_MIPS_abstract_name: return "DW_AT_MIPS_abstract_name";
6954 case DW_AT_MIPS_clone_origin: return "DW_AT_MIPS_clone_origin";
6955 case DW_AT_MIPS_has_inlines: return "DW_AT_MIPS_has_inlines";
6956 /* GNU extensions. */
6957 case DW_AT_sf_names: return "DW_AT_sf_names";
6958 case DW_AT_src_info: return "DW_AT_src_info";
6959 case DW_AT_mac_info: return "DW_AT_mac_info";
6960 case DW_AT_src_coords: return "DW_AT_src_coords";
6961 case DW_AT_body_begin: return "DW_AT_body_begin";
6962 case DW_AT_body_end: return "DW_AT_body_end";
6963 case DW_AT_GNU_vector: return "DW_AT_GNU_vector";
6964 /* UPC extension. */
6965 case DW_AT_upc_threads_scaled: return "DW_AT_upc_threads_scaled";
6966 default:
6968 static char buffer[100];
6970 sprintf (buffer, _("Unknown AT value: %lx"), attribute);
6971 return buffer;
6976 static char *
6977 get_FORM_name (form)
6978 unsigned long form;
6980 switch (form)
6982 case DW_FORM_addr: return "DW_FORM_addr";
6983 case DW_FORM_block2: return "DW_FORM_block2";
6984 case DW_FORM_block4: return "DW_FORM_block4";
6985 case DW_FORM_data2: return "DW_FORM_data2";
6986 case DW_FORM_data4: return "DW_FORM_data4";
6987 case DW_FORM_data8: return "DW_FORM_data8";
6988 case DW_FORM_string: return "DW_FORM_string";
6989 case DW_FORM_block: return "DW_FORM_block";
6990 case DW_FORM_block1: return "DW_FORM_block1";
6991 case DW_FORM_data1: return "DW_FORM_data1";
6992 case DW_FORM_flag: return "DW_FORM_flag";
6993 case DW_FORM_sdata: return "DW_FORM_sdata";
6994 case DW_FORM_strp: return "DW_FORM_strp";
6995 case DW_FORM_udata: return "DW_FORM_udata";
6996 case DW_FORM_ref_addr: return "DW_FORM_ref_addr";
6997 case DW_FORM_ref1: return "DW_FORM_ref1";
6998 case DW_FORM_ref2: return "DW_FORM_ref2";
6999 case DW_FORM_ref4: return "DW_FORM_ref4";
7000 case DW_FORM_ref8: return "DW_FORM_ref8";
7001 case DW_FORM_ref_udata: return "DW_FORM_ref_udata";
7002 case DW_FORM_indirect: return "DW_FORM_indirect";
7003 default:
7005 static char buffer[100];
7007 sprintf (buffer, _("Unknown FORM value: %lx"), form);
7008 return buffer;
7013 /* FIXME: There are better and more effiecint ways to handle
7014 these structures. For now though, I just want something that
7015 is simple to implement. */
7016 typedef struct abbrev_attr
7018 unsigned long attribute;
7019 unsigned long form;
7020 struct abbrev_attr *next;
7022 abbrev_attr;
7024 typedef struct abbrev_entry
7026 unsigned long entry;
7027 unsigned long tag;
7028 int children;
7029 struct abbrev_attr *first_attr;
7030 struct abbrev_attr *last_attr;
7031 struct abbrev_entry *next;
7033 abbrev_entry;
7035 static abbrev_entry *first_abbrev = NULL;
7036 static abbrev_entry *last_abbrev = NULL;
7038 static void
7039 free_abbrevs ()
7041 abbrev_entry *abbrev;
7043 for (abbrev = first_abbrev; abbrev;)
7045 abbrev_entry *next = abbrev->next;
7046 abbrev_attr *attr;
7048 for (attr = abbrev->first_attr; attr;)
7050 abbrev_attr *next = attr->next;
7052 free (attr);
7053 attr = next;
7056 free (abbrev);
7057 abbrev = next;
7060 last_abbrev = first_abbrev = NULL;
7063 static void
7064 add_abbrev (number, tag, children)
7065 unsigned long number;
7066 unsigned long tag;
7067 int children;
7069 abbrev_entry *entry;
7071 entry = (abbrev_entry *) malloc (sizeof (*entry));
7073 if (entry == NULL)
7074 /* ugg */
7075 return;
7077 entry->entry = number;
7078 entry->tag = tag;
7079 entry->children = children;
7080 entry->first_attr = NULL;
7081 entry->last_attr = NULL;
7082 entry->next = NULL;
7084 if (first_abbrev == NULL)
7085 first_abbrev = entry;
7086 else
7087 last_abbrev->next = entry;
7089 last_abbrev = entry;
7092 static void
7093 add_abbrev_attr (attribute, form)
7094 unsigned long attribute;
7095 unsigned long form;
7097 abbrev_attr *attr;
7099 attr = (abbrev_attr *) malloc (sizeof (*attr));
7101 if (attr == NULL)
7102 /* ugg */
7103 return;
7105 attr->attribute = attribute;
7106 attr->form = form;
7107 attr->next = NULL;
7109 if (last_abbrev->first_attr == NULL)
7110 last_abbrev->first_attr = attr;
7111 else
7112 last_abbrev->last_attr->next = attr;
7114 last_abbrev->last_attr = attr;
7117 /* Processes the (partial) contents of a .debug_abbrev section.
7118 Returns NULL if the end of the section was encountered.
7119 Returns the address after the last byte read if the end of
7120 an abbreviation set was found. */
7122 static unsigned char *
7123 process_abbrev_section (start, end)
7124 unsigned char *start;
7125 unsigned char *end;
7127 if (first_abbrev != NULL)
7128 return NULL;
7130 while (start < end)
7132 int bytes_read;
7133 unsigned long entry;
7134 unsigned long tag;
7135 unsigned long attribute;
7136 int children;
7138 entry = read_leb128 (start, & bytes_read, 0);
7139 start += bytes_read;
7141 /* A single zero is supposed to end the section according
7142 to the standard. If there's more, then signal that to
7143 the caller. */
7144 if (entry == 0)
7145 return start == end ? NULL : start;
7147 tag = read_leb128 (start, & bytes_read, 0);
7148 start += bytes_read;
7150 children = *start++;
7152 add_abbrev (entry, tag, children);
7156 unsigned long form;
7158 attribute = read_leb128 (start, & bytes_read, 0);
7159 start += bytes_read;
7161 form = read_leb128 (start, & bytes_read, 0);
7162 start += bytes_read;
7164 if (attribute != 0)
7165 add_abbrev_attr (attribute, form);
7167 while (attribute != 0);
7170 return NULL;
7174 static int
7175 display_debug_macinfo (section, start, file)
7176 Elf_Internal_Shdr *section;
7177 unsigned char *start;
7178 FILE *file ATTRIBUTE_UNUSED;
7180 unsigned char *end = start + section->sh_size;
7181 unsigned char *curr = start;
7182 unsigned int bytes_read;
7183 enum dwarf_macinfo_record_type op;
7185 printf (_("Contents of the %s section:\n\n"), SECTION_NAME (section));
7187 while (curr < end)
7189 unsigned int lineno;
7190 const char *string;
7192 op = *curr;
7193 curr++;
7195 switch (op)
7197 case DW_MACINFO_start_file:
7199 unsigned int filenum;
7201 lineno = read_leb128 (curr, & bytes_read, 0);
7202 curr += bytes_read;
7203 filenum = read_leb128 (curr, & bytes_read, 0);
7204 curr += bytes_read;
7206 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"), lineno, filenum);
7208 break;
7210 case DW_MACINFO_end_file:
7211 printf (_(" DW_MACINFO_end_file\n"));
7212 break;
7214 case DW_MACINFO_define:
7215 lineno = read_leb128 (curr, & bytes_read, 0);
7216 curr += bytes_read;
7217 string = curr;
7218 curr += strlen (string) + 1;
7219 printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"), lineno, string);
7220 break;
7222 case DW_MACINFO_undef:
7223 lineno = read_leb128 (curr, & bytes_read, 0);
7224 curr += bytes_read;
7225 string = curr;
7226 curr += strlen (string) + 1;
7227 printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"), lineno, string);
7228 break;
7230 case DW_MACINFO_vendor_ext:
7232 unsigned int constant;
7234 constant = read_leb128 (curr, & bytes_read, 0);
7235 curr += bytes_read;
7236 string = curr;
7237 curr += strlen (string) + 1;
7238 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"), constant, string);
7240 break;
7244 return 1;
7248 static int
7249 display_debug_abbrev (section, start, file)
7250 Elf_Internal_Shdr *section;
7251 unsigned char *start;
7252 FILE *file ATTRIBUTE_UNUSED;
7254 abbrev_entry *entry;
7255 unsigned char *end = start + section->sh_size;
7257 printf (_("Contents of the %s section:\n\n"), SECTION_NAME (section));
7261 start = process_abbrev_section (start, end);
7263 if (first_abbrev == NULL)
7264 continue;
7266 printf (_(" Number TAG\n"));
7268 for (entry = first_abbrev; entry; entry = entry->next)
7270 abbrev_attr *attr;
7272 printf (_(" %ld %s [%s]\n"),
7273 entry->entry,
7274 get_TAG_name (entry->tag),
7275 entry->children ? _("has children") : _("no children"));
7277 for (attr = entry->first_attr; attr; attr = attr->next)
7279 printf (_(" %-18s %s\n"),
7280 get_AT_name (attr->attribute),
7281 get_FORM_name (attr->form));
7285 free_abbrevs ();
7287 while (start);
7289 printf ("\n");
7291 return 1;
7295 static unsigned char *
7296 display_block (data, length)
7297 unsigned char *data;
7298 unsigned long length;
7300 printf (_(" %lu byte block: "), length);
7302 while (length --)
7303 printf ("%lx ", (unsigned long) byte_get (data++, 1));
7305 return data;
7308 static void
7309 decode_location_expression (data, pointer_size, length)
7310 unsigned char * data;
7311 unsigned int pointer_size;
7312 unsigned long length;
7314 unsigned op;
7315 int bytes_read;
7316 unsigned long uvalue;
7317 unsigned char *end = data + length;
7319 while (data < end)
7321 op = *data++;
7323 switch (op)
7325 case DW_OP_addr:
7326 printf ("DW_OP_addr: %lx",
7327 (unsigned long) byte_get (data, pointer_size));
7328 data += pointer_size;
7329 break;
7330 case DW_OP_deref:
7331 printf ("DW_OP_deref");
7332 break;
7333 case DW_OP_const1u:
7334 printf ("DW_OP_const1u: %lu", (unsigned long) byte_get (data++, 1));
7335 break;
7336 case DW_OP_const1s:
7337 printf ("DW_OP_const1s: %ld", (long) byte_get (data++, 1));
7338 break;
7339 case DW_OP_const2u:
7340 printf ("DW_OP_const2u: %lu", (unsigned long) byte_get (data, 2));
7341 data += 2;
7342 break;
7343 case DW_OP_const2s:
7344 printf ("DW_OP_const2s: %ld", (long) byte_get (data, 2));
7345 data += 2;
7346 break;
7347 case DW_OP_const4u:
7348 printf ("DW_OP_const4u: %lu", (unsigned long) byte_get (data, 4));
7349 data += 4;
7350 break;
7351 case DW_OP_const4s:
7352 printf ("DW_OP_const4s: %ld", (long) byte_get (data, 4));
7353 data += 4;
7354 break;
7355 case DW_OP_const8u:
7356 printf ("DW_OP_const8u: %lu %lu", (unsigned long) byte_get (data, 4),
7357 (unsigned long) byte_get (data + 4, 4));
7358 data += 8;
7359 break;
7360 case DW_OP_const8s:
7361 printf ("DW_OP_const8s: %ld %ld", (long) byte_get (data, 4),
7362 (long) byte_get (data + 4, 4));
7363 data += 8;
7364 break;
7365 case DW_OP_constu:
7366 printf ("DW_OP_constu: %lu", read_leb128 (data, &bytes_read, 0));
7367 data += bytes_read;
7368 break;
7369 case DW_OP_consts:
7370 printf ("DW_OP_consts: %ld", read_leb128 (data, &bytes_read, 1));
7371 data += bytes_read;
7372 break;
7373 case DW_OP_dup:
7374 printf ("DW_OP_dup");
7375 break;
7376 case DW_OP_drop:
7377 printf ("DW_OP_drop");
7378 break;
7379 case DW_OP_over:
7380 printf ("DW_OP_over");
7381 break;
7382 case DW_OP_pick:
7383 printf ("DW_OP_pick: %ld", (unsigned long) byte_get (data++, 1));
7384 break;
7385 case DW_OP_swap:
7386 printf ("DW_OP_swap");
7387 break;
7388 case DW_OP_rot:
7389 printf ("DW_OP_rot");
7390 break;
7391 case DW_OP_xderef:
7392 printf ("DW_OP_xderef");
7393 break;
7394 case DW_OP_abs:
7395 printf ("DW_OP_abs");
7396 break;
7397 case DW_OP_and:
7398 printf ("DW_OP_and");
7399 break;
7400 case DW_OP_div:
7401 printf ("DW_OP_div");
7402 break;
7403 case DW_OP_minus:
7404 printf ("DW_OP_minus");
7405 break;
7406 case DW_OP_mod:
7407 printf ("DW_OP_mod");
7408 break;
7409 case DW_OP_mul:
7410 printf ("DW_OP_mul");
7411 break;
7412 case DW_OP_neg:
7413 printf ("DW_OP_neg");
7414 break;
7415 case DW_OP_not:
7416 printf ("DW_OP_not");
7417 break;
7418 case DW_OP_or:
7419 printf ("DW_OP_or");
7420 break;
7421 case DW_OP_plus:
7422 printf ("DW_OP_plus");
7423 break;
7424 case DW_OP_plus_uconst:
7425 printf ("DW_OP_plus_uconst: %lu",
7426 read_leb128 (data, &bytes_read, 0));
7427 data += bytes_read;
7428 break;
7429 case DW_OP_shl:
7430 printf ("DW_OP_shl");
7431 break;
7432 case DW_OP_shr:
7433 printf ("DW_OP_shr");
7434 break;
7435 case DW_OP_shra:
7436 printf ("DW_OP_shra");
7437 break;
7438 case DW_OP_xor:
7439 printf ("DW_OP_xor");
7440 break;
7441 case DW_OP_bra:
7442 printf ("DW_OP_bra: %ld", (long) byte_get (data, 2));
7443 data += 2;
7444 break;
7445 case DW_OP_eq:
7446 printf ("DW_OP_eq");
7447 break;
7448 case DW_OP_ge:
7449 printf ("DW_OP_ge");
7450 break;
7451 case DW_OP_gt:
7452 printf ("DW_OP_gt");
7453 break;
7454 case DW_OP_le:
7455 printf ("DW_OP_le");
7456 break;
7457 case DW_OP_lt:
7458 printf ("DW_OP_lt");
7459 break;
7460 case DW_OP_ne:
7461 printf ("DW_OP_ne");
7462 break;
7463 case DW_OP_skip:
7464 printf ("DW_OP_skip: %ld", (long) byte_get (data, 2));
7465 data += 2;
7466 break;
7468 case DW_OP_lit0:
7469 case DW_OP_lit1:
7470 case DW_OP_lit2:
7471 case DW_OP_lit3:
7472 case DW_OP_lit4:
7473 case DW_OP_lit5:
7474 case DW_OP_lit6:
7475 case DW_OP_lit7:
7476 case DW_OP_lit8:
7477 case DW_OP_lit9:
7478 case DW_OP_lit10:
7479 case DW_OP_lit11:
7480 case DW_OP_lit12:
7481 case DW_OP_lit13:
7482 case DW_OP_lit14:
7483 case DW_OP_lit15:
7484 case DW_OP_lit16:
7485 case DW_OP_lit17:
7486 case DW_OP_lit18:
7487 case DW_OP_lit19:
7488 case DW_OP_lit20:
7489 case DW_OP_lit21:
7490 case DW_OP_lit22:
7491 case DW_OP_lit23:
7492 case DW_OP_lit24:
7493 case DW_OP_lit25:
7494 case DW_OP_lit26:
7495 case DW_OP_lit27:
7496 case DW_OP_lit28:
7497 case DW_OP_lit29:
7498 case DW_OP_lit30:
7499 case DW_OP_lit31:
7500 printf ("DW_OP_lit%d", op - DW_OP_lit0);
7501 break;
7503 case DW_OP_reg0:
7504 case DW_OP_reg1:
7505 case DW_OP_reg2:
7506 case DW_OP_reg3:
7507 case DW_OP_reg4:
7508 case DW_OP_reg5:
7509 case DW_OP_reg6:
7510 case DW_OP_reg7:
7511 case DW_OP_reg8:
7512 case DW_OP_reg9:
7513 case DW_OP_reg10:
7514 case DW_OP_reg11:
7515 case DW_OP_reg12:
7516 case DW_OP_reg13:
7517 case DW_OP_reg14:
7518 case DW_OP_reg15:
7519 case DW_OP_reg16:
7520 case DW_OP_reg17:
7521 case DW_OP_reg18:
7522 case DW_OP_reg19:
7523 case DW_OP_reg20:
7524 case DW_OP_reg21:
7525 case DW_OP_reg22:
7526 case DW_OP_reg23:
7527 case DW_OP_reg24:
7528 case DW_OP_reg25:
7529 case DW_OP_reg26:
7530 case DW_OP_reg27:
7531 case DW_OP_reg28:
7532 case DW_OP_reg29:
7533 case DW_OP_reg30:
7534 case DW_OP_reg31:
7535 printf ("DW_OP_reg%d", op - DW_OP_reg0);
7536 break;
7538 case DW_OP_breg0:
7539 case DW_OP_breg1:
7540 case DW_OP_breg2:
7541 case DW_OP_breg3:
7542 case DW_OP_breg4:
7543 case DW_OP_breg5:
7544 case DW_OP_breg6:
7545 case DW_OP_breg7:
7546 case DW_OP_breg8:
7547 case DW_OP_breg9:
7548 case DW_OP_breg10:
7549 case DW_OP_breg11:
7550 case DW_OP_breg12:
7551 case DW_OP_breg13:
7552 case DW_OP_breg14:
7553 case DW_OP_breg15:
7554 case DW_OP_breg16:
7555 case DW_OP_breg17:
7556 case DW_OP_breg18:
7557 case DW_OP_breg19:
7558 case DW_OP_breg20:
7559 case DW_OP_breg21:
7560 case DW_OP_breg22:
7561 case DW_OP_breg23:
7562 case DW_OP_breg24:
7563 case DW_OP_breg25:
7564 case DW_OP_breg26:
7565 case DW_OP_breg27:
7566 case DW_OP_breg28:
7567 case DW_OP_breg29:
7568 case DW_OP_breg30:
7569 case DW_OP_breg31:
7570 printf ("DW_OP_breg%d: %ld", op - DW_OP_breg0,
7571 read_leb128 (data, &bytes_read, 1));
7572 data += bytes_read;
7573 break;
7575 case DW_OP_regx:
7576 printf ("DW_OP_regx: %lu", read_leb128 (data, &bytes_read, 0));
7577 data += bytes_read;
7578 break;
7579 case DW_OP_fbreg:
7580 printf ("DW_OP_fbreg: %ld", read_leb128 (data, &bytes_read, 1));
7581 data += bytes_read;
7582 break;
7583 case DW_OP_bregx:
7584 uvalue = read_leb128 (data, &bytes_read, 0);
7585 data += bytes_read;
7586 printf ("DW_OP_bregx: %lu %ld", uvalue,
7587 read_leb128 (data, &bytes_read, 1));
7588 data += bytes_read;
7589 break;
7590 case DW_OP_piece:
7591 printf ("DW_OP_piece: %lu", read_leb128 (data, &bytes_read, 0));
7592 data += bytes_read;
7593 break;
7594 case DW_OP_deref_size:
7595 printf ("DW_OP_deref_size: %ld", (long) byte_get (data++, 1));
7596 break;
7597 case DW_OP_xderef_size:
7598 printf ("DW_OP_xderef_size: %ld", (long) byte_get (data++, 1));
7599 break;
7600 case DW_OP_nop:
7601 printf ("DW_OP_nop");
7602 break;
7604 /* DWARF 3 extensions. */
7605 case DW_OP_push_object_address:
7606 printf ("DW_OP_push_object_address");
7607 break;
7608 case DW_OP_call2:
7609 printf ("DW_OP_call2: <%lx>", (long) byte_get (data, 2));
7610 data += 2;
7611 break;
7612 case DW_OP_call4:
7613 printf ("DW_OP_call4: <%lx>", (long) byte_get (data, 4));
7614 data += 4;
7615 break;
7616 case DW_OP_call_ref:
7617 printf ("DW_OP_call_ref");
7618 break;
7620 /* GNU extensions. */
7621 case DW_OP_GNU_push_tls_address:
7622 printf ("DW_OP_GNU_push_tls_address");
7623 break;
7625 default:
7626 if (op >= DW_OP_lo_user
7627 && op <= DW_OP_hi_user)
7628 printf (_("(User defined location op)"));
7629 else
7630 printf (_("(Unknown location op)"));
7631 /* No way to tell where the next op is, so just bail. */
7632 return;
7635 /* Separate the ops. */
7636 printf ("; ");
7640 static const char *debug_loc_contents;
7641 static bfd_vma debug_loc_size;
7643 static void
7644 load_debug_loc (file)
7645 FILE *file;
7647 Elf_Internal_Shdr *sec;
7648 unsigned int i;
7650 /* If it is already loaded, do nothing. */
7651 if (debug_loc_contents != NULL)
7652 return;
7654 /* Locate the .debug_loc section. */
7655 for (i = 0, sec = section_headers;
7656 i < elf_header.e_shnum;
7657 i++, sec++)
7658 if (strcmp (SECTION_NAME (sec), ".debug_loc") == 0)
7659 break;
7661 if (i == elf_header.e_shnum || sec->sh_size == 0)
7662 return;
7664 debug_loc_size = sec->sh_size;
7666 debug_loc_contents = ((char *)
7667 get_data (NULL, file, sec->sh_offset, sec->sh_size,
7668 _("debug_loc section data")));
7671 static void
7672 free_debug_loc ()
7674 if (debug_loc_contents == NULL)
7675 return;
7677 free ((char *) debug_loc_contents);
7678 debug_loc_contents = NULL;
7679 debug_loc_size = 0;
7683 static int
7684 display_debug_loc (section, start, file)
7685 Elf_Internal_Shdr *section;
7686 unsigned char *start;
7687 FILE *file ATTRIBUTE_UNUSED;
7689 unsigned char *section_end;
7690 unsigned long bytes;
7691 unsigned char *section_begin = start;
7692 bfd_vma addr;
7694 addr = section->sh_addr;
7695 bytes = section->sh_size;
7696 section_end = start + bytes;
7698 if (bytes == 0)
7700 printf (_("\nThe .debug_loc section is empty.\n"));
7701 return 0;
7704 printf (_("Contents of the .debug_loc section:\n\n"));
7705 printf (_("\n Offset Begin End Expression\n"));
7707 while (start < section_end)
7709 unsigned long begin;
7710 unsigned long end;
7711 unsigned short length;
7712 unsigned long offset;
7714 offset = start - section_begin;
7716 while (1)
7718 /* Normally, the lists in the debug_loc section are related to a
7719 given compilation unit, and thus, we would use the pointer size
7720 of that compilation unit. However, since we are displaying it
7721 seperately here, we either have to store pointer sizes of all
7722 compilation units, or assume they don't change. We assume,
7723 like the debug_line display, that it doesn't change. */
7724 begin = byte_get (start, debug_line_pointer_size);
7725 start += debug_line_pointer_size;
7726 end = byte_get (start, debug_line_pointer_size);
7727 start += debug_line_pointer_size;
7729 if (begin == 0 && end == 0)
7730 break;
7732 /* For now, skip any base address specifiers. */
7733 if (begin == 0xffffffff)
7734 continue;
7736 begin += addr;
7737 end += addr;
7739 length = byte_get (start, 2);
7740 start += 2;
7742 printf (" %8.8lx %8.8lx %8.8lx (", offset, begin, end);
7743 decode_location_expression (start, debug_line_pointer_size, length);
7744 printf (")\n");
7746 start += length;
7748 printf ("\n");
7750 return 1;
7753 static const char *debug_str_contents;
7754 static bfd_vma debug_str_size;
7756 static void
7757 load_debug_str (file)
7758 FILE *file;
7760 Elf_Internal_Shdr *sec;
7761 unsigned int i;
7763 /* If it is already loaded, do nothing. */
7764 if (debug_str_contents != NULL)
7765 return;
7767 /* Locate the .debug_str section. */
7768 for (i = 0, sec = section_headers;
7769 i < elf_header.e_shnum;
7770 i++, sec++)
7771 if (strcmp (SECTION_NAME (sec), ".debug_str") == 0)
7772 break;
7774 if (i == elf_header.e_shnum || sec->sh_size == 0)
7775 return;
7777 debug_str_size = sec->sh_size;
7779 debug_str_contents = ((char *)
7780 get_data (NULL, file, sec->sh_offset, sec->sh_size,
7781 _("debug_str section data")));
7784 static void
7785 free_debug_str ()
7787 if (debug_str_contents == NULL)
7788 return;
7790 free ((char *) debug_str_contents);
7791 debug_str_contents = NULL;
7792 debug_str_size = 0;
7795 static const char *
7796 fetch_indirect_string (offset)
7797 unsigned long offset;
7799 if (debug_str_contents == NULL)
7800 return _("<no .debug_str section>");
7802 if (offset > debug_str_size)
7803 return _("<offset is too big>");
7805 return debug_str_contents + offset;
7808 static int
7809 display_debug_str (section, start, file)
7810 Elf_Internal_Shdr *section;
7811 unsigned char *start;
7812 FILE *file ATTRIBUTE_UNUSED;
7814 unsigned long bytes;
7815 bfd_vma addr;
7817 addr = section->sh_addr;
7818 bytes = section->sh_size;
7820 if (bytes == 0)
7822 printf (_("\nThe .debug_str section is empty.\n"));
7823 return 0;
7826 printf (_("Contents of the .debug_str section:\n\n"));
7828 while (bytes)
7830 int j;
7831 int k;
7832 int lbytes;
7834 lbytes = (bytes > 16 ? 16 : bytes);
7836 printf (" 0x%8.8lx ", (unsigned long) addr);
7838 for (j = 0; j < 16; j++)
7840 if (j < lbytes)
7841 printf ("%2.2x", start[j]);
7842 else
7843 printf (" ");
7845 if ((j & 3) == 3)
7846 printf (" ");
7849 for (j = 0; j < lbytes; j++)
7851 k = start[j];
7852 if (k >= ' ' && k < 0x80)
7853 printf ("%c", k);
7854 else
7855 printf (".");
7858 putchar ('\n');
7860 start += lbytes;
7861 addr += lbytes;
7862 bytes -= lbytes;
7865 return 1;
7868 static unsigned char *
7869 read_and_display_attr_value (attribute, form, data, cu_offset, pointer_size,
7870 offset_size, dwarf_version)
7871 unsigned long attribute;
7872 unsigned long form;
7873 unsigned char *data;
7874 unsigned long cu_offset;
7875 unsigned long pointer_size;
7876 unsigned long offset_size;
7877 int dwarf_version;
7879 unsigned long uvalue = 0;
7880 unsigned char *block_start = NULL;
7881 int bytes_read;
7883 switch (form)
7885 default:
7886 break;
7888 case DW_FORM_ref_addr:
7889 if (dwarf_version == 2)
7891 uvalue = byte_get (data, pointer_size);
7892 data += pointer_size;
7894 else if (dwarf_version == 3)
7896 uvalue = byte_get (data, offset_size);
7897 data += offset_size;
7899 else
7901 error (_("Internal error: DWARF version is not 2 or 3.\n"));
7903 break;
7905 case DW_FORM_addr:
7906 uvalue = byte_get (data, pointer_size);
7907 data += pointer_size;
7908 break;
7910 case DW_FORM_strp:
7911 uvalue = byte_get (data, offset_size);
7912 data += offset_size;
7913 break;
7915 case DW_FORM_ref1:
7916 case DW_FORM_flag:
7917 case DW_FORM_data1:
7918 uvalue = byte_get (data++, 1);
7919 break;
7921 case DW_FORM_ref2:
7922 case DW_FORM_data2:
7923 uvalue = byte_get (data, 2);
7924 data += 2;
7925 break;
7927 case DW_FORM_ref4:
7928 case DW_FORM_data4:
7929 uvalue = byte_get (data, 4);
7930 data += 4;
7931 break;
7933 case DW_FORM_sdata:
7934 uvalue = read_leb128 (data, & bytes_read, 1);
7935 data += bytes_read;
7936 break;
7938 case DW_FORM_ref_udata:
7939 case DW_FORM_udata:
7940 uvalue = read_leb128 (data, & bytes_read, 0);
7941 data += bytes_read;
7942 break;
7944 case DW_FORM_indirect:
7945 form = read_leb128 (data, & bytes_read, 0);
7946 data += bytes_read;
7947 printf (" %s", get_FORM_name (form));
7948 return read_and_display_attr_value (attribute, form, data, cu_offset,
7949 pointer_size, offset_size,
7950 dwarf_version);
7953 switch (form)
7955 case DW_FORM_ref_addr:
7956 printf (" <#%lx>", uvalue);
7957 break;
7959 case DW_FORM_ref1:
7960 case DW_FORM_ref2:
7961 case DW_FORM_ref4:
7962 case DW_FORM_ref_udata:
7963 printf (" <%lx>", uvalue + cu_offset);
7964 break;
7966 case DW_FORM_addr:
7967 printf (" %#lx", uvalue);
7969 case DW_FORM_flag:
7970 case DW_FORM_data1:
7971 case DW_FORM_data2:
7972 case DW_FORM_data4:
7973 case DW_FORM_sdata:
7974 case DW_FORM_udata:
7975 printf (" %ld", uvalue);
7976 break;
7978 case DW_FORM_ref8:
7979 case DW_FORM_data8:
7980 uvalue = byte_get (data, 4);
7981 printf (" %lx", uvalue);
7982 printf (" %lx", (unsigned long) byte_get (data + 4, 4));
7983 data += 8;
7984 break;
7986 case DW_FORM_string:
7987 printf (" %s", data);
7988 data += strlen ((char *) data) + 1;
7989 break;
7991 case DW_FORM_block:
7992 uvalue = read_leb128 (data, & bytes_read, 0);
7993 block_start = data + bytes_read;
7994 data = display_block (block_start, uvalue);
7995 break;
7997 case DW_FORM_block1:
7998 uvalue = byte_get (data, 1);
7999 block_start = data + 1;
8000 data = display_block (block_start, uvalue);
8001 break;
8003 case DW_FORM_block2:
8004 uvalue = byte_get (data, 2);
8005 block_start = data + 2;
8006 data = display_block (block_start, uvalue);
8007 break;
8009 case DW_FORM_block4:
8010 uvalue = byte_get (data, 4);
8011 block_start = data + 4;
8012 data = display_block (block_start, uvalue);
8013 break;
8015 case DW_FORM_strp:
8016 printf (_(" (indirect string, offset: 0x%lx): %s"),
8017 uvalue, fetch_indirect_string (uvalue));
8018 break;
8020 case DW_FORM_indirect:
8021 /* Handled above. */
8022 break;
8024 default:
8025 warn (_("Unrecognized form: %d\n"), form);
8026 break;
8029 /* For some attributes we can display futher information. */
8031 printf ("\t");
8033 switch (attribute)
8035 case DW_AT_inline:
8036 switch (uvalue)
8038 case DW_INL_not_inlined:
8039 printf (_("(not inlined)"));
8040 break;
8041 case DW_INL_inlined:
8042 printf (_("(inlined)"));
8043 break;
8044 case DW_INL_declared_not_inlined:
8045 printf (_("(declared as inline but ignored)"));
8046 break;
8047 case DW_INL_declared_inlined:
8048 printf (_("(declared as inline and inlined)"));
8049 break;
8050 default:
8051 printf (_(" (Unknown inline attribute value: %lx)"), uvalue);
8052 break;
8054 break;
8056 case DW_AT_language:
8057 switch (uvalue)
8059 case DW_LANG_C: printf ("(non-ANSI C)"); break;
8060 case DW_LANG_C89: printf ("(ANSI C)"); break;
8061 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
8062 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
8063 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
8064 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
8065 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
8066 case DW_LANG_Ada83: printf ("(Ada)"); break;
8067 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
8068 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
8069 /* DWARF 2.1 values. */
8070 case DW_LANG_C99: printf ("(ANSI C99)"); break;
8071 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
8072 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
8073 /* MIPS extension. */
8074 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
8075 /* UPC extension. */
8076 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
8077 default:
8078 printf ("(Unknown: %lx)", uvalue);
8079 break;
8081 break;
8083 case DW_AT_encoding:
8084 switch (uvalue)
8086 case DW_ATE_void: printf ("(void)"); break;
8087 case DW_ATE_address: printf ("(machine address)"); break;
8088 case DW_ATE_boolean: printf ("(boolean)"); break;
8089 case DW_ATE_complex_float: printf ("(complex float)"); break;
8090 case DW_ATE_float: printf ("(float)"); break;
8091 case DW_ATE_signed: printf ("(signed)"); break;
8092 case DW_ATE_signed_char: printf ("(signed char)"); break;
8093 case DW_ATE_unsigned: printf ("(unsigned)"); break;
8094 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
8095 /* DWARF 2.1 value. */
8096 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
8097 default:
8098 if (uvalue >= DW_ATE_lo_user
8099 && uvalue <= DW_ATE_hi_user)
8100 printf ("(user defined type)");
8101 else
8102 printf ("(unknown type)");
8103 break;
8105 break;
8107 case DW_AT_accessibility:
8108 switch (uvalue)
8110 case DW_ACCESS_public: printf ("(public)"); break;
8111 case DW_ACCESS_protected: printf ("(protected)"); break;
8112 case DW_ACCESS_private: printf ("(private)"); break;
8113 default:
8114 printf ("(unknown accessibility)");
8115 break;
8117 break;
8119 case DW_AT_visibility:
8120 switch (uvalue)
8122 case DW_VIS_local: printf ("(local)"); break;
8123 case DW_VIS_exported: printf ("(exported)"); break;
8124 case DW_VIS_qualified: printf ("(qualified)"); break;
8125 default: printf ("(unknown visibility)"); break;
8127 break;
8129 case DW_AT_virtuality:
8130 switch (uvalue)
8132 case DW_VIRTUALITY_none: printf ("(none)"); break;
8133 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
8134 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
8135 default: printf ("(unknown virtuality)"); break;
8137 break;
8139 case DW_AT_identifier_case:
8140 switch (uvalue)
8142 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
8143 case DW_ID_up_case: printf ("(up_case)"); break;
8144 case DW_ID_down_case: printf ("(down_case)"); break;
8145 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
8146 default: printf ("(unknown case)"); break;
8148 break;
8150 case DW_AT_calling_convention:
8151 switch (uvalue)
8153 case DW_CC_normal: printf ("(normal)"); break;
8154 case DW_CC_program: printf ("(program)"); break;
8155 case DW_CC_nocall: printf ("(nocall)"); break;
8156 default:
8157 if (uvalue >= DW_CC_lo_user
8158 && uvalue <= DW_CC_hi_user)
8159 printf ("(user defined)");
8160 else
8161 printf ("(unknown convention)");
8163 break;
8165 case DW_AT_ordering:
8166 switch (uvalue)
8168 case -1: printf ("(undefined)"); break;
8169 case 0: printf ("(row major)"); break;
8170 case 1: printf ("(column major)"); break;
8172 break;
8174 case DW_AT_frame_base:
8175 case DW_AT_location:
8176 case DW_AT_data_member_location:
8177 case DW_AT_vtable_elem_location:
8178 case DW_AT_allocated:
8179 case DW_AT_associated:
8180 case DW_AT_data_location:
8181 case DW_AT_stride:
8182 case DW_AT_upper_bound:
8183 case DW_AT_lower_bound:
8184 if (block_start)
8186 printf ("(");
8187 decode_location_expression (block_start, pointer_size, uvalue);
8188 printf (")");
8190 else if (form == DW_FORM_data4 || form == DW_FORM_data8)
8192 printf ("(");
8193 printf ("location list");
8194 printf (")");
8196 break;
8198 default:
8199 break;
8202 return data;
8205 static unsigned char *
8206 read_and_display_attr (attribute, form, data, cu_offset, pointer_size,
8207 offset_size, dwarf_version)
8208 unsigned long attribute;
8209 unsigned long form;
8210 unsigned char *data;
8211 unsigned long cu_offset;
8212 unsigned long pointer_size;
8213 unsigned long offset_size;
8214 int dwarf_version;
8216 printf (" %-18s:", get_AT_name (attribute));
8217 data = read_and_display_attr_value (attribute, form, data, cu_offset,
8218 pointer_size, offset_size, dwarf_version);
8219 printf ("\n");
8220 return data;
8223 static int
8224 display_debug_info (section, start, file)
8225 Elf_Internal_Shdr *section;
8226 unsigned char *start;
8227 FILE *file;
8229 unsigned char *end = start + section->sh_size;
8230 unsigned char *section_begin = start;
8232 printf (_("The section %s contains:\n\n"), SECTION_NAME (section));
8234 load_debug_str (file);
8235 load_debug_loc (file);
8237 while (start < end)
8239 DWARF2_Internal_CompUnit compunit;
8240 Elf_Internal_Shdr *relsec;
8241 unsigned char *hdrptr;
8242 unsigned char *cu_abbrev_offset_ptr;
8243 unsigned char *tags;
8244 unsigned int i;
8245 int level;
8246 unsigned long cu_offset;
8247 int offset_size;
8248 int initial_length_size;
8250 hdrptr = start;
8252 compunit.cu_length = byte_get (hdrptr, 4);
8253 hdrptr += 4;
8255 if (compunit.cu_length == 0xffffffff)
8257 compunit.cu_length = byte_get (hdrptr, 8);
8258 hdrptr += 8;
8259 offset_size = 8;
8260 initial_length_size = 12;
8262 else
8264 offset_size = 4;
8265 initial_length_size = 4;
8268 compunit.cu_version = byte_get (hdrptr, 2);
8269 hdrptr += 2;
8271 cu_abbrev_offset_ptr = hdrptr;
8272 compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
8273 hdrptr += offset_size;
8275 compunit.cu_pointer_size = byte_get (hdrptr, 1);
8276 hdrptr += 1;
8278 /* Check for RELA relocations in the
8279 abbrev_offset address, and apply them. */
8280 for (relsec = section_headers;
8281 relsec < section_headers + elf_header.e_shnum;
8282 ++relsec)
8284 unsigned long nrelas;
8285 Elf_Internal_Rela *rela, *rp;
8286 Elf_Internal_Shdr *symsec;
8287 Elf_Internal_Sym *symtab;
8288 Elf_Internal_Sym *sym;
8290 if (relsec->sh_type != SHT_RELA
8291 || SECTION_HEADER (relsec->sh_info) != section
8292 || relsec->sh_size == 0)
8293 continue;
8295 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
8296 & rela, & nrelas))
8297 return 0;
8299 symsec = SECTION_HEADER (relsec->sh_link);
8300 symtab = GET_ELF_SYMBOLS (file, symsec);
8302 for (rp = rela; rp < rela + nrelas; ++rp)
8304 if (rp->r_offset
8305 != (bfd_vma) (cu_abbrev_offset_ptr - section_begin))
8306 continue;
8308 if (is_32bit_elf)
8310 sym = symtab + ELF32_R_SYM (rp->r_info);
8312 if (ELF32_R_SYM (rp->r_info) != 0
8313 && ELF32_ST_TYPE (sym->st_info) != STT_SECTION)
8315 warn (_("Skipping unexpected symbol type %u\n"),
8316 ELF32_ST_TYPE (sym->st_info));
8317 continue;
8320 else
8322 sym = symtab + ELF64_R_SYM (rp->r_info);
8324 if (ELF64_R_SYM (rp->r_info) != 0
8325 && ELF64_ST_TYPE (sym->st_info) != STT_SECTION)
8327 warn (_("Skipping unexpected symbol type %u\n"),
8328 ELF64_ST_TYPE (sym->st_info));
8329 continue;
8333 compunit.cu_abbrev_offset = rp->r_addend;
8334 break;
8337 free (rela);
8338 break;
8341 tags = hdrptr;
8342 cu_offset = start - section_begin;
8343 start += compunit.cu_length + initial_length_size;
8345 printf (_(" Compilation Unit @ %lx:\n"), cu_offset);
8346 printf (_(" Length: %ld\n"), compunit.cu_length);
8347 printf (_(" Version: %d\n"), compunit.cu_version);
8348 printf (_(" Abbrev Offset: %ld\n"), compunit.cu_abbrev_offset);
8349 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
8351 if (compunit.cu_version != 2 && compunit.cu_version != 3)
8353 warn (_("Only version 2 and 3 DWARF debug information is currently supported.\n"));
8354 continue;
8357 free_abbrevs ();
8359 /* Read in the abbrevs used by this compilation unit. */
8361 Elf_Internal_Shdr *sec;
8362 unsigned char *begin;
8364 /* Locate the .debug_abbrev section and process it. */
8365 for (i = 0, sec = section_headers;
8366 i < elf_header.e_shnum;
8367 i++, sec++)
8368 if (strcmp (SECTION_NAME (sec), ".debug_abbrev") == 0)
8369 break;
8371 if (i == elf_header.e_shnum || sec->sh_size == 0)
8373 warn (_("Unable to locate .debug_abbrev section!\n"));
8374 return 0;
8377 begin = ((unsigned char *)
8378 get_data (NULL, file, sec->sh_offset, sec->sh_size,
8379 _("debug_abbrev section data")));
8380 if (!begin)
8381 return 0;
8383 process_abbrev_section (begin + compunit.cu_abbrev_offset,
8384 begin + sec->sh_size);
8386 free (begin);
8389 level = 0;
8390 while (tags < start)
8392 int bytes_read;
8393 unsigned long abbrev_number;
8394 abbrev_entry *entry;
8395 abbrev_attr *attr;
8397 abbrev_number = read_leb128 (tags, & bytes_read, 0);
8398 tags += bytes_read;
8400 /* A null DIE marks the end of a list of children. */
8401 if (abbrev_number == 0)
8403 --level;
8404 continue;
8407 /* Scan through the abbreviation list until we reach the
8408 correct entry. */
8409 for (entry = first_abbrev;
8410 entry && entry->entry != abbrev_number;
8411 entry = entry->next)
8412 continue;
8414 if (entry == NULL)
8416 warn (_("Unable to locate entry %lu in the abbreviation table\n"),
8417 abbrev_number);
8418 return 0;
8421 printf (_(" <%d><%lx>: Abbrev Number: %lu (%s)\n"),
8422 level,
8423 (unsigned long) (tags - section_begin - bytes_read),
8424 abbrev_number,
8425 get_TAG_name (entry->tag));
8427 for (attr = entry->first_attr; attr; attr = attr->next)
8428 tags = read_and_display_attr (attr->attribute,
8429 attr->form,
8430 tags, cu_offset,
8431 compunit.cu_pointer_size,
8432 offset_size,
8433 compunit.cu_version);
8435 if (entry->children)
8436 ++level;
8440 free_debug_str ();
8441 free_debug_loc ();
8443 printf ("\n");
8445 return 1;
8448 static int
8449 display_debug_aranges (section, start, file)
8450 Elf_Internal_Shdr *section;
8451 unsigned char *start;
8452 FILE *file ATTRIBUTE_UNUSED;
8454 unsigned char *end = start + section->sh_size;
8456 printf (_("The section %s contains:\n\n"), SECTION_NAME (section));
8458 while (start < end)
8460 unsigned char *hdrptr;
8461 DWARF2_Internal_ARange arange;
8462 unsigned char *ranges;
8463 unsigned long length;
8464 unsigned long address;
8465 int excess;
8466 int offset_size;
8467 int initial_length_size;
8469 hdrptr = start;
8471 arange.ar_length = byte_get (hdrptr, 4);
8472 hdrptr += 4;
8474 if (arange.ar_length == 0xffffffff)
8476 arange.ar_length = byte_get (hdrptr, 8);
8477 hdrptr += 8;
8478 offset_size = 8;
8479 initial_length_size = 12;
8481 else
8483 offset_size = 4;
8484 initial_length_size = 4;
8487 arange.ar_version = byte_get (hdrptr, 2);
8488 hdrptr += 2;
8490 arange.ar_info_offset = byte_get (hdrptr, offset_size);
8491 hdrptr += offset_size;
8493 arange.ar_pointer_size = byte_get (hdrptr, 1);
8494 hdrptr += 1;
8496 arange.ar_segment_size = byte_get (hdrptr, 1);
8497 hdrptr += 1;
8499 if (arange.ar_version != 2 && arange.ar_version != 3)
8501 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
8502 break;
8505 printf (_(" Length: %ld\n"), arange.ar_length);
8506 printf (_(" Version: %d\n"), arange.ar_version);
8507 printf (_(" Offset into .debug_info: %lx\n"), arange.ar_info_offset);
8508 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
8509 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
8511 printf (_("\n Address Length\n"));
8513 ranges = hdrptr;
8515 /* Must pad to an alignment boundary that is twice the pointer size. */
8516 excess = (hdrptr - start) % (2 * arange.ar_pointer_size);
8517 if (excess)
8518 ranges += (2 * arange.ar_pointer_size) - excess;
8520 for (;;)
8522 address = byte_get (ranges, arange.ar_pointer_size);
8524 ranges += arange.ar_pointer_size;
8526 length = byte_get (ranges, arange.ar_pointer_size);
8528 ranges += arange.ar_pointer_size;
8530 /* A pair of zeros marks the end of the list. */
8531 if (address == 0 && length == 0)
8532 break;
8534 printf (" %8.8lx %lu\n", address, length);
8537 start += arange.ar_length + initial_length_size;
8540 printf ("\n");
8542 return 1;
8545 typedef struct Frame_Chunk
8547 struct Frame_Chunk *next;
8548 unsigned char *chunk_start;
8549 int ncols;
8550 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8551 short int *col_type;
8552 int *col_offset;
8553 char *augmentation;
8554 unsigned int code_factor;
8555 int data_factor;
8556 unsigned long pc_begin;
8557 unsigned long pc_range;
8558 int cfa_reg;
8559 int cfa_offset;
8560 int ra;
8561 unsigned char fde_encoding;
8563 Frame_Chunk;
8565 /* A marker for a col_type that means this column was never referenced
8566 in the frame info. */
8567 #define DW_CFA_unreferenced (-1)
8569 static void frame_need_space PARAMS ((Frame_Chunk *, int));
8570 static void frame_display_row PARAMS ((Frame_Chunk *, int *, int *));
8571 static int size_of_encoded_value PARAMS ((int));
8573 static void
8574 frame_need_space (fc, reg)
8575 Frame_Chunk *fc;
8576 int reg;
8578 int prev = fc->ncols;
8580 if (reg < fc->ncols)
8581 return;
8583 fc->ncols = reg + 1;
8584 fc->col_type = (short int *) xrealloc (fc->col_type,
8585 fc->ncols * sizeof (short int));
8586 fc->col_offset = (int *) xrealloc (fc->col_offset,
8587 fc->ncols * sizeof (int));
8589 while (prev < fc->ncols)
8591 fc->col_type[prev] = DW_CFA_unreferenced;
8592 fc->col_offset[prev] = 0;
8593 prev++;
8597 static void
8598 frame_display_row (fc, need_col_headers, max_regs)
8599 Frame_Chunk *fc;
8600 int *need_col_headers;
8601 int *max_regs;
8603 int r;
8604 char tmp[100];
8606 if (*max_regs < fc->ncols)
8607 *max_regs = fc->ncols;
8609 if (*need_col_headers)
8611 *need_col_headers = 0;
8613 printf (" LOC CFA ");
8615 for (r = 0; r < *max_regs; r++)
8616 if (fc->col_type[r] != DW_CFA_unreferenced)
8618 if (r == fc->ra)
8619 printf ("ra ");
8620 else
8621 printf ("r%-4d", r);
8624 printf ("\n");
8627 printf ("%08lx ", fc->pc_begin);
8628 sprintf (tmp, "r%d%+d", fc->cfa_reg, fc->cfa_offset);
8629 printf ("%-8s ", tmp);
8631 for (r = 0; r < fc->ncols; r++)
8633 if (fc->col_type[r] != DW_CFA_unreferenced)
8635 switch (fc->col_type[r])
8637 case DW_CFA_undefined:
8638 strcpy (tmp, "u");
8639 break;
8640 case DW_CFA_same_value:
8641 strcpy (tmp, "s");
8642 break;
8643 case DW_CFA_offset:
8644 sprintf (tmp, "c%+d", fc->col_offset[r]);
8645 break;
8646 case DW_CFA_register:
8647 sprintf (tmp, "r%d", fc->col_offset[r]);
8648 break;
8649 default:
8650 strcpy (tmp, "n/a");
8651 break;
8653 printf ("%-5s", tmp);
8656 printf ("\n");
8659 static int
8660 size_of_encoded_value (encoding)
8661 int encoding;
8663 switch (encoding & 0x7)
8665 default: /* ??? */
8666 case 0: return is_32bit_elf ? 4 : 8;
8667 case 2: return 2;
8668 case 3: return 4;
8669 case 4: return 8;
8673 #define GET(N) byte_get (start, N); start += N
8674 #define LEB() read_leb128 (start, & length_return, 0); start += length_return
8675 #define SLEB() read_leb128 (start, & length_return, 1); start += length_return
8677 static int
8678 display_debug_frames (section, start, file)
8679 Elf_Internal_Shdr *section;
8680 unsigned char *start;
8681 FILE *file ATTRIBUTE_UNUSED;
8683 unsigned char *end = start + section->sh_size;
8684 unsigned char *section_start = start;
8685 Frame_Chunk *chunks = 0;
8686 Frame_Chunk *remembered_state = 0;
8687 Frame_Chunk *rs;
8688 int is_eh = (strcmp (SECTION_NAME (section), ".eh_frame") == 0);
8689 int length_return;
8690 int max_regs = 0;
8691 int addr_size = is_32bit_elf ? 4 : 8;
8693 printf (_("The section %s contains:\n"), SECTION_NAME (section));
8695 while (start < end)
8697 unsigned char *saved_start;
8698 unsigned char *block_end;
8699 unsigned long length;
8700 unsigned long cie_id;
8701 Frame_Chunk *fc;
8702 Frame_Chunk *cie;
8703 int need_col_headers = 1;
8704 unsigned char *augmentation_data = NULL;
8705 unsigned long augmentation_data_len = 0;
8706 int encoded_ptr_size = addr_size;
8707 int offset_size;
8708 int initial_length_size;
8710 saved_start = start;
8711 length = byte_get (start, 4); start += 4;
8713 if (length == 0)
8715 printf ("\n%08lx ZERO terminator\n\n",
8716 (unsigned long)(saved_start - section_start));
8717 return 1;
8720 if (length == 0xffffffff)
8722 length = byte_get (start, 8);
8723 start += 8;
8724 offset_size = 8;
8725 initial_length_size = 12;
8727 else
8729 offset_size = 4;
8730 initial_length_size = 4;
8733 block_end = saved_start + length + initial_length_size;
8734 cie_id = byte_get (start, offset_size); start += offset_size;
8736 if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
8738 int version;
8740 fc = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
8741 memset (fc, 0, sizeof (Frame_Chunk));
8743 fc->next = chunks;
8744 chunks = fc;
8745 fc->chunk_start = saved_start;
8746 fc->ncols = 0;
8747 fc->col_type = (short int *) xmalloc (sizeof (short int));
8748 fc->col_offset = (int *) xmalloc (sizeof (int));
8749 frame_need_space (fc, max_regs-1);
8751 version = *start++;
8753 fc->augmentation = start;
8754 start = strchr (start, '\0') + 1;
8756 if (fc->augmentation[0] == 'z')
8758 fc->code_factor = LEB ();
8759 fc->data_factor = SLEB ();
8760 fc->ra = byte_get (start, 1); start += 1;
8761 augmentation_data_len = LEB ();
8762 augmentation_data = start;
8763 start += augmentation_data_len;
8765 else if (strcmp (fc->augmentation, "eh") == 0)
8767 start += addr_size;
8768 fc->code_factor = LEB ();
8769 fc->data_factor = SLEB ();
8770 fc->ra = byte_get (start, 1); start += 1;
8772 else
8774 fc->code_factor = LEB ();
8775 fc->data_factor = SLEB ();
8776 fc->ra = byte_get (start, 1); start += 1;
8778 cie = fc;
8780 if (do_debug_frames_interp)
8781 printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
8782 (unsigned long)(saved_start - section_start), length, cie_id,
8783 fc->augmentation, fc->code_factor, fc->data_factor,
8784 fc->ra);
8785 else
8787 printf ("\n%08lx %08lx %08lx CIE\n",
8788 (unsigned long)(saved_start - section_start), length, cie_id);
8789 printf (" Version: %d\n", version);
8790 printf (" Augmentation: \"%s\"\n", fc->augmentation);
8791 printf (" Code alignment factor: %u\n", fc->code_factor);
8792 printf (" Data alignment factor: %d\n", fc->data_factor);
8793 printf (" Return address column: %d\n", fc->ra);
8795 if (augmentation_data_len)
8797 unsigned long i;
8798 printf (" Augmentation data: ");
8799 for (i = 0; i < augmentation_data_len; ++i)
8800 printf (" %02x", augmentation_data[i]);
8801 putchar ('\n');
8803 putchar ('\n');
8806 if (augmentation_data_len)
8808 unsigned char *p, *q;
8809 p = fc->augmentation + 1;
8810 q = augmentation_data;
8812 while (1)
8814 if (*p == 'L')
8815 q++;
8816 else if (*p == 'P')
8817 q += 1 + size_of_encoded_value (*q);
8818 else if (*p == 'R')
8819 fc->fde_encoding = *q++;
8820 else
8821 break;
8822 p++;
8825 if (fc->fde_encoding)
8826 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
8829 frame_need_space (fc, fc->ra);
8831 else
8833 unsigned char *look_for;
8834 static Frame_Chunk fde_fc;
8836 fc = & fde_fc;
8837 memset (fc, 0, sizeof (Frame_Chunk));
8839 look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
8841 for (cie = chunks; cie ; cie = cie->next)
8842 if (cie->chunk_start == look_for)
8843 break;
8845 if (!cie)
8847 warn ("Invalid CIE pointer %08lx in FDE at %08lx\n",
8848 cie_id, saved_start);
8849 start = block_end;
8850 fc->ncols = 0;
8851 fc->col_type = (short int *) xmalloc (sizeof (short int));
8852 fc->col_offset = (int *) xmalloc (sizeof (int));
8853 frame_need_space (fc, max_regs - 1);
8854 cie = fc;
8855 fc->augmentation = "";
8856 fc->fde_encoding = 0;
8858 else
8860 fc->ncols = cie->ncols;
8861 fc->col_type = (short int *) xmalloc (fc->ncols * sizeof (short int));
8862 fc->col_offset = (int *) xmalloc (fc->ncols * sizeof (int));
8863 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
8864 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
8865 fc->augmentation = cie->augmentation;
8866 fc->code_factor = cie->code_factor;
8867 fc->data_factor = cie->data_factor;
8868 fc->cfa_reg = cie->cfa_reg;
8869 fc->cfa_offset = cie->cfa_offset;
8870 fc->ra = cie->ra;
8871 frame_need_space (fc, max_regs-1);
8872 fc->fde_encoding = cie->fde_encoding;
8875 if (fc->fde_encoding)
8876 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
8878 fc->pc_begin = byte_get (start, encoded_ptr_size);
8879 if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
8880 fc->pc_begin += section->sh_addr + (start - section_start);
8881 start += encoded_ptr_size;
8882 fc->pc_range = byte_get (start, encoded_ptr_size);
8883 start += encoded_ptr_size;
8885 if (cie->augmentation[0] == 'z')
8887 augmentation_data_len = LEB ();
8888 augmentation_data = start;
8889 start += augmentation_data_len;
8892 printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
8893 (unsigned long)(saved_start - section_start), length, cie_id,
8894 (unsigned long)(cie->chunk_start - section_start),
8895 fc->pc_begin, fc->pc_begin + fc->pc_range);
8896 if (! do_debug_frames_interp && augmentation_data_len)
8898 unsigned long i;
8899 printf (" Augmentation data: ");
8900 for (i = 0; i < augmentation_data_len; ++i)
8901 printf (" %02x", augmentation_data[i]);
8902 putchar ('\n');
8903 putchar ('\n');
8907 /* At this point, fc is the current chunk, cie (if any) is set, and we're
8908 about to interpret instructions for the chunk. */
8910 if (do_debug_frames_interp)
8912 /* Start by making a pass over the chunk, allocating storage
8913 and taking note of what registers are used. */
8914 unsigned char *tmp = start;
8916 while (start < block_end)
8918 unsigned op, opa;
8919 unsigned long reg;
8921 op = *start++;
8922 opa = op & 0x3f;
8923 if (op & 0xc0)
8924 op &= 0xc0;
8926 /* Warning: if you add any more cases to this switch, be
8927 sure to add them to the corresponding switch below. */
8928 switch (op)
8930 case DW_CFA_advance_loc:
8931 break;
8932 case DW_CFA_offset:
8933 LEB ();
8934 frame_need_space (fc, opa);
8935 fc->col_type[opa] = DW_CFA_undefined;
8936 break;
8937 case DW_CFA_restore:
8938 frame_need_space (fc, opa);
8939 fc->col_type[opa] = DW_CFA_undefined;
8940 break;
8941 case DW_CFA_set_loc:
8942 start += encoded_ptr_size;
8943 break;
8944 case DW_CFA_advance_loc1:
8945 start += 1;
8946 break;
8947 case DW_CFA_advance_loc2:
8948 start += 2;
8949 break;
8950 case DW_CFA_advance_loc4:
8951 start += 4;
8952 break;
8953 case DW_CFA_offset_extended:
8954 reg = LEB (); LEB ();
8955 frame_need_space (fc, reg);
8956 fc->col_type[reg] = DW_CFA_undefined;
8957 break;
8958 case DW_CFA_restore_extended:
8959 reg = LEB ();
8960 frame_need_space (fc, reg);
8961 fc->col_type[reg] = DW_CFA_undefined;
8962 break;
8963 case DW_CFA_undefined:
8964 reg = LEB ();
8965 frame_need_space (fc, reg);
8966 fc->col_type[reg] = DW_CFA_undefined;
8967 break;
8968 case DW_CFA_same_value:
8969 reg = LEB ();
8970 frame_need_space (fc, reg);
8971 fc->col_type[reg] = DW_CFA_undefined;
8972 break;
8973 case DW_CFA_register:
8974 reg = LEB (); LEB ();
8975 frame_need_space (fc, reg);
8976 fc->col_type[reg] = DW_CFA_undefined;
8977 break;
8978 case DW_CFA_def_cfa:
8979 LEB (); LEB ();
8980 break;
8981 case DW_CFA_def_cfa_register:
8982 LEB ();
8983 break;
8984 case DW_CFA_def_cfa_offset:
8985 LEB ();
8986 break;
8987 case DW_CFA_offset_extended_sf:
8988 reg = LEB (); SLEB ();
8989 frame_need_space (fc, reg);
8990 fc->col_type[reg] = DW_CFA_undefined;
8991 break;
8992 case DW_CFA_def_cfa_sf:
8993 LEB (); SLEB ();
8994 break;
8995 case DW_CFA_def_cfa_offset_sf:
8996 SLEB ();
8997 break;
8998 case DW_CFA_GNU_args_size:
8999 LEB ();
9000 break;
9001 case DW_CFA_GNU_negative_offset_extended:
9002 reg = LEB (); LEB ();
9003 frame_need_space (fc, reg);
9004 fc->col_type[reg] = DW_CFA_undefined;
9006 default:
9007 break;
9010 start = tmp;
9013 /* Now we know what registers are used, make a second pass over
9014 the chunk, this time actually printing out the info. */
9016 while (start < block_end)
9018 unsigned op, opa;
9019 unsigned long ul, reg, roffs;
9020 long l, ofs;
9021 bfd_vma vma;
9023 op = *start++;
9024 opa = op & 0x3f;
9025 if (op & 0xc0)
9026 op &= 0xc0;
9028 /* Warning: if you add any more cases to this switch, be
9029 sure to add them to the corresponding switch above. */
9030 switch (op)
9032 case DW_CFA_advance_loc:
9033 if (do_debug_frames_interp)
9034 frame_display_row (fc, &need_col_headers, &max_regs);
9035 else
9036 printf (" DW_CFA_advance_loc: %d to %08lx\n",
9037 opa * fc->code_factor,
9038 fc->pc_begin + opa * fc->code_factor);
9039 fc->pc_begin += opa * fc->code_factor;
9040 break;
9042 case DW_CFA_offset:
9043 roffs = LEB ();
9044 if (! do_debug_frames_interp)
9045 printf (" DW_CFA_offset: r%d at cfa%+ld\n",
9046 opa, roffs * fc->data_factor);
9047 fc->col_type[opa] = DW_CFA_offset;
9048 fc->col_offset[opa] = roffs * fc->data_factor;
9049 break;
9051 case DW_CFA_restore:
9052 if (! do_debug_frames_interp)
9053 printf (" DW_CFA_restore: r%d\n", opa);
9054 fc->col_type[opa] = cie->col_type[opa];
9055 fc->col_offset[opa] = cie->col_offset[opa];
9056 break;
9058 case DW_CFA_set_loc:
9059 vma = byte_get (start, encoded_ptr_size);
9060 if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
9061 vma += section->sh_addr + (start - section_start);
9062 start += encoded_ptr_size;
9063 if (do_debug_frames_interp)
9064 frame_display_row (fc, &need_col_headers, &max_regs);
9065 else
9066 printf (" DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
9067 fc->pc_begin = vma;
9068 break;
9070 case DW_CFA_advance_loc1:
9071 ofs = byte_get (start, 1); start += 1;
9072 if (do_debug_frames_interp)
9073 frame_display_row (fc, &need_col_headers, &max_regs);
9074 else
9075 printf (" DW_CFA_advance_loc1: %ld to %08lx\n",
9076 ofs * fc->code_factor,
9077 fc->pc_begin + ofs * fc->code_factor);
9078 fc->pc_begin += ofs * fc->code_factor;
9079 break;
9081 case DW_CFA_advance_loc2:
9082 ofs = byte_get (start, 2); start += 2;
9083 if (do_debug_frames_interp)
9084 frame_display_row (fc, &need_col_headers, &max_regs);
9085 else
9086 printf (" DW_CFA_advance_loc2: %ld to %08lx\n",
9087 ofs * fc->code_factor,
9088 fc->pc_begin + ofs * fc->code_factor);
9089 fc->pc_begin += ofs * fc->code_factor;
9090 break;
9092 case DW_CFA_advance_loc4:
9093 ofs = byte_get (start, 4); start += 4;
9094 if (do_debug_frames_interp)
9095 frame_display_row (fc, &need_col_headers, &max_regs);
9096 else
9097 printf (" DW_CFA_advance_loc4: %ld to %08lx\n",
9098 ofs * fc->code_factor,
9099 fc->pc_begin + ofs * fc->code_factor);
9100 fc->pc_begin += ofs * fc->code_factor;
9101 break;
9103 case DW_CFA_offset_extended:
9104 reg = LEB ();
9105 roffs = LEB ();
9106 if (! do_debug_frames_interp)
9107 printf (" DW_CFA_offset_extended: r%ld at cfa%+ld\n",
9108 reg, roffs * fc->data_factor);
9109 fc->col_type[reg] = DW_CFA_offset;
9110 fc->col_offset[reg] = roffs * fc->data_factor;
9111 break;
9113 case DW_CFA_restore_extended:
9114 reg = LEB ();
9115 if (! do_debug_frames_interp)
9116 printf (" DW_CFA_restore_extended: r%ld\n", reg);
9117 fc->col_type[reg] = cie->col_type[reg];
9118 fc->col_offset[reg] = cie->col_offset[reg];
9119 break;
9121 case DW_CFA_undefined:
9122 reg = LEB ();
9123 if (! do_debug_frames_interp)
9124 printf (" DW_CFA_undefined: r%ld\n", reg);
9125 fc->col_type[reg] = DW_CFA_undefined;
9126 fc->col_offset[reg] = 0;
9127 break;
9129 case DW_CFA_same_value:
9130 reg = LEB ();
9131 if (! do_debug_frames_interp)
9132 printf (" DW_CFA_same_value: r%ld\n", reg);
9133 fc->col_type[reg] = DW_CFA_same_value;
9134 fc->col_offset[reg] = 0;
9135 break;
9137 case DW_CFA_register:
9138 reg = LEB ();
9139 roffs = LEB ();
9140 if (! do_debug_frames_interp)
9141 printf (" DW_CFA_register: r%ld\n", reg);
9142 fc->col_type[reg] = DW_CFA_register;
9143 fc->col_offset[reg] = roffs;
9144 break;
9146 case DW_CFA_remember_state:
9147 if (! do_debug_frames_interp)
9148 printf (" DW_CFA_remember_state\n");
9149 rs = (Frame_Chunk *) xmalloc (sizeof (Frame_Chunk));
9150 rs->ncols = fc->ncols;
9151 rs->col_type = (short int *) xmalloc (rs->ncols * sizeof (short int));
9152 rs->col_offset = (int *) xmalloc (rs->ncols * sizeof (int));
9153 memcpy (rs->col_type, fc->col_type, rs->ncols);
9154 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
9155 rs->next = remembered_state;
9156 remembered_state = rs;
9157 break;
9159 case DW_CFA_restore_state:
9160 if (! do_debug_frames_interp)
9161 printf (" DW_CFA_restore_state\n");
9162 rs = remembered_state;
9163 remembered_state = rs->next;
9164 frame_need_space (fc, rs->ncols-1);
9165 memcpy (fc->col_type, rs->col_type, rs->ncols);
9166 memcpy (fc->col_offset, rs->col_offset, rs->ncols * sizeof (int));
9167 free (rs->col_type);
9168 free (rs->col_offset);
9169 free (rs);
9170 break;
9172 case DW_CFA_def_cfa:
9173 fc->cfa_reg = LEB ();
9174 fc->cfa_offset = LEB ();
9175 if (! do_debug_frames_interp)
9176 printf (" DW_CFA_def_cfa: r%d ofs %d\n",
9177 fc->cfa_reg, fc->cfa_offset);
9178 break;
9180 case DW_CFA_def_cfa_register:
9181 fc->cfa_reg = LEB ();
9182 if (! do_debug_frames_interp)
9183 printf (" DW_CFA_def_cfa_reg: r%d\n", fc->cfa_reg);
9184 break;
9186 case DW_CFA_def_cfa_offset:
9187 fc->cfa_offset = LEB ();
9188 if (! do_debug_frames_interp)
9189 printf (" DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
9190 break;
9192 case DW_CFA_nop:
9193 if (! do_debug_frames_interp)
9194 printf (" DW_CFA_nop\n");
9195 break;
9197 case DW_CFA_offset_extended_sf:
9198 reg = LEB ();
9199 l = SLEB ();
9200 frame_need_space (fc, reg);
9201 if (! do_debug_frames_interp)
9202 printf (" DW_CFA_offset_extended_sf: r%ld at cfa%+ld\n",
9203 reg, l * fc->data_factor);
9204 fc->col_type[reg] = DW_CFA_offset;
9205 fc->col_offset[reg] = l * fc->data_factor;
9206 break;
9208 case DW_CFA_def_cfa_sf:
9209 fc->cfa_reg = LEB ();
9210 fc->cfa_offset = SLEB ();
9211 if (! do_debug_frames_interp)
9212 printf (" DW_CFA_def_cfa_sf: r%d ofs %d\n",
9213 fc->cfa_reg, fc->cfa_offset);
9214 break;
9216 case DW_CFA_def_cfa_offset_sf:
9217 fc->cfa_offset = SLEB ();
9218 if (! do_debug_frames_interp)
9219 printf (" DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
9220 break;
9222 case DW_CFA_GNU_window_save:
9223 if (! do_debug_frames_interp)
9224 printf (" DW_CFA_GNU_window_save\n");
9225 break;
9227 case DW_CFA_GNU_args_size:
9228 ul = LEB ();
9229 if (! do_debug_frames_interp)
9230 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
9231 break;
9233 case DW_CFA_GNU_negative_offset_extended:
9234 reg = LEB ();
9235 l = - LEB ();
9236 frame_need_space (fc, reg);
9237 if (! do_debug_frames_interp)
9238 printf (" DW_CFA_GNU_negative_offset_extended: r%ld at cfa%+ld\n",
9239 reg, l * fc->data_factor);
9240 fc->col_type[reg] = DW_CFA_offset;
9241 fc->col_offset[reg] = l * fc->data_factor;
9242 break;
9244 /* FIXME: How do we handle these? */
9245 case DW_CFA_def_cfa_expression:
9246 fprintf (stderr, "unsupported DW_CFA_def_cfa_expression\n");
9247 start = block_end;
9248 break;
9250 case DW_CFA_expression:
9251 fprintf (stderr, "unsupported DW_CFA_expression\n");
9252 start = block_end;
9253 break;
9255 default:
9256 fprintf (stderr, "unsupported or unknown DW_CFA_%d\n", op);
9257 start = block_end;
9261 if (do_debug_frames_interp)
9262 frame_display_row (fc, &need_col_headers, &max_regs);
9264 start = block_end;
9267 printf ("\n");
9269 return 1;
9272 #undef GET
9273 #undef LEB
9274 #undef SLEB
9276 static int
9277 display_debug_not_supported (section, start, file)
9278 Elf_Internal_Shdr *section;
9279 unsigned char *start ATTRIBUTE_UNUSED;
9280 FILE *file ATTRIBUTE_UNUSED;
9282 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
9283 SECTION_NAME (section));
9285 return 1;
9288 /* Pre-scan the .debug_info section to record the size of address.
9289 When dumping the .debug_line, we use that size information, assuming
9290 that all compilation units have the same address size. */
9291 static int
9292 prescan_debug_info (section, start, file)
9293 Elf_Internal_Shdr *section ATTRIBUTE_UNUSED;
9294 unsigned char *start;
9295 FILE *file ATTRIBUTE_UNUSED;
9297 unsigned long length;
9299 /* Read the first 4 bytes. For a 32-bit DWARF section, this will
9300 be the length. For a 64-bit DWARF section, it'll be the escape
9301 code 0xffffffff followed by an 8 byte length. For the purposes
9302 of this prescan, we don't care about the actual length, but the
9303 presence of the escape bytes does affect the location of the byte
9304 which describes the address size. */
9305 length = byte_get (start, 4);
9307 if (length == 0xffffffff)
9309 /* For 64-bit DWARF, the 1-byte address_size field is 22 bytes
9310 from the start of the section. This is computed as follows:
9312 unit_length: 12 bytes
9313 version: 2 bytes
9314 debug_abbrev_offset: 8 bytes
9315 -----------------------------
9316 Total: 22 bytes */
9318 debug_line_pointer_size = byte_get (start + 22, 1);
9320 else
9322 /* For 32-bit DWARF, the 1-byte address_size field is 10 bytes from
9323 the start of the section:
9324 unit_length: 4 bytes
9325 version: 2 bytes
9326 debug_abbrev_offset: 4 bytes
9327 -----------------------------
9328 Total: 10 bytes */
9330 debug_line_pointer_size = byte_get (start + 10, 1);
9332 return 0;
9335 /* A structure containing the name of a debug section and a pointer
9336 to a function that can decode it. The third field is a prescan
9337 function to be run over the section before displaying any of the
9338 sections. */
9339 struct
9341 const char *const name;
9342 int (*display) PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
9343 int (*prescan) PARAMS ((Elf_Internal_Shdr *, unsigned char *, FILE *));
9345 debug_displays[] =
9347 { ".debug_abbrev", display_debug_abbrev, NULL },
9348 { ".debug_aranges", display_debug_aranges, NULL },
9349 { ".debug_frame", display_debug_frames, NULL },
9350 { ".debug_info", display_debug_info, prescan_debug_info },
9351 { ".debug_line", display_debug_lines, NULL },
9352 { ".debug_pubnames", display_debug_pubnames, NULL },
9353 { ".eh_frame", display_debug_frames, NULL },
9354 { ".debug_macinfo", display_debug_macinfo, NULL },
9355 { ".debug_str", display_debug_str, NULL },
9356 { ".debug_loc", display_debug_loc, NULL },
9357 { ".debug_pubtypes", display_debug_not_supported, NULL },
9358 { ".debug_ranges", display_debug_not_supported, NULL },
9359 { ".debug_static_func", display_debug_not_supported, NULL },
9360 { ".debug_static_vars", display_debug_not_supported, NULL },
9361 { ".debug_types", display_debug_not_supported, NULL },
9362 { ".debug_weaknames", display_debug_not_supported, NULL }
9365 static int
9366 display_debug_section (section, file)
9367 Elf_Internal_Shdr *section;
9368 FILE *file;
9370 char *name = SECTION_NAME (section);
9371 bfd_size_type length;
9372 unsigned char *start;
9373 int i;
9375 length = section->sh_size;
9376 if (length == 0)
9378 printf (_("\nSection '%s' has no debugging data.\n"), name);
9379 return 0;
9382 start = (unsigned char *) get_data (NULL, file, section->sh_offset, length,
9383 _("debug section data"));
9384 if (!start)
9385 return 0;
9387 /* See if we know how to display the contents of this section. */
9388 if (strncmp (name, ".gnu.linkonce.wi.", 17) == 0)
9389 name = ".debug_info";
9391 for (i = NUM_ELEM (debug_displays); i--;)
9392 if (strcmp (debug_displays[i].name, name) == 0)
9394 debug_displays[i].display (section, start, file);
9395 break;
9398 if (i == -1)
9399 printf (_("Unrecognized debug section: %s\n"), name);
9401 free (start);
9403 /* If we loaded in the abbrev section at some point,
9404 we must release it here. */
9405 free_abbrevs ();
9407 return 1;
9410 static int
9411 process_section_contents (file)
9412 FILE *file;
9414 Elf_Internal_Shdr *section;
9415 unsigned int i;
9417 if (! do_dump)
9418 return 1;
9420 /* Pre-scan the debug sections to find some debug information not
9421 present in some of them. For the .debug_line, we must find out the
9422 size of address (specified in .debug_info and .debug_aranges). */
9423 for (i = 0, section = section_headers;
9424 i < elf_header.e_shnum && i < num_dump_sects;
9425 i++, section++)
9427 char *name = SECTION_NAME (section);
9428 int j;
9430 if (section->sh_size == 0)
9431 continue;
9433 /* See if there is some pre-scan operation for this section. */
9434 for (j = NUM_ELEM (debug_displays); j--;)
9435 if (strcmp (debug_displays[j].name, name) == 0)
9437 if (debug_displays[j].prescan != NULL)
9439 bfd_size_type length;
9440 unsigned char *start;
9442 length = section->sh_size;
9443 start = ((unsigned char *)
9444 get_data (NULL, file, section->sh_offset, length,
9445 _("debug section data")));
9446 if (!start)
9447 return 0;
9449 debug_displays[j].prescan (section, start, file);
9450 free (start);
9453 break;
9457 for (i = 0, section = section_headers;
9458 i < elf_header.e_shnum && i < num_dump_sects;
9459 i++, section++)
9461 #ifdef SUPPORT_DISASSEMBLY
9462 if (dump_sects[i] & DISASS_DUMP)
9463 disassemble_section (section, file);
9464 #endif
9465 if (dump_sects[i] & HEX_DUMP)
9466 dump_section (section, file);
9468 if (dump_sects[i] & DEBUG_DUMP)
9469 display_debug_section (section, file);
9472 if (i < num_dump_sects)
9473 warn (_("Some sections were not dumped because they do not exist!\n"));
9475 return 1;
9478 static void
9479 process_mips_fpe_exception (mask)
9480 int mask;
9482 if (mask)
9484 int first = 1;
9485 if (mask & OEX_FPU_INEX)
9486 fputs ("INEX", stdout), first = 0;
9487 if (mask & OEX_FPU_UFLO)
9488 printf ("%sUFLO", first ? "" : "|"), first = 0;
9489 if (mask & OEX_FPU_OFLO)
9490 printf ("%sOFLO", first ? "" : "|"), first = 0;
9491 if (mask & OEX_FPU_DIV0)
9492 printf ("%sDIV0", first ? "" : "|"), first = 0;
9493 if (mask & OEX_FPU_INVAL)
9494 printf ("%sINVAL", first ? "" : "|");
9496 else
9497 fputs ("0", stdout);
9500 static int
9501 process_mips_specific (file)
9502 FILE *file;
9504 Elf_Internal_Dyn *entry;
9505 size_t liblist_offset = 0;
9506 size_t liblistno = 0;
9507 size_t conflictsno = 0;
9508 size_t options_offset = 0;
9509 size_t conflicts_offset = 0;
9511 /* We have a lot of special sections. Thanks SGI! */
9512 if (dynamic_segment == NULL)
9513 /* No information available. */
9514 return 0;
9516 for (entry = dynamic_segment; entry->d_tag != DT_NULL; ++entry)
9517 switch (entry->d_tag)
9519 case DT_MIPS_LIBLIST:
9520 liblist_offset = entry->d_un.d_val - loadaddr;
9521 break;
9522 case DT_MIPS_LIBLISTNO:
9523 liblistno = entry->d_un.d_val;
9524 break;
9525 case DT_MIPS_OPTIONS:
9526 options_offset = entry->d_un.d_val - loadaddr;
9527 break;
9528 case DT_MIPS_CONFLICT:
9529 conflicts_offset = entry->d_un.d_val - loadaddr;
9530 break;
9531 case DT_MIPS_CONFLICTNO:
9532 conflictsno = entry->d_un.d_val;
9533 break;
9534 default:
9535 break;
9538 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
9540 Elf32_External_Lib *elib;
9541 size_t cnt;
9543 elib = ((Elf32_External_Lib *)
9544 get_data (NULL, file, liblist_offset,
9545 liblistno * sizeof (Elf32_External_Lib),
9546 _("liblist")));
9547 if (elib)
9549 printf ("\nSection '.liblist' contains %lu entries:\n",
9550 (unsigned long) liblistno);
9551 fputs (" Library Time Stamp Checksum Version Flags\n",
9552 stdout);
9554 for (cnt = 0; cnt < liblistno; ++cnt)
9556 Elf32_Lib liblist;
9557 time_t time;
9558 char timebuf[20];
9559 struct tm *tmp;
9561 liblist.l_name = BYTE_GET (elib[cnt].l_name);
9562 time = BYTE_GET (elib[cnt].l_time_stamp);
9563 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
9564 liblist.l_version = BYTE_GET (elib[cnt].l_version);
9565 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
9567 tmp = gmtime (&time);
9568 sprintf (timebuf, "%04u-%02u-%02uT%02u:%02u:%02u",
9569 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9570 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9572 printf ("%3lu: ", (unsigned long) cnt);
9573 print_symbol (20, dynamic_strings + liblist.l_name);
9574 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
9575 liblist.l_version);
9577 if (liblist.l_flags == 0)
9578 puts (" NONE");
9579 else
9581 static const struct
9583 const char *name;
9584 int bit;
9586 l_flags_vals[] =
9588 { " EXACT_MATCH", LL_EXACT_MATCH },
9589 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
9590 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
9591 { " EXPORTS", LL_EXPORTS },
9592 { " DELAY_LOAD", LL_DELAY_LOAD },
9593 { " DELTA", LL_DELTA }
9595 int flags = liblist.l_flags;
9596 size_t fcnt;
9598 for (fcnt = 0;
9599 fcnt < sizeof (l_flags_vals) / sizeof (l_flags_vals[0]);
9600 ++fcnt)
9601 if ((flags & l_flags_vals[fcnt].bit) != 0)
9603 fputs (l_flags_vals[fcnt].name, stdout);
9604 flags ^= l_flags_vals[fcnt].bit;
9606 if (flags != 0)
9607 printf (" %#x", (unsigned int) flags);
9609 puts ("");
9613 free (elib);
9617 if (options_offset != 0)
9619 Elf_External_Options *eopt;
9620 Elf_Internal_Shdr *sect = section_headers;
9621 Elf_Internal_Options *iopt;
9622 Elf_Internal_Options *option;
9623 size_t offset;
9624 int cnt;
9626 /* Find the section header so that we get the size. */
9627 while (sect->sh_type != SHT_MIPS_OPTIONS)
9628 ++sect;
9630 eopt = (Elf_External_Options *) get_data (NULL, file, options_offset,
9631 sect->sh_size, _("options"));
9632 if (eopt)
9634 iopt = ((Elf_Internal_Options *)
9635 malloc ((sect->sh_size / sizeof (eopt)) * sizeof (*iopt)));
9636 if (iopt == NULL)
9638 error (_("Out of memory"));
9639 return 0;
9642 offset = cnt = 0;
9643 option = iopt;
9645 while (offset < sect->sh_size)
9647 Elf_External_Options *eoption;
9649 eoption = (Elf_External_Options *) ((char *) eopt + offset);
9651 option->kind = BYTE_GET (eoption->kind);
9652 option->size = BYTE_GET (eoption->size);
9653 option->section = BYTE_GET (eoption->section);
9654 option->info = BYTE_GET (eoption->info);
9656 offset += option->size;
9658 ++option;
9659 ++cnt;
9662 printf (_("\nSection '%s' contains %d entries:\n"),
9663 SECTION_NAME (sect), cnt);
9665 option = iopt;
9667 while (cnt-- > 0)
9669 size_t len;
9671 switch (option->kind)
9673 case ODK_NULL:
9674 /* This shouldn't happen. */
9675 printf (" NULL %d %lx", option->section, option->info);
9676 break;
9677 case ODK_REGINFO:
9678 printf (" REGINFO ");
9679 if (elf_header.e_machine == EM_MIPS)
9681 /* 32bit form. */
9682 Elf32_External_RegInfo *ereg;
9683 Elf32_RegInfo reginfo;
9685 ereg = (Elf32_External_RegInfo *) (option + 1);
9686 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9687 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9688 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9689 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9690 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9691 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
9693 printf ("GPR %08lx GP 0x%lx\n",
9694 reginfo.ri_gprmask,
9695 (unsigned long) reginfo.ri_gp_value);
9696 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
9697 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9698 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9700 else
9702 /* 64 bit form. */
9703 Elf64_External_RegInfo *ereg;
9704 Elf64_Internal_RegInfo reginfo;
9706 ereg = (Elf64_External_RegInfo *) (option + 1);
9707 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9708 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9709 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9710 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9711 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9712 reginfo.ri_gp_value = BYTE_GET8 (ereg->ri_gp_value);
9714 printf ("GPR %08lx GP 0x",
9715 reginfo.ri_gprmask);
9716 printf_vma (reginfo.ri_gp_value);
9717 printf ("\n");
9719 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
9720 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9721 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9723 ++option;
9724 continue;
9725 case ODK_EXCEPTIONS:
9726 fputs (" EXCEPTIONS fpe_min(", stdout);
9727 process_mips_fpe_exception (option->info & OEX_FPU_MIN);
9728 fputs (") fpe_max(", stdout);
9729 process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
9730 fputs (")", stdout);
9732 if (option->info & OEX_PAGE0)
9733 fputs (" PAGE0", stdout);
9734 if (option->info & OEX_SMM)
9735 fputs (" SMM", stdout);
9736 if (option->info & OEX_FPDBUG)
9737 fputs (" FPDBUG", stdout);
9738 if (option->info & OEX_DISMISS)
9739 fputs (" DISMISS", stdout);
9740 break;
9741 case ODK_PAD:
9742 fputs (" PAD ", stdout);
9743 if (option->info & OPAD_PREFIX)
9744 fputs (" PREFIX", stdout);
9745 if (option->info & OPAD_POSTFIX)
9746 fputs (" POSTFIX", stdout);
9747 if (option->info & OPAD_SYMBOL)
9748 fputs (" SYMBOL", stdout);
9749 break;
9750 case ODK_HWPATCH:
9751 fputs (" HWPATCH ", stdout);
9752 if (option->info & OHW_R4KEOP)
9753 fputs (" R4KEOP", stdout);
9754 if (option->info & OHW_R8KPFETCH)
9755 fputs (" R8KPFETCH", stdout);
9756 if (option->info & OHW_R5KEOP)
9757 fputs (" R5KEOP", stdout);
9758 if (option->info & OHW_R5KCVTL)
9759 fputs (" R5KCVTL", stdout);
9760 break;
9761 case ODK_FILL:
9762 fputs (" FILL ", stdout);
9763 /* XXX Print content of info word? */
9764 break;
9765 case ODK_TAGS:
9766 fputs (" TAGS ", stdout);
9767 /* XXX Print content of info word? */
9768 break;
9769 case ODK_HWAND:
9770 fputs (" HWAND ", stdout);
9771 if (option->info & OHWA0_R4KEOP_CHECKED)
9772 fputs (" R4KEOP_CHECKED", stdout);
9773 if (option->info & OHWA0_R4KEOP_CLEAN)
9774 fputs (" R4KEOP_CLEAN", stdout);
9775 break;
9776 case ODK_HWOR:
9777 fputs (" HWOR ", stdout);
9778 if (option->info & OHWA0_R4KEOP_CHECKED)
9779 fputs (" R4KEOP_CHECKED", stdout);
9780 if (option->info & OHWA0_R4KEOP_CLEAN)
9781 fputs (" R4KEOP_CLEAN", stdout);
9782 break;
9783 case ODK_GP_GROUP:
9784 printf (" GP_GROUP %#06lx self-contained %#06lx",
9785 option->info & OGP_GROUP,
9786 (option->info & OGP_SELF) >> 16);
9787 break;
9788 case ODK_IDENT:
9789 printf (" IDENT %#06lx self-contained %#06lx",
9790 option->info & OGP_GROUP,
9791 (option->info & OGP_SELF) >> 16);
9792 break;
9793 default:
9794 /* This shouldn't happen. */
9795 printf (" %3d ??? %d %lx",
9796 option->kind, option->section, option->info);
9797 break;
9800 len = sizeof (*eopt);
9801 while (len < option->size)
9802 if (((char *) option)[len] >= ' '
9803 && ((char *) option)[len] < 0x7f)
9804 printf ("%c", ((char *) option)[len++]);
9805 else
9806 printf ("\\%03o", ((char *) option)[len++]);
9808 fputs ("\n", stdout);
9809 ++option;
9812 free (eopt);
9816 if (conflicts_offset != 0 && conflictsno != 0)
9818 Elf32_Conflict *iconf;
9819 size_t cnt;
9821 if (dynamic_symbols == NULL)
9823 error (_("conflict list found without a dynamic symbol table"));
9824 return 0;
9827 iconf = (Elf32_Conflict *) malloc (conflictsno * sizeof (*iconf));
9828 if (iconf == NULL)
9830 error (_("Out of memory"));
9831 return 0;
9834 if (is_32bit_elf)
9836 Elf32_External_Conflict *econf32;
9838 econf32 = ((Elf32_External_Conflict *)
9839 get_data (NULL, file, conflicts_offset,
9840 conflictsno * sizeof (*econf32),
9841 _("conflict")));
9842 if (!econf32)
9843 return 0;
9845 for (cnt = 0; cnt < conflictsno; ++cnt)
9846 iconf[cnt] = BYTE_GET (econf32[cnt]);
9848 free (econf32);
9850 else
9852 Elf64_External_Conflict *econf64;
9854 econf64 = ((Elf64_External_Conflict *)
9855 get_data (NULL, file, conflicts_offset,
9856 conflictsno * sizeof (*econf64),
9857 _("conflict")));
9858 if (!econf64)
9859 return 0;
9861 for (cnt = 0; cnt < conflictsno; ++cnt)
9862 iconf[cnt] = BYTE_GET (econf64[cnt]);
9864 free (econf64);
9867 printf (_("\nSection '.conflict' contains %ld entries:\n"),
9868 (long) conflictsno);
9869 puts (_(" Num: Index Value Name"));
9871 for (cnt = 0; cnt < conflictsno; ++cnt)
9873 Elf_Internal_Sym *psym = & dynamic_symbols[iconf[cnt]];
9875 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
9876 print_vma (psym->st_value, FULL_HEX);
9877 putchar (' ');
9878 print_symbol (25, dynamic_strings + psym->st_name);
9879 putchar ('\n');
9882 free (iconf);
9885 return 1;
9888 static int
9889 process_gnu_liblist (file)
9890 FILE *file;
9892 Elf_Internal_Shdr *section, *string_sec;
9893 Elf32_External_Lib *elib;
9894 char *strtab;
9895 size_t cnt;
9896 unsigned i;
9898 if (! do_arch)
9899 return 0;
9901 for (i = 0, section = section_headers;
9902 i < elf_header.e_shnum;
9903 i++, section++)
9905 switch (section->sh_type)
9907 case SHT_GNU_LIBLIST:
9908 elib = ((Elf32_External_Lib *)
9909 get_data (NULL, file, section->sh_offset, section->sh_size,
9910 _("liblist")));
9912 if (elib == NULL)
9913 break;
9914 string_sec = SECTION_HEADER (section->sh_link);
9916 strtab = (char *) get_data (NULL, file, string_sec->sh_offset,
9917 string_sec->sh_size,
9918 _("liblist string table"));
9920 if (strtab == NULL
9921 || section->sh_entsize != sizeof (Elf32_External_Lib))
9923 free (elib);
9924 break;
9927 printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
9928 SECTION_NAME (section),
9929 (long) (section->sh_size / sizeof (Elf32_External_Lib)));
9931 puts (" Library Time Stamp Checksum Version Flags");
9933 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
9934 ++cnt)
9936 Elf32_Lib liblist;
9937 time_t time;
9938 char timebuf[20];
9939 struct tm *tmp;
9941 liblist.l_name = BYTE_GET (elib[cnt].l_name);
9942 time = BYTE_GET (elib[cnt].l_time_stamp);
9943 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
9944 liblist.l_version = BYTE_GET (elib[cnt].l_version);
9945 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
9947 tmp = gmtime (&time);
9948 sprintf (timebuf, "%04u-%02u-%02uT%02u:%02u:%02u",
9949 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9950 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9952 printf ("%3lu: ", (unsigned long) cnt);
9953 if (do_wide)
9954 printf ("%-20s", strtab + liblist.l_name);
9955 else
9956 printf ("%-20.20s", strtab + liblist.l_name);
9957 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
9958 liblist.l_version, liblist.l_flags);
9961 free (elib);
9965 return 1;
9968 static const char *
9969 get_note_type (e_type)
9970 unsigned e_type;
9972 static char buff[64];
9974 switch (e_type)
9976 case NT_PRSTATUS: return _("NT_PRSTATUS (prstatus structure)");
9977 case NT_FPREGSET: return _("NT_FPREGSET (floating point registers)");
9978 case NT_PRPSINFO: return _("NT_PRPSINFO (prpsinfo structure)");
9979 case NT_TASKSTRUCT: return _("NT_TASKSTRUCT (task structure)");
9980 case NT_PRXFPREG: return _("NT_PRXFPREG (user_xfpregs structure)");
9981 case NT_PSTATUS: return _("NT_PSTATUS (pstatus structure)");
9982 case NT_FPREGS: return _("NT_FPREGS (floating point registers)");
9983 case NT_PSINFO: return _("NT_PSINFO (psinfo structure)");
9984 case NT_LWPSTATUS: return _("NT_LWPSTATUS (lwpstatus_t structure)");
9985 case NT_LWPSINFO: return _("NT_LWPSINFO (lwpsinfo_t structure)");
9986 case NT_WIN32PSTATUS: return _("NT_WIN32PSTATUS (win32_pstatus structure)");
9987 default:
9988 sprintf (buff, _("Unknown note type: (0x%08x)"), e_type);
9989 return buff;
9993 static const char *
9994 get_netbsd_elfcore_note_type (e_type)
9995 unsigned e_type;
9997 static char buff[64];
9999 if (e_type == NT_NETBSDCORE_PROCINFO)
10001 /* NetBSD core "procinfo" structure. */
10002 return _("NetBSD procinfo structure");
10005 /* As of Jan 2002 there are no other machine-independent notes
10006 defined for NetBSD core files. If the note type is less
10007 than the start of the machine-dependent note types, we don't
10008 understand it. */
10010 if (e_type < NT_NETBSDCORE_FIRSTMACH)
10012 sprintf (buff, _("Unknown note type: (0x%08x)"), e_type);
10013 return buff;
10016 switch (elf_header.e_machine)
10018 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
10019 and PT_GETFPREGS == mach+2. */
10021 case EM_OLD_ALPHA:
10022 case EM_ALPHA:
10023 case EM_SPARC:
10024 case EM_SPARC32PLUS:
10025 case EM_SPARCV9:
10026 switch (e_type)
10028 case NT_NETBSDCORE_FIRSTMACH+0:
10029 return _("PT_GETREGS (reg structure)");
10030 case NT_NETBSDCORE_FIRSTMACH+2:
10031 return _("PT_GETFPREGS (fpreg structure)");
10032 default:
10033 break;
10035 break;
10037 /* On all other arch's, PT_GETREGS == mach+1 and
10038 PT_GETFPREGS == mach+3. */
10039 default:
10040 switch (e_type)
10042 case NT_NETBSDCORE_FIRSTMACH+1:
10043 return _("PT_GETREGS (reg structure)");
10044 case NT_NETBSDCORE_FIRSTMACH+3:
10045 return _("PT_GETFPREGS (fpreg structure)");
10046 default:
10047 break;
10051 sprintf (buff, _("PT_FIRSTMACH+%d"), e_type - NT_NETBSDCORE_FIRSTMACH);
10052 return buff;
10055 /* Note that by the ELF standard, the name field is already null byte
10056 terminated, and namesz includes the terminating null byte.
10057 I.E. the value of namesz for the name "FSF" is 4.
10059 If the value of namesz is zero, there is no name present. */
10060 static int
10061 process_note (pnote)
10062 Elf_Internal_Note *pnote;
10064 const char *nt;
10066 if (pnote->namesz == 0)
10068 /* If there is no note name, then use the default set of
10069 note type strings. */
10070 nt = get_note_type (pnote->type);
10072 else if (strncmp (pnote->namedata, "NetBSD-CORE", 11) == 0)
10074 /* NetBSD-specific core file notes. */
10075 nt = get_netbsd_elfcore_note_type (pnote->type);
10077 else
10079 /* Don't recognize this note name; just use the default set of
10080 note type strings. */
10081 nt = get_note_type (pnote->type);
10084 printf (" %s\t\t0x%08lx\t%s\n",
10085 pnote->namesz ? pnote->namedata : "(NONE)",
10086 pnote->descsz, nt);
10087 return 1;
10091 static int
10092 process_corefile_note_segment (file, offset, length)
10093 FILE *file;
10094 bfd_vma offset;
10095 bfd_vma length;
10097 Elf_External_Note *pnotes;
10098 Elf_External_Note *external;
10099 int res = 1;
10101 if (length <= 0)
10102 return 0;
10104 pnotes = (Elf_External_Note *) get_data (NULL, file, offset, length,
10105 _("notes"));
10106 if (!pnotes)
10107 return 0;
10109 external = pnotes;
10111 printf (_("\nNotes at offset 0x%08lx with length 0x%08lx:\n"),
10112 (unsigned long) offset, (unsigned long) length);
10113 printf (_(" Owner\t\tData size\tDescription\n"));
10115 while (external < (Elf_External_Note *)((char *) pnotes + length))
10117 Elf_External_Note *next;
10118 Elf_Internal_Note inote;
10119 char *temp = NULL;
10121 inote.type = BYTE_GET (external->type);
10122 inote.namesz = BYTE_GET (external->namesz);
10123 inote.namedata = external->name;
10124 inote.descsz = BYTE_GET (external->descsz);
10125 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
10126 inote.descpos = offset + (inote.descdata - (char *) pnotes);
10128 next = (Elf_External_Note *)(inote.descdata + align_power (inote.descsz, 2));
10130 if (((char *) next) > (((char *) pnotes) + length))
10132 warn (_("corrupt note found at offset %x into core notes\n"),
10133 ((char *) external) - ((char *) pnotes));
10134 warn (_(" type: %x, namesize: %08lx, descsize: %08lx\n"),
10135 inote.type, inote.namesz, inote.descsz);
10136 break;
10139 external = next;
10141 /* Verify that name is null terminated. It appears that at least
10142 one version of Linux (RedHat 6.0) generates corefiles that don't
10143 comply with the ELF spec by failing to include the null byte in
10144 namesz. */
10145 if (inote.namedata[inote.namesz] != '\0')
10147 temp = malloc (inote.namesz + 1);
10149 if (temp == NULL)
10151 error (_("Out of memory\n"));
10152 res = 0;
10153 break;
10156 strncpy (temp, inote.namedata, inote.namesz);
10157 temp[inote.namesz] = 0;
10159 /* warn (_("'%s' NOTE name not properly null terminated\n"), temp); */
10160 inote.namedata = temp;
10163 res &= process_note (& inote);
10165 if (temp != NULL)
10167 free (temp);
10168 temp = NULL;
10172 free (pnotes);
10174 return res;
10177 static int
10178 process_corefile_note_segments (file)
10179 FILE *file;
10181 Elf_Internal_Phdr *program_headers;
10182 Elf_Internal_Phdr *segment;
10183 unsigned int i;
10184 int res = 1;
10186 program_headers = (Elf_Internal_Phdr *) malloc
10187 (elf_header.e_phnum * sizeof (Elf_Internal_Phdr));
10189 if (program_headers == NULL)
10191 error (_("Out of memory\n"));
10192 return 0;
10195 if (is_32bit_elf)
10196 i = get_32bit_program_headers (file, program_headers);
10197 else
10198 i = get_64bit_program_headers (file, program_headers);
10200 if (i == 0)
10202 free (program_headers);
10203 return 0;
10206 for (i = 0, segment = program_headers;
10207 i < elf_header.e_phnum;
10208 i++, segment++)
10210 if (segment->p_type == PT_NOTE)
10211 res &= process_corefile_note_segment (file,
10212 (bfd_vma) segment->p_offset,
10213 (bfd_vma) segment->p_filesz);
10216 free (program_headers);
10218 return res;
10221 static int
10222 process_corefile_contents (file)
10223 FILE *file;
10225 /* If we have not been asked to display the notes then do nothing. */
10226 if (! do_notes)
10227 return 1;
10229 /* If file is not a core file then exit. */
10230 if (elf_header.e_type != ET_CORE)
10231 return 1;
10233 /* No program headers means no NOTE segment. */
10234 if (elf_header.e_phnum == 0)
10236 printf (_("No note segments present in the core file.\n"));
10237 return 1;
10240 return process_corefile_note_segments (file);
10243 static int
10244 process_arch_specific (file)
10245 FILE *file;
10247 if (! do_arch)
10248 return 1;
10250 switch (elf_header.e_machine)
10252 case EM_MIPS:
10253 case EM_MIPS_RS3_LE:
10254 return process_mips_specific (file);
10255 break;
10256 default:
10257 break;
10259 return 1;
10262 static int
10263 get_file_header (file)
10264 FILE *file;
10266 /* Read in the identity array. */
10267 if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
10268 return 0;
10270 /* Determine how to read the rest of the header. */
10271 switch (elf_header.e_ident[EI_DATA])
10273 default: /* fall through */
10274 case ELFDATANONE: /* fall through */
10275 case ELFDATA2LSB: byte_get = byte_get_little_endian; break;
10276 case ELFDATA2MSB: byte_get = byte_get_big_endian; break;
10279 /* For now we only support 32 bit and 64 bit ELF files. */
10280 is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
10282 /* Read in the rest of the header. */
10283 if (is_32bit_elf)
10285 Elf32_External_Ehdr ehdr32;
10287 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
10288 return 0;
10290 elf_header.e_type = BYTE_GET (ehdr32.e_type);
10291 elf_header.e_machine = BYTE_GET (ehdr32.e_machine);
10292 elf_header.e_version = BYTE_GET (ehdr32.e_version);
10293 elf_header.e_entry = BYTE_GET (ehdr32.e_entry);
10294 elf_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
10295 elf_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
10296 elf_header.e_flags = BYTE_GET (ehdr32.e_flags);
10297 elf_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
10298 elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
10299 elf_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
10300 elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
10301 elf_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
10302 elf_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
10304 else
10306 Elf64_External_Ehdr ehdr64;
10308 /* If we have been compiled with sizeof (bfd_vma) == 4, then
10309 we will not be able to cope with the 64bit data found in
10310 64 ELF files. Detect this now and abort before we start
10311 overwritting things. */
10312 if (sizeof (bfd_vma) < 8)
10314 error (_("This instance of readelf has been built without support for a\n\
10315 64 bit data type and so it cannot read 64 bit ELF files.\n"));
10316 return 0;
10319 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
10320 return 0;
10322 elf_header.e_type = BYTE_GET (ehdr64.e_type);
10323 elf_header.e_machine = BYTE_GET (ehdr64.e_machine);
10324 elf_header.e_version = BYTE_GET (ehdr64.e_version);
10325 elf_header.e_entry = BYTE_GET8 (ehdr64.e_entry);
10326 elf_header.e_phoff = BYTE_GET8 (ehdr64.e_phoff);
10327 elf_header.e_shoff = BYTE_GET8 (ehdr64.e_shoff);
10328 elf_header.e_flags = BYTE_GET (ehdr64.e_flags);
10329 elf_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
10330 elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
10331 elf_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
10332 elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
10333 elf_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
10334 elf_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
10337 if (elf_header.e_shoff)
10339 /* There may be some extensions in the first section header. Don't
10340 bomb if we can't read it. */
10341 if (is_32bit_elf)
10342 get_32bit_section_headers (file, 1);
10343 else
10344 get_64bit_section_headers (file, 1);
10347 return 1;
10350 static int
10351 process_file (file_name)
10352 char *file_name;
10354 FILE *file;
10355 struct stat statbuf;
10356 unsigned int i;
10358 if (stat (file_name, & statbuf) < 0)
10360 error (_("Cannot stat input file %s.\n"), file_name);
10361 return 1;
10364 file = fopen (file_name, "rb");
10365 if (file == NULL)
10367 error (_("Input file %s not found.\n"), file_name);
10368 return 1;
10371 if (! get_file_header (file))
10373 error (_("%s: Failed to read file header\n"), file_name);
10374 fclose (file);
10375 return 1;
10378 /* Initialise per file variables. */
10379 for (i = NUM_ELEM (version_info); i--;)
10380 version_info[i] = 0;
10382 for (i = NUM_ELEM (dynamic_info); i--;)
10383 dynamic_info[i] = 0;
10385 /* Process the file. */
10386 if (show_name)
10387 printf (_("\nFile: %s\n"), file_name);
10389 if (! process_file_header ())
10391 fclose (file);
10392 return 1;
10395 if (! process_section_headers (file))
10397 /* Without loaded section headers we
10398 cannot process lots of things. */
10399 do_unwind = do_version = do_dump = do_arch = 0;
10401 if (! do_using_dynamic)
10402 do_syms = do_reloc = 0;
10405 if (process_program_headers (file))
10406 process_dynamic_segment (file);
10408 process_relocs (file);
10410 process_unwind (file);
10412 process_symbol_table (file);
10414 process_syminfo (file);
10416 process_version_sections (file);
10418 process_section_contents (file);
10420 process_corefile_contents (file);
10422 process_gnu_liblist (file);
10424 process_arch_specific (file);
10426 fclose (file);
10428 if (section_headers)
10430 free (section_headers);
10431 section_headers = NULL;
10434 if (string_table)
10436 free (string_table);
10437 string_table = NULL;
10438 string_table_length = 0;
10441 if (dynamic_strings)
10443 free (dynamic_strings);
10444 dynamic_strings = NULL;
10447 if (dynamic_symbols)
10449 free (dynamic_symbols);
10450 dynamic_symbols = NULL;
10451 num_dynamic_syms = 0;
10454 if (dynamic_syminfo)
10456 free (dynamic_syminfo);
10457 dynamic_syminfo = NULL;
10460 return 0;
10463 #ifdef SUPPORT_DISASSEMBLY
10464 /* Needed by the i386 disassembler. For extra credit, someone could
10465 fix this so that we insert symbolic addresses here, esp for GOT/PLT
10466 symbols. */
10468 void
10469 print_address (unsigned int addr, FILE *outfile)
10471 fprintf (outfile,"0x%8.8x", addr);
10474 /* Needed by the i386 disassembler. */
10475 void
10476 db_task_printsym (unsigned int addr)
10478 print_address (addr, stderr);
10480 #endif
10482 int main PARAMS ((int, char **));
10485 main (argc, argv)
10486 int argc;
10487 char **argv;
10489 int err;
10490 char *cmdline_dump_sects = NULL;
10491 unsigned num_cmdline_dump_sects = 0;
10493 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
10494 setlocale (LC_MESSAGES, "");
10495 #endif
10496 #if defined (HAVE_SETLOCALE)
10497 setlocale (LC_CTYPE, "");
10498 #endif
10499 bindtextdomain (PACKAGE, LOCALEDIR);
10500 textdomain (PACKAGE);
10502 parse_args (argc, argv);
10504 if (optind < (argc - 1))
10505 show_name = 1;
10507 /* When processing more than one file remember the dump requests
10508 issued on command line to reset them after each file. */
10509 if (optind + 1 < argc && dump_sects != NULL)
10511 cmdline_dump_sects = malloc (num_dump_sects);
10512 if (cmdline_dump_sects == NULL)
10513 error (_("Out of memory allocating dump request table."));
10514 else
10516 memcpy (cmdline_dump_sects, dump_sects, num_dump_sects);
10517 num_cmdline_dump_sects = num_dump_sects;
10521 err = 0;
10522 while (optind < argc)
10524 err |= process_file (argv[optind++]);
10526 /* Reset dump requests. */
10527 if (optind < argc && dump_sects != NULL)
10529 num_dump_sects = num_cmdline_dump_sects;
10530 if (num_cmdline_dump_sects > 0)
10531 memcpy (dump_sects, cmdline_dump_sects, num_cmdline_dump_sects);
10535 if (dump_sects != NULL)
10536 free (dump_sects);
10537 if (cmdline_dump_sects != NULL)
10538 free (cmdline_dump_sects);
10540 return err;