Home > Blockchain >  cannot execute binary file: Exec format error for a binary compiled in WSL environment
cannot execute binary file: Exec format error for a binary compiled in WSL environment

Time:01-27

I have recently started working on WSL environment. I have compiled one program in WSL environment which is of type "ELF 64-bit LSB shared object" but when I try to run it in the same environment, it throws an error "cannot execute binary file: Exec format error".

I am not sure about what I am missing here. How can I run the binary. Kindly suggest.

kshitij@APL-5CD010D2WP:~/runtime/bin$ ioc_broker &
[1] 3579
kshitij@APL-5CD010D2WP:~/runtime/bin$ bash: /bin/ioc_broker: cannot execute binary file: Exec format error
^C
[1]   Exit 126                ioc_broker
kshitij@APL-5CD010D2WP:~/runtime/bin$ file ioc_broker 
ioc_broker: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=aa1e76d798d46d45f131cb53de8f947ddb4c8526, for GNU/Linux 3.2.0, not stripped
kshitij@APL-5CD010D2WP:~/runtime/bin$ uname -srv
Linux 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022
kshitij@APL-5CD010D2WP:~/runtime/bin$ 

kshitij@APL-5CD010D2WP:/mnt/d/src/myproject/build$ uname -a
Linux APL-5CD010D2WP 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

I have compiled the binary using cmake in which I have specified nothing specific about comiler being used.

kshitij@APL-5CD010D2WP:/mnt/d/src/myproject/build$ cmake -DCMAKE_INSTALL_PREFIX=/home/kshitij/runtime ..
-- The CXX compiler identification is GNU 9.4.0
-- Check for working CXX compiler: /bin/c  
-- Check for working CXX compiler: /bin/c   -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Line: 57 Using COMMON_API_VERSION 3.1.12

CodePudding user response:

uname -srv
Linux 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022

Try uname -a instead. It should identify the kernel as either i686 or x86_64.

I expect that your kernel says i686, in which case you've installed a 32-bit WSL2 and are trying to run a 64-bit binary on it. That doesn't work for obvious reason -- you need a 64-bit kernel to run 64-bit binaries.

Update:

I see x86_64

Ok, my guess was wrong. The other reason for Exec format error is that the file is corrupt in some way.

One possible source of corruption is if you run out of disk space and the file is truncated. Check df -h . output and if there is a lot of space, try rebuilding the binary again.

Also try eu-readelf -a ioc_broker -- eu-readelf is good at warning about corrupt ELF files.

Finally, look in /var/log/messages for any kernel warnings -- you may have a bad disk block or something else.

  • Related