VirtualBox

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

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

header (C) fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/* $Id: ldrNative.cpp 44528 2013-02-04 14:27:54Z 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->hNative = ~(uintptr_t)0;
134
135 /*
136 * Attempt to open the module.
137 */
138 rc = rtldrNativeLoad(pszFilename, &pMod->hNative, fFlags, pErrInfo);
139 if (RT_SUCCESS(rc))
140 {
141 *phLdrMod = &pMod->Core;
142 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc, *phLdrMod));
143 return rc;
144 }
145
146 RTMemFree(pMod);
147 }
148 else
149 RTErrInfoSetF(pErrInfo, rc, "Failed to allocate %zu bytes for the module handle", sizeof(*pMod));
150 *phLdrMod = NIL_RTLDRMOD;
151 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
152 return rc;
153}
154RT_EXPORT_SYMBOL(RTLdrLoadEx);
155
156
157/**
158 * Loads a dynamic load library (/shared object) image file residing in the
159 * RTPathAppPrivateArch() directory.
160 *
161 * Suffix is not required.
162 *
163 * @returns iprt status code.
164 * @param pszFilename Image filename. No path.
165 * @param phLdrMod Where to store the handle to the loaded module.
166 */
167RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
168{
169 LogFlow(("RTLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
170
171 /*
172 * Validate input.
173 */
174 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
175 *phLdrMod = NIL_RTLDRMOD;
176 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
177 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
178
179 /*
180 * Check the filename.
181 */
182 size_t cchFilename = strlen(pszFilename);
183 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
184
185 const char *pszExt = "";
186 size_t cchExt = 0;
187 if (!RTPathHaveExt(pszFilename))
188 {
189 pszExt = RTLdrGetSuff();
190 cchExt = strlen(pszExt);
191 }
192
193 /*
194 * Construct the private arch path and check if the file exists.
195 */
196 char szPath[RTPATH_MAX];
197 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
198 AssertRCReturn(rc, rc);
199
200 char *psz = strchr(szPath, '\0');
201 *psz++ = RTPATH_SLASH;
202 memcpy(psz, pszFilename, cchFilename);
203 psz += cchFilename;
204 memcpy(psz, pszExt, cchExt + 1);
205
206 if (!RTPathExists(szPath))
207 {
208 LogRel(("RTLdrLoadAppPriv: \"%s\" not found\n", szPath));
209 return VERR_FILE_NOT_FOUND;
210 }
211
212 /*
213 * Pass it on to RTLdrLoad.
214 */
215 rc = RTLdrLoad(szPath, phLdrMod);
216
217 LogFlow(("RTLdrLoadAppPriv: returns %Rrc\n", rc));
218 return rc;
219}
220RT_EXPORT_SYMBOL(RTLdrLoadAppPriv);
221
222
223/**
224 * Gets the default file suffix for DLL/SO/DYLIB/whatever.
225 *
226 * @returns The stuff (readonly).
227 */
228RTDECL(const char *) RTLdrGetSuff(void)
229{
230#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
231 static const char s_szSuff[] = ".DLL";
232#elif defined(RT_OS_L4)
233 static const char s_szSuff[] = ".s.so";
234#elif defined(RT_OS_DARWIN)
235 static const char s_szSuff[] = ".dylib";
236#else
237 static const char s_szSuff[] = ".so";
238#endif
239
240 return s_szSuff;
241}
242RT_EXPORT_SYMBOL(RTLdrGetSuff);
243
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