简体   繁体   English

如何找到 rustc --target=$TARGET 将链接到哪个 libc.so?

[英]How to find which libc.so will `rustc --target=$TARGET` link against?

I want to find the libc.so file that's being used in a Rust build so that I can query it with --version .我想找到在 Rust 构建中使用的 libc.so 文件,以便我可以使用--version查询它。 (Some libcs expose their version information via C macros, so an alternative for them would be to use the cc crate in a build script. But others like musl don't.) (一些 libcs 通过 C 宏公开他们的版本信息,因此他们的替代方法是在构建脚本中使用cc crate。但像 musl 这样的其他人不会。)

I can figure out which libstd-*.so file a rust binary or library will be linked against.可以弄清楚 rust 二进制文件或库将链接到哪个libstd-*.so文件。 When this libstd.so is linked against the host 's libc, then running ldd on it shows that libc.so .当此libstd.so主机的 libc 链接时,在其上运行ldd会显示libc.so But when the host system is using glibc and the targeted environment is musl, this doesn't work ("Invalid ELF header").但是当主机系统使用 glibc 并且目标环境是 musl 时,这不起作用(“无效的 ELF 标头”)。 Instead of ldd , I could instead use readelf -d or objdump -p on the libstd.so .而不是ldd ,我可以在libstd.so上使用readelf -dobjdump -p But these only show the file name of the libc.so file it uses, not its full path.但这些只显示它使用的libc.so文件的文件,而不是它的完整路径。 And that libc.so isn't at any of the directories in LD_LIBRARY_PATH .而且那个libc.so不在LD_LIBRARY_PATH的任何目录中。 (I do know where it is on my own systems, but I'm trying to find it programmatically on arbitrary systems.) (我确实知道它在我自己的系统上的位置,但我正试图以编程方式在任意系统上找到它。)

Running ldconfig -p only gives me information about the libc for the host system.运行ldconfig -p只会给我有关主机系统的 libc 的信息。

It would be great if there were a rustc equivalent of gcc's and clang's -print-file-name=libc.so , so that I could do something like rustc --target=$TARGET --print-file-name=libc.so .如果有一个 rustc 相当于 gcc 和 clang 的-print-file-name=libc.so ,这样我就可以做类似rustc --target=$TARGET --print-file-name=libc.so .

Other ideas about how I could get this information?关于如何获取此信息的其他想法?

You can pass linker arguments to rustc like so:您可以像这样将 linker arguments 传递rustc

rustc -C link-args=...

To find out which libc.so is used, I believe the following command should suffice:要找出使用了哪个libc.so ,我相信以下命令就足够了:

rustc -C link-args=-Wl,-t ...

From man ld :man ld

  -t
  --trace
       Print the names of the input files as ld processes them. ...

Update :更新

This didn't work: rustc "eats up" the output from the linker.这不起作用: rustc “吃掉”了 linker 中的 output。

I was able to get the desired output indirectly:我能够间接获得所需的 output :

echo 'fn main() { println!("")}' | rustc -C link-args=-Wl,-Map=map.out -o foo -
grep 'libc\.so' map.out
libc.so.6                     /usr/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-f25e49a311b0f577.rlib(std-f25e49a311b0f577.std.cy8lhng1-cgu.2.rcgu.o) (setuid@@GLIBC_2.2.5)
LOAD /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/libc.so
LOAD /lib/x86_64-linux-gnu/libc.so.6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM