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",
|
|
49 "/usr/lib/libc.dylib",
|
|
50 "C:\\Windows\\system32\\msvcrt.dll"
|
|
51 };
|
|
52
|
|
53
|
|
54 for(i=0; i<(sizeof(clibs)/sizeof(const char*)); ++i) {
|
|
55 if(access(clibs[i], F_OK) != -1)
|
|
56 path = clibs[i];
|
|
57 }
|
|
58
|
|
59 if(path) {
|
|
60 printf("using clib to test at: %s\n", path);
|
|
61 ++r;
|
|
62
|
|
63 // dl*Library tests
|
|
64 // --------
|
|
65 pLib = dlLoadLibrary(path); // check if we can load a lib
|
|
66 if(pLib) {
|
|
67 printf("pLib handle: %p\n", pLib);
|
|
68 ++r;
|
|
69
|
|
70 p = dlFindSymbol(pLib, "printf"); // check if we can lookup a symbol
|
|
71 printf("printf at: %p\n", p);
|
|
72 r += (p != NULL);
|
|
73
|
|
74 dlFreeLibrary(pLib);
|
|
75 }
|
|
76 else
|
|
77 printf("unable to open library %s\n", path);
|
|
78
|
|
79
|
|
80 // dlSyms* tests (intentionally after freeing lib above, as they work standalone)
|
|
81 // --------
|
|
82 DLSyms* pSyms = dlSymsInit(path); // check if we can iterate over symbols - init
|
|
83 if(pSyms) {
|
|
84 int n;
|
|
85 const char* name;
|
|
86
|
|
87 printf("pSyms handle: %p\n", pSyms);
|
|
88 ++r;
|
|
89
|
|
90 n = dlSymsCount(pSyms); // check if there are some syms to iterate over
|
|
91 printf("num of libc symbols: %d\n", n);
|
|
92 r += (n > 0);
|
|
93
|
|
94 for(i=0; i<n; ++i) {
|
|
95 name = dlSymsName(pSyms, i);
|
218
|
96 if(name && strcmp(name, "printf") == 0) { // check if we find "printf" also in iterated symbols
|
214
|
97 ++r;
|
|
98 break;
|
|
99 }
|
|
100 }
|
|
101 printf("printf symbol found by iteration: %d\n", i<n);
|
|
102
|
|
103 name = dlSymsName(pSyms, i);
|
218
|
104 r += (name && strcmp(name, "printf") == 0); // check if we can lookup "printf" by index
|
|
105 printf("printf symbol name by index: %s\n", name?name:"");
|
214
|
106
|
|
107 pLib = dlLoadLibrary(path); // check if we can resolve ptr -> name,
|
|
108 if(pLib) { // need to lookup by name again, first
|
|
109 p = dlFindSymbol(pLib, "printf");
|
|
110 name = dlSymsNameFromValue(pSyms, p);
|
218
|
111 printf("printf symbol name by its own address (%p): %s\n", p, name?name:"");
|
|
112 r += (name && strcmp(name, "printf") == 0);
|
214
|
113 dlFreeLibrary(pLib);
|
|
114 }
|
|
115
|
|
116 dlSymsCleanup(pSyms);
|
|
117 }
|
|
118 else
|
|
119 printf("dlSymsInit failed\n");
|
|
120 }
|
|
121
|
|
122 // All worked if we got a score of 6 right ones
|
|
123 r = (r == 8);
|
|
124 printf("result: dynload_plain: %d\n", r);
|
|
125 return !r;
|
|
126 }
|
|
127
|