• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constexpr std::string_view ImportDescriptorPrefix = "__IMPORT_DESCRIPTOR_";
30 constexpr std::string_view NullImportDescriptorSymbolName =
31     "__NULL_IMPORT_DESCRIPTOR";
32 constexpr std::string_view NullThunkDataPrefix = "\x7f";
33 constexpr std::string_view NullThunkDataSuffix = "_NULL_THUNK_DATA";
34 
35 class COFFImportFile : public SymbolicFile {
36 private:
37   enum SymbolIndex { ImpSymbol, ThunkSymbol, ECAuxSymbol, ECThunkSymbol };
38 
39 public:
COFFImportFile(MemoryBufferRef Source)40   COFFImportFile(MemoryBufferRef Source)
41       : SymbolicFile(ID_COFFImportFile, Source) {}
42 
classof(Binary const * V)43   static bool classof(Binary const *V) { return V->isCOFFImportFile(); }
44 
moveSymbolNext(DataRefImpl & Symb)45   void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; }
46 
printSymbolName(raw_ostream & OS,DataRefImpl Symb)47   Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override {
48     switch (Symb.p) {
49     case ImpSymbol:
50       OS << "__imp_";
51       break;
52     case ECAuxSymbol:
53       OS << "__imp_aux_";
54       break;
55     }
56     const char *Name = Data.getBufferStart() + sizeof(coff_import_header);
57     if (Symb.p != ECThunkSymbol && COFF::isArm64EC(getMachine())) {
58       if (std::optional<std::string> DemangledName =
59               getArm64ECDemangledFunctionName(Name)) {
60         OS << StringRef(*DemangledName);
61         return Error::success();
62       }
63     }
64     OS << StringRef(Name);
65     return Error::success();
66   }
67 
getSymbolFlags(DataRefImpl Symb)68   Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override {
69     return SymbolRef::SF_Global;
70   }
71 
symbol_begin()72   basic_symbol_iterator symbol_begin() const override {
73     return BasicSymbolRef(DataRefImpl(), this);
74   }
75 
symbol_end()76   basic_symbol_iterator symbol_end() const override {
77     DataRefImpl Symb;
78     if (isData())
79       Symb.p = ImpSymbol + 1;
80     else if (COFF::isArm64EC(getMachine()))
81       Symb.p = ECThunkSymbol + 1;
82     else
83       Symb.p = ThunkSymbol + 1;
84     return BasicSymbolRef(Symb, this);
85   }
86 
is64Bit()87   bool is64Bit() const override { return false; }
88 
getCOFFImportHeader()89   const coff_import_header *getCOFFImportHeader() const {
90     return reinterpret_cast<const object::coff_import_header *>(
91         Data.getBufferStart());
92   }
93 
getMachine()94   uint16_t getMachine() const { return getCOFFImportHeader()->Machine; }
95 
96   StringRef getFileFormatName() const;
97   StringRef getExportName() const;
98 
99 private:
isData()100   bool isData() const {
101     return getCOFFImportHeader()->getType() == COFF::IMPORT_DATA;
102   }
103 };
104 
105 struct COFFShortExport {
106   /// The name of the export as specified in the .def file or on the command
107   /// line, i.e. "foo" in "/EXPORT:foo", and "bar" in "/EXPORT:foo=bar". This
108   /// may lack mangling, such as underscore prefixing and stdcall suffixing.
109   std::string Name;
110 
111   /// The external, exported name. Only non-empty when export renaming is in
112   /// effect, i.e. "foo" in "/EXPORT:foo=bar".
113   std::string ExtName;
114 
115   /// The real, mangled symbol name from the object file. Given
116   /// "/export:foo=bar", this could be "_bar@8" if bar is stdcall.
117   std::string SymbolName;
118 
119   /// Creates a weak alias. This is the name of the weak aliasee. In a .def
120   /// file, this is "baz" in "EXPORTS\nfoo = bar == baz".
121   std::string AliasTarget;
122 
123   /// Specifies EXPORTAS name. In a .def file, this is "bar" in
124   /// "EXPORTS\nfoo EXPORTAS bar".
125   std::string ExportAs;
126 
127   uint16_t Ordinal = 0;
128   bool Noname = false;
129   bool Data = false;
130   bool Private = false;
131   bool Constant = false;
132 
133   friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) {
134     return L.Name == R.Name && L.ExtName == R.ExtName &&
135             L.Ordinal == R.Ordinal && L.Noname == R.Noname &&
136             L.Data == R.Data && L.Private == R.Private;
137   }
138 
139   friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) {
140     return !(L == R);
141   }
142 };
143 
144 /// Writes a COFF import library containing entries described by the Exports
145 /// array.
146 ///
147 /// For hybrid targets such as ARM64EC, additional native entry points can be
148 /// exposed using the NativeExports parameter. When NativeExports is used, the
149 /// output import library will expose these native ARM64 imports alongside the
150 /// entries described in the Exports array. Such a library can be used for
151 /// linking both ARM64EC and pure ARM64 objects, and the linker will pick only
152 /// the exports relevant to the target platform. For non-hybrid targets,
153 /// the NativeExports parameter should not be used.
154 Error writeImportLibrary(
155     StringRef ImportName, StringRef Path, ArrayRef<COFFShortExport> Exports,
156     COFF::MachineTypes Machine, bool MinGW,
157     ArrayRef<COFFShortExport> NativeExports = std::nullopt);
158 
159 } // namespace object
160 } // namespace llvm
161 
162 #endif
163