File indexing completed on 2026-05-10 08:43:50
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_EXECUTIONENGINE_JITLINK_I386_H
0014 #define LLVM_EXECUTIONENGINE_JITLINK_I386_H
0015
0016 #include "llvm/ExecutionEngine/JITLink/JITLink.h"
0017 #include "llvm/ExecutionEngine/JITLink/TableManager.h"
0018
0019 namespace llvm::jitlink::i386 {
0020
0021 enum EdgeKind_i386 : Edge::Kind {
0022
0023
0024 None = Edge::FirstRelocation,
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035 Pointer32,
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 PCRel32,
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 Pointer16,
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 PCRel16,
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 Delta32,
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 Delta32FromGOT,
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 RequestGOTAndTransformToDelta32FromGOT,
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132 BranchPCRel32,
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161 BranchPCRel32ToPtrJumpStub,
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 BranchPCRel32ToPtrJumpStubBypassable,
0178 };
0179
0180
0181
0182 const char *getEdgeKindName(Edge::Kind K);
0183
0184
0185 inline Error applyFixup(LinkGraph &G, Block &B, const Edge &E,
0186 const Symbol *GOTSymbol) {
0187 using namespace i386;
0188 using namespace llvm::support;
0189
0190 char *BlockWorkingMem = B.getAlreadyMutableContent().data();
0191 char *FixupPtr = BlockWorkingMem + E.getOffset();
0192 auto FixupAddress = B.getAddress() + E.getOffset();
0193
0194 switch (E.getKind()) {
0195 case i386::None: {
0196 break;
0197 }
0198
0199 case i386::Pointer32: {
0200 uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
0201 *(ulittle32_t *)FixupPtr = Value;
0202 break;
0203 }
0204
0205 case i386::PCRel32: {
0206 int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
0207 *(little32_t *)FixupPtr = Value;
0208 break;
0209 }
0210
0211 case i386::Pointer16: {
0212 uint32_t Value = E.getTarget().getAddress().getValue() + E.getAddend();
0213 if (LLVM_LIKELY(isUInt<16>(Value)))
0214 *(ulittle16_t *)FixupPtr = Value;
0215 else
0216 return makeTargetOutOfRangeError(G, B, E);
0217 break;
0218 }
0219
0220 case i386::PCRel16: {
0221 int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
0222 if (LLVM_LIKELY(isInt<16>(Value)))
0223 *(little16_t *)FixupPtr = Value;
0224 else
0225 return makeTargetOutOfRangeError(G, B, E);
0226 break;
0227 }
0228
0229 case i386::Delta32: {
0230 int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
0231 *(little32_t *)FixupPtr = Value;
0232 break;
0233 }
0234
0235 case i386::Delta32FromGOT: {
0236 assert(GOTSymbol && "No GOT section symbol");
0237 int32_t Value =
0238 E.getTarget().getAddress() - GOTSymbol->getAddress() + E.getAddend();
0239 *(little32_t *)FixupPtr = Value;
0240 break;
0241 }
0242
0243 case i386::BranchPCRel32:
0244 case i386::BranchPCRel32ToPtrJumpStub:
0245 case i386::BranchPCRel32ToPtrJumpStubBypassable: {
0246 int32_t Value = E.getTarget().getAddress() - FixupAddress + E.getAddend();
0247 *(little32_t *)FixupPtr = Value;
0248 break;
0249 }
0250
0251 default:
0252 return make_error<JITLinkError>(
0253 "In graph " + G.getName() + ", section " + B.getSection().getName() +
0254 " unsupported edge kind " + getEdgeKindName(E.getKind()));
0255 }
0256
0257 return Error::success();
0258 }
0259
0260
0261 constexpr uint32_t PointerSize = 4;
0262
0263
0264 extern const char NullPointerContent[PointerSize];
0265
0266
0267
0268
0269
0270
0271 extern const char PointerJumpStubContent[6];
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283 inline Symbol &createAnonymousPointer(LinkGraph &G, Section &PointerSection,
0284 Symbol *InitialTarget = nullptr,
0285 uint64_t InitialAddend = 0) {
0286 auto &B = G.createContentBlock(PointerSection, NullPointerContent,
0287 orc::ExecutorAddr(), 8, 0);
0288 if (InitialTarget)
0289 B.addEdge(Pointer32, 0, *InitialTarget, InitialAddend);
0290 return G.addAnonymousSymbol(B, 0, PointerSize, false, false);
0291 }
0292
0293
0294
0295
0296
0297
0298
0299 inline Block &createPointerJumpStubBlock(LinkGraph &G, Section &StubSection,
0300 Symbol &PointerSymbol) {
0301 auto &B = G.createContentBlock(StubSection, PointerJumpStubContent,
0302 orc::ExecutorAddr(), 8, 0);
0303 B.addEdge(Pointer32,
0304
0305
0306
0307 2, PointerSymbol, 0);
0308 return B;
0309 }
0310
0311
0312
0313
0314
0315 inline Symbol &createAnonymousPointerJumpStub(LinkGraph &G,
0316 Section &StubSection,
0317 Symbol &PointerSymbol) {
0318 return G.addAnonymousSymbol(
0319 createPointerJumpStubBlock(G, StubSection, PointerSymbol), 0, 6, true,
0320 false);
0321 }
0322
0323
0324 class GOTTableManager : public TableManager<GOTTableManager> {
0325 public:
0326 static StringRef getSectionName() { return "$__GOT"; }
0327
0328 bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
0329 Edge::Kind KindToSet = Edge::Invalid;
0330 switch (E.getKind()) {
0331 case i386::Delta32FromGOT: {
0332
0333
0334 getGOTSection(G);
0335 return false;
0336 }
0337 case i386::RequestGOTAndTransformToDelta32FromGOT:
0338 KindToSet = i386::Delta32FromGOT;
0339 break;
0340 default:
0341 return false;
0342 }
0343 assert(KindToSet != Edge::Invalid &&
0344 "Fell through switch, but no new kind to set");
0345 DEBUG_WITH_TYPE("jitlink", {
0346 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
0347 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
0348 << formatv("{0:x}", E.getOffset()) << ")\n";
0349 });
0350 E.setKind(KindToSet);
0351 E.setTarget(getEntryForTarget(G, E.getTarget()));
0352 return true;
0353 }
0354
0355 Symbol &createEntry(LinkGraph &G, Symbol &Target) {
0356 return createAnonymousPointer(G, getGOTSection(G), &Target);
0357 }
0358
0359 private:
0360 Section &getGOTSection(LinkGraph &G) {
0361 if (!GOTSection)
0362 GOTSection = &G.createSection(getSectionName(), orc::MemProt::Read);
0363 return *GOTSection;
0364 }
0365
0366 Section *GOTSection = nullptr;
0367 };
0368
0369
0370 class PLTTableManager : public TableManager<PLTTableManager> {
0371 public:
0372 PLTTableManager(GOTTableManager &GOT) : GOT(GOT) {}
0373
0374 static StringRef getSectionName() { return "$__STUBS"; }
0375
0376 bool visitEdge(LinkGraph &G, Block *B, Edge &E) {
0377 if (E.getKind() == i386::BranchPCRel32 && !E.getTarget().isDefined()) {
0378 DEBUG_WITH_TYPE("jitlink", {
0379 dbgs() << " Fixing " << G.getEdgeKindName(E.getKind()) << " edge at "
0380 << B->getFixupAddress(E) << " (" << B->getAddress() << " + "
0381 << formatv("{0:x}", E.getOffset()) << ")\n";
0382 });
0383
0384
0385 E.setKind(i386::BranchPCRel32ToPtrJumpStubBypassable);
0386 E.setTarget(getEntryForTarget(G, E.getTarget()));
0387 return true;
0388 }
0389 return false;
0390 }
0391
0392 Symbol &createEntry(LinkGraph &G, Symbol &Target) {
0393 return createAnonymousPointerJumpStub(G, getStubsSection(G),
0394 GOT.getEntryForTarget(G, Target));
0395 }
0396
0397 public:
0398 Section &getStubsSection(LinkGraph &G) {
0399 if (!PLTSection)
0400 PLTSection = &G.createSection(getSectionName(),
0401 orc::MemProt::Read | orc::MemProt::Exec);
0402 return *PLTSection;
0403 }
0404
0405 GOTTableManager &GOT;
0406 Section *PLTSection = nullptr;
0407 };
0408
0409
0410
0411
0412
0413
0414
0415 Error optimizeGOTAndStubAccesses(LinkGraph &G);
0416
0417 }
0418
0419 #endif