2 #include "ace/OS_NS_macros.h"
3 #include "ace/OS_NS_errno.h"
4 #include "ace/OS_NS_fcntl.h"
5 #include "ace/OS_NS_string.h"
6 #include "ace/OS_NS_unistd.h"
7 #include "ace/Default_Constants.h"
8 #include "ace/os_include/os_fcntl.h"
9 #include "ace/os_include/os_string.h"
11 #if defined (ACE_USES_ASM_SYMBOL_IN_DLSYM)
12 # include "ace/OS_Memory.h"
13 # include "ace/OS_NS_string.h"
14 #endif /* ACE_USES_ASM_SYMBOL_IN_DLSYM */
16 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 ACE_OS::dlclose (ACE_SHLIB_HANDLE handle)
21 ACE_OS_TRACE ("ACE_OS::dlclose");
22 #if defined (ACE_LACKS_DLCLOSE)
23 ACE_UNUSED_ARG (handle);
25 #elif defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
27 # if !defined (ACE_HAS_AUTOMATIC_INIT_FINI)
28 // SunOS4 does not automatically call _fini()!
31 ACE_OSCALL (::dlsym (handle, ACE_TEXT ("_fini")), void *, ptr);
34 (*((int (*)(void)) ptr)) (); // Call _fini hook explicitly.
35 # endif /* ACE_HAS_AUTOMATIC_INIT_FINI */
36 return ::dlclose (handle);
37 #elif defined (ACE_WIN32)
38 ACE_WIN32CALL_RETURN (ACE_ADAPT_RETVAL (::FreeLibrary (handle), ace_result_), int, -1);
40 ACE_UNUSED_ARG (handle);
41 ACE_NOTSUP_RETURN (-1);
42 #endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
45 ACE_INLINE ACE_TCHAR *
48 ACE_OS_TRACE ("ACE_OS::dlerror");
49 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
51 ACE_OSCALL (::dlerror (), const char *, err);
54 # if defined (ACE_USES_WCHAR)
55 const size_t BufLen = 256;
56 static wchar_t buf[BufLen];
57 ACE_OS::strncpy (buf, ACE_TEXT_CHAR_TO_TCHAR (err), BufLen);
60 return const_cast <char *> (err);
61 # endif /* ACE_USES_WCHAR */
62 # elif defined (ACE_VXWORKS)
63 //FUZZ: disable check_for_lack_ACE_OS
64 return ::strerror(errno);
65 //FUZZ: enable check_for_lack_ACE_OS
66 # elif defined (ACE_WIN32)
67 static ACE_TCHAR buf[128];
68 ACE_TEXT_FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
73 sizeof buf / sizeof buf[0],
77 ACE_NOTSUP_RETURN (0);
78 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
81 ACE_INLINE ACE_SHLIB_HANDLE
82 ACE_OS::dlopen (const ACE_TCHAR *fname,
85 ACE_OS_TRACE ("ACE_OS::dlopen");
87 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
89 ACE_OSCALL (::dlopen (ACE_TEXT_ALWAYS_CHAR (fname), mode), void *, handle);
90 # if !defined (ACE_HAS_AUTOMATIC_INIT_FINI)
94 // Some systems (e.g., SunOS4) do not automatically call _init(), so
95 // we'll have to call it manually.
97 ACE_OSCALL (::dlsym (handle, ACE_TEXT ("_init")), void *, ptr);
99 if (ptr != 0 && (*((int (*)(void)) ptr)) () == -1) // Call _init hook explicitly.
101 // Close down the handle to prevent leaks.
106 # endif /* ACE_HAS_AUTOMATIC_INIT_FINI */
108 # elif defined (ACE_WIN32)
109 ACE_UNUSED_ARG (mode);
111 ACE_WIN32CALL_RETURN (ACE_TEXT_LoadLibrary (fname), ACE_SHLIB_HANDLE, 0);
112 # elif defined (ACE_VXWORKS) && !defined (__RTP__)
113 ACE_UNUSED_ARG (mode);
116 ACE_HANDLE filehandle = ACE_OS::open (fname,
118 ACE_DEFAULT_FILE_PERMS);
120 if (filehandle != ACE_INVALID_HANDLE)
122 ACE_OS::last_error(0);
123 ACE_OSCALL ( ::loadModule (filehandle, LOAD_GLOBAL_SYMBOLS|LOAD_COMMON_MATCH_ALL ), MODULE *, handle);
124 int loaderror = ACE_OS::last_error();
125 ACE_OS::close (filehandle);
127 if ( (loaderror != 0) && (handle != 0) )
129 // ouch something went wrong most likely unresolved externals
131 ::unldByModuleId ( handle, 0 );
137 // couldn't open file
142 ACE_UNUSED_ARG (fname);
143 ACE_UNUSED_ARG (mode);
144 ACE_NOTSUP_RETURN (0);
145 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
149 ACE_OS::dlsym (ACE_SHLIB_HANDLE handle,
150 const ACE_TCHAR *sname)
152 ACE_OS_TRACE ("ACE_OS::dlsym");
154 // Check if the handle is valid before making any calls using it.
155 if (handle == ACE_SHLIB_INVALID_HANDLE)
158 // Get the correct OS type.
159 #if defined (ACE_USES_WCHAR)
160 ACE_Wide_To_Ascii w_sname (sname);
161 char *symbolname = w_sname.char_rep ();
162 #elif defined (ACE_VXWORKS)
163 char *symbolname = const_cast<char *> (sname);
165 const char *symbolname = sname;
166 #endif /* ACE_USES_WCHAR */
168 # if defined (ACE_HAS_SVR4_DYNAMIC_LINKING)
170 # if defined (ACE_USES_ASM_SYMBOL_IN_DLSYM)
171 int const l = ACE_OS::strlen (symbolname) + 2;
172 char *asm_symbolname {};
173 ACE_NEW_RETURN (asm_symbolname, char[l], 0);
174 ACE_OS::strcpy (asm_symbolname, "_") ;
175 ACE_OS::strcpy (asm_symbolname + 1, symbolname) ;
177 ACE_OSCALL (::dlsym (handle, asm_symbolname), void *, ace_result);
178 delete [] asm_symbolname;
181 return ::dlsym (handle, symbolname);
182 # endif /* ACE_USES_ASM_SYMBOL_IN_DLSYM */
183 # elif defined (ACE_WIN32)
184 ACE_WIN32CALL_RETURN (::GetProcAddress (handle, symbolname), void *, 0);
185 # elif defined (ACE_VXWORKS) && !defined (__RTP__)
186 // For now we use the VxWorks global symbol table
187 // which resolves the most recently loaded symbols, which resolve
188 // mostly what we want..
189 ACE_UNUSED_ARG (handle);
190 #if (ACE_VXWORKS < 0x690)
194 ACE_OSCALL (::symFindByName(sysSymTbl, symbolname, &value, &symtype), int, status);
196 return status == OK ? reinterpret_cast <void*>(value) : nullptr;
200 SYMBOL_DESC symbolDesc; /* symFind() descriptor */
201 ACE_OS::memset (&symbolDesc, 0, sizeof (SYMBOL_DESC));
202 symbolDesc.mask = SYM_FIND_BY_NAME;
203 symbolDesc.name = symbolname;
205 ACE_OSCALL (::symFind(sysSymTbl, &symbolDesc), int, status);
207 return status == OK ? reinterpret_cast <void*>(symbolDesc.value) : nullptr;
208 #endif /* (ACE_VXWORKS < 0x690) */
211 ACE_UNUSED_ARG (handle);
212 ACE_UNUSED_ARG (symbolname);
213 ACE_NOTSUP_RETURN (nullptr);
214 # endif /* ACE_HAS_SVR4_DYNAMIC_LINKING */
217 ACE_END_VERSIONED_NAMESPACE_DECL