]>
sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/dlfcn.cc
3 Copyright 1998, 2000, 2001, 2002, 2003, 2004, 2008 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
16 #include "perprocess.h"
22 set_dl_error (const char *str
)
24 strcpy (_my_tls
.locals
.dl_buffer
, strerror (get_errno ()));
25 _my_tls
.locals
.dl_error
= 1;
28 /* Look for an executable file given the name and the environment
29 variable to use for searching (eg., PATH); returns the full
30 pathname (static buffer) if found or NULL if not. */
31 inline const char * __stdcall
32 check_path_access (const char *mywinenv
, const char *name
, path_conv
& buf
)
34 return find_exec (name
, buf
, mywinenv
, FE_NNF
| FE_NATIVE
| FE_CWD
| FE_DLL
);
37 /* Search LD_LIBRARY_PATH for dll, if it exists.
38 Return Windows version of given path. */
39 static const char * __stdcall
40 get_full_path_of_dll (const char* str
, char *name
)
42 int len
= strlen (str
);
44 /* empty string or too long to be legal win32 pathname? */
45 if (len
== 0 || len
>= PATH_MAX
)
46 return str
; /* Yes. Let caller deal with it. */
50 strcpy (name
, str
); /* Put it somewhere where we can manipulate it. */
52 /* Add extension if necessary */
53 if (str
[len
- 1] != '.')
55 /* Add .dll only if no extension provided. */
56 const char *p
= strrchr (str
, '.');
57 if (!p
|| strpbrk (p
, "\\/"))
58 strcat (name
, ".dll");
61 path_conv real_filename
;
63 if (isabspath (name
) ||
64 (ret
= check_path_access ("LD_LIBRARY_PATH=", name
, real_filename
)
65 ?: check_path_access ("/usr/lib", name
, real_filename
)) == NULL
)
66 real_filename
.check (name
, PC_SYM_FOLLOW
| PC_NOFULL
| PC_NULLEMPTY
); /* Convert */
68 if (!real_filename
.error
)
69 ret
= strcpy (name
, real_filename
.get_win32 ());
72 set_errno (real_filename
.error
);
80 dlopen (const char *name
, int)
85 ret
= (void *) GetModuleHandle (NULL
); /* handle for the current module */
89 char *buf
= tp
.c_get ();
90 /* handle for the named library */
91 const char *fullpath
= get_full_path_of_dll (name
, buf
);
96 ret
= (void *) LoadLibrary (fullpath
);
103 set_dl_error ("dlopen");
104 debug_printf ("ret %p", ret
);
110 dlsym (void *handle
, const char *name
)
113 if (handle
== RTLD_DEFAULT
)
114 { /* search all modules */
115 HANDLE cur_proc
= GetCurrentProcess ();
118 if (!EnumProcessModules (cur_proc
, NULL
, 0, &needed
))
121 set_dl_error ("dlsym");
124 modules
= (HMODULE
*) alloca (needed
);
125 if (!EnumProcessModules (cur_proc
, modules
, needed
, &needed
))
127 for (i
= 0; i
< needed
/ sizeof (HMODULE
); i
++)
128 if ((ret
= (void *) GetProcAddress (modules
[i
], name
)))
132 ret
= (void *) GetProcAddress ((HMODULE
)handle
, name
);
134 set_dl_error ("dlsym");
135 debug_printf ("ret %p", ret
);
140 dlclose (void *handle
)
143 if (handle
== GetModuleHandle (NULL
) || FreeLibrary ((HMODULE
) handle
))
146 set_dl_error ("dlclose");
154 if (!_my_tls
.locals
.dl_error
)
158 _my_tls
.locals
.dl_error
= 0;
159 res
= _my_tls
.locals
.dl_buffer
;
This page took 0.040313 seconds and 5 git commands to generate.