[PSDK][NTOS:KD64] Rename GCC_ULONG64 to ULPTR64 to self-document the fact it stores a pointer as a 64-bit quantity.

But the underlying GCC stupidity is still there (15 years later).

However, enable it only in 32-bit GCC builds, not in 64-bits nor with MSVC.
See commit b9cd3f2d9 (r25845) for some details.

GCC is indeed still incapable of casting 32-bit pointers up to 64-bits,
when static-initializing arrays (**outside** a function) without emitting
the error:

  "error: initializer element is not constant"

(which might somehow indicate it actually tries to generate executable
code for casting the pointers, instead of doing it at compile-time).

Going down the rabbit hole, other stupidities show up:

Our PVOID64 type and the related POINTER_64 (in 32-bit archs), or the
PVOID32 and POINTER_32 (in 64-bit archs), are all silently broken in
GCC builds, because the pointer size attributes __ptr64 and __ptr32,
which are originally MSVC-specific, are defined to nothing in _mingw.h.
(And similarly for the __uptr and __sptr sign-extension attributes.)

Clang and other sane ompilers has since then implemented those (enabled
with -fms-extensions), but not GCC. The closest thing that could exist
for GCC is to do:

  #define __ptr64 __attribute__((mode(DI)))

in order to get a 64-bit-sized pointer type with

  typedef void* __ptr64 PVOID64;

but even this does not work, with the error:

  "error: invalid pointer mode 'DI'"
This commit is contained in:
Hermès Bélusca-Maïto 2022-11-23 16:46:35 +01:00
parent 1c0950b557
commit de81021bab
No known key found for this signature in database
GPG key ID: 3B2539C65E7B93D0
2 changed files with 182 additions and 170 deletions

View file

