1 | /* $Id: ldrFile.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Binary Image Loader, The File Oriented Parts.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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/formats/mz.h>
|
---|
42 | #include "internal/ldr.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | /**
|
---|
49 | * File Reader instance.
|
---|
50 | * This provides raw image bits from a file.
|
---|
51 | */
|
---|
52 | typedef struct RTLDRREADERFILE
|
---|
53 | {
|
---|
54 | /** The core. */
|
---|
55 | RTLDRREADER Core;
|
---|
56 | /** The file. */
|
---|
57 | RTFILE hFile;
|
---|
58 | /** The file size. */
|
---|
59 | uint64_t 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 */
|
---|
72 | static 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 */
|
---|
103 | static DECLCALLBACK(RTFOFF) rtldrFileTell(PRTLDRREADER pReader)
|
---|
104 | {
|
---|
105 | PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
|
---|
106 | return pFileReader->off;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /** @copydoc RTLDRREADER::pfnSize */
|
---|
111 | static DECLCALLBACK(uint64_t) rtldrFileSize(PRTLDRREADER pReader)
|
---|
112 | {
|
---|
113 | PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
|
---|
114 | return pFileReader->cbFile;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /** @copydoc RTLDRREADER::pfnLogName */
|
---|
119 | static DECLCALLBACK(const char *) rtldrFileLogName(PRTLDRREADER pReader)
|
---|
120 | {
|
---|
121 | PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
|
---|
122 | return pFileReader->szFilename;
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /** @copydoc RTLDRREADER::pfnMap */
|
---|
127 | static 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 ((uint64_t)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 */
|
---|
167 | static 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 */
|
---|
184 | static DECLCALLBACK(int) rtldrFileDestroy(PRTLDRREADER pReader)
|
---|
185 | {
|
---|
186 | int rc = VINF_SUCCESS;
|
---|
187 | PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
|
---|
188 |
|
---|
189 | Assert(!pFileReader->cMappings);
|
---|
190 |
|
---|
191 | if (pFileReader->hFile != NIL_RTFILE)
|
---|
192 | {
|
---|
193 | rc = RTFileClose(pFileReader->hFile);
|
---|
194 | AssertRC(rc);
|
---|
195 | pFileReader->hFile = NIL_RTFILE;
|
---|
196 | }
|
---|
197 |
|
---|
198 | if (pFileReader->pvMapping)
|
---|
199 | {
|
---|
200 | RTMemFree(pFileReader->pvMapping);
|
---|
201 | pFileReader->pvMapping = NULL;
|
---|
202 | }
|
---|
203 |
|
---|
204 | RTMemFree(pFileReader);
|
---|
205 | return rc;
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Opens a loader file reader.
|
---|
211 | *
|
---|
212 | * @returns iprt status code.
|
---|
213 | * @param ppReader Where to store the reader instance on success.
|
---|
214 | * @param pszFilename The file to open.
|
---|
215 | */
|
---|
216 | static int rtldrFileCreate(PRTLDRREADER *ppReader, const char *pszFilename)
|
---|
217 | {
|
---|
218 | size_t cchFilename = strlen(pszFilename);
|
---|
219 | int rc = VERR_NO_MEMORY;
|
---|
220 | PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)RTMemAlloc(sizeof(*pFileReader) + cchFilename);
|
---|
221 | if (pFileReader)
|
---|
222 | {
|
---|
223 | memcpy(pFileReader->szFilename, pszFilename, cchFilename + 1);
|
---|
224 | rc = RTFileOpen(&pFileReader->hFile, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
|
---|
225 | if (RT_SUCCESS(rc))
|
---|
226 | {
|
---|
227 | rc = RTFileQuerySize(pFileReader->hFile, &pFileReader->cbFile);
|
---|
228 | if (RT_SUCCESS(rc))
|
---|
229 | {
|
---|
230 | pFileReader->Core.uMagic = RTLDRREADER_MAGIC;
|
---|
231 | pFileReader->Core.pfnRead = rtldrFileRead;
|
---|
232 | pFileReader->Core.pfnTell = rtldrFileTell;
|
---|
233 | pFileReader->Core.pfnSize = rtldrFileSize;
|
---|
234 | pFileReader->Core.pfnLogName = rtldrFileLogName;
|
---|
235 | pFileReader->Core.pfnMap = rtldrFileMap;
|
---|
236 | pFileReader->Core.pfnUnmap = rtldrFileUnmap;
|
---|
237 | pFileReader->Core.pfnDestroy = rtldrFileDestroy;
|
---|
238 | pFileReader->off = 0;
|
---|
239 | pFileReader->cMappings = 0;
|
---|
240 | pFileReader->pvMapping = NULL;
|
---|
241 | *ppReader = &pFileReader->Core;
|
---|
242 | return VINF_SUCCESS;
|
---|
243 | }
|
---|
244 |
|
---|
245 | RTFileClose(pFileReader->hFile);
|
---|
246 | }
|
---|
247 | RTMemFree(pFileReader);
|
---|
248 | }
|
---|
249 | *ppReader = NULL;
|
---|
250 | return rc;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Open a binary image file.
|
---|
256 | *
|
---|
257 | * @returns iprt status code.
|
---|
258 | * @param pszFilename Image filename.
|
---|
259 | * @param fFlags Valid RTLDR_O_XXX combination.
|
---|
260 | * @param enmArch CPU architecture specifier for the image to be loaded.
|
---|
261 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
262 | */
|
---|
263 | RTDECL(int) RTLdrOpen(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
|
---|
264 | {
|
---|
265 | return RTLdrOpenEx(pszFilename, fFlags, enmArch, phLdrMod, NULL /*pErrInfo*/);
|
---|
266 | }
|
---|
267 | RT_EXPORT_SYMBOL(RTLdrOpen);
|
---|
268 |
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * Open a binary image file, extended version.
|
---|
272 | *
|
---|
273 | * @returns iprt status code.
|
---|
274 | * @param pszFilename Image filename.
|
---|
275 | * @param fFlags Valid RTLDR_O_XXX combination.
|
---|
276 | * @param enmArch CPU architecture specifier for the image to be loaded.
|
---|
277 | * @param phLdrMod Where to store the handle to the loader module.
|
---|
278 | * @param pErrInfo Where to return extended error information. Optional.
|
---|
279 | */
|
---|
280 | RTDECL(int) RTLdrOpenEx(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
|
---|
281 | {
|
---|
282 | LogFlow(("RTLdrOpenEx: pszFilename=%p:{%s} fFlags=%#x enmArch=%d phLdrMod=%p\n",
|
---|
283 | pszFilename, pszFilename, fFlags, enmArch, phLdrMod));
|
---|
284 | AssertMsgReturn(!(fFlags & ~RTLDR_O_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
|
---|
285 | AssertMsgReturn(enmArch > RTLDRARCH_INVALID && enmArch < RTLDRARCH_END, ("%d\n", enmArch), VERR_INVALID_PARAMETER);
|
---|
286 |
|
---|
287 | /*
|
---|
288 | * Create file reader & invoke worker which identifies and calls the image interpreter.
|
---|
289 | */
|
---|
290 | PRTLDRREADER pReader;
|
---|
291 | int rc = rtldrFileCreate(&pReader, pszFilename);
|
---|
292 | if (RT_SUCCESS(rc))
|
---|
293 | {
|
---|
294 | rc = RTLdrOpenWithReader(pReader, fFlags, enmArch, phLdrMod, pErrInfo);
|
---|
295 | if (RT_SUCCESS(rc))
|
---|
296 | {
|
---|
297 | LogFlow(("RTLdrOpenEx: return %Rrc *phLdrMod=%p\n", rc, *phLdrMod));
|
---|
298 | return rc;
|
---|
299 | }
|
---|
300 | pReader->pfnDestroy(pReader);
|
---|
301 | }
|
---|
302 | *phLdrMod = NIL_RTLDRMOD;
|
---|
303 | LogFlow(("RTLdrOpenEx: return %Rrc\n", rc));
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 | RT_EXPORT_SYMBOL(RTLdrOpenEx);
|
---|
307 |
|
---|