Modernize getELFDynamicSymbolIterators.
[oota-llvm.git] / tools / llvm-symbolizer / LLVMSymbolize.cpp
index 31bbedf8f032b58e1d4ef34a8a99739b975c6e2e..eaf0d08f1052f47884d566946d623c694082b394 100644 (file)
@@ -14,6 +14,9 @@
 #include "LLVMSymbolize.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Config/config.h"
+#include "llvm/DebugInfo/DWARF/DWARFContext.h"
+#include "llvm/DebugInfo/PDB/PDB.h"
+#include "llvm/DebugInfo/PDB/PDBContext.h"
 #include "llvm/Object/ELFObjectFile.h"
 #include "llvm/Object/MachO.h"
 #include "llvm/Support/Casting.h"
 #include <sstream>
 #include <stdlib.h>
 
+#if defined(_MSC_VER)
+#include <Windows.h>
+#include <DbgHelp.h>
+#endif
+
 namespace llvm {
 namespace symbolize {
 
@@ -45,21 +53,39 @@ getDILineInfoSpecifier(const LLVMSymbolizer::Options &Opts) {
 
 ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
     : Module(Obj), DebugInfoContext(DICtx) {
+  std::unique_ptr<DataExtractor> OpdExtractor;
+  uint64_t OpdAddress = 0;
+  // Find the .opd (function descriptor) section if any, for big-endian
+  // PowerPC64 ELF.
+  if (Module->getArch() == Triple::ppc64) {
+    for (section_iterator Section : Module->sections()) {
+      StringRef Name;
+      if (!error(Section->getName(Name)) && Name == ".opd") {
+        StringRef Data;
+        if (!error(Section->getContents(Data))) {
+          OpdExtractor.reset(new DataExtractor(Data, Module->isLittleEndian(),
+                                               Module->getBytesInAddress()));
+          OpdAddress = Section->getAddress();
+        }
+        break;
+      }
+    }
+  }
   for (const SymbolRef &Symbol : Module->symbols()) {
-    addSymbol(Symbol);
+    addSymbol(Symbol, OpdExtractor.get(), OpdAddress);
   }
   bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
   if (NoSymbolTable && Module->isELF()) {
     // Fallback to dynamic symbol table, if regular symbol table is stripped.
-    std::pair<symbol_iterator, symbol_iterator> IDyn =
-        getELFDynamicSymbolIterators(Module);
-    for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
-      addSymbol(*si);
+    auto IDyn = cast<ELFObjectFileBase>(Module)->getDynamicSymbolIterators();
+    for (SymbolRef Sym : IDyn) {
+      addSymbol(Sym, OpdExtractor.get(), OpdAddress);
     }
   }
 }
 
-void ModuleInfo::addSymbol(const SymbolRef &Symbol) {
+void ModuleInfo::addSymbol(const SymbolRef &Symbol, DataExtractor *OpdExtractor,
+                           uint64_t OpdAddress) {
   SymbolRef::Type SymbolType;
   if (error(Symbol.getType(SymbolType)))
     return;
@@ -67,16 +93,27 @@ void ModuleInfo::addSymbol(const SymbolRef &Symbol) {
     return;
   uint64_t SymbolAddress;
   if (error(Symbol.getAddress(SymbolAddress)) ||
-      SymbolAddress == UnknownAddressOrSize)
+      SymbolAddress == UnknownAddress)
     return;
+  if (OpdExtractor) {
+    // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
+    // function descriptors. The first word of the descriptor is a pointer to
+    // the function's code.
+    // For the purposes of symbolization, pretend the symbol's address is that
+    // of the function's code, not the descriptor.
+    uint64_t OpdOffset = SymbolAddress - OpdAddress;
+    uint32_t OpdOffset32 = OpdOffset;
+    if (OpdOffset == OpdOffset32 && 
+        OpdExtractor->isValidOffsetForAddress(OpdOffset32))
+      SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
+  }
   uint64_t SymbolSize;
-  // Getting symbol size is linear for Mach-O files, so assume that symbol
-  // occupies the memory range up to the following symbol.
-  if (isa<MachOObjectFile>(Module))
+  // Onyl ELF has a size for every symbol so assume that symbol occupies the
+  // memory range up to the following symbol.
+  if (auto *E = dyn_cast<ELFObjectFileBase>(Module))
+    SymbolSize = E->getSymbolSize(Symbol);
+  else
     SymbolSize = 0;
-  else if (error(Symbol.getSize(SymbolSize)) ||
-           SymbolSize == UnknownAddressOrSize)
-    return;
   StringRef SymbolName;
   if (error(Symbol.getName(SymbolName)))
     return;
@@ -132,6 +169,7 @@ DILineInfo ModuleInfo::symbolizeCode(
 DIInliningInfo ModuleInfo::symbolizeInlinedCode(
     uint64_t ModuleOffset, const LLVMSymbolizer::Options &Opts) const {
   DIInliningInfo InlinedContext;
+
   if (DebugInfoContext) {
     InlinedContext = DebugInfoContext->getInliningInfoForAddress(
         ModuleOffset, getDILineInfoSpecifier(Opts));
@@ -326,7 +364,7 @@ ObjectFile *LLVMSymbolizer::lookUpDsymFile(const std::string &ExePath,
     if (EC != errc::no_such_file_or_directory && !error(EC)) {
       OwningBinary<Binary> B = std::move(BinaryOrErr.get());
       ObjectFile *DbgObj =
-          getObjectFileFromBinary(B.getBinary().get(), ArchName);
+          getObjectFileFromBinary(B.getBinary(), ArchName);
       const MachOObjectFile *MachDbgObj =
           dyn_cast<const MachOObjectFile>(DbgObj);
       if (!MachDbgObj) continue;
@@ -350,7 +388,7 @@ LLVMSymbolizer::getOrCreateObjects(const std::string &Path,
   ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(Path);
   if (!error(BinaryOrErr.getError())) {
     OwningBinary<Binary> &B = BinaryOrErr.get();
-    Obj = getObjectFileFromBinary(B.getBinary().get(), ArchName);
+    Obj = getObjectFileFromBinary(B.getBinary(), ArchName);
     if (!Obj) {
       ObjectPair Res = std::make_pair(nullptr, nullptr);
       ObjectPairForPathArch[std::make_pair(Path, ArchName)] = Res;
@@ -369,7 +407,7 @@ LLVMSymbolizer::getOrCreateObjects(const std::string &Path,
         BinaryOrErr = createBinary(DebugBinaryPath);
         if (!error(BinaryOrErr.getError())) {
           OwningBinary<Binary> B = std::move(BinaryOrErr.get());
-          DbgObj = getObjectFileFromBinary(B.getBinary().get(), ArchName);
+          DbgObj = getObjectFileFromBinary(B.getBinary(), ArchName);
           addOwningBinary(std::move(B));
         }
       }
@@ -394,7 +432,7 @@ LLVMSymbolizer::getObjectFileFromBinary(Binary *Bin,
     if (I != ObjectFileForArch.end())
       return I->second;
     ErrorOr<std::unique_ptr<ObjectFile>> ParsedObj =
-        UB->getObjectForArch(Triple(ArchName).getArch());
+        UB->getObjectForArch(ArchName);
     if (ParsedObj) {
       Res = ParsedObj.get().get();
       ParsedBinariesAndObjects.push_back(std::move(ParsedObj.get()));
@@ -429,7 +467,20 @@ LLVMSymbolizer::getOrCreateModuleInfo(const std::string &ModuleName) {
     Modules.insert(make_pair(ModuleName, (ModuleInfo *)nullptr));
     return nullptr;
   }
-  DIContext *Context = DIContext::getDWARFContext(*Objects.second);
+  DIContext *Context = nullptr;
+  if (auto CoffObject = dyn_cast<COFFObjectFile>(Objects.first)) {
+    // If this is a COFF object, assume it contains PDB debug information.  If
+    // we don't find any we will fall back to the DWARF case.
+    std::unique_ptr<IPDBSession> Session;
+    PDB_ErrorCode Error = loadDataForEXE(PDB_ReaderType::DIA,
+                                         Objects.first->getFileName(), Session);
+    if (Error == PDB_ErrorCode::Success) {
+      Context = new PDBContext(*CoffObject, std::move(Session),
+                               Opts.RelativeAddresses);
+    }
+  }
+  if (!Context)
+    Context = new DWARFContextInMemory(*Objects.second);
   assert(Context);
   ModuleInfo *Info = new ModuleInfo(Objects.first, Context);
   Modules.insert(make_pair(ModuleName, Info));
@@ -476,7 +527,17 @@ std::string LLVMSymbolizer::DemangleName(const std::string &Name) {
   free(DemangledName);
   return Result;
 #else
-  return Name;
+  char DemangledName[1024] = {0};
+  DWORD result = ::UnDecorateSymbolName(
+      Name.c_str(), DemangledName, 1023,
+      UNDNAME_NO_ACCESS_SPECIFIERS |       // Strip public, private, protected
+          UNDNAME_NO_ALLOCATION_LANGUAGE | // Strip __thiscall, __stdcall, etc
+          UNDNAME_NO_THROW_SIGNATURES |    // Strip throw() specifications
+          UNDNAME_NO_MEMBER_TYPE |      // Strip virtual, static, etc specifiers
+          UNDNAME_NO_MS_KEYWORDS |      // Strip all MS extension keywords
+          UNDNAME_NO_FUNCTION_RETURNS); // Strip function return types
+
+  return (result == 0) ? Name : std::string(DemangledName);
 #endif
 }