Commit in reactos on ELF_support
include/ntos/zw.h+2-21.35.2.1 -> 1.35.2.2
include/pe.h+6-51.19.32.1 -> 1.19.32.2
include/reactos/exeformat.h+22-61.1.2.2 -> 1.1.2.3
lib/kernel32/process/create.c+3-31.89 -> 1.89.2.1
lib/rtl/random.c+1-31.3 -> 1.3.8.1
ntoskrnl/ex/sysinfo.c+1-11.59.2.1 -> 1.59.2.2
           /time.c+57-11.24.2.1 -> 1.24.2.2
ntoskrnl/include/internal/mm.h+33-141.93.2.1 -> 1.93.2.2
ntoskrnl/mm/pe.c+8-81.1.2.2 -> 1.1.2.3
           /section.c+113-961.166.2.2 -> 1.166.2.3
+246-139
10 modified files
 - include/ntos/zw.h, lib/kernel32/process/create.c: fixed SECTION_IMAGE_INFORMATION (EntryPoint is an offset)
 - include/pe.h, lib/rtl/random.c: header hell strikes again and again and again
 - include/reactos/exeformat.h: finishing touches
 - ntoskrnl/ex/time.c: merge with HEAD (just enough to get it to compile)
 - ntoskrnl/include/internal/mm.h, ntoskrnl/mm/section.c: Hyperion vs bitrot, Hyperion wins
 - ntoskrnl/mm/pe.c: cosmetic changes

reactos/include/ntos
zw.h 1.35.2.1 -> 1.35.2.2
diff -u -r1.35.2.1 -r1.35.2.2
--- zw.h	8 Dec 2004 21:56:57 -0000	1.35.2.1
+++ zw.h	13 Dec 2004 05:55:31 -0000	1.35.2.2
@@ -1,5 +1,5 @@
 
-/* $Id: zw.h,v 1.35.2.1 2004/12/08 21:56:57 hyperion Exp $
+/* $Id: zw.h,v 1.35.2.2 2004/12/13 05:55:31 hyperion Exp $
  *
  * COPYRIGHT:        See COPYING in the top level directory
  * PROJECT:          ReactOS kernel
@@ -6480,7 +6480,7 @@
 
 typedef struct _SECTION_IMAGE_INFORMATION
 {
-  PVOID EntryPoint;
+  ULONG EntryPoint;
   ULONG Unknown1;
   ULONG StackReserve;
   ULONG StackCommit;

reactos/include
pe.h 1.19.32.1 -> 1.19.32.2
diff -u -r1.19.32.1 -r1.19.32.2
--- pe.h	9 Dec 2004 19:31:26 -0000	1.19.32.1
+++ pe.h	13 Dec 2004 05:55:31 -0000	1.19.32.2
@@ -545,6 +545,12 @@
 } IMAGE_RESOURCE_DIRECTORY, *PIMAGE_RESOURCE_DIRECTORY;
 */
 
+#ifdef _WIN64
+#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC
+#else
+#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC
+#endif
+
 #endif /* !__USE_W32API */
 
 #define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))
@@ -609,11 +615,6 @@
 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
 #define IMAGE_ROM_OPTIONAL_HDR_MAGIC 0x107
-#ifdef _WIN64
-#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR64_MAGIC
-#else
-#define IMAGE_NT_OPTIONAL_HDR_MAGIC IMAGE_NT_OPTIONAL_HDR32_MAGIC
-#endif
 #define IMAGE_SEPARATE_DEBUG_SIGNATURE 0x4944
 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
 #define IMAGE_SIZEOF_ROM_OPTIONAL_HEADER 56

reactos/include/reactos
exeformat.h 1.1.2.2 -> 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- exeformat.h	9 Dec 2004 19:31:26 -0000	1.1.2.2
+++ exeformat.h	13 Dec 2004 05:55:32 -0000	1.1.2.3
@@ -7,23 +7,39 @@
 #define EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED       (1 << 0)
 #define EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP   (1 << 1)
 #define EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED (1 << 2)
-/*
- * CALLBACKS
- */
-typedef NTSTATUS (NTAPI * PEXEFMT_LOADER_READ_FILE)
+
+#define EXEFMT_LOAD_ASSUME_SEGMENTS_OK \
+ ( \
+  EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED | \
+  EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP | \
+  EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED \
+ )
+
+typedef NTSTATUS (NTAPI * PEXEFMT_CB_READ_FILE)
 (
- IN struct _FILE_OBJECT * FileObject,
+ IN PFILE_OBJECT FileObject,
  OUT PVOID Buffer,
  IN ULONG BufferSize,
  IN PLARGE_INTEGER Offset,
  OUT PULONG ReadSize
 );
 
