-
Notifications
You must be signed in to change notification settings - Fork 1
/
binaries.c
659 lines (584 loc) · 18.2 KB
/
binaries.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
binaries.c: code to save loaded LPC objects to binary files (in order
to speed loading during subsequent runs of the driver).
这个文件可以加快加载LPC文件/对象
This is mostly original code by Darin Johnson. Ideas came from CD,
including crdir_fopen(). Feel free to use this code but please keep
credits intact.
*/
#define SUPPRESS_COMPILER_INLINES
#include "std.h"
#include "lpc_incl.h"
#include "file_incl.h"
#include "binaries.h"
#include "lex.h"
#include "backend.h"
#include "swap.h"
#include "compile_file.h"
#include "hash.h"
#include "master.h"
/* This should be a configure check. What the heck is it needed for, anyway?*/
#ifdef WIN32
#include <direct.h>
#endif
#ifdef BINARIES
static char *magic_id = "MUDB";
static time_t driver_id;
static time_t config_id;
char driver_name[512];
static void patch_out (program_t *, short *, int);
static void patch_in (program_t *, short *, int);
static int str_case_cmp (char *, char *);
static int check_times (time_t, char *);
static int do_stat (char *, struct stat *, char *);
/*
* stats fname or CONFIG_FILE_DIR/fname (for finding config files) and/or
* BIN_DIR/fname (for finding config files and/or driver), whichever exists.
*/
static int
do_stat(char * fname, struct stat * st, char * pathname)
{
int i;
char buf[256];
if ((i = stat(fname, st)) != -1) {
if (pathname)
strcpy(pathname, fname);
return i;
}
/* look in CONFIG_FILE_DIR */
if (!pathname) {
#ifdef LATTICE
if (strchr(CONFIG_FILE_DIR, ':'))
sprintf(buf, "%s%s", CONFIG_FILE_DIR, fname);
else
#endif
sprintf(buf, "%s/%s", CONFIG_FILE_DIR, fname);
if ((i = stat(buf, st)) != -1)
return i;
}
/* look in BIN_DIR 在BIN_DIR中找 */
#ifdef LATTICE
if (strchr(BIN_DIR, ':'))
sprintf(buf, "%s%s", BIN_DIR, fname);
else
#endif
sprintf(buf, "%s/%s", BIN_DIR, fname);
if ((i = stat(buf, st)) != -1) {
if (pathname)
strcpy(pathname, buf);
return i;
}
return -1;
} /* do_stat() */
void save_binary(program_t * prog, mem_block_t * includes, mem_block_t * patches)
{
char file_name_buf[200];
char *file_name = file_name_buf;
FILE *f;
int i, tmp;
short len;
program_t *p;
struct stat st;
svalue_t *ret;
char *nm;
nm = add_slash(prog->name);
push_malloced_string(nm);
ret = safe_apply_master_ob(APPLY_VALID_SAVE_BINARY, 1);
if (!MASTER_APPROVED(ret))
return;
if (prog->total_size > (int) USHRT_MAX ||
includes->current_size > (int) USHRT_MAX)
/* assume all other sizes ok */
return;
strcpy(file_name, SAVE_BINARIES);
if (file_name[0] == '/')
file_name++;
if (stat(file_name, &st) == -1)
return;
strcat(file_name, "/");
strcat(file_name, prog->name);
len = strlen(file_name);
#ifdef LPC_TO_C
if (compile_to_c)
file_name[len - 1] = 'B'; /* change .c ending to .B */
else
#endif
file_name[len - 1] = 'b'; /* change .c ending to .b */
if (!(f = crdir_fopen(file_name)))
return;
if (comp_flag) {
debug_message(" saving binary ... ");
#ifdef LATTICE
fflush(stderr);
#endif
}
/*
* Write out preamble. Includes magic number, etc, all of which must
* match while loading.
*/
if (fwrite(magic_id, strlen(magic_id), 1, f) != 1 ||
fwrite((char *) &driver_id, sizeof driver_id, 1, f) != 1 ||
fwrite((char *) &config_id, sizeof config_id, 1, f) != 1) {
debug_message("I/O error in save_binary.\n");
fclose(f);
unlink(file_name);
return;
}
/*
* Write out list of include files.
*/
len = includes->current_size;
fwrite((char *) &len, sizeof len, 1, f);
fwrite(includes->block, includes->current_size, 1, f);
/*
* copy and patch program
*/
p = (program_t *) DXALLOC(prog->total_size, TAG_TEMPORARY, "save_binary");
/* convert to relative pointers, copy, then convert back */
locate_out(prog);
memcpy(p, prog, prog->total_size);
locate_in(prog);
if (patches->current_size) {
locate_in(p);
patch_out(p, (short *) patches->block,
patches->current_size / sizeof(short));
locate_out(p);
}
/*
* write out prog. The prog structure is mostly setup, but strings will
* have to be stored specially.
*/
/* program name */
len = SHARED_STRLEN(p->name);
fwrite((char *) &len, sizeof len, 1, f);
fwrite(p->name, sizeof(char), len, f);
fwrite((char *) &p->total_size, sizeof p->total_size, 1, f);
fwrite((char *) p, p->total_size, 1, f);
FREE(p);
p = prog;
/* inherit names */
for (i = 0; i < (int) p->num_inherited; i++) {
len = SHARED_STRLEN(p->inherit[i].prog->name);
fwrite((char *) &len, sizeof len, 1, f);
fwrite(p->inherit[i].prog->name, sizeof(char), len, f);
}
/* string table */
for (i = 0; i < (int) p->num_strings; i++) {
tmp = SHARED_STRLEN(p->strings[i]);
if (tmp > (int) USHRT_MAX) { /* possible? */
fclose(f);
unlink(file_name);
error("String too long for save_binary.\n");
return;
}
len = tmp;
fwrite((char *) &len, sizeof len, 1, f);
fwrite(p->strings[i], sizeof(char), len, f);
}
/* var names */
for (i = 0; i < (int) p->num_variables_defined; i++) {
len = SHARED_STRLEN(p->variable_table[i]);
fwrite((char *) &len, sizeof len, 1, f);
fwrite(p->variable_table[i], sizeof(char), len, f);
}
/* function names */
for (i = 0; i < (int) p->num_functions_defined; i++) {
len = SHARED_STRLEN(p->function_table[i].name);
fwrite((char *) &len, sizeof len, 1, f);
fwrite(p->function_table[i].name, sizeof(char), len, f);
}
/* line_numbers */
if (p->line_info)
len = p->file_info[0];
else
len = 0;
fwrite((char *) &len, sizeof len, 1, f);
fwrite((char *) p->file_info, len, 1, f);
/*
* patches
*/
len = patches->current_size;
fwrite((char *) &len, sizeof len, 1, f);
fwrite(patches->block, patches->current_size, 1, f);
fclose(f);
} /* save_binary() */
#define ALLOC_BUF(size) \
if ((size) > buf_size) { FREE(buf); buf = DXALLOC(buf_size = size, TAG_TEMPORARY, "ALLOC_BUF"); }
/* crude hack to check both .B and .b */
#ifdef LPC_TO_C
#define OUT_OF_DATE (lpc_obj ? load_binary(name, 0) : 0)
#else
#define OUT_OF_DATE 0
#endif
#ifdef LPC_TO_C
program_t *int_load_binary(char * name, lpc_object_t * lpc_obj)
#else
program_t *int_load_binary(char * name)
#endif
{
char file_name_buf[400];
char *buf, *iname, *file_name = file_name_buf, *file_name_two = &file_name_buf[200];
FILE *f;
int i, buf_size, ilen;
time_t mtime;
short len;
program_t *p, *prog;
object_t *ob;
struct stat st;
/* stuff from prolog() */
num_parse_error = 0;
sprintf(file_name, "%s/%s", SAVE_BINARIES, name);
if (file_name[0] == '/')
file_name++;
len = strlen(file_name);
#ifdef LPC_TO_C
file_name[len - 1] = (lpc_obj ? 'B' : 'b'); /* change .c ending to .b */
#else
file_name[len - 1] = 'b';
#endif
if (stat(file_name, &st) != -1)
mtime = st.st_mtime;
else
return OUT_OF_DATE;
if (!(f = fopen(file_name, FOPEN_READ)))
return OUT_OF_DATE;
if (comp_flag) {
debug_message(" loading binary %s ... ", name);
#ifdef LATTICE
fflush(stderr);
#endif
}
/* see if we're out of date with source */
if (check_times(mtime, name) <= 0) {
if (comp_flag)
debug_message("out of date (source file newer).\n");
fclose(f);
return OUT_OF_DATE;
}
buf = DXALLOC(buf_size = SMALL_STRING_SIZE, TAG_TEMPORARY, "ALLOC_BUF");
/*
* Read preamble. This must match, or we assume a different driver or
* configuration.
*/
if (fread(buf, strlen(magic_id), 1, f) != 1 ||
strncmp(buf, magic_id, strlen(magic_id)) != 0) {
if (comp_flag)
debug_message("out of date. (bad magic number)\n");
fclose(f);
FREE(buf);
return OUT_OF_DATE;
}
if ((fread((char *) &i, sizeof i, 1, f) != 1 || driver_id != i)
#ifdef LPC_TO_C
&& !lpc_obj
#endif
) {
if (comp_flag)
debug_message("out of date. (driver changed)\n");
fclose(f);
FREE(buf);
return OUT_OF_DATE;
}
if (fread((char *) &i, sizeof i, 1, f) != 1 || config_id != i) {
if (comp_flag)
debug_message("out of date. (config file changed)\n");
fclose(f);
FREE(buf);
return OUT_OF_DATE;
}
if (check_times(mtime, SIMUL_EFUN) <= 0) {
if (comp_flag)
debug_message("out of date (simul_efun newer).\n");
fclose(f);
FREE(buf);
return OUT_OF_DATE;
}
/*
* read list of includes and check times
*/
fread((char *) &len, sizeof len, 1, f);
ALLOC_BUF(len);
fread(buf, sizeof(char), len, f);
for (iname = buf; iname < buf + len; iname += strlen(iname) + 1) {
if (check_times(mtime, iname) <= 0) {
if (comp_flag)
debug_message("out of date (include file newer).\n");
fclose(f);
FREE(buf);
return OUT_OF_DATE;
}
}
/* check program name */
fread((char *) &len, sizeof len, 1, f);
ALLOC_BUF(len + 1);
fread(buf, sizeof(char), len, f);
buf[len] = '\0';
if (strcmp(name, buf) != 0) {
if (comp_flag)
debug_message("binary name inconsistent with file.\n");
fclose(f);
FREE(buf);
return OUT_OF_DATE;
}
/*
* Read program structure.
*/
fread((char *) &ilen, sizeof ilen, 1, f);
p = (program_t *) DXALLOC(ilen, TAG_PROGRAM, "load_binary");
fread((char *) p, ilen, 1, f);
locate_in(p); /* from swap.c */
p->name = make_shared_string(name);
/* Read inherit names and find prog. Check mod times also. */
for (i = 0; i < (int) p->num_inherited; i++) {
fread((char *) &len, sizeof len, 1, f);
ALLOC_BUF(len + 1);
fread(buf, sizeof(char), len, f);
buf[len] = '\0';
/*
* Check times against inherited source. If saved binary of
* inherited prog exists, check against it also.
*/
sprintf(file_name_two, "%s/%s", SAVE_BINARIES, buf);
if (file_name_two[0] == '/')
file_name_two++;
len = strlen(file_name_two);
file_name_two[len - 1] = 'b';
if (check_times(mtime, buf) <= 0 ||
check_times(mtime, file_name_two) == 0) { /* ok if -1 */
if (comp_flag)
debug_message("out of date (inherited source newer).\n");
fclose(f);
free_string(p->name);
FREE(p);
FREE(buf);
return OUT_OF_DATE;
}
/* find inherited program (maybe load it here?) */
ob = find_object2(buf);
if (!ob) {
if (comp_flag)
debug_message("missing inherited prog.\n");
fclose(f);
free_string(p->name);
FREE(p);
inherit_file = buf; /* freed elsewhere */
return 0;
}
p->inherit[i].prog = ob->prog;
}
/* Read string table */
for (i = 0; i < (int) p->num_strings; i++) {
fread((char *) &len, sizeof len, 1, f);
ALLOC_BUF(len + 1);
fread(buf, sizeof(char), len, f);
buf[len] = '\0';
p->strings[i] = make_shared_string(buf);
}
/* var names */
for (i = 0; i < (int) p->num_variables_defined; i++) {
fread((char *) &len, sizeof len, 1, f);
ALLOC_BUF(len + 1);
fread(buf, sizeof(char), len, f);
buf[len] = '\0';
p->variable_table[i] = make_shared_string(buf);
}
/* function names */
for (i = 0; i < (int) p->num_functions_defined; i++) {
fread((char *) &len, sizeof len, 1, f);
ALLOC_BUF(len + 1);
fread(buf, sizeof(char), len, f);
buf[len] = '\0';
p->function_table[i].name = make_shared_string(buf);
}
/* line numbers */
fread((char *) &len, sizeof len, 1, f);
p->file_info = (unsigned short *) DXALLOC(len, TAG_LINENUMBERS, "load binary");
fread((char *) p->file_info, len, 1, f);
p->line_info = (unsigned char *)&p->file_info[p->file_info[1]];
/* patches */
fread((char *) &len, sizeof len, 1, f);
ALLOC_BUF(len);
fread(buf, len, 1, f);
/* fix up some stuff */
patch_in(p, (short *) buf, len / sizeof(short));
fclose(f);
FREE(buf);
/*
* Now finish everything up. (stuff from epilog())
*/
prog = p;
total_prog_block_size += prog->total_size;
total_num_prog_blocks += 1;
swap_line_numbers(prog);
reference_prog(prog, "load_binary");
for (i = 0; (unsigned) i < prog->num_inherited; i++) {
reference_prog(prog->inherit[i].prog, "inheritance");
}
#ifdef LPC_TO_C
if (prog->program_size == 0) {
if (lpc_obj) {
if (comp_flag)
debug_message("linking jump table ...\n");
link_jump_table(prog, (void **)lpc_obj->jump_table);
} else {
if (prog)
free_prog(prog, 1);
return OUT_OF_DATE;
}
}
#endif
if (comp_flag)
debug_message("done.\n");
return prog;
} /* load_binary() */
void init_binaries(int argc, char ** argv)
{
struct stat st;
int arg_id, i;
if (do_stat(argv[0], &st, driver_name) != -1) {
driver_id = st.st_mtime;
} else { /* 找不到二进制文件 */
driver_id = current_time; /* 用时间作为driver_id不错 */
debug_message("Can't find \"%s\", ignoring old binaries.\n", argv[0]);
debug_message("Try invoking the driver using the full pathname.\n");
}
arg_id = 0;
for (i = 1; i < argc; i++) {
if (argv[i][0] != '-') {
if (config_id) continue;
if (do_stat(argv[i], &st, 0) != -1)
config_id = st.st_mtime;
else
config_id = current_time;
} else if (argv[i][1] == 'D' || argv[i][1] == 'm') {
arg_id = (arg_id << 1) ^ whashstr(argv[i] + 2, 32);
}
}
config_id ^= (arg_id << (sizeof(short) * 8));
} /* init_binaries() */
/*
* Test against modification times. -1 if file doesn't exist,
* 0 if out of date, and 1 if it's ok.
*/
static int check_times(time_t mtime, char * nm)
{
struct stat st;
#ifdef LATTICE
if (*nm == '/')
nm++;
#endif
if (stat(nm, &st) == -1)
return -1;
if (st.st_mtime > mtime) {
return 0;
}
return 1;
} /* check_times() */
/*
* Routines to do some hacking on the program being saved/loaded.
* Basically to fix up string switch tables, since the alternative
* would probably need a linear search in f_switch().
* I set things up so these routines can be used with other things
* that might need patching.
*/
static void patch_out(program_t * prog, short * patches, int len)
{
int i;
char *p;
p = prog->program;
while (len > 0) {
i = patches[--len];
if (p[i] == F_SWITCH && p[i + 1] >> 4 != 0xf) { /* string switch */
short offset, break_addr;
char *s;
/* replace strings in table with string table indices */
COPY_SHORT(&offset, p + i + 2);
COPY_SHORT(&break_addr, p + i + 4);
while (offset < break_addr) {
COPY_PTR(&s, p + i + 1 + offset);
/*
* take advantage of fact that s is in strings table to find
* it's index.
*/
if (s == 0)
s = (char *)(POINTER_INT)-1;
else
s = (char *)(POINTER_INT)store_prog_string(s);
COPY_PTR(p + i + 1 + offset, &s);
offset += SWITCH_CASE_SIZE;
}
}
}
} /* patch_out() */
static int str_case_cmp(char * a, char * b)
{
char *s1, *s2;
COPY_PTR(&s1, a);
COPY_PTR(&s2, b);
return s1 - s2;
} /* str_case_cmp() */
static void patch_in(program_t * prog, short * patches, int len)
{
int i;
char *p;
p = prog->program;
while (len > 0) {
i = patches[--len];
if (p[i] == F_SWITCH && p[i + 1] >> 4 != 0xf) { /* string switch */
short offset, start, break_addr;
char *s;
/* replace string indices with string pointers */
COPY_SHORT(&offset, p + i + 2);
COPY_SHORT(&break_addr, p + i + 4);
start = offset;
while (offset < break_addr) {
COPY_PTR(&s, p + offset + i + 1);
/*
* get real pointer from strings table
*/
if (s == (char *)-1)
s = 0;
else
s = prog->strings[(POINTER_INT)s];
COPY_PTR(p + offset + i + 1, &s);
offset += SWITCH_CASE_SIZE;
}
/* sort so binary search still works */
quickSort(&p[start + i + 1],
(break_addr - start) / SWITCH_CASE_SIZE,
SWITCH_CASE_SIZE, str_case_cmp);
}
}
} /* patch_in() */
#endif
/*
* open file for writing, creating intermediate directories if needed.
*/
FILE *crdir_fopen(char * file_name)
{
char *p;
struct stat st;
FILE *ret;
/*
* Beek - These directories probably exist most of the time, so let's
* optimize by trying the fopen first
*/
if ((ret = fopen(file_name, "wb")) != NULL) { /* 可能文件已存在了,不用创建了 */
return ret;
}
p = file_name;
while (*p && (p = (char *) strchr(p, '/'))) {
*p = '\0';
if (stat(file_name, &st) == -1) {
/* make this dir */
if (OS_mkdir(file_name, 0770) == -1) { /* 如果没有找到,就创建文件夹 */
*p = '/';
return (FILE *) 0;
}
}
*p = '/';
p++;
}
return fopen(file_name, "wb"); /* 文件夹都创建好了之后就可以打开了 */
}
/* crdir_fopen() */