1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
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
7 // real Chromium libraries.
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
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.
16 #ifndef BASE_ANDROID_LINKER_LINKER_JNI_H_
17 #define BASE_ANDROID_LINKER_LINKER_JNI_H_
19 #include <android/log.h>
23 // Set this to 1 to enable debug traces to the Android log.
24 // Note that LOG() from "base/logging.h" cannot be used, since it is
25 // in base/ which hasn't been loaded yet.
28 #define TAG "cr.chromium_android_linker"
31 #define LOG_INFO(FORMAT, ...) \
32 __android_log_print(ANDROID_LOG_INFO, TAG, \
33 "%s: " FORMAT, __FUNCTION__, ##__VA_ARGS__)
35 #define LOG_INFO(FORMAT, ...) ((void)0)
37 #define LOG_ERROR(FORMAT, ...) \
38 __android_log_print(ANDROID_LOG_ERROR, TAG, \
39 "%s: " FORMAT, __FUNCTION__, ##__VA_ARGS__)
41 #define UNUSED __attribute__((unused))
43 // See commentary in crazy_linker_elf_loader.cpp for the effect of setting
44 // this. If changing there, change here also.
47 // https://crbug.com/504410
48 #define RESERVE_BREAKPAD_GUARD_REGION 1
50 namespace chromium_android_linker
{
52 // Larger than the largest library we might attempt to load.
53 static const size_t kAddressSpaceReservationSize
= 192 * 1024 * 1024;
55 // Size of any Breakpad guard region. 16MB is comfortably larger than the
56 // ~6MB relocation packing of the current 64-bit libchrome.so, the largest we
57 // expect to encounter.
58 #if RESERVE_BREAKPAD_GUARD_REGION
59 static const size_t kBreakpadGuardRegionBytes
= 16 * 1024 * 1024;
62 // A simple scoped UTF String class that can be initialized from
63 // a Java jstring handle. Modeled like std::string, which cannot
67 String(JNIEnv
* env
, jstring str
);
69 inline ~String() { ::free(ptr_
); }
71 inline const char* c_str() const { return ptr_
? ptr_
: ""; }
72 inline size_t size() const { return size_
; }
79 // Return true iff |address| is a valid address for the target CPU.
80 inline bool IsValidAddress(jlong address
) {
81 return static_cast<jlong
>(static_cast<size_t>(address
)) == address
;
84 // Find the jclass JNI reference corresponding to a given |class_name|.
85 // |env| is the current JNI environment handle.
86 // On success, return true and set |*clazz|.
87 extern bool InitClassReference(JNIEnv
* env
,
88 const char* class_name
,
91 // Initialize a jfieldID corresponding to the field of a given |clazz|,
92 // with name |field_name| and signature |field_sig|.
93 // |env| is the current JNI environment handle.
94 // On success, return true and set |*field_id|.
95 extern bool InitFieldId(JNIEnv
* env
,
97 const char* field_name
,
98 const char* field_sig
,
101 // Initialize a jmethodID corresponding to the static method of a given
102 // |clazz|, with name |method_name| and signature |method_sig|.
103 // |env| is the current JNI environment handle.
104 // On success, return true and set |*method_id|.
105 extern bool InitStaticMethodId(JNIEnv
* env
,
107 const char* method_name
,
108 const char* method_sig
,
109 jmethodID
* method_id
);
111 // Initialize a jfieldID corresponding to the static field of a given |clazz|,
112 // with name |field_name| and signature |field_sig|.
113 // |env| is the current JNI environment handle.
114 // On success, return true and set |*field_id|.
115 extern bool InitStaticFieldId(JNIEnv
* env
,
117 const char* field_name
,
118 const char* field_sig
,
121 // Initialize a jint corresponding to the static integer field of a class
122 // with class name |class_name| and field name |field_name|.
123 // |env| is the current JNI environment handle.
124 // On success, return true and set |*value|.
125 extern bool InitStaticInt(JNIEnv
* env
,
126 const char* class_name
,
127 const char* field_name
,
130 // Use Android ASLR to create a random library load address.
131 // |env| is the current JNI environment handle, and |clazz| a class.
132 // Returns the address selected by ASLR.
133 extern jlong
GetRandomBaseLoadAddress(JNIEnv
* env
, jclass clazz
);
135 // A class used to model the field IDs of the org.chromium.base.Linker
136 // LibInfo inner class, used to communicate data with the Java side
138 struct LibInfo_class
{
139 jfieldID load_address_id
;
140 jfieldID load_size_id
;
141 jfieldID relro_start_id
;
142 jfieldID relro_size_id
;
143 jfieldID relro_fd_id
;
145 // Initialize an instance.
146 bool Init(JNIEnv
* env
) {
148 if (!InitClassReference(
149 env
, "org/chromium/base/library_loader/Linker$LibInfo", &clazz
)) {
153 return InitFieldId(env
, clazz
, "mLoadAddress", "J", &load_address_id
) &&
154 InitFieldId(env
, clazz
, "mLoadSize", "J", &load_size_id
) &&
155 InitFieldId(env
, clazz
, "mRelroStart", "J", &relro_start_id
) &&
156 InitFieldId(env
, clazz
, "mRelroSize", "J", &relro_size_id
) &&
157 InitFieldId(env
, clazz
, "mRelroFd", "I", &relro_fd_id
);
160 void SetLoadInfo(JNIEnv
* env
,
161 jobject library_info_obj
,
164 env
->SetLongField(library_info_obj
, load_address_id
, load_address
);
165 env
->SetLongField(library_info_obj
, load_size_id
, load_size
);
168 void SetRelroInfo(JNIEnv
* env
,
169 jobject library_info_obj
,
173 env
->SetLongField(library_info_obj
, relro_start_id
, relro_start
);
174 env
->SetLongField(library_info_obj
, relro_size_id
, relro_size
);
175 env
->SetIntField(library_info_obj
, relro_fd_id
, relro_fd
);
178 // Use this instance to convert a RelroInfo reference into
179 // a crazy_library_info_t.
180 void GetRelroInfo(JNIEnv
* env
,
181 jobject library_info_obj
,
186 *relro_start
= static_cast<size_t>(
187 env
->GetLongField(library_info_obj
, relro_start_id
));
192 static_cast<size_t>(env
->GetLongField(library_info_obj
,
197 *relro_fd
= env
->GetIntField(library_info_obj
, relro_fd_id
);
202 // Variable containing LibInfo for the loaded library.
203 extern LibInfo_class s_lib_info_fields
;
205 } // namespace chromium_android_linker
207 #endif // BASE_ANDROID_LINKER_LINKER_JNI_H_