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