Files
clang-p2996/llvm/lib/ExecutionEngine/JITLink/MachOLinkGraphBuilder.h
Lang Hames 4f0325873f [ORC] Enable JIT support for the compact-unwind frame info format on Darwin.
For Darwin/arm64 (including Apple Silicon Macs) this will enable exception
handling and stack unwinding in JIT'd code.

Darwin supports two unwind-info formats: DWARF eh-frames and compact-unwind. On
Darwin/x86-64 compilers usually produce both by default, and ORC supported
exceptions and unwinding via eh-frames (same as on Linux), discarding the
redundant compact-unwind info. On Darwin/arm64 compilers typically default to
producing compact-unwind only, with DWARF eh-frames as a fallback for functions
that can't be described in compact-unwind. Since ORC did not previously support
the compact-unwind format and eh-frames were not present ORC was unable to
handle exceptions or unwinding by default in Darwin/arm64 JIT'd code.

This patch enables support for the compact-unwind-info format, and contains
three major moving parts:

(1) The JITLink CompactUnwindManager class is responsible for transforming the
    __compact_unwind records produced by the linker into the __unwind_info
    tables that libunwind parses during unwinding. To enable this the
    CompactUnwindManager class provides three JITLink passes: The
    prepareForPrune pass that splits the __compact_unwind section into
    single-record blocks, allowing unused records to be dead-stripped; the
    processAndReserveUnwindInfo pass that reserves space for the final
    __unwind_info section, and the writeUnwindInfo pass that writes the
    __unwind_info section.

