Commit 8220bc9d authored by Marcus Meissner's avatar Marcus Meissner Committed by Alexandre Julliard

Generic dynamic dll loader using dl*() API.

Includes: stdcall->cdecl mapping ability, snooping. (Tested only with glide2x.dll -> libglide2x.so)
parent 8deb379f
......@@ -81,6 +81,8 @@ dnl Check for -lw for Solaris
AC_CHECK_LIB(w,iswalnum)
dnl Check for -lxpg4 for FreeBSD
AC_CHECK_LIB(xpg4,setrunelocale)
dnl Check for -ldl
AC_CHECK_LIB(dl,dlopen)
dnl Check for XFree86 DGA extension
AC_CHECK_LIB(Xxf86dga,XF86DGAQueryExtension,AC_DEFINE(HAVE_LIBXXF86DGA) X_PRE_LIBS="$X_PRE_LIBS -lXxf86dga",,$X_LIBS -lXext -lX11)
......@@ -260,7 +262,7 @@ fi
dnl **** Check for functions and header files ****
AC_CHECK_FUNCS(clone getpagesize memmove sendmsg sigaltstack strerror stricmp tcgetattr timegm usleep wait4 waitpid vfscanf)
AC_CHECK_HEADERS(wctype.h sys/syscall.h syscall.h sys/param.h sys/vfs.h sys/mount.h sys/statfs.h float.h linux/cdrom.h linux/ucdrom.h sys/cdio.h sys/filio.h sys/modem.h strings.h sys/strtio.h)
AC_CHECK_HEADERS(wctype.h sys/syscall.h syscall.h sys/param.h sys/vfs.h sys/mount.h sys/statfs.h float.h linux/cdrom.h linux/ucdrom.h sys/cdio.h sys/filio.h sys/modem.h strings.h sys/strtio.h dlfcn.h)
AC_HEADER_STAT()
AC_C_CONST()
AC_TYPE_SIZE_T()
......
......@@ -105,6 +105,9 @@
/* Define if you have the waitpid function. */
#undef HAVE_WAITPID
/* Define if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H
/* Define if you have the <float.h> header file. */
#undef HAVE_FLOAT_H
......@@ -156,6 +159,9 @@
/* Define if you have the <wctype.h> header file. */
#undef HAVE_WCTYPE_H
/* Define if you have the dl library (-ldl). */
#undef HAVE_LIBDL
/* Define if you have the i386 library (-li386). */
#undef HAVE_LIBI386
......
......@@ -114,7 +114,7 @@ typedef struct _wine_modref
MODULE32_TYPE type;
union {
PE_MODREF pe;
/* ELF_MODREF elf; */
ELF_MODREF elf;
} binfmt;
HMODULE32 module;
......@@ -145,6 +145,7 @@ extern HMODULE32 MODULE_CreateDummyModule( const OFSTRUCT *ofs );
extern FARPROC16 MODULE_GetWndProcEntry16( const char *name );
extern FARPROC16 WINAPI WIN32_GetProcAddress16( HMODULE32 hmodule, LPCSTR name );
extern SEGPTR WINAPI HasGPHandler( SEGPTR address );
HMODULE32 MODULE_LoadLibraryEx32A(LPCSTR libname,struct _PDB32*process,HFILE32 hfile,DWORD flags);
/* loader/ne/module.c */
extern NE_MODULE *NE_GetPtr( HMODULE16 hModule );
......
......@@ -52,4 +52,27 @@ extern LPIMAGE_RESOURCE_DIRECTORY GetResDirEntryW(LPIMAGE_RESOURCE_DIRECTORY,LPC
typedef DWORD (CALLBACK*DLLENTRYPROC32)(HMODULE32,DWORD,LPVOID);
typedef struct {
WORD popl WINE_PACKED; /* 0x8f 0x05 */
DWORD addr_popped WINE_PACKED;/* ... */
BYTE pushl1 WINE_PACKED; /* 0x68 */
DWORD newret WINE_PACKED; /* ... */
BYTE pushl2 WINE_PACKED; /* 0x68 */
DWORD origfun WINE_PACKED; /* original function */
BYTE ret1 WINE_PACKED; /* 0xc3 */
WORD addesp WINE_PACKED; /* 0x83 0xc4 */
BYTE nrofargs WINE_PACKED; /* nr of arguments to add esp, */
BYTE pushl3 WINE_PACKED; /* 0x68 */
DWORD oldret WINE_PACKED; /* Filled out from popl above */
BYTE ret2 WINE_PACKED; /* 0xc3 */
} ELF_STDCALL_STUB;
typedef struct {
void* dlhandle;
ELF_STDCALL_STUB *stubs;
} ELF_MODREF;
extern HMODULE32 ELF_LoadLibraryEx32A(LPCSTR,struct _PDB32*,HFILE32,DWORD);
extern FARPROC32 ELF_FindExportedFunction(struct _PDB32 *process,struct _wine_modref *wm, LPCSTR funcName);
#endif /* __WINE_PE_IMAGE_H */
......@@ -130,6 +130,7 @@ typedef struct _IMAGE_OPTIONAL_HEADER
#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10
#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11
#define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */
#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13
/* Subsystem Values */
......
......@@ -6,6 +6,7 @@ VPATH = @srcdir@
MODULE = loader
C_SRCS = \
elf.c \
libres.c \
main.c \
module.c \
......
/*
* UNIX dynamic loader
*
* Currently only supports stuff using the dl* API.
*
* Copyright 1998 Marcus Meissner
*
* FIXME: Small reentrancy problem.
* IDEA(s): could be used to split up shell32,comctl32...
*/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "windows.h"
#include "snoop.h"
#include "process.h"
#include "peexe.h"
#include "heap.h"
#include "pe_image.h"
#include "module.h"
#include "debug.h"
#if defined(HAVE_LIBDL) && defined(HAVE_DLFCN_H)
#define UNIX_DLL_ENDING "so"
#define STUBSIZE 4095
#include <dlfcn.h>
HMODULE32
ELF_LoadLibraryEx32A(LPCSTR libname,PDB32 *process,HANDLE32 hf,DWORD flags) {
WINE_MODREF *wm;
char *modname,*s,*t,*x;
LPVOID *dlhandle;
LPIMAGE_DOS_HEADER dh;
LPIMAGE_NT_HEADERS nth;
LPIMAGE_SECTION_HEADER sh;
HMODULE32 hmod;
t = HeapAlloc(process->heap,HEAP_ZERO_MEMORY,strlen(libname)+strlen("lib.so")+1);
*t = '\0';
/* copy path to tempvar ... */
s=strrchr(libname,'/');
if (!s)
s=strrchr(libname,'\\');
if (s) {
strncpy(t,libname,s-libname+1);
t[libname-s+1]= '\0';
} else
s = (LPSTR)libname;
modname = s;
/* append "lib" foo ".so" */
strcat(t,"lib");
x = t+strlen(t);
strcat(t,s);
s = strchr(x,'.');
while (s) {
if (!strcasecmp(s,".dll")) {
strcpy(s+1,UNIX_DLL_ENDING);
break;
}
s=strchr(s+1,'.');
}
/* FIXME: make UNIX filename from DOS fn? */
/* ... and open it */
dlhandle = dlopen(t,RTLD_NOW);
if (!dlhandle) {
HeapFree(process->heap,0,t);
return 0;
}
wm=(WINE_MODREF*)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,sizeof(*wm));
wm->type = MODULE32_ELF;
wm->binfmt.elf.dlhandle = dlhandle;
/* FIXME: hmm, order? */
wm->next = process->modref_list;
process->modref_list = wm;
wm->modname = HEAP_strdupA(process->heap,0,modname);
wm->longname = HEAP_strdupA(process->heap,0,t);
hmod = (HMODULE32)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,sizeof(IMAGE_DOS_HEADER)+sizeof(IMAGE_NT_HEADERS)+sizeof(IMAGE_SECTION_HEADER)+100);
dh = (LPIMAGE_DOS_HEADER)hmod;
dh->e_magic = IMAGE_DOS_SIGNATURE;
dh->e_lfanew = sizeof(IMAGE_DOS_HEADER);
nth = PE_HEADER(hmod);
nth->Signature = IMAGE_NT_SIGNATURE;
nth->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
nth->FileHeader.NumberOfSections = 1;
nth->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER);
nth->FileHeader.Characteristics =
IMAGE_FILE_RELOCS_STRIPPED|IMAGE_FILE_LINE_NUMS_STRIPPED|
IMAGE_FILE_LOCAL_SYMS_STRIPPED|IMAGE_FILE_32BIT_MACHINE|
IMAGE_FILE_DLL|IMAGE_FILE_DEBUG_STRIPPED;
nth->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
nth->OptionalHeader.SizeOfCode = 0;
nth->OptionalHeader.SizeOfInitializedData = 0;
nth->OptionalHeader.SizeOfUninitializedData = 0;
nth->OptionalHeader.AddressOfEntryPoint = 0;
nth->OptionalHeader.BaseOfCode = 0;
nth->OptionalHeader.MajorOperatingSystemVersion = 4;
nth->OptionalHeader.MajorImageVersion = 4;
nth->OptionalHeader.SizeOfImage = 0;
nth->OptionalHeader.SizeOfHeaders = 0;
nth->OptionalHeader.Subsystem = IMAGE_SUBSYSTEM_NATIVE;
nth->OptionalHeader.DllCharacteristics = 0;
nth->OptionalHeader.NumberOfRvaAndSizes = 0;
/* allocate one code section that crosses the whole process range
* (we could find out from internal tables ... hmm )
*/
sh=(LPIMAGE_SECTION_HEADER)(nth+1);
strcpy(sh->Name,".text");
sh->Misc.VirtualSize = 0x7fffffff;
sh->VirtualAddress = 0x42; /* so snoop can use it ... */
sh->SizeOfRawData = 0x7fffffff;
sh->PointerToRawData = 0;
sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
wm->module = hmod;
SNOOP_RegisterDLL(hmod,libname,STUBSIZE/sizeof(ELF_STDCALL_STUB));
return hmod;
}
FARPROC32
ELF_FindExportedFunction( PDB32 *process,WINE_MODREF *wm, LPCSTR funcName) {
LPVOID fun;
int i,nrofargs = 0;
ELF_STDCALL_STUB *stub;
assert(wm->type == MODULE32_ELF);
if (!HIWORD(funcName)) {
ERR(win32,"Can't import from UNIX dynamic libs by ordinal, sorry.\n");
return (FARPROC32)0;
}
fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
/* we sometimes have an excess '_' at the beginning of the name */
if (!fun && (funcName[0]=='_')) {
funcName++ ;
fun = dlsym(wm->binfmt.elf.dlhandle,funcName);
}
if (!fun) {
/* Function@nrofargs usually marks a stdcall function
* with nrofargs bytes that are popped at the end
*/
if (strchr(funcName,'@')) {
LPSTR t,fn = HEAP_strdupA(process->heap,0,funcName);
t = strchr(fn,'@');
*t = '\0';
nrofargs = 0;
sscanf(t+1,"%d",&nrofargs);
fun = dlsym(wm->binfmt.elf.dlhandle,fn);
HeapFree(process->heap,0,fn);
}
}
/* We sometimes have Win32 dlls implemented using stdcall but UNIX
* dlls using cdecl. If we find out the number of args the function
* uses, we remove them from the stack using two small stubs.
*/
if (!wm->binfmt.elf.stubs) {
/* one page should suffice */
wm->binfmt.elf.stubs = VirtualAlloc(NULL,STUBSIZE,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE);
memset(wm->binfmt.elf.stubs,0,STUBSIZE);
}
stub = wm->binfmt.elf.stubs;
for (i=0;i<STUBSIZE/sizeof(ELF_STDCALL_STUB);i++) {
if (!stub->origfun)
break;
if (stub->origfun == (DWORD)fun)
break;
stub++;
}
if (i==STUBSIZE/sizeof(ELF_STDCALL_STUB)) {
ERR(win32,"please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
assert(i<STUBSIZE/sizeof(ELF_STDCALL_STUB));
}
if (!stub->origfun)
stub->origfun=(DWORD)fun; /* just a marker */
if (fun && nrofargs) { /* we don't need it for 0 args */
/* Selfmodifying entry/return stub for stdcall -> cdecl
* conversion.
* - Pop returnaddress directly into our return code
* popl <into code below>
* - Replace it by pointer to start of our returncode
* push $newret
* - And call the original function
* jmp $orgfun
* - Remove the arguments no longer needed
* newret: add esp, <nrofargs>
* - Push the original returnvalue on the stack
* pushl <poppedvalue>
* - And return to it.
* ret
*/
/* FIXME: The function stub is not reentrant. */
((LPBYTE)&(stub->popl))[0] = 0x8f;
((LPBYTE)&(stub->popl))[1] = 0x05;
stub->addr_popped = (DWORD)&(stub->oldret);
stub->pushl1 = 0x68;
stub->newret = (DWORD)&(stub->addesp);
stub->pushl2 = 0x68;
stub->origfun = (DWORD)fun;
stub->ret1 = 0xc3;
((LPBYTE)&(stub->addesp))[0]=0x83;
((LPBYTE)&(stub->addesp))[1]=0xc4;
stub->nrofargs = nrofargs;
stub->pushl3 = 0x68;
/* filled out by entrycode */
stub->oldret = 0xdeadbeef;
stub->ret2 = 0xc3;
fun=(FARPROC32)stub;
}
if (!fun) {
FIXME(win32,"function %s not found: %s\n",funcName,dlerror());
return fun;
}
fun = SNOOP_GetProcAddress32(wm->module,funcName,stub-wm->binfmt.elf.stubs,fun);
return (FARPROC32)fun;
}
#else
HMODULE32
ELF_LoadLibraryEx32A(LPCSTR libname,PDB32 *process,HANDLE32 hf,DWORD flags) {
return 0;
}
FARPROC32
ELF_FindExportedFunction( PDB32 *process,WINE_MODREF *wm, LPCSTR funcName) {
return (FARPROC32)0;
}
#endif
......@@ -687,19 +687,28 @@ HMODULE32 WINAPI LoadLibraryEx32W16( LPCSTR libname, HANDLE16 hf,
*/
HMODULE32 WINAPI LoadLibraryEx32A(LPCSTR libname,HFILE32 hfile,DWORD flags)
{
return MODULE_LoadLibraryEx32A(libname,PROCESS_Current(),hfile,flags);
}
HMODULE32 MODULE_LoadLibraryEx32A(LPCSTR libname,PDB32*process,HFILE32 hfile,DWORD flags)
{
HMODULE32 hmod;
hmod = PE_LoadLibraryEx32A(libname,PROCESS_Current(),hfile,flags);
hmod = ELF_LoadLibraryEx32A(libname,process,hfile,flags);
if (hmod)
return hmod; /* already initialized for ELF */
hmod = PE_LoadLibraryEx32A(libname,process,hfile,flags);
if (hmod < 32) {
char buffer[256];
strcpy( buffer, libname );
strcat( buffer, ".dll" );
hmod = PE_LoadLibraryEx32A(buffer,PROCESS_Current(),hfile,flags);
hmod = PE_LoadLibraryEx32A(buffer,process,hfile,flags);
}
/* initialize all DLLs, which haven't been initialized yet. */
if (hmod >= 32)
PE_InitializeDLLs( PROCESS_Current(), DLL_PROCESS_ATTACH, NULL);
PE_InitializeDLLs( process, DLL_PROCESS_ATTACH, NULL);
return hmod;
}
......@@ -1018,6 +1027,8 @@ FARPROC32 MODULE_GetProcAddress32(
{
case MODULE32_PE:
return PE_FindExportedFunction( process, wm, function);
case MODULE32_ELF:
return ELF_FindExportedFunction( process, wm, function);
default:
ERR(module,"wine_modref type %d not handled.\n",wm->type);
return (FARPROC32)0;
......
......@@ -253,7 +253,7 @@ DWORD fixup_imports (PDB32 *process,WINE_MODREF *wm)
char *name = (char *) RVA(pe_imp->Name);
/* don't use MODULE_Load, Win32 creates new task differently */
hImpModule = PE_LoadLibraryEx32A( name, process, 0, 0 );
hImpModule = MODULE_LoadLibraryEx32A( name, process, 0, 0 );
if (!hImpModule) {
char *p,buffer[2000];
......@@ -262,7 +262,7 @@ DWORD fixup_imports (PDB32 *process,WINE_MODREF *wm)
if (!(p = strrchr (buffer, '\\')))
p = buffer;
strcpy (p + 1, name);
hImpModule = PE_LoadLibraryEx32A( buffer, process, 0, 0 );
hImpModule = MODULE_LoadLibraryEx32A( buffer, process, 0, 0 );
}
if (!hImpModule) {
ERR (module, "Module %s not found\n", name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment