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;
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;
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;
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
65 AssertKind, // ASSERT(expr)
66 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
70 BaseCommand(int K) : Kind(K) {}
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) {}
79 static bool classof(const BaseCommand *C);
81 // The LHS of an expression. Name is either a symbol name or ".".
83 SymbolBody *Sym = nullptr;
85 // The RHS of an expression.
88 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
92 // Holds file name and line number for error reporting.
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 };
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
105 struct MemoryRegion {
114 struct OutputSectionCommand : BaseCommand {
115 OutputSectionCommand(StringRef Name)
116 : BaseCommand(OutputSectionKind), Name(Name) {}
118 static bool classof(const BaseCommand *C);
120 OutputSection *Sec = nullptr;
121 MemoryRegion *MemRegion = nullptr;
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;
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) {}
142 StringMatcher ExcludedFilePat;
143 StringMatcher SectionPat;
144 SortSectionPolicy SortOuter;
145 SortSectionPolicy SortInner;
148 struct InputSectionDescription : BaseCommand {
149 InputSectionDescription(StringRef FilePattern)
150 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
152 static bool classof(const BaseCommand *C);
154 StringMatcher FilePat;
156 // Input sections that matches at least one of SectionPatterns
157 // will be associated with this InputSectionDescription.
158 std::vector<SectionPattern> SectionPatterns;
160 std::vector<InputSectionBase *> Sections;
163 // Represents an ASSERT().
164 struct AssertCommand : BaseCommand {
165 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
167 static bool classof(const BaseCommand *C);
172 // Represents BYTE(), SHORT(), LONG(), or QUAD().
173 struct BytesDataCommand : BaseCommand {
174 BytesDataCommand(Expr E, unsigned Size)
175 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
177 static bool classof(const BaseCommand *C);
184 struct PhdrsCommand {
193 // ScriptConfiguration holds linker script parse results.
194 struct ScriptConfiguration {
195 // Used to assign addresses to sections.
196 std::vector<BaseCommand *> Commands;
198 // Used to assign sections to headers.
199 std::vector<PhdrsCommand> PhdrsCommands;
201 bool HasSections = false;
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;
207 // A map from memory region name to a memory region descriptor.
208 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
210 // A list of symbols referenced by the script.
211 std::vector<llvm::StringRef> ReferencedSymbols;
216 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
217 void setDot(Expr E, const Twine &Loc, bool InSec);
219 std::vector<InputSectionBase *>
220 computeInputSections(const InputSectionDescription *);
222 std::vector<InputSectionBase *>
223 createInputSectionList(OutputSectionCommand &Cmd);
225 std::vector<size_t> getPhdrIndices(StringRef SectionName);
226 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
228 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd);
230 void switchTo(OutputSection *Sec);
232 void output(InputSection *Sec);
233 void process(BaseCommand &Base);
235 OutputSection *Aether;
236 bool ErrorOnMissingSection = false;
239 uint64_t ThreadBssOffset = 0;
241 std::function<uint64_t()> LMAOffset;
242 OutputSection *CurOutSec = nullptr;
243 MemoryRegion *CurMemRegion = nullptr;
245 llvm::DenseSet<OutputSection *> AlreadyOutputOS;
246 llvm::DenseSet<InputSectionBase *> AlreadyOutputIS;
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);
255 ExprValue getSymbolValue(const Twine &Loc, StringRef S);
256 bool isDefined(StringRef S);
258 std::vector<OutputSection *> *OutputSections;
259 void fabricateDefaultCommands(bool AllocateHeader);
260 void addOrphanSections(OutputSectionFactory &Factory);
261 void removeEmptyCommands();
262 void adjustSectionsBeforeSorting();
263 void adjustSectionsAfterSorting();
265 std::vector<PhdrEntry> createPhdrs();
266 bool ignoreInterpSection();
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);
277 void writeDataBytes(StringRef Name, uint8_t *Buf);
278 void addSymbol(SymbolAssignment *Cmd);
279 void processCommands(OutputSectionFactory &Factory);
281 // Parsed linker script configurations are set to this struct.
282 ScriptConfiguration Opt;
285 extern LinkerScript *Script;
287 } // end namespace elf
288 } // end namespace lld
290 #endif // LLD_ELF_LINKER_SCRIPT_H