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