[PATCH] D27303: [ELF] - Implemented R_386_16 and R_386PC16 relocations

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 1 08:50:38 PST 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: llvm-commits, grimar, emaste, evgeny777.

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.

Previously we did not take addend in account for these relocation,
counting it as 0, what is wrong.

This patch needed for FreeBSD pmbr (protective mbr).
It fixes one of 2 reasons of different output from bfd,
I'll post patch for second reason soon.


https://reviews.llvm.org/D27303

Files:
  ELF/Target.cpp
  test/ELF/Inputs/i386-pc16.s
  test/ELF/i386-pc16.s


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
+
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: 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);
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D27303.79921.patch
Type: text/x-patch
Size: 2089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161201/419ad492/attachment.bin>


More information about the llvm-commits mailing list