Skip to content

Commit

Permalink
Merge pull request #325 from pacak/rc-0.2.41
Browse files Browse the repository at this point in the history
Release 0.2.41
  • Loading branch information
pacak authored Oct 17, 2024
2 parents 3db1348 + 39e4482 commit 8f5940e
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 43 deletions.
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 = "cargo-show-asm"
version = "0.2.40"
version = "0.2.41"
edition = "2021"
description = "A cargo subcommand that displays the generated assembly of Rust source code."
categories = ["development-tools::cargo-plugins", "development-tools::debugging"]
Expand Down
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Log

## [0.2.41] - Unreleased
## [0.2.41] - 2024-10-13
- make sure not to drop used labels (#318)
- add release-lto profile for slightly smaller/faster version
thanks @zamazan4ik for the suggestion
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Show the code rustc generates for any function

required for workspace projects, can also point to a dependency
- **` --file`**=_`PATH`_ —
Disassemble this file instead of calling cargo,
Disassemble or process this file instead of calling cargo,
requires cargo-show-asm to be compiled with disasm feature

You can specify executable, rlib or an object file
Expand Down
60 changes: 26 additions & 34 deletions src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,58 +206,50 @@ fn handle_non_mangled_labels(
if is_windows || is_mac {
// Search for .globl between sec_start and ix
for line in &lines[sec_start..ix] {
if let Statement::Directive(Directive::Generic(GenericDirective(g))) = line {
if let Statement::Directive(Directive::Global(g)) = line {
// last bool is responsible for stripping leading underscore.
// Stripping is not needed on Linux and 64-bit Windows.
// Currently we want to strip underscore on MacOS
// TODO: on 32-bit Windows we ought to remove underscores
if let Some(item) = get_item_in_section("globl\t", ix, label, g, is_mac) {
if let Some(item) = get_item_in_section(ix, label, g, is_mac) {
return Some(item);
}
}
}
None
} else {
// Linux symbols each have their own section, named with this prefix.
get_item_in_section(".text.", ix, label, ss, false)
get_item_in_section(ix, label, ss.strip_prefix(".text.")?, false)
}
}
Some(Statement::Directive(Directive::Generic(GenericDirective(g)))) => {
// macOS symbols after the first are matched here.
get_item_in_section("globl\t", ix, label, g, true)
}
// Some(Statement::Directive(Directive::Generic(GenericDirective(g)))) => {
// macOS symbols after the first are matched here.
// get_item_in_section(PrefixKind::Global, ix, label, g, true)
// }
Some(Statement::Directive(Directive::Global(g))) => get_item_in_section(ix, label, g, true),
_ => None,
}
}

/// Checks if the provided section `ss` starts with the provided `prefix`.
/// If it does, it further checks if the section starts with the `label`.
/// If both conditions are satisfied, it creates a new [`Item`], but sets `item.index` to 0.
fn get_item_in_section(
prefix: &str,
ix: usize,
label: &Label,
ss: &str,
strip_underscore: bool,
) -> Option<Item> {
if let Some(ss) = ss.strip_prefix(prefix) {
if ss.starts_with(label.id) {
let name = if strip_underscore && label.id.starts_with('_') {
String::from(&label.id[1..])
} else {
String::from(label.id)
};
return Some(Item {
mangled_name: label.id.to_owned(),
name: name.clone(),
hashed: name,
index: 0, // Written later in find_items
len: ix,
non_blank_len: 0,
});
}
/// Checks if the place (ss) starts with the `label`. Place can be either section or .global
/// Creates a new [`Item`], but sets `item.index` to 0.
fn get_item_in_section(ix: usize, label: &Label, ss: &str, strip_underscore: bool) -> Option<Item> {
if !ss.starts_with(label.id) {
return None;
}
None
let name = if strip_underscore && label.id.starts_with('_') {
String::from(&label.id[1..])
} else {
String::from(label.id)
};
Some(Item {
mangled_name: label.id.to_owned(),
name: name.clone(),
hashed: name,
index: 0, // Written later in find_items
len: ix,
non_blank_len: 0,
})
}

fn used_labels<'a>(stmts: &'_ [Statement<'a>]) -> BTreeSet<&'a str> {
Expand Down
3 changes: 3 additions & 0 deletions src/asm/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,15 +847,18 @@ fn good_for_label(c: char) -> bool {
c == '.' || c == '$' || c == '_' || c.is_ascii_alphanumeric()
}
impl Statement<'_> {
/// Is this a label that starts with ".Lfunc_end"?
pub(crate) fn is_end_of_fn(&self) -> bool {
let check_id = |id: &str| id.strip_prefix('.').unwrap_or(id).starts_with("Lfunc_end");
matches!(self, Statement::Label(Label { id, .. }) if check_id(id))
}

/// Is this a .section directive?
pub(crate) fn is_section_start(&self) -> bool {
matches!(self, Statement::Directive(Directive::SectionStart(_)))
}

/// Is this a .global directive?
pub(crate) fn is_global(&self) -> bool {
matches!(self, Statement::Directive(Directive::Global(_)))
}
Expand Down
26 changes: 22 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,32 @@ fn main() -> anyhow::Result<()> {

let cargo = match opts.code_source {
CodeSource::FromCargo { ref cargo } => cargo,
#[cfg(not(feature = "disasm"))]
CodeSource::File { .. } => no_disasm!(),
#[cfg(feature = "disasm")]
CodeSource::File { ref file } => {
if opts.format.verbosity > 0 {
esafeprintln!("Processing a given single file");
}
return dump_disasm(opts.to_dump, file, &opts.format, opts.syntax.output_style);
match file.extension() {
Some(ext) if ext == "s" => {
let nope = PathBuf::new();
let asm = Asm::new(&nope, &nope);
let mut format = opts.format;
// For standalone file we don't know the matching
// system root so don't even try to dump it
format.rust = false;
dump_function(&asm, opts.to_dump, file, &format)?;
}
_ => {
#[cfg(feature = "disasm")]
{
dump_disasm(opts.to_dump, file, &opts.format, opts.syntax.output_style)?
}
#[cfg(not(feature = "disasm"))]
{
no_disasm!()
}
}
}
return Ok(());
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub enum CodeSource {
cargo: Cargo,
},
File {
/// Disassemble this file instead of calling cargo,
/// Disassemble or process this file instead of calling cargo,
/// requires cargo-show-asm to be compiled with disasm feature
///
/// You can specify executable, rlib or an object file
Expand Down

0 comments on commit 8f5940e

Please sign in to comment.