diff --git a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp index 30366a82a0436..244975f4a51ac 100644 --- a/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp +++ b/llvm/lib/ExecutionEngine/JITLink/ELF_x86_64.cpp @@ -670,6 +670,17 @@ class ELFJITLinker_x86_64 : public JITLinker { return getELFX86RelocationKindName(R); } + static Error targetOutOfRangeError(const Block &B, const Edge &E) { + std::string ErrMsg; + { + raw_string_ostream ErrStream(ErrMsg); + ErrStream << "Relocation target out of range: "; + printEdge(ErrStream, B, E, getELFX86RelocationKindName(E.getKind())); + ErrStream << "\n"; + } + return make_error(std::move(ErrMsg)); + } + Error applyFixup(Block &B, const Edge &E, char *BlockWorkingMem) const { using namespace ELF_x86_64_Edges; using namespace llvm::support; @@ -681,12 +692,15 @@ class ELFJITLinker_x86_64 : public JITLinker { case ELFX86RelocationKind::PCRel32: case ELFX86RelocationKind::PCRel32GOTLoad: { int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress; - endian::write32le(FixupPtr, Value); + if (Value < std::numeric_limits::min() || + Value > std::numeric_limits::max()) + return targetOutOfRangeError(B, E); + *(little32_t *)FixupPtr = Value; break; } case ELFX86RelocationKind::Pointer64: { int64_t Value = E.getTarget().getAddress() + E.getAddend(); - endian::write64le(FixupPtr, Value); + *(ulittle64_t *)FixupPtr = Value; break; } }