#include "lld/Common/DWARF.h"
#include "lld/Common/ErrorHandler.h"
using namespace llvm;
namespace lld {
DWARFCache::DWARFCache(std::unique_ptr<llvm::DWARFContext> d)
: dwarf(std::move(d)) {
for (std::unique_ptr<DWARFUnit> &cu : dwarf->compile_units()) {
auto report = [](Error err) {
handleAllErrors(std::move(err),
[](ErrorInfoBase &info) { warn(info.message()); });
};
Expected<const DWARFDebugLine::LineTable *> expectedLT =
dwarf->getLineTableForUnit(cu.get(), report);
const DWARFDebugLine::LineTable *lt = nullptr;
if (expectedLT)
lt = *expectedLT;
else
report(expectedLT.takeError());
if (!lt)
continue;
lineTables.push_back(lt);
for (const auto &entry : cu->dies()) {
DWARFDie die(cu.get(), &entry);
if (die.getTag() != dwarf::DW_TAG_variable)
continue;
if (!dwarf::toUnsigned(die.find(dwarf::DW_AT_external), 0))
continue;
unsigned file = dwarf::toUnsigned(die.find(dwarf::DW_AT_decl_file), 0);
if (!lt->hasFileAtIndex(file))
continue;
unsigned line = dwarf::toUnsigned(die.find(dwarf::DW_AT_decl_line), 0);
StringRef name =
dwarf::toString(die.find(dwarf::DW_AT_linkage_name),
dwarf::toString(die.find(dwarf::DW_AT_name), ""));
if (!name.empty())
variableLoc.insert({name, {lt, file, line}});
}
}
}
std::optional<std::pair<std::string, unsigned>>
DWARFCache::getVariableLoc(StringRef name) {
auto it = variableLoc.find(name);
if (it == variableLoc.end())
return std::nullopt;
std::string fileName;
if (!it->second.lt->getFileNameByIndex(
it->second.file, {},
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, fileName))
return std::nullopt;
return std::make_pair(fileName, it->second.line);
}
std::optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,
uint64_t sectionIndex) {
DILineInfo info;
for (const llvm::DWARFDebugLine::LineTable *lt : lineTables) {
if (lt->getFileLineInfoForAddress(
{offset, sectionIndex}, nullptr,
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, info))
return info;
}
return std::nullopt;
}
}