aboutsummaryrefslogtreecommitdiff
path: root/mkpe.c
blob: 9920895498348373ea05b50b6c56751fd4294d9b (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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

/*
    int16_t lastsize;
    int16_t nblocks;
    int16_t nreloc;
    int16_t hdrsize;
    int16_t minalloc;
    int16_t maxalloc;
    void *ss; // 2 byte value
    void *sp; // 2 byte value
    int16_t checksum;
    void *ip; // 2 byte value
    void *cs; // 2 byte value
    int16_t relocpos;
    int16_t noverlay;
    int16_t reserved1[4];
    int16_t oem_id;
    int16_t oem_info;
    int16_t reserved2[10];
*/

#define ROUND_UP(N, S) ((((N) + (S) - 1) / (S)) * (S))

struct DOS_Header {
    char signature[2];    // {'M', 'Z'}
    uint16_t  e_cblp;     // Bytes on last page of file
    uint16_t  e_cp;       // Pages in file
    uint16_t  e_crlc;     // Relocations
    uint16_t  e_cparhdr;  // Size of header in paragraphs
    uint16_t  e_minalloc; // Minimum extra paragraphs needed
    uint16_t  e_maxalloc; // Maximum extra paragraphs needed
    uint16_t  e_ss;       // Initial (relative) SS value
    uint16_t  e_sp;       // Initial SP value
    uint16_t  e_csum;     // Checksum
    uint16_t  e_ip;       // Initial IP value
    uint16_t  e_cs;       // Initial (relative) CS value
    uint16_t  e_lfarlc;   // File address of relocation table
    uint16_t  e_ovno;     // Overlay number
    uint16_t  e_res[4];   // Reserved words
    uint16_t  e_oemid;    // OEM identifier (for e_oeminfo)
    uint16_t  e_oeminfo;  // OEM information; e_oemid specific
    uint16_t  e_res2[10]; // Reserved words
    int32_t e_lfanew; // Offset to the 'PE\0\0' signature relative to the beginning of the file
} __attribute__((packed));

struct PE_OpHdr {
    uint16_t format;
    char linker_ver_maj;
    char linker_ver_min;

    uint32_t codesz;
    uint32_t bsssz;
    uint32_t datasz;
    uint32_t entrypoint;
    uint32_t codebase;

    uint64_t imagebase;
    uint32_t sectalign;
    uint32_t filealign;
    uint16_t os_ver_maj;
    uint16_t os_ver_min;
    uint16_t img_ver_maj;
    uint16_t img_ver_min;
    uint16_t subs_ver_maj;
    uint16_t subs_ver_min;
    uint32_t win32_ver;

    uint32_t imagesz;
    uint32_t hdrsz;
    uint32_t cksum;
    uint16_t subsystem;
    uint16_t dllcharacteristics;
    uint64_t stack_res;
    uint64_t stack_commit;
    uint64_t heap_res;
    uint64_t heap_commit;

    uint32_t loader_flags;
    uint32_t nr_rva_sz;
} __attribute__((packed));

struct PE_Header {
    char signature[2];
    uint16_t fill0;
    uint16_t arch;
    uint16_t nr_sections;
    uint32_t datetime;
    uint32_t symtabptr;
    uint32_t nr_symbols;
    uint16_t ophdrsz;
    uint16_t characteristics;
    struct PE_OpHdr ophdr;
} __attribute__((packed));

struct PE_SecHdr {
    char name[8];
    uint32_t virtsz;
    uint32_t vaddr;
    uint32_t rawsz;
    uint32_t dataptr;
    uint32_t relocptr;
    uint32_t linenrptr;
    uint16_t nr_reloc;
    uint16_t nr_linenr;
    uint32_t characteristics;
} __attribute__((packed));

struct PE_Reloc {
    uint32_t offset;
    uint32_t value;
    char fill[0x20-0x8];
} __attribute__((packed));

ssize_t
writeall(int fd, void *buf, size_t sz)
{
    ssize_t wr, ssz;

    ssz = (ssize_t) sz;

    while ((wr = write(fd, buf, sz)) != -1) {
        if (wr == ssz) {
            break;
        }

        buf += wr;
        ssz -= wr;
    }

    return (wr);
}

char *
readall(int fd, size_t *sz)
{
    char *data;
    size_t cap, len;
    ssize_t n;
    int err;

    *sz = 0;

    cap = BUFSIZ;
    len = 0;
    data = malloc(cap);

    if (data == NULL) {
        return (NULL);
    }

    while ((n = read(fd, data+len, BUFSIZ)) >= 0) {
        if (n == 0) {
            break;
        }

        len += n;

        if (len >= cap) {
            cap += BUFSIZ;
            data = realloc(data, cap);
        }
    }
    if (n == -1) {
        err = errno;
        free(data);
        errno = err;
        return (NULL);
    }

    *sz = len;

    return (data);
}

int
main(int argc, char **argv)
{
    struct DOS_Header dhdr;
    struct PE_Header phdr;
    struct PE_SecHdr sect;
    struct PE_Reloc relo;
    uint32_t hdrsz;
    int ifd, ofd, err;
    char *code;
    size_t codesz;
    void *zero;

    assert(offsetof(struct DOS_Header, e_lfanew) == 0x3c);

    if (argc != 3) {
        fprintf(stderr, "Usage: %s INPUT.BIN OUTPUT.BIN\n", argv[0]);
        return (1);
    }

    ifd = open(argv[1], O_RDONLY);
    if (ifd < 0) {
        err = errno;
        perror(argv[1]);
        return (err);
    }

    ofd = open(argv[2], O_WRONLY|O_CREAT|O_TRUNC,
               S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);

    if (ofd < 0) {
        err = errno;
        perror(argv[2]);
        close(ifd);
        return (err);
    }

    code = readall(ifd, &codesz);
    if (code == NULL) {
        err = errno;
        perror("readall");
        close(ifd);
        close(ofd);
        return (err);
    }

    memset(&dhdr, 0, sizeof(struct DOS_Header));
    memset(&phdr, 0, sizeof(struct PE_Header));
    memset(&sect, 0, sizeof(struct PE_SecHdr));
    memset(&relo, 0, sizeof(struct PE_Reloc));

    dhdr.signature[0] = 'M';
    dhdr.signature[1] = 'Z';
    dhdr.e_lfanew = sizeof(struct DOS_Header);

    writeall(ofd, &dhdr, sizeof(struct DOS_Header));

    phdr.signature[0] = 'P';
    phdr.signature[1] = 'E';
    phdr.arch = 0x8664; /* x86_64 */
    phdr.nr_sections = 2; /* text, reloc */
    phdr.nr_symbols = 1;
    phdr.ophdrsz = sizeof(struct PE_OpHdr);
    printf("phdr = %zu, ophdr = %zu\n", sizeof(struct PE_Header), sizeof(struct PE_OpHdr));
    phdr.characteristics = 0x206;

    phdr.ophdr.format = 0x20b; /* PE32+ (x64) */
    phdr.ophdr.linker_ver_maj = 0x02;
    phdr.ophdr.linker_ver_min = 0x14;
    phdr.ophdr.codebase = 0x200;
    phdr.ophdr.sectalign = 0x20;
    assert(phdr.ophdr.sectalign == sizeof(struct PE_Reloc));
    phdr.ophdr.filealign = 0x20;
    phdr.ophdr.subsystem = 0xa; /* EFI Application */
    phdr.ophdr.nr_rva_sz = 0x0;

    assert(codesz <= (uint32_t)(~0));
    phdr.ophdr.codesz = (uint32_t) codesz;
    printf("codesz = %u\n", phdr.ophdr.codesz);

    hdrsz = sizeof(struct DOS_Header)
        + sizeof(struct PE_Header)
        + phdr.nr_sections * sizeof(struct PE_SecHdr);
    printf("hdrsz = %u\n", hdrsz);
    /* round up to multiple of filealign */
    phdr.ophdr.hdrsz = ROUND_UP(hdrsz, phdr.ophdr.filealign);;
    assert(phdr.ophdr.hdrsz % phdr.ophdr.filealign == 0);
    printf("hdrsz = %u (aligned)\n", phdr.ophdr.hdrsz);

    phdr.ophdr.entrypoint = phdr.ophdr.hdrsz;

    phdr.ophdr.imagesz = phdr.ophdr.hdrsz + codesz + phdr.ophdr.sectalign;
    printf("imagesz = %u\n", phdr.ophdr.imagesz);
    assert(phdr.ophdr.imagesz % phdr.ophdr.sectalign == 0);

    writeall(ofd, &phdr, sizeof(struct PE_Header));

    strcpy((char *)&sect.name, ".reloc");
    /* READ|DISCARDABLE|INITIALIZED */
    sect.characteristics = 0x42000040;
    sect.virtsz = phdr.ophdr.sectalign;
    sect.vaddr = phdr.ophdr.hdrsz + codesz;
    sect.rawsz = phdr.ophdr.sectalign;
    sect.dataptr = phdr.ophdr.hdrsz + codesz;

    relo.offset = sect.dataptr;
    relo.value = 10; /* arbitrary number */

    writeall(ofd, &sect, sizeof(struct PE_SecHdr));

    memset(&sect, 0, sizeof(struct PE_SecHdr));
    strcpy((char *)&sect.name, ".text");
    /* READ|EXEC|ALIGN_16BYTES|CODE */
    sect.characteristics = 0x60500020;
    sect.virtsz = ROUND_UP(codesz, phdr.ophdr.sectalign);
    sect.vaddr = phdr.ophdr.hdrsz;
    sect.rawsz = codesz;
    sect.dataptr = phdr.ophdr.hdrsz;

    writeall(ofd, &sect, sizeof(struct PE_SecHdr));

    if (phdr.ophdr.hdrsz - hdrsz) {
        zero = calloc(1, phdr.ophdr.hdrsz - hdrsz);
        writeall(ofd, zero, phdr.ophdr.hdrsz - hdrsz);
    }

    writeall(ofd, code, codesz);
    writeall(ofd, &relo, sizeof(struct PE_Reloc));

    close(ofd);
    close(ifd);
    free(code);

    return (0);
}