2 * Copyright (C) 2015 The Android-x86 Open Source Project
4 * by Chih-Wei Huang <cwhuang@linux.org.tw>
6 * Licensed under the GNU General Public License Version 2 or later.
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.gnu.org/licenses/gpl.html
14 #define LOG_TAG "libnb"
17 #include <cutils/log.h>
18 #include <cutils/properties.h>
19 #include "nativebridge/native_bridge.h"
23 static void *native_handle = nullptr;
25 static NativeBridgeCallbacks *get_callbacks()
27 static NativeBridgeCallbacks *callbacks = nullptr;
30 const char *libnb = "/system/lib"
36 native_handle = dlopen(libnb, RTLD_LAZY);
38 ALOGE("Unable to open %s", libnb);
42 callbacks = reinterpret_cast<NativeBridgeCallbacks *>(dlsym(native_handle, "NativeBridgeItf"));
47 // NativeBridgeCallbacks implementations
48 static bool native_bridge2_initialize(const NativeBridgeRuntimeCallbacks *art_cbs,
49 const char *app_code_cache_dir,
52 ALOGV("enter native_bridge2_initialize %s %s", app_code_cache_dir, isa);
53 if (property_get_bool("persist.sys.nativebridge", 0)) {
54 if (NativeBridgeCallbacks *cb = get_callbacks()) {
55 return cb->initialize(art_cbs, app_code_cache_dir, isa);
58 ALOGW("Native bridge is disabled");
63 static void *native_bridge2_loadLibrary(const char *libpath, int flag)
65 ALOGV("enter native_bridge2_loadLibrary %s", libpath);
66 NativeBridgeCallbacks *cb = get_callbacks();
67 return cb ? cb->loadLibrary(libpath, flag) : nullptr;
70 static void *native_bridge2_getTrampoline(void *handle, const char *name,
71 const char* shorty, uint32_t len)
73 ALOGV("enter native_bridge2_getTrampoline %s", name);
74 NativeBridgeCallbacks *cb = get_callbacks();
75 return cb ? cb->getTrampoline(handle, name, shorty, len) : nullptr;
78 static bool native_bridge2_isSupported(const char *libpath)
80 ALOGV("enter native_bridge2_isSupported %s", libpath);
81 NativeBridgeCallbacks *cb = get_callbacks();
82 return cb ? cb->isSupported(libpath) : false;
85 static const struct NativeBridgeRuntimeValues *native_bridge2_getAppEnv(const char *abi)
87 ALOGV("enter native_bridge2_getAppEnv %s", abi);
88 NativeBridgeCallbacks *cb = get_callbacks();
89 return cb ? cb->getAppEnv(abi) : nullptr;
92 static bool native_bridge2_is_compatible_compatible_with(uint32_t version)
94 // For testing, allow 1 and 2, but disallow 3+.
98 static NativeBridgeSignalHandlerFn native_bridge2_get_signal_handler(int signal)
100 ALOGV("enter native_bridge2_getAppEnv %d", signal);
101 NativeBridgeCallbacks *cb = get_callbacks();
102 return cb ? cb->getSignalHandler(signal) : nullptr;
105 static void __attribute__ ((destructor)) on_dlclose()
108 dlclose(native_handle);
109 native_handle = nullptr;
115 NativeBridgeCallbacks NativeBridgeItf = {
117 .initialize = &native_bridge2_initialize,
118 .loadLibrary = &native_bridge2_loadLibrary,
119 .getTrampoline = &native_bridge2_getTrampoline,
120 .isSupported = &native_bridge2_isSupported,
121 .getAppEnv = &native_bridge2_getAppEnv,
122 .isCompatibleWith = &native_bridge2_is_compatible_compatible_with,
123 .getSignalHandler = &native_bridge2_get_signal_handler,
127 } // namespace android