1 #include <unistd.h>
2
3 /* Give init non-default priority so that it runs before init_array. */
4 static void init (void) __attribute__ ((constructor (1000)));
5
6 static void
init(void)7 init (void)
8 {
9 write (STDOUT_FILENO, "init\n", 5);
10 }
11
12 /* Give fini the same priority as init. */
13 static void fini (void) __attribute__ ((destructor (1000)));
14
15 static void
fini(void)16 fini (void)
17 {
18 write (STDOUT_FILENO, "fini\n", 5);
19 }
20
21 static void
preinit_0(void)22 preinit_0 (void)
23 {
24 write (STDOUT_FILENO, "preinit array 0\n", 16);
25 }
26
27 static void
preinit_1(void)28 preinit_1 (void)
29 {
30 write (STDOUT_FILENO, "preinit array 1\n", 16);
31 }
32
33 static void
preinit_2(void)34 preinit_2 (void)
35 {
36 write (STDOUT_FILENO, "preinit array 2\n", 16);
37 }
38
39 void (*const preinit_array []) (void)
40 __attribute__ ((section (".preinit_array"), aligned (sizeof (void *)))) =
41 {
42 &preinit_0,
43 &preinit_1,
44 &preinit_2
45 };
46
47 static void
init_0(void)48 init_0 (void)
49 {
50 write (STDOUT_FILENO, "init array 0\n", 13);
51 }
52
53 static void
init_1(void)54 init_1 (void)
55 {
56 write (STDOUT_FILENO, "init array 1\n", 13);
57 }
58
59 static void
init_2(void)60 init_2 (void)
61 {
62 write (STDOUT_FILENO, "init array 2\n", 13);
63 }
64
65 void (*init_array []) (void)
66 __attribute__ ((section (".init_array"), aligned (sizeof (void *)))) =
67 {
68 &init_0,
69 &init_1,
70 &init_2
71 };
72
73 static void
fini_0(void)74 fini_0 (void)
75 {
76 write (STDOUT_FILENO, "fini array 0\n", 13);
77 }
78
79 static void
fini_1(void)80 fini_1 (void)
81 {
82 write (STDOUT_FILENO, "fini array 1\n", 13);
83 }
84
85 static void
fini_2(void)86 fini_2 (void)
87 {
88 write (STDOUT_FILENO, "fini array 2\n", 13);
89 }
90
91 void (*fini_array []) (void)
92 __attribute__ ((section (".fini_array"), aligned (sizeof (void *)))) =
93 {
94 &fini_0,
95 &fini_1,
96 &fini_2
97 };
98
99 int
main(void)100 main (void)
101 {
102 return 0;
103 }
104