Skip to content

Commit 23e026a

Browse files
committed
Prepare for arm64e
1 parent 46b6893 commit 23e026a

1 file changed

Lines changed: 63 additions & 4 deletions

File tree

macOS/debugger.cpp

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ limitations under the License.
3535
#include <signal.h>
3636
#include <fcntl.h>
3737

38+
#ifdef __arm64e__
39+
#include <ptrauth.h>
40+
#endif
41+
3842
#include "macOS/debugger.h"
3943
#include "common.h"
4044

@@ -217,15 +221,16 @@ uint64_t* Debugger::GetPointerToRegister(Register r) {
217221
case X28:
218222
case X29:
219223
return &state->__x[r];
224+
#ifndef __arm64e__
220225
case PC:
221226
return &state->__pc;
222-
case CPSR:
223-
return (uint64_t*)&state->__cpsr;
224227
case LR:
225228
return &state->__lr;
226229
case SP:
227230
return &state->__sp;
228-
231+
#endif
232+
case CPSR:
233+
return (uint64_t*)&state->__cpsr;
229234
default:
230235
FATAL("Unimplemented register");
231236
}
@@ -273,6 +278,20 @@ uint64_t* Debugger::GetPointerToRegister(Register r) {
273278
}
274279

275280
size_t Debugger::GetRegister(Register r) {
281+
#ifdef __arm64e__
282+
ARCH_THREAD_STATE_T *state = (ARCH_THREAD_STATE_T*)(mach_exception->new_state);
283+
switch(r) {
284+
case PC:
285+
return (size_t)ptrauth_strip(state->__opaque_pc, ptrauth_key_function_pointer);
286+
case LR:
287+
return (size_t)ptrauth_strip(state->__opaque_lr, ptrauth_key_function_pointer);
288+
case SP:
289+
return (size_t)ptrauth_strip((void *)state->__opaque_sp, ptrauth_key_process_independent_data);
290+
default:
291+
break;
292+
}
293+
#endif
294+
276295
#ifdef ARM64
277296
if (r == CPSR) {
278297
uint32_t *reg_pointer = (uint32_t *)GetPointerToRegister(r);
@@ -284,13 +303,36 @@ size_t Debugger::GetRegister(Register r) {
284303
}
285304

286305
void Debugger::SetRegister(Register r, size_t value) {
306+
#ifdef __arm64e__
307+
ARCH_THREAD_STATE_T *state = (ARCH_THREAD_STATE_T*)(mach_exception->new_state);
308+
switch(r) {
309+
case PC:
310+
value = (size_t)ptrauth_strip((void *)value, ptrauth_key_function_pointer);
311+
value = (size_t)ptrauth_sign_unauthenticated((void*)value, ptrauth_key_function_pointer, 0);
312+
arm_thread_state64_set_pc_fptr(*state, (void *)value);
313+
return;
314+
case LR:
315+
value = (size_t)ptrauth_strip((void *)value, ptrauth_key_function_pointer);
316+
value = (size_t)ptrauth_sign_unauthenticated((void*)value, ptrauth_key_function_pointer, 0);
317+
arm_thread_state64_set_lr_fptr(*state, (void *)value);
318+
return;
319+
case SP:
320+
arm_thread_state64_set_sp(*state, (void *)value);
321+
return;
322+
default:
323+
break;
324+
}
325+
#endif
326+
287327
#ifdef ARM64
288328
if (r == CPSR) {
289329
if(value & 0xFFFFFFFF00000000) FATAL("32 bit value required");
290330
uint32_t *reg_pointer = (uint32_t *)GetPointerToRegister(r);
291331
*reg_pointer = (uint32_t)(value & 0xFFFFFFFF);
332+
return;
292333
}
293334
#endif
335+
294336
uint64_t *reg_pointer = GetPointerToRegister(r);
295337
*reg_pointer = value;
296338
}
@@ -1095,11 +1137,18 @@ void *Debugger::GetModuleEntrypoint(void *base_address) {
10951137
uint64_t file_vm_slide = (uint64_t)base_address - text_cmd->vmaddr;
10961138

10971139
free(load_commands_buffer);
1140+
1141+
#ifdef __arm64e__
1142+
return (void*)((uint64_t)state->__opaque_pc + file_vm_slide);
1143+
#else
1144+
10981145
#ifdef ARM64
10991146
return (void*)(state->__pc + file_vm_slide);
11001147
#else
11011148
return (void*)(state->__rip + file_vm_slide);
11021149
#endif
1150+
1151+
#endif
11031152
}
11041153

11051154
bool Debugger::IsDyld(void *base_address) {
@@ -1548,7 +1597,9 @@ void Debugger::PrintContext() {
15481597
if(ret != KERN_SUCCESS) continue;
15491598
#ifdef ARM64
15501599
printf("thread %d\n", i);
1600+
#ifndef __arm64e__
15511601
printf("pc: %llx\n", state.__pc);
1602+
#endif
15521603
printf(" x0: %16llx x1: %16llx x2: %16llx x3: %16llx\n", state.__x[0], state.__x[1], state.__x[2], state.__x[3]);
15531604
printf(" x4: %16llx x5: %16llx x6: %16llx x7: %16llx\n", state.__x[4], state.__x[5], state.__x[6], state.__x[7]);
15541605
printf(" x8: %16llx x9: %16llx x10: %16llx x11: %16llx\n", state.__x[8], state.__x[9], state.__x[10], state.__x[11]);
@@ -1557,10 +1608,18 @@ void Debugger::PrintContext() {
15571608
printf("x20: %16llx x21: %16llx x22: %16llx x23: %16llx\n", state.__x[20], state.__x[21], state.__x[22], state.__x[23]);
15581609
printf("x24: %16llx x25: %16llx x26: %16llx x27: %16llx\n", state.__x[24], state.__x[25], state.__x[26], state.__x[27]);
15591610
printf("x28: %16llx\n", state.__x[28]);
1611+
#ifndef __arm64e__
15601612
printf(" sp: %16llx fp: %16llx lr: %16llx cpsr: %8x\n\n", state.__sp, state.__fp, state.__lr, state.__cpsr);
1613+
#endif
15611614
printf("stack:\n");
15621615
uint64_t stack[100];
1563-
mach_target->ReadMemory(state.__sp, sizeof(stack), stack);
1616+
uint64_t sp;
1617+
#ifdef __arm64e__
1618+
sp = (uint64_t)ptrauth_strip(state.__opaque_sp, ptrauth_key_process_independent_data);
1619+
#else
1620+
sp = state.__sp;
1621+
#endif
1622+
mach_target->ReadMemory(sp, sizeof(stack), stack);
15641623
#else
15651624
printf("thread %d\n", i);
15661625
printf("rip:%llx\n", state.__rip);

0 commit comments

Comments
 (0)