1 /* Global object symbol access tests with a static executable (BZ #15022).
2 Copyright (C) 2013-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
24 #define MAGIC1 0x5500ffaa
25 #define MAGIC2 0xaaff0055
26 #define MAGIC3 0xff55aa00
28 /* Check the ability to access the global symbol object and then
29 global-scope symbol access consistency via different mappings
30 requested from a static executable. */
34 unsigned int (*initial_getfoo
) (void);
35 void (*initial_setfoo
) (unsigned int);
36 unsigned int (*global_getfoo
) (void);
37 void (*global_setfoo
) (unsigned int);
38 unsigned int (*local_getfoo
) (void);
39 void (*local_setfoo
) (unsigned int);
40 unsigned int *initial_foop
;
41 unsigned int *global_foop
;
42 unsigned int *local_foop
;
48 /* Try to map self. */
49 initial_handle
= dlopen (NULL
, RTLD_LAZY
| RTLD_GLOBAL
);
50 if (initial_handle
== NULL
)
52 printf ("dlopen [initial] (NULL): %s\n", dlerror ());
56 /* Make sure symbol lookups fail gracefully. */
57 initial_foop
= dlsym (initial_handle
, "foo");
58 if (initial_foop
!= NULL
)
60 printf ("dlsym [initial] (foo): got %p, expected NULL\n", initial_foop
);
64 initial_getfoo
= dlsym (initial_handle
, "getfoo");
65 if (initial_getfoo
!= NULL
)
67 printf ("dlsym [initial] (getfoo): got %p, expected NULL\n",
72 initial_setfoo
= dlsym (initial_handle
, "setfoo");
73 if (initial_setfoo
!= NULL
)
75 printf ("dlsym [initial] (setfoo): got %p, expected NULL\n",
80 /* Try to map a module into the global scope. */
81 global_handle
= dlopen ("modstatic3.so", RTLD_LAZY
| RTLD_GLOBAL
);
82 if (global_handle
== NULL
)
84 printf ("dlopen [global] (modstatic3.so): %s\n", dlerror ());
88 /* Get at its symbols. */
89 global_foop
= dlsym (global_handle
, "foo");
90 if (global_foop
== NULL
)
92 printf ("dlsym [global] (foo): %s\n", dlerror ());
96 global_getfoo
= dlsym (global_handle
, "getfoo");
97 if (global_getfoo
== NULL
)
99 printf ("dlsym [global] (getfoo): %s\n", dlerror ());
103 global_setfoo
= dlsym (global_handle
, "setfoo");
104 if (global_setfoo
== NULL
)
106 printf ("dlsym [global] (setfoo): %s\n", dlerror ());
110 /* Try to map self again now. */
111 local_handle
= dlopen (NULL
, RTLD_LAZY
| RTLD_LOCAL
);
112 if (local_handle
== NULL
)
114 printf ("dlopen [local] (NULL): %s\n", dlerror ());
118 /* Make sure we can get at the previously loaded module's symbols
119 via this handle too. */
120 local_foop
= dlsym (local_handle
, "foo");
121 if (local_foop
== NULL
)
123 printf ("dlsym [local] (foo): %s\n", dlerror ());
127 local_getfoo
= dlsym (local_handle
, "getfoo");
128 if (local_getfoo
== NULL
)
130 printf ("dlsym [local] (getfoo): %s\n", dlerror ());
134 local_setfoo
= dlsym (local_handle
, "setfoo");
135 if (local_setfoo
== NULL
)
137 printf ("dlsym [local] (setfoo): %s\n", dlerror ());
141 /* Make sure we can get at the previously loaded module's symbols
142 via a handle that was obtained before the module was loaded too. */
143 initial_foop
= dlsym (initial_handle
, "foo");
144 if (initial_foop
== NULL
)
146 printf ("dlsym [initial] (foo): %s\n", dlerror ());
150 initial_getfoo
= dlsym (initial_handle
, "getfoo");
151 if (initial_getfoo
== NULL
)
153 printf ("dlsym [initial] (getfoo): %s\n", dlerror ());
157 initial_setfoo
= dlsym (initial_handle
, "setfoo");
158 if (initial_setfoo
== NULL
)
160 printf ("dlsym [initial] (setfoo): %s\n", dlerror ());
164 /* Make sure the view of the initial state is consistent. */
168 printf ("*foop [initial]: got %#x, expected %#x\n", foo
, MAGIC0
);
175 printf ("*foop [global]: got %#x, expected %#x\n", foo
, MAGIC0
);
182 printf ("*foop [local]: got %#x, expected %#x\n", foo
, MAGIC0
);
186 foo
= initial_getfoo ();
189 printf ("getfoo [initial]: got %#x, expected %#x\n", foo
, MAGIC0
);
193 foo
= global_getfoo ();
196 printf ("getfoo [global]: got %#x, expected %#x\n", foo
, MAGIC0
);
200 foo
= local_getfoo ();
203 printf ("getfoo [local]: got %#x, expected %#x\n", foo
, MAGIC0
);
207 /* Likewise with a change to its state made through the first handle. */
208 initial_setfoo (MAGIC1
);
213 printf ("*foop [initial]: got %#x, expected %#x\n", foo
, MAGIC1
);
220 printf ("*foop [global]: got %#x, expected %#x\n", foo
, MAGIC1
);
227 printf ("*foop [local]: got %#x, expected %#x\n", foo
, MAGIC1
);
231 foo
= initial_getfoo ();
234 printf ("getfoo [initial]: got %#x, expected %#x\n", foo
, MAGIC1
);
238 foo
= global_getfoo ();
241 printf ("getfoo [global]: got %#x, expected %#x\n", foo
, MAGIC1
);
245 foo
= local_getfoo ();
248 printf ("getfoo [local]: got %#x, expected %#x\n", foo
, MAGIC1
);
252 /* Likewise with a change to its state made through the second handle. */
253 global_setfoo (MAGIC2
);
258 printf ("*foop [initial]: got %#x, expected %#x\n", foo
, MAGIC2
);
265 printf ("*foop [global]: got %#x, expected %#x\n", foo
, MAGIC2
);
272 printf ("*foop [local]: got %#x, expected %#x\n", foo
, MAGIC2
);
276 foo
= initial_getfoo ();
279 printf ("getfoo [initial]: got %#x, expected %#x\n", foo
, MAGIC2
);
283 foo
= global_getfoo ();
286 printf ("getfoo [global]: got %#x, expected %#x\n", foo
, MAGIC2
);
290 foo
= local_getfoo ();
293 printf ("getfoo [local]: got %#x, expected %#x\n", foo
, MAGIC2
);
297 /* Likewise with a change to its state made through the third handle. */
298 local_setfoo (MAGIC3
);
303 printf ("*foop [initial]: got %#x, expected %#x\n", foo
, MAGIC3
);
310 printf ("*foop [global]: got %#x, expected %#x\n", foo
, MAGIC3
);
317 printf ("*foop [local]: got %#x, expected %#x\n", foo
, MAGIC3
);
321 foo
= initial_getfoo ();
324 printf ("getfoo [initial]: got %#x, expected %#x\n", foo
, MAGIC3
);
328 foo
= global_getfoo ();
331 printf ("getfoo [global]: got %#x, expected %#x\n", foo
, MAGIC3
);
335 foo
= local_getfoo ();
338 printf ("getfoo [local]: got %#x, expected %#x\n", foo
, MAGIC3
);
342 /* All done, clean up. */
343 initial_getfoo
= NULL
;
344 initial_setfoo
= NULL
;
350 dlclose (local_handle
);
352 global_getfoo
= NULL
;
353 global_setfoo
= NULL
;
355 dlclose (global_handle
);
357 dlclose (initial_handle
);
362 #define TEST_FUNCTION do_test ()
363 #include "../test-skeleton.c"