C++ 如何将程序执行跳转到C中的特定地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8158007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:06:07  来源:igfitidea点击:

How to jump the program execution to a specific address in C?

c++c

提问by Sam

I want the program to jump to a specific address in memory and continue execution from that address. I thought about using gotobut I don't have a label rather just an address in memory.

我希望程序跳转到内存中的特定地址并从该地址继续执行。我想过使用,goto但我没有标签,而只是内存中的地址。

There is no need to worry about return back from the jump address.

不用担心从跳转地址返回。

edit: using GCC compiler

编辑:使用 GCC 编译器

回答by Randall Cook

Inline assembly might be the easiest and most "elegant" solution, although doing this is highly unusual, unless you are writing a debugger or some specialized introspective system.

内联汇编可能是最简单和最“优雅”的解决方案,尽管这样做非常不寻常,除非您正在编写调试器或一些专门的内省系统。

Another option might be to declare a pointer to a void function (void (*foo)(void)), then set the pointer to contain your address, and then invoke it:

另一种选择可能是声明一个指向 void 函数 ( void (*foo)(void)) 的指针,然后将该指针设置为包含您的地址,然后调用它:

void (*foo)(void) = (void (*)())0x12345678;
foo();

There will be things pushed on the stack since the compiler thinks you are doing a subroutine call, but since you don't care about returning, this might work.

由于编译器认为您正在执行子例程调用,因此将有一些东西压入堆栈,但由于您不关心返回,这可能会起作用。

回答by Chris Dodd

gcc has an extension that allows jumping to an arbitrary address:

gcc 有一个允许跳转到任意地址的扩展:

void *ptr = (void *)0x1234567;  // a random memory address
goto *ptr;                      // jump there -- probably crash

This is pretty much the same as using a function pointer that you set to a fixed value, but it will actually use a jump instruction rather than a call instruction (so the stack won't be modified)

这与使用您设置为固定值的函数指针几乎相同,但它实际上将使用跳转指令而不是调用指令(因此堆栈不会被修改)

回答by Rob?

#include <stdio.h>
#include <stdlib.h>

void go(unsigned int addr) {
  (&addr)[-1] = addr;
}

int sub() {
  static int i;
  if(i++ < 10) printf("Hello %d\n", i);
  else exit(0);
  go((unsigned int)sub);
}

int main() {
  sub();
}

Of course, this invokes undefined behavior, is platform-dependent, assumes that code addresses are the same size as int, etc, etc.

当然,这会调用未定义的行为,是平台相关的,假设代码地址与 等的大小相同int

回答by Andro

It should look something like this:

它应该是这样的:

unsigned long address=0x80; 

void (*func_ptr)(void) = (void (*)(void))address;
func_ptr();

However, it is not a very safe operation, jumping to some unknown address will probably result in a crash!

但是,这不是一个很安全的操作,跳转到某个未知地址可能会导致崩溃!

回答by U007D

Since the question has a C++ tag, here's an example of a C++call to a function with a signature like main()--int main(int argc, char* argv[]):

由于这个问题有一个 C++ 标签,下面是一个C++调用具有像 main()-- 签名的函数的示例int main(int argc, char* argv[])

int main(int argc, char* argv[])
{
    auto funcAddr = 0x12345678; //or use &main...
    auto result = reinterpret_cast<int (*)(int, char**)>(funcAddr)(argc, argv);
}

回答by ?lkerK

This is what I am using for my bootstrap loader(MSP430AFE253,Compiler = gcc,CodeCompeserStudio);

这就是我用于引导加载程序的内容(MSP430AFE253,Compiler = gcc,CodeCompeserStudio);

#define API_RESET_VECT 0xFBFE
#define JUMP_TO_APP()  {((void (*)()) (*(uint16_t*)API_RESET_VECT)) ();}

回答by Bahman

I Propos this code:

我建议这个代码:

asm(
"LDR R0,=0x0a0000\n\t" /* Or 0x0a0000 for the base Addr. */
"LDR R0, [R0, #4]\n\t" /* Vector+4 for PC */
"BX R0"
);

回答by Taylor Price

Do you have control of the code at the address that you intend to jump to? Is this C or C++?

您是否可以控制要跳转到的地址处的代码?这是 C 还是 C++?

I hesitantly suggest setjmp() / longjmp()if you're using C and can run setjmp()where you need to jump back to. That being said, you've got to be VERY careful with these.

我犹豫地建议setjmp() / longjmp()你是否使用 C 并且可以运行setjmp()你需要跳回的地方。话虽如此,你必须非常小心这些。

As for C++, see the following discussion about longjmp()shortcutting exception handling and destructors destructors. This would make me even more hesitant to suggest it's use in C++.

至于 C++,请参阅以下有关longjmp()快捷方式异常处理和析构函数析构函数的讨论。这会让我更加犹豫是否建议在 C++ 中使用它。

C++: Safe to use longjmp and setjmp?

C++:使用 longjmp 和 setjmp 安全吗?