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 class ScriptParser;
23 template <class ELFT> class InputSectionBase;
24 
25 // This class represents each rule in SECTIONS command.
26 class SectionRule {
27 public:
28   SectionRule(StringRef D, StringRef S, bool Keep)
29       : Dest(D), Keep(Keep), SectionPattern(S) {}
30 
31   // Returns true if S should be in Dest section.
32   template <class ELFT> bool match(InputSectionBase<ELFT> *S);
33 
34   StringRef Dest;
35 
36   // KEEP command saves unused sections even if --gc-sections is specified.
37   bool Keep = false;
38 
39 private:
40   StringRef SectionPattern;
41 };
42 
43 // This is a runner of the linker script.
44 class LinkerScript {
45   friend class ScriptParser;
46 
47 public:
48   // Parses a linker script. Calling this function may update
49   // this object and Config.
50   void read(MemoryBufferRef MB);
51 
52   template <class ELFT> StringRef getOutputSection(InputSectionBase<ELFT> *S);
53   ArrayRef<uint8_t> getFiller(StringRef Name);
54   template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S);
55   template <class ELFT> bool shouldKeep(InputSectionBase<ELFT> *S);
56   int compareSections(StringRef A, StringRef B);
57 
58 private:
59   template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S);
60 
61   // SECTIONS commands.
62   std::vector<SectionRule> Sections;
63 
64   // Output sections are sorted by this order.
65   std::vector<StringRef> SectionOrder;
66 
67   // Section fill attribute for each section.
68   llvm::StringMap<std::vector<uint8_t>> Filler;
69 
70   llvm::BumpPtrAllocator Alloc;
71 };
72 
73 extern LinkerScript *Script;
74 
75 } // namespace elf
76 } // namespace lld
77 
78 #endif
79