- 论坛徽章:
- 11
|
本帖最后由 zylthinking 于 2012-11-05 15:23 编辑
int test()
{
int n = 9;
asm("movw %0, #1\n"
"ldr %0, [pc, #0]\n"
:"=r"(n)
);
return n;
}
反编译后的结果是
0x00004ed8 <test+0>: sub sp, #4
0x00004eda <test+2>: movs r0, #9
0x00004edc <test+4>: movt r0, #0 ; 0x0
0x00004ee0 <test+8>: str r0, [sp, #0]
0x00004ee2 <test+10>: movw r0, #1 ; 0x1
0x00004ee6 <test+14>: ldr.w r0, [pc] ; 0x4ee8 <test+16>
0x00004eea <test+18>: str r0, [sp, #0]
0x00004eec <test+20>: ldr r0, [sp, #0]
0x00004eee <test+22>: add sp, #4
0x00004ef0 <test+24>: bx lr
更离谱的是, 如果 "ldr %0, [pc, #0]\n" 改成 "ldr %0, [pc, #2]\n"
反编译的结果居然是
0x00004ed8 <test+0>: sub sp, #4
0x00004eda <test+2>: movs r0, #9
0x00004edc <test+4>: movt r0, #0 ; 0x0
0x00004ee0 <test+8>: str r0, [sp, #0]
0x00004ee2 <test+10>: movw r0, #1 ; 0x1
0x00004ee6 <test+14>: ldr.w r0, [pc, #-2] ; 0x4ee6 <test+14>
0x00004eea <test+18>: str r0, [sp, #0]
0x00004eec <test+20>: ldr r0, [sp, #0]
0x00004eee <test+22>: add sp, #4
0x00004ef0 <test+24>: bx lr
又一个神奇的:
int test()
{
int n = 9;
asm("mov %0, pc\n"
"ldr %0, [pc, #2]\n"
:"=r"(n)
);
return n;
}
(gdb) disassemble
Dump of assembler code for function test:
0x00004ed8 <test+0>: sub sp, #4
0x00004eda <test+2>: movs r0, #9
0x00004edc <test+4>: movt r0, #0 ; 0x0
0x00004ee0 <test+8>: str r0, [sp, #0]
0x00004ee2 <test+10>: mov r0, pc
0x00004ee4 <test+12>: ldr.w r0, [pc, #-2] ; 0x4ee6 <test+14>
0x00004ee8 <test+16>: str r0, [sp, #0]
0x00004eea <test+18>: ldr r0, [sp, #0]
0x00004eec <test+20>: add sp, #4
0x00004eee <test+22>: bx lr
End of assembler dump.
(gdb) si
0x00004ee4 97 asm("mov %0, pc\n"
(gdb) info register r0
r0 0x4ee6 20198
(gdb) info register pc
pc 0x4ee4 20196
(gdb)
貌似是 gdb 的 bug, 和 llvm 无关? 似乎是刻意的想将 pc - 4 来反映当前指令对应的 pc 导致的错乱 |
|