blob: 7e5ad329aca67652f42e47d0e6689a0dd276d1d2 [file] [log] [blame]
Alexey Samsonovea83baf2013-01-22 14:21:19 +00001//===-- LLVMSymbolize.cpp -------------------------------------------------===//
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// Implementation for LLVM symbolization library.
11//
12//===----------------------------------------------------------------------===//
13
Alexey Samsonov57f88372015-10-26 17:56:12 +000014#include "llvm/DebugInfo/Symbolize/Symbolize.h"
15
Alexey Samsonov8df3a072015-10-29 22:21:37 +000016#include "SymbolizableObjectFile.h"
17
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +000018#include "llvm/ADT/STLExtras.h"
Alexey Samsonova591ae62013-08-26 18:12:03 +000019#include "llvm/Config/config.h"
Zachary Turner6489d7b2015-04-23 17:37:47 +000020#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Zachary Turner20dbd0d2015-04-27 17:19:51 +000021#include "llvm/DebugInfo/PDB/PDB.h"
22#include "llvm/DebugInfo/PDB/PDBContext.h"
Alexey Samsonova5f07682014-02-26 13:10:01 +000023#include "llvm/Object/ELFObjectFile.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000024#include "llvm/Object/MachO.h"
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +000025#include "llvm/Object/MachOUniversal.h"
Reid Klecknerc25c7942015-08-10 21:47:11 +000026#include "llvm/Support/COFF.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000027#include "llvm/Support/Casting.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000028#include "llvm/Support/Compression.h"
29#include "llvm/Support/DataExtractor.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000030#include "llvm/Support/Errc.h"
Alexey Samsonov5239d582013-06-04 07:57:38 +000031#include "llvm/Support/FileSystem.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000032#include "llvm/Support/MemoryBuffer.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000033#include "llvm/Support/Path.h"
Eugene Zelenko35623fb2016-03-28 17:40:08 +000034#include <algorithm>
35#include <cassert>
36#include <cstdlib>
37#include <cstring>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000038
Zachary Turnerc007aa42015-05-06 22:26:30 +000039#if defined(_MSC_VER)
40#include <Windows.h>
41#include <DbgHelp.h>
Leny Kholodovbebb27b2015-07-02 14:34:57 +000042#pragma comment(lib, "dbghelp.lib")
Reid Klecknerc25c7942015-08-10 21:47:11 +000043
44// Windows.h conflicts with our COFF header definitions.
45#ifdef IMAGE_FILE_MACHINE_I386
46#undef IMAGE_FILE_MACHINE_I386
47#endif
Zachary Turnerc007aa42015-05-06 22:26:30 +000048#endif
49
Alexey Samsonovea83baf2013-01-22 14:21:19 +000050namespace llvm {
51namespace symbolize {
52
Alexey Samsonov884adda2015-11-04 00:30:24 +000053ErrorOr<DILineInfo> LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
54 uint64_t ModuleOffset) {
55 auto InfoOrErr = getOrCreateModuleInfo(ModuleName);
56 if (auto EC = InfoOrErr.getError())
57 return EC;
58 SymbolizableModule *Info = InfoOrErr.get();
Reid Klecknere94fef72015-10-09 00:15:01 +000059
60 // If the user is giving us relative addresses, add the preferred base of the
61 // object to the offset before we do the query. It's what DIContext expects.
62 if (Opts.RelativeAddresses)
63 ModuleOffset += Info->getModulePreferredBase();
64
Alexey Samsonov0fb64512015-10-26 22:34:56 +000065 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts.PrintFunctions,
66 Opts.UseSymbolTable);
Alexey Samsonov68812492015-11-03 21:36:13 +000067 if (Opts.Demangle)
68 LineInfo.FunctionName = DemangleName(LineInfo.FunctionName, Info);
Alexey Samsonovd6aa8202015-11-03 22:20:52 +000069 return LineInfo;
Alexey Samsonovea83baf2013-01-22 14:21:19 +000070}
71
Alexey Samsonov884adda2015-11-04 00:30:24 +000072ErrorOr<DIInliningInfo>
Alexey Samsonovd6aa8202015-11-03 22:20:52 +000073LLVMSymbolizer::symbolizeInlinedCode(const std::string &ModuleName,
74 uint64_t ModuleOffset) {
Alexey Samsonov884adda2015-11-04 00:30:24 +000075 auto InfoOrErr = getOrCreateModuleInfo(ModuleName);
76 if (auto EC = InfoOrErr.getError())
77 return EC;
78 SymbolizableModule *Info = InfoOrErr.get();
Alexey Samsonov46c1ce62015-10-30 00:40:20 +000079
80 // If the user is giving us relative addresses, add the preferred base of the
81 // object to the offset before we do the query. It's what DIContext expects.
82 if (Opts.RelativeAddresses)
83 ModuleOffset += Info->getModulePreferredBase();
84
85 DIInliningInfo InlinedContext = Info->symbolizeInlinedCode(
86 ModuleOffset, Opts.PrintFunctions, Opts.UseSymbolTable);
Alexey Samsonov68812492015-11-03 21:36:13 +000087 if (Opts.Demangle) {
88 for (int i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
89 auto *Frame = InlinedContext.getMutableFrame(i);
90 Frame->FunctionName = DemangleName(Frame->FunctionName, Info);
91 }
92 }
Alexey Samsonovd6aa8202015-11-03 22:20:52 +000093 return InlinedContext;
Alexey Samsonov46c1ce62015-10-30 00:40:20 +000094}
95
Alexey Samsonov884adda2015-11-04 00:30:24 +000096ErrorOr<DIGlobal> LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
97 uint64_t ModuleOffset) {
98 auto InfoOrErr = getOrCreateModuleInfo(ModuleName);
99 if (auto EC = InfoOrErr.getError())
100 return EC;
101 SymbolizableModule *Info = InfoOrErr.get();
Alexey Samsonov68812492015-11-03 21:36:13 +0000102
103 // If the user is giving us relative addresses, add the preferred base of
104 // the object to the offset before we do the query. It's what DIContext
105 // expects.
106 if (Opts.RelativeAddresses)
107 ModuleOffset += Info->getModulePreferredBase();
108
109 DIGlobal Global = Info->symbolizeData(ModuleOffset);
110 if (Opts.Demangle)
111 Global.Name = DemangleName(Global.Name, Info);
Alexey Samsonovd6aa8202015-11-03 22:20:52 +0000112 return Global;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000113}
114
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000115void LLVMSymbolizer::flush() {
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000116 ObjectForUBPathAndArch.clear();
117 BinaryForPath.clear();
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000118 ObjectPairForPathArch.clear();
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000119 Modules.clear();
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000120}
121
Eugene Zelenko35623fb2016-03-28 17:40:08 +0000122namespace {
123
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000124// For Path="/path/to/foo" and Basename="foo" assume that debug info is in
125// /path/to/foo.dSYM/Contents/Resources/DWARF/foo.
126// For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in
127// /path/to/bar.dSYM/Contents/Resources/DWARF/foo.
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000128std::string getDarwinDWARFResourceForPath(
129 const std::string &Path, const std::string &Basename) {
130 SmallString<16> ResourceName = StringRef(Path);
131 if (sys::path::extension(Path) != ".dSYM") {
132 ResourceName += ".dSYM";
133 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000134 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
135 sys::path::append(ResourceName, Basename);
136 return ResourceName.str();
137}
138
Eugene Zelenko35623fb2016-03-28 17:40:08 +0000139bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000140 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
141 MemoryBuffer::getFileOrSTDIN(Path);
142 if (!MB)
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000143 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000144 return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer());
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000145}
146
Eugene Zelenko35623fb2016-03-28 17:40:08 +0000147bool findDebugBinary(const std::string &OrigPath,
148 const std::string &DebuglinkName, uint32_t CRCHash,
149 std::string &Result) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000150 std::string OrigRealPath = OrigPath;
151#if defined(HAVE_REALPATH)
Craig Toppere6cb63e2014-04-25 04:24:47 +0000152 if (char *RP = realpath(OrigPath.c_str(), nullptr)) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000153 OrigRealPath = RP;
154 free(RP);
155 }
156#endif
157 SmallString<16> OrigDir(OrigRealPath);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000158 llvm::sys::path::remove_filename(OrigDir);
159 SmallString<16> DebugPath = OrigDir;
160 // Try /path/to/original_binary/debuglink_name
161 llvm::sys::path::append(DebugPath, DebuglinkName);
162 if (checkFileCRC(DebugPath, CRCHash)) {
163 Result = DebugPath.str();
164 return true;
165 }
166 // Try /path/to/original_binary/.debug/debuglink_name
Alexey Samsonova591ae62013-08-26 18:12:03 +0000167 DebugPath = OrigRealPath;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000168 llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
169 if (checkFileCRC(DebugPath, CRCHash)) {
170 Result = DebugPath.str();
171 return true;
172 }
173 // Try /usr/lib/debug/path/to/original_binary/debuglink_name
174 DebugPath = "/usr/lib/debug";
175 llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
176 DebuglinkName);
177 if (checkFileCRC(DebugPath, CRCHash)) {
178 Result = DebugPath.str();
179 return true;
180 }
181 return false;
182}
183
Eugene Zelenko35623fb2016-03-28 17:40:08 +0000184bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
185 uint32_t &CRCHash) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000186 if (!Obj)
187 return false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000188 for (const SectionRef &Section : Obj->sections()) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000189 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000190 Section.getName(Name);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000191 Name = Name.substr(Name.find_first_not_of("._"));
192 if (Name == "gnu_debuglink") {
193 StringRef Data;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000194 Section.getContents(Data);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000195 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
196 uint32_t Offset = 0;
197 if (const char *DebugNameStr = DE.getCStr(&Offset)) {
198 // 4-byte align the offset.
199 Offset = (Offset + 3) & ~0x3;
200 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
201 DebugName = DebugNameStr;
202 CRCHash = DE.getU32(&Offset);
203 return true;
204 }
205 }
206 break;
207 }
208 }
209 return false;
210}
211
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000212bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
213 const MachOObjectFile *Obj) {
214 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
215 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
216 if (dbg_uuid.empty() || bin_uuid.empty())
217 return false;
218 return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
219}
220
Eugene Zelenko35623fb2016-03-28 17:40:08 +0000221} // end anonymous namespace
222
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000223ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
224 const MachOObjectFile *MachExeObj, const std::string &ArchName) {
225 // On Darwin we may find DWARF in separate object file in
226 // resource directory.
227 std::vector<std::string> DsymPaths;
228 StringRef Filename = sys::path::filename(ExePath);
229 DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename));
230 for (const auto &Path : Opts.DsymHints) {
231 DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename));
232 }
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000233 for (const auto &Path : DsymPaths) {
234 auto DbgObjOrErr = getOrCreateObject(Path, ArchName);
Alexey Samsonov884adda2015-11-04 00:30:24 +0000235 if (!DbgObjOrErr)
236 continue;
237 ObjectFile *DbgObj = DbgObjOrErr.get();
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000238 const MachOObjectFile *MachDbgObj = dyn_cast<const MachOObjectFile>(DbgObj);
Alexey Samsonov884adda2015-11-04 00:30:24 +0000239 if (!MachDbgObj)
240 continue;
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000241 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj))
Alexey Samsonov884adda2015-11-04 00:30:24 +0000242 return DbgObj;
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000243 }
244 return nullptr;
245}
246
Alexey Samsonov5365a012015-11-04 00:30:26 +0000247ObjectFile *LLVMSymbolizer::lookUpDebuglinkObject(const std::string &Path,
248 const ObjectFile *Obj,
249 const std::string &ArchName) {
250 std::string DebuglinkName;
251 uint32_t CRCHash;
252 std::string DebugBinaryPath;
253 if (!getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash))
254 return nullptr;
255 if (!findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath))
256 return nullptr;
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000257 auto DbgObjOrErr = getOrCreateObject(DebugBinaryPath, ArchName);
Alexey Samsonov5365a012015-11-04 00:30:26 +0000258 if (!DbgObjOrErr)
259 return nullptr;
Alexey Samsonov5365a012015-11-04 00:30:26 +0000260 return DbgObjOrErr.get();
261}
262
Alexey Samsonov884adda2015-11-04 00:30:24 +0000263ErrorOr<LLVMSymbolizer::ObjectPair>
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000264LLVMSymbolizer::getOrCreateObjectPair(const std::string &Path,
265 const std::string &ArchName) {
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000266 const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
267 if (I != ObjectPairForPathArch.end())
268 return I->second;
Alexey Samsonov884adda2015-11-04 00:30:24 +0000269
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000270 auto ObjOrErr = getOrCreateObject(Path, ArchName);
Alexey Samsonov884adda2015-11-04 00:30:24 +0000271 if (auto EC = ObjOrErr.getError()) {
272 ObjectPairForPathArch.insert(
273 std::make_pair(std::make_pair(Path, ArchName), EC));
274 return EC;
275 }
Alexey Samsonov884adda2015-11-04 00:30:24 +0000276
277 ObjectFile *Obj = ObjOrErr.get();
278 assert(Obj != nullptr);
279 ObjectFile *DbgObj = nullptr;
280
281 if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
282 DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
Alexey Samsonov5365a012015-11-04 00:30:26 +0000283 if (!DbgObj)
284 DbgObj = lookUpDebuglinkObject(Path, Obj, ArchName);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000285 if (!DbgObj)
286 DbgObj = Obj;
287 ObjectPair Res = std::make_pair(Obj, DbgObj);
Alexey Samsonov884adda2015-11-04 00:30:24 +0000288 ObjectPairForPathArch.insert(
289 std::make_pair(std::make_pair(Path, ArchName), Res));
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000290 return Res;
291}
292
Alexey Samsonov884adda2015-11-04 00:30:24 +0000293ErrorOr<ObjectFile *>
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000294LLVMSymbolizer::getOrCreateObject(const std::string &Path,
295 const std::string &ArchName) {
296 const auto &I = BinaryForPath.find(Path);
297 Binary *Bin = nullptr;
298 if (I == BinaryForPath.end()) {
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000299 Expected<OwningBinary<Binary>> BinOrErr = createBinary(Path);
300 if (!BinOrErr) {
301 auto EC = errorToErrorCode(BinOrErr.takeError());
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000302 BinaryForPath.insert(std::make_pair(Path, EC));
Alexey Samsonov884adda2015-11-04 00:30:24 +0000303 return EC;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000304 }
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000305 Bin = BinOrErr->getBinary();
306 BinaryForPath.insert(std::make_pair(Path, std::move(BinOrErr.get())));
307 } else if (auto EC = I->second.getError()) {
308 return EC;
309 } else {
310 Bin = I->second->getBinary();
311 }
312
313 assert(Bin != nullptr);
314
315 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
316 const auto &I = ObjectForUBPathAndArch.find(std::make_pair(Path, ArchName));
317 if (I != ObjectForUBPathAndArch.end()) {
318 if (auto EC = I->second.getError())
319 return EC;
320 return I->second->get();
321 }
322 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
323 UB->getObjectForArch(ArchName);
324 if (auto EC = ObjOrErr.getError()) {
325 ObjectForUBPathAndArch.insert(
326 std::make_pair(std::make_pair(Path, ArchName), EC));
327 return EC;
328 }
329 ObjectFile *Res = ObjOrErr->get();
330 ObjectForUBPathAndArch.insert(std::make_pair(std::make_pair(Path, ArchName),
331 std::move(ObjOrErr.get())));
Alexey Samsonov884adda2015-11-04 00:30:24 +0000332 return Res;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000333 }
Alexey Samsonov884adda2015-11-04 00:30:24 +0000334 if (Bin->isObject()) {
335 return cast<ObjectFile>(Bin);
336 }
337 return object_error::arch_not_found;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000338}
339
Alexey Samsonov884adda2015-11-04 00:30:24 +0000340ErrorOr<SymbolizableModule *>
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000341LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000342 const auto &I = Modules.find(ModuleName);
Alexey Samsonov884adda2015-11-04 00:30:24 +0000343 if (I != Modules.end()) {
344 auto &InfoOrErr = I->second;
345 if (auto EC = InfoOrErr.getError())
346 return EC;
347 return InfoOrErr->get();
348 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000349 std::string BinaryName = ModuleName;
350 std::string ArchName = Opts.DefaultArch;
Alexey Samsonovb119b462013-07-17 06:45:36 +0000351 size_t ColonPos = ModuleName.find_last_of(':');
352 // Verify that substring after colon form a valid arch name.
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000353 if (ColonPos != std::string::npos) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000354 std::string ArchStr = ModuleName.substr(ColonPos + 1);
NAKAMURA Takumi8ee89c62013-07-17 06:53:51 +0000355 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000356 BinaryName = ModuleName.substr(0, ColonPos);
357 ArchName = ArchStr;
358 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000359 }
Alexey Samsonov1eaae4c2015-12-18 22:02:14 +0000360 auto ObjectsOrErr = getOrCreateObjectPair(BinaryName, ArchName);
Alexey Samsonov884adda2015-11-04 00:30:24 +0000361 if (auto EC = ObjectsOrErr.getError()) {
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000362 // Failed to find valid object file.
Alexey Samsonov884adda2015-11-04 00:30:24 +0000363 Modules.insert(std::make_pair(ModuleName, EC));
364 return EC;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000365 }
Alexey Samsonov884adda2015-11-04 00:30:24 +0000366 ObjectPair Objects = ObjectsOrErr.get();
367
Alexey Samsonov7a952e52015-10-26 19:41:23 +0000368 std::unique_ptr<DIContext> Context;
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000369 if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
Zachary Turnerec28fc32016-05-04 20:32:13 +0000370 using namespace pdb;
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000371 // If this is a COFF object, assume it contains PDB debug information. If
372 // we don't find any we will fall back to the DWARF case.
373 std::unique_ptr<IPDBSession> Session;
Zachary Turner819e77d2016-05-06 20:51:57 +0000374 auto Error = loadDataForEXE(
375 PDB_ReaderType::DIA, Objects.first->getFileName(), Session);
376 if (!Error) {
Alexey Samsonov7a952e52015-10-26 19:41:23 +0000377 Context.reset(new PDBContext(*CoffObject, std::move(Session)));
Zachary Turnerc007aa42015-05-06 22:26:30 +0000378 }
Zachary Turner20dbd0d2015-04-27 17:19:51 +0000379 }
380 if (!Context)
Alexey Samsonov7a952e52015-10-26 19:41:23 +0000381 Context.reset(new DWARFContextInMemory(*Objects.second));
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000382 assert(Context);
Alexey Samsonov884adda2015-11-04 00:30:24 +0000383 auto InfoOrErr =
Alexey Samsonov8df3a072015-10-29 22:21:37 +0000384 SymbolizableObjectFile::create(Objects.first, std::move(Context));
Alexey Samsonov884adda2015-11-04 00:30:24 +0000385 auto InsertResult =
386 Modules.insert(std::make_pair(ModuleName, std::move(InfoOrErr)));
387 assert(InsertResult.second);
388 if (auto EC = InsertResult.first->second.getError())
389 return EC;
390 return InsertResult.first->second->get();
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000391}
392
Eugene Zelenko35623fb2016-03-28 17:40:08 +0000393namespace {
394
Reid Klecknerc25c7942015-08-10 21:47:11 +0000395// Undo these various manglings for Win32 extern "C" functions:
396// cdecl - _foo
397// stdcall - _foo@12
398// fastcall - @foo@12
399// vectorcall - foo@@12
400// These are all different linkage names for 'foo'.
Eugene Zelenko35623fb2016-03-28 17:40:08 +0000401StringRef demanglePE32ExternCFunc(StringRef SymbolName) {
Reid Klecknerc25c7942015-08-10 21:47:11 +0000402 // Remove any '_' or '@' prefix.
403 char Front = SymbolName.empty() ? '\0' : SymbolName[0];
404 if (Front == '_' || Front == '@')
405 SymbolName = SymbolName.drop_front();
406
407 // Remove any '@[0-9]+' suffix.
408 if (Front != '?') {
409 size_t AtPos = SymbolName.rfind('@');
410 if (AtPos != StringRef::npos &&
411 std::all_of(SymbolName.begin() + AtPos + 1, SymbolName.end(),
412 [](char C) { return C >= '0' && C <= '9'; })) {
413 SymbolName = SymbolName.substr(0, AtPos);
414 }
415 }
416
417 // Remove any ending '@' for vectorcall.
418 if (SymbolName.endswith("@"))
419 SymbolName = SymbolName.drop_back();
420
421 return SymbolName;
422}
423
Eugene Zelenko35623fb2016-03-28 17:40:08 +0000424} // end anonymous namespace
425
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000426#if !defined(_MSC_VER)
427// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
428extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
429 size_t *length, int *status);
430#endif
431
Reid Klecknerc25c7942015-08-10 21:47:11 +0000432std::string LLVMSymbolizer::DemangleName(const std::string &Name,
Alexey Samsonov8df3a072015-10-29 22:21:37 +0000433 const SymbolizableModule *ModInfo) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000434#if !defined(_MSC_VER)
Ed Masteef6fed72014-01-16 17:25:12 +0000435 // We can spoil names of symbols with C linkage, so use an heuristic
436 // approach to check if the name should be demangled.
Reid Klecknerc25c7942015-08-10 21:47:11 +0000437 if (Name.substr(0, 2) == "_Z") {
438 int status = 0;
439 char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status);
440 if (status != 0)
441 return Name;
442 std::string Result = DemangledName;
443 free(DemangledName);
444 return Result;
445 }
Alexey Samsonov601beb72013-06-28 12:06:25 +0000446#else
Reid Klecknerc25c7942015-08-10 21:47:11 +0000447 if (!Name.empty() && Name.front() == '?') {
448 // Only do MSVC C++ demangling on symbols starting with '?'.
449 char DemangledName[1024] = {0};
450 DWORD result = ::UnDecorateSymbolName(
451 Name.c_str(), DemangledName, 1023,
452 UNDNAME_NO_ACCESS_SPECIFIERS | // Strip public, private, protected
453 UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc
454 UNDNAME_NO_THROW_SIGNATURES | // Strip throw() specifications
455 UNDNAME_NO_MEMBER_TYPE | // Strip virtual, static, etc specifiers
456 UNDNAME_NO_MS_KEYWORDS | // Strip all MS extension keywords
457 UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types
458 return (result == 0) ? Name : std::string(DemangledName);
459 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000460#endif
Alexey Samsonov76f7ecb2015-10-29 23:49:19 +0000461 if (ModInfo && ModInfo->isWin32Module())
Reid Klecknerc25c7942015-08-10 21:47:11 +0000462 return std::string(demanglePE32ExternCFunc(Name));
463 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000464}
465
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000466} // namespace symbolize
467} // namespace llvm