@ -518,11 +518,17 @@ DBGKD_GET_VERSION64 KdVersionBlock =
C_ASSERT(sizeof(KDDEBUGGER_DATA64) >= 0x318);
#endif
/* Minimal hackery for GCC, see commit b9cd3f2d9 (r25845) */
#if defined(__GNUC__) && defined(_M_IX86)
#define PtrToUL64(x) ((ULPTR64)(ULONG_PTR)(x))
#else
#define PtrToUL64(x) ((ULPTR64)(x))
#endif
KDDEBUGGER_DATA64 KdDebuggerDataBlock =
{
{{0}},
0,
{(ULONG_PTR)RtlpBreakWithStatusInstruction},
PtrToUL64(RtlpBreakWithStatusInstruction),
0,
FIELD_OFFSET(KTHREAD, CallbackStack),
#if defined(_M_ARM) || defined(_M_AMD64)
@ -533,82 +539,82 @@ KDDEBUGGER_DATA64 KdDebuggerDataBlock =
FIELD_OFFSET(KCALLOUT_FRAME, CBSTACK_FRAME_POINTER),
#endif
FALSE,
{(ULONG_PTR)KiCallUserMode},
PtrToUL64(KiCallUserMode),
0,
{(ULONG_PTR)&PsLoadedModuleList},
{(ULONG_PTR)&PsActiveProcessHead},
{(ULONG_PTR)&PspCidTable},
{(ULONG_PTR)&ExpSystemResourcesList},
{(ULONG_PTR)ExpPagedPoolDescriptor},
{(ULONG_PTR)&ExpNumberOfPagedPools},
{(ULONG_PTR)&KeTimeIncrement},
{(ULONG_PTR)&KeBugcheckCallbackListHead},
{(ULONG_PTR)KiBugCheckData},
{(ULONG_PTR)&IopErrorLogListHead},
{(ULONG_PTR)&ObpRootDirectoryObject},
{(ULONG_PTR)&ObpTypeObjectType},
{(ULONG_PTR)&MmSystemCacheStart},
{(ULONG_PTR)&MmSystemCacheEnd},
{(ULONG_PTR)&MmSystemCacheWs},
{(ULONG_PTR)&MmPfnDatabase},
{(ULONG_PTR)MmSystemPtesStart},
{(ULONG_PTR)MmSystemPtesEnd},
{(ULONG_PTR)&MmSubsectionBase},
{(ULONG_PTR)&MmNumberOfPagingFiles},
{(ULONG_PTR)&MmLowestPhysicalPage},
{(ULONG_PTR)&MmHighestPhysicalPage},
{(ULONG_PTR)&MmNumberOfPhysicalPages},
{(ULONG_PTR)&MmMaximumNonPagedPoolInBytes},
{(ULONG_PTR)&MmNonPagedSystemStart},
{(ULONG_PTR)&MmNonPagedPoolStart},
{(ULONG_PTR)&MmNonPagedPoolEnd},
{(ULONG_PTR)&MmPagedPoolStart},
{(ULONG_PTR)&MmPagedPoolEnd},
{(ULONG_PTR)&MmPagedPoolInfo},
PtrToUL64(&PsLoadedModuleList),
PtrToUL64(&PsActiveProcessHead),
PtrToUL64(&PspCidTable),
PtrToUL64(&ExpSystemResourcesList),
PtrToUL64(ExpPagedPoolDescriptor),
PtrToUL64(&ExpNumberOfPagedPools),
PtrToUL64(&KeTimeIncrement),
PtrToUL64(&KeBugcheckCallbackListHead),
PtrToUL64(KiBugCheckData),
PtrToUL64(&IopErrorLogListHead),
PtrToUL64(&ObpRootDirectoryObject),
PtrToUL64(&ObpTypeObjectType),
PtrToUL64(&MmSystemCacheStart),
PtrToUL64(&MmSystemCacheEnd),
PtrToUL64(&MmSystemCacheWs),
PtrToUL64(&MmPfnDatabase),
PtrToUL64(MmSystemPtesStart),
PtrToUL64(MmSystemPtesEnd),
PtrToUL64(&MmSubsectionBase),
PtrToUL64(&MmNumberOfPagingFiles),
PtrToUL64(&MmLowestPhysicalPage),
PtrToUL64(&MmHighestPhysicalPage),
PtrToUL64(&MmNumberOfPhysicalPages),
PtrToUL64(&MmMaximumNonPagedPoolInBytes),
PtrToUL64(&MmNonPagedSystemStart),
PtrToUL64(&MmNonPagedPoolStart),
PtrToUL64(&MmNonPagedPoolEnd),
PtrToUL64(&MmPagedPoolStart),
PtrToUL64(&MmPagedPoolEnd),
PtrToUL64(&MmPagedPoolInfo),
PAGE_SIZE,
{(ULONG_PTR)&MmSizeOfPagedPoolInBytes},
{(ULONG_PTR)&MmTotalCommitLimit},
{(ULONG_PTR)&MmTotalCommittedPages},
{(ULONG_PTR)&MmSharedCommit},
{(ULONG_PTR)&MmDriverCommit},
{(ULONG_PTR)&MmProcessCommit},
{(ULONG_PTR)&MmPagedPoolCommit},
{0},
{(ULONG_PTR)&MmZeroedPageListHead},
{(ULONG_PTR)&MmFreePageListHead},
{(ULONG_PTR)&MmStandbyPageListHead},
{(ULONG_PTR)&MmModifiedPageListHead},
{(ULONG_PTR)&MmModifiedNoWritePageListHead},
{(ULONG_PTR)&MmAvailablePages},
{(ULONG_PTR)&MmResidentAvailablePages},
{(ULONG_PTR)&PoolTrackTable},
{(ULONG_PTR)&NonPagedPoolDescriptor},
{(ULONG_PTR)&MmHighestUserAddress},
{(ULONG_PTR)&MmSystemRangeStart},
{(ULONG_PTR)&MmUserProbeAddress},
{(ULONG_PTR)KdPrintDefaultCircularBuffer},
{(ULONG_PTR)KdPrintDefaultCircularBuffer + 1},
{(ULONG_PTR)&KdPrintWritePointer},
{(ULONG_PTR)&KdPrintRolloverCount},
{(ULONG_PTR)&MmLoadedUserImageList},
{(ULONG_PTR)&NtBuildLab},
{0},
{(ULONG_PTR)KiProcessorBlock},
{(ULONG_PTR)&MmUnloadedDrivers},
{(ULONG_PTR)&MmLastUnloadedDrivers},
{(ULONG_PTR)&MmTriageActionTaken},
{(ULONG_PTR)&MmSpecialPoolTag},
{(ULONG_PTR)&KernelVerifier},
{(ULONG_PTR)&MmVerifierData},
{(ULONG_PTR)&MmAllocatedNonPagedPool},
{(ULONG_PTR)&MmPeakCommitment},
{(ULONG_PTR)&MmtotalCommitLimitMaximum},
{(ULONG_PTR)&CmNtCSDVersion},
{(ULONG_PTR)&MmPhysicalMemoryBlock},
{(ULONG_PTR)&MmSessionBase},
{(ULONG_PTR)&MmSessionSize},
{0},
{0},
PtrToUL64(&MmSizeOfPagedPoolInBytes),
PtrToUL64(&MmTotalCommitLimit),
PtrToUL64(&MmTotalCommittedPages),
PtrToUL64(&MmSharedCommit),
PtrToUL64(&MmDriverCommit),
PtrToUL64(&MmProcessCommit),
PtrToUL64(&MmPagedPoolCommit),
PtrToUL64(0),
PtrToUL64(&MmZeroedPageListHead),
PtrToUL64(&MmFreePageListHead),
PtrToUL64(&MmStandbyPageListHead),
PtrToUL64(&MmModifiedPageListHead),
PtrToUL64(&MmModifiedNoWritePageListHead),
PtrToUL64(&MmAvailablePages),
PtrToUL64(&MmResidentAvailablePages),
PtrToUL64(&PoolTrackTable),
PtrToUL64(&NonPagedPoolDescriptor),
PtrToUL64(&MmHighestUserAddress),
PtrToUL64(&MmSystemRangeStart),
PtrToUL64(&MmUserProbeAddress),
PtrToUL64(KdPrintDefaultCircularBuffer),
PtrToUL64(KdPrintDefaultCircularBuffer + 1),
PtrToUL64(&KdPrintWritePointer),
PtrToUL64(&KdPrintRolloverCount),
PtrToUL64(&MmLoadedUserImageList),
PtrToUL64(&NtBuildLab),
PtrToUL64(0),
PtrToUL64(KiProcessorBlock),
PtrToUL64(&MmUnloadedDrivers),
PtrToUL64(&MmLastUnloadedDrivers),
PtrToUL64(&MmTriageActionTaken),
PtrToUL64(&MmSpecialPoolTag),
PtrToUL64(&KernelVerifier),
PtrToUL64(&MmVerifierData),
PtrToUL64(&MmAllocatedNonPagedPool),
PtrToUL64(&MmPeakCommitment),
PtrToUL64(&MmtotalCommitLimitMaximum),
PtrToUL64(&CmNtCSDVersion),
PtrToUL64(&MmPhysicalMemoryBlock),
PtrToUL64(&MmSessionBase),
PtrToUL64(&MmSessionSize),
PtrToUL64(0),
PtrToUL64(0),
FIELD_OFFSET(KTHREAD, NextProcessor),
FIELD_OFFSET(KTHREAD, Teb),
FIELD_OFFSET(KTHREAD, KernelStack),
@ -630,9 +636,9 @@ KDDEBUGGER_DATA64 KdDebuggerDataBlock =
FIELD_OFFSET(KPRCB, ProcessorState.ContextFrame),
FIELD_OFFSET(KPRCB, Number),
sizeof(ETHREAD),
{(ULONG_PTR)KdPrintDefaultCircularBuffer},
{(ULONG_PTR)&KdPrintBufferSize},
{(ULONG_PTR)&KeLoaderBlock},
PtrToUL64(&KdPrintDefaultCircularBuffer),
PtrToUL64(&KdPrintBufferSize),
PtrToUL64(&KeLoaderBlock),
sizeof(KPCR),
KPCR_SELF_PCR_OFFSET,
KPCR_CURRENT_PRCB_OFFSET,
@ -693,8 +699,8 @@ KDDEBUGGER_DATA64 KdDebuggerDataBlock =
0,
0,
#endif
{(ULONG_PTR)&IopNumTriageDumpDataBlocks},
{(ULONG_PTR)IopTriageDumpDataBlocks},
PtrToUL64(&IopNumTriageDumpDataBlocks),
PtrToUL64(IopTriageDumpDataBlocks),
#if (NTDDI_VERSION >= NTDDI_LONGHORN)
#error KdDebuggerDataBlock requires other fields for this NT version!

View file

@ -174,110 +174,116 @@ typedef struct _DBGKD_DEBUG_DATA_HEADER64
ULONG Size;
} DBGKD_DEBUG_DATA_HEADER64, *PDBGKD_DEBUG_DATA_HEADER64;
typedef union _GCC_ULONG64
/* Self-documenting type: stores a pointer as a 64-bit quantity */
#if defined(__GNUC__) && defined(_M_IX86) // See commit b9cd3f2d9 (r25845)
typedef union _ULPTR64
{
ULONG_PTR Pointer;
ULONG64 RealPointer;
} GCC_ULONG64, *PGCC_ULONG64;
ULONG_PTR ptr;
ULONG64 ptr64;
} ULPTR64;
#else
// #define ULPTR64 PVOID64
#define ULPTR64 ULONG64
#endif
typedef struct _KDDEBUGGER_DATA64
{
DBGKD_DEBUG_DATA_HEADER64 Header;
ULONG64 KernBase;
GCC_ULONG64 BreakpointWithStatus;
ULPTR64 BreakpointWithStatus;
ULONG64 SavedContext;
USHORT ThCallbackStack;
USHORT NextCallback;
USHORT FramePointer;
USHORT PaeEnabled:1;
GCC_ULONG64 KiCallUserMode;
ULPTR64 KiCallUserMode;
ULONG64 KeUserCallbackDispatcher;
GCC_ULONG64 PsLoadedModuleList;
GCC_ULONG64 PsActiveProcessHead;
GCC_ULONG64 PspCidTable;
GCC_ULONG64 ExpSystemResourcesList;
GCC_ULONG64 ExpPagedPoolDescriptor;
GCC_ULONG64 ExpNumberOfPagedPools;
GCC_ULONG64 KeTimeIncrement;
GCC_ULONG64 KeBugCheckCallbackListHead;
GCC_ULONG64 KiBugcheckData;
GCC_ULONG64 IopErrorLogListHead;
GCC_ULONG64 ObpRootDirectoryObject;
GCC_ULONG64 ObpTypeObjectType;
GCC_ULONG64 MmSystemCacheStart;
GCC_ULONG64 MmSystemCacheEnd;
GCC_ULONG64 MmSystemCacheWs;
GCC_ULONG64 MmPfnDatabase;
GCC_ULONG64 MmSystemPtesStart;
GCC_ULONG64 MmSystemPtesEnd;
GCC_ULONG64 MmSubsectionBase;
GCC_ULONG64 MmNumberOfPagingFiles;
GCC_ULONG64 MmLowestPhysicalPage;
GCC_ULONG64 MmHighestPhysicalPage;
GCC_ULONG64 MmNumberOfPhysicalPages;
GCC_ULONG64 MmMaximumNonPagedPoolInBytes;
GCC_ULONG64 MmNonPagedSystemStart;
GCC_ULONG64 MmNonPagedPoolStart;
GCC_ULONG64 MmNonPagedPoolEnd;
GCC_ULONG64 MmPagedPoolStart;
GCC_ULONG64 MmPagedPoolEnd;
GCC_ULONG64 MmPagedPoolInformation;
ULPTR64 PsLoadedModuleList;
ULPTR64 PsActiveProcessHead;
ULPTR64 PspCidTable;
ULPTR64 ExpSystemResourcesList;
ULPTR64 ExpPagedPoolDescriptor;
ULPTR64 ExpNumberOfPagedPools;
ULPTR64 KeTimeIncrement;
ULPTR64 KeBugCheckCallbackListHead;
ULPTR64 KiBugcheckData;
ULPTR64 IopErrorLogListHead;
ULPTR64 ObpRootDirectoryObject;
ULPTR64 ObpTypeObjectType;
ULPTR64 MmSystemCacheStart;
ULPTR64 MmSystemCacheEnd;
ULPTR64 MmSystemCacheWs;
ULPTR64 MmPfnDatabase;
ULPTR64 MmSystemPtesStart;
ULPTR64 MmSystemPtesEnd;
ULPTR64 MmSubsectionBase;
ULPTR64 MmNumberOfPagingFiles;
ULPTR64 MmLowestPhysicalPage;
ULPTR64 MmHighestPhysicalPage;
ULPTR64 MmNumberOfPhysicalPages;
ULPTR64 MmMaximumNonPagedPoolInBytes;
ULPTR64 MmNonPagedSystemStart;
ULPTR64 MmNonPagedPoolStart;
ULPTR64 MmNonPagedPoolEnd;
ULPTR64 MmPagedPoolStart;
ULPTR64 MmPagedPoolEnd;
ULPTR64 MmPagedPoolInformation;
ULONG64 MmPageSize;
GCC_ULONG64 MmSizeOfPagedPoolInBytes;
GCC_ULONG64 MmTotalCommitLimit;
GCC_ULONG64 MmTotalCommittedPages;
GCC_ULONG64 MmSharedCommit;
GCC_ULONG64 MmDriverCommit;
GCC_ULONG64 MmProcessCommit;
GCC_ULONG64 MmPagedPoolCommit;
GCC_ULONG64 MmExtendedCommit;
GCC_ULONG64 MmZeroedPageListHead;
GCC_ULONG64 MmFreePageListHead;
GCC_ULONG64 MmStandbyPageListHead;
GCC_ULONG64 MmModifiedPageListHead;
GCC_ULONG64 MmModifiedNoWritePageListHead;
GCC_ULONG64 MmAvailablePages;
GCC_ULONG64 MmResidentAvailablePages;
GCC_ULONG64 PoolTrackTable;
GCC_ULONG64 NonPagedPoolDescriptor;
GCC_ULONG64 MmHighestUserAddress;
GCC_ULONG64 MmSystemRangeStart;
GCC_ULONG64 MmUserProbeAddress;
GCC_ULONG64 KdPrintCircularBuffer;
GCC_ULONG64 KdPrintCircularBufferEnd;
GCC_ULONG64 KdPrintWritePointer;
GCC_ULONG64 KdPrintRolloverCount;
GCC_ULONG64 MmLoadedUserImageList;
ULPTR64 MmSizeOfPagedPoolInBytes;
ULPTR64 MmTotalCommitLimit;
ULPTR64 MmTotalCommittedPages;
ULPTR64 MmSharedCommit;
ULPTR64 MmDriverCommit;
ULPTR64 MmProcessCommit;
ULPTR64 MmPagedPoolCommit;
ULPTR64 MmExtendedCommit;
ULPTR64 MmZeroedPageListHead;
ULPTR64 MmFreePageListHead;
ULPTR64 MmStandbyPageListHead;
ULPTR64 MmModifiedPageListHead;
ULPTR64 MmModifiedNoWritePageListHead;
ULPTR64 MmAvailablePages;
ULPTR64 MmResidentAvailablePages;
ULPTR64 PoolTrackTable;
ULPTR64 NonPagedPoolDescriptor;
ULPTR64 MmHighestUserAddress;
ULPTR64 MmSystemRangeStart;
ULPTR64 MmUserProbeAddress;
ULPTR64 KdPrintCircularBuffer;
ULPTR64 KdPrintCircularBufferEnd;
ULPTR64 KdPrintWritePointer;
ULPTR64 KdPrintRolloverCount;
ULPTR64 MmLoadedUserImageList;
#if (NTDDI_VERSION >= NTDDI_WINXP)
GCC_ULONG64 NtBuildLab;
GCC_ULONG64 KiNormalSystemCall;
ULPTR64 NtBuildLab;
ULPTR64 KiNormalSystemCall;
#endif
/* NOTE: Documented as "NT 5.0 hotfix (QFE) addition" */
#if (NTDDI_VERSION >= NTDDI_WIN2KSP4)
GCC_ULONG64 KiProcessorBlock;
GCC_ULONG64 MmUnloadedDrivers;
GCC_ULONG64 MmLastUnloadedDriver;
GCC_ULONG64 MmTriageActionTaken;
GCC_ULONG64 MmSpecialPoolTag;
GCC_ULONG64 KernelVerifier;
GCC_ULONG64 MmVerifierData;
GCC_ULONG64 MmAllocatedNonPagedPool;
GCC_ULONG64 MmPeakCommitment;
GCC_ULONG64 MmTotalCommitLimitMaximum;
GCC_ULONG64 CmNtCSDVersion;
ULPTR64 KiProcessorBlock;
ULPTR64 MmUnloadedDrivers;
ULPTR64 MmLastUnloadedDriver;
ULPTR64 MmTriageActionTaken;
ULPTR64 MmSpecialPoolTag;
ULPTR64 KernelVerifier;
ULPTR64 MmVerifierData;
ULPTR64 MmAllocatedNonPagedPool;
ULPTR64 MmPeakCommitment;
ULPTR64 MmTotalCommitLimitMaximum;
ULPTR64 CmNtCSDVersion;
#endif
#if (NTDDI_VERSION >= NTDDI_WINXP)
GCC_ULONG64 MmPhysicalMemoryBlock;
GCC_ULONG64 MmSessionBase;
GCC_ULONG64 MmSessionSize;
GCC_ULONG64 MmSystemParentTablePage;
ULPTR64 MmPhysicalMemoryBlock;
ULPTR64 MmSessionBase;
ULPTR64 MmSessionSize;
ULPTR64 MmSystemParentTablePage;
#endif
#if (NTDDI_VERSION >= NTDDI_WS03)
GCC_ULONG64 MmVirtualTranslationBase;
ULPTR64 MmVirtualTranslationBase;
USHORT OffsetKThreadNextProcessor;
USHORT OffsetKThreadTeb;
USHORT OffsetKThreadKernelStack;
@ -299,9 +305,9 @@ typedef struct _KDDEBUGGER_DATA64
USHORT OffsetPrcbProcStateContext;
USHORT OffsetPrcbNumber;
USHORT SizeEThread;
GCC_ULONG64 KdPrintCircularBufferPtr;
GCC_ULONG64 KdPrintBufferSize;
GCC_ULONG64 KeLoaderBlock;
ULPTR64 KdPrintCircularBufferPtr;
ULPTR64 KdPrintBufferSize;
ULPTR64 KeLoaderBlock;
USHORT SizePcr;
USHORT OffsetPcrSelfPcr;
USHORT OffsetPcrCurrentPrcb;
@ -322,18 +328,18 @@ typedef struct _KDDEBUGGER_DATA64
USHORT GdtTss;
USHORT Gdt64R3CmCode;
USHORT Gdt64R3CmTeb;
GCC_ULONG64 IopNumTriageDumpDataBlocks;
GCC_ULONG64 IopTriageDumpDataBlocks;
ULPTR64 IopNumTriageDumpDataBlocks;
ULPTR64 IopTriageDumpDataBlocks;
#endif
#if (NTDDI_VERSION >= NTDDI_LONGHORN)
GCC_ULONG64 VfCrashDataBlock;
GCC_ULONG64 MmBadPagesDetected;
GCC_ULONG64 MmZeroedPageSingleBitErrorsDetected;
ULPTR64 VfCrashDataBlock;
ULPTR64 MmBadPagesDetected;
ULPTR64 MmZeroedPageSingleBitErrorsDetected;
#endif
#if (NTDDI_VERSION >= NTDDI_WIN7)
GCC_ULONG64 EtwpDebuggerData;
ULPTR64 EtwpDebuggerData;
USHORT OffsetPrcbContext;
#endif
@ -358,11 +364,11 @@ typedef struct _KDDEBUGGER_DATA64
#if (NTDDI_VERSION >= NTDDI_WIN10_RS1)
USHORT Padding;
GCC_ULONG64 PteBase;
ULPTR64 PteBase;
#endif
#if (NTDDI_VERSION >= NTDDI_WIN10_RS5)
GCC_ULONG64 RetpolineStubFunctionTable;
ULPTR64 RetpolineStubFunctionTable;
ULONG RetpolineStubFunctionTableSize;
ULONG RetpolineStubOffset;
ULONG RetpolineStubSize;