xref: /third_party/libuv/src/win/dl.c (revision e66f31c5)
1/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22#include "uv.h"
23#include "internal.h"
24
25static int uv__dlerror(uv_lib_t* lib, const char* filename, DWORD errorno);
26
27
28int uv_dlopen(const char* filename, uv_lib_t* lib) {
29  WCHAR filename_w[32768];
30  ssize_t r;
31
32  lib->handle = NULL;
33  lib->errmsg = NULL;
34
35  r = uv_wtf8_length_as_utf16(filename);
36  if (r < 0)
37    return uv__dlerror(lib, filename, ERROR_NO_UNICODE_TRANSLATION);
38  if ((size_t) r > ARRAY_SIZE(filename_w))
39    return uv__dlerror(lib, filename, ERROR_INSUFFICIENT_BUFFER);
40  uv_wtf8_to_utf16(filename, filename_w, r);
41
42  lib->handle = LoadLibraryExW(filename_w, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
43  if (lib->handle == NULL) {
44    return uv__dlerror(lib, filename, GetLastError());
45  }
46
47  return 0;
48}
49
50
51void uv_dlclose(uv_lib_t* lib) {
52  if (lib->errmsg) {
53    LocalFree((void*)lib->errmsg);
54    lib->errmsg = NULL;
55  }
56
57  if (lib->handle) {
58    /* Ignore errors. No good way to signal them without leaking memory. */
59    FreeLibrary(lib->handle);
60    lib->handle = NULL;
61  }
62}
63
64
65int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) {
66  /* Cast though integer to suppress pedantic warning about forbidden cast. */
67  *ptr = (void*)(uintptr_t) GetProcAddress(lib->handle, name);
68  return uv__dlerror(lib, "", *ptr ? 0 : GetLastError());
69}
70
71
72const char* uv_dlerror(const uv_lib_t* lib) {
73  return lib->errmsg ? lib->errmsg : "no error";
74}
75
76
77static void uv__format_fallback_error(uv_lib_t* lib, int errorno){
78  static const CHAR fallback_error[] = "error: %1!d!";
79  DWORD_PTR args[1];
80  args[0] = (DWORD_PTR) errorno;
81
82  FormatMessageA(FORMAT_MESSAGE_FROM_STRING |
83                 FORMAT_MESSAGE_ARGUMENT_ARRAY |
84                 FORMAT_MESSAGE_ALLOCATE_BUFFER,
85                 fallback_error, 0, 0,
86                 (LPSTR) &lib->errmsg,
87                 0, (va_list*) args);
88}
89
90
91
92static int uv__dlerror(uv_lib_t* lib, const char* filename, DWORD errorno) {
93  DWORD_PTR arg;
94  DWORD res;
95  char* msg;
96
97  if (lib->errmsg) {
98    LocalFree(lib->errmsg);
99    lib->errmsg = NULL;
100  }
101
102  if (errorno == 0)
103    return 0;
104
105  res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
106                       FORMAT_MESSAGE_FROM_SYSTEM |
107                       FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
108                       MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
109                       (LPSTR) &lib->errmsg, 0, NULL);
110
111  if (!res && (GetLastError() == ERROR_MUI_FILE_NOT_FOUND ||
112               GetLastError() == ERROR_RESOURCE_TYPE_NOT_FOUND)) {
113    res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
114                         FORMAT_MESSAGE_FROM_SYSTEM |
115                         FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errorno,
116                         0, (LPSTR) &lib->errmsg, 0, NULL);
117  }
118
119  if (res && errorno == ERROR_BAD_EXE_FORMAT && strstr(lib->errmsg, "%1")) {
120    msg = lib->errmsg;
121    lib->errmsg = NULL;
122    arg = (DWORD_PTR) filename;
123    res = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
124                         FORMAT_MESSAGE_ARGUMENT_ARRAY |
125                         FORMAT_MESSAGE_FROM_STRING,
126                         msg,
127                         0, 0, (LPSTR) &lib->errmsg, 0, (va_list*) &arg);
128    LocalFree(msg);
129  }
130
131  if (!res)
132    uv__format_fallback_error(lib, errorno);
133
134  return -1;
135}
136