LLVM 19.0.0git
COFFImportFile.h
Go to the documentation of this file.
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/IR/Mangler.h"
21#include "llvm/Object/COFF.h"
26
27namespace llvm {
28namespace object {
29
30constexpr std::string_view ImportDescriptorPrefix = "__IMPORT_DESCRIPTOR_";
31constexpr std::string_view NullImportDescriptorSymbolName =
32 "__NULL_IMPORT_DESCRIPTOR";
33constexpr std::string_view NullThunkDataPrefix = "\x7f";
34constexpr std::string_view NullThunkDataSuffix = "_NULL_THUNK_DATA";
35
37private:
38 enum SymbolIndex { ImpSymbol, ThunkSymbol, ECAuxSymbol, ECThunkSymbol };
39
40public:
43
44 static bool classof(Binary const *V) { return V->isCOFFImportFile(); }
45
46 void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; }
47
48 Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override;
49
52 }
53
55 return BasicSymbolRef(DataRefImpl(), this);
56 }
57
59 DataRefImpl Symb;
60 if (isData())
61 Symb.p = ImpSymbol + 1;
62 else if (COFF::isArm64EC(getMachine()))
63 Symb.p = ECThunkSymbol + 1;
64 else
65 Symb.p = ThunkSymbol + 1;
66 return BasicSymbolRef(Symb, this);
67 }
68
69 bool is64Bit() const override { return false; }
70
72 return reinterpret_cast<const object::coff_import_header *>(
74 }
75
77
80
81private:
82 bool isData() const {
84 }
85};
86
88 /// The name of the export as specified in the .def file or on the command
89 /// line, i.e. "foo" in "/EXPORT:foo", and "bar" in "/EXPORT:foo=bar". This
90 /// may lack mangling, such as underscore prefixing and stdcall suffixing.
91 std::string Name;
92
93 /// The external, exported name. Only non-empty when export renaming is in
94 /// effect, i.e. "foo" in "/EXPORT:foo=bar".
95 std::string ExtName;
96
97 /// The real, mangled symbol name from the object file. Given
98 /// "/export:foo=bar", this could be "_bar@8" if bar is stdcall.
99 std::string SymbolName;
100
101 /// Creates a weak alias. This is the name of the weak aliasee. In a .def
102 /// file, this is "baz" in "EXPORTS\nfoo = bar == baz".
103 std::string AliasTarget;
104
105 /// Specifies EXPORTAS name. In a .def file, this is "bar" in
106 /// "EXPORTS\nfoo EXPORTAS bar".
107 std::string ExportAs;
108
110 bool Noname = false;
111 bool Data = false;
112 bool Private = false;
113 bool Constant = false;
114
115 friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) {
116 return L.Name == R.Name && L.ExtName == R.ExtName &&
117 L.Ordinal == R.Ordinal && L.Noname == R.Noname &&
118 L.Data == R.Data && L.Private == R.Private;
119 }
120
121 friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) {
122 return !(L == R);
123 }
124};
125
126/// Writes a COFF import library containing entries described by the Exports
127/// array.
128///
129/// For hybrid targets such as ARM64EC, additional native entry points can be
130/// exposed using the NativeExports parameter. When NativeExports is used, the
131/// output import library will expose these native ARM64 imports alongside the
132/// entries described in the Exports array. Such a library can be used for
133/// linking both ARM64EC and pure ARM64 objects, and the linker will pick only
134/// the exports relevant to the target platform. For non-hybrid targets,
135/// the NativeExports parameter should not be used.
137 StringRef ImportName, StringRef Path, ArrayRef<COFFShortExport> Exports,
138 COFF::MachineTypes Machine, bool MinGW,
139 ArrayRef<COFFShortExport> NativeExports = std::nullopt);
140
141} // namespace object
142} // namespace llvm
143
144#endif
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:371
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This is an important base class in LLVM.
Definition: Constant.h:41
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
Tagged union holding either a T or a Error.
Definition: Error.h:474
const char * getBufferStart() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: SymbolicFile.h:103
MemoryBufferRef Data
Definition: Binary.h:37
const coff_import_header * getCOFFImportHeader() const
Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const override
bool is64Bit() const override
void moveSymbolNext(DataRefImpl &Symb) const override
StringRef getFileFormatName() const
COFFImportFile(MemoryBufferRef Source)
basic_symbol_iterator symbol_begin() const override
StringRef getExportName() const
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
basic_symbol_iterator symbol_end() const override
static bool classof(Binary const *V)
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
MachineTypes
Definition: COFF.h:92
@ IMPORT_DATA
Definition: COFF.h:702
bool isArm64EC(T Machine)
Definition: COFF.h:124
constexpr std::string_view NullImportDescriptorSymbolName
Error writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef< COFFShortExport > Exports, COFF::MachineTypes Machine, bool MinGW, ArrayRef< COFFShortExport > NativeExports=std::nullopt)
Writes a COFF import library containing entries described by the Exports array.
constexpr std::string_view NullThunkDataPrefix
constexpr std::string_view NullThunkDataSuffix
constexpr std::string_view ImportDescriptorPrefix
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::string Name
The name of the export as specified in the .def file or on the command line, i.e.
std::string SymbolName
The real, mangled symbol name from the object file.
std::string ExportAs
Specifies EXPORTAS name.
friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R)
std::string AliasTarget
Creates a weak alias.
std::string ExtName
The external, exported name.
friend bool operator==(const COFFShortExport &L, const COFFShortExport &R)
support::ulittle16_t Machine
Definition: COFF.h:545