winebuild: Add support for creating a fake dll at compile time.

This commit is contained in:
Alexandre Julliard 2009-08-14 14:16:38 +02:00
parent 99037aa9cc
commit ef4a3c3c2d
6 changed files with 335 additions and 0 deletions

View file

@ -247,10 +247,13 @@ extern void output_imports( DLLSPEC *spec );
extern void output_exports( DLLSPEC *spec );
extern int load_res32_file( const char *name, DLLSPEC *spec );
extern void output_resources( DLLSPEC *spec );
extern void output_bin_resources( DLLSPEC *spec, unsigned int start_rva );
extern void output_fake_module( DLLSPEC *spec );
extern void load_res16_file( const char *name, DLLSPEC *spec );
extern void output_res16_data( DLLSPEC *spec );
extern void output_res16_directory( DLLSPEC *spec );
extern void output_spec16_file( DLLSPEC *spec );
extern void output_fake_module16( DLLSPEC *spec16 );
extern void output_res_o_file( DLLSPEC *spec );
extern void BuildRelays16(void);

View file

@ -80,6 +80,7 @@ char *spec_file_name = NULL;
FILE *output_file = NULL;
const char *output_file_name = NULL;
static const char *output_file_source_name;
static int fake_module;
char *as_command = NULL;
char *ld_command = NULL;
@ -216,6 +217,7 @@ static const char usage_str[] =
" --external-symbols Allow linking to external symbols\n"
" -f FLAGS Compiler flags (only -fPIC is supported)\n"
" -F, --filename=DLLFILE Set the DLL filename (default: from input file name)\n"
" --fake-module Create a fake binary module\n"
" -h, --help Display this help message\n"
" -H, --heap=SIZE Set the heap size for a Win16 dll\n"
" -i, --ignore=SYM[,SYM] Ignore specified symbols when resolving imports\n"
@ -255,6 +257,7 @@ enum long_options_values
LONG_OPT_EXE,
LONG_OPT_ASCMD,
LONG_OPT_EXTERNAL_SYMS,
LONG_OPT_FAKE_MODULE,
LONG_OPT_LARGE_ADDRESS_AWARE,
LONG_OPT_LDCMD,
LONG_OPT_NMCMD,
@ -276,6 +279,7 @@ static const struct option long_options[] =
{ "exe", 0, 0, LONG_OPT_EXE },
{ "as-cmd", 1, 0, LONG_OPT_ASCMD },
{ "external-symbols", 0, 0, LONG_OPT_EXTERNAL_SYMS },
{ "fake-module", 0, 0, LONG_OPT_FAKE_MODULE },
{ "large-address-aware", 0, 0, LONG_OPT_LARGE_ADDRESS_AWARE },
{ "ld-cmd", 1, 0, LONG_OPT_LDCMD },
{ "nm-cmd", 1, 0, LONG_OPT_NMCMD },
@ -451,6 +455,9 @@ static char **parse_options( int argc, char **argv, DLLSPEC *spec )
case LONG_OPT_ASCMD:
as_command = xstrdup( optarg );
break;
case LONG_OPT_FAKE_MODULE:
fake_module = 1;
break;
case LONG_OPT_EXTERNAL_SYMS:
link_ext_symbols = 1;
break;
@ -607,6 +614,12 @@ int main(int argc, char **argv)
load_resources( argv, spec );
load_import_libs( argv );
if (spec_file_name && !parse_input_file( spec )) break;
if (fake_module)
{
if (spec->type == SPEC_WIN16) output_fake_module16( spec );
else output_fake_module( spec );
break;
}
read_undef_symbols( spec, argv );
switch (spec->type)
{

View file

@ -476,6 +476,109 @@ void output_resources( DLLSPEC *spec )
free_resource_tree( tree );
}
/* output a Unicode string in binary format */
static void output_bin_string( const WCHAR *name )
{
int i, len = strlenW(name);
put_word( len );
for (i = 0; i < len; i++) put_word( name[i] );
}
/* output a resource directory in binary format */
static inline void output_bin_res_dir( unsigned int nb_names, unsigned int nb_ids )
{
put_dword( 0 ); /* Characteristics */
put_dword( 0 ); /* TimeDateStamp */
put_word( 0 ); /* MajorVersion */
put_word( 0 ); /* MinorVersion */
put_word( nb_names ); /* NumberOfNamedEntries */
put_word( nb_ids ); /* NumberOfIdEntries */
}
/* output the resource definitions in binary format */
void output_bin_resources( DLLSPEC *spec, unsigned int start_rva )
{
int k, nb_id_types;
unsigned int i, n, data_offset;
struct res_tree *tree;
struct res_type *type;
struct res_name *name;
const struct resource *res;
if (!spec->nb_resources) return;
tree = build_resource_tree( spec, &data_offset );
init_output_buffer();
/* output the resource directories */
for (i = nb_id_types = 0, type = tree->types; i < tree->nb_types; i++, type++)
if (!type->type->str) nb_id_types++;
output_bin_res_dir( tree->nb_types - nb_id_types, nb_id_types );
/* dump the type directory */
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
{
put_dword( type->name_offset );
put_dword( type->dir_offset | 0x80000000 );
}
/* dump the names and languages directories */
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
{
output_bin_res_dir( type->nb_names - type->nb_id_names, type->nb_id_names );
for (n = 0, name = type->names; n < type->nb_names; n++, name++)
{
put_dword( name->name_offset );
put_dword( name->dir_offset | 0x80000000 );
}
for (n = 0, name = type->names; n < type->nb_names; n++, name++)
{
output_bin_res_dir( 0, name->nb_languages );
for (k = 0, res = name->res; k < name->nb_languages; k++, res++)
{
put_dword( res->lang );
put_dword( res->data_offset );
}
}
}
/* dump the resource data entries */
for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
{
put_dword( data_offset + start_rva );
put_dword( (res->data_size + 3) & ~3 );
put_dword( 0 );
put_dword( 0 );
data_offset += (res->data_size + 3) & ~3;
}
/* dump the name strings */
for (i = 0, type = tree->types; i < tree->nb_types; i++, type++)
{
if (type->type->str) output_bin_string( type->type->str );
for (n = 0, name = type->names; n < type->nb_names; n++, name++)
if (name->name->str) output_bin_string( name->name->str );
}
/* resource data */
align_output( 4 );
for (i = 0, res = spec->resources; i < spec->nb_resources; i++, res++)
{
put_data( res->data, res->data_size );
align_output( 4 );
}
free_resource_tree( tree );
}
static unsigned int get_resource_header_size( const struct resource *res )
{
unsigned int size = 5 * sizeof(unsigned int) + 2 * sizeof(unsigned short);

View file

@ -896,3 +896,13 @@ void output_spec16_file( DLLSPEC *spec16 )
output_gnu_stack_note();
free_dll_spec( spec32 );
}
/*******************************************************************
* output_fake_module16
*
* Create a fake 16-bit binary module.
*/
void output_fake_module16( DLLSPEC *spec16 )
{
warning( "not implemented yet\n" );
}

