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