Coverage Report

Created: 2017-09-21 03:39

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/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/Core/LLVM.h"
16
#include "llvm/ADT/ArrayRef.h"
17
#include "llvm/ADT/iterator.h"
18
#include "llvm/ADT/iterator_range.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 SymbolBody;
39
40
// Mask for section types (code, data, bss, disacardable, etc.)
41
// and permissions (writable, readable or executable).
42
const uint32_t PermMask = 0xFF0000F0;
43
44
// A Chunk represents a chunk of data that will occupy space in the
45
// output (if the resolver chose that). It may or may not be backed by
46
// a section of an input file. It could be linker-created data, or
47
// doesn't even have actual data (if common or bss).
48
class Chunk {
49
public:
50
  enum Kind { SectionKind, OtherKind };
51
2.48k
  Kind kind() const { return ChunkKind; }
52
2.72k
  virtual ~Chunk() = default;
53
54
  // Returns the size of this chunk (even if this is a common or BSS.)
55
  virtual size_t getSize() const = 0;
56
57
  // Write this chunk to a mmap'ed file, assuming Buf is pointing to
58
  // beginning of the file. Because this function may use RVA values
59
  // of other chunks for relocations, you need to set them properly
60
  // before calling this function.
61
102
  virtual void writeTo(uint8_t *Buf) const {}
62
63
  // The writer sets and uses the addresses.
64
3.84k
  uint64_t getRVA() const { return RVA; }
65
3.04k
  void setRVA(uint64_t V) { RVA = V; }
66
67
  // Returns true if this has non-zero data. BSS chunks return
68
  // false. If false is returned, the space occupied by this chunk
69
  // will be filled with zeros.
70
709
  virtual bool hasData() const { return true; }
71
72
  // Returns readable/writable/executable bits.
73
0
  virtual uint32_t getPermissions() const { return 0; }
74
75
  // Returns the section name if this is a section chunk.
76
  // It is illegal to call this function on non-section chunks.
77
0
  virtual StringRef getSectionName() const {
78
0
    llvm_unreachable("unimplemented getSectionName");
79
0
  }
80
81
  // An output section has pointers to chunks in the section, and each
82
  // chunk has a back pointer to an output section.
83
1.52k
  void setOutputSection(OutputSection *O) { Out = O; }
84
708
  OutputSection *getOutputSection() const { return Out; }
85
86
  // Windows-specific.
87
  // Collect all locations that contain absolute addresses for base relocations.
88
732
  virtual void getBaserels(std::vector<Baserel> *Res) {}
89
90
  // Returns a human-readable name of this chunk. Chunks are unnamed chunks of
91
  // bytes, so this is used only for logging or debugging.
92
0
  virtual StringRef getDebugName() { return ""; }
93
94
  // The alignment of this chunk. The writer uses the value.
95
  uint32_t Alignment = 1;
96
97
protected:
98
2.72k
  Chunk(Kind K = OtherKind) : ChunkKind(K) {}
99
  const Kind ChunkKind;
100
101
  // The RVA of this chunk in the output. The writer sets a value.
102
  uint64_t RVA = 0;
103
104
  // The output section for this chunk.
105
  OutputSection *Out = nullptr;
106
107
public:
108
  // The offset from beginning of the output section. The writer sets a value.
109
  uint64_t OutputSectionOff = 0;
110
};
111
112
// A chunk corresponding a section of an input file.
113
class SectionChunk final : public Chunk {
114
  // Identical COMDAT Folding feature accesses section internal data.
115
  friend class ICF;
116
117
public:
118
  class symbol_iterator : public llvm::iterator_adaptor_base<
119
                              symbol_iterator, const coff_relocation *,
120
                              std::random_access_iterator_tag, SymbolBody *> {
121
    friend SectionChunk;
122
123
    ObjFile *File;
124
125
    symbol_iterator(ObjFile *File, const coff_relocation *I)
126
1.39k
        : symbol_iterator::iterator_adaptor_base(I), File(File) {}
127
128
  public:
129
    symbol_iterator() = default;
130
131
425
    SymbolBody *operator*() const {
132
425
      return File->getSymbolBody(I->SymbolTableIndex);
133
425
    }
134
  };
135
136
  SectionChunk(ObjFile *File, const coff_section *Header);
137
2.48k
  static bool classof(const Chunk *C) { return C->kind() == SectionKind; }
138
1.42k
  size_t getSize() const override { return Header->SizeOfRawData; }
139
  ArrayRef<uint8_t> getContents() const;
140
  void writeTo(uint8_t *Buf) const override;
141
  bool hasData() const override;
142
  uint32_t getPermissions() const override;
143
852
  StringRef getSectionName() const override { return SectionName; }
144
  void getBaserels(std::vector<Baserel> *Res) override;
145
  bool isCOMDAT() const;
146
  void applyRelX64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
147
                   uint64_t P) const;
