Merge "Clean up the linker a bit, remove prelinking support."
diff --git a/libc/Android.mk b/libc/Android.mk
index 46dd8d6..fd81d7e 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -529,13 +529,13 @@
     libc_common_cflags += -DANDROID_SMP=0
 endif
 
-# Needed to access private/__dso_handle.h from
-# crtbegin_xxx.c and crtend_xxx.c
-#
-libc_crt_target_cflags += -I$(LOCAL_PATH)/private
+# crtbrand.c needs <stdint.h> and a #define for the platform SDK version.
+libc_crt_target_cflags += \
+    -I$(LOCAL_PATH)/include  \
+    -DPLATFORM_SDK_VERSION=$(PLATFORM_SDK_VERSION)
 
 ifeq ($(TARGET_ARCH),arm)
-libc_crt_target_cflags += -DCRT_LEGACY_WORKAROUND
+    libc_crt_target_cflags += -DCRT_LEGACY_WORKAROUND
 endif
 
 # Define some common includes
@@ -548,8 +548,9 @@
 
 # Needed to access private/__dso_handle.h from
 # crtbegin_xxx.S and crtend_xxx.S
-#
-libc_crt_target_cflags += -I$(LOCAL_PATH)/private -I$(LOCAL_PATH)/arch-$(TARGET_ARCH)/include
+libc_crt_target_cflags += \
+    -I$(LOCAL_PATH)/private \
+    -I$(LOCAL_PATH)/arch-$(TARGET_ARCH)/include
 
 # Define the libc run-time (crt) support object files that must be built,
 # which are needed to build all other objects (shared/static libs and
@@ -577,6 +578,22 @@
     libc_crt_target_crtstart_file := $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin.S
     libc_crt_target_crtstart_so_file := $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_so.S
 endif
+
+# See the comment in crtbrand.c for the reason why we need to generate
+# crtbrand.s before generating crtbrand.o.
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.s
+$(GEN): $(LOCAL_PATH)/bionic/crtbrand.c
+	@mkdir -p $(dir $@)
+	$(TARGET_CC) $(libc_crt_target_so_cflags) -S -o $@ $<
+	sed -i -e '/\.note\.ABI-tag/s/progbits/note/' $@
+ALL_GENERATED_SOURCES += $(GEN)
+
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
+$(GEN): $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.s
+	@mkdir -p $(dir $@)
+	$(TARGET_CC) $(libc_crt_target_so_cflags) -o $@ -c $<
+ALL_GENERATED_SOURCES += $(GEN)
+
 GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_so.o
 $(GEN): $(libc_crt_target_crtstart_so_file)
 	@mkdir -p $(dir $@)
