1998-10-11 13:10:27 +02:00
|
|
|
/*
|
|
|
|
* 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...
|
|
|
|
*/
|
|
|
|
|
2001-11-06 21:57:11 +01:00
|
|
|
#include "config.h"
|
2001-10-14 18:25:47 +02:00
|
|
|
#include "wine/port.h"
|
1998-10-11 13:10:27 +02:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "snoop.h"
|
2000-12-03 04:14:29 +01:00
|
|
|
#include "file.h"
|
1998-10-11 13:10:27 +02:00
|
|
|
#include "module.h"
|
1999-06-12 17:45:58 +02:00
|
|
|
#include "debugtools.h"
|
1999-04-18 14:14:06 +02:00
|
|
|
#include "winerror.h"
|
1998-10-11 13:10:27 +02:00
|
|
|
|
2000-08-07 06:12:41 +02:00
|
|
|
DEFAULT_DEBUG_CHANNEL(win32);
|
|
|
|
|
|
|
|
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;
|
2000-02-03 02:33:48 +01:00
|
|
|
|
|
|
|
#define UNIX_DLL_ENDING "so"
|
|
|
|
|
|
|
|
#define STUBSIZE 4095
|
2000-08-07 06:12:41 +02:00
|
|
|
#define STUBOFFSET (sizeof(IMAGE_DOS_HEADER) + \
|
|
|
|
sizeof(IMAGE_NT_HEADERS) + \
|
|
|
|
sizeof(IMAGE_SECTION_HEADER))
|
2000-02-03 02:33:48 +01:00
|
|
|
|
2000-08-07 06:12:41 +02:00
|
|
|
static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, BOOL snoop );
|
2000-02-03 02:33:48 +01:00
|
|
|
|
2000-08-07 06:12:41 +02:00
|
|
|
static HMODULE ELF_CreateDummyModule( LPCSTR libname, LPCSTR modname )
|
1998-11-01 19:01:53 +01:00
|
|
|
{
|
1998-10-16 11:30:33 +02:00
|
|
|
PIMAGE_DOS_HEADER dh;
|
|
|
|
PIMAGE_NT_HEADERS nth;
|
|
|
|
PIMAGE_SECTION_HEADER sh;
|
1999-02-26 12:11:13 +01:00
|
|
|
HMODULE hmod;
|
1998-10-11 13:10:27 +02:00
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
hmod = (HMODULE)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
|
1999-02-24 15:27:07 +01:00
|
|
|
sizeof(IMAGE_DOS_HEADER) +
|
|
|
|
sizeof(IMAGE_NT_HEADERS) +
|
2000-08-07 06:12:41 +02:00
|
|
|
sizeof(IMAGE_SECTION_HEADER) + STUBSIZE );
|
1998-10-16 11:30:33 +02:00
|
|
|
dh = (PIMAGE_DOS_HEADER)hmod;
|
1998-10-11 13:10:27 +02:00
|
|
|
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 )
|
|
|
|
*/
|
1998-10-16 11:30:33 +02:00
|
|
|
sh=(PIMAGE_SECTION_HEADER)(nth+1);
|
1998-10-11 13:10:27 +02:00
|
|
|
strcpy(sh->Name,".text");
|
2000-08-07 06:12:41 +02:00
|
|
|
sh->Misc.VirtualSize = STUBSIZE;
|
|
|
|
sh->VirtualAddress = STUBOFFSET; /* so snoop can use it ... */
|
|
|
|
sh->SizeOfRawData = STUBSIZE;
|
1998-10-11 13:10:27 +02:00
|
|
|
sh->PointerToRawData = 0;
|
|
|
|
sh->Characteristics = IMAGE_SCN_CNT_CODE|IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ;
|
2000-08-07 06:12:41 +02:00
|
|
|
return hmod;
|
1998-11-01 19:01:53 +01:00
|
|
|
}
|
|
|
|
|
2000-03-09 19:18:41 +01:00
|
|
|
WINE_MODREF *ELF_LoadLibraryExA( LPCSTR libname, DWORD flags)
|
1999-02-24 15:27:07 +01:00
|
|
|
{
|
1998-11-01 19:01:53 +01:00
|
|
|
WINE_MODREF *wm;
|
2000-08-07 06:12:41 +02:00
|
|
|
HMODULE hmod;
|
1998-11-01 19:01:53 +01:00
|
|
|
char *modname,*s,*t,*x;
|
|
|
|
LPVOID *dlhandle;
|
2000-12-13 22:32:55 +01:00
|
|
|
char error[256];
|
1998-11-01 19:01:53 +01:00
|
|
|
|
1999-02-24 15:27:07 +01:00
|
|
|
t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
|
|
|
|
strlen(libname) + strlen("lib.so") + 1 );
|
1998-11-01 19:01:53 +01:00
|
|
|
*t = '\0';
|
|
|
|
/* copy path to tempvar ... */
|
|
|
|
s=strrchr(libname,'/');
|
|
|
|
if (!s)
|
|
|
|
s=strrchr(libname,'\\');
|
|
|
|
if (s) {
|
1999-07-03 18:01:42 +02:00
|
|
|
s++; /* skip / or \ */
|
|
|
|
/* copy everything up to s-1 */
|
|
|
|
memcpy(t,libname,s-libname);
|
|
|
|
t[s-libname]= '\0';
|
1998-11-01 19:01:53 +01:00
|
|
|
} else
|
|
|
|
s = (LPSTR)libname;
|
|
|
|
modname = s;
|
|
|
|
/* append "lib" foo ".so" */
|
|
|
|
strcat(t,"lib");
|
|
|
|
x = t+strlen(t);
|
|
|
|
strcat(t,s);
|
|
|
|
s = strchr(x,'.');
|
1999-04-01 12:09:46 +02:00
|
|
|
if (s) {
|
|
|
|
while (s) {
|
2000-12-03 04:14:29 +01:00
|
|
|
if (!FILE_strcasecmp(s,".dll")) {
|
1999-04-01 12:09:46 +02:00
|
|
|
strcpy(s+1,UNIX_DLL_ENDING);
|
|
|
|
break;
|
1998-11-01 19:01:53 +01:00
|
|
|
}
|
|
|
|
s=strchr(s+1,'.');
|
1999-04-01 12:09:46 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strcat(x,"."UNIX_DLL_ENDING);
|
1998-11-01 19:01:53 +01:00
|
|
|
}
|
|
|
|
|
2000-11-06 06:26:27 +01:00
|
|
|
/* grab just the last piece of the path/filename
|
|
|
|
which should be the name of the library we are
|
|
|
|
looking to load. increment by 1 to skip the DOS slash */
|
|
|
|
s = strrchr(t,'\\');
|
|
|
|
s++;
|
|
|
|
|
|
|
|
/* ... and open the library pointed by s, while t points
|
|
|
|
points to the ENTIRE DOS filename of the library
|
|
|
|
t is returned by HeapAlloc() above and so is also used
|
|
|
|
with HeapFree() below */
|
2000-12-13 22:32:55 +01:00
|
|
|
dlhandle = wine_dlopen(s,RTLD_NOW,error,sizeof(error));
|
1998-11-01 19:01:53 +01:00
|
|
|
if (!dlhandle) {
|
2000-12-13 22:32:55 +01:00
|
|
|
WARN("failed to load %s: %s\n", s, error);
|
1999-02-24 15:27:07 +01:00
|
|
|
HeapFree( GetProcessHeap(), 0, t );
|
2000-03-09 19:18:41 +01:00
|
|
|
SetLastError( ERROR_FILE_NOT_FOUND );
|
1999-04-18 14:14:06 +02:00
|
|
|
return NULL;
|
1998-11-01 19:01:53 +01:00
|
|
|
}
|
|
|
|
|
2000-08-07 06:12:41 +02:00
|
|
|
hmod = ELF_CreateDummyModule( t, modname );
|
1998-11-01 19:01:53 +01:00
|
|
|
|
2001-05-24 20:43:16 +02:00
|
|
|
SNOOP_RegisterDLL(hmod,libname,0,STUBSIZE/sizeof(ELF_STDCALL_STUB));
|
2000-08-07 06:12:41 +02:00
|
|
|
|
2001-01-05 05:08:07 +01:00
|
|
|
wm = PE_CreateModule( hmod, libname, 0, 0, FALSE );
|
2000-08-07 06:12:41 +02:00
|
|
|
wm->find_export = ELF_FindExportedFunction;
|
|
|
|
wm->dlhandle = dlhandle;
|
1999-04-18 14:14:06 +02:00
|
|
|
return wm;
|
1998-10-11 13:10:27 +02:00
|
|
|
}
|
|
|
|
|
2000-08-07 06:12:41 +02:00
|
|
|
static FARPROC ELF_FindExportedFunction( WINE_MODREF *wm, LPCSTR funcName, BOOL snoop )
|
1999-02-24 15:27:07 +01:00
|
|
|
{
|
1998-10-11 13:10:27 +02:00
|
|
|
LPVOID fun;
|
|
|
|
int i,nrofargs = 0;
|
2000-08-07 06:12:41 +02:00
|
|
|
ELF_STDCALL_STUB *stub, *first_stub;
|
2000-12-13 22:32:55 +01:00
|
|
|
char error[256];
|
1998-10-11 13:10:27 +02:00
|
|
|
|
|
|
|
if (!HIWORD(funcName)) {
|
1999-06-12 17:45:58 +02:00
|
|
|
ERR("Can't import from UNIX dynamic libs by ordinal, sorry.\n");
|
1999-02-26 12:11:13 +01:00
|
|
|
return (FARPROC)0;
|
1998-10-11 13:10:27 +02:00
|
|
|
}
|
2000-12-13 22:32:55 +01:00
|
|
|
fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
|
2000-12-05 22:17:59 +01:00
|
|
|
if (!fun)
|
|
|
|
{
|
|
|
|
/* we sometimes have an excess '_' at the beginning of the name */
|
|
|
|
if (funcName[0]=='_')
|
|
|
|
{
|
1998-10-11 13:10:27 +02:00
|
|
|
funcName++ ;
|
2000-12-13 22:32:55 +01:00
|
|
|
fun = wine_dlsym(wm->dlhandle,funcName,error,sizeof(error));
|
2000-12-05 22:17:59 +01:00
|
|
|
}
|
|
|
|
}
|
1998-10-11 13:10:27 +02:00
|
|
|
if (!fun) {
|
|
|
|
/* Function@nrofargs usually marks a stdcall function
|
|
|
|
* with nrofargs bytes that are popped at the end
|
|
|
|
*/
|
2001-07-24 23:45:22 +02:00
|
|
|
LPCSTR t;
|
|
|
|
if ((t = strchr(funcName,'@')))
|
|
|
|
{
|
|
|
|
LPSTR fn = HeapAlloc( GetProcessHeap(), 0, t - funcName + 1 );
|
|
|
|
memcpy( fn, funcName, t - funcName );
|
|
|
|
fn[t - funcName] = 0;
|
|
|
|
nrofargs = 0;
|
|
|
|
sscanf(t+1,"%d",&nrofargs);
|
|
|
|
fun = wine_dlsym(wm->dlhandle,fn,error,sizeof(error));
|
|
|
|
HeapFree( GetProcessHeap(), 0, fn );
|
|
|
|
}
|
1998-10-11 13:10:27 +02:00
|
|
|
}
|
|
|
|
/* 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.
|
|
|
|
*/
|
2000-08-07 06:12:41 +02:00
|
|
|
stub = first_stub = (ELF_STDCALL_STUB *)((char *)wm->module + STUBOFFSET);
|
1998-10-11 13:10:27 +02:00
|
|
|
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)) {
|
1999-06-12 17:45:58 +02:00
|
|
|
ERR("please report, that there are not enough slots for stdcall stubs in the ELF loader.\n");
|
1998-10-11 13:10:27 +02:00
|
|
|
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;
|
1999-02-26 12:11:13 +01:00
|
|
|
fun=(FARPROC)stub;
|
1998-10-11 13:10:27 +02:00
|
|
|
}
|
|
|
|
if (!fun) {
|
2000-12-13 22:32:55 +01:00
|
|
|
FIXME("function %s not found: %s\n",funcName,error);
|
1998-10-11 13:10:27 +02:00
|
|
|
return fun;
|
|
|
|
}
|
2000-08-07 06:12:41 +02:00
|
|
|
fun = SNOOP_GetProcAddress(wm->module,funcName,stub-first_stub,fun);
|
1999-02-26 12:11:13 +01:00
|
|
|
return (FARPROC)fun;
|
1998-10-11 13:10:27 +02:00
|
|
|
}
|