Skip to content

Commit

Permalink
Add on_fns symbol to magic.s
Browse files Browse the repository at this point in the history
  • Loading branch information
MyNameIsTrez committed Sep 13, 2024
1 parent 88c7394 commit 911d3f5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
12 changes: 11 additions & 1 deletion magic.s
Original file line number Diff line number Diff line change
@@ -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
25 changes: 6 additions & 19 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,22 @@
#include <stdio.h>
#include <stdlib.h>

// 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) {
printf("dlopen failed: %s\n", dlerror());
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());
}

0 comments on commit 911d3f5

Please sign in to comment.