-
Notifications
You must be signed in to change notification settings - Fork 2
/
builtins.rs
677 lines (652 loc) · 16.8 KB
/
builtins.rs
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
#[no_mangle]
static _fltused: i32 = 0;
#[cfg(target_arch = "x86")]
mod x86 {
// TODO: Remove all `_a*` functions once https://github.com/rust-lang/compiler-builtins/issues/403
// is solved.
#[naked]
#[no_mangle]
unsafe extern fn _alldiv(dividend: i64, divisor: i64) -> i64 {
asm!(
"
push edi
push esi
push ebx
xor edi, edi
mov eax, dword ptr [esp + 0x14]
or eax, eax
jge 2f
inc edi
mov edx, dword ptr [esp + 0x10]
neg eax
neg edx
sbb eax, 0
mov dword ptr [esp + 0x14], eax
mov dword ptr [esp + 0x10], edx
2:
mov eax, dword ptr [esp + 0x1C]
or eax, eax
jge 3f
inc edi
mov edx, dword ptr [esp + 0x18]
neg eax
neg edx
sbb eax, 0
mov dword ptr [esp + 0x1C], eax
mov dword ptr [esp + 0x18], edx
3:
or eax, eax
jnz 4f
mov ecx, dword ptr [esp + 0x18]
mov eax, dword ptr [esp + 0x14]
xor edx, edx
div ecx
mov ebx, eax
mov eax, dword ptr [esp + 0x10]
div ecx
mov edx, ebx
jmp 8f
4:
mov ebx, eax
mov ecx, dword ptr [esp + 0x18]
mov edx, dword ptr [esp + 0x14]
mov eax, dword ptr [esp + 0x10]
5:
shr ebx, 1
rcr ecx, 1
shr edx, 1
rcr eax, 1
or ebx, ebx
jnz 5b
div ecx
mov esi, eax
mul dword ptr [esp + 0x1C]
mov ecx, eax
mov eax, dword ptr [esp + 0x18]
mul esi
add edx, ecx
jb 6f
cmp edx, dword ptr [esp + 0x14]
ja 6f
jb 7f
cmp eax, dword ptr [esp + 0x10]
jbe 7f
6:
dec esi
7:
xor edx, edx
mov eax, esi
8:
dec edi
jnz 9f
neg edx
neg eax
sbb edx, 0
9:
pop ebx
pop esi
pop edi
ret 2*8
",
options(noreturn)
);
}
#[allow(improper_ctypes_definitions)]
#[naked]
#[no_mangle]
unsafe extern fn _alldvrm(dividend: i64, divisor: i64) -> (i64, i64) {
asm!(
"
push edi
push esi
push ebp
xor edi, edi
xor ebp, ebp
mov eax, dword ptr [esp + 0x14]
or eax, eax
jge 2f
inc edi
inc ebp
mov edx, dword ptr [esp + 0x10]
neg eax
neg edx
sbb eax, 0
mov dword ptr [esp + 0x14], eax
mov dword ptr [esp + 0x10], edx
2:
mov eax, dword ptr [esp + 0x1C]
or eax, eax
jge 3f
inc edi
mov edx, dword ptr [esp + 0x18]
neg eax
neg edx
sbb eax, 0
mov dword ptr [esp + 0x1C], eax
mov dword ptr [esp + 0x18], edx
3:
or eax, eax
jnz 4f
mov ecx, dword ptr [esp + 0x18]
mov eax, dword ptr [esp + 0x14]
xor edx, edx
div ecx
mov ebx, eax
mov eax, dword ptr [esp + 0x10]
div ecx
mov esi, eax
mov eax, ebx
mul dword ptr [esp + 0x18]
mov ecx, eax
mov eax, esi
mul dword ptr [esp + 0x18]
add edx, ecx
jmp 8f
4:
mov ebx, eax
mov ecx, dword ptr [esp + 0x18]
mov edx, dword ptr [esp + 0x14]
mov eax, dword ptr [esp + 0x10]
5:
shr ebx, 1
rcr ecx, 1
shr edx, 1
rcr eax, 1
or ebx, ebx
jnz 5b
div ecx
mov esi, eax
mul dword ptr [esp + 0x1C]
mov ecx, eax
mov eax, dword ptr [esp + 0x18]
mul esi
add edx, ecx
jb 6f
cmp edx, dword ptr [esp + 0x14]
ja 6f
jb 7f
cmp eax, dword ptr [esp + 0x10]
jbe 7f
6:
dec esi
sub eax, dword ptr [esp + 0x18]
sbb edx, dword ptr [esp + 0x1C]
7:
xor ebx, ebx
8:
sub eax, dword ptr [esp + 0x10]
sbb edx, dword ptr [esp + 0x14]
dec ebp
jns 9f
neg edx
neg eax
sbb edx, 0
9:
mov ecx, edx
mov edx, ebx
mov ebx, ecx
mov ecx, eax
mov eax, esi
dec edi
jnz 12f
neg edx
neg eax
sbb edx, 0
12:
pop ebp
pop esi
pop edi
ret 2*8
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _allmul(multiplier: i64, multiplicand: i64) -> i64 {
asm!(
"
mov eax, dword ptr [esp + 0x8]
mov ecx, dword ptr [esp + 0x10]
or ecx, eax
mov ecx, dword ptr [esp + 0xC]
jnz 2f
mov eax, dword ptr [esp + 0x4]
mul ecx
ret 2*8
2:
push ebx
mul ecx
mov ebx, eax
mov eax, dword ptr [esp + 0x8]
mul dword ptr [esp + 0x14]
add ebx, eax
mov eax, dword ptr [esp + 0x8]
mul ecx
add edx, ebx
pop ebx
ret 2*8
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _allrem(dividend: i64, divisor: i64) -> i64 {
asm!(
"
push ebx
push edi
xor edi, edi
mov eax, dword ptr [esp + 0x10]
or eax, eax
jge 2f
inc edi
mov edx, dword ptr [esp + 0xC]
neg eax
neg edx
sbb eax, 0
mov dword ptr [esp + 0x10], eax
mov dword ptr [esp + 0xC], edx
2:
mov eax, dword ptr [esp + 0x18]
or eax, eax
jge 3f
mov edx, dword ptr [esp + 0x14]
neg eax
neg edx
sbb eax, 0
mov dword ptr [esp + 0x18], eax
mov dword ptr [esp + 0x14], edx
3:
or eax, eax
jnz 4f
mov ecx, dword ptr [esp + 0x14]
mov eax, dword ptr [esp + 0x10]
xor edx, edx
div ecx
mov eax, dword ptr [esp + 0xC]
div ecx
mov eax, edx
xor edx, edx
dec edi
jns 8f
jmp 9f
4:
mov ebx, eax
mov ecx, dword ptr [esp + 0x14]
mov edx, dword ptr [esp + 0x10]
mov eax, dword ptr [esp + 0xC]
5:
shr ebx, 1
rcr ecx, 1
shr edx, 1
rcr eax, 1
or ebx, ebx
jnz 5b
div ecx
mov ecx, eax
mul dword ptr [esp + 0x18]
xchg eax, ecx
mul dword ptr [esp + 0x14]
add edx, ecx
jb 6f
cmp edx, dword ptr [esp + 0x10]
ja 6f
jb 7f
cmp eax, dword ptr [esp + 0xC]
jbe 7f
6:
sub eax, dword ptr [esp + 0x14]
sbb edx, dword ptr [esp + 0x18]
7:
sub eax, dword ptr [esp + 0xC]
sbb edx, dword ptr [esp + 0x10]
dec edi
jns 9f
8:
neg edx
neg eax
sbb edx, 0
9:
pop edi
pop ebx
ret 2*8
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _allshl(value: i64, positions: i64) -> i64 {
asm!(
"
cmp cl, 64
jnb 3f
cmp cl, 32
jnb 2f
shld edx, eax, cl
shl eax, cl
ret
2:
mov edx, eax
xor eax, eax
and cl, 32-1
shl edx, cl
ret
3:
xor eax, eax
xor edx, edx
ret
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _allshr(value: i64, positions: i64) -> i64 {
asm!(
"
cmp cl, 64
jnb 3f
cmp cl, 32
jnb 2f
shrd eax, edx, cl
sar edx, cl
ret
2:
mov eax, edx
sar edx, 32-1
and cl, 32-1
sar eax, cl
ret
3:
sar edx, 0x1F
mov eax, edx
ret
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _aulldiv(dividend: u64, divisor: u64) -> u64 {
asm!(
"
push ebx
push esi
mov eax, dword ptr [esp + 0x18]
or eax, eax
jnz 2f
mov ecx, dword ptr [esp + 0x14]
mov eax, dword ptr [esp + 0x10]
xor edx, edx
div ecx
mov ebx, eax
mov eax, dword ptr [esp + 0xC]
div ecx
mov edx, ebx
jmp 6f
2:
mov ecx, eax
mov ebx, dword ptr [esp + 0x14]
mov edx, dword ptr [esp + 0x10]
mov eax, dword ptr [esp + 0xC]
3:
shr ecx, 1
rcr ebx, 1
shr edx, 1
rcr eax, 1
or ecx, ecx
jnz 3b
div ebx
mov esi, eax
mul dword ptr [esp + 0x18]
mov ecx, eax
mov eax, dword ptr [esp + 0x14]
mul esi
add edx, ecx
jb 4f
cmp edx, dword ptr [esp + 0x10]
ja 4f
jb 5f
cmp eax, dword ptr [esp + 0xC]
jbe 5f
4:
dec esi
5:
xor edx, edx
mov eax, esi
6:
pop esi
pop ebx
ret 2*8
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _aulldvrm(dividend: u64, divisor: u64) -> u64 {
asm!(
"
push esi
mov eax, dword ptr [esp + 0x14]
or eax, eax
jnz 2f
mov ecx, dword ptr [esp + 0x10]
mov eax, dword ptr [esp + 0xC]
xor edx, edx
div ecx
mov ebx, eax
mov eax, dword ptr [esp + 0x8]
div ecx
mov esi, eax
mov eax, ebx
mul dword ptr [esp + 0x10]
mov ecx, eax
mov eax, esi
mul dword ptr [esp + 0x10]
add edx, ecx
jmp 6f
2:
mov ecx, eax
mov ebx, dword ptr [esp + 0x10]
mov edx, dword ptr [esp + 0xC]
mov eax, dword ptr [esp + 0x8]
3:
shr ecx, 1
rcr ebx, 1
shr edx, 1
rcr eax, 1
or ecx, ecx
jnz 3b
div ebx
mov esi, eax
mul dword ptr [esp + 0x14]
mov ecx, eax
mov eax, dword ptr [esp + 0x10]
mul esi
add edx, ecx
jb 4f
cmp edx, dword ptr [esp + 0xC]
ja 4f
jb 5f
cmp eax, dword ptr [esp + 0x8]
jbe 5f
4:
dec esi
sub eax, dword ptr [esp + 0x10]
sbb edx, dword ptr [esp + 0x14]
5:
xor ebx, ebx
6:
sub eax, dword ptr [esp + 0x8]
sbb edx, dword ptr [esp + 0xC]
neg edx
neg eax
sbb edx, 0
mov ecx, edx
mov edx, ebx
mov ebx, ecx
mov ecx, eax
mov eax, esi
pop esi
ret 2*8
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _aullrem(dividend: u64, divisor: u64) -> u64 {
asm!(
"
push ebx
mov eax, dword ptr [esp + 0x14]
or eax, eax
jnz 2f
mov ecx, dword ptr [esp + 0x10]
mov eax, dword ptr [esp + 0xC]
xor edx, edx
div ecx
mov eax, dword ptr [esp + 0x8]
div ecx
mov eax, edx
xor edx, edx
jmp 6f
2:
mov ecx, eax
mov ebx, dword ptr [esp + 0x10]
mov edx, dword ptr [esp + 0xC]
mov eax, dword ptr [esp + 0x8]
3:
shr ecx, 1
rcr ebx, 1
shr edx, 1
rcr eax, 1
or ecx, ecx
jnz 3b
div ebx
mov ecx, eax
mul dword ptr [esp + 0x14]
xchg eax, ecx
mul dword ptr [esp + 0x10]
add edx, ecx
jb 4f
cmp edx, dword ptr [esp + 0xC]
ja 4f
jb 5f
cmp eax, dword ptr [esp + 0x8]
jbe 5f
4:
sub eax, dword ptr [esp + 0x10]
sbb edx, dword ptr [esp + 0x14]
5:
sub eax, dword ptr [esp + 0x8]
sbb edx, dword ptr [esp + 0xC]
neg edx
neg eax
sbb edx, 0
6:
pop ebx
ret 2*8
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _aullshr(value: u64, positions: u64) -> u64 {
asm!(
"
cmp cl, 64
jnb 3f
cmp cl, 32
jnb 2f
shrd eax, edx, cl
shr edx, cl
ret
2:
mov eax, edx
xor edx, edx
and cl, 32-1
shr eax, cl
ret
3:
xor eax, eax
xor edx, edx
ret
",
options(noreturn)
);
}
#[naked]
#[no_mangle]
unsafe extern fn _chkstk() {
asm!(
"
push ecx
lea ecx, dword ptr [esp + 0x4]
sub ecx, eax
sbb eax, eax
not eax
and ecx, eax
mov eax, esp
and eax, 0xFFFFF000
2:
cmp ecx, eax
jb 3f
mov eax, ecx
pop ecx
xchg eax, esp
mov eax, dword ptr [eax]
mov dword ptr [esp], eax
ret
3:
sub eax, 0x1000
test dword ptr [eax], eax
jmp 2b
",
options(noreturn)
);
}
}
#[cfg(target_arch = "x86_64")]
mod x86_64 {
/// This function accesses `gs:[0x10]`, which is `_TEB.NtTib.StackLimit`.
/// This value is not officially documented, but:
/// - it has not changed since Win9x.
/// - the CRT generates the same `__chkstk` implementation, which means it `gs:[0x10]` has to be
/// stable and always point to `StackLimit`.
#[naked]
#[no_mangle]
unsafe extern fn __chkstk() {
asm!(
"
sub rsp, 0x10
mov qword ptr [rsp], r10
mov qword ptr [rsp + 0x8], r11
xor r11, r11
lea r10, qword ptr [rsp + 0x18]
sub r10, rax
cmovb r10, r11
mov r11, qword ptr gs:[0x10]
cmp r10, r11
jnb 3f
and r10w, 0xF000
2:
lea r11, qword ptr [r11 - 0x1000]
test byte ptr [r11], r11b
cmp r10, r11
jb 2b
3:
mov r10, qword ptr [rsp]
mov r11, qword ptr [rsp + 0x8]
add rsp, 10h
ret
",
options(noreturn)
);
}
}