我相信您所观察到的条目设置为 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
现在,如果我使用以下方式转储 test.o otool
:
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
我将其拆分以显示目标文件的外观,然后显示链接后生成的可执行文件。