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