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
18 #if defined(HITLS_BSL_SAL_DL)
19 #include <stdio.h>
20 #include <stdint.h>
21 #include "securec.h"
22 #include "bsl_sal.h"
23 #include "bsl_errno.h"
24 #include "bsl_err_internal.h"
25
26 #include "string.h"
27 #include "sal_dlimpl.h"
28 #include "bsl_log_internal.h"
29
30 static BSL_SAL_DlCallback g_dlCallback = {0};
31
32 // Define macro for path reserve length
33 #define BSL_SAL_PATH_RESERVE 10
34
35 #define BSL_SAL_PATH_MAX 4095
36 #define BSL_SAL_NAME_MAX 255
37
BSL_SAL_LibNameFormat(BSL_SAL_LibFmtCmd cmd,const char * fileName,char ** name)38 int32_t BSL_SAL_LibNameFormat(BSL_SAL_LibFmtCmd cmd, const char *fileName, char **name)
39 {
40 if (fileName == NULL || name == NULL) {
41 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM);
42 return BSL_SAL_ERR_BAD_PARAM;
43 }
44 int32_t ret = 0;
45 char *tempName = NULL;
46 uint32_t dlPathLen = strlen(fileName) + BSL_SAL_PATH_RESERVE;
47 if (dlPathLen > BSL_SAL_NAME_MAX) {
48 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_DL_PATH_EXCEED);
49 return BSL_SAL_ERR_DL_PATH_EXCEED;
50 }
51 tempName = (char *)BSL_SAL_Calloc(1, dlPathLen);
52 if (tempName == NULL) {
53 return BSL_MALLOC_FAIL;
54 }
55 switch (cmd) {
56 case BSL_SAL_LIB_FMT_SO:
57 ret = snprintf_s(tempName, dlPathLen, dlPathLen, "%s.so", fileName);
58 break;
59 case BSL_SAL_LIB_FMT_LIBSO:
60 ret = snprintf_s(tempName, dlPathLen, dlPathLen, "lib%s.so", fileName);
61 break;
62 case BSL_SAL_LIB_FMT_LIBDLL:
63 ret = snprintf_s(tempName, dlPathLen, dlPathLen, "lib%s.dll", fileName);
64 break;
65 case BSL_SAL_LIB_FMT_DLL:
66 ret = snprintf_s(tempName, dlPathLen, dlPathLen, "%s.dll", fileName);
67 break;
68 case BSL_SAL_LIB_FMT_OFF:
69 ret = snprintf_s(tempName, dlPathLen, dlPathLen, "%s", fileName);
70 break;
71 default:
72 // Default to the first(BSL_SAL_LIB_FMT_SO) conversion
73 BSL_SAL_Free(tempName);
74 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM);
75 return BSL_SAL_ERR_BAD_PARAM;
76 }
77 if (ret < 0) {
78 BSL_SAL_Free(tempName);
79 BSL_ERR_PUSH_ERROR(BSL_INTERNAL_EXCEPTION);
80 return BSL_INTERNAL_EXCEPTION;
81 }
82 *name = tempName;
83 return BSL_SUCCESS;
84 }
85
BSL_SAL_LoadLib(const char * fileName,void ** handle)86 int32_t BSL_SAL_LoadLib(const char *fileName, void **handle)
87 {
88 if (fileName == NULL || handle == NULL) {
89 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM);
90 return BSL_SAL_ERR_BAD_PARAM;
91 }
92 if (g_dlCallback.pfLoadLib != NULL && g_dlCallback.pfLoadLib != BSL_SAL_LoadLib) {
93 return g_dlCallback.pfLoadLib(fileName, handle);
94 }
95 #ifdef HITLS_BSL_SAL_LINUX
96 return SAL_LoadLib(fileName, handle);
97 #else
98 return BSL_SAL_DL_NO_REG_FUNC;
99 #endif
100 }
101
BSL_SAL_UnLoadLib(void * handle)102 int32_t BSL_SAL_UnLoadLib(void *handle)
103 {
104 if (handle == NULL) {
105 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM);
106 return BSL_SAL_ERR_BAD_PARAM;
107 }
108 if (g_dlCallback.pfUnLoadLib != NULL && g_dlCallback.pfUnLoadLib != BSL_SAL_UnLoadLib) {
109 return g_dlCallback.pfUnLoadLib(handle);
110 }
111 #ifdef HITLS_BSL_SAL_LINUX
112 return SAL_UnLoadLib(handle);
113 #else
114 return BSL_SAL_DL_NO_REG_FUNC;
115 #endif
116 }
117
BSL_SAL_GetFuncAddress(void * handle,const char * funcName,void ** func)118 int32_t BSL_SAL_GetFuncAddress(void *handle, const char *funcName, void **func)
119 {
120 if (handle == NULL || func == NULL) {
121 BSL_ERR_PUSH_ERROR(BSL_SAL_ERR_BAD_PARAM);
122 return BSL_SAL_ERR_BAD_PARAM;
123 }
124 if (g_dlCallback.pfGetFunc != NULL && g_dlCallback.pfGetFunc != BSL_SAL_GetFuncAddress) {
125 return g_dlCallback.pfGetFunc(handle, funcName, func);
126 }
127 #ifdef HITLS_BSL_SAL_LINUX
128 return SAL_GetFunc(handle, funcName, func);
129 #else
130 return BSL_SAL_DL_NO_REG_FUNC;
131 #endif
132 }
133
SAL_DlCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type,void * funcCb)134 int32_t SAL_DlCallback_Ctrl(BSL_SAL_CB_FUNC_TYPE type, void *funcCb)
135 {
136 if (type > BSL_SAL_DL_SYM_CB_FUNC || type < BSL_SAL_DL_OPEN_CB_FUNC) {
137 return BSL_SAL_ERR_BAD_PARAM;
138 }
139 uint32_t offset = (uint32_t)(type - BSL_SAL_DL_OPEN_CB_FUNC);
140 ((void **)&g_dlCallback)[offset] = funcCb;
141 return BSL_SUCCESS;
142 }
143
144 #endif /* HITLS_BSL_SAL_DL */
145