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

Missing rust-gdb pretty printer for cdylib shared objects #96365

Open
cjermain opened this issue Apr 24, 2022 · 3 comments
Open

Missing rust-gdb pretty printer for cdylib shared objects #96365

cjermain opened this issue Apr 24, 2022 · 3 comments
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cjermain
Copy link

Problem

Debugging in rust-gdb with a shared object library does not activate pretty-printers supplied by rustup.

Running cargo build with crate-type of bin or lib results in a .debug_gdb_scripts section on the target or library, allowing rust-gdb to auto-load the Python pretty printers dynamically during debugging. The pretty printers are great!

let section_name = b".debug_gdb_scripts\0";
let section_contents = b"\x01gdb_load_rust_pretty_printers.py\0";

However, for crate-type of cdylib, the .debug_gdb_scripts section is not written. This results in a lack of pretty printing in rust-gdb, and makes it more challenging to use rust-gdb to best effect. I'm seeing this when working with Python bindings of Rust code with pyo3. Is there a reason cdylib did not receive this pretty-printer support?

Workaround

I've confirmed that manually adding the .debug_gdb_scripts section to a .so library generated with crate-type=cdylib activates the pretty printers. I did noticed some Python exceptions in the pretty printers, but have not investigated further.

$ objcopy --add-section .debug_gdb_scripts=<(echo -ne "\x01gdb_load_rust_pretty_printers.py\0") ./libmytest_lib.so
$ objdump -sj .debug_gdb_scripts libmytest_lib.so

libmytest_lib.so:     file format elf64-x86-64

Contents of section .debug_gdb_scripts:
 0000 01676462 5f6c6f61 645f7275 73745f70  .gdb_load_rust_p
 0010 72657474 795f7072 696e7465 72732e70  retty_printers.p
 0020 7900                                 y.      

Version information

rustc 1.60.0 (7737e0b 2022-04-04)
cargo 1.60.0 (d1fd9fe 2022-03-01)

@bjorn3 bjorn3 added A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) C-bug Category: This is a bug. labels Apr 24, 2022
@cjermain
Copy link
Author

@tromey, I noticed you have experience in this area. Any thoughts on this, or who might be the right person?

@Enselic
Copy link
Member

Enselic commented Feb 19, 2023

It seems to be the linker arg --gc-sections ("garbage collect sections") that removes the .debug_gdb_scripts section for cdylib. But for some reason the same arg does not remove the section for bins.

Do check this, first create these two files:

// lib.rs
pub extern "C" fn yep() -> bool {
    true
}

// main.rs
#![feature(start)]
#[start]
fn start(_: isize, _: *const *const u8) -> isize {
    return 0;
}

Then build a bin and a cdylib in a way that prints the linker args and saves the intermediate files so that the linker can be re-invoked. I am using nightly-2023-02-19.

$ rustc +nightly \
    -C save-temps \
    -C debuginfo=2 \
    -C codegen-units=1 \
    --print link-args \
    --out-dir /tmp/bin-out \
    --crate-type bin \
    main.rs
LC_ALL="C" PATH="/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/martin/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Users/ensel/.cargo/bin:/mnt/c/Users/ensel/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/ensel/AppData/Local/Programs/Microsoft VS Code/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcVhABaM/symbols.o" "/tmp/bin-out/main.main.bb53c12c-cgu.0.rcgu.o" "/tmp/bin-out/main.149e45n1nrd5pscg.rcgu.o" "-Wl,--as-needed" "-L" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-67e0fe4bfa018a5e.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-6e25273444177929.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-05da49d3cca73bff.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-616c9bd2710f0982.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-d9df84ec1a8a7a8f.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-0242ef3eea1e9db2.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-127f477a16f3f8f8.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-eb235cc34134320b.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-c5f20f2274212453.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-4483c8bc4648568f.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-94da6a76998341a3.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-22a9646e8f27a6e4.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-eb91273024ac0258.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-323da837c64ef472.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-6d46d38f739892fe.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-8212dcd77adfe144.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-522518611024dce5.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-05898138a596088a.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-b78d27aa9e5e005b.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/tmp/bin-out/main" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"

$ rustc +nightly \
    -C save-temps \
    -C debuginfo=2 \
    -C codegen-units=1 \
    --print link-args \
    --out-dir /tmp/lib-out \
    --crate-type cdylib \
    lib.rs
LC_ALL="C" PATH="/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/martin/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Users/ensel/.cargo/bin:/mnt/c/Users/ensel/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/ensel/AppData/Local/Programs/Microsoft VS Code/bin" VSLANG="1033" "cc" "-Wl,--version-script=/tmp/rustc0bhGIi/list" "-m64" "/tmp/rustc0bhGIi/symbols.o" "/tmp/lib-out/lib.lib.447e930b-cgu.0.rcgu.o" "/tmp/lib-out/lib.23es8ilvqvufl30n.rcgu.o" "-Wl,--as-needed" "-L" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-67e0fe4bfa018a5e.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-6e25273444177929.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-05da49d3cca73bff.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-616c9bd2710f0982.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-d9df84ec1a8a7a8f.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-0242ef3eea1e9db2.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-127f477a16f3f8f8.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-eb235cc34134320b.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-c5f20f2274212453.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-4483c8bc4648568f.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-94da6a76998341a3.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-22a9646e8f27a6e4.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-eb91273024ac0258.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-323da837c64ef472.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-6d46d38f739892fe.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-8212dcd77adfe144.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-522518611024dce5.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-05898138a596088a.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-b78d27aa9e5e005b.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/tmp/lib-out/liblib.so" "-Wl,--gc-sections" "-shared" "-Wl,-zrelro,-znow" "-nodefaultlibs"

