VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/ldr/ldrFile.cpp@ 46087

Last change on this file since 46087 was 46023, checked in by vboxsync, 11 years ago

RTLdrOpen: Defined the first flag RTLDR_O_FOR_DEBUG for use with RTDbgMod.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.3 KB
Line 
1/* $Id: ldrFile.cpp 46023 2013-05-13 15:34:54Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, The File Oriented Parts.
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/file.h>
37#include <iprt/assert.h>
38#include <iprt/log.h>
39#include <iprt/err.h>
40#include <iprt/string.h>
41#include "internal/ldr.h"
42#include "internal/ldrMZ.h"
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48/**
49 * File Reader instance.
50 * This provides raw image bits from a file.
51 */
52typedef struct RTLDRREADERFILE
53{
54 /** The core. */
55 RTLDRREADER Core;
56 /** The file. */
57 RTFILE hFile;
58 /** The file size. */
59 RTFOFF cbFile;
60 /** The current offset. */
61 RTFOFF off;
62 /** Number of users or the mapping. */
63 RTUINT cMappings;
64 /** Pointer to the in memory mapping. */
65 void *pvMapping;
66 /** The filename (variable size). */
67 char szFilename[1];
68} RTLDRREADERFILE, *PRTLDRREADERFILE;
69
70
71/** @copydoc RTLDRREADER::pfnRead */
72static DECLCALLBACK(int) rtldrFileRead(PRTLDRREADER pReader, void *pvBuf, size_t cb, RTFOFF off)
73{
74 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
75
76 /*
77 * Seek.
78 */
79 if (pFileReader->off != off)
80 {
81 int rc = RTFileSeek(pFileReader->hFile, off, RTFILE_SEEK_BEGIN, NULL);
82 if (RT_FAILURE(rc))
83 {
84 pFileReader->off = -1;
85 return rc;
86 }
87 pFileReader->off = off;
88 }
89
90 /*
91 * Read.
92 */
93 int rc = RTFileRead(pFileReader->hFile, pvBuf, cb, NULL);
94 if (RT_SUCCESS(rc))
95 pFileReader->off += cb;
96 else
97 pFileReader->off = -1;
98 return rc;
99}
100
101
102/** @copydoc RTLDRREADER::pfnTell */
103static DECLCALLBACK(RTFOFF) rtldrFileTell(PRTLDRREADER pReader)
104{
105 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
106 return pFileReader->off;
107}
108
109
110/** @copydoc RTLDRREADER::pfnSize */
111static DECLCALLBACK(RTFOFF) rtldrFileSize(PRTLDRREADER pReader)
112{
113 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
114 return pFileReader->cbFile;
115}
116
117
118/** @copydoc RTLDRREADER::pfnLogName */
119static DECLCALLBACK(const char *) rtldrFileLogName(PRTLDRREADER pReader)
120{
121 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
122 return pFileReader->szFilename;
123}
124
125
126/** @copydoc RTLDRREADER::pfnMap */
127static DECLCALLBACK(int) rtldrFileMap(PRTLDRREADER pReader, const void **ppvBits)
128{
129 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
130
131 /*
132 * Already mapped?
133 */
134 if (pFileReader->pvMapping)
135 {
136 pFileReader->cMappings++;
137 *ppvBits = pFileReader->pvMapping;
138 return VINF_SUCCESS;
139 }
140
141 /*
142 * Allocate memory.
143 */
144 size_t cb = (size_t)pFileReader->cbFile;
145 if ((RTFOFF)cb != pFileReader->cbFile)
146 return VERR_IMAGE_TOO_BIG;
147 pFileReader->pvMapping = RTMemAlloc(cb);
148 if (!pFileReader->pvMapping)
149 return VERR_NO_MEMORY;
150 int rc = rtldrFileRead(pReader, pFileReader->pvMapping, cb, 0);
151 if (RT_SUCCESS(rc))
152 {
153 pFileReader->cMappings = 1;
154 *ppvBits = pFileReader->pvMapping;
155 }
156 else
157 {
158 RTMemFree(pFileReader->pvMapping);
159 pFileReader->pvMapping = NULL;
160 }
161
162 return rc;
163}
164
165
166/** @copydoc RTLDRREADER::pfnUnmap */
167static DECLCALLBACK(int) rtldrFileUnmap(PRTLDRREADER pReader, const void *pvBits)
168{
169 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
170 AssertReturn(pFileReader->cMappings > 0, VERR_INVALID_PARAMETER);
171
172 if (!--pFileReader->cMappings)
173 {
174 RTMemFree(pFileReader->pvMapping);
175 pFileReader->pvMapping = NULL;
176 }
177
178 NOREF(pvBits);
179 return VINF_SUCCESS;
180}
181
182
183/** @copydoc RTLDRREADER::pfnDestroy */
184static DECLCALLBACK(int) rtldrFileDestroy(PRTLDRREADER pReader)
185{
186 int rc = VINF_SUCCESS;
187 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
188 if (pFileReader->hFile != NIL_RTFILE)
189 {
190 rc = RTFileClose(pFileReader->hFile);
191 AssertRC(rc);
192 pFileReader->hFile = NIL_RTFILE;
193 }
194 RTMemFree(pFileReader);
195 return rc;
196}
197
198
199/**
200 * Opens a loader file reader.
201 *
202 * @returns iprt status code.
203 * @param ppReader Where to store the reader instance on success.
204 * @param pszFilename The file to open.
205 */
206static int rtldrFileCreate(PRTLDRREADER *ppReader, const char *pszFilename)
207{
208 size_t cchFilename = strlen(pszFilename);
209 int rc = VERR_NO_MEMORY;
210 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)RTMemAlloc(sizeof(*pFileReader) + cchFilename);
211 if (pFileReader)
212 {
213 memcpy(pFileReader->szFilename, pszFilename, cchFilename + 1);
214 rc = RTFileOpen(&pFileReader->hFile, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
215 if (RT_SUCCESS(rc))
216 {
217 rc = RTFileGetSize(pFileReader->hFile, (uint64_t *)&pFileReader->cbFile);
218 if (RT_SUCCESS(rc))
219 {
220 pFileReader->Core.pfnRead = rtldrFileRead;
221 pFileReader->Core.pfnTell = rtldrFileTell;
222 pFileReader->Core.pfnSize = rtldrFileSize;
223 pFileReader->Core.pfnLogName = rtldrFileLogName;
224 pFileReader->Core.pfnMap = rtldrFileMap;
225 pFileReader->Core.pfnUnmap = rtldrFileUnmap;
226 pFileReader->Core.pfnDestroy = rtldrFileDestroy;
227 pFileReader->off = 0;
228 pFileReader->cMappings = 0;
229 pFileReader->pvMapping = NULL;
230 *ppReader = &pFileReader->Core;
231 return VINF_SUCCESS;
232 }
233
234 RTFileClose(pFileReader->hFile);
235 }
236 RTMemFree(pFileReader);
237 }
238 *ppReader = NULL;
239 return rc;
240}
241
242
243/**
244 * Open a binary image file, extended version.
245 *
246 * @returns iprt status code.
247 * @param pszFilename Image filename.
248 * @param fFlags Reserved, MBZ.
249 * @param enmArch CPU architecture specifier for the image to be loaded.
250 * @param phLdrMod Where to store the handle to the loader module.
251 */
252RTDECL(int) RTLdrOpen(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
253{
254 LogFlow(("RTLdrOpen: pszFilename=%p:{%s} fFlags=%#x enmArch=%d phLdrMod=%p\n",
255 pszFilename, pszFilename, fFlags, enmArch, phLdrMod));
256 AssertMsgReturn(!(fFlags & ~RTLDR_O_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
257 AssertMsgReturn(enmArch > RTLDRARCH_INVALID && enmArch < RTLDRARCH_END, ("%d\n", enmArch), VERR_INVALID_PARAMETER);
258
259 /*
260 * Resolve RTLDRARCH_HOST.
261 */
262 if (enmArch == RTLDRARCH_HOST)
263#if defined(RT_ARCH_AMD64)
264 enmArch = RTLDRARCH_AMD64;
265#elif defined(RT_ARCH_X86)
266 enmArch = RTLDRARCH_X86_32;
267#else
268 enmArch = RTLDRARCH_WHATEVER;
269#endif
270
271 /*
272 * Create file reader & invoke worker which identifies and calls the image interpreter.
273 */
274 PRTLDRREADER pReader;
275 int rc = rtldrFileCreate(&pReader, pszFilename);
276 if (RT_SUCCESS(rc))
277 {
278 rc = rtldrOpenWithReader(pReader, fFlags, enmArch, phLdrMod);
279 if (RT_SUCCESS(rc))
280 {
281 LogFlow(("RTLdrOpen: return %Rrc *phLdrMod\n", rc, *phLdrMod));
282 return rc;
283 }
284 pReader->pfnDestroy(pReader);
285 }
286 *phLdrMod = NIL_RTLDRMOD;
287 LogFlow(("RTLdrOpen: return %Rrc\n", rc));
288 return rc;
289}
290RT_EXPORT_SYMBOL(RTLdrOpen);
291
292
293/**
294 * Opens a binary image file using kLdr.
295 *
296 * @returns iprt status code.
297 * @param pszFilename Image filename.
298 * @param fFlags Reserved, MBZ.
299 * @param enmArch CPU architecture specifier for the image to be loaded.
300 * @param phLdrMod Where to store the handle to the loaded module.
301 * @remark Primarily for testing the loader.
302 */
303RTDECL(int) RTLdrOpenkLdr(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
304{
305#ifdef LDR_WITH_KLDR
306 LogFlow(("RTLdrOpenkLdr: pszFilename=%p:{%s} fFlags=%#x enmArch=%d phLdrMod=%p\n",
307 pszFilename, pszFilename, fFlags, enmArch, phLdrMod));
308 AssertMsgReturn(!(fFlags & ~RTLDR_O_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
309
310 /*
311 * Resolve RTLDRARCH_HOST.
312 */
313 if (enmArch == RTLDRARCH_HOST)
314# if defined(RT_ARCH_AMD64)
315 enmArch = RTLDRARCH_AMD64;
316# elif defined(RT_ARCH_X86)
317 enmArch = RTLDRARCH_X86_32;
318# else
319 enmArch = RTLDRARCH_WHATEVER;
320# endif
321
322 /*
323 * Create file reader & invoke worker which identifies and calls the image interpreter.
324 */
325 PRTLDRREADER pReader;
326 int rc = rtldrFileCreate(&pReader, pszFilename);
327 if (RT_SUCCESS(rc))
328 {
329 rc = rtldrkLdrOpen(pReader, fFlags, enmArch, phLdrMod);
330 if (RT_SUCCESS(rc))
331 {
332 LogFlow(("RTLdrOpenkLdr: return %Rrc *phLdrMod\n", rc, *phLdrMod));
333 return rc;
334 }
335 pReader->pfnDestroy(pReader);
336 }
337 *phLdrMod = NIL_RTLDRMOD;
338 LogFlow(("RTLdrOpenkLdr: return %Rrc\n", rc));
339 return rc;
340
341#else
342 return RTLdrOpen(pszFilename, fFlags, enmArch, phLdrMod);
343#endif
344}
345RT_EXPORT_SYMBOL(RTLdrOpenkLdr);
346
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