VirtualBox

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

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

First part of loading .dSYM bundles.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.3 KB
Line 
1/* $Id: ldrNative.cpp 46161 2013-05-19 13:31:13Z 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 pMod->hNative = ~(uintptr_t)0;
143
144 /*
145 * Attempt to open the module.
146 */
147 rc = rtldrNativeLoad(pszFilename, &pMod->hNative, fFlags, pErrInfo);
148 if (RT_SUCCESS(rc))
149 {
150 *phLdrMod = &pMod->Core;
151 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc, *phLdrMod));
152 return rc;
153 }
154
155 RTMemFree(pMod);
156 }
157 else
158 RTErrInfoSetF(pErrInfo, rc, "Failed to allocate %zu bytes for the module handle", sizeof(*pMod));
159 *phLdrMod = NIL_RTLDRMOD;
160 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
161 return rc;
162}
163RT_EXPORT_SYMBOL(RTLdrLoadEx);
164
165
166/**
167 * Loads a dynamic load library (/shared object) image file residing in the
168 * RTPathAppPrivateArch() directory.
169 *
170 * Suffix is not required.
171 *
172 * @returns iprt status code.
173 * @param pszFilename Image filename. No path.
174 * @param phLdrMod Where to store the handle to the loaded module.
175 */
176RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
177{
178 LogFlow(("RTLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
179
180 /*
181 * Validate input.
182 */
183 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
184 *phLdrMod = NIL_RTLDRMOD;
185 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
186 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
187
188 /*
189 * Check the filename.
190 */
191 size_t cchFilename = strlen(pszFilename);
192 AssertMsgReturn(cchFilename < (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
193
194 const char *pszExt = "";
195 size_t cchExt = 0;
196 if (!RTPathHaveExt(pszFilename))
197 {
198 pszExt = RTLdrGetSuff();
199 cchExt = strlen(pszExt);
200 }
201
202 /*
203 * Construct the private arch path and check if the file exists.
204 */
205 char szPath[RTPATH_MAX];
206 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
207 AssertRCReturn(rc, rc);
208
209 char *psz = strchr(szPath, '\0');
210 *psz++ = RTPATH_SLASH;
211 memcpy(psz, pszFilename, cchFilename);
212 psz += cchFilename;
213 memcpy(psz, pszExt, cchExt + 1);
214
215 if (!RTPathExists(szPath))
216 {
217 LogRel(("RTLdrLoadAppPriv: \"%s\" not found\n", szPath));
218 return VERR_FILE_NOT_FOUND;
219 }
220
221 /*
222 * Pass it on to RTLdrLoad.
223 */
224 rc = RTLdrLoad(szPath, phLdrMod);
225
226 LogFlow(("RTLdrLoadAppPriv: returns %Rrc\n", rc));
227 return rc;
228}
229RT_EXPORT_SYMBOL(RTLdrLoadAppPriv);
230
231
232/**
233 * Gets the default file suffix for DLL/SO/DYLIB/whatever.
234 *
235 * @returns The stuff (readonly).
236 */
237RTDECL(const char *) RTLdrGetSuff(void)
238{
239#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
240 static const char s_szSuff[] = ".DLL";
241#elif defined(RT_OS_L4)
242 static const char s_szSuff[] = ".s.so";
243#elif defined(RT_OS_DARWIN)
244 static const char s_szSuff[] = ".dylib";
245#else
246 static const char s_szSuff[] = ".so";
247#endif
248
249 return s_szSuff;
250}
251RT_EXPORT_SYMBOL(RTLdrGetSuff);
252
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette