1 //===- LinkerScript.h -------------------------------------------*- C++ -*-===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLD_ELF_LINKER_SCRIPT_H
11 #define LLD_ELF_LINKER_SCRIPT_H
12 
13 #include "Config.h"
14 #include "Strings.h"
15 #include "Writer.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"
21 #include <cstddef>
22 #include <cstdint>
23 #include <functional>
24 #include <memory>
25 #include <vector>
26 
27 namespace lld {
28 namespace elf {
29 
30 class DefinedCommon;
31 class ScriptParser;
32 class SymbolBody;
33 template <class ELFT> class InputSectionBase;
34 template <class ELFT> class InputSection;
35 class OutputSectionBase;
36 template <class ELFT> class OutputSectionFactory;
37 class InputSectionData;
38 
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 ".".
43 struct Expr {
44   std::function<uint64_t(uint64_t)> Val;
45   std::function<bool()> IsAbsolute;
46 
47   // If expression is section-relative the function below is used
48   // to get the output section pointer.
49   std::function<const OutputSectionBase *()> Section;
50 
51   uint64_t operator()(uint64_t Dot) const { return Val(Dot); }
52   operator bool() const { return (bool)Val; }
53 
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) {}
57   template <typename T>
58   Expr(T V) : Expr(V, [] { return true; }, [] { return nullptr; }) {}
59   Expr() : Expr(nullptr) {}
60 };
61 
62 // Parses a linker script. Calling this function updates
63 // Config and ScriptConfig.
64 void readLinkerScript(MemoryBufferRef MB);
65 
66 // Parses a version script.
67 void readVersionScript(MemoryBufferRef MB);
68 
69 void readDynamicList(MemoryBufferRef MB);
70 
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
75   OutputSectionKind,
76   InputSectionKind,
77   AssertKind,   // ASSERT(expr)
78   BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
79 };
80 
81 struct BaseCommand {
82   BaseCommand(int K) : Kind(K) {}
83 
84   virtual ~BaseCommand() = default;
85 
86   int Kind;
87 };
88 
89 // This represents ". = <expr>" or "<symbol> = <expr>".
90 struct SymbolAssignment : BaseCommand {
91   SymbolAssignment(StringRef Name, Expr E)
92       : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
93 
94   static bool classof(const BaseCommand *C);
95 
96   // The LHS of an expression. Name is either a symbol name or ".".
97   StringRef Name;
98   SymbolBody *Sym = nullptr;
99 
100   // The RHS of an expression.
101   Expr Expression;
102 
103   // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
104   bool Provide = false;
105   bool Hidden = false;
106 };
107 
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 };
113 
114 struct OutputSectionCommand : BaseCommand {
115   OutputSectionCommand(StringRef Name)
116       : BaseCommand(OutputSectionKind), Name(Name) {}
117 
118   static bool classof(const BaseCommand *C);
119 
120   StringRef Name;
121   Expr AddrExpr;
122   Expr AlignExpr;
123   Expr LMAExpr;
124   Expr SubalignExpr;
125   std::vector<std::unique_ptr<BaseCommand>> Commands;
126   std::vector<StringRef> Phdrs;
127   uint32_t Filler = 0;
128   ConstraintKind Constraint = ConstraintKind::NoConstraint;
129   std::string Location;
130 };
131 
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) {}
138 
139   StringMatcher ExcludedFilePat;
140   StringMatcher SectionPat;
141   SortSectionPolicy SortOuter;
142   SortSectionPolicy SortInner;
143 };
144 
145 struct InputSectionDescription : BaseCommand {
146   InputSectionDescription(StringRef FilePattern)
147       : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
148 
149   static bool classof(const BaseCommand *C);
150 
151   StringMatcher FilePat;
152 
153   // Input sections that matches at least one of SectionPatterns
154   // will be associated with this InputSectionDescription.
155   std::vector<SectionPattern> SectionPatterns;
156 
157   std::vector<InputSectionData *> Sections;
158 };
159 
160 // Represents an ASSERT().
161 struct AssertCommand : BaseCommand {
162   AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
163 
164   static bool classof(const BaseCommand *C);
165 
166   Expr Expression;
167 };
168 
169 // Represents BYTE(), SHORT(), LONG(), or QUAD().
170 struct BytesDataCommand : BaseCommand {
171   BytesDataCommand(Expr E, unsigned Size)
172       : BaseCommand(BytesDataKind), Expression(E), Size(Size) {}
173 
174   static bool classof(const BaseCommand *C);
175 
176   Expr Expression;
177   unsigned Offset;
178   unsigned Size;
179 };
180 
181 struct PhdrsCommand {
182   StringRef Name;
183   unsigned Type;
184   bool HasFilehdr;
185   bool HasPhdrs;
186   unsigned Flags;
187   Expr LMAExpr;
188 };
189 
190 class LinkerScriptBase {
191 protected:
192   ~LinkerScriptBase() = default;
193 
194 public:
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,
201                                                     StringRef S) = 0;
202   virtual uint64_t getOutputSectionSize(StringRef S) = 0;
203 };
204 
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;
209 
210   // Used to assign sections to headers.
211   std::vector<PhdrsCommand> PhdrsCommands;
212 
213   bool HasSections = false;
214 
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;
218 };
219 
220 extern ScriptConfiguration *ScriptConfig;
221 
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;
225 
226 public:
227   LinkerScript();
228   ~LinkerScript();
229 
230   void processCommands(OutputSectionFactory<ELFT> &Factory);
231   void addOrphanSections(OutputSectionFactory<ELFT> &Factory);
232   void removeEmptyCommands();
233   void adjustSectionsBeforeSorting();
234   void adjustSectionsAfterSorting();
235 
236   std::vector<PhdrEntry> createPhdrs();
237   bool ignoreInterpSection();
238 
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;
255 
256   std::vector<OutputSectionBase *> *OutputSections;
257 
258   int getSectionIndex(StringRef Name);
259 
260 private:
261   void computeInputSections(InputSectionDescription *);
262 
263   void addSection(OutputSectionFactory<ELFT> &Factory,
264                   InputSectionBase<ELFT> *Sec, StringRef Name);
265   void discard(ArrayRef<InputSectionBase<ELFT> *> V);
266 
267   std::vector<InputSectionBase<ELFT> *>
268   createInputSectionList(OutputSectionCommand &Cmd);
269 
270   // "ScriptConfig" is a bit too long, so define a short name for it.
271   ScriptConfiguration &Opt = *ScriptConfig;
272 
273   std::vector<size_t> getPhdrIndices(StringRef SectionName);
274   size_t getPhdrIndex(const Twine &Loc, StringRef PhdrName);
275 
276   uintX_t Dot;
277   uintX_t LMAOffset = 0;
278   OutputSectionBase *CurOutSec = nullptr;
279   uintX_t ThreadBssOffset = 0;
280   void switchTo(OutputSectionBase *Sec);
281   void flush();
282   void output(InputSection<ELFT> *Sec);
283   void process(BaseCommand &Base);
284   llvm::DenseSet<OutputSectionBase *> AlreadyOutputOS;
285   llvm::DenseSet<InputSectionData *> AlreadyOutputIS;
286 };
287 
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;
292 
293 extern LinkerScriptBase *ScriptBase;
294 
295 } // end namespace elf
296 } // end namespace lld
297 
298 #endif // LLD_ELF_LINKER_SCRIPT_H
299