VirtualBox

source: vbox/trunk/src/bldprogs/preload.cpp@ 106159

Last change on this file since 106159 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
1/* $Id: preload.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * bin2c - Binary 2 C Structure Converter.
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#ifdef RT_OS_WINDOWS
33# include <Windows.h>
34#else
35# include <sys/mman.h>
36# include <sys/stat.h>
37# include <fcntl.h>
38# include <unistd.h>
39# include <errno.h>
40#endif
41#include <stdio.h>
42#include <string.h>
43
44
45static int load(const char *pszImage)
46{
47#ifdef RT_OS_WINDOWS
48 HANDLE hFile = CreateFile(pszImage,
49 GENERIC_READ,
50 FILE_SHARE_READ,
51 NULL /*pSecurityAttributes*/,
52 OPEN_EXISTING,
53 FILE_ATTRIBUTE_NORMAL,
54 NULL /*hTemplateFile*/);
55 if (hFile == INVALID_HANDLE_VALUE)
56 {
57 printf("error: CreateFile('%s',): %d\n", pszImage, GetLastError());
58 return 1;
59 }
60
61 DWORD cbHigh = 0;
62 DWORD cbLow = GetFileSize(hFile, &cbHigh);
63 size_t cbFile = cbLow != INVALID_FILE_SIZE
64 ? cbHigh == 0
65 ? cbLow
66 : ~(DWORD)0 / 4
67 : 64;
68
69 HANDLE hMap = CreateFileMapping(hFile,
70 NULL /*pAttributes*/,
71 PAGE_READONLY | SEC_COMMIT,
72 0 /*dwMaximumSizeHigh -> file size*/,
73 0 /*dwMaximumSizeLow -> file size*/,
74 NULL /*pName*/);
75 if (hMap == INVALID_HANDLE_VALUE)
76 printf("error: CreateFile('%s',): %d\n", pszImage, GetLastError());
77 CloseHandle(hFile);
78 if (hMap == INVALID_HANDLE_VALUE)
79 return 1;
80
81 void *pvWhere = MapViewOfFile(hMap,
82 FILE_MAP_READ,
83 0 /*dwFileOffsetHigh*/,
84 0 /*dwFileOffsetLow*/,
85 0 /*dwNumberOfBytesToMap - file size */);
86 if (!pvWhere)
87 {
88 printf("error: MapViewOfView('%s',): %d\n", pszImage, GetLastError());
89 CloseHandle(hMap);
90 return 1;
91 }
92
93#else
94 int fd = open(pszImage, O_RDONLY, 0);
95 if (fd < 0)
96 {
97 printf("error: open('%s',): %d\n", pszImage, errno);
98 return 1;
99 }
100
101 struct stat st;
102 memset(&st, 0, sizeof(st));
103 if (fstat(fd, &st))
104 st.st_size = 64;
105 size_t cbFile = st.st_size < ~(size_t)0
106 ? (size_t)st.st_size
107 : ~(size_t)0 / 4;
108
109 void *pvWhere = mmap(NULL /*addr*/, cbFile, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0 /*offset*/);
110 if (pvWhere == MAP_FAILED)
111 printf("error: mmap(,%lu,)/'%s': %d\n", (unsigned long)cbFile, pszImage, errno);
112 close(fd);
113 if (pvWhere == MAP_FAILED)
114 return 1;
115
116#endif
117
118 /* Touch the whole image... do a dummy crc to keep the optimizer from begin
119 smart with us. */
120 unsigned char *puchFile = (unsigned char *)pvWhere;
121 size_t off = 0;
122 unsigned int uCrc = 0;
123 while (off < cbFile)
124 uCrc += puchFile[off++];
125 printf("info: %p/%#lx/%#x - %s\n", pvWhere, (unsigned long)cbFile, (unsigned char)uCrc, pszImage);
126
127 return 0;
128}
129
130static int usage(const char *argv0)
131{
132 printf("Generic executable image preloader.\n"
133 "Usage: %s [dll|exe|file []]\n", argv0);
134 return 1;
135}
136
137int main(int argc, char **argv)
138{
139 /*
140 * Check for options.
141 */
142 for (int i = 1; i < argc; i++)
143 {
144 if (argv[i][0] == '-')
145 {
146 if ( argv[i][1] == '-'
147 && argv[i][2] == '\0')
148 break;
149 if ( !strcmp(argv[i], "--help")
150 || !strcmp(argv[i], "-help")
151 || !strcmp(argv[i], "-h")
152 || !strcmp(argv[i], "-?"))
153 {
154 usage(argv[0]);
155 return 1;
156 }
157 if ( !strcmp(argv[i], "--version")
158 || !strcmp(argv[i], "-V"))
159 {
160 printf("$Revision: 106061 $\n");
161 return 0;
162 }
163 fprintf(stderr, "syntax error: unknown option '%s'\n", argv[i]);
164 return 1;
165 }
166 }
167 if (argc <= 1)
168 return usage(argv[0]);
169
170 /*
171 * Do the loading.
172 */
173 for (int i = 1; i < argc; i++)
174 {
175 if (!strcmp(argv[i], "--"))
176 continue;
177 if (argv[i][0] == '@')
178 {
179 FILE *pFile = fopen(&argv[i][1], "r");
180 if (pFile)
181 {
182 char szLine[4096];
183 while (fgets(szLine, sizeof(szLine), pFile))
184 {
185 char *psz = szLine;
186 while (*psz == ' ' || *psz == '\t')
187 psz++;
188 size_t off = strlen(psz);
189 while ( off > 0
190 && ( psz[off - 1] == ' '
191 || psz[off - 1] == '\t'
192 || psz[off - 1] == '\n'
193 || psz[off - 1] == '\r')
194 )
195 psz[--off] = '\0';
196
197 if (*psz && *psz != '#')
198 load(psz);
199 }
200 fclose(pFile);
201 }
202 else
203 fprintf(stderr, "error: fopen('%s','r'): %d\n", &argv[i][1], errno);
204 }
205 else
206 load(argv[i]);
207 }
208
209 /*
210 * Sleep for ever.
211 */
212 for (;;)
213 {
214#ifdef RT_OS_WINDOWS
215 Sleep(3600*1000);
216#else
217 sleep(3600);
218#endif
219 }
220
221 return 0;
222}
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