LLVM  10.0.0svn
LibDriver.cpp
Go to the documentation of this file.
1 //===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Defines an interface to a lib.exe-compatible driver that also understands
10 // bitcode files. Used by llvm-lib and lld-link /lib.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/BinaryFormat/COFF.h"
21 #include "llvm/Object/COFF.h"
23 #include "llvm/Option/Arg.h"
24 #include "llvm/Option/ArgList.h"
25 #include "llvm/Option/Option.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/Process.h"
31 
32 using namespace llvm;
33 
34 namespace {
35 
36 enum {
37  OPT_INVALID = 0,
38 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
39 #include "Options.inc"
40 #undef OPTION
41 };
42 
43 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
44 #include "Options.inc"
45 #undef PREFIX
46 
47 static const opt::OptTable::Info InfoTable[] = {
48 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
49  {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \
50  X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
51 #include "Options.inc"
52 #undef OPTION
53 };
54 
55 class LibOptTable : public opt::OptTable {
56 public:
57  LibOptTable() : OptTable(InfoTable, true) {}
58 };
59 
60 }
61 
62 static std::string getOutputPath(opt::InputArgList *Args,
63  const NewArchiveMember &FirstMember) {
64  if (auto *Arg = Args->getLastArg(OPT_out))
65  return Arg->getValue();
66  SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
67  sys::path::replace_extension(Val, ".lib");
68  return Val.str();
69 }
70 
71 static std::vector<StringRef> getSearchPaths(opt::InputArgList *Args,
72  StringSaver &Saver) {
73  std::vector<StringRef> Ret;
74  // Add current directory as first item of the search path.
75  Ret.push_back("");
76 
77  // Add /libpath flags.
78  for (auto *Arg : Args->filtered(OPT_libpath))
79  Ret.push_back(Arg->getValue());
80 
81  // Add $LIB.
83  if (!EnvOpt.hasValue())
84  return Ret;
85  StringRef Env = Saver.save(*EnvOpt);
86  while (!Env.empty()) {
87  StringRef Path;
88  std::tie(Path, Env) = Env.split(';');
89  Ret.push_back(Path);
90  }
91  return Ret;
92 }
93 
94 static std::string findInputFile(StringRef File, ArrayRef<StringRef> Paths) {
95  for (StringRef Dir : Paths) {
96  SmallString<128> Path = Dir;
97  sys::path::append(Path, File);
98  if (sys::fs::exists(Path))
99  return Path.str().str();
100  }
101  return "";
102 }
103 
105  if (!E)
106  return;
107  handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
108  llvm::errs() << "error opening '" << File << "': " << EIB.message() << '\n';
109  exit(1);
110  });
111 }
112 
114  // lib.exe prints the contents of the first archive file.
115  std::unique_ptr<MemoryBuffer> B;
116  for (auto *Arg : Args.filtered(OPT_INPUT)) {
117  // Create or open the archive object.
119  MemoryBuffer::getFile(Arg->getValue(), -1, false);
120  fatalOpenError(errorCodeToError(MaybeBuf.getError()), Arg->getValue());
121 
122  if (identify_magic(MaybeBuf.get()->getBuffer()) == file_magic::archive) {
123  B = std::move(MaybeBuf.get());
124  break;
125  }
126  }
127 
128  // lib.exe doesn't print an error if no .lib files are passed.
129  if (!B)
130  return;
131 
132  Error Err = Error::success();
133  object::Archive Archive(B.get()->getMemBufferRef(), Err);
134  fatalOpenError(std::move(Err), B->getBufferIdentifier());
135 
136  for (auto &C : Archive.children(Err)) {
137  Expected<StringRef> NameOrErr = C.getName();
138  fatalOpenError(NameOrErr.takeError(), B->getBufferIdentifier());
139  StringRef Name = NameOrErr.get();
140  llvm::outs() << Name << '\n';
141  }
142  fatalOpenError(std::move(Err), B->getBufferIdentifier());
143 }
144 
146  std::error_code EC;
147  object::COFFObjectFile Obj(MB, EC);
148  if (EC) {
150  << ": failed to open: " << EC.message() << '\n';
151  exit(1);
152  }
153 
154  uint16_t Machine = Obj.getMachine();
155  if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
156  Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
157  Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
158  Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
159  llvm::errs() << MB.getBufferIdentifier() << ": unknown machine: " << Machine
160  << '\n';
161  exit(1);
162  }
163 
164  return static_cast<COFF::MachineTypes>(Machine);
165 }
166 
169  if (!TripleStr) {
171  << ": failed to get target triple from bitcode\n";
172  exit(1);
173  }
174 
175  switch (Triple(*TripleStr).getArch()) {
176  case Triple::x86:
178  case Triple::x86_64:
180  case Triple::arm:
182  case Triple::aarch64:
184  default:
186  << ": unknown arch in target triple " << *TripleStr << '\n';
187  exit(1);
188  }
189 }
190 
191 static void appendFile(std::vector<NewArchiveMember> &Members,
192  COFF::MachineTypes &LibMachine,
193  std::string &LibMachineSource, MemoryBufferRef MB) {
195 
196  if (Magic != file_magic::coff_object && Magic != file_magic::bitcode &&
197  Magic != file_magic::archive && Magic != file_magic::windows_resource) {
199  << ": not a COFF object, bitcode, archive or resource file\n";
200  exit(1);
201  }
202 
203  // If a user attempts to add an archive to another archive, llvm-lib doesn't
204  // handle the first archive file as a single file. Instead, it extracts all
205  // members from the archive and add them to the second archive. This beahvior
206  // is for compatibility with Microsoft's lib command.
207  if (Magic == file_magic::archive) {
208  Error Err = Error::success();
209  object::Archive Archive(MB, Err);
210  fatalOpenError(std::move(Err), MB.getBufferIdentifier());
211 
212  for (auto &C : Archive.children(Err)) {
213  Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef();
214  if (!ChildMB) {
215  handleAllErrors(ChildMB.takeError(), [&](const ErrorInfoBase &EIB) {
216  llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message()
217  << "\n";
218  });
219  exit(1);
220  }
221 
222  appendFile(Members, LibMachine, LibMachineSource, *ChildMB);
223  }
224 
225  fatalOpenError(std::move(Err), MB.getBufferIdentifier());
226  return;
227  }
228 
229  // Check that all input files have the same machine type.
230  // Mixing normal objects and LTO bitcode files is fine as long as they
231  // have the same machine type.
232  // Doing this here duplicates the header parsing work that writeArchive()
233  // below does, but it's not a lot of work and it's a bit awkward to do
234  // in writeArchive() which needs to support many tools, can't assume the
235  // input is COFF, and doesn't have a good way to report errors.
236  if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) {
237  COFF::MachineTypes FileMachine = (Magic == file_magic::coff_object)
238  ? getCOFFFileMachine(MB)
239  : getBitcodeFileMachine(MB);
240 
241  // FIXME: Once lld-link rejects multiple resource .obj files:
242  // Call convertResToCOFF() on .res files and add the resulting
243  // COFF file to the .lib output instead of adding the .res file, and remove
244  // this check. See PR42180.
245  if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
246  if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
247  LibMachine = FileMachine;
248  LibMachineSource =
249  (" (inferred from earlier file '" + MB.getBufferIdentifier() + "')")
250  .str();
251  } else if (LibMachine != FileMachine) {
252  llvm::errs() << MB.getBufferIdentifier() << ": file machine type "
253  << machineToStr(FileMachine)
254  << " conflicts with library machine type "
255  << machineToStr(LibMachine) << LibMachineSource << '\n';
256  exit(1);
257  }
258  }
259  }
260 
261  Members.emplace_back(MB);
262 }
263 
265  BumpPtrAllocator Alloc;
266  StringSaver Saver(Alloc);
267 
268  // Parse command line arguments.
269  SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
271  ArgsArr = NewArgs;
272 
273  LibOptTable Table;
274  unsigned MissingIndex;
275  unsigned MissingCount;
277  Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
278  if (MissingCount) {
279  llvm::errs() << "missing arg value for \""
280  << Args.getArgString(MissingIndex) << "\", expected "
281  << MissingCount
282  << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
283  return 1;
284  }
285  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
286  llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args)
287  << "\n";
288 
289  // Handle /help
290  if (Args.hasArg(OPT_help)) {
291  Table.PrintHelp(outs(), "llvm-lib [options] file...", "LLVM Lib");
292  return 0;
293  }
294 
295  // If no input files, silently do nothing to match lib.exe.
296  if (!Args.hasArgNoClaim(OPT_INPUT))
297  return 0;
298 
299  if (Args.hasArg(OPT_lst)) {
300  doList(Args);
301  return 0;
302  }
303 
304  std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
305 
307  std::string LibMachineSource;
308  if (auto *Arg = Args.getLastArg(OPT_machine)) {
309  LibMachine = getMachineType(Arg->getValue());
310  if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
311  llvm::errs() << "unknown /machine: arg " << Arg->getValue() << '\n';
312  return 1;
313  }
314  LibMachineSource =
315  std::string(" (from '/machine:") + Arg->getValue() + "' flag)";
316  }
317 
318  std::vector<std::unique_ptr<MemoryBuffer>> MBs;
319  StringSet<> Seen;
320  std::vector<NewArchiveMember> Members;
321 
322  // Create a NewArchiveMember for each input file.
323  for (auto *Arg : Args.filtered(OPT_INPUT)) {
324  // Find a file
325  std::string Path = findInputFile(Arg->getValue(), SearchPaths);
326  if (Path.empty()) {
327  llvm::errs() << Arg->getValue() << ": no such file or directory\n";
328  return 1;
329  }
330 
331  // Input files are uniquified by pathname. If you specify the exact same
332  // path more than once, all but the first one are ignored.
333  //
334  // Note that there's a loophole in the rule; you can prepend `.\` or
335  // something like that to a path to make it look different, and they are
336  // handled as if they were different files. This behavior is compatible with
337  // Microsoft lib.exe.
338  if (!Seen.insert(Path).second)
339  continue;
340 
341  // Open a file.
343  MemoryBuffer::getFile(Path, -1, false);
344  fatalOpenError(errorCodeToError(MOrErr.getError()), Path);
345  MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef();
346 
347  // Append a file.
348  appendFile(Members, LibMachine, LibMachineSource, MBRef);
349 
350  // Take the ownership of the file buffer to keep the file open.
351  MBs.push_back(std::move(*MOrErr));
352  }
353 
354  // Create an archive file.
355  std::string OutputPath = getOutputPath(&Args, Members[0]);
356  // llvm-lib uses relative paths for both regular and thin archives, unlike
357  // standard GNU ar, which only uses relative paths for thin archives and
358  // basenames for regular archives.
359  for (NewArchiveMember &Member : Members) {
360  if (sys::path::is_relative(Member.MemberName)) {
361  Expected<std::string> PathOrErr =
362  computeArchiveRelativePath(OutputPath, Member.MemberName);
363  if (PathOrErr)
364  Member.MemberName = Saver.save(*PathOrErr);
365  }
366  }
367 
368  if (Error E =
369  writeArchive(OutputPath, Members,
370  /*WriteSymtab=*/true, object::Archive::K_GNU,
371  /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) {
372  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
373  llvm::errs() << OutputPath << ": " << EI.message() << "\n";
374  });
375  return 1;
376  }
377 
378  return 0;
379 }
uint64_t CallInst * C
static COFF::MachineTypes getCOFFFileMachine(MemoryBufferRef MB)
Definition: LibDriver.cpp:145
Represents either an error or a value T.
Definition: ErrorOr.h:56
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:232
This class represents lattice values for constants.
Definition: AllocatorList.h:23
iterator begin() const
Definition: ArrayRef.h:136
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static std::string findInputFile(StringRef File, ArrayRef< StringRef > Paths)
Definition: LibDriver.cpp:94
Windows compiled resource file (.res)
Definition: Magic.h:47
virtual std::string message() const
Return the error message as a string.
Definition: Error.h:56
iterator_range< filtered_iterator< sizeof...(OptSpecifiers)> > filtered(OptSpecifiers ...Ids) const
Definition: ArgList.h:205
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
Base class for error info classes.
Definition: Error.h:48
Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr)
Bitcode file.
Definition: Magic.h:23
const char * getArgString(unsigned Index) const override
getArgString - Return the input argument string at Index.
Definition: ArgList.h:411
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:455
MachineTypes
Definition: COFF.h:93
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:34
StringRef machineToStr(COFF::MachineTypes MT)
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
void TokenizeWindowsCommandLine(StringRef Source, StringSaver &Saver, SmallVectorImpl< const char *> &NewArgv, bool MarkEOLs=false)
Tokenizes a Windows command line which may contain quotes and escaped quotes.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
static std::vector< StringRef > getSearchPaths(opt::InputArgList *Args, StringSaver &Saver)
Definition: LibDriver.cpp:71
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
Definition: Triple.h:296
bool hasArg(OptSpecifiers ...Ids) const
Definition: ArgList.h:244
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
COFF::MachineTypes Machine
Definition: COFFYAML.cpp:365
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:240
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void fatalOpenError(llvm::Error E, Twine File)
Definition: LibDriver.cpp:104
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:141
Instrumentation for Order File
Provide access to the Option info table.
Definition: OptTable.h:38
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
std::error_code getError() const
Definition: ErrorOr.h:159
static void doList(opt::InputArgList &Args)
Definition: LibDriver.cpp:113
Expected< std::string > getBitcodeTargetTriple(MemoryBufferRef Buffer)
Read the header of the specified bitcode buffer and extract just the triple information.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
int libDriverMain(ArrayRef< const char *> ARgs)
Definition: LibDriver.cpp:264
COFF object file.
Definition: Magic.h:44
std::pair< typename base::iterator, bool > insert(StringRef Key)
Definition: StringSet.h:38
uint16_t getMachine() const
Definition: COFF.h:814
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
Arg * getLastArg(OptSpecifiers ...Ids) const
Return the last argument matching Id, or null.
Definition: ArgList.h:250
static const char *const Magic
Definition: Archive.cpp:41
ar style archive file
Definition: Magic.h:24
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:924
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
void replace_extension(SmallVectorImpl< char > &path, const Twine &extension, Style style=Style::native)
Replace the file extension of path with extension.
Definition: Path.cpp:480
LLVM_NODISCARD std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:710
iterator end() const
Definition: ArrayRef.h:137
reference get()
Returns a reference to the stored T value.
Definition: Error.h:532
static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB)
Definition: LibDriver.cpp:167
StringRef save(const char *S)
Definition: StringSaver.h:28
Defines the llvm::Arg class for parsed arguments.
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:274
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:178
iterator_range< child_iterator > children(Error &Err, bool SkipInternal=true) const
Definition: Archive.h:241
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer...
Definition: StringSaver.h:21
bool hasValue() const
Definition: Optional.h:259
Provides a library for accessing information about this process and other processes on the operating ...
Expected< std::string > computeArchiveRelativePath(StringRef From, StringRef To)
static std::string getOutputPath(opt::InputArgList *Args, const NewArchiveMember &FirstMember)
Definition: LibDriver.cpp:62
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
Entry for a single option instance in the option data table.
Definition: OptTable.h:41
bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition: Path.cpp:674
static void appendFile(std::vector< NewArchiveMember > &Members, COFF::MachineTypes &LibMachine, std::string &LibMachineSource, MemoryBufferRef MB)
Definition: LibDriver.cpp:191
COFF::MachineTypes getMachineType(StringRef S)
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.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:27
bool ExpandResponseFiles(StringSaver &Saver, TokenizerCallback Tokenizer, SmallVectorImpl< const char *> &Argv, bool MarkEOLs=false, bool RelativeNames=false)
Expand response files on a command line recursively using the given StringSaver and tokenization stra...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
std::unique_ptr< MemoryBuffer > Buf
Definition: ArchiveWriter.h:24
bool exists(const basic_file_status &status)
Does file exist?
Definition: Path.cpp:1025
static Optional< std::string > GetEnv(StringRef name)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
reference get()
Definition: ErrorOr.h:156
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition: Magic.h:20