1 /* Copyright 2023-2024 Free Software Foundation, Inc. 2 3 This program is free software; you can redistribute it and/or modify 4 it under the terms of the GNU General Public License as published by 5 the Free Software Foundation; either version 3 of the License, or 6 (at your option) any later version. 7 8 This program is distributed in the hope that it will be useful, 9 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 GNU General Public License for more details. 12 13 You should have received a copy of the GNU General Public License 14 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #include <dlfcn.h> 17 #include <stdlib.h> 18 19 void 20 breakpt (void) 21 { 22 /* Nothing. */ 23 } 24 25 volatile int global_counter = 0; 26 27 volatile int call_count = 1; 28 29 int 30 main (void) 31 { 32 void *handle; 33 void (*func)(int); 34 35 /* Some filler work so that we don't initially stop on the breakpt call 36 below. */ 37 ++global_counter; 38 39 breakpt (); /* Break before open. */ 40 41 /* Now load the shared library. */ 42 handle = dlopen (SHLIB_NAME, RTLD_LAZY); 43 if (handle == NULL) 44 abort (); 45 46 breakpt (); /* Break after open. */ 47 48 /* Find the function symbol. */ 49 func = (void (*)(int)) dlsym (handle, "foo"); 50 51 for (; call_count > 0; --call_count) 52 { 53 /* Call the library function. */ 54 func (1); 55 } 56 57 breakpt (); /* Break before close. */ 58 59 /* Unload the shared library. */ 60 if (dlclose (handle) != 0) 61 abort (); 62 63 breakpt (); /* Break after close. */ 64 65 return 0; 66 } 67