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