Remove definition of functions to return relocation symbol strings.
[binutils-gdb.git] / binutils / readelf.c
1 /* readelf.c -- display contents of an ELF format file
2 Copyright (C) 1998 Free Software Foundation, Inc.
3
4 Originally developed by Eric Youngdale <eric@andante.jic.com>
5 Modifications by Nick Clifton <nickc@cygnus.com>
6
7 This file is part of GNU Binutils.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
23 \f
24
25 #include <assert.h>
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #include <time.h>
29
30 #include "bfd.h"
31 #include "elf/common.h"
32 #include "elf/i386.h"
33 #include "elf/v850.h"
34 #include "elf/external.h"
35 #include "elf/internal.h"
36
37 #include "bucomm.h"
38 #include "getopt.h"
39
40 #ifdef ANSI_PROTOTYPES
41 #include <stdarg.h>
42 #else
43 #include <varargs.h>
44 #endif
45
46 char * program_name = "readelf";
47 unsigned int dynamic_addr;
48 unsigned int dynamic_size;
49 unsigned int rela_addr;
50 unsigned int rela_size;
51 char * dynamic_strings;
52 char * string_table;
53 Elf_Internal_Sym * dynamic_symbols;
54 char program_interpreter [64];
55 int dynamic_info [DT_JMPREL + 1];
56 int version_info [16];
57 int loadaddr = 0;
58 Elf_Internal_Ehdr elf_header;
59 Elf_Internal_Shdr * section_headers;
60 Elf_Internal_Dyn * dynamic_segment;
61 int show_name;
62 int do_dynamic;
63 int do_syms;
64 int do_reloc;
65 int do_sections;
66 int do_segments;
67 int do_using_dynamic;
68 int do_header;
69 int do_dump;
70 int do_version;
71
72 static unsigned long int (* byte_get) PARAMS ((unsigned char *, int));
73
74 #define NUM_DUMP_SECTS 100
75 char dump_sects [NUM_DUMP_SECTS];
76
77 #define HEX_DUMP 1
78 #define DISASS_DUMP 2
79
80 /* Forward declarations for dumb compilers. */
81 static const char * get_mips_dynamic_type PARAMS ((unsigned long type));
82 static const char * get_dynamic_type PARAMS ((unsigned long type));
83 static const char * elf_i386_reloc_type PARAMS ((unsigned long rtype));
84 static const char * elf_m68k_reloc_type PARAMS ((unsigned long rtype));
85 static const char * elf_sparc_reloc_type PARAMS ((unsigned long rtype));
86 static const char * elf_m32r_reloc_type PARAMS ((unsigned long rtype));
87 static const char * elf_v850_reloc_type PARAMS ((unsigned long rtype));
88 static const char * elf_d10v_reloc_type PARAMS ((unsigned long rtype));
89 /* start-sanitize-d30v */
90 static const char * elf_d30v_reloc_type PARAMS ((unsigned long rtype));
91 /* end-sanitize-d30v */
92 static const char * elf_sh_reloc_type PARAMS ((unsigned long rtype));
93 static const char * elf_mn10300_reloc_type PARAMS ((unsigned long rtype));
94 static const char * elf_mn10200_reloc_type PARAMS ((unsigned long rtype));
95 static const char * elf_ppc_reloc_type PARAMS ((unsigned long rtype));
96 static const char * elf_mips_reloc_type PARAMS ((unsigned long rtype));
97 static const char * elf_alpha_reloc_type PARAMS ((unsigned long rtype));
98 static const char * elf_arm_reloc_type PARAMS ((unsigned long rtype));
99 static int dump_relocations
100 PARAMS ((FILE *, unsigned long, unsigned long, Elf_Internal_Sym *, char *));
101 static char * get_file_type PARAMS ((unsigned e_type));
102 static char * get_machine_name PARAMS ((unsigned e_machine));
103 static char * get_machine_data PARAMS ((unsigned e_data));
104 static char * get_machine_flags PARAMS ((unsigned, unsigned e_machine));
105 static const char * get_mips_segment_type PARAMS ((unsigned long type));
106 static const char * get_segment_type PARAMS ((unsigned long p_type));
107 static const char * get_mips_section_type_name PARAMS ((unsigned int sh_type));
108 static const char * get_section_type_name PARAMS ((unsigned int sh_type));
109 static char * get_symbol_binding PARAMS ((unsigned int binding));
110 static char * get_symbol_type PARAMS ((unsigned int type));
111 static void usage PARAMS ((void));
112 static void parse_args PARAMS ((int argc, char ** argv));
113 static int process_file_header PARAMS ((void));
114 static int process_program_headers PARAMS ((FILE *));
115 static int process_section_headers PARAMS ((FILE *));
116 static void dynamic_segment_mips_val PARAMS ((Elf_Internal_Dyn *entry));
117 static int process_dynamic_segment PARAMS ((FILE *));
118 static int process_symbol_table PARAMS ((FILE *));
119 static int process_section_contents PARAMS ((FILE *));
120 static void process_file PARAMS ((char * file_name));
121 static int process_relocs PARAMS ((FILE *));
122 static int process_version_sections PARAMS ((FILE *));
123 static char * get_ver_flags PARAMS ((unsigned int flags));
124 static char * get_symbol_index_type PARAMS ((unsigned int type));
125 static int get_section_headers PARAMS ((FILE * file));
126 static int get_file_header PARAMS ((FILE * file));
127 static Elf_Internal_Sym * get_elf_symbols
128 PARAMS ((FILE * file, unsigned long offset, unsigned long number));
129 static int * get_dynamic_data PARAMS ((FILE * file, unsigned int number));
130
131 typedef int Elf32_Word;
132
133 #define SECTION_NAME(X) (string_table + (X)->sh_name)
134
135 #define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
136
137 #define BYTE_GET(field) byte_get (field, sizeof (field))
138
139 #define NUM_ELEM(array) (sizeof (array) / sizeof ((array)[0]))
140
141 #define GET_DATA_ALLOC(offset, size, var, type, reason) \
142 if (fseek (file, offset, SEEK_SET)) \
143 { \
144 error (_("Unable to seek to start of %s at %x\n"), reason, offset); \
145 return 0; \
146 } \
147 \
148 var = (type) malloc (size); \
149 \
150 if (var == NULL) \
151 { \
152 error (_("Out of memory allocating %d bytes for %s\n"), size, reason); \
153 return 0; \
154 } \
155 \
156 if (fread (var, size, 1, file) != 1) \
157 { \
158 error (_("Unable to read in %d bytes of %s\n"), size, reason); \
159 free (var); \
160 var = NULL; \
161 return 0; \
162 }
163
164
165 #define GET_DATA(offset, var, reason) \
166 if (fseek (file, offset, SEEK_SET)) \
167 { \
168 error (_("Unable to seek to %x for %s\n"), offset, reason); \
169 return 0; \
170 } \
171 else if (fread (& var, sizeof (var), 1, file) != 1) \
172 { \
173 error (_("Unable to read data at %x for %s\n"), offset, reason); \
174 return 0; \
175 }
176
177 #ifdef ANSI_PROTOTYPES
178 static void
179 error (const char * message, ...)
180 {
181 va_list args;
182
183 fprintf (stderr, _("%s: Error: "), program_name);
184 va_start (args, message);
185 vfprintf (stderr, message, args);
186 va_end (args);
187 return;
188 }
189
190 static void
191 warn (const char * message, ...)
192 {
193 va_list args;
194
195 fprintf (stderr, _("%s: Warning: "), program_name);
196 va_start (args, message);
197 vfprintf (stderr, message, args);
198 va_end (args);
199 return;
200 }
201 #else
202 static void
203 error (va_alist)
204 va_dcl
205 {
206 char * message;
207 va_list args;
208
209 fprintf (stderr, _("%s: Error: "), program_name);
210 va_start (args);
211 message = va_arg (args, char *);
212 vfprintf (stderr, message, args);
213 va_end (args);
214 return;
215 }
216
217 static void
218 warn (va_alist)
219 va_dcl
220 {
221 char * message;
222 va_list args;
223
224 fprintf (stderr, _("%s: Warning: "), program_name);
225 va_start (args);
226 message = va_arg (args, char *);
227 vfprintf (stderr, message, args);
228 va_end (args);
229 return;
230 }
231 #endif
232
233 static unsigned long int
234 byte_get_little_endian (field, size)
235 unsigned char * field;
236 int size;
237 {
238 switch (size)
239 {
240 case 1:
241 return * field;
242
243 case 2:
244 return ((unsigned int) (field [0]))
245 | (((unsigned int) (field [1])) << 8);
246
247 case 4:
248 return ((unsigned long) (field [0]))
249 | (((unsigned long) (field [1])) << 8)
250 | (((unsigned long) (field [2])) << 16)
251 | (((unsigned long) (field [3])) << 24);
252
253 default:
254 error (_("Unhandled data length: %d\n"), size);
255 abort();
256 }
257 }
258
259 static unsigned long int
260 byte_get_big_endian (field, size)
261 unsigned char * field;
262 int size;
263 {
264 switch (size)
265 {
266 case 1:
267 return * field;
268
269 case 2:
270 return ((unsigned int) (field [1])) | (((int) (field [0])) << 8);
271
272 case 4:
273 return ((unsigned long) (field [3]))
274 | (((unsigned long) (field [2])) << 8)
275 | (((unsigned long) (field [1])) << 16)
276 | (((unsigned long) (field [0])) << 24);
277
278 default:
279 error (_("Unhandled data length: %d\n"), size);
280 abort();
281 }
282 }
283
284
285 /* Define the macros we need to define the relocation recognition functions. */
286 #define START_RELOC_NUMBERS(name) static const char * \
287 name (rtype) \
288 unsigned long rtype; \
289 { \
290 switch (rtype) {
291 #ifdef __STDC__
292 # define RELOC_NUMBER(name, number) case number: return #name;
293 #else
294 # define RELOC_NUMBER(name, number) case number: return "name";
295 #endif
296 #define END_RELOC_NUMBERS default: return NULL; \
297 } \
298 }
299
300 /* Define the i386 relocations. */
301 static const char *
302 elf_i386_reloc_type (rtype)
303 unsigned long rtype;
304 {
305 switch (rtype)
306 {
307 case R_386_NONE: return "R_386_NONE";
308 case R_386_32: return "R_386_32";
309 case R_386_PC32: return "R_386_PC32";
310 case R_386_GOT32: return "R_386_GOT32";
311 case R_386_PLT32: return "R_386_PLT32";
312 case R_386_COPY: return "R_386_COPY";
313 case R_386_GLOB_DAT: return "R_386_GLOB_DAT";
314 case R_386_JMP_SLOT: return "R_386_JMP_SLOT";
315 case R_386_RELATIVE: return "R_386_RELATIVE";
316 case R_386_GOTOFF: return "R_386_GOTOFF";
317 case R_386_GOTPC: return "R_386_GOTPC";
318 case R_386_16: return "R_386_16";
319 case R_386_PC16: return "R_386_PC16";
320 case R_386_PC8: return "R_386_PC8";
321 default: return NULL;
322 }
323 }
324
325 /* Define the m68k relocations. */
326 #include "elf/m68k.h"
327
328 /* Define the SPARC relocations. */
329 #include "elf/sparc.h"
330
331 /* Define the m32r relocations. */
332 #include "elf/m32r.h"
333
334
335 static const char *
336 elf_v850_reloc_type (rtype)
337 unsigned long rtype;
338 {
339 switch (rtype)
340 {
341 case R_V850_NONE: return "R_V850_NONE";
342 case R_V850_9_PCREL: return "R_V850_9_PCREL";
343 case R_V850_22_PCREL: return "R_V850_22_PCREL";
344 case R_V850_HI16_S: return "R_V850_HI16_S";
345 case R_V850_HI16: return "R_V850_HI16";
346 case R_V850_LO16: return "R_V850_LO16";
347 case R_V850_32: return "R_V850_32";
348 case R_V850_16: return "R_V850_16";
349 case R_V850_8: return "R_V850_8";
350 case R_V850_SDA_16_16_OFFSET: return "R_V850_SDA_16_16_OFFSET";
351 case R_V850_SDA_15_16_OFFSET: return "R_V850_SDA_15_16_OFFSET";
352 case R_V850_ZDA_16_16_OFFSET: return "R_V850_ZDA_16_16_OFFSET";
353 case R_V850_ZDA_15_16_OFFSET: return "R_V850_ZDA_15_16_OFFSET";
354 case R_V850_TDA_6_8_OFFSET: return "R_V850_TDA_6_8_OFFSET";
355 case R_V850_TDA_7_8_OFFSET: return "R_V850_TDA_7_8_OFFSET";
356 case R_V850_TDA_7_7_OFFSET: return "R_V850_TDA_7_7_OFFSET";
357 case R_V850_TDA_16_16_OFFSET: return "R_V850_TDA_16_16_OFFSET";
358 /* start-sanitize-v850e */
359 case R_V850_TDA_4_5_OFFSET: return "R_V850_TDA_4_5_OFFSET";
360 case R_V850_TDA_4_4_OFFSET: return "R_V850_TDA_4_4_OFFSET";
361 case R_V850_SDA_16_16_SPLIT_OFFSET: return "R_V850_SDA_16_16_SPLIT_OFFSET";
362 case R_V850_ZDA_16_16_SPLIT_OFFSET: return "R_V850_ZDA_16_16_SPLIT_OFFSET";
363 case R_V850_CALLT_6_7_OFFSET: return "R_V850_CALLT_6_7_OFFSET";
364 case R_V850_CALLT_16_16_OFFSET: return "R_V850_CALLT_16_16_OFFSET";
365 /* end-sanitize-v850e */
366 default: return NULL;
367 }
368 }
369
370 /* Define the function to get ARM relocations. */
371 #include "elf/d10v.h"
372
373 /* start-sanitize-d30v */
374 /* Define the function to get ARM relocations. */
375 #include "elf/d30v.h"
376
377 /* end-sanitize-d30v */
378 /* Define the function to get SH relocations. */
379 #include "elf/sh.h"
380
381 /* Define the function to get MN10300 relocations. */
382 #include "elf/mn10300.h"
383
384 /* Define the function to get MN10200 relocations. */
385 #include "elf/mn10200.h"
386
387 /* Define the function to get PPC relocations. */
388 #include "elf/ppc.h"
389
390 /* Define the function to get MIPS relocations. */
391 #include "elf/mips.h"
392
393 /* Define the function to get MIPS relocations. */
394 #include "elf/alpha.h"
395
396 /* Define the function to get ARM relocations. */
397 #include "elf/arm.h"
398
399
400 /* Display the contents of the relocation data
401 found at the specified offset. */
402 static int
403 dump_relocations (file, rel_offset, rel_size, symtab, strtab)
404 FILE * file;
405 unsigned long rel_offset;
406 unsigned long rel_size;
407 Elf_Internal_Sym * symtab;
408 char * strtab;
409 {
410 unsigned int i;
411 int is_rela;
412 Elf_Internal_Rel * rels;
413 Elf_Internal_Rela * relas;
414
415
416 /* Compute number of relocations and read them in. */
417 switch (elf_header.e_machine)
418 {
419 case EM_386:
420 case EM_486:
421 case EM_CYGNUS_M32R:
422 case EM_CYGNUS_D10V:
423 case EM_MIPS:
424 case EM_MIPS_RS4_BE:
425 case EM_ARM:
426 {
427 Elf32_External_Rel * erels;
428
429 GET_DATA_ALLOC (rel_offset, rel_size, erels,
430 Elf32_External_Rel *, "relocs");
431
432 rel_size = rel_size / sizeof (Elf32_External_Rel);
433
434 rels = (Elf_Internal_Rel *) malloc (rel_size *
435 sizeof (Elf_Internal_Rel));
436
437 for (i = 0; i < rel_size; i++)
438 {
439 rels[i].r_offset = BYTE_GET (erels[i].r_offset);
440 rels[i].r_info = BYTE_GET (erels[i].r_info);
441 }
442
443 free (erels);
444
445 is_rela = 0;
446 relas = (Elf_Internal_Rela *) rels;
447 }
448 break;
449
450 case EM_68K:
451 case EM_SPARC:
452 case EM_PPC:
453 case EM_CYGNUS_V850:
454 /* start-sanitize-d30v */
455 case EM_CYGNUS_D30V:
456 /* end-sanitize-d30v */
457 case EM_CYGNUS_MN10200:
458 case EM_CYGNUS_MN10300:
459 case EM_SH:
460 case EM_ALPHA:
461 {
462 Elf32_External_Rela * erelas;
463
464 GET_DATA_ALLOC (rel_offset, rel_size, erelas,
465 Elf32_External_Rela *, "relocs");
466
467 rel_size = rel_size / sizeof (Elf32_External_Rela);
468
469 relas = (Elf_Internal_Rela *) malloc (rel_size *
470 sizeof (Elf_Internal_Rela));
471
472 for (i = 0; i < rel_size; i++)
473 {
474 relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
475 relas[i].r_info = BYTE_GET (erelas[i].r_info);
476 relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
477 }
478
479 free (erelas);
480
481 is_rela = 1;
482 rels = (Elf_Internal_Rel *) relas;
483 }
484 break;
485
486 default:
487 warn (_("Don't know about relocations on this machine architecture\n"));
488 return 0;
489 }
490
491 if (is_rela)
492 printf
493 (_(" Offset Value Type Symbol's Value Symbol's Name Addend\n"));
494 else
495 printf
496 (_(" Offset Value Type Symbol's Value Symbol's Name\n"));
497
498 for (i = 0; i < rel_size; i++)
499 {
500 const char * rtype;
501 unsigned long offset;
502 unsigned long info;
503 int symtab_index;
504
505 if (is_rela)
506 {
507 offset = relas [i].r_offset;
508 info = relas [i].r_info;
509 }
510 else
511 {
512 offset = rels [i].r_offset;
513 info = rels [i].r_info;
514 }
515
516 printf (" %8.8lx %5.5lx ", offset, info);
517
518 switch (elf_header.e_machine)
519 {
520 default:
521 rtype = NULL;
522 break;
523
524 case EM_CYGNUS_M32R:
525 rtype = elf_m32r_reloc_type (ELF32_R_TYPE (info));
526 break;
527
528 case EM_386:
529 case EM_486:
530 rtype = elf_i386_reloc_type (ELF32_R_TYPE (info));
531 break;
532
533 case EM_68K:
534 rtype = elf_m68k_reloc_type (ELF32_R_TYPE (info));
535 break;
536
537 case EM_SPARC:
538 rtype = elf_sparc_reloc_type (ELF32_R_TYPE (info));
539 break;
540
541 case EM_CYGNUS_V850:
542 rtype = elf_v850_reloc_type (ELF32_R_TYPE (info));
543 break;
544
545 case EM_CYGNUS_D10V:
546 rtype = elf_d10v_reloc_type (ELF32_R_TYPE (info));
547 break;
548
549 /* start-sanitize-d30v */
550 case EM_CYGNUS_D30V:
551 rtype = elf_d30v_reloc_type (ELF32_R_TYPE (info));
552 break;
553
554 /* end-sanitize-d30v */
555 case EM_SH:
556 rtype = elf_sh_reloc_type (ELF32_R_TYPE (info));
557 break;
558
559 case EM_CYGNUS_MN10300:
560 rtype = elf_mn10300_reloc_type (ELF32_R_TYPE (info));
561 break;
562
563 case EM_CYGNUS_MN10200:
564 rtype = elf_mn10200_reloc_type (ELF32_R_TYPE (info));
565 break;
566
567 case EM_PPC:
568 rtype = elf_ppc_reloc_type (ELF32_R_TYPE (info));
569 break;
570
571 case EM_MIPS:
572 case EM_MIPS_RS4_BE:
573 rtype = elf_mips_reloc_type (ELF32_R_TYPE (info));
574 break;
575
576 case EM_ALPHA:
577 rtype = elf_alpha_reloc_type (ELF32_R_TYPE (info));
578 break;
579
580 case EM_ARM:
581 rtype = elf_arm_reloc_type (ELF32_R_TYPE (info));
582 break;
583 }
584
585 if (rtype == NULL)
586 printf (_("unrecognised: %-7x"), ELF32_R_TYPE (info));
587 else
588 printf ("%-21.21s", rtype);
589
590 symtab_index = ELF32_R_SYM (info);
591
592 if (symtab_index && symtab != NULL)
593 {
594 Elf_Internal_Sym * psym;
595
596 psym = symtab + symtab_index;
597
598 printf (" %08lx ", (unsigned long) psym->st_value);
599
600 if (psym->st_name == 0)
601 printf ("%-25.25s",
602 SECTION_NAME (section_headers + psym->st_shndx));
603 else if (strtab == NULL)
604 printf (_("<string table index %3d>"), psym->st_name);
605 else
606 printf ("%-25.25s", strtab + psym->st_name);
607
608 if (is_rela)
609 printf (" + %lx", (unsigned long) relas [i].r_addend);
610 }
611
612 putchar ('\n');
613 }
614
615 free (relas);
616
617 return 1;
618 }
619
620 static const char *
621 get_mips_dynamic_type (type)
622 unsigned long type;
623 {
624 switch (type)
625 {
626 case DT_MIPS_RLD_VERSION: return "MIPS_RLD_VERSION";
627 case DT_MIPS_TIME_STAMP: return "MIPS_TIME_STAMP";
628 case DT_MIPS_ICHECKSUM: return "MIPS_ICHECKSUM";
629 case DT_MIPS_IVERSION: return "MIPS_IVERSION";
630 case DT_MIPS_FLAGS: return "MIPS_FLAGS";
631 case DT_MIPS_BASE_ADDRESS: return "MIPS_BASE_ADDRESS";
632 case DT_MIPS_MSYM: return "MIPS_MSYM";
633 case DT_MIPS_CONFLICT: return "MIPS_CONFLICT";
634 case DT_MIPS_LIBLIST: return "MIPS_LIBLIST";
635 case DT_MIPS_LOCAL_GOTNO: return "MIPS_LOCAL_GOTNO";
636 case DT_MIPS_CONFLICTNO: return "MIPS_CONFLICTNO";
637 case DT_MIPS_LIBLISTNO: return "MIPS_LIBLISTNO";
638 case DT_MIPS_SYMTABNO: return "MIPS_SYMTABNO";
639 case DT_MIPS_UNREFEXTNO: return "MIPS_UNREFEXTNO";
640 case DT_MIPS_GOTSYM: return "MIPS_GOTSYM";
641 case DT_MIPS_HIPAGENO: return "MIPS_HIPAGENO";
642 case DT_MIPS_RLD_MAP: return "MIPS_RLD_MAP";
643 case DT_MIPS_DELTA_CLASS: return "MIPS_DELTA_CLASS";
644 case DT_MIPS_DELTA_CLASS_NO: return "MIPS_DELTA_CLASS_NO";
645 case DT_MIPS_DELTA_INSTANCE: return "MIPS_DELTA_INSTANCE";
646 case DT_MIPS_DELTA_INSTANCE_NO: return "MIPS_DELTA_INSTANCE_NO";
647 case DT_MIPS_DELTA_RELOC: return "MIPS_DELTA_RELOC";
648 case DT_MIPS_DELTA_RELOC_NO: return "MIPS_DELTA_RELOC_NO";
649 case DT_MIPS_DELTA_SYM: return "MIPS_DELTA_SYM";
650 case DT_MIPS_DELTA_SYM_NO: return "MIPS_DELTA_SYM_NO";
651 case DT_MIPS_DELTA_CLASSSYM: return "MIPS_DELTA_CLASSSYM";
652 case DT_MIPS_DELTA_CLASSSYM_NO: return "MIPS_DELTA_CLASSSYM_NO";
653 case DT_MIPS_CXX_FLAGS: return "MIPS_CXX_FLAGS";
654 case DT_MIPS_PIXIE_INIT: return "MIPS_PIXIE_INIT";
655 case DT_MIPS_SYMBOL_LIB: return "MIPS_SYMBOL_LIB";
656 case DT_MIPS_LOCALPAGE_GOTIDX: return "MIPS_LOCALPAGE_GOTIDX";
657 case DT_MIPS_LOCAL_GOTIDX: return "MIPS_LOCAL_GOTIDX";
658 case DT_MIPS_HIDDEN_GOTIDX: return "MIPS_HIDDEN_GOTIDX";
659 case DT_MIPS_PROTECTED_GOTIDX: return "MIPS_PROTECTED_GOTIDX";
660 case DT_MIPS_OPTIONS: return "MIPS_OPTIONS";
661 case DT_MIPS_INTERFACE: return "MIPS_INTERFACE";
662 case DT_MIPS_DYNSTR_ALIGN: return "MIPS_DYNSTR_ALIGN";
663 case DT_MIPS_INTERFACE_SIZE: return "MIPS_INTERFACE_SIZE";
664 case DT_MIPS_RLD_TEXT_RESOLVE_ADDR: return "MIPS_RLD_TEXT_RESOLVE_ADDR";
665 case DT_MIPS_PERF_SUFFIX: return "MIPS_PERF_SUFFIX";
666 case DT_MIPS_COMPACT_SIZE: return "MIPS_COMPACT_SIZE";
667 case DT_MIPS_GP_VALUE: return "MIPS_GP_VALUE";
668 case DT_MIPS_AUX_DYNAMIC: return "MIPS_AUX_DYNAMIC";
669 default:
670 return NULL;
671 }
672 }
673
674 static const char *
675 get_dynamic_type (type)
676 unsigned long type;
677 {
678 static char buff [32];
679
680 switch (type)
681 {
682 case DT_NULL: return _("NULL");
683 case DT_NEEDED: return _("NEEDED");
684 case DT_PLTRELSZ: return _("PLTRELSZ");
685 case DT_PLTGOT: return _("PLTGOT");
686 case DT_HASH: return _("HASH");
687 case DT_STRTAB: return _("STRTAB");
688 case DT_SYMTAB: return _("SYMTAB");
689 case DT_RELA: return _("RELA");
690 case DT_RELASZ: return _("RELASZ");
691 case DT_RELAENT: return _("RELAENT");
692 case DT_STRSZ: return _("STRSZ");
693 case DT_SYMENT: return _("SYMENT");
694 case DT_INIT: return _("INIT");
695 case DT_FINI: return _("FINI");
696 case DT_SONAME: return _("SONAME");
697 case DT_RPATH: return _("RPATH");
698 case DT_SYMBOLIC: return _("SYMBOLIC");
699 case DT_REL: return _("REL");
700 case DT_RELSZ: return _("RELSZ");
701 case DT_RELENT: return _("RELENT");
702 case DT_PLTREL: return _("PLTREL");
703 case DT_DEBUG: return _("DEBUG");
704 case DT_TEXTREL: return _("TEXTREL");
705 case DT_JMPREL: return _("JMPREL");
706 case DT_VERDEF: return _("VERDEF");
707 case DT_VERDEFNUM: return _("VERDEFNUM");
708 case DT_VERNEED: return _("VERNEED");
709 case DT_VERNEEDNUM: return _("VERNEEDNUM");
710 case DT_VERSYM: return _("VERSYN");
711 case DT_AUXILIARY: return _("AUXILARY");
712 case DT_FILTER: return _("FILTER");
713
714 default:
715 if ((type >= DT_LOPROC) && (type <= DT_HIPROC))
716 {
717 const char *result = NULL;
718 switch (elf_header.e_machine)
719 {
720 case EM_MIPS:
721 case EM_MIPS_RS4_BE:
722 result = get_mips_dynamic_type (type);
723 default:
724 }
725
726 if (result == NULL)
727 {
728 sprintf (buff, _("Processor Specific: (%x)"), type);
729 result = buff;
730 }
731 return result;
732 }
733 else
734 sprintf (buff, _("<unknown>: %x"), type);
735 return buff;
736 }
737 }
738
739 static char *
740 get_file_type (e_type)
741 unsigned e_type;
742 {
743 static char buff [32];
744
745 switch (e_type)
746 {
747 case ET_NONE: return _("NONE (None)");
748 case ET_REL: return _("REL (Relocatable file)");
749 case ET_EXEC: return _("EXEC (Executable file)");
750 case ET_DYN: return _("DYN (Shared object file)");
751 case ET_CORE: return _("CORE (Core file)");
752
753 default:
754 if ((e_type >= ET_LOPROC) && (e_type <= ET_HIPROC))
755 sprintf (buff, _("Processor Specific: (%x)"), e_type);
756 else
757 sprintf (buff, _("<unknown>: %x"), e_type);
758 return buff;
759 }
760 }
761
762 static char *
763 get_machine_name (e_machine)
764 unsigned e_machine;
765 {
766 static char buff [32];
767
768 switch (e_machine)
769 {
770 case EM_NONE: return _("None");
771 case EM_M32: return "WE32100";
772 case EM_SPARC: return "Sparc";
773 case EM_386: return "Intel 80386";
774 case EM_68K: return "MC68000";
775 case EM_88K: return "MC88000";
776 case EM_486: return "Intel 80486";
777 case EM_860: return "Intel 80860";
778 case EM_MIPS: return "MIPS R3000 big-endian";
779 case EM_S370: return "Amdahl";
780 case EM_MIPS_RS4_BE: return "MIPS R4000 big-endian";
781 case EM_OLD_SPARCV9: return "Sparc v9 (old)";
782 case EM_PARISC: return "HPPA";
783 case EM_PPC_OLD: return "Power PC (old)";
784 case EM_SPARC32PLUS: return "Sparc v8+" ;
785 case EM_960: return "Intel 90860";
786 case EM_PPC: return "PowerPC";
787 case EM_V800: return "NEC V800";
788 case EM_FR20: return "Fujitsu FR20";
789 case EM_RH32: return "TRW RH32";
790 case EM_MMA: return "Fujitsu MMA";
791 case EM_ARM: return "ARM";
792 case EM_OLD_ALPHA: return "Digital Alpha (old)";
793 case EM_SH: return "Hitachi SH";
794 case EM_SPARCV9: return "Sparc v9";
795 case EM_ALPHA: return "Alpha";
796 case EM_CYGNUS_D10V: return "d10v";
797 /* start-sanitize-d30v */
798 case EM_CYGNUS_D30V: return "d30v";
799 /* end-sanitize-d30v */
800 case EM_CYGNUS_M32R: return "M32r";
801 case EM_CYGNUS_V850: return "v850";
802 case EM_CYGNUS_MN10300: return "mn10300";
803 case EM_CYGNUS_MN10200: return "mn10200";
804
805 default:
806 sprintf (buff, _("<unknown>: %x"), e_machine);
807 return buff;
808 }
809 }
810
811 static char *
812 get_machine_flags (e_flags, e_machine)
813 unsigned e_flags;
814 unsigned e_machine;
815 {
816 static char buf [1024];
817
818 buf[0] = '\0';
819 if (e_flags)
820 {
821 switch (e_machine)
822 {
823 default:
824 break;
825
826 case EM_PPC:
827 if (e_flags & EF_PPC_EMB)
828 strcat (buf, ", emb");
829
830 if (e_flags & EF_PPC_RELOCATABLE)
831 strcat (buf, ", relocatable");
832
833 if (e_flags & EF_PPC_RELOCATABLE_LIB)
834 strcat (buf, ", relocatable-lib");
835 break;
836
837 case EM_CYGNUS_M32R:
838 if ((e_flags & EF_M32R_ARCH) == E_M32R_ARCH)
839 strcat (buf, ", m32r");
840
841 /* start-sanitize-m32rx */
842 #ifdef E_M32RX_ARCH
843 if ((e_flags & EF_M32R_ARCH) == E_M32RX_ARCH)
844 strcat (buf, ", m32rx");
845 #endif
846 /* end-sanitize-m32rx */
847 break;
848
849 case EM_MIPS:
850 case EM_MIPS_RS4_BE:
851 if (e_flags & EF_MIPS_NOREORDER)
852 strcat (buf, ", noreorder");
853
854 if (e_flags & EF_MIPS_PIC)
855 strcat (buf, ", pic");
856
857 if (e_flags & EF_MIPS_CPIC)
858 strcat (buf, ", cpic");
859
860 if (e_flags & EF_MIPS_ABI2)
861 strcat (buf, ", abi2");
862
863 if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_1)
864 strcat (buf, ", mips1");
865
866 if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_2)
867 strcat (buf, ", mips2");
868
869 if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_3)
870 strcat (buf, ", mips3");
871
872 if ((e_flags & EF_MIPS_ARCH) == E_MIPS_ARCH_4)
873 strcat (buf, ", mips4");
874 break;
875 }
876 }
877
878 return buf;
879 }
880
881 static char *
882 get_machine_data (e_data)
883 unsigned e_data;
884 {
885 static char buff [32];
886
887 switch (e_data)
888 {
889 case ELFDATA2LSB: return _("ELFDATA2LSB (little endian)");
890 case ELFDATA2MSB: return _("ELFDATA2MSB (big endian)");
891 default:
892 sprintf (buff, _("<unknown>: %x"), e_data);
893 return buff;
894 }
895 }
896
897 static const char *
898 get_mips_segment_type (type)
899 unsigned long type;
900 {
901 switch (type)
902 {
903 case PT_MIPS_REGINFO:
904 return "REGINFO";
905 case PT_MIPS_RTPROC:
906 return "RTPROC";
907 case PT_MIPS_OPTIONS:
908 return "OPTIONS";
909 default:
910 break;
911 }
912
913 return NULL;
914 }
915
916 static const char *
917 get_segment_type (p_type)
918 unsigned long p_type;
919 {
920 static char buff [32];
921
922 switch (p_type)
923 {
924 case PT_NULL: return "NULL";
925 case PT_LOAD: return "LOAD";
926 case PT_DYNAMIC: return "DYNAMIC";
927 case PT_INTERP: return "INTERP";
928 case PT_NOTE: return "NOTE";
929 case PT_SHLIB: return "SHLIB";
930 case PT_PHDR: return "PHDR";
931
932 default:
933 if ((p_type >= PT_LOPROC) && (p_type <= PT_HIPROC))
934 {
935 const char *result;
936 switch (elf_header.e_machine)
937 {
938 case EM_MIPS:
939 case EM_MIPS_RS4_BE:
940 result = get_mips_segment_type (p_type);
941 break;
942 default:
943 result = NULL;
944 break;
945 }
946 if (result == NULL)
947 {
948 sprintf (buff, "LOPROC+%d", p_type - PT_LOPROC);
949 result = buff;
950 }
951 return result;
952 }
953 else
954 {
955 sprintf (buff, _("<unknown>: %x"), p_type);
956 return buff;
957 }
958 }
959 }
960
961 static const char *
962 get_mips_section_type_name (sh_type)
963 unsigned int sh_type;
964 {
965 switch (sh_type)
966 {
967 case SHT_MIPS_LIBLIST:
968 return "MIPS_LIBLIST";
969 case SHT_MIPS_MSYM:
970 return "MIPS_MSYM";
971 case SHT_MIPS_CONFLICT:
972 return "MIPS_CONFLICT";
973 case SHT_MIPS_GPTAB:
974 return "MIPS_GPTAB";
975 case SHT_MIPS_UCODE:
976 return "MIPS_UCODE";
977 case SHT_MIPS_DEBUG:
978 return "MIPS_DEBUG";
979 case SHT_MIPS_REGINFO:
980 return "MIPS_REGINFO";
981 case SHT_MIPS_PACKAGE:
982 return "MIPS_PACKAGE";
983 case SHT_MIPS_PACKSYM:
984 return "MIPS_PACKSYM";
985 case SHT_MIPS_RELD:
986 return "MIPS_RELD";
987 case SHT_MIPS_IFACE:
988 return "MIPS_IFACE";
989 case SHT_MIPS_CONTENT:
990 return "MIPS_CONTENT";
991 case SHT_MIPS_OPTIONS:
992 return "MIPS_OPTIONS";
993 case SHT_MIPS_SHDR:
994 return "MIPS_SHDR";
995 case SHT_MIPS_FDESC:
996 return "MIPS_FDESC";
997 case SHT_MIPS_EXTSYM:
998 return "MIPS_EXTSYM";
999 case SHT_MIPS_DENSE:
1000 return "MIPS_DENSE";
1001 case SHT_MIPS_PDESC:
1002 return "MIPS_PDESC";
1003 case SHT_MIPS_LOCSYM:
1004 return "MIPS_LOCSYM";
1005 case SHT_MIPS_AUXSYM:
1006 return "MIPS_AUXSYM";
1007 case SHT_MIPS_OPTSYM:
1008 return "MIPS_OPTSYM";
1009 case SHT_MIPS_LOCSTR:
1010 return "MIPS_LOCSTR";
1011 case SHT_MIPS_LINE:
1012 return "MIPS_LINE";
1013 case SHT_MIPS_RFDESC:
1014 return "MIPS_RFDESC";
1015 case SHT_MIPS_DELTASYM:
1016 return "MIPS_DELTASYM";
1017 case SHT_MIPS_DELTAINST:
1018 return "MIPS_DELTAINST";
1019 case SHT_MIPS_DELTACLASS:
1020 return "MIPS_DELTACLASS";
1021 case SHT_MIPS_DWARF:
1022 return "MIPS_DWARF";
1023 case SHT_MIPS_DELTADECL:
1024 return "MIPS_DELTADECL";
1025 case SHT_MIPS_SYMBOL_LIB:
1026 return "MIPS_SYMBOL_LIB";
1027 case SHT_MIPS_EVENTS:
1028 return "MIPS_EVENTS";
1029 case SHT_MIPS_TRANSLATE:
1030 return "MIPS_TRANSLATE";
1031 case SHT_MIPS_PIXIE:
1032 return "MIPS_PIXIE";
1033 case SHT_MIPS_XLATE:
1034 return "MIPS_XLATE";
1035 case SHT_MIPS_XLATE_DEBUG:
1036 return "MIPS_XLATE_DEBUG";
1037 case SHT_MIPS_WHIRL:
1038 return "MIPS_WHIRL";
1039 case SHT_MIPS_EH_REGION:
1040 return "MIPS_EH_REGION";
1041 case SHT_MIPS_XLATE_OLD:
1042 return "MIPS_XLATE_OLD";
1043 case SHT_MIPS_PDR_EXCEPTION:
1044 return "MIPS_PDR_EXCEPTION";
1045 default:
1046 break;
1047 }
1048 return NULL;
1049 }
1050
1051 static const char *
1052 get_section_type_name (sh_type)
1053 unsigned int sh_type;
1054 {
1055 static char buff [32];
1056
1057 switch (sh_type)
1058 {
1059 case SHT_NULL: return "NULL";
1060 case SHT_PROGBITS: return "PROGBITS";
1061 case SHT_SYMTAB: return "SYMTAB";
1062 case SHT_STRTAB: return "STRTAB";
1063 case SHT_RELA: return "RELA";
1064 case SHT_HASH: return "HASH";
1065 case SHT_DYNAMIC: return "DYNAMIC";
1066 case SHT_NOTE: return "NOTE";
1067 case SHT_NOBITS: return "NOBITS";
1068 case SHT_REL: return "REL";
1069 case SHT_SHLIB: return "SHLIB";
1070 case SHT_DYNSYM: return "DYNSYM";
1071 case SHT_GNU_verdef: return "VERDEF";
1072 case SHT_GNU_verneed: return "VERNEED";
1073 case SHT_GNU_versym: return "VERSYM";
1074 case 0x6ffffff0: return "VERSYM";
1075 case 0x6ffffffc: return "VERDEF";
1076 case 0x7ffffffd: return "AUXILIARY";
1077 case 0x7fffffff: return "FILTER";
1078
1079 default:
1080 if ((sh_type >= SHT_LOPROC) && (sh_type <= SHT_HIPROC))
1081 {
1082 const char *result;
1083
1084 switch (elf_header.e_machine)
1085 {
1086 case EM_MIPS:
1087 case EM_MIPS_RS4_BE:
1088 result = get_mips_section_type_name (sh_type);
1089 break;
1090 default:
1091 result = NULL;
1092 break;
1093 }
1094
1095 if (result == NULL)
1096 {
1097 sprintf (buff, _("SHT_LOPROC+%d"), sh_type - SHT_LOPROC);
1098 result = buff;
1099 }
1100 return result;
1101 }
1102 else if ((sh_type >= SHT_LOUSER) && (sh_type <= SHT_HIUSER))
1103 sprintf (buff, _("SHT_LOUSER+%d"), sh_type - SHT_LOUSER);
1104 else
1105 sprintf (buff, _("<unknown>: %x"), sh_type);
1106 return buff;
1107 }
1108 }
1109
1110 struct option options [] =
1111 {
1112 {"all", no_argument, 0, 'a'},
1113 {"file-header", no_argument, 0, 'h'},
1114 {"program-headers", no_argument, 0, 'l'},
1115 {"headers", no_argument, 0, 'e'},
1116 {"segments", no_argument, 0, 'l'},
1117 {"sections", no_argument, 0, 'S'},
1118 {"section-headers", no_argument, 0, 'S'},
1119 {"symbols", no_argument, 0, 's'},
1120 {"relocs", no_argument, 0, 'r'},
1121 {"dynamic", no_argument, 0, 'd'},
1122 {"version-info", no_argument, 0, 'V'},
1123 {"use-dynamic", no_argument, 0, 'D'},
1124
1125 {"hex-dump", required_argument, 0, 'x'},
1126 #ifdef SUPPORT_DISASSEMBLY
1127 {"instruction-dump", required_argument, 0, 'i'},
1128 #endif
1129
1130 {"version", no_argument, 0, 'v'},
1131 {"help", no_argument, 0, 'H'},
1132
1133 {0, no_argument, 0, 0}
1134 };
1135
1136 static void
1137 usage ()
1138 {
1139 fprintf (stdout, _("Usage: readelf {options} elf-file(s)\n"));
1140 fprintf (stdout, _(" Options are:\n"));
1141 fprintf (stdout, _(" -a or --all Equivalent to: -h -l -S -s -r -d -V\n"));
1142 fprintf (stdout, _(" -h or --file-header Display the ELF file header\n"));
1143 fprintf (stdout, _(" -l or --program-headers or --segments\n"));
1144 fprintf (stdout, _(" Display the program headers\n"));
1145 fprintf (stdout, _(" -S or --section-headers or --sections\n"));
1146 fprintf (stdout, _(" Display the sections' header\n"));
1147 fprintf (stdout, _(" -e or --headers Equivalent to: -h -l -S\n"));
1148 fprintf (stdout, _(" -s or --symbols Display the symbol table\n"));
1149 fprintf (stdout, _(" -r or --relocs Display the relocations (if present)\n"));
1150 fprintf (stdout, _(" -d or --dynamic Display the dynamic segment (if present)\n"));
1151 fprintf (stdout, _(" -V or --version-info Display the version sections (if present)\n"));
1152 fprintf (stdout, _(" -D or --use-dynamic Use the dynamic section info when displaying symbols\n"));
1153 fprintf (stdout, _(" -x <number> or --hex-dump=<number>\n"));
1154 fprintf (stdout, _(" Dump the contents of section <number>\n"));
1155 #ifdef SUPPORT_DISASSEMBLY
1156 fprintf (stdout, _(" -i <number> or --instruction-dump=<number>\n"));
1157 fprintf (stdout, _(" Disassemble the contents of section <number>\n"));
1158 #endif
1159 fprintf (stdout, _(" -v or --version Display the version number of readelf\n"));
1160 fprintf (stdout, _(" -H or --help Display this information\n"));
1161 fprintf (stdout, _("Report bugs to bug-gnu-utils@gnu.org\n"));
1162
1163 exit (0);
1164 }
1165
1166 static void
1167 parse_args (argc, argv)
1168 int argc;
1169 char ** argv;
1170 {
1171 int c;
1172
1173 if (argc < 2)
1174 usage ();
1175
1176 while ((c = getopt_long
1177 (argc, argv, "ersahldSDx:i:vV", options, NULL)) != EOF)
1178 {
1179 char * cp;
1180 int section;
1181
1182 switch (c)
1183 {
1184 case 'H':
1185 usage ();
1186 break;
1187
1188 case 'a':
1189 do_syms ++;
1190 do_reloc ++;
1191 do_dynamic ++;
1192 do_header ++;
1193 do_sections ++;
1194 do_segments ++;
1195 do_version ++;
1196 break;
1197 case 'e':
1198 do_header ++;
1199 do_sections ++;
1200 do_segments ++;
1201 break;
1202 case 'D':
1203 do_using_dynamic ++;
1204 break;
1205 case 'r':
1206 do_reloc ++;
1207 break;
1208 case 'h':
1209 do_header ++;
1210 break;
1211 case 'l':
1212 do_segments ++;
1213 break;
1214 case 's':
1215 do_syms ++;
1216 break;
1217 case 'S':
1218 do_sections ++;
1219 break;
1220 case 'd':
1221 do_dynamic ++;
1222 break;
1223 case 'x':
1224 do_dump ++;
1225 section = strtoul (optarg, & cp, 0);
1226 if (! * cp && section >= 0 && section < NUM_DUMP_SECTS)
1227 {
1228 dump_sects [section] |= HEX_DUMP;
1229 break;
1230 }
1231 goto oops;
1232 #ifdef SUPPORT_DISASSEMBLY
1233 case 'i':
1234 do_dump ++;
1235 section = strtoul (optarg, & cp, 0);
1236 if (! * cp && section >= 0 && section < NUM_DUMP_SECTS)
1237 {
1238 dump_sects [section] |= DISASS_DUMP;
1239 break;
1240 }
1241 goto oops;
1242 #endif
1243 case 'v':
1244 print_version (program_name);
1245 break;
1246 case 'V':
1247 do_version ++;
1248 break;
1249 default:
1250 oops:
1251 /* xgettext:c-format */
1252 error (_("Invalid option '-%c'\n"), c);
1253 /* Drop through. */
1254 case '?':
1255 usage ();
1256 }
1257 }
1258
1259 if (!do_dynamic && !do_syms && !do_reloc && !do_sections
1260 && !do_segments && !do_header && !do_dump && !do_version)
1261 usage ();
1262 else if (argc < 3)
1263 {
1264 warn (_("Nothing to do.\n"));
1265 usage();
1266 }
1267 }
1268
1269 /* Decode the data held in 'elf_header'. */
1270 static int
1271 process_file_header ()
1272 {
1273 if ( elf_header.e_ident [EI_MAG0] != ELFMAG0
1274 || elf_header.e_ident [EI_MAG1] != ELFMAG1
1275 || elf_header.e_ident [EI_MAG2] != ELFMAG2
1276 || elf_header.e_ident [EI_MAG3] != ELFMAG3)
1277 {
1278 error
1279 (_("Not an ELF file - it has the wrong magic bytes at the start\n"));
1280 return 0;
1281 }
1282
1283 if (elf_header.e_ident [EI_CLASS] != ELFCLASS32)
1284 {
1285 error (_("Not a 32 bit ELF file\n"));
1286 return 0;
1287 }
1288
1289 if (do_header)
1290 {
1291 int i;
1292
1293 printf (_("ELF Header:\n"));
1294 printf (_(" Magic: "));
1295 for (i = 0; i < EI_NIDENT; i ++)
1296 printf ("%2.2x ", elf_header.e_ident [i]);
1297 printf ("\n");
1298 printf (_(" Type: %s\n"),
1299 get_file_type (elf_header.e_type));
1300 printf (_(" Machine: %s\n"),
1301 get_machine_name (elf_header.e_machine));
1302 printf (_(" Version: 0x%lx\n"),
1303 (unsigned long) elf_header.e_version);
1304 printf (_(" Data: %s\n"),
1305 get_machine_data (elf_header.e_ident [EI_DATA]));
1306 printf (_(" Entry point address: 0x%lx\n"),
1307 (unsigned long) elf_header.e_entry);
1308 printf (_(" Start of program headers: %ld (bytes into file)\n"),
1309 (long) elf_header.e_phoff);
1310 printf (_(" Start of section headers: %ld (bytes into file)\n"),
1311 (long) elf_header.e_shoff);
1312 printf (_(" Flags: 0x%lx%s\n"),
1313 (unsigned long) elf_header.e_flags,
1314 get_machine_flags (elf_header.e_flags, elf_header.e_machine));
1315 printf (_(" Size of this header: %ld (bytes)\n"),
1316 (long) elf_header.e_ehsize);
1317 printf (_(" Size of program headers: %ld (bytes)\n"),
1318 (long) elf_header.e_phentsize);
1319 printf (_(" Number of program headers: %ld\n"),
1320 (long) elf_header.e_phnum);
1321 printf (_(" Size of section headers: %ld (bytes)\n"),
1322 (long) elf_header.e_shentsize);
1323 printf (_(" Number of section headers: %ld\n"),
1324 (long) elf_header.e_shnum);
1325 printf (_(" Section header string table index: %ld\n"),
1326 (long) elf_header.e_shstrndx);
1327 }
1328
1329 return 1;
1330 }
1331
1332
1333 static int
1334 process_program_headers (file)
1335 FILE * file;
1336 {
1337 Elf32_External_Phdr * phdrs;
1338 Elf32_Internal_Phdr * program_headers;
1339 Elf32_Internal_Phdr * segment;
1340 unsigned int i;
1341
1342 if (elf_header.e_phnum == 0)
1343 {
1344 if (do_segments)
1345 printf (_("\nThere are no program headers in this file.\n"));
1346 return 1;
1347 }
1348
1349 if (do_segments && !do_header)
1350 {
1351 printf (_("\nElf file is %s\n"), get_file_type (elf_header.e_type));
1352 printf (_("Entry point 0x%lx\n"), (unsigned long) elf_header.e_entry);
1353 printf (_("There are %d program headers, starting at offset %lx:\n"),
1354 elf_header.e_phnum, (unsigned long) elf_header.e_phoff);
1355 }
1356
1357 GET_DATA_ALLOC (elf_header.e_phoff,
1358 elf_header.e_phentsize * elf_header.e_phnum,
1359 phdrs, Elf32_External_Phdr *, "program headers");
1360
1361 program_headers = (Elf32_Internal_Phdr *) malloc
1362 (elf_header.e_phnum * sizeof (Elf32_Internal_Phdr));
1363
1364 if (program_headers == NULL)
1365 {
1366 error (_("Out of memory\n"));
1367 return 0;
1368 }
1369
1370 for (i = 0, segment = program_headers;
1371 i < elf_header.e_phnum;
1372 i ++, segment ++)
1373 {
1374 segment->p_type = BYTE_GET (phdrs[i].p_type);
1375 segment->p_offset = BYTE_GET (phdrs[i].p_offset);
1376 segment->p_vaddr = BYTE_GET (phdrs[i].p_vaddr);
1377 segment->p_paddr = BYTE_GET (phdrs[i].p_paddr);
1378 segment->p_filesz = BYTE_GET (phdrs[i].p_filesz);
1379 segment->p_memsz = BYTE_GET (phdrs[i].p_memsz);
1380 segment->p_flags = BYTE_GET (phdrs[i].p_flags);
1381 segment->p_align = BYTE_GET (phdrs[i].p_align);
1382 }
1383
1384 free (phdrs);
1385
1386 if (do_segments)
1387 {
1388 printf
1389 (_("\nProgram Header%s:\n"), elf_header.e_phnum > 1 ? "s" : "");
1390 printf
1391 (_(" Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align\n"));
1392 }
1393
1394 loadaddr = -1;
1395 dynamic_addr = 0;
1396
1397 for (i = 0, segment = program_headers;
1398 i < elf_header.e_phnum;
1399 i ++, segment ++)
1400 {
1401 if (do_segments)
1402 {
1403 printf (" %-11.11s ", get_segment_type (segment->p_type));
1404 printf ("0x%5.5lx ", (unsigned long) segment->p_offset);
1405 printf ("0x%8.8lx ", (unsigned long) segment->p_vaddr);
1406 printf ("0x%8.8lx ", (unsigned long) segment->p_paddr);
1407 printf ("0x%5.5lx ", (unsigned long) segment->p_filesz);
1408 printf ("0x%5.5lx ", (unsigned long) segment->p_memsz);
1409 printf ("%c%c%c ",
1410 (segment->p_flags & PF_R ? 'R' : ' '),
1411 (segment->p_flags & PF_W ? 'W' : ' '),
1412 (segment->p_flags & PF_X ? 'E' : ' '));
1413 printf ("%#lx", (unsigned long) segment->p_align);
1414 }
1415
1416 switch (segment->p_type)
1417 {
1418 case PT_LOAD:
1419 if (loadaddr == -1)
1420 loadaddr = (segment->p_vaddr & 0xfffff000)
1421 - (segment->p_offset & 0xfffff000);
1422 break;
1423
1424 case PT_DYNAMIC:
1425 if (dynamic_addr)
1426 error (_("more than one dynamic segment\n"));
1427
1428 dynamic_addr = segment->p_offset;
1429 dynamic_size = segment->p_filesz;
1430 break;
1431
1432 case PT_INTERP:
1433 if (fseek (file, segment->p_offset, SEEK_SET))
1434 error (_("Unable to find program interpreter name\n"));
1435 else
1436 {
1437 program_interpreter[0] = 0;
1438 fscanf (file, "%63s", program_interpreter);
1439
1440 if (do_segments)
1441 printf (_("\n [Requesting program interpreter: %s]"),
1442 program_interpreter);
1443 }
1444 break;
1445 }
1446
1447 if (do_segments)
1448 putc ('\n', stdout);
1449 }
1450
1451 if (loadaddr == -1)
1452 {
1453 /* Very strange. */
1454 loadaddr = 0;
1455 }
1456
1457 if (do_segments && section_headers != NULL)
1458 {
1459 printf (_("\n Section to Segment mapping:\n"));
1460 printf (_(" Segment Sections...\n"));
1461
1462 assert (string_table != NULL);
1463
1464 for (i = 0; i < elf_header.e_phnum; i++)
1465 {
1466 int j;
1467 Elf32_Internal_Shdr * section;
1468
1469 segment = program_headers + i;
1470 section = section_headers;
1471
1472 printf (" %2.2d ", i);
1473
1474 for (j = 0; j < elf_header.e_shnum; j++, section ++)
1475 {
1476 if (section->sh_size > 0
1477 /* Compare allocated sections by VMA, unallocated
1478 sections by file offset. */
1479 && (section->sh_flags & SHF_ALLOC
1480 ? (section->sh_addr >= segment->p_vaddr
1481 && section->sh_addr + section->sh_size
1482 <= segment->p_vaddr + segment->p_memsz)
1483 : (section->sh_offset >= segment->p_offset
1484 && (section->sh_offset + section->sh_size
1485 <= segment->p_offset + segment->p_filesz))))
1486 printf ("%s ", SECTION_NAME (section));
1487 }
1488
1489 putc ('\n',stdout);
1490 }
1491 }
1492
1493 free (program_headers);
1494
1495 return 1;
1496 }
1497
1498
1499 static int
1500 get_section_headers (file)
1501 FILE * file;
1502 {
1503 Elf32_External_Shdr * shdrs;
1504 Elf32_Internal_Shdr * internal;
1505 unsigned int i;
1506
1507 GET_DATA_ALLOC (elf_header.e_shoff,
1508 elf_header.e_shentsize * elf_header.e_shnum,
1509 shdrs, Elf32_External_Shdr *, "section headers");
1510
1511 section_headers = (Elf32_Internal_Shdr *) malloc
1512 (elf_header.e_shnum * sizeof (Elf32_Internal_Shdr));
1513
1514 if (section_headers == NULL)
1515 {
1516 error (_("Out of memory\n"));
1517 return 0;
1518 }
1519
1520 for (i = 0, internal = section_headers;
1521 i < elf_header.e_shnum;
1522 i ++, internal ++)
1523 {
1524 internal->sh_name = BYTE_GET (shdrs[i].sh_name);
1525 internal->sh_type = BYTE_GET (shdrs[i].sh_type);
1526 internal->sh_flags = BYTE_GET (shdrs[i].sh_flags);
1527 internal->sh_addr = BYTE_GET (shdrs[i].sh_addr);
1528 internal->sh_offset = BYTE_GET (shdrs[i].sh_offset);
1529 internal->sh_size = BYTE_GET (shdrs[i].sh_size);
1530 internal->sh_link = BYTE_GET (shdrs[i].sh_link);
1531 internal->sh_info = BYTE_GET (shdrs[i].sh_info);
1532 internal->sh_addralign = BYTE_GET (shdrs[i].sh_addralign);
1533 internal->sh_entsize = BYTE_GET (shdrs[i].sh_entsize);
1534 }
1535
1536 free (shdrs);
1537
1538 return 1;
1539 }
1540
1541 static Elf_Internal_Sym *
1542 get_elf_symbols (file, offset, number)
1543 FILE * file;
1544 unsigned long offset;
1545 unsigned long number;
1546 {
1547 Elf32_External_Sym * esyms;
1548 Elf_Internal_Sym * isyms;
1549 Elf_Internal_Sym * psym;
1550 unsigned int j;
1551
1552 GET_DATA_ALLOC (offset, number * sizeof (Elf32_External_Sym),
1553 esyms, Elf32_External_Sym *, "symbols");
1554
1555 isyms = (Elf_Internal_Sym *) malloc (number * sizeof (Elf_Internal_Sym));
1556
1557 if (isyms == NULL)
1558 {
1559 error (_("Out of memory\n"));
1560 free (esyms);
1561
1562 return NULL;
1563 }
1564
1565 for (j = 0, psym = isyms;
1566 j < number;
1567 j ++, psym ++)
1568 {
1569 psym->st_name = BYTE_GET (esyms[j].st_name);
1570 psym->st_value = BYTE_GET (esyms[j].st_value);
1571 psym->st_size = BYTE_GET (esyms[j].st_size);
1572 psym->st_shndx = BYTE_GET (esyms[j].st_shndx);
1573 psym->st_info = BYTE_GET (esyms[j].st_info);
1574 psym->st_other = BYTE_GET (esyms[j].st_other);
1575 }
1576
1577 free (esyms);
1578
1579 return isyms;
1580 }
1581
1582 static int
1583 process_section_headers (file)
1584 FILE * file;
1585 {
1586 Elf32_Internal_Shdr * section;
1587 int i;
1588
1589 section_headers = NULL;
1590
1591 if (elf_header.e_shnum == 0)
1592 {
1593 if (do_sections)
1594 printf (_("\nThere are no sections in this file.\n"));
1595
1596 return 1;
1597 }
1598
1599 if (do_sections && !do_header)
1600 printf (_("There are %d section headers, starting at offset %x:\n"),
1601 elf_header.e_shnum, elf_header.e_shoff);
1602
1603 if (! get_section_headers (file))
1604 return 0;
1605
1606 /* Read in the string table, so that we have names to display. */
1607 section = section_headers + elf_header.e_shstrndx;
1608
1609 if (section->sh_size != 0)
1610 {
1611 unsigned long string_table_offset;
1612
1613 string_table_offset = section->sh_offset;
1614
1615 GET_DATA_ALLOC (section->sh_offset, section->sh_size,
1616 string_table, char *, "string table");
1617 }
1618
1619 /* Scan the sections for the dynamic symbol table
1620 and dynamic string table. */
1621 dynamic_symbols = NULL;
1622 dynamic_strings = NULL;
1623 for (i = 0, section = section_headers;
1624 i < elf_header.e_shnum;
1625 i ++, section ++)
1626 {
1627 if (section->sh_type == SHT_DYNSYM)
1628 {
1629 if (dynamic_symbols != NULL)
1630 {
1631 error (_("File contains multiple dynamic symbol tables\n"));
1632 continue;
1633 }
1634
1635 dynamic_symbols = get_elf_symbols
1636 (file, section->sh_offset, section->sh_size / section->sh_entsize);
1637 }
1638 else if (section->sh_type == SHT_STRTAB
1639 && strcmp (SECTION_NAME (section), ".dynstr") == 0)
1640 {
1641 if (dynamic_strings != NULL)
1642 {
1643 error (_("File contains multiple dynamic string tables\n"));
1644 continue;
1645 }
1646
1647 GET_DATA_ALLOC (section->sh_offset, section->sh_size,
1648 dynamic_strings, char *, "dynamic strings");
1649 }
1650 }
1651
1652 if (! do_sections)
1653 return 1;
1654
1655 printf (_("\nSection Header%s:\n"), elf_header.e_shnum > 1 ? "s" : "");
1656 printf
1657 (_(" [Nr] Name Type Addr Off Size ES Flg Lk Inf Al\n"));
1658
1659 for (i = 0, section = section_headers;
1660 i < elf_header.e_shnum;
1661 i ++, section ++)
1662 {
1663 printf (" [%2d] %-17.17s %-15.15s ",
1664 i,
1665 SECTION_NAME (section),
1666 get_section_type_name (section->sh_type));
1667
1668 printf ( "%8.8lx %6.6lx %6.6lx %2.2lx",
1669 (unsigned long) section->sh_addr,
1670 (unsigned long) section->sh_offset,
1671 (unsigned long) section->sh_size,
1672 (unsigned long) section->sh_entsize);
1673
1674 printf (" %c%c%c %2ld %3lx %ld \n",
1675 (section->sh_flags & SHF_WRITE ? 'W' : ' '),
1676 (section->sh_flags & SHF_ALLOC ? 'A' : ' '),
1677 (section->sh_flags & SHF_EXECINSTR ? 'X' : ' '),
1678 (unsigned long) section->sh_link,
1679 (unsigned long) section->sh_info,
1680 (unsigned long) section->sh_addralign);
1681 }
1682
1683 return 1;
1684 }
1685
1686 /* Process the reloc section. */
1687 static int
1688 process_relocs (file)
1689 FILE * file;
1690 {
1691 unsigned long rel_size;
1692 unsigned long rel_offset;
1693
1694
1695 if (!do_reloc)
1696 return 1;
1697
1698 if (do_using_dynamic)
1699 {
1700 rel_size = 0;
1701 rel_offset = 0;
1702
1703 if (dynamic_info [DT_REL])
1704 {
1705 rel_offset = dynamic_info [DT_REL];
1706 rel_size = dynamic_info [DT_RELSZ];
1707 }
1708 else if (dynamic_info [DT_RELA])
1709 {
1710 rel_offset = dynamic_info [DT_RELA];
1711 rel_size = dynamic_info [DT_RELASZ];
1712 }
1713 else if (dynamic_info [DT_JMPREL])
1714 {
1715 rel_offset = dynamic_info [DT_JMPREL];
1716 rel_size = dynamic_info [DT_PLTRELSZ];
1717 }
1718
1719 if (rel_size)
1720 {
1721 printf
1722 (_("\nRelocation section at offset 0x%x contains %d bytes:\n"),
1723 rel_offset, rel_size);
1724
1725 dump_relocations (file, rel_offset - loadaddr, rel_size,
1726 dynamic_symbols, dynamic_strings);
1727 }
1728 else
1729 printf (_("\nThere are no dynamic relocations in this file.\n"));
1730 }
1731 else
1732 {
1733 Elf32_Internal_Shdr * section;
1734 unsigned long i;
1735 int found = 0;
1736
1737 assert (string_table != NULL);
1738
1739 for (i = 0, section = section_headers;
1740 i < elf_header.e_shnum;
1741 i++, section ++)
1742 {
1743 if ( section->sh_type != SHT_RELA
1744 && section->sh_type != SHT_REL)
1745 continue;
1746
1747 rel_offset = section->sh_offset;
1748 rel_size = section->sh_size;
1749
1750 if (rel_size)
1751 {
1752 Elf32_Internal_Shdr * strsec;
1753 Elf32_Internal_Shdr * symsec;
1754 Elf_Internal_Sym * symtab;
1755 char * strtab;
1756
1757 printf
1758 (_("\nRelocation section '%s' at offset 0x%x contains %d entries:\n"),
1759 SECTION_NAME (section), rel_offset,
1760 rel_size / section->sh_entsize);
1761
1762 symsec = section_headers + section->sh_link;
1763
1764 symtab = get_elf_symbols (file, symsec->sh_offset,
1765 symsec->sh_size / symsec->sh_entsize);
1766
1767 if (symtab == NULL)
1768 continue;
1769
1770 strsec = section_headers + symsec->sh_link;
1771
1772 GET_DATA_ALLOC (strsec->sh_offset, strsec->sh_size, strtab,
1773 char *, "string table");
1774
1775 dump_relocations (file, rel_offset, rel_size, symtab, strtab);
1776
1777 free (strtab);
1778 free (symtab);
1779
1780 found = 1;
1781 }
1782 }
1783
1784 if (! found)
1785 printf (_("\nThere are no relocations in this file.\n"));
1786 }
1787
1788 return 1;
1789 }
1790
1791
1792 static void
1793 dynamic_segment_mips_val (entry)
1794 Elf_Internal_Dyn *entry;
1795 {
1796 switch (entry->d_tag)
1797 {
1798 case DT_MIPS_LOCAL_GOTNO:
1799 case DT_MIPS_CONFLICTNO:
1800 case DT_MIPS_LIBLISTNO:
1801 case DT_MIPS_SYMTABNO:
1802 case DT_MIPS_UNREFEXTNO:
1803 case DT_MIPS_HIPAGENO:
1804 case DT_MIPS_DELTA_CLASS_NO:
1805 case DT_MIPS_DELTA_INSTANCE_NO:
1806 case DT_MIPS_DELTA_RELOC_NO:
1807 case DT_MIPS_DELTA_SYM_NO:
1808 case DT_MIPS_DELTA_CLASSSYM_NO:
1809 if (do_dynamic)
1810 printf ("%#ld\n", (long) entry->d_un.d_ptr);
1811 break;
1812 default:
1813 if (do_dynamic)
1814 printf ("%#lx\n", (long) entry->d_un.d_ptr);
1815 }
1816 }
1817
1818 /* Parse the dynamic segment */
1819 static int
1820 process_dynamic_segment (file)
1821 FILE * file;
1822 {
1823 Elf_Internal_Dyn * entry;
1824 Elf32_External_Dyn * edyn;
1825 unsigned int i;
1826
1827 if (dynamic_size == 0)
1828 {
1829 if (do_dynamic)
1830 printf (_("\nThere is no dynamic segment in this file.\n"));
1831
1832 return 1;
1833 }
1834
1835 GET_DATA_ALLOC (dynamic_addr, dynamic_size,
1836 edyn, Elf32_External_Dyn *, "dynamic segment");
1837
1838 /* SGI's ELF has more than one section in the DYNAMIC segment. Determine
1839 how large .dynamic is now. We can do this even before the byte
1840 swapping since the DT_NULL tag is recognizable. */
1841 dynamic_size = 0;
1842 while (*(Elf32_Word *) edyn[dynamic_size++].d_tag != DT_NULL)
1843 ;
1844
1845 dynamic_segment = (Elf_Internal_Dyn *)
1846 malloc (dynamic_size * sizeof (Elf_Internal_Dyn));
1847
1848 if (dynamic_segment == NULL)
1849 {
1850 error (_("Out of memory\n"));
1851 free (edyn);
1852 return 0;
1853 }
1854
1855 for (i = 0, entry = dynamic_segment;
1856 i < dynamic_size;
1857 i ++, entry ++)
1858 {
1859 entry->d_tag = BYTE_GET (edyn [i].d_tag);
1860 entry->d_un.d_val = BYTE_GET (edyn [i].d_un.d_val);
1861 }
1862
1863 free (edyn);
1864
1865 /* Find the appropriate symbol table. */
1866 if (dynamic_symbols == NULL)
1867 {
1868 for (i = 0, entry = dynamic_segment;
1869 i < dynamic_size;
1870 ++i, ++ entry)
1871 {
1872 unsigned long offset;
1873 long num_syms;
1874
1875 if (entry->d_tag != DT_SYMTAB)
1876 continue;
1877
1878 dynamic_info [DT_SYMTAB] = entry->d_un.d_val;
1879
1880 /* Since we do not know how big the symbol table is,
1881 we default to reading in the entire file (!) and
1882 processing that. This is overkill, I know, but it
1883 should work. */
1884
1885 offset = entry->d_un.d_val - loadaddr;
1886
1887 if (fseek (file, 0, SEEK_END))
1888 error (_("Unable to seek to end of file!"));
1889
1890 num_syms = (ftell (file) - offset) / sizeof (Elf32_External_Sym);
1891
1892 if (num_syms < 1)
1893 {
1894 error (_("Unable to determine the number of symbols to load\n"));
1895 continue;
1896 }
1897
1898 dynamic_symbols = get_elf_symbols (file, offset, num_syms);
1899 }
1900 }
1901
1902 /* Similarly find a string table. */
1903 if (dynamic_strings == NULL)
1904 {
1905 for (i = 0, entry = dynamic_segment;
1906 i < dynamic_size;
1907 ++i, ++ entry)
1908 {
1909 unsigned long offset;
1910 long str_tab_len;
1911
1912 if (entry->d_tag != DT_STRTAB)
1913 continue;
1914
1915 dynamic_info [DT_STRTAB] = entry->d_un.d_val;
1916
1917 /* Since we do not know how big the string table is,
1918 we default to reading in the entire file (!) and
1919 processing that. This is overkill, I know, but it
1920 should work. */
1921
1922 offset = entry->d_un.d_val - loadaddr;
1923 if (fseek (file, 0, SEEK_END))
1924 error (_("Unable to seek to end of file\n"));
1925 str_tab_len = ftell (file) - offset;
1926
1927 if (str_tab_len < 1)
1928 {
1929 error
1930 (_("Unable to determine the length of the dynamic string table\n"));
1931 continue;
1932 }
1933
1934 GET_DATA_ALLOC (offset, str_tab_len, dynamic_strings, char *,
1935 "dynamic string table");
1936
1937 break;
1938 }
1939 }
1940
1941 if (do_dynamic && dynamic_addr)
1942 printf (_("\nDynamic segment at offset 0x%x contains %d entries:\n"),
1943 dynamic_addr, dynamic_size);
1944 if (do_dynamic)
1945 printf (_(" Tag Type Name/Value\n"));
1946
1947 for (i = 0, entry = dynamic_segment;
1948 i < dynamic_size;
1949 i++, entry ++)
1950 {
1951 if (do_dynamic)
1952 printf (_(" 0x%-8.8lx (%s)%*s"),
1953 (unsigned long) entry->d_tag,
1954 get_dynamic_type (entry->d_tag),
1955 27 - strlen (get_dynamic_type (entry->d_tag)),
1956 " ");
1957
1958 switch (entry->d_tag)
1959 {
1960 case DT_AUXILIARY:
1961 case DT_FILTER:
1962 if (do_dynamic)
1963 {
1964 if (entry->d_tag == DT_AUXILIARY)
1965 printf (_("Auxiliary library"));
1966 else
1967 printf (_("Filter library"));
1968
1969 if (dynamic_strings)
1970 printf (": [%s]\n", dynamic_strings + entry->d_un.d_val);
1971 else
1972 printf (": %#lx\n", (long) entry->d_un.d_val);
1973 }
1974 break;
1975
1976 case DT_NULL :
1977 case DT_NEEDED :
1978 case DT_PLTRELSZ:
1979 case DT_PLTGOT :
1980 case DT_HASH :
1981 case DT_STRTAB :
1982 case DT_SYMTAB :
1983 case DT_RELA :
1984 case DT_RELASZ :
1985 case DT_RELAENT :
1986 case DT_STRSZ :
1987 case DT_SYMENT :
1988 case DT_INIT :
1989 case DT_FINI :
1990 case DT_SONAME :
1991 case DT_RPATH :
1992 case DT_SYMBOLIC:
1993 case DT_REL :
1994 case DT_RELSZ :
1995 case DT_RELENT :
1996 case DT_PLTREL :
1997 case DT_DEBUG :
1998 case DT_TEXTREL :
1999 case DT_JMPREL :
2000 dynamic_info [entry->d_tag] = entry->d_un.d_val;
2001
2002 if (do_dynamic)
2003 {
2004 char * name;
2005
2006 if (dynamic_strings == NULL)
2007 name = NULL;
2008 else
2009 name = dynamic_strings + entry->d_un.d_val;
2010
2011 if (name)
2012 {
2013 switch (entry->d_tag)
2014 {
2015 case DT_NEEDED:
2016 printf (_("Shared library: [%s]"), name);
2017
2018 if (strcmp (name, program_interpreter))
2019 printf ("\n");
2020 else
2021 printf (_(" program interpreter\n"));
2022 break;
2023
2024 case DT_SONAME:
2025 printf (_("Library soname: [%s]\n"), name);
2026 break;
2027
2028 case DT_RPATH:
2029 printf (_("Library rpath: [%s]\n"), name);
2030 break;
2031
2032 default:
2033 printf ("%#lx\n", (long) entry->d_un.d_val);
2034 }
2035 }
2036 else
2037 printf ("%#lx\n", (long) entry->d_un.d_val);
2038 }
2039 break;
2040
2041 default:
2042 if ((entry->d_tag >= DT_VERSYM) && (entry->d_tag <= DT_VERNEEDNUM))
2043 {
2044 version_info [DT_VERSIONTAGIDX (entry->d_tag)] =
2045 entry->d_un.d_val;
2046
2047 if (do_dynamic)
2048 printf ("%#lx\n", (long) entry->d_un.d_ptr);
2049 }
2050 else
2051 switch (elf_header.e_machine)
2052 {
2053 case EM_MIPS:
2054 case EM_MIPS_RS4_BE:
2055 dynamic_segment_mips_val (entry);
2056 break;
2057 default:
2058 if (do_dynamic)
2059 printf ("%#lx\n", (long) entry->d_un.d_ptr);
2060 }
2061 break;
2062 }
2063 }
2064
2065 return 1;
2066 }
2067
2068 static char *
2069 get_ver_flags (flags)
2070 unsigned int flags;
2071 {
2072 static char buff [32];
2073
2074 buff[0] = 0;
2075
2076 if (flags == 0)
2077 return _("none");
2078
2079 if (flags & VER_FLG_BASE)
2080 strcat (buff, "BASE ");
2081
2082 if (flags & VER_FLG_WEAK)
2083 {
2084 if (flags & VER_FLG_BASE)
2085 strcat (buff, "| ");
2086
2087 strcat (buff, "WEAK ");
2088 }
2089
2090 if (flags & ~(VER_FLG_BASE | VER_FLG_WEAK))
2091 strcat (buff, "| <unknown>");
2092
2093 return buff;
2094 }
2095
2096 /* Display the contents of the version sections. */
2097 static int
2098 process_version_sections (file)
2099 FILE * file;
2100 {
2101 Elf32_Internal_Shdr * section;
2102 unsigned i;
2103 int found = 0;
2104
2105 if (! do_version)
2106 return 1;
2107
2108 for (i = 0, section = section_headers;
2109 i < elf_header.e_shnum;
2110 i++, section ++)
2111 {
2112 switch (section->sh_type)
2113 {
2114 case SHT_GNU_verdef:
2115 {
2116 Elf_External_Verdef * edefs;
2117 unsigned int idx;
2118 unsigned int cnt;
2119
2120 found = 1;
2121
2122 printf
2123 (_("\nVersion definition section '%s' contains %d entries:\n"),
2124 SECTION_NAME (section), section->sh_info);
2125
2126 printf (_(" Addr: %#08x Offset: %#08x Link: %x (%s)\n"),
2127 section->sh_addr, section->sh_offset, section->sh_link,
2128 SECTION_NAME (section_headers + section->sh_link));
2129
2130 GET_DATA_ALLOC (section->sh_offset, section->sh_size,
2131 edefs, Elf_External_Verdef *,
2132 "version definition section");
2133
2134 for (idx = cnt = 0; cnt < section->sh_info; ++ cnt)
2135 {
2136 char * vstart;
2137 Elf_External_Verdef * edef;
2138 Elf_Internal_Verdef ent;
2139 Elf_External_Verdaux * eaux;
2140 Elf_Internal_Verdaux aux;
2141 int j;
2142 int isum;
2143
2144 vstart = ((char *) edefs) + idx;
2145
2146 edef = (Elf_External_Verdef *) vstart;
2147
2148 ent.vd_version = BYTE_GET (edef->vd_version);
2149 ent.vd_flags = BYTE_GET (edef->vd_flags);
2150 ent.vd_ndx = BYTE_GET (edef->vd_ndx);
2151 ent.vd_cnt = BYTE_GET (edef->vd_cnt);
2152 ent.vd_hash = BYTE_GET (edef->vd_hash);
2153 ent.vd_aux = BYTE_GET (edef->vd_aux);
2154 ent.vd_next = BYTE_GET (edef->vd_next);
2155
2156 printf (_(" %#06x: Rev: %d Flags: %s"),
2157 idx, ent.vd_version, get_ver_flags (ent.vd_flags));
2158
2159 printf (_(" Index: %ld Cnt: %ld "), ent.vd_ndx, ent.vd_cnt);
2160
2161 vstart += ent.vd_aux;
2162
2163 eaux = (Elf_External_Verdaux *) vstart;
2164
2165 aux.vda_name = BYTE_GET (eaux->vda_name);
2166 aux.vda_next = BYTE_GET (eaux->vda_next);
2167
2168 if (dynamic_strings)
2169 printf (_("Name: %s\n"), dynamic_strings + aux.vda_name);
2170 else
2171 printf (_("Name index: %ld\n"), aux.vda_name);
2172
2173 isum = idx + ent.vd_aux;
2174
2175 for (j = 1; j < ent.vd_cnt; j ++)
2176 {
2177 isum += aux.vda_next;
2178 vstart += aux.vda_next;
2179
2180 eaux = (Elf_External_Verdaux *) vstart;
2181
2182 aux.vda_name = BYTE_GET (eaux->vda_name);
2183 aux.vda_next = BYTE_GET (eaux->vda_next);
2184
2185 if (dynamic_strings)
2186 printf (_(" %#06x: Parent %d: %s\n"),
2187 isum, j, dynamic_strings + aux.vda_name);
2188 else
2189 printf (_(" %#06x: Parent %d, name index: %ld\n"),
2190 isum, j, aux.vda_name);
2191 }
2192
2193 idx += ent.vd_next;
2194 }
2195
2196 free (edefs);
2197 }
2198 break;
2199
2200 case SHT_GNU_verneed:
2201 {
2202 Elf_External_Verneed * eneed;
2203 unsigned int idx;
2204 unsigned int cnt;
2205
2206 found = 1;
2207
2208 printf (_("\nVersion needs section '%s' contains %d entries:\n"),
2209 SECTION_NAME (section), section->sh_info);
2210
2211 printf
2212 (_(" Addr: %#08x Offset: %#08x Link to section: %d (%s)\n"),
2213 section->sh_addr, section->sh_offset, section->sh_link,
2214 SECTION_NAME (section_headers + section->sh_link));
2215
2216 GET_DATA_ALLOC (section->sh_offset, section->sh_size,
2217 eneed, Elf_External_Verneed *,
2218 "version need section");
2219
2220 for (idx = cnt = 0; cnt < section->sh_info; ++cnt)
2221 {
2222 Elf_External_Verneed * entry;
2223 Elf_Internal_Verneed ent;
2224 int j;
2225 int isum;
2226 char * vstart;
2227
2228 vstart = ((char *) eneed) + idx;
2229
2230 entry = (Elf_External_Verneed *) vstart;
2231
2232 ent.vn_version = BYTE_GET (entry->vn_version);
2233 ent.vn_cnt = BYTE_GET (entry->vn_cnt);
2234 ent.vn_file = BYTE_GET (entry->vn_file);
2235 ent.vn_aux = BYTE_GET (entry->vn_aux);
2236 ent.vn_next = BYTE_GET (entry->vn_next);
2237
2238 printf (_(" %#06x: Version: %d"), idx, ent.vn_version);
2239
2240 if (dynamic_strings)
2241 printf (_(" File: %s"), dynamic_strings + ent.vn_file);
2242 else
2243 printf (_(" File: %lx"), ent.vn_file);
2244
2245 printf (_(" Cnt: %d\n"), ent.vn_cnt);
2246
2247 vstart += ent.vn_aux;
2248
2249 for (j = 0, isum = idx + ent.vn_aux; j < ent.vn_cnt; ++j)
2250 {
2251 Elf_External_Vernaux * eaux;
2252 Elf_Internal_Vernaux aux;
2253
2254 eaux = (Elf_External_Vernaux *) vstart;
2255
2256 aux.vna_hash = BYTE_GET (eaux->vna_hash);
2257 aux.vna_flags = BYTE_GET (eaux->vna_flags);
2258 aux.vna_other = BYTE_GET (eaux->vna_other);
2259 aux.vna_name = BYTE_GET (eaux->vna_name);
2260 aux.vna_next = BYTE_GET (eaux->vna_next);
2261
2262 if (dynamic_strings)
2263 printf (_(" %#06x: Name: %s"),
2264 isum, dynamic_strings + aux.vna_name);
2265 else
2266 printf (_(" %#06x: Name index: %lx"),
2267 isum, aux.vna_name);
2268
2269 printf (_(" Flags: %s Version: %d\n"),
2270 get_ver_flags (aux.vna_flags), aux.vna_other);
2271
2272 isum += aux.vna_next;
2273 vstart += aux.vna_next;
2274 }
2275
2276 idx += ent.vn_next;
2277 }
2278
2279 free (eneed);
2280 }
2281 break;
2282
2283 case SHT_GNU_versym:
2284 {
2285 Elf32_Internal_Shdr * link_section;
2286 int total;
2287 int cnt;
2288 unsigned char * edata;
2289 unsigned short * data;
2290 char * strtab;
2291 Elf_Internal_Sym * symbols;
2292 Elf32_Internal_Shdr * string_sec;
2293
2294 link_section = section_headers + section->sh_link;
2295 total = section->sh_size / section->sh_entsize;
2296
2297 found = 1;
2298
2299 symbols = get_elf_symbols
2300 (file, link_section->sh_offset,
2301 link_section->sh_size / link_section->sh_entsize);
2302
2303 string_sec = section_headers + link_section->sh_link;
2304
2305 GET_DATA_ALLOC (string_sec->sh_offset, string_sec->sh_size,
2306 strtab, char *, "version string table");
2307
2308 printf (_("\nVersion symbols section '%s' contains %d entries:\n"),
2309 SECTION_NAME (section), total);
2310
2311 printf (_(" Addr: %#08x Offset: %#08x Link: %x (%s)\n"),
2312 section->sh_addr, section->sh_offset, section->sh_link,
2313 SECTION_NAME (link_section));
2314
2315 GET_DATA_ALLOC (version_info [DT_VERSIONTAGIDX (DT_VERSYM)]
2316 - loadaddr,
2317 total * sizeof (short), edata,
2318 char *, "version symbol data");
2319
2320 data = (unsigned short *) malloc (total * sizeof (short));
2321
2322 for (cnt = total; cnt --;)
2323 data [cnt] = byte_get (edata + cnt * sizeof (short), sizeof (short));
2324
2325 free (edata);
2326
2327 for (cnt = 0; cnt < total; cnt += 4)
2328 {
2329 int j, nn;
2330
2331 printf (" %03x:", cnt);
2332
2333 for (j = 0; (j < 4) && (cnt + j) < total; ++j)
2334 switch (data [cnt + j])
2335 {
2336 case 0:
2337 fputs (_(" 0 (*local*) "), stdout);
2338 break;
2339
2340 case 1:
2341 fputs (_(" 1 (*global*) "), stdout);
2342 break;
2343
2344 default:
2345 nn = printf ("%4x%c", data [cnt + j] & 0x7fff,
2346 data [cnt + j] & 0x8000 ? 'h' : ' ');
2347
2348 if (symbols [cnt + j].st_shndx < SHN_LORESERVE
2349 && section_headers[symbols [cnt + j].st_shndx].sh_type
2350 == SHT_NOBITS)
2351 {
2352 /* We must test both. */
2353 Elf_Internal_Verneed ivn;
2354 unsigned long offset;
2355
2356 offset = version_info [DT_VERSIONTAGIDX (DT_VERNEED)]
2357 - loadaddr;
2358
2359 do
2360 {
2361 Elf_External_Verneed evn;
2362 Elf_External_Vernaux evna;
2363 Elf_Internal_Vernaux ivna;
2364 unsigned long vna_off;
2365
2366 GET_DATA (offset, evn, "version need");
2367
2368 ivn.vn_aux = BYTE_GET (evn.vn_aux);
2369 ivn.vn_next = BYTE_GET (evn.vn_next);
2370
2371 vna_off = offset + ivn.vn_aux;
2372
2373 do
2374 {
2375 GET_DATA (vna_off, evna,
2376 "version need aux (1)");
2377
2378 ivna.vna_next = BYTE_GET (evna.vna_next);
2379 ivna.vna_other = BYTE_GET (evna.vna_other);
2380
2381 vna_off += ivna.vna_next;
2382 }
2383 while (ivna.vna_other != data [cnt + j]
2384 && ivna.vna_next != 0);
2385
2386 if (ivna.vna_other == data [cnt + j])
2387 {
2388 ivna.vna_name = BYTE_GET (evna.vna_name);
2389
2390 nn += printf ("(%s%-*s",
2391 strtab + ivna.vna_name,
2392 12 - strlen (strtab
2393 + ivna.vna_name),
2394 ")");
2395 break;
2396 }
2397 else if (ivn.vn_next == 0)
2398 {
2399 if (data [cnt + j] != 0x8001)
2400 {
2401 Elf_Internal_Verdef ivd;
2402 Elf_External_Verdef evd;
2403
2404 offset = version_info
2405 [DT_VERSIONTAGIDX (DT_VERDEF)]
2406 - loadaddr;
2407
2408 do
2409 {
2410 GET_DATA (offset, evd,
2411 "version definition");
2412
2413 ivd.vd_next = BYTE_GET (evd.vd_next);
2414 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
2415
2416 offset += ivd.vd_next;
2417 }
2418 while (ivd.vd_ndx
2419 != (data [cnt + j] & 0x7fff)
2420 && ivd.vd_next != 0);
2421
2422 if (ivd.vd_ndx
2423 == (data [cnt + j] & 0x7fff))
2424 {
2425 Elf_External_Verdaux evda;
2426 Elf_Internal_Verdaux ivda;
2427
2428 ivd.vd_aux = BYTE_GET (evd.vd_aux);
2429
2430 GET_DATA (offset + ivd.vd_aux, evda,
2431 "version definition aux");
2432
2433 ivda.vda_name =
2434 BYTE_GET (evda.vda_name);
2435
2436 nn +=
2437 printf ("(%s%-*s",
2438 strtab + ivda.vda_name,
2439 12
2440 - strlen (strtab
2441 + ivda.vda_name),
2442 ")");
2443 }
2444 }
2445
2446 break;
2447 }
2448 else
2449 offset += ivn.vn_next;
2450 }
2451 while (ivn.vn_next);
2452 }
2453 else if (symbols [cnt + j].st_shndx == SHN_UNDEF)
2454 {
2455 Elf_Internal_Verneed ivn;
2456 unsigned long offset;
2457
2458 offset = version_info [DT_VERSIONTAGIDX (DT_VERNEED)]
2459 - loadaddr;
2460
2461 do
2462 {
2463 Elf_Internal_Vernaux ivna;
2464 Elf_External_Verneed evn;
2465 Elf_External_Vernaux evna;
2466 unsigned long a_off;
2467
2468 GET_DATA (offset, evn, "version need");
2469
2470 ivn.vn_aux = BYTE_GET (evn.vn_aux);
2471 ivn.vn_next = BYTE_GET (evn.vn_next);
2472
2473 a_off = offset + ivn.vn_aux;
2474
2475 do
2476 {
2477 GET_DATA (a_off, evna,
2478 "version need aux (2)");
2479
2480 ivna.vna_next = BYTE_GET (evna.vna_next);
2481 ivna.vna_other = BYTE_GET (evna.vna_other);
2482
2483 a_off += ivna.vna_next;
2484 }
2485 while (ivna.vna_other != data [cnt + j]
2486 && ivna.vna_next != 0);
2487
2488 if (ivna.vna_other == data [cnt + j])
2489 {
2490 ivna.vna_name = BYTE_GET (evna.vna_name);
2491
2492 nn += printf ("(%s%-*s",
2493 strtab + ivna.vna_name,
2494 12 - strlen (strtab
2495 + ivna.vna_name),
2496 ")");
2497 break;
2498 }
2499
2500 offset += ivn.vn_next;
2501 }
2502 while (ivn.vn_next);
2503 }
2504 else if (data [cnt + j] != 0x8001)
2505 {
2506 Elf_Internal_Verdef ivd;
2507 Elf_External_Verdef evd;
2508 unsigned long offset;
2509
2510 offset = version_info
2511 [DT_VERSIONTAGIDX (DT_VERDEF)] - loadaddr;
2512
2513 do
2514 {
2515 GET_DATA (offset, evd, "version def");
2516
2517 ivd.vd_next = BYTE_GET (evd.vd_next);
2518 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
2519
2520 offset += ivd.vd_next;
2521 }
2522 while (ivd.vd_ndx != (data [cnt + j] & 0x7fff)
2523 && ivd.vd_next != 0);
2524
2525 if (ivd.vd_ndx == (data [cnt + j] & 0x7fff))
2526 {
2527 Elf_External_Verdaux evda;
2528 Elf_Internal_Verdaux ivda;
2529
2530 ivd.vd_aux = BYTE_GET (evd.vd_aux);
2531
2532 GET_DATA (offset - ivd.vd_next + ivd.vd_aux,
2533 evda, "version def aux");
2534
2535 ivda.vda_name = BYTE_GET (evda.vda_name);
2536
2537 nn += printf ("(%s%-*s",
2538 strtab + ivda.vda_name,
2539 12 - strlen (strtab
2540 + ivda.vda_name),
2541 ")");
2542 }
2543 }
2544
2545 if (nn < 18)
2546 printf ("%*c", 18 - nn, ' ');
2547 }
2548
2549 putchar ('\n');
2550 }
2551
2552 free (data);
2553 free (strtab);
2554 free (symbols);
2555 }
2556 break;
2557
2558 default:
2559 break;
2560 }
2561 }
2562
2563 if (! found)
2564 printf (_("\nNo version information found in this file.\n"));
2565
2566 return 1;
2567 }
2568
2569 static char *
2570 get_symbol_binding (binding)
2571 unsigned int binding;
2572 {
2573 static char buff [32];
2574
2575 switch (binding)
2576 {
2577 case STB_LOCAL: return _("LOCAL");
2578 case STB_GLOBAL: return _("GLOBAL");
2579 case STB_WEAK: return _("WEAK");
2580 default:
2581 if (binding >= STB_LOPROC && binding <= STB_HIPROC)
2582 sprintf (buff, _("<processor specific>: %d"), binding);
2583 else
2584 sprintf (buff, _("<unknown>: %d"), binding);
2585 return buff;
2586 }
2587 }
2588
2589 static char *
2590 get_symbol_type (type)
2591 unsigned int type;
2592 {
2593 static char buff [32];
2594
2595 switch (type)
2596 {
2597 case STT_NOTYPE: return _("NOTYPE");
2598 case STT_OBJECT: return _("OBJECT");
2599 case STT_FUNC: return _("FUNC");
2600 case STT_SECTION: return _("SECTION");
2601 case STT_FILE: return _("FILE");
2602 default:
2603 if (type >= STT_LOPROC && type <= STT_HIPROC)
2604 sprintf (buff, _("<processor specific>: %d"), type);
2605 else
2606 sprintf (buff, _("<unknown>: %d"), type);
2607 return buff;
2608 }
2609 }
2610
2611 static char *
2612 get_symbol_index_type (type)
2613 unsigned int type;
2614 {
2615 switch (type)
2616 {
2617 case SHN_UNDEF: return "UND";
2618 case SHN_ABS: return "ABS";
2619 case SHN_COMMON: return "COM";
2620 default:
2621 if (type >= SHN_LOPROC && type <= SHN_HIPROC)
2622 return "PRC";
2623 else if (type >= SHN_LORESERVE && type <= SHN_HIRESERVE)
2624 return "RSV";
2625 else
2626 {
2627 static char buff [32];
2628
2629 sprintf (buff, "%3d", type);
2630 return buff;
2631 }
2632 }
2633 }
2634
2635
2636 static int *
2637 get_dynamic_data (file, number)
2638 FILE * file;
2639 unsigned int number;
2640 {
2641 char * e_data;
2642 int * i_data;
2643
2644 e_data = (char *) malloc (number * 4);
2645
2646 if (e_data == NULL)
2647 {
2648 error (_("Out of memory\n"));
2649 return NULL;
2650 }
2651
2652 if (fread (e_data, 4, number, file) != number)
2653 {
2654 error (_("Unable to read in dynamic data\n"));
2655 return NULL;
2656 }
2657
2658 i_data = (int *) malloc (number * sizeof (* i_data));
2659
2660 if (i_data == NULL)
2661 {
2662 error (_("Out of memory\n"));
2663 free (e_data);
2664 return NULL;
2665 }
2666
2667 while (number--)
2668 i_data [number] = byte_get (e_data + number * 4, 4);
2669
2670 free (e_data);
2671
2672 return i_data;
2673 }
2674
2675 /* Dump the symbol table */
2676 static int
2677 process_symbol_table (file)
2678 FILE * file;
2679 {
2680 Elf32_Internal_Shdr * section;
2681
2682 if (! do_syms)
2683 return 1;
2684
2685 if (dynamic_info [DT_HASH] && do_using_dynamic && dynamic_strings != NULL)
2686 {
2687 char nb [4];
2688 char nc [4];
2689 int nbuckets;
2690 int nchains;
2691 int * buckets;
2692 int * chains;
2693 int hn;
2694 int si;
2695
2696 if (fseek (file, dynamic_info [DT_HASH] - loadaddr, SEEK_SET))
2697 {
2698 error (_("Unable to seek to start of dynamic information"));
2699 return 0;
2700 }
2701
2702 if (fread (& nb, sizeof (nb), 1, file) != 1)
2703 {
2704 error (_("Failed to read in number of buckets\n"));
2705 return 0;
2706 }
2707
2708 if (fread (& nc, sizeof (nc), 1, file) != 1)
2709 {
2710 error (_("Failed to read in number of chains\n"));
2711 return 0;
2712 }
2713
2714 nbuckets = byte_get (nb, 4);
2715 nchains = byte_get (nc, 4);
2716
2717 buckets = get_dynamic_data (file, nbuckets);
2718 chains = get_dynamic_data (file, nchains);
2719
2720 if (buckets == NULL || chains == NULL)
2721 return 0;
2722
2723 printf (_("\nSymbol table for image:\n"));
2724 printf (_(" Num Buc: Value Size Type Bind Ot Ndx Name\n"));
2725
2726 for (hn = 0; hn < nbuckets; hn++)
2727 {
2728 if (! buckets [hn])
2729 continue;
2730
2731 for (si = buckets [hn]; si; si = chains [si])
2732 {
2733 Elf_Internal_Sym * psym;
2734
2735 psym = dynamic_symbols + si;
2736
2737 printf (" %3d %3d: %8lx %5ld %6s %6s %2d ",
2738 si, hn,
2739 (unsigned long) psym->st_value,
2740 (unsigned long) psym->st_size,
2741 get_symbol_type (ELF_ST_TYPE (psym->st_info)),
2742 get_symbol_binding (ELF_ST_BIND (psym->st_info)),
2743 psym->st_other);
2744
2745 printf ("%3.3s", get_symbol_index_type (psym->st_shndx));
2746
2747 printf (" %s\n", dynamic_strings + psym->st_name);
2748 }
2749 }
2750
2751 free (buckets);
2752 free (chains);
2753 }
2754 else if (!do_using_dynamic)
2755 {
2756 unsigned int i;
2757
2758 for (i = 0, section = section_headers;
2759 i < elf_header.e_shnum;
2760 i++, section++)
2761 {
2762 unsigned int si;
2763 char * strtab;
2764 Elf_Internal_Sym * symtab;
2765 Elf_Internal_Sym * psym;
2766
2767
2768 if ( section->sh_type != SHT_SYMTAB
2769 && section->sh_type != SHT_DYNSYM)
2770 continue;
2771
2772 printf (_("\nSymbol table '%s' contains %d entries:\n"),
2773 SECTION_NAME (section),
2774 section->sh_size / section->sh_entsize);
2775 fputs (_(" Num: Value Size Type Bind Ot Ndx Name\n"),
2776 stdout);
2777
2778 symtab = get_elf_symbols (file, section->sh_offset,
2779 section->sh_size / section->sh_entsize);
2780 if (symtab == NULL)
2781 continue;
2782
2783 if (section->sh_link == elf_header.e_shstrndx)
2784 strtab = string_table;
2785 else
2786 {
2787 Elf32_Internal_Shdr * string_sec;
2788
2789 string_sec = section_headers + section->sh_link;
2790
2791 GET_DATA_ALLOC (string_sec->sh_offset, string_sec->sh_size,
2792 strtab, char *, "string table");
2793 }
2794
2795 for (si = 0, psym = symtab;
2796 si < section->sh_size / section->sh_entsize;
2797 si ++, psym ++)
2798 {
2799 printf (" %3d: %8lx %5ld %-7s %-6s %2d ",
2800 si,
2801 (unsigned long) psym->st_value,
2802 (unsigned long) psym->st_size,
2803 get_symbol_type (ELF_ST_TYPE (psym->st_info)),
2804 get_symbol_binding (ELF_ST_BIND (psym->st_info)),
2805 psym->st_other);
2806
2807 if (psym->st_shndx == 0)
2808 fputs (" UND", stdout);
2809 else if ((psym->st_shndx & 0xffff) == 0xfff1)
2810 fputs (" ABS", stdout);
2811 else if ((psym->st_shndx & 0xffff) == 0xfff2)
2812 fputs (" COM", stdout);
2813 else
2814 printf ("%4x", psym->st_shndx);
2815
2816 printf (" %s", strtab + psym->st_name);
2817
2818 if (section->sh_type == SHT_DYNSYM &&
2819 version_info [DT_VERSIONTAGIDX (DT_VERSYM)] != 0)
2820 {
2821 unsigned char data[2];
2822 unsigned short vers_data;
2823 unsigned long offset;
2824 int is_nobits;
2825 int check_def;
2826
2827 offset = version_info [DT_VERSIONTAGIDX (DT_VERSYM)]
2828 - loadaddr;
2829
2830 GET_DATA (offset + si * sizeof (vers_data), data,
2831 "version data");
2832
2833 vers_data = byte_get (data, 2);
2834
2835 is_nobits = psym->st_shndx < SHN_LORESERVE ?
2836 (section_headers [psym->st_shndx].sh_type == SHT_NOBITS)
2837 : 0;
2838
2839 check_def = (psym->st_shndx != SHN_UNDEF);
2840
2841 if ((vers_data & 0x8000) || vers_data > 1)
2842 {
2843 if (is_nobits || ! check_def)
2844 {
2845 Elf_External_Verneed evn;
2846 Elf_Internal_Verneed ivn;
2847 Elf_Internal_Vernaux ivna;
2848
2849 /* We must test both. */
2850 offset = version_info
2851 [DT_VERSIONTAGIDX (DT_VERNEED)] - loadaddr;
2852
2853 GET_DATA (offset, evn, "version need");
2854
2855 ivn.vn_aux = BYTE_GET (evn.vn_aux);
2856 ivn.vn_next = BYTE_GET (evn.vn_next);
2857
2858 do
2859 {
2860 unsigned long vna_off;
2861
2862 vna_off = offset + ivn.vn_aux;
2863
2864 do
2865 {
2866 Elf_External_Vernaux evna;
2867
2868 GET_DATA (vna_off, evna,
2869 "version need aux (3)");
2870
2871 ivna.vna_other = BYTE_GET (evna.vna_other);
2872 ivna.vna_next = BYTE_GET (evna.vna_next);
2873 ivna.vna_name = BYTE_GET (evna.vna_name);
2874
2875 vna_off += ivna.vna_next;
2876 }
2877 while (ivna.vna_other != vers_data
2878 && ivna.vna_next != 0);
2879
2880 if (ivna.vna_other == vers_data)
2881 break;
2882
2883 offset += ivn.vn_next;
2884 }
2885 while (ivn.vn_next != 0);
2886
2887 if (ivna.vna_other == vers_data)
2888 {
2889 printf ("@%s (%d)",
2890 strtab + ivna.vna_name, ivna.vna_other);
2891 check_def = 0;
2892 }
2893 else if (! is_nobits)
2894 error (_("bad dynamic symbol"));
2895 else
2896 check_def = 1;
2897 }
2898
2899 if (check_def)
2900 {
2901 if (vers_data != 0x8001)
2902 {
2903 Elf_Internal_Verdef ivd;
2904 Elf_Internal_Verdaux ivda;
2905 Elf_External_Verdaux evda;
2906 unsigned long offset;
2907
2908 offset =
2909 version_info [DT_VERSIONTAGIDX (DT_VERDEF)]
2910 - loadaddr;
2911
2912 do
2913 {
2914 Elf_External_Verdef evd;
2915
2916 GET_DATA (offset, evd, "version def");
2917
2918 ivd.vd_ndx = BYTE_GET (evd.vd_ndx);
2919 ivd.vd_aux = BYTE_GET (evd.vd_aux);
2920 ivd.vd_next = BYTE_GET (evd.vd_next);
2921
2922 offset += ivd.vd_next;
2923 }
2924 while (ivd.vd_ndx != (vers_data & 0x7fff)
2925 && ivd.vd_next != 0);
2926
2927 offset -= ivd.vd_next;
2928 offset += ivd.vd_aux;
2929
2930 GET_DATA (offset, evda, "version def aux");
2931
2932 ivda.vda_name = BYTE_GET (evda.vda_name);
2933
2934 if (psym->st_name != ivda.vda_name)
2935 printf ((vers_data & 0x8000)
2936 ? "@%s" : "@@%s",
2937 strtab + ivda.vda_name);
2938 }
2939 }
2940 }
2941 }
2942
2943 putchar ('\n');
2944 }
2945
2946 free (symtab);
2947 if (strtab != string_table)
2948 free (strtab);
2949 }
2950 }
2951 else
2952 printf
2953 (_("\nDynamic symbol information is not available for displaying symbols.\n"));
2954
2955 return 1;
2956 }
2957
2958 static int
2959 process_section_contents (file)
2960 FILE * file;
2961 {
2962 Elf32_Internal_Shdr * section;
2963 unsigned int i;
2964
2965 if (! do_dump)
2966 return 1;
2967
2968 for (i = 0, section = section_headers;
2969 i < elf_header.e_shnum;
2970 i ++, section ++)
2971 {
2972 #ifdef SUPPORT_DISASSEMBLY
2973 /* See if we need an assembly dump of this section */
2974
2975 if ((i < NUM_DUMP_SECTS) && (dump_sects[i] & DISASS_DUMP))
2976 {
2977 printf (_("\nAssembly dump of section %s\n"),
2978 SECTION_NAME (section));
2979
2980 /* XXX -- to be done --- XXX */
2981 }
2982 #endif
2983 /* See if we need a hex dump of this section. */
2984 if ((i < NUM_DUMP_SECTS) && (dump_sects[i] & HEX_DUMP))
2985 {
2986 int bytes;
2987 int addr;
2988 unsigned char * data;
2989 char * start;
2990
2991 printf (_("\nHex dump of section '%s':\n"), SECTION_NAME (section));
2992
2993 bytes = section->sh_size;
2994 addr = section->sh_addr;
2995
2996 GET_DATA_ALLOC (section->sh_offset, bytes, start, char *,
2997 "section data");
2998
2999 data = start;
3000
3001 while (bytes)
3002 {
3003 int j;
3004 int k;
3005 int lbytes;
3006
3007 lbytes = (bytes > 16 ? 16 : bytes);
3008
3009 printf (" 0x%8.8x ", addr);
3010
3011 switch (elf_header.e_ident [EI_DATA])
3012 {
3013 case ELFDATA2LSB:
3014 for (j = 15; j >= 0; j --)
3015 {
3016 if (j < lbytes)
3017 printf ("%2.2x", data [j]);
3018 else
3019 printf (" ");
3020
3021 if (!(j & 0x3))
3022 printf (" ");
3023 }
3024 break;
3025
3026 case ELFDATA2MSB:
3027 for (j = 0; j < 16; j++)
3028 {
3029 if (j < lbytes)
3030 printf ("%2.2x", data [j]);
3031 else
3032 printf (" ");
3033
3034 if ((j & 3) == 3)
3035 printf (" ");
3036 }
3037 break;
3038 }
3039
3040 for (j = 0; j < lbytes; j++)
3041 {
3042 k = data [j];
3043 if (k >= ' ' && k < 0x80)
3044 printf ("%c", k);
3045 else
3046 printf (".");
3047 }
3048
3049 putchar ('\n');
3050
3051 data += lbytes;
3052 addr += lbytes;
3053 bytes -= lbytes;
3054 }
3055
3056 free (start);
3057 }
3058 }
3059
3060 return 1;
3061 }
3062
3063 static void
3064 process_mips_fpe_exception (mask)
3065 int mask;
3066 {
3067 if (mask)
3068 {
3069 int first = 1;
3070 if (mask & OEX_FPU_INEX)
3071 fputs ("INEX", stdout), first = 0;
3072 if (mask & OEX_FPU_UFLO)
3073 printf ("%sUFLO", first ? "" : "|"), first = 0;
3074 if (mask & OEX_FPU_OFLO)
3075 printf ("%sOFLO", first ? "" : "|"), first = 0;
3076 if (mask & OEX_FPU_DIV0)
3077 printf ("%sDIV0", first ? "" : "|"), first = 0;
3078 if (mask & OEX_FPU_INVAL)
3079 printf ("%sINVAL", first ? "" : "|");
3080 }
3081 else
3082 fputs ("0", stdout);
3083 }
3084
3085 static int
3086 process_mips_specific (file)
3087 FILE *file;
3088 {
3089 Elf_Internal_Dyn * entry;
3090 size_t liblist_offset = 0;
3091 size_t liblistno = 0;
3092 size_t options_offset = 0;
3093
3094 /* We have a lot of special sections. Thanks SGI! */
3095 if (dynamic_segment == NULL)
3096 /* No information available. */
3097 return 0;
3098
3099 for (entry = dynamic_segment; entry->d_tag != DT_NULL; ++entry)
3100 switch (entry->d_tag)
3101 {
3102 case DT_MIPS_LIBLIST:
3103 liblist_offset = entry->d_un.d_val - loadaddr;
3104 break;
3105 case DT_MIPS_LIBLISTNO:
3106 liblistno = entry->d_un.d_val;
3107 break;
3108 case DT_MIPS_OPTIONS:
3109 options_offset = entry->d_un.d_val - loadaddr;
3110 break;
3111 default:
3112 break;
3113 }
3114
3115 if (liblist_offset != 0 && liblistno != 0 && do_dynamic)
3116 {
3117 Elf32_External_Lib *elib;
3118 size_t cnt;
3119
3120 GET_DATA_ALLOC (liblist_offset, liblistno * sizeof (Elf32_External_Lib),
3121 elib, Elf32_External_Lib *, "liblist");
3122
3123 printf ("\nSection '.liblist' contains %d entries:\n", liblistno);
3124 fputs (" Library Time Stamp Checksum Version Flags\n",
3125 stdout);
3126
3127 for (cnt = 0; cnt < liblistno; ++cnt)
3128 {
3129 Elf32_Lib liblist;
3130 time_t time;
3131 char timebuf[17];
3132
3133 liblist.l_name = BYTE_GET (elib[cnt].l_name);
3134 time = BYTE_GET (elib[cnt].l_time_stamp);
3135 liblist.l_checksum = BYTE_GET (elib[cnt].l_checksum);
3136 liblist.l_version = BYTE_GET (elib[cnt].l_version);
3137 liblist.l_flags = BYTE_GET (elib[cnt].l_flags);
3138
3139 strftime (timebuf, 17, "%Y-%m-%dT%H:%M", gmtime (&time));
3140
3141 printf ("%3d: %-20s %s %#10lx %-7ld %#lx\n", cnt,
3142 dynamic_strings + liblist.l_name, timebuf,
3143 liblist.l_checksum, liblist.l_version, liblist.l_flags);
3144 }
3145
3146 free (elib);
3147 }
3148
3149 if (options_offset != 0)
3150 {
3151 Elf_External_Options *eopt;
3152 Elf_Internal_Shdr *sect = section_headers;
3153 Elf_Internal_Options *iopt;
3154 Elf_Internal_Options *option;
3155 size_t offset;
3156 int cnt;
3157
3158 /* Find the section header so that we get the size. */
3159 while (sect->sh_type != SHT_MIPS_OPTIONS)
3160 ++sect;
3161
3162 GET_DATA_ALLOC (options_offset, sect->sh_size, eopt,
3163 Elf_External_Options *, "options");
3164
3165 iopt = (Elf_Internal_Options *) malloc ((sect->sh_size / sizeof (eopt))
3166 * sizeof (*iopt));
3167 if (iopt == NULL)
3168 {
3169 error (_("Out of memory"));
3170 return 0;
3171 }
3172
3173 offset = cnt = 0;
3174 option = iopt;
3175 while (offset < sect->sh_size)
3176 {
3177 Elf_External_Options *eoption;
3178
3179 eoption = (Elf_External_Options *) ((char *) eopt + offset);
3180
3181 option->kind = BYTE_GET (eoption->kind);
3182 option->size = BYTE_GET (eoption->size);
3183 option->section = BYTE_GET (eoption->section);
3184 option->info = BYTE_GET (eoption->info);
3185
3186 offset += option->size;
3187 ++option;
3188 ++cnt;
3189 }
3190
3191 printf (_("\nSection '%s' contains %d entries:\n"),
3192 string_table + sect->sh_name, cnt);
3193
3194 option = iopt;
3195 while (cnt-- > 0)
3196 {
3197 size_t len;
3198
3199 switch (option->kind)
3200 {
3201 case ODK_NULL:
3202 /* This shouldn't happen. */
3203 printf (" NULL %d %x", option->section, option->info);
3204 break;
3205 case ODK_REGINFO:
3206 printf (" REGINFO ");
3207 if (elf_header.e_machine == EM_MIPS)
3208 {
3209 /* 32bit form. */
3210 Elf32_External_RegInfo *ereg;
3211 Elf32_RegInfo reginfo;
3212
3213 ereg = (Elf32_External_RegInfo *) (option + 1);
3214 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
3215 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
3216 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
3217 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
3218 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
3219 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
3220
3221 printf ("GPR %08lx GP %ld\n",
3222 reginfo.ri_gprmask, reginfo.ri_gp_value);
3223 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
3224 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
3225 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
3226 }
3227 else
3228 {
3229 /* 64 bit form. */
3230 Elf64_External_RegInfo *ereg;
3231 Elf64_Internal_RegInfo reginfo;
3232
3233 ereg = (Elf64_External_RegInfo *) (option + 1);
3234 reginfo.ri_gprmask = BYTE_GET (ereg->ri_gprmask);
3235 reginfo.ri_cprmask[0] = BYTE_GET (ereg->ri_cprmask[0]);
3236 reginfo.ri_cprmask[1] = BYTE_GET (ereg->ri_cprmask[1]);
3237 reginfo.ri_cprmask[2] = BYTE_GET (ereg->ri_cprmask[2]);
3238 reginfo.ri_cprmask[3] = BYTE_GET (ereg->ri_cprmask[3]);
3239 reginfo.ri_gp_value = BYTE_GET (ereg->ri_gp_value);
3240
3241 printf ("GPR %08lx GP %ld\n",
3242 reginfo.ri_gprmask, reginfo.ri_gp_value);
3243 printf (" CPR0 %08lx CPR1 %08lx CPR2 %08lx CPR3 %08lx\n",
3244 reginfo.ri_cprmask[0], reginfo.ri_cprmask[1],
3245 reginfo.ri_cprmask[2], reginfo.ri_cprmask[3]);
3246 }
3247 ++option;
3248 continue;
3249 case ODK_EXCEPTIONS:
3250 fputs (" EXCEPTIONS fpe_min(", stdout);
3251 process_mips_fpe_exception (option->info & OEX_FPU_MIN);
3252 fputs (") fpe_max(", stdout);
3253 process_mips_fpe_exception ((option->info & OEX_FPU_MAX) >> 8);
3254 fputs (")", stdout);
3255
3256 if (option->info & OEX_PAGE0)
3257 fputs (" PAGE0", stdout);
3258 if (option->info & OEX_SMM)
3259 fputs (" SMM", stdout);
3260 if (option->info & OEX_FPDBUG)
3261 fputs (" FPDBUG", stdout);
3262 if (option->info & OEX_DISMISS)
3263 fputs (" DISMISS", stdout);
3264 break;
3265 case ODK_PAD:
3266 fputs (" PAD ", stdout);
3267 if (option->info & OPAD_PREFIX)
3268 fputs (" PREFIX", stdout);
3269 if (option->info & OPAD_POSTFIX)
3270 fputs (" POSTFIX", stdout);
3271 if (option->info & OPAD_SYMBOL)
3272 fputs (" SYMBOL", stdout);
3273 break;
3274 case ODK_HWPATCH:
3275 fputs (" HWPATCH ", stdout);
3276 if (option->info & OHW_R4KEOP)
3277 fputs (" R4KEOP", stdout);
3278 if (option->info & OHW_R8KPFETCH)
3279 fputs (" R8KPFETCH", stdout);
3280 if (option->info & OHW_R5KEOP)
3281 fputs (" R5KEOP", stdout);
3282 if (option->info & OHW_R5KCVTL)
3283 fputs (" R5KCVTL", stdout);
3284 break;
3285 case ODK_FILL:
3286 fputs (" FILL ", stdout);
3287 /* XXX Print content of info word? */
3288 break;
3289 case ODK_TAGS:
3290 fputs (" TAGS ", stdout);
3291 /* XXX Print content of info word? */
3292 break;
3293 case ODK_HWAND:
3294 fputs (" HWAND ", stdout);
3295 if (option->info & OHWA0_R4KEOP_CHECKED)
3296 fputs (" R4KEOP_CHECKED", stdout);
3297 if (option->info & OHWA0_R4KEOP_CLEAN)
3298 fputs (" R4KEOP_CLEAN", stdout);
3299 break;
3300 case ODK_HWOR:
3301 fputs (" HWOR ", stdout);
3302 if (option->info & OHWA0_R4KEOP_CHECKED)
3303 fputs (" R4KEOP_CHECKED", stdout);
3304 if (option->info & OHWA0_R4KEOP_CLEAN)
3305 fputs (" R4KEOP_CLEAN", stdout);
3306 break;
3307 case ODK_GP_GROUP:
3308 printf (" GP_GROUP %#06x self-contained %#06x",
3309 option->info & OGP_GROUP,
3310 (option->info & OGP_SELF) >> 16);
3311 break;
3312 case ODK_IDENT:
3313 printf (" IDENT %#06x self-contained %#06x",
3314 option->info & OGP_GROUP,
3315 (option->info & OGP_SELF) >> 16);
3316 break;
3317 default:
3318 /* This shouldn't happen. */
3319 printf (" %3d ??? %d %x",
3320 option->kind, option->section, option->info);
3321 break;
3322 }
3323
3324 len = sizeof (*eopt);
3325 while (len < option->size)
3326 if (((char *) option)[len] >= ' '
3327 && ((char *) option)[len] < 0x7f)
3328 printf ("%c", ((char *) option)[len++]);
3329 else
3330 printf ("\\%03o", ((char *) option)[len++]);
3331
3332 fputs ("\n", stdout);
3333 ++option;
3334 }
3335
3336 free (eopt);
3337 }
3338
3339 return 1;
3340 }
3341
3342 static int
3343 process_arch_specific (file)
3344 FILE *file;
3345 {
3346 switch (elf_header.e_machine)
3347 {
3348 case EM_MIPS:
3349 case EM_MIPS_RS4_BE:
3350 return process_mips_specific (file);
3351 break;
3352 default:
3353 break;
3354 }
3355 return 1;
3356 }
3357
3358 static int
3359 get_file_header (file)
3360 FILE * file;
3361 {
3362 Elf32_External_Ehdr ehdr;
3363
3364 if (fread (& ehdr, sizeof (ehdr), 1, file) != 1)
3365 return 0;
3366
3367 memcpy (elf_header.e_ident, ehdr.e_ident, EI_NIDENT);
3368
3369 if (elf_header.e_ident [EI_DATA] == ELFDATA2LSB)
3370 byte_get = byte_get_little_endian;
3371 else
3372 byte_get = byte_get_big_endian;
3373
3374 elf_header.e_entry = BYTE_GET (ehdr.e_entry);
3375 elf_header.e_phoff = BYTE_GET (ehdr.e_phoff);
3376 elf_header.e_shoff = BYTE_GET (ehdr.e_shoff);
3377 elf_header.e_version = BYTE_GET (ehdr.e_version);
3378 elf_header.e_flags = BYTE_GET (ehdr.e_flags);
3379 elf_header.e_type = BYTE_GET (ehdr.e_type);
3380 elf_header.e_machine = BYTE_GET (ehdr.e_machine);
3381 elf_header.e_ehsize = BYTE_GET (ehdr.e_ehsize);
3382 elf_header.e_phentsize = BYTE_GET (ehdr.e_phentsize);
3383 elf_header.e_phnum = BYTE_GET (ehdr.e_phnum);
3384 elf_header.e_shentsize = BYTE_GET (ehdr.e_shentsize);
3385 elf_header.e_shnum = BYTE_GET (ehdr.e_shnum);
3386 elf_header.e_shstrndx = BYTE_GET (ehdr.e_shstrndx);
3387
3388 return 1;
3389 }
3390
3391 static void
3392 process_file (file_name)
3393 char * file_name;
3394 {
3395 FILE * file;
3396 struct stat statbuf;
3397 unsigned int i;
3398
3399 if (stat (file_name, & statbuf) < 0)
3400 {
3401 error (_("Cannot stat input file %s.\n"), file_name);
3402 return;
3403 }
3404
3405 file = fopen (file_name, "rb");
3406 if (file == NULL)
3407 {
3408 error (_("Input file %s not found.\n"), file_name);
3409 return;
3410 }
3411
3412 if (! get_file_header (file))
3413 {
3414 error (_("%s: Failed to read file header\n"), file_name);
3415 fclose (file);
3416 return;
3417 }
3418
3419 /* Initialise per file variables. */
3420 for (i = NUM_ELEM (version_info); i--;)
3421 version_info [i] = 0;
3422
3423 for (i = NUM_ELEM (dynamic_info); i--;)
3424 dynamic_info [i] = 0;
3425
3426
3427 /* Process the file. */
3428 if (show_name)
3429 printf (_("\nFile: %s\n"), file_name);
3430
3431 if (! process_file_header ())
3432 {
3433 fclose (file);
3434 return;
3435 }
3436
3437 process_section_headers (file);
3438
3439 process_program_headers (file);
3440
3441 process_dynamic_segment (file);
3442
3443 process_relocs (file);
3444
3445 process_symbol_table (file);
3446
3447 process_version_sections (file);
3448
3449 process_section_contents (file);
3450
3451 process_arch_specific (file);
3452
3453 fclose (file);
3454
3455 if (section_headers)
3456 {
3457 free (section_headers);
3458 section_headers = NULL;
3459 }
3460
3461 if (string_table)
3462 {
3463 free (string_table);
3464 string_table = NULL;
3465 }
3466
3467 if (dynamic_strings)
3468 {
3469 free (dynamic_strings);
3470 dynamic_strings = NULL;
3471 }
3472
3473 if (dynamic_symbols)
3474 {
3475 free (dynamic_symbols);
3476 dynamic_symbols = NULL;
3477 }
3478 }
3479
3480 #ifdef SUPPORT_DISASSEMBLY
3481 /* Needed by the i386 disassembler. For extra credit, someone could
3482 fix this so that we insert symbolic addresses here, esp for GOT/PLT
3483 symbols */
3484
3485 void
3486 print_address (unsigned int addr, FILE * outfile)
3487 {
3488 fprintf (outfile,"0x%8.8x", addr);
3489 }
3490
3491 /* Needed by the i386 disassembler. */
3492 void
3493 db_task_printsym (unsigned int addr)
3494 {
3495 print_address (addr, stderr);
3496 }
3497 #endif
3498
3499 int
3500 main (argc, argv)
3501 int argc;
3502 char ** argv;
3503 {
3504 parse_args (argc, argv);
3505
3506 if (optind < (argc - 1))
3507 show_name = 1;
3508
3509 while (optind < argc)
3510 process_file (argv [optind ++]);
3511
3512 return 0;
3513 }