From 2c6fae9c203ccd808b8bd0a4f92c8a0c63c60891 Mon Sep 17 00:00:00 2001 From: Eric Kohl Date: Sat, 18 Mar 2000 14:02:01 +0000 Subject: [PATCH] Added image functions Include pe.h in windows.h to avoid conflicts svn path=/trunk/; revision=1076 --- reactos/include/ddk/rtl.h | 34 +++++++- reactos/include/internal/ldr.h | 1 - reactos/include/pe.h | 23 ++++-- reactos/include/structs.h | 32 +------- reactos/include/windows.h | 3 + reactos/lib/ntdll/def/ntdll.def | 12 +-- reactos/lib/ntdll/def/ntdll.edf | 6 +- reactos/lib/ntdll/ldr/startup.c | 15 ++-- reactos/lib/ntdll/ldr/utils.c | 7 +- reactos/lib/ntdll/makefile | 4 +- reactos/lib/ntdll/rtl/image.c | 140 ++++++++++++++++++++++++++++++++ reactos/lib/ntdll/rtl/ppb.c | 7 +- reactos/lib/ntdll/rtl/process.c | 6 +- reactos/ntoskrnl/ldr/rtl.c | 4 +- reactos/ntoskrnl/ntoskrnl.def | 4 +- reactos/ntoskrnl/ntoskrnl.edf | 3 +- 16 files changed, 223 insertions(+), 78 deletions(-) create mode 100644 reactos/lib/ntdll/rtl/image.c diff --git a/reactos/include/ddk/rtl.h b/reactos/include/ddk/rtl.h index 97e8a6905a6..6bafa74bad7 100644 --- a/reactos/include/ddk/rtl.h +++ b/reactos/include/ddk/rtl.h @@ -1,4 +1,4 @@ -/* $Id: rtl.h,v 1.29 2000/03/08 01:52:52 ekohl Exp $ +/* $Id: rtl.h,v 1.30 2000/03/18 13:53:26 ekohl Exp $ * */ @@ -619,6 +619,38 @@ RtlGetProcessHeap ( VOID ); +PVOID +STDCALL +RtlImageDirectoryEntryToData ( + PVOID BaseAddress, + BOOLEAN bFlag, + ULONG Directory, + PULONG Size + ); + +PIMAGE_NT_HEADERS +STDCALL +RtlImageNtHeader ( + PVOID BaseAddress + ); + +PIMAGE_SECTION_HEADER +STDCALL +RtlImageRvaToSection ( + PIMAGE_NT_HEADERS NtHeader, + PVOID BaseAddress, + ULONG Rva + ); + +ULONG +STDCALL +RtlImageRvaToVa ( + PIMAGE_NT_HEADERS NtHeader, + PVOID BaseAddress, + ULONG Rva, + PIMAGE_SECTION_HEADER *SectionHeader + ); + VOID STDCALL RtlInitAnsiString ( diff --git a/reactos/include/internal/ldr.h b/reactos/include/internal/ldr.h index 5d30b305aac..d384732e479 100644 --- a/reactos/include/internal/ldr.h +++ b/reactos/include/internal/ldr.h @@ -17,7 +17,6 @@ VOID LdrInitModuleManagement(VOID); NTSTATUS LdrProcessDriver(PVOID ModuleLoadBase); NTSTATUS LdrpMapSystemDll(HANDLE ProcessHandle, PVOID* LdrStartupAddress); -PIMAGE_NT_HEADERS RtlImageNtHeader(PVOID BaseAddress); PVOID LdrpGetSystemDllEntryPoint(VOID); NTSTATUS LdrpMapImage(HANDLE ProcessHandle, HANDLE SectionHandle, diff --git a/reactos/include/pe.h b/reactos/include/pe.h index fe35b75345c..995821d1311 100644 --- a/reactos/include/pe.h +++ b/reactos/include/pe.h @@ -17,6 +17,13 @@ #define IMAGE_DOS_MAGIC 0x5a4d #define IMAGE_PE_MAGIC 0x00004550 +#define IMAGE_DOS_SIGNATURE 0x5a4d +#define IMAGE_OS2_SIGNATURE 0x454e +#define IMAGE_OS2_SIGNATURE_LE 0x454c +#define IMAGE_VXD_SIGNATURE 0x454c +#define IMAGE_NT_SIGNATURE 0x00004550 + + typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header WORD e_magic; // Magic number WORD e_cblp; // Bytes on last page of file @@ -334,17 +341,17 @@ typedef struct _IMAGE_RESOURCE_DATA_ENTRY { // an empty name, zero ID, and zero flags. typedef struct _IMAGE_MENU_HEADER{ - WORD wVersion; // Currently zero - WORD cbHeaderSize; // Also zero + WORD wVersion; // Currently zero + WORD cbHeaderSize; // Also zero } IMAGE_MENU_HEADER, *PIMAGE_MENU_HEADER; typedef struct _IMAGE_POPUP_MENU_ITEM{ - WORD fItemFlags; + WORD fItemFlags; WCHAR szItemText[1]; } IMAGE_POPUP_MENU_ITEM, *PIMAGE_POPUP_MENU_ITEM; typedef struct _IMAGE_NORMAL_MENU_ITEM{ - WORD fItemFlags; + WORD fItemFlags; WORD wMenuID; WCHAR szItemText[1]; } IMAGE_NORMAL_MENU_ITEM, *PIMAGE_NORMAL_MENU_ITEM; @@ -379,7 +386,7 @@ typedef struct _IMAGE_NORMAL_MENU_ITEM{ // entries will be present. typedef struct _IMAGE_DIALOG_BOX_HEADER1{ - DWORD IStyle; + DWORD IStyle; DWORD IExtendedStyle; // New for Windows NT WORD nControls; // Number of Controls WORD x; @@ -396,16 +403,16 @@ typedef struct _IMAGE_DIALOG_BOX_HEADER1{ typedef union _NAME_OR_ORDINAL{ // Name or Ordinal ID struct _ORD_ID{ WORD flgId; - WORD Id; + WORD Id; } ORD_ID; - WCHAR szName[1]; + WCHAR szName[1]; } NAME_OR_ORDINAL, *PNAME_OR_ORDINAL; // The data for each control starts on a DWORD boundary (which may require // some padding from the previous control), and its format is as follows: typedef struct _IMAGE_CONTROL_DATA{ - DWORD IStyle; + DWORD IStyle; DWORD IExtendedStyle; WORD x; WORD y; diff --git a/reactos/include/structs.h b/reactos/include/structs.h index d90d7523461..9908fc93cee 100644 --- a/reactos/include/structs.h +++ b/reactos/include/structs.h @@ -4555,37 +4555,7 @@ typedef struct } LOCALGROUP_INFO_0, *PLOCALGROUP_INFO_0, *LPLOCALGROUP_INFO_0; /* PE executable header. */ - -#ifndef WIN32_NO_PEHDR - -typedef struct -{ - WORD e_magic; /* Magic number, 0x5a4d */ - WORD e_cblp; /* Bytes on last page of file, 0x90 */ - WORD e_cp; /* Pages in file, 0x3 */ - WORD e_crlc; /* Relocations, 0x0 */ - WORD e_cparhdr; /* Size of header in paragraphs, 0x4 */ - WORD e_minalloc; /* Minimum extra paragraphs needed, 0x0 */ - WORD e_maxalloc; /* Maximum extra paragraphs needed, 0xFFFF */ - WORD e_ss; /* Initial (relative) SS value, 0x0 */ - WORD e_sp; /* Initial SP value, 0xb8 */ - WORD e_csum; /* Checksum, 0x0 */ - WORD e_ip; /* Initial IP value, 0x0 */ - WORD e_cs; /* Initial (relative) CS value, 0x0 */ - WORD e_lfarlc; /* File address of relocation table, 0x40 */ - WORD e_ovno; /* Overlay number, 0x0 */ - WORD e_res[4]; /* Reserved words, all 0x0 */ - WORD e_oemid; /* OEM identifier (for e_oeminfo), 0x0 */ - WORD e_oeminfo; /* OEM information; e_oemid specific, 0x0 */ - WORD e_res2[10]; /* Reserved words, all 0x0 */ - LONG e_lfanew; /* File address of new exe header, 0x80 */ - /* We leave out the next two fields, since they aren't in the - Windows header file. */ - /* DWORD dos_message[16]; * text which always follows dos header */ - /* DWORD nt_signature; * required NT signature, 0x4550 */ -} IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER; - -#endif /* WIN32_NO_PEHDR */ +/* Windows.h now includes pe.h to avoid conflicts! */ #ifdef __cplusplus } diff --git a/reactos/include/windows.h b/reactos/include/windows.h index 20752a4d244..7996aa633a2 100644 --- a/reactos/include/windows.h +++ b/reactos/include/windows.h @@ -58,6 +58,9 @@ #include #endif +/* WIN32 PE file format */ +#include + #endif /* ! defined (RC_INVOKED) */ /* WIN32 error codes */ diff --git a/reactos/lib/ntdll/def/ntdll.def b/reactos/lib/ntdll/def/ntdll.def index 6864fa8a300..d0ebd7e329e 100644 --- a/reactos/lib/ntdll/def/ntdll.def +++ b/reactos/lib/ntdll/def/ntdll.def @@ -1,4 +1,4 @@ -; $Id: ntdll.def,v 1.46 2000/03/12 01:17:23 ekohl Exp $ +; $Id: ntdll.def,v 1.47 2000/03/18 13:58:33 ekohl Exp $ ; ; ReactOS Operating System ; @@ -388,13 +388,13 @@ RtlGetProcessHeap@0 ;RtlGetUserInfoHeap ;RtlGuidToPropertySetName RtlIdentifierAuthoritySid@4 -;RtlImageDirectoryEntryToData -;RtlImageNtHeader -;RtlImageRvaToSection -;RtlImageRvaToVa +RtlImageDirectoryEntryToData@16 +RtlImageNtHeader@4 +RtlImageRvaToSection@12 +RtlImageRvaToVa@16 ;RtlImpersonateSelf RtlInitAnsiString@8 -;RtlInitcodePageTable +;RtlInitCodePageTable ;RtlInitNlsTables RtlInitString@8 RtlInitUnicodeString@8 diff --git a/reactos/lib/ntdll/def/ntdll.edf b/reactos/lib/ntdll/def/ntdll.edf index 5e4680d984f..8c987ae6a16 100644 --- a/reactos/lib/ntdll/def/ntdll.edf +++ b/reactos/lib/ntdll/def/ntdll.edf @@ -1,4 +1,4 @@ -; $Id: ntdll.edf,v 1.35 2000/03/12 01:17:23 ekohl Exp $ +; $Id: ntdll.edf,v 1.36 2000/03/18 13:58:33 ekohl Exp $ ; ; ReactOS Operating System ; @@ -303,6 +303,10 @@ RtlGetLongestNtPathLength=RtlGetLongestNtPathLength@0 RtlGetOwnerSecurityDescriptor=RtlGetOwnerSecurityDescriptor@12 RtlGetProcessHeap=RtlGetProcessHeap@0 RtlIdentifierAuthoritySid=RtlIdentifierAuthoritySid@4 +RtlImageDirectoryEntryToData=RtlImageDirectoryEntryToData@16 +RtlImageNtHeader=RtlImageNtHeader@4 +RtlImageRvaToSection=RtlImageRvaToSection@12 +RtlImageRvaToVa=RtlImageRvaToVa@16 RtlInitAnsiString=RtlInitAnsiString@8 RtlInitString=RtlInitString@8 RtlInitUnicodeString=RtlInitUnicodeString@8 diff --git a/reactos/lib/ntdll/ldr/startup.c b/reactos/lib/ntdll/ldr/startup.c index 1daddd410fb..8389116958f 100644 --- a/reactos/lib/ntdll/ldr/startup.c +++ b/reactos/lib/ntdll/ldr/startup.c @@ -1,4 +1,4 @@ -/* $Id: startup.c,v 1.20 2000/02/27 02:05:53 ekohl Exp $ +/* $Id: startup.c,v 1.21 2000/03/18 13:57:02 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -11,13 +11,8 @@ /* INCLUDES *****************************************************************/ #include -#define WIN32_NO_STATUS -#define WIN32_NO_PEHDR -#include #include -#include -#include -#include +#include #include #include #include @@ -30,6 +25,8 @@ DLL LdrDllListHead; extern unsigned int _image_base__; +ULONG NtGlobalFlag = 0; + /* FUNCTIONS *****************************************************************/ @@ -61,7 +58,9 @@ VOID LdrStartup(VOID) DPRINT("ImageBase is null\n"); for(;;); } - + + NtGlobalFlag = Peb->NtGlobalFlag; + /* If MZ header exists */ PEDosHeader = (PIMAGE_DOS_HEADER) ImageBase; DPRINT("PEDosHeader %x\n", PEDosHeader); diff --git a/reactos/lib/ntdll/ldr/utils.c b/reactos/lib/ntdll/ldr/utils.c index 99d49eee8c1..a8e631dd40e 100644 --- a/reactos/lib/ntdll/ldr/utils.c +++ b/reactos/lib/ntdll/ldr/utils.c @@ -1,4 +1,4 @@ -/* $Id: utils.c,v 1.24 2000/02/13 16:05:16 dwelch Exp $ +/* $Id: utils.c,v 1.25 2000/03/18 13:57:02 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -11,11 +11,8 @@ /* INCLUDES *****************************************************************/ #include -#define WIN32_NO_STATUS -#define WIN32_NO_PEHDR -#include #include -#include +#include #include #include #include diff --git a/reactos/lib/ntdll/makefile b/reactos/lib/ntdll/makefile index 46e72f087ba..9356c118ae4 100644 --- a/reactos/lib/ntdll/makefile +++ b/reactos/lib/ntdll/makefile @@ -1,4 +1,4 @@ -# $Id: makefile,v 1.41 2000/03/12 01:16:41 ekohl Exp $ +# $Id: makefile,v 1.42 2000/03/18 13:57:43 ekohl Exp $ # # ReactOS Operating System # @@ -30,7 +30,7 @@ DBG_OBJECTS = dbg/brkpoint.o dbg/print.o RTL_OBJECTS = rtl/critical.o rtl/error.o rtl/heap.o rtl/largeint.o \ rtl/math.o rtl/mem.o rtl/nls.o rtl/process.o rtl/sd.o \ rtl/thread.o rtl/unicode.o rtl/env.o rtl/path.o rtl/ppb.o \ - rtl/bitmap.o rtl/time.o rtl/acl.o rtl/sid.o + rtl/bitmap.o rtl/time.o rtl/acl.o rtl/sid.o rtl/image.o STDIO_OBJECTS = stdio/sprintf.o stdio/swprintf.o diff --git a/reactos/lib/ntdll/rtl/image.c b/reactos/lib/ntdll/rtl/image.c new file mode 100644 index 00000000000..f78b78e336a --- /dev/null +++ b/reactos/lib/ntdll/rtl/image.c @@ -0,0 +1,140 @@ +/* $Id: image.c,v 1.1 2000/03/18 13:56:01 ekohl Exp $ + * + * COPYRIGHT: See COPYING in the top level directory + * PROJECT: ReactOS kernel + * FILE: lib/ntdll/rtl/image.c + * PURPOSE: Image handling functions + * UPDATE HISTORY: + * 17/03/2000 Created by Eric Kohl + */ + +#include +#include + + +/* FUNCTIONS ****************************************************************/ + +PIMAGE_NT_HEADERS +STDCALL +RtlImageNtHeader ( + PVOID BaseAddress + ) +{ + PIMAGE_NT_HEADERS NtHeader; + PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)BaseAddress; + + if (DosHeader && DosHeader->e_magic == IMAGE_DOS_SIGNATURE) + { + NtHeader = (PIMAGE_NT_HEADERS)(BaseAddress + DosHeader->e_lfanew); + if (NtHeader->Signature == IMAGE_NT_SIGNATURE) + return NtHeader; + } + return NULL; +} + + +PVOID +STDCALL +RtlImageDirectoryEntryToData ( + PVOID BaseAddress, + BOOLEAN bFlag, + ULONG Directory, + PULONG Size + ) +{ + PIMAGE_NT_HEADERS NtHeader; + PIMAGE_SECTION_HEADER SectionHeader; + ULONG Va; + ULONG Count; + + NtHeader = RtlImageNtHeader (BaseAddress); + if (NtHeader == NULL) + return NULL; + + if (Directory >= NtHeader->OptionalHeader.NumberOfRvaAndSizes) + return NULL; + + Va = NtHeader->OptionalHeader.DataDirectory[Directory].VirtualAddress; + if (Va == 0) + return NULL; + + if (Size) + *Size = NtHeader->OptionalHeader.DataDirectory[Directory].Size; + + if (bFlag) + return (PVOID)(BaseAddress + Va); + + /* image mapped as ordinary file, we must find raw pointer */ + SectionHeader = (PIMAGE_SECTION_HEADER)(NtHeader + 1); + Count = NtHeader->FileHeader.NumberOfSections; + while (Count--) + { + if (SectionHeader->VirtualAddress == Va) + return (PVOID)(BaseAddress + SectionHeader->PointerToRawData); + SectionHeader++; + } + + return NULL; +} + + +PIMAGE_SECTION_HEADER +STDCALL +RtlImageRvaToSection ( + PIMAGE_NT_HEADERS NtHeader, + PVOID BaseAddress, + ULONG Rva + ) +{ + PIMAGE_SECTION_HEADER Section; + ULONG Va; + ULONG Count; + + Count = NtHeader->FileHeader.NumberOfSections; + Section = (PIMAGE_SECTION_HEADER)((ULONG)&NtHeader->OptionalHeader + + NtHeader->FileHeader.SizeOfOptionalHeader); + while (Count) + { + Va = Section->VirtualAddress; + if ((Va <= Rva) && + (Rva < Va + Section->SizeOfRawData)) + return Section; + Section++; + } + return NULL; +} + + +ULONG +STDCALL +RtlImageRvaToVa ( + PIMAGE_NT_HEADERS NtHeader, + PVOID BaseAddress, + ULONG Rva, + PIMAGE_SECTION_HEADER *SectionHeader + ) +{ + PIMAGE_SECTION_HEADER Section = NULL; + + if (SectionHeader) + Section = *SectionHeader; + + if (Section == NULL || + Rva < Section->VirtualAddress || + Rva >= Section->VirtualAddress + Section->SizeOfRawData) + { + Section = RtlImageRvaToSection (NtHeader, BaseAddress, Rva); + if (Section == NULL) + return 0; + + if (SectionHeader) + *SectionHeader = Section; + } + + return (ULONG)(BaseAddress + + Rva + + Section->PointerToRawData - + Section->VirtualAddress); +} + +/* EOF */ diff --git a/reactos/lib/ntdll/rtl/ppb.c b/reactos/lib/ntdll/rtl/ppb.c index 5f2b4cd4a9b..d54c4adc03b 100644 --- a/reactos/lib/ntdll/rtl/ppb.c +++ b/reactos/lib/ntdll/rtl/ppb.c @@ -1,4 +1,4 @@ -/* $Id: ppb.c,v 1.4 2000/02/19 19:34:49 ekohl Exp $ +/* $Id: ppb.c,v 1.5 2000/03/18 13:56:01 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -11,12 +11,7 @@ /* INCLUDES ****************************************************************/ -#define WIN32_NO_PEHDR #include -#include -#include -#include -#include #include #include #include diff --git a/reactos/lib/ntdll/rtl/process.c b/reactos/lib/ntdll/rtl/process.c index bf17e1e7594..7a8d2013ace 100644 --- a/reactos/lib/ntdll/rtl/process.c +++ b/reactos/lib/ntdll/rtl/process.c @@ -1,4 +1,4 @@ -/* $Id: process.c,v 1.16 2000/02/25 23:58:03 ekohl Exp $ +/* $Id: process.c,v 1.17 2000/03/18 13:56:01 ekohl Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -11,11 +11,7 @@ /* INCLUDES ****************************************************************/ -#define WIN32_NO_PEHDR #include -#include -#include -#include #include #include #include diff --git a/reactos/ntoskrnl/ldr/rtl.c b/reactos/ntoskrnl/ldr/rtl.c index 034359ffdf5..a960808972a 100644 --- a/reactos/ntoskrnl/ldr/rtl.c +++ b/reactos/ntoskrnl/ldr/rtl.c @@ -26,7 +26,9 @@ /* FUNCTIONS ****************************************************************/ -PIMAGE_NT_HEADERS RtlImageNtHeader(PVOID BaseAddress) +PIMAGE_NT_HEADERS +STDCALL +RtlImageNtHeader(PVOID BaseAddress) { PIMAGE_DOS_HEADER DosHeader; PIMAGE_NT_HEADERS NTHeaders; diff --git a/reactos/ntoskrnl/ntoskrnl.def b/reactos/ntoskrnl/ntoskrnl.def index d77073839e2..508b852badb 100644 --- a/reactos/ntoskrnl/ntoskrnl.def +++ b/reactos/ntoskrnl/ntoskrnl.def @@ -1,4 +1,4 @@ -; $Id: ntoskrnl.def,v 1.58 2000/03/17 21:02:56 jfilby Exp $ +; $Id: ntoskrnl.def,v 1.59 2000/03/18 14:02:01 ekohl Exp $ ; ; reactos/ntoskrnl/ntoskrnl.def ; @@ -386,7 +386,7 @@ RtlGetDefaultCodePage@8 ;RtlGetElementGenericTable RtlGetGroupSecurityDescriptor@12 RtlGetOwnerSecurityDescriptor@12 -;RtlImageNtHeader +RtlImageNtHeader@4 RtlInitAnsiString@8 ;RtlInitCodePageTable RtlInitString@8 diff --git a/reactos/ntoskrnl/ntoskrnl.edf b/reactos/ntoskrnl/ntoskrnl.edf index 042613af923..d84df716d9d 100644 --- a/reactos/ntoskrnl/ntoskrnl.edf +++ b/reactos/ntoskrnl/ntoskrnl.edf @@ -1,4 +1,4 @@ -; $Id: ntoskrnl.edf,v 1.45 2000/03/17 21:02:56 jfilby Exp $ +; $Id: ntoskrnl.edf,v 1.46 2000/03/18 14:02:01 ekohl Exp $ ; ; reactos/ntoskrnl/ntoskrnl.def ; @@ -346,6 +346,7 @@ RtlGetDaclSecurityDescriptor=RtlGetDaclSecurityDescriptor@16 RtlGetDefaultCodePage=RtlGetDefaultCodePage@8 RtlGetGroupSecurityDescriptor=RtlGetGroupSecurityDescriptor@12 RtlGetOwnerSecurityDescriptor=RtlGetOwnerSecurityDescriptor@12 +RtlImageNtHeader=RtlImageNtHeader@4 RtlInitAnsiString=RtlInitAnsiString@8 RtlInitString=RtlInitString@8 RtlInitUnicodeString=RtlInitUnicodeString@8