blob: f894211b6447f41e4c7541d5ee1ed2075a5c4f9f [file] [log] [blame]
Martell Malone1079ef82017-07-18 21:26:38 +00001//===- DlltoolDriver.cpp - dlltool.exe-compatible driver ------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Martell Malone1079ef82017-07-18 21:26:38 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Defines an interface to a dlltool.exe-compatible driver.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ToolDrivers/llvm-dlltool/DlltoolDriver.h"
Martell Malone1079ef82017-07-18 21:26:38 +000014#include "llvm/Object/COFF.h"
15#include "llvm/Object/COFFImportFile.h"
16#include "llvm/Object/COFFModuleDefinition.h"
17#include "llvm/Option/Arg.h"
18#include "llvm/Option/ArgList.h"
19#include "llvm/Option/Option.h"
20#include "llvm/Support/Path.h"
21
Martell Malone1079ef82017-07-18 21:26:38 +000022#include <vector>
23
24using namespace llvm;
25using namespace llvm::object;
26using namespace llvm::COFF;
27
28namespace {
29
30enum {
31 OPT_INVALID = 0,
32#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
33#include "Options.inc"
34#undef OPTION
35};
36
37#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
38#include "Options.inc"
39#undef PREFIX
40
Martell Malonecc82cdf2017-08-23 02:10:28 +000041static const llvm::opt::OptTable::Info InfoTable[] = {
Martell Malone1079ef82017-07-18 21:26:38 +000042#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
43 {X1, X2, X10, X11, OPT_##ID, llvm::opt::Option::KIND##Class, \
44 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
45#include "Options.inc"
46#undef OPTION
47};
48
49class DllOptTable : public llvm::opt::OptTable {
50public:
Martell Malonecc82cdf2017-08-23 02:10:28 +000051 DllOptTable() : OptTable(InfoTable, false) {}
Martell Malone1079ef82017-07-18 21:26:38 +000052};
53
54} // namespace
55
Martell Malone1079ef82017-07-18 21:26:38 +000056// Opens a file. Path has to be resolved already.
Benjamin Kramerdf8c2622017-08-20 13:03:32 +000057static std::unique_ptr<MemoryBuffer> openFile(const Twine &Path) {
Martell Malone1079ef82017-07-18 21:26:38 +000058 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MB = MemoryBuffer::getFile(Path);
59
Martin Storsjo9d8ecb42017-08-17 05:58:27 +000060 if (std::error_code EC = MB.getError()) {
Martin Storsjocaff3262017-08-17 06:26:42 +000061 llvm::errs() << "cannot open file " << Path << ": " << EC.message() << "\n";
Benjamin Kramerdf8c2622017-08-20 13:03:32 +000062 return nullptr;
Martin Storsjo9d8ecb42017-08-17 05:58:27 +000063 }
Martell Malone1079ef82017-07-18 21:26:38 +000064
Benjamin Kramerdf8c2622017-08-20 13:03:32 +000065 return std::move(*MB);
Martell Malone1079ef82017-07-18 21:26:38 +000066}
67
68static MachineTypes getEmulation(StringRef S) {
69 return StringSwitch<MachineTypes>(S)
70 .Case("i386", IMAGE_FILE_MACHINE_I386)
71 .Case("i386:x86-64", IMAGE_FILE_MACHINE_AMD64)
72 .Case("arm", IMAGE_FILE_MACHINE_ARMNT)
Martin Storsjoc9263f42017-08-06 19:58:13 +000073 .Case("arm64", IMAGE_FILE_MACHINE_ARM64)
Martell Malone1079ef82017-07-18 21:26:38 +000074 .Default(IMAGE_FILE_MACHINE_UNKNOWN);
75}
76
Benjamin Kramerdf8c2622017-08-20 13:03:32 +000077static std::string getImplibPath(StringRef Path) {
Martell Malone1079ef82017-07-18 21:26:38 +000078 SmallString<128> Out = StringRef("lib");
79 Out.append(Path);
80 sys::path::replace_extension(Out, ".a");
81 return Out.str();
82}
83
84int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
85 DllOptTable Table;
86 unsigned MissingIndex;
87 unsigned MissingCount;
88 llvm::opt::InputArgList Args =
89 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
90 if (MissingCount) {
91 llvm::errs() << Args.getArgString(MissingIndex) << ": missing argument\n";
92 return 1;
93 }
94
95 // Handle when no input or output is specified
96 if (Args.hasArgNoClaim(OPT_INPUT) ||
97 (!Args.hasArgNoClaim(OPT_d) && !Args.hasArgNoClaim(OPT_l))) {
Fangrui Song88478bb2018-10-10 00:15:31 +000098 Table.PrintHelp(outs(), "llvm-dlltool [options] file...", "llvm-dlltool",
99 false);
Martin Storsjod0852002017-09-08 06:49:46 +0000100 llvm::outs() << "\nTARGETS: i386, i386:x86-64, arm, arm64\n";
Martell Malone1079ef82017-07-18 21:26:38 +0000101 return 1;
102 }
103
104 if (!Args.hasArgNoClaim(OPT_m) && Args.hasArgNoClaim(OPT_d)) {
105 llvm::errs() << "error: no target machine specified\n"
Martin Storsjod0852002017-09-08 06:49:46 +0000106 << "supported targets: i386, i386:x86-64, arm, arm64\n";
Martell Malone1079ef82017-07-18 21:26:38 +0000107 return 1;
108 }
109
110 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
111 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
112
Martin Storsjo9d8ecb42017-08-17 05:58:27 +0000113 if (!Args.hasArg(OPT_d)) {
114 llvm::errs() << "no definition file specified\n";
115 return 1;
116 }
Martell Malone1079ef82017-07-18 21:26:38 +0000117
Benjamin Kramerdf8c2622017-08-20 13:03:32 +0000118 std::unique_ptr<MemoryBuffer> MB =
119 openFile(Args.getLastArg(OPT_d)->getValue());
Martin Storsjo9d8ecb42017-08-17 05:58:27 +0000120 if (!MB)
121 return 1;
122
123 if (!MB->getBufferSize()) {
Martell Malone1079ef82017-07-18 21:26:38 +0000124 llvm::errs() << "definition file empty\n";
125 return 1;
126 }
127
128 COFF::MachineTypes Machine = IMAGE_FILE_MACHINE_UNKNOWN;
129 if (auto *Arg = Args.getLastArg(OPT_m))
130 Machine = getEmulation(Arg->getValue());
131
132 if (Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
133 llvm::errs() << "unknown target\n";
134 return 1;
135 }
136
137 Expected<COFFModuleDefinition> Def =
Martin Storsjo9d8ecb42017-08-17 05:58:27 +0000138 parseCOFFModuleDefinition(*MB, Machine, true);
Martell Malone1079ef82017-07-18 21:26:38 +0000139
140 if (!Def) {
141 llvm::errs() << "error parsing definition\n"
142 << errorToErrorCode(Def.takeError()).message();
143 return 1;
144 }
145
146 // Do this after the parser because parseCOFFModuleDefinition sets OutputFile.
147 if (auto *Arg = Args.getLastArg(OPT_D))
148 Def->OutputFile = Arg->getValue();
149
150 if (Def->OutputFile.empty()) {
151 llvm::errs() << "no output file specified\n";
152 return 1;
153 }
154
155 std::string Path = Args.getLastArgValue(OPT_l);
156 if (Path.empty())
157 Path = getImplibPath(Def->OutputFile);
158
Martin Storsjo58c95272017-08-16 05:18:36 +0000159 if (Machine == IMAGE_FILE_MACHINE_I386 && Args.getLastArg(OPT_k)) {
160 for (COFFShortExport& E : Def->Exports) {
Martin Storsjo284ab802018-05-09 09:21:53 +0000161 if (!E.AliasTarget.empty() || (!E.Name.empty() && E.Name[0] == '?'))
Martin Storsjo58c95272017-08-16 05:18:36 +0000162 continue;
163 E.SymbolName = E.Name;
164 // Trim off the trailing decoration. Symbols will always have a
165 // starting prefix here (either _ for cdecl/stdcall, @ for fastcall
Martin Storsjo843cbbd2017-10-23 09:08:13 +0000166 // or ? for C++ functions). Vectorcall functions won't have any
167 // fixed prefix, but the function base name will still be at least
168 // one char.
Martin Storsjo58c95272017-08-16 05:18:36 +0000169 E.Name = E.Name.substr(0, E.Name.find('@', 1));
170 // By making sure E.SymbolName != E.Name for decorated symbols,
171 // writeImportLibrary writes these symbols with the type
172 // IMPORT_NAME_UNDECORATE.
173 }
174 }
175
Martin Storsjo284ab802018-05-09 09:21:53 +0000176 if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine, true))
Martell Malone1079ef82017-07-18 21:26:38 +0000177 return 1;
178 return 0;
179}