linker: Make the errors reported by dlopen/dlsym be more useful.

Changed it so that when the linker generates error messages, they are
scribbled away into a buffer that dlfcn and friends can read from.

Since the error messages are generetad with snprintf, and snprintf
MAY call malloc during some code paths, we now link against a version
of libc that does not contain malloc/free/realloc/calloc. We then define
malloc and friends in the dynamic loader, and make them abort() if they
are ever called.

Signed-off-by: Dima Zavin <dima@android.com>
diff --git a/linker/Android.mk b/linker/Android.mk
index 48141be..3d3ad92 100644
--- a/linker/Android.mk
+++ b/linker/Android.mk
@@ -37,7 +37,7 @@
 
 LOCAL_MODULE:= linker
 
-LOCAL_STATIC_LIBRARIES := libcutils libc
+LOCAL_STATIC_LIBRARIES := libcutils libc_nomalloc
 
 #LOCAL_FORCE_STATIC_EXECUTABLE := true # not necessary when not including BUILD_EXECUTABLE
 
diff --git a/linker/dlfcn.c b/linker/dlfcn.c
index cd73d11..b54674f 100644
--- a/linker/dlfcn.c
+++ b/linker/dlfcn.c
@@ -15,41 +15,49 @@
  */
 #include <dlfcn.h>
 #include <pthread.h>
+#include <stdio.h>
 #include "linker.h"
 
 /* This file hijacks the symbols stubbed out in libdl.so. */
 
 #define DL_SUCCESS                    0
-#define DL_ERR_CANNOT_FIND_LIBRARY    1
+#define DL_ERR_CANNOT_LOAD_LIBRARY    1
 #define DL_ERR_INVALID_LIBRARY_HANDLE 2
 #define DL_ERR_BAD_SYMBOL_NAME        3
 #define DL_ERR_SYMBOL_NOT_FOUND       4
 #define DL_ERR_SYMBOL_NOT_GLOBAL      5
 
+static char dl_err_buf[1024];
+static const char *dl_err_str;
+
 static const char *dl_errors[] = {
-    [DL_SUCCESS] = NULL,
-    [DL_ERR_CANNOT_FIND_LIBRARY] = "Cannot find library",
+    [DL_ERR_CANNOT_LOAD_LIBRARY] = "Cannot load library",
     [DL_ERR_INVALID_LIBRARY_HANDLE] = "Invalid library handle",
     [DL_ERR_BAD_SYMBOL_NAME] = "Invalid symbol name",
     [DL_ERR_SYMBOL_NOT_FOUND] = "Symbol not found",
     [DL_ERR_SYMBOL_NOT_GLOBAL] = "Symbol is not global",
 };
 
-static int dl_last_err = DL_SUCCESS;
-
 #define likely(expr)   __builtin_expect (expr, 1)
 #define unlikely(expr) __builtin_expect (expr, 0)
 
 static pthread_mutex_t dl_lock = PTHREAD_MUTEX_INITIALIZER;
 
