From e7d244930c7d4d05c673daed57bcc4d3d918d058 Mon Sep 17 00:00:00 2001
From: John Ogness <john.ogness@linutronix.de>
Date: Mon, 22 Jan 2018 10:56:18 +0000
Subject: embedded-devel: add slides about PIE binaries

PIE binaries are now the default, so let's talk about them.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 .../embedded-devel/pres_embedded-devel_en.tex      | 77 +++++++++++++++++++++-
 1 file changed, 76 insertions(+), 1 deletion(-)

(limited to 'application-devel/embedded-devel/pres_embedded-devel_en.tex')

diff --git a/application-devel/embedded-devel/pres_embedded-devel_en.tex b/application-devel/embedded-devel/pres_embedded-devel_en.tex
index 95333a9..a405249 100644
--- a/application-devel/embedded-devel/pres_embedded-devel_en.tex
+++ b/application-devel/embedded-devel/pres_embedded-devel_en.tex
@@ -301,7 +301,7 @@ the linker searches for a file libhello.a
 
 \begin{frame}
 \frametitle{\textbf{P}osition \textbf{I}ndependent \textbf{C}ode}
-The -fPIC compiler option tells the compiler to only generate instructions that are position independen (i.e. relative). This means the generated instrutions can be run from any virtual address.
+The -fPIC compiler option tells the compiler to only generate instructions that are position independent (i.e. relative). This means the generated instrutions can be run from any virtual address.
 \end{frame}
 
 \begin{frame}[fragile]
@@ -517,6 +517,81 @@ Awareness and understanding of the dynamic loader leads to:
 \end{enumerate}
 \end{frame}
 
+\subsection{Position Independent Executables}
+
+\begin{frame}[fragile]
+\frametitle{\textbf{P}osition \textbf{I}ndependent \textbf{E}xecutables}
+The -fPIE compiler option is like the -fPIC compiler option (only generate instructions that are position independent), but the compiled objects should be used for executables instead of dynamic libraries.
+\begin{verbatim}
+gcc -c -fPIE hello.c
+\end{verbatim}
+The -pie linker option tells the linker to create a position independent executable. Objects must be compiled with -fPIE for predictable results.
+\begin{verbatim}
+gcc -pie -ohello hello.o
+\end{verbatim}
+The -no-pie linker option tells the linker \textbf{not} to create a position indepdendent executable.
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\textbf{A}ddress \textbf{S}pace \textbf{L}ayout \textbf{R}andomization}
+With PIE, the executable is loaded to a randomized address on each run.
+\begin{verbatim}
+$ LD_TRACE_PRELINKING=1 ./hello | grep '=>'
+        ./hello => ./hello (0x000055f721cde000, 0x000055f721cde000)
+        [...]
+$ LD_TRACE_PRELINKING=1 ./hello | grep '=>'
+        ./hello => ./hello (0x00005624239d2000, 0x00005624239d2000)
+        [...]
+\end{verbatim}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\textbf{A}ddress \textbf{S}pace \textbf{L}ayout \textbf{R}andomization}
+ASLR can be disabled.
+\begin{itemize}
+\item \textbf{setarch -R}: program environment (local)
+\item \textbf{norandmaps}: boot argument (global)
+\item \textbf{kernel.randomize\_va\_space=0}: sysctl data (global)
+\end{itemize}
+\end{frame}
+
+\begin{frame}[fragile]
+\frametitle{\textbf{A}ddress \textbf{S}pace \textbf{L}ayout \textbf{R}andomization}
+disable ASLR for a single program
+\begin{verbatim}
+$ setarch `uname -m` -R ./hello
+Hello, world!
+\end{verbatim}
+verify ASLR is disabled
+\begin{verbatim}
+$ setarch `uname -m` -R env LD_TRACE_PRELINKING=1 ./hello | grep '=>'
+        ./hello => ./hello (0x0000555555554000, 0x0000555555554000)
+        [...]
+$ setarch `uname -m` -R env LD_TRACE_PRELINKING=1 ./hello | grep '=>'
+        ./hello => ./hello (0x0000555555554000, 0x0000555555554000)
+        [...]
+\end{verbatim}
+\end{frame}
+
+\begin{frame}[containsverbatim]
+\frametitle{Identifying Source Code Lines with addr2line}
+disassemble program to see \textbf{relative} addresses
+\begin{verbatim}
+$ objdump -D hello | less
+[...]
+00000000000006b0 <main>:
+  6b0:   55                 push   %rbp
+  6b1:   48 89 e5           mov    %rsp,%rbp
+[...]
+\end{verbatim}
+
+identify source line number for a \textbf{relative} address
+\begin{verbatim}
+$ addr2line -e hello 6b0
+/home/devel/work/hello.c:4
+\end{verbatim}
+\end{frame}
+
 \subsection{Toolchains (cont.)}
 
 \begin{frame}
-- 
cgit v1.2.3

