1 //===- LinkerScript.h -------------------------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_ELF_LINKER_SCRIPT_H
11 #define LLD_ELF_LINKER_SCRIPT_H
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"
22 // Parses a linker script. Calling this function updates
23 // Config and ScriptConfig.
24 void readLinkerScript(MemoryBufferRef MB);
27 template <class ELFT> class InputSectionBase;
28 template <class ELFT> class OutputSectionBase;
30 // This class represents each rule in SECTIONS command.
32 SectionRule(StringRef D, StringRef S)
33 : Dest(D), SectionPattern(S) {}
37 StringRef SectionPattern;
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 { SectionKind, AssignmentKind };
45 struct SectionsCommand {
46 SectionsCommandKind Kind;
47 std::vector<StringRef> Expr;
51 // ScriptConfiguration holds linker script parse results.
52 struct ScriptConfiguration {
54 std::vector<SectionRule> Sections;
56 // Section fill attribute for each section.
57 llvm::StringMap<std::vector<uint8_t>> Filler;
59 // Used to assign addresses to sections.
60 std::vector<SectionsCommand> Commands;
62 bool DoLayout = false;
64 llvm::BumpPtrAllocator Alloc;
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;
71 extern ScriptConfiguration *ScriptConfig;
73 // This is a runner of the linker script.
74 template <class ELFT> class LinkerScript {
75 typedef typename ELFT::uint uintX_t;
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 void addScriptedSymbols();
87 // "ScriptConfig" is a bit too long, so define a short name for it.
88 ScriptConfiguration &Opt = *ScriptConfig;
90 int getSectionIndex(StringRef Name);
95 // Variable template is a C++14 feature, so we can't template
96 // a global variable. Use a struct to workaround.
97 template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
98 template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;