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