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