Coverage Report

Created: 2018-08-19 21:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/COFF/Chunks.h
Line
Count
Source (jump to first uncovered line)
1
//===- Chunks.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_COFF_CHUNKS_H
11
#define LLD_COFF_CHUNKS_H
12
13
#include "Config.h"
14
#include "InputFiles.h"
15
#include "lld/Common/LLVM.h"
16
#include "llvm/ADT/ArrayRef.h"
17
#include "llvm/ADT/iterator.h"
18
#include "llvm/ADT/iterator_range.h"
19
#include "llvm/MC/StringTableBuilder.h"
20
#include "llvm/Object/COFF.h"
21
#include <utility>
22
#include <vector>
23
24
namespace lld {
25
namespace coff {
26
27
using llvm::COFF::ImportDirectoryTableEntry;
28
using llvm::object::COFFSymbolRef;
29
using llvm::object::SectionRef;
30
using llvm::object::coff_relocation;
31
using llvm::object::coff_section;
32
33
class Baserel;
34
class Defined;
35
class DefinedImportData;
36
class DefinedRegular;
37
class ObjFile;
38
class OutputSection;
39
class Symbol;
40
41
// Mask for permissions (discardable, writable, readable, executable, etc).
42
const uint32_t PermMask = 0xFE000000;
43
44
// Mask for section types (code, data, bss).
45
const uint32_t TypeMask = 0x000000E0;
46
47
// A Chunk represents a chunk of data that will occupy space in the
48
// output (if the resolver chose that). It may or may not be backed by
49
// a section of an input file. It could be linker-created data, or
50
// doesn't even have actual data (if common or bss).
51
class Chunk {
52
public:
53
  enum Kind { SectionKind, OtherKind };
54
4.29k
  Kind kind() const { return ChunkKind; }
55
4.54k
  virtual ~Chunk() = default;
56
57
  // Returns the size of this chunk (even if this is a common or BSS.)
58
  virtual size_t getSize() const = 0;
59
60
  // Write this chunk to a mmap'ed file, assuming Buf is pointing to
61
  // beginning of the file. Because this function may use RVA values
62
  // of other chunks for relocations, you need to set them properly
63
  // before calling this function.
64
125
  virtual void writeTo(uint8_t *Buf) const {}
65
66
  // Called by the writer after an RVA is assigned, but before calling
67
  // getSize().
68
2.68k
  virtual void finalizeContents() {}
69
70
  // The writer sets and uses the addresses.
71
4.61k
  uint64_t getRVA() const { return RVA; }
72
2.69k
  void setRVA(uint64_t V) { RVA = V; }
73
74
  // Returns true if this has non-zero data. BSS chunks return
75
  // false. If false is returned, the space occupied by this chunk
76
  // will be filled with zeros.
77
1.27k
  virtual bool hasData() const { return true; }
78
79
  // Returns readable/writable/executable bits.
80
0
  virtual uint32_t getOutputCharacteristics() const { return 0; }
81
82
  // Returns the section name if this is a section chunk.
83
  // It is illegal to call this function on non-section chunks.
84
0
  virtual StringRef getSectionName() const {
85
0
    llvm_unreachable("unimplemented getSectionName");
86
0
  }
87
88
  // An output section has pointers to chunks in the section, and each
89
  // chunk has a back pointer to an output section.
90
3.95k
  void setOutputSection(OutputSection *O) { Out = O; }
91
2.38k
  OutputSection *getOutputSection() const { return Out; }
92
93
  // Windows-specific.
94
  // Collect all locations that contain absolute addresses for base relocations.
95
1.24k
  virtual void getBaserels(std::vector<Baserel> *Res) {}
96
97
  // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
98
  // bytes, so this is used only for logging or debugging.
99
0
  virtual StringRef getDebugName() { return ""; }
100
101
  // The alignment of this chunk. The writer uses the value.
102
  uint32_t Alignment = 1;
103
104
protected:
105
4.54k
  Chunk(Kind K = OtherKind) : ChunkKind(K) {}
106
  const Kind ChunkKind;
107
108
  // The RVA of this chunk in the output. The writer sets a value.
109
  uint64_t RVA = 0;
110
111
  // The output section for this chunk.
112
  OutputSection *Out = nullptr;
113
114
public:
115
  // The offset from beginning of the output section. The writer sets a value.
116
  uint64_t OutputSectionOff = 0;
117
};
118
119
// A chunk corresponding a section of an input file.
120
class SectionChunk final : public Chunk {
121
  // Identical COMDAT Folding feature accesses section internal data.
122
  friend class ICF;
123
124
public:
125
  class symbol_iterator : public llvm::iterator_adaptor_base<
126
                              symbol_iterator, const coff_relocation *,
127
                              std::random_access_iterator_tag, Symbol *> {
128
    friend SectionChunk;
129
130
    ObjFile *File;
131
132
    symbol_iterator(ObjFile *File, const coff_relocation *I)
133
1.99k
        : symbol_iterator::iterator_adaptor_base(I), File(File) {}
134
135
  public:
136
    symbol_iterator() = default;
137
138
547
    Symbol *operator*() const { return File->getSymbol(I->SymbolTableIndex); }
139
  };
140
141
  SectionChunk(ObjFile *File, const coff_section *Header);
142
4.29k
  static bool classof(const Chunk *C) { return C->kind() == SectionKind; }
143
2.86k
  size_t getSize() const override { return Header->SizeOfRawData; }
144
  ArrayRef<uint8_t> getContents() const;
145
  void writeTo(uint8_t *Buf) const override;
146
  bool hasData() const override;
147
  uint32_t getOutputCharacteristics() const override;
148
1.63k
  StringRef getSectionName() const override { return SectionName; }
149
  void getBaserels(std::vector<Baserel> *Res) override;
150
  bool isCOMDAT() const;
151
  void applyRelX64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
152
                   uint64_t P) const;
153
  void applyRelX86(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
154
                   uint64_t P) const;
155
  void applyRelARM(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
156
                   uint64_t P) const;
157
  void applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
158
                     uint64_t P) const;
159
160
  // Called if the garbage collector decides to not include this chunk
161
  // in a final output. It's supposed to print out a log message to stdout.
162
  void printDiscardedMessage() const;
163
164
  // Adds COMDAT associative sections to this COMDAT section. A chunk
165
  // and its children are treated as a group by the garbage collector.
166
  void addAssociative(SectionChunk *Child);
167
168
  StringRef getDebugName() override;
169
170
  // Returns true if the chunk was not dropped by GC.
171
3.97k
  bool isLive() { return Live; }
172
173
  // Used by the garbage collector.
174
199
  void markLive() {
175
199
    assert(Config->DoGC && "should only mark things live from GC");
176
199
    assert(!isLive() && "Cannot mark an already live section!");
177
199
    Live = true;
178
199
  }
179
180
  // True if this is a codeview debug info chunk. These will not be laid out in
181
  // the image. Instead they will end up in the PDB, if one is requested.
182
1.59k
  bool isCodeView() const {
183
1.59k
    return SectionName == ".debug" || SectionName.startswith(".debug$");
184
1.59k
  }
185
186
  // True if this is a DWARF debug info or exception handling chunk.
187
3
  bool isDWARF() const {
188
3
    return SectionName.startswith(".debug_") || 
SectionName == ".eh_frame"1
;
189
3
  }
190
191
  // Allow iteration over the bodies of this chunk's relocated symbols.
192
998
  llvm::iterator_range<symbol_iterator> symbols() const {
193
998
    return llvm::make_range(symbol_iterator(File, Relocs.begin()),
194
998
                            symbol_iterator(File, Relocs.end()));
195
998
  }
196
197
  // Allow iteration over the associated child chunks for this section.
198
1.10k
  ArrayRef<SectionChunk *> children() const { return AssocChildren; }
199
200
  // A pointer pointing to a replacement for this chunk.
201
  // Initially it points to "this" object. If this chunk is merged
202
  // with other chunk by ICF, it points to another chunk,
203
  // and this chunk is considrered as dead.
204
  SectionChunk *Repl;
205
206
  // The CRC of the contents as described in the COFF spec 4.5.5.
207
  // Auxiliary Format 5: Section Definitions. Used for ICF.
208
  uint32_t Checksum = 0;
209
210
  const coff_section *Header;
211
212
  // The file that this chunk was created from.
213
  ObjFile *File;
214
215
  // The COMDAT leader symbol if this is a COMDAT chunk.
216
  DefinedRegular *Sym = nullptr;
217
218
  ArrayRef<coff_relocation> Relocs;
219
220
private:
221
  StringRef SectionName;
222
  std::vector<SectionChunk *> AssocChildren;
223
224
  // Used by the garbage collector.
225
  bool Live;
226
227
  // Used for ICF (Identical COMDAT Folding)
228
  void replace(SectionChunk *Other);
229
  uint32_t Class[2] = {0, 0};
230
};
231
232
// This class is used to implement an lld-specific feature (not implemented in
233
// MSVC) that minimizes the output size by finding string literals sharing tail
234
// parts and merging them.
235
//
236
// If string tail merging is enabled and a section is identified as containing a
237
// string literal, it is added to a MergeChunk with an appropriate alignment.
238
// The MergeChunk then tail merges the strings using the StringTableBuilder
239
// class and assigns RVAs and section offsets to each of the member chunks based
240
// on the offsets assigned by the StringTableBuilder.
241
class MergeChunk : public Chunk {
242
public:
243
  MergeChunk(uint32_t Alignment);
244
  static void addSection(SectionChunk *C);
245
  void finalizeContents() override;
246
247
  uint32_t getOutputCharacteristics() const override;
248
0
  StringRef getSectionName() const override { return ".rdata"; }
249
  size_t getSize() const override;
250
  void writeTo(uint8_t *Buf) const override;
251
252
  static std::map<uint32_t, MergeChunk *> Instances;
253
  std::vector<SectionChunk *> Sections;
254
255
private:
256
  llvm::StringTableBuilder Builder;
257
};
258
259
// A chunk for common symbols. Common chunks don't have actual data.
260
class CommonChunk : public Chunk {
261
public:
262
  CommonChunk(const COFFSymbolRef Sym);
263
12
  size_t getSize() const override { return Sym.getValue(); }
264
12
  bool hasData() const override { return false; }
265
  uint32_t getOutputCharacteristics() const override;
266
12
  StringRef getSectionName() const override { return ".bss"; }
267
268
private:
269
  const COFFSymbolRef Sym;
270
};
271
272
// A chunk for linker-created strings.
273
class StringChunk : public Chunk {
274
public:
275
612
  explicit StringChunk(StringRef S) : Str(S) {}
276
421
  size_t getSize() const override { return Str.size() + 1; }
277
  void writeTo(uint8_t *Buf) const override;
278
279
private:
280
  StringRef Str;
281
};
282
283
static const uint8_t ImportThunkX86[] = {
284
    0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
285
};
286
287
static const uint8_t ImportThunkARM[] = {
288
    0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
289
    0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
290
    0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
291
};
292
293
static const uint8_t ImportThunkARM64[] = {
294
    0x10, 0x00, 0x00, 0x90, // adrp x16, #0
295
    0x10, 0x02, 0x40, 0xf9, // ldr  x16, [x16]
296
    0x00, 0x02, 0x1f, 0xd6, // br   x16
297
};
298
299
// Windows-specific.
300
// A chunk for DLL import jump table entry. In a final output, it's
301
// contents will be a JMP instruction to some __imp_ symbol.
302
class ImportThunkChunkX64 : public Chunk {
303
public:
304
  explicit ImportThunkChunkX64(Defined *S);
305
104
  size_t getSize() const override { return sizeof(ImportThunkX86); }
306
  void writeTo(uint8_t *Buf) const override;
307
308
private:
309
  Defined *ImpSymbol;
310
};
311
312
class ImportThunkChunkX86 : public Chunk {
313
public:
314
18
  explicit ImportThunkChunkX86(Defined *S) : ImpSymbol(S) {}
315
22
  size_t getSize() const override { return sizeof(ImportThunkX86); }
316
  void getBaserels(std::vector<Baserel> *Res) override;
317
  void writeTo(uint8_t *Buf) const override;
318
319
private:
320
  Defined *ImpSymbol;
321
};
322
323
class ImportThunkChunkARM : public Chunk {
324
public:
325
2
  explicit ImportThunkChunkARM(Defined *S) : ImpSymbol(S) {}
326
0
  size_t getSize() const override { return sizeof(ImportThunkARM); }
327
  void getBaserels(std::vector<Baserel> *Res) override;
328
  void writeTo(uint8_t *Buf) const override;
329
330
private:
331
  Defined *ImpSymbol;
332
};
333
334
class ImportThunkChunkARM64 : public Chunk {
335
public:
336
3
  explicit ImportThunkChunkARM64(Defined *S) : ImpSymbol(S) {}
337
3
  size_t getSize() const override { return sizeof(ImportThunkARM64); }
338
  void writeTo(uint8_t *Buf) const override;
339
340
private:
341
  Defined *ImpSymbol;
342
};
343
344
// Windows-specific.
345
// See comments for DefinedLocalImport class.
346
class LocalImportChunk : public Chunk {
347
public:
348
6
  explicit LocalImportChunk(Defined *S) : Sym(S) {}
349
  size_t getSize() const override;
350
  void getBaserels(std::vector<Baserel> *Res) override;
351
  void writeTo(uint8_t *Buf) const override;
352
353
private:
354
  Defined *Sym;
355
};
356
357
// Duplicate RVAs are not allowed in RVA tables, so unique symbols by chunk and
358
// offset into the chunk. Order does not matter as the RVA table will be sorted
359
// later.
360
struct ChunkAndOffset {
361
  Chunk *InputChunk;
362
  uint32_t Offset;
363
364
  struct DenseMapInfo {
365
192
    static ChunkAndOffset getEmptyKey() {
366
192
      return {llvm::DenseMapInfo<Chunk *>::getEmptyKey(), 0};
367
192
    }
368
141
    static ChunkAndOffset getTombstoneKey() {
369
141
      return {llvm::DenseMapInfo<Chunk *>::getTombstoneKey(), 0};
370
141
    }
371
36
    static unsigned getHashValue(const ChunkAndOffset &CO) {
372
36
      return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue(
373
36
          {CO.InputChunk, CO.Offset});
374
36
    }
375
3.20k
    static bool isEqual(const ChunkAndOffset &LHS, const ChunkAndOffset &RHS) {
376
3.20k
      return LHS.InputChunk == RHS.InputChunk && 
LHS.Offset == RHS.Offset2.98k
;
377
3.20k
    }
378
  };
379
};
380
381
using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>;
382
383
// Table which contains symbol RVAs. Used for /safeseh and /guard:cf.
384
class RVATableChunk : public Chunk {
385
public:
386
18
  explicit RVATableChunk(SymbolRVASet S) : Syms(std::move(S)) {}
387
36
  size_t getSize() const override { return Syms.size() * 4; }
388
  void writeTo(uint8_t *Buf) const override;
389
390
private:
391
  SymbolRVASet Syms;
392
};
393
394
// Windows-specific.
395
// This class represents a block in .reloc section.
396
// See the PE/COFF spec 5.6 for details.
397
class BaserelChunk : public Chunk {
398
public:
399
  BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End);
400
89
  size_t getSize() const override { return Data.size(); }
401
  void writeTo(uint8_t *Buf) const override;
402
403
private:
404
  std::vector<uint8_t> Data;
405
};
406
407
class Baserel {
408
public:
409
179
  Baserel(uint32_t V, uint8_t Ty) : RVA(V), Type(Ty) {}
410
32
  explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
lld::coff::Baserel::Baserel(unsigned int)
Line
Count
Source
410
32
  explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
Unexecuted instantiation: lld::coff::Baserel::Baserel(unsigned int)
411
  uint8_t getDefaultType();
412
413
  uint32_t RVA;
414
  uint8_t Type;
415
};
416
417
void applyMOV32T(uint8_t *Off, uint32_t V);
418
void applyBranch24T(uint8_t *Off, int32_t V);
419
420
} // namespace coff
421
} // namespace lld
422
423
namespace llvm {
424
template <>
425
struct DenseMapInfo<lld::coff::ChunkAndOffset>
426
    : lld::coff::ChunkAndOffset::DenseMapInfo {};
427
}
428
429
#endif