1

As soon as I'm trying to use labels in inline assembly code the app crashes immediately after the accordant asm statement because of incomprehensible EAX_BAD_ACCESS errors.

For example consider the following code:

asm volatile (
    "myloop:    \n"
    :
    :
    :
);

Why causes this snippet always a crash? I'm using Xcode 4.3.1 with gcc 4.2.

Dominik Seibold
  • 2,439
  • 1
  • 23
  • 29
  • That snippet should normally never crash. Are you sure your application crashes when only using that code? If not, please provide the surrounding code. – mtvec Apr 24 '12 at 13:59
  • Yes, it does. I just created a new empty ios-application and inserted this code somewhere in a startup-method of its AppDelegate. If I start it from the simulator everything works fine, but if I start it from my iPhone it crashes immediately with an inscrutable EAX_BAD_ACCESS error marked in some random disassembly-code. Without this code the app starts normally. – Dominik Seibold Apr 25 '12 at 16:55

1 Answers1

1

The issue is with the linker in the apple toolchain. I faced this issue too. The linker takes any label from the generated assembly and assumes it to be a function start and relocates the section starting from the label. This causes some code to be truncated without function epilog and leaving the PC to be orphaned and drifting to whatever section/function/routine is at next address. The linker understands a local label in two ways (as i understand from reverse engg.) 1. use a capital 'L' in start of label name. This is how the compiler marks its own local labels(loop etc). 2. use numeric labels eg "0:" etc and use directional branching like "b 0f" for forward jump and "b 0b" for backward. Hence the solution to your prob: asm volatile ( "Lmyloop: \n" : : : );

Or asm volatile ( "0: \n"
: : : );

SpeedCoder
  • 51
  • 3
  • Can you provide the source of "The linker understands a local label in two ways "? I met the same problem, and the numeric label magically solved the problem. But I don't know the reason behind this fix. – Mr.Ly Oct 29 '20 at 07:04
  • Tested, starting with "L" works. And the explanation of Apple's linker is here: https://opensource.apple.com/source/ld64/ld64-136/doc/design/linker.html – Mr.Ly Oct 29 '20 at 08:47