1 | /* $Id: ldrNative-posix.cpp 34959 2010-12-10 15:17:31Z 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, 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 | void *pvMod = dlopen(pszFilename, RTLD_NOW | RTLD_LOCAL);
|
---|
72 | if (pvMod)
|
---|
73 | {
|
---|
74 | *phHandle = (uintptr_t)pvMod;
|
---|
75 | return VINF_SUCCESS;
|
---|
76 | }
|
---|
77 |
|
---|
78 | const char *pszDlError = dlerror();
|
---|
79 | if (pszError)
|
---|
80 | RTStrCopy(pszError, cbError, pszDlError);
|
---|
81 | LogRel(("rtldrNativeLoad: dlopen('%s', RTLD_NOW | RTLD_LOCAL) failed: %s\n", pszFilename, pszDlError));
|
---|
82 | return VERR_FILE_NOT_FOUND;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue)
|
---|
87 | {
|
---|
88 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
89 | #ifdef RT_OS_OS2
|
---|
90 | /* Prefix the symbol with an underscore (assuming __cdecl/gcc-default). */
|
---|
91 | size_t cch = strlen(pszSymbol);
|
---|
92 | char *psz = (char *)alloca(cch + 2);
|
---|
93 | psz[0] = '_';
|
---|
94 | memcpy(psz + 1, pszSymbol, cch + 1);
|
---|
95 | pszSymbol = psz;
|
---|
96 | #endif
|
---|
97 | *ppvValue = dlsym((void *)pModNative->hNative, pszSymbol);
|
---|
98 | if (*ppvValue)
|
---|
99 | return VINF_SUCCESS;
|
---|
100 | return VERR_SYMBOL_NOT_FOUND;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod)
|
---|
105 | {
|
---|
106 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
107 | if (!dlclose((void *)pModNative->hNative))
|
---|
108 | {
|
---|
109 | pModNative->hNative = (uintptr_t)0;
|
---|
110 | return VINF_SUCCESS;
|
---|
111 | }
|
---|
112 | Log(("rtldrNativeFree: dlclose(%p) failed: %s\n", pModNative->hNative, dlerror()));
|
---|
113 | return VERR_GENERAL_FAILURE;
|
---|
114 | }
|
---|
115 |
|
---|