Merge "Add method to run fortified versions of funcs."
diff --git a/Android.bp b/Android.bp
deleted file mode 100644
index b44c296..0000000
--- a/Android.bp
+++ /dev/null
@@ -1 +0,0 @@
-subdirs = ["*"]
diff --git a/Android.mk b/Android.mk
index 7c39751..9f0f0c3 100644
--- a/Android.mk
+++ b/Android.mk
@@ -16,4 +16,5 @@
 
 LOCAL_PATH := $(call my-dir)
 
-include $(call all-subdir-makefiles)
+include $(call all-makefiles-under,$(LOCAL_PATH)) \
+	$(call all-makefiles-under,$(LOCAL_PATH)/libc)
diff --git a/libc/Android.bp b/libc/Android.bp
index dd9bb6e..c7f18b9 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -137,18 +137,47 @@
 // libc_stack_protector.a - stack protector code
 // ========================================================
 //
-// The stack protector code needs to be compiled
-// with -fno-stack-protector, since it modifies the
-// stack canary.
+// Code that implements the stack protector (or that runs
+// before TLS has been set up) needs to be compiled with
+// -fno-stack-protector, since it accesses the stack canary
+// TLS slot.
 
 cc_library_static {
 
-    srcs: ["bionic/__stack_chk_fail.cpp"],
+    srcs: [
+        "bionic/__libc_init_main_thread.cpp",
+        "bionic/__stack_chk_fail.cpp",
+    ],
+    arch: {
+        arm64: {
+            srcs: ["arch-arm64/bionic/__set_tls.c"],
+        },
+        x86: {
+            srcs: ["arch-x86/bionic/__set_tls.c"],
+        },
+        x86_64: {
+            srcs: ["arch-x86_64/bionic/__set_tls.c"],
+        },
+    },
+
     defaults: ["libc_defaults"],
     cflags: ["-fno-stack-protector"],
     name: "libc_stack_protector",
 }
 
+// libc_init_static.cpp also needs to be built without stack protector,
+// because it's responsible for setting up TLS for static executables.
+// This isn't the case for dynamic executables because the dynamic linker
+// has already set up the main thread's TLS.
+
+cc_library_static {
+    name: "libc_init_static",
+    defaults: ["libc_defaults"],
+    srcs: ["bionic/libc_init_static.cpp"],
+    cflags: ["-fno-stack-protector"],
+}
+
+
 // ========================================================
 // libc_tzcode.a - upstream 'tzcode' code
 // ========================================================
@@ -1084,7 +1113,6 @@
                 "arch-arm64/bionic/__bionic_clone.S",
                 "arch-arm64/bionic/_exit_with_stack_teardown.S",
                 "arch-arm64/bionic/setjmp.S",
-                "arch-arm64/bionic/__set_tls.c",
                 "arch-arm64/bionic/syscall.S",
                 "arch-arm64/bionic/vfork.S",
 
@@ -1168,7 +1196,6 @@
                 "arch-x86/bionic/libgcc_compat.c",
                 "arch-x86/bionic/__restore.S",
                 "arch-x86/bionic/setjmp.S",
-                "arch-x86/bionic/__set_tls.c",
                 "arch-x86/bionic/syscall.S",
                 "arch-x86/bionic/vfork.S",
 
@@ -1209,7 +1236,9 @@
                     "arch-x86/atom/string/sse2-strlen-atom.S",
                     "arch-x86/atom/string/ssse3-bcopy-atom.S",
                     "arch-x86/atom/string/ssse3-memcmp-atom.S",
+                    "arch-x86/atom/string/ssse3-memcpy-atom.S",
                     "arch-x86/atom/string/ssse3-memmove-atom.S",
+                    "arch-x86/atom/string/ssse3-strcpy-atom.S",
                     "arch-x86/atom/string/ssse3-strncpy-atom.S",
                     "arch-x86/atom/string/ssse3-wmemcmp-atom.S",
                 ],
@@ -1258,7 +1287,6 @@
                 "arch-x86_64/bionic/_exit_with_stack_teardown.S",
                 "arch-x86_64/bionic/__restore_rt.S",
                 "arch-x86_64/bionic/setjmp.S",
-                "arch-x86_64/bionic/__set_tls.c",
                 "arch-x86_64/bionic/syscall.S",
                 "arch-x86_64/bionic/vfork.S",
 
