forked from synopse/mORMot2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmormot.lib.static.pas
More file actions
2159 lines (1924 loc) · 58.2 KB
/
Copy pathmormot.lib.static.pas
File metadata and controls
2159 lines (1924 loc) · 58.2 KB
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
/// low-level definitions needed to link static binaries
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.lib.static;
{
*****************************************************************************
Ease Link Static Files, especially for FPC/GCC
- Some Linking Constants
- Link Dependencies
- GCC Math Functions
- Minimal libc Replacement for Windows
- Cross-Platform FPU Exceptions Masking
*****************************************************************************
Please download latest https://synopse.info/files/mormot2static.7z or
https://synopse.info/files/mormot2static.tgz and extract its content to
the /static sub-folder of the mORMot 2 repository.
}
interface
{$I ..\mormot.defines.inc}
uses
SysUtils,
math,
mormot.core.base,
mormot.core.os;
{ ****************** Some Linking Constants }
const
{$ifdef OSWINDOWS}
{$ifdef CPU64}
_PREFIX = '';
{$else}
_PREFIX = '_';
{$endif CPU64}
{$else}
{$ifdef OSDARWIN}
_PREFIX = '_';
{$else}
_PREFIX = ''; // other POSIX systems don't haveany trailing underscore
{$endif OSDARWIN}
{$endif OSWINDOWS}
{$ifndef NOLIBCSTATIC}
{ ********************** Minimal libc Replacement for Windows }
{$ifdef OSWINDOWS}
// some public symbols for Delphi proper linking
// e.g. for mormot.db.raw.sqlite3.static.pas and mormot.lib.quickjs.pas
// (static linking is sadly local to each unit with Delphi)
const
_CLIB = 'msvcrt.dll'; // redirect to the built-in Microsoft "libc"
type
qsort_compare_func = function(P1, P2: pointer): integer; cdecl;
procedure libc_qsort(baseP: PByte; NElem, Width: PtrInt; comparF: qsort_compare_func); cdecl;
function libc_rename(oldname, newname: PUtf8Char): integer; cdecl;
function libc_printf(format: PAnsiChar): integer; cdecl; varargs;
function libc_sprintf(buf: pointer; format: PAnsiChar): integer; cdecl; varargs;
function libc_fprintf(fHandle: pointer; format: PAnsiChar): integer; cdecl; varargs;
function libc_vsnprintf(buf: pointer; nzize: PtrInt; format: PAnsiChar;
param: pointer): integer; cdecl;
function libc_strcspn(str, reject: PUtf8Char): integer; cdecl;
function libc_strcat(dest: PAnsiChar; src: PAnsiChar): PAnsiChar; cdecl;
function libc_strcpy(dest, src: PAnsiChar): PAnsiChar; cdecl;
function libc_strspn(p1, p2: PAnsiChar): PAnsiChar; cdecl;
function libc_strncmp(p1, p2: PByte; Size: PtrInt): integer; cdecl;
function libc_memchr(s: pointer; c: integer; n: PtrInt): pointer; cdecl;
function libc_memcmp(p1, p2: PByte; Size: PtrInt): integer; cdecl;
function libc_strchr(s: pointer; c: integer): pointer; cdecl;
function libc_strtod(value: PAnsiChar; endPtr: PPAnsiChar): Double; cdecl;
function libc_fileno(f: pointer): integer; cdecl;
function libc_write(handle: integer; buf: pointer; len: LongWord): integer; cdecl;
function libc_log(d: double): double; cdecl;
type
timeval = record
sec: integer; // defined as long in Windows SDK
usec: integer;
end;
time_t = packed record
tm_sec: integer; { Seconds. [0-60] (1 leap second) }
tm_min: integer; { Minutes. [0-59] }
tm_hour: integer; { Hours. [0-23] }
tm_mday: integer; { Day. [1-31] }
tm_mon: integer; { Month. [0-11] }
tm_year: integer; { Year - 1900. }
tm_wday: integer; { Day of week. [0-6] }
tm_yday: integer; { Days in year. [0-365] }
tm_isdst: integer; { DST. [-1/0/1]}
__tm_gmtoff: integer; { Seconds east of UTC }
__tm_zone: PChar; { Timezone abbreviation}
end;
function localtime64_s(var t: Int64; var atm: time_t): integer; cdecl;
function localtime32_s(var t: cardinal; var atm: time_t): integer; cdecl;
function putchar(c: integer): integer; cdecl;
function gettimeofday(var tv: timeval; zone: pointer): integer; cdecl;
function fputc(c: integer; f: pointer): integer; cdecl;
function fwrite(buf: pointer; size, count: PtrInt; f: pointer): integer; cdecl;
function strrchr(s: PUtf8Char; c: AnsiChar): PUtf8Char; cdecl;
function moddi3(num, den: int64): int64; cdecl;
function umoddi3(num, den: uint64): uint64; cdecl;
function divdi3(num, den: int64): int64; cdecl;
function udivdi3(num, den: uint64): uint64; cdecl;
function udivmoddi4(a, b: UInt64; var c: UInt64): UInt64; cdecl;
{$ifdef CPUINTEL}
procedure __chkstk_ms;
{$endif CPUINTEL}
{$ifdef CPUX64}
procedure __udivti3;
procedure __udivmodti4;
procedure __divti3;
procedure __umodti3;
{$endif CPUX64}
{$endif OSWINDOWS}
/// calls setlocale(LC_NUMERIC, 'C') to force to use the C default locale
// - is mandatory e.g. for mormot.lib.quickjs to properly parse float values
// - on Windows, redirects to msvcrt.dll's setlocale() API
procedure SetLibcNumericLocale;
{$endif NOLIBCSTATIC}
{ ********************** Cross-Platform FPU Exceptions Masking }
type
/// define SetFpuFlags/ResetFpuFlags context
// - external libraries coded in C are likely to disable FPU exceptions,
// whereas Delphi/FPC code expects FPU exceptions to be raised ASAP
// - ffLibrary is used before calling an external library
// - ffPascal before calling pascal code from an external library callback
TFpuFlags = (
ffLibrary,
ffPascal);
{$ifdef CPUINTEL}
var
/// direct efficient x87 / SSE2 FPU flags for rounding and exceptions
_FPUFLAGS: array[TFpuFlags] of cardinal = (
{$ifdef CPU64}
$1FA0, $1920);
{$else}
$137F, $1372);
{$endif CPU64}
{$else}
{$ifdef WINARMDELPHI}
type
TFPUExceptionMask = TArithmeticExceptionMask; // undefined in math.pas on LLVM
{$endif WINARMDELPHI}
var
/// on non Intel/AMD, use slower but cross-platform RTL Math unit
// - defined as var for runtime customization
_FPUFLAGS: array[TFpuFlags] of TFPUExceptionMask = (
// ffLibrary
[exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision],
// ffPascal
[exDenormalized, exUnderflow, exPrecision]);
{$endif CPUINTEL}
/// mask/unmask all FPU exceptions, according to the running CPU
// - returns the previous exception flags, for ResetFpuFlags() call
// - x87 flags are $1372 for pascal, or $137F for library
// - sse flags are $1920 for pascal, or $1FA0 for library
// - on non Intel/AMD CPUs, will use TFPUExceptionMask from the RTL Math unit
// - do nothing and return -1 if the supplied flags are the one already set
function SetFpuFlags(flags: TFpuFlags = ffLibrary): cardinal;
/// restore the FPU exceptions flags as overriden by SetFpuFlags()
// - do nothing if the saved flags are the one already set, i.e. -1
procedure ResetFpuFlags(saved: cardinal);
implementation
{$ifndef NOLIBCSTATIC}
{ ****************** Link Dependencies }
{$ifdef FPC}
{$ifdef OSWINDOWS}
{$ifdef CPU64}
{$linklib ..\..\static\x86_64-win64\libkernel32.a}
{$else}
{$linklib ..\..\static\i386-win32\libkernel32.a}
{$endif CPU64}
{$endif OSWINDOWS}
{$ifdef OSANDROID}
{$ifdef CPUAARCH64}
{$linklib ..\..\static\aarch64-android\libgcc.a}
{$endif CPUAARCH64}
{$ifdef CPUARM}
{$linklib ..\..\static\arm-android\libgcc.a}
{$endif CPUARM}
{$ifdef CPUX64}
{$linklib ..\..\static\x86_64-android\libgcc.a}
{$endif CPUX64}
{$endif OSANDROID}
{$ifdef OSLINUX}
{$ifdef CPUAARCH64}
{$linklib ..\..\static\aarch64-linux\libgcc.a}
{$endif CPUAARCH64}
{$ifdef CPUARM}
{$linklib ..\..\static\arm-linux\libgcc.a}
{$endif CPUARM}
{$endif OSLINUX}
{$endif FPC}
{ ****************** Reimplemented some C Functions in Pascal }
// QuickJS and lizard source e.g. was patched to call those heap functions
// warning: we can't replace malloc/free/realloc and link libc/libpthread/libdl
function pas_malloc(size: cardinal): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'pas_malloc'; {$endif}
begin
GetMem(result, size);
end;
function pas_calloc(n, size: PtrInt): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'pas_calloc'; {$endif}
begin
result := AllocMem(size * n);
end;
procedure pas_free(P: pointer); cdecl;
{$ifdef FPC} public name _PREFIX + 'pas_free'; {$endif}
begin
FreeMem(P);
end;
function pas_realloc(P: pointer; Size: PtrInt): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'pas_realloc'; {$endif}
begin
ReallocMem(P, Size);
result := P;
end;
function pas_malloc_usable_size(P: pointer): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'pas_malloc_usable_size'; {$endif}
begin
{$ifdef FPC}
result := MemSize(P); // only available on FPC
{$else}
result := 0; // will reallocate each time - good enough with Delphi's FastMM4
{$endif FPC}
end;
// see e.g. #define assert(x) in QuickJS cutils.h
type
ELibStatic = class(ExceptionWithProps);
procedure pas_assertfailed(cond, fn: PAnsiChar; line: integer); cdecl;
{$ifdef FPC} public name _PREFIX + 'pas_assertfailed'; {$endif}
begin
raise ELibStatic.CreateFmt('Panic in %s:%d: assert(%s)', [fn, line, cond]);
end;
{ ********************** Minimal libc Replacement for Windows }
{$ifdef OSWINDOWS}
// SQLite3 is compiled with SQLITE_OMIT_LOCALTIME and SQLITE_NO_THREAD
// to ease static linking
function libc_rename(oldname, newname: PUtf8Char): integer; cdecl;
external _CLIB name 'rename';
function libc_printf(format: PAnsiChar): integer; cdecl; varargs;
external _CLIB name 'printf';
function libc_sprintf(buf: pointer; format: PAnsiChar): integer; cdecl; varargs;
external _CLIB name 'sprintf';
function libc_fprintf(fHandle: pointer; format: PAnsiChar): integer; cdecl; varargs;
external _CLIB name 'fprintf';
function libc_vsnprintf(buf: pointer; nzize: PtrInt; format: PAnsiChar;
param: pointer): integer; cdecl;
external _CLIB name '_vsnprintf';
function libc_strcspn(str, reject: PUtf8Char): integer; cdecl;
external _CLIB name 'strcspn';
function libc_strcat(dest, src: PAnsiChar): PAnsiChar; cdecl;
external _CLIB name 'strcat';
function libc_strcpy(dest, src: PAnsiChar): PAnsiChar; cdecl;
external _CLIB name 'strcpy';
function libc_strspn(p1, p2: PAnsiChar): PAnsiChar; cdecl;
external _CLIB name 'strspn';
function libc_strncmp(p1, p2: PByte; Size: PtrInt): integer; cdecl;
external _CLIB name 'strncmp';
function libc_memchr(s: pointer; c: integer; n: PtrInt): pointer; cdecl;
external _CLIB name 'memchr';
function libc_memcmp(p1, p2: PByte; Size: PtrInt): integer; cdecl;
external _CLIB name 'memcmp';
function libc_strchr(s: pointer; c: integer): pointer; cdecl;
external _CLIB name 'strchr';
function libc_strtod(value: PAnsiChar; endPtr: PPAnsiChar): Double; cdecl;
external _CLIB name 'strtod';
function libc_fileno(f: pointer): integer; cdecl;
external _CLIB name '_fileno';
function libc_write(handle: integer; buf: pointer; len: LongWord): integer; cdecl;
external _CLIB name '_write';
procedure libc_qsort(baseP: PByte; NElem, Width: PtrInt; comparF: qsort_compare_func); cdecl;
external _CLIB name 'qsort';
function libc_log(d: double): double; cdecl;
external _CLIB name 'log';
function libc_beginthreadex(security: pointer; stksize: cardinal;
start, arg: pointer; flags: cardinal; var threadid: cardinal): THandle; cdecl;
external _CLIB name '_beginthreadex';
procedure libc_endthreadex(exitcode: cardinal); cdecl;
external _CLIB name '_endthreadex';
function localtime64_s(var t: Int64; var atm: time_t): integer;
var
S: TSystemTime;
begin
UnixTimeToLocalTime(t, S);
atm.tm_sec := S.wSecond;
atm.tm_min := S.wMinute;
atm.tm_hour := S.wHour;
atm.tm_mday := S.wDay;
atm.tm_mon := S.wMonth - 1;
atm.tm_year := S.wYear - 1900;
atm.tm_wday := S.wDay;
result := 0; // on Windows, should return 0; on POSIX, nil or @atm :(
end;
function localtime32_s(var t: cardinal; var atm: time_t): integer;
var
t64: Int64;
begin
t64 := t;
result := localtime64_s(t64, atm);
end;
var
{ as standard C library documentation states:
Statically allocated buffer, shared by the functions gmtime() and localtime().
Each call of these functions overwrites the content of this structure.
-> this buffer is shared, but SQLite3 will protect it with a mutex :) }
atm: time_t;
function localtime32(t: PCardinal): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + '_localtime32'; {$endif}
begin
localtime32_s(t^, atm);
result := @atm;
end;
function localtime64(t: PInt64): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + '_localtime64'; {$endif}
begin
localtime64_s(t^, atm);
result := @atm;
end;
function putchar(c: integer): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'putchar'; {$endif}
begin
{$I-}
write(AnsiChar(c));
ioresult;
{$I+}
result := c;
end;
function gettimeofday(var tv: timeval; zone: pointer): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'gettimeofday'; {$else} export; {$endif}
var
ms, sec: QWord;
begin
ms := UnixMSTimeUtcFast;
sec := ms div MilliSecsPerSec;
tv.sec := sec;
tv.usec := (ms - sec * MilliSecsPerSec) * MicroSecsPerMilliSec;
result := 0;
end;
function fputc(c: integer; f: pointer): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'fputc'; {$endif}
begin
if libc_write(libc_fileno(f), @c, 1) = 1 then
result := c
else
result := 26;
end;
function strrchr(s: PUtf8Char; c: AnsiChar): PUtf8Char; cdecl;
{$ifdef FPC} public name _PREFIX + 'strrchr'; {$endif}
begin
// simple full pascal version of the standard C library function
result := nil;
if s <> nil then
while s^ <> #0 do
begin
if s^ = c then
result := s;
inc(s);
end;
end;
function fwrite(buf: pointer; size, count: PtrInt; f: pointer): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'fwrite'; {$endif}
begin
result := libc_write(libc_fileno(f), buf, size * count) div size;
end;
{$ifdef CPUX86}
// asm stubs to circumvent libgcc.a (cross)linking issues on Win32
procedure __chkstk_ms; assembler;
{$ifdef FPC} nostackframe; public name _PREFIX + '__chkstk_ms'; {$endif}
asm
push ecx
push eax
cmp eax, 4096
lea ecx, dword ptr [esp+0CH]
jc @@002
@@001: sub ecx, 4096
or dword ptr [ecx], 00H
sub eax, 4096
cmp eax, 4096
ja @@001
@@002: sub ecx, eax
or dword ptr [ecx], 00H
pop eax
pop ecx
end;
{$endif CPUX86}
{$ifdef CPUX64}
procedure __chkstk_ms; assembler;
{$ifdef FPC} nostackframe; public name _PREFIX + '___chkstk_ms'; {$endif}
asm
{$ifdef ISDELPHI}
.noframe
{$endif ISDELPHI}
push rcx
push rax
cmp rax, 4096
lea rcx, qword ptr [rsp+18H]
jc @@002
@@001: sub rcx, 4096
or qword ptr [rcx], 00H
sub rax, 4096
cmp rax, 4096
ja @@001
@@002: sub rcx, rax
or qword ptr [rcx], 00H
pop rax
pop rcx
end;
{$endif CPUX64}
{$ifdef FPC}
// we can safely redirect c code to our pascal heap on FPC/Windows
function malloc(size: cardinal): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'malloc'; {$endif}
begin
GetMem(result, size);
end;
function calloc(n, size: PtrInt): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'calloc'; {$endif}
begin
result := AllocMem(size * n);
end;
procedure free(P: pointer); cdecl;
{$ifdef FPC} public name _PREFIX + 'free'; {$endif}
begin
FreeMem(P);
end;
function realloc(P: pointer; Size: PtrInt): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'realloc'; {$endif}
begin
result := P;
ReallocMem(result, Size);
end;
function malloc_usable_size(P: pointer): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'malloc_usable_size'; {$endif}
begin
{$ifdef FPC}
result := MemSize(P); // only available on FPC
{$else}
result := 0; // will reallocate each time - good enough with Delphi's FastMM4
{$endif FPC}
end;
function memcpy(dest, src: pointer; count: PtrInt): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'memcpy'; {$endif}
begin
MoveFast(src^, dest^, count);
result := dest;
end;
function memset(dest: pointer; val: integer; count: PtrInt): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'memset'; {$endif}
begin
FillCharFast(dest^, count, val);
result := dest;
end;
function memmove(dest, src: pointer; count: PtrInt): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'memmove'; {$endif}
begin
MoveFast(src^, dest^, count);
result := dest;
end;
function strlen(p: PAnsiChar): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'strlen'; {$endif}
begin
result := mormot.core.base.StrLen(P);
end;
function strcmp(p1, p2: PAnsiChar): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'strcmp'; {$endif}
begin
// called only by some obscure FTS3 functions (normal code use dedicated functions)
result := mormot.core.base.StrComp(p1, p2);
end;
function rename(oldname, newname: PUtf8Char): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'rename'; {$else} export; {$endif}
begin
result := libc_rename(oldname, newname);
end;
{$ifdef FPC}
var
// redirect mingw import pointer to libc_beginthreadex
beginthreadex: pointer public name
{$ifdef CPU32} '__imp___beginthreadex' {$else} '__imp__beginthreadex' {$endif};
// redirect mingw import pointer to libc_endthreadex
endthreadex: pointer public name
{$ifdef CPU32} '__imp___endthreadex' {$else} '__imp__endthreadex' {$endif};
// redirect mingw import pointer to our internal functions
{$ifdef CPU32}
imp_localtime32: pointer public name '__imp___localtime32';
{$else}
imp_localtime64: pointer public name '__imp__localtime64';
{$endif CPU32}
{$endif FPC}
procedure __exit;
{$ifdef FPC} public name _PREFIX + 'exit'; {$else} export; {$endif}
begin
raise ELibStatic.Create('Unexpected exit() call');
end;
{$ifdef CPUINTEL}
procedure printf; assembler;
{$ifdef FPC} nostackframe; public name _PREFIX + 'printf'; {$else} export; {$endif}
asm
jmp libc_printf
end;
procedure sprintf; assembler;
{$ifdef FPC} nostackframe; public name _PREFIX + 'sprintf'; {$else} export; {$endif}
asm
jmp libc_sprintf
end;
procedure fprintf; assembler;
{$ifdef FPC} nostackframe; public name _PREFIX + 'fprintf'; {$else} export; {$endif}
asm
jmp libc_fprintf
end;
procedure _vsnprintf; assembler;
{$ifdef FPC} nostackframe; public name _PREFIX + '__ms_vsnprintf'; {$else} export; {$endif}
asm
jmp libc_vsnprintf
end;
{$else}
{$endif CPUINTEL}
function strcspn(str, reject: PUtf8Char): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'strcspn'; {$else} export; {$endif}
begin
result := libc_strcspn(str, reject);
end;
function strcat(dest: PAnsiChar; src: PAnsiChar): PAnsiChar; cdecl;
{$ifdef FPC} public name _PREFIX + 'strcat'; {$else} export; {$endif}
begin
result := libc_strcat(dest, src);
end;
function strcpy(dest, src: PAnsiChar): PAnsiChar; cdecl;
{$ifdef FPC} public name _PREFIX + 'strcpy'; {$else} export; {$endif}
begin
result := libc_strcpy(dest, src);
end;
function strspn(p1, p2: PAnsiChar): PAnsiChar; cdecl;
{$ifdef FPC} public name _PREFIX + 'strspn'; {$else} export; {$endif}
begin
result := libc_strspn(p1, p2);
end;
function strtod(value: PAnsiChar; endPtr: PPAnsiChar): Double; cdecl;
{$ifdef FPC} public name _PREFIX + '__strtod'; {$else} export; {$endif}
begin
result := libc_strtod(value, endPtr);
end;
function strncmp(p1, p2: PByte; Size: PtrInt): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'strncmp'; {$endif}
begin
result := libc_strncmp(p1, p2, Size);
end;
function atoi(const str: PUtf8Char): PtrInt; cdecl;
{$ifdef FPC} public name _PREFIX + 'atoi'; {$else} export; {$endif}
begin
result := GetInteger(str);
end;
function memchr(s: pointer; c: integer; n: PtrInt): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'memchr'; {$else} export; {$endif}
begin
result := libc_memchr(s, c, n);
end;
function memcmp(p1, p2: PByte; Size: PtrInt): integer; cdecl;
{$ifdef FPC} public name _PREFIX + 'memcmp'; {$endif}
begin
result := libc_memcmp(p1, p2, Size);
end;
function strchr(s: PAnsiChar; c: integer): pointer; cdecl;
{$ifdef FPC} public name _PREFIX + 'strchr'; {$else} export; {$endif}
begin
result := libc_strchr(s, c);
end;
procedure qsort(baseP: PByte; NElem, Width: PtrInt; comparF: qsort_compare_func); cdecl;
{$ifdef FPC} public name _PREFIX + 'qsort'; {$endif}
begin
libc_qsort(baseP, NElem, Width, comparF);
end;
{$ifdef CPUX86} // not a compiler intrinsic on x86
function _InterlockedCompareExchange(
var Dest: integer; New, Comp: integer): integer; stdcall;
public alias: '_InterlockedCompareExchange@12';
begin
result := InterlockedCompareExchange(Dest, New, Comp);
end;
{$endif CPUX86}
{$endif FPC}
{$endif OSWINDOWS}
// see clocale.pp unit for those values
function setlocale(category: integer; locale: PAnsiChar): PAnsiChar; cdecl;
{$ifdef OSWINDOWS}
external _CLIB name 'setlocale'; // redirect to msvcrt.dll
{$else}
{$ifdef NETBSD}
// NetBSD has a new setlocale function defined in /usr/include/locale.h
external 'c' name '__setlocale_mb_len_max_32';
{$else}
external 'c' name 'setlocale' + LIBC_SUFFIX; // regular libc POSIX call
{$endif NETBSD}
{$endif OSWINDOWS}
const
LC_CTYPE = 0;
LC_NUMERIC = 1;
LC_ALL = 6;
procedure SetLibcNumericLocale;
begin
setlocale(LC_NUMERIC, 'C');
end;
{$ifdef OSLINUXX64}
// some definitions dedicated to avoid libc name versioning for sqlite3.o
function __fxstat64(ver: integer; f: integer; st: pointer): integer;
cdecl; external 'c' name '__fxstat64' + LIBC_SUFFIX;
function __xstat64(ver: integer; fn, st: pointer): integer;
cdecl; external 'c' name '__xstat64' + LIBC_SUFFIX;
function __lxstat64(ver: integer; fn, st: pointer): integer;
cdecl; external 'c' name '__lxstat64' + LIBC_SUFFIX;
const
STAT_VER = {$ifdef CPUX86} 3 {$else} 1 {$endif}; // see xstatver.h
function stat64(f: PChar; st: pointer): integer; cdecl; public name 'stat64';
begin
result := __xstat64(STAT_VER, f, st);
end;
function fstat64(f: integer; st: pointer): integer; cdecl; public name 'fstat64';
begin
result := __fxstat64(STAT_VER, f, st);
end;
function lstat64(f: PChar; st: pointer): integer; cdecl; public name 'lstat64';
begin
result := __lxstat64(STAT_VER, f, st);
end;
// on x86_64, we redirect some libc public symbol names with a _ prefix as added
// by cp sqlite3.o sqlite3.orig
// objconv -np:pthread:_pthread -np:dl:_dl sqlite3.orig sqlite3.o
var
// static linking fails: use late binding with lazy initialization
// we already have a pthread library loaded by mormot.core.os.posix.inc anyway
_pthread_join: function(__th, __thread_return: pointer): integer; cdecl;
_pthread_mutex_trylock: function(__mutex:pointer): integer; cdecl;
_pthread_mutexattr_settype: function(__attr: pointer; Kind: integer): integer; cdecl;
_pthread_mutexattr_init: function(__attr: pointer): integer; cdecl;
_pthread_create: function(__thread, __attr, __start_routine, __arg: pointer): integer; cdecl;
_pthread_mutexattr_destroy: function(__attr: pointer): integer; cdecl;
_pthread_mutex_init: function(__mutexn, __mutex_attr: pointer): integer; cdecl;
_pthread_mutex_destroy: function(__mutex: pointer): integer; cdecl;
_pthread_mutex_lock: function(__mutex: pointer): integer; cdecl;
_pthread_mutex_unlock: function(__mutex: pointer): integer; cdecl;
function _dlopen(Name: PAnsiChar; Flags: integer): pointer;
cdecl; external 'dl' name 'dlopen' + LIBC_SUFFIX;
function _dlsym(Lib: pointer; Name : PAnsiChar): pointer;
cdecl; external 'dl' name 'dlsym' + LIBC_SUFFIX;
function _dlclose(Lib: pointer): integer;
cdecl; external 'dl' name 'dlclose' + LIBC_SUFFIX;
function _dlerror() : PAnsiChar;
cdecl; external 'dl' name 'dlerror' + LIBC_SUFFIX;
procedure _pthread_load;
begin
if not Assigned(pthread) then
raise ELibStatic.Create('Missing pthread from mormot.core.os');
_pthread_join := _dlsym(pthread, 'pthread_join');
_pthread_mutex_trylock := _dlsym(pthread, 'pthread_mutex_trylock');
_pthread_mutexattr_settype := _dlsym(pthread, 'pthread_mutexattr_settype');
_pthread_mutexattr_init := _dlsym(pthread, 'pthread_mutexattr_init');
_pthread_create := _dlsym(pthread, 'pthread_create');
_pthread_mutexattr_destroy := _dlsym(pthread, 'pthread_mutexattr_destroy');
_pthread_mutex_init := _dlsym(pthread, 'pthread_mutex_init');
_pthread_mutex_destroy := _dlsym(pthread, 'pthread_mutex_destroy');
_pthread_mutex_lock := _dlsym(pthread, 'pthread_mutex_lock');
_pthread_mutex_unlock := _dlsym(pthread, 'pthread_mutex_unlock');
end;
function __pthread_join(__th, __thread_return: pointer): integer;
cdecl; public name '_pthread_join';
begin
result := _pthread_join(__th, __thread_return);
end;
function __pthread_mutex_trylock(__mutex:pointer): integer;
cdecl; public name '_pthread_mutex_trylock';
begin
result := _pthread_mutex_trylock(__mutex);
end;
function __pthread_mutexattr_settype (__attr: pointer; Kind: integer): integer;
cdecl; public name '_pthread_mutexattr_settype';
begin
result := _pthread_mutexattr_settype (__attr, Kind);
end;
function __pthread_mutexattr_init(__attr: pointer): integer;
cdecl; public name '_pthread_mutexattr_init';
begin
result := _pthread_mutexattr_init(__attr);
end;
function __pthread_create(__thread, __attr, __start_routine, __arg: pointer): integer;
cdecl; public name '_pthread_create';
begin
result := _pthread_create(__thread, __attr, __start_routine, __arg);
end;
function __pthread_mutexattr_destroy(__attr: pointer): integer;
cdecl; public name '_pthread_mutexattr_destroy';
begin
result := _pthread_mutexattr_destroy(__attr);
end;
function __pthread_mutex_init(__mutexn, __mutex_attr: pointer): integer;
cdecl; public name '_pthread_mutex_init';
begin
result := _pthread_mutex_init(__mutexn, __mutex_attr);
end;
function __pthread_mutex_destroy(__mutex: pointer): integer;
cdecl; public name '_pthread_mutex_destroy';
begin
result := _pthread_mutex_destroy(__mutex);
end;
// most common methods have low-level asm redirection with no stack frame
function __pthread_mutex_lock(__mutex: pointer): integer;
cdecl; public name '_pthread_mutex_lock'; assembler; nostackframe;
asm
jmp qword ptr [rip + _pthread_mutex_lock]
end;
function __pthread_mutex_unlock(__mutex: pointer): integer;
cdecl; public name '_pthread_mutex_unlock'; assembler; nostackframe;
asm
jmp qword ptr [rip + _pthread_mutex_unlock]
end;
function __dlopen(Name: PAnsiChar; Flags: integer) : pointer;
cdecl; public name '_dlopen';
begin
result := _dlopen(Name, Flags);
end;
function __dlsym(Lib: pointer; Name: PAnsiChar) : pointer;
cdecl; public name '_dlsym';
begin
result := _dlsym(Lib, Name);
end;
function __dlclose(Lib: pointer): integer;
cdecl; public name '_dlclose';
begin
result := _dlclose(Lib);
end;
function __dlerror(): PAnsiChar;
cdecl; public name '_dlerror';
begin
result := _dlerror();
end;
{$endif OSLINUXX64}
{ ****************** GCC Math Functions }
function fabs(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'fabs'; {$endif}
begin
result := abs(x);
end;
function sqrt(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'sqrt'; {$endif}
begin
result := system.sqrt(x);
end;
function cbrt(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'cbrt'; {$endif}
begin
result := exp( (1 / 3) * ln(x));
end;
function trunk(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'trunc'; {$endif}
begin
result := trunc(x);
end;
function kos(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'cos'; {$endif}
begin
result := cos(x);
end;
function cin(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'sin'; {$endif}
begin
result := sin(x);
end;
function akos(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'acos'; {$endif}
begin
result := arccos(x);
end;
function acin(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'asin'; {$endif}
begin
result := arcsin(x);
end;
function axp(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'exp'; {$endif}
begin
result := exp(x);
end;
function axpm1(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'expm1'; {$endif}
begin
result := exp(x) - 1;
end;
function l0g(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'log'; {$endif}
begin
result := ln(x);
end;
function t0n(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'tan'; {$endif}
begin
result := tan(x);
end;
function kosh(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'cosh'; {$endif}
begin
result := cosh(x);
end;
function cinh(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'sinh'; {$endif}
begin
result := sinh(x);
end;
function t0nh(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'tanh'; {$endif}
begin
result := tanh(x);
end;
function ac0sh(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'acosh'; {$endif}
begin
result := arccosh(x);
end;
function as1nh(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'asinh'; {$endif}
begin
result := arcsinh(x);
end;
function at0n(x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'atan'; {$endif}
begin
result := arctan(x);
end;
function at0n2(y, x: double): double; cdecl;
{$ifdef FPC} public name _PREFIX + 'atan2'; {$endif}
begin