Module: wine Branch: master Commit: db3af1b238c3fe5f1fa7566787eb2afe292e0277 URL: https://gitlab.winehq.org/wine/wine/-/commit/db3af1b238c3fe5f1fa7566787eb2af...
Author: Alexandre Julliard julliard@winehq.org Date: Mon Sep 25 13:00:43 2023 +0200
ntdll: Consistently use the IMAGE_FIRST_SECTION helper macro.
---
dlls/ntdll/loader.c | 9 +++------ dlls/ntdll/tests/virtual.c | 3 +-- dlls/ntdll/unix/loader.c | 4 ++-- dlls/ntdll/unix/virtual.c | 10 +++++----- include/winnt.h | 3 +-- 5 files changed, 12 insertions(+), 17 deletions(-)
diff --git a/dlls/ntdll/loader.c b/dlls/ntdll/loader.c index 67104e63286..5b2f9bdce0d 100644 --- a/dlls/ntdll/loader.c +++ b/dlls/ntdll/loader.c @@ -2059,8 +2059,7 @@ static NTSTATUS perform_relocations( void *module, IMAGE_NT_HEADERS *nt, SIZE_T if (nt->FileHeader.NumberOfSections > ARRAY_SIZE( protect_old )) return STATUS_INVALID_IMAGE_FORMAT;
- sec = (const IMAGE_SECTION_HEADER *)((const char *)&nt->OptionalHeader + - nt->FileHeader.SizeOfOptionalHeader); + sec = IMAGE_FIRST_SECTION( nt ); for (i = 0; i < nt->FileHeader.NumberOfSections; i++) { void *addr = get_rva( module, sec[i].VirtualAddress ); @@ -2216,8 +2215,8 @@ static BOOL convert_to_pe64( HMODULE module, const SECTION_IMAGE_INFORMATION *in IMAGE_OPTIONAL_HEADER32 hdr32 = { IMAGE_NT_OPTIONAL_HDR32_MAGIC }; IMAGE_OPTIONAL_HEADER64 hdr64 = { IMAGE_NT_OPTIONAL_HDR64_MAGIC }; IMAGE_NT_HEADERS *nt = RtlImageNtHeader( module ); + IMAGE_SECTION_HEADER *sec = IMAGE_FIRST_SECTION( nt ); SIZE_T hdr_size = min( sizeof(hdr32), nt->FileHeader.SizeOfOptionalHeader ); - IMAGE_SECTION_HEADER *sec = (IMAGE_SECTION_HEADER *)((char *)&nt->OptionalHeader + hdr_size); SIZE_T size = min( nt->OptionalHeader.SizeOfHeaders, nt->OptionalHeader.SizeOfImage ); void *addr = module; ULONG i, old_prot; @@ -4361,10 +4360,8 @@ PIMAGE_SECTION_HEADER WINAPI RtlImageRvaToSection( const IMAGE_NT_HEADERS *nt, HMODULE module, DWORD rva ) { int i; - const IMAGE_SECTION_HEADER *sec; + const IMAGE_SECTION_HEADER *sec = IMAGE_FIRST_SECTION( nt );
- sec = (const IMAGE_SECTION_HEADER*)((const char*)&nt->OptionalHeader + - nt->FileHeader.SizeOfOptionalHeader); for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++) { if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva)) diff --git a/dlls/ntdll/tests/virtual.c b/dlls/ntdll/tests/virtual.c index 609d702a359..a9dec1b5a8a 100644 --- a/dlls/ntdll/tests/virtual.c +++ b/dlls/ntdll/tests/virtual.c @@ -2053,8 +2053,7 @@ static void perform_relocations( void *module, INT_PTR delta ) nt = RtlImageNtHeader( module ); relocs = &nt->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; if (!relocs->VirtualAddress || !relocs->Size) return; - sec = (const IMAGE_SECTION_HEADER *)((const char *)&nt->OptionalHeader + - nt->FileHeader.SizeOfOptionalHeader); + sec = IMAGE_FIRST_SECTION( nt ); for (i = 0; i < nt->FileHeader.NumberOfSections; i++) { void *addr = (char *)module + sec[i].VirtualAddress; diff --git a/dlls/ntdll/unix/loader.c b/dlls/ntdll/unix/loader.c index 38aaf778ff5..7bf3ca22566 100644 --- a/dlls/ntdll/unix/loader.c +++ b/dlls/ntdll/unix/loader.c @@ -1125,7 +1125,7 @@ static void relocate_ntdll( void *module )
if (!(rel = get_module_data_dir( module, IMAGE_DIRECTORY_ENTRY_BASERELOC, &size ))) return;
- sec = (IMAGE_SECTION_HEADER *)((char *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader); + sec = IMAGE_FIRST_SECTION( nt ); for (i = 0; i < nt->FileHeader.NumberOfSections; i++) { void *addr = get_rva( module, sec[i].VirtualAddress ); @@ -1991,7 +1991,7 @@ static void load_apiset_dll(void) if (!status) { nt = get_rva( ptr, ((IMAGE_DOS_HEADER *)ptr)->e_lfanew ); - sec = (IMAGE_SECTION_HEADER *)((char *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader); + sec = IMAGE_FIRST_SECTION( nt );
for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++) { diff --git a/dlls/ntdll/unix/virtual.c b/dlls/ntdll/unix/virtual.c index 078fe1c30a9..94e68edab77 100644 --- a/dlls/ntdll/unix/virtual.c +++ b/dlls/ntdll/unix/virtual.c @@ -2608,7 +2608,7 @@ static NTSTATUS map_image_into_view( struct file_view *view, const WCHAR *filena int i; off_t pos; struct stat st; - char *header_end, *header_start; + char *header_end; char *ptr = view->base; SIZE_T header_size, total_size = view->size;
@@ -2626,12 +2626,12 @@ static NTSTATUS map_image_into_view( struct file_view *view, const WCHAR *filena header_end = ptr + ROUND_SIZE( 0, header_size ); memset( ptr + header_size, 0, header_end - (ptr + header_size) ); if ((char *)(nt + 1) > header_end) return status; - header_start = (char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader; if (nt->FileHeader.NumberOfSections > ARRAY_SIZE( sections )) return status; - if (header_start + sizeof(*sections) * nt->FileHeader.NumberOfSections > header_end) return status; + sec = IMAGE_FIRST_SECTION( nt ); + if ((char *)(sec + nt->FileHeader.NumberOfSections) > header_end) return status; /* Some applications (e.g. the Steam version of Borderlands) map over the top of the section headers, * copying the headers into local memory is necessary to properly load such applications. */ - memcpy(sections, header_start, sizeof(*sections) * nt->FileHeader.NumberOfSections); + memcpy(sections, sec, sizeof(*sections) * nt->FileHeader.NumberOfSections); sec = sections;
imports = nt->OptionalHeader.DataDirectory + IMAGE_DIRECTORY_ENTRY_IMPORT; @@ -3297,7 +3297,7 @@ NTSTATUS virtual_create_builtin_view( void *module, const UNICODE_STRING *nt_nam /* The PE header is always read-only, no write, no execute. */ set_page_vprot( base, page_size, VPROT_COMMITTED | VPROT_READ );
- sec = (IMAGE_SECTION_HEADER *)((char *)&nt->OptionalHeader + nt->FileHeader.SizeOfOptionalHeader); + sec = IMAGE_FIRST_SECTION( nt ); for (i = 0; i < nt->FileHeader.NumberOfSections; i++) { BYTE flags = VPROT_COMMITTED; diff --git a/include/winnt.h b/include/winnt.h index d4137a64aea..df0c3282f8c 100644 --- a/include/winnt.h +++ b/include/winnt.h @@ -2927,8 +2927,7 @@ typedef struct _IMAGE_SECTION_HEADER { #define IMAGE_SIZEOF_SECTION_HEADER 40
#define IMAGE_FIRST_SECTION(ntheader) \ - ((PIMAGE_SECTION_HEADER)(ULONG_PTR)((const BYTE *)&((const IMAGE_NT_HEADERS *)(ntheader))->OptionalHeader + \ - ((const IMAGE_NT_HEADERS *)(ntheader))->FileHeader.SizeOfOptionalHeader)) + ((PIMAGE_SECTION_HEADER)((ULONG_PTR)&(ntheader)->OptionalHeader + (ntheader)->FileHeader.SizeOfOptionalHeader))
/* These defines are for the Characteristics bitfield. */ /* #define IMAGE_SCN_TYPE_REG 0x00000000 - Reserved */