Path: blob/main/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/Debugging/DebugInfoSupport.cpp
35294 views
//===--- DebugInfoSupport.cpp -- Utils for debug info support ---*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Utilities to preserve and parse debug info from LinkGraphs.9//10//===----------------------------------------------------------------------===//1112#include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h"1314#include "llvm/Support/SmallVectorMemoryBuffer.h"1516#define DEBUG_TYPE "orc"1718using namespace llvm;19using namespace llvm::orc;20using namespace llvm::jitlink;2122namespace {23static DenseSet<StringRef> DWARFSectionNames = {24#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \25StringRef(ELF_NAME),26#include "llvm/BinaryFormat/Dwarf.def"27#undef HANDLE_DWARF_SECTION28};2930// We might be able to drop relocations to symbols that do end up31// being pruned by the linker, but for now we just preserve all32static void preserveDWARFSection(LinkGraph &G, Section &Sec) {33DenseMap<Block *, Symbol *> Preserved;34for (auto Sym : Sec.symbols()) {35if (Sym->isLive())36Preserved[&Sym->getBlock()] = Sym;37else if (!Preserved.count(&Sym->getBlock()))38Preserved[&Sym->getBlock()] = Sym;39}40for (auto Block : Sec.blocks()) {41auto &PSym = Preserved[Block];42if (!PSym)43PSym = &G.addAnonymousSymbol(*Block, 0, 0, false, true);44else if (!PSym->isLive())45PSym->setLive(true);46}47}4849static SmallVector<char, 0> getSectionData(Section &Sec) {50SmallVector<char, 0> SecData;51SmallVector<Block *, 8> SecBlocks(Sec.blocks().begin(), Sec.blocks().end());52std::sort(SecBlocks.begin(), SecBlocks.end(), [](Block *LHS, Block *RHS) {53return LHS->getAddress() < RHS->getAddress();54});55// Convert back to what object file would have, one blob of section content56// Assumes all zerofill57// TODO handle alignment?58// TODO handle alignment offset?59for (auto *Block : SecBlocks) {60if (Block->isZeroFill())61SecData.resize(SecData.size() + Block->getSize(), 0);62else63SecData.append(Block->getContent().begin(), Block->getContent().end());64}65return SecData;66}6768static void dumpDWARFContext(DWARFContext &DC) {69auto options = llvm::DIDumpOptions();70options.DumpType &= ~DIDT_UUID;71options.DumpType &= ~(1 << DIDT_ID_DebugFrame);72LLVM_DEBUG(DC.dump(dbgs(), options));73}7475} // namespace7677Error llvm::orc::preserveDebugSections(LinkGraph &G) {78if (!G.getTargetTriple().isOSBinFormatELF()) {79return make_error<StringError>(80"preserveDebugSections only supports ELF LinkGraphs!",81inconvertibleErrorCode());82}83for (auto &Sec : G.sections()) {84if (DWARFSectionNames.count(Sec.getName())) {85LLVM_DEBUG(dbgs() << "Preserving DWARF section " << Sec.getName()86<< "\n");87preserveDWARFSection(G, Sec);88}89}90return Error::success();91}9293Expected<std::pair<std::unique_ptr<DWARFContext>,94StringMap<std::unique_ptr<MemoryBuffer>>>>95llvm::orc::createDWARFContext(LinkGraph &G) {96if (!G.getTargetTriple().isOSBinFormatELF()) {97return make_error<StringError>(98"createDWARFContext only supports ELF LinkGraphs!",99inconvertibleErrorCode());100}101StringMap<std::unique_ptr<MemoryBuffer>> DWARFSectionData;102for (auto &Sec : G.sections()) {103if (DWARFSectionNames.count(Sec.getName())) {104auto SecData = getSectionData(Sec);105auto Name = Sec.getName();106// DWARFContext expects the section name to not start with a dot107Name.consume_front(".");108LLVM_DEBUG(dbgs() << "Creating DWARFContext section " << Name109<< " with size " << SecData.size() << "\n");110DWARFSectionData[Name] =111std::make_unique<SmallVectorMemoryBuffer>(std::move(SecData));112}113}114auto Ctx =115DWARFContext::create(DWARFSectionData, G.getPointerSize(),116G.getEndianness() == llvm::endianness::little);117dumpDWARFContext(*Ctx);118return std::make_pair(std::move(Ctx), std::move(DWARFSectionData));119}120121122