22#define DEBUG_TYPE "jitlink"
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)) {}
57 for (
const auto &RelSect :
sections())
59 RelSect,
this, &COFFLinkGraphBuilder_x86_64::addSingleRelocation))
70 if (SymbolIt ==
getObject().symbol_end()) {
71 return make_error<StringError>(
72 formatv(
"Invalid symbol index in relocation entry. "
73 "index: {0}, section: {1}",
83 return make_error<StringError>(
84 formatv(
"Could not find symbol at given index, did you add it to "
85 "JITSymbolTable? index: {0}, section: {1}",
99 Kind = EdgeKind_coff_x86_64::Pointer32NB;
104 Kind = EdgeKind_coff_x86_64::PCRel32;
109 Kind = EdgeKind_coff_x86_64::PCRel32;
115 Kind = EdgeKind_coff_x86_64::PCRel32;
121 Kind = EdgeKind_coff_x86_64::PCRel32;
127 Kind = EdgeKind_coff_x86_64::PCRel32;
133 Kind = EdgeKind_coff_x86_64::PCRel32;
139 Kind = EdgeKind_coff_x86_64::Pointer64;
144 Kind = EdgeKind_coff_x86_64::SectionIdx16;
153 Scope::Local,
false);
154 GraphSymbol = AbsSym;
161 Kind = EdgeKind_coff_x86_64::SecRel32;
166 return make_error<JITLinkError>(
"Unsupported x86_64 relocation:" +
178 BlockToFix.
addEdge(std::move(GE));
190class COFFLinkGraphLowering_x86_64 {
194 for (
auto *
B :
G.blocks()) {
195 for (
auto &E :
B->edges()) {
196 switch (E.getKind()) {
197 case EdgeKind_coff_x86_64::Pointer32NB: {
198 auto ImageBase = getImageBaseAddress(
G, Ctx);
200 return ImageBase.takeError();
201 E.setAddend(E.getAddend() - ImageBase->getValue());
205 case EdgeKind_coff_x86_64::PCRel32: {
209 case EdgeKind_coff_x86_64::Pointer64: {
213 case EdgeKind_coff_x86_64::SectionIdx16: {
217 case EdgeKind_coff_x86_64::SecRel32: {
218 E.setAddend(E.getAddend() -
219 getSectionStart(E.getTarget().getBlock().getSection())
233 static StringRef getImageBaseSymbolName() {
return "__ImageBase"; }
236 if (!SectionStartCache.count(&Sec)) {
238 SectionStartCache[&Sec] =
Range.getStart();
240 return SectionStartCache[&Sec];
246 return this->ImageBase;
247 for (
auto *S :
G.defined_symbols())
248 if (S->getName() == getImageBaseSymbolName()) {
249 this->ImageBase = S->getAddress();
250 return this->ImageBase;
254 Symbols[getImageBaseSymbolName()] = SymbolLookupFlags::RequiredSymbol;
264 ImageBase = LR->begin()->second.getAddress();
267 return std::move(Err);
268 this->ImageBase = ImageBase;
278 COFFLinkGraphLowering_x86_64 GraphLowering;
280 if (
auto Err = GraphLowering.lowerCOFFRelocationEdges(
G, *Ctx))
296 return "Pointer32NB";
300 return "SectionIdx16";
311 dbgs() <<
"Building jitlink graph for new input "
317 return COFFObj.takeError();
319 auto Features = (*COFFObj)->getFeatures();
321 return Features.takeError();
323 return COFFLinkGraphBuilder_x86_64(**COFFObj, (*COFFObj)->makeTriple(),
324 std::move(*Features))
329 std::unique_ptr<JITLinkContext> Ctx) {
331 const Triple &TT =
G->getTargetTriple();
332 if (Ctx->shouldAddDefaultTargetPasses(TT)) {
334 if (
auto MarkLive = Ctx->getMarkLivePass(TT)) {
335 Config.PrePrunePasses.push_back(std::move(MarkLive));
342 Config.PreFixupPasses.push_back(
343 [CtxPtr](
LinkGraph &
G) {
return lowerEdges_COFF_x86_64(
G, CtxPtr); });
346 if (
auto Err = Ctx->modifyPassConfig(*
G,
Config))
347 return Ctx->notifyFailed(std::move(Err));
349 COFFJITLinker_x86_64::link(std::move(Ctx), std::move(
G), std::move(
Config));
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
Helper for Errors used as out-parameters.
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.
Error takeError()
Take ownership of the stored error.
StringRef getBufferIdentifier() const
StringRef - Represent a constant reference to a string, i.e.
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.
virtual Error addRelocations()=0
Error forEachRelocation(const object::SectionRef &RelSec, RelocHandlerFunction &&Func, bool ProcessDebugSections=false)
Traverse all matching relocation records in the given section.
Symbol * getGraphSymbol(COFFSymbolIndex SymIndex) const
object::COFFObjectFile::section_iterator_range sections() const
LinkGraph & getGraph() const
const object::COFFObjectFile & getObject() const
Represents fixups and constraints in the LinkGraph.
Holds context for a single jitLink invocation.
virtual void lookup(const LookupMap &Symbols, std::unique_ptr< JITLinkAsyncLookupContinuation > LC)=0
Called by JITLink to resolve external symbols.
Symbol & addAbsoluteSymbol(StringRef Name, orc::ExecutorAddr Address, orc::ExecutorAddrDiff Size, Linkage L, Scope S, bool IsLive)
Add an absolute symbol.
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....
const coff_relocation * getCOFFRelocation(const RelocationRef &Reloc) const
uint32_t getNumberOfSections() const
uint32_t getSymbolIndex(COFFSymbolRef Symbol) const
COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const
int32_t getSectionNumber() const
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
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
std::unique_ptr< JITLinkAsyncLookupContinuation > createLookupContinuation(Continuation Cont)
Create a lookup continuation from a function object.
Error markAllSymbolsLive(LinkGraph &G)
Marks all symbols in a graph live.
Expected< std::unique_ptr< LinkGraph > > createLinkGraphFromCOFFObject_x86_64(MemoryBufferRef ObjectBuffer)
Create a LinkGraph from an COFF/x86-64 relocatable object.
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.
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.
This is an optimization pass for GlobalISel generic memory operations.
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
auto formatv(const char *Fmt, Ts &&...Vals) -> formatv_object< decltype(std::make_tuple(support::detail::build_format_adapter(std::forward< Ts >(Vals))...))>
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
An LinkGraph pass configuration, consisting of a list of pre-prune, post-prune, and post-fixup passes...
support::ulittle32_t SymbolTableIndex