aboutsummaryrefslogtreecommitdiff
path: root/test.c
diff options
context:
space:
mode:
Diffstat (limited to 'test.c')
-rw-r--r--test.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..832c000
--- /dev/null
+++ b/test.c
@@ -0,0 +1,157 @@
1/*
2 * dlfcn-win32
3 * Copyright (c) 2007 Ramiro Polla
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <stdio.h>
21#include "dlfcn.h"
22
23/* This is what this test does:
24 * - Open library with RTLD_GLOBAL
25 * - Get global object
26 * - Get symbol from library through library object <- works
27 * - Run function if it worked
28 * - Get symbol from library through global object <- works
29 * - Run function if it worked
30 * - Close library
31 * - Open library with RTLD_LOCAL
32 * - Get symbol from library through library object <- works
33 * - Run function if it worked
34 * - Get symbol from library through global object <- fails
35 * - Run function if it worked
36 * - Open library again (without closing it first) with RTLD_GLOBAL
37 * - Get symbol from library through global object <- works
38 * - Close library
39 * - Close global object
40 */
41
42int main()
43{
44 void *global;
45 void *library;
46 char *error;
47 int (*function)( void );
48 int ret;
49
50 library = dlopen( "testdll.dll", RTLD_GLOBAL );
51 if( !library )
52 {
53 error = dlerror( );
54 printf( "Could not open library globally: %s\n", error ? error : "" );
55 }
56 else
57 printf( "Opened library globally: %p\n", library );
58
59 global = dlopen( 0, RTLD_GLOBAL );
60 if( !global )
61 {
62 error = dlerror( );
63 printf( "Could not open global handle: %s\n", error ? error : "" );
64 }
65 else
66 printf( "Got global handle: %p\n", global );
67
68 function = dlsym( library, "function" );
69 if( !function )
70 {
71 error = dlerror( );
72 printf( "Could not get symbol from library handle: %s\n",
73 error ? error : "" );
74 }
75 else
76 printf( "Got symbol from library handle: %p\n", function );
77
78 if( function )
79 function( );
80
81 function = dlsym( global, "function" );
82 if( !function )
83 {
84 error = dlerror( );
85 printf( "Could not get symbol from global handle: %s\n",
86 error ? error : "" );
87 }
88 else
89 printf( "Got symbol from global handle: %p\n", function );
90
91 if( function )
92 function( );
93
94 ret = dlclose( library );
95 if( ret )
96 {
97 error = dlerror( );
98 printf( "Could not close library: %s\n", error ? error : "" );
99 }
100 else
101 printf( "Closed library.\n" );
102
103 library = dlopen( "testdll.dll", RTLD_LOCAL );
104 if( !library )
105 {
106 error = dlerror( );
107 printf( "Could not open library locally: %s\n", error ? error : "" );
108 }
109 else
110 printf( "Opened library locally: %p\n", library );
111
112 function = dlsym( library, "function" );
113 if( !function )
114 {
115 error = dlerror( );
116 printf( "Could not get symbol from library handle: %s\n",
117 error ? error : "" );
118 }
119 else
120 printf( "Got symbol from library handle: %p\n", function );
121
122 if( function )
123 function( );
124
125 function = dlsym( global, "function" );
126 if( !function )
127 {
128 error = dlerror( );
129 printf( "Could not get symbol from global handle: %s\n",
130 error ? error : "" );
131 }
132 else
133 printf( "Got symbol from global handle: %p\n", function );
134
135 if( function )
136 function( );
137
138 ret = dlclose( library );
139 if( ret )
140 {
141 error = dlerror( );
142 printf( "Could not close library: %s\n", error ? error : "" );
143 }
144 else
145 printf( "Closed library.\n" );
146
147 ret = dlclose( global );
148 if( ret )
149 {
150 error = dlerror( );
151 printf( "Could not close global handle: %s\n", error ? error : "" );
152 }
153 else
154 printf( "Closed global handle.\n" );
155
156 return 0;
157}