forked from softace/sqliteodbc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcc-0.9.23.patch
1525 lines (1460 loc) · 49.3 KB
/
tcc-0.9.23.patch
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
diff -Nur tcc-0.9.23.orig/Changelog tcc-0.9.23/Changelog
--- tcc-0.9.23.orig/Changelog 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/Changelog 2007-01-27 18:23:29.000000000 +0100
@@ -1,3 +1,23 @@
+version 0.9.24:
+
+- ignore AS_NEEDED ld command
+- mark executable sections as executable when running in memory
+- added support for win32 wchar_t (Filip Navara)
+- segment override prefix support (Filip Navara)
+- normalized slashes in paths (Filip Navara)
+- windows style fastcall (Filip Navara)
+- support for empty input register section in asm (Filip Navara)
+- anonymous union/struct support (Filip Navara)
+- fixed parsing of function parameters
+- workaround for function pointers in conditional expressions (Dave Dodge)
+- initial '-E' option support to use the C preprocessor alone
+- discard type qualifiers when comparing function parameters (Dave Dodge)
+- Bug fix: A long long value used as a test expression ignores the
+ upper 32 bits at runtime (Dave Dodge)
+- fixed multiple concatenation of PPNUM tokens (initial patch by Dave Dodge)
+- fixed multiple typedef specifiers handling
+- fixed sign extension in some type conversions (Dave Dodge)
+
version 0.9.23:
- initial PE executable format for windows version (grischka)
diff -Nur tcc-0.9.23.orig/configure tcc-0.9.23/configure
--- tcc-0.9.23.orig/configure 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/configure 2007-01-27 18:24:20.000000000 +0100
@@ -114,7 +114,7 @@
;;
--enable-gprof) gprof="yes"
;;
- --enable-mingw32) mingw32="yes" ; cross_prefix="i386-mingw32-"
+ --enable-mingw32) mingw32="yes" ; cross_prefix="i386-mingw32msvc-"
;;
--enable-cross) build_cross="yes"
;;
@@ -194,7 +194,7 @@
echo " --libdir=DIR object code libraries in DIR [EPREFIX/lib]"
echo " --includedir=DIR C header files in DIR [PREFIX/include]"
echo " --mandir=DIR man documentation in DIR [PREFIX/man]"
-echo " --build-cross build cross compilers"
+echo " --enable-cross build cross compilers"
echo ""
echo "Advanced options (experts only):"
echo " --source-path=PATH path of source code [$source_path]"
diff -Nur tcc-0.9.23.orig/cross-configure.sh tcc-0.9.23/cross-configure.sh
--- tcc-0.9.23.orig/cross-configure.sh 1970-01-01 01:00:00.000000000 +0100
+++ tcc-0.9.23/cross-configure.sh 2005-07-28 11:54:56.000000000 +0200
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+CONFIG_SHELL=/bin/sh
+export CONFIG_SHELL
+PREFIX=/opt/mingw
+TARGET=i386-mingw32msvc
+PATH="$PREFIX/bin:$PREFIX/$TARGET/bin:$PATH"
+export PATH
+cache=cross-config.cache
+sh configure --cache-file="$cache" \
+ --target=$TARGET --host=$TARGET --build=i386-linux \
+ "$@"
+status=$?
+rm -f "$cache"
+exit $status
diff -Nur tcc-0.9.23.orig/cross-make.sh tcc-0.9.23/cross-make.sh
--- tcc-0.9.23.orig/cross-make.sh 1970-01-01 01:00:00.000000000 +0100
+++ tcc-0.9.23/cross-make.sh 2005-07-28 11:54:56.000000000 +0200
@@ -0,0 +1,7 @@
+#!/bin/sh
+
+PREFIX=/opt/mingw
+TARGET=i386-mingw32msvc
+PATH="$PREFIX/bin:$PREFIX/$TARGET/bin:$PATH"
+export PATH
+exec make "$@"
diff -Nur tcc-0.9.23.orig/i386-asm.c tcc-0.9.23/i386-asm.c
--- tcc-0.9.23.orig/i386-asm.c 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/i386-asm.c 2007-01-27 18:24:37.000000000 +0100
@@ -151,6 +151,15 @@
0x0f, /* g */
};
+static const uint8_t segment_prefixes[] = {
+ 0x26, /* es */
+ 0x2e, /* cs */
+ 0x36, /* ss */
+ 0x3e, /* ds */
+ 0x64, /* fs */
+ 0x65 /* gs */
+};
+
static const ASMInstr asm_instrs[] = {
#define ALT(x) x
#define DEF_ASM_OP0(name, opcode)
@@ -311,8 +320,10 @@
if (tok != ',') {
op->reg2 = asm_parse_reg();
}
- skip(',');
- op->shift = get_reg_shift(s1);
+ if (tok == ',') {
+ next();
+ op->shift = get_reg_shift(s1);
+ }
}
skip(')');
}
@@ -410,14 +421,15 @@
static void asm_opcode(TCCState *s1, int opcode)
{
const ASMInstr *pa;
- int i, modrm_index, reg, v, op1, is_short_jmp;
+ int i, modrm_index, reg, v, op1, is_short_jmp, has_seg_prefix;
int nb_ops, s, ss;
- Operand ops[MAX_OPERANDS], *pop;
+ Operand ops[MAX_OPERANDS], *pop, seg_prefix;
int op_type[3]; /* decoded op type */
/* get operands */
pop = ops;
nb_ops = 0;
+ has_seg_prefix = 0;
for(;;) {
if (tok == ';' || tok == TOK_LINEFEED)
break;
@@ -425,6 +437,18 @@
error("incorrect number of operands");
}
parse_operand(s1, pop);
+ if (tok == ':') {
+ if (pop->type != OP_SEG || has_seg_prefix) {
+ error("incorrect prefix");
+ }
+ seg_prefix = *pop;
+ has_seg_prefix = 1;
+ next();
+ parse_operand(s1, pop);
+ if (!(pop->type & OP_EA)) {
+ error("segment prefix must be followed by memory reference");
+ }
+ }
pop++;
nb_ops++;
if (tok != ',')
@@ -538,6 +562,8 @@
/* now generates the operation */
if (pa->instr_type & OPC_FWAIT)
g(0x9b);
+ if (has_seg_prefix)
+ g(segment_prefixes[seg_prefix.reg]);
v = pa->opcode;
if (v == 0x69 || v == 0x69) {
diff -Nur tcc-0.9.23.orig/i386-gen.c tcc-0.9.23/i386-gen.c
--- tcc-0.9.23.orig/i386-gen.c 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/i386-gen.c 2007-01-27 18:24:55.000000000 +0100
@@ -321,6 +321,7 @@
}
static uint8_t fastcall_regs[3] = { TREG_EAX, TREG_EDX, TREG_ECX };
+static uint8_t fastcallw_regs[2] = { TREG_ECX, TREG_EDX };
/* Generate function call. The function address is pushed first, then
all the parameters in call order. This functions pops all the
@@ -381,13 +382,21 @@
func_sym = vtop->type.ref;
func_call = func_sym->r;
/* fast call case */
- if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
+ if ((func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) ||
+ func_call == FUNC_FASTCALLW) {
int fastcall_nb_regs;
- fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
+ uint8_t *fastcall_regs_ptr;
+ if (func_call == FUNC_FASTCALLW) {
+ fastcall_regs_ptr = fastcallw_regs;
+ fastcall_nb_regs = 2;
+ } else {
+ fastcall_regs_ptr = fastcall_regs;
+ fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
+ }
for(i = 0;i < fastcall_nb_regs; i++) {
if (args_size <= 0)
break;
- o(0x58 + fastcall_regs[i]); /* pop r */
+ o(0x58 + fastcall_regs_ptr[i]); /* pop r */
/* XXX: incorrect for struct/floats */
args_size -= 4;
}
@@ -409,6 +418,7 @@
{
int addr, align, size, func_call, fastcall_nb_regs;
int param_index, param_addr;
+ uint8_t *fastcall_regs_ptr;
Sym *sym;
CType *type;
@@ -418,8 +428,13 @@
loc = 0;
if (func_call >= FUNC_FASTCALL1 && func_call <= FUNC_FASTCALL3) {
fastcall_nb_regs = func_call - FUNC_FASTCALL1 + 1;
+ fastcall_regs_ptr = fastcall_regs;
+ } else if (func_call == FUNC_FASTCALLW) {
+ fastcall_nb_regs = 2;
+ fastcall_regs_ptr = fastcallw_regs;
} else {
fastcall_nb_regs = 0;
+ fastcall_regs_ptr = NULL;
}
param_index = 0;
@@ -449,7 +464,7 @@
/* save FASTCALL register */
loc -= 4;
o(0x89); /* movl */
- gen_modrm(fastcall_regs[param_index], VT_LOCAL, NULL, loc);
+ gen_modrm(fastcall_regs_ptr[param_index], VT_LOCAL, NULL, loc);
param_addr = loc;
} else {
param_addr = addr;
@@ -585,7 +600,8 @@
gsym(vtop->c.i);
}
} else {
- if (is_float(vtop->type.t)) {
+ if (is_float(vtop->type.t) ||
+ (vtop->type.t & VT_BTYPE) == VT_LLONG) {
vpushi(0);
gen_op(TOK_NE);
}
diff -Nur tcc-0.9.23.orig/libtcc.h tcc-0.9.23/libtcc.h
--- tcc-0.9.23.orig/libtcc.h 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/libtcc.h 2007-01-27 18:25:07.000000000 +0100
@@ -12,6 +12,11 @@
/* create a new TCC compilation context */
TCCState *tcc_new(void);
+#ifdef _WIN32
+/* when libtcc linked to DLL, use this from DllMain() */
+void tcc_set_lib_path(void *module_handle);
+#endif
+
/* free a TCC compilation context */
void tcc_delete(TCCState *s);
@@ -60,6 +65,7 @@
#define TCC_OUTPUT_EXE 1 /* executable file */
#define TCC_OUTPUT_DLL 2 /* dynamic library */
#define TCC_OUTPUT_OBJ 3 /* object file */
+#define TCC_OUTPUT_PREPROCESS 4 /* preprocessed file (used internally) */
int tcc_set_output_type(TCCState *s, int output_type);
#define TCC_OUTPUT_FORMAT_ELF 0 /* default output format: ELF */
diff -Nur tcc-0.9.23.orig/Makefile tcc-0.9.23/Makefile
--- tcc-0.9.23.orig/Makefile 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/Makefile 2007-01-27 18:23:59.000000000 +0100
@@ -3,7 +3,7 @@
#
include config.mak
-CFLAGS=-O2 -g -Wall
+CFLAGS+=-g -Wall
ifndef CONFIG_WIN32
LIBS=-ldl
BCHECK_O=bcheck.o
@@ -171,7 +171,7 @@
ifdef CONFIG_WIN32
# for windows, we must use TCC because we generate ELF objects
LIBTCC1_OBJS=$(addprefix win32/lib/, crt1.o wincrt1.o dllcrt1.o dllmain.o chkstk.o) libtcc1.o
-LIBTCC1_CC=./tcc.exe -Bwin32
+LIBTCC1_CC=./tcc.exe -Bwin32 -DTCC_TARGET_PE -DWIN32
else
LIBTCC1_OBJS=libtcc1.o
LIBTCC1_CC=$(CC)
@@ -192,28 +192,28 @@
install: tcc_install libinstall
tcc_install: $(PROGS) tcc.1 libtcc1.a $(BCHECK_O) tcc-doc.html tcc.1
- mkdir -p "$(bindir)"
- $(INSTALL) -s -m755 $(PROGS) "$(bindir)"
+ mkdir -p "$(DESTDIR)$(bindir)"
+ $(INSTALL) -s -m755 $(PROGS) "$(DESTDIR)$(bindir)"
ifndef CONFIG_WIN32
- mkdir -p "$(mandir)/man1"
- $(INSTALL) tcc.1 "$(mandir)/man1"
+ mkdir -p "$(DESTDIR)$(mandir)/man1"
+ $(INSTALL) tcc.1 "$(DESTDIR)$(mandir)/man1"
endif
- mkdir -p "$(tccdir)"
- mkdir -p "$(tccdir)/include"
+ mkdir -p "$(DESTDIR)$(tccdir)"
+ mkdir -p "$(DESTDIR)$(tccdir)/include"
ifdef CONFIG_WIN32
- mkdir -p "$(tccdir)/lib"
- $(INSTALL) -m644 libtcc1.a win32/lib/*.def "$(tccdir)/lib"
- cp -r win32/include/. "$(tccdir)/include"
- cp -r win32/examples/. "$(tccdir)/examples"
+ mkdir -p "$(DESTDIR)$(tccdir)/lib"
+ $(INSTALL) -m644 libtcc1.a win32/lib/*.def "$(DESTDIR)$(tccdir)/lib"
+ cp -r win32/include/. "$(DESTDIR)$(tccdir)/include"
+ cp -r win32/examples/. "$(DESTDIR)$(tccdir)/examples"
else
- $(INSTALL) -m644 libtcc1.a $(BCHECK_O) "$(tccdir)"
+ $(INSTALL) -m644 libtcc1.a $(BCHECK_O) "$(DESTDIR)$(tccdir)"
$(INSTALL) -m644 stdarg.h stddef.h stdbool.h float.h varargs.h \
- tcclib.h "$(tccdir)/include"
+ tcclib.h "$(DESTDIR)$(tccdir)/include"
endif
- mkdir -p "$(docdir)"
- $(INSTALL) -m644 tcc-doc.html "$(docdir)"
+ mkdir -p "$(DESTDIR)$(docdir)"
+ $(INSTALL) -m644 tcc-doc.html "$(DESTDIR)$(docdir)"
ifdef CONFIG_WIN32
- $(INSTALL) -m644 win32/readme.txt "$(docdir)"
+ $(INSTALL) -m644 win32/readme.txt "$(DESTDIR)$(docdir)"
endif
clean:
@@ -231,13 +231,13 @@
# libtcc generation and example
libinstall: libtcc.a
- mkdir -p "$(libdir)"
- $(INSTALL) -m644 libtcc.a "$(libdir)"
- mkdir -p "$(includedir)"
- $(INSTALL) -m644 libtcc.h "$(includedir)"
+ mkdir -p "$(DESTDIR)$(libdir)"
+ $(INSTALL) -m644 libtcc.a "$(DESTDIR)$(libdir)"
+ mkdir -p "$(DESTDIR)$(includedir)"
+ $(INSTALL) -m644 libtcc.h "$(DESTDIR)$(includedir)"
libtcc.o: tcc.c i386-gen.c Makefile
- $(CC) $(CFLAGS) -DLIBTCC -c -o $@ $<
+ $(CC) $(CFLAGS) -DLIBTCC -DTCC_TARGET_PE -c -o $@ $<
libtcc.a: libtcc.o
$(AR) rcs $@ $^
diff -Nur tcc-0.9.23.orig/mingw-cross-build.sh tcc-0.9.23/mingw-cross-build.sh
--- tcc-0.9.23.orig/mingw-cross-build.sh 1970-01-01 01:00:00.000000000 +0100
+++ tcc-0.9.23/mingw-cross-build.sh 2007-01-27 18:45:27.000000000 +0100
@@ -0,0 +1,17 @@
+#!/bin/sh
+#
+# Build script for cross compiling TCC for Win32
+# for integration into SQLite ODBC driver.
+# Tested on Fedora Core 3 and 5.
+#
+# Cross toolchain and NSIS for Linux/i386 can be fetched from
+# http://www.ch-werner.de/xtools/cross-mingw32-3.1-3.i386.rpm
+# http://www.ch-werner.de/xtools/nsis-2.11-1.i386.rpm
+
+set -e
+
+rm -rf ../TCC
+sh cross-configure.sh --prefix=/TCC --enable-mingw32
+sh cross-make.sh clean all
+sh cross-make.sh DESTDIR=.. install
+sh cross-make.sh distclean
diff -Nur tcc-0.9.23.orig/stdarg.h tcc-0.9.23/stdarg.h
--- tcc-0.9.23.orig/stdarg.h 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/stdarg.h 2007-01-27 18:25:17.000000000 +0100
@@ -6,6 +6,7 @@
/* only correct for i386 */
#define va_start(ap,last) ap = ((char *)&(last)) + ((sizeof(last)+3)&~3)
#define va_arg(ap,type) (ap += (sizeof(type)+3)&~3, *(type *)(ap - ((sizeof(type)+3)&~3)))
+#define va_copy(dest, src) (dest) = (src)
#define va_end(ap)
/* fix a buggy dependency on GCC in libio.h */
diff -Nur tcc-0.9.23.orig/tccasm.c tcc-0.9.23/tccasm.c
--- tcc-0.9.23.orig/tccasm.c 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/tccasm.c 2007-01-27 18:26:24.000000000 +0100
@@ -916,21 +916,23 @@
nb_outputs = nb_operands;
if (tok == ':') {
next();
- /* input args */
- parse_asm_operands(operands, &nb_operands, 0);
- if (tok == ':') {
- /* clobber list */
- /* XXX: handle registers */
- next();
- for(;;) {
- if (tok != TOK_STR)
- expect("string constant");
- asm_clobber(clobber_regs, tokc.cstr->data);
+ if (tok != ')') {
+ /* input args */
+ parse_asm_operands(operands, &nb_operands, 0);
+ if (tok == ':') {
+ /* clobber list */
+ /* XXX: handle registers */
next();
- if (tok == ',') {
+ for(;;) {
+ if (tok != TOK_STR)
+ expect("string constant");
+ asm_clobber(clobber_regs, tokc.cstr->data);
next();
- } else {
- break;
+ if (tok == ',') {
+ next();
+ } else {
+ break;
+ }
}
}
}
diff -Nur tcc-0.9.23.orig/tcc.c tcc-0.9.23/tcc.c
--- tcc-0.9.23.orig/tcc.c 2005-06-18 00:09:15.000000000 +0200
+++ tcc-0.9.23/tcc.c 2007-01-28 22:28:55.000000000 +0100
@@ -44,10 +44,15 @@
#ifndef WIN32
#include <sys/time.h>
#include <sys/ucontext.h>
+#include <sys/mman.h>
#endif
#endif /* !CONFIG_TCCBOOT */
+#ifndef PAGESIZE
+#define PAGESIZE 4096
+#endif
+
#include "elf.h"
#include "stab.h"
@@ -131,9 +136,15 @@
char str[1];
} TokenSym;
+#ifdef TCC_TARGET_PE
+typedef unsigned short nwchar_t;
+#else
+typedef int nwchar_t;
+#endif
+
typedef struct CString {
int size; /* size in bytes */
- void *data; /* either 'char *' or 'int *' */
+ void *data; /* either 'char *' or 'nwchar_t *' */
int size_allocated;
void *data_allocated; /* if non NULL, data has been malloced */
} CString;
@@ -199,7 +210,7 @@
int sh_entsize; /* elf entry size */
unsigned long sh_size; /* section size (only used during output) */
unsigned long sh_addr; /* address at which the section is relocated */
- unsigned long sh_offset; /* address at which the section is relocated */
+ unsigned long sh_offset; /* file offset */
int nb_hashed_syms; /* used to resize the hash table */
struct Section *link; /* link to another section */
struct Section *reloc; /* corresponding section for relocation, if any */
@@ -237,6 +248,7 @@
#define FUNC_FASTCALL1 2 /* first param in %eax */
#define FUNC_FASTCALL2 3 /* first parameters in %eax, %edx */
#define FUNC_FASTCALL3 4 /* first parameter in %eax, %edx, %ecx */
+#define FUNC_FASTCALLW 5 /* first parameter in %ecx, %edx */
/* field 'Sym.t' for macros */
#define MACRO_OBJ 0 /* object like macro */
@@ -491,6 +503,9 @@
/* pack stack */
int pack_stack[PACK_STACK_SIZE];
int *pack_stack_ptr;
+
+ /* output file for preprocessing */
+ FILE *outfile;
};
/* The current value can be: */
@@ -704,11 +719,24 @@
#define TOK_UIDENT TOK_DEFINE
#ifdef WIN32
+
+#define PAGE_NOACCESS 0x0001
+#define PAGE_READONLY 0x0002
+#define PAGE_READWRITE 0x0004
+#define PAGE_WRITECOPY 0x0008
+#define PAGE_EXECUTE 0x0010
+#define PAGE_EXECUTE_READ 0x0020
+#define PAGE_EXECUTE_READWRITE 0x0040
+#define PAGE_EXECUTE_WRITECOPY 0x0080
+#define PAGE_GUARD 0x0100
+#define PAGE_NOCACHE 0x0200
+
int __stdcall GetModuleFileNameA(void *, char *, int);
void *__stdcall GetProcAddress(void *, const char *);
void *__stdcall GetModuleHandleA(const char *);
void *__stdcall LoadLibraryA(const char *);
int __stdcall FreeConsole(void);
+int __stdcall VirtualProtect(void *, int, int, int *);
#define snprintf _snprintf
#define vsnprintf _vsnprintf
@@ -785,7 +813,9 @@
static int lvalue_type(int t);
static int parse_btype(CType *type, AttributeDef *ad);
static void type_decl(CType *type, AttributeDef *ad, int *v, int td);
+static int compare_types(CType *type1, CType *type2, int unqualified);
static int is_compatible_types(CType *type1, CType *type2);
+static int is_compatible_parameter_types(CType *type1, CType *type2);
int ieee_finite(double d);
void error(const char *fmt, ...);
@@ -826,6 +856,7 @@
#define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
#define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
+#define AFF_PREPROCESS 0x0004 /* preprocess file */
static int tcc_add_file_internal(TCCState *s, const char *filename, int flags);
/* tcccoff.c */
@@ -833,7 +864,7 @@
/* tccpe.c */
void *resolve_sym(TCCState *s1, const char *sym, int type);
-int pe_load_def_file(struct TCCState *s1, FILE *fp);
+int pe_load_def_file(struct TCCState *s1, FILE *fp, int output_type);
void pe_setup_paths(struct TCCState *s1, int *p_output_type, const char **p_outfile, char *first_file);
unsigned long pe_add_runtime(struct TCCState *s1);
int tcc_output_pe(struct TCCState *s1, const char *filename);
@@ -1556,10 +1587,10 @@
static void cstr_wccat(CString *cstr, int ch)
{
int size;
- size = cstr->size + sizeof(int);
+ size = cstr->size + sizeof(nwchar_t);
if (size > cstr->size_allocated)
cstr_realloc(cstr, size);
- *(int *)(((unsigned char *)cstr->data) + size - sizeof(int)) = ch;
+ *(nwchar_t *)(((unsigned char *)cstr->data) + size - sizeof(nwchar_t)) = ch;
cstr->size = size;
}
@@ -1649,9 +1680,9 @@
for(i=0;i<len;i++)
add_char(&cstr_buf, ((unsigned char *)cstr->data)[i]);
} else {
- len = (cstr->size / sizeof(int)) - 1;
+ len = (cstr->size / sizeof(nwchar_t)) - 1;
for(i=0;i<len;i++)
- add_char(&cstr_buf, ((int *)cstr->data)[i]);
+ add_char(&cstr_buf, ((nwchar_t *)cstr->data)[i]);
}
cstr_ccat(&cstr_buf, '\"');
cstr_ccat(&cstr_buf, '\0');
@@ -1662,6 +1693,8 @@
case TOK_GT:
v = '>';
goto addv;
+ case TOK_DOTS:
+ return strcpy(p, "...");
case TOK_A_SHL:
return strcpy(p, "<<=");
case TOK_A_SAR:
@@ -1820,6 +1853,7 @@
{
int fd;
BufferedFile *bf;
+ int i, len;
fd = open(filename, O_RDONLY | O_BINARY);
if (fd < 0)
@@ -1834,6 +1868,10 @@
bf->buf_end = bf->buffer;
bf->buffer[0] = CH_EOB; /* put eob symbol */
pstrcpy(bf->filename, sizeof(bf->filename), filename);
+ len = strlen(bf->filename);
+ for (i = 0; i < len; i++)
+ if (bf->filename[i] == '\\')
+ bf->filename[i] = '/';
bf->line_num = 1;
bf->ifndef_macro = 0;
bf->ifdef_stack_ptr = s1->ifdef_stack_ptr;
@@ -3724,7 +3762,7 @@
if (!is_long)
char_size = 1;
else
- char_size = sizeof(int);
+ char_size = sizeof(nwchar_t);
if (tokcstr.size <= char_size)
error("empty character constant");
if (tokcstr.size > 2 * char_size)
@@ -3733,7 +3771,7 @@
tokc.i = *(int8_t *)tokcstr.data;
tok = TOK_CCHAR;
} else {
- tokc.i = *(int *)tokcstr.data;
+ tokc.i = *(nwchar_t *)tokcstr.data;
tok = TOK_LCHAR;
}
} else {
@@ -4216,7 +4254,10 @@
/* if number, then create a number token */
/* NOTE: no need to allocate because
tok_str_add2() does it */
- tokc.cstr = &cstr;
+ cstr_reset(&tokcstr);
+ tokcstr = cstr;
+ cstr_new(&cstr);
+ tokc.cstr = &tokcstr;
} else {
/* if identifier, we must do a test to
validate we have a correct identifier */
@@ -4586,7 +4627,7 @@
#ifdef TCC_TARGET_I386
/* x86 specific: need to pop fp register ST0 if saved */
if (r == TREG_ST0) {
- o(0xd9dd); /* fstp %st(1) */
+ o(0xd8dd); /* fstp %st(0) */
}
#endif
/* special long long case */
@@ -5010,7 +5051,7 @@
#ifdef TCC_TARGET_I386
/* for x86, we need to pop the FP stack */
if (v == TREG_ST0 && !nocode_wanted) {
- o(0xd9dd); /* fstp %st(1) */
+ o(0xd8dd); /* fstp %st(0) */
} else
#endif
if (v == VT_JMP || v == VT_JMPI) {
@@ -5735,6 +5776,10 @@
bits = 32 - bits;
vpushi(bits);
gen_op(TOK_SHL);
+ /* result must be signed or the SAR is converted to an SHL
+ This was not the case when "t" was a signed short
+ and the last value on the stack was an unsigned int */
+ vtop->type.t &= ~VT_UNSIGNED;
vpushi(bits);
gen_op(TOK_SAR);
}
@@ -5822,6 +5867,7 @@
/* we handle char/short/etc... with generic code */
if (dbt != (VT_INT | VT_UNSIGNED) &&
dbt != (VT_LLONG | VT_UNSIGNED) &&
+ dbt != VT_BOOL &&
dbt != VT_LLONG)
dbt = VT_INT;
if (c) {
@@ -5837,6 +5883,10 @@
case VT_LDOUBLE: vtop->c.ui = (unsigned int)vtop->c.d; break;
}
break;
+ case VT_BOOL:
+ vpushi(0);
+ gen_op(TOK_NE);
+ break;
default:
/* int case */
switch(sbt) {
@@ -5980,7 +6030,7 @@
while (s1 != NULL) {
if (s2 == NULL)
return 0;
- if (!is_compatible_types(&s1->type, &s2->type))
+ if (!is_compatible_parameter_types(&s1->type, &s2->type))
return 0;
s1 = s1->next;
s2 = s2->next;
@@ -5990,17 +6040,22 @@
return 1;
}
-/* return true if type1 and type2 are exactly the same (including
- qualifiers).
+/* return true if type1 and type2 are the same. If unqualified is
+ true, qualifiers on the types are ignored.
- enums are not checked as gcc __builtin_types_compatible_p ()
*/
-static int is_compatible_types(CType *type1, CType *type2)
+static int compare_types(CType *type1, CType *type2, int unqualified)
{
int bt1, t1, t2;
t1 = type1->t & VT_TYPE;
t2 = type2->t & VT_TYPE;
+ if (unqualified) {
+ /* strip qualifiers before comparing */
+ t1 &= ~(VT_CONSTANT | VT_VOLATILE);
+ t2 &= ~(VT_CONSTANT | VT_VOLATILE);
+ }
/* XXX: bitfields ? */
if (t1 != t2)
return 0;
@@ -6019,6 +6074,21 @@
}
}
+/* return true if type1 and type2 are exactly the same (including
+ qualifiers).
+*/
+static int is_compatible_types(CType *type1, CType *type2)
+{
+ return compare_types(type1,type2,0);
+}
+
+/* return true if type1 and type2 are the same (ignoring qualifiers).
+*/
+static int is_compatible_parameter_types(CType *type1, CType *type2)
+{
+ return compare_types(type1,type2,1);
+}
+
/* print a type. If 'varstr' is not NULL, then the variable is also
printed in the type */
/* XXX: union */
@@ -6411,6 +6481,11 @@
ad->func_call = FUNC_FASTCALL1 + n - 1;
skip(')');
break;
+ case TOK_FASTCALL1:
+ case TOK_FASTCALL2:
+ case TOK_FASTCALL3:
+ ad->func_call = FUNC_FASTCALLW;
+ break;
#endif
case TOK_DLLEXPORT:
ad->dllexport = 1;
@@ -6442,7 +6517,7 @@
{
int a, v, size, align, maxalign, c, offset;
int bit_size, bit_pos, bsize, bt, lbit_pos;
- Sym *s, *ss, **ps;
+ Sym *s, *ss, *ass, **ps;
AttributeDef ad;
CType type1, btype;
@@ -6513,7 +6588,9 @@
v = 0;
type1 = btype;
if (tok != ':') {
- type_decl(&type1, &ad, &v, TYPE_DIRECT);
+ type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
+ if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
+ expect("identifier");
if ((type1.t & VT_BTYPE) == VT_FUNC ||
(type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
error("invalid type for '%s'",
@@ -6576,7 +6653,7 @@
} else {
bit_pos = 0;
}
- if (v) {
+ if (v != 0 || (type1.t & VT_BTYPE) == VT_STRUCT) {
/* add new memory data only if starting
bit field */
if (lbit_pos == 0) {
@@ -6602,6 +6679,15 @@
}
printf("\n");
#endif
+ }
+ if (v == 0 && (type1.t & VT_BTYPE) == VT_STRUCT) {
+ ass = type1.ref;
+ while ((ass = ass->next) != NULL) {
+ ss = sym_push(ass->v, &ass->type, 0, offset + ass->c);
+ *ps = ss;
+ ps = &ss->next;
+ }
+ } else if (v) {
ss = sym_push(v | SYM_FIELD, &type1, 0, offset);
*ps = ss;
ps = &ss->next;
@@ -6772,6 +6858,7 @@
t |= (s->type.t & ~VT_TYPEDEF);
type->ref = s->type.ref;
next();
+ typespec_found = 1;
break;
}
type_found = 1;
@@ -6819,35 +6906,39 @@
l = 0;
first = NULL;
plast = &first;
- while (tok != ')') {
- /* read param name and compute offset */
- if (l != FUNC_OLD) {
- if (!parse_btype(&pt, &ad1)) {
- if (l) {
- error("invalid type");
- } else {
- l = FUNC_OLD;
- goto old_proto;
+ if (tok != ')') {
+ for(;;) {
+ /* read param name and compute offset */
+ if (l != FUNC_OLD) {
+ if (!parse_btype(&pt, &ad1)) {
+ if (l) {
+ error("invalid type");
+ } else {
+ l = FUNC_OLD;
+ goto old_proto;
+ }
}
+ l = FUNC_NEW;
+ if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
+ break;
+ type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
+ if ((pt.t & VT_BTYPE) == VT_VOID)
+ error("parameter declared as void");
+ } else {
+ old_proto:
+ n = tok;
+ if (n < TOK_UIDENT)
+ expect("identifier");
+ pt.t = VT_INT;
+ next();
}
- l = FUNC_NEW;
- if ((pt.t & VT_BTYPE) == VT_VOID && tok == ')')
+ convert_parameter_type(&pt);
+ s = sym_push(n | SYM_FIELD, &pt, 0, 0);
+ *plast = s;
+ plast = &s->next;
+ if (tok == ')')
break;
- type_decl(&pt, &ad1, &n, TYPE_DIRECT | TYPE_ABSTRACT);
- if ((pt.t & VT_BTYPE) == VT_VOID)
- error("parameter declared as void");
- } else {
- old_proto:
- n = tok;
- pt.t = VT_INT;
- next();
- }
- convert_parameter_type(&pt);
- s = sym_push(n | SYM_FIELD, &pt, 0, 0);
- *plast = s;
- plast = &s->next;
- if (tok == ',') {
- next();
+ skip(',');
if (l == FUNC_NEW && tok == TOK_DOTS) {
l = FUNC_ELLIPSIS;
next();
@@ -7129,7 +7220,11 @@
}
break;
case TOK_LSTR:
+#ifdef TCC_TARGET_PE
+ t = VT_SHORT | VT_UNSIGNED;
+#else
t = VT_INT;
+#endif
goto str_init;
case TOK_STR:
/* string parsing */
@@ -7726,6 +7821,9 @@
} else if (bt1 == VT_PTR || bt2 == VT_PTR) {
/* XXX: test pointer compatibility */
type = type1;
+ } else if (bt1 == VT_FUNC || bt2 == VT_FUNC) {
+ /* XXX: test function pointer compatibility */
+ type = type1;
} else if (bt1 == VT_STRUCT || bt2 == VT_STRUCT) {
/* XXX: test structure compatibility */
type = type1;
@@ -8403,6 +8501,9 @@
no_oblock = 1;
if ((first && tok != TOK_LSTR && tok != TOK_STR) ||
tok == '{') {
+ if (tok != '{')
+ error("character array initializer must be a literal,"
+ " optionally enclosed in braces");
skip('{');
no_oblock = 0;
}
@@ -8410,7 +8511,11 @@
/* only parse strings here if correct type (otherwise: handle
them as ((w)char *) expressions */
if ((tok == TOK_LSTR &&
+#ifdef TCC_TARGET_PE
+ (t1->t & VT_BTYPE) == VT_SHORT && (t1->t & VT_UNSIGNED)) ||
+#else
(t1->t & VT_BTYPE) == VT_INT) ||
+#endif
(tok == TOK_STR &&
(t1->t & VT_BTYPE) == VT_BYTE)) {
while (tok == TOK_STR || tok == TOK_LSTR) {
@@ -8422,7 +8527,7 @@
if (tok == TOK_STR)
cstr_len = cstr->size;
else
- cstr_len = cstr->size / sizeof(int);
+ cstr_len = cstr->size / sizeof(nwchar_t);
cstr_len--;
nb = cstr_len;
if (n >= 0 && nb > (n - array_length))
@@ -8440,7 +8545,7 @@
if (tok == TOK_STR)
ch = ((unsigned char *)cstr->data)[i];
else
- ch = ((int *)cstr->data)[i];
+ ch = ((nwchar_t *)cstr->data)[i];
init_putv(t1, sec, c + (array_length + i) * size1,
ch, EXPR_VAL);
}
@@ -8986,7 +9091,7 @@
#if 0
{
char buf[500];
- type_to_str(buf, sizeof(buf), t, get_tok_str(v, NULL));
+ type_to_str(buf, sizeof(buf), &type, get_tok_str(v, NULL));
printf("type = '%s'\n", buf);
}
#endif
@@ -9001,7 +9106,7 @@
if (tok == '{') {
if (l == VT_LOCAL)
error("cannot use local functions");
- if (!(type.t & VT_FUNC))
+ if ((type.t & VT_BTYPE) != VT_FUNC)
expect("function definition");
/* reject abstract declarators in function definition */
@@ -9233,6 +9338,47 @@
return s1->nb_errors != 0 ? -1 : 0;
}
+/* Preprocess the current file */
+/* XXX: add line and file infos, add options to preserve spaces */
+static int tcc_preprocess(TCCState *s1)
+{
+ Sym *define_start;
+ int last_is_space;
+
+ preprocess_init(s1);
+
+ define_start = define_stack;
+
+ ch = file->buf_ptr[0];
+ tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
+ parse_flags = PARSE_FLAG_ASM_COMMENTS | PARSE_FLAG_PREPROCESS |
+ PARSE_FLAG_LINEFEED;
+ last_is_space = 1;
+ next();
+ for(;;) {
+ if (tok == TOK_EOF)
+ break;
+ if (!last_is_space) {
+ fputc(' ', s1->outfile);
+ }
+ fputs(get_tok_str(tok, &tokc), s1->outfile);
+ if (tok == TOK_LINEFEED) {
+ last_is_space = 1;
+ /* XXX: suppress that hack */
+ parse_flags &= ~PARSE_FLAG_LINEFEED;
+ next();
+ parse_flags |= PARSE_FLAG_LINEFEED;
+ } else {
+ last_is_space = 0;
+ next();
+ }
+ }
+
+ free_defines(define_start);
+
+ return 0;
+}
+
#ifdef LIBTCC
int tcc_compile_string(TCCState *s, const char *str)
{
@@ -9615,6 +9761,30 @@
if (s->reloc)
relocate_section(s1, s);
}
+
+ /* mark executable sections as executable in memory */
+ for(i = 1; i < s1->nb_sections; i++) {
+ s = s1->sections[i];
+ if ((s->sh_flags & (SHF_ALLOC | SHF_EXECINSTR)) ==
+ (SHF_ALLOC | SHF_EXECINSTR)) {
+#ifdef WIN32
+ {
+ int old_protect;
+ VirtualProtect(s->data, s->data_offset,
+ PAGE_EXECUTE_READWRITE, &old_protect);
+ }
+#else
+ {
+ unsigned long start, end;
+ start = (unsigned long)(s->data) & ~(PAGESIZE - 1);
+ end = (unsigned long)(s->data + s->data_offset);
+ end = (end + PAGESIZE - 1) & ~(PAGESIZE - 1);
+ mprotect((void *)start, end - start,
+ PROT_READ | PROT_WRITE | PROT_EXEC);
+ }
+#endif