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