Coverage Report

Created: 2018-11-12 17:33

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Object/COFF.h
Line
Count
Source (jump to first uncovered line)
1
//===- COFF.h - COFF object file implementation -----------------*- C++ -*-===//
2
//
3
//                     The LLVM Compiler Infrastructure
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// This file declares the COFFObjectFile class.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_OBJECT_COFF_H
15
#define LLVM_OBJECT_COFF_H
16
17
#include "llvm/ADT/iterator_range.h"
18
#include "llvm/BinaryFormat/COFF.h"
19
#include "llvm/MC/SubtargetFeature.h"
20
#include "llvm/Object/Binary.h"
21
#include "llvm/Object/CVDebugRecord.h"
22
#include "llvm/Object/Error.h"
23
#include "llvm/Object/ObjectFile.h"
24
#include "llvm/Support/BinaryByteStream.h"
25
#include "llvm/Support/ConvertUTF.h"
26
#include "llvm/Support/Endian.h"
27
#include "llvm/Support/ErrorHandling.h"
28
#include <cassert>
29
#include <cstddef>
30
#include <cstdint>
31
#include <system_error>
32
33
namespace llvm {
34
35
template <typename T> class ArrayRef;
36
37
namespace object {
38
39
class BaseRelocRef;
40
class DelayImportDirectoryEntryRef;
41
class ExportDirectoryEntryRef;
42
class ImportDirectoryEntryRef;
43
class ImportedSymbolRef;
44
class ResourceSectionRef;
45
46
using import_directory_iterator = content_iterator<ImportDirectoryEntryRef>;
47
using delay_import_directory_iterator =
48
    content_iterator<DelayImportDirectoryEntryRef>;
49
using export_directory_iterator = content_iterator<ExportDirectoryEntryRef>;
50
using imported_symbol_iterator = content_iterator<ImportedSymbolRef>;
51
using base_reloc_iterator = content_iterator<BaseRelocRef>;
52
53
/// The DOS compatible header at the front of all PE/COFF executables.
54
struct dos_header {
55
  char                 Magic[2];
56
  support::ulittle16_t UsedBytesInTheLastPage;
57
  support::ulittle16_t FileSizeInPages;
58
  support::ulittle16_t NumberOfRelocationItems;
59
  support::ulittle16_t HeaderSizeInParagraphs;
60
  support::ulittle16_t MinimumExtraParagraphs;
61
  support::ulittle16_t MaximumExtraParagraphs;
62
  support::ulittle16_t InitialRelativeSS;
63
  support::ulittle16_t InitialSP;
64
  support::ulittle16_t Checksum;
65
  support::ulittle16_t InitialIP;
66
  support::ulittle16_t InitialRelativeCS;
67
  support::ulittle16_t AddressOfRelocationTable;
68
  support::ulittle16_t OverlayNumber;
69
  support::ulittle16_t Reserved[4];
70
  support::ulittle16_t OEMid;
71
  support::ulittle16_t OEMinfo;
72
  support::ulittle16_t Reserved2[10];
73
  support::ulittle32_t AddressOfNewExeHeader;
74
};
75
76
struct coff_file_header {
77
  support::ulittle16_t Machine;
78
  support::ulittle16_t NumberOfSections;
79
  support::ulittle32_t TimeDateStamp;
80
  support::ulittle32_t PointerToSymbolTable;
81
  support::ulittle32_t NumberOfSymbols;
82
  support::ulittle16_t SizeOfOptionalHeader;
83
  support::ulittle16_t Characteristics;
84
85
39.1k
  bool isImportLibrary() const { return NumberOfSections == 0xffff; }
86
};
87
88
struct coff_bigobj_file_header {
89
  support::ulittle16_t Sig1;
90
  support::ulittle16_t Sig2;
91
  support::ulittle16_t Version;
92
  support::ulittle16_t Machine;
93
  support::ulittle32_t TimeDateStamp;
94
  uint8_t              UUID[16];
95
  support::ulittle32_t unused1;
96
  support::ulittle32_t unused2;
97
  support::ulittle32_t unused3;
98
  support::ulittle32_t unused4;
99
  support::ulittle32_t NumberOfSections;
100
  support::ulittle32_t PointerToSymbolTable;
101
  support::ulittle32_t NumberOfSymbols;
102
};
103
104
/// The 32-bit PE header that follows the COFF header.
105
struct pe32_header {
106
  support::ulittle16_t Magic;
107
  uint8_t MajorLinkerVersion;
108
  uint8_t MinorLinkerVersion;
109
  support::ulittle32_t SizeOfCode;
110
  support::ulittle32_t SizeOfInitializedData;
111
  support::ulittle32_t SizeOfUninitializedData;
112
  support::ulittle32_t AddressOfEntryPoint;
113
  support::ulittle32_t BaseOfCode;
114
  support::ulittle32_t BaseOfData;
115
  support::ulittle32_t ImageBase;
116
  support::ulittle32_t SectionAlignment;
117
  support::ulittle32_t FileAlignment;
118
  support::ulittle16_t MajorOperatingSystemVersion;
119
  support::ulittle16_t MinorOperatingSystemVersion;
120
  support::ulittle16_t MajorImageVersion;
121
  support::ulittle16_t MinorImageVersion;
122
  support::ulittle16_t MajorSubsystemVersion;
123
  support::ulittle16_t MinorSubsystemVersion;
124
  support::ulittle32_t Win32VersionValue;
125
  support::ulittle32_t SizeOfImage;
126
  support::ulittle32_t SizeOfHeaders;
127
  support::ulittle32_t CheckSum;
128
  support::ulittle16_t Subsystem;
129
  // FIXME: This should be DllCharacteristics.
130
  support::ulittle16_t DLLCharacteristics;
131
  support::ulittle32_t SizeOfStackReserve;
132
  support::ulittle32_t SizeOfStackCommit;
133
  support::ulittle32_t SizeOfHeapReserve;
134
  support::ulittle32_t SizeOfHeapCommit;
135
  support::ulittle32_t LoaderFlags;
136
  // FIXME: This should be NumberOfRvaAndSizes.
137
  support::ulittle32_t NumberOfRvaAndSize;
138
};
139
140
/// The 64-bit PE header that follows the COFF header.
141
struct pe32plus_header {
142
  support::ulittle16_t Magic;
143
  uint8_t MajorLinkerVersion;
144
  uint8_t MinorLinkerVersion;
145
  support::ulittle32_t SizeOfCode;
146
  support::ulittle32_t SizeOfInitializedData;
147
  support::ulittle32_t SizeOfUninitializedData;
148
  support::ulittle32_t AddressOfEntryPoint;
149
  support::ulittle32_t BaseOfCode;
150
  support::ulittle64_t ImageBase;
151
  support::ulittle32_t SectionAlignment;
152
  support::ulittle32_t FileAlignment;
153
  support::ulittle16_t MajorOperatingSystemVersion;
154
  support::ulittle16_t MinorOperatingSystemVersion;
155
  support::ulittle16_t MajorImageVersion;
156
  support::ulittle16_t MinorImageVersion;
157
  support::ulittle16_t MajorSubsystemVersion;
158
  support::ulittle16_t MinorSubsystemVersion;
159
  support::ulittle32_t Win32VersionValue;
160
  support::ulittle32_t SizeOfImage;
161
  support::ulittle32_t SizeOfHeaders;
162
  support::ulittle32_t CheckSum;
163
  support::ulittle16_t Subsystem;
164
  support::ulittle16_t DLLCharacteristics;
165
  support::ulittle64_t SizeOfStackReserve;
166
  support::ulittle64_t SizeOfStackCommit;
167
  support::ulittle64_t SizeOfHeapReserve;
168
  support::ulittle64_t SizeOfHeapCommit;
169
  support::ulittle32_t LoaderFlags;
170
  support::ulittle32_t NumberOfRvaAndSize;
171
};
172
173
struct data_directory {
174
  support::ulittle32_t RelativeVirtualAddress;
175
  support::ulittle32_t Size;
176
};
177
178
struct debug_directory {
179
  support::ulittle32_t Characteristics;
180
  support::ulittle32_t TimeDateStamp;
181
  support::ulittle16_t MajorVersion;
182
  support::ulittle16_t MinorVersion;
183
  support::ulittle32_t Type;
184
  support::ulittle32_t SizeOfData;
185
  support::ulittle32_t AddressOfRawData;
186
  support::ulittle32_t PointerToRawData;
187
};
188
189
template <typename IntTy>
190
struct import_lookup_table_entry {
191
  IntTy Data;
192
193
190
  bool isOrdinal() const { return Data < 0; }
llvm::object::import_lookup_table_entry<llvm::support::detail::packed_endian_specific_integral<int, (llvm::support::endianness)1, 1ul> >::isOrdinal() const
Line
Count
Source
193
108
  bool isOrdinal() const { return Data < 0; }
llvm::object::import_lookup_table_entry<llvm::support::detail::packed_endian_specific_integral<long long, (llvm::support::endianness)1, 1ul> >::isOrdinal() const
Line
Count
Source
193
82
  bool isOrdinal() const { return Data < 0; }
194
195
7
  uint16_t getOrdinal() const {
196
7
    assert(isOrdinal() && "ILT entry is not an ordinal!");
197
7
    return Data & 0xFFFF;
198
7
  }
llvm::object::import_lookup_table_entry<llvm::support::detail::packed_endian_specific_integral<int, (llvm::support::endianness)1, 1ul> >::getOrdinal() const
Line
Count
Source
195
1
  uint16_t getOrdinal() const {
196
1
    assert(isOrdinal() && "ILT entry is not an ordinal!");
197
1
    return Data & 0xFFFF;
198
1
  }
llvm::object::import_lookup_table_entry<llvm::support::detail::packed_endian_specific_integral<long long, (llvm::support::endianness)1, 1ul> >::getOrdinal() const
Line
Count
Source
195
6
  uint16_t getOrdinal() const {
196
6
    assert(isOrdinal() && "ILT entry is not an ordinal!");
197
6
    return Data & 0xFFFF;
198
6
  }
199
200
176
  uint32_t getHintNameRVA() const {
201
176
    assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
202
176
    return Data & 0xFFFFFFFF;
203
176
  }
llvm::object::import_lookup_table_entry<llvm::support::detail::packed_endian_specific_integral<int, (llvm::support::endianness)1, 1ul> >::getHintNameRVA() const
Line
Count
Source
200
106
  uint32_t getHintNameRVA() const {
201
106
    assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
202
106
    return Data & 0xFFFFFFFF;
203
106
  }
llvm::object::import_lookup_table_entry<llvm::support::detail::packed_endian_specific_integral<long long, (llvm::support::endianness)1, 1ul> >::getHintNameRVA() const
Line
Count
Source
200
70
  uint32_t getHintNameRVA() const {
201
70
    assert(!isOrdinal() && "ILT entry is not a Hint/Name RVA!");
202
70
    return Data & 0xFFFFFFFF;
203
70
  }
204
};
205
206
using import_lookup_table_entry32 =
207
    import_lookup_table_entry<support::little32_t>;
208
using import_lookup_table_entry64 =
209
    import_lookup_table_entry<support::little64_t>;
210
211
struct delay_import_directory_table_entry {
212
  // dumpbin reports this field as "Characteristics" instead of "Attributes".
213
  support::ulittle32_t Attributes;
214
  support::ulittle32_t Name;
215
  support::ulittle32_t ModuleHandle;
216
  support::ulittle32_t DelayImportAddressTable;
217
  support::ulittle32_t DelayImportNameTable;
218
  support::ulittle32_t BoundDelayImportTable;
219
  support::ulittle32_t UnloadDelayImportTable;
220
  support::ulittle32_t TimeStamp;
221
};
222
223
struct export_directory_table_entry {
224
  support::ulittle32_t ExportFlags;
225
  support::ulittle32_t TimeDateStamp;
226
  support::ulittle16_t MajorVersion;
227
  support::ulittle16_t MinorVersion;
228
  support::ulittle32_t NameRVA;
229
  support::ulittle32_t OrdinalBase;
230
  support::ulittle32_t AddressTableEntries;
231
  support::ulittle32_t NumberOfNamePointers;
232
  support::ulittle32_t ExportAddressTableRVA;
233
  support::ulittle32_t NamePointerRVA;
234
  support::ulittle32_t OrdinalTableRVA;
235
};
236
237
union export_address_table_entry {
238
  support::ulittle32_t ExportRVA;
239
  support::ulittle32_t ForwarderRVA;
240
};
241
242
using export_name_pointer_table_entry = support::ulittle32_t;
243
using export_ordinal_table_entry = support::ulittle16_t;
244
245
struct StringTableOffset {
246
  support::ulittle32_t Zeroes;
247
  support::ulittle32_t Offset;
248
};
249
250
template <typename SectionNumberType>
251
struct coff_symbol {
252
  union {
253
    char ShortName[COFF::NameSize];
254
    StringTableOffset Offset;
255
  } Name;
256
257
  support::ulittle32_t Value;
258
  SectionNumberType SectionNumber;
259
260
  support::ulittle16_t Type;
261
262
  uint8_t StorageClass;
263
  uint8_t NumberOfAuxSymbols;
264
};
265
266
using coff_symbol16 = coff_symbol<support::ulittle16_t>;
267
using coff_symbol32 = coff_symbol<support::ulittle32_t>;
268
269
// Contains only common parts of coff_symbol16 and coff_symbol32.
270
struct coff_symbol_generic {
271
  union {
272
    char ShortName[COFF::NameSize];
273
    StringTableOffset Offset;
274
  } Name;
275
  support::ulittle32_t Value;
276
};
277
278
struct coff_aux_section_definition;
279
struct coff_aux_weak_external;
280
281
class COFFSymbolRef {
282
public:
283
  COFFSymbolRef() = default;
284
19.5k
  COFFSymbolRef(const coff_symbol16 *CS) : CS16(CS) {}
285
4
  COFFSymbolRef(const coff_symbol32 *CS) : CS32(CS) {}
286
287
377
  const void *getRawPtr() const {
288
377
    return CS16 ? 
static_cast<const void *>(CS16)373
:
CS324
;
289
377
  }
290
291
9.37k
  const coff_symbol_generic *getGeneric() const {
292
9.37k
    if (CS16)
293
9.36k
      return reinterpret_cast<const coff_symbol_generic *>(CS16);
294
4
    return reinterpret_cast<const coff_symbol_generic *>(CS32);
295
4
  }
296
297
0
  friend bool operator<(COFFSymbolRef A, COFFSymbolRef B) {
298
0
    return A.getRawPtr() < B.getRawPtr();
299
0
  }
300
301
  bool isBigObj() const {
302
    if (CS16)
303
      return false;
304
    if (CS32)
305
      return true;
306
    llvm_unreachable("COFFSymbolRef points to nothing!");
307
  }
308
309
0
  const char *getShortName() const {
310
0
    return CS16 ? CS16->Name.ShortName : CS32->Name.ShortName;
311
0
  }
312
313
0
  const StringTableOffset &getStringTableOffset() const {
314
0
    assert(isSet() && "COFFSymbolRef points to nothing!");
315
0
    return CS16 ? CS16->Name.Offset : CS32->Name.Offset;
316
0
  }
317
318
3.71k
  uint32_t getValue() const { return CS16 ? 
CS16->Value3.71k
:
CS32->Value4
; }
319
320
37.6k
  int32_t getSectionNumber() const {
321
37.6k
    assert(isSet() && "COFFSymbolRef points to nothing!");
322
37.6k
    if (CS16) {
323
37.6k
      // Reserved sections are returned as negative numbers.
324
37.6k
      if (CS16->SectionNumber <= COFF::MaxNumberOfSections16)
325
36.7k
        return CS16->SectionNumber;
326
894
      return static_cast<int16_t>(CS16->SectionNumber);
327
894
    }
328
12
    return static_cast<int32_t>(CS32->SectionNumber);
329
12
  }
330
331
2.10k
  uint16_t getType() const {
332
2.10k
    assert(isSet() && "COFFSymbolRef points to nothing!");
333
2.10k
    return CS16 ? 
CS16->Type2.09k
:
CS32->Type8
;
334
2.10k
  }
335
336
67.5k
  uint8_t getStorageClass() const {
337
67.5k
    assert(isSet() && "COFFSymbolRef points to nothing!");
338
67.5k
    return CS16 ? 
CS16->StorageClass67.5k
:
CS32->StorageClass26
;
339
67.5k
  }
340
341
20.7k
  uint8_t getNumberOfAuxSymbols() const {
342
20.7k
    assert(isSet() && "COFFSymbolRef points to nothing!");
343
20.7k
    return CS16 ? 
CS16->NumberOfAuxSymbols20.6k
:
CS32->NumberOfAuxSymbols23
;
344
20.7k
  }
345
346
  uint8_t getBaseType() const { return getType() & 0x0F; }
347
348
964
  uint8_t getComplexType() const {
349
964
    return (getType() & 0xF0) >> COFF::SCT_COMPLEX_TYPE_SHIFT;
350
964
  }
351
352
77
  template <typename T> const T *getAux() const {
353
77
    return CS16 ? reinterpret_cast<const T *>(CS16 + 1)
354
77
                : 
reinterpret_cast<const T *>(CS32 + 1)0
;
355
77
  }
356
357
  const coff_aux_section_definition *getSectionDefinition() const {
358
    if (!getNumberOfAuxSymbols() ||
359
        getStorageClass() != COFF::IMAGE_SYM_CLASS_STATIC)
360
      return nullptr;
361
    return getAux<coff_aux_section_definition>();
362
  }
363
364
6.28k
  const coff_aux_weak_external *getWeakExternal() const {
365
6.28k
    if (!getNumberOfAuxSymbols() ||
366
6.28k
        
getStorageClass() != COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL2.12k
)
367
6.21k
      return nullptr;
368
68
    return getAux<coff_aux_weak_external>();
369
68
  }
370
371
  bool isAbsolute() const {
372
    return getSectionNumber() == -1;
373
  }
374
375
34.1k
  bool isExternal() const {
376
34.1k
    return getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL;
377
34.1k
  }
378
379
11.3k
  bool isCommon() const {
380
11.3k
    return isExternal() && 
getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED4.43k
&&
381
11.3k
           
getValue() != 0768
;
382
11.3k
  }
383
384
12.0k
  bool isUndefined() const {
385
12.0k
    return isExternal() && 
getSectionNumber() == COFF::IMAGE_SYM_UNDEFINED4.78k
&&
386
12.0k
           
getValue() == 01.11k
;
387
12.0k
  }
388
389
9.06k
  bool isWeakExternal() const {
390
9.06k
    return getStorageClass() == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
391
9.06k
  }
392
393
  bool isFunctionDefinition() const {
394
    return isExternal() && getBaseType() == COFF::IMAGE_SYM_TYPE_NULL &&
395
           getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION &&
396
           !COFF::isReservedSectionNumber(getSectionNumber());
397
  }
398
399
  bool isFunctionLineInfo() const {
400
    return getStorageClass() == COFF::IMAGE_SYM_CLASS_FUNCTION;
401
  }
402
403
1.26k
  bool isAnyUndefined() const {
404
1.26k
    return isUndefined() || 
isWeakExternal()1.19k
;
405
1.26k
  }
406
407
6.75k
  bool isFileRecord() const {
408
6.75k
    return getStorageClass() == COFF::IMAGE_SYM_CLASS_FILE;
409
6.75k
  }
410
411
0
  bool isSection() const {
412
0
    return getStorageClass() == COFF::IMAGE_SYM_CLASS_SECTION;
413
0
  }
414
415
6.86k
  bool isSectionDefinition() const {
416
6.86k
    // C++/CLI creates external ABS symbols for non-const appdomain globals.
417
6.86k
    // These are also followed by an auxiliary section definition.
418
6.86k
    bool isAppdomainGlobal =
419
6.86k
        getStorageClass() == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
420
6.86k
        
getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE2.98k
;
421
6.86k
    bool isOrdinarySection = getStorageClass() == COFF::IMAGE_SYM_CLASS_STATIC;
422
6.86k
    if (!getNumberOfAuxSymbols())
423
4.28k
      return false;
424
2.57k
    return isAppdomainGlobal || 
isOrdinarySection2.57k
;
425
2.57k
  }
426
427
  bool isCLRToken() const {
428
    return getStorageClass() == COFF::IMAGE_SYM_CLASS_CLR_TOKEN;
429
  }
430
431
private:
432
0
  bool isSet() const { return CS16 || CS32; }
433
434
  const coff_symbol16 *CS16 = nullptr;
435
  const coff_symbol32 *CS32 = nullptr;
436
};
437
438
struct coff_section {
439
  char Name[COFF::NameSize];
440
  support::ulittle32_t VirtualSize;
441
  support::ulittle32_t VirtualAddress;
442
  support::ulittle32_t SizeOfRawData;
443
  support::ulittle32_t PointerToRawData;
444
  support::ulittle32_t PointerToRelocations;
445
  support::ulittle32_t PointerToLinenumbers;
446
  support::ulittle16_t NumberOfRelocations;
447
  support::ulittle16_t NumberOfLinenumbers;
448
  support::ulittle32_t Characteristics;
449
450
  // Returns true if the actual number of relocations is stored in
451
  // VirtualAddress field of the first relocation table entry.
452
9.73k
  bool hasExtendedRelocations() const {
453
9.73k
    return (Characteristics & COFF::IMAGE_SCN_LNK_NRELOC_OVFL) &&
454
9.73k
           
NumberOfRelocations == UINT16_MAX10
;
455
9.73k
  }
456
457
2.15k
  uint32_t getAlignment() const {
458
2.15k
    // The IMAGE_SCN_TYPE_NO_PAD bit is a legacy way of getting to
459
2.15k
    // IMAGE_SCN_ALIGN_1BYTES.
460
2.15k
    if (Characteristics & COFF::IMAGE_SCN_TYPE_NO_PAD)
461
0
      return 1;
462
2.15k
463
2.15k
    // Bit [20:24] contains section alignment. 0 means use a default alignment
464
2.15k
    // of 16.
465
2.15k
    uint32_t Shift = (Characteristics >> 20) & 0xF;
466
2.15k
    if (Shift > 0)
467
2.14k
      return 1U << (Shift - 1);
468
10
    return 16;
469
10
  }
470
};
471
472
struct coff_relocation {
473
  support::ulittle32_t VirtualAddress;
474
  support::ulittle32_t SymbolTableIndex;
475
  support::ulittle16_t Type;
476
};
477
478
struct coff_aux_function_definition {
479
  support::ulittle32_t TagIndex;
480
  support::ulittle32_t TotalSize;
481
  support::ulittle32_t PointerToLinenumber;
482
  support::ulittle32_t PointerToNextFunction;
483
  char Unused1[2];
484
};
485
486
static_assert(sizeof(coff_aux_function_definition) == 18,
487
              "auxiliary entry must be 18 bytes");
488
489
struct coff_aux_bf_and_ef_symbol {
490
  char Unused1[4];
491
  support::ulittle16_t Linenumber;
492
  char Unused2[6];
493
  support::ulittle32_t PointerToNextFunction;
494
  char Unused3[2];
495
};
496
497
static_assert(sizeof(coff_aux_bf_and_ef_symbol) == 18,
498
              "auxiliary entry must be 18 bytes");
499
500
struct coff_aux_weak_external {
501
  support::ulittle32_t TagIndex;
502
  support::ulittle32_t Characteristics;
503
  char Unused1[10];
504
};
505
506
static_assert(sizeof(coff_aux_weak_external) == 18,
507
              "auxiliary entry must be 18 bytes");
508
509
struct coff_aux_section_definition {
510
  support::ulittle32_t Length;
511
  support::ulittle16_t NumberOfRelocations;
512
  support::ulittle16_t NumberOfLinenumbers;
513
  support::ulittle32_t CheckSum;
514
  support::ulittle16_t NumberLowPart;
515
  uint8_t              Selection;
516
  uint8_t              Unused;
517
  support::ulittle16_t NumberHighPart;
518
  int32_t getNumber(bool IsBigObj) const {
519
    uint32_t Number = static_cast<uint32_t>(NumberLowPart);
520
    if (IsBigObj)
521
      Number |= static_cast<uint32_t>(NumberHighPart) << 16;
522
    return static_cast<int32_t>(Number);
523
  }
524
};
525
526
static_assert(sizeof(coff_aux_section_definition) == 18,
527
              "auxiliary entry must be 18 bytes");
528
529
struct coff_aux_clr_token {
530
  uint8_t              AuxType;
531
  uint8_t              Reserved;
532
  support::ulittle32_t SymbolTableIndex;
533
  char                 MBZ[12];
534
};
535
536
static_assert(sizeof(coff_aux_clr_token) == 18,
537
              "auxiliary entry must be 18 bytes");
538
539
struct coff_import_header {
540
  support::ulittle16_t Sig1;
541
  support::ulittle16_t Sig2;
542
  support::ulittle16_t Version;
543
  support::ulittle16_t Machine;
544
  support::ulittle32_t TimeDateStamp;
545
  support::ulittle32_t SizeOfData;
546
  support::ulittle16_t OrdinalHint;
547
  support::ulittle16_t TypeInfo;
548
549
565
  int getType() const { return TypeInfo & 0x3; }
550
  int getNameType() const { return (TypeInfo >> 2) & 0x7; }
551
};
552
553
struct coff_import_directory_table_entry {
554
  support::ulittle32_t ImportLookupTableRVA;
555
  support::ulittle32_t TimeDateStamp;
556
  support::ulittle32_t ForwarderChain;
557
  support::ulittle32_t NameRVA;
558
  support::ulittle32_t ImportAddressTableRVA;
559
560
50
  bool isNull() const {
561
50
    return ImportLookupTableRVA == 0 && 
TimeDateStamp == 020
&&
562
50
           
ForwarderChain == 020
&&
NameRVA == 020
&&
ImportAddressTableRVA == 020
;
563
50
  }
564
};
565
566
template <typename IntTy>
567
struct coff_tls_directory {
568
  IntTy StartAddressOfRawData;
569
  IntTy EndAddressOfRawData;
570
  IntTy AddressOfIndex;
571
  IntTy AddressOfCallBacks;
572
  support::ulittle32_t SizeOfZeroFill;
573
  support::ulittle32_t Characteristics;
574
575
  uint32_t getAlignment() const {
576
    // Bit [20:24] contains section alignment.
577
    uint32_t Shift = (Characteristics & 0x00F00000) >> 20;
578
    if (Shift > 0)
579
      return 1U << (Shift - 1);
580
    return 0;
581
  }
582
};
583
584
using coff_tls_directory32 = coff_tls_directory<support::little32_t>;
585
using coff_tls_directory64 = coff_tls_directory<support::little64_t>;
586
587
/// Bits in control flow guard flags as we understand them.
588
enum class coff_guard_flags : uint32_t {
589
  CFInstrumented = 0x00000100,
590
  HasFidTable = 0x00000400,
591
  ProtectDelayLoadIAT = 0x00001000,
592
  DelayLoadIATSection = 0x00002000, // Delay load in separate section
593
  HasLongJmpTable = 0x00010000,
594
  FidTableHasFlags = 0x10000000, // Indicates that fid tables are 5 bytes
595
};
596
597
enum class frame_type : uint16_t { Fpo = 0, Trap = 1, Tss = 2, NonFpo = 3 };
598
599
struct coff_load_config_code_integrity {
600
  support::ulittle16_t Flags;
601
  support::ulittle16_t Catalog;
602
  support::ulittle32_t CatalogOffset;
603
  support::ulittle32_t Reserved;
604
};
605
606
/// 32-bit load config (IMAGE_LOAD_CONFIG_DIRECTORY32)
607
struct coff_load_configuration32 {
608
  support::ulittle32_t Size;
609
  support::ulittle32_t TimeDateStamp;
610
  support::ulittle16_t MajorVersion;
611
  support::ulittle16_t MinorVersion;
612
  support::ulittle32_t GlobalFlagsClear;
613
  support::ulittle32_t GlobalFlagsSet;
614
  support::ulittle32_t CriticalSectionDefaultTimeout;
615
  support::ulittle32_t DeCommitFreeBlockThreshold;
616
  support::ulittle32_t DeCommitTotalFreeThreshold;
617
  support::ulittle32_t LockPrefixTable;
618
  support::ulittle32_t MaximumAllocationSize;
619
  support::ulittle32_t VirtualMemoryThreshold;
620
  support::ulittle32_t ProcessAffinityMask;
621
  support::ulittle32_t ProcessHeapFlags;
622
  support::ulittle16_t CSDVersion;
623
  support::ulittle16_t DependentLoadFlags;
624
  support::ulittle32_t EditList;
625
  support::ulittle32_t SecurityCookie;
626
  support::ulittle32_t SEHandlerTable;
627
  support::ulittle32_t SEHandlerCount;
628
629
  // Added in MSVC 2015 for /guard:cf.
630
  support::ulittle32_t GuardCFCheckFunction;
631
  support::ulittle32_t GuardCFCheckDispatch;
632
  support::ulittle32_t GuardCFFunctionTable;
633
  support::ulittle32_t GuardCFFunctionCount;
634
  support::ulittle32_t GuardFlags; // coff_guard_flags
635
636
  // Added in MSVC 2017
637
  coff_load_config_code_integrity CodeIntegrity;
638
  support::ulittle32_t GuardAddressTakenIatEntryTable;
639
  support::ulittle32_t GuardAddressTakenIatEntryCount;
640
  support::ulittle32_t GuardLongJumpTargetTable;
641
  support::ulittle32_t GuardLongJumpTargetCount;
642
  support::ulittle32_t DynamicValueRelocTable;
643
  support::ulittle32_t CHPEMetadataPointer;
644
  support::ulittle32_t GuardRFFailureRoutine;
645
  support::ulittle32_t GuardRFFailureRoutineFunctionPointer;
646
  support::ulittle32_t DynamicValueRelocTableOffset;
647
  support::ulittle16_t DynamicValueRelocTableSection;
648
  support::ulittle16_t Reserved2;
649
  support::ulittle32_t GuardRFVerifyStackPointerFunctionPointer;
650
  support::ulittle32_t HotPatchTableOffset;
651
};
652
653
/// 64-bit load config (IMAGE_LOAD_CONFIG_DIRECTORY64)
654
struct coff_load_configuration64 {
655
  support::ulittle32_t Size;
656
  support::ulittle32_t TimeDateStamp;
657
  support::ulittle16_t MajorVersion;
658
  support::ulittle16_t MinorVersion;
659
  support::ulittle32_t GlobalFlagsClear;
660
  support::ulittle32_t GlobalFlagsSet;
661
  support::ulittle32_t CriticalSectionDefaultTimeout;
662
  support::ulittle64_t DeCommitFreeBlockThreshold;
663
  support::ulittle64_t DeCommitTotalFreeThreshold;
664
  support::ulittle64_t LockPrefixTable;
665
  support::ulittle64_t MaximumAllocationSize;
666
  support::ulittle64_t VirtualMemoryThreshold;
667
  support::ulittle64_t ProcessAffinityMask;
668
  support::ulittle32_t ProcessHeapFlags;
669
  support::ulittle16_t CSDVersion;
670
  support::ulittle16_t DependentLoadFlags;
671
  support::ulittle64_t EditList;
672
  support::ulittle64_t SecurityCookie;
673
  support::ulittle64_t SEHandlerTable;
674
  support::ulittle64_t SEHandlerCount;
675
676
  // Added in MSVC 2015 for /guard:cf.
677
  support::ulittle64_t GuardCFCheckFunction;
678
  support::ulittle64_t GuardCFCheckDispatch;
679
  support::ulittle64_t GuardCFFunctionTable;
680
  support::ulittle64_t GuardCFFunctionCount;
681
  support::ulittle32_t GuardFlags;
682
683
  // Added in MSVC 2017
684
  coff_load_config_code_integrity CodeIntegrity;
685
  support::ulittle64_t GuardAddressTakenIatEntryTable;
686
  support::ulittle64_t GuardAddressTakenIatEntryCount;
687
  support::ulittle64_t GuardLongJumpTargetTable;
688
  support::ulittle64_t GuardLongJumpTargetCount;
689
  support::ulittle64_t DynamicValueRelocTable;
690
  support::ulittle64_t CHPEMetadataPointer;
691
  support::ulittle64_t GuardRFFailureRoutine;
692
  support::ulittle64_t GuardRFFailureRoutineFunctionPointer;
693
  support::ulittle32_t DynamicValueRelocTableOffset;
694
  support::ulittle16_t DynamicValueRelocTableSection;
695
  support::ulittle16_t Reserved2;
696
  support::ulittle64_t GuardRFVerifyStackPointerFunctionPointer;
697
  support::ulittle32_t HotPatchTableOffset;
698
};
699
700
struct coff_runtime_function_x64 {
701
  support::ulittle32_t BeginAddress;
702
  support::ulittle32_t EndAddress;
703
  support::ulittle32_t UnwindInformation;
704
};
705
706
struct coff_base_reloc_block_header {
707
  support::ulittle32_t PageRVA;
708
  support::ulittle32_t BlockSize;
709
};
710
711
struct coff_base_reloc_block_entry {
712
  support::ulittle16_t Data;
713
714
102
  int getType() const { return Data >> 12; }
715
102
  int getOffset() const { return Data & ((1 << 12) - 1); }
716
};
717
718
struct coff_resource_dir_entry {
719
  union {
720
    support::ulittle32_t NameOffset;
721
    support::ulittle32_t ID;
722
28
    uint32_t getNameOffset() const {
723
28
      return maskTrailingOnes<uint32_t>(31) & NameOffset;
724
28
    }
725
    // Even though the PE/COFF spec doesn't mention this, the high bit of a name
726
    // offset is set.
727
72
    void setNameOffset(uint32_t Offset) { NameOffset = Offset | (1 << 31); }
728
  } Identifier;
729
  union {
730
    support::ulittle32_t DataEntryOffset;
731
    support::ulittle32_t SubdirOffset;
732
733
    bool isSubDir() const { return SubdirOffset >> 31; }
734
229
    uint32_t value() const {
735
229
      return maskTrailingOnes<uint32_t>(31) & SubdirOffset;
736
229
    }
737
738
  } Offset;
739
};
740
741
struct coff_resource_data_entry {
742
  support::ulittle32_t DataRVA;
743
  support::ulittle32_t DataSize;
744
  support::ulittle32_t Codepage;
745
  support::ulittle32_t Reserved;
746
};
747
748
struct coff_resource_dir_table {
749
  support::ulittle32_t Characteristics;
750
  support::ulittle32_t TimeDateStamp;
751
  support::ulittle16_t MajorVersion;
752
  support::ulittle16_t MinorVersion;
753
  support::ulittle16_t NumberOfNameEntries;
754
  support::ulittle16_t NumberOfIDEntries;
755
};
756
757
struct debug_h_header {
758
  support::ulittle32_t Magic;
759
  support::ulittle16_t Version;
760
  support::ulittle16_t HashAlgorithm;
761
};
762
763
class COFFObjectFile : public ObjectFile {
764
private:
765
  friend class ImportDirectoryEntryRef;
766
  friend class ExportDirectoryEntryRef;
767
  const coff_file_header *COFFHeader;
768
  const coff_bigobj_file_header *COFFBigObjHeader;
769
  const pe32_header *PE32Header;
770
  const pe32plus_header *PE32PlusHeader;
771
  const data_directory *DataDirectory;
772
  const coff_section *SectionTable;
773
  const coff_symbol16 *SymbolTable16;
774
  const coff_symbol32 *SymbolTable32;
775
  const char *StringTable;
776
  uint32_t StringTableSize;
777
  const coff_import_directory_table_entry *ImportDirectory;
778
  const delay_import_directory_table_entry *DelayImportDirectory;
779
  uint32_t NumberOfDelayImportDirectory;
780
  const export_directory_table_entry *ExportDirectory;
781
  const coff_base_reloc_block_header *BaseRelocHeader;
782
  const coff_base_reloc_block_header *BaseRelocEnd;
783
  const debug_directory *DebugDirectoryBegin;
784
  const debug_directory *DebugDirectoryEnd;
785
  // Either coff_load_configuration32 or coff_load_configuration64.
786
  const void *LoadConfig = nullptr;
787
788
  std::error_code getString(uint32_t offset, StringRef &Res) const;
789
790
  template <typename coff_symbol_type>
791
  const coff_symbol_type *toSymb(DataRefImpl Symb) const;
792
  const coff_section *toSec(DataRefImpl Sec) const;
793
  const coff_relocation *toRel(DataRefImpl Rel) const;
794
795
  std::error_code initSymbolTablePtr();
796
  std::error_code initImportTablePtr();
797
  std::error_code initDelayImportTablePtr();
798
  std::error_code initExportTablePtr();
799
  std::error_code initBaseRelocPtr();
800
  std::error_code initDebugDirectoryPtr();
801
  std::error_code initLoadConfigPtr();
802
803
public:
804
5.73k
  uintptr_t getSymbolTable() const {
805
5.73k
    if (SymbolTable16)
806
5.69k
      return reinterpret_cast<uintptr_t>(SymbolTable16);
807
42
    if (SymbolTable32)
808
1
      return reinterpret_cast<uintptr_t>(SymbolTable32);
809
41
    return uintptr_t(0);
810
41
  }
811
812
154k
  uint16_t getMachine() const {
813
154k
    if (COFFHeader)
814
154k
      return COFFHeader->Machine;
815
4
    if (COFFBigObjHeader)
816
4
      return COFFBigObjHeader->Machine;
817
0
    llvm_unreachable("no COFF header!");
818
0
  }
819
820
  uint16_t getSizeOfOptionalHeader() const {
821
    if (COFFHeader)
822
      return COFFHeader->isImportLibrary() ? 0
823
                                           : COFFHeader->SizeOfOptionalHeader;
824
    // bigobj doesn't have this field.
825
    if (COFFBigObjHeader)
826
      return 0;
827
    llvm_unreachable("no COFF header!");
828
  }
829
830
  uint16_t getCharacteristics() const {
831
    if (COFFHeader)
832
      return COFFHeader->isImportLibrary() ? 0 : COFFHeader->Characteristics;
833
    // bigobj doesn't have characteristics to speak of,
834
    // editbin will silently lie to you if you attempt to set any.
835
    if (COFFBigObjHeader)
836
      return 0;
837
    llvm_unreachable("no COFF header!");
838
  }
839
840
  uint32_t getTimeDateStamp() const {
841
    if (COFFHeader)
842
      return COFFHeader->TimeDateStamp;
843
    if (COFFBigObjHeader)
844
      return COFFBigObjHeader->TimeDateStamp;
845
    llvm_unreachable("no COFF header!");
846
  }
847
848
16.8k
  uint32_t getNumberOfSections() const {
849
16.8k
    if (COFFHeader)
850
16.8k
      return COFFHeader->isImportLibrary() ? 
00
: COFFHeader->NumberOfSections;
851
7
    if (COFFBigObjHeader)
852
7
      return COFFBigObjHeader->NumberOfSections;
853
0
    llvm_unreachable("no COFF header!");
854
0
  }
855
856
5.35k
  uint32_t getPointerToSymbolTable() const {
857
5.35k
    if (COFFHeader)
858
5.34k
      return COFFHeader->isImportLibrary() ? 
00
859
5.34k
                                           : COFFHeader->PointerToSymbolTable;
860
4
    if (COFFBigObjHeader)
861
4
      return COFFBigObjHeader->PointerToSymbolTable;
862
0
    llvm_unreachable("no COFF header!");
863
0
  }
864
865
8.77k
  uint32_t getRawNumberOfSymbols() const {
866
8.77k
    if (COFFHeader)
867
8.76k
      return COFFHeader->isImportLibrary() ? 
00
: COFFHeader->NumberOfSymbols;
868
2
    if (COFFBigObjHeader)
869
2
      return COFFBigObjHeader->NumberOfSymbols;
870
0
    llvm_unreachable("no COFF header!");
871
0
  }
872
873
10.8k
  uint32_t getNumberOfSymbols() const {
874
10.8k
    if (!SymbolTable16 && 
!SymbolTable322.09k
)
875
2.09k
      return 0;
876
8.77k
    return getRawNumberOfSymbols();
877
8.77k
  }
878
879
  const coff_load_configuration32 *getLoadConfig32() const {
880
    assert(!is64());
881
    return reinterpret_cast<const coff_load_configuration32 *>(LoadConfig);
882
  }
883
884
  const coff_load_configuration64 *getLoadConfig64() const {
885
    assert(is64());
886
    return reinterpret_cast<const coff_load_configuration64 *>(LoadConfig);
887
  }
888
  StringRef getRelocationTypeName(uint16_t Type) const;
889
890
protected:
891
  void moveSymbolNext(DataRefImpl &Symb) const override;
892
  Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
893
  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
894
  uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
895
  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
896
  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
897
  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
898
  Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override;
899
  Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override;
900
  void moveSectionNext(DataRefImpl &Sec) const override;
901
  std::error_code getSectionName(DataRefImpl Sec,
902
                                 StringRef &Res) const override;
903
  uint64_t getSectionAddress(DataRefImpl Sec) const override;
904
  uint64_t getSectionIndex(DataRefImpl Sec) const override;
905
  uint64_t getSectionSize(DataRefImpl Sec) const override;
906
  std::error_code getSectionContents(DataRefImpl Sec,
907
                                     StringRef &Res) const override;
908
  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
909
  bool isSectionCompressed(DataRefImpl Sec) const override;
910
  bool isSectionText(DataRefImpl Sec) const override;
911
  bool isSectionData(DataRefImpl Sec) const override;
912
  bool isSectionBSS(DataRefImpl Sec) const override;
913
  bool isSectionVirtual(DataRefImpl Sec) const override;
914
  relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
915
  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
916
917
  void moveRelocationNext(DataRefImpl &Rel) const override;
918
  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
919
  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
920
  uint64_t getRelocationType(DataRefImpl Rel) const override;
921
  void getRelocationTypeName(DataRefImpl Rel,
922
                             SmallVectorImpl<char> &Result) const override;
923
924
public:
925
  COFFObjectFile(MemoryBufferRef Object, std::error_code &EC);
926
927
  basic_symbol_iterator symbol_begin() const override;
928
  basic_symbol_iterator symbol_end() const override;
929
  section_iterator section_begin() const override;
930
  section_iterator section_end() const override;
931
932
  const coff_section *getCOFFSection(const SectionRef &Section) const;
933
  COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const;
934
  COFFSymbolRef getCOFFSymbol(const SymbolRef &Symbol) const;
935
  const coff_relocation *getCOFFRelocation(const RelocationRef &Reloc) const;
936
  unsigned getSectionID(SectionRef Sec) const;
937
  unsigned getSymbolSectionID(SymbolRef Sym) const;
938
939
  uint8_t getBytesInAddress() const override;
940
  StringRef getFileFormatName() const override;
941
  Triple::ArchType getArch() const override;
942
  Expected<uint64_t> getStartAddress() const override;
943
61
  SubtargetFeatures getFeatures() const override { return SubtargetFeatures(); }
944
945
  import_directory_iterator import_directory_begin() const;
946
  import_directory_iterator import_directory_end() const;
947
  delay_import_directory_iterator delay_import_directory_begin() const;
948
  delay_import_directory_iterator delay_import_directory_end() const;
949
  export_directory_iterator export_directory_begin() const;
950
  export_directory_iterator export_directory_end() const;
951
  base_reloc_iterator base_reloc_begin() const;
952
  base_reloc_iterator base_reloc_end() const;
953
21
  const debug_directory *debug_directory_begin() const {
954
21
    return DebugDirectoryBegin;
955
21
  }
956
21
  const debug_directory *debug_directory_end() const {
957
21
    return DebugDirectoryEnd;
958
21
  }
959
960
  iterator_range<import_directory_iterator> import_directories() const;
961
  iterator_range<delay_import_directory_iterator>
962
      delay_import_directories() const;
963
  iterator_range<export_directory_iterator> export_directories() const;
964
  iterator_range<base_reloc_iterator> base_relocs() const;
965
21
  iterator_range<const debug_directory *> debug_directories() const {
966
21
    return make_range(debug_directory_begin(), debug_directory_end());
967
21
  }
968
969
153k
  const dos_header *getDOSHeader() const {
970
153k
    if (!PE32Header && 
!PE32PlusHeader153k
)
971
3.40k
      return nullptr;
972
150k
    return reinterpret_cast<const dos_header *>(base());
973
150k
  }
974
  std::error_code getPE32Header(const pe32_header *&Res) const;
975
  std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const;
976
  std::error_code getDataDirectory(uint32_t index,
977
                                   const data_directory *&Res) const;
978
  std::error_code getSection(int32_t index, const coff_section *&Res) const;
979
  std::error_code getSection(StringRef SectionName,
980
                             const coff_section *&Res) const;
981
982
  template <typename coff_symbol_type>
983
  std::error_code getSymbol(uint32_t Index,
984
                            const coff_symbol_type *&Res) const {
985
    if (Index >= getNumberOfSymbols())
986
      return object_error::parse_failed;
987
988
    Res = reinterpret_cast<coff_symbol_type *>(getSymbolTable()) + Index;
989
    return std::error_code();
990
  }
991
  Expected<COFFSymbolRef> getSymbol(uint32_t index) const {
992
    if (SymbolTable16) {
993
      const coff_symbol16 *Symb = nullptr;
994
      if (std::error_code EC = getSymbol(index, Symb))
995
        return errorCodeToError(EC);
996
      return COFFSymbolRef(Symb);
997
    }
998
    if (SymbolTable32) {
999
      const coff_symbol32 *Symb = nullptr;
1000
      if (std::error_code EC = getSymbol(index, Symb))
1001
        return errorCodeToError(EC);
1002
      return COFFSymbolRef(Symb);
1003
    }
1004
    return errorCodeToError(object_error::parse_failed);
1005
  }
1006
1007
  template <typename T>
1008
  std::error_code getAuxSymbol(uint32_t index, const T *&Res) const {
1009
    Expected<COFFSymbolRef> S = getSymbol(index);
1010
    if (Error E = S.takeError())
1011
      return errorToErrorCode(std::move(E));
1012
    Res = reinterpret_cast<const T *>(S->getRawPtr());
1013
    return std::error_code();
1014
  }
1015
1016
  std::error_code getSymbolName(COFFSymbolRef Symbol, StringRef &Res) const;
1017
  std::error_code getSymbolName(const coff_symbol_generic *Symbol,
1018
                                StringRef &Res) const;
1019
1020
  ArrayRef<uint8_t> getSymbolAuxData(COFFSymbolRef Symbol) const;
1021
1022
4.51k
  size_t getSymbolTableEntrySize() const {
1023
4.51k
    if (COFFHeader)
1024
4.50k
      return sizeof(coff_symbol16);
1025
11
    if (COFFBigObjHeader)
1026
11
      return sizeof(coff_symbol32);
1027
0
    llvm_unreachable("null symbol table pointer!");
1028
0
  }
1029
1030
  ArrayRef<coff_relocation> getRelocations(const coff_section *Sec) const;
1031
1032
  std::error_code getSectionName(const coff_section *Sec, StringRef &Res) const;
1033
  uint64_t getSectionSize(const coff_section *Sec) const;
1034
  std::error_code getSectionContents(const coff_section *Sec,
1035
                                     ArrayRef<uint8_t> &Res) const;
1036
1037
  uint64_t getImageBase() const;
1038
  std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const;
1039
  std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const;
1040
1041
  /// Given an RVA base and size, returns a valid array of bytes or an error
1042
  /// code if the RVA and size is not contained completely within a valid
1043
  /// section.
1044
  std::error_code getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size,
1045
                                       ArrayRef<uint8_t> &Contents) const;
1046
1047
  std::error_code getHintName(uint32_t Rva, uint16_t &Hint,
1048
                              StringRef &Name) const;
1049
1050
  /// Get PDB information out of a codeview debug directory entry.
1051
  std::error_code getDebugPDBInfo(const debug_directory *DebugDir,
1052
                                  const codeview::DebugInfo *&Info,
1053
                                  StringRef &PDBFileName) const;
1054
1055
  /// Get PDB information from an executable. If the information is not present,
1056
  /// Info will be set to nullptr and PDBFileName will be empty. An error is
1057
  /// returned only on corrupt object files. Convenience accessor that can be
1058
  /// used if the debug directory is not already handy.
1059
  std::error_code getDebugPDBInfo(const codeview::DebugInfo *&Info,
1060
                                  StringRef &PDBFileName) const;
1061
1062
  bool isRelocatableObject() const override;
1063
41
  bool is64() const { return PE32PlusHeader; }
1064
1065
7.44k
  static bool classof(const Binary *v) { return v->isCOFF(); }
1066
};
1067
1068
// The iterator for the import directory table.
1069
class ImportDirectoryEntryRef {
1070
public:
1071
  ImportDirectoryEntryRef() = default;
1072
  ImportDirectoryEntryRef(const coff_import_directory_table_entry *Table,
1073
                          uint32_t I, const COFFObjectFile *Owner)
1074
104
      : ImportTable(Table), Index(I), OwningObject(Owner) {}
1075
1076
  bool operator==(const ImportDirectoryEntryRef &Other) const;
1077
  void moveNext();
1078
1079
  imported_symbol_iterator imported_symbol_begin() const;
1080
  imported_symbol_iterator imported_symbol_end() const;
1081
  iterator_range<imported_symbol_iterator> imported_symbols() const;
1082
1083
  imported_symbol_iterator lookup_table_begin() const;
1084
  imported_symbol_iterator lookup_table_end() const;
1085
  iterator_range<imported_symbol_iterator> lookup_table_symbols() const;
1086
1087
  std::error_code getName(StringRef &Result) const;
1088
  std::error_code getImportLookupTableRVA(uint32_t &Result) const;
1089
  std::error_code getImportAddressTableRVA(uint32_t &Result) const;
1090
1091
  std::error_code
1092
  getImportTableEntry(const coff_import_directory_table_entry *&Result) const;
1093
1094
private:
1095
  const coff_import_directory_table_entry *ImportTable;
1096
  uint32_t Index;
1097
  const COFFObjectFile *OwningObject = nullptr;
1098
};
1099
1100
class DelayImportDirectoryEntryRef {
1101
public:
1102
  DelayImportDirectoryEntryRef() = default;
1103
  DelayImportDirectoryEntryRef(const delay_import_directory_table_entry *T,
1104
                               uint32_t I, const COFFObjectFile *Owner)
1105
46
      : Table(T), Index(I), OwningObject(Owner) {}
1106
1107
  bool operator==(const DelayImportDirectoryEntryRef &Other) const;
1108
  void moveNext();
1109
1110
  imported_symbol_iterator imported_symbol_begin() const;
1111
  imported_symbol_iterator imported_symbol_end() const;
1112
  iterator_range<imported_symbol_iterator> imported_symbols() const;
1113
1114
  std::error_code getName(StringRef &Result) const;
1115
  std::error_code getDelayImportTable(
1116
      const delay_import_directory_table_entry *&Result) const;
1117
  std::error_code getImportAddress(int AddrIndex, uint64_t &Result) const;
1118
1119
private:
1120
  const delay_import_directory_table_entry *Table;
1121
  uint32_t Index;
1122
  const COFFObjectFile *OwningObject = nullptr;
1123
};
1124
1125
// The iterator for the export directory table entry.
1126
class ExportDirectoryEntryRef {
1127
public:
1128
  ExportDirectoryEntryRef() = default;
1129
  ExportDirectoryEntryRef(const export_directory_table_entry *Table, uint32_t I,
1130
                          const COFFObjectFile *Owner)
1131
234
      : ExportTable(Table), Index(I), OwningObject(Owner) {}
1132
1133
  bool operator==(const ExportDirectoryEntryRef &Other) const;
1134
  void moveNext();
1135
1136
  std::error_code getDllName(StringRef &Result) const;
1137
  std::error_code getOrdinalBase(uint32_t &Result) const;
1138
  std::error_code getOrdinal(uint32_t &Result) const;
1139
  std::error_code getExportRVA(uint32_t &Result) const;
1140
  std::error_code getSymbolName(StringRef &Result) const;
1141
1142
  std::error_code isForwarder(bool &Result) const;
1143
  std::error_code getForwardTo(StringRef &Result) const;
1144
1145
private:
1146
  const export_directory_table_entry *ExportTable;
1147
  uint32_t Index;
1148
  const COFFObjectFile *OwningObject = nullptr;
1149
};
1150
1151
class ImportedSymbolRef {
1152
public:
1153
  ImportedSymbolRef() = default;
1154
  ImportedSymbolRef(const import_lookup_table_entry32 *Entry, uint32_t I,
1155
                    const COFFObjectFile *Owner)
1156
26
      : Entry32(Entry), Entry64(nullptr), Index(I), OwningObject(Owner) {}
1157
  ImportedSymbolRef(const import_lookup_table_entry64 *Entry, uint32_t I,
1158
                    const COFFObjectFile *Owner)
1159
40
      : Entry32(nullptr), Entry64(Entry), Index(I), OwningObject(Owner) {}
1160
1161
  bool operator==(const ImportedSymbolRef &Other) const;
1162
  void moveNext();
1163
1164
  std::error_code getSymbolName(StringRef &Result) const;
1165
  std::error_code isOrdinal(bool &Result) const;
1166
  std::error_code getOrdinal(uint16_t &Result) const;
1167
  std::error_code getHintNameRVA(uint32_t &Result) const;
1168
1169
private:
1170
  const import_lookup_table_entry32 *Entry32;
1171
  const import_lookup_table_entry64 *Entry64;
1172
  uint32_t Index;
1173
  const COFFObjectFile *OwningObject = nullptr;
1174
};
1175
1176
class BaseRelocRef {
1177
public:
1178
  BaseRelocRef() = default;
1179
  BaseRelocRef(const coff_base_reloc_block_header *Header,
1180
               const COFFObjectFile *Owner)
1181
28
      : Header(Header), Index(0) {}
1182
1183
  bool operator==(const BaseRelocRef &Other) const;
1184
  void moveNext();
1185
1186
  std::error_code getType(uint8_t &Type) const;
1187
  std::error_code getRVA(uint32_t &Result) const;
1188
1189
private:
1190
  const coff_base_reloc_block_header *Header;
1191
  uint32_t Index;
1192
};
1193
1194
class ResourceSectionRef {
1195
public:
1196
  ResourceSectionRef() = default;
1197
  explicit ResourceSectionRef(StringRef Ref) : BBS(Ref, support::little) {}
1198
1199
  Expected<ArrayRef<UTF16>>
1200
  getEntryNameString(const coff_resource_dir_entry &Entry);
1201
  Expected<const coff_resource_dir_table &>
1202
  getEntrySubDir(const coff_resource_dir_entry &Entry);
1203
  Expected<const coff_resource_dir_table &> getBaseTable();
1204
1205
private:
1206
  BinaryByteStream BBS;
1207
1208
  Expected<const coff_resource_dir_table &> getTableAtOffset(uint32_t Offset);
1209
  Expected<ArrayRef<UTF16>> getDirStringAtOffset(uint32_t Offset);
1210
};
1211
1212
// Corresponds to `_FPO_DATA` structure in the PE/COFF spec.
1213
struct FpoData {
1214
  support::ulittle32_t Offset; // ulOffStart: Offset 1st byte of function code
1215
  support::ulittle32_t Size;   // cbProcSize: # bytes in function
1216
  support::ulittle32_t NumLocals; // cdwLocals: # bytes in locals/4
1217
  support::ulittle16_t NumParams; // cdwParams: # bytes in params/4
1218
  support::ulittle16_t Attributes;
1219
1220
  // cbProlog: # bytes in prolog
1221
  int getPrologSize() const { return Attributes & 0xF; }
1222
1223
  // cbRegs: # regs saved
1224
  int getNumSavedRegs() const { return (Attributes >> 8) & 0x7; }
1225
1226
  // fHasSEH: true if seh is func
1227
  bool hasSEH() const { return (Attributes >> 9) & 1; }
1228
1229
  // fUseBP: true if EBP has been allocated
1230
  bool useBP() const { return (Attributes >> 10) & 1; }
1231
1232
  // cbFrame: frame pointer
1233
  frame_type getFP() const { return static_cast<frame_type>(Attributes >> 14); }
1234
};
1235
1236
} // end namespace object
1237
1238
} // end namespace llvm
1239
1240
#endif // LLVM_OBJECT_COFF_H