Why the setjmp and longjmp I wrote can not work?



I wrote a program as follow:
#include <stdio.h>

typedef struct __myjmp_buf
{
int efp;
int epc;
}myjmp_buf;

int mysetjmp(myjmp_buf env)
{
int reval=0;
__asm__("movl %%ebp,%0":"=r"(env[0].efp));
__asm__("movl $1f,%0\n\t"
"1:"
:"=r"(env[0].epc));
return reval;
}

void mylongjmp(myjmp_buf env , int val)
{
__asm__("movl %1,-4(%0)\n\t"
"movl %0,%%ebp\n\t"
"jmp %2"
::"r"(env[0].efp),
"r"(val),
"r"(env[0].epc));
}

myjmp_buf buf;

int test()
{
int i=0;
i++;
mylongjmp(buf , 1);
return 0;
}

int main()
{
if(mysetjmp(buf))
{
printf("return success\n");
}
printf("pc:%x,fp:%x\n" , buf[0].epc , buf[0].efp);
printf("main address:%x\n" , main);
test();
exit(0);
}

When the computer execute jmp %2, and gives me a segment fault.
I do not know why the address I save is a invalid address, and wonder
to know what should I do if I want that mysetjmp and mylongjmp can work

like setjmp and longjmp.
There is no error to compile it in my system but a warning "indirect
jmp
without '*' ", and I do not know what the warning means.
My platform is x86, the system I use is Fedora core1, and I compile the

program with gcc.

.



Relevant Pages

  • Re: new order doubt
    ... Warning test2.c: 8 no type specified. ... Defaulting to int ... Tests probably come in several different types: this should compile ... Even Microsoft has problems with this obscure rules. ...
    (comp.lang.c)
  • Re: How does the compiler do with this code?
    ... an int. ... That's why you should always compile asking for errors and warnings: ... p.c:3: warning: implicit declaration of function `printf' ...
    (comp.os.linux.development.apps)
  • Re: Why is C compiler stubborn ?
    ... when I compile the code I get this warning from gcc (although it ... you are storing in a "pointer to int". ...
    (comp.lang.c)
  • Re: what does this warning mean ?
    ... Warning c:\tmp\long.c: 4 old-style function definition for 'main' ... Warning c:\tmp\long.c: 4 'int main' is a non-ANSI definition ... about being forced to compile code after quitting time on ... but it can issue diagnostics anyhow. ...
    (comp.lang.c)
  • Re: How does the compiler do with this code?
    ... So you get a declaration gratis: int printf; ... That's why you should always compile asking for errors and warnings: ... p.c:3: warning: implicit declaration of function `printf' ...
    (comp.os.linux.development.apps)