Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/coreclr/debug/createdump/dumpwritermacho.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "createdump.h"
#include "specialthreadinfo.h"
#include <sys/sysctl.h>

extern int g_readProcessMemoryResult;

Expand All @@ -16,13 +17,22 @@ DumpWriter::WriteDump()

BuildThreadLoadCommands();

BuildAddressableBitsNote();

BuildProcessMetadataNote();

uint64_t fileOffset = 0;
if (!WriteHeader(&fileOffset)) {
return false;
}

// LC_NOTE payloads follow the load commands in the order the notes are written
if (m_hasAddressableBitsNote)
{
m_addressableBitsNote.offset = fileOffset;
fileOffset += m_addressableBitsNote.size;
}

m_processMetadataNote.offset = fileOffset;
fileOffset += m_processMetadataNote.size;

Expand Down Expand Up @@ -59,10 +69,18 @@ DumpWriter::WriteDump()
}
}

// Write the process metadata note as the final load command
// Write the LC_NOTE commands as the final load commands, followed by their payloads
if (m_hasAddressableBitsNote &&
!WriteData(&m_addressableBitsNote, m_addressableBitsNote.cmdsize)) {
return false;
}
if (!WriteData(&m_processMetadataNote, m_processMetadataNote.cmdsize)) {
return false;
}
if (m_hasAddressableBitsNote &&
!WriteData(&m_addressableBits, sizeof(m_addressableBits))) {
return false;
}
if (!WriteData(m_processMetadata.data(), m_processMetadata.size())) {
return false;
}
Expand Down Expand Up @@ -100,6 +118,43 @@ ConvertFlags(uint32_t flags)
return prot;
}

void
DumpWriter::BuildAddressableBitsNote()
{
#if defined(HOST_ARM64)
// arm64e system libraries sign return addresses with pointer authentication. Without
// this note, LLDB only strips the top byte and can't unwind through signed frames.
// Use the same source as debugserver for the number of valid address bits.
uint32_t addressBits = 0;
size_t length = sizeof(addressBits);
if (sysctlbyname("machdep.virtual_address_size", &addressBits, &length, nullptr, 0) != 0)
{
TRACE("sysctlbyname(machdep.virtual_address_size) FAILED %s (%d)\n", strerror(errno), errno);
return;
}
if (length != sizeof(addressBits) || addressBits == 0 || addressBits >= 64)
{
TRACE("sysctlbyname(machdep.virtual_address_size) returned unexpected value %u (length %zu)\n",
addressBits, length);
return;
}

m_addressableBits.version = 4;
m_addressableBits.lowAddressBits = addressBits;
m_addressableBits.highAddressBits = addressBits;
m_addressableBits.reserved = 0;

m_addressableBitsNote.cmd = LC_NOTE;
m_addressableBitsNote.cmdsize = sizeof(note_command);
static_assert(sizeof(AddressableBitsPayload) == 16);
static_assert(sizeof(m_addressableBitsNote.data_owner) > sizeof("addrable bits") - 1);
memset(m_addressableBitsNote.data_owner, 0, sizeof(m_addressableBitsNote.data_owner));
memcpy(m_addressableBitsNote.data_owner, "addrable bits", sizeof("addrable bits") - 1);
m_addressableBitsNote.size = sizeof(m_addressableBits);
m_hasAddressableBitsNote = true;
#endif
}

void
DumpWriter::BuildProcessMetadataNote()
{
Expand Down Expand Up @@ -230,6 +285,12 @@ DumpWriter::WriteHeader(uint64_t* pFileOffset)
header.sizeofcmds += segment.cmdsize;
}

if (m_hasAddressableBitsNote)
{
header.ncmds++;
header.sizeofcmds += m_addressableBitsNote.cmdsize;
}

header.ncmds++;
header.sizeofcmds += m_processMetadataNote.cmdsize;

Expand Down
14 changes: 14 additions & 0 deletions src/coreclr/debug/createdump/dumpwritermacho.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ struct ThreadCommand
#endif
};

// Payload of LLDB's version 4 "addrable bits" LC_NOTE: the number of valid
// address bits for low (user) and high (kernel) addresses.
struct AddressableBitsPayload
{
uint32_t version;
uint32_t lowAddressBits;
uint32_t highAddressBits;
uint32_t reserved;
};

class DumpWriter
{
private:
Expand All @@ -30,6 +40,9 @@ class DumpWriter
std::vector<ThreadCommand> m_threadLoadCommands;
note_command m_processMetadataNote{};
std::string m_processMetadata;
note_command m_addressableBitsNote{};
AddressableBitsPayload m_addressableBits{};
bool m_hasAddressableBitsNote = false;
BYTE m_tempBuffer[0x4000];

// no public copy constructor
Expand All @@ -45,6 +58,7 @@ class DumpWriter

private:
bool WriteDiagInfo(size_t size);
void BuildAddressableBitsNote();
void BuildProcessMetadataNote();
void BuildSegmentLoadCommands();
void BuildThreadLoadCommands();
Expand Down
Loading