xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Make sure dlopen/dlclose are not marked as leaf functions. |
| 2 | |
| 3 | Copyright (C) 2013-2016 Free Software Foundation, Inc. |
| 4 | This file is part of the GNU C Library. |
| 5 | Contributed by Mike Frysinger <vapier@gentoo.org> |
| 6 | |
| 7 | The GNU C Library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation; either |
| 10 | version 2.1 of the License, or (at your option) any later version. |
| 11 | |
| 12 | The GNU C Library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with the GNU C Library; if not, see |
| 19 | <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | /* The bug-dl-leaf.c file will call our lib_main directly. We do this to |
| 22 | keep things simple -- no need to use --export-dynamic with the linker |
| 23 | or build the main ELF as a PIE. |
| 24 | |
| 25 | The lib_main func will modify some of its state while dlopening and |
| 26 | dlclosing the bug-dl-leaf-lib-cb.so library. The constructors and |
| 27 | destructors in that library will call back into this library to also |
| 28 | muck with state (the check_val_xxx funcs). |
| 29 | |
| 30 | If dlclose/dlopen are marked as "leaf" functions, then with newer |
| 31 | versions of gcc, the state modification won't work correctly. */ |
| 32 | |
| 33 | #include <assert.h> |
| 34 | #include <dlfcn.h> |
| 35 | |
| 36 | static int val = 1; |
| 37 | static int called = 0; |
| 38 | |
| 39 | void check_val_init (void) |
| 40 | { |
| 41 | called = 1; |
| 42 | assert (val == 2); |
| 43 | } |
| 44 | |
| 45 | void check_val_fini (void) |
| 46 | { |
| 47 | called = 2; |
| 48 | assert (val == 4); |
| 49 | } |
| 50 | |
| 51 | int lib_main (void) |
| 52 | { |
| 53 | int ret; |
| 54 | void *hdl; |
| 55 | |
| 56 | /* Make sure the constructor sees the updated val. */ |
| 57 | val = 2; |
| 58 | hdl = dlopen ("bug-dl-leaf-lib-cb.so", RTLD_GLOBAL | RTLD_LAZY); |
| 59 | val = 3; |
| 60 | assert (hdl); |
| 61 | assert (called == 1); |
| 62 | |
| 63 | /* Make sure the destructor sees the updated val. */ |
| 64 | val = 4; |
| 65 | ret = dlclose (hdl); |
| 66 | val = 5; |
| 67 | assert (ret == 0); |
| 68 | assert (called == 2); |
| 69 | |
| 70 | return !val; |
| 71 | } |