当尝试在 zsh 中执行时,以下命令取自这里
/lib64/ld-linux-x86-64.so.2 =(/bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}'\
| sudo gcc -x c -o /dev/stdout -)
它返回以下错误
/tmp/zshgYCSAH:加载共享库时出错:/tmp/zshgYCSAH:文件太短
然而,发行
/lib64/ld-linux-x86-64.so.2 /bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}'
#include <stdio.h>
int main(){printf("c one liners\n");}
返回预期的行为。
我在这里错过了一些非常明显的东西吗?
操作系统和 GCC 权限和版本
uname -a
Linux debian 4.19.0-8-amd64 #1 SMP Debian 4.19.98-1 (2020-01-26) x86_64 GNU/Linux
\ls -l /usr/bin/x86_64-linux-gnu-gcc-8
-rwxr-x--- 1 root root 1100664 Apr 6 2019 /usr/bin/x86_64-linux-gnu-gcc-8
sudo gcc --version
gcc (Debian 8.3.0-6) 8.3.0
Copyright (C) 2018 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.
语境
file =(/bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | sudo gcc -x c -o /dev/stdout -)
/tmp/zshrOdFnK: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=d94f408ab2b445d20624e2b1fb7c4939dfc25c46, not stripped
\ls -l =(/bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | sudo gcc -x c -o /dev/stdout -)
-rw------- 1 paulo paulo 16600 Mar 30 12:37 /tmp/zshTNxAXl
编译器子命令似乎工作正常
/bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | gcc -x c -o a.out -
ls -l a.out
-rwxr-xr-x 1 root root 16600 Mar 30 12:03 a.out
du a.out
20 a.out
./a.out
c one liners
答案1
值得检查编译器子命令是否正常工作
/bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | gcc -x c -o a.out -
ls -l a.out
如果结果是临时输出文件的长度为零,这将触发您所看到的错误
/lib64/ld-linux-x86-64.so.2 /dev/null
/dev/null: error while loading shared libraries: /dev/null: file too short
答案2
刚刚弄清楚问题的根源是什么:权限。
海湾合作委员会的权限
\ls -l /usr/bin/gcc
lrwxrwxrwx 1 root root 5 Feb 25 2019 /usr/bin/gcc -> gcc-8
\ls -l /usr/bin/gcc-8
lrwxrwxrwx 1 root root 22 Apr 6 2019 /usr/bin/gcc-8 -> x86_64-linux-gnu-gcc-8
\ls -l /usr/bin/x86_64-linux-gnu-gcc-8
-rwxr-x--- 1 root root 1100664 Apr 6 2019 /usr/bin/x86_64-linux-gnu-gcc-8
ELF对象的权限
\ls -l =(/bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | sudo gcc -x c -o /dev/stdout -)
-rw------- 1 paulo paulo 16600 Mar 30 12:37 /tmp/zshTNxAXl
发出前缀为的命令sudo
sudo /lib64/ld-linux-x86-64.so.2 =(/bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | sudo gcc -x c -o /dev/stdout -)
c one liners
返回正确的结果。