VirtualBox

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

Last change on this file since 21337 was 21337, checked in by vboxsync, 15 years ago

IPRT,HostDrv,AddDrv: Export public IPRT symbols for the linux kernel (pain).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: ldrNative.cpp 21337 2009-07-07 14:58:27Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, Native interface.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_LDR
36#include <iprt/ldr.h>
37#include "internal/iprt.h"
38
39#include <iprt/alloc.h>
40#include <iprt/assert.h>
41#include <iprt/log.h>
42#include <iprt/param.h>
43#include <iprt/path.h>
44#include <iprt/string.h>
45#include <iprt/err.h>
46#include "internal/ldr.h"
47
48
49/** @copydoc RTLDROPS::pfnEnumSymbols */
50static DECLCALLBACK(int) rtldrNativeEnumSymbols(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress,
51 PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
52{
53 return VERR_NOT_SUPPORTED;
54}
55
56
57/** @copydoc RTLDROPS::pfnDone */
58static DECLCALLBACK(int) rtldrNativeDone(PRTLDRMODINTERNAL pMod)
59{
60 return VINF_SUCCESS;
61}
62
63
64/**
65 * Operations for a native module.
66 */
67static const RTLDROPS s_rtldrNativeOps =
68{
69 "native",
70 rtldrNativeClose,
71 rtldrNativeGetSymbol,
72 rtldrNativeDone,
73 rtldrNativeEnumSymbols,
74 /* ext: */
75 NULL,
76 NULL,
77 NULL,
78 NULL,
79 42
80};
81
82
83
84/**
85 * Loads a dynamic load library (/shared object) image file using native
86 * OS facilities.
87 *
88 * The filename will be appended the default DLL/SO extension of
89 * the platform if it have been omitted. This means that it's not
90 * possible to load DLLs/SOs with no extension using this interface,
91 * but that's not a bad tradeoff.
92 *
93 * If no path is specified in the filename, the OS will usually search it's library
94 * path to find the image file.
95 *
96 * @returns iprt status code.
97 * @param pszFilename Image filename.
98 * @param phLdrMod Where to store the handle to the loaded module.
99 */
100RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
101{
102 LogFlow(("RTLdrLoad: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
103
104 /*
105 * validate input.
106 */
107 AssertMsgReturn(VALID_PTR(pszFilename), ("pszFilename=%p\n", pszFilename), VERR_INVALID_PARAMETER);
108 AssertMsgReturn(VALID_PTR(phLdrMod), ("phLdrMod=%p\n", phLdrMod), VERR_INVALID_PARAMETER);
109
110 /*
111 * Allocate and initialize module structure.
112 */
113 int rc = VERR_NO_MEMORY;
114 PRTLDRMODNATIVE pMod = (PRTLDRMODNATIVE)RTMemAlloc(sizeof(*pMod));
115 if (pMod)
116 {
117 pMod->Core.u32Magic = RTLDRMOD_MAGIC;
118 pMod->Core.eState = LDR_STATE_LOADED;
119 pMod->Core.pOps = &s_rtldrNativeOps;
120 pMod->hNative = ~(uintptr_t)0;
121
122 /*
123 * Attempt to open the module.
124 */
125 rc = rtldrNativeLoad(pszFilename, &pMod->hNative);
126 if (RT_SUCCESS(rc))
127 {
128 *phLdrMod = &pMod->Core;
129 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc, *phLdrMod));
130 return rc;
131 }
132 RTMemFree(pMod);
133 }
134 *phLdrMod = NIL_RTLDRMOD;
135 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
136 return rc;
137}
138RT_EXPORT_SYMBOL(RTLdrLoad);
139
140
141/**
142 * Loads a dynamic load library (/shared object) image file residing in the
143 * RTPathAppPrivateArch() directory.
144 *
145 * Suffix is not required.
146 *
147 * @returns iprt status code.
148 * @param pszFilename Image filename. No path.
149 * @param phLdrMod Where to store the handle to the loaded module.
150 */
151RTDECL(int) RTLdrLoadAppPriv(const char *pszFilename, PRTLDRMOD phLdrMod)
152{
153 LogFlow(("RTLdrLoadAppPriv: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
154
155 /*
156 * Validate input.
157 */
158 AssertPtrReturn(phLdrMod, VERR_INVALID_PARAMETER);
159 *phLdrMod = NIL_RTLDRMOD;
160 AssertPtrReturn(pszFilename, VERR_INVALID_PARAMETER);
161 AssertMsgReturn(!RTPathHavePath(pszFilename), ("%s\n", pszFilename), VERR_INVALID_PARAMETER);
162
163 /*
164 * Check the filename.
165 */
166 size_t cchFilename = strlen(pszFilename);
167 AssertMsgReturn(cchFilename > (RTPATH_MAX / 4) * 3, ("%zu\n", cchFilename), VERR_INVALID_PARAMETER);
168
169 const char *pszExt = "";
170 size_t cchExt = 0;
171 if (!RTPathHaveExt(pszFilename))
172 {
173 pszExt = RTLdrGetSuff();
174 cchExt = strlen(pszExt);
175 }
176
177 /*
178 * Construct the private arch path and check if the file exists.
179 */
180 char szPath[RTPATH_MAX];
181 int rc = RTPathAppPrivateArch(szPath, sizeof(szPath) - 1 - cchExt - cchFilename);
182 AssertRCReturn(rc, rc);
183
184 char *psz = strchr(szPath, '\0');
185 *psz++ = RTPATH_SLASH;
186 memcpy(psz, pszFilename, cchFilename);
187 psz += cchFilename;
188 memcpy(psz, pszExt, cchExt + 1);
189
190 if (!RTPathExists(szPath))
191 {
192 LogRel(("RTLdrLoadAppPriv: \"%s\" not found\n", szPath));
193 return VERR_FILE_NOT_FOUND;
194 }
195
196 /*
197 * Pass it on to RTLdrLoad.
198 */
199 rc = RTLdrLoad(szPath, phLdrMod);
200
201 LogFlow(("RTLdrLoadAppPriv: returns %Rrc\n", rc));
202 return rc;
203}
204RT_EXPORT_SYMBOL(RTLdrLoadAppPriv);
205
206
207/**
208 * Gets the default file suffix for DLL/SO/DYLIB/whatever.
209 *
210 * @returns The stuff (readonly).
211 */
212RTDECL(const char *) RTLdrGetSuff(void)
213{
214#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
215 static const char s_szSuff[] = ".DLL";
216#elif defined(RT_OS_L4)
217 static const char s_szSuff[] = ".s.so";
218#elif defined(RT_OS_DARWIN)
219 static const char s_szSuff[] = ".dylib";
220#else
221 static const char s_szSuff[] = ".so";
222#endif
223
224 return s_szSuff;
225}
226RT_EXPORT_SYMBOL(RTLdrGetSuff);
227
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