VirtualBox

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

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

Made it possible to find symbols for windows nt using a image-in-guest-memory loader fallback.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.0 KB
Line 
1/* $Id: ldrNative.cpp 46083 2013-05-14 23:39:28Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Native interface.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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 s_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 42
84};
85
86
87
88/**
89 * Loads a dynamic load library (/shared object) image file using native
90 * OS facilities.
91 *
92 * The filename will be appended the default DLL/SO extension of
93 * the platform if it have been omitted. This means that it's not
94 * possible to load DLLs/SOs with no extension using this interface,
95 * but that's not a bad tradeoff.
96 *
97 * If no path is specified in the filename, the OS will usually search it's library
98 * path to find the image file.
99 *
100 * @returns iprt status code.
101 * @param pszFilename Image filename.
102 * @param phLdrMod Where to store the handle to the loaded module.
103 */
104RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
105{
106 return RTLdrLoadEx(pszFilename, phLdrMod, RTLDRLOAD_FLAGS_LOCAL, NULL);
107}
108RT_EXPORT_SYMBOL(RTLdrLoad);
109
110
111RTDECL(int) RTLdrLoadEx(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
112{
113 LogFlow(("RTLdrLoadEx: pszFilename=%p:{%s} phLdrMod=%p fFlags=%#x pErrInfo=%p\n", pszFilename, pszFilename, phLdrMod, fFlags, pErrInfo));
114
115 /*
116 * Validate and massage the input.
117 */
118 RTErrInfoClear(pErrInfo);
119 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
120 AssertPtrReturn(phLdrMod, VERR_INVALID_POINTER);
121 AssertReturn(!(fFlags & ~RTLDRLOAD_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
122
123 /*
124 * Allocate and initialize module structure.
125 */
126 int rc = VERR_NO_MEMORY;
127 PRTLDRMODNATIVE pMod = (PRTLDRMODNATIVE)RTMemAlloc(sizeof(*pMod));
128 if (pMod)
129 {
130 pMod->Core.u32Magic = RTLDRMOD_MAGIC;
131 pMod->Core.eState = LDR_STATE_LOADED;
132 pMod->Core.pOps = &s_rtldrNativeOps;
133 pMod->Core.pReader = NULL;
134 pMod->hNative = ~(uintptr_t)0;
135
136 /*
137 * Attempt to open the module.
138 */
139 rc = rtldrNativeLoad(pszFilename, &pMod->hNative, fFlags, pErrInfo);
140 if (RT_SUCCESS(rc))
141 {
142 *phLdrMod = &pMod->Core;
143 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc, *phLdrMod));
144 return rc;
145 }
146
147 RTMemFree(pMod);
148 }
149 else
150 RTErrInfoSetF(pErrInfo, rc, "Failed to allocate %zu bytes for the module handle", sizeof(*pMod));
151 *phLdrMod = NIL_RTLDRMOD;
152 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
153 return rc;
154}
155RT_EXPORT_SYMBOL(RTLdrLoadEx);
156
157
158/**
159 * Loads a dynamic load library (/shared object) image file residing in the
160 * RTPathAppPrivateArch() directory.
161 *
162 * Suffix is not required.
163 *
164 * @returns iprt status code.
165 * @param pszFilename Image filename. No path.
166 * @param phLdrMod Where to store the handle to the loaded module.
167 */
168RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
169{
170 LogFlow(("RTLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
171
172 /*
173 * Validate input.
174 */
175 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
176 *phLdrMod = NIL_RTLDRMOD;
177 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
178 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
179
180 /*
181 * Check the filename.
182 */
183 size_t cchFilename = strlen(pszFilename);
184 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
185
186 const char *pszExt = "";
187 size_t cchExt = 0;
188 if (!RTPathHaveExt(pszFilename))
189 {
190 pszExt = RTLdrGetSuff();
191 cchExt = strlen(pszExt);
192 }
193
194 /*
195 * Construct the private arch path and check if the file exists.
196 */
197 char szPath[RTPATH_MAX];
198 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
199 AssertRCReturn(rc, rc);
200
201 char *psz = strchr(szPath, '\0');
202 *psz++ = RTPATH_SLASH;
203 memcpy(psz, pszFilename, cchFilename);
204 psz += cchFilename;
205 memcpy(psz, pszExt, cchExt + 1);
206
207 if (!RTPathExists(szPath))
208 {
209 LogRel(("RTLdrLoadAppPriv: \"%s\" not found\n", szPath));
210 return VERR_FILE_NOT_FOUND;
211 }
212
213 /*
214 * Pass it on to RTLdrLoad.
215 */
216 rc = RTLdrLoad(szPath, phLdrMod);
217
218 LogFlow(("RTLdrLoadAppPriv: returns %Rrc\n", rc));
219 return rc;
220}
221RT_EXPORT_SYMBOL(RTLdrLoadAppPriv);
222
223
224/**
225 * Gets the default file suffix for DLL/SO/DYLIB/whatever.
226 *
227 * @returns The stuff (readonly).
228 */
229RTDECL(const char *) RTLdrGetSuff(void)
230{
231#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
232 static const char s_szSuff[] = ".DLL";
233#elif defined(RT_OS_L4)
234 static const char s_szSuff[] = ".s.so";
235#elif defined(RT_OS_DARWIN)
236 static const char s_szSuff[] = ".dylib";
237#else
238 static const char s_szSuff[] = ".so";
239#endif
240
241 return s_szSuff;
242}
243RT_EXPORT_SYMBOL(RTLdrGetSuff);
244
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