`bash: ./ a.out: no such file or directory` when running the executable created by` ld`

Here is the Hello World code in C:

// a.c
#include <stdio.h>

int main() {
    printf("Hello world\n");
    return 0;
}

I will compile it as gcc a.c, which produces a.outas expected, and ./a.outprints Hello world... as expected.

Now, if I do the compilation and the link separately: gcc -c a.c; ld -lc a.oit starts a.out, created as ./a.out. I get a message:

bash: ./a.out: No such file or directory

I was looking for this error and it seems to happen when the executable is a 32-bit ELF and the machine architecture is 64-bit.

I start a 64 bit machine and it works file a.out:

a.out: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

Why is this happening?

EDIT:

Output uname -m

$ uname -m
x86_64

Output ldd a.out

$ ldd a.out
    linux-vdso.so.1 =>  (0x00007ffeeedfb000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa13a7b8000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fa13abab000)

gcc a.ccreates a.outthat works correctly.

+4
3

, , , .

gcc -c a.c; ld -lc a.o, , :

ld: warning: cannot find entry symbol _start; defaulting to 0000000000400260

, , , , . . @EmployedRussian answer , .


, , :

$ strace ./a.out 
execve("./a.out", ["./a.out"], [/* 72 vars */]) = -1 ENOENT (No such file or directory)

execve(2) ENOENT, ( file .., . ). ,

#!/usr/non-existant-path/bin/bash

, ELF (, 64- 32- ). , .


Ubuntu 15.10, GNU file 5.22:

a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld64.so.1, not stripped

/lib/ld64.so.1. ldd , ldd ELF , , .

$ ldd a.out
        linux-vdso.so.1 =>  (0x00007ffc18d2b000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f0e0a79f000)
        /lib/ld64.so.1 => /lib64/ld-linux-x86-64.so.2 (0x0000559dbc9d2000)

, , , ldd, .

ldd, , , /lib64/ld-linux-x86-64.so.2 . - , , , , , , .

readelf -l a.out

ELF , . ( @EmployedRussian .)

+4

ld -lc a.o

:

  • ld (gcc ) .

    , , gcc, , , , .

    , gcc -v a.o.

    , gcc (, crt1.o , ), OS- ( ld).

  • . ld -lc a.o () ld a.o -lc. .

+4

:

  ld -o a.out -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o -lc c.o /usr/lib/crtn.o
+2

Source: https://habr.com/ru/post/1617820/


All Articles