X-Git-Url: https://git.saurik.com/apple/objc4.git/blobdiff_plain/7c0e6487d7b67b6bf6c632300ee4b74e8950b051..7af964d1562d70f51a8e9aca24215ac3d83d0624:/runtime/objcrt.c diff --git a/runtime/objcrt.c b/runtime/objcrt.c new file mode 100644 index 0000000..af28301 --- /dev/null +++ b/runtime/objcrt.c @@ -0,0 +1,97 @@ +#define WIN32_LEAN_AND_MEAN +#include +#include +#include +#include "objcrt.h" + +// Boundary symbols for metadata sections + +#pragma section(".objc_module_info$A",long,read,write) +#pragma data_seg(".objc_module_info$A") +static uintptr_t __objc_modStart = 0; +#pragma section(".objc_module_info$C",long,read,write) +#pragma data_seg(".objc_module_info$C") +static uintptr_t __objc_modEnd = 0; + +#pragma section(".objc_protocol$A",long,read,write) +#pragma data_seg(".objc_protocol$A") +static uintptr_t __objc_protoStart = 0; +#pragma section(".objc_protocol$C",long,read,write) +#pragma data_seg(".objc_protocol$C") +static uintptr_t __objc_protoEnd = 0; + +#pragma section(".objc_image_info$A",long,read,write) +#pragma data_seg(".objc_image_info$A") +static uintptr_t __objc_iiStart = 0; +#pragma section(".objc_image_info$C",long,read,write) +#pragma data_seg(".objc_image_info$C") +static uintptr_t __objc_iiEnd = 0; + +#pragma section(".objc_message_refs$A",long,read,write) +#pragma data_seg(".objc_message_refs$A") +static uintptr_t __objc_selrefsStart = 0; +#pragma section(".objc_message_refs$C",long,read,write) +#pragma data_seg(".objc_message_refs$C") +static uintptr_t __objc_selrefsEnd = 0; + +#pragma section(".objc_class_refs$A",long,read,write) +#pragma data_seg(".objc_class_refs$A") +static uintptr_t __objc_clsrefsStart = 0; +#pragma section(".objc_class_refs$C",long,read,write) +#pragma data_seg(".objc_class_refs$C") +static uintptr_t __objc_clsrefsEnd = 0; + +#pragma data_seg() + +// Merge all metadata into .data +// fixme order these by usage? +#pragma comment(linker, "/MERGE:.objc_module_info=.data") +#pragma comment(linker, "/MERGE:.objc_protocol=.data") +#pragma comment(linker, "/MERGE:.objc_image_info=.data") +#pragma comment(linker, "/MERGE:.objc_message_refs=.data") +#pragma comment(linker, "/MERGE:.objc_class_refs=.data") + + +// Image initializers + +static void *__hinfo = NULL; // cookie from runtime +extern IMAGE_DOS_HEADER __ImageBase; // this image's header + +static int __objc_init(void) +{ + objc_sections sections = { + 5, + &__objc_modStart, &__objc_modEnd, + &__objc_protoStart, &__objc_protoEnd, + &__objc_iiStart, &__objc_iiEnd, + &__objc_selrefsStart, &__objc_selrefsEnd, + &__objc_clsrefsStart, &__objc_clsrefsEnd, + }; + __hinfo = _objc_init_image((HMODULE)&__ImageBase, §ions); + return 0; +} + +static void __objc_unload(void) +{ + _objc_unload_image((HMODULE)&__ImageBase, __hinfo); +} + +static int __objc_load(void) +{ + _objc_load_image((HMODULE)&__ImageBase, __hinfo); + return 0; +} + +// run _objc_init_image ASAP +#pragma section(".CRT$XIAA",long,read,write) +#pragma data_seg(".CRT$XIAA") +static void *__objc_init_fn = &__objc_init; + +// run _objc_load_image (+load methods) after init_seg(compiler) but before init_seg(library) or init_seg(user) +#pragma section(".CRT$XCI",long,read,write) +#pragma data_seg(".CRT$XCI") +static void *__objc_load_fn = &__objc_load; + +// _objc_unload_image is called by atexit(), not by an image terminator + +#pragma data_seg()