blob: 50fc45a9e376710782b2c51463f390b0744fd8c3 [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
105bool DWARFVerifier::handleDebugInfo() {
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000106 OS << "Verifying .debug_info Unit Header Chain...\n";
107
Rafael Espindolac398e672017-07-19 22:27:28 +0000108 const DWARFObject &DObj = DCtx.getDWARFObj();
109 DWARFDataExtractor DebugInfoData(DObj, DObj.getInfoSection(),
110 DCtx.isLittleEndian(), 0);
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000111 uint32_t NumDebugInfoErrors = 0;
112 uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
113 uint8_t UnitType = 0;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000114 bool isUnitDWARF64 = false;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000115 bool isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000116 bool hasDIE = DebugInfoData.isValidOffset(Offset);
117 while (hasDIE) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000118 OffsetStart = Offset;
119 if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
120 isUnitDWARF64)) {
121 isHeaderChainValid = false;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000122 if (isUnitDWARF64)
123 break;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000124 } else {
125 std::unique_ptr<DWARFUnit> Unit;
126 switch (UnitType) {
127 case dwarf::DW_UT_type:
128 case dwarf::DW_UT_split_type: {
129 DWARFUnitSection<DWARFTypeUnit> TUSection{};
130 Unit.reset(new DWARFTypeUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000131 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
132 &DObj.getRangeSection(), DObj.getStringSection(),
133 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
134 DObj.getLineSection(), DCtx.isLittleEndian(), false, TUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000135 nullptr));
136 break;
137 }
138 case dwarf::DW_UT_skeleton:
139 case dwarf::DW_UT_split_compile:
140 case dwarf::DW_UT_compile:
141 case dwarf::DW_UT_partial:
142 // UnitType = 0 means that we are
143 // verifying a compile unit in DWARF v4.
144 case 0: {
145 DWARFUnitSection<DWARFCompileUnit> CUSection{};
146 Unit.reset(new DWARFCompileUnit(
Rafael Espindolac398e672017-07-19 22:27:28 +0000147 DCtx, DObj.getInfoSection(), DCtx.getDebugAbbrev(),
148 &DObj.getRangeSection(), DObj.getStringSection(),
149 DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
150 DObj.getLineSection(), DCtx.isLittleEndian(), false, CUSection,
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000151 nullptr));
152 break;
153 }
154 default: { llvm_unreachable("Invalid UnitType."); }
155 }
156 Unit->extract(DebugInfoData, &OffsetStart);
157 if (!verifyUnitContents(*Unit))
158 ++NumDebugInfoErrors;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000159 }
160 hasDIE = DebugInfoData.isValidOffset(Offset);
161 ++UnitIdx;
162 }
163 if (UnitIdx == 0 && !hasDIE) {
164 OS << "Warning: .debug_info is empty.\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000165 isHeaderChainValid = true;
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000166 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000167 NumDebugInfoErrors += verifyDebugInfoReferences();
168 return (isHeaderChainValid && NumDebugInfoErrors == 0);
Spyridoula Gravani890eedc2017-07-13 23:25:24 +0000169}
170
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000171unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
172 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000173 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000174 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000175 const auto Attr = AttrValue.Attr;
176 switch (Attr) {
177 case DW_AT_ranges:
178 // Make sure the offset in the DW_AT_ranges attribute is valid.
179 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000180 if (*SectionOffset >= DObj.getRangeSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000181 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000182 OS << "error: DW_AT_ranges offset is beyond .debug_ranges "
183 "bounds:\n";
184 Die.dump(OS, 0);
185 OS << "\n";
186 }
187 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000188 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000189 OS << "error: DIE has invalid DW_AT_ranges encoding:\n";
190 Die.dump(OS, 0);
191 OS << "\n";
192 }
193 break;
194 case DW_AT_stmt_list:
195 // Make sure the offset in the DW_AT_stmt_list attribute is valid.
196 if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000197 if (*SectionOffset >= DObj.getLineSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000198 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000199 OS << "error: DW_AT_stmt_list offset is beyond .debug_line "
200 "bounds: "
201 << format("0x%08" PRIx32, *SectionOffset) << "\n";
202 Die.dump(OS, 0);
203 OS << "\n";
204 }
205 } else {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000206 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000207 OS << "error: DIE has invalid DW_AT_stmt_list encoding:\n";
208 Die.dump(OS, 0);
209 OS << "\n";
210 }
211 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000212
Greg Claytonc5b2d562017-05-03 18:25:46 +0000213 default:
214 break;
215 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000216 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000217}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000218
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000219unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
220 DWARFAttribute &AttrValue) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000221 const DWARFObject &DObj = DCtx.getDWARFObj();
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000222 unsigned NumErrors = 0;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000223 const auto Form = AttrValue.Value.getForm();
224 switch (Form) {
225 case DW_FORM_ref1:
226 case DW_FORM_ref2:
227 case DW_FORM_ref4:
228 case DW_FORM_ref8:
229 case DW_FORM_ref_udata: {
230 // Verify all CU relative references are valid CU offsets.
231 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
232 assert(RefVal);
233 if (RefVal) {
234 auto DieCU = Die.getDwarfUnit();
235 auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
236 auto CUOffset = AttrValue.Value.getRawUValue();
237 if (CUOffset >= CUSize) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000238 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000239 OS << "error: " << FormEncodingString(Form) << " CU offset "
240 << format("0x%08" PRIx32, CUOffset)
241 << " is invalid (must be less than CU size of "
242 << format("0x%08" PRIx32, CUSize) << "):\n";
243 Die.dump(OS, 0);
244 OS << "\n";
245 } else {
246 // Valid reference, but we will verify it points to an actual
247 // DIE later.
248 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
Greg Claytonb8c162b2017-05-03 16:02:29 +0000249 }
250 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000251 break;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000252 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000253 case DW_FORM_ref_addr: {
254 // Verify all absolute DIE references have valid offsets in the
255 // .debug_info section.
256 Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
257 assert(RefVal);
258 if (RefVal) {
Rafael Espindolac398e672017-07-19 22:27:28 +0000259 if (*RefVal >= DObj.getInfoSection().Data.size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000260 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000261 OS << "error: DW_FORM_ref_addr offset beyond .debug_info "
262 "bounds:\n";
263 Die.dump(OS, 0);
264 OS << "\n";
265 } else {
266 // Valid reference, but we will verify it points to an actual
267 // DIE later.
268 ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
269 }
270 }
271 break;
272 }
273 case DW_FORM_strp: {
274 auto SecOffset = AttrValue.Value.getAsSectionOffset();
275 assert(SecOffset); // DW_FORM_strp is a section offset.
Rafael Espindolac398e672017-07-19 22:27:28 +0000276 if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000277 ++NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000278 OS << "error: DW_FORM_strp offset beyond .debug_str bounds:\n";
279 Die.dump(OS, 0);
280 OS << "\n";
281 }
282 break;
283 }
284 default:
285 break;
286 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000287 return NumErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000288}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000289
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000290unsigned DWARFVerifier::verifyDebugInfoReferences() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000291 // Take all references and make sure they point to an actual DIE by
292 // getting the DIE by offset and emitting an error
293 OS << "Verifying .debug_info references...\n";
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000294 unsigned NumErrors = 0;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000295 for (auto Pair : ReferenceToDIEOffsets) {
296 auto Die = DCtx.getDIEForOffset(Pair.first);
297 if (Die)
298 continue;
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000299 ++NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000300 OS << "error: invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
301 << ". Offset is in between DIEs:\n";
302 for (auto Offset : Pair.second) {
303 auto ReferencingDie = DCtx.getDIEForOffset(Offset);
304 ReferencingDie.dump(OS, 0);
305 OS << "\n";
306 }
307 OS << "\n";
308 }
Spyridoula Gravanif6bd788d2017-07-18 01:00:26 +0000309 return NumErrors;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000310}
311
Greg Claytonc5b2d562017-05-03 18:25:46 +0000312void DWARFVerifier::verifyDebugLineStmtOffsets() {
Greg Claytonb8c162b2017-05-03 16:02:29 +0000313 std::map<uint64_t, DWARFDie> StmtListToDie;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000314 for (const auto &CU : DCtx.compile_units()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000315 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000316 // Get the attribute value as a section offset. No need to produce an
317 // error here if the encoding isn't correct because we validate this in
318 // the .debug_info verifier.
Greg Claytonc5b2d562017-05-03 18:25:46 +0000319 auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
Greg Claytonb8c162b2017-05-03 16:02:29 +0000320 if (!StmtSectionOffset)
321 continue;
322 const uint32_t LineTableOffset = *StmtSectionOffset;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000323 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Rafael Espindolac398e672017-07-19 22:27:28 +0000324 if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
Greg Claytonc5b2d562017-05-03 18:25:46 +0000325 if (!LineTable) {
326 ++NumDebugLineErrors;
327 OS << "error: .debug_line[" << format("0x%08" PRIx32, LineTableOffset)
328 << "] was not able to be parsed for CU:\n";
329 Die.dump(OS, 0);
330 OS << '\n';
331 continue;
332 }
333 } else {
334 // Make sure we don't get a valid line table back if the offset is wrong.
335 assert(LineTable == nullptr);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000336 // Skip this line table as it isn't valid. No need to create an error
337 // here because we validate this in the .debug_info verifier.
338 continue;
339 }
Greg Claytonb8c162b2017-05-03 16:02:29 +0000340 auto Iter = StmtListToDie.find(LineTableOffset);
341 if (Iter != StmtListToDie.end()) {
342 ++NumDebugLineErrors;
343 OS << "error: two compile unit DIEs, "
344 << format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
Greg Claytonc5b2d562017-05-03 18:25:46 +0000345 << format("0x%08" PRIx32, Die.getOffset())
Greg Claytonb8c162b2017-05-03 16:02:29 +0000346 << ", have the same DW_AT_stmt_list section offset:\n";
347 Iter->second.dump(OS, 0);
Greg Claytonc5b2d562017-05-03 18:25:46 +0000348 Die.dump(OS, 0);
Greg Claytonb8c162b2017-05-03 16:02:29 +0000349 OS << '\n';
350 // Already verified this line table before, no need to do it again.
351 continue;
352 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000353 StmtListToDie[LineTableOffset] = Die;
354 }
355}
Greg Claytonb8c162b2017-05-03 16:02:29 +0000356
Greg Claytonc5b2d562017-05-03 18:25:46 +0000357void DWARFVerifier::verifyDebugLineRows() {
358 for (const auto &CU : DCtx.compile_units()) {
359 auto Die = CU->getUnitDIE();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000360 auto LineTable = DCtx.getLineTableForUnit(CU.get());
Greg Claytonc5b2d562017-05-03 18:25:46 +0000361 // If there is no line table we will have created an error in the
362 // .debug_info verifier or in verifyDebugLineStmtOffsets().
363 if (!LineTable)
Greg Claytonb8c162b2017-05-03 16:02:29 +0000364 continue;
Greg Claytonb8c162b2017-05-03 16:02:29 +0000365 uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
366 uint64_t PrevAddress = 0;
367 uint32_t RowIndex = 0;
368 for (const auto &Row : LineTable->Rows) {
369 if (Row.Address < PrevAddress) {
370 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000371 OS << "error: .debug_line["
372 << format("0x%08" PRIx32,
373 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000374 << "] row[" << RowIndex
375 << "] decreases in address from previous row:\n";
376
377 DWARFDebugLine::Row::dumpTableHeader(OS);
378 if (RowIndex > 0)
379 LineTable->Rows[RowIndex - 1].dump(OS);
380 Row.dump(OS);
381 OS << '\n';
382 }
383
384 if (Row.File > MaxFileIndex) {
385 ++NumDebugLineErrors;
Greg Claytonc5b2d562017-05-03 18:25:46 +0000386 OS << "error: .debug_line["
387 << format("0x%08" PRIx32,
388 *toSectionOffset(Die.find(DW_AT_stmt_list)))
Greg Claytonb8c162b2017-05-03 16:02:29 +0000389 << "][" << RowIndex << "] has invalid file index " << Row.File
390 << " (valid values are [1," << MaxFileIndex << "]):\n";
391 DWARFDebugLine::Row::dumpTableHeader(OS);
392 Row.dump(OS);
393 OS << '\n';
394 }
395 if (Row.EndSequence)
396 PrevAddress = 0;
397 else
398 PrevAddress = Row.Address;
399 ++RowIndex;
400 }
401 }
Greg Claytonc5b2d562017-05-03 18:25:46 +0000402}
403
404bool DWARFVerifier::handleDebugLine() {
405 NumDebugLineErrors = 0;
406 OS << "Verifying .debug_line...\n";
407 verifyDebugLineStmtOffsets();
408 verifyDebugLineRows();
Greg Claytonb8c162b2017-05-03 16:02:29 +0000409 return NumDebugLineErrors == 0;
410}
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000411
412bool DWARFVerifier::handleAppleNames() {
413 NumAppleNamesErrors = 0;
Rafael Espindolac398e672017-07-19 22:27:28 +0000414 const DWARFObject &D = DCtx.getDWARFObj();
415 DWARFDataExtractor AppleNamesSection(D, D.getAppleNamesSection(),
Paul Robinson17536b92017-06-29 16:52:08 +0000416 DCtx.isLittleEndian(), 0);
Rafael Espindolac398e672017-07-19 22:27:28 +0000417 DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
Paul Robinson17536b92017-06-29 16:52:08 +0000418 DWARFAcceleratorTable AppleNames(AppleNamesSection, StrData);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000419
420 if (!AppleNames.extract()) {
Spyridoula Gravani32614fc2017-06-16 22:03:21 +0000421 return true;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000422 }
423
Spyridoula Gravani32614fc2017-06-16 22:03:21 +0000424 OS << "Verifying .apple_names...\n";
425
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000426 // Verify that all buckets have a valid hash index or are empty.
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000427 uint32_t NumBuckets = AppleNames.getNumBuckets();
428 uint32_t NumHashes = AppleNames.getNumHashes();
429
430 uint32_t BucketsOffset =
431 AppleNames.getSizeHdr() + AppleNames.getHeaderDataLength();
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000432 uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
433 uint32_t OffsetsBase = HashesBase + NumHashes * 4;
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000434
435 for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
436 uint32_t HashIdx = AppleNamesSection.getU32(&BucketsOffset);
437 if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000438 OS << format("error: Bucket[%d] has invalid hash index: %u\n", BucketIdx,
439 HashIdx);
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000440 ++NumAppleNamesErrors;
441 }
442 }
Spyridoula Gravani837c1102017-06-29 20:13:05 +0000443
444 uint32_t NumAtoms = AppleNames.getAtomsDesc().size();
445 if (NumAtoms == 0) {
446 OS << "error: no atoms; failed to read HashData\n";
447 ++NumAppleNamesErrors;
448 return false;
449 }
450
451 if (!AppleNames.validateForms()) {
452 OS << "error: unsupported form; failed to read HashData\n";
453 ++NumAppleNamesErrors;
454 return false;
455 }
456
457 for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
458 uint32_t HashOffset = HashesBase + 4 * HashIdx;
459 uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
460 uint32_t Hash = AppleNamesSection.getU32(&HashOffset);
461 uint32_t HashDataOffset = AppleNamesSection.getU32(&DataOffset);
462 if (!AppleNamesSection.isValidOffsetForDataOfSize(HashDataOffset,
463 sizeof(uint64_t))) {
464 OS << format("error: Hash[%d] has invalid HashData offset: 0x%08x\n",
465 HashIdx, HashDataOffset);
466 ++NumAppleNamesErrors;
467 }
468
469 uint32_t StrpOffset;
470 uint32_t StringOffset;
471 uint32_t StringCount = 0;
472 uint32_t DieOffset = dwarf::DW_INVALID_OFFSET;
473
474 while ((StrpOffset = AppleNamesSection.getU32(&HashDataOffset)) != 0) {
475 const uint32_t NumHashDataObjects =
476 AppleNamesSection.getU32(&HashDataOffset);
477 for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
478 ++HashDataIdx) {
479 DieOffset = AppleNames.readAtoms(HashDataOffset);
480 if (!DCtx.getDIEForOffset(DieOffset)) {
481 const uint32_t BucketIdx =
482 NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
483 StringOffset = StrpOffset;
484 const char *Name = StrData.getCStr(&StringOffset);
485 if (!Name)
486 Name = "<NULL>";
487
488 OS << format(
489 "error: .apple_names Bucket[%d] Hash[%d] = 0x%08x "
490 "Str[%u] = 0x%08x "
491 "DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
492 BucketIdx, HashIdx, Hash, StringCount, StrpOffset, HashDataIdx,
493 DieOffset, Name);
494
495 ++NumAppleNamesErrors;
496 }
497 }
498 ++StringCount;
499 }
500 }
Spyridoula Gravanie41823b2017-06-14 00:17:55 +0000501 return NumAppleNamesErrors == 0;
502}