Skip to content

Commit

Permalink
debuginfo: fix offset to UnwindData on Win64
Browse files Browse the repository at this point in the history
We have 2 copies of this data, and so need to make sure we are pointing
at the correct one for runtime.
  • Loading branch information
vtjnash committed Feb 13, 2022
1 parent d395659 commit 2f1f2f6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/cgmemmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ static void unmap_page(void *ptr, size_t size)
enum class Prot : int {
RW = PAGE_READWRITE,
RX = PAGE_EXECUTE,
RO = PAGE_READONLY
RO = PAGE_READONLY,
NO = PAGE_NOACCESS
};

static void protect_page(void *ptr, size_t size, Prot flags)
Expand All @@ -81,7 +82,8 @@ static void protect_page(void *ptr, size_t size, Prot flags)
enum class Prot : int {
RW = PROT_READ | PROT_WRITE,
RX = PROT_READ | PROT_EXEC,
RO = PROT_READ
RO = PROT_READ,
NO = PROT_NONE
};

static void protect_page(void *ptr, size_t size, Prot flags)
Expand Down Expand Up @@ -647,7 +649,7 @@ class DualMapAllocator : public ROAllocator<exec> {
unmap_page((void*)block.wr_ptr, block.total);
}
else {
protect_page((void*)block.wr_ptr, block.total, Prot::RO);
protect_page((void*)block.wr_ptr, block.total, Prot::NO);
block.state = SplitPtrBlock::WRInit;
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/debuginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ static void create_PRUNTIME_FUNCTION(uint8_t *Code, size_t Size, StringRef fnnam
tbl->BeginAddress = (DWORD)(Code - Section);
tbl->EndAddress = (DWORD)(Code - Section + Size);
tbl->UnwindData = (DWORD)(UnwindData - Section);
assert(Code >= Section && Code + Size <= Section + Allocated);
assert(UnwindData >= Section && UnwindData <= Section + Allocated);
#else // defined(_CPU_X86_64_)
Section += (uintptr_t)Code;
mod_size = Size;
Expand Down Expand Up @@ -265,20 +267,13 @@ class JITObjectRegistry
uint8_t *catchjmp = NULL;
for (const object::SymbolRef &sym_iter : Object.symbols()) {
StringRef sName = cantFail(sym_iter.getName());
uint8_t **pAddr = NULL;
if (sName.equals("__UnwindData")) {
pAddr = &UnwindData;
}
else if (sName.equals("__catchjmp")) {
pAddr = &catchjmp;
}
if (pAddr) {
if (sName.equals("__UnwindData") || sName.equals("__catchjmp")) {
uint64_t Addr = cantFail(sym_iter.getAddress());
auto Section = cantFail(sym_iter.getSection());
assert(Section != EndSection && Section->isText());
uint64_t SectionAddr = Section->getAddress();
sName = cantFail(Section->getName());
uint64_t SectionLoadAddr = getLoadAddress(sName);
StringRef secName = cantFail(Section->getName());
uint64_t SectionLoadAddr = getLoadAddress(secName);
assert(SectionLoadAddr);
if (SectionAddrCheck) // assert that all of the Sections are at the same location
assert(SectionAddrCheck == SectionAddr &&
Expand All @@ -288,8 +283,13 @@ class JITObjectRegistry
SectionWriteCheck = SectionLoadAddr;
if (lookupWriteAddress)
SectionWriteCheck = (uintptr_t)lookupWriteAddress((void*)SectionLoadAddr);
Addr += SectionWriteCheck - SectionLoadAddr;
*pAddr = (uint8_t*)Addr;
Addr += SectionWriteCheck - SectionLoadCheck;
if (sName.equals("__UnwindData")) {
UnwindData = (uint8_t*)Addr;
}
else if (sName.equals("__catchjmp")) {
catchjmp = (uint8_t*)Addr;
}
}
}
assert(catchjmp);
Expand All @@ -312,6 +312,7 @@ class JITObjectRegistry
UnwindData[6] = 1; // first instruction
UnwindData[7] = 0x50; // push RBP
*(DWORD*)&UnwindData[8] = (DWORD)(catchjmp - (uint8_t*)SectionWriteCheck); // relative location of catchjmp
UnwindData -= SectionWriteCheck - SectionLoadCheck;
#endif // defined(_OS_X86_64_)
#endif // defined(_OS_WINDOWS_)

Expand Down

0 comments on commit 2f1f2f6

Please sign in to comment.