Coverage Report

Created: 2019-05-19 14:56

/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
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLD_COFF_CHUNKS_H
10
#define LLD_COFF_CHUNKS_H
11
12
#include "Config.h"
13
#include "InputFiles.h"
14
#include "lld/Common/LLVM.h"
15
#include "llvm/ADT/ArrayRef.h"
16
#include "llvm/ADT/iterator.h"
17
#include "llvm/ADT/iterator_range.h"
18
#include "llvm/MC/StringTableBuilder.h"
19
#include "llvm/Object/COFF.h"
20
#include <utility>
21
#include <vector>
22
23
namespace lld {
24
namespace coff {
25
26
using llvm::COFF::ImportDirectoryTableEntry;
27
using llvm::object::COFFSymbolRef;
28
using llvm::object::SectionRef;
29
using llvm::object::coff_relocation;
30
using llvm::object::coff_section;
31
32
class Baserel;
33
class Defined;
34
class DefinedImportData;
35
class DefinedRegular;
36
class ObjFile;
37
class OutputSection;
38
class RuntimePseudoReloc;
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 : uint8_t { SectionKind, OtherKind };
54
7.38k
  Kind kind() const { return ChunkKind; }
55
6.82k
  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
12
  virtual void writeTo(uint8_t *Buf) const {}
65
66
  // Called by the writer after an RVA is assigned, but before calling
67
  // getSize().
68
4.23k
  virtual void finalizeContents() {}
69
70
  // The writer sets and uses the addresses. In practice, PE images cannot be
71
  // larger than 2GB. Chunks are always laid as part of the image, so Chunk RVAs
72
  // can be stored with 32 bits.
73
10.8k
  uint32_t getRVA() const { return RVA; }
74
4.30k
  void setRVA(uint64_t V) {
75
4.30k
    RVA = (uint32_t)V;
76
4.30k
    assert(RVA == V && "RVA truncated");
77
4.30k
  }
78
79
  // Returns true if this has non-zero data. BSS chunks return
80
  // false. If false is returned, the space occupied by this chunk
81
  // will be filled with zeros.
82
1.95k
  virtual bool hasData() const { return true; }
83
84
  // Returns readable/writable/executable bits.
85
0
  virtual uint32_t getOutputCharacteristics() const { return 0; }
86
87
  // Returns the section name if this is a section chunk.
88
  // It is illegal to call this function on non-section chunks.
89
0
  virtual StringRef getSectionName() const {
90
0
    llvm_unreachable("unimplemented getSectionName");
91
0
  }
92
93
  // An output section has pointers to chunks in the section, and each
94
  // chunk has a back pointer to an output section.
95
6.09k
  void setOutputSection(OutputSection *O) { Out = O; }
96
3.21k
  OutputSection *getOutputSection() const { return Out; }
97
98
  // Windows-specific.
99
  // Collect all locations that contain absolute addresses for base relocations.
100
1.94k
  virtual void getBaserels(std::vector<Baserel> *Res) {}
101
102
  // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
103
  // bytes, so this is used only for logging or debugging.
104
0
  virtual StringRef getDebugName() { return ""; }
105
106
  // The alignment of this chunk. The writer uses the value.
107
  uint32_t Alignment = 1;
108
109
0
  virtual bool isHotPatchable() const { return false; }
110
111
protected:
112
6.82k
  Chunk(Kind K = OtherKind) : ChunkKind(K) {}
113
  const Kind ChunkKind;
114
115
public:
116
  // Whether this section needs to be kept distinct from other sections during
117
  // ICF. This is set by the driver using address-significance tables.
118
  bool KeepUnique = false;
119
120
protected:
121
  // The RVA of this chunk in the output. The writer sets a value.
122
  uint32_t RVA = 0;
123
124
protected:
125
  // The output section for this chunk.
126
  OutputSection *Out = nullptr;
127
};
128
129
// A chunk corresponding a section of an input file.
130
class SectionChunk final : public Chunk {
131
  // Identical COMDAT Folding feature accesses section internal data.
132
  friend class ICF;
133
134
public:
135
  class symbol_iterator : public llvm::iterator_adaptor_base<
136
                              symbol_iterator, const coff_relocation *,
137
                              std::random_access_iterator_tag, Symbol *> {
138
    friend SectionChunk;
139
140
    ObjFile *File;
141
142
    symbol_iterator(ObjFile *File, const coff_relocation *I)
143
3.69k
        : symbol_iterator::iterator_adaptor_base(I), File(File) {}
144
145
  public:
146
    symbol_iterator() = default;
147
148
999
    Symbol *operator*() const { return File->getSymbol(I->SymbolTableIndex); }
149
  };
150
151
  SectionChunk(ObjFile *File, const coff_section *Header);
152
7.38k
  static bool classof(const Chunk *C) { return C->kind() == SectionKind; }
153
4.87k
  size_t getSize() const override { return Header->SizeOfRawData; }
154
  ArrayRef<uint8_t> getContents() const;
155
  void writeTo(uint8_t *Buf) const override;
156
  bool hasData() const override;
157
  uint32_t getOutputCharacteristics() const override;
158
8.43k
  StringRef getSectionName() const override {
159
8.43k
    return StringRef(SectionNameData, SectionNameSize);
160
8.43k
  }
161
  void getBaserels(std::vector<Baserel> *Res) override;
162
  bool isCOMDAT() const;
163
  void applyRelX64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
164
                   uint64_t P) const;
165
  void applyRelX86(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
166
                   uint64_t P) const;
167
  void applyRelARM(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
168
                   uint64_t P) const;
169
  void applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
170
                     uint64_t P) const;
