尝试使用 gdb 从共享对象文件执行函数时出现分段错误

segmentation fault when trying to execute a function from a shared object file with gdb

我正在尝试使用 gdb 从 .so 文件中调用一个函数。

我写了一个简单的test.c来简化问题:

#include<stdio.h>

void asdasdasd()
{
    printf("asdasdasd");
}

然后通过 gcc -shared -g -o test.so -fPIC test.c 将其编译为 .so 文件。然后使用 gdb 附加到目标进程并加载我通过 symbol-file /home/korcan/PINCE/Injection/test.so 创建的 .so 文件。已经加载成功(我是写call asd后按Tab才理解的,自动完成)

问题来了,每当我尝试从 test.so 执行函数时,gdb 都会给出此错误:

Program received signal SIGSEGV, Segmentation fault.
asdasdasd () at test.c:4
4       {
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(asdasdasd) will be abandoned.
When the function is done executing, GDB will silently stop.

在尝试理解出了什么问题之后,我又写了几个测试文件,似乎 gdb 总是在函数的第一行给出分段错误,它总是这样的:

Program received signal SIGSEGV, Segmentation fault.
functionname () at test.c:linenumber
linenumber       {

看来我的错误是试图用 symbol-file 命令加载 so 文件。我使用 dlopen() 系统调用而不是像这样的 symbol-filecall dlopen("+ PATH +", 2) 并且它起作用了。 symbol-file 只加载文件中的符号,而不是我猜的内容。