1. Overview

In this tutorial, we’ll trace what could be causing the “No such file or directory” error when we try to execute a binary of a program. We’ll use objdump, ldd, and readelf commands that are available on most Linux distributions.

2. Introduction to the Problem

When we try to execute a binary of a program that we download from the internet, or when we deploy our own program to a new environment, we might get a “No such file or directory” error which could be caused by missing libraries or a program interpreter.

2.1. Missing Libraries

Let’s say we want to run a certain binary file:

$ ./binaryfile
./binaryfile: error while loading shared libraries: libbooster.so.0: cannot open shared object file: No such file or directory

The error message says it cannot find the shared library libbooster.so.0.

We can build or download the missing library and put it in the directory where the binary can find it, for example, in the same directory where the binary is in or in one of the directories that are in the PATH variable. After that, we can try to run the binary again:

$ ./binaryfile
./binaryfile: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory

It says it cannot find another library. We can keep repeating the process until all libraries are found, or we can use Linux command such as objdump, ldd, or readelf to list all the libraries the binary requires.

2.2. Missing a Program Interpreter

On another occasion, we may get another similar error but does not give a meaningful message:

$ ./binaryfile
-bash: ./binaryfile: No such file or directory

For this kind of error, assuming the binaryfile file exists and has correct permissions, it’s usually because our system doesn’t have the program interpreter to run the binary. We can use Linux utilities such as readelf to trace what could be causing the error.

3. Using the objdump Command

Let’s use the objdump command to list all the libraries that the binary needs:

$ objdump -p binaryfile | grep NEEDED
    NEEDED libbooster.so.0
    NEEDED libcrypto.so.1.0.0
    NEEDED libpthread.so.0
    ...
    NEEDED libgcc_s.so.1
    NEEDED libc.so.6

We can run through all the libraries that the binary needs and ensure our system has them all.

4. Using the ldd Command

Another way to check that our system has all the libraries that the binary needs are by using the ldd command:

$ ldd binaryfile
    libbooster.so.0 => not found
    libcrypto.so.1.0.0 => not found
    libboost_thread.so.1.68.0 => not found
    ...
    libhogweed.so.4 => /lib/x86_64-linux-gnu/libhogweed.so.4 (0x00007fb0f5036000)
    libgmp.so.10 => /lib/x86_64-linux-gnu/libgmp.so.10 (0x00007fb0f4fb3000)
    libffi.so.6 => /lib/x86_64-linux-gnu/libffi.so.6 (0x00007fb0f4fa9000)

The ldd command gives more detailed information about the libraries that the binary needs.

Note that we should never employ ldd on untrusted variables as it may attempt to obtain the dependency information by attempting to directly execute the program.

5. Using the readelf Command

Another command that will give information about not only the libraries that the binary needs, but also another common cause for the “No such file or directory” error is readelf.

Let’s list all the libraries that the binary needs:

$ readelf -a binaryfile | grep NEEDED
0x0000000000000001 (NEEDED) Shared library: [libbooster.so.0]
0x0000000000000001 (NEEDED) Shared library: [libcrypto.so.1.0.0]
0x0000000000000001 (NEEDED) Shared library: [libboost_thread.so.1.68.0]
...
0x0000000000000001 (NEEDED) Shared library: [libgcc_s.so.1]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]

And verify if the program interpreter exists on our system:

$ readelf -a binaryfile | grep interpreter
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]

The command gives information that it’s requesting a program interpreter called ld-linux-x86-64.so.2.

ld-linux-x86-64.so.2 is a dynamic linker program that’s used to run a 32-bit program on a 64-bit system.

If the program interpreter doesn’t exist, we can usually download it from the official package repositories. Debian or Ubuntu package that has this file is libc6-i386.

6. Conclusion

In this article, there are two common causes for the “No such file or directory” error when we try to run a binary, which is missing libraries and/or a program interpreter. We can use Linux commands such as objdump, ldd, and readelf to check all the libraries that the binary needs and to check if our system has the program interpreter to run the binary.

Comments are closed on this article!