以下代码不能按预期工作,但希望说明我的尝试:
long foo (int a, int b) {
return a + b;
}
void call_foo_from_stack (void) {
/* reserve space on the stack to store foo's code */
char code[sizeof(*foo)];
/* have a pointer to the beginning of the code */
long (*fooptr)(int, int) = (long (*)(int, int)) code;
/* copy foo's code to the stack */
memcpy(code, foo, sizeof(*foo));
/* execute foo from the stack */
fooptr(3, 5);
}
Run Code Online (Sandbox Code Playgroud)
显然,sizeof(*foo)不返回foo()函数代码的大小.
我知道在某些CPU上执行堆栈是受限制的(或者至少在设置了限制标志的情况下).除了最终可以存储在堆栈中的GCC嵌套函数之外,还有一种方法可以在标准C中实现吗?
我想在Linux上用c做一个简单的即时编译器.
我如何分配内存,以便我可以写出原始x86代码并执行它作为任何其他功能?
我正在编写一个程序来加载和执行文件中的代码.但是我遇到了一个问题:"写"系统调用不起作用.代码成功加载并执行,但不在屏幕上显示任何文本.
加载代码的程序:
#include < stdio.h >
#include < stdlib.h >
int main(int argc,char* argv[])
{
unsigned int f_size = 0;
unsigned char* code_buf = NULL;
void (*func_call)(void) = NULL;
if(argc < 2)
{
printf("Usage: %s <FILE>\n",argv[0]);
return 1;
}
FILE* fp = fopen(argv[1],"rb");
if(!fp)
{
printf("Error while opening this file: %s\n",argv[1]);
return 1;
}
unsigned int fsize = 0;
fseek(fp,0,SEEK_END);
fsize = ftell(fp);
fseek(fp,0,SEEK_SET);
if(fsize < 4)
{
printf("Code size must be > 4 bytes\n");
return 1;
}
code_buf = …Run Code Online (Sandbox Code Playgroud) 我有一个包含 326 个部分的 C++ gcc 泄漏程序,如下所示
33300000-33500000 rwxp 33300000 00:00 0
Size: 2048 kB
Rss: 620 kB
Shared_Clean: 0 kB
Shared_Dirty: 0 kB
Private_Clean: 244 kB
Private_Dirty: 376 kB
Run Code Online (Sandbox Code Playgroud)
我想知道什么样的分配会导致向程序添加 2MB 可写代码段。通常我看到这样的部分被用作线程的堆栈内存,但它们有 10 MB 大。
我想在 x64 Linux 上编写自己的二进制代码加载器。将来我希望能够自己执行链接步骤,从而能够从.o对象文件调用代码。但现在,我想从已链接的可执行二进制文件中调用函数。
为了创建一些可以从“外部”调用的函数,我从以下源代码开始:
void foo(void)
{
int a = 2;
int b = 3;
a + b;
}
int main(void)
{
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是foo()我想使用加载程序调用的函数。使用以下命令链
gcc -o /tmp/main main.c
strip -s /tmp/main
objdump -D /tmp/main
Run Code Online (Sandbox Code Playgroud)
我获得了该函数的汇编代码foo(),如下所示:
...
0000000000001125 <foo>:
1125: 55 push %rbp
1126: 48 89 e5 mov %rsp,%rbp
1129: c7 45 fc 02 00 00 00 movl $0x2,-0x4(%rbp)
1130: c7 45 f8 03 00 00 00 movl $0x3,-0x8(%rbp)
1137: 90 …Run Code Online (Sandbox Code Playgroud) c ×4
linux ×3
assembly ×2
compilation ×1
executable ×1
gdb ×1
linker ×1
malloc ×1
memory ×1
memory-leaks ×1
shellcode ×1
stack ×1
system-calls ×1