18dffb485Schristos /* Platform independent shared object routines for GDB. 28dffb485Schristos 3*5ba1f45fSchristos Copyright (C) 2011-2024 Free Software Foundation, Inc. 48dffb485Schristos 58dffb485Schristos This file is part of GDB. 68dffb485Schristos 78dffb485Schristos This program is free software; you can redistribute it and/or modify 88dffb485Schristos it under the terms of the GNU General Public License as published by 98dffb485Schristos the Free Software Foundation; either version 3 of the License, or 108dffb485Schristos (at your option) any later version. 118dffb485Schristos 128dffb485Schristos This program is distributed in the hope that it will be useful, 138dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 148dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 158dffb485Schristos GNU General Public License for more details. 168dffb485Schristos 178dffb485Schristos You should have received a copy of the GNU General Public License 188dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 198dffb485Schristos 208dffb485Schristos #include "gdb-dlfcn.h" 218dffb485Schristos 228dffb485Schristos #ifdef HAVE_DLFCN_H 238dffb485Schristos #include <dlfcn.h> 248dffb485Schristos #elif __MINGW32__ 258dffb485Schristos #include <windows.h> 268dffb485Schristos #else 278dffb485Schristos /* Unsupported configuration. */ 288dffb485Schristos #define NO_SHARED_LIB 298dffb485Schristos #endif 308dffb485Schristos 318dffb485Schristos #ifdef NO_SHARED_LIB 328dffb485Schristos 338dffb485Schristos gdb_dlhandle_up 348dffb485Schristos gdb_dlopen (const char *filename) 358dffb485Schristos { 368dffb485Schristos gdb_assert_not_reached ("gdb_dlopen should not be called on this platform."); 378dffb485Schristos } 388dffb485Schristos 398dffb485Schristos void * 408dffb485Schristos gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol) 418dffb485Schristos { 428dffb485Schristos gdb_assert_not_reached ("gdb_dlsym should not be called on this platform."); 438dffb485Schristos } 448dffb485Schristos 458dffb485Schristos void 468dffb485Schristos dlclose_deleter::operator() (void *handle) const 478dffb485Schristos { 488dffb485Schristos gdb_assert_not_reached ("gdb_dlclose should not be called on this platform."); 498dffb485Schristos } 508dffb485Schristos 518dffb485Schristos int 528dffb485Schristos is_dl_available (void) 538dffb485Schristos { 548dffb485Schristos return 0; 558dffb485Schristos } 568dffb485Schristos 578dffb485Schristos #else /* NO_SHARED_LIB */ 588dffb485Schristos 598dffb485Schristos gdb_dlhandle_up 608dffb485Schristos gdb_dlopen (const char *filename) 618dffb485Schristos { 628dffb485Schristos void *result; 638dffb485Schristos #ifdef HAVE_DLFCN_H 648dffb485Schristos result = dlopen (filename, RTLD_NOW); 658dffb485Schristos #elif __MINGW32__ 668dffb485Schristos result = (void *) LoadLibrary (filename); 678dffb485Schristos #endif 688dffb485Schristos if (result != NULL) 698dffb485Schristos return gdb_dlhandle_up (result); 708dffb485Schristos 718dffb485Schristos #ifdef HAVE_DLFCN_H 728dffb485Schristos error (_("Could not load %s: %s"), filename, dlerror()); 738dffb485Schristos #else 748dffb485Schristos { 758dffb485Schristos LPVOID buffer; 768dffb485Schristos DWORD dw; 778dffb485Schristos 788dffb485Schristos dw = GetLastError(); 798dffb485Schristos 808dffb485Schristos FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | 818dffb485Schristos FORMAT_MESSAGE_IGNORE_INSERTS, 828dffb485Schristos NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), 838dffb485Schristos (LPTSTR) &buffer, 848dffb485Schristos 0, NULL); 858dffb485Schristos 868dffb485Schristos error (_("Could not load %s: %s"), filename, (char *) buffer); 878dffb485Schristos } 888dffb485Schristos #endif 898dffb485Schristos } 908dffb485Schristos 918dffb485Schristos void * 928dffb485Schristos gdb_dlsym (const gdb_dlhandle_up &handle, const char *symbol) 938dffb485Schristos { 948dffb485Schristos #ifdef HAVE_DLFCN_H 958dffb485Schristos return dlsym (handle.get (), symbol); 968dffb485Schristos #elif __MINGW32__ 978dffb485Schristos return (void *) GetProcAddress ((HMODULE) handle.get (), symbol); 988dffb485Schristos #endif 998dffb485Schristos } 1008dffb485Schristos 1018dffb485Schristos void 1028dffb485Schristos dlclose_deleter::operator() (void *handle) const 1038dffb485Schristos { 1048dffb485Schristos #ifdef HAVE_DLFCN_H 1058dffb485Schristos dlclose (handle); 1068dffb485Schristos #elif __MINGW32__ 1078dffb485Schristos FreeLibrary ((HMODULE) handle); 1088dffb485Schristos #endif 1098dffb485Schristos } 1108dffb485Schristos 1118dffb485Schristos int 1128dffb485Schristos is_dl_available (void) 1138dffb485Schristos { 1148dffb485Schristos return 1; 1158dffb485Schristos } 1168dffb485Schristos 1178dffb485Schristos #endif /* NO_SHARED_LIB */ 118