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

Linking with LLD #39915

Open
Tracked by #15
bstrie opened this issue Feb 17, 2017 · 112 comments
Open
Tracked by #15

Linking with LLD #39915

bstrie opened this issue Feb 17, 2017 · 112 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-feature-request Category: A feature request, i.e: not implemented / a PR. I-compiletime Issue: Problems and improvements with respect to compile times. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@bstrie
Copy link
Contributor

bstrie commented Feb 17, 2017

LLVM 4.0 is shipping with LLD enabled, though AFAIK it is not yet production-ready on all platforms. I believe we've got an LLVM upgrade planned soon to resolve AVR/emscripten problems anyway, so now's the time to start determining what we might need to do to support it, how it impacts compiler performance/binary size/runtime performance compared to our usual linkers, and what platforms on which we might want to enable it by default.

Current status (2020-04-24) summarized at #39915 (comment)

Possible problems:

  • LLD has internal parallelism, and is not documented to have jobserver support, which can lead to poor contention resolution in practice
@bstrie bstrie added A-linkage Area: linking into static, shared libraries and binaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 17, 2017
@bstrie
Copy link
Contributor Author

bstrie commented Feb 17, 2017

See also a PoC in #36120.

@retep998
Copy link
Member

retep998 commented Feb 18, 2017

LLD may be a very good candidate for MinGW targets, because we currently bundle a linker with them anyway and MinGW's linker has a variety of issues ranging from a lack of ASLR to no bigobj support. If we can somehow also bring along the necessary mingw libraries when cross compiling and not just native targeting (which rustup's mingw package is currently limited to), then that would enable rust cross compilation from linux out of the box, which would be a huge improvement over the existing situation where people get MinGW from their distro and then run into issues because distros almost always use an incompatible MinGW.

LLD is not a good candidate for natively targeting MSVC targets, due to several reasons, the primary reason being the lack of debuginfo support. Cross compiling to MSVC targets requires libraries that can't be redistributed so we can't support that out of the box anyway.

@bstrie
Copy link
Contributor Author

bstrie commented Feb 19, 2017

The tracking issue for upgrading to LLVM 4.0 is #37609 .

@binarycrusader
Copy link
Contributor

For the record lld is definitely not ready for Solaris targets. But on Solaris, there's no reason that I'm aware of to use lld instead of the native ld instead. We've already been looking at what it would take to have rust use Solaris ld on Solaris instead of using gcc for linking.

@whitequark
Copy link
Member

@binarycrusader One reason to use lld is when building for Solaris, not on Solaris.

@japaric
Copy link
Member

japaric commented Feb 21, 2017

PR #40018 adds a -Z linker-flavor flag to rustc to make it possible to use LLD as a linker. That PR doesn't embed LLD in rustc but allows out of tree experimentation with it.

@binarycrusader ^ that may help with your experiment of directly using Solaris' ld instead of gcc.

@bstrie
Copy link
Contributor Author

bstrie commented Apr 25, 2017

We now appear to be running on LLVM 4.0. @japaric , does this mean that the linker-flavor flag can now be easily used to compare and contrast LLD with the system linker?

@japaric
Copy link
Member

japaric commented Apr 29, 2017

@bstrie #40018 landed a few weeks ago. Since that landed one has been able to use -Z linker-flavor=ld -C linker=ld.lld to use an external LLD binary as a linker. Note that, unlike gcc, LLD doesn't know where the system libraries are so you'll have to pass the library search path to the linker using -C link-args='-L ...' if you are linking to any system library.

What LLVM 4.0 helps with is merging LLD into rustc. With that change we wouldn't require an external linker in some scenarios like linking MUSL binaries or bare metal programs. I say some scenarios because most targets require linking to system libraries where you will run into the library search path problem I mentioned above. For those targets, LLD won't work out of the box. It's not clear how and where to solve that problem and without a solution for that we can't switch to LLD for the most important (tier 1) targets, which the reduces the appeal of embedding LLD into rustc in the first place.

@whitequark
Copy link
Member

@japaric What are the arguments against embedding (sysroot-relative library) search paths, as well as things like -lc -lpthread crt0.o, directly into rustc? After all, some component of the toolchain has to embed them as we don't have any standard for the platforms to follow, and binutils is not a good golden source of this knowledge.

The only drawback I can think of is the situation where the same triple would have different search paths on different flavors of systems (which will likely be exclusive to Linux/glibc triples, and especially bad on platforms with multilib). In that case, I believe clang snoops the OS name and hardcodes OS-specific conventions, which seems bad but probably unavoidable if one wants to distribute a single binary that runs on any Linux (and doesn't need the system linker).

@iainnicol
Copy link

@retep998 I had a brief look at lld a few months ago. I wasn't able to cross compile (cross link?) an .exe from Linux. It seemed like lld only supports platform native formats.

Hopefully I'm mistaken.

@Mark-Simulacrum Mark-Simulacrum added the C-feature-request Category: A feature request, i.e: not implemented / a PR. label Jul 27, 2017
@bstrie bstrie added the I-slow Issue: Problems and improvements with respect to performance of generated code. label Sep 8, 2017
@bstrie
Copy link
Contributor Author

bstrie commented Sep 8, 2017

Tagging this as a performance bug, since according to LLD's benchmarks it seems to outperform GNU ld by a factor of ten, and linking performance is a large component of compiler speed currently.

@bstrie
Copy link
Contributor Author

bstrie commented Sep 8, 2017

Er, forgot to link the benchmarks: https://lld.llvm.org/#performance

(Relevant today since LLVM 5.0 has just been released.)

@tpimh
Copy link

tpimh commented Sep 9, 2017

Linking with LLD is much faster than bfd or gold, but I doubt that using it will significantly improve overall performance. Still I think this issue is important and should be a priority.

@bstrie
Copy link
Contributor Author

bstrie commented Sep 9, 2017

@tpimh I'm actually not entirely sure whether the I-slow tag is supposed to represent runtime performance bugs or compiletime performance bugs, I was intending it as the latter. And IME when I look at time-passes output linking is usually in the top three longest phases, significantly longer than most, so even cutting linking time in half would probably be a huge win (especially for big things like Servo and rustc).

@jonas-schievink
Copy link
Contributor

@bstrie I-slow is for bad runtime performance, I-compiletime is for compiler perf last time I checked

@retep998 retep998 added I-compiletime Issue: Problems and improvements with respect to compile times. and removed I-slow Issue: Problems and improvements with respect to performance of generated code. labels Sep 10, 2017
@iainnicol
Copy link

iainnicol commented Sep 17, 2017

Good news for anybody interested in the obscure topic of cross linking from Linux to Windows. I said earlier it wasn't possible with lld, but that's only true for lld's ld flavor. It's possible for lld's link.exe flavour (lld-link).

Specifically for Rust we can do this today with a couple code changes.

  1. We need to compile a very small subset of mingw-w64's CRT into .o object files. Namely some thread local storage init. We also need chkstk.

  2. lld does not like MinGW's usual import libraries. Instead we need to build the .def files into .lib files ourselves, using lld-link or llvm-dlltool

  3. Modify lld to treat IMPORT_NAME_NOPREFIX like
    IMPORT_NAME_UNDECORATE, because even with step 2 the .libs aren't perfect

  4. Modify Rust's seh.rs to replace TYPE_INFO_VTABLE with ptr::null(). Required because the symbol ??_7type_info@@6B@ isn't defined in MinGW. Then build and install Rust.

  5. Use .cargo/config to specify a custom wrapper script as the linker.

  6. Our wrapper linker script should invoke lld-link mostly using the parameters it is passed. However we must make a few tweaks:

    a) Fix filename casing e.g. change AdvAPI32.Lib to advapi32.lib

    b) Modify the .def file Rust generates to prefix symbols with an extra underscore

    c) Override the entry point (/entry). Required probably due to a name mangling issue.

    d) Append the mingw-crt object files you compiled in step 1

  7. Build your Rust project using xargo --target=i686-pc-windows-msvc

