Merge "linker: Fix LD_PRELOADS for calling constructors"
diff --git a/libc/include/limits.h b/libc/include/limits.h
index 1de8ea6..d691a8f 100644
--- a/libc/include/limits.h
+++ b/libc/include/limits.h
@@ -90,6 +90,7 @@
 #endif
 
 #ifndef PAGESIZE
+#include <asm/page.h>
 #define  PAGESIZE  PAGE_SIZE
 #endif
 
diff --git a/libc/include/string.h b/libc/include/string.h
index 8730ea3..8e472e7 100644
--- a/libc/include/string.h
+++ b/libc/include/string.h
@@ -93,10 +93,6 @@
     __attribute__((__error__("memcpy called with size bigger than source")));
 extern void __memcpy_overlap_error()
     __attribute__((__error__("memcpy called with overlapping regions")));
-extern void *__memcpy_real(void *, const void *, size_t)
-    __asm__(__USER_LABEL_PREFIX__ "memcpy");
-extern void *__memcpy_chk2(void *, const void *, size_t, size_t, size_t);
-
 
 __BIONIC_FORTIFY_INLINE
 void *memcpy (void *dest, const void *src, size_t copy_amount) {
@@ -118,11 +114,7 @@
         __memcpy_overlap_error();
     }
 
-    if (__builtin_constant_p(copy_amount) && __builtin_constant_p(d - s)) {
-         return __memcpy_real(dest, src, copy_amount);
-    }
-
-    return __memcpy_chk2(dest, src, copy_amount, d_len, s_len);
+    return __builtin___memcpy_chk(dest, src, copy_amount, d_len);
 }
 
 __BIONIC_FORTIFY_INLINE
diff --git a/libc/string/__memcpy_chk.c b/libc/string/__memcpy_chk.c
index 934ed67..10334ba 100644
--- a/libc/string/__memcpy_chk.c
+++ b/libc/string/__memcpy_chk.c
@@ -32,7 +32,7 @@
 #include <private/logd.h>
 
 /*
- * Runtime implementation of __memcpy_chk2.
+ * Runtime implementation of __memcpy_chk.
  *
  * See
  *   http://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html
@@ -42,12 +42,9 @@
  * This memcpy check is called if _FORTIFY_SOURCE is defined and
  * greater than 0.
  */
-void *__memcpy_chk2(void *dest, const void *src,
-              size_t copy_amount, size_t dest_len, size_t src_len)
+void *__memcpy_chk(void *dest, const void *src,
+              size_t copy_amount, size_t dest_len)
 {
-    char *d = (char *) dest;
-    const char *s = (const char *) src;
-
     if (__builtin_expect(copy_amount > dest_len, 0)) {
         __libc_android_log_print(ANDROID_LOG_FATAL, "libc",
             "*** memcpy buffer overflow detected ***\n");
@@ -55,28 +52,5 @@
         abort();
     }
 
-    if (__builtin_expect(copy_amount > src_len, 0)) {
-        __libc_android_log_print(ANDROID_LOG_FATAL, "libc",
-            "*** memcpy read overflow detected ***\n");
-        abort();
-    }
-
-    if (__builtin_expect(((d <= s) && ((size_t)(s - d) < copy_amount))
-                      || ((d >= s) && ((size_t)(d - s) < copy_amount)), 0)) {
-        __libc_android_log_print(ANDROID_LOG_FATAL, "libc",
-            "*** memcpy memory overlap detected ***\n");
-        abort();
-    }
-
     return memcpy(dest, src, copy_amount);
 }
-
-/*
- * GCC can create references to __memcpy_chk when using
- * __builtin__memmove_chk().
- */
-void *__memcpy_chk(void *dest, const void *src,
-              size_t copy_amount, size_t dest_len)
-{
-    return __memcpy_chk2(dest, src, copy_amount, dest_len, (size_t) -1);
-}
diff --git a/linker/Android.mk b/linker/Android.mk
index a739b4f..c9d053f 100644
--- a/linker/Android.mk
+++ b/linker/Android.mk
@@ -15,7 +15,8 @@
 
 LOCAL_CFLAGS += -fno-stack-protector \
         -Wstrict-overflow=5 \
-        -fvisibility=hidden
+        -fvisibility=hidden \
+        -std=gnu99
 
 # Set LINKER_DEBUG to either 1 or 0
 #
diff --git a/linker/linker.c b/linker/linker.c
index 8d4efaf..b96e072 100644
--- a/linker/linker.c
+++ b/linker/linker.c
@@ -393,8 +393,8 @@
         switch(ELF32_ST_BIND(s->st_info)){
         case STB_GLOBAL:
         case STB_WEAK:
-                /* no section == undefined */
-            if(s->st_shndx == 0) continue;
+            if(s->st_shndx == SHN_UNDEF)
+                continue;
 
             TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
                        name, si->name, s->st_value, s->st_size);
@@ -462,7 +462,7 @@
             DEBUG("%5d %s: looking up %s in %s\n",
                   pid, si->name, name, lsi->name);
             s = soinfo_elf_lookup(lsi, elf_hash, name);
-            if ((s != NULL) && (s->st_shndx != SHN_UNDEF))
+            if (s != NULL)
                 goto done;
         }
     }
@@ -1640,7 +1640,7 @@
  * fixed it's own GOT. It is safe to make references to externs
  * and other non-local data at this point.
  */
-static unsigned __linker_init_post_relocation(unsigned **elfdata)
+static unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
 {
     static soinfo linker_soinfo;
 
@@ -1731,15 +1731,16 @@
     _r_debug.r_map = map;
     r_debug_tail = map;
 
-        /* gdb expects the linker to be in the debug shared object list,
-         * and we need to make sure that the reported load address is zero.
-         * Without this, gdb gets the wrong idea of where rtld_db_dlactivity()
-         * is.  Don't use soinfo_alloc(), because the linker shouldn't
+        /* gdb expects the linker to be in the debug shared object list.
+         * Without this, gdb has trouble locating the linker's ".text"
+         * and ".plt" sections. Gdb could also potentially use this to
+         * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
+         * Don't use soinfo_alloc(), because the linker shouldn't
          * be on the soinfo list.
          */
     strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
     linker_soinfo.flags = 0;
-    linker_soinfo.base = 0;     // This is the important part; must be zero.
+    linker_soinfo.base = linker_base;
     insert_soinfo_into_debug_map(&linker_soinfo);
 
         /* extract information passed from the kernel */
@@ -1935,5 +1936,5 @@
 
     // We have successfully fixed our own relocations. It's safe to run
     // the main part of the linker now.
-    return __linker_init_post_relocation(elfdata);
+    return __linker_init_post_relocation(elfdata, linker_addr);
 }