https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128967
Bug ID: 128967
Summary: format.o has weak copies of std::string members, so a
static link can pull it in for std::system_error
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: bernie at codewiz dot org
Target Milestone: ---
Created attachment 66267
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66267&action=edit
patch
src/c++20/format.cc is compiled with -std=gnu++26, and since C++23
<bits/basic_string.tcc> declares no "extern template class basic_string<char>".
So format.o implicitly instantiates the basic_string<char> members
it uses but does not inline, as weak definitions, although
src/c++11/string-inst.o already defines them.
With libstdc++ built at -Os, _M_dispose() is one of them. On x86_64-linux
with GCC 16.2.1, compiling format.cc with the flags of the libstdc++
build (-std=gnu++26 -fno-access-control -fno-implicit-templates
-ffunction-sections -fdata-sections):
-O2: no weak basic_string<char> member in format.o
-Os: format.o defines
_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE10_M_disposeEv (W)
In a static link, a program that needs _M_dispose() out of line, for
example through std::system_error, gets the first archive member that
defines it after the reference appears in ld's pass over the archive
index.
When that is format.o rather than string-inst.o, the program also
links the whole format.o and everything it references, including the
locale machinery for both char and wchar_t.
A program using threads grew from 117 KB to 749 KB of text in our
m68k-amigaos toolchain.
--gc-sections recovers little, as the locale code remains reachable.
This affects toolchains that build libstdc++ at -Os and link statically,
such as embedded ones.
Declaring the explicit instantiation in format.cc, before
text_encoding.cc is included, makes format.o refer to string-inst.o's
members instead:
#include <string>
namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
extern template class basic_string<char>;
_GLIBCXX_END_NAMESPACE_VERSION
}
The only new undefined symbol is _M_dispose(). std::format and
std::text_encoding work as before.