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/DenseMap.h" 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/ADT/MapVector.h" 20 #include "llvm/Support/Allocator.h" 21 #include "llvm/Support/MemoryBuffer.h" 22 #include "llvm/Support/Regex.h" 23 #include <functional> 24 25 namespace lld { 26 namespace elf { 27 class DefinedCommon; 28 class ScriptParser; 29 class SymbolBody; 30 template <class ELFT> class InputSectionBase; 31 template <class ELFT> class InputSection; 32 template <class ELFT> class OutputSectionBase; 33 template <class ELFT> class OutputSectionFactory; 34 class InputSectionData; 35 36 typedef std::function<uint64_t(uint64_t)> Expr; 37 38 // Parses a linker script. Calling this function updates 39 // Config and ScriptConfig. 40 void readLinkerScript(MemoryBufferRef MB); 41 42 void readVersionScript(MemoryBufferRef MB); 43 44 // This enum is used to implement linker script SECTIONS command. 45 // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS 46 enum SectionsCommandKind { 47 AssignmentKind, // . = expr or <sym> = expr 48 OutputSectionKind, 49 InputSectionKind, 50 AssertKind, // ASSERT(expr) 51 BytesDataKind // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr) 52 }; 53 54 struct BaseCommand { 55 BaseCommand(int K) : Kind(K) {} 56 virtual ~BaseCommand() {} 57 int Kind; 58 }; 59 60 struct SymbolAssignment : BaseCommand { 61 SymbolAssignment(StringRef Name, Expr E, bool IsAbsolute) 62 : BaseCommand(AssignmentKind), Name(Name), Expression(E), 63 IsAbsolute(IsAbsolute) {} 64 static bool classof(const BaseCommand *C); 65 66 // The LHS of an expression. Name is either a symbol name or ".". 67 StringRef Name; 68 SymbolBody *Sym = nullptr; 69 70 // The RHS of an expression. 71 Expr Expression; 72 73 // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN. 74 bool Provide = false; 75 bool Hidden = false; 76 bool IsAbsolute; 77 }; 78 79 // Linker scripts allow additional constraints to be put on ouput sections. 80 // An output section will only be created if all of its input sections are 81 // read-only 82 // or all of its input sections are read-write by using the keyword ONLY_IF_RO 83 // and ONLY_IF_RW respectively. 84 enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite }; 85 86 struct OutputSectionCommand : BaseCommand { 87 OutputSectionCommand(StringRef Name) 88 : BaseCommand(OutputSectionKind), Name(Name) {} 89 static bool classof(const BaseCommand *C); 90 StringRef Name; 91 Expr AddrExpr; 92 Expr AlignExpr; 93 Expr LMAExpr; 94 Expr SubalignExpr; 95 std::vector<std::unique_ptr<BaseCommand>> Commands; 96 std::vector<StringRef> Phdrs; 97 std::vector<uint8_t> Filler; 98 ConstraintKind Constraint = ConstraintKind::NoConstraint; 99 }; 100 101 // This struct represents one section match pattern in SECTIONS() command. 102 // It can optionally have negative match pattern for EXCLUDED_FILE command. 103 // Also it may be surrounded with SORT() command, so contains sorting rules. 104 struct SectionPattern { 105 SectionPattern(llvm::Regex &&Re1, llvm::Regex &&Re2) 106 : ExcludedFileRe(std::forward<llvm::Regex>(Re1)), 107 SectionRe(std::forward<llvm::Regex>(Re2)) {} 108 109 SectionPattern(SectionPattern &&Other) { 110 std::swap(ExcludedFileRe, Other.ExcludedFileRe); 111 std::swap(SectionRe, Other.SectionRe); 112 std::swap(SortOuter, Other.SortOuter); 113 std::swap(SortInner, Other.SortInner); 114 } 115 116 llvm::Regex ExcludedFileRe; 117 llvm::Regex SectionRe; 118 SortSectionPolicy SortOuter; 119 SortSectionPolicy SortInner; 120 }; 121 122 struct InputSectionDescription : BaseCommand { 123 InputSectionDescription(StringRef FilePattern) 124 : BaseCommand(InputSectionKind), 125 FileRe(compileGlobPatterns({FilePattern})) {} 126 static bool classof(const BaseCommand *C); 127 llvm::Regex FileRe; 128 129 // Input sections that matches at least one of SectionPatterns 130 // will be associated with this InputSectionDescription. 131 std::vector<SectionPattern> SectionPatterns; 132 133 std::vector<InputSectionData *> Sections; 134 }; 135 136 struct AssertCommand : BaseCommand { 137 AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {} 138 static bool classof(const BaseCommand *C); 139 Expr Expression; 140 }; 141 142 struct BytesDataCommand : BaseCommand { 143 BytesDataCommand(uint64_t Data, unsigned Size) 144 : BaseCommand(BytesDataKind), Data(Data), Size(Size) {} 145 static bool classof(const BaseCommand *C); 146 uint64_t Data; 147 unsigned Offset; 148 unsigned Size; 149 }; 150 151 struct PhdrsCommand { 152 StringRef Name; 153 unsigned Type; 154 bool HasFilehdr; 155 bool HasPhdrs; 156 unsigned Flags; 157 Expr LMAExpr; 158 }; 159 160 class LinkerScriptBase { 161 protected: 162 ~LinkerScriptBase() = default; 163 164 public: 165 virtual uint64_t getOutputSectionAddress(StringRef Name) = 0; 166 virtual uint64_t getOutputSectionSize(StringRef Name) = 0; 167 virtual uint64_t getOutputSectionAlign(StringRef Name) = 0; 168 virtual uint64_t getOutputSectionLMA(StringRef Name) = 0; 169 virtual uint64_t getHeaderSize() = 0; 170 virtual uint64_t getSymbolValue(StringRef S) = 0; 171 virtual bool isDefined(StringRef S) = 0; 172 }; 173 174 // ScriptConfiguration holds linker script parse results. 175 struct ScriptConfiguration { 176 // Used to assign addresses to sections. 177 std::vector<std::unique_ptr<BaseCommand>> Commands; 178 179 // Used to assign sections to headers. 180 std::vector<PhdrsCommand> PhdrsCommands; 181 182 bool HasSections = false; 183 184 llvm::BumpPtrAllocator Alloc; 185 186 // List of section patterns specified with KEEP commands. They will 187 // be kept even if they are unused and --gc-sections is specified. 188 std::vector<InputSectionDescription *> KeptSections; 189 }; 190 191 extern ScriptConfiguration *ScriptConfig; 192 193 // This is a runner of the linker script. 194 template <class ELFT> class LinkerScript final : public LinkerScriptBase { 195 typedef typename ELFT::uint uintX_t; 196 197 public: 198 LinkerScript(); 199 ~LinkerScript(); 200 void processCommands(OutputSectionFactory<ELFT> &Factory); 201 void createSections(OutputSectionFactory<ELFT> &Factory); 202 void adjustSectionsBeforeSorting(); 203 204 std::vector<PhdrEntry<ELFT>> createPhdrs(); 205 bool ignoreInterpSection(); 206 207 ArrayRef<uint8_t> getFiller(StringRef Name); 208 void writeDataBytes(StringRef Name, uint8_t *Buf); 209 bool hasLMA(StringRef Name); 210 bool shouldKeep(InputSectionBase<ELFT> *S); 211 void assignOffsets(OutputSectionCommand *Cmd); 212 void assignAddresses(std::vector<PhdrEntry<ELFT>> &Phdrs); 213 bool hasPhdrsCommands(); 214 uint64_t getOutputSectionAddress(StringRef Name) override; 215 uint64_t getOutputSectionSize(StringRef Name) override; 216 uint64_t getOutputSectionAlign(StringRef Name) override; 217 uint64_t getOutputSectionLMA(StringRef Name) override; 218 uint64_t getHeaderSize() override; 219 uint64_t getSymbolValue(StringRef S) override; 220 bool isDefined(StringRef S) override; 221 222 std::vector<OutputSectionBase<ELFT> *> *OutputSections; 223 224 int getSectionIndex(StringRef Name); 225 226 private: 227 void computeInputSections(InputSectionDescription *); 228 229 void addSection(OutputSectionFactory<ELFT> &Factory, 230 InputSectionBase<ELFT> *Sec, StringRef Name); 231 void discard(ArrayRef<InputSectionBase<ELFT> *> V); 232 233 std::vector<InputSectionBase<ELFT> *> 234 createInputSectionList(OutputSectionCommand &Cmd); 235 236 // "ScriptConfig" is a bit too long, so define a short name for it. 237 ScriptConfiguration &Opt = *ScriptConfig; 238 239 std::vector<size_t> getPhdrIndices(StringRef SectionName); 240 size_t getPhdrIndex(StringRef PhdrName); 241 242 uintX_t Dot; 243 uintX_t LMAOffset = 0; 244 OutputSectionBase<ELFT> *CurOutSec = nullptr; 245 uintX_t ThreadBssOffset = 0; 246 void switchTo(OutputSectionBase<ELFT> *Sec); 247 void flush(); 248 void output(InputSection<ELFT> *Sec); 249 void process(BaseCommand &Base); 250 llvm::DenseSet<OutputSectionBase<ELFT> *> AlreadyOutputOS; 251 llvm::DenseSet<InputSectionData *> AlreadyOutputIS; 252 }; 253 254 // Variable template is a C++14 feature, so we can't template 255 // a global variable. Use a struct to workaround. 256 template <class ELFT> struct Script { static LinkerScript<ELFT> *X; }; 257 template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X; 258 259 extern LinkerScriptBase *ScriptBase; 260 261 } // namespace elf 262 } // namespace lld 263 264 #endif 265