diff options
author | Jiyong Park | 2017-05-10 12:15:24 -0500 |
---|---|---|
committer | Jiyong Park | 2017-05-14 21:35:30 -0500 |
commit | 8902f7571adb68147f6641cb44beb5186c53dc44 (patch) | |
tree | 1c41f20eadb04797561c59c53e51314a54b52425 /libvndksupport | |
parent | a7172f41b8ccb82d04e3317bb84a46b19d8c008d (diff) | |
download | platform-system-core-8902f7571adb68147f6641cb44beb5186c53dc44.tar.gz platform-system-core-8902f7571adb68147f6641cb44beb5186c53dc44.tar.xz platform-system-core-8902f7571adb68147f6641cb44beb5186c53dc44.zip |
add libvndksupport
libvndksupport is a new LL-NDK library that provides vendor-visible APIs
for platform-only functionalities of other LL-NDK libraries. Currently,
it provides android_(load|unload)_sphal_library which abstracts the
platform-only APIs of libdl (android_get_exported_namespace, etc.)
Bug: 37323945
Test: sailfish builds and boots
Test: libvndksupport-tests passes
Merged-In: I6d2911b57e009d0c842554933aac87d6573ffcbf
Change-Id: I6d2911b57e009d0c842554933aac87d6573ffcbf
(cherry picked from commit 7130e13262f788d35e85e0b38db58a1e2aa6266b)
Diffstat (limited to 'libvndksupport')
-rw-r--r-- | libvndksupport/Android.bp | 15 | ||||
-rw-r--r-- | libvndksupport/include/vndksupport/linker.h | 31 | ||||
-rw-r--r-- | libvndksupport/libvndksupport.map.txt | 7 | ||||
-rw-r--r-- | libvndksupport/linker.c | 50 | ||||
-rw-r--r-- | libvndksupport/tests/Android.bp | 26 | ||||
-rw-r--r-- | libvndksupport/tests/linker_test.cpp | 60 |
6 files changed, 189 insertions, 0 deletions
diff --git a/libvndksupport/Android.bp b/libvndksupport/Android.bp new file mode 100644 index 000000000..ab9e26fe4 --- /dev/null +++ b/libvndksupport/Android.bp | |||
@@ -0,0 +1,15 @@ | |||
1 | subdirs = ["tests"] | ||
2 | |||
3 | cc_library_shared { | ||
4 | name: "libvndksupport", | ||
5 | srcs: ["linker.c"], | ||
6 | local_include_dirs: ["include/vndksupport"], | ||
7 | export_include_dirs: ["include"], | ||
8 | shared_libs: ["liblog"], | ||
9 | } | ||
10 | |||
11 | llndk_library { | ||
12 | name: "libvndksupport", | ||
13 | symbol_file: "libvndksupport.map.txt", | ||
14 | export_include_dirs: ["include"], | ||
15 | } | ||
diff --git a/libvndksupport/include/vndksupport/linker.h b/libvndksupport/include/vndksupport/linker.h new file mode 100644 index 000000000..f509564e1 --- /dev/null +++ b/libvndksupport/include/vndksupport/linker.h | |||
@@ -0,0 +1,31 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 The Android Open Source Project | ||
3 | * | ||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | * you may not use this file except in compliance with the License. | ||
6 | * You may obtain a copy of the License at | ||
7 | * | ||
8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | * | ||
10 | * Unless required by applicable law or agreed to in writing, software | ||
11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | * See the License for the specific language governing permissions and | ||
14 | * limitations under the License. | ||
15 | */ | ||
16 | #ifndef VNDKSUPPORT_LINKER_H_ | ||
17 | #define VNDKSUPPORT_LINKER_H_ | ||
18 | |||
19 | #ifdef __cplusplus | ||
20 | extern "C" { | ||
21 | #endif | ||
22 | |||
23 | void* android_load_sphal_library(const char* name, int flag); | ||
24 | |||
25 | int android_unload_sphal_library(void* handle); | ||
26 | |||
27 | #ifdef __cplusplus | ||
28 | } | ||
29 | #endif | ||
30 | |||
31 | #endif // VNDKSUPPORT_LINKER_H_ | ||
diff --git a/libvndksupport/libvndksupport.map.txt b/libvndksupport/libvndksupport.map.txt new file mode 100644 index 000000000..16e38da1a --- /dev/null +++ b/libvndksupport/libvndksupport.map.txt | |||
@@ -0,0 +1,7 @@ | |||
1 | LIBVNDKSUPPORT { | ||
2 | global: | ||
3 | android_load_sphal_library; # vndk | ||
4 | android_unload_sphal_library; # vndk | ||
5 | local: | ||
6 | *; | ||
7 | }; | ||
diff --git a/libvndksupport/linker.c b/libvndksupport/linker.c new file mode 100644 index 000000000..12aa3bec8 --- /dev/null +++ b/libvndksupport/linker.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 The Android Open Source Project | ||
3 | * | ||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | * you may not use this file except in compliance with the License. | ||
6 | * You may obtain a copy of the License at | ||
7 | * | ||
8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | * | ||
10 | * Unless required by applicable law or agreed to in writing, software | ||
11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | * See the License for the specific language governing permissions and | ||
14 | * limitations under the License. | ||
15 | */ | ||
16 | #include "linker.h" | ||
17 | |||
18 | #include <android/dlext.h> | ||
19 | #include <dlfcn.h> | ||
20 | |||
21 | #define LOG_TAG "vndksupport" | ||
22 | #include <log/log.h> | ||
23 | |||
24 | extern struct android_namespace_t* android_get_exported_namespace(const char*); | ||
25 | |||
26 | void* android_load_sphal_library(const char* name, int flag) { | ||
27 | struct android_namespace_t* sphal_namespace = android_get_exported_namespace("sphal"); | ||
28 | if (sphal_namespace != NULL) { | ||
29 | const android_dlextinfo dlextinfo = { | ||
30 | .flags = ANDROID_DLEXT_USE_NAMESPACE, .library_namespace = sphal_namespace, | ||
31 | }; | ||
32 | void* handle = android_dlopen_ext(name, flag, &dlextinfo); | ||
33 | if (handle) { | ||
34 | return handle; | ||
35 | } else { | ||
36 | ALOGW( | ||
37 | "Could not load %s from sphal namespace: %s. " | ||
38 | "Falling back to loading it from the current namespace,", | ||
39 | name, dlerror()); | ||
40 | } | ||
41 | } else { | ||
42 | ALOGI( | ||
43 | "sphal namespace is not configured for this process. " | ||
44 | "Loading %s from the current namespace instead.", | ||
45 | name); | ||
46 | } | ||
47 | return dlopen(name, flag); | ||
48 | } | ||
49 | |||
50 | int android_unload_sphal_library(void* handle) { return dlclose(handle); } | ||
diff --git a/libvndksupport/tests/Android.bp b/libvndksupport/tests/Android.bp new file mode 100644 index 000000000..3587cf88a --- /dev/null +++ b/libvndksupport/tests/Android.bp | |||
@@ -0,0 +1,26 @@ | |||
1 | // Copyright (C) 2017 The Android Open Source Project | ||
2 | // | ||
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | ||
4 | // you may not use this file except in compliance with the License. | ||
5 | // You may obtain a copy of the License at | ||
6 | // | ||
7 | // http://www.apache.org/licenses/LICENSE-2.0 | ||
8 | // | ||
9 | // Unless required by applicable law or agreed to in writing, software | ||
10 | // distributed under the License is distributed on an "AS IS" BASIS, | ||
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
12 | // See the License for the specific language governing permissions and | ||
13 | // limitations under the License. | ||
14 | |||
15 | cc_test { | ||
16 | name: "libvndksupport-tests", | ||
17 | srcs: [ | ||
18 | "linker_test.cpp", | ||
19 | ], | ||
20 | |||
21 | host_supported: false, | ||
22 | shared_libs: [ | ||
23 | "libvndksupport", | ||
24 | "libbase", | ||
25 | ] | ||
26 | } | ||
diff --git a/libvndksupport/tests/linker_test.cpp b/libvndksupport/tests/linker_test.cpp new file mode 100644 index 000000000..7ce27d411 --- /dev/null +++ b/libvndksupport/tests/linker_test.cpp | |||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2017 The Android Open Source Project | ||
3 | * | ||
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | * you may not use this file except in compliance with the License. | ||
6 | * You may obtain a copy of the License at | ||
7 | * | ||
8 | * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | * | ||
10 | * Unless required by applicable law or agreed to in writing, software | ||
11 | * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | * See the License for the specific language governing permissions and | ||
14 | * limitations under the License. | ||
15 | */ | ||
16 | #include <gtest/gtest.h> | ||
17 | |||
18 | #include <android-base/strings.h> | ||
19 | #include <dirent.h> | ||
20 | #include <dlfcn.h> | ||
21 | #include <vndksupport/linker.h> | ||
22 | #include <string> | ||
23 | |||
24 | // Since the test executable will be in /data and ld.config.txt does not | ||
25 | // configure sphal namespace for executables in /data, the call to | ||
26 | // android_load_sphal_library will always fallback to the plain dlopen from the | ||
27 | // default namespace. | ||
28 | |||
29 | // Let's use libEGL_<chipset>.so as a SP-HAL in test | ||
30 | static std::string find_sphal_lib() { | ||
31 | const char* path = | ||
32 | #if defined(__LP64__) | ||
33 | "/vendor/lib64/egl"; | ||
34 | #else | ||
35 | "/vendor/lib/egl"; | ||
36 | #endif | ||
37 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path), closedir); | ||
38 | |||
39 | dirent* dp; | ||
40 | while ((dp = readdir(dir.get())) != nullptr) { | ||
41 | std::string name = dp->d_name; | ||
42 | if (android::base::StartsWith(name, "libEGL_")) { | ||
43 | return std::string(path) + "/" + name; | ||
44 | } | ||
45 | } | ||
46 | return ""; | ||
47 | } | ||
48 | |||
49 | TEST(linker, load_existing_lib) { | ||
50 | std::string name = find_sphal_lib(); | ||
51 | ASSERT_NE("", name); | ||
52 | void* handle = android_load_sphal_library(name.c_str(), RTLD_NOW | RTLD_LOCAL); | ||
53 | ASSERT_NE(nullptr, handle); | ||
54 | android_unload_sphal_library(handle); | ||
55 | } | ||
56 | |||
57 | TEST(linker, load_nonexisting_lib) { | ||
58 | void* handle = android_load_sphal_library("libNeverUseThisName.so", RTLD_NOW | RTLD_LOCAL); | ||
59 | ASSERT_EQ(nullptr, handle); | ||
60 | } | ||