0

While trying to execute, in the zsh, the following command taken from here

/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 -)

It returns the following error

/tmp/zshgYCSAH: error while loading shared libraries: /tmp/zshgYCSAH: file too short

However, issuing

/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");}

returns the expected behaviour.

Am I missing something really obvious here?

OS and GCC permissions and version

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.

Context

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

The compiler subcommand seems to be working correctly

/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
3
  • What do you get if you replace /lib64/ld-linux-x86-64.so.2 by file? What is your OS and gcc version? Mar 30, 2020 at 2:40
  • Thanks - but I was interested in the result of file on the thing you're trying to load - not on the loader itself i.e. making sure that gcc is emitting a valid ELF object. Also gcc --version without sudo just like in your command (in case it's an alias or your PATH and secure_path are different for example) Mar 30, 2020 at 11:13
  • @steeldriver See updated question. Mar 30, 2020 at 11:36

2 Answers 2

0

It would be worth checking that the compiler subcommand is working correctly

/bin/echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | gcc -x c -o a.out -
ls -l a.out

If the result is that the temporary output file is zero length, this will trigger the error you're seeing

/lib64/ld-linux-x86-64.so.2 /dev/null
/dev/null: error while loading shared libraries: /dev/null: file too short
0

Just figured out what is the origin of the problem: permissions.

Permissions of 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

Permissions of ELF object

\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

Issuing the command prepended with 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

returns the correct result.

4
  • So you were running the gcc subcommand test as root, which worked, but your actual command wasn't running as root so gcc couldn't run. Mar 30, 2020 at 11:58
  • Yes. In my actual command /lib64/ld-linux-x86-64.so.2 wasn't being called as root, thus the problem. Mar 30, 2020 at 12:04
  • In that case the original two examples in your question must have been running with different context too Mar 30, 2020 at 12:18
  • 1
    @roaima You are correct. In the original example in my question I omitted the sudo in call to GCC. But the result was the one with the sudo being called. I thought that it was that wouldn't have any consequence on the question. In the end it turned out that it had. I'm sorry for misleading you and all the other readers. Mar 30, 2020 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .