当尝试在我的 asm 程序中为 iphone (arm64) 创建跳转表时,我遇到了一个很奇怪的问题:
.globl my_func
my_func:
...
//jump (switch) table
.L.f_switch:
.short .L.case0 - .L.f_switch
.short .L.case1 - .L.f_switch
...
.L.case0:
//some case code
...
.L.case1:
//other case code
编译后,此表将由零而不是实际值填充。通过转储编译好的目标文件可以看出。
(__TEXT,__text) section
_my_func:
0000000000000000 adr x4, #16
0000000000000004 ldrh w5, [x4, x3, lsl #1]
0000000000000008 add x4, x4, w5, uxth
000000000000000c br x4
.L.f_switch:
0000000000000010 .long 0x00000000
0000000000000014 .long 0x00000000
0000000000000018 .long 0x00000000
000000000000001c nop
如何解决?
最佳答案
我相信您观察到的条目设置为 0 与重定位有关。编译器可能发出链接器最终解析的重定位信息。为此,我创建了这个小示例程序:
测试.s
.text
.align 4
.globl _main
_main:
adr x0, .L.f_switch
ldr w1, [x0, x1, LSL#2]
add x0, x0, x1
br x0
.L.f_switch:
.word .L.case0 - .L.f_switch
.word .L.case1 - .L.f_switch
.word .L.case2 - .L.f_switch
.L.case0:
nop
.L.case1:
nop
.L.case2:
nop
ret
我正在使用 XCode 7,clang 报告了 clang --version
的版本信息:
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
为了简化命令行操作,我设置了一个环境变量来指向我的 iPhone SDK:
export ISYSROOT="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/"
第一个实验是将test.s
编译成test.o
。我使用这个命令:
clang -x assembler -arch arm64 test.s -o test.o -c
现在,如果我使用 otool
转储 test.o,使用:
otool -drGtv test.o
我明白了:
test.o:
Data in code table (0 entries)
offset length kind
Relocation information (__TEXT,__text) 6 entries
address pcrel length extern type scattered symbolnum/value
00000018 False long True SUB False .L.f_switch
00000018 False long True UNSIGND False .L.case2
00000014 False long True SUB False .L.f_switch
00000014 False long True UNSIGND False .L.case1
00000010 False long True SUB False .L.f_switch
00000010 False long True UNSIGND False .L.case0
(__TEXT,__text) section
_main:
0000000000000000 adr x0, #16
0000000000000004 ldr w1, [x0, x1, lsl #2]
0000000000000008 add x0, x0, x1
000000000000000c br x0
.L.f_switch:
0000000000000010 .long 0x00000000
0000000000000014 .long 0x00000000
0000000000000018 .long 0x00000000
.L.case0:
000000000000001c nop
.L.case1:
0000000000000020 nop
.L.case2:
0000000000000024 nop
0000000000000028 ret
编译器(汇编程序)已为等式的两个部分(.L.case#
和 .L.F_switch
发出了 00000010、00000014 和 00000018 的重定位条目).表格本身充满了占位符零。解决重定位将是链接器的工作。我可以使用如下命令手动链接上面的 test.o
:
ld -demangle -dynamic -arch arm64 -iphoneos_version_min 5.0.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/ -o test -L/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk//usr/lib/system test.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.0/lib/darwin/libclang_rt.ios.a
我现在可以使用 otool
通过如下命令转储最终的可执行文件:
otool -drGtv test
并得到这个输出:
test:
Data in code table (0 entries)
offset length kind
(__TEXT,__text) section
_main:
0000000100007f80 adr x0, #16
0000000100007f84 ldr w1, [x0, x1, lsl #2]
0000000100007f88 add x0, x0, x1
0000000100007f8c br x0
.L.f_switch:
0000000100007f90 .long 0x0000000c
0000000100007f94 .long 0x00000010
0000000100007f98 .long 0x00000014
.L.case0:
0000000100007f9c nop
.L.case1:
0000000100007fa0 nop
.L.case2:
0000000100007fa4 nop
0000000100007fa8 ret
请注意,所有重定位都已由最终可执行文件中的链接器解析。
或者,我可以一步编译和链接所有内容,生成可执行文件 test
,命令如下:
clang -x assembler -arch arm64 -L$ISYSROOT/usr/lib/system --sysroot=$ISYSROOT test.s -o test
我将其拆分以显示目标文件的外观以及链接后生成的可执行文件。
关于xcode - 创建常量跳转表;代码;铛;汇编,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32642544/