apolukhin / Boost.DLL

Library for comfortable work with DLL and DSO
https://boost.org/libs/dll
110 stars 69 forks source link

Suggestion: it seems there is no library enumeration mechanism in Boost.DLL #64

Open fsmoke opened 1 year ago

fsmoke commented 1 year ago

My suggestion - add routins for enumeration shared libraries already loaded by current process

For example we have host app 1) host.exe(or just bin on nix) 2) host.exe dynamically loads some plugin.dll/so 3) plugin.dll/so loads impl.dll/so automatically by lnker's import table(before run host(for example on linux) we set LD_LIBRARY_PATH to directory with impl.so to choose actual implementation) 4) now from host.exe i want to set some global variable inside impl.so(for example some global interfaces used by impl) - but i dont know which of impl.so was loaded inside my process, i can not determine path of loaded dll/so - so i can't import symbol

i wrote some platform dependent code to demonstrate root of my suggestion

std::filesystem::path impl_dll_path;
#ifdef BOOST_OS_LINUX
        auto self_handle = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL);
        for (auto link_map = static_cast<const struct link_map*>(self_handle);
            link_map; link_map = link_map->l_next)
        {
            std::string dll_name(link_map->l_name);
            if (dll_name.ends_with("libimpl.so"))
            {
                impl_dll_path = dll_name;
                break;
            }
        }
#endif

Overall i think enumeration of loaded libraries will be usefull in many other cases too

PS For Windows OS this article will be useful obviosly https://learn.microsoft.com/en-us/windows/win32/psapi/enumerating-all-modules-for-a-process