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
This commit is contained in:
Elliott Hughes 2014-07-10 12:34:23 -07:00
parent f0f8cd1ff3
commit 91570ce987
9 changed files with 19 additions and 16 deletions

View file

@ -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__;

View file

@ -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;

View file

@ -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() {

View file

@ -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);
}

View file

@ -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("<libc_pid=%03u, pid=%03u> 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);
}

View file

@ -8,6 +8,7 @@
#include <string.h>
#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);

View file

@ -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;
}

View file

@ -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, ...)

View file

@ -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();
}