VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/ldr/ldrVfsFile.cpp@ 76452

Last change on this file since 76452 was 74654, checked in by vboxsync, 6 years ago

IPRT: More kStuff cleanup. bugref:9232

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 10.0 KB
Line 
1/* $Id: ldrVfsFile.cpp 74654 2018-10-07 13:00:04Z vboxsync $ */
2/** @file
3 * IPRT - Binary Image Loader, The File Oriented Parts, VFS variant.
4 */
5
6/*
7 * Copyright (C) 2006-2017 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 <iprt/vfs.h>
42#include <iprt/formats/mz.h>
43#include "internal/ldr.h"
44
45
46/*********************************************************************************************************************************
47* Structures and Typedefs *
48*********************************************************************************************************************************/
49/**
50 * VFS file Reader instance.
51 * This provides raw image bits from a file.
52 */
53typedef struct RTLDRREADERVFSFILE
54{
55 /** The core. */
56 RTLDRREADER Core;
57 /** The VFS file. */
58 RTVFSFILE hVfsFile;
59 /** Number of users or the mapping. */
60 RTUINT cMappings;
61 /** Pointer to the in memory mapping. */
62 void *pvMapping;
63 /** The filename (variable size). */
64 char szFilename[1];
65} RTLDRREADERVFSFILE;
66typedef RTLDRREADERVFSFILE *PRTLDRREADERVFSFILE;
67
68
69/** @copydoc RTLDRREADER::pfnRead */
70static DECLCALLBACK(int) rtldrVfsFileRead(PRTLDRREADER pReader, void *pvBuf, size_t cb, RTFOFF off)
71{
72 PRTLDRREADERVFSFILE pFileReader = (PRTLDRREADERVFSFILE)pReader;
73 return RTVfsFileReadAt(pFileReader->hVfsFile, off, pvBuf, cb, NULL);
74}
75
76
77/** @copydoc RTLDRREADER::pfnTell */
78static DECLCALLBACK(RTFOFF) rtldrVfsFileTell(PRTLDRREADER pReader)
79{
80 PRTLDRREADERVFSFILE pFileReader = (PRTLDRREADERVFSFILE)pReader;
81 return RTVfsFileTell(pFileReader->hVfsFile);
82}
83
84
85/** @copydoc RTLDRREADER::pfnSize */
86static DECLCALLBACK(RTFOFF) rtldrVfsFileSize(PRTLDRREADER pReader)
87{
88 PRTLDRREADERVFSFILE pFileReader = (PRTLDRREADERVFSFILE)pReader;
89 uint64_t cbFile;
90 int rc = RTVfsFileGetSize(pFileReader->hVfsFile, &cbFile);
91 if (RT_SUCCESS(rc))
92 return cbFile;
93 return 0;
94}
95
96
97/** @copydoc RTLDRREADER::pfnLogName */
98static DECLCALLBACK(const char *) rtldrVfsFileLogName(PRTLDRREADER pReader)
99{
100 PRTLDRREADERVFSFILE pFileReader = (PRTLDRREADERVFSFILE)pReader;
101 return pFileReader->szFilename;
102}
103
104
105/** @copydoc RTLDRREADER::pfnMap */
106static DECLCALLBACK(int) rtldrVfsFileMap(PRTLDRREADER pReader, const void **ppvBits)
107{
108 PRTLDRREADERVFSFILE pFileReader = (PRTLDRREADERVFSFILE)pReader;
109
110 /*
111 * Already mapped?
112 */
113 if (pFileReader->pvMapping)
114 {
115 pFileReader->cMappings++;
116 *ppvBits = pFileReader->pvMapping;
117 return VINF_SUCCESS;
118 }
119
120 /*
121 * Allocate memory.
122 */
123 RTFOFF cbFile = rtldrVfsFileSize(pReader);
124 size_t cb = (size_t)cbFile;
125 if ((RTFOFF)cb != cbFile)
126 return VERR_IMAGE_TOO_BIG;
127 pFileReader->pvMapping = RTMemAlloc(cb);
128 if (!pFileReader->pvMapping)
129 return VERR_NO_MEMORY;
130 int rc = rtldrVfsFileRead(pReader, pFileReader->pvMapping, cb, 0);
131 if (RT_SUCCESS(rc))
132 {
133 pFileReader->cMappings = 1;
134 *ppvBits = pFileReader->pvMapping;
135 }
136 else
137 {
138 RTMemFree(pFileReader->pvMapping);
139 pFileReader->pvMapping = NULL;
140 }
141
142 return rc;
143}
144
145
146/** @copydoc RTLDRREADER::pfnUnmap */
147static DECLCALLBACK(int) rtldrVfsFileUnmap(PRTLDRREADER pReader, const void *pvBits)
148{
149 PRTLDRREADERVFSFILE pFileReader = (PRTLDRREADERVFSFILE)pReader;
150 AssertReturn(pFileReader->cMappings > 0, VERR_INVALID_PARAMETER);
151
152 if (!--pFileReader->cMappings)
153 {
154 RTMemFree(pFileReader->pvMapping);
155 pFileReader->pvMapping = NULL;
156 }
157
158 NOREF(pvBits);
159 return VINF_SUCCESS;
160}
161
162
163/** @copydoc RTLDRREADER::pfnDestroy */
164static DECLCALLBACK(int) rtldrVfsFileDestroy(PRTLDRREADER pReader)
165{
166 PRTLDRREADERVFSFILE pFileReader = (PRTLDRREADERVFSFILE)pReader;
167 if (pFileReader->hVfsFile != NIL_RTVFSFILE)
168 {
169 RTVfsFileRelease(pFileReader->hVfsFile);
170 pFileReader->hVfsFile = NIL_RTVFSFILE;
171 }
172 RTMemFree(pFileReader);
173 return VINF_SUCCESS;
174}
175
176
177/**
178 * Opens a loader file reader.
179 *
180 * @returns iprt status code.
181 * @param pszFilename The file to open, can be VFS chain.
182 * @param ppReader Where to store the reader instance on success.
183 * @param poffError Where to return the offset into @a pszFilename of an VFS
184 * chain element causing trouble. Optional.
185 * @param pErrInfo Where to return extended error information. Optional.
186 */
187static int rtldrVfsFileCreate(const char *pszFilename, PRTLDRREADER *ppReader, uint32_t *poffError, PRTERRINFO pErrInfo)
188{
189 size_t cbFilename = strlen(pszFilename) + 1;
190 int rc = VERR_NO_MEMORY;
191 PRTLDRREADERVFSFILE pFileReader = (PRTLDRREADERVFSFILE)RTMemAlloc(RT_UOFFSETOF_DYN(RTLDRREADERVFSFILE, szFilename[cbFilename]));
192 if (pFileReader)
193 {
194 memcpy(pFileReader->szFilename, pszFilename, cbFilename);
195 pFileReader->szFilename[0] = '\0';
196 rc = RTVfsChainOpenFile(pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE, &pFileReader->hVfsFile,
197 poffError, pErrInfo);
198 if (RT_SUCCESS(rc))
199 {
200 pFileReader->Core.uMagic = RTLDRREADER_MAGIC;
201 pFileReader->Core.pfnRead = rtldrVfsFileRead;
202 pFileReader->Core.pfnTell = rtldrVfsFileTell;
203 pFileReader->Core.pfnSize = rtldrVfsFileSize;
204 pFileReader->Core.pfnLogName = rtldrVfsFileLogName;
205 pFileReader->Core.pfnMap = rtldrVfsFileMap;
206 pFileReader->Core.pfnUnmap = rtldrVfsFileUnmap;
207 pFileReader->Core.pfnDestroy = rtldrVfsFileDestroy;
208 pFileReader->cMappings = 0;
209 pFileReader->pvMapping = NULL;
210 *ppReader = &pFileReader->Core;
211 return VINF_SUCCESS;
212 }
213 RTMemFree(pFileReader);
214 }
215 *ppReader = NULL;
216 return rc;
217}
218
219
220/**
221 * Open a binary image file allowing VFS chains in the filename.
222 *
223 * @returns iprt status code.
224 * @param pszFilename Image filename, VFS chain specifiers allowed.
225 * @param fFlags Valid RTLDR_O_XXX combination.
226 * @param enmArch CPU architecture specifier for the image to be loaded.
227 * @param phLdrMod Where to store the handle to the loader module.
228 * @param poffError Where to return the offset into @a pszFilename of an VFS
229 * chain element causing trouble. Optional.
230 * @param pErrInfo Where to return extended error information. Optional.
231 */
232RTDECL(int) RTLdrOpenVfsChain(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch,
233 PRTLDRMOD phLdrMod, uint32_t *poffError, PRTERRINFO pErrInfo)
234{
235 LogFlow(("RTLdrOpenVfsChain: pszFilename=%p:{%s} fFlags=%#x enmArch=%d phLdrMod=%p\n",
236 pszFilename, pszFilename, fFlags, enmArch, phLdrMod));
237 AssertMsgReturn(!(fFlags & ~RTLDR_O_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
238 AssertMsgReturn(enmArch > RTLDRARCH_INVALID && enmArch < RTLDRARCH_END, ("%d\n", enmArch), VERR_INVALID_PARAMETER);
239
240 /*
241 * Create file reader & invoke worker which identifies and calls the image interpreter.
242 */
243 PRTLDRREADER pReader;
244 int rc = rtldrVfsFileCreate(pszFilename, &pReader, poffError, pErrInfo);
245 if (RT_SUCCESS(rc))
246 {
247 if (poffError)
248 *poffError = 0;
249 rc = RTLdrOpenWithReader(pReader, fFlags, enmArch, phLdrMod, pErrInfo);
250 if (RT_SUCCESS(rc))
251 {
252 LogFlow(("RTLdrOpenEx: return %Rrc *phLdrMod=%p\n", rc, *phLdrMod));
253 return rc;
254 }
255 pReader->pfnDestroy(pReader);
256 }
257 *phLdrMod = NIL_RTLDRMOD;
258 LogFlow(("RTLdrOpenEx: return %Rrc\n", rc));
259 return rc;
260}
261RT_EXPORT_SYMBOL(RTLdrOpenVfsChain);
262
263
264/**
265 * Open a binary image file using kLdr allowing VFS chains in the filename.
266 *
267 * @returns iprt status code.
268 * @param pszFilename Image filename.
269 * @param fFlags Reserved, MBZ.
270 * @param enmArch CPU architecture specifier for the image to be loaded.
271 * @param phLdrMod Where to store the handle to the loaded module.
272 * @param poffError Where to return the offset into @a pszFilename of an VFS
273 * chain element causing trouble. Optional.
274 * @param pErrInfo Where to return extended error information. Optional.
275 * @remark Primarily for testing the loader.
276 */
277RTDECL(int) RTLdrOpenVfsChainkLdr(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch,
278 PRTLDRMOD phLdrMod, uint32_t *poffError, PRTERRINFO pErrInfo)
279{
280 return RTLdrOpenVfsChain(pszFilename, fFlags, enmArch, phLdrMod, poffError, pErrInfo);
281}
282RT_EXPORT_SYMBOL(RTLdrOpenVfsChainkLdr);
283
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