From 7526a5f7b68b49e362a0203b656ef2b24b15aa2e Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Tue, 20 Oct 2015 09:17:48 -0700
Subject: [PATCH 1/3] Add R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX

X86 instruction encoding supports converting some instructions on memory
operand with GOTPCREL relocation against symbol, foo, into a different
form on immediate operand if foo is defined locally.  Those instructions
are:

call *foo@GOTPCREL(%rip)         =>   nop call foo or call foo nop
jmp *foo@GOTPCREL(%rip)          =>   jmp foo nop
mov foo@GOTPCREL(%rip), %reg     =>   lea foo(%rip), %reg

When PIC is false,

test %reg, foo@GOTPCREL(%rip)    =>   test $foo, %reg
binop foo@GOTPCREL(%rip), %reg   =>   binop $foo, %reg

where binop is one of adc, add, and, cmp, or, sbb, sub, xor instructions.

I am proposing to add 2 new relocations, R_X86_64_GOTPCRELX and
R_X86_64_REX_GOTPCRELX, to x86-64 psABI.  Instead of generating
R_X86_64_GOTPCREL relocation agasint foo for foo@GOTPCREL(%rip),
we generate R_X86_64_GOTPCRELX or R_X86_64_REX_GOTPCRELX if there
is a REX prefix.  Linker can treat them the same as R_X86_64_GOTPCREL
or it can perform the transformations listed above.

	* object-files.tex (Relocation Types): Add R_X86_64_GOTPCRELX
	and R_X86_64_REX_GOTPCRELX.
	Generate R_X86_64_GOTPCRELX/R_X86_64_REX_GOTPCRELX instead of
	R_X86_64_GOTPCREL for indirect call/jmp, mov, test and binop.
---
 object-files.tex | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/object-files.tex b/object-files.tex
index 423fbc3..826783b 100644
--- a/object-files.tex
+++ b/object-files.tex
@@ -438,7 +438,7 @@ or \texttt{Elf32_Rel} relocation entries.
 \begin{table}[H]
 \Hrule
   \caption{Relocation Types}
-  \small
+  \myfontsize
   \label{tab-relocations}
   \begin{center}
     \begin{tabular}[t]{l|r|l|l}
@@ -481,6 +481,8 @@ or \texttt{Elf32_Rel} relocation entries.
       \texttt{R_X86_64_TLSDESC} & 36 & \textit{word64}$\times 2$ & \\
       \texttt{R_X86_64_IRELATIVE} & 37 & \textit{wordclass} & \texttt{indirect (B + A)}\\
       \texttt{R_X86_64_RELATIVE64} $^{\dagger\dagger}$ & 38 & \textit{word64} & \texttt{B + A} \\
+      \texttt{R_X86_64_GOTPCRELX} & 41 & \textit{word32} & \texttt{G + GOT + A - P} \\
+      \texttt{R_X86_64_REX_GOTPCRELX} & 42 & \textit{word32} & \texttt{G + GOT + A - P} \\
 %      \texttt{R_X86_64_GOT64} & 16 & \textit{word64} & \texttt{G + A} \\
 %      \texttt{R_X86_64_PLT64} & 17 & \textit{word64} & \texttt{L + A - P} \\
      \cline{1-4}
@@ -517,6 +519,26 @@ in the GOT where the symbol's address is given and the location where the
 relocation is applied.
 \end{sloppypar}
 
+For \code{name@GOTPCREL} in:
+
+\begin{footnotesize}
+\begin{verbatim}
+        call       *name@GOTPCREL(%rip)
+        jmp        *name@GOTPCREL(%rip)
+        mov        name@GOTPCREL(%rip), %reg
+        test       %reg, name@GOTPCREL(%rip)
+        binop      name@GOTPCREL(%rip), %reg
+\end{verbatim}
+\end{footnotesize}
+
+\noindent
+where \code{binop} is one of \code{adc}, \code{add}, \code{and},
+\code{cmp}, \code{or}, \code{sbb}, \code{sub}, \code{xor}
+instructions, the \texttt{R_X86_64_GOTPCRELX} relocation,
+\texttt{R_X86_64_REX_GOTPCRELX} if the  \code{REX} prefix presents,
+should be generated, instead of the \texttt{R_X86_64_GOTPCREL}
+relocation.
+
 \begin{sloppypar}
 The \texttt{R_X86_64_32} and \texttt{R_X86_64_32S} relocations truncate
 the computed value to 32-bits.  The linker must verify that the
-- 
2.4.3

