-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjdump.c
3510 lines (2991 loc) · 90.4 KB
/
objdump.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
/* objdump.c -- dump information about an object file.
Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
Free Software Foundation, Inc.
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, 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, 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
/* Objdump overview.
Objdump displays information about one or more object files, either on
their own, or inside libraries. It is commonly used as a disassembler,
but it can also display information about file headers, symbol tables,
relocations, debugging directives and more.
The flow of execution is as follows:
1. Command line arguments are checked for control switches and the
information to be displayed is selected.
2. Any remaining arguments are assumed to be object files, and they are
processed in order by display_bfd(). If the file is an archive each
of its elements is processed in turn.
3. The file's target architecture and binary file format are determined
by bfd_check_format(). If they are recognised, then dump_bfd() is
called.
4. dump_bfd() in turn calls separate functions to display the requested
item(s) of information(s). For example disassemble_data() is called if
a disassembly has been requested.
When disassembling the code loops through blocks of instructions bounded
by symbols, calling disassemble_bytes() on each block. The actual
disassembling is done by the libopcodes library, via a function pointer
supplied by the disassembler() function. */
#include "sysdep.h"
#include "bfd.h"
#include "elf-bfd.h"
#include "progress.h"
#include "bucomm.h"
#include "dwarf.h"
#include "getopt.h"
#include "safe-ctype.h"
#include "dis-asm.h"
#include "libiberty.h"
#include "demangle.h"
#include "filenames.h"
#include "debug.h"
#include "budbg.h"
#ifdef HAVE_MMAP
#include <sys/mman.h>
#endif
#include <sys/stat.h>
/* Internal headers for the ELF .stab-dump code - sorry. */
#define BYTES_IN_WORD 32
#include "aout/aout64.h"
/* Exit status. */
static int exit_status = 0;
static char *default_target = NULL; /* Default at runtime. */
/* The following variables are set based on arguments passed on the
command line. */
static int show_version = 0; /* Show the version number. */
static int dump_section_contents; /* -s */
static int dump_section_headers; /* -h */
static bfd_boolean dump_file_header; /* -f */
static int dump_symtab; /* -t */
static int dump_dynamic_symtab; /* -T */
static int dump_reloc_info; /* -r */
static int dump_dynamic_reloc_info; /* -R */
static int dump_ar_hdrs; /* -a */
static int dump_private_headers; /* -p */
static int prefix_addresses; /* --prefix-addresses */
static int with_line_numbers; /* -l */
static bfd_boolean with_source_code; /* -S */
static int show_raw_insn; /* --show-raw-insn */
static int dump_dwarf_section_info; /* --dwarf */
static int dump_stab_section_info; /* --stabs */
static int do_demangle; /* -C, --demangle */
static bfd_boolean disassemble; /* -d */
static bfd_boolean disassemble_all; /* -D */
static int disassemble_zeroes; /* --disassemble-zeroes */
static bfd_boolean formats_info; /* -i */
static int wide_output; /* -w */
static int insn_width; /* --insn-width */
static bfd_vma start_address = (bfd_vma) -1; /* --start-address */
static bfd_vma stop_address = (bfd_vma) -1; /* --stop-address */
static int dump_debugging; /* --debugging */
static int dump_debugging_tags; /* --debugging-tags */
static int dump_special_syms = 0; /* --special-syms */
static bfd_vma adjust_section_vma = 0; /* --adjust-vma */
static int file_start_context = 0; /* --file-start-context */
static bfd_boolean display_file_offsets;/* -F */
static const char *prefix; /* --prefix */
static int prefix_strip; /* --prefix-strip */
static size_t prefix_length;
/* A structure to record the sections mentioned in -j switches. */
struct only
{
const char * name; /* The name of the section. */
bfd_boolean seen; /* A flag to indicate that the section has been found in one or more input files. */
struct only * next; /* Pointer to the next structure in the list. */
};
/* Pointer to an array of 'only' structures.
This pointer is NULL if the -j switch has not been used. */
static struct only * only_list = NULL;
/* Variables for handling include file path table. */
static const char **include_paths;
static int include_path_count;
/* Extra info to pass to the section disassembler and address printing
function. */
struct objdump_disasm_info
{
bfd * abfd;
asection * sec;
bfd_boolean require_sec;
arelent ** dynrelbuf;
long dynrelcount;
disassembler_ftype disassemble_fn;
arelent * reloc;
};
/* Architecture to disassemble for, or default if NULL. */
static char *machine = NULL;
/* Target specific options to the disassembler. */
static char *disassembler_options = NULL;
/* Endianness to disassemble for, or default if BFD_ENDIAN_UNKNOWN. */
static enum bfd_endian endian = BFD_ENDIAN_UNKNOWN;
/* The symbol table. */
static asymbol **syms;
/* Number of symbols in `syms'. */
static long symcount = 0;
/* The sorted symbol table. */
static asymbol **sorted_syms;
/* Number of symbols in `sorted_syms'. */
static long sorted_symcount = 0;
/* The dynamic symbol table. */
static asymbol **dynsyms;
/* The synthetic symbol table. */
static asymbol *synthsyms;
static long synthcount = 0;
/* Number of symbols in `dynsyms'. */
static long dynsymcount = 0;
static bfd_byte *stabs;
static bfd_size_type stab_size;
static char *strtab;
static bfd_size_type stabstr_size;
static bfd_boolean is_relocatable = FALSE;
static void
usage (FILE *stream, int status)
{
fprintf (stream, _("Usage: %s <option(s)> <file(s)>\n"), program_name);
fprintf (stream, _(" Display information from object <file(s)>.\n"));
fprintf (stream, _(" At least one of the following switches must be given:\n"));
fprintf (stream, _("\
-a, --archive-headers Display archive header information\n\
-f, --file-headers Display the contents of the overall file header\n\
-p, --private-headers Display object format specific file header contents\n\
-h, --[section-]headers Display the contents of the section headers\n\
-x, --all-headers Display the contents of all headers\n\
-d, --disassemble Display assembler contents of executable sections\n\
-D, --disassemble-all Display assembler contents of all sections\n\
-S, --source Intermix source code with disassembly\n\
-s, --full-contents Display the full contents of all sections requested\n\
-g, --debugging Display debug information in object file\n\
-e, --debugging-tags Display debug information using ctags style\n\
-G, --stabs Display (in raw form) any STABS info in the file\n\
-W[lLiaprmfFsoRt] or\n\
--dwarf[=rawline,=decodedline,=info,=abbrev,=pubnames,=aranges,=macro,=frames,\n\
=frames-interp,=str,=loc,=Ranges,=pubtypes,\n\
=trace_info,=trace_abbrev,=trace_aranges]\n\
Display DWARF info in the file\n\
-t, --syms Display the contents of the symbol table(s)\n\
-T, --dynamic-syms Display the contents of the dynamic symbol table\n\
-r, --reloc Display the relocation entries in the file\n\
-R, --dynamic-reloc Display the dynamic relocation entries in the file\n\
@<file> Read options from <file>\n\
-v, --version Display this program's version number\n\
-i, --info List object formats and architectures supported\n\
-H, --help Display this information\n\
"));
if (status != 2)
{
fprintf (stream, _("\n The following switches are optional:\n"));
fprintf (stream, _("\
-b, --target=BFDNAME Specify the target object format as BFDNAME\n\
-m, --architecture=MACHINE Specify the target architecture as MACHINE\n\
-j, --section=NAME Only display information for section NAME\n\
-M, --disassembler-options=OPT Pass text OPT on to the disassembler\n\
-EB --endian=big Assume big endian format when disassembling\n\
-EL --endian=little Assume little endian format when disassembling\n\
--file-start-context Include context from start of file (with -S)\n\
-I, --include=DIR Add DIR to search list for source files\n\
-l, --line-numbers Include line numbers and filenames in output\n\
-F, --file-offsets Include file offsets when displaying information\n\
-C, --demangle[=STYLE] Decode mangled/processed symbol names\n\
The STYLE, if specified, can be `auto', `gnu',\n\
`lucid', `arm', `hp', `edg', `gnu-v3', `java'\n\
or `gnat'\n\
-w, --wide Format output for more than 80 columns\n\
-z, --disassemble-zeroes Do not skip blocks of zeroes when disassembling\n\
--start-address=ADDR Only process data whose address is >= ADDR\n\
--stop-address=ADDR Only process data whose address is <= ADDR\n\
--prefix-addresses Print complete address alongside disassembly\n\
--[no-]show-raw-insn Display hex alongside symbolic disassembly\n\
--insn-width=WIDTH Display WIDTH bytes on a signle line for -d\n\
--adjust-vma=OFFSET Add OFFSET to all displayed section addresses\n\
--special-syms Include special symbols in symbol dumps\n\
--prefix=PREFIX Add PREFIX to absolute paths for -S\n\
--prefix-strip=LEVEL Strip initial directory names for -S\n\
\n"));
list_supported_targets (program_name, stream);
list_supported_architectures (program_name, stream);
disassembler_usage (stream);
}
if (REPORT_BUGS_TO[0] && status == 0)
fprintf (stream, _("Report bugs to %s.\n"), REPORT_BUGS_TO);
exit (status);
}
/* 150 isn't special; it's just an arbitrary non-ASCII char value. */
enum option_values
{
OPTION_ENDIAN=150,
OPTION_START_ADDRESS,
OPTION_STOP_ADDRESS,
OPTION_DWARF,
OPTION_PREFIX,
OPTION_PREFIX_STRIP,
OPTION_INSN_WIDTH,
OPTION_ADJUST_VMA
};
static struct option long_options[]=
{
{"adjust-vma", required_argument, NULL, OPTION_ADJUST_VMA},
{"all-headers", no_argument, NULL, 'x'},
{"private-headers", no_argument, NULL, 'p'},
{"architecture", required_argument, NULL, 'm'},
{"archive-headers", no_argument, NULL, 'a'},
{"debugging", no_argument, NULL, 'g'},
{"debugging-tags", no_argument, NULL, 'e'},
{"demangle", optional_argument, NULL, 'C'},
{"disassemble", no_argument, NULL, 'd'},
{"disassemble-all", no_argument, NULL, 'D'},
{"disassembler-options", required_argument, NULL, 'M'},
{"disassemble-zeroes", no_argument, NULL, 'z'},
{"dynamic-reloc", no_argument, NULL, 'R'},
{"dynamic-syms", no_argument, NULL, 'T'},
{"endian", required_argument, NULL, OPTION_ENDIAN},
{"file-headers", no_argument, NULL, 'f'},
{"file-offsets", no_argument, NULL, 'F'},
{"file-start-context", no_argument, &file_start_context, 1},
{"full-contents", no_argument, NULL, 's'},
{"headers", no_argument, NULL, 'h'},
{"help", no_argument, NULL, 'H'},
{"info", no_argument, NULL, 'i'},
{"line-numbers", no_argument, NULL, 'l'},
{"no-show-raw-insn", no_argument, &show_raw_insn, -1},
{"prefix-addresses", no_argument, &prefix_addresses, 1},
{"reloc", no_argument, NULL, 'r'},
{"section", required_argument, NULL, 'j'},
{"section-headers", no_argument, NULL, 'h'},
{"show-raw-insn", no_argument, &show_raw_insn, 1},
{"source", no_argument, NULL, 'S'},
{"special-syms", no_argument, &dump_special_syms, 1},
{"include", required_argument, NULL, 'I'},
{"dwarf", optional_argument, NULL, OPTION_DWARF},
{"stabs", no_argument, NULL, 'G'},
{"start-address", required_argument, NULL, OPTION_START_ADDRESS},
{"stop-address", required_argument, NULL, OPTION_STOP_ADDRESS},
{"syms", no_argument, NULL, 't'},
{"target", required_argument, NULL, 'b'},
{"version", no_argument, NULL, 'V'},
{"wide", no_argument, NULL, 'w'},
{"prefix", required_argument, NULL, OPTION_PREFIX},
{"prefix-strip", required_argument, NULL, OPTION_PREFIX_STRIP},
{"insn-width", required_argument, NULL, OPTION_INSN_WIDTH},
{0, no_argument, 0, 0}
};
static void
nonfatal (const char *msg)
{
bfd_nonfatal (msg);
exit_status = 1;
}
/* Returns TRUE if the specified section should be dumped. */
static bfd_boolean
process_section_p (asection * section)
{
struct only * only;
if (only_list == NULL)
return TRUE;
for (only = only_list; only; only = only->next)
if (strcmp (only->name, section->name) == 0)
{
only->seen = TRUE;
return TRUE;
}
return FALSE;
}
/* Add an entry to the 'only' list. */
static void
add_only (char * name)
{
struct only * only;
/* First check to make sure that we do not
already have an entry for this name. */
for (only = only_list; only; only = only->next)
if (strcmp (only->name, name) == 0)
return;
only = xmalloc (sizeof * only);
only->name = name;
only->seen = FALSE;
only->next = only_list;
only_list = only;
}
/* Release the memory used by the 'only' list.
PR 11225: Issue a warning message for unseen sections.
Only do this if none of the sections were seen. This is mainly to support
tools like the GAS testsuite where an object file is dumped with a list of
generic section names known to be present in a range of different file
formats. */
static void
free_only_list (void)
{
bfd_boolean at_least_one_seen = FALSE;
struct only * only;
struct only * next;
if (only_list == NULL)
return;
for (only = only_list; only; only = only->next)
if (only->seen)
{
at_least_one_seen = TRUE;
break;
}
for (only = only_list; only; only = next)
{
if (! at_least_one_seen)
{
non_fatal (_("section '%s' mentioned in a -j option, "
"but not found in any input file"),
only->name);
exit_status = 1;
}
next = only->next;
free (only);
}
}
static void
dump_section_header (bfd *abfd, asection *section,
void *ignored ATTRIBUTE_UNUSED)
{
char *comma = "";
unsigned int opb = bfd_octets_per_byte (abfd);
/* Ignore linker created section. See elfNN_ia64_object_p in
bfd/elfxx-ia64.c. */
if (section->flags & SEC_LINKER_CREATED)
return;
/* PR 10413: Skip sections that we are ignoring. */
if (! process_section_p (section))
return;
printf ("%3d %-13s %08lx ", section->index,
bfd_get_section_name (abfd, section),
(unsigned long) bfd_section_size (abfd, section) / opb);
bfd_printf_vma (abfd, bfd_get_section_vma (abfd, section));
printf (" ");
bfd_printf_vma (abfd, section->lma);
printf (" %08lx 2**%u", (unsigned long) section->filepos,
bfd_get_section_alignment (abfd, section));
if (! wide_output)
printf ("\n ");
printf (" ");
#define PF(x, y) \
if (section->flags & x) { printf ("%s%s", comma, y); comma = ", "; }
PF (SEC_HAS_CONTENTS, "CONTENTS");
PF (SEC_ALLOC, "ALLOC");
PF (SEC_CONSTRUCTOR, "CONSTRUCTOR");
PF (SEC_LOAD, "LOAD");
PF (SEC_RELOC, "RELOC");
PF (SEC_READONLY, "READONLY");
PF (SEC_CODE, "CODE");
PF (SEC_DATA, "DATA");
PF (SEC_ROM, "ROM");
PF (SEC_DEBUGGING, "DEBUGGING");
PF (SEC_NEVER_LOAD, "NEVER_LOAD");
PF (SEC_EXCLUDE, "EXCLUDE");
PF (SEC_SORT_ENTRIES, "SORT_ENTRIES");
if (bfd_get_arch (abfd) == bfd_arch_tic54x)
{
PF (SEC_TIC54X_BLOCK, "BLOCK");
PF (SEC_TIC54X_CLINK, "CLINK");
}
PF (SEC_SMALL_DATA, "SMALL_DATA");
if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
PF (SEC_COFF_SHARED, "SHARED");
PF (SEC_THREAD_LOCAL, "THREAD_LOCAL");
PF (SEC_GROUP, "GROUP");
if ((section->flags & SEC_LINK_ONCE) != 0)
{
const char *ls;
struct coff_comdat_info *comdat;
switch (section->flags & SEC_LINK_DUPLICATES)
{
default:
abort ();
case SEC_LINK_DUPLICATES_DISCARD:
ls = "LINK_ONCE_DISCARD";
break;
case SEC_LINK_DUPLICATES_ONE_ONLY:
ls = "LINK_ONCE_ONE_ONLY";
break;
case SEC_LINK_DUPLICATES_SAME_SIZE:
ls = "LINK_ONCE_SAME_SIZE";
break;
case SEC_LINK_DUPLICATES_SAME_CONTENTS:
ls = "LINK_ONCE_SAME_CONTENTS";
break;
}
printf ("%s%s", comma, ls);
comdat = bfd_coff_get_comdat_section (abfd, section);
if (comdat != NULL)
printf (" (COMDAT %s %ld)", comdat->name, comdat->symbol);
comma = ", ";
}
printf ("\n");
#undef PF
}
static void
dump_headers (bfd *abfd)
{
printf (_("Sections:\n"));
#ifndef BFD64
printf (_("Idx Name Size VMA LMA File off Algn"));
#else
/* With BFD64, non-ELF returns -1 and wants always 64 bit addresses. */
if (bfd_get_arch_size (abfd) == 32)
printf (_("Idx Name Size VMA LMA File off Algn"));
else
printf (_("Idx Name Size VMA LMA File off Algn"));
#endif
if (wide_output)
printf (_(" Flags"));
if (abfd->flags & HAS_LOAD_PAGE)
printf (_(" Pg"));
printf ("\n");
bfd_map_over_sections (abfd, dump_section_header, NULL);
}
static asymbol **
slurp_symtab (bfd *abfd)
{
asymbol **sy = NULL;
long storage;
if (!(bfd_get_file_flags (abfd) & HAS_SYMS))
{
symcount = 0;
return NULL;
}
storage = bfd_get_symtab_upper_bound (abfd);
if (storage < 0)
bfd_fatal (bfd_get_filename (abfd));
if (storage)
sy = (asymbol **) xmalloc (storage);
symcount = bfd_canonicalize_symtab (abfd, sy);
if (symcount < 0)
bfd_fatal (bfd_get_filename (abfd));
return sy;
}
/* Read in the dynamic symbols. */
static asymbol **
slurp_dynamic_symtab (bfd *abfd)
{
asymbol **sy = NULL;
long storage;
storage = bfd_get_dynamic_symtab_upper_bound (abfd);
if (storage < 0)
{
if (!(bfd_get_file_flags (abfd) & DYNAMIC))
{
non_fatal (_("%s: not a dynamic object"), bfd_get_filename (abfd));
exit_status = 1;
dynsymcount = 0;
return NULL;
}
bfd_fatal (bfd_get_filename (abfd));
}
if (storage)
sy = (asymbol **) xmalloc (storage);
dynsymcount = bfd_canonicalize_dynamic_symtab (abfd, sy);
if (dynsymcount < 0)
bfd_fatal (bfd_get_filename (abfd));
return sy;
}
/* Filter out (in place) symbols that are useless for disassembly.
COUNT is the number of elements in SYMBOLS.
Return the number of useful symbols. */
static long
remove_useless_symbols (asymbol **symbols, long count)
{
asymbol **in_ptr = symbols, **out_ptr = symbols;
while (--count >= 0)
{
asymbol *sym = *in_ptr++;
if (sym->name == NULL || sym->name[0] == '\0')
continue;
if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
continue;
if (bfd_is_und_section (sym->section)
|| bfd_is_com_section (sym->section))
continue;
*out_ptr++ = sym;
}
return out_ptr - symbols;
}
/* Sort symbols into value order. */
static int
compare_symbols (const void *ap, const void *bp)
{
const asymbol *a = * (const asymbol **) ap;
const asymbol *b = * (const asymbol **) bp;
const char *an;
const char *bn;
size_t anl;
size_t bnl;
bfd_boolean af;
bfd_boolean bf;
flagword aflags;
flagword bflags;
if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
return 1;
else if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
return -1;
if (a->section > b->section)
return 1;
else if (a->section < b->section)
return -1;
an = bfd_asymbol_name (a);
bn = bfd_asymbol_name (b);
anl = strlen (an);
bnl = strlen (bn);
/* The symbols gnu_compiled and gcc2_compiled convey no real
information, so put them after other symbols with the same value. */
af = (strstr (an, "gnu_compiled") != NULL
|| strstr (an, "gcc2_compiled") != NULL);
bf = (strstr (bn, "gnu_compiled") != NULL
|| strstr (bn, "gcc2_compiled") != NULL);
if (af && ! bf)
return 1;
if (! af && bf)
return -1;
/* We use a heuristic for the file name, to try to sort it after
more useful symbols. It may not work on non Unix systems, but it
doesn't really matter; the only difference is precisely which
symbol names get printed. */
#define file_symbol(s, sn, snl) \
(((s)->flags & BSF_FILE) != 0 \
|| ((sn)[(snl) - 2] == '.' \
&& ((sn)[(snl) - 1] == 'o' \
|| (sn)[(snl) - 1] == 'a')))
af = file_symbol (a, an, anl);
bf = file_symbol (b, bn, bnl);
if (af && ! bf)
return 1;
if (! af && bf)
return -1;
/* Try to sort global symbols before local symbols before function
symbols before debugging symbols. */
aflags = a->flags;
bflags = b->flags;
if ((aflags & BSF_DEBUGGING) != (bflags & BSF_DEBUGGING))
{
if ((aflags & BSF_DEBUGGING) != 0)
return 1;
else
return -1;
}
if ((aflags & BSF_FUNCTION) != (bflags & BSF_FUNCTION))
{
if ((aflags & BSF_FUNCTION) != 0)
return -1;
else
return 1;
}
if ((aflags & BSF_LOCAL) != (bflags & BSF_LOCAL))
{
if ((aflags & BSF_LOCAL) != 0)
return 1;
else
return -1;
}
if ((aflags & BSF_GLOBAL) != (bflags & BSF_GLOBAL))
{
if ((aflags & BSF_GLOBAL) != 0)
return -1;
else
return 1;
}
/* Symbols that start with '.' might be section names, so sort them
after symbols that don't start with '.'. */
if (an[0] == '.' && bn[0] != '.')
return 1;
if (an[0] != '.' && bn[0] == '.')
return -1;
/* Finally, if we can't distinguish them in any other way, try to
get consistent results by sorting the symbols by name. */
return strcmp (an, bn);
}
/* Sort relocs into address order. */
static int
compare_relocs (const void *ap, const void *bp)
{
const arelent *a = * (const arelent **) ap;
const arelent *b = * (const arelent **) bp;
if (a->address > b->address)
return 1;
else if (a->address < b->address)
return -1;
/* So that associated relocations tied to the same address show up
in the correct order, we don't do any further sorting. */
if (a > b)
return 1;
else if (a < b)
return -1;
else
return 0;
}
/* Print an address (VMA) to the output stream in INFO.
If SKIP_ZEROES is TRUE, omit leading zeroes. */
static void
objdump_print_value (bfd_vma vma, struct disassemble_info *inf,
bfd_boolean skip_zeroes)
{
char buf[30];
char *p;
struct objdump_disasm_info *aux;
aux = (struct objdump_disasm_info *) inf->application_data;
bfd_sprintf_vma (aux->abfd, buf, vma);
if (! skip_zeroes)
p = buf;
else
{
for (p = buf; *p == '0'; ++p)
;
if (*p == '\0')
--p;
}
(*inf->fprintf_func) (inf->stream, "%s", p);
}
/* Print the name of a symbol. */
static void
objdump_print_symname (bfd *abfd, struct disassemble_info *inf,
asymbol *sym)
{
char *alloc;
const char *name;
alloc = NULL;
name = bfd_asymbol_name (sym);
if (do_demangle && name[0] != '\0')
{
/* Demangle the name. */
alloc = bfd_demangle (abfd, name, DMGL_ANSI | DMGL_PARAMS);
if (alloc != NULL)
name = alloc;
}
if (inf != NULL)
(*inf->fprintf_func) (inf->stream, "%s", name);
else
printf ("%s", name);
if (alloc != NULL)
free (alloc);
}
/* Locate a symbol given a bfd and a section (from INFO->application_data),
and a VMA. If INFO->application_data->require_sec is TRUE, then always
require the symbol to be in the section. Returns NULL if there is no
suitable symbol. If PLACE is not NULL, then *PLACE is set to the index
of the symbol in sorted_syms. */
static asymbol *
find_symbol_for_address (bfd_vma vma,
struct disassemble_info *inf,
long *place)
{
/* @@ Would it speed things up to cache the last two symbols returned,
and maybe their address ranges? For many processors, only one memory
operand can be present at a time, so the 2-entry cache wouldn't be
constantly churned by code doing heavy memory accesses. */
/* Indices in `sorted_syms'. */
long min = 0;
long max_count = sorted_symcount;
long thisplace;
struct objdump_disasm_info *aux;
bfd *abfd;
asection *sec;
unsigned int opb;
bfd_boolean want_section;
if (sorted_symcount < 1)
return NULL;
aux = (struct objdump_disasm_info *) inf->application_data;
abfd = aux->abfd;
sec = aux->sec;
opb = inf->octets_per_byte;
/* Perform a binary search looking for the closest symbol to the
required value. We are searching the range (min, max_count]. */
while (min + 1 < max_count)
{
asymbol *sym;
thisplace = (max_count + min) / 2;
sym = sorted_syms[thisplace];
if (bfd_asymbol_value (sym) > vma)
max_count = thisplace;
else if (bfd_asymbol_value (sym) < vma)
min = thisplace;
else
{
min = thisplace;
break;
}
}
/* The symbol we want is now in min, the low end of the range we
were searching. If there are several symbols with the same
value, we want the first one. */
thisplace = min;
while (thisplace > 0
&& (bfd_asymbol_value (sorted_syms[thisplace])
== bfd_asymbol_value (sorted_syms[thisplace - 1])))
--thisplace;
/* Prefer a symbol in the current section if we have multple symbols
with the same value, as can occur with overlays or zero size
sections. */
min = thisplace;
while (min < max_count
&& (bfd_asymbol_value (sorted_syms[min])
== bfd_asymbol_value (sorted_syms[thisplace])))
{
if (sorted_syms[min]->section == sec
&& inf->symbol_is_valid (sorted_syms[min], inf))
{
thisplace = min;
if (place != NULL)
*place = thisplace;
return sorted_syms[thisplace];
}
++min;
}
/* If the file is relocatable, and the symbol could be from this
section, prefer a symbol from this section over symbols from
others, even if the other symbol's value might be closer.
Note that this may be wrong for some symbol references if the
sections have overlapping memory ranges, but in that case there's
no way to tell what's desired without looking at the relocation
table.
Also give the target a chance to reject symbols. */
want_section = (aux->require_sec
|| ((abfd->flags & HAS_RELOC) != 0
&& vma >= bfd_get_section_vma (abfd, sec)
&& vma < (bfd_get_section_vma (abfd, sec)
+ bfd_section_size (abfd, sec) / opb)));
if ((sorted_syms[thisplace]->section != sec && want_section)
|| ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
{
long i;
long newplace = sorted_symcount;
for (i = min - 1; i >= 0; i--)
{
if ((sorted_syms[i]->section == sec || !want_section)
&& inf->symbol_is_valid (sorted_syms[i], inf))
{
if (newplace == sorted_symcount)
newplace = i;
if (bfd_asymbol_value (sorted_syms[i])
!= bfd_asymbol_value (sorted_syms[newplace]))
break;
/* Remember this symbol and keep searching until we reach
an earlier address. */
newplace = i;
}
}
if (newplace != sorted_symcount)
thisplace = newplace;
else
{
/* We didn't find a good symbol with a smaller value.
Look for one with a larger value. */
for (i = thisplace + 1; i < sorted_symcount; i++)
{
if ((sorted_syms[i]->section == sec || !want_section)
&& inf->symbol_is_valid (sorted_syms[i], inf))
{
thisplace = i;
break;
}
}
}
if ((sorted_syms[thisplace]->section != sec && want_section)
|| ! inf->symbol_is_valid (sorted_syms[thisplace], inf))
/* There is no suitable symbol. */
return NULL;
}
if (place != NULL)
*place = thisplace;
return sorted_syms[thisplace];
}
/* Print an address and the offset to the nearest symbol. */
static void
objdump_print_addr_with_sym (bfd *abfd, asection *sec, asymbol *sym,
bfd_vma vma, struct disassemble_info *inf,
bfd_boolean skip_zeroes)
{
objdump_print_value (vma, inf, skip_zeroes);
if (sym == NULL)
{
bfd_vma secaddr;
(*inf->fprintf_func) (inf->stream, " <%s",
bfd_get_section_name (abfd, sec));
secaddr = bfd_get_section_vma (abfd, sec);
if (vma < secaddr)
{
(*inf->fprintf_func) (inf->stream, "-0x");
objdump_print_value (secaddr - vma, inf, TRUE);
}
else if (vma > secaddr)
{
(*inf->fprintf_func) (inf->stream, "+0x");
objdump_print_value (vma - secaddr, inf, TRUE);
}
(*inf->fprintf_func) (inf->stream, ">");
}
else
{
(*inf->fprintf_func) (inf->stream, " <");
objdump_print_symname (abfd, inf, sym);
if (bfd_asymbol_value (sym) > vma)
{
(*inf->fprintf_func) (inf->stream, "-0x");
objdump_print_value (bfd_asymbol_value (sym) - vma, inf, TRUE);
}
else if (vma > bfd_asymbol_value (sym))
{
(*inf->fprintf_func) (inf->stream, "+0x");
objdump_print_value (vma - bfd_asymbol_value (sym), inf, TRUE);
}
(*inf->fprintf_func) (inf->stream, ">");
}
if (display_file_offsets)
inf->fprintf_func (inf->stream, _(" (File Offset: 0x%lx)"),
(long int)(sec->filepos + (vma - sec->vma)));
}
/* Print an address (VMA), symbolically if possible.
If SKIP_ZEROES is TRUE, don't output leading zeroes. */
static void
objdump_print_addr (bfd_vma vma,
struct disassemble_info *inf,
bfd_boolean skip_zeroes)
{
struct objdump_disasm_info *aux;
asymbol *sym = NULL;
bfd_boolean skip_find = FALSE;
aux = (struct objdump_disasm_info *) inf->application_data;