Replace .S version of x86 crtfiles with .c version

This patch replaces .S versions of x86 crtfiles with .c which are much
easier to support. Some of the files are matching .c version of Arm
crtfiles. x86 files required some cleanup anyway and this cleanup actually
led to matching Arm files.

I didn't change anything to share the same crt*.c between x86 and Arm. I
prefer to keep them separate for a while in case any change is required
for one of the arch, but it's good thing to do in the following patches.

Change-Id: Ibcf033f8d15aa5b10c05c879fd4b79a64dfc70f3
Signed-off-by: Pavel Chupin <pavel.v.chupin@intel.com>
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"