Mercurial > pub > dyncall > dyncall
annotate test/dynload_plain/dynload_plain.c @ 254:9d70178c1ded
- better cpp checking whether dlinfo() is available, to also cover GLIBC based systems that don't identify as Linux (e.g. debian/k*BSD)
author | Tassilo Philipp |
---|---|
date | Mon, 15 May 2017 13:06:04 +0200 |
parents | ab23f9f2934a |
children | a94a9a83dae6 |
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> | |
242 | 30 #include <sys/stat.h> |
233 | 31 #if defined(DC_WINDOWS) |
32 # include <io.h> | |
33 # define F_OK 0 | |
34 #else | |
35 # include <unistd.h> | |
36 #endif | |
214 | 37 |
38 | |
39 int main(int argc, char* argv[]) | |
40 { | |
41 int r = 0, i; | |
42 void* p; | |
43 DLLib* pLib; | |
233 | 44 DLSyms* pSyms; |
214 | 45 const char* path = NULL; |
242 | 46 const char* clibs[] = { /* hacky/lazy list of some clib paths per platform */ |
214 | 47 "/lib/libc.so", |
254
9d70178c1ded
- better cpp checking whether dlinfo() is available, to also cover GLIBC based systems that don't identify as Linux (e.g. debian/k*BSD)
Tassilo Philipp
parents:
248
diff
changeset
|
48 "/lib/libc.so.0.1", |
214 | 49 "/lib/libc.so.6", |
50 "/lib/libc.so.7", | |
51 "/lib64/libc.so", | |
52 "/lib64/libc.so.6", | |
53 "/lib64/libc.so.7", | |
54 "/lib32/libc.so", | |
55 "/lib32/libc.so.6", | |
56 "/lib32/libc.so.7", | |
242 | 57 "/usr/lib/libc.so", |
58 "/usr/lib/libc.so.6", | |
59 "/usr/lib/libc.so.7", | |
245
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
60 "/usr/lib/libc.so.39.3", /* hack: for OpenBSD used in dyncall test env */ |
242 | 61 "/usr/lib/system/libsystem_c.dylib", |
214 | 62 "/usr/lib/libc.dylib", |
248 | 63 "/boot/system/lib/libroot.so", /* Haiku */ |
234 | 64 "\\ReactOS\\system32\\msvcrt.dll", |
65 "C:\\ReactOS\\system32\\msvcrt.dll", | |
233 | 66 "\\Windows\\system32\\msvcrt.dll", |
234 | 67 "C:\\Windows\\system32\\msvcrt.dll" |
214 | 68 }; |
69 | |
70 | |
71 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
|
72 if(access(clibs[i], F_OK) != -1) { |
214 | 73 path = clibs[i]; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
74 break; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
75 } |
214 | 76 } |
77 | |
78 if(path) { | |
79 printf("using clib to test at: %s\n", path); | |
80 ++r; | |
81 | |
242 | 82 /* dl*Library tests */ |
83 /* ---------------- */ | |
84 pLib = dlLoadLibrary(path); /* check if we can load a lib */ | |
214 | 85 if(pLib) { |
242 | 86 char queriedPath[200]; /* enough for our test paths */ |
87 int bs; | |
88 | |
214 | 89 printf("pLib handle: %p\n", pLib); |
90 ++r; | |
91 | |
242 | 92 p = dlFindSymbol(pLib, "printf"); /* check if we can lookup a symbol */ |
214 | 93 printf("printf at: %p\n", p); |
94 r += (p != NULL); | |
95 | |
242 | 96 bs = dlGetLibraryPath(pLib, queriedPath, 200); |
97 if(bs && bs <= 200) { | |
98 struct stat st0, st1; /* to check if same file */ | |
99 int b; | |
100 printf("path of lib looked up via handle: %s\n", queriedPath); | |
101 b = (stat(path, &st0) != -1) && (stat(queriedPath, &st1) != -1); | |
245
0ba6189a51dd
- dynload dlGetLibraryPath simplifications:
Tassilo Philipp
parents:
242
diff
changeset
|
102 printf("lib (inode:%d) and looked up lib (inode:%d) are same: %d\n", b?st0.st_ino:-1, b?st1.st_ino:-1, b && (st0.st_ino == st1.st_ino)); //@@@ on windows, inode numbers returned here are always 0 |
242 | 103 r += b && (st0.st_ino == st1.st_ino); /* compare if same lib using inode */ |
104 /*@@@ check of resolved path is absolute*/ | |
105 | |
106 /* check correct bufsize retval */ | |
107 b = (bs == strlen(queriedPath) + 1); | |
108 printf("looked up path's needed buffer size (%d) computed correctly: %d\n", bs, b); | |
109 r += b; | |
110 } | |
111 else | |
112 printf("failed to query lib path using lib's handle\n"); | |
113 | |
214 | 114 dlFreeLibrary(pLib); |
115 } | |
116 else | |
117 printf("unable to open library %s\n", path); | |
118 | |
119 | |
242 | 120 /* dlSyms* tests (intentionally after freeing lib above, as they work standalone) */ |
121 /* ------------- */ | |
122 pSyms = dlSymsInit(path); /* check if we can iterate over symbols - init */ | |
214 | 123 if(pSyms) { |
124 int n; | |
125 const char* name; | |
126 | |
127 printf("pSyms handle: %p\n", pSyms); | |
128 ++r; | |
129 | |
242 | 130 n = dlSymsCount(pSyms); /* check if there are some syms to iterate over */ |
214 | 131 printf("num of libc symbols: %d\n", n); |
132 r += (n > 0); | |
133 | |
134 for(i=0; i<n; ++i) { | |
135 name = dlSymsName(pSyms, i); | |
242 | 136 if(name && strcmp(name, "printf") == 0) { /* check if we find "printf" also in iterated symbols */ |
214 | 137 ++r; |
138 break; | |
139 } | |
140 } | |
141 printf("printf symbol found by iteration: %d\n", i<n); | |
142 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
143 name = (i<n) ? dlSymsName(pSyms, i) : NULL; |
242 | 144 r += (name && strcmp(name, "printf") == 0); /* check if we can lookup "printf" by index */ |
218 | 145 printf("printf symbol name by index: %s\n", name?name:""); |
214 | 146 |
242 | 147 pLib = dlLoadLibrary(path); /* check if we can resolve ptr -> name, */ |
148 if(pLib) { /* need to lookup by name again, first */ | |
214 | 149 p = dlFindSymbol(pLib, "printf"); |
150 name = dlSymsNameFromValue(pSyms, p); | |
218 | 151 printf("printf symbol name by its own address (%p): %s\n", p, name?name:""); |
152 r += (name && strcmp(name, "printf") == 0); | |
214 | 153 dlFreeLibrary(pLib); |
154 } | |
155 | |
156 dlSymsCleanup(pSyms); | |
157 } | |
158 else | |
159 printf("dlSymsInit failed\n"); | |
160 } | |
161 | |
242 | 162 /* Check final score of right ones to see if all worked */ |
163 r = (r == 10); | |
214 | 164 printf("result: dynload_plain: %d\n", r); |
165 return !r; | |
166 } | |
167 |