[英]Can not access memory in the .bss section, but gdb 'info files' shows the address is in range
[英]GDB shows incorrect jump address at PLT section
我編寫了以下示例來掌握 PLT/GOT 部分。
共享庫libshar
代碼:
shared.h
int sum(int a, int b);
shared.c
#include "shared.h"
int sum(int a, int b){
return a + b;
}
可執行的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);
}
所以我編譯並將共享庫與可執行文件鏈接,並編寫了以下 gdb-script 以進入動態 linker 代碼。 我希望它在第一次調用sum
時執行。
set pagination off
file build/bin_shared
b main
commands
layout asm
info proc mappings
end
r
我遇到了兩個問題:
I.當main
function 條目上的斷點被擊中時, info proc mappings
顯示libshar.so
已被映射:
0x7ffff7bd3000 0x7ffff7bd4000 0x1000 0x0 /home/me/c/build/libshar.so
0x7ffff7bd4000 0x7ffff7dd3000 0x1ff000 0x1000 /home/me/c/build/libshar.so
0x7ffff7dd3000 0x7ffff7dd4000 0x1000 0x0 /home/me/c/build/libshar.so
0x7ffff7dd4000 0x7ffff7dd5000 0x1000 0x1000 /home/me/c/build/libshar.so
sum
共享庫 function 尚未被調用。 為什么它已經迫不及待地加載了?
二、 第一次輸入sum@plt
時,我看到以下 asm:
0x555555554690 <sum@plt> jmp QWORD PTR [rip+0x200932] # 0x555555754fc8
正如預期的那樣,這是指向 GOT 的指針:
(gdb) disassemble 0x555555754fc8
Dump of assembler code for function _GLOBAL_OFFSET_TABLE_:
但問題是此時單指令步進將gdb
直接放入
0x7ffff7bd3580 <sum> lea eax,[rdi+rsi*1]
這意味着指向 GOT 的指針已經被實際的 function 指針覆蓋,但 gdb 仍然顯示 GOT 指針。 這是為什么?
我在jmp
到GOT
地址處提取了原始的memory,希望找到被覆蓋的地址,但它看起來不像:
(gdb) x/2xg 0x555555554690
0x555555554690 <sum@plt>: 0x01680020093225ff 0xffffffd0e9000000
為什么它已經迫不及待地加載了?
因為動態加載程序mmap
是您直接鏈接的所有共享庫。
如果您希望按需加載共享庫,則必須使用dlopen
而不是將二進制文件與libshar.so
鏈接。
指向 GOT 的指針已經被實際的 function 指針覆蓋,但 gdb 仍然顯示 GOT 指針。 這是為什么?
兩種可能的原因之一:
LD_BIND_NOW
或-Wl,-z,now
鏈接(這可能是較新的 Linux 發行版的默認值)。您可以通過以下方式檢查上述 2 是否為真:
readelf -d bin_shared | grep FLAGS
對於-z now
二進制文件,您會看到:
0x000000000000001e (FLAGS) BIND_NOW
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.