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