OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 package org.chromium.base.library_loader; | 5 package org.chromium.base.library_loader; |
6 | 6 |
| 7 import android.os.Build; |
7 import android.os.Bundle; | 8 import android.os.Bundle; |
8 import android.os.Parcel; | 9 import android.os.Parcel; |
9 import android.os.ParcelFileDescriptor; | 10 import android.os.ParcelFileDescriptor; |
10 import android.os.Parcelable; | 11 import android.os.Parcelable; |
11 | 12 |
12 import org.chromium.base.Log; | 13 import org.chromium.base.Log; |
13 import org.chromium.base.annotations.AccessedByNative; | 14 import org.chromium.base.annotations.AccessedByNative; |
14 | 15 |
15 import java.util.HashMap; | 16 import java.util.HashMap; |
16 import java.util.Locale; | 17 import java.util.Locale; |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 // Singleton. | 217 // Singleton. |
217 private static Linker sSingleton = null; | 218 private static Linker sSingleton = null; |
218 private static Object sSingletonLock = new Object(); | 219 private static Object sSingletonLock = new Object(); |
219 | 220 |
220 // Protected singleton constructor. | 221 // Protected singleton constructor. |
221 protected Linker() {} | 222 protected Linker() {} |
222 | 223 |
223 /** | 224 /** |
224 * Get singleton instance. Returns either a LegacyLinker or a ModernLinker. | 225 * Get singleton instance. Returns either a LegacyLinker or a ModernLinker. |
225 * | 226 * |
| 227 * Returns a ModernLinker if running on Android M or later, otherwise return
s |
| 228 * a LegacyLinker. |
| 229 * |
| 230 * ModernLinker requires OS features from Android M and later: a system link
er |
| 231 * that handles packed relocations and load from APK, and android_dlopen_ext
() |
| 232 * for shared RELRO support. It cannot run on Android releases earlier than
M. |
| 233 * |
| 234 * LegacyLinker runs on all Android releases but it is slower and more compl
ex |
| 235 * than ModernLinker, so ModernLinker is preferred for Android M and later. |
| 236 * |
226 * @return the Linker implementation instance. | 237 * @return the Linker implementation instance. |
227 */ | 238 */ |
228 public static final Linker getInstance() { | 239 public static final Linker getInstance() { |
229 synchronized (sSingletonLock) { | 240 synchronized (sSingletonLock) { |
230 if (sSingleton == null) { | 241 if (sSingleton == null) { |
231 // TODO(simonb): Check version once the Android M build version | 242 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
232 // code becomes available. | |
233 // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.<ANDROID-M>)
{ | |
234 if (false) { | |
235 sSingleton = ModernLinker.create(); | 243 sSingleton = ModernLinker.create(); |
236 } else { | 244 } else { |
237 sSingleton = LegacyLinker.create(); | 245 sSingleton = LegacyLinker.create(); |
238 } | 246 } |
239 Log.i(TAG, "Using linker: " + sSingleton.getClass().getName()); | 247 Log.i(TAG, "Using linker: " + sSingleton.getClass().getName()); |
240 } | 248 } |
241 return sSingleton; | 249 return sSingleton; |
242 } | 250 } |
243 } | 251 } |
244 | 252 |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 public boolean isChromiumLinkerLibrary(String library) { | 465 public boolean isChromiumLinkerLibrary(String library) { |
458 return library.equals(LINKER_JNI_LIBRARY) || library.equals(LINKER_JNI_L
IBRARY + ".cr"); | 466 return library.equals(LINKER_JNI_LIBRARY) || library.equals(LINKER_JNI_L
IBRARY + ".cr"); |
459 } | 467 } |
460 | 468 |
461 /** | 469 /** |
462 * Load the Linker JNI library. Throws UnsatisfiedLinkError on error. | 470 * Load the Linker JNI library. Throws UnsatisfiedLinkError on error. |
463 * In a component build, the suffix ".cr" is added to each library name, so | 471 * In a component build, the suffix ".cr" is added to each library name, so |
464 * if the initial load fails we retry with a suffix. | 472 * if the initial load fails we retry with a suffix. |
465 */ | 473 */ |
466 protected void loadLinkerJNILibrary() { | 474 protected void loadLinkerJNILibrary() { |
467 String lib_name = "lib" + LINKER_JNI_LIBRARY + ".so"; | 475 String libName = "lib" + LINKER_JNI_LIBRARY + ".so"; |
468 if (DEBUG) { | 476 if (DEBUG) { |
469 Log.i(TAG, "Loading " + lib_name); | 477 Log.i(TAG, "Loading " + libName); |
470 } | 478 } |
471 try { | 479 try { |
472 System.loadLibrary(LINKER_JNI_LIBRARY); | 480 System.loadLibrary(LINKER_JNI_LIBRARY); |
473 } catch (UnsatisfiedLinkError e) { | 481 } catch (UnsatisfiedLinkError e) { |
474 Log.w(TAG, "Couldn't load " + lib_name + ", trying " + lib_name + ".
so"); | 482 Log.w(TAG, "Couldn't load " + libName + ", trying " + libName + ".so
"); |
475 System.loadLibrary(LINKER_JNI_LIBRARY + ".cr"); | 483 System.loadLibrary(LINKER_JNI_LIBRARY + ".cr"); |
476 } | 484 } |
477 } | 485 } |
478 | 486 |
479 /** | 487 /** |
480 * Obtain a random base load address at which to place loaded libraries. | 488 * Obtain a random base load address at which to place loaded libraries. |
481 * | 489 * |
482 * @return new base load address | 490 * @return new base load address |
483 */ | 491 */ |
484 protected long getRandomBaseLoadAddress() { | 492 protected long getRandomBaseLoadAddress() { |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 * Return a random address that should be free to be mapped with the given s
ize. | 771 * Return a random address that should be free to be mapped with the given s
ize. |
764 * Maps an area large enough for the largest library we might attempt to loa
d, | 772 * Maps an area large enough for the largest library we might attempt to loa
d, |
765 * and if successful then unmaps it and returns the address of the area allo
cated | 773 * and if successful then unmaps it and returns the address of the area allo
cated |
766 * by the system (with ASLR). The idea is that this area should remain free
of | 774 * by the system (with ASLR). The idea is that this area should remain free
of |
767 * other mappings until we map our library into it. | 775 * other mappings until we map our library into it. |
768 * | 776 * |
769 * @return address to pass to future mmap, or 0 on error. | 777 * @return address to pass to future mmap, or 0 on error. |
770 */ | 778 */ |
771 private static native long nativeGetRandomBaseLoadAddress(); | 779 private static native long nativeGetRandomBaseLoadAddress(); |
772 } | 780 } |
OLD | NEW |