Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 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 | |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame^] | 26 | static const char* namespace_name = NULL; |
| 27 | |
| 28 | static struct android_namespace_t* get_vendor_namespace() { |
| 29 | const char* namespace_names[] = {"sphal", "default", NULL}; |
| 30 | static struct android_namespace_t* vendor_namespace = NULL; |
| 31 | if (vendor_namespace == NULL) { |
| 32 | int name_idx = 0; |
| 33 | while (namespace_names[name_idx] != NULL) { |
| 34 | vendor_namespace = android_get_exported_namespace(namespace_names[name_idx]); |
| 35 | if (vendor_namespace != NULL) { |
| 36 | namespace_name = namespace_names[name_idx]; |
| 37 | break; |
| 38 | } |
| 39 | name_idx++; |
| 40 | } |
| 41 | } |
| 42 | return vendor_namespace; |
| 43 | } |
| 44 | |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 45 | void* android_load_sphal_library(const char* name, int flag) { |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame^] | 46 | struct android_namespace_t* vendor_namespace = get_vendor_namespace(); |
| 47 | if (vendor_namespace != NULL) { |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 48 | const android_dlextinfo dlextinfo = { |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame^] | 49 | .flags = ANDROID_DLEXT_USE_NAMESPACE, .library_namespace = vendor_namespace, |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 50 | }; |
| 51 | void* handle = android_dlopen_ext(name, flag, &dlextinfo); |
Justin Yun | 17baed1 | 2017-05-22 14:31:56 +0900 | [diff] [blame] | 52 | if (!handle) { |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame^] | 53 | ALOGE("Could not load %s from %s namespace: %s.", name, namespace_name, dlerror()); |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 54 | } |
Justin Yun | 17baed1 | 2017-05-22 14:31:56 +0900 | [diff] [blame] | 55 | return handle; |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 56 | } else { |
Justin Yun | 090b593 | 2017-07-11 18:58:51 +0900 | [diff] [blame^] | 57 | ALOGD("Loading %s from current namespace instead of sphal namespace.", name); |
Justin Yun | 17baed1 | 2017-05-22 14:31:56 +0900 | [diff] [blame] | 58 | return dlopen(name, flag); |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 59 | } |
Jiyong Park | 7130e13 | 2017-05-11 02:15:24 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int android_unload_sphal_library(void* handle) { |
| 63 | return dlclose(handle); |
| 64 | } |