Experiment with supporting Windows Arm64 natively

So far I haven't found any way to run native Arm64 code on Windows Arm64
without using MSVC. When I build a PE binary from scratch that should be
a valid Windows Arm64 program, the OS refuses to run it. Possibly due to
requiring additional content like XML manifests or relocation or control
flow integrity data that isn't normally required on x64. I've also tried
using VirtualAlloc2() to JIT an Arm64 native function, but VirtualAlloc2
always fails with invalid parameter. I tried using MSVC to create an ARM
DLL that my x64 emulated program can link at runtime, to pass a function
pointer with ARM code, but LoadLibrary() rejects ARM DLLs as invalid exe

The only option left, is likely to write a new program like ape/ape-m1.c
which can be compiled by MSVC to load and run an AARCH64 ELF executable.
The emulated x64 binary would detect emulation using IsWow64Process2 and
then drop the loader executable in a temporary folder, and re-launch the
original executable, using the Arm64 segments of the cosmocc fat binary.
This commit is contained in:
Justine Tunney 2024-08-16 06:43:59 -07:00
parent 1671283f1a
commit 5bd22aef12
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
12 changed files with 88 additions and 16 deletions

20
libc/nt/struct/arm64.h Normal file
View file

@ -0,0 +1,20 @@
#ifndef COSMOPOLITAN_LIBC_NT_STRUCT_ARM64_H_
#define COSMOPOLITAN_LIBC_NT_STRUCT_ARM64_H_
struct NtArm64RuntimeFunction {
uint32_t BeginAddress;
union {
uint32_t UnwindData;
struct {
uint32_t Flag : 2;
uint32_t FunctionLength : 11;
uint32_t RegF : 3;
uint32_t RegI : 4;
uint32_t H : 1;
uint32_t CR : 2;
uint32_t FrameSize : 9;
};
};
};
#endif /* COSMOPOLITAN_LIBC_NT_STRUCT_ARM64_H_ */

View file

@ -9,26 +9,28 @@
#define kNtMemExtendedParameterPartitionHandle 3
#define kNtMemExtendedParameterUserPhysicalHandle 4
#define kNtMemExtendedParameterAttributeFlags 5
#define kNtMemExtendedParameterMax 6
#define kNtMemExtendedParameterImageMachine 6
#define kNtMemExtendedParameterMax 7
#define kNtMemExtendedParameterGraphics 0x00000001
#define kNtMemExtendedParameterNonpaged 0x00000002
#define kNtMemExtendedParameterZeroPagesOptional 0x00000004
#define kNtMemExtendedParameterNonpagedLarge 0x00000008
#define kNtMemExtendedParameterNonpagedHuge 0x00000010
#define kNtMemExtendedParameterSoftFaultPages 0x00000020
#define kNtMemExtendedParameterEcCode 0x00000040
#define kNtMemExtendedParameterImageNoHpat 0x00000080
struct NtMemExtendedParameter {
struct {
uint64_t Type : kNtMemExtendedParameterTypeBits;
uint64_t Reserved : 64 - kNtMemExtendedParameterTypeBits;
} DUMMYSTRUCTNAME;
uint8_t Type;
uint8_t Reserved[7];
union {
uint64_t ULong64;
void *Pointer;
size_t Size;
intptr_t Handle;
unsigned ULong;
} DUMMYUNIONNAME;
};
};
#endif /* COSMOPOLITAN_LIBC_NT_STRUCT_MEMEXTENDEDPARAMETER_H_ */