Skip to content

Commit

Permalink
lc subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
Arsynth committed Jun 2, 2023
1 parent ac48730 commit bbcbf3f
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ All notable changes will be listed here.
* New command - `rel`, prints relocation entries

### Fixed
* Sections parsing
* Sections parsing

## [0.3.0]
### Added
* New command - `lc`, prints load commands
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "schnauzer"
version = "0.2.9"
version = "0.3.0"
edition = "2021"
license = "MIT OR Apache-2.0"
repository = "https://github.com/Arsynth/schnauzer"
Expand Down
43 changes: 35 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ You also can specify path with `-p` or `--path` (But this not required)


## Supported commands
* [`syms FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#syms)
* [`rpaths FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#rpaths)
* [`dylibs FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#dylibs)
* [`segs FILE [--path <FILE>] [--help] [--arch <NAME>] [--segs] [--sects] [--short] [--noidx]`](#segs)
* [`fat FILE [--path <FILE>] [--help] [--arch <NAME>]`](#fat)
* [`headers FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#headers)
* [`rel FILE [--path <FILE>] [--help] [--arch <NAME>]`](#rel)
* [`schnauzer lc FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#lc)
* [`schnauzer syms FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#syms)
* [`schnauzer rpaths FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#rpaths)
* [`schnauzer dylibs FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#dylibs)
* [`schnauzer segs FILE [--path <FILE>] [--help] [--arch <NAME>] [--segs] [--sects] [--short] [--noidx]`](#segs)
* [`schnauzer fat FILE [--path <FILE>] [--help] [--arch <NAME>]`](#fat)
* [`schnauzer headers FILE [--path <FILE>] [--help] [--arch <NAME>] [--short] [--noidx]`](#headers)
* [`schnauzer rel FILE [--path <FILE>] [--help] [--arch <NAME>]`](#rel)

### Default
```shell
Expand Down Expand Up @@ -76,6 +77,32 @@ Fat arch:
|*flags: 0x00000000
...
```

### lc
```shell
# Prints load commands
schnauzer lc path_to_binary
```
```
Arch #0 (Arch: x86_64, File type: Exec, Flags: 0x00200085):
cmd: LC_SEGMENT_64 cmdsize: 72
segname: __PAGEZERO
vmaddr: 0x0000000000000000
vmsize: 0x0000000100000000
fileoff: 0
filesize: 0
maxprot: 0x00000000
initprot: 0x00000000
nsects: 0
flags: 0x00000000
cmd: LC_SEGMENT_64 cmdsize: 552
segname: __TEXT
vmaddr: 0x0000000100000000
vmsize: 0x0000000000004000
fileoff: 0
```

### syms
```shell
# Prints symtab
Expand Down Expand Up @@ -221,7 +248,7 @@ address pcrel length extern type scattered symbolnum/value

```toml
[dependencies]
schnauzer = "0.2.9"
schnauzer = "0.3.0"
```

### Examples
Expand Down
101 changes: 101 additions & 0 deletions src/commands/lc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use super::common;
use super::common::options::AddToOptions;
use super::common::Format;
use super::common::ObjectFilter;
use super::handler::*;
use super::Printer;
use super::Result;
use crate::auto_enum_fields::*;
use crate::*;
use colored::*;
use getopts::*;

static SUBCOMM_NAME: &str = "lc";

pub(super) struct LcHandler {
pub(super) printer: Printer,
}

impl LcHandler {
pub(super) fn new(printer: Printer) -> Self {
LcHandler { printer }
}
}

impl Handler for LcHandler {
fn command_name(&self) -> String {
SUBCOMM_NAME.to_string()
}

fn description(&self) -> String {
"Prints all load commands".to_string()
}

fn can_handle_with_name(&self, name: &str) -> bool {
SUBCOMM_NAME == name
}

fn handle_object(&self, object: ObjectType, other_args: Vec<String>) -> Result<()> {
let mut opts = Options::new();
self.accepted_option_items().add_to_opts(&mut opts);

let format = &Format::build(&mut opts, &other_args)?;
let filter = ObjectFilter::build(&mut opts, &other_args)?;

let objects = &filter.get_objects(object);
let out_arch = objects.len() > 1;
for (idx, obj) in objects.iter().enumerate() {
if out_arch {
common::out_single_arch_title(&self.printer, &obj.header(), idx, format.short);
}
self.handle_load_commands(obj.load_commands_iterator(), format);
}

Ok(())
}

fn accepted_option_items(&self) -> Vec<common::options::OptionItem> {
let mut result = default_option_items();
result.append(&mut Format::option_items());
result
}
}

impl LcHandler {
fn handle_load_commands(&self, commands: LoadCommandIterator, format: &Format) {
for (idx, cmd) in commands.enumerate() {
self.handle_command(cmd, idx, format);
}
}

fn handle_command(&self, cmd: LoadCommand, index: usize, format: &Format) {
if format.show_indices {
self.printer.out_list_item_dash(0, index);
}

if format.short {
self.printer.print_line(fmt_ext::load_command_to_string(cmd.cmd).yellow());
} else {
self.printer.out_field(
"cmd".bright_white(),
fmt_ext::load_command_to_string(cmd.cmd).yellow(),
" ",
);
self.printer.out_field(
"cmdsize".bright_white(),
cmd.cmdsize.to_string().yellow(),
"\n",
);

self.handle_command_variant(cmd.variant);
}
}

fn handle_command_variant(&self, variant: LcVariant) {
for field in variant.all_fields() {
self.printer.print_string(" ");
self.printer
.out_default_colored_field(&field.name, &field.value, "\n");
}
}
}
3 changes: 3 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod rpaths;
mod segs;
mod syms;
mod rel;
mod lc;

mod common;

Expand All @@ -27,6 +28,7 @@ use rpaths::*;
use segs::*;
use syms::*;
use rel::*;
use lc::*;

use std::process::exit;

Expand Down Expand Up @@ -159,5 +161,6 @@ fn available_handlers() -> Vec<Box<dyn Handler>> {
Box::new(ArchsHandler::new(printer.clone())),
Box::new(HeadersHandler::new(printer.clone())),
Box::new(RelHandler::new(printer.clone())),
Box::new(LcHandler::new(printer.clone())),
]
}

0 comments on commit bbcbf3f

Please sign in to comment.