Skip to content

Instantly share code, notes, and snippets.

@namazso
Created March 26, 2024 10:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namazso/829380a4a1a55913646acc43c1e8cfb6 to your computer and use it in GitHub Desktop.
Save namazso/829380a4a1a55913646acc43c1e8cfb6 to your computer and use it in GitHub Desktop.
Universal function proxy
.section .text, "x", discard, proxy_fn
.balign 16
.globl proxy_fn
.def proxy_fn; .scl 2; .type 32; .endef
proxy_fn:
.seh_proc proxy_fn
sub $0x68, %rsp
.seh_stackalloc 0x68
.seh_endprologue
mov %rcx, 0x70(%rsp)
mov %rdx, 0x78(%rsp)
mov %r8, 0x80(%rsp)
mov %r9, 0x88(%rsp)
movups %xmm0, 0x50(%rsp)
movups %xmm1, 0x40(%rsp)
movups %xmm2, 0x30(%rsp)
movups %xmm3, 0x20(%rsp)
call my_fn
mov 0x70(%rsp), %rcx
mov 0x78(%rsp), %rdx
mov 0x80(%rsp), %r8
mov 0x88(%rsp), %r9
movups 0x50(%rsp), %xmm0
movups 0x40(%rsp), %xmm1
movups 0x30(%rsp), %xmm2
movups 0x20(%rsp), %xmm3
add $0x68, %rsp
jmp *%rax
.seh_endproc
cmake_minimum_required(VERSION 3.28)
project(untitled C ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreaded "")
set(CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL "")
set(CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug "")
set(CMAKE_ASM_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL "")
add_executable(untitled main.c blah.S)
#include <stdio.h>
void original_fn(double a, float b, size_t c, int d) {
printf("hello from original: %lf, %f, %zu, %d\n", a, b, c, d);
}
void* my_fn() {
printf("hello from my_fn! %f %f %f\n", 1.0, 2.0, 3.0);
return (void*)&original_fn;
}
void proxy_fn(double a, float b, size_t c, int d);
int main() {
proxy_fn(11.0, 12.f, 13, 14);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment