VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/ldr/ldrNative.cpp@ 47646

Last change on this file since 47646 was 46593, checked in by vboxsync, 12 years ago

updates

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.3 KB
Line 
1/* $Id: ldrNative.cpp 46593 2013-06-17 14:32:51Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Native interface.
4 */
5
6/*
7 * Copyright (C) 2006-2013 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 <iprt/ldr.h>
33#include "internal/iprt.h"
34
35#include <iprt/alloc.h>
36#include <iprt/assert.h>
37#include <iprt/log.h>
38#include <iprt/param.h>
39#include <iprt/path.h>
40#include <iprt/string.h>
41#include <iprt/err.h>
42#include "internal/ldr.h"
43
44
45/** @copydoc RTLDROPS::pfnEnumSymbols */
46static DECLCALLBACK(int) rtldrNativeEnumSymbols(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits,
47 RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
48{
49 NOREF(pMod); NOREF(fFlags); NOREF(pvBits); NOREF(BaseAddress); NOREF(pfnCallback); NOREF(pvUser);
50 return VERR_NOT_SUPPORTED;
51}
52
53
54/** @copydoc RTLDROPS::pfnDone */
55static DECLCALLBACK(int) rtldrNativeDone(PRTLDRMODINTERNAL pMod)
56{
57 NOREF(pMod);
58 return VINF_SUCCESS;
59}
60
61
62/**
63 * Operations for a native module.
64 */
65static const RTLDROPS g_rtldrNativeOps =
66{
67 "native",
68 rtldrNativeClose,
69 rtldrNativeGetSymbol,
70 rtldrNativeDone,
71 rtldrNativeEnumSymbols,
72 /* ext: */
73 NULL,
74 NULL,
75 NULL,
76 NULL,
77 NULL,
78 NULL,
79 NULL,
80 NULL,
81 NULL,
82 NULL,
83 NULL,
84 42
85};
86
87
88
89/**
90 * Loads a dynamic load library (/shared object) image file using native
91 * OS facilities.
92 *
93 * The filename will be appended the default DLL/SO extension of
94 * the platform if it have been omitted. This means that it's not
95 * possible to load DLLs/SOs with no extension using this interface,
96 * but that's not a bad tradeoff.
97 *
98 * If no path is specified in the filename, the OS will usually search it's library
99 * path to find the image file.
100 *
101 * @returns iprt status code.
102 * @param pszFilename Image filename.
103 * @param phLdrMod Where to store the handle to the loaded module.
104 */
105RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
106{
107 return RTLdrLoadEx(pszFilename, phLdrMod, RTLDRLOAD_FLAGS_LOCAL, NULL);
108}
109RT_EXPORT_SYMBOL(RTLdrLoad);
110
111
112RTDECL(int) RTLdrLoadEx(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
113{
114 LogFlow(("RTLdrLoadEx: pszFilename=%p:{%s} phLdrMod=%p fFlags=%#x pErrInfo=%p\n", pszFilename, pszFilename, phLdrMod, fFlags, pErrInfo));
115
116 /*
117 * Validate and massage the input.
118 */
119 RTErrInfoClear(pErrInfo);
120 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
121 AssertPtrReturn(phLdrMod, VERR_INVALID_POINTER);
122 AssertReturn(!(fFlags & ~RTLDRLOAD_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
123
124 /*
125 * Allocate and initialize module structure.
126 */
127 int rc = VERR_NO_MEMORY;
128 PRTLDRMODNATIVE pMod = (PRTLDRMODNATIVE)RTMemAlloc(sizeof(*pMod));
129 if (pMod)
130 {
131 pMod->Core.u32Magic = RTLDRMOD_MAGIC;
132 pMod->Core.eState = LDR_STATE_LOADED;
133 pMod->Core.pOps = &g_rtldrNativeOps;
134 pMod->Core.pReader = NULL;
135 pMod->Core.enmFormat = RTLDRFMT_NATIVE;
136 pMod->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE; /* approx */
137#ifdef RT_BIG_ENDIAN
138 pMod->Core.enmEndian = RTLDRENDIAN_BIG;
139#else
140 pMod->Core.enmEndian = RTLDRENDIAN_LITTLE;
141#endif
142#ifdef RT_ARCH_AMD64
143 pMod->Core.enmArch = RTLDRARCH_AMD64;
144#elif defined(RT_ARCH_X86)
145 pMod->Core.enmArch = RTLDRARCH_X86_32;
146#else
147 pMod->Core.enmArch = RTLDRARCH_HOST;
148#endif
149 pMod->hNative = ~(uintptr_t)0;
150 pMod->fFlags = fFlags;
151
152 /*
153 * Attempt to open the module.
154 */
155 rc = rtldrNativeLoad(pszFilename, &pMod->hNative, fFlags, pErrInfo);
156 if (RT_SUCCESS(rc))
157 {
158 *phLdrMod = &pMod->Core;
159 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc, *phLdrMod));
160 return rc;
161 }
162
163 RTMemFree(pMod);
164 }
165 else
166 RTErrInfoSetF(pErrInfo, rc, "Failed to allocate %zu bytes for the module handle", sizeof(*pMod));
167 *phLdrMod = NIL_RTLDRMOD;
168 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
169 return rc;
170}
171RT_EXPORT_SYMBOL(RTLdrLoadEx);
172
173
174RTDECL(int) RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod)
175{
176 LogFlow(("RTLdrLoadSystem: pszFilename=%p:{%s} fNoUnload=%RTbool phLdrMod=%p\n",
177 pszFilename, pszFilename, fNoUnload, phLdrMod));
178
179 /*
180 * Validate input.
181 */
182 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
183 *phLdrMod = NIL_RTLDRMOD;
184 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
185 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
186
187 /*
188 * Check the filename.
189 */
190 size_t cchFilename = strlen(pszFilename);
191 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
192
193 const char *pszExt = "";
194 if (!RTPathHaveExt(pszFilename))
195 pszExt = RTLdrGetSuff();
196
197 /*
198 * Let the platform specific code do the rest.
199 */
200 int rc = rtldrNativeLoadSystem(pszFilename, pszExt, fNoUnload ? RTLDRLOAD_FLAGS_NO_UNLOAD : 0, phLdrMod);
201 LogFlow(("RTLdrLoadSystem: returns %Rrc\n", rc));
202 return rc;
203}
204
205
206RTDECL(void *) RTLdrGetSystemSymbol(const char *pszFilename, const char *pszSymbol)
207{
208 void *pvRet = NULL;
209 RTLDRMOD hLdrMod;
210 int rc = RTLdrLoadSystem(pszFilename, true /*fNoUnload*/, &hLdrMod);
211 if (RT_SUCCESS(rc))
212 {
213 rc = RTLdrGetSymbol(hLdrMod, pszSymbol, &pvRet);
214 if (RT_FAILURE(rc))
215 pvRet = NULL; /* paranoia */
216 RTLdrClose(hLdrMod);
217 }
218 return pvRet;
219}
220
221
222/**
223 * Loads a dynamic load library (/shared object) image file residing in the
224 * RTPathAppPrivateArch() directory.
225 *
226 * Suffix is not required.
227 *
228 * @returns iprt status code.
229 * @param pszFilename Image filename. No path.
230 * @param phLdrMod Where to store the handle to the loaded module.
231 */
232RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
233{
234 LogFlow(("RTLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
235
236 /*
237 * Validate input.
238 */
239 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
240 *phLdrMod = NIL_RTLDRMOD;
241 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
242 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
243
244 /*
245 * Check the filename.
246 */
247 size_t cchFilename = strlen(pszFilename);
248 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
249
250 const char *pszExt = "";
251 size_t cchExt = 0;
252 if (!RTPathHaveExt(pszFilename))
253 {
254 pszExt = RTLdrGetSuff();
255 cchExt = strlen(pszExt);
256 }
257
258 /*
259 * Construct the private arch path and check if the file exists.
260 */
261 char szPath[RTPATH_MAX];
262 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
263 AssertRCReturn(rc, rc);
264
265 char *psz = strchr(szPath, '\0');
266 *psz++ = RTPATH_SLASH;
267 memcpy(psz, pszFilename, cchFilename);
268 psz += cchFilename;
269 memcpy(psz, pszExt, cchExt + 1);
270
271 if (!RTPathExists(szPath))
272 {
273 LogRel(("RTLdrLoadAppPriv: \"%s\" not found\n", szPath));
274 return VERR_FILE_NOT_FOUND;
275 }
276
277 /*
278 * Pass it on to RTLdrLoad.
279 */
280 rc = RTLdrLoad(szPath, phLdrMod);
281
282 LogFlow(("RTLdrLoadAppPriv: returns %Rrc\n", rc));
283 return rc;
284}
285RT_EXPORT_SYMBOL(RTLdrLoadAppPriv);
286
287
288/**
289 * Gets the default file suffix for DLL/SO/DYLIB/whatever.
290 *
291 * @returns The stuff (readonly).
292 */
293RTDECL(const char *) RTLdrGetSuff(void)
294{
295#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
296 static const char s_szSuff[] = ".DLL";
297#elif defined(RT_OS_L4)
298 static const char s_szSuff[] = ".s.so";
299#elif defined(RT_OS_DARWIN)
300 static const char s_szSuff[] = ".dylib";
301#else
302 static const char s_szSuff[] = ".so";
303#endif
304
305 return s_szSuff;
306}
307RT_EXPORT_SYMBOL(RTLdrGetSuff);
308
309
310RTDECL(uintptr_t) RTLdrGetNativeHandle(RTLDRMOD hLdrMod)
311{
312 PRTLDRMODNATIVE pThis = (PRTLDRMODNATIVE)hLdrMod;
313 AssertPtrReturn(pThis, ~(uintptr_t)0);
314 AssertReturn(pThis->Core.u32Magic == RTLDRMOD_MAGIC, ~(uintptr_t)0);
315 AssertReturn(pThis->Core.pOps == &g_rtldrNativeOps, ~(uintptr_t)0);
316 return pThis->hNative;
317}
318RT_EXPORT_SYMBOL(RTLdrGetNativeHandle);
319
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette