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 "nativebridge/native_bridge.h"
22 static void *native_handle = nullptr;
24 static NativeBridgeCallbacks *get_callbacks()
26 static NativeBridgeCallbacks *callbacks = nullptr;
29 const char *libnb = "/system/"
37 native_handle = dlopen(libnb, RTLD_LAZY);
39 ALOGE("Unable to open %s", libnb);
43 callbacks = reinterpret_cast<NativeBridgeCallbacks *>(dlsym(native_handle, "NativeBridgeItf"));
48 // NativeBridgeCallbacks implementations
49 static bool native_bridge2_initialize(const NativeBridgeRuntimeCallbacks *art_cbs,
50 const char *app_code_cache_dir,
53 ALOGV("enter native_bridge2_initialize %s %s", app_code_cache_dir, isa);
54 NativeBridgeCallbacks *cb = get_callbacks();
55 return cb ? cb->initialize(art_cbs, app_code_cache_dir, isa) : false;
58 static void *native_bridge2_loadLibrary(const char *libpath, int flag)
60 ALOGV("enter native_bridge2_loadLibrary %s", libpath);
61 NativeBridgeCallbacks *cb = get_callbacks();
62 return cb ? cb->loadLibrary(libpath, flag) : nullptr;
65 static void *native_bridge2_getTrampoline(void *handle, const char *name,
66 const char* shorty, uint32_t len)
68 ALOGV("enter native_bridge2_getTrampoline %s", name);
69 NativeBridgeCallbacks *cb = get_callbacks();
70 return cb ? cb->getTrampoline(handle, name, shorty, len) : nullptr;
73 static bool native_bridge2_isSupported(const char *libpath)
75 ALOGV("enter native_bridge2_isSupported %s", libpath);
76 NativeBridgeCallbacks *cb = get_callbacks();
77 return cb ? cb->isSupported(libpath) : false;
80 static const struct NativeBridgeRuntimeValues *native_bridge2_getAppEnv(const char *abi)
82 ALOGV("enter native_bridge2_getAppEnv %s", abi);
83 NativeBridgeCallbacks *cb = get_callbacks();
84 return cb ? cb->getAppEnv(abi) : nullptr;
87 static bool native_bridge2_is_compatible_compatible_with(uint32_t version)
89 // For testing, allow 1 and 2, but disallow 3+.
93 static NativeBridgeSignalHandlerFn native_bridge2_get_signal_handler(int signal)
95 ALOGV("enter native_bridge2_getAppEnv %d", signal);
96 NativeBridgeCallbacks *cb = get_callbacks();
97 return cb ? cb->getSignalHandler(signal) : nullptr;
100 static void __attribute__ ((destructor)) on_dlclose()
103 dlclose(native_handle);
104 native_handle = nullptr;
110 NativeBridgeCallbacks NativeBridgeItf = {
112 initialize: &native_bridge2_initialize,
113 loadLibrary: &native_bridge2_loadLibrary,
114 getTrampoline: &native_bridge2_getTrampoline,
115 isSupported: &native_bridge2_isSupported,
116 getAppEnv: &native_bridge2_getAppEnv,
117 isCompatibleWith: &native_bridge2_is_compatible_compatible_with,
118 getSignalHandler: &native_bridge2_get_signal_handler,
122 } // namespace android