1 /* $NetBSD: dlfcn_w32.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
3 /***********************************************************************
4 * Copyright (c) 2009, Secure Endpoints Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 **********************************************************************/
39 #define ERR_STR_LEN 256
41 static volatile LONG dlfcn_tls
= TLS_OUT_OF_INDEXES
;
43 static DWORD
get_tl_error_slot(void)
45 if (dlfcn_tls
== TLS_OUT_OF_INDEXES
) {
46 DWORD slot
= TlsAlloc();
49 if (slot
== TLS_OUT_OF_INDEXES
)
52 if ((old_slot
= InterlockedCompareExchange(&dlfcn_tls
, slot
,
53 TLS_OUT_OF_INDEXES
)) !=
67 static void set_error(const char * e
)
73 DWORD slot
= get_tl_error_slot();
75 if (slot
== TLS_OUT_OF_INDEXES
)
78 len
= strlen(e
) * sizeof(char) + sizeof(char);
79 s
= LocalAlloc(LMEM_FIXED
, len
);
83 old_s
= (char *) TlsGetValue(slot
);
84 TlsSetValue(slot
, (LPVOID
) s
);
90 static void set_error_from_last(void) {
91 DWORD slot
= get_tl_error_slot();
95 if (slot
== TLS_OUT_OF_INDEXES
)
98 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
,
105 old_s
= (char *) TlsGetValue(slot
);
106 TlsSetValue(slot
, (LPVOID
) s
);
112 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
117 brv
= FreeLibrary((HMODULE
) vhm
);
119 set_error_from_last();
124 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
127 DWORD slot
= get_tl_error_slot();
129 if (slot
== TLS_OUT_OF_INDEXES
)
132 return (char *) TlsGetValue(slot
);
135 ROKEN_LIB_FUNCTION
void * ROKEN_LIB_CALL
136 dlopen(const char *fn
, int flags
)
141 /* We don't support dlopen(0, ...) on Windows.*/
143 set_error("Not implemented");
147 old_error_mode
= SetErrorMode(SEM_FAILCRITICALERRORS
);
149 hm
= LoadLibrary(fn
);
152 set_error_from_last();
155 SetErrorMode(old_error_mode
);
160 ROKEN_LIB_FUNCTION DLSYM_RET_TYPE ROKEN_LIB_CALL
161 dlsym(void * vhm
, const char * func_name
)
163 HMODULE hm
= (HMODULE
) vhm
;
165 return (DLSYM_RET_TYPE
)(ULONG_PTR
)GetProcAddress(hm
, func_name
);