VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/ldrNative-posix.cpp

Last change on this file was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.6 KB
Line 
1/* $Id: ldrNative-posix.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, POSIX native.
4 */
5
6/*
7 * Copyright (C) 2006-2024 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 <dlfcn.h>
43
44#include <iprt/ldr.h>
45#include <iprt/assert.h>
46#include <iprt/path.h>
47#include <iprt/alloca.h>
48#include <iprt/string.h>
49#include <iprt/err.h>
50#include <iprt/log.h>
51#include "internal/ldr.h"
52
53
54/*********************************************************************************************************************************
55* Global Variables *
56*********************************************************************************************************************************/
57#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
58static const char g_szSuff[] = ".DLL";
59#elif defined(RT_OS_L4)
60static const char g_szSuff[] = ".s.so";
61#elif defined(RT_OS_DARWIN)
62static const char g_szSuff[] = ".dylib";
63#else
64static const char g_szSuff[] = ".so";
65#endif
66
67
68DECLHIDDEN(int) rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo)
69{
70 /*
71 * Do we need to add an extension?
72 */
73 if (!RTPathHasSuffix(pszFilename) && !(fFlags & RTLDRLOAD_FLAGS_NO_SUFFIX))
74 {
75 size_t cch = strlen(pszFilename);
76 char *psz = (char *)alloca(cch + sizeof(g_szSuff));
77 if (!psz)
78 return RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "alloca failed");
79 memcpy(psz, pszFilename, cch);
80 memcpy(psz + cch, g_szSuff, sizeof(g_szSuff));
81 pszFilename = psz;
82 }
83
84 /*
85 * Attempt load.
86 */
87 int fFlagsNative = RTLD_NOW;
88 if (fFlags & RTLDRLOAD_FLAGS_GLOBAL)
89 fFlagsNative |= RTLD_GLOBAL;
90 else
91 fFlagsNative |= RTLD_LOCAL;
92 void *pvMod = dlopen(pszFilename, fFlagsNative);
93 if (pvMod)
94 {
95 *phHandle = (uintptr_t)pvMod;
96 return VINF_SUCCESS;
97 }
98
99 const char *pszDlError = dlerror();
100 RTErrInfoSet(pErrInfo, VERR_FILE_NOT_FOUND, pszDlError);
101 LogRel(("rtldrNativeLoad: dlopen('%s', RTLD_NOW | RTLD_LOCAL) failed: %s\n", pszFilename, pszDlError));
102 return VERR_FILE_NOT_FOUND;
103}
104
105
106DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue)
107{
108 PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
109#ifdef RT_OS_OS2
110 /* Prefix the symbol with an underscore (assuming __cdecl/gcc-default). */
111 size_t cch = strlen(pszSymbol);
112 char *psz = (char *)alloca(cch + 2);
113 psz[0] = '_';
114 memcpy(psz + 1, pszSymbol, cch + 1);
115 pszSymbol = psz;
116#endif
117 *ppvValue = dlsym((void *)pModNative->hNative, pszSymbol);
118 if (*ppvValue)
119 return VINF_SUCCESS;
120 return VERR_SYMBOL_NOT_FOUND;
121}
122
123
124DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod)
125{
126 PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
127#ifdef __SANITIZE_ADDRESS__
128 /* If we are compiled with enabled address sanitizer (gcc/llvm), don't
129 * unload the module to prevent <unknown module> in the stack trace */
130 pModNative->fFlags |= RTLDRLOAD_FLAGS_NO_UNLOAD;
131#endif
132 if ( (pModNative->fFlags & RTLDRLOAD_FLAGS_NO_UNLOAD)
133 || !dlclose((void *)pModNative->hNative))
134 {
135 pModNative->hNative = (uintptr_t)0;
136 return VINF_SUCCESS;
137 }
138 Log(("rtldrNativeFree: dlclose(%p) failed: %s\n", pModNative->hNative, dlerror()));
139 return VERR_GENERAL_FAILURE;
140}
141
142
143DECLHIDDEN(int) rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod)
144{
145 /*
146 * For the present we ASSUME that we can trust dlopen to load what we want
147 * when not specifying a path. There seems to be very little we can do to
148 * restrict the places dlopen will search for library without doing
149 * auditing (linux) or something like that.
150 */
151 Assert(strchr(pszFilename, '/') == NULL);
152
153 uint32_t const fFlags2 = fFlags & ~(RTLDRLOAD_FLAGS_SO_VER_BEGIN_MASK | RTLDRLOAD_FLAGS_SO_VER_END_MASK);
154
155 /*
156 * If no suffix is given and we haven't got any RTLDRLOAD_FLAGS_SO_VER_ range to work
157 * with, we can call RTLdrLoadEx directly.
158 */
159 if (!pszExt)
160 {
161#if !defined(RT_OS_DARWIN) && !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
162 if ( (fFlags & RTLDRLOAD_FLAGS_SO_VER_BEGIN_MASK) >> RTLDRLOAD_FLAGS_SO_VER_BEGIN_SHIFT
163 == (fFlags & RTLDRLOAD_FLAGS_SO_VER_END_MASK) >> RTLDRLOAD_FLAGS_SO_VER_END_SHIFT)
164#endif
165 return RTLdrLoadEx(pszFilename, phLdrMod, fFlags2, NULL);
166 pszExt = "";
167 }
168
169 /*
170 * Combine filename and suffix and then do the loading.
171 */
172 size_t const cchFilename = strlen(pszFilename);
173 size_t const cchSuffix = strlen(pszExt);
174 char *pszTmp = (char *)alloca(cchFilename + cchSuffix + 16 + 1);
175 memcpy(pszTmp, pszFilename, cchFilename);
176 memcpy(&pszTmp[cchFilename], pszExt, cchSuffix);
177 pszTmp[cchFilename + cchSuffix] = '\0';
178
179 int rc = RTLdrLoadEx(pszTmp, phLdrMod, fFlags2, NULL);
180
181#if !defined(RT_OS_DARWIN) && !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
182 /*
183 * If no version was given after the .so and do .so.MAJOR search according
184 * to the range in the fFlags.
185 */
186 if (RT_FAILURE(rc) && !(fFlags & RTLDRLOAD_FLAGS_NO_SUFFIX))
187 {
188 const char *pszActualSuff = RTPathSuffix(pszTmp);
189 if (pszActualSuff && strcmp(pszActualSuff, ".so") == 0)
190 {
191 int32_t const iBegin = (fFlags & RTLDRLOAD_FLAGS_SO_VER_BEGIN_MASK) >> RTLDRLOAD_FLAGS_SO_VER_BEGIN_SHIFT;
192 int32_t const iEnd = (fFlags & RTLDRLOAD_FLAGS_SO_VER_END_MASK) >> RTLDRLOAD_FLAGS_SO_VER_END_SHIFT;
193 int32_t const iIncr = iBegin <= iEnd ? 1 : -1;
194 for (int32_t iMajorVer = iBegin; iMajorVer != iEnd; iMajorVer += iIncr)
195 {
196 RTStrPrintf(&pszTmp[cchFilename + cchSuffix], 16 + 1, ".%d", iMajorVer);
197 rc = RTLdrLoadEx(pszTmp, phLdrMod, fFlags2, NULL);
198 if (RT_SUCCESS(rc))
199 break;
200 }
201 }
202 }
203#endif
204
205 return rc;
206}
207
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