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