Skip to content

Commit

Permalink
libunwind: Simplify _unw_{get,set}context under c18n
Browse files Browse the repository at this point in the history
Assembly stubs for _rtld_unw_{get,set}context are removed. Instead, turn
calls to these functions into no-ops when they are not defined by RTLD.
  • Loading branch information
dpgao committed Jun 19, 2024
1 parent 45c9be3 commit 3af16f4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 38 deletions.
42 changes: 13 additions & 29 deletions contrib/subrepo-cheri-libunwind/src/UnwindRegistersRestore.S
Original file line number Diff line number Diff line change
Expand Up @@ -703,25 +703,6 @@ Lnovec:

#elif defined(__aarch64__)

//
// extern "C" void __rtld_unw_setcontext(void *c0, void *c1,
// void *rcsp, void **sealed_ecsp);
//
#if defined(__CHERI_PURE_CAPABILITY__)
DEFINE_LIBUNWIND_FUNCTION(__rtld_unw_setcontext)
mov c16, c2
ldp c2, c3, [c3, #(-0x210 + 0x20)]
mov csp, c16
#ifdef __ARM_MORELLO_PURECAP_BENCHMARK_ABI
and x30, x30, #~1
ret x30
#else
ret
#endif
END_LIBUNWIND_FUNCTION(__rtld_unw_setcontext)
WEAK_ALIAS(__rtld_unw_setcontext, _rtld_unw_setcontext)
#endif

//
// extern "C" void __libunwind_Registers_arm64_jumpto(Registers_arm64 *);
//
Expand All @@ -731,8 +712,14 @@ WEAK_ALIAS(__rtld_unw_setcontext, _rtld_unw_setcontext)
.p2align 2
DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_arm64_jumpto)
#ifdef __CHERI_PURE_CAPABILITY__
ldr c1, [c0, #0x1f0] // Pass the target untrusted stack pointer
ldr c2, [c0, #0x210] // Pass the target trusted stack pointer
bl _rtld_unw_setcontext
DEFINE_LIBUNWIND_FUNCTION(__rtld_unw_setcontext)
WEAK_ALIAS(__rtld_unw_setcontext, _rtld_unw_setcontext)

// skip restore of c0,c1 for now
// also skip restoring c2 and c3 because they will get clobbered later on
ldp c2, c3, [c0, #0x020]
ldp c4, c5, [c0, #0x040]
ldp c6, c7, [c0, #0x060]
ldp c8, c9, [c0, #0x080]
Expand Down Expand Up @@ -772,17 +759,14 @@ DEFINE_LIBUNWIND_FUNCTION(__libunwind_Registers_arm64_jumpto)
// context struct, because it is allocated on the stack, and an exception
// could clobber the de-allocated portion of the stack after csp has been
// restored.
ldr c2, [c0, #0x1f0]
add c3, c0, #0x210
ldp c0, c1, [c0, #0x000]
// XXX: variant PCS is not yet supported by rtld, work around it
// using a function pointer.
adrp c16, :got:_rtld_unw_setcontext
ldr c16, [c16, :got_lo12:_rtld_unw_setcontext]
ldr c16, [c0, #0x1f0]
ldp c0, c1, [c0, #0x000] // restore c0,c1
mov csp,c16 // restore csp
#ifdef __ARM_MORELLO_PURECAP_BENCHMARK_ABI
br x16
and x30, x30, #~1
ret x30 // jump to pc
#else
br c16
ret // jump to pcc
#endif
#else
// skip restore of x0,x1 for now
Expand Down
17 changes: 8 additions & 9 deletions contrib/subrepo-cheri-libunwind/src/UnwindRegistersSave.S
Original file line number Diff line number Diff line change
Expand Up @@ -837,15 +837,6 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)

#elif defined(__aarch64__)

#if defined(__CHERI_PURE_CAPABILITY__)
DEFINE_LIBUNWIND_FUNCTION(__rtld_unw_getcontext)
mov c2, csp
str c2, [c1]
ret c30
END_LIBUNWIND_FUNCTION(__rtld_unw_getcontext)
WEAK_ALIAS(__rtld_unw_getcontext, _rtld_unw_getcontext)
#endif

//
// extern int __unw_getcontext(unw_context_t* thread_state)
//
Expand Down Expand Up @@ -899,6 +890,14 @@ DEFINE_LIBUNWIND_FUNCTION(__unw_getcontext)
str d31, [c0, #0x0f8]
mov x0, #0 // return UNW_ESUCCESS
b _rtld_unw_getcontext
DEFINE_LIBUNWIND_FUNCTION(__rtld_unw_getcontext)
WEAK_ALIAS(__rtld_unw_getcontext, _rtld_unw_getcontext)
#ifdef __ARM_MORELLO_PURECAP_BENCHMARK_ABI
and x30, x30, #~1
ret x30
#else
ret
#endif
#else
stp x0, x1, [x0, #0x000]
stp x2, x3, [x0, #0x010]
Expand Down

0 comments on commit 3af16f4

Please sign in to comment.