38264c604f03c94ef6e23c7c93586773a2d14387
[oota-llvm.git] / include / llvm / Object / COFFImportFile.h
1 //===- COFFImportFile.h - COFF short import file implementation -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // COFF short import file is a special kind of file which contains
11 // only symbol names for DLL-exported symbols. This class implements
12 // SymbolicFile interface for the file.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_OBJECT_COFF_IMPORT_FILE_H
17 #define LLVM_OBJECT_COFF_IMPORT_FILE_H
18
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Object/IRObjectFile.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Object/SymbolicFile.h"
23 #include "llvm/Support/MemoryBuffer.h"
24
25 namespace llvm {
26 namespace object {
27
28 class COFFImportFile : public SymbolicFile {
29 public:
30   COFFImportFile(MemoryBufferRef Source)
31       : SymbolicFile(ID_COFFImportFile, Source) {}
32
33   static inline bool classof(Binary const *V) { return V->isCOFFImportFile(); }
34
35   void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; }
36
37   std::error_code printSymbolName(raw_ostream &OS,
38                                   DataRefImpl Symb) const override {
39     if (Symb.p == 1)
40       OS << "__imp_";
41     OS << StringRef(Data.getBufferStart() + sizeof(coff_import_header));
42     return std::error_code();
43   }
44
45   uint32_t getSymbolFlags(DataRefImpl Symb) const override {
46     return SymbolRef::SF_Global;
47   }
48
49   basic_symbol_iterator symbol_begin_impl() const override {
50     return BasicSymbolRef(DataRefImpl(), this);
51   }
52
53   basic_symbol_iterator symbol_end_impl() const override {
54     DataRefImpl Symb;
55     Symb.p = isCode() ? 2 : 1;
56     return BasicSymbolRef(Symb, this);
57   }
58
59 private:
60   bool isCode() const {
61     auto *Import = reinterpret_cast<const object::coff_import_header *>(
62         Data.getBufferStart());
63     return Import->getType() == COFF::IMPORT_CODE;
64   }
65 };
66
67 } // namespace object
68 } // namespace llvm
69
70 #endif