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/BinaryFormat/COFF.h"
20 #include "llvm/Object/COFF.h"
22 #include "llvm/Option/Arg.h"
23 #include "llvm/Option/ArgList.h"
24 #include "llvm/Option/Option.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/Process.h"
30 
31 using namespace llvm;
32 
33 namespace {
34 
35 enum {
36  OPT_INVALID = 0,
37 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
38 #include "Options.inc"
39 #undef OPTION
40 };
41 
42 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
43 #include "Options.inc"
44 #undef PREFIX
45 
46 static const opt::OptTable::Info InfoTable[] = {
47 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
48  {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \
49  X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
50 #include "Options.inc"
51 #undef OPTION
52 };
53 
54 class LibOptTable : public opt::OptTable {
55 public:
56  LibOptTable() : OptTable(InfoTable, true) {}
57 };
58 
59 }
60 
61 static std::string getOutputPath(opt::InputArgList *Args,
62  const NewArchiveMember &FirstMember) {
63  if (auto *Arg = Args->getLastArg(OPT_out))
64  return Arg->getValue();
65  SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
66  sys::path::replace_extension(Val, ".lib");
67  return Val.str();
68 }
69 
70 static std::vector<StringRef> getSearchPaths(opt::InputArgList *Args,
71  StringSaver &Saver) {
72  std::vector<StringRef> Ret;
73  // Add current directory as first item of the search path.
74  Ret.push_back("");
75 
76  // Add /libpath flags.
77  for (auto *Arg : Args->filtered(OPT_libpath))
78  Ret.push_back(Arg->getValue());
79 
80  // Add $LIB.
82  if (!EnvOpt.hasValue())
83  return Ret;
84  StringRef Env = Saver.save(*EnvOpt);
85  while (!Env.empty()) {
86  StringRef Path;
87  std::tie(Path, Env) = Env.split(';');
88  Ret.push_back(Path);
89  }
90  return Ret;
91 }
92 
93 static std::string findInputFile(StringRef File, ArrayRef<StringRef> Paths) {
94  for (StringRef Dir : Paths) {
95  SmallString<128> Path = Dir;
96  sys::path::append(Path, File);
97  if (sys::fs::exists(Path))
98  return Path.str().str();
99  }
100  return "";
101 }
102 
104  if (!E)
105  return;
106  handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
107  llvm::errs() << "error opening '" << File << "': " << EIB.message() << '\n';
108  exit(1);
109  });
110 }
111 
113  // lib.exe prints the contents of the first archive file.
114  std::unique_ptr<MemoryBuffer> B;
115  for (auto *Arg : Args.filtered(OPT_INPUT)) {
116  // Create or open the archive object.
118  MemoryBuffer::getFile(Arg->getValue(), -1, false);
119  fatalOpenError(errorCodeToError(MaybeBuf.getError()), Arg->getValue());
120 
121  if (identify_magic(MaybeBuf.get()->getBuffer()) == file_magic::archive) {
122  B = std::move(MaybeBuf.get());
123  break;
124  }
125  }
126 
127  // lib.exe doesn't print an error if no .lib files are passed.
128  if (!B)
129  return;
130 
131  Error Err = Error::success();
132  object::Archive Archive(B.get()->getMemBufferRef(), Err);
133  fatalOpenError(std::move(Err), B->getBufferIdentifier());
134 
135  for (auto &C : Archive.children(Err)) {
136  Expected<StringRef> NameOrErr = C.getName();
137  fatalOpenError(NameOrErr.takeError(), B->getBufferIdentifier());
138  StringRef Name = NameOrErr.get();
139  llvm::outs() << Name << '\n';
140  }
141  fatalOpenError(std::move(Err), B->getBufferIdentifier());
142 }
143 
145  BumpPtrAllocator Alloc;
146  StringSaver Saver(Alloc);
147 
148  // Parse command line arguments.
149  SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
151  ArgsArr = NewArgs;
152 
153  LibOptTable Table;
154  unsigned MissingIndex;
155  unsigned MissingCount;
157  Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
158  if (MissingCount) {
159  llvm::errs() << "missing arg value for \""
160  << Args.getArgString(MissingIndex) << "\", expected "
161  << MissingCount
162  << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
163  return 1;
164  }
165  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
166  llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args)
167  << "\n";
168 
169  // Handle /help
170  if (Args.hasArg(OPT_help)) {
171  Table.PrintHelp(outs(), "llvm-lib [options] file...", "LLVM Lib");
172  return 0;
173  }
174 
175  // If no input files, silently do nothing to match lib.exe.
176  if (!Args.hasArgNoClaim(OPT_INPUT))
177  return 0;
178 
179  if (Args.hasArg(OPT_lst)) {
180  doList(Args);
181  return 0;
182  }
183 
184  std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
185 
187  std::string LibMachineSource;
188  if (auto *Arg = Args.getLastArg(OPT_machine)) {
189  LibMachine = getMachineType(Arg->getValue());
190  if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
191  llvm::errs() << "unknown /machine: arg " << Arg->getValue() << '\n';
192  return 1;
193  }
194  LibMachineSource =
195  std::string(" (from '/machine:") + Arg->getValue() + "' flag)";
196  }
197 
198  // Create a NewArchiveMember for each input file.
199  std::vector<NewArchiveMember> Members;
200  for (auto *Arg : Args.filtered(OPT_INPUT)) {
201  std::string Path = findInputFile(Arg->getValue(), SearchPaths);
202  if (Path.empty()) {
203  llvm::errs() << Arg->getValue() << ": no such file or directory\n";
204  return 1;
205  }
206 
208  NewArchiveMember::getFile(Saver.save(Path), /*Deterministic=*/true);
209  if (!MOrErr) {
210  handleAllErrors(MOrErr.takeError(), [&](const ErrorInfoBase &EIB) {
211  llvm::errs() << Arg->getValue() << ": " << EIB.message() << "\n";
212  });
213  return 1;
214  }
215 
216  file_magic Magic = identify_magic(MOrErr->Buf->getBuffer());
217  if (Magic != file_magic::coff_object && Magic != file_magic::bitcode &&
218  Magic != file_magic::windows_resource) {
219  llvm::errs() << Arg->getValue()
220  << ": not a COFF object, bitcode or resource file\n";
221  return 1;
222  }
223 
224  // Check that all input files have the same machine type.
225  // Mixing normal objects and LTO bitcode files is fine as long as they
226  // have the same machine type.
227  // Doing this here duplicates the header parsing work that writeArchive()
228  // below does, but it's not a lot of work and it's a bit awkward to do
229  // in writeArchive() which needs to support many tools, can't assume the
230  // input is COFF, and doesn't have a good way to report errors.
232  if (Magic == file_magic::coff_object) {
233  std::error_code EC;
234  object::COFFObjectFile Obj(*MOrErr->Buf, EC);
235  if (EC) {
236  llvm::errs() << Arg->getValue() << ": failed to open: " << EC.message()
237  << '\n';
238  return 1;
239  }
240  uint16_t Machine = Obj.getMachine();
241  if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
242  Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
243  Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
244  Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
245  llvm::errs() << Arg->getValue() << ": unknown machine: " << Machine
246  << '\n';
247  return 1;
248  }
249  FileMachine = static_cast<COFF::MachineTypes>(Machine);
250  } else if (Magic == file_magic::bitcode) {
251  Expected<std::string> TripleStr = getBitcodeTargetTriple(*MOrErr->Buf);
252  if (!TripleStr) {
253  llvm::errs() << Arg->getValue()
254  << ": failed to get target triple from bitcode\n";
255  return 1;
256  }
257  switch (Triple(*TripleStr).getArch()) {
258  case Triple::x86:
259  FileMachine = COFF::IMAGE_FILE_MACHINE_I386;
260  break;
261  case Triple::x86_64:
262  FileMachine = COFF::IMAGE_FILE_MACHINE_AMD64;
263  break;
264  case Triple::arm:
265  FileMachine = COFF::IMAGE_FILE_MACHINE_ARMNT;
266  break;
267  case Triple::aarch64:
268  FileMachine = COFF::IMAGE_FILE_MACHINE_ARM64;
269  break;
270  default:
271  llvm::errs() << Arg->getValue() << ": unknown arch in target triple "
272  << *TripleStr << '\n';
273  return 1;
274  }
275  }
276 
277  // FIXME: Once lld-link rejects multiple resource .obj files:
278  // Call convertResToCOFF() on .res files and add the resulting
279  // COFF file to the .lib output instead of adding the .res file, and remove
280  // this check. See PR42180.
281  if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
282  if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
283  LibMachine = FileMachine;
284  LibMachineSource = std::string(" (inferred from earlier file '") +
285  Arg->getValue() + "')";
286  } else if (LibMachine != FileMachine) {
287  llvm::errs() << Arg->getValue() << ": file machine type "
288  << machineToStr(FileMachine)
289  << " conflicts with library machine type "
290  << machineToStr(LibMachine) << LibMachineSource << '\n';
291  return 1;
292  }
293  }
294 
295  Members.emplace_back(std::move(*MOrErr));
296  }
297 
298  // Create an archive file.
299  std::string OutputPath = getOutputPath(&Args, Members[0]);
300  // llvm-lib uses relative paths for both regular and thin archives, unlike
301  // standard GNU ar, which only uses relative paths for thin archives and
302  // basenames for regular archives.
303  for (NewArchiveMember &Member : Members) {
304  if (sys::path::is_relative(Member.MemberName)) {
305  Expected<std::string> PathOrErr =
306  computeArchiveRelativePath(OutputPath, Member.MemberName);
307  if (PathOrErr)
308  Member.MemberName = Saver.save(*PathOrErr);
309  }
310  }
311 
312  if (Error E =
313  writeArchive(OutputPath, Members,
314  /*WriteSymtab=*/true, object::Archive::K_GNU,
315  /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) {
316  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
317  llvm::errs() << OutputPath << ": " << EI.message() << "\n";
318  });
319  return 1;
320  }
321 
322  return 0;
323 }
uint64_t CallInst * C
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:93
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
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:70
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:103
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:140
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:112
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:144
COFF object file.
Definition: Magic.h:44
uint16_t getMachine() const
Definition: COFF.h:811
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:904
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
StringRef save(const char *S)
Definition: StringSaver.h:28
Defines the llvm::Arg class for parsed arguments.
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
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:61
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 Expected< NewArchiveMember > getFile(StringRef FileName, bool Deterministic)
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
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