Skip to content

Commit

Permalink
nm: Get closer to POSIX spec output
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarzik committed Mar 9, 2024
1 parent 71f1328 commit 018c30b
Showing 1 changed file with 36 additions and 41 deletions.
77 changes: 36 additions & 41 deletions dev/src/nm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
// file in the root directory of this project.
// SPDX-License-Identifier: MIT
//
// TODO:
// - vary output based on args
// - sort output
//

extern crate clap;
extern crate plib;
Expand Down Expand Up @@ -76,41 +80,6 @@ struct Args {
file: String,
}

fn show_object_file(args: &Args) -> Result<(), Box<dyn std::error::Error>> {
let file_path = &args.file;
{
let filedata = match fs::read(file_path) {
Ok(file) => file,
Err(err) => {
println!("Failed to open file '{}': {}", file_path, err,);
return Err(Box::new(err));
}
};
let file = match object::File::parse(&*filedata) {
Ok(file) => file,
Err(err) => {
println!("Failed to parse file '{}': {}", file_path, err);
return Err(Box::new(err));
}
};

let section_kinds = file.sections().map(|s| (s.index(), s.kind())).collect();

println!("Debugging symbols:");
for symbol in file.symbols() {
print_symbol(&symbol, &section_kinds);
}
println!();

println!("Dynamic symbols:");
for symbol in file.dynamic_symbols() {
print_symbol(&symbol, &section_kinds);
}
}

Ok(())
}

fn print_symbol(symbol: &Symbol<'_, '_>, section_kinds: &HashMap<SectionIndex, SectionKind>) {
if let SymbolKind::Section | SymbolKind::File = symbol.kind() {
return;
Expand Down Expand Up @@ -142,12 +111,38 @@ fn print_symbol(symbol: &Symbol<'_, '_>, section_kinds: &HashMap<SectionIndex, S
} else {
print!("{:016x} ", symbol.address());
}
println!(
"{:016x} {} {}",
symbol.size(),
kind,
symbol.name().unwrap_or("<unknown>"),
);
println!("{} {}", kind, symbol.name().unwrap_or("<unknown>"),);
}

fn show_object_file(args: &Args) -> Result<(), Box<dyn std::error::Error>> {
let file_path = &args.file;
{
let filedata = match fs::read(file_path) {
Ok(file) => file,
Err(err) => {
println!("Failed to open file '{}': {}", file_path, err,);
return Err(Box::new(err));
}
};
let file = match object::File::parse(&*filedata) {
Ok(file) => file,
Err(err) => {
println!("Failed to parse file '{}': {}", file_path, err);
return Err(Box::new(err));
}
};

let section_kinds = file.sections().map(|s| (s.index(), s.kind())).collect();

for symbol in file.symbols() {
print_symbol(&symbol, &section_kinds);
}
for symbol in file.dynamic_symbols() {
print_symbol(&symbol, &section_kinds);
}
}

Ok(())
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down

0 comments on commit 018c30b

Please sign in to comment.