Get rid of non-POSIX code for loading dynlibs on *nix

The alternative, (non-POSIX) shl_xxx() API is/was apparently available on
HP-UX, but even there the POSIX dlxxx() functions have been the preferred
way to load libraries since the past ~20 years.
This commit is contained in:
Lauri Nurmi 2020-05-13 09:30:40 +03:00
parent c50784ba0b
commit e289eb07e1

View File

@ -50,9 +50,7 @@
#endif #endif
#if defined(HAVE_DLOPEN) #ifndef HAVE_DLOPEN
#define USE_POSIX_DL_FUNCS
#elif !defined(HAVE_SHL_LOAD)
#error "Don't know how to load dynamic libraries on this platform!" #error "Don't know how to load dynamic libraries on this platform!"
#endif #endif
@ -66,11 +64,7 @@
wxDllType wxDynamicLibrary::GetProgramHandle() wxDllType wxDynamicLibrary::GetProgramHandle()
{ {
#ifdef USE_POSIX_DL_FUNCS
return dlopen(0, RTLD_LAZY); return dlopen(0, RTLD_LAZY);
#else
return PROG_HANDLE;
#endif
} }
/* static */ /* static */
@ -79,7 +73,6 @@ wxDllType wxDynamicLibrary::RawLoad(const wxString& libname, int flags)
wxASSERT_MSG( !(flags & wxDL_NOW) || !(flags & wxDL_LAZY), wxASSERT_MSG( !(flags & wxDL_NOW) || !(flags & wxDL_LAZY),
wxT("wxDL_LAZY and wxDL_NOW are mutually exclusive.") ); wxT("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
#ifdef USE_POSIX_DL_FUNCS
// we need to use either RTLD_NOW or RTLD_LAZY because if we call dlopen() // we need to use either RTLD_NOW or RTLD_LAZY because if we call dlopen()
// with flags == 0 recent versions of glibc just fail the call, so use // with flags == 0 recent versions of glibc just fail the call, so use
// RTLD_NOW even if wxDL_NOW was not specified // RTLD_NOW even if wxDL_NOW was not specified
@ -89,54 +82,21 @@ wxDllType wxDynamicLibrary::RawLoad(const wxString& libname, int flags)
rtldFlags |= RTLD_GLOBAL; rtldFlags |= RTLD_GLOBAL;
return dlopen(libname.fn_str(), rtldFlags); return dlopen(libname.fn_str(), rtldFlags);
#else // !USE_POSIX_DL_FUNCS
int shlFlags = 0;
if ( flags & wxDL_LAZY )
{
shlFlags |= BIND_DEFERRED;
}
else if ( flags & wxDL_NOW )
{
shlFlags |= BIND_IMMEDIATE;
}
return shl_load(libname.fn_str(), shlFlags, 0);
#endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
} }
/* static */ /* static */
void wxDynamicLibrary::Unload(wxDllType handle) void wxDynamicLibrary::Unload(wxDllType handle)
{ {
#ifdef HAVE_DLERROR int rc = dlclose(handle);
int rc =
#endif
#ifdef USE_POSIX_DL_FUNCS
dlclose(handle);
#else // !USE_POSIX_DL_FUNCS
shl_unload(handle);
#endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
#if defined(USE_POSIX_DL_FUNCS) && defined(HAVE_DLERROR)
if ( rc != 0 ) if ( rc != 0 )
#endif
ReportError(_("Failed to unload shared library")); ReportError(_("Failed to unload shared library"));
} }
/* static */ /* static */
void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name) void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
{ {
void *symbol; void *symbol = dlsym(handle, name.fn_str());
#ifdef USE_POSIX_DL_FUNCS
symbol = dlsym(handle, name.fn_str());
#else // !USE_POSIX_DL_FUNCS
// note that shl_findsym modifies the handle argument to indicate where the
// symbol was found, but it's ok to modify the local handle copy here
if ( shl_findsym(&handle, name.fn_str(), TYPE_UNDEFINED, &symbol) != 0 )
symbol = 0;
#endif // USE_POSIX_DL_FUNCS/!USE_POSIX_DL_FUNCS
return symbol; return symbol;
} }
@ -154,16 +114,13 @@ void wxDynamicLibrary::ReportError(const wxString& message,
msg += "%s"; msg += "%s";
// msg needs a %s for the name // msg needs a %s for the name
wxASSERT(msg.Find("%s") != wxNOT_FOUND); wxASSERT(msg.Find("%s") != wxNOT_FOUND);
#ifdef HAVE_DLERROR
wxString err(dlerror()); wxString err(dlerror());
if ( err.empty() ) if ( err.empty() )
err = _("Unknown dynamic library error"); err = _("Unknown dynamic library error");
wxLogError(msg + wxT(": %s"), name, err); wxLogError(msg + wxT(": %s"), name, err);
#else // !HAVE_DLERROR
wxLogSysError(msg, name);
#endif // HAVE_DLERROR
} }