-
Notifications
You must be signed in to change notification settings - Fork 2
/
exports.rs
314 lines (310 loc) · 9.96 KB
/
exports.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
extern crate regex;
use regex::Regex;
use std::collections::HashMap;
use std::env::args;
use std::fs::{read_dir, File};
use std::io::{BufWriter, Write};
use std::path::Path;
use std::process::Command;
use std::str::FromStr;
//Fields:
//DLL name = name of DLL
//Hint = ordinal hint
//Library = name of import library
//Machine = 8664 (x64)|14C (x86)
//Name = name of the symbol in the DLL itself
//Name type = undecorate|name|ordinal|no prefix
//Ordinal = ordinal of symbol in dll for exports with no name
//SizeOfData = size of data in bytes
//Symbol name = possibly mangled symbol that the code links against
//TimeDateStamp = some sort of time stamp
//Type = code|data|const
//Version = 0
pub const DUMPBIN: &'static str = r"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.25.28610\bin\Hostx64\x64\dumpbin.exe";
pub const SDKBASE: &'static str = r"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um";
pub const WINBASE: &'static str = r"E:\Code\winapi-rs";
pub const DLLTOOL64: &'static str = r"D:\Software\mingw64\x86_64-w64-mingw32\bin\dlltool.exe";
pub const DLLTOOL32: &'static str = r"D:\Software\mingw32\i686-w64-mingw32\bin\dlltool.exe";
pub const SDK64: &'static str = r"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x64";
pub const SDK32: &'static str = r"C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\um\x86";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum Machine {
X64,
X86,
}
impl Machine {
fn msvc(&self) -> &'static str {
match self {
&Machine::X64 => "x64",
&Machine::X86 => "x86",
}
}
fn rust(&self) -> &'static str {
match self {
&Machine::X64 => "x86_64",
&Machine::X86 => "i686",
}
}
}
impl FromStr for Machine {
type Err = ();
fn from_str(s: &str) -> Result<Machine, ()> {
Ok(match s {
"8664 (x64)" => Machine::X64,
"14C (x86)" => Machine::X86,
x => panic!("Unknown Machine of {:?}", x),
})
}
}
#[derive(Debug)]
enum NameType {
Undecorate,
Name,
Ordinal,
NoPrefix,
}
impl FromStr for NameType {
type Err = ();
fn from_str(s: &str) -> Result<NameType, ()> {
Ok(match s {
"undecorate" => NameType::Undecorate,
"name" => NameType::Name,
"ordinal" => NameType::Ordinal,
"no prefix" => NameType::NoPrefix,
x => panic!("Unknown Name type of {:?}", x),
})
}
}
#[derive(Debug, Eq, PartialEq)]
enum Type {
Code,
Data,
Const,
}
impl FromStr for Type {
type Err = ();
fn from_str(s: &str) -> Result<Type, ()> {
Ok(match s {
"code" => Type::Code,
"data" => Type::Data,
"const" => Type::Const,
x => panic!("Unknown Type of {:?}", x),
})
}
}
#[derive(Debug)]
struct Export {
dll: String,
hint: Option<u32>,
machine: Machine,
name: Option<String>,
name_type: NameType,
ordinal: Option<u32>,
size_of_data: u32,
symbol_name: String,
time_date_stamp: String,
data_type: Type,
}
impl Export {
fn write<T>(&self, fout: &mut T, arch: Machine)
where
T: Write,
{
match self.name_type {
NameType::Undecorate | NameType::NoPrefix => {
let symbol = sanitize(&self.symbol_name, arch);
writeln!(fout, "{}", symbol).unwrap();
}
NameType::Name => {
writeln!(fout, "{}", self.symbol_name).unwrap();
}
NameType::Ordinal => {
let symbol = sanitize(&self.symbol_name, arch);
writeln!(fout, "{} @{}", symbol, self.ordinal.unwrap()).unwrap();
}
}
}
}
fn export(name: &str, arch: Machine) {
println!("Working on {}", name);
let plibmsvc = Path::new(SDKBASE)
.join(arch.msvc())
.join(format!("{}.lib", name));
if !plibmsvc.exists() {
println!("Library does not exist!");
return;
}
let reg = Regex::new("^ ([a-zA-Z][a-zA-Z ]*?) *: (.*)$").unwrap();
let cin = Command::new(DUMPBIN)
.arg("/HEADERS")
.arg(&plibmsvc)
.output()
.unwrap();
let input = String::from_utf8_lossy(&cin.stdout);
let mut next: HashMap<String, String> = HashMap::new();
let mut exports: Vec<Export> = Vec::new();
for line in input.lines() {
if let Some(cap) = reg.captures(line) {
let key = cap.get(1).unwrap();
let val = cap.get(2).unwrap();
next.insert(key.as_str().into(), val.as_str().into());
} else if !next.is_empty() {
let version: u32 = next.remove("Version").unwrap().parse().unwrap();
assert_eq!(version, 0);
let export = Export {
dll: next.remove("DLL name").unwrap(),
hint: next.remove("Hint").map(|x| x.parse().unwrap()),
machine: next.remove("Machine").unwrap().parse().unwrap(),
name: next.remove("Name"),
name_type: next.remove("Name type").unwrap().parse().unwrap(),
ordinal: next.remove("Ordinal").map(|x| x.parse().unwrap()),
size_of_data: u32::from_str_radix(&next.remove("SizeOfData").unwrap(), 16).unwrap(),
symbol_name: next.remove("Symbol name").unwrap(),
time_date_stamp: next.remove("TimeDateStamp").unwrap(),
data_type: next.remove("Type").unwrap().parse().unwrap(),
};
assert!(next.is_empty());
exports.push(export);
}
}
let mut dll_exports = HashMap::new();
for export in exports {
if export.data_type != Type::Code {
// println!("Skipping non-code {:?}", export);
continue;
}
if export.symbol_name.contains("@@") {
// println!("Skipping C++ {:?}", export.symbol_name);
continue;
}
dll_exports
.entry(export.dll.clone())
.or_insert_with(|| Vec::new())
.push(export);
}
for (_, exports) in &mut dll_exports {
exports.sort_by(|a, b| a.name.cmp(&b.name));
}
let dlltool = match arch {
Machine::X64 => DLLTOOL64,
Machine::X86 => DLLTOOL32,
};
if dll_exports.len() == 0 {
return;
} else if dll_exports.len() == 1 {
let (dll, exports) = dll_exports.into_iter().next().unwrap();
let pdef = Path::new(WINBASE)
.join(arch.rust())
.join("def")
.join(format!("{}.def", name));
let plibgnu = Path::new(WINBASE)
.join(arch.rust())
.join("lib")
.join(format!("libwinapi_{}.a", name));
println!("{}", pdef.display());
let mut fout = BufWriter::new(File::create(&pdef).unwrap());
writeln!(&mut fout, "LIBRARY {}", dll).unwrap();
writeln!(&mut fout, "EXPORTS").unwrap();
for export in exports {
export.write(&mut fout, arch);
}
drop(fout);
Command::new(dlltool)
.arg("-d")
.arg(&pdef)
.arg("-l")
.arg(&plibgnu)
.arg("-k")
.output()
.unwrap();
} else {
let plib = Path::new(WINBASE)
.join(arch.rust())
.join("lib")
.join(format!("libwinapi_{}.a", name));
let mut flib = BufWriter::new(File::create(&plib).unwrap());
writeln!(&mut flib, "INPUT(").unwrap();
for (dll, exports) in dll_exports {
let stem = Path::new(&dll)
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_lowercase();
let pdef = Path::new(WINBASE)
.join(arch.rust())
.join("def")
.join(format!("{}-{}.def", name, stem));
let psublib = Path::new(WINBASE)
.join(arch.rust())
.join("lib")
.join(format!("libwinapi_{}-{}.a", name, stem));
let mut fout = BufWriter::new(File::create(&pdef).unwrap());
writeln!(&mut fout, "LIBRARY {}", dll).unwrap();
writeln!(&mut fout, "EXPORTS").unwrap();
for export in exports {
export.write(&mut fout, arch);
}
writeln!(&mut flib, "libwinapi_{}-{}.a", name, stem).unwrap();
drop(fout);
Command::new(dlltool)
.arg("-d")
.arg(&pdef)
.arg("-l")
.arg(&psublib)
.arg("-k")
.output()
.unwrap();
}
writeln!(&mut flib, ")").unwrap();
}
}
fn sanitize(symbol: &str, arch: Machine) -> &str {
if arch != Machine::X86 {
symbol
} else if &symbol[0..1] == "_" {
&symbol[1..]
} else {
symbol
}
}
fn main() {
let args = args().skip(1).collect::<Vec<_>>();
if args.is_empty() {
for entry in read_dir(SDK64).unwrap() {
let path = entry.unwrap().path();
if path
.extension()
.and_then(|x| x.to_str())
.map(|x| x.to_lowercase() != "lib")
.unwrap_or(true)
{
continue;
}
export(
&path.file_stem().unwrap().to_str().unwrap().to_lowercase(),
Machine::X64,
);
}
for entry in read_dir(SDK32).unwrap() {
let path = entry.unwrap().path();
if path
.extension()
.and_then(|x| x.to_str())
.map(|x| x.to_lowercase() != "lib")
.unwrap_or(true)
{
continue;
}
export(
&path.file_stem().unwrap().to_str().unwrap().to_lowercase(),
Machine::X86,
);
}
} else {
for arg in args {
export(&arg, Machine::X64);
export(&arg, Machine::X86);
}
}
}