Update.
[glibc.git] / elf / nodelete.c
blob0a87e02480f51e3575707a434c545746336678eb
1 #include <dlfcn.h>
2 #include <setjmp.h>
3 #include <signal.h>
4 #include <stdio.h>
7 static sigjmp_buf jmpbuf;
10 static void
11 handler (int sig)
13 siglongjmp (jmpbuf, 1);
17 #define TEST_FUNCTION do_test ()
18 static int
19 do_test (void)
21 /* We are testing the two possibilities to mark an object as not deletable:
22 - marked on the linker commandline with `-z nodelete'
23 - with the RTLD_NODELETE flag at dlopen()-time.
25 The test we are performing should be safe. We are loading the objects,
26 get the address of variables in the respective object, unload the object
27 and then try to read the variable. If the object is unloaded this
28 should lead to an segmentation fault. */
29 int result = 0;
30 void *p;
31 struct sigaction sa;
33 sa.sa_handler = handler;
34 sigfillset (&sa.sa_mask);
35 sa.sa_flags = SA_RESTART;
36 sa.sa_restorer = NULL;
38 if (sigaction (SIGSEGV, &sa, NULL) == -1)
39 printf ("cannot install signal handler: %m\n");
41 p = dlopen ("nodelmod1.so", RTLD_LAZY);
42 if (p == NULL)
44 printf ("failed to load \"nodelmod1.so\": %s\n", dlerror ());
45 result = 1;
47 else
49 int *varp;
51 puts ("succeeded loading \"nodelmod1.so\"");
53 varp = dlsym (p, "var1");
54 if (varp == NULL)
56 puts ("failed to get address of \"var1\" in \"nodelmod1.so\"");
57 result = 1;
59 else
61 *varp = 20000720;
63 /* Now close the object. */
64 if (dlclose (p) != 0)
66 puts ("failed to close \"nodelmod1.so\"");
67 result = 1;
69 else if (! sigsetjmp (jmpbuf, 1))
71 /* Access the variable again. */
72 if (*varp != 20000720)
74 puts ("\"var1\" value not correct");
75 result = 1;
77 else
78 puts ("-z nodelete test succeeded");
80 else
82 /* We caught an segmentation fault. */
83 puts ("\"nodelmod1.so\" got deleted");
84 result = 1;
89 p = dlopen ("nodelmod2.so", RTLD_LAZY | RTLD_NODELETE);
90 if (p == NULL)
92 printf ("failed to load \"nodelmod2.so\": %s\n", dlerror ());
93 result = 1;
95 else
97 int *varp;
99 puts ("succeeded loading \"nodelmod2.so\"");
101 varp = dlsym (p, "var2");
102 if (varp == NULL)
104 puts ("failed to get address of \"var2\" in \"nodelmod2.so\"");
105 result = 1;
107 else
109 *varp = 42;
111 /* Now close the object. */
112 if (dlclose (p) != 0)
114 puts ("failed to close \"nodelmod2.so\"");
115 result = 1;
117 else if (! sigsetjmp (jmpbuf, 1))
119 /* Access the variable again. */
120 if (*varp != 42)
122 puts ("\"var2\" value not correct");
123 result = 1;
125 else
126 puts ("RTLD_NODELETE test succeeded");
128 else
130 /* We caught an segmentation fault. */
131 puts ("\"nodelmod2.so\" got deleted");
132 result = 1;
137 p = dlopen ("nodelmod3.so", RTLD_LAZY);
138 if (p == NULL)
140 printf ("failed to load \"nodelmod3.so\": %s\n", dlerror ());
141 result = 1;
143 else
145 int *(*fctp) (void);
147 puts ("succeeded loading \"nodelmod3.so\"");
149 fctp = dlsym (p, "addr");
150 if (fctp == NULL)
152 puts ("failed to get address of \"addr\" in \"nodelmod3.so\"");
153 result = 1;
155 else
157 int *varp = fctp ();
159 *varp = -1;
161 /* Now close the object. */
162 if (dlclose (p) != 0)
164 puts ("failed to close \"nodelmod3.so\"");
165 result = 1;
167 else if (! sigsetjmp (jmpbuf, 1))
169 /* Access the variable again. */
170 if (*varp != -1)
172 puts ("\"var_in_mod4\" value not correct");
173 result = 1;
175 else
176 puts ("-z nodelete in dependency succeeded");
178 else
180 /* We caught an segmentation fault. */
181 puts ("\"nodelmod4.so\" got deleted");
182 result = 1;
187 return result;
190 #include "../test-skeleton.c"