injectionforge/build.rs

78 lines
2.6 KiB
Rust
Raw Normal View History

use std::env;
#[cfg(feature = "dotnet")]
use csbindgen;
fn main() {
2023-05-26 15:40:45 +03:00
println!("cargo:rerun-if-env-changed=FRIDA_CODE");
println!("cargo:rerun-if-env-changed=DLL_PROXY");
2023-05-26 15:40:45 +03:00
if let Ok(lib_path) = env::var("DLL_PROXY") {
println!("cargo:rerun-if-changed={}", &lib_path);
2024-03-03 13:10:58 +02:00
use goblin::Object;
2023-05-25 17:48:18 +03:00
let path = std::path::Path::new(&lib_path);
let lib_filename = path.file_name().unwrap().to_str().unwrap();
2023-05-25 17:48:18 +03:00
let lib_bytes = std::fs::read(path).expect(format!("Failed to open given library file {}", &lib_filename).as_str());
let object = Object::parse(&lib_bytes).expect(format!("Failed to parse given libary file {}", &lib_filename).as_str());
2023-05-25 17:48:18 +03:00
let (exports, lib_name): (Vec<&str>, String) = match object {
2024-03-03 13:10:58 +02:00
#[cfg(target_os = "windows")]
Object::PE(o) => {
(o.exports
2023-05-26 17:26:11 +03:00
.iter()
2024-02-26 18:54:54 +02:00
.map(|e| e.name.unwrap())
2023-05-26 17:26:11 +03:00
.collect(),
2024-03-03 13:10:58 +02:00
o.name.expect("Couldn't read the name of the DLL. Is it a .NET DLL? It's not supported").replace(".dll", ""))
}
#[cfg(target_os = "linux")]
Object::Elf(o) => {
(o.dynsyms
.iter()
.filter(|e| e.is_function() && !e.is_import())
.map(|e| o.dynstrtab.get_at(e.st_name).unwrap())
.collect(),
o.soname.expect("Couldn't read the name of the SO.").replace(".so", ""))
},
// #[cfg(target_os = "darwin")]
// Object::Mach(goblin::mach::Mach::Binary(o)) => {
// (o.dynsyms
// .iter()
// .filter(|e| e.is_function() && !e.is_import())
// .map(|e| o.dynstrtab.get_at(e.st_name).unwrap())
// .collect(),
// o.name.expect("Couldn't read the name of the DLL. Is it a .NET DLL? It's not supported").replace(".dll", ""))
// },
_ => {
2024-03-03 13:10:58 +02:00
println!("Only PE (.dll) and ELF (.so) files are supported in their respective target platforms.");
2023-05-26 17:26:11 +03:00
std::process::exit(1);
},
2023-05-25 17:48:18 +03:00
};
2024-03-03 13:10:58 +02:00
#[cfg(target_os = "windows")]
2023-05-25 17:48:18 +03:00
for e in exports.iter() {
2023-06-05 15:17:52 +03:00
println!("cargo:warning=Exported function: {} => {}-orig.{}", e, lib_name, e);
println!("cargo:rustc-link-arg=/export:{}={}-orig.{}", e, lib_name, e);
2023-05-25 17:48:18 +03:00
}
2024-03-03 13:10:58 +02:00
#[cfg(not(target_os = "windows"))]
for e in exports.iter() {
println!("cargo:warning=Exported function: {}", e);
}
println!("cargo:warning=Expected library name: {}-orig.dll", lib_name);
println!("cargo:rustc-env=LIB_NAME={}-orig.dll", lib_name);
2023-05-25 17:48:18 +03:00
}
#[cfg(feature = "dotnet")]
{
let lib_path = concat!(env!("CARGO_MANIFEST_DIR"), "/src/lib.rs");
let csharp_file = concat!(env!("CARGO_MANIFEST_DIR"), "/dotnet/NativeMethods.g.cs");
csbindgen::Builder::default()
.input_extern_file(lib_path)
.csharp_dll_name("deepfreeze")
.generate_csharp_file(csharp_file)
.unwrap();
}
}