1 /***********************************************************************
2 * Copyright (c) 2009, Secure Endpoints Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 **********************************************************************/
37 #define ERR_STR_LEN 256
39 static volatile LONG dlfcn_tls
= TLS_OUT_OF_INDEXES
;
41 static DWORD
get_tl_error_slot(void)
43 if (dlfcn_tls
== TLS_OUT_OF_INDEXES
) {
44 DWORD slot
= TlsAlloc();
47 if (slot
== TLS_OUT_OF_INDEXES
)
50 if ((old_slot
= InterlockedCompareExchange(&dlfcn_tls
, slot
,
51 TLS_OUT_OF_INDEXES
)) !=
65 static void set_error(const char * e
)
71 DWORD slot
= get_tl_error_slot();
73 if (slot
== TLS_OUT_OF_INDEXES
)
76 len
= strlen(e
) * sizeof(char) + sizeof(char);
77 s
= LocalAlloc(LMEM_FIXED
, len
);
81 old_s
= (char *) TlsGetValue(slot
);
82 TlsSetValue(slot
, (LPVOID
) s
);
88 static void set_error_from_last(void) {
89 DWORD slot
= get_tl_error_slot();
93 if (slot
== TLS_OUT_OF_INDEXES
)
96 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ALLOCATE_BUFFER
,
103 old_s
= (char *) TlsGetValue(slot
);
104 TlsSetValue(slot
, (LPVOID
) s
);
110 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
115 brv
= FreeLibrary((HMODULE
) vhm
);
117 set_error_from_last();
122 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
125 DWORD slot
= get_tl_error_slot();
127 if (slot
== TLS_OUT_OF_INDEXES
)
130 return (char *) TlsGetValue(slot
);
133 ROKEN_LIB_FUNCTION
void * ROKEN_LIB_CALL
134 dlopen(const char *fn
, int flags
)
139 /* We don't support dlopen(0, ...) on Windows.*/
141 set_error("Not implemented");
145 old_error_mode
= SetErrorMode(SEM_FAILCRITICALERRORS
);
147 hm
= LoadLibrary(fn
);
150 set_error_from_last();
153 SetErrorMode(old_error_mode
);
158 ROKEN_LIB_FUNCTION DLSYM_RET_TYPE ROKEN_LIB_CALL
159 dlsym(void * vhm
, const char * func_name
)
161 HMODULE hm
= (HMODULE
) vhm
;
163 return (DLSYM_RET_TYPE
)(ULONG_PTR
)GetProcAddress(hm
, func_name
);