gpt4 book ai didi

c - GDB和汇编: how to examine consts variables defined in heap?

转载 作者:行者123 更新时间:2023-11-30 18:56:46 25 4
gpt4 key购买 nike

例如下面的代码“justatest”和格式“%s”在堆中定义:

char str[15]="justatest";
int main(){
printf("%s",str);
return 0;
}

在GDB中,我在调用printf之前得到了汇编代码:

=> 0x0804841f <+14>:    movl   $0x804a020,0x4(%esp)
0x08048427 <+22>: movl $0x80484d8,(%esp)
0x0804842e <+29>: call 0x80482f0 <printf@plt>

我是否必须使用“x/s 0x804a020”和“x/s 0x80484d8”检查参数 1by1

或者堆中是否有一个我可以直接引用的常量表?

谢谢!

最佳答案

您对str驻留在上的理解不正确。它的全局变量存储在数据段中。关于打印全局变量,您可以在我的 GNU/Linux 终端上执行以下操作。

$ gcc -g -Wall hello.c
$ gdb -q ./a.out
Reading symbols from /home/mantosh/practice/a.out...done.
(gdb) break main
Breakpoint 1 at 0x400524: file hello.c, line 6.
(gdb) run
Starting program: /home/mantosh/practice/a.out

Breakpoint 1, main () at bakwas.c:6
6 printf("%s",str);
(gdb) disassemble main
Dump of assembler code for function main:
0x0000000000400520 <+0>: push %rbp
0x0000000000400521 <+1>: mov %rsp,%rbp
=> 0x0000000000400524 <+4>: mov $0x601020,%esi
0x0000000000400529 <+9>: mov $0x4005e4,%edi
0x000000000040052e <+14>: mov $0x0,%eax
0x0000000000400533 <+19>: callq 0x4003f0 <printf@plt>
0x0000000000400538 <+24>: mov $0x0,%eax
0x000000000040053d <+29>: pop %rbp
0x000000000040053e <+30>: retq
End of assembler dump.

(gdb) p str
$1 = "justatest\000\000\000\000\000"
(gdb) p &str
$2 = (char (*)[15]) 0x601020

// These are addresses of two arguments which would be passed in printf.
// From assembly instruction we can verify that before calling the printf
// these are getting stored into the registers.
(gdb) x/s 0x4005e4
0x4005e4: "%s"
(gdb) x/s 0x601020
0x601020 <str>: "justatest

关于c - GDB和汇编: how to examine consts variables defined in heap?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23318837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com