From: Waldemar Kozaczuk <jwkozac...@gmail.com>
Committer: Waldemar Kozaczuk <jwkozac...@gmail.com>
Branch: master

iconv: use stubs if iconv functions not used by an app

The GCC libstdc++ commit 
https://github.com/gcc-mirror/gcc/commit/c0ace69ec677d1f85f6a433c8fae2d4df6f75714
which I believe fixes the bug 
https://gcc.gnu.org/bugzilla/show_bug.cgi?format=multiple&id=64132
makes the C++ standard library depend on libc iconv* functions. The
implementation of these functions in musl employs many large arrays
used to translate characters from one encoding to another and inflates
the kernel by around 156K. After some research to see where exactly
the related code - _M_initialize_numpunct() and _M_initialize_moneypunct()
is executed it turns out that OSv itself internally does not trigger
the code path leading to the execution of iconv functions because
it uses a default C locale. This observation in general would be a mute
point as by default OSv provides iconv* functions as part of libc anyway so C++ 
standard
library using it does not really change anything. Except when one builds
custom kernel to remove all symbols but what application specifically
needs like so (for details see d19ccb1cde100ab4a5c8e6db9a0d69560cabbd04):

./scripts/build fs=rofs conf_hide_symbols=1 image=native-example \
     conf_version_script=build/last/app_version_script

In this case we would like to optimize kernel size and replace
real iconv functions which would be referenced by _M_initialize_numpunct and
_M_initialize_moneypunct with simple stubs in libc/locale/iconv_stubs.cc.
This optimization allows us to reduce kernel size by another 156K.

Signed-off-by: Waldemar Kozaczuk <jwkozac...@gmail.com>
Message-Id: <20220627185608.37930-1-jwkozac...@gmail.com>

---
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -1142,8 +1142,6 @@ musl += locale/catgets.o
 libc += locale/catopen.o
 libc += locale/duplocale.o
 libc += locale/freelocale.o
-musl += locale/iconv.o
-musl += locale/iconv_close.o
 libc += locale/intl.o
 libc += locale/langinfo.o
 musl += locale/localeconv.o
@@ -2085,8 +2083,16 @@ endif
 endif
 linker_archives_options = --no-whole-archive $(libstdc++.a) $(libgcc.a) 
$(libgcc_eh.a) $(boost-libs) \
   --exclude-libs libstdc++.a --gc-sections
+ifneq ($(shell grep -c iconv $(out)/version_script),0)
+musl += locale/iconv.o
+musl += locale/iconv_close.o
+else
+libc += locale/iconv_stubs.o
+endif
 else
 linker_archives_options = --whole-archive $(libstdc++.a) $(libgcc_eh.a) 
$(boost-libs) --no-whole-archive $(libgcc.a)
+musl += locale/iconv.o
+musl += locale/iconv_close.o
 endif
 
 $(out)/default_version_script: exported_symbols/*.symbols 
exported_symbols/$(arch)/*.symbols
diff --git a/libc/locale/iconv_stubs.cc b/libc/locale/iconv_stubs.cc
--- a/libc/locale/iconv_stubs.cc
+++ b/libc/locale/iconv_stubs.cc
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2022 Waldemar Kozaczuk
+ *
+ * This work is open source software, licensed under the terms of the
+ * BSD license as described in the LICENSE file in the top-level directory.
+ */
+
+#include <iconv.h>
+#include <errno.h>
+#include <osv/stubbing.hh>
+
+iconv_t iconv_open(const char *to, const char *from) {
+    WARN_STUBBED();
+    errno = EINVAL;
+    return (iconv_t)-1;
+}
+
+size_t iconv(iconv_t cd, char **in, size_t *inb, char **out, size_t *outb) {
+    WARN_STUBBED();
+    return 0l;
+}
+
+int iconv_close(iconv_t cd) {
+    WARN_STUBBED();
+    return 0l;
+}

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/000000000000cef62005e3407cbc%40google.com.

Reply via email to