1 | /* $Id: ldrNative-posix.cpp 12146 2008-09-05 16:27:28Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader, POSIX native.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /*******************************************************************************
|
---|
32 | * Header Files *
|
---|
33 | *******************************************************************************/
|
---|
34 | #define LOG_GROUP RTLOGGROUP_LDR
|
---|
35 | #include <dlfcn.h>
|
---|
36 |
|
---|
37 | #include <iprt/ldr.h>
|
---|
38 | #include <iprt/assert.h>
|
---|
39 | #include <iprt/path.h>
|
---|
40 | #include <iprt/alloca.h>
|
---|
41 | #include <iprt/string.h>
|
---|
42 | #include <iprt/err.h>
|
---|
43 | #include <iprt/log.h>
|
---|
44 | #include "internal/ldr.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | int rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle)
|
---|
48 | {
|
---|
49 | /*
|
---|
50 | * Do we need to add an extension?
|
---|
51 | */
|
---|
52 | if (!RTPathHaveExt(pszFilename))
|
---|
53 | {
|
---|
54 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
55 | static const char s_szSuff[] = ".DLL";
|
---|
56 | #elif defined(RT_OS_L4)
|
---|
57 | static const char s_szSuff[] = ".s.so";
|
---|
58 | #elif defined(RT_OS_DARWIN)
|
---|
59 | static const char s_szSuff[] = ".dylib";
|
---|
60 | #else
|
---|
61 | static const char s_szSuff[] = ".so";
|
---|
62 | #endif
|
---|
63 | size_t cch = strlen(pszFilename);
|
---|
64 | char *psz = (char *)alloca(cch + sizeof(s_szSuff));
|
---|
65 | if (!psz)
|
---|
66 | return VERR_NO_MEMORY;
|
---|
67 | memcpy(psz, pszFilename, cch);
|
---|
68 | memcpy(psz + cch, s_szSuff, sizeof(s_szSuff));
|
---|
69 | pszFilename = psz;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Attempt load.
|
---|
74 | */
|
---|
75 |
|
---|
76 | void *pvMod = dlopen(pszFilename, RTLD_NOW | RTLD_LOCAL);
|
---|
77 | if (pvMod)
|
---|
78 | {
|
---|
79 | *phHandle = (uintptr_t)pvMod;
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 | LogRel(("rtldrNativeLoad: dlopen('%s', RTLD_NOW | RTLD_LOCAL) failed: %s\n", pszFilename, dlerror()));
|
---|
83 | return VERR_FILE_NOT_FOUND;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue)
|
---|
88 | {
|
---|
89 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
90 | #ifdef RT_OS_OS2
|
---|
91 | /* Prefix the symbol with an underscore (assuming __cdecl/gcc-default). */
|
---|
92 | size_t cch = strlen(pszSymbol);
|
---|
93 | char *psz = (char *)alloca(cch + 2);
|
---|
94 | psz[0] = '_';
|
---|
95 | memcpy(psz + 1, pszSymbol, cch + 1);
|
---|
96 | pszSymbol = psz;
|
---|
97 | #endif
|
---|
98 | *ppvValue = dlsym((void *)pModNative->hNative, pszSymbol);
|
---|
99 | if (*ppvValue)
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | return VERR_SYMBOL_NOT_FOUND;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod)
|
---|
106 | {
|
---|
107 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
108 | if (!dlclose((void *)pModNative->hNative))
|
---|
109 | {
|
---|
110 | pModNative->hNative = (uintptr_t)0;
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 | Log(("rtldrNativeFree: dlclose(%p) failed: %s\n", pModNative->hNative, dlerror()));
|
---|
114 | return VERR_GENERAL_FAILURE;
|
---|
115 | }
|
---|
116 |
|
---|