3 // $Id: OS_NS_dlfcn.inl 80826 2008-03-04 14:51:23Z wotte $
5 #include "ace/OS_NS_macros.h"
6 #include "ace/OS_NS_errno.h"
7 #include "ace/OS_NS_fcntl.h"
8 #include "ace/OS_NS_string.h"
9 #include "ace/OS_NS_unistd.h"
10 #include "ace/Default_Constants.h"
11 #include "ace/os_include/os_fcntl.h"
12 #include "ace/os_include/os_string.h"
14 #if defined (ACE_WIN32) && defined (ACE_HAS_PHARLAP)
15 # include "ace/OS_NS_stdio.h"
18 #if defined (ACE_USES_ASM_SYMBOL_IN_DLSYM)
19 # include "ace/OS_Memory.h"
20 # include "ace/OS_NS_string.h"
21 #endif /* ACE_USES_ASM_SYMBOL_IN_DLSYM */
23 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
26 ACE_OS::dlclose (ACE_SHLIB_HANDLE handle)
28 ACE_OS_TRACE ("ACE_OS::dlclose");
29 #if defined (ACE_LACKS_DLCLOSE)
30 ACE_UNUSED_ARG (handle);
32 #elif defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
34 # if !defined (ACE_HAS_AUTOMATIC_INIT_FINI)
35 // SunOS4 does not automatically call _fini()!
38 ACE_OSCALL (::dlsym (handle, ACE_TEXT ("_fini")), void *, 0, ptr);
41 (*((int (*)(void)) ptr)) (); // Call _fini hook explicitly.
42 # endif /* ACE_HAS_AUTOMATIC_INIT_FINI */
44 ACE_OSCALL_RETURN (::_dlclose (handle), int, -1);
46 ACE_OSCALL_RETURN (::dlclose (handle), int, -1);
48 #elif defined (ACE_WIN32)
49 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::FreeLibrary (handle), ace_result_), int, -1);
50 #elif defined (__hpux)
51 // HP-UX 10.x and 32-bit 11.00 do not pay attention to the ref count
52 // when unloading a dynamic lib. So, if the ref count is more than
53 // 1, do not unload the lib. This will cause a library loaded more
54 // than once to not be unloaded until the process runs down, but
55 // that's life. It's better than unloading a library that's in use.
56 // So far as I know, there's no way to decrement the refcnt that the
57 // kernel is looking at - the shl_descriptor is a copy of what the
58 // kernel has, not the actual struct. On 64-bit HP-UX using dlopen,
59 // this problem has been fixed.
60 struct shl_descriptor desc;
61 if (shl_gethandle_r (handle, &desc) == -1)
63 if (desc.ref_count > 1)
65 # if defined(__GNUC__) || __cplusplus >= 199707L
66 ACE_OSCALL_RETURN (::shl_unload (handle), int, -1);
68 ACE_OSCALL_RETURN (::cxxshl_unload (handle), int, -1);
69 # endif /* aC++ vs. Hp C++ */
71 ACE_UNUSED_ARG (handle);
72 ACE_NOTSUP_RETURN (-1);
73 #endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
76 ACE_INLINE ACE_TCHAR *
77 ACE_OS::dlerror (void)
79 ACE_OS_TRACE ("ACE_OS::dlerror");
80 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
83 ACE_OSCALL (::_dlerror (), const char *, 0, err);
85 ACE_OSCALL (::dlerror (), const char *, 0, err);
89 # if defined (ACE_USES_WCHAR)
90 const size_t BufLen = 256;
91 static wchar_t buf[BufLen];
92 ACE_OS::strncpy (buf, ACE_TEXT_CHAR_TO_TCHAR (err), BufLen);
95 return const_cast <char *> (err);
96 # endif /* ACE_USES_WCHAR */
97 # elif defined (__hpux) || defined (ACE_VXWORKS)
98 //FUZZ: disable check_for_lack_ACE_OS
99 ACE_OSCALL_RETURN (::strerror(errno), char *, 0);
100 //FUZZ: enable check_for_lack_ACE_OS
101 # elif defined (ACE_WIN32)
102 static ACE_TCHAR buf[128];
103 # if defined (ACE_HAS_PHARLAP)
104 ACE_OS::sprintf (buf, "error code %d", GetLastError());
106 ACE_TEXT_FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
111 sizeof buf / sizeof buf[0],
113 # endif /* ACE_HAS_PHARLAP */
116 ACE_NOTSUP_RETURN (0);
117 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
120 ACE_INLINE ACE_SHLIB_HANDLE
121 ACE_OS::dlopen (const ACE_TCHAR *fname,
124 ACE_OS_TRACE ("ACE_OS::dlopen");
126 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
128 # if defined (ACE_HAS_SGIDLADD)
130 (::sgidladd (ACE_TEXT_ALWAYS_CHAR (fname), mode), void *, 0, handle);
131 # elif defined (_M_UNIX)
133 (::_dlopen (ACE_TEXT_ALWAYS_CHAR (fname), mode), void *, 0, handle);
136 (::dlopen (ACE_TEXT_ALWAYS_CHAR (fname), mode), void *, 0, handle);
137 # endif /* ACE_HAS_SGIDLADD */
138 # if !defined (ACE_HAS_AUTOMATIC_INIT_FINI)
142 // Some systems (e.g., SunOS4) do not automatically call _init(), so
143 // we'll have to call it manually.
145 ACE_OSCALL (::dlsym (handle, ACE_TEXT ("_init")), void *, 0, ptr);
147 if (ptr != 0 && (*((int (*)(void)) ptr)) () == -1) // Call _init hook explicitly.
149 // Close down the handle to prevent leaks.
154 # endif /* ACE_HAS_AUTOMATIC_INIT_FINI */
156 # elif defined (ACE_WIN32)
157 ACE_UNUSED_ARG (mode);
159 ACE_WIN32CALL_RETURN (ACE_TEXT_LoadLibrary (fname), ACE_SHLIB_HANDLE, 0);
160 # elif defined (__hpux)
162 # if defined(__GNUC__) || __cplusplus >= 199707L
163 ACE_OSCALL_RETURN (::shl_load(fname, mode, 0L), ACE_SHLIB_HANDLE, 0);
165 ACE_OSCALL_RETURN (::cxxshl_load(fname, mode, 0L), ACE_SHLIB_HANDLE, 0);
166 # endif /* aC++ vs. Hp C++ */
167 # elif defined (ACE_VXWORKS) && !defined (__RTP__)
170 ACE_HANDLE filehandle = ACE_OS::open (fname,
172 ACE_DEFAULT_FILE_PERMS);
174 if (filehandle != ACE_INVALID_HANDLE)
176 ACE_OS::last_error(0);
177 ACE_OSCALL ( ::loadModule (filehandle, LOAD_GLOBAL_SYMBOLS|LOAD_COMMON_MATCH_ALL ), MODULE *, 0, handle);
178 int loaderror = ACE_OS::last_error();
179 ACE_OS::close (filehandle);
181 if ( (loaderror != 0) && (handle != 0) )
183 // ouch something went wrong most likely unresolved externals
185 ::unldByModuleId ( handle, 0 );
191 // couldn't open file
196 ACE_UNUSED_ARG (fname);
197 ACE_UNUSED_ARG (mode);
198 ACE_NOTSUP_RETURN (0);
199 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
203 ACE_OS::dlsym (ACE_SHLIB_HANDLE handle,
204 const ACE_TCHAR *sname)
206 ACE_OS_TRACE ("ACE_OS::dlsym");
208 #if defined (ACE_HAS_DLSYM_SEGFAULT_ON_INVALID_HANDLE)
209 // Check if the handle is valid before making any calls using it.
210 if (handle == ACE_SHLIB_INVALID_HANDLE)
212 #endif /* ACE_HAS_DLSYM_SEGFAULT_ON_INVALID_HANDLE */
214 // Get the correct OS type.
215 #if defined (ACE_HAS_WINCE)
216 // CE (at least thru Pocket PC 2003) offers GetProcAddressW, not ...A, so
217 // we always need a wide-char string.
218 const wchar_t *symbolname = 0;
219 # if defined (ACE_USES_WCHAR)
222 ACE_Ascii_To_Wide sname_xlate (sname);
223 symbolname = sname_xlate.wchar_rep ();
224 # endif /* ACE_USES_WCHAR */
225 #elif defined (ACE_USES_WCHAR)
226 // WinCE is WCHAR always; other platforms need a char * symbol name
227 ACE_Wide_To_Ascii w_sname (sname);
228 char *symbolname = w_sname.char_rep ();
229 #elif defined (ACE_VXWORKS)
230 char *symbolname = const_cast<char *> (sname);
232 const char *symbolname = sname;
233 #endif /* ACE_HAS_WINCE */
235 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
237 # if defined (ACE_USES_ASM_SYMBOL_IN_DLSYM)
238 int l = ACE_OS::strlen (symbolname) + 2;
239 char *asm_symbolname = 0;
240 ACE_NEW_RETURN (asm_symbolname, char[l], 0);
241 ACE_OS::strcpy (asm_symbolname, "_") ;
242 ACE_OS::strcpy (asm_symbolname + 1, symbolname) ;
244 ACE_OSCALL (::dlsym (handle, asm_symbolname), void *, 0, ace_result);
245 delete [] asm_symbolname;
247 # elif defined (_M_UNIX)
248 ACE_OSCALL_RETURN (::_dlsym (handle, symbolname), void *, 0);
250 ACE_OSCALL_RETURN (::dlsym (handle, symbolname), void *, 0);
251 # endif /* ACE_USES_ASM_SYMBOL_IN_DLSYM */
253 # elif defined (ACE_WIN32)
255 ACE_WIN32CALL_RETURN (::GetProcAddress (handle, symbolname), void *, 0);
257 # elif defined (__hpux)
261 shl_t _handle = handle;
262 ACE_OSCALL (::shl_findsym(&_handle, symbolname, TYPE_UNDEFINED, &value), int, -1, status);
263 return status == 0 ? value : 0;
265 # elif defined (ACE_VXWORKS) && !defined (__RTP__)
267 // For now we use the VxWorks global symbol table
268 // which resolves the most recently loaded symbols .. which resolve mostly what we want..
269 ACE_UNUSED_ARG (handle);
273 ACE_OSCALL (::symFindByName(sysSymTbl, symbolname, &value, &symtype), int, -1, status);
275 return status == OK ? reinterpret_cast <void*>(value) : 0;
279 ACE_UNUSED_ARG (handle);
280 ACE_UNUSED_ARG (symbolname);
281 ACE_NOTSUP_RETURN (0);
283 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
286 ACE_END_VERSIONED_NAMESPACE_DECL