diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2015-01-18 16:17:27 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2015-01-18 16:17:27 +0000 |
commit | 67c32a98315f785a9ec9d531c1f571a0196c7463 (patch) | |
tree | 4abb9cbeecc7901726dd0b4a37369596c852e9ef /tools/dsymutil | |
parent | 9f61947910e6ab40de38e6b4034751ef1513200f (diff) |
Diffstat (limited to 'tools/dsymutil')
-rw-r--r-- | tools/dsymutil/BinaryHolder.cpp | 111 | ||||
-rw-r--r-- | tools/dsymutil/BinaryHolder.h | 104 | ||||
-rw-r--r-- | tools/dsymutil/CMakeLists.txt | 13 | ||||
-rw-r--r-- | tools/dsymutil/DebugMap.cpp | 80 | ||||
-rw-r--r-- | tools/dsymutil/DebugMap.h | 128 | ||||
-rw-r--r-- | tools/dsymutil/DwarfLinker.cpp | 20 | ||||
-rw-r--r-- | tools/dsymutil/LLVMBuild.txt | 22 | ||||
-rw-r--r-- | tools/dsymutil/MachODebugMapParser.cpp | 234 | ||||
-rw-r--r-- | tools/dsymutil/Makefile | 17 | ||||
-rw-r--r-- | tools/dsymutil/dsymutil.cpp | 71 | ||||
-rw-r--r-- | tools/dsymutil/dsymutil.h | 39 |
11 files changed, 839 insertions, 0 deletions
diff --git a/tools/dsymutil/BinaryHolder.cpp b/tools/dsymutil/BinaryHolder.cpp new file mode 100644 index 000000000000..ad66105bc249 --- /dev/null +++ b/tools/dsymutil/BinaryHolder.cpp @@ -0,0 +1,111 @@ +//===-- BinaryHolder.cpp --------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This program is a utility that aims to be a dropin replacement for +// Darwin's dsymutil. +// +//===----------------------------------------------------------------------===// + +#include "BinaryHolder.h" +#include "llvm/Support/raw_ostream.h" + +namespace llvm { +namespace dsymutil { + +ErrorOr<MemoryBufferRef> +BinaryHolder::GetMemoryBufferForFile(StringRef Filename) { + if (Verbose) + outs() << "trying to open '" << Filename << "'\n"; + + // Try that first as it doesn't involve any filesystem access. + if (auto ErrOrArchiveMember = GetArchiveMemberBuffer(Filename)) + return *ErrOrArchiveMember; + + // If the name ends with a closing paren, there is a huge chance + // it is an archive member specification. + if (Filename.endswith(")")) + if (auto ErrOrArchiveMember = MapArchiveAndGetMemberBuffer(Filename)) + return *ErrOrArchiveMember; + + // Otherwise, just try opening a standard file. If this is an + // archive member specifiaction and any of the above didn't handle it + // (either because the archive is not there anymore, or because the + // archive doesn't contain the requested member), this will still + // provide a sensible error message. + auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(Filename); + if (auto Err = ErrOrFile.getError()) + return Err; + + if (Verbose) + outs() << "\tloaded file.\n"; + CurrentArchive.reset(); + CurrentMemoryBuffer = std::move(ErrOrFile.get()); + return CurrentMemoryBuffer->getMemBufferRef(); +} + +ErrorOr<MemoryBufferRef> +BinaryHolder::GetArchiveMemberBuffer(StringRef Filename) { + if (!CurrentArchive) + return make_error_code(errc::no_such_file_or_directory); + + StringRef CurArchiveName = CurrentArchive->getFileName(); + if (!Filename.startswith(Twine(CurArchiveName, "(").str())) + return make_error_code(errc::no_such_file_or_directory); + + // Remove the archive name and the parens around the archive member name. + Filename = Filename.substr(CurArchiveName.size() + 1).drop_back(); + + for (const auto &Child : CurrentArchive->children()) { + if (auto NameOrErr = Child.getName()) + if (*NameOrErr == Filename) { + if (Verbose) + outs() << "\tfound member in current archive.\n"; + return Child.getMemoryBufferRef(); + } + } + + return make_error_code(errc::no_such_file_or_directory); +} + +ErrorOr<MemoryBufferRef> +BinaryHolder::MapArchiveAndGetMemberBuffer(StringRef Filename) { + StringRef ArchiveFilename = Filename.substr(0, Filename.find('(')); + + auto ErrOrBuff = MemoryBuffer::getFileOrSTDIN(ArchiveFilename); + if (auto Err = ErrOrBuff.getError()) + return Err; + + if (Verbose) + outs() << "\topened new archive '" << ArchiveFilename << "'\n"; + auto ErrOrArchive = object::Archive::create((*ErrOrBuff)->getMemBufferRef()); + if (auto Err = ErrOrArchive.getError()) + return Err; + + CurrentArchive = std::move(*ErrOrArchive); + CurrentMemoryBuffer = std::move(*ErrOrBuff); + + return GetArchiveMemberBuffer(Filename); +} + +ErrorOr<const object::ObjectFile &> +BinaryHolder::GetObjectFile(StringRef Filename) { + auto ErrOrMemBufferRef = GetMemoryBufferForFile(Filename); + if (auto Err = ErrOrMemBufferRef.getError()) + return Err; + + auto ErrOrObjectFile = + object::ObjectFile::createObjectFile(*ErrOrMemBufferRef); + if (auto Err = ErrOrObjectFile.getError()) + return Err; + + CurrentObjectFile = std::move(*ErrOrObjectFile); + return *CurrentObjectFile; +} +} +} diff --git a/tools/dsymutil/BinaryHolder.h b/tools/dsymutil/BinaryHolder.h new file mode 100644 index 000000000000..04871b5d5855 --- /dev/null +++ b/tools/dsymutil/BinaryHolder.h @@ -0,0 +1,104 @@ +//===-- BinaryHolder.h - Utility class for accessing binaries -------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This program is a utility that aims to be a dropin replacement for +// Darwin's dsymutil. +// +//===----------------------------------------------------------------------===// +#ifndef LLVM_TOOLS_DSYMUTIL_BINARYHOLDER_H +#define LLVM_TOOLS_DSYMUTIL_BINARYHOLDER_H + +#include "llvm/Object/Archive.h" +#include "llvm/Object/Error.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/ErrorOr.h" + +namespace llvm { +namespace dsymutil { + +/// \brief The BinaryHolder class is responsible for creating and +/// owning ObjectFile objects and their underlying MemoryBuffer. This +/// is different from a simple OwningBinary in that it handles +/// accessing to archive members. +/// +/// As an optimization, this class will reuse an already mapped and +/// parsed Archive object if 2 successive requests target the same +/// archive file (Which is always the case in debug maps). +/// Currently it only owns one memory buffer at any given time, +/// meaning that a mapping request will invalidate the previous memory +/// mapping. +class BinaryHolder { + std::unique_ptr<object::Archive> CurrentArchive; + std::unique_ptr<MemoryBuffer> CurrentMemoryBuffer; + std::unique_ptr<object::ObjectFile> CurrentObjectFile; + bool Verbose; + + /// \brief Get the MemoryBufferRef for the file specification in \p + /// Filename from the current archive. + /// + /// This function performs no system calls, it just looks up a + /// potential match for the given \p Filename in the currently + /// mapped archive if there is one. + ErrorOr<MemoryBufferRef> GetArchiveMemberBuffer(StringRef Filename); + + /// \brief Interpret Filename as an archive member specification, + /// map the corresponding archive to memory and return the + /// MemoryBufferRef corresponding to the described member. + ErrorOr<MemoryBufferRef> MapArchiveAndGetMemberBuffer(StringRef Filename); + + /// \brief Return the MemoryBufferRef that holds the memory + /// mapping for the given \p Filename. This function will try to + /// parse archive member specifications of the form + /// /path/to/archive.a(member.o). + /// + /// The returned MemoryBufferRef points to a buffer owned by this + /// object. The buffer is valid until the next call to + /// GetMemoryBufferForFile() on this object. + ErrorOr<MemoryBufferRef> GetMemoryBufferForFile(StringRef Filename); + +public: + BinaryHolder(bool Verbose) : Verbose(Verbose) {} + + /// \brief Get the ObjectFile designated by the \p Filename. This + /// might be an archive member specification of the form + /// /path/to/archive.a(member.o). + /// + /// Calling this function invalidates the previous mapping owned by + /// the BinaryHolder. + ErrorOr<const object::ObjectFile &> GetObjectFile(StringRef Filename); + + /// \brief Wraps GetObjectFile() to return a derived ObjectFile type. + template <typename ObjectFileType> + ErrorOr<const ObjectFileType &> GetFileAs(StringRef Filename) { + auto ErrOrObjFile = GetObjectFile(Filename); + if (auto Err = ErrOrObjFile.getError()) + return Err; + if (const auto *Derived = dyn_cast<ObjectFileType>(CurrentObjectFile.get())) + return *Derived; + return make_error_code(object::object_error::invalid_file_type); + } + + /// \brief Access the currently owned ObjectFile. As successfull + /// call to GetObjectFile() or GetFileAs() must have been performed + /// before calling this. + const object::ObjectFile &Get() { + assert(CurrentObjectFile); + return *CurrentObjectFile; + } + + /// \brief Access to a derived version of the currently owned + /// ObjectFile. The conversion must be known to be valid. + template <typename ObjectFileType> const ObjectFileType &GetAs() { + return cast<ObjectFileType>(*CurrentObjectFile); + } +}; +} +} +#endif diff --git a/tools/dsymutil/CMakeLists.txt b/tools/dsymutil/CMakeLists.txt new file mode 100644 index 000000000000..bfe7c706391a --- /dev/null +++ b/tools/dsymutil/CMakeLists.txt @@ -0,0 +1,13 @@ +set(LLVM_LINK_COMPONENTS + Object + Support + ) + +add_llvm_tool(llvm-dsymutil + dsymutil.cpp + BinaryHolder.cpp + DebugMap.cpp + DwarfLinker.cpp + MachODebugMapParser.cpp + ) + diff --git a/tools/dsymutil/DebugMap.cpp b/tools/dsymutil/DebugMap.cpp new file mode 100644 index 000000000000..7898160ae6b9 --- /dev/null +++ b/tools/dsymutil/DebugMap.cpp @@ -0,0 +1,80 @@ +//===- tools/dsymutil/DebugMap.cpp - Generic debug map representation -----===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +#include "DebugMap.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/iterator_range.h" +#include "llvm/Support/DataTypes.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" +#include <algorithm> + +namespace llvm { +namespace dsymutil { + +using namespace llvm::object; + +DebugMapObject::DebugMapObject(StringRef ObjectFilename) + : Filename(ObjectFilename) {} + +bool DebugMapObject::addSymbol(StringRef Name, uint64_t ObjectAddress, + uint64_t LinkedAddress) { + auto InsertResult = Symbols.insert( + std::make_pair(Name, SymbolMapping(ObjectAddress, LinkedAddress))); + return InsertResult.second; +} + +void DebugMapObject::print(raw_ostream &OS) const { + OS << getObjectFilename() << ":\n"; + // Sort the symbols in alphabetical order, like llvm-nm (and to get + // deterministic output for testing). + typedef std::pair<StringRef, SymbolMapping> Entry; + std::vector<Entry> Entries; + Entries.reserve(Symbols.getNumItems()); + for (const auto &Sym : make_range(Symbols.begin(), Symbols.end())) + Entries.push_back(std::make_pair(Sym.getKey(), Sym.getValue())); + std::sort( + Entries.begin(), Entries.end(), + [](const Entry &LHS, const Entry &RHS) { return LHS.first < RHS.first; }); + for (const auto &Sym : Entries) { + OS << format("\t%016" PRIx64 " => %016" PRIx64 "\t%s\n", + Sym.second.ObjectAddress, Sym.second.BinaryAddress, + Sym.first.data()); + } + OS << '\n'; +} + +#ifndef NDEBUG +void DebugMapObject::dump() const { print(errs()); } +#endif + +DebugMapObject &DebugMap::addDebugMapObject(StringRef ObjectFilePath) { + Objects.emplace_back(new DebugMapObject(ObjectFilePath)); + return *Objects.back(); +} + +const DebugMapObject::SymbolMapping * +DebugMapObject::lookupSymbol(StringRef SymbolName) const { + StringMap<SymbolMapping>::const_iterator Sym = Symbols.find(SymbolName); + if (Sym == Symbols.end()) + return nullptr; + return &Sym->getValue(); +} + +void DebugMap::print(raw_ostream &OS) const { + OS << "DEBUG MAP: object addr => executable addr\tsymbol name\n"; + for (const auto &Obj : objects()) + Obj->print(OS); + OS << "END DEBUG MAP\n"; +} + +#ifndef NDEBUG +void DebugMap::dump() const { print(errs()); } +#endif +} +} diff --git a/tools/dsymutil/DebugMap.h b/tools/dsymutil/DebugMap.h new file mode 100644 index 000000000000..54bff3272080 --- /dev/null +++ b/tools/dsymutil/DebugMap.h @@ -0,0 +1,128 @@ +//===- tools/dsymutil/DebugMap.h - Generic debug map representation -------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// +/// This file contains the class declaration of the DebugMap +/// entity. A DebugMap lists all the object files linked together to +/// produce an executable along with the linked address of all the +/// atoms used in these object files. +/// The DebugMap is an input to the DwarfLinker class that will +/// extract the Dwarf debug information from the referenced object +/// files and link their usefull debug info together. +/// +//===----------------------------------------------------------------------===// +#ifndef LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H +#define LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H + +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/iterator_range.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/Format.h" +#include <vector> + +namespace llvm { +class raw_ostream; + +namespace dsymutil { +class DebugMapObject; + +/// \brief The DebugMap object stores the list of object files to +/// query for debug information along with the mapping between the +/// symbols' addresses in the object file to their linked address in +/// the linked binary. +/// +/// A DebugMap producer could look like this: +/// DebugMap *DM = new DebugMap(); +/// for (const auto &Obj: LinkedObjects) { +/// DebugMapObject &DMO = DM->addDebugMapObject(Obj.getPath()); +/// for (const auto &Sym: Obj.getLinkedSymbols()) +/// DMO.addSymbol(Sym.getName(), Sym.getObjectFileAddress(), +/// Sym.getBinaryAddress()); +/// } +/// +/// A DebugMap consumer can then use the map to link the debug +/// information. For example something along the lines of: +/// for (const auto &DMO: DM->objects()) { +/// auto Obj = createBinary(DMO.getObjectFilename()); +/// for (auto &DIE: Obj.getDwarfDIEs()) { +/// if (SymbolMapping *Sym = DMO.lookup(DIE.getName())) +/// DIE.relocate(Sym->ObjectAddress, Sym->BinaryAddress); +/// else +/// DIE.discardSubtree(); +/// } +/// } +class DebugMap { + typedef std::vector<std::unique_ptr<DebugMapObject>> ObjectContainer; + ObjectContainer Objects; + +public: + typedef ObjectContainer::const_iterator const_iterator; + + iterator_range<const_iterator> objects() const { + return make_range(begin(), end()); + } + + const_iterator begin() const { return Objects.begin(); } + + const_iterator end() const { return Objects.end(); } + + /// This function adds an DebugMapObject to the list owned by this + /// debug map. + DebugMapObject &addDebugMapObject(StringRef ObjectFilePath); + + void print(raw_ostream &OS) const; + +#ifndef NDEBUG + void dump() const; +#endif +}; + +/// \brief The DebugMapObject represents one object file described by +/// the DebugMap. It contains a list of mappings between addresses in +/// the object file and in the linked binary for all the linked atoms +/// in this object file. +class DebugMapObject { +public: + struct SymbolMapping { + uint64_t ObjectAddress; + uint64_t BinaryAddress; + SymbolMapping(uint64_t ObjectAddress, uint64_t BinaryAddress) + : ObjectAddress(ObjectAddress), BinaryAddress(BinaryAddress) {} + }; + + /// \brief Adds a symbol mapping to this DebugMapObject. + /// \returns false if the symbol was already registered. The request + /// is discarded in this case. + bool addSymbol(llvm::StringRef SymName, uint64_t ObjectAddress, + uint64_t LinkedAddress); + + /// \brief Lookup a symbol mapping. + /// \returns null if the symbol isn't found. + const SymbolMapping *lookupSymbol(StringRef SymbolName) const; + + llvm::StringRef getObjectFilename() const { return Filename; } + + void print(raw_ostream &OS) const; +#ifndef NDEBUG + void dump() const; +#endif +private: + friend class DebugMap; + /// DebugMapObjects can only be constructed by the owning DebugMap. + DebugMapObject(StringRef ObjectFilename); + + std::string Filename; + StringMap<SymbolMapping> Symbols; +}; +} +} + +#endif // LLVM_TOOLS_DSYMUTIL_DEBUGMAP_H diff --git a/tools/dsymutil/DwarfLinker.cpp b/tools/dsymutil/DwarfLinker.cpp new file mode 100644 index 000000000000..ad471055bd9c --- /dev/null +++ b/tools/dsymutil/DwarfLinker.cpp @@ -0,0 +1,20 @@ +//===- tools/dsymutil/DwarfLinker.cpp - Dwarf debug info linker -----------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +#include "DebugMap.h" +#include "dsymutil.h" + +namespace llvm { +namespace dsymutil { + +bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, bool Verbose) { + // Do nothing for now. + return true; +} +} +} diff --git a/tools/dsymutil/LLVMBuild.txt b/tools/dsymutil/LLVMBuild.txt new file mode 100644 index 000000000000..24b9033c78c6 --- /dev/null +++ b/tools/dsymutil/LLVMBuild.txt @@ -0,0 +1,22 @@ +;===- ./tools/dsymutil/LLVMBuild.txt ---------------------*- Conf -*--===; +; +; The LLVM Compiler Infrastructure +; +; This file is distributed under the University of Illinois Open Source +; License. See LICENSE.TXT for details. +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Tool +name = llvm-dsymutil +parent = Tools +required_libraries = Object Support diff --git a/tools/dsymutil/MachODebugMapParser.cpp b/tools/dsymutil/MachODebugMapParser.cpp new file mode 100644 index 000000000000..6b244fc369e9 --- /dev/null +++ b/tools/dsymutil/MachODebugMapParser.cpp @@ -0,0 +1,234 @@ +//===- tools/dsymutil/MachODebugMapParser.cpp - Parse STABS debug maps ----===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "BinaryHolder.h" +#include "DebugMap.h" +#include "dsymutil.h" +#include "llvm/Object/MachO.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/raw_ostream.h" + +namespace { +using namespace llvm; +using namespace llvm::dsymutil; +using namespace llvm::object; + +class MachODebugMapParser { +public: + MachODebugMapParser(StringRef BinaryPath, StringRef PathPrefix = "", + bool Verbose = false) + : BinaryPath(BinaryPath), PathPrefix(PathPrefix), + MainBinaryHolder(Verbose), CurrentObjectHolder(Verbose), + CurrentDebugMapObject(nullptr) {} + + /// \brief Parses and returns the DebugMap of the input binary. + /// \returns an error in case the provided BinaryPath doesn't exist + /// or isn't of a supported type. + ErrorOr<std::unique_ptr<DebugMap>> parse(); + +private: + std::string BinaryPath; + std::string PathPrefix; + + /// Owns the MemoryBuffer for the main binary. + BinaryHolder MainBinaryHolder; + /// Map of the binary symbol addresses. + StringMap<uint64_t> MainBinarySymbolAddresses; + StringRef MainBinaryStrings; + /// The constructed DebugMap. + std::unique_ptr<DebugMap> Result; + + /// Owns the MemoryBuffer for the currently handled object file. + BinaryHolder CurrentObjectHolder; + /// Map of the currently processed object file symbol addresses. + StringMap<uint64_t> CurrentObjectAddresses; + /// Element of the debug map corresponfing to the current object file. + DebugMapObject *CurrentDebugMapObject; + + void switchToNewDebugMapObject(StringRef Filename); + void resetParserState(); + uint64_t getMainBinarySymbolAddress(StringRef Name); + void loadMainBinarySymbols(); + void loadCurrentObjectFileSymbols(); + void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type, + uint8_t SectionIndex, uint16_t Flags, + uint64_t Value); + + template <typename STEType> void handleStabDebugMapEntry(const STEType &STE) { + handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc, + STE.n_value); + } +}; + +static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; } +} + +/// Reset the parser state coresponding to the current object +/// file. This is to be called after an object file is finished +/// processing. +void MachODebugMapParser::resetParserState() { + CurrentObjectAddresses.clear(); + CurrentDebugMapObject = nullptr; +} + +/// Create a new DebugMapObject. This function resets the state of the +/// parser that was referring to the last object file and sets +/// everything up to add symbols to the new one. +void MachODebugMapParser::switchToNewDebugMapObject(StringRef Filename) { + resetParserState(); + + SmallString<80> Path(PathPrefix); + sys::path::append(Path, Filename); + + auto MachOOrError = CurrentObjectHolder.GetFileAs<MachOObjectFile>(Path); + if (auto Error = MachOOrError.getError()) { + Warning(Twine("cannot open debug object \"") + Path.str() + "\": " + + Error.message() + "\n"); + return; + } + + loadCurrentObjectFileSymbols(); + CurrentDebugMapObject = &Result->addDebugMapObject(Path); +} + +/// This main parsing routine tries to open the main binary and if +/// successful iterates over the STAB entries. The real parsing is +/// done in handleStabSymbolTableEntry. +ErrorOr<std::unique_ptr<DebugMap>> MachODebugMapParser::parse() { + auto MainBinOrError = MainBinaryHolder.GetFileAs<MachOObjectFile>(BinaryPath); + if (auto Error = MainBinOrError.getError()) + return Error; + + const MachOObjectFile &MainBinary = *MainBinOrError; + loadMainBinarySymbols(); + Result = make_unique<DebugMap>(); + MainBinaryStrings = MainBinary.getStringTableData(); + for (const SymbolRef &Symbol : MainBinary.symbols()) { + const DataRefImpl &DRI = Symbol.getRawDataRefImpl(); + if (MainBinary.is64Bit()) + handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI)); + else + handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI)); + } + + resetParserState(); + return std::move(Result); +} + +/// Interpret the STAB entries to fill the DebugMap. +void MachODebugMapParser::handleStabSymbolTableEntry(uint32_t StringIndex, + uint8_t Type, + uint8_t SectionIndex, + uint16_t Flags, + uint64_t Value) { + if (!(Type & MachO::N_STAB)) + return; + + const char *Name = &MainBinaryStrings.data()[StringIndex]; + + // An N_OSO entry represents the start of a new object file description. + if (Type == MachO::N_OSO) + return switchToNewDebugMapObject(Name); + + // If the last N_OSO object file wasn't found, + // CurrentDebugMapObject will be null. Do not update anything + // until we find the next valid N_OSO entry. + if (!CurrentDebugMapObject) + return; + + switch (Type) { + case MachO::N_GSYM: + // This is a global variable. We need to query the main binary + // symbol table to find its address as it might not be in the + // debug map (for common symbols). + Value = getMainBinarySymbolAddress(Name); + if (Value == UnknownAddressOrSize) + return; + break; + case MachO::N_FUN: + // Functions are scopes in STABS. They have an end marker that we + // need to ignore. + if (Name[0] == '\0') + return; + break; + case MachO::N_STSYM: + break; + default: + return; + } + + auto ObjectSymIt = CurrentObjectAddresses.find(Name); + if (ObjectSymIt == CurrentObjectAddresses.end()) + return Warning("could not find object file symbol for symbol " + + Twine(Name)); + if (!CurrentDebugMapObject->addSymbol(Name, ObjectSymIt->getValue(), Value)) + return Warning(Twine("failed to insert symbol '") + Name + + "' in the debug map."); +} + +/// Load the current object file symbols into CurrentObjectAddresses. +void MachODebugMapParser::loadCurrentObjectFileSymbols() { + CurrentObjectAddresses.clear(); + + for (auto Sym : CurrentObjectHolder.Get().symbols()) { + StringRef Name; + uint64_t Addr; + if (Sym.getAddress(Addr) || Addr == UnknownAddressOrSize || + Sym.getName(Name)) + continue; + CurrentObjectAddresses[Name] = Addr; + } +} + +/// Lookup a symbol address in the main binary symbol table. The +/// parser only needs to query common symbols, thus not every symbol's +/// address is available through this function. +uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) { + auto Sym = MainBinarySymbolAddresses.find(Name); + if (Sym == MainBinarySymbolAddresses.end()) + return UnknownAddressOrSize; + return Sym->second; +} + +/// Load the interesting main binary symbols' addresses into +/// MainBinarySymbolAddresses. +void MachODebugMapParser::loadMainBinarySymbols() { + const MachOObjectFile &MainBinary = MainBinaryHolder.GetAs<MachOObjectFile>(); + section_iterator Section = MainBinary.section_end(); + for (const auto &Sym : MainBinary.symbols()) { + SymbolRef::Type Type; + // Skip undefined and STAB entries. + if (Sym.getType(Type) || (Type & SymbolRef::ST_Debug) || + (Type & SymbolRef::ST_Unknown)) + continue; + StringRef Name; + uint64_t Addr; + // The only symbols of interest are the global variables. These + // are the only ones that need to be queried because the address + // of common data won't be described in the debug map. All other + // addresses should be fetched for the debug map. + if (Sym.getAddress(Addr) || Addr == UnknownAddressOrSize || + !(Sym.getFlags() & SymbolRef::SF_Global) || Sym.getSection(Section) || + Section->isText() || Sym.getName(Name) || Name.size() == 0 || + Name[0] == '\0') + continue; + MainBinarySymbolAddresses[Name] = Addr; + } +} + +namespace llvm { +namespace dsymutil { +llvm::ErrorOr<std::unique_ptr<DebugMap>> parseDebugMap(StringRef InputFile, + StringRef PrependPath, + bool Verbose) { + MachODebugMapParser Parser(InputFile, PrependPath, Verbose); + return Parser.parse(); +} +} +} diff --git a/tools/dsymutil/Makefile b/tools/dsymutil/Makefile new file mode 100644 index 000000000000..9eda7dcabc0a --- /dev/null +++ b/tools/dsymutil/Makefile @@ -0,0 +1,17 @@ +##===- tools/dsymutil/Makefile -----------------------------*- Makefile -*-===## +# +# The LLVM Compiler Infrastructure +# +# This file is distributed under the University of Illinois Open Source +# License. See LICENSE.TXT for details. +# +##===----------------------------------------------------------------------===## + +LEVEL := ../.. +TOOLNAME := llvm-dsymutil +LINK_COMPONENTS := Object Support + +# This tool has no plugins, optimize startup time. +TOOL_NO_EXPORTS := 1 + +include $(LEVEL)/Makefile.common diff --git a/tools/dsymutil/dsymutil.cpp b/tools/dsymutil/dsymutil.cpp new file mode 100644 index 000000000000..2b4fcfe07008 --- /dev/null +++ b/tools/dsymutil/dsymutil.cpp @@ -0,0 +1,71 @@ +//===-- dsymutil.cpp - Debug info dumping utility for llvm ----------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This program is a utility that aims to be a dropin replacement for +// Darwin's dsymutil. +// +//===----------------------------------------------------------------------===// + +#include "DebugMap.h" +#include "dsymutil.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/Options.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Signals.h" +#include "llvm/Support/raw_ostream.h" +#include <string> + +using namespace llvm::dsymutil; + +namespace { +using namespace llvm::cl; + +static opt<std::string> InputFile(Positional, desc("<input file>"), + init("a.out")); + +static opt<std::string> OsoPrependPath("oso-prepend-path", + desc("Specify a directory to prepend " + "to the paths of object files."), + value_desc("path")); + +static opt<bool> Verbose("v", desc("Verbosity level"), init(false)); + +static opt<bool> + ParseOnly("parse-only", + desc("Only parse the debug map, do not actaully link " + "the DWARF."), + init(false)); +} + +int main(int argc, char **argv) { + llvm::sys::PrintStackTraceOnErrorSignal(); + llvm::PrettyStackTraceProgram StackPrinter(argc, argv); + llvm::llvm_shutdown_obj Shutdown; + + llvm::cl::ParseCommandLineOptions(argc, argv, "llvm dsymutil\n"); + auto DebugMapPtrOrErr = parseDebugMap(InputFile, OsoPrependPath, Verbose); + + if (auto EC = DebugMapPtrOrErr.getError()) { + llvm::errs() << "error: cannot parse the debug map for \"" << InputFile + << "\": " << EC.message() << '\n'; + return 1; + } + + if (Verbose) + (*DebugMapPtrOrErr)->print(llvm::outs()); + + if (ParseOnly) + return 0; + + std::string OutputBasename(InputFile); + if (OutputBasename == "-") + OutputBasename = "a.out"; + + return !linkDwarf(OutputBasename + ".dwarf", **DebugMapPtrOrErr, Verbose); +} diff --git a/tools/dsymutil/dsymutil.h b/tools/dsymutil/dsymutil.h new file mode 100644 index 000000000000..9203beaf6774 --- /dev/null +++ b/tools/dsymutil/dsymutil.h @@ -0,0 +1,39 @@ +//===- tools/dsymutil/dsymutil.h - dsymutil high-level functionality ------===// +// +// The LLVM Linker +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +/// +/// \file +/// +/// This file contains the class declaration for the code that parses STABS +/// debug maps that are embedded in the binaries symbol tables. +/// +//===----------------------------------------------------------------------===// +#ifndef LLVM_TOOLS_DSYMUTIL_DSYMUTIL_H +#define LLVM_TOOLS_DSYMUTIL_DSYMUTIL_H + +#include "DebugMap.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/ErrorOr.h" +#include <memory> + +namespace llvm { +namespace dsymutil { +/// \brief Extract the DebugMap from the given file. +/// The file has to be a MachO object file. +llvm::ErrorOr<std::unique_ptr<DebugMap>> +parseDebugMap(StringRef InputFile, StringRef PrependPath = "", + bool Verbose = false); + +/// \brief Link the Dwarf debuginfo as directed by the passed DebugMap +/// \p DM into a DwarfFile named \p OutputFilename. +/// \returns false if the link failed. +bool linkDwarf(StringRef OutputFilename, const DebugMap &DM, + bool Verbose = false); +} +} +#endif // LLVM_TOOLS_DSYMUTIL_DSYMUTIL_H |