From 18b4cbf12a8bcd5619c02637b8da7055493a3036 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Mon, 3 Nov 2003 22:19:44 +0000 Subject: [PATCH] Put better values for code and data sizes in the NT header of builtin dlls. --- libs/wine/loader.c | 29 +++++++++++++++++------------ tools/winebuild/spec32.c | 18 +++++++++++------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/libs/wine/loader.c b/libs/wine/loader.c index ece5dc44e0d..d3b87135416 100644 --- a/libs/wine/loader.c +++ b/libs/wine/loader.c @@ -210,8 +210,10 @@ static void *map_dll( const IMAGE_NT_HEADERS *nt_descr ) IMAGE_DOS_HEADER *dos; IMAGE_NT_HEADERS *nt; IMAGE_SECTION_HEADER *sec; - BYTE *addr, *code_start, *data_start; - size_t page_size = getpagesize(); + BYTE *addr; + DWORD code_start, data_start, data_end; + const size_t page_size = getpagesize(); + const size_t page_mask = page_size - 1; int nb_sections = 2; /* code + data */ size_t size = (sizeof(IMAGE_DOS_HEADER) @@ -227,10 +229,6 @@ static void *map_dll( const IMAGE_NT_HEADERS *nt_descr ) dos = (IMAGE_DOS_HEADER *)addr; nt = (IMAGE_NT_HEADERS *)(dos + 1); sec = (IMAGE_SECTION_HEADER *)(nt + 1); - code_start = addr + page_size; - - /* HACK! */ - data_start = code_start + page_size; /* Build the DOS and NT headers */ @@ -239,10 +237,17 @@ static void *map_dll( const IMAGE_NT_HEADERS *nt_descr ) *nt = *nt_descr; + code_start = page_size; + data_start = ((BYTE *)nt->OptionalHeader.BaseOfData - addr) & ~page_mask; + data_end = (((BYTE *)nt->OptionalHeader.SizeOfImage - addr) + page_mask) & ~page_mask; + nt->FileHeader.NumberOfSections = nb_sections; + nt->OptionalHeader.BaseOfCode = code_start; + nt->OptionalHeader.BaseOfData = data_start; nt->OptionalHeader.SizeOfCode = data_start - code_start; - nt->OptionalHeader.SizeOfInitializedData = 0; + nt->OptionalHeader.SizeOfInitializedData = data_end - data_start; nt->OptionalHeader.SizeOfUninitializedData = 0; + nt->OptionalHeader.SizeOfImage = data_end; nt->OptionalHeader.ImageBase = (DWORD)addr; fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 ); @@ -252,18 +257,18 @@ static void *map_dll( const IMAGE_NT_HEADERS *nt_descr ) 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->VirtualAddress = code_start; + sec->PointerToRawData = code_start; 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->SizeOfRawData = data_end - data_start; sec->Misc.VirtualSize = sec->SizeOfRawData; - sec->VirtualAddress = data_start - addr; - sec->PointerToRawData = data_start - addr; + sec->VirtualAddress = data_start; + sec->PointerToRawData = data_start; sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ); sec++; diff --git a/tools/winebuild/spec32.c b/tools/winebuild/spec32.c index bbe6fd1e7db..02567c3a99e 100644 --- a/tools/winebuild/spec32.c +++ b/tools/winebuild/spec32.c @@ -515,18 +515,22 @@ void BuildSpec32File( FILE *outfile ) /* Reserve some space for the PE header */ - fprintf( outfile, "extern char pe_header[];\n" ); + fprintf( outfile, "extern char __wine_spec_pe_header[];\n" ); fprintf( outfile, "#ifndef __GNUC__\n" ); fprintf( outfile, "static void __asm__dummy_header(void) {\n" ); fprintf( outfile, "#endif\n" ); fprintf( outfile, "asm(\".text\\n\\t\"\n" ); fprintf( outfile, " \".align %d\\n\"\n", get_alignment(page_size) ); - fprintf( outfile, " \"" __ASM_NAME("pe_header") ":\\t" __ASM_SKIP " 65536\\n\\t\");\n" ); + fprintf( outfile, " \"" __ASM_NAME("__wine_spec_pe_header") ":\\t" __ASM_SKIP " 65536\\n\\t\"\n" ); + fprintf( outfile, " \".data\\n\\t\"\n" ); + fprintf( outfile, " \".align %d\\n\"\n", get_alignment(4) ); + fprintf( outfile, " \"" __ASM_NAME("__wine_spec_data_start") ":\\t.long 1\");\n" ); fprintf( outfile, "#ifndef __GNUC__\n" ); fprintf( outfile, "}\n" ); fprintf( outfile, "#endif\n" ); - fprintf( outfile, "extern int __wine_spec_exports[];\n\n" ); + fprintf( outfile, "extern char _end[];\n" ); + fprintf( outfile, "extern int __wine_spec_data_start[], __wine_spec_exports[];\n\n" ); #ifdef __i386__ fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" ); @@ -753,7 +757,7 @@ void BuildSpec32File( FILE *outfile ) fprintf( outfile, " short MajorSubsystemVersion;\n" ); fprintf( outfile, " short MinorSubsystemVersion;\n" ); fprintf( outfile, " int Win32VersionValue;\n" ); - fprintf( outfile, " int SizeOfImage;\n" ); + fprintf( outfile, " void *SizeOfImage;\n" ); fprintf( outfile, " int SizeOfHeaders;\n" ); fprintf( outfile, " int CheckSum;\n" ); fprintf( outfile, " short Subsystem;\n" ); @@ -784,15 +788,15 @@ void BuildSpec32File( FILE *outfile ) fprintf( outfile, " 0, 0,\n" ); /* Major/MinorLinkerVersion */ fprintf( outfile, " 0, 0, 0,\n" ); /* SizeOfCode/Data */ fprintf( outfile, " %s,\n", init_func ? init_func : "DllMain" ); /* AddressOfEntryPoint */ - fprintf( outfile, " 0, 0,\n" ); /* BaseOfCode/Data */ - fprintf( outfile, " pe_header,\n" ); /* ImageBase */ + fprintf( outfile, " 0, __wine_spec_data_start,\n" ); /* BaseOfCode/Data */ + fprintf( outfile, " __wine_spec_pe_header,\n" ); /* ImageBase */ fprintf( outfile, " %ld,\n", page_size ); /* SectionAlignment */ fprintf( outfile, " %ld,\n", page_size ); /* FileAlignment */ fprintf( outfile, " 1, 0,\n" ); /* Major/MinorOperatingSystemVersion */ fprintf( outfile, " 0, 0,\n" ); /* Major/MinorImageVersion */ fprintf( outfile, " 4, 0,\n" ); /* Major/MinorSubsystemVersion */ fprintf( outfile, " 0,\n" ); /* Win32VersionValue */ - fprintf( outfile, " %ld,\n", page_size ); /* SizeOfImage */ + fprintf( outfile, " _end,\n" ); /* SizeOfImage */ fprintf( outfile, " %ld,\n", page_size ); /* SizeOfHeaders */ fprintf( outfile, " 0,\n" ); /* CheckSum */ fprintf( outfile, " 0x%04x,\n", subsystem ); /* Subsystem */