Native Embedded Code on Android

 

 

 

 

 

While this is not exactly the norm CNC, embeded, laser cutting post here.  I am posting how to compile and write C code to run directly on Andorid.

  • Download the Android NDK .
  • Extract it to where ever you like.  I put it (/Applications/eclipse/android-ndk-r7b)
  • Open a terminal and cd to where you put it.
  • Then run the build script: (android-5 is gingerbread)
/build/tools/make-standalone-toolchain.sh --platform=android-5 --install-dir=/tmp/my-android-toolchain

 

Now that you have your toolchain built.. (insane easy!  thx google) its time to test it out.

#include 
void main(){
printf("hello world!\n");
}

Here is out little program to test out our new arm toolchain.
Lets compile it with:

./arm-linux-androideabi-gcc -o hello hello.c

If all goes well you should have a arm executable named hello in the same directory.

Running file on hello should in fact show us our binary is of ARM architecture!

hello: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

Here is the arm-linux-objdump output from hello: (-d is the disassemble flag)

./arm-linux-androideabi-objdump -d hello

hello:     file format elf32-littlearm
Disassembly of section .plt:

000082d4 :
    82d4:	e52de004 	push	{lr}		; (str lr, [sp, #-4]!)
    82d8:	e59fe004 	ldr	lr, [pc, #4]	; 82e4 
    82dc:	e08fe00e 	add	lr, pc, lr
    82e0:	e5bef008 	ldr	pc, [lr, #8]!
    82e4:	00001158 	.word	0x00001158
    82e8:	e28fc600 	add	ip, pc, #0	; 0x0
    82ec:	e28cca01 	add	ip, ip, #4096	; 0x1000
    82f0:	e5bcf158 	ldr	pc, [ip, #344]!
    82f4:	e28fc600 	add	ip, pc, #0	; 0x0
    82f8:	e28cca01 	add	ip, ip, #4096	; 0x1000
    82fc:	e5bcf150 	ldr	pc, [ip, #336]!

Disassembly of section .text:

00008300 :
    8300:	e1a0000d 	mov	r0, sp
    8304:	e3a01000 	mov	r1, #0	; 0x0
    8308:	e28f2004 	add	r2, pc, #4	; 0x4
    830c:	e28f3004 	add	r3, pc, #4	; 0x4
    8310:	eafffff7 	b	82f4 
    8314:	ea000005 	b	8330 
    8318:	0000935c 	.word	0x0000935c
    831c:	00009364 	.word	0x00009364
    8320:	0000936c 	.word	0x0000936c
    8324:	00009374 	.word	0x00009374
    8328:	e1a00000 	.word	0xe1a00000
    832c:	e1a00000 	.word	0xe1a00000

00008330 :
    8330:	e92d4800 	push	{fp, lr}
    8334:	e28db004 	add	fp, sp, #4	; 0x4
    8338:	e59f300c 	ldr	r3, [pc, #12]	; 834c 
    833c:	e08f3003 	add	r3, pc, r3
    8340:	e1a00003 	mov	r0, r3
    8344:	ebffffe7 	bl	82e8 
    8348:	e8bd8800 	pop	{fp, pc}
    834c:	0000000c 	.word	0x0000000c

There we see our ARM registers (r’s) doing their thing. Admittedly I am still getting used to looking at arm assembly.

So now that we know this is the right arch we can push this up to our device via adb.  adb is included in the Android SDK download.

./adb push /tmp/my-android-toolchain/bin/hello /data/hello

This will upload the arm binary hello to the /data/hello location. Note you must have your phone connected (or emulator.. duh right?)
Once this is uploaded we will use adb again to get a shell on the phone/tablet/whatever.

adb shell
#

This returns us to a blank shell. I personally like bash so I toss out a bash command and drop right into the bash shell.
From here we make the hello file executable and run it!

chmod +x /data/hello
/data/hello

Which gives us:

localhost / # /data/hello 
Hello Worldlocalhost / #

There we have it! C code without any official Java interfaces to the NDK. Pretty cool.  As a afterthought I did this all from OSX.  So any specific Windows questions on how to do the same I will not be able to assist with.  Better just “read the docs” from google on that.
Anyhow this post is mainly for me to keep around for my records on how to do this in the future.

Comments Are Closed