LLVM  5.0.0
COFFObjectFile.cpp
Go to the documentation of this file.
1 //===- COFFObjectFile.cpp - COFF object file implementation ---------------===//
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 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Triple.h"
18 #include "llvm/BinaryFormat/COFF.h"
19 #include "llvm/Object/Binary.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Object/Error.h"
22 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Endian.h"
25 #include "llvm/Support/Error.h"
29 #include <algorithm>
30 #include <cassert>
31 #include <cstddef>
32 #include <cstdint>
33 #include <cstring>
34 #include <limits>
35 #include <memory>
36 #include <system_error>
37 
38 using namespace llvm;
39 using namespace object;
40 
45 
46 // Returns false if size is greater than the buffer size. And sets ec.
47 static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
48  if (M.getBufferSize() < Size) {
50  return false;
51  }
52  return true;
53 }
54 
55 static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
56  const uint64_t Size) {
57  if (Addr + Size < Addr || Addr + Size < Size ||
58  Addr + Size > uintptr_t(M.getBufferEnd()) ||
59  Addr < uintptr_t(M.getBufferStart())) {
61  }
62  return std::error_code();
63 }
64 
65 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
66 // Returns unexpected_eof if error.
67 template <typename T>
68 static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
69  const void *Ptr,
70  const uint64_t Size = sizeof(T)) {
71  uintptr_t Addr = uintptr_t(Ptr);
72  if (std::error_code EC = checkOffset(M, Addr, Size))
73  return EC;
74  Obj = reinterpret_cast<const T *>(Addr);
75  return std::error_code();
76 }
77 
78 // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
79 // prefixed slashes.
80 static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
81  assert(Str.size() <= 6 && "String too long, possible overflow.");
82  if (Str.size() > 6)
83  return true;
84 
85  uint64_t Value = 0;
86  while (!Str.empty()) {
87  unsigned CharVal;
88  if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
89  CharVal = Str[0] - 'A';
90  else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
91  CharVal = Str[0] - 'a' + 26;
92  else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
93  CharVal = Str[0] - '0' + 52;
94  else if (Str[0] == '+') // 62
95  CharVal = 62;
96  else if (Str[0] == '/') // 63
97  CharVal = 63;
98  else
99  return true;
100 
101  Value = (Value * 64) + CharVal;
102  Str = Str.substr(1);
103  }
104 
106  return true;
107 
108  Result = static_cast<uint32_t>(Value);
109  return false;
110 }
111 
112 template <typename coff_symbol_type>
113 const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
114  const coff_symbol_type *Addr =
115  reinterpret_cast<const coff_symbol_type *>(Ref.p);
116 
117  assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
118 #ifndef NDEBUG
119  // Verify that the symbol points to a valid entry in the symbol table.
120  uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
121 
122  assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
123  "Symbol did not point to the beginning of a symbol");
124 #endif
125 
126  return Addr;
127 }
128 
129 const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
130  const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
131 
132 #ifndef NDEBUG
133  // Verify that the section points to a valid entry in the section table.
134  if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
135  report_fatal_error("Section was outside of section table.");
136 
137  uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
138  assert(Offset % sizeof(coff_section) == 0 &&
139  "Section did not point to the beginning of a section");
140 #endif
141 
142  return Addr;
143 }
144 
146  auto End = reinterpret_cast<uintptr_t>(StringTable);
147  if (SymbolTable16) {
148  const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
149  Symb += 1 + Symb->NumberOfAuxSymbols;
150  Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
151  } else if (SymbolTable32) {
152  const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
153  Symb += 1 + Symb->NumberOfAuxSymbols;
154  Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
155  } else {
156  llvm_unreachable("no symbol table pointer!");
157  }
158 }
159 
161  COFFSymbolRef Symb = getCOFFSymbol(Ref);
162  StringRef Result;
163  if (std::error_code EC = getSymbolName(Symb, Result))
164  return errorCodeToError(EC);
165  return Result;
166 }
167 
169  return getCOFFSymbol(Ref).getValue();
170 }
171 
173  // MSVC/link.exe seems to align symbols to the next-power-of-2
174  // up to 32 bytes.
175  COFFSymbolRef Symb = getCOFFSymbol(Ref);
176  return std::min(uint64_t(32), PowerOf2Ceil(Symb.getValue()));
177 }
178 
180  uint64_t Result = getSymbolValue(Ref);
181  COFFSymbolRef Symb = getCOFFSymbol(Ref);
182  int32_t SectionNumber = Symb.getSectionNumber();
183 
184  if (Symb.isAnyUndefined() || Symb.isCommon() ||
185  COFF::isReservedSectionNumber(SectionNumber))
186  return Result;
187 
188  const coff_section *Section = nullptr;
189  if (std::error_code EC = getSection(SectionNumber, Section))
190  return errorCodeToError(EC);
191  Result += Section->VirtualAddress;
192 
193  // The section VirtualAddress does not include ImageBase, and we want to
194  // return virtual addresses.
195  Result += getImageBase();
196 
197  return Result;
198 }
199 
201  COFFSymbolRef Symb = getCOFFSymbol(Ref);
202  int32_t SectionNumber = Symb.getSectionNumber();
203 
205  return SymbolRef::ST_Function;
206  if (Symb.isAnyUndefined())
207  return SymbolRef::ST_Unknown;
208  if (Symb.isCommon())
209  return SymbolRef::ST_Data;
210  if (Symb.isFileRecord())
211  return SymbolRef::ST_File;
212 
213  // TODO: perhaps we need a new symbol type ST_Section.
214  if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
215  return SymbolRef::ST_Debug;
216 
217  if (!COFF::isReservedSectionNumber(SectionNumber))
218  return SymbolRef::ST_Data;
219 
220  return SymbolRef::ST_Other;
221 }
222 
224  COFFSymbolRef Symb = getCOFFSymbol(Ref);
225  uint32_t Result = SymbolRef::SF_None;
226 
227  if (Symb.isExternal() || Symb.isWeakExternal())
228  Result |= SymbolRef::SF_Global;
229 
230  if (Symb.isWeakExternal()) {
231  Result |= SymbolRef::SF_Weak;
232  // We use indirect to allow the archiver to write weak externs
233  Result |= SymbolRef::SF_Indirect;
234  }
235 
237  Result |= SymbolRef::SF_Absolute;
238 
239  if (Symb.isFileRecord())
241 
242  if (Symb.isSectionDefinition())
244 
245  if (Symb.isCommon())
246  Result |= SymbolRef::SF_Common;
247 
248  if (Symb.isAnyUndefined())
249  Result |= SymbolRef::SF_Undefined;
250 
251  return Result;
252 }
253 
255  COFFSymbolRef Symb = getCOFFSymbol(Ref);
256  return Symb.getValue();
257 }
258 
261  COFFSymbolRef Symb = getCOFFSymbol(Ref);
263  return section_end();
264  const coff_section *Sec = nullptr;
265  if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
266  return errorCodeToError(EC);
268  Ret.p = reinterpret_cast<uintptr_t>(Sec);
269  return section_iterator(SectionRef(Ret, this));
270 }
271 
274  return Symb.getSectionNumber();
275 }
276 
278  const coff_section *Sec = toSec(Ref);
279  Sec += 1;
280  Ref.p = reinterpret_cast<uintptr_t>(Sec);
281 }
282 
283 std::error_code COFFObjectFile::getSectionName(DataRefImpl Ref,
284  StringRef &Result) const {
285  const coff_section *Sec = toSec(Ref);
286  return getSectionName(Sec, Result);
287 }
288 
290  const coff_section *Sec = toSec(Ref);
291  uint64_t Result = Sec->VirtualAddress;
292 
293  // The section VirtualAddress does not include ImageBase, and we want to
294  // return virtual addresses.
295  Result += getImageBase();
296  return Result;
297 }
298 
300  return toSec(Sec) - SectionTable;
301 }
302 
303 uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
304  return getSectionSize(toSec(Ref));
305 }
306 
307 std::error_code COFFObjectFile::getSectionContents(DataRefImpl Ref,
308  StringRef &Result) const {
309  const coff_section *Sec = toSec(Ref);
310  ArrayRef<uint8_t> Res;
311  std::error_code EC = getSectionContents(Sec, Res);
312  Result = StringRef(reinterpret_cast<const char*>(Res.data()), Res.size());
313  return EC;
314 }
315 
317  const coff_section *Sec = toSec(Ref);
318  return Sec->getAlignment();
319 }
320 
322  return false;
323 }
324 
326  const coff_section *Sec = toSec(Ref);
328 }
329 
331  const coff_section *Sec = toSec(Ref);
333 }
334 
336  const coff_section *Sec = toSec(Ref);
340  return (Sec->Characteristics & BssFlags) == BssFlags;
341 }
342 
344  uintptr_t Offset =
345  uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
346  assert((Offset % sizeof(coff_section)) == 0);
347  return (Offset / sizeof(coff_section)) + 1;
348 }
349 
351  const coff_section *Sec = toSec(Ref);
352  // In COFF, a virtual section won't have any in-file
353  // content, so the file pointer to the content will be zero.
354  return Sec->PointerToRawData == 0;
355 }
356 
357 static uint32_t getNumberOfRelocations(const coff_section *Sec,
358  MemoryBufferRef M, const uint8_t *base) {
359  // The field for the number of relocations in COFF section table is only
360  // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
361  // NumberOfRelocations field, and the actual relocation count is stored in the
362  // VirtualAddress field in the first relocation entry.
363  if (Sec->hasExtendedRelocations()) {
364  const coff_relocation *FirstReloc;
365  if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
366  base + Sec->PointerToRelocations)))
367  return 0;
368  // -1 to exclude this first relocation entry.
369  return FirstReloc->VirtualAddress - 1;
370  }
371  return Sec->NumberOfRelocations;
372 }
373 
374 static const coff_relocation *
375 getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
376  uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
377  if (!NumRelocs)
378  return nullptr;
379  auto begin = reinterpret_cast<const coff_relocation *>(
380  Base + Sec->PointerToRelocations);
381  if (Sec->hasExtendedRelocations()) {
382  // Skip the first relocation entry repurposed to store the number of
383  // relocations.
384  begin++;
385  }
386  if (checkOffset(M, uintptr_t(begin), sizeof(coff_relocation) * NumRelocs))
387  return nullptr;
388  return begin;
389 }
390 
392  const coff_section *Sec = toSec(Ref);
393  const coff_relocation *begin = getFirstReloc(Sec, Data, base());
394  if (begin && Sec->VirtualAddress != 0)
395  report_fatal_error("Sections with relocations should have an address of 0");
397  Ret.p = reinterpret_cast<uintptr_t>(begin);
398  return relocation_iterator(RelocationRef(Ret, this));
399 }
400 
402  const coff_section *Sec = toSec(Ref);
403  const coff_relocation *I = getFirstReloc(Sec, Data, base());
404  if (I)
405  I += getNumberOfRelocations(Sec, Data, base());
407  Ret.p = reinterpret_cast<uintptr_t>(I);
408  return relocation_iterator(RelocationRef(Ret, this));
409 }
410 
411 // Initialize the pointer to the symbol table.
412 std::error_code COFFObjectFile::initSymbolTablePtr() {
413  if (COFFHeader)
414  if (std::error_code EC = getObject(
415  SymbolTable16, Data, base() + getPointerToSymbolTable(),
417  return EC;
418 
419  if (COFFBigObjHeader)
420  if (std::error_code EC = getObject(
421  SymbolTable32, Data, base() + getPointerToSymbolTable(),
423  return EC;
424 
425  // Find string table. The first four byte of the string table contains the
426  // total size of the string table, including the size field itself. If the
427  // string table is empty, the value of the first four byte would be 4.
430  const uint8_t *StringTableAddr = base() + StringTableOffset;
431  const ulittle32_t *StringTableSizePtr;
432  if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
433  return EC;
434  StringTableSize = *StringTableSizePtr;
435  if (std::error_code EC =
436  getObject(StringTable, Data, StringTableAddr, StringTableSize))
437  return EC;
438 
439  // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
440  // tools like cvtres write a size of 0 for an empty table instead of 4.
441  if (StringTableSize < 4)
442  StringTableSize = 4;
443 
444  // Check that the string table is null terminated if has any in it.
445  if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
447  return std::error_code();
448 }
449 
451  if (PE32Header)
452  return PE32Header->ImageBase;
453  else if (PE32PlusHeader)
454  return PE32PlusHeader->ImageBase;
455  // This actually comes up in practice.
456  return 0;
457 }
458 
459 // Returns the file offset for the given VA.
460 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
461  uint64_t ImageBase = getImageBase();
462  uint64_t Rva = Addr - ImageBase;
463  assert(Rva <= UINT32_MAX);
464  return getRvaPtr((uint32_t)Rva, Res);
465 }
466 
467 // Returns the file offset for the given RVA.
468 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
469  for (const SectionRef &S : sections()) {
470  const coff_section *Section = getCOFFSection(S);
471  uint32_t SectionStart = Section->VirtualAddress;
472  uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
473  if (SectionStart <= Addr && Addr < SectionEnd) {
474  uint32_t Offset = Addr - SectionStart;
475  Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
476  return std::error_code();
477  }
478  }
480 }
481 
482 std::error_code
484  ArrayRef<uint8_t> &Contents) const {
485  for (const SectionRef &S : sections()) {
486  const coff_section *Section = getCOFFSection(S);
487  uint32_t SectionStart = Section->VirtualAddress;
488  // Check if this RVA is within the section bounds. Be careful about integer
489  // overflow.
490  uint32_t OffsetIntoSection = RVA - SectionStart;
491  if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize &&
492  Size <= Section->VirtualSize - OffsetIntoSection) {
493  uintptr_t Begin =
494  uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection;
495  Contents =
496  ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size);
497  return std::error_code();
498  }
499  }
501 }
502 
503 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
504 // table entry.
505 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
506  StringRef &Name) const {
507  uintptr_t IntPtr = 0;
508  if (std::error_code EC = getRvaPtr(Rva, IntPtr))
509  return EC;
510  const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
511  Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
512  Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
513  return std::error_code();
514 }
515 
516 std::error_code
518  const codeview::DebugInfo *&PDBInfo,
519  StringRef &PDBFileName) const {
520  ArrayRef<uint8_t> InfoBytes;
521  if (std::error_code EC = getRvaAndSizeAsBytes(
522  DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
523  return EC;
524  if (InfoBytes.size() < sizeof(*PDBInfo) + 1)
526  PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data());
527  InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo));
528  PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
529  InfoBytes.size());
530  // Truncate the name at the first null byte. Ignore any padding.
531  PDBFileName = PDBFileName.split('\0').first;
532  return std::error_code();
533 }
534 
535 std::error_code
537  StringRef &PDBFileName) const {
538  for (const debug_directory &D : debug_directories())
540  return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
541  // If we get here, there is no PDB info to return.
542  PDBInfo = nullptr;
543  PDBFileName = StringRef();
544  return std::error_code();
545 }
546 
547 // Find the import table.
548 std::error_code COFFObjectFile::initImportTablePtr() {
549  // First, we get the RVA of the import table. If the file lacks a pointer to
550  // the import table, do nothing.
551  const data_directory *DataEntry;
552  if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
553  return std::error_code();
554 
555  // Do nothing if the pointer to import table is NULL.
556  if (DataEntry->RelativeVirtualAddress == 0)
557  return std::error_code();
558 
559  uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
560 
561  // Find the section that contains the RVA. This is needed because the RVA is
562  // the import table's memory address which is different from its file offset.
563  uintptr_t IntPtr = 0;
564  if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
565  return EC;
566  if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
567  return EC;
568  ImportDirectory = reinterpret_cast<
569  const coff_import_directory_table_entry *>(IntPtr);
570  return std::error_code();
571 }
572 
573 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
574 std::error_code COFFObjectFile::initDelayImportTablePtr() {
575  const data_directory *DataEntry;
577  return std::error_code();
578  if (DataEntry->RelativeVirtualAddress == 0)
579  return std::error_code();
580 
581  uint32_t RVA = DataEntry->RelativeVirtualAddress;
582  NumberOfDelayImportDirectory = DataEntry->Size /
583  sizeof(delay_import_directory_table_entry) - 1;
584 
585  uintptr_t IntPtr = 0;
586  if (std::error_code EC = getRvaPtr(RVA, IntPtr))
587  return EC;
588  DelayImportDirectory = reinterpret_cast<
589  const delay_import_directory_table_entry *>(IntPtr);
590  return std::error_code();
591 }
592 
593 // Find the export table.
594 std::error_code COFFObjectFile::initExportTablePtr() {
595  // First, we get the RVA of the export table. If the file lacks a pointer to
596  // the export table, do nothing.
597  const data_directory *DataEntry;
598  if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
599  return std::error_code();
600 
601  // Do nothing if the pointer to export table is NULL.
602  if (DataEntry->RelativeVirtualAddress == 0)
603  return std::error_code();
604 
605  uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
606  uintptr_t IntPtr = 0;
607  if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
608  return EC;
609  ExportDirectory =
610  reinterpret_cast<const export_directory_table_entry *>(IntPtr);
611  return std::error_code();
612 }
613 
614 std::error_code COFFObjectFile::initBaseRelocPtr() {
615  const data_directory *DataEntry;
617  return std::error_code();
618  if (DataEntry->RelativeVirtualAddress == 0)
619  return std::error_code();
620 
621  uintptr_t IntPtr = 0;
622  if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
623  return EC;
624  BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
625  IntPtr);
626  BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
627  IntPtr + DataEntry->Size);
628  return std::error_code();
629 }
630 
631 std::error_code COFFObjectFile::initDebugDirectoryPtr() {
632  // Get the RVA of the debug directory. Do nothing if it does not exist.
633  const data_directory *DataEntry;
634  if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
635  return std::error_code();
636 
637  // Do nothing if the RVA is NULL.
638  if (DataEntry->RelativeVirtualAddress == 0)
639  return std::error_code();
640 
641  // Check that the size is a multiple of the entry size.
642  if (DataEntry->Size % sizeof(debug_directory) != 0)
644 
645  uintptr_t IntPtr = 0;
646  if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
647  return EC;
648  DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
649  if (std::error_code EC = getRvaPtr(
650  DataEntry->RelativeVirtualAddress + DataEntry->Size, IntPtr))
651  return EC;
652  DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(IntPtr);
653  return std::error_code();
654 }
655 
656 std::error_code COFFObjectFile::initLoadConfigPtr() {
657  // Get the RVA of the debug directory. Do nothing if it does not exist.
658  const data_directory *DataEntry;
660  return std::error_code();
661 
662  // Do nothing if the RVA is NULL.
663  if (DataEntry->RelativeVirtualAddress == 0)
664  return std::error_code();
665  uintptr_t IntPtr = 0;
666  if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
667  return EC;
668 
669  LoadConfig = (const void *)IntPtr;
670  return std::error_code();
671 }
672 
674  : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
675  COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
676  DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
677  SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
678  ImportDirectory(nullptr),
679  DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
680  ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
681  DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
682  // Check that we at least have enough room for a header.
683  if (!checkSize(Data, EC, sizeof(coff_file_header)))
684  return;
685 
686  // The current location in the file where we are looking at.
687  uint64_t CurPtr = 0;
688 
689  // PE header is optional and is present only in executables. If it exists,
690  // it is placed right after COFF header.
691  bool HasPEHeader = false;
692 
693  // Check if this is a PE/COFF file.
694  if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
695  // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
696  // PE signature to find 'normal' COFF header.
697  const auto *DH = reinterpret_cast<const dos_header *>(base());
698  if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
699  CurPtr = DH->AddressOfNewExeHeader;
700  // Check the PE magic bytes. ("PE\0\0")
701  if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
703  return;
704  }
705  CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
706  HasPEHeader = true;
707  }
708  }
709 
710  if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
711  return;
712 
713  // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
714  // import libraries share a common prefix but bigobj is more restrictive.
715  if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
716  COFFHeader->NumberOfSections == uint16_t(0xffff) &&
717  checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
718  if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
719  return;
720 
721  // Verify that we are dealing with bigobj.
722  if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
723  std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
724  sizeof(COFF::BigObjMagic)) == 0) {
725  COFFHeader = nullptr;
726  CurPtr += sizeof(coff_bigobj_file_header);
727  } else {
728  // It's not a bigobj.
729  COFFBigObjHeader = nullptr;
730  }
731  }
732  if (COFFHeader) {
733  // The prior checkSize call may have failed. This isn't a hard error
734  // because we were just trying to sniff out bigobj.
735  EC = std::error_code();
736  CurPtr += sizeof(coff_file_header);
737 
738  if (COFFHeader->isImportLibrary())
739  return;
740  }
741 
742  if (HasPEHeader) {
743  const pe32_header *Header;
744  if ((EC = getObject(Header, Data, base() + CurPtr)))
745  return;
746 
747  const uint8_t *DataDirAddr;
748  uint64_t DataDirSize;
749  if (Header->Magic == COFF::PE32Header::PE32) {
750  PE32Header = Header;
751  DataDirAddr = base() + CurPtr + sizeof(pe32_header);
752  DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
753  } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
754  PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
755  DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
756  DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
757  } else {
758  // It's neither PE32 nor PE32+.
760  return;
761  }
762  if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
763  return;
764  }
765 
766  if (COFFHeader)
767  CurPtr += COFFHeader->SizeOfOptionalHeader;
768 
769  if ((EC = getObject(SectionTable, Data, base() + CurPtr,
770  (uint64_t)getNumberOfSections() * sizeof(coff_section))))
771  return;
772 
773  // Initialize the pointer to the symbol table.
774  if (getPointerToSymbolTable() != 0) {
775  if ((EC = initSymbolTablePtr())) {
776  SymbolTable16 = nullptr;
777  SymbolTable32 = nullptr;
778  StringTable = nullptr;
779  StringTableSize = 0;
780  }
781  } else {
782  // We had better not have any symbols if we don't have a symbol table.
783  if (getNumberOfSymbols() != 0) {
785  return;
786  }
787  }
788 
789  // Initialize the pointer to the beginning of the import table.
790  if ((EC = initImportTablePtr()))
791  return;
792  if ((EC = initDelayImportTablePtr()))
793  return;
794 
795  // Initialize the pointer to the export table.
796  if ((EC = initExportTablePtr()))
797  return;
798 
799  // Initialize the pointer to the base relocation table.
800  if ((EC = initBaseRelocPtr()))
801  return;
802 
803  // Initialize the pointer to the export table.
804  if ((EC = initDebugDirectoryPtr()))
805  return;
806 
807  if ((EC = initLoadConfigPtr()))
808  return;
809 
810  EC = std::error_code();
811 }
812 
815  Ret.p = getSymbolTable();
816  return basic_symbol_iterator(SymbolRef(Ret, this));
817 }
818 
820  // The symbol table ends where the string table begins.
822  Ret.p = reinterpret_cast<uintptr_t>(StringTable);
823  return basic_symbol_iterator(SymbolRef(Ret, this));
824 }
825 
827  if (!ImportDirectory)
828  return import_directory_end();
829  if (ImportDirectory->isNull())
830  return import_directory_end();
832  ImportDirectoryEntryRef(ImportDirectory, 0, this));
833 }
834 
837  ImportDirectoryEntryRef(nullptr, -1, this));
838 }
839 
843  DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
844 }
845 
850  DelayImportDirectory, NumberOfDelayImportDirectory, this));
851 }
852 
855  ExportDirectoryEntryRef(ExportDirectory, 0, this));
856 }
857 
859  if (!ExportDirectory)
860  return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
861  ExportDirectoryEntryRef Ref(ExportDirectory,
862  ExportDirectory->AddressTableEntries, this);
863  return export_directory_iterator(Ref);
864 }
865 
868  Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
869  return section_iterator(SectionRef(Ret, this));
870 }
871 
874  int NumSections =
875  COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
876  Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
877  return section_iterator(SectionRef(Ret, this));
878 }
879 
881  return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
882 }
883 
885  return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
886 }
887 
889  return getArch() == Triple::x86_64 || getArch() == Triple::aarch64 ? 8 : 4;
890 }
891 
893  switch(getMachine()) {
895  return "COFF-i386";
897  return "COFF-x86-64";
899  return "COFF-ARM";
901  return "COFF-ARM64";
902  default:
903  return "COFF-<unknown arch>";
904  }
905 }
906 
907 unsigned COFFObjectFile::getArch() const {
908  switch (getMachine()) {
910  return Triple::x86;
912  return Triple::x86_64;
914  return Triple::thumb;
916  return Triple::aarch64;
917  default:
918  return Triple::UnknownArch;
919  }
920 }
921 
925 }
926 
931 }
932 
936 }
937 
940 }
941 
942 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
943  Res = PE32Header;
944  return std::error_code();
945 }
946 
947 std::error_code
949  Res = PE32PlusHeader;
950  return std::error_code();
951 }
952 
953 std::error_code
955  const data_directory *&Res) const {
956  // Error if if there's no data directory or the index is out of range.
957  if (!DataDirectory) {
958  Res = nullptr;
960  }
961  assert(PE32Header || PE32PlusHeader);
962  uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
963  : PE32PlusHeader->NumberOfRvaAndSize;
964  if (Index >= NumEnt) {
965  Res = nullptr;
967  }
968  Res = &DataDirectory[Index];
969  return std::error_code();
970 }
971 
972 std::error_code COFFObjectFile::getSection(int32_t Index,
973  const coff_section *&Result) const {
974  Result = nullptr;
976  return std::error_code();
977  if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
978  // We already verified the section table data, so no need to check again.
979  Result = SectionTable + (Index - 1);
980  return std::error_code();
981  }
983 }
984 
985 std::error_code COFFObjectFile::getString(uint32_t Offset,
986  StringRef &Result) const {
987  if (StringTableSize <= 4)
988  // Tried to get a string from an empty string table.
990  if (Offset >= StringTableSize)
992  Result = StringRef(StringTable + Offset);
993  return std::error_code();
994 }
995 
996 std::error_code COFFObjectFile::getSymbolName(COFFSymbolRef Symbol,
997  StringRef &Res) const {
998  return getSymbolName(Symbol.getGeneric(), Res);
999 }
1000 
1001 std::error_code COFFObjectFile::getSymbolName(const coff_symbol_generic *Symbol,
1002  StringRef &Res) const {
1003  // Check for string table entry. First 4 bytes are 0.
1004  if (Symbol->Name.Offset.Zeroes == 0) {
1005  if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
1006  return EC;
1007  return std::error_code();
1008  }
1009 
1010  if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
1011  // Null terminated, let ::strlen figure out the length.
1012  Res = StringRef(Symbol->Name.ShortName);
1013  else
1014  // Not null terminated, use all 8 bytes.
1015  Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
1016  return std::error_code();
1017 }
1018 
1021  const uint8_t *Aux = nullptr;
1022 
1023  size_t SymbolSize = getSymbolTableEntrySize();
1024  if (Symbol.getNumberOfAuxSymbols() > 0) {
1025  // AUX data comes immediately after the symbol in COFF
1026  Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
1027 #ifndef NDEBUG
1028  // Verify that the Aux symbol points to a valid entry in the symbol table.
1029  uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
1030  if (Offset < getPointerToSymbolTable() ||
1031  Offset >=
1032  getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
1033  report_fatal_error("Aux Symbol data was outside of symbol table.");
1034 
1035  assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
1036  "Aux Symbol data did not point to the beginning of a symbol");
1037 #endif
1038  }
1039  return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
1040 }
1041 
1042 std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
1043  StringRef &Res) const {
1044  StringRef Name;
1045  if (Sec->Name[COFF::NameSize - 1] == 0)
1046  // Null terminated, let ::strlen figure out the length.
1047  Name = Sec->Name;
1048  else
1049  // Not null terminated, use all 8 bytes.
1050  Name = StringRef(Sec->Name, COFF::NameSize);
1051 
1052  // Check for string table entry. First byte is '/'.
1053  if (Name.startswith("/")) {
1054  uint32_t Offset;
1055  if (Name.startswith("//")) {
1056  if (decodeBase64StringEntry(Name.substr(2), Offset))
1058  } else {
1059  if (Name.substr(1).getAsInteger(10, Offset))
1061  }
1062  if (std::error_code EC = getString(Offset, Name))
1063  return EC;
1064  }
1065 
1066  Res = Name;
1067  return std::error_code();
1068 }
1069 
1070 uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1071  // SizeOfRawData and VirtualSize change what they represent depending on
1072  // whether or not we have an executable image.
1073  //
1074  // For object files, SizeOfRawData contains the size of section's data;
1075  // VirtualSize should be zero but isn't due to buggy COFF writers.
1076  //
1077  // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1078  // actual section size is in VirtualSize. It is possible for VirtualSize to
1079  // be greater than SizeOfRawData; the contents past that point should be
1080  // considered to be zero.
1081  if (getDOSHeader())
1082  return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1083  return Sec->SizeOfRawData;
1084 }
1085 
1086 std::error_code
1087 COFFObjectFile::getSectionContents(const coff_section *Sec,
1088  ArrayRef<uint8_t> &Res) const {
1089  // In COFF, a virtual section won't have any in-file
1090  // content, so the file pointer to the content will be zero.
1091  if (Sec->PointerToRawData == 0)
1092  return std::error_code();
1093  // The only thing that we need to verify is that the contents is contained
1094  // within the file bounds. We don't need to make sure it doesn't cover other
1095  // data, as there's nothing that says that is not allowed.
1096  uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
1098  if (checkOffset(Data, ConStart, SectionSize))
1100  Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
1101  return std::error_code();
1102 }
1103 
1104 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
1105  return reinterpret_cast<const coff_relocation*>(Rel.p);
1106 }
1107 
1109  Rel.p = reinterpret_cast<uintptr_t>(
1110  reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
1111 }
1112 
1114  const coff_relocation *R = toRel(Rel);
1115  return R->VirtualAddress;
1116 }
1117 
1119  const coff_relocation *R = toRel(Rel);
1120  DataRefImpl Ref;
1122  return symbol_end();
1123  if (SymbolTable16)
1124  Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1125  else if (SymbolTable32)
1126  Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1127  else
1128  llvm_unreachable("no symbol table pointer!");
1129  return symbol_iterator(SymbolRef(Ref, this));
1130 }
1131 
1133  const coff_relocation* R = toRel(Rel);
1134  return R->Type;
1135 }
1136 
1137 const coff_section *
1139  return toSec(Section.getRawDataRefImpl());
1140 }
1141 
1143  if (SymbolTable16)
1144  return toSymb<coff_symbol16>(Ref);
1145  if (SymbolTable32)
1146  return toSymb<coff_symbol32>(Ref);
1147  llvm_unreachable("no symbol table pointer!");
1148 }
1149 
1150 COFFSymbolRef COFFObjectFile::getCOFFSymbol(const SymbolRef &Symbol) const {
1151  return getCOFFSymbol(Symbol.getRawDataRefImpl());
1152 }
1153 
1154 const coff_relocation *
1156  return toRel(Reloc.getRawDataRefImpl());
1157 }
1158 
1161  const coff_relocation *I = getFirstReloc(Sec, Data, base());
1162  const coff_relocation *E = I;
1163  if (I)
1164  E += getNumberOfRelocations(Sec, Data, base());
1165  return make_range(I, E);
1166 }
1167 
1168 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1169  case COFF::reloc_type: \
1170  Res = #reloc_type; \
1171  break;
1172 
1174  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1175  const coff_relocation *Reloc = toRel(Rel);
1176  StringRef Res;
1177  switch (getMachine()) {
1179  switch (Reloc->Type) {
1197  default:
1198  Res = "Unknown";
1199  }
1200  break;
1202  switch (Reloc->Type) {
1218  default:
1219  Res = "Unknown";
1220  }
1221  break;
1223  switch (Reloc->Type) {
1241  default:
1242  Res = "Unknown";
1243  }
1244  break;
1246  switch (Reloc->Type) {
1258  default:
1259  Res = "Unknown";
1260  }
1261  break;
1262  default:
1263  Res = "Unknown";
1264  }
1265  Result.append(Res.begin(), Res.end());
1266 }
1267 
1268 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1269 
1271  return !DataDirectory;
1272 }
1273 
1276  return ImportTable == Other.ImportTable && Index == Other.Index;
1277 }
1278 
1280  ++Index;
1281  if (ImportTable[Index].isNull()) {
1282  Index = -1;
1283  ImportTable = nullptr;
1284  }
1285 }
1286 
1288  const coff_import_directory_table_entry *&Result) const {
1289  return getObject(Result, OwningObject->Data, ImportTable + Index);
1290 }
1291 
1293 makeImportedSymbolIterator(const COFFObjectFile *Object,
1294  uintptr_t Ptr, int Index) {
1295  if (Object->getBytesInAddress() == 4) {
1296  auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
1297  return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1298  }
1299  auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
1300  return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1301 }
1302 
1304 importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object) {
1305  uintptr_t IntPtr = 0;
1306  Object->getRvaPtr(RVA, IntPtr);
1307  return makeImportedSymbolIterator(Object, IntPtr, 0);
1308 }
1309 
1311 importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object) {
1312  uintptr_t IntPtr = 0;
1313  Object->getRvaPtr(RVA, IntPtr);
1314  // Forward the pointer to the last entry which is null.
1315  int Index = 0;
1316  if (Object->getBytesInAddress() == 4) {
1317  auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1318  while (*Entry++)
1319  ++Index;
1320  } else {
1321  auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1322  while (*Entry++)
1323  ++Index;
1324  }
1325  return makeImportedSymbolIterator(Object, IntPtr, Index);
1326 }
1327 
1330  return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA,
1331  OwningObject);
1332 }
1333 
1336  return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA,
1337  OwningObject);
1338 }
1339 
1343 }
1344 
1346  return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1347  OwningObject);
1348 }
1349 
1351  return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1352  OwningObject);
1353 }
1354 
1358 }
1359 
1360 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1361  uintptr_t IntPtr = 0;
1362  if (std::error_code EC =
1363  OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
1364  return EC;
1365  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1366  return std::error_code();
1367 }
1368 
1369 std::error_code
1371  Result = ImportTable[Index].ImportLookupTableRVA;
1372  return std::error_code();
1373 }
1374 
1375 std::error_code
1377  Result = ImportTable[Index].ImportAddressTableRVA;
1378  return std::error_code();
1379 }
1380 
1383  return Table == Other.Table && Index == Other.Index;
1384 }
1385 
1387  ++Index;
1388 }
1389 
1392  return importedSymbolBegin(Table[Index].DelayImportNameTable,
1393  OwningObject);
1394 }
1395 
1398  return importedSymbolEnd(Table[Index].DelayImportNameTable,
1399  OwningObject);
1400 }
1401 
1405 }
1406 
1407 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1408  uintptr_t IntPtr = 0;
1409  if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1410  return EC;
1411  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1412  return std::error_code();
1413 }
1414 
1415 std::error_code DelayImportDirectoryEntryRef::
1417  Result = Table;
1418  return std::error_code();
1419 }
1420 
1421 std::error_code DelayImportDirectoryEntryRef::
1422 getImportAddress(int AddrIndex, uint64_t &Result) const {
1423  uint32_t RVA = Table[Index].DelayImportAddressTable +
1424  AddrIndex * (OwningObject->is64() ? 8 : 4);
1425  uintptr_t IntPtr = 0;
1426  if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1427  return EC;
1428  if (OwningObject->is64())
1429  Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
1430  else
1431  Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
1432  return std::error_code();
1433 }
1434 
1437  return ExportTable == Other.ExportTable && Index == Other.Index;
1438 }
1439 
1441  ++Index;
1442 }
1443 
1444 // Returns the name of the current export symbol. If the symbol is exported only
1445 // by ordinal, the empty string is set as a result.
1446 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1447  uintptr_t IntPtr = 0;
1448  if (std::error_code EC =
1449  OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1450  return EC;
1451  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1452  return std::error_code();
1453 }
1454 
1455 // Returns the starting ordinal number.
1456 std::error_code
1458  Result = ExportTable->OrdinalBase;
1459  return std::error_code();
1460 }
1461 
1462 // Returns the export ordinal of the current export symbol.
1463 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1464  Result = ExportTable->OrdinalBase + Index;
1465  return std::error_code();
1466 }
1467 
1468 // Returns the address of the current export symbol.
1469 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1470  uintptr_t IntPtr = 0;
1471  if (std::error_code EC =
1472  OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
1473  return EC;
1474  const export_address_table_entry *entry =
1475  reinterpret_cast<const export_address_table_entry *>(IntPtr);
1476  Result = entry[Index].ExportRVA;
1477  return std::error_code();
1478 }
1479 
1480 // Returns the name of the current export symbol. If the symbol is exported only
1481 // by ordinal, the empty string is set as a result.
1482 std::error_code
1484  uintptr_t IntPtr = 0;
1485  if (std::error_code EC =
1486  OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
1487  return EC;
1488  const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1489 
1490  uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1491  int Offset = 0;
1492  for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1493  I < E; ++I, ++Offset) {
1494  if (*I != Index)
1495  continue;
1496  if (std::error_code EC =
1497  OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
1498  return EC;
1499  const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1500  if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1501  return EC;
1502  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1503  return std::error_code();
1504  }
1505  Result = "";
1506  return std::error_code();
1507 }
1508 
1509 std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1510  const data_directory *DataEntry;
1511  if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1512  return EC;
1513  uint32_t RVA;
1514  if (auto EC = getExportRVA(RVA))
1515  return EC;
1516  uint32_t Begin = DataEntry->RelativeVirtualAddress;
1517  uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1518  Result = (Begin <= RVA && RVA < End);
1519  return std::error_code();
1520 }
1521 
1522 std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1523  uint32_t RVA;
1524  if (auto EC = getExportRVA(RVA))
1525  return EC;
1526  uintptr_t IntPtr = 0;
1527  if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1528  return EC;
1529  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1530  return std::error_code();
1531 }
1532 
1534 operator==(const ImportedSymbolRef &Other) const {
1535  return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1536  && Index == Other.Index;
1537 }
1538 
1540  ++Index;
1541 }
1542 
1543 std::error_code
1545  uint32_t RVA;
1546  if (Entry32) {
1547  // If a symbol is imported only by ordinal, it has no name.
1548  if (Entry32[Index].isOrdinal())
1549  return std::error_code();
1550  RVA = Entry32[Index].getHintNameRVA();
1551  } else {
1552  if (Entry64[Index].isOrdinal())
1553  return std::error_code();
1554  RVA = Entry64[Index].getHintNameRVA();
1555  }
1556  uintptr_t IntPtr = 0;
1557  if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1558  return EC;
1559  // +2 because the first two bytes is hint.
1560  Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1561  return std::error_code();
1562 }
1563 
1564 std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1565  if (Entry32)
1566  Result = Entry32[Index].isOrdinal();
1567  else
1568  Result = Entry64[Index].isOrdinal();
1569  return std::error_code();
1570 }
1571 
1572 std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1573  if (Entry32)
1574  Result = Entry32[Index].getHintNameRVA();
1575  else
1576  Result = Entry64[Index].getHintNameRVA();
1577  return std::error_code();
1578 }
1579 
1580 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1581  uint32_t RVA;
1582  if (Entry32) {
1583  if (Entry32[Index].isOrdinal()) {
1584  Result = Entry32[Index].getOrdinal();
1585  return std::error_code();
1586  }
1587  RVA = Entry32[Index].getHintNameRVA();
1588  } else {
1589  if (Entry64[Index].isOrdinal()) {
1590  Result = Entry64[Index].getOrdinal();
1591  return std::error_code();
1592  }
1593  RVA = Entry64[Index].getHintNameRVA();
1594  }
1595  uintptr_t IntPtr = 0;
1596  if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1597  return EC;
1598  Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1599  return std::error_code();
1600 }
1601 
1604  std::error_code EC;
1605  std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
1606  if (EC)
1607  return EC;
1608  return std::move(Ret);
1609 }
1610 
1611 bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1612  return Header == Other.Header && Index == Other.Index;
1613 }
1614 
1616  // Header->BlockSize is the size of the current block, including the
1617  // size of the header itself.
1618  uint32_t Size = sizeof(*Header) +
1619  sizeof(coff_base_reloc_block_entry) * (Index + 1);
1620  if (Size == Header->BlockSize) {
1621  // .reloc contains a list of base relocation blocks. Each block
1622  // consists of the header followed by entries. The header contains
1623  // how many entories will follow. When we reach the end of the
1624  // current block, proceed to the next block.
1625  Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1626  reinterpret_cast<const uint8_t *>(Header) + Size);
1627  Index = 0;
1628  } else {
1629  ++Index;
1630  }
1631 }
1632 
1633 std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1634  auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1635  Type = Entry[Index].getType();
1636  return std::error_code();
1637 }
1638 
1639 std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1640  auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1641  Result = Header->PageRVA + Entry[Index].getOffset();
1642  return std::error_code();
1643 }
1644 
1645 #define RETURN_IF_ERROR(X) \
1646  if (auto EC = errorToErrorCode(X)) \
1647  return EC;
1648 
1649 ErrorOr<ArrayRef<UTF16>> ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) {
1650  BinaryStreamReader Reader = BinaryStreamReader(BBS);
1651  Reader.setOffset(Offset);
1652  uint16_t Length;
1653  RETURN_IF_ERROR(Reader.readInteger(Length));
1654  ArrayRef<UTF16> RawDirString;
1655  RETURN_IF_ERROR(Reader.readArray(RawDirString, Length));
1656  return RawDirString;
1657 }
1658 
1661  return getDirStringAtOffset(Entry.Identifier.getNameOffset());
1662 }
1663 
1665 ResourceSectionRef::getTableAtOffset(uint32_t Offset) {
1666  const coff_resource_dir_table *Table = nullptr;
1667 
1668  BinaryStreamReader Reader(BBS);
1669  Reader.setOffset(Offset);
1670  RETURN_IF_ERROR(Reader.readObject(Table));
1671  assert(Table != nullptr);
1672  return *Table;
1673 }
1674 
1677  return getTableAtOffset(Entry.Offset.value());
1678 }
1679 
1681  return getTableAtOffset(0);
1682 }
std::error_code getRVA(uint32_t &Result) const
uint32_t getNumberOfSections() const
Definition: COFF.h:818
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:497
bool operator==(const ImportDirectoryEntryRef &Other) const
bool isOrdinal() const
Definition: COFF.h:194
static uint32_t getNumberOfRelocations(const coff_section *Sec, MemoryBufferRef M, const uint8_t *base)
std::error_code getImportTableEntry(const coff_import_directory_table_entry *&Result) const
Represents either an error or a value T.
Definition: ErrorOr.h:69
The 64-bit PE header that follows the COFF header.
Definition: COFF.h:142
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:234
section_iterator section_begin() const override
support::ulittle16_t Machine
Definition: COFF.h:78
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:205
friend class SymbolRef
Definition: ObjectFile.h:207
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:103
iterator_range< imported_symbol_iterator > imported_symbols() const
bool isNull() const
Definition: COFF.h:538
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream's offset.
support::ulittle32_t Name
Definition: COFF.h:215
support::ulittle32_t OrdinalBase
Definition: COFF.h:230
const void * getRawPtr() const
Definition: COFF.h:285
std::error_code getDelayImportTable(const delay_import_directory_table_entry *&Result) const
friend class SectionRef
Definition: ObjectFile.h:221
COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
uint64_t getRelocationOffset(DataRefImpl Rel) const override
std::error_code getImportAddress(int AddrIndex, uint64_t &Result) const
imported_symbol_iterator imported_symbol_begin() const
const uint8_t * base() const
Definition: ObjectFile.h:195
support::ulittle32_t VirtualAddress
Definition: COFF.h:420
import_directory_iterator import_directory_end() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
void moveSectionNext(DataRefImpl &Sec) const override
ErrorOr< const coff_resource_dir_table & > getBaseTable()
static std::error_code getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
std::error_code getDebugPDBInfo(const debug_directory *DebugDir, const codeview::DebugInfo *&Info, StringRef &PDBFileName) const
Get PDB information out of a codeview debug directory entry.
void moveRelocationNext(DataRefImpl &Rel) const override
static ErrorOr< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
support::ulittle16_t Version
Definition: COFF.h:92
This class is the base class for all object file types.
Definition: ObjectFile.h:189
support::ulittle16_t NumberOfSections
Definition: COFF.h:79
content_iterator< DelayImportDirectoryEntryRef > delay_import_directory_iterator
Definition: COFF.h:49
static const coff_relocation * getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base)
const char * getBufferStart() const
Definition: MemoryBuffer.h:173
bool isSectionCompressed(DataRefImpl Sec) const override
Definition: COFF.h:238
uint32_t getValue() const
Definition: COFF.h:316
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
Definition: Endian.h:269
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:454
std::error_code getImportAddressTableRVA(uint32_t &Result) const
uint32_t getAlignment() const
Definition: COFF.h:436
std::error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
const coff_section * getCOFFSection(const SectionRef &Section) const
std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override
std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const
static const char BigObjMagic[]
Definition: COFF.h:39
support::ulittle32_t DelayImportAddressTable
Definition: COFF.h:217
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
basic_symbol_iterator symbol_end() const override
bool isImportLibrary() const
Definition: COFF.h:86
detail::packed_endian_specific_integral< uint64_t, little, unaligned > ulittle64_t
Definition: Endian.h:273
std::error_code isOrdinal(bool &Result) const
support::ulittle64_t ImageBase
Definition: COFF.h:151
support::ulittle32_t NameRVA
Definition: COFF.h:229
uint16_t getOrdinal() const
Definition: COFF.h:196
friend class ExportDirectoryEntryRef
Definition: COFF.h:736
uint64_t getSectionAlignment(DataRefImpl Sec) const override
char Name[COFF::NameSize]
Definition: COFF.h:418
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:451
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
support::ulittle16_t SizeOfOptionalHeader
Definition: COFF.h:83
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
bool operator==(const ExportDirectoryEntryRef &Other) const
iterator_range< base_reloc_iterator > base_relocs() const
bool is64() const
Definition: COFF.h:1030
std::error_code getOrdinal(uint32_t &Result) const
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
static imported_symbol_iterator importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object)
iterator_range< import_directory_iterator > import_directories() const
bool isReservedSectionNumber(int32_t SectionNumber)
Definition: COFF.h:712
support::ulittle32_t VirtualSize
Definition: COFF.h:419
#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type)
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:48
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
uint32_t getSymbolFlags(DataRefImpl Symb) const override
std::error_code getName(StringRef &Result) const
uint64_t getSectionAddress(DataRefImpl Sec) const override
iterator_range< const coff_relocation * > getRelocations(const coff_section *Sec) const
std::error_code getSymbolName(StringRef &Result) const
void moveSymbolNext(DataRefImpl &Symb) const override
support::ulittle32_t NamePointerRVA
Definition: COFF.h:234
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
support::ulittle32_t ExportRVA
Definition: COFF.h:239
support::ulittle32_t NameRVA
Definition: COFF.h:535
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
const char * Name
unsigned getArch() const override
imported_symbol_iterator imported_symbol_end() const
iterator begin() const
Definition: StringRef.h:106
unsigned getSectionID(SectionRef Sec) const
static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size)
relocation_iterator section_rel_end(DataRefImpl Sec) const override
support::ulittle16_t Magic
Definition: COFF.h:107
bool isExternal() const
Definition: COFF.h:354
std::error_code getForwardTo(StringRef &Result) const
#define P(N)
support::ulittle32_t Characteristics
Definition: COFF.h:427
support::ulittle32_t NumberOfRvaAndSize
Definition: COFF.h:171
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
content_iterator< ExportDirectoryEntryRef > export_directory_iterator
Definition: COFF.h:50
bool isFileRecord() const
Definition: COFF.h:386
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:271
uint16_t getMachine() const
Definition: COFF.h:782
int getType() const
Definition: COFF.h:690
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
std::error_code getDataDirectory(uint32_t index, const data_directory *&Res) const
ErrorOr< ArrayRef< UTF16 > > getEntryNameString(const coff_resource_dir_entry &Entry)
const char * getBufferEnd() const
Definition: MemoryBuffer.h:174
int32_t getSectionNumber() const
Definition: COFF.h:318
content_iterator< ImportedSymbolRef > imported_symbol_iterator
Definition: COFF.h:51
The 32-bit PE header that follows the COFF header.
Definition: COFF.h:106
std::error_code getImportLookupTableRVA(uint32_t &Result) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
import_lookup_table_entry< support::little64_t > import_lookup_table_entry64
Definition: COFF.h:210
export_directory_iterator export_directory_end() const
base_reloc_iterator base_reloc_begin() const
support::ulittle32_t RelativeVirtualAddress
Definition: COFF.h:175
content_iterator< ImportDirectoryEntryRef > import_directory_iterator
Definition: COFF.h:47
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
std::error_code getOrdinal(uint16_t &Result) const
uint8_t getComplexType() const
Definition: COFF.h:346
std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const
static const unsigned End
A function that returns a base type.
Definition: COFF.h:259
support::ulittle32_t ImportAddressTableRVA
Definition: COFF.h:536
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
iterator_range< const debug_directory * > debug_directories() const
Definition: COFF.h:933
support::ulittle16_t Type
Definition: COFF.h:453
support::ulittle32_t OrdinalTableRVA
Definition: COFF.h:235
COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const
section_iterator_range sections() const
Definition: ObjectFile.h:272
export_directory_iterator export_directory_begin() const
static imported_symbol_iterator makeImportedSymbolIterator(const COFFObjectFile *Object, uintptr_t Ptr, int Index)
uint64_t getRelocationType(DataRefImpl Rel) const override
imported_symbol_iterator lookup_table_end() const
StringRef getFileFormatName() const override
support::ulittle32_t VirtualAddress
Definition: COFF.h:451
support::ulittle32_t SymbolTableIndex
Definition: COFF.h:452
union llvm::object::coff_resource_dir_entry::@224 Offset
Definition: COFF.h:531
size_t getBufferSize() const
Definition: MemoryBuffer.h:175
friend class ImportDirectoryEntryRef
Definition: COFF.h:735
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
import_directory_iterator import_directory_begin() const
void setOffset(uint32_t Off)
support::ulittle32_t PointerToRawData
Definition: COFF.h:422
friend class RelocationRef
Definition: ObjectFile.h:244
#define E
Definition: LargeTest.cpp:27
bool isWeakExternal() const
Definition: COFF.h:368
#define RETURN_IF_ERROR(X)
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
basic_symbol_iterator symbol_begin() const override
std::error_code getRvaAndSizeAsBytes(uint32_t RVA, uint32_t Size, ArrayRef< uint8_t > &Contents) const
Given an RVA base and size, returns a valid array of bytes or an error code if the RVA and size is no...
static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result)
iterator_range< imported_symbol_iterator > lookup_table_symbols() const
Definition: COFF.h:687
imported_symbol_iterator imported_symbol_end() const
bool isSectionData(DataRefImpl Sec) const override
const dos_header * getDOSHeader() const
Definition: COFF.h:937
imported_symbol_iterator lookup_table_begin() const
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:139
The DOS compatible header at the front of all PE/COFF executables.
Definition: COFF.h:55
A range adaptor for a pair of iterators.
section_iterator section_end() const override
support::ulittle32_t AddressOfRawData
Definition: COFF.h:186
MemoryBufferRef Data
Definition: Binary.h:36
delay_import_directory_iterator delay_import_directory_begin() const
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:727
support::ulittle32_t Size
Definition: COFF.h:176
uint8_t getNumberOfAuxSymbols() const
Definition: COFF.h:339
unsigned getSymbolSectionID(SymbolRef Sym) const
std::error_code getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:128
support::ulittle32_t ImageBase
Definition: COFF.h:116
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:395
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
std::error_code getHintNameRVA(uint32_t &Result) const
llvm_unreachable("Unknown RegBank!")
const coff_relocation * getCOFFRelocation(const RelocationRef &Reloc) const
imported_symbol_iterator imported_symbol_begin() const
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
delay_import_directory_iterator delay_import_directory_end() const
support::ulittle32_t NumberOfNamePointers
Definition: COFF.h:232
std::error_code getPE32Header(const pe32_header *&Res) const
bool isSectionDefinition() const
Definition: COFF.h:394
Definition: COFF.h:694
iterator_range< imported_symbol_iterator > imported_symbols() const
bool isSectionText(DataRefImpl Sec) const override
bool operator==(const DelayImportDirectoryEntryRef &Other) const
uintptr_t getSymbolTable() const
Definition: COFF.h:774
std::error_code getDllName(StringRef &Result) const
bool operator==(const BaseRelocRef &Other) const
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:51
support::ulittle32_t ImportLookupTableRVA
Definition: COFF.h:532
detail::packed_endian_specific_integral< int16_t, little, unaligned > little16_t
Definition: Endian.h:276
support::ulittle32_t AddressTableEntries
Definition: COFF.h:231
#define I(x, y, z)
Definition: MD5.cpp:58
static const char PEMagic[]
Definition: COFF.h:37
bool isCommon() const
Definition: COFF.h:358
size_t getSymbolTableEntrySize() const
Definition: COFF.h:988
support::ulittle32_t BlockSize
Definition: COFF.h:684
Definition: COFF.h:212
iterator_range< export_directory_iterator > export_directories() const
bool isSectionVirtual(DataRefImpl Sec) const override
static imported_symbol_iterator importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object)
bool operator==(const ImportedSymbolRef &Other) const
uint64_t getSectionSize(DataRefImpl Sec) const override
uint32_t getNumberOfSymbols() const
Definition: COFF.h:843
uint8_t NumberOfAuxSymbols
Definition: COFF.h:264
bool isAnyUndefined() const
Definition: COFF.h:382
support::ulittle32_t SizeOfData
Definition: COFF.h:185
bool isSectionBSS(DataRefImpl Sec) const override
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
base_reloc_iterator base_reloc_end() const
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:188
std::error_code getSymbolName(StringRef &Result) const
std::error_code getOrdinalBase(uint32_t &Result) const
bool isNull(StringRef S)
Definition: YAMLTraits.h:488
LLVM Value Representation.
Definition: Value.h:73
support::ulittle32_t ExportAddressTableRVA
Definition: COFF.h:233
content_iterator< BaseRelocRef > base_reloc_iterator
Definition: COFF.h:52
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
import_lookup_table_entry< support::little32_t > import_lookup_table_entry32
Definition: COFF.h:208
iterator end() const
Definition: StringRef.h:108
support::ulittle32_t NumberOfRvaAndSize
Definition: COFF.h:138
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
union llvm::object::coff_resource_dir_entry::@223 Identifier
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
std::error_code getType(uint8_t &Type) const
int * Ptr
uint32_t getPointerToSymbolTable() const
Definition: COFF.h:826
std::error_code getName(StringRef &Result) const
uint32_t getHintNameRVA() const
Definition: COFF.h:201
ArrayRef< uint8_t > getSymbolAuxData(COFFSymbolRef Symbol) const
const T * data() const
Definition: ArrayRef.h:146
std::error_code getSection(int32_t index, const coff_section *&Res) const
std::error_code isForwarder(bool &Result) const
uint64_t getSectionIndex(DataRefImpl Sec) const override
#define D
Definition: LargeTest.cpp:26
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
std::error_code getExportRVA(uint32_t &Result) const
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
uint64_t PowerOf2Ceil(uint64_t A)
Returns the power of two which is greater than or equal to the given value.
Definition: MathExtras.h:651
iterator_range< delay_import_directory_iterator > delay_import_directories() const
ErrorOr< const coff_resource_dir_table & > getEntrySubDir(const coff_resource_dir_entry &Entry)
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:492