Skip to content

Linux kernel cannot be linked with the wrapped lld #242244

Closed

Description

@SaltyKitkat

Describe the bug

Since upstream kernel already support built with llvm toolchains(clang, lld, ...), I also expect this will work on NixOS. However, It seems that link the kernel with wrapped lld would fail:

ld.lld: error: section .bsdata file range overlaps with .header
>>> .bsdata range is [0x1092, 0x122B]
>>> .header range is [0x11EF, 0x126B]

ld.lld: error: section .bsdata virtual address range overlaps with .header
>>> .bsdata range is [0x92, 0x22B]
>>> .header range is [0x1EF, 0x26B]

ld.lld: error: section .bsdata load address range overlaps with .header
>>> .bsdata range is [0x92, 0x22B]
>>> .header range is [0x1EF, 0x26B]
make[1]: *** [/nix/store/rrs90d2s51c5wmpj613l88ghvm1mf478-linux-6.4.2-dev/lib/modules/6.4.2/source/arch/x86/boot/Makefile:109: arch/x86/boot/setup.elf] Error 1
make: *** [/nix/store/rrs90d2s51c5wmpj613l88ghvm1mf478-linux-6.4.2-dev/lib/modules/6.4.2/source/arch/x86/Makefile:283: bzImage] Error 2
make: *** Waiting for unfinished jobs....

Steps To Reproduce

This is my kernel config to reproduce this error.

Expected behavior

kernel built and linked successfully with llvm toolchains.

Notify maintainers

I don't know who is the maintainer of bintools wrapper and lld wrapper.

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

[user@system:~]$ nix-shell -p nix-info --run "nix-info -m"

 - system: `"x86_64-linux"`
 - host os: `Linux 6.4.2, NixOS, 23.11 (Tapir), 23.11.20230706.3c74875`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.15.1`
 - nixpkgs: `/nix/store/b6028bvfc4fvg8qvlqvvj4ldqwalc9cs-source`

Activity

  1. added
    0.kind: bug
    on Jul 8, 2023
  2. SaltyKitkat commented on Jul 8, 2023

    @SaltyKitkat
    ContributorAuthor
  3. nickdesaulniers commented on Jul 10, 2023

    @nickdesaulniers

    Background perspective from upstream kernel maintainer for LLVM support:

    Once in a while we get issues reported that aren't reproducible for us on our machines; some Linux distros make changes to the implicit default values of their toolchains when configuring the builds of their toolchains. Unless the kernel's build system is explicit in setting these flags, we can get build failures (such as what's reported above) that appear to be specific to one distro's toolchain distribution.

    Personally, I'm curious if NixOS makes changes to ld.lld or wraps it in a script that implicitly sets other flags?

  4. cillynder commented on Aug 26, 2023

    @cillynder
    Contributor

    It appears that the wrapper sets the dynamic linker and causes the linking to fail.
    A workaround I found is to set sharedLibraryLoader to null in bintools like so:

    stdenv = overrideCC llvmPackages.stdenv (llvmPackages.stdenv.cc.override {
      bintools = llvmPackages.bintools.override {
        sharedLibraryLoader = null;
      };
    });

    I'm not sure what the implications of this are, if any, but my kernel seems to boot and work fine :D

  5. changed the title [-]Linux kernel cannot be liinked with the wrapped lld[/-] [+]Linux kernel cannot be linked with the wrapped lld[/+] on Aug 27, 2023
  6. nickdesaulniers commented on Aug 28, 2023

    @nickdesaulniers

    Interesting and good find! I wonder if we set -dynamic-linker /dev/null in upstream Kbuild (Linux kernel build system) if that would work, or if the wrapper will put -dynamic-linker after that (which would take precendence)? Maybe that's the distinction between extraBefore vs extraAfter?

    If it's possible in Kbuild, I wouldn't mind a patch that says something to the effect of:

    NixOS' distribution of ld.lld wraps it to add -dynamic-linker <path/to/loader/>. When this is set, linking will fail with error messages like:

    ld.lld: error: section .bsdata file range overlaps with .header
    .bsdata range is [0x1092, 0x122B]
    .header range is [0x11EF, 0x126B]
    ld.lld: error: section .bsdata virtual address range overlaps with .header
    .bsdata range is [0x92, 0x22B]
    .header range is [0x1EF, 0x26B]
    ld.lld: error: section .bsdata load address range overlaps with .header
    .bsdata range is [0x92, 0x22B]
    .header range is [0x1EF, 0x26B]
    

    Avoid this by explicitly setting the -dynamic-linker to /dev/null to avoid this.

    Note: it's not clear to my why setting the path to the dynamic loader explicitly would result in those error messages.

  7. yshui commented on Jan 21, 2024

    @yshui
    Contributor

    @nickdesaulniers

    Note: it's not clear to my why setting the path to the dynamic loader explicitly would result in those error messages.

    take a look at this kernel linker script:

    SECTIONS
    {
    	. = 0;
    	.bstext		: { *(.bstext) }
    	.bsdata		: { *(.bsdata) }
    
    	. = 495;
    	.header		: { *(.header) }
    	.entrytext	: { *(.entrytext) }
    	.inittext	: { *(.inittext) }
    	.initdata	: { *(.initdata) }
    	__end_init = .;
    
    

    it has hard coded addresses for sections. lld putting an .interp section at the start would mess up the addresses.

    the cause is ld.lld adds .interp in cases ld.bfd doesn't. I think the fix is either add --no-dynamic-linker to kernel LDFLAGS (--dynamic-linker /dev/null won't work btw); or make ld.lld behave the same as ld.bfd (llvm/llvm-project#78873). I don't know which one's the better solution.

  8. nickdesaulniers commented on Jan 21, 2024

    @nickdesaulniers

    Does nixos wrap the invocation of the kernel's build system (i.e. the invocation of make)? If so, then maybe it's appropriate for that build config to set a make command line var like KLDFLAGS. Oh, kbuild doesn't have KLDFLAGS like it does KCFLAGS according to https://docs.kernel.org/kbuild/kbuild.html. ARGH

    Based on @MaskRay's input, then a kernel patch adding --no-dynamic-linker when $(LD) == lld is appropriate.

  9. yshui commented on Jan 22, 2024

    @yshui
    Contributor

    @nickdesaulniers can't we add --no-dynamic-linker unconditionally? ld.bfd understands it too.

  10. nickdesaulniers commented on Jan 22, 2024

    @nickdesaulniers

    Probably, the last thing to check is that all supported versions (according to process.rst) for both toolchains support that flag, then it can be added unconditionally. Otherwise ld-option make macro should be used.

  11. yshui commented on Feb 13, 2024

    @yshui
    Contributor

    hello, I've submitted a kernel patch to address this problem. since my nix knowledge is limited, i need some help convincing kernel devs that this is the right solution, or if someone from nix thinks there is a better solution, what would be good too.

    that said, this is the mailing list thread: https://lore.kernel.org/llvm/[email protected]/T/#u would appreciate if someone can chime in.

    cc: (lld) @dtzWill @Ericson2314 @lovek323 @alyssais @RaitoBezarius @rrbutani @sternenseemann (kernel) @TredwellGit @Ma27 @NeQuissimus @thoughtpolice

  12. cillynder commented on Mar 14, 2025

    @cillynder
    Contributor

    #344665 should have already included the workaround for this issue by using the unwrapped linker when building with LLVM. I can get the kernel to build without having to unset the dynamic linker; doing so would make the build segfault very early on.. I'm not quite sure why, but I digress.

  13. added
    2.status: stale
    on Sep 11, 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

    0.kind: bug2.status: stale

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions