GCC 将标签作为函数调用
一切都在标题中。 由于某些原因我必须这样做。
但是当我编译我的代码时,GCC(或 GAS 也许...)显示以下错误:
.../Temp/cc1C1fjs.s:19: Error:立即操作数非法,绝对跳转
代码:
int main ( int argc, char **argv )
{
/* Some code */
( (void(*)()) &&label)();
/* Some code */
return 0;
label:
asm ("push %ebp");
asm ("mov %esp,%ebp");
/* Some code */
printf("Hello world");
asm ("leave");
asm("ret");
}
我确信这应该之所以有效,是因为我尝试使用 CreateThread 函数(我在 Windows 下)创建一个线程,指定标签地址作为入口点,并且它工作得很好。 那么如何确保编译器接受这个语法呢? 或者还有其他方法可以做到这一点?
All is in the title.
For some reasons I have to do it like this.
But when I compile my code, GCC (or GAS maybe...) displays the following error:
.../Temp/cc1C1fjs.s:19: Error: immediate operand illegal with absolute jump
Code:
int main ( int argc, char **argv )
{
/* Some code */
( (void(*)()) &&label)();
/* Some code */
return 0;
label:
asm ("push %ebp");
asm ("mov %esp,%ebp");
/* Some code */
printf("Hello world");
asm ("leave");
asm("ret");
}
I'm sure that this should works because I tried to create a thread using CreateThread function (I'm under windows) specifing as entry point the address of label, and it works perfectly well.
So how can I ensure that the compiler accepting this syntax?
Or there is anothers ways for doing that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我没有适合您的解决方案,但我确实有一些建议:
gcc -S file.c
并查看第 19 行,看看您是否可以发现实际问题是什么。.s
文件的其余部分,看看是否有任何明显错误。例如,我的gcc
版本似乎决定return 0
之后的所有内容都是死代码,因此您的asm
代码和>printf
实际上将其发送到汇编器。I don't have a solution for you, but I do have a couple of suggestions:
gcc -S file.c
and look at line #19 to see if you can spot what the actual problem is..s
file to see if anything is obviously amiss. For example, my version ofgcc
seems to decide that everything afterreturn 0
is dead code, so none of yourasm
code nor theprintf
actually make it to the assembler.我修复了部分问题:
@aix 你说得对,GCC 删除
主要功能的一切
在
return 0;
之后,我修复了这个问题将其替换为
现在生成我的标签后的代码。
然后运行
gcc -S file.c
gcc file.s -o file.exe
,当然它显示错误并在错误行是
call *$L2
(L2 是我的 c 文件中的标签)。有用
将其替换为
调用 L2
。现在我的标签后面和我在 main 中调用之后的代码是
执行并且程序正确
以状态 0 终止。
但我不想每次编译时都必须这样做。
GCC 写的是
call *$L2
而不是call L2
正常吗?I fixed a part of the problem:
@aix you have right, GCC remove
everything of the main function
after
return 0;
, I fixed thisreplacing it by
Now the code after my label is generated.
Running
gcc -S file.c
thengcc file.s -o file.exe
, of course it displays the error and atthe error line there is
call *$L2
(L2 is label in my c file). It works
by replacing it by
call L2
.Now the code after my label and after my call in main is
executed and the program properly
terminates with state 0.
But I don't want to have to do that each time I will compile.
Is it normal that GCC write
call *$L2
rather thancall L2
?