-typedef PMM_SECTION_SEGMENT (NTAPI * PEXEFMT_LOADER_ALLOCATE_SEGMENTS)
+typedef PMM_SECTION_SEGMENT (NTAPI * PEXEFMT_CB_ALLOCATE_SEGMENTS)
 (
  IN ULONG NrSegments
 );
 
+typedef NTSTATUS (NTAPI * PEXEFMT_LOADER)
+(
+ IN CONST VOID * FileHeader,
+ IN SIZE_T FileHeaderSize,
+ IN PFILE_OBJECT File,
+ OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
+ OUT PULONG Flags,
+ IN PEXEFMT_CB_READ_FILE ReadFileCb,
+ IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
+);
+
 /*
  * STATUS CONSTANTS
  */

reactos/lib/kernel32/process
create.c 1.89 -> 1.89.2.1
diff -u -r1.89 -r1.89.2.1
--- create.c	21 Nov 2004 21:09:42 -0000	1.89
+++ create.c	13 Dec 2004 05:55:32 -0000	1.89.2.1
@@ -1,4 +1,4 @@
-/* $Id: create.c,v 1.89 2004/11/21 21:09:42 weiden Exp $
+/* $Id: create.c,v 1.89.2.1 2004/12/13 05:55:32 hyperion Exp $
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS system libraries
@@ -1373,11 +1373,11 @@
     * Create the thread for the kernel
     */
    DPRINT("Creating thread for process (EntryPoint = 0x%.08x)\n",
-    ImageBaseAddress + (ULONG)Sii.EntryPoint);
+    ImageBaseAddress + Sii.EntryPoint);
    hThread =  KlCreateFirstThread(hProcess,
 				  lpThreadAttributes,
           &Sii,
-          ImageBaseAddress + (ULONG)Sii.EntryPoint,
+          ImageBaseAddress + Sii.EntryPoint,
 				  dwCreationFlags,
 				  &lpProcessInformation->dwThreadId);
    if (hThread == INVALID_HANDLE_VALUE)

reactos/lib/rtl
random.c 1.3 -> 1.3.8.1
diff -u -r1.3 -r1.3.8.1
--- random.c	7 Aug 2004 19:13:25 -0000	1.3
+++ random.c	13 Dec 2004 05:55:32 -0000	1.3.8.1
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: random.c,v 1.3 2004/08/07 19:13:25 ion Exp $
+/* $Id: random.c,v 1.3.8.1 2004/12/13 05:55:32 hyperion Exp $
  *
  * COPYRIGHT:         See COPYING in the top level directory
  * PROJECT:           ReactOS kernel
@@ -31,8 +31,6 @@
 #define NDEBUG
 #include <debug.h>
 
-#define MAXLONG (0x7fffffff)
-
 static ULONG SavedValue[128] =
    {
       0x4c8bc0aa, 0x4c022957, 0x2232827a, 0x2f1e7626,  /*   0 */

