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, "DSO init\n", 9);
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, "DSO fini\n", 9);
19 }
20
21 static void
init_0(void)22 init_0 (void)
23 {
24 write (STDOUT_FILENO, "DSO init array 0\n", 17);
25 }
26
27 static void
init_1(void)28 init_1 (void)
29 {
30 write (STDOUT_FILENO, "DSO init array 1\n", 17);
31 }
32
33 static void
init_2(void)34 init_2 (void)
35 {
36 write (STDOUT_FILENO, "DSO init array 2\n", 17);
37 }
38
39 void (*init_array []) (void)
40 __attribute__ ((section (".init_array"), aligned (sizeof (void *)))) =
41 {
42 &init_0,
43 &init_1,
44 &init_2
45 };
46
47 static void
fini_0(void)48 fini_0 (void)
49 {
50 write (STDOUT_FILENO, "DSO fini array 0\n", 17);
51 }
52
53 static void
fini_1(void)54 fini_1 (void)
55 {
56 write (STDOUT_FILENO, "DSO fini array 1\n", 17);
57 }
58
59 static void
fini_2(void)60 fini_2 (void)
61 {
62 write (STDOUT_FILENO, "DSO fini array 2\n", 17);
63 }
64
65 void (*fini_array []) (void)
66 __attribute__ ((section (".fini_array"), aligned (sizeof (void *)))) =
67 {
68 &fini_0,
69 &fini_1,
70 &fini_2
71 };
72