| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This is the Android-specific Chromium linker, a tiny shared library | 5 // This is the Android-specific Chromium linker, a tiny shared library |
| 6 // implementing a custom dynamic linker that can be used to load the | 6 // implementing a custom dynamic linker that can be used to load the |
| 7 // real Chromium libraries (e.g. libcontentshell.so). | 7 // real Chromium libraries (e.g. libcontentshell.so). |
| 8 | 8 |
| 9 // The main point of this linker is to be able to share the RELRO | 9 // The main point of this linker is to be able to share the RELRO |
| 10 // section of libcontentshell.so (or equivalent) between the browser and | 10 // section of libcontentshell.so (or equivalent) between the browser and |
| 11 // renderer process. | 11 // renderer process. |
| 12 | 12 |
| 13 // This source code *cannot* depend on anything from base/ or the C++ | 13 // This source code *cannot* depend on anything from base/ or the C++ |
| 14 // STL, to keep the final library small, and avoid ugly dependency issues. | 14 // STL, to keep the final library small, and avoid ugly dependency issues. |
| 15 | 15 |
| 16 #include <android/log.h> | 16 #include <android/log.h> |
| 17 #include <crazy_linker.h> | 17 #include <crazy_linker.h> |
| 18 #include <fcntl.h> | 18 #include <fcntl.h> |
| 19 #include <jni.h> | 19 #include <jni.h> |
| 20 #include <limits.h> | 20 #include <limits.h> |
| 21 #include <stdlib.h> | 21 #include <stdlib.h> |
| 22 #include <sys/mman.h> | 22 #include <sys/mman.h> |
| 23 #include <unistd.h> | 23 #include <unistd.h> |
| 24 | 24 |
| 25 // See commentary in crazy_linker_elf_loader.cpp for the effect of setting |
| 26 // this. If changing there, change here also. |
| 27 // |
| 28 // For more, see: |
| 29 // https://crbug.com/504410 |
| 30 #define RESERVE_BREAKPAD_GUARD_REGION 1 |
| 31 |
| 25 // Set this to 1 to enable debug traces to the Android log. | 32 // Set this to 1 to enable debug traces to the Android log. |
| 26 // Note that LOG() from "base/logging.h" cannot be used, since it is | 33 // Note that LOG() from "base/logging.h" cannot be used, since it is |
| 27 // in base/ which hasn't been loaded yet. | 34 // in base/ which hasn't been loaded yet. |
| 28 #define DEBUG 0 | 35 #define DEBUG 0 |
| 29 | 36 |
| 30 #define TAG "chromium_android_linker" | 37 #define TAG "chromium_android_linker" |
| 31 | 38 |
| 32 #if DEBUG | 39 #if DEBUG |
| 33 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) | 40 #define LOG_INFO(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__) |
| 34 #else | 41 #else |
| (...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 lib_name.c_str()); | 626 lib_name.c_str()); |
| 620 | 627 |
| 621 return true; | 628 return true; |
| 622 } | 629 } |
| 623 | 630 |
| 624 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { | 631 jboolean CanUseSharedRelro(JNIEnv* env, jclass clazz) { |
| 625 return crazy_system_can_share_relro(); | 632 return crazy_system_can_share_relro(); |
| 626 } | 633 } |
| 627 | 634 |
| 628 jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz, jlong bytes) { | 635 jlong GetRandomBaseLoadAddress(JNIEnv* env, jclass clazz, jlong bytes) { |
| 636 #if RESERVE_BREAKPAD_GUARD_REGION |
| 637 // Add a Breakpad guard region. 16Mb should be comfortably larger than |
| 638 // the largest relocation packer saving we expect to encounter. |
| 639 static const size_t kBreakpadGuardRegionBytes = 16 * 1024 * 1024; |
| 640 bytes += kBreakpadGuardRegionBytes; |
| 641 #endif |
| 642 |
| 629 void* address = | 643 void* address = |
| 630 mmap(NULL, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | 644 mmap(NULL, bytes, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 631 if (address == MAP_FAILED) { | 645 if (address == MAP_FAILED) { |
| 632 LOG_INFO("%s: Random base load address not determinable\n", __FUNCTION__); | 646 LOG_INFO("%s: Random base load address not determinable\n", __FUNCTION__); |
| 633 return 0; | 647 return 0; |
| 634 } | 648 } |
| 635 munmap(address, bytes); | 649 munmap(address, bytes); |
| 650 |
| 651 #if RESERVE_BREAKPAD_GUARD_REGION |
| 652 // Allow for a Breakpad guard region ahead of the returned address. |
| 653 address = reinterpret_cast<void*>( |
| 654 reinterpret_cast<uintptr_t>(address) + kBreakpadGuardRegionBytes); |
| 655 #endif |
| 656 |
| 636 LOG_INFO("%s: Random base load address is %p\n", __FUNCTION__, address); | 657 LOG_INFO("%s: Random base load address is %p\n", __FUNCTION__, address); |
| 637 return static_cast<jlong>(reinterpret_cast<uintptr_t>(address)); | 658 return static_cast<jlong>(reinterpret_cast<uintptr_t>(address)); |
| 638 } | 659 } |
| 639 | 660 |
| 640 // Get the full path of a library in the zip file | 661 // Get the full path of a library in the zip file |
| 641 // (lib/<abi>/crazy.<lib_name>). | 662 // (lib/<abi>/crazy.<lib_name>). |
| 642 // | 663 // |
| 643 // |env| is the current JNI environment handle. | 664 // |env| is the current JNI environment handle. |
| 644 // |clazz| is the static class handle which is not used here. | 665 // |clazz| is the static class handle which is not used here. |
| 645 // |lib_name| is the library base name. | 666 // |lib_name| is the library base name. |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 crazy_context_t* context = GetCrazyContext(); | 815 crazy_context_t* context = GetCrazyContext(); |
| 795 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); | 816 crazy_context_set_java_vm(context, vm, JNI_VERSION_1_4); |
| 796 | 817 |
| 797 // Register the function that the crazy linker can call to post code | 818 // Register the function that the crazy linker can call to post code |
| 798 // for later execution. | 819 // for later execution. |
| 799 crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL); | 820 crazy_context_set_callback_poster(context, &PostForLaterExecution, NULL); |
| 800 | 821 |
| 801 LOG_INFO("%s: Done", __FUNCTION__); | 822 LOG_INFO("%s: Done", __FUNCTION__); |
| 802 return JNI_VERSION_1_4; | 823 return JNI_VERSION_1_4; |
| 803 } | 824 } |
| OLD | NEW |