bash: ./helloworld_s: 没有那个文件或目录。该文件明显存在。

5

我对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

在测试链接器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

这个文件 helloworld_s 是如下产生的。

[OP@localhost linking]$ as helloworld-lib.s -o helloworld-lib.o
[OP@localhost linking]$ ld -lc helloworld-lib.o -o helloworld_s

我不知道这些信息是否相关。顺便说一下,如果我尝试运行其他文件,我只会得到一个权限被拒绝的错误(正如所预期的那样)。有什么想法吗?

编辑:如建议所示,这是 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

以下是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

编辑:有关答案,请参见评论。 在此处查看


2
@Kenster 我认为这并不是问题所在。比如说,我可以毫无问题地使用 cat 命令读取这个文件。而且用 basename 命令也无法发现这个文件名有什么异常之处。 - extremeaxe5
2
请在您的问题中添加 ls -lid 的输出。 - Cyrus
9
很可能,ldd 命令会告诉你它正在寻找类似于 /lib/ld64.so.1 的文件,但实际上这个文件并不存在。 - David Schwartz
6
@DavidSchwartz哦,确实是这样。readelf -l helloworld_s 告诉我该程序请求解释器为“/lib/ld64.so.1”,正如你所说,它并不存在。所以很明显有问题。当我明确指定解释器为“/usr/lib64/ld-linux-x86-64.so.2”时,一切都正常。首先,您能否将此发布为答案?但是,如果是这种情况,我为什么会得到这个错误?另外,为什么GNU链接器默认请求一个不存在的程序解释器? - extremeaxe5
3
我们能否得到一个答案并将其标记为已回答或关闭? - Ryan Smith
显示剩余6条评论
2个回答

2

如在redhat bug #868662中所解释的那样,链接的推荐方法是让gcc像下面这样调用ld;

Original Answer翻译成"最初的回答"

> gcc -nostartfiles helloworld-lib.o -o helloworld_s -lc

这将导致正确的链接。翻译后的内容:

这会确保链接正确无误。

> ldd helloworld_s
        linux-vdso.so.1 =>  (0x00007ffd283bf000)
        libc.so.6 => /lib64/libc.so.6 (0x00007fd011b62000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fd011f2f000)

最初的回答
执行正常;
> ./helloworld_s
Hello, world!

为什么ld链接到不存在的/lib/ld64.so.1?因为这是通用系统的默认设置,不仅限于Linux。"最初的回答"。

0

在某些情况下,现有的可执行文件可能会被错误地报告为丢失,而实际问题是它们无法被执行。

实际原因各不相同,但包括以下原因:

  • 文件存在缺陷,可能是由于链接无效导致的,如另一个答案中所述

  • 文件适用于平台不支持的不同架构或ABI

  • 用户尝试执行该文件时,该文件缺少执行权限位

  • 文件位于已挂载禁止执行标志的卷上

在许多这些情况下,更具体和相关的错误消息显然更可取,然而,有时实际实现的内容(或由于不太明显的失败路径而触发的内容)确实会令人困惑,将“无法使用”的东西标记为“丢失”。错误的精度在不同环境之间可能会有所不同。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接