xref: /llvm-project/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp (revision 64a362e7216a43e3ad44e50a89265e72aeb14294)
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/BinaryFormat/COFF.h"
17 #include "llvm/BinaryFormat/Magic.h"
18 #include "llvm/Bitcode/BitcodeReader.h"
19 #include "llvm/Object/ArchiveWriter.h"
20 #include "llvm/Object/COFF.h"
21 #include "llvm/Object/WindowsMachineFlag.h"
22 #include "llvm/Option/Arg.h"
23 #include "llvm/Option/ArgList.h"
24 #include "llvm/Option/Option.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/Process.h"
28 #include "llvm/Support/StringSaver.h"
29 #include "llvm/Support/raw_ostream.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.
81   Optional<std::string> EnvOpt = sys::Process::GetEnv("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 
103 static void fatalOpenError(llvm::Error E, Twine File) {
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 
112 static void doList(opt::InputArgList& Args) {
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.
117     ErrorOr<std::unique_ptr<MemoryBuffer>> MaybeBuf =
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 
144 static COFF::MachineTypes getCOFFFileMachine(MemoryBufferRef MB) {
145   std::error_code EC;
146   object::COFFObjectFile Obj(MB, EC);
147   if (EC) {
148     llvm::errs() << MB.getBufferIdentifier()
149                  << ": failed to open: " << EC.message() << '\n';
150     exit(1);
151   }
152 
153   uint16_t Machine = Obj.getMachine();
154   if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
155       Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
156       Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
157       Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
158     llvm::errs() << MB.getBufferIdentifier() << ": unknown machine: " << Machine
159                  << '\n';
160     exit(1);
161   }
162 
163   return static_cast<COFF::MachineTypes>(Machine);
164 }
165 
166 static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB) {
167   Expected<std::string> TripleStr = getBitcodeTargetTriple(MB);
168   if (!TripleStr) {
169     llvm::errs() << MB.getBufferIdentifier()
170                  << ": failed to get target triple from bitcode\n";
171     exit(1);
172   }
173 
174   switch (Triple(*TripleStr).getArch()) {
175   case Triple::x86:
176     return COFF::IMAGE_FILE_MACHINE_I386;
177   case Triple::x86_64:
178     return COFF::IMAGE_FILE_MACHINE_AMD64;
179   case Triple::arm:
180     return COFF::IMAGE_FILE_MACHINE_ARMNT;
181   case Triple::aarch64:
182     return COFF::IMAGE_FILE_MACHINE_ARM64;
183   default:
184     llvm::errs() << MB.getBufferIdentifier()
185                  << ": unknown arch in target triple " << *TripleStr << '\n';
186     exit(1);
187   }
188 }
189 
190 static void appendFile(std::vector<NewArchiveMember> &Members,
191                        COFF::MachineTypes &LibMachine,
192                        std::string &LibMachineSource, MemoryBufferRef MB) {
193   file_magic Magic = identify_magic(MB.getBuffer());
194 
195   if (Magic != file_magic::coff_object && Magic != file_magic::bitcode &&
196       Magic != file_magic::archive && Magic != file_magic::windows_resource) {
197     llvm::errs() << MB.getBufferIdentifier()
198                  << ": not a COFF object, bitcode, archive or resource file\n";
199     exit(1);
200   }
201 
202   // If a user attempts to add an archive to another archive, llvm-lib doesn't
203   // handle the first archive file as a single file. Instead, it extracts all
204   // members from the archive and add them to the second archive. This beahvior
205   // is for compatibility with Microsoft's lib command.
206   if (Magic == file_magic::archive) {
207     Error Err = Error::success();
208     object::Archive Archive(MB, Err);
209     fatalOpenError(std::move(Err), MB.getBufferIdentifier());
210 
211     for (auto &C : Archive.children(Err)) {
212       Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef();
213       if (!ChildMB) {
214         handleAllErrors(ChildMB.takeError(), [&](const ErrorInfoBase &EIB) {
215           llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message()
216                        << "\n";
217         });
218         exit(1);
219       }
220 
221       appendFile(Members, LibMachine, LibMachineSource, *ChildMB);
222     }
223 
224     fatalOpenError(std::move(Err), MB.getBufferIdentifier());
225     return;
226   }
227 
228   // Check that all input files have the same machine type.
229   // Mixing normal objects and LTO bitcode files is fine as long as they
230   // have the same machine type.
231   // Doing this here duplicates the header parsing work that writeArchive()
232   // below does, but it's not a lot of work and it's a bit awkward to do
233   // in writeArchive() which needs to support many tools, can't assume the
234   // input is COFF, and doesn't have a good way to report errors.
235   if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) {
236     COFF::MachineTypes FileMachine = (Magic == file_magic::coff_object)
237                                          ? getCOFFFileMachine(MB)
238                                          : getBitcodeFileMachine(MB);
239 
240     // FIXME: Once lld-link rejects multiple resource .obj files:
241     // Call convertResToCOFF() on .res files and add the resulting
242     // COFF file to the .lib output instead of adding the .res file, and remove
243     // this check. See PR42180.
244     if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
245       if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
246         LibMachine = FileMachine;
247         LibMachineSource =
248             (" (inferred from earlier file '" + MB.getBufferIdentifier() + "')")
249                 .str();
250       } else if (LibMachine != FileMachine) {
251         llvm::errs() << MB.getBufferIdentifier() << ": file machine type "
252                      << machineToStr(FileMachine)
253                      << " conflicts with library machine type "
254                      << machineToStr(LibMachine) << LibMachineSource << '\n';
255         exit(1);
256       }
257     }
258   }
259 
260   Members.emplace_back(MB);
261 }
262 
263 int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
264   BumpPtrAllocator Alloc;
265   StringSaver Saver(Alloc);
266 
267   // Parse command line arguments.
268   SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
269   cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
270   ArgsArr = NewArgs;
271 
272   LibOptTable Table;
273   unsigned MissingIndex;
274   unsigned MissingCount;
275   opt::InputArgList Args =
276       Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
277   if (MissingCount) {
278     llvm::errs() << "missing arg value for \""
279                  << Args.getArgString(MissingIndex) << "\", expected "
280                  << MissingCount
281                  << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
282     return 1;
283   }
284   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
285     llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args)
286                  << "\n";
287 
288   // Handle /help
289   if (Args.hasArg(OPT_help)) {
290     Table.PrintHelp(outs(), "llvm-lib [options] file...", "LLVM Lib");
291     return 0;
292   }
293 
294   // If no input files, silently do nothing to match lib.exe.
295   if (!Args.hasArgNoClaim(OPT_INPUT))
296     return 0;
297 
298   if (Args.hasArg(OPT_lst)) {
299     doList(Args);
300     return 0;
301   }
302 
303   std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
304 
305   COFF::MachineTypes LibMachine = COFF::IMAGE_FILE_MACHINE_UNKNOWN;
306   std::string LibMachineSource;
307   if (auto *Arg = Args.getLastArg(OPT_machine)) {
308     LibMachine = getMachineType(Arg->getValue());
309     if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
310       llvm::errs() << "unknown /machine: arg " << Arg->getValue() << '\n';
311       return 1;
312     }
313     LibMachineSource =
314         std::string(" (from '/machine:") + Arg->getValue() + "' flag)";
315   }
316 
317   std::vector<std::unique_ptr<MemoryBuffer>> MBs;
318   std::vector<NewArchiveMember> Members;
319 
320   // Create a NewArchiveMember for each input file.
321   for (auto *Arg : Args.filtered(OPT_INPUT)) {
322     // Find a file
323     std::string Path = findInputFile(Arg->getValue(), SearchPaths);
324     if (Path.empty()) {
325       llvm::errs() << Arg->getValue() << ": no such file or directory\n";
326       return 1;
327     }
328 
329     // Open a file.
330     ErrorOr<std::unique_ptr<MemoryBuffer>> MOrErr =
331         MemoryBuffer::getFile(Path, -1, false);
332     fatalOpenError(errorCodeToError(MOrErr.getError()), Path);
333     MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef();
334 
335     // Append a file.
336     appendFile(Members, LibMachine, LibMachineSource, MBRef);
337 
338     // Take the ownership of the file buffer to keep the file open.
339     MBs.push_back(std::move(*MOrErr));
340   }
341 
342   // Create an archive file.
343   std::string OutputPath = getOutputPath(&Args, Members[0]);
344   // llvm-lib uses relative paths for both regular and thin archives, unlike
345   // standard GNU ar, which only uses relative paths for thin archives and
346   // basenames for regular archives.
347   for (NewArchiveMember &Member : Members) {
348     if (sys::path::is_relative(Member.MemberName)) {
349       Expected<std::string> PathOrErr =
350           computeArchiveRelativePath(OutputPath, Member.MemberName);
351       if (PathOrErr)
352         Member.MemberName = Saver.save(*PathOrErr);
353     }
354   }
355 
356   if (Error E =
357           writeArchive(OutputPath, Members,
358                        /*WriteSymtab=*/true, object::Archive::K_GNU,
359                        /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) {
360     handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
361       llvm::errs() << OutputPath << ": " << EI.message() << "\n";
362     });
363     return 1;
364   }
365 
366   return 0;
367 }
368