neshkeev
neshkeev

Reputation: 6486

Executable says "line 1: ELF: not found" when starts

I try to build a simple hello, world ELF for my router Xiaomi Router 3g with cmake. It runs

Linux OpenWrt 4.14.95 #0 SMP Wed Jan 30 12:21:02 2019 mips GNU/Linux

I use the sdk for ramips platform (my router's platform).

The idea is to use $(PKG_BUILD_DIR) as a directory to perform an external build (which is usually done via mkdir build ; cd build ; cmake ..)

The build finishes successfully and when I install it on my router and start it, it fails with:

root@OpenWrt:~# chw
/usr/bin/chw: line 1: ELF: not found
/usr/bin/chw: line 2: syntax error: unexpected "("

When I build the ELF without cmake (

  1. comment out the line include $(INCLUDE_DIR)/cmake.mk,
  2. uncomment the $(CP) ... line in the define Build/Prepare section
  3. eliminate the define Build/Compile section

), it works just fine: it prints Hello, World!.

Log file is here, .config file is here

The source code is available here

Here is the source code:

main.c:

#include <stdio.h>

int main()
{
    printf("Hello, World!");
    return 0;
}

CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)

project (chw)

add_executable(chw main.c)

install(TARGETS chw DESTINATION /usr/bin)

Makefile:

include $(TOPDIR)/rules.mk

PKG_NAME:=chw
PKG_VERSION:=0.1
PKG_RELEASE:=1

PKG_MAINTAINER:=John Doe <[email protected]>
PKG_LICENSE:=CC0-1.0

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk

define Package/chw
    SECTION:=utils
    CATEGORY:=Utilities
    TITLE:=Hello world application
    URL:=https://www.example.com
endef

define Package/chw/description
    hello world application
endef

define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    # $(CP) ./src/{main.c,Makefile} $(PKG_BUILD_DIR)/
endef

define Build/Configure
    cmake -B $(PKG_BUILD_DIR) -S ./src
endef

define Build/Compile
    $(call Build/Compile/Default,-C $(PKG_BUILD_DIR))
endef

define Package/chw/install
    $(INSTALL_DIR) $(1)/usr/bin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/chw $(1)/usr/bin/
endef

$(eval $(call BuildPackage,chw))

Upvotes: 5

Views: 47658

Answers (5)

Owen DeLong
Owen DeLong

Reputation: 117

Another possibility is that the ELF is not for the correct architecture (e.g. trying to execute an X86_64 ELF on an ARM system).

Upvotes: 2

Jay Harrell
Jay Harrell

Reputation: 21

It sounds like you are using WSL 1. You need to switch to WSL 2 to run ELF

Upvotes: 2

Colin Rooney
Colin Rooney

Reputation: 437

Did you remember to change the file property to make it executable?

chmod +x filename

I know I often forget this step when I download something (usually an installation binary). Just a thought!

Upvotes: 2

Alex
Alex

Reputation: 705

In addition to other answers here, there might be simple cause for "line 1: ELF: not found, line 2: syntax error: unexpected "("":

You are running a 64-bit program on a 32-bit machine. Try changing your compiling config.

Upvotes: 16

Keith Thompson
Keith Thompson

Reputation: 263627

Apparently your target system either doesn't support executing ELF files, or doesn't recognize your file as an ELF file.

When you execute a file on a UNIX-like system, it will be executed directly if the kernel recognizes it as an executable format. If not, if there's no #! line at the top, it will try to execute it as a shell script using /bin/sh. It's a binary file, but there's no really firm distinction between binary and text files.

The shell apparently tried to interpret the beginning of the file (which includes the characters ELF) as a command, and wasn't able to find that command in $PATH.

You'll need to find a way to generate an executable file that your target system will recognize and accept. (I don't know how to do that.)

Upvotes: 9

Related Questions