Coverage Report

Created: 2018-01-17 21:32

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/LinkerScript.h
Line
Count
Source
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 "Config.h"
14
#include "Strings.h"
15
#include "Writer.h"
16
#include "lld/Common/LLVM.h"
17
#include "llvm/ADT/ArrayRef.h"
18
#include "llvm/ADT/DenseMap.h"
19
#include "llvm/ADT/DenseSet.h"
20
#include "llvm/ADT/MapVector.h"
21
#include "llvm/ADT/StringRef.h"
22
#include "llvm/Support/MemoryBuffer.h"
23
#include <cstddef>
24
#include <cstdint>
25
#include <functional>
26
#include <memory>
27
#include <vector>
28
29
namespace lld {
30
namespace elf {
31
32
class Defined;
33
class Symbol;
34
class InputSectionBase;
35
class InputSection;
36
class OutputSection;
37
class InputSectionBase;
38
class SectionBase;
39
40
// This represents an r-value in the linker script.
41
struct ExprValue {
42
  ExprValue(SectionBase *Sec, bool ForceAbsolute, uint64_t Val,
43
            const Twine &Loc)
44
2.31k
      : Sec(Sec), ForceAbsolute(ForceAbsolute), Val(Val), Loc(Loc.str()) {}
45
46
2.07k
  ExprValue(uint64_t Val) : ExprValue(nullptr, false, Val, "") {}
Unexecuted instantiation: lld::elf::ExprValue::ExprValue(unsigned long long)
lld::elf::ExprValue::ExprValue(unsigned long long)
Line
Count
Source
46
2.07k
  ExprValue(uint64_t Val) : ExprValue(nullptr, false, Val, "") {}
47
48
254
  bool isAbsolute() const { return ForceAbsolute || 
Sec == nullptr244
; }
49
  uint64_t getValue() const;
50
  uint64_t getSecAddr() const;
51
  uint64_t getSectionOffset() const;
52
53
  // If a value is relative to a section, it has a non-null Sec.
54
  SectionBase *Sec;
55
56
  // True if this expression is enclosed in ABSOLUTE().
57
  // This flag affects the return value of getValue().
58
  bool ForceAbsolute;
59
60
  uint64_t Val;
61
  uint64_t Alignment = 1;
62
63
  // Original source location. Used for error messages.
64
  std::string Loc;
65
};
66
67
// This represents an expression in the linker script.
68
// ScriptParser::readExpr reads an expression and returns an Expr.
69
// Later, we evaluate the expression by calling the function.
70
typedef std::function<ExprValue()> Expr;
71
72
// This enum is used to implement linker script SECTIONS command.
73
// https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS
74
enum SectionsCommandKind {
75
  AssignmentKind, // . = expr or <sym> = expr
76
  OutputSectionKind,
77
  InputSectionKind,
78
  AssertKind, // ASSERT(expr)
79
  ByteKind    // BYTE(expr), SHORT(expr), LONG(expr) or QUAD(expr)
80
};
81
82
struct BaseCommand {
83
165k
  BaseCommand(int K) : Kind(K) {}
84
  int Kind;
85
};
86
87
// This represents ". = <expr>" or "<symbol> = <expr>".
88
struct SymbolAssignment : BaseCommand {
89
  SymbolAssignment(StringRef Name, Expr E, std::string Loc)
90
187
      : BaseCommand(AssignmentKind), Name(Name), Expression(E), Location(Loc) {}
91
92
152k
  static bool classof(const BaseCommand *C) {
93
152k
    return C->Kind == AssignmentKind;
94
152k
  }
95
96
  // The LHS of an expression. Name is either a symbol name or ".".
97
  StringRef Name;
98
  Defined *Sym = nullptr;
99
100
  // The RHS of an expression.
101
  Expr Expression;
102
103
  // Command attributes for PROVIDE, HIDDEN and PROVIDE_HIDDEN.
104
  bool Provide = false;
105
  bool Hidden = false;
106
107
  // Holds file name and line number for error reporting.
108
  std::string Location;
109
};
110
111
// Linker scripts allow additional constraints to be put on ouput sections.
112
// If an output section is marked as ONLY_IF_RO, the section is created
113
// only if its input sections are read-only. Likewise, an output section
114
// with ONLY_IF_RW is created if all input sections are RW.
115
enum class ConstraintKind { NoConstraint, ReadOnly, ReadWrite };
116
117
// This struct is used to represent the location and size of regions of
118
// target memory. Instances of the struct are created by parsing the
119
// MEMORY command.
120
struct MemoryRegion {
121
  std::string Name;
122
  uint64_t Origin;
123
  uint64_t Length;
124
  uint32_t Flags;
125
  uint32_t NegFlags;
126
};
127
128
// This struct represents one section match pattern in SECTIONS() command.
129
// It can optionally have negative match pattern for EXCLUDED_FILE command.
130
// Also it may be surrounded with SORT() command, so contains sorting rules.
131
struct SectionPattern {
132
  SectionPattern(StringMatcher &&Pat1, StringMatcher &&Pat2)
133
314
      : ExcludedFilePat(Pat1), SectionPat(Pat2) {}
134
135
  StringMatcher ExcludedFilePat;
136
  StringMatcher SectionPat;
137
  SortSectionPolicy SortOuter;
138
  SortSectionPolicy SortInner;
139
};
140
141
class ThunkSection;
142
struct InputSectionDescription : BaseCommand {
143
  InputSectionDescription(StringRef FilePattern)
144
81.2k
      : BaseCommand(InputSectionKind), FilePat(FilePattern) {}
145
146
161k
  static bool classof(const BaseCommand *C) {
147
161k
    return C->Kind == InputSectionKind;
148
161k
  }
149
150
  StringMatcher FilePat;
151
152
  // Input sections that matches at least one of SectionPatterns
153
  // will be associated with this InputSectionDescription.
154
  std::vector<SectionPattern> SectionPatterns;
155
156
  std::vector<InputSection *> Sections;
157
158
  // Temporary record of synthetic ThunkSection instances and the pass that
159
  // they were created in. This is used to insert newly created ThunkSections
160
  // into Sections at the end of a createThunks() pass.
161
  std::vector<std::pair<ThunkSection *, uint32_t>> ThunkSections;
162
};
163
164
// Represents an ASSERT().
165
struct AssertCommand : BaseCommand {
166
3
  AssertCommand(Expr E) : BaseCommand(AssertKind), Expression(E) {}
167
168
150k
  static bool classof(const BaseCommand *C) { return C->Kind == AssertKind; }
169
170
  Expr Expression;
171
};
172
173
// Represents BYTE(), SHORT(), LONG(), or QUAD().
174
struct ByteCommand : BaseCommand {
175
  ByteCommand(Expr E, unsigned Size)
176
10
      : BaseCommand(ByteKind), Expression(E), Size(Size) {}
177
178
222k
  static bool classof(const BaseCommand *C) { return C->Kind == ByteKind; }
179
180
  Expr Expression;
181
  unsigned Offset;
182
  unsigned Size;
183
};
184
185
struct PhdrsCommand {
186
  StringRef Name;
187
  unsigned Type = llvm::ELF::PT_NULL;
188
  bool HasFilehdr = false;
189
  bool HasPhdrs = false;
190
  llvm::Optional<unsigned> Flags;
191
  Expr LMAExpr = nullptr;
192
};
193
194
class LinkerScript final {
195
  // Temporary state used in processSectionCommands() and assignAddresses()
196
  // that must be reinitialized for each call to the above functions, and must
197
  // not be used outside of the scope of a call to the above functions.
198
  struct AddressState {
199
    AddressState();
200
    uint64_t ThreadBssOffset = 0;
201
    OutputSection *OutSec = nullptr;
202
    MemoryRegion *MemRegion = nullptr;
203
    llvm::DenseMap<const MemoryRegion *, uint64_t> MemRegionOffset;
204
    std::function<uint64_t()> LMAOffset;
205
  };
206
207
  llvm::DenseMap<StringRef, OutputSection *> NameToOutputSection;
208
209
  void addSymbol(SymbolAssignment *Cmd);
210
  void assignSymbol(SymbolAssignment *Cmd, bool InSec);
211
  void setDot(Expr E, const Twine &Loc, bool InSec);
212
213
  std::vector<InputSection *>
214
  computeInputSections(const InputSectionDescription *,
215
                       const llvm::DenseMap<SectionBase *, int> &Order);
216
217
  std::vector<InputSection *>
218
  createInputSectionList(OutputSection &Cmd,
219
                         const llvm::DenseMap<SectionBase *, int> &Order);
220
221
  std::vector<size_t> getPhdrIndices(OutputSection *Sec);
222
223
  MemoryRegion *findMemoryRegion(OutputSection *Sec);
224
225
  void switchTo(OutputSection *Sec);
226
  uint64_t advance(uint64_t Size, unsigned Align);
227
  void output(InputSection *Sec);
228
229
  void assignOffsets(OutputSection *Sec);
230
231
  // Ctx captures the local AddressState and makes it accessible
232
  // deliberately. This is needed as there are some cases where we cannot just
233
  // thread the current state through to a lambda function created by the
234
  // script parser.
235
  // This should remain a plain pointer as its lifetime is smaller than
236
  // LinkerScript.
237
  AddressState *Ctx = nullptr;
238
239
  OutputSection *Aether;
240
241
  uint64_t Dot;
242
243
public:
244
  OutputSection *createOutputSection(StringRef Name, StringRef Location);
245
  OutputSection *getOrCreateOutputSection(StringRef Name);
246
247
1.00k
  bool hasPhdrsCommands() { return !PhdrsCommands.empty(); }
248
1.67k
  uint64_t getDot() { return Dot; }
249
  void discard(ArrayRef<InputSection *> V);
250
251
  ExprValue getSymbolValue(StringRef Name, const Twine &Loc);
252
253
  void addOrphanSections();
254
  void removeEmptyCommands();
255
  void adjustSectionsBeforeSorting();
256
  void adjustSectionsAfterSorting();
257
258
  std::vector<PhdrEntry *> createPhdrs();
259
  bool needsInterpSection();
260
261
  bool shouldKeep(InputSectionBase *S);
262
  void assignAddresses();
263
  void allocateHeaders(std::vector<PhdrEntry *> &Phdrs);
264
  void processSectionCommands();
265
266
  // SECTIONS command list.
267
  std::vector<BaseCommand *> SectionCommands;
268
269
  // PHDRS command list.
270
  std::vector<PhdrsCommand> PhdrsCommands;
271
272
  bool HasSectionsCommand = false;
273
  bool ErrorOnMissingSection = false;
274
275
  // List of section patterns specified with KEEP commands. They will
276
  // be kept even if they are unused and --gc-sections is specified.
277
  std::vector<InputSectionDescription *> KeptSections;
278
279
  // A map from memory region name to a memory region descriptor.
280
  llvm::MapVector<llvm::StringRef, MemoryRegion *> MemoryRegions;
281
282
  // A list of symbols referenced by the script.
283
  std::vector<llvm::StringRef> ReferencedSymbols;
284
};
285
286
extern LinkerScript *Script;
287
288
} // end namespace elf
289
} // end namespace lld
290
291
#endif // LLD_ELF_LINKER_SCRIPT_H