-void *dlopen(const char *filename, int flag) 
+static void set_dlerror(int err)
+{
+    snprintf(dl_err_buf, sizeof(dl_err_buf), "%s: %s", dl_errors[err],
+             linker_get_error());
+    dl_err_str = (const char *)&dl_err_buf[0];
+};
+
+void *dlopen(const char *filename, int flag)
 {
     soinfo *ret;
 
     pthread_mutex_lock(&dl_lock);
     ret = find_library(filename);
     if (unlikely(ret == NULL)) {
-        dl_last_err = DL_ERR_CANNOT_FIND_LIBRARY;
+        set_dlerror(DL_ERR_CANNOT_LOAD_LIBRARY);
     } else {
         ret->refcount++;
     }
@@ -59,9 +67,9 @@
 
 const char *dlerror(void)
 {
-    const char *err = dl_errors[dl_last_err];
-    dl_last_err = DL_SUCCESS;
-    return err;
+    const char *tmp = dl_err_str;
+    dl_err_str = NULL;
+    return (const char *)tmp;
 }
 
 void *dlsym(void *handle, const char *symbol)
@@ -71,16 +79,16 @@
     unsigned bind;
 
     pthread_mutex_lock(&dl_lock);
-    
+
     if(unlikely(handle == 0)) { 
-        dl_last_err = DL_ERR_INVALID_LIBRARY_HANDLE;
+        set_dlerror(DL_ERR_INVALID_LIBRARY_HANDLE);
         goto err;
     }
     if(unlikely(symbol == 0)) {
-        dl_last_err = DL_ERR_BAD_SYMBOL_NAME;
+        set_dlerror(DL_ERR_BAD_SYMBOL_NAME);
         goto err;
     }
-    
+
     if(handle == RTLD_DEFAULT) {
         sym = lookup(symbol, &base);
     } else if(handle == RTLD_NEXT) {
@@ -92,16 +100,17 @@
 
     if(likely(sym != 0)) {
         bind = ELF32_ST_BIND(sym->st_info);
-    
+
         if(likely((bind == STB_GLOBAL) && (sym->st_shndx != 0))) {
             unsigned ret = sym->st_value + base;
             pthread_mutex_unlock(&dl_lock);
             return (void*)ret;
         }
 
-        dl_last_err = DL_ERR_SYMBOL_NOT_GLOBAL;
+        set_dlerror(DL_ERR_SYMBOL_NOT_GLOBAL);
     }
-    else dl_last_err = DL_ERR_SYMBOL_NOT_FOUND;
+    else
+        set_dlerror(DL_ERR_SYMBOL_NOT_FOUND);
 
 err:
     pthread_mutex_unlock(&dl_lock);
diff --git a/linker/linker.c b/linker/linker.c
index 5180ae0..08a591a 100644
--- a/linker/linker.c
+++ b/linker/linker.c
@@ -104,6 +104,31 @@
 #define PT_ARM_EXIDX    0x70000001      /* .ARM.exidx segment */
 #endif
 
+#define HOODLUM(name, ret, ...)                                               \
+    ret name __VA_ARGS__                                                      \
+    {                                                                         \
+        char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \
+        write(2, errstr, sizeof(errstr));                                     \
+        abort();                                                              \
+    }
+HOODLUM(malloc, void *, (size_t size));
+HOODLUM(free, void, (void *ptr));
+HOODLUM(realloc, void *, (void *ptr, size_t size));
+HOODLUM(calloc, void *, (size_t cnt, size_t size));
+
+static char __linker_dl_err_buf[768];
+#define DL_ERR(fmt, x...)                                                     \
+    do {                                                                      \
+        snprintf(__linker_dl_err_buf, sizeof(__linker_dl_err_buf),            \
+                 "%s[%d]: " fmt, __func__, __LINE__, ##x);                    \
+        ERROR(fmt, ##x);                                                      \
+    } while(0)
+
+const char *linker_get_error(void)
+{
+    return (const char *)&__linker_dl_err_buf[0];
+}
+
 /*
  * This function is an empty stub where GDB locates a breakpoint to get notified
  * about linker activity.
@@ -207,7 +232,7 @@
     soinfo *si;
 
     if(strlen(name) >= SOINFO_NAME_LEN) {
-        ERROR("%5d library name %s too long\n", pid, name);
+        DL_ERR("%5d library name %s too long\n", pid, name);
         return 0;
     }
 
@@ -216,7 +241,7 @@
     */
     if (!freelist) {
         if(socount == SO_MAX) {
-            ERROR("%5d too many libraries when loading %s\n", pid, name);
+            DL_ERR("%5d too many libraries when loading %s\n", pid, name);
             return NULL;
         }
         freelist = sopool + socount++;
@@ -252,7 +277,7 @@
     }
     if (trav == NULL) {
         /* si was not ni solist */
-        ERROR("%5d name %s is not in solist!\n", pid, si->name);
+        DL_ERR("%5d name %s is not in solist!\n", pid, si->name);
         return;
     }
 
@@ -521,7 +546,7 @@
 
     sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
     if (sz < 0) {
-        ERROR("lseek() failed!\n");
+        DL_ERR("lseek() failed!\n");
         return 0;
     }
 
@@ -597,7 +622,7 @@
 
     TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name);
     if (verify_elf_object(_hdr, name) < 0) {
-        ERROR("%5d - %s is not a valid ELF object\n", pid, name);
+        DL_ERR("%5d - %s is not a valid ELF object\n", pid, name);
         return (unsigned)-1;
     }
 
@@ -625,7 +650,7 @@
     }
 
     if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) {
-        ERROR("%5d - No loadable segments found in %s.\n", pid, name);
+        DL_ERR("%5d - No loadable segments found in %s.\n", pid, name);
         return (unsigned)-1;
     }
 
@@ -661,13 +686,13 @@
     void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC,
                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
     if (base == MAP_FAILED) {
-        ERROR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
+        DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x "
               "as requested, will try general pool: %d (%s)\n",
               pid, (si->base ? "" : "non-"), si->name, si->base,
               errno, strerror(errno));
         return -1;
     } else if (base != (void *)si->base) {
-        ERROR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
+        DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, "
               "not at 0x%08x\n", pid, (si->base ? "" : "non-"),
               si->name, (unsigned)base, si->base);
         munmap(base, si->size);
