**Contents** [[!toc levels=3]] # Introduction The NetBSD installation is not big, but it can be made smaller. Here are a few tricks to make the base of user space, the C standard library libc, a bit smaller for dynamically linked systems. These trials were done on a cross compiled NetBSD current ARMv6 branch. First the user space is built with default options. Then the cross compiler script **$TOOLDIR/bin/nbmake-evbarm** was used to clean and rebuild the libc with special options. The new libc binary was then copied to the target file system and a smoke test of booting the ARM development board was done. If /sbin/init and /bin/sh managed with the new libc, the test was a success while everything else was a failure. The result is a crippled libc and /lib/libc.so.12.159 file size reduction from 1164 to 692 kilobytes. Run time memory usage is harder to predict since it depends on what parts of in memory libc are actually used by the processes, but at least text and data sections reported by the size utility give some idea. # Build options ## Default -O2, file size 1164025 -O2 optimization is used by default and the libc file size after a **./build.sh -U -m evbarm build** is: -r--r--r-- 1 test test 1164025 2008-04-18 08:23 obj/destdir.evbarm/lib/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 931608 25728 64332 1021668 f96e4 obj/libc.so.12.159 ## -O1, file size 1159845 If the libc is build with **CFLAGS=-O1**, the ELF shared object file size is: -rw-r--r-- 1 test test 1159845 Apr 19 09:20 lib/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 927436 25728 64332 1017496 f8698 obj/libc.so.12.159 ### Linker strip, file size 1065624 If the **-O1** build is stripped and size optimized by the linker with **LDFLAGS=-Wl,-O1\ -Wl,-s**, the file size reduces to: -rwxr-xr-x 1 test test 1065624 2008-04-19 13:28 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 923316 25728 64332 1013376 f7680 obj/libc.so.12.159 ## -Os, file size 1094281 The gcc compiler can optimize binaries for size with **CFLAGS=-Os**: -rwxr-xr-x 1 test test 1094281 2008-04-19 10:56 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 861864 25736 64332 951932 e867c obj/libc.so.12.159 ### Manual strip, file size 1004180 The binary can then be stripped manually: $ $TOOLDIR/bin/arm--netbsdelf-strip -s obj/libc.so.12.159 $ ls -l obj/libc.so.12.159 -rwxr-xr-x 1 test test 1004180 2008-04-19 11:02 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 861864 25736 64332 951932 e867c obj/libc.so.12.159 ### Linker strip, file size 1000060 The **-Os** compiled binary is smaller with linker based strip and optimization where **LDFLAGS=-Wl,-O1\ -Wl,-s** than with a manual strip: -rwxr-xr-x 1 test test 1000060 2008-04-19 12:07 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 857744 25736 64332 947812 e7664 obj/libc.so.12.159 # Feature removal In addition to compiler flags **CFLAGS=-Os LDFLAGS=-Wl,-O1\ -Wl,-s**, special feature flags can be used to strip features out of libc and reduce its size. Some feature flags, as documented by **BUILDING** and **share/mk/bsd.README**, are supported for the whole user space. ## SCCS version strings, file size 953136 SCCS version strings are normally embedded into object file, but they be removed by following changes in lib/libc/Makefile: --- lib/libc/Makefile.inc 3 Jun 2007 17:36:08 -0000 1.3 +++ lib/libc/Makefile.inc 19 Apr 2008 11:01:23 -0000 @@ -24,7 +24,8 @@ .include WARNS=4 -CPPFLAGS+= -D_LIBC -DLIBC_SCCS -DSYSLIBC_SCCS -D_REENTRANT +#CPPFLAGS+= -D_LIBC -DLIBC_SCCS -DSYSLIBC_SCCS -D_REENTRANT +CPPFLAGS+= -D_LIBC -D_REENTRANT .if (${USE_HESIOD} != "no") CPPFLAGS+= -DHESIOD The resulting libc binary finally goes below the one megabyte mark: -rwxr-xr-x 1 test test 953136 2008-04-19 13:54 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 857744 25736 64332 947812 e7664 obj/libc.so.12.159 ## Hesiod name service, file size 942468 [Hesiod](http://en.wikipedia.org/wiki/Hesiod_(name_service)), a DNS based database service, support can be removed from libc with **USE_HESIOD=no MKHESIOD=no** build variables and result is: -rwxr-xr-x 1 test test 942468 2008-04-19 14:16 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 847625 25252 62180 935057 e4491 obj/libc.so.12.159 ## Yellow Pages (YP), file size 917368 [Yellow Pages (YP) or Network Information Service (NIS)](http://en.wikipedia.org/wiki/Network_Information_Service) directory service support can be removed with **USE_YP=no MKYP=no** variables: -rwxr-xr-x 1 test test 917368 2008-04-19 14:29 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 824328 24488 58944 907760 dd9f0 obj/libc.so.12.159 ## IPv6 support, file size 909272 [IPv6](http://en.wikipedia.org/wiki/IPv6) support can be removed with **USE_INET6=no**: -rwxr-xr-x 1 test test 909272 2008-04-19 14:48 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 816537 24368 58944 899849 dbb09 obj/libc.so.12.159 ## Stack smashing protection (SSP), file size 894764 [SSP](http://en.wikipedia.org/wiki/Stack-smashing_protection) buffer overflow protection from the GCC compiler can be disabled with **USE_SSP=no** and the libc binary size goes below 900k: -rwxr-xr-x 1 test test 894764 2008-04-19 15:02 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 802029 24368 58944 885341 d825d obj/libc.so.12.159 ## Remote procedure call (RPC), file size 806036 [RPC](http://en.wikipedia.org/wiki/ONC_RPC) support can be disabled with **MKRPC=no** variable and a patch like this: --- lib/libc/Makefile 9 Jan 2008 01:33:52 -0000 1.131.4.1 +++ lib/libc/Makefile 23 Apr 2008 13:04:42 -0000 @@ -74,7 +80,10 @@ .endif .include "${.CURDIR}/regex/Makefile.inc" .include "${.CURDIR}/resolv/Makefile.inc" +MKRPC?= yes +.if (${MKRPC} != "no") .include "${.CURDIR}/rpc/Makefile.inc" +.endif .include "${.CURDIR}/ssp/Makefile.inc" .include "${.CURDIR}/stdio/Makefile.inc" .include "${.CURDIR}/stdlib/Makefile.inc" As a result the libc size goes down to 806 kilobytes: -rw-r--r-- 1 test test 806036 2008-04-23 16:00 lib/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 717964 22624 58500 799088 c3170 obj/libc.so.12.159 ## Execution profiling control, file size 801720 [Profiling control](http://www.daemon-systems.org/man/moncontrol.3.html) can be removed from libc with **MKGMON=no** and a patch like: --- lib/libc/Makefile 9 Jan 2008 01:33:52 -0000 1.131.4.1 +++ lib/libc/Makefile 24 Apr 2008 08:07:08 -0000 @@ -58,7 +58,10 @@ .include "${.CURDIR}/dlfcn/Makefile.inc" .include "${.CURDIR}/gdtoa/Makefile.inc" .include "${.CURDIR}/gen/Makefile.inc" +MKGMON?= yes +.if (${MKGMON} != "no") .include "${.CURDIR}/gmon/Makefile.inc" +.endif .include "${.CURDIR}/hash/Makefile.inc" .include "${.CURDIR}/iconv/Makefile.inc" .include "${.CURDIR}/inet/Makefile.inc" And libc size goes down around 4 kilobytes: -rwxr-xr-x 1 test test 801720 2008-04-24 10:57 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 713959 22500 58436 794895 c210f obj/libc.so.12.159 ## Citrus I18N, file size 767560 [Citrus I18N](http://citrus.bsdclub.org/) support can be removed with **MKCITRUS=no**, [lib/libc/locale/setlocale.c patch](http://wiki.netbsd.se/images/d/d5/citrus_locale_patch01.txt) and a makefile patch: --- Makefile 9 Jan 2008 01:33:52 -0000 1.131.4.1 +++ Makefile 25 Apr 2008 07:51:20 -0000 @@ -53,12 +53,18 @@ .include "${.CURDIR}/../../common/lib/libc/Makefile.inc" .include "${.CURDIR}/db/Makefile.inc" +MKCITRUS?= yes +.if (${MKCITRUS} != "no") .include "${.CURDIR}/citrus/Makefile.inc" +.endif .include "${.CURDIR}/compat-43/Makefile.inc" .include "${.CURDIR}/dlfcn/Makefile.inc" .include "${.CURDIR}/gdtoa/Makefile.inc" The libc binary is now below 800 kilobytes: -rwxr-xr-x 1 test test 767560 2008-04-25 10:01 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 685150 18696 56968 760814 b9bee obj/libc.so.12.159 ## MD2, RMD160, SHA1, SHA2, MD4 and MD5 cryptographic hash functions, file size 723780 All [cryptographic hash functions](http://en.wikipedia.org/wiki/Cryptographic_hash_function) can be removed from the libc with **MKMD2=no MKRMD160=no MKSHA1=no MKSHA2=no MKMD=no** and lib/libc/hash/Makefile.inf and lib/libc/Makefile patches: --- lib/libc/hash/Makefile.inc 27 Oct 2006 18:29:21 -0000 1.11 +++ lib/libc/hash/Makefile.inc 30 Apr 2008 09:10:21 -0000 @@ -4,8 +4,20 @@ # hash functions .PATH: ${ARCHDIR}/hash ${.CURDIR}/hash +MKMD2?= yes +.if (${MKMD2} != "no") .include "${.CURDIR}/hash/md2/Makefile.inc" +.endif +MKRMD160?= yes +.if (${MKRMD160} != "no") .include "${.CURDIR}/hash/rmd160/Makefile.inc" +.endif +MKSHA1?= yes +.if (${MKSHA1} != "no") .include "${.CURDIR}/hash/sha1/Makefile.inc" +.endif +MKSHA2?= yes +.if (${MKSHA2} != "no") .include "${.CURDIR}/hash/sha2/Makefile.inc" +.endif --- lib/libc/Makefile 9 Jan 2008 01:33:52 -0000 1.131.4.1 +++ lib/libc/Makefile 30 Apr 2008 09:11:25 -0000 .include "${.CURDIR}/inet/Makefile.inc" .include "${.CURDIR}/isc/Makefile.inc" .include "${.CURDIR}/locale/Makefile.inc" +MKMD?= yes +.if (${MKMD} != "no") .include "${.CURDIR}/md/Makefile.inc" +.endif .include "${.CURDIR}/misc/Makefile.inc" .include "${.CURDIR}/net/Makefile.inc" .include "${.CURDIR}/nameser/Makefile.inc" libc size reduces to: -rwxr-xr-x 1 test test 723780 2008-04-30 12:03 obj/libc.so.12.159 Sections reported by size utility: text data bss dec hex filename 642476 18456 56968 717900 af44c obj/libc.so.12.159 ## Misc, Native Language Support (NLS), Regular Expression and Stack Smashing Protection, file size 691884 Indeed, misc object, [NLS](http://en.wikipedia.org/wiki/Internationalization_and_localization), [regular expression](http://en.wikipedia.org/wiki/Regular_expression) (IEEE Std 1003.2-1992 (“POSIX.2”) regular expressions) and [Stack Smashing Protection (SSP)](http://en.wikipedia.org/wiki/Stack-smashing_protection) support library are easily removed from the build process with **MKMISC=no MKNLS=no MKREGEX=no MKSSP=no** and an obvious patch to lib/libc/Makefile: .include "${.CURDIR}/md/Makefile.inc" +.endif +MKMISC?= yes +.if (${MKMISC} != "no") .include "${.CURDIR}/misc/Makefile.inc" +.endif .include "${.CURDIR}/net/Makefile.inc" .include "${.CURDIR}/nameser/Makefile.inc" +MKNLS?= yes +.if (${MKNLS} != "no") .include "${.CURDIR}/nls/Makefile.inc" +.endif .if (${MACHINE_ARCH} != "alpha") && (${MACHINE_ARCH} != "sparc64") .include "${.CURDIR}/quad/Makefile.inc" .endif +MKREGEX?= yes +.if (${MKREGEX} != "no") .include "${.CURDIR}/regex/Makefile.inc" +.endif .include "${.CURDIR}/resolv/Makefile.inc" ... .include "${.CURDIR}/rpc/Makefile.inc" +.endif +MKSSP?= yes +.if (${MKSSP} != "no") .include "${.CURDIR}/ssp/Makefile.inc" +.endif .include "${.CURDIR}/stdio/Makefile.inc" .include "${.CURDIR}/stdlib/Makefile.inc" .include "${.CURDIR}/string/Makefile.inc" Boot with sbin/init still works as does bin/sh, but user friendly programs like bin/ps and bin/ls now fail due to missing symbols: # ls -l lib/libc.so.12.159 /lib/libc.so.12: Undefined PLT symbol "__fgets_chk" (symnum = 156) # ls lib/libc.so.12.159 lib/libc.so.12.159 # ps aux /lib/libc.so.12: Undefined PLT symbol "__strcat_chk" (symnum = 207) # ps ps: warning: /var/run/dev.db: /lib/libc.so.12: Undefined PLT symbol "_catopen" ) File size now: -rwxr-xr-x 1 test test 691884 2008-04-30 14:18 obj/libc.so.12.159 Segment sizes reported by size utility: text data bss dec hex filename 614238 17284 56928 688450 a8142 obj/libc.so.12.159 # Ideas While a few compiler and feature options were a tried out, a number of new ideas were found. Some of these were quickly tried out, but they resulted in the build or smoke/boot test failures. ## Compiler options ### Thumb instruction set, file size 585440 Compile to 16 bit THUMB instruction set instead of normal 32 bit * Whole user space needs to be build with **-mthumb-interwork** * CPUFLAGS build variable should contain **-mthumb** and **-mthumb-interwork** * If some files (like atomic_init_testset.c) need arm32 code due to embedded ARM assembly or other tool chain issues, the CPUFLAGS build variable can be overridden: [[Per file build options override]] * libc from matt-armv6 branch builds with -mthumb but fails to run with SIGILL: [Thumb compilation discussion on port-arm](http://mail-index.netbsd.org/port-arm/2008/04/30/msg000175.html) After a successfull compile with atomic_init_testset.o compiled with -mthumb-interwork only, the file size is: -rwxr-xr-x 1 mira mira 585440 2008-05-12 11:20 obj/libc.so.12.159 size utility reports: text data bss dec hex filename 507462 17616 56928 582006 8e176 obj/libc.so.12.159 ## Feature removals ### Database support from lib/libc/db * sbin/init and possibly others depend on Berkeley DB support * a custom init and bin/sh propably work without it ### Regular memory allocator instead of JEMALLOC * USE_JEMALLOC: (share/mk/bsd.README) If "no", disables building the "jemalloc" allocator designed for improved performance with threaded applications. * seems to require rebuild of user space, but even after that init dies with: warning: no /dev/console panic: init died (signal 0, exit 12) Stopped in pid 1.1 (init) at netbsd:cpu_Debugger+0x4: bx r14 ### Reduce resolver size in lib/libc/resolver * remove IPv6 support * remove debug features ### Use libhack * Use size optimized distrib/utils/libhack with/instead of libc # Alternatives ## Crunchgen crunchgen can be used to create statically (at compile time) linked executables, which include only those object files, which are really used by the executable. * Use crunchgen or related build infra to build a dynamic library from only those object files that are actually required by the applications # References * The **BUILDING** and **share/mk/bsd.README** files in addition to libc specific makefiles and sources contain information on the different build and feature options. * gcc and ld manual pages * [Shrinking NetBSD - deep final distribution re-linker, discussion on tech-userlevel](http://thread.gmane.org/gmane.os.netbsd.devel.embedded/268) * [Reducing libc size, discussion on tech-userlevel](http://thread.gmane.org/gmane.os.netbsd.devel.userlevel/9333)