blob: 36061d7e684fa0cbd6b2de621ec8d5db6d26254f [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"
Alexey Samsonova5f07682014-02-26 13:10:01 +000017#include "llvm/Object/ELFObjectFile.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000018#include "llvm/Object/MachO.h"
19#include "llvm/Support/Casting.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000020#include "llvm/Support/Compression.h"
21#include "llvm/Support/DataExtractor.h"
Rafael Espindola2a826e42014-06-13 17:20:48 +000022#include "llvm/Support/Errc.h"
Alexey Samsonov5239d582013-06-04 07:57:38 +000023#include "llvm/Support/FileSystem.h"
Alexey Samsonov3e9997f2013-08-14 17:09:30 +000024#include "llvm/Support/MemoryBuffer.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000025#include "llvm/Support/Path.h"
Alexey Samsonovea83baf2013-01-22 14:21:19 +000026#include <sstream>
Alexey Samsonova591ae62013-08-26 18:12:03 +000027#include <stdlib.h>
Alexey Samsonovea83baf2013-01-22 14:21:19 +000028
29namespace llvm {
30namespace symbolize {
31
Rafael Espindola4453e42942014-06-13 03:07:50 +000032static bool error(std::error_code ec) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000033 if (!ec)
34 return false;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000035 errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
36 return true;
37}
38
Alexey Samsonovdce67342014-05-15 21:24:32 +000039static DILineInfoSpecifier
40getDILineInfoSpecifier(const LLVMSymbolizer::Options &Opts) {
41 return DILineInfoSpecifier(
42 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
Alexey Samsonovcd014722014-05-17 00:07:48 +000043 Opts.PrintFunctions);
Alexey Samsonovea83baf2013-01-22 14:21:19 +000044}
45
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000046ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +000047 : Module(Obj), DebugInfoContext(DICtx) {
Jay Foad52695da2014-11-07 09:08:39 +000048 std::unique_ptr<DataExtractor> OpdExtractor;
49 uint64_t OpdAddress = 0;
50 // Find the .opd (function descriptor) section if any, for big-endian
51 // PowerPC64 ELF.
52 if (Module->getArch() == Triple::ppc64) {
53 for (section_iterator Section : Module->sections()) {
54 StringRef Name;
55 if (!error(Section->getName(Name)) && Name == ".opd") {
56 StringRef Data;
57 if (!error(Section->getContents(Data))) {
58 OpdExtractor.reset(new DataExtractor(Data, Module->isLittleEndian(),
59 Module->getBytesInAddress()));
60 OpdAddress = Section->getAddress();
61 }
62 break;
63 }
64 }
65 }
Alexey Samsonov464d2e42014-03-17 07:28:19 +000066 for (const SymbolRef &Symbol : Module->symbols()) {
Jay Foad52695da2014-11-07 09:08:39 +000067 addSymbol(Symbol, OpdExtractor.get(), OpdAddress);
Dmitry Vyukovef8fb722013-02-14 13:06:18 +000068 }
Alexey Samsonova5f07682014-02-26 13:10:01 +000069 bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
70 if (NoSymbolTable && Module->isELF()) {
71 // Fallback to dynamic symbol table, if regular symbol table is stripped.
72 std::pair<symbol_iterator, symbol_iterator> IDyn =
73 getELFDynamicSymbolIterators(Module);
74 for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
Jay Foad52695da2014-11-07 09:08:39 +000075 addSymbol(*si, OpdExtractor.get(), OpdAddress);
Alexey Samsonova5f07682014-02-26 13:10:01 +000076 }
77 }
78}
79
Jay Foad52695da2014-11-07 09:08:39 +000080void ModuleInfo::addSymbol(const SymbolRef &Symbol, DataExtractor *OpdExtractor,
81 uint64_t OpdAddress) {
Alexey Samsonova5f07682014-02-26 13:10:01 +000082 SymbolRef::Type SymbolType;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000083 if (error(Symbol.getType(SymbolType)))
Alexey Samsonova5f07682014-02-26 13:10:01 +000084 return;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000085 if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
Alexey Samsonova5f07682014-02-26 13:10:01 +000086 return;
87 uint64_t SymbolAddress;
Alexey Samsonov464d2e42014-03-17 07:28:19 +000088 if (error(Symbol.getAddress(SymbolAddress)) ||
Alexey Samsonova5f07682014-02-26 13:10:01 +000089 SymbolAddress == UnknownAddressOrSize)
90 return;
Jay Foad52695da2014-11-07 09:08:39 +000091 if (OpdExtractor) {
92 // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
93 // function descriptors. The first word of the descriptor is a pointer to
94 // the function's code.
95 // For the purposes of symbolization, pretend the symbol's address is that
96 // of the function's code, not the descriptor.
97 uint64_t OpdOffset = SymbolAddress - OpdAddress;
98 uint32_t OpdOffset32 = OpdOffset;
99 if (OpdOffset == OpdOffset32 &&
100 OpdExtractor->isValidOffsetForAddress(OpdOffset32))
101 SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
102 }
Alexey Samsonova5f07682014-02-26 13:10:01 +0000103 uint64_t SymbolSize;
104 // Getting symbol size is linear for Mach-O files, so assume that symbol
105 // occupies the memory range up to the following symbol.
106 if (isa<MachOObjectFile>(Module))
107 SymbolSize = 0;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000108 else if (error(Symbol.getSize(SymbolSize)) ||
Alexey Samsonova5f07682014-02-26 13:10:01 +0000109 SymbolSize == UnknownAddressOrSize)
110 return;
111 StringRef SymbolName;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000112 if (error(Symbol.getName(SymbolName)))
Alexey Samsonova5f07682014-02-26 13:10:01 +0000113 return;
114 // Mach-O symbol table names have leading underscore, skip it.
115 if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
116 SymbolName = SymbolName.drop_front();
117 // FIXME: If a function has alias, there are two entries in symbol table
118 // with same address size. Make sure we choose the correct one.
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000119 auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
Alexey Samsonova5f07682014-02-26 13:10:01 +0000120 SymbolDesc SD = { SymbolAddress, SymbolSize };
121 M.insert(std::make_pair(SD, SymbolName));
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000122}
123
124bool ModuleInfo::getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
125 std::string &Name, uint64_t &Addr,
126 uint64_t &Size) const {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000127 const auto &SymbolMap = Type == SymbolRef::ST_Function ? Functions : Objects;
128 if (SymbolMap.empty())
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000129 return false;
Alexey Samsonov5239d582013-06-04 07:57:38 +0000130 SymbolDesc SD = { Address, Address };
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000131 auto SymbolIterator = SymbolMap.upper_bound(SD);
132 if (SymbolIterator == SymbolMap.begin())
Alexey Samsonov35c987d2013-06-07 15:25:27 +0000133 return false;
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000134 --SymbolIterator;
135 if (SymbolIterator->first.Size != 0 &&
136 SymbolIterator->first.Addr + SymbolIterator->first.Size <= Address)
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000137 return false;
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000138 Name = SymbolIterator->second.str();
139 Addr = SymbolIterator->first.Addr;
140 Size = SymbolIterator->first.Size;
Dmitry Vyukovef8fb722013-02-14 13:06:18 +0000141 return true;
142}
143
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000144DILineInfo ModuleInfo::symbolizeCode(
145 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000146 DILineInfo LineInfo;
147 if (DebugInfoContext) {
148 LineInfo = DebugInfoContext->getLineInfoForAddress(
Alexey Samsonovdce67342014-05-15 21:24:32 +0000149 ModuleOffset, getDILineInfoSpecifier(Opts));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000150 }
151 // Override function name from symbol table if necessary.
Alexey Samsonovcd014722014-05-17 00:07:48 +0000152 if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000153 std::string FunctionName;
154 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000155 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
156 FunctionName, Start, Size)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000157 LineInfo.FunctionName = FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000158 }
159 }
160 return LineInfo;
161}
162
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000163DIInliningInfo ModuleInfo::symbolizeInlinedCode(
164 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000165 DIInliningInfo InlinedContext;
166 if (DebugInfoContext) {
167 InlinedContext = DebugInfoContext->getInliningInfoForAddress(
Alexey Samsonovdce67342014-05-15 21:24:32 +0000168 ModuleOffset, getDILineInfoSpecifier(Opts));
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000169 }
170 // Make sure there is at least one frame in context.
171 if (InlinedContext.getNumberOfFrames() == 0) {
172 InlinedContext.addFrame(DILineInfo());
173 }
174 // Override the function name in lower frame with name from symbol table.
Alexey Samsonovcd014722014-05-17 00:07:48 +0000175 if (Opts.PrintFunctions != FunctionNameKind::None && Opts.UseSymbolTable) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000176 DIInliningInfo PatchedInlinedContext;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000177 for (uint32_t i = 0, n = InlinedContext.getNumberOfFrames(); i < n; i++) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000178 DILineInfo LineInfo = InlinedContext.getFrame(i);
179 if (i == n - 1) {
180 std::string FunctionName;
181 uint64_t Start, Size;
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000182 if (getNameFromSymbolTable(SymbolRef::ST_Function, ModuleOffset,
183 FunctionName, Start, Size)) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000184 LineInfo.FunctionName = FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000185 }
186 }
187 PatchedInlinedContext.addFrame(LineInfo);
188 }
189 InlinedContext = PatchedInlinedContext;
190 }
191 return InlinedContext;
192}
193
194bool ModuleInfo::symbolizeData(uint64_t ModuleOffset, std::string &Name,
195 uint64_t &Start, uint64_t &Size) const {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000196 return getNameFromSymbolTable(SymbolRef::ST_Data, ModuleOffset, Name, Start,
197 Size);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000198}
199
Alexey Samsonovd6cef102013-02-04 15:55:26 +0000200const char LLVMSymbolizer::kBadString[] = "??";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000201
202std::string LLVMSymbolizer::symbolizeCode(const std::string &ModuleName,
203 uint64_t ModuleOffset) {
204 ModuleInfo *Info = getOrCreateModuleInfo(ModuleName);
Craig Toppere6cb63e2014-04-25 04:24:47 +0000205 if (!Info)
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000206 return printDILineInfo(DILineInfo());
207 if (Opts.PrintInlining) {
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000208 DIInliningInfo InlinedContext =
209 Info->symbolizeInlinedCode(ModuleOffset, Opts);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000210 uint32_t FramesNum = InlinedContext.getNumberOfFrames();
211 assert(FramesNum > 0);
212 std::string Result;
213 for (uint32_t i = 0; i < FramesNum; i++) {
214 DILineInfo LineInfo = InlinedContext.getFrame(i);
215 Result += printDILineInfo(LineInfo);
216 }
217 return Result;
218 }
219 DILineInfo LineInfo = Info->symbolizeCode(ModuleOffset, Opts);
220 return printDILineInfo(LineInfo);
221}
222
223std::string LLVMSymbolizer::symbolizeData(const std::string &ModuleName,
224 uint64_t ModuleOffset) {
225 std::string Name = kBadString;
226 uint64_t Start = 0;
227 uint64_t Size = 0;
228 if (Opts.UseSymbolTable) {
229 if (ModuleInfo *Info = getOrCreateModuleInfo(ModuleName)) {
Alexey Samsonov601beb72013-06-28 12:06:25 +0000230 if (Info->symbolizeData(ModuleOffset, Name, Start, Size) && Opts.Demangle)
Ed Masteef6fed72014-01-16 17:25:12 +0000231 Name = DemangleName(Name);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000232 }
233 }
234 std::stringstream ss;
235 ss << Name << "\n" << Start << " " << Size << "\n";
236 return ss.str();
237}
238
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000239void LLVMSymbolizer::flush() {
Alexey Samsonovdd71c5b2013-03-19 15:33:18 +0000240 DeleteContainerSeconds(Modules);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000241 ObjectPairForPathArch.clear();
Alexey Samsonovfe3a5d92013-06-28 15:08:29 +0000242 ObjectFileForArch.clear();
Dmitry Vyukove8504e22013-03-19 10:24:42 +0000243}
244
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000245// For Path="/path/to/foo" and Basename="foo" assume that debug info is in
246// /path/to/foo.dSYM/Contents/Resources/DWARF/foo.
247// For Path="/path/to/bar.dSYM" and Basename="foo" assume that debug info is in
248// /path/to/bar.dSYM/Contents/Resources/DWARF/foo.
249static
250std::string getDarwinDWARFResourceForPath(
251 const std::string &Path, const std::string &Basename) {
252 SmallString<16> ResourceName = StringRef(Path);
253 if (sys::path::extension(Path) != ".dSYM") {
254 ResourceName += ".dSYM";
255 }
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000256 sys::path::append(ResourceName, "Contents", "Resources", "DWARF");
257 sys::path::append(ResourceName, Basename);
258 return ResourceName.str();
259}
260
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000261static bool checkFileCRC(StringRef Path, uint32_t CRCHash) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000262 ErrorOr<std::unique_ptr<MemoryBuffer>> MB =
263 MemoryBuffer::getFileOrSTDIN(Path);
264 if (!MB)
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000265 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000266 return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer());
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000267}
268
269static bool findDebugBinary(const std::string &OrigPath,
270 const std::string &DebuglinkName, uint32_t CRCHash,
271 std::string &Result) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000272 std::string OrigRealPath = OrigPath;
273#if defined(HAVE_REALPATH)
Craig Toppere6cb63e2014-04-25 04:24:47 +0000274 if (char *RP = realpath(OrigPath.c_str(), nullptr)) {
Alexey Samsonova591ae62013-08-26 18:12:03 +0000275 OrigRealPath = RP;
276 free(RP);
277 }
278#endif
279 SmallString<16> OrigDir(OrigRealPath);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000280 llvm::sys::path::remove_filename(OrigDir);
281 SmallString<16> DebugPath = OrigDir;
282 // Try /path/to/original_binary/debuglink_name
283 llvm::sys::path::append(DebugPath, DebuglinkName);
284 if (checkFileCRC(DebugPath, CRCHash)) {
285 Result = DebugPath.str();
286 return true;
287 }
288 // Try /path/to/original_binary/.debug/debuglink_name
Alexey Samsonova591ae62013-08-26 18:12:03 +0000289 DebugPath = OrigRealPath;
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000290 llvm::sys::path::append(DebugPath, ".debug", DebuglinkName);
291 if (checkFileCRC(DebugPath, CRCHash)) {
292 Result = DebugPath.str();
293 return true;
294 }
295 // Try /usr/lib/debug/path/to/original_binary/debuglink_name
296 DebugPath = "/usr/lib/debug";
297 llvm::sys::path::append(DebugPath, llvm::sys::path::relative_path(OrigDir),
298 DebuglinkName);
299 if (checkFileCRC(DebugPath, CRCHash)) {
300 Result = DebugPath.str();
301 return true;
302 }
303 return false;
304}
305
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000306static bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000307 uint32_t &CRCHash) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000308 if (!Obj)
309 return false;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000310 for (const SectionRef &Section : Obj->sections()) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000311 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000312 Section.getName(Name);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000313 Name = Name.substr(Name.find_first_not_of("._"));
314 if (Name == "gnu_debuglink") {
315 StringRef Data;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000316 Section.getContents(Data);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000317 DataExtractor DE(Data, Obj->isLittleEndian(), 0);
318 uint32_t Offset = 0;
319 if (const char *DebugNameStr = DE.getCStr(&Offset)) {
320 // 4-byte align the offset.
321 Offset = (Offset + 3) & ~0x3;
322 if (DE.isValidOffsetForDataOfSize(Offset, 4)) {
323 DebugName = DebugNameStr;
324 CRCHash = DE.getU32(&Offset);
325 return true;
326 }
327 }
328 break;
329 }
330 }
331 return false;
332}
333
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000334static
335bool darwinDsymMatchesBinary(const MachOObjectFile *DbgObj,
336 const MachOObjectFile *Obj) {
337 ArrayRef<uint8_t> dbg_uuid = DbgObj->getUuid();
338 ArrayRef<uint8_t> bin_uuid = Obj->getUuid();
339 if (dbg_uuid.empty() || bin_uuid.empty())
340 return false;
341 return !memcmp(dbg_uuid.data(), bin_uuid.data(), dbg_uuid.size());
342}
343
344ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
345 const MachOObjectFile *MachExeObj, const std::string &ArchName) {
346 // On Darwin we may find DWARF in separate object file in
347 // resource directory.
348 std::vector<std::string> DsymPaths;
349 StringRef Filename = sys::path::filename(ExePath);
350 DsymPaths.push_back(getDarwinDWARFResourceForPath(ExePath, Filename));
351 for (const auto &Path : Opts.DsymHints) {
352 DsymPaths.push_back(getDarwinDWARFResourceForPath(Path, Filename));
353 }
354 for (const auto &path : DsymPaths) {
355 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(path);
356 std::error_code EC = BinaryOrErr.getError();
357 if (EC != errc::no_such_file_or_directory && !error(EC)) {
358 OwningBinary<Binary> B = std::move(BinaryOrErr.get());
359 ObjectFile *DbgObj =
Lang Hamesf04de6e2014-10-31 21:37:49 +0000360 getObjectFileFromBinary(B.getBinary(), ArchName);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000361 const MachOObjectFile *MachDbgObj =
362 dyn_cast<const MachOObjectFile>(DbgObj);
363 if (!MachDbgObj) continue;
364 if (darwinDsymMatchesBinary(MachDbgObj, MachExeObj)) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000365 addOwningBinary(std::move(B));
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000366 return DbgObj;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000367 }
368 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000369 }
370 return nullptr;
371}
372
373LLVMSymbolizer::ObjectPair
374LLVMSymbolizer::getOrCreateObjects(const std::string &Path,
375 const std::string &ArchName) {
376 const auto &I = ObjectPairForPathArch.find(std::make_pair(Path, ArchName));
377 if (I != ObjectPairForPathArch.end())
378 return I->second;
379 ObjectFile *Obj = nullptr;
380 ObjectFile *DbgObj = nullptr;
381 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
382 if (!error(BinaryOrErr.getError())) {
383 OwningBinary<Binary> &B = BinaryOrErr.get();
Lang Hamesf04de6e2014-10-31 21:37:49 +0000384 Obj = getObjectFileFromBinary(B.getBinary(), ArchName);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000385 if (!Obj) {
386 ObjectPair Res = std::make_pair(nullptr, nullptr);
387 ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
388 return Res;
389 }
390 addOwningBinary(std::move(B));
391 if (auto MachObj = dyn_cast<const MachOObjectFile>(Obj))
392 DbgObj = lookUpDsymFile(Path, MachObj, ArchName);
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000393 // Try to locate the debug binary using .gnu_debuglink section.
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000394 if (!DbgObj) {
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000395 std::string DebuglinkName;
396 uint32_t CRCHash;
397 std::string DebugBinaryPath;
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000398 if (getGNUDebuglinkContents(Obj, DebuglinkName, CRCHash) &&
Rafael Espindola63da2952014-01-15 19:37:43 +0000399 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
400 BinaryOrErr = createBinary(DebugBinaryPath);
401 if (!error(BinaryOrErr.getError())) {
Rafael Espindola48af1c22014-08-19 18:44:46 +0000402 OwningBinary<Binary> B = std::move(BinaryOrErr.get());
Lang Hamesf04de6e2014-10-31 21:37:49 +0000403 DbgObj = getObjectFileFromBinary(B.getBinary(), ArchName);
Rafael Espindola48af1c22014-08-19 18:44:46 +0000404 addOwningBinary(std::move(B));
Rafael Espindola63da2952014-01-15 19:37:43 +0000405 }
Alexey Samsonov3e9997f2013-08-14 17:09:30 +0000406 }
407 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000408 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000409 if (!DbgObj)
410 DbgObj = Obj;
411 ObjectPair Res = std::make_pair(Obj, DbgObj);
412 ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000413 return Res;
414}
415
416ObjectFile *
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000417LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin,
418 const std::string &ArchName) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000419 if (!Bin)
420 return nullptr;
421 ObjectFile *Res = nullptr;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000422 if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin)) {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000423 const auto &I = ObjectFileForArch.find(
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000424 std::make_pair(UB, ArchName));
425 if (I != ObjectFileForArch.end())
426 return I->second;
Rafael Espindola4f7932b2014-06-23 20:41:02 +0000427 ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj =
428 UB->getObjectForArch(Triple(ArchName).getArch());
429 if (ParsedObj) {
430 Res = ParsedObj.get().get();
431 ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get()));
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000432 }
433 ObjectFileForArch[std::make_pair(UB, ArchName)] = Res;
434 } else if (Bin->isObject()) {
435 Res = cast<ObjectFile>(Bin);
436 }
437 return Res;
438}
439
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000440ModuleInfo *
441LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
Alexander Potapenko45bfe372014-10-14 13:40:44 +0000442 const auto &I = Modules.find(ModuleName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000443 if (I != Modules.end())
444 return I->second;
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000445 std::string BinaryName = ModuleName;
446 std::string ArchName = Opts.DefaultArch;
Alexey Samsonovb119b462013-07-17 06:45:36 +0000447 size_t ColonPos = ModuleName.find_last_of(':');
448 // Verify that substring after colon form a valid arch name.
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000449 if (ColonPos != std::string::npos) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000450 std::string ArchStr = ModuleName.substr(ColonPos + 1);
NAKAMURA Takumi8ee89c62013-07-17 06:53:51 +0000451 if (Triple(ArchStr).getArch() != Triple::UnknownArch) {
Alexey Samsonovb119b462013-07-17 06:45:36 +0000452 BinaryName = ModuleName.substr(0, ColonPos);
453 ArchName = ArchStr;
454 }
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000455 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000456 ObjectPair Objects = getOrCreateObjects(BinaryName, ArchName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000457
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000458 if (!Objects.first) {
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000459 // Failed to find valid object file.
Craig Toppere6cb63e2014-04-25 04:24:47 +0000460 Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr));
461 return nullptr;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000462 }
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000463 DIContext *Context = DIContext::getDWARFContext(*Objects.second);
Alexey Samsonov2ca65362013-06-28 08:15:40 +0000464 assert(Context);
Alexander Potapenko7aaf5142014-10-17 00:50:19 +0000465 ModuleInfo *Info = new ModuleInfo(Objects.first, Context);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000466 Modules.insert(make_pair(ModuleName, Info));
467 return Info;
468}
469
470std::string LLVMSymbolizer::printDILineInfo(DILineInfo LineInfo) const {
471 // By default, DILineInfo contains "<invalid>" for function/filename it
472 // cannot fetch. We replace it to "??" to make our output closer to addr2line.
473 static const std::string kDILineInfoBadString = "<invalid>";
474 std::stringstream Result;
Alexey Samsonovcd014722014-05-17 00:07:48 +0000475 if (Opts.PrintFunctions != FunctionNameKind::None) {
Alexey Samsonovd0109992014-04-18 21:36:39 +0000476 std::string FunctionName = LineInfo.FunctionName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000477 if (FunctionName == kDILineInfoBadString)
478 FunctionName = kBadString;
Alexey Samsonov601beb72013-06-28 12:06:25 +0000479 else if (Opts.Demangle)
480 FunctionName = DemangleName(FunctionName);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000481 Result << FunctionName << "\n";
482 }
Alexey Samsonovd0109992014-04-18 21:36:39 +0000483 std::string Filename = LineInfo.FileName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000484 if (Filename == kDILineInfoBadString)
485 Filename = kBadString;
Alexey Samsonovd0109992014-04-18 21:36:39 +0000486 Result << Filename << ":" << LineInfo.Line << ":" << LineInfo.Column << "\n";
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000487 return Result.str();
488}
489
490#if !defined(_MSC_VER)
491// Assume that __cxa_demangle is provided by libcxxabi (except for Windows).
492extern "C" char *__cxa_demangle(const char *mangled_name, char *output_buffer,
493 size_t *length, int *status);
494#endif
495
Alexey Samsonov601beb72013-06-28 12:06:25 +0000496std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000497#if !defined(_MSC_VER)
Ed Masteef6fed72014-01-16 17:25:12 +0000498 // We can spoil names of symbols with C linkage, so use an heuristic
499 // approach to check if the name should be demangled.
500 if (Name.substr(0, 2) != "_Z")
501 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000502 int status = 0;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000503 char *DemangledName = __cxa_demangle(Name.c_str(), nullptr, nullptr, &status);
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000504 if (status != 0)
Alexey Samsonov601beb72013-06-28 12:06:25 +0000505 return Name;
506 std::string Result = DemangledName;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000507 free(DemangledName);
Alexey Samsonov601beb72013-06-28 12:06:25 +0000508 return Result;
509#else
510 return Name;
Alexey Samsonovea83baf2013-01-22 14:21:19 +0000511#endif
512}
513
Alexey Samsonovd5d7bb52013-02-15 08:54:47 +0000514} // namespace symbolize
515} // namespace llvm