-
-
Notifications
You must be signed in to change notification settings - Fork 240
/
wrappedlibc.c
3578 lines (3351 loc) · 120 KB
/
wrappedlibc.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
#define _LARGEFILE_SOURCE 1
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <wchar.h>
#include <dlfcn.h>
#include <signal.h>
#include <errno.h>
#include <err.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <unistd.h>
#include <fcntl.h>
#include <glob.h>
#include <ctype.h>
#include <dirent.h>
#include <search.h>
#include <sys/types.h>
#include <poll.h>
#include <sys/epoll.h>
#include <ftw.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#include <sys/utsname.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <setjmp.h>
#include <sys/vfs.h>
#include <spawn.h>
#include <fts.h>
#include <syslog.h>
#include <malloc.h>
#include <getopt.h>
#include <sys/resource.h>
#include <sys/prctl.h>
#include <sys/ptrace.h>
#undef LOG_INFO
#undef LOG_DEBUG
#include "wrappedlibs.h"
#include "box64stack.h"
#include "x64emu.h"
#include "debug.h"
#include "wrapper.h"
#include "bridge.h"
#include "callback.h"
#include "librarian.h"
#include "librarian/library_private.h"
#include "emu/x64emu_private.h"
#include "box64context.h"
#include "myalign.h"
#include "signals.h"
#include "fileutils.h"
#include "auxval.h"
#include "elfloader.h"
#include "bridge.h"
#include "globalsymbols.h"
#include "rcfile.h"
#ifndef LOG_INFO
#define LOG_INFO 1
#endif
#ifndef LOG_DEBUG
#define LOG_DEBUG 2
#endif
#define LIBNAME libc
const char* libcName =
#ifdef ANDROID
"libc.so"
#else
"libc.so.6"
#endif
;
typedef int (*iFi_t)(int);
typedef int (*iFp_t)(void*);
typedef int (*iFL_t)(unsigned long);
typedef void (*vFpp_t)(void*, void*);
typedef void (*vFipp_t)(int32_t, void*, void*);
typedef int32_t (*iFpi_t)(void*, int32_t);
typedef int32_t (*iFpp_t)(void*, void*);
typedef int32_t (*iFpL_t)(void*, size_t);
typedef int32_t (*iFiip_t)(int32_t, int32_t, void*);
typedef int32_t (*iFipp_t)(int32_t, void*, void*);
typedef int32_t (*iFppi_t)(void*, void*, int32_t);
typedef int32_t (*iFpup_t)(void*, uint32_t, void*);
typedef int32_t (*iFpuu_t)(void*, uint32_t, uint32_t);
typedef int32_t (*iFiiII_t)(int, int, int64_t, int64_t);
typedef int32_t (*iFiiiV_t)(int, int, int, ...);
typedef int32_t (*iFippi_t)(int32_t, void*, void*, int32_t);
typedef int32_t (*iFpppp_t)(void*, void*, void*, void*);
typedef int32_t (*iFpipp_t)(void*, int32_t, void*, void*);
typedef int32_t (*iFppii_t)(void*, void*, int32_t, int32_t);
typedef int32_t (*iFipuu_t)(int32_t, void*, uint32_t, uint32_t);
typedef int32_t (*iFipiI_t)(int32_t, void*, int32_t, int64_t);
typedef int32_t (*iFipuup_t)(int32_t, void*, uint32_t, uint32_t, void*);
typedef int32_t (*iFiiV_t)(int32_t, int32_t, ...);
typedef void* (*pFp_t)(void*);
typedef void* (*pFpip_t)(void*, int, void*);
#define SUPER() \
GO(_ITM_addUserCommitAction, iFpup_t) \
GO(_IO_file_stat, iFpp_t) \
GO(fts64_open, pFpip_t) \
GO(register_printf_specifier, iFipp_t) \
GO(register_printf_type, iFp_t)
#include "wrappercallback.h"
static int regs_abi[] = {_DI, _SI, _DX, _CX, _R8, _R9};
void* getVargN(x64emu_t *emu, int n)
{
if(n<6)
return (void*)emu->regs[regs_abi[n]].q[0];
return ((void**)R_RSP)[1+n-6];
}
// utility functions
#define SUPER() \
GO(0) \
GO(1) \
GO(2) \
GO(3) \
GO(4) \
GO(5) \
GO(6) \
GO(7) \
GO(8) \
GO(9) \
GO(10) \
GO(11) \
GO(12) \
GO(13) \
GO(14) \
GO(15)
// compare
#define GO(A) \
static uintptr_t my_compare_fct_##A = 0; \
static int my_compare_##A(void* a, void* b) \
{ \
return (int)RunFunctionFmt(my_compare_fct_##A, "pp", a, b); \
}
SUPER()
#undef GO
static void* findcompareFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_compare_fct_##A == (uintptr_t)fct) return my_compare_##A;
SUPER()
#undef GO
#define GO(A) if(my_compare_fct_##A == 0) {my_compare_fct_##A = (uintptr_t)fct; return my_compare_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc compare callback\n");
return NULL;
}
// action
#define GO(A) \
static uintptr_t my_action_fct_##A = 0; \
static void my_action_##A(void* a, uint32_t b, int c) \
{ \
RunFunctionFmt(my_action_fct_##A, "pui", a, b, c); \
}
SUPER()
#undef GO
static void* findactionFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_action_fct_##A == (uintptr_t)fct) return my_action_##A;
SUPER()
#undef GO
#define GO(A) if(my_action_fct_##A == 0) {my_action_fct_##A = (uintptr_t)fct; return my_action_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc action callback\n");
return NULL;
}
// ftw64
#define GO(A) \
static uintptr_t my_ftw64_fct_##A = 0; \
static int my_ftw64_##A(void* fpath, void* sb, int flag) \
{ \
struct x64_stat64 x64st; \
UnalignStat64(sb, &x64st); \
return (int)RunFunctionFmt(my_ftw64_fct_##A, "ppi", fpath, &x64st, flag); \
}
SUPER()
#undef GO
static void* findftw64Fct(void* fct)
{
if(!fct) return NULL;
#define GO(A) if(my_ftw64_fct_##A == (uintptr_t)fct) return my_ftw64_##A;
SUPER()
#undef GO
#define GO(A) if(my_ftw64_fct_##A == 0) {my_ftw64_fct_##A = (uintptr_t)fct; return my_ftw64_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc ftw64 callback\n");
return NULL;
}
// nftw64
#define GO(A) \
static uintptr_t my_nftw64_fct_##A = 0; \
static int my_nftw64_##A(void* fpath, void* sb, int flag, void* ftwbuff) \
{ \
struct x64_stat64 x64st; \
UnalignStat64(sb, &x64st); \
return (int)RunFunctionFmt(my_nftw64_fct_##A, "ppip", fpath, &x64st, flag, ftwbuff); \
}
SUPER()
#undef GO
static void* findnftw64Fct(void* fct)
{
if(!fct) return NULL;
#define GO(A) if(my_nftw64_fct_##A == (uintptr_t)fct) return my_nftw64_##A;
SUPER()
#undef GO
#define GO(A) if(my_nftw64_fct_##A == 0) {my_nftw64_fct_##A = (uintptr_t)fct; return my_nftw64_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc nftw64 callback\n");
return NULL;
}
// globerr
#define GO(A) \
static uintptr_t my_globerr_fct_##A = 0; \
static int my_globerr_##A(void* epath, int eerrno) \
{ \
return (int)RunFunctionFmt(my_globerr_fct_##A, "pi", epath, eerrno); \
}
SUPER()
#undef GO
static void* findgloberrFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_globerr_fct_##A == (uintptr_t)fct) return my_globerr_##A;
SUPER()
#undef GO
#define GO(A) if(my_globerr_fct_##A == 0) {my_globerr_fct_##A = (uintptr_t)fct; return my_globerr_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc globerr callback\n");
return NULL;
}
// free
#define GO(A) \
static uintptr_t my_free_fct_##A = 0; \
static void my_free_##A(void* p) \
{ \
RunFunctionFmt(my_free_fct_##A, "p", p); \
}
SUPER()
#undef GO
static void* findfreeFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_free_fct_##A == (uintptr_t)fct) return my_free_##A;
SUPER()
#undef GO
#define GO(A) if(my_free_fct_##A == 0) {my_free_fct_##A = (uintptr_t)fct; return my_free_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc free callback\n");
return NULL;
}
#if 0
#undef dirent
// filter_dir
#define GO(A) \
static uintptr_t my_filter_dir_fct_##A = 0; \
static int my_filter_dir_##A(const struct dirent* a) \
{ \
return (int)RunFunctionFmt(my_filter_dir_fct_##A, "p", a); \
}
SUPER()
#undef GO
static void* findfilter_dirFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_filter_dir_fct_##A == (uintptr_t)fct) return my_filter_dir_##A;
SUPER()
#undef GO
#define GO(A) if(my_filter_dir_fct_##A == 0) {my_filter_dir_fct_##A = (uintptr_t)fct; return my_filter_dir_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc filter_dir callback\n");
return NULL;
}
// compare_dir
#define GO(A) \
static uintptr_t my_compare_dir_fct_##A = 0; \
static int my_compare_dir_##A(const struct dirent* a, const struct dirent* b) \
{ \
return (int)RunFunctionFmt(my_compare_dir_fct_##A, "pp", a, b); \
}
SUPER()
#undef GO
static void* findcompare_dirFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_compare_dir_fct_##A == (uintptr_t)fct) return my_compare_dir_##A;
SUPER()
#undef GO
#define GO(A) if(my_compare_dir_fct_##A == 0) {my_compare_dir_fct_##A = (uintptr_t)fct; return my_compare_dir_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc compare_dir callback\n");
return NULL;
}
#endif
// filter64
#define GO(A) \
static uintptr_t my_filter64_fct_##A = 0; \
static int my_filter64_##A(const struct dirent64* a) \
{ \
return (int)RunFunctionFmt(my_filter64_fct_##A, "p", a); \
}
SUPER()
#undef GO
static void* findfilter64Fct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_filter64_fct_##A == (uintptr_t)fct) return my_filter64_##A;
SUPER()
#undef GO
#define GO(A) if(my_filter64_fct_##A == 0) {my_filter64_fct_##A = (uintptr_t)fct; return my_filter64_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc filter64 callback\n");
return NULL;
}
// compare64
#define GO(A) \
static uintptr_t my_compare64_fct_##A = 0; \
static int my_compare64_##A(const struct dirent64* a, const struct dirent64* b) \
{ \
return (int)RunFunctionFmt(my_compare64_fct_##A, "pp", a, b); \
}
SUPER()
#undef GO
static void* findcompare64Fct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_compare64_fct_##A == (uintptr_t)fct) return my_compare64_##A;
SUPER()
#undef GO
#define GO(A) if(my_compare64_fct_##A == 0) {my_compare64_fct_##A = (uintptr_t)fct; return my_compare64_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc compare64 callback\n");
return NULL;
}
// printf_output
#define GO(A) \
static uintptr_t my_printf_output_fct_##A = 0; \
static int my_printf_output_##A(void* a, void* b, void* c) \
{ \
return (int)RunFunctionFmt(my_printf_output_fct_##A, "ppp", a, b, c); \
}
SUPER()
#undef GO
static void* findprintf_outputFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_printf_output_fct_##A == (uintptr_t)fct) return my_printf_output_##A;
SUPER()
#undef GO
#define GO(A) if(my_printf_output_fct_##A == 0) {my_printf_output_fct_##A = (uintptr_t)fct; return my_printf_output_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc printf_output callback\n");
return NULL;
}
// printf_arginfo
#define GO(A) \
static uintptr_t my_printf_arginfo_fct_##A = 0; \
static int my_printf_arginfo_##A(void* a, size_t b, void* c, void* d) \
{ \
return (int)RunFunctionFmt(my_printf_arginfo_fct_##A, "pLpp", a, b, c, d); \
}
SUPER()
#undef GO
static void* findprintf_arginfoFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_printf_arginfo_fct_##A == (uintptr_t)fct) return my_printf_arginfo_##A;
SUPER()
#undef GO
#define GO(A) if(my_printf_arginfo_fct_##A == 0) {my_printf_arginfo_fct_##A = (uintptr_t)fct; return my_printf_arginfo_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc printf_arginfo callback\n");
return NULL;
}
// printf_type
#define GO(A) \
static uintptr_t my_printf_type_fct_##A = 0; \
static void my_printf_type_##A(void* a, va_list* b) \
{ \
RunFunctionFmt(my_printf_type_fct_##A, "pp", a, b); \
}
SUPER()
#undef GO
static void* findprintf_typeFct(void* fct)
{
if(!fct) return NULL;
void* p;
if((p = GetNativeFnc((uintptr_t)fct))) return p;
#define GO(A) if(my_printf_type_fct_##A == (uintptr_t)fct) return my_printf_type_##A;
SUPER()
#undef GO
#define GO(A) if(my_printf_type_fct_##A == 0) {my_printf_type_fct_##A = (uintptr_t)fct; return my_printf_type_##A; }
SUPER()
#undef GO
printf_log(LOG_NONE, "Warning, no more slot for libc printf_type callback\n");
return NULL;
}
#undef SUPER
// some my_XXX declare and defines
int32_t my___libc_start_main(x64emu_t* emu, int (*main) (int, char * *, char * *),
int argc, char * * ubp_av, void (*init) (void), void (*fini) (void),
void (*rtld_fini) (void), void (* stack_end)); // implemented in x64run_private.c
EXPORT void my___libc_init_first(x64emu_t* emu, int argc, char* arg0, char** b)
{
// do nothing specific for now
(void)emu; (void)argc; (void)arg0; (void)b;
return;
}
uintptr_t my_syscall(x64emu_t *emu); // implemented in x64syscall.c
void EXPORT my___stack_chk_fail(x64emu_t* emu)
{
char buff[200];
#ifdef HAVE_TRACE
sprintf(buff, "%p: Stack is corrupted, aborting (prev IP=%p)\n", (void*)emu->old_ip, (void*)emu->prev2_ip);
#else
sprintf(buff, "%p: Stack is corrupted, aborting\n", (void*)emu->old_ip);
#endif
if(cycle_log) {
print_cycle_log(LOG_INFO);
}
StopEmu(emu, buff, emu->segs[_CS]==0x23);
}
void EXPORT my___gmon_start__(x64emu_t *emu)
{
(void)emu;
printf_log(LOG_DEBUG, "__gmon_start__ called (dummy call)\n");
}
int EXPORT my___cxa_atexit(x64emu_t* emu, void* p, void* a, void* dso_handle)
{
AddCleanup1Arg(emu, p, a, FindElfAddress(my_context, (uintptr_t)dso_handle));
return 0;
}
void EXPORT my___cxa_finalize(x64emu_t* emu, void* p)
{
if(!p) {
// p is null, call (and remove) all Cleanup functions
CallAllCleanup(emu);
return;
}
CallCleanup(emu, FindElfAddress(my_context, (uintptr_t)p));
}
int EXPORT my_atexit(x64emu_t* emu, void *p)
{
AddCleanup(emu, p);
return 0;
}
int my_getcontext(x64emu_t* emu, void* ucp);
int my_setcontext(x64emu_t* emu, void* ucp);
int my_makecontext(x64emu_t* emu, void* ucp, void* fnc, int32_t argc, void* argv);
int my_swapcontext(x64emu_t* emu, void* ucp1, void* ucp2);
// All signal and context functions defined in signals.c
// All fts function defined in myfts.c
// getauxval implemented in auxval.c
// this one is defined in elfloader.c
int my_dl_iterate_phdr(x64emu_t *emu, void* F, void *data);
pid_t EXPORT my_fork(x64emu_t* emu)
{
#if 1
emu->quit = 1;
emu->fork = 1; // use regular fork...
return 0;
#else
// execute atforks prepare functions, in reverse order
for (int i=my_context->atfork_sz-1; i>=0; --i)
if(my_context->atforks[i].prepare)
RunFunctionWithEmu(emu, 0, my_context->atforks[i].prepare, 0);
int type = emu->type;
pid_t v;
v = fork();
if(type == EMUTYPE_MAIN)
thread_set_emu(emu);
if(v<0) {
printf_log(LOG_NONE, "BOX64: Warning, fork errored... (%d)\n", v);
// error...
} else if(v>0) {
// execute atforks parent functions
for (int i=0; i<my_context->atfork_sz; --i)
if(my_context->atforks[i].parent)
RunFunctionWithEmu(emu, 0, my_context->atforks[i].parent, 0);
} else /*if(v==0)*/ {
// execute atforks child functions
for (int i=0; i<my_context->atfork_sz; --i)
if(my_context->atforks[i].child)
RunFunctionWithEmu(emu, 0, my_context->atforks[i].child, 0);
}
return v;
#endif
}
pid_t EXPORT my___fork(x64emu_t* emu) __attribute__((alias("my_fork")));
pid_t EXPORT my_vfork(x64emu_t* emu)
{
#if 1
emu->quit = 1;
emu->fork = 3; // use regular fork...
return 0;
#else
return 0;
#endif
}
int EXPORT my_uname(struct utsname *buf)
{
//TODO: check sizeof(struct utsname) == 390
int ret = uname(buf);
strcpy(buf->machine, "x86_64");
return ret;
}
// X86_O_RDONLY 0x00
#define X86_O_WRONLY 0x01 // octal 01
#define X86_O_RDWR 0x02 // octal 02
#define X86_FMODE_EXEC 0x20
#define X86_O_CREAT 0x40 // octal 0100
#define X86_O_EXCL 0x80 // octal 0200
#define X86_O_NOCTTY 0x100 // octal 0400
#define X86_O_TRUNC 0x200 // octal 01000
#define X86_O_APPEND 0x400 // octal 02000
#define X86_O_NONBLOCK 0x800 // octal 04000
#define X86_O_SYNC 0x101000 // octal 04010000
#define X86_O_DSYNC 0x1000 // octal 010000
#define X86_O_RSYNC O_SYNC
#define X86_FASYNC 020000
#define X86_O_DIRECT 040000
#define X86_O_LARGEFILE 0100000
#define X86_O_DIRECTORY 0200000
#define X86_O_NOFOLLOW 0400000
#define X86_O_NOATIME 01000000
#define X86_O_CLOEXEC 02000000
#define X86_O_PATH 010000000
#define X86_O_TMPFILE 020200000
#define X86_FMODE_NONOTIFY 0x4000000
#ifndef O_TMPFILE
#define O_TMPFILE (020000000 | O_DIRECTORY)
#endif
#ifndef FMODE_EXEC
#define FMODE_EXEC 0x20
#endif
#ifndef FMODE_NONOTIFY
#define FMODE_NONOTIFY 0x4000000
#endif
#define SUPER() \
GO(O_WRONLY) \
GO(O_RDWR) \
GO(FMODE_EXEC) \
GO(O_CREAT) \
GO(O_EXCL) \
GO(O_NOCTTY) \
GO(O_TRUNC) \
GO(O_APPEND) \
GO(O_NONBLOCK) \
GO(O_SYNC) \
GO(O_DSYNC) \
GO(O_RSYNC) \
GO(FASYNC) \
GO(O_DIRECT) \
GO(O_LARGEFILE) \
GO(O_TMPFILE) \
GO(O_DIRECTORY) \
GO(O_NOFOLLOW) \
GO(O_NOATIME) \
GO(O_CLOEXEC) \
GO(O_PATH) \
GO(FMODE_NONOTIFY)
// x86->arm
int of_convert(int a)
{
if(!a || a==-1) return a;
int b=0;
#define GO(A) if((a&(X86_##A))==(X86_##A)) {a&=~(X86_##A); b|=(A);}
SUPER();
#undef GO
if(a) {
printf_log(LOG_NONE, "Warning, of_convert(...) left over 0x%x, converted 0x%x\n", a, b);
}
return a|b;
}
// arm->x86
int of_unconvert(int a)
{
if(!a || a==-1) return a;
int b=0;
#define GO(A) if((a&(A))==(A)) {a&=~(A); b|=(X86_##A);}
SUPER();
#undef GO
// flags 0x20000 unknown?!
if(a && (a&~0x20000)) {
printf_log(LOG_NONE, "Warning, of_unconvert(...) left over 0x%x, converted 0x%x\n", a, b);
}
return a|b;
}
#undef SUPER
EXPORT void* my__ZGTtnaX (size_t a) { (void)a; printf("warning _ZGTtnaX called\n"); return NULL; }
EXPORT void my__ZGTtdlPv (void* a) { (void)a; printf("warning _ZGTtdlPv called\n"); }
EXPORT uint8_t my__ITM_RU1(const uint8_t * a) { (void)a; printf("warning _ITM_RU1 called\n"); return 0; }
EXPORT uint32_t my__ITM_RU4(const uint32_t * a) { (void)a; printf("warning _ITM_RU4 called\n"); return 0; }
EXPORT uint64_t my__ITM_RU8(const uint64_t * a) { (void)a; printf("warning _ITM_RU8 called\n"); return 0; }
EXPORT void my__ITM_memcpyRtWn(void * a, const void * b, size_t c) { (void)a; (void)b; (void)c; printf("warning _ITM_memcpyRtWn called\n"); }
EXPORT void my__ITM_memcpyRnWt(void * a, const void * b, size_t c) { (void)a; (void)b; (void)c; printf("warning _ITM_memcpyRnWt called\n"); }
EXPORT void my_longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val);
EXPORT void my__longjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val) __attribute__((alias("my_longjmp")));
EXPORT void my_siglongjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val) __attribute__((alias("my_longjmp")));
EXPORT void my___longjmp_chk(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p, int32_t __val) __attribute__((alias("my_longjmp")));
//EXPORT int32_t my_setjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p);
//EXPORT int32_t my__setjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p) __attribute__((alias("my_setjmp")));
//EXPORT int32_t my___sigsetjmp(x64emu_t* emu, /*struct __jmp_buf_tag __env[1]*/void *p) __attribute__((alias("my_setjmp")));
EXPORT int my_printf(x64emu_t *emu, void* fmt, void* b) {
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 1);
PREPARE_VALIST;
return vprintf((const char*)fmt, VARARGS);
}
EXPORT int my___printf_chk(x64emu_t *emu, int chk, void* fmt, void* b)
{
(void)chk;
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 2);
PREPARE_VALIST;
return vprintf((const char*)fmt, VARARGS);
}
EXPORT int my_wprintf(x64emu_t *emu, void* fmt, void* b) {
myStackAlignW(emu, (const char*)fmt, b, emu->scratch, R_EAX, 1);
PREPARE_VALIST;
return vwprintf((const wchar_t*)fmt, VARARGS);
}
EXPORT int my___wprintf_chk(x64emu_t *emu, int chk, void* fmt, void* b)
{
(void)chk;
myStackAlignW(emu, (const char*)fmt, b, emu->scratch, R_EAX, 2);
PREPARE_VALIST;
return vwprintf((const wchar_t*)fmt, VARARGS);
}
EXPORT int my_vprintf(x64emu_t *emu, void* fmt, x64_va_list_t b) {
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vprintf(fmt, VARARGS);
}
EXPORT int my___vprintf_chk(x64emu_t *emu, void* fmt, x64_va_list_t b) __attribute__((alias("my_vprintf")));
EXPORT int my_vfprintf(x64emu_t *emu, void* F, void* fmt, x64_va_list_t b) {
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vfprintf(F, fmt, VARARGS);
}
EXPORT int my___vfprintf_chk(x64emu_t *emu, void* F, void* fmt, x64_va_list_t b) __attribute__((alias("my_vfprintf")));
EXPORT int my__IO_vfprintf(x64emu_t *emu, void* F, void* fmt, x64_va_list_t b) __attribute__((alias("my_vfprintf")));
EXPORT int my_fprintf(x64emu_t *emu, void* F, void* fmt, void* b) {
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 2);
PREPARE_VALIST;
return vfprintf(F, fmt, VARARGS);
}
EXPORT int my___fprintf_chk(x64emu_t *emu, void* F, int flag, void* fmt, void* b) {
(void)flag;
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 3);
PREPARE_VALIST;
return vfprintf(F, fmt, VARARGS);
}
EXPORT int my_vwprintf(x64emu_t *emu, void* fmt, x64_va_list_t b) {
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignWValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
int r = vwprintf(fmt, VARARGS);
return r;
}
EXPORT int my_fwprintf(x64emu_t *emu, void* F, void* fmt, void* b) {
myStackAlignW(emu, (const char*)fmt, b, emu->scratch, R_EAX, 2);
PREPARE_VALIST;
return vfwprintf(F, fmt, VARARGS);
}
EXPORT int my___fwprintf_chk(x64emu_t *emu, void* F, int flag, void* fmt, void* b) {
myStackAlignW(emu, (const char*)fmt, b, emu->scratch, R_EAX, 3);
PREPARE_VALIST;
return vfwprintf(F, fmt, VARARGS);
}
EXPORT int my_vfwprintf(x64emu_t *emu, void* F, void* fmt, x64_va_list_t b) {
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignWValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vfwprintf(F, fmt, VARARGS);
}
EXPORT int my___vfwprintf_chk(x64emu_t *emu, void* F, int flag, void* fmt, x64_va_list_t b) {
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignWValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vfwprintf(F, fmt, VARARGS);
}
EXPORT int my_dprintf(x64emu_t *emu, int d, void* fmt, void* b) {
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 2);
PREPARE_VALIST;
return vdprintf(d, fmt, VARARGS);
}
EXPORT int my___dprintf_chk(x64emu_t *emu, int d, int flag, void* fmt, void* b) {
(void)flag;
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 3);
PREPARE_VALIST;
return vdprintf(d, fmt, VARARGS);
}
EXPORT int my_vdprintf(x64emu_t *emu, int d, void* fmt, x64_va_list_t b) {
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vdprintf(d, fmt, VARARGS);
}
EXPORT int my___vdprintf_chk(x64emu_t *emu, int d, int flag, void* fmt, x64_va_list_t b) {
(void)flag;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vdprintf(d, fmt, VARARGS);
}
#if 0
EXPORT void *my_div(void *result, int numerator, int denominator) {
*(div_t *)result = div(numerator, denominator);
return result;
}
#endif
EXPORT int my_snprintf(x64emu_t* emu, void* buff, size_t s, void * fmt, uint64_t * b) {
#ifdef PREFER_CONVERT_VAARG
CREATE_VALIST_FROM_VAARG(b, emu->scratch, 3);
#else
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 3);
PREPARE_VALIST;
#endif
int r = vsnprintf(buff, s, fmt, VARARGS);
return r;
}
EXPORT int my___snprintf(x64emu_t* emu, void* buff, size_t s, void * fmt, uint64_t * b) __attribute__((alias("my_snprintf")));
EXPORT int my___snprintf_chk(x64emu_t* emu, void* buff, size_t s, int flags, size_t maxlen, void * fmt, uint64_t * b)
{
(void)flags; (void)maxlen;
#ifdef PREFER_CONVERT_VAARG
CREATE_VALIST_FROM_VAARG(b, emu->scratch, 5);
#else
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 5);
PREPARE_VALIST;
#endif
int r = vsnprintf(buff, s, fmt, VARARGS);
return r;
}
EXPORT int my_sprintf(x64emu_t* emu, void* buff, void * fmt, void * b) {
#ifdef PREFER_CONVERT_VAARG
CREATE_VALIST_FROM_VAARG(b, emu->scratch, 2);
#else
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 2);
PREPARE_VALIST;
#endif
return vsprintf(buff, (const char*)fmt, VARARGS);
}
EXPORT int my___sprintf_chk(x64emu_t* emu, void* buff, int flag, size_t l, void * fmt, void * b) {
(void)flag; (void)l;
#ifdef PREFER_CONVERT_VAARG
CREATE_VALIST_FROM_VAARG(b, emu->scratch, 4);
#else
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 4);
PREPARE_VALIST;
#endif
return vsprintf(buff, (const char*)fmt, VARARGS);
}
EXPORT int my_asprintf(x64emu_t* emu, void** buff, void * fmt, uint64_t * b) {
myStackAlign(emu, (const char*)fmt, b, emu->scratch, R_EAX, 2);
PREPARE_VALIST;
return vasprintf((char**)buff, (char*)fmt, VARARGS);
}
EXPORT int my___asprintf(x64emu_t* emu, void** buff, void * fmt, uint64_t * b) __attribute__((alias("my_asprintf")));
EXPORT int my_vasprintf(x64emu_t* emu, char** buff, void* fmt, x64_va_list_t b) {
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vasprintf(buff, fmt, VARARGS);
}
EXPORT int my_vsprintf(x64emu_t* emu, void* buff, void * fmt, x64_va_list_t b) {
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vsprintf(buff, fmt, VARARGS);
}
EXPORT int my___vsprintf_chk(x64emu_t* emu, void* buff, void * fmt, x64_va_list_t b) __attribute__((alias("my_vsprintf")));
EXPORT int my_vfscanf(x64emu_t* emu, void* stream, void* fmt, x64_va_list_t b)
{
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignScanfValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vfscanf(stream, fmt, VARARGS);
}
EXPORT int my_vsscanf(x64emu_t* emu, void* stream, void* fmt, x64_va_list_t b)
{
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignScanfValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vsscanf(stream, fmt, VARARGS);
}
EXPORT int my___vsscanf(x64emu_t* emu, void* stream, void* fmt, void* b) __attribute__((alias("my_vsscanf")));
EXPORT int my_vswscanf(x64emu_t* emu, void* stream, void* fmt, x64_va_list_t b)
{
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignScanfWValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vswscanf(stream, fmt, VARARGS);
}
EXPORT int my_sscanf(x64emu_t* emu, void* stream, void* fmt, uint64_t* b)
{
myStackAlignScanf(emu, (const char*)fmt, b, emu->scratch, 2);
PREPARE_VALIST;
return vsscanf(stream, fmt, VARARGS);
}
EXPORT int my_vscanf(x64emu_t* emu, void* fmt, x64_va_list_t b)
{
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);
#else
myStackAlignScanfValist(emu, (const char*)fmt, emu->scratch, b);
PREPARE_VALIST;
#endif
return vscanf(fmt, VARARGS);
}
EXPORT int my__IO_vfscanf(x64emu_t* emu, void* stream, void* fmt, void* b) __attribute__((alias("my_vfscanf")));
EXPORT int my___isoc99_vsscanf(x64emu_t* emu, void* stream, void* fmt, void* b) __attribute__((alias("my_vsscanf")));
EXPORT int my___isoc99_vscanf(x64emu_t* emu, void* fmt, void* b) __attribute__((alias("my_vscanf")));
EXPORT int my___isoc99_vswscanf(x64emu_t* emu, void* stream, void* fmt, void* b) __attribute__((alias("my_vswscanf")));
EXPORT int my___isoc99_vfscanf(x64emu_t* emu, void* stream, void* fmt, void* b) __attribute__((alias("my_vfscanf")));
EXPORT int my___isoc99_fscanf(x64emu_t* emu, void* stream, void* fmt, uint64_t* b)
{
myStackAlignScanf(emu, (const char*)fmt, b, emu->scratch, 2);
PREPARE_VALIST;
return vfscanf(stream, fmt, VARARGS);
}
EXPORT int my_fscanf(x64emu_t* emu, void* stream, void* fmt, uint64_t* b) __attribute__((alias("my___isoc99_fscanf")));
EXPORT int my___isoc99_scanf(x64emu_t* emu, void* fmt, uint64_t* b)
{
myStackAlignScanf(emu, (const char*)fmt, b, emu->scratch, 1);
PREPARE_VALIST;
return vscanf(fmt, VARARGS);
}
EXPORT int my___isoc99_sscanf(x64emu_t* emu, void* stream, void* fmt, uint64_t* b)
{
myStackAlignScanf(emu, (const char*)fmt, b, emu->scratch, 2);
PREPARE_VALIST;
return vsscanf(stream, fmt, VARARGS);
}
EXPORT int my___isoc99_swscanf(x64emu_t* emu, void* stream, void* fmt, uint64_t* b)
{
myStackAlignScanf(emu, (const char*)fmt, b, emu->scratch, 2);
PREPARE_VALIST;
return vswscanf(stream, fmt, VARARGS);
}
EXPORT int my_vsnprintf(x64emu_t* emu, void* buff, size_t s, void * fmt, x64_va_list_t b) {
(void)emu;
#ifdef CONVERT_VALIST
CONVERT_VALIST(b);