View file

@ -553,6 +553,207 @@ void BuildSpec32File( DLLSPEC *spec )
}
/*******************************************************************
* output_fake_module
*
* Build a fake binary module from a spec file.
*/
void output_fake_module( DLLSPEC *spec )
{
static const unsigned char dll_code_section[] = { 0x31, 0xc0, /* xor %eax,%eax */
0xc2, 0x0c, 0x00 }; /* ret $12 */
static const unsigned char exe_code_section[] = { 0xb8, 0x01, 0x00, 0x00, 0x00, /* movl $1,%eax */
0xc2, 0x04, 0x00 }; /* ret $4 */
static const char fakedll_signature[] = "Wine placeholder DLL";
const unsigned int page_size = get_page_size();
const unsigned int section_align = page_size;
const unsigned int file_align = 0x200;
const unsigned int reloc_size = 8;
const unsigned int lfanew = (0x40 + sizeof(fakedll_signature) + 15) & ~15;
const unsigned int nb_sections = 2 + (spec->nb_resources != 0);
const unsigned int text_size = (spec->characteristics & IMAGE_FILE_DLL) ?
sizeof(dll_code_section) : sizeof(exe_code_section);
unsigned char *resources;
unsigned int resources_size;
unsigned int image_size = 3 * section_align;
resolve_imports( spec );
output_bin_resources( spec, 3 * section_align );
resources = output_buffer;
resources_size = output_buffer_pos;
if (resources_size) image_size += (resources_size + section_align - 1) & ~(section_align - 1);
init_output_buffer();
put_word( 0x5a4d ); /* e_magic */
put_word( 0x40 ); /* e_cblp */
put_word( 0x01 ); /* e_cp */
put_word( 0 ); /* e_crlc */
put_word( lfanew / 16 ); /* e_cparhdr */
put_word( 0x0000 ); /* e_minalloc */
put_word( 0xffff ); /* e_maxalloc */
put_word( 0x0000 ); /* e_ss */
put_word( 0x00b8 ); /* e_sp */
put_word( 0 ); /* e_csum */
put_word( 0 ); /* e_ip */
put_word( 0 ); /* e_cs */
put_word( lfanew ); /* e_lfarlc */
put_word( 0 ); /* e_ovno */
put_dword( 0 ); /* e_res */
put_dword( 0 );
put_word( 0 ); /* e_oemid */
put_word( 0 ); /* e_oeminfo */
put_dword( 0 ); /* e_res2 */
put_dword( 0 );
put_dword( 0 );
put_dword( 0 );
put_dword( 0 );
put_dword( lfanew );
put_data( fakedll_signature, sizeof(fakedll_signature) );
align_output( 16 );
put_dword( 0x4550 ); /* Signature */
switch(target_cpu)
{
case CPU_x86: put_word( IMAGE_FILE_MACHINE_I386 ); break;
case CPU_x86_64: put_word( IMAGE_FILE_MACHINE_AMD64 ); break;
case CPU_POWERPC: put_word( IMAGE_FILE_MACHINE_POWERPC ); break;
case CPU_ALPHA: put_word( IMAGE_FILE_MACHINE_ALPHA ); break;
case CPU_SPARC: put_word( IMAGE_FILE_MACHINE_UNKNOWN ); break;
}
put_word( nb_sections ); /* NumberOfSections */
put_dword( 0 ); /* TimeDateStamp */
put_dword( 0 ); /* PointerToSymbolTable */
put_dword( 0 ); /* NumberOfSymbols */
put_word( get_ptr_size() == 8 ?
IMAGE_SIZEOF_NT_OPTIONAL64_HEADER :
IMAGE_SIZEOF_NT_OPTIONAL32_HEADER ); /* SizeOfOptionalHeader */
put_word( spec->characteristics ); /* Characteristics */
put_word( get_ptr_size() == 8 ?
IMAGE_NT_OPTIONAL_HDR64_MAGIC :
IMAGE_NT_OPTIONAL_HDR32_MAGIC ); /* Magic */
put_byte( 0 ); /* MajorLinkerVersion */
put_byte( 0 ); /* MinorLinkerVersion */
put_dword( text_size ); /* SizeOfCode */
put_dword( 0 ); /* SizeOfInitializedData */
put_dword( 0 ); /* SizeOfUninitializedData */
put_dword( section_align ); /* AddressOfEntryPoint */
put_dword( section_align ); /* BaseOfCode */
if (get_ptr_size() == 4) put_dword( 0 ); /* BaseOfData */
put_pword( 0x10000000 ); /* ImageBase */
put_dword( section_align ); /* SectionAlignment */
put_dword( file_align ); /* FileAlignment */
put_word( 1 ); /* MajorOperatingSystemVersion */
put_word( 0 ); /* MinorOperatingSystemVersion */
put_word( 0 ); /* MajorImageVersion */
put_word( 0 ); /* MinorImageVersion */
put_word( spec->subsystem_major ); /* MajorSubsystemVersion */
put_word( spec->subsystem_minor ); /* MinorSubsystemVersion */
put_dword( 0 ); /* Win32VersionValue */
put_dword( image_size ); /* SizeOfImage */
put_dword( file_align ); /* SizeOfHeaders */
put_dword( 0 ); /* CheckSum */
put_word( spec->subsystem ); /* Subsystem */
put_word( spec->dll_characteristics ); /* DllCharacteristics */
put_pword( (spec->stack_size ? spec->stack_size : 1024) * 1024 ); /* SizeOfStackReserve */
put_pword( page_size ); /* SizeOfStackCommit */
put_pword( (spec->heap_size ? spec->heap_size : 1024) * 1024 ); /* SizeOfHeapReserve */
put_pword( page_size ); /* SizeOfHeapCommit */
put_dword( 0 ); /* LoaderFlags */
put_dword( 16 ); /* NumberOfRvaAndSizes */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] */
if (resources_size) /* DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE] */
{
put_dword( 3 * section_align );
put_dword( resources_size );
}
else
{
put_dword( 0 );
put_dword( 0 );
}
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_EXCEPTION] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_SECURITY] */
put_dword( 2 * section_align ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC] */
put_dword( reloc_size );
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COPYRIGHT] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_GLOBALPTR] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_TLS] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR] */
put_dword( 0 ); put_dword( 0 ); /* DataDirectory[15] */
/* .text section */
put_data( ".text\0\0", 8 ); /* Name */
put_dword( section_align ); /* VirtualSize */
put_dword( section_align ); /* VirtualAddress */
put_dword( text_size ); /* SizeOfRawData */
put_dword( file_align ); /* PointerToRawData */
put_dword( 0 ); /* PointerToRelocations */
put_dword( 0 ); /* PointerToLinenumbers */
put_word( 0 ); /* NumberOfRelocations */
put_word( 0 ); /* NumberOfLinenumbers */
put_dword( 0x60000020 /* CNT_CODE|MEM_EXECUTE|MEM_READ */ ); /* Characteristics */
/* .reloc section */
put_data( ".reloc\0", 8 ); /* Name */
put_dword( section_align ); /* VirtualSize */
put_dword( 2 * section_align );/* VirtualAddress */
put_dword( reloc_size ); /* SizeOfRawData */
put_dword( 2 * file_align ); /* PointerToRawData */
put_dword( 0 ); /* PointerToRelocations */
put_dword( 0 ); /* PointerToLinenumbers */
put_word( 0 ); /* NumberOfRelocations */
put_word( 0 ); /* NumberOfLinenumbers */
put_dword( 0x42000040 /* CNT_INITIALIZED_DATA|MEM_DISCARDABLE|MEM_READ */ ); /* Characteristics */
/* .rsrc section */
if (resources_size)
{
put_data( ".rsrc\0\0", 8 ); /* Name */
put_dword( (resources_size + section_align - 1) & ~(section_align - 1) ); /* VirtualSize */
put_dword( 3 * section_align );/* VirtualAddress */
put_dword( resources_size ); /* SizeOfRawData */
put_dword( 3 * file_align ); /* PointerToRawData */
put_dword( 0 ); /* PointerToRelocations */
put_dword( 0 ); /* PointerToLinenumbers */
put_word( 0 ); /* NumberOfRelocations */
put_word( 0 ); /* NumberOfLinenumbers */
put_dword( 0x40000040 /* CNT_INITIALIZED_DATA|MEM_READ */ ); /* Characteristics */
}
/* .text contents */
align_output( file_align );
if (spec->characteristics & IMAGE_FILE_DLL)
put_data( dll_code_section, sizeof(dll_code_section) );
else
put_data( exe_code_section, sizeof(exe_code_section) );
/* .reloc contents */
align_output( file_align );
put_dword( 0 ); /* VirtualAddress */
put_dword( 0 ); /* SizeOfBlock */
/* .rsrc contents */
if (resources_size)
{
align_output( file_align );
put_data( resources, resources_size );
}
flush_output_buffer();
}
/*******************************************************************
* BuildDef32File
*

View file

@ -108,6 +108,11 @@ specification must be used instead).
.BI \-f\ flags
Ignored for compatibility with the C compiler.
.TP
.B \--fake-module
Create a fake PE module for a dll or exe, instead of the normal
assembly or object file. The PE module contains the resources for the
module, but no executable code.
.TP
.BI \-F,\ --filename= filename
Set the file name of the module. The default is to use the base name
of the spec file (without any extension).