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 "Writer.h"
14 #include "lld/Core/LLVM.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include <functional>
20 
21 namespace lld {
22 namespace elf {
23 class SymbolBody;
24 template <class ELFT> class InputSectionBase;
25 template <class ELFT> class OutputSectionBase;
26 template <class ELFT> class OutputSectionFactory;
27 template <class ELFT> class DefinedCommon;
28 template <class ELFT> class LayoutInputSection;
29 
30 typedef std::function<uint64_t(uint64_t)> Expr;
31 
32 // Parses a linker script. Calling this function updates
33 // Config and ScriptConfig.
34 void readLinkerScript(MemoryBufferRef MB);
35 
36 class ScriptParser;
37 template <class ELFT> class InputSectionBase;
38 template <class ELFT> class OutputSectionBase;
39 
40 // This enum is used to implement linker script SECTIONS command.
41 // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
42 enum SectionsCommandKind {
43   AssignmentKind,
44   OutputSectionKind,
45   InputSectionKind,
46   AssertKind
47 };
48 
49 struct BaseCommand {
50   BaseCommand(int K) : Kind(K) {}
51   virtual ~BaseCommand() {}
52   int Kind;
53 };
54 
55 struct SymbolAssignment : BaseCommand {
56   SymbolAssignment(StringRef Name, Expr E)
57       : BaseCommand(AssignmentKind), Name(Name), Expression(E) {}
58   static bool classof(const BaseCommand *C);
59 
60   // The LHS of an expression. Name is either a symbol name or ".".
61   StringRef Name;
62   SymbolBody *Sym = nullptr;
63 
64   // The RHS of an expression.
65   Expr Expression;
66 
67   // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
68   bool Provide = false;
69   bool Hidden = false;
70 };
71 
72 // Linker scripts allow additional constraints to be put on ouput sections.
73 // An output section will only be created if all of its input sections are
74 // read-only
75 // or all of its input sections are read-write by using the keyword ONLY_IF_RO
76 // and ONLY_IF_RW respectively.
77 enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
78 
79 struct OutputSectionCommand : BaseCommand {
80   OutputSectionCommand(StringRef Name)
81       : BaseCommand(OutputSectionKind), Name(Name) {}
82   static bool classof(const BaseCommand *C);
83   StringRef Name;
84   Expr AddrExpr;
85   Expr AlignExpr;
86   std::vector<std::unique_ptr<BaseCommand>> Commands;
87   std::vector<StringRef> Phdrs;
88   std::vector<uint8_t> Filler;
89   ConstraintKind Constraint = ConstraintKind::NoConstraint;
90 };
91 
92 enum SortKind { SortNone, SortByName, SortByAlignment };
93 
94 struct InputSectionDescription : BaseCommand {
95   InputSectionDescription() : BaseCommand(InputSectionKind) {}
96   static bool classof(const BaseCommand *C);
97   StringRef FilePattern;
98   SortKind SortOuter = SortNone;
99   SortKind SortInner = SortNone;
100   std::vector<StringRef> ExcludedFiles;
101   std::vector<StringRef> SectionPatterns;
102 };
103 
104 struct AssertCommand : BaseCommand {
105   AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
106   static bool classof(const BaseCommand *C);
107   Expr Expression;
108 };
109 
110 struct PhdrsCommand {
111   StringRef Name;
112   unsigned Type;
113   bool HasFilehdr;
114   bool HasPhdrs;
115   unsigned Flags;
116 };
117 
118 // ScriptConfiguration holds linker script parse results.
119 struct ScriptConfiguration {
120   // Used to assign addresses to sections.
121   std::vector<std::unique_ptr<BaseCommand>> Commands;
122 
123   // Used to assign sections to headers.
124   std::vector<PhdrsCommand> PhdrsCommands;
125 
126   bool HasContents = false;
127 
128   llvm::BumpPtrAllocator Alloc;
129 
130   // List of section patterns specified with KEEP commands. They will
131   // be kept even if they are unused and --gc-sections is specified.
132   std::vector<StringRef> KeptSections;
133 };
134 
135 extern ScriptConfiguration *ScriptConfig;
136 
137 // This is a runner of the linker script.
138 template <class ELFT> class LinkerScript {
139   typedef typename ELFT::uint uintX_t;
140 
141 public:
142   LinkerScript();
143   ~LinkerScript();
144   void createSections(OutputSectionFactory<ELFT> &Factory);
145 
146   std::vector<PhdrEntry<ELFT>> createPhdrs();
147 
148   ArrayRef<uint8_t> getFiller(StringRef Name);
149   bool shouldKeep(InputSectionBase<ELFT> *S);
150   void assignAddresses();
151   int compareSections(StringRef A, StringRef B);
152   bool hasPhdrsCommands();
153   uintX_t getOutputSectionSize(StringRef Name);
154   uintX_t getHeaderSize();
155 
156   std::vector<OutputSectionBase<ELFT> *> *OutputSections;
157 
158 private:
159   std::vector<InputSectionBase<ELFT> *>
160   getInputSections(const InputSectionDescription *);
161 
162   void discard(OutputSectionCommand &Cmd);
163 
164   std::vector<InputSectionBase<ELFT> *>
165   createInputSectionList(OutputSectionCommand &Cmd);
166 
167   // "ScriptConfig" is a bit too long, so define a short name for it.
168   ScriptConfiguration &Opt = *ScriptConfig;
169 
170   int getSectionIndex(StringRef Name);
171   std::vector<size_t> getPhdrIndices(StringRef SectionName);
172   size_t getPhdrIndex(StringRef PhdrName);
173 
174   llvm::SpecificBumpPtrAllocator<LayoutInputSection<ELFT>> LAlloc;
175 
176   uintX_t Dot;
177 };
178 
179 // Variable template is a C++14 feature, so we can't template
180 // a global variable. Use a struct to workaround.
181 template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
182 template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
183 
184 } // namespace elf
185 } // namespace lld
186 
187 #endif
188