148
  void applyRelX86(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
149
                   uint64_t P) const;
150
  void applyRelARM(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
151
                   uint64_t P) const;
152
  void applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS, uint64_t S,
153
                     uint64_t P) const;
154
155
  // Called if the garbage collector decides to not include this chunk
156
  // in a final output. It's supposed to print out a log message to stdout.
157
  void printDiscardedMessage() const;
158
159
  // Adds COMDAT associative sections to this COMDAT section. A chunk
160
  // and its children are treated as a group by the garbage collector.
161
  void addAssociative(SectionChunk *Child);
162
163
  StringRef getDebugName() override;
164
70
  void setSymbol(DefinedRegular *S) 
{ if (70
!Sym70
)
Sym = S67
; }
165
166
  // Returns true if the chunk was not dropped by GC or COMDAT deduplication.
167
2.69k
  bool isLive() 
{ return Live && 2.69k
!Discarded2.52k
; }
168
169
  // Used by the garbage collector.
170
49
  void markLive() {
171
49
    assert(Config->DoGC && "should only mark things live from GC");
172
49
    assert(!isLive() && "Cannot mark an already live section!");
173
49
    Live = true;
174
49
  }
175
176
  // Returns true if this chunk was dropped by COMDAT deduplication.
177
713
  bool isDiscarded() const { return Discarded; }
178
179
  // Used by the SymbolTable when discarding unused comdat sections. This is
180
  // redundant when GC is enabled, as all comdat sections will start out dead.
181
13
  void markDiscarded() { Discarded = true; }
182
183
  // True if this is a codeview debug info chunk. These will not be laid out in
184
  // the image. Instead they will end up in the PDB, if one is requested.
185
1.17k
  bool isCodeView() const {
186
1.17k
    return SectionName == ".debug" || SectionName.startswith(".debug$");
187
1.17k
  }
188
189
  // True if this is a DWARF debug info chunk.
190
1
  bool isDWARF() const { return SectionName.startswith(".debug_"); }
191
192
  // Allow iteration over the bodies of this chunk's relocated symbols.
193
698
  llvm::iterator_range<symbol_iterator> symbols() const {
194
698
    return llvm::make_range(symbol_iterator(File, Relocs.begin()),
195
698
                            symbol_iterator(File, Relocs.end()));
196
698
  }
197
198
  // Allow iteration over the associated child chunks for this section.
199
707
  ArrayRef<SectionChunk *> children() const { return AssocChildren; }
200
201
  // A pointer pointing to a replacement for this chunk.
202
  // Initially it points to "this" object. If this chunk is merged
203
  // with other chunk by ICF, it points to another chunk,
204
  // and this chunk is considrered as dead.
205
  SectionChunk *Repl;
206
207
  // The CRC of the contents as described in the COFF spec 4.5.5.
208
  // Auxiliary Format 5: Section Definitions. Used for ICF.
209
  uint32_t Checksum = 0;
210
211
  const coff_section *Header;
212
213
  // The file that this chunk was created from.
214
  ObjFile *File;
215
216
private:
217
  StringRef SectionName;
218
  std::vector<SectionChunk *> AssocChildren;
219
  llvm::iterator_range<const coff_relocation *> Relocs;
220
  size_t NumRelocs;
221
222
  // True if this chunk was discarded because it was a duplicate comdat section.
223
  bool Discarded;
224
225
  // Used by the garbage collector.
226
  bool Live;
227
228
  // Used for ICF (Identical COMDAT Folding)
229
  void replace(SectionChunk *Other);
230
  uint32_t Class[2] = {0, 0};
231
232
  // Sym points to a section symbol if this is a COMDAT chunk.
233
  DefinedRegular *Sym = nullptr;
