gpt4 book ai didi

linux - ELF 文件和附加符号

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:18:45 27 4
gpt4 key购买 nike

我正在阅读有关 ELF 文件格式的内容,我注意到一个用 C++ 编写的小型 hello world 测试程序在 _start 符号中包含一些额外的初始化:

0000000000400770 <_start>:
...
40077f: 49 c7 c0 60 09 40 00 mov $0x400960,%r8
400786: 48 c7 c1 f0 08 40 00 mov $0x4008f0,%rcx
40078d: 48 c7 c7 5d 08 40 00 mov $0x40085d,%rdi
...

40077f__libc_csu_fini

4008f0__libc_csu_init

40085dmain

不应该只是 _startmain 吗?为什么不?如果我只是删除对 40077f40008f0 的调用并替换为 nop 会发生什么情况?基本上,需要 libc 的意义是什么?

最佳答案

查看 glibc source code :

/* These functions are passed to __libc_start_main by the startup code.
These get statically linked into each program. For dynamically linked
programs, this module will come from libc_nonshared.a and differs from
the libc.a module in that it doesn't call the preinit array. */


void
__libc_csu_init (int argc, char **argv, char **envp)
{
/* For dynamically linked executables the preinit array is executed by
the dynamic linker (before initializing any shared object). */

#ifndef LIBC_NONSHARED
/* For static executables, preinit happens right before init. */
{
const size_t size = __preinit_array_end - __preinit_array_start;
size_t i;
for (i = 0; i < size; i++)
(*__preinit_array_start [i]) (argc, argv, envp);
}
#endif

#ifndef NO_INITFINI
_init ();
#endif

const size_t size = __init_array_end - __init_array_start;
for (size_t i = 0; i < size; i++)
(*__init_array_start [i]) (argc, argv, envp);
}

/* This function should not be used anymore. We run the executable's
destructor now just like any other. We cannot remove the function,
though. */
void
__libc_csu_fini (void)
{
#ifndef LIBC_NONSHARED
size_t i = __fini_array_end - __fini_array_start;
while (i-- > 0)
(*__fini_array_start [i]) ();

# ifndef NO_INITFINI
_fini ();
# endif
#endif
}

这允许库初始化代码运行。链接到程序的库可以在 gcc 中用 __attribute__((constructor)) 标记函数,这种机制将在 main 之前运行这些函数,允许库自行初始化在程序开始之前。

关于linux - ELF 文件和附加符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963628/

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