linux - __libc_start_main@plt 是如何工作的?

标签 linux gcc dynamic-linking x86 elf

为了研究目标文件在linux下是如何加载和运行的,我做了一个最简单的c代码,文件名simple.c。

int main(){}

接下来,我制作目标文件并将目标文件保存为文本文件。

$gcc ./simple.c 
$objdump -xD ./a.out > simple.text

从许多互联网文章中,我可以发现 gcc 动态加载启动函数,如 _start、_init、__libc_start_main@plt 等。所以在 http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html 的帮助下,我开始阅读我的汇编代码.

下面是部分汇编代码。

080482e0 <__libc_start_main@plt>:
 80482e0:       ff 25 10 a0 04 08       jmp    *0x804a010
 80482e6:       68 08 00 00 00          push   $0x8
 80482eb:       e9 d0 ff ff ff          jmp    80482c0 <_init+0x2c>

Disassembly of section .text:

080482f0 <_start>:
 80482f0:       31 ed                   xor    %ebp,%ebp
 80482f2:       5e                      pop    %esi
 80482f3:       89 e1                   mov    %esp,%ecx
 80482f5:       83 e4 f0                and    $0xfffffff0,%esp
 80482f8:       50                      push   %eax
 80482f9:       54                      push   %esp
 80482fa:       52                      push   %edx
 80482fb:       68 70 84 04 08          push   $0x8048470
 8048300:       68 00 84 04 08          push   $0x8048400
 8048305:       51                      push   %ecx
 8048306:       56                      push   %esi
 8048307:       68 ed 83 04 08          push   $0x80483ed
 804830c:       e8 cf ff ff ff          call   80482e0 <__libc_start_main@plt>
 8048311:       f4                      hlt
 8048312:       66 90                   xchg   %ax,%ax
 8048314:       66 90                   xchg   %ax,%ax
 8048316:       66 90                   xchg   %ax,%ax
 8048318:       66 90                   xchg   %ax,%ax
 804831a:       66 90                   xchg   %ax,%ax
 804831c:       66 90                   xchg   %ax,%ax
 804831e:       66 90                   xchg   %ax,%ax

080483ed <main>:
 80483ed:       55                      push   %ebp
 80483ee:       89 e5                   mov    %esp,%ebp
 80483f0:       b8 00 00 00 00          mov    $0x0,%eax
 80483f5:       5d                      pop    %ebp
 80483f6:       c3                      ret
 80483f7:       66 90                   xchg   %ax,%ax
 80483f9:       66 90                   xchg   %ax,%ax
 80483fb:       66 90                   xchg   %ax,%ax
 80483fd:       66 90                   xchg   %ax,%ax
 80483ff:       90                      nop



 ...

Disassembly of section .got:

08049ffc <.got>:
 8049ffc:       00 00                   add    %al,(%eax)
        ...

Disassembly of section .got.plt:

0804a000 <_GLOBAL_OFFSET_TABLE_>:
 804a000:       14 9f                   adc    $0x9f,%al
 804a002:       04 08                   add    $0x8,%al
        ...
 804a00c:       d6                      (bad)
 804a00d:       82                      (bad)
 804a00e:       04 08                   add    $0x8,%al
 804a010:       e6 82                   out    %al,$0x82
 804a012:       04 08                   add    $0x8,%al

我的问题是;

在 0x804830c 中,调用了 0x80482e0(我已经理解了前面的指令。)。

在0x80482e0,进程跳转到0x804a010。

在0x804a010,指令是

……等等。刚出去? %al 中有什么,0x82 在哪里?我陷入了这一行。

请帮忙....

*附注我是 linux 和操作系统的初学者。我正在通过学​​校类(class)学习操作系统概念,但仍然找不到如何学习正确的 linux 汇编语言。我已经下载了英特尔处理器手册,但它太大了,无法阅读。任何人都可以为我提供好的 Material 吗?谢谢。

最佳答案

80482e0:       ff 25 10 a0 04 08       jmp    *0x804a010

这意味着“检索存储在 0x804a010 的 4 字节地址并跳转到它。”

804a010:       e6 82                   out    %al,$0x82
804a012:       04 08                   add    $0x8,%al

这 4 个字节将被视为地址 0x80482e6,而不是指令。

80482e0:       ff 25 10 a0 04 08       jmp    *0x804a010
80482e6:       68 08 00 00 00          push   $0x8
80482eb:       e9 d0 ff ff ff          jmp    80482c0 <_init+0x2c>

所以我们刚刚执行了一条指令,它使我们正好向前移动了一条指令。此时,您可能想知道这样做是否有充分的理由。

有。这是典型的 PLT/GOT 实现。包括图表在内的更多详细信息位于 Position Independent Code in shared libraries: The Procedure Linkage Table。 .

__libc_start_main 的真正代码在共享库 glibc 中。编译器和编译时链接器不知道代码在运行时的位置,因此它们会在编译后的程序中放置一个简短的 __libc_start_main 函数,它仅包含三个指令:

  • 跳转到 GOT 中第 4 个(或第 5 个,取决于你喜欢从 0 还是 1 开始计数)条目指定的位置
  • 将$8压入堆栈
  • 跳转到解析程序

第一次调用 __libc_start_main 时,解析器代码将运行。它将在共享库中找到 __libc_start_main 的实际位置,并将 GOT 的第 4 个条目修补为该地址。如果您的程序再次调用 __libc_start_mainjmp *0x804a010 指令会将程序直接带到共享库中的代码。

Can anyone inform me good material for me?

x86 Assembly在 Wikibooks 上预订可能是一个起点。

关于linux - __libc_start_main@plt 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29502614/

相关文章:

c++ - 使用 GAlib 模板实例化的 Cygwin gcc 编译器问题

gcc - 如何通过库名找到库的文件名?

c++ - 如何使二进制文件与不同的发行版兼容

linux - 安装 PCRE 时出错

c - GNU Make - 对非程序代码的依赖

linux - 有条件地将行附加为列 bash 脚本

php - wget下载完成后运行脚本(wget后台模式)

c++ - 尝试将 sfml 和 c++ 与 Windows 10 上的可移植 vscode 链接起来

linux - 循环附加到 tar 文件

php - Cron 作业的输出与在浏览器中运行脚本不同