VirtualBox

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

Last change on this file since 49930 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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