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