LLVM  9.0.0svn
COFFObjectFile.cpp
Go to the documentation of this file.
1 //===- COFFObjectFile.cpp - COFF object file implementation ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the COFFObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Triple.h"
17 #include "llvm/BinaryFormat/COFF.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Object/Error.h"
21 #include "llvm/Object/ObjectFile.h"
23 #include "llvm/Support/Endian.h"
24 #include "llvm/Support/Error.h"
28 #include <algorithm>
29 #include <cassert>
30 #include <cstddef>
31 #include <cstdint>
32 #include <cstring>
33 #include <limits>
34 #include <memory>
35 #include <system_error>
36 
37 using namespace llvm;
38 using namespace object;
39 
44 
45 // Returns false if size is greater than the buffer size. And sets ec.
46 static bool checkSize(MemoryBufferRef M, std::error_code &EC, uint64_t Size) {
47  if (M.getBufferSize() < Size) {
49  return false;
50  }
51  return true;
52 }
53 
54 // Sets Obj unless any bytes in [addr, addr + size) fall outsize of m.
55 // Returns unexpected_eof if error.
56 template <typename T>
57 static std::error_code getObject(const T *&Obj, MemoryBufferRef M,
58  const void *Ptr,
59  const uint64_t Size = sizeof(T)) {
60  uintptr_t Addr = uintptr_t(Ptr);
61  if (std::error_code EC = Binary::checkOffset(M, Addr, Size))
62  return EC;
63  Obj = reinterpret_cast<const T *>(Addr);
64  return std::error_code();
65 }
66 
67 // Decode a string table entry in base 64 (//AAAAAA). Expects \arg Str without
68 // prefixed slashes.
69 static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result) {
70  assert(Str.size() <= 6 && "String too long, possible overflow.");
71  if (Str.size() > 6)
72  return true;
73 
74  uint64_t Value = 0;
75  while (!Str.empty()) {
76  unsigned CharVal;
77  if (Str[0] >= 'A' && Str[0] <= 'Z') // 0..25
78  CharVal = Str[0] - 'A';
79  else if (Str[0] >= 'a' && Str[0] <= 'z') // 26..51
80  CharVal = Str[0] - 'a' + 26;
81  else if (Str[0] >= '0' && Str[0] <= '9') // 52..61
82  CharVal = Str[0] - '0' + 52;
83  else if (Str[0] == '+') // 62
84  CharVal = 62;
85  else if (Str[0] == '/') // 63
86  CharVal = 63;
87  else
88  return true;
89 
90  Value = (Value * 64) + CharVal;
91  Str = Str.substr(1);
92  }
93 
95  return true;
96 
97  Result = static_cast<uint32_t>(Value);
98  return false;
99 }
100 
101 template <typename coff_symbol_type>
102 const coff_symbol_type *COFFObjectFile::toSymb(DataRefImpl Ref) const {
103  const coff_symbol_type *Addr =
104  reinterpret_cast<const coff_symbol_type *>(Ref.p);
105 
106  assert(!checkOffset(Data, uintptr_t(Addr), sizeof(*Addr)));
107 #ifndef NDEBUG
108  // Verify that the symbol points to a valid entry in the symbol table.
109  uintptr_t Offset = uintptr_t(Addr) - uintptr_t(base());
110 
111  assert((Offset - getPointerToSymbolTable()) % sizeof(coff_symbol_type) == 0 &&
112  "Symbol did not point to the beginning of a symbol");
113 #endif
114 
115  return Addr;
116 }
117 
118 const coff_section *COFFObjectFile::toSec(DataRefImpl Ref) const {
119  const coff_section *Addr = reinterpret_cast<const coff_section*>(Ref.p);
120 
121 #ifndef NDEBUG
122  // Verify that the section points to a valid entry in the section table.
123  if (Addr < SectionTable || Addr >= (SectionTable + getNumberOfSections()))
124  report_fatal_error("Section was outside of section table.");
125 
126  uintptr_t Offset = uintptr_t(Addr) - uintptr_t(SectionTable);
127  assert(Offset % sizeof(coff_section) == 0 &&
128  "Section did not point to the beginning of a section");
129 #endif
130 
131  return Addr;
132 }
133 
135  auto End = reinterpret_cast<uintptr_t>(StringTable);
136  if (SymbolTable16) {
137  const coff_symbol16 *Symb = toSymb<coff_symbol16>(Ref);
138  Symb += 1 + Symb->NumberOfAuxSymbols;
139  Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
140  } else if (SymbolTable32) {
141  const coff_symbol32 *Symb = toSymb<coff_symbol32>(Ref);
142  Symb += 1 + Symb->NumberOfAuxSymbols;
143  Ref.p = std::min(reinterpret_cast<uintptr_t>(Symb), End);
144  } else {
145  llvm_unreachable("no symbol table pointer!");
146  }
147 }
148 
150  COFFSymbolRef Symb = getCOFFSymbol(Ref);
151  StringRef Result;
152  if (std::error_code EC = getSymbolName(Symb, Result))
153  return errorCodeToError(EC);
154  return Result;
155 }
156 
158  return getCOFFSymbol(Ref).getValue();
159 }
160 
162  // MSVC/link.exe seems to align symbols to the next-power-of-2
163  // up to 32 bytes.
164  COFFSymbolRef Symb = getCOFFSymbol(Ref);
165  return std::min(uint64_t(32), PowerOf2Ceil(Symb.getValue()));
166 }
167 
169  uint64_t Result = getSymbolValue(Ref);
170  COFFSymbolRef Symb = getCOFFSymbol(Ref);
171  int32_t SectionNumber = Symb.getSectionNumber();
172 
173  if (Symb.isAnyUndefined() || Symb.isCommon() ||
174  COFF::isReservedSectionNumber(SectionNumber))
175  return Result;
176 
177  const coff_section *Section = nullptr;
178  if (std::error_code EC = getSection(SectionNumber, Section))
179  return errorCodeToError(EC);
180  Result += Section->VirtualAddress;
181 
182  // The section VirtualAddress does not include ImageBase, and we want to
183  // return virtual addresses.
184  Result += getImageBase();
185 
186  return Result;
187 }
188 
190  COFFSymbolRef Symb = getCOFFSymbol(Ref);
191  int32_t SectionNumber = Symb.getSectionNumber();
192 
194  return SymbolRef::ST_Function;
195  if (Symb.isAnyUndefined())
196  return SymbolRef::ST_Unknown;
197  if (Symb.isCommon())
198  return SymbolRef::ST_Data;
199  if (Symb.isFileRecord())
200  return SymbolRef::ST_File;
201 
202  // TODO: perhaps we need a new symbol type ST_Section.
203  if (SectionNumber == COFF::IMAGE_SYM_DEBUG || Symb.isSectionDefinition())
204  return SymbolRef::ST_Debug;
205 
206  if (!COFF::isReservedSectionNumber(SectionNumber))
207  return SymbolRef::ST_Data;
208 
209  return SymbolRef::ST_Other;
210 }
211 
213  COFFSymbolRef Symb = getCOFFSymbol(Ref);
214  uint32_t Result = SymbolRef::SF_None;
215 
216  if (Symb.isExternal() || Symb.isWeakExternal())
217  Result |= SymbolRef::SF_Global;
218 
219  if (const coff_aux_weak_external *AWE = Symb.getWeakExternal()) {
220  Result |= SymbolRef::SF_Weak;
221  if (AWE->Characteristics != COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS)
222  Result |= SymbolRef::SF_Undefined;
223  }
224 
226  Result |= SymbolRef::SF_Absolute;
227 
228  if (Symb.isFileRecord())
230 
231  if (Symb.isSectionDefinition())
233 
234  if (Symb.isCommon())
235  Result |= SymbolRef::SF_Common;
236 
237  if (Symb.isUndefined())
238  Result |= SymbolRef::SF_Undefined;
239 
240  return Result;
241 }
242 
244  COFFSymbolRef Symb = getCOFFSymbol(Ref);
245  return Symb.getValue();
246 }
247 
250  COFFSymbolRef Symb = getCOFFSymbol(Ref);
252  return section_end();
253  const coff_section *Sec = nullptr;
254  if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
255  return errorCodeToError(EC);
257  Ret.p = reinterpret_cast<uintptr_t>(Sec);
258  return section_iterator(SectionRef(Ret, this));
259 }
260 
263  return Symb.getSectionNumber();
264 }
265 
267  const coff_section *Sec = toSec(Ref);
268  Sec += 1;
269  Ref.p = reinterpret_cast<uintptr_t>(Sec);
270 }
271 
273  const coff_section *Sec = toSec(Ref);
274  return getSectionName(Sec);
275 }
276 
278  const coff_section *Sec = toSec(Ref);
279  uint64_t Result = Sec->VirtualAddress;
280 
281  // The section VirtualAddress does not include ImageBase, and we want to
282  // return virtual addresses.
283  Result += getImageBase();
284  return Result;
285 }
286 
288  return toSec(Sec) - SectionTable;
289 }
290 
291 uint64_t COFFObjectFile::getSectionSize(DataRefImpl Ref) const {
292  return getSectionSize(toSec(Ref));
293 }
294 
297  const coff_section *Sec = toSec(Ref);
298  ArrayRef<uint8_t> Res;
299  if (Error E = getSectionContents(Sec, Res))
300  return std::move(E);
301  return Res;
302 }
303 
305  const coff_section *Sec = toSec(Ref);
306  return Sec->getAlignment();
307 }
308 
310  return false;
311 }
312 
314  const coff_section *Sec = toSec(Ref);
316 }
317 
319  const coff_section *Sec = toSec(Ref);
321 }
322 
324  const coff_section *Sec = toSec(Ref);
328  return (Sec->Characteristics & BssFlags) == BssFlags;
329 }
330 
332  uintptr_t Offset =
333  uintptr_t(Sec.getRawDataRefImpl().p) - uintptr_t(SectionTable);
334  assert((Offset % sizeof(coff_section)) == 0);
335  return (Offset / sizeof(coff_section)) + 1;
336 }
337 
339  const coff_section *Sec = toSec(Ref);
340  // In COFF, a virtual section won't have any in-file
341  // content, so the file pointer to the content will be zero.
342  return Sec->PointerToRawData == 0;
343 }
344 
345 static uint32_t getNumberOfRelocations(const coff_section *Sec,
346  MemoryBufferRef M, const uint8_t *base) {
347  // The field for the number of relocations in COFF section table is only
348  // 16-bit wide. If a section has more than 65535 relocations, 0xFFFF is set to
349  // NumberOfRelocations field, and the actual relocation count is stored in the
350  // VirtualAddress field in the first relocation entry.
351  if (Sec->hasExtendedRelocations()) {
352  const coff_relocation *FirstReloc;
353  if (getObject(FirstReloc, M, reinterpret_cast<const coff_relocation*>(
354  base + Sec->PointerToRelocations)))
355  return 0;
356  // -1 to exclude this first relocation entry.
357  return FirstReloc->VirtualAddress - 1;
358  }
359  return Sec->NumberOfRelocations;
360 }
361 
362 static const coff_relocation *
363 getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base) {
364  uint64_t NumRelocs = getNumberOfRelocations(Sec, M, Base);
365  if (!NumRelocs)
366  return nullptr;
367  auto begin = reinterpret_cast<const coff_relocation *>(
368  Base + Sec->PointerToRelocations);
369  if (Sec->hasExtendedRelocations()) {
370  // Skip the first relocation entry repurposed to store the number of
371  // relocations.
372  begin++;
373  }
374  if (Binary::checkOffset(M, uintptr_t(begin),
375  sizeof(coff_relocation) * NumRelocs))
376  return nullptr;
377  return begin;
378 }
379 
381  const coff_section *Sec = toSec(Ref);
382  const coff_relocation *begin = getFirstReloc(Sec, Data, base());
383  if (begin && Sec->VirtualAddress != 0)
384  report_fatal_error("Sections with relocations should have an address of 0");
386  Ret.p = reinterpret_cast<uintptr_t>(begin);
387  return relocation_iterator(RelocationRef(Ret, this));
388 }
389 
391  const coff_section *Sec = toSec(Ref);
392  const coff_relocation *I = getFirstReloc(Sec, Data, base());
393  if (I)
394  I += getNumberOfRelocations(Sec, Data, base());
396  Ret.p = reinterpret_cast<uintptr_t>(I);
397  return relocation_iterator(RelocationRef(Ret, this));
398 }
399 
400 // Initialize the pointer to the symbol table.
401 std::error_code COFFObjectFile::initSymbolTablePtr() {
402  if (COFFHeader)
403  if (std::error_code EC = getObject(
404  SymbolTable16, Data, base() + getPointerToSymbolTable(),
406  return EC;
407 
408  if (COFFBigObjHeader)
409  if (std::error_code EC = getObject(
410  SymbolTable32, Data, base() + getPointerToSymbolTable(),
412  return EC;
413 
414  // Find string table. The first four byte of the string table contains the
415  // total size of the string table, including the size field itself. If the
416  // string table is empty, the value of the first four byte would be 4.
419  const uint8_t *StringTableAddr = base() + StringTableOffset;
420  const ulittle32_t *StringTableSizePtr;
421  if (std::error_code EC = getObject(StringTableSizePtr, Data, StringTableAddr))
422  return EC;
423  StringTableSize = *StringTableSizePtr;
424  if (std::error_code EC =
425  getObject(StringTable, Data, StringTableAddr, StringTableSize))
426  return EC;
427 
428  // Treat table sizes < 4 as empty because contrary to the PECOFF spec, some
429  // tools like cvtres write a size of 0 for an empty table instead of 4.
430  if (StringTableSize < 4)
431  StringTableSize = 4;
432 
433  // Check that the string table is null terminated if has any in it.
434  if (StringTableSize > 4 && StringTable[StringTableSize - 1] != 0)
436  return std::error_code();
437 }
438 
440  if (PE32Header)
441  return PE32Header->ImageBase;
442  else if (PE32PlusHeader)
443  return PE32PlusHeader->ImageBase;
444  // This actually comes up in practice.
445  return 0;
446 }
447 
448 // Returns the file offset for the given VA.
449 std::error_code COFFObjectFile::getVaPtr(uint64_t Addr, uintptr_t &Res) const {
450  uint64_t ImageBase = getImageBase();
451  uint64_t Rva = Addr - ImageBase;
452  assert(Rva <= UINT32_MAX);
453  return getRvaPtr((uint32_t)Rva, Res);
454 }
455 
456 // Returns the file offset for the given RVA.
457 std::error_code COFFObjectFile::getRvaPtr(uint32_t Addr, uintptr_t &Res) const {
458  for (const SectionRef &S : sections()) {
459  const coff_section *Section = getCOFFSection(S);
460  uint32_t SectionStart = Section->VirtualAddress;
461  uint32_t SectionEnd = Section->VirtualAddress + Section->VirtualSize;
462  if (SectionStart <= Addr && Addr < SectionEnd) {
463  uint32_t Offset = Addr - SectionStart;
464  Res = uintptr_t(base()) + Section->PointerToRawData + Offset;
465  return std::error_code();
466  }
467  }
469 }
470 
471 std::error_code
473  ArrayRef<uint8_t> &Contents) const {
474  for (const SectionRef &S : sections()) {
475  const coff_section *Section = getCOFFSection(S);
476  uint32_t SectionStart = Section->VirtualAddress;
477  // Check if this RVA is within the section bounds. Be careful about integer
478  // overflow.
479  uint32_t OffsetIntoSection = RVA - SectionStart;
480  if (SectionStart <= RVA && OffsetIntoSection < Section->VirtualSize &&
481  Size <= Section->VirtualSize - OffsetIntoSection) {
482  uintptr_t Begin =
483  uintptr_t(base()) + Section->PointerToRawData + OffsetIntoSection;
484  Contents =
485  ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Begin), Size);
486  return std::error_code();
487  }
488  }
490 }
491 
492 // Returns hint and name fields, assuming \p Rva is pointing to a Hint/Name
493 // table entry.
494 std::error_code COFFObjectFile::getHintName(uint32_t Rva, uint16_t &Hint,
495  StringRef &Name) const {
496  uintptr_t IntPtr = 0;
497  if (std::error_code EC = getRvaPtr(Rva, IntPtr))
498  return EC;
499  const uint8_t *Ptr = reinterpret_cast<const uint8_t *>(IntPtr);
500  Hint = *reinterpret_cast<const ulittle16_t *>(Ptr);
501  Name = StringRef(reinterpret_cast<const char *>(Ptr + 2));
502  return std::error_code();
503 }
504 
505 std::error_code
507  const codeview::DebugInfo *&PDBInfo,
508  StringRef &PDBFileName) const {
509  ArrayRef<uint8_t> InfoBytes;
510  if (std::error_code EC = getRvaAndSizeAsBytes(
511  DebugDir->AddressOfRawData, DebugDir->SizeOfData, InfoBytes))
512  return EC;
513  if (InfoBytes.size() < sizeof(*PDBInfo) + 1)
515  PDBInfo = reinterpret_cast<const codeview::DebugInfo *>(InfoBytes.data());
516  InfoBytes = InfoBytes.drop_front(sizeof(*PDBInfo));
517  PDBFileName = StringRef(reinterpret_cast<const char *>(InfoBytes.data()),
518  InfoBytes.size());
519  // Truncate the name at the first null byte. Ignore any padding.
520  PDBFileName = PDBFileName.split('\0').first;
521  return std::error_code();
522 }
523 
524 std::error_code
526  StringRef &PDBFileName) const {
527  for (const debug_directory &D : debug_directories())
529  return getDebugPDBInfo(&D, PDBInfo, PDBFileName);
530  // If we get here, there is no PDB info to return.
531  PDBInfo = nullptr;
532  PDBFileName = StringRef();
533  return std::error_code();
534 }
535 
536 // Find the import table.
537 std::error_code COFFObjectFile::initImportTablePtr() {
538  // First, we get the RVA of the import table. If the file lacks a pointer to
539  // the import table, do nothing.
540  const data_directory *DataEntry;
541  if (getDataDirectory(COFF::IMPORT_TABLE, DataEntry))
542  return std::error_code();
543 
544  // Do nothing if the pointer to import table is NULL.
545  if (DataEntry->RelativeVirtualAddress == 0)
546  return std::error_code();
547 
548  uint32_t ImportTableRva = DataEntry->RelativeVirtualAddress;
549 
550  // Find the section that contains the RVA. This is needed because the RVA is
551  // the import table's memory address which is different from its file offset.
552  uintptr_t IntPtr = 0;
553  if (std::error_code EC = getRvaPtr(ImportTableRva, IntPtr))
554  return EC;
555  if (std::error_code EC = checkOffset(Data, IntPtr, DataEntry->Size))
556  return EC;
557  ImportDirectory = reinterpret_cast<
558  const coff_import_directory_table_entry *>(IntPtr);
559  return std::error_code();
560 }
561 
562 // Initializes DelayImportDirectory and NumberOfDelayImportDirectory.
563 std::error_code COFFObjectFile::initDelayImportTablePtr() {
564  const data_directory *DataEntry;
566  return std::error_code();
567  if (DataEntry->RelativeVirtualAddress == 0)
568  return std::error_code();
569 
570  uint32_t RVA = DataEntry->RelativeVirtualAddress;
571  NumberOfDelayImportDirectory = DataEntry->Size /
573 
574  uintptr_t IntPtr = 0;
575  if (std::error_code EC = getRvaPtr(RVA, IntPtr))
576  return EC;
577  DelayImportDirectory = reinterpret_cast<
578  const delay_import_directory_table_entry *>(IntPtr);
579  return std::error_code();
580 }
581 
582 // Find the export table.
583 std::error_code COFFObjectFile::initExportTablePtr() {
584  // First, we get the RVA of the export table. If the file lacks a pointer to
585  // the export table, do nothing.
586  const data_directory *DataEntry;
587  if (getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
588  return std::error_code();
589 
590  // Do nothing if the pointer to export table is NULL.
591  if (DataEntry->RelativeVirtualAddress == 0)
592  return std::error_code();
593 
594  uint32_t ExportTableRva = DataEntry->RelativeVirtualAddress;
595  uintptr_t IntPtr = 0;
596  if (std::error_code EC = getRvaPtr(ExportTableRva, IntPtr))
597  return EC;
598  ExportDirectory =
599  reinterpret_cast<const export_directory_table_entry *>(IntPtr);
600  return std::error_code();
601 }
602 
603 std::error_code COFFObjectFile::initBaseRelocPtr() {
604  const data_directory *DataEntry;
606  return std::error_code();
607  if (DataEntry->RelativeVirtualAddress == 0)
608  return std::error_code();
609 
610  uintptr_t IntPtr = 0;
611  if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
612  return EC;
613  BaseRelocHeader = reinterpret_cast<const coff_base_reloc_block_header *>(
614  IntPtr);
615  BaseRelocEnd = reinterpret_cast<coff_base_reloc_block_header *>(
616  IntPtr + DataEntry->Size);
617  // FIXME: Verify the section containing BaseRelocHeader has at least
618  // DataEntry->Size bytes after DataEntry->RelativeVirtualAddress.
619  return std::error_code();
620 }
621 
622 std::error_code COFFObjectFile::initDebugDirectoryPtr() {
623  // Get the RVA of the debug directory. Do nothing if it does not exist.
624  const data_directory *DataEntry;
625  if (getDataDirectory(COFF::DEBUG_DIRECTORY, DataEntry))
626  return std::error_code();
627 
628  // Do nothing if the RVA is NULL.
629  if (DataEntry->RelativeVirtualAddress == 0)
630  return std::error_code();
631 
632  // Check that the size is a multiple of the entry size.
633  if (DataEntry->Size % sizeof(debug_directory) != 0)
635 
636  uintptr_t IntPtr = 0;
637  if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
638  return EC;
639  DebugDirectoryBegin = reinterpret_cast<const debug_directory *>(IntPtr);
640  DebugDirectoryEnd = reinterpret_cast<const debug_directory *>(
641  IntPtr + DataEntry->Size);
642  // FIXME: Verify the section containing DebugDirectoryBegin has at least
643  // DataEntry->Size bytes after DataEntry->RelativeVirtualAddress.
644  return std::error_code();
645 }
646 
647 std::error_code COFFObjectFile::initLoadConfigPtr() {
648  // Get the RVA of the debug directory. Do nothing if it does not exist.
649  const data_directory *DataEntry;
651  return std::error_code();
652 
653  // Do nothing if the RVA is NULL.
654  if (DataEntry->RelativeVirtualAddress == 0)
655  return std::error_code();
656  uintptr_t IntPtr = 0;
657  if (std::error_code EC = getRvaPtr(DataEntry->RelativeVirtualAddress, IntPtr))
658  return EC;
659 
660  LoadConfig = (const void *)IntPtr;
661  return std::error_code();
662 }
663 
665  : ObjectFile(Binary::ID_COFF, Object), COFFHeader(nullptr),
666  COFFBigObjHeader(nullptr), PE32Header(nullptr), PE32PlusHeader(nullptr),
667  DataDirectory(nullptr), SectionTable(nullptr), SymbolTable16(nullptr),
668  SymbolTable32(nullptr), StringTable(nullptr), StringTableSize(0),
669  ImportDirectory(nullptr),
670  DelayImportDirectory(nullptr), NumberOfDelayImportDirectory(0),
671  ExportDirectory(nullptr), BaseRelocHeader(nullptr), BaseRelocEnd(nullptr),
672  DebugDirectoryBegin(nullptr), DebugDirectoryEnd(nullptr) {
673  // Check that we at least have enough room for a header.
674  if (!checkSize(Data, EC, sizeof(coff_file_header)))
675  return;
676 
677  // The current location in the file where we are looking at.
678  uint64_t CurPtr = 0;
679 
680  // PE header is optional and is present only in executables. If it exists,
681  // it is placed right after COFF header.
682  bool HasPEHeader = false;
683 
684  // Check if this is a PE/COFF file.
685  if (checkSize(Data, EC, sizeof(dos_header) + sizeof(COFF::PEMagic))) {
686  // PE/COFF, seek through MS-DOS compatibility stub and 4-byte
687  // PE signature to find 'normal' COFF header.
688  const auto *DH = reinterpret_cast<const dos_header *>(base());
689  if (DH->Magic[0] == 'M' && DH->Magic[1] == 'Z') {
690  CurPtr = DH->AddressOfNewExeHeader;
691  // Check the PE magic bytes. ("PE\0\0")
692  if (memcmp(base() + CurPtr, COFF::PEMagic, sizeof(COFF::PEMagic)) != 0) {
694  return;
695  }
696  CurPtr += sizeof(COFF::PEMagic); // Skip the PE magic bytes.
697  HasPEHeader = true;
698  }
699  }
700 
701  if ((EC = getObject(COFFHeader, Data, base() + CurPtr)))
702  return;
703 
704  // It might be a bigobj file, let's check. Note that COFF bigobj and COFF
705  // import libraries share a common prefix but bigobj is more restrictive.
706  if (!HasPEHeader && COFFHeader->Machine == COFF::IMAGE_FILE_MACHINE_UNKNOWN &&
707  COFFHeader->NumberOfSections == uint16_t(0xffff) &&
708  checkSize(Data, EC, sizeof(coff_bigobj_file_header))) {
709  if ((EC = getObject(COFFBigObjHeader, Data, base() + CurPtr)))
710  return;
711 
712  // Verify that we are dealing with bigobj.
713  if (COFFBigObjHeader->Version >= COFF::BigObjHeader::MinBigObjectVersion &&
714  std::memcmp(COFFBigObjHeader->UUID, COFF::BigObjMagic,
715  sizeof(COFF::BigObjMagic)) == 0) {
716  COFFHeader = nullptr;
717  CurPtr += sizeof(coff_bigobj_file_header);
718  } else {
719  // It's not a bigobj.
720  COFFBigObjHeader = nullptr;
721  }
722  }
723  if (COFFHeader) {
724  // The prior checkSize call may have failed. This isn't a hard error
725  // because we were just trying to sniff out bigobj.
726  EC = std::error_code();
727  CurPtr += sizeof(coff_file_header);
728 
729  if (COFFHeader->isImportLibrary())
730  return;
731  }
732 
733  if (HasPEHeader) {
734  const pe32_header *Header;
735  if ((EC = getObject(Header, Data, base() + CurPtr)))
736  return;
737 
738  const uint8_t *DataDirAddr;
739  uint64_t DataDirSize;
740  if (Header->Magic == COFF::PE32Header::PE32) {
741  PE32Header = Header;
742  DataDirAddr = base() + CurPtr + sizeof(pe32_header);
743  DataDirSize = sizeof(data_directory) * PE32Header->NumberOfRvaAndSize;
744  } else if (Header->Magic == COFF::PE32Header::PE32_PLUS) {
745  PE32PlusHeader = reinterpret_cast<const pe32plus_header *>(Header);
746  DataDirAddr = base() + CurPtr + sizeof(pe32plus_header);
747  DataDirSize = sizeof(data_directory) * PE32PlusHeader->NumberOfRvaAndSize;
748  } else {
749  // It's neither PE32 nor PE32+.
751  return;
752  }
753  if ((EC = getObject(DataDirectory, Data, DataDirAddr, DataDirSize)))
754  return;
755  }
756 
757  if (COFFHeader)
758  CurPtr += COFFHeader->SizeOfOptionalHeader;
759 
760  if ((EC = getObject(SectionTable, Data, base() + CurPtr,
761  (uint64_t)getNumberOfSections() * sizeof(coff_section))))
762  return;
763 
764  // Initialize the pointer to the symbol table.
765  if (getPointerToSymbolTable() != 0) {
766  if ((EC = initSymbolTablePtr())) {
767  SymbolTable16 = nullptr;
768  SymbolTable32 = nullptr;
769  StringTable = nullptr;
770  StringTableSize = 0;
771  }
772  } else {
773  // We had better not have any symbols if we don't have a symbol table.
774  if (getNumberOfSymbols() != 0) {
776  return;
777  }
778  }
779 
780  // Initialize the pointer to the beginning of the import table.
781  if ((EC = initImportTablePtr()))
782  return;
783  if ((EC = initDelayImportTablePtr()))
784  return;
785 
786  // Initialize the pointer to the export table.
787  if ((EC = initExportTablePtr()))
788  return;
789 
790  // Initialize the pointer to the base relocation table.
791  if ((EC = initBaseRelocPtr()))
792  return;
793 
794  // Initialize the pointer to the export table.
795  if ((EC = initDebugDirectoryPtr()))
796  return;
797 
798  if ((EC = initLoadConfigPtr()))
799  return;
800 
801  EC = std::error_code();
802 }
803 
806  Ret.p = getSymbolTable();
807  return basic_symbol_iterator(SymbolRef(Ret, this));
808 }
809 
811  // The symbol table ends where the string table begins.
813  Ret.p = reinterpret_cast<uintptr_t>(StringTable);
814  return basic_symbol_iterator(SymbolRef(Ret, this));
815 }
816 
818  if (!ImportDirectory)
819  return import_directory_end();
820  if (ImportDirectory->isNull())
821  return import_directory_end();
823  ImportDirectoryEntryRef(ImportDirectory, 0, this));
824 }
825 
828  ImportDirectoryEntryRef(nullptr, -1, this));
829 }
830 
834  DelayImportDirectoryEntryRef(DelayImportDirectory, 0, this));
835 }
836 
841  DelayImportDirectory, NumberOfDelayImportDirectory, this));
842 }
843 
846  ExportDirectoryEntryRef(ExportDirectory, 0, this));
847 }
848 
850  if (!ExportDirectory)
851  return export_directory_iterator(ExportDirectoryEntryRef(nullptr, 0, this));
852  ExportDirectoryEntryRef Ref(ExportDirectory,
853  ExportDirectory->AddressTableEntries, this);
854  return export_directory_iterator(Ref);
855 }
856 
859  Ret.p = reinterpret_cast<uintptr_t>(SectionTable);
860  return section_iterator(SectionRef(Ret, this));
861 }
862 
865  int NumSections =
866  COFFHeader && COFFHeader->isImportLibrary() ? 0 : getNumberOfSections();
867  Ret.p = reinterpret_cast<uintptr_t>(SectionTable + NumSections);
868  return section_iterator(SectionRef(Ret, this));
869 }
870 
872  return base_reloc_iterator(BaseRelocRef(BaseRelocHeader, this));
873 }
874 
876  return base_reloc_iterator(BaseRelocRef(BaseRelocEnd, this));
877 }
878 
880  return getArch() == Triple::x86_64 || getArch() == Triple::aarch64 ? 8 : 4;
881 }
882 
884  switch(getMachine()) {
886  return "COFF-i386";
888  return "COFF-x86-64";
890  return "COFF-ARM";
892  return "COFF-ARM64";
893  default:
894  return "COFF-<unknown arch>";
895  }
896 }
897 
899  switch (getMachine()) {
901  return Triple::x86;
903  return Triple::x86_64;
905  return Triple::thumb;
907  return Triple::aarch64;
908  default:
909  return Triple::UnknownArch;
910  }
911 }
912 
914  if (PE32Header)
915  return PE32Header->AddressOfEntryPoint;
916  return 0;
917 }
918 
922 }
923 
928 }
929 
933 }
934 
937 }
938 
939 std::error_code
941  Res = COFFHeader;
942  return std::error_code();
943 }
944 
945 std::error_code
947  Res = COFFBigObjHeader;
948  return std::error_code();
949 }
950 
951 std::error_code COFFObjectFile::getPE32Header(const pe32_header *&Res) const {
952  Res = PE32Header;
953  return std::error_code();
954 }
955 
956 std::error_code
958  Res = PE32PlusHeader;
959  return std::error_code();
960 }
961 
962 std::error_code
964  const data_directory *&Res) const {
965  // Error if there's no data directory or the index is out of range.
966  if (!DataDirectory) {
967  Res = nullptr;
969  }
970  assert(PE32Header || PE32PlusHeader);
971  uint32_t NumEnt = PE32Header ? PE32Header->NumberOfRvaAndSize
972  : PE32PlusHeader->NumberOfRvaAndSize;
973  if (Index >= NumEnt) {
974  Res = nullptr;
976  }
977  Res = &DataDirectory[Index];
978  return std::error_code();
979 }
980 
981 std::error_code COFFObjectFile::getSection(int32_t Index,
982  const coff_section *&Result) const {
983  Result = nullptr;
985  return std::error_code();
986  if (static_cast<uint32_t>(Index) <= getNumberOfSections()) {
987  // We already verified the section table data, so no need to check again.
988  Result = SectionTable + (Index - 1);
989  return std::error_code();
990  }
992 }
993 
995  const coff_section *&Result) const {
996  Result = nullptr;
997  StringRef SecName;
998  for (const SectionRef &Section : sections()) {
999  if (std::error_code E = Section.getName(SecName))
1000  return E;
1001  if (SecName == SectionName) {
1002  Result = getCOFFSection(Section);
1003  return std::error_code();
1004  }
1005  }
1007 }
1008 
1009 std::error_code COFFObjectFile::getString(uint32_t Offset,
1010  StringRef &Result) const {
1011  if (StringTableSize <= 4)
1012  // Tried to get a string from an empty string table.
1014  if (Offset >= StringTableSize)
1016  Result = StringRef(StringTable + Offset);
1017  return std::error_code();
1018 }
1019 
1021  StringRef &Res) const {
1022  return getSymbolName(Symbol.getGeneric(), Res);
1023 }
1024 
1026  StringRef &Res) const {
1027  // Check for string table entry. First 4 bytes are 0.
1028  if (Symbol->Name.Offset.Zeroes == 0) {
1029  if (std::error_code EC = getString(Symbol->Name.Offset.Offset, Res))
1030  return EC;
1031  return std::error_code();
1032  }
1033 
1034  if (Symbol->Name.ShortName[COFF::NameSize - 1] == 0)
1035  // Null terminated, let ::strlen figure out the length.
1036  Res = StringRef(Symbol->Name.ShortName);
1037  else
1038  // Not null terminated, use all 8 bytes.
1039  Res = StringRef(Symbol->Name.ShortName, COFF::NameSize);
1040  return std::error_code();
1041 }
1042 
1045  const uint8_t *Aux = nullptr;
1046 
1047  size_t SymbolSize = getSymbolTableEntrySize();
1048  if (Symbol.getNumberOfAuxSymbols() > 0) {
1049  // AUX data comes immediately after the symbol in COFF
1050  Aux = reinterpret_cast<const uint8_t *>(Symbol.getRawPtr()) + SymbolSize;
1051 #ifndef NDEBUG
1052  // Verify that the Aux symbol points to a valid entry in the symbol table.
1053  uintptr_t Offset = uintptr_t(Aux) - uintptr_t(base());
1054  if (Offset < getPointerToSymbolTable() ||
1055  Offset >=
1056  getPointerToSymbolTable() + (getNumberOfSymbols() * SymbolSize))
1057  report_fatal_error("Aux Symbol data was outside of symbol table.");
1058 
1059  assert((Offset - getPointerToSymbolTable()) % SymbolSize == 0 &&
1060  "Aux Symbol data did not point to the beginning of a symbol");
1061 #endif
1062  }
1063  return makeArrayRef(Aux, Symbol.getNumberOfAuxSymbols() * SymbolSize);
1064 }
1065 
1067  uintptr_t Offset =
1068  reinterpret_cast<uintptr_t>(Symbol.getRawPtr()) - getSymbolTable();
1069  assert(Offset % getSymbolTableEntrySize() == 0 &&
1070  "Symbol did not point to the beginning of a symbol");
1071  size_t Index = Offset / getSymbolTableEntrySize();
1072  assert(Index < getNumberOfSymbols());
1073  return Index;
1074 }
1075 
1078  StringRef Name;
1079  if (Sec->Name[COFF::NameSize - 1] == 0)
1080  // Null terminated, let ::strlen figure out the length.
1081  Name = Sec->Name;
1082  else
1083  // Not null terminated, use all 8 bytes.
1084  Name = StringRef(Sec->Name, COFF::NameSize);
1085 
1086  // Check for string table entry. First byte is '/'.
1087  if (Name.startswith("/")) {
1088  uint32_t Offset;
1089  if (Name.startswith("//")) {
1090  if (decodeBase64StringEntry(Name.substr(2), Offset))
1092  "inalid section name");
1093  } else {
1094  if (Name.substr(1).getAsInteger(10, Offset))
1096  "invalid section name");
1097  }
1098  if (std::error_code EC = getString(Offset, Name))
1099  return errorCodeToError(EC);
1100  }
1101 
1102  return Name;
1103 }
1104 
1105 uint64_t COFFObjectFile::getSectionSize(const coff_section *Sec) const {
1106  // SizeOfRawData and VirtualSize change what they represent depending on
1107  // whether or not we have an executable image.
1108  //
1109  // For object files, SizeOfRawData contains the size of section's data;
1110  // VirtualSize should be zero but isn't due to buggy COFF writers.
1111  //
1112  // For executables, SizeOfRawData *must* be a multiple of FileAlignment; the
1113  // actual section size is in VirtualSize. It is possible for VirtualSize to
1114  // be greater than SizeOfRawData; the contents past that point should be
1115  // considered to be zero.
1116  if (getDOSHeader())
1117  return std::min(Sec->VirtualSize, Sec->SizeOfRawData);
1118  return Sec->SizeOfRawData;
1119 }
1120 
1122  ArrayRef<uint8_t> &Res) const {
1123  // In COFF, a virtual section won't have any in-file
1124  // content, so the file pointer to the content will be zero.
1125  if (Sec->PointerToRawData == 0)
1126  return Error::success();
1127  // The only thing that we need to verify is that the contents is contained
1128  // within the file bounds. We don't need to make sure it doesn't cover other
1129  // data, as there's nothing that says that is not allowed.
1130  uintptr_t ConStart = uintptr_t(base()) + Sec->PointerToRawData;
1132  if (checkOffset(Data, ConStart, SectionSize))
1133  return make_error<BinaryError>();
1134  Res = makeArrayRef(reinterpret_cast<const uint8_t *>(ConStart), SectionSize);
1135  return Error::success();
1136 }
1137 
1138 const coff_relocation *COFFObjectFile::toRel(DataRefImpl Rel) const {
1139  return reinterpret_cast<const coff_relocation*>(Rel.p);
1140 }
1141 
1143  Rel.p = reinterpret_cast<uintptr_t>(
1144  reinterpret_cast<const coff_relocation*>(Rel.p) + 1);
1145 }
1146 
1148  const coff_relocation *R = toRel(Rel);
1149  return R->VirtualAddress;
1150 }
1151 
1153  const coff_relocation *R = toRel(Rel);
1154  DataRefImpl Ref;
1156  return symbol_end();
1157  if (SymbolTable16)
1158  Ref.p = reinterpret_cast<uintptr_t>(SymbolTable16 + R->SymbolTableIndex);
1159  else if (SymbolTable32)
1160  Ref.p = reinterpret_cast<uintptr_t>(SymbolTable32 + R->SymbolTableIndex);
1161  else
1162  llvm_unreachable("no symbol table pointer!");
1163  return symbol_iterator(SymbolRef(Ref, this));
1164 }
1165 
1167  const coff_relocation* R = toRel(Rel);
1168  return R->Type;
1169 }
1170 
1171 const coff_section *
1173  return toSec(Section.getRawDataRefImpl());
1174 }
1175 
1177  if (SymbolTable16)
1178  return toSymb<coff_symbol16>(Ref);
1179  if (SymbolTable32)
1180  return toSymb<coff_symbol32>(Ref);
1181  llvm_unreachable("no symbol table pointer!");
1182 }
1183 
1185  return getCOFFSymbol(Symbol.getRawDataRefImpl());
1186 }
1187 
1188 const coff_relocation *
1190  return toRel(Reloc.getRawDataRefImpl());
1191 }
1192 
1195  return {getFirstReloc(Sec, Data, base()),
1196  getNumberOfRelocations(Sec, Data, base())};
1197 }
1198 
1199 #define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type) \
1200  case COFF::reloc_type: \
1201  return #reloc_type;
1202 
1204  switch (getMachine()) {
1206  switch (Type) {
1224  default:
1225  return "Unknown";
1226  }
1227  break;
1229  switch (Type) {
1247  default:
1248  return "Unknown";
1249  }
1250  break;
1252  switch (Type) {
1271  default:
1272  return "Unknown";
1273  }
1274  break;
1276  switch (Type) {
1288  default:
1289  return "Unknown";
1290  }
1291  break;
1292  default:
1293  return "Unknown";
1294  }
1295 }
1296 
1297 #undef LLVM_COFF_SWITCH_RELOC_TYPE_NAME
1298 
1300  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
1301  const coff_relocation *Reloc = toRel(Rel);
1302  StringRef Res = getRelocationTypeName(Reloc->Type);
1303  Result.append(Res.begin(), Res.end());
1304 }
1305 
1307  return !DataDirectory;
1308 }
1309 
1312  .Case("eh_fram", "eh_frame")
1313  .Default(Name);
1314 }
1315 
1318  return ImportTable == Other.ImportTable && Index == Other.Index;
1319 }
1320 
1322  ++Index;
1323  if (ImportTable[Index].isNull()) {
1324  Index = -1;
1325  ImportTable = nullptr;
1326  }
1327 }
1328 
1330  const coff_import_directory_table_entry *&Result) const {
1331  return getObject(Result, OwningObject->Data, ImportTable + Index);
1332 }
1333 
1336  uintptr_t Ptr, int Index) {
1337  if (Object->getBytesInAddress() == 4) {
1338  auto *P = reinterpret_cast<const import_lookup_table_entry32 *>(Ptr);
1339  return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1340  }
1341  auto *P = reinterpret_cast<const import_lookup_table_entry64 *>(Ptr);
1342  return imported_symbol_iterator(ImportedSymbolRef(P, Index, Object));
1343 }
1344 
1347  uintptr_t IntPtr = 0;
1348  Object->getRvaPtr(RVA, IntPtr);
1349  return makeImportedSymbolIterator(Object, IntPtr, 0);
1350 }
1351 
1354  uintptr_t IntPtr = 0;
1355  Object->getRvaPtr(RVA, IntPtr);
1356  // Forward the pointer to the last entry which is null.
1357  int Index = 0;
1358  if (Object->getBytesInAddress() == 4) {
1359  auto *Entry = reinterpret_cast<ulittle32_t *>(IntPtr);
1360  while (*Entry++)
1361  ++Index;
1362  } else {
1363  auto *Entry = reinterpret_cast<ulittle64_t *>(IntPtr);
1364  while (*Entry++)
1365  ++Index;
1366  }
1367  return makeImportedSymbolIterator(Object, IntPtr, Index);
1368 }
1369 
1372  return importedSymbolBegin(ImportTable[Index].ImportAddressTableRVA,
1373  OwningObject);
1374 }
1375 
1378  return importedSymbolEnd(ImportTable[Index].ImportAddressTableRVA,
1379  OwningObject);
1380 }
1381 
1384  return make_range(imported_symbol_begin(), imported_symbol_end());
1385 }
1386 
1388  return importedSymbolBegin(ImportTable[Index].ImportLookupTableRVA,
1389  OwningObject);
1390 }
1391 
1393  return importedSymbolEnd(ImportTable[Index].ImportLookupTableRVA,
1394  OwningObject);
1395 }
1396 
1399  return make_range(lookup_table_begin(), lookup_table_end());
1400 }
1401 
1402 std::error_code ImportDirectoryEntryRef::getName(StringRef &Result) const {
1403  uintptr_t IntPtr = 0;
1404  if (std::error_code EC =
1405  OwningObject->getRvaPtr(ImportTable[Index].NameRVA, IntPtr))
1406  return EC;
1407  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1408  return std::error_code();
1409 }
1410 
1411 std::error_code
1413  Result = ImportTable[Index].ImportLookupTableRVA;
1414  return std::error_code();
1415 }
1416 
1417 std::error_code
1419  Result = ImportTable[Index].ImportAddressTableRVA;
1420  return std::error_code();
1421 }
1422 
1425  return Table == Other.Table && Index == Other.Index;
1426 }
1427 
1429  ++Index;
1430 }
1431 
1434  return importedSymbolBegin(Table[Index].DelayImportNameTable,
1435  OwningObject);
1436 }
1437 
1440  return importedSymbolEnd(Table[Index].DelayImportNameTable,
1441  OwningObject);
1442 }
1443 
1446  return make_range(imported_symbol_begin(), imported_symbol_end());
1447 }
1448 
1449 std::error_code DelayImportDirectoryEntryRef::getName(StringRef &Result) const {
1450  uintptr_t IntPtr = 0;
1451  if (std::error_code EC = OwningObject->getRvaPtr(Table[Index].Name, IntPtr))
1452  return EC;
1453  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1454  return std::error_code();
1455 }
1456 
1457 std::error_code DelayImportDirectoryEntryRef::
1459  Result = &Table[Index];
1460  return std::error_code();
1461 }
1462 
1463 std::error_code DelayImportDirectoryEntryRef::
1464 getImportAddress(int AddrIndex, uint64_t &Result) const {
1465  uint32_t RVA = Table[Index].DelayImportAddressTable +
1466  AddrIndex * (OwningObject->is64() ? 8 : 4);
1467  uintptr_t IntPtr = 0;
1468  if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1469  return EC;
1470  if (OwningObject->is64())
1471  Result = *reinterpret_cast<const ulittle64_t *>(IntPtr);
1472  else
1473  Result = *reinterpret_cast<const ulittle32_t *>(IntPtr);
1474  return std::error_code();
1475 }
1476 
1479  return ExportTable == Other.ExportTable && Index == Other.Index;
1480 }
1481 
1483  ++Index;
1484 }
1485 
1486 // Returns the name of the current export symbol. If the symbol is exported only
1487 // by ordinal, the empty string is set as a result.
1488 std::error_code ExportDirectoryEntryRef::getDllName(StringRef &Result) const {
1489  uintptr_t IntPtr = 0;
1490  if (std::error_code EC =
1491  OwningObject->getRvaPtr(ExportTable->NameRVA, IntPtr))
1492  return EC;
1493  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1494  return std::error_code();
1495 }
1496 
1497 // Returns the starting ordinal number.
1498 std::error_code
1500  Result = ExportTable->OrdinalBase;
1501  return std::error_code();
1502 }
1503 
1504 // Returns the export ordinal of the current export symbol.
1505 std::error_code ExportDirectoryEntryRef::getOrdinal(uint32_t &Result) const {
1506  Result = ExportTable->OrdinalBase + Index;
1507  return std::error_code();
1508 }
1509 
1510 // Returns the address of the current export symbol.
1511 std::error_code ExportDirectoryEntryRef::getExportRVA(uint32_t &Result) const {
1512  uintptr_t IntPtr = 0;
1513  if (std::error_code EC =
1514  OwningObject->getRvaPtr(ExportTable->ExportAddressTableRVA, IntPtr))
1515  return EC;
1517  reinterpret_cast<const export_address_table_entry *>(IntPtr);
1518  Result = entry[Index].ExportRVA;
1519  return std::error_code();
1520 }
1521 
1522 // Returns the name of the current export symbol. If the symbol is exported only
1523 // by ordinal, the empty string is set as a result.
1524 std::error_code
1526  uintptr_t IntPtr = 0;
1527  if (std::error_code EC =
1528  OwningObject->getRvaPtr(ExportTable->OrdinalTableRVA, IntPtr))
1529  return EC;
1530  const ulittle16_t *Start = reinterpret_cast<const ulittle16_t *>(IntPtr);
1531 
1532  uint32_t NumEntries = ExportTable->NumberOfNamePointers;
1533  int Offset = 0;
1534  for (const ulittle16_t *I = Start, *E = Start + NumEntries;
1535  I < E; ++I, ++Offset) {
1536  if (*I != Index)
1537  continue;
1538  if (std::error_code EC =
1539  OwningObject->getRvaPtr(ExportTable->NamePointerRVA, IntPtr))
1540  return EC;
1541  const ulittle32_t *NamePtr = reinterpret_cast<const ulittle32_t *>(IntPtr);
1542  if (std::error_code EC = OwningObject->getRvaPtr(NamePtr[Offset], IntPtr))
1543  return EC;
1544  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1545  return std::error_code();
1546  }
1547  Result = "";
1548  return std::error_code();
1549 }
1550 
1551 std::error_code ExportDirectoryEntryRef::isForwarder(bool &Result) const {
1552  const data_directory *DataEntry;
1553  if (auto EC = OwningObject->getDataDirectory(COFF::EXPORT_TABLE, DataEntry))
1554  return EC;
1555  uint32_t RVA;
1556  if (auto EC = getExportRVA(RVA))
1557  return EC;
1558  uint32_t Begin = DataEntry->RelativeVirtualAddress;
1559  uint32_t End = DataEntry->RelativeVirtualAddress + DataEntry->Size;
1560  Result = (Begin <= RVA && RVA < End);
1561  return std::error_code();
1562 }
1563 
1564 std::error_code ExportDirectoryEntryRef::getForwardTo(StringRef &Result) const {
1565  uint32_t RVA;
1566  if (auto EC = getExportRVA(RVA))
1567  return EC;
1568  uintptr_t IntPtr = 0;
1569  if (auto EC = OwningObject->getRvaPtr(RVA, IntPtr))
1570  return EC;
1571  Result = StringRef(reinterpret_cast<const char *>(IntPtr));
1572  return std::error_code();
1573 }
1574 
1576 operator==(const ImportedSymbolRef &Other) const {
1577  return Entry32 == Other.Entry32 && Entry64 == Other.Entry64
1578  && Index == Other.Index;
1579 }
1580 
1582  ++Index;
1583 }
1584 
1585 std::error_code
1587  uint32_t RVA;
1588  if (Entry32) {
1589  // If a symbol is imported only by ordinal, it has no name.
1590  if (Entry32[Index].isOrdinal())
1591  return std::error_code();
1592  RVA = Entry32[Index].getHintNameRVA();
1593  } else {
1594  if (Entry64[Index].isOrdinal())
1595  return std::error_code();
1596  RVA = Entry64[Index].getHintNameRVA();
1597  }
1598  uintptr_t IntPtr = 0;
1599  if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1600  return EC;
1601  // +2 because the first two bytes is hint.
1602  Result = StringRef(reinterpret_cast<const char *>(IntPtr + 2));
1603  return std::error_code();
1604 }
1605 
1606 std::error_code ImportedSymbolRef::isOrdinal(bool &Result) const {
1607  if (Entry32)
1608  Result = Entry32[Index].isOrdinal();
1609  else
1610  Result = Entry64[Index].isOrdinal();
1611  return std::error_code();
1612 }
1613 
1614 std::error_code ImportedSymbolRef::getHintNameRVA(uint32_t &Result) const {
1615  if (Entry32)
1616  Result = Entry32[Index].getHintNameRVA();
1617  else
1618  Result = Entry64[Index].getHintNameRVA();
1619  return std::error_code();
1620 }
1621 
1622 std::error_code ImportedSymbolRef::getOrdinal(uint16_t &Result) const {
1623  uint32_t RVA;
1624  if (Entry32) {
1625  if (Entry32[Index].isOrdinal()) {
1626  Result = Entry32[Index].getOrdinal();
1627  return std::error_code();
1628  }
1629  RVA = Entry32[Index].getHintNameRVA();
1630  } else {
1631  if (Entry64[Index].isOrdinal()) {
1632  Result = Entry64[Index].getOrdinal();
1633  return std::error_code();
1634  }
1635  RVA = Entry64[Index].getHintNameRVA();
1636  }
1637  uintptr_t IntPtr = 0;
1638  if (std::error_code EC = OwningObject->getRvaPtr(RVA, IntPtr))
1639  return EC;
1640  Result = *reinterpret_cast<const ulittle16_t *>(IntPtr);
1641  return std::error_code();
1642 }
1643 
1646  std::error_code EC;
1647  std::unique_ptr<COFFObjectFile> Ret(new COFFObjectFile(Object, EC));
1648  if (EC)
1649  return errorCodeToError(EC);
1650  return std::move(Ret);
1651 }
1652 
1653 bool BaseRelocRef::operator==(const BaseRelocRef &Other) const {
1654  return Header == Other.Header && Index == Other.Index;
1655 }
1656 
1658  // Header->BlockSize is the size of the current block, including the
1659  // size of the header itself.
1660  uint32_t Size = sizeof(*Header) +
1661  sizeof(coff_base_reloc_block_entry) * (Index + 1);
1662  if (Size == Header->BlockSize) {
1663  // .reloc contains a list of base relocation blocks. Each block
1664  // consists of the header followed by entries. The header contains
1665  // how many entories will follow. When we reach the end of the
1666  // current block, proceed to the next block.
1667  Header = reinterpret_cast<const coff_base_reloc_block_header *>(
1668  reinterpret_cast<const uint8_t *>(Header) + Size);
1669  Index = 0;
1670  } else {
1671  ++Index;
1672  }
1673 }
1674 
1675 std::error_code BaseRelocRef::getType(uint8_t &Type) const {
1676  auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1677  Type = Entry[Index].getType();
1678  return std::error_code();
1679 }
1680 
1681 std::error_code BaseRelocRef::getRVA(uint32_t &Result) const {
1682  auto *Entry = reinterpret_cast<const coff_base_reloc_block_entry *>(Header + 1);
1683  Result = Header->PageRVA + Entry[Index].getOffset();
1684  return std::error_code();
1685 }
1686 
1687 #define RETURN_IF_ERROR(E) \
1688  if (E) \
1689  return E;
1690 
1692 ResourceSectionRef::getDirStringAtOffset(uint32_t Offset) {
1693  BinaryStreamReader Reader = BinaryStreamReader(BBS);
1694  Reader.setOffset(Offset);
1695  uint16_t Length;
1696  RETURN_IF_ERROR(Reader.readInteger(Length));
1697  ArrayRef<UTF16> RawDirString;
1698  RETURN_IF_ERROR(Reader.readArray(RawDirString, Length));
1699  return RawDirString;
1700 }
1701 
1704  return getDirStringAtOffset(Entry.Identifier.getNameOffset());
1705 }
1706 
1708 ResourceSectionRef::getTableAtOffset(uint32_t Offset) {
1709  const coff_resource_dir_table *Table = nullptr;
1710 
1711  BinaryStreamReader Reader(BBS);
1712  Reader.setOffset(Offset);
1713  RETURN_IF_ERROR(Reader.readObject(Table));
1714  assert(Table != nullptr);
1715  return *Table;
1716 }
1717 
1720  return getTableAtOffset(Entry.Offset.value());
1721 }
1722 
1724  return getTableAtOffset(0);
1725 }
bool isNull() const
Definition: COFF.h:559
uint32_t getAlignment() const
Definition: COFF.h:456
bool isCommon() const
Definition: COFF.h:378
static uint32_t getNumberOfRelocations(const coff_section *Sec, MemoryBufferRef M, const uint8_t *base)
std::error_code getHintNameRVA(uint32_t &Result) const
The 64-bit PE header that follows the COFF header.
Definition: COFF.h:140
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:224
std::error_code getImportAddress(int AddrIndex, uint64_t &Result) const
section_iterator section_begin() const override
support::ulittle16_t Machine
Definition: COFF.h:76
friend class SymbolRef
Definition: ObjectFile.h:244
std::error_code getType(uint8_t &Type) const
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:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const coff_section * getCOFFSection(const SectionRef &Section) const
Error readInteger(T &Dest)
Read an integer of the specified endianness into Dest and update the stream&#39;s offset.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
friend class SectionRef
Definition: ObjectFile.h:258
COFFObjectFile(MemoryBufferRef Object, std::error_code &EC)
uint64_t getRelocationOffset(DataRefImpl Rel) const override
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
support::ulittle32_t VirtualAddress
Definition: COFF.h:440
bool operator==(const DelayImportDirectoryEntryRef &Other) const
Definition: COFF.h:222
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
void moveSectionNext(DataRefImpl &Sec) const override
COFFSymbolRef getCOFFSymbol(const DataRefImpl &Ref) const
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:155
export_directory_iterator export_directory_end() const
Error readObject(const T *&Dest)
Get a pointer to an object of type T from the underlying stream, as if by memcpy, and store the resul...
std::error_code getSection(int32_t index, const coff_section *&Res) const
std::error_code getOrdinal(uint32_t &Result) const
base_reloc_iterator base_reloc_end() const
void moveRelocationNext(DataRefImpl &Rel) const override
support::ulittle16_t Version
Definition: COFF.h:90
This class is the base class for all object file types.
Definition: ObjectFile.h:226
support::ulittle16_t NumberOfSections
Definition: COFF.h:77
content_iterator< DelayImportDirectoryEntryRef > delay_import_directory_iterator
Definition: COFF.h:47
uint32_t getPointerToSymbolTable() const
Definition: COFF.h:855
static const coff_relocation * getFirstReloc(const coff_section *Sec, MemoryBufferRef M, const uint8_t *Base)
iterator_range< imported_symbol_iterator > imported_symbols() const
bool isSectionDefinition() const
Definition: COFF.h:414
bool isSectionCompressed(DataRefImpl Sec) const override
static std::error_code getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
const uint8_t * base() const
Definition: ObjectFile.h:232
uint8_t getNumberOfAuxSymbols() const
Definition: COFF.h:340
export_directory_iterator export_directory_begin() const
Definition: COFF.h:236
std::error_code getName(StringRef &Result) const
detail::packed_endian_specific_integral< uint16_t, little, unaligned > ulittle16_t
Definition: Endian.h:272
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
static const char BigObjMagic[]
Definition: COFF.h:38
const coff_relocation * getCOFFRelocation(const RelocationRef &Reloc) const
import_directory_iterator import_directory_begin() const
iterator_range< const debug_directory * > debug_directories() const
Definition: COFF.h:963
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
detail::packed_endian_specific_integral< uint64_t, little, unaligned > ulittle64_t
Definition: Endian.h:276
std::error_code getCOFFHeader(const coff_file_header *&Res) const
char ShortName[COFF::NameSize]
Definition: COFF.h:271
support::ulittle64_t ImageBase
Definition: COFF.h:149
friend class ExportDirectoryEntryRef
Definition: COFF.h:765
uint64_t getSectionAlignment(DataRefImpl Sec) const override
char Name[COFF::NameSize]
Definition: COFF.h:438
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
support::ulittle32_t AddressOfEntryPoint
Definition: COFF.h:111
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:203
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:81
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
The access may reference the value stored in memory.
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
iterator_range< base_reloc_iterator > base_relocs() const
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
static imported_symbol_iterator importedSymbolBegin(uint32_t RVA, const COFFObjectFile *Object)
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:578
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
bool isReservedSectionNumber(int32_t SectionNumber)
Definition: COFF.h:722
imported_symbol_iterator lookup_table_begin() const
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...
support::ulittle32_t VirtualSize
Definition: COFF.h:439
#define LLVM_COFF_SWITCH_RELOC_TYPE_NAME(reloc_type)
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:48
iterator_range< imported_symbol_iterator > imported_symbols() const
imported_symbol_iterator imported_symbol_end() const
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
int32_t getSectionNumber() const
Definition: COFF.h:319
uint32_t getSymbolFlags(DataRefImpl Symb) const override
std::error_code getDllName(StringRef &Result) const
uint64_t getSectionAddress(DataRefImpl Sec) const override
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
section_iterator_range sections() const
Definition: ObjectFile.h:315
void moveSymbolNext(DataRefImpl &Symb) const override
uint8_t getComplexType() const
Definition: COFF.h:347
support::ulittle32_t ExportRVA
Definition: COFF.h:237
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
std::error_code getRVA(uint32_t &Result) 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:105
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
std::error_code getDelayImportTable(const delay_import_directory_table_entry *&Result) const
#define P(N)
support::ulittle32_t Characteristics
Definition: COFF.h:447
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
base_reloc_iterator base_reloc_begin() const
std::error_code getName(StringRef &Result) const
support::ulittle32_t NumberOfRvaAndSize
Definition: COFF.h:169
Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const override
content_iterator< ExportDirectoryEntryRef > export_directory_iterator
Definition: COFF.h:48
size_t getBufferSize() const
Definition: MemoryBuffer.h:278
delay_import_directory_iterator delay_import_directory_end() const
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
detail::packed_endian_specific_integral< uint32_t, little, unaligned > ulittle32_t
Definition: Endian.h:274
bool operator==(const BaseRelocRef &Other) const
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:148
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
const dos_header * getDOSHeader() const
Definition: COFF.h:967
content_iterator< ImportedSymbolRef > imported_symbol_iterator
Definition: COFF.h:49
The 32-bit PE header that follows the COFF header.
Definition: COFF.h:104
std::error_code getOrdinal(uint16_t &Result) const
import_lookup_table_entry< support::little64_t > import_lookup_table_entry64
Definition: COFF.h:208
std::error_code getHintName(uint32_t Rva, uint16_t &Hint, StringRef &Name) const
bool operator==(const ImportedSymbolRef &Other) const
delay_import_directory_iterator delay_import_directory_begin() const
support::ulittle32_t RelativeVirtualAddress
Definition: COFF.h:173
content_iterator< ImportDirectoryEntryRef > import_directory_iterator
Definition: COFF.h:45
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
std::error_code isForwarder(bool &Result) const
std::error_code getSymbolName(StringRef &Result) const
imported_symbol_iterator imported_symbol_begin() const
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
const void * getRawPtr() const
Definition: COFF.h:286
support::ulittle16_t Type
Definition: COFF.h:474
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.
const coff_symbol_generic * getGeneric() const
Definition: COFF.h:290
static imported_symbol_iterator makeImportedSymbolIterator(const COFFObjectFile *Object, uintptr_t Ptr, int Index)
StringTableOffset Offset
Definition: COFF.h:272
support::ulittle32_t SizeOfRawData
Definition: COFF.h:441
bool isImportLibrary() const
Definition: COFF.h:84
uint64_t getRelocationType(DataRefImpl Rel) const override
std::error_code getPE32Header(const pe32_header *&Res) const
std::error_code getOrdinalBase(uint32_t &Result) const
StringRef getFileFormatName() const override
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:517
unsigned getSectionID(SectionRef Sec) const
support::ulittle32_t VirtualAddress
Definition: COFF.h:472
support::ulittle32_t SymbolTableIndex
Definition: COFF.h:473
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isUndefined() const
Definition: COFF.h:383
const T * data() const
Definition: ArrayRef.h:145
StringRef getRelocationTypeName(uint16_t Type) const
import_directory_iterator import_directory_end() const
uint16_t getMachine() const
Definition: COFF.h:811
Definition: COFF.h:552
friend class ImportDirectoryEntryRef
Definition: COFF.h:764
std::error_code isOrdinal(bool &Result) const
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:478
const coff_aux_weak_external * getWeakExternal() const
Definition: COFF.h:363
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
imported_symbol_iterator imported_symbol_begin() const
void setOffset(uint32_t Off)
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
support::ulittle32_t PointerToRawData
Definition: COFF.h:442
friend class RelocationRef
Definition: ObjectFile.h:283
iterator_range< import_directory_iterator > import_directories() const
static Expected< std::unique_ptr< COFFObjectFile > > createCOFFObjectFile(MemoryBufferRef Object)
Expected< const coff_resource_dir_table & > getEntrySubDir(const coff_resource_dir_entry &Entry)
uint32_t getNumberOfSections() const
Definition: COFF.h:847
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
Expected< ArrayRef< UTF16 > > getEntryNameString(const coff_resource_dir_entry &Entry)
basic_symbol_iterator symbol_begin() const override
static bool decodeBase64StringEntry(StringRef Str, uint32_t &Result)
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:696
Definition: COFF.h:710
bool isSectionData(DataRefImpl Sec) const override
uint64_t getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:50
std::error_code getForwardTo(StringRef &Result) const
union llvm::object::coff_symbol_generic::@278 Name
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
std::error_code getVaPtr(uint64_t VA, uintptr_t &Res) const
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:138
The DOS compatible header at the front of all PE/COFF executables.
Definition: COFF.h:53
std::error_code getExportRVA(uint32_t &Result) const
A range adaptor for a pair of iterators.
std::error_code getRvaPtr(uint32_t Rva, uintptr_t &Res) const
section_iterator section_end() const override
support::ulittle32_t AddressOfRawData
Definition: COFF.h:184
MemoryBufferRef Data
Definition: Binary.h:37
size_t getSymbolTableEntrySize() const
Definition: COFF.h:1025
Expected< uint64_t > getStartAddress() const override
support::ulittle32_t Size
Definition: COFF.h:174
std::error_code getDataDirectory(uint32_t index, const data_directory *&Res) const
Triple::ArchType getArch() const override
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:165
iterator begin() const
Definition: StringRef.h:101
support::ulittle32_t ImageBase
Definition: COFF.h:114
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:387
support::ulittle32_t Zeroes
Definition: COFF.h:245
StringRef mapDebugSectionName(StringRef Name) const override
Maps a debug section name to a standard DWARF section name.
ArrayRef< coff_relocation > getRelocations(const coff_section *Sec) const
bool operator==(const ExportDirectoryEntryRef &Other) const
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
bool isWeakExternal() const
Definition: COFF.h:388
Definition: COFF.h:717
bool isSectionText(DataRefImpl Sec) const override
std::error_code getCOFFBigObjHeader(const coff_bigobj_file_header *&Res) const
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:922
std::error_code getImportTableEntry(const coff_import_directory_table_entry *&Result) const
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition: ArrayRef.h:187
Expected< const coff_resource_dir_table & > getBaseTable()
detail::packed_endian_specific_integral< int16_t, little, unaligned > little16_t
Definition: Endian.h:279
support::ulittle32_t AddressTableEntries
Definition: COFF.h:229
#define I(x, y, z)
Definition: MD5.cpp:58
iterator_range< export_directory_iterator > export_directories() const
static const char PEMagic[]
Definition: COFF.h:36
uint32_t Size
Definition: Profile.cpp:46
Definition: COFF.h:210
bool isAnyUndefined() const
Definition: COFF.h:402
bool isSectionVirtual(DataRefImpl Sec) const override
static imported_symbol_iterator importedSymbolEnd(uint32_t RVA, const COFFObjectFile *Object)
ArrayRef< uint8_t > getSymbolAuxData(COFFSymbolRef Symbol) const
uint64_t getSectionSize(DataRefImpl Sec) const override
iterator_range< imported_symbol_iterator > lookup_table_symbols() const
uint8_t NumberOfAuxSymbols
Definition: COFF.h:262
support::ulittle32_t SizeOfData
Definition: COFF.h:183
bool isSectionBSS(DataRefImpl Sec) const override
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
support::ulittle32_t AddressOfNewExeHeader
Definition: COFF.h:72
union llvm::object::coff_resource_dir_entry::@279 Identifier
uintptr_t getSymbolTable() const
Definition: COFF.h:803
bool isFileRecord() const
Definition: COFF.h:406
LLVM Value Representation.
Definition: Value.h:72
content_iterator< BaseRelocRef > base_reloc_iterator
Definition: COFF.h:50
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
imported_symbol_iterator imported_symbol_end() const
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
import_lookup_table_entry< support::little32_t > import_lookup_table_entry32
Definition: COFF.h:206
std::error_code getImportAddressTableRVA(uint32_t &Result) const
support::ulittle32_t NumberOfRvaAndSize
Definition: COFF.h:136
uint32_t getValue() const
Definition: COFF.h:317
print Instructions which execute on loop entry
#define RETURN_IF_ERROR(E)
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool operator==(const ImportDirectoryEntryRef &Other) const
std::error_code getImportLookupTableRVA(uint32_t &Result) const
union llvm::object::coff_resource_dir_entry::@280 Offset
std::error_code getPE32PlusHeader(const pe32plus_header *&Res) const
uint32_t getSymbolIndex(COFFSymbolRef Symbol) const
bool isExternal() const
Definition: COFF.h:374
iterator end() const
Definition: StringRef.h:103
imported_symbol_iterator lookup_table_end() const
uint64_t getSectionIndex(DataRefImpl Sec) const override
unsigned getSymbolSectionID(SymbolRef Sym) const
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:555
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 getSymbolName(StringRef &Result) const
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1163
iterator_range< delay_import_directory_iterator > delay_import_directories() 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:658
uint32_t getNumberOfSymbols() const
Definition: COFF.h:872
support::ulittle32_t Offset
Definition: COFF.h:246
A function that returns a base type.
Definition: COFF.h:261