]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/LinkerScript.h
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306325, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / LinkerScript.h
1 //===- LinkerScript.h -------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_ELF_LINKER_SCRIPT_H
11 #define LLD_ELF_LINKER_SCRIPT_H
12
13 #include "Config.h"
14 #include "Strings.h"
15 #include "Writer.h"
16 #include "lld/Core/LLVM.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include <cstddef>
23 #include <cstdint>
24 #include <functional>
25 #include <memory>
26 #include <vector>
27
28 namespace lld {
29 namespace elf {
30
31 class DefinedCommon;
32 class SymbolBody;
33 class InputSectionBase;
34 class InputSection;
35 class OutputSection;
36 class OutputSectionFactory;
37 class InputSectionBase;
38 class SectionBase;
39
40 struct ExprValue {
41   SectionBase *Sec;
42   uint64_t Val;
43   bool ForceAbsolute;
44   uint64_t Alignment = 1;
45   std::string Loc;
46
47   ExprValue(SectionBase *Sec, bool ForceAbsolute, uint64_t Val,
48             const Twine &Loc)
49       : Sec(Sec), Val(Val), ForceAbsolute(ForceAbsolute), Loc(Loc.str()) {}
50   ExprValue(SectionBase *Sec, uint64_t Val, const Twine &Loc)
51       : ExprValue(Sec, false, Val, Loc) {}
52   ExprValue(uint64_t Val) : ExprValue(nullptr, Val, "") {}
53   bool isAbsolute() const { return ForceAbsolute || Sec == nullptr; }
54   uint64_t getValue() const;
55   uint64_t getSecAddr() const;
56 };
57
58 // This represents an expression in the linker script.
59 // ScriptParser::readExpr reads an expression and returns an Expr.
60 // Later, we evaluate the expression by calling the function.
61 typedef std::function<ExprValue()> Expr;
62
63 // This enum is used to implement linker script SECTIONS command.
64 // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
65 enum SectionsCommandKind {
66   AssignmentKind, // . = expr or <sym> = expr
67   OutputSectionKind,
68   InputSectionKind,
69   AssertKind,   // ASSERT(expr)
70   BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
71 };
72
73 struct BaseCommand {
74   BaseCommand(int K) : Kind(K) {}
75   int Kind;
76 };
77
78 // This represents ". = <expr>" or "<symbol> = <expr>".
79 struct SymbolAssignment : BaseCommand {
80   SymbolAssignment(StringRef Name, Expr E, std::string Loc)
81       : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc) {}
82
83   static bool classof(const BaseCommand *C);
84
85   // The LHS of an expression. Name is either a symbol name or ".".
86   StringRef Name;
87   SymbolBody *Sym = nullptr;
88
89   // The RHS of an expression.
90   Expr Expression;
91
92   // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
93   bool Provide = false;
94   bool Hidden = false;
95
96   // Holds file name and line number for error reporting.
97   std::string Location;
98 };
99
100 // Linker scripts allow additional constraints to be put on ouput sections.
101 // If an output section is marked as ONLY_IF_RO, the section is created
102 // only if its input sections are read-only. Likewise, an output section
103 // with ONLY_IF_RW is created if all input sections are RW.
104 enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
105
106 // This struct is used to represent the location and size of regions of
107 // target memory. Instances of the struct are created by parsing the
108 // MEMORY command.
109 struct MemoryRegion {
110   std::string Name;
111   uint64_t Origin;
112   uint64_t Length;
113   uint64_t Offset;
114   uint32_t Flags;
115   uint32_t NegFlags;
116 };
117
118 struct OutputSectionCommand : BaseCommand {
119   OutputSectionCommand(StringRef Name)
120       : BaseCommand(OutputSectionKind), Name(Name) {}
121
122   static bool classof(const BaseCommand *C);
123
124   OutputSection *Sec = nullptr;
125   MemoryRegion *MemRegion = nullptr;
126   StringRef Name;
127   Expr AddrExpr;
128   Expr AlignExpr;
129   Expr LMAExpr;
130   Expr SubalignExpr;
131   std::vector<BaseCommand *> Commands;
132   std::vector<StringRef> Phdrs;
133   llvm::Optional<uint32_t> Filler;
134   ConstraintKind Constraint = ConstraintKind::NoConstraint;
135   std::string Location;
136   std::string MemoryRegionName;
137   bool Noload = false;
138
139   template <class ELFT> void finalize();
140   template <class ELFT> void writeTo(uint8_t *Buf);
141   template <class ELFT> void maybeCompress();
142   uint32_t getFiller();
143 };
144
145 // This struct represents one section match pattern in SECTIONS() command.
146 // It can optionally have negative match pattern for EXCLUDED_FILE command.
147 // Also it may be surrounded with SORT() command, so contains sorting rules.
148 struct SectionPattern {
149   SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
150       : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
151
152   StringMatcher ExcludedFilePat;
153   StringMatcher SectionPat;
154   SortSectionPolicy SortOuter;
155   SortSectionPolicy SortInner;
156 };
157
158 struct InputSectionDescription : BaseCommand {
159   InputSectionDescription(StringRef FilePattern)
160       : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
161
162   static bool classof(const BaseCommand *C);
163
164   StringMatcher FilePat;
165
166   // Input sections that matches at least one of SectionPatterns
167   // will be associated with this InputSectionDescription.
168   std::vector<SectionPattern> SectionPatterns;
169
170   std::vector<InputSection *> Sections;
171 };
172
173 // Represents an ASSERT().
174 struct AssertCommand : BaseCommand {
175   AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
176
177   static bool classof(const BaseCommand *C);
178
179   Expr Expression;
180 };
181
182 // Represents BYTE(), SHORT(), LONG(), or QUAD().
183 struct BytesDataCommand : BaseCommand {
184   BytesDataCommand(Expr E, unsigned Size)
185       : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
186
187   static bool classof(const BaseCommand *C);
188
189   Expr Expression;
190   unsigned Offset;
191   unsigned Size;
192 };
193
194 struct PhdrsCommand {
195   StringRef Name;
196   unsigned Type;
197   bool HasFilehdr;
198   bool HasPhdrs;
199   unsigned Flags;
200   Expr LMAExpr;
201 };
202
203 // ScriptConfiguration holds linker script parse results.
204 struct ScriptConfiguration {
205   // Used to assign addresses to sections.
206   std::vector<BaseCommand *> Commands;
207
208   // Used to assign sections to headers.
209   std::vector<PhdrsCommand> PhdrsCommands;
210
211   bool HasSections = false;
212
213   // List of section patterns specified with KEEP commands. They will
214   // be kept even if they are unused and --gc-sections is specified.
215   std::vector<InputSectionDescription *> KeptSections;
216
217   // A map from memory region name to a memory region descriptor.
218   llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
219
220   // A list of symbols referenced by the script.
221   std::vector<llvm::StringRef> ReferencedSymbols;
222 };
223
224 class LinkerScript final {
225   llvm::DenseMap<OutputSection *, OutputSectionCommand *> SecToCommand;
226   llvm::DenseMap<StringRef, OutputSectionCommand *> NameToOutputSectionCommand;
227
228   void assignSymbol(SymbolAssignment *Cmd, bool InSec);
229   void setDot(Expr E, const Twine &Loc, bool InSec);
230
231   std::vector<InputSection *>
232   computeInputSections(const InputSectionDescription *);
233
234   std::vector<InputSectionBase *>
235   createInputSectionList(OutputSectionCommand &Cmd);
236
237   std::vector<size_t> getPhdrIndices(OutputSection *Sec);
238   size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
239
240   MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd);
241
242   void switchTo(OutputSection *Sec);
243   uint64_t advance(uint64_t Size, unsigned Align);
244   void output(InputSection *Sec);
245   void process(BaseCommand &Base);
246
247   OutputSection *Aether;
248
249   uint64_t Dot;
250   uint64_t ThreadBssOffset = 0;
251
252   std::function<uint64_t()> LMAOffset;
253   OutputSection *CurOutSec = nullptr;
254   MemoryRegion *CurMemRegion = nullptr;
255
256 public:
257   bool ErrorOnMissingSection = false;
258   OutputSectionCommand *createOutputSectionCommand(StringRef Name,
259                                                    StringRef Location);
260   OutputSectionCommand *getOrCreateOutputSectionCommand(StringRef Name);
261
262   OutputSectionCommand *getCmd(OutputSection *Sec) const;
263   bool hasPhdrsCommands() { return !Opt.PhdrsCommands.empty(); }
264   uint64_t getDot() { return Dot; }
265   void discard(ArrayRef<InputSectionBase *> V);
266
267   ExprValue getSymbolValue(const Twine &Loc, StringRef S);
268   bool isDefined(StringRef S);
269
270   void fabricateDefaultCommands();
271   void addOrphanSections(OutputSectionFactory &Factory);
272   void removeEmptyCommands();
273   void adjustSectionsBeforeSorting();
274   void adjustSectionsAfterSorting();
275
276   std::vector<PhdrEntry> createPhdrs();
277   bool ignoreInterpSection();
278
279   bool hasLMA(OutputSection *Sec);
280   bool shouldKeep(InputSectionBase *S);
281   void assignOffsets(OutputSectionCommand *Cmd);
282   void createOrphanCommands();
283   void processNonSectionCommands();
284   void assignAddresses(std::vector<PhdrEntry> &Phdrs);
285
286   void addSymbol(SymbolAssignment *Cmd);
287   void processCommands(OutputSectionFactory &Factory);
288
289   // Parsed linker script configurations are set to this struct.
290   ScriptConfiguration Opt;
291 };
292
293 extern LinkerScript *Script;
294
295 } // end namespace elf
296 } // end namespace lld
297
298 #endif // LLD_ELF_LINKER_SCRIPT_H