dyld-832.7.1.tar.gz
[apple/dyld.git] / testing / kernel-cache-tests / ctf-x86_64 / test.py
1 #!/usr/bin/python2.7
2
3 import os
4 import KernelCollection
5
6 # This is the fixups-x86_64 test, but with __CTF inserted so that we can see that CTF doesn't impact the result
7 # Note the ctf.txt file is 16k just to ensure that if its vm addr wasn't updated, we'd exceed the size of __LINKEDIT with the __CTF
8
9 def check(kernel_cache):
10 kernel_cache.buildKernelCollection("x86_64", "/ctf-x86_64/main.kc", "/ctf-x86_64/main.kernel", "/ctf-x86_64/extensions", ["com.apple.foo", "com.apple.bar"], [])
11 kernel_cache.analyze("/ctf-x86_64/main.kc", ["-layout", "-arch", "x86_64"])
12
13 assert len(kernel_cache.dictionary()["cache-segments"]) == 7
14 assert kernel_cache.dictionary()["cache-segments"][0]["name"] == "__TEXT"
15 assert kernel_cache.dictionary()["cache-segments"][0]["vmAddr"] == "0xFFFFFF8000200000"
16 assert kernel_cache.dictionary()["cache-segments"][1]["name"] == "__PRELINK_TEXT"
17 assert kernel_cache.dictionary()["cache-segments"][1]["vmAddr"] == "0xFFFFFF8000204000"
18 assert kernel_cache.dictionary()["cache-segments"][2]["name"] == "__TEXT_EXEC"
19 assert kernel_cache.dictionary()["cache-segments"][2]["vmAddr"] == "0xFFFFFF8000204000"
20 assert kernel_cache.dictionary()["cache-segments"][3]["name"] == "__PRELINK_INFO"
21 assert kernel_cache.dictionary()["cache-segments"][3]["vmAddr"] == "0xFFFFFF8000208000"
22 assert kernel_cache.dictionary()["cache-segments"][4]["name"] == "__DATA"
23 assert kernel_cache.dictionary()["cache-segments"][4]["vmAddr"] == "0xFFFFFF800020C000"
24 assert kernel_cache.dictionary()["cache-segments"][5]["name"] == "__HIB"
25 assert kernel_cache.dictionary()["cache-segments"][5]["vmAddr"] == "0xFFFFFF8000100000"
26 assert kernel_cache.dictionary()["cache-segments"][6]["name"] == "__LINKEDIT"
27 assert kernel_cache.dictionary()["cache-segments"][6]["vmAddr"] == "0xFFFFFF8000218000"
28
29 assert len(kernel_cache.dictionary()["dylibs"]) == 3
30 # main.kernel
31 assert kernel_cache.dictionary()["dylibs"][0]["name"] == "com.apple.kernel"
32 assert len(kernel_cache.dictionary()["dylibs"][0]["segments"]) == 5
33 assert kernel_cache.dictionary()["dylibs"][0]["segments"][0]["name"] == "__TEXT"
34 assert kernel_cache.dictionary()["dylibs"][0]["segments"][0]["vmAddr"] == "0xFFFFFF8000204000"
35 assert kernel_cache.dictionary()["dylibs"][0]["segments"][1]["name"] == "__DATA"
36 assert kernel_cache.dictionary()["dylibs"][0]["segments"][1]["vmAddr"] == "0xFFFFFF800020C000"
37 assert kernel_cache.dictionary()["dylibs"][0]["segments"][2]["name"] == "__HIB"
38 assert kernel_cache.dictionary()["dylibs"][0]["segments"][2]["vmAddr"] == "0xFFFFFF8000100000"
39 assert kernel_cache.dictionary()["dylibs"][0]["segments"][3]["name"] == "__CTF"
40 assert kernel_cache.dictionary()["dylibs"][0]["segments"][3]["vmAddr"] == "0xFFFFFF8000204000"
41 assert kernel_cache.dictionary()["dylibs"][0]["segments"][3]["vmSize"] == "0x0"
42 assert kernel_cache.dictionary()["dylibs"][0]["segments"][3]["sections"][0]["vmAddr"] == "0xFFFFFF8000204000"
43 assert kernel_cache.dictionary()["dylibs"][0]["segments"][3]["sections"][0]["vmSize"] == "0x0"
44 assert kernel_cache.dictionary()["dylibs"][0]["segments"][4]["name"] == "__LINKEDIT"
45 assert kernel_cache.dictionary()["dylibs"][0]["segments"][4]["vmAddr"] == "0xFFFFFF8000218000"
46 # bar.kext
47 assert kernel_cache.dictionary()["dylibs"][1]["name"] == "com.apple.bar"
48 assert len(kernel_cache.dictionary()["dylibs"][1]["segments"]) == 3
49 assert kernel_cache.dictionary()["dylibs"][1]["segments"][0]["name"] == "__TEXT"
50 assert kernel_cache.dictionary()["dylibs"][1]["segments"][0]["vmAddr"] == "0xFFFFFF8000205000"
51 assert kernel_cache.dictionary()["dylibs"][1]["segments"][1]["name"] == "__DATA"
52 assert kernel_cache.dictionary()["dylibs"][1]["segments"][1]["vmAddr"] == "0xFFFFFF8000215000"
53 assert kernel_cache.dictionary()["dylibs"][1]["segments"][2]["name"] == "__LINKEDIT"
54 assert kernel_cache.dictionary()["dylibs"][1]["segments"][2]["vmAddr"] == "0xFFFFFF8000218000"
55 # foo.kext
56 assert kernel_cache.dictionary()["dylibs"][2]["name"] == "com.apple.foo"
57 assert len(kernel_cache.dictionary()["dylibs"][2]["segments"]) == 3
58 assert kernel_cache.dictionary()["dylibs"][2]["segments"][0]["name"] == "__TEXT"
59 assert kernel_cache.dictionary()["dylibs"][2]["segments"][0]["vmAddr"] == "0xFFFFFF8000206000"
60 assert kernel_cache.dictionary()["dylibs"][2]["segments"][1]["name"] == "__DATA"
61 assert kernel_cache.dictionary()["dylibs"][2]["segments"][1]["vmAddr"] == "0xFFFFFF8000215010"
62 assert kernel_cache.dictionary()["dylibs"][2]["segments"][2]["name"] == "__LINKEDIT"
63 assert kernel_cache.dictionary()["dylibs"][2]["segments"][2]["vmAddr"] == "0xFFFFFF8000218000"
64
65 # Check the fixups
66 kernel_cache.analyze("/ctf-x86_64/main.kc", ["-fixups", "-arch", "x86_64"])
67 assert len(kernel_cache.dictionary()["fixups"]) == 12
68 # main.kernel: S s = { &func, &func, &g, &func, &g };
69 # _s is at 0xFFFFFF8000208000 which is offset 0x108000 from __HIB
70 assert kernel_cache.dictionary()["fixups"]["0x10C000"] == "kc(0) + 0xFFFFFF8000204FF0 : pointer64"
71 assert kernel_cache.dictionary()["fixups"]["0x10C008"] == "kc(0) + 0xFFFFFF8000204FF0 : pointer64"
72 assert kernel_cache.dictionary()["fixups"]["0x10C010"] == "kc(0) + 0xFFFFFF800021402C : pointer64"
73 assert kernel_cache.dictionary()["fixups"]["0x110000"] == "kc(0) + 0xFFFFFF8000204FF0 : pointer64"
74 assert kernel_cache.dictionary()["fixups"]["0x110008"] == "kc(0) + 0xFFFFFF800021402C : pointer64"
75 # main.kernel: PackedS ps = { 0, &func, &func, 0, &g, 0, &g };
76 # _ps is at 0xFFFFFF8000210000 which is offset 0x110000 from __HIB
77 assert kernel_cache.dictionary()["fixups"]["0x114004"] == "kc(0) + 0xFFFFFF8000204FF0 : pointer64"
78 assert kernel_cache.dictionary()["fixups"]["0x11400C"] == "kc(0) + 0xFFFFFF8000204FF0 : pointer64"
79 assert kernel_cache.dictionary()["fixups"]["0x114018"] == "kc(0) + 0xFFFFFF800021402C : pointer64"
80 assert kernel_cache.dictionary()["fixups"]["0x114021"] == "kc(0) + 0xFFFFFF800021402C : pointer64"
81 # bar.kext: __typeof(&bar) barPtr = &bar;
82 # _barPtr is at 0xFFFFFF8000210030 which is offset 0x110030 from __HIB
83 assert kernel_cache.dictionary()["fixups"]["0x115000"] == "kc(0) + 0xFFFFFF8000205FE0"
84 # foo.kext: int* gPtr = &g;
85 # _gPtr is at 0xFFFFFF8000210040 which is offset 0x110040 from __HIB
86 assert kernel_cache.dictionary()["fixups"]["0x115010"] == "kc(0) + 0xFFFFFF8000215018"
87 # main.kernel: movl _foo, %esp
88 # The _foo reloc is at 0xFFFFFF8000100002 which is offset 0x2 from __HIB
89 assert kernel_cache.dictionary()["fixups"]["0x2"] == "kc(0) + 0x100000 : pointer32"
90 assert len(kernel_cache.dictionary()["dylibs"]) == 3
91 assert kernel_cache.dictionary()["dylibs"][0]["name"] == "com.apple.kernel"
92 assert kernel_cache.dictionary()["dylibs"][0]["fixups"] == "none"
93 assert kernel_cache.dictionary()["dylibs"][1]["name"] == "com.apple.bar"
94 assert kernel_cache.dictionary()["dylibs"][1]["fixups"] == "none"
95 assert kernel_cache.dictionary()["dylibs"][2]["name"] == "com.apple.foo"
96 assert kernel_cache.dictionary()["dylibs"][2]["fixups"] == "none"
97
98 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-static -mkernel -nostdlib -Wl,-kernel -Wl,-e,__start -Wl,-pie main.cpp -Wl,-pagezero_size,0x0 -o main.kernel -Wl,-image_base,0xffffff8000200000 -Wl,-segaddr,__HIB,0xffffff8000100000 -Wl,-add_split_seg_info -Wl,-install_name,/usr/lib/swift/split.seg.v2.hack
99 # [~]> xcrun -sdk macosx.internal ctf_insert main.kernel -arch x86_64 ctf.txt -o main.kernel
100 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info foo.c -o extensions/foo.kext/foo
101 # [~]> xcrun -sdk macosx.internal cc -arch x86_64 -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info bar.c -o extensions/bar.kext/bar
102