VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.9 KB
Line 
1/* $Id: ldrNative.cpp 98103 2023-01-17 14:15:46Z 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
104/**
105 * Loads a dynamic load library (/shared object) image file using native
106 * OS facilities.
107 *
108 * The filename will be appended the default DLL/SO extension of
109 * the platform if it have been omitted. This means that it's not
110 * possible to load DLLs/SOs with no extension using this interface,
111 * but that's not a bad tradeoff.
112 *
113 * If no path is specified in the filename, the OS will usually search it's library
114 * path to find the image file.
115 *
116 * @returns iprt status code.
117 * @param pszFilename Image filename.
118 * @param phLdrMod Where to store the handle to the loaded module.
119 */
120RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
121{
122 return RTLdrLoadEx(pszFilename, phLdrMod, RTLDRLOAD_FLAGS_LOCAL, NULL);
123}
124RT_EXPORT_SYMBOL(RTLdrLoad);
125
126
127RTDECL(int) RTLdrLoadEx(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
128{
129 LogFlow(("RTLdrLoadEx: pszFilename=%p:{%s} phLdrMod=%p fFlags=%#x pErrInfo=%p\n", pszFilename, pszFilename, phLdrMod, fFlags, pErrInfo));
130
131 /*
132 * Validate and massage the input.
133 */
134 RTErrInfoClear(pErrInfo);
135 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
136 AssertPtrReturn(phLdrMod, VERR_INVALID_POINTER);
137 AssertReturn(!(fFlags & ~RTLDRLOAD_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
138
139 /*
140 * Allocate and initialize module structure.
141 */
142 int rc = VERR_NO_MEMORY;
143 PRTLDRMODNATIVE pMod = (PRTLDRMODNATIVE)RTMemAlloc(sizeof(*pMod));
144 if (pMod)
145 {
146 pMod->Core.u32Magic = RTLDRMOD_MAGIC;
147 pMod->Core.eState = LDR_STATE_LOADED;
148 pMod->Core.pOps = &g_rtldrNativeOps;
149 pMod->Core.pReader = NULL;
150 pMod->Core.enmFormat = RTLDRFMT_NATIVE;
151 pMod->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE; /* approx */
152#ifdef RT_BIG_ENDIAN
153 pMod->Core.enmEndian = RTLDRENDIAN_BIG;
154#else
155 pMod->Core.enmEndian = RTLDRENDIAN_LITTLE;
156#endif
157#ifdef RT_ARCH_AMD64
158 pMod->Core.enmArch = RTLDRARCH_AMD64;
159#elif defined(RT_ARCH_X86)
160 pMod->Core.enmArch = RTLDRARCH_X86_32;
161#else
162 pMod->Core.enmArch = RTLDRARCH_HOST;
163#endif
164 pMod->hNative = ~(uintptr_t)0;
165 pMod->fFlags = fFlags;
166
167 /*
168 * Attempt to open the module.
169 */
170 rc = rtldrNativeLoad(pszFilename, &pMod->hNative, fFlags, pErrInfo);
171 if (RT_SUCCESS(rc))
172 {
173 if (fFlags & RTLDRLOAD_FLAGS_NO_UNLOAD)
174 RTMEM_MAY_LEAK(pMod);
175
176 *phLdrMod = &pMod->Core;
177 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc,*phLdrMod));
178 return rc;
179 }
180
181 RTMemFree(pMod);
182 }
183 else
184 RTErrInfoSetF(pErrInfo, rc, "Failed to allocate %zu bytes for the module handle", sizeof(*pMod));
185 *phLdrMod = NIL_RTLDRMOD;
186 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
187 return rc;
188}
189RT_EXPORT_SYMBOL(RTLdrLoadEx);
190
191
192RTDECL(int) RTLdrLoadSystem(const char *pszFilename, bool fNoUnload, PRTLDRMOD phLdrMod)
193{
194 return RTLdrLoadSystemEx(pszFilename, fNoUnload ? RTLDRLOAD_FLAGS_NO_UNLOAD : 0, phLdrMod);
195}
196
197
198RTDECL(int) RTLdrLoadSystemEx(const char *pszFilename, uint32_t fFlags, PRTLDRMOD phLdrMod)
199{
200 LogFlow(("RTLdrLoadSystemEx: pszFilename=%p:{%s} fFlags=%#RX32 phLdrMod=%p\n", pszFilename, pszFilename, fFlags, phLdrMod));
201
202 /*
203 * Validate input.
204 */
205 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
206 *phLdrMod = NIL_RTLDRMOD;
207 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
208 AssertMsgReturn(!RTPathHasPath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
209 AssertMsgReturn(!(fFlags & ~(RTLDRLOAD_FLAGS_VALID_MASK | RTLDRLOAD_FLAGS_SO_VER_BEGIN_MASK | RTLDRLOAD_FLAGS_SO_VER_END_MASK)),
210 ("fFlags=%#RX32\n", fFlags), VERR_INVALID_FLAGS);
211
212 /*
213 * Check the filename.
214 */
215 size_t cchFilename = strlen(pszFilename);
216 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
217
218 const char *pszSuffix = NULL;
219 if (!RTPathHasSuffix(pszFilename))
220 pszSuffix = RTLdrGetSuff();
221
222 /*
223 * Let the platform specific code do the rest.
224 */
225 int rc = rtldrNativeLoadSystem(pszFilename, pszSuffix, fFlags, phLdrMod);
226 LogFlow(("RTLdrLoadSystem: returns %Rrc\n", rc));
227 return rc;
228}
229
230
231RTDECL(void *) RTLdrGetSystemSymbol(const char *pszFilename, const char *pszSymbol)
232{
233 return RTLdrGetSystemSymbolEx(pszFilename, pszSymbol, RTLDRLOAD_FLAGS_NO_UNLOAD);
234}
235
236
237RTDECL(void *) RTLdrGetSystemSymbolEx(const char *pszFilename, const char *pszSymbol, uint32_t fFlags)
238{
239 void *pvRet = NULL;
240 RTLDRMOD hLdrMod;
241 int rc = RTLdrLoadSystemEx(pszFilename, fFlags | RTLDRLOAD_FLAGS_NO_UNLOAD, &hLdrMod);
242 if (RT_SUCCESS(rc))
243 {
244 rc = RTLdrGetSymbol(hLdrMod, pszSymbol, &pvRet);
245 if (RT_FAILURE(rc))
246 pvRet = NULL; /* paranoia */
247 RTLdrClose(hLdrMod);
248 }
249 return pvRet;
250}
251
252
253/**
254 * Loads a dynamic load library (/shared object) image file residing in the
255 * RTPathAppPrivateArch() directory.
256 *
257 * Suffix is not required.
258 *
259 * @returns iprt status code.
260 * @param pszFilename Image filename. No path.
261 * @param phLdrMod Where to store the handle to the loaded module.
262 */
263RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
264{
265 LogFlow(("RTLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
266
267 /*
268 * Validate input.
269 */
270 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
271 *phLdrMod = NIL_RTLDRMOD;
272 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
273 AssertMsgReturn(!RTPathHasPath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
274
275 /*
276 * Check the filename.
277 */
278 size_t cchFilename = strlen(pszFilename);
279 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
280
281 const char *pszSuffix = "";
282 size_t cchSuffix = 0;
283 if (!RTPathHasSuffix(pszFilename))
284 {
285 pszSuffix = RTLdrGetSuff();
286 cchSuffix = strlen(pszSuffix);
287 }
288
289 /*
290 * Construct the private arch path and check if the file exists.
291 */
292 char szPath[RTPATH_MAX];
293 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchSuffix - cchFilename);
294 AssertRCReturn(rc, rc);
295
296 char *psz = strchr(szPath, '\0');
297 *psz++ = RTPATH_SLASH;
298 memcpy(psz, pszFilename, cchFilename);
299 psz += cchFilename;
300 memcpy(psz, pszSuffix, cchSuffix + 1);
301
302 if (!RTPathExists(szPath))
303 {
304 LogRel(("RTLdrLoadAppPriv: \"%s\" not found\n", szPath));
305 return VERR_FILE_NOT_FOUND;
306 }
307
308 /*
309 * Pass it on to RTLdrLoad.
310 */
311 rc = RTLdrLoad(szPath, phLdrMod);
312
313 LogFlow(("RTLdrLoadAppPriv: returns %Rrc\n", rc));
314 return rc;
315}
316RT_EXPORT_SYMBOL(RTLdrLoadAppPriv);
317
318
319/**
320 * Gets the default file suffix for DLL/SO/DYLIB/whatever.
321 *
322 * @returns The stuff (readonly).
323 */
324RTDECL(const char *) RTLdrGetSuff(void)
325{
326#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
327 static const char s_szSuff[] = ".DLL";
328#elif defined(RT_OS_L4)
329 static const char s_szSuff[] = ".s.so";
330#elif defined(RT_OS_DARWIN)
331 static const char s_szSuff[] = ".dylib";
332#else
333 static const char s_szSuff[] = ".so";
334#endif
335
336 return s_szSuff;
337}
338RT_EXPORT_SYMBOL(RTLdrGetSuff);
339
340
341RTDECL(uintptr_t) RTLdrGetNativeHandle(RTLDRMOD hLdrMod)
342{
343 PRTLDRMODNATIVE pThis = (PRTLDRMODNATIVE)hLdrMod;
344 AssertPtrReturn(pThis, ~(uintptr_t)0);
345 AssertReturn(pThis->Core.u32Magic == RTLDRMOD_MAGIC, ~(uintptr_t)0);
346 AssertReturn(pThis->Core.pOps == &g_rtldrNativeOps, ~(uintptr_t)0);
347 return pThis->hNative;
348}
349RT_EXPORT_SYMBOL(RTLdrGetNativeHandle);
350
351
352RTDECL(bool) RTLdrIsLoadable(const char *pszFilename)
353{
354 /*
355 * Try to load the library.
356 */
357 RTLDRMOD hLib;
358 int rc = RTLdrLoad(pszFilename, &hLib);
359 if (RT_SUCCESS(rc))
360 {
361 RTLdrClose(hLib);
362 return true;
363 }
364 return false;
365}
366RT_EXPORT_SYMBOL(RTLdrIsLoadable);
367
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use