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