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: COFFImportFile(MemoryBufferRef Source)31 COFFImportFile(MemoryBufferRef Source) 32 : SymbolicFile(ID_COFFImportFile, Source) {} 33 classof(Binary const * V)34 static bool classof(Binary const *V) { return V->isCOFFImportFile(); } 35 moveSymbolNext(DataRefImpl & Symb)36 void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; } 37 printSymbolName(raw_ostream & OS,DataRefImpl Symb)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 getSymbolFlags(DataRefImpl Symb)45 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override { 46 return SymbolRef::SF_Global; 47 } 48 symbol_begin()49 basic_symbol_iterator symbol_begin() const override { 50 return BasicSymbolRef(DataRefImpl(), this); 51 } 52 symbol_end()53 basic_symbol_iterator symbol_end() const override { 54 DataRefImpl Symb; 55 Symb.p = isData() ? 1 : 2; 56 return BasicSymbolRef(Symb, this); 57 } 58 is64Bit()59 bool is64Bit() const override { return false; } 60 getCOFFImportHeader()61 const coff_import_header *getCOFFImportHeader() const { 62 return reinterpret_cast<const object::coff_import_header *>( 63 Data.getBufferStart()); 64 } 65 getMachine()66 uint16_t getMachine() const { return getCOFFImportHeader()->Machine; } 67 68 private: isData()69 bool isData() const { 70 return getCOFFImportHeader()->getType() == COFF::IMPORT_DATA; 71 } 72 }; 73 74 struct COFFShortExport { 75 /// The name of the export as specified in the .def file or on the command 76 /// line, i.e. "foo" in "/EXPORT:foo", and "bar" in "/EXPORT:foo=bar". This 77 /// may lack mangling, such as underscore prefixing and stdcall suffixing. 78 std::string Name; 79 80 /// The external, exported name. Only non-empty when export renaming is in 81 /// effect, i.e. "foo" in "/EXPORT:foo=bar". 82 std::string ExtName; 83 84 /// The real, mangled symbol name from the object file. Given 85 /// "/export:foo=bar", this could be "_bar@8" if bar is stdcall. 86 std::string SymbolName; 87 88 /// Creates a weak alias. This is the name of the weak aliasee. In a .def 89 /// file, this is "baz" in "EXPORTS\nfoo = bar == baz". 90 std::string AliasTarget; 91 92 uint16_t Ordinal = 0; 93 bool Noname = false; 94 bool Data = false; 95 bool Private = false; 96 bool Constant = false; 97 98 friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) { 99 return L.Name == R.Name && L.ExtName == R.ExtName && 100 L.Ordinal == R.Ordinal && L.Noname == R.Noname && 101 L.Data == R.Data && L.Private == R.Private; 102 } 103 104 friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) { 105 return !(L == R); 106 } 107 }; 108 109 Error writeImportLibrary(StringRef ImportName, StringRef Path, 110 ArrayRef<COFFShortExport> Exports, 111 COFF::MachineTypes Machine, bool MinGW); 112 113 } // namespace object 114 } // namespace llvm 115 116 #endif 117