summaryrefslogtreecommitdiffstats
path: root/src/bldprogs/preload.cpp
blob: 5285047c68ecc4c0e9388534f43acc2a443d9c22 (plain)
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
/* $Id: preload.cpp $ */
/** @file
 * bin2c - Binary 2 C Structure Converter.
 */

/*
 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#ifdef RT_OS_WINDOWS
# include <Windows.h>
#else
# include <sys/mman.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <unistd.h>
# include <errno.h>
#endif
#include <stdio.h>
#include <string.h>


static int load(const char *pszImage)
{
#ifdef RT_OS_WINDOWS
    HANDLE hFile = CreateFile(pszImage,
                              GENERIC_READ,
                              FILE_SHARE_READ,
                              NULL /*pSecurityAttributes*/,
                              OPEN_EXISTING,
                              FILE_ATTRIBUTE_NORMAL,
                              NULL /*hTemplateFile*/);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        printf("error: CreateFile('%s',): %d\n", pszImage, GetLastError());
        return 1;
    }

    DWORD cbHigh  = 0;
    DWORD cbLow   = GetFileSize(hFile, &cbHigh);
    size_t cbFile = cbLow != INVALID_FILE_SIZE
                  ? cbHigh == 0
                  ? cbLow
                  : ~(DWORD)0 / 4
                  : 64;

    HANDLE hMap = CreateFileMapping(hFile,
                                    NULL /*pAttributes*/,
                                    PAGE_READONLY | SEC_COMMIT,
                                    0 /*dwMaximumSizeHigh -> file size*/,
                                    0 /*dwMaximumSizeLow  -> file size*/,
                                    NULL /*pName*/);
    if (hMap == INVALID_HANDLE_VALUE)
        printf("error: CreateFile('%s',): %d\n", pszImage, GetLastError());
        CloseHandle(hFile);
    if (hMap == INVALID_HANDLE_VALUE)
        return 1;

    void *pvWhere = MapViewOfFile(hMap,
                                  FILE_MAP_READ,
                                  0 /*dwFileOffsetHigh*/,
                                  0 /*dwFileOffsetLow*/,
                                  0 /*dwNumberOfBytesToMap - file size */);
    if (!pvWhere)
    {
        printf("error: MapViewOfView('%s',): %d\n", pszImage, GetLastError());
        CloseHandle(hMap);
        return 1;
    }

#else
    int fd = open(pszImage, O_RDONLY, 0);
    if (fd < 0)
    {
        printf("error: open('%s',): %d\n", pszImage, errno);
        return 1;
    }

    struct stat st;
    memset(&st, 0, sizeof(st));
    if (fstat(fd, &st))
        st.st_size = 64;
    size_t cbFile = st.st_size < ~(size_t)0
                  ? (size_t)st.st_size
                  : ~(size_t)0 / 4;

    void *pvWhere = mmap(NULL /*addr*/, cbFile, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0 /*offset*/);
    if (pvWhere == MAP_FAILED)
        printf("error: mmap(,%lu,)/'%s': %d\n", (unsigned long)cbFile, pszImage, errno);
    close(fd);
    if (pvWhere == MAP_FAILED)
        return 1;

#endif

    /* Touch the whole image... do a dummy crc to keep the optimizer from begin
       smart with us. */
    unsigned char  *puchFile = (unsigned char *)pvWhere;
    size_t          off      = 0;
    unsigned int    uCrc     = 0;
    while (off < cbFile)
        uCrc += puchFile[off++];
    printf("info: %p/%#lx/%#x - %s\n", pvWhere, (unsigned long)cbFile, (unsigned char)uCrc, pszImage);

    return 0;
}

static int usage(const char *argv0)
{
    printf("Generic executable image preloader.\n"
           "Usage: %s [dll|exe|file []]\n", argv0);
    return 1;
}

int main(int argc, char **argv)
{
    /*
     * Check for options.
     */
    for (int i = 1; i < argc; i++)
    {
        if (argv[i][0] == '-')
        {
            if (   argv[i][1] == '-'
                && argv[i][2] == '\0')
                break;
            if (   !strcmp(argv[i], "--help")
                || !strcmp(argv[i], "-help")
                || !strcmp(argv[i], "-h")
                || !strcmp(argv[i], "-?"))
            {
                usage(argv[0]);
                return 1;
            }
            if (   !strcmp(argv[i], "--version")
                || !strcmp(argv[i], "-V"))
            {
                printf("$Revision: 155244 $\n");
                return 0;
            }
            fprintf(stderr, "syntax error: unknown option '%s'\n", argv[i]);
            return 1;
        }
    }
    if (argc <= 1)
        return usage(argv[0]);

    /*
     * Do the loading.
     */
    for (int i = 1; i < argc; i++)
    {
        if (!strcmp(argv[i], "--"))
            continue;
        if (argv[i][0] == '@')
        {
            FILE *pFile = fopen(&argv[i][1], "r");
            if (pFile)
            {
                char szLine[4096];
                while (fgets(szLine, sizeof(szLine), pFile))
                {
                    char *psz = szLine;
                    while (*psz == ' ' || *psz == '\t')
                        psz++;
                    size_t off = strlen(psz);
                    while (     off > 0
                           &&   (   psz[off - 1] == ' '
                                 || psz[off - 1] == '\t'
                                 || psz[off - 1] == '\n'
                                 || psz[off - 1] == '\r')
                          )
                        psz[--off] = '\0';

                    if (*psz && *psz != '#')
                        load(psz);
                }
                fclose(pFile);
            }
            else
                fprintf(stderr, "error: fopen('%s','r'): %d\n", &argv[i][1], errno);
        }
        else
            load(argv[i]);
    }

    /*
     * Sleep for ever.
     */
    for (;;)
    {
#ifdef RT_OS_WINDOWS
        Sleep(3600*1000);
#else
        sleep(3600);
#endif
    }

    return 0;
}