loadlibrary.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* loader-loadlibrary.c -- dynamic linking for Win32
  2. Copyright (C) 1998-2000, 2004-2008, 2010-2015 Free Software
  3. Foundation, Inc.
  4. Written by Thomas Tanner, 1998
  5. NOTE: The canonical source of this file is maintained with the
  6. GNU Libtool package. Report bugs to bug-libtool@gnu.org.
  7. GNU Libltdl is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Lesser General Public
  9. License as published by the Free Software Foundation; either
  10. version 2 of the License, or (at your option) any later version.
  11. As a special exception to the GNU Lesser General Public License,
  12. if you distribute this file as part of a program or library that
  13. is built using GNU Libtool, you may include this file under the
  14. same distribution terms that you use for the rest of that program.
  15. GNU Libltdl is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU Lesser General Public License for more details.
  19. You should have received a copy of the GNU Lesser General Public
  20. License along with GNU Libltdl; see the file COPYING.LIB. If not, a
  21. copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
  22. or obtained by writing to the Free Software Foundation, Inc.,
  23. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  24. */
  25. #include "lt__private.h"
  26. #include "lt_dlloader.h"
  27. #if defined __CYGWIN__
  28. # include <sys/cygwin.h>
  29. #endif
  30. /* Use the preprocessor to rename non-static symbols to avoid namespace
  31. collisions when the loader code is statically linked into libltdl.
  32. Use the "<module_name>_LTX_" prefix so that the symbol addresses can
  33. be fetched from the preloaded symbol list by lt_dlsym(): */
  34. #define get_vtable loadlibrary_LTX_get_vtable
  35. LT_BEGIN_C_DECLS
  36. LT_SCOPE lt_dlvtable *get_vtable (lt_user_data loader_data);
  37. LT_END_C_DECLS
  38. /* Boilerplate code to set up the vtable for hooking this loader into
  39. libltdl's loader list: */
  40. static int vl_exit (lt_user_data loader_data);
  41. static lt_module vm_open (lt_user_data loader_data, const char *filename,
  42. lt_dladvise advise);
  43. static int vm_close (lt_user_data loader_data, lt_module module);
  44. static void * vm_sym (lt_user_data loader_data, lt_module module,
  45. const char *symbolname);
  46. static lt_dlinterface_id iface_id = 0;
  47. static lt_dlvtable *vtable = 0;
  48. /* Return the vtable for this loader, only the name and sym_prefix
  49. attributes (plus the virtual function implementations, obviously)
  50. change between loaders. */
  51. lt_dlvtable *
  52. get_vtable (lt_user_data loader_data)
  53. {
  54. if (!vtable)
  55. {
  56. vtable = (lt_dlvtable *) lt__zalloc (sizeof *vtable);
  57. iface_id = lt_dlinterface_register ("ltdl loadlibrary", NULL);
  58. }
  59. if (vtable && !vtable->name)
  60. {
  61. vtable->name = "lt_loadlibrary";
  62. vtable->module_open = vm_open;
  63. vtable->module_close = vm_close;
  64. vtable->find_sym = vm_sym;
  65. vtable->dlloader_exit = vl_exit;
  66. vtable->dlloader_data = loader_data;
  67. vtable->priority = LT_DLLOADER_APPEND;
  68. }
  69. if (vtable && (vtable->dlloader_data != loader_data))
  70. {
  71. LT__SETERROR (INIT_LOADER);
  72. return 0;
  73. }
  74. return vtable;
  75. }
  76. /* --- IMPLEMENTATION --- */
  77. #include <windows.h>
  78. #define LOCALFREE(mem) LT_STMT_START { \
  79. if (mem) { LocalFree ((void *)mem); mem = NULL; } } LT_STMT_END
  80. #define LOADLIB__SETERROR(errmsg) LT__SETERRORSTR (loadlibraryerror (errmsg))
  81. #define LOADLIB_SETERROR(errcode) LOADLIB__SETERROR (LT__STRERROR (errcode))
  82. static const char *loadlibraryerror (const char *default_errmsg);
  83. static DWORD WINAPI wrap_getthreaderrormode (void);
  84. static DWORD WINAPI fallback_getthreaderrormode (void);
  85. static BOOL WINAPI wrap_setthreaderrormode (DWORD mode, DWORD *oldmode);
  86. static BOOL WINAPI fallback_setthreaderrormode (DWORD mode, DWORD *oldmode);
  87. typedef DWORD (WINAPI getthreaderrormode_type) (void);
  88. typedef BOOL (WINAPI setthreaderrormode_type) (DWORD, DWORD *);
  89. static getthreaderrormode_type *getthreaderrormode = wrap_getthreaderrormode;
  90. static setthreaderrormode_type *setthreaderrormode = wrap_setthreaderrormode;
  91. static char *error_message = 0;
  92. /* A function called through the vtable when this loader is no
  93. longer needed by the application. */
  94. static int
  95. vl_exit (lt_user_data loader_data LT__UNUSED)
  96. {
  97. vtable = NULL;
  98. LOCALFREE (error_message);
  99. return 0;
  100. }
  101. /* A function called through the vtable to open a module with this
  102. loader. Returns an opaque representation of the newly opened
  103. module for processing with this loader's other vtable functions. */
  104. static lt_module
  105. vm_open (lt_user_data loader_data LT__UNUSED, const char *filename,
  106. lt_dladvise advise LT__UNUSED)
  107. {
  108. lt_module module = 0;
  109. char *ext;
  110. char wpath[MAX_PATH];
  111. size_t len;
  112. if (!filename)
  113. {
  114. /* Get the name of main module */
  115. *wpath = 0;
  116. GetModuleFileName (NULL, wpath, sizeof (wpath));
  117. filename = wpath;
  118. }
  119. else
  120. {
  121. len = LT_STRLEN (filename);
  122. if (len >= MAX_PATH)
  123. {
  124. LT__SETERROR (CANNOT_OPEN);
  125. return 0;
  126. }
  127. #if HAVE_DECL_CYGWIN_CONV_PATH
  128. if (cygwin_conv_path (CCP_POSIX_TO_WIN_A, filename, wpath, MAX_PATH))
  129. {
  130. LT__SETERROR (CANNOT_OPEN);
  131. return 0;
  132. }
  133. len = 0;
  134. #elif defined __CYGWIN__
  135. cygwin_conv_to_full_win32_path (filename, wpath);
  136. len = 0;
  137. #else
  138. strcpy(wpath, filename);
  139. #endif
  140. ext = strrchr (wpath, '.');
  141. if (!ext)
  142. {
  143. /* Append a '.' to stop Windows from adding an
  144. implicit '.dll' extension. */
  145. if (!len)
  146. len = strlen (wpath);
  147. if (len + 1 >= MAX_PATH)
  148. {
  149. LT__SETERROR (CANNOT_OPEN);
  150. return 0;
  151. }
  152. wpath[len] = '.';
  153. wpath[len+1] = '\0';
  154. }
  155. }
  156. {
  157. /* Silence dialog from LoadLibrary on some failures. */
  158. DWORD errormode = getthreaderrormode ();
  159. DWORD last_error;
  160. setthreaderrormode (errormode | SEM_FAILCRITICALERRORS, NULL);
  161. module = LoadLibrary (wpath);
  162. /* Restore the error mode. */
  163. last_error = GetLastError ();
  164. setthreaderrormode (errormode, NULL);
  165. SetLastError (last_error);
  166. }
  167. /* libltdl expects this function to fail if it is unable
  168. to physically load the library. Sadly, LoadLibrary
  169. will search the loaded libraries for a match and return
  170. one of them if the path search load fails.
  171. We check whether LoadLibrary is returning a handle to
  172. an already loaded module, and simulate failure if we
  173. find one. */
  174. {
  175. lt_dlhandle cur = 0;
  176. while ((cur = lt_dlhandle_iterate (iface_id, cur)))
  177. {
  178. if (!cur->module)
  179. {
  180. cur = 0;
  181. break;
  182. }
  183. if (cur->module == module)
  184. {
  185. break;
  186. }
  187. }
  188. if (!module)
  189. LOADLIB_SETERROR (CANNOT_OPEN);
  190. else if (cur)
  191. {
  192. LT__SETERROR (CANNOT_OPEN);
  193. module = 0;
  194. }
  195. }
  196. return module;
  197. }
  198. /* A function called through the vtable when a particular module
  199. should be unloaded. */
  200. static int
  201. vm_close (lt_user_data loader_data LT__UNUSED, lt_module module)
  202. {
  203. int errors = 0;
  204. if (FreeLibrary ((HMODULE) module) == 0)
  205. {
  206. LOADLIB_SETERROR (CANNOT_CLOSE);
  207. ++errors;
  208. }
  209. return errors;
  210. }
  211. /* A function called through the vtable to get the address of
  212. a symbol loaded from a particular module. */
  213. static void *
  214. vm_sym (lt_user_data loader_data LT__UNUSED, lt_module module, const char *name)
  215. {
  216. void *address = (void *) GetProcAddress ((HMODULE) module, name);
  217. if (!address)
  218. {
  219. LOADLIB_SETERROR (SYMBOL_NOT_FOUND);
  220. }
  221. return address;
  222. }
  223. /* --- HELPER FUNCTIONS --- */
  224. /* Return the windows error message, or the passed in error message on
  225. failure. */
  226. static const char *
  227. loadlibraryerror (const char *default_errmsg)
  228. {
  229. size_t len;
  230. LOCALFREE (error_message);
  231. FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER |
  232. FORMAT_MESSAGE_FROM_SYSTEM |
  233. FORMAT_MESSAGE_IGNORE_INSERTS,
  234. NULL,
  235. GetLastError (),
  236. 0,
  237. (char *) &error_message,
  238. 0, NULL);
  239. /* Remove trailing CRNL */
  240. len = LT_STRLEN (error_message);
  241. if (len && error_message[len - 1] == '\n')
  242. error_message[--len] = LT_EOS_CHAR;
  243. if (len && error_message[len - 1] == '\r')
  244. error_message[--len] = LT_EOS_CHAR;
  245. return len ? error_message : default_errmsg;
  246. }
  247. /* A function called through the getthreaderrormode variable that checks
  248. if the system supports GetThreadErrorMode (or GetErrorMode) and arranges
  249. for it or a fallback implementation to be called directly in the future.
  250. The selected version is then called. */
  251. static DWORD WINAPI
  252. wrap_getthreaderrormode (void)
  253. {
  254. HMODULE kernel32 = GetModuleHandleA ("kernel32.dll");
  255. getthreaderrormode
  256. = (getthreaderrormode_type *) GetProcAddress (kernel32,
  257. "GetThreadErrorMode");
  258. if (!getthreaderrormode)
  259. getthreaderrormode
  260. = (getthreaderrormode_type *) GetProcAddress (kernel32,
  261. "GetErrorMode");
  262. if (!getthreaderrormode)
  263. getthreaderrormode = fallback_getthreaderrormode;
  264. return getthreaderrormode ();
  265. }
  266. /* A function called through the getthreaderrormode variable for cases
  267. where the system does not support GetThreadErrorMode or GetErrorMode */
  268. static DWORD WINAPI
  269. fallback_getthreaderrormode (void)
  270. {
  271. /* Prior to Windows Vista, the only way to get the current error
  272. mode was to set a new one. In our case, we are setting a new
  273. error mode right after "getting" it while ignoring the error
  274. mode in effect when setting the new error mode, so that's
  275. fairly ok. */
  276. return (DWORD) SetErrorMode (SEM_FAILCRITICALERRORS);
  277. }
  278. /* A function called through the setthreaderrormode variable that checks
  279. if the system supports SetThreadErrorMode and arranges for it or a
  280. fallback implementation to be called directly in the future.
  281. The selected version is then called. */
  282. static BOOL WINAPI
  283. wrap_setthreaderrormode (DWORD mode, DWORD *oldmode)
  284. {
  285. HMODULE kernel32 = GetModuleHandleA ("kernel32.dll");
  286. setthreaderrormode
  287. = (setthreaderrormode_type *) GetProcAddress (kernel32,
  288. "SetThreadErrorMode");
  289. if (!setthreaderrormode)
  290. setthreaderrormode = fallback_setthreaderrormode;
  291. return setthreaderrormode (mode, oldmode);
  292. }
  293. /* A function called through the setthreaderrormode variable for cases
  294. where the system does not support SetThreadErrorMode. */
  295. static BOOL WINAPI
  296. fallback_setthreaderrormode (DWORD mode, DWORD *oldmode)
  297. {
  298. /* Prior to Windows 7, there was no way to set the thread local error
  299. mode, so set the process global error mode instead. */
  300. DWORD old = (DWORD) SetErrorMode (mode);
  301. if (oldmode)
  302. *oldmode = old;
  303. return TRUE;
  304. }