-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadelf.c
13069 lines (11353 loc) · 341 KB
/
readelf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* readelf.c -- display contents of an ELF format file
Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010
Free Software Foundation, Inc.
Originally developed by Eric Youngdale <[email protected]>
Modifications by Nick Clifton <[email protected]>
This file is part of GNU Binutils.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
/* The difference between readelf and objdump:
Both programs are capable of displaying the contents of ELF format files,
so why does the binutils project have two file dumpers ?
The reason is that objdump sees an ELF file through a BFD filter of the
world; if BFD has a bug where, say, it disagrees about a machine constant
in e_flags, then the odds are good that it will remain internally
consistent. The linker sees it the BFD way, objdump sees it the BFD way,
GAS sees it the BFD way. There was need for a tool to go find out what
the file actually says.
This is why the readelf program does not link against the BFD library - it
exists as an independent program to help verify the correct working of BFD.
There is also the case that readelf can provide more information about an
ELF file than is provided by objdump. In particular it can display DWARF
debugging information which (at the moment) objdump cannot. */
#include "config.h"
#include "sysdep.h"
#include <assert.h>
#include <sys/stat.h>
#include <time.h>
#ifdef HAVE_ZLIB_H
#include <zlib.h>
#endif
#if __GNUC__ >= 2
/* Define BFD64 here, even if our default architecture is 32 bit ELF
as this will allow us to read in and parse 64bit and 32bit ELF files.
Only do this if we believe that the compiler can support a 64 bit
data type. For now we only rely on GCC being able to do this. */
#define BFD64
#endif
#include "bfd.h"
#include "bucomm.h"
#include "dwarf.h"
#include "elf/common.h"
#include "elf/external.h"
#include "elf/internal.h"
/* Included here, before RELOC_MACROS_GEN_FUNC is defined, so that
we can obtain the H8 reloc numbers. We need these for the
get_reloc_size() function. We include h8.h again after defining
RELOC_MACROS_GEN_FUNC so that we get the naming function as well. */
#include "elf/h8.h"
#undef _ELF_H8_H
/* Undo the effects of #including reloc-macros.h. */
#undef START_RELOC_NUMBERS
#undef RELOC_NUMBER
#undef FAKE_RELOC
#undef EMPTY_RELOC
#undef END_RELOC_NUMBERS
#undef _RELOC_MACROS_H
/* The following headers use the elf/reloc-macros.h file to
automatically generate relocation recognition functions
such as elf_mips_reloc_type() */
#define RELOC_MACROS_GEN_FUNC
#include "elf/alpha.h"
#include "elf/arc.h"
#include "elf/arm.h"
#include "elf/avr.h"
#include "elf/bfin.h"
#include "elf/cr16.h"
#include "elf/cris.h"
#include "elf/crx.h"
#include "elf/d10v.h"
#include "elf/d30v.h"
#include "elf/dlx.h"
#include "elf/fr30.h"
#include "elf/frv.h"
#include "elf/h8.h"
#include "elf/hppa.h"
#include "elf/i386.h"
#include "elf/i370.h"
#include "elf/i860.h"
#include "elf/i960.h"
#include "elf/ia64.h"
#include "elf/ip2k.h"
#include "elf/lm32.h"
#include "elf/iq2000.h"
#include "elf/m32c.h"
#include "elf/m32r.h"
#include "elf/m68k.h"
#include "elf/m68hc11.h"
#include "elf/mcore.h"
#include "elf/mep.h"
#include "elf/microblaze.h"
#include "elf/mips.h"
#include "elf/mmix.h"
#include "elf/mn10200.h"
#include "elf/mn10300.h"
#include "elf/moxie.h"
#include "elf/mt.h"
#include "elf/msp430.h"
#include "elf/or32.h"
#include "elf/pj.h"
#include "elf/ppc.h"
#include "elf/ppc64.h"
#include "elf/rx.h"
#include "elf/s390.h"
#include "elf/score.h"
#include "elf/sh.h"
#include "elf/sparc.h"
#include "elf/spu.h"
#include "elf/tic6x.h"
#include "elf/v850.h"
#include "elf/vax.h"
#include "elf/x86-64.h"
#include "elf/xc16x.h"
#include "elf/xstormy16.h"
#include "elf/xtensa.h"
#include "aout/ar.h"
#include "getopt.h"
#include "libiberty.h"
#include "safe-ctype.h"
#include "filenames.h"
char * program_name = "readelf";
static long archive_file_offset;
static unsigned long archive_file_size;
static unsigned long dynamic_addr;
static bfd_size_type dynamic_size;
static unsigned int dynamic_nent;
static char * dynamic_strings;
static unsigned long dynamic_strings_length;
static char * string_table;
static unsigned long string_table_length;
static unsigned long num_dynamic_syms;
static Elf_Internal_Sym * dynamic_symbols;
static Elf_Internal_Syminfo * dynamic_syminfo;
static unsigned long dynamic_syminfo_offset;
static unsigned int dynamic_syminfo_nent;
static char program_interpreter[PATH_MAX];
static bfd_vma dynamic_info[DT_ENCODING];
static bfd_vma dynamic_info_DT_GNU_HASH;
static bfd_vma version_info[16];
static Elf_Internal_Ehdr elf_header;
static Elf_Internal_Shdr * section_headers;
static Elf_Internal_Phdr * program_headers;
static Elf_Internal_Dyn * dynamic_section;
static Elf_Internal_Shdr * symtab_shndx_hdr;
static int show_name;
static int do_dynamic;
static int do_syms;
static int do_dyn_syms;
static int do_reloc;
static int do_sections;
static int do_section_groups;
static int do_section_details;
static int do_segments;
static int do_unwind;
static int do_using_dynamic;
static int do_header;
static int do_dump;
static int do_version;
static int do_histogram;
static int do_debugging;
static int do_arch;
static int do_notes;
static int do_archive_index;
static int is_32bit_elf;
struct group_list
{
struct group_list * next;
unsigned int section_index;
};
struct group
{
struct group_list * root;
unsigned int group_index;
};
static size_t group_count;
static struct group * section_groups;
static struct group ** section_headers_groups;
/* Flag bits indicating particular types of dump. */
#define HEX_DUMP (1 << 0) /* The -x command line switch. */
#define DISASS_DUMP (1 << 1) /* The -i command line switch. */
#define DEBUG_DUMP (1 << 2) /* The -w command line switch. */
#define STRING_DUMP (1 << 3) /* The -p command line switch. */
#define RELOC_DUMP (1 << 4) /* The -R command line switch. */
typedef unsigned char dump_type;
/* A linked list of the section names for which dumps were requested. */
struct dump_list_entry
{
char * name;
dump_type type;
struct dump_list_entry * next;
};
static struct dump_list_entry * dump_sects_byname;
/* A dynamic array of flags indicating for which sections a dump
has been requested via command line switches. */
static dump_type * cmdline_dump_sects = NULL;
static unsigned int num_cmdline_dump_sects = 0;
/* A dynamic array of flags indicating for which sections a dump of
some kind has been requested. It is reset on a per-object file
basis and then initialised from the cmdline_dump_sects array,
the results of interpreting the -w switch, and the
dump_sects_byname list. */
static dump_type * dump_sects = NULL;
static unsigned int num_dump_sects = 0;
/* How to print a vma value. */
typedef enum print_mode
{
HEX,
DEC,
DEC_5,
UNSIGNED,
PREFIX_HEX,
FULL_HEX,
LONG_HEX
}
print_mode;
static void (* byte_put) (unsigned char *, bfd_vma, int);
#define UNKNOWN -1
#define SECTION_NAME(X) \
((X) == NULL ? _("<none>") \
: string_table == NULL ? _("<no-name>") \
: ((X)->sh_name >= string_table_length ? _("<corrupt>") \
: string_table + (X)->sh_name))
#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
#define BYTE_GET(field) byte_get (field, sizeof (field))
#define GET_ELF_SYMBOLS(file, section) \
(is_32bit_elf ? get_32bit_elf_symbols (file, section) \
: get_64bit_elf_symbols (file, section))
#define VALID_DYNAMIC_NAME(offset) ((dynamic_strings != NULL) && (offset < dynamic_strings_length))
/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
already been called and verified that the string exists. */
#define GET_DYNAMIC_NAME(offset) (dynamic_strings + offset)
/* This is just a bit of syntatic sugar. */
#define streq(a,b) (strcmp ((a), (b)) == 0)
#define strneq(a,b,n) (strncmp ((a), (b), (n)) == 0)
#define const_strneq(a,b) (strncmp ((a), (b), sizeof (b) - 1) == 0)
#define REMOVE_ARCH_BITS(ADDR) do { \
if (elf_header.e_machine == EM_ARM) \
(ADDR) &= ~1; \
} while (0)
static void *
get_data (void * var, FILE * file, long offset, size_t size, size_t nmemb,
const char * reason)
{
void * mvar;
if (size == 0 || nmemb == 0)
return NULL;
if (fseek (file, archive_file_offset + offset, SEEK_SET))
{
error (_("Unable to seek to 0x%lx for %s\n"),
(unsigned long) archive_file_offset + offset, reason);
return NULL;
}
mvar = var;
if (mvar == NULL)
{
/* Check for overflow. */
if (nmemb < (~(size_t) 0 - 1) / size)
/* + 1 so that we can '\0' terminate invalid string table sections. */
mvar = malloc (size * nmemb + 1);
if (mvar == NULL)
{
error (_("Out of memory allocating 0x%lx bytes for %s\n"),
(unsigned long)(size * nmemb), reason);
return NULL;
}
((char *) mvar)[size * nmemb] = '\0';
}
if (fread (mvar, size, nmemb, file) != nmemb)
{
error (_("Unable to read in 0x%lx bytes of %s\n"),
(unsigned long)(size * nmemb), reason);
if (mvar != var)
free (mvar);
return NULL;
}
return mvar;
}
static void
byte_put_little_endian (unsigned char * field, bfd_vma value, int size)
{
switch (size)
{
case 8:
field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
field[6] = ((value >> 24) >> 24) & 0xff;
field[5] = ((value >> 24) >> 16) & 0xff;
field[4] = ((value >> 24) >> 8) & 0xff;
/* Fall through. */
case 4:
field[3] = (value >> 24) & 0xff;
/* Fall through. */
case 3:
field[2] = (value >> 16) & 0xff;
/* Fall through. */
case 2:
field[1] = (value >> 8) & 0xff;
/* Fall through. */
case 1:
field[0] = value & 0xff;
break;
default:
error (_("Unhandled data length: %d\n"), size);
abort ();
}
}
/* Print a VMA value. */
static int
print_vma (bfd_vma vma, print_mode mode)
{
int nc = 0;
switch (mode)
{
case FULL_HEX:
nc = printf ("0x");
/* Drop through. */
case LONG_HEX:
#ifdef BFD64
if (is_32bit_elf)
return nc + printf ("%8.8" BFD_VMA_FMT "x", vma);
#endif
printf_vma (vma);
return nc + 16;
case DEC_5:
if (vma <= 99999)
return printf ("%5" BFD_VMA_FMT "d", vma);
/* Drop through. */
case PREFIX_HEX:
nc = printf ("0x");
/* Drop through. */
case HEX:
return nc + printf ("%" BFD_VMA_FMT "x", vma);
case DEC:
return printf ("%" BFD_VMA_FMT "d", vma);
case UNSIGNED:
return printf ("%" BFD_VMA_FMT "u", vma);
}
return 0;
}
/* Display a symbol on stdout. Handles the display of non-printing characters.
If DO_WIDE is not true then format the symbol to be at most WIDTH characters,
truncating as necessary. If WIDTH is negative then format the string to be
exactly - WIDTH characters, truncating or padding as necessary.
Returns the number of emitted characters. */
static unsigned int
print_symbol (int width, const char * symbol)
{
const char * c;
bfd_boolean extra_padding = FALSE;
unsigned int num_printed = 0;
if (do_wide)
{
/* Set the width to a very large value. This simplifies the code below. */
width = INT_MAX;
}
else if (width < 0)
{
/* Keep the width positive. This also helps. */
width = - width;
extra_padding = TRUE;
}
while (width)
{
int len;
c = symbol;
/* Look for non-printing symbols inside the symbol's name.
This test is triggered in particular by the names generated
by the assembler for local labels. */
while (ISPRINT (* c))
c++;
len = c - symbol;
if (len)
{
if (len > width)
len = width;
printf ("%.*s", len, symbol);
width -= len;
num_printed += len;
}
if (* c == 0 || width == 0)
break;
/* Now display the non-printing character, if
there is room left in which to dipslay it. */
if (*c < 32)
{
if (width < 2)
break;
printf ("^%c", *c + 0x40);
width -= 2;
num_printed += 2;
}
else
{
if (width < 6)
break;
printf ("<0x%.2x>", *c);
width -= 6;
num_printed += 6;
}
symbol = c + 1;
}
if (extra_padding && width > 0)
{
/* Fill in the remaining spaces. */
printf ("%-*s", width, " ");
num_printed += 2;
}
return num_printed;
}
static void
byte_put_big_endian (unsigned char * field, bfd_vma value, int size)
{
switch (size)
{
case 8:
field[7] = value & 0xff;
field[6] = (value >> 8) & 0xff;
field[5] = (value >> 16) & 0xff;
field[4] = (value >> 24) & 0xff;
value >>= 16;
value >>= 16;
/* Fall through. */
case 4:
field[3] = value & 0xff;
value >>= 8;
/* Fall through. */
case 3:
field[2] = value & 0xff;
value >>= 8;
/* Fall through. */
case 2:
field[1] = value & 0xff;
value >>= 8;
/* Fall through. */
case 1:
field[0] = value & 0xff;
break;
default:
error (_("Unhandled data length: %d\n"), size);
abort ();
}
}
/* Return a pointer to section NAME, or NULL if no such section exists. */
static Elf_Internal_Shdr *
find_section (const char * name)
{
unsigned int i;
for (i = 0; i < elf_header.e_shnum; i++)
if (streq (SECTION_NAME (section_headers + i), name))
return section_headers + i;
return NULL;
}
/* Return a pointer to a section containing ADDR, or NULL if no such
section exists. */
static Elf_Internal_Shdr *
find_section_by_address (bfd_vma addr)
{
unsigned int i;
for (i = 0; i < elf_header.e_shnum; i++)
{
Elf_Internal_Shdr *sec = section_headers + i;
if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
return sec;
}
return NULL;
}
/* Read an unsigned LEB128 encoded value from p. Set *PLEN to the number of
bytes read. */
static unsigned long
read_uleb128 (unsigned char *data, unsigned int *length_return)
{
return read_leb128 (data, length_return, 0);
}
/* Return true if the current file is for IA-64 machine and OpenVMS ABI.
This OS has so many departures from the ELF standard that we test it at
many places. */
static inline int
is_ia64_vms (void)
{
return elf_header.e_machine == EM_IA_64
&& elf_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
}
/* Guess the relocation size commonly used by the specific machines. */
static int
guess_is_rela (unsigned int e_machine)
{
switch (e_machine)
{
/* Targets that use REL relocations. */
case EM_386:
case EM_486:
case EM_960:
case EM_ARM:
case EM_D10V:
case EM_CYGNUS_D10V:
case EM_DLX:
case EM_MIPS:
case EM_MIPS_RS3_LE:
case EM_CYGNUS_M32R:
case EM_OPENRISC:
case EM_OR32:
case EM_SCORE:
return FALSE;
/* Targets that use RELA relocations. */
case EM_68K:
case EM_860:
case EM_ALPHA:
case EM_ALTERA_NIOS2:
case EM_AVR:
case EM_AVR_OLD:
case EM_BLACKFIN:
case EM_CR16:
case EM_CR16_OLD:
case EM_CRIS:
case EM_CRX:
case EM_D30V:
case EM_CYGNUS_D30V:
case EM_FR30:
case EM_CYGNUS_FR30:
case EM_CYGNUS_FRV:
case EM_H8S:
case EM_H8_300:
case EM_H8_300H:
case EM_IA_64:
case EM_IP2K:
case EM_IP2K_OLD:
case EM_IQ2000:
case EM_LATTICEMICO32:
case EM_M32C_OLD:
case EM_M32C:
case EM_M32R:
case EM_MCORE:
case EM_CYGNUS_MEP:
case EM_MMIX:
case EM_MN10200:
case EM_CYGNUS_MN10200:
case EM_MN10300:
case EM_CYGNUS_MN10300:
case EM_MOXIE:
case EM_MSP430:
case EM_MSP430_OLD:
case EM_MT:
case EM_NIOS32:
case EM_PPC64:
case EM_PPC:
case EM_RX:
case EM_S390:
case EM_S390_OLD:
case EM_SH:
case EM_SPARC:
case EM_SPARC32PLUS:
case EM_SPARCV9:
case EM_SPU:
case EM_TI_C6000:
case EM_V850:
case EM_CYGNUS_V850:
case EM_VAX:
case EM_X86_64:
case EM_L1OM:
case EM_XSTORMY16:
case EM_XTENSA:
case EM_XTENSA_OLD:
case EM_MICROBLAZE:
case EM_MICROBLAZE_OLD:
return TRUE;
case EM_68HC05:
case EM_68HC08:
case EM_68HC11:
case EM_68HC16:
case EM_FX66:
case EM_ME16:
case EM_MMA:
case EM_NCPU:
case EM_NDR1:
case EM_PCP:
case EM_ST100:
case EM_ST19:
case EM_ST7:
case EM_ST9PLUS:
case EM_STARCORE:
case EM_SVX:
case EM_TINYJ:
default:
warn (_("Don't know about relocations on this machine architecture\n"));
return FALSE;
}
}
static int
slurp_rela_relocs (FILE * file,
unsigned long rel_offset,
unsigned long rel_size,
Elf_Internal_Rela ** relasp,
unsigned long * nrelasp)
{
Elf_Internal_Rela * relas;
unsigned long nrelas;
unsigned int i;
if (is_32bit_elf)
{
Elf32_External_Rela * erelas;
erelas = (Elf32_External_Rela *) get_data (NULL, file, rel_offset, 1,
rel_size, _("relocs"));
if (!erelas)
return 0;
nrelas = rel_size / sizeof (Elf32_External_Rela);
relas = (Elf_Internal_Rela *) cmalloc (nrelas,
sizeof (Elf_Internal_Rela));
if (relas == NULL)
{
free (erelas);
error (_("out of memory parsing relocs\n"));
return 0;
}
for (i = 0; i < nrelas; i++)
{
relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
relas[i].r_info = BYTE_GET (erelas[i].r_info);
relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
}
free (erelas);
}
else
{
Elf64_External_Rela * erelas;
erelas = (Elf64_External_Rela *) get_data (NULL, file, rel_offset, 1,
rel_size, _("relocs"));
if (!erelas)
return 0;
nrelas = rel_size / sizeof (Elf64_External_Rela);
relas = (Elf_Internal_Rela *) cmalloc (nrelas,
sizeof (Elf_Internal_Rela));
if (relas == NULL)
{
free (erelas);
error (_("out of memory parsing relocs\n"));
return 0;
}
for (i = 0; i < nrelas; i++)
{
relas[i].r_offset = BYTE_GET (erelas[i].r_offset);
relas[i].r_info = BYTE_GET (erelas[i].r_info);
relas[i].r_addend = BYTE_GET (erelas[i].r_addend);
/* The #ifdef BFD64 below is to prevent a compile time
warning. We know that if we do not have a 64 bit data
type that we will never execute this code anyway. */
#ifdef BFD64
if (elf_header.e_machine == EM_MIPS
&& elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
{
/* In little-endian objects, r_info isn't really a
64-bit little-endian value: it has a 32-bit
little-endian symbol index followed by four
individual byte fields. Reorder INFO
accordingly. */
bfd_vma inf = relas[i].r_info;
inf = (((inf & 0xffffffff) << 32)
| ((inf >> 56) & 0xff)
| ((inf >> 40) & 0xff00)
| ((inf >> 24) & 0xff0000)
| ((inf >> 8) & 0xff000000));
relas[i].r_info = inf;
}
#endif /* BFD64 */
}
free (erelas);
}
*relasp = relas;
*nrelasp = nrelas;
return 1;
}
static int
slurp_rel_relocs (FILE * file,
unsigned long rel_offset,
unsigned long rel_size,
Elf_Internal_Rela ** relsp,
unsigned long * nrelsp)
{
Elf_Internal_Rela * rels;
unsigned long nrels;
unsigned int i;
if (is_32bit_elf)
{
Elf32_External_Rel * erels;
erels = (Elf32_External_Rel *) get_data (NULL, file, rel_offset, 1,
rel_size, _("relocs"));
if (!erels)
return 0;
nrels = rel_size / sizeof (Elf32_External_Rel);
rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
if (rels == NULL)
{
free (erels);
error (_("out of memory parsing relocs\n"));
return 0;
}
for (i = 0; i < nrels; i++)
{
rels[i].r_offset = BYTE_GET (erels[i].r_offset);
rels[i].r_info = BYTE_GET (erels[i].r_info);
rels[i].r_addend = 0;
}
free (erels);
}
else
{
Elf64_External_Rel * erels;
erels = (Elf64_External_Rel *) get_data (NULL, file, rel_offset, 1,
rel_size, _("relocs"));
if (!erels)
return 0;
nrels = rel_size / sizeof (Elf64_External_Rel);
rels = (Elf_Internal_Rela *) cmalloc (nrels, sizeof (Elf_Internal_Rela));
if (rels == NULL)
{
free (erels);
error (_("out of memory parsing relocs\n"));
return 0;
}
for (i = 0; i < nrels; i++)
{
rels[i].r_offset = BYTE_GET (erels[i].r_offset);
rels[i].r_info = BYTE_GET (erels[i].r_info);
rels[i].r_addend = 0;
/* The #ifdef BFD64 below is to prevent a compile time
warning. We know that if we do not have a 64 bit data
type that we will never execute this code anyway. */
#ifdef BFD64
if (elf_header.e_machine == EM_MIPS
&& elf_header.e_ident[EI_DATA] != ELFDATA2MSB)
{
/* In little-endian objects, r_info isn't really a
64-bit little-endian value: it has a 32-bit
little-endian symbol index followed by four
individual byte fields. Reorder INFO
accordingly. */
bfd_vma inf = rels[i].r_info;
inf = (((inf & 0xffffffff) << 32)
| ((inf >> 56) & 0xff)
| ((inf >> 40) & 0xff00)
| ((inf >> 24) & 0xff0000)
| ((inf >> 8) & 0xff000000));
rels[i].r_info = inf;
}
#endif /* BFD64 */
}
free (erels);
}
*relsp = rels;
*nrelsp = nrels;
return 1;
}
/* Returns the reloc type extracted from the reloc info field. */
static unsigned int
get_reloc_type (bfd_vma reloc_info)
{
if (is_32bit_elf)
return ELF32_R_TYPE (reloc_info);
switch (elf_header.e_machine)
{
case EM_MIPS:
/* Note: We assume that reloc_info has already been adjusted for us. */
return ELF64_MIPS_R_TYPE (reloc_info);
case EM_SPARCV9:
return ELF64_R_TYPE_ID (reloc_info);
default:
return ELF64_R_TYPE (reloc_info);
}
}
/* Return the symbol index extracted from the reloc info field. */
static bfd_vma
get_reloc_symindex (bfd_vma reloc_info)
{
return is_32bit_elf ? ELF32_R_SYM (reloc_info) : ELF64_R_SYM (reloc_info);
}
/* Display the contents of the relocation data found at the specified
offset. */
static void
dump_relocations (FILE * file,
unsigned long rel_offset,
unsigned long rel_size,
Elf_Internal_Sym * symtab,
unsigned long nsyms,
char * strtab,
unsigned long strtablen,
int is_rela)
{
unsigned int i;
Elf_Internal_Rela * rels;
if (is_rela == UNKNOWN)
is_rela = guess_is_rela (elf_header.e_machine);
if (is_rela)
{
if (!slurp_rela_relocs (file, rel_offset, rel_size, &rels, &rel_size))
return;
}
else
{
if (!slurp_rel_relocs (file, rel_offset, rel_size, &rels, &rel_size))
return;
}
if (is_32bit_elf)
{
if (is_rela)
{
if (do_wide)
printf (_(" Offset Info Type Sym. Value Symbol's Name + Addend\n"));
else
printf (_(" Offset Info Type Sym.Value Sym. Name + Addend\n"));
}
else
{
if (do_wide)
printf (_(" Offset Info Type Sym. Value Symbol's Name\n"));
else
printf (_(" Offset Info Type Sym.Value Sym. Name\n"));
}
}
else
{
if (is_rela)
{
if (do_wide)
printf (_(" Offset Info Type Symbol's Value Symbol's Name + Addend\n"));
else
printf (_(" Offset Info Type Sym. Value Sym. Name + Addend\n"));
}
else
{
if (do_wide)
printf (_(" Offset Info Type Symbol's Value Symbol's Name\n"));
else
printf (_(" Offset Info Type Sym. Value Sym. Name\n"));
}
}
for (i = 0; i < rel_size; i++)
{
const char * rtype;
bfd_vma offset;
bfd_vma inf;
bfd_vma symtab_index;
bfd_vma type;
offset = rels[i].r_offset;
inf = rels[i].r_info;