• Show log

    Commit

  • Hash : 29c46a54
    Author : Pali Rohár
    Date : 2019-01-29T22:38:42

    Fix resolving global symbols when LoadLibrary() is called after dlopen() Usage of first_automatic_object cache is wrong. This cache is filled by all loaded DLL files (either implicitly or explicitly with LoadLibrary() call) by EnumProcessModules() call at first usage of dlopen(). So dlsym() can resolve global symbols only if they were loaded prior to dlopen() call. Any future usage of LoadLibrary() does not include newly loaded DLLs into first_automatic_object cache. To fix this problem, first_automatic_object cache is fully removed and EnumProcessModules() call is issued directly in dlsym() call. As EnumProcessModules() returns all DLLs, included those which were loaded by dlopen() with RTLD_LOCAL, it may break RTLD_LOCAL support. To address this problem switch linked-list of all loaded DLLs with RTLD_GLOBAL to linked-list of all loaded DLLs with RTLD_LOCAL flag. And then skip modules from EnumProcessModules() which are in linked-list. Also in WinAPI all DLLs loaded by LoadLibrary() behaves like RTLD_GLOBAL. So above change is compatible with this behavior. There may be another problem. Before retrieving HMODULE for DLL filename (which is done by LoadLibrary()), it is not possible to detect if DLL was already loaded by RTLD_LOCAL or not. And after calling LoadLibrary() it is not possible to know if DLL was loaded either by dlsym() with RTLD_LOCAL or by LoadLibrary() (which is equivalent to RTLD_GLOBAL). To address this problem, compare number of loaded modules (counted by EnumProcessModules()) before and after LoadLibrary() called from dlsym(). If number does not change it means that DLL was already loaded. So based on this result either add or remove HMODULE from linked-list of RTLD_LOCAL modules. Added test demonstrate usage of: global = dlopen(NULL, RTLD_GLOBAL); /* global handle */ LoadLibrary("library.dll"); /* this provides function */ function = dlsym(global, "function"); /* resolve function from library.dll */

  • Properties

  • Git HTTP https://git.kmx.io/kc3-lang/dlfcn-win32.git
    Git SSH git@git.kmx.io:kc3-lang/dlfcn-win32.git
    Public access ? public
    Description
    Users
    thodg_l kc3_lang_org thodg_w thodg_m thodg www_kmx_io
    Tags

  • README.md

  • dlfcn-win32 Build status

    dlfcn-win32 is an implementation of dlfcn for Windows.

    dlfcn is a set of functions that allows runtime dynamic library loading. It is standardized in the POSIX. Windows also provide similar routines, but not in a POSIX-compatible way. This library attempts to implement a wrapper around the Windows functions to make programs written for POSIX that use dlfcn work in Windows without any modifications.

    It follows the standard as described here:

    http://www.opengroup.org/onlinepubs/009695399/basedefs/dlfcn.h.html http://www.opengroup.org/onlinepubs/009695399/functions/dlerror.html http://www.opengroup.org/onlinepubs/009695399/functions/dlsym.html http://www.opengroup.org/onlinepubs/009695399/functions/dlclose.html http://www.opengroup.org/onlinepubs/009695399/functions/dlopen.html

    Using This Library

    Using CMake

    Once the library has been installed, add to your project CMakeLists.txt :

    ...
    find_package(dlfcn-win32 REQUIRED)
    ...
    target_link_libraries(<target> dlfcn-win32::dl)
    ...

    If you want to use this library in a cross-platform project, a convenient way to proceed is to define the CMake variable CMAKE_DL_LIBS (that is normally empty on Windows) and then use it for linking:

    ...
    if (WIN32)
      find_package(dlfcn-win32 REQUIRED)
      set(CMAKE_DL_LIBS dlfcn-win32::dl)
    endif ()  
    ...
    target_link_libraries(<target> ${CMAKE_DL_LIBS})
    ...

    Linking caveat

    This library uses the Process Status API in Windows (psapi.lib). If you are linking to the static dl.lib or libdl.a, then you would need to explicitly add psapi.lib or -lpsapi to your linking command, depending on if MinGW is used.

    Author

    Written by Ramiro Polla in 2007. Maintained by Tiancheng “Timothy” Gu from 2013.

    License

    dlfcn-win32 is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.

    dlfcn-win32 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License along with dlfcn-win32; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA