Merge trunk head (r43756)
[reactos.git] / reactos / dll / ntdll / rtl / libsupp.c
index 8bae8c6..c10bb87 100644 (file)
 #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
  */
@@ -29,12 +34,14 @@ RtlWalkFrameChain(OUT PVOID *Callers,
     /* 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
@@ -162,7 +169,7 @@ RtlpFreeMemory(PVOID Mem,
 }
 
 
-#ifdef DBG
+#if DBG
 VOID FASTCALL
 CHECK_PAGED_CODE_RTL(char *file, int line)
 {
@@ -203,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
@@ -358,6 +385,62 @@ RtlpGetAtomEntry(PRTL_ATOM_TABLE AtomTable, ULONG Index)
    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