39 size_t value_size)
noexcept {
40 if (!ctx || !out_value || value_size == 0 || value_size >
sizeof(uint64_t))
43 const auto &uc = *
static_cast<const ucontext_t *
>(ctx);
49#if defined(__x86_64__)
53 switch (dwarf_reg_index) {
55 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RAX]);
59 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RDX]);
63 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RCX]);
67 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RBX]);
71 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RSI]);
75 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RDI]);
79 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RBP]);
83 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RSP]);
87 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_R8]);
91 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_R9]);
95 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_R10]);
99 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_R11]);
103 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_R12]);
107 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_R13]);
111 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_R14]);
115 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_R15]);
119 value =
static_cast<uint64_t
>(uc.uc_mcontext.gregs[REG_RIP]);
126#elif defined(__i386__)
128 switch (dwarf_reg_index) {
130 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_EAX]));
134 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_ECX]));
138 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_EDX]));
142 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_EBX]));
146 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_ESP]));
150 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_EBP]));
154 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_ESI]));
158 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_EDI]));
162 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.gregs[REG_EIP]));
169#elif defined(__aarch64__)
172 if (dwarf_reg_index <= 30) {
173 value = uc.uc_mcontext.regs[dwarf_reg_index];
176 value = uc.uc_mcontext.sp;
179 value = uc.uc_mcontext.pc;
183#elif defined(__arm__)
188 switch (dwarf_reg_index) {
190 value = uc.uc_mcontext.arm_r0;
194 value = uc.uc_mcontext.arm_r1;
198 value = uc.uc_mcontext.arm_r2;
202 value = uc.uc_mcontext.arm_r3;
206 value = uc.uc_mcontext.arm_r4;
210 value = uc.uc_mcontext.arm_r5;
214 value = uc.uc_mcontext.arm_r6;
218 value = uc.uc_mcontext.arm_r7;
222 value = uc.uc_mcontext.arm_r8;
226 value = uc.uc_mcontext.arm_r9;
230 value = uc.uc_mcontext.arm_r10;
234 value = uc.uc_mcontext.arm_fp;
238 value = uc.uc_mcontext.arm_ip;
242 value = uc.uc_mcontext.arm_sp;
246 value = uc.uc_mcontext.arm_lr;
250 value = uc.uc_mcontext.arm_pc;
254 value = uc.uc_mcontext.arm_cpsr;
261#elif defined(__riscv) && (__riscv_xlen == 64)
267 }
else if (dwarf_reg_index >= 1 && dwarf_reg_index <= 31) {
268 value =
static_cast<uint64_t
>(uc.uc_mcontext.__gregs[dwarf_reg_index]);
271 value =
static_cast<uint64_t
>(uc.uc_mcontext.__gregs[0]);
276#elif defined(__FreeBSD__)
278#if defined(__amd64__) || defined(__x86_64__)
279 switch (dwarf_reg_index) {
281 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rax);
285 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rdx);
289 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rcx);
293 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rbx);
297 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rsi);
301 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rdi);
305 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rbp);
309 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rsp);
313 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_r8);
317 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_r9);
321 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_r10);
325 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_r11);
329 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_r12);
333 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_r13);
337 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_r14);
341 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_r15);
345 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_rip);
352#elif defined(__i386__)
353 switch (dwarf_reg_index) {
355 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_eax));
359 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_ecx));
363 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_edx));
367 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_ebx));
371 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_esp));
375 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_ebp));
379 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_esi));
383 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_edi));
387 value =
static_cast<uint64_t
>(
static_cast<uint32_t
>(uc.uc_mcontext.mc_eip));
394#elif defined(__aarch64__)
398 if (dwarf_reg_index <= 29) {
399 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_gpregs.gp_x[dwarf_reg_index]);
401 }
else if (dwarf_reg_index == 30) {
402 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_gpregs.gp_lr);
405 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_gpregs.gp_sp);
408 value =
static_cast<uint64_t
>(uc.uc_mcontext.mc_gpregs.gp_elr);
412#elif defined(__arm__)
416 if (dwarf_reg_index <= 15) {
417 value =
static_cast<uint64_t
>(uc.uc_mcontext.__gregs[dwarf_reg_index]);
421#elif defined(__riscv) && (__riscv_xlen == 64)
424 const auto &gp = uc.uc_mcontext.mc_gpregs;
429 value =
static_cast<uint64_t
>(gp.gp_ra);
432 value =
static_cast<uint64_t
>(gp.gp_sp);
435 value =
static_cast<uint64_t
>(gp.gp_gp);
438 value =
static_cast<uint64_t
>(gp.gp_tp);
450 value =
static_cast<uint64_t
>(gp.gp_s[2 + (dwarf_reg_index -
dwarf::riscv::s2)]);
453 value =
static_cast<uint64_t
>(gp.gp_t[3 + (dwarf_reg_index -
dwarf::riscv::t3)]);
456 value =
static_cast<uint64_t
>(gp.gp_sepc);
465 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
467 std::memcpy(out_value, &value, value_size);
469 RELOCO_END_UNSAFE_BUFFER_USAGE;
488 size_t value_size)
noexcept {
489 if (!ctx || !in_value || value_size == 0 || value_size >
sizeof(uint64_t))
493 RELOCO_BEGIN_UNSAFE_BUFFER_USAGE;
495 std::memcpy(&value, in_value, value_size);
496 auto &uc = *
static_cast<ucontext_t *
>(ctx);
498 RELOCO_END_UNSAFE_BUFFER_USAGE;
502#if defined(__linux__)
504#if defined(__x86_64__)
505 switch (dwarf_reg_index) {
507 uc.uc_mcontext.gregs[REG_RAX] =
static_cast<greg_t
>(value);
511 uc.uc_mcontext.gregs[REG_RDX] =
static_cast<greg_t
>(value);
515 uc.uc_mcontext.gregs[REG_RCX] =
static_cast<greg_t
>(value);
519 uc.uc_mcontext.gregs[REG_RBX] =
static_cast<greg_t
>(value);
523 uc.uc_mcontext.gregs[REG_RSI] =
static_cast<greg_t
>(value);
527 uc.uc_mcontext.gregs[REG_RDI] =
static_cast<greg_t
>(value);
531 uc.uc_mcontext.gregs[REG_RBP] =
static_cast<greg_t
>(value);
535 uc.uc_mcontext.gregs[REG_RSP] =
static_cast<greg_t
>(value);
539 uc.uc_mcontext.gregs[REG_R8] =
static_cast<greg_t
>(value);
543 uc.uc_mcontext.gregs[REG_R9] =
static_cast<greg_t
>(value);
547 uc.uc_mcontext.gregs[REG_R10] =
static_cast<greg_t
>(value);
551 uc.uc_mcontext.gregs[REG_R11] =
static_cast<greg_t
>(value);
555 uc.uc_mcontext.gregs[REG_R12] =
static_cast<greg_t
>(value);
559 uc.uc_mcontext.gregs[REG_R13] =
static_cast<greg_t
>(value);
563 uc.uc_mcontext.gregs[REG_R14] =
static_cast<greg_t
>(value);
567 uc.uc_mcontext.gregs[REG_R15] =
static_cast<greg_t
>(value);
571 uc.uc_mcontext.gregs[REG_RIP] =
static_cast<greg_t
>(value);
578#elif defined(__i386__)
579 switch (dwarf_reg_index) {
581 uc.uc_mcontext.gregs[REG_EAX] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
585 uc.uc_mcontext.gregs[REG_ECX] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
589 uc.uc_mcontext.gregs[REG_EDX] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
593 uc.uc_mcontext.gregs[REG_EBX] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
597 uc.uc_mcontext.gregs[REG_ESP] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
601 uc.uc_mcontext.gregs[REG_EBP] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
605 uc.uc_mcontext.gregs[REG_ESI] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
609 uc.uc_mcontext.gregs[REG_EDI] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
613 uc.uc_mcontext.gregs[REG_EIP] =
static_cast<greg_t
>(
static_cast<uint32_t
>(value));
620#elif defined(__aarch64__)
621 if (dwarf_reg_index <= 30) {
622 uc.uc_mcontext.regs[dwarf_reg_index] = value;
625 uc.uc_mcontext.sp = value;
628 uc.uc_mcontext.pc = value;
632#elif defined(__arm__)
633 switch (dwarf_reg_index) {
635 uc.uc_mcontext.arm_r0 =
static_cast<uint32_t
>(value);
639 uc.uc_mcontext.arm_r1 =
static_cast<uint32_t
>(value);
643 uc.uc_mcontext.arm_r2 =
static_cast<uint32_t
>(value);
647 uc.uc_mcontext.arm_r3 =
static_cast<uint32_t
>(value);
651 uc.uc_mcontext.arm_r4 =
static_cast<uint32_t
>(value);
655 uc.uc_mcontext.arm_r5 =
static_cast<uint32_t
>(value);
659 uc.uc_mcontext.arm_r6 =
static_cast<uint32_t
>(value);
663 uc.uc_mcontext.arm_r7 =
static_cast<uint32_t
>(value);
667 uc.uc_mcontext.arm_r8 =
static_cast<uint32_t
>(value);
671 uc.uc_mcontext.arm_r9 =
static_cast<uint32_t
>(value);
675 uc.uc_mcontext.arm_r10 =
static_cast<uint32_t
>(value);
679 uc.uc_mcontext.arm_fp =
static_cast<uint32_t
>(value);
683 uc.uc_mcontext.arm_ip =
static_cast<uint32_t
>(value);
687 uc.uc_mcontext.arm_sp =
static_cast<uint32_t
>(value);
691 uc.uc_mcontext.arm_lr =
static_cast<uint32_t
>(value);
695 uc.uc_mcontext.arm_pc =
static_cast<uint32_t
>(value);
699 uc.uc_mcontext.arm_cpsr =
static_cast<uint32_t
>(value);
706#elif defined(__riscv) && (__riscv_xlen == 64)
711 }
else if (dwarf_reg_index >= 1 && dwarf_reg_index <= 31) {
712 uc.uc_mcontext.__gregs[dwarf_reg_index] =
static_cast<unsigned long>(value);
715 uc.uc_mcontext.__gregs[0] =
static_cast<unsigned long>(value);
720#elif defined(__FreeBSD__)
722#if defined(__amd64__) || defined(__x86_64__)
723 switch (dwarf_reg_index) {
725 uc.uc_mcontext.mc_rax =
static_cast<decltype(uc.uc_mcontext.mc_rax)
>(value);
729 uc.uc_mcontext.mc_rdx =
static_cast<decltype(uc.uc_mcontext.mc_rdx)
>(value);
733 uc.uc_mcontext.mc_rcx =
static_cast<decltype(uc.uc_mcontext.mc_rcx)
>(value);
737 uc.uc_mcontext.mc_rbx =
static_cast<decltype(uc.uc_mcontext.mc_rbx)
>(value);
741 uc.uc_mcontext.mc_rsi =
static_cast<decltype(uc.uc_mcontext.mc_rsi)
>(value);
745 uc.uc_mcontext.mc_rdi =
static_cast<decltype(uc.uc_mcontext.mc_rdi)
>(value);
749 uc.uc_mcontext.mc_rbp =
static_cast<decltype(uc.uc_mcontext.mc_rbp)
>(value);
753 uc.uc_mcontext.mc_rsp =
static_cast<decltype(uc.uc_mcontext.mc_rsp)
>(value);
757 uc.uc_mcontext.mc_r8 =
static_cast<decltype(uc.uc_mcontext.mc_r8)
>(value);
761 uc.uc_mcontext.mc_r9 =
static_cast<decltype(uc.uc_mcontext.mc_r9)
>(value);
765 uc.uc_mcontext.mc_r10 =
static_cast<decltype(uc.uc_mcontext.mc_r10)
>(value);
769 uc.uc_mcontext.mc_r11 =
static_cast<decltype(uc.uc_mcontext.mc_r11)
>(value);
773 uc.uc_mcontext.mc_r12 =
static_cast<decltype(uc.uc_mcontext.mc_r12)
>(value);
777 uc.uc_mcontext.mc_r13 =
static_cast<decltype(uc.uc_mcontext.mc_r13)
>(value);
781 uc.uc_mcontext.mc_r14 =
static_cast<decltype(uc.uc_mcontext.mc_r14)
>(value);
785 uc.uc_mcontext.mc_r15 =
static_cast<decltype(uc.uc_mcontext.mc_r15)
>(value);
789 uc.uc_mcontext.mc_rip =
static_cast<decltype(uc.uc_mcontext.mc_rip)
>(value);
796#elif defined(__i386__)
797 switch (dwarf_reg_index) {
799 uc.uc_mcontext.mc_eax =
static_cast<decltype(uc.uc_mcontext.mc_eax)
>(
static_cast<uint32_t
>(value));
803 uc.uc_mcontext.mc_ecx =
static_cast<decltype(uc.uc_mcontext.mc_ecx)
>(
static_cast<uint32_t
>(value));
807 uc.uc_mcontext.mc_edx =
static_cast<decltype(uc.uc_mcontext.mc_edx)
>(
static_cast<uint32_t
>(value));
811 uc.uc_mcontext.mc_ebx =
static_cast<decltype(uc.uc_mcontext.mc_ebx)
>(
static_cast<uint32_t
>(value));
815 uc.uc_mcontext.mc_esp =
static_cast<decltype(uc.uc_mcontext.mc_esp)
>(
static_cast<uint32_t
>(value));
819 uc.uc_mcontext.mc_ebp =
static_cast<decltype(uc.uc_mcontext.mc_ebp)
>(
static_cast<uint32_t
>(value));
823 uc.uc_mcontext.mc_esi =
static_cast<decltype(uc.uc_mcontext.mc_esi)
>(
static_cast<uint32_t
>(value));
827 uc.uc_mcontext.mc_edi =
static_cast<decltype(uc.uc_mcontext.mc_edi)
>(
static_cast<uint32_t
>(value));
831 uc.uc_mcontext.mc_eip =
static_cast<decltype(uc.uc_mcontext.mc_eip)
>(
static_cast<uint32_t
>(value));
838#elif defined(__aarch64__)
839 if (dwarf_reg_index <= 29) {
840 uc.uc_mcontext.mc_gpregs.gp_x[dwarf_reg_index] =
841 static_cast<std::remove_reference_t<decltype(uc.uc_mcontext.mc_gpregs.gp_x[0])
>>(value);
843 }
else if (dwarf_reg_index == 30) {
844 uc.uc_mcontext.mc_gpregs.gp_lr =
static_cast<decltype(uc.uc_mcontext.mc_gpregs.gp_lr)
>(value);
847 uc.uc_mcontext.mc_gpregs.gp_sp =
static_cast<decltype(uc.uc_mcontext.mc_gpregs.gp_sp)
>(value);
850 uc.uc_mcontext.mc_gpregs.gp_elr =
static_cast<decltype(uc.uc_mcontext.mc_gpregs.gp_elr)
>(value);
854#elif defined(__arm__)
855 if (dwarf_reg_index <= 15) {
856 uc.uc_mcontext.__gregs[dwarf_reg_index] =
static_cast<__greg_t
>(value);
860#elif defined(__riscv) && (__riscv_xlen == 64)
861 auto &gp = uc.uc_mcontext.mc_gpregs;
866 gp.gp_ra =
static_cast<decltype(gp.gp_ra)
>(value);
869 gp.gp_sp =
static_cast<decltype(gp.gp_sp)
>(value);
872 gp.gp_gp =
static_cast<decltype(gp.gp_gp)
>(value);
875 gp.gp_tp =
static_cast<decltype(gp.gp_tp)
>(value);
878 gp.gp_t[dwarf_reg_index -
dwarf::riscv::t0] =
static_cast<std::remove_reference_t<decltype(gp.gp_t[0])
>>(value);
881 gp.gp_s[dwarf_reg_index -
dwarf::riscv::s0] =
static_cast<std::remove_reference_t<decltype(gp.gp_s[0])
>>(value);
884 gp.gp_a[dwarf_reg_index -
dwarf::riscv::a0] =
static_cast<std::remove_reference_t<decltype(gp.gp_a[0])
>>(value);
888 static_cast<std::remove_reference_t<decltype(gp.gp_s[0])
>>(value);
892 static_cast<std::remove_reference_t<decltype(gp.gp_t[0])
>>(value);
895 gp.gp_sepc =
static_cast<decltype(gp.gp_sepc)
>(value);