1ece8a530Spatrick //===- DWARF.cpp ----------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick
9ece8a530Spatrick #include "lld/Common/DWARF.h"
10ece8a530Spatrick #include "lld/Common/ErrorHandler.h"
11ece8a530Spatrick
12ece8a530Spatrick using namespace llvm;
13ece8a530Spatrick
14ece8a530Spatrick namespace lld {
15ece8a530Spatrick
DWARFCache(std::unique_ptr<llvm::DWARFContext> d)16ece8a530Spatrick DWARFCache::DWARFCache(std::unique_ptr<llvm::DWARFContext> d)
17ece8a530Spatrick : dwarf(std::move(d)) {
18ece8a530Spatrick for (std::unique_ptr<DWARFUnit> &cu : dwarf->compile_units()) {
19ece8a530Spatrick auto report = [](Error err) {
20ece8a530Spatrick handleAllErrors(std::move(err),
21ece8a530Spatrick [](ErrorInfoBase &info) { warn(info.message()); });
22ece8a530Spatrick };
23ece8a530Spatrick Expected<const DWARFDebugLine::LineTable *> expectedLT =
24ece8a530Spatrick dwarf->getLineTableForUnit(cu.get(), report);
25ece8a530Spatrick const DWARFDebugLine::LineTable *lt = nullptr;
26ece8a530Spatrick if (expectedLT)
27ece8a530Spatrick lt = *expectedLT;
28ece8a530Spatrick else
29ece8a530Spatrick report(expectedLT.takeError());
30ece8a530Spatrick if (!lt)
31ece8a530Spatrick continue;
32ece8a530Spatrick lineTables.push_back(lt);
33ece8a530Spatrick
34ece8a530Spatrick // Loop over variable records and insert them to variableLoc.
35ece8a530Spatrick for (const auto &entry : cu->dies()) {
36ece8a530Spatrick DWARFDie die(cu.get(), &entry);
37ece8a530Spatrick // Skip all tags that are not variables.
38ece8a530Spatrick if (die.getTag() != dwarf::DW_TAG_variable)
39ece8a530Spatrick continue;
40ece8a530Spatrick
41ece8a530Spatrick // Skip if a local variable because we don't need them for generating
42ece8a530Spatrick // error messages. In general, only non-local symbols can fail to be
43ece8a530Spatrick // linked.
44ece8a530Spatrick if (!dwarf::toUnsigned(die.find(dwarf::DW_AT_external), 0))
45ece8a530Spatrick continue;
46ece8a530Spatrick
47ece8a530Spatrick // Get the source filename index for the variable.
48ece8a530Spatrick unsigned file = dwarf::toUnsigned(die.find(dwarf::DW_AT_decl_file), 0);
49ece8a530Spatrick if (!lt->hasFileAtIndex(file))
50ece8a530Spatrick continue;
51ece8a530Spatrick
52ece8a530Spatrick // Get the line number on which the variable is declared.
53ece8a530Spatrick unsigned line = dwarf::toUnsigned(die.find(dwarf::DW_AT_decl_line), 0);
54ece8a530Spatrick
55ece8a530Spatrick // Here we want to take the variable name to add it into variableLoc.
56ece8a530Spatrick // Variable can have regular and linkage name associated. At first, we try
57ece8a530Spatrick // to get linkage name as it can be different, for example when we have
58ece8a530Spatrick // two variables in different namespaces of the same object. Use common
59ece8a530Spatrick // name otherwise, but handle the case when it also absent in case if the
60ece8a530Spatrick // input object file lacks some debug info.
61ece8a530Spatrick StringRef name =
62ece8a530Spatrick dwarf::toString(die.find(dwarf::DW_AT_linkage_name),
63ece8a530Spatrick dwarf::toString(die.find(dwarf::DW_AT_name), ""));
64ece8a530Spatrick if (!name.empty())
65ece8a530Spatrick variableLoc.insert({name, {lt, file, line}});
66ece8a530Spatrick }
67ece8a530Spatrick }
68ece8a530Spatrick }
69ece8a530Spatrick
70ece8a530Spatrick // Returns the pair of file name and line number describing location of data
71ece8a530Spatrick // object (variable, array, etc) definition.
72*dfe94b16Srobert std::optional<std::pair<std::string, unsigned>>
getVariableLoc(StringRef name)73ece8a530Spatrick DWARFCache::getVariableLoc(StringRef name) {
74ece8a530Spatrick // Return if we have no debug information about data object.
75ece8a530Spatrick auto it = variableLoc.find(name);
76ece8a530Spatrick if (it == variableLoc.end())
77*dfe94b16Srobert return std::nullopt;
78ece8a530Spatrick
79ece8a530Spatrick // Take file name string from line table.
80ece8a530Spatrick std::string fileName;
81ece8a530Spatrick if (!it->second.lt->getFileNameByIndex(
82ece8a530Spatrick it->second.file, {},
83ece8a530Spatrick DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, fileName))
84*dfe94b16Srobert return std::nullopt;
85ece8a530Spatrick
86ece8a530Spatrick return std::make_pair(fileName, it->second.line);
87ece8a530Spatrick }
88ece8a530Spatrick
89ece8a530Spatrick // Returns source line information for a given offset
90ece8a530Spatrick // using DWARF debug info.
getDILineInfo(uint64_t offset,uint64_t sectionIndex)91*dfe94b16Srobert std::optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,
92ece8a530Spatrick uint64_t sectionIndex) {
93ece8a530Spatrick DILineInfo info;
94ece8a530Spatrick for (const llvm::DWARFDebugLine::LineTable *lt : lineTables) {
95ece8a530Spatrick if (lt->getFileLineInfoForAddress(
96ece8a530Spatrick {offset, sectionIndex}, nullptr,
97ece8a530Spatrick DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, info))
98ece8a530Spatrick return info;
99ece8a530Spatrick }
100*dfe94b16Srobert return std::nullopt;
101ece8a530Spatrick }
102ece8a530Spatrick
103ece8a530Spatrick } // namespace lld
104