Merge trunk head (r43756)
[reactos.git] / reactos / dll / ntdll / rtl / libsupp.c
index 82342be..c10bb87 100644 (file)
 #define NDEBUG
 #include <debug.h>
 
+SIZE_T RtlpAllocDeallocQueryBufferSize = PAGE_SIZE;
+PTEB LdrpTopLevelDllBeingLoadedTeb = NULL;
+#define IMAGE_DOS_MAGIC 0x5a4d
+#define IMAGE_PE_MAGIC 0x00004550
+
 /* FUNCTIONS ***************************************************************/
 
+#ifndef _M_AMD64
+// FIXME: Why "Not implemented"???
+/*
+ * @implemented
+ */
+ULONG
+NTAPI
+RtlWalkFrameChain(OUT PVOID *Callers,
+                  IN ULONG Count,
+                  IN ULONG Flags)
+{
+    /* Not implemented for user-mode */
+    return 0;
+}
+#endif
+
 BOOLEAN
 NTAPI
-RtlpCheckForActiveDebugger(BOOLEAN Type)
+RtlpCheckForActiveDebugger(VOID)
 {
-    return (NtCurrentPeb()->BeingDebugged);
+    /* Return the flag in the PEB */
+    return NtCurrentPeb()->BeingDebugged;
 }
 
 BOOLEAN
@@ -48,9 +70,12 @@ RtlpGetMode()
    return UserMode;
 }
 
+/*
+ * @implemented
+ */
 PPEB
 NTAPI
-RtlpCurrentPeb(VOID)
+RtlGetCurrentPeb(VOID)
 {
     return NtCurrentPeb();
 }
@@ -124,7 +149,7 @@ RtlpAllocateMemory(UINT Bytes,
                    ULONG Tag)
 {
     UNREFERENCED_PARAMETER(Tag);
-    
+
     return RtlAllocateHeap(RtlGetProcessHeap(),
                            0,
                            Bytes);
@@ -137,14 +162,14 @@ RtlpFreeMemory(PVOID Mem,
                ULONG Tag)
 {
     UNREFERENCED_PARAMETER(Tag);
-    
+
     RtlFreeHeap(RtlGetProcessHeap(),
                 0,
                 Mem);
 }
 
 
-#ifdef DBG
+#if DBG
 VOID FASTCALL
 CHECK_PAGED_CODE_RTL(char *file, int line)
 {
@@ -185,6 +210,26 @@ RtlpCaptureStackLimits(IN ULONG_PTR Ebp,
     return TRUE;
 }
 
+#ifdef _AMD64_
+VOID
+NTAPI
+RtlpGetStackLimits(
+    OUT PULONG_PTR LowLimit,
+    OUT PULONG_PTR HighLimit)
+{
+    *LowLimit = (ULONG_PTR)NtCurrentTeb()->Tib.StackLimit;
+    *HighLimit = (ULONG_PTR)NtCurrentTeb()->Tib.StackBase;
+    return;
+}
+#endif
+
+BOOLEAN
+NTAPI
+RtlIsThreadWithinLoaderCallout(VOID)
+{
+    return LdrpTopLevelDllBeingLoadedTeb == NtCurrentTeb();
+}
+
 /* RTL Atom Tables ************************************************************/
 
 typedef struct _RTL_ATOM_HANDLE
@@ -277,7 +322,7 @@ VOID
 RtlpFreeAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
 {
    PRTL_HANDLE_TABLE_ENTRY RtlHandleEntry;
-   
+
    if (RtlIsValidIndexHandle(&AtomTable->RtlHandleTable,
                              (ULONG)Entry->HandleIndex,
                              &RtlHandleEntry))
@@ -292,7 +337,7 @@ RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
 {
    ULONG HandleIndex;
    PRTL_HANDLE_TABLE_ENTRY RtlHandle;
-   
+
    RtlHandle = RtlAllocateHandle(&AtomTable->RtlHandleTable,
                                  &HandleIndex);
    if (RtlHandle != NULL)
@@ -314,7 +359,7 @@ RtlpCreateAtomHandle(PRTL_ATOM_TABLE AtomTable, PRTL_ATOM_TABLE_ENTRY Entry)
       {
          /* set the valid flag, otherwise RtlFreeHandle will fail! */
          AtomHandle->Handle.Flags = RTL_HANDLE_VALID;
-         
+
          RtlFreeHandle(&AtomTable->RtlHandleTable,
                        RtlHandle);
       }
@@ -327,7 +372,7 @@ PRTL_ATOM_TABLE_ENTRY
 RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable, ULONG Index)
 {
    PRTL_HANDLE_TABLE_ENTRY RtlHandle;
-   
+
    if (RtlIsValidIndexHandle(&AtomTable->RtlHandleTable,
                              Index,
                              &RtlHandle))
@@ -336,10 +381,66 @@ RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable, ULONG Index)
 
       return AtomHandle->AtomEntry;
    }
-   
+
    return NULL;
 }
 
+PVOID
+NTAPI
+RtlpLookupModuleBase(
+    PVOID Address)
+{
+    NTSTATUS Status;
+    MEMORY_BASIC_INFORMATION MemoryInformation;
+    ULONG_PTR Base, Limit;
+    PIMAGE_DOS_HEADER DosHeader;
+    PIMAGE_NT_HEADERS NtHeader;
+
+    Status = NtQueryVirtualMemory(NtCurrentProcess(),
+                                  Address,
+                                  MemoryBasicInformation,
+                                  &MemoryInformation,
+                                  sizeof(MEMORY_BASIC_INFORMATION),
+                                  NULL);
+    if (!NT_SUCCESS(Status))
+    {
+        return NULL;
+    }
+
+    /* FIXME: remove these checks? */
+    Base = (ULONG_PTR)MemoryInformation.BaseAddress;
+    Limit = Base + MemoryInformation.RegionSize;
+    if ( ((ULONG_PTR)Address < Base) ||
+         ((ULONG_PTR)Address >= Limit) )
+    {
+        /* WTF? */
+        return NULL;
+    }
+
+    /* Check if we got the right kind of memory */
+    if ( (MemoryInformation.State != MEM_COMMIT) ||
+         (MemoryInformation.Type != MEM_IMAGE) )
+    {
+        return NULL;
+    }
+
+    /* Check DOS magic */
+    DosHeader = MemoryInformation.AllocationBase;
+    if (DosHeader->e_magic != IMAGE_DOS_MAGIC)
+    {
+        return NULL;
+    }
+
+    /* Check NT header */
+    NtHeader = (PVOID)((ULONG_PTR)DosHeader + DosHeader->e_lfanew);
+    if (NtHeader->Signature != IMAGE_PE_MAGIC)
+    {
+        return NULL;
+    }
+
+    return MemoryInformation.AllocationBase;
+}
+
 
 /*
  * Ldr Resource support code