Sweden-Number/relay32/builtin32.c

374 lines
11 KiB
C

/*
* Win32 builtin functions
*
* Copyright 1997 Alexandre Julliard
*/
#include "config.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_DL_API
#include <dlfcn.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include "windef.h"
#include "wine/winbase16.h"
#include "builtin32.h"
#include "elfdll.h"
#include "file.h"
#include "global.h"
#include "neexe.h"
#include "heap.h"
#include "main.h"
#include "winerror.h"
#include "server.h"
#include "debugtools.h"
DEFAULT_DEBUG_CHANNEL(module);
DECLARE_DEBUG_CHANNEL(relay);
typedef struct
{
BYTE *restab;
DWORD nresources;
DWORD restabsize;
IMAGE_RESOURCE_DATA_ENTRY *entries;
} BUILTIN32_RESOURCE;
#define MAX_DLLS 60
static const BUILTIN32_DESCRIPTOR *builtin_dlls[MAX_DLLS];
static int nb_dlls;
/***********************************************************************
* BUILTIN32_dlopen
*/
void *BUILTIN32_dlopen( const char *name )
{
#ifdef HAVE_DL_API
void *handle;
char buffer[128], *p;
if ((p = strrchr( name, '/' ))) name = p + 1;
if ((p = strrchr( name, '\\' ))) name = p + 1;
sprintf( buffer, "lib%s", name );
for (p = buffer; *p; p++) *p = tolower(*p);
if ((p = strrchr( buffer, '.' )) && (!strcmp( p, ".dll" ) || !strcmp( p, ".exe" ))) *p = 0;
strcat( buffer, ".so" );
if (!(handle = ELFDLL_dlopen( buffer, RTLD_NOW )))
WARN( "failed to load %s: %s\n", buffer, dlerror() );
return handle;
#else
return NULL;
#endif
}
/***********************************************************************
* BUILTIN32_dlclose
*/
int BUILTIN32_dlclose( void *handle )
{
#ifdef HAVE_DL_API
/* FIXME: should unregister descriptors first */
/* return dlclose( handle ); */
#endif
return 0;
}
/***********************************************************************
* fixup_rva_ptrs
*
* Adjust an array of pointers to make them into RVAs.
*/
static inline void fixup_rva_ptrs( void *array, void *base, int count )
{
void **ptr = (void **)array;
while (count--)
{
if (*ptr) *ptr = (void *)((char *)*ptr - (char *)base);
ptr++;
}
}
/***********************************************************************
* BUILTIN32_DoLoadImage
*
* Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
*/
static HMODULE BUILTIN32_DoLoadImage( const BUILTIN32_DESCRIPTOR *descr )
{
IMAGE_DATA_DIRECTORY *dir;
IMAGE_DOS_HEADER *dos;
IMAGE_NT_HEADERS *nt;
IMAGE_SECTION_HEADER *sec;
IMAGE_IMPORT_DESCRIPTOR *imp;
IMAGE_EXPORT_DIRECTORY *exports = descr->exports;
INT i, size, nb_sections;
BYTE *addr, *code_start, *data_start;
int page_size = VIRTUAL_GetPageSize();
/* Allocate the module */
nb_sections = 2; /* code + data */
size = (sizeof(IMAGE_DOS_HEADER)
+ sizeof(IMAGE_NT_HEADERS)
+ nb_sections * sizeof(IMAGE_SECTION_HEADER)
+ (descr->nb_imports+1) * sizeof(IMAGE_IMPORT_DESCRIPTOR));
assert( size <= page_size );
if (descr->pe_header)
{
if ((addr = FILE_dommap( -1, descr->pe_header, 0, page_size, 0, 0,
PROT_READ|PROT_WRITE, MAP_FIXED )) != descr->pe_header)
{
ERR("failed to map over PE header for %s at %p\n", descr->filename, descr->pe_header );
return 0;
}
}
else
{
if (!(addr = VirtualAlloc( NULL, page_size, MEM_COMMIT, PAGE_READWRITE ))) return 0;
}
dos = (IMAGE_DOS_HEADER *)addr;
nt = (IMAGE_NT_HEADERS *)(dos + 1);
sec = (IMAGE_SECTION_HEADER *)(nt + 1);
imp = (IMAGE_IMPORT_DESCRIPTOR *)(sec + nb_sections);
code_start = addr + page_size;
/* HACK! */
data_start = code_start + page_size;
/* Build the DOS and NT headers */
dos->e_magic = IMAGE_DOS_SIGNATURE;
dos->e_lfanew = sizeof(*dos);
nt->Signature = IMAGE_NT_SIGNATURE;
nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
nt->FileHeader.NumberOfSections = nb_sections;
nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
nt->FileHeader.Characteristics = descr->characteristics;
nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
nt->OptionalHeader.SizeOfCode = data_start - code_start;
nt->OptionalHeader.SizeOfInitializedData = 0;
nt->OptionalHeader.SizeOfUninitializedData = 0;
nt->OptionalHeader.ImageBase = (DWORD)addr;
nt->OptionalHeader.SectionAlignment = page_size;
nt->OptionalHeader.FileAlignment = page_size;
nt->OptionalHeader.MajorOperatingSystemVersion = 1;
nt->OptionalHeader.MinorOperatingSystemVersion = 0;
nt->OptionalHeader.MajorSubsystemVersion = 4;
nt->OptionalHeader.MinorSubsystemVersion = 0;
nt->OptionalHeader.SizeOfImage = page_size;
nt->OptionalHeader.SizeOfHeaders = page_size;
nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
if (descr->dllentrypoint)
nt->OptionalHeader.AddressOfEntryPoint = (DWORD)descr->dllentrypoint - (DWORD)addr;
/* Build the code section */
strcpy( sec->Name, ".text" );
sec->SizeOfRawData = data_start - code_start;
sec->Misc.VirtualSize = sec->SizeOfRawData;
sec->VirtualAddress = code_start - addr;
sec->PointerToRawData = code_start - addr;
sec->Characteristics = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
sec++;
/* Build the data section */
strcpy( sec->Name, ".data" );
sec->SizeOfRawData = 0;
sec->Misc.VirtualSize = sec->SizeOfRawData;
sec->VirtualAddress = data_start - addr;
sec->PointerToRawData = data_start - addr;
sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
sec++;
/* Build the import directory */
if (descr->nb_imports)
{
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
dir->VirtualAddress = (BYTE *)imp - addr;
dir->Size = sizeof(*imp) * (descr->nb_imports + 1);
/* Build the imports */
for (i = 0; i < descr->nb_imports; i++)
{
imp[i].u.Characteristics = 0;
imp[i].ForwarderChain = -1;
imp[i].Name = (BYTE *)descr->imports[i] - addr;
/* hack: make first thunk point to some zero value */
imp[i].FirstThunk = (PIMAGE_THUNK_DATA)((BYTE *)&imp[i].u.Characteristics - addr);
}
}
/* Build the resource directory */
if (descr->rsrc)
{
BUILTIN32_RESOURCE *rsrc = descr->rsrc;
IMAGE_RESOURCE_DATA_ENTRY *rdep;
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
dir->VirtualAddress = (BYTE *)rsrc->restab - addr;
dir->Size = rsrc->restabsize;
rdep = rsrc->entries;
for (i = 0; i < rsrc->nresources; i++) rdep[i].OffsetToData += dir->VirtualAddress;
}
/* Build the export directory */
if (exports)
{
dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
dir->VirtualAddress = (BYTE *)exports - addr;
dir->Size = descr->exports_size;
fixup_rva_ptrs( (void *)exports->AddressOfFunctions, addr, exports->NumberOfFunctions );
fixup_rva_ptrs( (void *)exports->AddressOfNames, addr, exports->NumberOfNames );
fixup_rva_ptrs( &exports->Name, addr, 1 );
fixup_rva_ptrs( &exports->AddressOfFunctions, addr, 1 );
fixup_rva_ptrs( &exports->AddressOfNames, addr, 1 );
fixup_rva_ptrs( &exports->AddressOfNameOrdinals, addr, 1 );
/* Setup relay debugging entry points */
if (WARN_ON(relay) || TRACE_ON(relay)) RELAY_SetupDLL( addr );
}
return (HMODULE)addr;
}
/***********************************************************************
* BUILTIN32_LoadLibraryExA
*
* Partly copied from the original PE_ version.
*
*/
WINE_MODREF *BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags)
{
HMODULE module;
WINE_MODREF *wm;
char dllname[20], *p;
LPCSTR name;
void *handle;
int i;
/* Fix the name in case we have a full path and extension */
name = path;
if ((p = strrchr( name, '\\' ))) name = p + 1;
if ((p = strrchr( name, '/' ))) name = p + 1;
if (strlen(name) >= sizeof(dllname)-4) goto error;
strcpy( dllname, name );
p = strrchr( dllname, '.' );
if (!p) strcat( dllname, ".dll" );
/* Search built-in descriptor */
for (i = 0; i < nb_dlls; i++)
if (!strcasecmp( builtin_dlls[i]->filename, dllname )) goto found;
if ((handle = BUILTIN32_dlopen( dllname )))
{
for (i = 0; i < nb_dlls; i++)
if (!strcasecmp( builtin_dlls[i]->filename, dllname )) goto found;
ERR( "loaded .so but dll %s still not found\n", dllname );
BUILTIN32_dlclose( handle );
}
error:
SetLastError( ERROR_FILE_NOT_FOUND );
return NULL;
found:
/* Load built-in module */
if (!(module = BUILTIN32_DoLoadImage( builtin_dlls[i] ))) return NULL;
/* Create 32-bit MODREF */
if ( !(wm = PE_CreateModule( module, path, flags, -1, TRUE )) )
{
ERR( "can't load %s\n", path );
SetLastError( ERROR_OUTOFMEMORY );
return NULL;
}
wm->refCount++; /* we don't support freeing builtin dlls (FIXME)*/
return wm;
}
/***********************************************************************
* BUILTIN32_LoadExeModule
*/
HMODULE BUILTIN32_LoadExeModule(void)
{
int i, exe = -1;
/* Search built-in EXE descriptor */
for ( i = 0; i < nb_dlls; i++ )
if ( !(builtin_dlls[i]->characteristics & IMAGE_FILE_DLL) )
{
if ( exe != -1 )
{
MESSAGE( "More than one built-in EXE module loaded!\n" );
break;
}
exe = i;
}
if ( exe == -1 )
{
MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
return 0;
}
/* Load built-in module */
return BUILTIN32_DoLoadImage( builtin_dlls[exe] );
}
/***********************************************************************
* BUILTIN32_RegisterDLL
*
* Register a built-in DLL descriptor.
*/
void BUILTIN32_RegisterDLL( const BUILTIN32_DESCRIPTOR *descr )
{
assert( nb_dlls < MAX_DLLS );
builtin_dlls[nb_dlls++] = descr;
}
/***********************************************************************
* BUILTIN32_Unimplemented
*
* This function is called for unimplemented 32-bit entry points (declared
* as 'stub' in the spec file).
*/
void BUILTIN32_Unimplemented( const char *dllname, const char *funcname )
{
__RESTORE_ES; /* Just in case */
MESSAGE( "FATAL: No handler for Win32 routine %s.%s", dllname, funcname );
#ifdef __GNUC__
MESSAGE( " (called from %p)", __builtin_return_address(1) );
#endif
MESSAGE( "\n" );
ExitProcess(1);
}