From ce51256c91e566d339c6a670af11bab519f98349 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Thu, 9 Jul 2026 21:49:15 +0200 Subject: Get rid of windows.h --- src/stack_walk.c | 283 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 281 insertions(+), 2 deletions(-) (limited to 'src/stack_walk.c') diff --git a/src/stack_walk.c b/src/stack_walk.c index cc030b2..cbb6464 100644 --- a/src/stack_walk.c +++ b/src/stack_walk.c @@ -1,10 +1,289 @@ #include "stack_walk.h" -#include - #include "module.h" #include "utils.h" +// ======================= +// ====== windows.h ====== +// ======================= + +#define CONTEXT_AMD64 0x00100000L + +#define CONTEXT_CONTROL (CONTEXT_AMD64 | 0x00000001L) +#define CONTEXT_INTEGER (CONTEXT_AMD64 | 0x00000002L) +#define CONTEXT_SEGMENTS (CONTEXT_AMD64 | 0x00000004L) +#define CONTEXT_FLOATING_POINT (CONTEXT_AMD64 | 0x00000008L) +#define CONTEXT_DEBUG_REGISTERS (CONTEXT_AMD64 | 0x00000010L) + +#define CONTEXT_FULL (CONTEXT_CONTROL | CONTEXT_INTEGER | \ + CONTEXT_FLOATING_POINT) + +#define CONTEXT_ALL (CONTEXT_CONTROL | CONTEXT_INTEGER | \ + CONTEXT_SEGMENTS | CONTEXT_FLOATING_POINT | \ + CONTEXT_DEBUG_REGISTERS) + +#define CONTEXT_XSTATE (CONTEXT_AMD64 | 0x00000040L) +#define CONTEXT_KERNEL_CET (CONTEXT_AMD64 | 0x00000080L) + +// +// Define 128-bit 16-byte aligned xmm register type. +// + +typedef struct __declspec(align(16)) _M128A { + u64 Low; + i64 High; +} M128A, *PM128A; + +// +// Format of data for (F)XSAVE/(F)XRSTOR instruction +// + +typedef struct __declspec(align(16)) _XSAVE_FORMAT { + u16 ControlWord; + u16 StatusWord; + u8 TagWord; + u8 Reserved1; + u16 ErrorOpcode; + u32 ErrorOffset; + u16 ErrorSelector; + u16 Reserved2; + u32 DataOffset; + u16 DataSelector; + u16 Reserved3; + u32 MxCsr; + u32 MxCsr_Mask; + M128A FloatRegisters[8]; + +#if defined(_WIN64) + + M128A XmmRegisters[16]; + u8 Reserved4[96]; + +#else + + M128A XmmRegisters[8]; + BYTE Reserved4[224]; + +#endif + +} XSAVE_FORMAT, *PXSAVE_FORMAT; + +typedef XSAVE_FORMAT XMM_SAVE_AREA32, *PXMM_SAVE_AREA32; + +// +// Context Frame +// +// This frame has a several purposes: 1) it is used as an argument to +// NtContinue, 2) it is used to constuct a call frame for APC delivery, +// and 3) it is used in the user level thread creation routines. +// +// +// The flags field within this record controls the contents of a CONTEXT +// record. +// +// If the context record is used as an input parameter, then for each +// portion of the context record controlled by a flag whose value is +// set, it is assumed that that portion of the context record contains +// valid context. If the context record is being used to modify a threads +// context, then only that portion of the threads context is modified. +// +// If the context record is used as an output parameter to capture the +// context of a thread, then only those portions of the thread's context +// corresponding to set flags will be returned. +// +// CONTEXT_CONTROL specifies SegSs, Rsp, SegCs, Rip, and EFlags. +// +// CONTEXT_INTEGER specifies Rax, Rcx, Rdx, Rbx, Rbp, Rsi, Rdi, and R8-R15. +// +// CONTEXT_SEGMENTS specifies SegDs, SegEs, SegFs, and SegGs. +// +// CONTEXT_FLOATING_POINT specifies Xmm0-Xmm15. +// +// CONTEXT_DEBUG_REGISTERS specifies Dr0-Dr3 and Dr6-Dr7. +// + +typedef struct __declspec(align(16)) _CONTEXT { + + // + // Register parameter home addresses. + // + // N.B. These fields are for convience - they could be used to extend the + // context record in the future. + // + + u64 P1Home; + u64 P2Home; + u64 P3Home; + u64 P4Home; + u64 P5Home; + u64 P6Home; + + // + // Control flags. + // + + u32 ContextFlags; + u32 MxCsr; + + // + // Segment Registers and processor flags. + // + + u16 SegCs; + u16 SegDs; + u16 SegEs; + u16 SegFs; + u16 SegGs; + u16 SegSs; + u32 EFlags; + + // + // Debug registers + // + + u64 Dr0; + u64 Dr1; + u64 Dr2; + u64 Dr3; + u64 Dr6; + u64 Dr7; + + // + // Integer registers. + // + + u64 Rax; + u64 Rcx; + u64 Rdx; + u64 Rbx; + u64 Rsp; + u64 Rbp; + u64 Rsi; + u64 Rdi; + u64 R8; + u64 R9; + u64 R10; + u64 R11; + u64 R12; + u64 R13; + u64 R14; + u64 R15; + + // + // Program counter. + // + + u64 Rip; + + // + // Floating point state. + // + + union { + XMM_SAVE_AREA32 FltSave; + struct { + M128A Header[2]; + M128A Legacy[8]; + M128A Xmm0; + M128A Xmm1; + M128A Xmm2; + M128A Xmm3; + M128A Xmm4; + M128A Xmm5; + M128A Xmm6; + M128A Xmm7; + M128A Xmm8; + M128A Xmm9; + M128A Xmm10; + M128A Xmm11; + M128A Xmm12; + M128A Xmm13; + M128A Xmm14; + M128A Xmm15; + }; + }; + + // + // Vector registers. + // + + M128A VectorRegister[26]; + u64 VectorControl; + + // + // Special debug control registers. + // + + u64 DebugControl; + u64 LastBranchToRip; + u64 LastBranchFromRip; + u64 LastExceptionToRip; + u64 LastExceptionFromRip; +} CONTEXT, *PCONTEXT; +#define PAGE_NOACCESS 0x01 +#define PAGE_READONLY 0x02 +#define PAGE_READWRITE 0x04 +#define PAGE_WRITECOPY 0x08 +#define PAGE_EXECUTE 0x10 +#define PAGE_EXECUTE_READ 0x20 +#define PAGE_EXECUTE_READWRITE 0x40 +#define PAGE_EXECUTE_WRITECOPY 0x80 +#define PAGE_GUARD 0x100 +#define PAGE_NOCACHE 0x200 +#define PAGE_WRITECOMBINE 0x400 +#define PAGE_GRAPHICS_NOACCESS 0x0800 +#define PAGE_GRAPHICS_READONLY 0x1000 +#define PAGE_GRAPHICS_READWRITE 0x2000 +#define PAGE_GRAPHICS_EXECUTE 0x4000 +#define PAGE_GRAPHICS_EXECUTE_READ 0x8000 +#define PAGE_GRAPHICS_EXECUTE_READWRITE 0x10000 +#define PAGE_GRAPHICS_COHERENT 0x20000 +#define PAGE_GRAPHICS_NOCACHE 0x40000 +#define PAGE_ENCLAVE_THREAD_CONTROL 0x80000000 +#define PAGE_REVERT_TO_FILE_MAP 0x80000000 +#define PAGE_TARGETS_NO_UPDATE 0x40000000 +#define PAGE_TARGETS_INVALID 0x40000000 +#define PAGE_ENCLAVE_UNVALIDATED 0x20000000 +#define PAGE_ENCLAVE_MASK 0x10000000 +#define PAGE_ENCLAVE_DECOMMIT (PAGE_ENCLAVE_MASK | 0) +#define PAGE_ENCLAVE_SS_FIRST (PAGE_ENCLAVE_MASK | 1) +#define PAGE_ENCLAVE_SS_REST (PAGE_ENCLAVE_MASK | 2) +#define MEM_COMMIT 0x00001000 +#define MEM_RESERVE 0x00002000 +#define MEM_REPLACE_PLACEHOLDER 0x00004000 +#define MEM_RESERVE_PLACEHOLDER 0x00040000 +#define MEM_RESET 0x00080000 +#define MEM_TOP_DOWN 0x00100000 +#define MEM_WRITE_WATCH 0x00200000 +#define MEM_PHYSICAL 0x00400000 +#define MEM_ROTATE 0x00800000 +#define MEM_DIFFERENT_IMAGE_BASE_OK 0x00800000 +#define MEM_RESET_UNDO 0x01000000 +#define MEM_LARGE_PAGES 0x20000000 +#define MEM_4MB_PAGES 0x80000000 +#define MEM_64K_PAGES (MEM_LARGE_PAGES | MEM_PHYSICAL) +#define MEM_UNMAP_WITH_TRANSIENT_BOOST 0x00000001 +#define MEM_COALESCE_PLACEHOLDERS 0x00000001 +#define MEM_PRESERVE_PLACEHOLDER 0x00000002 +#define MEM_DECOMMIT 0x00004000 +#define MEM_RELEASE 0x00008000 +#define MEM_FREE 0x00010000 +#define MEM_EXTENDED_PARAMETER_GRAPHICS 0x00000001 +#define MEM_EXTENDED_PARAMETER_NONPAGED 0x00000002 +#define MEM_EXTENDED_PARAMETER_ZERO_PAGES_OPTIONAL 0x00000004 +#define MEM_EXTENDED_PARAMETER_NONPAGED_LARGE 0x00000008 +#define MEM_EXTENDED_PARAMETER_NONPAGED_HUGE 0x00000010 +#define MEM_EXTENDED_PARAMETER_SOFT_FAULT_PAGES 0x00000020 +#define MEM_EXTENDED_PARAMETER_EC_CODE 0x00000040 +#define MEM_EXTENDED_PARAMETER_NUMA_NODE_MANDATORY MINLONG64 + +extern void* VirtualAlloc(void* lpAddress, size_t dwSize, u32 flAllocationType, u32 flProtect); +extern i32 VirtualFree(void* lpAddress, size_t dwSize, u32 dwFreeType); + +// ======================== +// ====== !windows.h ====== +// ======================== + // enum UNWIND_INFO_FLAGS { // UNW_FLAG_EHANDLER, // UNW_FLAG_UHANDLER, -- cgit v1.3