xref: /aosp_15_r20/system/chre/platform/shared/dlfcn.cc (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2020 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 
17 #include <dlfcn.h>
18 
19 #include "chre/platform/assert.h"
20 #include "chre/platform/log.h"
21 #include "chre/platform/shared/nanoapp_loader.h"
22 #include "chre/util/unique_ptr.h"
23 
dlopenbuf(void * elfBinary,bool mapIntoTcm)24 void *dlopenbuf(void *elfBinary, bool mapIntoTcm) {
25   return chre::NanoappLoader::create(elfBinary, mapIntoTcm);
26 }
27 
dlsym(void * handle,const char * symbol)28 void *dlsym(void *handle, const char *symbol) {
29   LOGV("Attempting to find %s", symbol);
30 
31   void *resolvedSymbol = nullptr;
32   if (handle == RTLD_NEXT) {
33     resolvedSymbol = chre::NanoappLoader::findExportedSymbol(symbol);
34   } else if (handle != nullptr) {
35     auto *loader = reinterpret_cast<chre::NanoappLoader *>(handle);
36     resolvedSymbol = loader->findSymbolByName(symbol);
37   }
38 
39   if (resolvedSymbol == nullptr) {
40     LOGE("dlsym unable to resolve %s", symbol);
41   } else {
42     LOGV("Found symbol at %p", resolvedSymbol);
43   }
44 
45   return resolvedSymbol;
46 }
47 
dlclose(void * handle)48 int dlclose(void *handle) {
49   int rv = -1;
50 
51   if (handle != nullptr) {
52     chre::NanoappLoader::destroy(static_cast<chre::NanoappLoader *>(handle));
53     rv = 0;
54   }
55 
56   return rv;
57 }
58 
dlerror()59 const char *dlerror() {
60   return "Shared library load failed";
61 }
62