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 {
128 // This struct represents one section match pattern in SECTIONS() command.
129 // It can optionally have negative match pattern for EXCLUDED_FILE command.
130 // Also it may be surrounded with SORT() command, so contains sorting rules.
131 struct SectionPattern {
132 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
133 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
135 StringMatcher ExcludedFilePat;
136 StringMatcher SectionPat;
137 SortSectionPolicy SortOuter;
138 SortSectionPolicy SortInner;
142 struct InputSectionDescription : BaseCommand {
143 InputSectionDescription(StringRef FilePattern)
144 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
146 static bool classof(const BaseCommand *C) {
147 return C->Kind == InputSectionKind;
150 StringMatcher FilePat;
152 // Input sections that matches at least one of SectionPatterns
153 // will be associated with this InputSectionDescription.
154 std::vector<SectionPattern> SectionPatterns;
156 std::vector<InputSection *> Sections;
158 // Temporary record of synthetic ThunkSection instances and the pass that
159 // they were created in. This is used to insert newly created ThunkSections
160 // into Sections at the end of a createThunks() pass.
161 std::vector<std::pair<ThunkSection *, uint32_t>> ThunkSections;
164 // Represents an ASSERT().
165 struct AssertCommand : BaseCommand {
166 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
168 static bool classof(const BaseCommand *C) { return C->Kind == AssertKind; }
173 // Represents BYTE(), SHORT(), LONG(), or QUAD().
174 struct ByteCommand : BaseCommand {
175 ByteCommand(Expr E, unsigned Size)
176 : BaseCommand(ByteKind), Expression(E), Size(Size) {}
178 static bool classof(const BaseCommand *C) { return C->Kind == ByteKind; }
185 struct PhdrsCommand {
187 unsigned Type = llvm::ELF::PT_NULL;
188 bool HasFilehdr = false;
189 bool HasPhdrs = false;
190 llvm::Optional<unsigned> Flags;
191 Expr LMAExpr = nullptr;
194 class LinkerScript final {
195 // Temporary state used in processSectionCommands() and assignAddresses()
196 // that must be reinitialized for each call to the above functions, and must
197 // not be used outside of the scope of a call to the above functions.
198 struct AddressState {
200 uint64_t ThreadBssOffset = 0;
201 OutputSection *OutSec = nullptr;
202 MemoryRegion *MemRegion = nullptr;
203 llvm::DenseMap<const MemoryRegion *, uint64_t> MemRegionOffset;
204 std::function<uint64_t()> LMAOffset;
207 llvm::DenseMap<StringRef, OutputSection *> NameToOutputSection;
209 void addSymbol(SymbolAssignment *Cmd);
210 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
211 void setDot(Expr E, const Twine &Loc, bool InSec);
213 std::vector<InputSection *>
214 computeInputSections(const InputSectionDescription *,
215 const llvm::DenseMap<SectionBase *, int> &Order);
217 std::vector<InputSection *>
218 createInputSectionList(OutputSection &Cmd,
219 const llvm::DenseMap<SectionBase *, int> &Order);
221 std::vector<size_t> getPhdrIndices(OutputSection *Sec);
223 MemoryRegion *findMemoryRegion(OutputSection *Sec);
225 void switchTo(OutputSection *Sec);
226 uint64_t advance(uint64_t Size, unsigned Align);
227 void output(InputSection *Sec);
229 void assignOffsets(OutputSection *Sec);
231 // Ctx captures the local AddressState and makes it accessible
232 // deliberately. This is needed as there are some cases where we cannot just
233 // thread the current state through to a lambda function created by the
235 // This should remain a plain pointer as its lifetime is smaller than
237 AddressState *Ctx = nullptr;
239 OutputSection *Aether;
244 OutputSection *createOutputSection(StringRef Name, StringRef Location);
245 OutputSection *getOrCreateOutputSection(StringRef Name);
247 bool hasPhdrsCommands() { return !PhdrsCommands.empty(); }
248 uint64_t getDot() { return Dot; }
249 void discard(ArrayRef<InputSection *> V);
251 ExprValue getSymbolValue(StringRef Name, const Twine &Loc);
253 void addOrphanSections();
254 void removeEmptyCommands();
255 void adjustSectionsBeforeSorting();
256 void adjustSectionsAfterSorting();
258 std::vector<PhdrEntry *> createPhdrs();
259 bool needsInterpSection();
261 bool shouldKeep(InputSectionBase *S);
262 void assignAddresses();
263 void allocateHeaders(std::vector<PhdrEntry *> &Phdrs);
264 void processSectionCommands();
266 // SECTIONS command list.
267 std::vector<BaseCommand *> SectionCommands;
269 // PHDRS command list.
270 std::vector<PhdrsCommand> PhdrsCommands;
272 bool HasSectionsCommand = false;
273 bool ErrorOnMissingSection = false;
275 // List of section patterns specified with KEEP commands. They will
276 // be kept even if they are unused and --gc-sections is specified.
277 std::vector<InputSectionDescription *> KeptSections;
279 // A map from memory region name to a memory region descriptor.
280 llvm::MapVector<llvm::StringRef, MemoryRegion *> MemoryRegions;
282 // A list of symbols referenced by the script.
283 std::vector<llvm::StringRef> ReferencedSymbols;
286 extern LinkerScript *Script;
288 } // end namespace elf
289 } // end namespace lld
291 #endif // LLD_ELF_LINKER_SCRIPT_H