我对bash并不陌生,但这是我第一次看到这种情况。
[OP@localhost linking]$ ls
helloworld-lib.o helloworld-lib.s helloworld_s
[OP@localhost linking]$ ./helloworld_s
bash: ./helloworld_s: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我在测试链接器时发生此错误ld
。的内容helloworld-lib.s
是:
[OP@localhost linking]$ cat helloworld-lib.s
.section .data
helloworld:
.ascii "Hello, world!\n\0"
.section .text
.globl _start
_start:
mov $helloworld, %rdi
call printf
mov $0, %rdi
call exit
Run Code Online (Sandbox Code Playgroud)
该文件helloworld_s
产生如下。
[OP@localhost linking]$ as helloworld-lib.s -o helloworld-lib.o
[OP@localhost linking]$ ld -lc helloworld-lib.o -o helloworld_s
Run Code Online (Sandbox Code Playgroud)
如果有任何相关信息,则为IDK。仅供参考,如果我尝试运行其他文件,我只会得到一个被拒绝的权限(如预期的那样)。有任何想法吗?
编辑:按照建议,这是输出ls -l
:
[OP@localhost linking]$ ls -l
total 88
-rw-rw-r--. 1 OP OP 968 Mar 23 18:40 helloworld-lib.o
-rw-rw-r--. 1 OP OP 159 Mar 23 18:40 helloworld-lib.s
-rwxrwxr-x. 1 OP OP 14384 Mar 23 18:41 helloworld_s
Run Code Online (Sandbox Code Playgroud)
这是输出id
:
[OP@localhost linking]$ id
uid=1000(OP) gid=1000(OP) groups=1000(OP),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Run Code Online (Sandbox Code Playgroud)
编辑:对于答案,请参阅注释。看这里
正如 redhat bug #868662中所解释的,推荐的链接方式是让 gcc 调用 ld ,如下所示;
> gcc -nostartfiles helloworld-lib.o -o helloworld_s -lc
Run Code Online (Sandbox Code Playgroud)
这会导致正确的链接;
> ldd helloworld_s
linux-vdso.so.1 => (0x00007ffd283bf000)
libc.so.6 => /lib64/libc.so.6 (0x00007fd011b62000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd011f2f000)
Run Code Online (Sandbox Code Playgroud)
执行进展顺利;
> ./helloworld_s
Hello, world!
Run Code Online (Sandbox Code Playgroud)
为什么 ld 链接到不存在的 /lib/ld64.so.1 ?
因为这是通用系统的默认设置,而不仅仅是 Linux。
归档时间: |
|
查看次数: |
270 次 |
最近记录: |