blob: c3988e136177ff5cd42cf47159cce2d766e1f9d6 [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
14#include "LLVMSymbolize.h"
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +000015#include "llvm/ADT/STLExtras.h"
Alexey Samsonova591ae62013-08-26 18:12:03 +000016#include "llvm/Config/config.h"
Zachary Turner6489d7b2015-04-23 17:37:47 +000017#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Alexey Samsonova5f07682014-02-26 13:10:01 +000018#include "llvm/Object/ELFObjectFile.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000019#include "llvm/Object/MachO.h"
20#include "llvm/Support/Casting.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000021#include "llvm/Support/Compression.h"
22#include "llvm/Support/DataExtractor.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000023#include "llvm/Support/Errc.h"
Alexey Samsonov5239d582013-06-04 07:57:38 +000024#include "llvm/Support/FileSystem.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000025#include "llvm/Support/MemoryBuffer.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000026#include "llvm/Support/Path.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000027#include <sstream>
Alexey Samsonova591ae62013-08-26 18:12:03 +000028#include <stdlib.h>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000029
30namespace llvm {
31namespace symbolize {
32
Rafael Espindola4453e42942014-06-13 03:07:50 +000033static bool error(std::error_code ec) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000034 if (!ec)
35 return false;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000036 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
37 return true;
38}
39
Alexey Samsonovdce67342014-05-15 21:24:32 +000040static DILineInfoSpecifier
41getDILineInfoSpecifier(const LLVMSymbolizer::Options &Opts) {
42 return DILineInfoSpecifier(
43 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
Alexey Samsonovcd014722014-05-17 00:07:48 +000044 Opts.PrintFunctions);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000045}
46
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000047ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000048 : Module(Obj), DebugInfoContext(DICtx) {
Jay Foad52695da2014-11-07 09:08:39 +000049 std::unique_ptr<DataExtractor> OpdExtractor;
50 uint64_t OpdAddress = 0;
51 // Find the .opd (function descriptor) section if any, for big-endian
52 // PowerPC64 ELF.
53 if (Module->getArch() == Triple::ppc64) {
54 for (section_iterator Section : Module->sections()) {
55 StringRef Name;
56 if (!error(Section->getName(Name)) && Name == ".opd") {
57 StringRef Data;
58 if (!error(Section->getContents(Data))) {
59 OpdExtractor.reset(new DataExtractor(Data, Module->isLittleEndian(),
60 Module->getBytesInAddress()));
61 OpdAddress = Section->getAddress();
62 }
63 break;
64 }
65 }
66 }
Alexey Samsonov464d2e42014-03-17 07:28:19 +000067 for (const SymbolRef &Symbol : Module->symbols()) {
Jay Foad52695da2014-11-07 09:08:39 +000068 addSymbol(Symbol, OpdExtractor.get(), OpdAddress);
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000069 }
Alexey Samsonova5f07682014-02-26 13:10:01 +000070 bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
71 if (NoSymbolTable && Module->isELF()) {
72 // Fallback to dynamic symbol table, if regular symbol table is stripped.
73 std::pair<symbol_iterator, symbol_iterator> IDyn =
74 getELFDynamicSymbolIterators(Module);
75 for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
Jay Foad52695da2014-11-07 09:08:39 +000076 addSymbol(*si, OpdExtractor.get(), OpdAddress);
Alexey Samsonova5f07682014-02-26 13:10:01 +000077 }
78 }
79}
80
Jay Foad52695da2014-11-07 09:08:39 +000081void ModuleInfo::addSymbol(const SymbolRef &Symbol, DataExtractor *OpdExtractor,
82 uint64_t OpdAddress) {
Alexey Samsonova5f07682014-02-26 13:10:01 +000083 SymbolRef::Type SymbolType;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000084 if (error(Symbol.getType(SymbolType)))
Alexey Samsonova5f07682014-02-26 13:10:01 +000085 return;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000086 if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
Alexey Samsonova5f07682014-02-26 13:10:01 +000087 return;
88 uint64_t SymbolAddress;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000089 if (error(Symbol.getAddress(SymbolAddress)) ||
Alexey Samsonova5f07682014-02-26 13:10:01 +000090 SymbolAddress == UnknownAddressOrSize)
91 return;
Jay Foad52695da2014-11-07 09:08:39 +000092 if (OpdExtractor) {
93 // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
94 // function descriptors. The first word of the descriptor is a pointer to
95 // the function's code.
96 // For the purposes of symbolization, pretend the symbol's address is that
97 // of the function's code, not the descriptor.
98 uint64_t OpdOffset = SymbolAddress - OpdAddress;
99 uint32_t OpdOffset32 = OpdOffset;
100 if (OpdOffset == OpdOffset32 &&
101 OpdExtractor->isValidOffsetForAddress(OpdOffset32))
102 SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
103 }
Alexey Samsonova5f07682014-02-26 13:10:01 +0000104 uint64_t SymbolSize;
105 // Getting symbol size is linear for Mach-O files, so assume that symbol
106 // occupies the memory range up to the following symbol.
107 if (isa<MachOObjectFile>(Module))
108 SymbolSize = 0;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000109 else if (error(Symbol.getSize(SymbolSize)) ||
Alexey Samsonova5f07682014-02-26 13:10:01 +0000110 SymbolSize == UnknownAddressOrSize)
111 return;
112 StringRef SymbolName;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000113 if (error(Symbol.getName(SymbolName)))
Alexey Samsonova5f07682014-02-26 13:10:01 +0000114 return;
115 // Mach-O symbol table names have leading underscore, skip it.
116 if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
117 SymbolName = SymbolName.drop_front();
118 // FIXME: If a function has alias, there are two entries in symbol table
119 // with same address size. Make sure we choose the correct one.
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000120 auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonova5f07682014-02-26 13:10:01 +0000121 SymbolDesc SD = { SymbolAddress, SymbolSize };
122 M.insert(std::make_pair(SD, SymbolName));
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000123}
124
125bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
126 std::string &Name, uint64_t &Addr,
127 uint64_t &Size) const {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000128 const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects;
129 if (SymbolMap.empty())
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000130 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +0000131 SymbolDesc SD = { Address, Address };
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000132 auto SymbolIterator = SymbolMap.upper_bound(SD);
133 if (SymbolIterator == SymbolMap.begin())
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000134 return false;
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000135 --SymbolIterator;
136 if (SymbolIterator->first.Size != 0 &&
137 SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000138 return false;
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000139 Name = SymbolIterator->second.str();
140 Addr = SymbolIterator->first.Addr;
141 Size = SymbolIterator->first.Size;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000142 return true;
143}
144
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000145DILineInfo ModuleInfo::symbolizeCode(
146 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000147 DILineInfo LineInfo;
148 if (DebugInfoContext) {
149 LineInfo = DebugInfoContext->getLineInfoForAddress(
Alexey Samsonovdce67342014-05-15 21:24:32 +0000150 ModuleOffset, getDILineInfoSpecifier(Opts));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000151 }
152 // Override function name from symbol table if necessary.
Alexey Samsonovcd014722014-05-17 00:07:48 +0000153 if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000154 std::string FunctionName;
155 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000156 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
157 FunctionName, Start, Size)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000158 LineInfo.FunctionName = FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000159 }
160 }
161 return LineInfo;
162}
163
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000164DIInliningInfo ModuleInfo::symbolizeInlinedCode(
165 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000166 DIInliningInfo InlinedContext;
167 if (DebugInfoContext) {
168 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
Alexey Samsonovdce67342014-05-15 21:24:32 +0000169 ModuleOffset, getDILineInfoSpecifier(Opts));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000170 }
171 // Make sure there is at least one frame in context.
172 if (InlinedContext.getNumberOfFrames() == 0) {
173 InlinedContext.addFrame(DILineInfo());
174 }
175 // Override the function name in lower frame with name from symbol table.
Alexey Samsonovcd014722014-05-17 00:07:48 +0000176 if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000177 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000178 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000179 DILineInfo LineInfo = InlinedContext.getFrame(i);
180 if (i == n - 1) {
181 std::string FunctionName;
182 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000183 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
184 FunctionName, Start, Size)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000185 LineInfo.FunctionName = FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000186 }
187 }
188 PatchedInlinedContext.addFrame(LineInfo);
189 }
190 InlinedContext = PatchedInlinedContext;
191 }
192 return InlinedContext;
193}
194
195bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
196 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000197 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
198 Size);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000199}
200
Alexey Samsonovd6cef102013-02-04 15:55:26 +0000201const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000202
203std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
204 uint64_t ModuleOffset) {
205 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000206 if (!Info)
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000207 return printDILineInfo(DILineInfo());
208 if (Opts.PrintInlining) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000209 DIInliningInfo InlinedContext =
210 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000211 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
212 assert(FramesNum > 0);
213 std::string Result;
214 for (uint32_t i = 0; i < FramesNum; i++) {
215 DILineInfo LineInfo = InlinedContext.getFrame(i);
216 Result += printDILineInfo(LineInfo);
217 }
218 return Result;
219 }
220 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
221 return printDILineInfo(LineInfo);
222}
223
224std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
225 uint64_t ModuleOffset) {
226 std::string Name = kBadString;
227 uint64_t Start = 0;
228 uint64_t Size = 0;
229 if (Opts.UseSymbolTable) {
230 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
Alexey Samsonov601beb72013-06-28 12:06:25 +0000231 if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
Ed Masteef6fed72014-01-16 17:25:12 +0000232 Name = DemangleName(Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000233 }
234 }
235 std::stringstream ss;
236 ss << Name << "\n" << Start << " " << Size << "\n";
237 return ss.str();
238}
239
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000240void LLVMSymbolizer::flush() {
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +0000241 DeleteContainerSeconds(Modules);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000242 ObjectPairForPathArch.clear();
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +0000243 ObjectFileForArch.clear();
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000244}
245
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000246// For Path="/path/to/foo" and Basename="foo" assume that debug info is in
247// /path/to/foo.dSYM/Contents/Resources/DWARF/foo.
248// For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in
249// /path/to/bar.dSYM/Contents/Resources/DWARF/foo.
250static
251std::string getDarwinDWARFResourceForPath(
252 const std::string &Path, const std::string &Basename) {
253 SmallString<16> ResourceName = StringRef(Path);
254 if (sys::path::extension(Path) != ".dSYM") {
255 ResourceName += ".dSYM";
256 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000257 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
258 sys::path::append(ResourceName, Basename);
259 return ResourceName.str();
260}
261
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000262static bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000263 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
264 MemoryBuffer::getFileOrSTDIN(Path);
265 if (!MB)
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000266 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000267 return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer());
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000268}
269
270static bool findDebugBinary(const std::string &OrigPath,
271 const std::string &DebuglinkName, uint32_t CRCHash,
272 std::string &Result) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000273 std::string OrigRealPath = OrigPath;
274#if defined(HAVE_REALPATH)
Craig Toppere6cb63e2014-04-25 04:24:47 +0000275 if (char *RP = realpath(OrigPath.c_str(), nullptr)) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000276 OrigRealPath = RP;
277 free(RP);
278 }
279#endif
280 SmallString<16> OrigDir(OrigRealPath);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000281 llvm::sys::path::remove_filename(OrigDir);
282 SmallString<16> DebugPath = OrigDir;
283 // Try /path/to/original_binary/debuglink_name
284 llvm::sys::path::append(DebugPath, DebuglinkName);
285 if (checkFileCRC(DebugPath, CRCHash)) {
286 Result = DebugPath.str();
287 return true;
288 }
289 // Try /path/to/original_binary/.debug/debuglink_name
Alexey Samsonova591ae62013-08-26 18:12:03 +0000290 DebugPath = OrigRealPath;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000291 llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
292 if (checkFileCRC(DebugPath, CRCHash)) {
293 Result = DebugPath.str();
294 return true;
295 }
296 // Try /usr/lib/debug/path/to/original_binary/debuglink_name
297 DebugPath = "/usr/lib/debug";
298 llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
299 DebuglinkName);
300 if (checkFileCRC(DebugPath, CRCHash)) {
301 Result = DebugPath.str();
302 return true;
303 }
304 return false;
305}
306
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000307static bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000308 uint32_t &CRCHash) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000309 if (!Obj)
310 return false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000311 for (const SectionRef &Section : Obj->sections()) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000312 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000313 Section.getName(Name);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000314 Name = Name.substr(Name.find_first_not_of("._"));
315 if (Name == "gnu_debuglink") {
316 StringRef Data;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000317 Section.getContents(Data);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000318 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
319 uint32_t Offset = 0;
320 if (const char *DebugNameStr = DE.getCStr(&Offset)) {
321 // 4-byte align the offset.
322 Offset = (Offset + 3) & ~0x3;
323 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
324 DebugName = DebugNameStr;
325 CRCHash = DE.getU32(&Offset);
326 return true;
327 }
328 }
329 break;
330 }
331 }
332 return false;
333}
334
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000335static
336bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
337 const MachOObjectFile *Obj) {
338 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
339 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
340 if (dbg_uuid.empty() || bin_uuid.empty())
341 return false;
342 return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
343}
344
345ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
346 const MachOObjectFile *MachExeObj, const std::string &ArchName) {
347 // On Darwin we may find DWARF in separate object file in
348 // resource directory.
349 std::vector<std::string> DsymPaths;
350 StringRef Filename = sys::path::filename(ExePath);
351 DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename));
352 for (const auto &Path : Opts.DsymHints) {
353 DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename));
354 }
355 for (const auto &path : DsymPaths) {
356 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(path);
357 std::error_code EC = BinaryOrErr.getError();
358 if (EC != errc::no_such_file_or_directory && !error(EC)) {
359 OwningBinary<Binary> B = std::move(BinaryOrErr.get());
360 ObjectFile *DbgObj =
Lang Hamesf04de6e2014-10-31 21:37:49 +0000361 getObjectFileFromBinary(B.getBinary(), ArchName);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000362 const MachOObjectFile *MachDbgObj =
363 dyn_cast<const MachOObjectFile>(DbgObj);
364 if (!MachDbgObj) continue;
365 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000366 addOwningBinary(std::move(B));
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000367 return DbgObj;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000368 }
369 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000370 }
371 return nullptr;
372}
373
374LLVMSymbolizer::ObjectPair
375LLVMSymbolizer::getOrCreateObjects(const std::string &Path,
376 const std::string &ArchName) {
377 const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
378 if (I != ObjectPairForPathArch.end())
379 return I->second;
380 ObjectFile *Obj = nullptr;
381 ObjectFile *DbgObj = nullptr;
382 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
383 if (!error(BinaryOrErr.getError())) {
384 OwningBinary<Binary> &B = BinaryOrErr.get();
Lang Hamesf04de6e2014-10-31 21:37:49 +0000385 Obj = getObjectFileFromBinary(B.getBinary(), ArchName);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000386 if (!Obj) {
387 ObjectPair Res = std::make_pair(nullptr, nullptr);
388 ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
389 return Res;
390 }
391 addOwningBinary(std::move(B));
392 if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
393 DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000394 // Try to locate the debug binary using .gnu_debuglink section.
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000395 if (!DbgObj) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000396 std::string DebuglinkName;
397 uint32_t CRCHash;
398 std::string DebugBinaryPath;
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000399 if (getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash) &&
Rafael Espindola63da2952014-01-15 19:37:43 +0000400 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
401 BinaryOrErr = createBinary(DebugBinaryPath);
402 if (!error(BinaryOrErr.getError())) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000403 OwningBinary<Binary> B = std::move(BinaryOrErr.get());
Lang Hamesf04de6e2014-10-31 21:37:49 +0000404 DbgObj = getObjectFileFromBinary(B.getBinary(), ArchName);
Rafael Espindola48af1c22014-08-19 18:44:46 +0000405 addOwningBinary(std::move(B));
Rafael Espindola63da2952014-01-15 19:37:43 +0000406 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000407 }
408 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000409 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000410 if (!DbgObj)
411 DbgObj = Obj;
412 ObjectPair Res = std::make_pair(Obj, DbgObj);
413 ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000414 return Res;
415}
416
417ObjectFile *
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000418LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin,
419 const std::string &ArchName) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000420 if (!Bin)
421 return nullptr;
422 ObjectFile *Res = nullptr;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000423 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000424 const auto &I = ObjectFileForArch.find(
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000425 std::make_pair(UB, ArchName));
426 if (I != ObjectFileForArch.end())
427 return I->second;
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000428 ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj =
429 UB->getObjectForArch(Triple(ArchName).getArch());
430 if (ParsedObj) {
431 Res = ParsedObj.get().get();
432 ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get()));
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000433 }
434 ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
435 } else if (Bin->isObject()) {
436 Res = cast<ObjectFile>(Bin);
437 }
438 return Res;
439}
440
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000441ModuleInfo *
442LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000443 const auto &I = Modules.find(ModuleName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000444 if (I != Modules.end())
445 return I->second;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000446 std::string BinaryName = ModuleName;
447 std::string ArchName = Opts.DefaultArch;
Alexey Samsonovb119b462013-07-17 06:45:36 +0000448 size_t ColonPos = ModuleName.find_last_of(':');
449 // Verify that substring after colon form a valid arch name.
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000450 if (ColonPos != std::string::npos) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000451 std::string ArchStr = ModuleName.substr(ColonPos + 1);
NAKAMURA Takumi8ee89c62013-07-17 06:53:51 +0000452 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000453 BinaryName = ModuleName.substr(0, ColonPos);
454 ArchName = ArchStr;
455 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000456 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000457 ObjectPair Objects = getOrCreateObjects(BinaryName, ArchName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000458
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000459 if (!Objects.first) {
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000460 // Failed to find valid object file.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000461 Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr));
462 return nullptr;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000463 }
Zachary Turner6489d7b2015-04-23 17:37:47 +0000464 DIContext *Context = new DWARFContextInMemory(*Objects.second);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000465 assert(Context);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000466 ModuleInfo *Info = new ModuleInfo(Objects.first, Context);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000467 Modules.insert(make_pair(ModuleName, Info));
468 return Info;
469}
470
471std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
472 // By default, DILineInfo contains "<invalid>" for function/filename it
473 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
474 static const std::string kDILineInfoBadString = "<invalid>";
475 std::stringstream Result;
Alexey Samsonovcd014722014-05-17 00:07:48 +0000476 if (Opts.PrintFunctions != FunctionNameKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000477 std::string FunctionName = LineInfo.FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000478 if (FunctionName == kDILineInfoBadString)
479 FunctionName = kBadString;
Alexey Samsonov601beb72013-06-28 12:06:25 +0000480 else if (Opts.Demangle)
481 FunctionName = DemangleName(FunctionName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000482 Result << FunctionName << "\n";
483 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000484 std::string Filename = LineInfo.FileName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000485 if (Filename == kDILineInfoBadString)
486 Filename = kBadString;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000487 Result << Filename << ":" << LineInfo.Line << ":" << LineInfo.Column << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000488 return Result.str();
489}
490
491#if !defined(_MSC_VER)
492// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
493extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
494 size_t *length, int *status);
495#endif
496
Alexey Samsonov601beb72013-06-28 12:06:25 +0000497std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000498#if !defined(_MSC_VER)
Ed Masteef6fed72014-01-16 17:25:12 +0000499 // We can spoil names of symbols with C linkage, so use an heuristic
500 // approach to check if the name should be demangled.
501 if (Name.substr(0, 2) != "_Z")
502 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000503 int status = 0;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000504 char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000505 if (status != 0)
Alexey Samsonov601beb72013-06-28 12:06:25 +0000506 return Name;
507 std::string Result = DemangledName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000508 free(DemangledName);
Alexey Samsonov601beb72013-06-28 12:06:25 +0000509 return Result;
510#else
511 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000512#endif
513}
514
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000515} // namespace symbolize
516} // namespace llvm