-
Notifications
You must be signed in to change notification settings - Fork 0
/
dllmain.rs
124 lines (109 loc) · 3.48 KB
/
dllmain.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
mod min_crt_init {
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
use core::ffi::c_void;
use core::option::Option;
type CallBack = Option<unsafe extern "system" fn(*const c_void, u32, *const c_void)>;
type CrtInit = Option<unsafe extern "C" fn()>;
#[repr(C)]
struct IMAGE_TLS_DIRECTORY {
StartAddressOfRawData: *const u64,
EndAddressOfRawData: *const u64,
AddressOfIndex: *const u32,
AddressOfCallBacks: *const CallBack,
SizeOfZeroFill: u32,
Characteristics: u32,
}
unsafe impl Sync for IMAGE_TLS_DIRECTORY {}
unsafe extern "system" fn nop(_: *const c_void, _: u32, _: *const c_void) {}
#[no_mangle]
#[link_section = ".CRT$XLA"]
static __xl_a: CallBack = Some(nop);
#[used]
#[link_section = ".CRT$XLZ"]
static __xl_z: CallBack = None;
#[link_section = ".tls"]
static _tls_start: u64 = 0;
#[link_section = ".tls$ZZZ"]
static _tls_end: u64 = 0;
#[no_mangle]
static mut _tls_index: u32 = 0;
#[no_mangle]
static _tls_used: IMAGE_TLS_DIRECTORY = IMAGE_TLS_DIRECTORY {
StartAddressOfRawData: &_tls_start,
EndAddressOfRawData: &_tls_end,
AddressOfIndex: unsafe { core::ptr::addr_of!(_tls_index) },
AddressOfCallBacks: &__xl_a,
SizeOfZeroFill: 0,
Characteristics: 0,
};
#[link_section = ".CRT$XCA"]
static __xc_a: CrtInit = None;
#[link_section = ".CRT$XCZ"]
static __xc_z: CrtInit = None;
#[no_mangle]
unsafe extern "system" fn DllMain(h: *const c_void, reason: u32, _: *const c_void) -> i32 {
const DLL_PROCESS_ATTACH: u32 = 1;
extern "system" { fn DisableThreadLibraryCalls(h: *const c_void); }
if reason == DLL_PROCESS_ATTACH {
DisableThreadLibraryCalls(h);
let mut ptr = &__xc_a as *const CrtInit;
while ptr < &__xc_z {
if let Some(f) = *ptr {
f();
}
ptr = ptr.add(1);
}
}
1
}
#[no_mangle]
extern "C" fn memcpy(dst: *mut u8, src: *const u8, n: usize) -> *mut u8 {
for i in 0..n {
unsafe { *dst.add(i) = *src.add(i) };
}
dst
}
#[no_mangle]
extern "C" fn memmove(dst: *mut u8, src: *const u8, n: usize) -> *mut u8 {
if src >= unsafe { dst.add(n) } || unsafe { src.add(n) } <= dst {
for i in 0..n {
unsafe { *dst.add(i) = *src.add(i) };
}
} else if src > dst {
for i in 0..n {
unsafe { dst.add(i).write_volatile(src.add(i).read_volatile()) };
}
} else if src != dst {
for i in 1..n + 1 {
unsafe { dst.add(n - i).write_volatile(src.add(n - i).read_volatile()) };
}
}
dst
}
#[no_mangle]
extern "C" fn memset(dst: *mut u8, c: i32, n: usize) -> *mut u8 {
for i in 0..n {
unsafe { *dst.add(i) = c as u8 };
}
dst
}
#[no_mangle]
extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 {
for i in 0..n {
let ret = unsafe { (*s1.add(i) as i32) - (*s2.add(i) as i32) };
if ret != 0 {
return ret;
}
}
0
}
#[no_mangle]
extern "C" fn strlen(s: *const u8) -> usize {
let mut len = 0usize;
while unsafe { *s.add(len) } != 0u8 {
len += 1;
}
len
}
}