1

I am trying to write THUMB assembly code and I want to run it on Linux (x86). I can write ARM assembly and it works fine, but when compiling with the -mthumb option, nothing works. I can't find any tutorial online, so any resources would be great.

Here is the assembly code I am using :

.text

.globl _start
_start:
add r0, r0, $0

Here are the compilation options I am using :

arm-linux-gnueabihf-as -march=armv7-m -mcpu=cortex-m0 -mthumb -o main.o main.s
arm-linux-gnueabihf-ld -o main main.o

Here is how I try to run the binary :

LD_LIBRARY_PATH=/lib/arm-linux-gnueabihf qemu-arm main

Though, I did try to specify the CPU here as well (and also tried all sorts of combinations). I get the following error message :

qemu: uncaught target signal 4 (Illegal instruction) - core dumped
Illegal instruction

Finally, using gdb-multiarch, all instructions are indeed shown as invalid (but I am unsure how to actually use gdb-multiarch, just followed a guide online).

I am obviously doing something wrong (whether it is the format of instructions or along with the compilation), I hope someone can clarify what is wrong and maybe give some online resources. Note that I don't have any issue with ARM/THUMB per se, I have been studying it for a bit, but never tried to write any code.

Leop
  • 11
  • 1
  • Try to add a `.thumb_func` directive before `_start:`. Also, I'm not sure if `$` as a prefix for immediate operands is correct. `#` is the usual prefix on ARM. You might also want to use `.syntax unified`. – fuz May 18 '22 at 16:46
  • When you single-step with a debugger connected to QEMU, does it fault on the `add`? Or on whatever garbage is in memory after it, since you don't make an `_exit` system call. [How to single step ARM assembly in GDB on QEMU?](https://stackoverflow.com/a/51310791) is a complete example of doing this. – Peter Cordes May 18 '22 at 17:15
  • `.thumb_func` does not compile (invalid pseudo_op). GDB shows the correct instruction but if I try to single step using `si` it crashes with "illegal instruction". – Leop May 19 '22 at 15:46
  • [Understanding this part arm assembly code](https://stackoverflow.com/q/22396214) shows usage of that directive for GNU assembler like you're using. Your `arm-linux-gnueabihf-as` should definitely support it, although possibly there's some context (earlier directives) or options that are required. – Peter Cordes May 19 '22 at 16:27
  • @Leop: This will not solve your problem but please note that the architecture for the `cortex-m0` is `armv6-m` , not `armv7-m`. This being said, what is it that you really want to do: run/debug arm linux thumb assembly programs using qemu-arm, or run/debug cortex-m0 assembly programs on a qemu virtual machine using qemu-system-arm ? – Frant May 21 '22 at 03:43

0 Answers0