[1] | 1 | /* $Id: ldrNative-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[8245] | 3 | * IPRT - Binary Image Loader, POSIX native.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[1] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[5999] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[5999] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[5999] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[1] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[1] | 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 |
|
---|
[79003] | 54 | /*********************************************************************************************************************************
|
---|
| 55 | * Global Variables *
|
---|
| 56 | *********************************************************************************************************************************/
|
---|
| 57 | #if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
|
---|
| 58 | static const char g_szSuff[] = ".DLL";
|
---|
| 59 | #elif defined(RT_OS_L4)
|
---|
| 60 | static const char g_szSuff[] = ".s.so";
|
---|
| 61 | #elif defined(RT_OS_DARWIN)
|
---|
| 62 | static const char g_szSuff[] = ".dylib";
|
---|
| 63 | #else
|
---|
| 64 | static const char g_szSuff[] = ".so";
|
---|
| 65 | #endif
|
---|
| 66 |
|
---|
| 67 |
|
---|
[74638] | 68 | DECLHIDDEN(int) rtldrNativeLoad(const char *pszFilename, uintptr_t *phHandle, uint32_t fFlags, PRTERRINFO pErrInfo)
|
---|
[1] | 69 | {
|
---|
| 70 | /*
|
---|
| 71 | * Do we need to add an extension?
|
---|
| 72 | */
|
---|
[74253] | 73 | if (!RTPathHasSuffix(pszFilename) && !(fFlags & RTLDRLOAD_FLAGS_NO_SUFFIX))
|
---|
[1] | 74 | {
|
---|
| 75 | size_t cch = strlen(pszFilename);
|
---|
[79003] | 76 | char *psz = (char *)alloca(cch + sizeof(g_szSuff));
|
---|
[1] | 77 | if (!psz)
|
---|
[35183] | 78 | return RTErrInfoSet(pErrInfo, VERR_NO_MEMORY, "alloca failed");
|
---|
[1] | 79 | memcpy(psz, pszFilename, cch);
|
---|
[79003] | 80 | memcpy(psz + cch, g_szSuff, sizeof(g_szSuff));
|
---|
[1] | 81 | pszFilename = psz;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /*
|
---|
| 85 | * Attempt load.
|
---|
| 86 | */
|
---|
[35152] | 87 | int fFlagsNative = RTLD_NOW;
|
---|
[35191] | 88 | if (fFlags & RTLDRLOAD_FLAGS_GLOBAL)
|
---|
[35152] | 89 | fFlagsNative |= RTLD_GLOBAL;
|
---|
| 90 | else
|
---|
| 91 | fFlagsNative |= RTLD_LOCAL;
|
---|
| 92 | void *pvMod = dlopen(pszFilename, fFlagsNative);
|
---|
[1] | 93 | if (pvMod)
|
---|
| 94 | {
|
---|
| 95 | *phHandle = (uintptr_t)pvMod;
|
---|
| 96 | return VINF_SUCCESS;
|
---|
| 97 | }
|
---|
[34959] | 98 |
|
---|
| 99 | const char *pszDlError = dlerror();
|
---|
[35183] | 100 | RTErrInfoSet(pErrInfo, VERR_FILE_NOT_FOUND, pszDlError);
|
---|
[34959] | 101 | LogRel(("rtldrNativeLoad: dlopen('%s', RTLD_NOW | RTLD_LOCAL) failed: %s\n", pszFilename, pszDlError));
|
---|
[1290] | 102 | return VERR_FILE_NOT_FOUND;
|
---|
[1] | 103 | }
|
---|
| 104 |
|
---|
| 105 |
|
---|
| 106 | DECLCALLBACK(int) rtldrNativeGetSymbol(PRTLDRMODINTERNAL pMod, const char *pszSymbol, void **ppvValue)
|
---|
| 107 | {
|
---|
| 108 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
[3672] | 109 | #ifdef RT_OS_OS2
|
---|
[1] | 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 |
|
---|
| 124 | DECLCALLBACK(int) rtldrNativeClose(PRTLDRMODINTERNAL pMod)
|
---|
| 125 | {
|
---|
| 126 | PRTLDRMODNATIVE pModNative = (PRTLDRMODNATIVE)pMod;
|
---|
[65704] | 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
|
---|
[46593] | 132 | if ( (pModNative->fFlags & RTLDRLOAD_FLAGS_NO_UNLOAD)
|
---|
| 133 | || !dlclose((void *)pModNative->hNative))
|
---|
[1] | 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 |
|
---|
[46593] | 142 |
|
---|
[74638] | 143 | DECLHIDDEN(int) rtldrNativeLoadSystem(const char *pszFilename, const char *pszExt, uint32_t fFlags, PRTLDRMOD phLdrMod)
|
---|
[46593] | 144 | {
|
---|
[79003] | 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 |
|
---|
[79561] | 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 | */
|
---|
[79003] | 159 | if (!pszExt)
|
---|
[79561] | 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 | }
|
---|
[79003] | 168 |
|
---|
[79561] | 169 | /*
|
---|
| 170 | * Combine filename and suffix and then do the loading.
|
---|
| 171 | */
|
---|
[79003] | 172 | size_t const cchFilename = strlen(pszFilename);
|
---|
| 173 | size_t const cchSuffix = strlen(pszExt);
|
---|
[79559] | 174 | char *pszTmp = (char *)alloca(cchFilename + cchSuffix + 16 + 1);
|
---|
[79003] | 175 | memcpy(pszTmp, pszFilename, cchFilename);
|
---|
| 176 | memcpy(&pszTmp[cchFilename], pszExt, cchSuffix);
|
---|
| 177 | pszTmp[cchFilename + cchSuffix] = '\0';
|
---|
| 178 |
|
---|
[79561] | 179 | int rc = RTLdrLoadEx(pszTmp, phLdrMod, fFlags2, NULL);
|
---|
[79559] | 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 | {
|
---|
[79561] | 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)
|
---|
[79559] | 195 | {
|
---|
[79561] | 196 | RTStrPrintf(&pszTmp[cchFilename + cchSuffix], 16 + 1, ".%d", iMajorVer);
|
---|
| 197 | rc = RTLdrLoadEx(pszTmp, phLdrMod, fFlags2, NULL);
|
---|
[79559] | 198 | if (RT_SUCCESS(rc))
|
---|
| 199 | break;
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | #endif
|
---|
| 204 |
|
---|
| 205 | return rc;
|
---|
[46593] | 206 | }
|
---|
| 207 |
|
---|