2017-05-04 00:02:29 +08:00
|
|
|
//===- DWARFVerifier.cpp --------------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-05-04 00:02:29 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFVerifier.h"
|
2018-03-22 22:50:44 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2017-05-04 00:02:29 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFContext.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDebugLine.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFDie.h"
|
2017-10-27 18:42:04 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFExpression.h"
|
2017-05-04 00:02:29 +08:00
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
|
|
|
|
#include "llvm/DebugInfo/DWARF/DWARFSection.h"
|
2018-03-16 18:02:16 +08:00
|
|
|
#include "llvm/Support/DJB.h"
|
2017-10-27 18:42:04 +08:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
2018-03-09 17:56:24 +08:00
|
|
|
#include "llvm/Support/WithColor.h"
|
2017-05-04 00:02:29 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace dwarf;
|
|
|
|
using namespace object;
|
|
|
|
|
2017-09-14 19:33:42 +08:00
|
|
|
DWARFVerifier::DieRangeInfo::address_range_iterator
|
|
|
|
DWARFVerifier::DieRangeInfo::insert(const DWARFAddressRange &R) {
|
|
|
|
auto Begin = Ranges.begin();
|
|
|
|
auto End = Ranges.end();
|
|
|
|
auto Pos = std::lower_bound(Begin, End, R);
|
|
|
|
|
|
|
|
if (Pos != End) {
|
|
|
|
if (Pos->intersects(R))
|
|
|
|
return Pos;
|
|
|
|
if (Pos != Begin) {
|
|
|
|
auto Iter = Pos - 1;
|
|
|
|
if (Iter->intersects(R))
|
|
|
|
return Iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ranges.insert(Pos, R);
|
|
|
|
return Ranges.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
DWARFVerifier::DieRangeInfo::die_range_info_iterator
|
|
|
|
DWARFVerifier::DieRangeInfo::insert(const DieRangeInfo &RI) {
|
|
|
|
auto End = Children.end();
|
|
|
|
auto Iter = Children.begin();
|
|
|
|
while (Iter != End) {
|
|
|
|
if (Iter->intersects(RI))
|
|
|
|
return Iter;
|
|
|
|
++Iter;
|
|
|
|
}
|
|
|
|
Children.insert(RI);
|
|
|
|
return Children.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFVerifier::DieRangeInfo::contains(const DieRangeInfo &RHS) const {
|
|
|
|
// Both list of ranges are sorted so we can make this fast.
|
|
|
|
|
|
|
|
if (Ranges.empty() || RHS.Ranges.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Since the ranges are sorted we can advance where we start searching with
|
|
|
|
// this object's ranges as we traverse RHS.Ranges.
|
|
|
|
auto End = Ranges.end();
|
|
|
|
auto Iter = findRange(RHS.Ranges.front());
|
|
|
|
|
|
|
|
// Now linearly walk the ranges in this object and see if they contain each
|
|
|
|
// ranges from RHS.Ranges.
|
|
|
|
for (const auto &R : RHS.Ranges) {
|
|
|
|
while (Iter != End) {
|
|
|
|
if (Iter->contains(R))
|
|
|
|
break;
|
|
|
|
++Iter;
|
|
|
|
}
|
|
|
|
if (Iter == End)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFVerifier::DieRangeInfo::intersects(const DieRangeInfo &RHS) const {
|
|
|
|
if (Ranges.empty() || RHS.Ranges.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto End = Ranges.end();
|
|
|
|
auto Iter = findRange(RHS.Ranges.front());
|
|
|
|
for (const auto &R : RHS.Ranges) {
|
2018-09-17 22:23:47 +08:00
|
|
|
if (Iter == End)
|
2017-09-15 01:46:23 +08:00
|
|
|
return false;
|
2017-09-14 19:33:42 +08:00
|
|
|
if (R.HighPC <= Iter->LowPC)
|
|
|
|
continue;
|
|
|
|
while (Iter != End) {
|
|
|
|
if (Iter->intersects(R))
|
|
|
|
return true;
|
|
|
|
++Iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-14 07:25:24 +08:00
|
|
|
bool DWARFVerifier::verifyUnitHeader(const DWARFDataExtractor DebugInfoData,
|
2017-07-18 09:00:26 +08:00
|
|
|
uint32_t *Offset, unsigned UnitIndex,
|
|
|
|
uint8_t &UnitType, bool &isUnitDWARF64) {
|
2017-07-14 07:25:24 +08:00
|
|
|
uint32_t AbbrOffset, Length;
|
2017-07-18 09:00:26 +08:00
|
|
|
uint8_t AddrSize = 0;
|
2017-07-14 07:25:24 +08:00
|
|
|
uint16_t Version;
|
|
|
|
bool Success = true;
|
|
|
|
|
|
|
|
bool ValidLength = false;
|
|
|
|
bool ValidVersion = false;
|
|
|
|
bool ValidAddrSize = false;
|
|
|
|
bool ValidType = true;
|
|
|
|
bool ValidAbbrevOffset = true;
|
|
|
|
|
|
|
|
uint32_t OffsetStart = *Offset;
|
|
|
|
Length = DebugInfoData.getU32(Offset);
|
|
|
|
if (Length == UINT32_MAX) {
|
|
|
|
isUnitDWARF64 = true;
|
|
|
|
OS << format(
|
|
|
|
"Unit[%d] is in 64-bit DWARF format; cannot verify from this point.\n",
|
|
|
|
UnitIndex);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Version = DebugInfoData.getU16(Offset);
|
|
|
|
|
|
|
|
if (Version >= 5) {
|
|
|
|
UnitType = DebugInfoData.getU8(Offset);
|
|
|
|
AddrSize = DebugInfoData.getU8(Offset);
|
|
|
|
AbbrOffset = DebugInfoData.getU32(Offset);
|
2017-10-07 06:27:31 +08:00
|
|
|
ValidType = dwarf::isUnitType(UnitType);
|
2017-07-14 07:25:24 +08:00
|
|
|
} else {
|
2017-07-18 09:00:26 +08:00
|
|
|
UnitType = 0;
|
2017-07-14 07:25:24 +08:00
|
|
|
AbbrOffset = DebugInfoData.getU32(Offset);
|
|
|
|
AddrSize = DebugInfoData.getU8(Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DCtx.getDebugAbbrev()->getAbbreviationDeclarationSet(AbbrOffset))
|
|
|
|
ValidAbbrevOffset = false;
|
|
|
|
|
|
|
|
ValidLength = DebugInfoData.isValidOffset(OffsetStart + Length + 3);
|
|
|
|
ValidVersion = DWARFContext::isSupportedVersion(Version);
|
|
|
|
ValidAddrSize = AddrSize == 4 || AddrSize == 8;
|
|
|
|
if (!ValidLength || !ValidVersion || !ValidAddrSize || !ValidAbbrevOffset ||
|
|
|
|
!ValidType) {
|
|
|
|
Success = false;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << format("Units[%d] - start offset: 0x%08x \n", UnitIndex,
|
|
|
|
OffsetStart);
|
2017-07-14 07:25:24 +08:00
|
|
|
if (!ValidLength)
|
2017-09-29 17:33:31 +08:00
|
|
|
note() << "The length for this unit is too "
|
2018-09-17 22:23:47 +08:00
|
|
|
"large for the .debug_info provided.\n";
|
2017-07-14 07:25:24 +08:00
|
|
|
if (!ValidVersion)
|
2017-09-29 17:33:31 +08:00
|
|
|
note() << "The 16 bit unit header version is not valid.\n";
|
2017-07-14 07:25:24 +08:00
|
|
|
if (!ValidType)
|
2017-09-29 17:33:31 +08:00
|
|
|
note() << "The unit type encoding is not valid.\n";
|
2017-07-14 07:25:24 +08:00
|
|
|
if (!ValidAbbrevOffset)
|
2017-09-29 17:33:31 +08:00
|
|
|
note() << "The offset into the .debug_abbrev section is "
|
2018-09-17 22:23:47 +08:00
|
|
|
"not valid.\n";
|
2017-07-14 07:25:24 +08:00
|
|
|
if (!ValidAddrSize)
|
2017-09-29 17:33:31 +08:00
|
|
|
note() << "The address size is unsupported.\n";
|
2017-07-14 07:25:24 +08:00
|
|
|
}
|
|
|
|
*Offset = OffsetStart + Length + 4;
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:23:47 +08:00
|
|
|
unsigned DWARFVerifier::verifyUnitContents(DWARFUnit &Unit) {
|
2018-08-09 07:50:22 +08:00
|
|
|
unsigned NumUnitErrors = 0;
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned NumDies = Unit.getNumDIEs();
|
|
|
|
for (unsigned I = 0; I < NumDies; ++I) {
|
|
|
|
auto Die = Unit.getDIEAtIndex(I);
|
2018-09-21 15:50:21 +08:00
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
if (Die.getTag() == DW_TAG_null)
|
|
|
|
continue;
|
2018-09-21 15:50:21 +08:00
|
|
|
|
|
|
|
bool HasTypeAttr = false;
|
2017-07-18 09:00:26 +08:00
|
|
|
for (auto AttrValue : Die.attributes()) {
|
|
|
|
NumUnitErrors += verifyDebugInfoAttribute(Die, AttrValue);
|
|
|
|
NumUnitErrors += verifyDebugInfoForm(Die, AttrValue);
|
2018-09-21 15:50:21 +08:00
|
|
|
HasTypeAttr |= (AttrValue.Attr == DW_AT_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasTypeAttr && (Die.getTag() == DW_TAG_formal_parameter ||
|
|
|
|
Die.getTag() == DW_TAG_variable ||
|
|
|
|
Die.getTag() == DW_TAG_array_type)) {
|
|
|
|
error() << "DIE with tag " << TagString(Die.getTag())
|
|
|
|
<< " is missing type attribute:\n";
|
|
|
|
dump(Die) << '\n';
|
|
|
|
NumUnitErrors++;
|
2017-07-18 09:00:26 +08:00
|
|
|
}
|
2018-10-06 04:37:17 +08:00
|
|
|
NumUnitErrors += verifyDebugInfoCallSite(Die);
|
2017-07-18 09:00:26 +08:00
|
|
|
}
|
2017-09-14 19:33:42 +08:00
|
|
|
|
2017-10-07 06:27:31 +08:00
|
|
|
DWARFDie Die = Unit.getUnitDIE(/* ExtractUnitDIEOnly = */ false);
|
|
|
|
if (!Die) {
|
|
|
|
error() << "Compilation unit without DIE.\n";
|
|
|
|
NumUnitErrors++;
|
2018-08-09 07:50:22 +08:00
|
|
|
return NumUnitErrors;
|
2017-10-07 06:27:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!dwarf::isUnitType(Die.getTag())) {
|
|
|
|
error() << "Compilation unit root DIE is not a unit DIE: "
|
|
|
|
<< dwarf::TagString(Die.getTag()) << ".\n";
|
2017-09-28 23:57:50 +08:00
|
|
|
NumUnitErrors++;
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:23:47 +08:00
|
|
|
uint8_t UnitType = Unit.getUnitType();
|
|
|
|
if (!DWARFUnit::isMatchingUnitTypeAndTag(UnitType, Die.getTag())) {
|
2017-10-07 06:27:31 +08:00
|
|
|
error() << "Compilation unit type (" << dwarf::UnitTypeString(UnitType)
|
|
|
|
<< ") and root DIE (" << dwarf::TagString(Die.getTag())
|
|
|
|
<< ") do not match.\n";
|
|
|
|
NumUnitErrors++;
|
|
|
|
}
|
|
|
|
|
|
|
|
DieRangeInfo RI;
|
|
|
|
NumUnitErrors += verifyDieRanges(Die, RI);
|
|
|
|
|
2018-08-09 07:50:22 +08:00
|
|
|
return NumUnitErrors;
|
2017-07-18 09:00:26 +08:00
|
|
|
}
|
|
|
|
|
2018-10-06 04:37:17 +08:00
|
|
|
unsigned DWARFVerifier::verifyDebugInfoCallSite(const DWARFDie &Die) {
|
|
|
|
if (Die.getTag() != DW_TAG_call_site)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
DWARFDie Curr = Die.getParent();
|
|
|
|
for (; Curr.isValid() && !Curr.isSubprogramDIE(); Curr = Die.getParent()) {
|
|
|
|
if (Curr.getTag() == DW_TAG_inlined_subroutine) {
|
|
|
|
error() << "Call site entry nested within inlined subroutine:";
|
|
|
|
Curr.dump(OS);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Curr.isValid()) {
|
|
|
|
error() << "Call site entry not nested within a valid subprogram:";
|
|
|
|
Die.dump(OS);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<DWARFFormValue> CallAttr =
|
|
|
|
Curr.find({DW_AT_call_all_calls, DW_AT_call_all_source_calls,
|
|
|
|
DW_AT_call_all_tail_calls});
|
|
|
|
if (!CallAttr) {
|
|
|
|
error() << "Subprogram with call site entry has no DW_AT_call attribute:";
|
|
|
|
Curr.dump(OS);
|
|
|
|
Die.dump(OS, /*indent*/ 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-21 08:51:32 +08:00
|
|
|
unsigned DWARFVerifier::verifyAbbrevSection(const DWARFDebugAbbrev *Abbrev) {
|
2017-07-20 10:06:52 +08:00
|
|
|
unsigned NumErrors = 0;
|
|
|
|
if (Abbrev) {
|
|
|
|
const DWARFAbbreviationDeclarationSet *AbbrDecls =
|
|
|
|
Abbrev->getAbbreviationDeclarationSet(0);
|
|
|
|
for (auto AbbrDecl : *AbbrDecls) {
|
|
|
|
SmallDenseSet<uint16_t> AttributeSet;
|
|
|
|
for (auto Attribute : AbbrDecl.attributes()) {
|
|
|
|
auto Result = AttributeSet.insert(Attribute.Attr);
|
|
|
|
if (!Result.second) {
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "Abbreviation declaration contains multiple "
|
|
|
|
<< AttributeString(Attribute.Attr) << " attributes.\n";
|
2017-07-21 08:51:32 +08:00
|
|
|
AbbrDecl.dump(OS);
|
2017-07-20 10:06:52 +08:00
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 08:51:32 +08:00
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFVerifier::handleDebugAbbrev() {
|
|
|
|
OS << "Verifying .debug_abbrev...\n";
|
|
|
|
|
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
|
|
|
bool noDebugAbbrev = DObj.getAbbrevSection().empty();
|
|
|
|
bool noDebugAbbrevDWO = DObj.getAbbrevDWOSection().empty();
|
|
|
|
|
|
|
|
if (noDebugAbbrev && noDebugAbbrevDWO) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
if (!noDebugAbbrev)
|
|
|
|
NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrev());
|
|
|
|
|
|
|
|
if (!noDebugAbbrevDWO)
|
|
|
|
NumErrors += verifyAbbrevSection(DCtx.getDebugAbbrevDWO());
|
2017-07-20 10:06:52 +08:00
|
|
|
return NumErrors == 0;
|
|
|
|
}
|
|
|
|
|
2018-08-09 07:50:22 +08:00
|
|
|
unsigned DWARFVerifier::verifyUnitSection(const DWARFSection &S,
|
|
|
|
DWARFSectionKind SectionKind) {
|
2017-07-20 06:27:28 +08:00
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
2018-08-09 07:50:22 +08:00
|
|
|
DWARFDataExtractor DebugInfoData(DObj, S, DCtx.isLittleEndian(), 0);
|
|
|
|
unsigned NumDebugInfoErrors = 0;
|
2017-07-18 09:00:26 +08:00
|
|
|
uint32_t OffsetStart = 0, Offset = 0, UnitIdx = 0;
|
|
|
|
uint8_t UnitType = 0;
|
2017-07-14 07:25:24 +08:00
|
|
|
bool isUnitDWARF64 = false;
|
2017-07-18 09:00:26 +08:00
|
|
|
bool isHeaderChainValid = true;
|
2017-07-14 07:25:24 +08:00
|
|
|
bool hasDIE = DebugInfoData.isValidOffset(Offset);
|
2018-09-21 15:49:29 +08:00
|
|
|
DWARFUnitVector TypeUnitVector;
|
|
|
|
DWARFUnitVector CompileUnitVector;
|
2017-07-14 07:25:24 +08:00
|
|
|
while (hasDIE) {
|
2017-07-18 09:00:26 +08:00
|
|
|
OffsetStart = Offset;
|
|
|
|
if (!verifyUnitHeader(DebugInfoData, &Offset, UnitIdx, UnitType,
|
|
|
|
isUnitDWARF64)) {
|
|
|
|
isHeaderChainValid = false;
|
2017-07-14 07:25:24 +08:00
|
|
|
if (isUnitDWARF64)
|
|
|
|
break;
|
2017-07-18 09:00:26 +08:00
|
|
|
} else {
|
2018-05-15 04:32:31 +08:00
|
|
|
DWARFUnitHeader Header;
|
2018-08-09 07:50:22 +08:00
|
|
|
Header.extract(DCtx, DebugInfoData, &OffsetStart, SectionKind);
|
2018-09-21 15:49:29 +08:00
|
|
|
DWARFUnit *Unit;
|
2017-07-18 09:00:26 +08:00
|
|
|
switch (UnitType) {
|
|
|
|
case dwarf::DW_UT_type:
|
|
|
|
case dwarf::DW_UT_split_type: {
|
2018-09-21 15:49:29 +08:00
|
|
|
Unit = TypeUnitVector.addUnit(llvm::make_unique<DWARFTypeUnit>(
|
2018-08-09 07:50:22 +08:00
|
|
|
DCtx, S, Header, DCtx.getDebugAbbrev(), &DObj.getRangeSection(),
|
2018-10-20 03:23:16 +08:00
|
|
|
&DObj.getLocSection(), DObj.getStringSection(),
|
|
|
|
DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
|
|
|
|
DObj.getLineSection(), DCtx.isLittleEndian(), false,
|
|
|
|
TypeUnitVector));
|
2017-07-18 09:00:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case dwarf::DW_UT_skeleton:
|
|
|
|
case dwarf::DW_UT_split_compile:
|
|
|
|
case dwarf::DW_UT_compile:
|
|
|
|
case dwarf::DW_UT_partial:
|
2018-09-17 22:23:47 +08:00
|
|
|
// UnitType = 0 means that we are verifying a compile unit in DWARF v4.
|
2017-07-18 09:00:26 +08:00
|
|
|
case 0: {
|
2018-09-21 15:49:29 +08:00
|
|
|
Unit = CompileUnitVector.addUnit(llvm::make_unique<DWARFCompileUnit>(
|
2018-08-09 07:50:22 +08:00
|
|
|
DCtx, S, Header, DCtx.getDebugAbbrev(), &DObj.getRangeSection(),
|
2018-10-20 03:23:16 +08:00
|
|
|
&DObj.getLocSection(), DObj.getStringSection(),
|
|
|
|
DObj.getStringOffsetSection(), &DObj.getAppleObjCSection(),
|
|
|
|
DObj.getLineSection(), DCtx.isLittleEndian(), false,
|
|
|
|
CompileUnitVector));
|
2017-07-18 09:00:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: { llvm_unreachable("Invalid UnitType."); }
|
|
|
|
}
|
2018-09-17 22:23:47 +08:00
|
|
|
NumDebugInfoErrors += verifyUnitContents(*Unit);
|
2017-07-14 07:25:24 +08:00
|
|
|
}
|
|
|
|
hasDIE = DebugInfoData.isValidOffset(Offset);
|
|
|
|
++UnitIdx;
|
|
|
|
}
|
|
|
|
if (UnitIdx == 0 && !hasDIE) {
|
2018-08-09 07:50:22 +08:00
|
|
|
warn() << "Section is empty.\n";
|
2017-07-18 09:00:26 +08:00
|
|
|
isHeaderChainValid = true;
|
2017-07-14 07:25:24 +08:00
|
|
|
}
|
2018-08-09 07:50:22 +08:00
|
|
|
if (!isHeaderChainValid)
|
|
|
|
++NumDebugInfoErrors;
|
2017-07-18 09:00:26 +08:00
|
|
|
NumDebugInfoErrors += verifyDebugInfoReferences();
|
2018-08-09 07:50:22 +08:00
|
|
|
return NumDebugInfoErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DWARFVerifier::handleDebugInfo() {
|
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
2018-11-08 05:39:09 +08:00
|
|
|
unsigned NumErrors = 0;
|
2018-08-09 07:50:22 +08:00
|
|
|
|
|
|
|
OS << "Verifying .debug_info Unit Header Chain...\n";
|
2018-11-08 05:39:09 +08:00
|
|
|
DObj.forEachInfoSections([&](const DWARFSection &S) {
|
|
|
|
NumErrors += verifyUnitSection(S, DW_SECT_INFO);
|
|
|
|
});
|
2018-08-09 07:50:22 +08:00
|
|
|
|
|
|
|
OS << "Verifying .debug_types Unit Header Chain...\n";
|
|
|
|
DObj.forEachTypesSections([&](const DWARFSection &S) {
|
2018-11-08 05:39:09 +08:00
|
|
|
NumErrors += verifyUnitSection(S, DW_SECT_TYPES);
|
2018-08-09 07:50:22 +08:00
|
|
|
});
|
2018-11-08 05:39:09 +08:00
|
|
|
return NumErrors == 0;
|
2017-07-14 07:25:24 +08:00
|
|
|
}
|
|
|
|
|
2017-09-14 19:33:42 +08:00
|
|
|
unsigned DWARFVerifier::verifyDieRanges(const DWARFDie &Die,
|
|
|
|
DieRangeInfo &ParentRI) {
|
2017-07-25 05:04:11 +08:00
|
|
|
unsigned NumErrors = 0;
|
2017-09-14 19:33:42 +08:00
|
|
|
|
|
|
|
if (!Die.isValid())
|
|
|
|
return NumErrors;
|
|
|
|
|
2018-06-21 06:56:37 +08:00
|
|
|
auto RangesOrError = Die.getAddressRanges();
|
|
|
|
if (!RangesOrError) {
|
|
|
|
// FIXME: Report the error.
|
|
|
|
++NumErrors;
|
|
|
|
llvm::consumeError(RangesOrError.takeError());
|
|
|
|
return NumErrors;
|
|
|
|
}
|
2017-09-14 19:33:42 +08:00
|
|
|
|
2018-06-21 06:56:37 +08:00
|
|
|
DWARFAddressRangesVector Ranges = RangesOrError.get();
|
2017-09-14 19:33:42 +08:00
|
|
|
// Build RI for this DIE and check that ranges within this DIE do not
|
|
|
|
// overlap.
|
|
|
|
DieRangeInfo RI(Die);
|
|
|
|
|
2018-10-29 06:30:48 +08:00
|
|
|
// TODO support object files better
|
|
|
|
//
|
|
|
|
// Some object file formats (i.e. non-MachO) support COMDAT. ELF in
|
|
|
|
// particular does so by placing each function into a section. The DWARF data
|
|
|
|
// for the function at that point uses a section relative DW_FORM_addrp for
|
|
|
|
// the DW_AT_low_pc and a DW_FORM_data4 for the offset as the DW_AT_high_pc.
|
|
|
|
// In such a case, when the Die is the CU, the ranges will overlap, and we
|
|
|
|
// will flag valid conflicting ranges as invalid.
|
|
|
|
//
|
|
|
|
// For such targets, we should read the ranges from the CU and partition them
|
|
|
|
// by the section id. The ranges within a particular section should be
|
|
|
|
// disjoint, although the ranges across sections may overlap. We would map
|
|
|
|
// the child die to the entity that it references and the section with which
|
|
|
|
// it is associated. The child would then be checked against the range
|
|
|
|
// information for the associated section.
|
|
|
|
//
|
|
|
|
// For now, simply elide the range verification for the CU DIEs if we are
|
|
|
|
// processing an object file.
|
|
|
|
|
2018-10-31 07:45:27 +08:00
|
|
|
if (!IsObjectFile || IsMachOObject || Die.getTag() != DW_TAG_compile_unit) {
|
2018-10-29 06:30:48 +08:00
|
|
|
for (auto Range : Ranges) {
|
|
|
|
if (!Range.valid()) {
|
|
|
|
++NumErrors;
|
|
|
|
error() << "Invalid address range " << Range << "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that ranges don't intersect.
|
|
|
|
const auto IntersectingRange = RI.insert(Range);
|
|
|
|
if (IntersectingRange != RI.Ranges.end()) {
|
|
|
|
++NumErrors;
|
|
|
|
error() << "DIE has overlapping address ranges: " << Range << " and "
|
|
|
|
<< *IntersectingRange << "\n";
|
|
|
|
break;
|
|
|
|
}
|
2017-09-14 19:33:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that children don't intersect.
|
|
|
|
const auto IntersectingChild = ParentRI.insert(RI);
|
|
|
|
if (IntersectingChild != ParentRI.Children.end()) {
|
|
|
|
++NumErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "DIEs have overlapping address ranges:";
|
2018-09-19 16:08:13 +08:00
|
|
|
dump(Die);
|
|
|
|
dump(IntersectingChild->Die) << '\n';
|
2017-09-14 19:33:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify that ranges are contained within their parent.
|
|
|
|
bool ShouldBeContained = !Ranges.empty() && !ParentRI.Ranges.empty() &&
|
|
|
|
!(Die.getTag() == DW_TAG_subprogram &&
|
|
|
|
ParentRI.Die.getTag() == DW_TAG_subprogram);
|
|
|
|
if (ShouldBeContained && !ParentRI.contains(RI)) {
|
|
|
|
++NumErrors;
|
2018-05-23 01:38:03 +08:00
|
|
|
error() << "DIE address ranges are not contained in its parent's ranges:";
|
2018-09-19 16:08:13 +08:00
|
|
|
dump(ParentRI.Die);
|
|
|
|
dump(Die, 2) << '\n';
|
2017-07-25 05:04:11 +08:00
|
|
|
}
|
2017-09-14 19:33:42 +08:00
|
|
|
|
|
|
|
// Recursively check children.
|
|
|
|
for (DWARFDie Child : Die)
|
|
|
|
NumErrors += verifyDieRanges(Child, RI);
|
|
|
|
|
2017-07-25 05:04:11 +08:00
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned DWARFVerifier::verifyDebugInfoAttribute(const DWARFDie &Die,
|
|
|
|
DWARFAttribute &AttrValue) {
|
|
|
|
unsigned NumErrors = 0;
|
2017-10-27 18:42:04 +08:00
|
|
|
auto ReportError = [&](const Twine &TitleMsg) {
|
|
|
|
++NumErrors;
|
|
|
|
error() << TitleMsg << '\n';
|
2018-09-19 16:08:13 +08:00
|
|
|
dump(Die) << '\n';
|
2017-10-27 18:42:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
2017-05-04 02:25:46 +08:00
|
|
|
const auto Attr = AttrValue.Attr;
|
|
|
|
switch (Attr) {
|
|
|
|
case DW_AT_ranges:
|
|
|
|
// Make sure the offset in the DW_AT_ranges attribute is valid.
|
|
|
|
if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
|
2017-10-27 18:42:04 +08:00
|
|
|
if (*SectionOffset >= DObj.getRangeSection().Data.size())
|
|
|
|
ReportError("DW_AT_ranges offset is beyond .debug_ranges bounds:");
|
|
|
|
break;
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
2017-10-27 18:42:04 +08:00
|
|
|
ReportError("DIE has invalid DW_AT_ranges encoding:");
|
2017-05-04 02:25:46 +08:00
|
|
|
break;
|
|
|
|
case DW_AT_stmt_list:
|
|
|
|
// Make sure the offset in the DW_AT_stmt_list attribute is valid.
|
|
|
|
if (auto SectionOffset = AttrValue.Value.getAsSectionOffset()) {
|
2017-10-27 18:42:04 +08:00
|
|
|
if (*SectionOffset >= DObj.getLineSection().Data.size())
|
|
|
|
ReportError("DW_AT_stmt_list offset is beyond .debug_line bounds: " +
|
2017-10-27 18:58:04 +08:00
|
|
|
llvm::formatv("{0:x8}", *SectionOffset));
|
2017-10-27 18:42:04 +08:00
|
|
|
break;
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
2017-10-27 18:42:04 +08:00
|
|
|
ReportError("DIE has invalid DW_AT_stmt_list encoding:");
|
2017-05-04 02:25:46 +08:00
|
|
|
break;
|
2017-10-27 18:42:04 +08:00
|
|
|
case DW_AT_location: {
|
2018-05-23 01:37:27 +08:00
|
|
|
auto VerifyLocationExpr = [&](StringRef D) {
|
2018-02-17 21:06:37 +08:00
|
|
|
DWARFUnit *U = Die.getDwarfUnit();
|
|
|
|
DataExtractor Data(D, DCtx.isLittleEndian(), 0);
|
|
|
|
DWARFExpression Expression(Data, U->getVersion(),
|
|
|
|
U->getAddressByteSize());
|
|
|
|
bool Error = llvm::any_of(Expression, [](DWARFExpression::Operation &Op) {
|
|
|
|
return Op.isError();
|
|
|
|
});
|
2019-02-21 16:20:24 +08:00
|
|
|
if (Error || !Expression.verify(U))
|
2018-02-17 21:06:37 +08:00
|
|
|
ReportError("DIE contains invalid DWARF expression:");
|
|
|
|
};
|
|
|
|
if (Optional<ArrayRef<uint8_t>> Expr = AttrValue.Value.getAsBlock()) {
|
|
|
|
// Verify inlined location.
|
2018-05-23 01:37:27 +08:00
|
|
|
VerifyLocationExpr(llvm::toStringRef(*Expr));
|
|
|
|
} else if (auto LocOffset = AttrValue.Value.getAsSectionOffset()) {
|
2018-02-17 21:06:37 +08:00
|
|
|
// Verify location list.
|
|
|
|
if (auto DebugLoc = DCtx.getDebugLoc())
|
|
|
|
if (auto LocList = DebugLoc->getLocationListAtOffset(*LocOffset))
|
|
|
|
for (const auto &Entry : LocList->Entries)
|
2018-05-23 01:37:27 +08:00
|
|
|
VerifyLocationExpr({Entry.Loc.data(), Entry.Loc.size()});
|
2017-10-27 18:42:04 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-09-21 15:49:29 +08:00
|
|
|
case DW_AT_specification:
|
|
|
|
case DW_AT_abstract_origin: {
|
|
|
|
if (auto ReferencedDie = Die.getAttributeValueAsReferencedDie(Attr)) {
|
|
|
|
auto DieTag = Die.getTag();
|
|
|
|
auto RefTag = ReferencedDie.getTag();
|
|
|
|
if (DieTag == RefTag)
|
|
|
|
break;
|
|
|
|
if (DieTag == DW_TAG_inlined_subroutine && RefTag == DW_TAG_subprogram)
|
|
|
|
break;
|
|
|
|
if (DieTag == DW_TAG_variable && RefTag == DW_TAG_member)
|
|
|
|
break;
|
|
|
|
ReportError("DIE with tag " + TagString(DieTag) + " has " +
|
|
|
|
AttributeString(Attr) +
|
|
|
|
" that points to DIE with "
|
|
|
|
"incompatible tag " +
|
|
|
|
TagString(RefTag));
|
|
|
|
}
|
2018-10-11 04:10:37 +08:00
|
|
|
break;
|
2018-09-21 15:49:29 +08:00
|
|
|
}
|
2018-09-21 15:50:21 +08:00
|
|
|
case DW_AT_type: {
|
|
|
|
DWARFDie TypeDie = Die.getAttributeValueAsReferencedDie(DW_AT_type);
|
|
|
|
if (TypeDie && !isType(TypeDie.getTag())) {
|
|
|
|
ReportError("DIE has " + AttributeString(Attr) +
|
|
|
|
" with incompatible tag " + TagString(TypeDie.getTag()));
|
|
|
|
}
|
2018-10-11 04:10:37 +08:00
|
|
|
break;
|
2018-09-21 15:50:21 +08:00
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-07-18 09:00:26 +08:00
|
|
|
return NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
2017-05-04 00:02:29 +08:00
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned DWARFVerifier::verifyDebugInfoForm(const DWARFDie &Die,
|
|
|
|
DWARFAttribute &AttrValue) {
|
2017-07-20 06:27:28 +08:00
|
|
|
const DWARFObject &DObj = DCtx.getDWARFObj();
|
2018-11-08 05:39:09 +08:00
|
|
|
auto DieCU = Die.getDwarfUnit();
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned NumErrors = 0;
|
2017-05-04 02:25:46 +08:00
|
|
|
const auto Form = AttrValue.Value.getForm();
|
|
|
|
switch (Form) {
|
|
|
|
case DW_FORM_ref1:
|
|
|
|
case DW_FORM_ref2:
|
|
|
|
case DW_FORM_ref4:
|
|
|
|
case DW_FORM_ref8:
|
|
|
|
case DW_FORM_ref_udata: {
|
|
|
|
// Verify all CU relative references are valid CU offsets.
|
|
|
|
Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
|
|
|
|
assert(RefVal);
|
|
|
|
if (RefVal) {
|
|
|
|
auto CUSize = DieCU->getNextUnitOffset() - DieCU->getOffset();
|
|
|
|
auto CUOffset = AttrValue.Value.getRawUValue();
|
|
|
|
if (CUOffset >= CUSize) {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << FormEncodingString(Form) << " CU offset "
|
|
|
|
<< format("0x%08" PRIx64, CUOffset)
|
|
|
|
<< " is invalid (must be less than CU size of "
|
|
|
|
<< format("0x%08" PRIx32, CUSize) << "):\n";
|
2017-09-21 01:44:00 +08:00
|
|
|
Die.dump(OS, 0, DumpOpts);
|
2018-09-19 16:08:13 +08:00
|
|
|
dump(Die) << '\n';
|
2017-05-04 02:25:46 +08:00
|
|
|
} else {
|
|
|
|
// Valid reference, but we will verify it points to an actual
|
|
|
|
// DIE later.
|
|
|
|
ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
|
2017-05-04 00:02:29 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DW_FORM_ref_addr: {
|
|
|
|
// Verify all absolute DIE references have valid offsets in the
|
|
|
|
// .debug_info section.
|
|
|
|
Optional<uint64_t> RefVal = AttrValue.Value.getAsReference();
|
|
|
|
assert(RefVal);
|
|
|
|
if (RefVal) {
|
2018-11-08 05:39:09 +08:00
|
|
|
if (*RefVal >= DieCU->getInfoSection().Data.size()) {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "DW_FORM_ref_addr offset beyond .debug_info "
|
|
|
|
"bounds:\n";
|
2018-09-19 16:08:13 +08:00
|
|
|
dump(Die) << '\n';
|
2017-05-04 02:25:46 +08:00
|
|
|
} else {
|
|
|
|
// Valid reference, but we will verify it points to an actual
|
|
|
|
// DIE later.
|
|
|
|
ReferenceToDIEOffsets[*RefVal].insert(Die.getOffset());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case DW_FORM_strp: {
|
|
|
|
auto SecOffset = AttrValue.Value.getAsSectionOffset();
|
|
|
|
assert(SecOffset); // DW_FORM_strp is a section offset.
|
2017-07-20 06:27:28 +08:00
|
|
|
if (SecOffset && *SecOffset >= DObj.getStringSection().size()) {
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "DW_FORM_strp offset beyond .debug_str bounds:\n";
|
2018-09-19 16:08:13 +08:00
|
|
|
dump(Die) << '\n';
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
|
|
|
break;
|
2017-05-04 00:02:29 +08:00
|
|
|
}
|
2018-11-03 08:27:35 +08:00
|
|
|
case DW_FORM_strx:
|
|
|
|
case DW_FORM_strx1:
|
|
|
|
case DW_FORM_strx2:
|
|
|
|
case DW_FORM_strx3:
|
|
|
|
case DW_FORM_strx4: {
|
|
|
|
auto Index = AttrValue.Value.getRawUValue();
|
|
|
|
auto DieCU = Die.getDwarfUnit();
|
|
|
|
// Check that we have a valid DWARF v5 string offsets table.
|
|
|
|
if (!DieCU->getStringOffsetsTableContribution()) {
|
|
|
|
++NumErrors;
|
|
|
|
error() << FormEncodingString(Form)
|
|
|
|
<< " used without a valid string offsets table:\n";
|
|
|
|
dump(Die) << '\n';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Check that the index is within the bounds of the section.
|
|
|
|
unsigned ItemSize = DieCU->getDwarfStringOffsetsByteSize();
|
|
|
|
// Use a 64-bit type to calculate the offset to guard against overflow.
|
|
|
|
uint64_t Offset =
|
|
|
|
(uint64_t)DieCU->getStringOffsetsBase() + Index * ItemSize;
|
|
|
|
if (DObj.getStringOffsetSection().Data.size() < Offset + ItemSize) {
|
|
|
|
++NumErrors;
|
|
|
|
error() << FormEncodingString(Form) << " uses index "
|
|
|
|
<< format("%" PRIu64, Index) << ", which is too large:\n";
|
|
|
|
dump(Die) << '\n';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Check that the string offset is valid.
|
|
|
|
uint64_t StringOffset = *DieCU->getStringOffsetSectionItem(Index);
|
|
|
|
if (StringOffset >= DObj.getStringSection().size()) {
|
|
|
|
++NumErrors;
|
|
|
|
error() << FormEncodingString(Form) << " uses index "
|
|
|
|
<< format("%" PRIu64, Index)
|
|
|
|
<< ", but the referenced string"
|
|
|
|
" offset is beyond .debug_str bounds:\n";
|
|
|
|
dump(Die) << '\n';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-07-18 09:00:26 +08:00
|
|
|
return NumErrors;
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
2017-05-04 00:02:29 +08:00
|
|
|
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned DWARFVerifier::verifyDebugInfoReferences() {
|
2017-05-04 00:02:29 +08:00
|
|
|
// Take all references and make sure they point to an actual DIE by
|
|
|
|
// getting the DIE by offset and emitting an error
|
|
|
|
OS << "Verifying .debug_info references...\n";
|
2017-07-18 09:00:26 +08:00
|
|
|
unsigned NumErrors = 0;
|
2017-05-04 00:02:29 +08:00
|
|
|
for (auto Pair : ReferenceToDIEOffsets) {
|
|
|
|
auto Die = DCtx.getDIEForOffset(Pair.first);
|
|
|
|
if (Die)
|
|
|
|
continue;
|
2017-07-18 09:00:26 +08:00
|
|
|
++NumErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "invalid DIE reference " << format("0x%08" PRIx64, Pair.first)
|
|
|
|
<< ". Offset is in between DIEs:\n";
|
2018-09-19 16:08:13 +08:00
|
|
|
for (auto Offset : Pair.second)
|
|
|
|
dump(DCtx.getDIEForOffset(Offset)) << '\n';
|
2017-05-04 00:02:29 +08:00
|
|
|
OS << "\n";
|
|
|
|
}
|
2017-07-18 09:00:26 +08:00
|
|
|
return NumErrors;
|
2017-05-04 00:02:29 +08:00
|
|
|
}
|
|
|
|
|
2017-05-04 02:25:46 +08:00
|
|
|
void DWARFVerifier::verifyDebugLineStmtOffsets() {
|
2017-05-04 00:02:29 +08:00
|
|
|
std::map<uint64_t, DWARFDie> StmtListToDie;
|
|
|
|
for (const auto &CU : DCtx.compile_units()) {
|
2017-05-04 02:25:46 +08:00
|
|
|
auto Die = CU->getUnitDIE();
|
2017-05-04 00:02:29 +08:00
|
|
|
// Get the attribute value as a section offset. No need to produce an
|
|
|
|
// error here if the encoding isn't correct because we validate this in
|
|
|
|
// the .debug_info verifier.
|
2017-05-04 02:25:46 +08:00
|
|
|
auto StmtSectionOffset = toSectionOffset(Die.find(DW_AT_stmt_list));
|
2017-05-04 00:02:29 +08:00
|
|
|
if (!StmtSectionOffset)
|
|
|
|
continue;
|
|
|
|
const uint32_t LineTableOffset = *StmtSectionOffset;
|
2017-05-04 02:25:46 +08:00
|
|
|
auto LineTable = DCtx.getLineTableForUnit(CU.get());
|
2017-07-20 06:27:28 +08:00
|
|
|
if (LineTableOffset < DCtx.getDWARFObj().getLineSection().Data.size()) {
|
2017-05-04 02:25:46 +08:00
|
|
|
if (!LineTable) {
|
|
|
|
++NumDebugLineErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << ".debug_line[" << format("0x%08" PRIx32, LineTableOffset)
|
|
|
|
<< "] was not able to be parsed for CU:\n";
|
2018-09-19 16:08:13 +08:00
|
|
|
dump(Die) << '\n';
|
2017-05-04 02:25:46 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Make sure we don't get a valid line table back if the offset is wrong.
|
|
|
|
assert(LineTable == nullptr);
|
2017-05-04 00:02:29 +08:00
|
|
|
// Skip this line table as it isn't valid. No need to create an error
|
|
|
|
// here because we validate this in the .debug_info verifier.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
auto Iter = StmtListToDie.find(LineTableOffset);
|
|
|
|
if (Iter != StmtListToDie.end()) {
|
|
|
|
++NumDebugLineErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "two compile unit DIEs, "
|
|
|
|
<< format("0x%08" PRIx32, Iter->second.getOffset()) << " and "
|
|
|
|
<< format("0x%08" PRIx32, Die.getOffset())
|
|
|
|
<< ", have the same DW_AT_stmt_list section offset:\n";
|
2018-09-19 16:08:13 +08:00
|
|
|
dump(Iter->second);
|
|
|
|
dump(Die) << '\n';
|
2017-05-04 00:02:29 +08:00
|
|
|
// Already verified this line table before, no need to do it again.
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
StmtListToDie[LineTableOffset] = Die;
|
|
|
|
}
|
|
|
|
}
|
2017-05-04 00:02:29 +08:00
|
|
|
|
2017-05-04 02:25:46 +08:00
|
|
|
void DWARFVerifier::verifyDebugLineRows() {
|
|
|
|
for (const auto &CU : DCtx.compile_units()) {
|
|
|
|
auto Die = CU->getUnitDIE();
|
2017-05-04 00:02:29 +08:00
|
|
|
auto LineTable = DCtx.getLineTableForUnit(CU.get());
|
2017-05-04 02:25:46 +08:00
|
|
|
// If there is no line table we will have created an error in the
|
|
|
|
// .debug_info verifier or in verifyDebugLineStmtOffsets().
|
|
|
|
if (!LineTable)
|
2017-05-04 00:02:29 +08:00
|
|
|
continue;
|
2017-09-08 17:48:51 +08:00
|
|
|
|
|
|
|
// Verify prologue.
|
2017-05-04 00:02:29 +08:00
|
|
|
uint32_t MaxFileIndex = LineTable->Prologue.FileNames.size();
|
2017-09-08 17:48:51 +08:00
|
|
|
uint32_t MaxDirIndex = LineTable->Prologue.IncludeDirectories.size();
|
|
|
|
uint32_t FileIndex = 1;
|
|
|
|
StringMap<uint16_t> FullPathMap;
|
|
|
|
for (const auto &FileName : LineTable->Prologue.FileNames) {
|
|
|
|
// Verify directory index.
|
|
|
|
if (FileName.DirIdx > MaxDirIndex) {
|
|
|
|
++NumDebugLineErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << ".debug_line["
|
|
|
|
<< format("0x%08" PRIx64,
|
|
|
|
*toSectionOffset(Die.find(DW_AT_stmt_list)))
|
|
|
|
<< "].prologue.file_names[" << FileIndex
|
|
|
|
<< "].dir_idx contains an invalid index: " << FileName.DirIdx
|
|
|
|
<< "\n";
|
2017-09-08 17:48:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check file paths for duplicates.
|
|
|
|
std::string FullPath;
|
|
|
|
const bool HasFullPath = LineTable->getFileNameByIndex(
|
|
|
|
FileIndex, CU->getCompilationDir(),
|
|
|
|
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FullPath);
|
|
|
|
assert(HasFullPath && "Invalid index?");
|
|
|
|
(void)HasFullPath;
|
|
|
|
auto It = FullPathMap.find(FullPath);
|
|
|
|
if (It == FullPathMap.end())
|
|
|
|
FullPathMap[FullPath] = FileIndex;
|
|
|
|
else if (It->second != FileIndex) {
|
2017-09-29 17:33:31 +08:00
|
|
|
warn() << ".debug_line["
|
|
|
|
<< format("0x%08" PRIx64,
|
|
|
|
*toSectionOffset(Die.find(DW_AT_stmt_list)))
|
|
|
|
<< "].prologue.file_names[" << FileIndex
|
|
|
|
<< "] is a duplicate of file_names[" << It->second << "]\n";
|
2017-09-08 17:48:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FileIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify rows.
|
2017-05-04 00:02:29 +08:00
|
|
|
uint64_t PrevAddress = 0;
|
|
|
|
uint32_t RowIndex = 0;
|
|
|
|
for (const auto &Row : LineTable->Rows) {
|
2017-09-08 17:48:51 +08:00
|
|
|
// Verify row address.
|
2017-05-04 00:02:29 +08:00
|
|
|
if (Row.Address < PrevAddress) {
|
|
|
|
++NumDebugLineErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << ".debug_line["
|
|
|
|
<< format("0x%08" PRIx64,
|
|
|
|
*toSectionOffset(Die.find(DW_AT_stmt_list)))
|
|
|
|
<< "] row[" << RowIndex
|
|
|
|
<< "] decreases in address from previous row:\n";
|
2017-05-04 00:02:29 +08:00
|
|
|
|
|
|
|
DWARFDebugLine::Row::dumpTableHeader(OS);
|
|
|
|
if (RowIndex > 0)
|
|
|
|
LineTable->Rows[RowIndex - 1].dump(OS);
|
|
|
|
Row.dump(OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
2017-09-08 17:48:51 +08:00
|
|
|
// Verify file index.
|
2017-05-04 00:02:29 +08:00
|
|
|
if (Row.File > MaxFileIndex) {
|
|
|
|
++NumDebugLineErrors;
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << ".debug_line["
|
|
|
|
<< format("0x%08" PRIx64,
|
|
|
|
*toSectionOffset(Die.find(DW_AT_stmt_list)))
|
|
|
|
<< "][" << RowIndex << "] has invalid file index " << Row.File
|
|
|
|
<< " (valid values are [1," << MaxFileIndex << "]):\n";
|
2017-05-04 00:02:29 +08:00
|
|
|
DWARFDebugLine::Row::dumpTableHeader(OS);
|
|
|
|
Row.dump(OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
if (Row.EndSequence)
|
|
|
|
PrevAddress = 0;
|
|
|
|
else
|
|
|
|
PrevAddress = Row.Address;
|
|
|
|
++RowIndex;
|
|
|
|
}
|
|
|
|
}
|
2017-05-04 02:25:46 +08:00
|
|
|
}
|
|
|
|
|
2018-10-29 06:30:48 +08:00
|
|
|
DWARFVerifier::DWARFVerifier(raw_ostream &S, DWARFContext &D,
|
|
|
|
DIDumpOptions DumpOpts)
|
|
|
|
: OS(S), DCtx(D), DumpOpts(std::move(DumpOpts)), IsObjectFile(false),
|
|
|
|
IsMachOObject(false) {
|
|
|
|
if (const auto *F = DCtx.getDWARFObj().getFile()) {
|
|
|
|
IsObjectFile = F->isRelocatableObject();
|
|
|
|
IsMachOObject = F->isMachO();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:25:46 +08:00
|
|
|
bool DWARFVerifier::handleDebugLine() {
|
|
|
|
NumDebugLineErrors = 0;
|
|
|
|
OS << "Verifying .debug_line...\n";
|
|
|
|
verifyDebugLineStmtOffsets();
|
|
|
|
verifyDebugLineRows();
|
2017-05-04 00:02:29 +08:00
|
|
|
return NumDebugLineErrors == 0;
|
|
|
|
}
|
2017-06-14 08:17:55 +08:00
|
|
|
|
2018-01-22 21:17:23 +08:00
|
|
|
unsigned DWARFVerifier::verifyAppleAccelTable(const DWARFSection *AccelSection,
|
|
|
|
DataExtractor *StrData,
|
|
|
|
const char *SectionName) {
|
2017-07-26 08:52:31 +08:00
|
|
|
unsigned NumErrors = 0;
|
|
|
|
DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), *AccelSection,
|
|
|
|
DCtx.isLittleEndian(), 0);
|
2018-01-22 21:17:23 +08:00
|
|
|
AppleAcceleratorTable AccelTable(AccelSectionData, *StrData);
|
2017-06-14 08:17:55 +08:00
|
|
|
|
2017-07-26 08:52:31 +08:00
|
|
|
OS << "Verifying " << SectionName << "...\n";
|
2017-06-17 06:03:21 +08:00
|
|
|
|
2017-09-29 17:33:31 +08:00
|
|
|
// Verify that the fixed part of the header is not too short.
|
2017-07-26 08:52:31 +08:00
|
|
|
if (!AccelSectionData.isValidOffset(AccelTable.getSizeHdr())) {
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "Section is too small to fit a section header.\n";
|
2017-07-26 08:52:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2017-09-29 17:33:31 +08:00
|
|
|
|
2017-07-26 08:52:31 +08:00
|
|
|
// Verify that the section is not too short.
|
2017-12-12 02:22:47 +08:00
|
|
|
if (Error E = AccelTable.extract()) {
|
|
|
|
error() << toString(std::move(E)) << '\n';
|
2017-07-26 08:52:31 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2017-09-29 17:33:31 +08:00
|
|
|
|
2017-06-30 04:13:05 +08:00
|
|
|
// Verify that all buckets have a valid hash index or are empty.
|
2017-07-26 08:52:31 +08:00
|
|
|
uint32_t NumBuckets = AccelTable.getNumBuckets();
|
|
|
|
uint32_t NumHashes = AccelTable.getNumHashes();
|
2017-06-14 08:17:55 +08:00
|
|
|
|
|
|
|
uint32_t BucketsOffset =
|
2017-07-26 08:52:31 +08:00
|
|
|
AccelTable.getSizeHdr() + AccelTable.getHeaderDataLength();
|
2017-06-30 04:13:05 +08:00
|
|
|
uint32_t HashesBase = BucketsOffset + NumBuckets * 4;
|
|
|
|
uint32_t OffsetsBase = HashesBase + NumHashes * 4;
|
2017-06-14 08:17:55 +08:00
|
|
|
for (uint32_t BucketIdx = 0; BucketIdx < NumBuckets; ++BucketIdx) {
|
2017-07-26 08:52:31 +08:00
|
|
|
uint32_t HashIdx = AccelSectionData.getU32(&BucketsOffset);
|
2017-06-14 08:17:55 +08:00
|
|
|
if (HashIdx >= NumHashes && HashIdx != UINT32_MAX) {
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << format("Bucket[%d] has invalid hash index: %u.\n", BucketIdx,
|
|
|
|
HashIdx);
|
2017-07-26 08:52:31 +08:00
|
|
|
++NumErrors;
|
2017-06-14 08:17:55 +08:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 08:52:31 +08:00
|
|
|
uint32_t NumAtoms = AccelTable.getAtomsDesc().size();
|
2017-06-30 04:13:05 +08:00
|
|
|
if (NumAtoms == 0) {
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "No atoms: failed to read HashData.\n";
|
2017-07-26 08:52:31 +08:00
|
|
|
return 1;
|
2017-06-30 04:13:05 +08:00
|
|
|
}
|
2017-07-26 08:52:31 +08:00
|
|
|
if (!AccelTable.validateForms()) {
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "Unsupported form: failed to read HashData.\n";
|
2017-07-26 08:52:31 +08:00
|
|
|
return 1;
|
2017-06-30 04:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t HashIdx = 0; HashIdx < NumHashes; ++HashIdx) {
|
|
|
|
uint32_t HashOffset = HashesBase + 4 * HashIdx;
|
|
|
|
uint32_t DataOffset = OffsetsBase + 4 * HashIdx;
|
2017-07-26 08:52:31 +08:00
|
|
|
uint32_t Hash = AccelSectionData.getU32(&HashOffset);
|
|
|
|
uint32_t HashDataOffset = AccelSectionData.getU32(&DataOffset);
|
|
|
|
if (!AccelSectionData.isValidOffsetForDataOfSize(HashDataOffset,
|
|
|
|
sizeof(uint64_t))) {
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << format("Hash[%d] has invalid HashData offset: 0x%08x.\n",
|
|
|
|
HashIdx, HashDataOffset);
|
2017-07-26 08:52:31 +08:00
|
|
|
++NumErrors;
|
2017-06-30 04:13:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t StrpOffset;
|
|
|
|
uint32_t StringOffset;
|
|
|
|
uint32_t StringCount = 0;
|
2017-08-01 02:01:16 +08:00
|
|
|
unsigned Offset;
|
|
|
|
unsigned Tag;
|
2017-07-26 08:52:31 +08:00
|
|
|
while ((StrpOffset = AccelSectionData.getU32(&HashDataOffset)) != 0) {
|
2017-06-30 04:13:05 +08:00
|
|
|
const uint32_t NumHashDataObjects =
|
2017-07-26 08:52:31 +08:00
|
|
|
AccelSectionData.getU32(&HashDataOffset);
|
2017-06-30 04:13:05 +08:00
|
|
|
for (uint32_t HashDataIdx = 0; HashDataIdx < NumHashDataObjects;
|
|
|
|
++HashDataIdx) {
|
2017-08-01 02:01:16 +08:00
|
|
|
std::tie(Offset, Tag) = AccelTable.readAtoms(HashDataOffset);
|
|
|
|
auto Die = DCtx.getDIEForOffset(Offset);
|
|
|
|
if (!Die) {
|
2017-06-30 04:13:05 +08:00
|
|
|
const uint32_t BucketIdx =
|
|
|
|
NumBuckets ? (Hash % NumBuckets) : UINT32_MAX;
|
|
|
|
StringOffset = StrpOffset;
|
2017-07-26 08:52:31 +08:00
|
|
|
const char *Name = StrData->getCStr(&StringOffset);
|
2017-06-30 04:13:05 +08:00
|
|
|
if (!Name)
|
|
|
|
Name = "<NULL>";
|
|
|
|
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << format(
|
|
|
|
"%s Bucket[%d] Hash[%d] = 0x%08x "
|
2017-06-30 04:13:05 +08:00
|
|
|
"Str[%u] = 0x%08x "
|
|
|
|
"DIE[%d] = 0x%08x is not a valid DIE offset for \"%s\".\n",
|
2017-07-26 08:52:31 +08:00
|
|
|
SectionName, BucketIdx, HashIdx, Hash, StringCount, StrpOffset,
|
2017-08-01 02:01:16 +08:00
|
|
|
HashDataIdx, Offset, Name);
|
2017-06-30 04:13:05 +08:00
|
|
|
|
2017-07-26 08:52:31 +08:00
|
|
|
++NumErrors;
|
2017-08-01 02:01:16 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((Tag != dwarf::DW_TAG_null) && (Die.getTag() != Tag)) {
|
2017-09-29 17:33:31 +08:00
|
|
|
error() << "Tag " << dwarf::TagString(Tag)
|
|
|
|
<< " in accelerator table does not match Tag "
|
|
|
|
<< dwarf::TagString(Die.getTag()) << " of DIE[" << HashDataIdx
|
|
|
|
<< "].\n";
|
2017-08-01 02:01:16 +08:00
|
|
|
++NumErrors;
|
2017-06-30 04:13:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++StringCount;
|
|
|
|
}
|
|
|
|
}
|
2017-07-26 08:52:31 +08:00
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2018-03-08 23:34:42 +08:00
|
|
|
unsigned
|
|
|
|
DWARFVerifier::verifyDebugNamesCULists(const DWARFDebugNames &AccelTable) {
|
|
|
|
// A map from CU offset to the (first) Name Index offset which claims to index
|
|
|
|
// this CU.
|
|
|
|
DenseMap<uint32_t, uint32_t> CUMap;
|
|
|
|
const uint32_t NotIndexed = std::numeric_limits<uint32_t>::max();
|
|
|
|
|
|
|
|
CUMap.reserve(DCtx.getNumCompileUnits());
|
|
|
|
for (const auto &CU : DCtx.compile_units())
|
|
|
|
CUMap[CU->getOffset()] = NotIndexed;
|
|
|
|
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
for (const DWARFDebugNames::NameIndex &NI : AccelTable) {
|
|
|
|
if (NI.getCUCount() == 0) {
|
|
|
|
error() << formatv("Name Index @ {0:x} does not index any CU\n",
|
|
|
|
NI.getUnitOffset());
|
|
|
|
++NumErrors;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (uint32_t CU = 0, End = NI.getCUCount(); CU < End; ++CU) {
|
|
|
|
uint32_t Offset = NI.getCUOffset(CU);
|
|
|
|
auto Iter = CUMap.find(Offset);
|
|
|
|
|
|
|
|
if (Iter == CUMap.end()) {
|
|
|
|
error() << formatv(
|
|
|
|
"Name Index @ {0:x} references a non-existing CU @ {1:x}\n",
|
|
|
|
NI.getUnitOffset(), Offset);
|
|
|
|
++NumErrors;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Iter->second != NotIndexed) {
|
|
|
|
error() << formatv("Name Index @ {0:x} references a CU @ {1:x}, but "
|
2018-09-17 22:23:47 +08:00
|
|
|
"this CU is already indexed by Name Index @ {2:x}\n",
|
|
|
|
NI.getUnitOffset(), Offset, Iter->second);
|
2018-03-08 23:34:42 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Iter->second = NI.getUnitOffset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &KV : CUMap) {
|
|
|
|
if (KV.second == NotIndexed)
|
|
|
|
warn() << formatv("CU @ {0:x} not covered by any Name Index\n", KV.first);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2018-03-16 18:02:16 +08:00
|
|
|
unsigned
|
|
|
|
DWARFVerifier::verifyNameIndexBuckets(const DWARFDebugNames::NameIndex &NI,
|
|
|
|
const DataExtractor &StrData) {
|
|
|
|
struct BucketInfo {
|
|
|
|
uint32_t Bucket;
|
|
|
|
uint32_t Index;
|
|
|
|
|
|
|
|
constexpr BucketInfo(uint32_t Bucket, uint32_t Index)
|
|
|
|
: Bucket(Bucket), Index(Index) {}
|
|
|
|
bool operator<(const BucketInfo &RHS) const { return Index < RHS.Index; };
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t NumErrors = 0;
|
|
|
|
if (NI.getBucketCount() == 0) {
|
|
|
|
warn() << formatv("Name Index @ {0:x} does not contain a hash table.\n",
|
|
|
|
NI.getUnitOffset());
|
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build up a list of (Bucket, Index) pairs. We use this later to verify that
|
|
|
|
// each Name is reachable from the appropriate bucket.
|
|
|
|
std::vector<BucketInfo> BucketStarts;
|
|
|
|
BucketStarts.reserve(NI.getBucketCount() + 1);
|
|
|
|
for (uint32_t Bucket = 0, End = NI.getBucketCount(); Bucket < End; ++Bucket) {
|
|
|
|
uint32_t Index = NI.getBucketArrayEntry(Bucket);
|
|
|
|
if (Index > NI.getNameCount()) {
|
|
|
|
error() << formatv("Bucket {0} of Name Index @ {1:x} contains invalid "
|
|
|
|
"value {2}. Valid range is [0, {3}].\n",
|
|
|
|
Bucket, NI.getUnitOffset(), Index, NI.getNameCount());
|
|
|
|
++NumErrors;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (Index > 0)
|
|
|
|
BucketStarts.emplace_back(Bucket, Index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there were any buckets with invalid values, skip further checks as they
|
|
|
|
// will likely produce many errors which will only confuse the actual root
|
|
|
|
// problem.
|
|
|
|
if (NumErrors > 0)
|
|
|
|
return NumErrors;
|
|
|
|
|
|
|
|
// Sort the list in the order of increasing "Index" entries.
|
|
|
|
array_pod_sort(BucketStarts.begin(), BucketStarts.end());
|
|
|
|
|
|
|
|
// Insert a sentinel entry at the end, so we can check that the end of the
|
|
|
|
// table is covered in the loop below.
|
|
|
|
BucketStarts.emplace_back(NI.getBucketCount(), NI.getNameCount() + 1);
|
|
|
|
|
|
|
|
// Loop invariant: NextUncovered is the (1-based) index of the first Name
|
|
|
|
// which is not reachable by any of the buckets we processed so far (and
|
|
|
|
// hasn't been reported as uncovered).
|
|
|
|
uint32_t NextUncovered = 1;
|
|
|
|
for (const BucketInfo &B : BucketStarts) {
|
|
|
|
// Under normal circumstances B.Index be equal to NextUncovered, but it can
|
|
|
|
// be less if a bucket points to names which are already known to be in some
|
|
|
|
// bucket we processed earlier. In that case, we won't trigger this error,
|
|
|
|
// but report the mismatched hash value error instead. (We know the hash
|
|
|
|
// will not match because we have already verified that the name's hash
|
|
|
|
// puts it into the previous bucket.)
|
|
|
|
if (B.Index > NextUncovered) {
|
|
|
|
error() << formatv("Name Index @ {0:x}: Name table entries [{1}, {2}] "
|
|
|
|
"are not covered by the hash table.\n",
|
|
|
|
NI.getUnitOffset(), NextUncovered, B.Index - 1);
|
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
uint32_t Idx = B.Index;
|
|
|
|
|
|
|
|
// The rest of the checks apply only to non-sentinel entries.
|
|
|
|
if (B.Bucket == NI.getBucketCount())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// This triggers if a non-empty bucket points to a name with a mismatched
|
|
|
|
// hash. Clients are likely to interpret this as an empty bucket, because a
|
|
|
|
// mismatched hash signals the end of a bucket, but if this is indeed an
|
|
|
|
// empty bucket, the producer should have signalled this by marking the
|
|
|
|
// bucket as empty.
|
|
|
|
uint32_t FirstHash = NI.getHashArrayEntry(Idx);
|
|
|
|
if (FirstHash % NI.getBucketCount() != B.Bucket) {
|
|
|
|
error() << formatv(
|
|
|
|
"Name Index @ {0:x}: Bucket {1} is not empty but points to a "
|
|
|
|
"mismatched hash value {2:x} (belonging to bucket {3}).\n",
|
|
|
|
NI.getUnitOffset(), B.Bucket, FirstHash,
|
|
|
|
FirstHash % NI.getBucketCount());
|
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This find the end of this bucket and also verifies that all the hashes in
|
|
|
|
// this bucket are correct by comparing the stored hashes to the ones we
|
|
|
|
// compute ourselves.
|
|
|
|
while (Idx <= NI.getNameCount()) {
|
|
|
|
uint32_t Hash = NI.getHashArrayEntry(Idx);
|
|
|
|
if (Hash % NI.getBucketCount() != B.Bucket)
|
|
|
|
break;
|
|
|
|
|
2018-06-01 18:33:11 +08:00
|
|
|
const char *Str = NI.getNameTableEntry(Idx).getString();
|
2018-03-16 18:02:16 +08:00
|
|
|
if (caseFoldingDjbHash(Str) != Hash) {
|
|
|
|
error() << formatv("Name Index @ {0:x}: String ({1}) at index {2} "
|
|
|
|
"hashes to {3:x}, but "
|
|
|
|
"the Name Index hash is {4:x}\n",
|
|
|
|
NI.getUnitOffset(), Str, Idx,
|
|
|
|
caseFoldingDjbHash(Str), Hash);
|
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
++Idx;
|
|
|
|
}
|
|
|
|
NextUncovered = std::max(NextUncovered, Idx);
|
|
|
|
}
|
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2018-03-22 22:50:44 +08:00
|
|
|
unsigned DWARFVerifier::verifyNameIndexAttribute(
|
|
|
|
const DWARFDebugNames::NameIndex &NI, const DWARFDebugNames::Abbrev &Abbr,
|
|
|
|
DWARFDebugNames::AttributeEncoding AttrEnc) {
|
|
|
|
StringRef FormName = dwarf::FormEncodingString(AttrEnc.Form);
|
|
|
|
if (FormName.empty()) {
|
|
|
|
error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
|
|
|
|
"unknown form: {3}.\n",
|
|
|
|
NI.getUnitOffset(), Abbr.Code, AttrEnc.Index,
|
|
|
|
AttrEnc.Form);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AttrEnc.Index == DW_IDX_type_hash) {
|
|
|
|
if (AttrEnc.Form != dwarf::DW_FORM_data8) {
|
|
|
|
error() << formatv(
|
|
|
|
"NameIndex @ {0:x}: Abbreviation {1:x}: DW_IDX_type_hash "
|
|
|
|
"uses an unexpected form {2} (should be {3}).\n",
|
|
|
|
NI.getUnitOffset(), Abbr.Code, AttrEnc.Form, dwarf::DW_FORM_data8);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A list of known index attributes and their expected form classes.
|
|
|
|
// DW_IDX_type_hash is handled specially in the check above, as it has a
|
|
|
|
// specific form (not just a form class) we should expect.
|
|
|
|
struct FormClassTable {
|
|
|
|
dwarf::Index Index;
|
|
|
|
DWARFFormValue::FormClass Class;
|
|
|
|
StringLiteral ClassName;
|
|
|
|
};
|
|
|
|
static constexpr FormClassTable Table[] = {
|
|
|
|
{dwarf::DW_IDX_compile_unit, DWARFFormValue::FC_Constant, {"constant"}},
|
|
|
|
{dwarf::DW_IDX_type_unit, DWARFFormValue::FC_Constant, {"constant"}},
|
|
|
|
{dwarf::DW_IDX_die_offset, DWARFFormValue::FC_Reference, {"reference"}},
|
|
|
|
{dwarf::DW_IDX_parent, DWARFFormValue::FC_Constant, {"constant"}},
|
|
|
|
};
|
|
|
|
|
|
|
|
ArrayRef<FormClassTable> TableRef(Table);
|
|
|
|
auto Iter = find_if(TableRef, [AttrEnc](const FormClassTable &T) {
|
|
|
|
return T.Index == AttrEnc.Index;
|
|
|
|
});
|
|
|
|
if (Iter == TableRef.end()) {
|
|
|
|
warn() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} contains an "
|
|
|
|
"unknown index attribute: {2}.\n",
|
|
|
|
NI.getUnitOffset(), Abbr.Code, AttrEnc.Index);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DWARFFormValue(AttrEnc.Form).isFormClass(Iter->Class)) {
|
|
|
|
error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x}: {2} uses an "
|
|
|
|
"unexpected form {3} (expected form class {4}).\n",
|
|
|
|
NI.getUnitOffset(), Abbr.Code, AttrEnc.Index,
|
|
|
|
AttrEnc.Form, Iter->ClassName);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
DWARFVerifier::verifyNameIndexAbbrevs(const DWARFDebugNames::NameIndex &NI) {
|
2018-04-06 21:34:12 +08:00
|
|
|
if (NI.getLocalTUCount() + NI.getForeignTUCount() > 0) {
|
|
|
|
warn() << formatv("Name Index @ {0:x}: Verifying indexes of type units is "
|
|
|
|
"not currently supported.\n",
|
|
|
|
NI.getUnitOffset());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-22 22:50:44 +08:00
|
|
|
unsigned NumErrors = 0;
|
|
|
|
for (const auto &Abbrev : NI.getAbbrevs()) {
|
|
|
|
StringRef TagName = dwarf::TagString(Abbrev.Tag);
|
|
|
|
if (TagName.empty()) {
|
|
|
|
warn() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} references an "
|
|
|
|
"unknown tag: {2}.\n",
|
|
|
|
NI.getUnitOffset(), Abbrev.Code, Abbrev.Tag);
|
|
|
|
}
|
|
|
|
SmallSet<unsigned, 5> Attributes;
|
|
|
|
for (const auto &AttrEnc : Abbrev.Attributes) {
|
|
|
|
if (!Attributes.insert(AttrEnc.Index).second) {
|
|
|
|
error() << formatv("NameIndex @ {0:x}: Abbreviation {1:x} contains "
|
|
|
|
"multiple {2} attributes.\n",
|
|
|
|
NI.getUnitOffset(), Abbrev.Code, AttrEnc.Index);
|
|
|
|
++NumErrors;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
NumErrors += verifyNameIndexAttribute(NI, Abbrev, AttrEnc);
|
|
|
|
}
|
2018-04-06 21:34:12 +08:00
|
|
|
|
|
|
|
if (NI.getCUCount() > 1 && !Attributes.count(dwarf::DW_IDX_compile_unit)) {
|
|
|
|
error() << formatv("NameIndex @ {0:x}: Indexing multiple compile units "
|
|
|
|
"and abbreviation {1:x} has no {2} attribute.\n",
|
|
|
|
NI.getUnitOffset(), Abbrev.Code,
|
|
|
|
dwarf::DW_IDX_compile_unit);
|
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
if (!Attributes.count(dwarf::DW_IDX_die_offset)) {
|
|
|
|
error() << formatv(
|
|
|
|
"NameIndex @ {0:x}: Abbreviation {1:x} has no {2} attribute.\n",
|
|
|
|
NI.getUnitOffset(), Abbrev.Code, dwarf::DW_IDX_die_offset);
|
|
|
|
++NumErrors;
|
|
|
|
}
|
2018-03-22 22:50:44 +08:00
|
|
|
}
|
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2018-09-03 20:12:17 +08:00
|
|
|
static SmallVector<StringRef, 2> getNames(const DWARFDie &DIE,
|
|
|
|
bool IncludeLinkageName = true) {
|
2018-04-06 21:34:12 +08:00
|
|
|
SmallVector<StringRef, 2> Result;
|
|
|
|
if (const char *Str = DIE.getName(DINameKind::ShortName))
|
|
|
|
Result.emplace_back(Str);
|
|
|
|
else if (DIE.getTag() == dwarf::DW_TAG_namespace)
|
|
|
|
Result.emplace_back("(anonymous namespace)");
|
|
|
|
|
2018-09-03 20:12:17 +08:00
|
|
|
if (IncludeLinkageName) {
|
|
|
|
if (const char *Str = DIE.getName(DINameKind::LinkageName)) {
|
|
|
|
if (Result.empty() || Result[0] != Str)
|
|
|
|
Result.emplace_back(Str);
|
|
|
|
}
|
2018-04-06 21:34:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2018-06-01 18:33:11 +08:00
|
|
|
unsigned DWARFVerifier::verifyNameIndexEntries(
|
|
|
|
const DWARFDebugNames::NameIndex &NI,
|
|
|
|
const DWARFDebugNames::NameTableEntry &NTE) {
|
2018-04-06 21:34:12 +08:00
|
|
|
// Verifying type unit indexes not supported.
|
|
|
|
if (NI.getLocalTUCount() + NI.getForeignTUCount() > 0)
|
|
|
|
return 0;
|
|
|
|
|
2018-06-01 18:33:11 +08:00
|
|
|
const char *CStr = NTE.getString();
|
2018-04-06 21:34:12 +08:00
|
|
|
if (!CStr) {
|
|
|
|
error() << formatv(
|
|
|
|
"Name Index @ {0:x}: Unable to get string associated with name {1}.\n",
|
2018-06-01 18:33:11 +08:00
|
|
|
NI.getUnitOffset(), NTE.getIndex());
|
2018-04-06 21:34:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
StringRef Str(CStr);
|
|
|
|
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
unsigned NumEntries = 0;
|
2018-06-01 18:33:11 +08:00
|
|
|
uint32_t EntryID = NTE.getEntryOffset();
|
|
|
|
uint32_t NextEntryID = EntryID;
|
|
|
|
Expected<DWARFDebugNames::Entry> EntryOr = NI.getEntry(&NextEntryID);
|
|
|
|
for (; EntryOr; ++NumEntries, EntryID = NextEntryID,
|
|
|
|
EntryOr = NI.getEntry(&NextEntryID)) {
|
2018-04-06 21:34:12 +08:00
|
|
|
uint32_t CUIndex = *EntryOr->getCUIndex();
|
|
|
|
if (CUIndex > NI.getCUCount()) {
|
|
|
|
error() << formatv("Name Index @ {0:x}: Entry @ {1:x} contains an "
|
|
|
|
"invalid CU index ({2}).\n",
|
|
|
|
NI.getUnitOffset(), EntryID, CUIndex);
|
|
|
|
++NumErrors;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint32_t CUOffset = NI.getCUOffset(CUIndex);
|
[DWARF/AccelTable] Remove getDIESectionOffset for DWARF v5 entries
Summary:
This method was not correct for entries in DWO files as it assumed it
could just add up the CU and DIE offsets to get the absolute DIE offset.
This is not correct for the DWO files, as here the CU offset will
reference the skeleton unit, whereas the DIE offset will be the offset
in the full unit in the DWO file.
Unfortunately, this means that we are not able to determine the absolute
DIE offset using the information in the .debug_names section alone,
which means we have to offload some of this work to the users of this
class.
To demonstrate how this can be done, I've added/fixed the ability to
lookup entries using accelerator tables in DWO files in llvm-dwarfdump.
To make this happen, I've needed to make two extra changes in other
classes:
- made the DWARFContext method to lookup a CU based on the section
offset public. I've needed this functionality to lookup a CU, and this
seems like a useful thing in general.
- made DWARFUnit::getDWOId call extractDIEsIfNeeded. Before this, the
DWOId was filled in only if the root DIE happened to be parsed
before we called the accessor. Since the lazy parsing is supposed to
happen under the hood, calling extractDIEsIfNeeded seems appropriate.
Reviewers: JDevlieghere, aprantl, dblaikie
Subscribers: mgrang, llvm-commits
Differential Revision: https://reviews.llvm.org/D48009
llvm-svn: 334578
2018-06-13 16:14:27 +08:00
|
|
|
uint64_t DIEOffset = CUOffset + *EntryOr->getDIEUnitOffset();
|
2018-04-06 21:34:12 +08:00
|
|
|
DWARFDie DIE = DCtx.getDIEForOffset(DIEOffset);
|
|
|
|
if (!DIE) {
|
|
|
|
error() << formatv("Name Index @ {0:x}: Entry @ {1:x} references a "
|
|
|
|
"non-existing DIE @ {2:x}.\n",
|
|
|
|
NI.getUnitOffset(), EntryID, DIEOffset);
|
|
|
|
++NumErrors;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (DIE.getDwarfUnit()->getOffset() != CUOffset) {
|
|
|
|
error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched CU of "
|
|
|
|
"DIE @ {2:x}: index - {3:x}; debug_info - {4:x}.\n",
|
|
|
|
NI.getUnitOffset(), EntryID, DIEOffset, CUOffset,
|
|
|
|
DIE.getDwarfUnit()->getOffset());
|
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
if (DIE.getTag() != EntryOr->tag()) {
|
|
|
|
error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched Tag of "
|
|
|
|
"DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
|
|
|
|
NI.getUnitOffset(), EntryID, DIEOffset, EntryOr->tag(),
|
|
|
|
DIE.getTag());
|
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto EntryNames = getNames(DIE);
|
|
|
|
if (!is_contained(EntryNames, Str)) {
|
|
|
|
error() << formatv("Name Index @ {0:x}: Entry @ {1:x}: mismatched Name "
|
|
|
|
"of DIE @ {2:x}: index - {3}; debug_info - {4}.\n",
|
|
|
|
NI.getUnitOffset(), EntryID, DIEOffset, Str,
|
|
|
|
make_range(EntryNames.begin(), EntryNames.end()));
|
2018-05-14 22:13:20 +08:00
|
|
|
++NumErrors;
|
2018-04-06 21:34:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
handleAllErrors(EntryOr.takeError(),
|
|
|
|
[&](const DWARFDebugNames::SentinelError &) {
|
|
|
|
if (NumEntries > 0)
|
|
|
|
return;
|
|
|
|
error() << formatv("Name Index @ {0:x}: Name {1} ({2}) is "
|
|
|
|
"not associated with any entries.\n",
|
2018-06-01 18:33:11 +08:00
|
|
|
NI.getUnitOffset(), NTE.getIndex(), Str);
|
2018-04-06 21:34:12 +08:00
|
|
|
++NumErrors;
|
|
|
|
},
|
|
|
|
[&](const ErrorInfoBase &Info) {
|
2018-06-01 18:33:11 +08:00
|
|
|
error()
|
|
|
|
<< formatv("Name Index @ {0:x}: Name {1} ({2}): {3}\n",
|
|
|
|
NI.getUnitOffset(), NTE.getIndex(), Str,
|
|
|
|
Info.message());
|
2018-04-06 21:34:12 +08:00
|
|
|
++NumErrors;
|
|
|
|
});
|
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2018-05-15 21:24:10 +08:00
|
|
|
static bool isVariableIndexable(const DWARFDie &Die, DWARFContext &DCtx) {
|
|
|
|
Optional<DWARFFormValue> Location = Die.findRecursively(DW_AT_location);
|
|
|
|
if (!Location)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto ContainsInterestingOperators = [&](StringRef D) {
|
|
|
|
DWARFUnit *U = Die.getDwarfUnit();
|
|
|
|
DataExtractor Data(D, DCtx.isLittleEndian(), U->getAddressByteSize());
|
|
|
|
DWARFExpression Expression(Data, U->getVersion(), U->getAddressByteSize());
|
|
|
|
return any_of(Expression, [](DWARFExpression::Operation &Op) {
|
|
|
|
return !Op.isError() && (Op.getCode() == DW_OP_addr ||
|
|
|
|
Op.getCode() == DW_OP_form_tls_address ||
|
|
|
|
Op.getCode() == DW_OP_GNU_push_tls_address);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Optional<ArrayRef<uint8_t>> Expr = Location->getAsBlock()) {
|
|
|
|
// Inlined location.
|
|
|
|
if (ContainsInterestingOperators(toStringRef(*Expr)))
|
|
|
|
return true;
|
|
|
|
} else if (Optional<uint64_t> Offset = Location->getAsSectionOffset()) {
|
|
|
|
// Location list.
|
|
|
|
if (const DWARFDebugLoc *DebugLoc = DCtx.getDebugLoc()) {
|
|
|
|
if (const DWARFDebugLoc::LocationList *LocList =
|
|
|
|
DebugLoc->getLocationListAtOffset(*Offset)) {
|
|
|
|
if (any_of(LocList->Entries, [&](const DWARFDebugLoc::Entry &E) {
|
|
|
|
return ContainsInterestingOperators({E.Loc.data(), E.Loc.size()});
|
|
|
|
}))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned DWARFVerifier::verifyNameIndexCompleteness(
|
|
|
|
const DWARFDie &Die, const DWARFDebugNames::NameIndex &NI) {
|
|
|
|
|
|
|
|
// First check, if the Die should be indexed. The code follows the DWARF v5
|
|
|
|
// wording as closely as possible.
|
|
|
|
|
|
|
|
// "All non-defining declarations (that is, debugging information entries
|
|
|
|
// with a DW_AT_declaration attribute) are excluded."
|
|
|
|
if (Die.find(DW_AT_declaration))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// "DW_TAG_namespace debugging information entries without a DW_AT_name
|
|
|
|
// attribute are included with the name “(anonymous namespace)”.
|
|
|
|
// All other debugging information entries without a DW_AT_name attribute
|
|
|
|
// are excluded."
|
|
|
|
// "If a subprogram or inlined subroutine is included, and has a
|
|
|
|
// DW_AT_linkage_name attribute, there will be an additional index entry for
|
|
|
|
// the linkage name."
|
2018-09-03 20:12:17 +08:00
|
|
|
auto IncludeLinkageName = Die.getTag() == DW_TAG_subprogram ||
|
|
|
|
Die.getTag() == DW_TAG_inlined_subroutine;
|
|
|
|
auto EntryNames = getNames(Die, IncludeLinkageName);
|
2018-05-15 21:24:10 +08:00
|
|
|
if (EntryNames.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// We deviate from the specification here, which says:
|
|
|
|
// "The name index must contain an entry for each debugging information entry
|
|
|
|
// that defines a named subprogram, label, variable, type, or namespace,
|
|
|
|
// subject to ..."
|
|
|
|
// Instead whitelisting all TAGs representing a "type" or a "subprogram", to
|
|
|
|
// make sure we catch any missing items, we instead blacklist all TAGs that we
|
|
|
|
// know shouldn't be indexed.
|
|
|
|
switch (Die.getTag()) {
|
2018-08-03 20:01:43 +08:00
|
|
|
// Compile units and modules have names but shouldn't be indexed.
|
2018-05-15 21:24:10 +08:00
|
|
|
case DW_TAG_compile_unit:
|
2018-08-03 20:01:43 +08:00
|
|
|
case DW_TAG_module:
|
2018-05-15 21:24:10 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Function and template parameters are not globally visible, so we shouldn't
|
|
|
|
// index them.
|
|
|
|
case DW_TAG_formal_parameter:
|
|
|
|
case DW_TAG_template_value_parameter:
|
|
|
|
case DW_TAG_template_type_parameter:
|
|
|
|
case DW_TAG_GNU_template_parameter_pack:
|
|
|
|
case DW_TAG_GNU_template_template_param:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Object members aren't globally visible.
|
|
|
|
case DW_TAG_member:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// According to a strict reading of the specification, enumerators should not
|
|
|
|
// be indexed (and LLVM currently does not do that). However, this causes
|
|
|
|
// problems for the debuggers, so we may need to reconsider this.
|
|
|
|
case DW_TAG_enumerator:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Imported declarations should not be indexed according to the specification
|
|
|
|
// and LLVM currently does not do that.
|
|
|
|
case DW_TAG_imported_declaration:
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// "DW_TAG_subprogram, DW_TAG_inlined_subroutine, and DW_TAG_label debugging
|
|
|
|
// information entries without an address attribute (DW_AT_low_pc,
|
|
|
|
// DW_AT_high_pc, DW_AT_ranges, or DW_AT_entry_pc) are excluded."
|
|
|
|
case DW_TAG_subprogram:
|
|
|
|
case DW_TAG_inlined_subroutine:
|
|
|
|
case DW_TAG_label:
|
|
|
|
if (Die.findRecursively(
|
|
|
|
{DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges, DW_AT_entry_pc}))
|
|
|
|
break;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// "DW_TAG_variable debugging information entries with a DW_AT_location
|
|
|
|
// attribute that includes a DW_OP_addr or DW_OP_form_tls_address operator are
|
|
|
|
// included; otherwise, they are excluded."
|
|
|
|
//
|
|
|
|
// LLVM extension: We also add DW_OP_GNU_push_tls_address to this list.
|
|
|
|
case DW_TAG_variable:
|
|
|
|
if (isVariableIndexable(Die, DCtx))
|
|
|
|
break;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we know that our Die should be present in the Index. Let's check if
|
|
|
|
// that's the case.
|
|
|
|
unsigned NumErrors = 0;
|
[DWARF/AccelTable] Remove getDIESectionOffset for DWARF v5 entries
Summary:
This method was not correct for entries in DWO files as it assumed it
could just add up the CU and DIE offsets to get the absolute DIE offset.
This is not correct for the DWO files, as here the CU offset will
reference the skeleton unit, whereas the DIE offset will be the offset
in the full unit in the DWO file.
Unfortunately, this means that we are not able to determine the absolute
DIE offset using the information in the .debug_names section alone,
which means we have to offload some of this work to the users of this
class.
To demonstrate how this can be done, I've added/fixed the ability to
lookup entries using accelerator tables in DWO files in llvm-dwarfdump.
To make this happen, I've needed to make two extra changes in other
classes:
- made the DWARFContext method to lookup a CU based on the section
offset public. I've needed this functionality to lookup a CU, and this
seems like a useful thing in general.
- made DWARFUnit::getDWOId call extractDIEsIfNeeded. Before this, the
DWOId was filled in only if the root DIE happened to be parsed
before we called the accessor. Since the lazy parsing is supposed to
happen under the hood, calling extractDIEsIfNeeded seems appropriate.
Reviewers: JDevlieghere, aprantl, dblaikie
Subscribers: mgrang, llvm-commits
Differential Revision: https://reviews.llvm.org/D48009
llvm-svn: 334578
2018-06-13 16:14:27 +08:00
|
|
|
uint64_t DieUnitOffset = Die.getOffset() - Die.getDwarfUnit()->getOffset();
|
2018-05-15 21:24:10 +08:00
|
|
|
for (StringRef Name : EntryNames) {
|
[DWARF/AccelTable] Remove getDIESectionOffset for DWARF v5 entries
Summary:
This method was not correct for entries in DWO files as it assumed it
could just add up the CU and DIE offsets to get the absolute DIE offset.
This is not correct for the DWO files, as here the CU offset will
reference the skeleton unit, whereas the DIE offset will be the offset
in the full unit in the DWO file.
Unfortunately, this means that we are not able to determine the absolute
DIE offset using the information in the .debug_names section alone,
which means we have to offload some of this work to the users of this
class.
To demonstrate how this can be done, I've added/fixed the ability to
lookup entries using accelerator tables in DWO files in llvm-dwarfdump.
To make this happen, I've needed to make two extra changes in other
classes:
- made the DWARFContext method to lookup a CU based on the section
offset public. I've needed this functionality to lookup a CU, and this
seems like a useful thing in general.
- made DWARFUnit::getDWOId call extractDIEsIfNeeded. Before this, the
DWOId was filled in only if the root DIE happened to be parsed
before we called the accessor. Since the lazy parsing is supposed to
happen under the hood, calling extractDIEsIfNeeded seems appropriate.
Reviewers: JDevlieghere, aprantl, dblaikie
Subscribers: mgrang, llvm-commits
Differential Revision: https://reviews.llvm.org/D48009
llvm-svn: 334578
2018-06-13 16:14:27 +08:00
|
|
|
if (none_of(NI.equal_range(Name), [&](const DWARFDebugNames::Entry &E) {
|
|
|
|
return E.getDIEUnitOffset() == DieUnitOffset;
|
2018-05-15 21:24:10 +08:00
|
|
|
})) {
|
|
|
|
error() << formatv("Name Index @ {0:x}: Entry for DIE @ {1:x} ({2}) with "
|
|
|
|
"name {3} missing.\n",
|
|
|
|
NI.getUnitOffset(), Die.getOffset(), Die.getTag(),
|
|
|
|
Name);
|
|
|
|
++NumErrors;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2018-03-08 23:34:42 +08:00
|
|
|
unsigned DWARFVerifier::verifyDebugNames(const DWARFSection &AccelSection,
|
|
|
|
const DataExtractor &StrData) {
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
DWARFDataExtractor AccelSectionData(DCtx.getDWARFObj(), AccelSection,
|
|
|
|
DCtx.isLittleEndian(), 0);
|
|
|
|
DWARFDebugNames AccelTable(AccelSectionData, StrData);
|
|
|
|
|
|
|
|
OS << "Verifying .debug_names...\n";
|
|
|
|
|
|
|
|
// This verifies that we can read individual name indices and their
|
|
|
|
// abbreviation tables.
|
|
|
|
if (Error E = AccelTable.extract()) {
|
|
|
|
error() << toString(std::move(E)) << '\n';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
NumErrors += verifyDebugNamesCULists(AccelTable);
|
2018-03-16 18:02:16 +08:00
|
|
|
for (const auto &NI : AccelTable)
|
|
|
|
NumErrors += verifyNameIndexBuckets(NI, StrData);
|
2018-03-22 22:50:44 +08:00
|
|
|
for (const auto &NI : AccelTable)
|
|
|
|
NumErrors += verifyNameIndexAbbrevs(NI);
|
2018-03-08 23:34:42 +08:00
|
|
|
|
2018-04-06 21:34:12 +08:00
|
|
|
// Don't attempt Entry validation if any of the previous checks found errors
|
|
|
|
if (NumErrors > 0)
|
|
|
|
return NumErrors;
|
|
|
|
for (const auto &NI : AccelTable)
|
2018-06-01 18:33:11 +08:00
|
|
|
for (DWARFDebugNames::NameTableEntry NTE : NI)
|
|
|
|
NumErrors += verifyNameIndexEntries(NI, NTE);
|
2018-04-06 21:34:12 +08:00
|
|
|
|
2018-05-15 21:24:10 +08:00
|
|
|
if (NumErrors > 0)
|
|
|
|
return NumErrors;
|
|
|
|
|
2018-08-02 04:43:47 +08:00
|
|
|
for (const std::unique_ptr<DWARFUnit> &U : DCtx.compile_units()) {
|
2018-05-15 21:24:10 +08:00
|
|
|
if (const DWARFDebugNames::NameIndex *NI =
|
2018-08-02 04:43:47 +08:00
|
|
|
AccelTable.getCUNameIndex(U->getOffset())) {
|
|
|
|
auto *CU = cast<DWARFCompileUnit>(U.get());
|
2018-05-15 21:24:10 +08:00
|
|
|
for (const DWARFDebugInfoEntry &Die : CU->dies())
|
2018-08-02 04:43:47 +08:00
|
|
|
NumErrors += verifyNameIndexCompleteness(DWARFDie(CU, &Die), *NI);
|
2018-05-15 21:24:10 +08:00
|
|
|
}
|
|
|
|
}
|
2018-03-08 23:34:42 +08:00
|
|
|
return NumErrors;
|
|
|
|
}
|
|
|
|
|
2017-07-26 08:52:31 +08:00
|
|
|
bool DWARFVerifier::handleAccelTables() {
|
|
|
|
const DWARFObject &D = DCtx.getDWARFObj();
|
|
|
|
DataExtractor StrData(D.getStringSection(), DCtx.isLittleEndian(), 0);
|
|
|
|
unsigned NumErrors = 0;
|
|
|
|
if (!D.getAppleNamesSection().Data.empty())
|
2018-09-17 22:23:47 +08:00
|
|
|
NumErrors += verifyAppleAccelTable(&D.getAppleNamesSection(), &StrData,
|
|
|
|
".apple_names");
|
2017-07-26 08:52:31 +08:00
|
|
|
if (!D.getAppleTypesSection().Data.empty())
|
2018-09-17 22:23:47 +08:00
|
|
|
NumErrors += verifyAppleAccelTable(&D.getAppleTypesSection(), &StrData,
|
|
|
|
".apple_types");
|
2017-07-26 08:52:31 +08:00
|
|
|
if (!D.getAppleNamespacesSection().Data.empty())
|
2018-01-22 21:17:23 +08:00
|
|
|
NumErrors += verifyAppleAccelTable(&D.getAppleNamespacesSection(), &StrData,
|
2018-09-17 22:23:47 +08:00
|
|
|
".apple_namespaces");
|
2017-07-26 08:52:31 +08:00
|
|
|
if (!D.getAppleObjCSection().Data.empty())
|
2018-09-17 22:23:47 +08:00
|
|
|
NumErrors += verifyAppleAccelTable(&D.getAppleObjCSection(), &StrData,
|
|
|
|
".apple_objc");
|
2018-03-08 23:34:42 +08:00
|
|
|
|
|
|
|
if (!D.getDebugNamesSection().Data.empty())
|
|
|
|
NumErrors += verifyDebugNames(D.getDebugNamesSection(), StrData);
|
2017-07-26 08:52:31 +08:00
|
|
|
return NumErrors == 0;
|
2017-06-14 08:17:55 +08:00
|
|
|
}
|
2017-09-29 17:33:31 +08:00
|
|
|
|
2018-04-15 16:44:15 +08:00
|
|
|
raw_ostream &DWARFVerifier::error() const { return WithColor::error(OS); }
|
2017-09-29 17:33:31 +08:00
|
|
|
|
2018-04-15 16:44:15 +08:00
|
|
|
raw_ostream &DWARFVerifier::warn() const { return WithColor::warning(OS); }
|
2017-09-29 17:33:31 +08:00
|
|
|
|
2018-04-15 16:44:15 +08:00
|
|
|
raw_ostream &DWARFVerifier::note() const { return WithColor::note(OS); }
|
2018-09-19 16:08:13 +08:00
|
|
|
|
|
|
|
raw_ostream &DWARFVerifier::dump(const DWARFDie &Die, unsigned indent) const {
|
|
|
|
Die.dump(OS, indent, DumpOpts);
|
|
|
|
return OS;
|
|
|
|
}
|