reactos/ntoskrnl/ex
sysinfo.c 1.59.2.1 -> 1.59.2.2
diff -u -r1.59.2.1 -r1.59.2.2
--- sysinfo.c	8 Dec 2004 21:57:32 -0000	1.59.2.1
+++ sysinfo.c	13 Dec 2004 05:55:32 -0000	1.59.2.2
@@ -1,4 +1,4 @@
-/* $Id: sysinfo.c,v 1.59.2.1 2004/12/08 21:57:32 hyperion Exp $
+/* $Id: sysinfo.c,v 1.59.2.2 2004/12/13 05:55:32 hyperion Exp $
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS kernel

reactos/ntoskrnl/ex
time.c 1.24.2.1 -> 1.24.2.2
diff -u -r1.24.2.1 -r1.24.2.2
--- time.c	8 Dec 2004 21:57:32 -0000	1.24.2.1
+++ time.c	13 Dec 2004 05:55:32 -0000	1.24.2.2
@@ -1,4 +1,4 @@
-/* $Id: time.c,v 1.24.2.1 2004/12/08 21:57:32 hyperion Exp $
+/* $Id: time.c,v 1.24.2.2 2004/12/13 05:55:32 hyperion Exp $
  *
  * COPYRIGHT:       See COPYING in the top level directory
  * PROJECT:         ReactOS kernel
@@ -76,6 +76,62 @@
 }
 
 
+NTSTATUS
+ExpSetTimeZoneInformation(PTIME_ZONE_INFORMATION TimeZoneInformation)
+{
+  LARGE_INTEGER LocalTime;
+  LARGE_INTEGER SystemTime;
+  TIME_FIELDS TimeFields;
+
+  DPRINT("ExpSetTimeZoneInformation() called\n");
+
+  DPRINT("Old time zone bias: %d minutes\n",
+	 ExpTimeZoneInfo.Bias);
+  DPRINT("Old time zone standard bias: %d minutes\n",
+	 ExpTimeZoneInfo.StandardBias);
+
+  DPRINT("New time zone bias: %d minutes\n",
+	 TimeZoneInformation->Bias);
+  DPRINT("New time zone standard bias: %d minutes\n",
+	 TimeZoneInformation->StandardBias);
+
+  /* Get the local time */
+  HalQueryRealTimeClock(&TimeFields);
+  RtlTimeFieldsToTime(&TimeFields,
+		      &LocalTime);
+
+  /* FIXME: Calculate transition dates */
+
+  ExpTimeZoneBias.QuadPart =
+    ((LONGLONG)(TimeZoneInformation->Bias + TimeZoneInformation->StandardBias)) * TICKSPERMINUTE;
+  ExpTimeZoneId = TIME_ZONE_ID_STANDARD;
+
+  memcpy(&ExpTimeZoneInfo,
+	 TimeZoneInformation,
+	 sizeof(TIME_ZONE_INFORMATION));
+
+  /* Set the new time zone information */
+  SharedUserData->TimeZoneBias.High1Time = ExpTimeZoneBias.u.HighPart;
+  SharedUserData->TimeZoneBias.High2Time = ExpTimeZoneBias.u.HighPart;
+  SharedUserData->TimeZoneBias.LowPart = ExpTimeZoneBias.u.LowPart;
+  SharedUserData->TimeZoneId = ExpTimeZoneId;
+
+  DPRINT("New time zone bias: %I64d minutes\n",
+	 ExpTimeZoneBias.QuadPart / TICKSPERMINUTE);
+
+  /* Calculate the new system time */
+  ExLocalTimeToSystemTime(&LocalTime,
+			  &SystemTime);
+
+  /* Set the new system time */
+  KiSetSystemTime(&SystemTime);
+
+  DPRINT("ExpSetTimeZoneInformation() done\n");
+
+  return STATUS_SUCCESS;
+}
+
+
 /*
  * FUNCTION: Sets the system time.
  * PARAMETERS:

reactos/ntoskrnl/include/internal
mm.h 1.93.2.1 -> 1.93.2.2
diff -u -r1.93.2.1 -r1.93.2.2
--- mm.h	8 Dec 2004 20:01:40 -0000	1.93.2.1
+++ mm.h	13 Dec 2004 05:55:32 -0000	1.93.2.2
@@ -71,6 +71,26 @@
 						 PAGE_NOACCESS | \
 						 PAGE_NOCACHE)
 
+#define PAGE_IS_READABLE (PAGE_READONLY | \
+                          PAGE_READWRITE | \
+                          PAGE_WRITECOPY | \
+                          PAGE_EXECUTE_READ | \
+                          PAGE_EXECUTE_READWRITE | \
+                          PAGE_EXECUTE_WRITECOPY)
+
+#define PAGE_IS_WRITABLE (PAGE_READWRITE | \
+                          PAGE_WRITECOPY | \
+                          PAGE_EXECUTE_READWRITE | \
+                          PAGE_EXECUTE_WRITECOPY)
+
+#define PAGE_IS_EXECUTABLE (PAGE_EXECUTE | \
+                            PAGE_EXECUTE_READ | \
+                            PAGE_EXECUTE_READWRITE | \
+                            PAGE_EXECUTE_WRITECOPY)
+
+#define PAGE_IS_WRITECOPY (PAGE_WRITECOPY | \
+                           PAGE_EXECUTE_WRITECOPY)
+
 typedef struct
 {
   ULONG Entry[NR_SECTION_PAGE_ENTRIES];
@@ -89,32 +109,31 @@
 typedef struct _MM_SECTION_SEGMENT
 {
   ULONG FileOffset;
-  ULONG Protection;
-  ULONG Length;
   ULONG RawLength;
+  ULONG_PTR VirtualAddress;
+  SIZE_T Length;
+  ULONG Protection;
+  LONG ReferenceCount;
+  ULONG Flags;
+  ULONG Characteristics;
   FAST_MUTEX Lock;
-  LONG ReferenceCount;/* WRONG TYPE */
   SECTION_PAGE_DIRECTORY PageDirectory;
-  /*ULONG Flags;*/ /* USELESS */
-  ULONG VirtualAddress;/* WRONG TYPE */
-  ULONG Characteristics;
-  /*BOOLEAN WriteCopy;*/ /* USELESS */
 } MM_SECTION_SEGMENT, *PMM_SECTION_SEGMENT;
 
 typedef struct _MM_IMAGE_SECTION_OBJECT
 {
-  ULONG_PTR ImageBase;/* WRONG TYPE */
-  ULONG EntryPoint;/* WRONG TYPE */
-  ULONG_PTR StackReserve;/* WRONG TYPE */
-  ULONG_PTR StackCommit;/* WRONG TYPE */
+  ULONG_PTR ImageBase;
+  ULONG_PTR StackReserve;
+  ULONG_PTR StackCommit;
+  ULONG EntryPoint;
   ULONG Subsystem;
   ULONG ImageCharacteristics;
-  USHORT MinorSubsystemVersion;/* WRONG TYPE */
-  USHORT MajorSubsystemVersion;/* WRONG TYPE */
+  USHORT MinorSubsystemVersion;
+  USHORT MajorSubsystemVersion;
   USHORT Machine;
   BOOLEAN Executable;
   ULONG NrSegments;
-  PMM_SECTION_SEGMENT Segments;/* WRONG TYPE */
+  PMM_SECTION_SEGMENT Segments;
 } MM_IMAGE_SECTION_OBJECT, *PMM_IMAGE_SECTION_OBJECT;
 
 typedef struct _SECTION_OBJECT

reactos/ntoskrnl/mm
pe.c 1.1.2.2 -> 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- pe.c	9 Dec 2004 19:31:26 -0000	1.1.2.2
+++ pe.c	13 Dec 2004 05:55:32 -0000	1.1.2.3
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: pe.c,v 1.1.2.2 2004/12/09 19:31:26 hyperion Exp $
+/* $Id: pe.c,v 1.1.2.3 2004/12/13 05:55:32 hyperion Exp $
  *
  * PROJECT:         ReactOS kernel
  * FILE:            ntoskrnl/mm/pe.c
@@ -223,8 +223,8 @@
  IN PFILE_OBJECT File,
  OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
  OUT PULONG Flags,
- IN PEXEFMT_LOADER_READ_FILE ReadFileFunc,
- IN PEXEFMT_LOADER_ALLOCATE_SEGMENTS AllocateSegmentsFunc
+ IN PEXEFMT_CB_READ_FILE ReadFileCb,
+ IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
 )
 {
  NTSTATUS nStatus;
@@ -251,8 +251,8 @@
  ASSERT(FileHeaderSize > 0);
  ASSERT(File);
  ASSERT(ImageSectionObject);
- ASSERT(ReadFileFunc);
- ASSERT(AllocateSegmentsFunc);
+ ASSERT(ReadFileCb);
+ ASSERT(AllocateSegmentsCb);
  
  ASSERT(Intsafe_CanOffsetPointer(FileHeader, FileHeaderSize));
 
@@ -327,7 +327,7 @@
   lnOffset.QuadPart = pidhDosHeader->e_lfanew;
 
   /* read the header from the file */
