From 91570ce987ef93f9ba2fa663a5fee1bd2525a2ba Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 10 Jul 2014 12:34:23 -0700 Subject: [PATCH] Slim down static binaries by avoiding stdio. It's okay for a program to choose to drag in stdio, but it's unfortunate if even the minimal "int main() { return 42; }" drags in stdio... This brings the minimal static binary on ARM down from 78KiB to 46KiB. Given that we don't have a separate -lpthread it's not obvious to me that we can shave this down any further. I'm not sure whether this is a worthwhile change for that reason. (And the fact that dynamic binaries, the usual case, are unaffected either way.) Change-Id: I02f91dcff37d14354314a30b72fed2563f431c88 --- libc/bionic/dlmalloc.h | 2 ++ libc/bionic/jemalloc_wrapper.cpp | 2 +- libc/bionic/malloc_debug_check.cpp | 4 ++-- libc/bionic/malloc_debug_leak.cpp | 8 ++++---- libc/bionic/malloc_debug_qemu.cpp | 4 ++-- libc/bionic/strerror_r.cpp | 5 +++-- libc/bionic/stubs.cpp | 3 ++- libc/private/libc_logging.h | 2 +- libc/stdlib/atexit.c | 5 ++--- 9 files changed, 19 insertions(+), 16 deletions(-) diff --git a/libc/bionic/dlmalloc.h b/libc/bionic/dlmalloc.h index e0656877f..46efa911f 100644 --- a/libc/bionic/dlmalloc.h +++ b/libc/bionic/dlmalloc.h @@ -32,6 +32,8 @@ #define USE_SPIN_LOCKS 0 #define DEFAULT_MMAP_THRESHOLD (64U * 1024U) +#define malloc_getpagesize getpagesize() + /* Export two symbols used by the VM. */ __BEGIN_DECLS int dlmalloc_trim(size_t) __LIBC_ABI_PUBLIC__; diff --git a/libc/bionic/jemalloc_wrapper.cpp b/libc/bionic/jemalloc_wrapper.cpp index d1fe96056..e33d560a4 100644 --- a/libc/bionic/jemalloc_wrapper.cpp +++ b/libc/bionic/jemalloc_wrapper.cpp @@ -21,7 +21,7 @@ #include "private/bionic_macros.h" void* je_pvalloc(size_t bytes) { - size_t pagesize = sysconf(_SC_PAGESIZE); + size_t pagesize = getpagesize(); size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { return NULL; diff --git a/libc/bionic/malloc_debug_check.cpp b/libc/bionic/malloc_debug_check.cpp index 1b6f8ea9d..1c63d4dcf 100644 --- a/libc/bionic/malloc_debug_check.cpp +++ b/libc/bionic/malloc_debug_check.cpp @@ -539,7 +539,7 @@ extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) } extern "C" void* chk_pvalloc(size_t bytes) { - size_t pagesize = sysconf(_SC_PAGESIZE); + size_t pagesize = getpagesize(); size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { // Overflow return NULL; @@ -548,7 +548,7 @@ extern "C" void* chk_pvalloc(size_t bytes) { } extern "C" void* chk_valloc(size_t size) { - return chk_memalign(sysconf(_SC_PAGESIZE), size); + return chk_memalign(getpagesize(), size); } static void ReportMemoryLeaks() { diff --git a/libc/bionic/malloc_debug_leak.cpp b/libc/bionic/malloc_debug_leak.cpp index bc88d2397..d9824f09f 100644 --- a/libc/bionic/malloc_debug_leak.cpp +++ b/libc/bionic/malloc_debug_leak.cpp @@ -268,7 +268,7 @@ extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) } extern "C" void* fill_pvalloc(size_t bytes) { - size_t pagesize = sysconf(_SC_PAGESIZE); + size_t pagesize = getpagesize(); size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { // Overflow return NULL; @@ -277,7 +277,7 @@ extern "C" void* fill_pvalloc(size_t bytes) { } extern "C" void* fill_valloc(size_t size) { - return fill_memalign(sysconf(_SC_PAGESIZE), size); + return fill_memalign(getpagesize(), size); } // ============================================================================= @@ -477,7 +477,7 @@ extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) } extern "C" void* leak_pvalloc(size_t bytes) { - size_t pagesize = sysconf(_SC_PAGESIZE); + size_t pagesize = getpagesize(); size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { // Overflow return NULL; @@ -486,5 +486,5 @@ extern "C" void* leak_pvalloc(size_t bytes) { } extern "C" void* leak_valloc(size_t size) { - return leak_memalign(sysconf(_SC_PAGESIZE), size); + return leak_memalign(getpagesize(), size); } diff --git a/libc/bionic/malloc_debug_qemu.cpp b/libc/bionic/malloc_debug_qemu.cpp index d0069e1d1..b3b604d86 100644 --- a/libc/bionic/malloc_debug_qemu.cpp +++ b/libc/bionic/malloc_debug_qemu.cpp @@ -1053,7 +1053,7 @@ extern "C" int qemu_instrumented_posix_memalign(void** memptr, size_t alignment, } extern "C" void* qemu_instrumented_pvalloc(size_t bytes) { - size_t pagesize = sysconf(_SC_PAGESIZE); + size_t pagesize = getpagesize(); size_t size = BIONIC_ALIGN(bytes, pagesize); if (size < bytes) { // Overflow qemu_error_log(" pvalloc(%zu): overflow (%zu).", @@ -1064,5 +1064,5 @@ extern "C" void* qemu_instrumented_pvalloc(size_t bytes) { } extern "C" void* qemu_instrumented_valloc(size_t size) { - return qemu_instrumented_memalign(sysconf(_SC_PAGESIZE), size); + return qemu_instrumented_memalign(getpagesize(), size); } diff --git a/libc/bionic/strerror_r.cpp b/libc/bionic/strerror_r.cpp index 5f2d36214..1e57cc0b3 100644 --- a/libc/bionic/strerror_r.cpp +++ b/libc/bionic/strerror_r.cpp @@ -8,6 +8,7 @@ #include #include "private/ErrnoRestorer.h" +#include "private/libc_logging.h" struct Pair { int code; @@ -49,9 +50,9 @@ int strerror_r(int error_number, char* buf, size_t buf_len) { const char* error_name = __strerror_lookup(error_number); if (error_name != NULL) { - length = snprintf(buf, buf_len, "%s", error_name); + length = strlcpy(buf, error_name, buf_len); } else { - length = snprintf(buf, buf_len, "Unknown error %d", error_number); + length = __libc_format_buffer(buf, buf_len, "Unknown error %d", error_number); } if (length >= buf_len) { errno_restorer.override(ERANGE); diff --git a/libc/bionic/stubs.cpp b/libc/bionic/stubs.cpp index 9b025df7c..0937e9c06 100644 --- a/libc/bionic/stubs.cpp +++ b/libc/bionic/stubs.cpp @@ -469,5 +469,6 @@ void endusershell() { // Portable code should use sysconf(_SC_PAGE_SIZE) directly instead. int getpagesize() { - return sysconf(_SC_PAGE_SIZE); + // We dont use sysconf(3) here because that drags in stdio, which makes static binaries fat. + return PAGE_SIZE; } diff --git a/libc/private/libc_logging.h b/libc/private/libc_logging.h index 7dd97a493..35c756bb9 100644 --- a/libc/private/libc_logging.h +++ b/libc/private/libc_logging.h @@ -88,7 +88,7 @@ __LIBC_HIDDEN__ void __libc_fatal_no_abort(const char* format, ...) // // Formatting routines for the C library's internal debugging. -// Unlike the usual alternatives, these don't allocate. +// Unlike the usual alternatives, these don't allocate, and they don't drag in all of stdio. // __LIBC_HIDDEN__ int __libc_format_buffer(char* buffer, size_t buffer_size, const char* format, ...) diff --git a/libc/stdlib/atexit.c b/libc/stdlib/atexit.c index 05f2faa52..e10238b31 100644 --- a/libc/stdlib/atexit.c +++ b/libc/stdlib/atexit.c @@ -103,7 +103,7 @@ __cxa_atexit(void (*func)(void *), void *arg, void *dso) { struct atexit *p = __atexit; struct atexit_fn *fnp; - size_t pgsize = sysconf(_SC_PAGESIZE); + size_t pgsize = getpagesize(); int ret = -1; if (pgsize < sizeof(*p)) @@ -219,7 +219,7 @@ void __atexit_register_cleanup(void (*func)(void)) { struct atexit *p; - size_t pgsize = sysconf(_SC_PAGESIZE); + size_t pgsize = getpagesize(); if (pgsize < sizeof(*p)) return; @@ -248,4 +248,3 @@ __atexit_register_cleanup(void (*func)(void)) unlock: _ATEXIT_UNLOCK(); } -