mirror of
https://github.com/ZDoom/gzdoom.git
synced 2024-11-11 07:12:02 +00:00
- hook up unix unwind info (still not working)
This commit is contained in:
parent
b1bb82fe01
commit
6e598274f7
1 changed files with 133 additions and 74 deletions
|
@ -289,35 +289,32 @@ extern "C"
|
||||||
void __deregister_frame(const void*);
|
void __deregister_frame(const void*);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteLength(TArray<uint8_t> &stream, unsigned int pos, unsigned int v)
|
static void WriteLength64(TArray<uint8_t> &stream, unsigned int pos, unsigned int v)
|
||||||
{
|
{
|
||||||
*(uint64_t*)(&stream[pos]) = v;
|
*(uint64_t*)(&stream[pos]) = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void WriteLength(TArray<uint8_t> &stream, unsigned int pos, unsigned int v)
|
||||||
|
{
|
||||||
|
*(uint32_t*)(&stream[pos]) = v;
|
||||||
|
}
|
||||||
|
|
||||||
static void WriteUInt64(TArray<uint8_t> &stream, uint64_t v)
|
static void WriteUInt64(TArray<uint8_t> &stream, uint64_t v)
|
||||||
{
|
{
|
||||||
stream.Push(v & 0xff);
|
for (int i = 0; i < 8; i++)
|
||||||
stream.Push((v >> 8) & 0xff);
|
stream.Push((v >> (i * 8)) & 0xff);
|
||||||
stream.Push((v >> 16) & 0xff);
|
|
||||||
stream.Push((v >> 24) & 0xff);
|
|
||||||
stream.Push((v >> 32) & 0xff);
|
|
||||||
stream.Push((v >> 40) & 0xff);
|
|
||||||
stream.Push((v >> 48) & 0xff);
|
|
||||||
stream.Push(v >> 56);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteUInt32(TArray<uint8_t> &stream, uint32_t v)
|
static void WriteUInt32(TArray<uint8_t> &stream, uint32_t v)
|
||||||
{
|
{
|
||||||
stream.Push(v & 0xff);
|
for (int i = 0; i < 4; i++)
|
||||||
stream.Push((v >> 8) & 0xff);
|
stream.Push((v >> (i * 8)) & 0xff);
|
||||||
stream.Push((v >> 16) & 0xff);
|
|
||||||
stream.Push(v >> 24);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteUInt16(TArray<uint8_t> &stream, uint16_t v)
|
static void WriteUInt16(TArray<uint8_t> &stream, uint16_t v)
|
||||||
{
|
{
|
||||||
stream.Push(v & 0xff);
|
for (int i = 0; i < 2; i++)
|
||||||
stream.Push((v >> 8) & 0xff);
|
stream.Push((v >> (i * 8)) & 0xff);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteUInt8(TArray<uint8_t> &stream, uint8_t v)
|
static void WriteUInt8(TArray<uint8_t> &stream, uint8_t v)
|
||||||
|
@ -366,22 +363,18 @@ static void WriteSLEB128(TArray<uint8_t> &stream, int32_t v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteCIE(TArray<uint8_t> &stream, const TArray<uint8_t> &cieInstructions, uint8_t returnAddressReg, int minInstAlignment, int dataAlignmentFactor)
|
static void WritePadding(TArray<uint8_t> &stream)
|
||||||
{
|
{
|
||||||
WriteUInt32(stream, 0xffffffff); // this is a 64-bit entry
|
int padding = stream.Size() % 8;
|
||||||
unsigned int lengthPos = stream.Size();
|
if (padding != 0)
|
||||||
WriteUInt64(stream, 0); // Length
|
{
|
||||||
|
padding = 8 - padding;
|
||||||
|
for (int i = 0; i <= padding; i++) WriteUInt8(stream, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
WriteUInt32(stream, 0); // CIE ID
|
static void WriteEmptyAugmentation(TArray<uint8_t> &stream)
|
||||||
WriteUInt8(stream, 1); // CIE Version
|
{
|
||||||
WriteUInt8(stream, 'z');
|
|
||||||
//WriteUInt8(stream, 'R'); // fde encoding
|
|
||||||
WriteUInt8(stream, 0);
|
|
||||||
WriteULEB128(stream, minInstAlignment);
|
|
||||||
WriteSLEB128(stream, dataAlignmentFactor);
|
|
||||||
WriteUInt8(stream, returnAddressReg);
|
|
||||||
|
|
||||||
// augmentation length and data (empty but aligned)
|
|
||||||
int padding = (stream.Size() + 1) % 8;
|
int padding = (stream.Size() + 1) % 8;
|
||||||
if (padding == 0)
|
if (padding == 0)
|
||||||
{
|
{
|
||||||
|
@ -393,38 +386,72 @@ static void WriteCIE(TArray<uint8_t> &stream, const TArray<uint8_t> &cieInstruct
|
||||||
WriteULEB128(stream, padding);
|
WriteULEB128(stream, padding);
|
||||||
for (int i = 0; i <= padding; i++) WriteUInt8(stream, 0);
|
for (int i = 0; i <= padding; i++) WriteUInt8(stream, 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void WriteCIE(TArray<uint8_t> &stream, const TArray<uint8_t> &cieInstructions, uint8_t returnAddressReg)
|
||||||
|
{
|
||||||
|
#ifdef USE_DWARF64
|
||||||
|
WriteUInt32(stream, 0xffffffff); // this is a 64-bit entry
|
||||||
|
unsigned int lengthPos = stream.Size();
|
||||||
|
WriteUInt64(stream, 0); // Length
|
||||||
|
WriteUInt64(stream, 0); // CIE ID
|
||||||
|
#else
|
||||||
|
unsigned int lengthPos = stream.Size();
|
||||||
|
WriteUInt32(stream, 0); // Length
|
||||||
|
WriteUInt32(stream, 0); // CIE ID
|
||||||
|
#endif
|
||||||
|
|
||||||
|
WriteUInt8(stream, 1); // CIE Version
|
||||||
|
WriteUInt8(stream, 'z');
|
||||||
|
//WriteUInt8(stream, 'R'); // fde encoding
|
||||||
|
WriteUInt8(stream, 0);
|
||||||
|
WriteULEB128(stream, 1);
|
||||||
|
WriteSLEB128(stream, -4);
|
||||||
|
WriteUInt8(stream, returnAddressReg);
|
||||||
|
|
||||||
|
WriteEmptyAugmentation(stream);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < cieInstructions.Size(); i++)
|
for (unsigned int i = 0; i < cieInstructions.Size(); i++)
|
||||||
stream.Push(cieInstructions[i]);
|
stream.Push(cieInstructions[i]);
|
||||||
|
|
||||||
// Padding and update length field
|
WritePadding(stream);
|
||||||
unsigned int length = stream.Size() - lengthPos - 8;
|
#ifdef USE_DWARF64
|
||||||
padding = stream.Size() % 8;
|
WriteLength64(stream, lengthPos, stream.Size() - lengthPos - 8);
|
||||||
for (int i = 0; i <= padding; i++) WriteUInt8(stream, 0);
|
#else
|
||||||
WriteLength(stream, lengthPos, length);
|
WriteLength(stream, lengthPos, stream.Size() - lengthPos - 4);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteFDE(TArray<uint8_t> &stream, const TArray<uint8_t> &fdeInstructions, uint32_t cieLocation, unsigned int &functionStart)
|
static void WriteFDE(TArray<uint8_t> &stream, const TArray<uint8_t> &fdeInstructions, uint32_t cieLocation, unsigned int &functionStart)
|
||||||
{
|
{
|
||||||
uint32_t offsetToCIE = stream.Size() - cieLocation;
|
#ifdef USE_DWARF64
|
||||||
|
|
||||||
WriteUInt32(stream, 0xffffffff); // this is a 64-bit entry
|
WriteUInt32(stream, 0xffffffff); // this is a 64-bit entry
|
||||||
unsigned int lengthPos = stream.Size();
|
unsigned int lengthPos = stream.Size();
|
||||||
WriteUInt64(stream, 0); // Length
|
WriteUInt64(stream, 0); // Length
|
||||||
|
uint32_t offsetToCIE = stream.Size() - cieLocation;
|
||||||
|
WriteUInt64(stream, offsetToCIE);
|
||||||
|
#else
|
||||||
|
unsigned int lengthPos = stream.Size();
|
||||||
|
WriteUInt32(stream, 0); // Length
|
||||||
|
uint32_t offsetToCIE = stream.Size() - cieLocation;
|
||||||
WriteUInt32(stream, offsetToCIE);
|
WriteUInt32(stream, offsetToCIE);
|
||||||
|
#endif
|
||||||
|
|
||||||
functionStart = stream.Size();
|
functionStart = stream.Size();
|
||||||
WriteUInt64(stream, 0); // func start
|
WriteUInt64(stream, 0); // func start
|
||||||
WriteUInt64(stream, 0); // func size
|
WriteUInt64(stream, 0); // func size
|
||||||
|
|
||||||
|
WriteEmptyAugmentation(stream);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < fdeInstructions.Size(); i++)
|
for (unsigned int i = 0; i < fdeInstructions.Size(); i++)
|
||||||
stream.Push(fdeInstructions[i]);
|
stream.Push(fdeInstructions[i]);
|
||||||
|
|
||||||
// Padding and update length field
|
WritePadding(stream);
|
||||||
unsigned int length = stream.Size() - lengthPos - 8;
|
#ifdef USE_DWARF64
|
||||||
int padding = stream.Size() % 8;
|
WriteLength64(stream, lengthPos, stream.Size() - lengthPos - 8);
|
||||||
for (int i = 0; i <= padding; i++) WriteUInt8(stream, 0);
|
#else
|
||||||
WriteLength(stream, lengthPos, length);
|
WriteLength(stream, lengthPos, stream.Size() - lengthPos - 4);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteAdvanceLoc(TArray<uint8_t> &fdeInstructions, uint64_t offset, uint64_t &lastOffset)
|
static void WriteAdvanceLoc(TArray<uint8_t> &fdeInstructions, uint64_t offset, uint64_t &lastOffset)
|
||||||
|
@ -452,17 +479,6 @@ static TArray<uint8_t> CreateUnwindInfoUnix(asmjit::CCFunc *func, unsigned int &
|
||||||
{
|
{
|
||||||
using namespace asmjit;
|
using namespace asmjit;
|
||||||
|
|
||||||
FuncFrameLayout layout;
|
|
||||||
Error error = layout.init(func->getDetail(), func->getFrameInfo());
|
|
||||||
if (error != kErrorOk)
|
|
||||||
I_FatalError("FuncFrameLayout.init failed");
|
|
||||||
|
|
||||||
// We need a dummy emitter for instruction size calculations
|
|
||||||
CodeHolder code;
|
|
||||||
code.init(GetHostCodeInfo());
|
|
||||||
X86Assembler assembler(&code);
|
|
||||||
X86Emitter *emitter = assembler.asEmitter();
|
|
||||||
|
|
||||||
// Build .eh_frame:
|
// Build .eh_frame:
|
||||||
//
|
//
|
||||||
// The documentation for this can be found in the DWARF standard
|
// The documentation for this can be found in the DWARF standard
|
||||||
|
@ -495,12 +511,35 @@ static TArray<uint8_t> CreateUnwindInfoUnix(asmjit::CCFunc *func, unsigned int &
|
||||||
TArray<uint8_t> fdeInstructions;
|
TArray<uint8_t> fdeInstructions;
|
||||||
uint64_t lastOffset = 0;
|
uint64_t lastOffset = 0;
|
||||||
|
|
||||||
int minInstAlignment = 1;
|
|
||||||
int dataAlignmentFactor = -4;
|
|
||||||
uint8_t returnAddressReg = dwarfRegRAId;
|
uint8_t returnAddressReg = dwarfRegRAId;
|
||||||
|
|
||||||
// To do: do we need to write register defaults into the CIE or does the defaults match the x64 calling convention?
|
// Do we need to write register defaults into the CIE or does the defaults match the x64 calling convention?
|
||||||
// Great! the "System V Application Binary Interface AMD64 Architecture Processor Supplement" doesn't say what the defaults are..
|
// Great! the "System V Application Binary Interface AMD64 Architecture Processor Supplement" doesn't say what the defaults are..
|
||||||
|
// This is basically just the x64 calling convention..
|
||||||
|
WriteUInt8(cieInstructions, 0x0c); // DW_CFA_def_cfa
|
||||||
|
WriteULEB128(cieInstructions, dwarfRegId[X86Gp::kIdSp]);
|
||||||
|
WriteULEB128(cieInstructions, 0);
|
||||||
|
for (auto regId : { X86Gp::kIdAx, X86Gp::kIdDx, X86Gp::kIdCx, X86Gp::kIdSi, X86Gp::kIdDi, X86Gp::kIdSp, X86Gp::kIdR8, X86Gp::kIdR9, X86Gp::kIdR10, X86Gp::kIdR11 })
|
||||||
|
{
|
||||||
|
WriteUInt8(cieInstructions, 0x07); // DW_CFA_undefined
|
||||||
|
WriteULEB128(cieInstructions, dwarfRegId[regId]);
|
||||||
|
}
|
||||||
|
for (auto regId : { X86Gp::kIdBx, X86Gp::kIdBp, X86Gp::kIdR12, X86Gp::kIdR13, X86Gp::kIdR14, X86Gp::kIdR15 })
|
||||||
|
{
|
||||||
|
WriteUInt8(cieInstructions, 0x08); // DW_CFA_same_value
|
||||||
|
WriteULEB128(cieInstructions, dwarfRegId[regId]);
|
||||||
|
}
|
||||||
|
|
||||||
|
FuncFrameLayout layout;
|
||||||
|
Error error = layout.init(func->getDetail(), func->getFrameInfo());
|
||||||
|
if (error != kErrorOk)
|
||||||
|
I_FatalError("FuncFrameLayout.init failed");
|
||||||
|
|
||||||
|
// We need a dummy emitter for instruction size calculations
|
||||||
|
CodeHolder code;
|
||||||
|
code.init(GetHostCodeInfo());
|
||||||
|
X86Assembler assembler(&code);
|
||||||
|
X86Emitter *emitter = assembler.asEmitter();
|
||||||
|
|
||||||
// Note: the following code must match exactly what X86Internal::emitProlog does
|
// Note: the following code must match exactly what X86Internal::emitProlog does
|
||||||
|
|
||||||
|
@ -511,7 +550,7 @@ static TArray<uint8_t> CreateUnwindInfoUnix(asmjit::CCFunc *func, unsigned int &
|
||||||
X86Gp saReg = emitter->zsp(); // Stack-arguments base register.
|
X86Gp saReg = emitter->zsp(); // Stack-arguments base register.
|
||||||
uint32_t gpSaved = layout.getSavedRegs(X86Reg::kKindGp);
|
uint32_t gpSaved = layout.getSavedRegs(X86Reg::kKindGp);
|
||||||
|
|
||||||
int saveoffset = 0;
|
int stackOffset = 0;
|
||||||
|
|
||||||
if (layout.hasPreservedFP())
|
if (layout.hasPreservedFP())
|
||||||
{
|
{
|
||||||
|
@ -521,9 +560,11 @@ static TArray<uint8_t> CreateUnwindInfoUnix(asmjit::CCFunc *func, unsigned int &
|
||||||
emitter->push(zbp);
|
emitter->push(zbp);
|
||||||
|
|
||||||
WriteAdvanceLoc(fdeInstructions, assembler.getOffset(), lastOffset);
|
WriteAdvanceLoc(fdeInstructions, assembler.getOffset(), lastOffset);
|
||||||
|
stackOffset += 8;
|
||||||
|
WriteUInt8(fdeInstructions, 0x0e); // DW_CFA_def_cfa_offset
|
||||||
|
WriteULEB128(fdeInstructions, stackOffset);
|
||||||
WriteUInt8(fdeInstructions, (2 << 6) | dwarfRegId[X86Gp::kIdBp]); // DW_CFA_offset
|
WriteUInt8(fdeInstructions, (2 << 6) | dwarfRegId[X86Gp::kIdBp]); // DW_CFA_offset
|
||||||
WriteULEB128(fdeInstructions, saveoffset);
|
WriteULEB128(fdeInstructions, stackOffset - 8);
|
||||||
saveoffset += 2;
|
|
||||||
|
|
||||||
emitter->mov(zbp, zsp);
|
emitter->mov(zbp, zsp);
|
||||||
}
|
}
|
||||||
|
@ -538,9 +579,11 @@ static TArray<uint8_t> CreateUnwindInfoUnix(asmjit::CCFunc *func, unsigned int &
|
||||||
emitter->push(gpReg);
|
emitter->push(gpReg);
|
||||||
|
|
||||||
WriteAdvanceLoc(fdeInstructions, assembler.getOffset(), lastOffset);
|
WriteAdvanceLoc(fdeInstructions, assembler.getOffset(), lastOffset);
|
||||||
|
stackOffset += 8;
|
||||||
|
WriteUInt8(fdeInstructions, 0x0e); // DW_CFA_def_cfa_offset
|
||||||
|
WriteULEB128(fdeInstructions, stackOffset);
|
||||||
WriteUInt8(fdeInstructions, (2 << 6) | dwarfRegId[regId]); // DW_CFA_offset
|
WriteUInt8(fdeInstructions, (2 << 6) | dwarfRegId[regId]); // DW_CFA_offset
|
||||||
WriteULEB128(fdeInstructions, saveoffset);
|
WriteULEB128(fdeInstructions, stackOffset - 8);
|
||||||
saveoffset += 2;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -566,12 +609,10 @@ static TArray<uint8_t> CreateUnwindInfoUnix(asmjit::CCFunc *func, unsigned int &
|
||||||
// Emit: 'sub zsp, StackAdjustment'.
|
// Emit: 'sub zsp, StackAdjustment'.
|
||||||
emitter->sub(zsp, layout.getStackAdjustment());
|
emitter->sub(zsp, layout.getStackAdjustment());
|
||||||
|
|
||||||
uint32_t stackadjust = layout.getStackAdjustment();
|
|
||||||
WriteAdvanceLoc(fdeInstructions, assembler.getOffset(), lastOffset);
|
WriteAdvanceLoc(fdeInstructions, assembler.getOffset(), lastOffset);
|
||||||
|
stackOffset += layout.getStackAdjustment();
|
||||||
WriteUInt8(fdeInstructions, 0x0e); // DW_CFA_def_cfa_offset
|
WriteUInt8(fdeInstructions, 0x0e); // DW_CFA_def_cfa_offset
|
||||||
WriteULEB128(fdeInstructions, stackadjust);
|
WriteULEB128(fdeInstructions, stackOffset);
|
||||||
|
|
||||||
saveoffset += layout.getStackAdjustment() / dataAlignmentFactor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (layout.hasDynamicAlignment() && layout.hasDsaSlotUsed())
|
if (layout.hasDynamicAlignment() && layout.hasDsaSlotUsed())
|
||||||
|
@ -584,7 +625,7 @@ static TArray<uint8_t> CreateUnwindInfoUnix(asmjit::CCFunc *func, unsigned int &
|
||||||
uint32_t xmmSaved = layout.getSavedRegs(X86Reg::kKindVec);
|
uint32_t xmmSaved = layout.getSavedRegs(X86Reg::kKindVec);
|
||||||
if (xmmSaved)
|
if (xmmSaved)
|
||||||
{
|
{
|
||||||
saveoffset += layout.getVecStackOffset() / dataAlignmentFactor;
|
stackOffset += layout.getVecStackOffset();
|
||||||
X86Mem vecBase = x86::ptr(zsp, layout.getVecStackOffset());
|
X86Mem vecBase = x86::ptr(zsp, layout.getVecStackOffset());
|
||||||
X86Reg vecReg = x86::xmm(0);
|
X86Reg vecReg = x86::xmm(0);
|
||||||
bool avx = layout.isAvxEnabled();
|
bool avx = layout.isAvxEnabled();
|
||||||
|
@ -602,13 +643,13 @@ static TArray<uint8_t> CreateUnwindInfoUnix(asmjit::CCFunc *func, unsigned int &
|
||||||
|
|
||||||
WriteAdvanceLoc(fdeInstructions, assembler.getOffset(), lastOffset);
|
WriteAdvanceLoc(fdeInstructions, assembler.getOffset(), lastOffset);
|
||||||
WriteUInt8(fdeInstructions, (2 << 6) | (dwarfRegXmmId + regId)); // DW_CFA_offset
|
WriteUInt8(fdeInstructions, (2 << 6) | (dwarfRegXmmId + regId)); // DW_CFA_offset
|
||||||
WriteULEB128(fdeInstructions, saveoffset);
|
WriteULEB128(fdeInstructions, stackOffset);
|
||||||
saveoffset += 2;
|
stackOffset += 8;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TArray<uint8_t> stream;
|
TArray<uint8_t> stream;
|
||||||
WriteCIE(stream, cieInstructions, returnAddressReg, minInstAlignment, dataAlignmentFactor);
|
WriteCIE(stream, cieInstructions, returnAddressReg);
|
||||||
WriteFDE(stream, fdeInstructions, 0, functionStart);
|
WriteFDE(stream, fdeInstructions, 0, functionStart);
|
||||||
WriteUInt32(stream, 0);
|
WriteUInt32(stream, 0);
|
||||||
return stream;
|
return stream;
|
||||||
|
@ -623,7 +664,7 @@ void *AddJitFunction(asmjit::CodeHolder* code, asmjit::CCFunc *func)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
unsigned int fdeFunctionStart = 0;
|
unsigned int fdeFunctionStart = 0;
|
||||||
TArray<uint8_t> unwindInfo;// = CreateUnwindInfoUnix(func, fdeFunctionStart);
|
TArray<uint8_t> unwindInfo = CreateUnwindInfoUnix(func, fdeFunctionStart);
|
||||||
size_t unwindInfoSize = unwindInfo.Size();
|
size_t unwindInfoSize = unwindInfo.Size();
|
||||||
|
|
||||||
codeSize = (codeSize + 15) / 16 * 16;
|
codeSize = (codeSize + 15) / 16 * 16;
|
||||||
|
@ -661,11 +702,29 @@ void *AddJitFunction(asmjit::CodeHolder* code, asmjit::CCFunc *func)
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
uint32_t offset = *((uint32_t *)(entry + 4));
|
if (length == 0xffffffff)
|
||||||
if (offset != 0)
|
|
||||||
{
|
{
|
||||||
__register_frame(entry);
|
uint64_t length64 = *((uint64_t *)(entry + 4));
|
||||||
JitFrames.Push(entry);
|
if (length64 == 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
uint64_t offset = *((uint64_t *)(entry + 12));
|
||||||
|
if (offset != 0)
|
||||||
|
{
|
||||||
|
__register_frame(entry);
|
||||||
|
JitFrames.Push(entry);
|
||||||
|
}
|
||||||
|
entry += length64 + 12;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uint32_t offset = *((uint32_t *)(entry + 4));
|
||||||
|
if (offset != 0)
|
||||||
|
{
|
||||||
|
__register_frame(entry);
|
||||||
|
JitFrames.Push(entry);
|
||||||
|
}
|
||||||
|
entry += length + 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
|
|
Loading…
Reference in a new issue