diff --git a/llvm/include/llvm/ExecutionEngine/Orc/DebugInfoSupport.h b/llvm/include/llvm/ExecutionEngine/Orc/DebugInfoSupport.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/ExecutionEngine/Orc/DebugInfoSupport.h @@ -0,0 +1,62 @@ +//===--- DebugInfoSupport.h ---- Utils for debug info support ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Utilities to preserve and parse debug info from LinkGraphs. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_EXECUTIONENGINE_ORC_DEBUGINFOSUPPORT_H +#define LLVM_EXECUTIONENGINE_ORC_DEBUGINFOSUPPORT_H + +#include "llvm/ExecutionEngine/Orc/Core.h" +#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" + +#include "llvm/DebugInfo/DWARF/DWARFContext.h" + +namespace llvm { + +namespace orc { + +Error preserveDebugSections(jitlink::LinkGraph &G); +// The backing stringmap is also returned, for memory liftime management. +Expected, + StringMap>>> +createDWARFContext(jitlink::LinkGraph &G); + +// Thin wrapper around preserveDebugSections to be used as a standalone plugin. +class DebugInfoPreservationPlugin : public ObjectLinkingLayer::Plugin { +public: + void modifyPassConfig(MaterializationResponsibility &MR, + jitlink::LinkGraph &LG, + jitlink::PassConfiguration &PassConfig) override { + PassConfig.PrePrunePasses.push_back(preserveDebugSections); + } + + Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override { + // Do nothing. + return Error::success(); + } + Error notifyFailed(MaterializationResponsibility &MR) override { + // Do nothing. + return Error::success(); + } + void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, + ResourceKey SrcKey) override { + // Do nothing. + } + + static Expected> Create() { + return std::make_unique(); + } +}; + +} // namespace orc + +} // namespace llvm + +#endif \ No newline at end of file diff --git a/llvm/include/llvm/ExecutionEngine/Orc/PerfSupportPlugin.h b/llvm/include/llvm/ExecutionEngine/Orc/PerfSupportPlugin.h --- a/llvm/include/llvm/ExecutionEngine/Orc/PerfSupportPlugin.h +++ b/llvm/include/llvm/ExecutionEngine/Orc/PerfSupportPlugin.h @@ -28,7 +28,8 @@ PerfSupportPlugin(ExecutorProcessControl &EPC, ExecutorAddr RegisterPerfStartAddr, ExecutorAddr RegisterPerfEndAddr, - ExecutorAddr RegisterPerfImplAddr, bool EmitUnwindInfo); + ExecutorAddr RegisterPerfImplAddr, bool EmitDebugInfo, + bool EmitUnwindInfo); ~PerfSupportPlugin(); void modifyPassConfig(MaterializationResponsibility &MR, @@ -47,7 +48,8 @@ ResourceKey SrcKey) override {} static Expected> - Create(ExecutorProcessControl &EPC, JITDylib &JD, bool EmitUnwindInfo); + Create(ExecutorProcessControl &EPC, JITDylib &JD, bool EmitDebugInfo, + bool EmitUnwindInfo); private: ExecutorProcessControl &EPC; @@ -55,6 +57,7 @@ ExecutorAddr RegisterPerfEndAddr; ExecutorAddr RegisterPerfImplAddr; std::atomic CodeIndex; + bool EmitDebugInfo; bool EmitUnwindInfo; }; diff --git a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt --- a/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt +++ b/llvm/lib/ExecutionEngine/Orc/CMakeLists.txt @@ -10,6 +10,7 @@ Core.cpp DebugObjectManagerPlugin.cpp DebuggerSupportPlugin.cpp + DebugInfoSupport.cpp DebugUtils.cpp EPCDynamicLibrarySearchGenerator.cpp EPCDebugObjectRegistrar.cpp diff --git a/llvm/lib/ExecutionEngine/Orc/DebugInfoSupport.cpp b/llvm/lib/ExecutionEngine/Orc/DebugInfoSupport.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/ExecutionEngine/Orc/DebugInfoSupport.cpp @@ -0,0 +1,124 @@ +//===--- DebugInfoSupport.cpp -- Utils for debug info support ---*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Utilities to preserve and parse debug info from LinkGraphs. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ExecutionEngine/Orc/DebugInfoSupport.h" + +#include "llvm/Support/SmallVectorMemoryBuffer.h" + +#define DEBUG_TYPE "orc" + +using namespace llvm; +using namespace llvm::orc; +using namespace llvm::jitlink; + +namespace { +static DenseSet DWARFSectionNames = { +#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \ + StringRef(ELF_NAME), +#include "llvm/BinaryFormat/Dwarf.def" +#undef HANDLE_DWARF_SECTION +}; + +// We might be able to drop relocations to symbols that do end up +// being pruned by the linker, but for now we just preserve all +static void preserveDWARFSection(LinkGraph &G, Section &Sec) { + DenseMap Preserved; + for (auto Sym : Sec.symbols()) { + if (Sym->isLive()) { + Preserved[&Sym->getBlock()] = Sym; + } else if (!Preserved.count(&Sym->getBlock())) { + Preserved[&Sym->getBlock()] = Sym; + } + } + for (auto Block : Sec.blocks()) { + auto &PSym = Preserved[Block]; + if (!PSym) { + PSym = &G.addAnonymousSymbol(*Block, 0, 0, false, true); + } else if (!PSym->isLive()) { + PSym->setLive(true); + } + } +} + +static SmallVector getSectionData(Section &Sec) { + SmallVector SecData; + SmallVector SecBlocks(Sec.blocks().begin(), Sec.blocks().end()); + std::sort(SecBlocks.begin(), SecBlocks.end(), [](Block *LHS, Block *RHS) { + return LHS->getAddress() < RHS->getAddress(); + }); + // Convert back to what object file would have, one blob of section content + // Assumes all zerofill + // TODO handle alignment? + // TODO handle alignment offset? + for (auto *Block : SecBlocks) { + if (Block->isZeroFill()) { + SecData.resize(SecData.size() + Block->getSize(), 0); + } else { + SecData.append(Block->getContent().begin(), Block->getContent().end()); + } + } + return SecData; +} + +static void dumpDWARFContext(DWARFContext &DC) { + auto options = llvm::DIDumpOptions(); + options.DumpType &= ~DIDT_UUID; + options.DumpType &= ~(1 << DIDT_ID_DebugFrame); + LLVM_DEBUG(DC.dump(dbgs(), options)); +} + +} // namespace + +Error llvm::orc::preserveDebugSections(LinkGraph &G) { + if (!G.getTargetTriple().isOSBinFormatELF()) { + return make_error( + "preserveDebugSections only supports ELF LinkGraphs!", + inconvertibleErrorCode()); + } + for (auto &Sec : G.sections()) { + if (DWARFSectionNames.count(Sec.getName())) { + LLVM_DEBUG(dbgs() << "Preserving DWARF section " << Sec.getName() + << "\n"); + preserveDWARFSection(G, Sec); + } + } + return Error::success(); +} + +Expected, + StringMap>>> +llvm::orc::createDWARFContext(LinkGraph &G) { + if (!G.getTargetTriple().isOSBinFormatELF()) { + return make_error( + "createDWARFContext only supports ELF LinkGraphs!", + inconvertibleErrorCode()); + } + StringMap> DWARFSectionData; + for (auto &Sec : G.sections()) { + if (DWARFSectionNames.count(Sec.getName())) { + auto SecData = getSectionData(Sec); + auto Name = Sec.getName(); + // DWARFContext expects the section name to not start with a dot + if (Name.startswith(".")) { + Name = Name.drop_front(); + } + LLVM_DEBUG(dbgs() << "Creating DWARFContext section " << Name + << " with size " << SecData.size() << "\n"); + DWARFSectionData[Name] = + std::make_unique(std::move(SecData)); + } + } + auto Ctx = DWARFContext::create(DWARFSectionData, G.getPointerSize(), + G.getEndianness() == support::little); + dumpDWARFContext(*Ctx); + return std::make_pair(std::move(Ctx), std::move(DWARFSectionData)); +} diff --git a/llvm/lib/ExecutionEngine/Orc/PerfSupportPlugin.cpp b/llvm/lib/ExecutionEngine/Orc/PerfSupportPlugin.cpp --- a/llvm/lib/ExecutionEngine/Orc/PerfSupportPlugin.cpp +++ b/llvm/lib/ExecutionEngine/Orc/PerfSupportPlugin.cpp @@ -14,7 +14,8 @@ #include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderPerf.h" -#include "llvm/DebugInfo/DWARF/DWARFContext.h" +#include "llvm/ExecutionEngine/Orc/DebugInfoSupport.h" + #include "llvm/ExecutionEngine/JITLink/x86_64.h" #include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" #include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h" @@ -130,11 +131,7 @@ } static std::optional -getDebugInfoRecord(const Symbol &Sym, DWARFContext *DC) { - if (!DC) { - LLVM_DEBUG(dbgs() << "No debug info available\n"); - return std::nullopt; - } +getDebugInfoRecord(const Symbol &Sym, DWARFContext &DC) { auto &Section = Sym.getBlock().getSection(); auto Addr = Sym.getAddress(); auto Size = Sym.getSize(); @@ -143,7 +140,7 @@ << " at address " << Addr.getValue() << " with size " << Size << "\n" << "Section ordinal: " << Section.getOrdinal() << "\n"); - auto LInfo = DC->getLineInfoForAddressRange( + auto LInfo = DC.getLineInfoForAddressRange( SAddr, Size, DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath); if (LInfo.empty()) { // No line info available @@ -231,16 +228,30 @@ return Record; } -static PerfJITRecordBatch getRecords(LinkGraph &G, DWARFContext *DC, +static PerfJITRecordBatch getRecords(ExecutionSession &ES, LinkGraph &G, std::atomic &CodeIndex, - bool EmitUnwindInfo) { + bool EmitDebugInfo, bool EmitUnwindInfo) { + std::unique_ptr DC; + StringMap> DCBacking; + if (EmitDebugInfo) { + auto EDC = createDWARFContext(G); + if (!EDC) { + ES.reportError(EDC.takeError()); + EmitDebugInfo = false; + } else { + DC = std::move(EDC->first); + DCBacking = std::move(EDC->second); + } + } PerfJITRecordBatch Batch; for (auto Sym : G.defined_symbols()) { if (!Sym->hasName() || !Sym->isCallable()) continue; - auto DebugInfo = getDebugInfoRecord(*Sym, DC); - if (DebugInfo) - Batch.DebugInfoRecords.push_back(std::move(*DebugInfo)); + if (EmitDebugInfo) { + auto DebugInfo = getDebugInfoRecord(*Sym, *DC); + if (DebugInfo) + Batch.DebugInfoRecords.push_back(std::move(*DebugInfo)); + } Batch.CodeLoadRecords.push_back(getCodeLoadRecord(*Sym, CodeIndex)); } if (EmitUnwindInfo) { @@ -256,11 +267,11 @@ ExecutorAddr RegisterPerfStartAddr, ExecutorAddr RegisterPerfEndAddr, ExecutorAddr RegisterPerfImplAddr, - bool EmitUnwindInfo) + bool EmitDebugInfo, bool EmitUnwindInfo) : EPC(EPC), RegisterPerfStartAddr(RegisterPerfStartAddr), RegisterPerfEndAddr(RegisterPerfEndAddr), RegisterPerfImplAddr(RegisterPerfImplAddr), CodeIndex(0), - EmitUnwindInfo(EmitUnwindInfo) { + EmitDebugInfo(EmitDebugInfo), EmitUnwindInfo(EmitUnwindInfo) { cantFail(EPC.callSPSWrapper(RegisterPerfStartAddr)); } PerfSupportPlugin::~PerfSupportPlugin() { @@ -271,9 +282,8 @@ LinkGraph &G, PassConfiguration &Config) { Config.PostFixupPasses.push_back([this, &MR](LinkGraph &G) { - // TODO get an actual DWARFContext for line info - DWARFContext *DWC = nullptr; - auto Batch = getRecords(G, DWC, CodeIndex, EmitUnwindInfo); + auto Batch = getRecords(EPC.getExecutionSession(), G, CodeIndex, + EmitDebugInfo, EmitUnwindInfo); G.allocActions().push_back( {cantFail(shared::WrapperFunctionCall::Create< shared::SPSArgList>( @@ -285,7 +295,7 @@ Expected> PerfSupportPlugin::Create(ExecutorProcessControl &EPC, JITDylib &JD, - bool EmitUnwindInfo) { + bool EmitDebugInfo, bool EmitUnwindInfo) { auto &ES = EPC.getExecutionSession(); auto StartName = ES.intern(RegisterPerfStartSymbolName); auto EndName = ES.intern(RegisterPerfEndSymbolName); @@ -298,5 +308,5 @@ auto EndAddr = ExecutorAddr(Res->find(EndName)->second.getAddress()); auto ImplAddr = ExecutorAddr(Res->find(ImplName)->second.getAddress()); return std::make_unique(EPC, StartAddr, EndAddr, ImplAddr, - EmitUnwindInfo); + EmitDebugInfo, EmitUnwindInfo); } diff --git a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp --- a/llvm/tools/llvm-jitlink/llvm-jitlink.cpp +++ b/llvm/tools/llvm-jitlink/llvm-jitlink.cpp @@ -17,6 +17,7 @@ #include "llvm/BinaryFormat/Magic.h" #include "llvm/ExecutionEngine/Orc/COFFPlatform.h" #include "llvm/ExecutionEngine/Orc/COFFVCRuntimeSupport.h" +#include "llvm/ExecutionEngine/Orc/DebugInfoSupport.h" #include "llvm/ExecutionEngine/Orc/DebugObjectManagerPlugin.h" #include "llvm/ExecutionEngine/Orc/DebuggerSupportPlugin.h" #include "llvm/ExecutionEngine/Orc/ELFNixPlatform.h" @@ -998,9 +999,11 @@ ObjLayer.addPlugin(ExitOnErr( GDBJITDebugInfoRegistrationPlugin::Create(this->ES, *MainJD, TT))); - if (PerfSupport && TT.isOSBinFormatELF()) + if (PerfSupport && TT.isOSBinFormatELF()) { + ObjLayer.addPlugin(ExitOnErr(DebugInfoPreservationPlugin::Create())); ObjLayer.addPlugin(ExitOnErr(PerfSupportPlugin::Create( - this->ES.getExecutorProcessControl(), *MainJD, true))); + this->ES.getExecutorProcessControl(), *MainJD, true, true))); + } // Set up the platform. if (TT.isOSBinFormatMachO() && !OrcRuntime.empty()) {