From 97433fc7d1be34204251c2aed29b56aea4e85d5a Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Thu, 31 Aug 2023 19:42:14 -0700 Subject: [PATCH] [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. --- src/libraries/libc/arch/amd64/crt/crt0.s | 14 ++++++++++---- src/libraries/util/include/util/got.inc | 7 +++++++ 2 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 src/libraries/util/include/util/got.inc diff --git a/src/libraries/libc/arch/amd64/crt/crt0.s b/src/libraries/libc/arch/amd64/crt/crt0.s index 079be06..076da03 100644 --- a/src/libraries/libc/arch/amd64/crt/crt0.s +++ b/src/libraries/libc/arch/amd64/crt/crt0.s @@ -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: diff --git a/src/libraries/util/include/util/got.inc b/src/libraries/util/include/util/got.inc new file mode 100644 index 0000000..e877634 --- /dev/null +++ b/src/libraries/util/include/util/got.inc @@ -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 \ No newline at end of file