How do I make labels in inline assembly unique?
The statement of an inline assembler can contain a jump to a label defined in the same inline assembler. If the inline assembler code can be reused like in the following example, the compiler will raise an error if the label is not unique.
Example 1
void foo(void)
{
asm volatile(
"named_label:\n"
"jl named_label\n"
);
}
void bar(void)
{
foo();
}
This example will run fine with low/no optimization. However, at higher optimization levels, the inline assembly will be cloned and thus run into an error:
Error
error: symbol 'named_label' is already defined
"named_label:\n"
^
<inline asm>:1:2: note: instantiated into assembly here
named_label:
^
To fix this behavior, the labels in the inline assembler should use the following syntax: label_%=
.
Example 2
void foo(void)
{
asm volatile(
"named_label_%=:\n"
"jl named_label_%=\n"
:
);
}
void bar(void)
{
foo();
}
This will number labels if the compiler issues them more than once and grants them a unique identifier. Otherwise, the compiler will issue an error that the label is used more than once.
Map file
80000000 80000000 0 1 named_label_0
80000000 80000000 6 1 foo
80000006 80000006 0 1 named_label_1
80000006 80000006 6 1 bar