VirtualBox

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

Last change on this file since 38581 was 38581, checked in by vboxsync, 13 years ago

IPRT: More debug info & ldr stuff.

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