pacak / cargo-show-asm

cargo subcommand showing the assembly, LLVM-IR and MIR generated for Rust code
Apache License 2.0
679 stars 33 forks source link

Support merged functions #310

Open zheland opened 14 hours ago

zheland commented 14 hours ago

Currently cargo-show-asm doesn't detect and can't find merged functions (functions with the same implementation).

I think it is expected to be possible to explore such functions without having to parse --everything manually.

Example

Same for both: #[no_mangle] pub fn extern "C" ..., or #[inline(never)] pub fn .... Checked and reproducible for targets: x86_64-unknown-linux-gnu, i686-unknown-linux-gnu, x86_64-pc-windows-gnu, i686-pc-windows-gnu

#[no_mangle]
pub extern "C" fn two() -> u32 {
    2
}

#[no_mangle]
pub extern "C" fn one() -> u32 {
    1
}

#[no_mangle]
pub extern "C" fn one_plus_one() -> u32 {
    1 + 1
}

#[no_mangle]
pub extern "C" fn two_minus_one() -> u32 {
    2 - 1
}
$ cargo asm --lib
Try one of those by name or a sequence number
0 "one" [8]
1 "one_plus_one" [7]

$ cargo asm --lib two
Can't find any items matching "two"

$ cargo asm --target=x86_64-unknown-linux-gnu --lib --everything > asm-x86_64-unknown-linux-gnu.s
$ cargo asm --target=i686-unknown-linux-gnu --lib --everything > asm-i686-unknown-linux-gnu.s
$ cargo asm --target=x86_64-pc-windows-gnu --lib --everything > asm-x86_64-pc-windows-gnu.s
$ cargo asm --target=i686-pc-windows-gnu --lib --everything > asm-i686-pc-windows-gnu.s

$ cat asm-x86_64-unknown-linux-gnu.s | grep -B 1 -A 1 -n two
24- .cfi_endproc
25-
26: .globl  two
27: .type   two,@function
28:.set  two, one_plus_one
29: .globl  two_minus_one
30: .type   two_minus_one,@function
31:.set  two_minus_one, one
32-.section .debug_abbrev,"",@progbits
33- .byte   1

$ cat asm-i686-unknown-linux-gnu.s | grep -B 1 -A 1 -n two
24- .cfi_endproc
25-
26: .globl  two
27: .type   two,@function
28:.set  two, one_plus_one
29: .globl  two_minus_one
30: .type   two_minus_one,@function
31:.set  two_minus_one, one
32-.section .debug_abbrev,"",@progbits
33- .byte   1

$ cat asm-x86_64-pc-windows-gnu.s | grep -B 2 -A 2 -n two
28- ret
29-
30: .globl  two
31: .def    two;
32- .scl    2;
33- .type   32;
34- .endef
35:.set  two, one_plus_one
36: .globl  two_minus_one
37: .def    two_minus_one;
38- .scl    2;
39- .type   32;
40- .endef
41:.set  two_minus_one, one
42-.section .debug_abbrev,"dr"
43- .byte   1

$ cat asm-i686-pc-windows-gnu.s | grep -B 2 -A 2 -n two
48- .cfi_endproc
49-
50: .globl  _two
51: .def    _two;
52- .scl    2;
53- .type   32;
54- .endef
55:.set  _two, _one_plus_one
56: .globl  _two_minus_one
57: .def    _two_minus_one;
58- .scl    2;
59- .type   32;
60- .endef
61:.set  _two_minus_one, _one
62-.section .debug_abbrev,"dr"
63-Lsection_abbrev:

Meta

$ rustc --version --verbose
rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

$ cargo asm -V
Version: 0.2.39
pacak commented 13 hours ago

It might be able to list them with --disasm as is, but yeah, it makes sense to support them. Will look into it.