SCS是Stack Chain Structure的缩写,用于管理栈帧的结构。在arm64架构上,SCS包含五个连续的栈帧,在栈下面的区域,用寄存器x18指向SCS的底部。可以使用下面的代码来获取x18寄存器中的SCS栈顶指针:
#include
#include
void* get_scs_top(void) {
void* top;
// Get current stack pointer.
__asm__ volatile ("mov %0, sp" : "=r" (top));
// Round up to next SCS alignment boundary.
uintptr_t scs_align = 16 * sizeof(void*);
uintptr_t top_int = (uintptr_t)top;
uintptr_t scs_bot_int = (top_int + scs_align - 1) & -scs_align;
// Calculate SCS top pointer.
size_t num_frames = 5;
size_t scs_size = num_frames * 16 * sizeof(void*);
top_int = scs_bot_int + scs_size;
top = (void*)top_int;
return top;
}