Merge "mmap: Remove madvise() workaround"
diff --git a/libc/Android.mk b/libc/Android.mk
index a64287a..dc2b331 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -211,7 +211,6 @@
 	bionic/semaphore.c \
 	bionic/send.c \
 	bionic/setegid.c \
-	bionic/__set_errno.c \
 	bionic/seteuid.c \
 	bionic/setpgrp.c \
 	bionic/setresuid.c \
@@ -283,6 +282,7 @@
     bionic/__memcpy_chk.cpp \
     bionic/__memmove_chk.cpp \
     bionic/__memset_chk.cpp \
+    bionic/__set_errno.cpp \
     bionic/setlocale.cpp \
     bionic/__strcat_chk.cpp \
     bionic/__strcpy_chk.cpp \
@@ -623,7 +623,7 @@
     libc_crt_target_so_cflags := -fPIC
 endif
 ifeq ($(TARGET_ARCH),x86)
-    libc_crtbegin_extension := S
+    libc_crtbegin_extension := c
     libc_crt_target_so_cflags := -fPIC
 endif
 ifeq ($(libc_crtbegin_extension),)
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index 58e0ac1..2973caa 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -62,7 +62,7 @@
 int     getgroups:getgroups(int, gid_t *)      -1,-1,80
 pid_t   getpgid(pid_t)             132
 pid_t   getppid()                  64
-pid_t   getsid(pid_t)              147
+pid_t   getsid(pid_t)              147,147,151
 pid_t   setsid()                   66
 int     setgid:setgid32(gid_t)     214,214,-1
 int     setgid:setgid(gid_t)       -1,-1,46
diff --git a/libc/arch-arm/include/endian.h b/libc/arch-arm/include/endian.h
index 6de0889..7cba3b9 100644
--- a/libc/arch-arm/include/endian.h
+++ b/libc/arch-arm/include/endian.h
@@ -77,7 +77,7 @@
 #endif  /* __ARM_ARCH__ */
 #endif  /* __GNUC__ */
 
-#ifdef __ARMEB__
+#if defined(__ARMEB__)
 #define _BYTE_ORDER _BIG_ENDIAN
 #else
 #define _BYTE_ORDER _LITTLE_ENDIAN
diff --git a/libc/arch-arm/include/machine/_types.h b/libc/arch-arm/include/machine/_types.h
index 3e779ca..1521a4c 100644
--- a/libc/arch-arm/include/machine/_types.h
+++ b/libc/arch-arm/include/machine/_types.h
@@ -35,17 +35,6 @@
 #ifndef _ARM__TYPES_H_
 #define _ARM__TYPES_H_
 
-
-#if !defined(__ARM_EABI__)
-/* the kernel defines size_t as unsigned int, but g++ wants it to be unsigned long */
-#define _SIZE_T
-#define _SSIZE_T
-#define _PTRDIFF_T
-typedef unsigned long  size_t;
-typedef long           ssize_t;
-typedef long           ptrdiff_t;
-#endif
-
 /* 7.18.1.1 Exact-width integer types */
 typedef	__signed char		__int8_t;
 typedef	unsigned char		__uint8_t;
@@ -116,10 +105,4 @@
 typedef	void *			__wctrans_t;
 typedef	void *			__wctype_t;
 
-#ifdef __ARMEB__
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
-#define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
-
 #endif	/* _ARM__TYPES_H_ */
diff --git a/libc/arch-mips/include/endian.h b/libc/arch-mips/include/endian.h
index 6d4ca4e..41a9004 100644
--- a/libc/arch-mips/include/endian.h
+++ b/libc/arch-mips/include/endian.h
@@ -29,17 +29,6 @@
 #ifndef _MIPS64_ENDIAN_H_
 #define _MIPS64_ENDIAN_H_
 
-#if defined(__MIPSEL__)
-#define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
-#if defined(__MIPSEB__)
-#define _BYTE_ORDER _BIG_ENDIAN
-#endif
-
-#if !defined(_BYTE_ORDER) && !defined(lint)
-#error "__MIPSEL__ or __MIPSEB__ must be defined to define BYTE_ORDER!!!"
-#endif
-
 #ifdef __GNUC__
 
 #if defined(__mips_isa_rev) && (__mips_isa_rev >= 2)
@@ -69,9 +58,13 @@
 #endif  /* __mips32r2__ */
 #endif  /* __GNUC__ */
 
-
+#if defined(__MIPSEB__)
+#define _BYTE_ORDER _BIG_ENDIAN
+#else
+#define _BYTE_ORDER _LITTLE_ENDIAN
+#endif
+#define __STRICT_ALIGNMENT
+#include <sys/types.h>
 #include <sys/endian.h>
 
-#define	__STRICT_ALIGNMENT
-
 #endif /* _MIPS64_ENDIAN_H_ */
diff --git a/libc/arch-mips/include/machine/_types.h b/libc/arch-mips/include/machine/_types.h
index 1cc6c21..05f79ef 100644
--- a/libc/arch-mips/include/machine/_types.h
+++ b/libc/arch-mips/include/machine/_types.h
@@ -105,8 +105,6 @@
 typedef float			__float_t;
 typedef long long		__off_t;
 typedef long			__ptrdiff_t;
-/*typedef	unsigned long		__size_t;*/
-typedef	long			__ssize_t;
 typedef	int			__time_t;
 typedef int			__timer_t;
 #if defined(__GNUC__) && __GNUC__ >= 3
@@ -124,12 +122,6 @@
 typedef	void *			__wctrans_t;
 typedef	void *			__wctype_t;
 
