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