171
172
  void getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> &Res);
173
174
  // Called if the garbage collector decides to not include this chunk
175
  // in a final output. It's supposed to print out a log message to stdout.
176
  void printDiscardedMessage() const;
177
178
  // Adds COMDAT associative sections to this COMDAT section. A chunk
179
  // and its children are treated as a group by the garbage collector.
180
  void addAssociative(SectionChunk *Child);
181
182
  StringRef getDebugName() override;
183
184
  // True if this is a codeview debug info chunk. These will not be laid out in
185
  // the image. Instead they will end up in the PDB, if one is requested.
186
2.55k
  bool isCodeView() const {
187
2.55k
    return getSectionName() == ".debug" || getSectionName().startswith(".debug$");
188
2.55k
  }
189
190
  // True if this is a DWARF debug info or exception handling chunk.
191
9
  bool isDWARF() const {
192
9
    return getSectionName().startswith(".debug_") || 
getSectionName() == ".eh_frame"7
;
193
9
  }
194
195
  // Allow iteration over the bodies of this chunk's relocated symbols.
196
1.84k
  llvm::iterator_range<symbol_iterator> symbols() const {
197
1.84k
    return llvm::make_range(symbol_iterator(File, RelocsData),
198
1.84k
                            symbol_iterator(File, RelocsData + RelocsSize));
199
1.84k
  }
200
201
807
  ArrayRef<coff_relocation> getRelocs() const {
202
807
    return llvm::makeArrayRef(RelocsData, RelocsSize);
203
807
  }
204
205
  // Reloc setter used by ARM range extension thunk insertion.
206
2.63k
  void setRelocs(ArrayRef<coff_relocation> NewRelocs) {
207
2.63k
    RelocsData = NewRelocs.data();
208
2.63k
    RelocsSize = NewRelocs.size();
209
2.63k
    assert(RelocsSize == NewRelocs.size() && "reloc size truncation");
210
2.63k
  }
211
212
  // Single linked list iterator for associated comdat children.
213
  class AssociatedIterator
214
      : public llvm::iterator_facade_base<
215
            AssociatedIterator, std::forward_iterator_tag, SectionChunk> {
216
  public:
217
    AssociatedIterator() = default;
218
3.32k
    AssociatedIterator(SectionChunk *Head) : Cur(Head) {}
219
0
    AssociatedIterator &operator=(const AssociatedIterator &R) {
220
0
      Cur = R.Cur;
221
0
      return *this;
222
0
    }
223
1.77k
    bool operator==(const AssociatedIterator &R) const { return Cur == R.Cur; }
224
0
    const SectionChunk &operator*() const { return *Cur; }
225
118
    SectionChunk &operator*() { return *Cur; }
226
118
    AssociatedIterator &operator++() {
227
118
      Cur = Cur->AssocChildren;
228
118
      return *this;
229
118
    }
230
231
  private:
232
    SectionChunk *Cur = nullptr;
233
  };
234
235
  // Allow iteration over the associated child chunks for this section.
236
1.66k
  llvm::iterator_range<AssociatedIterator> children() const {
237
1.66k
    return llvm::make_range(AssociatedIterator(AssocChildren),
238
1.66k
                            AssociatedIterator(nullptr));
239
1.66k
  }
240
241
  // The section ID this chunk belongs to in its Obj.
242
  uint32_t getSectionNumber() const;
