objcrt.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <stdlib.h>
  5. #include "objcrt.h"
  6. // Boundary symbols for metadata sections
  7. #pragma section(".objc_module_info$A",long,read,write)
  8. #pragma data_seg(".objc_module_info$A")
  9. static uintptr_t __objc_modStart = 0;
  10. #pragma section(".objc_module_info$C",long,read,write)
  11. #pragma data_seg(".objc_module_info$C")
  12. static uintptr_t __objc_modEnd = 0;
  13. #pragma section(".objc_protocol$A",long,read,write)
  14. #pragma data_seg(".objc_protocol$A")
  15. static uintptr_t __objc_protoStart = 0;
  16. #pragma section(".objc_protocol$C",long,read,write)
  17. #pragma data_seg(".objc_protocol$C")
  18. static uintptr_t __objc_protoEnd = 0;
  19. #pragma section(".objc_image_info$A",long,read,write)
  20. #pragma data_seg(".objc_image_info$A")
  21. static uintptr_t __objc_iiStart = 0;
  22. #pragma section(".objc_image_info$C",long,read,write)
  23. #pragma data_seg(".objc_image_info$C")
  24. static uintptr_t __objc_iiEnd = 0;
  25. #pragma section(".objc_message_refs$A",long,read,write)
  26. #pragma data_seg(".objc_message_refs$A")
  27. static uintptr_t __objc_selrefsStart = 0;
  28. #pragma section(".objc_message_refs$C",long,read,write)
  29. #pragma data_seg(".objc_message_refs$C")
  30. static uintptr_t __objc_selrefsEnd = 0;
  31. #pragma section(".objc_class_refs$A",long,read,write)
  32. #pragma data_seg(".objc_class_refs$A")
  33. static uintptr_t __objc_clsrefsStart = 0;
  34. #pragma section(".objc_class_refs$C",long,read,write)
  35. #pragma data_seg(".objc_class_refs$C")
  36. static uintptr_t __objc_clsrefsEnd = 0;
  37. #pragma data_seg()
  38. // Merge all metadata into .data
  39. // fixme order these by usage?
  40. #pragma comment(linker, "/MERGE:.objc_module_info=.data")
  41. #pragma comment(linker, "/MERGE:.objc_protocol=.data")
  42. #pragma comment(linker, "/MERGE:.objc_image_info=.data")
  43. #pragma comment(linker, "/MERGE:.objc_message_refs=.data")
  44. #pragma comment(linker, "/MERGE:.objc_class_refs=.data")
  45. // Image initializers
  46. static void *__hinfo = NULL; // cookie from runtime
  47. extern IMAGE_DOS_HEADER __ImageBase; // this image's header
  48. static int __objc_init(void)
  49. {
  50. objc_sections sections = {
  51. 5,
  52. &__objc_modStart, &__objc_modEnd,
  53. &__objc_protoStart, &__objc_protoEnd,
  54. &__objc_iiStart, &__objc_iiEnd,
  55. &__objc_selrefsStart, &__objc_selrefsEnd,
  56. &__objc_clsrefsStart, &__objc_clsrefsEnd,
  57. };
  58. __hinfo = _objc_init_image((HMODULE)&__ImageBase, &sections);
  59. return 0;
  60. }
  61. static void __objc_unload(void)
  62. {
  63. _objc_unload_image((HMODULE)&__ImageBase, __hinfo);
  64. }
  65. static int __objc_load(void)
  66. {
  67. _objc_load_image((HMODULE)&__ImageBase, __hinfo);
  68. return 0;
  69. }
  70. // run _objc_init_image ASAP
  71. #pragma section(".CRT$XIAA",long,read,write)
  72. #pragma data_seg(".CRT$XIAA")
  73. static void *__objc_init_fn = &__objc_init;
  74. // run _objc_load_image (+load methods) after all other initializers;
  75. // otherwise constant NSStrings are not initialized yet
  76. #pragma section(".CRT$XCUO",long,read,write)
  77. #pragma data_seg(".CRT$XCUO")
  78. static void *__objc_load_fn = &__objc_load;
  79. // _objc_unload_image is called by atexit(), not by an image terminator
  80. #pragma data_seg()