aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/rtdl/preinit/test.c
blob: 5b5d5e80145cfc4d8820d20d1e7d8abddd2e1ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <assert.h>

int mainDone = 0;

int isFooDone();

void preInit1() {
	// Use dprintf because stdout might not be initialized yet.
	dprintf(1, "pre-initialization function 1 called in main executable\n");

	assert(isFooDone() == 0);
	assert(mainDone == 0);
	mainDone++;
}

void preInit2() {
	dprintf(1, "pre-initialization function 2 called in main executable\n");

	assert(isFooDone() == 0);
	assert(mainDone == 1);
	mainDone++;
}

__attribute__((constructor))
void mainInit() {
	dprintf(1, "initialization function called in main executable\n");

	assert(isFooDone() == 1);
	assert(mainDone == 2);
	mainDone++;
}

// Manually register the pre-initialization functions.
__attribute__((used, section(".preinit_array")))
static void (*preinitFunctions[])(void) = {
	&preInit1,
	&preInit2,
};

int main() {
	assert(isFooDone() == 1);
	assert(mainDone == 3);
}