entry.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * ctors.c
  3. * Copyright 2019 Peter Jones <[email protected]>
  4. *
  5. */
  6. #include <efi.h>
  7. #include <efilib.h>
  8. typedef void (*funcp)(void);
  9. /*
  10. * Note that these aren't the using the GNU "CONSTRUCTOR" output section
  11. * command, so they don't start with a size. Because of p2align and the
  12. * end/END definitions, and the fact that they're mergeable, they can also
  13. * have NULLs which aren't guaranteed to be at the end.
  14. */
  15. extern funcp __init_array_start[], __init_array_end[];
  16. extern funcp __CTOR_LIST__[], __CTOR_END__[];
  17. extern funcp __fini_array_start[], __fini_array_end[];
  18. extern funcp __DTOR_LIST__[], __DTOR_END__[];
  19. static void ctors(void)
  20. {
  21. size_t __init_array_length = __init_array_end - __init_array_start;
  22. for (size_t i = 0; i < __init_array_length; i++) {
  23. funcp func = __init_array_start[i];
  24. if (func != NULL)
  25. func();
  26. }
  27. size_t __CTOR_length = __CTOR_END__ - __CTOR_LIST__;
  28. for (size_t i = 0; i < __CTOR_length; i++) {
  29. size_t current = __CTOR_length - i - 1;
  30. funcp func = __CTOR_LIST__[current];
  31. if (func != NULL)
  32. func();
  33. }
  34. }
  35. static void dtors(void)
  36. {
  37. size_t __DTOR_length = __DTOR_END__ - __DTOR_LIST__;
  38. for (size_t i = 0; i < __DTOR_length; i++) {
  39. funcp func = __DTOR_LIST__[i];
  40. if (func != NULL)
  41. func();
  42. }
  43. size_t __fini_array_length = __fini_array_end - __fini_array_start;
  44. for (size_t i = 0; i < __fini_array_length; i++) {
  45. size_t current = __fini_array_length - i - 1;
  46. funcp func = __fini_array_start[current];
  47. if (func != NULL)
  48. func();
  49. }
  50. }
  51. extern EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab);
  52. EFI_STATUS _entry(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
  53. {
  54. EFI_STATUS status;
  55. InitializeLib(image, systab);
  56. ctors();
  57. status = efi_main(image, systab);
  58. dtors();
  59. return status;
  60. }
  61. // vim:fenc=utf-8:tw=75:noet