mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-10-27 12:19:13 +00:00
Use Rust elf-cleaner implementation
This commit is contained in:
Binary file not shown.
1
tools/elf-cleaner/.gitignore
vendored
Normal file
1
tools/elf-cleaner/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
133
tools/elf-cleaner/Cargo.lock
generated
Normal file
133
tools/elf-cleaner/Cargo.lock
generated
Normal file
@@ -0,0 +1,133 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "adler2"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.97"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "crc32fast"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "elf-cleaner"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"object",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "11faaf5a5236997af9848be0bef4db95824b1d534ebc64d0f0c6cf3e67bd38dc"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bf151400ff0baff5465007dd2f3e717f3fe502074ca563069ce3a6629d07b289"
|
||||
dependencies = [
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3954d50fe15b02142bf25d3b8bdadb634ec3948f103d04ffe3031bc8fe9d7058"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e3e04debbb59698c15bacbb6d93584a8c0ca9cc3213cb423d31f760d8843ce5"
|
||||
dependencies = [
|
||||
"adler2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.36.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62948e14d923ea95ea2c7c86c71013138b66525b86bdc08d2dcc262bdb497b87"
|
||||
dependencies = [
|
||||
"crc32fast",
|
||||
"flate2",
|
||||
"hashbrown",
|
||||
"indexmap",
|
||||
"memchr",
|
||||
"ruzstd",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ruzstd"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fad02996bfc73da3e301efe90b1837be9ed8f4a462b6ed410aa35d00381de89f"
|
||||
dependencies = [
|
||||
"twox-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "twox-hash"
|
||||
version = "1.6.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"static_assertions",
|
||||
]
|
||||
13
tools/elf-cleaner/Cargo.toml
Normal file
13
tools/elf-cleaner/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "elf-cleaner"
|
||||
version = "0.0.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
object = { version = "0.36", features = ["build"] }
|
||||
anyhow = "1.0"
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
85
tools/elf-cleaner/src/main.rs
Normal file
85
tools/elf-cleaner/src/main.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use object::build::elf::{Builder, Dynamic, SectionData};
|
||||
use object::elf;
|
||||
use std::{env, fs};
|
||||
|
||||
// Implementation adapted from https://github.com/termux/termux-elf-cleaner
|
||||
|
||||
// Missing ELF constants
|
||||
const DT_AARCH64_BTI_PLT: u32 = elf::DT_LOPROC + 1;
|
||||
const DT_AARCH64_PAC_PLT: u32 = elf::DT_LOPROC + 3;
|
||||
const DT_AARCH64_VARIANT_PCS: u32 = elf::DT_LOPROC + 5;
|
||||
|
||||
const SUPPORTED_DT_FLAGS: u32 = elf::DF_1_NOW | elf::DF_1_GLOBAL;
|
||||
|
||||
fn print_remove_dynamic(name: &str, path: &str) {
|
||||
println!("Removing dynamic section entry {} in '{}'", name, path);
|
||||
}
|
||||
|
||||
fn process_elf(path: &str) -> anyhow::Result<()> {
|
||||
let bytes = fs::read(path)?;
|
||||
let mut elf = Builder::read(bytes.as_slice())?;
|
||||
let is_aarch64 = elf.header.e_machine == elf::EM_AARCH64;
|
||||
|
||||
elf.sections.iter_mut().for_each(|section| {
|
||||
if let SectionData::Dynamic(entries) = &mut section.data {
|
||||
// Remove unsupported entries
|
||||
entries.retain(|e| {
|
||||
let tag = e.tag();
|
||||
match tag {
|
||||
elf::DT_RPATH => {
|
||||
print_remove_dynamic("DT_RPATH", path);
|
||||
return false;
|
||||
}
|
||||
elf::DT_RUNPATH => {
|
||||
print_remove_dynamic("DT_RUNPATH", path);
|
||||
return false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
if is_aarch64 {
|
||||
match tag {
|
||||
DT_AARCH64_BTI_PLT => {
|
||||
print_remove_dynamic("DT_AARCH64_BTI_PLT", path);
|
||||
return false;
|
||||
}
|
||||
DT_AARCH64_PAC_PLT => {
|
||||
print_remove_dynamic("DT_AARCH64_PAC_PLT", path);
|
||||
return false;
|
||||
}
|
||||
DT_AARCH64_VARIANT_PCS => {
|
||||
print_remove_dynamic("DT_AARCH64_VARIANT_PCS", path);
|
||||
return false;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
true
|
||||
});
|
||||
// Remove unsupported flags
|
||||
for entry in entries.iter_mut() {
|
||||
if let Dynamic::Integer { tag, val } = entry {
|
||||
if *tag == elf::DT_FLAGS_1 {
|
||||
let new_flags = *val & SUPPORTED_DT_FLAGS as u64;
|
||||
if new_flags != *val {
|
||||
println!(
|
||||
"Replacing unsupported DT_FLAGS_1 {:#x} with {:#x} in '{}'",
|
||||
*val, new_flags, path
|
||||
);
|
||||
*val = new_flags;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let mut out_bytes = Vec::new();
|
||||
elf.write(&mut out_bytes)?;
|
||||
fs::write(path, &out_bytes)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
env::args().skip(1).try_for_each(|s| process_elf(&s))
|
||||
}
|
||||
@@ -12,7 +12,7 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustup_wrapper"
|
||||
name = "rustup-wrapper"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"home",
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "rustup_wrapper"
|
||||
name = "rustup-wrapper"
|
||||
version = "0.0.0"
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
home = "0.5"
|
||||
Submodule tools/termux-elf-cleaner deleted from 55b68119ab
Reference in New Issue
Block a user