xref: /llvm-project/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp (revision aa171833ab0017d9732e82b8682c9848ab25ff9e)
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 
14 #include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/BinaryFormat/COFF.h"
18 #include "llvm/BinaryFormat/Magic.h"
19 #include "llvm/Bitcode/BitcodeReader.h"
20 #include "llvm/Object/ArchiveWriter.h"
21 #include "llvm/Object/COFF.h"
22 #include "llvm/Object/WindowsMachineFlag.h"
23 #include "llvm/Option/Arg.h"
24 #include "llvm/Option/ArgList.h"
25 #include "llvm/Option/Option.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/Process.h"
29 #include "llvm/Support/StringSaver.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <optional>
32 
33 using namespace llvm;
34 
35 namespace {
36 
37 enum {
38   OPT_INVALID = 0,
39 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
40 #include "Options.inc"
41 #undef OPTION
42 };
43 
44 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
45 #include "Options.inc"
46 #undef PREFIX
47 
48 static constexpr opt::OptTable::Info InfoTable[] = {
49 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
50   {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
51    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
52 #include "Options.inc"
53 #undef OPTION
54 };
55 
56 class LibOptTable : public opt::OptTable {
57 public:
58   LibOptTable() : OptTable(InfoTable, true) {}
59 };
60 
61 }
62 
63 static std::string getDefaultOutputPath(const NewArchiveMember &FirstMember) {
64   SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
65   sys::path::replace_extension(Val, ".lib");
66   return std::string(Val.str());
67 }
68 
69 static std::vector<StringRef> getSearchPaths(opt::InputArgList *Args,
70                                              StringSaver &Saver) {
71   std::vector<StringRef> Ret;
72   // Add current directory as first item of the search path.
73   Ret.push_back("");
74 
75   // Add /libpath flags.
76   for (auto *Arg : Args->filtered(OPT_libpath))
77     Ret.push_back(Arg->getValue());
78 
79   // Add $LIB.
80   std::optional<std::string> EnvOpt = sys::Process::GetEnv("LIB");
81   if (!EnvOpt)
82     return Ret;
83   StringRef Env = Saver.save(*EnvOpt);
84   while (!Env.empty()) {
85     StringRef Path;
86     std::tie(Path, Env) = Env.split(';');
87     Ret.push_back(Path);
88   }
89   return Ret;
90 }
91 
92 static std::string findInputFile(StringRef File, ArrayRef<StringRef> Paths) {
93   for (StringRef Dir : Paths) {
94     SmallString<128> Path = Dir;
95     sys::path::append(Path, File);
96     if (sys::fs::exists(Path))
97       return std::string(Path);
98   }
99   return "";
100 }
101 
102 static void fatalOpenError(llvm::Error E, Twine File) {
103   if (!E)
104     return;
105   handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
106     llvm::errs() << "error opening '" << File << "': " << EIB.message() << '\n';
107     exit(1);
108   });
109 }
110 
111 static void doList(opt::InputArgList& Args) {
112   // lib.exe prints the contents of the first archive file.
113   std::unique_ptr<MemoryBuffer> B;
114   for (auto *Arg : Args.filtered(OPT_INPUT)) {
115     // Create or open the archive object.
116     ErrorOr<std::unique_ptr<MemoryBuffer>> MaybeBuf = MemoryBuffer::getFile(
117         Arg->getValue(), /*IsText=*/false, /*RequiresNullTerminator=*/false);
118     fatalOpenError(errorCodeToError(MaybeBuf.getError()), Arg->getValue());
119 
120     if (identify_magic(MaybeBuf.get()->getBuffer()) == file_magic::archive) {
121       B = std::move(MaybeBuf.get());
122       break;
123     }
124   }
125 
126   // lib.exe doesn't print an error if no .lib files are passed.
127   if (!B)
128     return;
129 
130   Error Err = Error::success();
131   object::Archive Archive(B.get()->getMemBufferRef(), Err);
132   fatalOpenError(std::move(Err), B->getBufferIdentifier());
133 
134   for (auto &C : Archive.children(Err)) {
135     Expected<StringRef> NameOrErr = C.getName();
136     fatalOpenError(NameOrErr.takeError(), B->getBufferIdentifier());
137     StringRef Name = NameOrErr.get();
138     llvm::outs() << Name << '\n';
139   }
140   fatalOpenError(std::move(Err), B->getBufferIdentifier());
141 }
142 
143 static Expected<COFF::MachineTypes> getCOFFFileMachine(MemoryBufferRef MB) {
144   std::error_code EC;
145   auto Obj = object::COFFObjectFile::create(MB);
146   if (!Obj)
147     return Obj.takeError();
148 
149   uint16_t Machine = (*Obj)->getMachine();
150   if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
151       Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
152       Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
153       Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
154     return createStringError(inconvertibleErrorCode(),
155                              "unknown machine: " + std::to_string(Machine));
156   }
157 
158   return static_cast<COFF::MachineTypes>(Machine);
159 }
160 
161 static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) {
162   Expected<std::string> TripleStr = getBitcodeTargetTriple(MB);
163   if (!TripleStr)
164     return TripleStr.takeError();
165 
166   switch (Triple(*TripleStr).getArch()) {
167   case Triple::x86:
168     return COFF::IMAGE_FILE_MACHINE_I386;
169   case Triple::x86_64:
170     return COFF::IMAGE_FILE_MACHINE_AMD64;
171   case Triple::arm:
172     return COFF::IMAGE_FILE_MACHINE_ARMNT;
173   case Triple::aarch64:
174     return COFF::IMAGE_FILE_MACHINE_ARM64;
175   default:
176     return createStringError(inconvertibleErrorCode(),
177                              "unknown arch in target triple: " + *TripleStr);
178   }
179 }
180 
181 static void appendFile(std::vector<NewArchiveMember> &Members,
182                        COFF::MachineTypes &LibMachine,
183                        std::string &LibMachineSource, MemoryBufferRef MB) {
184   file_magic Magic = identify_magic(MB.getBuffer());
185 
186   if (Magic != file_magic::coff_object && Magic != file_magic::bitcode &&
187       Magic != file_magic::archive && Magic != file_magic::windows_resource &&
188       Magic != file_magic::coff_import_library) {
189     llvm::errs() << MB.getBufferIdentifier()
190                  << ": not a COFF object, bitcode, archive, import library or "
191                     "resource file\n";
192     exit(1);
193   }
194 
195   // If a user attempts to add an archive to another archive, llvm-lib doesn't
196   // handle the first archive file as a single file. Instead, it extracts all
197   // members from the archive and add them to the second archive. This behavior
198   // is for compatibility with Microsoft's lib command.
199   if (Magic == file_magic::archive) {
200     Error Err = Error::success();
201     object::Archive Archive(MB, Err);
202     fatalOpenError(std::move(Err), MB.getBufferIdentifier());
203 
204     for (auto &C : Archive.children(Err)) {
205       Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef();
206       if (!ChildMB) {
207         handleAllErrors(ChildMB.takeError(), [&](const ErrorInfoBase &EIB) {
208           llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message()
209                        << "\n";
210         });
211         exit(1);
212       }
213 
214       appendFile(Members, LibMachine, LibMachineSource, *ChildMB);
215     }
216 
217     fatalOpenError(std::move(Err), MB.getBufferIdentifier());
218     return;
219   }
220 
221   // Check that all input files have the same machine type.
222   // Mixing normal objects and LTO bitcode files is fine as long as they
223   // have the same machine type.
224   // Doing this here duplicates the header parsing work that writeArchive()
225   // below does, but it's not a lot of work and it's a bit awkward to do
226   // in writeArchive() which needs to support many tools, can't assume the
227   // input is COFF, and doesn't have a good way to report errors.
228   if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) {
229     Expected<COFF::MachineTypes> MaybeFileMachine =
230         (Magic == file_magic::coff_object) ? getCOFFFileMachine(MB)
231                                            : getBitcodeFileMachine(MB);
232     if (!MaybeFileMachine) {
233       handleAllErrors(MaybeFileMachine.takeError(),
234                       [&](const ErrorInfoBase &EIB) {
235                         llvm::errs() << MB.getBufferIdentifier() << ": "
236                                      << EIB.message() << "\n";
237                       });
238       exit(1);
239     }
240     COFF::MachineTypes FileMachine = *MaybeFileMachine;
241 
242     // FIXME: Once lld-link rejects multiple resource .obj files:
243     // Call convertResToCOFF() on .res files and add the resulting
244     // COFF file to the .lib output instead of adding the .res file, and remove
245     // this check. See PR42180.
246     if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
247       if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
248         LibMachine = FileMachine;
249         LibMachineSource =
250             (" (inferred from earlier file '" + MB.getBufferIdentifier() + "')")
251                 .str();
252       } else if (LibMachine != FileMachine) {
253         llvm::errs() << MB.getBufferIdentifier() << ": file machine type "
254                      << machineToStr(FileMachine)
255                      << " conflicts with library machine type "
256                      << machineToStr(LibMachine) << LibMachineSource << '\n';
257         exit(1);
258       }
259     }
260   }
261 
262   Members.emplace_back(MB);
263 }
264 
265 int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
266   BumpPtrAllocator Alloc;
267   StringSaver Saver(Alloc);
268 
269   // Parse command line arguments.
270   SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
271   cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
272   ArgsArr = NewArgs;
273 
274   LibOptTable Table;
275   unsigned MissingIndex;
276   unsigned MissingCount;
277   opt::InputArgList Args =
278       Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
279   if (MissingCount) {
280     llvm::errs() << "missing arg value for \""
281                  << Args.getArgString(MissingIndex) << "\", expected "
282                  << MissingCount
283                  << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
284     return 1;
285   }
286   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
287     llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args)
288                  << "\n";
289 
290   // Handle /help
291   if (Args.hasArg(OPT_help)) {
292     Table.printHelp(outs(), "llvm-lib [options] file...", "LLVM Lib");
293     return 0;
294   }
295 
296   // Parse /ignore:
297   llvm::StringSet<> IgnoredWarnings;
298   for (auto *Arg : Args.filtered(OPT_ignore))
299     IgnoredWarnings.insert(Arg->getValue());
300 
301   // If no input files and not told otherwise, silently do nothing to match
302   // lib.exe
303   if (!Args.hasArgNoClaim(OPT_INPUT) && !Args.hasArg(OPT_llvmlibempty)) {
304     if (!IgnoredWarnings.contains("emptyoutput")) {
305       llvm::errs() << "warning: no input files, not writing output file\n";
306       llvm::errs() << "         pass /llvmlibempty to write empty .lib file,\n";
307       llvm::errs() << "         pass /ignore:emptyoutput to suppress warning\n";
308       if (Args.hasFlag(OPT_WX, OPT_WX_no, false)) {
309         llvm::errs() << "treating warning as error due to /WX\n";
310         return 1;
311       }
312     }
313     return 0;
314   }
315 
316   if (Args.hasArg(OPT_lst)) {
317     doList(Args);
318     return 0;
319   }
320 
321   std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
322 
323   COFF::MachineTypes LibMachine = COFF::IMAGE_FILE_MACHINE_UNKNOWN;
324   std::string LibMachineSource;
325   if (auto *Arg = Args.getLastArg(OPT_machine)) {
326     LibMachine = getMachineType(Arg->getValue());
327     if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
328       llvm::errs() << "unknown /machine: arg " << Arg->getValue() << '\n';
329       return 1;
330     }
331     LibMachineSource =
332         std::string(" (from '/machine:") + Arg->getValue() + "' flag)";
333   }
334 
335   std::vector<std::unique_ptr<MemoryBuffer>> MBs;
336   StringSet<> Seen;
337   std::vector<NewArchiveMember> Members;
338 
339   // Create a NewArchiveMember for each input file.
340   for (auto *Arg : Args.filtered(OPT_INPUT)) {
341     // Find a file
342     std::string Path = findInputFile(Arg->getValue(), SearchPaths);
343     if (Path.empty()) {
344       llvm::errs() << Arg->getValue() << ": no such file or directory\n";
345       return 1;
346     }
347 
348     // Input files are uniquified by pathname. If you specify the exact same
349     // path more than once, all but the first one are ignored.
350     //
351     // Note that there's a loophole in the rule; you can prepend `.\` or
352     // something like that to a path to make it look different, and they are
353     // handled as if they were different files. This behavior is compatible with
354     // Microsoft lib.exe.
355     if (!Seen.insert(Path).second)
356       continue;
357 
358     // Open a file.
359     ErrorOr<std::unique_ptr<MemoryBuffer>> MOrErr = MemoryBuffer::getFile(
360         Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
361     fatalOpenError(errorCodeToError(MOrErr.getError()), Path);
362     MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef();
363 
364     // Append a file.
365     appendFile(Members, LibMachine, LibMachineSource, MBRef);
366 
367     // Take the ownership of the file buffer to keep the file open.
368     MBs.push_back(std::move(*MOrErr));
369   }
370 
371   // Create an archive file.
372   std::string OutputPath;
373   if (auto *Arg = Args.getLastArg(OPT_out)) {
374     OutputPath = Arg->getValue();
375   } else if (!Members.empty()) {
376     OutputPath = getDefaultOutputPath(Members[0]);
377   } else {
378     llvm::errs() << "no output path given, and cannot infer with no inputs\n";
379     return 1;
380   }
381   // llvm-lib uses relative paths for both regular and thin archives, unlike
382   // standard GNU ar, which only uses relative paths for thin archives and
383   // basenames for regular archives.
384   for (NewArchiveMember &Member : Members) {
385     if (sys::path::is_relative(Member.MemberName)) {
386       Expected<std::string> PathOrErr =
387           computeArchiveRelativePath(OutputPath, Member.MemberName);
388       if (PathOrErr)
389         Member.MemberName = Saver.save(*PathOrErr);
390     }
391   }
392 
393   if (Error E =
394           writeArchive(OutputPath, Members,
395                        /*WriteSymtab=*/true, object::Archive::K_GNU,
396                        /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) {
397     handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
398       llvm::errs() << OutputPath << ": " << EI.message() << "\n";
399     });
400     return 1;
401   }
402 
403   return 0;
404 }
405