Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@

# Denote all files that are truly binary and should not be modified.
tools/** binary
tools/rustup_wrapper/** -binary
tools/rustup-wrapper/** -binary
tools/elf-cleaner/** -binary
*.jar binary
*.exe binary
*.apk binary
Expand Down
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,3 @@
[submodule "crt0"]
path = native/src/external/crt0
url = https://github.com/topjohnwu/crt0.git
[submodule "termux-elf-cleaner"]
path = tools/termux-elf-cleaner
url = https://github.com/termux/termux-elf-cleaner.git
34 changes: 12 additions & 22 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,16 @@ def cmd_out(cmds: list):


def clean_elf():
if is_windows:
elf_cleaner = Path("tools", "elf-cleaner.exe")
else:
elf_cleaner = Path("native", "out", "elf-cleaner")
if not elf_cleaner.exists():
execv(
[
"gcc",
'-DPACKAGE_NAME="termux-elf-cleaner"',
'-DPACKAGE_VERSION="2.1.1"',
'-DCOPYRIGHT="Copyright (C) 2022 Termux."',
"tools/termux-elf-cleaner/elf-cleaner.cpp",
"tools/termux-elf-cleaner/arghandling.c",
"-o",
elf_cleaner,
]
)
cmds = [elf_cleaner, "--api-level", "23"]
cargo_toml = Path("tools", "elf-cleaner", "Cargo.toml")
cmds = ["run", "--release", "--manifest-path", cargo_toml]
if args.verbose == 0:
cmds.append("-q")
elif args.verbose > 1:
cmds.append("--verbose")
cmds.append("--")
cmds.extend(glob.glob("native/out/*/magisk"))
cmds.extend(glob.glob("native/out/*/magiskpolicy"))
execv(cmds)
run_cargo(cmds)


def run_ndk_build(cmds: list):
Expand Down Expand Up @@ -484,6 +473,7 @@ def cleanup():

if "native" in targets:
rm_rf(Path("native", "out"))
rm_rf(Path("tools", "elf-cleaner", "target"))

if "app" in targets:
header("* Cleaning app")
Expand Down Expand Up @@ -553,8 +543,8 @@ def setup_rustup():
tgt = wrapper_dir / src.name
tgt.symlink_to(f"rustup{EXE_EXT}")

# Build rustup_wrapper
wrapper_src = Path("tools", "rustup_wrapper")
# Build rustup-wrapper
wrapper_src = Path("tools", "rustup-wrapper")
cargo_toml = wrapper_src / "Cargo.toml"
cmds = ["build", "--release", f"--manifest-path={cargo_toml}"]
if args.verbose > 1:
Expand All @@ -564,7 +554,7 @@ def setup_rustup():
# Replace rustup with wrapper
wrapper = wrapper_dir / (f"rustup{EXE_EXT}")
wrapper.unlink(missing_ok=True)
cp(wrapper_src / "target" / "release" / (f"rustup_wrapper{EXE_EXT}"), wrapper)
cp(wrapper_src / "target" / "release" / (f"rustup-wrapper{EXE_EXT}"), wrapper)
wrapper.chmod(0o755)


Expand Down
Binary file removed tools/elf-cleaner.exe
Binary file not shown.
1 change: 1 addition & 0 deletions tools/elf-cleaner/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
133 changes: 133 additions & 0 deletions tools/elf-cleaner/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tools/elf-cleaner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 changes: 85 additions & 0 deletions tools/elf-cleaner/src/main.rs
Original file line number Diff line number Diff line change
@@ -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))
}
File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rustup_wrapper"
name = "rustup-wrapper"
version = "0.0.0"
edition = "2021"
edition = "2024"

[dependencies]
home = "0.5"
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion tools/termux-elf-cleaner
Submodule termux-elf-cleaner deleted from 55b681