/
readelf.c
22858 lines (20143 loc) · 610 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-2022 Free Software Foundation, Inc.
Originally developed by Eric Youngdale <eric@andante.jic.com>
Modifications by Nick Clifton <nickc@redhat.com>
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>
#include <wchar.h>
#if defined HAVE_MSGPACK
#include <msgpack.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 "demanguse.h"
#include "dwarf.h"
#include "ctf-api.h"
#include "demangle.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/amdgpu.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/bpf.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/xgate.h"
#include "elf/xstormy16.h"
#include "elf/xtensa.h"
#include "elf/z80.h"
#include "elf/loongarch.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. */
#define CTF_DUMP (1 << 5) /* The --ctf 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;
};
/* A dynamic array of flags indicating for which sections a dump
has been requested via command line switches. */
struct dump_data
{
dump_type * dump_sects;
unsigned int num_dump_sects;
};
static struct dump_data cmdline;
static struct dump_list_entry * dump_sects_byname;
char * program_name = "readelf";
static bool show_name = false;
static bool do_dynamic = false;
static bool do_syms = false;
static bool do_dyn_syms = false;
static bool do_lto_syms = false;
static bool do_reloc = false;
static bool do_sections = false;
static bool do_section_groups = false;
static bool do_section_details = false;
static bool do_segments = false;
static bool do_unwind = false;
static bool do_using_dynamic = false;
static bool do_header = false;
static bool do_dump = false;
static bool do_version = false;
static bool do_histogram = false;
static bool do_debugging = false;
static bool do_ctf = false;
static bool do_arch = false;
static bool do_notes = false;
static bool do_archive_index = false;
static bool check_all = false;
static bool is_32bit_elf = false;
static bool decompress_dumps = false;
static bool do_not_show_symbol_truncation = false;
static bool do_demangle = false; /* Pretty print C++ symbol names. */
static bool process_links = false;
static bool dump_any_debugging = false;
static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
static int sym_base = 0;
static char *dump_ctf_parent_name;
static char *dump_ctf_symtab_name;
static char *dump_ctf_strtab_name;
struct group_list
{
struct group_list * next;
unsigned int section_index;
};
struct group
{
struct group_list * root;
unsigned int group_index;
};
typedef struct filedata
{
const char * file_name;
bool is_separate;
FILE * handle;
bfd_size_type file_size;
Elf_Internal_Ehdr file_header;
unsigned long archive_file_offset;
unsigned long archive_file_size;
/* Everything below this point is cleared out by free_filedata. */
Elf_Internal_Shdr * section_headers;
Elf_Internal_Phdr * program_headers;
char * string_table;
unsigned long string_table_length;
unsigned long dynamic_addr;
bfd_size_type dynamic_size;
size_t dynamic_nent;
Elf_Internal_Dyn * dynamic_section;
Elf_Internal_Shdr * dynamic_strtab_section;
char * dynamic_strings;
unsigned long dynamic_strings_length;
Elf_Internal_Shdr * dynamic_symtab_section;
unsigned long num_dynamic_syms;
Elf_Internal_Sym * dynamic_symbols;
bfd_vma version_info[16];
unsigned int dynamic_syminfo_nent;
Elf_Internal_Syminfo * dynamic_syminfo;
unsigned long dynamic_syminfo_offset;
bfd_size_type nbuckets;
bfd_size_type nchains;
bfd_vma * buckets;
bfd_vma * chains;
bfd_size_type ngnubuckets;
bfd_size_type ngnuchains;
bfd_vma * gnubuckets;
bfd_vma * gnuchains;
bfd_vma * mipsxlat;
bfd_vma gnusymidx;
char * program_interpreter;
bfd_vma dynamic_info[DT_ENCODING];
bfd_vma dynamic_info_DT_GNU_HASH;
bfd_vma dynamic_info_DT_MIPS_XHASH;
elf_section_list * symtab_shndx_list;
size_t group_count;
struct group * section_groups;
struct group ** section_headers_groups;
/* 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. */
struct dump_data dump;
} Filedata;
/* How to print a vma value. */
typedef enum print_mode
{
HEX,
HEX_5,
DEC,
DEC_5,
UNSIGNED,
UNSIGNED_5,
PREFIX_HEX,
PREFIX_HEX_5,
FULL_HEX,
LONG_HEX,
OCTAL,
OCTAL_5
}
print_mode;
typedef enum unicode_display_type
{
unicode_default = 0,
unicode_locale,
unicode_escape,
unicode_hex,
unicode_highlight,
unicode_invalid
} unicode_display_type;
static unicode_display_type unicode_display = unicode_default;
typedef enum
{
reltype_unknown,
reltype_rel,
reltype_rela,
reltype_relr
} relocation_type;
/* Versioned symbol info. */
enum versioned_symbol_info
{
symbol_undefined,
symbol_hidden,
symbol_public
};
static const char * get_symbol_version_string
(Filedata *, bool, const char *, unsigned long, unsigned,
Elf_Internal_Sym *, enum versioned_symbol_info *, unsigned short *);
#define UNKNOWN -1
static inline const char *
section_name (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
{
return filedata->string_table + hdr->sh_name;
}
static inline bool
section_name_valid (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
{
return (hdr != NULL
&& filedata->string_table != NULL
&& hdr->sh_name < filedata->string_table_length);
}
static inline const char *
section_name_print (const Filedata *filedata, const Elf_Internal_Shdr *hdr)
{
if (hdr == NULL)
return _("<none>");
if (filedata->string_table == NULL)
return _("<no-strings>");
if (hdr->sh_name >= filedata->string_table_length)
return _("<corrupt>");
return section_name (filedata, hdr);
}
#define DT_VERSIONTAGIDX(tag) (DT_VERNEEDNUM - (tag)) /* Reverse order! */
static inline bool
valid_symbol_name (const char *strtab, size_t strtab_size, uint64_t offset)
{
return strtab != NULL && offset < strtab_size;
}
static inline bool
valid_dynamic_name (const Filedata *filedata, uint64_t offset)
{
return valid_symbol_name (filedata->dynamic_strings,
filedata->dynamic_strings_length, offset);
}
/* GET_DYNAMIC_NAME asssumes that VALID_DYNAMIC_NAME has
already been called and verified that the string exists. */
static inline const char *
get_dynamic_name (const Filedata *filedata, size_t offset)
{
return filedata->dynamic_strings + offset;
}
#define REMOVE_ARCH_BITS(ADDR) \
do \
{ \
if (filedata->file_header.e_machine == EM_ARM) \
(ADDR) &= ~1; \
} \
while (0)
/* Get the correct GNU hash section name. */
#define GNU_HASH_SECTION_NAME(filedata) \
filedata->dynamic_info_DT_MIPS_XHASH ? ".MIPS.xhash" : ".gnu.hash"
/* 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 ((size_t) size != size
|| (size_t) nmemb != nmemb
|| (size_t) amt != amt)
{
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 / size != nmemb || (size_t) amt + 1 == 0)
{
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 (filedata->archive_file_offset > filedata->file_size
|| offset > filedata->file_size - filedata->archive_file_offset
|| amt > filedata->file_size - filedata->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, filedata->archive_file_offset + offset,
SEEK_SET))
{
if (reason)
error (_("Unable to seek to 0x%lx for %s\n"),
filedata->archive_file_offset + offset, reason);
return NULL;
}
mvar = var;
if (mvar == NULL)
{
/* + 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 PREFIX_HEX_5:
nc = printf ("0x");
/* Fall through. */
case HEX_5:
return nc + printf ("%05" BFD_VMA_FMT "x", vma);
case DEC:
return printf ("%" BFD_VMA_FMT "d", vma);
case UNSIGNED:
return printf ("%" BFD_VMA_FMT "u", vma);
case UNSIGNED_5:
return printf ("%5" BFD_VMA_FMT "u", vma);
case OCTAL:
return printf ("%" BFD_VMA_FMT "o", vma);
case OCTAL_5:
return printf ("%5" BFD_VMA_FMT "o", 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 truncation will happen and do_not_show_symbol_truncation is FALSE then display
abs(WIDTH) - 5 characters followed by "[...]".
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)
{
bool extra_padding = false;
bool do_dots = false;
signed int num_printed = 0;
#ifdef HAVE_MBSTATE_T
mbstate_t state;
#endif
unsigned int width_remaining;
const void * alloced_symbol = NULL;
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;
if (! do_not_show_symbol_truncation
&& (int) strlen (symbol) > width)
{
width_remaining -= 5;
if ((int) width_remaining < 0)
width_remaining = 0;
do_dots = true;
}
}
#ifdef HAVE_MBSTATE_T
/* Initialise the multibyte conversion state. */
memset (& state, 0, sizeof (state));
#endif
if (do_demangle && *symbol)
{
const char * res = cplus_demangle (symbol, demangle_flags);
if (res != NULL)
alloced_symbol = symbol = res;
}
while (width_remaining)
{
size_t n;
const char c = *symbol++;
if (c == 0)
break;
if (ISPRINT (c))
{
putchar (c);
width_remaining --;
num_printed ++;
}
else if (ISCNTRL (c))
{
/* 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 (width_remaining < 2)
break;
printf ("^%c", c + 0x40);
width_remaining -= 2;
num_printed += 2;
}
else if (c == 0x7f)
{
if (width_remaining < 5)
break;
printf ("<DEL>");
width_remaining -= 5;
num_printed += 5;
}
else if (unicode_display != unicode_locale
&& unicode_display != unicode_default)
{
/* Display unicode characters as something else. */
unsigned char bytes[4];
bool is_utf8;
unsigned int nbytes;
bytes[0] = c;
if (bytes[0] < 0xc0)
{
nbytes = 1;
is_utf8 = false;
}
else
{
bytes[1] = *symbol++;
if ((bytes[1] & 0xc0) != 0x80)
{
is_utf8 = false;
/* Do not consume this character. It may only
be the first byte in the sequence that was
corrupt. */
--symbol;
nbytes = 1;
}
else if ((bytes[0] & 0x20) == 0)
{
is_utf8 = true;
nbytes = 2;
}
else
{
bytes[2] = *symbol++;
if ((bytes[2] & 0xc0) != 0x80)
{
is_utf8 = false;
symbol -= 2;
nbytes = 1;
}
else if ((bytes[0] & 0x10) == 0)
{
is_utf8 = true;
nbytes = 3;
}
else
{
bytes[3] = *symbol++;
nbytes = 4;
if ((bytes[3] & 0xc0) != 0x80)
{
is_utf8 = false;
symbol -= 3;
nbytes = 1;
}
else
is_utf8 = true;
}
}
}
if (unicode_display == unicode_invalid)
is_utf8 = false;
if (unicode_display == unicode_hex || ! is_utf8)
{
unsigned int i;
if (width_remaining < (nbytes * 2) + 2)
break;
putchar (is_utf8 ? '<' : '{');
printf ("0x");
for (i = 0; i < nbytes; i++)
printf ("%02x", bytes[i]);
putchar (is_utf8 ? '>' : '}');
}
else
{
if (unicode_display == unicode_highlight && isatty (1))
printf ("\x1B[31;47m"); /* Red. */
switch (nbytes)
{
case 2:
if (width_remaining < 6)
break;
printf ("\\u%02x%02x",
(bytes[0] & 0x1c) >> 2,
((bytes[0] & 0x03) << 6) | (bytes[1] & 0x3f));
break;
case 3:
if (width_remaining < 6)
break;
printf ("\\u%02x%02x",
((bytes[0] & 0x0f) << 4) | ((bytes[1] & 0x3c) >> 2),
((bytes[1] & 0x03) << 6) | (bytes[2] & 0x3f));
break;
case 4:
if (width_remaining < 8)
break;
printf ("\\u%02x%02x%02x",
((bytes[0] & 0x07) << 6) | ((bytes[1] & 0x3c) >> 2),
((bytes[1] & 0x03) << 6) | ((bytes[2] & 0x3c) >> 2),
((bytes[2] & 0x03) << 6) | (bytes[3] & 0x3f));
break;
default:
/* URG. */
break;
}
if (unicode_display == unicode_highlight && isatty (1))
printf ("\033[0m"); /* Default colour. */
}
if (bytes[nbytes - 1] == 0)
break;
}
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 (do_dots)
num_printed += printf ("[...]");
if (extra_padding && num_printed < width)
{
/* Fill in the remaining spaces. */
printf ("%-*s", width - num_printed, " ");
num_printed = width;
}
free ((void *) alloced_symbol);
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 256
static char sec_name_buf [MAX_PRINT_SEC_NAME_LEN + 1];
const char * name = section_name_print (filedata, 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 (section_name_valid (filedata, filedata->section_headers + i)
&& streq (section_name (filedata, 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;