39

I want to build a static hello world from C using arm-linux-gnueabi-gcc as opposed to using the NDK standalone toolchain or Codesourcery for that matter.

In Ubuntu...

I have done the following:

sudo apt-get install gcc-arm-linux-gnueabi

I created a hi.c like this:

#include <stdio.h>

int main(int argc, char** argv) {
   printf("hello world\n");
   return 0;
}

I have compiled it like this:

arm-linux-gnueabi-gcc -static hi.c -o hi 

I ran it on an emulator like this:

adb push hi /data/hi
adb shell /data/hi

But, I get this:

[1]   Illegal instruction     /data/hi

What step have I forgot? Based on past experience this "should" have worked, but I obviously messed this up.

corbin
  • 1,446
  • 2
  • 27
  • 40

6 Answers6

14

Try specifying the architecture/cpu. It sounds like the compiler is creating code with a higher architecture version than the emulator can handle.

This might work:

arm-linux-gnueabi-gcc -static -march=armv5 hi.c -o hi
Leo
  • 2,328
  • 2
  • 21
  • 41
  • Thanks for the response. Immediate result (will dig more), get a warning: target CPU does not support THUMB instructions [enabled by default]. But it still generates a result. Then if I push that unto the device I get the same Illegal instruction. – corbin Feb 21 '12 at 04:31
  • 1
    If I used -march=armv5te I don't get the warning, but still get the illegal instruction error. Similar if I use -march=armv5te -mthumb . – corbin Feb 21 '12 at 04:37
  • 2
    It might be that the standard libraries for arm-linux-gnueabi-gcc uses a higher architecture than the emulator (this sounds strange though). We got the same error when compiling with CodeSourcery for a device with a ARMv4 cpu. Apparantly the CodeSourcery libs were all ARMv5. – Leo Feb 21 '12 at 11:15
  • How do you confirm? Is there a way to tell what architecture a library was built for? The solution was to then build the standard libraries again? Any immediate thoughts on how to get started with that? – corbin Feb 21 '12 at 16:21
  • 1
    Our "solution" was not to cross-compile with CodeSourcery and instead compile on the device (which was running Linux of some kind and had gcc installed). Probably not very helpful for you since we didnt really solve the problem, and not really sure how to confirm if this is the problem or not. – Leo Feb 22 '12 at 13:06
  • I have thought of that, but I am going to avoid compiling on the device for now. Will report back where I end up. Might help you too in the future. – corbin Feb 22 '12 at 17:07
  • I ended up finding a way forward. You may have been right about the libc, but I am not sure how to tell. Thanks for the feedback. – corbin Mar 01 '12 at 19:41
  • 4
    You are correct, the libc that come with the linaro toolchains are built for a v7. So, it will compile your code with the architecture you request, but then when it links it will use the highest architecture used by any of the libraries or your code. – corbin Jun 15 '12 at 03:55
  • When I build with CMake, CMake generated `arm-linux-gnueabi-gcc-8 -g -static -march=armv7-a hello.c.o -c hello.c`, Actually useful `arm-linux-gnueabi-gcc -static -march=armv7-a hello.c -o hello`,I don't know how to fix it. – s f Jan 22 '21 at 01:26
11

It worked for me with CodeBench compiler on ubuntu desktop. https://sourcery.mentor.com/sgpp/lite/arm/portal/release2029

Just create a static binary with this command:

arm-none-linux-gnueabi-gcc -o hello -static hello.c

then push it to phone

adb push hello /data/local/tmp

go run it:

adb shell
$ chmod 755 /data/local/tmp/hello
$ /data/local/tmp/hello

This will print Hello World on terminal. Same can be done from phone also. Use terminal emulator or SL4A bash shell to execute.

pevik
  • 4,523
  • 3
  • 33
  • 44
Srikant
  • 167
  • 7
3

If I do this on a Debian machine (VM in my case), all seems well. I am not sure what when wrong with doing similar on ubuntu. It could be as Leo suggested, but I cannot confirm. This should work for you though.

http://www.cnx-software.com/2012/01/16/installing-emdebian-arm-cross-toolchain-in-debian/

Someone added this link, but it is not using the toolchain I mentioned in the description. Leaving it in case anyone is interested.

http://tariqzubairy.wordpress.com/2012/03/09/arm-binaries-static-library-for-android/

corbin
  • 1,446
  • 2
  • 27
  • 40
1

As far as I know, you cannot run user-land applications within Android that are not compiled with some form of gcc-arm-linux-androideabi.

HacDan
  • 27
  • 1
  • This has been done with the same toolchain, code sourcery and others. Just looking for the missing step. – corbin Feb 17 '12 at 16:12
  • 1
    Wrong. Works with armv5tel-redhat-linux-gnueabi-gcc, for example. As long as you compile static binaries and the architecture is right, it works. – thejh Jun 23 '12 at 10:57
0

Your code actually works for me.

I compiled it on Ubuntu and push it to /data/local/tmp

And then chmod 777 hi

Finally it works well.

Andong Zhan
  • 12,608
  • 4
  • 24
  • 24
  • I figured out (not sure if this is still true) that the cross compiler that ubuntu was grabbing by default only supported hard float and my device was an older one that didn't work with that. – corbin Dec 11 '13 at 06:46
-2

Did you check the permissions of the data folder ? Try using the local instead ! You can just use adb shell and then cd into the folder where the executable was pushed and try ./hi. I guess this is just a permissions issue

psteelk
  • 1,305
  • 3
  • 16
  • 24
  • 2
    Shouldn't it complain about permission denied rather than illegal instruction then? I don't think I have ever gotten "Illegal instruction" unless it has really been some kind of problem with the binary. – Leo Feb 27 '12 at 16:42