Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dlfcn.cc « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb89edf423e451fb35b002cce7b885228394867f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* dlfcn.cc

   Copyright 1998, 2000 Cygnus Solutions

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "winsup.h"
#include <ctype.h>
#include "dlfcn.h"
#include "dll_init.h"

#define _dl_error _reent_winsup()->_dl_error
#define _dl_buffer _reent_winsup()->_dl_buffer

static void __stdcall
set_dl_error (const char *str)
{
  __small_sprintf (_dl_buffer, "%s: %E", str);
  _dl_error = 1;
}

/* Check for existence of a file specified by the directory
   and name components. If successful, return a pointer the
   full pathname (static buffer), else return 0. */
static const char * __stdcall
check_access (const char *dir, const char *name)
{
  static char buf[MAX_PATH];
  const char *ret = 0;

  buf[0] = 0;
  strcpy (buf, dir);
  strcat (buf, "\\");
  strcat (buf, name);

  if (!access (buf, F_OK))
    ret = buf;
  return ret;
}

/* Look for an executable file given the name and the environment
   variable to use for searching (eg., PATH); returns the full
   pathname (static buffer) if found or NULL if not. */
static const char * __stdcall
check_path_access (const char *mywinenv, const char *name)
{
  path_conv buf;
  return find_exec (name, buf, mywinenv, TRUE);
}

/* Simulate the same search as LoadLibary + check environment
   variable LD_LIBRARY_PATH. If found, return the full pathname
   (static buffer); if illegal, return the input string unchanged
   and let the caller deal with it; return NULL otherwise.

   Note that this should never be called with a NULL string, since
   that is the introspective case, and the caller should not call
   this function at all.  */
static const char * __stdcall
get_full_path_of_dll (const char* str)
{
  int len = (str) ? strlen (str) : 0;

  /* NULL or empty string or too long to be legal win32 pathname? */
  if (len == 0 || len >= MAX_PATH - 1)
    return str;

  char buf[MAX_PATH];
  static char name[MAX_PATH];
  const char *ret = 0;

  strcpy (name, str);

  /* Add extension if necessary, but leave a trailing '.', if any, alone.
     Files with trailing '.'s are handled differently by win32 API. */
  if (str[len - 1] != '.')
    {
      /* Add .dll only if no extension provided. Handle various cases:
	   ./shlib		-->	./shlib.dll
	   ./dir/shlib.so	-->	./dir/shlib.so
	   shlib		-->	shlib.dll
	   shlib.dll		-->	shlib.dll
	   shlib.so		-->	shlib.so */
      const char *p = strrchr (str, '.');
      if (!p || isdirsep (p[1]))
	strcat (name, ".dll");
    }

  /* Deal with fully qualified filename right away. Do the actual
     conversion to win32 filename just before returning however. */
  if (isabspath (str))
    ret = name;

  /* current directory */
  if (!ret)
    {
      if (GetCurrentDirectory (MAX_PATH, buf) == 0)
	small_printf ("WARNING: get_full_path_of_dll can't get current directory win32 %E\n");
      else
	ret = check_access (buf, name);
    }

  /* LD_LIBRARY_PATH */
  if (!ret)
    ret = check_path_access ("LD_LIBRARY_PATH=", name);

  if (!ret)
    {
      if (GetSystemDirectory (buf, MAX_PATH) == 0)
	small_printf ("WARNING: get_full_path_of_dll can't get system directory win32 %E\n");
      else
	ret = check_access (buf, name);
    }

  /* 16 bits system directory */
  if (!ret && (os_being_run == winNT))
    {
      /* we assume last dir was xxxxx\SYSTEM32, so we remove 32 */
      len = strlen (buf);
      buf[len - 2] = 0;
      ret = check_access (buf, name);
    }

  /* windows directory */
  if (!ret)
    {
      if (GetWindowsDirectory (buf, MAX_PATH) == 0)
	small_printf ("WARNING: get_full_path_of_dll can't get Windows directory win32 %E\n");
      else
	ret = check_access (buf, name);
    }

  /* PATH */
  if (!ret)
    ret = check_path_access ("PATH=", name);

  /* Now do a final conversion to win32 pathname. This step is necessary
     to resolve symlinks etc so that win32 API finds the underlying file.  */
  if (ret)
    {
      path_conv real_filename (ret, PC_SYM_FOLLOW | PC_FULL);
      if (real_filename.error)
	ret = 0;
      else
	{
	  strcpy (name, real_filename.get_win32 ());
	  ret = name;
	}
    }
  return ret;
}

void *
dlopen (const char *name, int)
{
  SetResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlopen");

  void *ret = 0;

  if (!name)
    {
      /* handle for the current module */
      ret = (void *) GetModuleHandle (NULL);
    }
  else
    {
      /* handle for the named library */
      const char *fullpath = get_full_path_of_dll (name);
      ret = (void *) LoadLibrary (fullpath);
    }

  if (!ret)
    set_dl_error ("dlopen");
  debug_printf ("ret %p", ret);

  ReleaseResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlopen");
  return ret;
}

void *
dlsym (void *handle, const char *name)
{
  void *ret = (void *) GetProcAddress ((HMODULE) handle, name);
  if (!ret)
    set_dl_error ("dlsym");
  debug_printf ("ret %p", ret);
  return ret;
}

int
dlclose (void *handle)
{
  SetResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlclose");

  int ret = -1;
  if (FreeLibrary ((HMODULE) handle))
    ret = 0;
  if (ret)
    set_dl_error ("dlclose");

  ReleaseResourceLock(LOCK_DLL_LIST,READ_LOCK|WRITE_LOCK," dlclose");
  return ret;
}

char *
dlerror ()
{
  char *ret = 0;
  if (_dl_error)
    ret = _dl_buffer;
  return ret;
}