[PATCH] D29490: [ELF] - Use SignExtend when reading R_386_PC8 addend.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 3 05:40:55 PST 2017


grimar created this revision.

Previously we did not do that. A result was that 0xFF addend was
not treated as 0xFFFFFFFF(-1), but was 0x000000FF.
Recently added check for R_386_PC8 failed because of calculation 
overflow as a result.

Interesting, that ignoring such checks would still produce the correct result
in the tested reproducables.

This patch fixes the case mentioned in 
https://reviews.llvm.org/D29392#664771


https://reviews.llvm.org/D29490

Files:
  ELF/Target.cpp
  test/ELF/i386-pc8-addend.s


Index: test/ELF/i386-pc8-addend.s
===================================================================
--- test/ELF/i386-pc8-addend.s
+++ test/ELF/i386-pc8-addend.s
@@ -0,0 +1,42 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=i386-pc-linux-gnu %s -o %t1.o
+# RUN: llvm-objdump -d %t1.o | FileCheck %s --check-prefix=OBJDISASM
+# RUN: llvm-readobj -r %t1.o | FileCheck %s --check-prefix=OBJRELOC
+
+## Show that we have R_386_PC8 relocation with addend = 0xFF(-1).
+# OBJDISASM:      Disassembly of section .text:
+# OBJDISASM-NEXT: .text:
+# OBJDISASM-NEXT:  0: eb ff jmp -1 <.text+0x1>
+# OBJRELOC:       Relocations [
+# OBJRELOC-NEXT:   Section {{.*}} .rel.text {
+# OBJRELOC-NEXT:     0x1 R_386_PC8 .foo 0x0
+# OBJRELOC-NEXT:   }
+# OBJRELOC-NEXT: ]
+
+## Check that there is no link error and relocation applied correctly.
+# RUN: ld.lld %t1.o -o %t.out
+# RUN: llvm-objdump -d %t.out | FileCheck %s --check-prefix=DISASM
+# DISASM:      Disassembly of section .text:
+# DISASM-NEXT: .text:
+# DISASM-NEXT:    11000: eb 04 jmp 4 <foo>
+# DISASM:      Disassembly of section .foo:
+# DISASM-NEXT: foo:
+# DISASM-NEXT:    11006: 90 nop
+
+# CHECK:      Contents of section .text:
+# CHECK-NEXT:  0000 15253748
+
+.code16
+
+.byte  0xeb   # short (2-byte) jump
+.byte  foo-1f
+
+1:
+nop
+nop
+nop
+nop
+
+.section ".foo", "ax"
+foo:
+ nop
Index: ELF/Target.cpp
===================================================================
--- ELF/Target.cpp
+++ ELF/Target.cpp
@@ -492,8 +492,9 @@
   default:
     return 0;
   case R_386_8:
-  case R_386_PC8:
     return *Buf;
+  case R_386_PC8:
+    return SignExtend64<8>(*Buf);
   case R_386_16:
   case R_386_PC16:
     return read16le(Buf);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D29490.86952.patch
Type: text/x-patch
Size: 1699 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170203/573901a1/attachment.bin>


More information about the llvm-commits mailing list