243
244
  ArrayRef<uint8_t> consumeDebugMagic();
245
246
  static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data,
247
                                             StringRef SectionName);
248
249
  static SectionChunk *findByName(ArrayRef<SectionChunk *> Sections,
250
                                  StringRef Name);
251
252
10
  bool isHotPatchable() const override { return File->HotPatchable; }
253
254
  // The file that this chunk was created from.
255
  ObjFile *File;
256
257
  // Pointer to the COFF section header in the input file.
258
  const coff_section *Header;
259
260
  // The COMDAT leader symbol if this is a COMDAT chunk.
261
  DefinedRegular *Sym = nullptr;
262
263
  // The CRC of the contents as described in the COFF spec 4.5.5.
264
  // Auxiliary Format 5: Section Definitions. Used for ICF.
265
  uint32_t Checksum = 0;
266
267
  // Used by the garbage collector.
268
  bool Live;
269
270
  // The COMDAT selection if this is a COMDAT chunk.
271
  llvm::COFF::COMDATType Selection = (llvm::COFF::COMDATType)0;
272
273
  // A pointer pointing to a replacement for this chunk.
274
  // Initially it points to "this" object. If this chunk is merged
275
  // with other chunk by ICF, it points to another chunk,
276
  // and this chunk is considered as dead.
277
  SectionChunk *Repl;
278
279
private:
280
  SectionChunk *AssocChildren = nullptr;
281
282
  // Used for ICF (Identical COMDAT Folding)
283
  void replace(SectionChunk *Other);
284
  uint32_t Class[2] = {0, 0};
285
286
  // Relocations for this section. Size is stored below.
287
  const coff_relocation *RelocsData;
288
289
  // Section name string. Size is stored below.
290
  const char *SectionNameData;
291
292
  uint32_t RelocsSize = 0;
293
  uint32_t SectionNameSize = 0;
