无法执行指向 exe 的符号链接,除非位于链接所在的目录中

无法执行指向 exe 的符号链接,除非位于链接所在的目录中

我使用以下 bash 脚本创建了指向 bin 目录中 exe 文件的符号链接:

dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ cat do_links.sh
./make_links.sh as
./make_links.sh nm
./make_links.sh ar
./make_links.sh ranlib
./make_links.sh strip
./make_links.sh objdump
./make_links.sh ld
./make_links.sh gcov
./make_links.sh gcc-4.0.1
./make_links.sh gcc
./make_links.sh gprof
./make_links.sh gccbug
./make_links.sh g++
./make_links.sh c++
./make_links.sh strings
./make_links.sh c++filt
./make_links.sh size
./make_links.sh cpp
./make_links.sh readelf
./make_links.sh addr2line
./make_links.sh objcopy
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ cat make_links.sh
ln -s arm-unknown-linux-gnu-$1 $1

然后我在 bin 目录中运行 ./do_links.sh,bin 目录如下所示:

dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ lt
total 26552
-rwxr-xr-x 2 dan dan 2566199 May 30 16:44 arm-unknown-linux-gnu-as
-rwxr-xr-x 2 dan dan 1762396 May 30 16:44 arm-unknown-linux-gnu-nm
-rwxr-xr-x 2 dan dan 1706104 May 30 16:44 arm-unknown-linux-gnu-ar
-rwxr-xr-x 2 dan dan 1706099 May 30 16:44 arm-unknown-linux-gnu-ranlib
-rwxr-xr-x 2 dan dan 2203025 May 30 16:44 arm-unknown-linux-gnu-strip
-rwxr-xr-x 2 dan dan 2346625 May 30 16:44 arm-unknown-linux-gnu-objdump
-rwxr-xr-x 2 dan dan 2471491 May 30 16:44 arm-unknown-linux-gnu-ld
-rwxr-xr-x 1 dan dan   67493 May 30 16:44 arm-unknown-linux-gnu-gcov
-rwxr-xr-x 2 dan dan  201417 May 30 16:44 arm-unknown-linux-gnu-gcc-4.0.1
-rwxr-xr-x 2 dan dan  201417 May 30 16:44 arm-unknown-linux-gnu-gcc
-rwxr-xr-x 1 dan dan 2013095 May 30 16:44 arm-unknown-linux-gnu-gprof
-rwxr-xr-x 1 dan dan   16196 May 30 16:44 arm-unknown-linux-gnu-gccbug
-rwxr-xr-x 2 dan dan  206621 May 30 16:44 arm-unknown-linux-gnu-g++
-rwxr-xr-x 2 dan dan  206621 May 30 16:44 arm-unknown-linux-gnu-c++
-rwxr-xr-x 1 dan dan 1602788 May 30 16:44 arm-unknown-linux-gnu-strings
-rwxr-xr-x 1 dan dan 1692490 May 30 16:44 arm-unknown-linux-gnu-c++filt
-rwxrwxr-x 1 dan dan   12281 May 30 16:44 fix-embedded-paths
-rwxr-xr-x 1 dan dan 1620406 May 30 16:44 arm-unknown-linux-gnu-size
-rwxr-xr-x 1 dan dan  202864 May 30 16:44 arm-unknown-linux-gnu-cpp
-rwxr-xr-x 1 dan dan  398151 May 30 16:44 arm-unknown-linux-gnu-readelf
-rwxr-xr-x 1 dan dan 1727820 May 30 16:44 arm-unknown-linux-gnu-addr2line
-rwxr-xr-x 1 dan dan 2203026 May 30 16:44 arm-unknown-linux-gnu-objcopy
-rwxrwxr-x 1 dan dan     339 May 30 18:31 check_ver.sh
-rwxrwxr-x 1 dan dan     460 May 30 19:09 do_links.sh
-rwxr-xr-x 1 dan dan     129 May 30 19:13 make_links.sh
lrwxrwxrwx 1 dan dan      24 May 30 19:38 as -> arm-unknown-linux-gnu-as
lrwxrwxrwx 1 dan dan      24 May 30 19:38 nm -> arm-unknown-linux-gnu-nm
lrwxrwxrwx 1 dan dan      28 May 30 19:38 ranlib -> arm-unknown-linux-gnu-ranlib
lrwxrwxrwx 1 dan dan      24 May 30 19:38 ar -> arm-unknown-linux-gnu-ar
lrwxrwxrwx 1 dan dan      27 May 30 19:38 strip -> arm-unknown-linux-gnu-strip
lrwxrwxrwx 1 dan dan      29 May 30 19:38 objdump -> arm-unknown-linux-gnu-objdump
lrwxrwxrwx 1 dan dan      24 May 30 19:38 ld -> arm-unknown-linux-gnu-ld
lrwxrwxrwx 1 dan dan      26 May 30 19:38 gcov -> arm-unknown-linux-gnu-gcov
lrwxrwxrwx 1 dan dan      31 May 30 19:38 gcc-4.0.1 -> arm-unknown-linux-gnu-gcc-4.0.1
lrwxrwxrwx 1 dan dan      25 May 30 19:38 gcc -> arm-unknown-linux-gnu-gcc
lrwxrwxrwx 1 dan dan      27 May 30 19:38 gprof -> arm-unknown-linux-gnu-gprof
lrwxrwxrwx 1 dan dan      28 May 30 19:38 gccbug -> arm-unknown-linux-gnu-gccbug
lrwxrwxrwx 1 dan dan      25 May 30 19:38 g++ -> arm-unknown-linux-gnu-g++
lrwxrwxrwx 1 dan dan      25 May 30 19:38 c++ -> arm-unknown-linux-gnu-c++
lrwxrwxrwx 1 dan dan      29 May 30 19:38 strings -> arm-unknown-linux-gnu-strings
lrwxrwxrwx 1 dan dan      29 May 30 19:38 c++filt -> arm-unknown-linux-gnu-c++filt
lrwxrwxrwx 1 dan dan      26 May 30 19:38 size -> arm-unknown-linux-gnu-size
lrwxrwxrwx 1 dan dan      25 May 30 19:38 cpp -> arm-unknown-linux-gnu-cpp
lrwxrwxrwx 1 dan dan      29 May 30 19:38 readelf -> arm-unknown-linux-gnu-readelf
lrwxrwxrwx 1 dan dan      31 May 30 19:38 addr2line -> arm-unknown-linux-gnu-addr2line
lrwxrwxrwx 1 dan dan      29 May 30 19:38 objcopy -> arm-unknown-linux-gnu-objcopy

