diff --git a/magic.s b/magic.s index cd119a43..0db822d4 100644 --- a/magic.s +++ b/magic.s @@ -1,5 +1,15 @@ +// Assemble: clang magic.s --target=arm64 -shared + +.data + +.global _on_fns +on_fns: + .xword _magic + +.text + .global _magic .balign 16 _magic: - mov X0, 42 + mov w0, 42 ret diff --git a/main.c b/main.c index 98fe78df..07c87026 100644 --- a/main.c +++ b/main.c @@ -2,15 +2,10 @@ #include #include -// struct magic_on_fns { -// int (*magic)(void); -// }; +struct magic_on_fns { + int (*magic)(void); +}; -// nasm -fmacho64 magic.s -o magic.dylib -// clang main.c && ./a.out -// xxd magic.dylib -// llvm-readelf magic.dylib -// llvm-objdump -D magic.dylib int main(void) { void *l = dlopen("./magic.dylib", RTLD_NOW); if (l == NULL) { @@ -18,19 +13,11 @@ int main(void) { return EXIT_FAILURE; } - int (*magic)(void) = dlsym(l, "magic"); - if (magic == NULL) { + void *on_fns = dlsym(l, "on_fns"); + if (on_fns == NULL) { printf("dlsym failed: %s\n", dlerror()); return EXIT_FAILURE; } - printf("magic: %d\n", magic()); - - // void *on_fns = dlsym(l, "on_fns"); - // if (on_fns == NULL) { - // printf("dlsym failed: %s\n", dlerror()); - // return EXIT_FAILURE; - // } - - // printf("magic: %d\n", ((struct magic_on_fns *)on_fns)->magic()); + printf("magic: %d\n", ((struct magic_on_fns *)on_fns)->magic()); }