rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
93.58k stars 12.05k forks source link

I wonder what rustc's command to call the linker looks like when it ends up #124734

Closed syejing closed 1 week ago

syejing commented 2 weeks ago

Hi,

clang can see the linker call command through -v, does rustc have a similar function, not --print link-args?

workingjubilee commented 2 weeks ago

@syejing We actually, for some target platforms, invoke a C compiler as our linker driver.

Only for Windows and bare metal targets do we typically use the linker directly.

syejing commented 2 weeks ago

OK, my operating system is mac os, in fact I tracked and found that I did use cc, my original intention was to see how to use rustc to convert .rs files to .ll llvm ir, use llc to convert .ll to .s assembly, and then use as to convert .s to .o machine code, and now stuck in how to use linker to turn .o into executable files. `use std::io;

fn main() { let mut a = String::new(); // 声明两个字符串变量 let mut b = String::new(); let s: i32; // 声明一个整型变量

io::stdin().read_line(&mut a).expect("Failed to read line"); // 输入数据,放入到变量a中
io::stdin().read_line(&mut b).expect("Failed to read line"); // 输入数据,放入到变量b中

let a: i32 = a.trim().parse().expect("Please type a number!"); // 将字符串a转换为整数
let b: i32 = b.trim().parse().expect("Please type a number!"); // 将字符串b转换为整数

s = a + b;                         // 求和操作
println!("The sum is {}.", s);     // 输出到屏幕中

} `

syejing commented 1 week ago

env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET LC_ALL="C" PATH="/Users/admin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/bin:/usr/local/opt/llvm/bin:/Users/admin/.nvm/versions/node/v16.17.0/bin:/usr/local/anaconda3/bin:/Users/admin/Library/Java/JavaVirtualMachines/corretto-1.8.0_332/Contents/Home/bin:/usr/local/Cellar/node@12/12.18.3/bin:/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/admin/.cargo/bin:.:/Users/admin/go_dev/go/bin" VSLANG="1033" ZERO_AR_DATE="1" cc main.o -arch x86_64 -m64 -L /Users/admin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib /Users/ ... -lSystem -lc -lm -L /Users/admin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib -o main -Wl,-dead_strip -nodefaultlibs and I've tried cc, but it got an error Undefined symbols for architecture x86_64: "___rust_alloc", referenced from: core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::ha4c1a9c0063f99d9 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::hb15c9b25770b9417 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) core::result::Result$LT$T$C$E$GT$::map::h907e242bb5dd5719 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) alloc::raw_vec::finish_grow::hafe90ce51047470b in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) gimli::read::line::FileEntryFormat::parse::h0b980bcfd85b723e in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) _$LT$gimli..read..line..LineProgramHeader$LT$R$C$Offset$GT$$u20$as$u20$core..clone..Clone$GT$::clone::h9f8a101362c4c6e6 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) ... addr2line::ResUnit$LT$R$GT$::find_function_or_location::_$u7b$$u7b$closure$u7d$$u7d$::he77e85d1c7940c3d in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) addr2line::Lines::parse::h634e3216ac780800 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) addr2line::Lines::parse::h634e3216ac780800 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) addr2line::Lines::parse::h634e3216ac780800 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) ... ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

workingjubilee commented 1 week ago

OK, my operating system is mac os, in fact I tracked and found that I did use cc, my original intention was to see how to use rustc to convert .rs files to .ll llvm ir, use llc to convert .ll to .s assembly, and then use as to convert .s to .o machine code, and now stuck in how to use linker to turn .o into executable files.

Eh?

Last I checked, we don't actually do those things. We convert the code to the data structure forms directly used by LLVM, in memory, mostly by invoking LLVM. Then we invoke LLVM on the data structures to finish compilation. We don't write .ll to disk if we can avoid doing so, and we definitely don't call llc on it, and LLVM doesn't invoke as to assemble the files either. If you ask for us to --emit llvm-ir or --emit asm we do that on a best-effort basis, and may have subtle or even major differences from what we actually do. It's informational/for debugging, in other words.

syejing commented 1 week ago

Hmm, I can understand the true meaning, but I want to ask how to call the linker and what is command?

workingjubilee commented 1 week ago

I believe this is what you want:

RUSTC_LOG=INFO rustc hello.rs 2>&1 | grep "link"
syejing commented 1 week ago

这输出的结果和--print link-args一样 rustc src/main.rs -v --print link-args

syejing commented 1 week ago

env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET LC_ALL="C" PATH="..." VSLANG="1033" ZERO_AR_DATE="1" cc -arch x86_64 -m64 ".../symbols.o" ".../main.main.44961560ebe940ad-cgu.0.rcgu.o" "-L" "..." ".../libstd-2ad7f97be9e16ca5.rlib" "..." -lSystem -lc -lm "-L" "..." "-o" "main" "-Wl,-dead_strip" "-nodefaultlibs" According to the first principle, I can use cc as the linker to link, but it seems to report an error, which really tortures me.

syejing commented 1 week ago

Adding -C save-temps solved the problem. Symbols.o, main.main.o, main.0 are saved, and cc is enough. Thank you.