1 | /* $Id: ldrNative-posix.cpp 35152 2010-12-15 16:45:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader, POSIX native.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
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 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define LOG_GROUP RTLOGGROUP_LDR
|
---|
31 | #include <dlfcn.h>
|
---|
32 |
|
---|
33 | #include <iprt/ldr.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <iprt/alloca.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/err.h>
|
---|
39 | #include <iprt/log.h>
|
---|
40 | #include "internal/ldr.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | int rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, char *pszError, size_t cbError)
|
---|
44 | {
|
---|
45 | /*
|
---|
46 | * Do we need to add an extension?
|
---|
47 | */
|
---|
48 | if (!RTPathHaveExt(pszFilename))
|
---|
49 | {
|
---|
50 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
51 | static const char s_szSuff[] = ".DLL";
|
---|
52 | #elif defined(RT_OS_L4)
|
---|
53 | static const char s_szSuff[] = ".s.so";
|
---|
54 | #elif defined(RT_OS_DARWIN)
|
---|
55 | static const char s_szSuff[] = ".dylib";
|
---|
56 | #else
|
---|
57 | static const char s_szSuff[] = ".so";
|
---|
58 | #endif
|
---|
59 | size_t cch = strlen(pszFilename);
|
---|
60 | char *psz = (char *)alloca(cch + sizeof(s_szSuff));
|
---|
61 | if (!psz)
|
---|
62 | return VERR_NO_MEMORY;
|
---|
63 | memcpy(psz, pszFilename, cch);
|
---|
64 | memcpy(psz + cch, s_szSuff, sizeof(s_szSuff));
|
---|
65 | pszFilename = psz;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Attempt load.
|
---|
70 | */
|
---|
71 | int fFlagsNative = RTLD_NOW;
|
---|
72 | if (fFlags & RTLDRFLAGS_GLOBAL)
|
---|
73 | fFlagsNative |= RTLD_GLOBAL;
|
---|
74 | else
|
---|
75 | fFlagsNative |= RTLD_LOCAL;
|
---|
76 | void *pvMod = dlopen(pszFilename, fFlagsNative);
|
---|
77 | if (pvMod)
|
---|
78 | {
|
---|
79 | *phHandle = (uintptr_t)pvMod;
|
---|
80 | return VINF_SUCCESS;
|
---|
81 | }
|
---|
82 |
|
---|
83 | const char *pszDlError = dlerror();
|
---|
84 | if (pszError)
|
---|
85 | RTStrCopy(pszError, cbError, pszDlError);
|
---|
86 | LogRel(("rtldrNativeLoad: dlopen('%s', RTLD_NOW | RTLD_LOCAL) failed: %s\n", pszFilename, pszDlError));
|
---|
87 | return VERR_FILE_NOT_FOUND;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue)
|
---|
92 | {
|
---|
93 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
94 | #ifdef RT_OS_OS2
|
---|
95 | /* Prefix the symbol with an underscore (assuming __cdecl/gcc-default). */
|
---|
96 | size_t cch = strlen(pszSymbol);
|
---|
97 | char *psz = (char *)alloca(cch + 2);
|
---|
98 | psz[0] = '_';
|
---|
99 | memcpy(psz + 1, pszSymbol, cch + 1);
|
---|
100 | pszSymbol = psz;
|
---|
101 | #endif
|
---|
102 | *ppvValue = dlsym((void *)pModNative->hNative, pszSymbol);
|
---|
103 | if (*ppvValue)
|
---|
104 | return VINF_SUCCESS;
|
---|
105 | return VERR_SYMBOL_NOT_FOUND;
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod)
|
---|
110 | {
|
---|
111 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
112 | if (!dlclose((void *)pModNative->hNative))
|
---|
113 | {
|
---|
114 | pModNative->hNative = (uintptr_t)0;
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 | Log(("rtldrNativeFree: dlclose(%p) failed: %s\n", pModNative->hNative, dlerror()));
|
---|
118 | return VERR_GENERAL_FAILURE;
|
---|
119 | }
|
---|
120 |
|
---|