2

I have this small subroutine (from Hopper, but IDA is similar):

             sub_stringbegin:
000127b8         push       {r11, lr}                                          
000127bc         add        r11, sp, #0x4
000127c0         sub        sp, sp, #0x8
000127c4         str        r0, [r11, #-0x8]
000127c8         ldr        r0, [r11, #-0x8]
000127cc         bl         _ZNSs5beginEv@PLT                                   
000127d0         mov        r3, r0
000127d4         mov        r0, r3
000127d8         sub        sp, r11, #0x4
000127dc         pop        {r11, pc}
                        ; endp

The parameter is passed to this in r0.

Why is this stored into the stack frame then immediately read out? It seems wasteful.

I understand that r0-r3 aren't preserved in the ARM calling convention, but in that instance it would be sufficient to either just store it in the stack frame or pop it onto the stack.

Similarly, moving r0 into r3 and back again seems wasteful after the branch.

This is a ELF executable from a Busybox system.

2
  • Do you know with what level of optimization (and with which compiler) it has been compiled ?
    – perror
    Oct 1, 2014 at 21:50
  • It was built with Yocto and is a "poky" distribution. I suspect it is "arm-poky-linux-gnueabi-gcc" but the specific parameters I don't know. Oct 1, 2014 at 22:00

2 Answers 2

4

This is most likely just non-optimized code. When optimization is disabled, the compiler may do the silliest things.

I hear that many old-time embedded engineers do not enable optimizations in principle because they got burned by optimization bugs in old compilers.

0

This is a straightforward translation of

x = foo;
doSomething(x);

An optimization pass would have realized that x was still in r0 here. Presumably optimizations were turned off.

By the way, it's not that wasteful, as the memory location in question will still be in L1 cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.