]> git.saurik.com Git - apple/dyld.git/blob - testing/kernel-cache-tests/data-const/test.py
dyld-851.27.tar.gz
[apple/dyld.git] / testing / kernel-cache-tests / data-const / test.py
1 #!/usr/bin/python2.7
2
3 import os
4 import KernelCollection
5
6 # __DATA_CONST is r/o in xnu and r/w in the kext's but should be r/o in the final image
7
8 def check(kernel_cache):
9 kernel_cache.buildKernelCollection("arm64", "/data-const/main.kc", "/data-const/main.kernel", "/data-const/extensions", ["com.apple.foo", "com.apple.bar"], [])
10 kernel_cache.analyze("/data-const/main.kc", ["-layout", "-arch", "arm64"])
11
12 assert len(kernel_cache.dictionary()["cache-segments"]) == 7
13 assert kernel_cache.dictionary()["cache-segments"][0]["name"] == "__TEXT"
14 assert kernel_cache.dictionary()["cache-segments"][0]["vmAddr"] == "0x0"
15 assert kernel_cache.dictionary()["cache-segments"][0]["vmSize"] == "0x4000"
16 assert kernel_cache.dictionary()["cache-segments"][1]["name"] == "__PRELINK_TEXT"
17 assert kernel_cache.dictionary()["cache-segments"][1]["vmAddr"] == "0x4000"
18 assert kernel_cache.dictionary()["cache-segments"][1]["vmSize"] == "0x8000"
19 assert kernel_cache.dictionary()["cache-segments"][2]["name"] == "__TEXT_EXEC"
20 assert kernel_cache.dictionary()["cache-segments"][2]["vmAddr"] == "0xC000"
21 assert kernel_cache.dictionary()["cache-segments"][2]["vmSize"] == "0xC000"
22 assert kernel_cache.dictionary()["cache-segments"][3]["name"] == "__DATA_CONST"
23 assert kernel_cache.dictionary()["cache-segments"][3]["vmAddr"] == "0x18000"
24 assert kernel_cache.dictionary()["cache-segments"][3]["vmSize"] == "0x8000"
25 assert kernel_cache.dictionary()["cache-segments"][4]["name"] == "__PRELINK_INFO"
26 assert kernel_cache.dictionary()["cache-segments"][4]["vmAddr"] == "0x20000"
27 assert kernel_cache.dictionary()["cache-segments"][5]["name"] == "__DATA"
28 assert kernel_cache.dictionary()["cache-segments"][5]["vmAddr"] == "0x24000"
29 assert kernel_cache.dictionary()["cache-segments"][5]["vmSize"] == "0x8000"
30 assert kernel_cache.dictionary()["cache-segments"][6]["name"] == "__LINKEDIT"
31 assert kernel_cache.dictionary()["cache-segments"][6]["vmAddr"] == "0x2C000"
32
33 assert len(kernel_cache.dictionary()["dylibs"]) == 3
34 # main.kernel
35 assert kernel_cache.dictionary()["dylibs"][0]["name"] == "com.apple.kernel"
36 assert len(kernel_cache.dictionary()["dylibs"][0]["segments"]) == 5
37 assert kernel_cache.dictionary()["dylibs"][0]["segments"][0]["name"] == "__TEXT"
38 assert kernel_cache.dictionary()["dylibs"][0]["segments"][0]["vmAddr"] == "0xC000"
39 assert kernel_cache.dictionary()["dylibs"][0]["segments"][1]["name"] == "__DATA"
40 assert kernel_cache.dictionary()["dylibs"][0]["segments"][1]["vmAddr"] == "0x24000"
41 assert kernel_cache.dictionary()["dylibs"][0]["segments"][2]["name"] == "__TEXT_EXEC"
42 assert kernel_cache.dictionary()["dylibs"][0]["segments"][2]["vmAddr"] == "0x10000"
43 assert kernel_cache.dictionary()["dylibs"][0]["segments"][3]["name"] == "__DATA_CONST"
44 assert kernel_cache.dictionary()["dylibs"][0]["segments"][3]["vmAddr"] == "0x18000"
45 assert kernel_cache.dictionary()["dylibs"][0]["segments"][4]["name"] == "__LINKEDIT"
46 assert kernel_cache.dictionary()["dylibs"][0]["segments"][4]["vmAddr"] == "0x2C000"
47 # bar.kext
48 assert kernel_cache.dictionary()["dylibs"][1]["name"] == "com.apple.bar"
49 assert len(kernel_cache.dictionary()["dylibs"][1]["segments"]) == 5
50 assert kernel_cache.dictionary()["dylibs"][1]["segments"][0]["name"] == "__TEXT"
51 assert kernel_cache.dictionary()["dylibs"][1]["segments"][0]["vmAddr"] == "0x4000"
52 assert kernel_cache.dictionary()["dylibs"][1]["segments"][1]["name"] == "__TEXT_EXEC"
53 assert kernel_cache.dictionary()["dylibs"][1]["segments"][1]["vmAddr"] == "0x14000"
54 assert kernel_cache.dictionary()["dylibs"][1]["segments"][2]["name"] == "__DATA"
55 assert kernel_cache.dictionary()["dylibs"][1]["segments"][2]["vmAddr"] == "0x28000"
56 assert kernel_cache.dictionary()["dylibs"][1]["segments"][3]["name"] == "__DATA_CONST"
57 assert kernel_cache.dictionary()["dylibs"][1]["segments"][3]["vmAddr"] == "0x1C000"
58 assert kernel_cache.dictionary()["dylibs"][1]["segments"][4]["name"] == "__LINKEDIT"
59 assert kernel_cache.dictionary()["dylibs"][1]["segments"][4]["vmAddr"] == "0x2C000"
60 # foo.kext
61 assert kernel_cache.dictionary()["dylibs"][2]["name"] == "com.apple.foo"
62 assert len(kernel_cache.dictionary()["dylibs"][2]["segments"]) == 4
63 assert kernel_cache.dictionary()["dylibs"][2]["segments"][0]["name"] == "__TEXT"
64 assert kernel_cache.dictionary()["dylibs"][2]["segments"][0]["vmAddr"] == "0x8000"
65 assert kernel_cache.dictionary()["dylibs"][2]["segments"][1]["name"] == "__TEXT_EXEC"
66 assert kernel_cache.dictionary()["dylibs"][2]["segments"][1]["vmAddr"] == "0x14030"
67 assert kernel_cache.dictionary()["dylibs"][2]["segments"][2]["name"] == "__DATA"
68 assert kernel_cache.dictionary()["dylibs"][2]["segments"][2]["vmAddr"] == "0x28004"
69 assert kernel_cache.dictionary()["dylibs"][2]["segments"][3]["name"] == "__LINKEDIT"
70 assert kernel_cache.dictionary()["dylibs"][2]["segments"][3]["vmAddr"] == "0x2C000"
71
72 # Check we have the correct addresses of the symbols being bound to
73 kernel_cache.analyze("/data-const/main.kc", ["-symbols", "-arch", "arm64"])
74 # int g = &x;
75 assert kernel_cache.dictionary()["dylibs"][0]["global-symbols"][3]["name"] == "_x"
76 assert kernel_cache.dictionary()["dylibs"][0]["global-symbols"][3]["vmAddr"] == "0x24000"
77 # foo()
78 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][0]["name"] == "_foo"
79 assert kernel_cache.dictionary()["dylibs"][2]["global-symbols"][0]["vmAddr"] == "0x14030"
80
81 kernel_cache.analyze("/data-const/main.kc", ["-fixups", "-arch", "arm64"])
82 # int g = &x;
83 assert kernel_cache.dictionary()["fixups"]["0x18000"] == "kc(0) + 0x24000"
84 # foo()
85 assert kernel_cache.dictionary()["fixups"]["0x1C000"] == "kc(0) + 0x14030"
86
87 # [~]> xcrun -sdk iphoneos.internal cc -arch arm64 -Wl,-static -mkernel -nostdlib -Wl,-add_split_seg_info -Wl,-rename_section,__TEXT,__text,__TEXT_EXEC,__text -Wl,-e,__start -Wl,-pagezero_size,0x0 -Wl,-pie -Wl,-rename_section,__DATA,__data,__DATA_CONST,__data -Wl,-segprot,__DATA_CONST,r--,r-- main.c -o main.kernel
88 # [~]> xcrun -sdk iphoneos.internal cc -arch arm64 -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info foo.c -o extensions/foo.kext/foo
89 # [~]> xcrun -sdk iphoneos.internal cc -arch arm64 -Wl,-kext -mkernel -nostdlib -Wl,-add_split_seg_info bar.c -o extensions/bar.kext/bar
90 # [~]> rm -r extensions/*.kext/*.ld
91