Mercurial > pub > dyncall > dyncall
annotate test/dynload_plain/dynload_plain.c @ 234:808045066f12
- dynload_plain test ReactOS support
author | Tassilo Philipp |
---|---|
date | Sun, 16 Apr 2017 19:27:32 +0200 |
parents | 8216c86b4cbc |
children | 85b61e8facfe |
rev | line source |
---|---|
214 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: test | |
5 File: test/dynload_plain/dynload_plain.c | |
6 Description: | |
7 License: | |
8 | |
9 Copyright (c) 2017 Tassilo Philipp <tphilipp@potion-studios.com> | |
10 | |
11 Permission to use, copy, modify, and distribute this software for any | |
12 purpose with or without fee is hereby granted, provided that the above | |
13 copyright notice and this permission notice appear in all copies. | |
14 | |
15 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
16 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
17 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
18 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
22 | |
23 */ | |
24 | |
25 | |
26 #include "../../dynload/dynload.h" | |
27 #include "../common/platformInit.h" | |
28 | |
29 #include <string.h> | |
233 | 30 #if defined(DC_WINDOWS) |
31 # include <io.h> | |
32 # define F_OK 0 | |
33 #else | |
34 # include <unistd.h> | |
35 #endif | |
214 | 36 |
37 | |
38 int main(int argc, char* argv[]) | |
39 { | |
40 int r = 0, i; | |
41 void* p; | |
42 DLLib* pLib; | |
233 | 43 DLSyms* pSyms; |
214 | 44 const char* path = NULL; |
45 const char* clibs[] = { // hacky/lazy list of some clib paths per platform | |
46 "/lib/libc.so", | |
47 "/lib/libc.so.6", | |
48 "/lib/libc.so.7", | |
49 "/lib64/libc.so", | |
50 "/lib64/libc.so.6", | |
51 "/lib64/libc.so.7", | |
52 "/lib32/libc.so", | |
53 "/lib32/libc.so.6", | |
54 "/lib32/libc.so.7", | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
55 "/usr/lib/system/libsystem_c.dylib", |
214 | 56 "/usr/lib/libc.dylib", |
234 | 57 "\\ReactOS\\system32\\msvcrt.dll", |
58 "C:\\ReactOS\\system32\\msvcrt.dll", | |
233 | 59 "\\Windows\\system32\\msvcrt.dll", |
234 | 60 "C:\\Windows\\system32\\msvcrt.dll" |
214 | 61 }; |
62 | |
63 | |
64 for(i=0; i<(sizeof(clibs)/sizeof(const char*)); ++i) { | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
65 if(access(clibs[i], F_OK) != -1) { |
214 | 66 path = clibs[i]; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
67 break; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
68 } |
214 | 69 } |
70 | |
71 if(path) { | |
72 printf("using clib to test at: %s\n", path); | |
73 ++r; | |
74 | |
75 // dl*Library tests | |
76 // -------- | |
77 pLib = dlLoadLibrary(path); // check if we can load a lib | |
78 if(pLib) { | |
79 printf("pLib handle: %p\n", pLib); | |
80 ++r; | |
81 | |
82 p = dlFindSymbol(pLib, "printf"); // check if we can lookup a symbol | |
83 printf("printf at: %p\n", p); | |
84 r += (p != NULL); | |
85 | |
86 dlFreeLibrary(pLib); | |
87 } | |
88 else | |
89 printf("unable to open library %s\n", path); | |
90 | |
91 | |
92 // dlSyms* tests (intentionally after freeing lib above, as they work standalone) | |
93 // -------- | |
233 | 94 pSyms = dlSymsInit(path); // check if we can iterate over symbols - init |
214 | 95 if(pSyms) { |
96 int n; | |
97 const char* name; | |
98 | |
99 printf("pSyms handle: %p\n", pSyms); | |
100 ++r; | |
101 | |
102 n = dlSymsCount(pSyms); // check if there are some syms to iterate over | |
103 printf("num of libc symbols: %d\n", n); | |
104 r += (n > 0); | |
105 | |
106 for(i=0; i<n; ++i) { | |
107 name = dlSymsName(pSyms, i); | |
218 | 108 if(name && strcmp(name, "printf") == 0) { // check if we find "printf" also in iterated symbols |
214 | 109 ++r; |
110 break; | |
111 } | |
112 } | |
113 printf("printf symbol found by iteration: %d\n", i<n); | |
114 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
115 name = (i<n) ? dlSymsName(pSyms, i) : NULL; |
218 | 116 r += (name && strcmp(name, "printf") == 0); // check if we can lookup "printf" by index |
117 printf("printf symbol name by index: %s\n", name?name:""); | |
214 | 118 |
119 pLib = dlLoadLibrary(path); // check if we can resolve ptr -> name, | |
120 if(pLib) { // need to lookup by name again, first | |
121 p = dlFindSymbol(pLib, "printf"); | |
122 name = dlSymsNameFromValue(pSyms, p); | |
218 | 123 printf("printf symbol name by its own address (%p): %s\n", p, name?name:""); |
124 r += (name && strcmp(name, "printf") == 0); | |
214 | 125 dlFreeLibrary(pLib); |
126 } | |
127 | |
128 dlSymsCleanup(pSyms); | |
129 } | |
130 else | |
131 printf("dlSymsInit failed\n"); | |
132 } | |
133 | |
134 // All worked if we got a score of 6 right ones | |
135 r = (r == 8); | |
136 printf("result: dynload_plain: %d\n", r); | |
137 return !r; | |
138 } | |
139 |