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

source code of lld/Common/DWARF.cpp