@@ -705,7 +730,7 @@
     }
 
 err:
-    ERROR("OOPS: %5d cannot map library '%s'. no vspace available.\n",
+    DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.\n",
           pid, si->name);
     return -1;
 }
@@ -764,7 +789,7 @@
                          MAP_PRIVATE | MAP_FIXED, fd,
                          phdr->p_offset & (~PAGE_MASK));
             if (pbase == MAP_FAILED) {
-                ERROR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
+                DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). "
                       "p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name,
                       (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset);
                 goto fail;
@@ -820,8 +845,8 @@
                                   MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
                                   -1, 0);
                 if (extra_base == MAP_FAILED) {
-                    ERROR("[ %5d - failed to extend segment from '%s' @ 0x%08x "
-                          "(0x%08x) ]\n", pid, si->name, (unsigned)tmp,
+                    DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x"
+                           " (0x%08x) ]\n", pid, si->name, (unsigned)tmp,
                           extra_len);
                     goto fail;
                 }
@@ -873,7 +898,7 @@
 
     /* Sanity check */
     if (total_sz > si->size) {
-        ERROR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
+        DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is "
               "greater than what was allocated (0x%08x). THIS IS BAD!\n",
               pid, total_sz, si->name, si->size);
         goto fail;
@@ -940,18 +965,20 @@
     soinfo *si = NULL;
     Elf32_Ehdr *hdr;
 
-    if(fd == -1)
+    if(fd == -1) {
+        DL_ERR("Library '%s' not found\n", name);
         return NULL;
+    }
 
     /* We have to read the ELF header to figure out what to do with this image
      */
     if (lseek(fd, 0, SEEK_SET) < 0) {
-        ERROR("lseek() failed!\n");
+        DL_ERR("lseek() failed!\n");
         goto fail;
     }
 
     if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) {
-        ERROR("read() failed!\n");
+        DL_ERR("read() failed!\n");
         goto fail;
     }
 
@@ -1042,8 +1069,8 @@
         if(!strcmp(name, si->name)) {
             if(si->flags & FLAG_ERROR) return 0;
             if(si->flags & FLAG_LINKED) return si;
-            ERROR("OOPS: %5d recursive link to '%s'\n", pid, si->name);
-            return 0;
+            DL_ERR("OOPS: %5d recursive link to '%s'\n", pid, si->name);
+            return NULL;
         }
     }
 
@@ -1074,7 +1101,7 @@
                 if(lsi)
                     unload_library(lsi);
                 else
-                    ERROR("%5d could not unload '%s'\n",
+                    DL_ERR("%5d could not unload '%s'\n",
                           pid, si->strtab + d[1]);
             }
         }
