链接器如何处理共享对象(.so)中的main()函数? [英] How is function main() inside a shared object (.so) taken care of by the linker?

查看:293
本文介绍了链接器如何处理共享对象(.so)中的main()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

  • 一个不带功能main的目标文件(source.o).

具有功能main的共享库(libmain.so).

a shared object (libmain.so) with function main.

当两者动态链接以创建二进制文件source.bin时,链接器将如何处理入口点?

How will the linker take care of the entry point when both are linked dynamically to create binary source.bin?

推荐答案

每当您有一个动态链接程序时,都会涉及多个入口点".第一个是动态链接器本身的入口点,例如/lib/ld-linux.so.2(在Linux/x86上)或类似.动态链接器首先运行,将所有符号名称解析为其定义(无论定义是在主程序中还是在库中),然后将执行传递到主程序二进制文件中的第二个入口点.这不是main,而是"C运行时"(因此是crt.o和类似名称)的一部分,用于处理某些main之前的东西(例如C ++ ctor,设置指向环境变量的指针并进行构造) main的正确参数).该代码以exit(main(argc, argv));结尾(等效于exit(main(argc, argv));),该代码将使用exitmain的重新定位的地址(通过动态链接器).

Whenever you have a dynamic linked program, there are several "entry points" involved. The first is in entry point of the dynamic linker itself, e.g. /lib/ld-linux.so.2 (on Linux/x86) or similar. The dynamic linker runs first, resolving all symbol names to their definitions (regardless of whether the definitions are in the main program or a library), then passes execution to the second entry point in the main program binary. This is not main but part of the "C runtime" (thus crt.o and similar names) that takes care of some pre-main stuff (like C++ ctors, setting up the pointer to the environment variables, and constructing the right arguments for main). This code ends with (the equivalent of) exit(main(argc, argv)); which will use the relocated (by the dynamic linker) addresses of both exit and main.

这篇关于链接器如何处理共享对象(.so)中的main()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