1 //===-- LLVMSymbolize.h ----------------------------------------- C++ -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Header for LLVM symbolization library.
12 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H
14 #define LLVM_TOOLS_LLVM_SYMBOLIZER_LLVMSYMBOLIZE_H
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/DebugInfo/DIContext.h"
18 #include "llvm/Object/MachOUniversal.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/Support/DataExtractor.h"
21 #include "llvm/Support/MemoryBuffer.h"
28 typedef DILineInfoSpecifier::FunctionNameKind FunctionNameKind;
29 using namespace object;
35 class LLVMSymbolizer {
38 FunctionNameKind PrintFunctions;
39 bool UseSymbolTable : 1;
40 bool PrintInlining : 1;
42 bool RelativeAddresses : 1;
43 std::string DefaultArch;
44 std::vector<std::string> DsymHints;
45 Options(FunctionNameKind PrintFunctions = FunctionNameKind::LinkageName,
46 bool UseSymbolTable = true, bool PrintInlining = true,
47 bool Demangle = true, bool RelativeAddresses = false,
48 std::string DefaultArch = "")
49 : PrintFunctions(PrintFunctions), UseSymbolTable(UseSymbolTable),
50 PrintInlining(PrintInlining), Demangle(Demangle),
51 RelativeAddresses(RelativeAddresses), DefaultArch(DefaultArch) {}
54 LLVMSymbolizer(const Options &Opts = Options()) : Opts(Opts) {}
59 // Returns the result of symbolization for module name/offset as
60 // a string (possibly containing newlines).
62 symbolizeCode(const std::string &ModuleName, uint64_t ModuleOffset);
64 symbolizeData(const std::string &ModuleName, uint64_t ModuleOffset);
66 static std::string DemangleName(const std::string &Name, ModuleInfo *ModInfo);
69 typedef std::pair<ObjectFile*, ObjectFile*> ObjectPair;
71 ModuleInfo *getOrCreateModuleInfo(const std::string &ModuleName);
72 ObjectFile *lookUpDsymFile(const std::string &Path, const MachOObjectFile *ExeObj,
73 const std::string &ArchName);
75 /// \brief Returns pair of pointers to object and debug object.
76 ObjectPair getOrCreateObjects(const std::string &Path,
77 const std::string &ArchName);
78 /// \brief Returns a parsed object file for a given architecture in a
79 /// universal binary (or the binary itself if it is an object file).
80 ObjectFile *getObjectFileFromBinary(Binary *Bin, const std::string &ArchName);
82 std::string printDILineInfo(DILineInfo LineInfo, ModuleInfo *ModInfo) const;
84 // Owns all the parsed binaries and object files.
85 SmallVector<std::unique_ptr<Binary>, 4> ParsedBinariesAndObjects;
86 SmallVector<std::unique_ptr<MemoryBuffer>, 4> MemoryBuffers;
87 void addOwningBinary(OwningBinary<Binary> OwningBin) {
88 std::unique_ptr<Binary> Bin;
89 std::unique_ptr<MemoryBuffer> MemBuf;
90 std::tie(Bin, MemBuf) = OwningBin.takeBinary();
91 ParsedBinariesAndObjects.push_back(std::move(Bin));
92 MemoryBuffers.push_back(std::move(MemBuf));
95 // Owns module info objects.
96 std::map<std::string, ModuleInfo *> Modules;
97 std::map<std::pair<MachOUniversalBinary *, std::string>, ObjectFile *>
99 std::map<std::pair<std::string, std::string>, ObjectPair>
100 ObjectPairForPathArch;
103 static const char kBadString[];
108 ModuleInfo(ObjectFile *Obj, DIContext *DICtx);
110 DILineInfo symbolizeCode(uint64_t ModuleOffset,
111 const LLVMSymbolizer::Options &Opts) const;
112 DIInliningInfo symbolizeInlinedCode(
113 uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const;
114 bool symbolizeData(uint64_t ModuleOffset, std::string &Name, uint64_t &Start,
115 uint64_t &Size) const;
117 // Return true if this is a 32-bit x86 PE COFF module.
118 bool isWin32Module() const;
120 // Returns the preferred base of the module, i.e. where the loader would place
121 // it in memory assuming there were no conflicts.
122 uint64_t getModulePreferredBase() const;
125 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
126 std::string &Name, uint64_t &Addr,
127 uint64_t &Size) const;
128 // For big-endian PowerPC64 ELF, OpdAddress is the address of the .opd
129 // (function descriptor) section and OpdExtractor refers to its contents.
130 void addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
131 DataExtractor *OpdExtractor = nullptr,
132 uint64_t OpdAddress = 0);
133 void addCoffExportSymbols(const COFFObjectFile *CoffObj);
135 std::unique_ptr<DIContext> DebugInfoContext;
139 // If size is 0, assume that symbol occupies the whole memory range up to
140 // the following symbol.
142 friend bool operator<(const SymbolDesc &s1, const SymbolDesc &s2) {
143 return s1.Addr < s2.Addr;
146 std::map<SymbolDesc, StringRef> Functions;
147 std::map<SymbolDesc, StringRef> Objects;
150 } // namespace symbolize