@@ -1124,19 +1151,19 @@
             sym_name = (char *)(strtab + symtab[sym].st_name);
             s = _do_lookup(si, sym_name, &base);
             if(s == 0) {
-                ERROR("%5d cannot locate '%s'...\n", pid, sym_name);
+                DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name);
                 return -1;
             }
 #if 0
             if((base == 0) && (si->base != 0)){
                     /* linking from libraries to main image is bad */
-                ERROR("%5d cannot locate '%s'...\n", 
+                DL_ERR("%5d cannot locate '%s'...\n",
                        pid, strtab + symtab[sym].st_name);
                 return -1;
             }
 #endif
             if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) {
-                ERROR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
+                DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not "
                       "handle this yet\n", pid, si->name, s->st_shndx,
                       s->st_value);
                 return -1;
@@ -1198,7 +1225,7 @@
             COUNT_RELOC(RELOC_RELATIVE);
             MARK(rel->r_offset);
             if(sym){
-                ERROR("%5d odd RELATIVE form...\n", pid);
+                DL_ERR("%5d odd RELATIVE form...\n", pid);
                 return -1;
             }
             TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
@@ -1239,7 +1266,7 @@
 #endif /* ANDROID_ARM_LINKER */
 
         default:
-            ERROR("%5d unknown reloc type %d @ %p (%d)\n",
+            DL_ERR("%5d unknown reloc type %d @ %p (%d)\n",
                   pid, type, rel, (int) (rel - start));
             return -1;
         }
@@ -1296,9 +1323,9 @@
         TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
     } else {
         if (si->preinit_array) {
-            ERROR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
-                  " This is INVALID.\n", pid, si->name,
-                  (unsigned)si->preinit_array);
+            DL_ERR("%5d Shared library '%s' has a preinit_array table @ 0x%08x."
+                   " This is INVALID.\n", pid, si->name,
+                   (unsigned)si->preinit_array);
         }
     }
 
@@ -1344,7 +1371,7 @@
 
     dev_null = open("/dev/null", O_RDWR);
     if (dev_null < 0) {
-        ERROR("Cannot open /dev/null.\n");
+        DL_ERR("Cannot open /dev/null.\n");
         return -1;
     }
     TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
@@ -1370,7 +1397,7 @@
         /* The only error we allow is that the file descriptor does not
            exist, in which case we dup /dev/null to it. */
         if (errno != EBADF) {
-            ERROR("nullify_stdio: unhandled error %s\n", strerror(errno));
+            DL_ERR("nullify_stdio: unhandled error %s\n", strerror(errno));
             return_value = -1;
             continue;
         }
@@ -1381,9 +1408,9 @@
         do {
             status = dup2(dev_null, i);
         } while (status < 0 && errno == EINTR);
-        
+
         if (status < 0) {
-            ERROR("nullify_stdio: dup2 error %s\n", strerror(errno));
+            DL_ERR("nullify_stdio: dup2 error %s\n", strerror(errno));
             return_value = -1;
             continue;
         }
