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