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