]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/bind-rebase.dtest/main.c
015bee1dd115d6d43843a28410b22a30f068ab26
[apple/dyld.git] / testing / test-cases / bind-rebase.dtest / main.c
1
2 // BUILD: $CC main.c -Wl,-fixup_chains -o $BUILD_DIR/bind-rebase.exe
3
4 // RUN: ./bind-rebase.exe
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <dlfcn.h>
10 #include <assert.h>
11
12 extern char tzname[]; // a char array in libSystem.dylib
13
14
15 #define VERIFY(a,b) assert(a==b)
16
17
18 static uint8_t a;
19 uint8_t* const rebasedPtrs[] = { NULL, NULL, &a, &a+1, &a+16, &a+1023, NULL, &a-1 };
20
21 void verifyRebases()
22 {
23 VERIFY(rebasedPtrs[0], NULL);
24 VERIFY(rebasedPtrs[1], NULL);
25 VERIFY(rebasedPtrs[2], &a);
26 VERIFY(rebasedPtrs[3], &a+1);
27 VERIFY(rebasedPtrs[4], &a+16);
28 VERIFY(rebasedPtrs[5], &a+1023);
29 VERIFY(rebasedPtrs[6], NULL);
30 VERIFY(rebasedPtrs[7], &a-1);
31 }
32
33
34 static char* const bindPtrs[] = { NULL, NULL, tzname, tzname+1, &tzname[16], &tzname[1023], NULL, &tzname[-1], (char*)&malloc, (char*)&free };
35
36 void verifyBinds()
37 {
38 VERIFY(bindPtrs[0], NULL);
39 VERIFY(bindPtrs[1], NULL);
40 VERIFY(bindPtrs[2], tzname);
41 VERIFY(bindPtrs[3], tzname+1);
42 VERIFY(bindPtrs[4], tzname+16);
43 VERIFY(bindPtrs[5], tzname+1023);
44 VERIFY(bindPtrs[6], NULL);
45 VERIFY(bindPtrs[7], tzname-1);
46 VERIFY(bindPtrs[8], (char*)&malloc);
47 VERIFY(bindPtrs[9], (char*)&free);
48 }
49
50 #if !__LP64__
51 #define JUNK ((uint8_t*)0x12345678)
52 uint8_t* const otherPtrs[] = {
53 &a,
54 // far enough apart to require co-opting a NULL
55 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
56 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
57 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
58 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
59 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
60 &a+1,
61 // far enough apart to require co-opting two NULLs
62 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
63 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
64 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
65 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
66 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
67 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
68 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
69 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
70 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
71 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
72 &a,
73 // far apart and intermediate values are not co-optable
74 // so a new chain must be used
75 JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK,
76 JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK,
77 JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK,
78 JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK,
79 JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK,
80 &a,
81 &a,
82 };
83
84 void verifyLongChains()
85 {
86 VERIFY(otherPtrs[0], &a);
87 VERIFY(otherPtrs[41], &a+1);
88 VERIFY(otherPtrs[122], &a);
89 VERIFY(otherPtrs[163], &a);
90 VERIFY(otherPtrs[164], &a);
91 }
92 #endif
93
94 int main()
95 {
96 printf("[BEGIN] bind-rebase\n");
97 verifyRebases();
98 verifyBinds();
99
100 #if !__LP64__
101 verifyLongChains();
102 #endif
103
104 printf("[PASS] bind-rebase\n");
105 return 0;
106 }
107