1 //===- COFFImportFile.h - COFF short import file implementation -*- C++ -*-===//
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 // COFF short import file is a special kind of file which contains
10 // only symbol names for DLL-exported symbols. This class implements
11 // exporting of Symbols to create libraries and a SymbolicFile
12 // interface for the file type.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_OBJECT_COFFIMPORTFILE_H
17 #define LLVM_OBJECT_COFFIMPORTFILE_H
18 
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Object/SymbolicFile.h"
23 #include "llvm/Support/MemoryBufferRef.h"
24 #include "llvm/Support/raw_ostream.h"
25 
26 namespace llvm {
27 namespace object {
28 
29 class COFFImportFile : public SymbolicFile {
30 public:
31   COFFImportFile(MemoryBufferRef Source)
32       : SymbolicFile(ID_COFFImportFile, Source) {}
33 
34   static bool classof(Binary const *V) { return V->isCOFFImportFile(); }
35 
36   void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; }
37 
38   Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override {
39     if (Symb.p == 0)
40       OS << "__imp_";
41     OS << StringRef(Data.getBufferStart() + sizeof(coff_import_header));
42     return Error::success();
43   }
44 
45   Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override {
46     return SymbolRef::SF_Global;
47   }
48 
49   basic_symbol_iterator symbol_begin() const override {
50     return BasicSymbolRef(DataRefImpl(), this);
51   }
52 
53   basic_symbol_iterator symbol_end() const override {
54     DataRefImpl Symb;
55     Symb.p = isData() ? 1 : 2;
56     return BasicSymbolRef(Symb, this);
57   }
58 
59   const coff_import_header *getCOFFImportHeader() const {
60     return reinterpret_cast<const object::coff_import_header *>(
61         Data.getBufferStart());
62   }
63 
64 private:
65   bool isData() const {
66     return getCOFFImportHeader()->getType() == COFF::IMPORT_DATA;
67   }
68 };
69 
70 struct COFFShortExport {
71   /// The name of the export as specified in the .def file or on the command
72   /// line, i.e. "foo" in "/EXPORT:foo", and "bar" in "/EXPORT:foo=bar". This
73   /// may lack mangling, such as underscore prefixing and stdcall suffixing.
74   std::string Name;
75 
76   /// The external, exported name. Only non-empty when export renaming is in
77   /// effect, i.e. "foo" in "/EXPORT:foo=bar".
78   std::string ExtName;
79 
80   /// The real, mangled symbol name from the object file. Given
81   /// "/export:foo=bar", this could be "_bar@8" if bar is stdcall.
82   std::string SymbolName;
83 
84   /// Creates a weak alias. This is the name of the weak aliasee. In a .def
85   /// file, this is "baz" in "EXPORTS\nfoo = bar == baz".
86   std::string AliasTarget;
87 
88   uint16_t Ordinal = 0;
89   bool Noname = false;
90   bool Data = false;
91   bool Private = false;
92   bool Constant = false;
93 
94   friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) {
95     return L.Name == R.Name && L.ExtName == R.ExtName &&
96             L.Ordinal == R.Ordinal && L.Noname == R.Noname &&
97             L.Data == R.Data && L.Private == R.Private;
98   }
99 
100   friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) {
101     return !(L == R);
102   }
103 };
104 
105 Error writeImportLibrary(StringRef ImportName, StringRef Path,
106                          ArrayRef<COFFShortExport> Exports,
107                          COFF::MachineTypes Machine, bool MinGW);
108 
109 } // namespace object
110 } // namespace llvm
111 
112 #endif
113