1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
/* $Id: ldrFile.cpp $ */
/** @file
* IPRT - Binary Image Loader, The File Oriented Parts.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_LDR
#include <iprt/ldr.h>
#include "internal/iprt.h"
#include <iprt/alloc.h>
#include <iprt/file.h>
#include <iprt/assert.h>
#include <iprt/log.h>
#include <iprt/err.h>
#include <iprt/string.h>
#include <iprt/formats/mz.h>
#include "internal/ldr.h"
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/**
* File Reader instance.
* This provides raw image bits from a file.
*/
typedef struct RTLDRREADERFILE
{
/** The core. */
RTLDRREADER Core;
/** The file. */
RTFILE hFile;
/** The file size. */
RTFOFF cbFile;
/** The current offset. */
RTFOFF off;
/** Number of users or the mapping. */
RTUINT cMappings;
/** Pointer to the in memory mapping. */
void *pvMapping;
/** The filename (variable size). */
char szFilename[1];
} RTLDRREADERFILE, *PRTLDRREADERFILE;
/** @copydoc RTLDRREADER::pfnRead */
static DECLCALLBACK(int) rtldrFileRead(PRTLDRREADER pReader, void *pvBuf, size_t cb, RTFOFF off)
{
PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
/*
* Seek.
*/
if (pFileReader->off != off)
{
int rc = RTFileSeek(pFileReader->hFile, off, RTFILE_SEEK_BEGIN, NULL);
if (RT_FAILURE(rc))
{
pFileReader->off = -1;
return rc;
}
pFileReader->off = off;
}
/*
* Read.
*/
int rc = RTFileRead(pFileReader->hFile, pvBuf, cb, NULL);
if (RT_SUCCESS(rc))
pFileReader->off += cb;
else
pFileReader->off = -1;
return rc;
}
/** @copydoc RTLDRREADER::pfnTell */
static DECLCALLBACK(RTFOFF) rtldrFileTell(PRTLDRREADER pReader)
{
PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
return pFileReader->off;
}
/** @copydoc RTLDRREADER::pfnSize */
static DECLCALLBACK(RTFOFF) rtldrFileSize(PRTLDRREADER pReader)
{
PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
return pFileReader->cbFile;
}
/** @copydoc RTLDRREADER::pfnLogName */
static DECLCALLBACK(const char *) rtldrFileLogName(PRTLDRREADER pReader)
{
PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
return pFileReader->szFilename;
}
/** @copydoc RTLDRREADER::pfnMap */
static DECLCALLBACK(int) rtldrFileMap(PRTLDRREADER pReader, const void **ppvBits)
{
PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
/*
* Already mapped?
*/
if (pFileReader->pvMapping)
{
pFileReader->cMappings++;
*ppvBits = pFileReader->pvMapping;
return VINF_SUCCESS;
}
/*
* Allocate memory.
*/
size_t cb = (size_t)pFileReader->cbFile;
if ((RTFOFF)cb != pFileReader->cbFile)
return VERR_IMAGE_TOO_BIG;
pFileReader->pvMapping = RTMemAlloc(cb);
if (!pFileReader->pvMapping)
return VERR_NO_MEMORY;
int rc = rtldrFileRead(pReader, pFileReader->pvMapping, cb, 0);
if (RT_SUCCESS(rc))
{
pFileReader->cMappings = 1;
*ppvBits = pFileReader->pvMapping;
}
else
{
RTMemFree(pFileReader->pvMapping);
pFileReader->pvMapping = NULL;
}
return rc;
}
/** @copydoc RTLDRREADER::pfnUnmap */
static DECLCALLBACK(int) rtldrFileUnmap(PRTLDRREADER pReader, const void *pvBits)
{
PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
AssertReturn(pFileReader->cMappings > 0, VERR_INVALID_PARAMETER);
if (!--pFileReader->cMappings)
{
RTMemFree(pFileReader->pvMapping);
pFileReader->pvMapping = NULL;
}
NOREF(pvBits);
return VINF_SUCCESS;
}
/** @copydoc RTLDRREADER::pfnDestroy */
static DECLCALLBACK(int) rtldrFileDestroy(PRTLDRREADER pReader)
{
int rc = VINF_SUCCESS;
PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)pReader;
if (pFileReader->hFile != NIL_RTFILE)
{
rc = RTFileClose(pFileReader->hFile);
AssertRC(rc);
pFileReader->hFile = NIL_RTFILE;
}
RTMemFree(pFileReader);
return rc;
}
/**
* Opens a loader file reader.
*
* @returns iprt status code.
* @param ppReader Where to store the reader instance on success.
* @param pszFilename The file to open.
*/
static int rtldrFileCreate(PRTLDRREADER *ppReader, const char *pszFilename)
{
size_t cchFilename = strlen(pszFilename);
int rc = VERR_NO_MEMORY;
PRTLDRREADERFILE pFileReader = (PRTLDRREADERFILE)RTMemAlloc(sizeof(*pFileReader) + cchFilename);
if (pFileReader)
{
memcpy(pFileReader->szFilename, pszFilename, cchFilename + 1);
rc = RTFileOpen(&pFileReader->hFile, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_WRITE);
if (RT_SUCCESS(rc))
{
rc = RTFileGetSize(pFileReader->hFile, (uint64_t *)&pFileReader->cbFile);
if (RT_SUCCESS(rc))
{
pFileReader->Core.uMagic = RTLDRREADER_MAGIC;
pFileReader->Core.pfnRead = rtldrFileRead;
pFileReader->Core.pfnTell = rtldrFileTell;
pFileReader->Core.pfnSize = rtldrFileSize;
pFileReader->Core.pfnLogName = rtldrFileLogName;
pFileReader->Core.pfnMap = rtldrFileMap;
pFileReader->Core.pfnUnmap = rtldrFileUnmap;
pFileReader->Core.pfnDestroy = rtldrFileDestroy;
pFileReader->off = 0;
pFileReader->cMappings = 0;
pFileReader->pvMapping = NULL;
*ppReader = &pFileReader->Core;
return VINF_SUCCESS;
}
RTFileClose(pFileReader->hFile);
}
RTMemFree(pFileReader);
}
*ppReader = NULL;
return rc;
}
/**
* Open a binary image file.
*
* @returns iprt status code.
* @param pszFilename Image filename.
* @param fFlags Valid RTLDR_O_XXX combination.
* @param enmArch CPU architecture specifier for the image to be loaded.
* @param phLdrMod Where to store the handle to the loader module.
*/
RTDECL(int) RTLdrOpen(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod)
{
return RTLdrOpenEx(pszFilename, fFlags, enmArch, phLdrMod, NULL /*pErrInfo*/);
}
RT_EXPORT_SYMBOL(RTLdrOpen);
/**
* Open a binary image file, extended version.
*
* @returns iprt status code.
* @param pszFilename Image filename.
* @param fFlags Valid RTLDR_O_XXX combination.
* @param enmArch CPU architecture specifier for the image to be loaded.
* @param phLdrMod Where to store the handle to the loader module.
* @param pErrInfo Where to return extended error information. Optional.
*/
RTDECL(int) RTLdrOpenEx(const char *pszFilename, uint32_t fFlags, RTLDRARCH enmArch, PRTLDRMOD phLdrMod, PRTERRINFO pErrInfo)
{
LogFlow(("RTLdrOpenEx: pszFilename=%p:{%s} fFlags=%#x enmArch=%d phLdrMod=%p\n",
pszFilename, pszFilename, fFlags, enmArch, phLdrMod));
AssertMsgReturn(!(fFlags & ~RTLDR_O_VALID_MASK), ("%#x\n", fFlags), VERR_INVALID_PARAMETER);
AssertMsgReturn(enmArch > RTLDRARCH_INVALID && enmArch < RTLDRARCH_END, ("%d\n", enmArch), VERR_INVALID_PARAMETER);
/*
* Create file reader & invoke worker which identifies and calls the image interpreter.
*/
PRTLDRREADER pReader;
int rc = rtldrFileCreate(&pReader, pszFilename);
if (RT_SUCCESS(rc))
{
rc = RTLdrOpenWithReader(pReader, fFlags, enmArch, phLdrMod, pErrInfo);
if (RT_SUCCESS(rc))
{
LogFlow(("RTLdrOpenEx: return %Rrc *phLdrMod=%p\n", rc, *phLdrMod));
return rc;
}
pReader->pfnDestroy(pReader);
}
*phLdrMod = NIL_RTLDRMOD;
LogFlow(("RTLdrOpenEx: return %Rrc\n", rc));
return rc;
}
RT_EXPORT_SYMBOL(RTLdrOpenEx);
|