From 2c58afdb3ae6f900583cf7264cba6ab8a797e3e2 Mon Sep 17 00:00:00 2001 From: linted Date: Sat, 23 Jul 2022 16:25:41 -0400 Subject: Added support for creation of Static Position-Independent Executables (PIE) on i386, x86_64, and arm. This patch adds the generation of rcrt1.o which is used by gcc when compiling with the --static-pie flag. rcrt1.o differs from crt1.o and Scrt1.o in that it the executable has a dynamic section but no relocations have been performed prior to _start being called. crt1.o assumes there to be no dynamic relocations, and Scrt1.o has all relocations performed prior to execution by lsdo. The new reloc_static_pie function handles parsing the dynamic section, and performing the relocations in a architecture agnostic method. It also sets _dl_load_base which is used when initalizing TLS to ensure loading from the proper location. This allows for easier porting of static-pie support to additional architectures as only modifications to crt1.S to find the load address are required. Signed-off-by: linted --- libc/sysdeps/linux/x86_64/crt1.S | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'libc/sysdeps/linux/x86_64') diff --git a/libc/sysdeps/linux/x86_64/crt1.S b/libc/sysdeps/linux/x86_64/crt1.S index 87777dd5d..701cbf2f6 100644 --- a/libc/sysdeps/linux/x86_64/crt1.S +++ b/libc/sysdeps/linux/x86_64/crt1.S @@ -80,6 +80,20 @@ _start: the outermost frame obviously. */ xorl %ebp, %ebp +#ifdef L_rcrt1 + pushq %rdi /* save rdi (but should be 0...) */ + pushq %rdx /* store rdx (rtld_fini) */ + xorq %rcx, %rcx /* ensure rcx is 0 */ + addq _start@GOTPCREL(%rip), %rcx /* get offset of _start from beginning of file */ + movq _start@GOTPCREL(%rip), %rax /* get run time address of _start */ + subq %rcx, %rax /* calculate run time load offset */ + movq %rax, %rdi /* load offset -> param 1 */ + call reloc_static_pie /* relocate dynamic addrs */ + xorq %rax, %rax /* cleanup */ + popq %rdx + popq %rdi +#endif + /* Extract the arguments as encoded on the stack and set up the arguments for __libc_start_main (int (*main) (int, char **, char **), int argc, char *argv, @@ -107,7 +121,7 @@ _start: which grow downwards). */ pushq %rsp -#if defined(L_Scrt1) +#if defined(L_Scrt1) || defined(L_rcrt1) /* Give address for main() */ movq main@GOTPCREL(%rip), %rdi -- cgit v1.2.3