]> sourceware.org Git - newlib-cygwin.git/blame - winsup/cygwin/dlfcn.cc
* Merge in cygwin-64bit-branch.
[newlib-cygwin.git] / winsup / cygwin / dlfcn.cc
CommitLineData
1fd5e000
CF
1/* dlfcn.cc
2
bc837d22 3 Copyright 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
61522196 4 2010, 2011, 2013 Red Hat, Inc.
1fd5e000
CF
5
6This file is part of Cygwin.
7
8This software is a copyrighted work licensed under the terms of the
9Cygwin license. Please consult the file "CYGWIN_LICENSE" for
10details. */
11
4c8d72de 12#include "winsup.h"
599b41c4 13#include <psapi.h>
1fd5e000 14#include <stdlib.h>
1fd5e000 15#include <ctype.h>
7ac61736 16#include "path.h"
95a8465b 17#include "perprocess.h"
f0338f54 18#include "dlfcn.h"
29d52c8a 19#include "cygtls.h"
0c4cb560 20#include "tls_pbuf.h"
31ddf45d 21#include "ntdll.h"
1fd5e000
CF
22
23static void __stdcall
24set_dl_error (const char *str)
25{
ac5e21b0 26 strcpy (_my_tls.locals.dl_buffer, strerror (get_errno ()));
29d52c8a 27 _my_tls.locals.dl_error = 1;
1fd5e000
CF
28}
29
01e6597b
CF
30/* Look for an executable file given the name and the environment
31 variable to use for searching (eg., PATH); returns the full
32 pathname (static buffer) if found or NULL if not. */
aa73152e
CF
33inline const char * __stdcall
34check_path_access (const char *mywinenv, const char *name, path_conv& buf)
1fd5e000 35{
4b84e3dc 36 return find_exec (name, buf, mywinenv, FE_NNF | FE_NATIVE | FE_CWD | FE_DLL);
1fd5e000
CF
37}
38
e2d88014
CV
39/* Search LD_LIBRARY_PATH for dll, if it exists. Search /usr/bin and /usr/lib
40 by default. Return valid full path in path_conv real_filename. */
41static inline bool
42gfpod_helper (const char *name, path_conv &real_filename)
43{
44 if (isabspath (name))
45 real_filename.check (name, PC_SYM_FOLLOW | PC_NULLEMPTY);
46 else if (!check_path_access ("LD_LIBRARY_PATH=", name, real_filename))
47 check_path_access ("/usr/bin:/usr/lib", name, real_filename);
48 if (!real_filename.exists ())
49 real_filename.error = ENOENT;
50 return !real_filename.error;
51}
52
54155bc3
CV
53static bool __stdcall
54get_full_path_of_dll (const char* str, path_conv &real_filename)
1fd5e000 55{
aa73152e 56 int len = strlen (str);
1fd5e000 57
54155bc3
CV
58 /* empty string? */
59 if (len == 0)
60 {
61 set_errno (EINVAL);
62 return false; /* Yes. Let caller deal with it. */
63 }
1fd5e000 64
54155bc3
CV
65 tmp_pathbuf tp;
66 char *name = tp.c_get ();
1fd5e000 67
aa73152e 68 strcpy (name, str); /* Put it somewhere where we can manipulate it. */
1fd5e000 69
e2d88014
CV
70 char *basename = strrchr (name, '/');
71 basename = basename ? basename + 1 : name;
72 char *suffix = strrchr (name, '.');
73 if (suffix && suffix < basename)
74 suffix = NULL;
75
76 /* Is suffix ".so"? */
77 if (suffix && !strcmp (suffix, ".so"))
78 {
79 /* Does the file exist? */
80 if (gfpod_helper (name, real_filename))
81 return true;
82 /* No, replace ".so" with ".dll". */
83 strcpy (suffix, ".dll");
84 }
85 /* Does the filename start with "lib"? */
86 if (!strncmp (basename, "lib", 3))
87 {
88 /* Yes, replace "lib" with "cyg". */
89 strncpy (basename, "cyg", 3);
90 /* Does the file exist? */
91 if (gfpod_helper (name, real_filename))
92 return true;
93 /* No, revert back to "lib". */
94 strncpy (basename, "lib", 3);
95 }
96 if (gfpod_helper (name, real_filename))
97 return true;
1fd5e000 98
e2d88014
CV
99 /* If nothing worked, create a relative path from the original incoming
100 filename and let LoadLibrary search for it using the system default
101 DLL search path. */
102 real_filename.check (str, PC_SYM_FOLLOW | PC_NOFULL | PC_NULLEMPTY);
aec297d5 103 if (!real_filename.error)
54155bc3 104 return true;
1fd5e000 105
aec297d5 106 set_errno (real_filename.error);
54155bc3 107 return false;
1fd5e000
CF
108}
109
6bc64eac
CV
110extern "C" void *
111dlopen (const char *name, int flags)
1fd5e000 112{
6bc64eac 113 void *ret = NULL;
1fd5e000 114
aa73152e 115 if (name == NULL)
31ddf45d
CV
116 {
117 ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
118 if (!ret)
b86f999a 119 __seterrno ();
31ddf45d 120 }
1fd5e000
CF
121 else
122 {
01e6597b 123 /* handle for the named library */
54155bc3 124 path_conv pc;
6bc64eac 125 if (get_full_path_of_dll (name, pc))
aa73152e 126 {
54155bc3
CV
127 tmp_pathbuf tp;
128 wchar_t *path = tp.w_get ();
129
130 pc.get_wide_win32_path (path);
df958670 131 /* Check if the last path component contains a dot. If so,
6bc64eac 132 leave the filename alone. Otherwise add a trailing dot
df958670
CV
133 to override LoadLibrary's automatic adding of a ".dll" suffix. */
134 wchar_t *last_bs = wcsrchr (path, L'\\');
135 if (last_bs && !wcschr (last_bs, L'.'))
136 wcscat (last_bs, L".");
ce5eb135
CV
137
138 /* Workaround for broken DLLs built against Cygwin versions 1.7.0-49
139 up to 1.7.0-57. They override the cxx_malloc pointer in their
140 DLL initialization code even if loaded dynamically. This is a
141 no-no since a later dlclose lets cxx_malloc point into nirvana.
142 The below kludge "fixes" that by reverting the original cxx_malloc
143 pointer after LoadLibrary. This implies that their overrides
144 won't be applied; that's OK. All overrides should be present at
145 final link time, as Windows doesn't allow undefined references;
146 it would actually be wrong for a dlopen'd DLL to opportunistically
147 override functions in a way that wasn't known then. We're not
148 going to try and reproduce the full ELF dynamic loader here! */
149
150 /* Store original cxx_malloc pointer. */
151 struct per_process_cxx_malloc *tmp_malloc;
152 tmp_malloc = __cygwin_user_data.cxx_malloc;
153
6bc64eac
CV
154 if (!(flags & RTLD_NOLOAD)
155 || (ret = GetModuleHandleW (path)) != NULL)
156 {
157 ret = (void *) LoadLibraryW (path);
61522196
CV
158 if (ret && (flags & RTLD_NODELETE))
159 GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_PIN, path,
160 (HMODULE *) &ret);
6bc64eac 161 }
ce5eb135
CV
162
163 /* Restore original cxx_malloc pointer. */
164 __cygwin_user_data.cxx_malloc = tmp_malloc;
165
31ddf45d 166 if (!ret)
aa73152e
CF
167 __seterrno ();
168 }
1fd5e000
CF
169 }
170
171 if (!ret)
172 set_dl_error ("dlopen");
173 debug_printf ("ret %p", ret);
174
1fd5e000
CF
175 return ret;
176}
177
6bc64eac 178extern "C" void *
1fd5e000
CF
179dlsym (void *handle, const char *name)
180{
599b41c4 181 void *ret = NULL;
31ddf45d 182
599b41c4
CV
183 if (handle == RTLD_DEFAULT)
184 { /* search all modules */
31ddf45d
CV
185 PDEBUG_BUFFER buf;
186 NTSTATUS status;
187
188 buf = RtlCreateQueryDebugBuffer (0, FALSE);
189 if (!buf)
05726ddd 190 {
31ddf45d 191 set_errno (ENOMEM);
05726ddd
CF
192 set_dl_error ("dlsym");
193 return NULL;
194 }
31ddf45d
CV
195 status = RtlQueryProcessDebugInformation (GetCurrentProcessId (),
196 PDI_MODULES, buf);
197 if (!NT_SUCCESS (status))
198 __seterrno_from_nt_status (status);
199 else
200 {
201 PDEBUG_MODULE_ARRAY mods = (PDEBUG_MODULE_ARRAY)
202 buf->ModuleInformation;
203 for (ULONG i = 0; i < mods->Count; ++i)
204 if ((ret = (void *)
205 GetProcAddress ((HMODULE) mods->Modules[i].Base, name)))
206 break;
207 if (!ret)
208 set_errno (ENOENT);
209 }
210 RtlDestroyQueryDebugBuffer (buf);
599b41c4
CV
211 }
212 else
31ddf45d
CV
213 {
214 ret = (void *) GetProcAddress ((HMODULE) handle, name);
215 if (!ret)
216 __seterrno ();
217 }
1fd5e000
CF
218 if (!ret)
219 set_dl_error ("dlsym");
220 debug_printf ("ret %p", ret);
221 return ret;
222}
223
6bc64eac 224extern "C" int
1fd5e000
CF
225dlclose (void *handle)
226{
d5d5bf4d
CF
227 int ret;
228 if (handle == GetModuleHandle (NULL))
1fd5e000 229 ret = 0;
98a97ac6
CF
230 else if (FreeLibrary ((HMODULE) handle))
231 ret = 0;
d5d5bf4d 232 else
98a97ac6 233 ret = -1;
1fd5e000
CF
234 if (ret)
235 set_dl_error ("dlclose");
1fd5e000
CF
236 return ret;
237}
238
6bc64eac 239extern "C" char *
1fd5e000
CF
240dlerror ()
241{
a5a93a62 242 char *res;
29d52c8a 243 if (!_my_tls.locals.dl_error)
a5a93a62
CF
244 res = NULL;
245 else
246 {
29d52c8a
CF
247 _my_tls.locals.dl_error = 0;
248 res = _my_tls.locals.dl_buffer;
a5a93a62
CF
249 }
250 return res;
1fd5e000 251}
This page took 0.413815 seconds and 5 git commands to generate.