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/DenseSet.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/MemoryBuffer.h"
33 template <class ELFT> class InputSectionBase;
34 template <class ELFT> class InputSection;
35 class OutputSectionBase;
36 template <class ELFT> class OutputSectionFactory;
37 class InputSectionData;
39 // This represents an expression in the linker script.
40 // ScriptParser::readExpr reads an expression and returns an Expr.
41 // Later, we evaluate the expression by calling the function
42 // with the value of special context variable ".".
44 std::function<uint64_t(uint64_t)> Val;
45 std::function<bool()> IsAbsolute;
47 // If expression is section-relative the function below is used
48 // to get the output section pointer.
49 std::function<const OutputSectionBase *()> Section;
51 uint64_t operator()(uint64_t Dot) const { return Val(Dot); }
52 operator bool() const { return (bool)Val; }
54 Expr(std::function<uint64_t(uint64_t)> Val, std::function<bool()> IsAbsolute,
55 std::function<const OutputSectionBase *()> Section)
56 : Val(Val), IsAbsolute(IsAbsolute), Section(Section) {}
58 Expr(T V) : Expr(V, [] { return true; }, [] { return nullptr; }) {}
59 Expr() : Expr(nullptr) {}
62 // Parses a linker script. Calling this function updates
63 // Config and ScriptConfig.
64 void readLinkerScript(MemoryBufferRef MB);
66 // Parses a version script.
67 void readVersionScript(MemoryBufferRef MB);
69 void readDynamicList(MemoryBufferRef MB);
71 // This enum is used to implement linker script SECTIONS command.
72 // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
73 enum SectionsCommandKind {
74 AssignmentKind, // . = expr or <sym> = expr
77 AssertKind, // ASSERT(expr)
78 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
82 BaseCommand(int K) : Kind(K) {}
84 virtual ~BaseCommand() = default;
89 // This represents ". = <expr>" or "<symbol> = <expr>".
90 struct SymbolAssignment : BaseCommand {
91 SymbolAssignment(StringRef Name, Expr E)
92 : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
94 static bool classof(const BaseCommand *C);
96 // The LHS of an expression. Name is either a symbol name or ".".
98 SymbolBody *Sym = nullptr;
100 // The RHS of an expression.
103 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
104 bool Provide = false;
108 // Linker scripts allow additional constraints to be put on ouput sections.
109 // If an output section is marked as ONLY_IF_RO, the section is created
110 // only if its input sections are read-only. Likewise, an output section
111 // with ONLY_IF_RW is created if all input sections are RW.
112 enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
114 struct OutputSectionCommand : BaseCommand {
115 OutputSectionCommand(StringRef Name)
116 : BaseCommand(OutputSectionKind), Name(Name) {}
118 static bool classof(const BaseCommand *C);
125 std::vector<std::unique_ptr<BaseCommand>> Commands;
126 std::vector<StringRef> Phdrs;
128 ConstraintKind Constraint = ConstraintKind::NoConstraint;
129 std::string Location;
132 // This struct represents one section match pattern in SECTIONS() command.
133 // It can optionally have negative match pattern for EXCLUDED_FILE command.
134 // Also it may be surrounded with SORT() command, so contains sorting rules.
135 struct SectionPattern {
136 SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
137 : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
139 StringMatcher ExcludedFilePat;
140 StringMatcher SectionPat;
141 SortSectionPolicy SortOuter;
142 SortSectionPolicy SortInner;
145 struct InputSectionDescription : BaseCommand {
146 InputSectionDescription(StringRef FilePattern)
147 : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
149 static bool classof(const BaseCommand *C);
151 StringMatcher FilePat;
153 // Input sections that matches at least one of SectionPatterns
154 // will be associated with this InputSectionDescription.
155 std::vector<SectionPattern> SectionPatterns;
157 std::vector<InputSectionData *> Sections;
160 // Represents an ASSERT().
161 struct AssertCommand : BaseCommand {
162 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
164 static bool classof(const BaseCommand *C);
169 // Represents BYTE(), SHORT(), LONG(), or QUAD().
170 struct BytesDataCommand : BaseCommand {
171 BytesDataCommand(Expr E, unsigned Size)
172 : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
174 static bool classof(const BaseCommand *C);
181 struct PhdrsCommand {
190 class LinkerScriptBase {
192 ~LinkerScriptBase() = default;
195 virtual uint64_t getHeaderSize() = 0;
196 virtual uint64_t getSymbolValue(const Twine &Loc, StringRef S) = 0;
197 virtual bool isDefined(StringRef S) = 0;
198 virtual bool isAbsolute(StringRef S) = 0;
199 virtual const OutputSectionBase *getSymbolSection(StringRef S) = 0;
200 virtual const OutputSectionBase *getOutputSection(const Twine &Loc,
202 virtual uint64_t getOutputSectionSize(StringRef S) = 0;
205 // ScriptConfiguration holds linker script parse results.
206 struct ScriptConfiguration {
207 // Used to assign addresses to sections.
208 std::vector<std::unique_ptr<BaseCommand>> Commands;
210 // Used to assign sections to headers.
211 std::vector<PhdrsCommand> PhdrsCommands;
213 bool HasSections = false;
215 // List of section patterns specified with KEEP commands. They will
216 // be kept even if they are unused and --gc-sections is specified.
217 std::vector<InputSectionDescription *> KeptSections;
220 extern ScriptConfiguration *ScriptConfig;
222 // This is a runner of the linker script.
223 template <class ELFT> class LinkerScript final : public LinkerScriptBase {
224 typedef typename ELFT::uint uintX_t;
230 void processCommands(OutputSectionFactory<ELFT> &Factory);
231 void addOrphanSections(OutputSectionFactory<ELFT> &Factory);
232 void removeEmptyCommands();
233 void adjustSectionsBeforeSorting();
234 void adjustSectionsAfterSorting();
236 std::vector<PhdrEntry> createPhdrs();
237 bool ignoreInterpSection();
239 uint32_t getFiller(StringRef Name);
240 void writeDataBytes(StringRef Name, uint8_t *Buf);
241 bool hasLMA(StringRef Name);
242 bool shouldKeep(InputSectionBase<ELFT> *S);
243 void assignOffsets(OutputSectionCommand *Cmd);
244 void placeOrphanSections();
245 void assignAddresses(std::vector<PhdrEntry> &Phdrs);
246 bool hasPhdrsCommands();
247 uint64_t getHeaderSize() override;
248 uint64_t getSymbolValue(const Twine &Loc, StringRef S) override;
249 bool isDefined(StringRef S) override;
250 bool isAbsolute(StringRef S) override;
251 const OutputSectionBase *getSymbolSection(StringRef S) override;
252 const OutputSectionBase *getOutputSection(const Twine &Loc,
253 StringRef S) override;
254 uint64_t getOutputSectionSize(StringRef S) override;
256 std::vector<OutputSectionBase *> *OutputSections;
258 int getSectionIndex(StringRef Name);
261 void computeInputSections(InputSectionDescription *);
263 void addSection(OutputSectionFactory<ELFT> &Factory,
264 InputSectionBase<ELFT> *Sec, StringRef Name);
265 void discard(ArrayRef<InputSectionBase<ELFT> *> V);
267 std::vector<InputSectionBase<ELFT> *>
268 createInputSectionList(OutputSectionCommand &Cmd);
270 // "ScriptConfig" is a bit too long, so define a short name for it.
271 ScriptConfiguration &Opt = *ScriptConfig;
273 std::vector<size_t> getPhdrIndices(StringRef SectionName);
274 size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
277 uintX_t LMAOffset = 0;
278 OutputSectionBase *CurOutSec = nullptr;
279 uintX_t ThreadBssOffset = 0;
280 void switchTo(OutputSectionBase *Sec);
282 void output(InputSection<ELFT> *Sec);
283 void process(BaseCommand &Base);
284 llvm::DenseSet<OutputSectionBase *> AlreadyOutputOS;
285 llvm::DenseSet<InputSectionData *> AlreadyOutputIS;
288 // Variable template is a C++14 feature, so we can't template
289 // a global variable. Use a struct to workaround.
290 template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
291 template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
293 extern LinkerScriptBase *ScriptBase;
295 } // end namespace elf
296 } // end namespace lld
298 #endif // LLD_ELF_LINKER_SCRIPT_H