blob: d683642af02350aff2452e9502b53d4b2e4ab5f2 [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"
11#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
12#include "llvm/DebugInfo/DWARF/DWARFContext.h"
13#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
14#include "llvm/DebugInfo/DWARF/DWARFDie.h"
15#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
16#include "llvm/DebugInfo/DWARF/DWARFSection.h"
Spyridoula Gravanie41823b2017-06-14 00:17:55 +000017#include "llvm/DebugInfo/DWARF/DWARFAcceleratorTable.h"
Greg Claytonb8c162b2017-05-03 16:02:29 +000018#include "llvm/Support/raw_ostream.h"
19#include <map>
20#include <set>
21#include <vector>
22
23using namespace llvm;
24using namespace dwarf;
25using namespace object;
26
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000027bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000028 uint32_t *Offset, unsigned UnitIndex,
29 uint8_t &UnitType, bool &isUnitDWARF64) {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000030 uint32_t AbbrOffset, Length;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000031 uint8_t AddrSize = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000032 uint16_t Version;
33 bool Success = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000034
35 bool ValidLength = false;
36 bool ValidVersion = false;
37 bool ValidAddrSize = false;
38 bool ValidType = true;
39 bool ValidAbbrevOffset = true;
40
41 uint32_t OffsetStart = *Offset;
42 Length = DebugInfoData.getU32(Offset);
43 if (Length == UINT32_MAX) {
44 isUnitDWARF64 = true;
45 OS << format(
46 "Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
47 UnitIndex);
48 return false;
49 }
50 Version = DebugInfoData.getU16(Offset);
51
52 if (Version >= 5) {
53 UnitType = DebugInfoData.getU8(Offset);
54 AddrSize = DebugInfoData.getU8(Offset);
55 AbbrOffset = DebugInfoData.getU32(Offset);
56 ValidType = DWARFUnit::isValidUnitType(UnitType);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000057 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000058 UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +000059 AbbrOffset = DebugInfoData.getU32(Offset);
60 AddrSize = DebugInfoData.getU8(Offset);
61 }
62
63 if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
64 ValidAbbrevOffset = false;
65
66 ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
67 ValidVersion = DWARFContext::isSupportedVersion(Version);
68 ValidAddrSize = AddrSize == 4 || AddrSize == 8;
69 if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
70 !ValidType) {
71 Success = false;
72 OS << format("Units[%d] - start offset: 0x%08x \n", UnitIndex, OffsetStart);
73 if (!ValidLength)
74 OS << "\tError: The length for this unit is too "
75 "large for the .debug_info provided.\n";
76 if (!ValidVersion)
77 OS << "\tError: The 16 bit unit header version is not valid.\n";
78 if (!ValidType)
79 OS << "\tError: The unit type encoding is not valid.\n";
80 if (!ValidAbbrevOffset)
81 OS << "\tError: The offset into the .debug_abbrev section is "
82 "not valid.\n";
83 if (!ValidAddrSize)
84 OS << "\tError: The address size is unsupported.\n";
85 }
86 *Offset = OffsetStart + Length + 4;
87 return Success;
88}
89
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +000090bool DWARFVerifier::verifyUnitContents(DWARFUnit Unit) {
91 uint32_t NumUnitErrors = 0;
92 unsigned NumDies = Unit.getNumDIEs();
93 for (unsigned I = 0; I < NumDies; ++I) {
94 auto Die = Unit.getDIEAtIndex(I);
95 if (Die.getTag() == DW_TAG_null)
96 continue;
97 for (auto AttrValue : Die.attributes()) {
98 NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
99 NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
100 }
101 }
102 return NumUnitErrors == 0;
103}
104
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000105bool DWARFVerifier::handleDebugAbbrev() {
106 OS << "Verifying .debug_abbrev...\n";
107
108 const DWARFObject &DObj = DCtx.getDWARFObj();
109 if (DObj.getAbbrevSection().empty()) {
110 OS << "Warning: .debug_abbrev is empty.\n";
111 return true;
112 }
113
114 unsigned NumErrors = 0;
115 const DWARFDebugAbbrev *Abbrev = DCtx.getDebugAbbrev();
116 if (Abbrev) {
117 const DWARFAbbreviationDeclarationSet *AbbrDecls =
118 Abbrev->getAbbreviationDeclarationSet(0);
119 for (auto AbbrDecl : *AbbrDecls) {
120 SmallDenseSet<uint16_t> AttributeSet;
121 for (auto Attribute : AbbrDecl.attributes()) {
122 auto Result = AttributeSet.insert(Attribute.Attr);
123 if (!Result.second) {
124 OS << format("Error: Abbreviation declaration with code %d ",
125 AbbrDecl.getCode());
126 OS << "contains multiple " << AttributeString(Attribute.Attr)
127 << " attributes.\n";
128 ++NumErrors;
129 }
130 }
131 }
132 }
133 return NumErrors == 0;
134}
135
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000136bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000137 OS << "Verifying .debug_info Unit Header Chain...\n";
138
Rafael Espindolac398e672017-07-19 22:27:28 +0000139 const DWARFObject &DObj = DCtx.getDWARFObj();
140 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
141 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000142 uint32_t NumDebugInfoErrors = 0;
143 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
144 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000145 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000146 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000147 bool hasDIE = DebugInfoData.isValidOffset(Offset);
148 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000149 OffsetStart = Offset;
150 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
151 isUnitDWARF64)) {
152 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000153 if (isUnitDWARF64)
154 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000155 } else {
156 std::unique_ptr<DWARFUnit> Unit;
157 switch (UnitType) {
158 case dwarf::DW_UT_type:
159 case dwarf::DW_UT_split_type: {
160 DWARFUnitSection<DWARFTypeUnit> TUSection{};
161 Unit.reset(new DWARFTypeUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000162 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
163 &DObj.getRangeSection(), DObj.getStringSection(),
164 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
165 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000166 nullptr));
167 break;
168 }
169 case dwarf::DW_UT_skeleton:
170 case dwarf::DW_UT_split_compile:
171 case dwarf::DW_UT_compile:
172 case dwarf::DW_UT_partial:
173 // UnitType = 0 means that we are
174 // verifying a compile unit in DWARF v4.
175 case 0: {
176 DWARFUnitSection<DWARFCompileUnit> CUSection{};
177 Unit.reset(new DWARFCompileUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000178 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
179 &DObj.getRangeSection(), DObj.getStringSection(),
180 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
181 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000182 nullptr));
183 break;
184 }
185 default: { llvm_unreachable("Invalid UnitType."); }
186 }
187 Unit->extract(DebugInfoData, &OffsetStart);
188 if (!verifyUnitContents(*Unit))
189 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000190 }
191 hasDIE = DebugInfoData.isValidOffset(Offset);
192 ++UnitIdx;
193 }
194 if (UnitIdx == 0 && !hasDIE) {
195 OS << "Warning: .debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000196 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000197 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000198 NumDebugInfoErrors += verifyDebugInfoReferences();
199 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000200}
201
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000202unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
203 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000204 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000205 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000206 const auto Attr = AttrValue.Attr;
207 switch (Attr) {
208 case DW_AT_ranges:
209 // Make sure the offset in the DW_AT_ranges attribute is valid.
210 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000211 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000212 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000213 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
214 "bounds:\n";
215 Die.dump(OS, 0);
216 OS << "\n";
217 }
218 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000219 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000220 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
221 Die.dump(OS, 0);
222 OS << "\n";
223 }
224 break;
225 case DW_AT_stmt_list:
226 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
227 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000228 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000229 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000230 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
231 "bounds: "
232 << format("0x%08" PRIx32, *SectionOffset) << "\n";
233 Die.dump(OS, 0);
234 OS << "\n";
235 }
236 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000237 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000238 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
239 Die.dump(OS, 0);
240 OS << "\n";
241 }
242 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000243
Greg Claytonc5b2d562017-05-03 18:25:46 +0000244 default:
245 break;
246 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000247 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000248}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000249
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000250unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
251 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000252 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000253 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000254 const auto Form = AttrValue.Value.getForm();
255 switch (Form) {
256 case DW_FORM_ref1:
257 case DW_FORM_ref2:
258 case DW_FORM_ref4:
259 case DW_FORM_ref8:
260 case DW_FORM_ref_udata: {
261 // Verify all CU relative references are valid CU offsets.
262 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
263 assert(RefVal);
264 if (RefVal) {
265 auto DieCU = Die.getDwarfUnit();
266 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
267 auto CUOffset = AttrValue.Value.getRawUValue();
268 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000269 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000270 OS << "error: " << FormEncodingString(Form) << " CU offset "
271 << format("0x%08" PRIx32, CUOffset)
272 << " is invalid (must be less than CU size of "
273 << format("0x%08" PRIx32, CUSize) << "):\n";
274 Die.dump(OS, 0);
275 OS << "\n";
276 } else {
277 // Valid reference, but we will verify it points to an actual
278 // DIE later.
279 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000280 }
281 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000282 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000283 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000284 case DW_FORM_ref_addr: {
285 // Verify all absolute DIE references have valid offsets in the
286 // .debug_info section.
287 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
288 assert(RefVal);
289 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000290 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000291 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000292 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
293 "bounds:\n";
294 Die.dump(OS, 0);
295 OS << "\n";
296 } else {
297 // Valid reference, but we will verify it points to an actual
298 // DIE later.
299 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
300 }
301 }
302 break;
303 }
304 case DW_FORM_strp: {
305 auto SecOffset = AttrValue.Value.getAsSectionOffset();
306 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000307 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000308 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000309 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
310 Die.dump(OS, 0);
311 OS << "\n";
312 }
313 break;
314 }
315 default:
316 break;
317 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000318 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000319}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000320
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000321unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000322 // Take all references and make sure they point to an actual DIE by
323 // getting the DIE by offset and emitting an error
324 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000325 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000326 for (auto Pair : ReferenceToDIEOffsets) {
327 auto Die = DCtx.getDIEForOffset(Pair.first);
328 if (Die)
329 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000330 ++NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000331 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
332 << ". Offset is in between DIEs:\n";
333 for (auto Offset : Pair.second) {
334 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
335 ReferencingDie.dump(OS, 0);
336 OS << "\n";
337 }
338 OS << "\n";
339 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000340 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000341}
342
Greg Claytonc5b2d562017-05-03 18:25:46 +0000343void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000344 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000345 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000346 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000347 // Get the attribute value as a section offset. No need to produce an
348 // error here if the encoding isn't correct because we validate this in
349 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000350 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000351 if (!StmtSectionOffset)
352 continue;
353 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000354 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000355 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000356 if (!LineTable) {
357 ++NumDebugLineErrors;
358 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
359 << "] was not able to be parsed for CU:\n";
360 Die.dump(OS, 0);
361 OS << '\n';
362 continue;
363 }
364 } else {
365 // Make sure we don't get a valid line table back if the offset is wrong.
366 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000367 // Skip this line table as it isn't valid. No need to create an error
368 // here because we validate this in the .debug_info verifier.
369 continue;
370 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000371 auto Iter = StmtListToDie.find(LineTableOffset);
372 if (Iter != StmtListToDie.end()) {
373 ++NumDebugLineErrors;
374 OS << "error: two compile unit DIEs, "
375 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000376 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000377 << ", have the same DW_AT_stmt_list section offset:\n";
378 Iter->second.dump(OS, 0);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000379 Die.dump(OS, 0);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000380 OS << '\n';
381 // Already verified this line table before, no need to do it again.
382 continue;
383 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000384 StmtListToDie[LineTableOffset] = Die;
385 }
386}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000387
Greg Claytonc5b2d562017-05-03 18:25:46 +0000388void DWARFVerifier::verifyDebugLineRows() {
389 for (const auto &CU : DCtx.compile_units()) {
390 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000391 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000392 // If there is no line table we will have created an error in the
393 // .debug_info verifier or in verifyDebugLineStmtOffsets().
394 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000395 continue;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000396 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
397 uint64_t PrevAddress = 0;
398 uint32_t RowIndex = 0;
399 for (const auto &Row : LineTable->Rows) {
400 if (Row.Address < PrevAddress) {
401 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000402 OS << "error: .debug_line["
403 << format("0x%08" PRIx32,
404 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000405 << "] row[" << RowIndex
406 << "] decreases in address from previous row:\n";
407
408 DWARFDebugLine::Row::dumpTableHeader(OS);
409 if (RowIndex > 0)
410 LineTable->Rows[RowIndex - 1].dump(OS);
411 Row.dump(OS);
412 OS << '\n';
413 }
414
415 if (Row.File > MaxFileIndex) {
416 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000417 OS << "error: .debug_line["
418 << format("0x%08" PRIx32,
419 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000420 << "][" << RowIndex << "] has invalid file index " << Row.File
421 << " (valid values are [1," << MaxFileIndex << "]):\n";
422 DWARFDebugLine::Row::dumpTableHeader(OS);
423 Row.dump(OS);
424 OS << '\n';
425 }
426 if (Row.EndSequence)
427 PrevAddress = 0;
428 else
429 PrevAddress = Row.Address;
430 ++RowIndex;
431 }
432 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000433}
434
435bool DWARFVerifier::handleDebugLine() {
436 NumDebugLineErrors = 0;
437 OS << "Verifying .debug_line...\n";
438 verifyDebugLineStmtOffsets();
439 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000440 return NumDebugLineErrors == 0;
441}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000442
443bool DWARFVerifier::handleAppleNames() {
444 NumAppleNamesErrors = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000445 const DWARFObject &D = DCtx.getDWARFObj();
446 DWARFDataExtractor AppleNamesSection(D, D.getAppleNamesSection(),
Paul Robinson17536b92017-06-29 16:52:08 +0000447 DCtx.isLittleEndian(), 0);
Rafael Espindolac398e672017-07-19 22:27:28 +0000448 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
Paul Robinson17536b92017-06-29 16:52:08 +0000449 DWARFAcceleratorTable AppleNames(AppleNamesSection, StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000450
451 if (!AppleNames.extract()) {
Spyridoula Gravani32614fc2017-06-16 22:03:21 +0000452 return true;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000453 }
454
Spyridoula Gravani32614fc2017-06-16 22:03:21 +0000455 OS << "Verifying .apple_names...\n";
456
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000457 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000458 uint32_t NumBuckets = AppleNames.getNumBuckets();
459 uint32_t NumHashes = AppleNames.getNumHashes();
460
461 uint32_t BucketsOffset =
462 AppleNames.getSizeHdr() + AppleNames.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000463 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
464 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000465
466 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
467 uint32_t HashIdx = AppleNamesSection.getU32(&BucketsOffset);
468 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000469 OS << format("error: Bucket[%d] has invalid hash index: %u\n", BucketIdx,
470 HashIdx);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000471 ++NumAppleNamesErrors;
472 }
473 }
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000474
475 uint32_t NumAtoms = AppleNames.getAtomsDesc().size();
476 if (NumAtoms == 0) {
477 OS << "error: no atoms; failed to read HashData\n";
478 ++NumAppleNamesErrors;
479 return false;
480 }
481
482 if (!AppleNames.validateForms()) {
483 OS << "error: unsupported form; failed to read HashData\n";
484 ++NumAppleNamesErrors;
485 return false;
486 }
487
488 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
489 uint32_t HashOffset = HashesBase + 4 * HashIdx;
490 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
491 uint32_t Hash = AppleNamesSection.getU32(&HashOffset);
492 uint32_t HashDataOffset = AppleNamesSection.getU32(&DataOffset);
493 if (!AppleNamesSection.isValidOffsetForDataOfSize(HashDataOffset,
494 sizeof(uint64_t))) {
495 OS << format("error: Hash[%d] has invalid HashData offset: 0x%08x\n",
496 HashIdx, HashDataOffset);
497 ++NumAppleNamesErrors;
498 }
499
500 uint32_t StrpOffset;
501 uint32_t StringOffset;
502 uint32_t StringCount = 0;
503 uint32_t DieOffset = dwarf::DW_INVALID_OFFSET;
504
505 while ((StrpOffset = AppleNamesSection.getU32(&HashDataOffset)) != 0) {
506 const uint32_t NumHashDataObjects =
507 AppleNamesSection.getU32(&HashDataOffset);
508 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
509 ++HashDataIdx) {
510 DieOffset = AppleNames.readAtoms(HashDataOffset);
511 if (!DCtx.getDIEForOffset(DieOffset)) {
512 const uint32_t BucketIdx =
513 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
514 StringOffset = StrpOffset;
515 const char *Name = StrData.getCStr(&StringOffset);
516 if (!Name)
517 Name = "<NULL>";
518
519 OS << format(
520 "error: .apple_names Bucket[%d] Hash[%d] = 0x%08x "
521 "Str[%u] = 0x%08x "
522 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
523 BucketIdx, HashIdx, Hash, StringCount, StrpOffset, HashDataIdx,
524 DieOffset, Name);
525
526 ++NumAppleNamesErrors;
527 }
528 }
529 ++StringCount;
530 }
531 }
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000532 return NumAppleNamesErrors == 0;
533}