xref: /DragonStub/lib/entry.c (revision 9835e11ebe95ed8372ac2a6671de07cc970cdee0)
14f8b339fSPeter Jones /*
24f8b339fSPeter Jones  * ctors.c
34f8b339fSPeter Jones  * Copyright 2019 Peter Jones <pjones@redhat.com>
44f8b339fSPeter Jones  *
54f8b339fSPeter Jones  */
64f8b339fSPeter Jones 
74f8b339fSPeter Jones #include <efi.h>
84f8b339fSPeter Jones #include <efilib.h>
94f8b339fSPeter Jones 
10*7dac1844SCallum Farmer typedef void (*funcp)(void);
11*7dac1844SCallum Farmer 
124f8b339fSPeter Jones /*
134f8b339fSPeter Jones  * Note that these aren't the using the GNU "CONSTRUCTOR" output section
144f8b339fSPeter Jones  * command, so they don't start with a size.  Because of p2align and the
154f8b339fSPeter Jones  * end/END definitions, and the fact that they're mergeable, they can also
164f8b339fSPeter Jones  * have NULLs which aren't guaranteed to be at the end.
174f8b339fSPeter Jones  */
18*7dac1844SCallum Farmer extern funcp __init_array_start[], __init_array_end[];
19*7dac1844SCallum Farmer extern funcp __CTOR_LIST__[], __CTOR_END__[];
20*7dac1844SCallum Farmer extern funcp __fini_array_start[], __fini_array_end[];
21*7dac1844SCallum Farmer extern funcp __DTOR_LIST__[], __DTOR_END__[];
224f8b339fSPeter Jones 
ctors(void)234f8b339fSPeter Jones static void ctors(void)
244f8b339fSPeter Jones {
25*7dac1844SCallum Farmer 	size_t __init_array_length = __init_array_end - __init_array_start;
26*7dac1844SCallum Farmer 	for (size_t i = 0; i < __init_array_length; i++) {
27*7dac1844SCallum Farmer 		funcp func = __init_array_start[i];
28*7dac1844SCallum Farmer 		if (func != NULL)
294f8b339fSPeter Jones 			func();
304f8b339fSPeter Jones 	}
314f8b339fSPeter Jones 
32*7dac1844SCallum Farmer 	size_t __CTOR_length = __CTOR_END__ - __CTOR_LIST__;
33*7dac1844SCallum Farmer 	for (size_t i = 0; i < __CTOR_length; i++) {
34*7dac1844SCallum Farmer 		size_t current = __CTOR_length - i - 1;
35*7dac1844SCallum Farmer 		funcp func = __CTOR_LIST__[current];
36*7dac1844SCallum Farmer 		if (func != NULL)
374f8b339fSPeter Jones 			func();
384f8b339fSPeter Jones 	}
394f8b339fSPeter Jones }
404f8b339fSPeter Jones 
dtors(void)414f8b339fSPeter Jones static void dtors(void)
424f8b339fSPeter Jones {
43*7dac1844SCallum Farmer 	size_t __DTOR_length = __DTOR_END__ - __DTOR_LIST__;
44*7dac1844SCallum Farmer 	for (size_t i = 0; i < __DTOR_length; i++) {
45*7dac1844SCallum Farmer 		funcp func = __DTOR_LIST__[i];
46*7dac1844SCallum Farmer 		if (func != NULL)
474f8b339fSPeter Jones 			func();
484f8b339fSPeter Jones 	}
494f8b339fSPeter Jones 
50*7dac1844SCallum Farmer 	size_t __fini_array_length = __fini_array_end - __fini_array_start;
51*7dac1844SCallum Farmer 	for (size_t i = 0; i < __fini_array_length; i++) {
52*7dac1844SCallum Farmer 		size_t current = __fini_array_length - i - 1;
53*7dac1844SCallum Farmer 		funcp func = __fini_array_start[current];
54*7dac1844SCallum Farmer 		if (func != NULL)
554f8b339fSPeter Jones 			func();
564f8b339fSPeter Jones 	}
574f8b339fSPeter Jones }
584f8b339fSPeter Jones 
594f8b339fSPeter Jones extern EFI_STATUS efi_main(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab);
604f8b339fSPeter Jones 
_entry(EFI_HANDLE image,EFI_SYSTEM_TABLE * systab)614f8b339fSPeter Jones EFI_STATUS _entry(EFI_HANDLE image, EFI_SYSTEM_TABLE *systab)
624f8b339fSPeter Jones {
634f8b339fSPeter Jones 	EFI_STATUS status;
644f8b339fSPeter Jones 	InitializeLib(image, systab);
654f8b339fSPeter Jones 
664f8b339fSPeter Jones 	ctors();
674f8b339fSPeter Jones 	status = efi_main(image, systab);
684f8b339fSPeter Jones 	dtors();
694f8b339fSPeter Jones 
704f8b339fSPeter Jones 	return status;
714f8b339fSPeter Jones }
724f8b339fSPeter Jones 
734f8b339fSPeter Jones // vim:fenc=utf-8:tw=75:noet
74