234
};
235
236
// A chunk for common symbols. Common chunks don't have actual data.
237
class CommonChunk : public Chunk {
238
public:
239
  CommonChunk(const COFFSymbolRef Sym);
240
10
  size_t getSize() const override { return Sym.getValue(); }
241
10
  bool hasData() const override { return false; }
242
  uint32_t getPermissions() const override;
243
10
  StringRef getSectionName() const override { return ".bss"; }
244
245
private:
246
  const COFFSymbolRef Sym;
247
};
248
249
// A chunk for linker-created strings.
250
class StringChunk : public Chunk {
251
public:
252
367
  explicit StringChunk(StringRef S) : Str(S) {}
253
154
  size_t getSize() const override { return Str.size() + 1; }
254
  void writeTo(uint8_t *Buf) const override;
255
256
private:
257
  StringRef Str;
258
};
259
260
static const uint8_t ImportThunkX86[] = {
261
    0xff, 0x25, 0x00, 0x00, 0x00, 0x00, // JMP *0x0
262
};
263
264
static const uint8_t ImportThunkARM[] = {
265
    0x40, 0xf2, 0x00, 0x0c, // mov.w ip, #0
266
    0xc0, 0xf2, 0x00, 0x0c, // mov.t ip, #0
267
    0xdc, 0xf8, 0x00, 0xf0, // ldr.w pc, [ip]
268
};
269
270
static const uint8_t ImportThunkARM64[] = {
271
    0x10, 0x00, 0x00, 0x90, // adrp x16, #0
272
    0x10, 0x02, 0x40, 0xf9, // ldr  x16, [x16]
273
    0x00, 0x02, 0x1f, 0xd6, // br   x16
274
};
275
276
// Windows-specific.
277
// A chunk for DLL import jump table entry. In a final output, it's
278
// contents will be a JMP instruction to some __imp_ symbol.
279
class ImportThunkChunkX64 : public Chunk {
280
public:
281
  explicit ImportThunkChunkX64(Defined *S);
282
96
  size_t getSize() const override { return sizeof(ImportThunkX86); }
283
  void writeTo(uint8_t *Buf) const override;
284
285
private:
286
  Defined *ImpSymbol;
287
};
288
289
class ImportThunkChunkX86 : public Chunk {
290
public:
291
12
  explicit ImportThunkChunkX86(Defined *S) : ImpSymbol(S) {}
292
14
  size_t getSize() const override { return sizeof(ImportThunkX86); }
293
  void getBaserels(std::vector<Baserel> *Res) override;
294
  void writeTo(uint8_t *Buf) const override;
295
296
private:
297
  Defined *ImpSymbol;
298
};
299
300
class ImportThunkChunkARM : public Chunk {
301
public:
302
2
  explicit ImportThunkChunkARM(Defined *S) : ImpSymbol(S) {}
303
2
  size_t getSize() const override { return sizeof(ImportThunkARM); }
304
  void getBaserels(std::vector<Baserel> *Res) override;
305
  void writeTo(uint8_t *Buf) const override;
306
307
private:
308
  Defined *ImpSymbol;
309
};
310
311
class ImportThunkChunkARM64 : public Chunk {
312
public:
313
3
  explicit ImportThunkChunkARM64(Defined *S) : ImpSymbol(S) {}
314
3
  size_t getSize() const override { return sizeof(ImportThunkARM64); }
315
  void writeTo(uint8_t *Buf) const override;
316
317
private:
318
  Defined *ImpSymbol;
319
};
320
321
// Windows-specific.
322
// See comments for DefinedLocalImport class.
323
class LocalImportChunk : public Chunk {
324
public:
325
2
  explicit LocalImportChunk(Defined *S) : Sym(S) {}
326
  size_t getSize() const override;
327
  void getBaserels(std::vector<Baserel> *Res) override;
328
  void writeTo(uint8_t *Buf) const override;
329
330
private:
331
  Defined *Sym;
332
};
333
334
// Windows-specific.
335
// A chunk for SEH table which contains RVAs of safe exception handler
336
// functions. x86-only.
337
class SEHTableChunk : public Chunk {
338
public:
339
2
  explicit SEHTableChunk(std::set<Defined *> S) : Syms(std::move(S)) {}
340
4
  size_t getSize() const override { return Syms.size() * 4; }
341
  void writeTo(uint8_t *Buf) const override;
342
343
private:
344
  std::set<Defined *> Syms;
345
};
346
347
// Windows-specific.
348
// This class represents a block in .reloc section.
349
// See the PE/COFF spec 5.6 for details.
350
class BaserelChunk : public Chunk {
351
public:
352
  BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End);
353
55
  size_t getSize() const override { return Data.size(); }
354
  void writeTo(uint8_t *Buf) const override;
355
356
private:
357
  std::vector<uint8_t> Data;
358
};
359
360
class Baserel {
361
public:
362
94
  Baserel(uint32_t V, uint8_t Ty) : RVA(V), Type(Ty) {}
363
24
  explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
lld::coff::Baserel::Baserel(unsigned int)
Line
Count
Source
363
24
  explicit Baserel(uint32_t V) : Baserel(V, getDefaultType()) {}
Unexecuted instantiation: lld::coff::Baserel::Baserel(unsigned int)
364
  uint8_t getDefaultType();
365
366
  uint32_t RVA;
367
  uint8_t Type;
368
};
369
370
void applyMOV32T(uint8_t *Off, uint32_t V);
371
void applyBranch24T(uint8_t *Off, int32_t V);
372
373
} // namespace coff
374
} // namespace lld
375
376
#endif