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 @@ -25,7 +25,8 @@ PerfSupportPlugin(ExecutorProcessControl &EPC, ExecutorAddr RegisterPerfStartAddr, ExecutorAddr RegisterPerfEndAddr, - ExecutorAddr RegisterPerfImplAddr, bool EmitUnwindInfo); + ExecutorAddr RegisterPerfImplAddr, bool EmitDebugInfo, + bool EmitUnwindInfo); ~PerfSupportPlugin(); void modifyPassConfig(MaterializationResponsibility &MR, @@ -44,7 +45,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; @@ -52,6 +54,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,113 @@ +#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); + 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())) { + dbgs() << "Preserving DWARF section " << Sec.getName() << "\n"; + preserveDWARFSection(G, Sec); + } else { + dbgs() << "Not preserving section " << Sec.getName() << "\n"; + } + } + 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(); + } + 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 @@ -2,7 +2,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" @@ -142,21 +143,15 @@ } static std::optional -getDebugInfoRecord(const Symbol &Sym, DWARFContext *DC) { - // dbgs() << "Getting debug info for symbol " << Sym.getName() << "\n"; - if (!DC) { - // No debug info available - 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(); auto SAddr = object::SectionedAddress{Addr.getValue(), Section.getOrdinal()}; dbgs() << "Getting debug info for symbol " << Sym.getName() << " at address " - << Addr.getValue() << " with size " << Size << "\n"; + << (void *)Addr.getValue() << " with size " << Size << "\n"; dbgs() << "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 @@ -209,7 +204,7 @@ auto EHFrameSize = SR.getSize(); auto Eh_frame_hdr = G.findSectionByName(".eh_frame_hdr"); if (!Eh_frame_hdr) { - if (G.getTargetTriple().getArch() != Triple::x86_64) { + if (G.getTargetTriple().getArch() == Triple::x86_64) { Record.EHFrameHdr = createX64EHFrameHeader(*Eh_frame, G.getEndianness(), true); } else { @@ -243,16 +238,31 @@ return Record; } -static PerfJITRecordBatch getRecords(LinkGraph &G, DWARFContext *DC, +static PerfJITRecordBatch getRecords(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) { + // TODO fatal errors when debug info fails? Some other way of reporting? + consumeError(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()) 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) { @@ -268,11 +278,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)); dbgs() << "Created perf support plugin\n"; } @@ -288,9 +298,7 @@ Config.PostFixupPasses.push_back([this, &MR](LinkGraph &G) { dbgs() << "Running perf support plugin post-fixup pass\n"; dbgs() << serialize(G) << "\n"; - // TODO get an actual DWARFContext for line info - DWARFContext *DWC = nullptr; - auto Batch = getRecords(G, DWC, CodeIndex, EmitUnwindInfo); + auto Batch = getRecords(G, CodeIndex, EmitDebugInfo, EmitUnwindInfo); G.allocActions().push_back( {cantFail(shared::WrapperFunctionCall::Create< shared::SPSArgList>( @@ -304,7 +312,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); @@ -317,5 +325,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()) {