Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
syejing opened this issue May 5, 2024 · 9 comments
Closed
Labels
A-linkage Area: linking into static, shared libraries and binaries O-ios Operating system: iOS

Comments

@syejing
Copy link

syejing commented May 5, 2024

Hi,

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

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label May 5, 2024
@workingjubilee
Copy link
Member

@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
Copy link
Author

syejing commented May 5, 2024

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
Copy link
Author

syejing commented May 5, 2024

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
Copy link
Member

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
Copy link
Author

syejing commented May 5, 2024

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

@workingjubilee
Copy link
Member

I believe this is what you want:

RUSTC_LOG=INFO rustc hello.rs 2>&1 | grep "link"

@syejing
Copy link
Author

syejing commented May 5, 2024

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

@syejing
Copy link
Author

syejing commented May 5, 2024

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
Copy link
Author

syejing commented May 5, 2024

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

@syejing syejing closed this as completed May 5, 2024
@saethlin saethlin added A-linkage Area: linking into static, shared libraries and binaries O-ios Operating system: iOS and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels May 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries O-ios Operating system: iOS
Projects
None yet
Development

No branches or pull requests

4 participants