22#define DEBUG_TYPE "jitlink"
29enum EdgeKind_coff_x86_64 : Edge::Kind {
37class COFFJITLinker_x86_64 :
public JITLinker<COFFJITLinker_x86_64> {
38 friend class JITLinker<COFFJITLinker_x86_64>;
41 COFFJITLinker_x86_64(std::unique_ptr<JITLinkContext> Ctx,
42 std::unique_ptr<LinkGraph>
G,
44 :
JITLinker(std::move(Ctx), std::move(
G), std::move(PassConfig)) {}
54 Error addRelocations()
override {
57 for (
const auto &RelSect :
sections())
59 RelSect,
this, &COFFLinkGraphBuilder_x86_64::addSingleRelocation))
70 if (SymbolIt ==
getObject().symbol_end()) {
72 formatv(
"Invalid symbol index in relocation entry. "
73 "index: {0}, section: {1}",
79 COFFSymbolIndex SymIndex =
getObject().getSymbolIndex(COFFSymbol);
81 Symbol *GraphSymbol = getGraphSymbol(SymIndex);
84 formatv(
"Could not find symbol at given index, did you add it to "
85 "JITSymbolTable? index: {0}, section: {1}",
94 Edge::Kind Kind = Edge::Invalid;
101 ImageBase = &addImageBaseSymbol();
102 Kind = EdgeKind_coff_x86_64::Pointer32NB;
107 Kind = EdgeKind_coff_x86_64::PCRel32;
112 Kind = EdgeKind_coff_x86_64::PCRel32;
118 Kind = EdgeKind_coff_x86_64::PCRel32;
124 Kind = EdgeKind_coff_x86_64::PCRel32;
130 Kind = EdgeKind_coff_x86_64::PCRel32;
136 Kind = EdgeKind_coff_x86_64::PCRel32;
142 Kind = EdgeKind_coff_x86_64::Pointer64;
147 Kind = EdgeKind_coff_x86_64::SectionIdx16;
150 if (COFFSymbol.isAbsolute())
151 SectionIdx =
getObject().getNumberOfSections() + 1;
153 SectionIdx = COFFSymbol.getSectionNumber();
155 auto *AbsSym = &getGraph().addAbsoluteSymbol(
158 GraphSymbol = AbsSym;
165 Kind = EdgeKind_coff_x86_64::SecRel32;
182 BlockToFix.
addEdge(std::move(GE));
189 std::shared_ptr<orc::SymbolStringPool> SSP,
196class COFFLinkGraphLowering_x86_64 {
200 for (
auto *
B :
G.blocks()) {
201 for (
auto &
E :
B->edges()) {
202 switch (
E.getKind()) {
203 case EdgeKind_coff_x86_64::Pointer32NB: {
204 auto ImageBase = GetImageBase(
G);
205 assert(ImageBase &&
"__ImageBase symbol must be defined");
206 E.setAddend(
E.getAddend() - ImageBase->getAddress().getValue());
210 case EdgeKind_coff_x86_64::PCRel32: {
214 case EdgeKind_coff_x86_64::Pointer64: {
218 case EdgeKind_coff_x86_64::SectionIdx16: {
222 case EdgeKind_coff_x86_64::SecRel32: {
223 E.setAddend(
E.getAddend() -
224 getSectionStart(
E.getTarget().getSection()).getValue());
238 auto [It, Inserted] = SectionStartCache.try_emplace(&Sec);
241 It->second =
Range.getStart();
260 return "Pointer32NB";
264 return "SectionIdx16";
273 MemoryBufferRef ObjectBuffer, std::shared_ptr<orc::SymbolStringPool> SSP) {
275 dbgs() <<
"Building jitlink graph for new input "
281 return COFFObj.takeError();
283 auto Features = (*COFFObj)->getFeatures();
285 return Features.takeError();
287 return COFFLinkGraphBuilder_x86_64(**COFFObj, std::move(SSP),
288 (*COFFObj)->makeTriple(),
289 std::move(*Features))
294 std::unique_ptr<JITLinkContext> Ctx) {
296 const Triple &TT =
G->getTargetTriple();
297 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
299 if (
auto MarkLive = Ctx->getMarkLivePass(TT)) {
309 if (
auto Err = Ctx->modifyPassConfig(*
G, Config))
310 return Ctx->notifyFailed(std::move(Err));
312 COFFJITLinker_x86_64::link(std::move(Ctx), std::move(
G), std::move(Config));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
bbsections Prepares for basic block sections
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static Error getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
StringRef getBufferIdentifier() const
Manages the enabling and disabling of subtarget specific features.
Triple - Helper class for working with autoconf configuration names.
orc::ExecutorAddr getAddress() const
An Addressable with content and edges.
ArrayRef< char > getContent() const
Get the content for this block. Block must not be a zero-fill block.
void addEdge(Edge::Kind K, Edge::OffsetT Offset, Symbol &Target, Edge::AddendT Addend)
Add an edge to this block.
Error forEachRelocation(const object::SectionRef &RelSec, RelocHandlerFunction &&Func, bool ProcessDebugSections=false)
Traverse all matching relocation records in the given section.
Represents fixups and constraints in the LinkGraph.
This pass adds keep-alive edge from SEH frame sections to the parent function content block.
Represents a section address range via a pair of Block pointers to the first and last Blocks in the s...
Represents an object file section.
bool isDefined() const
Returns true if this Symbol has content (potentially) defined within this object file (i....
static Expected< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
This is a value type class that represents a single relocation in the list of relocations in the obje...
uint64_t getOffset() const
symbol_iterator getSymbol() const
This is a value type class that represents a single section in the list of sections in the object fil...
uint64_t getIndex() const
uint64_t getAddress() const
Represents an address in the executor process.
@ IMAGE_REL_AMD64_REL32_5
@ IMAGE_REL_AMD64_REL32_3
@ IMAGE_REL_AMD64_ADDR32NB
@ IMAGE_REL_AMD64_SECTION
@ IMAGE_REL_AMD64_REL32_2
@ IMAGE_REL_AMD64_REL32_1
@ IMAGE_REL_AMD64_REL32_4
LLVM_ABI const char * getEdgeKindName(Edge::Kind K)
Returns a string name for the given x86-64 edge.
Error applyFixup(LinkGraph &G, Block &B, const Edge &E, const Symbol *GOTSymbol)
Apply fixup expression for edge to block content.
@ Pointer32
A plain 32-bit pointer value relocation.
@ PCRel32
A 32-bit PC-relative relocation.
@ Pointer16
A plain 16-bit pointer value relocation.
@ Pointer64
A plain 64-bit pointer value relocation.
@ FirstPlatformRelocation
Expected< std::unique_ptr< LinkGraph > > createLinkGraphFromCOFFObject_x86_64(MemoryBufferRef ObjectBuffer, std::shared_ptr< orc::SymbolStringPool > SSP)
Create a LinkGraph from an COFF/x86-64 relocatable object.
LLVM_ABI Error markAllSymbolsLive(LinkGraph &G)
Marks all symbols in a graph live.
void link_COFF_x86_64(std::unique_ptr< LinkGraph > G, std::unique_ptr< JITLinkContext > Ctx)
jit-link the given object buffer, which must be a COFF x86-64 object file.
LLVM_ABI void printEdge(raw_ostream &OS, const Block &B, const Edge &E, StringRef EdgeKindName)
const char * getCOFFX86RelocationKindName(Edge::Kind R)
Return the string name of the given COFF x86-64 edge kind.
detail::packed_endian_specific_integral< int16_t, llvm::endianness::little, unaligned > little16_t
detail::packed_endian_specific_integral< int32_t, llvm::endianness::little, unaligned > little32_t
detail::packed_endian_specific_integral< int64_t, llvm::endianness::little, unaligned > little64_t
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
An LinkGraph pass configuration, consisting of a list of pre-prune, post-prune, and post-fixup passes...
LinkGraphPassList PreFixupPasses
Pre-fixup passes.
LinkGraphPassList PrePrunePasses
Pre-prune passes.
support::ulittle32_t SymbolTableIndex