294
};
295
296
// This class is used to implement an lld-specific feature (not implemented in
297
// MSVC) that minimizes the output size by finding string literals sharing tail
298
// parts and merging them.
299
//
300
// If string tail merging is enabled and a section is identified as containing a
301
// string literal, it is added to a MergeChunk with an appropriate alignment.
302
// The MergeChunk then tail merges the strings using the StringTableBuilder
303
// class and assigns RVAs and section offsets to each of the member chunks based
304
// on the offsets assigned by the StringTableBuilder.
305
class MergeChunk : public Chunk {
306
public:
307
  MergeChunk(uint32_t Alignment);
308
  static void addSection(SectionChunk *C);
309
  void finalizeContents() override;
310
311
  uint32_t getOutputCharacteristics() const override;
312
0
  StringRef getSectionName() const override { return ".rdata"; }
313
  size_t getSize() const override;
314
  void writeTo(uint8_t *Buf) const override;
315
316
  static std::map<uint32_t, MergeChunk *> Instances;
317
  std::vector<SectionChunk *> Sections;
318
319
private:
320
  llvm::StringTableBuilder Builder;
321
  bool Finalized = false;
322
};
323
324
// A chunk for common symbols. Common chunks don't have actual data.
325
class CommonChunk : public Chunk {
326
public:
327
  CommonChunk(const COFFSymbolRef Sym);
328
12
  size_t getSize() const override { return Sym.getValue(); }
329
12
  bool hasData() const override { return false; }
330
  uint32_t getOutputCharacteristics() const override;
331
12
  StringRef getSectionName() const override { return ".bss"; }
332
333
private:
334
  const COFFSymbolRef Sym;
335
};
336
337
// A chunk for linker-created strings.
338
class StringChunk : public Chunk {
339
public:
340
804
  explicit StringChunk(StringRef S) : Str(S) {}
341
522
  size_t getSize() const override { return Str.size() + 1; }
342
  void writeTo(uint8_t *Buf) const override;
343
344
private:
345
  StringRef Str;
346
};
347
348
static const uint8_t ImportThunkX86[] = {
349
    0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
350
};
351
352
static const uint8_t ImportThunkARM[] = {
353
    0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
354
    0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
355
    0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
356
};
357
358
static const uint8_t ImportThunkARM64[] = {
359
    0x10, 0x00, 0x00, 0x90, // adrp x16, #0
360
    0x10, 0x02, 0x40, 0xf9, // ldr  x16, [x16]
361
    0x00, 0x02, 0x1f, 0xd6, // br   x16
362
};
363
364
// Windows-specific.
365
// A chunk for DLL import jump table entry. In a final output, its
366
// contents will be a JMP instruction to some __imp_ symbol.
367
class ImportThunkChunkX64 : public Chunk {
368
public:
369
  explicit ImportThunkChunkX64(Defined *S);
370
146
  size_t getSize() const override { return sizeof(ImportThunkX86); }
371
  void writeTo(uint8_t *Buf) const override;
372
373
1
  bool isHotPatchable() const override { return true; }
374
375
private:
376
  Defined *ImpSymbol;
377
};
378
379
class ImportThunkChunkX86 : public Chunk {
380
public:
381
18
  explicit ImportThunkChunkX86(Defined *S) : ImpSymbol(S) {}
382
30
  size_t getSize() const override { return sizeof(ImportThunkX86); }
383
  void getBaserels(std::vector<Baserel> *Res) override;
384
  void writeTo(uint8_t *Buf) const override;
385
386
0
  bool isHotPatchable() const override { return true; }
387
388
private:
389
  Defined *ImpSymbol;
390
};
391
392
class ImportThunkChunkARM : public Chunk {
393
public:
394
2
  explicit ImportThunkChunkARM(Defined *S) : ImpSymbol(S) {}
395
0
  size_t getSize() const override { return sizeof(ImportThunkARM); }
396
  void getBaserels(std::vector<Baserel> *Res) override;
397
  void writeTo(uint8_t *Buf) const override;
398
399
0
  bool isHotPatchable() const override { return true; }
400
401
private:
402
  Defined *ImpSymbol;
403
};
404
405
class ImportThunkChunkARM64 : public Chunk {
406
public:
407
4
  explicit ImportThunkChunkARM64(Defined *S) : ImpSymbol(S) {}
408
4
  size_t getSize() const override { return sizeof(ImportThunkARM64); }
409
  void writeTo(uint8_t *Buf) const override;
410
411
0
  bool isHotPatchable() const override { return true; }
412
413
private:
414
  Defined *ImpSymbol;
415
};
416
417
class RangeExtensionThunkARM : public Chunk {
418
public:
419
50
  explicit RangeExtensionThunkARM(Defined *T) : Target(T) {}
420
  size_t getSize() const override;
421
  void writeTo(uint8_t *Buf) const override;
422
423
  Defined *Target;
424
};
425
426
class RangeExtensionThunkARM64 : public Chunk {
427
public:
428
1
  explicit RangeExtensionThunkARM64(Defined *T) : Target(T) {}
429
  size_t getSize() const override;
430
  void writeTo(uint8_t *Buf) const override;
431
432
  Defined *Target;
433
};
434
435
// Windows-specific.
436
// See comments for DefinedLocalImport class.
437
class LocalImportChunk : public Chunk {
438
public:
439
9
  explicit LocalImportChunk(Defined *S) : Sym(S) {
440
9
    Alignment = Config->Wordsize;
441
9
  }
442
  size_t getSize() const override;
443
  void getBaserels(std::vector<Baserel> *Res) override;
444
  void writeTo(uint8_t *Buf) const override;
445
446
private:
447
  Defined *Sym;
448
};
449
450
// Duplicate RVAs are not allowed in RVA tables, so unique symbols by chunk and
451
// offset into the chunk. Order does not matter as the RVA table will be sorted
452
// later.
453
struct ChunkAndOffset {
454
  Chunk *InputChunk;
455
  uint32_t Offset;
456
457
  struct DenseMapInfo {
458
208
    static ChunkAndOffset getEmptyKey() {
459
208
      return {llvm::DenseMapInfo<Chunk *>::getEmptyKey(), 0};
460
208
    }
461
153
    static ChunkAndOffset getTombstoneKey() {
462
153
      return {llvm::DenseMapInfo<Chunk *>::getTombstoneKey(), 0};
463
153
    }
464
38
    static unsigned getHashValue(const ChunkAndOffset &CO) {
465
38
      return llvm::DenseMapInfo<std::pair<Chunk *, uint32_t>>::getHashValue(
466
38
          {CO.InputChunk, CO.Offset});
467
38
    }
468
3.59k
    static bool isEqual(const ChunkAndOffset &LHS, const ChunkAndOffset &RHS) {
469
3.59k
      return LHS.InputChunk == RHS.InputChunk && 
LHS.Offset == RHS.Offset3.36k
;
470
3.59k
    }
471
  };
472
};
473
474
using SymbolRVASet = llvm::DenseSet<ChunkAndOffset>;
475
476
// Table which contains symbol RVAs. Used for /safeseh and /guard:cf.
477
class RVATableChunk : public Chunk {
478
public:
479
20
  explicit RVATableChunk(SymbolRVASet S) : Syms(std::move(S)) {}
480
40
  size_t getSize() const override { return Syms.size() * 4; }
481
  void writeTo(uint8_t *Buf) const override;
482
483
private:
484
  SymbolRVASet Syms;
485
};
486
487
// Windows-specific.
488
// This class represents a block in .reloc section.
489
// See the PE/COFF spec 5.6 for details.
490
class BaserelChunk : public Chunk {
491
public:
492
  BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End);
