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 {
117 struct OutputSectionCommand : BaseCommand {
118 OutputSectionCommand(StringRef Name)
119 : BaseCommand(OutputSectionKind), Name(Name) {}
121 static bool classof(const BaseCommand *C);
123 OutputSection *Sec = nullptr;
124 MemoryRegion *MemRegion = nullptr;
130 std::vector<BaseCommand *> Commands;
131 std::vector<StringRef> Phdrs;
132 llvm::Optional<uint32_t> Filler;
133 ConstraintKind Constraint = ConstraintKind::NoConstraint;
134 std::string Location;
135 std::string MemoryRegionName;
138 template <class ELFT> void finalize();
139 template <class ELFT> void writeTo(uint8_t *Buf);
140 template <class ELFT> void maybeCompress();
141 uint32_t getFiller();
143 void sort(std::function<int(InputSectionBase *S)> Order);
145 void sortCtorsDtors();
148 // This struct represents one section match pattern in SECTIONS() command.
149 // It can optionally have negative match pattern for EXCLUDED_FILE command.
150 // Also it may be surrounded with SORT() command, so contains sorting rules.
151 struct SectionPattern {
152 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
153 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
155 StringMatcher ExcludedFilePat;
156 StringMatcher SectionPat;
157 SortSectionPolicy SortOuter;
158 SortSectionPolicy SortInner;
161 struct InputSectionDescription : BaseCommand {
162 InputSectionDescription(StringRef FilePattern)
163 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
165 static bool classof(const BaseCommand *C);
167 StringMatcher FilePat;
169 // Input sections that matches at least one of SectionPatterns
170 // will be associated with this InputSectionDescription.
171 std::vector<SectionPattern> SectionPatterns;
173 std::vector<InputSection *> Sections;
176 // Represents an ASSERT().
177 struct AssertCommand : BaseCommand {
178 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
180 static bool classof(const BaseCommand *C);
185 // Represents BYTE(), SHORT(), LONG(), or QUAD().
186 struct BytesDataCommand : BaseCommand {
187 BytesDataCommand(Expr E, unsigned Size)
188 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
190 static bool classof(const BaseCommand *C);
197 struct PhdrsCommand {
206 // ScriptConfiguration holds linker script parse results.
207 struct ScriptConfiguration {
208 // Used to assign addresses to sections.
209 std::vector<BaseCommand *> Commands;
211 // Used to assign sections to headers.
212 std::vector<PhdrsCommand> PhdrsCommands;
214 bool HasSections = false;
216 // List of section patterns specified with KEEP commands. They will
217 // be kept even if they are unused and --gc-sections is specified.
218 std::vector<InputSectionDescription *> KeptSections;
220 // A map from memory region name to a memory region descriptor.
221 llvm::DenseMap<llvm::StringRef, MemoryRegion> MemoryRegions;
223 // A list of symbols referenced by the script.
224 std::vector<llvm::StringRef> ReferencedSymbols;
227 class LinkerScript final {
228 // Temporary state used in processCommands() and assignAddresses()
229 // that must be reinitialized for each call to the above functions, and must
230 // not be used outside of the scope of a call to the above functions.
231 struct AddressState {
232 uint64_t ThreadBssOffset = 0;
233 OutputSection *OutSec = nullptr;
234 MemoryRegion *MemRegion = nullptr;
235 llvm::DenseMap<const MemoryRegion *, uint64_t> MemRegionOffset;
236 std::function<uint64_t()> LMAOffset;
237 AddressState(const ScriptConfiguration &Opt);
239 llvm::DenseMap<OutputSection *, OutputSectionCommand *> SecToCommand;
240 llvm::DenseMap<StringRef, OutputSectionCommand *> NameToOutputSectionCommand;
242 void assignSymbol(SymbolAssignment *Cmd, bool InSec);
243 void setDot(Expr E, const Twine &Loc, bool InSec);
245 std::vector<InputSection *>
246 computeInputSections(const InputSectionDescription *);
248 std::vector<InputSectionBase *>
249 createInputSectionList(OutputSectionCommand &Cmd);
251 std::vector<size_t> getPhdrIndices(OutputSectionCommand *Cmd);
252 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
254 MemoryRegion *findMemoryRegion(OutputSectionCommand *Cmd);
256 void switchTo(OutputSection *Sec);
257 uint64_t advance(uint64_t Size, unsigned Align);
258 void output(InputSection *Sec);
259 void process(BaseCommand &Base);
261 AddressState *CurAddressState = nullptr;
262 OutputSection *Aether;
267 bool ErrorOnMissingSection = false;
268 OutputSectionCommand *createOutputSectionCommand(StringRef Name,
270 OutputSectionCommand *getOrCreateOutputSectionCommand(StringRef Name);
272 OutputSectionCommand *getCmd(OutputSection *Sec) const;
273 bool hasPhdrsCommands() { return !Opt.PhdrsCommands.empty(); }
274 uint64_t getDot() { return Dot; }
275 void discard(ArrayRef<InputSectionBase *> V);
277 ExprValue getSymbolValue(const Twine &Loc, StringRef S);
278 bool isDefined(StringRef S);
280 void fabricateDefaultCommands();
281 void addOrphanSections(OutputSectionFactory &Factory);
282 void removeEmptyCommands();
283 void adjustSectionsBeforeSorting();
284 void adjustSectionsAfterSorting();
286 std::vector<PhdrEntry> createPhdrs();
287 bool ignoreInterpSection();
289 bool shouldKeep(InputSectionBase *S);
290 void assignOffsets(OutputSectionCommand *Cmd);
291 void processNonSectionCommands();
292 void assignAddresses();
293 void allocateHeaders(std::vector<PhdrEntry> &Phdrs);
294 void addSymbol(SymbolAssignment *Cmd);
295 void processCommands(OutputSectionFactory &Factory);
297 // Parsed linker script configurations are set to this struct.
298 ScriptConfiguration Opt;
301 extern LinkerScript *Script;
303 } // end namespace elf
304 } // end namespace lld
306 #endif // LLD_ELF_LINKER_SCRIPT_H