diff options
author | linted <linted@users.noreply.github.com> | 2022-07-23 16:25:41 -0400 |
---|---|---|
committer | Waldemar Brodkorb <wbx@openadk.org> | 2022-07-26 09:56:03 +0200 |
commit | 2c58afdb3ae6f900583cf7264cba6ab8a797e3e2 (patch) | |
tree | abb641e31fcab71a4e78905fd0124ca8b9a25eae /libc/sysdeps/linux/i386/crt1.S | |
parent | 01961b12bc71d6eb4d9bda3632d73bb6764b8e85 (diff) |
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 <linted@users.noreply.github.com>
Diffstat (limited to 'libc/sysdeps/linux/i386/crt1.S')
-rw-r--r-- | libc/sysdeps/linux/i386/crt1.S | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/libc/sysdeps/linux/i386/crt1.S b/libc/sysdeps/linux/i386/crt1.S index 35a6552e8..decc68967 100644 --- a/libc/sysdeps/linux/i386/crt1.S +++ b/libc/sysdeps/linux/i386/crt1.S @@ -67,6 +67,9 @@ #endif .type main,%function .type __uClibc_main,%function +#ifdef L_rcrt1 +.type reloc_static_pie,%function +#endif _start: /* Clear the frame pointer. The ABI suggests this be done, to mark the outermost frame obviously. */ @@ -100,6 +103,23 @@ _start: pop %ebx addl $_GLOBAL_OFFSET_TABLE_+[.-.L0],%ebx +#ifdef L_rcrt1 + /* We cannot rely on _DYNAMIC being usable here due to RELRO. + Instead we calculate the load address based off a symbol + that we know will exist, _start. */ + pushl %ecx /* Save ecx so it won't get clobbered */ + pushl %ebx /* Save ebx so it won't get clobbered */ + xorl %ecx, %ecx /* Clear ecx */ + addl _start@GOT(%ebx), %ecx /* Get the offset of _start */ + movl _start@GOT(%ebx), %eax /* Get the run time address of _start */ + subl %ecx, %eax /* Subtract to find the load address */ + pushl %eax /* Pass the load address */ + call reloc_static_pie@PLT + popl %eax /* Clean up from function call */ + popl %ebx /* Restore the GOT address */ + popl %ecx /* restore ecx */ +#endif + /* Push address of our own entry points to .fini and .init. */ pushl _fini@GOT(%ebx) pushl _init@GOT(%ebx) |