-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreadelf.c
19957 lines (17548 loc) · 534 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 (C) 1998-2019 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 "sysdep.h"
#include <assert.h>
#include <time.h>
#include <zlib.h>
#ifdef HAVE_WCHAR_H
#include <wchar.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 "elfcomm.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/aarch64.h"
#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/csky.h"
#include "elf/d10v.h"
#include "elf/d30v.h"
#include "elf/dlx.h"
#include "elf/epiphany.h"
#include "elf/fr30.h"
#include "elf/frv.h"
#include "elf/ft32.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/s12z.h"
#include "elf/mcore.h"
#include "elf/mep.h"
#include "elf/metag.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/nds32.h"
#include "elf/nfp.h"
#include "elf/nios2.h"
#include "elf/or1k.h"
#include "elf/pj.h"
#include "elf/ppc.h"
#include "elf/ppc64.h"
#include "elf/pru.h"
#include "elf/riscv.h"
#include "elf/rl78.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/tilegx.h"
#include "elf/tilepro.h"
#include "elf/v850.h"
#include "elf/vax.h"
#include "elf/visium.h"
#include "elf/wasm32.h"
#include "elf/x86-64.h"
#include "elf/xc16x.h"
#include "elf/xgate.h"
#include "elf/xstormy16.h"
#include "elf/xtensa.h"
#include "getopt.h"
#include "libiberty.h"
#include "safe-ctype.h"
#include "filenames.h"
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER))
#endif
typedef struct elf_section_list
{
Elf_Internal_Shdr * hdr;
struct elf_section_list * next;
} elf_section_list;
/* 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;
};
typedef struct filedata
{
const char * file_name;
FILE * handle;
bfd_size_type file_size;
Elf_Internal_Ehdr file_header;
Elf_Internal_Shdr * section_headers;
Elf_Internal_Phdr * program_headers;
char * string_table;
unsigned long string_table_length;
/* 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. */
dump_type * dump_sects;
unsigned int num_dump_sects;
} Filedata;
char * program_name = "readelf";
static unsigned long archive_file_offset;
static unsigned long archive_file_size;
static unsigned long dynamic_addr;
static bfd_size_type dynamic_size;
static size_t dynamic_nent;
static char * dynamic_strings;
static unsigned long dynamic_strings_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_Dyn * dynamic_section;
static elf_section_list * symtab_shndx_list;
static bfd_boolean show_name = FALSE;
static bfd_boolean do_dynamic = FALSE;
static bfd_boolean do_syms = FALSE;
static bfd_boolean do_dyn_syms = FALSE;
static bfd_boolean do_reloc = FALSE;
static bfd_boolean do_sections = FALSE;
static bfd_boolean do_section_groups = FALSE;
static bfd_boolean do_section_details = FALSE;
static bfd_boolean do_segments = FALSE;
static bfd_boolean do_unwind = FALSE;
static bfd_boolean do_using_dynamic = FALSE;
static bfd_boolean do_header = FALSE;
static bfd_boolean do_dump = FALSE;
static bfd_boolean do_version = FALSE;
static bfd_boolean do_histogram = FALSE;
static bfd_boolean do_debugging = FALSE;
static bfd_boolean do_arch = FALSE;
static bfd_boolean do_notes = FALSE;
static bfd_boolean do_archive_index = FALSE;
static bfd_boolean is_32bit_elf = FALSE;
static bfd_boolean decompress_dumps = FALSE;
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;
/* A dynamic array of flags indicating for which sections a dump
has been requested via command line switches. */
static Filedata cmdline;
static struct dump_list_entry * dump_sects_byname;
/* How to print a vma value. */
typedef enum print_mode
{
HEX,
DEC,
DEC_5,
UNSIGNED,
PREFIX_HEX,
FULL_HEX,
LONG_HEX
}
print_mode;
/* Versioned symbol info. */
enum versioned_symbol_info
{
symbol_undefined,
symbol_hidden,
symbol_public
};
static const char * get_symbol_version_string
(Filedata *, bfd_boolean, const char *, unsigned long, unsigned,
Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
#define UNKNOWN -1
#define SECTION_NAME(X) \
((X) == NULL ? _("<none>") \
: filedata->string_table == NULL ? _("<no-strings>") \
: ((X)->sh_name >= filedata->string_table_length ? _("<corrupt>") \
: filedata->string_table + (X)->sh_name))
#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
#define GET_ELF_SYMBOLS(file, section, sym_count) \
(is_32bit_elf ? get_32bit_elf_symbols (file, section, sym_count) \
: get_64bit_elf_symbols (file, section, sym_count))
#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)
#define REMOVE_ARCH_BITS(ADDR) \
do \
{ \
if (filedata->file_header.e_machine == EM_ARM) \
(ADDR) &= ~1; \
} \
while (0)
/* Print a BFD_VMA to an internal buffer, for use in error messages.
BFD_FMA_FMT can't be used in translated strings. */
static const char *
bfd_vmatoa (char *fmtch, bfd_vma value)
{
/* bfd_vmatoa is used more then once in a printf call for output.
Cycle through an array of buffers. */
static int buf_pos = 0;
static struct bfd_vmatoa_buf
{
char place[64];
} buf[4];
char *ret;
char fmt[32];
ret = buf[buf_pos++].place;
buf_pos %= ARRAY_SIZE (buf);
sprintf (fmt, "%%%s%s", BFD_VMA_FMT, fmtch);
snprintf (ret, sizeof (buf[0].place), fmt, value);
return ret;
}
/* Retrieve NMEMB structures, each SIZE bytes long from FILEDATA starting at
OFFSET + the offset of the current archive member, if we are examining an
archive. Put the retrieved data into VAR, if it is not NULL. Otherwise
allocate a buffer using malloc and fill that. In either case return the
pointer to the start of the retrieved data or NULL if something went wrong.
If something does go wrong and REASON is not NULL then emit an error
message using REASON as part of the context. */
static void *
get_data (void * var,
Filedata * filedata,
unsigned long offset,
bfd_size_type size,
bfd_size_type nmemb,
const char * reason)
{
void * mvar;
bfd_size_type amt = size * nmemb;
if (size == 0 || nmemb == 0)
return NULL;
/* If the size_t type is smaller than the bfd_size_type, eg because
you are building a 32-bit tool on a 64-bit host, then make sure
that when the sizes are cast to (size_t) no information is lost. */
if (sizeof (size_t) < sizeof (bfd_size_type)
&& ( (bfd_size_type) ((size_t) size) != size
|| (bfd_size_type) ((size_t) nmemb) != nmemb))
{
if (reason)
error (_("Size truncation prevents reading %s"
" elements of size %s for %s\n"),
bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
return NULL;
}
/* Check for size overflow. */
if (amt < nmemb)
{
if (reason)
error (_("Size overflow prevents reading %s"
" elements of size %s for %s\n"),
bfd_vmatoa ("u", nmemb), bfd_vmatoa ("u", size), reason);
return NULL;
}
/* Be kind to memory checkers (eg valgrind, address sanitizer) by not
attempting to allocate memory when the read is bound to fail. */
if (archive_file_offset > filedata->file_size
|| offset > filedata->file_size - archive_file_offset
|| amt > filedata->file_size - archive_file_offset - offset)
{
if (reason)
error (_("Reading %s bytes extends past end of file for %s\n"),
bfd_vmatoa ("u", amt), reason);
return NULL;
}
if (fseek (filedata->handle, archive_file_offset + offset, SEEK_SET))
{
if (reason)
error (_("Unable to seek to 0x%lx for %s\n"),
archive_file_offset + offset, reason);
return NULL;
}
mvar = var;
if (mvar == NULL)
{
/* Check for overflow. */
if (nmemb < (~(bfd_size_type) 0 - 1) / size)
/* + 1 so that we can '\0' terminate invalid string table sections. */
mvar = malloc ((size_t) amt + 1);
if (mvar == NULL)
{
if (reason)
error (_("Out of memory allocating %s bytes for %s\n"),
bfd_vmatoa ("u", amt), reason);
return NULL;
}
((char *) mvar)[amt] = '\0';
}
if (fread (mvar, (size_t) size, (size_t) nmemb, filedata->handle) != nmemb)
{
if (reason)
error (_("Unable to read in %s bytes of %s\n"),
bfd_vmatoa ("u", amt), reason);
if (mvar != var)
free (mvar);
return NULL;
}
return mvar;
}
/* Print a VMA value in the MODE specified.
Returns the number of characters displayed. */
static unsigned int
print_vma (bfd_vma vma, print_mode mode)
{
unsigned int nc = 0;
switch (mode)
{
case FULL_HEX:
nc = printf ("0x");
/* Fall 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);
/* Fall through. */
case PREFIX_HEX:
nc = printf ("0x");
/* Fall 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);
default:
/* FIXME: Report unrecognised mode ? */
return 0;
}
}
/* Display a symbol on stdout. Handles the display of control characters and
multibye characters (assuming the host environment supports them).
Display at most abs(WIDTH) characters, truncating as necessary, unless do_wide is true.
If WIDTH is negative then ensure that the output is at least (- WIDTH) characters,
padding as necessary.
Returns the number of emitted characters. */
static unsigned int
print_symbol (signed int width, const char *symbol)
{
bfd_boolean extra_padding = FALSE;
signed int num_printed = 0;
#ifdef HAVE_MBSTATE_T
mbstate_t state;
#endif
unsigned int width_remaining;
if (width < 0)
{
/* Keep the width positive. This helps the code below. */
width = - width;
extra_padding = TRUE;
}
else if (width == 0)
return 0;
if (do_wide)
/* Set the remaining width to a very large value.
This simplifies the code below. */
width_remaining = INT_MAX;
else
width_remaining = width;
#ifdef HAVE_MBSTATE_T
/* Initialise the multibyte conversion state. */
memset (& state, 0, sizeof (state));
#endif
while (width_remaining)
{
size_t n;
const char c = *symbol++;
if (c == 0)
break;
/* Do not print control characters directly as they can affect terminal
settings. Such characters usually appear in the names generated
by the assembler for local labels. */
if (ISCNTRL (c))
{
if (width_remaining < 2)
break;
printf ("^%c", c + 0x40);
width_remaining -= 2;
num_printed += 2;
}
else if (ISPRINT (c))
{
putchar (c);
width_remaining --;
num_printed ++;
}
else
{
#ifdef HAVE_MBSTATE_T
wchar_t w;
#endif
/* Let printf do the hard work of displaying multibyte characters. */
printf ("%.1s", symbol - 1);
width_remaining --;
num_printed ++;
#ifdef HAVE_MBSTATE_T
/* Try to find out how many bytes made up the character that was
just printed. Advance the symbol pointer past the bytes that
were displayed. */
n = mbrtowc (& w, symbol - 1, MB_CUR_MAX, & state);
#else
n = 1;
#endif
if (n != (size_t) -1 && n != (size_t) -2 && n > 0)
symbol += (n - 1);
}
}
if (extra_padding && num_printed < width)
{
/* Fill in the remaining spaces. */
printf ("%-*s", width - num_printed, " ");
num_printed = width;
}
return num_printed;
}
/* Returns a pointer to a static buffer containing a printable version of
the given section's name. Like print_symbol, except that it does not try
to print multibyte characters, it just interprets them as hex values. */
static const char *
printable_section_name (Filedata * filedata, const Elf_Internal_Shdr * sec)
{
#define MAX_PRINT_SEC_NAME_LEN 128
static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
const char * name = SECTION_NAME (sec);
char * buf = sec_name_buf;
char c;
unsigned int remaining = MAX_PRINT_SEC_NAME_LEN;
while ((c = * name ++) != 0)
{
if (ISCNTRL (c))
{
if (remaining < 2)
break;
* buf ++ = '^';
* buf ++ = c + 0x40;
remaining -= 2;
}
else if (ISPRINT (c))
{
* buf ++ = c;
remaining -= 1;
}
else
{
static char hex[17] = "0123456789ABCDEF";
if (remaining < 4)
break;
* buf ++ = '<';
* buf ++ = hex[(c & 0xf0) >> 4];
* buf ++ = hex[c & 0x0f];
* buf ++ = '>';
remaining -= 4;
}
if (remaining == 0)
break;
}
* buf = 0;
return sec_name_buf;
}
static const char *
printable_section_name_from_index (Filedata * filedata, unsigned long ndx)
{
if (ndx >= filedata->file_header.e_shnum)
return _("<corrupt>");
return printable_section_name (filedata, filedata->section_headers + ndx);
}
/* Return a pointer to section NAME, or NULL if no such section exists. */
static Elf_Internal_Shdr *
find_section (Filedata * filedata, const char * name)
{
unsigned int i;
if (filedata->section_headers == NULL)
return NULL;
for (i = 0; i < filedata->file_header.e_shnum; i++)
if (streq (SECTION_NAME (filedata->section_headers + i), name))
return filedata->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 (Filedata * filedata, bfd_vma addr)
{
unsigned int i;
if (filedata->section_headers == NULL)
return NULL;
for (i = 0; i < filedata->file_header.e_shnum; i++)
{
Elf_Internal_Shdr *sec = filedata->section_headers + i;
if (addr >= sec->sh_addr && addr < sec->sh_addr + sec->sh_size)
return sec;
}
return NULL;
}
static Elf_Internal_Shdr *
find_section_by_type (Filedata * filedata, unsigned int type)
{
unsigned int i;
if (filedata->section_headers == NULL)
return NULL;
for (i = 0; i < filedata->file_header.e_shnum; i++)
{
Elf_Internal_Shdr *sec = filedata->section_headers + i;
if (sec->sh_type == type)
return sec;
}
return NULL;
}
/* Return a pointer to section NAME, or NULL if no such section exists,
restricted to the list of sections given in SET. */
static Elf_Internal_Shdr *
find_section_in_set (Filedata * filedata, const char * name, unsigned int * set)
{
unsigned int i;
if (filedata->section_headers == NULL)
return NULL;
if (set != NULL)
{
while ((i = *set++) > 0)
{
/* See PR 21156 for a reproducer. */
if (i >= filedata->file_header.e_shnum)
continue; /* FIXME: Should we issue an error message ? */
if (streq (SECTION_NAME (filedata->section_headers + i), name))
return filedata->section_headers + i;
}
}
return find_section (filedata, name);
}
/* Read an unsigned LEB128 encoded value from DATA.
Set *LENGTH_RETURN to the number of bytes read. */
static inline unsigned long
read_uleb128 (unsigned char * data,
unsigned int * length_return,
const unsigned char * const end)
{
return read_leb128 (data, length_return, FALSE, end);
}
/* 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 bfd_boolean
is_ia64_vms (Filedata * filedata)
{
return filedata->file_header.e_machine == EM_IA_64
&& filedata->file_header.e_ident[EI_OSABI] == ELFOSABI_OPENVMS;
}
/* Guess the relocation size commonly used by the specific machines. */
static bfd_boolean
guess_is_rela (unsigned int e_machine)
{
switch (e_machine)
{
/* Targets that use REL relocations. */
case EM_386:
case EM_IAMCU:
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_SCORE:
case EM_XGATE:
case EM_NFP:
return FALSE;
/* Targets that use RELA relocations. */
case EM_68K:
case EM_860:
case EM_AARCH64:
case EM_ADAPTEVA_EPIPHANY:
case EM_ALPHA:
case EM_ALTERA_NIOS2:
case EM_ARC:
case EM_ARC_COMPACT:
case EM_ARC_COMPACT2:
case EM_AVR:
case EM_AVR_OLD:
case EM_BLACKFIN:
case EM_CR16:
case EM_CRIS:
case EM_CRX:
case EM_CSKY:
case EM_D30V:
case EM_CYGNUS_D30V:
case EM_FR30:
case EM_FT32:
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_METAG:
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_NDS32:
case EM_NIOS32:
case EM_OR1K:
case EM_PPC64:
case EM_PPC:
case EM_TI_PRU:
case EM_RISCV:
case EM_RL78:
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_TILEGX:
case EM_TILEPRO:
case EM_V800:
case EM_V850:
case EM_CYGNUS_V850:
case EM_VAX:
case EM_VISIUM:
case EM_X86_64:
case EM_L1OM:
case EM_K1OM:
case EM_XSTORMY16:
case EM_XTENSA:
case EM_XTENSA_OLD:
case EM_MICROBLAZE:
case EM_MICROBLAZE_OLD:
case EM_WEBASSEMBLY:
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;
}
}
/* Load RELA type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
Returns TRUE upon success, FALSE otherwise. If successful then a
pointer to a malloc'ed buffer containing the relocs is placed in *RELASP,
and the number of relocs loaded is placed in *NRELASP. It is the caller's
responsibility to free the allocated buffer. */
static bfd_boolean
slurp_rela_relocs (Filedata * filedata,
unsigned long rel_offset,
unsigned long rel_size,
Elf_Internal_Rela ** relasp,
unsigned long * nrelasp)
{
Elf_Internal_Rela * relas;
size_t nrelas;
unsigned int i;
if (is_32bit_elf)
{
Elf32_External_Rela * erelas;
erelas = (Elf32_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
rel_size, _("32-bit relocation data"));
if (!erelas)
return FALSE;
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 FALSE;
}
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_SIGNED (erelas[i].r_addend);
}
free (erelas);
}
else
{
Elf64_External_Rela * erelas;
erelas = (Elf64_External_Rela *) get_data (NULL, filedata, rel_offset, 1,
rel_size, _("64-bit relocation data"));
if (!erelas)
return FALSE;
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 FALSE;
}
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_SIGNED (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 (filedata->file_header.e_machine == EM_MIPS
&& filedata->file_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 TRUE;
}
/* Load REL type relocations from FILEDATA at REL_OFFSET extending for REL_SIZE bytes.
Returns TRUE upon success, FALSE otherwise. If successful then a
pointer to a malloc'ed buffer containing the relocs is placed in *RELSP,
and the number of relocs loaded is placed in *NRELSP. It is the caller's
responsibility to free the allocated buffer. */