From 5e30a6ae149e670c03ee614acd3fc9f1fb23ef45 Mon Sep 17 00:00:00 2001 From: Andrew Hsieh Date: Mon, 25 Jun 2012 17:56:19 -0700 Subject: [PATCH] ndk: Add crtbrand.c ; crtbegin_static.S includes "atexit.S" for x86/mips "atexit" isn't defined in libc.a for x86/mips (unlike their arm's counterpart). Include atexit.S otherwise app using atexit() may fail to link statically. Also, add crtbegin.c to brand crtbegin_static/dynamic.c with .note.ABI-tag section of given API level. See $NDK/gen-platforms.sh Change-Id: I2fdbb92119e3ca2da2c1ced6d77e63136ec35c34 --- .../android-9/arch-mips/src/crtbegin_static.S | 2 +- .../android-9/arch-x86/src/crtbegin_dynamic.S | 5 +- .../android-9/arch-x86/src/crtbegin_static.S | 3 +- ndk/platforms/common/src/crtbrand.c | 89 +++++++++++++++++++ 4 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 ndk/platforms/common/src/crtbrand.c diff --git a/ndk/platforms/android-9/arch-mips/src/crtbegin_static.S b/ndk/platforms/android-9/arch-mips/src/crtbegin_static.S index 16534e88b..9f00811ea 100644 --- a/ndk/platforms/android-9/arch-mips/src/crtbegin_static.S +++ b/ndk/platforms/android-9/arch-mips/src/crtbegin_static.S @@ -228,4 +228,4 @@ $L3: .weak __register_frame_info .include "__dso_handle.S" -/* NOTE: atexit() is not be provided by crtbegin_static.S, but by libc.a */ +.include "atexit.S" diff --git a/ndk/platforms/android-9/arch-x86/src/crtbegin_dynamic.S b/ndk/platforms/android-9/arch-x86/src/crtbegin_dynamic.S index 177244b70..0ccad70a4 100644 --- a/ndk/platforms/android-9/arch-x86/src/crtbegin_dynamic.S +++ b/ndk/platforms/android-9/arch-x86/src/crtbegin_dynamic.S @@ -60,10 +60,9 @@ _start: mov $0, %edx pushl %edx pushl %eax - call __libc_init + call __libc_init -0: - jmp main +0: jmp main 1: .long __PREINIT_ARRAY__ .long __INIT_ARRAY__ diff --git a/ndk/platforms/android-9/arch-x86/src/crtbegin_static.S b/ndk/platforms/android-9/arch-x86/src/crtbegin_static.S index 4f95b1e40..4fffecdc8 100644 --- a/ndk/platforms/android-9/arch-x86/src/crtbegin_static.S +++ b/ndk/platforms/android-9/arch-x86/src/crtbegin_static.S @@ -134,6 +134,5 @@ frame_dummy: .weak __deregister_frame_info_bases #include "__dso_handle.S" +#include "atexit.S" #include "__stack_chk_fail_local.S" -/* NOTE: atexit() is not be provided by crtbegin_static.S, but by libc.a */ - diff --git a/ndk/platforms/common/src/crtbrand.c b/ndk/platforms/common/src/crtbrand.c new file mode 100644 index 000000000..fa514ff60 --- /dev/null +++ b/ndk/platforms/common/src/crtbrand.c @@ -0,0 +1,89 @@ +/*- + * 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. + */ + +/* The following trick defines int32_t w/o including + We want "int" to be 4-byte. But just in case it isn't, the following + line turns into "typedef int int32_t[0]" and later fails with error messages + read like: + + crtbrand.c:83: error: excess elements in struct initializer + */ +typedef int int32_t[sizeof(int) == 4]; + +#define ABI_VENDOR "GNU" +#define ABI_SECTION ".note.ABI-tag" +#define ABI_NOTETYPE 1 +#define ABI_OS 0 +#define ABI_OS_MAJOR 2 +#define ABI_OS_MINOR 6 +#define ABI_OS_TEENY 15 +#define ABI_ANDROID 1 +#define ABI_ANDROID_API PLATFORM_SDK_VERSION + +/* + * Special ".note" entry specifying the ABI version. See + * http://refspecs.linuxfoundation.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/noteabitag.html + * for more information. + * + * For all arches except sparc, gcc emits the section directive for the + * following struct with a PROGBITS type. However, newer versions of binutils + * (after 2.16.90) require the section to be of NOTE type, to guarantee that the + * .note.ABI-tag section correctly ends up in the first page of the final + * executable. + * + * 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 os; + int32_t major; + int32_t minor; + int32_t teeny; + int32_t os_variant; + int32_t android_api; +} abitag __attribute__ ((section (ABI_SECTION), aligned(4), used)) = { + sizeof ABI_VENDOR, + 6 * sizeof(int32_t), + ABI_NOTETYPE, + ABI_VENDOR, + ABI_OS, + ABI_OS_MAJOR, + ABI_OS_MINOR, + ABI_OS_TEENY, + ABI_ANDROID, + ABI_ANDROID_API, +};