How do I solve "rust-lld: error: relocation R_X86_64_32 out of range"?

I am getting a bunch of linker errors when compiling this workspace (commit 5ffcc38af569e33597949bea787dbb6b6409c5bc).

This is on linux. Note that compiling this takes quite a bit of RAM, 16GiB is not enough.
Simply running cargo build in the workspace results in the following:

Error reported by cargo
error: linking with `cc` failed: exit status: 1
  |
  = note:  "cc" "-m64" "/home/sebschmi/git/template-switch-aligner/target/debug/deps/rustckmYmQR/symbols.o" "<257 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/home/sebschmi/git/template-switch-aligner/target/debug/deps/{libtoml-da8511d0c473a6d4,libtoml_writer-e8cca88213b03a1d,libtoml_parser-b65a1a3f5d5b4389,libwinnow-986d8e13b44c7141,libserde_spanned-8a268e80abf52687,libtoml_datetime-081e4fd285a6af1b,libsha-342544120d01cef8,libbswap-e7afa733331b8356,liblib_tsshow-98b91bdac0b5fcc4,libstrong_type-17362976b96b6abc,libtagged_vec-57e5d35f36a12934,libsvg-65e4a8bb6824931a,libresvg-6d9b0316d4637bad,libimage_webp-7d3d399f2049aa0d,libquick_error-c31e49688f5262a2,libbyteorder_lite-8044bf4fc77023aa,libgif-0888d07649cef43c,libcolor_quant-200048bed7c0fbcf,libweezl-011182038a55cbb9,libzune_jpeg-33ae1bf43979ad20,libzune_core-570986b22f7712a4,librgb-8cb7a64d032e848e,libusvg-d36407ee4cf967de,libbase64-93d13499e98064b8,libunicode_bidi-d0c427ff0f4cbdbc,libunicode_vo-94c2c4dc996e4189,libimagesize-ab4a0490a967085b,libdata_url-0c417b8596bf4e72,libxmlwriter-955da1487bfcf662,librustybuzz-d97b77075167cfbc,libunicode_bidi_mirroring-970c2e6afe520038,libbitflags-7103a049596a7514,libunicode_script-c97bd9fd58c1fd33,libunicode_properties-088162fc05d8d83e,libunicode_ccc-13c2a6ae0b75bfdf,libsimplecss-5b509012de8852ed,libsvgtypes-efe850d47e02dcfb,libkurbo-98cec03759189387,libsmallvec-c85e941a8f6330c7,libsiphasher-8fe0fa32b8be4a01,libfontdb-63c5fdae9773bf30,libfontconfig_parser-3e20cb86d2f2c7c8,libroxmltree-f760c85f5d91c74c,libmemmap2-6b481c63ce8052b0,libtinyvec-449225b51f86bf6d,libtinyvec_macros-ae826fd0b2f5f044,libslotmap-87df94ff0c822a10,libttf_parser-b2e30cf9b1cd0b57,libtiny_skia-db1a96ef286e54a4,libpng-29ad6fec074af13c,libbitflags-c787aa160115669f,libfdeflate-17abf645182ac5b6,libtiny_skia_path-dfce405923d76e0d,libbytemuck-5ea905d1228866d5,libstrict_num-2f3dc62ac0562d1d,libfloat_cmp-676bde38ca212b11,libarrayref-6cea67aa60f47a68,libutf8_chars-16d27e220302364b,libarrayvec-6ebd1ad5e3ff9fa8,libsimplelog-cf4b2f80288b3fcc,libtime-c554d3eac6ad6e96,libnum_threads-9afa9b263db09155,libitoa-5b927177094538a1,libtime_core-4f6f339c6f118b83,libnum_conv-d91b53fde0b7c7e3,libderanged-f2f1aef5e23b7709,libpowerfmt-6a3024e09d07a3a9,libtermcolor-e25636badd086c0b,liblib_ts_chainalign-e24e361360afdb21,libbincode-fd6f80f45e26eba1,libunty-bb27e3a62c66f10c,libindicatif-a7ea652c4bad2493,libportable_atomic-32032d9119346fe1,libunit_prefix-688a168f93c6d359,libconsole-671ef2746e5a99b8,liblibc-f5786945c942fcc0,libunicode_width-a98708a0d0280085,liblib_tsalign-dfef4327edf7a77c,libnom-13bb431128f29afc,libmemchr-2d539f98a62b1a86,libtemplate_switch_error_free_inners-b4ccd5986537a3b0,libsuffix-3cd9daa036199142,libseed_chain-2ea6a58c87b7e7a3,libndarray-e906c1a5315de1f4,libmatrixmultiply-708a0b63627d65f3,libnum_complex-1b58946cca3f4aa6,libnum_integer-06ca47c08bc5cbe9,librawpointer-018099b4bc3fb67b,libnoisy_float-49f86658e64aa3d9,libgeneric_a_star-e67e05fccb68e2b9,libserde-2c4c300fe6bc369a,libserde_core-bdf0eb8a40e58e47,librustc_hash-50381adac9c6bd06,libnum_traits-effbbb10b32ca861,liblog-06b20b58f54a9048,libextend_map-305f9d95f5ec3d4f,libbinary_heap_plus-873bdd9efd1d75ea,libcompare-059b260c5e25eabf,libcompact_genome-6bb8560cb89a5f42,libenum_iterator-ab1fdd8f28a2090b,libitertools-03efde869fb0699d,libeither-58956e2ecf2f1ae1,libflate2-2c0ae92787e8042f,libminiz_oxide-986a9e9cad340626,libsimd_adler32-49feba163590c1ad,libcrc32fast-b2a67b3a037cfcf4,libcfg_if-595cd1fd9b5b1165,libthiserror-97173aa94ee1c771,libbitvec-a809f85c868c3929,libwyz-d62789bff53e7200,libtap-6a06a261a936a81e,libradium-6424756134ad0793,libfunty-0b5b5ad8874d6b6f,libtraitsequence-7b4383343e51a0f8,libref_cast-8f5d8c8901ad811c,libclap-01cdcd69ad6c1903,libclap_builder-1776ad515d3e37a0,libstrsim-a84462a1b39a8130,libanstream-93d5468b10ffcb66,libanstyle_query-9dd16a97c1ee81b6,libis_terminal_polyfill-a949bf5434c90de7,libcolorchoice-996538a6a0e7a78c,libanstyle_parse-ebad23be754493aa,libutf8parse-26766122121de462,libclap_lex-1036f15429b928a9,libanstyle-14904db143869bb2,libanyhow-81e0f801a5d79612}.rlib" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,libcfg_if-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/home/sebschmi/git/template-switch-aligner/target/debug/deps/rustckmYmQR/raw-dylibs" "-B<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/liblib_ts_chainalign-e24e361360afdb21.rlib(lib_ts_chainalign-e24e361360afdb21.3mw64yxdh63e0u4b9pqtns6hz.0906uv8.rcgu.o):(.debug_info+0xc): relocation R_X86_64_32 out of range: 6142626857 is not in [0, 4294967295]
          >>> referenced by 3mw64yxdh63e0u4b9pqtns6hz; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/libclap_builder-1776ad515d3e37a0.rlib(clap_builder-1776ad515d3e37a0.clap_builder.3da6961d31d0451b-cgu.09.rcgu.o):(.debug_info+0xc): relocation R_X86_64_32 out of range: 6142626857 is not in [0, 4294967295]
          >>> referenced by clap_builder.3da6961d31d0451b-cgu.09; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/libkurbo-98cec03759189387.rlib(kurbo-98cec03759189387.kurbo.65462c28a022e624-cgu.07.rcgu.o):(.debug_info+0xc): relocation R_X86_64_32 out of range: 6142626857 is not in [0, 4294967295]
          >>> referenced by kurbo.65462c28a022e624-cgu.07; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.euhi9i2vwaxrtqx4433186uqq.144neor.rcgu.o:(.debug_info+0xc): relocation R_X86_64_32 out of range: 6142626857 is not in [0, 4294967295]
          >>> referenced by euhi9i2vwaxrtqx4433186uqq; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/libusvg-d36407ee4cf967de.rlib(usvg-d36407ee4cf967de.usvg.a73d0f379e1fd698-cgu.03.rcgu.o):(.debug_info+0xc): relocation R_X86_64_32 out of range: 6142626857 is not in [0, 4294967295]
          >>> referenced by usvg.a73d0f379e1fd698-cgu.03; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/liblib_ts_chainalign-e24e361360afdb21.rlib(lib_ts_chainalign-e24e361360afdb21.3mw64yxdh63e0u4b9pqtns6hz.0906uv8.rcgu.o):(.debug_info+0x35): relocation R_X86_64_32 out of range: 5549797056 is not in [0, 4294967295]
          >>> referenced by 3mw64yxdh63e0u4b9pqtns6hz; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/libkurbo-98cec03759189387.rlib(kurbo-98cec03759189387.kurbo.65462c28a022e624-cgu.07.rcgu.o):(.debug_info+0x42): relocation R_X86_64_32 out of range: 4954397249 is not in [0, 4294967295]
          >>> referenced by kurbo.65462c28a022e624-cgu.07; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.euhi9i2vwaxrtqx4433186uqq.144neor.rcgu.o:(.debug_info+0x54): relocation R_X86_64_32 out of range: 5745787684 is not in [0, 4294967295]
          >>> referenced by euhi9i2vwaxrtqx4433186uqq; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/libclap_builder-1776ad515d3e37a0.rlib(clap_builder-1776ad515d3e37a0.clap_builder.3da6961d31d0451b-cgu.09.rcgu.o):(.debug_info+0x54): relocation R_X86_64_32 out of range: 5745787684 is not in [0, 4294967295]
          >>> referenced by clap_builder.3da6961d31d0451b-cgu.09; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.cuzgghv0rwhz30b88ywkeukmd.144neor.rcgu.o:(.debug_info+0xc): relocation R_X86_64_32 out of range: 6142626857 is not in [0, 4294967295]
          >>> referenced by cuzgghv0rwhz30b88ywkeukmd; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.c7xntx5zhkk751195wisqucjt.144neor.rcgu.o:(.debug_info+0xc): relocation R_X86_64_32 out of range: 6142626857 is not in [0, 4294967295]
          >>> referenced by c7xntx5zhkk751195wisqucjt; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/libusvg-d36407ee4cf967de.rlib(usvg-d36407ee4cf967de.usvg.a73d0f379e1fd698-cgu.03.rcgu.o):(.debug_info+0x54): relocation R_X86_64_32 out of range: 5745787684 is not in [0, 4294967295]
          >>> referenced by usvg.a73d0f379e1fd698-cgu.03; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.dv2f714x0lymets9mo7alm798.144neor.rcgu.o:(.debug_info+0xc): relocation R_X86_64_32 out of range: 6142626857 is not in [0, 4294967295]
          >>> referenced by dv2f714x0lymets9mo7alm798; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/liblib_ts_chainalign-e24e361360afdb21.rlib(lib_ts_chainalign-e24e361360afdb21.3mw64yxdh63e0u4b9pqtns6hz.0906uv8.rcgu.o):(.debug_info+0x4d): relocation R_X86_64_32 out of range: 6341025946 is not in [0, 4294967295]
          >>> referenced by 3mw64yxdh63e0u4b9pqtns6hz; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/libkurbo-98cec03759189387.rlib(kurbo-98cec03759189387.kurbo.65462c28a022e624-cgu.07.rcgu.o):(.debug_info+0x54): relocation R_X86_64_32 out of range: 5745787684 is not in [0, 4294967295]
          >>> referenced by kurbo.65462c28a022e624-cgu.07; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.euhi9i2vwaxrtqx4433186uqq.144neor.rcgu.o:(.debug_info+0x5f): relocation R_X86_64_32 out of range: 5743192942 is not in [0, 4294967295]
          >>> referenced by euhi9i2vwaxrtqx4433186uqq; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.c7xntx5zhkk751195wisqucjt.144neor.rcgu.o:(.debug_info+0x2b): relocation R_X86_64_32 out of range: 5590852511 is not in [0, 4294967295]
          >>> referenced by c7xntx5zhkk751195wisqucjt; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.cuzgghv0rwhz30b88ywkeukmd.144neor.rcgu.o:(.debug_info+0x50): relocation R_X86_64_32 out of range: 4551818001 is not in [0, 4294967295]
          >>> referenced by cuzgghv0rwhz30b88ywkeukmd; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.dv2f714x0lymets9mo7alm798.144neor.rcgu.o:(.debug_info+0x56): relocation R_X86_64_32 out of range: 4349986755 is not in [0, 4294967295]
          >>> referenced by dv2f714x0lymets9mo7alm798; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: /home/sebschmi/git/template-switch-aligner/target/debug/deps/tsalign-3a0d261c4ad623ed.euhi9i2vwaxrtqx4433186uqq.144neor.rcgu.o:(.debug_info+0x95): relocation R_X86_64_32 out of range: 5025561396 is not in [0, 4294967295]
          >>> referenced by euhi9i2vwaxrtqx4433186uqq; consider recompiling with -fdebug-types-section to reduce size of debug sections
          
          rust-lld: error: too many errors emitted, stopping now (use --error-limit=0 to see all errors)
          collect2: error: ld returned 1 exit status
          

