File indexing completed on 2026-05-10 08:44:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef LLVM_OBJECT_COFFIMPORTFILE_H
0017 #define LLVM_OBJECT_COFFIMPORTFILE_H
0018
0019 #include "llvm/ADT/ArrayRef.h"
0020 #include "llvm/IR/Mangler.h"
0021 #include "llvm/Object/COFF.h"
0022 #include "llvm/Object/ObjectFile.h"
0023 #include "llvm/Object/SymbolicFile.h"
0024 #include "llvm/Support/MemoryBufferRef.h"
0025 #include "llvm/Support/raw_ostream.h"
0026
0027 namespace llvm {
0028 namespace object {
0029
0030 constexpr std::string_view ImportDescriptorPrefix = "__IMPORT_DESCRIPTOR_";
0031 constexpr std::string_view NullImportDescriptorSymbolName =
0032 "__NULL_IMPORT_DESCRIPTOR";
0033 constexpr std::string_view NullThunkDataPrefix = "\x7f";
0034 constexpr std::string_view NullThunkDataSuffix = "_NULL_THUNK_DATA";
0035
0036 class COFFImportFile : public SymbolicFile {
0037 private:
0038 enum SymbolIndex { ImpSymbol, ThunkSymbol, ECAuxSymbol, ECThunkSymbol };
0039
0040 public:
0041 COFFImportFile(MemoryBufferRef Source)
0042 : SymbolicFile(ID_COFFImportFile, Source) {}
0043
0044 static bool classof(Binary const *V) { return V->isCOFFImportFile(); }
0045
0046 void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; }
0047
0048 Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override;
0049
0050 Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override {
0051 return SymbolRef::SF_Global;
0052 }
0053
0054 basic_symbol_iterator symbol_begin() const override {
0055 return BasicSymbolRef(DataRefImpl(), this);
0056 }
0057
0058 basic_symbol_iterator symbol_end() const override {
0059 DataRefImpl Symb;
0060 if (isData())
0061 Symb.p = ImpSymbol + 1;
0062 else if (COFF::isArm64EC(getMachine()))
0063 Symb.p = ECThunkSymbol + 1;
0064 else
0065 Symb.p = ThunkSymbol + 1;
0066 return BasicSymbolRef(Symb, this);
0067 }
0068
0069 bool is64Bit() const override { return false; }
0070
0071 const coff_import_header *getCOFFImportHeader() const {
0072 return reinterpret_cast<const object::coff_import_header *>(
0073 Data.getBufferStart());
0074 }
0075
0076 uint16_t getMachine() const { return getCOFFImportHeader()->Machine; }
0077
0078 StringRef getFileFormatName() const;
0079 StringRef getExportName() const;
0080
0081 private:
0082 bool isData() const {
0083 return getCOFFImportHeader()->getType() == COFF::IMPORT_DATA;
0084 }
0085 };
0086
0087 struct COFFShortExport {
0088
0089
0090
0091 std::string Name;
0092
0093
0094
0095 std::string ExtName;
0096
0097
0098
0099 std::string SymbolName;
0100
0101
0102
0103
0104
0105 std::string ImportName;
0106
0107
0108
0109 std::string ExportAs;
0110
0111 uint16_t Ordinal = 0;
0112 bool Noname = false;
0113 bool Data = false;
0114 bool Private = false;
0115 bool Constant = false;
0116
0117 friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) {
0118 return L.Name == R.Name && L.ExtName == R.ExtName &&
0119 L.Ordinal == R.Ordinal && L.Noname == R.Noname &&
0120 L.Data == R.Data && L.Private == R.Private;
0121 }
0122
0123 friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) {
0124 return !(L == R);
0125 }
0126 };
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138 Error writeImportLibrary(StringRef ImportName, StringRef Path,
0139 ArrayRef<COFFShortExport> Exports,
0140 COFF::MachineTypes Machine, bool MinGW,
0141 ArrayRef<COFFShortExport> NativeExports = {});
0142
0143 }
0144 }
0145
0146 #endif