Commit ffa2c6f9 authored by Ulrich Weigand's avatar Ulrich Weigand Committed by Alexandre Julliard

Reorganized PE module loading to prepare for elf-dll loader.

WINE_MODREF creation moved to one central location. Bugfix: correctly select native/built-in version if .DLL extension is missing in LoadLibrary call.
parent 4d6248ba
......@@ -196,8 +196,7 @@ extern BOOL32 BUILTIN_ParseDLLOptions( const char *str );
extern void BUILTIN_PrintDLLs(void);
/* relay32/builtin.c */
extern HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force,
struct _PDB32 *process );
extern HMODULE32 BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs, BOOL32 force );
/* if1632/builtin.c */
extern HMODULE16 (*fnBUILTIN_LoadModule)(LPCSTR name, BOOL32 force);
......
......@@ -13,7 +13,6 @@ typedef struct {
PIMAGE_IMPORT_DESCRIPTOR pe_import;
PIMAGE_EXPORT_DIRECTORY pe_export;
PIMAGE_RESOURCE_DIRECTORY pe_resource;
PIMAGE_BASE_RELOCATION pe_reloc;
int flags;
#define PE_MODREF_PROCESS_ATTACHED 0x00000001
#define PE_MODREF_NO_DLL_CALLS 0x00000002
......
......@@ -833,17 +833,9 @@ HMODULE32 MODULE_LoadLibraryEx32A(LPCSTR libname,PDB32*process,HFILE32 hfile,DWO
HMODULE32 hmod;
hmod = ELF_LoadLibraryEx32A(libname,process,hfile,flags);
if (hmod)
return hmod; /* already initialized for ELF */
if (hmod) return hmod;
hmod = PE_LoadLibraryEx32A(libname,process,hfile,flags);
if (hmod < 32) {
char buffer[256];
strcpy( buffer, libname );
strcat( buffer, ".dll" );
hmod = PE_LoadLibraryEx32A(buffer,process,hfile,flags);
}
return hmod;
}
......
......@@ -386,18 +386,12 @@ static int calc_vma_size( HMODULE32 hModule )
return vma_size;
}
static void do_relocations(WINE_MODREF *wm)
static void do_relocations( unsigned int load_addr, IMAGE_BASE_RELOCATION *r )
{
PE_MODREF *pem = &(wm->binfmt.pe);
int delta = wm->module - PE_HEADER(wm->module)->OptionalHeader.ImageBase;
unsigned int load_addr= wm->module;
IMAGE_BASE_RELOCATION *r = pem->pe_reloc;
int delta = load_addr - PE_HEADER(load_addr)->OptionalHeader.ImageBase;
int hdelta = (delta >> 16) & 0xFFFF;
int ldelta = delta & 0xFFFF;
/* int reloc_size = */
if(delta == 0)
/* Nothing to do */
return;
......@@ -462,44 +456,67 @@ static void do_relocations(WINE_MODREF *wm)
* BUT we have to map the whole image anyway, for Win32 programs sometimes
* want to access them. (HMODULE32 point to the start of it)
*/
static HMODULE32 PE_LoadImage( HFILE32 hFile )
static HMODULE32 PE_LoadImage( LPCSTR name, OFSTRUCT *ofs )
{
HMODULE32 hModule;
HFILE32 hFile;
HANDLE32 mapping;
int i,rawsize = 0;
IMAGE_NT_HEADERS *nt;
IMAGE_SECTION_HEADER *pe_sec;
BY_HANDLE_FILE_INFORMATION bhfi;
int i, rawsize, lowest_va, lowest_fa, vma_size, file_size = 0;
DWORD load_addr, aoep, reloc = 0;
char dllname[256], *p;
/* Append .DLL to name if no extension present */
strcpy( dllname, name );
if ((p = strrchr( dllname, '\\' ))) p++; else p = dllname;
if (!strchr( p, '.' )) strcat( dllname, ".DLL" );
/* Open PE file */
hFile = OpenFile32( dllname, ofs, OF_READ );
if ( hFile == HFILE_ERROR32 )
{
WARN( win32, "OpenFile error %ld\n", GetLastError() );
return 2;
}
/* Retrieve file size */
if ( GetFileInformationByHandle( hFile, &bhfi ) )
file_size = bhfi.nFileSizeLow; /* FIXME: 64 bit */
/* map the PE file somewhere */
/* Map the PE file somewhere */
mapping = CreateFileMapping32A( hFile, NULL, PAGE_READONLY | SEC_COMMIT,
0, 0, NULL );
CloseHandle( hFile );
if (!mapping)
{
WARN( win32, "CreateFileMapping error %ld\n",
GetLastError() );
WARN( win32, "CreateFileMapping error %ld\n", GetLastError() );
return 0;
}
hModule = (HMODULE32)MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
CloseHandle( mapping );
if (!hModule)
{
WARN( win32, "PE_LoadImage: MapViewOfFile error %ld\n",
GetLastError() );
WARN( win32, "MapViewOfFile error %ld\n", GetLastError() );
return 0;
}
nt = PE_HEADER( hModule );
if (PE_HEADER(hModule)->Signature != IMAGE_NT_SIGNATURE)
/* Check signature */
if ( nt->Signature != IMAGE_NT_SIGNATURE )
{
WARN(win32,"image doesn't have PE signature, but 0x%08lx\n",
PE_HEADER(hModule)->Signature );
WARN(win32, "image doesn't have PE signature, but 0x%08lx\n",
nt->Signature );
goto error;
}
if (PE_HEADER(hModule)->FileHeader.Machine != IMAGE_FILE_MACHINE_I386)
/* Check architecture */
if ( nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386 )
{
MSG("Trying to load PE image for unsupported architecture (");
switch (PE_HEADER(hModule)->FileHeader.Machine)
switch (nt->FileHeader.Machine)
{
case IMAGE_FILE_MACHINE_UNKNOWN: MSG("Unknown"); break;
case IMAGE_FILE_MACHINE_I860: MSG("I860"); break;
......@@ -508,357 +525,324 @@ static HMODULE32 PE_LoadImage( HFILE32 hFile )
case IMAGE_FILE_MACHINE_R10000: MSG("R10000"); break;
case IMAGE_FILE_MACHINE_ALPHA: MSG("Alpha"); break;
case IMAGE_FILE_MACHINE_POWERPC: MSG("PowerPC"); break;
default: MSG("Unknown-%04x",
PE_HEADER(hModule)->FileHeader.Machine); break;
default: MSG("Unknown-%04x", nt->FileHeader.Machine); break;
}
MSG(")\n");
goto error;
}
/* find out how large this executeable should be */
pe_sec = PE_SECTIONS(hModule);
for (i=0;i<PE_HEADER(hModule)->FileHeader.NumberOfSections;i++) {
/* Find out how large this executeable should be */
pe_sec = PE_SECTIONS( hModule );
rawsize = 0; lowest_va = 0x10000; lowest_fa = 0x10000;
for (i = 0; i < nt->FileHeader.NumberOfSections; i++)
{
if (lowest_va > pe_sec[i].VirtualAddress)
lowest_va = pe_sec[i].VirtualAddress;
if (pe_sec[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
continue;
if (pe_sec[i].PointerToRawData < lowest_fa)
lowest_fa = pe_sec[i].PointerToRawData;
if (pe_sec[i].PointerToRawData+pe_sec[i].SizeOfRawData > rawsize)
rawsize = pe_sec[i].PointerToRawData+pe_sec[i].SizeOfRawData;
}
if (GetFileInformationByHandle(hFile,&bhfi)) {
/* FIXME: 64 bit */
if (bhfi.nFileSizeLow < rawsize) {
ERR(win32,"PE module is too small (header: %d, filesize: %d), probably truncated download?\n",rawsize,bhfi.nFileSizeLow);
/* Check file size */
if ( file_size && file_size < rawsize )
{
ERR( win32, "PE module is too small (header: %d, filesize: %d), "
"probably truncated download?\n",
rawsize, file_size );
goto error;
}
}
/* Else ... Hmm, we have opened it, so we should be able to get info?
* Anyway, don't care in this case
*/
return hModule;
error:
UnmapViewOfFile( (LPVOID)hModule );
return 0;
}
/* Check entrypoint address */
aoep = nt->OptionalHeader.AddressOfEntryPoint;
if (aoep && (aoep < lowest_va))
FIXME( win32, "WARNING: '%s' has an invalid entrypoint (0x%08lx) "
"below the first virtual address (0x%08x) "
"(possible Virus Infection or broken binary)!\n",
ofs->szPathName, aoep, lowest_va );
/**********************************************************************
* This maps a loaded PE dll into the address space of the specified process.
*/
static BOOL32 PE_MapImage( PDB32 *process,WINE_MODREF *wm, OFSTRUCT *ofs, DWORD flags )
{
PE_MODREF *pem;
int i, result;
DWORD load_addr,lowest_va,lowest_fa;
IMAGE_DATA_DIRECTORY dir;
char *modname;
int vma_size;
HMODULE32 hModule = wm->module;
DWORD aoep = PE_HEADER(hModule)->OptionalHeader.AddressOfEntryPoint;
IMAGE_SECTION_HEADER *pe_seg;
IMAGE_DOS_HEADER *dos_header = (IMAGE_DOS_HEADER *)hModule;
IMAGE_NT_HEADERS *nt_header = PE_HEADER(hModule);
pem = &(wm->binfmt.pe);
result = GetLongPathName32A(ofs->szPathName,NULL,0);
wm->longname = (char*)HeapAlloc(process->heap,0,result+1);
GetLongPathName32A(ofs->szPathName,wm->longname,result+1);
wm->shortname = HEAP_strdupA(process->heap,0,ofs->szPathName);
if (!(nt_header->FileHeader.Characteristics & IMAGE_FILE_DLL))
{
if (process->exe_modref)
FIXME(win32,"overwriting old exe_modref... arrgh\n");
process->exe_modref = wm;
}
load_addr = nt_header->OptionalHeader.ImageBase;
/* Allocate memory for module */
load_addr = nt->OptionalHeader.ImageBase;
vma_size = calc_vma_size( hModule );
TRACE(win32, "Load addr is %lx\n",load_addr);
load_addr = (DWORD)VirtualAlloc( (void*)load_addr, vma_size,
MEM_RESERVE | MEM_COMMIT,
PAGE_EXECUTE_READWRITE );
if (load_addr == 0) {
if (load_addr == 0)
{
/* We need to perform base relocations */
IMAGE_DATA_DIRECTORY *dir;
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_BASERELOC;
if (dir->Size)
reloc = dir->VirtualAddress;
else
{
FIXME( win32,
"Need to relocate %s, but no relocation records present (%s).\n",
ofs->szPathName,
(nt->FileHeader.Characteristics&IMAGE_FILE_RELOCS_STRIPPED)?
"stripped during link" : "unknown reason" );
goto error;
}
load_addr = (DWORD)VirtualAlloc( NULL, vma_size,
MEM_RESERVE | MEM_COMMIT,
PAGE_EXECUTE_READWRITE );
}
/* NOTE: this changes a value in the process modref chain, which can
* be accessed independently from this function
*/
wm->module = (HMODULE32)load_addr;
TRACE(win32, "Load addr is really %lx, range %x\n",
load_addr, vma_size);
TRACE(segment, "Loading %s at %lx, range %x\n",
TRACE( win32, "Load addr is %lx (base %lx), range %x\n",
load_addr, nt->OptionalHeader.ImageBase, vma_size );
TRACE( segment, "Loading %s at %lx, range %x\n",
ofs->szPathName, load_addr, vma_size );
/* Find out where this executeable should start */
pe_seg = PE_SECTIONS(hModule);lowest_va = 0x10000;lowest_fa = 0x10000;
for (i=0;i<PE_HEADER(hModule)->FileHeader.NumberOfSections;i++) {
if (lowest_va > pe_seg[i].VirtualAddress)
lowest_va = pe_seg[i].VirtualAddress;
if (!(pe_seg[i].Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
&& (pe_seg[i].PointerToRawData<lowest_fa)
)
lowest_fa = pe_seg[i].PointerToRawData;
}
if (aoep && (aoep<lowest_va))
FIXME(win32,"WARNING: '%s' has an invalid entrypoint (0x%08lx) below the first virtual address (0x%08lx) (possible Virus Infection or broken binary)!\n",ofs->szPathName,aoep,lowest_va);
/* Store the NT header at the load addr
* (FIXME: should really use mmap)
*/
*(IMAGE_DOS_HEADER *)load_addr = *dos_header;
*(IMAGE_NT_HEADERS *)(load_addr + dos_header->e_lfanew) = *nt_header;
memcpy(PE_SECTIONS(load_addr),PE_SECTIONS(hModule),sizeof(IMAGE_SECTION_HEADER)*nt_header->FileHeader.NumberOfSections);
/* Store the NT header at the load addr */
*(PIMAGE_DOS_HEADER)load_addr = *(PIMAGE_DOS_HEADER)hModule;
*PE_HEADER( load_addr ) = *nt;
memcpy( PE_SECTIONS(load_addr), PE_SECTIONS(hModule),
sizeof(IMAGE_SECTION_HEADER) * nt->FileHeader.NumberOfSections );
#if 0
/* Copies all stuff up to the first section. Including win32 viruses. */
memcpy(load_addr,hModule,lowest_fa);
memcpy( load_addr, hModule, lowest_fa );
#endif
pe_seg = PE_SECTIONS(hModule);
for (i = 0; i < nt_header->FileHeader.NumberOfSections; i++, pe_seg++)
/* Copy sections into module image */
pe_sec = PE_SECTIONS( hModule );
for (i = 0; i < nt->FileHeader.NumberOfSections; i++, pe_sec++)
{
/* memcpy only non-BSS segments */
/* FIXME: this should be done by mmap(..MAP_PRIVATE|MAP_FIXED..)
* but it is not possible for (at least) Linux needs
* a page-aligned offset.
*/
if(!(pe_seg->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA))
memcpy((char*)RVA(pe_seg->VirtualAddress),
(char*)(hModule + pe_seg->PointerToRawData),
pe_seg->SizeOfRawData
);
result = RVA (pe_seg->VirtualAddress);
#if 1
if(!(pe_sec->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA))
memcpy((char*)RVA(pe_sec->VirtualAddress),
(char*)(hModule + pe_sec->PointerToRawData),
pe_sec->SizeOfRawData);
#if 0
/* not needed, memory is zero */
if(strcmp(pe_seg->Name, ".bss") == 0)
memset((void *)result, 0,
pe_seg->Misc.VirtualSize ?
pe_seg->Misc.VirtualSize :
pe_seg->SizeOfRawData);
if(strcmp(pe_sec->Name, ".bss") == 0)
memset((void *)RVA(pe_sec->VirtualAddress), 0,
pe_sec->Misc.VirtualSize ?
pe_sec->Misc.VirtualSize :
pe_sec->SizeOfRawData);
#endif
}
if(strcmp(pe_seg->Name, ".idata") == 0)
pem->pe_import = (PIMAGE_IMPORT_DESCRIPTOR) result;
/* Perform base relocation, if necessary */
if ( reloc )
do_relocations( load_addr, (IMAGE_BASE_RELOCATION *)RVA(reloc) );
if(strcmp(pe_seg->Name, ".edata") == 0)
pem->pe_export = (PIMAGE_EXPORT_DIRECTORY) result;
/* We don't need the orignal mapping any more */
UnmapViewOfFile( (LPVOID)hModule );
return (HMODULE32)load_addr;
if(strcmp(pe_seg->Name, ".rsrc") == 0)
pem->pe_resource = (PIMAGE_RESOURCE_DIRECTORY) result;
error:
UnmapViewOfFile( (LPVOID)hModule );
return 0;
}
if(strcmp(pe_seg->Name, ".reloc") == 0)
pem->pe_reloc = (PIMAGE_BASE_RELOCATION) result;
}
/**********************************************************************
* PE_CreateModule
*
* Create WINE_MODREF structure for loaded HMODULE32, link it into
* process modref_list, and fixup all imports.
*
* Note: hModule must point to a correctly allocated PE image,
* with base relocations applied; the 16-bit dummy module
* associated to hModule must already exist.
*/
static WINE_MODREF *PE_CreateModule( PDB32 *process, HMODULE32 hModule,
OFSTRUCT *ofs, DWORD flags, BOOL32 builtin )
{
DWORD load_addr = (DWORD)hModule; /* for RVA */
IMAGE_NT_HEADERS *nt = PE_HEADER(hModule);
IMAGE_DATA_DIRECTORY *dir;
IMAGE_IMPORT_DESCRIPTOR *pe_import = NULL;
IMAGE_EXPORT_DIRECTORY *pe_export = NULL;
IMAGE_RESOURCE_DIRECTORY *pe_resource = NULL;
WINE_MODREF *wm;
int result;
char *modname;
/* There is word that the actual loader does not care about the
section names, and only goes for the DataDirectory */
dir=nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
if(dir.Size)
{
if(pem->pe_export && (int)pem->pe_export!=RVA(dir.VirtualAddress))
WARN(win32,"wrong export directory??\n");
/* always trust the directory */
pem->pe_export = (PIMAGE_EXPORT_DIRECTORY) RVA(dir.VirtualAddress);
}
dir=nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
if(dir.Size)
{
/*
if(pem->pe_import && (int)pem->pe_import!=RVA(dir.VirtualAddress))
WARN(win32,"wrong import directory??\n");
*/
pem->pe_import = (PIMAGE_IMPORT_DESCRIPTOR) RVA(dir.VirtualAddress);
}
/* Retrieve DataDirectory entries */
dir=nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE];
if(dir.Size)
{
if(pem->pe_resource && (int)pem->pe_resource!=RVA(dir.VirtualAddress))
WARN(win32,"wrong resource directory??\n");
pem->pe_resource = (PIMAGE_RESOURCE_DIRECTORY) RVA(dir.VirtualAddress);
}
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXPORT;
if (dir->Size)
pe_export = (PIMAGE_EXPORT_DIRECTORY)RVA(dir->VirtualAddress);
if(nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size)
FIXME(win32,"Exception directory ignored\n");
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IMPORT;
if (dir->Size)
pe_import = (PIMAGE_IMPORT_DESCRIPTOR)RVA(dir->VirtualAddress);
if(nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY].Size)
FIXME(win32,"Security directory ignored\n");
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_RESOURCE;
if (dir->Size)
pe_resource = (PIMAGE_RESOURCE_DIRECTORY)RVA(dir->VirtualAddress);
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_EXCEPTION;
if (dir->Size) FIXME( win32, "Exception directory ignored\n" );
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_SECURITY;
if (dir->Size) FIXME( win32, "Security directory ignored\n" );
dir=nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
if(dir.Size)
{
if(pem->pe_reloc && (int)pem->pe_reloc!= RVA(dir.VirtualAddress))
WARN(win32,"wrong relocation list??\n");
pem->pe_reloc = (void *) RVA(dir.VirtualAddress);
}
/* IMAGE_DIRECTORY_ENTRY_BASERELOC handled in PE_LoadImage */
/* IMAGE_DIRECTORY_ENTRY_DEBUG handled by debugger */
if(nt_header->OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COPYRIGHT].Size)
FIXME(win32,"Copyright string ignored\n");
if(nt_header->OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_GLOBALPTR].Size)
FIXME(win32,"Global Pointer (MIPS) ignored\n");
if(nt_header->OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size)
FIXME(win32,"Load Configuration directory ignored\n");
if(nt_header->OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT].Size)
TRACE(win32,"Bound Import directory ignored\n");
if(nt_header->OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IAT].Size)
TRACE(win32,"Import Address Table directory ignored\n");
if(nt_header->OptionalHeader.DataDirectory[13].Size)
FIXME(win32,"Unknown directory 13 ignored\n");
if(nt_header->OptionalHeader.DataDirectory[14].Size)
FIXME(win32,"Unknown directory 14 ignored\n");
if(nt_header->OptionalHeader.DataDirectory[15].Size)
FIXME(win32,"Unknown directory 15 ignored\n");
if(wm->module!=nt_header->OptionalHeader.ImageBase) {
if (pem->pe_reloc)
do_relocations(wm);
else {
/* FIXME: we could return before the second VirtualAlloc ... */
FIXME(win32,
"Had to relocate %s, but without relocation records (%s), continuing. (But expect crash now).\n",
ofs->szPathName,
(nt_header->FileHeader.Characteristics&IMAGE_FILE_RELOCS_STRIPPED)?"stripped during link":"unknown reason"
);
}
}
if(pem->pe_export) {
dump_exports(wm->module);
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_DEBUG;
if (dir->Size) TRACE( win32, "Debug directory ignored\n" );
wm->modname = HEAP_strdupA(process->heap,0,(char*)RVA(pem->pe_export->Name));
} else {
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_COPYRIGHT;
if (dir->Size) FIXME( win32, "Copyright string ignored\n" );
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_GLOBALPTR;
if (dir->Size) FIXME( win32, "Global Pointer (MIPS) ignored\n" );
/* IMAGE_DIRECTORY_ENTRY_TLS handled in PE_TlsInit */
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG;
if (dir->Size) FIXME (win32, "Load Configuration directory ignored\n" );
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT;
if (dir->Size) TRACE( win32, "Bound Import directory ignored\n" );
dir = nt->OptionalHeader.DataDirectory+IMAGE_DIRECTORY_ENTRY_IAT;
if (dir->Size) TRACE( win32, "Import Address Table directory ignored\n" );
dir = nt->OptionalHeader.DataDirectory+13;
if (dir->Size) FIXME( win32, "Unknown directory 13 ignored\n" );
dir = nt->OptionalHeader.DataDirectory+14;
if (dir->Size) FIXME( win32, "Unknown directory 14 ignored\n" );
dir = nt->OptionalHeader.DataDirectory+15;
if (dir->Size) FIXME( win32, "Unknown directory 15 ignored\n" );
/* Allocate and fill WINE_MODREF */
wm = (WINE_MODREF *)HeapAlloc( process->heap, HEAP_ZERO_MEMORY, sizeof(*wm) );
wm->module = hModule;
wm->type = MODULE32_PE;
wm->binfmt.pe.flags = builtin? PE_MODREF_INTERNAL : 0;
wm->binfmt.pe.pe_export = pe_export;
wm->binfmt.pe.pe_import = pe_import;
wm->binfmt.pe.pe_resource = pe_resource;
if ( pe_export )
modname = (char *)RVA( pe_export->Name );
else
{
/* try to find out the name from the OFSTRUCT */
char *s;
modname = s = ofs->szPathName;
modname = ofs->szPathName;
while ((s=strchr(modname,'\\')))
modname = s+1;
wm->modname = HEAP_strdupA(process->heap,0,modname);
}
if(pem->pe_import) {
if (fixup_imports(process,wm)) {
WINE_MODREF **xwm;
wm->modname = HEAP_strdupA( process->heap, 0, modname );
result = GetLongPathName32A( ofs->szPathName, NULL, 0 );
wm->longname = (char *)HeapAlloc( process->heap, 0, result+1 );
GetLongPathName32A( ofs->szPathName, wm->longname, result+1 );
wm->shortname = HEAP_strdupA( process->heap, 0, ofs->szPathName );
/* Link MODREF into process list */
wm->next = process->modref_list;
process->modref_list = wm;
if ( !(nt->FileHeader.Characteristics & IMAGE_FILE_DLL) )
{
if ( process->exe_modref )
FIXME( win32, "overwriting old exe_modref... arrgh\n" );
process->exe_modref = wm;
}
/* Dump Exports */
if ( pe_export )
dump_exports( hModule );
/* Fixup Imports */
if ( pe_import && fixup_imports( process, wm ) )
{
/* remove entry from modref chain */
xwm = &(process->modref_list);
while (*xwm) {
if (*xwm==wm) {
WINE_MODREF **xwm;
for ( xwm = &process->modref_list; *xwm; xwm = &(*xwm)->next )
if ( *xwm == wm )
{
*xwm = wm->next;
break;
}
xwm = &((*xwm)->next);
}
/* FIXME: there are several more dangling references
* left. Including dlls loaded by this dll before the
* failed one. Unrolling is rather difficult with the
* current structure and we can leave it them lying
* around with no problems, so we don't care
* around with no problems, so we don't care.
* As these might reference our wm, we don't free it.
*/
return 0;
}
return NULL;
}
/* Now that we got everything at the right address,
* we can unmap the previous module */
UnmapViewOfFile( (LPVOID)hModule );
return 1;
return wm;
}
/******************************************************************************
* The PE Library Loader frontend.
* FIXME: handle the flags.
* internal module handling should be made better here (and in builtin.c)
*/
HMODULE32 PE_LoadLibraryEx32A (LPCSTR name, PDB32 *process,
HFILE32 hFile, DWORD flags)
{
OFSTRUCT ofs;
HMODULE32 hModule;
HMODULE32 hModule32;
HMODULE16 hModule16;
NE_MODULE *pModule;
WINE_MODREF *wm;
BOOL32 builtin;
if ((hModule = MODULE_FindModule32( process, name ))) {
for (wm= process->modref_list;wm;wm=wm->next)
if (wm->module == hModule)
return hModule;
/* Since MODULE_FindModule32 uses the modref chain too, the
* module MUST have been found above. If not, something has gone
* terribly wrong.
*/
assert(0);
}
/* try to load builtin, enabled modules first */
if ((hModule = BUILTIN32_LoadModule( name, FALSE, process )))
return hModule;
/* Check for already loaded module */
if ((hModule32 = MODULE_FindModule32( process, name )))
return hModule32;
/* try to load builtin, enabled modules first */
if ((hModule32 = BUILTIN32_LoadImage( name, &ofs, FALSE )))
builtin = TRUE;
/* try to load the specified dll/exe */
if (HFILE_ERROR32==(hFile=OpenFile32(name,&ofs,OF_READ))) {
else if ((hModule32 = PE_LoadImage( name, &ofs )) >= 32)
builtin = FALSE;
/* Now try the built-in even if disabled */
if ((hModule = BUILTIN32_LoadModule( name, TRUE, process ))) {
else if ((hModule32 = BUILTIN32_LoadImage( name, &ofs, TRUE )))
{
WARN( module, "Could not load external DLL '%s', using built-in module.\n", name );
return hModule;
builtin = TRUE;
}
else
return 0;
}
/* will go away ... */
if ((hModule = MODULE_CreateDummyModule( &ofs )) < 32) {
_lclose32(hFile);
return hModule;
}
pModule = (NE_MODULE *)GlobalLock16( hModule );
pModule->flags = NE_FFLAGS_WIN32;
/* .. */
wm=(WINE_MODREF*)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,sizeof(*wm));
wm->type = MODULE32_PE;
/* NOTE: fixup_imports takes care of the correct order */
wm->next = process->modref_list;
process->modref_list = wm;
wm->module = pModule->module32 = PE_LoadImage( hFile );
/* Create 16-bit dummy module */
if ((hModule16 = MODULE_CreateDummyModule( &ofs )) < 32) return hModule16;
pModule = (NE_MODULE *)GlobalLock16( hModule16 );
pModule->flags = NE_FFLAGS_LIBMODULE | NE_FFLAGS_SINGLEDATA |
NE_FFLAGS_WIN32 | (builtin? NE_FFLAGS_BUILTIN : 0);
pModule->module32 = hModule32;
CloseHandle( hFile );
if (wm->module < 32)
/* Create 32-bit MODREF */
if ( !(wm = PE_CreateModule( process, hModule32, &ofs, flags, builtin )) )
{
process->modref_list = wm->next;
FreeLibrary16( hModule);
HeapFree(process->heap,0,wm);
ERR(win32,"can't load %s\n",ofs.szPathName);
FreeLibrary16( hModule16 );
return 0;
}
/* (possible) recursion */
if (!PE_MapImage(process,wm,&ofs,flags)) {
/* ERROR cleanup ... */
WINE_MODREF **xwm;
ERR(win32,"couldn't load %s\n",ofs.szPathName);
/* unlink from process modref chain */
for ( xwm=&(process->modref_list);
*xwm && (*xwm!=wm);
xwm=&((*xwm)->next)
) /* EMPTY */;
if (*xwm)
*xwm=(*xwm)->next;
return 0;
}
pModule->module32 = wm->module;
if (wm->binfmt.pe.pe_export)
SNOOP_RegisterDLL(wm->module,wm->modname,wm->binfmt.pe.pe_export->NumberOfFunctions);
return wm->module;
}
......@@ -875,44 +859,37 @@ HINSTANCE16 PE_CreateProcess( LPCSTR name, LPCSTR cmd_line,
HMODULE32 hModule32;
HINSTANCE16 hInstance;
NE_MODULE *pModule;
HFILE32 hFile;
OFSTRUCT ofs;
PDB32 *process;
TDB *pTask;
WINE_MODREF *wm;
if ((hFile = OpenFile32( name, &ofs, OF_READ )) == HFILE_ERROR32)
return 2; /* File not found */
/* Load file */
if ((hModule32 = PE_LoadImage( name, &ofs )) < 32)
return hModule32;
if (PE_HEADER(hModule32)->FileHeader.Characteristics & IMAGE_FILE_DLL)
return 11;
/* Create 16-bit dummy module */
if ((hModule16 = MODULE_CreateDummyModule( &ofs )) < 32) return hModule16;
pModule = (NE_MODULE *)GlobalLock16( hModule16 );
pModule->flags = NE_FFLAGS_WIN32;
pModule->module32 = hModule32;
pModule->module32 = hModule32 = PE_LoadImage( hFile );
if (hModule32 < 32) return 21;
if (PE_HEADER(hModule32)->FileHeader.Characteristics & IMAGE_FILE_DLL)
return 11;
/* Create new process */
hInstance = NE_CreateInstance( pModule, NULL, FALSE );
process = PROCESS_Create( pModule, cmd_line, env,
hInstance, 0, startup, info );
pTask = (TDB *)GlobalLock16( process->task );
wm=(WINE_MODREF*)HeapAlloc(process->heap,HEAP_ZERO_MEMORY,sizeof(*wm));
wm->type = MODULE32_PE;
/* NOTE: fixup_imports takes care of the correct order */
wm->next = process->modref_list;
wm->module = hModule32;
process->modref_list = wm;
if (!PE_MapImage( process, wm, &ofs, 0 ))
/* Create 32-bit MODREF */
if ( !(wm = PE_CreateModule( process, hModule32, &ofs, 0, FALSE )) )
{
/* FIXME: should destroy the task created and free referenced stuff */
return 0;
}
pModule->module32 = wm->module;
/* FIXME: Yuck. Is there no other good place to do that? */
pTask = (TDB *)GlobalLock16( process->task );
PE_InitTls( pTask->thdb );
return hInstance;
......
......@@ -7,10 +7,8 @@
#include <assert.h>
#include <string.h>
#include "builtin32.h"
#include "module.h"
#include "peexe.h"
#include "heap.h"
#include "task.h"
#include "process.h"
#include "debug.h"
typedef struct
......@@ -113,17 +111,14 @@ static BUILTIN32_DLL BuiltinDLLs[] =
/***********************************************************************
* BUILTIN32_DoLoadModule
* BUILTIN32_DoLoadImage
*
* Load a built-in Win32 module. Helper function for BUILTIN32_LoadModule.
* Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
*/
static HMODULE32 BUILTIN32_DoLoadModule( BUILTIN32_DLL *dll, PDB32 *pdb )
static HMODULE32 BUILTIN32_DoLoadImage( BUILTIN32_DLL *dll )
{
extern void RELAY_CallFrom32();
HMODULE16 hModule;
NE_MODULE *pModule;
OFSTRUCT ofs;
IMAGE_DATA_DIRECTORY *dir;
IMAGE_DOS_HEADER *dos;
IMAGE_NT_HEADERS *nt;
......@@ -132,8 +127,6 @@ static HMODULE32 BUILTIN32_DoLoadModule( BUILTIN32_DLL *dll, PDB32 *pdb )
LPVOID *funcs;
LPSTR *names;
DEBUG_ENTRY_POINT *debug;
WINE_MODREF *wm;
PE_MODREF *pem;
INT32 i, size;
BYTE *addr;
......@@ -283,40 +276,16 @@ static HMODULE32 BUILTIN32_DoLoadModule( BUILTIN32_DLL *dll, PDB32 *pdb )
if (dll->descr->names[i])
*names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
/* Create a modref */
wm = (WINE_MODREF *)HeapAlloc( pdb->heap, HEAP_ZERO_MEMORY, sizeof(*wm) );
wm->type = MODULE32_PE;
pem = &(wm->binfmt.pe);
wm->module = (HMODULE32)addr;
wm->next = pdb->modref_list;
pdb->modref_list = wm;
wm->modname = HEAP_strdupA(pdb->heap,0,dll->descr->name);
/* FIXME: hmm ... probably add windows directory? don't know ... -MM */
wm->shortname = HEAP_strdupA(pdb->heap,0,wm->modname);
wm->longname = HEAP_strdupA(pdb->heap,0,wm->modname);
pem->pe_export = exp;
pem->flags = PE_MODREF_INTERNAL;
/* Create a Win16 dummy module */
sprintf( ofs.szPathName, "%s.DLL", dll->descr->name );
hModule = MODULE_CreateDummyModule( &ofs );
pModule = (NE_MODULE *)GlobalLock16( hModule );
pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
pModule->module32 = (HMODULE32)addr;
return pModule->module32;
return (HMODULE32)addr;
}
/***********************************************************************
* BUILTIN32_LoadModule
* BUILTIN32_LoadImage
*
* Load a built-in module. If the 'force' parameter is FALSE, we only
* load the module if it has not been disabled via the -dll option.
*/
HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force, PDB32 *process )
HMODULE32 BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs, BOOL32 force )
{
BUILTIN32_DLL *table;
char dllname[16], *p;
......@@ -335,7 +304,9 @@ HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force, PDB32 *process )
if (!force) return 0;
table->used = TRUE; /* So next time we use it at once */
}
return BUILTIN32_DoLoadModule( table, process );
sprintf( ofs->szPathName, "%s.DLL", table->descr->name );
return BUILTIN32_DoLoadImage( table );
}
......
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