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;
46 ExprValue(SectionBase *Sec, bool ForceAbsolute, uint64_t Val,
48 : Sec(Sec), Val(Val), ForceAbsolute(ForceAbsolute), Alignment(Alignment) {
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;
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;
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
70 AssertKind, // ASSERT(expr)
71 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
75 BaseCommand(int K) : Kind(K) {}
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) {}
84 static bool classof(const BaseCommand *C);
86 // The LHS of an expression. Name is either a symbol name or ".".
88 SymbolBody *Sym = nullptr;
90 // The RHS of an expression.
93 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
97 // Holds file name and line number for error reporting.
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 };
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
110 struct MemoryRegion {
119 struct OutputSectionCommand : BaseCommand {
120 OutputSectionCommand(StringRef Name)
121 : BaseCommand(OutputSectionKind), Name(Name) {}
123 static bool classof(const BaseCommand *C);
125 OutputSection *Sec = nullptr;
126 MemoryRegion *MemRegion = nullptr;
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;
139 template <class ELFT> void writeTo(uint8_t *Buf);
140 template <class ELFT> void maybeCompress();
141 uint32_t getFiller();
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) {}
151 StringMatcher ExcludedFilePat;
152 StringMatcher SectionPat;
153 SortSectionPolicy SortOuter;
154 SortSectionPolicy SortInner;
157 struct InputSectionDescription : BaseCommand {
158 InputSectionDescription(StringRef FilePattern)
159 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
161 static bool classof(const BaseCommand *C);
163 StringMatcher FilePat;
165 // Input sections that matches at least one of SectionPatterns
166 // will be associated with this InputSectionDescription.
167 std::vector<SectionPattern> SectionPatterns;
169 std::vector<InputSection *> Sections;
172 // Represents an ASSERT().
173 struct AssertCommand : BaseCommand {
174 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
176 static bool classof(const BaseCommand *C);
181 // Represents BYTE(), SHORT(), LONG(), or QUAD().
182 struct BytesDataCommand : BaseCommand {
183 BytesDataCommand(Expr E, unsigned Size)
184 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
186 static bool classof(const BaseCommand *C);
193 struct PhdrsCommand {
202 // ScriptConfiguration holds linker script parse results.
203 struct ScriptConfiguration {
204 // Used to assign addresses to sections.
205 std::vector<BaseCommand *> Commands;
207 // Used to assign sections to headers.
208 std::vector<PhdrsCommand> PhdrsCommands;
210 bool HasSections = false;
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;
216 // A map from memory region name to a memory region descriptor.
217 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
219 // A list of symbols referenced by the script.
220 std::vector<llvm::StringRef> ReferencedSymbols;
223 class LinkerScript final {
224 llvm::DenseMap<OutputSection *, OutputSectionCommand *> SecToCommand;
225 llvm::DenseMap<StringRef, OutputSectionCommand *> NameToOutputSectionCommand;
227 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
228 void setDot(Expr E, const Twine &Loc, bool InSec);
230 std::vector<InputSection *>
231 computeInputSections(const InputSectionDescription *);
233 std::vector<InputSectionBase *>
234 createInputSectionList(OutputSectionCommand &Cmd);
236 std::vector<size_t> getPhdrIndices(OutputSection *Sec);
237 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
239 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd);
241 void switchTo(OutputSection *Sec);
242 uint64_t advance(uint64_t Size, unsigned Align);
243 void output(InputSection *Sec);
244 void process(BaseCommand &Base);
246 OutputSection *Aether;
249 uint64_t ThreadBssOffset = 0;
251 std::function<uint64_t()> LMAOffset;
252 OutputSection *CurOutSec = nullptr;
253 MemoryRegion *CurMemRegion = nullptr;
256 bool ErrorOnMissingSection = false;
257 OutputSectionCommand *createOutputSectionCommand(StringRef Name,
259 OutputSectionCommand *getOrCreateOutputSectionCommand(StringRef Name);
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);
266 ExprValue getSymbolValue(const Twine &Loc, StringRef S);
267 bool isDefined(StringRef S);
269 std::vector<OutputSection *> *OutputSections;
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 placeOrphanSections();
283 void processNonSectionCommands();
285 void assignAddresses(std::vector<PhdrEntry> &Phdrs,
286 ArrayRef<OutputSectionCommand *> OutputSectionCommands);
288 void addSymbol(SymbolAssignment *Cmd);
289 void processCommands(OutputSectionFactory &Factory);
291 // Parsed linker script configurations are set to this struct.
292 ScriptConfiguration Opt;
295 extern LinkerScript *Script;
297 } // end namespace elf
298 } // end namespace lld
300 #endif // LLD_ELF_LINKER_SCRIPT_H