[libc] Properly call init functions and main through GOT

In the CRT startup code, when linked in a PIC executable, jumps to
`__init_libj6`, `__init_libc`, `main`, and `exit` were not linked
correctly. They needed a bit more support for looking up the GOT, and
getting the symbol address out of it.

Now libutil has a `got.inc` file for inclusion in asm code that needs to
reference symbols from the GOT.
This commit is contained in:
Justin C. Miller
2023-08-31 19:42:14 -07:00
parent fc16ed54b3
commit 97433fc7d1
2 changed files with 17 additions and 4 deletions

View File

@@ -1,3 +1,5 @@
%include "util/got.inc"
extern main
extern exit
extern __init_libj6
@@ -12,17 +14,21 @@ _libc_crt0_start:
push 0
mov rbp, rsp
call __init_libj6 wrt ..got
lookup_GOT __init_libj6
call rax
mov rbx, rax
call __init_libc wrt ..got
lookup_GOT __init_libc
call rax
mov rdi, 0
mov rsi, rsp
mov rdx, 0 ; TODO: actually parse stack for argc, argv, envp
mov rcx, rbx
call main wrt ..got
lookup_GOT main
call rax
mov rdi, rax
call exit wrt ..got
lookup_GOT exit
call rax
.end: