- Make the NDK compatible with the MSDDK again.
[reactos.git] / reactos / ntoskrnl / ldr / loader.c
index 7997747..07fd9b7 100644 (file)
@@ -89,18 +89,23 @@ LdrPEFixupImports ( PLDR_DATA_TABLE_ENTRY Module );
 /* FUNCTIONS *****************************************************************/
 
 VOID
+NTAPI
 LdrInitDebug ( PLOADER_MODULE Module, PWCH Name )
 {
 }
 
-VOID INIT_FUNCTION
+VOID
+INIT_FUNCTION
+NTAPI
 LdrInit1 ( VOID )
 {
     /* Hook for KDB on initialization of the loader. */
     KDB_LOADERINIT_HOOK(&NtoskrnlModuleObject, &HalModuleObject);
 }
 
-VOID INIT_FUNCTION
+VOID
+INIT_FUNCTION
+NTAPI
 LdrInitModuleManagement ( VOID )
 {
     PIMAGE_NT_HEADERS NtHeader;
@@ -138,6 +143,7 @@ LdrInitModuleManagement ( VOID )
 }
 
 NTSTATUS
+NTAPI
 LdrpLoadImage (
     PUNICODE_STRING DriverName,
     PVOID *ModuleBase,
@@ -161,8 +167,8 @@ LdrpLoadImage (
     if (ModuleBase)
         *ModuleBase = ModuleObject->DllBase;
 
-    //if (SectionPointer)
-    //    *SectionPointer = ModuleObject->
+    if (SectionPointer)
+        *SectionPointer = ModuleObject;
 
     if (EntryPoint)
         *EntryPoint = ModuleObject->EntryPoint;
@@ -175,6 +181,7 @@ LdrpLoadImage (
 
 
 NTSTATUS
+NTAPI
 LdrpUnloadImage ( PVOID ModuleBase )
 {
     return(STATUS_NOT_IMPLEMENTED);
@@ -182,6 +189,7 @@ LdrpUnloadImage ( PVOID ModuleBase )
 
 
 NTSTATUS
+NTAPI
 LdrpLoadAndCallImage ( PUNICODE_STRING ModuleName )
 {
     PDRIVER_INITIALIZE DriverEntry;
@@ -204,7 +212,7 @@ LdrpLoadAndCallImage ( PUNICODE_STRING ModuleName )
     DriverEntry = (PDRIVER_INITIALIZE)ModuleObject->EntryPoint;
 
     RtlZeroMemory(&DriverObject, sizeof(DriverObject));
-    DriverObject.DriverStart = ModuleObject->DllBase;
+//    DriverObject.DriverStart = ModuleObject->DllBase;
 
     Status = DriverEntry(&DriverObject, NULL);
     if (!NT_SUCCESS(Status))
@@ -217,6 +225,7 @@ LdrpLoadAndCallImage ( PUNICODE_STRING ModuleName )
 
 
 NTSTATUS
+NTAPI
 LdrLoadModule(
     PUNICODE_STRING Filename,
     PLDR_DATA_TABLE_ENTRY *ModuleObject )
@@ -249,7 +258,7 @@ LdrLoadModule(
     CHECKPOINT;
     if (!NT_SUCCESS(Status))
     {
-        CPRINT("Could not open module file: %wZ\n", Filename);
+        CPRINT("Could not open module file: %wZ (Status 0x%08lx)\n", Filename, Status);
         return(Status);
     }
     CHECKPOINT;
@@ -321,6 +330,7 @@ LdrLoadModule(
 
 
 NTSTATUS
+NTAPI
 LdrUnloadModule ( PLDR_DATA_TABLE_ENTRY ModuleObject )
 {
     KIRQL Irql;
@@ -365,6 +375,7 @@ LdrProcessModule(
 }
 
 NTSTATUS
+NTAPI
 LdrpQueryModuleInformation (
     PVOID Buffer,
     ULONG Size,
@@ -542,6 +553,7 @@ LdrpCompareModuleNames (
 }
 
 PLDR_DATA_TABLE_ENTRY
+NTAPI
 LdrGetModuleObject ( PUNICODE_STRING ModuleName )
 {
     PLDR_DATA_TABLE_ENTRY Module;
@@ -558,7 +570,7 @@ LdrGetModuleObject ( PUNICODE_STRING ModuleName )
         Module = CONTAINING_RECORD(Entry, LDR_DATA_TABLE_ENTRY, InLoadOrderModuleList);
 
         DPRINT("Comparing %wZ and %wZ\n",
-            &Module->BaseName,
+            &Module->BaseDllName,
             ModuleName);
 
         if (!LdrpCompareModuleNames(&Module->BaseDllName, ModuleName))
@@ -768,6 +780,7 @@ LdrPEProcessModule(
     }
 
     RtlCopyUnicodeString(&CreatedModuleObject->FullDllName, FileName);
+    CreatedModuleObject->FullDllName.Buffer[FileName->Length / sizeof(WCHAR)] = 0;
     LdrpBuildModuleBaseName(&CreatedModuleObject->BaseDllName,
         &CreatedModuleObject->FullDllName);
 
@@ -869,7 +882,9 @@ LdrPEProcessModule(
 }
 
 
-PVOID INIT_FUNCTION
+PVOID
+INIT_FUNCTION
+NTAPI
 LdrSafePEProcessModule (
     PVOID ModuleLoadBase,
     PVOID DriverBase,
@@ -1115,7 +1130,7 @@ LdrPEPerformRelocations (
 
             /*
             * Don't relocate within the relocation section itself.
-            * GCC/LD generates sometimes relocation records for the relecotion section.
+            * GCC/LD generates sometimes relocation records for the relocation section.
             * This is a bug in GCC/LD.
             */
             if ((ULONG_PTR)ShortPtr < (ULONG_PTR)RelocationDir ||
@@ -1241,11 +1256,10 @@ LdrPEGetExportByName (
     PDWORD * ExFunctions;
     PDWORD * ExNames;
     USHORT * ExOrdinals;
-    ULONG i;
     PVOID ExName;
     ULONG Ordinal;
     PVOID Function;
-    LONG minn, maxn;
+    LONG minn, maxn, mid, res;
     ULONG ExportDirSize;
 
     DPRINT("LdrPEGetExportByName %x %s %hu\n", BaseAddress, SymbolName, Hint);
@@ -1304,15 +1318,12 @@ LdrPEGetExportByName (
     }
 
     /*
-    * Try a binary search first
+    * Binary search
     */
     minn = 0;
     maxn = ExportDir->NumberOfNames - 1;
     while (minn <= maxn)
     {
-        LONG mid;
-        LONG res;
-
         mid = (minn + maxn) / 2;
 
         ExName = RVA(BaseAddress, ExNames[mid]);
@@ -1337,11 +1348,6 @@ LdrPEGetExportByName (
                 return Function;
             }
         }
-        else if (minn == maxn)
-        {
-            DPRINT("LdrPEGetExportByName(): binary search failed\n");
-            break;
-        }
         else if (res > 0)
         {
             maxn = mid - 1;
@@ -1352,31 +1358,7 @@ LdrPEGetExportByName (
         }
     }
 
-    /*
-    * Fall back on a linear search
-    */
-    DPRINT("LdrPEGetExportByName(): Falling back on a linear search of export table\n");
-    for (i = 0; i < ExportDir->NumberOfNames; i++)
-    {
-        ExName = RVA(BaseAddress, ExNames[i]);
-        if (strcmp(ExName, (PCHAR)SymbolName) == 0)
-        {
-            Ordinal = ExOrdinals[i];
-            Function = RVA(BaseAddress, ExFunctions[Ordinal]);
-            DPRINT("%x %x %x\n", Function, ExportDir, ExportDir + ExportDirSize);
-            if ((ULONG_PTR)Function >= (ULONG_PTR)ExportDir &&
-                (ULONG_PTR)Function < (ULONG_PTR)ExportDir + ExportDirSize)
-            {
-                DPRINT("Forward: %s\n", (PCHAR)Function);
-                Function = LdrPEFixupForward((PCHAR)Function);
-            }
-            if (Function == NULL)
-            {
-                break;
-            }
-            return Function;
-        }
-    }
+    ExName = RVA(BaseAddress, ExNames[mid]);
     DPRINT1("LdrPEGetExportByName(): failed to find %s\n",SymbolName);
     return (PVOID)NULL;
 }
@@ -1407,8 +1389,8 @@ LdrPEGetExportByOrdinal (
         ? RVA(BaseAddress, ExFunctions[Ordinal - ExportDir->Base] )
         : NULL;
 
-    if (((ULONG)Function >= (ULONG)ExportDir) &&
-        ((ULONG)Function < (ULONG)ExportDir + (ULONG)ExportDirSize))
+    if (((ULONG_PTR)Function >= (ULONG_PTR)ExportDir) &&
+        ((ULONG_PTR)Function < (ULONG_PTR)ExportDir + ExportDirSize))
     {
         DPRINT("Forward: %s\n", (PCHAR)Function);
         Function = LdrPEFixupForward((PCHAR)Function);
@@ -1482,13 +1464,14 @@ LdrPEFixupImports ( PLDR_DATA_TABLE_ENTRY Module )
     PCHAR ImportedName;
     PLDR_DATA_TABLE_ENTRY ImportedModule;
     NTSTATUS Status;
+    ULONG Size;
 
     /*  Process each import module  */
     ImportModuleDirectory = (PIMAGE_IMPORT_DESCRIPTOR)
         RtlImageDirectoryEntryToData(Module->DllBase,
         TRUE,
         IMAGE_DIRECTORY_ENTRY_IMPORT,
-        NULL);
+        &Size);
     DPRINT("Processeing import directory at %p\n", ImportModuleDirectory);
     while (ImportModuleDirectory->Name)
     {