所以我可以在该目录中执行符号链接:

dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

但是当我尝试在其他任何地方执行符号链接时:

dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ cd ..
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu$ bin/gcc --version
bash: bin/gcc: No such file or directory

只是为了好玩,我在 bin 目录中创建了一个 bash 脚本:

dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ echo "gcc --version" > check.sh
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ chmod +x check.sh
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ ./check.sh
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ cd ..
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu$ bin/check.sh
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

并且它在任何其他目录中都能正常工作。

然后我认为它与其他 gcc 冲突,并将链接名称更改为 gcc2:

dan@Linux-Desktop:~$ which gcc
/usr/bin/gcc
dan@Linux-Desktop:~$ which gcc2
/home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/gcc2
dan@Linux-Desktop:~$ gcc2 --version
bash: /home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/gcc2: No such file or directory

我甚至无法从任何其他目录运行显式 exe:

dan@Linux-Desktop:~$ cat testd.sh
ls -ltr /home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-gcc
/home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-gcc --version
dan@Linux-Desktop:~$ ./testd.sh
-rwxr-xr-x 2 dan dan 201417 May 30 16:44 /home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-gcc
./testd.sh: line 2: /home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-gcc: No such file or directory
dan@Linux-Desktop:~$

这是我的PATH

dan@Linux-Desktop:~$ printenv PATH
/home/dan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/dan/java/jdk-11.0.11+9/bin:/home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin

答案1

假设:您尝试运行的可执行文件是对于不兼容的架构

程序是针对不兼容的架构编译的,导致程序无法执行。错误消息显示“不存在”而不是“无效的可执行文件”,这只是一个非常具有误导性的消息。

或者它缺少所需的库

尤其:

  1. 在目录中时,gcc --version运行gcc 来自别处在您的 中PATH,而不是目录中存在的文件(符号链接)。可能是/usr/bin/gcc。标题是“除非在链接所在的目录中,否则无法执行指向 exe 的符号链接”,但“除非”部分是误解。在您没有运行符号链接的目录中。./gcc --version将运行它。
  2. 当在父目录中时,bin/gcc --version尝试运行符号链接并且它指向arm-unknown-linux-gnu-gcc根据假设触发误导性错误的那个目录。
  3. 无论如何, shell 脚本check.sh都可以正常工作,因为通过您的第一个位置找到它PATH是目录,并且问题不适用于运行脚本本身。请注意,gcc脚本内部是通过PATH(1) 中的方式定位的,并且它的工作方式与 (1) 中的方式相同。
  4. 名为 的链接永远不起作用,因为之前的目录中gcc2没有。唯一的是目录中的符号链接,它指向有问题的可执行文件。gcc2PATHgcc2
  5. 脚本testd.sh明确地通过其完整路径调用arm-unknown-linux-gnu-gcc,从而出现问题。

相关内容