1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: module.c,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <sal/types.h>
32 #include <osl/diagnose.h>
33 #include <osl/module.h>
34 #include <osl/thread.h>
35 #include <osl/process.h>
39 #ifndef _RLD_INTERFACE_DLFCN_H_DLADDR
40 #define _RLD_INTERFACE_DLFCN_H_DLADDR
41 typedef struct DL_INFO
{
42 const char * dli_fname
;
44 const char * dli_sname
;
51 #include <rld_interface.h>
52 #define _RLD_DLADDR 14
53 int dladdr(void *address
, Dl_info
*dl
);
55 int dladdr(void *address
, Dl_info
*dl
)
58 v
= _rld_new_interface(_RLD_DLADDR
,address
,dl
);
66 #if OSL_DEBUG_LEVEL > 1
70 /* implemented in file.c */
71 extern int UnicodeToText(char *, size_t, const sal_Unicode
*, sal_Int32
);
73 /*****************************************************************************/
75 /*****************************************************************************/
77 oslModule SAL_CALL
osl_loadModule(rtl_uString
*ustrModuleName
, sal_Int32 nRtldMode
)
80 rtl_uString
* ustrTmp
= NULL
;
82 OSL_ENSURE(ustrModuleName
,"osl_loadModule : string is not valid");
84 /* ensure ustrTmp hold valid string */
85 if (osl_File_E_None
!= osl_getSystemPathFromFileURL(ustrModuleName
, &ustrTmp
))
86 rtl_uString_assign(&ustrTmp
, ustrModuleName
);
90 char buffer
[PATH_MAX
];
92 if (UnicodeToText(buffer
, PATH_MAX
, ustrTmp
->buffer
, ustrTmp
->length
))
93 pModule
= osl_loadModuleAscii(buffer
, nRtldMode
);
94 rtl_uString_release(ustrTmp
);
100 /*****************************************************************************/
101 /* osl_loadModuleAscii */
102 /*****************************************************************************/
104 oslModule SAL_CALL
osl_loadModuleAscii(const sal_Char
*pModuleName
, sal_Int32 nRtldMode
)
107 (nRtldMode
& SAL_LOADMODULE_LAZY
) == 0 ||
108 (nRtldMode
& SAL_LOADMODULE_NOW
) == 0); /* only either LAZY or NOW */
111 #ifndef NO_DL_FUNCTIONS
113 ((nRtldMode
& SAL_LOADMODULE_NOW
) ? RTLD_NOW
: RTLD_LAZY
) |
114 ((nRtldMode
& SAL_LOADMODULE_GLOBAL
) ? RTLD_GLOBAL
: RTLD_LOCAL
);
115 void* pLib
= dlopen(pModuleName
, rtld_mode
);
117 #if OSL_DEBUG_LEVEL > 1
119 OSL_TRACE("Error osl_loadModule: %s\n", dlerror());
120 #endif /* OSL_DEBUG_LEVEL */
122 return ((oslModule
)(pLib
));
124 #else /* NO_DL_FUNCTIONS */
125 printf("No DL Functions\n");
126 #endif /* NO_DL_FUNCTIONS */
131 /*****************************************************************************/
132 /* osl_getModuleHandle */
133 /*****************************************************************************/
136 osl_getModuleHandle(rtl_uString
*pModuleName
, oslModule
*pResult
)
138 (void) pModuleName
; /* avoid warning about unused parameter */
139 *pResult
= (oslModule
) RTLD_DEFAULT
;
143 /*****************************************************************************/
144 /* osl_unloadModule */
145 /*****************************************************************************/
146 void SAL_CALL
osl_unloadModule(oslModule hModule
)
150 #ifndef NO_DL_FUNCTIONS
151 int nRet
= dlclose(hModule
);
153 #if OSL_DEBUG_LEVEL > 1
156 fprintf(stderr
, "Error osl_unloadModule: %s\n", dlerror());
160 #endif /* if OSL_DEBUG_LEVEL */
162 #endif /* ifndef NO_DL_FUNCTIONS */
166 /*****************************************************************************/
168 /*****************************************************************************/
170 osl_getSymbol(oslModule Module
, rtl_uString
* pSymbolName
)
172 return (void *) osl_getFunctionSymbol(Module
, pSymbolName
);
176 /*****************************************************************************/
177 /* osl_getAsciiFunctionSymbol */
178 /*****************************************************************************/
179 oslGenericFunction SAL_CALL
180 osl_getAsciiFunctionSymbol(oslModule Module
, const sal_Char
*pSymbol
)
182 void *fcnAddr
= NULL
;
184 #ifndef NO_DL_FUNCTIONS
187 fcnAddr
= dlsym(Module
, pSymbol
);
190 OSL_TRACE("Error osl_getAsciiFunctionSymbol: %s\n", dlerror());
194 return (oslGenericFunction
) fcnAddr
;
197 /*****************************************************************************/
198 /* osl_getFunctionSymbol */
199 /*****************************************************************************/
200 oslGenericFunction SAL_CALL
201 osl_getFunctionSymbol(oslModule module
, rtl_uString
*puFunctionSymbolName
)
203 oslGenericFunction pSymbol
= NULL
;
205 if( puFunctionSymbolName
)
207 rtl_String
* pSymbolName
= NULL
;
209 rtl_uString2String( &pSymbolName
,
210 rtl_uString_getStr(puFunctionSymbolName
),
211 rtl_uString_getLength(puFunctionSymbolName
),
212 RTL_TEXTENCODING_UTF8
,
213 OUSTRING_TO_OSTRING_CVTFLAGS
);
215 if( pSymbolName
!= NULL
)
217 pSymbol
= osl_getAsciiFunctionSymbol(module
, rtl_string_getStr(pSymbolName
));
218 rtl_string_release(pSymbolName
);
225 /*****************************************************************************/
226 /* osl_getModuleURLFromAddress */
227 /*****************************************************************************/
228 sal_Bool SAL_CALL
osl_getModuleURLFromAddress(void * addr
, rtl_uString
** ppLibraryUrl
)
230 sal_Bool result
= sal_False
;
233 if ((result
= dladdr(addr
, &dl_info
)) != 0)
235 rtl_uString
* workDir
= NULL
;
236 osl_getProcessWorkingDir(&workDir
);
239 #if OSL_DEBUG_LEVEL > 1
240 OSL_TRACE("module.c::osl_getModuleURLFromAddress - %s\n", dl_info
.dli_fname
);
242 rtl_string2UString(ppLibraryUrl
,
244 strlen(dl_info
.dli_fname
),
245 osl_getThreadTextEncoding(),
246 OSTRING_TO_OUSTRING_CVTFLAGS
);
248 OSL_ASSERT(*ppLibraryUrl
!= NULL
);
249 osl_getFileURLFromSystemPath(*ppLibraryUrl
, ppLibraryUrl
);
250 osl_getAbsoluteFileURL(workDir
, *ppLibraryUrl
, ppLibraryUrl
);
252 rtl_uString_release(workDir
);
263 /*****************************************************************************/
264 /* osl_getModuleURLFromFunctionAddress */
265 /*****************************************************************************/
266 sal_Bool SAL_CALL
osl_getModuleURLFromFunctionAddress(oslGenericFunction addr
, rtl_uString
** ppLibraryUrl
)
268 return osl_getModuleURLFromAddress((void*)addr
, ppLibraryUrl
);