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