Fixes handling logic for i386/ELF GOTPC relocation
authorKshitij Jain <jkshtj@outlook.com>
Sat, 14 Jan 2023 01:31:43 +0000 (01:31 +0000)
committerKshitij Jain <jkshtj@outlook.com>
Sat, 14 Jan 2023 05:01:49 +0000 (05:01 +0000)
The i386/ELF JITLink backend was not correctly handling the GOTPC relocation
by skipping the in-built addend, which was manifesting itself in the form of
a segmentation fault in the `LF_external_to_absolute_conversion.s` test. This
CR has fixed that issue.

Reviewed By: lhames

Differential Revision: https://reviews.llvm.org/D141746

llvm/lib/ExecutionEngine/JITLink/ELF_i386.cpp

index 07f5ecc..dec59e4 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ExecutionEngine/JITLink/ELF_i386.h"
+#include "DefineExternalSectionStartAndEndSymbols.h"
 #include "ELFLinkGraphBuilder.h"
 #include "JITLinkGeneric.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/ExecutionEngine/JITLink/i386.h"
 #include "llvm/Object/ELFObjectFile.h"
 
-#include "DefineExternalSectionStartAndEndSymbols.h"
-
 #define DEBUG_TYPE "jitlink"
 
 using namespace llvm;
@@ -180,9 +179,18 @@ private:
     if (!Kind)
       return Kind.takeError();
 
+    auto FixupAddress = orc::ExecutorAddr(FixupSection.sh_addr) + Rel.r_offset;
     int64_t Addend = 0;
 
-    auto FixupAddress = orc::ExecutorAddr(FixupSection.sh_addr) + Rel.r_offset;
+    switch (*Kind) {
+    case i386::EdgeKind_i386::Delta32: {
+      const char *FixupContent = BlockToFix.getContent().data() +
+                                 (FixupAddress - BlockToFix.getAddress());
+      Addend = *(const support::ulittle32_t *)FixupContent;
+      break;
+    }
+    }
+
     Edge::OffsetT Offset = FixupAddress - BlockToFix.getAddress();
     Edge GE(*Kind, Offset, *GraphSymbol, Addend);
     LLVM_DEBUG({