@@ -603,19 +620,30 @@
 endif # TARGET_ARCH == x86 || TARGET_ARCH == arm
 
 
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static1.o
+$(GEN): $(libc_crt_target_crtstart_file)
+	@mkdir -p $(dir $@)
+	$(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $<
+ALL_GENERATED_SOURCES += $(GEN)
+
 GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static.o
+$(GEN): $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_static1.o $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
+	@mkdir -p $(dir $@)
+	$(TARGET_LD) -r -o $@ $^
+ALL_GENERATED_SOURCES += $(GEN)
+
+GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic1.o
 $(GEN): $(libc_crt_target_crtstart_file)
 	@mkdir -p $(dir $@)
 	$(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $<
 ALL_GENERATED_SOURCES += $(GEN)
 
 GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic.o
-$(GEN): $(libc_crt_target_crtstart_file)
+$(GEN): $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbegin_dynamic1.o $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtbrand.o
 	@mkdir -p $(dir $@)
-	$(TARGET_CC) $(libc_crt_target_cflags) -o $@ -c $<
+	$(TARGET_LD) -r -o $@ $^
 ALL_GENERATED_SOURCES += $(GEN)
 
-
 # We rename crtend.o to crtend_android.o to avoid a
 # name clash between gcc and bionic.
 GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_android.o
diff --git a/libc/NOTICE b/libc/NOTICE
index d9e6818..e992c18 100644
--- a/libc/NOTICE
+++ b/libc/NOTICE
@@ -380,3 +380,27 @@
 SUCH DAMAGE.
 
 
+Copyright 2000 David E. O'Brien, John D. Polstra.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
diff --git a/libc/arch-x86/include/machine/_types.h b/libc/arch-x86/include/machine/_types.h
index 65892a1..d3d9eeb 100644
--- a/libc/arch-x86/include/machine/_types.h
+++ b/libc/arch-x86/include/machine/_types.h
@@ -58,8 +58,6 @@
 #  endif
 #endif
 
-#include <linux/types.h>
-
 /* 7.18.1.1 Exact-width integer types */
 typedef	__signed char		__int8_t;
 typedef	unsigned char		__uint8_t;
diff --git a/libc/bionic/crtbrand.c b/libc/bionic/crtbrand.c
new file mode 100644
index 0000000..31fcbc7
--- /dev/null
+++ b/libc/bionic/crtbrand.c
@@ -0,0 +1,63 @@
+/*-
+ * Copyright 2000 David E. O'Brien, John D. Polstra.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdint.h>
+
+#define ABI_VENDOR	"Android"
+#define ABI_SECTION	".note.android.ident"
+#define ABI_NOTETYPE	1
+#define ABI_ANDROID_API	PLATFORM_SDK_VERSION
+
+/*
+ * Special ".note" entry to tag an Android binary and specify the ABI version.
+ *
+ * For all arches except sparc, gcc emits the section directive for the
+ * following struct with a PROGBITS type.  However, the section should be
+ * of NOTE type, according to the Generic SysV ABI spec.
+ *
+ * Unfortunately, there is no clean way to tell gcc to use another section type,
+ * so this C file (or the C file that includes it) must be compiled in multiple
+ * steps:
+ *
+ * - Compile the .c file to a .s file.
+ * - Edit the .s file to change the 'progbits' type to 'note', for the section
+ *   directive that defines the .note.ABI-tag section.
+ * - Compile the .s file to an object file.
+ *
+ * These steps are done in the invididual Makefiles for each applicable arch.
+ */
+static const struct {
+    int32_t	namesz;
+    int32_t	descsz;
+    int32_t	type;
+    char	name[sizeof ABI_VENDOR];
+    int32_t	android_api;
+} abitag __attribute__ ((section (ABI_SECTION), aligned(4), used)) = {
+    sizeof ABI_VENDOR,
+    sizeof(int32_t),
+    ABI_NOTETYPE,
+    ABI_VENDOR,
+    ABI_ANDROID_API,
+};
diff --git a/libc/include/string.h b/libc/include/string.h
index 8e472e7..d1dc4c4 100644
--- a/libc/include/string.h
+++ b/libc/include/string.h
@@ -147,6 +147,7 @@
     return __builtin___memset_chk(s, c, n, __builtin_object_size (s, 0));
 }
 
+#if !defined(__clang__)
 extern size_t __strlcpy_real(char *, const char *, size_t)
     __asm__(__USER_LABEL_PREFIX__ "strlcpy");
 extern void __strlcpy_error()
@@ -223,6 +224,7 @@
 
     return __strlen_chk(s, bos);
 }
+#endif  /* !defined(__clang__) */
 
 
 #endif /* defined(__BIONIC_FORTIFY_INLINE) */
diff --git a/libc/include/sys/cdefs.h b/libc/include/sys/cdefs.h
index f088e33..987a5e3 100644
--- a/libc/include/sys/cdefs.h
+++ b/libc/include/sys/cdefs.h
@@ -502,18 +502,11 @@
 #include <android/api-level.h>
 
 #if defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0 && defined(__OPTIMIZE__) && __OPTIMIZE__ > 0
-#if defined(__clang__)
-#define __BIONIC_FORTIFY_INLINE \
-    extern inline \
-    __attribute__ ((always_inline)) \
-    __attribute__ ((artificial))
-#else
 #define __BIONIC_FORTIFY_INLINE \
     extern inline \
     __attribute__ ((always_inline)) \
     __attribute__ ((gnu_inline)) \
     __attribute__ ((artificial))
-#endif
 #define __BIONIC_FORTIFY_UNKNOWN_SIZE ((size_t) -1)
 #endif