Goto a specific Address in C

自作多情 提交于 2019-12-10 14:13:15

问题


How can I JMP to a specific address in C?

I want to use

goto 0x10080000

This is not working, is there other way I can change the address of Program Counter??


回答1:


You can cast the address to a function pointer and then jump into:

((void (*)(void))0x10008000)();

To make it a bit more clear:

typedef void (*func_t)(void);
...
((func_t)0x10008000)();

But this is a function, the compiler will emit a branch instruction that expect to return (then is up to you to make your function return or not). Also notice that the compiler will produce a code that expect to find a C function at the given address, about how the function arguments are given and returned.

If you need to emit a branch instruction without return you need to use inline assembly.




回答2:


In GCC you can use computed goto. I didn't try it myself, but it should work like this:

void *address = 0x10080000;
...
goto *address;



回答3:


Inline assembly instruction should work too:

asm("jump 0x10080000");



来源:https://stackoverflow.com/questions/19173493/goto-a-specific-address-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!