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/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"
33 class InputSectionBase;
36 class OutputSectionFactory;
37 class InputSectionBase;
44 uint64_t Alignment = 1;
47 ExprValue(SectionBase *Sec, bool ForceAbsolute, uint64_t Val,
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;
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;
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
69 AssertKind, // ASSERT(expr)
70 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
74 BaseCommand(int K) : Kind(K) {}
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) {}
83 static bool classof(const BaseCommand *C);
85 // The LHS of an expression. Name is either a symbol name or ".".
87 SymbolBody *Sym = nullptr;
89 // The RHS of an expression.
92 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
96 // Holds file name and line number for error reporting.
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 };
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
109 struct MemoryRegion {
118 struct OutputSectionCommand : BaseCommand {
119 OutputSectionCommand(StringRef Name)
120 : BaseCommand(OutputSectionKind), Name(Name) {}
122 static bool classof(const BaseCommand *C);
124 OutputSection *Sec = nullptr;
125 MemoryRegion *MemRegion = nullptr;
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;
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();
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) {}
152 StringMatcher ExcludedFilePat;
153 StringMatcher SectionPat;
154 SortSectionPolicy SortOuter;
155 SortSectionPolicy SortInner;
158 struct InputSectionDescription : BaseCommand {
159 InputSectionDescription(StringRef FilePattern)
160 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
162 static bool classof(const BaseCommand *C);
164 StringMatcher FilePat;
166 // Input sections that matches at least one of SectionPatterns
167 // will be associated with this InputSectionDescription.
168 std::vector<SectionPattern> SectionPatterns;
170 std::vector<InputSection *> Sections;
173 // Represents an ASSERT().
174 struct AssertCommand : BaseCommand {
175 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
177 static bool classof(const BaseCommand *C);
182 // Represents BYTE(), SHORT(), LONG(), or QUAD().
183 struct BytesDataCommand : BaseCommand {
184 BytesDataCommand(Expr E, unsigned Size)
185 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
187 static bool classof(const BaseCommand *C);
194 struct PhdrsCommand {
203 // ScriptConfiguration holds linker script parse results.
204 struct ScriptConfiguration {
205 // Used to assign addresses to sections.
206 std::vector<BaseCommand *> Commands;
208 // Used to assign sections to headers.
209 std::vector<PhdrsCommand> PhdrsCommands;
211 bool HasSections = false;
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;
217 // A map from memory region name to a memory region descriptor.
218 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
220 // A list of symbols referenced by the script.
221 std::vector<llvm::StringRef> ReferencedSymbols;
224 class LinkerScript final {
225 llvm::DenseMap<OutputSection *, OutputSectionCommand *> SecToCommand;
226 llvm::DenseMap<StringRef, OutputSectionCommand *> NameToOutputSectionCommand;
228 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
229 void setDot(Expr E, const Twine &Loc, bool InSec);
231 std::vector<InputSection *>
232 computeInputSections(const InputSectionDescription *);
234 std::vector<InputSectionBase *>
235 createInputSectionList(OutputSectionCommand &Cmd);
237 std::vector<size_t> getPhdrIndices(OutputSection *Sec);
238 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
240 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd);
242 void switchTo(OutputSection *Sec);
243 uint64_t advance(uint64_t Size, unsigned Align);
244 void output(InputSection *Sec);
245 void process(BaseCommand &Base);
247 OutputSection *Aether;
250 uint64_t ThreadBssOffset = 0;
252 std::function<uint64_t()> LMAOffset;
253 OutputSection *CurOutSec = nullptr;
254 MemoryRegion *CurMemRegion = nullptr;
257 bool ErrorOnMissingSection = false;
258 OutputSectionCommand *createOutputSectionCommand(StringRef Name,
260 OutputSectionCommand *getOrCreateOutputSectionCommand(StringRef Name);
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);
267 ExprValue getSymbolValue(const Twine &Loc, StringRef S);
268 bool isDefined(StringRef S);
270 void fabricateDefaultCommands();
271 void addOrphanSections(OutputSectionFactory &Factory);
272 void removeEmptyCommands();
273 void adjustSectionsBeforeSorting();
274 void adjustSectionsAfterSorting();
276 std::vector<PhdrEntry> createPhdrs();
277 bool ignoreInterpSection();
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);
286 void addSymbol(SymbolAssignment *Cmd);
287 void processCommands(OutputSectionFactory &Factory);
289 // Parsed linker script configurations are set to this struct.
290 ScriptConfiguration Opt;
293 extern LinkerScript *Script;
295 } // end namespace elf
296 } // end namespace lld
298 #endif // LLD_ELF_LINKER_SCRIPT_H