Commit db3af1b2 authored by Alexandre Julliard's avatar Alexandre Julliard

ntdll: Consistently use the IMAGE_FIRST_SECTION helper macro.

parent af25f29c
......@@ -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))
......
......@@ -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;
......
......@@ -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++)
{
......
......@@ -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;
......
......@@ -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 */
......
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