blob: c5a28d4f1c080e56703deb09db27a4dee1958e50 [file] [log] [blame]
Martell Malone1079ef82017-07-18 21:26:38 +00001//===- DlltoolDriver.cpp - dlltool.exe-compatible driver ------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Defines an interface to a dlltool.exe-compatible driver.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ToolDrivers/llvm-dlltool/DlltoolDriver.h"
Martell Malone1079ef82017-07-18 21:26:38 +000015#include "llvm/Object/COFF.h"
16#include "llvm/Object/COFFImportFile.h"
17#include "llvm/Object/COFFModuleDefinition.h"
18#include "llvm/Option/Arg.h"
19#include "llvm/Option/ArgList.h"
20#include "llvm/Option/Option.h"
21#include "llvm/Support/Path.h"
22
Martell Malone1079ef82017-07-18 21:26:38 +000023#include <vector>
24
25using namespace llvm;
26using namespace llvm::object;
27using namespace llvm::COFF;
28
29namespace {
30
31enum {
32 OPT_INVALID = 0,
33#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
34#include "Options.inc"
35#undef OPTION
36};
37
38#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
39#include "Options.inc"
40#undef PREFIX
41
Martell Malonecc82cdf2017-08-23 02:10:28 +000042static const llvm::opt::OptTable::Info InfoTable[] = {
Martell Malone1079ef82017-07-18 21:26:38 +000043#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
44 {X1, X2, X10, X11, OPT_##ID, llvm::opt::Option::KIND##Class, \
45 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
46#include "Options.inc"
47#undef OPTION
48};
49
50class DllOptTable : public llvm::opt::OptTable {
51public:
Martell Malonecc82cdf2017-08-23 02:10:28 +000052 DllOptTable() : OptTable(InfoTable, false) {}
Martell Malone1079ef82017-07-18 21:26:38 +000053};
54
55} // namespace
56
Martell Malone1079ef82017-07-18 21:26:38 +000057// Opens a file. Path has to be resolved already.
Benjamin Kramerdf8c2622017-08-20 13:03:32 +000058static std::unique_ptr<MemoryBuffer> openFile(const Twine &Path) {
Martell Malone1079ef82017-07-18 21:26:38 +000059 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MB = MemoryBuffer::getFile(Path);
60
Martin Storsjo9d8ecb42017-08-17 05:58:27 +000061 if (std::error_code EC = MB.getError()) {
Martin Storsjocaff3262017-08-17 06:26:42 +000062 llvm::errs() << "cannot open file " << Path << ": " << EC.message() << "\n";
Benjamin Kramerdf8c2622017-08-20 13:03:32 +000063 return nullptr;
Martin Storsjo9d8ecb42017-08-17 05:58:27 +000064 }
Martell Malone1079ef82017-07-18 21:26:38 +000065
Benjamin Kramerdf8c2622017-08-20 13:03:32 +000066 return std::move(*MB);
Martell Malone1079ef82017-07-18 21:26:38 +000067}
68
69static MachineTypes getEmulation(StringRef S) {
70 return StringSwitch<MachineTypes>(S)
71 .Case("i386", IMAGE_FILE_MACHINE_I386)
72 .Case("i386:x86-64", IMAGE_FILE_MACHINE_AMD64)
73 .Case("arm", IMAGE_FILE_MACHINE_ARMNT)
Martin Storsjoc9263f42017-08-06 19:58:13 +000074 .Case("arm64", IMAGE_FILE_MACHINE_ARM64)
Martell Malone1079ef82017-07-18 21:26:38 +000075 .Default(IMAGE_FILE_MACHINE_UNKNOWN);
76}
77
Benjamin Kramerdf8c2622017-08-20 13:03:32 +000078static std::string getImplibPath(StringRef Path) {
Martell Malone1079ef82017-07-18 21:26:38 +000079 SmallString<128> Out = StringRef("lib");
80 Out.append(Path);
81 sys::path::replace_extension(Out, ".a");
82 return Out.str();
83}
84
85int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
86 DllOptTable Table;
87 unsigned MissingIndex;
88 unsigned MissingCount;
89 llvm::opt::InputArgList Args =
90 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
91 if (MissingCount) {
92 llvm::errs() << Args.getArgString(MissingIndex) << ": missing argument\n";
93 return 1;
94 }
95
96 // Handle when no input or output is specified
97 if (Args.hasArgNoClaim(OPT_INPUT) ||
98 (!Args.hasArgNoClaim(OPT_d) && !Args.hasArgNoClaim(OPT_l))) {
Fangrui Song88478bb2018-10-10 00:15:31 +000099 Table.PrintHelp(outs(), "llvm-dlltool [options] file...", "llvm-dlltool",
100 false);
Martin Storsjod0852002017-09-08 06:49:46 +0000101 llvm::outs() << "\nTARGETS: i386, i386:x86-64, arm, arm64\n";
Martell Malone1079ef82017-07-18 21:26:38 +0000102 return 1;
103 }
104
105 if (!Args.hasArgNoClaim(OPT_m) && Args.hasArgNoClaim(OPT_d)) {
106 llvm::errs() << "error: no target machine specified\n"
Martin Storsjod0852002017-09-08 06:49:46 +0000107 << "supported targets: i386, i386:x86-64, arm, arm64\n";
Martell Malone1079ef82017-07-18 21:26:38 +0000108 return 1;
109 }
110
111 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
112 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
113
Martin Storsjo9d8ecb42017-08-17 05:58:27 +0000114 if (!Args.hasArg(OPT_d)) {
115 llvm::errs() << "no definition file specified\n";
116 return 1;
117 }
Martell Malone1079ef82017-07-18 21:26:38 +0000118
Benjamin Kramerdf8c2622017-08-20 13:03:32 +0000119 std::unique_ptr<MemoryBuffer> MB =
120 openFile(Args.getLastArg(OPT_d)->getValue());
Martin Storsjo9d8ecb42017-08-17 05:58:27 +0000121 if (!MB)
122 return 1;
123
124 if (!MB->getBufferSize()) {
Martell Malone1079ef82017-07-18 21:26:38 +0000125 llvm::errs() << "definition file empty\n";
126 return 1;
127 }
128
129 COFF::MachineTypes Machine = IMAGE_FILE_MACHINE_UNKNOWN;
130 if (auto *Arg = Args.getLastArg(OPT_m))
131 Machine = getEmulation(Arg->getValue());
132
133 if (Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
134 llvm::errs() << "unknown target\n";
135 return 1;
136 }
137
138 Expected<COFFModuleDefinition> Def =
Martin Storsjo9d8ecb42017-08-17 05:58:27 +0000139 parseCOFFModuleDefinition(*MB, Machine, true);
Martell Malone1079ef82017-07-18 21:26:38 +0000140
141 if (!Def) {
142 llvm::errs() << "error parsing definition\n"
143 << errorToErrorCode(Def.takeError()).message();
144 return 1;
145 }
146
147 // Do this after the parser because parseCOFFModuleDefinition sets OutputFile.
148 if (auto *Arg = Args.getLastArg(OPT_D))
149 Def->OutputFile = Arg->getValue();
150
151 if (Def->OutputFile.empty()) {
152 llvm::errs() << "no output file specified\n";
153 return 1;
154 }
155
156 std::string Path = Args.getLastArgValue(OPT_l);
157 if (Path.empty())
158 Path = getImplibPath(Def->OutputFile);
159
Martin Storsjo58c95272017-08-16 05:18:36 +0000160 if (Machine == IMAGE_FILE_MACHINE_I386 && Args.getLastArg(OPT_k)) {
161 for (COFFShortExport& E : Def->Exports) {
Martin Storsjo284ab802018-05-09 09:21:53 +0000162 if (!E.AliasTarget.empty() || (!E.Name.empty() && E.Name[0] == '?'))
Martin Storsjo58c95272017-08-16 05:18:36 +0000163 continue;
164 E.SymbolName = E.Name;
165 // Trim off the trailing decoration. Symbols will always have a
166 // starting prefix here (either _ for cdecl/stdcall, @ for fastcall
Martin Storsjo843cbbd2017-10-23 09:08:13 +0000167 // or ? for C++ functions). Vectorcall functions won't have any
168 // fixed prefix, but the function base name will still be at least
169 // one char.
Martin Storsjo58c95272017-08-16 05:18:36 +0000170 E.Name = E.Name.substr(0, E.Name.find('@', 1));
171 // By making sure E.SymbolName != E.Name for decorated symbols,
172 // writeImportLibrary writes these symbols with the type
173 // IMPORT_NAME_UNDECORATE.
174 }
175 }
176
Martin Storsjo284ab802018-05-09 09:21:53 +0000177 if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine, true))
Martell Malone1079ef82017-07-18 21:26:38 +0000178 return 1;
179 return 0;
180}