-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbunny-gcc.c
1049 lines (789 loc) · 28.3 KB
/
bunny-gcc.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
bunny - gcc wrapper
-------------------
A drop-in replacement for gcc, meant to inject tracing hooks into the source after the
preprocessing stage, but before compilation.
Author: Michal Zalewski <[email protected]>
Copyright 2007 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* set default to resume interrupted primitives, avoiding EINTR,
cf. GNU LIBC manual 24.5 "Primitives Interrupted by Signals" */
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifndef __FreeBSD__
# include <getopt.h>
#endif /* !__FreeBSD__ */
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <ctype.h>
#include "types.h"
#include "config.h"
#include "debug.h"
#include "nlist.h"
#include "gcc-hook.h"
#include "message.h"
#include "util.h"
static struct naive_list
used_files, /* C source files to be processed */
preproc_params, /* Preprocessor parameter list */
compile_params; /* Compiler parameter list */
static _u32 mypid;
static _u8 gcc_mode = '\0'; /* -c, -E, -o, etc */
static _u8* output_file; /* final -o parameter */
/*
parse_params(argc,argv) - analyze gcc command-line parameters
Notes: gcc does not use getopt(), and implements a custom parser instead. It's pretty
inconsistent, too: '-g2' cannot be written as '-g 2', but '-xfoo' and '-x foo' are both
accepted. Long options are sometimes denoted with a single dash (-Xlinker), sometimes
with two dashes (--param). Parameter values are separated by ',', '=', or ' ', pretty much at
random. We need to mimick that insanity.
*/
static void parse_params(_u32 argc, _u8** argv) {
_u32 pos = 1;
#define LANG_AUTO 0
#define LANG_C 1
#define LANG_OTHER 2
_u8 lang = 0; /* LANG_* */
if (argc <= 1) fatal("not enough parameters for gcc");
/* Fill out argv[0] */
ADD(preproc_params,"gcc");
ADD(compile_params,"gcc");
#define c_alone(x) ((x) << 8)
#define c_any(x) c_alone(x) ... (c_alone(x) + 254)
#define c_2(x,y) ((x) << 8 | (y))
#define ADD_maysplit(list1,list2) do { \
if (list1) ADD(preproc_params,argv[pos]); \
if (list2) ADD(compile_params,argv[pos]); \
if (!argv[pos][2]) { \
if (++pos == argc) fatal("malformed %s option",argv[pos-1]); \
if (list1) ADD(preproc_params,argv[pos]); \
if (list2) ADD(compile_params,argv[pos]); \
} \
} while (0)
#define ADD_mustsplit(list1,list2) do { \
if (list1) ADD(preproc_params,argv[pos]); \
if (list2) ADD(compile_params,argv[pos]); \
if (++pos == argc) fatal("malformed %s option",argv[pos-1]); \
if (list1) ADD(preproc_params,argv[pos]); \
if (list2) ADD(compile_params,argv[pos]); \
} while (0)
while (pos < argc) {
if (*argv[pos] == '-' && argv[pos][1]) {
switch (c_2(argv[pos][1],argv[pos][2])) {
/* We need special handlings for options that may be split into two
argvs, or need to be *not* passed to one of the processing stages. */
/* BINARY OPTIONS TO KEEP OFF PREPROCESSOR COMMAND LINE */
case c_alone('M'): /* Makefile output (takes precedence over any other mode) */
case c_alone('c'): /* stop at assembly */
case c_alone('E'): /* stop at preprocessing */
case c_alone('S'): /* stop at compiling */
if (gcc_mode != 'M') gcc_mode = argv[pos][1];
ADD(compile_params,argv[pos]);
break;
/* SPLITTABLE PARAMETRIC OPTIONS (FOR PREPROCESSOR ONLY) */
case c_any ('A'): /* -A assertion */
case c_any ('D'): /* -D macro=val */
case c_any ('I'): /* -I dir, -I- */
case c_any ('U'): /* -U macro */
ADD_maysplit(1,0);
break;
case c_2('i','d'): /* -idirafter */
case c_2('i','m'): /* -imacros */
case c_2('i','n'): /* -include */
case c_2('i','s'): /* -isyste, */
case c_2('i','w'): /* -iwithpref... */
case c_2('X','p'): /* -Xpreprocessor */
ADD_mustsplit(1,0);
break;
/* SPLITTABLE PARAMETRIC OPTIONS (FOR COMPILER/LINKER ONLY) */
case c_alone('b'): /* -b machine */
case c_any ('l'): /* -l library */
case c_any ('L'): /* -L libpath */
case c_alone('u'): /* -u symbol */
case c_any ('V'): /* -V version */
ADD_maysplit(0,1);
break;
case c_2('X','a'): /* -Xassembler */
case c_2('X','l'): /* -Xlinker */
case c_2('-','p'): /* --param a=b */
ADD_mustsplit(0,1);
break;
case c_any ('o'): /* -o file */
if (argv[pos][2]) output_file = argv[pos]+2; else output_file = argv[pos+1];
ADD_maysplit(0,1);
break;
/* SPLITTABLE PARAMETRIC OPTIONS (FOR ALL STAGES) */
case c_any ('B'): /* -B bindir */
ADD_maysplit(1,1);
break;
case c_2('a','u'): /* -aux-info */
ADD_mustsplit(1,1);
break;
/* It's important to track -x <foo> to determine the language of input files,
should they have unusual extensions. */
case c_any ('x'): {
_u8* lname = argv[pos] + 2;
/* Handle splitting */
if (!*lname) {
if (++pos == argc) fatal("malformed -x option");
lname = argv[pos];
}
if (!strcmp(lname,"c")) lang = LANG_C; else
if (!strcmp(lname,"none")) lang = LANG_AUTO;
else lang = LANG_OTHER;
ADD(compile_params,"-x");
ADD(compile_params, (lang == LANG_C) ? (_u8*)"cpp-output" : lname);
break;
}
default:
/* Several compiler options won't play well with our solution: -fstrict-aliasing
will generate warnings about our type-neutral parameter reading code; and -ansi
or -std= parameters may restrict our access to crucial gcc features we rely on,
such as __attribute__ and typeof(). */
if (strcmp(argv[pos],"-fstrict-aliasing") && strcmp(argv[pos],"-ansi") &&
strncmp(argv[pos],"-std=",5)) {
ADD(preproc_params,argv[pos]);
ADD(compile_params,argv[pos]);
} else {
debug("[bunny] WARNING: '%s' is not supported, ignoring.\n",argv[pos]);
}
break;
}
#undef c_alone
#undef c_any
#undef ADD_maysplit
#undef ADD_mustsplit
} else {
_u8 tmp[128];
/* Got what appears to be a filename. Decide how to proceed. */
switch (lang) {
case LANG_AUTO:
if (!strcasecmp(argv[pos] + strlen(argv[pos]) - 2, ".c")) {
sprintf(tmp,".bunny-%x-%u-f.i",mypid,used_files.c);
ADD(used_files,argv[pos]);
DYN_ADD(compile_params,tmp);
} else ADD(compile_params,argv[pos]);
break;
case LANG_C:
sprintf(tmp,".bunny-%x-%u-f.i",mypid,used_files.c);
ADD(used_files,argv[pos]);
DYN_ADD(compile_params,tmp);
break;
case LANG_OTHER:
ADD(compile_params,argv[pos]);
}
}
pos++;
}
if (!used_files.c)
debug("[bunny] No .c files to process spotted, will not install hooks.\n");
/* Finishing touches: */
ADD(compile_params,"-fno-strict-aliasing"); /* Explained earlier */
ADD(preproc_params,"-xc"); /* Force C processing */
ADD(preproc_params,"-C"); /* Avoid stripping of comments */
ADD(preproc_params,"-E"); /* Preprocess only */
ADD(preproc_params,"<file.c>"); /* Just a placeholder for input file */
ADD(preproc_params,"-o");
ADD(preproc_params,"<file.i>"); /* Ditto, output. */
}
/* wait_execvp() - execute program in a new process, wait for return, handle errors. */
static void wait_execvp(_u8* path, _u8** argv) {
_s32 pid, st;
if (getenv("BUNNY_EXEC")) {
_u32 i = 0;
debug(">> EXEC");
while (argv[i]) debug(" %s",argv[i++]);
debug("\n");
}
pid = fork();
if (pid < 0) fatal("unable to spawn a process");
if (!pid) {
argv[0] = path;
execvp(path,(char**)argv);
pfatal("unable to invoke gcc (set $BUNNY_GCC)");
} else {
if (waitpid(pid,&st,0) != pid) pfatal("waitpid() fails?");
if (WIFSIGNALED(st)) fatal("gcc died on signal %u",WTERMSIG(st));
else if (WEXITSTATUS(st))
fatal("gcc error (exit code %u)",WEXITSTATUS(st));
}
}
/* precompile() - invoke precompiler, store output in a temporary file. */
static void precompile(void) {
_u32 i;
_u8 tmp[128], *gcc = getenv("BUNNY_GCC");
if (!gcc) gcc = "/usr/bin/gcc";
for (i=0;i<used_files.c;i++) {
sprintf(tmp,".bunny-%x-%u.i",mypid,i);
/* We "trust" gcc to safely create the output file. Perhaps not the best idea ever, but
then neither is running gcc in world-writable directories to begin with - no point
in pretending we could fix it. */
preproc_params.v[preproc_params.c-3] = used_files.v[i];
preproc_params.v[preproc_params.c-1] = tmp;
debug("[bunny] STAGE 1/3: Precompiling '%s'...\n",used_files.v[i]);
wait_execvp(gcc,preproc_params.v);
}
}
/* get_item() - try to isolate a single language token from the input file.
See inline comments for more insight. */
static _u8* get_item(FILE* f) {
_s32 c;
static _u8 obuf[MAXTOKEN];
_u32 olen = 0;
_u8 qopen;
if ((c = getc(f)) == EOF) return 0;
/* Cluster whitespaces together */
if (isspace(c)) {
obuf[olen++] = c;
while ((c=getc(f)) != EOF && isspace(c) && olen < MAXTOKEN - 1) obuf[olen++] = c;
obuf[olen] = 0;
if (olen != MAXTOKEN - 1 && c != EOF) ungetc(c,f);
return obuf;
}
/* Dump all text between "..." or '...' as a single token; watch for \, though */
if (c == '"' || c == '\'') {
_u8 escnext = 0;
qopen = obuf[olen++] = c;
while ((c=getc(f)) != EOF && (escnext || c != qopen) && olen < MAXTOKEN - 2) {
if (!escnext) escnext = (c == '\\'); else escnext = 0;
obuf[olen++] = c;
}
if (olen == MAXTOKEN - 2) fatal("string too long: '%.32s...'",obuf);
if (c == EOF) fatal("unterminated string: '%.32s...'",obuf);
obuf[olen++] = qopen;
obuf[olen] = 0;
return obuf;
}
/* Special handling for compiler directives (#) - copy whole line */
if (c == '#') {
obuf[olen++] = c;
while ((c=getc(f)) != EOF && c != '\n' && olen < MAXTOKEN - 2) obuf[olen++] = c;
if (olen == MAXTOKEN - 2) fatal("compiler directive too long: '%.32s...'",obuf);
obuf[olen++] = '\n';
obuf[olen] = 0;
return obuf;
}
/* skip C++-style comments */
if ( c=='/' ) {
_s32 oldc = c;
if ( (c=getc(f)) != EOF && c =='/' ) {
while ( (c=getc(f)) != EOF && c !='\n' ) ;
return get_item(f);
} else {
ungetc(c,f);
c = oldc;
}
}
/* return C-style comments as single token */
if ( c=='/' ) {
_s32 oldc = c;
if ( (c=getc(f)) != EOF && c =='*' ) {
obuf[olen++] = '/';
obuf[olen++] = '*';
while ( (c=getc(f)) != EOF ) {
if ( olen < MAXTOKEN -2 )
obuf[olen++] = c;
if ( oldc == '*' && c == '/' ) {
obuf[olen-2] = '*';
obuf[olen-1] = '/';
obuf[olen] = 0;
return obuf;
}
oldc = c;
}
return 0;
} else {
ungetc(c,f);
c = oldc;
}
}
/* If found a run of [A-Za-z0-9_], consume it all. Everything else, return char-by-char. */
while (olen < MAXTOKEN - 1) {
if (!isalnum(c) && c != '_') {
if (olen) ungetc(c,f); else obuf[olen++] = c;
obuf[olen] = 0;
return obuf;
}
obuf[olen++] = c;
if ((c = getc(f)) == EOF) {
obuf[olen] = 0;
return obuf;
}
}
fatal("line too long: '%.30s...'",obuf);
}
/* insert_hooks() - parse preprocessed file, locate functions, enumerate params, insert hooks.
This truly is wicked - the goal is to insert possibly non-disruptive code to intercept
function parameters as soon as possible (and this may look trivial until you consider that
whole structs can be pushed on stack as parameters, local variables may shadow params,
varargs need to be handled...); and to grab return codes with the same caveats.
*/
static void insert_hooks(void) {
FILE *f, *o;
_u32 fno;
_u8 fname[128];
/* A ridiculously complex state machine... */
_u8 *tok, /* Current input token */
*prev_name, /* Previous alnum token */
*func_name, /* Suspected function name */
*prev_name1; /* Kept as per keep_nest1 */
_u32 code_nest, /* Code nesting level - { ... } */
expr_nest, /* Expression nesting level - ( ... ) */
prev_nest, /* Previous element's expr nest level */
prev_cnest, /* Previous element's code nest level */
param_nest, /* Parameter parser nesting level */
token_cnt, /* Pointless stats */
ret_nest, /* Testing for void return (code + expr nest) */
square_nest, /* [...] nest level */
hook_cnt; /* Ditto */
_u8 get_params, /* Inside a parameter list? */
keep_nest1, /* Grab first token at expr nest param_nest + 1 */
params_ok, /* Parameters collected OK */
got_struct, /* { } may refer to struct/enum/union */
do_second, /* Second chance parameter acquisition */
ret_void, /* return is void? */
in_func, /* in function? */
ignore_now, /* ignoring func params until ; */
check_decl; /* Try to tell decl from def */
struct naive_list params = { 0, 0 };
for (fno=0;fno<used_files.c;fno++) {
_s32 ofn;
_u8 *curfn;
code_nest = 0;
expr_nest = 0;
prev_name = 0;
func_name = 0;
get_params = 0;
params_ok = 0;
keep_nest1 = 0;
prev_nest = 0;
prev_cnest = 0;
prev_name1 = 0;
param_nest = 0;
check_decl = 0;
token_cnt = 0;
hook_cnt = 0;
got_struct = 0;
do_second = 0;
ret_void = 0;
ret_nest = 0;
in_func = 0;
ignore_now = 0;
square_nest = 0;
curfn = used_files.v[fno];
/* Open input and output files... */
sprintf(fname,".bunny-%x-%u.i",mypid,fno);
f = fopen(fname,"r");
if (!f) pfatal("unable to read file %s",fname);
if (!getenv("BUNNY_KEEPTEMP")) unlink(fname);
sprintf(fname,".bunny-%x-%u-f.i",mypid,fno);
unlink(fname);
ofn = open(fname,O_WRONLY|O_CREAT|O_EXCL,0600);
if (ofn < 0) pfatal("unable to create temp file %s",fname);
if (!(o = fdopen(ofn,"w"))) fatal("unable to create FILE object");
#define outf(x...) fprintf(o,x)
#define OUTPUT_RETURN_VOID() \
outf(" do { __bunny_send_msg(0x%08x, 0, 0, 0); return; } while (0) ", MESSAGE_LEAVE)
#define OUTPUT_RETURN_FINAL() \
outf(" __bunny_send_msg(0x%08x, 0, 0, 0); ", MESSAGE_LEAVE)
#define OUTPUT_RETURN_PROLOGUE() \
outf(" do { __bunny_ret_t __bunny_ret = ( ")
#define OUTPUT_RETURN_EPILOGUE() \
outf(" ); __bunny_send_msg(0x%08x, *(unsigned int*)&__bunny_ret, 0, 0); " \
"return __bunny_ret; } while (0) ", MESSAGE_LEAVE)
#if 0
# define DEBUGF(x...) printf(x)
#else
# define DEBUGF(x...) do {} while (0)
#endif
/* Store our hook code at the very beginning the output file. */
outf("# 1 \"<bunny internal code>\"\n\n%s\n\n",bunny_hook_code);
while ((tok=get_item(f))) {
token_cnt++;
DEBUGF(">>> NEW TOKEN [%s]\n",tok);
/* This is a special case - don't look inside */
if (!strcmp(tok,"__attribute__") || !strcmp(tok,"__asm__")) {
_s32 iexp_nest = expr_nest;
outf("%s",tok);
while (iexp_nest >= 0 && (tok=get_item(f))) {
token_cnt++;
switch (tok[0]) {
case '(': outf("%s", tok); expr_nest++; break;
case ')': outf("%s", tok);
if (!expr_nest)
fatal("nesting error inside __attribute__ / __asm__ in '%s'",curfn);
if (--expr_nest == iexp_nest) iexp_nest = -1;
break;
default: outf("%s", tok);
}
}
if (!tok) fatal("unterminated __attribute__ in '%s'",used_files.v[fno]);
continue;
}
/* Another special case is our "BunnySnoop" directive */
if (!strcmp(tok,"BunnySnoop")) {
if (!code_nest) fatal("stray BunnySnoop block in '%s'",used_files.v[fno]);
outf("do { volatile char __attribute__((unused)) __dummy = "
"__bunny_send_msg(0x%08x,(unsigned int)(",
MESSAGE_SPOT);
while ((tok = get_item(f))) {
token_cnt++;
if (tok[0] == ';') break;
outf("%s",tok);
}
if (!tok) fatal("unterminated BunnySnoop block in '%s'",curfn);
outf("),0,0); } while (0);\n");
hook_cnt++;
continue;
}
/* And a yet another one... */
if (!strcmp(tok,"BunnySkip") || !strncmp(tok,"/*BunnySkip",11)) {
if (!code_nest && !expr_nest) {
DEBUGF("<<BunnySkip detected, bailing out>>\n");
DYN_FREE(params);
free(func_name);
free(prev_name);
prev_name = 0;
func_name = 0;
params_ok = 0;
check_decl = 0;
ignore_now = 1;
} else fatal("misplaced BunnySkip statement in '%s'",curfn);
continue;
}
switch (tolower(tok[0])) {
case '(':
if (ret_void) {
OUTPUT_RETURN_PROLOGUE();
ret_void = 0;
}
expr_nest++;
/* This might be a function param list */
if (!code_nest && !get_params && !prev_cnest && !ignore_now) {
if (params_ok && !do_second && params.c == 1) {
DEBUGF("<<<second chance (name set to %s)>>>\n",params.v[0]);
free(func_name);
free(prev_name);
func_name = 0;
prev_name = strdup(params.v[0]);
if (!prev_name) fatal("out of memory");
DYN_FREE(params);
params_ok = 0;
keep_nest1 = 0;
do_second = 1;
check_decl = 1;
}
if (!params_ok && prev_name) {
DEBUGF("<<starting to collect params>>\n");
func_name = strdup(prev_name);
if (!func_name) fatal("out of memory");
free(prev_name);
prev_name = 0;
get_params = 1;
param_nest = expr_nest;
keep_nest1 = 1;
}
}
outf("%s",tok);
break;
case '=':
/* Disregard anything that seemed like a function if = is encountered */
if (!code_nest && !expr_nest) {
DEBUGF("<<assignment detected, bailing out>>\n");
DYN_FREE(params);
free(func_name);
free(prev_name);
prev_name = 0;
func_name = 0;
params_ok = 0;
check_decl = 0;
ignore_now = 1;
}
case ',':
if (!code_nest && !expr_nest) goto handle_as_colon;
/* Fall through */
case ')':
/* We might have a parameter! Write it down. */
if (get_params && !code_nest && expr_nest == param_nest) {
if (prev_nest == param_nest) {
DEBUGF("<<storing prev_name as param>>\n");
if (prev_name && strcmp(prev_name,"void")) DYN_ADD(params,prev_name);
} else {
DEBUGF("<<storing prev_name1 as param>>\n");
if (prev_name1) DYN_ADD(params,prev_name1);
}
/* Avoid unintentional reuse of prev_name */
if (prev_name) free(prev_name);
if (prev_name1) free(prev_name1);
prev_name = prev_name1 = 0;
/* Finalize parameter list... */
if (tok[0] == ')') {
DEBUGF("<<finalizing param list>>\n");
keep_nest1 = 0;
get_params = 0;
params_ok = 1;
check_decl = 1;
} else keep_nest1 = 1;
}
if (tok[0] == ')') {
if (!expr_nest) fatal("')' nesting level error in '%s'", curfn);
expr_nest--;
}
outf("%s",tok);
break;
case '[':
expr_nest++;
square_nest++;
outf("%s",tok);
break;
case ']':
if (!expr_nest) fatal("']' nesting level error in '%s'", curfn);
expr_nest--;
square_nest--;
outf("%s",tok);
break;
case '*':
if (ret_void) {
OUTPUT_RETURN_PROLOGUE();
ret_void = 0;
}
/* What we thought is a parameter list contains a function name */
if (expr_nest == 1 && !code_nest && get_params == 1 && !do_second) {
DEBUGF("<<param list reclassified as func name>>\n");
free(func_name);
func_name = 0;
get_params = 0;
keep_nest1 = 0;
do_second = 1;
}
outf("%s",tok);
break;
case ';':
handle_as_colon:
if (!code_nest) { got_struct = 0; do_second = 0; ignore_now = 0; }
if (ret_nest && code_nest + expr_nest == ret_nest) {
if (ret_void) OUTPUT_RETURN_VOID(); else OUTPUT_RETURN_EPILOGUE();
ret_void = 0;
ret_nest = 0;
}
/* So, we indeed have a declaration and nothing more. */
if (!code_nest && !expr_nest) {
DEBUGF("<<no function body detected, bailing out>>\n");
DYN_FREE(params);
free(func_name);
func_name = 0;
params_ok = 0;
check_decl = 0;
}
outf("%s",tok);
break;
case '{':
outf("%s",tok);
/* We're set for a real function, eh? */
if (!code_nest && !expr_nest && params_ok && !got_struct) {
_u32 i;
DEBUGF(">>> NEW FUNCTION '%s'\n", func_name);
for (i=0;i<params.c;i++)
DEBUGF(">>> parameter '%s'\n",params.v[i]);
/* We want to execute our code at the very beginning of the function.
This is possible with a nifty abuse of variable initialization. */
outf(" volatile char __attribute__((unused)) __dummy_i = "
"__bunny_send_msg(0x%08x,%u,\"%s\",%u); ", MESSAGE_ENTER,
params.c, func_name, (_u32)strlen(func_name));
for (i=0;i<params.c;i++)
outf(" volatile char __attribute__((unused)) __dummy_%03u = "
"__bunny_send_msg(0x%08x,*(unsigned int*)&(%s),0,0); ", i,
MESSAGE_PARAM, params.v[i]);
/* We need to know what our function returns to intercept return values
at a later date. */
outf(" typedef typeof(%s(",func_name);
for (i=0;i<params.c;i++) outf("%s%s", i?", ":"", params.v[i]);
outf(")) __bunny_ret_t;");
/* Count enter, return, params */
hook_cnt += 2 + params.c;
DYN_FREE(params);
free(func_name);
func_name = 0;
params_ok = 0;
in_func = 1;
}
code_nest++;
break;
case '}':
if (!code_nest) fatal("code nesting level error in '%s'", curfn);
code_nest--;
if (!code_nest && in_func) {
OUTPUT_RETURN_FINAL();
do_second = 0;
in_func = 0;
}
outf("%s",tok);
break;
/* Word token */
case 'a' ... 'z':
case '_':
if (get_params == 1) get_params = 2;
if (!square_nest) {
if (prev_name) free(prev_name);
prev_name = strdup(tok);
if (!prev_name) fatal("out of memory");
prev_nest = expr_nest;
prev_cnest = code_nest;
}
if (keep_nest1 && param_nest + 1 == expr_nest && !square_nest) {
DEBUGF("<<storing as keep_nest1>>\n");
prev_name1 = strdup(tok);
if (!prev_name1) fatal("out of memory");
keep_nest1 = 0;
}
/* We got something other than ';' and '__attribute__' following a parameter
list; this is likely a definition, not a declaration. */
if (!expr_nest && !square_nest) check_decl = 0;
if (!code_nest && params_ok && (!strcmp(tok,"struct") || !strcmp(tok,"enum") ||
!strcmp(tok,"union"))) got_struct = 1;
/* Register parameters are incompatible with our parameter grabbing code,
and getting rid of them has pretty much no effect for most uses. */
if (!code_nest && !strcmp(tok,"register")) break;
/* We need to intercept 'return' and report the value. */
if (!strcmp(tok,"return") && in_func) { ret_nest = code_nest + expr_nest; ret_void = 1; break; }
/* Fall through */
/* Whitespaces, numbers, operators, text, compiler directives... */
case '0' ... '9':
default:
if (ret_void && !isspace(tok[0])) {
OUTPUT_RETURN_PROLOGUE();
ret_void = 0;
}
outf("%s",tok);
break;
}
DEBUGF(">>> prevn='%s' fn='%s' prevn1='%s' cn=%d en=%d prevn=%d parn=%d\n getp%d keep%d pok%d cdecl%d sec%d ig%d\n",
prev_name, func_name, prev_name1, code_nest, expr_nest, prev_nest, param_nest,
get_params, keep_nest1, params_ok, check_decl,do_second, ignore_now);
#undef DEBUGF
#undef outf
#undef OUTPUT_RETURN_VOID
#undef OUTPUT_RETURN_PROLOGUE
#undef OUTPUT_RETURN_EPILOGUE
}
if (prev_name) free(prev_name);
if (func_name) free(func_name);
DYN_FREE(params);
if (code_nest || expr_nest)
fatal("EOF at nest level %u/%u in '%s'", code_nest, expr_nest, used_files.v[fno]);
fclose(f);
fclose(o);
debug("[bunny] STAGE 2/3: Injected %u hooks into '%s' (%u tokens).\n",hook_cnt,
used_files.v[fno],token_cnt);
}
/* Free at last! */
}
/* compile() - consolidate, compile and link all the files. */
static void compile(void) {
_u8 *gcc = getenv("BUNNY_GCC");
_u32 i;
_u8 fname[128];
if (!gcc) gcc = "/usr/bin/gcc";
switch (gcc_mode) {
case 'c':
if (output_file) {
debug("[bunny] STAGE 3/3: Compiling binary to '%s'...\n",output_file);
} else {
debug("[bunny] STAGE 3/3: Compiling binary to default .o file(s)...\n");
}
break;
case 'S':
if (output_file) {
debug("[bunny] STAGE 3/3: Compiling assembly code to '%s'...\n",output_file);
} else {
debug("[bunny] STAGE 3/3: Compiling assembly code to default .s file(s)...\n");
}
break;
default:
if (output_file) {
debug("[bunny] STAGE 3/3: Compiling and linking executable to '%s'...\n",output_file);
} else {
debug("[bunny] STAGE 3/3: Compiling and linking executable to default location...\n");
}
break;
}
wait_execvp(gcc,compile_params.v);
/* Us succeed? That's unpossible. */
for (i=0;i<used_files.c;i++) {
sprintf(fname,".bunny-%x-%u-f.i",mypid,i);
if (!getenv("BUNNY_KEEPTEMP")) unlink(fname);
}
/* We have to remove .o files to their ultimate destinations if no 'o' flag was found
(if one was used, rename() will fail silently). */
if (gcc_mode == 'c' || gcc_mode == 'S')
for (i=0;i<used_files.c;i++) {
_u8 tmp[MAXTOKEN + 2], *per;
if (gcc_mode == 'c') sprintf(fname,".bunny-%x-%u-f.o",mypid,i);
else sprintf(fname,".bunny-%x-%u-f.s",mypid,i);
tmp[MAXTOKEN-1] = 0;
per = strrchr(used_files.v[i],'/');
if (!per) per = used_files.v[i]; else per++;
strncpy(tmp,per,MAXTOKEN-1);
per = strrchr(tmp,'.');
if (per) *per = 0;
if (gcc_mode == 'c') strcat(tmp,".o"); else strcat(tmp,".s");
rename(fname,tmp);
}
}
/* dump_code() - output preprocessed code (-E option) */
static void dump_code(void) {
_u32 fno;
if (output_file) {
debug("[bunny] STAGE 3/3: Dumping modified source to '%s'...\n",output_file);
} else {
debug("[bunny] STAGE 3/3: Dumping modified source to stdout...\n");
}
for (fno=0;fno<used_files.c;fno++) {
_u8 cbuf[1024], fname[128];
_s32 fin, fout, i;
sprintf(fname,".bunny-%x-%u-f.i",mypid,fno);
if (output_file) {
unlink(output_file);