blob: c0b09ca106fdd444cb110a1448565fe063ab9d8d [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"
15#include "llvm/Object/ArchiveWriter.h"
16#include "llvm/Object/COFF.h"
17#include "llvm/Object/COFFImportFile.h"
18#include "llvm/Object/COFFModuleDefinition.h"
19#include "llvm/Option/Arg.h"
20#include "llvm/Option/ArgList.h"
21#include "llvm/Option/Option.h"
22#include "llvm/Support/Path.h"
23
24#include <string>
25#include <vector>
26
27using namespace llvm;
28using namespace llvm::object;
29using namespace llvm::COFF;
30
31namespace {
32
33enum {
34 OPT_INVALID = 0,
35#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
36#include "Options.inc"
37#undef OPTION
38};
39
40#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
41#include "Options.inc"
42#undef PREFIX
43
44static const llvm::opt::OptTable::Info infoTable[] = {
45#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
46 {X1, X2, X10, X11, OPT_##ID, llvm::opt::Option::KIND##Class, \
47 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
48#include "Options.inc"
49#undef OPTION
50};
51
52class DllOptTable : public llvm::opt::OptTable {
53public:
54 DllOptTable() : OptTable(infoTable, false) {}
55};
56
57} // namespace
58
59std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
60
61// Opens a file. Path has to be resolved already.
62// Newly created memory buffers are owned by this driver.
63MemoryBufferRef openFile(StringRef Path) {
64 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MB = MemoryBuffer::getFile(Path);
65
66 if (std::error_code EC = MB.getError())
67 llvm::errs() << "fail openFile: " << EC.message() << "\n";
68
69 MemoryBufferRef MBRef = MB.get()->getMemBufferRef();
70 OwningMBs.push_back(std::move(MB.get())); // take ownership
71 return MBRef;
72}
73
74static MachineTypes getEmulation(StringRef S) {
75 return StringSwitch<MachineTypes>(S)
76 .Case("i386", IMAGE_FILE_MACHINE_I386)
77 .Case("i386:x86-64", IMAGE_FILE_MACHINE_AMD64)
78 .Case("arm", IMAGE_FILE_MACHINE_ARMNT)
Martin Storsjoc9263f42017-08-06 19:58:13 +000079 .Case("arm64", IMAGE_FILE_MACHINE_ARM64)
Martell Malone1079ef82017-07-18 21:26:38 +000080 .Default(IMAGE_FILE_MACHINE_UNKNOWN);
81}
82
83static std::string getImplibPath(std::string Path) {
84 SmallString<128> Out = StringRef("lib");
85 Out.append(Path);
86 sys::path::replace_extension(Out, ".a");
87 return Out.str();
88}
89
90int llvm::dlltoolDriverMain(llvm::ArrayRef<const char *> ArgsArr) {
91 DllOptTable Table;
92 unsigned MissingIndex;
93 unsigned MissingCount;
94 llvm::opt::InputArgList Args =
95 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
96 if (MissingCount) {
97 llvm::errs() << Args.getArgString(MissingIndex) << ": missing argument\n";
98 return 1;
99 }
100
101 // Handle when no input or output is specified
102 if (Args.hasArgNoClaim(OPT_INPUT) ||
103 (!Args.hasArgNoClaim(OPT_d) && !Args.hasArgNoClaim(OPT_l))) {
104 Table.PrintHelp(outs(), ArgsArr[0], "dlltool", false);
105 llvm::outs() << "\nTARGETS: i386, i386:x86-64, arm\n";
106 return 1;
107 }
108
109 if (!Args.hasArgNoClaim(OPT_m) && Args.hasArgNoClaim(OPT_d)) {
110 llvm::errs() << "error: no target machine specified\n"
111 << "supported targets: i386, i386:x86-64, arm\n";
112 return 1;
113 }
114
115 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
116 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
117
118 MemoryBufferRef MB;
119 if (auto *Arg = Args.getLastArg(OPT_d))
120 MB = openFile(Arg->getValue());
121
122 if (!MB.getBufferSize()) {
123 llvm::errs() << "definition file empty\n";
124 return 1;
125 }
126
127 COFF::MachineTypes Machine = IMAGE_FILE_MACHINE_UNKNOWN;
128 if (auto *Arg = Args.getLastArg(OPT_m))
129 Machine = getEmulation(Arg->getValue());
130
131 if (Machine == IMAGE_FILE_MACHINE_UNKNOWN) {
132 llvm::errs() << "unknown target\n";
133 return 1;
134 }
135
136 Expected<COFFModuleDefinition> Def =
137 parseCOFFModuleDefinition(MB, Machine, true);
138
139 if (!Def) {
140 llvm::errs() << "error parsing definition\n"
141 << errorToErrorCode(Def.takeError()).message();
142 return 1;
143 }
144
145 // Do this after the parser because parseCOFFModuleDefinition sets OutputFile.
146 if (auto *Arg = Args.getLastArg(OPT_D))
147 Def->OutputFile = Arg->getValue();
148
149 if (Def->OutputFile.empty()) {
150 llvm::errs() << "no output file specified\n";
151 return 1;
152 }
153
154 std::string Path = Args.getLastArgValue(OPT_l);
155 if (Path.empty())
156 Path = getImplibPath(Def->OutputFile);
157
Martin Storsjo58c95272017-08-16 05:18:36 +0000158 if (Machine == IMAGE_FILE_MACHINE_I386 && Args.getLastArg(OPT_k)) {
159 for (COFFShortExport& E : Def->Exports) {
160 if (E.isWeak() || (!E.Name.empty() && E.Name[0] == '?'))
161 continue;
162 E.SymbolName = E.Name;
163 // Trim off the trailing decoration. Symbols will always have a
164 // starting prefix here (either _ for cdecl/stdcall, @ for fastcall
165 // or ? for C++ functions). (Vectorcall functions also will end up having
166 // a prefix here, even if they shouldn't.)
167 E.Name = E.Name.substr(0, E.Name.find('@', 1));
168 // By making sure E.SymbolName != E.Name for decorated symbols,
169 // writeImportLibrary writes these symbols with the type
170 // IMPORT_NAME_UNDECORATE.
171 }
172 }
173
Martell Malone1079ef82017-07-18 21:26:38 +0000174 if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine))
175 return 1;
176 return 0;
177}