[PATCH] libio: Properly link in libio functions in static binaries
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Thu Aug 21 17:33:18 GMT 2025
On 21/08/25 11:17, H.J. Lu wrote:
> commit 3020f72618e4f1d7338cd42b8bc7b2813e961b5a
> Author: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
> Date: Tue Dec 27 18:11:43 2022 -0300
>
> libio: Remove the usage of __libc_IO_vtables
>
> added
>
> #define libio_static_fn_required(name) __asm (".globl " #name);
>
> to link in libio functions in static binaries. But there is no relocation
> in
> .globl _IO_file_open
>
> and "strip --strip-unneeded" will remove such unreferenced symbols which
> breaks static binaries. Redefine libio_static_fn_required to create a
> reference to the required function with
>
> .pushsection ".data.rel.ro.local","aw"
> .L_IO_file_open:
> .dc.a _IO_file_open
> .popsection
>
> This fixes BZ #33300.
>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
It seems that this trick does not work on s390 (32 bits):
$ s390x-glibc-linux-gnu-gcc -m31 stdio.c [...]
/tmp/ccDrrXoz.s: Assembler messages:
/tmp/ccDrrXoz.s:10: Error: cannot represent relocation type BFD_RELOC_64
/tmp/ccDrrXoz.s:14: Error: cannot represent relocation type BFD_RELOC_64
It does looks like a gas issue, I would expect the target would use
BFD_RELOC_32 internally. Using .dc.l seems to fix the build:
diff --git a/libio/libioP.h b/libio/libioP.h
index 24958676b1..4e8dea6fd1 100644
--- a/libio/libioP.h
+++ b/libio/libioP.h
@@ -535,10 +535,15 @@ extern const struct _IO_jump_t __io_vtables[] attribute_hidden;
#ifdef SHARED
# define libio_static_fn_required(name)
#else
+# if __WORDSIZE == 64
+# define _dc_directive ".dc.a"
+# else
+# define _dc_directive ".dc.l"
+# endif
# define libio_static_fn_required(name) \
__asm (".pushsection \".data.rel.ro.local\",\"aw\"\n" \
".L"#name":\n" \
-" .dc.a " #name"\n" \
+" " _dc_directive " "#name"\n" \
" .popsection");
#endif
> ---
> libio/Makefile | 9 +++++++++
> libio/libioP.h | 6 +++++-
> libio/tst-stdio-static.c | 28 ++++++++++++++++++++++++++++
> 3 files changed, 42 insertions(+), 1 deletion(-)
> create mode 100644 libio/tst-stdio-static.c
>
> diff --git a/libio/Makefile b/libio/Makefile
> index f020f8ec4d..6ce669e103 100644
> --- a/libio/Makefile
> +++ b/libio/Makefile
> @@ -130,6 +130,7 @@ tests = \
> tst-sprintf-chk-ub \
> tst-sprintf-ub \
> tst-sscanf \
> + tst-stdio-static \
> tst-swscanf \
> tst-ungetwc1 \
> tst-ungetwc2 \
> @@ -149,6 +150,8 @@ tests = \
> tst_wscanf \
> # tests
>
> +tests-static += tst-stdio-static
> +
> $(objpfx)tst-popen-fork: $(shared-thread-library)
>
> tests-internal = tst-vtables tst-vtables-interposed
> @@ -445,3 +448,9 @@ $(objpfx)tst-bz22415-mem.out: $(objpfx)tst-bz22415.out
> $(objpfx)tst-bz24228-mem.out: $(objpfx)tst-bz24228.out
> $(common-objpfx)malloc/mtrace $(objpfx)tst-bz24228.mtrace > $@; \
> $(evaluate-test)
> +
> +# Test stdio.o with "strip --strip-unneeded".
> +$(objpfx)tst-stdio-static: $(objpfx)stdio-stripped.o
> +
> +$(objpfx)stdio-stripped.o: $(objpfx)stdio.o
> + $(STRIP) --strip-unneeded -o $@ $<
> diff --git a/libio/libioP.h b/libio/libioP.h
> index 1e3d28bb34..24958676b1 100644
> --- a/libio/libioP.h
> +++ b/libio/libioP.h
> @@ -535,7 +535,11 @@ extern const struct _IO_jump_t __io_vtables[] attribute_hidden;
> #ifdef SHARED
> # define libio_static_fn_required(name)
> #else
> -# define libio_static_fn_required(name) __asm (".globl " #name);
> +# define libio_static_fn_required(name) \
> + __asm (".pushsection \".data.rel.ro.local\",\"aw\"\n" \
> +".L"#name":\n" \
> +" .dc.a " #name"\n" \
> +" .popsection");
> #endif
>
> extern int _IO_do_write (FILE *, const char *, size_t);
> diff --git a/libio/tst-stdio-static.c b/libio/tst-stdio-static.c
> new file mode 100644
> index 0000000000..48c7f76e6e
> --- /dev/null
> +++ b/libio/tst-stdio-static.c
> @@ -0,0 +1,28 @@
> +/* Test static stdio.o with "strip --strip-unneeded".
> + Copyright (C) 2025 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library 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.
> +
> + The GNU C Library 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 the GNU C Library; if not, see
> + <https://www.gnu.org/licenses/>. */
> +
> +#include <stdio.h>
> +
> +/* NB: Call main directly to trigger BZ #33300. */
> +
> +int
> +main (void)
> +{
> + printf ("OK\n");
> + return 0;
> +}
More information about the Libc-alpha
mailing list