【发布时间】:2017-01-06 07:03:29
【问题描述】:
虽然我看过有关使用 rustc 直接输出程序集的文档,但必须手动提取 Cargo 使用的命令并对其进行编辑以编写程序集是很乏味的。
有没有办法运行 Cargo 来写出汇编文件?
【问题讨论】:
标签: assembly rust rust-cargo
虽然我看过有关使用 rustc 直接输出程序集的文档,但必须手动提取 Cargo 使用的命令并对其进行编辑以编写程序集是很乏味的。
有没有办法运行 Cargo 来写出汇编文件?
【问题讨论】:
标签: assembly rust rust-cargo
您可以使用 Cargo 的 cargo rustc 命令直接向 rustc 发送参数:
cargo rustc -- --emit asm
ls target/debug/deps/<crate_name>-<hash>.s
为了优化组装:
cargo rustc --release -- --emit asm
ls target/release/deps/<crate_name>-<hash>.s
如果您看到多个<crate_name>-<hash>-<hash>.rcgu.s 文件而不是<crate_name>-<hash>.s 文件,请通过设置环境变量CARGO_INCREMENTAL=0 禁用增量编译。
【讨论】:
cargo rustc 提供 ARM 目标,例如cargo rustc --target aarch64-apple-ios --release -- --emit asm。程序集将位于target/aarch64-apple-ios/release/deps/*.s。
cargo rustc -- --emit asm -C "llvm-args=-x86-asm-syntax=intel"获得intel语法
--emit asm 参数会使编译时间增加四倍,结果可执行二进制文件大 40%。为什么发射程序集需要这么多时间,为什么输出的二进制文件也会改变?
除了kennytm的回答,还可以使用RUSTFLAGS环境变量,使用标准的cargo命令:
RUSTFLAGS="--emit asm" cargo build
cat target/debug/deps/project_name-hash.s
或处于发布模式(有优化):
RUSTFLAGS="--emit asm" cargo build --release
cat target/release/deps/project_name-hash.s
您可以将不同的值传递给--emit 参数,包括(但不限于):
mir(Rust 中间表示)llvm-ir(LLVM 中间表示)llvm-bc(LLVM 字节码)asm(组装)【讨论】:
现有的两个答案(使用cargo rustc 和RUSTFLAGS)都是使用标准工具获得组装的最佳方式。如果您发现自己经常尝试查看程序集,您可能需要考虑使用the cargo asm subcommand。使用cargo install cargo-asm 安装后,您可以像这样打印程序集:
cargo build --release
cargo asm my_crate::my_function
不过有几点需要注意:
cargo asm,它就会列出您可以检查的所有符号。cargo build --release,然后再尝试查看程序集,因为cargo asm(显然)只查看已经存在的构建工件【讨论】:
asm
cargo install cargo-asm 安装后"