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