blob: 14d180936902002cfa906c227b89284e08c53928 [file] [log] [blame]
Daniel Dunbar59694112012-04-06 21:02:24 +00001.. _design:
2
3Linker Design
4=============
5
6Introduction
7------------
8
9lld is a new generation of linker. It is not "section" based like traditional
10linkers which mostly just interlace sections from multiple object files into the
11output file. Instead, lld is based on "Atoms". Traditional section based
12linking work well for simple linking, but their model makes advanced linking
13features difficult to implement. Features like dead code stripping, reordering
14functions for locality, and C++ coalescing require the linker to work at a finer
15grain.
16
17An atom is an indivisible chunk of code or data. An atom has a set of
18attributes, such as: name, scope, content-type, alignment, etc. An atom also
19has a list of References. A Reference contains: a kind, an optional offset, an
20optional addend, and an optional target atom.
21
22The Atom model allows the linker to use standard graph theory models for linking
23data structures. Each atom is a node, and each Reference is an edge. The
24feature of dead code stripping is implemented by following edges to mark all
25live atoms, and then delete the non-live atoms.
26
27
28Atom Model
29----------
30
Michael J. Spenceraa53d682012-04-25 19:59:06 +000031An atom is an indivisible chunk of code or data. Typically each user written
Daniel Dunbar59694112012-04-06 21:02:24 +000032function or global variable is an atom. In addition, the compiler may emit
33other atoms, such as for literal c-strings or floating point constants, or for
34runtime data structures like dwarf unwind info or pointers to initializers.
35
36A simple "hello world" object file would be modeled like this:
37
38.. image:: hello.png
39
40There are three atoms: main, a proxy for printf, and an anonymous atom
41containing the c-string literal "hello world". The Atom "main" has two
42references. One is the call site for the call to printf, and the other is a
Michael J. Spenceraa53d682012-04-25 19:59:06 +000043reference for the instruction that loads the address of the c-string literal.
Daniel Dunbar59694112012-04-06 21:02:24 +000044
Marshall Clow341f4962012-07-18 23:20:40 +000045There are only four different types of atoms:
46
47 * DefinedAtom
48 95% of all atoms. This is a chunk of code or data
Shankar Easwaran3d8de472014-01-27 03:09:26 +000049
50 * UndefinedAtom
Marshall Clow341f4962012-07-18 23:20:40 +000051 This is a place holder in object files for a reference to some atom
52 outside the translation unit.During core linking it is usually replaced
53 by (coalesced into) another Atom.
Shankar Easwaran3d8de472014-01-27 03:09:26 +000054
Marshall Clow341f4962012-07-18 23:20:40 +000055 * SharedLibraryAtom
Shankar Easwaran3d8de472014-01-27 03:09:26 +000056 If a required symbol name turns out to be defined in a dynamic shared
57 library (and not some object file). A SharedLibraryAtom is the
Marshall Clow341f4962012-07-18 23:20:40 +000058 placeholder Atom used to represent that fact.
Shankar Easwaran3d8de472014-01-27 03:09:26 +000059
60 It is similar to an UndefinedAtom, but it also tracks information
Marshall Clow341f4962012-07-18 23:20:40 +000061 about the associated shared library.
Shankar Easwaran3d8de472014-01-27 03:09:26 +000062
Marshall Clow341f4962012-07-18 23:20:40 +000063 * AbsoluteAtom
64 This is for embedded support where some stuff is implemented in ROM at
65 some fixed address. This atom has no content. It is just an address
Alex Rosenbergb65e8882013-02-03 07:05:26 +000066 that the Writer needs to fix up any references to point to.
Marshall Clow341f4962012-07-18 23:20:40 +000067
68
Daniel Dunbar59694112012-04-06 21:02:24 +000069File Model
70----------
71
72The linker views the input files as basically containers of Atoms and
73References, and just a few attributes of their own. The linker works with three
74kinds of files: object files, static libraries, and dynamic shared libraries.
75Each kind of file has reader object which presents the file in the model
76expected by the linker.
77
78Object File
79~~~~~~~~~~~
80
81An object file is just a container of atoms. When linking an object file, a
82reader is instantiated which parses the object file and instantiates a set of
83atoms representing all content in the .o file. The linker adds all those atoms
84to a master graph.
85
86Static Library (Archive)
87~~~~~~~~~~~~~~~~~~~~~~~~
88
89This is the traditional unix static archive which is just a collection of object
90files with a "table of contents". When linking with a static library, by default
91nothing is added to the master graph of atoms. Instead, if after merging all
92atoms from object files into a master graph, if any "undefined" atoms are left
93remaining in the master graph, the linker reads the table of contents for each
94static library to see if any have the needed definitions. If so, the set of
95atoms from the specified object file in the static library is added to the
96master graph of atoms.
97
98Dynamic Library (Shared Object)
99~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100
101Dynamic libraries are different than object files and static libraries in that
102they don't directly add any content. Their purpose is to check at build time
103that the remaining undefined references can be resolved at runtime, and provide
104a list of dynamic libraries (SO_NEEDED) that will be needed at runtime. The way
105this is modeled in the linker is that a dynamic library contributes no atoms to
106the initial graph of atoms. Instead, (like static libraries) if there are
107"undefined" atoms in the master graph of all atoms, then each dynamic library is
108checked to see if exports the required symbol. If so, a "shared library" atom is
109instantiated by the by the reader which the linker uses to replace the
110"undefined" atom.
111
112Linking Steps
113-------------
114
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000115Through the use of abstract Atoms, the core of linking is architecture
Daniel Dunbar59694112012-04-06 21:02:24 +0000116independent and file format independent. All command line parsing is factored
117out into a separate "options" abstraction which enables the linker to be driven
118with different command line sets.
119
120The overall steps in linking are:
121
122 #. Command line processing
123
124 #. Parsing input files
125
126 #. Resolving
127
128 #. Passes/Optimizations
129
130 #. Generate output file
131
132The Resolving and Passes steps are done purely on the master graph of atoms, so
133they have no notion of file formats such as mach-o or ELF.
134
Nick Kledzikabb69812012-05-31 22:34:00 +0000135
136Input Files
137~~~~~~~~~~~
138
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000139Existing developer tools using different file formats for object files.
Nick Kledzikabb69812012-05-31 22:34:00 +0000140A goal of lld is to be file format independent. This is done
141through a plug-in model for reading object files. The lld::Reader is the base
142class for all object file readers. A Reader follows the factory method pattern.
143A Reader instantiates an lld::File object (which is a graph of Atoms) from a
144given object file (on disk or in-memory).
145
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000146Every Reader subclass defines its own "options" class (for instance the mach-o
147Reader defines the class ReaderOptionsMachO). This options class is the
Nick Kledzikabb69812012-05-31 22:34:00 +0000148one-and-only way to control how the Reader operates when parsing an input file
149into an Atom graph. For instance, you may want the Reader to only accept
150certain architectures. The options class can be instantiated from command
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000151line options, or it can be subclassed and the ivars programmatically set.
Nick Kledzikabb69812012-05-31 22:34:00 +0000152
Daniel Dunbar59694112012-04-06 21:02:24 +0000153Resolving
154~~~~~~~~~
155
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000156The resolving step takes all the atoms' graphs from each object file and
157combines them into one master object graph. Unfortunately, it is not as simple
158as appending the atom list from each file into one big list. There are many
Nick Kledzikabb69812012-05-31 22:34:00 +0000159cases where atoms need to be coalesced. That is, two or more atoms need to be
Daniel Dunbar59694112012-04-06 21:02:24 +0000160coalesced into one atom. This is necessary to support: C language "tentative
161definitions", C++ weak symbols for templates and inlines defined in headers,
162replacing undefined atoms with actual definition atoms, and for merging copies
163of constants like c-strings and floating point constants.
164
165The linker support coalescing by-name and by-content. By-name is used for
166tentative definitions and weak symbols. By-content is used for constant data
167that can be merged.
168
169The resolving process maintains some global linking "state", including a "symbol
170table" which is a map from llvm::StringRef to lld::Atom*. With these data
Gabor Greifc52fc9e2012-04-25 21:09:37 +0000171structures, the linker iterates all atoms in all input files. For each atom, it
Daniel Dunbar59694112012-04-06 21:02:24 +0000172checks if the atom is named and has a global or hidden scope. If so, the atom
173is added to the symbol table map. If there already is a matching atom in that
174table, that means the current atom needs to be coalesced with the found atom, or
175it is a multiple definition error.
176
177When all initial input file atoms have been processed by the resolver, a scan is
178made to see if there are any undefined atoms in the graph. If there are, the
179linker scans all libraries (both static and dynamic) looking for definitions to
180replace the undefined atoms. It is an error if any undefined atoms are left
181remaining.
182
183Dead code stripping (if requested) is done at the end of resolving. The linker
184does a simple mark-and-sweep. It starts with "root" atoms (like "main" in a main
185executable) and follows each references and marks each Atom that it visits as
186"live". When done, all atoms not marked "live" are removed.
187
188The result of the Resolving phase is the creation of an lld::File object. The
Nick Kledzikbb963df2012-04-18 21:55:06 +0000189goal is that the lld::File model is **the** internal representation
Daniel Dunbar59694112012-04-06 21:02:24 +0000190throughout the linker. The file readers parse (mach-o, ELF, COFF) into an
191lld::File. The file writers (mach-o, ELF, COFF) taken an lld::File and produce
192their file kind, and every Pass only operates on an lld::File. This is not only
193a simpler, consistent model, but it enables the state of the linker to be dumped
194at any point in the link for testing purposes.
195
196
197Passes
198~~~~~~
199
200The Passes step is an open ended set of routines that each get a change to
201modify or enhance the current lld::File object. Some example Passes are:
202
203 * stub (PLT) generation
204
205 * GOT instantiation
206
207 * order_file optimization
208
209 * branch island generation
210
211 * branch shim generation
212
213 * Objective-C optimizations (Darwin specific)
214
215 * TLV instantiation (Darwin specific)
216
Alex Rosenbergb65e8882013-02-03 07:05:26 +0000217 * DTrace probe processing (Darwin specific)
Daniel Dunbar59694112012-04-06 21:02:24 +0000218
219 * compact unwind encoding (Darwin specific)
220
221
222Some of these passes are specific to Darwin's runtime environments. But many of
223the passes are applicable to any OS (such as generating branch island for out of
224range branch instructions).
225
226The general structure of a pass is to iterate through the atoms in the current
227lld::File object, inspecting each atom and doing something. For instance, the
228stub pass, looks for call sites to shared library atoms (e.g. call to printf).
229It then instantiates a "stub" atom (PLT entry) and a "lazy pointer" atom for
230each proxy atom needed, and these new atoms are added to the current lld::File
231object. Next, all the noted call sites to shared library atoms have their
232References altered to point to the stub atom instead of the shared library atom.
233
Nick Kledzikabb69812012-05-31 22:34:00 +0000234
Daniel Dunbar59694112012-04-06 21:02:24 +0000235Generate Output File
236~~~~~~~~~~~~~~~~~~~~
237
238Once the passes are done, the output file writer is given current lld::File
239object. The writer's job is to create the executable content file wrapper and
240place the content of the atoms into it.
241
Nick Kledzikabb69812012-05-31 22:34:00 +0000242lld uses a plug-in model for writing output files. All concrete writers (e.g.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000243ELF, mach-o, etc) are subclasses of the lld::Writer class.
Nick Kledzikbb963df2012-04-18 21:55:06 +0000244
Nick Kledzikabb69812012-05-31 22:34:00 +0000245Unlike the Reader class which has just one method to instantiate an lld::File,
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000246the Writer class has multiple methods. The crucial method is to generate the
Nick Kledzikabb69812012-05-31 22:34:00 +0000247output file, but there are also methods which allow the Writer to contribute
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000248Atoms to the resolver and specify passes to run.
Nick Kledzikabb69812012-05-31 22:34:00 +0000249
250An example of contributing
251atoms is that if the Writer knows a main executable is being linked and such
252an executable requires a specially named entry point (e.g. "_main"), the Writer
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000253can add an UndefinedAtom with that special name to the resolver. This will
254cause the resolver to issue an error if that symbol is not defined.
Nick Kledzikabb69812012-05-31 22:34:00 +0000255
256Sometimes a Writer supports lazily created symbols, such as names for the start
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000257of sections. To support this, the Writer can create a File object which vends
258no initial atoms, but does lazily supply atoms by name as needed.
Nick Kledzikabb69812012-05-31 22:34:00 +0000259
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000260Every Writer subclass defines its own "options" class (for instance the mach-o
261Writer defines the class WriterOptionsMachO). This options class is the
262one-and-only way to control how the Writer operates when producing an output
Nick Kledzikabb69812012-05-31 22:34:00 +0000263file from an Atom graph. For instance, you may want the Writer to optimize
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000264the output for certain OS versions, or strip local symbols, etc. The options
265class can be instantiated from command line options, or it can be subclassed
266and the ivars programmatically set.
Nick Kledzikbb963df2012-04-18 21:55:06 +0000267
268
Daniel Dunbar59694112012-04-06 21:02:24 +0000269lld::File representations
270-------------------------
271
Rui Ueyamaaa7b3042015-04-10 21:23:51 +0000272Just as LLVM has three representations of its IR model, lld has two
Daniel Dunbar59694112012-04-06 21:02:24 +0000273representations of its File/Atom/Reference model:
274
275 * In memory, abstract C++ classes (lld::Atom, lld::Reference, and lld::File).
276
277 * textual (in YAML)
278
Daniel Dunbar59694112012-04-06 21:02:24 +0000279
280Textual representations in YAML
281~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282
283In designing a textual format we want something easy for humans to read and easy
284for the linker to parse. Since an atom has lots of attributes most of which are
285usually just the default, we should define default values for every attribute so
286that those can be omitted from the text representation. Here is the atoms for a
287simple hello world program expressed in YAML::
288
289 target-triple: x86_64-apple-darwin11
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000290
Daniel Dunbar59694112012-04-06 21:02:24 +0000291 atoms:
292 - name: _main
293 scope: global
294 type: code
295 content: [ 55, 48, 89, e5, 48, 8d, 3d, 00, 00, 00, 00, 30, c0, e8, 00, 00,
296 00, 00, 31, c0, 5d, c3 ]
297 fixups:
298 - offset: 07
299 kind: pcrel32
300 target: 2
301 - offset: 0E
302 kind: call32
303 target: _fprintf
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000304
Daniel Dunbar59694112012-04-06 21:02:24 +0000305 - type: c-string
306 content: [ 73, 5A, 00 ]
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000307
Daniel Dunbar59694112012-04-06 21:02:24 +0000308 ...
309
310The biggest use for the textual format will be writing test cases. Writing test
311cases in C is problematic because the compiler may vary its output over time for
312its own optimization reasons which my inadvertently disable or break the linker
313feature trying to be tested. By writing test cases in the linkers own textual
314format, we can exactly specify every attribute of every atom and thus target
315specific linker logic.
316
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000317The textual/YAML format follows the ReaderWriter patterns used in lld. The lld
318library comes with the classes: ReaderYAML and WriterYAML.
Nick Kledzikabb69812012-05-31 22:34:00 +0000319
320
Daniel Dunbar59694112012-04-06 21:02:24 +0000321Testing
Nick Kledzikabb69812012-05-31 22:34:00 +0000322-------
Daniel Dunbar59694112012-04-06 21:02:24 +0000323
324The lld project contains a test suite which is being built up as new code is
325added to lld. All new lld functionality should have a tests added to the test
326suite. The test suite is `lit <http://llvm.org/cmds/lit.html/>`_ driven. Each
327test is a text file with comments telling lit how to run the test and check the
328result To facilitate testing, the lld project builds a tool called lld-core.
329This tool reads a YAML file (default from stdin), parses it into one or more
330lld::File objects in memory and then feeds those lld::File objects to the
Rui Ueyamaadafac62015-04-09 20:43:38 +0000331resolver phase.
Daniel Dunbar59694112012-04-06 21:02:24 +0000332
333
334Resolver testing
335~~~~~~~~~~~~~~~~
336
337Basic testing is the "core linking" or resolving phase. That is where the
338linker merges object files. All test cases are written in YAML. One feature of
339YAML is that it allows multiple "documents" to be encoding in one YAML stream.
340That means one text file can appear to the linker as multiple .o files - the
341normal case for the linker.
342
343Here is a simple example of a core linking test case. It checks that an
344undefined atom from one file will be replaced by a definition from another
345file::
346
347 # RUN: lld-core %s | FileCheck %s
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000348
Daniel Dunbar59694112012-04-06 21:02:24 +0000349 #
350 # Test that undefined atoms are replaced with defined atoms.
351 #
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000352
Daniel Dunbar59694112012-04-06 21:02:24 +0000353 ---
354 atoms:
355 - name: foo
356 definition: undefined
357 ---
358 atoms:
359 - name: foo
360 scope: global
361 type: code
362 ...
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000363
Daniel Dunbar59694112012-04-06 21:02:24 +0000364 # CHECK: name: foo
365 # CHECK: scope: global
366 # CHECK: type: code
367 # CHECK-NOT: name: foo
368 # CHECK: ...
369
370
371Passes testing
372~~~~~~~~~~~~~~
373
374Since Passes just operate on an lld::File object, the lld-core tool has the
375option to run a particular pass (after resolving). Thus, you can write a YAML
376test case with carefully crafted input to exercise areas of a Pass and the check
377the resulting lld::File object as represented in YAML.
378
379
380Design Issues
381-------------
382
383There are a number of open issues in the design of lld. The plan is to wait and
384make these design decisions when we need to.
385
386
387Debug Info
388~~~~~~~~~~
389
390Currently, the lld model says nothing about debug info. But the most popular
391debug format is DWARF and there is some impedance mismatch with the lld model
392and DWARF. In lld there are just Atoms and only Atoms that need to be in a
393special section at runtime have an associated section. Also, Atoms do not have
394addresses. The way DWARF is spec'ed different parts of DWARF are supposed to go
395into specially named sections and the DWARF references function code by address.
396
397CPU and OS specific functionality
398~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
399
400Currently, lld has an abstract "Platform" that deals with any CPU or OS specific
401differences in linking. We just keep adding virtual methods to the base
402Platform class as we find linking areas that might need customization. At some
403point we'll need to structure this better.
404
405
406File Attributes
407~~~~~~~~~~~~~~~
408
409Currently, lld::File just has a path and a way to iterate its atoms. We will
Gabor Greifc52fc9e2012-04-25 21:09:37 +0000410need to add more attributes on a File. For example, some equivalent to the
Daniel Dunbar59694112012-04-06 21:02:24 +0000411target triple. There is also a number of cached or computed attributes that
412could make various Passes more efficient. For instance, on Darwin there are a
413number of Objective-C optimizations that can be done by a Pass. But it would
414improve the plain C case if the Objective-C optimization Pass did not have to
415scan all atoms looking for any Objective-C data structures. This could be done
416if the lld::File object had an attribute that said if the file had any
417Objective-C data in it. The Resolving phase would then be required to "merge"
418that attribute as object files are added.