VirtualBox

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

Last change on this file since 46168 was 46164, checked in by vboxsync, 12 years ago

More exteran .dSYM and .dwo bundles/files changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.5 KB
Line 
1/* $Id: ldrNative.cpp 46164 2013-05-19 16:58:01Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Native interface.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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,
47 RTUINTPTR BaseAddress, PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
48{
49 NOREF(pMod); NOREF(fFlags); NOREF(pvBits); NOREF(BaseAddress); NOREF(pfnCallback); NOREF(pvUser);
50 return VERR_NOT_SUPPORTED;
51}
52
53
54/** @copydoc RTLDROPS::pfnDone */
55static DECLCALLBACK(int) rtldrNativeDone(PRTLDRMODINTERNAL pMod)
56{
57 NOREF(pMod);
58 return VINF_SUCCESS;
59}
60
61
62/**
63 * Operations for a native module.
64 */
65static const RTLDROPS s_rtldrNativeOps =
66{
67 "native",
68 rtldrNativeClose,
69 rtldrNativeGetSymbol,
70 rtldrNativeDone,
71 rtldrNativeEnumSymbols,
72 /* ext: */
73 NULL,
74 NULL,
75 NULL,
76 NULL,
77 NULL,
78 NULL,
79 NULL,
80 NULL,
81 NULL,
82 NULL,
83 NULL,
84 42
85};
86
87
88
89/**
90 * Loads a dynamic load library (/shared object) image file using native
91 * OS facilities.
92 *
93 * The filename will be appended the default DLL/SO extension of
94 * the platform if it have been omitted. This means that it's not
95 * possible to load DLLs/SOs with no extension using this interface,
96 * but that's not a bad tradeoff.
97 *
98 * If no path is specified in the filename, the OS will usually search it's library
99 * path to find the image file.
100 *
101 * @returns iprt status code.
102 * @param pszFilename Image filename.
103 * @param phLdrMod Where to store the handle to the loaded module.
104 */
105RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
106{
107 return RTLdrLoadEx(pszFilename, phLdrMod, RTLDRLOAD_FLAGS_LOCAL, NULL);
108}
109RT_EXPORT_SYMBOL(RTLdrLoad);
110
111
112RTDECL(int) RTLdrLoadEx(const char *pszFilename, PRTLDRMOD phLdrMod, uint32_t fFlags, PRTERRINFO pErrInfo)
113{
114 LogFlow(("RTLdrLoadEx: pszFilename=%p:{%s} phLdrMod=%p fFlags=%#x pErrInfo=%p\n", pszFilename, pszFilename, phLdrMod, fFlags, pErrInfo));
115
116 /*
117 * Validate and massage the input.
118 */
119 RTErrInfoClear(pErrInfo);
120 AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
121 AssertPtrReturn(phLdrMod, VERR_INVALID_POINTER);
122 AssertReturn(!(fFlags & ~RTLDRLOAD_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
123
124 /*
125 * Allocate and initialize module structure.
126 */
127 int rc = VERR_NO_MEMORY;
128 PRTLDRMODNATIVE pMod = (PRTLDRMODNATIVE)RTMemAlloc(sizeof(*pMod));
129 if (pMod)
130 {
131 pMod->Core.u32Magic = RTLDRMOD_MAGIC;
132 pMod->Core.eState = LDR_STATE_LOADED;
133 pMod->Core.pOps = &s_rtldrNativeOps;
134 pMod->Core.pReader = NULL;
135 pMod->Core.enmFormat = RTLDRFMT_NATIVE;
136 pMod->Core.enmType = RTLDRTYPE_SHARED_LIBRARY_RELOCATABLE; /* approx */
137#ifdef RT_BIG_ENDIAN
138 pMod->Core.enmEndian = RTLDRENDIAN_BIG;
139#else
140 pMod->Core.enmEndian = RTLDRENDIAN_LITTLE;
141#endif
142#ifdef RT_ARCH_AMD64
143 pMod->Core.enmArch = RTLDRARCH_AMD64;
144#elif defined(RT_ARCH_X86)
145 pMod->Core.enmArch = RTLDRARCH_X86_32;
146#else
147 pMod->Core.enmArch = RTLDRARCH_HOST;
148#endif
149 pMod->hNative = ~(uintptr_t)0;
150
151 /*
152 * Attempt to open the module.
153 */
154 rc = rtldrNativeLoad(pszFilename, &pMod->hNative, fFlags, pErrInfo);
155 if (RT_SUCCESS(rc))
156 {
157 *phLdrMod = &pMod->Core;
158 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc, *phLdrMod));
159 return rc;
160 }
161
162 RTMemFree(pMod);
163 }
164 else
165 RTErrInfoSetF(pErrInfo, rc, "Failed to allocate %zu bytes for the module handle", sizeof(*pMod));
166 *phLdrMod = NIL_RTLDRMOD;
167 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
168 return rc;
169}
170RT_EXPORT_SYMBOL(RTLdrLoadEx);
171
172
173/**
174 * Loads a dynamic load library (/shared object) image file residing in the
175 * RTPathAppPrivateArch() directory.
176 *
177 * Suffix is not required.
178 *
179 * @returns iprt status code.
180 * @param pszFilename Image filename. No path.
181 * @param phLdrMod Where to store the handle to the loaded module.
182 */
183RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
184{
185 LogFlow(("RTLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
186
187 /*
188 * Validate input.
189 */
190 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
191 *phLdrMod = NIL_RTLDRMOD;
192 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
193 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
194
195 /*
196 * Check the filename.
197 */
198 size_t cchFilename = strlen(pszFilename);
199 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
200
201 const char *pszExt = "";
202 size_t cchExt = 0;
203 if (!RTPathHaveExt(pszFilename))
204 {
205 pszExt = RTLdrGetSuff();
206 cchExt = strlen(pszExt);
207 }
208
209 /*
210 * Construct the private arch path and check if the file exists.
211 */
212 char szPath[RTPATH_MAX];
213 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
214 AssertRCReturn(rc, rc);
215
216 char *psz = strchr(szPath, '\0');
217 *psz++ = RTPATH_SLASH;
218 memcpy(psz, pszFilename, cchFilename);
219 psz += cchFilename;
220 memcpy(psz, pszExt, cchExt + 1);
221
222 if (!RTPathExists(szPath))
223 {
224 LogRel(("RTLdrLoadAppPriv: \"%s\" not found\n", szPath));
225 return VERR_FILE_NOT_FOUND;
226 }
227
228 /*
229 * Pass it on to RTLdrLoad.
230 */
231 rc = RTLdrLoad(szPath, phLdrMod);
232
233 LogFlow(("RTLdrLoadAppPriv: returns %Rrc\n", rc));
234 return rc;
235}
236RT_EXPORT_SYMBOL(RTLdrLoadAppPriv);
237
238
239/**
240 * Gets the default file suffix for DLL/SO/DYLIB/whatever.
241 *
242 * @returns The stuff (readonly).
243 */
244RTDECL(const char *) RTLdrGetSuff(void)
245{
246#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
247 static const char s_szSuff[] = ".DLL";
248#elif defined(RT_OS_L4)
249 static const char s_szSuff[] = ".s.so";
250#elif defined(RT_OS_DARWIN)
251 static const char s_szSuff[] = ".dylib";
252#else
253 static const char s_szSuff[] = ".so";
254#endif
255
256 return s_szSuff;
257}
258RT_EXPORT_SYMBOL(RTLdrGetSuff);
259
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