entry.c 1.6 KB

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