1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #include <sal/types.h> 25 #include <osl/diagnose.h> 26 #include <osl/module.h> 27 #include <osl/thread.h> 28 #include <osl/process.h> 29 #include <osl/file.h> 30 31 #include "system.h" 32 33 #if OSL_DEBUG_LEVEL > 1 34 #include <stdio.h> 35 #endif 36 37 /* implemented in file.c */ 38 extern int UnicodeToText(char *, size_t, const sal_Unicode *, sal_Int32); 39 40 oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode); 41 42 /*****************************************************************************/ 43 /* osl_loadModule */ 44 /*****************************************************************************/ 45 46 oslModule SAL_CALL osl_loadModule(rtl_uString *ustrModuleName, sal_Int32 nRtldMode) 47 { 48 oslModule pModule=0; 49 rtl_uString* ustrTmp = NULL; 50 51 OSL_ENSURE(ustrModuleName,"osl_loadModule : string is not valid"); 52 53 /* ensure ustrTmp hold valid string */ 54 if (osl_File_E_None != osl_getSystemPathFromFileURL(ustrModuleName, &ustrTmp)) 55 rtl_uString_assign(&ustrTmp, ustrModuleName); 56 57 if (ustrTmp) 58 { 59 char buffer[PATH_MAX]; 60 61 if (UnicodeToText(buffer, PATH_MAX, ustrTmp->buffer, ustrTmp->length)) 62 pModule = osl_psz_loadModule(buffer, nRtldMode); 63 rtl_uString_release(ustrTmp); 64 } 65 66 return pModule; 67 } 68 69 /*****************************************************************************/ 70 /* osl_psz_loadModule */ 71 /*****************************************************************************/ 72 73 oslModule SAL_CALL osl_psz_loadModule(const sal_Char *pszModuleName, sal_Int32 nRtldMode) 74 { 75 OSL_ASSERT( 76 (nRtldMode & SAL_LOADMODULE_LAZY) == 0 || 77 (nRtldMode & SAL_LOADMODULE_NOW) == 0); /* only either LAZY or NOW */ 78 if (pszModuleName) 79 { 80 #ifndef NO_DL_FUNCTIONS 81 int rtld_mode = 82 ((nRtldMode & SAL_LOADMODULE_NOW) ? RTLD_NOW : RTLD_LAZY) | 83 ((nRtldMode & SAL_LOADMODULE_GLOBAL) ? RTLD_GLOBAL : RTLD_LOCAL); 84 void* pLib = dlopen(pszModuleName, rtld_mode); 85 86 #if OSL_DEBUG_LEVEL > 1 87 if (pLib == 0) 88 OSL_TRACE("Error osl_loadModule: %s\n", dlerror()); 89 #endif /* OSL_DEBUG_LEVEL */ 90 91 return ((oslModule)(pLib)); 92 93 #else /* NO_DL_FUNCTIONS */ 94 printf("No DL Functions\n"); 95 #endif /* NO_DL_FUNCTIONS */ 96 } 97 return NULL; 98 } 99 100 /*****************************************************************************/ 101 /* osl_getModuleHandle */ 102 /*****************************************************************************/ 103 104 sal_Bool SAL_CALL 105 osl_getModuleHandle(rtl_uString *pModuleName, oslModule *pResult) 106 { 107 (void) pModuleName; /* avoid warning about unused parameter */ 108 *pResult = (oslModule) RTLD_DEFAULT; 109 return sal_True; 110 } 111 112 /*****************************************************************************/ 113 /* osl_unloadModule */ 114 /*****************************************************************************/ 115 void SAL_CALL osl_unloadModule(oslModule hModule) 116 { 117 if (hModule) 118 { 119 #ifndef NO_DL_FUNCTIONS 120 int nRet = dlclose(hModule); 121 122 #if OSL_DEBUG_LEVEL > 1 123 if (nRet != 0) 124 { 125 fprintf(stderr, "Error osl_unloadModule: %s\n", dlerror()); 126 } 127 #else 128 (void) nRet; 129 #endif /* if OSL_DEBUG_LEVEL */ 130 131 #endif /* ifndef NO_DL_FUNCTIONS */ 132 } 133 } 134 135 /*****************************************************************************/ 136 /* osl_getSymbol */ 137 /*****************************************************************************/ 138 void* SAL_CALL 139 osl_getSymbol(oslModule Module, rtl_uString* pSymbolName) 140 { 141 return (void *) osl_getFunctionSymbol(Module, pSymbolName); 142 } 143 144 145 /*****************************************************************************/ 146 /* osl_getAsciiFunctionSymbol */ 147 /*****************************************************************************/ 148 oslGenericFunction SAL_CALL 149 osl_getAsciiFunctionSymbol(oslModule Module, const sal_Char *pSymbol) 150 { 151 void *fcnAddr = NULL; 152 153 #ifndef NO_DL_FUNCTIONS 154 if (pSymbol) 155 { 156 fcnAddr = dlsym(Module, pSymbol); 157 158 if (!fcnAddr) 159 OSL_TRACE("Error osl_getAsciiFunctionSymbol: %s\n", dlerror()); 160 } 161 #endif 162 163 return (oslGenericFunction) fcnAddr; 164 } 165 166 /*****************************************************************************/ 167 /* osl_getFunctionSymbol */ 168 /*****************************************************************************/ 169 oslGenericFunction SAL_CALL 170 osl_getFunctionSymbol(oslModule module, rtl_uString *puFunctionSymbolName) 171 { 172 oslGenericFunction pSymbol = NULL; 173 174 if( puFunctionSymbolName ) 175 { 176 rtl_String* pSymbolName = NULL; 177 178 rtl_uString2String( &pSymbolName, 179 rtl_uString_getStr(puFunctionSymbolName), 180 rtl_uString_getLength(puFunctionSymbolName), 181 RTL_TEXTENCODING_UTF8, 182 OUSTRING_TO_OSTRING_CVTFLAGS ); 183 184 if( pSymbolName != NULL ) 185 { 186 pSymbol = osl_getAsciiFunctionSymbol(module, rtl_string_getStr(pSymbolName)); 187 rtl_string_release(pSymbolName); 188 } 189 } 190 191 return pSymbol; 192 } 193 194 /*****************************************************************************/ 195 /* osl_getModuleURLFromAddress */ 196 /*****************************************************************************/ 197 sal_Bool SAL_CALL osl_getModuleURLFromAddress(void * addr, rtl_uString ** ppLibraryUrl) 198 { 199 sal_Bool result = sal_False; 200 Dl_info dl_info; 201 202 if ((result = dladdr(addr, &dl_info)) != 0) 203 { 204 rtl_uString * workDir = NULL; 205 osl_getProcessWorkingDir(&workDir); 206 if (workDir) 207 { 208 #if OSL_DEBUG_LEVEL > 1 209 OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", dl_info.dli_fname); 210 #endif 211 rtl_string2UString(ppLibraryUrl, 212 dl_info.dli_fname, 213 strlen(dl_info.dli_fname), 214 osl_getThreadTextEncoding(), 215 OSTRING_TO_OUSTRING_CVTFLAGS); 216 217 OSL_ASSERT(*ppLibraryUrl != NULL); 218 osl_getFileURLFromSystemPath(*ppLibraryUrl, ppLibraryUrl); 219 osl_getAbsoluteFileURL(workDir, *ppLibraryUrl, ppLibraryUrl); 220 221 rtl_uString_release(workDir); 222 result = sal_True; 223 } 224 else 225 { 226 result = sal_False; 227 } 228 } 229 return result; 230 } 231 232 /*****************************************************************************/ 233 /* osl_getModuleURLFromFunctionAddress */ 234 /*****************************************************************************/ 235 sal_Bool SAL_CALL osl_getModuleURLFromFunctionAddress(oslGenericFunction addr, rtl_uString ** ppLibraryUrl) 236 { 237 return osl_getModuleURLFromAddress((void*)addr, ppLibraryUrl); 238 } 239