Useful One Liners

Disassemble Raw ARM Code

If you are in need of doing some ARM disassembly and want to use regular GNU tools the magical command you are looking for is:

$ objdump -m arm --disassembler-options=force-thumb -b binary -D code.bin

This will decode the file 'code.bin' and spit it out on stdout, of course you might want to drop the forcing of 'thumb' decoding depending on whether it is present or not. The source of this wisdom came from the CHDK wiki in regards to disassembling code with GNU tools.

N.B. in the case of when the code is written using ARM Thumb, everything is in 16bit blocks and not 32bit blocks so pay careful attention to reading the output of the disassembler. So if you are reading values from a hexdump, read them in 32bit chunks in your head (aka two lines at a time) but remember it has been stored in a 16bit atomic little-endian format ((when stored 0xaabbccdd, it's actual value to us is 0xccddaabb which is why the values can look wierd)).

Toolchain

$ cat /etc/apt/sources.list.d/emdebian.list 
  deb http://www.emdebian.org/debian/ unstable main
# aptitude install gcc-4.3-arm-linux-gnueabi libc6-armel-cross libc6-dev-armel-cross binutils-arm-linux-gnu linux-libc-dev-armel-cross linux-kernel-headers-armel-cross

Userland ARM Emulation

This is a really nice and little known feature of qemu where even though you might be using a filthy x86/amd64 based workstation, you can run compiled ARM code as if it was not for another architecture.

# echo "deb http://www.emdebian.org/debian `<stable|testing|unstable>` main" > /etc/apt/sources.list.d/emdebian.list

# aptitude install qemu libc6-armel-cross libc6-armel-cross binfmt-support

[edit /usr/local/bin/qemu-wrapper.arm]
# cat /usr/local/bin/qemu-wrapper.arm
#!/bin/sh

/usr/bin/qemu-arm -L /usr/arm-linux-gnueabi "$@"

# chmod +x /usr/local/bin/qemu-wrapper.arm
# update-binfmts --install qemu-arm /usr/local/bin/qemu-wrapper.arm \
                 --magic '\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00' \
                 --mask '\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff'

Now to test if this is all working. Debian ships a hello world application that you can download and use as a good test:

$ dpkg -x hello_2.2-2_armel.deb /tmp/hello_armel

$ qemu-arm -L /usr/arm-linux-gnueabi /tmp/hello_armel/usr/bin/hello
Hello, world!
$ /tmp/hello_armel/usr/bin/hello
Hello, world!