0

I am analyzing a binary taken from a STM32 SoC (ArmV6) and it uses a to me unknown calling convention for small functions. First argument in r0, second argument in r3 and return in r1. (In my binary no call with three arguments seem to use this convention)

As an example, a trivial check_if_bit_is_set function:

*************************************************************
*                           FUNCTION                          
*************************************************************
bool  check_if_bit_is_set (uint *  addr , byte  bit )
bool              r1:1           <RETURN>
uint *            r3:4           addr
byte              r0:1           bit

08001230 1a  68           ldr        r2,[addr ,#0x0 ]
08001232 01  21           movs       r1,#0x1
08001234 81  40           lsls       r1,bit
08001236 11  40           ands       r1,r2
08001238 c1  40           lsrs       r1,bit
0800123a 70  47           bx         lr

Why would this be done, is this a known calling convention or used by a specific compiler?

Octetz
  • 159
  • 8
  • 2
    The other weird thing is the inefficiency of that asm. Instead of `(x >> bit) & 1`, it's over-complicating it to `(x & (1<> bit`. I'd hope that most compilers wouldn't make this asm (especially given the non-standard calling convention), and it's plausible that a human could write this if they had a brain fart and missed the obvious pointlessness of clearing the bits *before* right shifting. – Peter Cordes Aug 19 '21 at 03:09
  • 2
    Is it a `static` function? In `static` functions, many compilers don't use any calling convention but they place the arguments in a way that is specific for one single function. – Martin Rosenau Aug 19 '21 at 04:52
  • Good point, it is likely that a large part (or all) functions in this binary can be considered static given that there will never be a need for loading additional code during runtime. – Octetz Aug 19 '21 at 06:11

0 Answers0