Mclinker - the final Toolchain Frontier
Total Page:16
File Type:pdf, Size:1020Kb
MCLinker - the final toolchain frontier J¨orgSonnenberger March 16, 2013 Abstract Google gold on the other hand mixes the first two phases. Symbols that can't be resolved in the first LLVM and Clang provide the top half of an inte- round are retried later. This choice improves the grated BSD licensed cross-platform toolchain. The maintainability of performance of the linker code, MCLinker project implements the remaining parts. but is still more complicated than the design of It is compact, modular and efficient. Currently MCLinker. It is useful to note that gold supports supported platforms are ELF systems running X86 multi-threading during the relocation processing to (32bit and 64bit), ARM and MIPS. MIPS64 and speed up linking large input sets. Hexagon are work-in-progress. 2.1 Input tree 1 Introduction The input tree is the first intermediate representa- Following the GPL3 announcement of the Free Soft- tion (IR) used by MCLinker. It contains vertices ware Foundation, the LLVM project gained a lot for every input file and all positional options. Posi- of traction. The Clang front-end allows replac- tional options are options like {start-group or {as- ing GCC for C and C++ code on many impor- needed that change the handling of a subset of the tant platforms. The Machine Code layer provides input files. Input files can form subtrees, e.g. linker an integrated assembler, replacing the dependency archives form a subtree with a child node for every on external assemblers for most situations. The archive member. Edges in the input graph are ei- combination provides a powerful toolchain with full ther positional relationships for options or inclusion support for cross-compilation. For a long time, links for archives. the only major component missing to replace GNU binutils has been a linker. This gap is now filled by 2.2 Symbol resolution the Machine Code Linker (MCLinker) project. This paper first introduces the architecture of Symbol resolution is implemented using tree itera- MCLinker and compares it to GNU ld. The perfor- tion. As result of symbol resolution, the low-level mance for linking large programs is evaluated and IR is created: the fragment reference graph. This compared with GNU ld and Google gold. A de- graph represents the individual sections found dur- tailed status report of the implementation is pre- ing input process and the symbolic dependencies sented as well as list of future projects. between them. The input tree is traversed and for every input file it is checked whether it must be included in the 2 Architecture output. An input file is included if it was explicitly mentioned on the command line or if it provides MCLinker is a modern, modular linker. It is de- a definition for a currently undefined symbol. If signed to process input in three separate phases: the input is to be included, its sections and symbol • Building the input tree based on command line table are processed. When the tree iteration hits and implicit default values. the start of a linker group, the current position is pushed onto a stack. Iteration continues until the • Resolve symbols. corresponding end is seen. The linker now checks if any new undefined reference was found while pro- • Determine output positions, apply relocations cessing this group. If so, the process starts again and write output files. from memorized position. GNU ld doesn't have a clear phase separation; The data structures used for symbol resolution individual steps are re-applied iteratively until all are highly optimized to maximize cache locality. input files are processed and all resolvable symbols The symbol attributes and the initial part of the are resolved. The result is complicated and cache- name share a cache line on modern CPUs. This unfriendly code. helps the CPU when linking large programs with many smaller functions; C++ applications are well- The resulting binaries have the following size: known for such characteristics. MCLinker GNU ld gold llvm-tblgen .text 2,123,527 1,827,773 1,786,483 2.3 Layout .data 2,408 2,664 2,520 .bss 5,360 5,912 2,520 The layout step is the first part of the last linking phase. It is responsible for deciding what sections clang should be written in which order and to what posi- .text 34,299,746 26,917,022 26,698,448 tion. It merges sections as necessary or drops them, .data 21,984 22,112 22,112 if they are redundant (e.g. multiple inline defini- .bss 47,624 47,736 47,704 tions for C++ methods). At the end of this phase, Both MCLinker and gold are significantly faster the symbol values are finalized. than GNU ld. MCLinker is clearly pulling in too many objects, which is responsible for the much Defering the section merging until such a late larger binaries. This is likely also reponsible for step in the linking process has the advantage of a good part of the performance difference to gold. avoiding many redundant computations. The list Evidently, further analysis is needed. of all input sections is known at this point and af- Of special interest for embedded use is ter a single ordering pass, addresses can be assigned the memory footprint of the different linkers: directly. MCLinker GNU ld gold llvm-tblgen 17,508KB 17,700KB 17,528KB 2.4 Relocation clang 176MB 150MB 182MB The peak Resident Memory Size shows clearly The relocation step follows the layout step and is re- that both MCLinker and gold are optimised for in- sponsible for determining the symbol values in the memory processing. Since they are reading the full various places that reference the symbols. It may symbol tables of all input files first, they require prepare to modify the instructions to replace more more memory. costly code with simpler sequences. This helps platforms with limited immediate value ranges like ARM and MIPS to load short (relative) address di- 4 Implementation status rectly instead of using constant tables. It can also be used to use more efficient access methods for Linker functionality falls into two categories. The Thread Local Storage (TLS). first is generic features like symbol versioning which are handled in the platform independent core. The second is features specific to a target platform like 2.5 Writing the handling of the different relocation types. Once the relocation step is done, all that is left is 4.1 Platform independent applying the relocations to the input sections and writing the result to the output file with any meta- Most features necessary for ELF systems are im- data necessary for the file format. MCLinker ag- plemented. This includes static and shared linkage gressively uses memory mapped files when possible. as well partially linking objects to form a new re- This leverages the page lookup table (TLB) cache locatable object. Weak symbols are overwritten by and improves page locality. It helps improves the strong definitions. The different visibility types are use of the OS file system cache. handled. Missing for shared libraries is the process- ing of DT NEEDED entries and loading the sym- bols of the referenced objects. This means that cur- 3 Performance rently only symbols in libraries explicitly requested on the command line are found, when standard To measure the performance of MCLinker, the ELF behavior is to recursively scan libraries and llvm-tblgen and clang binaries were built using the their dependencies. This omission is not absolved normal options in NetBSD (-O2, no -g). The fi- by recent versions of GNU ld switching to the same nal linker invocation was repeated using MCLinker, broken semantic. GNU ld and Google gold. For MCLinker the cur- C++ requires constructor and destructor sup- rent development version of March 11 was used. port. The compiler uses .ctors or .init array sec- GNU ld and gold are both from GNU binutils tions (or .dtor and .fini array for destructors) to 2.22.90. implement this. MCLinker has full support for ei- MCLinker GNU ld gold ther form. It doesn't try to convert one mecha- llvm-tblgen 0.05s 0.10s 0.04s nism into the other due to the slightly different clang 0.69s 1.41s 0.44s ordering rules. Support for COMDAT-sections is present. This allows defining the implemention of 5 Future work a function in more than one object file and let- ting the linker pick one. It is used for implicit The biggest priority for the MCLinker project is to template specialisations. The .eh frame sections finish the missing platform independent features. necessary for exception handling are correctly pro- This means adding symbol versioning and linker cessed and the associated .eh frame hdr section is script support. Another important item is support created. The latter provides a fast binary search for Link Time Optimisation (LTO). Open research table to reduce runtime overhead. One issue in this questions include the impact of fine-grained layout area is the missing support for relocations into the decisions by creating individual sections for every .eh frame section. This is needed for support the function in the compiler and letting the linker figure older register frame info style shared library in- out the best placement. terface. Support for the alternative 32bit ABI on X86 The platform-independent part of TLS support (X32), MIPS64 and Hexagon is work-in-progres. is present. This concerns the proper merging of the .tdata and .tbss sections and the corresponding segments. 6 Summary Support for symbol versioning is missing. For MCLinker provides a cross-compiling friendly linker NetBSD, it is only used by libgcc at the moment, so for many platforms targetted by the BSDs.