1 | /* $Id: ldrNative-posix.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader, POSIX native.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_LDR
|
---|
32 | #include <dlfcn.h>
|
---|
33 |
|
---|
34 | #include <iprt/ldr.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/alloca.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/log.h>
|
---|
41 | #include "internal/ldr.h"
|
---|
42 |
|
---|
43 |
|
---|
44 | int rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo)
|
---|
45 | {
|
---|
46 | /*
|
---|
47 | * Do we need to add an extension?
|
---|
48 | */
|
---|
49 | if (!RTPathHasSuffix(pszFilename))
|
---|
50 | {
|
---|
51 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
52 | static const char s_szSuff[] = ".DLL";
|
---|
53 | #elif defined(RT_OS_L4)
|
---|
54 | static const char s_szSuff[] = ".s.so";
|
---|
55 | #elif defined(RT_OS_DARWIN)
|
---|
56 | static const char s_szSuff[] = ".dylib";
|
---|
57 | #else
|
---|
58 | static const char s_szSuff[] = ".so";
|
---|
59 | #endif
|
---|
60 | size_t cch = strlen(pszFilename);
|
---|
61 | char *psz = (char *)alloca(cch + sizeof(s_szSuff));
|
---|
62 | if (!psz)
|
---|
63 | return RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "alloca failed");
|
---|
64 | memcpy(psz, pszFilename, cch);
|
---|
65 | memcpy(psz + cch, s_szSuff, sizeof(s_szSuff));
|
---|
66 | pszFilename = psz;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /*
|
---|
70 | * Attempt load.
|
---|
71 | */
|
---|
72 | int fFlagsNative = RTLD_NOW;
|
---|
73 | if (fFlags & RTLDRLOAD_FLAGS_GLOBAL)
|
---|
74 | fFlagsNative |= RTLD_GLOBAL;
|
---|
75 | else
|
---|
76 | fFlagsNative |= RTLD_LOCAL;
|
---|
77 | void *pvMod = dlopen(pszFilename, fFlagsNative);
|
---|
78 | if (pvMod)
|
---|
79 | {
|
---|
80 | *phHandle = (uintptr_t)pvMod;
|
---|
81 | return VINF_SUCCESS;
|
---|
82 | }
|
---|
83 |
|
---|
84 | const char *pszDlError = dlerror();
|
---|
85 | RTErrInfoSet(pErrInfo, VERR_FILE_NOT_FOUND, 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 ( (pModNative->fFlags & RTLDRLOAD_FLAGS_NO_UNLOAD)
|
---|
113 | || !dlclose((void *)pModNative->hNative))
|
---|
114 | {
|
---|
115 | pModNative->hNative = (uintptr_t)0;
|
---|
116 | return VINF_SUCCESS;
|
---|
117 | }
|
---|
118 | Log(("rtldrNativeFree: dlclose(%p) failed: %s\n", pModNative->hNative, dlerror()));
|
---|
119 | return VERR_GENERAL_FAILURE;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | int rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod)
|
---|
124 | {
|
---|
125 | /** @todo implement this in some sensible fashion. */
|
---|
126 | return VERR_NOT_SUPPORTED;
|
---|
127 | }
|
---|
128 |
|
---|