forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDWARFContext.cpp
2029 lines (1808 loc) · 74.7 KB
/
DWARFContext.cpp
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
//===- DWARFContext.cpp ---------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugAddr.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugArangeSet.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugAranges.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugFrame.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugLoc.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugRangeList.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugRnglists.h"
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
#include "llvm/DebugInfo/DWARF/DWARFGdbIndex.h"
#include "llvm/DebugInfo/DWARF/DWARFSection.h"
#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Object/Decompressor.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Object/RelocationResolver.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/DataExtractor.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/LEB128.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cstdint>
#include <deque>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace llvm;
using namespace dwarf;
using namespace object;
#define DEBUG_TYPE "dwarf"
using DWARFLineTable = DWARFDebugLine::LineTable;
using FileLineInfoKind = DILineInfoSpecifier::FileLineInfoKind;
using FunctionNameKind = DILineInfoSpecifier::FunctionNameKind;
DWARFContext::DWARFContext(std::unique_ptr<const DWARFObject> DObj,
std::string DWPName,
std::function<void(Error)> RecoverableErrorHandler,
std::function<void(Error)> WarningHandler)
: DIContext(CK_DWARF), DWPName(std::move(DWPName)),
RecoverableErrorHandler(RecoverableErrorHandler),
WarningHandler(WarningHandler), DObj(std::move(DObj)) {}
DWARFContext::~DWARFContext() = default;
/// Dump the UUID load command.
static void dumpUUID(raw_ostream &OS, const ObjectFile &Obj) {
auto *MachO = dyn_cast<MachOObjectFile>(&Obj);
if (!MachO)
return;
for (auto LC : MachO->load_commands()) {
raw_ostream::uuid_t UUID;
if (LC.C.cmd == MachO::LC_UUID) {
if (LC.C.cmdsize < sizeof(UUID) + sizeof(LC.C)) {
OS << "error: UUID load command is too short.\n";
return;
}
OS << "UUID: ";
memcpy(&UUID, LC.Ptr+sizeof(LC.C), sizeof(UUID));
OS.write_uuid(UUID);
Triple T = MachO->getArchTriple();
OS << " (" << T.getArchName() << ')';
OS << ' ' << MachO->getFileName() << '\n';
}
}
}
using ContributionCollection =
std::vector<Optional<StrOffsetsContributionDescriptor>>;
// Collect all the contributions to the string offsets table from all units,
// sort them by their starting offsets and remove duplicates.
static ContributionCollection
collectContributionData(DWARFContext::unit_iterator_range Units) {
ContributionCollection Contributions;
for (const auto &U : Units)
if (const auto &C = U->getStringOffsetsTableContribution())
Contributions.push_back(C);
// Sort the contributions so that any invalid ones are placed at
// the start of the contributions vector. This way they are reported
// first.
llvm::sort(Contributions,
[](const Optional<StrOffsetsContributionDescriptor> &L,
const Optional<StrOffsetsContributionDescriptor> &R) {
if (L && R)
return L->Base < R->Base;
return R.hasValue();
});
// Uniquify contributions, as it is possible that units (specifically
// type units in dwo or dwp files) share contributions. We don't want
// to report them more than once.
Contributions.erase(
std::unique(Contributions.begin(), Contributions.end(),
[](const Optional<StrOffsetsContributionDescriptor> &L,
const Optional<StrOffsetsContributionDescriptor> &R) {
if (L && R)
return L->Base == R->Base && L->Size == R->Size;
return false;
}),
Contributions.end());
return Contributions;
}
// Dump a DWARF string offsets section. This may be a DWARF v5 formatted
// string offsets section, where each compile or type unit contributes a
// number of entries (string offsets), with each contribution preceded by
// a header containing size and version number. Alternatively, it may be a
// monolithic series of string offsets, as generated by the pre-DWARF v5
// implementation of split DWARF; however, in that case we still need to
// collect contributions of units because the size of the offsets (4 or 8
// bytes) depends on the format of the referencing unit (DWARF32 or DWARF64).
static void dumpStringOffsetsSection(raw_ostream &OS, DIDumpOptions DumpOpts,
StringRef SectionName,
const DWARFObject &Obj,
const DWARFSection &StringOffsetsSection,
StringRef StringSection,
DWARFContext::unit_iterator_range Units,
bool LittleEndian) {
auto Contributions = collectContributionData(Units);
DWARFDataExtractor StrOffsetExt(Obj, StringOffsetsSection, LittleEndian, 0);
DataExtractor StrData(StringSection, LittleEndian, 0);
uint64_t SectionSize = StringOffsetsSection.Data.size();
uint64_t Offset = 0;
for (auto &Contribution : Contributions) {
// Report an ill-formed contribution.
if (!Contribution) {
OS << "error: invalid contribution to string offsets table in section ."
<< SectionName << ".\n";
return;
}
dwarf::DwarfFormat Format = Contribution->getFormat();
int OffsetDumpWidth = 2 * dwarf::getDwarfOffsetByteSize(Format);
uint16_t Version = Contribution->getVersion();
uint64_t ContributionHeader = Contribution->Base;
// In DWARF v5 there is a contribution header that immediately precedes
// the string offsets base (the location we have previously retrieved from
// the CU DIE's DW_AT_str_offsets attribute). The header is located either
// 8 or 16 bytes before the base, depending on the contribution's format.
if (Version >= 5)
ContributionHeader -= Format == DWARF32 ? 8 : 16;
// Detect overlapping contributions.
if (Offset > ContributionHeader) {
DumpOpts.RecoverableErrorHandler(createStringError(
errc::invalid_argument,
"overlapping contributions to string offsets table in section .%s.",
SectionName.data()));
}
// Report a gap in the table.
if (Offset < ContributionHeader) {
OS << format("0x%8.8" PRIx64 ": Gap, length = ", Offset);
OS << (ContributionHeader - Offset) << "\n";
}
OS << format("0x%8.8" PRIx64 ": ", ContributionHeader);
// In DWARF v5 the contribution size in the descriptor does not equal
// the originally encoded length (it does not contain the length of the
// version field and the padding, a total of 4 bytes). Add them back in
// for reporting.
OS << "Contribution size = " << (Contribution->Size + (Version < 5 ? 0 : 4))
<< ", Format = " << dwarf::FormatString(Format)
<< ", Version = " << Version << "\n";
Offset = Contribution->Base;
unsigned EntrySize = Contribution->getDwarfOffsetByteSize();
while (Offset - Contribution->Base < Contribution->Size) {
OS << format("0x%8.8" PRIx64 ": ", Offset);
uint64_t StringOffset =
StrOffsetExt.getRelocatedValue(EntrySize, &Offset);
OS << format("%0*" PRIx64 " ", OffsetDumpWidth, StringOffset);
const char *S = StrData.getCStr(&StringOffset);
if (S)
OS << format("\"%s\"", S);
OS << "\n";
}
}
// Report a gap at the end of the table.
if (Offset < SectionSize) {
OS << format("0x%8.8" PRIx64 ": Gap, length = ", Offset);
OS << (SectionSize - Offset) << "\n";
}
}
// Dump the .debug_addr section.
static void dumpAddrSection(raw_ostream &OS, DWARFDataExtractor &AddrData,
DIDumpOptions DumpOpts, uint16_t Version,
uint8_t AddrSize) {
uint64_t Offset = 0;
while (AddrData.isValidOffset(Offset)) {
DWARFDebugAddrTable AddrTable;
uint64_t TableOffset = Offset;
if (Error Err = AddrTable.extract(AddrData, &Offset, Version, AddrSize,
DumpOpts.WarningHandler)) {
DumpOpts.RecoverableErrorHandler(std::move(Err));
// Keep going after an error, if we can, assuming that the length field
// could be read. If it couldn't, stop reading the section.
if (auto TableLength = AddrTable.getFullLength()) {
Offset = TableOffset + *TableLength;
continue;
}
break;
}
AddrTable.dump(OS, DumpOpts);
}
}
// Dump the .debug_rnglists or .debug_rnglists.dwo section (DWARF v5).
static void dumpRnglistsSection(
raw_ostream &OS, DWARFDataExtractor &rnglistData,
llvm::function_ref<Optional<object::SectionedAddress>(uint32_t)>
LookupPooledAddress,
DIDumpOptions DumpOpts) {
uint64_t Offset = 0;
while (rnglistData.isValidOffset(Offset)) {
llvm::DWARFDebugRnglistTable Rnglists;
uint64_t TableOffset = Offset;
if (Error Err = Rnglists.extract(rnglistData, &Offset)) {
DumpOpts.RecoverableErrorHandler(std::move(Err));
uint64_t Length = Rnglists.length();
// Keep going after an error, if we can, assuming that the length field
// could be read. If it couldn't, stop reading the section.
if (Length == 0)
break;
Offset = TableOffset + Length;
} else {
Rnglists.dump(rnglistData, OS, LookupPooledAddress, DumpOpts);
}
}
}
std::unique_ptr<DWARFDebugMacro>
DWARFContext::parseMacroOrMacinfo(MacroSecType SectionType) {
auto Macro = std::make_unique<DWARFDebugMacro>();
auto ParseAndDump = [&](DWARFDataExtractor &Data, bool IsMacro) {
if (Error Err = IsMacro ? Macro->parseMacro(SectionType == MacroSection
? compile_units()
: dwo_compile_units(),
SectionType == MacroSection
? getStringExtractor()
: getStringDWOExtractor(),
Data)
: Macro->parseMacinfo(Data)) {
RecoverableErrorHandler(std::move(Err));
Macro = nullptr;
}
};
switch (SectionType) {
case MacinfoSection: {
DWARFDataExtractor Data(DObj->getMacinfoSection(), isLittleEndian(), 0);
ParseAndDump(Data, /*IsMacro=*/false);
break;
}
case MacinfoDwoSection: {
DWARFDataExtractor Data(DObj->getMacinfoDWOSection(), isLittleEndian(), 0);
ParseAndDump(Data, /*IsMacro=*/false);
break;
}
case MacroSection: {
DWARFDataExtractor Data(*DObj, DObj->getMacroSection(), isLittleEndian(),
0);
ParseAndDump(Data, /*IsMacro=*/true);
break;
}
case MacroDwoSection: {
DWARFDataExtractor Data(DObj->getMacroDWOSection(), isLittleEndian(), 0);
ParseAndDump(Data, /*IsMacro=*/true);
break;
}
}
return Macro;
}
static void dumpLoclistsSection(raw_ostream &OS, DIDumpOptions DumpOpts,
DWARFDataExtractor Data,
const MCRegisterInfo *MRI,
const DWARFObject &Obj,
Optional<uint64_t> DumpOffset) {
uint64_t Offset = 0;
while (Data.isValidOffset(Offset)) {
DWARFListTableHeader Header(".debug_loclists", "locations");
if (Error E = Header.extract(Data, &Offset)) {
DumpOpts.RecoverableErrorHandler(std::move(E));
return;
}
Header.dump(Data, OS, DumpOpts);
uint64_t EndOffset = Header.length() + Header.getHeaderOffset();
Data.setAddressSize(Header.getAddrSize());
DWARFDebugLoclists Loc(Data, Header.getVersion());
if (DumpOffset) {
if (DumpOffset >= Offset && DumpOffset < EndOffset) {
Offset = *DumpOffset;
Loc.dumpLocationList(&Offset, OS, /*BaseAddr=*/None, MRI, Obj, nullptr,
DumpOpts, /*Indent=*/0);
OS << "\n";
return;
}
} else {
Loc.dumpRange(Offset, EndOffset - Offset, OS, MRI, Obj, DumpOpts);
}
Offset = EndOffset;
}
}
static void dumpPubTableSection(raw_ostream &OS, DIDumpOptions DumpOpts,
DWARFDataExtractor Data, bool GnuStyle) {
DWARFDebugPubTable Table;
Table.extract(Data, GnuStyle, DumpOpts.RecoverableErrorHandler);
Table.dump(OS);
}
void DWARFContext::dump(
raw_ostream &OS, DIDumpOptions DumpOpts,
std::array<Optional<uint64_t>, DIDT_ID_Count> DumpOffsets) {
uint64_t DumpType = DumpOpts.DumpType;
StringRef Extension = sys::path::extension(DObj->getFileName());
bool IsDWO = (Extension == ".dwo") || (Extension == ".dwp");
// Print UUID header.
const auto *ObjFile = DObj->getFile();
if (DumpType & DIDT_UUID)
dumpUUID(OS, *ObjFile);
// Print a header for each explicitly-requested section.
// Otherwise just print one for non-empty sections.
// Only print empty .dwo section headers when dumping a .dwo file.
bool Explicit = DumpType != DIDT_All && !IsDWO;
bool ExplicitDWO = Explicit && IsDWO;
auto shouldDump = [&](bool Explicit, const char *Name, unsigned ID,
StringRef Section) -> Optional<uint64_t> * {
unsigned Mask = 1U << ID;
bool Should = (DumpType & Mask) && (Explicit || !Section.empty());
if (!Should)
return nullptr;
OS << "\n" << Name << " contents:\n";
return &DumpOffsets[ID];
};
// Dump individual sections.
if (shouldDump(Explicit, ".debug_abbrev", DIDT_ID_DebugAbbrev,
DObj->getAbbrevSection()))
getDebugAbbrev()->dump(OS);
if (shouldDump(ExplicitDWO, ".debug_abbrev.dwo", DIDT_ID_DebugAbbrev,
DObj->getAbbrevDWOSection()))
getDebugAbbrevDWO()->dump(OS);
auto dumpDebugInfo = [&](const char *Name, unit_iterator_range Units) {
OS << '\n' << Name << " contents:\n";
if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugInfo])
for (const auto &U : Units)
U->getDIEForOffset(DumpOffset.getValue())
.dump(OS, 0, DumpOpts.noImplicitRecursion());
else
for (const auto &U : Units)
U->dump(OS, DumpOpts);
};
if ((DumpType & DIDT_DebugInfo)) {
if (Explicit || getNumCompileUnits())
dumpDebugInfo(".debug_info", info_section_units());
if (ExplicitDWO || getNumDWOCompileUnits())
dumpDebugInfo(".debug_info.dwo", dwo_info_section_units());
}
auto dumpDebugType = [&](const char *Name, unit_iterator_range Units) {
OS << '\n' << Name << " contents:\n";
for (const auto &U : Units)
if (auto DumpOffset = DumpOffsets[DIDT_ID_DebugTypes])
U->getDIEForOffset(*DumpOffset)
.dump(OS, 0, DumpOpts.noImplicitRecursion());
else
U->dump(OS, DumpOpts);
};
if ((DumpType & DIDT_DebugTypes)) {
if (Explicit || getNumTypeUnits())
dumpDebugType(".debug_types", types_section_units());
if (ExplicitDWO || getNumDWOTypeUnits())
dumpDebugType(".debug_types.dwo", dwo_types_section_units());
}
DIDumpOptions LLDumpOpts = DumpOpts;
if (LLDumpOpts.Verbose)
LLDumpOpts.DisplayRawContents = true;
if (const auto *Off = shouldDump(Explicit, ".debug_loc", DIDT_ID_DebugLoc,
DObj->getLocSection().Data)) {
getDebugLoc()->dump(OS, getRegisterInfo(), *DObj, LLDumpOpts, *Off);
}
if (const auto *Off =
shouldDump(Explicit, ".debug_loclists", DIDT_ID_DebugLoclists,
DObj->getLoclistsSection().Data)) {
DWARFDataExtractor Data(*DObj, DObj->getLoclistsSection(), isLittleEndian(),
0);
dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off);
}
if (const auto *Off =
shouldDump(ExplicitDWO, ".debug_loclists.dwo", DIDT_ID_DebugLoclists,
DObj->getLoclistsDWOSection().Data)) {
DWARFDataExtractor Data(*DObj, DObj->getLoclistsDWOSection(),
isLittleEndian(), 0);
dumpLoclistsSection(OS, LLDumpOpts, Data, getRegisterInfo(), *DObj, *Off);
}
if (const auto *Off =
shouldDump(ExplicitDWO, ".debug_loc.dwo", DIDT_ID_DebugLoc,
DObj->getLocDWOSection().Data)) {
DWARFDataExtractor Data(*DObj, DObj->getLocDWOSection(), isLittleEndian(),
4);
DWARFDebugLoclists Loc(Data, /*Version=*/4);
if (*Off) {
uint64_t Offset = **Off;
Loc.dumpLocationList(&Offset, OS,
/*BaseAddr=*/None, getRegisterInfo(), *DObj, nullptr,
LLDumpOpts, /*Indent=*/0);
OS << "\n";
} else {
Loc.dumpRange(0, Data.getData().size(), OS, getRegisterInfo(), *DObj,
LLDumpOpts);
}
}
if (const Optional<uint64_t> *Off =
shouldDump(Explicit, ".debug_frame", DIDT_ID_DebugFrame,
DObj->getFrameSection().Data)) {
if (Expected<const DWARFDebugFrame *> DF = getDebugFrame())
(*DF)->dump(OS, DumpOpts, getRegisterInfo(), *Off);
else
RecoverableErrorHandler(DF.takeError());
}
if (const Optional<uint64_t> *Off =
shouldDump(Explicit, ".eh_frame", DIDT_ID_DebugFrame,
DObj->getEHFrameSection().Data)) {
if (Expected<const DWARFDebugFrame *> DF = getEHFrame())
(*DF)->dump(OS, DumpOpts, getRegisterInfo(), *Off);
else
RecoverableErrorHandler(DF.takeError());
}
if (shouldDump(Explicit, ".debug_macro", DIDT_ID_DebugMacro,
DObj->getMacroSection().Data)) {
if (auto Macro = getDebugMacro())
Macro->dump(OS);
}
if (shouldDump(Explicit, ".debug_macro.dwo", DIDT_ID_DebugMacro,
DObj->getMacroDWOSection())) {
if (auto MacroDWO = getDebugMacroDWO())
MacroDWO->dump(OS);
}
if (shouldDump(Explicit, ".debug_macinfo", DIDT_ID_DebugMacro,
DObj->getMacinfoSection())) {
if (auto Macinfo = getDebugMacinfo())
Macinfo->dump(OS);
}
if (shouldDump(Explicit, ".debug_macinfo.dwo", DIDT_ID_DebugMacro,
DObj->getMacinfoDWOSection())) {
if (auto MacinfoDWO = getDebugMacinfoDWO())
MacinfoDWO->dump(OS);
}
if (shouldDump(Explicit, ".debug_aranges", DIDT_ID_DebugAranges,
DObj->getArangesSection())) {
uint64_t offset = 0;
DWARFDataExtractor arangesData(DObj->getArangesSection(), isLittleEndian(),
0);
DWARFDebugArangeSet set;
while (arangesData.isValidOffset(offset)) {
if (Error E =
set.extract(arangesData, &offset, DumpOpts.WarningHandler)) {
RecoverableErrorHandler(std::move(E));
break;
}
set.dump(OS);
}
}
auto DumpLineSection = [&](DWARFDebugLine::SectionParser Parser,
DIDumpOptions DumpOpts,
Optional<uint64_t> DumpOffset) {
while (!Parser.done()) {
if (DumpOffset && Parser.getOffset() != *DumpOffset) {
Parser.skip(DumpOpts.WarningHandler, DumpOpts.WarningHandler);
continue;
}
OS << "debug_line[" << format("0x%8.8" PRIx64, Parser.getOffset())
<< "]\n";
Parser.parseNext(DumpOpts.WarningHandler, DumpOpts.WarningHandler, &OS,
DumpOpts.Verbose);
}
};
auto DumpStrSection = [&](StringRef Section) {
DataExtractor StrData(Section, isLittleEndian(), 0);
uint64_t Offset = 0;
uint64_t StrOffset = 0;
while (StrData.isValidOffset(Offset)) {
Error Err = Error::success();
const char *CStr = StrData.getCStr(&Offset, &Err);
if (Err) {
DumpOpts.WarningHandler(std::move(Err));
return;
}
OS << format("0x%8.8" PRIx64 ": \"", StrOffset);
OS.write_escaped(CStr);
OS << "\"\n";
StrOffset = Offset;
}
};
if (const auto *Off = shouldDump(Explicit, ".debug_line", DIDT_ID_DebugLine,
DObj->getLineSection().Data)) {
DWARFDataExtractor LineData(*DObj, DObj->getLineSection(), isLittleEndian(),
0);
DWARFDebugLine::SectionParser Parser(LineData, *this, normal_units());
DumpLineSection(Parser, DumpOpts, *Off);
}
if (const auto *Off =
shouldDump(ExplicitDWO, ".debug_line.dwo", DIDT_ID_DebugLine,
DObj->getLineDWOSection().Data)) {
DWARFDataExtractor LineData(*DObj, DObj->getLineDWOSection(),
isLittleEndian(), 0);
DWARFDebugLine::SectionParser Parser(LineData, *this, dwo_units());
DumpLineSection(Parser, DumpOpts, *Off);
}
if (shouldDump(Explicit, ".debug_cu_index", DIDT_ID_DebugCUIndex,
DObj->getCUIndexSection())) {
getCUIndex().dump(OS);
}
if (shouldDump(Explicit, ".debug_tu_index", DIDT_ID_DebugTUIndex,
DObj->getTUIndexSection())) {
getTUIndex().dump(OS);
}
if (shouldDump(Explicit, ".debug_str", DIDT_ID_DebugStr,
DObj->getStrSection()))
DumpStrSection(DObj->getStrSection());
if (shouldDump(ExplicitDWO, ".debug_str.dwo", DIDT_ID_DebugStr,
DObj->getStrDWOSection()))
DumpStrSection(DObj->getStrDWOSection());
if (shouldDump(Explicit, ".debug_line_str", DIDT_ID_DebugLineStr,
DObj->getLineStrSection()))
DumpStrSection(DObj->getLineStrSection());
if (shouldDump(Explicit, ".debug_addr", DIDT_ID_DebugAddr,
DObj->getAddrSection().Data)) {
DWARFDataExtractor AddrData(*DObj, DObj->getAddrSection(),
isLittleEndian(), 0);
dumpAddrSection(OS, AddrData, DumpOpts, getMaxVersion(), getCUAddrSize());
}
if (shouldDump(Explicit, ".debug_ranges", DIDT_ID_DebugRanges,
DObj->getRangesSection().Data)) {
uint8_t savedAddressByteSize = getCUAddrSize();
DWARFDataExtractor rangesData(*DObj, DObj->getRangesSection(),
isLittleEndian(), savedAddressByteSize);
uint64_t offset = 0;
DWARFDebugRangeList rangeList;
while (rangesData.isValidOffset(offset)) {
if (Error E = rangeList.extract(rangesData, &offset)) {
DumpOpts.RecoverableErrorHandler(std::move(E));
break;
}
rangeList.dump(OS);
}
}
auto LookupPooledAddress = [&](uint32_t Index) -> Optional<SectionedAddress> {
const auto &CUs = compile_units();
auto I = CUs.begin();
if (I == CUs.end())
return None;
return (*I)->getAddrOffsetSectionItem(Index);
};
if (shouldDump(Explicit, ".debug_rnglists", DIDT_ID_DebugRnglists,
DObj->getRnglistsSection().Data)) {
DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsSection(),
isLittleEndian(), 0);
dumpRnglistsSection(OS, RnglistData, LookupPooledAddress, DumpOpts);
}
if (shouldDump(ExplicitDWO, ".debug_rnglists.dwo", DIDT_ID_DebugRnglists,
DObj->getRnglistsDWOSection().Data)) {
DWARFDataExtractor RnglistData(*DObj, DObj->getRnglistsDWOSection(),
isLittleEndian(), 0);
dumpRnglistsSection(OS, RnglistData, LookupPooledAddress, DumpOpts);
}
if (shouldDump(Explicit, ".debug_pubnames", DIDT_ID_DebugPubnames,
DObj->getPubnamesSection().Data)) {
DWARFDataExtractor PubTableData(*DObj, DObj->getPubnamesSection(),
isLittleEndian(), 0);
dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/false);
}
if (shouldDump(Explicit, ".debug_pubtypes", DIDT_ID_DebugPubtypes,
DObj->getPubtypesSection().Data)) {
DWARFDataExtractor PubTableData(*DObj, DObj->getPubtypesSection(),
isLittleEndian(), 0);
dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/false);
}
if (shouldDump(Explicit, ".debug_gnu_pubnames", DIDT_ID_DebugGnuPubnames,
DObj->getGnuPubnamesSection().Data)) {
DWARFDataExtractor PubTableData(*DObj, DObj->getGnuPubnamesSection(),
isLittleEndian(), 0);
dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/true);
}
if (shouldDump(Explicit, ".debug_gnu_pubtypes", DIDT_ID_DebugGnuPubtypes,
DObj->getGnuPubtypesSection().Data)) {
DWARFDataExtractor PubTableData(*DObj, DObj->getGnuPubtypesSection(),
isLittleEndian(), 0);
dumpPubTableSection(OS, DumpOpts, PubTableData, /*GnuStyle=*/true);
}
if (shouldDump(Explicit, ".debug_str_offsets", DIDT_ID_DebugStrOffsets,
DObj->getStrOffsetsSection().Data))
dumpStringOffsetsSection(
OS, DumpOpts, "debug_str_offsets", *DObj, DObj->getStrOffsetsSection(),
DObj->getStrSection(), normal_units(), isLittleEndian());
if (shouldDump(ExplicitDWO, ".debug_str_offsets.dwo", DIDT_ID_DebugStrOffsets,
DObj->getStrOffsetsDWOSection().Data))
dumpStringOffsetsSection(OS, DumpOpts, "debug_str_offsets.dwo", *DObj,
DObj->getStrOffsetsDWOSection(),
DObj->getStrDWOSection(), dwo_units(),
isLittleEndian());
if (shouldDump(Explicit, ".gdb_index", DIDT_ID_GdbIndex,
DObj->getGdbIndexSection())) {
getGdbIndex().dump(OS);
}
if (shouldDump(Explicit, ".apple_names", DIDT_ID_AppleNames,
DObj->getAppleNamesSection().Data))
getAppleNames().dump(OS);
if (shouldDump(Explicit, ".apple_types", DIDT_ID_AppleTypes,
DObj->getAppleTypesSection().Data))
getAppleTypes().dump(OS);
if (shouldDump(Explicit, ".apple_namespaces", DIDT_ID_AppleNamespaces,
DObj->getAppleNamespacesSection().Data))
getAppleNamespaces().dump(OS);
if (shouldDump(Explicit, ".apple_objc", DIDT_ID_AppleObjC,
DObj->getAppleObjCSection().Data))
getAppleObjC().dump(OS);
if (shouldDump(Explicit, ".debug_names", DIDT_ID_DebugNames,
DObj->getNamesSection().Data))
getDebugNames().dump(OS);
}
DWARFTypeUnit *DWARFContext::getTypeUnitForHash(uint16_t Version, uint64_t Hash,
bool IsDWO) {
// FIXME: Check for/use the tu_index here, if there is one.
for (const auto &U : IsDWO ? dwo_units() : normal_units()) {
if (DWARFTypeUnit *TU = dyn_cast<DWARFTypeUnit>(U.get())) {
if (TU->getTypeHash() == Hash)
return TU;
}
}
return nullptr;
}
DWARFCompileUnit *DWARFContext::getDWOCompileUnitForHash(uint64_t Hash) {
parseDWOUnits(LazyParse);
if (const auto &CUI = getCUIndex()) {
if (const auto *R = CUI.getFromHash(Hash))
return dyn_cast_or_null<DWARFCompileUnit>(
DWOUnits.getUnitForIndexEntry(*R));
return nullptr;
}
// If there's no index, just search through the CUs in the DWO - there's
// probably only one unless this is something like LTO - though an in-process
// built/cached lookup table could be used in that case to improve repeated
// lookups of different CUs in the DWO.
for (const auto &DWOCU : dwo_compile_units()) {
// Might not have parsed DWO ID yet.
if (!DWOCU->getDWOId()) {
if (Optional<uint64_t> DWOId =
toUnsigned(DWOCU->getUnitDIE().find(DW_AT_GNU_dwo_id)))
DWOCU->setDWOId(*DWOId);
else
// No DWO ID?
continue;
}
if (DWOCU->getDWOId() == Hash)
return dyn_cast<DWARFCompileUnit>(DWOCU.get());
}
return nullptr;
}
DWARFDie DWARFContext::getDIEForOffset(uint64_t Offset) {
parseNormalUnits();
if (auto *CU = NormalUnits.getUnitForOffset(Offset))
return CU->getDIEForOffset(Offset);
return DWARFDie();
}
bool DWARFContext::verify(raw_ostream &OS, DIDumpOptions DumpOpts) {
bool Success = true;
DWARFVerifier verifier(OS, *this, DumpOpts);
Success &= verifier.handleDebugAbbrev();
if (DumpOpts.DumpType & DIDT_DebugInfo)
Success &= verifier.handleDebugInfo();
if (DumpOpts.DumpType & DIDT_DebugLine)
Success &= verifier.handleDebugLine();
Success &= verifier.handleAccelTables();
return Success;
}
const DWARFUnitIndex &DWARFContext::getCUIndex() {
if (CUIndex)
return *CUIndex;
DataExtractor CUIndexData(DObj->getCUIndexSection(), isLittleEndian(), 0);
CUIndex = std::make_unique<DWARFUnitIndex>(DW_SECT_INFO);
CUIndex->parse(CUIndexData);
return *CUIndex;
}
const DWARFUnitIndex &DWARFContext::getTUIndex() {
if (TUIndex)
return *TUIndex;
DataExtractor TUIndexData(DObj->getTUIndexSection(), isLittleEndian(), 0);
TUIndex = std::make_unique<DWARFUnitIndex>(DW_SECT_EXT_TYPES);
TUIndex->parse(TUIndexData);
return *TUIndex;
}
DWARFGdbIndex &DWARFContext::getGdbIndex() {
if (GdbIndex)
return *GdbIndex;
DataExtractor GdbIndexData(DObj->getGdbIndexSection(), true /*LE*/, 0);
GdbIndex = std::make_unique<DWARFGdbIndex>();
GdbIndex->parse(GdbIndexData);
return *GdbIndex;
}
const DWARFDebugAbbrev *DWARFContext::getDebugAbbrev() {
if (Abbrev)
return Abbrev.get();
DataExtractor abbrData(DObj->getAbbrevSection(), isLittleEndian(), 0);
Abbrev.reset(new DWARFDebugAbbrev());
Abbrev->extract(abbrData);
return Abbrev.get();
}
const DWARFDebugAbbrev *DWARFContext::getDebugAbbrevDWO() {
if (AbbrevDWO)
return AbbrevDWO.get();
DataExtractor abbrData(DObj->getAbbrevDWOSection(), isLittleEndian(), 0);
AbbrevDWO.reset(new DWARFDebugAbbrev());
AbbrevDWO->extract(abbrData);
return AbbrevDWO.get();
}
const DWARFDebugLoc *DWARFContext::getDebugLoc() {
if (Loc)
return Loc.get();
// Assume all units have the same address byte size.
auto LocData =
getNumCompileUnits()
? DWARFDataExtractor(*DObj, DObj->getLocSection(), isLittleEndian(),
getUnitAtIndex(0)->getAddressByteSize())
: DWARFDataExtractor("", isLittleEndian(), 0);
Loc.reset(new DWARFDebugLoc(std::move(LocData)));
return Loc.get();
}
const DWARFDebugAranges *DWARFContext::getDebugAranges() {
if (Aranges)
return Aranges.get();
Aranges.reset(new DWARFDebugAranges());
Aranges->generate(this);
return Aranges.get();
}
Expected<const DWARFDebugFrame *> DWARFContext::getDebugFrame() {
if (DebugFrame)
return DebugFrame.get();
const DWARFSection &DS = DObj->getFrameSection();
// There's a "bug" in the DWARFv3 standard with respect to the target address
// size within debug frame sections. While DWARF is supposed to be independent
// of its container, FDEs have fields with size being "target address size",
// which isn't specified in DWARF in general. It's only specified for CUs, but
// .eh_frame can appear without a .debug_info section. Follow the example of
// other tools (libdwarf) and extract this from the container (ObjectFile
// provides this information). This problem is fixed in DWARFv4
// See this dwarf-discuss discussion for more details:
// http://lists.dwarfstd.org/htdig.cgi/dwarf-discuss-dwarfstd.org/2011-December/001173.html
DWARFDataExtractor DebugFrameData(*DObj, DS, isLittleEndian(),
DObj->getAddressSize());
auto DF =
std::make_unique<DWARFDebugFrame>(getArch(), /*IsEH=*/false, DS.Address);
if (Error E = DF->parse(DebugFrameData))
return std::move(E);
DebugFrame.swap(DF);
return DebugFrame.get();
}
Expected<const DWARFDebugFrame *> DWARFContext::getEHFrame() {
if (EHFrame)
return EHFrame.get();
const DWARFSection &DS = DObj->getEHFrameSection();
DWARFDataExtractor DebugFrameData(*DObj, DS, isLittleEndian(),
DObj->getAddressSize());
auto DF =
std::make_unique<DWARFDebugFrame>(getArch(), /*IsEH=*/true, DS.Address);
if (Error E = DF->parse(DebugFrameData))
return std::move(E);
DebugFrame.swap(DF);
return DebugFrame.get();
}
const DWARFDebugMacro *DWARFContext::getDebugMacro() {
if (!Macro)
Macro = parseMacroOrMacinfo(MacroSection);
return Macro.get();
}
const DWARFDebugMacro *DWARFContext::getDebugMacroDWO() {
if (!MacroDWO)
MacroDWO = parseMacroOrMacinfo(MacroDwoSection);
return MacroDWO.get();
}
const DWARFDebugMacro *DWARFContext::getDebugMacinfo() {
if (!Macinfo)
Macinfo = parseMacroOrMacinfo(MacinfoSection);
return Macinfo.get();
}
const DWARFDebugMacro *DWARFContext::getDebugMacinfoDWO() {
if (!MacinfoDWO)
MacinfoDWO = parseMacroOrMacinfo(MacinfoDwoSection);
return MacinfoDWO.get();
}
template <typename T>
static T &getAccelTable(std::unique_ptr<T> &Cache, const DWARFObject &Obj,
const DWARFSection &Section, StringRef StringSection,
bool IsLittleEndian) {
if (Cache)
return *Cache;
DWARFDataExtractor AccelSection(Obj, Section, IsLittleEndian, 0);
DataExtractor StrData(StringSection, IsLittleEndian, 0);
Cache.reset(new T(AccelSection, StrData));
if (Error E = Cache->extract())
llvm::consumeError(std::move(E));
return *Cache;
}
const DWARFDebugNames &DWARFContext::getDebugNames() {
return getAccelTable(Names, *DObj, DObj->getNamesSection(),
DObj->getStrSection(), isLittleEndian());
}
const AppleAcceleratorTable &DWARFContext::getAppleNames() {
return getAccelTable(AppleNames, *DObj, DObj->getAppleNamesSection(),
DObj->getStrSection(), isLittleEndian());
}
const AppleAcceleratorTable &DWARFContext::getAppleTypes() {
return getAccelTable(AppleTypes, *DObj, DObj->getAppleTypesSection(),
DObj->getStrSection(), isLittleEndian());
}
const AppleAcceleratorTable &DWARFContext::getAppleNamespaces() {
return getAccelTable(AppleNamespaces, *DObj,
DObj->getAppleNamespacesSection(),
DObj->getStrSection(), isLittleEndian());
}
const AppleAcceleratorTable &DWARFContext::getAppleObjC() {
return getAccelTable(AppleObjC, *DObj, DObj->getAppleObjCSection(),
DObj->getStrSection(), isLittleEndian());
}
const DWARFDebugLine::LineTable *
DWARFContext::getLineTableForUnit(DWARFUnit *U) {
Expected<const DWARFDebugLine::LineTable *> ExpectedLineTable =
getLineTableForUnit(U, WarningHandler);
if (!ExpectedLineTable) {
WarningHandler(ExpectedLineTable.takeError());
return nullptr;
}
return *ExpectedLineTable;
}
Expected<const DWARFDebugLine::LineTable *> DWARFContext::getLineTableForUnit(
DWARFUnit *U, function_ref<void(Error)> RecoverableErrorHandler) {
if (!Line)
Line.reset(new DWARFDebugLine);
auto UnitDIE = U->getUnitDIE();
if (!UnitDIE)
return nullptr;
auto Offset = toSectionOffset(UnitDIE.find(DW_AT_stmt_list));
if (!Offset)
return nullptr; // No line table for this compile unit.
uint64_t stmtOffset = *Offset + U->getLineTableOffset();
// See if the line table is cached.
if (const DWARFLineTable *lt = Line->getLineTable(stmtOffset))
return lt;
// Make sure the offset is good before we try to parse.
if (stmtOffset >= U->getLineSection().Data.size())
return nullptr;
// We have to parse it first.
DWARFDataExtractor lineData(*DObj, U->getLineSection(), isLittleEndian(),
U->getAddressByteSize());
return Line->getOrParseLineTable(lineData, stmtOffset, *this, U,
RecoverableErrorHandler);
}
void DWARFContext::parseNormalUnits() {
if (!NormalUnits.empty())
return;
DObj->forEachInfoSections([&](const DWARFSection &S) {
NormalUnits.addUnitsForSection(*this, S, DW_SECT_INFO);
});
NormalUnits.finishedInfoUnits();
DObj->forEachTypesSections([&](const DWARFSection &S) {
NormalUnits.addUnitsForSection(*this, S, DW_SECT_EXT_TYPES);
});
}
void DWARFContext::parseDWOUnits(bool Lazy) {
if (!DWOUnits.empty())
return;
DObj->forEachInfoDWOSections([&](const DWARFSection &S) {
DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_INFO, Lazy);
});
DWOUnits.finishedInfoUnits();
DObj->forEachTypesDWOSections([&](const DWARFSection &S) {
DWOUnits.addUnitsForDWOSection(*this, S, DW_SECT_EXT_TYPES, Lazy);