我在下面突出显示了两个问题。我正在使用 64 位 Linux。
我在另一篇文章中看到了作为实现提到的另一个帖子。MUSL
libc
我尝试将它与以下使用两个函数的Hello world汇编程序一起使用,并且.libc
write
_exit
.data
hello:
.ascii "Hello, world\n"
.text
.globl _start
_start:
movl $13, %edx
movl $hello, %esi
movl $1, %edi
call write
movl $0, %edi
call _exit
我组装了代码:
# Command 1
$ as -o hello.o hello.s
然后我跑来ld
生成一个静态链接的可执行文件MUSL
libc
。
# Command 2
$ ld hello.o /usr/lib/x86_64-linux-musl/libc.a
这会生成一个a.out
按预期工作的文件,在执行时输出“Hello, world”。
我还尝试了对前面ld
命令的不同调用,使用-static -lc
而不是直接指定路径,并且还使用-L
给出路径MUSL
以便glibc
不使用,因为后者已经在ld
的搜索路径上。
# Command 3
$ ld hello.o -L/usr/lib/x86_64-linux-musl/ -static -lc
这按预期工作。
接下来我尝试动态链接MUSL
libc
.
# Command 4
$ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 hello.o \
/usr/lib/x86_64-linux-musl/libc.so
这似乎按预期工作。我可以运行a.out
,并调用ldd
显示'sa.out
已链接。MUSL
libc
最后,我尝试了相对于之前的静态链接版本的类似修改,使用-lc
而-L
不是.so
直接指定文件的路径。
# Command 5
$ ld -dynamic-linker /lib64/ld-linux-x86-64.so.2 hello.o \
-L/usr/lib/x86_64-linux-musl -lc
程序运行不正常,输出错误:
bash: ./a.out: No such file or directory
当我使用标志运行相同ld
的命令时--verbose
,输出与传递--verbose
给早期ld
命令(Command 4
生成工作可执行文件)时的输出相同。
运行ldd
ona.out
也会输出错误:
./a.out: error while loading shared libraries: /usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header
问题1:为什么在这种情况下调用ld
和-L
与-lc
之前的行为不匹配,我.so
直接指定了文件?
我注意到,如果我将指定的动态链接器更改为/lib/ld-musl-x86_64.so.1
,生成的a.out
将按预期运行。
# Command 6
$ ld -dynamic-linker /lib/ld-musl-x86_64.so.1 hello.o \
-L/usr/lib/x86_64-linux-musl -lc
但是,调用ldd
generateda.out
会产生以下错误,而之前我没有使用-lc
and -L
in时,情况并非如此Command 4
:
./a.out: error while loading shared libraries: /usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header
问题 2:为什么ld
这个二进制文件会失败,但是当我将.so
文件的路径传递给ldd
并使用不同的动态链接器时更早地工作?