Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -329,6 +329,7 @@ return R_TLSLD; case R_386_PLT32: return R_PLT_PC; + case R_386_PC16: case R_386_PC32: return R_PC; case R_386_GOTPC: @@ -437,11 +438,13 @@ switch (Type) { default: return 0; + case R_386_16: case R_386_32: case R_386_GOT32: case R_386_GOT32X: case R_386_GOTOFF: case R_386_GOTPC: + case R_386_PC16: case R_386_PC32: case R_386_PLT32: case R_386_TLS_LE: @@ -451,6 +454,16 @@ void X86TargetInfo::relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const { + // A program or object file using R_386_8, R_386_16, R_386_PC16 or R_386_PC8 + // relocations is not conformant to latest ABI. The R_386_16, and R_386_8 + // relocations truncate the computed value to 16 - bits and 8 - bits + // respectively. Unfortunately R_386_PC16 and R_386_16 are used by some + // applications, for example by FreeBSD loaders. + if (Type == R_386_16 || Type == R_386_PC16) { + write16le(Loc, Val); + return; + } + checkInt<32>(Loc, Val, Type); write32le(Loc, Val); } Index: test/ELF/Inputs/i386-pc16.s =================================================================== --- test/ELF/Inputs/i386-pc16.s +++ test/ELF/Inputs/i386-pc16.s @@ -0,0 +1,3 @@ +.global bar +bar: + .quad 2 Index: test/ELF/i386-pc16.s =================================================================== --- test/ELF/i386-pc16.s +++ test/ELF/i386-pc16.s @@ -0,0 +1,20 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %s -o %t.o +# RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %S/Inputs/i386-pc16.s -o %t2.o +# RUN: ld.lld %t.o %t2.o -o %t.exe +# RUN: llvm-objdump -s -section=.text %t.exe | FileCheck %s + +# CHECK: Contents of section .text: +# CHECK: 11000 be071010 85090002 00000000 00000000 +# CHECK: 11010 02000000 00000000 + +.global _start +.code16 +_start: + movw $foo,%si #R_386_16 + jne bar #R_386_PC16 + +foo: + .quad 1 +