[MIPS] Rewrite MIPS crtbegin* as C files.
This updates the MIPS arch to be much more in
sync with the commit Nick Kralevich made last
June; see 9d40326830c2bd407427889c554adeb915ee6b4a.
Rewrite
crtbegin.S -> crtbegin.c
crtbegin_so.S -> crtbegin_so.c
__dso_handle.S -> __dso_handle.c
__dso_handle_so.S -> __dso_handle_so.c
atexit.S -> atexit.c
Previously __do_global_dtors_aux was in the tasks
__FINI_ARRAY__ linked with crtbegin.S and it now being
removed as there is no need to call a destructor just
before terminating a process.
Shared libraries, on the other hand, are linked with
crtbegin_so.c and have a hidden destructor declared
to allow the bionic linker to call __on_dlclose().
Change-Id: Ieb4da5199b54573de05743990e309db381a11cb8
Signed-off-by: Pete Delaney <piet.delaney@imgtec.com>
Signed-off-by: Chao-Ying Fu <chao-ying.fu@imgtec.com>
Signed-off-by: Chris Dearman <chris.dearman@imgtec.com>
diff --git a/libc/Android.mk b/libc/Android.mk
index 0e93acd..4206020 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -573,7 +573,7 @@
libc_crt_target_so_cflags :=
endif
ifeq ($(TARGET_ARCH),mips)
- libc_crtbegin_extension := S
+ libc_crtbegin_extension := c
libc_crt_target_so_cflags := -fPIC
endif
ifeq ($(TARGET_ARCH),x86)
diff --git a/libc/arch-mips/bionic/__dso_handle.S b/libc/arch-mips/bionic/__dso_handle.S
index 3e80128..63a4d3f 100644
--- a/libc/arch-mips/bionic/__dso_handle.S
+++ b/libc/arch-mips/bionic/__dso_handle.S
@@ -33,9 +33,7 @@
.section .bss
.align 4
-#ifndef CRT_LEGACY_WORKAROUND
.hidden __dso_handle
-#endif
.globl __dso_handle
__dso_handle:
diff --git a/libc/arch-mips/bionic/atexit.S b/libc/arch-mips/bionic/atexit.h
similarity index 81%
rename from libc/arch-mips/bionic/atexit.S
rename to libc/arch-mips/bionic/atexit.h
index 7f0c820..759008c 100644
--- a/libc/arch-mips/bionic/atexit.S
+++ b/libc/arch-mips/bionic/atexit.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,19 +25,12 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
- .text
- .globl atexit
- .hidden atexit
- .type atexit, @function
- .align 4
- .ent atexit
-atexit:
- .set noreorder
- .cpload $t9
- .set reorder
- la $t9, __cxa_atexit
- move $a1, $0
- la $a2, __dso_handle
- j $t9
- .size atexit, .-atexit
- .end atexit
+
+extern void *__dso_handle;
+extern int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
+
+__attribute__ ((visibility ("hidden")))
+int atexit(void (*func)(void))
+{
+ return (__cxa_atexit((void (*)(void *))func, (void *)0, &__dso_handle));
+}
diff --git a/libc/arch-mips/bionic/crtbegin.S b/libc/arch-mips/bionic/crtbegin.S
deleted file mode 100644
index 40b689e..0000000
--- a/libc/arch-mips/bionic/crtbegin.S
+++ /dev/null
@@ -1,146 +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
- .align 4
- .type __start,@function
- .globl __start
- .globl _start
-
-# this is the small startup code that is first run when
-# any executable that is statically-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.
-#
-# - address of the constructor list
-#
-
- .ent __start
-__start:
-_start:
- bal 1f
-1:
- .set noreorder
- .cpload $ra
- .set reorder
-
- move $a0, $sp
- move $a1, $0
- la $a2, main
- la $a3, 1f
- subu $sp, 32
- la $t9, __libc_init
- j $t9
- .end __start
-
-1: .long __PREINIT_ARRAY__
- .long __INIT_ARRAY__
- .long __FINI_ARRAY__
-
- .section .preinit_array, "aw"
- .type __PREINIT_ARRAY__, @object
- .globl __PREINIT_ARRAY__
-__PREINIT_ARRAY__:
- .long -1
-
- .section .init_array, "aw"
- .type __INIT_ARRAY__, @object
- .globl __INIT_ARRAY__
-__INIT_ARRAY__:
- .long -1
-
- .section .fini_array, "aw"
- .type __FINI_ARRAY__, @object
- .globl __FINI_ARRAY__
-__FINI_ARRAY__:
- .long -1
- .long __do_global_dtors_aux
-
- .abicalls
- .text
- .align 2
- .set nomips16
- .ent __do_global_dtors_aux
- .type __do_global_dtors_aux, @function
-__do_global_dtors_aux:
- .frame $sp,32,$31 # vars= 0, regs= 1/0, args= 16, gp= 8
- .mask 0x80000000,-4
- .fmask 0x00000000,0
- .set noreorder
- .cpload $25
- .set nomacro
- addiu $sp,$sp,-32
- sw $31,28($sp)
- .cprestore 16
- lw $2,%got(completed.1269)($28)
- lbu $2,%lo(completed.1269)($2)
- bne $2,$0,$L8
- nop
-
-$L4:
- lw $2,%got(__cxa_finalize)($28)
- beq $2,$0,$L6
- nop
-
- lw $2,%got(__dso_handle)($28)
- lw $4,0($2)
- lw $25,%call16(__cxa_finalize)($28)
- .reloc 1f,R_MIPS_JALR,__cxa_finalize
-1: jalr $25
- nop
-
- lw $28,16($sp)
-$L6:
- lw $2,%got(completed.1269)($28)
- li $3,1 # 0x1
- sb $3,%lo(completed.1269)($2)
-$L8:
- lw $31,28($sp)
- addiu $sp,$sp,32
- j $31
- nop
-
- .set macro
- .set reorder
- .end __do_global_dtors_aux
- .size __do_global_dtors_aux, .-__do_global_dtors_aux
- .local completed.1269
- .comm completed.1269,1,1
- .weak __cxa_finalize
-
-#include "__dso_handle.S"
-#include "atexit.S"
diff --git a/libc/arch-mips/bionic/crtbegin.c b/libc/arch-mips/bionic/crtbegin.c
new file mode 100644
index 0000000..d3a3401
--- /dev/null
+++ b/libc/arch-mips/bionic/crtbegin.c
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2013 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 do_mips_start(void *raw_args) {
+ structors_array_t array;
+ array.preinit_array = &__PREINIT_ARRAY__;
+ array.init_array = &__INIT_ARRAY__;
+ array.fini_array = &__FINI_ARRAY__;
+
+ __libc_init(raw_args, NULL, &main, &array);
+}
+
+/*
+ * This function prepares the return address with a branch-and-link
+ * instruction (bal) and then uses a .cpload to compute the Global
+ * Offset Table (GOT) pointer ($gp). The $gp is then used to load
+ * the address of _do_start() into $t9 just before calling it.
+ * Terminating the stack with a NULL return address.
+ */
+__asm__ (
+" .set push \n"
+" \n"
+" .text \n"
+" .align 4 \n"
+" .type __start,@function \n"
+" .globl __start \n"
+" .globl _start \n"
+" \n"
+" .ent __start \n"
+"__start: \n"
+" _start: \n"
+" .frame $sp,32,$ra \n"
+" .mask 0x80000000,-4 \n"
+" \n"
+" .set noreorder \n"
+" bal 1f \n"
+" nop \n"
+"1: \n"
+" .cpload $ra \n"
+" .set reorder \n"
+" \n"
+" move $a0, $sp \n"
+" addiu $sp, $sp, (-32) \n"
+" sw $0, 28($sp) \n"
+" la $t9, do_mips_start \n"
+" jalr $t9 \n"
+" \n"
+"2: b 2b \n"
+" .end __start \n"
+" \n"
+" .set pop \n"
+);
+
+#include "__dso_handle.h"
+#include "atexit.h"
diff --git a/libc/arch-mips/bionic/crtbegin_so.S b/libc/arch-mips/bionic/crtbegin_so.S
deleted file mode 100644
index 377888a..0000000
--- a/libc/arch-mips/bionic/crtbegin_so.S
+++ /dev/null
@@ -1,94 +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"
- .type __INIT_ARRAY__, @object
- .globl __INIT_ARRAY__
-__INIT_ARRAY__:
- .long -1
-
- .section .fini_array, "aw"
- .type __FINI_ARRAY__, @object
- .globl __FINI_ARRAY__
-__FINI_ARRAY__:
- .long -1
- .long __do_global_dtors_aux
-
- .abicalls
- .text
- .align 2
- .set nomips16
- .ent __do_global_dtors_aux
- .type __do_global_dtors_aux, @function
-__do_global_dtors_aux:
- .frame $sp,32,$31 # vars= 0, regs= 1/0, args= 16, gp= 8
- .mask 0x80000000,-4
- .fmask 0x00000000,0
- .set noreorder
- .cpload $25
- .set nomacro
- addiu $sp,$sp,-32
- sw $31,28($sp)
- .cprestore 16
- lw $2,%got(completed.1269)($28)
- lbu $2,%lo(completed.1269)($2)
- bne $2,$0,$L8
- nop
-
-$L4:
- lw $2,%got(__cxa_finalize)($28)
- beq $2,$0,$L6
- nop
-
- lw $2,%got(__dso_handle)($28)
- lw $4,0($2)
- lw $25,%call16(__cxa_finalize)($28)
- .reloc 1f,R_MIPS_JALR,__cxa_finalize
-1: jalr $25
- nop
-
- lw $28,16($sp)
-$L6:
- lw $2,%got(completed.1269)($28)
- li $3,1 # 0x1
- sb $3,%lo(completed.1269)($2)
-$L8:
- lw $31,28($sp)
- addiu $sp,$sp,32
- j $31
- nop
-
- .set macro
- .set reorder
- .end __do_global_dtors_aux
- .size __do_global_dtors_aux, .-__do_global_dtors_aux
- .local completed.1269
- .comm completed.1269,1,1
- .weak __cxa_finalize
-
-#include "__dso_handle_so.S"
-#include "atexit.S"
diff --git a/libc/arch-mips/bionic/atexit.S b/libc/arch-mips/bionic/crtbegin_so.c
similarity index 81%
copy from libc/arch-mips/bionic/atexit.S
copy to libc/arch-mips/bionic/crtbegin_so.c
index 7f0c820..925dc8c 100644
--- a/libc/arch-mips/bionic/atexit.S
+++ b/libc/arch-mips/bionic/crtbegin_so.c
@@ -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,19 +25,14 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
- .text
- .globl atexit
- .hidden atexit
- .type atexit, @function
- .align 4
- .ent atexit
-atexit:
- .set noreorder
- .cpload $t9
- .set reorder
- la $t9, __cxa_atexit
- move $a1, $0
- la $a2, __dso_handle
- j $t9
- .size atexit, .-atexit
- .end atexit
+
+extern void __cxa_finalize(void *);
+extern void *__dso_handle;
+
+__attribute__((visibility("hidden"),destructor))
+void __on_dlclose() {
+ __cxa_finalize(&__dso_handle);
+}
+
+#include "__dso_handle_so.h"
+#include "atexit.h"