【发布时间】:2020-10-02 03:06:31
【问题描述】:
在c:
#include <stdio.h>
#include <stdlib.h>
int x;
int main(){
printf("eneter x\n");
scanf("%i",&x);
printf("you enetered: %i\n", x);
return 0;
}
在 gdb 中:
starti
disas main
0x0000555555555155 <+0>: push %rbp
0x0000555555555156 <+1>: mov %rsp,%rbp
0x0000555555555159 <+4>: lea 0xea4(%rip),%rdi # 0x555555556004
0x0000555555555160 <+11>: callq 0x555555555030 <puts@plt>
0x0000555555555165 <+16>: lea 0x2ed8(%rip),%rsi # 0x555555558044 <x>
0x000055555555516c <+23>: lea 0xe9a(%rip),%rdi # 0x55555555600d
0x0000555555555173 <+30>: mov $0x0,%eax
0x0000555555555178 <+35>: callq 0x555555555050 <__isoc99_scanf@plt>
0x000055555555517d <+40>: mov 0x2ec1(%rip),%eax # 0x555555558044 <x>
0x0000555555555183 <+46>: mov %eax,%esi
0x0000555555555185 <+48>: lea 0xe84(%rip),%rdi # 0x555555556010
0x000055555555518c <+55>: mov $0x0,%eax
0x0000555555555191 <+60>: callq 0x555555555040 <printf@plt>
0x0000555555555196 <+65>: mov $0x0,%eax
0x000055555555519b <+70>: pop %rbp
0x000055555555519c <+71>: retq
这里x变量的相对地址是$rip+0x2ed8(来自指令lea 0x2ed8(%rip),%rsi # 0x555555558044)。但正如您在评论# 中看到的那样,绝对 地址是0x555555558044。好的,当我尝试从亲戚那里读取时,我会得到那个地址吗?让我们看看:
x $rip+0x2ed8
0x555555558055: 0x00000000
nop - 相对地址没有使用绝对地址,x var 是真正存储的 (0x555555558055!= 0x555555558044) 差异是 17 个字节。是指令本身的字节数(lea + 操作数)吗?我不知道,但不这么认为。那么为什么 gdb 中的相对寻址和绝对寻址不同呢?
PS,生成的程序集:
.file "a.c"
.comm x,4,4
.section .rodata
.LC0:
.string "eneter x"
.LC1:
.string "%i"
.LC2:
.string "you enetered: %i\n"
.text
.globl main
.type main, @function
main:
pushq %rbp #
movq %rsp, %rbp #,
# a.c:5: printf("eneter x\n");
leaq .LC0(%rip), %rdi #,
call puts@PLT #
# a.c:6: scanf("%i",&x);
leaq x(%rip), %rsi #,
leaq .LC1(%rip), %rdi #,
movl $0, %eax #,
call __isoc99_scanf@PLT #
# a.c:7: printf("you enetered: %i\n", x);
movl x(%rip), %eax # x, x.0_1
movl %eax, %esi # x.0_1,
leaq .LC2(%rip), %rdi #,
movl $0, %eax #,
call printf@PLT #
# a.c:8: return 0;
movl $0, %eax #, _6
# a.c:9: }
popq %rbp #
ret
.size main, .-main
.ident "GCC: (Debian 8.3.0-6) 8.3.0"
.section .note.GNU-stack,"",@progbits
这里使用的是相对于RIP的模式:
# a.c:6: scanf("%i",&x);
leaq x(%rip), %rsi #,
x 是x 符号的位置。但是在cmets中,有人说$rip+0x2ed8不一样,偏移0x2ed8不会导致x的地址。但是为什么这两个不同呢?但应该是 RIP 相对模式寻址,并且两者都应该获得相同的偏移量(因此地址)。
【问题讨论】:
-
评论不用于扩展讨论;这个对话是moved to chat。
标签: c debugging gdb disassembly