问题
I have following dispatch code for my user level thread library.
The code can pass GCC and runs correctly without optimization, but if I choose -O1 optimization (also higher levels), when run the code, program generates segmentation fault.
Basically the function does save context and jump to next context.
void __attribute__ ((noinline)) __lwt_dispatch(lwt_context *curr, lwt_context *next)
{
__asm__ __volatile
(
"mov 0xc(%ebp),%eax\n\t"
"mov 0x4(%eax),%ecx\n\t"
"mov (%eax),%edx\n\t"
"mov 0x8(%ebp),%eax\n\t"
"add $0x4,%eax\n\t"
"mov 0x8(%ebp),%ebx\n\t"
"push %ebp\n\t"
"push %ebx\n\t"
"mov %esp,(%eax)\n\t"
"movl $return,(%ebx)\n\t"
"mov %ecx,%esp\n\t"
"jmp *%edx\n\t"
"return: pop %ebx\n\t"
"pop %ebp\n\t"
);
}
回答1:
Thanks for help, I figured out some ways to solve it.
Normally compile this function as a separate .o file then use O3 to optimize it with other files.
using inline assembly is much easier and simpler than this function. Like below:
int foo = 10, bar = 15; asm volatile("addl %%ebx,%%eax" :"=a"(foo) :"a"(foo), "b"(bar) ); printf("foo+bar=%d\n", foo);
Another post has helped me figuring out labeling problem, see here: Labels in GCC inline assembly
来源:https://stackoverflow.com/questions/42019107/how-to-let-inline-assembly-pass-o1-optimization