1999-04-18 14:09:21 +02:00
|
|
|
/*
|
|
|
|
* Elf-dll loader functions
|
|
|
|
*
|
|
|
|
* Copyright 1999 Bertho A. Stultiens
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
1999-04-19 18:44:22 +02:00
|
|
|
#include <stdlib.h>
|
1999-04-18 14:09:21 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "windef.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "process.h"
|
|
|
|
#include "module.h"
|
|
|
|
#include "neexe.h"
|
|
|
|
#include "heap.h"
|
|
|
|
#include "wine/winbase16.h"
|
|
|
|
#include "elfdll.h"
|
1999-05-02 16:32:27 +02:00
|
|
|
#include "debugtools.h"
|
1999-04-18 14:09:21 +02:00
|
|
|
#include "winerror.h"
|
|
|
|
|
1999-12-08 04:56:23 +01:00
|
|
|
DEFAULT_DEBUG_CHANNEL(elfdll)
|
1999-04-19 16:56:29 +02:00
|
|
|
|
1999-04-24 14:02:14 +02:00
|
|
|
#if defined(HAVE_DL_API)
|
1999-04-18 14:09:21 +02:00
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
/*------------------ HACKS -----------------*/
|
|
|
|
extern DWORD fixup_imports(WINE_MODREF *wm);
|
1999-05-22 13:25:24 +02:00
|
|
|
extern void dump_exports(HMODULE hModule);
|
1999-04-18 14:09:21 +02:00
|
|
|
/*---------------- END HACKS ---------------*/
|
|
|
|
|
1999-04-22 11:13:38 +02:00
|
|
|
char *extra_ld_library_path = NULL; /* The extra search-path set in wine.conf */
|
|
|
|
|
1999-04-18 14:09:21 +02:00
|
|
|
struct elfdll_image
|
|
|
|
{
|
|
|
|
HMODULE pe_module_start;
|
|
|
|
DWORD pe_module_size;
|
|
|
|
NE_MODULE *ne_module_start;
|
|
|
|
DWORD ne_module_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1999-04-19 18:44:22 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* ELFDLL_dlopen
|
|
|
|
*
|
1999-04-22 11:13:38 +02:00
|
|
|
* Wrapper for dlopen to search the EXTRA_LD_LIBRARY_PATH from wine.conf
|
|
|
|
* manually because libdl.so caches the environment and does not accept our
|
|
|
|
* changes.
|
1999-04-19 18:44:22 +02:00
|
|
|
*/
|
|
|
|
void *ELFDLL_dlopen(const char *libname, int flags)
|
|
|
|
{
|
|
|
|
char buffer[256];
|
|
|
|
int namelen;
|
1999-04-22 11:13:38 +02:00
|
|
|
void *handle;
|
|
|
|
char *ldpath;
|
1999-04-19 18:44:22 +02:00
|
|
|
|
1999-04-22 11:13:38 +02:00
|
|
|
/* First try the default path search of dlopen() */
|
|
|
|
handle = dlopen(libname, flags);
|
2000-10-19 22:25:02 +02:00
|
|
|
/* do NOT call dlerror() here ! (check after return) */
|
1999-04-22 11:13:38 +02:00
|
|
|
if(handle)
|
|
|
|
return handle;
|
1999-04-19 18:44:22 +02:00
|
|
|
|
1999-04-22 11:13:38 +02:00
|
|
|
/* Now try to construct searches through our extra search-path */
|
1999-04-19 18:44:22 +02:00
|
|
|
namelen = strlen(libname);
|
1999-04-22 11:13:38 +02:00
|
|
|
ldpath = extra_ld_library_path;
|
|
|
|
while(ldpath && *ldpath)
|
1999-04-19 18:44:22 +02:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char *cptr;
|
|
|
|
char *from;
|
|
|
|
|
|
|
|
from = ldpath;
|
|
|
|
cptr = strchr(ldpath, ':');
|
|
|
|
if(!cptr)
|
|
|
|
{
|
|
|
|
len = strlen(ldpath);
|
|
|
|
ldpath = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
len = cptr - ldpath;
|
|
|
|
ldpath = cptr + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len + namelen + 1 >= sizeof(buffer))
|
|
|
|
{
|
1999-12-08 04:56:23 +01:00
|
|
|
ERR("Buffer overflow! Check EXTRA_LD_LIBRARY_PATH or increase buffer size.\n");
|
1999-04-19 18:44:22 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strncpy(buffer, from, len);
|
|
|
|
if(len)
|
|
|
|
{
|
|
|
|
buffer[len] = '/';
|
|
|
|
strcpy(buffer + len + 1, libname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strcpy(buffer + len, libname);
|
|
|
|
|
1999-12-08 04:56:23 +01:00
|
|
|
TRACE("Trying dlopen('%s', %d)\n", buffer, flags);
|
1999-04-19 18:44:22 +02:00
|
|
|
|
|
|
|
handle = dlopen(buffer, flags);
|
2000-10-19 22:25:02 +02:00
|
|
|
/* do NOT call dlerror() here ! (check after return) */
|
1999-04-19 18:44:22 +02:00
|
|
|
if(handle)
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-18 14:09:21 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* get_sobasename (internal)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static LPSTR get_sobasename(LPCSTR path, LPSTR name)
|
|
|
|
{
|
|
|
|
char *cptr;
|
|
|
|
|
|
|
|
/* Strip the path from the library name */
|
|
|
|
if((cptr = strrchr(path, '/')))
|
|
|
|
{
|
|
|
|
char *cp = strrchr(cptr+1, '\\');
|
|
|
|
if(cp && cp > cptr)
|
1999-05-08 14:37:34 +02:00
|
|
|
cptr = cp;
|
1999-04-18 14:09:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
cptr = strrchr(path, '\\');
|
|
|
|
|
|
|
|
if(!cptr)
|
|
|
|
cptr = (char *)path; /* No '/' nor '\\' in path */
|
|
|
|
else
|
|
|
|
cptr++;
|
|
|
|
|
|
|
|
strcpy(name, cptr);
|
|
|
|
cptr = strrchr(name, '.');
|
|
|
|
if(cptr)
|
|
|
|
*cptr = '\0'; /* Strip extension */
|
|
|
|
|
|
|
|
/* Convert to lower case.
|
|
|
|
* This must be done manually because it is not sure that
|
|
|
|
* other modules are accessible.
|
|
|
|
*/
|
|
|
|
for(cptr = name; *cptr; cptr++)
|
|
|
|
*cptr = tolower(*cptr);
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* ELFDLL_LoadLibraryExA (internal)
|
|
|
|
*
|
|
|
|
* Implementation of elf-dll loading for PE modules
|
|
|
|
*/
|
2000-03-09 19:18:41 +01:00
|
|
|
WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR path, DWORD flags)
|
1999-04-18 14:09:21 +02:00
|
|
|
{
|
|
|
|
LPVOID dlhandle;
|
|
|
|
struct elfdll_image *image;
|
|
|
|
char name[129];
|
|
|
|
char soname[129];
|
|
|
|
WINE_MODREF *wm;
|
|
|
|
|
|
|
|
get_sobasename(path, name);
|
|
|
|
strcpy(soname, name);
|
|
|
|
strcat(soname, ".so");
|
|
|
|
|
1999-04-19 18:44:22 +02:00
|
|
|
/* Try to open the elf-dll */
|
|
|
|
dlhandle = ELFDLL_dlopen(soname, RTLD_LAZY);
|
1999-04-18 14:09:21 +02:00
|
|
|
if(!dlhandle)
|
|
|
|
{
|
1999-12-08 04:56:23 +01:00
|
|
|
WARN("Could not load %s (%s)\n", soname, dlerror());
|
2000-03-09 19:18:41 +01:00
|
|
|
SetLastError( ERROR_FILE_NOT_FOUND );
|
1999-04-18 14:09:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the 'dllname_elfdll_image' variable */
|
|
|
|
strcpy(soname, name);
|
|
|
|
strcat(soname, "_elfdll_image");
|
|
|
|
image = (struct elfdll_image *)dlsym(dlhandle, soname);
|
|
|
|
if(!image)
|
|
|
|
{
|
1999-12-08 04:56:23 +01:00
|
|
|
ERR("Could not get elfdll image descriptor %s (%s)\n", soname, dlerror());
|
1999-04-18 14:09:21 +02:00
|
|
|
dlclose(dlhandle);
|
2000-03-09 19:18:41 +01:00
|
|
|
SetLastError( ERROR_BAD_FORMAT );
|
1999-04-18 14:09:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-07 06:12:41 +02:00
|
|
|
wm = PE_CreateModule( image->pe_module_start, path, 0, -1, FALSE );
|
1999-04-18 14:09:21 +02:00
|
|
|
if(!wm)
|
|
|
|
{
|
1999-12-08 04:56:23 +01:00
|
|
|
ERR("Could not create WINE_MODREF for %s\n", path);
|
1999-04-18 14:09:21 +02:00
|
|
|
dlclose(dlhandle);
|
2000-03-09 19:18:41 +01:00
|
|
|
SetLastError( ERROR_OUTOFMEMORY );
|
1999-04-18 14:09:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-08-07 06:12:41 +02:00
|
|
|
wm->dlhandle = dlhandle;
|
1999-04-18 14:09:21 +02:00
|
|
|
|
1999-05-22 13:25:24 +02:00
|
|
|
dump_exports(image->pe_module_start);
|
1999-04-18 14:09:21 +02:00
|
|
|
return wm;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No elfdlls possible
|
|
|
|
* Just put stubs in here.
|
|
|
|
*/
|
|
|
|
|
2000-03-09 19:18:41 +01:00
|
|
|
WINE_MODREF *ELFDLL_LoadLibraryExA(LPCSTR libname, DWORD flags)
|
1999-04-18 14:09:21 +02:00
|
|
|
{
|
2000-03-09 19:18:41 +01:00
|
|
|
SetLastError( ERROR_FILE_NOT_FOUND );
|
1999-04-18 14:09:21 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|