Permissions

載入共享庫時出錯:/tmp/zshgYCSAH: 文件太短

  • March 30, 2020

嘗試在 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

剛剛弄清楚問題的根源是什麼:權限。

GCC 的權限

\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

返回正確的結果。

值得檢查編譯器子命令是否正常工作

/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

引用自:https://unix.stackexchange.com/questions/576816