493
120
  size_t getSize() const override { return Data.size(); }
494
  void writeTo(uint8_t *Buf) const override;
495
496
private:
497
  std::vector<uint8_t> Data;
498
};
499
500
class Baserel {
501
public:
502
220
  Baserel(uint32_t V, uint8_t Ty) : RVA(V), Type(Ty) {}
503
40
  explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
lld::coff::Baserel::Baserel(unsigned int)
Line
Count
Source
503
40
  explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
Unexecuted instantiation: lld::coff::Baserel::Baserel(unsigned int)
504
  uint8_t getDefaultType();
505
506
  uint32_t RVA;
507
  uint8_t Type;
508
};
509
510
// This is a placeholder Chunk, to allow attaching a DefinedSynthetic to a
511
// specific place in a section, without any data. This is used for the MinGW
512
// specific symbol __RUNTIME_PSEUDO_RELOC_LIST_END__, even though the concept
513
// of an empty chunk isn't MinGW specific.
514
class EmptyChunk : public Chunk {
515
public:
516
42
  EmptyChunk() {}
517
42
  size_t getSize() const override { return 0; }
518
42
  void writeTo(uint8_t *Buf) const override {}
519
};
520
521
// MinGW specific, for the "automatic import of variables from DLLs" feature.
522
// This provides the table of runtime pseudo relocations, for variable
523
// references that turned out to need to be imported from a DLL even though
524
// the reference didn't use the dllimport attribute. The MinGW runtime will
525
// process this table after loading, before handling control over to user
526
// code.
527
class PseudoRelocTableChunk : public Chunk {
528
public:
529
  PseudoRelocTableChunk(std::vector<RuntimePseudoReloc> &Relocs)
530
42
      : Relocs(std::move(Relocs)) {
531
42
    Alignment = 4;
532
42
  }
533
  size_t getSize() const override;
534
  void writeTo(uint8_t *Buf) const override;
535
536
private:
537
  std::vector<RuntimePseudoReloc> Relocs;
538
};
539
540
// MinGW specific; information about one individual location in the image
541
// that needs to be fixed up at runtime after loading. This represents
542
// one individual element in the PseudoRelocTableChunk table.
543
class RuntimePseudoReloc {
544
public:
545
  RuntimePseudoReloc(Defined *Sym, SectionChunk *Target, uint32_t TargetOffset,
546
                     int Flags)
547
5
      : Sym(Sym), Target(Target), TargetOffset(TargetOffset), Flags(Flags) {}
548
549
  Defined *Sym;
550
  SectionChunk *Target;
551
  uint32_t TargetOffset;
552
  // The Flags field contains the size of the relocation, in bits. No other
553
  // flags are currently defined.
554
  int Flags;
555
};
556
557
// MinGW specific. A Chunk that contains one pointer-sized absolute value.
558
class AbsolutePointerChunk : public Chunk {
559
public:
560
168
  AbsolutePointerChunk(uint64_t Value) : Value(Value) {
561
168
    Alignment = getSize();
562
168
  }
563
  size_t getSize() const override;
564
  void writeTo(uint8_t *Buf) const override;
565
566
private:
567
  uint64_t Value;
568
};
569
570
void applyMOV32T(uint8_t *Off, uint32_t V);
571
void applyBranch24T(uint8_t *Off, int32_t V);
572
573
void applyArm64Addr(uint8_t *Off, uint64_t S, uint64_t P, int Shift);
574
void applyArm64Imm(uint8_t *Off, uint64_t Imm, uint32_t RangeLimit);
575
void applyArm64Branch26(uint8_t *Off, int64_t V);
576
577
} // namespace coff
578
} // namespace lld
579
580
namespace llvm {
581
template <>
582
struct DenseMapInfo<lld::coff::ChunkAndOffset>
583
    : lld::coff::ChunkAndOffset::DenseMapInfo {};
584
}
585
586
#endif