2000-02-18 03:38:33 +08:00
|
|
|
/* 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. */
|
|
|
|
|
2000-08-03 00:28:18 +08:00
|
|
|
#include "winsup.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
2000-08-22 13:10:20 +08:00
|
|
|
#include "fhandler.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "thread.h"
|
2000-09-08 10:56:55 +08:00
|
|
|
#include "perprocess.h"
|
|
|
|
#include "dlfcn.h"
|
|
|
|
#include "dll_init.h"
|
2000-02-18 03:38:33 +08:00
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* 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. */
|
2000-02-18 03:38:33 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* 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. */
|
2000-02-18 03:38:33 +08:00
|
|
|
static const char * __stdcall
|
|
|
|
check_path_access (const char *mywinenv, const char *name)
|
|
|
|
{
|
2000-04-26 13:13:32 +08:00
|
|
|
path_conv buf;
|
2000-02-18 03:38:33 +08:00
|
|
|
return find_exec (name, buf, mywinenv, TRUE);
|
|
|
|
}
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* 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. */
|
2000-02-18 03:38:33 +08:00
|
|
|
static const char * __stdcall
|
|
|
|
get_full_path_of_dll (const char* str)
|
|
|
|
{
|
|
|
|
int len = (str) ? strlen (str) : 0;
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* NULL or empty string or too long to be legal win32 pathname? */
|
2000-02-18 03:38:33 +08:00
|
|
|
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);
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* Add extension if necessary, but leave a trailing '.', if any, alone.
|
|
|
|
Files with trailing '.'s are handled differently by win32 API. */
|
2000-02-18 03:38:33 +08:00
|
|
|
if (str[len - 1] != '.')
|
|
|
|
{
|
2000-07-16 12:34:05 +08:00
|
|
|
/* 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 */
|
2000-02-18 03:38:33 +08:00
|
|
|
const char *p = strrchr (str, '.');
|
|
|
|
if (!p || isdirsep (p[1]))
|
|
|
|
strcat (name, ".dll");
|
|
|
|
}
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* Deal with fully qualified filename right away. Do the actual
|
|
|
|
conversion to win32 filename just before returning however. */
|
2000-02-18 03:38:33 +08:00
|
|
|
if (isabspath (str))
|
|
|
|
ret = name;
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* current directory */
|
2000-02-18 03:38:33 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* LD_LIBRARY_PATH */
|
2000-02-18 03:38:33 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* 16 bits system directory */
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!ret && (os_being_run == winNT))
|
|
|
|
{
|
2000-07-16 12:34:05 +08:00
|
|
|
/* we assume last dir was xxxxx\SYSTEM32, so we remove 32 */
|
2000-02-18 03:38:33 +08:00
|
|
|
len = strlen (buf);
|
|
|
|
buf[len - 2] = 0;
|
|
|
|
ret = check_access (buf, name);
|
|
|
|
}
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* windows directory */
|
2000-02-18 03:38:33 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* PATH */
|
2000-02-18 03:38:33 +08:00
|
|
|
if (!ret)
|
|
|
|
ret = check_path_access ("PATH=", name);
|
|
|
|
|
2000-07-16 12:34:05 +08:00
|
|
|
/* Now do a final conversion to win32 pathname. This step is necessary
|
|
|
|
to resolve symlinks etc so that win32 API finds the underlying file. */
|
2000-02-18 03:38:33 +08:00
|
|
|
if (ret)
|
|
|
|
{
|
2000-07-18 03:18:21 +08:00
|
|
|
path_conv real_filename (ret, PC_SYM_FOLLOW | PC_FULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
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)
|
|
|
|
{
|
2000-07-16 12:34:05 +08:00
|
|
|
/* handle for the current module */
|
2000-07-15 10:48:11 +08:00
|
|
|
ret = (void *) GetModuleHandle (NULL);
|
2000-02-18 03:38:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-07-16 12:34:05 +08:00
|
|
|
/* handle for the named library */
|
2000-02-18 03:38:33 +08:00
|
|
|
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;
|
|
|
|
}
|