https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111347

            Bug ID: 111347
           Summary: Memory leak loading a shared library built with
                    --static-libsdtc++ when version script is used
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gyunaev at ulduzsoft dot com
  Target Milestone: ---

Created attachment 55860
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55860&action=edit
The source code of the library, tester and Makefile

Gcc 13.2 built from the source. Linux x86_64, OpenSuSe Leap 15.5.

Our team has found that when building a C++ DSO using --static-libgcc and
--static-libstdc++ and using a version script to hide the visibility of
symbols, loading and unloading the resulting shared library leaks memory each
time such dlopen/dlclose happens. 

If a version script is not used, there is no leak.

Smallest shared library source which reproduces the issue:

------ libtest.cpp ------
#include <string>

int test()
{
    std::string a;
    return a.length();
}
---

The version script:
------ libtest.ver ------
{
    global:
        test;

    local:
        *;
};
---

Built with:

g++ -fPIC -shared -static-libgcc -static-libstdc++
-Wl,--version-script=libtest.ver libtest.cpp -o libtest.so

load-unload of this library leaks memory each time it unloads.
Loading-unloading is all that is needed - you don't need to call test or even
dlsym it. Easiest to see it when running dlclose( dlopen( "./libtest.so",
RTLD_NOW)) in a loop.

If you remove --version-script and rebuild the library, load-unload will not
leak.

Experimentally we found out that adding the symbol "_ZNSt8messagesIwE2idE;"
into global: section of version script would stop the leak, but this isn't
obvious to me why. This symbol demangles into std::messages<wchar_t>::id;

Attaching the tester, makefile and the loader.

Reply via email to