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