OSDN Git Service

* cygtls.h (_threadinfo::call): Remove regparm declaration to work around
[pf3gnuchains/sourceware.git] / winsup / cygwin / dlfcn.cc
1 /* dlfcn.cc
2
3    Copyright 1998, 2000, 2001 Red Hat, Inc.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <ctype.h>
16 #include "security.h"
17 #include "path.h"
18 #include "fhandler.h"
19 #include "perprocess.h"
20 #include "thread.h"
21 #include "dlfcn.h"
22 #include "dll_init.h"
23 #include "cygerrno.h"
24 #include "cygtls.h"
25
26 static void __stdcall
27 set_dl_error (const char *str)
28 {
29   __small_sprintf (_my_tls.locals.dl_buffer, "%s: %E", str);
30   _my_tls.locals.dl_error = 1;
31 }
32
33 /* Look for an executable file given the name and the environment
34    variable to use for searching (eg., PATH); returns the full
35    pathname (static buffer) if found or NULL if not. */
36 inline const char * __stdcall
37 check_path_access (const char *mywinenv, const char *name, path_conv& buf)
38 {
39   return find_exec (name, buf, mywinenv, FE_NNF | FE_NATIVE | FE_CWD);
40 }
41
42 /* Search LD_LIBRARY_PATH for dll, if it exists.
43    Return Windows version of given path. */
44 static const char * __stdcall
45 get_full_path_of_dll (const char* str, char *name)
46 {
47   int len = strlen (str);
48
49   /* empty string or too long to be legal win32 pathname? */
50   if (len == 0 || len >= CYG_MAX_PATH - 1)
51     return str;         /* Yes.  Let caller deal with it. */
52
53   const char *ret;
54
55   strcpy (name, str);   /* Put it somewhere where we can manipulate it. */
56
57   /* Add extension if necessary */
58   if (str[len - 1] != '.')
59     {
60       /* Add .dll only if no extension provided. */
61       const char *p = strrchr (str, '.');
62       if (!p || strpbrk (p, "\\/"))
63         strcat (name, ".dll");
64     }
65
66   path_conv real_filename;
67
68   if (isabspath (name) ||
69       (ret = check_path_access ("LD_LIBRARY_PATH=", name, real_filename)) == NULL)
70     real_filename.check (name); /* Convert */
71
72   if (!real_filename.error)
73     ret = strcpy (name, real_filename);
74   else
75     {
76       set_errno (real_filename.error);
77       ret = NULL;
78     }
79
80   return ret;
81 }
82
83 void *
84 dlopen (const char *name, int)
85 {
86   void *ret;
87
88   if (name == NULL)
89     ret = (void *) GetModuleHandle (NULL); /* handle for the current module */
90   else
91     {
92       char buf[CYG_MAX_PATH];
93       /* handle for the named library */
94       const char *fullpath = get_full_path_of_dll (name, buf);
95       if (!fullpath)
96         ret = NULL;
97       else
98         {
99           ret = (void *) LoadLibrary (fullpath);
100           if (ret == NULL)
101             __seterrno ();
102         }
103     }
104
105   if (!ret)
106     set_dl_error ("dlopen");
107   debug_printf ("ret %p", ret);
108
109   return ret;
110 }
111
112 void *
113 dlsym (void *handle, const char *name)
114 {
115   void *ret = (void *) GetProcAddress ((HMODULE) handle, name);
116   if (!ret)
117     set_dl_error ("dlsym");
118   debug_printf ("ret %p", ret);
119   return ret;
120 }
121
122 int
123 dlclose (void *handle)
124 {
125   int ret = -1;
126   void *temphandle = (void *) GetModuleHandle (NULL);
127   if (temphandle == handle || FreeLibrary ((HMODULE) handle))
128     ret = 0;
129   if (ret)
130     set_dl_error ("dlclose");
131   CloseHandle ((HMODULE) temphandle);
132   return ret;
133 }
134
135 char *
136 dlerror ()
137 {
138   char *res;
139   if (!_my_tls.locals.dl_error)
140     res = NULL;
141   else
142     {
143       _my_tls.locals.dl_error = 0;
144       res = _my_tls.locals.dl_buffer;
145     }
146   return res;
147 }