@@ -1392,12 +1419,12 @@
     /* If /dev/null is not one of the stdio file descriptors, close it. */
     if (dev_null > 2) {
         TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
-	do {
+        do {
             status = close(dev_null);
         } while (status < 0 && errno == EINTR);
 
         if (status < 0) {
-            ERROR("nullify_stdio: close error %s\n", strerror(errno));
+            DL_ERR("nullify_stdio: close error %s\n", strerror(errno));
             return_value = -1;
         }
     }
@@ -1465,7 +1492,7 @@
                 }
             } else if (phdr->p_type == PT_DYNAMIC) {
                 if (si->dynamic != (unsigned *)-1) {
-                    ERROR("%5d multiple PT_DYNAMIC segments found in '%s'. "
+                    DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. "
                           "Segment at 0x%08x, previously one found at 0x%08x\n",
                           pid, si->name, si->base + phdr->p_vaddr,
                           (unsigned)si->dynamic);
@@ -1478,7 +1505,7 @@
     }
 
     if (si->dynamic == (unsigned *)-1) {
-        ERROR("%5d missing PT_DYNAMIC?!\n", pid);
+        DL_ERR("%5d missing PT_DYNAMIC?!\n", pid);
         goto fail;
     }
 
@@ -1502,7 +1529,7 @@
             break;
         case DT_PLTREL:
             if(*d != DT_REL) {
-                ERROR("DT_RELA not supported\n");
+                DL_ERR("DT_RELA not supported\n");
                 goto fail;
             }
             break;
@@ -1527,7 +1554,7 @@
             *d = (int) &_r_debug;
             break;
         case DT_RELA:
-            ERROR("%5d DT_RELA not supported\n", pid);
+            DL_ERR("%5d DT_RELA not supported\n", pid);
             goto fail;
         case DT_INIT:
             si->init_func = (void (*)(void))(si->base + *d);
@@ -1579,16 +1606,16 @@
            pid, si->base, si->strtab, si->symtab);
 
     if((si->strtab == 0) || (si->symtab == 0)) {
-        ERROR("%5d missing essential tables\n", pid);
+        DL_ERR("%5d missing essential tables\n", pid);
         goto fail;
     }
 
     for(d = si->dynamic; *d; d += 2) {
         if(d[0] == DT_NEEDED){
             DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
-            soinfo *lsi = find_library(si->strtab + d[1]);            
+            soinfo *lsi = find_library(si->strtab + d[1]);
             if(lsi == 0) {
-                ERROR("%5d could not load '%s'\n", pid, si->strtab + d[1]);
+                DL_ERR("%5d could not load '%s'\n", pid, si->strtab + d[1]);
                 goto fail;
             }
             lsi->refcount++;
@@ -1747,8 +1774,10 @@
     si->wrprotect_start = 0xffffffff;
     si->wrprotect_end = 0;
 
-    if(link_image(si, 0)){
-        ERROR("CANNOT LINK EXECUTABLE '%s'\n", argv[0]);
+    if(link_image(si, 0)) {
+        char errmsg[] = "CANNOT LINK EXECUTABLE\n";
+        write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
+        write(2, errmsg, sizeof(errmsg));
         exit(-1);
     }
 
diff --git a/linker/linker.h b/linker/linker.h
index d80c761..69042c0 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -204,6 +204,7 @@
 unsigned unload_library(soinfo *si);
 Elf32_Sym *lookup_in_library(soinfo *si, const char *name);
 Elf32_Sym *lookup(const char *name, unsigned *base);
+const char *linker_get_error(void);
 
 #ifdef ANDROID_ARM_LINKER 
 typedef long unsigned int *_Unwind_Ptr;
diff --git a/linker/linker_debug.h b/linker/linker_debug.h
index 3cc1343..3f4fc4c 100644
--- a/linker/linker_debug.h
+++ b/linker/linker_debug.h
@@ -55,17 +55,16 @@
 #define TRUE                 1
 #define FALSE                0
 
-
-#define __PRINTVF(v,f,x...)   do {                                        \
-        (debug_verbosity > (v)) && (printf(x), ((f) && fflush(stdout))); \
-    } while (0)
 /* Only use printf() during debugging.  We have seen occasional memory
  * corruption when the linker uses printf().
  */
 #if LINKER_DEBUG
 extern int debug_verbosity;
 #warning "*** LINKER IS USING printf(); DO NOT CHECK THIS IN ***"
-#define _PRINTVF(v,f,x...)    __PRINTVF(v,f,x)
+#define _PRINTVF(v,f,x...)                                                \
+    do {                                                                  \
+        (debug_verbosity > (v)) && (printf(x), ((f) && fflush(stdout)));  \
+    } while (0)
 #else /* !LINKER_DEBUG */
 #define _PRINTVF(v,f,x...)   do {} while(0)
 #endif /* LINKER_DEBUG */
@@ -75,8 +74,9 @@
 #define TRACE(x...)          _PRINTVF(1, TRUE, x)
 #define WARN(fmt,args...)    \
         _PRINTVF(-1, TRUE, "%s:%d| WARNING: " fmt, __FILE__, __LINE__, ## args)
-#define ERROR(fmt,args...)   \
-        __PRINTVF(-1, TRUE, "%s:%d| ERROR: " fmt, __FILE__, __LINE__, ## args)
+#define ERROR(fmt,args...)    \
+        _PRINTVF(-1, TRUE, "%s:%d| ERROR: " fmt, __FILE__, __LINE__, ## args)
+
 
 #if TRACE_DEBUG
 #define DEBUG(x...)          _PRINTVF(2, TRUE, "DEBUG: " x)