<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/56567>56567</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
armv7-a thumb clang reorders ldr before the first inline asm in __attribute__((naked)) function
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
k15tfu
</td>
</tr>
</table>
<pre>
Hi!
Compile the following code with `-O2 -fPIC -march=armv7-a -mthumb`:
```
void * this_ptr;
void (*call_enter_ptr)(void * this_);
__attribute__((naked))
void raw_call_enter() {
#if defined(__aarch64__)
asm volatile(
"stp x0, x1, [sp, #-16]!\n"
// ...
: : :
);
asm volatile("" : : : "memory"); // compiler barrier
asm volatile(
"ldr x0, %1\n"
"blr %0\n"
: : "r"(call_enter_ptr), "m"(this_ptr) : "x0"
);
asm volatile(
// ...
"ldp x0, x1, [sp], #16\n"
: : :
);
asm volatile(
"ret"
);
#elif defined(__arm__)
asm volatile(
"push {r0-r3, r12, lr}\n"
: : :
);
asm volatile("" : : : "memory"); // compiler barrier
asm volatile(
"ldr r0, %1\n"
"blx %0\n"
: : "r"(call_enter_ptr), "m"(this_ptr) : "r0"
);
asm volatile(
"pop {r0-r3, r12, lr}\n"
: : :
);
asm volatile(
"bx lr"
);
#endif
}
```
Output:
```
raw_call_enter():
ldr r0, .LCPI0_0
ldr r1, .LCPI0_1
push.w {r0, r1, r2, r3, r12, lr} <-- and now r0 & r1 are invalid
.LPC0_0:
add r0, pc
.LPC0_1:
add r1, pc
ldr r0, [r0]
ldr r1, [r1]
ldr r2, [r0]
ldr r0, [r1]
blx r2
pop.w {r0, r1, r2, r3, r12, lr}
bx lr
.LCPI0_0:
.Ltmp1:
.long call_enter_ptr(GOT_PREL)-((.LPC0_0+4)-.Ltmp1)
.LCPI0_1:
.Ltmp2:
.long this_ptr(GOT_PREL)-((.LPC0_1+4)-.Ltmp2)
this_ptr:
.long 0
call_enter_ptr:
.long 0
```
armv7-a clang 11.0.1
https://godbolt.org/z/d9dMndvsj
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9VtuOozgQ_RryYoGwCQl54KE72dkdqVfdWu07MtgknjEYGZNk9uu3bCC3Dr1paXZQYoyrfKrqVPmSK_Yj_UN4BHvhxguf-natqkZIjsyOo1JJqQ6i3qJCMY4OwuyQtwj9V4L88u3rGvkV1cXOizZUV_ulT2HA7LoqByUvGgBtv_-5z70SDHnkCQyINmuM9qLnK0kCwoJKmfHacO00yAqGryfasXFi32YZNUaLvDM8yxxMUtPvnLnZqwsTmh6yswGnuULecgQjkSgR46Wo7dwEYG2Mi3mWnWAQPLSt0F5JaoAsi3ES2McjpDUNOoYeWaMjtq0XP7eN65DIxwsv3lji43UNureTv8APBUFwMx49jf-z4D0P95wDE4RcAlgPK14p_cPJLMZot-gLQKOcai2AoIdDlkwPIXsknoqN5FJbeXhffnZPO8-SO6Wwdt734lMV2Rz2E60L5LMMPZYBG-O9tNpkuszaxH4U1udTd-uA5mY6PBJxeVu9uvpk5TZdu7PrQYe-jmxcGhP7ktpbbn52eL-uMvUDlXn8fytT_4TKJI1qfll23jF0tHY-Kr-aiXL4AnfuHQB9-9qZpjNTh8S9LfrKffvYpNqnT2zwsn77GmbhhA6-0MHXOrbegwPqSe0Zda2j9T3HltC17yNaM1SrA5gHEhaggqjmSNR7KgW7DDV4eVtbx279p4xd-N8Ul9p4Whtfat_nAjYl6MSbj7iwOnhahzyAE07j2KU04NyUmGNcNZbwRxm_B5EfB2f0SNuQ_ujEuqma9zQGUsF1BqHbxZv8_vp39vbXby9QaX5_exjzRp7ndnAAHHfSUy1dGSSTBs_bwaQpfGWKnEydb0oT2Fcr6yay_5pzb3GOt7lCUtDFOAiDYc3sjGlai-l25K1iuZImUHoLX__An63YnzXbt99mLI3YKlrRmRFG8nSEdNfDAVhzpRnXrauonJdKD9dOoVsDK0nCIea2JFGjj293qOzqwghVzzot0xsn4dba5QEcHvAh5X58-Y1W33gBp-kX0bYdb6ETL-LFcrZL4yjPKS3oqkxCFq9IEdMlhoEYJ_MQR_OZpDmXbQrFD7thzQ_IQdidMd7MREpCQsIlXuB5CL0AxyueJ_MkBNg8D-fePOQVFTKwflj2Zjp1LuXdtgWhFK1pz0LatmJbc-7MAT7tzE7p9DuOTdnNnOXUef4vHUcGDw">