Skip to content

implement strip #83

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 1, 2024
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
71 changes: 69 additions & 2 deletions Cargo.lock

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

7 changes: 6 additions & 1 deletion dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ repository = "https://github.com/rustcoreutils/posixutils-rs.git"
plib = { path = "../plib" }
clap.workspace = true
gettext-rs.workspace = true
object = "0.33"
object = { version = "0.35", features = ["read", "build", "elf"]}
chrono = "0.4"
ar = "0.9"

[[bin]]
name = "nm"
Expand All @@ -21,3 +22,7 @@ path = "src/nm.rs"
name = "ar"
path = "src/ar.rs"
required-features = ["object/read_core", "object/archive"]

[[bin]]
name = "strip"
path = "src/strip.rs"
156 changes: 156 additions & 0 deletions dev/src/strip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
//
// Copyright (c) 2024 Hemi Labs, Inc.
//
// This file is part of the posixutils-rs project covered under
// the MIT License. For the full license text, please see the LICENSE
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//

use std::{
ffi::{OsStr, OsString},
io::Read,
};

use clap::Parser;
use gettextrs::{bind_textdomain_codeset, textdomain};
use object::{
archive,
build::elf::{Builder, Section, SectionData},
elf,
};
use plib::PROJECT_NAME;

/// strip - remove unnecessary information from strippable files
#[derive(Parser)]
#[command(author, version, about, long_about)]
struct Args {
input_files: Vec<OsString>,
}

fn is_debug_section(name: &[u8]) -> bool {
// names taken from the GNU binutils sources
name.starts_with(b".debug")
|| name.starts_with(b".gnu.debuglto_.debug_")
|| name.starts_with(b".gnu.linkonce.wi.")
|| name.starts_with(b".zdebug")
|| name.starts_with(b".line")
|| name.starts_with(b".stab")
|| name.starts_with(b".gdb_index")
}

fn strip_section(section: &Section) -> bool {
is_debug_section(section.name.as_slice())
// by removing all the symbols in the symbol table,
// the below sections will all be empty
|| section.sh_type == elf::SHT_GROUP
|| section.sh_type == elf::SHT_RELA
|| section.sh_type == elf::SHT_REL
// after we removed all symbols, the
// symbol table contains only the undefined
// symbol entry, which can be removed
|| section.name == ".symtab".into()
// this section contains the strings of the symbol
// table which are no longer used
// after we removed all the symbols
|| matches!(section.data, SectionData::String)
}

type StripResult = Result<Vec<u8>, Box<dyn std::error::Error>>;

fn strip(data: &[u8]) -> StripResult {
let mut builder = Builder::read(data)?;

for symbol in &mut builder.symbols {
symbol.delete = true;
}
for section in &mut builder.sections {
if strip_section(section) {
section.delete = true;
}
}
builder.delete_orphans();
let mut contents = Vec::new();
builder.write(&mut contents)?;
Ok(contents)
}

fn strip_archive(data: &[u8], file_name: &OsStr) -> StripResult {
let mut archive = ar::Archive::new(data);
let mut result = Vec::new();
let mut stripped_archive = ar::Builder::new(&mut result);
while let Some(entry) = archive.next_entry() {
let mut entry = entry?;
let mut data = Vec::new();
entry.read_to_end(&mut data)?;
let header = entry.header();

if is_elf(&data) {
let new_data = strip(&data)?;
let mut new_header = header.clone();
new_header.set_size(new_data.len() as u64);
stripped_archive.append(&new_header, &*new_data)?;
} else {
eprintln!(
"strip: ({}){}: file format not recognized",
file_name.to_string_lossy(),
String::from_utf8_lossy(header.identifier()),
);
}
}
Ok(result)
}

fn is_elf(data: &[u8]) -> bool {
data.starts_with(&elf::ELFMAG)
}

fn is_archive(data: &[u8]) -> bool {
data.starts_with(&archive::MAGIC)
}

fn strip_file(file: &OsStr) {
let contents = match std::fs::read(file) {
Ok(contents) => contents,
Err(err) => {
eprintln!("strip: error reading {}: {}", file.to_string_lossy(), err);
return;
}
};
let stripped_contents = if is_elf(&contents) {
strip(&contents)
} else if is_archive(&contents) {
strip_archive(&contents, file)
} else {
eprintln!(
"strip: {}: file format not recognized",
file.to_string_lossy()
);
return;
};
match stripped_contents {
Ok(stripped_contents) => {
if let Err(err) = std::fs::write(file, stripped_contents) {
eprintln!(
"strip: error writing file {}: {}",
file.to_string_lossy(),
err
);
}
}
Err(err) => {
eprintln!("strip: {}: {}", file.to_string_lossy(), err);
}
}
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
textdomain(PROJECT_NAME)?;
bind_textdomain_codeset(PROJECT_NAME, "UTF-8")?;

let args = Args::parse();
for file in args.input_files {
strip_file(&file);
}
Ok(())
}
Loading