0

In setjmp.h library in linux system jmp_buf is encrypted to decrypt it we use mangle function

*/static long int i64_ptr_mangle(long int p) {
    long int ret;
    asm(" mov %1, %%rax;\n"
        " xor %%fs:0x30, %%rax;"
        " rol $0x11, %%rax;"
        " mov %%rax, %0;"
        : "=r"(ret)
        : "r"(p)
        : "%rax"
    );
    return ret;
}

 

I need to save the context and change the stack pointer, base pointer and program counter in jmp_buffer any alternative to this function that I can use. I am trying to build basic thread library can't head around this. I can't use ucontext.h .

no ai please
  • 732
  • 3
  • 11
  • 24

1 Answers1

0

You might as well roll your own version of setjmp/longjmp; even if you reverse engineered that mess, your result will be more fragile than a proper version. You will need to have a peek at the calling conventions for your environment, but mainly something like:

mov  4(%esp), %eax
mov  %ebx, _BX(%eax)
mov  %esi, _SI(%eax)
mov  %edi, _DI(%eax)
mov  %ebp, _BP(%eax)
pushf; pop _FL(%eax)
mov  %esp, _SP(%eax)
pop _PC(%eax)
xor %eax,%eax
ret

loadctx:
mov  4(%esp), %edx
mov  8(%esp), %eax
mov  _BX(%edx), %ebx
...
push _FL(%edx)
popf
mov  _SP(%edx), %esp
jmp _PC(%edx)

Then you define your register layout maybe like:

#define _PC  0
#define _SP  4
#define _FL  8
...

This should work in a dated compiler, like gcc2.x as is. More modern compilers have been, uh, enhanced, to rely on thead local storage(TLS) and the like. You may have to add bits to your context. Another enhancement is stack checking, typically layered on TLS. Even if you disable stack checking, it is possible that libraries you use will rely on it, so you will have to swap the appropriate entries.

mevets
  • 10,070
  • 1
  • 21
  • 33