Move common arch-* code to arch-common directory

Will be helpful on adding x86_64

Change-Id: I96cf6fc7912c02f289c75f07ae0079c32d69173f
Signed-off-by: Pavel Chupin <pavel.v.chupin@intel.com>
diff --git a/libc/Android.mk b/libc/Android.mk
index c7478c5..3d06514 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -646,18 +646,20 @@
 # that will call __cxa_finalize(&__dso_handle) in order to ensure that
 # static C++ destructors are properly called on dlclose().
 #
+libc_crt_target_crtbegin_file := $(LOCAL_PATH)/arch-common/bionic/crtbegin.c
+libc_crt_target_crtbegin_so_file := $(LOCAL_PATH)/arch-common/bionic/crtbegin_so.c
+
 ifeq ($(TARGET_ARCH),arm)
     libc_crt_target_so_cflags :=
 endif
 ifeq ($(TARGET_ARCH),mips)
     libc_crt_target_so_cflags := -fPIC
+libc_crt_target_crtbegin_file := $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin.c
 endif
 ifeq ($(TARGET_ARCH),$(filter $(TARGET_ARCH),x86 x86_64))
     libc_crt_target_so_cflags := -fPIC
 endif
 libc_crt_target_so_cflags += $(libc_crt_target_cflags)
-libc_crt_target_crtbegin_file := $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin.c
-libc_crt_target_crtbegin_so_file := $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtbegin_so.c
 
 # See the comment in crtbrand.c for the reason why we need to generate
 # crtbrand.s before generating crtbrand.o.
@@ -687,7 +689,7 @@
 ALL_GENERATED_SOURCES += $(GEN)
 
 GEN := $(TARGET_OUT_INTERMEDIATE_LIBRARIES)/crtend_so.o
-$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend_so.S
+$(GEN): $(LOCAL_PATH)/arch-common/bionic/crtend_so.S
 	@mkdir -p $(dir $@)
 	$(hide) $(TARGET_CC) $(libc_crt_target_so_cflags) \
 		-MD -MF $(@:%.o=%.d) -o $@ -c $<
@@ -740,7 +742,7 @@
 # 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
-$(GEN): $(LOCAL_PATH)/arch-$(TARGET_ARCH)/bionic/crtend.S
+$(GEN): $(LOCAL_PATH)/arch-common/bionic/crtend.S
 	@mkdir -p $(dir $@)
 	$(hide) $(TARGET_CC) $(libc_crt_target_cflags) \
 		-MD -MF $(@:%.o=%.d) -o $@ -c $<
@@ -979,10 +981,10 @@
 	LOCAL_CFLAGS += -DCRT_LEGACY_WORKAROUND
 
 	LOCAL_SRC_FILES := \
-		arch-arm/bionic/crtbegin_so.c \
+		arch-common/bionic/crtbegin_so.c \
 		arch-arm/bionic/atexit_legacy.c \
 		$(LOCAL_SRC_FILES) \
-		arch-arm/bionic/crtend_so.S
+		arch-common/bionic/crtend_so.S
 endif
 
 LOCAL_MODULE:= libc
