VirtualBox

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

Last change on this file since 37608 was 35191, checked in by vboxsync, 14 years ago

Some cleanup. Pass RTLDRLOAD_FLAGS_LOCAL instead of 0, since it's defined.

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