Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:50

0001 //=== i386.h - Generic JITLink i386 edge kinds, utilities -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // Generic utilities for graphs representing i386 objects.
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 /// Represets i386 fixups
0021 enum EdgeKind_i386 : Edge::Kind {
0022 
0023   /// None
0024   None = Edge::FirstRelocation,
0025 
0026   /// A plain 32-bit pointer value relocation.
0027   ///
0028   /// Fixup expression:
0029   ///   Fixup <- Target + Addend : uint32
0030   ///
0031   /// Errors:
0032   ///   - The target must reside in the low 32-bits of the address space,
0033   ///     otherwise an out-of-range error will be returned.
0034   ///
0035   Pointer32,
0036 
0037   /// A 32-bit PC-relative relocation.
0038   ///
0039   /// Represents a data/control flow instruction using PC-relative addressing
0040   /// to a target.
0041   ///
0042   /// Fixup expression:
0043   ///   Fixup <- Target - Fixup + Addend : int32
0044   ///
0045   /// Errors:
0046   ///   - The result of the fixup expression must fit into an int32, otherwise
0047   ///     an out-of-range error will be returned.
0048   ///
0049   PCRel32,
0050 
0051   /// A plain 16-bit pointer value relocation.
0052   ///
0053   /// Fixup expression:
0054   ///   Fixup <- Target + Addend : uint16
0055   ///
0056   /// Errors:
0057   ///   - The target must reside in the low 16-bits of the address space,
0058   ///     otherwise an out-of-range error will be returned.
0059   ///
0060   Pointer16,
0061 
0062   /// A 16-bit PC-relative relocation.
0063   ///
0064   /// Represents a data/control flow instruction using PC-relative addressing
0065   /// to a target.
0066   ///
0067   /// Fixup expression:
0068   ///   Fixup <- Target - Fixup + Addend : int16
0069   ///
0070   /// Errors:
0071   ///   - The result of the fixup expression must fit into an int16, otherwise
0072   ///     an out-of-range error will be returned.
0073   ///
0074   PCRel16,
0075 
0076   /// A 32-bit delta.
0077   ///
0078   /// Delta from the fixup to the target.
0079   ///
0080   /// Fixup expression:
0081   ///   Fixup <- Target - Fixup + Addend : int32
0082   ///
0083   /// Errors:
0084   ///   - The result of the fixup expression must fit into an int32, otherwise
0085   ///     an out-of-range error will be returned.
0086   Delta32,
0087 
0088   /// A 32-bit GOT delta.
0089   ///
0090   /// Delta from the global offset table to the target.
0091   ///
0092   /// Fixup expression:
0093   ///   Fixup <- Target - GOTSymbol + Addend : int32
0094   ///
0095   /// Errors:
0096   ///   - *ASSERTION* Failure to a null pointer GOTSymbol, which the GOT section
0097   ///     symbol was not been defined.
0098   Delta32FromGOT,
0099 
0100   /// A GOT entry offset within GOT getter/constructor, transformed to
0101   /// Delta32FromGOT pointing at the GOT entry for the original target.
0102   ///
0103   /// Indicates that this edge should be transformed into a Delta32FromGOT
0104   /// targeting the GOT entry for the edge's current target, maintaining the
0105   /// same addend.
0106   /// A GOT entry for the target should be created if one does not already
0107   /// exist.
0108   ///
0109   /// Edges of this kind are usually handled by a GOT builder pass inserted by
0110   /// default
0111   ///
0112   /// Fixup expression:
0113   ///   NONE
0114   ///
0115   /// Errors:
0116   ///   - *ASSERTION* Failure to handle edges of this kind prior to the fixup
0117   ///     phase will result in an assert/unreachable during the fixup phase
0118   RequestGOTAndTransformToDelta32FromGOT,
0119 
0120   /// A 32-bit PC-relative branch.
0121   ///
0122   /// Represents a PC-relative call or branch to a target. This can be used to
0123   /// identify, record, and/or patch call sites.
0124   ///
0125   /// Fixup expression:
0126   ///   Fixup <- Target - Fixup + Addend : int32
0127   ///
0128   /// Errors:
0129   ///   - The result of the fixup expression must fit into an int32, otherwise
0130   ///     an out-of-range error will be returned.
0131   ///
0132   BranchPCRel32,
0133 
0134   /// A 32-bit PC-relative branch to a pointer jump stub.
0135   ///
0136   /// The target of this relocation should be a pointer jump stub of the form:
0137   ///
0138   /// \code{.s}
0139   ///   .text
0140   ///   jmp *tgtptr
0141   ///   ; ...
0142   ///
0143   ///   .data
0144   ///   tgtptr:
0145   ///     .quad 0
0146   /// \endcode
0147   ///
0148   /// This edge kind has the same fixup expression as BranchPCRel32, but further
0149   /// identifies the call/branch as being to a pointer jump stub. For edges of
0150   /// this kind the jump stub should not be bypassed (use
0151   /// BranchPCRel32ToPtrJumpStubBypassable for that), but the pointer location
0152   /// target may be recorded to allow manipulation at runtime.
0153   ///
0154   /// Fixup expression:
0155   ///   Fixup <- Target - Fixup + Addend : int32
0156   ///
0157   /// Errors:
0158   ///   - The result of the fixup expression must fit into an int32, otherwise
0159   ///     an out-of-range error will be returned.
0160   ///
0161   BranchPCRel32ToPtrJumpStub,
0162 
0163   /// A relaxable version of BranchPCRel32ToPtrJumpStub.
0164   ///
0165   /// The edge kind has the same fixup expression as BranchPCRel32ToPtrJumpStub,
0166   /// but identifies the call/branch as being to a pointer jump stub that may be
0167   /// bypassed with a direct jump to the ultimate target if the ultimate target
0168   /// is within range of the fixup location.
0169   ///
0170   /// Fixup expression:
0171   ///   Fixup <- Target - Fixup + Addend : int32
0172   ///
0173   /// Errors:
0174   ///   - The result of the fixup expression must fit into an int32, otherwise
0175   ///     an out-of-range error will be returned.
0176   ///
0177   BranchPCRel32ToPtrJumpStubBypassable,
0178 };
0179 
0180 /// Returns a string name for the given i386 edge. For debugging purposes
0181 /// only
0182 const char *getEdgeKindName(Edge::Kind K);
0183 
0184 /// Apply fixup expression for edge to block content.
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 /// i386 pointer size.
0261 constexpr uint32_t PointerSize = 4;
0262 
0263 /// i386 null pointer content.
0264 extern const char NullPointerContent[PointerSize];
0265 
0266 /// i386 pointer jump stub content.
0267 ///
0268 /// Contains the instruction sequence for an indirect jump via an in-memory
0269 /// pointer:
0270 ///   jmpq *ptr
0271 extern const char PointerJumpStubContent[6];
0272 
0273 /// Creates a new pointer block in the given section and returns an anonymous
0274 /// symbol pointing to it.
0275 ///
0276 /// If InitialTarget is given then an Pointer32 relocation will be added to the
0277 /// block pointing at InitialTarget.
0278 ///
0279 /// The pointer block will have the following default values:
0280 ///   alignment: 32-bit
0281 ///   alignment-offset: 0
0282 ///   address: highest allowable (~7U)
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 /// Create a jump stub block that jumps via the pointer at the given symbol.
0294 ///
0295 /// The stub block will have the following default values:
0296 ///   alignment: 8-bit
0297 ///   alignment-offset: 0
0298 ///   address: highest allowable: (~5U)
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             // Offset is 2 because the the first 2 bytes of the
0305             // jump stub block are {0xff, 0x25} -- an indirect absolute
0306             // jump.
0307             2, PointerSymbol, 0);
0308   return B;
0309 }
0310 
0311 /// Create a jump stub that jumps via the pointer at the given symbol and
0312 /// an anonymous symbol pointing to it. Return the anonymous symbol.
0313 ///
0314 /// The stub block will be created by createPointerJumpStubBlock.
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 /// Global Offset Table Builder.
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       // we need to make sure that the GOT section exists, but don't otherwise
0333       // need to fix up this edge
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 /// Procedure Linkage Table Builder.
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       // Set the edge kind to Branch32ToPtrJumpStubBypassable to enable it to
0384       // be optimized when the target is in-range.
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 /// Optimize the GOT and Stub relocations if the edge target address is in range
0410 /// 1. PCRel32GOTLoadRelaxable. For this edge kind, if the target is in range,
0411 /// then replace GOT load with lea. (THIS IS UNIMPLEMENTED RIGHT NOW!)
0412 /// 2. BranchPCRel32ToPtrJumpStubRelaxable. For this edge kind, if the target is
0413 /// in range, replace a indirect jump by plt stub with a direct jump to the
0414 /// target
0415 Error optimizeGOTAndStubAccesses(LinkGraph &G);
0416 
0417 } // namespace llvm::jitlink::i386
0418 
0419 #endif // LLVM_EXECUTIONENGINE_JITLINK_I386_H