1cdc920a0Smrg/**************************************************************************
2cdc920a0Smrg *
3cdc920a0Smrg * Copyright 2009 VMware, Inc.
4cdc920a0Smrg * Copyright 1999-2008  Brian Paul
5cdc920a0Smrg * All Rights Reserved.
6cdc920a0Smrg *
7cdc920a0Smrg * Permission is hereby granted, free of charge, to any person obtaining a
8cdc920a0Smrg * copy of this software and associated documentation files (the
9cdc920a0Smrg * "Software"), to deal in the Software without restriction, including
10cdc920a0Smrg * without limitation the rights to use, copy, modify, merge, publish,
11cdc920a0Smrg * distribute, sub license, and/or sell copies of the Software, and to
12cdc920a0Smrg * permit persons to whom the Software is furnished to do so, subject to
13cdc920a0Smrg * the following conditions:
14cdc920a0Smrg *
15cdc920a0Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16cdc920a0Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17cdc920a0Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18cdc920a0Smrg * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
19cdc920a0Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20cdc920a0Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21cdc920a0Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE.
22cdc920a0Smrg *
23cdc920a0Smrg * The above copyright notice and this permission notice (including the
24cdc920a0Smrg * next paragraph) shall be included in all copies or substantial portions
25cdc920a0Smrg * of the Software.
26cdc920a0Smrg *
27cdc920a0Smrg **************************************************************************/
28cdc920a0Smrg
29cdc920a0Smrg
30cdc920a0Smrg#include "pipe/p_config.h"
31cdc920a0Smrg#include "pipe/p_compiler.h"
32cdc920a0Smrg
33cdc920a0Smrg#if defined(PIPE_OS_UNIX)
34cdc920a0Smrg#include <dlfcn.h>
35cdc920a0Smrg#endif
36cdc920a0Smrg#if defined(PIPE_OS_WINDOWS)
37cdc920a0Smrg#include <windows.h>
38cdc920a0Smrg#endif
39cdc920a0Smrg
40cdc920a0Smrg#include "u_dl.h"
413464ebd5Sriastradh#include "u_pointer.h"
42cdc920a0Smrg
43cdc920a0Smrg
44cdc920a0Smrgstruct util_dl_library *
45cdc920a0Smrgutil_dl_open(const char *filename)
46cdc920a0Smrg{
47cdc920a0Smrg#if defined(PIPE_OS_UNIX)
4801e04c3fSmrg   return (struct util_dl_library *)dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
49cdc920a0Smrg#elif defined(PIPE_OS_WINDOWS)
50cdc920a0Smrg   return (struct util_dl_library *)LoadLibraryA(filename);
51cdc920a0Smrg#else
52cdc920a0Smrg   return NULL;
53cdc920a0Smrg#endif
54cdc920a0Smrg}
55cdc920a0Smrg
56cdc920a0Smrg
57cdc920a0Smrgutil_dl_proc
58cdc920a0Smrgutil_dl_get_proc_address(struct util_dl_library *library,
59cdc920a0Smrg                         const char *procname)
60cdc920a0Smrg{
61cdc920a0Smrg#if defined(PIPE_OS_UNIX)
623464ebd5Sriastradh   return (util_dl_proc) pointer_to_func(dlsym((void *)library, procname));
63cdc920a0Smrg#elif defined(PIPE_OS_WINDOWS)
64cdc920a0Smrg   return (util_dl_proc)GetProcAddress((HMODULE)library, procname);
65cdc920a0Smrg#else
66cdc920a0Smrg   return (util_dl_proc)NULL;
67cdc920a0Smrg#endif
68cdc920a0Smrg}
69cdc920a0Smrg
70cdc920a0Smrg
71cdc920a0Smrgvoid
72cdc920a0Smrgutil_dl_close(struct util_dl_library *library)
73cdc920a0Smrg{
74cdc920a0Smrg#if defined(PIPE_OS_UNIX)
75cdc920a0Smrg   dlclose((void *)library);
76cdc920a0Smrg#elif defined(PIPE_OS_WINDOWS)
77cdc920a0Smrg   FreeLibrary((HMODULE)library);
78cdc920a0Smrg#else
79cdc920a0Smrg   (void)library;
80cdc920a0Smrg#endif
81cdc920a0Smrg}
823464ebd5Sriastradh
833464ebd5Sriastradh
843464ebd5Sriastradhconst char *
853464ebd5Sriastradhutil_dl_error(void)
863464ebd5Sriastradh{
873464ebd5Sriastradh#if defined(PIPE_OS_UNIX)
883464ebd5Sriastradh   return dlerror();
893464ebd5Sriastradh#elif defined(PIPE_OS_WINDOWS)
903464ebd5Sriastradh   return "unknown error";
913464ebd5Sriastradh#else
923464ebd5Sriastradh   return "unknown error";
933464ebd5Sriastradh#endif
943464ebd5Sriastradh}
95