We can see that the bin contains the section but the cdylib does not:

$ readelf -x .debug_gdb_scripts /tmp/bin-out/main /tmp/lib-out/liblib.so

File: /tmp/bin-out/main

Hex dump of section '.debug_gdb_scripts':
  0x00002000 01676462 5f6c6f61 645f7275 73745f70 .gdb_load_rust_p
  0x00002010 72657474 795f7072 696e7465 72732e70 retty_printers.p
  0x00002020 7900                                y.


File: /tmp/lib-out/liblib.so
readelf: Warning: Section '.debug_gdb_scripts' was not dumped because it does not exist

However, if we remove the "-Wl,--gc-sections" from both of the commands above and re-run them, both the bin and the cdylib does contain the section:

$ LC_ALL="C" PATH="/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/martin/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Users/ensel/.cargo/bin:/mnt/c/Users/ensel/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/ensel/AppData/Local/Programs/Microsoft VS Code/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcVhABaM/symbols.o" "/tmp/bin-out/main.main.bb53c12c-cgu.0.rcgu.o" "/tmp/bin-out/main.149e45n1nrd5pscg.rcgu.o" "-Wl,--as-needed" "-L" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-67e0fe4bfa018a5e.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-6e25273444177929.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-05da49d3cca73bff.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-616c9bd2710f0982.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-d9df84ec1a8a7a8f.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-0242ef3eea1e9db2.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-127f477a16f3f8f8.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-eb235cc34134320b.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-c5f20f2274212453.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-4483c8bc4648568f.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-94da6a76998341a3.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-22a9646e8f27a6e4.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-eb91273024ac0258.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-323da837c64ef472.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-6d46d38f739892fe.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-8212dcd77adfe144.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-522518611024dce5.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-05898138a596088a.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-b78d27aa9e5e005b.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/tmp/bin-out/main" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
$ LC_ALL="C" PATH="/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/martin/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Users/ensel/.cargo/bin:/mnt/c/Users/ensel/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/ensel/AppData/Local/Programs/Microsoft VS Code/bin" VSLANG="1033" "cc" "-Wl,--version-script=/tmp/rustc0bhGIi/list" "-m64" "/tmp/rustc0bhGIi/symbols.o" "/tmp/lib-out/lib.lib.447e930b-cgu.0.rcgu.o" "/tmp/lib-out/lib.23es8ilvqvufl30n.rcgu.o" "-Wl,--as-needed" "-L" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-67e0fe4bfa018a5e.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-6e25273444177929.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-05da49d3cca73bff.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-616c9bd2710f0982.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-d9df84ec1a8a7a8f.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-0242ef3eea1e9db2.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-127f477a16f3f8f8.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-eb235cc34134320b.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-c5f20f2274212453.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-4483c8bc4648568f.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-94da6a76998341a3.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-22a9646e8f27a6e4.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-eb91273024ac0258.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-323da837c64ef472.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-6d46d38f739892fe.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-8212dcd77adfe144.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-522518611024dce5.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-05898138a596088a.rlib" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-b78d27aa9e5e005b.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/tmp/lib-out/liblib.so" "-shared" "-Wl,-zrelro,-znow" "-nodefaultlibs"
$ readelf -x .debug_gdb_scripts /tmp/bin-out/main /tmp/lib-out/liblib.so

File: /tmp/bin-out/main

Hex dump of section '.debug_gdb_scripts':
  0x000f9e80 01676462 5f6c6f61 645f7275 73745f70 .gdb_load_rust_p
  0x000f9e90 72657474 795f7072 696e7465 72732e70 retty_printers.p
  0x000f9ea0 7900                                y.


File: /tmp/lib-out/liblib.so

Hex dump of section '.debug_gdb_scripts':
  0x000f9e70 01676462 5f6c6f61 645f7275 73745f70 .gdb_load_rust_p
  0x000f9e80 72657474 795f7072 696e7465 72732e70 retty_printers.p
  0x000f9e90 7900                                y.

In other words, the object files emitted by rustc contains .debug_gdb_scripts both in the bin and cdylib case, but it gets lost in the linking phase for cdylib, but not for bin.

Workaround

As a workaround it seems to work to pass -C link-args=-Wl,--no-gc-sections to rustc:

$ cargo rustc --lib
   Compiling issue-96365-repro v0.1.0 (/home/martin/src/issue-96365-repro)
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
$ readelf -x .debug_gdb_scripts ./target/debug/libissue_96365_repro.so
readelf: Warning: Section '.debug_gdb_scripts' was not dumped because it does not exist
$ cargo rustc --lib -- -C link-args=-Wl,--no-gc-sections
   Compiling issue-96365-repro v0.1.0 (/home/martin/src/issue-96365-repro)
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s
$ readelf -x .debug_gdb_scripts ./target/debug/libissue_96365_repro.so

Hex dump of section '.debug_gdb_scripts':
  0x000f2d68 01676462 5f6c6f61 645f7275 73745f70 .gdb_load_rust_p
  0x000f2d78 72657474 795f7072 696e7465 72732e70 retty_printers.p
  0x000f2d88 7900                                y.

but be aware of unintended consequences, especially for release builds.

@bjorn3
Copy link
Member

bjorn3 commented Feb 19, 2023

Adding .debug_gdb_scripts to exported_symbols with used: true when needs_gdb_debug_scripts_section() is true may be enough to fix this.

@Noratrieb Noratrieb added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Apr 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants