diff --git a/subject/export.h b/subject/export.h new file mode 100644 index 0000000..fa61a93 --- /dev/null +++ b/subject/export.h @@ -0,0 +1,32 @@ +#include + +#define __stringify_1(x) #x +#define __stringify(x) __stringify_1(x) + +#define __ADDRESSABLE(sym) \ + static void * __addressable_##sym __attribute__((used)) = (void *)&(sym) + +#define __GENDWARFKSYMS_EXPORT(sym) /* nothing */ + +#define ASM_NL "\n" + +#define __EXPORT_SYMBOL_REF(sym) \ + ".balign 8" ASM_NL \ + ".quad " __stringify(sym) + +#define ___EXPORT_SYMBOL(sym, license, ns...) \ + ".section \".export_symbol\",\"a\"" ASM_NL \ + "__export_symbol_" __stringify(sym) ":" ASM_NL \ + ".asciz \"" license "\"" ASM_NL \ + ".ascii \"" ns "\\0\"" ASM_NL \ + __EXPORT_SYMBOL_REF(sym) ASM_NL \ + ".previous" + +#define __EXPORT_SYMBOL(sym, license, ns) \ + extern typeof(sym) sym; \ + __ADDRESSABLE(sym); \ + __GENDWARFKSYMS_EXPORT(sym); \ + asm(___EXPORT_SYMBOL(sym, license, ns)) + +#define EXPORT_SYMBOL(sym) __EXPORT_SYMBOL(sym, "license", "namespace") + diff --git a/subject/main.c b/subject/main.c index 92328a4..089665c 100644 --- a/subject/main.c +++ b/subject/main.c @@ -1,6 +1,7 @@ #include #include "task_struct.h" +#include "export.h" int second_pid(); const char* second_comm(); @@ -15,6 +16,8 @@ struct task_struct global = { .pid = 42, .comm = "main_global", .arrfun = { { .a = 17, .b = 18, .c = 19, .d = 20.0 } }, .tasks = LIST_HEAD_SELF(global.tasks) }; +EXPORT_SYMBOL(global); + static void print_layout() { // TODO -> Make builtin __spslr_initial_offsetof(type, field) that is not patched printf("Current task_struct layout:\n");