Index: lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h =================================================================== --- lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h +++ lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldCOFFX86_64.h @@ -30,15 +30,33 @@ // unregisteredEH frame sections with the memory manager. SmallVector UnregisteredEHFrameSections; SmallVector RegisteredEHFrameSections; + uint64_t ImageBase; + + // Fake an __ImageBase pointer by returning the section with the lowest adress + uint64_t getImageBase() { + if (!ImageBase) { + ImageBase = std::numeric_limits::max(); + for (const SectionEntry &Section : Sections) + ImageBase = std::min(ImageBase, Section.getLoadAddress()); + } + return ImageBase; + } + + void writeDeltaReloc(uint8_t *Target, int64_t Addend, uint64_t Delta) { + uint64_t Result = Addend + Delta; + assert(Result <= UINT32_MAX && "Relocation overflow"); + writeBytesUnaligned(Result, Target, 4); + } public: RuntimeDyldCOFFX86_64(RuntimeDyld::MemoryManager &MM, JITSymbolResolver &Resolver) - : RuntimeDyldCOFF(MM, Resolver) {} + : RuntimeDyldCOFF(MM, Resolver), ImageBase(0) {} - unsigned getMaxStubSize() override { - return 6; // 2-byte jmp instruction + 32-bit relative address - } + unsigned getStubAlignment() override { return 1; } + + // 2-byte jmp instruction + 32-bit relative address + 64-bit absolute jump + unsigned getMaxStubSize() override { return 14; } // The target location for the relocation is described by RE.SectionID and // RE.Offset. RE.SectionID can be used to find the SectionEntry. Each @@ -85,13 +103,17 @@ } case COFF::IMAGE_REL_AMD64_ADDR32NB: { - // Note ADDR32NB requires a well-established notion of - // image base. This address must be less than or equal - // to every section's load address, and all sections must be - // within a 32 bit offset from the base. - // - // For now we just set these to zero. - writeBytesUnaligned(0, Target, 4); + // ADDR32NB requires an offset less than 2GB from 'ImageBase'. + // The MemoryManager can make sure this is always true by forcing the + // memory layout to be: CodeSection < ReadOnlySection < ReadWriteSection. + const uint64_t ImageBase = getImageBase(); + if (Value < ImageBase || ((Value - ImageBase) > UINT32_MAX)) { + llvm::errs() << "IMAGE_REL_AMD64_ADDR32NB relocation requires an" + << "ordered section layout."; + writeDeltaReloc(Target, 0, 0); + } else + writeDeltaReloc(Target, RE.Addend, Value - ImageBase); + break; } @@ -106,6 +128,56 @@ } } + std::tuple + generateRelocationStub(unsigned SectionID, StringRef TargetName, + uint64_t Offset, uint64_t RelType, + uint64_t Addend, StubMap &Stubs) { + uintptr_t StubOffset; + SectionEntry &Section = Sections[SectionID]; + + { + RelocationValueRef OriginalRelValueRef; + OriginalRelValueRef.SectionID = SectionID; + OriginalRelValueRef.Offset = Offset; + OriginalRelValueRef.Addend = Addend; + OriginalRelValueRef.SymbolName = TargetName.data(); + + auto Itr = Stubs.find(OriginalRelValueRef); + if (Itr == Stubs.end()) { + DEBUG(dbgs() << " Create a new stub function for " + << TargetName.data() << "\n"); + + StubOffset = Section.getStubOffset(); + Stubs[OriginalRelValueRef] = StubOffset; + createStubFunction(Section.getAddressWithOffset(StubOffset)); + Section.advanceStubOffset(getMaxStubSize()); + } + else { + DEBUG(dbgs() << " Stub function found for " + << TargetName.data() << "\n"); + + StubOffset = Itr->second; + } + } + + // FIXME: If RelType == COFF::IMAGE_REL_AMD64_ADDR32NB we should be able + // to ignore the __ImageBase requirement and just forward to the stub + // directly as an offset of this section: + // writeDeltaReloc(Section.getAddressWithOffset(Offset), 0, StubOffset); + // .xdata exception handler's aren't having this though. + + // Resolve original relocation to stub function. + const RelocationEntry RE(SectionID, Offset, RelType, Addend); + resolveRelocation(RE, Section.getLoadAddressWithOffset(StubOffset)); + + // adjust relocation info so resolution writes to the stub function + Addend = 0; + Offset = StubOffset + 6; + RelType = COFF::IMAGE_REL_AMD64_ADDR64; + + return std::make_tuple(Offset, RelType, Addend); + } + Expected processRelocationRef(unsigned SectionID, relocation_iterator RelI, @@ -131,6 +203,11 @@ SectionEntry &Section = Sections[SectionID]; uintptr_t ObjTarget = Section.getObjAddress() + Offset; + Expected TargetNameOrErr = Symbol->getName(); + if (!TargetNameOrErr) + return TargetNameOrErr.takeError(); + StringRef TargetName = *TargetNameOrErr; + switch (RelType) { case COFF::IMAGE_REL_AMD64_REL32: @@ -142,6 +219,11 @@ case COFF::IMAGE_REL_AMD64_ADDR32NB: { uint8_t *Displacement = (uint8_t *)ObjTarget; Addend = readBytesUnaligned(Displacement, 4); + + if (IsExtern) + std::tie(Offset, RelType, Addend) = generateRelocationStub( + SectionID, TargetName, Offset, RelType, Addend, Stubs); + break; } @@ -155,11 +237,6 @@ break; } - Expected TargetNameOrErr = Symbol->getName(); - if (!TargetNameOrErr) - return TargetNameOrErr.takeError(); - StringRef TargetName = *TargetNameOrErr; - DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset << " RelType: " << RelType << " TargetName: " << TargetName << " Addend " << Addend << "\n"); @@ -183,7 +260,6 @@ return ++RelI; } - unsigned getStubAlignment() override { return 1; } void registerEHFrames() override { for (auto const &EHFrameSID : UnregisteredEHFrameSections) { uint8_t *EHFrameAddr = Sections[EHFrameSID].getAddress(); @@ -202,9 +278,10 @@ StringRef Name; if (auto EC = Section.getName(Name)) return errorCodeToError(EC); - // Note unwind info is split across .pdata and .xdata, so this - // may not be sufficiently general for all users. - if (Name == ".xdata") { + // Note unwind info is stored in .pdata but often points to .xdata + // with an IMAGE_REL_AMD64_ADDR32NB relocation. Using a memory manager + // that keeps sections ordered in relation to __ImageBase is necessary. + if (Name == ".pdata") { UnregisteredEHFrameSections.push_back(SectionPair.second); } }