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