diff --git a/libc/arch-arm/bionic/crtbegin.c b/libc/arch-arm/bionic/crtbegin.c
deleted file mode 100644
index 1bcb335..0000000
--- a/libc/arch-arm/bionic/crtbegin.c
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * 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.
- */
-
-#include "../../bionic/libc_init_common.h"
-#include <stddef.h>
-#include <stdint.h>
-
-__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;
-
-__LIBC_HIDDEN__ void _start() {
-  structors_array_t array;
-  array.preinit_array = &__PREINIT_ARRAY__;
-  array.init_array = &__INIT_ARRAY__;
-  array.fini_array = &__FINI_ARRAY__;
-
-  void* raw_args = (void*) ((uintptr_t) __builtin_frame_address(0) + sizeof(void*));
-  __libc_init(raw_args, NULL, &main, &array);
-}
-
-#include "__dso_handle.h"
-#include "atexit.h"
diff --git a/libc/arch-arm/bionic/crtend_so.S b/libc/arch-arm/bionic/crtend_so.S
deleted file mode 100644
index bb6570e..0000000
--- a/libc/arch-arm/bionic/crtend_so.S
+++ /dev/null
@@ -1,31 +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.
- */
-
-#if defined(__linux__) && defined(__ELF__)
-	.section .note.GNU-stack,"",%progbits
-#endif
diff --git a/libc/arch-mips/bionic/crtend.S b/libc/arch-common/bionic/asm_multiarch.h
similarity index 86%
rename from libc/arch-mips/bionic/crtend.S
rename to libc/arch-common/bionic/asm_multiarch.h
index 6984d0e..32c7e5b 100644
--- a/libc/arch-mips/bionic/crtend.S
+++ b/libc/arch-common/bionic/asm_multiarch.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,13 +25,12 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-	
-	.section .preinit_array, "aw"
-	.long 0
 
-	.section .init_array, "aw"
-	.long 0
-
-	.section .fini_array, "aw"
-	.long 0
+#ifdef __LP64__
+# define ASM_PTR_SIZE(x) .quad x
+# define ASM_ALIGN(x)    .align 4
+#else
+# define ASM_PTR_SIZE(x) .long x
+# define ASM_ALIGN(x)
+#endif
 
diff --git a/libc/arch-arm/bionic/atexit.h b/libc/arch-common/bionic/atexit.h
similarity index 100%
rename from libc/arch-arm/bionic/atexit.h
rename to libc/arch-common/bionic/atexit.h
diff --git a/libc/arch-x86/bionic/crtbegin.c b/libc/arch-common/bionic/crtbegin.c
similarity index 95%
rename from libc/arch-x86/bionic/crtbegin.c
rename to libc/arch-common/bionic/crtbegin.c
index 43e9306..bc67bfb 100644
--- a/libc/arch-x86/bionic/crtbegin.c
+++ b/libc/arch-common/bionic/crtbegin.c
@@ -40,7 +40,9 @@
 void (*__FINI_ARRAY__)(void) = (void (*)(void)) -1;
 
 __LIBC_HIDDEN__
+#ifdef __i386__
 __attribute__((force_align_arg_pointer))
