LLVM  7.0.0svn
DlltoolDriver.cpp
Go to the documentation of this file.
1 //===- 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 
15 #include "llvm/Object/COFF.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 
23 #include <vector>
24 
25 using namespace llvm;
26 using namespace llvm::object;
27 using namespace llvm::COFF;
28 
29 namespace {
30 
31 enum {
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 
42 static const llvm::opt::OptTable::Info InfoTable[] = {
43 #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 
50 class DllOptTable : public llvm::opt::OptTable {
51 public:
52  DllOptTable() : OptTable(InfoTable, false) {}
53 };
54 
55 } // namespace
56 
57 // Opens a file. Path has to be resolved already.
58 static std::unique_ptr<MemoryBuffer> openFile(const Twine &Path) {
60 
61  if (std::error_code EC = MB.getError()) {
62  llvm::errs() << "cannot open file " << Path << ": " << EC.message() << "\n";
63  return nullptr;
64  }
65 
66  return std::move(*MB);
67 }
68 
71  .Case("i386", IMAGE_FILE_MACHINE_I386)
72  .Case("i386:x86-64", IMAGE_FILE_MACHINE_AMD64)
76 }
77 
78 static std::string getImplibPath(StringRef Path) {
79  SmallString<128> Out = StringRef("lib");
80  Out.append(Path);
82  return Out.str();
83 }
84 
86  DllOptTable Table;
87  unsigned MissingIndex;
88  unsigned MissingCount;
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))) {
99  Table.PrintHelp(outs(), ArgsArr[0], "dlltool", false);
100  llvm::outs() << "\nTARGETS: i386, i386:x86-64, arm, arm64\n";
101  return 1;
102  }
103 
104  if (!Args.hasArgNoClaim(OPT_m) && Args.hasArgNoClaim(OPT_d)) {
105  llvm::errs() << "error: no target machine specified\n"
106  << "supported targets: i386, i386:x86-64, arm, arm64\n";
107  return 1;
108  }
109 
110  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
111  llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
112 
113  if (!Args.hasArg(OPT_d)) {
114  llvm::errs() << "no definition file specified\n";
115  return 1;
116  }
117 
118  std::unique_ptr<MemoryBuffer> MB =
119  openFile(Args.getLastArg(OPT_d)->getValue());
120  if (!MB)
121  return 1;
122 
123  if (!MB->getBufferSize()) {
124  llvm::errs() << "definition file empty\n";
125  return 1;
126  }
127 
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 
138  parseCOFFModuleDefinition(*MB, Machine, true);
139 
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 
159  if (Machine == IMAGE_FILE_MACHINE_I386 && Args.getLastArg(OPT_k)) {
160  for (COFFShortExport& E : Def->Exports) {
161  if (E.isWeak() || (!E.Name.empty() && E.Name[0] == '?'))
162  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
166  // 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.
169  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 
176  if (writeImportLibrary(Def->OutputFile, Path, Def->Exports, Machine, true, true))
177  return 1;
178  return 0;
179 }
Represents either an error or a value T.
Definition: ErrorOr.h:69
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
iterator_range< filtered_iterator< sizeof...(OptSpecifiers)> > filtered(OptSpecifiers ...Ids) const
Definition: ArgList.h:209
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
Expected< COFFModuleDefinition > parseCOFFModuleDefinition(MemoryBufferRef MB, COFF::MachineTypes Machine, bool MingwDef=false)
const char * getArgString(unsigned Index) const override
getArgString - Return the input argument string at Index.
Definition: ArgList.h:413
static MachineTypes getEmulation(StringRef S)
MachineTypes
Definition: COFF.h:94
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
int dlltoolDriverMain(ArrayRef< const char *> ArgsArr)
Error writeImportLibrary(StringRef ImportName, StringRef Path, ArrayRef< COFFShortExport > Exports, COFF::MachineTypes Machine, bool MakeWeakAliases, bool MinGW)
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:74
bool hasArg(OptSpecifiers ...Ids) const
Definition: ArgList.h:248
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:363
void append(in_iter S, in_iter E)
Append from an iterator pair.
Definition: SmallString.h:75
raw_ostream & outs()
This returns a reference to a raw_ostream for standard output.
bool hasArgNoClaim(OptSpecifiers ...Ids) const
hasArg - Does the arg list contain any option matching Id.
Definition: ArgList.h:244
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
Provide access to the Option info table.
Definition: OptTable.h:39
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::error_code getError() const
Definition: ErrorOr.h:172
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Definition: StringSwitch.h:244
Arg * getLastArg(OptSpecifiers ...Ids) const
Return the last argument matching Id, or null.
Definition: ArgList.h:254
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition: Path.cpp:491
Defines the llvm::Arg class for parsed arguments.
static std::string getImplibPath(StringRef Path)
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...
Definition: ArrayRef.h:179
StringRef getLastArgValue(OptSpecifier Id, StringRef Default="") const
getLastArgValue - Return the value of the last argument, or a default.
Definition: ArgList.cpp:86
amdgpu Simplify well known AMD library false Value Value * Arg
Entry for a single option instance in the option data table.
Definition: OptTable.h:42
static std::unique_ptr< MemoryBuffer > openFile(const Twine &Path)
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
const char * getValue(unsigned N=0) const
Definition: Arg.h:96
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:84
constexpr char Args[]
Key for Kernel::Metadata::mArgs.