(2) The OrcTargetProcess UnwindInfoManager class maintains a table of
    registered JIT'd __unwind_info and __eh_frame sections, and handles
    requests from libunwind for unwind info sections (by registering a callback
    with libunwind's __unw_add_find_dynamic_unwind_sections function).

(3) The Orc UnwindInfoRegistrationPlugin, which scans LinkGraphs for
    __unwind_info and __eh_frame sections to register with the
    UnwindInfoManager.

This commit adds the CompactUnwindManager passes to the default JITLink
pipelines for Darwin/arm64 and Darwin/x86-64, and UnwindInfoManager intances to
the SelfExecutorProcessControl class (when built for apple platforms) and the
llvm-jitlink-executor tool.

The LLJIT class will now create an UnwindInfoRegistrationPlugin when targeting
a process running on Darwin if it detects that an UnwindInfoManager is
available to handle the registrations.

The ORC runtime macho_platform class already supported libunwind callbacks, so
out-of-process execution and unwinding support will work when loading the ORC
runtime.

The llvm-jitlink tool will only support compact-unwind when the orc-runtime is
loaded, as the UnwindInfoRegistrationPlugin requires access to an IR compiler
to load a helper module and llvm-jitlink does not provide an IR compiler.
2025-01-23 22:55:01 +00:00

243 lines
8.7 KiB
C++

//===----- MachOLinkGraphBuilder.h - MachO LinkGraph builder ----*- 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
//
//===----------------------------------------------------------------------===//
//
// Generic MachO LinkGraph building code.
//
//===----------------------------------------------------------------------===//
#ifndef LIB_EXECUTIONENGINE_JITLINK_MACHOLINKGRAPHBUILDER_H
#define LIB_EXECUTIONENGINE_JITLINK_MACHOLINKGRAPHBUILDER_H
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
#include "llvm/Object/MachO.h"
#include "EHFrameSupportImpl.h"
#include "JITLinkGeneric.h"
#include <list>
namespace llvm {
namespace jitlink {
class MachOLinkGraphBuilder {
public:
virtual ~MachOLinkGraphBuilder();
Expected<std::unique_ptr<LinkGraph>> buildGraph();
protected:
struct NormalizedSymbol {
friend class MachOLinkGraphBuilder;
private:
NormalizedSymbol(std::optional<StringRef> Name, uint64_t Value,
uint8_t Type, uint8_t Sect, uint16_t Desc, Linkage L,
Scope S)
: Name(Name), Value(Value), Type(Type), Sect(Sect), Desc(Desc), L(L),
S(S) {
assert((!Name || !Name->empty()) && "Name must be none or non-empty");
}
public:
NormalizedSymbol(const NormalizedSymbol &) = delete;
NormalizedSymbol &operator=(const NormalizedSymbol &) = delete;
NormalizedSymbol(NormalizedSymbol &&) = delete;
NormalizedSymbol &operator=(NormalizedSymbol &&) = delete;
std::optional<StringRef> Name;
uint64_t Value = 0;
uint8_t Type = 0;
uint8_t Sect = 0;
uint16_t Desc = 0;
Linkage L = Linkage::Strong;
Scope S = Scope::Default;
Symbol *GraphSymbol = nullptr;
};
// Normalized section representation. Section and segment names are guaranteed
// to be null-terminated, hence the extra bytes on SegName and SectName.
class NormalizedSection {
friend class MachOLinkGraphBuilder;
private:
NormalizedSection() = default;
public:
char SectName[17];
char SegName[17];
orc::ExecutorAddr Address;
uint64_t Size = 0;
uint64_t Alignment = 0;
uint32_t Flags = 0;
const char *Data = nullptr;
Section *GraphSection = nullptr;
std::map<orc::ExecutorAddr, Symbol *> CanonicalSymbols;
};
using SectionParserFunction = std::function<Error(NormalizedSection &S)>;
MachOLinkGraphBuilder(const object::MachOObjectFile &Obj,
std::shared_ptr<orc::SymbolStringPool> SSP, Triple TT,
SubtargetFeatures Features,
LinkGraph::GetEdgeKindNameFunction GetEdgeKindName);
LinkGraph &getGraph() const { return *G; }
const object::MachOObjectFile &getObject() const { return Obj; }
void addCustomSectionParser(StringRef SectionName,
SectionParserFunction Parse);
virtual Error addRelocations() = 0;
/// Create a symbol.
template <typename... ArgTs>
NormalizedSymbol &createNormalizedSymbol(ArgTs &&... Args) {
NormalizedSymbol *Sym = reinterpret_cast<NormalizedSymbol *>(
Allocator.Allocate<NormalizedSymbol>());
new (Sym) NormalizedSymbol(std::forward<ArgTs>(Args)...);
return *Sym;
}
/// Index is zero-based (MachO section indexes are usually one-based) and
/// assumed to be in-range. Client is responsible for checking.
NormalizedSection &getSectionByIndex(unsigned Index) {
auto I = IndexToSection.find(Index);
assert(I != IndexToSection.end() && "No section recorded at index");
return I->second;
}
/// Try to get the section at the given index. Will return an error if the
/// given index is out of range, or if no section has been added for the given
/// index.
Expected<NormalizedSection &> findSectionByIndex(unsigned Index) {
auto I = IndexToSection.find(Index);
if (I == IndexToSection.end())
return make_error<JITLinkError>("No section recorded for index " +
formatv("{0:d}", Index));
return I->second;
}
/// Try to get the symbol at the given index. Will return an error if the
/// given index is out of range, or if no symbol has been added for the given
/// index.
Expected<NormalizedSymbol &> findSymbolByIndex(uint64_t Index) {
auto I = IndexToSymbol.find(Index);
if (I == IndexToSymbol.end())
return make_error<JITLinkError>("No symbol at index " +
formatv("{0:d}", Index));
assert(I->second && "Null symbol at index");
return *I->second;
}
/// Returns the symbol with the highest address not greater than the search
/// address, or null if no such symbol exists.
Symbol *getSymbolByAddress(NormalizedSection &NSec,
orc::ExecutorAddr Address) {
auto I = NSec.CanonicalSymbols.upper_bound(Address);
if (I == NSec.CanonicalSymbols.begin())
return nullptr;
return std::prev(I)->second;
}
/// Returns the symbol with the highest address not greater than the search
/// address, or an error if no such symbol exists.
Expected<Symbol &> findSymbolByAddress(NormalizedSection &NSec,
orc::ExecutorAddr Address) {
auto *Sym = getSymbolByAddress(NSec, Address);
if (Sym)
if (Address <= Sym->getAddress() + Sym->getSize())
return *Sym;
return make_error<JITLinkError>("No symbol covering address " +
formatv("{0:x16}", Address));
}
static Linkage getLinkage(uint16_t Desc);
static Scope getScope(StringRef Name, uint8_t Type);
static bool isAltEntry(const NormalizedSymbol &NSym);
static bool isDebugSection(const NormalizedSection &NSec);
static bool isZeroFillSection(const NormalizedSection &NSec);
MachO::relocation_info
getRelocationInfo(const object::relocation_iterator RelItr) {
MachO::any_relocation_info ARI =
getObject().getRelocation(RelItr->getRawDataRefImpl());
MachO::relocation_info RI;
RI.r_address = ARI.r_word0;
RI.r_symbolnum = ARI.r_word1 & 0xffffff;
RI.r_pcrel = (ARI.r_word1 >> 24) & 1;
RI.r_length = (ARI.r_word1 >> 25) & 3;
RI.r_extern = (ARI.r_word1 >> 27) & 1;
RI.r_type = (ARI.r_word1 >> 28);
return RI;
}
private:
static unsigned getPointerSize(const object::MachOObjectFile &Obj);
static llvm::endianness getEndianness(const object::MachOObjectFile &Obj);
void setCanonicalSymbol(NormalizedSection &NSec, Symbol &Sym) {
auto *&CanonicalSymEntry = NSec.CanonicalSymbols[Sym.getAddress()];
// There should be no symbol at this address, or, if there is,
// it should be a zero-sized symbol from an empty section (which
// we can safely override).
assert((!CanonicalSymEntry || CanonicalSymEntry->getSize() == 0) &&
"Duplicate canonical symbol at address");
CanonicalSymEntry = &Sym;
}
Section &getCommonSection();
void addSectionStartSymAndBlock(unsigned SecIndex, Section &GraphSec,
orc::ExecutorAddr Address, const char *Data,
orc::ExecutorAddrDiff Size,
uint32_t Alignment, bool IsLive);
Error createNormalizedSections();
Error createNormalizedSymbols();
/// Create graph blocks and symbols for externals, absolutes, commons and
/// all defined symbols in sections without custom parsers.
Error graphifyRegularSymbols();
/// Create and return a graph symbol for the given normalized symbol.
///
/// NSym's GraphSymbol member will be updated to point at the newly created
/// symbol.
Symbol &createStandardGraphSymbol(NormalizedSymbol &Sym, Block &B,
size_t Size, bool IsText,
bool IsNoDeadStrip, bool IsCanonical);
/// Create graph blocks and symbols for all sections.
Error graphifySectionsWithCustomParsers();
/// Graphify cstring section.
Error graphifyCStringSection(NormalizedSection &NSec,
std::vector<NormalizedSymbol *> NSyms);
// Put the BumpPtrAllocator first so that we don't free any of the underlying
// memory until the Symbol/Addressable destructors have been run.
BumpPtrAllocator Allocator;
const object::MachOObjectFile &Obj;
std::unique_ptr<LinkGraph> G;
bool SubsectionsViaSymbols = false;
DenseMap<unsigned, NormalizedSection> IndexToSection;
Section *CommonSection = nullptr;
DenseMap<uint32_t, NormalizedSymbol *> IndexToSymbol;
StringMap<SectionParserFunction> CustomSectionParserFunctions;
};
} // end namespace jitlink
} // end namespace llvm
#endif // LIB_EXECUTIONENGINE_JITLINK_MACHOLINKGRAPHBUILDER_H