我尝试使用gcc编译非PIC代码,并且我注意到GCC生成的汇编代码不使用纯函数地址来调用,而是在其上添加了一个怪异的偏移量。我将GCC 9.3.0用作带有以下代码的gcc test.c -o test-nopic -mcmodel=large -no-pie -O0
。
#include <stdio.h>
int var1 = 1;
int var2 = 2;
void putstr(int* ptr) {
printf("val: %d\n", *ptr);
}
int main() {
putstr(&var1);
putstr(&var2);
}
这里列出的是main()
中的代码objdump -wdrC -M intel test-nopic
。
000000000040117e <main>:
40117e: 55 push rbp
40117f: 48 89 e5 mov rbp,rsp
401182: 53 push rbx
401183: 48 83 ec 08 sub rsp,0x8
401187: 48 8d 1d f9 ff ff ff lea rbx,[rip+0xfffffffffffffff9] # 401187 <main+0x9>
40118e: 49 bb 79 2e 00 00 00 00 00 00 movabs r11,0x2e79
401198: 4c 01 db add rbx,r11
40119b: 48 b8 30 00 00 00 00 00 00 00 movabs rax,0x30
4011a5: 48 8d 3c 03 lea rdi,[rbx+rax*1]
4011a9: 48 b8 26 d1 ff ff ff ff ff ff movabs rax,0xffffffffffffd126
4011b3: 48 8d 04 03 lea rax,[rbx+rax*1]
4011b7: ff d0 call rax
4011b9: 48 b8 34 00 00 00 00 00 00 00 movabs rax,0x34
4011c3: 48 8d 3c 03 lea rdi,[rbx+rax*1]
4011c7: 48 b8 26 d1 ff ff ff ff ff ff movabs rax,0xffffffffffffd126
4011d1: 48 8d 04 03 lea rax,[rbx+rax*1]
4011d5: ff d0 call rax
4011d7: b8 00 00 00 00 mov eax,0x0
4011dc: 48 83 c4 08 add rsp,0x8
4011e0: 5b pop rbx
4011e1: 5d pop rbp
4011e2: c3 ret
pustr(int *)的地址是0x401126
。 readelf -l test-nopic
显示文件类型为EXEC和以下标头:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
0x0000000000000268 0x0000000000000268 R 0x8
INTERP 0x00000000000002a8 0x00000000004002a8 0x00000000004002a8
0x000000000000001c 0x000000000000001c R 0x1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x00000000000004d8 0x00000000000004d8 R 0x1000
LOAD 0x0000000000001000 0x0000000000401000 0x0000000000401000
0x0000000000000275 0x0000000000000275 R E 0x1000
LOAD 0x0000000000002000 0x0000000000402000 0x0000000000402000
0x0000000000000168 0x0000000000000168 R 0x1000
LOAD 0x0000000000002e00 0x0000000000403e00 0x0000000000403e00
0x0000000000000238 0x0000000000000240 RW 0x1000
DYNAMIC 0x0000000000002e10 0x0000000000403e10 0x0000000000403e10
0x00000000000001d0 0x00000000000001d0 RW 0x8
NOTE 0x00000000000002c4 0x00000000004002c4 0x00000000004002c4
0x0000000000000044 0x0000000000000044 R 0x4
GNU_EH_FRAME 0x0000000000002010 0x0000000000402010 0x0000000000402010
0x0000000000000044 0x0000000000000044 R 0x4
GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 0x10
GNU_RELRO 0x0000000000002e00 0x0000000000403e00 0x0000000000403e00
0x0000000000000200 0x0000000000000200 R 0x1
movabs rax, 0x401126
?通过@Jester的评论,我解开了谜团。我还必须使用-fnopic
标志进行编译,以获得真正的非PIC代码。问题中的代码(与-mcmodel=large -no-pie -O0
标志一起编译)使用对从rax
寄存器获取的绝对地址的调用。该地址是使用以下代码从rip
寄存器计算得出的。
401187: 48 8d 1d f9 ff ff ff lea rbx,[rip+0xfffffffffffffff9] # 401187 <main+0x9>
40118e: 49 bb 79 2e 00 00 00 00 00 00 movabs r11,0x2e79
401198: 4c 01 db add rbx,r11
40119b: 48 b8 30 00 00 00 00 00 00 00 movabs rax,0x30
4011a5: 48 8d 3c 03 lea rdi,[rbx+rax*1]
4011a9: 48 b8 26 d1 ff ff ff ff ff ff movabs rax,0xffffffffffffd126
4011b3: 48 8d 04 03 lea rax,[rbx+rax*1]
4011b7: ff d0 call rax
我计算了存储在rip
中的地址,它看起来指向0x40118e。它用于计算函数及其参数的地址(var1的地址存储在rdi
寄存器中,它指向RW LOAD段)。使用-fno-pic
标志,函数调用看起来像我想要的。
40115c: 48 bf 30 40 40 00 00 00 00 00 movabs rdi,0x404030
401166: 48 b8 26 11 40 00 00 00 00 00 movabs rax,0x401126
401170: ff d0 call rax
没有-mcmodel=large
标志(-no-pie -fno-pic -O0
),它看起来与众不同。这是相对版本的call。
401150: bf 30 40 40 00 mov edi,0x404030
401155: e8 cc ff ff ff call 401126 <putstr>
这里是一个只有-pie
的代码(在我的配置中默认启用)。>
1165: 48 8d 3d c4 2e 00 00 lea rdi,[rip+0x2ec4] # 4030 <var1> 116c: e8 c8 ff ff ff call 1139 <putstr>
并且添加
-fpic
标志之后。
1165: 48 8d 05 c4 2e 00 00 lea rax,[rip+0x2ec4] # 4030 <var1>
116c: 48 89 c7 mov rdi,rax
116f: e8 c5 ff ff ff call 1139 <putstr>