ld 是否在输出的命令行中保持输入 objfiles 的顺序?

Does ld keep the order of input objfiles in the command line in output?

ld在输出的命令行中是否保持输入objfiles的顺序?例如,假设命令行是:

ld -o output input1.o input2.o input3.o

input1会先输出,然后是input2,最后是input3吗?那么,加载后,input1中的代码是否会占据地址space中最低的部分,然后是input2和input3中最高的?该平台是 Linux 20.04,GNU 开发工具链是最新的(例如,ld 的版本是 2.34)。谢谢。

取决于您的 linker 脚本。每个 link 都由 linker 脚本控制。此脚本是用 linker 命令语言编写的。

来自 GCC 文档:

The main purpose of the linker script is to describe how the sections in the input files should be mapped into the output file, and to control the memory layout of the output file. Most linker scripts do nothing more than this. However, when necessary, the linker script can also direct the linker to perform many other operations.

The linker always uses a linker script. If you do not supply one yourself, the linker will use a default script that is compiled into the linker executable. You can use the --verbose command line option to display the default linker script. Certain command line options, such as -r or -N, will affect the default linker script.

参见 https://wiki.osdev.org/Linker_Scripts