error: could not compile `tsalign` (bin "tsalign") due to 1 previous error

I would like to keep using debug info, so changing the debug profile to exclude debug info is not an option.

What is the effect of using -fdebug-types-section? And how do I set this variable in my workspace's Cargo.toml?

-fdebug-types-section is a GCC/Clang flag that according to Debugging Options (Using the GNU Compiler Collection (GCC)) puts type debuginfo into a separate section per type. This allows it to be deduplicated, which may potentially shrink the total debuginfo size enough to allow linking to succeed. You could try if -Cllvm-args=-fdebug-types-section works, but some LLVM features can only be set explicitly by the frontend (rustc in this case). -Cllvm-args also doesn't have any stability guarantees. Using -Csymbol-mangling-version=v0 may also help. cc Large types cause linker failure · Issue #130729 · rust-lang/rust · GitHub

Do you need all the debug info? debug=true (full) includes details of variable usage which is used only when stepping in a debugger. If you only need backtraces and function arguments, debug=1 is much smaller and a bit faster to compile.

In Cargo you can also use package overrides to reduce debug info for some or all dependencies. This way you could have precise debug for the code you wrote, without bloat from potentially hundreds of deps you may not need to look at.

This usually happens because your build artifacts or debug symbols are overflowing the 32-bit address space limits of the x86_64 small code model. a
Before trying to switch code models, you can usually fix this by just stripping or thinning out the debug symbols in your Cargo.toml. Try adding this to your profile:

