简体   繁体   English

在gdb中运行后`main`函数的运动?

[英]Movement of `main` function after running in gdb?

I have the following C code in ret0.c . 我在ret0.c有以下C代码。

int main(){
  return 0;
}

Compiling and running through gdb, I have the following output: 编译并通过gdb运行,​​我有以下输出:

(gdb) disas main
Dump of assembler code for function main:
   0x0000000000001119 <+0>: push   %rbp
   0x000000000000111a <+1>: mov    %rsp,%rbp
   0x000000000000111d <+4>: mov    $0x0,%eax
   0x0000000000001122 <+9>: pop    %rbp
   0x0000000000001123 <+10>:    retq   
End of assembler dump.
(gdb) run
Starting program: /home/michael/core/mind/ob/thm/cs/lang/other/c/ret0 
[Inferior 1 (process 14766) exited normally]
(gdb) disas main
Dump of assembler code for function main:
   0x0000555555555119 <+0>: push   %rbp
   0x000055555555511a <+1>: mov    %rsp,%rbp
   0x000055555555511d <+4>: mov    $0x0,%eax
   0x0000555555555122 <+9>: pop    %rbp
   0x0000555555555123 <+10>:    retq   
End of assembler dump.

So after executing once, the location of main in memory has changed? 那么执行一次后, main在内存中的位置发生了变化吗? What's going on here? 这里发生了什么?

The first output was printed before the process was started. 在开始该过程之前,先打印出第一个输出。 In this case gdb prints the offset in the binary file. 在这种情况下,gdb在二进制文件中打印偏移量。

The second output is after the process was terminated, and you see the actual address in the memory space of the process. 第二个输出是在进程终止之后,您会在进程的内存空间中看到实际地址。

What's going on here? 这里发生了什么?

You have a position-independent executable (which is really a special form of a shared library), which is relocated to random address at runtime. 您有一个与位置无关的可执行文件 (实际上是共享库的一种特殊形式),该可执行文件在运行时会重定位到随机地址。

You can verify this by running file ret0 , which will say something like ELF 64-bit LSB pie executable, x86-64, version 1 ... 您可以通过运行file ret0来验证这file ret0 ,该file ret0将显示ELF 64-bit LSB pie executable, x86-64, version 1 ... file ret0 ELF 64-bit LSB pie executable, x86-64, version 1 ...

To build a non-PIE executable, use gcc -no-pie ... . 要构建非PIE可执行文件,请使用gcc -no-pie ... Non-position-independent executables must be loaded at the address at which they were linked, main will stay in place. 非位置无关的可执行文件必须在它们链接的地址处加载, main会保留在原位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM