[llvm-symbolizer] Make --relative-address work with DWARF contexts
[oota-llvm.git] / lib / DebugInfo / PDB / PDBContext.cpp
1 //===-- PDBContext.cpp ------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===/
9
10 #include "llvm/DebugInfo/PDB/PDBContext.h"
11 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
12 #include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
13 #include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
14 #include "llvm/DebugInfo/PDB/PDBSymbol.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
16 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
17 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
18 #include "llvm/Object/COFF.h"
19
20 using namespace llvm;
21 using namespace llvm::object;
22
23 PDBContext::PDBContext(const COFFObjectFile &Object,
24                        std::unique_ptr<IPDBSession> PDBSession)
25     : DIContext(CK_PDB), Session(std::move(PDBSession)) {
26   ErrorOr<uint64_t> ImageBase = Object.getImageBase();
27   if (ImageBase)
28     Session->setLoadAddress(ImageBase.get());
29 }
30
31 void PDBContext::dump(raw_ostream &OS, DIDumpType DumpType) {}
32
33 DILineInfo PDBContext::getLineInfoForAddress(uint64_t Address,
34                                              DILineInfoSpecifier Specifier) {
35   DILineInfo Result;
36   Result.FunctionName = getFunctionName(Address, Specifier.FNKind);
37
38   uint32_t Length = 1;
39   std::unique_ptr<PDBSymbol> Symbol =
40       Session->findSymbolByAddress(Address, PDB_SymType::None);
41   if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(Symbol.get())) {
42     Length = Func->getLength();
43   } else if (auto Data = dyn_cast_or_null<PDBSymbolData>(Symbol.get())) {
44     Length = Data->getLength();
45   }
46
47   // If we couldn't find a symbol, then just assume 1 byte, so that we get
48   // only the line number of the first instruction.
49   auto LineNumbers = Session->findLineNumbersByAddress(Address, Length);
50   if (!LineNumbers || LineNumbers->getChildCount() == 0)
51     return Result;
52
53   auto LineInfo = LineNumbers->getNext();
54   assert(LineInfo);
55   auto SourceFile = Session->getSourceFileById(LineInfo->getSourceFileId());
56
57   if (SourceFile &&
58       Specifier.FLIKind != DILineInfoSpecifier::FileLineInfoKind::None)
59     Result.FileName = SourceFile->getFileName();
60   Result.Column = LineInfo->getColumnNumber();
61   Result.Line = LineInfo->getLineNumber();
62   return Result;
63 }
64
65 DILineInfoTable
66 PDBContext::getLineInfoForAddressRange(uint64_t Address, uint64_t Size,
67                                        DILineInfoSpecifier Specifier) {
68   if (Size == 0)
69     return DILineInfoTable();
70
71   DILineInfoTable Table;
72   auto LineNumbers = Session->findLineNumbersByAddress(Address, Size);
73   if (!LineNumbers || LineNumbers->getChildCount() == 0)
74     return Table;
75
76   while (auto LineInfo = LineNumbers->getNext()) {
77     DILineInfo LineEntry =
78         getLineInfoForAddress(LineInfo->getVirtualAddress(), Specifier);
79     Table.push_back(std::make_pair(LineInfo->getVirtualAddress(), LineEntry));
80   }
81   return Table;
82 }
83
84 DIInliningInfo
85 PDBContext::getInliningInfoForAddress(uint64_t Address,
86                                       DILineInfoSpecifier Specifier) {
87   DIInliningInfo InlineInfo;
88   DILineInfo Frame = getLineInfoForAddress(Address, Specifier);
89   InlineInfo.addFrame(Frame);
90   return InlineInfo;
91 }
92
93 std::string PDBContext::getFunctionName(uint64_t Address,
94                                         DINameKind NameKind) const {
95   if (NameKind == DINameKind::None)
96     return std::string();
97
98   if (NameKind == DINameKind::LinkageName) {
99     // It is not possible to get the mangled linkage name through a
100     // PDBSymbolFunc.  For that we have to specifically request a
101     // PDBSymbolPublicSymbol.
102     auto PublicSym =
103         Session->findSymbolByAddress(Address, PDB_SymType::PublicSymbol);
104     if (auto PS = dyn_cast_or_null<PDBSymbolPublicSymbol>(PublicSym.get()))
105       return PS->getName();
106   }
107
108   auto FuncSymbol =
109       Session->findSymbolByAddress(Address, PDB_SymType::Function);
110
111   // This could happen either if there was no public symbol (e.g. not
112   // external) or the user requested the short name.  In the former case,
113   // although they technically requested the linkage name, if the linkage
114   // name is not available we fallback to at least returning a non-empty
115   // string.
116   if (auto Func = dyn_cast_or_null<PDBSymbolFunc>(FuncSymbol.get()))
117       return Func->getName();
118
119   return std::string();
120 }