【问题标题】:bash: ./helloworld_s: no such file or directory. The file is clearly therebash: ./helloworld_s: 没有这样的文件或目录。文件明显在那里
【发布时间】:2019-03-23 22:42:26
【问题描述】:

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

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

这是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

编辑:有关答案,请参阅 cmets。 See here

【问题讨论】:

  • @Kenster 我不相信它。例如,我可以cat 该文件没有问题。使用basename,我没有发现文件名有任何异常。
  • ls -lid 的输出添加到您的问题中。
  • 很可能,ldd 会告诉您它正在寻找像 /lib/ld64.so.1 这样不存在的东西。
  • @DavidSchwartz 哦,是的,确实如此。 readelf -l helloworld_s 告诉我程序请求解释器 /lib/ld64.so.1,正如你所说,它不存在。很明显,这里有东西。当我将解释器明确指定为/usr/lib64/ld-linux-x86-64.so.2 时,一切正常。首先,您可以将其发布为答案吗?但是,如果是这种情况,为什么我会将此作为我的错误?此外,为什么 GNU 链接器默认请求一个不存在的程序解释器?
  • 我们可以在答案中得到这个并将其标记为已回答或关闭吗?

标签: linux bash


【解决方案1】:

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

> 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。

【讨论】:

    【解决方案2】:

    在实际问题是它们无法执行的情况下,现有的可执行文件可能会被混淆地报告为缺失。

    实际原因各不相同,但包括诸如

    • 文件有缺陷,可能是另一个答案中提到的无效链接的结果

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

    • 该文件缺少尝试执行此操作的用户的执行权限位

    • 文件位于安装有禁止执行标志的卷上

    在许多情况下,很明显,更具体和相关的错误消息会更可取,但是,有时实际实现的内容(或由不太明显的故障路径触发)确实会让人感到困惑将“不可用”的东西标记为“缺失”。错误的精确程度会因环境而有所不同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多