VirtualBox

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

Last change on this file since 8245 was 8245, checked in by vboxsync, 17 years ago

rebranding: IPRT files again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.5 KB
Line 
1/* $Id: ldrNative.cpp 8245 2008-04-21 17:24:28Z 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 <iprt/alloc.h>
38#include <iprt/assert.h>
39#include <iprt/log.h>
40#include <iprt/param.h>
41#include <iprt/path.h>
42#include <iprt/string.h>
43#include <iprt/err.h>
44#include "internal/ldr.h"
45
46
47/** @copydoc RTLDROPS::pfnEnumSymbols */
48static DECLCALLBACK(int) rtldrNativeEnumSymbols(PRTLDRMODINTERNAL pMod, unsigned fFlags, const void *pvBits, RTUINTPTR BaseAddress,
49 PFNRTLDRENUMSYMS pfnCallback, void *pvUser)
50{
51 return VERR_NOT_SUPPORTED;
52}
53
54
55/** @copydoc RTLDROPS::pfnDone */
56static DECLCALLBACK(int) rtldrNativeDone(PRTLDRMODINTERNAL pMod)
57{
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 42
78};
79
80
81
82/**
83 * Loads a dynamic load library (/shared object) image file using native
84 * OS facilities.
85 *
86 * The filename will be appended the default DLL/SO extension of
87 * the platform if it have been omitted. This means that it's not
88 * possible to load DLLs/SOs with no extension using this interface,
89 * but that's not a bad tradeoff.
90 *
91 * If no path is specified in the filename, the OS will usually search it's library
92 * path to find the image file.
93 *
94 * @returns iprt status code.
95 * @param pszFilename Image filename.
96 * @param phLdrMod Where to store the handle to the loaded module.
97 */
98RTDECL(int) RTLdrLoad(const char *pszFilename, PRTLDRMOD phLdrMod)
99{
100 LogFlow(("RTLdrLoad: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
101
102 /*
103 * validate input.
104 */
105 AssertMsgReturn(VALID_PTR(pszFilename), ("pszFilename=%p\n", pszFilename), VERR_INVALID_PARAMETER);
106 AssertMsgReturn(VALID_PTR(phLdrMod), ("phLdrMod=%p\n", phLdrMod), VERR_INVALID_PARAMETER);
107
108 /*
109 * Allocate and initialize module structure.
110 */
111 int rc = VERR_NO_MEMORY;
112 PRTLDRMODNATIVE pMod = (PRTLDRMODNATIVE)RTMemAlloc(sizeof(*pMod));
113 if (pMod)
114 {
115 pMod->Core.u32Magic = RTLDRMOD_MAGIC;
116 pMod->Core.eState = LDR_STATE_LOADED;
117 pMod->Core.pOps = &s_rtldrNativeOps;
118 pMod->hNative = ~(uintptr_t)0;
119
120 /*
121 * Attempt to open the module.
122 */
123 rc = rtldrNativeLoad(pszFilename, &pMod->hNative);
124 if (RT_SUCCESS(rc))
125 {
126 *phLdrMod = &pMod->Core;
127 LogFlow(("RTLdrLoad: returns %Rrc *phLdrMod=%RTldrm\n", rc, *phLdrMod));
128 return rc;
129 }
130 RTMemFree(pMod);
131 }
132 *phLdrMod = NIL_RTLDRMOD;
133 LogFlow(("RTLdrLoad: returns %Rrc\n", rc));
134 return rc;
135}
136
137
138/**
139 * Loads a dynamic load library (/shared object) image file using native
140 * OS facilities.
141 *
142 * If the path is specified in the filename, only this path is used.
143 * If only the image file name is specified, then try to load it from:
144 * - RTPathAppPrivateArch
145 * - RTPathSharedLibs (legacy)
146 *
147 * @returns iprt status code.
148 * @param pszFilename Image filename.
149 * @param phLdrMod Where to store the handle to the loaded module.
150 */
151RTDECL(int) RTLdrLoadAppSharedLib(const char *pszFilename, PRTLDRMOD phLdrMod)
152{
153 LogFlow(("RTLdrLoadAppSharedLib: pszFilename=%p:{%s} phLdrMod=%p\n", pszFilename, pszFilename, phLdrMod));
154
155 /*
156 * Validate input.
157 */
158 AssertMsgReturn(VALID_PTR(pszFilename), ("pszFilename=%p\n", pszFilename), VERR_INVALID_PARAMETER);
159 AssertMsgReturn(VALID_PTR(phLdrMod), ("phLdrMod=%p\n", phLdrMod), VERR_INVALID_PARAMETER);
160
161 /*
162 * If a path is specified, just load the file.
163 */
164 if (RTPathHavePath(pszFilename))
165 return RTLdrLoad(pszFilename, phLdrMod);
166
167 /*
168 * By default nothing is found.
169 */
170 int rc = VERR_FILE_NOT_FOUND;
171 *phLdrMod = NIL_RTLDRMOD;
172
173 /*
174 * Try default locations.
175 */
176 int i;
177 for (i = 0;; i++)
178 {
179 /*
180 * Get the appropriate base path.
181 */
182 char szBase[RTPATH_MAX];
183 if (i == 0)
184 rc = RTPathAppPrivateArch(szBase, sizeof (szBase));
185 else if (i == 1)
186 rc = RTPathSharedLibs(szBase, sizeof (szBase));
187 else
188 break;
189
190 if (RT_SUCCESS(rc))
191 {
192 /*
193 * Got the base path. Construct szPath = pszBase + pszFilename
194 */
195 char szPath[RTPATH_MAX];
196 rc = RTPathAbsEx(szBase, pszFilename, szPath, sizeof (szPath));
197 if (RT_SUCCESS(rc))
198 {
199 /*
200 * Finally try to load the image file.
201 */
202 rc = RTLdrLoad(szPath, phLdrMod);
203 if (RT_SUCCESS(rc))
204 {
205 /*
206 * Successfully loaded the image file.
207 */
208 LogFlow(("Library loaded: [%s]\n", szPath));
209 break;
210 }
211 }
212 }
213 }
214 LogFlow(("RTLdrLoadAppSharedLib: returns %Rrc\n", rc));
215 return rc;
216}
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