@@ -1670,7 +1698,6 @@
     defaults: ["libc_defaults"],
     srcs: [
         "bionic/dl_iterate_phdr_static.cpp",
-        "bionic/libc_init_static.cpp",
     ],
 
     arch: {
@@ -1683,7 +1710,10 @@
 
     name: "libc_nomalloc",
 
-    whole_static_libs: ["libc_common"],
+    whole_static_libs: [
+        "libc_common",
+        "libc_init_static",
+    ],
 }
 
 // ========================================================
@@ -1720,9 +1750,9 @@
     static: {
         srcs: [
             "bionic/dl_iterate_phdr_static.cpp",
-            "bionic/libc_init_static.cpp",
         ],
         cflags: ["-DLIBC_STATIC"],
+        whole_static_libs: ["libc_init_static"],
     },
     shared: {
         srcs: [
diff --git a/libc/Android.mk b/libc/Android.mk
index 753e946..39c8c9e 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -1629,6 +1629,3 @@
 LOCAL_NATIVE_COVERAGE := $(bionic_coverage)
 include $(BUILD_STATIC_LIBRARY)
 
-
-# ========================================================
-include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/libc/libc.arm.brillo.map b/libc/libc.arm.brillo.map
index 4ebdc45..c51b7e1 100644
--- a/libc/libc.arm.brillo.map
+++ b/libc/libc.arm.brillo.map
@@ -187,21 +187,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
-    __system_properties_init; # arm x86 mips
-    __system_property_add; # arm x86 mips
-    __system_property_area__; # arm x86 mips
-    __system_property_area_init; # arm x86 mips
-    __system_property_area_serial; # arm x86 mips
-    __system_property_find; # arm x86 mips
-    __system_property_find_nth; # arm x86 mips
-    __system_property_foreach; # arm x86 mips
-    __system_property_get; # arm x86 mips
-    __system_property_read; # arm x86 mips
-    __system_property_serial; # arm x86 mips
-    __system_property_set; # arm x86 mips
-    __system_property_set_filename; # arm x86 mips
-    __system_property_update; # arm x86 mips
-    __system_property_wait_any; # arm x86 mips
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __timer_create; # arm x86 mips
     __timer_delete; # arm x86 mips
     __timer_getoverrun; # arm x86 mips
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index a2fc481..6297fd1 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -188,21 +188,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
-    __system_properties_init; # arm x86 mips
-    __system_property_add; # arm x86 mips
-    __system_property_area__; # arm x86 mips
-    __system_property_area_init; # arm x86 mips
-    __system_property_area_serial; # arm x86 mips
-    __system_property_find; # arm x86 mips
-    __system_property_find_nth; # arm x86 mips
-    __system_property_foreach; # arm x86 mips
-    __system_property_get; # arm x86 mips
-    __system_property_read; # arm x86 mips
-    __system_property_serial; # arm x86 mips
-    __system_property_set; # arm x86 mips
-    __system_property_set_filename; # arm x86 mips
-    __system_property_update; # arm x86 mips
-    __system_property_wait_any; # arm x86 mips
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __timer_create; # arm x86 mips
     __timer_delete; # arm x86 mips
     __timer_getoverrun; # arm x86 mips
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index dcf1e8f..fea1759 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -131,6 +131,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __umask_chk;
     __vsnprintf_chk;
     __vsprintf_chk;
@@ -1165,21 +1180,6 @@
 
 LIBC_PRIVATE {
   global:
-    __system_properties_init; # arm64 x86_64 mips64
-    __system_property_add; # arm64 x86_64 mips64
-    __system_property_area__; # arm64 x86_64 mips64
-    __system_property_area_init; # arm64 x86_64 mips64
-    __system_property_area_serial; # arm64 x86_64 mips64
-    __system_property_find; # arm64 x86_64 mips64
-    __system_property_find_nth; # arm64 x86_64 mips64
-    __system_property_foreach; # arm64 x86_64 mips64
-    __system_property_get; # arm64 x86_64 mips64
-    __system_property_read; # arm64 x86_64 mips64
-    __system_property_serial; # arm64 x86_64 mips64
-    __system_property_set; # arm64 x86_64 mips64
-    __system_property_set_filename; # arm64 x86_64 mips64
-    __system_property_update; # arm64 x86_64 mips64
-    __system_property_wait_any; # arm64 x86_64 mips64
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index 8306463..258ff48 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -190,21 +190,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
-    __system_properties_init; # arm x86 mips
-    __system_property_add; # arm x86 mips
-    __system_property_area__; # arm x86 mips
-    __system_property_area_init; # arm x86 mips
-    __system_property_area_serial; # arm x86 mips
-    __system_property_find; # arm x86 mips
-    __system_property_find_nth; # arm x86 mips
-    __system_property_foreach; # arm x86 mips
-    __system_property_get; # arm x86 mips
-    __system_property_read; # arm x86 mips
-    __system_property_serial; # arm x86 mips
-    __system_property_set; # arm x86 mips
-    __system_property_set_filename; # arm x86 mips
-    __system_property_update; # arm x86 mips
-    __system_property_wait_any; # arm x86 mips
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __timer_create; # arm x86 mips
     __timer_delete; # arm x86 mips
     __timer_getoverrun; # arm x86 mips
@@ -1440,21 +1440,6 @@
     __swbuf; # arm x86 mips
     __swrite; # arm x86 mips
     __swsetup; # arm x86 mips
-    __system_properties_init; # arm64 x86_64 mips64
-    __system_property_add; # arm64 x86_64 mips64
-    __system_property_area__; # arm64 x86_64 mips64
-    __system_property_area_init; # arm64 x86_64 mips64
-    __system_property_area_serial; # arm64 x86_64 mips64
-    __system_property_find; # arm64 x86_64 mips64
-    __system_property_find_nth; # arm64 x86_64 mips64
-    __system_property_foreach; # arm64 x86_64 mips64
-    __system_property_get; # arm64 x86_64 mips64
-    __system_property_read; # arm64 x86_64 mips64
-    __system_property_serial; # arm64 x86_64 mips64
-    __system_property_set; # arm64 x86_64 mips64
-    __system_property_set_filename; # arm64 x86_64 mips64
-    __system_property_update; # arm64 x86_64 mips64
-    __system_property_wait_any; # arm64 x86_64 mips64
     __truncdfsf2; # arm
     __udivdi3; # arm mips
     __udivsi3; # arm
diff --git a/libc/libc.mips.brillo.map b/libc/libc.mips.brillo.map
index e3b5a5e..67e1c05 100644
--- a/libc/libc.mips.brillo.map
+++ b/libc/libc.mips.brillo.map
@@ -185,21 +185,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
-    __system_properties_init; # arm x86 mips
-    __system_property_add; # arm x86 mips
-    __system_property_area__; # arm x86 mips
-    __system_property_area_init; # arm x86 mips
-    __system_property_area_serial; # arm x86 mips
-    __system_property_find; # arm x86 mips
-    __system_property_find_nth; # arm x86 mips
-    __system_property_foreach; # arm x86 mips
-    __system_property_get; # arm x86 mips
-    __system_property_read; # arm x86 mips
-    __system_property_serial; # arm x86 mips
-    __system_property_set; # arm x86 mips
-    __system_property_set_filename; # arm x86 mips
-    __system_property_update; # arm x86 mips
-    __system_property_wait_any; # arm x86 mips
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __timer_create; # arm x86 mips
     __timer_delete; # arm x86 mips
     __timer_getoverrun; # arm x86 mips
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 4d2f826..b82ef0f 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -186,21 +186,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
-    __system_properties_init; # arm x86 mips
-    __system_property_add; # arm x86 mips
-    __system_property_area__; # arm x86 mips
-    __system_property_area_init; # arm x86 mips
-    __system_property_area_serial; # arm x86 mips
-    __system_property_find; # arm x86 mips
-    __system_property_find_nth; # arm x86 mips
-    __system_property_foreach; # arm x86 mips
-    __system_property_get; # arm x86 mips
-    __system_property_read; # arm x86 mips
-    __system_property_serial; # arm x86 mips
-    __system_property_set; # arm x86 mips
-    __system_property_set_filename; # arm x86 mips
-    __system_property_update; # arm x86 mips
-    __system_property_wait_any; # arm x86 mips
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __timer_create; # arm x86 mips
     __timer_delete; # arm x86 mips
     __timer_getoverrun; # arm x86 mips
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index dcf1e8f..fea1759 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -131,6 +131,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __umask_chk;
     __vsnprintf_chk;
     __vsprintf_chk;
@@ -1165,21 +1180,6 @@
 
 LIBC_PRIVATE {
   global:
-    __system_properties_init; # arm64 x86_64 mips64
-    __system_property_add; # arm64 x86_64 mips64
-    __system_property_area__; # arm64 x86_64 mips64
-    __system_property_area_init; # arm64 x86_64 mips64
-    __system_property_area_serial; # arm64 x86_64 mips64
-    __system_property_find; # arm64 x86_64 mips64
-    __system_property_find_nth; # arm64 x86_64 mips64
-    __system_property_foreach; # arm64 x86_64 mips64
-    __system_property_get; # arm64 x86_64 mips64
-    __system_property_read; # arm64 x86_64 mips64
-    __system_property_serial; # arm64 x86_64 mips64
-    __system_property_set; # arm64 x86_64 mips64
-    __system_property_set_filename; # arm64 x86_64 mips64
-    __system_property_update; # arm64 x86_64 mips64
-    __system_property_wait_any; # arm64 x86_64 mips64
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;
diff --git a/libc/libc.x86.brillo.map b/libc/libc.x86.brillo.map
index 637ad06..71390b7 100644
--- a/libc/libc.x86.brillo.map
+++ b/libc/libc.x86.brillo.map
@@ -185,21 +185,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
-    __system_properties_init; # arm x86 mips
-    __system_property_add; # arm x86 mips
-    __system_property_area__; # arm x86 mips
-    __system_property_area_init; # arm x86 mips
-    __system_property_area_serial; # arm x86 mips
-    __system_property_find; # arm x86 mips
-    __system_property_find_nth; # arm x86 mips
-    __system_property_foreach; # arm x86 mips
-    __system_property_get; # arm x86 mips
-    __system_property_read; # arm x86 mips
-    __system_property_serial; # arm x86 mips
-    __system_property_set; # arm x86 mips
-    __system_property_set_filename; # arm x86 mips
-    __system_property_update; # arm x86 mips
-    __system_property_wait_any; # arm x86 mips
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __timer_create; # arm x86 mips
     __timer_delete; # arm x86 mips
     __timer_getoverrun; # arm x86 mips
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index 76bf9ea..6905ff8 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -186,21 +186,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
-    __system_properties_init; # arm x86 mips
-    __system_property_add; # arm x86 mips
-    __system_property_area__; # arm x86 mips
-    __system_property_area_init; # arm x86 mips
-    __system_property_area_serial; # arm x86 mips
-    __system_property_find; # arm x86 mips
-    __system_property_find_nth; # arm x86 mips
-    __system_property_foreach; # arm x86 mips
-    __system_property_get; # arm x86 mips
-    __system_property_read; # arm x86 mips
-    __system_property_serial; # arm x86 mips
-    __system_property_set; # arm x86 mips
-    __system_property_set_filename; # arm x86 mips
-    __system_property_update; # arm x86 mips
-    __system_property_wait_any; # arm x86 mips
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __timer_create; # arm x86 mips
     __timer_delete; # arm x86 mips
     __timer_getoverrun; # arm x86 mips
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index dcf1e8f..fea1759 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -131,6 +131,21 @@
     __sym_ntop;
     __sym_ntos;
     __sym_ston;
+    __system_properties_init;
+    __system_property_add;
+    __system_property_area__;
+    __system_property_area_init;
+    __system_property_area_serial;
+    __system_property_find;
+    __system_property_find_nth;
+    __system_property_foreach;
+    __system_property_get;
+    __system_property_read;
+    __system_property_serial;
+    __system_property_set;
+    __system_property_set_filename;
+    __system_property_update;
+    __system_property_wait_any;
     __umask_chk;
     __vsnprintf_chk;
     __vsprintf_chk;
@@ -1165,21 +1180,6 @@
 
 LIBC_PRIVATE {
   global:
-    __system_properties_init; # arm64 x86_64 mips64
-    __system_property_add; # arm64 x86_64 mips64
-    __system_property_area__; # arm64 x86_64 mips64
-    __system_property_area_init; # arm64 x86_64 mips64
-    __system_property_area_serial; # arm64 x86_64 mips64
-    __system_property_find; # arm64 x86_64 mips64
-    __system_property_find_nth; # arm64 x86_64 mips64
-    __system_property_foreach; # arm64 x86_64 mips64
-    __system_property_get; # arm64 x86_64 mips64
-    __system_property_read; # arm64 x86_64 mips64
-    __system_property_serial; # arm64 x86_64 mips64
-    __system_property_set; # arm64 x86_64 mips64
-    __system_property_set_filename; # arm64 x86_64 mips64
-    __system_property_update; # arm64 x86_64 mips64
-    __system_property_wait_any; # arm64 x86_64 mips64
     free_malloc_leak_info;
     get_malloc_leak_info;
     gMallocLeakZygoteChild;