Mercurial > pub > dyncall > dyncall
annotate test/dynload_plain/dynload_plain.c @ 223:7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
- dynload test code improvements
- dynload(3) clarification
author | Tassilo Philipp |
---|---|
date | Sat, 15 Apr 2017 15:21:37 +0200 |
parents | cb56f077fd37 |
children | 8216c86b4cbc |
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> | |
30 #include <unistd.h> | |
31 | |
32 | |
33 int main(int argc, char* argv[]) | |
34 { | |
35 int r = 0, i; | |
36 void* p; | |
37 DLLib* pLib; | |
38 const char* path = NULL; | |
39 const char* clibs[] = { // hacky/lazy list of some clib paths per platform | |
40 "/lib/libc.so", | |
41 "/lib/libc.so.6", | |
42 "/lib/libc.so.7", | |
43 "/lib64/libc.so", | |
44 "/lib64/libc.so.6", | |
45 "/lib64/libc.so.7", | |
46 "/lib32/libc.so", | |
47 "/lib32/libc.so.6", | |
48 "/lib32/libc.so.7", | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
49 "/usr/lib/system/libsystem_c.dylib", |
214 | 50 "/usr/lib/libc.dylib", |
51 "C:\\Windows\\system32\\msvcrt.dll" | |
52 }; | |
53 | |
54 | |
55 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
|
56 if(access(clibs[i], F_OK) != -1) { |
214 | 57 path = clibs[i]; |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
58 break; |
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
59 } |
214 | 60 } |
61 | |
62 if(path) { | |
63 printf("using clib to test at: %s\n", path); | |
64 ++r; | |
65 | |
66 // dl*Library tests | |
67 // -------- | |
68 pLib = dlLoadLibrary(path); // check if we can load a lib | |
69 if(pLib) { | |
70 printf("pLib handle: %p\n", pLib); | |
71 ++r; | |
72 | |
73 p = dlFindSymbol(pLib, "printf"); // check if we can lookup a symbol | |
74 printf("printf at: %p\n", p); | |
75 r += (p != NULL); | |
76 | |
77 dlFreeLibrary(pLib); | |
78 } | |
79 else | |
80 printf("unable to open library %s\n", path); | |
81 | |
82 | |
83 // dlSyms* tests (intentionally after freeing lib above, as they work standalone) | |
84 // -------- | |
85 DLSyms* pSyms = dlSymsInit(path); // check if we can iterate over symbols - init | |
86 if(pSyms) { | |
87 int n; | |
88 const char* name; | |
89 | |
90 printf("pSyms handle: %p\n", pSyms); | |
91 ++r; | |
92 | |
93 n = dlSymsCount(pSyms); // check if there are some syms to iterate over | |
94 printf("num of libc symbols: %d\n", n); | |
95 r += (n > 0); | |
96 | |
97 for(i=0; i<n; ++i) { | |
98 name = dlSymsName(pSyms, i); | |
218 | 99 if(name && strcmp(name, "printf") == 0) { // check if we find "printf" also in iterated symbols |
214 | 100 ++r; |
101 break; | |
102 } | |
103 } | |
104 printf("printf symbol found by iteration: %d\n", i<n); | |
105 | |
223
7076f551faf5
- dynload mach-o handling fixes for 64bit platforms
Tassilo Philipp
parents:
218
diff
changeset
|
106 name = (i<n) ? dlSymsName(pSyms, i) : NULL; |
218 | 107 r += (name && strcmp(name, "printf") == 0); // check if we can lookup "printf" by index |
108 printf("printf symbol name by index: %s\n", name?name:""); | |
214 | 109 |
110 pLib = dlLoadLibrary(path); // check if we can resolve ptr -> name, | |
111 if(pLib) { // need to lookup by name again, first | |
112 p = dlFindSymbol(pLib, "printf"); | |
113 name = dlSymsNameFromValue(pSyms, p); | |
218 | 114 printf("printf symbol name by its own address (%p): %s\n", p, name?name:""); |
115 r += (name && strcmp(name, "printf") == 0); | |
214 | 116 dlFreeLibrary(pLib); |
117 } | |
118 | |
119 dlSymsCleanup(pSyms); | |
120 } | |
121 else | |
122 printf("dlSymsInit failed\n"); | |
123 } | |
124 | |
125 // All worked if we got a score of 6 right ones | |
126 r = (r == 8); | |
127 printf("result: dynload_plain: %d\n", r); | |
128 return !r; | |
129 } | |
130 |