+#endif
 void _start() {
   structors_array_t array;
   array.preinit_array = &__PREINIT_ARRAY__;
@@ -53,4 +55,6 @@
 
 #include "__dso_handle.h"
 #include "atexit.h"
-#include "__stack_chk_fail_local.h"
+#ifdef __i386__
+# include "../../arch-x86/bionic/__stack_chk_fail_local.h"
+#endif
diff --git a/libc/arch-arm/bionic/crtbegin_so.c b/libc/arch-common/bionic/crtbegin_so.c
similarity index 93%
rename from libc/arch-arm/bionic/crtbegin_so.c
rename to libc/arch-common/bionic/crtbegin_so.c
index cd0257a..bd70edf 100644
--- a/libc/arch-arm/bionic/crtbegin_so.c
+++ b/libc/arch-common/bionic/crtbegin_so.c
@@ -51,8 +51,12 @@
  */
 
 #ifdef CRT_LEGACY_WORKAROUND
-#include "__dso_handle.h"
+# include "__dso_handle.h"
 #else
-#include "__dso_handle_so.h"
-#include "atexit.h"
+# include "__dso_handle_so.h"
+# include "atexit.h"
 #endif
+#ifdef __i386__
+# include "../../arch-x86/bionic/__stack_chk_fail_local.h"
+#endif
+
diff --git a/libc/arch-arm/bionic/crtend.S b/libc/arch-common/bionic/crtend.S
similarity index 85%
rename from libc/arch-arm/bionic/crtend.S
rename to libc/arch-common/bionic/crtend.S
index fffb0ab..a4cf8de 100644
--- a/libc/arch-arm/bionic/crtend.S
+++ b/libc/arch-common/bionic/crtend.S
@@ -25,16 +25,26 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-	
+
+#include "asm_multiarch.h"
+
 	.section .preinit_array, "aw"
-	.long 0
+	ASM_PTR_SIZE(0)
 
 	.section .init_array, "aw"
-	.long 0
+	ASM_PTR_SIZE(0)
 
 	.section .fini_array, "aw"
-	.long 0
+	ASM_PTR_SIZE(0)
 
 #if defined(__linux__) && defined(__ELF__)
 	.section .note.GNU-stack,"",%progbits
 #endif
+#if defined(__i386__) || defined(__x86_64__)
+	.section	.eh_frame,"a",@progbits
+	ASM_ALIGN(4)
+	.type	__FRAME_END__, @object
+	.size	__FRAME_END__, 4
+__FRAME_END__:
+	.zero	4
+#endif
diff --git a/libc/arch-arm/bionic/crtend.S b/libc/arch-common/bionic/crtend_so.S
similarity index 81%
copy from libc/arch-arm/bionic/crtend.S
copy to libc/arch-common/bionic/crtend_so.S
index fffb0ab..f745109 100644
--- a/libc/arch-arm/bionic/crtend.S
+++ b/libc/arch-common/bionic/crtend_so.S
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2008 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -25,16 +25,25 @@
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-	
-	.section .preinit_array, "aw"
-	.long 0
 
+#include "asm_multiarch.h"
+
+#ifndef __arm__
 	.section .init_array, "aw"
-	.long 0
+	ASM_PTR_SIZE(0)
 
 	.section .fini_array, "aw"
-	.long 0
+	ASM_PTR_SIZE(0)
+#endif
 
 #if defined(__linux__) && defined(__ELF__)
 	.section .note.GNU-stack,"",%progbits
 #endif
+#if defined(__i386__) || defined(__x86_64__)
+	.section	.eh_frame,"a",@progbits
+	ASM_ALIGN(4)
+	.type	__FRAME_END__, @object
+	.size	__FRAME_END__, 4
+__FRAME_END__:
+	.zero	4
+#endif
diff --git a/libc/arch-mips/bionic/crtend_so.S b/libc/arch-mips/bionic/crtend_so.S
deleted file mode 100644
index f09c427..0000000
--- a/libc/arch-mips/bionic/crtend_so.S
+++ /dev/null
@@ -1,5 +0,0 @@
-	.section .init_array, "aw"
-	.long 0
-
-	.section .fini_array, "aw"
-	.long 0
diff --git a/libc/arch-x86/bionic/crtbegin_so.c b/libc/arch-x86/bionic/crtbegin_so.c
deleted file mode 100644
index 30de6af..0000000
--- a/libc/arch-x86/bionic/crtbegin_so.c
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * 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/bionic/crtend.S b/libc/arch-x86/bionic/crtend.S
deleted file mode 100644
index 68447e7..0000000
--- a/libc/arch-x86/bionic/crtend.S
+++ /dev/null
@@ -1,15 +0,0 @@
-	.section .preinit_array, "aw"
-	.long 0
-
-	.section .init_array, "aw"
-	.long 0
-
-	.section .fini_array, "aw"
-	.long 0
-
-	.section	.eh_frame,"a",@progbits
-	.align 4
-	.type	__FRAME_END__, @object
-	.size	__FRAME_END__, 4
-__FRAME_END__:
-	.zero	4
diff --git a/libc/arch-x86/bionic/crtend_so.S b/libc/arch-x86/bionic/crtend_so.S
deleted file mode 100644
index 63e58b9..0000000
--- a/libc/arch-x86/bionic/crtend_so.S
+++ /dev/null
@@ -1,12 +0,0 @@
-.section .init_array, "aw"
-    .long 0
-
-.section .fini_array, "aw"
-    .long 0
-
-	.section	.eh_frame,"a",@progbits
-	.align 4
-	.type	__FRAME_END__, @object
-	.size	__FRAME_END__, 4
-__FRAME_END__:
-	.zero	4