-#ifdef __MIPSEB__
-#define _BYTE_ORDER _BIG_ENDIAN
-#else
-#define _BYTE_ORDER _LITTLE_ENDIAN
-#endif
-
 #if defined(_KERNEL)
 typedef struct label_t {
 	__register_t val[14];
diff --git a/libc/arch-x86/bionic/__dso_handle.S b/libc/arch-x86/bionic/__dso_handle.S
deleted file mode 100644
index 3e80128..0000000
--- a/libc/arch-x86/bionic/__dso_handle.S
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2010 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "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
- * COPYRIGHT OWNER OR CONTRIBUTORS 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.
- */
-
-# The __dso_handle global variable is used by static
-# C++ constructors and destructors in the binary.
-# See http://www.codesourcery.com/public/cxx-abi/abi.html#dso-dtor
-#
-        .section .bss
-        .align 4
-
-#ifndef CRT_LEGACY_WORKAROUND
-	.hidden __dso_handle
-#endif
-
-        .globl __dso_handle
-__dso_handle:
-        .long 0
diff --git a/libc/arch-x86/bionic/__stack_chk_fail_local.S b/libc/arch-x86/bionic/__stack_chk_fail_local.S
deleted file mode 100644
index 59fe86e..0000000
--- a/libc/arch-x86/bionic/__stack_chk_fail_local.S
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Copyright (C) 2011 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "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
- * COPYRIGHT OWNER OR CONTRIBUTORS 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.
- */
-/*
- * Contributed by: Intel Corporation
- */
-
-	.text
-	.p2align 4,,15
-	.globl	__stack_chk_fail_local
-	.hidden	__stack_chk_fail_local
-	.type	__stack_chk_fail_local, @function
-
-__stack_chk_fail_local:
-#ifdef __PIC__
-	pushl	%ebx
-	call	__x86.get_pc_thunk.bx
-	addl	$_GLOBAL_OFFSET_TABLE_, %ebx
-	call	__stack_chk_fail@PLT
-#else /* PIC */
-	jmp   __stack_chk_fail
-#endif /* not PIC */
-
-	.size	__stack_chk_fail_local, .-__stack_chk_fail_local
diff --git a/libc/arch-x86/bionic/__stack_chk_fail_local.h b/libc/arch-x86/bionic/__stack_chk_fail_local.h
new file mode 100755
index 0000000..4f3699a
--- /dev/null
+++ b/libc/arch-x86/bionic/__stack_chk_fail_local.h
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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
+ * COPYRIGHT OWNER OR CONTRIBUTORS 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.
+ */
+
+
+/*
+   __stack_chk_fail routine is runtime part of stack protector compiler
+   feature. It's implemented in libc and represents die routine when stack
+   corruption is detected.
+
+   Calls are generated by compiler and injected into user functions when
+   -fstack-protector* options are used.
+
+   __stack_chk_fail_local is wrapper for __stack_chk_fail. Compiler generates
+   wrapper calls instead for PIC code only and only on IA32 for optimization
+   purpose (see gcc/config/i386/i386.c). Wrapper body is always included into
+   executable or library. This is the idea of optimization.
+
+   Glibc is doing this via libc_nonshared.a which is linked automatically
+   everytime with libc.so. In bionic we have to bring it within crtfiles
+   because libc.so is real library and not a link script like libc.so at glibc.
+
+   For x86_64 or non-PIC code compiler always generates __stack_chk_fail calls.
+*/
+
+#ifdef __i386__
+#ifdef __PIC__
+extern void __stack_chk_fail();
+
+__attribute__ ((visibility ("hidden")))
+void __stack_chk_fail_local()
+{
+  __stack_chk_fail();
+}
+#endif
+#endif
diff --git a/libc/arch-x86/bionic/atexit.S b/libc/arch-x86/bionic/atexit.S
deleted file mode 100644
index b28f40b..0000000
--- a/libc/arch-x86/bionic/atexit.S
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "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
- * COPYRIGHT OWNER OR CONTRIBUTORS 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.
- */
-	.text
-	.p2align 4,,15
-	.globl	atexit
-	.hidden	atexit
-	.type	atexit, @function
-atexit:
-	pushl	%ebp
-	movl	%esp, %ebp
-	pushl	%ebx
-	call	__x86.get_pc_thunk.bx
-	addl	$_GLOBAL_OFFSET_TABLE_, %ebx
-	subl	$20, %esp
-	movl	$0, 4(%esp)
-	movl	__dso_handle@GOTOFF(%ebx), %eax
-	movl	%eax, 8(%esp)
-	movl	8(%ebp), %eax
-	movl	%eax, (%esp)
-	call	__cxa_atexit@PLT
-	addl	$20, %esp
-	popl	%ebx
-	popl	%ebp
-	ret
-	.size	atexit, .-atexit
-
-	.section	.text.__x86.get_pc_thunk.bx,"axG",@progbits,__x86.get_pc_thunk.bx,comdat
-	.globl	__x86.get_pc_thunk.bx
-	.hidden	__x86.get_pc_thunk.bx
-	.type	__x86.get_pc_thunk.bx, @function
-__x86.get_pc_thunk.bx:
-	nop
-	nop
-	nop
-	nop
-	nop
-	nop
-	nop
-	nop
-	movl	(%esp), %ebx
-	ret
diff --git a/libc/arch-x86/bionic/__dso_handle_so.S b/libc/arch-x86/bionic/atexit.h
similarity index 79%
rename from libc/arch-x86/bionic/__dso_handle_so.S
rename to libc/arch-x86/bionic/atexit.h
index 77a5d7f..bc776a8 100644
--- a/libc/arch-x86/bionic/__dso_handle_so.S
+++ b/libc/arch-x86/bionic/atexit.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,13 +26,10 @@
  * SUCH DAMAGE.
  */
 
-# The __dso_handle global variable is used by static
-# C++ constructors and destructors in the binary.
-# See http://www.codesourcery.com/public/cxx-abi/abi.html#dso-dtor
-#
-	.data
-        .align 4
-	.hidden __dso_handle
-        .globl __dso_handle
-__dso_handle:
-        .long __dso_handle
+extern void *__dso_handle;
+
+__attribute__ ((visibility ("hidden")))
+int atexit(void (*func)(void))
+{
+  return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
+}
diff --git a/libc/arch-x86/bionic/crtbegin.S b/libc/arch-x86/bionic/crtbegin.S
deleted file mode 100644
index 39b6af0..0000000
--- a/libc/arch-x86/bionic/crtbegin.S
+++ /dev/null
@@ -1,138 +0,0 @@
-# bionic/arch-x86/bionic/crtbegin_dynamic.S
-#
-# Copyright 2006, The Android Open Source Project
-#
-# Redistribution and use in source and binary forms, with or without
-# modification, are permitted provided that the following conditions are met:
-#     * Redistributions of source code must retain the above copyright
-#       notice, this list of conditions and the following disclaimer.
-#     * 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.
-#     * Neither the name of Google Inc. nor the names of its contributors may
-#       be used to endorse or promote products derived from this software
-#       without specific prior written permission.
-#
-# THIS SOFTWARE IS PROVIDED BY Google Inc. ``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 Google Inc. 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.
-
-	.text
-	.align 4
-	.type _start, @function
-	.globl _start
-
-# this is the small startup code that is first run when
-# any executable that is linked with Bionic runs.
-#
-# it's purpose is to call __libc_init with appropriate
-# arguments, which are:
-#
-#    - the address of the raw data block setup by the Linux
-#      kernel ELF loader
-#
-#    - address of an "onexit" function, not used on any
-#      platform supported by Bionic
-#
-#    - address of the "main" function of the program. We
-#      can't hard-code it in the adr pseudo instruction
-#      so we use a tiny trampoline that will get relocated
-#      by the dynamic linker before this code runs
-#
-#    - address of the constructor list
-#
-_start:	
-        mov     %esp, %eax
-        # before push arguments, align the stack to a 16 byte boundary
-        andl    $~15, %esp
-        mov     $1f, %edx
-        pushl   %edx
-        mov     $0f, %edx
-        pushl   %edx
-        mov     $0, %edx
-        pushl   %edx
-        pushl   %eax
-        call     __libc_init
-
-0:
-        jmp   main
-
-1:  .long   __PREINIT_ARRAY__
-    .long   __INIT_ARRAY__
-    .long   __FINI_ARRAY__
-
-	.section .preinit_array, "aw"
-	.globl __PREINIT_ARRAY__
-__PREINIT_ARRAY__:
-	.long -1
-
-	.section .init_array, "aw"
-	.globl __INIT_ARRAY__
-__INIT_ARRAY__:
-	.long -1
-	.long	frame_dummy
-
-	.section .fini_array, "aw"
-	.globl __FINI_ARRAY__
-__FINI_ARRAY__:
-	.long -1
-	.long	__do_global_dtors_aux
-
-	.section	.eh_frame,"a",@progbits
-	.align 4
-	.type	__EH_FRAME_BEGIN__, @object
-__EH_FRAME_BEGIN__:
-	.text
-	.p2align 4,,15
-	.type	__do_global_dtors_aux, @function
-__do_global_dtors_aux:
-	pushl	%ebp
-	movl	%esp, %ebp
-	subl	$24, %esp
-	cmpb	$0, completed.4454
-	jne	.L4
-	movl	$__deregister_frame_info_bases, %eax
-	testl	%eax, %eax
-	je	.L3
-	movl	$__EH_FRAME_BEGIN__, (%esp)
-	call	__deregister_frame_info_bases
-.L3:
-	movb	$1, completed.4454
-.L4:
-	leave
-	ret
-	.text
-	.p2align 4,,15
-	.type	frame_dummy, @function
-frame_dummy:
-	pushl	%ebp
-	movl	$__register_frame_info_bases, %eax
-	movl	%esp, %ebp
-	subl	$24, %esp
-	testl	%eax, %eax
-	je	.L7
-	movl	%ebx, 12(%esp)
-	movl	$0, 8(%esp)
-	movl	$object.4466, 4(%esp)
-	movl	$__EH_FRAME_BEGIN__, (%esp)
-	call	__register_frame_info_bases
-.L7:
-	leave
-	ret
-	.local	completed.4454
-	.comm	completed.4454,1,1
-	.local	object.4466
-	.comm	object.4466,24,4
-	.weak	__register_frame_info_bases
-	.weak	__deregister_frame_info_bases
-
-#include "__dso_handle.S"
-#include "atexit.S"
-#include "__stack_chk_fail_local.S"
diff --git a/libc/arch-x86/bionic/crtbegin.c b/libc/arch-x86/bionic/crtbegin.c
new file mode 100755
index 0000000..5106d9e
--- /dev/null
+++ b/libc/arch-x86/bionic/crtbegin.c
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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
+ * COPYRIGHT OWNER OR CONTRIBUTORS 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.
+ */
+
+typedef struct
+{
+    void (**preinit_array)(void);
+    void (**init_array)(void);
+    void (**fini_array)(void);
+} structors_array_t;
+
+extern int main(int argc, char **argv, char **env);
+
+extern void __libc_init(
+  unsigned int *elfdata,
+  void (*onexit)(void),
+  int (*slingshot)(int, char**, char**),
+  structors_array_t const * const structors
+);
+
+__attribute__ ((section (".preinit_array")))
+void (*__PREINIT_ARRAY__)(void) = (void (*)(void)) -1;
+
+__attribute__ ((section (".init_array")))
+void (*__INIT_ARRAY__)(void) = (void (*)(void)) -1;
+
+__attribute__ ((section (".fini_array")))
+void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
+
+__attribute__((visibility("hidden")))
+__attribute__((force_align_arg_pointer))
+void _start() {
+  structors_array_t array;
+  void *elfdata;
+
+  array.preinit_array = &__PREINIT_ARRAY__;
+  array.init_array =    &__INIT_ARRAY__;
+  array.fini_array =    &__FINI_ARRAY__;
+
+  elfdata = __builtin_frame_address(0) + sizeof(void *);
+  __libc_init(elfdata, (void *) 0, &main, &array);
+}
+
+#include "__dso_handle.h"
+#include "atexit.h"
+#include "__stack_chk_fail_local.h"
diff --git a/libc/arch-x86/bionic/crtbegin_so.S b/libc/arch-x86/bionic/crtbegin_so.S
deleted file mode 100644
index 99662fe..0000000
--- a/libc/arch-x86/bionic/crtbegin_so.S
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *  * Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *  * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "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
- * COPYRIGHT OWNER OR CONTRIBUTORS 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.
- */
-
-.section .init_array, "aw"
-.align 4
-.type __INIT_ARRAY__, @object
-.globl __INIT_ARRAY__
-__INIT_ARRAY__:
-    .long -1
-    .long frame_dummy
-
-.section .fini_array, "aw"
-.align 4
-.type __FINI_ARRAY__, @object
-.globl __FINI_ARRAY__
-__FINI_ARRAY__:
-    .long -1
-    .long __do_global_dtors_aux
-
-	.section	.eh_frame,"a",@progbits
-	.align 4
-	.type	__EH_FRAME_BEGIN__, @object
-__EH_FRAME_BEGIN__:
-	.text
-	.p2align 4,,15
-	.type	__do_global_dtors_aux, @function
-__do_global_dtors_aux:
-	pushl	%ebp
-	movl	%esp, %ebp
-	pushl	%ebx
-	call	__x86.get_pc_thunk.bx
-	addl	$_GLOBAL_OFFSET_TABLE_, %ebx
-	subl	$20, %esp
-	cmpb	$0, completed.4454@GOTOFF(%ebx)
-	jne	.L5
-	movl	__dso_handle@GOTOFF(%ebx), %eax
-	movl	%eax, (%esp)
-	call	__cxa_finalize@PLT
-	movl	__deregister_frame_info_bases@GOT(%ebx), %eax
-	testl	%eax, %eax
-	je	.L4
-	leal	__EH_FRAME_BEGIN__@GOTOFF(%ebx), %eax
-	movl	%eax, (%esp)
-	call	__deregister_frame_info_bases@PLT
-.L4:
-	movb	$1, completed.4454@GOTOFF(%ebx)
-.L5:
-	addl	$20, %esp
-	popl	%ebx
-	popl	%ebp
-	ret
-	.text
-	.p2align 4,,15
-	.type	frame_dummy, @function
-frame_dummy:
-	pushl	%ebp
-	movl	%esp, %ebp
-	pushl	%ebx
-	call	__x86.get_pc_thunk.bx
-	addl	$_GLOBAL_OFFSET_TABLE_, %ebx
-	subl	$20, %esp
-	movl	__register_frame_info_bases@GOT(%ebx), %eax
-	testl	%eax, %eax
-	je	.L8
-	leal	object.4469@GOTOFF(%ebx), %eax
-	movl	%eax, 4(%esp)
-	leal	__EH_FRAME_BEGIN__@GOTOFF(%ebx), %eax
-	movl	%ebx, 12(%esp)
-	movl	$0, 8(%esp)
-	movl	%eax, (%esp)
-	call	__register_frame_info_bases@PLT
-.L8:
-	addl	$20, %esp
-	popl	%ebx
-	popl	%ebp
-	ret
-	.local	completed.4454
-	.comm	completed.4454,1,1
-	.local	object.4469
-	.comm	object.4469,24,4
-	.weak	__register_frame_info_bases
-	.weak	__deregister_frame_info_bases
-
-#include "__dso_handle_so.S"
-#include "atexit.S"
-#include "__stack_chk_fail_local.S"
diff --git a/libc/arch-x86/bionic/crtbegin_so.c b/libc/arch-x86/bionic/crtbegin_so.c
new file mode 100755
index 0000000..30de6af
--- /dev/null
+++ b/libc/arch-x86/bionic/crtbegin_so.c
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * 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 COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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
+ * COPYRIGHT OWNER OR CONTRIBUTORS 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.
+ */
+
+extern void __cxa_finalize(void *);
+extern void *__dso_handle;
+
+__attribute__((visibility("hidden"),destructor))
+void __on_dlclose() {
+  __cxa_finalize(&__dso_handle);
+}
+
+/* CRT_LEGACY_WORKAROUND should only be defined when building
+ * this file as part of the platform's C library.
+ *
+ * The C library already defines a function named 'atexit()'
+ * for backwards compatibility with older NDK-generated binaries.
+ *
+ * For newer ones, 'atexit' is actually embedded in the C
+ * runtime objects that are linked into the final ELF
+ * binary (shared library or executable), and will call
+ * __cxa_atexit() in order to un-register any atexit()
+ * handler when a library is unloaded.
+ *
+ * This function must be global *and* hidden. Only the
+ * code inside the same ELF binary should be able to access it.
+ */
+
+#ifdef CRT_LEGACY_WORKAROUND
+#include "__dso_handle.h"
+#else
+#include "__dso_handle_so.h"
+#include "atexit.h"
+#include "__stack_chk_fail_local.h"
+#endif
diff --git a/libc/arch-x86/include/endian.h b/libc/arch-x86/include/endian.h
index 4a70536..e7ad257 100644
--- a/libc/arch-x86/include/endian.h
+++ b/libc/arch-x86/include/endian.h
@@ -65,6 +65,7 @@
 #endif	/* __GNUC__ */
 
 #define _BYTE_ORDER _LITTLE_ENDIAN
+#include <sys/types.h>
 #include <sys/endian.h>
 
 #endif /* _I386_ENDIAN_H_ */
diff --git a/libc/arch-x86/include/machine/_types.h b/libc/arch-x86/include/machine/_types.h
index d3d9eeb..d217822 100644
--- a/libc/arch-x86/include/machine/_types.h
+++ b/libc/arch-x86/include/machine/_types.h
@@ -35,29 +35,6 @@
 #ifndef _I386__TYPES_H_
 #define _I386__TYPES_H_
 
-/* the kernel defines size_t as unsigned int, but g++ wants it to be unsigned long */
-#ifndef _SIZE_T_DEFINED_
-#  define _SIZE_T_DEFINED_
-#  ifdef __ANDROID__
-     typedef unsigned int  size_t;
-#  else
-     typedef unsigned long  size_t;
-#  endif
-#endif
-#if !defined(_SSIZE_T) && !defined(_SSIZE_T_DEFINED_)
-#define _SSIZE_T
-#define _SSIZE_T_DEFINED_
-typedef long int       ssize_t;
-#endif
-#ifndef _PTRDIFF_T
-#define _PTRDIFF_T
-#  ifdef __ANDROID__
-     typedef int            ptrdiff_t;
-#  else
-     typedef long           ptrdiff_t;
-#  endif
-#endif
-
 /* 7.18.1.1 Exact-width integer types */
 typedef	__signed char		__int8_t;
 typedef	unsigned char		__uint8_t;
diff --git a/libc/bionic/__set_errno.c b/libc/bionic/__set_errno.cpp
similarity index 66%
rename from libc/bionic/__set_errno.c
rename to libc/bionic/__set_errno.cpp
index 163d404..5b249c8 100644
--- a/libc/bionic/__set_errno.c
+++ b/libc/bionic/__set_errno.cpp
@@ -28,29 +28,25 @@
 
 #include <errno.h>
 
+// These functions are called from our assembler syscall stubs.
+// C/C++ code should just assign 'errno' instead.
 
-int __set_errno(int n)
-{
-    errno = n;
-    return -1;
+// TODO: should be __LIBC_HIDDEN__, but already exported by NDK :-(
+// TODO: this isn't used on ARM.
+extern "C" int __set_errno(int n) {
+  errno = n;
+  return -1;
 }
 
-/*
- * this function is called from syscall stubs,
- * (tail-called in the case of 0-4 arg versions)
- */
-
-__LIBC_HIDDEN__
-int __set_syscall_errno(int n)
-{
-        /* some syscalls, mmap() for example, have valid return
-        ** values that are "negative".  Since errno values are not
-        ** greater than 131 on Linux, we will just consider 
-        ** anything significantly out of range as not-an-error
-        */
-    if(n > -256) {
-        return __set_errno(-n);
-    } else {
-        return n;
-    }
+// TODO: this is only used on ARM, but is exported by NDK on all platforms :-(
+extern "C" __LIBC_HIDDEN__ int __set_syscall_errno(int n) {
+  // Some syscalls, mmap() for example, have valid return
+  // values that are "negative".  Since errno values are not
+  // greater than 131 on Linux, we will just consider
+  // anything significantly out of range as not-an-error.
+  if(n > -256) {
+    return __set_errno(-n);
+  } else {
+    return n;
+  }
 }
diff --git a/libc/bionic/pthread.c b/libc/bionic/pthread.c
index d8d0d05..b685f2d 100644
--- a/libc/bionic/pthread.c
+++ b/libc/bionic/pthread.c
@@ -59,7 +59,6 @@
 extern int  __pthread_clone(int (*fn)(void*), void *child_stack, int flags, void *arg);
 extern void _exit_with_stack_teardown(void * stackBase, int stackSize, int retCode);
 extern void _exit_thread(int  retCode);
-extern int  __set_errno(int);
 
 int  __futex_wake_ex(volatile void *ftx, int pshared, int val)
 {
diff --git a/libc/include/errno.h b/libc/include/errno.h
index e1b15c0..2e5ce5f 100644
--- a/libc/include/errno.h
+++ b/libc/include/errno.h
@@ -40,10 +40,6 @@
 #define  ENOTSUP  EOPNOTSUPP
 #endif
 
-/* internal function that should *only* be called from system calls */
-/* use errno = xxxx instead in C code                               */
-extern int    __set_errno(int  error);
-
 /* internal function returning the address of the thread-specific errno */
 extern volatile int*   __errno(void);
 
diff --git a/libc/include/search.h b/libc/include/search.h
index ed0d216..e12e49e 100644
--- a/libc/include/search.h
+++ b/libc/include/search.h
@@ -12,13 +12,6 @@
 #include <sys/cdefs.h>
 #include <sys/_types.h>
 
-#if 0
-#ifndef _SIZE_T_DECLARED
-typedef	__size_t	size_t;
-#define	_SIZE_T_DECLARED
-#endif
-#endif
-
 typedef	enum {
 	preorder,
 	postorder,
diff --git a/libc/include/signal.h b/libc/include/signal.h
index 6432c18..9d3badc 100644
--- a/libc/include/signal.h
+++ b/libc/include/signal.h
@@ -28,6 +28,7 @@
 #ifndef _SIGNAL_H_
 #define _SIGNAL_H_
 
+#include <errno.h>
 #include <sys/cdefs.h>
 #include <limits.h>		/* For LONG_BIT */
 #include <string.h>		/* For memset() */
@@ -53,45 +54,57 @@
 #  define _NSIG  64
 #endif
 
-extern const char * const sys_siglist[];
-extern const char * const sys_signame[];
+extern const char* const sys_siglist[];
+extern const char* const sys_signame[];
 
-static __inline__ int sigismember(sigset_t *set, int signum)
-{
-    unsigned long *local_set = (unsigned long *)set;
-    signum--;
-    return (int)((local_set[signum/LONG_BIT] >> (signum%LONG_BIT)) & 1);
+static __inline__ int sigismember(sigset_t* set, int signum) {
+  if (set == NULL || signum < 1 || signum >= 8*sizeof(sigset_t)) {
+    errno = EINVAL;
+    return -1;
+  }
+  unsigned long* local_set = (unsigned long*) set;
+  signum--;
+  return (int) ((local_set[signum/LONG_BIT] >> (signum%LONG_BIT)) & 1);
 }
 
-
-static __inline__ int sigaddset(sigset_t *set, int signum)
-{
-    unsigned long *local_set = (unsigned long *)set;
-    signum--;
-    local_set[signum/LONG_BIT] |= 1UL << (signum%LONG_BIT);
-    return 0;
+static __inline__ int sigaddset(sigset_t* set, int signum) {
+  if (set == NULL || signum < 1 || signum >= 8*sizeof(sigset_t)) {
+    errno = EINVAL;
+    return -1;
+  }
+  unsigned long* local_set = (unsigned long*) set;
+  signum--;
+  local_set[signum/LONG_BIT] |= 1UL << (signum%LONG_BIT);
+  return 0;
 }
 
-
-static __inline__ int sigdelset(sigset_t *set, int signum)
-{
-    unsigned long *local_set = (unsigned long *)set;
-    signum--;
-    local_set[signum/LONG_BIT] &= ~(1UL << (signum%LONG_BIT));
-    return 0;
+static __inline__ int sigdelset(sigset_t* set, int signum) {
+  if (set == NULL || signum < 1 || signum >= 8*sizeof(sigset_t)) {
+    errno = EINVAL;
+    return -1;
+  }
+  unsigned long* local_set = (unsigned long*) set;
+  signum--;
+  local_set[signum/LONG_BIT] &= ~(1UL << (signum%LONG_BIT));
+  return 0;
 }
 
-
-static __inline__ int sigemptyset(sigset_t *set)
-{
-    memset(set, 0, sizeof *set);
-    return 0;
+static __inline__ int sigemptyset(sigset_t* set) {
+  if (set == NULL) {
+    errno = EINVAL;
+    return -1;
+  }
+  memset(set, 0, sizeof *set);
+  return 0;
 }
 
-static __inline__ int sigfillset(sigset_t *set)
-{
-    memset(set, ~0, sizeof *set);
-    return 0;
+static __inline__ int sigfillset(sigset_t* set) {
+  if (set == NULL) {
+    errno = EINVAL;
+    return -1;
+  }
+  memset(set, ~0, sizeof *set);
+  return 0;
 }
 
 
diff --git a/libc/include/stdint.h b/libc/include/stdint.h
index 49879cb..c3e29dd 100644
--- a/libc/include/stdint.h
+++ b/libc/include/stdint.h
@@ -226,12 +226,6 @@
 #  define UINTMAX_C(c)	UINT64_C(c)
 #endif
 
-/* size_t is defined by the GCC-specific <stddef.h> */
-#ifndef _SSIZE_T_DEFINED_
-#define _SSIZE_T_DEFINED_
-typedef long int  ssize_t;
-#endif
-
 #define _BITSIZE 32
 
 /* Keep the kernel from trying to define these types... */
diff --git a/libc/include/stdio.h b/libc/include/stdio.h
index 20a9829..cd04d9c 100644
--- a/libc/include/stdio.h
+++ b/libc/include/stdio.h
@@ -39,30 +39,11 @@
 #define	_STDIO_H_
 
 #include <sys/cdefs.h>
-#include <sys/_types.h>
+#include <sys/types.h>
 
 #include <stdarg.h>
 #include <stddef.h>
 
-#if __BSD_VISIBLE || __POSIX_VISIBLE || __XPG_VISIBLE
-#include <sys/types.h>	/* XXX should be removed */
-#endif
-
-#ifndef _SIZE_T_DEFINED_
-#define _SIZE_T_DEFINED_
-typedef unsigned int  size_t;
-#endif
-
-#ifndef _SSIZE_T_DEFINED_
-#define _SSIZE_T_DEFINED_
-typedef long int  ssize_t;
-#endif
-
-#ifndef	_OFF_T_DEFINED_
-#define	_OFF_T_DEFINED_
-typedef	long    off_t;
-#endif
-
 #define __need_NULL
 #include <stddef.h>
 
diff --git a/libc/include/string.h b/libc/include/string.h
index 06e2284..32b9310 100644
--- a/libc/include/string.h
+++ b/libc/include/string.h
@@ -224,6 +224,23 @@
     return __strlen_chk(s, bos);
 }
 
+__purefunc extern char* __strchr_real(const char *, int)
+    __asm__(__USER_LABEL_PREFIX__ "strchr");
+extern char* __strchr_chk(const char *, int, size_t);
+
+__BIONIC_FORTIFY_INLINE
+char* strchr(const char *s, int c) {
+    size_t bos = __builtin_object_size(s, 0);
+
+    // Compiler doesn't know destination size. Don't call __strchr_chk
+    if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
+        return __strchr_real(s, c);
+    }
+
+    return __strchr_chk(s, c, bos);
+}
+
+
 #endif /* defined(__BIONIC_FORTIFY_INLINE) */
 
 __END_DECLS
diff --git a/libc/include/sys/linux-syscalls.h b/libc/include/sys/linux-syscalls.h
index f2249fd..ec48ada 100644
--- a/libc/include/sys/linux-syscalls.h
+++ b/libc/include/sys/linux-syscalls.h
@@ -83,7 +83,6 @@
 #define __NR_msync                        (__NR_SYSCALL_BASE + 144)
 #define __NR_readv                        (__NR_SYSCALL_BASE + 145)
 #define __NR_writev                       (__NR_SYSCALL_BASE + 146)
-#define __NR_getsid                       (__NR_SYSCALL_BASE + 147)
 
 #ifdef __arm__
 #define __NR_exit_group                   (__NR_SYSCALL_BASE + 248)
@@ -98,6 +97,7 @@
 #define __NR_gettid                       (__NR_SYSCALL_BASE + 224)
 #define __NR_readahead                    (__NR_SYSCALL_BASE + 225)
 #define __NR_getgroups32                  (__NR_SYSCALL_BASE + 205)
+#define __NR_getsid                       (__NR_SYSCALL_BASE + 147)
 #define __NR_setgid32                     (__NR_SYSCALL_BASE + 214)
 #define __NR_setreuid32                   (__NR_SYSCALL_BASE + 203)
 #define __NR_setresuid32                  (__NR_SYSCALL_BASE + 208)
@@ -221,6 +221,7 @@
 #define __NR_pipe                         (__NR_SYSCALL_BASE + 42)
 #define __NR_sigsuspend                   (__NR_SYSCALL_BASE + 72)
 #define __NR_socketcall                   (__NR_SYSCALL_BASE + 102)
+#define __NR_getsid                       (__NR_SYSCALL_BASE + 147)
 #define __NR_fdatasync                    (__NR_SYSCALL_BASE + 148)
 #define __NR_mlock                        (__NR_SYSCALL_BASE + 150)
 #define __NR_munlock                      (__NR_SYSCALL_BASE + 151)
@@ -352,6 +353,7 @@
 #define __NR_setgroups                    (__NR_SYSCALL_BASE + 81)
 #define __NR_fchown                       (__NR_SYSCALL_BASE + 95)
 #define __NR_cacheflush                   (__NR_SYSCALL_BASE + 147)
+#define __NR_getsid                       (__NR_SYSCALL_BASE + 151)
 #define __NR_fdatasync                    (__NR_SYSCALL_BASE + 152)
 #define __NR_mlock                        (__NR_SYSCALL_BASE + 154)
 #define __NR_munlock                      (__NR_SYSCALL_BASE + 155)
diff --git a/libc/include/sys/stat.h b/libc/include/sys/stat.h
index 62e0d8b..05b221f 100644
--- a/libc/include/sys/stat.h
+++ b/libc/include/sys/stat.h
@@ -112,6 +112,13 @@
 #define  st_mtimensec  st_mtime_nsec
 #define  st_ctimensec  st_ctime_nsec
 
+#ifdef __USE_BSD
+/* Permission macros provided by glibc for compatibility with BSDs. */
+#define ACCESSPERMS (S_IRWXU | S_IRWXG | S_IRWXO) /* 0777 */
+#define ALLPERMS    (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) /* 07777 */
+#define DEFFILEMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) /* 0666 */
+#endif
+
 extern int    chmod(const char *, mode_t);
 extern int    fchmod(int, mode_t);
 extern int    mkdir(const char *, mode_t);
diff --git a/libc/include/sys/types.h b/libc/include/sys/types.h
index 77b3223..8cfeeb6 100644
--- a/libc/include/sys/types.h
+++ b/libc/include/sys/types.h
@@ -83,11 +83,6 @@
 typedef  .... pthread_t;
 #endif
 
-#ifndef _SIZE_T_DEFINED_
-#define _SIZE_T_DEFINED_
-typedef unsigned int  size_t;
-#endif
-
 #ifndef _SSIZE_T_DEFINED_
 #define _SSIZE_T_DEFINED_
 typedef long int  ssize_t;
diff --git a/libc/include/wchar.h b/libc/include/wchar.h
index b46985b..ef88e04 100644
--- a/libc/include/wchar.h
+++ b/libc/include/wchar.h
@@ -45,7 +45,7 @@
 
 __BEGIN_DECLS
 
-typedef int                     wint_t;
+typedef __WINT_TYPE__           wint_t;
 typedef struct { int  dummy; }  mbstate_t;
 
 typedef enum {
diff --git a/libc/private/logd.h b/libc/private/logd.h
index c81a91a..26878ba 100644
--- a/libc/private/logd.h
+++ b/libc/private/logd.h
@@ -29,6 +29,7 @@
 #define _ANDROID_BIONIC_LOGD_H
 
 #include <stdarg.h>
+#include <stdint.h>
 
 #define BIONIC_EVENT_MEMCPY_BUFFER_OVERFLOW 80100
 #define BIONIC_EVENT_STRCAT_BUFFER_OVERFLOW 80105
diff --git a/libc/stdio/fopen.c b/libc/stdio/fopen.c
index a6cddd3..6d2d882 100644
--- a/libc/stdio/fopen.c
+++ b/libc/stdio/fopen.c
@@ -31,6 +31,8 @@
  * SUCH DAMAGE.
  */
 
+#define __USE_BSD
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
diff --git a/libc/stdio/freopen.c b/libc/stdio/freopen.c
index da3a674..f0386e9 100644
--- a/libc/stdio/freopen.c
+++ b/libc/stdio/freopen.c
@@ -31,6 +31,8 @@
  * SUCH DAMAGE.
  */
 
+#define __USE_BSD
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
diff --git a/libc/stdio/local.h b/libc/stdio/local.h
index 6b2111a..664cec1 100644
--- a/libc/stdio/local.h
+++ b/libc/stdio/local.h
@@ -35,10 +35,6 @@
 #include "wcio.h"
 #include "fileext.h"
 
-#ifndef DEFFILEMODE
-#define DEFFILEMODE  (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
-#endif
-
 
 /*
  * Information local to this implementation of stdio,
diff --git a/libc/string/strchr.c b/libc/string/strchr.c
index 9b4332c..44516ef 100644
--- a/libc/string/strchr.c
+++ b/libc/string/strchr.c
@@ -29,11 +29,17 @@
  */
 
 #include <string.h>
+#include <private/logd.h>
 
 char *
-strchr(const char *p, int ch)
+__strchr_chk(const char *p, int ch, size_t s_len)
 {
-	for (;; ++p) {
+	for (;; ++p, s_len--) {
+		if (s_len == 0) {
+			__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
+				"*** FORTIFY_SOURCE strchr read beyond buffer ***\n");
+			abort();
+		}
 		if (*p == (char) ch)
 			return((char *)p);
 		if (!*p)
@@ -41,3 +47,8 @@
 	}
 	/* NOTREACHED */
 }
+
+char *
+strchr(const char *p, int ch) {
+    return __strchr_chk(p, ch, (size_t) -1);
+}
diff --git a/libc/tools/checksyscalls.py b/libc/tools/checksyscalls.py
index 286e727..b325676 100755
--- a/libc/tools/checksyscalls.py
+++ b/libc/tools/checksyscalls.py
@@ -2,7 +2,7 @@
 #
 # this tool is used to check that the syscall numbers that are in
 # SYSCALLS.TXT correspond to those found in the Linux kernel sources
-# for the arm and i386 architectures
+# for the arm, i386 and mips architectures
 #
 
 import sys, re, string, os, commands
@@ -167,13 +167,12 @@
     for sc in syscalls:
         sc_name = sc["name"]
         sc_id   = sc[idname]
+        if sc_id == -1:
+            sc_id = sc["common"]
         if sc_id >= 0:
             if not arch_dict.has_key(sc_name):
                 print "error: %s syscall %s not defined, should be %d" % (archname, sc_name, sc_id)
                 errors += 1
-            elif not arch_dict.has_key(sc_name):
-                print "error: %s syscall %s is not implemented" % (archname, sc_name)
-                errors += 1
             elif arch_dict[sc_name] != sc_id:
                 print "error: %s syscall %s should be %d instead of %d" % (archname, sc_name, arch_dict[sc_name], sc_id)
                 errors += 1
diff --git a/libc/tools/zoneinfo/generate b/libc/tools/zoneinfo/generate
index ba8ea0d..334ba3c 100755
--- a/libc/tools/zoneinfo/generate
+++ b/libc/tools/zoneinfo/generate
@@ -66,22 +66,24 @@
   """Downloads and repackages the given data from the given FTP server."""
 
   new_version = re.search('(tzdata.+)\\.tar\\.gz', data_filename).group(1)
-  signature_filename = '%s.sign' % data_filename
 
   # Switch to a temporary directory.
   tmp_dir = tempfile.mkdtemp('-tzdata')
   os.chdir(tmp_dir)
   print 'Created temporary directory "%s"...' % tmp_dir
 
-  print 'Downloading data and signature...'
+  print 'Downloading data...'
   Retrieve(ftp, data_filename)
+
+  print 'Downloading signature...'
+  signature_filename = '%s.asc' % data_filename
   Retrieve(ftp, signature_filename)
 
   print 'Verifying signature...'
   # If this fails for you, you probably need to import Paul Eggert's public key:
   # gpg --recv-keys ED97E90E62AA7E34
-  subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34',
-                         '--verify', signature_filename, data_filename])
+  subprocess.check_call(['gpg', '--trusted-key=ED97E90E62AA7E34', '--verify',
+                         signature_filename, data_filename])
 
   print 'Extracting...'
   os.mkdir('extracted')
diff --git a/libc/zoneinfo/tzdata b/libc/zoneinfo/tzdata
index 367c0f9..019650e 100644
--- a/libc/zoneinfo/tzdata
+++ b/libc/zoneinfo/tzdata
Binary files differ
diff --git a/linker/Android.mk b/linker/Android.mk
index e3bbffa..b757030 100644
--- a/linker/Android.mk
+++ b/linker/Android.mk
@@ -1,8 +1,14 @@
 LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
+ifeq ($(TARGET_ARCH),x86)
+    linker_begin_extension := c
+else
+    linker_begin_extension := S
+endif
+
 LOCAL_SRC_FILES:= \
-	arch/$(TARGET_ARCH)/begin.S \
+	arch/$(TARGET_ARCH)/begin.$(linker_begin_extension) \
 	debugger.cpp \
 	dlfcn.cpp \
 	linker.cpp \
diff --git a/linker/arch/x86/begin.S b/linker/arch/x86/begin.c
old mode 100644
new mode 100755
similarity index 62%
rename from linker/arch/x86/begin.S
rename to linker/arch/x86/begin.c
index baa386f..2ca15c4
--- a/linker/arch/x86/begin.S
+++ b/linker/arch/x86/begin.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2012 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -26,22 +26,30 @@
  * SUCH DAMAGE.
  */
 
-.text
-.align 4
-.type _start, @function
-.globl _start
+extern unsigned __linker_init(unsigned int *elfdata);
 
-_start:
-        /* save the elfdata ptr to %eax, AND push it onto the stack */
-        mov    %esp, %eax
-        pushl  %esp
+__attribute__((visibility("hidden")))
+void _start() {
+  void *elfdata;
+  void (*start)(void);
 
-        pushl  %eax
-        call   __linker_init
+  elfdata = __builtin_frame_address(0) + sizeof(void *);
+  start = (void(*)(void))__linker_init(elfdata);
 
-        /* linker init returns (%eax) the _entry address in the main image */
-        /* entry point expects sp to point to elfdata */
-        popl   %esp
-        jmp    *%eax
+  /* linker init returns (%eax) the _entry address in the main image */
+  /* entry point expects sp to point to elfdata */
 
-#include "arch-x86/bionic/__stack_chk_fail_local.S"
+  __asm__ (
+     "mov %0, %%esp\n\t"
+     "jmp *%1\n\t"
+     : : "r"(elfdata), "r"(start) :
+  );
+
+  /* Unreachable */
+}
+
+/* Since linker has its own version of crtbegin (this file) it should have */
+/* own version of __stack_chk_fail_local for the case when it's built with */
+/* stack protector feature */
+
+#include "arch-x86/bionic/__stack_chk_fail_local.h"
diff --git a/linker/linker.cpp b/linker/linker.cpp
index fc54f3a..0d0a8a9 100755
--- a/linker/linker.cpp
+++ b/linker/linker.cpp
@@ -146,7 +146,7 @@
     {                                                                           \
         const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
          __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg);            \
-        write(2, msg, sizeof(msg));                                             \
+        write(2, msg, strlen(msg));                                             \
         abort();                                                                \
     }
 #define UNUSED __attribute__((unused))
@@ -1916,9 +1916,9 @@
     somain = si;
 
     if (!soinfo_link_image(si)) {
-        char errmsg[] = "CANNOT LINK EXECUTABLE\n";
+        const char* msg = "CANNOT LINK EXECUTABLE\n";
         write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
-        write(2, errmsg, sizeof(errmsg));
+        write(2, msg, strlen(msg));
         exit(EXIT_FAILURE);
     }
 
diff --git a/tests/Android.mk b/tests/Android.mk
index 0da3951..abc6f52 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -59,6 +59,7 @@
     libgen_test.cpp \
     pthread_test.cpp \
     regex_test.cpp \
+    signal_test.cpp \
     stack_protector_test.cpp \
     stdio_test.cpp \
     stdlib_test.cpp \
diff --git a/tests/signal_test.cpp b/tests/signal_test.cpp
new file mode 100644
index 0000000..a54f14d
--- /dev/null
+++ b/tests/signal_test.cpp
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <errno.h>
+#include <signal.h>
+
+template <typename Fn>
+static void TestSigSet1(Fn fn) {
+  // NULL sigset_t*.
+  sigset_t* set_ptr = NULL;
+  errno = 0;
+  ASSERT_EQ(-1, fn(set_ptr));
+  ASSERT_EQ(EINVAL, errno);
+
+  // Non-NULL.
+  sigset_t set;
+  errno = 0;
+  ASSERT_EQ(0, fn(&set));
+  ASSERT_EQ(0, errno);
+}
+
+template <typename Fn>
+static void TestSigSet2(Fn fn) {
+  // NULL sigset_t*.
+  sigset_t* set_ptr = NULL;
+  errno = 0;
+  ASSERT_EQ(-1, fn(set_ptr, SIGSEGV));
+  ASSERT_EQ(EINVAL, errno);
+
+  sigset_t set;
+  sigemptyset(&set);
+
+  int min_signal = SIGHUP;
+  int max_signal = SIGRTMAX;
+
+#if __BIONIC__
+  // bionic's sigset_t is too small: 32 bits instead of 64.
+  // This means you can't refer to any of the real-time signals.
+  // See http://b/3038348 and http://b/5828899.
+  max_signal = 31;
+#else
+  // Other C libraries are perfectly capable of using their largest signal.
+  ASSERT_GE(sizeof(sigset_t) * 8, static_cast<size_t>(SIGRTMAX));
+#endif
+
+  // Bad signal number: too small.
+  errno = 0;
+  ASSERT_EQ(-1, fn(&set, 0));
+  ASSERT_EQ(EINVAL, errno);
+
+  // Bad signal number: too high.
+  errno = 0;
+  ASSERT_EQ(-1, fn(&set, max_signal + 1));
+  ASSERT_EQ(EINVAL, errno);
+
+  // Good signal numbers, low and high ends of range.
+  errno = 0;
+  ASSERT_EQ(0, fn(&set, min_signal));
+  ASSERT_EQ(0, errno);
+  ASSERT_EQ(0, fn(&set, max_signal));
+  ASSERT_EQ(0, errno);
+}
+
+TEST(signal, sigismember_invalid) {
+  TestSigSet2(sigismember);
+}
+
+TEST(signal, sigaddset_invalid) {
+  TestSigSet2(sigaddset);
+}
+
+TEST(signal, sigdelset_invalid) {
+  TestSigSet2(sigdelset);
+}
+
+TEST(signal, sigemptyset_invalid) {
+  TestSigSet1(sigemptyset);
+}
+
+TEST(signal, sigfillset_invalid) {
+  TestSigSet1(sigfillset);
+}