[profile.dev]
debug = 1 # or "line-tables-only" instead of full debug info
split-debuginfo = "unpacked" # Keeps symbols out of the main object file

If it's happening during a release build or you actually just have a massive static binary, you might need to force the large code model via .cargo/config.toml:

[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "code-model=large"]

Thanks for all the responses! I did the following now, all separately:

  • Adding a file .cargo/config.toml containing:
    [target.x86_64-unknown-linux-gnu]
    rustflags = ["-Cllvm-args=\"-fdebug-types-section\""]
    # rustflags = ["-Cllvm-args=-fdebug-types-section"] Also doesn't work
    # rustflags = ["-C", "llvm-args=-fdebug-types-section"] Also doesn't work
    
    All variants result in the following error:
    error: failed to run `rustc` to learn about target-specific information
    
    Caused by:
      process didn't exit successfully: `/home/sebschmi/.rustup/toolchains/1.95.0-x86_64-unknown-linux-gnu/bin/rustc - --crate-name ___ --print=file-names '-Cllvm-args="-fdebug-types-section"' --crate-type bin --crate-type rlib --crate-type dylib --crate-type cdylib --crate-type staticlib --crate-type proc-macro --print=sysroot --print=split-debuginfo --print=crate-name --print=cfg -Wwarnings` (exit status: 1)
      --- stderr
      rustc -Cllvm-args="..." with: Unknown command line argument '"-fdebug-types-section"'.  Try: 'rustc -Cllvm-args="..." with --help'
    
    So I assume I cannot just pass this flag through cargo.
  • Adding a file .cargo/config.toml containing:
    [target.x86_64-unknown-linux-gnu]
    rustflags = ["-C", "code-model=large"]
    
    Then I get the same linker errors as before.
  • Adding the following lines to the workspace toplevel Cargo.toml
    [profile.dev]
    debug = 1
    
    Then I get the same linker errors as before.
  • Adding the following lines to the workspace toplevel Cargo.toml
    [profile.dev]
    debug = false
    
    Then everything compiles, but I don't have debug info :frowning:
  • Adding the following lines to the workspace toplevel Cargo.toml
    [profile.dev]
    debug = 1
    split-debuginfo = "unpacked"
    
    Then everything compiles, and while I didn't check, I trust that I get nice stack traces now in debug mode.

My profile.dev binary size without debug info is about 350MiB btw, not sure if that counts as "large". Release with debug=1 and without splitting is 2.6GiB.

Well, the solution with splitting the debug info seems perfect for me right now. I am still confused as to why the large code model did not work.

I don't think the code model affects debuginfo.

Ah I understand. I thought it was somehow related to how much debug info there can be.