Skip to content
Open
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
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Avoid generic proof disclaimers such as "presence is not established"; use preci

## ELF symbol inspection

`fat_cli` shares one `st_shndx`-based ELF section reader (`symbol_elf`) between symbol discovery (`elf_symbols`) and enrichment (`symbol_enrich`); never infer sections from virtual addresses. Section and region labels come from section flags, and `--in-flash` includes read-only and executable sections. Demangling keeps raw names for selection and drains `c++filt` concurrently so large symbol tables cannot deadlock. Diff reports use their own schema version, and selection, rendering, and diffing remain separate modules.
`fat_cli` shares one `st_shndx`-based ELF section reader (`symbol_elf`) between symbol discovery (`elf_symbols`) and enrichment (`symbol_enrich`); never infer sections from virtual addresses. Section and region labels come from section flags, and `--in-flash` includes read-only and executable sections. Demangling keeps raw names for selection and drains `c++filt` concurrently so large symbol tables cannot deadlock. Diff reports use their own schema version, and selection, rendering, and diffing remain separate modules. Totals are the union of symbol ranges per section, so aliases count once; the default table is capped at 25 rows and hides symbols that occupy no image bytes, while CSV, JSONL, and `--json` stay complete unless `--limit` is given.

## Decompilation

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
`inspect layout`, including custom table locations, optional MD5 validation,
legacy tables, and separate file offsets and flash addresses.

- `inspect symbols` lists ELF symbols with byte sizes, section and region filters, batched name input, bounded hex previews, demangling, text/CSV/JSON output, and two-file symbol diffs.
- `inspect symbols` lists ELF symbols with byte sizes, section and region filters, batched name input, bounded hex previews, demangling, text/CSV/JSON output, and two-file symbol diffs. The default table shows the 25 largest symbols with a bytes-by-section summary, hides zero-size, absolute, and undefined symbols (`--all` restores them, `--limit N` changes the row count), and counts aliased address ranges once in the total. Text diffs list only changed, added, and removed symbols.

## [2.1.0]

Expand Down
35 changes: 29 additions & 6 deletions crates/fat_cli/src/inspect_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ pub struct SymbolsArgs<'a> {
pub writable: bool,
pub in_ram: bool,
pub in_flash: bool,
pub sort: &'a str,
pub sort: Option<&'a str>,
pub reverse: bool,
pub requested_order: bool,
pub no_requested_order: bool,
pub limit: Option<usize>,
pub all: bool,
pub format: &'a str,
pub no_color: bool,
pub preview: usize,
Expand All @@ -234,6 +236,9 @@ fn emit(text: &str) -> DynResult<()> {
}
}

/// Rows shown by `fat inspect symbols` tables when no --limit is given.
const DEFAULT_SYMBOL_ROWS: usize = 25;

pub fn run_symbols(args: SymbolsArgs<'_>) -> DynResult<()> {
use crate::symbol_model::SelectionInfo;
use std::io::IsTerminal;
Expand All @@ -254,10 +259,12 @@ pub fn run_symbols(args: SymbolsArgs<'_>) -> DynResult<()> {
("--writable", args.writable),
("--in-ram", args.in_ram),
("--in-flash", args.in_flash),
("--sort", args.sort != "address"),
("--sort", args.sort.is_some()),
("--reverse", args.reverse),
("--requested-order", args.requested_order),
("--no-requested-order", args.no_requested_order),
("--limit", args.limit.is_some()),
("--all", args.all),
("--format", args.format != "table"),
("--no-color", args.no_color),
("--preview", args.preview != 0),
Expand Down Expand Up @@ -324,9 +331,9 @@ pub fn run_symbols(args: SymbolsArgs<'_>) -> DynResult<()> {
Some(raw) => Some(crate::symbol_selection::parse_symbol_type(raw)?),
None => None,
};
let sort = crate::symbol_selection::parse_sort(args.sort)?;
let sort = crate::symbol_selection::parse_sort(args.sort.unwrap_or_default())?;
let requested_order = args.requested_order
|| (!args.no_requested_order && sort == "address" && !names.is_empty());
|| (!args.no_requested_order && args.sort.is_none() && !names.is_empty());
let selection = SelectionInfo {
names,
filter: args.filter.map(str::to_string),
Expand All @@ -341,9 +348,11 @@ pub fn run_symbols(args: SymbolsArgs<'_>) -> DynResult<()> {
sort,
reverse: args.reverse,
requested_order,
include_all: args.all,
};
let requested_names = !selection.names.is_empty();

let selected = crate::symbol_selection::select(&base, &selection);
let mut selected = crate::symbol_selection::select(&base, &selection);
if args.fail_on_missing && !selected.missing_names.is_empty() {
let messages: Vec<String> = selected
.missing_names
Expand All @@ -352,10 +361,24 @@ pub fn run_symbols(args: SymbolsArgs<'_>) -> DynResult<()> {
.collect();
return Err(messages.join("; ").into());
}
// Only human-facing tables are capped by default; machine formats stay
// complete unless a limit is asked for.
let format = crate::symbol_format::parse_format(args.format)?;
let tabular = !args.json
&& matches!(
format,
crate::symbol_format::OutputFormat::Table
| crate::symbol_format::OutputFormat::Markdown
);
let limit = match args.limit {
Some(limit) => limit,
None if args.all || requested_names || !tabular => 0,
None => DEFAULT_SYMBOL_ROWS,
};
crate::symbol_selection::truncate(&mut selected, limit);
if args.json {
emit(&format!("{}\n", serde_json::to_string_pretty(&selected)?))?;
} else {
let format = crate::symbol_format::parse_format(args.format)?;
let color = !args.no_color && std::io::stdout().is_terminal();
let width = args.width.unwrap_or_else(|| {
std::env::var("COLUMNS")
Expand Down
33 changes: 30 additions & 3 deletions crates/fat_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2170,6 +2170,11 @@ enum InspectCommand {
json: bool,
},
/// List ELF symbols with their sizes in hex and decimal, and total the matched bytes.
///
/// By default the table shows the largest symbols first, capped at 25 rows,
/// with a per-section byte summary when rows are left out. Zero-size,
/// absolute and undefined symbols occupy no bytes in the image and are
/// hidden unless --all or exact --name selections are given.
Symbols {
/// ELF file to inspect; repeat with --diff to compare two files.
#[arg(long = "file")]
Expand Down Expand Up @@ -2199,38 +2204,56 @@ enum InspectCommand {
/// Only include this ELF symbol type: object, func, or any.
symbol_type: Option<String>,
#[arg(long)]
/// Restrict to writable sections (.data, .bss).
writable: bool,
#[arg(long = "in-ram")]
/// Restrict to writable RAM regions.
in_ram: bool,
#[arg(long = "in-flash")]
/// Restrict to flash-backed read-only and executable regions.
in_flash: bool,
#[arg(long, default_value = "address")]
sort: String,
#[arg(long)]
/// Sort key: size (largest first, the default), address, or name.
sort: Option<String>,
#[arg(long)]
/// Reverse the sort order.
reverse: bool,
#[arg(long = "requested-order")]
/// Keep --name/--names-file order (the default when --sort is not given).
requested_order: bool,
#[arg(long = "no-requested-order")]
/// Sort --name/--names-file results instead of keeping their order.
no_requested_order: bool,
#[arg(long)]
/// Show at most N rows; 0 shows every row. Tables default to 25 unless
/// exact names are requested; csv, jsonl, and --json default to all.
limit: Option<usize>,
#[arg(long)]
/// Include zero-size, absolute, and undefined symbols, and show every row.
all: bool,
#[arg(long, default_value = "table")]
/// Output format: table, markdown, csv, or jsonl.
format: String,
#[arg(long = "no-color")]
/// Disable ANSI color in table output.
no_color: bool,
#[arg(long, default_value_t = 0)]
/// Include up to N leading file bytes per symbol as hex; 0 disables (max 64).
preview: usize,
#[arg(long)]
/// Demangle C++ names with c++filt; selection still matches raw names.
demangle: bool,
#[arg(long)]
/// Prefer .dynsym over .symtab when both symbol tables exist.
dynsym: bool,
#[arg(long = "fail-on-missing")]
/// Exit with an error when a requested --name is not found.
fail_on_missing: bool,
#[arg(long)]
/// Compare symbol sizes between two --file arguments.
diff: bool,
#[arg(long)]
/// Emit the full selection report as JSON.
json: bool,
#[arg(long)]
/// Terminal width used for table output.
Expand Down Expand Up @@ -3145,6 +3168,8 @@ fn run() -> DynResult<()> {
reverse,
requested_order,
no_requested_order,
limit,
all,
format,
no_color,
preview,
Expand All @@ -3167,10 +3192,12 @@ fn run() -> DynResult<()> {
writable,
in_ram,
in_flash,
sort: &sort,
sort: sort.as_deref(),
reverse,
requested_order,
no_requested_order,
limit,
all,
format: &format,
no_color,
preview,
Expand Down
36 changes: 33 additions & 3 deletions crates/fat_cli/src/symbol_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,14 @@ pub fn render(report: &DiffReport) -> String {
report.unchanged
);

if report.rows.is_empty() {
// Unchanged rows are counted in the summary and kept in JSON; listing
// them would bury the differences.
let differing: Vec<_> = report
.rows
.iter()
.filter(|row| row.status != "same")
.collect();
if differing.is_empty() {
return format!("{summary}\n");
}

Expand All @@ -166,8 +173,7 @@ pub fn render(report: &DiffReport) -> String {
"Address A".to_string(),
"Address B".to_string(),
];
let body: Vec<[String; 7]> = report
.rows
let body: Vec<[String; 7]> = differing
.iter()
.map(|row| {
[
Expand Down Expand Up @@ -394,6 +400,30 @@ mod tests {
assert!(lines.iter().any(|line| line.contains("gone")));
}

#[test]
fn render_lists_only_differences() {
let a = report(
"a.elf",
vec![symbol("keep", 0x10, 10), symbol("grow", 0x40, 40)],
);
let b = report(
"b.elf",
vec![symbol("keep", 0x10, 10), symbol("grow", 0x40, 45)],
);
let result = diff(&a, &b);
assert_eq!(result.rows.len(), 2, "JSON keeps unchanged rows");
let text = render(&result);
assert!(text.contains("grow"), "{text}");
assert!(!text.contains("keep"), "{text}");
assert!(
text.ends_with("1 changed, 0 added, 0 removed, 1 unchanged\n"),
"{text}"
);

let identical = render(&diff(&a, &a));
assert_eq!(identical, "0 changed, 0 added, 0 removed, 2 unchanged\n");
}

#[test]
fn same_requires_matching_size_and_address() {
let a = report(
Expand Down
Loading
Loading