Path: blob/main/contrib/llvm-project/lld/Common/DWARF.cpp
34870 views
//===- DWARF.cpp ----------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lld/Common/DWARF.h"9#include "lld/Common/ErrorHandler.h"1011using namespace llvm;1213namespace lld {1415DWARFCache::DWARFCache(std::unique_ptr<llvm::DWARFContext> d)16: dwarf(std::move(d)) {17for (std::unique_ptr<DWARFUnit> &cu : dwarf->compile_units()) {18auto report = [](Error err) {19handleAllErrors(std::move(err),20[](ErrorInfoBase &info) { warn(info.message()); });21};22Expected<const DWARFDebugLine::LineTable *> expectedLT =23dwarf->getLineTableForUnit(cu.get(), report);24const DWARFDebugLine::LineTable *lt = nullptr;25if (expectedLT)26lt = *expectedLT;27else28report(expectedLT.takeError());29if (!lt)30continue;31lineTables.push_back(lt);3233// Loop over variable records and insert them to variableLoc.34for (const auto &entry : cu->dies()) {35DWARFDie die(cu.get(), &entry);36// Skip all tags that are not variables.37if (die.getTag() != dwarf::DW_TAG_variable)38continue;3940// Skip if a local variable because we don't need them for generating41// error messages. In general, only non-local symbols can fail to be42// linked.43if (!dwarf::toUnsigned(die.find(dwarf::DW_AT_external), 0))44continue;4546// Get the source filename index for the variable.47unsigned file = dwarf::toUnsigned(die.find(dwarf::DW_AT_decl_file), 0);48if (!lt->hasFileAtIndex(file))49continue;5051// Get the line number on which the variable is declared.52unsigned line = dwarf::toUnsigned(die.find(dwarf::DW_AT_decl_line), 0);5354// Here we want to take the variable name to add it into variableLoc.55// Variable can have regular and linkage name associated. At first, we try56// to get linkage name as it can be different, for example when we have57// two variables in different namespaces of the same object. Use common58// name otherwise, but handle the case when it also absent in case if the59// input object file lacks some debug info.60StringRef name =61dwarf::toString(die.find(dwarf::DW_AT_linkage_name),62dwarf::toString(die.find(dwarf::DW_AT_name), ""));63if (!name.empty())64variableLoc.insert({name, {lt, file, line}});65}66}67}6869// Returns the pair of file name and line number describing location of data70// object (variable, array, etc) definition.71std::optional<std::pair<std::string, unsigned>>72DWARFCache::getVariableLoc(StringRef name) {73// Return if we have no debug information about data object.74auto it = variableLoc.find(name);75if (it == variableLoc.end())76return std::nullopt;7778// Take file name string from line table.79std::string fileName;80if (!it->second.lt->getFileNameByIndex(81it->second.file, {},82DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, fileName))83return std::nullopt;8485return std::make_pair(fileName, it->second.line);86}8788// Returns source line information for a given offset89// using DWARF debug info.90std::optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,91uint64_t sectionIndex) {92DILineInfo info;93for (const llvm::DWARFDebugLine::LineTable *lt : lineTables) {94if (lt->getFileLineInfoForAddress(95{offset, sectionIndex}, nullptr,96DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, info))97return info;98}99return std::nullopt;100}101102} // namespace lld103104105