Doing the above steps allows me to cross compile Rust code. I can even panic and catch panics using Rust's SEH-based unwinding.

@retep998
Copy link
Member

@iainnicol You're mixing the msvc target with MinGW bits, which is why you have to do all those weird modifications. If you just copy over the libraries from an existing VC++ installation then you can use lld-link normally without all those modifications or any MinGW bits.

@whitequark
Copy link
Member

But I don't want to use an existing VC++ installation. There isn't even any way to get one without spending something like eight hours downloading and installing junk, much less to redistribute.

@rpjohnst
Copy link
Contributor

The standalone build tools are much lighter-weight, unless that's already what you're referring to, in which case perhaps we ought to put some work into improving or recreating what MinGW did so it's actually compatible with MSVC.

@whitequark
Copy link
Member

The standalone build tools are much lighter-weight

I haven't realized Microsoft distributes those. Could you link to them? Is there any reasonable way to extract the installation archive without actually running it i.e. is it an msi or something similar?

@rpjohnst
Copy link
Contributor

Here they are: http://landinghub.visualstudio.com/visual-cpp-build-tools

Both the 2015 and 2017 versions are exes, but you may be able to convince the 2017 exe to give you what you want via this: https://docs.microsoft.com/en-us/visualstudio/install/install-vs-inconsistent-quality-network

@retep998
Copy link
Member

If we really want to do this right for Windows, we'd first of all need #30027 to eliminate the need for the Windows SDK or MinGW's import libraries. Then all we'd have left is to replace the CRT bits with our own pure Rust versions (math/memory functions, entry point, a few other runtime bits Rust needs) and we'd have a fully self-contained Rust toolchain that can create Windows binaries! The downside of this is that you wouldn't be able to statically link C/C++ code because that relies very heavily on linking in the appropriate CRT from either MinGW or VC++. Of course the whole point of Rust is to rewrite everything in Rust, so this isn't really much of an issue.

@thormighti
Copy link

I will like to know if the goal of using lld as the default linker has been achieved ?

@bjorn3
Copy link
Member

bjorn3 commented Mar 19, 2023

It isn't the default yet.

@nicoburns
Copy link

I believe LLD on macOS is now in a much better state (and specifically that whereas previously the macOS version of lld was a completely separate codebase from the windows/linux versions, macOS lld is now based on the same codebase as windows/linux lld). Certainly, linking with homebrew installed LLD seems to work well with stable Rust.

Would it make sense to create an issue for making LLD the default linker on macOS?

@nnethercote
Copy link
Contributor

There are issues open for Linux and Windows already and none for Mac. So having one might be reasonable for parity, but I don't think it will make any difference one way or the other about when this will happen. AIUI there are still various things blocking shipping on any platform, e.g. this. @lqd and @petrochenkov will know more.

@neon-mmd
Copy link

neon-mmd commented Feb 7, 2024

I would like to know the current status of the lld support as the default linker, so could anyone of you please let me know about the current status?

@andrewzhurov

This comment was marked as abuse.

@whitequark
Copy link
Member

Here's a summary by ChatGPT 3.5, hope it's of help:

It is actively unhelpful.

@Kobzol
Copy link
Contributor

Kobzol commented Mar 17, 2024

I would like to know the current status of the lld support as the default linker, so could anyone of you please let me know about the current status?

It's mostly prepared, implementation-wise, but we're still deciding on how should the CLI flags work. See #119906 for the most recent discussion.

@lqd
Copy link
Member

lqd commented May 17, 2024

status update: as of #124129, rustup-distributed rustc (not distros') on x86_64-unknown-linux-gnu uses the self-contained lld linker by default, (rust-lld, itself also distributed with rustup), on nightly only for now and starting from tomorrow's nightly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-feature-request Category: A feature request, i.e: not implemented / a PR. I-compiletime Issue: Problems and improvements with respect to compile times. 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