VirtualBox

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

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

Runtime function to load a shared library from the application directories.

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