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 template <class ELFT> class OutputSectionBase;
25 
26 // This class represents each rule in SECTIONS command.
27 class SectionRule {
28 public:
29   SectionRule(StringRef D, StringRef S, bool Keep)
30       : Dest(D), Keep(Keep), SectionPattern(S) {}
31 
32   // Returns true if S should be in Dest section.
33   template <class ELFT> bool match(InputSectionBase<ELFT> *S);
34 
35   StringRef Dest;
36 
37   // KEEP command saves unused sections even if --gc-sections is specified.
38   bool Keep = false;
39 
40 private:
41   StringRef SectionPattern;
42 };
43 
44 // This enum represents what we can observe in SECTIONS tag of script:
45 // Expr is a location counter change, like ". = . + 0x1000"
46 // Section is a description of output section, like ".data :..."
47 enum SectionsCommandKind { ExprKind, SectionKind };
48 
49 struct SectionsCommand {
50   SectionsCommandKind Kind;
51   std::vector<StringRef> Expr;
52   StringRef SectionName;
53 };
54 
55 // This is a runner of the linker script.
56 class LinkerScript {
57   friend class ScriptParser;
58 
59 public:
60   // Parses a linker script. Calling this function may update
61   // this object and Config.
62   void read(MemoryBufferRef MB);
63 
64   template <class ELFT> StringRef getOutputSection(InputSectionBase<ELFT> *S);
65   ArrayRef<uint8_t> getFiller(StringRef Name);
66   template <class ELFT> bool isDiscarded(InputSectionBase<ELFT> *S);
67   template <class ELFT> bool shouldKeep(InputSectionBase<ELFT> *S);
68   template <class ELFT>
69   void assignAddresses(std::vector<OutputSectionBase<ELFT> *> &S);
70   int compareSections(StringRef A, StringRef B);
71 
72   bool DoLayout = false;
73 
74 private:
75   uint64_t evaluate(std::vector<StringRef> &Tokens, uint64_t LocCounter);
76   template <class ELFT> SectionRule *find(InputSectionBase<ELFT> *S);
77 
78   // SECTIONS commands.
79   std::vector<SectionRule> Sections;
80 
81   // Output sections are sorted by this order.
82   std::vector<StringRef> SectionOrder;
83 
84   // Section fill attribute for each section.
85   llvm::StringMap<std::vector<uint8_t>> Filler;
86 
87   // Used to assign addresses to sections.
88   std::vector<SectionsCommand> Commands;
89 
90   llvm::BumpPtrAllocator Alloc;
91 };
92 
93 extern LinkerScript *Script;
94 
95 } // namespace elf
96 } // namespace lld
97 
98 #endif
99