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.
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”
:
:
:
);