1- #include < string>
1+ /* *
2+ * Shims for the "hidden" api, not found in the Android NDK.
3+ *
4+ * The symbols are compiled with the NDK, but the real Android ones have
5+ * a slightly different mangled name.
6+ */
7+
28#include < log/log.h>
9+ #include < dlfcn.h>
10+
11+ // libcutils
12+ void *ashmem_create_region;
13+ void *ashmem_set_prot_region;
14+ void *native_handle_create;
15+ void *native_handle_delete;
16+ void *native_handle_close;
17+
18+ // libfmq
19+ void *_ZN7android8hardware7details5checkEbPKc;
20+ void *_ZN7android8hardware7details8logErrorERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE; // ndk
21+ void *_ZN7android8hardware9EventFlag15createEventFlagEPNSt6__ndk16atomicIjEEPPS1_; // ndk
22+ void *_ZN7android8hardware9EventFlag15deleteEventFlagEPPS1_;
323
4- namespace android ::hardware::details {
5- void check (bool exp) {
6- ALOGE_IF (!exp, " Check failed" );
24+ static void init_libcutils () {
25+ void *libcutils = dlopen (" libcutils.so" , RTLD_NOW);
26+ if (libcutils == nullptr ) {
27+ ALOGE (" Failed to load libcutils.so" );
28+ return ;
729 }
830
9- void check (bool exp, const char * message) {
10- ALOGE_IF (!exp, " %s" , message);
31+ ashmem_create_region = dlsym (libcutils, " ashmem_create_region" );
32+ ashmem_set_prot_region = dlsym (libcutils, " ashmem_set_prot_region" );
33+
34+ native_handle_create = dlsym (libcutils, " native_handle_create" );
35+ native_handle_close = dlsym (libcutils, " native_handle_close" );
36+ native_handle_delete = dlsym (libcutils, " native_handle_delete" );
37+
38+ if (ashmem_create_region == nullptr ||
39+ ashmem_set_prot_region == nullptr ||
40+ native_handle_create == nullptr ||
41+ native_handle_close == nullptr ||
42+ native_handle_delete == nullptr ) {
43+ ALOGE (" Failed to load symbols from libcutils.so" );
1144 }
1245
13- void logError (const std::string &message) {
14- ALOGE (" %s" , message.c_str ());
46+ dlclose (libcutils);
47+ }
48+
49+ static void init_libfmq () {
50+ void *libfmq = dlopen (" libfmq.so" , RTLD_NOW);
51+ if (libfmq == nullptr ) {
52+ ALOGE (" Failed to load libfmq.so" );
53+ return ;
1554 }
1655
17- void errorWriteLog (int tag, const char * info) {
18- ALOGE (" %d: %s" , tag, info);
56+ _ZN7android8hardware7details5checkEbPKc = dlsym (libfmq, " _ZN7android8hardware7details5checkEbPKc" );
57+ // ndk variant, real symbol: _ZN7android8hardware7details8logErrorERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE
58+ _ZN7android8hardware7details8logErrorERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE = dlsym (libfmq, " _ZN7android8hardware7details8logErrorERKNSt3__112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE" );
59+ // ndk variant, real symbol: _ZN7android8hardware9EventFlag15createEventFlagEPNSt3__16atomicIjEEPPS1_
60+ _ZN7android8hardware9EventFlag15createEventFlagEPNSt6__ndk16atomicIjEEPPS1_ = dlsym (libfmq, " _ZN7android8hardware9EventFlag15createEventFlagEPNSt3__16atomicIjEEPPS1_" );
61+ _ZN7android8hardware9EventFlag15deleteEventFlagEPPS1_ = dlsym (libfmq, " _ZN7android8hardware9EventFlag15deleteEventFlagEPPS1_" );
62+
63+ if (_ZN7android8hardware7details5checkEbPKc == nullptr ||
64+ _ZN7android8hardware7details8logErrorERKNSt6__ndk112basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEE == nullptr ||
65+ _ZN7android8hardware9EventFlag15createEventFlagEPNSt6__ndk16atomicIjEEPPS1_ == nullptr ||
66+ _ZN7android8hardware9EventFlag15deleteEventFlagEPPS1_ == nullptr ) {
67+ ALOGE (" Failed to load symbols from libfmq.so" );
1968 }
20- } // namespace android::hardware::details
69+
70+ dlclose (libfmq);
71+ }
72+
73+ __attribute__ ((constructor))
74+ void shim_init() {
75+ ALOGD (" shim_init" );
76+ init_libcutils ();
77+ init_libfmq ();
78+ ALOGD (" shim_init done" );
79+ }
0 commit comments