aboutsummaryrefslogblamecommitdiff
path: root/03_hello_dynld/dynld.S
blob: 811fe2c292c17a4bcdbd1b12a6f4b7cbd5f0fea0 (plain) (tree)






























                                                                               
// Copyright (c) 2021 Johannes Stoelp

#if !defined(__linux__) || !defined(__x86_64__)
#    error "Only supported in linux(x86_64)!"
#endif

#include <asm/unistd.h>

.intel_syntax noprefix

.section .text, "ax", @progbits
.global dl_start
dl_start:
    // $rsp is guaranteed to be 16-byte aligned.

    // Clear $rbp as specified by the SysV AMD64 ABI.
    xor rbp, rbp

    // Load pointer to process context prepared by execve(2) syscall as
    // specified in the SysV AMD64 ABI.
    // Save pointer in $rdi which is the arg0 (int/ptr) register.
    lea rdi, [rsp]

    // Stack frames must be 16-byte aligned before control is transfered to the
    // callees entry point.
    call dl_entry

    // Call exit(1) syscall to indicate error, dl_entry should not return.
    mov rdi, 1
    mov rax, __NR_exit
    syscall