[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 main
extern exit extern exit
extern __init_libj6 extern __init_libj6
@@ -12,17 +14,21 @@ _libc_crt0_start:
push 0 push 0
mov rbp, rsp mov rbp, rsp
call __init_libj6 wrt ..got lookup_GOT __init_libj6
call rax
mov rbx, rax mov rbx, rax
call __init_libc wrt ..got lookup_GOT __init_libc
call rax
mov rdi, 0 mov rdi, 0
mov rsi, rsp mov rsi, rsp
mov rdx, 0 ; TODO: actually parse stack for argc, argv, envp mov rdx, 0 ; TODO: actually parse stack for argc, argv, envp
mov rcx, rbx mov rcx, rbx
call main wrt ..got lookup_GOT main
call rax
mov rdi, rax mov rdi, rax
call exit wrt ..got lookup_GOT exit
call rax
.end: .end:

View File

@@ -0,0 +1,7 @@
extern _GLOBAL_OFFSET_TABLE_
; Put the address of the given symbol in rax
%macro lookup_GOT 1
lea rax, [rel _GLOBAL_OFFSET_TABLE_]
mov rax, [rax + %1 wrt ..got]
%endmacro