编译器如何选择哪个“ get_pc_thunk”函数来获取当前地址?

时间:2018-08-15 14:44:35

标签: gcc assembly x86 register-allocation position-independent-code

PIE二进制文件使用函数__x86.get_pc_thunk来获取在加载时动态定义的地址。


i)有时它调用__x86.get_pc_thunk.ax

5ff:   e8 24 00 00 00          call   628 <__x86.get_pc_thunk.ax>
604:   05 fc 19 00 00          add    $0x19fc,%eax
609:   83 ec 0c                sub    $0xc,%esp
60c:   8d 90 b0 e6 ff ff       lea    -0x1950(%eax),%edx
612:   52                      push   %edx
613:   89 c3                   mov    %eax,%ebx
615:   e8 36 fe ff ff          call   450 <printf@plt>


ii)有时它调用__x86.get_pc_thunk.bx

634:   e8 87 fe ff ff          call   4c0  <__x86.get_pc_thunk.bx>
639:   81 c3 c7 19 00 00       add    $0x19c7,%ebx
63f:   83 ec 0c                sub    $0xc,%esp
642:   8b 6c 24 20             mov    0x20(%esp),%ebp
646:   8d b3 f0 fe ff ff       lea    -0x110(%ebx),%esi
64c:   e8 cb fd ff ff          call   41c <_init>


iii)有时调用__x86.get_pc_thunk.cx或有时调用__x86.get_pc_thunk.dx



我知道__x86.get_pc_thunk.bx__x86.get_pc_thunk.ax之间的区别是寄存器,该寄存器将返回地址存储在.bxEBX.axEAX中。


然后,我的问题是:

编译器如何在__x86.get_pc_thunk.ax / __x86.get_pc_thunk.bx / __x86.get_pc_thunk.cx / __x86.get_pc_thunk.dx中选择要使用的函数?

在编译时是否真的选择了随机变量?
还是有选择算法?

1 个答案:

答案 0 :(得分:2)

编译器为要使用的任何寄存器选择适当的功能。使用哪个寄存器是编译器为其编译的函数分配寄存器的结果,并且通常不可预测。