LLVM 24.0.0git
COFFAutoImportGenerator.h
Go to the documentation of this file.
1//===- COFFAutoImportGenerator.h - COFF dllimport auto-import -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Declares COFFAutoImportGenerator, which synthesizes COFF dllimport __imp_
10// symbols and jump-thunks for the symbols exported by a single dynamic
11// library.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_EXECUTIONENGINE_ORC_COFFAUTOIMPORTGENERATOR_H
16#define LLVM_EXECUTIONENGINE_ORC_COFFAUTOIMPORTGENERATOR_H
17
18#include "llvm/ADT/StringRef.h"
24
25namespace llvm::orc {
26
27/// A utility class that synthesizes COFF dllimport __imp_ symbols and PLT
28/// stubs for the symbols exported by a single dynamic library ("easy mode"
29/// auto-import).
30///
31/// Unlike DLLImportDefinitionGenerator, which resolves the underlying symbol
32/// through the JITDylib's link order, this generator is bound to one dynamic
33/// library: that library's export table is the authority on what may be
34/// synthesized. Any requested symbol the library does not export is left
35/// unresolved, so the link fails exactly as a static link against the
36/// corresponding import library would.
37///
38/// Synthesis is lazy (driven by JITLink external-symbol lookups) and assumes
39/// every import is a function: for each resolved import X it creates an __imp_X
40/// pointer slot holding X's address in the library and an X thunk that jumps
41/// through that slot. Data imports are not distinguished from code and will
42/// misbehave; clients with data imports must supply an import library or use
43/// __declspec(dllimport). Note also that &X resolves to the synthesized thunk,
44/// not to X's address inside the library.
45///
46/// All synthesized stubs share a single ResourceTracker; see
47/// getImportStubsResourceTracker() to reclaim them.
48///
49/// Supports whichever architectures JITLink has a pointer / pointer-jump-stub
50/// creator registered for (see jitlink::getAnonymousPointerCreator and
51/// jitlink::getPointerJumpStubCreator); Load() fails for any other
52/// architecture.
53class LLVM_ABI COFFAutoImportGenerator : public DefinitionGenerator {
54public:
55 /// Loads the dynamic library at the given path in the executor (via the given
56 /// DylibManager) and, on success, returns a COFFAutoImportGenerator that
57 /// synthesizes imports for the symbols it exports. On failure returns the
58 /// reason the library failed to load. Resolving imports through DylibManager
59 /// means this works for both in-process and out-of-process execution.
62 const char *LibraryPath);
63
65 JITDylibLookupFlags JDLookupFlags,
66 const SymbolLookupSet &Symbols) override;
67
68 /// Returns the ResourceTracker that owns the stubs synthesized by this
69 /// generator, or null if none have been synthesized yet. Calling remove() on
70 /// it reclaims every synthesized __imp_ slot and thunk without affecting
71 /// other definitions in the JITDylib; synthesis afterwards transparently
72 /// starts a fresh tracker. Not thread-safe with respect to lookups that may
73 /// concurrently trigger synthesis -- reclaim at a quiescent point.
75 return ImportStubsRT;
76 }
77
78private:
80 DylibManager &DylibMgr,
81 tpctypes::DylibHandle LibHandle,
84 : ES(ES), L(L), DylibMgr(DylibMgr), LibHandle(LibHandle),
85 CreatePointer(std::move(CreatePointer)),
86 CreateStub(std::move(CreateStub)) {}
87
89 createStubsGraph(const SymbolMap &Resolved);
90
91 static constexpr StringLiteral getImpPrefix() { return "__imp_"; }
92 static constexpr StringLiteral getSectionName() {
93 return "$__AUTOIMPORT_STUBS";
94 }
95
96 ExecutionSession &ES;
97 ObjectLinkingLayer &L;
98 DylibManager &DylibMgr;
99 tpctypes::DylibHandle LibHandle;
100
101 /// Cached at Load() time so unsupported architectures are rejected eagerly,
102 /// rather than later inside tryToGenerate's asynchronous lookup callback.
103 jitlink::AnonymousPointerCreator CreatePointer;
104 jitlink::PointerJumpStubCreator CreateStub;
105
106 /// Owns the synthesized stubs; (re)created lazily on first synthesis.
107 ResourceTrackerSP ImportStubsRT;
108};
109
110} // namespace llvm::orc
111
112#endif // LLVM_EXECUTIONENGINE_ORC_COFFAUTOIMPORTGENERATOR_H
#define LLVM_ABI
Definition Compiler.h:215
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition StringRef.h:888
A utility class that synthesizes COFF dllimport __imp_ symbols and PLT stubs for the symbols exported...
static Expected< std::unique_ptr< COFFAutoImportGenerator > > Load(ExecutionSession &ES, ObjectLinkingLayer &L, DylibManager &DylibMgr, const char *LibraryPath)
Loads the dynamic library at the given path in the executor (via the given DylibManager) and,...
Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD, JITDylibLookupFlags JDLookupFlags, const SymbolLookupSet &Symbols) override
DefinitionGenerators should override this method to insert new definitions into the parent JITDylib.
ResourceTrackerSP getImportStubsResourceTracker() const
Returns the ResourceTracker that owns the stubs synthesized by this generator, or null if none have b...
Definition generators can be attached to JITDylibs to generate new definitions for otherwise unresolv...
Definition Core.h:632
friend class ExecutionSession
Definition Core.h:633
An ExecutionSession represents a running JIT program.
Definition Core.h:1111
Represents a JIT'd dynamic library.
Definition Core.h:675
Wraps state for a lookup-in-progress.
Definition Core.h:607
An ObjectLayer implementation built on JITLink.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
ExecutorAddr DylibHandle
A handle used to represent a loaded dylib in the target process.
IntrusiveRefCntPtr< ResourceTracker > ResourceTrackerSP
Definition Core.h:57
JITDylibLookupFlags
Lookup flags that apply to each dylib in the search order for a lookup.
Definition Core.h:132
LookupKind
Describes the kind of lookup being performed.
Definition Core.h:144
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878