blob: db38b63242286f0f9b08e4604dd1201746987dff [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 Gravanic6ef9872017-07-21 00:51:32 +0000105unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000106 unsigned NumErrors = 0;
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000107 if (Abbrev) {
108 const DWARFAbbreviationDeclarationSet *AbbrDecls =
109 Abbrev->getAbbreviationDeclarationSet(0);
110 for (auto AbbrDecl : *AbbrDecls) {
111 SmallDenseSet<uint16_t> AttributeSet;
112 for (auto Attribute : AbbrDecl.attributes()) {
113 auto Result = AttributeSet.insert(Attribute.Attr);
114 if (!Result.second) {
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000115 OS << "Error: Abbreviation declaration contains multiple "
116 << AttributeString(Attribute.Attr) << " attributes.\n";
117 AbbrDecl.dump(OS);
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000118 ++NumErrors;
119 }
120 }
121 }
122 }
Spyridoula Gravanic6ef9872017-07-21 00:51:32 +0000123 return NumErrors;
124}
125
126bool DWARFVerifier::handleDebugAbbrev() {
127 OS << "Verifying .debug_abbrev...\n";
128
129 const DWARFObject &DObj = DCtx.getDWARFObj();
130 bool noDebugAbbrev = DObj.getAbbrevSection().empty();
131 bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
132
133 if (noDebugAbbrev && noDebugAbbrevDWO) {
134 return true;
135 }
136
137 unsigned NumErrors = 0;
138 if (!noDebugAbbrev)
139 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
140
141 if (!noDebugAbbrevDWO)
142 NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
Spyridoula Gravani364b5352017-07-20 02:06:52 +0000143 return NumErrors == 0;
144}
145
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000146bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000147 OS << "Verifying .debug_info Unit Header Chain...\n";
148
Rafael Espindolac398e672017-07-19 22:27:28 +0000149 const DWARFObject &DObj = DCtx.getDWARFObj();
150 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
151 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000152 uint32_t NumDebugInfoErrors = 0;
153 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
154 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000155 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000156 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000157 bool hasDIE = DebugInfoData.isValidOffset(Offset);
158 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000159 OffsetStart = Offset;
160 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
161 isUnitDWARF64)) {
162 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000163 if (isUnitDWARF64)
164 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000165 } else {
166 std::unique_ptr<DWARFUnit> Unit;
167 switch (UnitType) {
168 case dwarf::DW_UT_type:
169 case dwarf::DW_UT_split_type: {
170 DWARFUnitSection<DWARFTypeUnit> TUSection{};
171 Unit.reset(new DWARFTypeUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000172 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
173 &DObj.getRangeSection(), DObj.getStringSection(),
174 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
175 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000176 nullptr));
177 break;
178 }
179 case dwarf::DW_UT_skeleton:
180 case dwarf::DW_UT_split_compile:
181 case dwarf::DW_UT_compile:
182 case dwarf::DW_UT_partial:
183 // UnitType = 0 means that we are
184 // verifying a compile unit in DWARF v4.
185 case 0: {
186 DWARFUnitSection<DWARFCompileUnit> CUSection{};
187 Unit.reset(new DWARFCompileUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000188 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
189 &DObj.getRangeSection(), DObj.getStringSection(),
190 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
191 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000192 nullptr));
193 break;
194 }
195 default: { llvm_unreachable("Invalid UnitType."); }
196 }
197 Unit->extract(DebugInfoData, &OffsetStart);
198 if (!verifyUnitContents(*Unit))
199 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000200 }
201 hasDIE = DebugInfoData.isValidOffset(Offset);
202 ++UnitIdx;
203 }
204 if (UnitIdx == 0 && !hasDIE) {
205 OS << "Warning: .debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000206 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000207 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000208 NumDebugInfoErrors += verifyDebugInfoReferences();
209 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000210}
211
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000212unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
213 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000214 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000215 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000216 const auto Attr = AttrValue.Attr;
217 switch (Attr) {
218 case DW_AT_ranges:
219 // Make sure the offset in the DW_AT_ranges attribute is valid.
220 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000221 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000222 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000223 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
224 "bounds:\n";
225 Die.dump(OS, 0);
226 OS << "\n";
227 }
228 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000229 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000230 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
231 Die.dump(OS, 0);
232 OS << "\n";
233 }
234 break;
235 case DW_AT_stmt_list:
236 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
237 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000238 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000239 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000240 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
241 "bounds: "
242 << format("0x%08" PRIx32, *SectionOffset) << "\n";
243 Die.dump(OS, 0);
244 OS << "\n";
245 }
246 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000247 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000248 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
249 Die.dump(OS, 0);
250 OS << "\n";
251 }
252 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000253
Greg Claytonc5b2d562017-05-03 18:25:46 +0000254 default:
255 break;
256 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000257 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000258}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000259
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000260unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
261 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000262 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000263 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000264 const auto Form = AttrValue.Value.getForm();
265 switch (Form) {
266 case DW_FORM_ref1:
267 case DW_FORM_ref2:
268 case DW_FORM_ref4:
269 case DW_FORM_ref8:
270 case DW_FORM_ref_udata: {
271 // Verify all CU relative references are valid CU offsets.
272 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
273 assert(RefVal);
274 if (RefVal) {
275 auto DieCU = Die.getDwarfUnit();
276 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
277 auto CUOffset = AttrValue.Value.getRawUValue();
278 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000279 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000280 OS << "error: " << FormEncodingString(Form) << " CU offset "
281 << format("0x%08" PRIx32, CUOffset)
282 << " is invalid (must be less than CU size of "
283 << format("0x%08" PRIx32, CUSize) << "):\n";
284 Die.dump(OS, 0);
285 OS << "\n";
286 } else {
287 // Valid reference, but we will verify it points to an actual
288 // DIE later.
289 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000290 }
291 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000292 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000293 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000294 case DW_FORM_ref_addr: {
295 // Verify all absolute DIE references have valid offsets in the
296 // .debug_info section.
297 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
298 assert(RefVal);
299 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000300 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000301 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000302 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
303 "bounds:\n";
304 Die.dump(OS, 0);
305 OS << "\n";
306 } else {
307 // Valid reference, but we will verify it points to an actual
308 // DIE later.
309 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
310 }
311 }
312 break;
313 }
314 case DW_FORM_strp: {
315 auto SecOffset = AttrValue.Value.getAsSectionOffset();
316 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000317 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000318 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000319 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
320 Die.dump(OS, 0);
321 OS << "\n";
322 }
323 break;
324 }
325 default:
326 break;
327 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000328 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000329}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000330
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000331unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000332 // Take all references and make sure they point to an actual DIE by
333 // getting the DIE by offset and emitting an error
334 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000335 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000336 for (auto Pair : ReferenceToDIEOffsets) {
337 auto Die = DCtx.getDIEForOffset(Pair.first);
338 if (Die)
339 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000340 ++NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000341 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
342 << ". Offset is in between DIEs:\n";
343 for (auto Offset : Pair.second) {
344 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
345 ReferencingDie.dump(OS, 0);
346 OS << "\n";
347 }
348 OS << "\n";
349 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000350 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000351}
352
Greg Claytonc5b2d562017-05-03 18:25:46 +0000353void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000354 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000355 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000356 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000357 // Get the attribute value as a section offset. No need to produce an
358 // error here if the encoding isn't correct because we validate this in
359 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000360 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000361 if (!StmtSectionOffset)
362 continue;
363 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000364 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000365 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000366 if (!LineTable) {
367 ++NumDebugLineErrors;
368 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
369 << "] was not able to be parsed for CU:\n";
370 Die.dump(OS, 0);
371 OS << '\n';
372 continue;
373 }
374 } else {
375 // Make sure we don't get a valid line table back if the offset is wrong.
376 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000377 // Skip this line table as it isn't valid. No need to create an error
378 // here because we validate this in the .debug_info verifier.
379 continue;
380 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000381 auto Iter = StmtListToDie.find(LineTableOffset);
382 if (Iter != StmtListToDie.end()) {
383 ++NumDebugLineErrors;
384 OS << "error: two compile unit DIEs, "
385 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000386 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000387 << ", have the same DW_AT_stmt_list section offset:\n";
388 Iter->second.dump(OS, 0);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000389 Die.dump(OS, 0);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000390 OS << '\n';
391 // Already verified this line table before, no need to do it again.
392 continue;
393 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000394 StmtListToDie[LineTableOffset] = Die;
395 }
396}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000397
Greg Claytonc5b2d562017-05-03 18:25:46 +0000398void DWARFVerifier::verifyDebugLineRows() {
399 for (const auto &CU : DCtx.compile_units()) {
400 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000401 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000402 // If there is no line table we will have created an error in the
403 // .debug_info verifier or in verifyDebugLineStmtOffsets().
404 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000405 continue;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000406 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
407 uint64_t PrevAddress = 0;
408 uint32_t RowIndex = 0;
409 for (const auto &Row : LineTable->Rows) {
410 if (Row.Address < PrevAddress) {
411 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000412 OS << "error: .debug_line["
413 << format("0x%08" PRIx32,
414 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000415 << "] row[" << RowIndex
416 << "] decreases in address from previous row:\n";
417
418 DWARFDebugLine::Row::dumpTableHeader(OS);
419 if (RowIndex > 0)
420 LineTable->Rows[RowIndex - 1].dump(OS);
421 Row.dump(OS);
422 OS << '\n';
423 }
424
425 if (Row.File > MaxFileIndex) {
426 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000427 OS << "error: .debug_line["
428 << format("0x%08" PRIx32,
429 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000430 << "][" << RowIndex << "] has invalid file index " << Row.File
431 << " (valid values are [1," << MaxFileIndex << "]):\n";
432 DWARFDebugLine::Row::dumpTableHeader(OS);
433 Row.dump(OS);
434 OS << '\n';
435 }
436 if (Row.EndSequence)
437 PrevAddress = 0;
438 else
439 PrevAddress = Row.Address;
440 ++RowIndex;
441 }
442 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000443}
444
445bool DWARFVerifier::handleDebugLine() {
446 NumDebugLineErrors = 0;
447 OS << "Verifying .debug_line...\n";
448 verifyDebugLineStmtOffsets();
449 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000450 return NumDebugLineErrors == 0;
451}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000452
453bool DWARFVerifier::handleAppleNames() {
454 NumAppleNamesErrors = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000455 const DWARFObject &D = DCtx.getDWARFObj();
456 DWARFDataExtractor AppleNamesSection(D, D.getAppleNamesSection(),
Paul Robinson17536b92017-06-29 16:52:08 +0000457 DCtx.isLittleEndian(), 0);
Rafael Espindolac398e672017-07-19 22:27:28 +0000458 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
Paul Robinson17536b92017-06-29 16:52:08 +0000459 DWARFAcceleratorTable AppleNames(AppleNamesSection, StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000460
461 if (!AppleNames.extract()) {
Spyridoula Gravani32614fc2017-06-16 22:03:21 +0000462 return true;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000463 }
464
Spyridoula Gravani32614fc2017-06-16 22:03:21 +0000465 OS << "Verifying .apple_names...\n";
466
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000467 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000468 uint32_t NumBuckets = AppleNames.getNumBuckets();
469 uint32_t NumHashes = AppleNames.getNumHashes();
470
471 uint32_t BucketsOffset =
472 AppleNames.getSizeHdr() + AppleNames.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000473 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
474 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000475
476 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
477 uint32_t HashIdx = AppleNamesSection.getU32(&BucketsOffset);
478 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000479 OS << format("error: Bucket[%d] has invalid hash index: %u\n", BucketIdx,
480 HashIdx);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000481 ++NumAppleNamesErrors;
482 }
483 }
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000484
485 uint32_t NumAtoms = AppleNames.getAtomsDesc().size();
486 if (NumAtoms == 0) {
487 OS << "error: no atoms; failed to read HashData\n";
488 ++NumAppleNamesErrors;
489 return false;
490 }
491
492 if (!AppleNames.validateForms()) {
493 OS << "error: unsupported form; failed to read HashData\n";
494 ++NumAppleNamesErrors;
495 return false;
496 }
497
498 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
499 uint32_t HashOffset = HashesBase + 4 * HashIdx;
500 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
501 uint32_t Hash = AppleNamesSection.getU32(&HashOffset);
502 uint32_t HashDataOffset = AppleNamesSection.getU32(&DataOffset);
503 if (!AppleNamesSection.isValidOffsetForDataOfSize(HashDataOffset,
504 sizeof(uint64_t))) {
505 OS << format("error: Hash[%d] has invalid HashData offset: 0x%08x\n",
506 HashIdx, HashDataOffset);
507 ++NumAppleNamesErrors;
508 }
509
510 uint32_t StrpOffset;
511 uint32_t StringOffset;
512 uint32_t StringCount = 0;
513 uint32_t DieOffset = dwarf::DW_INVALID_OFFSET;
514
515 while ((StrpOffset = AppleNamesSection.getU32(&HashDataOffset)) != 0) {
516 const uint32_t NumHashDataObjects =
517 AppleNamesSection.getU32(&HashDataOffset);
518 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
519 ++HashDataIdx) {
520 DieOffset = AppleNames.readAtoms(HashDataOffset);
521 if (!DCtx.getDIEForOffset(DieOffset)) {
522 const uint32_t BucketIdx =
523 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
524 StringOffset = StrpOffset;
525 const char *Name = StrData.getCStr(&StringOffset);
526 if (!Name)
527 Name = "<NULL>";
528
529 OS << format(
530 "error: .apple_names Bucket[%d] Hash[%d] = 0x%08x "
531 "Str[%u] = 0x%08x "
532 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
533 BucketIdx, HashIdx, Hash, StringCount, StrpOffset, HashDataIdx,
534 DieOffset, Name);
535
536 ++NumAppleNamesErrors;
537 }
538 }
539 ++StringCount;
540 }
541 }
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000542 return NumAppleNamesErrors == 0;
543}