Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if ARM or Thumb mode at entry point of program

I'm writing an ARMv7 disassembler. The ways to switch between ARM and Thumb mode are clearly described in the ARM reference manual, but how do you know what mode a program starts in?

I am using Xcode which compiles to thumb by default, so I know that all of my own programs will start in Thumb unless I force compilation to ARM mode. But, I would like to be able to take an arbitrary mach-o executable and find out the instruction set mode at the beginning of the code.

Is there somewhere in the mach-o header that specifies the instruction set upon entry point?

like image 430
Magg G. Avatar asked Dec 09 '22 01:12

Magg G.


2 Answers

The processor knows that it's in Thumb mode by turning on the least-significant bit of the program counter, causing the program counter to have an odd value. This bit is ignored for the purpose of instruction fetching and you can switch between ARM and Thumb mode by toggling this bit.

When you create an ARM binary, the linker will set the least significant bit of the address of a symbol depending on whether this symbol points to ARM or Thumb code so the processor automatically picks the right mode on program start. You don't need to care about this.

like image 173
fuz Avatar answered Mar 02 '23 19:03

fuz


Most operating systems insert a bit of code before your application's entry point, the C Runtime support. They will launch your app in whatever mode that code is written. That code will then mode change as necessary when calling into your main() or other entry point.

In the case of iOS, which is what I assume you're targeting since you're using Xcode, that code is in /usr/local/lib/crt0.o in your iOS SDK directory. Disassembling it shows that the symbol start is ARM code. That is, iOS apps always start running in ARM mode, but they can change mode very early thereafter.

like image 39
Variable Length Coder Avatar answered Mar 02 '23 19:03

Variable Length Coder