Skip to content

LLD linker does not have required nixpkgs wrapper script and flags #24744

Open

Description

@nh2

Currently using the lld linker in nix doesn't work correctly.

Executables that it produces have entries in lld being not found (rpaths are missing).

That's because ld and gold have wrappers that do the following:

# Second, for each directory in the library search path (-L...),

 # First, find all -L... switches.

    # Second, for each directory in the library search path (-L...),
    # see if it contains a dynamic library used by a -l... flag.  If
    # so, add the directory to the rpath.

But lld currently doesn't have such a wrapper.

(I found this via See also #24692 (comment).)

Also this type of patch or setting to --enable-new-dtags will likely be needed, and potentially other changes nixpkgs makes to ld and gold.

Activity

  1. added
    6.topic: haskell
    on Apr 27, 2017
  2. domenkozar commented on Apr 27, 2017

    @domenkozar
    Member

    This would be a nice addition to speed up Haskell builds by reducing time in linking phase.

  3. taktoa commented on Jun 6, 2017

    @taktoa
    Member

    As part of my work this summer on speeding up Nix Haskell builds for Awake Networks, I'm interested in tackling this issue (and upstreaming a fix). Does it just amount to adding something similar to the code snippet below to pkgs/build-support/cc-wrapper/default.nix (around line 180)?

          if [ -e ${binutils_bin}/bin/ld.gold ]; then
            wrap ld.gold ${./ld-wrapper.sh} ${binutils_bin}/bin/ld.gold
          fi
    

    or should I be modifying the lld package itself to use the ld-wrapper.sh script?
    Also, if someone could cook up an example Nix expression that should build iff I have correctly wrapped lld, that would be quite useful in implementing a fix for this.

  4. nh2 commented on Jun 6, 2017

    @nh2
    ContributorAuthor

    @taktoa I can't answer conclusively, but I suspect it won't be as trivial as using the exiting scripts.

    The right approach is likely to first do a proper reads through all things nixpkgs does to ld or gold, and then check which of those have to be done to lld as well.

    For example, I know that lld searches through some default paths. We probably want to patch them out, or pass flags so that it doesn't do that.

  5. domenkozar commented on Jun 6, 2017

    @domenkozar
    Member

    @taktoa we're interested in this project at https://github.com/input-output-hk as well - maybe we could have quick chat and join some forces :)

    we invested a bit of time into parallel GC and general GHC option tuning for scaling the compilation into multiple cores: https://github.com/input-output-hk/iohk-nixops/pull/49/files

    Indeed linking takes a hell lot of time, but buildTarget fix in Cabal master will help a lot, limiting what executables are built for the given package.

  6. domenkozar commented on Jun 6, 2017

    @domenkozar
    Member

    @taktoa for this particular change, I'd talk to @vcunat or @wkennington

  7. vcunat commented on Jun 6, 2017

    @vcunat
    Member

    Assuming the relevant linker options have the same syntax, I'd expect that the same wrapping approach should work. The way cc-wrapper is written now, I'd suggest to first try adding an option that makes it include ld.lld. (And maybe an option to make it the default ld, though I don't know if that's the standard way to exchange linkers.)

  8. matthewbauer commented on May 1, 2019

    @matthewbauer
    Member

    I have now added the lldClang stdenv. We could expose this better as needed if people want lld to be installable. Otherwise, without anything actionable I think this can be closed?

    for instance, you can run llvm linker:

    with import <nixpkgs> { system = "x86_64-linux"; };
    let stdenv' = overrideCC stdenv llvmPackages_8.lldClang;
    in stdenv'.mkDerivation {
      name = "test";
      buildCommand = ''
      cc ${./test.c} -o test
      ./test
      '';
    }
    

    Also install lld using:

    nix-env -iA nixpkgs.llvmPackages_8.lldClang.bintools
    
  9. nh2 commented on May 1, 2019

    @nh2
    ContributorAuthor

    Otherwise, without anything actionable I think this can be closed?

    @matthewbauer The actionable part from the issue description was:

    Executables that it produces have entries in lld being not found (rpaths are missing).
    But lld currently doesn't have such a wrapper [that fixes the rpaths].

    Concretely, we want to be able to:

    • Use a nix-provided lld in nix-build and nix-shell to link executables, also on non-NixOS platforms like Ubuntu
      • nix-shell support means it should work without sandbox, without lld picking up some global paths that it has hardcoded; it should be patched or wrapped to not do that, like binutils ld is
    • Use lld for general nix builds of specific or all packages (lddClang sounds like it does exactly that)

    If your lldClang env fixes all those points, then this can be closed; if it does not fix/wrap/patch lld so that the nix-shell-on-Ubuntu use case works, then we still have to do that.

  10. domenkozar commented on Aug 8, 2019

    @domenkozar
    Member

    Can't easily override stdenv in haskell #66267, but could be checked by overriding it in nixpkgs and see if darwin builds.

  11. added a commit that references this issue on Mar 3, 2020
  12. stale commented on Jun 2, 2020

    @stale

    Thank you for your contributions.

    This has been automatically marked as stale because it has had no activity for 180 days.

    If this is still important to you, we ask that you leave a comment below. Your comment can be as simple as "still important to me". This lets people see that at least one person still cares about this. Someone will have to do this at most twice a year if there is no other activity.

    Here are suggestions that might help resolve this more quickly:

    1. Search for maintainers and people that previously touched the related code and @ mention them in a comment.
    2. Ask on the NixOS Discourse.
    3. Ask on the #nixos channel on irc.freenode.net.
  13. added
    2.status: stale
    on Jun 2, 2020
  14. 12 remaining items

  15. matklad commented on Mar 29, 2022

    @matklad
    Contributor

    @happysalada https://matklad.github.io/2022/03/14/rpath-or-why-lld-doesnt-work-on-nixos.html includes some notes on debugging. Useful things:

    • Compare working and broken linkers
    • readelf -d target/debug/examples/my-binary | rg PATH to get the actual RPATH
    • cat (which ld.lld) to check if it's actually a script
    • check which C compiler is used as a linker. For me right now, cc is clang
  16. happysalada commented on Mar 30, 2022

    @happysalada
    Contributor

    for me adding lld_13 to the list of buildInputs did the trick. It makes lld work on darwin as well.

    Thanks again for the help!

  17. nixos-discourse commented on Apr 7, 2022

    @nixos-discourse
  18. nixos-discourse commented on Apr 7, 2022

    @nixos-discourse
  19. gabyx commented on Feb 5, 2024

    @gabyx
    Member

    I used the following to make lld work:

     # Define settings
      # Toolchain.
      gccVersion = "13";
      gccPkg = pkgs."gcc${gccVersion}";
    
      llvmVersion = "17";
      llvmPkgs = pkgs."llvmPackages_${llvmVersion}";
    
      clangStdEnv = pkgs.stdenvAdapters.overrideCC llvmPkgs.stdenv (
        llvmPkgs.clang.override {
          bintools = llvmPkgs.bintools;
          gccForLibs = gccPkg.cc; # This is the unwrapped gcc.
        }
      ); 
      
    
      clangStdEnv.mkDerivation( ... )
  20. szlend commented on May 4, 2024

    @szlend
    Member

    Here's a wrapped bintools that's cross-platform and includes lld (tested on linux and darwin):

    { clangStdenv, lld, path }:
    
    clangStdenv.cc.bintools.override {
      extraBuildCommands = ''
        for ld in $(find ${lld}/bin -name "ld*" -printf "%f\n"); do
          wrap ${clangStdenv.cc.bintools.targetPrefix}$ld \
               ${path + /pkgs/build-support/bintools-wrapper/ld-wrapper.sh} \
               ${lld}/bin/$ld
        done
      '';
    }
  21. domenkozar commented on May 10, 2024

    @domenkozar
    Member

    @szlend however on linux it shouldn't be using clangStdenv?

  22. szlend commented on May 10, 2024

    @szlend
    Member

    @szlend however on linux it shouldn't be using clangStdenv?

    Yeah. For my use case I wanted to use clang on all platforms.

  23. added
    6.topic: llvm/clang
    on May 27, 2024
  24. PeterWang-dev commented on Jun 9, 2025

    @PeterWang-dev

    This issue causes .so not found of the executable built by rust-nightly using dynamic library. As rust-nightly changed the default linker from ld to lld . nix-community/fenix#191 rust-lang/rust#124129

  25. DieracDelta commented on Jun 17, 2025

    @DieracDelta
    Member

    Any chance you have a sense of what the practical the implication of this is @PeterWang-dev ? Are newer versions of nightly rust using fenix just not usable because they won't link? Related: do you know if this also applies to oxalica's rust overlay?

    Note: I'm interested, so I intend to try both toolchains and see if linking works when I have a few free minutes. I will update this post with the results

  26. PeterWang-dev commented on Jun 18, 2025

    @PeterWang-dev

    Any chance you have a sense of what the practical the implication of this is @PeterWang-dev ? Are newer versions of nightly rust using fenix just not usable because they won't link? Related: do you know if this also applies to oxalica's rust overlay?

    Note: I'm interested, so I intend to try both toolchains and see if linking works when I have a few free minutes. I will update this post with the results

    I didn't try oxalica's rust overlay. The newest fenix just not usable because it cannot get correct library path of dynamic library. It can compile, but just cannot run without specifying LD_LIBRARY_PATH

  27. swagtop commented on Jun 18, 2025

    @swagtop
    Contributor

    Related: do you know if this also applies to oxalica's rust overlay?

    Can confirm that I've had this issue on oxalica's overlay. Using this rustflag solved it: -Zlinker-features=-lld.

    It seems I only need to use this flag when specifying a target, but it should be mentioned that on the host target I'm setting the dynamic library searchpath through rpath with rustflags, instead of using LD_LIBRARY_PATH.

  28. added
    2.status: stale
    on Dec 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    2.status: stale6.topic: haskell6.topic: llvm/clang

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions