1 //===- LinkerScript.h -------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_LINKER_SCRIPT_H
11 #define LLD_ELF_LINKER_SCRIPT_H
16 #include "lld/Common/LLVM.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/MapVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/MemoryBuffer.h"
34 class InputSectionBase;
37 class InputSectionBase;
40 // This represents an r-value in the linker script.
42 ExprValue(SectionBase *Sec, bool ForceAbsolute, uint64_t Val,
44 : Sec(Sec), ForceAbsolute(ForceAbsolute), Val(Val), Loc(Loc.str()) {}
46 ExprValue(uint64_t Val) : ExprValue(nullptr, false, Val, "") {}
48 bool isAbsolute() const { return ForceAbsolute || Sec == nullptr; }
49 uint64_t getValue() const;
50 uint64_t getSecAddr() const;
51 uint64_t getSectionOffset() const;
53 // If a value is relative to a section, it has a non-null Sec.
56 // True if this expression is enclosed in ABSOLUTE().
57 // This flag affects the return value of getValue().
61 uint64_t Alignment = 1;
63 // Original source location. Used for error messages.
67 // This represents an expression in the linker script.
68 // ScriptParser::readExpr reads an expression and returns an Expr.
69 // Later, we evaluate the expression by calling the function.
70 typedef std::function<ExprValue()> Expr;
72 // This enum is used to implement linker script SECTIONS command.
73 // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
74 enum SectionsCommandKind {
75 AssignmentKind, // . = expr or <sym> = expr
78 AssertKind, // ASSERT(expr)
79 ByteKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
83 BaseCommand(int K) : Kind(K) {}
87 // This represents ". = <expr>" or "<symbol> = <expr>".
88 struct SymbolAssignment : BaseCommand {
89 SymbolAssignment(StringRef Name, Expr E, std::string Loc)
90 : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc) {}
92 static bool classof(const BaseCommand *C) {
93 return C->Kind == AssignmentKind;
96 // The LHS of an expression. Name is either a symbol name or ".".
98 Defined *Sym = nullptr;
100 // The RHS of an expression.
103 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
104 bool Provide = false;
107 // Holds file name and line number for error reporting.
108 std::string Location;
111 // Linker scripts allow additional constraints to be put on ouput sections.
112 // If an output section is marked as ONLY_IF_RO, the section is created
113 // only if its input sections are read-only. Likewise, an output section
114 // with ONLY_IF_RW is created if all input sections are RW.
115 enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
117 // This struct is used to represent the location and size of regions of
118 // target memory. Instances of the struct are created by parsing the
120 struct MemoryRegion {
121 MemoryRegion(StringRef Name, uint64_t Origin, uint64_t Length, uint32_t Flags,
123 : Name(Name), Origin(Origin), Length(Length), Flags(Flags),
124 NegFlags(NegFlags) {}
134 // This struct represents one section match pattern in SECTIONS() command.
135 // It can optionally have negative match pattern for EXCLUDED_FILE command.
136 // Also it may be surrounded with SORT() command, so contains sorting rules.
137 struct SectionPattern {
138 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
139 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
141 StringMatcher ExcludedFilePat;
142 StringMatcher SectionPat;
143 SortSectionPolicy SortOuter;
144 SortSectionPolicy SortInner;
148 struct InputSectionDescription : BaseCommand {
149 InputSectionDescription(StringRef FilePattern)
150 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
152 static bool classof(const BaseCommand *C) {
153 return C->Kind == InputSectionKind;
156 StringMatcher FilePat;
158 // Input sections that matches at least one of SectionPatterns
159 // will be associated with this InputSectionDescription.
160 std::vector<SectionPattern> SectionPatterns;
162 std::vector<InputSection *> Sections;
164 // Temporary record of synthetic ThunkSection instances and the pass that
165 // they were created in. This is used to insert newly created ThunkSections
166 // into Sections at the end of a createThunks() pass.
167 std::vector<std::pair<ThunkSection *, uint32_t>> ThunkSections;
170 // Represents an ASSERT().
171 struct AssertCommand : BaseCommand {
172 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
174 static bool classof(const BaseCommand *C) { return C->Kind == AssertKind; }
179 // Represents BYTE(), SHORT(), LONG(), or QUAD().
180 struct ByteCommand : BaseCommand {
181 ByteCommand(Expr E, unsigned Size)
182 : BaseCommand(ByteKind), Expression(E), Size(Size) {}
184 static bool classof(const BaseCommand *C) { return C->Kind == ByteKind; }
191 struct PhdrsCommand {
193 unsigned Type = llvm::ELF::PT_NULL;
194 bool HasFilehdr = false;
195 bool HasPhdrs = false;
196 llvm::Optional<unsigned> Flags;
197 Expr LMAExpr = nullptr;
200 class LinkerScript final {
201 // Temporary state used in processSectionCommands() and assignAddresses()
202 // that must be reinitialized for each call to the above functions, and must
203 // not be used outside of the scope of a call to the above functions.
204 struct AddressState {
206 uint64_t ThreadBssOffset = 0;
207 OutputSection *OutSec = nullptr;
208 MemoryRegion *MemRegion = nullptr;
209 MemoryRegion *LMARegion = nullptr;
210 uint64_t LMAOffset = 0;
213 llvm::DenseMap<StringRef, OutputSection *> NameToOutputSection;
215 void addSymbol(SymbolAssignment *Cmd);
216 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
217 void setDot(Expr E, const Twine &Loc, bool InSec);
219 std::vector<InputSection *>
220 computeInputSections(const InputSectionDescription *,
221 const llvm::DenseMap<SectionBase *, int> &Order);
223 std::vector<InputSection *>
224 createInputSectionList(OutputSection &Cmd,
225 const llvm::DenseMap<SectionBase *, int> &Order);
227 std::vector<size_t> getPhdrIndices(OutputSection *Sec);
229 MemoryRegion *findMemoryRegion(OutputSection *Sec);
231 void switchTo(OutputSection *Sec);
232 uint64_t advance(uint64_t Size, unsigned Align);
233 void output(InputSection *Sec);
235 void assignOffsets(OutputSection *Sec);
237 // Ctx captures the local AddressState and makes it accessible
238 // deliberately. This is needed as there are some cases where we cannot just
239 // thread the current state through to a lambda function created by the
241 // This should remain a plain pointer as its lifetime is smaller than
243 AddressState *Ctx = nullptr;
245 OutputSection *Aether;
250 OutputSection *createOutputSection(StringRef Name, StringRef Location);
251 OutputSection *getOrCreateOutputSection(StringRef Name);
253 bool hasPhdrsCommands() { return !PhdrsCommands.empty(); }
254 uint64_t getDot() { return Dot; }
255 void discard(ArrayRef<InputSection *> V);
257 ExprValue getSymbolValue(StringRef Name, const Twine &Loc);
259 void addOrphanSections();
260 void removeEmptyCommands();
261 void adjustSectionsBeforeSorting();
262 void adjustSectionsAfterSorting();
264 std::vector<PhdrEntry *> createPhdrs();
265 bool needsInterpSection();
267 bool shouldKeep(InputSectionBase *S);
268 void assignAddresses();
269 void allocateHeaders(std::vector<PhdrEntry *> &Phdrs);
270 void processSectionCommands();
272 // SECTIONS command list.
273 std::vector<BaseCommand *> SectionCommands;
275 // PHDRS command list.
276 std::vector<PhdrsCommand> PhdrsCommands;
278 bool HasSectionsCommand = false;
279 bool ErrorOnMissingSection = false;
281 // List of section patterns specified with KEEP commands. They will
282 // be kept even if they are unused and --gc-sections is specified.
283 std::vector<InputSectionDescription *> KeptSections;
285 // A map from memory region name to a memory region descriptor.
286 llvm::MapVector<llvm::StringRef, MemoryRegion *> MemoryRegions;
288 // A list of symbols referenced by the script.
289 std::vector<llvm::StringRef> ReferencedSymbols;
292 extern LinkerScript *Script;
294 } // end namespace elf
295 } // end namespace lld
297 #endif // LLD_ELF_LINKER_SCRIPT_H