blob: fe44071d9a27691ccdc393e7c6cba4603b23ef1f [file] [log] [blame]
Greg Claytonb8c162b2017-05-03 16:02:29 +00001//===- DWARFVerifier.cpp --------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
Pavel Labath79cd9422018-03-22 14:50:44 +000011#include "llvm/ADT/SmallSet.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000012#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
13#include "llvm/DebugInfo/DWARF/DWARFContext.h"
14#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
15#include "llvm/DebugInfo/DWARF/DWARFDie.h"
George Rimar144e4c52017-10-27 10:42:04 +000016#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000017#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
18#include "llvm/DebugInfo/DWARF/DWARFSection.h"
Pavel Labath906b7772018-03-16 10:02:16 +000019#include "llvm/Support/DJB.h"
George Rimar144e4c52017-10-27 10:42:04 +000020#include "llvm/Support/FormatVariadic.h"
Jonas Devlieghere69217532018-03-09 09:56:24 +000021#include "llvm/Support/WithColor.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000022#include "llvm/Support/raw_ostream.h"
23#include <map>
24#include <set>
25#include <vector>
26
27using namespace llvm;
28using namespace dwarf;
29using namespace object;
30
Jonas Devlieghere58910602017-09-14 11:33:42 +000031DWARFVerifier::DieRangeInfo::address_range_iterator
32DWARFVerifier::DieRangeInfo::insert(const DWARFAddressRange &R) {
33 auto Begin = Ranges.begin();
34 auto End = Ranges.end();
35 auto Pos = std::lower_bound(Begin, End, R);
36
37 if (Pos != End) {
38 if (Pos->intersects(R))
39 return Pos;
40 if (Pos != Begin) {
41 auto Iter = Pos - 1;
42 if (Iter->intersects(R))
43 return Iter;
44 }
45 }
46
47 Ranges.insert(Pos, R);
48 return Ranges.end();
49}
50
51DWARFVerifier::DieRangeInfo::die_range_info_iterator
52DWARFVerifier::DieRangeInfo::insert(const DieRangeInfo &RI) {
53 auto End = Children.end();
54 auto Iter = Children.begin();
55 while (Iter != End) {
56 if (Iter->intersects(RI))
57 return Iter;
58 ++Iter;
59 }
60 Children.insert(RI);
61 return Children.end();
62}
63
64bool DWARFVerifier::DieRangeInfo::contains(const DieRangeInfo &RHS) const {
65 // Both list of ranges are sorted so we can make this fast.
66
67 if (Ranges.empty() || RHS.Ranges.empty())
68 return false;
69
70 // Since the ranges are sorted we can advance where we start searching with
71 // this object's ranges as we traverse RHS.Ranges.
72 auto End = Ranges.end();
73 auto Iter = findRange(RHS.Ranges.front());
74
75 // Now linearly walk the ranges in this object and see if they contain each
76 // ranges from RHS.Ranges.
77 for (const auto &R : RHS.Ranges) {
78 while (Iter != End) {
79 if (Iter->contains(R))
80 break;
81 ++Iter;
82 }
83 if (Iter == End)
84 return false;
85 }
86 return true;
87}
88
89bool DWARFVerifier::DieRangeInfo::intersects(const DieRangeInfo &RHS) const {
90 if (Ranges.empty() || RHS.Ranges.empty())
91 return false;
92
93 auto End = Ranges.end();
94 auto Iter = findRange(RHS.Ranges.front());
95 for (const auto &R : RHS.Ranges) {
Jonas Devlieghered585a202017-09-14 17:46:23 +000096 if(Iter == End)
97 return false;
Jonas Devlieghere58910602017-09-14 11:33:42 +000098 if (R.HighPC <= Iter->LowPC)
99 continue;
100 while (Iter != End) {
101 if (Iter->intersects(R))
102 return true;
103 ++Iter;
104 }
105 }
106
107 return false;
108}
109
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000110bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000111 uint32_t *Offset, unsigned UnitIndex,
112 uint8_t &UnitType, bool &isUnitDWARF64) {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000113 uint32_t AbbrOffset, Length;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000114 uint8_t AddrSize = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000115 uint16_t Version;
116 bool Success = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000117
118 bool ValidLength = false;
119 bool ValidVersion = false;
120 bool ValidAddrSize = false;
121 bool ValidType = true;
122 bool ValidAbbrevOffset = true;
123
124 uint32_t OffsetStart = *Offset;
125 Length = DebugInfoData.getU32(Offset);
126 if (Length == UINT32_MAX) {
127 isUnitDWARF64 = true;
128 OS << format(
129 "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
130 UnitIndex);
131 return false;
132 }
133 Version = DebugInfoData.getU16(Offset);
134
135 if (Version >= 5) {
136 UnitType = DebugInfoData.getU8(Offset);
137 AddrSize = DebugInfoData.getU8(Offset);
138 AbbrOffset = DebugInfoData.getU32(Offset);
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000139 ValidType = dwarf::isUnitType(UnitType);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000140 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000141 UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000142 AbbrOffset = DebugInfoData.getU32(Offset);
143 AddrSize = DebugInfoData.getU8(Offset);
144 }
145
146 if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
147 ValidAbbrevOffset = false;
148
149 ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
150 ValidVersion = DWARFContext::isSupportedVersion(Version);
151 ValidAddrSize = AddrSize == 4 || AddrSize == 8;
152 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
153 !ValidType) {
154 Success = false;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000155 error() << format("Units[%d] - start offset: 0x%08x \n", UnitIndex,
156 OffsetStart);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000157 if (!ValidLength)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000158 note() << "The length for this unit is too "
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000159 "large for the .debug_info provided.\n";
160 if (!ValidVersion)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000161 note() << "The 16 bit unit header version is not valid.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000162 if (!ValidType)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000163 note() << "The unit type encoding is not valid.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000164 if (!ValidAbbrevOffset)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000165 note() << "The offset into the .debug_abbrev section is "
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000166 "not valid.\n";
167 if (!ValidAddrSize)
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000168 note() << "The address size is unsupported.\n";
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000169 }
170 *Offset = OffsetStart + Length + 4;
171 return Success;
172}
173
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000174bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit, uint8_t UnitType) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000175 uint32_t NumUnitErrors = 0;
176 unsigned NumDies = Unit.getNumDIEs();
177 for (unsigned I = 0; I < NumDies; ++I) {
178 auto Die = Unit.getDIEAtIndex(I);
179 if (Die.getTag() == DW_TAG_null)
180 continue;
181 for (auto AttrValue : Die.attributes()) {
182 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
183 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
184 }
185 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000186
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000187 DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false);
188 if (!Die) {
189 error() << "Compilation unit without DIE.\n";
190 NumUnitErrors++;
191 return NumUnitErrors == 0;
192 }
193
194 if (!dwarf::isUnitType(Die.getTag())) {
195 error() << "Compilation unit root DIE is not a unit DIE: "
196 << dwarf::TagString(Die.getTag()) << ".\n";
Jonas Devlieghere35fdaa92017-09-28 15:57:50 +0000197 NumUnitErrors++;
198 }
199
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000200 if (UnitType != 0 &&
201 !DWARFUnit::isMatchingUnitTypeAndTag(UnitType, Die.getTag())) {
202 error() << "Compilation unit type (" << dwarf::UnitTypeString(UnitType)
203 << ") and root DIE (" << dwarf::TagString(Die.getTag())
204 << ") do not match.\n";
205 NumUnitErrors++;
206 }
207
208 DieRangeInfo RI;
209 NumUnitErrors += verifyDieRanges(Die, RI);
210
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000211 return NumUnitErrors == 0;
212}
213
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000214unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000215 unsigned NumErrors = 0;
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000216 if (Abbrev) {
217 const DWARFAbbreviationDeclarationSet *AbbrDecls =
218 Abbrev->getAbbreviationDeclarationSet(0);
219 for (auto AbbrDecl : *AbbrDecls) {
220 SmallDenseSet<uint16_t> AttributeSet;
221 for (auto Attribute : AbbrDecl.attributes()) {
222 auto Result = AttributeSet.insert(Attribute.Attr);
223 if (!Result.second) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000224 error() << "Abbreviation declaration contains multiple "
225 << AttributeString(Attribute.Attr) << " attributes.\n";
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000226 AbbrDecl.dump(OS);
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000227 ++NumErrors;
228 }
229 }
230 }
231 }
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000232 return NumErrors;
233}
234
235bool DWARFVerifier::handleDebugAbbrev() {
236 OS << "Verifying .debug_abbrev...\n";
237
238 const DWARFObject &DObj = DCtx.getDWARFObj();
239 bool noDebugAbbrev = DObj.getAbbrevSection().empty();
240 bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
241
242 if (noDebugAbbrev && noDebugAbbrevDWO) {
243 return true;
244 }
245
246 unsigned NumErrors = 0;
247 if (!noDebugAbbrev)
248 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
249
250 if (!noDebugAbbrevDWO)
251 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000252 return NumErrors == 0;
253}
254
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000255bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000256 OS << "Verifying .debug_info Unit Header Chain...\n";
257
Rafael Espindolac398e672017-07-19 22:27:28 +0000258 const DWARFObject &DObj = DCtx.getDWARFObj();
259 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
260 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000261 uint32_t NumDebugInfoErrors = 0;
262 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
263 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000264 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000265 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000266 bool hasDIE = DebugInfoData.isValidOffset(Offset);
Jonas Devlieghereaa6be822017-10-10 14:15:25 +0000267 DWARFUnitSection<DWARFTypeUnit> TUSection{};
268 DWARFUnitSection<DWARFCompileUnit> CUSection{};
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000269 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000270 OffsetStart = Offset;
271 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
272 isUnitDWARF64)) {
273 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000274 if (isUnitDWARF64)
275 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000276 } else {
Paul Robinson5f53f072018-05-14 20:32:31 +0000277 DWARFUnitHeader Header;
278 Header.extract(DCtx, DebugInfoData, &OffsetStart);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000279 std::unique_ptr<DWARFUnit> Unit;
280 switch (UnitType) {
281 case dwarf::DW_UT_type:
282 case dwarf::DW_UT_split_type: {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000283 Unit.reset(new DWARFTypeUnit(
Paul Robinson5f53f072018-05-14 20:32:31 +0000284 DCtx, DObj.getInfoSection(), Header, DCtx.getDebugAbbrev(),
Rafael Espindolac398e672017-07-19 22:27:28 +0000285 &DObj.getRangeSection(), DObj.getStringSection(),
286 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
Paul Robinson5f53f072018-05-14 20:32:31 +0000287 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection));
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000288 break;
289 }
290 case dwarf::DW_UT_skeleton:
291 case dwarf::DW_UT_split_compile:
292 case dwarf::DW_UT_compile:
293 case dwarf::DW_UT_partial:
294 // UnitType = 0 means that we are
295 // verifying a compile unit in DWARF v4.
296 case 0: {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000297 Unit.reset(new DWARFCompileUnit(
Paul Robinson5f53f072018-05-14 20:32:31 +0000298 DCtx, DObj.getInfoSection(), Header, DCtx.getDebugAbbrev(),
Rafael Espindolac398e672017-07-19 22:27:28 +0000299 &DObj.getRangeSection(), DObj.getStringSection(),
300 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
Paul Robinson5f53f072018-05-14 20:32:31 +0000301 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection));
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000302 break;
303 }
304 default: { llvm_unreachable("Invalid UnitType."); }
305 }
Jonas Devliegheref2fa9eb2017-10-06 22:27:31 +0000306 if (!verifyUnitContents(*Unit, UnitType))
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000307 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000308 }
309 hasDIE = DebugInfoData.isValidOffset(Offset);
310 ++UnitIdx;
311 }
312 if (UnitIdx == 0 && !hasDIE) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000313 warn() << ".debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000314 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000315 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000316 NumDebugInfoErrors += verifyDebugInfoReferences();
317 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000318}
319
Jonas Devlieghere58910602017-09-14 11:33:42 +0000320unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
321 DieRangeInfo &ParentRI) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000322 unsigned NumErrors = 0;
Jonas Devlieghere58910602017-09-14 11:33:42 +0000323
324 if (!Die.isValid())
325 return NumErrors;
326
327 DWARFAddressRangesVector Ranges = Die.getAddressRanges();
328
329 // Build RI for this DIE and check that ranges within this DIE do not
330 // overlap.
331 DieRangeInfo RI(Die);
332 for (auto Range : Ranges) {
333 if (!Range.valid()) {
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000334 ++NumErrors;
Jonas Devliegherea15f25d32017-09-29 15:41:22 +0000335 error() << "Invalid address range " << Range << "\n";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000336 continue;
337 }
338
339 // Verify that ranges don't intersect.
340 const auto IntersectingRange = RI.insert(Range);
341 if (IntersectingRange != RI.Ranges.end()) {
342 ++NumErrors;
Jonas Devliegherea15f25d32017-09-29 15:41:22 +0000343 error() << "DIE has overlapping address ranges: " << Range << " and "
344 << *IntersectingRange << "\n";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000345 break;
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000346 }
347 }
Jonas Devlieghere58910602017-09-14 11:33:42 +0000348
349 // Verify that children don't intersect.
350 const auto IntersectingChild = ParentRI.insert(RI);
351 if (IntersectingChild != ParentRI.Children.end()) {
352 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000353 error() << "DIEs have overlapping address ranges:";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000354 Die.dump(OS, 0);
355 IntersectingChild->Die.dump(OS, 0);
356 OS << "\n";
357 }
358
359 // Verify that ranges are contained within their parent.
360 bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() &&
361 !(Die.getTag() == DW_TAG_subprogram &&
362 ParentRI.Die.getTag() == DW_TAG_subprogram);
363 if (ShouldBeContained && !ParentRI.contains(RI)) {
364 ++NumErrors;
Jonas Devlieghere63eca152018-05-22 17:38:03 +0000365 error() << "DIE address ranges are not contained in its parent's ranges:";
Jonas Devlieghere58910602017-09-14 11:33:42 +0000366 ParentRI.Die.dump(OS, 0);
Jonas Devlieghere63eca152018-05-22 17:38:03 +0000367 Die.dump(OS, 2);
Jonas Devlieghere58910602017-09-14 11:33:42 +0000368 OS << "\n";
369 }
370
371 // Recursively check children.
372 for (DWARFDie Child : Die)
373 NumErrors += verifyDieRanges(Child, RI);
374
Spyridoula Gravanie0ba4152017-07-24 21:04:11 +0000375 return NumErrors;
376}
377
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000378unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
379 DWARFAttribute &AttrValue) {
380 unsigned NumErrors = 0;
George Rimar144e4c52017-10-27 10:42:04 +0000381 auto ReportError = [&](const Twine &TitleMsg) {
382 ++NumErrors;
383 error() << TitleMsg << '\n';
384 Die.dump(OS, 0, DumpOpts);
385 OS << "\n";
386 };
387
388 const DWARFObject &DObj = DCtx.getDWARFObj();
Greg Claytonc5b2d562017-05-03 18:25:46 +0000389 const auto Attr = AttrValue.Attr;
390 switch (Attr) {
391 case DW_AT_ranges:
392 // Make sure the offset in the DW_AT_ranges attribute is valid.
393 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
George Rimar144e4c52017-10-27 10:42:04 +0000394 if (*SectionOffset >= DObj.getRangeSection().Data.size())
395 ReportError("DW_AT_ranges offset is beyond .debug_ranges bounds:");
396 break;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000397 }
George Rimar144e4c52017-10-27 10:42:04 +0000398 ReportError("DIE has invalid DW_AT_ranges encoding:");
Greg Claytonc5b2d562017-05-03 18:25:46 +0000399 break;
400 case DW_AT_stmt_list:
401 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
402 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
George Rimar144e4c52017-10-27 10:42:04 +0000403 if (*SectionOffset >= DObj.getLineSection().Data.size())
404 ReportError("DW_AT_stmt_list offset is beyond .debug_line bounds: " +
George Rimar3d07f602017-10-27 10:58:04 +0000405 llvm::formatv("{0:x8}", *SectionOffset));
George Rimar144e4c52017-10-27 10:42:04 +0000406 break;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000407 }
George Rimar144e4c52017-10-27 10:42:04 +0000408 ReportError("DIE has invalid DW_AT_stmt_list encoding:");
Greg Claytonc5b2d562017-05-03 18:25:46 +0000409 break;
George Rimar144e4c52017-10-27 10:42:04 +0000410 case DW_AT_location: {
Jonas Devlieghere7e0b0232018-05-22 17:37:27 +0000411 auto VerifyLocationExpr = [&](StringRef D) {
Jonas Devlieghere7d4a9742018-02-17 13:06:37 +0000412 DWARFUnit *U = Die.getDwarfUnit();
413 DataExtractor Data(D, DCtx.isLittleEndian(), 0);
414 DWARFExpression Expression(Data, U->getVersion(),
415 U->getAddressByteSize());
416 bool Error = llvm::any_of(Expression, [](DWARFExpression::Operation &Op) {
417 return Op.isError();
418 });
419 if (Error)
420 ReportError("DIE contains invalid DWARF expression:");
421 };
422 if (Optional<ArrayRef<uint8_t>> Expr = AttrValue.Value.getAsBlock()) {
423 // Verify inlined location.
Jonas Devlieghere7e0b0232018-05-22 17:37:27 +0000424 VerifyLocationExpr(llvm::toStringRef(*Expr));
425 } else if (auto LocOffset = AttrValue.Value.getAsSectionOffset()) {
Jonas Devlieghere7d4a9742018-02-17 13:06:37 +0000426 // Verify location list.
427 if (auto DebugLoc = DCtx.getDebugLoc())
428 if (auto LocList = DebugLoc->getLocationListAtOffset(*LocOffset))
429 for (const auto &Entry : LocList->Entries)
Jonas Devlieghere7e0b0232018-05-22 17:37:27 +0000430 VerifyLocationExpr({Entry.Loc.data(), Entry.Loc.size()});
George Rimar144e4c52017-10-27 10:42:04 +0000431 }
George Rimar144e4c52017-10-27 10:42:04 +0000432 break;
433 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000434
Greg Claytonc5b2d562017-05-03 18:25:46 +0000435 default:
436 break;
437 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000438 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000439}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000440
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000441unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
442 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000443 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000444 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000445 const auto Form = AttrValue.Value.getForm();
446 switch (Form) {
447 case DW_FORM_ref1:
448 case DW_FORM_ref2:
449 case DW_FORM_ref4:
450 case DW_FORM_ref8:
451 case DW_FORM_ref_udata: {
452 // Verify all CU relative references are valid CU offsets.
453 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
454 assert(RefVal);
455 if (RefVal) {
456 auto DieCU = Die.getDwarfUnit();
457 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
458 auto CUOffset = AttrValue.Value.getRawUValue();
459 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000460 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000461 error() << FormEncodingString(Form) << " CU offset "
462 << format("0x%08" PRIx64, CUOffset)
463 << " is invalid (must be less than CU size of "
464 << format("0x%08" PRIx32, CUSize) << "):\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000465 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000466 OS << "\n";
467 } else {
468 // Valid reference, but we will verify it points to an actual
469 // DIE later.
470 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000471 }
472 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000473 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000474 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000475 case DW_FORM_ref_addr: {
476 // Verify all absolute DIE references have valid offsets in the
477 // .debug_info section.
478 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
479 assert(RefVal);
480 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000481 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000482 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000483 error() << "DW_FORM_ref_addr offset beyond .debug_info "
484 "bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000485 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000486 OS << "\n";
487 } else {
488 // Valid reference, but we will verify it points to an actual
489 // DIE later.
490 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
491 }
492 }
493 break;
494 }
495 case DW_FORM_strp: {
496 auto SecOffset = AttrValue.Value.getAsSectionOffset();
497 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000498 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000499 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000500 error() << "DW_FORM_strp offset beyond .debug_str bounds:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000501 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000502 OS << "\n";
503 }
504 break;
505 }
506 default:
507 break;
508 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000509 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000510}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000511
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000512unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000513 // Take all references and make sure they point to an actual DIE by
514 // getting the DIE by offset and emitting an error
515 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000516 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000517 for (auto Pair : ReferenceToDIEOffsets) {
518 auto Die = DCtx.getDIEForOffset(Pair.first);
519 if (Die)
520 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000521 ++NumErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000522 error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
523 << ". Offset is in between DIEs:\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000524 for (auto Offset : Pair.second) {
525 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
Adrian Prantld3f9f212017-09-20 17:44:00 +0000526 ReferencingDie.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000527 OS << "\n";
528 }
529 OS << "\n";
530 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000531 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000532}
533
Greg Claytonc5b2d562017-05-03 18:25:46 +0000534void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000535 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000536 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000537 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000538 // Get the attribute value as a section offset. No need to produce an
539 // error here if the encoding isn't correct because we validate this in
540 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000541 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000542 if (!StmtSectionOffset)
543 continue;
544 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000545 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000546 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000547 if (!LineTable) {
548 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000549 error() << ".debug_line[" << format("0x%08" PRIx32, LineTableOffset)
550 << "] was not able to be parsed for CU:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000551 Die.dump(OS, 0, DumpOpts);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000552 OS << '\n';
553 continue;
554 }
555 } else {
556 // Make sure we don't get a valid line table back if the offset is wrong.
557 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000558 // Skip this line table as it isn't valid. No need to create an error
559 // here because we validate this in the .debug_info verifier.
560 continue;
561 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000562 auto Iter = StmtListToDie.find(LineTableOffset);
563 if (Iter != StmtListToDie.end()) {
564 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000565 error() << "two compile unit DIEs, "
566 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
567 << format("0x%08" PRIx32, Die.getOffset())
568 << ", have the same DW_AT_stmt_list section offset:\n";
Adrian Prantld3f9f212017-09-20 17:44:00 +0000569 Iter->second.dump(OS, 0, DumpOpts);
570 Die.dump(OS, 0, DumpOpts);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000571 OS << '\n';
572 // Already verified this line table before, no need to do it again.
573 continue;
574 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000575 StmtListToDie[LineTableOffset] = Die;
576 }
577}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000578
Greg Claytonc5b2d562017-05-03 18:25:46 +0000579void DWARFVerifier::verifyDebugLineRows() {
580 for (const auto &CU : DCtx.compile_units()) {
581 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000582 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000583 // If there is no line table we will have created an error in the
584 // .debug_info verifier or in verifyDebugLineStmtOffsets().
585 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000586 continue;
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000587
588 // Verify prologue.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000589 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000590 uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
591 uint32_t FileIndex = 1;
592 StringMap<uint16_t> FullPathMap;
593 for (const auto &FileName : LineTable->Prologue.FileNames) {
594 // Verify directory index.
595 if (FileName.DirIdx > MaxDirIndex) {
596 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000597 error() << ".debug_line["
598 << format("0x%08" PRIx64,
599 *toSectionOffset(Die.find(DW_AT_stmt_list)))
600 << "].prologue.file_names[" << FileIndex
601 << "].dir_idx contains an invalid index: " << FileName.DirIdx
602 << "\n";
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000603 }
604
605 // Check file paths for duplicates.
606 std::string FullPath;
607 const bool HasFullPath = LineTable->getFileNameByIndex(
608 FileIndex, CU->getCompilationDir(),
609 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
610 assert(HasFullPath && "Invalid index?");
611 (void)HasFullPath;
612 auto It = FullPathMap.find(FullPath);
613 if (It == FullPathMap.end())
614 FullPathMap[FullPath] = FileIndex;
615 else if (It->second != FileIndex) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000616 warn() << ".debug_line["
617 << format("0x%08" PRIx64,
618 *toSectionOffset(Die.find(DW_AT_stmt_list)))
619 << "].prologue.file_names[" << FileIndex
620 << "] is a duplicate of file_names[" << It->second << "]\n";
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000621 }
622
623 FileIndex++;
624 }
625
626 // Verify rows.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000627 uint64_t PrevAddress = 0;
628 uint32_t RowIndex = 0;
629 for (const auto &Row : LineTable->Rows) {
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000630 // Verify row address.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000631 if (Row.Address < PrevAddress) {
632 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000633 error() << ".debug_line["
634 << format("0x%08" PRIx64,
635 *toSectionOffset(Die.find(DW_AT_stmt_list)))
636 << "] row[" << RowIndex
637 << "] decreases in address from previous row:\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000638
639 DWARFDebugLine::Row::dumpTableHeader(OS);
640 if (RowIndex > 0)
641 LineTable->Rows[RowIndex - 1].dump(OS);
642 Row.dump(OS);
643 OS << '\n';
644 }
645
Jonas Devliegheref4ed65d2017-09-08 09:48:51 +0000646 // Verify file index.
Greg Claytonb8c162b2017-05-03 16:02:29 +0000647 if (Row.File > MaxFileIndex) {
648 ++NumDebugLineErrors;
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000649 error() << ".debug_line["
650 << format("0x%08" PRIx64,
651 *toSectionOffset(Die.find(DW_AT_stmt_list)))
652 << "][" << RowIndex << "] has invalid file index " << Row.File
653 << " (valid values are [1," << MaxFileIndex << "]):\n";
Greg Claytonb8c162b2017-05-03 16:02:29 +0000654 DWARFDebugLine::Row::dumpTableHeader(OS);
655 Row.dump(OS);
656 OS << '\n';
657 }
658 if (Row.EndSequence)
659 PrevAddress = 0;
660 else
661 PrevAddress = Row.Address;
662 ++RowIndex;
663 }
664 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000665}
666
667bool DWARFVerifier::handleDebugLine() {
668 NumDebugLineErrors = 0;
669 OS << "Verifying .debug_line...\n";
670 verifyDebugLineStmtOffsets();
671 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000672 return NumDebugLineErrors == 0;
673}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000674
Pavel Labath9b36fd22018-01-22 13:17:23 +0000675unsigned DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
676 DataExtractor *StrData,
677 const char *SectionName) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000678 unsigned NumErrors = 0;
679 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
680 DCtx.isLittleEndian(), 0);
Pavel Labath9b36fd22018-01-22 13:17:23 +0000681 AppleAcceleratorTable AccelTable(AccelSectionData, *StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000682
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000683 OS << "Verifying " << SectionName << "...\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000684
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000685 // Verify that the fixed part of the header is not too short.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000686 if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000687 error() << "Section is too small to fit a section header.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000688 return 1;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000689 }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000690
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000691 // Verify that the section is not too short.
Jonas Devlieghereba915892017-12-11 18:22:47 +0000692 if (Error E = AccelTable.extract()) {
693 error() << toString(std::move(E)) << '\n';
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000694 return 1;
695 }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000696
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000697 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000698 uint32_t NumBuckets = AccelTable.getNumBuckets();
699 uint32_t NumHashes = AccelTable.getNumHashes();
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000700
701 uint32_t BucketsOffset =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000702 AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000703 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
704 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000705 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000706 uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000707 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000708 error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
709 HashIdx);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000710 ++NumErrors;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000711 }
712 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000713 uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000714 if (NumAtoms == 0) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000715 error() << "No atoms: failed to read HashData.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000716 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000717 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000718 if (!AccelTable.validateForms()) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000719 error() << "Unsupported form: failed to read HashData.\n";
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000720 return 1;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000721 }
722
723 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
724 uint32_t HashOffset = HashesBase + 4 * HashIdx;
725 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000726 uint32_t Hash = AccelSectionData.getU32(&HashOffset);
727 uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
728 if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
729 sizeof(uint64_t))) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000730 error() << format("Hash[%d] has invalid HashData offset: 0x%08x.\n",
731 HashIdx, HashDataOffset);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000732 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000733 }
734
735 uint32_t StrpOffset;
736 uint32_t StringOffset;
737 uint32_t StringCount = 0;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000738 unsigned Offset;
739 unsigned Tag;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000740 while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000741 const uint32_t NumHashDataObjects =
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000742 AccelSectionData.getU32(&HashDataOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000743 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
744 ++HashDataIdx) {
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000745 std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
746 auto Die = DCtx.getDIEForOffset(Offset);
747 if (!Die) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000748 const uint32_t BucketIdx =
749 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
750 StringOffset = StrpOffset;
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000751 const char *Name = StrData->getCStr(&StringOffset);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000752 if (!Name)
753 Name = "<NULL>";
754
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000755 error() << format(
756 "%s Bucket[%d] Hash[%d] = 0x%08x "
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000757 "Str[%u] = 0x%08x "
758 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000759 SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000760 HashDataIdx, Offset, Name);
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000761
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000762 ++NumErrors;
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000763 continue;
764 }
765 if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +0000766 error() << "Tag " << dwarf::TagString(Tag)
767 << " in accelerator table does not match Tag "
768 << dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
769 << "].\n";
Spyridoula Gravani70d35e12017-07-31 18:01:16 +0000770 ++NumErrors;
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000771 }
772 }
773 ++StringCount;
774 }
775 }
Spyridoula Gravanidc635f42017-07-26 00:52:31 +0000776 return NumErrors;
777}
778
Pavel Labathb136c392018-03-08 15:34:42 +0000779unsigned
780DWARFVerifier::verifyDebugNamesCULists(const DWARFDebugNames &AccelTable) {
781 // A map from CU offset to the (first) Name Index offset which claims to index
782 // this CU.
783 DenseMap<uint32_t, uint32_t> CUMap;
784 const uint32_t NotIndexed = std::numeric_limits<uint32_t>::max();
785
786 CUMap.reserve(DCtx.getNumCompileUnits());
787 for (const auto &CU : DCtx.compile_units())
788 CUMap[CU->getOffset()] = NotIndexed;
789
790 unsigned NumErrors = 0;
791 for (const DWARFDebugNames::NameIndex &NI : AccelTable) {
792 if (NI.getCUCount() == 0) {
793 error() << formatv("Name Index @ {0:x} does not index any CU\n",
794 NI.getUnitOffset());
795 ++NumErrors;
796 continue;
797 }
798 for (uint32_t CU = 0, End = NI.getCUCount(); CU < End; ++CU) {
799 uint32_t Offset = NI.getCUOffset(CU);
800 auto Iter = CUMap.find(Offset);
801
802 if (Iter == CUMap.end()) {
803 error() << formatv(
804 "Name Index @ {0:x} references a non-existing CU @ {1:x}\n",
805 NI.getUnitOffset(), Offset);
806 ++NumErrors;
807 continue;
808 }
809
810 if (Iter->second != NotIndexed) {
811 error() << formatv("Name Index @ {0:x} references a CU @ {1:x}, but "
812 "this CU is already indexed by Name Index @ {2:x}\n",
813 NI.getUnitOffset(), Offset, Iter->second);
814 continue;
815 }
816 Iter->second = NI.getUnitOffset();
817 }
818 }
819
820 for (const auto &KV : CUMap) {
821 if (KV.second == NotIndexed)
822 warn() << formatv("CU @ {0:x} not covered by any Name Index\n", KV.first);
823 }
824
825 return NumErrors;
826}
827
Pavel Labath906b7772018-03-16 10:02:16 +0000828unsigned
829DWARFVerifier::verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
830 const DataExtractor &StrData) {
831 struct BucketInfo {
832 uint32_t Bucket;
833 uint32_t Index;
834
835 constexpr BucketInfo(uint32_t Bucket, uint32_t Index)
836 : Bucket(Bucket), Index(Index) {}
837 bool operator<(const BucketInfo &RHS) const { return Index < RHS.Index; };
838 };
839
840 uint32_t NumErrors = 0;
841 if (NI.getBucketCount() == 0) {
842 warn() << formatv("Name Index @ {0:x} does not contain a hash table.\n",
843 NI.getUnitOffset());
844 return NumErrors;
845 }
846
847 // Build up a list of (Bucket, Index) pairs. We use this later to verify that
848 // each Name is reachable from the appropriate bucket.
849 std::vector<BucketInfo> BucketStarts;
850 BucketStarts.reserve(NI.getBucketCount() + 1);
851 for (uint32_t Bucket = 0, End = NI.getBucketCount(); Bucket < End; ++Bucket) {
852 uint32_t Index = NI.getBucketArrayEntry(Bucket);
853 if (Index > NI.getNameCount()) {
854 error() << formatv("Bucket {0} of Name Index @ {1:x} contains invalid "
855 "value {2}. Valid range is [0, {3}].\n",
856 Bucket, NI.getUnitOffset(), Index, NI.getNameCount());
857 ++NumErrors;
858 continue;
859 }
860 if (Index > 0)
861 BucketStarts.emplace_back(Bucket, Index);
862 }
863
864 // If there were any buckets with invalid values, skip further checks as they
865 // will likely produce many errors which will only confuse the actual root
866 // problem.
867 if (NumErrors > 0)
868 return NumErrors;
869
870 // Sort the list in the order of increasing "Index" entries.
871 array_pod_sort(BucketStarts.begin(), BucketStarts.end());
872
873 // Insert a sentinel entry at the end, so we can check that the end of the
874 // table is covered in the loop below.
875 BucketStarts.emplace_back(NI.getBucketCount(), NI.getNameCount() + 1);
876
877 // Loop invariant: NextUncovered is the (1-based) index of the first Name
878 // which is not reachable by any of the buckets we processed so far (and
879 // hasn't been reported as uncovered).
880 uint32_t NextUncovered = 1;
881 for (const BucketInfo &B : BucketStarts) {
882 // Under normal circumstances B.Index be equal to NextUncovered, but it can
883 // be less if a bucket points to names which are already known to be in some
884 // bucket we processed earlier. In that case, we won't trigger this error,
885 // but report the mismatched hash value error instead. (We know the hash
886 // will not match because we have already verified that the name's hash
887 // puts it into the previous bucket.)
888 if (B.Index > NextUncovered) {
889 error() << formatv("Name Index @ {0:x}: Name table entries [{1}, {2}] "
890 "are not covered by the hash table.\n",
891 NI.getUnitOffset(), NextUncovered, B.Index - 1);
892 ++NumErrors;
893 }
894 uint32_t Idx = B.Index;
895
896 // The rest of the checks apply only to non-sentinel entries.
897 if (B.Bucket == NI.getBucketCount())
898 break;
899
900 // This triggers if a non-empty bucket points to a name with a mismatched
901 // hash. Clients are likely to interpret this as an empty bucket, because a
902 // mismatched hash signals the end of a bucket, but if this is indeed an
903 // empty bucket, the producer should have signalled this by marking the
904 // bucket as empty.
905 uint32_t FirstHash = NI.getHashArrayEntry(Idx);
906 if (FirstHash % NI.getBucketCount() != B.Bucket) {
907 error() << formatv(
908 "Name Index @ {0:x}: Bucket {1} is not empty but points to a "
909 "mismatched hash value {2:x} (belonging to bucket {3}).\n",
910 NI.getUnitOffset(), B.Bucket, FirstHash,
911 FirstHash % NI.getBucketCount());
912 ++NumErrors;
913 }
914
915 // This find the end of this bucket and also verifies that all the hashes in
916 // this bucket are correct by comparing the stored hashes to the ones we
917 // compute ourselves.
918 while (Idx <= NI.getNameCount()) {
919 uint32_t Hash = NI.getHashArrayEntry(Idx);
920 if (Hash % NI.getBucketCount() != B.Bucket)
921 break;
922
Pavel Labathd6ca0632018-06-01 10:33:11 +0000923 const char *Str = NI.getNameTableEntry(Idx).getString();
Pavel Labath906b7772018-03-16 10:02:16 +0000924 if (caseFoldingDjbHash(Str) != Hash) {
925 error() << formatv("Name Index @ {0:x}: String ({1}) at index {2} "
926 "hashes to {3:x}, but "
927 "the Name Index hash is {4:x}\n",
928 NI.getUnitOffset(), Str, Idx,
929 caseFoldingDjbHash(Str), Hash);
930 ++NumErrors;
931 }
932
933 ++Idx;
934 }
935 NextUncovered = std::max(NextUncovered, Idx);
936 }
937 return NumErrors;
938}
939
Pavel Labath79cd9422018-03-22 14:50:44 +0000940unsigned DWARFVerifier::verifyNameIndexAttribute(
941 const DWARFDebugNames::NameIndex &NI, const DWARFDebugNames::Abbrev &Abbr,
942 DWARFDebugNames::AttributeEncoding AttrEnc) {
943 StringRef FormName = dwarf::FormEncodingString(AttrEnc.Form);
944 if (FormName.empty()) {
945 error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
946 "unknown form: {3}.\n",
947 NI.getUnitOffset(), Abbr.Code, AttrEnc.Index,
948 AttrEnc.Form);
949 return 1;
950 }
951
952 if (AttrEnc.Index == DW_IDX_type_hash) {
953 if (AttrEnc.Form != dwarf::DW_FORM_data8) {
954 error() << formatv(
955 "NameIndex @ {0:x}: Abbreviation {1:x}: DW_IDX_type_hash "
956 "uses an unexpected form {2} (should be {3}).\n",
957 NI.getUnitOffset(), Abbr.Code, AttrEnc.Form, dwarf::DW_FORM_data8);
958 return 1;
959 }
960 }
961
962 // A list of known index attributes and their expected form classes.
963 // DW_IDX_type_hash is handled specially in the check above, as it has a
964 // specific form (not just a form class) we should expect.
965 struct FormClassTable {
966 dwarf::Index Index;
967 DWARFFormValue::FormClass Class;
968 StringLiteral ClassName;
969 };
970 static constexpr FormClassTable Table[] = {
971 {dwarf::DW_IDX_compile_unit, DWARFFormValue::FC_Constant, {"constant"}},
972 {dwarf::DW_IDX_type_unit, DWARFFormValue::FC_Constant, {"constant"}},
973 {dwarf::DW_IDX_die_offset, DWARFFormValue::FC_Reference, {"reference"}},
974 {dwarf::DW_IDX_parent, DWARFFormValue::FC_Constant, {"constant"}},
975 };
976
977 ArrayRef<FormClassTable> TableRef(Table);
978 auto Iter = find_if(TableRef, [AttrEnc](const FormClassTable &T) {
979 return T.Index == AttrEnc.Index;
980 });
981 if (Iter == TableRef.end()) {
982 warn() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} contains an "
983 "unknown index attribute: {2}.\n",
984 NI.getUnitOffset(), Abbr.Code, AttrEnc.Index);
985 return 0;
986 }
987
988 if (!DWARFFormValue(AttrEnc.Form).isFormClass(Iter->Class)) {
989 error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
990 "unexpected form {3} (expected form class {4}).\n",
991 NI.getUnitOffset(), Abbr.Code, AttrEnc.Index,
992 AttrEnc.Form, Iter->ClassName);
993 return 1;
994 }
995 return 0;
996}
997
998unsigned
999DWARFVerifier::verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI) {
Pavel Labathc9f07b02018-04-06 13:34:12 +00001000 if (NI.getLocalTUCount() + NI.getForeignTUCount() > 0) {
1001 warn() << formatv("Name Index @ {0:x}: Verifying indexes of type units is "
1002 "not currently supported.\n",
1003 NI.getUnitOffset());
1004 return 0;
1005 }
1006
Pavel Labath79cd9422018-03-22 14:50:44 +00001007 unsigned NumErrors = 0;
1008 for (const auto &Abbrev : NI.getAbbrevs()) {
1009 StringRef TagName = dwarf::TagString(Abbrev.Tag);
1010 if (TagName.empty()) {
1011 warn() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} references an "
1012 "unknown tag: {2}.\n",
1013 NI.getUnitOffset(), Abbrev.Code, Abbrev.Tag);
1014 }
1015 SmallSet<unsigned, 5> Attributes;
1016 for (const auto &AttrEnc : Abbrev.Attributes) {
1017 if (!Attributes.insert(AttrEnc.Index).second) {
1018 error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} contains "
1019 "multiple {2} attributes.\n",
1020 NI.getUnitOffset(), Abbrev.Code, AttrEnc.Index);
1021 ++NumErrors;
1022 continue;
1023 }
1024 NumErrors += verifyNameIndexAttribute(NI, Abbrev, AttrEnc);
1025 }
Pavel Labathc9f07b02018-04-06 13:34:12 +00001026
1027 if (NI.getCUCount() > 1 && !Attributes.count(dwarf::DW_IDX_compile_unit)) {
1028 error() << formatv("NameIndex @ {0:x}: Indexing multiple compile units "
1029 "and abbreviation {1:x} has no {2} attribute.\n",
1030 NI.getUnitOffset(), Abbrev.Code,
1031 dwarf::DW_IDX_compile_unit);
1032 ++NumErrors;
1033 }
1034 if (!Attributes.count(dwarf::DW_IDX_die_offset)) {
1035 error() << formatv(
1036 "NameIndex @ {0:x}: Abbreviation {1:x} has no {2} attribute.\n",
1037 NI.getUnitOffset(), Abbrev.Code, dwarf::DW_IDX_die_offset);
1038 ++NumErrors;
1039 }
Pavel Labath79cd9422018-03-22 14:50:44 +00001040 }
1041 return NumErrors;
1042}
1043
Pavel Labathc9f07b02018-04-06 13:34:12 +00001044static SmallVector<StringRef, 2> getNames(const DWARFDie &DIE) {
1045 SmallVector<StringRef, 2> Result;
1046 if (const char *Str = DIE.getName(DINameKind::ShortName))
1047 Result.emplace_back(Str);
1048 else if (DIE.getTag() == dwarf::DW_TAG_namespace)
1049 Result.emplace_back("(anonymous namespace)");
1050
1051 if (const char *Str = DIE.getName(DINameKind::LinkageName)) {
1052 if (Result.empty() || Result[0] != Str)
1053 Result.emplace_back(Str);
1054 }
1055
1056 return Result;
1057}
1058
Pavel Labathd6ca0632018-06-01 10:33:11 +00001059unsigned DWARFVerifier::verifyNameIndexEntries(
1060 const DWARFDebugNames::NameIndex &NI,
1061 const DWARFDebugNames::NameTableEntry &NTE) {
Pavel Labathc9f07b02018-04-06 13:34:12 +00001062 // Verifying type unit indexes not supported.
1063 if (NI.getLocalTUCount() + NI.getForeignTUCount() > 0)
1064 return 0;
1065
Pavel Labathd6ca0632018-06-01 10:33:11 +00001066 const char *CStr = NTE.getString();
Pavel Labathc9f07b02018-04-06 13:34:12 +00001067 if (!CStr) {
1068 error() << formatv(
1069 "Name Index @ {0:x}: Unable to get string associated with name {1}.\n",
Pavel Labathd6ca0632018-06-01 10:33:11 +00001070 NI.getUnitOffset(), NTE.getIndex());
Pavel Labathc9f07b02018-04-06 13:34:12 +00001071 return 1;
1072 }
1073 StringRef Str(CStr);
1074
1075 unsigned NumErrors = 0;
1076 unsigned NumEntries = 0;
Pavel Labathd6ca0632018-06-01 10:33:11 +00001077 uint32_t EntryID = NTE.getEntryOffset();
1078 uint32_t NextEntryID = EntryID;
1079 Expected<DWARFDebugNames::Entry> EntryOr = NI.getEntry(&NextEntryID);
1080 for (; EntryOr; ++NumEntries, EntryID = NextEntryID,
1081 EntryOr = NI.getEntry(&NextEntryID)) {
Pavel Labathc9f07b02018-04-06 13:34:12 +00001082 uint32_t CUIndex = *EntryOr->getCUIndex();
1083 if (CUIndex > NI.getCUCount()) {
1084 error() << formatv("Name Index @ {0:x}: Entry @ {1:x} contains an "
1085 "invalid CU index ({2}).\n",
1086 NI.getUnitOffset(), EntryID, CUIndex);
1087 ++NumErrors;
1088 continue;
1089 }
1090 uint32_t CUOffset = NI.getCUOffset(CUIndex);
1091 uint64_t DIEOffset = *EntryOr->getDIESectionOffset();
1092 DWARFDie DIE = DCtx.getDIEForOffset(DIEOffset);
1093 if (!DIE) {
1094 error() << formatv("Name Index @ {0:x}: Entry @ {1:x} references a "
1095 "non-existing DIE @ {2:x}.\n",
1096 NI.getUnitOffset(), EntryID, DIEOffset);
1097 ++NumErrors;
1098 continue;
1099 }
1100 if (DIE.getDwarfUnit()->getOffset() != CUOffset) {
1101 error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched CU of "
1102 "DIE @ {2:x}: index - {3:x}; debug_info - {4:x}.\n",
1103 NI.getUnitOffset(), EntryID, DIEOffset, CUOffset,
1104 DIE.getDwarfUnit()->getOffset());
1105 ++NumErrors;
1106 }
1107 if (DIE.getTag() != EntryOr->tag()) {
1108 error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched Tag of "
1109 "DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
1110 NI.getUnitOffset(), EntryID, DIEOffset, EntryOr->tag(),
1111 DIE.getTag());
1112 ++NumErrors;
1113 }
1114
1115 auto EntryNames = getNames(DIE);
1116 if (!is_contained(EntryNames, Str)) {
1117 error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched Name "
1118 "of DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
1119 NI.getUnitOffset(), EntryID, DIEOffset, Str,
1120 make_range(EntryNames.begin(), EntryNames.end()));
Pavel Labath2a6afe52018-05-14 14:13:20 +00001121 ++NumErrors;
Pavel Labathc9f07b02018-04-06 13:34:12 +00001122 }
1123 }
1124 handleAllErrors(EntryOr.takeError(),
1125 [&](const DWARFDebugNames::SentinelError &) {
1126 if (NumEntries > 0)
1127 return;
1128 error() << formatv("Name Index @ {0:x}: Name {1} ({2}) is "
1129 "not associated with any entries.\n",
Pavel Labathd6ca0632018-06-01 10:33:11 +00001130 NI.getUnitOffset(), NTE.getIndex(), Str);
Pavel Labathc9f07b02018-04-06 13:34:12 +00001131 ++NumErrors;
1132 },
1133 [&](const ErrorInfoBase &Info) {
Pavel Labathd6ca0632018-06-01 10:33:11 +00001134 error()
1135 << formatv("Name Index @ {0:x}: Name {1} ({2}): {3}\n",
1136 NI.getUnitOffset(), NTE.getIndex(), Str,
1137 Info.message());
Pavel Labathc9f07b02018-04-06 13:34:12 +00001138 ++NumErrors;
1139 });
1140 return NumErrors;
1141}
1142
Pavel Labath80827f12018-05-15 13:24:10 +00001143static bool isVariableIndexable(const DWARFDie &Die, DWARFContext &DCtx) {
1144 Optional<DWARFFormValue> Location = Die.findRecursively(DW_AT_location);
1145 if (!Location)
1146 return false;
1147
1148 auto ContainsInterestingOperators = [&](StringRef D) {
1149 DWARFUnit *U = Die.getDwarfUnit();
1150 DataExtractor Data(D, DCtx.isLittleEndian(), U->getAddressByteSize());
1151 DWARFExpression Expression(Data, U->getVersion(), U->getAddressByteSize());
1152 return any_of(Expression, [](DWARFExpression::Operation &Op) {
1153 return !Op.isError() && (Op.getCode() == DW_OP_addr ||
1154 Op.getCode() == DW_OP_form_tls_address ||
1155 Op.getCode() == DW_OP_GNU_push_tls_address);
1156 });
1157 };
1158
1159 if (Optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
1160 // Inlined location.
1161 if (ContainsInterestingOperators(toStringRef(*Expr)))
1162 return true;
1163 } else if (Optional<uint64_t> Offset = Location->getAsSectionOffset()) {
1164 // Location list.
1165 if (const DWARFDebugLoc *DebugLoc = DCtx.getDebugLoc()) {
1166 if (const DWARFDebugLoc::LocationList *LocList =
1167 DebugLoc->getLocationListAtOffset(*Offset)) {
1168 if (any_of(LocList->Entries, [&](const DWARFDebugLoc::Entry &E) {
1169 return ContainsInterestingOperators({E.Loc.data(), E.Loc.size()});
1170 }))
1171 return true;
1172 }
1173 }
1174 }
1175 return false;
1176}
1177
1178unsigned DWARFVerifier::verifyNameIndexCompleteness(
1179 const DWARFDie &Die, const DWARFDebugNames::NameIndex &NI) {
1180
1181 // First check, if the Die should be indexed. The code follows the DWARF v5
1182 // wording as closely as possible.
1183
1184 // "All non-defining declarations (that is, debugging information entries
1185 // with a DW_AT_declaration attribute) are excluded."
1186 if (Die.find(DW_AT_declaration))
1187 return 0;
1188
1189 // "DW_TAG_namespace debugging information entries without a DW_AT_name
1190 // attribute are included with the name “(anonymous namespace)”.
1191 // All other debugging information entries without a DW_AT_name attribute
1192 // are excluded."
1193 // "If a subprogram or inlined subroutine is included, and has a
1194 // DW_AT_linkage_name attribute, there will be an additional index entry for
1195 // the linkage name."
1196 auto EntryNames = getNames(Die);
1197 if (EntryNames.empty())
1198 return 0;
1199
1200 // We deviate from the specification here, which says:
1201 // "The name index must contain an entry for each debugging information entry
1202 // that defines a named subprogram, label, variable, type, or namespace,
1203 // subject to ..."
1204 // Instead whitelisting all TAGs representing a "type" or a "subprogram", to
1205 // make sure we catch any missing items, we instead blacklist all TAGs that we
1206 // know shouldn't be indexed.
1207 switch (Die.getTag()) {
1208 // Compile unit has a name but it shouldn't be indexed.
1209 case DW_TAG_compile_unit:
1210 return 0;
1211
1212 // Function and template parameters are not globally visible, so we shouldn't
1213 // index them.
1214 case DW_TAG_formal_parameter:
1215 case DW_TAG_template_value_parameter:
1216 case DW_TAG_template_type_parameter:
1217 case DW_TAG_GNU_template_parameter_pack:
1218 case DW_TAG_GNU_template_template_param:
1219 return 0;
1220
1221 // Object members aren't globally visible.
1222 case DW_TAG_member:
1223 return 0;
1224
1225 // According to a strict reading of the specification, enumerators should not
1226 // be indexed (and LLVM currently does not do that). However, this causes
1227 // problems for the debuggers, so we may need to reconsider this.
1228 case DW_TAG_enumerator:
1229 return 0;
1230
1231 // Imported declarations should not be indexed according to the specification
1232 // and LLVM currently does not do that.
1233 case DW_TAG_imported_declaration:
1234 return 0;
1235
1236 // "DW_TAG_subprogram, DW_TAG_inlined_subroutine, and DW_TAG_label debugging
1237 // information entries without an address attribute (DW_AT_low_pc,
1238 // DW_AT_high_pc, DW_AT_ranges, or DW_AT_entry_pc) are excluded."
1239 case DW_TAG_subprogram:
1240 case DW_TAG_inlined_subroutine:
1241 case DW_TAG_label:
1242 if (Die.findRecursively(
1243 {DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_entry_pc}))
1244 break;
1245 return 0;
1246
1247 // "DW_TAG_variable debugging information entries with a DW_AT_location
1248 // attribute that includes a DW_OP_addr or DW_OP_form_tls_address operator are
1249 // included; otherwise, they are excluded."
1250 //
1251 // LLVM extension: We also add DW_OP_GNU_push_tls_address to this list.
1252 case DW_TAG_variable:
1253 if (isVariableIndexable(Die, DCtx))
1254 break;
1255 return 0;
1256
1257 default:
1258 break;
1259 }
1260
1261 // Now we know that our Die should be present in the Index. Let's check if
1262 // that's the case.
1263 unsigned NumErrors = 0;
1264 for (StringRef Name : EntryNames) {
1265 if (none_of(NI.equal_range(Name), [&Die](const DWARFDebugNames::Entry &E) {
1266 return E.getDIESectionOffset() == uint64_t(Die.getOffset());
1267 })) {
1268 error() << formatv("Name Index @ {0:x}: Entry for DIE @ {1:x} ({2}) with "
1269 "name {3} missing.\n",
1270 NI.getUnitOffset(), Die.getOffset(), Die.getTag(),
1271 Name);
1272 ++NumErrors;
1273 }
1274 }
1275 return NumErrors;
1276}
1277
Pavel Labathb136c392018-03-08 15:34:42 +00001278unsigned DWARFVerifier::verifyDebugNames(const DWARFSection &AccelSection,
1279 const DataExtractor &StrData) {
1280 unsigned NumErrors = 0;
1281 DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), AccelSection,
1282 DCtx.isLittleEndian(), 0);
1283 DWARFDebugNames AccelTable(AccelSectionData, StrData);
1284
1285 OS << "Verifying .debug_names...\n";
1286
1287 // This verifies that we can read individual name indices and their
1288 // abbreviation tables.
1289 if (Error E = AccelTable.extract()) {
1290 error() << toString(std::move(E)) << '\n';
1291 return 1;
1292 }
1293
1294 NumErrors += verifyDebugNamesCULists(AccelTable);
Pavel Labath906b7772018-03-16 10:02:16 +00001295 for (const auto &NI : AccelTable)
1296 NumErrors += verifyNameIndexBuckets(NI, StrData);
Pavel Labath79cd9422018-03-22 14:50:44 +00001297 for (const auto &NI : AccelTable)
1298 NumErrors += verifyNameIndexAbbrevs(NI);
Pavel Labathb136c392018-03-08 15:34:42 +00001299
Pavel Labathc9f07b02018-04-06 13:34:12 +00001300 // Don't attempt Entry validation if any of the previous checks found errors
1301 if (NumErrors > 0)
1302 return NumErrors;
1303 for (const auto &NI : AccelTable)
Pavel Labathd6ca0632018-06-01 10:33:11 +00001304 for (DWARFDebugNames::NameTableEntry NTE : NI)
1305 NumErrors += verifyNameIndexEntries(NI, NTE);
Pavel Labathc9f07b02018-04-06 13:34:12 +00001306
Pavel Labath80827f12018-05-15 13:24:10 +00001307 if (NumErrors > 0)
1308 return NumErrors;
1309
1310 for (const std::unique_ptr<DWARFCompileUnit> &CU : DCtx.compile_units()) {
1311 if (const DWARFDebugNames::NameIndex *NI =
1312 AccelTable.getCUNameIndex(CU->getOffset())) {
1313 for (const DWARFDebugInfoEntry &Die : CU->dies())
1314 NumErrors += verifyNameIndexCompleteness(DWARFDie(CU.get(), &Die), *NI);
1315 }
1316 }
Pavel Labathb136c392018-03-08 15:34:42 +00001317 return NumErrors;
1318}
1319
Spyridoula Gravanidc635f42017-07-26 00:52:31 +00001320bool DWARFVerifier::handleAccelTables() {
1321 const DWARFObject &D = DCtx.getDWARFObj();
1322 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
1323 unsigned NumErrors = 0;
1324 if (!D.getAppleNamesSection().Data.empty())
1325 NumErrors +=
Pavel Labath9b36fd22018-01-22 13:17:23 +00001326 verifyAppleAccelTable(&D.getAppleNamesSection(), &StrData, ".apple_names");
Spyridoula Gravanidc635f42017-07-26 00:52:31 +00001327 if (!D.getAppleTypesSection().Data.empty())
1328 NumErrors +=
Pavel Labath9b36fd22018-01-22 13:17:23 +00001329 verifyAppleAccelTable(&D.getAppleTypesSection(), &StrData, ".apple_types");
Spyridoula Gravanidc635f42017-07-26 00:52:31 +00001330 if (!D.getAppleNamespacesSection().Data.empty())
Pavel Labath9b36fd22018-01-22 13:17:23 +00001331 NumErrors += verifyAppleAccelTable(&D.getAppleNamespacesSection(), &StrData,
Spyridoula Gravanidc635f42017-07-26 00:52:31 +00001332 ".apple_namespaces");
1333 if (!D.getAppleObjCSection().Data.empty())
1334 NumErrors +=
Pavel Labath9b36fd22018-01-22 13:17:23 +00001335 verifyAppleAccelTable(&D.getAppleObjCSection(), &StrData, ".apple_objc");
Pavel Labathb136c392018-03-08 15:34:42 +00001336
1337 if (!D.getDebugNamesSection().Data.empty())
1338 NumErrors += verifyDebugNames(D.getDebugNamesSection(), StrData);
Spyridoula Gravanidc635f42017-07-26 00:52:31 +00001339 return NumErrors == 0;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +00001340}
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +00001341
Jonas Devlieghere6be1f012018-04-15 08:44:15 +00001342raw_ostream &DWARFVerifier::error() const { return WithColor::error(OS); }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +00001343
Jonas Devlieghere6be1f012018-04-15 08:44:15 +00001344raw_ostream &DWARFVerifier::warn() const { return WithColor::warning(OS); }
Jonas Devlieghere19fc4d92017-09-29 09:33:31 +00001345
Jonas Devlieghere6be1f012018-04-15 08:44:15 +00001346raw_ostream &DWARFVerifier::note() const { return WithColor::note(OS); }