Index: tools/Makefile =================================================================== --- tools/Makefile +++ tools/Makefile @@ -33,7 +33,7 @@ macho-dump llvm-objdump llvm-readobj llvm-rtdyld \ llvm-dwarfdump llvm-cov llvm-size llvm-stress llvm-mcmarkup \ llvm-profdata llvm-symbolizer obj2yaml yaml2obj llvm-c-test \ - llvm-vtabledump verify-uselistorder + llvm-vtabledump verify-uselistorder dsymutil # If Intel JIT Events support is configured, build an extra tool to test it. ifeq ($(USE_INTEL_JITEVENTS), 1) Index: tools/dsymutil/CMakeLists.txt =================================================================== --- /dev/null +++ tools/dsymutil/CMakeLists.txt @@ -0,0 +1,12 @@ +set(LLVM_LINK_COMPONENTS + Object + Support + ) + +add_llvm_tool(llvm-dsymutil + dsymutil.cpp + DebugMap.cpp + DwarfLinker.cpp + MachODebugMapParser.cpp + ) + Index: tools/dsymutil/DebugMap.h =================================================================== --- /dev/null +++ tools/dsymutil/DebugMap.h @@ -0,0 +1,85 @@ +//===- 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 DSYMUTIL_DEBUGMAP_H +#define DSYMUTIL_DEBUGMAP_H + +#include "llvm/ADT/StringMap.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/Format.h" +#include "llvm/Support/raw_ostream.h" + +#include + +namespace llvm { +class DebugMapObject; + +class DebugMap { + std::vector> Objects; + +public: + const std::vector> &getObjects() const { + return Objects; + } + + void addDebugMapObject(std::unique_ptr &&NewObject); + void dump() const; +}; + +class DebugMapObject { + std::string Filename; + llvm::object::OwningBinary File; + + struct Symbol { + enum SymTy : uint8_t { + Unknown, + Variable, + Function, + }; + + uint64_t OrigAddress; + uint64_t Address; + SymTy Type; + bool Used; + }; + + llvm::StringMap Symbols; + +public: + DebugMapObject(llvm::StringRef Name, + llvm::object::OwningBinary &&File); + + llvm::StringRef getName() const { return Filename; } + + void addSymbol(llvm::StringRef Name, Symbol::SymTy Type, + uint64_t OrigAddress); + + bool relocateSymbol(llvm::StringRef Name, uint64_t Address); + + void dump() const; + +private: + void createSymbols(); +}; + +} + +#endif // DSYMUTIL_DEBUGMAP_H Index: tools/dsymutil/DebugMap.cpp =================================================================== --- /dev/null +++ tools/dsymutil/DebugMap.cpp @@ -0,0 +1,81 @@ +//===- 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" + +namespace llvm { + +using namespace llvm::object; + +DebugMapObject::DebugMapObject(StringRef Name, + OwningBinary &&Binary) + : Filename(Name), File(std::move(Binary)) { + createSymbols(); +} + +void DebugMapObject::addSymbol(StringRef Name, Symbol::SymTy Type, + uint64_t OrigAddress) { + Symbol &Sym = Symbols[Name]; + assert(Sym.Type == Symbol::Unknown && + "Symbol added twice in the same file."); + Sym.Type = Type; + Sym.OrigAddress = OrigAddress; +} + +bool DebugMapObject::relocateSymbol(StringRef Name, uint64_t Address) { + auto Sym = Symbols.find(Name); + if (Sym == Symbols.end()) + return false; + Sym->second.Address = Address; + Sym->second.Used = true; + return true; +} + +void DebugMapObject::dump() const { + errs() << "Object file " << getName() << '\n'; + auto End = Symbols.end(); + for (auto SymIt = Symbols.begin(); SymIt != End; ++SymIt) { + const auto &Sym = SymIt->getValue(); + errs() << format(" %s %s\t%016llx => %016llx (%s)\n", + Sym.Type == Symbol::Function ? "FUNC" : "VAR", + SymIt->getKeyData(), + Sym.OrigAddress, Sym.Address, + Sym.Used ? "USED" : "UNUSED"); + } +} + +void DebugMapObject::createSymbols() { + const auto &Binary = *File.getBinary(); + + section_iterator Section = Binary.section_end(); + section_iterator SectionEnd = Section; + for (auto Sym : Binary.symbols()) { + StringRef Name; + uint64_t Addr; + if (Sym.getAddress(Addr) || Addr == UnknownAddressOrSize || + Sym.getName(Name) || Sym.getSection(Section)) + continue; + // Section will be unknwon for common symbols. + addSymbol(Name, Section != SectionEnd && Section->isText() + ? Symbol::Function + : Symbol::Variable, + Addr); + } +} + +void DebugMap::addDebugMapObject(std::unique_ptr &&NewObject) { + Objects.push_back(std::move(NewObject)); +} + +void DebugMap::dump() const { + for (const auto &Obj: Objects) + Obj->dump(); +} + + +} Index: tools/dsymutil/DwarfLinker.h =================================================================== --- /dev/null +++ tools/dsymutil/DwarfLinker.h @@ -0,0 +1,40 @@ +//===- tools/dsymutil/DwarfLinker.h - Dwarf debug info linker -------------===// +// +// 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 DwarfLinker +/// object. A DwarfLinker takes a DebugMap as input and links the +/// debug information of all the referenced object files together. It +/// may drop and rewrite some parts of the debug info tree in the +/// process. +/// +//===----------------------------------------------------------------------===// +#ifndef DSYMUTIL_DWARFLINKER_H +#define DSYMUTIL_DWARFLINKER_H + +#include "llvm/ADT/StringRef.h" + +namespace llvm { + +class DebugMap; + +class DwarfLinker { + std::string OutputFilename; +public: + DwarfLinker(StringRef OutputFilename); + + /// \brief Link the passed debug map into the ouptut file. + /// \returns false if the link encountered a fatal error. + bool link(const DebugMap&); +}; + +} + +#endif Index: tools/dsymutil/DwarfLinker.cpp =================================================================== --- /dev/null +++ tools/dsymutil/DwarfLinker.cpp @@ -0,0 +1,22 @@ +//===- 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 "DwarfLinker.h" +#include "DebugMap.h" + +namespace llvm { + +DwarfLinker::DwarfLinker(StringRef Filename) + : OutputFilename(Filename) +{} + +bool DwarfLinker::link(const DebugMap &Map) { + return true; +} + +} Index: tools/dsymutil/LLVMBuild.txt =================================================================== --- /dev/null +++ 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 = DebugInfo Object Index: tools/dsymutil/MachODebugMapParser.h =================================================================== --- /dev/null +++ tools/dsymutil/MachODebugMapParser.h @@ -0,0 +1,58 @@ +//===- tools/dsymutil/MachODebugMapParser.h - Parse STABS debug maps ------===// +// +// 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 DSYMUTIL_MACHODEBUGMAPPARSER_H +#define DSYMUTIL_MACHODEBUGMAPPARSER_H + +#include "DebugMap.h" + +#include "llvm/ADT/StringMap.h" +#include "llvm/Object/MachO.h" +#include "llvm/Object/Error.h" + +namespace llvm { + +class MachODebugMapParser { +public: + MachODebugMapParser(StringRef Filename) + : Filename(Filename) {} + + /// \brief Parses and returns the LinkMap of the input binary. + ErrorOr> parse(); + +private: + std::string Filename; + object::OwningBinary MainOwningBinary; + std::unique_ptr Result; + DebugMapObject *CurrentDebugMapObject; + StringMap MainBinarySymbolAddresses; + + uint64_t getMainBinarySymbolAddress(StringRef Name); + void loadMainBinarySymbols(); + void handleStabSymbolTableEntry(uint32_t StringIndex, uint8_t Type, + uint8_t SectionIndex, uint16_t Flags, + uint64_t Value); + + // \brief Helper to handle 32 and 64bits mach-o Symbol Table Entries + // in the same way. + template void handleStabDebugMapEntry(const STEType &STE) { + handleStabSymbolTableEntry(STE.n_strx, STE.n_type, STE.n_sect, STE.n_desc, + STE.n_value); + } +}; + +} + +#endif // DSYMUTIL_MACHODEBUGMAPPARSER_H Index: tools/dsymutil/MachODebugMapParser.cpp =================================================================== --- /dev/null +++ tools/dsymutil/MachODebugMapParser.cpp @@ -0,0 +1,156 @@ +//===- 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 "MachODebugMapParser.h" + +using namespace llvm::object; + +namespace llvm { + +static void Warning(const Twine &Msg) { errs() << "warning: " + Msg + "\n"; } + +/// This is a helper to create MachO OwningBinaries. Although this +/// will always create MachO ObjectFiles, the return type is templated +/// so that we can return a more generic OwningBinary type to be +/// passed to the generic code. +template +static ErrorOr> createMachOBinary(StringRef file) { + ErrorOr> BinaryOrErr = createBinary(file); + if (BinaryOrErr.getError()) + return BinaryOrErr.getError(); + + std::unique_ptr Bin; + std::unique_ptr Buf; + std::tie(Bin, Buf) = std::move(BinaryOrErr->takeBinary()); + + if (!isa(Bin.get())) + return make_error_code(object_error::invalid_file_type); + + std::unique_ptr MachOFile(cast(Bin.release())); + return OwningBinary(std::move(MachOFile), std::move(Buf)); +} + +/// Try to create a new debug object for the given filename. +static std::unique_ptr newDebugMapObject(StringRef Filename) { + auto MachOOrError = createMachOBinary(Filename); + if (auto Error = MachOOrError.getError()) { + Warning(Twine("Cannot open debug object \"") + Filename + "\": " + + Error.message() + "\n"); + return nullptr; + } + return make_unique(Filename, std::move(*MachOOrError)); +} + +ErrorOr> MachODebugMapParser::parse() { + auto MainBinaryOrError = createMachOBinary(Filename); + if (MainBinaryOrError.getError()) + return MainBinaryOrError.getError(); + + MainOwningBinary = std::move(*MainBinaryOrError); + Result = make_unique(); + const auto &MainBinary = *MainOwningBinary.getBinary(); + for (const SymbolRef &Symbol : MainBinary.symbols()) { + const DataRefImpl &DRI = Symbol.getRawDataRefImpl(); + if (MainBinary.is64Bit()) + handleStabDebugMapEntry(MainBinary.getSymbol64TableEntry(DRI)); + else + handleStabDebugMapEntry(MainBinary.getSymbolTableEntry(DRI)); + } + return std::move(Result); +} + +uint64_t MachODebugMapParser::getMainBinarySymbolAddress(StringRef Name) { + if (MainBinarySymbolAddresses.empty()) + loadMainBinarySymbols(); + + auto Sym = MainBinarySymbolAddresses.find(Name); + if (Sym == MainBinarySymbolAddresses.end()) + return UnknownAddressOrSize; + return Sym->second; +} + +void MachODebugMapParser::loadMainBinarySymbols() { + const MachOObjectFile &Binary = *MainOwningBinary.getBinary(); + section_iterator Section = Binary.section_end(); + for (const auto &Sym : Binary.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; + } +} + + +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 MachOObjectFile &MachOBinary = *MainOwningBinary.getBinary(); + const char *Name = &MachOBinary.getStringTableData().data()[StringIndex]; + + // An N_OSO entry represents the start of a new object file description. + if (Type == MachO::N_OSO) { + auto NewObject = newDebugMapObject(Name); + CurrentDebugMapObject = NewObject.get(); + if (NewObject) + Result->addDebugMapObject(std::move(NewObject)); + return; + } + + // 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 Warning("Could not find relocated address for global symbol " + + Twine(Name)); + 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; + } + + if (!CurrentDebugMapObject->relocateSymbol(Name, Value)) + return Warning("Could not find non-relocated symbol for symbol " + + Twine(Name)); + +} +} Index: tools/dsymutil/Makefile =================================================================== --- /dev/null +++ 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 Index: tools/dsymutil/dsymutil.cpp =================================================================== --- /dev/null +++ tools/dsymutil/dsymutil.cpp @@ -0,0 +1,55 @@ +//===-- 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 "DwarfLinker.h" +#include "MachODebugMapParser.h" + +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/Options.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Signals.h" + +#include + +static llvm::cl::opt InputFile(llvm::cl::Positional, + llvm::cl::desc(""), + llvm::cl::init("-")); + +static llvm::cl::opt Verbose("v", llvm::cl::desc("Verbosity level"), + llvm::cl::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"); + + llvm::MachODebugMapParser Parser(InputFile); + llvm::ErrorOr> DebugMap = Parser.parse(); + + if (auto EC = DebugMap.getError()) { + llvm::errs() << "error: Cannot parse the debug map for \"" << InputFile << + "\": " << EC.message() << '\n'; + return 1; + } + + if (Verbose) + (*DebugMap)->dump(); + + llvm::DwarfLinker Linker(InputFile + ".dwarf"); + return !Linker.link(*DebugMap.get()); +}