1 /* This testcase is part of GDB, the GNU debugger. 2 3 Copyright 2017-2020 Free Software Foundation, Inc. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program 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 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #include <dlfcn.h> 19 #include <errno.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 24 int 25 main () 26 { 27 void *handle; 28 int (*foo) (int); 29 char *error; 30 char *dest = VANISH_LIB ".renamed"; 31 32 /* Open library. */ 33 handle = dlopen (VANISH_LIB, RTLD_NOW); 34 if (!handle) 35 { 36 fprintf (stderr, "%s\n", dlerror ()); 37 exit (EXIT_FAILURE); 38 } 39 40 /* Clear any existing error */ 41 dlerror (); 42 43 /* Simulate deleting file by renaming it. */ 44 if (rename (VANISH_LIB, dest) == -1) 45 { 46 error = strerror (errno); 47 fprintf (stderr, "rename %s -> %s: %s\n", VANISH_LIB, dest, error); 48 exit (EXIT_FAILURE); 49 } 50 51 /* Get function pointer. */ 52 foo = dlsym (handle, "foo"); 53 error = dlerror (); 54 if (error != NULL) 55 { 56 fprintf (stderr, "%s\n", error); 57 exit (EXIT_FAILURE); 58 } 59 60 /* Call function. */ 61 (*foo) (1); 62 63 /* Close and exit. */ 64 dlclose (handle); 65 66 /* Put VANISH_LIB back where we found it. */ 67 if (rename (dest, VANISH_LIB) == -1) 68 { 69 error = strerror (errno); 70 fprintf (stderr, "rename %s -> %s: %s\n", dest, VANISH_LIB, error); 71 exit (EXIT_FAILURE); 72 } 73 74 exit (EXIT_SUCCESS); 75 } 76