-  nStatus = ReadFileFunc
+  nStatus = ReadFileCb
   (
    File,
    pBuffer,
@@ -637,7 +637,7 @@
   lnOffset.QuadPart = cbSectionHeadersOffset;
 
   /* read the header from the file */
-  nStatus = ReadFileFunc
+  nStatus = ReadFileCb
   (
    File,
    pBuffer,
@@ -659,7 +659,7 @@
  /* SEGMENTS */
  /* allocate the segments */
  nStatus = STATUS_INSUFFICIENT_RESOURCES;
- ImageSectionObject->Segments = AllocateSegmentsFunc(ImageSectionObject->NrSegments);
+ ImageSectionObject->Segments = AllocateSegmentsCb(ImageSectionObject->NrSegments);
 
  if(ImageSectionObject->Segments == NULL)
   goto l_Return;

reactos/ntoskrnl/mm
section.c 1.166.2.2 -> 1.166.2.3
diff -u -r1.166.2.2 -r1.166.2.3
--- section.c	8 Dec 2004 20:01:41 -0000	1.166.2.2
+++ section.c	13 Dec 2004 05:55:32 -0000	1.166.2.3
@@ -16,7 +16,7 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
-/* $Id: section.c,v 1.166.2.2 2004/12/08 20:01:41 hyperion Exp $
+/* $Id: section.c,v 1.166.2.3 2004/12/13 05:55:32 hyperion Exp $
  *
  * PROJECT:         ReactOS kernel
  * FILE:            ntoskrnl/mm/section.c
@@ -29,9 +29,12 @@
 /* INCLUDES *****************************************************************/
 
 #include <ntoskrnl.h>
+
 #define NDEBUG
 #include <internal/debug.h>
 
+#include <reactos/exeformat.h>
+
 /* TYPES *********************************************************************/
 
 typedef struct
@@ -122,14 +125,15 @@
    KeSetEvent(&File->Lock, IO_NO_INCREMENT, FALSE);
 }
 
+/* TODO: use this instead of ZwReadFile for all of our paging needs */
 static
 NTSTATUS
 NTAPI
 MmspPageRead(IN PFILE_OBJECT FileObject,
              OUT PVOID Buffer,
-             IN SIZE_T BufferSize,
-             IN LARGE_INTEGER Offset,
-             OUT SIZE_T * ReadSize)
+             IN ULONG BufferSize,
+             IN PLARGE_INTEGER Offset,
+             OUT PULONG ReadSize)
 {
    NTSTATUS Status;
    KEVENT Event;
@@ -153,52 +157,6 @@
    return Iosb.Status;
 }
 
-static
-NTSTATUS
-NTAPI
-MmspQueryFileSize(IN PFILE_OBJECT FileObject,
-                  OUT PLARGE_INTEGER FileSize)
-{
-   PDEVICE_OBJECT DeviceObject;
-   KEVENT Event;
-   PIRP Irp;
-   IO_STATUS_BLOCK Iosb;
-   PIO_STACK_LOCATION StackPtr;
-   FILE_END_OF_FILE_INFORMATION Eof;
-
-   DeviceObject = IoGetRelatedDeviceObject(FileObject);
-   KeInitializeEvent(&Event, NotificationEvent, FALSE);
-
-   Irp = IoBuildSynchronousFsdRequest(IRP_MJ_QUERY_INFORMATION,
-                                      DeviceObject,
-                                      &Eof,
-                                      sizeof(Eof),
-                                      NULL,
-                                      &Event,
-                                      &Iosb);
-
-   if (Irp == NULL)
-      return (STATUS_INSUFFICIENT_RESOURCES);
-
-   StackPtr = IoGetNextIrpStackLocation(Irp);
-   StackPtr->FileObject = FileObject;
-   StackPtr->Parameters.QueryFile.Length = sizeof(Eof);
-   StackPtr->Parameters.QueryFile.FileInformationClass = FileEndOfFileInformation;
-
-   if (IoCallDriver(DeviceObject, Irp) == STATUS_PENDING)
-      KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
-
-   if (NT_SUCCESS(Iosb.Status))
-   {
-      if (!RTL_CONTAINS_FIELD(&Eof, Iosb.Information, EndOfFile))
-         return STATUS_UNSUCCESSFUL;
-
-      *FileSize = Eof.EndOfFile;
-   }
-
-   return Iosb.Status;
-}
-
 VOID
 MmFreePageTablesSectionSegment(PMM_SECTION_SEGMENT Segment)
 {
@@ -238,6 +196,7 @@
          }
          MmFreePageTablesSectionSegment(&SectionSegments[i]);
       }
+      ExFreePool(ImageSectionObject->Segments);
       ExFreePool(ImageSectionObject);
       FileObject->SectionObjectPointer->ImageSectionObject = NULL;
    }
@@ -730,7 +689,7 @@
    /*
     * Check if this page needs to be mapped COW
     */
