michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include michael@0: michael@0: /* The Android NDK headers define those */ michael@0: #undef Elf_Ehdr michael@0: #undef Elf_Addr michael@0: michael@0: #if defined(__LP64__) michael@0: #define Elf_Ehdr Elf64_Ehdr michael@0: #define Elf_Addr Elf64_Addr michael@0: #else michael@0: #define Elf_Ehdr Elf32_Ehdr michael@0: #define Elf_Addr Elf32_Addr michael@0: #endif michael@0: michael@0: extern __attribute__((visibility("hidden"))) void original_init(int argc, char **argv, char **env); michael@0: michael@0: extern __attribute__((visibility("hidden"))) Elf32_Rel relhack[]; michael@0: extern __attribute__((visibility("hidden"))) Elf_Ehdr elf_header; michael@0: michael@0: static inline __attribute__((always_inline)) michael@0: void do_relocations(void) michael@0: { michael@0: Elf32_Rel *rel; michael@0: Elf_Addr *ptr, *start; michael@0: for (rel = relhack; rel->r_offset; rel++) { michael@0: start = (Elf_Addr *)((intptr_t)&elf_header + rel->r_offset); michael@0: for (ptr = start; ptr < &start[rel->r_info]; ptr++) michael@0: *ptr += (intptr_t)&elf_header; michael@0: } michael@0: } michael@0: michael@0: __attribute__((section(".text._init_noinit"))) michael@0: int init_noinit(int argc, char **argv, char **env) michael@0: { michael@0: do_relocations(); michael@0: return 0; michael@0: } michael@0: michael@0: __attribute__((section(".text._init"))) michael@0: int init(int argc, char **argv, char **env) michael@0: { michael@0: do_relocations(); michael@0: original_init(argc, argv, env); michael@0: // Ensure there is no tail-call optimization, avoiding the use of the michael@0: // B.W instruction in Thumb for the call above. michael@0: return 0; michael@0: }