1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

Reserve space for redirect table and install trampolines

The rt0_64 code reserves space for _rt0_redirect_table using the output
from the redirect tool's "count" command as a hint to the size of the
table. The table itself is located in the .goredirectstbl section which
the linker moves to a dedicated section in the final ELF image.

When the kernel boots, the _rt0_install_redirect_trampolines function
iterates the _rt0_redirect_table entries (populated as a post-link step)
and overwrite the original function code with a trampoline that
redirects control to the destination function.

The trampoline is implemented as a 14-byte instruction that exploits
rip-relative addressing to ensure that no registers are made dirty. The
actual trampoline code looks like this:

jmp [rip+0]                 ; 6-bytes
dq abs_address_to_jump_to   ; 8-bytes

The _rt0_install_redirect_trampolines function sets up the abs_address
to "dst" for each (src, dst) tuple and then copies the trampoline to
"src". After the trampoline is installed, any calls to "src" will be
transparently redirected to "dst". This hack (modifying code in the
.text section) is only possible because the code runs in supervisor mode
before memory protection is enabled.
This commit is contained in:
Achilleas Anagnostopoulos
2017-06-25 20:00:05 +01:00
parent 275664219e
commit d17f582c0b
2 changed files with 76 additions and 1 deletions

View File

@@ -38,6 +38,15 @@ SECTIONS {
*(COMMON)
*(.bss)
}
/* Go function redirection table. This table is used for hooking
* Go runtime function symbols so that calls to them are redirected to
* functions provided by the kernel.
*/
.goredirectstbl ALIGN(4K): AT(ADDR(.goredirectstbl) - PAGE_OFFSET)
{
*(.goredirectstbl)
}
_kernel_end = ALIGN(4K);
}