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 bool is64Bit() const override { return false; } 60 61 const coff_import_header *getCOFFImportHeader() const { 62 return reinterpret_cast<const object::coff_import_header *>( 63 Data.getBufferStart()); 64 } 65 66 private: 67 bool isData() const { 68 return getCOFFImportHeader()->getType() == COFF::IMPORT_DATA; 69 } 70 }; 71 72 struct COFFShortExport { 73 /// The name of the export as specified in the .def file or on the command 74 /// line, i.e. "foo" in "/EXPORT:foo", and "bar" in "/EXPORT:foo=bar". This 75 /// may lack mangling, such as underscore prefixing and stdcall suffixing. 76 std::string Name; 77 78 /// The external, exported name. Only non-empty when export renaming is in 79 /// effect, i.e. "foo" in "/EXPORT:foo=bar". 80 std::string ExtName; 81 82 /// The real, mangled symbol name from the object file. Given 83 /// "/export:foo=bar", this could be "_bar@8" if bar is stdcall. 84 std::string SymbolName; 85 86 /// Creates a weak alias. This is the name of the weak aliasee. In a .def 87 /// file, this is "baz" in "EXPORTS\nfoo = bar == baz". 88 std::string AliasTarget; 89 90 uint16_t Ordinal = 0; 91 bool Noname = false; 92 bool Data = false; 93 bool Private = false; 94 bool Constant = false; 95 96 friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) { 97 return L.Name == R.Name && L.ExtName == R.ExtName && 98 L.Ordinal == R.Ordinal && L.Noname == R.Noname && 99 L.Data == R.Data && L.Private == R.Private; 100 } 101 102 friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) { 103 return !(L == R); 104 } 105 }; 106 107 Error writeImportLibrary(StringRef ImportName, StringRef Path, 108 ArrayRef<COFFShortExport> Exports, 109 COFF::MachineTypes Machine, bool MinGW); 110 111 } // namespace object 112 } // namespace llvm 113 114 #endif 115