Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -492,11 +492,13 @@ 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); + case R_386_PC16: + return SignExtend64<16>(read16le(Buf)); case R_386_32: case R_386_GOT32: case R_386_GOT32X: Index: test/ELF/i386-pc8-pc16-addend.s =================================================================== --- test/ELF/i386-pc8-pc16-addend.s +++ test/ELF/i386-pc8-pc16-addend.s @@ -0,0 +1,32 @@ +# 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 + +## Show that we have R_386_PC8 relocation with addend = 0xFF(-1), +## and R_386_PC16 relocation with addend = 0xFEFF(-2) +## we do sign extend for addends values in the code. +# OBJDISASM: Disassembly of section .text: +# OBJDISASM-NEXT: .text: +# OBJDISASM-NEXT: 0: ff fe +# OBJDISASM-NEXT: 2: ff + +## 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 +# RUN: llvm-readobj -symbols %t.out | FileCheck %s --check-prefix=SYM +# DISASM: Disassembly of section .text: +# DISASM-NEXT: .text: +# DISASM-NEXT: 11000: 02 00 +# DISASM-NEXT: 11002: 00 +## 0x11003 - 0x11000 + addend(-1) = 0x02 +## 0x11003 - 0x11001 + addend(-2) = 0x0000 +# SYM: Symbol { +# SYM: Name: und +# SYM-NEXT: Value: 0x11003 + +.code16 +.byte und-.-1 +.word und-.-2 + +.section .und, "ax" +und: