-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbgsplice.c
302 lines (253 loc) · 6.94 KB
/
dbgsplice.c
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
/* Time-stamp: <2016-06-08 01:43:13 andreiw>
* Copyright (C) 2012 Andrei Warkentin <andrey.warkentin@gmail.com>
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define _DEFAULT_SOURCE
#include <endian.h>
#include <stdio.h>
#include <inttypes.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include "pe32.h"
typedef struct f_state {
int fd;
struct stat st;
void *d;
} f_state;
int validate_off(const f_state *f,
off_t off)
{
if (off < f->st.st_size) {
return 0;
}
return -1;
}
void *off_to_ptr(void *ptr,
off_t off)
{
return (void *)(((uintptr_t) ptr) + off);
}
#define validate_ele(f, ptr, size) \
(validate_off(f, ((void *) ptr - (f)->d)) | \
validate_off(f, ((void *) ptr - (f)->d) + (size - 1)))
int parse_f(const char *file,
f_state *s)
{
s->fd = open(file, 0);
if (s->fd == -1) {
fprintf(stderr, "Couldn't open '%s': %s\n", file, strerror(errno));
return -1;
}
if (fstat(s->fd, &s->st) != 0) {
fprintf(stderr, "Couldn't stat '%s': %s\n", file, strerror(errno));
return -1;
}
s->d = mmap(NULL, s->st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
s->fd, 0);
if (s->d == NULL) {
fprintf(stderr, "Couldn't mmap '%s': %s\n", file, strerror(errno));
return -1;
}
return 0;
}
typedef struct dbg_state {
f_state f;
di_header *di;
off_t symtab_offset;
coff_symtab_hdr *symtab;
} dbg_state;
int parse_dbg(const char *file,
dbg_state *s)
{
int ret;
off_t off;
unsigned sec_count;
unsigned dir_size;
dbg_dir *dir, *dir_end;
if ((ret = parse_f(file, &s->f)) < 0) {
return ret;
}
s->di = s->f.d;
if (validate_ele(&s->f, s->di, sizeof(*s->di)) < 0) {
fprintf(stderr, "No IMAGE_SEPARATE_DEBUG_HEADER in '%s'\n", file);
return -1;
}
if (le16toh(s->di->signature) != IMAGE_SEPARATE_DEBUG_SIGNATURE) {
fprintf(stderr, "Bad IMAGE_SEPARATE_DEBUG_HEADER in '%s'\n", file);
return -1;
}
sec_count = le32toh(s->di->number_of_sections);
dir_size = le32toh(s->di->debug_directory_size);
off = sizeof(di_header) + (sec_count * sizeof(pe32_sec)) +
s->di->exported_names_size;
dir = off_to_ptr(s->di, off);
if (validate_ele(&s->f, dir, dir_size) < 0) {
fprintf(stderr, "No IMAGE_DEBUG_DIRECTORY in '%s'\n", file);
return -1;
}
s->symtab = NULL;
for (dir_end = off_to_ptr(dir, dir_size);
dir < dir_end;
dir++) {
if (le32toh(dir->type) == IMAGE_DEBUG_TYPE_COFF) {
break;
}
}
if (dir == dir_end) {
fprintf(stderr, "No COFF symbol table in '%s'\n", file);
return -1;
}
s->symtab_offset = le32toh(dir->pointer_to_data);
s->symtab = off_to_ptr(s->di, s->symtab_offset);
if (validate_ele(&s->f, s->symtab, sizeof(s->symtab)) < 0) {
fprintf(stderr, "Bad COFF symbol table in '%s'\n", file);
return -1;
}
return 0;
}
typedef struct exe_state {
f_state f;
pe32_hdr *pe;
} exe_state;
int parse_exe(const char *file,
exe_state *s)
{
int ret;
mz_hdr *mz;
if ((ret = parse_f(file, &s->f)) < 0) {
return ret;
}
mz = s->f.d;
if (validate_ele(&s->f, mz, sizeof(*mz)) == 0 &&
le16toh(mz->e_magic) == PE32_DOS_MAGIC) {
s->pe = off_to_ptr(mz, le32toh(mz->e_lfanew) + 4);
} else {
s->pe = s->f.d;
}
if (validate_ele(&s->f, s->pe, sizeof(*s->pe)) != 0) {
fprintf(stderr, "No PE header in '%s'\n", file);
return -1;
}
if (le16toh(mz->e_magic) == PE32_DOS_MAGIC) {
uint32_t *magic = (uint32_t *) s->pe - 1;
if (le32toh(*magic) != PE32_MAGIC) {
fprintf(stderr, "Bad PE header in '%s'\n", file);
return -1;
}
}
return 0;
}
int link_dbg_exe(const dbg_state *ds,
const exe_state *es)
{
static char *m = "DBG and EXE probably unrelated, don't match on: %s\n";
if (le16toh(es->pe->machine) !=
le16toh(ds->di->machine)) {
fprintf(stderr, m, "machine type");
return -1;
}
if (le32toh(es->pe->time_date_stamp) !=
le32toh(ds->di->time_date_stamp)) {
fprintf(stderr, m, "time stamp");
return -1;
}
if (le16toh(es->pe->sections) !=
le32toh(ds->di->number_of_sections)) {
fprintf(stderr, m, "section count");
return -1;
}
if (le32toh(es->pe->number_of_syms) !=
le32toh(ds->symtab->number_of_syms)) {
fprintf(stderr, m, "symbol count");
return -1;
}
es->pe->pointer_to_symtab = htole32(
es->f.st.st_size + ds->symtab_offset +
sizeof(coff_symtab_hdr));
es->pe->chars = htole16(
le16toh(es->pe->chars) & ~(IMAGE_FILE_DEBUG_STRIPPED |
IMAGE_FILE_LOCAL_SYMS_STRIPPED |
IMAGE_FILE_LINE_NUMS_STRIPPED));;
return 0;
}
int write_new(const char *file,
const dbg_state *ds,
const exe_state *es)
{
int fd;
uint8_t *d;
off_t size;
fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0666);
if (fd == -1) {
fprintf(stderr, "Couldn't create '%s': %s\n", file, strerror(errno));
return -1;
}
size = ds->f.st.st_size + es->f.st.st_size;
if (lseek(fd, size - 1, SEEK_SET) != size - 1) {
fprintf(stderr, "Couldn't seek '%s': %s\n", file, strerror(errno));
return -1;
}
if (write(fd, " ", 1) != 1) {
fprintf(stderr, "Couldn't write '%s': %s\n", file, strerror(errno));
return -1;
}
d = mmap(NULL, size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (d == NULL) {
fprintf(stderr, "Couldn't mmap '%s': %s\n", file, strerror(errno));
return -1;
}
memcpy(d, es->f.d, es->f.st.st_size);
memcpy(d + es->f.st.st_size, ds->f.d, ds->f.st.st_size);
munmap(d, size);
close(fd);
return 0;
}
int main(int argc,
const char **argv)
{
const char *dbg_file;
const char *exe_file;
const char *new_file;
dbg_state ds;
exe_state es;
if (argc < 4) {
fprintf(stderr, "Usage: %s file.dbg file.exe new.exe\n", argv[0]);
return 1;
}
dbg_file = argv[1];
exe_file = argv[2];
new_file = argv[3];
if (parse_dbg(dbg_file, &ds) < 0) {
return 2;
}
if (parse_exe(exe_file, &es) < 0) {
return 3;
}
if (link_dbg_exe(&ds, &es) < 0) {
return 4;
}
if (write_new(new_file, &ds, &es) < 0) {
return 5;
}
printf("Done!\n");
return 0;
}