dyld-750.5.tar.gz
[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 #include "test_support.h"
13
14 extern char tzname[]; // a char array in libSystem.dylib
15
16
17 #define VERIFY(a,b) assert(a==b)
18
19
20 static uint8_t a;
21 uint8_t* const rebasedPtrs[] = { NULL, NULL, &a, &a+1, &a+16, &a+1023, NULL, &a-1 };
22
23 void verifyRebases()
24 {
25 VERIFY(rebasedPtrs[0], NULL);
26 VERIFY(rebasedPtrs[1], NULL);
27 VERIFY(rebasedPtrs[2], &a);
28 VERIFY(rebasedPtrs[3], &a+1);
29 VERIFY(rebasedPtrs[4], &a+16);
30 VERIFY(rebasedPtrs[5], &a+1023);
31 VERIFY(rebasedPtrs[6], NULL);
32 VERIFY(rebasedPtrs[7], &a-1);
33 }
34
35
36 static char* const bindPtrs[] = { NULL, NULL, tzname, tzname+1, &tzname[16], &tzname[1023], NULL, &tzname[-1], (char*)&malloc, (char*)&free };
37
38 void verifyBinds()
39 {
40 VERIFY(bindPtrs[0], NULL);
41 VERIFY(bindPtrs[1], NULL);
42 VERIFY(bindPtrs[2], tzname);
43 VERIFY(bindPtrs[3], tzname+1);
44 VERIFY(bindPtrs[4], tzname+16);
45 VERIFY(bindPtrs[5], tzname+1023);
46 VERIFY(bindPtrs[6], NULL);
47 VERIFY(bindPtrs[7], tzname-1);
48 VERIFY(bindPtrs[8], (char*)&malloc);
49 VERIFY(bindPtrs[9], (char*)&free);
50 }
51
52 #if !__LP64__
53 #define JUNK ((uint8_t*)0x12345678)
54 uint8_t* const otherPtrs[] = {
55 &a,
56 // far enough apart to require co-opting a 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 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
61 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
62 &a+1,
63 // far enough apart to require co-opting two NULLs
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 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
73 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
74 &a,
75 // far apart and intermediate values are not co-optable
76 // so a new chain must be used
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 JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK,
81 JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK, JUNK,
82 &a,
83 &a,
84 };
85
86 void verifyLongChains()
87 {
88 VERIFY(otherPtrs[0], &a);
89 VERIFY(otherPtrs[41], &a+1);
90 VERIFY(otherPtrs[122], &a);
91 VERIFY(otherPtrs[163], &a);
92 VERIFY(otherPtrs[164], &a);
93 }
94 #endif
95
96 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
97 verifyRebases();
98 verifyBinds();
99
100 #if !__LP64__
101 verifyLongChains();
102 #endif
103
104 PASS("Success");
105 }
106