VirtualBox

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

Last change on this file since 67954 was 62537, checked in by vboxsync, 8 years ago

(C) 2016

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 KB
Line 
1/* $Id: preload.cpp 62537 2016-07-22 19:32:06Z vboxsync $ */
2/** @file
3 * bin2c - Binary 2 C Structure Converter.
4 */
5
6/*
7 * Copyright (C) 2006-2016 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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#ifdef RT_OS_WINDOWS
23# include <Windows.h>
24#else
25# include <sys/mman.h>
26# include <sys/stat.h>
27# include <fcntl.h>
28# include <unistd.h>
29# include <errno.h>
30#endif
31#include <stdio.h>
32#include <string.h>
33
34
35static int load(const char *pszImage)
36{
37#ifdef RT_OS_WINDOWS
38 HANDLE hFile = CreateFile(pszImage,
39 GENERIC_READ,
40 FILE_SHARE_READ,
41 NULL /*pSecurityAttributes*/,
42 OPEN_EXISTING,
43 FILE_ATTRIBUTE_NORMAL,
44 NULL /*hTemplateFile*/);
45 if (hFile == INVALID_HANDLE_VALUE)
46 {
47 printf("error: CreateFile('%s',): %d\n", pszImage, GetLastError());
48 return 1;
49 }
50
51 DWORD cbHigh = 0;
52 DWORD cbLow = GetFileSize(hFile, &cbHigh);
53 size_t cbFile = cbLow != INVALID_FILE_SIZE
54 ? cbHigh == 0
55 ? cbLow
56 : ~(DWORD)0 / 4
57 : 64;
58
59 HANDLE hMap = CreateFileMapping(hFile,
60 NULL /*pAttributes*/,
61 PAGE_READONLY | SEC_COMMIT,
62 0 /*dwMaximumSizeHigh -> file size*/,
63 0 /*dwMaximumSizeLow -> file size*/,
64 NULL /*pName*/);
65 if (hMap == INVALID_HANDLE_VALUE)
66 printf("error: CreateFile('%s',): %d\n", pszImage, GetLastError());
67 CloseHandle(hFile);
68 if (hMap == INVALID_HANDLE_VALUE)
69 return 1;
70
71 void *pvWhere = MapViewOfFile(hMap,
72 FILE_MAP_READ,
73 0 /*dwFileOffsetHigh*/,
74 0 /*dwFileOffsetLow*/,
75 0 /*dwNumberOfBytesToMap - file size */);
76 if (!pvWhere)
77 {
78 printf("error: MapViewOfView('%s',): %d\n", pszImage, GetLastError());
79 CloseHandle(hMap);
80 return 1;
81 }
82
83#else
84 int fd = open(pszImage, O_RDONLY, 0);
85 if (fd < 0)
86 {
87 printf("error: open('%s',): %d\n", pszImage, errno);
88 return 1;
89 }
90
91 struct stat st;
92 memset(&st, 0, sizeof(st));
93 if (fstat(fd, &st))
94 st.st_size = 64;
95 size_t cbFile = st.st_size < ~(size_t)0
96 ? (size_t)st.st_size
97 : ~(size_t)0 / 4;
98
99 void *pvWhere = mmap(NULL /*addr*/, cbFile, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0 /*offset*/);
100 if (pvWhere == MAP_FAILED)
101 printf("error: mmap(,%lu,)/'%s': %d\n", (unsigned long)cbFile, pszImage, errno);
102 close(fd);
103 if (pvWhere == MAP_FAILED)
104 return 1;
105
106#endif
107
108 /* Touch the whole image... do a dummy crc to keep the optimizer from begin
109 smart with us. */
110 unsigned char *puchFile = (unsigned char *)pvWhere;
111 size_t off = 0;
112 unsigned int uCrc = 0;
113 while (off < cbFile)
114 uCrc += puchFile[off++];
115 printf("info: %p/%#lx/%#x - %s\n", pvWhere, (unsigned long)cbFile, (unsigned char)uCrc, pszImage);
116
117 return 0;
118}
119
120static int usage(const char *argv0)
121{
122 printf("Generic executable image preloader.\n"
123 "Usage: %s [dll|exe|file []]\n", argv0);
124 return 1;
125}
126
127int main(int argc, char **argv)
128{
129 /*
130 * Check for options.
131 */
132 for (int i = 1; i < argc; i++)
133 {
134 if (argv[i][0] == '-')
135 {
136 if ( argv[i][1] == '-'
137 && argv[i][2] == '\0')
138 break;
139 if ( !strcmp(argv[i], "--help")
140 || !strcmp(argv[i], "-help")
141 || !strcmp(argv[i], "-h")
142 || !strcmp(argv[i], "-?"))
143 {
144 usage(argv[0]);
145 return 1;
146 }
147 if ( !strcmp(argv[i], "--version")
148 || !strcmp(argv[i], "-V"))
149 {
150 printf("$Revision: 62537 $\n");
151 return 0;
152 }
153 fprintf(stderr, "syntax error: unknown option '%s'\n", argv[i]);
154 return 1;
155 }
156 }
157 if (argc <= 1)
158 return usage(argv[0]);
159
160 /*
161 * Do the loading.
162 */
163 for (int i = 1; i < argc; i++)
164 {
165 if (!strcmp(argv[i], "--"))
166 continue;
167 if (argv[i][0] == '@')
168 {
169 FILE *pFile = fopen(&argv[i][1], "r");
170 if (pFile)
171 {
172 char szLine[4096];
173 while (fgets(szLine, sizeof(szLine), pFile))
174 {
175 char *psz = szLine;
176 while (*psz == ' ' || *psz == '\t')
177 psz++;
178 size_t off = strlen(psz);
179 while ( off > 0
180 && ( psz[off - 1] == ' '
181 || psz[off - 1] == '\t'
182 || psz[off - 1] == '\n'
183 || psz[off - 1] == '\r')
184 )
185 psz[--off] = '\0';
186
187 if (*psz && *psz != '#')
188 load(psz);
189 }
190 fclose(pFile);
191 }
192 else
193 fprintf(stderr, "error: fopen('%s','r'): %d\n", &argv[i][1], errno);
194 }
195 else
196 load(argv[i]);
197 }
198
199 /*
200 * Sleep for ever.
201 */
202 for (;;)
203 {
204#ifdef RT_OS_WINDOWS
205 Sleep(3600*1000);
206#else
207 sleep(3600);
208#endif
209 }
210
211 return 0;
212}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use