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 "lld/Core/LLVM.h" 14 #include "llvm/ADT/DenseMap.h" 15 #include "llvm/ADT/MapVector.h" 16 #include "llvm/Support/Allocator.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 19 namespace lld { 20 namespace elf { 21 22 // Parses a linker script. Calling this function updates 23 // Config and ScriptConfig. 24 void readLinkerScript(MemoryBufferRef MB); 25 26 class ScriptParser; 27 template <class ELFT> class InputSectionBase; 28 template <class ELFT> class OutputSectionBase; 29 30 // This class represents each rule in SECTIONS command. 31 struct SectionRule { 32 SectionRule(StringRef D, StringRef S) 33 : Dest(D), SectionPattern(S) {} 34 35 // Returns true if S should be in Dest section. 36 template <class ELFT> bool match(InputSectionBase<ELFT> *S); 37 38 StringRef Dest; 39 40 StringRef SectionPattern; 41 }; 42 43 // This enum represents what we can observe in SECTIONS tag of script: 44 // ExprKind is a location counter change, like ". = . + 0x1000" 45 // SectionKind is a description of output section, like ".data :..." 46 enum SectionsCommandKind { ExprKind, SectionKind }; 47 48 struct SectionsCommand { 49 SectionsCommandKind Kind; 50 std::vector<StringRef> Expr; 51 StringRef SectionName; 52 }; 53 54 // ScriptConfiguration holds linker script parse results. 55 struct ScriptConfiguration { 56 // SECTIONS commands. 57 std::vector<SectionRule> Sections; 58 59 // Section fill attribute for each section. 60 llvm::StringMap<std::vector<uint8_t>> Filler; 61 62 // Used to assign addresses to sections. 63 std::vector<SectionsCommand> Commands; 64 65 bool DoLayout = false; 66 67 llvm::BumpPtrAllocator Alloc; 68 69 // List of section patterns specified with KEEP commands. They will 70 // be kept even if they are unused and --gc-sections is specified. 71 std::vector<StringRef> KeptSections; 72 }; 73 74 extern ScriptConfiguration *ScriptConfig; 75 76 // This is a runner of the linker script. 77 template <class ELFT> class LinkerScript { 78 typedef typename ELFT::uint uintX_t; 79 80 public: 81 StringRef getOutputSection(InputSectionBase<ELFT> *S); 82 ArrayRef<uint8_t> getFiller(StringRef Name); 83 bool isDiscarded(InputSectionBase<ELFT> *S); 84 bool shouldKeep(InputSectionBase<ELFT> *S); 85 void assignAddresses(ArrayRef<OutputSectionBase<ELFT> *> S); 86 int compareSections(StringRef A, StringRef B); 87 88 private: 89 // "ScriptConfig" is a bit too long, so define a short name for it. 90 ScriptConfiguration &Opt = *ScriptConfig; 91 92 int getSectionIndex(StringRef Name); 93 94 uintX_t Dot; 95 }; 96 97 // Variable template is a C++14 feature, so we can't template 98 // a global variable. Use a struct to workaround. 99 template <class ELFT> struct Script { static LinkerScript<ELFT> *X; }; 100 template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X; 101 102 } // namespace elf 103 } // namespace lld 104 105 #endif 106