LinkerScript.h revision 309124
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
19namespace lld {
20namespace elf {
21
22// Parses a linker script. Calling this function updates
23// Config and ScriptConfig.
24void readLinkerScript(MemoryBufferRef MB);
25
26class ScriptParser;
27template <class ELFT> class InputSectionBase;
28template <class ELFT> class OutputSectionBase;
29
30// This class represents each rule in SECTIONS command.
31struct 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 :..."
43enum SectionsCommandKind { SectionKind, AssignmentKind };
44
45struct SectionsCommand {
46  SectionsCommandKind Kind;
47  std::vector<StringRef> Expr;
48  StringRef Name;
49};
50
51// ScriptConfiguration holds linker script parse results.
52struct 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
71extern ScriptConfiguration *ScriptConfig;
72
73// This is a runner of the linker script.
74template <class ELFT> class LinkerScript {
75  typedef typename ELFT::uint uintX_t;
76
77public:
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();
85
86private:
87  // "ScriptConfig" is a bit too long, so define a short name for it.
88  ScriptConfiguration &Opt = *ScriptConfig;
89
90  int getSectionIndex(StringRef Name);
91
92  uintX_t Dot;
93};
94
95// Variable template is a C++14 feature, so we can't template
96// a global variable. Use a struct to workaround.
97template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
98template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
99
100} // namespace elf
101} // namespace lld
102
103#endif
104