* objcopy.c: Remove unnecessary prototypes and casts.
[deliverable/binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@redhat.com>
6
7 This file is part of GNU Binutils.
8
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.
13
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.
18
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. */
23 \f
24
25 #include <assert.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdio.h>
29 #include <time.h>
30
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
38
39 #include "bfd.h"
40
41 #include "elf/common.h"
42 #include "elf/external.h"
43 #include "elf/internal.h"
44 #include "elf/dwarf2.h"
45
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() */
49
50 #define RELOC_MACROS_GEN_FUNC
51
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"
91 #include "elf/xtensa.h"
92
93 #include "bucomm.h"
94 #include "getopt.h"
95 #include "libiberty.h"
96
97 char *program_name = "readelf";
98 unsigned long dynamic_addr;
99 bfd_size_type dynamic_size;
100 char *dynamic_strings;
101 char *string_table;
102 unsigned long string_table_length;
103 unsigned long num_dynamic_syms;
104 Elf_Internal_Sym *dynamic_symbols;
105 Elf_Internal_Syminfo *dynamic_syminfo;
106 unsigned long dynamic_syminfo_offset;
107 unsigned int dynamic_syminfo_nent;
108 char program_interpreter[64];
109 bfd_vma dynamic_info[DT_JMPREL + 1];
110 bfd_vma version_info[16];
111 Elf_Internal_Ehdr elf_header;
112 Elf_Internal_Shdr *section_headers;
113 Elf_Internal_Phdr *program_headers;
114 Elf_Internal_Dyn *dynamic_segment;
115 Elf_Internal_Shdr *symtab_shndx_hdr;
116 int show_name;
117 int do_dynamic;
118 int do_syms;
119 int do_reloc;
120 int do_sections;
121 int do_segments;
122 int do_unwind;
123 int do_using_dynamic;
124 int do_header;
125 int do_dump;
126 int do_version;
127 int do_wide;
128 int do_histogram;
129 int do_debugging;
130 int do_debug_info;
131 int do_debug_abbrevs;
132 int do_debug_lines;
133 int do_debug_pubnames;
134 int do_debug_aranges;
135 int do_debug_frames;
136 int do_debug_frames_interp;
137 int do_debug_macinfo;
138 int do_debug_str;
139 int do_debug_loc;
140 int do_arch;
141 int do_notes;
142 int is_32bit_elf;
143
144 /* A dynamic array of flags indicating which sections require dumping. */
145 char *dump_sects = NULL;
146 unsigned int num_dump_sects = 0;
147
148 #define HEX_DUMP (1 << 0)
149 #define DISASS_DUMP (1 << 1)
150 #define DEBUG_DUMP (1 << 2)
151
152 /* How to rpint a vma value. */
153 typedef enum print_mode
154 {
155 HEX,
156 DEC,
157 DEC_5,
158 UNSIGNED,
159 PREFIX_HEX,
160 FULL_HEX,
161 LONG_HEX
162 }
163 print_mode;
164
165 static bfd_vma (*byte_get) (unsigned char *, int);
166 static void (*byte_put) (unsigned char *, bfd_vma, int);
167
168 typedef int Elf32_Word;
169
170 #define UNKNOWN -1
171
172 #define SECTION_NAME(X) ((X) == NULL ? "<none>" : \
173 ((X)->sh_name >= string_table_length \
174 ? "<corrupt>" : string_table + (X)->sh_name))
175
176 /* Given st_shndx I, map to section_headers index. */
177 #define SECTION_HEADER_INDEX(I) \
178 ((I) < SHN_LORESERVE \
179 ? (I) \
180 : ((I) <= SHN_HIRESERVE \
181 ? 0 \
182 : (I) - (SHN_HIRESERVE + 1 - SHN_LORESERVE)))
183
184 /* Reverse of the above. */
185 #define SECTION_HEADER_NUM(N) \
186 ((N) < SHN_LORESERVE \
187 ? (N) \
188 : (N) + (SHN_HIRESERVE + 1 - SHN_LORESERVE))
189
190 #define SECTION_HEADER(I) (section_headers + SECTION_HEADER_INDEX (I))
191
192 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
193
194 #define BYTE_GET(field) byte_get (field, sizeof (field))
195
196 /* If we can support a 64 bit data type then BFD64 should be defined
197 and sizeof (bfd_vma) == 8. In this case when translating from an
198 external 8 byte field to an internal field, we can assume that the
199 internal field is also 8 bytes wide and so we can extract all the data.
200 If, however, BFD64 is not defined, then we must assume that the
201 internal data structure only has 4 byte wide fields that are the
202 equivalent of the 8 byte wide external counterparts, and so we must
203 truncate the data. */
204 #ifdef BFD64
205 #define BYTE_GET8(field) byte_get (field, -8)
206 #else
207 #define BYTE_GET8(field) byte_get (field, 8)
208 #endif
209
210 #define NUM_ELEM(array) (sizeof (array) / sizeof ((array)[0]))
211
212 #define GET_ELF_SYMBOLS(file, section) \
213 (is_32bit_elf ? get_32bit_elf_symbols (file, section) \
214 : get_64bit_elf_symbols (file, section))
215
216
217 static void
218 error (const char *message, ...)
219 {
220 va_list args;
221
222 va_start (args, message);
223 fprintf (stderr, _("%s: Error: "), program_name);
224 vfprintf (stderr, message, args);
225 va_end (args);
226 }
227
228 static void
229 warn (const char *message, ...)
230 {
231 va_list args;
232
233 va_start (args, message);
234 fprintf (stderr, _("%s: Warning: "), program_name);
235 vfprintf (stderr, message, args);
236 va_end (args);
237 }
238
239 static void *
240 get_data (void *var, FILE *file, long offset, size_t size, const char *reason)
241 {
242 void *mvar;
243
244 if (size == 0)
245 return NULL;
246
247 if (fseek (file, offset, SEEK_SET))
248 {
249 error (_("Unable to seek to 0x%x for %s\n"), offset, reason);
250 return NULL;
251 }
252
253 mvar = var;
254 if (mvar == NULL)
255 {
256 mvar = malloc (size);
257
258 if (mvar == NULL)
259 {
260 error (_("Out of memory allocating 0x%x bytes for %s\n"),
261 size, reason);
262 return NULL;
263 }
264 }
265
266 if (fread (mvar, size, 1, file) != 1)
267 {
268 error (_("Unable to read in 0x%x bytes of %s\n"), size, reason);
269 if (mvar != var)
270 free (mvar);
271 return NULL;
272 }
273
274 return mvar;
275 }
276
277 static bfd_vma
278 byte_get_little_endian (unsigned char *field, int size)
279 {
280 switch (size)
281 {
282 case 1:
283 return *field;
284
285 case 2:
286 return ((unsigned int) (field[0]))
287 | (((unsigned int) (field[1])) << 8);
288
289 #ifndef BFD64
290 case 8:
291 /* We want to extract data from an 8 byte wide field and
292 place it into a 4 byte wide field. Since this is a little
293 endian source we can just use the 4 byte extraction code. */
294 /* Fall through. */
295 #endif
296 case 4:
297 return ((unsigned long) (field[0]))
298 | (((unsigned long) (field[1])) << 8)
299 | (((unsigned long) (field[2])) << 16)
300 | (((unsigned long) (field[3])) << 24);
301
302 #ifdef BFD64
303 case 8:
304 case -8:
305 /* This is a special case, generated by the BYTE_GET8 macro.
306 It means that we are loading an 8 byte value from a field
307 in an external structure into an 8 byte value in a field
308 in an internal strcuture. */
309 return ((bfd_vma) (field[0]))
310 | (((bfd_vma) (field[1])) << 8)
311 | (((bfd_vma) (field[2])) << 16)
312 | (((bfd_vma) (field[3])) << 24)
313 | (((bfd_vma) (field[4])) << 32)
314 | (((bfd_vma) (field[5])) << 40)
315 | (((bfd_vma) (field[6])) << 48)
316 | (((bfd_vma) (field[7])) << 56);
317 #endif
318 default:
319 error (_("Unhandled data length: %d\n"), size);
320 abort ();
321 }
322 }
323
324 static bfd_vma
325 byte_get_signed (unsigned char *field, int size)
326 {
327 bfd_vma x = byte_get (field, size);
328
329 switch (size)
330 {
331 case 1:
332 return (x ^ 0x80) - 0x80;
333 case 2:
334 return (x ^ 0x8000) - 0x8000;
335 case 4:
336 return (x ^ 0x80000000) - 0x80000000;
337 case 8:
338 case -8:
339 return x;
340 default:
341 abort ();
342 }
343 }
344
345 static void
346 byte_put_little_endian (unsigned char *field, bfd_vma value, int size)
347 {
348 switch (size)
349 {
350 case 8:
351 field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
352 field[6] = ((value >> 24) >> 24) & 0xff;
353 field[5] = ((value >> 24) >> 16) & 0xff;
354 field[4] = ((value >> 24) >> 8) & 0xff;
355 /* Fall through. */
356 case 4:
357 field[3] = (value >> 24) & 0xff;
358 field[2] = (value >> 16) & 0xff;
359 /* Fall through. */
360 case 2:
361 field[1] = (value >> 8) & 0xff;
362 /* Fall through. */
363 case 1:
364 field[0] = value & 0xff;
365 break;
366
367 default:
368 error (_("Unhandled data length: %d\n"), size);
369 abort ();
370 }
371 }
372
373 /* Print a VMA value. */
374 static void
375 print_vma (bfd_vma vma, print_mode mode)
376 {
377 #ifdef BFD64
378 if (is_32bit_elf)
379 #endif
380 {
381 switch (mode)
382 {
383 case FULL_HEX:
384 printf ("0x");
385 /* Drop through. */
386 case LONG_HEX:
387 printf ("%8.8lx", (unsigned long) vma);
388 break;
389
390 case DEC_5:
391 if (vma <= 99999)
392 {
393 printf ("%5ld", (long) vma);
394 break;
395 }
396 /* Drop through. */
397 case PREFIX_HEX:
398 printf ("0x");
399 /* Drop through. */
400 case HEX:
401 printf ("%lx", (unsigned long) vma);
402 break;
403
404 case DEC:
405 printf ("%ld", (unsigned long) vma);
406 break;
407
408 case UNSIGNED:
409 printf ("%lu", (unsigned long) vma);
410 break;
411 }
412 }
413 #ifdef BFD64
414 else
415 {
416 switch (mode)
417 {
418 case FULL_HEX:
419 printf ("0x");
420 /* Drop through. */
421
422 case LONG_HEX:
423 printf_vma (vma);
424 break;
425
426 case PREFIX_HEX:
427 printf ("0x");
428 /* Drop through. */
429
430 case HEX:
431 #if BFD_HOST_64BIT_LONG
432 printf ("%lx", vma);
433 #else
434 if (_bfd_int64_high (vma))
435 printf ("%lx%8.8lx", _bfd_int64_high (vma), _bfd_int64_low (vma));
436 else
437 printf ("%lx", _bfd_int64_low (vma));
438 #endif
439 break;
440
441 case DEC:
442 #if BFD_HOST_64BIT_LONG
443 printf ("%ld", vma);
444 #else
445 if (_bfd_int64_high (vma))
446 /* ugg */
447 printf ("++%ld", _bfd_int64_low (vma));
448 else
449 printf ("%ld", _bfd_int64_low (vma));
450 #endif
451 break;
452
453 case DEC_5:
454 #if BFD_HOST_64BIT_LONG
455 if (vma <= 99999)
456 printf ("%5ld", vma);
457 else
458 printf ("%#lx", vma);
459 #else
460 if (_bfd_int64_high (vma))
461 /* ugg */
462 printf ("++%ld", _bfd_int64_low (vma));
463 else if (vma <= 99999)
464 printf ("%5ld", _bfd_int64_low (vma));
465 else
466 printf ("%#lx", _bfd_int64_low (vma));
467 #endif
468 break;
469
470 case UNSIGNED:
471 #if BFD_HOST_64BIT_LONG
472 printf ("%lu", vma);
473 #else
474 if (_bfd_int64_high (vma))
475 /* ugg */
476 printf ("++%lu", _bfd_int64_low (vma));
477 else
478 printf ("%lu", _bfd_int64_low (vma));
479 #endif
480 break;
481 }
482 }
483 #endif
484 }
485
486 /* Display a symbol on stdout. If do_wide is not true then
487 format the symbol to be at most WIDTH characters,
488 truncating as necessary. If WIDTH is negative then
489 format the string to be exactly - WIDTH characters,
490 truncating or padding as necessary. */
491
492 static void
493 print_symbol (int width, const char *symbol)
494 {
495 if (do_wide)
496 printf ("%s", symbol);
497 else if (width < 0)
498 printf ("%-*.*s", width, width, symbol);
499 else
500 printf ("%-.*s", width, symbol);
501 }
502
503 static bfd_vma
504 byte_get_big_endian (unsigned char *field, int size)
505 {
506 switch (size)
507 {
508 case 1:
509 return *field;
510
511 case 2:
512 return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
513
514 case 4:
515 return ((unsigned long) (field[3]))
516 | (((unsigned long) (field[2])) << 8)
517 | (((unsigned long) (field[1])) << 16)
518 | (((unsigned long) (field[0])) << 24);
519
520 #ifndef BFD64
521 case 8:
522 /* Although we are extracing data from an 8 byte wide field, we
523 are returning only 4 bytes of data. */
524 return ((unsigned long) (field[7]))
525 | (((unsigned long) (field[6])) << 8)
526 | (((unsigned long) (field[5])) << 16)
527 | (((unsigned long) (field[4])) << 24);
528 #else
529 case 8:
530 case -8:
531 /* This is a special case, generated by the BYTE_GET8 macro.
532 It means that we are loading an 8 byte value from a field
533 in an external structure into an 8 byte value in a field
534 in an internal strcuture. */
535 return ((bfd_vma) (field[7]))
536 | (((bfd_vma) (field[6])) << 8)
537 | (((bfd_vma) (field[5])) << 16)
538 | (((bfd_vma) (field[4])) << 24)
539 | (((bfd_vma) (field[3])) << 32)
540 | (((bfd_vma) (field[2])) << 40)
541 | (((bfd_vma) (field[1])) << 48)
542 | (((bfd_vma) (field[0])) << 56);
543 #endif
544
545 default:
546 error (_("Unhandled data length: %d\n"), size);
547 abort ();
548 }
549 }
550
551 static void
552 byte_put_big_endian (unsigned char *field, bfd_vma value, int size)
553 {
554 switch (size)
555 {
556 case 8:
557 field[7] = value & 0xff;
558 field[6] = (value >> 8) & 0xff;
559 field[5] = (value >> 16) & 0xff;
560 field[4] = (value >> 24) & 0xff;
561 value >>= 16;
562 value >>= 16;
563 /* Fall through. */
564 case 4:
565 field[3] = value & 0xff;
566 field[2] = (value >> 8) & 0xff;
567 value >>= 16;
568 /* Fall through. */
569 case 2:
570 field[1] = value & 0xff;
571 value >>= 8;
572 /* Fall through. */
573 case 1:
574 field[0] = value & 0xff;
575 break;
576
577 default:
578 error (_("Unhandled data length: %d\n"), size);
579 abort ();
580 }
581 }
582
583 /* Guess the relocation size commonly used by the specific machines. */
584
585 static int
586 guess_is_rela (unsigned long e_machine)
587 {
588 switch (e_machine)
589 {
590 /* Targets that use REL relocations. */
591 case EM_ARM:
592 case EM_386:
593 case EM_486:
594 case EM_960:
595 case EM_DLX:
596 case EM_OPENRISC:
597 case EM_OR32:
598 case EM_M32R:
599 case EM_CYGNUS_M32R:
600 case EM_D10V:
601 case EM_CYGNUS_D10V:
602 case EM_MIPS:
603 case EM_MIPS_RS3_LE:
604 return FALSE;
605
606 /* Targets that use RELA relocations. */
607 case EM_68K:
608 case EM_H8_300:
609 case EM_H8_300H:
610 case EM_H8S:
611 case EM_SPARC32PLUS:
612 case EM_SPARCV9:
613 case EM_SPARC:
614 case EM_PPC:
615 case EM_PPC64:
616 case EM_V850:
617 case EM_CYGNUS_V850:
618 case EM_D30V:
619 case EM_CYGNUS_D30V:
620 case EM_MN10200:
621 case EM_CYGNUS_MN10200:
622 case EM_MN10300:
623 case EM_CYGNUS_MN10300:
624 case EM_FR30:
625 case EM_CYGNUS_FR30:
626 case EM_CYGNUS_FRV:
627 case EM_SH:
628 case EM_ALPHA:
629 case EM_MCORE:
630 case EM_IA_64:
631 case EM_AVR:
632 case EM_AVR_OLD:
633 case EM_CRIS:
634 case EM_860:
635 case EM_X86_64:
636 case EM_S390:
637 case EM_S390_OLD:
638 case EM_MMIX:
639 case EM_MSP430:
640 case EM_MSP430_OLD:
641 case EM_XSTORMY16:
642 case EM_VAX:
643 case EM_IP2K:
644 case EM_IP2K_OLD:
645 case EM_IQ2000:
646 case EM_XTENSA:
647 case EM_XTENSA_OLD:
648 return TRUE;
649
650 case EM_MMA:
651 case EM_PCP:
652 case EM_NCPU:
653 case EM_NDR1:
654 case EM_STARCORE:
655 case EM_ME16:
656 case EM_ST100:
657 case EM_TINYJ:
658 case EM_FX66:
659 case EM_ST9PLUS:
660 case EM_ST7:
661 case EM_68HC16:
662 case EM_68HC11:
663 case EM_68HC08:
664 case EM_68HC05:
665 case EM_SVX:
666 case EM_ST19:
667 default:
668 warn (_("Don't know about relocations on this machine architecture\n"));
669 return FALSE;
670 }
671 }
672
673 static int
674 slurp_rela_relocs (FILE *file,
675 unsigned long rel_offset,
676 unsigned long rel_size,
677 Elf_Internal_Rela **relasp,
678 unsigned long *nrelasp)
679 {
680 Elf_Internal_Rela *relas;
681 unsigned long nrelas;
682 unsigned int i;
683
684 if (is_32bit_elf)
685 {
686 Elf32_External_Rela *erelas;
687
688 erelas = get_data (NULL, file, rel_offset, rel_size, _("relocs"));
689 if (!erelas)
690 return 0;
691
692 nrelas = rel_size / sizeof (Elf32_External_Rela);
693
694 relas = malloc (nrelas * sizeof (Elf_Internal_Rela));
695
696 if (relas == NULL)
697 {
698 error(_("out of memory parsing relocs"));
699 return 0;
700 }
701
702 for (i = 0; i < nrelas; i++)
703 {
704 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
705 relas[i].r_info = BYTE_GET (erelas[i].r_info);
706 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
707 }
708
709 free (erelas);
710 }
711 else
712 {
713 Elf64_External_Rela *erelas;
714
715 erelas = get_data (NULL, file, rel_offset, rel_size, _("relocs"));
716 if (!erelas)
717 return 0;
718
719 nrelas = rel_size / sizeof (Elf64_External_Rela);
720
721 relas = malloc (nrelas * sizeof (Elf_Internal_Rela));
722
723 if (relas == NULL)
724 {
725 error(_("out of memory parsing relocs"));
726 return 0;
727 }
728
729 for (i = 0; i < nrelas; i++)
730 {
731 relas[i].r_offset = BYTE_GET8 (erelas[i].r_offset);
732 relas[i].r_info = BYTE_GET8 (erelas[i].r_info);
733 relas[i].r_addend = BYTE_GET8 (erelas[i].r_addend);
734 }
735
736 free (erelas);
737 }
738 *relasp = relas;
739 *nrelasp = nrelas;
740 return 1;
741 }
742
743 static int
744 slurp_rel_relocs (FILE *file,
745 unsigned long rel_offset,
746 unsigned long rel_size,
747 Elf_Internal_Rela **relsp,
748 unsigned long *nrelsp)
749 {
750 Elf_Internal_Rela *rels;
751 unsigned long nrels;
752 unsigned int i;
753
754 if (is_32bit_elf)
755 {
756 Elf32_External_Rel *erels;
757
758 erels = get_data (NULL, file, rel_offset, rel_size, _("relocs"));
759 if (!erels)
760 return 0;
761
762 nrels = rel_size / sizeof (Elf32_External_Rel);
763
764 rels = malloc (nrels * sizeof (Elf_Internal_Rela));
765
766 if (rels == NULL)
767 {
768 error(_("out of memory parsing relocs"));
769 return 0;
770 }
771
772 for (i = 0; i < nrels; i++)
773 {
774 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
775 rels[i].r_info = BYTE_GET (erels[i].r_info);
776 rels[i].r_addend = 0;
777 }
778
779 free (erels);
780 }
781 else
782 {
783 Elf64_External_Rel *erels;
784
785 erels = get_data (NULL, file, rel_offset, rel_size, _("relocs"));
786 if (!erels)
787 return 0;
788
789 nrels = rel_size / sizeof (Elf64_External_Rel);
790
791 rels = malloc (nrels * sizeof (Elf_Internal_Rela));
792
793 if (rels == NULL)
794 {
795 error(_("out of memory parsing relocs"));
796 return 0;
797 }
798
799 for (i = 0; i < nrels; i++)
800 {
801 rels[i].r_offset = BYTE_GET8 (erels[i].r_offset);
802 rels[i].r_info = BYTE_GET8 (erels[i].r_info);
803 rels[i].r_addend = 0;
804 }
805
806 free (erels);
807 }
808 *relsp = rels;
809 *nrelsp = nrels;
810 return 1;
811 }
812
813 /* Display the contents of the relocation data found at the specified
814 offset. */
815
816 static int
817 dump_relocations (FILE *file,
818 unsigned long rel_offset,
819 unsigned long rel_size,
820 Elf_Internal_Sym *symtab,
821 unsigned long nsyms,
822 char *strtab,
823 int is_rela)
824 {
825 unsigned int i;
826 Elf_Internal_Rela *rels;
827
828
829 if (is_rela == UNKNOWN)
830 is_rela = guess_is_rela (elf_header.e_machine);
831
832 if (is_rela)
833 {
834 if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
835 return 0;
836 }
837 else
838 {
839 if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
840 return 0;
841 }
842
843 if (is_32bit_elf)
844 {
845 if (is_rela)
846 {
847 if (do_wide)
848 printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
849 else
850 printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
851 }
852 else
853 {
854 if (do_wide)
855 printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
856 else
857 printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
858 }
859 }
860 else
861 {
862 if (is_rela)
863 {
864 if (do_wide)
865 printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
866 else
867 printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
868 }
869 else
870 {
871 if (do_wide)
872 printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
873 else
874 printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
875 }
876 }
877
878 for (i = 0; i < rel_size; i++)
879 {
880 const char *rtype;
881 const char *rtype2 = NULL;
882 const char *rtype3 = NULL;
883 bfd_vma offset;
884 bfd_vma info;
885 bfd_vma symtab_index;
886 bfd_vma type;
887 bfd_vma type2 = 0;
888 bfd_vma type3 = 0;
889
890 offset = rels[i].r_offset;
891 info = rels[i].r_info;
892
893 if (is_32bit_elf)
894 {
895 type = ELF32_R_TYPE (info);
896 symtab_index = ELF32_R_SYM (info);
897 }
898 else
899 {
900 /* The #ifdef BFD64 below is to prevent a compile time warning.
901 We know that if we do not have a 64 bit data type that we
902 will never execute this code anyway. */
903 #ifdef BFD64
904 if (elf_header.e_machine == EM_MIPS)
905 {
906 /* In little-endian objects, r_info isn't really a 64-bit
907 little-endian value: it has a 32-bit little-endian
908 symbol index followed by four individual byte fields.
909 Reorder INFO accordingly. */
910 if (elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
911 info = (((info & 0xffffffff) << 32)
912 | ((info >> 56) & 0xff)
913 | ((info >> 40) & 0xff00)
914 | ((info >> 24) & 0xff0000)
915 | ((info >> 8) & 0xff000000));
916 type = ELF64_MIPS_R_TYPE (info);
917 type2 = ELF64_MIPS_R_TYPE2 (info);
918 type3 = ELF64_MIPS_R_TYPE3 (info);
919 }
920 else if (elf_header.e_machine == EM_SPARCV9)
921 type = ELF64_R_TYPE_ID (info);
922 else
923 type = ELF64_R_TYPE (info);
924
925 symtab_index = ELF64_R_SYM (info);
926 #endif
927 }
928
929 if (is_32bit_elf)
930 {
931 #ifdef _bfd_int64_low
932 printf ("%8.8lx %8.8lx ", _bfd_int64_low (offset), _bfd_int64_low (info));
933 #else
934 printf ("%8.8lx %8.8lx ", offset, info);
935 #endif
936 }
937 else
938 {
939 #ifdef _bfd_int64_low
940 printf (do_wide
941 ? "%8.8lx%8.8lx %8.8lx%8.8lx "
942 : "%4.4lx%8.8lx %4.4lx%8.8lx ",
943 _bfd_int64_high (offset),
944 _bfd_int64_low (offset),
945 _bfd_int64_high (info),
946 _bfd_int64_low (info));
947 #else
948 printf (do_wide
949 ? "%16.16lx %16.16lx "
950 : "%12.12lx %12.12lx ",
951 offset, info);
952 #endif
953 }
954
955 switch (elf_header.e_machine)
956 {
957 default:
958 rtype = NULL;
959 break;
960
961 case EM_M32R:
962 case EM_CYGNUS_M32R:
963 rtype = elf_m32r_reloc_type (type);
964 break;
965
966 case EM_386:
967 case EM_486:
968 rtype = elf_i386_reloc_type (type);
969 break;
970
971 case EM_68HC11:
972 case EM_68HC12:
973 rtype = elf_m68hc11_reloc_type (type);
974 break;
975
976 case EM_68K:
977 rtype = elf_m68k_reloc_type (type);
978 break;
979
980 case EM_960:
981 rtype = elf_i960_reloc_type (type);
982 break;
983
984 case EM_AVR:
985 case EM_AVR_OLD:
986 rtype = elf_avr_reloc_type (type);
987 break;
988
989 case EM_OLD_SPARCV9:
990 case EM_SPARC32PLUS:
991 case EM_SPARCV9:
992 case EM_SPARC:
993 rtype = elf_sparc_reloc_type (type);
994 break;
995
996 case EM_V850:
997 case EM_CYGNUS_V850:
998 rtype = v850_reloc_type (type);
999 break;
1000
1001 case EM_D10V:
1002 case EM_CYGNUS_D10V:
1003 rtype = elf_d10v_reloc_type (type);
1004 break;
1005
1006 case EM_D30V:
1007 case EM_CYGNUS_D30V:
1008 rtype = elf_d30v_reloc_type (type);
1009 break;
1010
1011 case EM_DLX:
1012 rtype = elf_dlx_reloc_type (type);
1013 break;
1014
1015 case EM_SH:
1016 rtype = elf_sh_reloc_type (type);
1017 break;
1018
1019 case EM_MN10300:
1020 case EM_CYGNUS_MN10300:
1021 rtype = elf_mn10300_reloc_type (type);
1022 break;
1023
1024 case EM_MN10200:
1025 case EM_CYGNUS_MN10200:
1026 rtype = elf_mn10200_reloc_type (type);
1027 break;
1028
1029 case EM_FR30:
1030 case EM_CYGNUS_FR30:
1031 rtype = elf_fr30_reloc_type (type);
1032 break;
1033
1034 case EM_CYGNUS_FRV:
1035 rtype = elf_frv_reloc_type (type);
1036 break;
1037
1038 case EM_MCORE:
1039 rtype = elf_mcore_reloc_type (type);
1040 break;
1041
1042 case EM_MMIX:
1043 rtype = elf_mmix_reloc_type (type);
1044 break;
1045
1046 case EM_MSP430:
1047 case EM_MSP430_OLD:
1048 rtype = elf_msp430_reloc_type (type);
1049 break;
1050
1051 case EM_PPC:
1052 rtype = elf_ppc_reloc_type (type);
1053 break;
1054
1055 case EM_PPC64:
1056 rtype = elf_ppc64_reloc_type (type);
1057 break;
1058
1059 case EM_MIPS:
1060 case EM_MIPS_RS3_LE:
1061 rtype = elf_mips_reloc_type (type);
1062 if (!is_32bit_elf)
1063 {
1064 rtype2 = elf_mips_reloc_type (type2);
1065 rtype3 = elf_mips_reloc_type (type3);
1066 }
1067 break;
1068
1069 case EM_ALPHA:
1070 rtype = elf_alpha_reloc_type (type);
1071 break;
1072
1073 case EM_ARM:
1074 rtype = elf_arm_reloc_type (type);
1075 break;
1076
1077 case EM_ARC:
1078 rtype = elf_arc_reloc_type (type);
1079 break;
1080
1081 case EM_PARISC:
1082 rtype = elf_hppa_reloc_type (type);
1083 break;
1084
1085 case EM_H8_300:
1086 case EM_H8_300H:
1087 case EM_H8S:
1088 rtype = elf_h8_reloc_type (type);
1089 break;
1090
1091 case EM_OPENRISC:
1092 case EM_OR32:
1093 rtype = elf_or32_reloc_type (type);
1094 break;
1095
1096 case EM_PJ:
1097 case EM_PJ_OLD:
1098 rtype = elf_pj_reloc_type (type);
1099 break;
1100 case EM_IA_64:
1101 rtype = elf_ia64_reloc_type (type);
1102 break;
1103
1104 case EM_CRIS:
1105 rtype = elf_cris_reloc_type (type);
1106 break;
1107
1108 case EM_860:
1109 rtype = elf_i860_reloc_type (type);
1110 break;
1111
1112 case EM_X86_64:
1113 rtype = elf_x86_64_reloc_type (type);
1114 break;
1115
1116 case EM_S370:
1117 rtype = i370_reloc_type (type);
1118 break;
1119
1120 case EM_S390_OLD:
1121 case EM_S390:
1122 rtype = elf_s390_reloc_type (type);
1123 break;
1124
1125 case EM_XSTORMY16:
1126 rtype = elf_xstormy16_reloc_type (type);
1127 break;
1128
1129 case EM_VAX:
1130 rtype = elf_vax_reloc_type (type);
1131 break;
1132
1133 case EM_IP2K:
1134 case EM_IP2K_OLD:
1135 rtype = elf_ip2k_reloc_type (type);
1136 break;
1137
1138 case EM_IQ2000:
1139 rtype = elf_iq2000_reloc_type (type);
1140 break;
1141
1142 case EM_XTENSA_OLD:
1143 case EM_XTENSA:
1144 rtype = elf_xtensa_reloc_type (type);
1145 break;
1146 }
1147
1148 if (rtype == NULL)
1149 #ifdef _bfd_int64_low
1150 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type));
1151 #else
1152 printf (_("unrecognized: %-7lx"), type);
1153 #endif
1154 else
1155 printf (do_wide ? "%-22.22s" : "%-17.17s", rtype);
1156
1157 if (symtab_index)
1158 {
1159 if (symtab == NULL || symtab_index >= nsyms)
1160 printf (" bad symbol index: %08lx", (unsigned long) symtab_index);
1161 else
1162 {
1163 Elf_Internal_Sym *psym;
1164
1165 psym = symtab + symtab_index;
1166
1167 printf (" ");
1168 print_vma (psym->st_value, LONG_HEX);
1169 printf (is_32bit_elf ? " " : " ");
1170
1171 if (psym->st_name == 0)
1172 {
1173 const char *sec_name = "<null>";
1174 char name_buf[40];
1175
1176 if (ELF_ST_TYPE (psym->st_info) == STT_SECTION)
1177 {
1178 bfd_vma sec_index = (bfd_vma) -1;
1179
1180 if (psym->st_shndx < SHN_LORESERVE)
1181 sec_index = psym->st_shndx;
1182 else if (psym->st_shndx > SHN_LORESERVE)
1183 sec_index = psym->st_shndx - (SHN_HIRESERVE + 1
1184 - SHN_LORESERVE);
1185
1186 if (sec_index != (bfd_vma) -1)
1187 sec_name = SECTION_NAME (section_headers + sec_index);
1188 else if (psym->st_shndx == SHN_ABS)
1189 sec_name = "ABS";
1190 else if (psym->st_shndx == SHN_COMMON)
1191 sec_name = "COMMON";
1192 else
1193 {
1194 sprintf (name_buf, "<section 0x%x>",
1195 (unsigned int) psym->st_shndx);
1196 sec_name = name_buf;
1197 }
1198 }
1199 print_symbol (22, sec_name);
1200 }
1201 else if (strtab == NULL)
1202 printf (_("<string table index %3ld>"), psym->st_name);
1203 else
1204 print_symbol (22, strtab + psym->st_name);
1205
1206 if (is_rela)
1207 printf (" + %lx", (unsigned long) rels[i].r_addend);
1208 }
1209 }
1210 else if (is_rela)
1211 {
1212 printf ("%*c", is_32bit_elf ? (do_wide ? 34 : 28) : (do_wide ? 26 : 20), ' ');
1213 print_vma (rels[i].r_addend, LONG_HEX);
1214 }
1215
1216 if (elf_header.e_machine == EM_SPARCV9
1217 && !strcmp (rtype, "R_SPARC_OLO10"))
1218 printf (" + %lx", (unsigned long) ELF64_R_TYPE_DATA (info));
1219
1220 putchar ('\n');
1221
1222 if (! is_32bit_elf && elf_header.e_machine == EM_MIPS)
1223 {
1224 printf (" Type2: ");
1225
1226 if (rtype2 == NULL)
1227 #ifdef _bfd_int64_low
1228 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type2));
1229 #else
1230 printf (_("unrecognized: %-7lx"), type2);
1231 #endif
1232 else
1233 printf ("%-17.17s", rtype2);
1234
1235 printf("\n Type3: ");
1236
1237 if (rtype3 == NULL)
1238 #ifdef _bfd_int64_low
1239 printf (_("unrecognized: %-7lx"), _bfd_int64_low (type3));
1240 #else
1241 printf (_("unrecognized: %-7lx"), type3);
1242 #endif
1243 else
1244 printf ("%-17.17s", rtype3);
1245
1246 putchar ('\n');
1247 }
1248 }
1249
1250 free (rels);
1251
1252 return 1;
1253 }
1254
1255 static const char *
1256 get_mips_dynamic_type (unsigned long type)
1257 {
1258 switch (type)
1259 {
1260 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
1261 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
1262 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
1263 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
1264 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
1265 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
1266 case DT_MIPS_MSYM: return "MIPS_MSYM";
1267 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
1268 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
1269 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
1270 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
1271 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
1272 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
1273 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
1274 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
1275 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
1276 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
1277 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
1278 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
1279 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
1280 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
1281 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
1282 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
1283 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
1284 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
1285 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
1286 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
1287 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
1288 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
1289 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
1290 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
1291 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
1292 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
1293 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
1294 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
1295 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
1296 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
1297 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
1298 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
1299 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
1300 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
1301 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
1302 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
1303 default:
1304 return NULL;
1305 }
1306 }
1307
1308 static const char *
1309 get_sparc64_dynamic_type (unsigned long type)
1310 {
1311 switch (type)
1312 {
1313 case DT_SPARC_REGISTER: return "SPARC_REGISTER";
1314 default:
1315 return NULL;
1316 }
1317 }
1318
1319 static const char *
1320 get_ppc64_dynamic_type (unsigned long type)
1321 {
1322 switch (type)
1323 {
1324 case DT_PPC64_GLINK: return "PPC64_GLINK";
1325 case DT_PPC64_OPD: return "PPC64_OPD";
1326 case DT_PPC64_OPDSZ: return "PPC64_OPDSZ";
1327 default:
1328 return NULL;
1329 }
1330 }
1331
1332 static const char *
1333 get_parisc_dynamic_type (unsigned long type)
1334 {
1335 switch (type)
1336 {
1337 case DT_HP_LOAD_MAP: return "HP_LOAD_MAP";
1338 case DT_HP_DLD_FLAGS: return "HP_DLD_FLAGS";
1339 case DT_HP_DLD_HOOK: return "HP_DLD_HOOK";
1340 case DT_HP_UX10_INIT: return "HP_UX10_INIT";
1341 case DT_HP_UX10_INITSZ: return "HP_UX10_INITSZ";
1342 case DT_HP_PREINIT: return "HP_PREINIT";
1343 case DT_HP_PREINITSZ: return "HP_PREINITSZ";
1344 case DT_HP_NEEDED: return "HP_NEEDED";
1345 case DT_HP_TIME_STAMP: return "HP_TIME_STAMP";
1346 case DT_HP_CHECKSUM: return "HP_CHECKSUM";
1347 case DT_HP_GST_SIZE: return "HP_GST_SIZE";
1348 case DT_HP_GST_VERSION: return "HP_GST_VERSION";
1349 case DT_HP_GST_HASHVAL: return "HP_GST_HASHVAL";
1350 default:
1351 return NULL;
1352 }
1353 }
1354
1355 static const char *
1356 get_ia64_dynamic_type (unsigned long type)
1357 {
1358 switch (type)
1359 {
1360 case DT_IA_64_PLT_RESERVE: return "IA_64_PLT_RESERVE";
1361 default:
1362 return NULL;
1363 }
1364 }
1365
1366 static const char *
1367 get_dynamic_type (unsigned long type)
1368 {
1369 static char buff[32];
1370
1371 switch (type)
1372 {
1373 case DT_NULL: return "NULL";
1374 case DT_NEEDED: return "NEEDED";
1375 case DT_PLTRELSZ: return "PLTRELSZ";
1376 case DT_PLTGOT: return "PLTGOT";
1377 case DT_HASH: return "HASH";
1378 case DT_STRTAB: return "STRTAB";
1379 case DT_SYMTAB: return "SYMTAB";
1380 case DT_RELA: return "RELA";
1381 case DT_RELASZ: return "RELASZ";
1382 case DT_RELAENT: return "RELAENT";
1383 case DT_STRSZ: return "STRSZ";
1384 case DT_SYMENT: return "SYMENT";
1385 case DT_INIT: return "INIT";
1386 case DT_FINI: return "FINI";
1387 case DT_SONAME: return "SONAME";
1388 case DT_RPATH: return "RPATH";
1389 case DT_SYMBOLIC: return "SYMBOLIC";
1390 case DT_REL: return "REL";
1391 case DT_RELSZ: return "RELSZ";
1392 case DT_RELENT: return "RELENT";
1393 case DT_PLTREL: return "PLTREL";
1394 case DT_DEBUG: return "DEBUG";
1395 case DT_TEXTREL: return "TEXTREL";
1396 case DT_JMPREL: return "JMPREL";
1397 case DT_BIND_NOW: return "BIND_NOW";
1398 case DT_INIT_ARRAY: return "INIT_ARRAY";
1399 case DT_FINI_ARRAY: return "FINI_ARRAY";
1400 case DT_INIT_ARRAYSZ: return "INIT_ARRAYSZ";
1401 case DT_FINI_ARRAYSZ: return "FINI_ARRAYSZ";
1402 case DT_RUNPATH: return "RUNPATH";
1403 case DT_FLAGS: return "FLAGS";
1404
1405 case DT_PREINIT_ARRAY: return "PREINIT_ARRAY";
1406 case DT_PREINIT_ARRAYSZ: return "PREINIT_ARRAYSZ";
1407
1408 case DT_CHECKSUM: return "CHECKSUM";
1409 case DT_PLTPADSZ: return "PLTPADSZ";
1410 case DT_MOVEENT: return "MOVEENT";
1411 case DT_MOVESZ: return "MOVESZ";
1412 case DT_FEATURE: return "FEATURE";
1413 case DT_POSFLAG_1: return "POSFLAG_1";
1414 case DT_SYMINSZ: return "SYMINSZ";
1415 case DT_SYMINENT: return "SYMINENT"; /* aka VALRNGHI */
1416
1417 case DT_ADDRRNGLO: return "ADDRRNGLO";
1418 case DT_CONFIG: return "CONFIG";
1419 case DT_DEPAUDIT: return "DEPAUDIT";
1420 case DT_AUDIT: return "AUDIT";
1421 case DT_PLTPAD: return "PLTPAD";
1422 case DT_MOVETAB: return "MOVETAB";
1423 case DT_SYMINFO: return "SYMINFO"; /* aka ADDRRNGHI */
1424
1425 case DT_VERSYM: return "VERSYM";
1426
1427 case DT_RELACOUNT: return "RELACOUNT";
1428 case DT_RELCOUNT: return "RELCOUNT";
1429 case DT_FLAGS_1: return "FLAGS_1";
1430 case DT_VERDEF: return "VERDEF";
1431 case DT_VERDEFNUM: return "VERDEFNUM";
1432 case DT_VERNEED: return "VERNEED";
1433 case DT_VERNEEDNUM: return "VERNEEDNUM";
1434
1435 case DT_AUXILIARY: return "AUXILIARY";
1436 case DT_USED: return "USED";
1437 case DT_FILTER: return "FILTER";
1438
1439 case DT_GNU_PRELINKED: return "GNU_PRELINKED";
1440 case DT_GNU_CONFLICT: return "GNU_CONFLICT";
1441 case DT_GNU_CONFLICTSZ: return "GNU_CONFLICTSZ";
1442 case DT_GNU_LIBLIST: return "GNU_LIBLIST";
1443 case DT_GNU_LIBLISTSZ: return "GNU_LIBLISTSZ";
1444
1445 default:
1446 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
1447 {
1448 const char *result;
1449
1450 switch (elf_header.e_machine)
1451 {
1452 case EM_MIPS:
1453 case EM_MIPS_RS3_LE:
1454 result = get_mips_dynamic_type (type);
1455 break;
1456 case EM_SPARCV9:
1457 result = get_sparc64_dynamic_type (type);
1458 break;
1459 case EM_PPC64:
1460 result = get_ppc64_dynamic_type (type);
1461 break;
1462 case EM_IA_64:
1463 result = get_ia64_dynamic_type (type);
1464 break;
1465 default:
1466 result = NULL;
1467 break;
1468 }
1469
1470 if (result != NULL)
1471 return result;
1472
1473 sprintf (buff, _("Processor Specific: %lx"), type);
1474 }
1475 else if ((type >= DT_LOOS) && (type <= DT_HIOS))
1476 {
1477 const char *result;
1478
1479 switch (elf_header.e_machine)
1480 {
1481 case EM_PARISC:
1482 result = get_parisc_dynamic_type (type);
1483 break;
1484 default:
1485 result = NULL;
1486 break;
1487 }
1488
1489 if (result != NULL)
1490 return result;
1491
1492 sprintf (buff, _("Operating System specific: %lx"), type);
1493 }
1494 else
1495 sprintf (buff, _("<unknown>: %lx"), type);
1496
1497 return buff;
1498 }
1499 }
1500
1501 static char *
1502 get_file_type (unsigned e_type)
1503 {
1504 static char buff[32];
1505
1506 switch (e_type)
1507 {
1508 case ET_NONE: return _("NONE (None)");
1509 case ET_REL: return _("REL (Relocatable file)");
1510 case ET_EXEC: return _("EXEC (Executable file)");
1511 case ET_DYN: return _("DYN (Shared object file)");
1512 case ET_CORE: return _("CORE (Core file)");
1513
1514 default:
1515 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
1516 sprintf (buff, _("Processor Specific: (%x)"), e_type);
1517 else if ((e_type >= ET_LOOS) && (e_type <= ET_HIOS))
1518 sprintf (buff, _("OS Specific: (%x)"), e_type);
1519 else
1520 sprintf (buff, _("<unknown>: %x"), e_type);
1521 return buff;
1522 }
1523 }
1524
1525 static char *
1526 get_machine_name (unsigned e_machine)
1527 {
1528 static char buff[64]; /* XXX */
1529
1530 switch (e_machine)
1531 {
1532 case EM_NONE: return _("None");
1533 case EM_M32: return "WE32100";
1534 case EM_SPARC: return "Sparc";
1535 case EM_386: return "Intel 80386";
1536 case EM_68K: return "MC68000";
1537 case EM_88K: return "MC88000";
1538 case EM_486: return "Intel 80486";
1539 case EM_860: return "Intel 80860";
1540 case EM_MIPS: return "MIPS R3000";
1541 case EM_S370: return "IBM System/370";
1542 case EM_MIPS_RS3_LE: return "MIPS R4000 big-endian";
1543 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
1544 case EM_PARISC: return "HPPA";
1545 case EM_PPC_OLD: return "Power PC (old)";
1546 case EM_SPARC32PLUS: return "Sparc v8+" ;
1547 case EM_960: return "Intel 90860";
1548 case EM_PPC: return "PowerPC";
1549 case EM_PPC64: return "PowerPC64";
1550 case EM_V800: return "NEC V800";
1551 case EM_FR20: return "Fujitsu FR20";
1552 case EM_RH32: return "TRW RH32";
1553 case EM_MCORE: return "MCORE";
1554 case EM_ARM: return "ARM";
1555 case EM_OLD_ALPHA: return "Digital Alpha (old)";
1556 case EM_SH: return "Renesas / SuperH SH";
1557 case EM_SPARCV9: return "Sparc v9";
1558 case EM_TRICORE: return "Siemens Tricore";
1559 case EM_ARC: return "ARC";
1560 case EM_H8_300: return "Renesas H8/300";
1561 case EM_H8_300H: return "Renesas H8/300H";
1562 case EM_H8S: return "Renesas H8S";
1563 case EM_H8_500: return "Renesas H8/500";
1564 case EM_IA_64: return "Intel IA-64";
1565 case EM_MIPS_X: return "Stanford MIPS-X";
1566 case EM_COLDFIRE: return "Motorola Coldfire";
1567 case EM_68HC12: return "Motorola M68HC12";
1568 case EM_ALPHA: return "Alpha";
1569 case EM_CYGNUS_D10V:
1570 case EM_D10V: return "d10v";
1571 case EM_CYGNUS_D30V:
1572 case EM_D30V: return "d30v";
1573 case EM_CYGNUS_M32R:
1574 case EM_M32R: return "Renesas M32R (formerly Mitsubishi M32r)";
1575 case EM_CYGNUS_V850:
1576 case EM_V850: return "NEC v850";
1577 case EM_CYGNUS_MN10300:
1578 case EM_MN10300: return "mn10300";
1579 case EM_CYGNUS_MN10200:
1580 case EM_MN10200: return "mn10200";
1581 case EM_CYGNUS_FR30:
1582 case EM_FR30: return "Fujitsu FR30";
1583 case EM_CYGNUS_FRV: return "Fujitsu FR-V";
1584 case EM_PJ_OLD:
1585 case EM_PJ: return "picoJava";
1586 case EM_MMA: return "Fujitsu Multimedia Accelerator";
1587 case EM_PCP: return "Siemens PCP";
1588 case EM_NCPU: return "Sony nCPU embedded RISC processor";
1589 case EM_NDR1: return "Denso NDR1 microprocesspr";
1590 case EM_STARCORE: return "Motorola Star*Core processor";
1591 case EM_ME16: return "Toyota ME16 processor";
1592 case EM_ST100: return "STMicroelectronics ST100 processor";
1593 case EM_TINYJ: return "Advanced Logic Corp. TinyJ embedded processor";
1594 case EM_FX66: return "Siemens FX66 microcontroller";
1595 case EM_ST9PLUS: return "STMicroelectronics ST9+ 8/16 bit microcontroller";
1596 case EM_ST7: return "STMicroelectronics ST7 8-bit microcontroller";
1597 case EM_68HC16: return "Motorola MC68HC16 Microcontroller";
1598 case EM_68HC11: return "Motorola MC68HC11 Microcontroller";
1599 case EM_68HC08: return "Motorola MC68HC08 Microcontroller";
1600 case EM_68HC05: return "Motorola MC68HC05 Microcontroller";
1601 case EM_SVX: return "Silicon Graphics SVx";
1602 case EM_ST19: return "STMicroelectronics ST19 8-bit microcontroller";
1603 case EM_VAX: return "Digital VAX";
1604 case EM_AVR_OLD:
1605 case EM_AVR: return "Atmel AVR 8-bit microcontroller";
1606 case EM_CRIS: return "Axis Communications 32-bit embedded processor";
1607 case EM_JAVELIN: return "Infineon Technologies 32-bit embedded cpu";
1608 case EM_FIREPATH: return "Element 14 64-bit DSP processor";
1609 case EM_ZSP: return "LSI Logic's 16-bit DSP processor";
1610 case EM_MMIX: return "Donald Knuth's educational 64-bit processor";
1611 case EM_HUANY: return "Harvard Universitys's machine-independent object format";
1612 case EM_PRISM: return "Vitesse Prism";
1613 case EM_X86_64: return "Advanced Micro Devices X86-64";
1614 case EM_S390_OLD:
1615 case EM_S390: return "IBM S/390";
1616 case EM_XSTORMY16: return "Sanyo Xstormy16 CPU core";
1617 case EM_OPENRISC:
1618 case EM_OR32: return "OpenRISC";
1619 case EM_DLX: return "OpenDLX";
1620 case EM_IP2K_OLD:
1621 case EM_IP2K: return "Ubicom IP2xxx 8-bit microcontrollers";
1622 case EM_IQ2000: return "Vitesse IQ2000";
1623 case EM_XTENSA_OLD:
1624 case EM_XTENSA: return "Tensilica Xtensa Processor";
1625 default:
1626 sprintf (buff, _("<unknown>: %x"), e_machine);
1627 return buff;
1628 }
1629 }
1630
1631 static void
1632 decode_ARM_machine_flags (unsigned e_flags, char buf[])
1633 {
1634 unsigned eabi;
1635 int unknown = 0;
1636
1637 eabi = EF_ARM_EABI_VERSION (e_flags);
1638 e_flags &= ~ EF_ARM_EABIMASK;
1639
1640 /* Handle "generic" ARM flags. */
1641 if (e_flags & EF_ARM_RELEXEC)
1642 {
1643 strcat (buf, ", relocatable executable");
1644 e_flags &= ~ EF_ARM_RELEXEC;
1645 }
1646
1647 if (e_flags & EF_ARM_HASENTRY)
1648 {
1649 strcat (buf, ", has entry point");
1650 e_flags &= ~ EF_ARM_HASENTRY;
1651 }
1652
1653 /* Now handle EABI specific flags. */
1654 switch (eabi)
1655 {
1656 default:
1657 strcat (buf, ", <unrecognized EABI>");
1658 if (e_flags)
1659 unknown = 1;
1660 break;
1661
1662 case EF_ARM_EABI_VER1:
1663 strcat (buf, ", Version1 EABI");
1664 while (e_flags)
1665 {
1666 unsigned flag;
1667
1668 /* Process flags one bit at a time. */
1669 flag = e_flags & - e_flags;
1670 e_flags &= ~ flag;
1671
1672 switch (flag)
1673 {
1674 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1675 strcat (buf, ", sorted symbol tables");
1676 break;
1677
1678 default:
1679 unknown = 1;
1680 break;
1681 }
1682 }
1683 break;
1684
1685 case EF_ARM_EABI_VER2:
1686 strcat (buf, ", Version2 EABI");
1687 while (e_flags)
1688 {
1689 unsigned flag;
1690
1691 /* Process flags one bit at a time. */
1692 flag = e_flags & - e_flags;
1693 e_flags &= ~ flag;
1694
1695 switch (flag)
1696 {
1697 case EF_ARM_SYMSARESORTED: /* Conflicts with EF_ARM_INTERWORK. */
1698 strcat (buf, ", sorted symbol tables");
1699 break;
1700
1701 case EF_ARM_DYNSYMSUSESEGIDX:
1702 strcat (buf, ", dynamic symbols use segment index");
1703 break;
1704
1705 case EF_ARM_MAPSYMSFIRST:
1706 strcat (buf, ", mapping symbols precede others");
1707 break;
1708
1709 default:
1710 unknown = 1;
1711 break;
1712 }
1713 }
1714 break;
1715
1716 case EF_ARM_EABI_UNKNOWN:
1717 strcat (buf, ", GNU EABI");
1718 while (e_flags)
1719 {
1720 unsigned flag;
1721
1722 /* Process flags one bit at a time. */
1723 flag = e_flags & - e_flags;
1724 e_flags &= ~ flag;
1725
1726 switch (flag)
1727 {
1728 case EF_ARM_INTERWORK:
1729 strcat (buf, ", interworking enabled");
1730 break;
1731
1732 case EF_ARM_APCS_26:
1733 strcat (buf, ", uses APCS/26");
1734 break;
1735
1736 case EF_ARM_APCS_FLOAT:
1737 strcat (buf, ", uses APCS/float");
1738 break;
1739
1740 case EF_ARM_PIC:
1741 strcat (buf, ", position independent");
1742 break;
1743
1744 case EF_ARM_ALIGN8:
1745 strcat (buf, ", 8 bit structure alignment");
1746 break;
1747
1748 case EF_ARM_NEW_ABI:
1749 strcat (buf, ", uses new ABI");
1750 break;
1751
1752 case EF_ARM_OLD_ABI:
1753 strcat (buf, ", uses old ABI");
1754 break;
1755
1756 case EF_ARM_SOFT_FLOAT:
1757 strcat (buf, ", software FP");
1758 break;
1759
1760 case EF_ARM_MAVERICK_FLOAT:
1761 strcat (buf, ", Maverick FP");
1762 break;
1763
1764 default:
1765 unknown = 1;
1766 break;
1767 }
1768 }
1769 }
1770
1771 if (unknown)
1772 strcat (buf,", <unknown>");
1773 }
1774
1775 static char *
1776 get_machine_flags (unsigned e_flags, unsigned e_machine)
1777 {
1778 static char buf[1024];
1779
1780 buf[0] = '\0';
1781
1782 if (e_flags)
1783 {
1784 switch (e_machine)
1785 {
1786 default:
1787 break;
1788
1789 case EM_ARM:
1790 decode_ARM_machine_flags (e_flags, buf);
1791 break;
1792
1793 case EM_68K:
1794 if (e_flags & EF_CPU32)
1795 strcat (buf, ", cpu32");
1796 if (e_flags & EF_M68000)
1797 strcat (buf, ", m68000");
1798 break;
1799
1800 case EM_PPC:
1801 if (e_flags & EF_PPC_EMB)
1802 strcat (buf, ", emb");
1803
1804 if (e_flags & EF_PPC_RELOCATABLE)
1805 strcat (buf, ", relocatable");
1806
1807 if (e_flags & EF_PPC_RELOCATABLE_LIB)
1808 strcat (buf, ", relocatable-lib");
1809 break;
1810
1811 case EM_V850:
1812 case EM_CYGNUS_V850:
1813 switch (e_flags & EF_V850_ARCH)
1814 {
1815 case E_V850E_ARCH:
1816 strcat (buf, ", v850e");
1817 break;
1818 case E_V850_ARCH:
1819 strcat (buf, ", v850");
1820 break;
1821 default:
1822 strcat (buf, ", unknown v850 architecture variant");
1823 break;
1824 }
1825 break;
1826
1827 case EM_M32R:
1828 case EM_CYGNUS_M32R:
1829 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
1830 strcat (buf, ", m32r");
1831
1832 break;
1833
1834 case EM_MIPS:
1835 case EM_MIPS_RS3_LE:
1836 if (e_flags & EF_MIPS_NOREORDER)
1837 strcat (buf, ", noreorder");
1838
1839 if (e_flags & EF_MIPS_PIC)
1840 strcat (buf, ", pic");
1841
1842 if (e_flags & EF_MIPS_CPIC)
1843 strcat (buf, ", cpic");
1844
1845 if (e_flags & EF_MIPS_UCODE)
1846 strcat (buf, ", ugen_reserved");
1847
1848 if (e_flags & EF_MIPS_ABI2)
1849 strcat (buf, ", abi2");
1850
1851 if (e_flags & EF_MIPS_OPTIONS_FIRST)
1852 strcat (buf, ", odk first");
1853
1854 if (e_flags & EF_MIPS_32BITMODE)
1855 strcat (buf, ", 32bitmode");
1856
1857 switch ((e_flags & EF_MIPS_MACH))
1858 {
1859 case E_MIPS_MACH_3900: strcat (buf, ", 3900"); break;
1860 case E_MIPS_MACH_4010: strcat (buf, ", 4010"); break;
1861 case E_MIPS_MACH_4100: strcat (buf, ", 4100"); break;
1862 case E_MIPS_MACH_4111: strcat (buf, ", 4111"); break;
1863 case E_MIPS_MACH_4120: strcat (buf, ", 4120"); break;
1864 case E_MIPS_MACH_4650: strcat (buf, ", 4650"); break;
1865 case E_MIPS_MACH_5400: strcat (buf, ", 5400"); break;
1866 case E_MIPS_MACH_5500: strcat (buf, ", 5500"); break;
1867 case E_MIPS_MACH_SB1: strcat (buf, ", sb1"); break;
1868 case 0:
1869 /* We simply ignore the field in this case to avoid confusion:
1870 MIPS ELF does not specify EF_MIPS_MACH, it is a GNU
1871 extension. */
1872 break;
1873 default: strcat (buf, ", unknown CPU"); break;
1874 }
1875
1876 switch ((e_flags & EF_MIPS_ABI))
1877 {
1878 case E_MIPS_ABI_O32: strcat (buf, ", o32"); break;
1879 case E_MIPS_ABI_O64: strcat (buf, ", o64"); break;
1880 case E_MIPS_ABI_EABI32: strcat (buf, ", eabi32"); break;
1881 case E_MIPS_ABI_EABI64: strcat (buf, ", eabi64"); break;
1882 case 0:
1883 /* We simply ignore the field in this case to avoid confusion:
1884 MIPS ELF does not specify EF_MIPS_ABI, it is a GNU extension.
1885 This means it is likely to be an o32 file, but not for
1886 sure. */
1887 break;
1888 default: strcat (buf, ", unknown ABI"); break;
1889 }
1890
1891 if (e_flags & EF_MIPS_ARCH_ASE_MDMX)
1892 strcat (buf, ", mdmx");
1893
1894 if (e_flags & EF_MIPS_ARCH_ASE_M16)
1895 strcat (buf, ", mips16");
1896
1897 switch ((e_flags & EF_MIPS_ARCH))
1898 {
1899 case E_MIPS_ARCH_1: strcat (buf, ", mips1"); break;
1900 case E_MIPS_ARCH_2: strcat (buf, ", mips2"); break;
1901 case E_MIPS_ARCH_3: strcat (buf, ", mips3"); break;
1902 case E_MIPS_ARCH_4: strcat (buf, ", mips4"); break;
1903 case E_MIPS_ARCH_5: strcat (buf, ", mips5"); break;
1904 case E_MIPS_ARCH_32: strcat (buf, ", mips32"); break;
1905 case E_MIPS_ARCH_32R2: strcat (buf, ", mips32r2"); break;
1906 case E_MIPS_ARCH_64: strcat (buf, ", mips64"); break;
1907 default: strcat (buf, ", unknown ISA"); break;
1908 }
1909
1910 break;
1911
1912 case EM_SPARCV9:
1913 if (e_flags & EF_SPARC_32PLUS)
1914 strcat (buf, ", v8+");
1915
1916 if (e_flags & EF_SPARC_SUN_US1)
1917 strcat (buf, ", ultrasparcI");
1918
1919 if (e_flags & EF_SPARC_SUN_US3)
1920 strcat (buf, ", ultrasparcIII");
1921
1922 if (e_flags & EF_SPARC_HAL_R1)
1923 strcat (buf, ", halr1");
1924
1925 if (e_flags & EF_SPARC_LEDATA)
1926 strcat (buf, ", ledata");
1927
1928 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_TSO)
1929 strcat (buf, ", tso");
1930
1931 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_PSO)
1932 strcat (buf, ", pso");
1933
1934 if ((e_flags & EF_SPARCV9_MM) == EF_SPARCV9_RMO)
1935 strcat (buf, ", rmo");
1936 break;
1937
1938 case EM_PARISC:
1939 switch (e_flags & EF_PARISC_ARCH)
1940 {
1941 case EFA_PARISC_1_0:
1942 strcpy (buf, ", PA-RISC 1.0");
1943 break;
1944 case EFA_PARISC_1_1:
1945 strcpy (buf, ", PA-RISC 1.1");
1946 break;
1947 case EFA_PARISC_2_0:
1948 strcpy (buf, ", PA-RISC 2.0");
1949 break;
1950 default:
1951 break;
1952 }
1953 if (e_flags & EF_PARISC_TRAPNIL)
1954 strcat (buf, ", trapnil");
1955 if (e_flags & EF_PARISC_EXT)
1956 strcat (buf, ", ext");
1957 if (e_flags & EF_PARISC_LSB)
1958 strcat (buf, ", lsb");
1959 if (e_flags & EF_PARISC_WIDE)
1960 strcat (buf, ", wide");
1961 if (e_flags & EF_PARISC_NO_KABP)
1962 strcat (buf, ", no kabp");
1963 if (e_flags & EF_PARISC_LAZYSWAP)
1964 strcat (buf, ", lazyswap");
1965 break;
1966
1967 case EM_PJ:
1968 case EM_PJ_OLD:
1969 if ((e_flags & EF_PICOJAVA_NEWCALLS) == EF_PICOJAVA_NEWCALLS)
1970 strcat (buf, ", new calling convention");
1971
1972 if ((e_flags & EF_PICOJAVA_GNUCALLS) == EF_PICOJAVA_GNUCALLS)
1973 strcat (buf, ", gnu calling convention");
1974 break;
1975
1976 case EM_IA_64:
1977 if ((e_flags & EF_IA_64_ABI64))
1978 strcat (buf, ", 64-bit");
1979 else
1980 strcat (buf, ", 32-bit");
1981 if ((e_flags & EF_IA_64_REDUCEDFP))
1982 strcat (buf, ", reduced fp model");
1983 if ((e_flags & EF_IA_64_NOFUNCDESC_CONS_GP))
1984 strcat (buf, ", no function descriptors, constant gp");
1985 else if ((e_flags & EF_IA_64_CONS_GP))
1986 strcat (buf, ", constant gp");
1987 if ((e_flags & EF_IA_64_ABSOLUTE))
1988 strcat (buf, ", absolute");
1989 break;
1990
1991 case EM_VAX:
1992 if ((e_flags & EF_VAX_NONPIC))
1993 strcat (buf, ", non-PIC");
1994 if ((e_flags & EF_VAX_DFLOAT))
1995 strcat (buf, ", D-Float");
1996 if ((e_flags & EF_VAX_GFLOAT))
1997 strcat (buf, ", G-Float");
1998 break;
1999 }
2000 }
2001
2002 return buf;
2003 }
2004
2005 static const char *
2006 get_osabi_name (unsigned int osabi)
2007 {
2008 static char buff[32];
2009
2010 switch (osabi)
2011 {
2012 case ELFOSABI_NONE: return "UNIX - System V";
2013 case ELFOSABI_HPUX: return "UNIX - HP-UX";
2014 case ELFOSABI_NETBSD: return "UNIX - NetBSD";
2015 case ELFOSABI_LINUX: return "UNIX - Linux";
2016 case ELFOSABI_HURD: return "GNU/Hurd";
2017 case ELFOSABI_SOLARIS: return "UNIX - Solaris";
2018 case ELFOSABI_AIX: return "UNIX - AIX";
2019 case ELFOSABI_IRIX: return "UNIX - IRIX";
2020 case ELFOSABI_FREEBSD: return "UNIX - FreeBSD";
2021 case ELFOSABI_TRU64: return "UNIX - TRU64";
2022 case ELFOSABI_MODESTO: return "Novell - Modesto";
2023 case ELFOSABI_OPENBSD: return "UNIX - OpenBSD";
2024 case ELFOSABI_OPENVMS: return "VMS - OpenVMS";
2025 case ELFOSABI_NSK: return "HP - Non-Stop Kernel";
2026 case ELFOSABI_AROS: return "Amiga Research OS";
2027 case ELFOSABI_STANDALONE: return _("Standalone App");
2028 case ELFOSABI_ARM: return "ARM";
2029 default:
2030 sprintf (buff, _("<unknown: %x>"), osabi);
2031 return buff;
2032 }
2033 }
2034
2035 static const char *
2036 get_mips_segment_type (unsigned long type)
2037 {
2038 switch (type)
2039 {
2040 case PT_MIPS_REGINFO:
2041 return "REGINFO";
2042 case PT_MIPS_RTPROC:
2043 return "RTPROC";
2044 case PT_MIPS_OPTIONS:
2045 return "OPTIONS";
2046 default:
2047 break;
2048 }
2049
2050 return NULL;
2051 }
2052
2053 static const char *
2054 get_parisc_segment_type (unsigned long type)
2055 {
2056 switch (type)
2057 {
2058 case PT_HP_TLS: return "HP_TLS";
2059 case PT_HP_CORE_NONE: return "HP_CORE_NONE";
2060 case PT_HP_CORE_VERSION: return "HP_CORE_VERSION";
2061 case PT_HP_CORE_KERNEL: return "HP_CORE_KERNEL";
2062 case PT_HP_CORE_COMM: return "HP_CORE_COMM";
2063 case PT_HP_CORE_PROC: return "HP_CORE_PROC";
2064 case PT_HP_CORE_LOADABLE: return "HP_CORE_LOADABLE";
2065 case PT_HP_CORE_STACK: return "HP_CORE_STACK";
2066 case PT_HP_CORE_SHM: return "HP_CORE_SHM";
2067 case PT_HP_CORE_MMF: return "HP_CORE_MMF";
2068 case PT_HP_PARALLEL: return "HP_PARALLEL";
2069 case PT_HP_FASTBIND: return "HP_FASTBIND";
2070 case PT_PARISC_ARCHEXT: return "PARISC_ARCHEXT";
2071 case PT_PARISC_UNWIND: return "PARISC_UNWIND";
2072 default:
2073 break;
2074 }
2075
2076 return NULL;
2077 }
2078
2079 static const char *
2080 get_ia64_segment_type (unsigned long type)
2081 {
2082 switch (type)
2083 {
2084 case PT_IA_64_ARCHEXT: return "IA_64_ARCHEXT";
2085 case PT_IA_64_UNWIND: return "IA_64_UNWIND";
2086 case PT_HP_TLS: return "HP_TLS";
2087 case PT_IA_64_HP_OPT_ANOT: return "HP_OPT_ANNOT";
2088 case PT_IA_64_HP_HSL_ANOT: return "HP_HSL_ANNOT";
2089 case PT_IA_64_HP_STACK: return "HP_STACK";
2090 default:
2091 break;
2092 }
2093
2094 return NULL;
2095 }
2096
2097 static const char *
2098 get_segment_type (unsigned long p_type)
2099 {
2100 static char buff[32];
2101
2102 switch (p_type)
2103 {
2104 case PT_NULL: return "NULL";
2105 case PT_LOAD: return "LOAD";
2106 case PT_DYNAMIC: return "DYNAMIC";
2107 case PT_INTERP: return "INTERP";
2108 case PT_NOTE: return "NOTE";
2109 case PT_SHLIB: return "SHLIB";
2110 case PT_PHDR: return "PHDR";
2111 case PT_TLS: return "TLS";
2112
2113 case PT_GNU_EH_FRAME:
2114 return "GNU_EH_FRAME";
2115 case PT_GNU_STACK: return "STACK";
2116
2117 default:
2118 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
2119 {
2120 const char *result;
2121
2122 switch (elf_header.e_machine)
2123 {
2124 case EM_MIPS:
2125 case EM_MIPS_RS3_LE:
2126 result = get_mips_segment_type (p_type);
2127 break;
2128 case EM_PARISC:
2129 result = get_parisc_segment_type (p_type);
2130 break;
2131 case EM_IA_64:
2132 result = get_ia64_segment_type (p_type);
2133 break;
2134 default:
2135 result = NULL;
2136 break;
2137 }
2138
2139 if (result != NULL)
2140 return result;
2141
2142 sprintf (buff, "LOPROC+%lx", p_type - PT_LOPROC);
2143 }
2144 else if ((p_type >= PT_LOOS) && (p_type <= PT_HIOS))
2145 {
2146 const char *result;
2147
2148 switch (elf_header.e_machine)
2149 {
2150 case EM_PARISC:
2151 result = get_parisc_segment_type (p_type);
2152 break;
2153 case EM_IA_64:
2154 result = get_ia64_segment_type (p_type);
2155 break;
2156 default:
2157 result = NULL;
2158 break;
2159 }
2160
2161 if (result != NULL)
2162 return result;
2163
2164 sprintf (buff, "LOOS+%lx", p_type - PT_LOOS);
2165 }
2166 else
2167 sprintf (buff, _("<unknown>: %lx"), p_type);
2168
2169 return buff;
2170 }
2171 }
2172
2173 static const char *
2174 get_mips_section_type_name (unsigned int sh_type)
2175 {
2176 switch (sh_type)
2177 {
2178 case SHT_MIPS_LIBLIST: return "MIPS_LIBLIST";
2179 case SHT_MIPS_MSYM: return "MIPS_MSYM";
2180 case SHT_MIPS_CONFLICT: return "MIPS_CONFLICT";
2181 case SHT_MIPS_GPTAB: return "MIPS_GPTAB";
2182 case SHT_MIPS_UCODE: return "MIPS_UCODE";
2183 case SHT_MIPS_DEBUG: return "MIPS_DEBUG";
2184 case SHT_MIPS_REGINFO: return "MIPS_REGINFO";
2185 case SHT_MIPS_PACKAGE: return "MIPS_PACKAGE";
2186 case SHT_MIPS_PACKSYM: return "MIPS_PACKSYM";
2187 case SHT_MIPS_RELD: return "MIPS_RELD";
2188 case SHT_MIPS_IFACE: return "MIPS_IFACE";
2189 case SHT_MIPS_CONTENT: return "MIPS_CONTENT";
2190 case SHT_MIPS_OPTIONS: return "MIPS_OPTIONS";
2191 case SHT_MIPS_SHDR: return "MIPS_SHDR";
2192 case SHT_MIPS_FDESC: return "MIPS_FDESC";
2193 case SHT_MIPS_EXTSYM: return "MIPS_EXTSYM";
2194 case SHT_MIPS_DENSE: return "MIPS_DENSE";
2195 case SHT_MIPS_PDESC: return "MIPS_PDESC";
2196 case SHT_MIPS_LOCSYM: return "MIPS_LOCSYM";
2197 case SHT_MIPS_AUXSYM: return "MIPS_AUXSYM";
2198 case SHT_MIPS_OPTSYM: return "MIPS_OPTSYM";
2199 case SHT_MIPS_LOCSTR: return "MIPS_LOCSTR";
2200 case SHT_MIPS_LINE: return "MIPS_LINE";
2201 case SHT_MIPS_RFDESC: return "MIPS_RFDESC";
2202 case SHT_MIPS_DELTASYM: return "MIPS_DELTASYM";
2203 case SHT_MIPS_DELTAINST: return "MIPS_DELTAINST";
2204 case SHT_MIPS_DELTACLASS: return "MIPS_DELTACLASS";
2205 case SHT_MIPS_DWARF: return "MIPS_DWARF";
2206 case SHT_MIPS_DELTADECL: return "MIPS_DELTADECL";
2207 case SHT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
2208 case SHT_MIPS_EVENTS: return "MIPS_EVENTS";
2209 case SHT_MIPS_TRANSLATE: return "MIPS_TRANSLATE";
2210 case SHT_MIPS_PIXIE: return "MIPS_PIXIE";
2211 case SHT_MIPS_XLATE: return "MIPS_XLATE";
2212 case SHT_MIPS_XLATE_DEBUG: return "MIPS_XLATE_DEBUG";
2213 case SHT_MIPS_WHIRL: return "MIPS_WHIRL";
2214 case SHT_MIPS_EH_REGION: return "MIPS_EH_REGION";
2215 case SHT_MIPS_XLATE_OLD: return "MIPS_XLATE_OLD";
2216 case SHT_MIPS_PDR_EXCEPTION: return "MIPS_PDR_EXCEPTION";
2217 default:
2218 break;
2219 }
2220 return NULL;
2221 }
2222
2223 static const char *
2224 get_parisc_section_type_name (unsigned int sh_type)
2225 {
2226 switch (sh_type)
2227 {
2228 case SHT_PARISC_EXT: return "PARISC_EXT";
2229 case SHT_PARISC_UNWIND: return "PARISC_UNWIND";
2230 case SHT_PARISC_DOC: return "PARISC_DOC";
2231 default:
2232 break;
2233 }
2234 return NULL;
2235 }
2236
2237 static const char *
2238 get_ia64_section_type_name (unsigned int sh_type)
2239 {
2240 /* If the top 8 bits are 0x78 the next 8 are the os/abi ID. */
2241 if ((sh_type & 0xFF000000) == SHT_IA_64_LOPSREG)
2242 return get_osabi_name ((sh_type & 0x00FF0000) >> 16);
2243
2244 switch (sh_type)
2245 {
2246 case SHT_IA_64_EXT: return "IA_64_EXT";
2247 case SHT_IA_64_UNWIND: return "IA_64_UNWIND";
2248 case SHT_IA_64_PRIORITY_INIT: return "IA_64_PRIORITY_INIT";
2249 default:
2250 break;
2251 }
2252 return NULL;
2253 }
2254
2255 static const char *
2256 get_section_type_name (unsigned int sh_type)
2257 {
2258 static char buff[32];
2259
2260 switch (sh_type)
2261 {
2262 case SHT_NULL: return "NULL";
2263 case SHT_PROGBITS: return "PROGBITS";
2264 case SHT_SYMTAB: return "SYMTAB";
2265 case SHT_STRTAB: return "STRTAB";
2266 case SHT_RELA: return "RELA";
2267 case SHT_HASH: return "HASH";
2268 case SHT_DYNAMIC: return "DYNAMIC";
2269 case SHT_NOTE: return "NOTE";
2270 case SHT_NOBITS: return "NOBITS";
2271 case SHT_REL: return "REL";
2272 case SHT_SHLIB: return "SHLIB";
2273 case SHT_DYNSYM: return "DYNSYM";
2274 case SHT_INIT_ARRAY: return "INIT_ARRAY";
2275 case SHT_FINI_ARRAY: return "FINI_ARRAY";
2276 case SHT_PREINIT_ARRAY: return "PREINIT_ARRAY";
2277 case SHT_GROUP: return "GROUP";
2278 case SHT_SYMTAB_SHNDX: return "SYMTAB SECTION INDICIES";
2279 case SHT_GNU_verdef: return "VERDEF";
2280 case SHT_GNU_verneed: return "VERNEED";
2281 case SHT_GNU_versym: return "VERSYM";
2282 case 0x6ffffff0: return "VERSYM";
2283 case 0x6ffffffc: return "VERDEF";
2284 case 0x7ffffffd: return "AUXILIARY";
2285 case 0x7fffffff: return "FILTER";
2286 case SHT_GNU_LIBLIST: return "GNU_LIBLIST";
2287
2288 default:
2289 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
2290 {
2291 const char *result;
2292
2293 switch (elf_header.e_machine)
2294 {
2295 case EM_MIPS:
2296 case EM_MIPS_RS3_LE:
2297 result = get_mips_section_type_name (sh_type);
2298 break;
2299 case EM_PARISC:
2300 result = get_parisc_section_type_name (sh_type);
2301 break;
2302 case EM_IA_64:
2303 result = get_ia64_section_type_name (sh_type);
2304 break;
2305 default:
2306 result = NULL;
2307 break;
2308 }
2309
2310 if (result != NULL)
2311 return result;
2312
2313 sprintf (buff, "LOPROC+%x", sh_type - SHT_LOPROC);
2314 }
2315 else if ((sh_type >= SHT_LOOS) && (sh_type <= SHT_HIOS))
2316 sprintf (buff, "LOOS+%x", sh_type - SHT_LOOS);
2317 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
2318 sprintf (buff, "LOUSER+%x", sh_type - SHT_LOUSER);
2319 else
2320 sprintf (buff, _("<unknown>: %x"), sh_type);
2321
2322 return buff;
2323 }
2324 }
2325
2326 #define OPTION_DEBUG_DUMP 512
2327
2328 struct option options[] =
2329 {
2330 {"all", no_argument, 0, 'a'},
2331 {"file-header", no_argument, 0, 'h'},
2332 {"program-headers", no_argument, 0, 'l'},
2333 {"headers", no_argument, 0, 'e'},
2334 {"histogram", no_argument, 0, 'I'},
2335 {"segments", no_argument, 0, 'l'},
2336 {"sections", no_argument, 0, 'S'},
2337 {"section-headers", no_argument, 0, 'S'},
2338 {"symbols", no_argument, 0, 's'},
2339 {"syms", no_argument, 0, 's'},
2340 {"relocs", no_argument, 0, 'r'},
2341 {"notes", no_argument, 0, 'n'},
2342 {"dynamic", no_argument, 0, 'd'},
2343 {"arch-specific", no_argument, 0, 'A'},
2344 {"version-info", no_argument, 0, 'V'},
2345 {"use-dynamic", no_argument, 0, 'D'},
2346 {"hex-dump", required_argument, 0, 'x'},
2347 {"debug-dump", optional_argument, 0, OPTION_DEBUG_DUMP},
2348 {"unwind", no_argument, 0, 'u'},
2349 #ifdef SUPPORT_DISASSEMBLY
2350 {"instruction-dump", required_argument, 0, 'i'},
2351 #endif
2352
2353 {"version", no_argument, 0, 'v'},
2354 {"wide", no_argument, 0, 'W'},
2355 {"help", no_argument, 0, 'H'},
2356 {0, no_argument, 0, 0}
2357 };
2358
2359 static void
2360 usage (void)
2361 {
2362 fprintf (stdout, _("Usage: readelf <option(s)> elf-file(s)\n"));
2363 fprintf (stdout, _(" Display information about the contents of ELF format files\n"));
2364 fprintf (stdout, _(" Options are:\n\
2365 -a --all Equivalent to: -h -l -S -s -r -d -V -A -I\n\
2366 -h --file-header Display the ELF file header\n\
2367 -l --program-headers Display the program headers\n\
2368 --segments An alias for --program-headers\n\
2369 -S --section-headers Display the sections' header\n\
2370 --sections An alias for --section-headers\n\
2371 -e --headers Equivalent to: -h -l -S\n\
2372 -s --syms Display the symbol table\n\
2373 --symbols An alias for --syms\n\
2374 -n --notes Display the core notes (if present)\n\
2375 -r --relocs Display the relocations (if present)\n\
2376 -u --unwind Display the unwind info (if present)\n\
2377 -d --dynamic Display the dynamic segment (if present)\n\
2378 -V --version-info Display the version sections (if present)\n\
2379 -A --arch-specific Display architecture specific information (if any).\n\
2380 -D --use-dynamic Use the dynamic section info when displaying symbols\n\
2381 -x --hex-dump=<number> Dump the contents of section <number>\n\
2382 -w[liaprmfFso] or\n\
2383 --debug-dump[=line,=info,=abbrev,=pubnames,=ranges,=macro,=frames,=str,=loc]\n\
2384 Display the contents of DWARF2 debug sections\n"));
2385 #ifdef SUPPORT_DISASSEMBLY
2386 fprintf (stdout, _("\
2387 -i --instruction-dump=<number>\n\
2388 Disassemble the contents of section <number>\n"));
2389 #endif
2390 fprintf (stdout, _("\
2391 -I --histogram Display histogram of bucket list lengths\n\
2392 -W --wide Allow output width to exceed 80 characters\n\
2393 -H --help Display this information\n\
2394 -v --version Display the version number of readelf\n"));
2395 fprintf (stdout, _("Report bugs to %s\n"), REPORT_BUGS_TO);
2396
2397 exit (0);
2398 }
2399
2400 static void
2401 request_dump (unsigned int section, int type)
2402 {
2403 if (section >= num_dump_sects)
2404 {
2405 char *new_dump_sects;
2406
2407 new_dump_sects = calloc (section + 1, 1);
2408
2409 if (new_dump_sects == NULL)
2410 error (_("Out of memory allocating dump request table."));
2411 else
2412 {
2413 /* Copy current flag settings. */
2414 memcpy (new_dump_sects, dump_sects, num_dump_sects);
2415
2416 free (dump_sects);
2417
2418 dump_sects = new_dump_sects;
2419 num_dump_sects = section + 1;
2420 }
2421 }
2422
2423 if (dump_sects)
2424 dump_sects[section] |= type;
2425
2426 return;
2427 }
2428
2429 static void
2430 parse_args (int argc, char **argv)
2431 {
2432 int c;
2433
2434 if (argc < 2)
2435 usage ();
2436
2437 while ((c = getopt_long
2438 (argc, argv, "ersuahnldSDAIw::x:i:vVWH", options, NULL)) != EOF)
2439 {
2440 char *cp;
2441 int section;
2442
2443 switch (c)
2444 {
2445 case 0:
2446 /* Long options. */
2447 break;
2448 case 'H':
2449 usage ();
2450 break;
2451
2452 case 'a':
2453 do_syms++;
2454 do_reloc++;
2455 do_unwind++;
2456 do_dynamic++;
2457 do_header++;
2458 do_sections++;
2459 do_segments++;
2460 do_version++;
2461 do_histogram++;
2462 do_arch++;
2463 do_notes++;
2464 break;
2465 case 'e':
2466 do_header++;
2467 do_sections++;
2468 do_segments++;
2469 break;
2470 case 'A':
2471 do_arch++;
2472 break;
2473 case 'D':
2474 do_using_dynamic++;
2475 break;
2476 case 'r':
2477 do_reloc++;
2478 break;
2479 case 'u':
2480 do_unwind++;
2481 break;
2482 case 'h':
2483 do_header++;
2484 break;
2485 case 'l':
2486 do_segments++;
2487 break;
2488 case 's':
2489 do_syms++;
2490 break;
2491 case 'S':
2492 do_sections++;
2493 break;
2494 case 'd':
2495 do_dynamic++;
2496 break;
2497 case 'I':
2498 do_histogram++;
2499 break;
2500 case 'n':
2501 do_notes++;
2502 break;
2503 case 'x':
2504 do_dump++;
2505 section = strtoul (optarg, & cp, 0);
2506 if (! *cp && section >= 0)
2507 {
2508 request_dump (section, HEX_DUMP);
2509 break;
2510 }
2511 goto oops;
2512 case 'w':
2513 do_dump++;
2514 if (optarg == 0)
2515 do_debugging = 1;
2516 else
2517 {
2518 unsigned int index = 0;
2519
2520 do_debugging = 0;
2521
2522 while (optarg[index])
2523 switch (optarg[index++])
2524 {
2525 case 'i':
2526 case 'I':
2527 do_debug_info = 1;
2528 break;
2529
2530 case 'a':
2531 case 'A':
2532 do_debug_abbrevs = 1;
2533 break;
2534
2535 case 'l':
2536 case 'L':
2537 do_debug_lines = 1;
2538 break;
2539
2540 case 'p':
2541 case 'P':
2542 do_debug_pubnames = 1;
2543 break;
2544
2545 case 'r':
2546 case 'R':
2547 do_debug_aranges = 1;
2548 break;
2549
2550 case 'F':
2551 do_debug_frames_interp = 1;
2552 case 'f':
2553 do_debug_frames = 1;
2554 break;
2555
2556 case 'm':
2557 case 'M':
2558 do_debug_macinfo = 1;
2559 break;
2560
2561 case 's':
2562 case 'S':
2563 do_debug_str = 1;
2564 break;
2565
2566 case 'o':
2567 case 'O':
2568 do_debug_loc = 1;
2569 break;
2570
2571 default:
2572 warn (_("Unrecognized debug option '%s'\n"), optarg);
2573 break;
2574 }
2575 }
2576 break;
2577 case OPTION_DEBUG_DUMP:
2578 do_dump++;
2579 if (optarg == 0)
2580 do_debugging = 1;
2581 else
2582 {
2583 static const char *debug_dump_opt[]
2584 = { "line", "info", "abbrev", "pubnames", "ranges",
2585 "macro", "frames", "frames-interp", "str", "loc", NULL };
2586 unsigned int index;
2587 const char *p;
2588
2589 do_debugging = 0;
2590
2591 p = optarg;
2592 while (*p)
2593 {
2594 for (index = 0; debug_dump_opt[index]; index++)
2595 {
2596 size_t len = strlen (debug_dump_opt[index]);
2597
2598 if (strncmp (p, debug_dump_opt[index], len) == 0
2599 && (p[len] == ',' || p[len] == '\0'))
2600 {
2601 switch (p[0])
2602 {
2603 case 'i':
2604 do_debug_info = 1;
2605 break;
2606
2607 case 'a':
2608 do_debug_abbrevs = 1;
2609 break;
2610
2611 case 'l':
2612 if (p[1] == 'i')
2613 do_debug_lines = 1;
2614 else
2615 do_debug_loc = 1;
2616 break;
2617
2618 case 'p':
2619 do_debug_pubnames = 1;
2620 break;
2621
2622 case 'r':
2623 do_debug_aranges = 1;
2624 break;
2625
2626 case 'f':
2627 if (len > 6)
2628 do_debug_frames_interp = 1;
2629 do_debug_frames = 1;
2630 break;
2631
2632 case 'm':
2633 do_debug_macinfo = 1;
2634 break;
2635
2636 case 's':
2637 do_debug_str = 1;
2638 break;
2639 }
2640
2641 p += len;
2642 break;
2643 }
2644 }
2645
2646 if (debug_dump_opt[index] == NULL)
2647 {
2648 warn (_("Unrecognized debug option '%s'\n"), p);
2649 p = strchr (p, ',');
2650 if (p == NULL)
2651 break;
2652 }
2653
2654 if (*p == ',')
2655 p++;
2656 }
2657 }
2658 break;
2659 #ifdef SUPPORT_DISASSEMBLY
2660 case 'i':
2661 do_dump++;
2662 section = strtoul (optarg, & cp, 0);
2663 if (! *cp && section >= 0)
2664 {
2665 request_dump (section, DISASS_DUMP);
2666 break;
2667 }
2668 goto oops;
2669 #endif
2670 case 'v':
2671 print_version (program_name);
2672 break;
2673 case 'V':
2674 do_version++;
2675 break;
2676 case 'W':
2677 do_wide++;
2678 break;
2679 default:
2680 oops:
2681 /* xgettext:c-format */
2682 error (_("Invalid option '-%c'\n"), c);
2683 /* Drop through. */
2684 case '?':
2685 usage ();
2686 }
2687 }
2688
2689 if (!do_dynamic && !do_syms && !do_reloc && !do_unwind && !do_sections
2690 && !do_segments && !do_header && !do_dump && !do_version
2691 && !do_histogram && !do_debugging && !do_arch && !do_notes)
2692 usage ();
2693 else if (argc < 3)
2694 {
2695 warn (_("Nothing to do.\n"));
2696 usage();
2697 }
2698 }
2699
2700 static const char *
2701 get_elf_class (unsigned int elf_class)
2702 {
2703 static char buff[32];
2704
2705 switch (elf_class)
2706 {
2707 case ELFCLASSNONE: return _("none");
2708 case ELFCLASS32: return "ELF32";
2709 case ELFCLASS64: return "ELF64";
2710 default:
2711 sprintf (buff, _("<unknown: %x>"), elf_class);
2712 return buff;
2713 }
2714 }
2715
2716 static const char *
2717 get_data_encoding (unsigned int encoding)
2718 {
2719 static char buff[32];
2720
2721 switch (encoding)
2722 {
2723 case ELFDATANONE: return _("none");
2724 case ELFDATA2LSB: return _("2's complement, little endian");
2725 case ELFDATA2MSB: return _("2's complement, big endian");
2726 default:
2727 sprintf (buff, _("<unknown: %x>"), encoding);
2728 return buff;
2729 }
2730 }
2731
2732 /* Decode the data held in 'elf_header'. */
2733
2734 static int
2735 process_file_header (void)
2736 {
2737 if ( elf_header.e_ident[EI_MAG0] != ELFMAG0
2738 || elf_header.e_ident[EI_MAG1] != ELFMAG1
2739 || elf_header.e_ident[EI_MAG2] != ELFMAG2
2740 || elf_header.e_ident[EI_MAG3] != ELFMAG3)
2741 {
2742 error
2743 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
2744 return 0;
2745 }
2746
2747 if (do_header)
2748 {
2749 int i;
2750
2751 printf (_("ELF Header:\n"));
2752 printf (_(" Magic: "));
2753 for (i = 0; i < EI_NIDENT; i++)
2754 printf ("%2.2x ", elf_header.e_ident[i]);
2755 printf ("\n");
2756 printf (_(" Class: %s\n"),
2757 get_elf_class (elf_header.e_ident[EI_CLASS]));
2758 printf (_(" Data: %s\n"),
2759 get_data_encoding (elf_header.e_ident[EI_DATA]));
2760 printf (_(" Version: %d %s\n"),
2761 elf_header.e_ident[EI_VERSION],
2762 (elf_header.e_ident[EI_VERSION] == EV_CURRENT
2763 ? "(current)"
2764 : (elf_header.e_ident[EI_VERSION] != EV_NONE
2765 ? "<unknown: %lx>"
2766 : "")));
2767 printf (_(" OS/ABI: %s\n"),
2768 get_osabi_name (elf_header.e_ident[EI_OSABI]));
2769 printf (_(" ABI Version: %d\n"),
2770 elf_header.e_ident[EI_ABIVERSION]);
2771 printf (_(" Type: %s\n"),
2772 get_file_type (elf_header.e_type));
2773 printf (_(" Machine: %s\n"),
2774 get_machine_name (elf_header.e_machine));
2775 printf (_(" Version: 0x%lx\n"),
2776 (unsigned long) elf_header.e_version);
2777
2778 printf (_(" Entry point address: "));
2779 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
2780 printf (_("\n Start of program headers: "));
2781 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
2782 printf (_(" (bytes into file)\n Start of section headers: "));
2783 print_vma ((bfd_vma) elf_header.e_shoff, DEC);
2784 printf (_(" (bytes into file)\n"));
2785
2786 printf (_(" Flags: 0x%lx%s\n"),
2787 (unsigned long) elf_header.e_flags,
2788 get_machine_flags (elf_header.e_flags, elf_header.e_machine));
2789 printf (_(" Size of this header: %ld (bytes)\n"),
2790 (long) elf_header.e_ehsize);
2791 printf (_(" Size of program headers: %ld (bytes)\n"),
2792 (long) elf_header.e_phentsize);
2793 printf (_(" Number of program headers: %ld\n"),
2794 (long) elf_header.e_phnum);
2795 printf (_(" Size of section headers: %ld (bytes)\n"),
2796 (long) elf_header.e_shentsize);
2797 printf (_(" Number of section headers: %ld"),
2798 (long) elf_header.e_shnum);
2799 if (section_headers != NULL && elf_header.e_shnum == 0)
2800 printf (" (%ld)", (long) section_headers[0].sh_size);
2801 putc ('\n', stdout);
2802 printf (_(" Section header string table index: %ld"),
2803 (long) elf_header.e_shstrndx);
2804 if (section_headers != NULL && elf_header.e_shstrndx == SHN_XINDEX)
2805 printf (" (%ld)", (long) section_headers[0].sh_link);
2806 putc ('\n', stdout);
2807 }
2808
2809 if (section_headers != NULL)
2810 {
2811 if (elf_header.e_shnum == 0)
2812 elf_header.e_shnum = section_headers[0].sh_size;
2813 if (elf_header.e_shstrndx == SHN_XINDEX)
2814 elf_header.e_shstrndx = section_headers[0].sh_link;
2815 free (section_headers);
2816 section_headers = NULL;
2817 }
2818
2819 return 1;
2820 }
2821
2822
2823 static int
2824 get_32bit_program_headers (FILE *file, Elf_Internal_Phdr *program_headers)
2825 {
2826 Elf32_External_Phdr *phdrs;
2827 Elf32_External_Phdr *external;
2828 Elf_Internal_Phdr *internal;
2829 unsigned int i;
2830
2831 phdrs = get_data (NULL, file, elf_header.e_phoff,
2832 elf_header.e_phentsize * elf_header.e_phnum,
2833 _("program headers"));
2834 if (!phdrs)
2835 return 0;
2836
2837 for (i = 0, internal = program_headers, external = phdrs;
2838 i < elf_header.e_phnum;
2839 i++, internal++, external++)
2840 {
2841 internal->p_type = BYTE_GET (external->p_type);
2842 internal->p_offset = BYTE_GET (external->p_offset);
2843 internal->p_vaddr = BYTE_GET (external->p_vaddr);
2844 internal->p_paddr = BYTE_GET (external->p_paddr);
2845 internal->p_filesz = BYTE_GET (external->p_filesz);
2846 internal->p_memsz = BYTE_GET (external->p_memsz);
2847 internal->p_flags = BYTE_GET (external->p_flags);
2848 internal->p_align = BYTE_GET (external->p_align);
2849 }
2850
2851 free (phdrs);
2852
2853 return 1;
2854 }
2855
2856 static int
2857 get_64bit_program_headers (FILE *file, Elf_Internal_Phdr *program_headers)
2858 {
2859 Elf64_External_Phdr *phdrs;
2860 Elf64_External_Phdr *external;
2861 Elf_Internal_Phdr *internal;
2862 unsigned int i;
2863
2864 phdrs = get_data (NULL, file, elf_header.e_phoff,
2865 elf_header.e_phentsize * elf_header.e_phnum,
2866 _("program headers"));
2867 if (!phdrs)
2868 return 0;
2869
2870 for (i = 0, internal = program_headers, external = phdrs;
2871 i < elf_header.e_phnum;
2872 i++, internal++, external++)
2873 {
2874 internal->p_type = BYTE_GET (external->p_type);
2875 internal->p_flags = BYTE_GET (external->p_flags);
2876 internal->p_offset = BYTE_GET8 (external->p_offset);
2877 internal->p_vaddr = BYTE_GET8 (external->p_vaddr);
2878 internal->p_paddr = BYTE_GET8 (external->p_paddr);
2879 internal->p_filesz = BYTE_GET8 (external->p_filesz);
2880 internal->p_memsz = BYTE_GET8 (external->p_memsz);
2881 internal->p_align = BYTE_GET8 (external->p_align);
2882 }
2883
2884 free (phdrs);
2885
2886 return 1;
2887 }
2888
2889 /* Returns 1 if the program headers were read into `program_headers'. */
2890
2891 static int
2892 get_program_headers (FILE *file)
2893 {
2894 Elf_Internal_Phdr *phdrs;
2895
2896 /* Check cache of prior read. */
2897 if (program_headers != NULL)
2898 return 1;
2899
2900 phdrs = malloc (elf_header.e_phnum * sizeof (Elf_Internal_Phdr));
2901
2902 if (phdrs == NULL)
2903 {
2904 error (_("Out of memory\n"));
2905 return 0;
2906 }
2907
2908 if (is_32bit_elf
2909 ? get_32bit_program_headers (file, phdrs)
2910 : get_64bit_program_headers (file, phdrs))
2911 {
2912 program_headers = phdrs;
2913 return 1;
2914 }
2915
2916 free (phdrs);
2917 return 0;
2918 }
2919
2920 /* Returns 1 if the program headers were loaded. */
2921
2922 static int
2923 process_program_headers (FILE *file)
2924 {
2925 Elf_Internal_Phdr *segment;
2926 unsigned int i;
2927
2928 if (elf_header.e_phnum == 0)
2929 {
2930 if (do_segments)
2931 printf (_("\nThere are no program headers in this file.\n"));
2932 return 0;
2933 }
2934
2935 if (do_segments && !do_header)
2936 {
2937 printf (_("\nElf file type is %s\n"), get_file_type (elf_header.e_type));
2938 printf (_("Entry point "));
2939 print_vma ((bfd_vma) elf_header.e_entry, PREFIX_HEX);
2940 printf (_("\nThere are %d program headers, starting at offset "),
2941 elf_header.e_phnum);
2942 print_vma ((bfd_vma) elf_header.e_phoff, DEC);
2943 printf ("\n");
2944 }
2945
2946 if (! get_program_headers (file))
2947 return 0;
2948
2949 if (do_segments)
2950 {
2951 if (elf_header.e_phnum > 1)
2952 printf (_("\nProgram Headers:\n"));
2953 else
2954 printf (_("\nProgram Headers:\n"));
2955
2956 if (is_32bit_elf)
2957 printf
2958 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
2959 else if (do_wide)
2960 printf
2961 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
2962 else
2963 {
2964 printf
2965 (_(" Type Offset VirtAddr PhysAddr\n"));
2966 printf
2967 (_(" FileSiz MemSiz Flags Align\n"));
2968 }
2969 }
2970
2971 dynamic_addr = 0;
2972 dynamic_size = 0;
2973
2974 for (i = 0, segment = program_headers;
2975 i < elf_header.e_phnum;
2976 i++, segment++)
2977 {
2978 if (do_segments)
2979 {
2980 printf (" %-14.14s ", get_segment_type (segment->p_type));
2981
2982 if (is_32bit_elf)
2983 {
2984 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
2985 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
2986 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
2987 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
2988 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
2989 printf ("%c%c%c ",
2990 (segment->p_flags & PF_R ? 'R' : ' '),
2991 (segment->p_flags & PF_W ? 'W' : ' '),
2992 (segment->p_flags & PF_X ? 'E' : ' '));
2993 printf ("%#lx", (unsigned long) segment->p_align);
2994 }
2995 else if (do_wide)
2996 {
2997 if ((unsigned long) segment->p_offset == segment->p_offset)
2998 printf ("0x%6.6lx ", (unsigned long) segment->p_offset);
2999 else
3000 {
3001 print_vma (segment->p_offset, FULL_HEX);
3002 putchar (' ');
3003 }
3004
3005 print_vma (segment->p_vaddr, FULL_HEX);
3006 putchar (' ');
3007 print_vma (segment->p_paddr, FULL_HEX);
3008 putchar (' ');
3009
3010 if ((unsigned long) segment->p_filesz == segment->p_filesz)
3011 printf ("0x%6.6lx ", (unsigned long) segment->p_filesz);
3012 else
3013 {
3014 print_vma (segment->p_filesz, FULL_HEX);
3015 putchar (' ');
3016 }
3017
3018 if ((unsigned long) segment->p_memsz == segment->p_memsz)
3019 printf ("0x%6.6lx", (unsigned long) segment->p_memsz);
3020 else
3021 {
3022 print_vma (segment->p_offset, FULL_HEX);
3023 }
3024
3025 printf (" %c%c%c ",
3026 (segment->p_flags & PF_R ? 'R' : ' '),
3027 (segment->p_flags & PF_W ? 'W' : ' '),
3028 (segment->p_flags & PF_X ? 'E' : ' '));
3029
3030 if ((unsigned long) segment->p_align == segment->p_align)
3031 printf ("%#lx", (unsigned long) segment->p_align);
3032 else
3033 {
3034 print_vma (segment->p_align, PREFIX_HEX);
3035 }
3036 }
3037 else
3038 {
3039 print_vma (segment->p_offset, FULL_HEX);
3040 putchar (' ');
3041 print_vma (segment->p_vaddr, FULL_HEX);
3042 putchar (' ');
3043 print_vma (segment->p_paddr, FULL_HEX);
3044 printf ("\n ");
3045 print_vma (segment->p_filesz, FULL_HEX);
3046 putchar (' ');
3047 print_vma (segment->p_memsz, FULL_HEX);
3048 printf (" %c%c%c ",
3049 (segment->p_flags & PF_R ? 'R' : ' '),
3050 (segment->p_flags & PF_W ? 'W' : ' '),
3051 (segment->p_flags & PF_X ? 'E' : ' '));
3052 print_vma (segment->p_align, HEX);
3053 }
3054 }
3055
3056 switch (segment->p_type)
3057 {
3058 case PT_DYNAMIC:
3059 if (dynamic_addr)
3060 error (_("more than one dynamic segment\n"));
3061
3062 dynamic_addr = segment->p_offset;
3063 dynamic_size = segment->p_filesz;
3064 break;
3065
3066 case PT_INTERP:
3067 if (fseek (file, (long) segment->p_offset, SEEK_SET))
3068 error (_("Unable to find program interpreter name\n"));
3069 else
3070 {
3071 program_interpreter[0] = 0;
3072 fscanf (file, "%63s", program_interpreter);
3073
3074 if (do_segments)
3075 printf (_("\n [Requesting program interpreter: %s]"),
3076 program_interpreter);
3077 }
3078 break;
3079 }
3080
3081 if (do_segments)
3082 putc ('\n', stdout);
3083 }
3084
3085 if (do_segments && section_headers != NULL)
3086 {
3087 printf (_("\n Section to Segment mapping:\n"));
3088 printf (_(" Segment Sections...\n"));
3089
3090 assert (string_table != NULL);
3091
3092 for (i = 0; i < elf_header.e_phnum; i++)
3093 {
3094 unsigned int j;
3095 Elf_Internal_Shdr *section;
3096
3097 segment = program_headers + i;
3098 section = section_headers;
3099
3100 printf (" %2.2d ", i);
3101
3102 for (j = 1; j < elf_header.e_shnum; j++, section++)
3103 {
3104 if (section->sh_size > 0
3105 /* Compare allocated sections by VMA, unallocated
3106 sections by file offset. */
3107 && (section->sh_flags & SHF_ALLOC
3108 ? (section->sh_addr >= segment->p_vaddr
3109 && section->sh_addr + section->sh_size
3110 <= segment->p_vaddr + segment->p_memsz)
3111 : ((bfd_vma) section->sh_offset >= segment->p_offset
3112 && (section->sh_offset + section->sh_size
3113 <= segment->p_offset + segment->p_filesz))))
3114 printf ("%s ", SECTION_NAME (section));
3115 }
3116
3117 putc ('\n',stdout);
3118 }
3119 }
3120
3121 return 1;
3122 }
3123
3124
3125 /* Find the file offset corresponding to VMA by using the program headers. */
3126
3127 static long
3128 offset_from_vma (FILE *file, bfd_vma vma, bfd_size_type size)
3129 {
3130 Elf_Internal_Phdr *seg;
3131
3132 if (! get_program_headers (file))
3133 {
3134 warn (_("Cannot interpret virtual addresses without program headers.\n"));
3135 return (long) vma;
3136 }
3137
3138 for (seg = program_headers;
3139 seg < program_headers + elf_header.e_phnum;
3140 ++seg)
3141 {
3142 if (seg->p_type != PT_LOAD)
3143 continue;
3144
3145 if (vma >= (seg->p_vaddr & -seg->p_align)
3146 && vma + size <= seg->p_vaddr + seg->p_filesz)
3147 return vma - seg->p_vaddr + seg->p_offset;
3148 }
3149
3150 warn (_("Virtual address 0x%lx not located in any PT_LOAD segment.\n"),
3151 (long) vma);
3152 return (long) vma;
3153 }
3154
3155
3156 static int
3157 get_32bit_section_headers (FILE *file, unsigned int num)
3158 {
3159 Elf32_External_Shdr *shdrs;
3160 Elf_Internal_Shdr *internal;
3161 unsigned int i;
3162
3163 shdrs = get_data (NULL, file, elf_header.e_shoff,
3164 elf_header.e_shentsize * num, _("section headers"));
3165 if (!shdrs)
3166 return 0;
3167
3168 section_headers = malloc (num * sizeof (Elf_Internal_Shdr));
3169
3170 if (section_headers == NULL)
3171 {
3172 error (_("Out of memory\n"));
3173 return 0;
3174 }
3175
3176 for (i = 0, internal = section_headers;
3177 i < num;
3178 i++, internal++)
3179 {
3180 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3181 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3182 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
3183 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
3184 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3185 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
3186 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3187 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3188 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3189 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
3190 }
3191
3192 free (shdrs);
3193
3194 return 1;
3195 }
3196
3197 static int
3198 get_64bit_section_headers (FILE *file, unsigned int num)
3199 {
3200 Elf64_External_Shdr *shdrs;
3201 Elf_Internal_Shdr *internal;
3202 unsigned int i;
3203
3204 shdrs = get_data (NULL, file, elf_header.e_shoff,
3205 elf_header.e_shentsize * num, _("section headers"));
3206 if (!shdrs)
3207 return 0;
3208
3209 section_headers = malloc (num * sizeof (Elf_Internal_Shdr));
3210
3211 if (section_headers == NULL)
3212 {
3213 error (_("Out of memory\n"));
3214 return 0;
3215 }
3216
3217 for (i = 0, internal = section_headers;
3218 i < num;
3219 i++, internal++)
3220 {
3221 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
3222 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
3223 internal->sh_flags = BYTE_GET8 (shdrs[i].sh_flags);
3224 internal->sh_addr = BYTE_GET8 (shdrs[i].sh_addr);
3225 internal->sh_size = BYTE_GET8 (shdrs[i].sh_size);
3226 internal->sh_entsize = BYTE_GET8 (shdrs[i].sh_entsize);
3227 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
3228 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
3229 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
3230 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
3231 }
3232
3233 free (shdrs);
3234
3235 return 1;
3236 }
3237
3238 static Elf_Internal_Sym *
3239 get_32bit_elf_symbols (FILE *file, Elf_Internal_Shdr *section)
3240 {
3241 unsigned long number;
3242 Elf32_External_Sym *esyms;
3243 Elf_External_Sym_Shndx *shndx;
3244 Elf_Internal_Sym *isyms;
3245 Elf_Internal_Sym *psym;
3246 unsigned int j;
3247
3248 esyms = get_data (NULL, file, section->sh_offset, section->sh_size,
3249 _("symbols"));
3250 if (!esyms)
3251 return NULL;
3252
3253 shndx = NULL;
3254 if (symtab_shndx_hdr != NULL
3255 && (symtab_shndx_hdr->sh_link
3256 == (unsigned long) SECTION_HEADER_NUM (section - section_headers)))
3257 {
3258 shndx = get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3259 symtab_shndx_hdr->sh_size, _("symtab shndx"));
3260 if (!shndx)
3261 {
3262 free (esyms);
3263 return NULL;
3264 }
3265 }
3266
3267 number = section->sh_size / section->sh_entsize;
3268 isyms = malloc (number * sizeof (Elf_Internal_Sym));
3269
3270 if (isyms == NULL)
3271 {
3272 error (_("Out of memory\n"));
3273 if (shndx)
3274 free (shndx);
3275 free (esyms);
3276 return NULL;
3277 }
3278
3279 for (j = 0, psym = isyms;
3280 j < number;
3281 j++, psym++)
3282 {
3283 psym->st_name = BYTE_GET (esyms[j].st_name);
3284 psym->st_value = BYTE_GET (esyms[j].st_value);
3285 psym->st_size = BYTE_GET (esyms[j].st_size);
3286 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3287 if (psym->st_shndx == SHN_XINDEX && shndx != NULL)
3288 psym->st_shndx
3289 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3290 psym->st_info = BYTE_GET (esyms[j].st_info);
3291 psym->st_other = BYTE_GET (esyms[j].st_other);
3292 }
3293
3294 if (shndx)
3295 free (shndx);
3296 free (esyms);
3297
3298 return isyms;
3299 }
3300
3301 static Elf_Internal_Sym *
3302 get_64bit_elf_symbols (FILE *file, Elf_Internal_Shdr *section)
3303 {
3304 unsigned long number;
3305 Elf64_External_Sym *esyms;
3306 Elf_External_Sym_Shndx *shndx;
3307 Elf_Internal_Sym *isyms;
3308 Elf_Internal_Sym *psym;
3309 unsigned int j;
3310
3311 esyms = get_data (NULL, file, section->sh_offset, section->sh_size,
3312 _("symbols"));
3313 if (!esyms)
3314 return NULL;
3315
3316 shndx = NULL;
3317 if (symtab_shndx_hdr != NULL
3318 && (symtab_shndx_hdr->sh_link
3319 == (unsigned long) SECTION_HEADER_NUM (section - section_headers)))
3320 {
3321 shndx = get_data (NULL, file, symtab_shndx_hdr->sh_offset,
3322 symtab_shndx_hdr->sh_size, _("symtab shndx"));
3323 if (!shndx)
3324 {
3325 free (esyms);
3326 return NULL;
3327 }
3328 }
3329
3330 number = section->sh_size / section->sh_entsize;
3331 isyms = malloc (number * sizeof (Elf_Internal_Sym));
3332
3333 if (isyms == NULL)
3334 {
3335 error (_("Out of memory\n"));
3336 if (shndx)
3337 free (shndx);
3338 free (esyms);
3339 return NULL;
3340 }
3341
3342 for (j = 0, psym = isyms;
3343 j < number;
3344 j++, psym++)
3345 {
3346 psym->st_name = BYTE_GET (esyms[j].st_name);
3347 psym->st_info = BYTE_GET (esyms[j].st_info);
3348 psym->st_other = BYTE_GET (esyms[j].st_other);
3349 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
3350 if (psym->st_shndx == SHN_XINDEX && shndx != NULL)
3351 psym->st_shndx
3352 = byte_get ((unsigned char *) &shndx[j], sizeof (shndx[j]));
3353 psym->st_value = BYTE_GET8 (esyms[j].st_value);
3354 psym->st_size = BYTE_GET8 (esyms[j].st_size);
3355 }
3356
3357 if (shndx)
3358 free (shndx);
3359 free (esyms);
3360
3361 return isyms;
3362 }
3363
3364 static const char *
3365 get_elf_section_flags (bfd_vma sh_flags)
3366 {
3367 static char buff[32];
3368
3369 *buff = 0;
3370
3371 while (sh_flags)
3372 {
3373 bfd_vma flag;
3374
3375 flag = sh_flags & - sh_flags;
3376 sh_flags &= ~ flag;
3377
3378 switch (flag)
3379 {
3380 case SHF_WRITE: strcat (buff, "W"); break;
3381 case SHF_ALLOC: strcat (buff, "A"); break;
3382 case SHF_EXECINSTR: strcat (buff, "X"); break;
3383 case SHF_MERGE: strcat (buff, "M"); break;
3384 case SHF_STRINGS: strcat (buff, "S"); break;
3385 case SHF_INFO_LINK: strcat (buff, "I"); break;
3386 case SHF_LINK_ORDER: strcat (buff, "L"); break;
3387 case SHF_OS_NONCONFORMING: strcat (buff, "O"); break;
3388 case SHF_GROUP: strcat (buff, "G"); break;
3389 case SHF_TLS: strcat (buff, "T"); break;
3390
3391 default:
3392 if (flag & SHF_MASKOS)
3393 {
3394 strcat (buff, "o");
3395 sh_flags &= ~ SHF_MASKOS;
3396 }
3397 else if (flag & SHF_MASKPROC)
3398 {
3399 strcat (buff, "p");
3400 sh_flags &= ~ SHF_MASKPROC;
3401 }
3402 else
3403 strcat (buff, "x");
3404 break;
3405 }
3406 }
3407
3408 return buff;
3409 }
3410
3411 static int
3412 process_section_headers (FILE *file)
3413 {
3414 Elf_Internal_Shdr *section;
3415 unsigned int i;
3416
3417 section_headers = NULL;
3418
3419 if (elf_header.e_shnum == 0)
3420 {
3421 if (do_sections)
3422 printf (_("\nThere are no sections in this file.\n"));
3423
3424 return 1;
3425 }
3426
3427 if (do_sections && !do_header)
3428 printf (_("There are %d section headers, starting at offset 0x%lx:\n"),
3429 elf_header.e_shnum, (unsigned long) elf_header.e_shoff);
3430
3431 if (is_32bit_elf)
3432 {
3433 if (! get_32bit_section_headers (file, elf_header.e_shnum))
3434 return 0;
3435 }
3436 else if (! get_64bit_section_headers (file, elf_header.e_shnum))
3437 return 0;
3438
3439 /* Read in the string table, so that we have names to display. */
3440 section = SECTION_HEADER (elf_header.e_shstrndx);
3441
3442 if (section->sh_size != 0)
3443 {
3444 string_table = get_data (NULL, file, section->sh_offset,
3445 section->sh_size, _("string table"));
3446
3447 if (string_table == NULL)
3448 return 0;
3449
3450 string_table_length = section->sh_size;
3451 }
3452
3453 /* Scan the sections for the dynamic symbol table
3454 and dynamic string table and debug sections. */
3455 dynamic_symbols = NULL;
3456 dynamic_strings = NULL;
3457 dynamic_syminfo = NULL;
3458 symtab_shndx_hdr = NULL;
3459
3460 for (i = 0, section = section_headers;
3461 i < elf_header.e_shnum;
3462 i++, section++)
3463 {
3464 char *name = SECTION_NAME (section);
3465
3466 if (section->sh_type == SHT_DYNSYM)
3467 {
3468 if (dynamic_symbols != NULL)
3469 {
3470 error (_("File contains multiple dynamic symbol tables\n"));
3471 continue;
3472 }
3473
3474 num_dynamic_syms = section->sh_size / section->sh_entsize;
3475 dynamic_symbols = GET_ELF_SYMBOLS (file, section);
3476 }
3477 else if (section->sh_type == SHT_STRTAB
3478 && strcmp (name, ".dynstr") == 0)
3479 {
3480 if (dynamic_strings != NULL)
3481 {
3482 error (_("File contains multiple dynamic string tables\n"));
3483 continue;
3484 }
3485
3486 dynamic_strings = get_data (NULL, file, section->sh_offset,
3487 section->sh_size, _("dynamic strings"));
3488 }
3489 else if (section->sh_type == SHT_SYMTAB_SHNDX)
3490 {
3491 if (symtab_shndx_hdr != NULL)
3492 {
3493 error (_("File contains multiple symtab shndx tables\n"));
3494 continue;
3495 }
3496 symtab_shndx_hdr = section;
3497 }
3498 else if ((do_debugging || do_debug_info || do_debug_abbrevs
3499 || do_debug_lines || do_debug_pubnames || do_debug_aranges
3500 || do_debug_frames || do_debug_macinfo || do_debug_str
3501 || do_debug_loc)
3502 && strncmp (name, ".debug_", 7) == 0)
3503 {
3504 name += 7;
3505
3506 if (do_debugging
3507 || (do_debug_info && (strcmp (name, "info") == 0))
3508 || (do_debug_abbrevs && (strcmp (name, "abbrev") == 0))
3509 || (do_debug_lines && (strcmp (name, "line") == 0))
3510 || (do_debug_pubnames && (strcmp (name, "pubnames") == 0))
3511 || (do_debug_aranges && (strcmp (name, "aranges") == 0))
3512 || (do_debug_frames && (strcmp (name, "frame") == 0))
3513 || (do_debug_macinfo && (strcmp (name, "macinfo") == 0))
3514 || (do_debug_str && (strcmp (name, "str") == 0))
3515 || (do_debug_loc && (strcmp (name, "loc") == 0))
3516 )
3517 request_dump (i, DEBUG_DUMP);
3518 }
3519 /* linkonce section to be combined with .debug_info at link time. */
3520 else if ((do_debugging || do_debug_info)
3521 && strncmp (name, ".gnu.linkonce.wi.", 17) == 0)
3522 request_dump (i, DEBUG_DUMP);
3523 else if (do_debug_frames && strcmp (name, ".eh_frame") == 0)
3524 request_dump (i, DEBUG_DUMP);
3525 }
3526
3527 if (! do_sections)
3528 return 1;
3529
3530 if (elf_header.e_shnum > 1)
3531 printf (_("\nSection Headers:\n"));
3532 else
3533 printf (_("\nSection Header:\n"));
3534
3535 if (is_32bit_elf)
3536 printf
3537 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
3538 else if (do_wide)
3539 printf
3540 (_(" [Nr] Name Type Address Off Size ES Flg Lk Inf Al\n"));
3541 else
3542 {
3543 printf (_(" [Nr] Name Type Address Offset\n"));
3544 printf (_(" Size EntSize Flags Link Info Align\n"));
3545 }
3546
3547 for (i = 0, section = section_headers;
3548 i < elf_header.e_shnum;
3549 i++, section++)
3550 {
3551 printf (" [%2u] %-17.17s %-15.15s ",
3552 SECTION_HEADER_NUM (i),
3553 SECTION_NAME (section),
3554 get_section_type_name (section->sh_type));
3555
3556 if (is_32bit_elf)
3557 {
3558 print_vma (section->sh_addr, LONG_HEX);
3559
3560 printf ( " %6.6lx %6.6lx %2.2lx",
3561 (unsigned long) section->sh_offset,
3562 (unsigned long) section->sh_size,
3563 (unsigned long) section->sh_entsize);
3564
3565 printf (" %3s ", get_elf_section_flags (section->sh_flags));
3566
3567 printf ("%2ld %3lx %2ld\n",
3568 (unsigned long) section->sh_link,
3569 (unsigned long) section->sh_info,
3570 (unsigned long) section->sh_addralign);
3571 }
3572 else if (do_wide)
3573 {
3574 print_vma (section->sh_addr, LONG_HEX);
3575
3576 if ((long) section->sh_offset == section->sh_offset)
3577 printf (" %6.6lx", (unsigned long) section->sh_offset);
3578 else
3579 {
3580 putchar (' ');
3581 print_vma (section->sh_offset, LONG_HEX);
3582 }
3583
3584 if ((unsigned long) section->sh_size == section->sh_size)
3585 printf (" %6.6lx", (unsigned long) section->sh_size);
3586 else
3587 {
3588 putchar (' ');
3589 print_vma (section->sh_size, LONG_HEX);
3590 }
3591
3592 if ((unsigned long) section->sh_entsize == section->sh_entsize)
3593 printf (" %2.2lx", (unsigned long) section->sh_entsize);
3594 else
3595 {
3596 putchar (' ');
3597 print_vma (section->sh_entsize, LONG_HEX);
3598 }
3599
3600 printf (" %3s ", get_elf_section_flags (section->sh_flags));
3601
3602 printf ("%2ld %3lx ",
3603 (unsigned long) section->sh_link,
3604 (unsigned long) section->sh_info);
3605
3606 if ((unsigned long) section->sh_addralign == section->sh_addralign)
3607 printf ("%2ld\n", (unsigned long) section->sh_addralign);
3608 else
3609 {
3610 print_vma (section->sh_addralign, DEC);
3611 putchar ('\n');
3612 }
3613 }
3614 else
3615 {
3616 putchar (' ');
3617 print_vma (section->sh_addr, LONG_HEX);
3618 if ((long) section->sh_offset == section->sh_offset)
3619 printf (" %8.8lx", (unsigned long) section->sh_offset);
3620 else
3621 {
3622 printf (" ");
3623 print_vma (section->sh_offset, LONG_HEX);
3624 }
3625 printf ("\n ");
3626 print_vma (section->sh_size, LONG_HEX);
3627 printf (" ");
3628 print_vma (section->sh_entsize, LONG_HEX);
3629
3630 printf (" %3s ", get_elf_section_flags (section->sh_flags));
3631
3632 printf (" %2ld %3lx %ld\n",
3633 (unsigned long) section->sh_link,
3634 (unsigned long) section->sh_info,
3635 (unsigned long) section->sh_addralign);
3636 }
3637 }
3638
3639 printf (_("Key to Flags:\n\
3640 W (write), A (alloc), X (execute), M (merge), S (strings)\n\
3641 I (info), L (link order), G (group), x (unknown)\n\
3642 O (extra OS processing required) o (OS specific), p (processor specific)\n"));
3643
3644 return 1;
3645 }
3646
3647 struct
3648 {
3649 const char *name;
3650 int reloc;
3651 int size;
3652 int rela;
3653 } dynamic_relocations [] =
3654 {
3655 { "REL", DT_REL, DT_RELSZ, FALSE },
3656 { "RELA", DT_RELA, DT_RELASZ, TRUE },
3657 { "PLT", DT_JMPREL, DT_PLTRELSZ, UNKNOWN }
3658 };
3659
3660 /* Process the reloc section. */
3661 static int
3662 process_relocs (FILE *file)
3663 {
3664 unsigned long rel_size;
3665 unsigned long rel_offset;
3666
3667
3668 if (!do_reloc)
3669 return 1;
3670
3671 if (do_using_dynamic)
3672 {
3673 int is_rela;
3674 const char *name;
3675 int has_dynamic_reloc;
3676 unsigned int i;
3677
3678 has_dynamic_reloc = 0;
3679
3680 for (i = 0; i < ARRAY_SIZE (dynamic_relocations); i++)
3681 {
3682 is_rela = dynamic_relocations [i].rela;
3683 name = dynamic_relocations [i].name;
3684 rel_size = dynamic_info [dynamic_relocations [i].size];
3685 rel_offset = dynamic_info [dynamic_relocations [i].reloc];
3686
3687 has_dynamic_reloc |= rel_size;
3688
3689 if (is_rela == UNKNOWN)
3690 {
3691 if (dynamic_relocations [i].reloc == DT_JMPREL)
3692 switch (dynamic_info[DT_PLTREL])
3693 {
3694 case DT_REL:
3695 is_rela = FALSE;
3696 break;
3697 case DT_RELA:
3698 is_rela = TRUE;
3699 break;
3700 }
3701 }
3702
3703 if (rel_size)
3704 {
3705 printf
3706 (_("\n'%s' relocation section at offset 0x%lx contains %ld bytes:\n"),
3707 name, rel_offset, rel_size);
3708
3709 dump_relocations (file,
3710 offset_from_vma (file, rel_offset, rel_size),
3711 rel_size,
3712 dynamic_symbols, num_dynamic_syms,
3713 dynamic_strings, is_rela);
3714 }
3715 }
3716
3717 if (! has_dynamic_reloc)
3718 printf (_("\nThere are no dynamic relocations in this file.\n"));
3719 }
3720 else
3721 {
3722 Elf_Internal_Shdr *section;
3723 unsigned long i;
3724 int found = 0;
3725
3726 for (i = 0, section = section_headers;
3727 i < elf_header.e_shnum;
3728 i++, section++)
3729 {
3730 if ( section->sh_type != SHT_RELA
3731 && section->sh_type != SHT_REL)
3732 continue;
3733
3734 rel_offset = section->sh_offset;
3735 rel_size = section->sh_size;
3736
3737 if (rel_size)
3738 {
3739 Elf_Internal_Shdr *strsec;
3740 Elf_Internal_Sym *symtab;
3741 char *strtab;
3742 int is_rela;
3743 unsigned long nsyms;
3744
3745 printf (_("\nRelocation section "));
3746
3747 if (string_table == NULL)
3748 printf ("%d", section->sh_name);
3749 else
3750 printf (_("'%s'"), SECTION_NAME (section));
3751
3752 printf (_(" at offset 0x%lx contains %lu entries:\n"),
3753 rel_offset, (unsigned long) (rel_size / section->sh_entsize));
3754
3755 symtab = NULL;
3756 strtab = NULL;
3757 nsyms = 0;
3758 if (section->sh_link)
3759 {
3760 Elf_Internal_Shdr *symsec;
3761
3762 symsec = SECTION_HEADER (section->sh_link);
3763 nsyms = symsec->sh_size / symsec->sh_entsize;
3764 symtab = GET_ELF_SYMBOLS (file, symsec);
3765
3766 if (symtab == NULL)
3767 continue;
3768
3769 strsec = SECTION_HEADER (symsec->sh_link);
3770
3771 strtab = get_data (NULL, file, strsec->sh_offset,
3772 strsec->sh_size, _("string table"));
3773 }
3774 is_rela = section->sh_type == SHT_RELA;
3775
3776 dump_relocations (file, rel_offset, rel_size,
3777 symtab, nsyms, strtab, is_rela);
3778
3779 if (strtab)
3780 free (strtab);
3781 if (symtab)
3782 free (symtab);
3783
3784 found = 1;
3785 }
3786 }
3787
3788 if (! found)
3789 printf (_("\nThere are no relocations in this file.\n"));
3790 }
3791
3792 return 1;
3793 }
3794
3795 #include "unwind-ia64.h"
3796
3797 /* An absolute address consists of a section and an offset. If the
3798 section is NULL, the offset itself is the address, otherwise, the
3799 address equals to LOAD_ADDRESS(section) + offset. */
3800
3801 struct absaddr
3802 {
3803 unsigned short section;
3804 bfd_vma offset;
3805 };
3806
3807 struct unw_aux_info
3808 {
3809 struct unw_table_entry
3810 {
3811 struct absaddr start;
3812 struct absaddr end;
3813 struct absaddr info;
3814 }
3815 *table; /* Unwind table. */
3816 unsigned long table_len; /* Length of unwind table. */
3817 unsigned char *info; /* Unwind info. */
3818 unsigned long info_size; /* Size of unwind info. */
3819 bfd_vma info_addr; /* starting address of unwind info. */
3820 bfd_vma seg_base; /* Starting address of segment. */
3821 Elf_Internal_Sym *symtab; /* The symbol table. */
3822 unsigned long nsyms; /* Number of symbols. */
3823 char *strtab; /* The string table. */
3824 unsigned long strtab_size; /* Size of string table. */
3825 };
3826
3827 static void
3828 find_symbol_for_address (struct unw_aux_info *aux,
3829 struct absaddr addr,
3830 const char **symname,
3831 bfd_vma *offset)
3832 {
3833 bfd_vma dist = 0x100000;
3834 Elf_Internal_Sym *sym, *best = NULL;
3835 unsigned long i;
3836
3837 for (i = 0, sym = aux->symtab; i < aux->nsyms; ++i, ++sym)
3838 {
3839 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC
3840 && sym->st_name != 0
3841 && (addr.section == SHN_UNDEF || addr.section == sym->st_shndx)
3842 && addr.offset >= sym->st_value
3843 && addr.offset - sym->st_value < dist)
3844 {
3845 best = sym;
3846 dist = addr.offset - sym->st_value;
3847 if (!dist)
3848 break;
3849 }
3850 }
3851 if (best)
3852 {
3853 *symname = (best->st_name >= aux->strtab_size
3854 ? "<corrupt>" : aux->strtab + best->st_name);
3855 *offset = dist;
3856 return;
3857 }
3858 *symname = NULL;
3859 *offset = addr.offset;
3860 }
3861
3862 static void
3863 dump_ia64_unwind (struct unw_aux_info *aux)
3864 {
3865 bfd_vma addr_size;
3866 struct unw_table_entry *tp;
3867 int in_body;
3868
3869 addr_size = is_32bit_elf ? 4 : 8;
3870
3871 for (tp = aux->table; tp < aux->table + aux->table_len; ++tp)
3872 {
3873 bfd_vma stamp;
3874 bfd_vma offset;
3875 const unsigned char *dp;
3876 const unsigned char *head;
3877 const char *procname;
3878
3879 find_symbol_for_address (aux, tp->start, &procname, &offset);
3880
3881 fputs ("\n<", stdout);
3882
3883 if (procname)
3884 {
3885 fputs (procname, stdout);
3886
3887 if (offset)
3888 printf ("+%lx", (unsigned long) offset);
3889 }
3890
3891 fputs (">: [", stdout);
3892 print_vma (tp->start.offset, PREFIX_HEX);
3893 fputc ('-', stdout);
3894 print_vma (tp->end.offset, PREFIX_HEX);
3895 printf ("], info at +0x%lx\n",
3896 (unsigned long) (tp->info.offset - aux->seg_base));
3897
3898 head = aux->info + (tp->info.offset - aux->info_addr);
3899 stamp = BYTE_GET8 ((unsigned char *) head);
3900
3901 printf (" v%u, flags=0x%lx (%s%s), len=%lu bytes\n",
3902 (unsigned) UNW_VER (stamp),
3903 (unsigned long) ((stamp & UNW_FLAG_MASK) >> 32),
3904 UNW_FLAG_EHANDLER (stamp) ? " ehandler" : "",
3905 UNW_FLAG_UHANDLER (stamp) ? " uhandler" : "",
3906 (unsigned long) (addr_size * UNW_LENGTH (stamp)));
3907
3908 if (UNW_VER (stamp) != 1)
3909 {
3910 printf ("\tUnknown version.\n");
3911 continue;
3912 }
3913
3914 in_body = 0;
3915 for (dp = head + 8; dp < head + 8 + addr_size * UNW_LENGTH (stamp);)
3916 dp = unw_decode (dp, in_body, & in_body);
3917 }
3918 }
3919
3920 static int
3921 slurp_ia64_unwind_table (FILE *file,
3922 struct unw_aux_info *aux,
3923 Elf_Internal_Shdr *sec)
3924 {
3925 unsigned long size, addr_size, nrelas, i;
3926 Elf_Internal_Phdr *seg;
3927 struct unw_table_entry *tep;
3928 Elf_Internal_Shdr *relsec;
3929 Elf_Internal_Rela *rela, *rp;
3930 unsigned char *table, *tp;
3931 Elf_Internal_Sym *sym;
3932 const char *relname;
3933
3934 addr_size = is_32bit_elf ? 4 : 8;
3935
3936 /* First, find the starting address of the segment that includes
3937 this section: */
3938
3939 if (elf_header.e_phnum)
3940 {
3941 if (! get_program_headers (file))
3942 return 0;
3943
3944 for (seg = program_headers;
3945 seg < program_headers + elf_header.e_phnum;
3946 ++seg)
3947 {
3948 if (seg->p_type != PT_LOAD)
3949 continue;
3950
3951 if (sec->sh_addr >= seg->p_vaddr
3952 && (sec->sh_addr + sec->sh_size <= seg->p_vaddr + seg->p_memsz))
3953 {
3954 aux->seg_base = seg->p_vaddr;
3955 break;
3956 }
3957 }
3958 }
3959
3960 /* Second, build the unwind table from the contents of the unwind section: */
3961 size = sec->sh_size;
3962 table = get_data (NULL, file, sec->sh_offset, size, _("unwind table"));
3963 if (!table)
3964 return 0;
3965
3966 tep = aux->table = xmalloc (size / (3 * addr_size) * sizeof (aux->table[0]));
3967 for (tp = table; tp < table + size; tp += 3 * addr_size, ++tep)
3968 {
3969 tep->start.section = SHN_UNDEF;
3970 tep->end.section = SHN_UNDEF;
3971 tep->info.section = SHN_UNDEF;
3972 if (is_32bit_elf)
3973 {
3974 tep->start.offset = byte_get ((unsigned char *) tp + 0, 4);
3975 tep->end.offset = byte_get ((unsigned char *) tp + 4, 4);
3976 tep->info.offset = byte_get ((unsigned char *) tp + 8, 4);
3977 }
3978 else
3979 {
3980 tep->start.offset = BYTE_GET8 ((unsigned char *) tp + 0);
3981 tep->end.offset = BYTE_GET8 ((unsigned char *) tp + 8);
3982 tep->info.offset = BYTE_GET8 ((unsigned char *) tp + 16);
3983 }
3984 tep->start.offset += aux->seg_base;
3985 tep->end.offset += aux->seg_base;
3986 tep->info.offset += aux->seg_base;
3987 }
3988 free (table);
3989
3990 /* Third, apply any relocations to the unwind table: */
3991
3992 for (relsec = section_headers;
3993 relsec < section_headers + elf_header.e_shnum;
3994 ++relsec)
3995 {
3996 if (relsec->sh_type != SHT_RELA
3997 || SECTION_HEADER (relsec->sh_info) != sec)
3998 continue;
3999
4000 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
4001 & rela, & nrelas))
4002 return 0;
4003
4004 for (rp = rela; rp < rela + nrelas; ++rp)
4005 {
4006 if (is_32bit_elf)
4007 {
4008 relname = elf_ia64_reloc_type (ELF32_R_TYPE (rp->r_info));
4009 sym = aux->symtab + ELF32_R_SYM (rp->r_info);
4010
4011 if (ELF32_ST_TYPE (sym->st_info) != STT_SECTION)
4012 {
4013 warn (_("Skipping unexpected symbol type %u\n"),
4014 ELF32_ST_TYPE (sym->st_info));
4015 continue;
4016 }
4017 }
4018 else
4019 {
4020 relname = elf_ia64_reloc_type (ELF64_R_TYPE (rp->r_info));
4021 sym = aux->symtab + ELF64_R_SYM (rp->r_info);
4022
4023 if (ELF64_ST_TYPE (sym->st_info) != STT_SECTION)
4024 {
4025 warn (_("Skipping unexpected symbol type %u\n"),
4026 ELF64_ST_TYPE (sym->st_info));
4027 continue;
4028 }
4029 }
4030
4031 if (strncmp (relname, "R_IA64_SEGREL", 13) != 0)
4032 {
4033 warn (_("Skipping unexpected relocation type %s\n"), relname);
4034 continue;
4035 }
4036
4037 i = rp->r_offset / (3 * addr_size);
4038
4039 switch (rp->r_offset/addr_size % 3)
4040 {
4041 case 0:
4042 aux->table[i].start.section = sym->st_shndx;
4043 aux->table[i].start.offset += rp->r_addend;
4044 break;
4045 case 1:
4046 aux->table[i].end.section = sym->st_shndx;
4047 aux->table[i].end.offset += rp->r_addend;
4048 break;
4049 case 2:
4050 aux->table[i].info.section = sym->st_shndx;
4051 aux->table[i].info.offset += rp->r_addend;
4052 break;
4053 default:
4054 break;
4055 }
4056 }
4057
4058 free (rela);
4059 }
4060
4061 aux->table_len = size / (3 * addr_size);
4062 return 1;
4063 }
4064
4065 static int
4066 process_unwind (FILE *file)
4067 {
4068 Elf_Internal_Shdr *sec, *unwsec = NULL, *strsec;
4069 unsigned long i, addr_size, unwcount = 0, unwstart = 0;
4070 struct unw_aux_info aux;
4071
4072 if (!do_unwind)
4073 return 1;
4074
4075 if (elf_header.e_machine != EM_IA_64)
4076 {
4077 printf (_("\nThere are no unwind sections in this file.\n"));
4078 return 1;
4079 }
4080
4081 memset (& aux, 0, sizeof (aux));
4082
4083 addr_size = is_32bit_elf ? 4 : 8;
4084
4085 for (i = 0, sec = section_headers; i < elf_header.e_shnum; ++i, ++sec)
4086 {
4087 if (sec->sh_type == SHT_SYMTAB)
4088 {
4089 aux.nsyms = sec->sh_size / sec->sh_entsize;
4090 aux.symtab = GET_ELF_SYMBOLS (file, sec);
4091
4092 strsec = SECTION_HEADER (sec->sh_link);
4093 aux.strtab_size = strsec->sh_size;
4094 aux.strtab = get_data (NULL, file, strsec->sh_offset,
4095 aux.strtab_size, _("string table"));
4096 }
4097 else if (sec->sh_type == SHT_IA_64_UNWIND)
4098 unwcount++;
4099 }
4100
4101 if (!unwcount)
4102 printf (_("\nThere are no unwind sections in this file.\n"));
4103
4104 while (unwcount-- > 0)
4105 {
4106 char *suffix;
4107 size_t len, len2;
4108
4109 for (i = unwstart, sec = section_headers + unwstart;
4110 i < elf_header.e_shnum; ++i, ++sec)
4111 if (sec->sh_type == SHT_IA_64_UNWIND)
4112 {
4113 unwsec = sec;
4114 break;
4115 }
4116
4117 unwstart = i + 1;
4118 len = sizeof (ELF_STRING_ia64_unwind_once) - 1;
4119
4120 if (strncmp (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind_once,
4121 len) == 0)
4122 {
4123 /* .gnu.linkonce.ia64unw.FOO -> .gnu.linkonce.ia64unwi.FOO */
4124 len2 = sizeof (ELF_STRING_ia64_unwind_info_once) - 1;
4125 suffix = SECTION_NAME (unwsec) + len;
4126 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
4127 ++i, ++sec)
4128 if (strncmp (SECTION_NAME (sec),
4129 ELF_STRING_ia64_unwind_info_once, len2) == 0
4130 && strcmp (SECTION_NAME (sec) + len2, suffix) == 0)
4131 break;
4132 }
4133 else
4134 {
4135 /* .IA_64.unwindFOO -> .IA_64.unwind_infoFOO
4136 .IA_64.unwind or BAR -> .IA_64.unwind_info */
4137 len = sizeof (ELF_STRING_ia64_unwind) - 1;
4138 len2 = sizeof (ELF_STRING_ia64_unwind_info) - 1;
4139 suffix = "";
4140 if (strncmp (SECTION_NAME (unwsec), ELF_STRING_ia64_unwind,
4141 len) == 0)
4142 suffix = SECTION_NAME (unwsec) + len;
4143 for (i = 0, sec = section_headers; i < elf_header.e_shnum;
4144 ++i, ++sec)
4145 if (strncmp (SECTION_NAME (sec),
4146 ELF_STRING_ia64_unwind_info, len2) == 0
4147 && strcmp (SECTION_NAME (sec) + len2, suffix) == 0)
4148 break;
4149 }
4150
4151 if (i == elf_header.e_shnum)
4152 {
4153 printf (_("\nCould not find unwind info section for "));
4154
4155 if (string_table == NULL)
4156 printf ("%d", unwsec->sh_name);
4157 else
4158 printf (_("'%s'"), SECTION_NAME (unwsec));
4159 }
4160 else
4161 {
4162 aux.info_size = sec->sh_size;
4163 aux.info_addr = sec->sh_addr;
4164 aux.info = get_data (NULL, file, sec->sh_offset, aux.info_size,
4165 _("unwind info"));
4166
4167 printf (_("\nUnwind section "));
4168
4169 if (string_table == NULL)
4170 printf ("%d", unwsec->sh_name);
4171 else
4172 printf (_("'%s'"), SECTION_NAME (unwsec));
4173
4174 printf (_(" at offset 0x%lx contains %lu entries:\n"),
4175 (unsigned long) unwsec->sh_offset,
4176 (unsigned long) (unwsec->sh_size / (3 * addr_size)));
4177
4178 (void) slurp_ia64_unwind_table (file, & aux, unwsec);
4179
4180 if (aux.table_len > 0)
4181 dump_ia64_unwind (& aux);
4182
4183 if (aux.table)
4184 free ((char *) aux.table);
4185 if (aux.info)
4186 free ((char *) aux.info);
4187 aux.table = NULL;
4188 aux.info = NULL;
4189 }
4190 }
4191
4192 if (aux.symtab)
4193 free (aux.symtab);
4194 if (aux.strtab)
4195 free ((char *) aux.strtab);
4196
4197 return 1;
4198 }
4199
4200 static void
4201 dynamic_segment_mips_val (Elf_Internal_Dyn *entry)
4202 {
4203 switch (entry->d_tag)
4204 {
4205 case DT_MIPS_FLAGS:
4206 if (entry->d_un.d_val == 0)
4207 printf ("NONE\n");
4208 else
4209 {
4210 static const char * opts[] =
4211 {
4212 "QUICKSTART", "NOTPOT", "NO_LIBRARY_REPLACEMENT",
4213 "NO_MOVE", "SGI_ONLY", "GUARANTEE_INIT", "DELTA_C_PLUS_PLUS",
4214 "GUARANTEE_START_INIT", "PIXIE", "DEFAULT_DELAY_LOAD",
4215 "REQUICKSTART", "REQUICKSTARTED", "CORD", "NO_UNRES_UNDEF",
4216 "RLD_ORDER_SAFE"
4217 };
4218 unsigned int cnt;
4219 int first = 1;
4220 for (cnt = 0; cnt < NUM_ELEM (opts); ++cnt)
4221 if (entry->d_un.d_val & (1 << cnt))
4222 {
4223 printf ("%s%s", first ? "" : " ", opts[cnt]);
4224 first = 0;
4225 }
4226 puts ("");
4227 }
4228 break;
4229
4230 case DT_MIPS_IVERSION:
4231 if (dynamic_strings != NULL)
4232 printf ("Interface Version: %s\n",
4233 dynamic_strings + entry->d_un.d_val);
4234 else
4235 printf ("%ld\n", (long) entry->d_un.d_ptr);
4236 break;
4237
4238 case DT_MIPS_TIME_STAMP:
4239 {
4240 char timebuf[20];
4241 struct tm *tmp;
4242
4243 time_t time = entry->d_un.d_val;
4244 tmp = gmtime (&time);
4245 sprintf (timebuf, "%04u-%02u-%02uT%02u:%02u:%02u",
4246 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
4247 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
4248 printf ("Time Stamp: %s\n", timebuf);
4249 }
4250 break;
4251
4252 case DT_MIPS_RLD_VERSION:
4253 case DT_MIPS_LOCAL_GOTNO:
4254 case DT_MIPS_CONFLICTNO:
4255 case DT_MIPS_LIBLISTNO:
4256 case DT_MIPS_SYMTABNO:
4257 case DT_MIPS_UNREFEXTNO:
4258 case DT_MIPS_HIPAGENO:
4259 case DT_MIPS_DELTA_CLASS_NO:
4260 case DT_MIPS_DELTA_INSTANCE_NO:
4261 case DT_MIPS_DELTA_RELOC_NO:
4262 case DT_MIPS_DELTA_SYM_NO:
4263 case DT_MIPS_DELTA_CLASSSYM_NO:
4264 case DT_MIPS_COMPACT_SIZE:
4265 printf ("%ld\n", (long) entry->d_un.d_ptr);
4266 break;
4267
4268 default:
4269 printf ("%#lx\n", (long) entry->d_un.d_ptr);
4270 }
4271 }
4272
4273
4274 static void
4275 dynamic_segment_parisc_val (Elf_Internal_Dyn *entry)
4276 {
4277 switch (entry->d_tag)
4278 {
4279 case DT_HP_DLD_FLAGS:
4280 {
4281 static struct
4282 {
4283 long int bit;
4284 const char *str;
4285 }
4286 flags[] =
4287 {
4288 { DT_HP_DEBUG_PRIVATE, "HP_DEBUG_PRIVATE" },
4289 { DT_HP_DEBUG_CALLBACK, "HP_DEBUG_CALLBACK" },
4290 { DT_HP_DEBUG_CALLBACK_BOR, "HP_DEBUG_CALLBACK_BOR" },
4291 { DT_HP_NO_ENVVAR, "HP_NO_ENVVAR" },
4292 { DT_HP_BIND_NOW, "HP_BIND_NOW" },
4293 { DT_HP_BIND_NONFATAL, "HP_BIND_NONFATAL" },
4294 { DT_HP_BIND_VERBOSE, "HP_BIND_VERBOSE" },
4295 { DT_HP_BIND_RESTRICTED, "HP_BIND_RESTRICTED" },
4296 { DT_HP_BIND_SYMBOLIC, "HP_BIND_SYMBOLIC" },
4297 { DT_HP_RPATH_FIRST, "HP_RPATH_FIRST" },
4298 { DT_HP_BIND_DEPTH_FIRST, "HP_BIND_DEPTH_FIRST" }
4299 };
4300 int first = 1;
4301 size_t cnt;
4302 bfd_vma val = entry->d_un.d_val;
4303
4304 for (cnt = 0; cnt < sizeof (flags) / sizeof (flags[0]); ++cnt)
4305 if (val & flags[cnt].bit)
4306 {
4307 if (! first)
4308 putchar (' ');
4309 fputs (flags[cnt].str, stdout);
4310 first = 0;
4311 val ^= flags[cnt].bit;
4312 }
4313
4314 if (val != 0 || first)
4315 {
4316 if (! first)
4317 putchar (' ');
4318 print_vma (val, HEX);
4319 }
4320 }
4321 break;
4322
4323 default:
4324 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
4325 break;
4326 }
4327 putchar ('\n');
4328 }
4329
4330 static void
4331 dynamic_segment_ia64_val (Elf_Internal_Dyn *entry)
4332 {
4333 switch (entry->d_tag)
4334 {
4335 case DT_IA_64_PLT_RESERVE:
4336 /* First 3 slots reserved. */
4337 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
4338 printf (" -- ");
4339 print_vma (entry->d_un.d_ptr + (3 * 8), PREFIX_HEX);
4340 break;
4341
4342 default:
4343 print_vma (entry->d_un.d_ptr, PREFIX_HEX);
4344 break;
4345 }
4346 putchar ('\n');
4347 }
4348
4349 static int
4350 get_32bit_dynamic_segment (FILE *file)
4351 {
4352 Elf32_External_Dyn *edyn;
4353 Elf_Internal_Dyn *entry;
4354 bfd_size_type i;
4355
4356 edyn = get_data (NULL, file, dynamic_addr, dynamic_size,
4357 _("dynamic segment"));
4358 if (!edyn)
4359 return 0;
4360
4361 /* SGI's ELF has more than one section in the DYNAMIC segment. Determine
4362 how large this .dynamic is now. We can do this even before the byte
4363 swapping since the DT_NULL tag is recognizable. */
4364 dynamic_size = 0;
4365 while (*(Elf32_Word *) edyn[dynamic_size++].d_tag != DT_NULL)
4366 ;
4367
4368 dynamic_segment = malloc (dynamic_size * sizeof (Elf_Internal_Dyn));
4369
4370 if (dynamic_segment == NULL)
4371 {
4372 error (_("Out of memory\n"));
4373 free (edyn);
4374 return 0;
4375 }
4376
4377 for (i = 0, entry = dynamic_segment;
4378 i < dynamic_size;
4379 i++, entry++)
4380 {
4381 entry->d_tag = BYTE_GET (edyn[i].d_tag);
4382 entry->d_un.d_val = BYTE_GET (edyn[i].d_un.d_val);
4383 }
4384
4385 free (edyn);
4386
4387 return 1;
4388 }
4389
4390 static int
4391 get_64bit_dynamic_segment (FILE *file)
4392 {
4393 Elf64_External_Dyn *edyn;
4394 Elf_Internal_Dyn *entry;
4395 bfd_size_type i;
4396
4397 edyn = get_data (NULL, file, dynamic_addr, dynamic_size,
4398 _("dynamic segment"));
4399 if (!edyn)
4400 return 0;
4401
4402 /* SGI's ELF has more than one section in the DYNAMIC segment. Determine
4403 how large this .dynamic is now. We can do this even before the byte
4404 swapping since the DT_NULL tag is recognizable. */
4405 dynamic_size = 0;
4406 while (*(bfd_vma *) edyn[dynamic_size++].d_tag != DT_NULL)
4407 ;
4408
4409 dynamic_segment = malloc (dynamic_size * sizeof (Elf_Internal_Dyn));
4410
4411 if (dynamic_segment == NULL)
4412 {
4413 error (_("Out of memory\n"));
4414 free (edyn);
4415 return 0;
4416 }
4417
4418 for (i = 0, entry = dynamic_segment;
4419 i < dynamic_size;
4420 i++, entry++)
4421 {
4422 entry->d_tag = BYTE_GET8 (edyn[i].d_tag);
4423 entry->d_un.d_val = BYTE_GET8 (edyn[i].d_un.d_val);
4424 }
4425
4426 free (edyn);
4427
4428 return 1;
4429 }
4430
4431 static const char *
4432 get_dynamic_flags (bfd_vma flags)
4433 {
4434 static char buff[128];
4435 char *p = buff;
4436
4437 *p = '\0';
4438 while (flags)
4439 {
4440 bfd_vma flag;
4441
4442 flag = flags & - flags;
4443 flags &= ~ flag;
4444
4445 if (p != buff)
4446 *p++ = ' ';
4447
4448 switch (flag)
4449 {
4450 case DF_ORIGIN: strcpy (p, "ORIGIN"); break;
4451 case DF_SYMBOLIC: strcpy (p, "SYMBOLIC"); break;
4452 case DF_TEXTREL: strcpy (p, "TEXTREL"); break;
4453 case DF_BIND_NOW: strcpy (p, "BIND_NOW"); break;
4454 case DF_STATIC_TLS: strcpy (p, "STATIC_TLS"); break;
4455 default: strcpy (p, "unknown"); break;
4456 }
4457
4458 p = strchr (p, '\0');
4459 }
4460 return buff;
4461 }
4462
4463 /* Parse and display the contents of the dynamic segment. */
4464 static int
4465 process_dynamic_segment (FILE *file)
4466 {
4467 Elf_Internal_Dyn *entry;
4468 bfd_size_type i;
4469
4470 if (dynamic_size == 0)
4471 {
4472 if (do_dynamic)
4473 printf (_("\nThere is no dynamic segment in this file.\n"));
4474
4475 return 1;
4476 }
4477
4478 if (is_32bit_elf)
4479 {
4480 if (! get_32bit_dynamic_segment (file))
4481 return 0;
4482 }
4483 else if (! get_64bit_dynamic_segment (file))
4484 return 0;
4485
4486 /* Find the appropriate symbol table. */
4487 if (dynamic_symbols == NULL)
4488 {
4489 for (i = 0, entry = dynamic_segment;
4490 i < dynamic_size;
4491 ++i, ++entry)
4492 {
4493 Elf_Internal_Shdr section;
4494
4495 if (entry->d_tag != DT_SYMTAB)
4496 continue;
4497
4498 dynamic_info[DT_SYMTAB] = entry->d_un.d_val;
4499
4500 /* Since we do not know how big the symbol table is,
4501 we default to reading in the entire file (!) and
4502 processing that. This is overkill, I know, but it
4503 should work. */
4504 section.sh_offset = offset_from_vma (file, entry->d_un.d_val, 0);
4505
4506 if (fseek (file, 0, SEEK_END))
4507 error (_("Unable to seek to end of file!"));
4508
4509 section.sh_size = ftell (file) - section.sh_offset;
4510 if (is_32bit_elf)
4511 section.sh_entsize = sizeof (Elf32_External_Sym);
4512 else
4513 section.sh_entsize = sizeof (Elf64_External_Sym);
4514
4515 num_dynamic_syms = section.sh_size / section.sh_entsize;
4516 if (num_dynamic_syms < 1)
4517 {
4518 error (_("Unable to determine the number of symbols to load\n"));
4519 continue;
4520 }
4521
4522 dynamic_symbols = GET_ELF_SYMBOLS (file, &section);
4523 }
4524 }
4525
4526 /* Similarly find a string table. */
4527 if (dynamic_strings == NULL)
4528 {
4529 for (i = 0, entry = dynamic_segment;
4530 i < dynamic_size;
4531 ++i, ++entry)
4532 {
4533 unsigned long offset;
4534 long str_tab_len;
4535
4536 if (entry->d_tag != DT_STRTAB)
4537 continue;
4538
4539 dynamic_info[DT_STRTAB] = entry->d_un.d_val;
4540
4541 /* Since we do not know how big the string table is,
4542 we default to reading in the entire file (!) and
4543 processing that. This is overkill, I know, but it
4544 should work. */
4545
4546 offset = offset_from_vma (file, entry->d_un.d_val, 0);
4547 if (fseek (file, 0, SEEK_END))
4548 error (_("Unable to seek to end of file\n"));
4549 str_tab_len = ftell (file) - offset;
4550
4551 if (str_tab_len < 1)
4552 {
4553 error
4554 (_("Unable to determine the length of the dynamic string table\n"));
4555 continue;
4556 }
4557
4558 dynamic_strings = get_data (NULL, file, offset, str_tab_len,
4559 _("dynamic string table"));
4560 break;
4561 }
4562 }
4563
4564 /* And find the syminfo section if available. */
4565 if (dynamic_syminfo == NULL)
4566 {
4567 unsigned long syminsz = 0;
4568
4569 for (i = 0, entry = dynamic_segment;
4570 i < dynamic_size;
4571 ++i, ++entry)
4572 {
4573 if (entry->d_tag == DT_SYMINENT)
4574 {
4575 /* Note: these braces are necessary to avoid a syntax
4576 error from the SunOS4 C compiler. */
4577 assert (sizeof (Elf_External_Syminfo) == entry->d_un.d_val);
4578 }
4579 else if (entry->d_tag == DT_SYMINSZ)
4580 syminsz = entry->d_un.d_val;
4581 else if (entry->d_tag == DT_SYMINFO)
4582 dynamic_syminfo_offset = offset_from_vma (file, entry->d_un.d_val,
4583 syminsz);
4584 }
4585
4586 if (dynamic_syminfo_offset != 0 && syminsz != 0)
4587 {
4588 Elf_External_Syminfo *extsyminfo;
4589 Elf_Internal_Syminfo *syminfo;
4590
4591 /* There is a syminfo section. Read the data. */
4592 extsyminfo = get_data (NULL, file, dynamic_syminfo_offset, syminsz,
4593 _("symbol information"));
4594 if (!extsyminfo)
4595 return 0;
4596
4597 dynamic_syminfo = malloc (syminsz);
4598 if (dynamic_syminfo == NULL)
4599 {
4600 error (_("Out of memory\n"));
4601 return 0;
4602 }
4603
4604 dynamic_syminfo_nent = syminsz / sizeof (Elf_External_Syminfo);
4605 for (i = 0, syminfo = dynamic_syminfo; i < dynamic_syminfo_nent;
4606 ++i, ++syminfo)
4607 {
4608 syminfo->si_boundto = BYTE_GET (extsyminfo[i].si_boundto);
4609 syminfo->si_flags = BYTE_GET (extsyminfo[i].si_flags);
4610 }
4611
4612 free (extsyminfo);
4613 }
4614 }
4615
4616 if (do_dynamic && dynamic_addr)
4617 printf (_("\nDynamic segment at offset 0x%lx contains %ld entries:\n"),
4618 dynamic_addr, (long) dynamic_size);
4619 if (do_dynamic)
4620 printf (_(" Tag Type Name/Value\n"));
4621
4622 for (i = 0, entry = dynamic_segment;
4623 i < dynamic_size;
4624 i++, entry++)
4625 {
4626 if (do_dynamic)
4627 {
4628 const char *dtype;
4629
4630 putchar (' ');
4631 print_vma (entry->d_tag, FULL_HEX);
4632 dtype = get_dynamic_type (entry->d_tag);
4633 printf (" (%s)%*s", dtype,
4634 ((is_32bit_elf ? 27 : 19)
4635 - (int) strlen (dtype)),
4636 " ");
4637 }
4638
4639 switch (entry->d_tag)
4640 {
4641 case DT_FLAGS:
4642 if (do_dynamic)
4643 puts (get_dynamic_flags (entry->d_un.d_val));
4644 break;
4645
4646 case DT_AUXILIARY:
4647 case DT_FILTER:
4648 case DT_CONFIG:
4649 case DT_DEPAUDIT:
4650 case DT_AUDIT:
4651 if (do_dynamic)
4652 {
4653 switch (entry->d_tag)
4654 {
4655 case DT_AUXILIARY:
4656 printf (_("Auxiliary library"));
4657 break;
4658
4659 case DT_FILTER:
4660 printf (_("Filter library"));
4661 break;
4662
4663 case DT_CONFIG:
4664 printf (_("Configuration file"));
4665 break;
4666
4667 case DT_DEPAUDIT:
4668 printf (_("Dependency audit library"));
4669 break;
4670
4671 case DT_AUDIT:
4672 printf (_("Audit library"));
4673 break;
4674 }
4675
4676 if (dynamic_strings)
4677 printf (": [%s]\n", dynamic_strings + entry->d_un.d_val);
4678 else
4679 {
4680 printf (": ");
4681 print_vma (entry->d_un.d_val, PREFIX_HEX);
4682 putchar ('\n');
4683 }
4684 }
4685 break;
4686
4687 case DT_FEATURE:
4688 if (do_dynamic)
4689 {
4690 printf (_("Flags:"));
4691
4692 if (entry->d_un.d_val == 0)
4693 printf (_(" None\n"));
4694 else
4695 {
4696 unsigned long int val = entry->d_un.d_val;
4697
4698 if (val & DTF_1_PARINIT)
4699 {
4700 printf (" PARINIT");
4701 val ^= DTF_1_PARINIT;
4702 }
4703 if (val & DTF_1_CONFEXP)
4704 {
4705 printf (" CONFEXP");
4706 val ^= DTF_1_CONFEXP;
4707 }
4708 if (val != 0)
4709 printf (" %lx", val);
4710 puts ("");
4711 }
4712 }
4713 break;
4714
4715 case DT_POSFLAG_1:
4716 if (do_dynamic)
4717 {
4718 printf (_("Flags:"));
4719
4720 if (entry->d_un.d_val == 0)
4721 printf (_(" None\n"));
4722 else
4723 {
4724 unsigned long int val = entry->d_un.d_val;
4725
4726 if (val & DF_P1_LAZYLOAD)
4727 {
4728 printf (" LAZYLOAD");
4729 val ^= DF_P1_LAZYLOAD;
4730 }
4731 if (val & DF_P1_GROUPPERM)
4732 {
4733 printf (" GROUPPERM");
4734 val ^= DF_P1_GROUPPERM;
4735 }
4736 if (val != 0)
4737 printf (" %lx", val);
4738 puts ("");
4739 }
4740 }
4741 break;
4742
4743 case DT_FLAGS_1:
4744 if (do_dynamic)
4745 {
4746 printf (_("Flags:"));
4747 if (entry->d_un.d_val == 0)
4748 printf (_(" None\n"));
4749 else
4750 {
4751 unsigned long int val = entry->d_un.d_val;
4752
4753 if (val & DF_1_NOW)
4754 {
4755 printf (" NOW");
4756 val ^= DF_1_NOW;
4757 }
4758 if (val & DF_1_GLOBAL)
4759 {
4760 printf (" GLOBAL");
4761 val ^= DF_1_GLOBAL;
4762 }
4763 if (val & DF_1_GROUP)
4764 {
4765 printf (" GROUP");
4766 val ^= DF_1_GROUP;
4767 }
4768 if (val & DF_1_NODELETE)
4769 {
4770 printf (" NODELETE");
4771 val ^= DF_1_NODELETE;
4772 }
4773 if (val & DF_1_LOADFLTR)
4774 {
4775 printf (" LOADFLTR");
4776 val ^= DF_1_LOADFLTR;
4777 }
4778 if (val & DF_1_INITFIRST)
4779 {
4780 printf (" INITFIRST");
4781 val ^= DF_1_INITFIRST;
4782 }
4783 if (val & DF_1_NOOPEN)
4784 {
4785 printf (" NOOPEN");
4786 val ^= DF_1_NOOPEN;
4787 }
4788 if (val & DF_1_ORIGIN)
4789 {
4790 printf (" ORIGIN");
4791 val ^= DF_1_ORIGIN;
4792 }
4793 if (val & DF_1_DIRECT)
4794 {
4795 printf (" DIRECT");
4796 val ^= DF_1_DIRECT;
4797 }
4798 if (val & DF_1_TRANS)
4799 {
4800 printf (" TRANS");
4801 val ^= DF_1_TRANS;
4802 }
4803 if (val & DF_1_INTERPOSE)
4804 {
4805 printf (" INTERPOSE");
4806 val ^= DF_1_INTERPOSE;
4807 }
4808 if (val & DF_1_NODEFLIB)
4809 {
4810 printf (" NODEFLIB");
4811 val ^= DF_1_NODEFLIB;
4812 }
4813 if (val & DF_1_NODUMP)
4814 {
4815 printf (" NODUMP");
4816 val ^= DF_1_NODUMP;
4817 }
4818 if (val & DF_1_CONLFAT)
4819 {
4820 printf (" CONLFAT");
4821 val ^= DF_1_CONLFAT;
4822 }
4823 if (val != 0)
4824 printf (" %lx", val);
4825 puts ("");
4826 }
4827 }
4828 break;
4829
4830 case DT_PLTREL:
4831 dynamic_info[entry->d_tag] = entry->d_un.d_val;
4832 if (do_dynamic)
4833 puts (get_dynamic_type (entry->d_un.d_val));
4834 break;
4835
4836 case DT_NULL :
4837 case DT_NEEDED :
4838 case DT_PLTGOT :
4839 case DT_HASH :
4840 case DT_STRTAB :
4841 case DT_SYMTAB :
4842 case DT_RELA :
4843 case DT_INIT :
4844 case DT_FINI :
4845 case DT_SONAME :
4846 case DT_RPATH :
4847 case DT_SYMBOLIC:
4848 case DT_REL :
4849 case DT_DEBUG :
4850 case DT_TEXTREL :
4851 case DT_JMPREL :
4852 case DT_RUNPATH :
4853 dynamic_info[entry->d_tag] = entry->d_un.d_val;
4854
4855 if (do_dynamic)
4856 {
4857 char *name;
4858
4859 if (dynamic_strings == NULL)
4860 name = NULL;
4861 else
4862 name = dynamic_strings + entry->d_un.d_val;
4863
4864 if (name)
4865 {
4866 switch (entry->d_tag)
4867 {
4868 case DT_NEEDED:
4869 printf (_("Shared library: [%s]"), name);
4870
4871 if (strcmp (name, program_interpreter) == 0)
4872 printf (_(" program interpreter"));
4873 break;
4874
4875 case DT_SONAME:
4876 printf (_("Library soname: [%s]"), name);
4877 break;
4878
4879 case DT_RPATH:
4880 printf (_("Library rpath: [%s]"), name);
4881 break;
4882
4883 case DT_RUNPATH:
4884 printf (_("Library runpath: [%s]"), name);
4885 break;
4886
4887 default:
4888 print_vma (entry->d_un.d_val, PREFIX_HEX);
4889 break;
4890 }
4891 }
4892 else
4893 print_vma (entry->d_un.d_val, PREFIX_HEX);
4894
4895 putchar ('\n');
4896 }
4897 break;
4898
4899 case DT_PLTRELSZ:
4900 case DT_RELASZ :
4901 case DT_STRSZ :
4902 case DT_RELSZ :
4903 case DT_RELAENT :
4904 case DT_SYMENT :
4905 case DT_RELENT :
4906 dynamic_info[entry->d_tag] = entry->d_un.d_val;
4907 case DT_PLTPADSZ:
4908 case DT_MOVEENT :
4909 case DT_MOVESZ :
4910 case DT_INIT_ARRAYSZ:
4911 case DT_FINI_ARRAYSZ:
4912 case DT_GNU_CONFLICTSZ:
4913 case DT_GNU_LIBLISTSZ:
4914 if (do_dynamic)
4915 {
4916 print_vma (entry->d_un.d_val, UNSIGNED);
4917 printf (" (bytes)\n");
4918 }
4919 break;
4920
4921 case DT_VERDEFNUM:
4922 case DT_VERNEEDNUM:
4923 case DT_RELACOUNT:
4924 case DT_RELCOUNT:
4925 if (do_dynamic)
4926 {
4927 print_vma (entry->d_un.d_val, UNSIGNED);
4928 putchar ('\n');
4929 }
4930 break;
4931
4932 case DT_SYMINSZ:
4933 case DT_SYMINENT:
4934 case DT_SYMINFO:
4935 case DT_USED:
4936 case DT_INIT_ARRAY:
4937 case DT_FINI_ARRAY:
4938 if (do_dynamic)
4939 {
4940 if (dynamic_strings != NULL && entry->d_tag == DT_USED)
4941 {
4942 char *name;
4943
4944 name = dynamic_strings + entry->d_un.d_val;
4945
4946 if (*name)
4947 {
4948 printf (_("Not needed object: [%s]\n"), name);
4949 break;
4950 }
4951 }
4952
4953 print_vma (entry->d_un.d_val, PREFIX_HEX);
4954 putchar ('\n');
4955 }
4956 break;
4957
4958 case DT_BIND_NOW:
4959 /* The value of this entry is ignored. */
4960 if (do_dynamic)
4961 putchar ('\n');
4962 break;
4963
4964 case DT_GNU_PRELINKED:
4965 if (do_dynamic)
4966 {
4967 struct tm *tmp;
4968 time_t time = entry->d_un.d_val;
4969
4970 tmp = gmtime (&time);
4971 printf ("%04u-%02u-%02uT%02u:%02u:%02u\n",
4972 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
4973 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
4974
4975 }
4976 break;
4977
4978 default:
4979 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
4980 version_info[DT_VERSIONTAGIDX (entry->d_tag)] =
4981 entry->d_un.d_val;
4982
4983 if (do_dynamic)
4984 {
4985 switch (elf_header.e_machine)
4986 {
4987 case EM_MIPS:
4988 case EM_MIPS_RS3_LE:
4989 dynamic_segment_mips_val (entry);
4990 break;
4991 case EM_PARISC:
4992 dynamic_segment_parisc_val (entry);
4993 break;
4994 case EM_IA_64:
4995 dynamic_segment_ia64_val (entry);
4996 break;
4997 default:
4998 print_vma (entry->d_un.d_val, PREFIX_HEX);
4999 putchar ('\n');
5000 }
5001 }
5002 break;
5003 }
5004 }
5005
5006 return 1;
5007 }
5008
5009 static char *
5010 get_ver_flags (unsigned int flags)
5011 {
5012 static char buff[32];
5013
5014 buff[0] = 0;
5015
5016 if (flags == 0)
5017 return _("none");
5018
5019 if (flags & VER_FLG_BASE)
5020 strcat (buff, "BASE ");
5021
5022 if (flags & VER_FLG_WEAK)
5023 {
5024 if (flags & VER_FLG_BASE)
5025 strcat (buff, "| ");
5026
5027 strcat (buff, "WEAK ");
5028 }
5029
5030 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK))
5031 strcat (buff, "| <unknown>");
5032
5033 return buff;
5034 }
5035
5036 /* Display the contents of the version sections. */
5037 static int
5038 process_version_sections (FILE *file)
5039 {
5040 Elf_Internal_Shdr *section;
5041 unsigned i;
5042 int found = 0;
5043
5044 if (! do_version)
5045 return 1;
5046
5047 for (i = 0, section = section_headers;
5048 i < elf_header.e_shnum;
5049 i++, section++)
5050 {
5051 switch (section->sh_type)
5052 {
5053 case SHT_GNU_verdef:
5054 {
5055 Elf_External_Verdef *edefs;
5056 unsigned int idx;
5057 unsigned int cnt;
5058
5059 found = 1;
5060
5061 printf
5062 (_("\nVersion definition section '%s' contains %ld entries:\n"),
5063 SECTION_NAME (section), section->sh_info);
5064
5065 printf (_(" Addr: 0x"));
5066 printf_vma (section->sh_addr);
5067 printf (_(" Offset: %#08lx Link: %lx (%s)\n"),
5068 (unsigned long) section->sh_offset, section->sh_link,
5069 SECTION_NAME (SECTION_HEADER (section->sh_link)));
5070
5071 edefs = get_data (NULL, file, section->sh_offset, section->sh_size,
5072 _("version definition section"));
5073 if (!edefs)
5074 break;
5075
5076 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
5077 {
5078 char *vstart;
5079 Elf_External_Verdef *edef;
5080 Elf_Internal_Verdef ent;
5081 Elf_External_Verdaux *eaux;
5082 Elf_Internal_Verdaux aux;
5083 int j;
5084 int isum;
5085
5086 vstart = ((char *) edefs) + idx;
5087
5088 edef = (Elf_External_Verdef *) vstart;
5089
5090 ent.vd_version = BYTE_GET (edef->vd_version);
5091 ent.vd_flags = BYTE_GET (edef->vd_flags);
5092 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
5093 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
5094 ent.vd_hash = BYTE_GET (edef->vd_hash);
5095 ent.vd_aux = BYTE_GET (edef->vd_aux);
5096 ent.vd_next = BYTE_GET (edef->vd_next);
5097
5098 printf (_(" %#06x: Rev: %d Flags: %s"),
5099 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
5100
5101 printf (_(" Index: %d Cnt: %d "),
5102 ent.vd_ndx, ent.vd_cnt);
5103
5104 vstart += ent.vd_aux;
5105
5106 eaux = (Elf_External_Verdaux *) vstart;
5107
5108 aux.vda_name = BYTE_GET (eaux->vda_name);
5109 aux.vda_next = BYTE_GET (eaux->vda_next);
5110
5111 if (dynamic_strings)
5112 printf (_("Name: %s\n"), dynamic_strings + aux.vda_name);
5113 else
5114 printf (_("Name index: %ld\n"), aux.vda_name);
5115
5116 isum = idx + ent.vd_aux;
5117
5118 for (j = 1; j < ent.vd_cnt; j++)
5119 {
5120 isum += aux.vda_next;
5121 vstart += aux.vda_next;
5122
5123 eaux = (Elf_External_Verdaux *) vstart;
5124
5125 aux.vda_name = BYTE_GET (eaux->vda_name);
5126 aux.vda_next = BYTE_GET (eaux->vda_next);
5127
5128 if (dynamic_strings)
5129 printf (_(" %#06x: Parent %d: %s\n"),
5130 isum, j, dynamic_strings + aux.vda_name);
5131 else
5132 printf (_(" %#06x: Parent %d, name index: %ld\n"),
5133 isum, j, aux.vda_name);
5134 }
5135
5136 idx += ent.vd_next;
5137 }
5138
5139 free (edefs);
5140 }
5141 break;
5142
5143 case SHT_GNU_verneed:
5144 {
5145 Elf_External_Verneed *eneed;
5146 unsigned int idx;
5147 unsigned int cnt;
5148
5149 found = 1;
5150
5151 printf (_("\nVersion needs section '%s' contains %ld entries:\n"),
5152 SECTION_NAME (section), section->sh_info);
5153
5154 printf (_(" Addr: 0x"));
5155 printf_vma (section->sh_addr);
5156 printf (_(" Offset: %#08lx Link to section: %ld (%s)\n"),
5157 (unsigned long) section->sh_offset, section->sh_link,
5158 SECTION_NAME (SECTION_HEADER (section->sh_link)));
5159
5160 eneed = get_data (NULL, file, section->sh_offset, section->sh_size,
5161 _("version need section"));
5162 if (!eneed)
5163 break;
5164
5165 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
5166 {
5167 Elf_External_Verneed *entry;
5168 Elf_Internal_Verneed ent;
5169 int j;
5170 int isum;
5171 char *vstart;
5172
5173 vstart = ((char *) eneed) + idx;
5174
5175 entry = (Elf_External_Verneed *) vstart;
5176
5177 ent.vn_version = BYTE_GET (entry->vn_version);
5178 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
5179 ent.vn_file = BYTE_GET (entry->vn_file);
5180 ent.vn_aux = BYTE_GET (entry->vn_aux);
5181 ent.vn_next = BYTE_GET (entry->vn_next);
5182
5183 printf (_(" %#06x: Version: %d"), idx, ent.vn_version);
5184
5185 if (dynamic_strings)
5186 printf (_(" File: %s"), dynamic_strings + ent.vn_file);
5187 else
5188 printf (_(" File: %lx"), ent.vn_file);
5189
5190 printf (_(" Cnt: %d\n"), ent.vn_cnt);
5191
5192 vstart += ent.vn_aux;
5193
5194 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
5195 {
5196 Elf_External_Vernaux *eaux;
5197 Elf_Internal_Vernaux aux;
5198
5199 eaux = (Elf_External_Vernaux *) vstart;
5200
5201 aux.vna_hash = BYTE_GET (eaux->vna_hash);
5202 aux.vna_flags = BYTE_GET (eaux->vna_flags);
5203 aux.vna_other = BYTE_GET (eaux->vna_other);
5204 aux.vna_name = BYTE_GET (eaux->vna_name);
5205 aux.vna_next = BYTE_GET (eaux->vna_next);
5206
5207 if (dynamic_strings)
5208 printf (_(" %#06x: Name: %s"),
5209 isum, dynamic_strings + aux.vna_name);
5210 else
5211 printf (_(" %#06x: Name index: %lx"),
5212 isum, aux.vna_name);
5213
5214 printf (_(" Flags: %s Version: %d\n"),
5215 get_ver_flags (aux.vna_flags), aux.vna_other);
5216
5217 isum += aux.vna_next;
5218 vstart += aux.vna_next;
5219 }
5220
5221 idx += ent.vn_next;
5222 }
5223
5224 free (eneed);
5225 }
5226 break;
5227
5228 case SHT_GNU_versym:
5229 {
5230 Elf_Internal_Shdr *link_section;
5231 int total;
5232 int cnt;
5233 unsigned char *edata;
5234 unsigned short *data;
5235 char *strtab;
5236 Elf_Internal_Sym *symbols;
5237 Elf_Internal_Shdr *string_sec;
5238 long off;
5239
5240 link_section = SECTION_HEADER (section->sh_link);
5241 total = section->sh_size / section->sh_entsize;
5242
5243 found = 1;
5244
5245 symbols = GET_ELF_SYMBOLS (file, link_section);
5246
5247 string_sec = SECTION_HEADER (link_section->sh_link);
5248
5249 strtab = get_data (NULL, file, string_sec->sh_offset,
5250 string_sec->sh_size, _("version string table"));
5251 if (!strtab)
5252 break;
5253
5254 printf (_("\nVersion symbols section '%s' contains %d entries:\n"),
5255 SECTION_NAME (section), total);
5256
5257 printf (_(" Addr: "));
5258 printf_vma (section->sh_addr);
5259 printf (_(" Offset: %#08lx Link: %lx (%s)\n"),
5260 (unsigned long) section->sh_offset, section->sh_link,
5261 SECTION_NAME (link_section));
5262
5263 off = offset_from_vma (file,
5264 version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
5265 total * sizeof (short));
5266 edata = get_data (NULL, file, off, total * sizeof (short),
5267 _("version symbol data"));
5268 if (!edata)
5269 {
5270 free (strtab);
5271 break;
5272 }
5273
5274 data = malloc (total * sizeof (short));
5275
5276 for (cnt = total; cnt --;)
5277 data[cnt] = byte_get (edata + cnt * sizeof (short),
5278 sizeof (short));
5279
5280 free (edata);
5281
5282 for (cnt = 0; cnt < total; cnt += 4)
5283 {
5284 int j, nn;
5285 int check_def, check_need;
5286 char *name;
5287
5288 printf (" %03x:", cnt);
5289
5290 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
5291 switch (data[cnt + j])
5292 {
5293 case 0:
5294 fputs (_(" 0 (*local*) "), stdout);
5295 break;
5296
5297 case 1:
5298 fputs (_(" 1 (*global*) "), stdout);
5299 break;
5300
5301 default:
5302 nn = printf ("%4x%c", data[cnt + j] & 0x7fff,
5303 data[cnt + j] & 0x8000 ? 'h' : ' ');
5304
5305 check_def = 1;
5306 check_need = 1;
5307 if (SECTION_HEADER (symbols[cnt + j].st_shndx)->sh_type
5308 != SHT_NOBITS)
5309 {
5310 if (symbols[cnt + j].st_shndx == SHN_UNDEF)
5311 check_def = 0;
5312 else
5313 check_need = 0;
5314 }
5315
5316 if (check_need
5317 && version_info[DT_VERSIONTAGIDX (DT_VERNEED)])
5318 {
5319 Elf_Internal_Verneed ivn;
5320 unsigned long offset;
5321
5322 offset = offset_from_vma
5323 (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
5324 sizeof (Elf_External_Verneed));
5325
5326 do
5327 {
5328 Elf_Internal_Vernaux ivna;
5329 Elf_External_Verneed evn;
5330 Elf_External_Vernaux evna;
5331 unsigned long a_off;
5332
5333 get_data (&evn, file, offset, sizeof (evn),
5334 _("version need"));
5335
5336 ivn.vn_aux = BYTE_GET (evn.vn_aux);
5337 ivn.vn_next = BYTE_GET (evn.vn_next);
5338
5339 a_off = offset + ivn.vn_aux;
5340
5341 do
5342 {
5343 get_data (&evna, file, a_off, sizeof (evna),
5344 _("version need aux (2)"));
5345
5346 ivna.vna_next = BYTE_GET (evna.vna_next);
5347 ivna.vna_other = BYTE_GET (evna.vna_other);
5348
5349 a_off += ivna.vna_next;
5350 }
5351 while (ivna.vna_other != data[cnt + j]
5352 && ivna.vna_next != 0);
5353
5354 if (ivna.vna_other == data[cnt + j])
5355 {
5356 ivna.vna_name = BYTE_GET (evna.vna_name);
5357
5358 name = strtab + ivna.vna_name;
5359 nn += printf ("(%s%-*s",
5360 name,
5361 12 - (int) strlen (name),
5362 ")");
5363 check_def = 0;
5364 break;
5365 }
5366
5367 offset += ivn.vn_next;
5368 }
5369 while (ivn.vn_next);
5370 }
5371
5372 if (check_def && data[cnt + j] != 0x8001
5373 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
5374 {
5375 Elf_Internal_Verdef ivd;
5376 Elf_External_Verdef evd;
5377 unsigned long offset;
5378
5379 offset = offset_from_vma
5380 (file, version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
5381 sizeof evd);
5382
5383 do
5384 {
5385 get_data (&evd, file, offset, sizeof (evd),
5386 _("version def"));
5387
5388 ivd.vd_next = BYTE_GET (evd.vd_next);
5389 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
5390
5391 offset += ivd.vd_next;
5392 }
5393 while (ivd.vd_ndx != (data[cnt + j] & 0x7fff)
5394 && ivd.vd_next != 0);
5395
5396 if (ivd.vd_ndx == (data[cnt + j] & 0x7fff))
5397 {
5398 Elf_External_Verdaux evda;
5399 Elf_Internal_Verdaux ivda;
5400
5401 ivd.vd_aux = BYTE_GET (evd.vd_aux);
5402
5403 get_data (&evda, file,
5404 offset - ivd.vd_next + ivd.vd_aux,
5405 sizeof (evda), _("version def aux"));
5406
5407 ivda.vda_name = BYTE_GET (evda.vda_name);
5408
5409 name = strtab + ivda.vda_name;
5410 nn += printf ("(%s%-*s",
5411 name,
5412 12 - (int) strlen (name),
5413 ")");
5414 }
5415 }
5416
5417 if (nn < 18)
5418 printf ("%*c", 18 - nn, ' ');
5419 }
5420
5421 putchar ('\n');
5422 }
5423
5424 free (data);
5425 free (strtab);
5426 free (symbols);
5427 }
5428 break;
5429
5430 default:
5431 break;
5432 }
5433 }
5434
5435 if (! found)
5436 printf (_("\nNo version information found in this file.\n"));
5437
5438 return 1;
5439 }
5440
5441 static const char *
5442 get_symbol_binding (unsigned int binding)
5443 {
5444 static char buff[32];
5445
5446 switch (binding)
5447 {
5448 case STB_LOCAL: return "LOCAL";
5449 case STB_GLOBAL: return "GLOBAL";
5450 case STB_WEAK: return "WEAK";
5451 default:
5452 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
5453 sprintf (buff, _("<processor specific>: %d"), binding);
5454 else if (binding >= STB_LOOS && binding <= STB_HIOS)
5455 sprintf (buff, _("<OS specific>: %d"), binding);
5456 else
5457 sprintf (buff, _("<unknown>: %d"), binding);
5458 return buff;
5459 }
5460 }
5461
5462 static const char *
5463 get_symbol_type (unsigned int type)
5464 {
5465 static char buff[32];
5466
5467 switch (type)
5468 {
5469 case STT_NOTYPE: return "NOTYPE";
5470 case STT_OBJECT: return "OBJECT";
5471 case STT_FUNC: return "FUNC";
5472 case STT_SECTION: return "SECTION";
5473 case STT_FILE: return "FILE";
5474 case STT_COMMON: return "COMMON";
5475 case STT_TLS: return "TLS";
5476 default:
5477 if (type >= STT_LOPROC && type <= STT_HIPROC)
5478 {
5479 if (elf_header.e_machine == EM_ARM && type == STT_ARM_TFUNC)
5480 return "THUMB_FUNC";
5481
5482 if (elf_header.e_machine == EM_SPARCV9 && type == STT_REGISTER)
5483 return "REGISTER";
5484
5485 if (elf_header.e_machine == EM_PARISC && type == STT_PARISC_MILLI)
5486 return "PARISC_MILLI";
5487
5488 sprintf (buff, _("<processor specific>: %d"), type);
5489 }
5490 else if (type >= STT_LOOS && type <= STT_HIOS)
5491 {
5492 if (elf_header.e_machine == EM_PARISC)
5493 {
5494 if (type == STT_HP_OPAQUE)
5495 return "HP_OPAQUE";
5496 if (type == STT_HP_STUB)
5497 return "HP_STUB";
5498 }
5499
5500 sprintf (buff, _("<OS specific>: %d"), type);
5501 }
5502 else
5503 sprintf (buff, _("<unknown>: %d"), type);
5504 return buff;
5505 }
5506 }
5507
5508 static const char *
5509 get_symbol_visibility (unsigned int visibility)
5510 {
5511 switch (visibility)
5512 {
5513 case STV_DEFAULT: return "DEFAULT";
5514 case STV_INTERNAL: return "INTERNAL";
5515 case STV_HIDDEN: return "HIDDEN";
5516 case STV_PROTECTED: return "PROTECTED";
5517 default: abort ();
5518 }
5519 }
5520
5521 static const char *
5522 get_symbol_index_type (unsigned int type)
5523 {
5524 static char buff[32];
5525
5526 switch (type)
5527 {
5528 case SHN_UNDEF: return "UND";
5529 case SHN_ABS: return "ABS";
5530 case SHN_COMMON: return "COM";
5531 default:
5532 if (type >= SHN_LOPROC && type <= SHN_HIPROC)
5533 sprintf (buff, "PRC[0x%04x]", type);
5534 else if (type >= SHN_LOOS && type <= SHN_HIOS)
5535 sprintf (buff, "OS [0x%04x]", type);
5536 else if (type >= SHN_LORESERVE && type <= SHN_HIRESERVE)
5537 sprintf (buff, "RSV[0x%04x]", type);
5538 else
5539 sprintf (buff, "%3d", type);
5540 break;
5541 }
5542
5543 return buff;
5544 }
5545
5546 static int *
5547 get_dynamic_data (FILE *file, unsigned int number)
5548 {
5549 unsigned char *e_data;
5550 int *i_data;
5551
5552 e_data = malloc (number * 4);
5553
5554 if (e_data == NULL)
5555 {
5556 error (_("Out of memory\n"));
5557 return NULL;
5558 }
5559
5560 if (fread (e_data, 4, number, file) != number)
5561 {
5562 error (_("Unable to read in dynamic data\n"));
5563 return NULL;
5564 }
5565
5566 i_data = malloc (number * sizeof (*i_data));
5567
5568 if (i_data == NULL)
5569 {
5570 error (_("Out of memory\n"));
5571 free (e_data);
5572 return NULL;
5573 }
5574
5575 while (number--)
5576 i_data[number] = byte_get (e_data + number * 4, 4);
5577
5578 free (e_data);
5579
5580 return i_data;
5581 }
5582
5583 /* Dump the symbol table. */
5584 static int
5585 process_symbol_table (FILE *file)
5586 {
5587 Elf_Internal_Shdr *section;
5588 unsigned char nb[4];
5589 unsigned char nc[4];
5590 int nbuckets = 0;
5591 int nchains = 0;
5592 int *buckets = NULL;
5593 int *chains = NULL;
5594
5595 if (! do_syms && !do_histogram)
5596 return 1;
5597
5598 if (dynamic_info[DT_HASH] && ((do_using_dynamic && dynamic_strings != NULL)
5599 || do_histogram))
5600 {
5601 if (fseek (file, offset_from_vma (file, dynamic_info[DT_HASH],
5602 sizeof nb + sizeof nc),
5603 SEEK_SET))
5604 {
5605 error (_("Unable to seek to start of dynamic information"));
5606 return 0;
5607 }
5608
5609 if (fread (nb, sizeof (nb), 1, file) != 1)
5610 {
5611 error (_("Failed to read in number of buckets\n"));
5612 return 0;
5613 }
5614
5615 if (fread (nc, sizeof (nc), 1, file) != 1)
5616 {
5617 error (_("Failed to read in number of chains\n"));
5618 return 0;
5619 }
5620
5621 nbuckets = byte_get (nb, 4);
5622 nchains = byte_get (nc, 4);
5623
5624 buckets = get_dynamic_data (file, nbuckets);
5625 chains = get_dynamic_data (file, nchains);
5626
5627 if (buckets == NULL || chains == NULL)
5628 return 0;
5629 }
5630
5631 if (do_syms
5632 && dynamic_info[DT_HASH] && do_using_dynamic && dynamic_strings != NULL)
5633 {
5634 int hn;
5635 int si;
5636
5637 printf (_("\nSymbol table for image:\n"));
5638 if (is_32bit_elf)
5639 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
5640 else
5641 printf (_(" Num Buc: Value Size Type Bind Vis Ndx Name\n"));
5642
5643 for (hn = 0; hn < nbuckets; hn++)
5644 {
5645 if (! buckets[hn])
5646 continue;
5647
5648 for (si = buckets[hn]; si < nchains && si > 0; si = chains[si])
5649 {
5650 Elf_Internal_Sym *psym;
5651
5652 psym = dynamic_symbols + si;
5653
5654 printf (" %3d %3d: ", si, hn);
5655 print_vma (psym->st_value, LONG_HEX);
5656 putchar (' ' );
5657 print_vma (psym->st_size, DEC_5);
5658
5659 printf (" %6s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
5660 printf (" %6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
5661 printf (" %3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
5662 printf (" %3.3s ", get_symbol_index_type (psym->st_shndx));
5663 print_symbol (25, dynamic_strings + psym->st_name);
5664 putchar ('\n');
5665 }
5666 }
5667 }
5668 else if (do_syms && !do_using_dynamic)
5669 {
5670 unsigned int i;
5671
5672 for (i = 0, section = section_headers;
5673 i < elf_header.e_shnum;
5674 i++, section++)
5675 {
5676 unsigned int si;
5677 char *strtab;
5678 Elf_Internal_Sym *symtab;
5679 Elf_Internal_Sym *psym;
5680
5681
5682 if ( section->sh_type != SHT_SYMTAB
5683 && section->sh_type != SHT_DYNSYM)
5684 continue;
5685
5686 printf (_("\nSymbol table '%s' contains %lu entries:\n"),
5687 SECTION_NAME (section),
5688 (unsigned long) (section->sh_size / section->sh_entsize));
5689 if (is_32bit_elf)
5690 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
5691 else
5692 printf (_(" Num: Value Size Type Bind Vis Ndx Name\n"));
5693
5694 symtab = GET_ELF_SYMBOLS (file, section);
5695 if (symtab == NULL)
5696 continue;
5697
5698 if (section->sh_link == elf_header.e_shstrndx)
5699 strtab = string_table;
5700 else
5701 {
5702 Elf_Internal_Shdr *string_sec;
5703
5704 string_sec = SECTION_HEADER (section->sh_link);
5705
5706 strtab = get_data (NULL, file, string_sec->sh_offset,
5707 string_sec->sh_size, _("string table"));
5708 }
5709
5710 for (si = 0, psym = symtab;
5711 si < section->sh_size / section->sh_entsize;
5712 si++, psym++)
5713 {
5714 printf ("%6d: ", si);
5715 print_vma (psym->st_value, LONG_HEX);
5716 putchar (' ');
5717 print_vma (psym->st_size, DEC_5);
5718 printf (" %-7s", get_symbol_type (ELF_ST_TYPE (psym->st_info)));
5719 printf (" %-6s", get_symbol_binding (ELF_ST_BIND (psym->st_info)));
5720 printf (" %-3s", get_symbol_visibility (ELF_ST_VISIBILITY (psym->st_other)));
5721 printf (" %4s ", get_symbol_index_type (psym->st_shndx));
5722 print_symbol (25, strtab + psym->st_name);
5723
5724 if (section->sh_type == SHT_DYNSYM &&
5725 version_info[DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
5726 {
5727 unsigned char data[2];
5728 unsigned short vers_data;
5729 unsigned long offset;
5730 int is_nobits;
5731 int check_def;
5732
5733 offset = offset_from_vma
5734 (file, version_info[DT_VERSIONTAGIDX (DT_VERSYM)],
5735 sizeof data + si * sizeof (vers_data));
5736
5737 get_data (&data, file, offset + si * sizeof (vers_data),
5738 sizeof (data), _("version data"));
5739
5740 vers_data = byte_get (data, 2);
5741
5742 is_nobits = (SECTION_HEADER (psym->st_shndx)->sh_type
5743 == SHT_NOBITS);
5744
5745 check_def = (psym->st_shndx != SHN_UNDEF);
5746
5747 if ((vers_data & 0x8000) || vers_data > 1)
5748 {
5749 if (version_info[DT_VERSIONTAGIDX (DT_VERNEED)]
5750 && (is_nobits || ! check_def))
5751 {
5752 Elf_External_Verneed evn;
5753 Elf_Internal_Verneed ivn;
5754 Elf_Internal_Vernaux ivna;
5755
5756 /* We must test both. */
5757 offset = offset_from_vma
5758 (file, version_info[DT_VERSIONTAGIDX (DT_VERNEED)],
5759 sizeof evn);
5760
5761 do
5762 {
5763 unsigned long vna_off;
5764
5765 get_data (&evn, file, offset, sizeof (evn),
5766 _("version need"));
5767
5768 ivn.vn_aux = BYTE_GET (evn.vn_aux);
5769 ivn.vn_next = BYTE_GET (evn.vn_next);
5770
5771 vna_off = offset + ivn.vn_aux;
5772
5773 do
5774 {
5775 Elf_External_Vernaux evna;
5776
5777 get_data (&evna, file, vna_off,
5778 sizeof (evna),
5779 _("version need aux (3)"));
5780
5781 ivna.vna_other = BYTE_GET (evna.vna_other);
5782 ivna.vna_next = BYTE_GET (evna.vna_next);
5783 ivna.vna_name = BYTE_GET (evna.vna_name);
5784
5785 vna_off += ivna.vna_next;
5786 }
5787 while (ivna.vna_other != vers_data
5788 && ivna.vna_next != 0);
5789
5790 if (ivna.vna_other == vers_data)
5791 break;
5792
5793 offset += ivn.vn_next;
5794 }
5795 while (ivn.vn_next != 0);
5796
5797 if (ivna.vna_other == vers_data)
5798 {
5799 printf ("@%s (%d)",
5800 strtab + ivna.vna_name, ivna.vna_other);
5801 check_def = 0;
5802 }
5803 else if (! is_nobits)
5804 error (_("bad dynamic symbol"));
5805 else
5806 check_def = 1;
5807 }
5808
5809 if (check_def)
5810 {
5811 if (vers_data != 0x8001
5812 && version_info[DT_VERSIONTAGIDX (DT_VERDEF)])
5813 {
5814 Elf_Internal_Verdef ivd;
5815 Elf_Internal_Verdaux ivda;
5816 Elf_External_Verdaux evda;
5817 unsigned long offset;
5818
5819 offset = offset_from_vma
5820 (file,
5821 version_info[DT_VERSIONTAGIDX (DT_VERDEF)],
5822 sizeof (Elf_External_Verdef));
5823
5824 do
5825 {
5826 Elf_External_Verdef evd;
5827
5828 get_data (&evd, file, offset, sizeof (evd),
5829 _("version def"));
5830
5831 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
5832 ivd.vd_aux = BYTE_GET (evd.vd_aux);
5833 ivd.vd_next = BYTE_GET (evd.vd_next);
5834
5835 offset += ivd.vd_next;
5836 }
5837 while (ivd.vd_ndx != (vers_data & 0x7fff)
5838 && ivd.vd_next != 0);
5839
5840 offset -= ivd.vd_next;
5841 offset += ivd.vd_aux;
5842
5843 get_data (&evda, file, offset, sizeof (evda),
5844 _("version def aux"));
5845
5846 ivda.vda_name = BYTE_GET (evda.vda_name);
5847
5848 if (psym->st_name != ivda.vda_name)
5849 printf ((vers_data & 0x8000)
5850 ? "@%s" : "@@%s",
5851 strtab + ivda.vda_name);
5852 }
5853 }
5854 }
5855 }
5856
5857 putchar ('\n');
5858 }
5859
5860 free (symtab);
5861 if (strtab != string_table)
5862 free (strtab);
5863 }
5864 }
5865 else if (do_syms)
5866 printf
5867 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
5868
5869 if (do_histogram && buckets != NULL)
5870 {
5871 int *lengths;
5872 int *counts;
5873 int hn;
5874 int si;
5875 int maxlength = 0;
5876 int nzero_counts = 0;
5877 int nsyms = 0;
5878
5879 printf (_("\nHistogram for bucket list length (total of %d buckets):\n"),
5880 nbuckets);
5881 printf (_(" Length Number %% of total Coverage\n"));
5882
5883 lengths = calloc (nbuckets, sizeof (int));
5884 if (lengths == NULL)
5885 {
5886 error (_("Out of memory"));
5887 return 0;
5888 }
5889 for (hn = 0; hn < nbuckets; ++hn)
5890 {
5891 if (! buckets[hn])
5892 continue;
5893
5894 for (si = buckets[hn]; si > 0 && si < nchains; si = chains[si])
5895 {
5896 ++nsyms;
5897 if (maxlength < ++lengths[hn])
5898 ++maxlength;
5899 }
5900 }
5901
5902 counts = calloc (maxlength + 1, sizeof (int));
5903 if (counts == NULL)
5904 {
5905 error (_("Out of memory"));
5906 return 0;
5907 }
5908
5909 for (hn = 0; hn < nbuckets; ++hn)
5910 ++counts[lengths[hn]];
5911
5912 if (nbuckets > 0)
5913 {
5914 printf (" 0 %-10d (%5.1f%%)\n",
5915 counts[0], (counts[0] * 100.0) / nbuckets);
5916 for (si = 1; si <= maxlength; ++si)
5917 {
5918 nzero_counts += counts[si] * si;
5919 printf ("%7d %-10d (%5.1f%%) %5.1f%%\n",
5920 si, counts[si], (counts[si] * 100.0) / nbuckets,
5921 (nzero_counts * 100.0) / nsyms);
5922 }
5923 }
5924
5925 free (counts);
5926 free (lengths);
5927 }
5928
5929 if (buckets != NULL)
5930 {
5931 free (buckets);
5932 free (chains);
5933 }
5934
5935 return 1;
5936 }
5937
5938 static int
5939 process_syminfo (FILE *file ATTRIBUTE_UNUSED)
5940 {
5941 unsigned int i;
5942
5943 if (dynamic_syminfo == NULL
5944 || !do_dynamic)
5945 /* No syminfo, this is ok. */
5946 return 1;
5947
5948 /* There better should be a dynamic symbol section. */
5949 if (dynamic_symbols == NULL || dynamic_strings == NULL)
5950 return 0;
5951
5952 if (dynamic_addr)
5953 printf (_("\nDynamic info segment at offset 0x%lx contains %d entries:\n"),
5954 dynamic_syminfo_offset, dynamic_syminfo_nent);
5955
5956 printf (_(" Num: Name BoundTo Flags\n"));
5957 for (i = 0; i < dynamic_syminfo_nent; ++i)
5958 {
5959 unsigned short int flags = dynamic_syminfo[i].si_flags;
5960
5961 printf ("%4d: ", i);
5962 print_symbol (30, dynamic_strings + dynamic_symbols[i].st_name);
5963 putchar (' ');
5964
5965 switch (dynamic_syminfo[i].si_boundto)
5966 {
5967 case SYMINFO_BT_SELF:
5968 fputs ("SELF ", stdout);
5969 break;
5970 case SYMINFO_BT_PARENT:
5971 fputs ("PARENT ", stdout);
5972 break;
5973 default:
5974 if (dynamic_syminfo[i].si_boundto > 0
5975 && dynamic_syminfo[i].si_boundto < dynamic_size)
5976 {
5977 print_symbol (10,
5978 dynamic_strings
5979 + (dynamic_segment
5980 [dynamic_syminfo[i].si_boundto].d_un.d_val));
5981 putchar (' ' );
5982 }
5983 else
5984 printf ("%-10d ", dynamic_syminfo[i].si_boundto);
5985 break;
5986 }
5987
5988 if (flags & SYMINFO_FLG_DIRECT)
5989 printf (" DIRECT");
5990 if (flags & SYMINFO_FLG_PASSTHRU)
5991 printf (" PASSTHRU");
5992 if (flags & SYMINFO_FLG_COPY)
5993 printf (" COPY");
5994 if (flags & SYMINFO_FLG_LAZYLOAD)
5995 printf (" LAZYLOAD");
5996
5997 puts ("");
5998 }
5999
6000 return 1;
6001 }
6002
6003 #ifdef SUPPORT_DISASSEMBLY
6004 static void
6005 disassemble_section (Elf_Internal_Shdr *section, FILE *file)
6006 {
6007 printf (_("\nAssembly dump of section %s\n"),
6008 SECTION_NAME (section));
6009
6010 /* XXX -- to be done --- XXX */
6011
6012 return 1;
6013 }
6014 #endif
6015
6016 static int
6017 dump_section (Elf_Internal_Shdr *section, FILE *file)
6018 {
6019 bfd_size_type bytes;
6020 bfd_vma addr;
6021 unsigned char *data;
6022 unsigned char *start;
6023
6024 bytes = section->sh_size;
6025
6026 if (bytes == 0)
6027 {
6028 printf (_("\nSection '%s' has no data to dump.\n"),
6029 SECTION_NAME (section));
6030 return 0;
6031 }
6032 else
6033 printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section));
6034
6035 addr = section->sh_addr;
6036
6037 start = get_data (NULL, file, section->sh_offset, bytes, _("section data"));
6038 if (!start)
6039 return 0;
6040
6041 data = start;
6042
6043 while (bytes)
6044 {
6045 int j;
6046 int k;
6047 int lbytes;
6048
6049 lbytes = (bytes > 16 ? 16 : bytes);
6050
6051 printf (" 0x%8.8lx ", (unsigned long) addr);
6052
6053 switch (elf_header.e_ident[EI_DATA])
6054 {
6055 default:
6056 case ELFDATA2LSB:
6057 for (j = 15; j >= 0; j --)
6058 {
6059 if (j < lbytes)
6060 printf ("%2.2x", data[j]);
6061 else
6062 printf (" ");
6063
6064 if (!(j & 0x3))
6065 printf (" ");
6066 }
6067 break;
6068
6069 case ELFDATA2MSB:
6070 for (j = 0; j < 16; j++)
6071 {
6072 if (j < lbytes)
6073 printf ("%2.2x", data[j]);
6074 else
6075 printf (" ");
6076
6077 if ((j & 3) == 3)
6078 printf (" ");
6079 }
6080 break;
6081 }
6082
6083 for (j = 0; j < lbytes; j++)
6084 {
6085 k = data[j];
6086 if (k >= ' ' && k < 0x80)
6087 printf ("%c", k);
6088 else
6089 printf (".");
6090 }
6091
6092 putchar ('\n');
6093
6094 data += lbytes;
6095 addr += lbytes;
6096 bytes -= lbytes;
6097 }
6098
6099 free (start);
6100
6101 return 1;
6102 }
6103
6104
6105 static unsigned long int
6106 read_leb128 (unsigned char *data, int *length_return, int sign)
6107 {
6108 unsigned long int result = 0;
6109 unsigned int num_read = 0;
6110 int shift = 0;
6111 unsigned char byte;
6112
6113 do
6114 {
6115 byte = *data++;
6116 num_read++;
6117
6118 result |= (byte & 0x7f) << shift;
6119
6120 shift += 7;
6121
6122 }
6123 while (byte & 0x80);
6124
6125 if (length_return != NULL)
6126 *length_return = num_read;
6127
6128 if (sign && (shift < 32) && (byte & 0x40))
6129 result |= -1 << shift;
6130
6131 return result;
6132 }
6133
6134 typedef struct State_Machine_Registers
6135 {
6136 unsigned long address;
6137 unsigned int file;
6138 unsigned int line;
6139 unsigned int column;
6140 int is_stmt;
6141 int basic_block;
6142 int end_sequence;
6143 /* This variable hold the number of the last entry seen
6144 in the File Table. */
6145 unsigned int last_file_entry;
6146 } SMR;
6147
6148 static SMR state_machine_regs;
6149
6150 static void
6151 reset_state_machine (int is_stmt)
6152 {
6153 state_machine_regs.address = 0;
6154 state_machine_regs.file = 1;
6155 state_machine_regs.line = 1;
6156 state_machine_regs.column = 0;
6157 state_machine_regs.is_stmt = is_stmt;
6158 state_machine_regs.basic_block = 0;
6159 state_machine_regs.end_sequence = 0;
6160 state_machine_regs.last_file_entry = 0;
6161 }
6162
6163 /* Handled an extend line op. Returns true if this is the end
6164 of sequence. */
6165 static int
6166 process_extended_line_op (unsigned char *data, int is_stmt, int pointer_size)
6167 {
6168 unsigned char op_code;
6169 int bytes_read;
6170 unsigned int len;
6171 unsigned char *name;
6172 unsigned long adr;
6173
6174 len = read_leb128 (data, & bytes_read, 0);
6175 data += bytes_read;
6176
6177 if (len == 0)
6178 {
6179 warn (_("badly formed extended line op encountered!\n"));
6180 return bytes_read;
6181 }
6182
6183 len += bytes_read;
6184 op_code = *data++;
6185
6186 printf (_(" Extended opcode %d: "), op_code);
6187
6188 switch (op_code)
6189 {
6190 case DW_LNE_end_sequence:
6191 printf (_("End of Sequence\n\n"));
6192 reset_state_machine (is_stmt);
6193 break;
6194
6195 case DW_LNE_set_address:
6196 adr = byte_get (data, pointer_size);
6197 printf (_("set Address to 0x%lx\n"), adr);
6198 state_machine_regs.address = adr;
6199 break;
6200
6201 case DW_LNE_define_file:
6202 printf (_(" define new File Table entry\n"));
6203 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
6204
6205 printf (_(" %d\t"), ++state_machine_regs.last_file_entry);
6206 name = data;
6207 data += strlen ((char *) data) + 1;
6208 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6209 data += bytes_read;
6210 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6211 data += bytes_read;
6212 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6213 printf (_("%s\n\n"), name);
6214 break;
6215
6216 default:
6217 printf (_("UNKNOWN: length %d\n"), len - bytes_read);
6218 break;
6219 }
6220
6221 return len;
6222 }
6223
6224 /* Size of pointers in the .debug_line section. This information is not
6225 really present in that section. It's obtained before dumping the debug
6226 sections by doing some pre-scan of the .debug_info section. */
6227 static int debug_line_pointer_size = 4;
6228
6229 static int
6230 display_debug_lines (Elf_Internal_Shdr *section,
6231 unsigned char * start,
6232 FILE *file ATTRIBUTE_UNUSED)
6233 {
6234 unsigned char *hdrptr;
6235 DWARF2_Internal_LineInfo info;
6236 unsigned char *standard_opcodes;
6237 unsigned char *data = start;
6238 unsigned char *end = start + section->sh_size;
6239 unsigned char *end_of_sequence;
6240 int i;
6241 int offset_size;
6242 int initial_length_size;
6243
6244 printf (_("\nDump of debug contents of section %s:\n\n"),
6245 SECTION_NAME (section));
6246
6247 while (data < end)
6248 {
6249 hdrptr = data;
6250
6251 /* Check the length of the block. */
6252 info.li_length = byte_get (hdrptr, 4);
6253 hdrptr += 4;
6254
6255 if (info.li_length == 0xffffffff)
6256 {
6257 /* This section is 64-bit DWARF 3. */
6258 info.li_length = byte_get (hdrptr, 8);
6259 hdrptr += 8;
6260 offset_size = 8;
6261 initial_length_size = 12;
6262 }
6263 else
6264 {
6265 offset_size = 4;
6266 initial_length_size = 4;
6267 }
6268
6269 if (info.li_length + initial_length_size > section->sh_size)
6270 {
6271 warn
6272 (_("The line info appears to be corrupt - the section is too small\n"));
6273 return 0;
6274 }
6275
6276 /* Check its version number. */
6277 info.li_version = byte_get (hdrptr, 2);
6278 hdrptr += 2;
6279 if (info.li_version != 2 && info.li_version != 3)
6280 {
6281 warn (_("Only DWARF version 2 and 3 line info is currently supported.\n"));
6282 return 0;
6283 }
6284
6285 info.li_prologue_length = byte_get (hdrptr, offset_size);
6286 hdrptr += offset_size;
6287 info.li_min_insn_length = byte_get (hdrptr, 1);
6288 hdrptr++;
6289 info.li_default_is_stmt = byte_get (hdrptr, 1);
6290 hdrptr++;
6291 info.li_line_base = byte_get (hdrptr, 1);
6292 hdrptr++;
6293 info.li_line_range = byte_get (hdrptr, 1);
6294 hdrptr++;
6295 info.li_opcode_base = byte_get (hdrptr, 1);
6296 hdrptr++;
6297
6298 /* Sign extend the line base field. */
6299 info.li_line_base <<= 24;
6300 info.li_line_base >>= 24;
6301
6302 printf (_(" Length: %ld\n"), info.li_length);
6303 printf (_(" DWARF Version: %d\n"), info.li_version);
6304 printf (_(" Prologue Length: %d\n"), info.li_prologue_length);
6305 printf (_(" Minimum Instruction Length: %d\n"), info.li_min_insn_length);
6306 printf (_(" Initial value of 'is_stmt': %d\n"), info.li_default_is_stmt);
6307 printf (_(" Line Base: %d\n"), info.li_line_base);
6308 printf (_(" Line Range: %d\n"), info.li_line_range);
6309 printf (_(" Opcode Base: %d\n"), info.li_opcode_base);
6310
6311 end_of_sequence = data + info.li_length + initial_length_size;
6312
6313 reset_state_machine (info.li_default_is_stmt);
6314
6315 /* Display the contents of the Opcodes table. */
6316 standard_opcodes = hdrptr;
6317
6318 printf (_("\n Opcodes:\n"));
6319
6320 for (i = 1; i < info.li_opcode_base; i++)
6321 printf (_(" Opcode %d has %d args\n"), i, standard_opcodes[i - 1]);
6322
6323 /* Display the contents of the Directory table. */
6324 data = standard_opcodes + info.li_opcode_base - 1;
6325
6326 if (*data == 0)
6327 printf (_("\n The Directory Table is empty.\n"));
6328 else
6329 {
6330 printf (_("\n The Directory Table:\n"));
6331
6332 while (*data != 0)
6333 {
6334 printf (_(" %s\n"), data);
6335
6336 data += strlen ((char *) data) + 1;
6337 }
6338 }
6339
6340 /* Skip the NUL at the end of the table. */
6341 data++;
6342
6343 /* Display the contents of the File Name table. */
6344 if (*data == 0)
6345 printf (_("\n The File Name Table is empty.\n"));
6346 else
6347 {
6348 printf (_("\n The File Name Table:\n"));
6349 printf (_(" Entry\tDir\tTime\tSize\tName\n"));
6350
6351 while (*data != 0)
6352 {
6353 unsigned char *name;
6354 int bytes_read;
6355
6356 printf (_(" %d\t"), ++state_machine_regs.last_file_entry);
6357 name = data;
6358
6359 data += strlen ((char *) data) + 1;
6360
6361 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6362 data += bytes_read;
6363 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6364 data += bytes_read;
6365 printf (_("%lu\t"), read_leb128 (data, & bytes_read, 0));
6366 data += bytes_read;
6367 printf (_("%s\n"), name);
6368 }
6369 }
6370
6371 /* Skip the NUL at the end of the table. */
6372 data++;
6373
6374 /* Now display the statements. */
6375 printf (_("\n Line Number Statements:\n"));
6376
6377
6378 while (data < end_of_sequence)
6379 {
6380 unsigned char op_code;
6381 int adv;
6382 int bytes_read;
6383
6384 op_code = *data++;
6385
6386 if (op_code >= info.li_opcode_base)
6387 {
6388 op_code -= info.li_opcode_base;
6389 adv = (op_code / info.li_line_range) * info.li_min_insn_length;
6390 state_machine_regs.address += adv;
6391 printf (_(" Special opcode %d: advance Address by %d to 0x%lx"),
6392 op_code, adv, state_machine_regs.address);
6393 adv = (op_code % info.li_line_range) + info.li_line_base;
6394 state_machine_regs.line += adv;
6395 printf (_(" and Line by %d to %d\n"),
6396 adv, state_machine_regs.line);
6397 }
6398 else switch (op_code)
6399 {
6400 case DW_LNS_extended_op:
6401 data += process_extended_line_op (data, info.li_default_is_stmt,
6402 debug_line_pointer_size);
6403 break;
6404
6405 case DW_LNS_copy:
6406 printf (_(" Copy\n"));
6407 break;
6408
6409 case DW_LNS_advance_pc:
6410 adv = info.li_min_insn_length * read_leb128 (data, & bytes_read, 0);
6411 data += bytes_read;
6412 state_machine_regs.address += adv;
6413 printf (_(" Advance PC by %d to %lx\n"), adv,
6414 state_machine_regs.address);
6415 break;
6416
6417 case DW_LNS_advance_line:
6418 adv = read_leb128 (data, & bytes_read, 1);
6419 data += bytes_read;
6420 state_machine_regs.line += adv;
6421 printf (_(" Advance Line by %d to %d\n"), adv,
6422 state_machine_regs.line);
6423 break;
6424
6425 case DW_LNS_set_file:
6426 adv = read_leb128 (data, & bytes_read, 0);
6427 data += bytes_read;
6428 printf (_(" Set File Name to entry %d in the File Name Table\n"),
6429 adv);
6430 state_machine_regs.file = adv;
6431 break;
6432
6433 case DW_LNS_set_column:
6434 adv = read_leb128 (data, & bytes_read, 0);
6435 data += bytes_read;
6436 printf (_(" Set column to %d\n"), adv);
6437 state_machine_regs.column = adv;
6438 break;
6439
6440 case DW_LNS_negate_stmt:
6441 adv = state_machine_regs.is_stmt;
6442 adv = ! adv;
6443 printf (_(" Set is_stmt to %d\n"), adv);
6444 state_machine_regs.is_stmt = adv;
6445 break;
6446
6447 case DW_LNS_set_basic_block:
6448 printf (_(" Set basic block\n"));
6449 state_machine_regs.basic_block = 1;
6450 break;
6451
6452 case DW_LNS_const_add_pc:
6453 adv = (((255 - info.li_opcode_base) / info.li_line_range)
6454 * info.li_min_insn_length);
6455 state_machine_regs.address += adv;
6456 printf (_(" Advance PC by constant %d to 0x%lx\n"), adv,
6457 state_machine_regs.address);
6458 break;
6459
6460 case DW_LNS_fixed_advance_pc:
6461 adv = byte_get (data, 2);
6462 data += 2;
6463 state_machine_regs.address += adv;
6464 printf (_(" Advance PC by fixed size amount %d to 0x%lx\n"),
6465 adv, state_machine_regs.address);
6466 break;
6467
6468 case DW_LNS_set_prologue_end:
6469 printf (_(" Set prologue_end to true\n"));
6470 break;
6471
6472 case DW_LNS_set_epilogue_begin:
6473 printf (_(" Set epilogue_begin to true\n"));
6474 break;
6475
6476 case DW_LNS_set_isa:
6477 adv = read_leb128 (data, & bytes_read, 0);
6478 data += bytes_read;
6479 printf (_(" Set ISA to %d\n"), adv);
6480 break;
6481
6482 default:
6483 printf (_(" Unknown opcode %d with operands: "), op_code);
6484 {
6485 int i;
6486 for (i = standard_opcodes[op_code - 1]; i > 0 ; --i)
6487 {
6488 printf ("0x%lx%s", read_leb128 (data, &bytes_read, 0),
6489 i == 1 ? "" : ", ");
6490 data += bytes_read;
6491 }
6492 putchar ('\n');
6493 }
6494 break;
6495 }
6496 }
6497 putchar ('\n');
6498 }
6499
6500 return 1;
6501 }
6502
6503 static int
6504 display_debug_pubnames (Elf_Internal_Shdr *section,
6505 unsigned char *start,
6506 FILE *file ATTRIBUTE_UNUSED)
6507 {
6508 DWARF2_Internal_PubNames pubnames;
6509 unsigned char *end;
6510
6511 end = start + section->sh_size;
6512
6513 printf (_("Contents of the %s section:\n\n"), SECTION_NAME (section));
6514
6515 while (start < end)
6516 {
6517 unsigned char *data;
6518 unsigned long offset;
6519 int offset_size, initial_length_size;
6520
6521 data = start;
6522
6523 pubnames.pn_length = byte_get (data, 4);
6524 data += 4;
6525 if (pubnames.pn_length == 0xffffffff)
6526 {
6527 pubnames.pn_length = byte_get (data, 8);
6528 data += 8;
6529 offset_size = 8;
6530 initial_length_size = 12;
6531 }
6532 else
6533 {
6534 offset_size = 4;
6535 initial_length_size = 4;
6536 }
6537
6538 pubnames.pn_version = byte_get (data, 2);
6539 data += 2;
6540 pubnames.pn_offset = byte_get (data, offset_size);
6541 data += offset_size;
6542 pubnames.pn_size = byte_get (data, offset_size);
6543 data += offset_size;
6544
6545 start += pubnames.pn_length + initial_length_size;
6546
6547 if (pubnames.pn_version != 2 && pubnames.pn_version != 3)
6548 {
6549 static int warned = 0;
6550
6551 if (! warned)
6552 {
6553 warn (_("Only DWARF 2 and 3 pubnames are currently supported\n"));
6554 warned = 1;
6555 }
6556
6557 continue;
6558 }
6559
6560 printf (_(" Length: %ld\n"),
6561 pubnames.pn_length);
6562 printf (_(" Version: %d\n"),
6563 pubnames.pn_version);
6564 printf (_(" Offset into .debug_info section: %ld\n"),
6565 pubnames.pn_offset);
6566 printf (_(" Size of area in .debug_info section: %ld\n"),
6567 pubnames.pn_size);
6568
6569 printf (_("\n Offset\tName\n"));
6570
6571 do
6572 {
6573 offset = byte_get (data, offset_size);
6574
6575 if (offset != 0)
6576 {
6577 data += offset_size;
6578 printf (" %ld\t\t%s\n", offset, data);
6579 data += strlen ((char *) data) + 1;
6580 }
6581 }
6582 while (offset != 0);
6583 }
6584
6585 printf ("\n");
6586 return 1;
6587 }
6588
6589 static char *
6590 get_TAG_name (unsigned long tag)
6591 {
6592 switch (tag)
6593 {
6594 case DW_TAG_padding: return "DW_TAG_padding";
6595 case DW_TAG_array_type: return "DW_TAG_array_type";
6596 case DW_TAG_class_type: return "DW_TAG_class_type";
6597 case DW_TAG_entry_point: return "DW_TAG_entry_point";
6598 case DW_TAG_enumeration_type: return "DW_TAG_enumeration_type";
6599 case DW_TAG_formal_parameter: return "DW_TAG_formal_parameter";
6600 case DW_TAG_imported_declaration: return "DW_TAG_imported_declaration";
6601 case DW_TAG_label: return "DW_TAG_label";
6602 case DW_TAG_lexical_block: return "DW_TAG_lexical_block";
6603 case DW_TAG_member: return "DW_TAG_member";
6604 case DW_TAG_pointer_type: return "DW_TAG_pointer_type";
6605 case DW_TAG_reference_type: return "DW_TAG_reference_type";
6606 case DW_TAG_compile_unit: return "DW_TAG_compile_unit";
6607 case DW_TAG_string_type: return "DW_TAG_string_type";
6608 case DW_TAG_structure_type: return "DW_TAG_structure_type";
6609 case DW_TAG_subroutine_type: return "DW_TAG_subroutine_type";
6610 case DW_TAG_typedef: return "DW_TAG_typedef";
6611 case DW_TAG_union_type: return "DW_TAG_union_type";
6612 case DW_TAG_unspecified_parameters: return "DW_TAG_unspecified_parameters";
6613 case DW_TAG_variant: return "DW_TAG_variant";
6614 case DW_TAG_common_block: return "DW_TAG_common_block";
6615 case DW_TAG_common_inclusion: return "DW_TAG_common_inclusion";
6616 case DW_TAG_inheritance: return "DW_TAG_inheritance";
6617 case DW_TAG_inlined_subroutine: return "DW_TAG_inlined_subroutine";
6618 case DW_TAG_module: return "DW_TAG_module";
6619 case DW_TAG_ptr_to_member_type: return "DW_TAG_ptr_to_member_type";
6620 case DW_TAG_set_type: return "DW_TAG_set_type";
6621 case DW_TAG_subrange_type: return "DW_TAG_subrange_type";
6622 case DW_TAG_with_stmt: return "DW_TAG_with_stmt";
6623 case DW_TAG_access_declaration: return "DW_TAG_access_declaration";
6624 case DW_TAG_base_type: return "DW_TAG_base_type";
6625 case DW_TAG_catch_block: return "DW_TAG_catch_block";
6626 case DW_TAG_const_type: return "DW_TAG_const_type";
6627 case DW_TAG_constant: return "DW_TAG_constant";
6628 case DW_TAG_enumerator: return "DW_TAG_enumerator";
6629 case DW_TAG_file_type: return "DW_TAG_file_type";
6630 case DW_TAG_friend: return "DW_TAG_friend";
6631 case DW_TAG_namelist: return "DW_TAG_namelist";
6632 case DW_TAG_namelist_item: return "DW_TAG_namelist_item";
6633 case DW_TAG_packed_type: return "DW_TAG_packed_type";
6634 case DW_TAG_subprogram: return "DW_TAG_subprogram";
6635 case DW_TAG_template_type_param: return "DW_TAG_template_type_param";
6636 case DW_TAG_template_value_param: return "DW_TAG_template_value_param";
6637 case DW_TAG_thrown_type: return "DW_TAG_thrown_type";
6638 case DW_TAG_try_block: return "DW_TAG_try_block";
6639 case DW_TAG_variant_part: return "DW_TAG_variant_part";
6640 case DW_TAG_variable: return "DW_TAG_variable";
6641 case DW_TAG_volatile_type: return "DW_TAG_volatile_type";
6642 case DW_TAG_MIPS_loop: return "DW_TAG_MIPS_loop";
6643 case DW_TAG_format_label: return "DW_TAG_format_label";
6644 case DW_TAG_function_template: return "DW_TAG_function_template";
6645 case DW_TAG_class_template: return "DW_TAG_class_template";
6646 /* DWARF 2.1 values. */
6647 case DW_TAG_dwarf_procedure: return "DW_TAG_dwarf_procedure";
6648 case DW_TAG_restrict_type: return "DW_TAG_restrict_type";
6649 case DW_TAG_interface_type: return "DW_TAG_interface_type";
6650 case DW_TAG_namespace: return "DW_TAG_namespace";
6651 case DW_TAG_imported_module: return "DW_TAG_imported_module";
6652 case DW_TAG_unspecified_type: return "DW_TAG_unspecified_type";
6653 case DW_TAG_partial_unit: return "DW_TAG_partial_unit";
6654 case DW_TAG_imported_unit: return "DW_TAG_imported_unit";
6655 /* UPC values. */
6656 case DW_TAG_upc_shared_type: return "DW_TAG_upc_shared_type";
6657 case DW_TAG_upc_strict_type: return "DW_TAG_upc_strict_type";
6658 case DW_TAG_upc_relaxed_type: return "DW_TAG_upc_relaxed_type";
6659 default:
6660 {
6661 static char buffer[100];
6662
6663 sprintf (buffer, _("Unknown TAG value: %lx"), tag);
6664 return buffer;
6665 }
6666 }
6667 }
6668
6669 static char *
6670 get_AT_name (unsigned long attribute)
6671 {
6672 switch (attribute)
6673 {
6674 case DW_AT_sibling: return "DW_AT_sibling";
6675 case DW_AT_location: return "DW_AT_location";
6676 case DW_AT_name: return "DW_AT_name";
6677 case DW_AT_ordering: return "DW_AT_ordering";
6678 case DW_AT_subscr_data: return "DW_AT_subscr_data";
6679 case DW_AT_byte_size: return "DW_AT_byte_size";
6680 case DW_AT_bit_offset: return "DW_AT_bit_offset";
6681 case DW_AT_bit_size: return "DW_AT_bit_size";
6682 case DW_AT_element_list: return "DW_AT_element_list";
6683 case DW_AT_stmt_list: return "DW_AT_stmt_list";
6684 case DW_AT_low_pc: return "DW_AT_low_pc";
6685 case DW_AT_high_pc: return "DW_AT_high_pc";
6686 case DW_AT_language: return "DW_AT_language";
6687 case DW_AT_member: return "DW_AT_member";
6688 case DW_AT_discr: return "DW_AT_discr";
6689 case DW_AT_discr_value: return "DW_AT_discr_value";
6690 case DW_AT_visibility: return "DW_AT_visibility";
6691 case DW_AT_import: return "DW_AT_import";
6692 case DW_AT_string_length: return "DW_AT_string_length";
6693 case DW_AT_common_reference: return "DW_AT_common_reference";
6694 case DW_AT_comp_dir: return "DW_AT_comp_dir";
6695 case DW_AT_const_value: return "DW_AT_const_value";
6696 case DW_AT_containing_type: return "DW_AT_containing_type";
6697 case DW_AT_default_value: return "DW_AT_default_value";
6698 case DW_AT_inline: return "DW_AT_inline";
6699 case DW_AT_is_optional: return "DW_AT_is_optional";
6700 case DW_AT_lower_bound: return "DW_AT_lower_bound";
6701 case DW_AT_producer: return "DW_AT_producer";
6702 case DW_AT_prototyped: return "DW_AT_prototyped";
6703 case DW_AT_return_addr: return "DW_AT_return_addr";
6704 case DW_AT_start_scope: return "DW_AT_start_scope";
6705 case DW_AT_stride_size: return "DW_AT_stride_size";
6706 case DW_AT_upper_bound: return "DW_AT_upper_bound";
6707 case DW_AT_abstract_origin: return "DW_AT_abstract_origin";
6708 case DW_AT_accessibility: return "DW_AT_accessibility";
6709 case DW_AT_address_class: return "DW_AT_address_class";
6710 case DW_AT_artificial: return "DW_AT_artificial";
6711 case DW_AT_base_types: return "DW_AT_base_types";
6712 case DW_AT_calling_convention: return "DW_AT_calling_convention";
6713 case DW_AT_count: return "DW_AT_count";
6714 case DW_AT_data_member_location: return "DW_AT_data_member_location";
6715 case DW_AT_decl_column: return "DW_AT_decl_column";
6716 case DW_AT_decl_file: return "DW_AT_decl_file";
6717 case DW_AT_decl_line: return "DW_AT_decl_line";
6718 case DW_AT_declaration: return "DW_AT_declaration";
6719 case DW_AT_discr_list: return "DW_AT_discr_list";
6720 case DW_AT_encoding: return "DW_AT_encoding";
6721 case DW_AT_external: return "DW_AT_external";
6722 case DW_AT_frame_base: return "DW_AT_frame_base";
6723 case DW_AT_friend: return "DW_AT_friend";
6724 case DW_AT_identifier_case: return "DW_AT_identifier_case";
6725 case DW_AT_macro_info: return "DW_AT_macro_info";
6726 case DW_AT_namelist_items: return "DW_AT_namelist_items";
6727 case DW_AT_priority: return "DW_AT_priority";
6728 case DW_AT_segment: return "DW_AT_segment";
6729 case DW_AT_specification: return "DW_AT_specification";
6730 case DW_AT_static_link: return "DW_AT_static_link";
6731 case DW_AT_type: return "DW_AT_type";
6732 case DW_AT_use_location: return "DW_AT_use_location";
6733 case DW_AT_variable_parameter: return "DW_AT_variable_parameter";
6734 case DW_AT_virtuality: return "DW_AT_virtuality";
6735 case DW_AT_vtable_elem_location: return "DW_AT_vtable_elem_location";
6736 /* DWARF 2.1 values. */
6737 case DW_AT_allocated: return "DW_AT_allocated";
6738 case DW_AT_associated: return "DW_AT_associated";
6739 case DW_AT_data_location: return "DW_AT_data_location";
6740 case DW_AT_stride: return "DW_AT_stride";
6741 case DW_AT_entry_pc: return "DW_AT_entry_pc";
6742 case DW_AT_use_UTF8: return "DW_AT_use_UTF8";
6743 case DW_AT_extension: return "DW_AT_extension";
6744 case DW_AT_ranges: return "DW_AT_ranges";
6745 case DW_AT_trampoline: return "DW_AT_trampoline";
6746 case DW_AT_call_column: return "DW_AT_call_column";
6747 case DW_AT_call_file: return "DW_AT_call_file";
6748 case DW_AT_call_line: return "DW_AT_call_line";
6749 /* SGI/MIPS extensions. */
6750 case DW_AT_MIPS_fde: return "DW_AT_MIPS_fde";
6751 case DW_AT_MIPS_loop_begin: return "DW_AT_MIPS_loop_begin";
6752 case DW_AT_MIPS_tail_loop_begin: return "DW_AT_MIPS_tail_loop_begin";
6753 case DW_AT_MIPS_epilog_begin: return "DW_AT_MIPS_epilog_begin";
6754 case DW_AT_MIPS_loop_unroll_factor: return "DW_AT_MIPS_loop_unroll_factor";
6755 case DW_AT_MIPS_software_pipeline_depth:
6756 return "DW_AT_MIPS_software_pipeline_depth";
6757 case DW_AT_MIPS_linkage_name: return "DW_AT_MIPS_linkage_name";
6758 case DW_AT_MIPS_stride: return "DW_AT_MIPS_stride";
6759 case DW_AT_MIPS_abstract_name: return "DW_AT_MIPS_abstract_name";
6760 case DW_AT_MIPS_clone_origin: return "DW_AT_MIPS_clone_origin";
6761 case DW_AT_MIPS_has_inlines: return "DW_AT_MIPS_has_inlines";
6762 /* GNU extensions. */
6763 case DW_AT_sf_names: return "DW_AT_sf_names";
6764 case DW_AT_src_info: return "DW_AT_src_info";
6765 case DW_AT_mac_info: return "DW_AT_mac_info";
6766 case DW_AT_src_coords: return "DW_AT_src_coords";
6767 case DW_AT_body_begin: return "DW_AT_body_begin";
6768 case DW_AT_body_end: return "DW_AT_body_end";
6769 case DW_AT_GNU_vector: return "DW_AT_GNU_vector";
6770 /* UPC extension. */
6771 case DW_AT_upc_threads_scaled: return "DW_AT_upc_threads_scaled";
6772 default:
6773 {
6774 static char buffer[100];
6775
6776 sprintf (buffer, _("Unknown AT value: %lx"), attribute);
6777 return buffer;
6778 }
6779 }
6780 }
6781
6782 static char *
6783 get_FORM_name (unsigned long form)
6784 {
6785 switch (form)
6786 {
6787 case DW_FORM_addr: return "DW_FORM_addr";
6788 case DW_FORM_block2: return "DW_FORM_block2";
6789 case DW_FORM_block4: return "DW_FORM_block4";
6790 case DW_FORM_data2: return "DW_FORM_data2";
6791 case DW_FORM_data4: return "DW_FORM_data4";
6792 case DW_FORM_data8: return "DW_FORM_data8";
6793 case DW_FORM_string: return "DW_FORM_string";
6794 case DW_FORM_block: return "DW_FORM_block";
6795 case DW_FORM_block1: return "DW_FORM_block1";
6796 case DW_FORM_data1: return "DW_FORM_data1";
6797 case DW_FORM_flag: return "DW_FORM_flag";
6798 case DW_FORM_sdata: return "DW_FORM_sdata";
6799 case DW_FORM_strp: return "DW_FORM_strp";
6800 case DW_FORM_udata: return "DW_FORM_udata";
6801 case DW_FORM_ref_addr: return "DW_FORM_ref_addr";
6802 case DW_FORM_ref1: return "DW_FORM_ref1";
6803 case DW_FORM_ref2: return "DW_FORM_ref2";
6804 case DW_FORM_ref4: return "DW_FORM_ref4";
6805 case DW_FORM_ref8: return "DW_FORM_ref8";
6806 case DW_FORM_ref_udata: return "DW_FORM_ref_udata";
6807 case DW_FORM_indirect: return "DW_FORM_indirect";
6808 default:
6809 {
6810 static char buffer[100];
6811
6812 sprintf (buffer, _("Unknown FORM value: %lx"), form);
6813 return buffer;
6814 }
6815 }
6816 }
6817
6818 /* FIXME: There are better and more effiecint ways to handle
6819 these structures. For now though, I just want something that
6820 is simple to implement. */
6821 typedef struct abbrev_attr
6822 {
6823 unsigned long attribute;
6824 unsigned long form;
6825 struct abbrev_attr *next;
6826 }
6827 abbrev_attr;
6828
6829 typedef struct abbrev_entry
6830 {
6831 unsigned long entry;
6832 unsigned long tag;
6833 int children;
6834 struct abbrev_attr *first_attr;
6835 struct abbrev_attr *last_attr;
6836 struct abbrev_entry *next;
6837 }
6838 abbrev_entry;
6839
6840 static abbrev_entry *first_abbrev = NULL;
6841 static abbrev_entry *last_abbrev = NULL;
6842
6843 static void
6844 free_abbrevs (void)
6845 {
6846 abbrev_entry *abbrev;
6847
6848 for (abbrev = first_abbrev; abbrev;)
6849 {
6850 abbrev_entry *next = abbrev->next;
6851 abbrev_attr *attr;
6852
6853 for (attr = abbrev->first_attr; attr;)
6854 {
6855 abbrev_attr *next = attr->next;
6856
6857 free (attr);
6858 attr = next;
6859 }
6860
6861 free (abbrev);
6862 abbrev = next;
6863 }
6864
6865 last_abbrev = first_abbrev = NULL;
6866 }
6867
6868 static void
6869 add_abbrev (unsigned long number, unsigned long tag, int children)
6870 {
6871 abbrev_entry *entry;
6872
6873 entry = malloc (sizeof (*entry));
6874
6875 if (entry == NULL)
6876 /* ugg */
6877 return;
6878
6879 entry->entry = number;
6880 entry->tag = tag;
6881 entry->children = children;
6882 entry->first_attr = NULL;
6883 entry->last_attr = NULL;
6884 entry->next = NULL;
6885
6886 if (first_abbrev == NULL)
6887 first_abbrev = entry;
6888 else
6889 last_abbrev->next = entry;
6890
6891 last_abbrev = entry;
6892 }
6893
6894 static void
6895 add_abbrev_attr (unsigned long attribute, unsigned long form)
6896 {
6897 abbrev_attr *attr;
6898
6899 attr = malloc (sizeof (*attr));
6900
6901 if (attr == NULL)
6902 /* ugg */
6903 return;
6904
6905 attr->attribute = attribute;
6906 attr->form = form;
6907 attr->next = NULL;
6908
6909 if (last_abbrev->first_attr == NULL)
6910 last_abbrev->first_attr = attr;
6911 else
6912 last_abbrev->last_attr->next = attr;
6913
6914 last_abbrev->last_attr = attr;
6915 }
6916
6917 /* Processes the (partial) contents of a .debug_abbrev section.
6918 Returns NULL if the end of the section was encountered.
6919 Returns the address after the last byte read if the end of
6920 an abbreviation set was found. */
6921
6922 static unsigned char *
6923 process_abbrev_section (unsigned char *start, unsigned char *end)
6924 {
6925 if (first_abbrev != NULL)
6926 return NULL;
6927
6928 while (start < end)
6929 {
6930 int bytes_read;
6931 unsigned long entry;
6932 unsigned long tag;
6933 unsigned long attribute;
6934 int children;
6935
6936 entry = read_leb128 (start, & bytes_read, 0);
6937 start += bytes_read;
6938
6939 /* A single zero is supposed to end the section according
6940 to the standard. If there's more, then signal that to
6941 the caller. */
6942 if (entry == 0)
6943 return start == end ? NULL : start;
6944
6945 tag = read_leb128 (start, & bytes_read, 0);
6946 start += bytes_read;
6947
6948 children = *start++;
6949
6950 add_abbrev (entry, tag, children);
6951
6952 do
6953 {
6954 unsigned long form;
6955
6956 attribute = read_leb128 (start, & bytes_read, 0);
6957 start += bytes_read;
6958
6959 form = read_leb128 (start, & bytes_read, 0);
6960 start += bytes_read;
6961
6962 if (attribute != 0)
6963 add_abbrev_attr (attribute, form);
6964 }
6965 while (attribute != 0);
6966 }
6967
6968 return NULL;
6969 }
6970
6971
6972 static int
6973 display_debug_macinfo (Elf_Internal_Shdr *section,
6974 unsigned char *start,
6975 FILE *file ATTRIBUTE_UNUSED)
6976 {
6977 unsigned char *end = start + section->sh_size;
6978 unsigned char *curr = start;
6979 unsigned int bytes_read;
6980 enum dwarf_macinfo_record_type op;
6981
6982 printf (_("Contents of the %s section:\n\n"), SECTION_NAME (section));
6983
6984 while (curr < end)
6985 {
6986 unsigned int lineno;
6987 const char *string;
6988
6989 op = *curr;
6990 curr++;
6991
6992 switch (op)
6993 {
6994 case DW_MACINFO_start_file:
6995 {
6996 unsigned int filenum;
6997
6998 lineno = read_leb128 (curr, & bytes_read, 0);
6999 curr += bytes_read;
7000 filenum = read_leb128 (curr, & bytes_read, 0);
7001 curr += bytes_read;
7002
7003 printf (_(" DW_MACINFO_start_file - lineno: %d filenum: %d\n"), lineno, filenum);
7004 }
7005 break;
7006
7007 case DW_MACINFO_end_file:
7008 printf (_(" DW_MACINFO_end_file\n"));
7009 break;
7010
7011 case DW_MACINFO_define:
7012 lineno = read_leb128 (curr, & bytes_read, 0);
7013 curr += bytes_read;
7014 string = curr;
7015 curr += strlen (string) + 1;
7016 printf (_(" DW_MACINFO_define - lineno : %d macro : %s\n"), lineno, string);
7017 break;
7018
7019 case DW_MACINFO_undef:
7020 lineno = read_leb128 (curr, & bytes_read, 0);
7021 curr += bytes_read;
7022 string = curr;
7023 curr += strlen (string) + 1;
7024 printf (_(" DW_MACINFO_undef - lineno : %d macro : %s\n"), lineno, string);
7025 break;
7026
7027 case DW_MACINFO_vendor_ext:
7028 {
7029 unsigned int constant;
7030
7031 constant = read_leb128 (curr, & bytes_read, 0);
7032 curr += bytes_read;
7033 string = curr;
7034 curr += strlen (string) + 1;
7035 printf (_(" DW_MACINFO_vendor_ext - constant : %d string : %s\n"), constant, string);
7036 }
7037 break;
7038 }
7039 }
7040
7041 return 1;
7042 }
7043
7044
7045 static int
7046 display_debug_abbrev (Elf_Internal_Shdr *section,
7047 unsigned char *start,
7048 FILE *file ATTRIBUTE_UNUSED)
7049 {
7050 abbrev_entry *entry;
7051 unsigned char *end = start + section->sh_size;
7052
7053 printf (_("Contents of the %s section:\n\n"), SECTION_NAME (section));
7054
7055 do
7056 {
7057 start = process_abbrev_section (start, end);
7058
7059 if (first_abbrev == NULL)
7060 continue;
7061
7062 printf (_(" Number TAG\n"));
7063
7064 for (entry = first_abbrev; entry; entry = entry->next)
7065 {
7066 abbrev_attr *attr;
7067
7068 printf (_(" %ld %s [%s]\n"),
7069 entry->entry,
7070 get_TAG_name (entry->tag),
7071 entry->children ? _("has children") : _("no children"));
7072
7073 for (attr = entry->first_attr; attr; attr = attr->next)
7074 {
7075 printf (_(" %-18s %s\n"),
7076 get_AT_name (attr->attribute),
7077 get_FORM_name (attr->form));
7078 }
7079 }
7080
7081 free_abbrevs ();
7082 }
7083 while (start);
7084
7085 printf ("\n");
7086
7087 return 1;
7088 }
7089
7090
7091 static unsigned char *
7092 display_block (unsigned char *data, unsigned long length)
7093 {
7094 printf (_(" %lu byte block: "), length);
7095
7096 while (length --)
7097 printf ("%lx ", (unsigned long) byte_get (data++, 1));
7098
7099 return data;
7100 }
7101
7102 static void
7103 decode_location_expression (unsigned char * data,
7104 unsigned int pointer_size,
7105 unsigned long length)
7106 {
7107 unsigned op;
7108 int bytes_read;
7109 unsigned long uvalue;
7110 unsigned char *end = data + length;
7111
7112 while (data < end)
7113 {
7114 op = *data++;
7115
7116 switch (op)
7117 {
7118 case DW_OP_addr:
7119 printf ("DW_OP_addr: %lx",
7120 (unsigned long) byte_get (data, pointer_size));
7121 data += pointer_size;
7122 break;
7123 case DW_OP_deref:
7124 printf ("DW_OP_deref");
7125 break;
7126 case DW_OP_const1u:
7127 printf ("DW_OP_const1u: %lu", (unsigned long) byte_get (data++, 1));
7128 break;
7129 case DW_OP_const1s:
7130 printf ("DW_OP_const1s: %ld", (long) byte_get (data++, 1));
7131 break;
7132 case DW_OP_const2u:
7133 printf ("DW_OP_const2u: %lu", (unsigned long) byte_get (data, 2));
7134 data += 2;
7135 break;
7136 case DW_OP_const2s:
7137 printf ("DW_OP_const2s: %ld", (long) byte_get (data, 2));
7138 data += 2;
7139 break;
7140 case DW_OP_const4u:
7141 printf ("DW_OP_const4u: %lu", (unsigned long) byte_get (data, 4));
7142 data += 4;
7143 break;
7144 case DW_OP_const4s:
7145 printf ("DW_OP_const4s: %ld", (long) byte_get (data, 4));
7146 data += 4;
7147 break;
7148 case DW_OP_const8u:
7149 printf ("DW_OP_const8u: %lu %lu", (unsigned long) byte_get (data, 4),
7150 (unsigned long) byte_get (data + 4, 4));
7151 data += 8;
7152 break;
7153 case DW_OP_const8s:
7154 printf ("DW_OP_const8s: %ld %ld", (long) byte_get (data, 4),
7155 (long) byte_get (data + 4, 4));
7156 data += 8;
7157 break;
7158 case DW_OP_constu:
7159 printf ("DW_OP_constu: %lu", read_leb128 (data, &bytes_read, 0));
7160 data += bytes_read;
7161 break;
7162 case DW_OP_consts:
7163 printf ("DW_OP_consts: %ld", read_leb128 (data, &bytes_read, 1));
7164 data += bytes_read;
7165 break;
7166 case DW_OP_dup:
7167 printf ("DW_OP_dup");
7168 break;
7169 case DW_OP_drop:
7170 printf ("DW_OP_drop");
7171 break;
7172 case DW_OP_over:
7173 printf ("DW_OP_over");
7174 break;
7175 case DW_OP_pick:
7176 printf ("DW_OP_pick: %ld", (unsigned long) byte_get (data++, 1));
7177 break;
7178 case DW_OP_swap:
7179 printf ("DW_OP_swap");
7180 break;
7181 case DW_OP_rot:
7182 printf ("DW_OP_rot");
7183 break;
7184 case DW_OP_xderef:
7185 printf ("DW_OP_xderef");
7186 break;
7187 case DW_OP_abs:
7188 printf ("DW_OP_abs");
7189 break;
7190 case DW_OP_and:
7191 printf ("DW_OP_and");
7192 break;
7193 case DW_OP_div:
7194 printf ("DW_OP_div");
7195 break;
7196 case DW_OP_minus:
7197 printf ("DW_OP_minus");
7198 break;
7199 case DW_OP_mod:
7200 printf ("DW_OP_mod");
7201 break;
7202 case DW_OP_mul:
7203 printf ("DW_OP_mul");
7204 break;
7205 case DW_OP_neg:
7206 printf ("DW_OP_neg");
7207 break;
7208 case DW_OP_not:
7209 printf ("DW_OP_not");
7210 break;
7211 case DW_OP_or:
7212 printf ("DW_OP_or");
7213 break;
7214 case DW_OP_plus:
7215 printf ("DW_OP_plus");
7216 break;
7217 case DW_OP_plus_uconst:
7218 printf ("DW_OP_plus_uconst: %lu",
7219 read_leb128 (data, &bytes_read, 0));
7220 data += bytes_read;
7221 break;
7222 case DW_OP_shl:
7223 printf ("DW_OP_shl");
7224 break;
7225 case DW_OP_shr:
7226 printf ("DW_OP_shr");
7227 break;
7228 case DW_OP_shra:
7229 printf ("DW_OP_shra");
7230 break;
7231 case DW_OP_xor:
7232 printf ("DW_OP_xor");
7233 break;
7234 case DW_OP_bra:
7235 printf ("DW_OP_bra: %ld", (long) byte_get (data, 2));
7236 data += 2;
7237 break;
7238 case DW_OP_eq:
7239 printf ("DW_OP_eq");
7240 break;
7241 case DW_OP_ge:
7242 printf ("DW_OP_ge");
7243 break;
7244 case DW_OP_gt:
7245 printf ("DW_OP_gt");
7246 break;
7247 case DW_OP_le:
7248 printf ("DW_OP_le");
7249 break;
7250 case DW_OP_lt:
7251 printf ("DW_OP_lt");
7252 break;
7253 case DW_OP_ne:
7254 printf ("DW_OP_ne");
7255 break;
7256 case DW_OP_skip:
7257 printf ("DW_OP_skip: %ld", (long) byte_get (data, 2));
7258 data += 2;
7259 break;
7260
7261 case DW_OP_lit0:
7262 case DW_OP_lit1:
7263 case DW_OP_lit2:
7264 case DW_OP_lit3:
7265 case DW_OP_lit4:
7266 case DW_OP_lit5:
7267 case DW_OP_lit6:
7268 case DW_OP_lit7:
7269 case DW_OP_lit8:
7270 case DW_OP_lit9:
7271 case DW_OP_lit10:
7272 case DW_OP_lit11:
7273 case DW_OP_lit12:
7274 case DW_OP_lit13:
7275 case DW_OP_lit14:
7276 case DW_OP_lit15:
7277 case DW_OP_lit16:
7278 case DW_OP_lit17:
7279 case DW_OP_lit18:
7280 case DW_OP_lit19:
7281 case DW_OP_lit20:
7282 case DW_OP_lit21:
7283 case DW_OP_lit22:
7284 case DW_OP_lit23:
7285 case DW_OP_lit24:
7286 case DW_OP_lit25:
7287 case DW_OP_lit26:
7288 case DW_OP_lit27:
7289 case DW_OP_lit28:
7290 case DW_OP_lit29:
7291 case DW_OP_lit30:
7292 case DW_OP_lit31:
7293 printf ("DW_OP_lit%d", op - DW_OP_lit0);
7294 break;
7295
7296 case DW_OP_reg0:
7297 case DW_OP_reg1:
7298 case DW_OP_reg2:
7299 case DW_OP_reg3:
7300 case DW_OP_reg4:
7301 case DW_OP_reg5:
7302 case DW_OP_reg6:
7303 case DW_OP_reg7:
7304 case DW_OP_reg8:
7305 case DW_OP_reg9:
7306 case DW_OP_reg10:
7307 case DW_OP_reg11:
7308 case DW_OP_reg12:
7309 case DW_OP_reg13:
7310 case DW_OP_reg14:
7311 case DW_OP_reg15:
7312 case DW_OP_reg16:
7313 case DW_OP_reg17:
7314 case DW_OP_reg18:
7315 case DW_OP_reg19:
7316 case DW_OP_reg20:
7317 case DW_OP_reg21:
7318 case DW_OP_reg22:
7319 case DW_OP_reg23:
7320 case DW_OP_reg24:
7321 case DW_OP_reg25:
7322 case DW_OP_reg26:
7323 case DW_OP_reg27:
7324 case DW_OP_reg28:
7325 case DW_OP_reg29:
7326 case DW_OP_reg30:
7327 case DW_OP_reg31:
7328 printf ("DW_OP_reg%d", op - DW_OP_reg0);
7329 break;
7330
7331 case DW_OP_breg0:
7332 case DW_OP_breg1:
7333 case DW_OP_breg2:
7334 case DW_OP_breg3:
7335 case DW_OP_breg4:
7336 case DW_OP_breg5:
7337 case DW_OP_breg6:
7338 case DW_OP_breg7:
7339 case DW_OP_breg8:
7340 case DW_OP_breg9:
7341 case DW_OP_breg10:
7342 case DW_OP_breg11:
7343 case DW_OP_breg12:
7344 case DW_OP_breg13:
7345 case DW_OP_breg14:
7346 case DW_OP_breg15:
7347 case DW_OP_breg16:
7348 case DW_OP_breg17:
7349 case DW_OP_breg18:
7350 case DW_OP_breg19:
7351 case DW_OP_breg20:
7352 case DW_OP_breg21:
7353 case DW_OP_breg22:
7354 case DW_OP_breg23:
7355 case DW_OP_breg24:
7356 case DW_OP_breg25:
7357 case DW_OP_breg26:
7358 case DW_OP_breg27:
7359 case DW_OP_breg28:
7360 case DW_OP_breg29:
7361 case DW_OP_breg30:
7362 case DW_OP_breg31:
7363 printf ("DW_OP_breg%d: %ld", op - DW_OP_breg0,
7364 read_leb128 (data, &bytes_read, 1));
7365 data += bytes_read;
7366 break;
7367
7368 case DW_OP_regx:
7369 printf ("DW_OP_regx: %lu", read_leb128 (data, &bytes_read, 0));
7370 data += bytes_read;
7371 break;
7372 case DW_OP_fbreg:
7373 printf ("DW_OP_fbreg: %ld", read_leb128 (data, &bytes_read, 1));
7374 data += bytes_read;
7375 break;
7376 case DW_OP_bregx:
7377 uvalue = read_leb128 (data, &bytes_read, 0);
7378 data += bytes_read;
7379 printf ("DW_OP_bregx: %lu %ld", uvalue,
7380 read_leb128 (data, &bytes_read, 1));
7381 data += bytes_read;
7382 break;
7383 case DW_OP_piece:
7384 printf ("DW_OP_piece: %lu", read_leb128 (data, &bytes_read, 0));
7385 data += bytes_read;
7386 break;
7387 case DW_OP_deref_size:
7388 printf ("DW_OP_deref_size: %ld", (long) byte_get (data++, 1));
7389 break;
7390 case DW_OP_xderef_size:
7391 printf ("DW_OP_xderef_size: %ld", (long) byte_get (data++, 1));
7392 break;
7393 case DW_OP_nop:
7394 printf ("DW_OP_nop");
7395 break;
7396
7397 /* DWARF 3 extensions. */
7398 case DW_OP_push_object_address:
7399 printf ("DW_OP_push_object_address");
7400 break;
7401 case DW_OP_call2:
7402 printf ("DW_OP_call2: <%lx>", (long) byte_get (data, 2));
7403 data += 2;
7404 break;
7405 case DW_OP_call4:
7406 printf ("DW_OP_call4: <%lx>", (long) byte_get (data, 4));
7407 data += 4;
7408 break;
7409 case DW_OP_call_ref:
7410 printf ("DW_OP_call_ref");
7411 break;
7412
7413 /* GNU extensions. */
7414 case DW_OP_GNU_push_tls_address:
7415 printf ("DW_OP_GNU_push_tls_address");
7416 break;
7417
7418 default:
7419 if (op >= DW_OP_lo_user
7420 && op <= DW_OP_hi_user)
7421 printf (_("(User defined location op)"));
7422 else
7423 printf (_("(Unknown location op)"));
7424 /* No way to tell where the next op is, so just bail. */
7425 return;
7426 }
7427
7428 /* Separate the ops. */
7429 if (data < end)
7430 printf ("; ");
7431 }
7432 }
7433
7434 static const char *debug_loc_contents;
7435 static bfd_vma debug_loc_size;
7436
7437 static void
7438 load_debug_loc (FILE *file)
7439 {
7440 Elf_Internal_Shdr *sec;
7441 unsigned int i;
7442
7443 /* If it is already loaded, do nothing. */
7444 if (debug_loc_contents != NULL)
7445 return;
7446
7447 /* Locate the .debug_loc section. */
7448 for (i = 0, sec = section_headers;
7449 i < elf_header.e_shnum;
7450 i++, sec++)
7451 if (strcmp (SECTION_NAME (sec), ".debug_loc") == 0)
7452 break;
7453
7454 if (i == elf_header.e_shnum || sec->sh_size == 0)
7455 return;
7456
7457 debug_loc_size = sec->sh_size;
7458
7459 debug_loc_contents = get_data (NULL, file, sec->sh_offset, sec->sh_size,
7460 _("debug_loc section data"));
7461 }
7462
7463 static void
7464 free_debug_loc (void)
7465 {
7466 if (debug_loc_contents == NULL)
7467 return;
7468
7469 free ((char *) debug_loc_contents);
7470 debug_loc_contents = NULL;
7471 debug_loc_size = 0;
7472 }
7473
7474
7475 static int
7476 display_debug_loc (Elf_Internal_Shdr *section,
7477 unsigned char *start,
7478 FILE *file ATTRIBUTE_UNUSED)
7479 {
7480 unsigned char *section_end;
7481 unsigned long bytes;
7482 unsigned char *section_begin = start;
7483 bfd_vma addr;
7484
7485 addr = section->sh_addr;
7486 bytes = section->sh_size;
7487 section_end = start + bytes;
7488
7489 if (bytes == 0)
7490 {
7491 printf (_("\nThe .debug_loc section is empty.\n"));
7492 return 0;
7493 }
7494
7495 printf (_("Contents of the .debug_loc section:\n\n"));
7496 printf (_("\n Offset Begin End Expression\n"));
7497
7498 while (start < section_end)
7499 {
7500 unsigned long begin;
7501 unsigned long end;
7502 unsigned short length;
7503 unsigned long offset;
7504
7505 offset = start - section_begin;
7506
7507 while (1)
7508 {
7509 /* Normally, the lists in the debug_loc section are related to a
7510 given compilation unit, and thus, we would use the pointer size
7511 of that compilation unit. However, since we are displaying it
7512 seperately here, we either have to store pointer sizes of all
7513 compilation units, or assume they don't change. We assume,
7514 like the debug_line display, that it doesn't change. */
7515 begin = byte_get (start, debug_line_pointer_size);
7516 start += debug_line_pointer_size;
7517 end = byte_get (start, debug_line_pointer_size);
7518 start += debug_line_pointer_size;
7519
7520 if (begin == 0 && end == 0)
7521 break;
7522
7523 /* For now, skip any base address specifiers. */
7524 if (begin == 0xffffffff)
7525 continue;
7526
7527 begin += addr;
7528 end += addr;
7529
7530 length = byte_get (start, 2);
7531 start += 2;
7532
7533 printf (" %8.8lx %8.8lx %8.8lx (", offset, begin, end);
7534 decode_location_expression (start, debug_line_pointer_size, length);
7535 printf (")\n");
7536
7537 start += length;
7538 }
7539 printf ("\n");
7540 }
7541 return 1;
7542 }
7543
7544 static const char *debug_str_contents;
7545 static bfd_vma debug_str_size;
7546
7547 static void
7548 load_debug_str (FILE *file)
7549 {
7550 Elf_Internal_Shdr *sec;
7551 unsigned int i;
7552
7553 /* If it is already loaded, do nothing. */
7554 if (debug_str_contents != NULL)
7555 return;
7556
7557 /* Locate the .debug_str section. */
7558 for (i = 0, sec = section_headers;
7559 i < elf_header.e_shnum;
7560 i++, sec++)
7561 if (strcmp (SECTION_NAME (sec), ".debug_str") == 0)
7562 break;
7563
7564 if (i == elf_header.e_shnum || sec->sh_size == 0)
7565 return;
7566
7567 debug_str_size = sec->sh_size;
7568
7569 debug_str_contents = get_data (NULL, file, sec->sh_offset, sec->sh_size,
7570 _("debug_str section data"));
7571 }
7572
7573 static void
7574 free_debug_str (void)
7575 {
7576 if (debug_str_contents == NULL)
7577 return;
7578
7579 free ((char *) debug_str_contents);
7580 debug_str_contents = NULL;
7581 debug_str_size = 0;
7582 }
7583
7584 static const char *
7585 fetch_indirect_string (unsigned long offset)
7586 {
7587 if (debug_str_contents == NULL)
7588 return _("<no .debug_str section>");
7589
7590 if (offset > debug_str_size)
7591 return _("<offset is too big>");
7592
7593 return debug_str_contents + offset;
7594 }
7595
7596 static int
7597 display_debug_str (Elf_Internal_Shdr *section,
7598 unsigned char *start,
7599 FILE *file ATTRIBUTE_UNUSED)
7600 {
7601 unsigned long bytes;
7602 bfd_vma addr;
7603
7604 addr = section->sh_addr;
7605 bytes = section->sh_size;
7606
7607 if (bytes == 0)
7608 {
7609 printf (_("\nThe .debug_str section is empty.\n"));
7610 return 0;
7611 }
7612
7613 printf (_("Contents of the .debug_str section:\n\n"));
7614
7615 while (bytes)
7616 {
7617 int j;
7618 int k;
7619 int lbytes;
7620
7621 lbytes = (bytes > 16 ? 16 : bytes);
7622
7623 printf (" 0x%8.8lx ", (unsigned long) addr);
7624
7625 for (j = 0; j < 16; j++)
7626 {
7627 if (j < lbytes)
7628 printf ("%2.2x", start[j]);
7629 else
7630 printf (" ");
7631
7632 if ((j & 3) == 3)
7633 printf (" ");
7634 }
7635
7636 for (j = 0; j < lbytes; j++)
7637 {
7638 k = start[j];
7639 if (k >= ' ' && k < 0x80)
7640 printf ("%c", k);
7641 else
7642 printf (".");
7643 }
7644
7645 putchar ('\n');
7646
7647 start += lbytes;
7648 addr += lbytes;
7649 bytes -= lbytes;
7650 }
7651
7652 return 1;
7653 }
7654
7655 static unsigned char *
7656 read_and_display_attr_value (unsigned long attribute,
7657 unsigned long form,
7658 unsigned char *data,
7659 unsigned long cu_offset,
7660 unsigned long pointer_size,
7661 unsigned long offset_size,
7662 int dwarf_version)
7663 {
7664 unsigned long uvalue = 0;
7665 unsigned char *block_start = NULL;
7666 int bytes_read;
7667
7668 switch (form)
7669 {
7670 default:
7671 break;
7672
7673 case DW_FORM_ref_addr:
7674 if (dwarf_version == 2)
7675 {
7676 uvalue = byte_get (data, pointer_size);
7677 data += pointer_size;
7678 }
7679 else if (dwarf_version == 3)
7680 {
7681 uvalue = byte_get (data, offset_size);
7682 data += offset_size;
7683 }
7684 else
7685 {
7686 error (_("Internal error: DWARF version is not 2 or 3.\n"));
7687 }
7688 break;
7689
7690 case DW_FORM_addr:
7691 uvalue = byte_get (data, pointer_size);
7692 data += pointer_size;
7693 break;
7694
7695 case DW_FORM_strp:
7696 uvalue = byte_get (data, offset_size);
7697 data += offset_size;
7698 break;
7699
7700 case DW_FORM_ref1:
7701 case DW_FORM_flag:
7702 case DW_FORM_data1:
7703 uvalue = byte_get (data++, 1);
7704 break;
7705
7706 case DW_FORM_ref2:
7707 case DW_FORM_data2:
7708 uvalue = byte_get (data, 2);
7709 data += 2;
7710 break;
7711
7712 case DW_FORM_ref4:
7713 case DW_FORM_data4:
7714 uvalue = byte_get (data, 4);
7715 data += 4;
7716 break;
7717
7718 case DW_FORM_sdata:
7719 uvalue = read_leb128 (data, & bytes_read, 1);
7720 data += bytes_read;
7721 break;
7722
7723 case DW_FORM_ref_udata:
7724 case DW_FORM_udata:
7725 uvalue = read_leb128 (data, & bytes_read, 0);
7726 data += bytes_read;
7727 break;
7728
7729 case DW_FORM_indirect:
7730 form = read_leb128 (data, & bytes_read, 0);
7731 data += bytes_read;
7732 printf (" %s", get_FORM_name (form));
7733 return read_and_display_attr_value (attribute, form, data, cu_offset,
7734 pointer_size, offset_size,
7735 dwarf_version);
7736 }
7737
7738 switch (form)
7739 {
7740 case DW_FORM_ref_addr:
7741 printf (" <#%lx>", uvalue);
7742 break;
7743
7744 case DW_FORM_ref1:
7745 case DW_FORM_ref2:
7746 case DW_FORM_ref4:
7747 case DW_FORM_ref_udata:
7748 printf (" <%lx>", uvalue + cu_offset);
7749 break;
7750
7751 case DW_FORM_addr:
7752 printf (" %#lx", uvalue);
7753
7754 case DW_FORM_flag:
7755 case DW_FORM_data1:
7756 case DW_FORM_data2:
7757 case DW_FORM_data4:
7758 case DW_FORM_sdata:
7759 case DW_FORM_udata:
7760 printf (" %ld", uvalue);
7761 break;
7762
7763 case DW_FORM_ref8:
7764 case DW_FORM_data8:
7765 uvalue = byte_get (data, 4);
7766 printf (" %lx", uvalue);
7767 printf (" %lx", (unsigned long) byte_get (data + 4, 4));
7768 data += 8;
7769 break;
7770
7771 case DW_FORM_string:
7772 printf (" %s", data);
7773 data += strlen ((char *) data) + 1;
7774 break;
7775
7776 case DW_FORM_block:
7777 uvalue = read_leb128 (data, & bytes_read, 0);
7778 block_start = data + bytes_read;
7779 data = display_block (block_start, uvalue);
7780 break;
7781
7782 case DW_FORM_block1:
7783 uvalue = byte_get (data, 1);
7784 block_start = data + 1;
7785 data = display_block (block_start, uvalue);
7786 break;
7787
7788 case DW_FORM_block2:
7789 uvalue = byte_get (data, 2);
7790 block_start = data + 2;
7791 data = display_block (block_start, uvalue);
7792 break;
7793
7794 case DW_FORM_block4:
7795 uvalue = byte_get (data, 4);
7796 block_start = data + 4;
7797 data = display_block (block_start, uvalue);
7798 break;
7799
7800 case DW_FORM_strp:
7801 printf (_(" (indirect string, offset: 0x%lx): %s"),
7802 uvalue, fetch_indirect_string (uvalue));
7803 break;
7804
7805 case DW_FORM_indirect:
7806 /* Handled above. */
7807 break;
7808
7809 default:
7810 warn (_("Unrecognized form: %d\n"), form);
7811 break;
7812 }
7813
7814 /* For some attributes we can display futher information. */
7815
7816 printf ("\t");
7817
7818 switch (attribute)
7819 {
7820 case DW_AT_inline:
7821 switch (uvalue)
7822 {
7823 case DW_INL_not_inlined:
7824 printf (_("(not inlined)"));
7825 break;
7826 case DW_INL_inlined:
7827 printf (_("(inlined)"));
7828 break;
7829 case DW_INL_declared_not_inlined:
7830 printf (_("(declared as inline but ignored)"));
7831 break;
7832 case DW_INL_declared_inlined:
7833 printf (_("(declared as inline and inlined)"));
7834 break;
7835 default:
7836 printf (_(" (Unknown inline attribute value: %lx)"), uvalue);
7837 break;
7838 }
7839 break;
7840
7841 case DW_AT_language:
7842 switch (uvalue)
7843 {
7844 case DW_LANG_C: printf ("(non-ANSI C)"); break;
7845 case DW_LANG_C89: printf ("(ANSI C)"); break;
7846 case DW_LANG_C_plus_plus: printf ("(C++)"); break;
7847 case DW_LANG_Fortran77: printf ("(FORTRAN 77)"); break;
7848 case DW_LANG_Fortran90: printf ("(Fortran 90)"); break;
7849 case DW_LANG_Modula2: printf ("(Modula 2)"); break;
7850 case DW_LANG_Pascal83: printf ("(ANSI Pascal)"); break;
7851 case DW_LANG_Ada83: printf ("(Ada)"); break;
7852 case DW_LANG_Cobol74: printf ("(Cobol 74)"); break;
7853 case DW_LANG_Cobol85: printf ("(Cobol 85)"); break;
7854 /* DWARF 2.1 values. */
7855 case DW_LANG_C99: printf ("(ANSI C99)"); break;
7856 case DW_LANG_Ada95: printf ("(ADA 95)"); break;
7857 case DW_LANG_Fortran95: printf ("(Fortran 95)"); break;
7858 /* MIPS extension. */
7859 case DW_LANG_Mips_Assembler: printf ("(MIPS assembler)"); break;
7860 /* UPC extension. */
7861 case DW_LANG_Upc: printf ("(Unified Parallel C)"); break;
7862 default:
7863 printf ("(Unknown: %lx)", uvalue);
7864 break;
7865 }
7866 break;
7867
7868 case DW_AT_encoding:
7869 switch (uvalue)
7870 {
7871 case DW_ATE_void: printf ("(void)"); break;
7872 case DW_ATE_address: printf ("(machine address)"); break;
7873 case DW_ATE_boolean: printf ("(boolean)"); break;
7874 case DW_ATE_complex_float: printf ("(complex float)"); break;
7875 case DW_ATE_float: printf ("(float)"); break;
7876 case DW_ATE_signed: printf ("(signed)"); break;
7877 case DW_ATE_signed_char: printf ("(signed char)"); break;
7878 case DW_ATE_unsigned: printf ("(unsigned)"); break;
7879 case DW_ATE_unsigned_char: printf ("(unsigned char)"); break;
7880 /* DWARF 2.1 value. */
7881 case DW_ATE_imaginary_float: printf ("(imaginary float)"); break;
7882 default:
7883 if (uvalue >= DW_ATE_lo_user
7884 && uvalue <= DW_ATE_hi_user)
7885 printf ("(user defined type)");
7886 else
7887 printf ("(unknown type)");
7888 break;
7889 }
7890 break;
7891
7892 case DW_AT_accessibility:
7893 switch (uvalue)
7894 {
7895 case DW_ACCESS_public: printf ("(public)"); break;
7896 case DW_ACCESS_protected: printf ("(protected)"); break;
7897 case DW_ACCESS_private: printf ("(private)"); break;
7898 default:
7899 printf ("(unknown accessibility)");
7900 break;
7901 }
7902 break;
7903
7904 case DW_AT_visibility:
7905 switch (uvalue)
7906 {
7907 case DW_VIS_local: printf ("(local)"); break;
7908 case DW_VIS_exported: printf ("(exported)"); break;
7909 case DW_VIS_qualified: printf ("(qualified)"); break;
7910 default: printf ("(unknown visibility)"); break;
7911 }
7912 break;
7913
7914 case DW_AT_virtuality:
7915 switch (uvalue)
7916 {
7917 case DW_VIRTUALITY_none: printf ("(none)"); break;
7918 case DW_VIRTUALITY_virtual: printf ("(virtual)"); break;
7919 case DW_VIRTUALITY_pure_virtual:printf ("(pure_virtual)"); break;
7920 default: printf ("(unknown virtuality)"); break;
7921 }
7922 break;
7923
7924 case DW_AT_identifier_case:
7925 switch (uvalue)
7926 {
7927 case DW_ID_case_sensitive: printf ("(case_sensitive)"); break;
7928 case DW_ID_up_case: printf ("(up_case)"); break;
7929 case DW_ID_down_case: printf ("(down_case)"); break;
7930 case DW_ID_case_insensitive: printf ("(case_insensitive)"); break;
7931 default: printf ("(unknown case)"); break;
7932 }
7933 break;
7934
7935 case DW_AT_calling_convention:
7936 switch (uvalue)
7937 {
7938 case DW_CC_normal: printf ("(normal)"); break;
7939 case DW_CC_program: printf ("(program)"); break;
7940 case DW_CC_nocall: printf ("(nocall)"); break;
7941 default:
7942 if (uvalue >= DW_CC_lo_user
7943 && uvalue <= DW_CC_hi_user)
7944 printf ("(user defined)");
7945 else
7946 printf ("(unknown convention)");
7947 }
7948 break;
7949
7950 case DW_AT_ordering:
7951 switch (uvalue)
7952 {
7953 case -1: printf ("(undefined)"); break;
7954 case 0: printf ("(row major)"); break;
7955 case 1: printf ("(column major)"); break;
7956 }
7957 break;
7958
7959 case DW_AT_frame_base:
7960 case DW_AT_location:
7961 case DW_AT_data_member_location:
7962 case DW_AT_vtable_elem_location:
7963 case DW_AT_allocated:
7964 case DW_AT_associated:
7965 case DW_AT_data_location:
7966 case DW_AT_stride:
7967 case DW_AT_upper_bound:
7968 case DW_AT_lower_bound:
7969 if (block_start)
7970 {
7971 printf ("(");
7972 decode_location_expression (block_start, pointer_size, uvalue);
7973 printf (")");
7974 }
7975 else if (form == DW_FORM_data4 || form == DW_FORM_data8)
7976 {
7977 printf ("(");
7978 printf ("location list");
7979 printf (")");
7980 }
7981 break;
7982
7983 default:
7984 break;
7985 }
7986
7987 return data;
7988 }
7989
7990 static unsigned char *
7991 read_and_display_attr (unsigned long attribute,
7992 unsigned long form,
7993 unsigned char *data,
7994 unsigned long cu_offset,
7995 unsigned long pointer_size,
7996 unsigned long offset_size,
7997 int dwarf_version)
7998 {
7999 printf (" %-18s:", get_AT_name (attribute));
8000 data = read_and_display_attr_value (attribute, form, data, cu_offset,
8001 pointer_size, offset_size, dwarf_version);
8002 printf ("\n");
8003 return data;
8004 }
8005
8006 static int
8007 display_debug_info (Elf_Internal_Shdr *section,
8008 unsigned char *start,
8009 FILE *file)
8010 {
8011 unsigned char *end = start + section->sh_size;
8012 unsigned char *section_begin = start;
8013
8014 printf (_("The section %s contains:\n\n"), SECTION_NAME (section));
8015
8016 load_debug_str (file);
8017 load_debug_loc (file);
8018
8019 while (start < end)
8020 {
8021 DWARF2_Internal_CompUnit compunit;
8022 Elf_Internal_Shdr *relsec;
8023 unsigned char *hdrptr;
8024 unsigned char *cu_abbrev_offset_ptr;
8025 unsigned char *tags;
8026 unsigned int i;
8027 int level;
8028 unsigned long cu_offset;
8029 int offset_size;
8030 int initial_length_size;
8031
8032 hdrptr = start;
8033
8034 compunit.cu_length = byte_get (hdrptr, 4);
8035 hdrptr += 4;
8036
8037 if (compunit.cu_length == 0xffffffff)
8038 {
8039 compunit.cu_length = byte_get (hdrptr, 8);
8040 hdrptr += 8;
8041 offset_size = 8;
8042 initial_length_size = 12;
8043 }
8044 else
8045 {
8046 offset_size = 4;
8047 initial_length_size = 4;
8048 }
8049
8050 compunit.cu_version = byte_get (hdrptr, 2);
8051 hdrptr += 2;
8052
8053 /* Apply addends of RELA relocations. */
8054 for (relsec = section_headers;
8055 relsec < section_headers + elf_header.e_shnum;
8056 ++relsec)
8057 {
8058 unsigned long nrelas;
8059 Elf_Internal_Rela *rela, *rp;
8060 Elf_Internal_Shdr *symsec;
8061 Elf_Internal_Sym *symtab;
8062 Elf_Internal_Sym *sym;
8063
8064 if (relsec->sh_type != SHT_RELA
8065 || SECTION_HEADER (relsec->sh_info) != section
8066 || relsec->sh_size == 0)
8067 continue;
8068
8069 if (!slurp_rela_relocs (file, relsec->sh_offset, relsec->sh_size,
8070 & rela, & nrelas))
8071 return 0;
8072
8073 symsec = SECTION_HEADER (relsec->sh_link);
8074 symtab = GET_ELF_SYMBOLS (file, symsec);
8075
8076 for (rp = rela; rp < rela + nrelas; ++rp)
8077 {
8078 unsigned char *loc;
8079
8080 if (rp->r_offset >= (bfd_vma) (hdrptr - section_begin)
8081 && section->sh_size > (bfd_vma) offset_size
8082 && rp->r_offset <= section->sh_size - offset_size)
8083 loc = section_begin + rp->r_offset;
8084 else
8085 continue;
8086
8087 if (is_32bit_elf)
8088 {
8089 sym = symtab + ELF32_R_SYM (rp->r_info);
8090
8091 if (ELF32_R_SYM (rp->r_info) != 0
8092 && ELF32_ST_TYPE (sym->st_info) != STT_SECTION)
8093 {
8094 warn (_("Skipping unexpected symbol type %u\n"),
8095 ELF32_ST_TYPE (sym->st_info));
8096 continue;
8097 }
8098 }
8099 else
8100 {
8101 sym = symtab + ELF64_R_SYM (rp->r_info);
8102
8103 if (ELF64_R_SYM (rp->r_info) != 0
8104 && ELF64_ST_TYPE (sym->st_info) != STT_SECTION)
8105 {
8106 warn (_("Skipping unexpected symbol type %u\n"),
8107 ELF64_ST_TYPE (sym->st_info));
8108 continue;
8109 }
8110 }
8111
8112 byte_put (loc, rp->r_addend, offset_size);
8113 }
8114
8115 free (rela);
8116 break;
8117 }
8118
8119 cu_abbrev_offset_ptr = hdrptr;
8120 compunit.cu_abbrev_offset = byte_get (hdrptr, offset_size);
8121 hdrptr += offset_size;
8122
8123 compunit.cu_pointer_size = byte_get (hdrptr, 1);
8124 hdrptr += 1;
8125
8126 tags = hdrptr;
8127 cu_offset = start - section_begin;
8128 start += compunit.cu_length + initial_length_size;
8129
8130 printf (_(" Compilation Unit @ %lx:\n"), cu_offset);
8131 printf (_(" Length: %ld\n"), compunit.cu_length);
8132 printf (_(" Version: %d\n"), compunit.cu_version);
8133 printf (_(" Abbrev Offset: %ld\n"), compunit.cu_abbrev_offset);
8134 printf (_(" Pointer Size: %d\n"), compunit.cu_pointer_size);
8135
8136 if (compunit.cu_version != 2 && compunit.cu_version != 3)
8137 {
8138 warn (_("Only version 2 and 3 DWARF debug information is currently supported.\n"));
8139 continue;
8140 }
8141
8142 free_abbrevs ();
8143
8144 /* Read in the abbrevs used by this compilation unit. */
8145 {
8146 Elf_Internal_Shdr *sec;
8147 unsigned char *begin;
8148
8149 /* Locate the .debug_abbrev section and process it. */
8150 for (i = 0, sec = section_headers;
8151 i < elf_header.e_shnum;
8152 i++, sec++)
8153 if (strcmp (SECTION_NAME (sec), ".debug_abbrev") == 0)
8154 break;
8155
8156 if (i == elf_header.e_shnum || sec->sh_size == 0)
8157 {
8158 warn (_("Unable to locate .debug_abbrev section!\n"));
8159 return 0;
8160 }
8161
8162 begin = get_data (NULL, file, sec->sh_offset, sec->sh_size,
8163 _("debug_abbrev section data"));
8164 if (!begin)
8165 return 0;
8166
8167 process_abbrev_section (begin + compunit.cu_abbrev_offset,
8168 begin + sec->sh_size);
8169
8170 free (begin);
8171 }
8172
8173 level = 0;
8174 while (tags < start)
8175 {
8176 int bytes_read;
8177 unsigned long abbrev_number;
8178 abbrev_entry *entry;
8179 abbrev_attr *attr;
8180
8181 abbrev_number = read_leb128 (tags, & bytes_read, 0);
8182 tags += bytes_read;
8183
8184 /* A null DIE marks the end of a list of children. */
8185 if (abbrev_number == 0)
8186 {
8187 --level;
8188 continue;
8189 }
8190
8191 /* Scan through the abbreviation list until we reach the
8192 correct entry. */
8193 for (entry = first_abbrev;
8194 entry && entry->entry != abbrev_number;
8195 entry = entry->next)
8196 continue;
8197
8198 if (entry == NULL)
8199 {
8200 warn (_("Unable to locate entry %lu in the abbreviation table\n"),
8201 abbrev_number);
8202 return 0;
8203 }
8204
8205 printf (_(" <%d><%lx>: Abbrev Number: %lu (%s)\n"),
8206 level,
8207 (unsigned long) (tags - section_begin - bytes_read),
8208 abbrev_number,
8209 get_TAG_name (entry->tag));
8210
8211 for (attr = entry->first_attr; attr; attr = attr->next)
8212 tags = read_and_display_attr (attr->attribute,
8213 attr->form,
8214 tags, cu_offset,
8215 compunit.cu_pointer_size,
8216 offset_size,
8217 compunit.cu_version);
8218
8219 if (entry->children)
8220 ++level;
8221 }
8222 }
8223
8224 free_debug_str ();
8225 free_debug_loc ();
8226
8227 printf ("\n");
8228
8229 return 1;
8230 }
8231
8232 static int
8233 display_debug_aranges (Elf_Internal_Shdr *section,
8234 unsigned char *start,
8235 FILE *file ATTRIBUTE_UNUSED)
8236 {
8237 unsigned char *end = start + section->sh_size;
8238
8239 printf (_("The section %s contains:\n\n"), SECTION_NAME (section));
8240
8241 while (start < end)
8242 {
8243 unsigned char *hdrptr;
8244 DWARF2_Internal_ARange arange;
8245 unsigned char *ranges;
8246 unsigned long length;
8247 unsigned long address;
8248 int excess;
8249 int offset_size;
8250 int initial_length_size;
8251
8252 hdrptr = start;
8253
8254 arange.ar_length = byte_get (hdrptr, 4);
8255 hdrptr += 4;
8256
8257 if (arange.ar_length == 0xffffffff)
8258 {
8259 arange.ar_length = byte_get (hdrptr, 8);
8260 hdrptr += 8;
8261 offset_size = 8;
8262 initial_length_size = 12;
8263 }
8264 else
8265 {
8266 offset_size = 4;
8267 initial_length_size = 4;
8268 }
8269
8270 arange.ar_version = byte_get (hdrptr, 2);
8271 hdrptr += 2;
8272
8273 arange.ar_info_offset = byte_get (hdrptr, offset_size);
8274 hdrptr += offset_size;
8275
8276 arange.ar_pointer_size = byte_get (hdrptr, 1);
8277 hdrptr += 1;
8278
8279 arange.ar_segment_size = byte_get (hdrptr, 1);
8280 hdrptr += 1;
8281
8282 if (arange.ar_version != 2 && arange.ar_version != 3)
8283 {
8284 warn (_("Only DWARF 2 and 3 aranges are currently supported.\n"));
8285 break;
8286 }
8287
8288 printf (_(" Length: %ld\n"), arange.ar_length);
8289 printf (_(" Version: %d\n"), arange.ar_version);
8290 printf (_(" Offset into .debug_info: %lx\n"), arange.ar_info_offset);
8291 printf (_(" Pointer Size: %d\n"), arange.ar_pointer_size);
8292 printf (_(" Segment Size: %d\n"), arange.ar_segment_size);
8293
8294 printf (_("\n Address Length\n"));
8295
8296 ranges = hdrptr;
8297
8298 /* Must pad to an alignment boundary that is twice the pointer size. */
8299 excess = (hdrptr - start) % (2 * arange.ar_pointer_size);
8300 if (excess)
8301 ranges += (2 * arange.ar_pointer_size) - excess;
8302
8303 for (;;)
8304 {
8305 address = byte_get (ranges, arange.ar_pointer_size);
8306
8307 ranges += arange.ar_pointer_size;
8308
8309 length = byte_get (ranges, arange.ar_pointer_size);
8310
8311 ranges += arange.ar_pointer_size;
8312
8313 /* A pair of zeros marks the end of the list. */
8314 if (address == 0 && length == 0)
8315 break;
8316
8317 printf (" %8.8lx %lu\n", address, length);
8318 }
8319
8320 start += arange.ar_length + initial_length_size;
8321 }
8322
8323 printf ("\n");
8324
8325 return 1;
8326 }
8327
8328 typedef struct Frame_Chunk
8329 {
8330 struct Frame_Chunk *next;
8331 unsigned char *chunk_start;
8332 int ncols;
8333 /* DW_CFA_{undefined,same_value,offset,register,unreferenced} */
8334 short int *col_type;
8335 int *col_offset;
8336 char *augmentation;
8337 unsigned int code_factor;
8338 int data_factor;
8339 unsigned long pc_begin;
8340 unsigned long pc_range;
8341 int cfa_reg;
8342 int cfa_offset;
8343 int ra;
8344 unsigned char fde_encoding;
8345 unsigned char cfa_exp;
8346 }
8347 Frame_Chunk;
8348
8349 /* A marker for a col_type that means this column was never referenced
8350 in the frame info. */
8351 #define DW_CFA_unreferenced (-1)
8352
8353 static void
8354 frame_need_space (Frame_Chunk *fc, int reg)
8355 {
8356 int prev = fc->ncols;
8357
8358 if (reg < fc->ncols)
8359 return;
8360
8361 fc->ncols = reg + 1;
8362 fc->col_type = xrealloc (fc->col_type, fc->ncols * sizeof (short int));
8363 fc->col_offset = xrealloc (fc->col_offset, fc->ncols * sizeof (int));
8364
8365 while (prev < fc->ncols)
8366 {
8367 fc->col_type[prev] = DW_CFA_unreferenced;
8368 fc->col_offset[prev] = 0;
8369 prev++;
8370 }
8371 }
8372
8373 static void
8374 frame_display_row (Frame_Chunk *fc, int *need_col_headers, int *max_regs)
8375 {
8376 int r;
8377 char tmp[100];
8378
8379 if (*max_regs < fc->ncols)
8380 *max_regs = fc->ncols;
8381
8382 if (*need_col_headers)
8383 {
8384 *need_col_headers = 0;
8385
8386 printf (" LOC CFA ");
8387
8388 for (r = 0; r < *max_regs; r++)
8389 if (fc->col_type[r] != DW_CFA_unreferenced)
8390 {
8391 if (r == fc->ra)
8392 printf ("ra ");
8393 else
8394 printf ("r%-4d", r);
8395 }
8396
8397 printf ("\n");
8398 }
8399
8400 printf ("%08lx ", fc->pc_begin);
8401 if (fc->cfa_exp)
8402 strcpy (tmp, "exp");
8403 else
8404 sprintf (tmp, "r%d%+d", fc->cfa_reg, fc->cfa_offset);
8405 printf ("%-8s ", tmp);
8406
8407 for (r = 0; r < fc->ncols; r++)
8408 {
8409 if (fc->col_type[r] != DW_CFA_unreferenced)
8410 {
8411 switch (fc->col_type[r])
8412 {
8413 case DW_CFA_undefined:
8414 strcpy (tmp, "u");
8415 break;
8416 case DW_CFA_same_value:
8417 strcpy (tmp, "s");
8418 break;
8419 case DW_CFA_offset:
8420 sprintf (tmp, "c%+d", fc->col_offset[r]);
8421 break;
8422 case DW_CFA_register:
8423 sprintf (tmp, "r%d", fc->col_offset[r]);
8424 break;
8425 case DW_CFA_expression:
8426 strcpy (tmp, "exp");
8427 break;
8428 default:
8429 strcpy (tmp, "n/a");
8430 break;
8431 }
8432 printf ("%-5s", tmp);
8433 }
8434 }
8435 printf ("\n");
8436 }
8437
8438 static int
8439 size_of_encoded_value (int encoding)
8440 {
8441 switch (encoding & 0x7)
8442 {
8443 default: /* ??? */
8444 case 0: return is_32bit_elf ? 4 : 8;
8445 case 2: return 2;
8446 case 3: return 4;
8447 case 4: return 8;
8448 }
8449 }
8450
8451 static bfd_vma
8452 get_encoded_value (unsigned char *data, int encoding)
8453 {
8454 int size = size_of_encoded_value (encoding);
8455 if (encoding & DW_EH_PE_signed)
8456 return byte_get_signed (data, size);
8457 else
8458 return byte_get (data, size);
8459 }
8460
8461 #define GET(N) byte_get (start, N); start += N
8462 #define LEB() read_leb128 (start, & length_return, 0); start += length_return
8463 #define SLEB() read_leb128 (start, & length_return, 1); start += length_return
8464
8465 static int
8466 display_debug_frames (Elf_Internal_Shdr *section,
8467 unsigned char *start,
8468 FILE *file ATTRIBUTE_UNUSED)
8469 {
8470 unsigned char *end = start + section->sh_size;
8471 unsigned char *section_start = start;
8472 Frame_Chunk *chunks = 0;
8473 Frame_Chunk *remembered_state = 0;
8474 Frame_Chunk *rs;
8475 int is_eh = (strcmp (SECTION_NAME (section), ".eh_frame") == 0);
8476 int length_return;
8477 int max_regs = 0;
8478 int addr_size = is_32bit_elf ? 4 : 8;
8479
8480 printf (_("The section %s contains:\n"), SECTION_NAME (section));
8481
8482 while (start < end)
8483 {
8484 unsigned char *saved_start;
8485 unsigned char *block_end;
8486 unsigned long length;
8487 unsigned long cie_id;
8488 Frame_Chunk *fc;
8489 Frame_Chunk *cie;
8490 int need_col_headers = 1;
8491 unsigned char *augmentation_data = NULL;
8492 unsigned long augmentation_data_len = 0;
8493 int encoded_ptr_size = addr_size;
8494 int offset_size;
8495 int initial_length_size;
8496
8497 saved_start = start;
8498 length = byte_get (start, 4); start += 4;
8499
8500 if (length == 0)
8501 {
8502 printf ("\n%08lx ZERO terminator\n\n",
8503 (unsigned long)(saved_start - section_start));
8504 return 1;
8505 }
8506
8507 if (length == 0xffffffff)
8508 {
8509 length = byte_get (start, 8);
8510 start += 8;
8511 offset_size = 8;
8512 initial_length_size = 12;
8513 }
8514 else
8515 {
8516 offset_size = 4;
8517 initial_length_size = 4;
8518 }
8519
8520 block_end = saved_start + length + initial_length_size;
8521 cie_id = byte_get (start, offset_size); start += offset_size;
8522
8523 if (is_eh ? (cie_id == 0) : (cie_id == DW_CIE_ID))
8524 {
8525 int version;
8526
8527 fc = xmalloc (sizeof (Frame_Chunk));
8528 memset (fc, 0, sizeof (Frame_Chunk));
8529
8530 fc->next = chunks;
8531 chunks = fc;
8532 fc->chunk_start = saved_start;
8533 fc->ncols = 0;
8534 fc->col_type = xmalloc (sizeof (short int));
8535 fc->col_offset = xmalloc (sizeof (int));
8536 frame_need_space (fc, max_regs-1);
8537
8538 version = *start++;
8539
8540 fc->augmentation = start;
8541 start = strchr (start, '\0') + 1;
8542
8543 if (fc->augmentation[0] == 'z')
8544 {
8545 fc->code_factor = LEB ();
8546 fc->data_factor = SLEB ();
8547 fc->ra = byte_get (start, 1); start += 1;
8548 augmentation_data_len = LEB ();
8549 augmentation_data = start;
8550 start += augmentation_data_len;
8551 }
8552 else if (strcmp (fc->augmentation, "eh") == 0)
8553 {
8554 start += addr_size;
8555 fc->code_factor = LEB ();
8556 fc->data_factor = SLEB ();
8557 fc->ra = byte_get (start, 1); start += 1;
8558 }
8559 else
8560 {
8561 fc->code_factor = LEB ();
8562 fc->data_factor = SLEB ();
8563 fc->ra = byte_get (start, 1); start += 1;
8564 }
8565 cie = fc;
8566
8567 if (do_debug_frames_interp)
8568 printf ("\n%08lx %08lx %08lx CIE \"%s\" cf=%d df=%d ra=%d\n",
8569 (unsigned long)(saved_start - section_start), length, cie_id,
8570 fc->augmentation, fc->code_factor, fc->data_factor,
8571 fc->ra);
8572 else
8573 {
8574 printf ("\n%08lx %08lx %08lx CIE\n",
8575 (unsigned long)(saved_start - section_start), length, cie_id);
8576 printf (" Version: %d\n", version);
8577 printf (" Augmentation: \"%s\"\n", fc->augmentation);
8578 printf (" Code alignment factor: %u\n", fc->code_factor);
8579 printf (" Data alignment factor: %d\n", fc->data_factor);
8580 printf (" Return address column: %d\n", fc->ra);
8581
8582 if (augmentation_data_len)
8583 {
8584 unsigned long i;
8585 printf (" Augmentation data: ");
8586 for (i = 0; i < augmentation_data_len; ++i)
8587 printf (" %02x", augmentation_data[i]);
8588 putchar ('\n');
8589 }
8590 putchar ('\n');
8591 }
8592
8593 if (augmentation_data_len)
8594 {
8595 unsigned char *p, *q;
8596 p = fc->augmentation + 1;
8597 q = augmentation_data;
8598
8599 while (1)
8600 {
8601 if (*p == 'L')
8602 q++;
8603 else if (*p == 'P')
8604 q += 1 + size_of_encoded_value (*q);
8605 else if (*p == 'R')
8606 fc->fde_encoding = *q++;
8607 else
8608 break;
8609 p++;
8610 }
8611
8612 if (fc->fde_encoding)
8613 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
8614 }
8615
8616 frame_need_space (fc, fc->ra);
8617 }
8618 else
8619 {
8620 unsigned char *look_for;
8621 static Frame_Chunk fde_fc;
8622
8623 fc = & fde_fc;
8624 memset (fc, 0, sizeof (Frame_Chunk));
8625
8626 look_for = is_eh ? start - 4 - cie_id : section_start + cie_id;
8627
8628 for (cie = chunks; cie ; cie = cie->next)
8629 if (cie->chunk_start == look_for)
8630 break;
8631
8632 if (!cie)
8633 {
8634 warn ("Invalid CIE pointer %08lx in FDE at %08lx\n",
8635 cie_id, saved_start);
8636 start = block_end;
8637 fc->ncols = 0;
8638 fc->col_type = xmalloc (sizeof (short int));
8639 fc->col_offset = xmalloc (sizeof (int));
8640 frame_need_space (fc, max_regs - 1);
8641 cie = fc;
8642 fc->augmentation = "";
8643 fc->fde_encoding = 0;
8644 }
8645 else
8646 {
8647 fc->ncols = cie->ncols;
8648 fc->col_type = xmalloc (fc->ncols * sizeof (short int));
8649 fc->col_offset = xmalloc (fc->ncols * sizeof (int));
8650 memcpy (fc->col_type, cie->col_type, fc->ncols * sizeof (short int));
8651 memcpy (fc->col_offset, cie->col_offset, fc->ncols * sizeof (int));
8652 fc->augmentation = cie->augmentation;
8653 fc->code_factor = cie->code_factor;
8654 fc->data_factor = cie->data_factor;
8655 fc->cfa_reg = cie->cfa_reg;
8656 fc->cfa_offset = cie->cfa_offset;
8657 fc->ra = cie->ra;
8658 frame_need_space (fc, max_regs-1);
8659 fc->fde_encoding = cie->fde_encoding;
8660 }
8661
8662 if (fc->fde_encoding)
8663 encoded_ptr_size = size_of_encoded_value (fc->fde_encoding);
8664
8665 fc->pc_begin = get_encoded_value (start, fc->fde_encoding);
8666 if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
8667 fc->pc_begin += section->sh_addr + (start - section_start);
8668 start += encoded_ptr_size;
8669 fc->pc_range = byte_get (start, encoded_ptr_size);
8670 start += encoded_ptr_size;
8671
8672 if (cie->augmentation[0] == 'z')
8673 {
8674 augmentation_data_len = LEB ();
8675 augmentation_data = start;
8676 start += augmentation_data_len;
8677 }
8678
8679 printf ("\n%08lx %08lx %08lx FDE cie=%08lx pc=%08lx..%08lx\n",
8680 (unsigned long)(saved_start - section_start), length, cie_id,
8681 (unsigned long)(cie->chunk_start - section_start),
8682 fc->pc_begin, fc->pc_begin + fc->pc_range);
8683 if (! do_debug_frames_interp && augmentation_data_len)
8684 {
8685 unsigned long i;
8686 printf (" Augmentation data: ");
8687 for (i = 0; i < augmentation_data_len; ++i)
8688 printf (" %02x", augmentation_data[i]);
8689 putchar ('\n');
8690 putchar ('\n');
8691 }
8692 }
8693
8694 /* At this point, fc is the current chunk, cie (if any) is set, and we're
8695 about to interpret instructions for the chunk. */
8696 /* ??? At present we need to do this always, since this sizes the
8697 fc->col_type and fc->col_offset arrays, which we write into always.
8698 We should probably split the interpreted and non-interpreted bits
8699 into two different routines, since there's so much that doesn't
8700 really overlap between them. */
8701 if (1 || do_debug_frames_interp)
8702 {
8703 /* Start by making a pass over the chunk, allocating storage
8704 and taking note of what registers are used. */
8705 unsigned char *tmp = start;
8706
8707 while (start < block_end)
8708 {
8709 unsigned op, opa;
8710 unsigned long reg, tmp;
8711
8712 op = *start++;
8713 opa = op & 0x3f;
8714 if (op & 0xc0)
8715 op &= 0xc0;
8716
8717 /* Warning: if you add any more cases to this switch, be
8718 sure to add them to the corresponding switch below. */
8719 switch (op)
8720 {
8721 case DW_CFA_advance_loc:
8722 break;
8723 case DW_CFA_offset:
8724 LEB ();
8725 frame_need_space (fc, opa);
8726 fc->col_type[opa] = DW_CFA_undefined;
8727 break;
8728 case DW_CFA_restore:
8729 frame_need_space (fc, opa);
8730 fc->col_type[opa] = DW_CFA_undefined;
8731 break;
8732 case DW_CFA_set_loc:
8733 start += encoded_ptr_size;
8734 break;
8735 case DW_CFA_advance_loc1:
8736 start += 1;
8737 break;
8738 case DW_CFA_advance_loc2:
8739 start += 2;
8740 break;
8741 case DW_CFA_advance_loc4:
8742 start += 4;
8743 break;
8744 case DW_CFA_offset_extended:
8745 reg = LEB (); LEB ();
8746 frame_need_space (fc, reg);
8747 fc->col_type[reg] = DW_CFA_undefined;
8748 break;
8749 case DW_CFA_restore_extended:
8750 reg = LEB ();
8751 frame_need_space (fc, reg);
8752 fc->col_type[reg] = DW_CFA_undefined;
8753 break;
8754 case DW_CFA_undefined:
8755 reg = LEB ();
8756 frame_need_space (fc, reg);
8757 fc->col_type[reg] = DW_CFA_undefined;
8758 break;
8759 case DW_CFA_same_value:
8760 reg = LEB ();
8761 frame_need_space (fc, reg);
8762 fc->col_type[reg] = DW_CFA_undefined;
8763 break;
8764 case DW_CFA_register:
8765 reg = LEB (); LEB ();
8766 frame_need_space (fc, reg);
8767 fc->col_type[reg] = DW_CFA_undefined;
8768 break;
8769 case DW_CFA_def_cfa:
8770 LEB (); LEB ();
8771 break;
8772 case DW_CFA_def_cfa_register:
8773 LEB ();
8774 break;
8775 case DW_CFA_def_cfa_offset:
8776 LEB ();
8777 break;
8778 case DW_CFA_def_cfa_expression:
8779 tmp = LEB ();
8780 start += tmp;
8781 break;
8782 case DW_CFA_expression:
8783 reg = LEB ();
8784 tmp = LEB ();
8785 start += tmp;
8786 frame_need_space (fc, reg);
8787 fc->col_type[reg] = DW_CFA_undefined;
8788 break;
8789 case DW_CFA_offset_extended_sf:
8790 reg = LEB (); SLEB ();
8791 frame_need_space (fc, reg);
8792 fc->col_type[reg] = DW_CFA_undefined;
8793 break;
8794 case DW_CFA_def_cfa_sf:
8795 LEB (); SLEB ();
8796 break;
8797 case DW_CFA_def_cfa_offset_sf:
8798 SLEB ();
8799 break;
8800 case DW_CFA_MIPS_advance_loc8:
8801 start += 8;
8802 break;
8803 case DW_CFA_GNU_args_size:
8804 LEB ();
8805 break;
8806 case DW_CFA_GNU_negative_offset_extended:
8807 reg = LEB (); LEB ();
8808 frame_need_space (fc, reg);
8809 fc->col_type[reg] = DW_CFA_undefined;
8810
8811 default:
8812 break;
8813 }
8814 }
8815 start = tmp;
8816 }
8817
8818 /* Now we know what registers are used, make a second pass over
8819 the chunk, this time actually printing out the info. */
8820
8821 while (start < block_end)
8822 {
8823 unsigned op, opa;
8824 unsigned long ul, reg, roffs;
8825 long l, ofs;
8826 bfd_vma vma;
8827
8828 op = *start++;
8829 opa = op & 0x3f;
8830 if (op & 0xc0)
8831 op &= 0xc0;
8832
8833 /* Warning: if you add any more cases to this switch, be
8834 sure to add them to the corresponding switch above. */
8835 switch (op)
8836 {
8837 case DW_CFA_advance_loc:
8838 if (do_debug_frames_interp)
8839 frame_display_row (fc, &need_col_headers, &max_regs);
8840 else
8841 printf (" DW_CFA_advance_loc: %d to %08lx\n",
8842 opa * fc->code_factor,
8843 fc->pc_begin + opa * fc->code_factor);
8844 fc->pc_begin += opa * fc->code_factor;
8845 break;
8846
8847 case DW_CFA_offset:
8848 roffs = LEB ();
8849 if (! do_debug_frames_interp)
8850 printf (" DW_CFA_offset: r%d at cfa%+ld\n",
8851 opa, roffs * fc->data_factor);
8852 fc->col_type[opa] = DW_CFA_offset;
8853 fc->col_offset[opa] = roffs * fc->data_factor;
8854 break;
8855
8856 case DW_CFA_restore:
8857 if (! do_debug_frames_interp)
8858 printf (" DW_CFA_restore: r%d\n", opa);
8859 fc->col_type[opa] = cie->col_type[opa];
8860 fc->col_offset[opa] = cie->col_offset[opa];
8861 break;
8862
8863 case DW_CFA_set_loc:
8864 vma = get_encoded_value (start, fc->fde_encoding);
8865 if ((fc->fde_encoding & 0x70) == DW_EH_PE_pcrel)
8866 vma += section->sh_addr + (start - section_start);
8867 start += encoded_ptr_size;
8868 if (do_debug_frames_interp)
8869 frame_display_row (fc, &need_col_headers, &max_regs);
8870 else
8871 printf (" DW_CFA_set_loc: %08lx\n", (unsigned long)vma);
8872 fc->pc_begin = vma;
8873 break;
8874
8875 case DW_CFA_advance_loc1:
8876 ofs = byte_get (start, 1); start += 1;
8877 if (do_debug_frames_interp)
8878 frame_display_row (fc, &need_col_headers, &max_regs);
8879 else
8880 printf (" DW_CFA_advance_loc1: %ld to %08lx\n",
8881 ofs * fc->code_factor,
8882 fc->pc_begin + ofs * fc->code_factor);
8883 fc->pc_begin += ofs * fc->code_factor;
8884 break;
8885
8886 case DW_CFA_advance_loc2:
8887 ofs = byte_get (start, 2); start += 2;
8888 if (do_debug_frames_interp)
8889 frame_display_row (fc, &need_col_headers, &max_regs);
8890 else
8891 printf (" DW_CFA_advance_loc2: %ld to %08lx\n",
8892 ofs * fc->code_factor,
8893 fc->pc_begin + ofs * fc->code_factor);
8894 fc->pc_begin += ofs * fc->code_factor;
8895 break;
8896
8897 case DW_CFA_advance_loc4:
8898 ofs = byte_get (start, 4); start += 4;
8899 if (do_debug_frames_interp)
8900 frame_display_row (fc, &need_col_headers, &max_regs);
8901 else
8902 printf (" DW_CFA_advance_loc4: %ld to %08lx\n",
8903 ofs * fc->code_factor,
8904 fc->pc_begin + ofs * fc->code_factor);
8905 fc->pc_begin += ofs * fc->code_factor;
8906 break;
8907
8908 case DW_CFA_offset_extended:
8909 reg = LEB ();
8910 roffs = LEB ();
8911 if (! do_debug_frames_interp)
8912 printf (" DW_CFA_offset_extended: r%ld at cfa%+ld\n",
8913 reg, roffs * fc->data_factor);
8914 fc->col_type[reg] = DW_CFA_offset;
8915 fc->col_offset[reg] = roffs * fc->data_factor;
8916 break;
8917
8918 case DW_CFA_restore_extended:
8919 reg = LEB ();
8920 if (! do_debug_frames_interp)
8921 printf (" DW_CFA_restore_extended: r%ld\n", reg);
8922 fc->col_type[reg] = cie->col_type[reg];
8923 fc->col_offset[reg] = cie->col_offset[reg];
8924 break;
8925
8926 case DW_CFA_undefined:
8927 reg = LEB ();
8928 if (! do_debug_frames_interp)
8929 printf (" DW_CFA_undefined: r%ld\n", reg);
8930 fc->col_type[reg] = DW_CFA_undefined;
8931 fc->col_offset[reg] = 0;
8932 break;
8933
8934 case DW_CFA_same_value:
8935 reg = LEB ();
8936 if (! do_debug_frames_interp)
8937 printf (" DW_CFA_same_value: r%ld\n", reg);
8938 fc->col_type[reg] = DW_CFA_same_value;
8939 fc->col_offset[reg] = 0;
8940 break;
8941
8942 case DW_CFA_register:
8943 reg = LEB ();
8944 roffs = LEB ();
8945 if (! do_debug_frames_interp)
8946 printf (" DW_CFA_register: r%ld in r%ld\n", reg, roffs);
8947 fc->col_type[reg] = DW_CFA_register;
8948 fc->col_offset[reg] = roffs;
8949 break;
8950
8951 case DW_CFA_remember_state:
8952 if (! do_debug_frames_interp)
8953 printf (" DW_CFA_remember_state\n");
8954 rs = xmalloc (sizeof (Frame_Chunk));
8955 rs->ncols = fc->ncols;
8956 rs->col_type = xmalloc (rs->ncols * sizeof (short int));
8957 rs->col_offset = xmalloc (rs->ncols * sizeof (int));
8958 memcpy (rs->col_type, fc->col_type, rs->ncols);
8959 memcpy (rs->col_offset, fc->col_offset, rs->ncols * sizeof (int));
8960 rs->next = remembered_state;
8961 remembered_state = rs;
8962 break;
8963
8964 case DW_CFA_restore_state:
8965 if (! do_debug_frames_interp)
8966 printf (" DW_CFA_restore_state\n");
8967 rs = remembered_state;
8968 remembered_state = rs->next;
8969 frame_need_space (fc, rs->ncols-1);
8970 memcpy (fc->col_type, rs->col_type, rs->ncols);
8971 memcpy (fc->col_offset, rs->col_offset, rs->ncols * sizeof (int));
8972 free (rs->col_type);
8973 free (rs->col_offset);
8974 free (rs);
8975 break;
8976
8977 case DW_CFA_def_cfa:
8978 fc->cfa_reg = LEB ();
8979 fc->cfa_offset = LEB ();
8980 fc->cfa_exp = 0;
8981 if (! do_debug_frames_interp)
8982 printf (" DW_CFA_def_cfa: r%d ofs %d\n",
8983 fc->cfa_reg, fc->cfa_offset);
8984 break;
8985
8986 case DW_CFA_def_cfa_register:
8987 fc->cfa_reg = LEB ();
8988 fc->cfa_exp = 0;
8989 if (! do_debug_frames_interp)
8990 printf (" DW_CFA_def_cfa_reg: r%d\n", fc->cfa_reg);
8991 break;
8992
8993 case DW_CFA_def_cfa_offset:
8994 fc->cfa_offset = LEB ();
8995 if (! do_debug_frames_interp)
8996 printf (" DW_CFA_def_cfa_offset: %d\n", fc->cfa_offset);
8997 break;
8998
8999 case DW_CFA_nop:
9000 if (! do_debug_frames_interp)
9001 printf (" DW_CFA_nop\n");
9002 break;
9003
9004 case DW_CFA_def_cfa_expression:
9005 ul = LEB ();
9006 if (! do_debug_frames_interp)
9007 {
9008 printf (" DW_CFA_def_cfa_expression (");
9009 decode_location_expression (start, addr_size, ul);
9010 printf (")\n");
9011 }
9012 fc->cfa_exp = 1;
9013 start += ul;
9014 break;
9015
9016 case DW_CFA_expression:
9017 reg = LEB ();
9018 ul = LEB ();
9019 if (! do_debug_frames_interp)
9020 {
9021 printf (" DW_CFA_expression: r%ld (", reg);
9022 decode_location_expression (start, addr_size, ul);
9023 printf (")\n");
9024 }
9025 fc->col_type[reg] = DW_CFA_expression;
9026 start += ul;
9027 break;
9028
9029 case DW_CFA_offset_extended_sf:
9030 reg = LEB ();
9031 l = SLEB ();
9032 frame_need_space (fc, reg);
9033 if (! do_debug_frames_interp)
9034 printf (" DW_CFA_offset_extended_sf: r%ld at cfa%+ld\n",
9035 reg, l * fc->data_factor);
9036 fc->col_type[reg] = DW_CFA_offset;
9037 fc->col_offset[reg] = l * fc->data_factor;
9038 break;
9039
9040 case DW_CFA_def_cfa_sf:
9041 fc->cfa_reg = LEB ();
9042 fc->cfa_offset = SLEB ();
9043 fc->cfa_exp = 0;
9044 if (! do_debug_frames_interp)
9045 printf (" DW_CFA_def_cfa_sf: r%d ofs %d\n",
9046 fc->cfa_reg, fc->cfa_offset);
9047 break;
9048
9049 case DW_CFA_def_cfa_offset_sf:
9050 fc->cfa_offset = SLEB ();
9051 if (! do_debug_frames_interp)
9052 printf (" DW_CFA_def_cfa_offset_sf: %d\n", fc->cfa_offset);
9053 break;
9054
9055 case DW_CFA_MIPS_advance_loc8:
9056 ofs = byte_get (start, 8); start += 8;
9057 if (do_debug_frames_interp)
9058 frame_display_row (fc, &need_col_headers, &max_regs);
9059 else
9060 printf (" DW_CFA_MIPS_advance_loc8: %ld to %08lx\n",
9061 ofs * fc->code_factor,
9062 fc->pc_begin + ofs * fc->code_factor);
9063 fc->pc_begin += ofs * fc->code_factor;
9064 break;
9065
9066 case DW_CFA_GNU_window_save:
9067 if (! do_debug_frames_interp)
9068 printf (" DW_CFA_GNU_window_save\n");
9069 break;
9070
9071 case DW_CFA_GNU_args_size:
9072 ul = LEB ();
9073 if (! do_debug_frames_interp)
9074 printf (" DW_CFA_GNU_args_size: %ld\n", ul);
9075 break;
9076
9077 case DW_CFA_GNU_negative_offset_extended:
9078 reg = LEB ();
9079 l = - LEB ();
9080 frame_need_space (fc, reg);
9081 if (! do_debug_frames_interp)
9082 printf (" DW_CFA_GNU_negative_offset_extended: r%ld at cfa%+ld\n",
9083 reg, l * fc->data_factor);
9084 fc->col_type[reg] = DW_CFA_offset;
9085 fc->col_offset[reg] = l * fc->data_factor;
9086 break;
9087
9088 default:
9089 fprintf (stderr, "unsupported or unknown DW_CFA_%d\n", op);
9090 start = block_end;
9091 }
9092 }
9093
9094 if (do_debug_frames_interp)
9095 frame_display_row (fc, &need_col_headers, &max_regs);
9096
9097 start = block_end;
9098 }
9099
9100 printf ("\n");
9101
9102 return 1;
9103 }
9104
9105 #undef GET
9106 #undef LEB
9107 #undef SLEB
9108
9109 static int
9110 display_debug_not_supported (Elf_Internal_Shdr *section,
9111 unsigned char *start ATTRIBUTE_UNUSED,
9112 FILE *file ATTRIBUTE_UNUSED)
9113 {
9114 printf (_("Displaying the debug contents of section %s is not yet supported.\n"),
9115 SECTION_NAME (section));
9116
9117 return 1;
9118 }
9119
9120 /* Pre-scan the .debug_info section to record the size of address.
9121 When dumping the .debug_line, we use that size information, assuming
9122 that all compilation units have the same address size. */
9123 static int
9124 prescan_debug_info (Elf_Internal_Shdr *section ATTRIBUTE_UNUSED,
9125 unsigned char *start,
9126 FILE *file ATTRIBUTE_UNUSED)
9127 {
9128 unsigned long length;
9129
9130 /* Read the first 4 bytes. For a 32-bit DWARF section, this will
9131 be the length. For a 64-bit DWARF section, it'll be the escape
9132 code 0xffffffff followed by an 8 byte length. For the purposes
9133 of this prescan, we don't care about the actual length, but the
9134 presence of the escape bytes does affect the location of the byte
9135 which describes the address size. */
9136 length = byte_get (start, 4);
9137
9138 if (length == 0xffffffff)
9139 {
9140 /* For 64-bit DWARF, the 1-byte address_size field is 22 bytes
9141 from the start of the section. This is computed as follows:
9142
9143 unit_length: 12 bytes
9144 version: 2 bytes
9145 debug_abbrev_offset: 8 bytes
9146 -----------------------------
9147 Total: 22 bytes */
9148
9149 debug_line_pointer_size = byte_get (start + 22, 1);
9150 }
9151 else
9152 {
9153 /* For 32-bit DWARF, the 1-byte address_size field is 10 bytes from
9154 the start of the section:
9155 unit_length: 4 bytes
9156 version: 2 bytes
9157 debug_abbrev_offset: 4 bytes
9158 -----------------------------
9159 Total: 10 bytes */
9160
9161 debug_line_pointer_size = byte_get (start + 10, 1);
9162 }
9163 return 0;
9164 }
9165
9166 /* A structure containing the name of a debug section and a pointer
9167 to a function that can decode it. The third field is a prescan
9168 function to be run over the section before displaying any of the
9169 sections. */
9170 struct
9171 {
9172 const char *const name;
9173 int (*display) (Elf_Internal_Shdr *, unsigned char *, FILE *);
9174 int (*prescan) (Elf_Internal_Shdr *, unsigned char *, FILE *);
9175 }
9176 debug_displays[] =
9177 {
9178 { ".debug_abbrev", display_debug_abbrev, NULL },
9179 { ".debug_aranges", display_debug_aranges, NULL },
9180 { ".debug_frame", display_debug_frames, NULL },
9181 { ".debug_info", display_debug_info, prescan_debug_info },
9182 { ".debug_line", display_debug_lines, NULL },
9183 { ".debug_pubnames", display_debug_pubnames, NULL },
9184 { ".eh_frame", display_debug_frames, NULL },
9185 { ".debug_macinfo", display_debug_macinfo, NULL },
9186 { ".debug_str", display_debug_str, NULL },
9187 { ".debug_loc", display_debug_loc, NULL },
9188 { ".debug_pubtypes", display_debug_not_supported, NULL },
9189 { ".debug_ranges", display_debug_not_supported, NULL },
9190 { ".debug_static_func", display_debug_not_supported, NULL },
9191 { ".debug_static_vars", display_debug_not_supported, NULL },
9192 { ".debug_types", display_debug_not_supported, NULL },
9193 { ".debug_weaknames", display_debug_not_supported, NULL }
9194 };
9195
9196 static int
9197 display_debug_section (Elf_Internal_Shdr *section, FILE *file)
9198 {
9199 char *name = SECTION_NAME (section);
9200 bfd_size_type length;
9201 unsigned char *start;
9202 int i;
9203
9204 length = section->sh_size;
9205 if (length == 0)
9206 {
9207 printf (_("\nSection '%s' has no debugging data.\n"), name);
9208 return 0;
9209 }
9210
9211 start = get_data (NULL, file, section->sh_offset, length,
9212 _("debug section data"));
9213 if (!start)
9214 return 0;
9215
9216 /* See if we know how to display the contents of this section. */
9217 if (strncmp (name, ".gnu.linkonce.wi.", 17) == 0)
9218 name = ".debug_info";
9219
9220 for (i = NUM_ELEM (debug_displays); i--;)
9221 if (strcmp (debug_displays[i].name, name) == 0)
9222 {
9223 debug_displays[i].display (section, start, file);
9224 break;
9225 }
9226
9227 if (i == -1)
9228 printf (_("Unrecognized debug section: %s\n"), name);
9229
9230 free (start);
9231
9232 /* If we loaded in the abbrev section at some point,
9233 we must release it here. */
9234 free_abbrevs ();
9235
9236 return 1;
9237 }
9238
9239 static int
9240 process_section_contents (FILE *file)
9241 {
9242 Elf_Internal_Shdr *section;
9243 unsigned int i;
9244
9245 if (! do_dump)
9246 return 1;
9247
9248 /* Pre-scan the debug sections to find some debug information not
9249 present in some of them. For the .debug_line, we must find out the
9250 size of address (specified in .debug_info and .debug_aranges). */
9251 for (i = 0, section = section_headers;
9252 i < elf_header.e_shnum && i < num_dump_sects;
9253 i++, section++)
9254 {
9255 char *name = SECTION_NAME (section);
9256 int j;
9257
9258 if (section->sh_size == 0)
9259 continue;
9260
9261 /* See if there is some pre-scan operation for this section. */
9262 for (j = NUM_ELEM (debug_displays); j--;)
9263 if (strcmp (debug_displays[j].name, name) == 0)
9264 {
9265 if (debug_displays[j].prescan != NULL)
9266 {
9267 bfd_size_type length;
9268 unsigned char *start;
9269
9270 length = section->sh_size;
9271 start = get_data (NULL, file, section->sh_offset, length,
9272 _("debug section data"));
9273 if (!start)
9274 return 0;
9275
9276 debug_displays[j].prescan (section, start, file);
9277 free (start);
9278 }
9279
9280 break;
9281 }
9282 }
9283
9284 for (i = 0, section = section_headers;
9285 i < elf_header.e_shnum && i < num_dump_sects;
9286 i++, section++)
9287 {
9288 #ifdef SUPPORT_DISASSEMBLY
9289 if (dump_sects[i] & DISASS_DUMP)
9290 disassemble_section (section, file);
9291 #endif
9292 if (dump_sects[i] & HEX_DUMP)
9293 dump_section (section, file);
9294
9295 if (dump_sects[i] & DEBUG_DUMP)
9296 display_debug_section (section, file);
9297 }
9298
9299 if (i < num_dump_sects)
9300 warn (_("Some sections were not dumped because they do not exist!\n"));
9301
9302 return 1;
9303 }
9304
9305 static void
9306 process_mips_fpe_exception (int mask)
9307 {
9308 if (mask)
9309 {
9310 int first = 1;
9311 if (mask & OEX_FPU_INEX)
9312 fputs ("INEX", stdout), first = 0;
9313 if (mask & OEX_FPU_UFLO)
9314 printf ("%sUFLO", first ? "" : "|"), first = 0;
9315 if (mask & OEX_FPU_OFLO)
9316 printf ("%sOFLO", first ? "" : "|"), first = 0;
9317 if (mask & OEX_FPU_DIV0)
9318 printf ("%sDIV0", first ? "" : "|"), first = 0;
9319 if (mask & OEX_FPU_INVAL)
9320 printf ("%sINVAL", first ? "" : "|");
9321 }
9322 else
9323 fputs ("0", stdout);
9324 }
9325
9326 static int
9327 process_mips_specific (FILE *file)
9328 {
9329 Elf_Internal_Dyn *entry;
9330 size_t liblist_offset = 0;
9331 size_t liblistno = 0;
9332 size_t conflictsno = 0;
9333 size_t options_offset = 0;
9334 size_t conflicts_offset = 0;
9335
9336 /* We have a lot of special sections. Thanks SGI! */
9337 if (dynamic_segment == NULL)
9338 /* No information available. */
9339 return 0;
9340
9341 for (entry = dynamic_segment; entry->d_tag != DT_NULL; ++entry)
9342 switch (entry->d_tag)
9343 {
9344 case DT_MIPS_LIBLIST:
9345 liblist_offset
9346 = offset_from_vma (file, entry->d_un.d_val,
9347 liblistno * sizeof (Elf32_External_Lib));
9348 break;
9349 case DT_MIPS_LIBLISTNO:
9350 liblistno = entry->d_un.d_val;
9351 break;
9352 case DT_MIPS_OPTIONS:
9353 options_offset = offset_from_vma (file, entry->d_un.d_val, 0);
9354 break;
9355 case DT_MIPS_CONFLICT:
9356 conflicts_offset
9357 = offset_from_vma (file, entry->d_un.d_val,
9358 conflictsno * sizeof (Elf32_External_Conflict));
9359 break;
9360 case DT_MIPS_CONFLICTNO:
9361 conflictsno = entry->d_un.d_val;
9362 break;
9363 default:
9364 break;
9365 }
9366
9367 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
9368 {
9369 Elf32_External_Lib *elib;
9370 size_t cnt;
9371
9372 elib = get_data (NULL, file, liblist_offset,
9373 liblistno * sizeof (Elf32_External_Lib),
9374 _("liblist"));
9375 if (elib)
9376 {
9377 printf ("\nSection '.liblist' contains %lu entries:\n",
9378 (unsigned long) liblistno);
9379 fputs (" Library Time Stamp Checksum Version Flags\n",
9380 stdout);
9381
9382 for (cnt = 0; cnt < liblistno; ++cnt)
9383 {
9384 Elf32_Lib liblist;
9385 time_t time;
9386 char timebuf[20];
9387 struct tm *tmp;
9388
9389 liblist.l_name = BYTE_GET (elib[cnt].l_name);
9390 time = BYTE_GET (elib[cnt].l_time_stamp);
9391 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
9392 liblist.l_version = BYTE_GET (elib[cnt].l_version);
9393 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
9394
9395 tmp = gmtime (&time);
9396 sprintf (timebuf, "%04u-%02u-%02uT%02u:%02u:%02u",
9397 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9398 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9399
9400 printf ("%3lu: ", (unsigned long) cnt);
9401 print_symbol (20, dynamic_strings + liblist.l_name);
9402 printf (" %s %#10lx %-7ld", timebuf, liblist.l_checksum,
9403 liblist.l_version);
9404
9405 if (liblist.l_flags == 0)
9406 puts (" NONE");
9407 else
9408 {
9409 static const struct
9410 {
9411 const char *name;
9412 int bit;
9413 }
9414 l_flags_vals[] =
9415 {
9416 { " EXACT_MATCH", LL_EXACT_MATCH },
9417 { " IGNORE_INT_VER", LL_IGNORE_INT_VER },
9418 { " REQUIRE_MINOR", LL_REQUIRE_MINOR },
9419 { " EXPORTS", LL_EXPORTS },
9420 { " DELAY_LOAD", LL_DELAY_LOAD },
9421 { " DELTA", LL_DELTA }
9422 };
9423 int flags = liblist.l_flags;
9424 size_t fcnt;
9425
9426 for (fcnt = 0;
9427 fcnt < sizeof (l_flags_vals) / sizeof (l_flags_vals[0]);
9428 ++fcnt)
9429 if ((flags & l_flags_vals[fcnt].bit) != 0)
9430 {
9431 fputs (l_flags_vals[fcnt].name, stdout);
9432 flags ^= l_flags_vals[fcnt].bit;
9433 }
9434 if (flags != 0)
9435 printf (" %#x", (unsigned int) flags);
9436
9437 puts ("");
9438 }
9439 }
9440
9441 free (elib);
9442 }
9443 }
9444
9445 if (options_offset != 0)
9446 {
9447 Elf_External_Options *eopt;
9448 Elf_Internal_Shdr *sect = section_headers;
9449 Elf_Internal_Options *iopt;
9450 Elf_Internal_Options *option;
9451 size_t offset;
9452 int cnt;
9453
9454 /* Find the section header so that we get the size. */
9455 while (sect->sh_type != SHT_MIPS_OPTIONS)
9456 ++sect;
9457
9458 eopt = get_data (NULL, file, options_offset, sect->sh_size,
9459 _("options"));
9460 if (eopt)
9461 {
9462 iopt = malloc ((sect->sh_size / sizeof (eopt)) * sizeof (*iopt));
9463 if (iopt == NULL)
9464 {
9465 error (_("Out of memory"));
9466 return 0;
9467 }
9468
9469 offset = cnt = 0;
9470 option = iopt;
9471
9472 while (offset < sect->sh_size)
9473 {
9474 Elf_External_Options *eoption;
9475
9476 eoption = (Elf_External_Options *) ((char *) eopt + offset);
9477
9478 option->kind = BYTE_GET (eoption->kind);
9479 option->size = BYTE_GET (eoption->size);
9480 option->section = BYTE_GET (eoption->section);
9481 option->info = BYTE_GET (eoption->info);
9482
9483 offset += option->size;
9484
9485 ++option;
9486 ++cnt;
9487 }
9488
9489 printf (_("\nSection '%s' contains %d entries:\n"),
9490 SECTION_NAME (sect), cnt);
9491
9492 option = iopt;
9493
9494 while (cnt-- > 0)
9495 {
9496 size_t len;
9497
9498 switch (option->kind)
9499 {
9500 case ODK_NULL:
9501 /* This shouldn't happen. */
9502 printf (" NULL %d %lx", option->section, option->info);
9503 break;
9504 case ODK_REGINFO:
9505 printf (" REGINFO ");
9506 if (elf_header.e_machine == EM_MIPS)
9507 {
9508 /* 32bit form. */
9509 Elf32_External_RegInfo *ereg;
9510 Elf32_RegInfo reginfo;
9511
9512 ereg = (Elf32_External_RegInfo *) (option + 1);
9513 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9514 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9515 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9516 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9517 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9518 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
9519
9520 printf ("GPR %08lx GP 0x%lx\n",
9521 reginfo.ri_gprmask,
9522 (unsigned long) reginfo.ri_gp_value);
9523 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
9524 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9525 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9526 }
9527 else
9528 {
9529 /* 64 bit form. */
9530 Elf64_External_RegInfo *ereg;
9531 Elf64_Internal_RegInfo reginfo;
9532
9533 ereg = (Elf64_External_RegInfo *) (option + 1);
9534 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
9535 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
9536 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
9537 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
9538 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
9539 reginfo.ri_gp_value = BYTE_GET8 (ereg->ri_gp_value);
9540
9541 printf ("GPR %08lx GP 0x",
9542 reginfo.ri_gprmask);
9543 printf_vma (reginfo.ri_gp_value);
9544 printf ("\n");
9545
9546 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
9547 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
9548 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
9549 }
9550 ++option;
9551 continue;
9552 case ODK_EXCEPTIONS:
9553 fputs (" EXCEPTIONS fpe_min(", stdout);
9554 process_mips_fpe_exception (option->info & OEX_FPU_MIN);
9555 fputs (") fpe_max(", stdout);
9556 process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
9557 fputs (")", stdout);
9558
9559 if (option->info & OEX_PAGE0)
9560 fputs (" PAGE0", stdout);
9561 if (option->info & OEX_SMM)
9562 fputs (" SMM", stdout);
9563 if (option->info & OEX_FPDBUG)
9564 fputs (" FPDBUG", stdout);
9565 if (option->info & OEX_DISMISS)
9566 fputs (" DISMISS", stdout);
9567 break;
9568 case ODK_PAD:
9569 fputs (" PAD ", stdout);
9570 if (option->info & OPAD_PREFIX)
9571 fputs (" PREFIX", stdout);
9572 if (option->info & OPAD_POSTFIX)
9573 fputs (" POSTFIX", stdout);
9574 if (option->info & OPAD_SYMBOL)
9575 fputs (" SYMBOL", stdout);
9576 break;
9577 case ODK_HWPATCH:
9578 fputs (" HWPATCH ", stdout);
9579 if (option->info & OHW_R4KEOP)
9580 fputs (" R4KEOP", stdout);
9581 if (option->info & OHW_R8KPFETCH)
9582 fputs (" R8KPFETCH", stdout);
9583 if (option->info & OHW_R5KEOP)
9584 fputs (" R5KEOP", stdout);
9585 if (option->info & OHW_R5KCVTL)
9586 fputs (" R5KCVTL", stdout);
9587 break;
9588 case ODK_FILL:
9589 fputs (" FILL ", stdout);
9590 /* XXX Print content of info word? */
9591 break;
9592 case ODK_TAGS:
9593 fputs (" TAGS ", stdout);
9594 /* XXX Print content of info word? */
9595 break;
9596 case ODK_HWAND:
9597 fputs (" HWAND ", stdout);
9598 if (option->info & OHWA0_R4KEOP_CHECKED)
9599 fputs (" R4KEOP_CHECKED", stdout);
9600 if (option->info & OHWA0_R4KEOP_CLEAN)
9601 fputs (" R4KEOP_CLEAN", stdout);
9602 break;
9603 case ODK_HWOR:
9604 fputs (" HWOR ", stdout);
9605 if (option->info & OHWA0_R4KEOP_CHECKED)
9606 fputs (" R4KEOP_CHECKED", stdout);
9607 if (option->info & OHWA0_R4KEOP_CLEAN)
9608 fputs (" R4KEOP_CLEAN", stdout);
9609 break;
9610 case ODK_GP_GROUP:
9611 printf (" GP_GROUP %#06lx self-contained %#06lx",
9612 option->info & OGP_GROUP,
9613 (option->info & OGP_SELF) >> 16);
9614 break;
9615 case ODK_IDENT:
9616 printf (" IDENT %#06lx self-contained %#06lx",
9617 option->info & OGP_GROUP,
9618 (option->info & OGP_SELF) >> 16);
9619 break;
9620 default:
9621 /* This shouldn't happen. */
9622 printf (" %3d ??? %d %lx",
9623 option->kind, option->section, option->info);
9624 break;
9625 }
9626
9627 len = sizeof (*eopt);
9628 while (len < option->size)
9629 if (((char *) option)[len] >= ' '
9630 && ((char *) option)[len] < 0x7f)
9631 printf ("%c", ((char *) option)[len++]);
9632 else
9633 printf ("\\%03o", ((char *) option)[len++]);
9634
9635 fputs ("\n", stdout);
9636 ++option;
9637 }
9638
9639 free (eopt);
9640 }
9641 }
9642
9643 if (conflicts_offset != 0 && conflictsno != 0)
9644 {
9645 Elf32_Conflict *iconf;
9646 size_t cnt;
9647
9648 if (dynamic_symbols == NULL)
9649 {
9650 error (_("conflict list found without a dynamic symbol table"));
9651 return 0;
9652 }
9653
9654 iconf = malloc (conflictsno * sizeof (*iconf));
9655 if (iconf == NULL)
9656 {
9657 error (_("Out of memory"));
9658 return 0;
9659 }
9660
9661 if (is_32bit_elf)
9662 {
9663 Elf32_External_Conflict *econf32;
9664
9665 econf32 = get_data (NULL, file, conflicts_offset,
9666 conflictsno * sizeof (*econf32), _("conflict"));
9667 if (!econf32)
9668 return 0;
9669
9670 for (cnt = 0; cnt < conflictsno; ++cnt)
9671 iconf[cnt] = BYTE_GET (econf32[cnt]);
9672
9673 free (econf32);
9674 }
9675 else
9676 {
9677 Elf64_External_Conflict *econf64;
9678
9679 econf64 = get_data (NULL, file, conflicts_offset,
9680 conflictsno * sizeof (*econf64), _("conflict"));
9681 if (!econf64)
9682 return 0;
9683
9684 for (cnt = 0; cnt < conflictsno; ++cnt)
9685 iconf[cnt] = BYTE_GET (econf64[cnt]);
9686
9687 free (econf64);
9688 }
9689
9690 printf (_("\nSection '.conflict' contains %ld entries:\n"),
9691 (long) conflictsno);
9692 puts (_(" Num: Index Value Name"));
9693
9694 for (cnt = 0; cnt < conflictsno; ++cnt)
9695 {
9696 Elf_Internal_Sym *psym = & dynamic_symbols[iconf[cnt]];
9697
9698 printf ("%5lu: %8lu ", (unsigned long) cnt, iconf[cnt]);
9699 print_vma (psym->st_value, FULL_HEX);
9700 putchar (' ');
9701 print_symbol (25, dynamic_strings + psym->st_name);
9702 putchar ('\n');
9703 }
9704
9705 free (iconf);
9706 }
9707
9708 return 1;
9709 }
9710
9711 static int
9712 process_gnu_liblist (FILE *file)
9713 {
9714 Elf_Internal_Shdr *section, *string_sec;
9715 Elf32_External_Lib *elib;
9716 char *strtab;
9717 size_t cnt;
9718 unsigned i;
9719
9720 if (! do_arch)
9721 return 0;
9722
9723 for (i = 0, section = section_headers;
9724 i < elf_header.e_shnum;
9725 i++, section++)
9726 {
9727 switch (section->sh_type)
9728 {
9729 case SHT_GNU_LIBLIST:
9730 elib = get_data (NULL, file, section->sh_offset, section->sh_size,
9731 _("liblist"));
9732
9733 if (elib == NULL)
9734 break;
9735 string_sec = SECTION_HEADER (section->sh_link);
9736
9737 strtab = get_data (NULL, file, string_sec->sh_offset,
9738 string_sec->sh_size, _("liblist string table"));
9739
9740 if (strtab == NULL
9741 || section->sh_entsize != sizeof (Elf32_External_Lib))
9742 {
9743 free (elib);
9744 break;
9745 }
9746
9747 printf (_("\nLibrary list section '%s' contains %lu entries:\n"),
9748 SECTION_NAME (section),
9749 (long) (section->sh_size / sizeof (Elf32_External_Lib)));
9750
9751 puts (" Library Time Stamp Checksum Version Flags");
9752
9753 for (cnt = 0; cnt < section->sh_size / sizeof (Elf32_External_Lib);
9754 ++cnt)
9755 {
9756 Elf32_Lib liblist;
9757 time_t time;
9758 char timebuf[20];
9759 struct tm *tmp;
9760
9761 liblist.l_name = BYTE_GET (elib[cnt].l_name);
9762 time = BYTE_GET (elib[cnt].l_time_stamp);
9763 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
9764 liblist.l_version = BYTE_GET (elib[cnt].l_version);
9765 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
9766
9767 tmp = gmtime (&time);
9768 sprintf (timebuf, "%04u-%02u-%02uT%02u:%02u:%02u",
9769 tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
9770 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
9771
9772 printf ("%3lu: ", (unsigned long) cnt);
9773 if (do_wide)
9774 printf ("%-20s", strtab + liblist.l_name);
9775 else
9776 printf ("%-20.20s", strtab + liblist.l_name);
9777 printf (" %s %#010lx %-7ld %-7ld\n", timebuf, liblist.l_checksum,
9778 liblist.l_version, liblist.l_flags);
9779 }
9780
9781 free (elib);
9782 }
9783 }
9784
9785 return 1;
9786 }
9787
9788 static const char *
9789 get_note_type (unsigned e_type)
9790 {
9791 static char buff[64];
9792
9793 switch (e_type)
9794 {
9795 case NT_PRSTATUS: return _("NT_PRSTATUS (prstatus structure)");
9796 case NT_FPREGSET: return _("NT_FPREGSET (floating point registers)");
9797 case NT_PRPSINFO: return _("NT_PRPSINFO (prpsinfo structure)");
9798 case NT_TASKSTRUCT: return _("NT_TASKSTRUCT (task structure)");
9799 case NT_PRXFPREG: return _("NT_PRXFPREG (user_xfpregs structure)");
9800 case NT_PSTATUS: return _("NT_PSTATUS (pstatus structure)");
9801 case NT_FPREGS: return _("NT_FPREGS (floating point registers)");
9802 case NT_PSINFO: return _("NT_PSINFO (psinfo structure)");
9803 case NT_LWPSTATUS: return _("NT_LWPSTATUS (lwpstatus_t structure)");
9804 case NT_LWPSINFO: return _("NT_LWPSINFO (lwpsinfo_t structure)");
9805 case NT_WIN32PSTATUS: return _("NT_WIN32PSTATUS (win32_pstatus structure)");
9806 default:
9807 sprintf (buff, _("Unknown note type: (0x%08x)"), e_type);
9808 return buff;
9809 }
9810 }
9811
9812 static const char *
9813 get_netbsd_elfcore_note_type (unsigned e_type)
9814 {
9815 static char buff[64];
9816
9817 if (e_type == NT_NETBSDCORE_PROCINFO)
9818 {
9819 /* NetBSD core "procinfo" structure. */
9820 return _("NetBSD procinfo structure");
9821 }
9822
9823 /* As of Jan 2002 there are no other machine-independent notes
9824 defined for NetBSD core files. If the note type is less
9825 than the start of the machine-dependent note types, we don't
9826 understand it. */
9827
9828 if (e_type < NT_NETBSDCORE_FIRSTMACH)
9829 {
9830 sprintf (buff, _("Unknown note type: (0x%08x)"), e_type);
9831 return buff;
9832 }
9833
9834 switch (elf_header.e_machine)
9835 {
9836 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0
9837 and PT_GETFPREGS == mach+2. */
9838
9839 case EM_OLD_ALPHA:
9840 case EM_ALPHA:
9841 case EM_SPARC:
9842 case EM_SPARC32PLUS:
9843 case EM_SPARCV9:
9844 switch (e_type)
9845 {
9846 case NT_NETBSDCORE_FIRSTMACH+0:
9847 return _("PT_GETREGS (reg structure)");
9848 case NT_NETBSDCORE_FIRSTMACH+2:
9849 return _("PT_GETFPREGS (fpreg structure)");
9850 default:
9851 break;
9852 }
9853 break;
9854
9855 /* On all other arch's, PT_GETREGS == mach+1 and
9856 PT_GETFPREGS == mach+3. */
9857 default:
9858 switch (e_type)
9859 {
9860 case NT_NETBSDCORE_FIRSTMACH+1:
9861 return _("PT_GETREGS (reg structure)");
9862 case NT_NETBSDCORE_FIRSTMACH+3:
9863 return _("PT_GETFPREGS (fpreg structure)");
9864 default:
9865 break;
9866 }
9867 }
9868
9869 sprintf (buff, _("PT_FIRSTMACH+%d"), e_type - NT_NETBSDCORE_FIRSTMACH);
9870 return buff;
9871 }
9872
9873 /* Note that by the ELF standard, the name field is already null byte
9874 terminated, and namesz includes the terminating null byte.
9875 I.E. the value of namesz for the name "FSF" is 4.
9876
9877 If the value of namesz is zero, there is no name present. */
9878 static int
9879 process_note (Elf_Internal_Note *pnote)
9880 {
9881 const char *nt;
9882
9883 if (pnote->namesz == 0)
9884 {
9885 /* If there is no note name, then use the default set of
9886 note type strings. */
9887 nt = get_note_type (pnote->type);
9888 }
9889 else if (strncmp (pnote->namedata, "NetBSD-CORE", 11) == 0)
9890 {
9891 /* NetBSD-specific core file notes. */
9892 nt = get_netbsd_elfcore_note_type (pnote->type);
9893 }
9894 else
9895 {
9896 /* Don't recognize this note name; just use the default set of
9897 note type strings. */
9898 nt = get_note_type (pnote->type);
9899 }
9900
9901 printf (" %s\t\t0x%08lx\t%s\n",
9902 pnote->namesz ? pnote->namedata : "(NONE)",
9903 pnote->descsz, nt);
9904 return 1;
9905 }
9906
9907
9908 static int
9909 process_corefile_note_segment (FILE *file, bfd_vma offset, bfd_vma length)
9910 {
9911 Elf_External_Note *pnotes;
9912 Elf_External_Note *external;
9913 int res = 1;
9914
9915 if (length <= 0)
9916 return 0;
9917
9918 pnotes = get_data (NULL, file, offset, length, _("notes"));
9919 if (!pnotes)
9920 return 0;
9921
9922 external = pnotes;
9923
9924 printf (_("\nNotes at offset 0x%08lx with length 0x%08lx:\n"),
9925 (unsigned long) offset, (unsigned long) length);
9926 printf (_(" Owner\t\tData size\tDescription\n"));
9927
9928 while (external < (Elf_External_Note *)((char *) pnotes + length))
9929 {
9930 Elf_External_Note *next;
9931 Elf_Internal_Note inote;
9932 char *temp = NULL;
9933
9934 inote.type = BYTE_GET (external->type);
9935 inote.namesz = BYTE_GET (external->namesz);
9936 inote.namedata = external->name;
9937 inote.descsz = BYTE_GET (external->descsz);
9938 inote.descdata = inote.namedata + align_power (inote.namesz, 2);
9939 inote.descpos = offset + (inote.descdata - (char *) pnotes);
9940
9941 next = (Elf_External_Note *)(inote.descdata + align_power (inote.descsz, 2));
9942
9943 if (((char *) next) > (((char *) pnotes) + length))
9944 {
9945 warn (_("corrupt note found at offset %x into core notes\n"),
9946 ((char *) external) - ((char *) pnotes));
9947 warn (_(" type: %x, namesize: %08lx, descsize: %08lx\n"),
9948 inote.type, inote.namesz, inote.descsz);
9949 break;
9950 }
9951
9952 external = next;
9953
9954 /* Verify that name is null terminated. It appears that at least
9955 one version of Linux (RedHat 6.0) generates corefiles that don't
9956 comply with the ELF spec by failing to include the null byte in
9957 namesz. */
9958 if (inote.namedata[inote.namesz] != '\0')
9959 {
9960 temp = malloc (inote.namesz + 1);
9961
9962 if (temp == NULL)
9963 {
9964 error (_("Out of memory\n"));
9965 res = 0;
9966 break;
9967 }
9968
9969 strncpy (temp, inote.namedata, inote.namesz);
9970 temp[inote.namesz] = 0;
9971
9972 /* warn (_("'%s' NOTE name not properly null terminated\n"), temp); */
9973 inote.namedata = temp;
9974 }
9975
9976 res &= process_note (& inote);
9977
9978 if (temp != NULL)
9979 {
9980 free (temp);
9981 temp = NULL;
9982 }
9983 }
9984
9985 free (pnotes);
9986
9987 return res;
9988 }
9989
9990 static int
9991 process_corefile_note_segments (FILE *file)
9992 {
9993 Elf_Internal_Phdr *segment;
9994 unsigned int i;
9995 int res = 1;
9996
9997 if (! get_program_headers (file))
9998 return 0;
9999
10000 for (i = 0, segment = program_headers;
10001 i < elf_header.e_phnum;
10002 i++, segment++)
10003 {
10004 if (segment->p_type == PT_NOTE)
10005 res &= process_corefile_note_segment (file,
10006 (bfd_vma) segment->p_offset,
10007 (bfd_vma) segment->p_filesz);
10008 }
10009
10010 return res;
10011 }
10012
10013 static int
10014 process_corefile_contents (FILE *file)
10015 {
10016 /* If we have not been asked to display the notes then do nothing. */
10017 if (! do_notes)
10018 return 1;
10019
10020 /* If file is not a core file then exit. */
10021 if (elf_header.e_type != ET_CORE)
10022 return 1;
10023
10024 /* No program headers means no NOTE segment. */
10025 if (elf_header.e_phnum == 0)
10026 {
10027 printf (_("No note segments present in the core file.\n"));
10028 return 1;
10029 }
10030
10031 return process_corefile_note_segments (file);
10032 }
10033
10034 static int
10035 process_arch_specific (FILE *file)
10036 {
10037 if (! do_arch)
10038 return 1;
10039
10040 switch (elf_header.e_machine)
10041 {
10042 case EM_MIPS:
10043 case EM_MIPS_RS3_LE:
10044 return process_mips_specific (file);
10045 break;
10046 default:
10047 break;
10048 }
10049 return 1;
10050 }
10051
10052 static int
10053 get_file_header (FILE *file)
10054 {
10055 /* Read in the identity array. */
10056 if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
10057 return 0;
10058
10059 /* Determine how to read the rest of the header. */
10060 switch (elf_header.e_ident[EI_DATA])
10061 {
10062 default: /* fall through */
10063 case ELFDATANONE: /* fall through */
10064 case ELFDATA2LSB:
10065 byte_get = byte_get_little_endian;
10066 byte_put = byte_put_little_endian;
10067 break;
10068 case ELFDATA2MSB:
10069 byte_get = byte_get_big_endian;
10070 byte_put = byte_put_big_endian;
10071 break;
10072 }
10073
10074 /* For now we only support 32 bit and 64 bit ELF files. */
10075 is_32bit_elf = (elf_header.e_ident[EI_CLASS] != ELFCLASS64);
10076
10077 /* Read in the rest of the header. */
10078 if (is_32bit_elf)
10079 {
10080 Elf32_External_Ehdr ehdr32;
10081
10082 if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT, 1, file) != 1)
10083 return 0;
10084
10085 elf_header.e_type = BYTE_GET (ehdr32.e_type);
10086 elf_header.e_machine = BYTE_GET (ehdr32.e_machine);
10087 elf_header.e_version = BYTE_GET (ehdr32.e_version);
10088 elf_header.e_entry = BYTE_GET (ehdr32.e_entry);
10089 elf_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
10090 elf_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
10091 elf_header.e_flags = BYTE_GET (ehdr32.e_flags);
10092 elf_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
10093 elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
10094 elf_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
10095 elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
10096 elf_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
10097 elf_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
10098 }
10099 else
10100 {
10101 Elf64_External_Ehdr ehdr64;
10102
10103 /* If we have been compiled with sizeof (bfd_vma) == 4, then
10104 we will not be able to cope with the 64bit data found in
10105 64 ELF files. Detect this now and abort before we start
10106 overwritting things. */
10107 if (sizeof (bfd_vma) < 8)
10108 {
10109 error (_("This instance of readelf has been built without support for a\n\
10110 64 bit data type and so it cannot read 64 bit ELF files.\n"));
10111 return 0;
10112 }
10113
10114 if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT, 1, file) != 1)
10115 return 0;
10116
10117 elf_header.e_type = BYTE_GET (ehdr64.e_type);
10118 elf_header.e_machine = BYTE_GET (ehdr64.e_machine);
10119 elf_header.e_version = BYTE_GET (ehdr64.e_version);
10120 elf_header.e_entry = BYTE_GET8 (ehdr64.e_entry);
10121 elf_header.e_phoff = BYTE_GET8 (ehdr64.e_phoff);
10122 elf_header.e_shoff = BYTE_GET8 (ehdr64.e_shoff);
10123 elf_header.e_flags = BYTE_GET (ehdr64.e_flags);
10124 elf_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
10125 elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
10126 elf_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
10127 elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
10128 elf_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
10129 elf_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
10130 }
10131
10132 if (elf_header.e_shoff)
10133 {
10134 /* There may be some extensions in the first section header. Don't
10135 bomb if we can't read it. */
10136 if (is_32bit_elf)
10137 get_32bit_section_headers (file, 1);
10138 else
10139 get_64bit_section_headers (file, 1);
10140 }
10141
10142 return 1;
10143 }
10144
10145 static int
10146 process_file (char *file_name)
10147 {
10148 FILE *file;
10149 struct stat statbuf;
10150 unsigned int i;
10151
10152 if (stat (file_name, & statbuf) < 0)
10153 {
10154 error (_("Cannot stat input file %s.\n"), file_name);
10155 return 1;
10156 }
10157
10158 file = fopen (file_name, "rb");
10159 if (file == NULL)
10160 {
10161 error (_("Input file %s not found.\n"), file_name);
10162 return 1;
10163 }
10164
10165 if (! get_file_header (file))
10166 {
10167 error (_("%s: Failed to read file header\n"), file_name);
10168 fclose (file);
10169 return 1;
10170 }
10171
10172 /* Initialise per file variables. */
10173 for (i = NUM_ELEM (version_info); i--;)
10174 version_info[i] = 0;
10175
10176 for (i = NUM_ELEM (dynamic_info); i--;)
10177 dynamic_info[i] = 0;
10178
10179 /* Process the file. */
10180 if (show_name)
10181 printf (_("\nFile: %s\n"), file_name);
10182
10183 if (! process_file_header ())
10184 {
10185 fclose (file);
10186 return 1;
10187 }
10188
10189 if (! process_section_headers (file))
10190 {
10191 /* Without loaded section headers we
10192 cannot process lots of things. */
10193 do_unwind = do_version = do_dump = do_arch = 0;
10194
10195 if (! do_using_dynamic)
10196 do_syms = do_reloc = 0;
10197 }
10198
10199 if (process_program_headers (file))
10200 process_dynamic_segment (file);
10201
10202 process_relocs (file);
10203
10204 process_unwind (file);
10205
10206 process_symbol_table (file);
10207
10208 process_syminfo (file);
10209
10210 process_version_sections (file);
10211
10212 process_section_contents (file);
10213
10214 process_corefile_contents (file);
10215
10216 process_gnu_liblist (file);
10217
10218 process_arch_specific (file);
10219
10220 fclose (file);
10221
10222 if (program_headers)
10223 {
10224 free (program_headers);
10225 program_headers = NULL;
10226 }
10227
10228 if (section_headers)
10229 {
10230 free (section_headers);
10231 section_headers = NULL;
10232 }
10233
10234 if (string_table)
10235 {
10236 free (string_table);
10237 string_table = NULL;
10238 string_table_length = 0;
10239 }
10240
10241 if (dynamic_strings)
10242 {
10243 free (dynamic_strings);
10244 dynamic_strings = NULL;
10245 }
10246
10247 if (dynamic_symbols)
10248 {
10249 free (dynamic_symbols);
10250 dynamic_symbols = NULL;
10251 num_dynamic_syms = 0;
10252 }
10253
10254 if (dynamic_syminfo)
10255 {
10256 free (dynamic_syminfo);
10257 dynamic_syminfo = NULL;
10258 }
10259
10260 return 0;
10261 }
10262
10263 #ifdef SUPPORT_DISASSEMBLY
10264 /* Needed by the i386 disassembler. For extra credit, someone could
10265 fix this so that we insert symbolic addresses here, esp for GOT/PLT
10266 symbols. */
10267
10268 void
10269 print_address (unsigned int addr, FILE *outfile)
10270 {
10271 fprintf (outfile,"0x%8.8x", addr);
10272 }
10273
10274 /* Needed by the i386 disassembler. */
10275 void
10276 db_task_printsym (unsigned int addr)
10277 {
10278 print_address (addr, stderr);
10279 }
10280 #endif
10281
10282 int
10283 main (int argc, char **argv)
10284 {
10285 int err;
10286 char *cmdline_dump_sects = NULL;
10287 unsigned num_cmdline_dump_sects = 0;
10288
10289 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
10290 setlocale (LC_MESSAGES, "");
10291 #endif
10292 #if defined (HAVE_SETLOCALE)
10293 setlocale (LC_CTYPE, "");
10294 #endif
10295 bindtextdomain (PACKAGE, LOCALEDIR);
10296 textdomain (PACKAGE);
10297
10298 parse_args (argc, argv);
10299
10300 if (optind < (argc - 1))
10301 show_name = 1;
10302
10303 /* When processing more than one file remember the dump requests
10304 issued on command line to reset them after each file. */
10305 if (optind + 1 < argc && dump_sects != NULL)
10306 {
10307 cmdline_dump_sects = malloc (num_dump_sects);
10308 if (cmdline_dump_sects == NULL)
10309 error (_("Out of memory allocating dump request table."));
10310 else
10311 {
10312 memcpy (cmdline_dump_sects, dump_sects, num_dump_sects);
10313 num_cmdline_dump_sects = num_dump_sects;
10314 }
10315 }
10316
10317 err = 0;
10318 while (optind < argc)
10319 {
10320 err |= process_file (argv[optind++]);
10321
10322 /* Reset dump requests. */
10323 if (optind < argc && dump_sects != NULL)
10324 {
10325 num_dump_sects = num_cmdline_dump_sects;
10326 if (num_cmdline_dump_sects > 0)
10327 memcpy (dump_sects, cmdline_dump_sects, num_cmdline_dump_sects);
10328 }
10329 }
10330
10331 if (dump_sects != NULL)
10332 free (dump_sects);
10333 if (cmdline_dump_sects != NULL)
10334 free (cmdline_dump_sects);
10335
10336 return err;
10337 }
This page took 0.375266 seconds and 5 git commands to generate.