VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 10.1 KB
Line 
1/* $Id: ldrFile.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, The File Oriented Parts.
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/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 File;
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->File, 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->File, 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 return VINF_SUCCESS;
179}
180
181
182/** @copydoc RTLDRREADER::pfnDestroy */
183static DECLCALLBACK(int) rtldrFileDestroy(PRTLDRREADER pReader)
184{
185 int rc = VINF_SUCCESS;
186 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
187 if (pFileReader->File != NIL_RTFILE)
188 {
189 rc = RTFileClose(pFileReader->File);
190 AssertRC(rc);
191 pFileReader->File = NIL_RTFILE;
192 }
193 RTMemFree(pFileReader);
194 return rc;
195}
196
197
198/**
199 * Opens a loader file reader.
200 *
201 * @returns iprt status code.
202 * @param ppReader Where to store the reader instance on success.
203 * @param pszFilename The file to open.
204 */
205static int rtldrFileCreate(PRTLDRREADER *ppReader, const char *pszFilename)
206{
207 size_t cchFilename = strlen(pszFilename);
208 int rc = VERR_NO_MEMORY;
209 PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)RTMemAlloc(sizeof(*pFileReader) + cchFilename);
210 if (pFileReader)
211 {
212 memcpy(pFileReader->szFilename, pszFilename, cchFilename + 1);
213 rc = RTFileOpen(&pFileReader->File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
214 if (RT_SUCCESS(rc))
215 {
216 rc = RTFileGetSize(pFileReader->File, (uint64_t *)&pFileReader->cbFile);
217 if (RT_SUCCESS(rc))
218 {
219 pFileReader->Core.pfnRead = rtldrFileRead;
220 pFileReader->Core.pfnTell = rtldrFileTell;
221 pFileReader->Core.pfnSize = rtldrFileSize;
222 pFileReader->Core.pfnLogName = rtldrFileLogName;
223 pFileReader->Core.pfnMap = rtldrFileMap;
224 pFileReader->Core.pfnUnmap = rtldrFileUnmap;
225 pFileReader->Core.pfnDestroy = rtldrFileDestroy;
226 pFileReader->off = 0;
227 pFileReader->cMappings = 0;
228 pFileReader->pvMapping = NULL;
229 *ppReader = &pFileReader->Core;
230 return VINF_SUCCESS;
231 }
232 RTFileClose(pFileReader->File);
233 }
234 RTMemFree(pFileReader);
235 }
236 *ppReader = NULL;
237 return rc;
238}
239
240
241/**
242 * Open a binary image file, extended version.
243 *
244 * @returns iprt status code.
245 * @param pszFilename Image filename.
246 * @param fFlags Reserved, MBZ.
247 * @param enmArch CPU architecture specifier for the image to be loaded.
248 * @param phLdrMod Where to store the handle to the loader module.
249 */
250RTDECL(int) RTLdrOpen(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
251{
252 LogFlow(("RTLdrOpen: pszFilename=%p:{%s} fFlags=%#x enmArch=%d phLdrMod=%p\n",
253 pszFilename, pszFilename, fFlags, enmArch, phLdrMod));
254 AssertMsgReturn(!fFlags, ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
255 AssertMsgReturn(enmArch > RTLDRARCH_INVALID && enmArch < RTLDRARCH_END, ("%d\n", enmArch), VERR_INVALID_PARAMETER);
256
257 /*
258 * Resolve RTLDRARCH_HOST.
259 */
260 if (enmArch == RTLDRARCH_HOST)
261#if defined(RT_ARCH_AMD64)
262 enmArch = RTLDRARCH_AMD64;
263#elif defined(RT_ARCH_X86)
264 enmArch = RTLDRARCH_X86_32;
265#else
266 enmArch = RTLDRARCH_WHATEVER;
267#endif
268
269 /*
270 * Create file reader & invoke worker which identifies and calls the image interpreter.
271 */
272 PRTLDRREADER pReader;
273 int rc = rtldrFileCreate(&pReader, pszFilename);
274 if (RT_SUCCESS(rc))
275 {
276 rc = rtldrOpenWithReader(pReader, fFlags, enmArch, phLdrMod);
277 if (RT_SUCCESS(rc))
278 {
279 LogFlow(("RTLdrOpen: return %Rrc *phLdrMod\n", rc, *phLdrMod));
280 return rc;
281 }
282 pReader->pfnDestroy(pReader);
283 }
284 *phLdrMod = NIL_RTLDRMOD;
285 LogFlow(("RTLdrOpen: return %Rrc\n", rc));
286 return rc;
287}
288RT_EXPORT_SYMBOL(RTLdrOpen);
289
290
291/**
292 * Opens a binary image file using kLdr.
293 *
294 * @returns iprt status code.
295 * @param pszFilename Image filename.
296 * @param fFlags Reserved, MBZ.
297 * @param enmArch CPU architecture specifier for the image to be loaded.
298 * @param phLdrMod Where to store the handle to the loaded module.
299 * @remark Primarily for testing the loader.
300 */
301RTDECL(int) RTLdrOpenkLdr(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
302{
303#ifdef LDR_WITH_KLDR
304 LogFlow(("RTLdrOpenkLdr: pszFilename=%p:{%s} fFlags=%#x enmArch=%d phLdrMod=%p\n",
305 pszFilename, pszFilename, fFlags, enmArch, phLdrMod));
306
307 /*
308 * Resolve RTLDRARCH_HOST.
309 */
310 if (enmArch == RTLDRARCH_HOST)
311# if defined(RT_ARCH_AMD64)
312 enmArch = RTLDRARCH_AMD64;
313# elif defined(RT_ARCH_X86)
314 enmArch = RTLDRARCH_X86_32;
315# else
316 enmArch = RTLDRARCH_WHATEVER;
317# endif
318
319 /*
320 * Create file reader & invoke worker which identifies and calls the image interpreter.
321 */
322 PRTLDRREADER pReader;
323 int rc = rtldrFileCreate(&pReader, pszFilename);
324 if (RT_SUCCESS(rc))
325 {
326 rc = rtldrkLdrOpen(pReader, fFlags, enmArch, phLdrMod);
327 if (RT_SUCCESS(rc))
328 {
329 LogFlow(("RTLdrOpenkLdr: return %Rrc *phLdrMod\n", rc, *phLdrMod));
330 return rc;
331 }
332 pReader->pfnDestroy(pReader);
333 }
334 *phLdrMod = NIL_RTLDRMOD;
335 LogFlow(("RTLdrOpenkLdr: return %Rrc\n", rc));
336 return rc;
337
338#else
339 return RTLdrOpen(pszFilename, fFlags, enmArch, phLdrMod);
340#endif
341}
342RT_EXPORT_SYMBOL(RTLdrOpenkLdr);
343
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