VirtualBox

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

Last change on this file was 99758, checked in by vboxsync, 12 months ago

IPRT: Make doxygen 1.9.6 happy. Mostly removing duplicate docs (iprt is documented in the header files). bugref:10442

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 9.9 KB
Line 
1/* $Id: ldrNative.cpp 99758 2023-05-11 21:37:59Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Native interface.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_LDR
42#include <iprt/ldr.h>
43#include "internal/iprt.h"
44
45#include <iprt/alloc.h>
46#include <iprt/assert.h>
47#include <iprt/log.h>
48#include <iprt/param.h>
49#include <iprt/path.h>
50#include <iprt/string.h>
51#include <iprt/err.h>
52#include "internal/ldr.h"
53
54
55/** @copydoc RTLDROPS::pfnEnumSymbols */
56static DECLCALLBACK(int) rtldrNativeEnumSymbols(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits,
57 RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
58{
59 NOREF(pMod); NOREF(fFlags); NOREF(pvBits); NOREF(BaseAddress); NOREF(pfnCallback); NOREF(pvUser);
60 return VERR_NOT_SUPPORTED;
61}
62
63
64/** @copydoc RTLDROPS::pfnDone */
65static DECLCALLBACK(int) rtldrNativeDone(PRTLDRMODINTERNAL pMod)
66{
67 NOREF(pMod);
68 return VINF_SUCCESS;
69}
70
71
72/**
73 * Operations for a native module.
74 */
75static const RTLDROPS g_rtldrNativeOps =
76{
77 "native",
78 rtldrNativeClose,
79 rtldrNativeGetSymbol,
80 rtldrNativeDone,
81 rtldrNativeEnumSymbols,
82 /* ext: */
83 NULL,
84 NULL,
85 NULL,
86 NULL,
87 NULL /*pfnQueryForwarderInfo*/,
88 NULL,
89 NULL,
90 NULL,
91 NULL,
92 NULL,
93 NULL,
94 NULL,
95 NULL,
96 NULL,
97 NULL,
98 NULL /*pfnUnwindFrame*/,
99 42
100};
101
102
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 = &g_rtldrNativeOps;
133 pMod->Core.pReader = NULL;
134 pMod->Core.enmFormat = RTLDRFMT_NATIVE;
135 pMod->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE; /* approx */
136#ifdef RT_BIG_ENDIAN
137 pMod->Core.enmEndian = RTLDRENDIAN_BIG;
138#else
139 pMod->Core.enmEndian = RTLDRENDIAN_LITTLE;
140#endif
141#ifdef RT_ARCH_AMD64
142 pMod->Core.enmArch = RTLDRARCH_AMD64;
143#elif defined(RT_ARCH_X86)
144 pMod->Core.enmArch = RTLDRARCH_X86_32;
145#else
146 pMod->Core.enmArch = RTLDRARCH_HOST;
147#endif
148 pMod->hNative = ~(uintptr_t)0;
149 pMod->fFlags = fFlags;
150
151 /*
152 * Attempt to open the module.
153 */
154 rc = rtldrNativeLoad(pszFilename, &pMod->hNative, fFlags, pErrInfo);
155 if (RT_SUCCESS(rc))
156 {
157 if (fFlags & RTLDRLOAD_FLAGS_NO_UNLOAD)
158 RTMEM_MAY_LEAK(pMod);
159
160 *phLdrMod = &pMod->Core;
161 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc,*phLdrMod));
162 return rc;
163 }
164
165 RTMemFree(pMod);
166 }
167 else
168 RTErrInfoSetF(pErrInfo, rc, "Failed to allocate %zu bytes for the module handle", sizeof(*pMod));
169 *phLdrMod = NIL_RTLDRMOD;
170 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
171 return rc;
172}
173RT_EXPORT_SYMBOL(RTLdrLoadEx);
174
175
176RTDECL(int) RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod)
177{
178 return RTLdrLoadSystemEx(pszFilename, fNoUnload ? RTLDRLOAD_FLAGS_NO_UNLOAD : 0, phLdrMod);
179}
180
181
182RTDECL(int) RTLdrLoadSystemEx(const char *pszFilename, uint32_t fFlags, PRTLDRMOD phLdrMod)
183{
184 LogFlow(("RTLdrLoadSystemEx: pszFilename=%p:{%s} fFlags=%#RX32 phLdrMod=%p\n", pszFilename, pszFilename, fFlags, phLdrMod));
185
186 /*
187 * Validate input.
188 */
189 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
190 *phLdrMod = NIL_RTLDRMOD;
191 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
192 AssertMsgReturn(!RTPathHasPath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
193 AssertMsgReturn(!(fFlags & ~(RTLDRLOAD_FLAGS_VALID_MASK | RTLDRLOAD_FLAGS_SO_VER_BEGIN_MASK | RTLDRLOAD_FLAGS_SO_VER_END_MASK)),
194 ("fFlags=%#RX32\n", fFlags), VERR_INVALID_FLAGS);
195
196 /*
197 * Check the filename.
198 */
199 size_t cchFilename = strlen(pszFilename);
200 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
201
202 const char *pszSuffix = NULL;
203 if (!RTPathHasSuffix(pszFilename))
204 pszSuffix = RTLdrGetSuff();
205
206 /*
207 * Let the platform specific code do the rest.
208 */
209 int rc = rtldrNativeLoadSystem(pszFilename, pszSuffix, fFlags, phLdrMod);
210 LogFlow(("RTLdrLoadSystem: returns %Rrc\n", rc));
211 return rc;
212}
213
214
215RTDECL(void *) RTLdrGetSystemSymbol(const char *pszFilename, const char *pszSymbol)
216{
217 return RTLdrGetSystemSymbolEx(pszFilename, pszSymbol, RTLDRLOAD_FLAGS_NO_UNLOAD);
218}
219
220
221RTDECL(void *) RTLdrGetSystemSymbolEx(const char *pszFilename, const char *pszSymbol, uint32_t fFlags)
222{
223 void *pvRet = NULL;
224 RTLDRMOD hLdrMod;
225 int rc = RTLdrLoadSystemEx(pszFilename, fFlags | RTLDRLOAD_FLAGS_NO_UNLOAD, &hLdrMod);
226 if (RT_SUCCESS(rc))
227 {
228 rc = RTLdrGetSymbol(hLdrMod, pszSymbol, &pvRet);
229 if (RT_FAILURE(rc))
230 pvRet = NULL; /* paranoia */
231 RTLdrClose(hLdrMod);
232 }
233 return pvRet;
234}
235
236
237RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
238{
239 LogFlow(("RTLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
240
241 /*
242 * Validate input.
243 */
244 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
245 *phLdrMod = NIL_RTLDRMOD;
246 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
247 AssertMsgReturn(!RTPathHasPath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
248
249 /*
250 * Check the filename.
251 */
252 size_t cchFilename = strlen(pszFilename);
253 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
254
255 const char *pszSuffix = "";
256 size_t cchSuffix = 0;
257 if (!RTPathHasSuffix(pszFilename))
258 {
259 pszSuffix = RTLdrGetSuff();
260 cchSuffix = strlen(pszSuffix);
261 }
262
263 /*
264 * Construct the private arch path and check if the file exists.
265 */
266 char szPath[RTPATH_MAX];
267 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchSuffix - cchFilename);
268 AssertRCReturn(rc, rc);
269
270 char *psz = strchr(szPath, '\0');
271 *psz++ = RTPATH_SLASH;
272 memcpy(psz, pszFilename, cchFilename);
273 psz += cchFilename;
274 memcpy(psz, pszSuffix, cchSuffix + 1);
275
276 if (!RTPathExists(szPath))
277 {
278 LogRel(("RTLdrLoadAppPriv: \"%s\" not found\n", szPath));
279 return VERR_FILE_NOT_FOUND;
280 }
281
282 /*
283 * Pass it on to RTLdrLoad.
284 */
285 rc = RTLdrLoad(szPath, phLdrMod);
286
287 LogFlow(("RTLdrLoadAppPriv: returns %Rrc\n", rc));
288 return rc;
289}
290RT_EXPORT_SYMBOL(RTLdrLoadAppPriv);
291
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
320
321RTDECL(bool) RTLdrIsLoadable(const char *pszFilename)
322{
323 /*
324 * Try to load the library.
325 */
326 RTLDRMOD hLib;
327 int rc = RTLdrLoad(pszFilename, &hLib);
328 if (RT_SUCCESS(rc))
329 {
330 RTLdrClose(hLib);
331 return true;
332 }
333 return false;
334}
335RT_EXPORT_SYMBOL(RTLdrIsLoadable);
336
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use