1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17 #if defined(HITLS_BSL_SAL_LINUX) && defined(HITLS_BSL_SAL_DL)
18
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dlfcn.h>
23 #include "bsl_errno.h"
24 #include "bsl_err_internal.h"
25
SAL_LoadLib(const char * fileName,void ** handle)26 int32_t SAL_LoadLib(const char *fileName, void **handle)
27 {
28 void *tempHandle = dlopen(fileName, RTLD_NOW);
29 if (tempHandle == NULL) {
30 char *error = dlerror();
31 if (strstr(error, "No such file or directory") != NULL) {
32 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_NOT_FOUND);
33 return BSL_SAL_ERR_DL_NOT_FOUND;
34 }
35 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_LOAD_FAIL);
36 return BSL_SAL_ERR_DL_LOAD_FAIL;
37 }
38 *handle = tempHandle;
39 return BSL_SUCCESS;
40 }
41
SAL_UnLoadLib(void * handle)42 int32_t SAL_UnLoadLib(void *handle)
43 {
44 int32_t ret = dlclose(handle);
45 if (ret != 0) {
46 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_UNLOAAD_FAIL);
47 return BSL_SAL_ERR_DL_UNLOAAD_FAIL;
48 }
49 return BSL_SUCCESS;
50 }
51
SAL_GetFunc(void * handle,const char * funcName,void ** func)52 int32_t SAL_GetFunc(void *handle, const char *funcName, void **func)
53 {
54 void *tempFunc = dlsym(handle, funcName);
55 if (tempFunc == NULL) {
56 char *error = dlerror();
57 if (strstr(error, "undefined symbol") != NULL) {
58 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_NON_FUNCTION);
59 return BSL_SAL_ERR_DL_NON_FUNCTION;
60 }
61 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_LOOKUP_METHOD);
62 return BSL_SAL_ERR_DL_LOOKUP_METHOD;
63 }
64 *func = tempFunc;
65 return BSL_SUCCESS;
66 }
67
68 #endif
69