1 /*
2 * Copyright (C) 2024 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 #define LOG_TAG "ApexSupport"
17
18 #include "android/apexsupport.h"
19
20 #include <dlfcn.h>
21
22 #include <string>
23
24 #include <android/dlext.h>
25 #include <log/log.h>
26
27 extern "C" android_namespace_t *android_get_exported_namespace(const char *);
28
AApexSupport_loadLibrary(const char * _Nonnull name,const char * _Nonnull apexName,int flag)29 void *AApexSupport_loadLibrary(const char *_Nonnull name,
30 const char *_Nonnull apexName, int flag) {
31 // convert the apex name into the linker namespace name
32 std::string namespaceName = apexName;
33 std::replace(namespaceName.begin(), namespaceName.end(), '.', '_');
34
35 android_namespace_t *ns =
36 android_get_exported_namespace(namespaceName.c_str());
37 if (ns == nullptr) {
38 ALOGE("Could not find namespace for %s APEX. Is it visible?", apexName);
39 return nullptr;
40 }
41
42 const android_dlextinfo dlextinfo = {
43 .flags = ANDROID_DLEXT_USE_NAMESPACE,
44 .library_namespace = ns,
45 };
46 void *handle = android_dlopen_ext(name, flag, &dlextinfo);
47 if (!handle) {
48 ALOGE("Could not load %s: %s", name, dlerror());
49 return nullptr;
50 }
51 return handle;
52 }
53