我编写了以下示例来了解 PLT/GOT 部分。
共享库libshar代码:
shared.h
int sum(int a, int b);
Run Code Online (Sandbox Code Playgroud)
shared.c
#include "shared.h"
int sum(int a, int b){
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
可执行bin_shared代码:
#include <stdio.h>
#include "shared.h"
int main(void){
printf("Starting the programm... \n");
int s = sum(1, 2); //<=== I expected the dynamic linker would be called here
int s2 = sum(2, 3);
printf("s = %d, s2 = %d\n", s, s2);
}
Run Code Online (Sandbox Code Playgroud)
因此,我编译了共享库并将其与可执行文件链接,并编写了以下 gdb 脚本来进入动态链接器代码。我预计它会在第一次调用时执行sum。
set pagination off
file build/bin_shared
b main
commands …Run Code Online (Sandbox Code Playgroud)