小编Sba*_*cDY的帖子

如何从 C / 汇编中的内存地址执行函数?

我有以下汇编函数(已用 objdump 显示)

0000000000000000 <add>:
   0:   b8 06 00 00 00          mov    $0x6,%eax
   5:   c3                      retq  
Run Code Online (Sandbox Code Playgroud)

现在在 CI 中做了以下代码:

#include <stdio.h>

typedef int (*funcp) (int x);

unsigned char foo[] = {0xb8,0x06,0x00,0x00,0x00,0xc3};

int main(void)
{
  int i;
  funcp f = (funcp)foo;
  i = (*f);
  
  printf("exit = %d\n", i);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在全局变量 foo 中,我在汇编中输入了我的函数的内存地址并尝试执行它,但它没有按预期返回 6。如何为它们的内存地址执行函数?此外,我可以在哪里研究更多关于该主题的信息?

obs:有时我会遇到 Segmentation fault (core dumped) 错误

c linux x86 assembly disassembly

5
推荐指数
1
解决办法
105
查看次数

标签 统计

assembly ×1

c ×1

disassembly ×1

linux ×1

x86 ×1