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
diff --git a/libc/bionic/dlmalloc.h b/libc/bionic/dlmalloc.h
index e065687..46efa91 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 d1fe960..e33d560 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 1b6f8ea..1c63d4d 100644
--- a/libc/bionic/malloc_debug_check.cpp
+++ b/libc/bionic/malloc_debug_check.cpp
@@ -539,7 +539,7 @@
 }
 
 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_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 bc88d23..d9824f0 100644
--- a/libc/bionic/malloc_debug_leak.cpp
+++ b/libc/bionic/malloc_debug_leak.cpp
@@ -268,7 +268,7 @@
 }
 
 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_valloc(size_t size) {
-  return fill_memalign(sysconf(_SC_PAGESIZE), size);
+  return fill_memalign(getpagesize(), size);
 }
 
 // =============================================================================
@@ -477,7 +477,7 @@
 }
 
 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_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 d0069e1..b3b604d 100644
--- a/libc/bionic/malloc_debug_qemu.cpp
+++ b/libc/bionic/malloc_debug_qemu.cpp
@@ -1053,7 +1053,7 @@
 }
 
 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_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 5f2d362..1e57cc0 100644
--- a/libc/bionic/strerror_r.cpp
+++ b/libc/bionic/strerror_r.cpp
@@ -8,6 +8,7 @@
 #include <string.h>
 
 #include "private/ErrnoRestorer.h"
+#include "private/libc_logging.h"
 
 struct Pair {
   int code;
@@ -49,9 +50,9 @@
 
   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 9b025df..0937e9c 100644
--- a/libc/bionic/stubs.cpp
+++ b/libc/bionic/stubs.cpp
@@ -469,5 +469,6 @@
 
 // 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 7dd97a4..35c756b 100644
--- a/libc/private/libc_logging.h
+++ b/libc/private/libc_logging.h
@@ -88,7 +88,7 @@
 
 //
 // 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 05f2faa..e10238b 100644
--- a/libc/stdlib/atexit.c
+++ b/libc/stdlib/atexit.c
@@ -103,7 +103,7 @@
 {
 	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 @@
 __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 @@
 unlock:
         _ATEXIT_UNLOCK();
 }
-