35#define OPTTABLE_STR_TABLE_CODE
37#undef OPTTABLE_STR_TABLE_CODE
41#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
46#define OPTTABLE_PREFIXES_TABLE_CODE
48#undef OPTTABLE_PREFIXES_TABLE_CODE
52#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
68 if (std::error_code EC = MB.
getError()) {
73 return std::move(*MB);
88 switch (
T.getArch()) {
109std::optional<std::string> getPrefix(
StringRef Argv0) {
114 ProgName = ProgName.
rtrim(
"0123456789.-");
119 return ProgName.
str();
124 std::vector<COFFShortExport> &Exports,
125 std::string &OutputFile) {
126 std::unique_ptr<MemoryBuffer> MB =
openFile(DefFileName);
130 if (!MB->getBufferSize()) {
136 *MB,
Machine,
true, AddUnderscores);
143 if (OutputFile.empty())
144 OutputFile = std::move(
Def->OutputFile);
158 Exports = std::move(
Def->Exports);
171template <
typename Callable>
187 return printError(Obj.takeError(),
Name);
188 if (!Callback(*Obj->get(),
Name))
193 return printError(std::move(Err),
Name);
221 std::vector<StringRef> &Names,
bool IsMsStyleImplib) {
222 StringRef TargetName = IsMsStyleImplib ?
".idata$6" :
".idata$7";
223 for (
const auto &S : Obj.
sections()) {
226 printError(NameOrErr.
takeError(), ObjName);
230 if (
Name != TargetName)
235 if (!IsMsStyleImplib && !S.relocations().
empty())
239 if (!ContentsOrErr) {
240 printError(ContentsOrErr.
takeError(), ObjName);
244 Contents = Contents.
substr(0, Contents.
find(
'\0'));
245 if (Contents.
empty())
247 Names.push_back(Contents);
253int doIdentify(
StringRef File,
bool IdentifyStrict) {
263 std::unique_ptr<MemoryBuffer>
B = std::move(MaybeBuf.
get());
267 return printError(std::move(Err),
B->getBufferIdentifier());
269 bool IsMsStyleImplib =
false;
271 if (S.getName() ==
"__NULL_IMPORT_DESCRIPTOR") {
272 IsMsStyleImplib =
true;
276 std::vector<StringRef> Names;
277 if (forEachCoff(
Archive,
B->getBufferIdentifier(),
279 return identifyImportName(Obj, ObjName, Names,
288 if (Names.size() > 1 && IdentifyStrict) {
289 llvm::errs() <<
File <<
"contains imports for two or more DLLs\n";
303 unsigned MissingIndex;
304 unsigned MissingCount;
306 Table.ParseArgs(ArgsArr.
slice(1), MissingIndex, MissingCount);
308 llvm::errs() << Args.getArgString(MissingIndex) <<
": missing argument\n";
313 if (Args.hasArgNoClaim(OPT_INPUT) ||
314 (!Args.hasArgNoClaim(OPT_d) && !Args.hasArgNoClaim(OPT_l) &&
315 !Args.hasArgNoClaim(OPT_I))) {
316 Table.printHelp(
outs(),
"llvm-dlltool [options] file...",
"llvm-dlltool",
319 <<
"\nTARGETS: i386, i386:x86-64, arm, arm64, arm64ec, r4000\n";
323 for (
auto *
Arg : Args.filtered(OPT_UNKNOWN))
327 if (Args.hasArg(OPT_I)) {
328 return doIdentify(Args.getLastArg(OPT_I)->getValue(),
329 Args.hasArg(OPT_identify_strict));
332 if (!Args.hasArg(OPT_d)) {
333 llvm::errs() <<
"no definition file specified\n";
338 if (std::optional<std::string> Prefix = getPrefix(ArgsArr[0])) {
343 if (
auto *
Arg = Args.getLastArg(OPT_m))
351 bool AddUnderscores = !Args.hasArg(OPT_no_leading_underscore);
353 std::string OutputFile;
354 if (
auto *
Arg = Args.getLastArg(OPT_D))
357 std::vector<COFFShortExport> Exports, NativeExports;
359 if (Args.hasArg(OPT_N)) {
361 llvm::errs() <<
"native .def file is supported only on arm64ec target\n";
364 if (!parseModuleDefinition(Args.getLastArg(OPT_N)->getValue(),
366 NativeExports, OutputFile))
370 if (!parseModuleDefinition(Args.getLastArg(OPT_d)->getValue(),
Machine,
371 AddUnderscores, Exports, OutputFile))
374 if (OutputFile.empty()) {
381 if (!E.ImportName.empty() || (!E.Name.empty() && E.Name[0] ==
'?'))
383 E.SymbolName = E.Name;
389 E.Name = E.Name.substr(0, E.Name.find(
'@', 1));
396 std::string Path = std::string(Args.getLastArgValue(OPT_l));
399 true, NativeExports)) {
Defines the llvm::Arg class for parsed arguments.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
COFF::MachineTypes Machine
std::unique_ptr< MemoryBuffer > openFile(const Twine &Path)
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Base class for error info classes.
virtual std::string message() const
Return the error message as a string.
Represents either an error or a value T.
std::error_code getError() const
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
Error takeError()
Take ownership of the stored error.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr bool empty() const
empty - Check if the string is empty.
StringRef rtrim(char Char) const
Return string with consecutive Char characters starting from the right removed.
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
bool consume_back_insensitive(StringRef Suffix)
Returns true if this StringRef has the given suffix, ignoring case, and removes that suffix.
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
Triple - Helper class for working with autoconf configuration names.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
iterator_range< symbol_iterator > symbols() const
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
static Expected< std::unique_ptr< COFFObjectFile > > create(MemoryBufferRef Object)
section_iterator_range sections() const
A concrete instance of a particular driver option.
LLVM_ABI std::string getAsString(const ArgList &Args) const
Return a formatted version of the argument and its values, for diagnostics.
const char * getValue(unsigned N=0) const
Specialization of OptTable.
@ IMAGE_FILE_MACHINE_ARM64
@ IMAGE_FILE_MACHINE_UNKNOWN
@ IMAGE_FILE_MACHINE_AMD64
@ IMAGE_FILE_MACHINE_ARM64EC
@ IMAGE_FILE_MACHINE_R4000
@ IMAGE_FILE_MACHINE_I386
@ IMAGE_FILE_MACHINE_ARMNT
bool isArm64EC(T Machine)
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI Expected< COFFModuleDefinition > parseCOFFModuleDefinition(MemoryBufferRef MB, COFF::MachineTypes Machine, bool MingwDef=false, bool AddUnderscores=true)
LLVM_ABI Error writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef< COFFShortExport > Exports, COFF::MachineTypes Machine, bool MinGW, ArrayRef< COFFShortExport > NativeExports={})
Writes a COFF import library containing entries described by the Exports array.
NodeAddr< DefNode * > Def
LLVM_ABI StringRef stem(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get stem.
LLVM_ABI std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
LLVM_ABI int dlltoolDriverMain(ArrayRef< const char * > ArgsArr)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
@ archive
ar style archive file
@ coff_object
COFF object file.
std::string Name
The name of the export as specified in the .def file or on the command line, i.e.
std::string ExtName
The external, exported name.
Entry for a single option instance in the option data table.