Index: memlog.cpp
===================================================================
--- memlog.cpp	(revision 6444e037b8e7d36df974ee031a460059f5b63568)
+++ memlog.cpp	(revision ed8b809b5146578a127242eb57b9f7f10008a55c)
@@ -54,14 +54,4 @@
 void *initial_brk = 0;
 
-#ifdef __PIC__
-typedef int (*__real_posix_memalign_t)(void **memptr, size_t alignment,
-                                       size_t size);
-__real_posix_memalign_t __real_posix_memalign = 0;
-#else
-extern "C" {
-extern int __real_posix_memalign(void **memptr, size_t alignment, size_t size);
-}
-#endif
-
 __attribute__((__constructor__))
 static void record_init() {
@@ -255,4 +245,24 @@
 }
 
+#ifdef __PIC__
+int (*__real_posix_memalign)(void **memptr, size_t alignment, size_t size) = 0;
+
+void *(*__real_mmap)(void *addr, size_t length, int prot, int flags,
+                     int fd, off_t offset) = 0;
+void *(*__real_mmap64)(void *addr, size_t length, int prot, int flags,
+                       int fd, off64_t offset) = 0;
+int (*__real_munmap)(void *addr, size_t length) = 0;
+#else
+extern "C" {
+extern int __real_posix_memalign(void **memptr, size_t alignment, size_t size);
+
+extern void *__real_mmap(void *addr, size_t length, int prot, int flags,
+                         int fd, off_t offset);
+extern void *__real_mmap64(void *addr, size_t length, int prot, int flags,
+                           int fd, off64_t offset);
+extern int __real_munmap(void *addr, size_t length);
+}
+#endif
+
 // glibc exports its underlying malloc implementation under the name
 // __libc_malloc so that hooks like this can use it.
@@ -265,10 +275,4 @@
 extern void __libc_free(void *ptr);
 
-extern void *__mmap(void *addr, size_t length, int prot, int flags,
-                    int fd, off_t offset);
-extern void *__mmap64(void *addr, size_t length, int prot, int flags,
-                      int fd, off64_t offset);
-extern int __munmap(void *addr, size_t length);
-
 #ifdef __PIC__
 #define FUNC(x) x
@@ -392,7 +396,8 @@
 #ifdef __PIC__
   if (!__real_posix_memalign)
-    if (!(__real_posix_memalign =
-        (__real_posix_memalign_t) dlsym(RTLD_NEXT, "posix_memalign")))
-      return -1;
+    if (!(*(void **) (&__real_posix_memalign) =
+        dlsym(RTLD_NEXT, "posix_memalign"))) {
+      return ELIBACC;
+    }
 #endif
 
@@ -417,10 +422,18 @@
     __builtin_extract_return_addr(__builtin_return_address(0));
 
-  if (in_malloc)
-    return __mmap(addr, length, prot, flags, fd, offset);
-
-  in_malloc = 1;
-
-  void *ptr = __mmap(addr, length, prot, flags, fd, offset);
+#ifdef __PIC__
+  if (!__real_mmap)
+    if (!(*(void **) (&__real_mmap) = dlsym(RTLD_NEXT, "mmap"))) {
+      errno = ELIBACC;
+      return MAP_FAILED;
+    }
+#endif
+
+  if (in_malloc)
+    return __real_mmap(addr, length, prot, flags, fd, offset);
+
+  in_malloc = 1;
+
+  void *ptr = __real_mmap(addr, length, prot, flags, fd, offset);
 
   if (ptr != MAP_FAILED)
@@ -437,10 +450,18 @@
     __builtin_extract_return_addr(__builtin_return_address(0));
 
-  if (in_malloc)
-    return __mmap64(addr, length, prot, flags, fd, offset);
-
-  in_malloc = 1;
-
-  void *ptr = __mmap64(addr, length, prot, flags, fd, offset);
+#ifdef __PIC__
+  if (!__real_mmap64)
+    if (!(*(void **) (&__real_mmap64) = dlsym(RTLD_NEXT, "mmap64"))) {
+      errno = ELIBACC;
+      return MAP_FAILED;
+    }
+#endif
+
+  if (in_malloc)
+    return __real_mmap64(addr, length, prot, flags, fd, offset);
+
+  in_malloc = 1;
+
+  void *ptr = __real_mmap64(addr, length, prot, flags, fd, offset);
 
   if (ptr != MAP_FAILED)
@@ -456,6 +477,14 @@
     __builtin_extract_return_addr(__builtin_return_address(0));
 
-  if (in_malloc)
-    return __munmap(addr, length);
+#ifdef __PIC__
+  if (!__real_munmap)
+    if (!(*(void **) (&__real_munmap) = dlsym(RTLD_NEXT, "munmap"))) {
+      errno = ELIBACC;
+      return -1;
+    }
+#endif
+
+  if (in_malloc)
+    return __real_munmap(addr, length);
 
   in_malloc = 1;
@@ -463,5 +492,5 @@
   record_free(addr, caller);
 
-  int r = __munmap(addr, length);
+  int r = __real_munmap(addr, length);
 
   in_malloc = 0;
