]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - docs/Readers.rst
Vendor import of lld trunk r233088:
[FreeBSD/FreeBSD.git] / docs / Readers.rst
1 .. _Readers:
2
3 Developing lld Readers
4 ======================
5
6 Introduction
7 ------------
8
9 The purpose of a "Reader" is to take an object file in a particular format
10 and create an `lld::File`:cpp:class: (which is a graph of Atoms)
11 representing the object file.  A Reader inherits from
12 `lld::Reader`:cpp:class: which lives in
13 :file:`include/lld/Core/Reader.h` and
14 :file:`lib/Core/Reader.cpp`.
15
16 The Reader infrastructure for an object format ``Foo`` requires the
17 following pieces in order to fit into lld:
18
19 :file:`include/lld/ReaderWriter/ReaderFoo.h`
20
21    .. cpp:class:: ReaderOptionsFoo : public ReaderOptions
22
23       This Options class is the only way to configure how the Reader will
24       parse any file into an `lld::Reader`:cpp:class: object.  This class
25       should be declared in the `lld`:cpp:class: namespace.
26
27    .. cpp:function:: Reader *createReaderFoo(ReaderOptionsFoo &reader)
28
29       This factory function configures and create the Reader. This function
30       should be declared in the `lld`:cpp:class: namespace.
31
32 :file:`lib/ReaderWriter/Foo/ReaderFoo.cpp`
33
34    .. cpp:class:: ReaderFoo : public Reader
35
36       This is the concrete Reader class which can be called to parse
37       object files. It should be declared in an anonymous namespace or
38       if there is shared code with the `lld::WriterFoo`:cpp:class: you
39       can make a nested namespace (e.g. `lld::foo`:cpp:class:).
40
41 You may have noticed that :cpp:class:`ReaderFoo` is not declared in the
42 ``.h`` file. An important design aspect of lld is that all Readers are
43 created *only* through an object-format-specific
44 :cpp:func:`createReaderFoo` factory function. The creation of the Reader is
45 parametrized through a :cpp:class:`ReaderOptionsFoo` class. This options
46 class is the one-and-only way to control how the Reader operates when
47 parsing an input file into an Atom graph. For instance, you may want the
48 Reader to only accept certain architectures. The options class can be
49 instantiated from command line options or be programmatically configured.
50
51 Where to start
52 --------------
53
54 The lld project already has a skeleton of source code for Readers for
55 ``ELF``, ``PECOFF``, ``MachO``, and lld's native Atom graph format
56 (both binary ``Native`` and ``YAML`` representations).  If your file format
57 is a variant of one of those, you should modify the existing Reader to
58 support your variant. This is done by customizing the Options
59 class for the Reader and making appropriate changes to the ``.cpp`` file to
60 interpret those options and act accordingly.
61
62 If your object file format is not a variant of any existing Reader, you'll need
63 to create a new Reader subclass with the organization described above.
64
65 Readers are factories
66 ---------------------
67
68 The linker will usually only instantiate your Reader once.  That one Reader will
69 have its loadFile() method called many times with different input files.
70 To support multithreaded linking, the Reader may be parsing multiple input
71 files in parallel. Therefore, there should be no parsing state in you Reader
72 object.  Any parsing state should be in ivars of your File subclass or in
73 some temporary object.
74
75 The key method to implement in a reader is::
76
77   virtual error_code loadFile(LinkerInput &input,
78                               std::vector<std::unique_ptr<File>> &result);
79
80 It takes a memory buffer (which contains the contents of the object file
81 being read) and returns an instantiated lld::File object which is
82 a collection of Atoms. The result is a vector of File pointers (instead of
83 simple a File pointer) because some file formats allow multiple object
84 "files" to be encoded in one file system file.
85
86
87 Memory Ownership
88 ----------------
89
90 Atoms are always owned by their File object. During core linking when Atoms
91 are coalesced or stripped away, core linking does not delete them.
92 Core linking just removes those unused Atoms from its internal list.
93 The destructor of a File object is responsible for deleting all Atoms it
94 owns, and if ownership of the MemoryBuffer was passed to it, the File
95 destructor needs to delete that too.
96
97 Making Atoms
98 ------------
99
100 The internal model of lld is purely Atom based.  But most object files do not
101 have an explicit concept of Atoms, instead most have "sections". The way
102 to think of this is that a section is just a list of Atoms with common
103 attributes.
104
105 The first step in parsing section-based object files is to cleave each
106 section into a list of Atoms. The technique may vary by section type. For
107 code sections (e.g. .text), there are usually symbols at the start of each
108 function. Those symbol addresses are the points at which the section is
109 cleaved into discrete Atoms.  Some file formats (like ELF) also include the
110 length of each symbol in the symbol table. Otherwise, the length of each
111 Atom is calculated to run to the start of the next symbol or the end of the
112 section.
113
114 Other sections types can be implicitly cleaved. For instance c-string literals
115 or unwind info (e.g. .eh_frame) can be cleaved by having the Reader look at
116 the content of the section.  It is important to cleave sections into Atoms
117 to remove false dependencies. For instance the .eh_frame section often
118 has no symbols, but contains "pointers" to the functions for which it
119 has unwind info.  If the .eh_frame section was not cleaved (but left as one
120 big Atom), there would always be a reference (from the eh_frame Atom) to
121 each function.  So the linker would be unable to coalesce or dead stripped
122 away the function atoms.
123
124 The lld Atom model also requires that a reference to an undefined symbol be
125 modeled as a Reference to an UndefinedAtom. So the Reader also needs to
126 create an UndefinedAtom for each undefined symbol in the object file.
127
128 Once all Atoms have been created, the second step is to create References
129 (recall that Atoms are "nodes" and References are "edges"). Most References
130 are created by looking at the "relocation records" in the object file. If
131 a function contains a call to "malloc", there is usually a relocation record
132 specifying the address in the section and the symbol table index. Your
133 Reader will need to convert the address to an Atom and offset and the symbol
134 table index into a target Atom. If "malloc" is not defined in the object file,
135 the target Atom of the Reference will be an UndefinedAtom.
136
137
138 Performance
139 -----------
140 Once you have the above working to parse an object file into Atoms and
141 References, you'll want to look at performance.  Some techniques that can
142 help performance are:
143
144 * Use llvm::BumpPtrAllocator or pre-allocate one big vector<Reference> and then
145   just have each atom point to its subrange of References in that vector.
146   This can be faster that allocating each Reference as separate object.
147 * Pre-scan the symbol table and determine how many atoms are in each section
148   then allocate space for all the Atom objects at once.
149 * Don't copy symbol names or section content to each Atom, instead use
150   StringRef and ArrayRef in each Atom to point to its name and content in the
151   MemoryBuffer.
152
153
154 Testing
155 -------
156
157 We are still working on infrastructure to test Readers. The issue is that
158 you don't want to check in binary files to the test suite. And the tools
159 for creating your object file from assembly source may not be available on
160 every OS.
161
162 We are investigating a way to use YAML to describe the section, symbols,
163 and content of a file. Then have some code which will write out an object
164 file from that YAML description.
165
166 Once that is in place, you can write test cases that contain section/symbols
167 YAML and is run through the linker to produce Atom/References based YAML which
168 is then run through FileCheck to verify the Atoms and References are as
169 expected.
170
171
172