-   if ((Segment->WriteCopy || MemoryArea->Data.SectionData.WriteCopyView) &&
+   if ((Segment->Protection & PAGE_IS_WRITECOPY || MemoryArea->Data.SectionData.WriteCopyView) &&
        (Region->Protect == PAGE_READWRITE ||
        Region->Protect == PAGE_EXECUTE_READWRITE))
    {
@@ -1248,7 +1207,7 @@
    /*
     * Check if we are doing COW
     */
-   if (!((Segment->WriteCopy || MemoryArea->Data.SectionData.WriteCopyView) &&
+   if (!((Segment->Protection & PAGE_IS_WRITECOPY || MemoryArea->Data.SectionData.WriteCopyView) &&
          (Region->Protect == PAGE_READWRITE ||
           Region->Protect == PAGE_EXECUTE_READWRITE)))
    {
@@ -1911,7 +1870,7 @@
    MemoryArea = MmOpenMemoryAreaByAddress(AddressSpace, BaseAddress);
    Segment = MemoryArea->Data.SectionData.Segment;
 
-   if ((Segment->WriteCopy || MemoryArea->Data.SectionData.WriteCopyView) &&
+   if ((Segment->Protection & PAGE_IS_WRITECOPY || MemoryArea->Data.SectionData.WriteCopyView) &&
          (NewProtect == PAGE_READWRITE || NewProtect == PAGE_EXECUTE_READWRITE))
    {
       DoCOW = TRUE;
@@ -2108,7 +2067,7 @@
          {
             MmLockSectionSegment(&SectionSegments[i]);
          }
-         RefCount = InterlockedDecrement((LONG *)&SectionSegments[i].ReferenceCount);
+         RefCount = InterlockedDecrement(&SectionSegments[i].ReferenceCount);
          if (SectionSegments[i].Characteristics & IMAGE_SECTION_CHAR_SHARED)
          {
             if (RefCount == 0)
@@ -2137,7 +2096,7 @@
       }
       else
       {
-         InterlockedDecrement((LONG *)&Section->Segment->ReferenceCount);
+         InterlockedDecrement(&Section->Segment->ReferenceCount);
       }
    }
    if (Section->FileObject != NULL)
@@ -2310,11 +2269,9 @@
    ExInitializeFastMutex(&Segment->Lock);
    Segment->FileOffset = 0;
    Segment->Protection = SectionPageProtection;
-   Segment->Attributes = AllocationAttributes;
    Segment->RawLength = MaximumSize.u.LowPart;
    Segment->Length = PAGE_ROUND_UP(MaximumSize.u.LowPart);
    Segment->Flags = MM_PAGEFILE_SEGMENT;
-   Segment->WriteCopy = FALSE;
    RtlZeroMemory(&Segment->PageDirectory, sizeof(SECTION_PAGE_DIRECTORY));
    Segment->VirtualAddress = 0;
    Segment->Characteristics = 0;
@@ -2508,7 +2465,7 @@
                                       TAG_MM_SECTION_SEGMENT);
       if (Segment == NULL)
       {
-         KeSetEvent((PVOID)&FileObject->Lock, IO_NO_INCREMENT, FALSE);
+         MmspReleaseFileLock(FileObject);
          ObDereferenceObject(Section);
          ObDereferenceObject(FileObject);
          return(STATUS_NO_MEMORY);
@@ -2524,10 +2481,8 @@
 
       Segment->FileOffset = 0;
       Segment->Protection = SectionPageProtection;
-      Segment->Attributes = 0;
       Segment->Flags = MM_DATAFILE_SEGMENT;
       Segment->Characteristics = 0;
-      Segment->WriteCopy = FALSE;
       if (AllocationAttributes & SEC_RESERVE)
       {
          Segment->Length = Segment->RawLength = 0;
@@ -2537,7 +2492,7 @@
          Segment->RawLength = MaximumSize.u.LowPart;
          Segment->Length = PAGE_ROUND_UP(Segment->RawLength);
       }
-      Segment->VirtualAddress = NULL;
+      Segment->VirtualAddress = 0;
       RtlZeroMemory(&Segment->PageDirectory, sizeof(SECTION_PAGE_DIRECTORY));
    }
    else
@@ -2550,7 +2505,7 @@
          (PMM_SECTION_SEGMENT)FileObject->SectionObjectPointer->
          DataSectionObject;
       Section->Segment = Segment;
-      InterlockedIncrement((PLONG)&Segment->ReferenceCount);
+      InterlockedIncrement(&Segment->ReferenceCount);
       MmLockSectionSegment(Segment);
 
       if (MaximumSize.u.LowPart > Segment->RawLength &&
@@ -2564,11 +2519,43 @@
    Section->FileObject = FileObject;
    Section->MaximumSize = MaximumSize;
    CcRosReferenceCache(FileObject);
-   KeSetEvent((PVOID)&FileObject->Lock, IO_NO_INCREMENT, FALSE);
+   MmspReleaseFileLock(FileObject);
    *SectionObject = Section;
    return(STATUS_SUCCESS);
 }
 
+/*
+ TODO: not that great (declaring loaders statically, having to declare all of
+ them, having to keep them extern, etc.), will fix in the future
+*/
+extern NTSTATUS NTAPI PeFmtCreateSection
+(
+ IN CONST VOID * FileHeader,
+ IN SIZE_T FileHeaderSize,
+ IN PFILE_OBJECT File,
+ OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
+ OUT PULONG Flags,
+ IN PEXEFMT_CB_READ_FILE ReadFileCb,
+ IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
+);
+
+/* TODO: this is a standard DDK/PSDK macro */
+#ifndef RTL_NUMBER_OF
+#define RTL_NUMBER_OF(ARR_) (sizeof(ARR_) / sizeof((ARR_)[0]))
+#endif
+
+static PEXEFMT_LOADER ExeFmtpLoaders[] =
+{
+ PeFmtCreateSection
+};
+
+static PMM_SECTION_SEGMENT NTAPI MmspAllocateSegments(IN ULONG NrSegments)
+{
+ return ExAllocatePoolWithTag(NonPagedPool,
+                              sizeof(MM_SECTION_SEGMENT) * NrSegments,
+                              TAG_MM_SECTION_SEGMENT);
+}
+
 NTSTATUS
 MmCreateImageSection(PSECTION_OBJECT *SectionObject,
                      ACCESS_MASK DesiredAccess,
@@ -2581,6 +2568,7 @@
    NTSTATUS Status;
    PSECTION_OBJECT Section;
    PMM_IMAGE_SECTION_OBJECT ImageSectionObject;
+   ULONG i;
 
    /*
     * Specifying a maximum size is meaningless for an image section
@@ -2601,8 +2589,8 @@
                             0,
                             (PVOID*)&Section);
 
-   if (!NT_SUCCESS(nStatus))
-      return nStatus;
+   if (!NT_SUCCESS(Status))
+      return Status;
 
    /*
     * Initialize it
@@ -2621,10 +2609,10 @@
 
    if (!NT_SUCCESS(Status) || FileObject->SectionObjectPointer->ImageSectionObject == NULL)
    {
+      LARGE_INTEGER Offset;
       PVOID FileHeader;
-      SIZE_T FileHeaderSize;
-      LARGE_INTEGER FileSize;
-      ULONG i;
+      ULONG FileHeaderSize;
+      ULONG Flags;
 
       /*
        * Allocate the image section object
@@ -2646,22 +2634,8 @@
        */
       Status = MmspWaitForFileLock(FileObject);
 
-      if (!NT_SUCCESS(nStatus))
-         goto l_FreeImageSection;
-      
-      /*
-       * Query the file's size
-       */
-      Status = MmspQueryFileSize(FileObject, &FileSize);
-
       if (!NT_SUCCESS(Status))
-         goto l_UnlockFile;
-
-      if (FileSize.QuadPart < FileHeaderSize)
-      {
-         Status = STATUS_UNSUCCESSFUL;
-         goto l_UnlockFile;
-      }
+         goto l_FreeImageSection;
 
       /*
        * Read the beginning of the file (2 pages). Should be enough to contain
@@ -2692,29 +2666,65 @@
          goto l_UnlockFile;
       }
 
-      /* Just in case */
-      FileSize.QuadPart = RtlRosMax(FileSize.QuadPart, FileHeaderSize);
+      Flags = 0;
+
+      /*
+       * Look for a loader that can handle this executable
+       */
+      for (i = 0; i < RTL_NUMBER_OF(ExeFmtpLoaders); ++ i)
+      {
+         ImageSectionObject->Segments = NULL;
+
+         Status = ExeFmtpLoaders[i](FileHeader,
+                                    FileHeaderSize,
+                                    FileObject,
+                                    ImageSectionObject,
+                                    &Flags,
+                                    MmspPageRead,
+                                    MmspAllocateSegments);
+
+         if (!NT_SUCCESS(Status))
+         {
+            if (ImageSectionObject->Segments)
+               ExFreePool(ImageSectionObject->Segments);
+         }
+
+         if (Status != STATUS_ROS_EXEFMT_UNKNOWN_FORMAT)
+            break;
+      }
 
-      /* FIXME */
+      /*
+       * No loader handled the format
+       */
+      if (Status == STATUS_ROS_EXEFMT_UNKNOWN_FORMAT)
+         Status = STATUS_INVALID_IMAGE_FORMAT;
 
       ExFreePool(FileHeader);
 
       if (!NT_SUCCESS(Status))
          goto l_UnlockFile;
 
+      /*
+       * And now the fun part: fixing the segments
+       */
+      if (!(Flags & EXEFMT_LOAD_ASSUME_SEGMENTS_OK))
+      {
+      }
+
       if (0 != InterlockedCompareExchange((PLONG)&FileObject->SectionObjectPointer->ImageSectionObject,
                                           (LONG)ImageSectionObject, 0))
       {
          /*
           * An other thread has initialized the some image in the background
           */
+         ExFreePool(ImageSectionObject->Segments);
          ExFreePool(ImageSectionObject);
          ImageSectionObject = FileObject->SectionObjectPointer->ImageSectionObject;
          Section->ImageSection = ImageSectionObject;
 
          for (i = 0; i < ImageSectionObject->NrSegments; i++)
          {
-            InterlockedIncrement((LONG *)&ImageSectionObject->Segments[i].ReferenceCount);
+            InterlockedIncrement(&ImageSectionObject->Segments[i].ReferenceCount);
          }
       }
 
@@ -2726,6 +2736,7 @@
                       return Status;
 
 l_Success:
+      (void)0;
    }
    else
    {
@@ -2747,15 +2758,21 @@
        */
       for (i = 0; i < ImageSectionObject->NrSegments; i++)
       {
-         InterlockedIncrement((LONG *)&ImageSectionObject->Segments[i].ReferenceCount);
+         InterlockedIncrement(&ImageSectionObject->Segments[i].ReferenceCount);
       }
 
+      Status = STATUS_SUCCESS;
    }
 
    CcRosReferenceCache(FileObject);
    MmspReleaseFileLock(FileObject);
    *SectionObject = Section;
-   return(STATUS_SUCCESS);
+
+   /*
+    * It's important that Status, and not STATUS_SUCCESS, is returned, because
+    * Status may be one of the STATUS_ROS_EXEFMT_LOADED_FORMAT constants
+    */
+   return Status;
 }
 
 /*
@@ -3223,7 +3240,7 @@
          {
             if (Segment == &SectionSegments[i])
             {
-               ImageBaseAddress = (char*)BaseAddress - (ULONG_PTR)SectionSegments[i].VirtualAddress;
+               ImageBaseAddress = (char*)BaseAddress - SectionSegments[i].VirtualAddress;
                break;
             }
          }
@@ -3238,7 +3255,7 @@
          if (!(SectionSegments[i].Characteristics & IMAGE_SECTION_NOLOAD))
          {
             PVOID SBaseAddress = (PVOID)
-                                 ((char*)ImageBaseAddress + (ULONG_PTR)SectionSegments[i].VirtualAddress);
+                                 ((char*)ImageBaseAddress + SectionSegments[i].VirtualAddress);
 
             Status = MmUnmapViewOfSegment(AddressSpace, SBaseAddress);
          }
@@ -3366,7 +3383,7 @@
             }
             else
             {
-               Sbi->BaseAddress = Section->Segment->VirtualAddress;
+               Sbi->BaseAddress = (PVOID)Section->Segment->VirtualAddress;
                Sbi->Size.QuadPart = Section->Segment->Length;
             }
 
@@ -3396,8 +3413,8 @@
                Sii->StackReserve = ImageSectionObject->StackReserve;
                Sii->StackCommit = ImageSectionObject->StackCommit;
                Sii->Subsystem = ImageSectionObject->Subsystem;
-               Sii->MinorSubsystemVersion = (USHORT)ImageSectionObject->MinorSubsystemVersion;
-               Sii->MajorSubsystemVersion = (USHORT)ImageSectionObject->MajorSubsystemVersion;
+               Sii->MinorSubsystemVersion = ImageSectionObject->MinorSubsystemVersion;
+               Sii->MajorSubsystemVersion = ImageSectionObject->MajorSubsystemVersion;
                Sii->Characteristics = ImageSectionObject->ImageCharacteristics;
                Sii->ImageNumber = ImageSectionObject->Machine;
                Sii->Executable = ImageSectionObject->Executable;
@@ -3634,7 +3651,7 @@
       ImageBase = *BaseAddress;
       if (ImageBase == NULL)
       {
-         ImageBase = ImageSectionObject->ImageBase;
+         ImageBase = (PVOID)ImageSectionObject->ImageBase;
       }
 
       ImageSize = 0;
@@ -3673,7 +3690,7 @@
          if (!(SectionSegments[i].Characteristics & IMAGE_SECTION_NOLOAD))
          {
             PVOID SBaseAddress = (PVOID)
-                                 ((char*)ImageBase + (ULONG_PTR)SectionSegments[i].VirtualAddress);
+                                 ((char*)ImageBase + SectionSegments[i].VirtualAddress);
             MmLockSectionSegment(&SectionSegments[i]);
             Status = MmMapViewOfSegment(Process,
                                         AddressSpace,
CVSspam 0.2.8