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
40enum {
41 OPT_INVALID = 0,
42#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
43#include "Options.inc"
44#undef OPTION
45};
46
47using namespace llvm::opt;
48#define OPTTABLE_CODE
49#include "Options.inc"
50
51class LibOptTable : public opt::OptTable {
52public:
53 LibOptTable() : opt::OptTable(optionTables(), true) {}
54};
55} // namespace
56
57static std::string getDefaultOutputPath(const NewArchiveMember &FirstMember) {
58 SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
59 sys::path::replace_extension(path&: Val, extension: ".lib");
60 return std::string(Val);
61}
62
63static std::vector<StringRef> getSearchPaths(opt::InputArgList *Args,
64 StringSaver &Saver) {
65 std::vector<StringRef> Ret;
66 // Add current directory as first item of the search path.
67 Ret.push_back(x: "");
68
69 // Add /libpath flags.
70 for (auto *Arg : Args->filtered(Ids: OPT_libpath))
71 Ret.push_back(x: Arg->getValue());
72
73 // Add $LIB.
74 std::optional<std::string> EnvOpt = sys::Process::GetEnv(name: "LIB");
75 if (!EnvOpt)
76 return Ret;
77 StringRef Env = Saver.save(S: *EnvOpt);
78 while (!Env.empty()) {
79 StringRef Path;
80 std::tie(args&: Path, args&: Env) = Env.split(Separator: ';');
81 Ret.push_back(x: Path);
82 }
83 return Ret;
84}
85
86// Opens a file. Path has to be resolved already. (used for def file)
87std::unique_ptr<MemoryBuffer> openFile(const Twine &Path) {
88 ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MB =
89 MemoryBuffer::getFile(Filename: Path, /*IsText=*/true);
90
91 if (std::error_code EC = MB.getError()) {
92 llvm::errs() << "cannot open file " << Path << ": " << EC.message() << "\n";
93 return nullptr;
94 }
95
96 return std::move(*MB);
97}
98
99static std::string findInputFile(StringRef File, ArrayRef<StringRef> Paths) {
100 for (StringRef Dir : Paths) {
101 SmallString<128> Path = Dir;
102 sys::path::append(path&: Path, a: File);
103 if (sys::fs::exists(Path))
104 return std::string(Path);
105 }
106 return "";
107}
108
109static void fatalOpenError(llvm::Error E, Twine File) {
110 if (!E)
111 return;
112 handleAllErrors(E: std::move(E), Handlers: [&](const llvm::ErrorInfoBase &EIB) {
113 llvm::errs() << "error opening '" << File << "': " << EIB.message() << '\n';
114 exit(status: 1);
115 });
116}
117
118static void doList(opt::InputArgList &Args) {
119 // lib.exe prints the contents of the first archive file.
120 std::unique_ptr<MemoryBuffer> B;
121 for (auto *Arg : Args.filtered(Ids: OPT_INPUT)) {
122 // Create or open the archive object.
123 ErrorOr<std::unique_ptr<MemoryBuffer>> MaybeBuf = MemoryBuffer::getFile(
124 Filename: Arg->getValue(), /*IsText=*/false, /*RequiresNullTerminator=*/false);
125 fatalOpenError(E: errorCodeToError(EC: MaybeBuf.getError()), File: Arg->getValue());
126
127 if (identify_magic(magic: MaybeBuf.get()->getBuffer()) == file_magic::archive) {
128 B = std::move(MaybeBuf.get());
129 break;
130 }
131 }
132
133 // lib.exe doesn't print an error if no .lib files are passed.
134 if (!B)
135 return;
136
137 Error Err = Error::success();
138 object::Archive Archive(B->getMemBufferRef(), Err);
139 fatalOpenError(E: std::move(Err), File: B->getBufferIdentifier());
140
141 std::vector<StringRef> Names;
142 for (auto &C : Archive.children(Err)) {
143 Expected<StringRef> NameOrErr = C.getName();
144 fatalOpenError(E: NameOrErr.takeError(), File: B->getBufferIdentifier());
145 Names.push_back(x: NameOrErr.get());
146 }
147 for (auto Name : reverse(C&: Names))
148 llvm::outs() << Name << '\n';
149 fatalOpenError(E: std::move(Err), File: B->getBufferIdentifier());
150}
151
152static Expected<COFF::MachineTypes> getCOFFFileMachine(MemoryBufferRef MB) {
153 std::error_code EC;
154 auto Obj = object::COFFObjectFile::create(Object: MB);
155 if (!Obj)
156 return Obj.takeError();
157
158 uint16_t Machine = (*Obj)->getMachine();
159 if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
160 Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
161 Machine != COFF::IMAGE_FILE_MACHINE_R4000 &&
162 Machine != COFF::IMAGE_FILE_MACHINE_ARMNT && !COFF::isAnyArm64(Machine)) {
163 return createStringError(EC: inconvertibleErrorCode(),
164 S: "unknown machine: " + std::to_string(val: Machine));
165 }
166
167 return static_cast<COFF::MachineTypes>(Machine);
168}
169
170static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) {
171 Expected<std::string> TripleStr = getBitcodeTargetTriple(Buffer: MB);
172 if (!TripleStr)
173 return TripleStr.takeError();
174
175 Triple T(*TripleStr);
176 switch (T.getArch()) {
177 case Triple::x86:
178 return COFF::IMAGE_FILE_MACHINE_I386;
179 case Triple::x86_64:
180 return COFF::IMAGE_FILE_MACHINE_AMD64;
181 case Triple::arm:
182 case Triple::thumb:
183 return COFF::IMAGE_FILE_MACHINE_ARMNT;
184 case Triple::aarch64:
185 return T.isWindowsArm64EC() ? COFF::IMAGE_FILE_MACHINE_ARM64EC
186 : COFF::IMAGE_FILE_MACHINE_ARM64;
187 case Triple::mipsel:
188 return COFF::IMAGE_FILE_MACHINE_R4000;
189 default:
190 return createStringError(EC: inconvertibleErrorCode(),
191 S: "unknown arch in target triple: " + *TripleStr);
192 }
193}
194
195static bool machineMatches(COFF::MachineTypes LibMachine,
196 COFF::MachineTypes FileMachine) {
197 if (LibMachine == FileMachine)
198 return true;
199 // ARM64EC mode allows both pure ARM64, ARM64EC and X64 objects to be mixed in
200 // the archive.
201 switch (LibMachine) {
202 case COFF::IMAGE_FILE_MACHINE_ARM64:
203 return FileMachine == COFF::IMAGE_FILE_MACHINE_ARM64X;
204 case COFF::IMAGE_FILE_MACHINE_ARM64EC:
205 case COFF::IMAGE_FILE_MACHINE_ARM64X:
206 return COFF::isAnyArm64(Machine: FileMachine) ||
207 FileMachine == COFF::IMAGE_FILE_MACHINE_AMD64;
208 default:
209 return false;
210 }
211}
212
213static void appendFile(std::vector<NewArchiveMember> &Members,
214 COFF::MachineTypes &LibMachine,
215 std::string &LibMachineSource, MemoryBufferRef MB) {
216 file_magic Magic = identify_magic(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(status: 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(E: std::move(Err), File: MB.getBufferIdentifier());
235
236 for (auto &C : Archive.children(Err)) {
237 Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef();
238 if (!ChildMB) {
239 handleAllErrors(E: ChildMB.takeError(), Handlers: [&](const ErrorInfoBase &EIB) {
240 llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message()
241 << "\n";
242 });
243 exit(status: 1);
244 }
245
246 appendFile(Members, LibMachine, LibMachineSource, MB: *ChildMB);
247 }
248
249 fatalOpenError(E: std::move(Err), File: 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(E: MaybeFileMachine.takeError(),
266 Handlers: [&](const ErrorInfoBase &EIB) {
267 llvm::errs() << MB.getBufferIdentifier() << ": "
268 << EIB.message() << "\n";
269 });
270 exit(status: 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(MT: FileMachine)
283 << " conflicts with inferred library machine type,"
284 << " use /machine:arm64ec or /machine:arm64x\n";
285 exit(status: 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(MT: FileMachine)
294 << " conflicts with library machine type "
295 << machineToStr(MT: LibMachine) << LibMachineSource << '\n';
296 exit(status: 1);
297 }
298 }
299 }
300
301 Members.emplace_back(args&: MB);
302}
303
304int 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);
310 cl::ExpandResponseFiles(Saver, Tokenizer: cl::TokenizeWindowsCommandLine, Argv&: NewArgs);
311 ArgsArr = NewArgs;
312
313 LibOptTable Table;
314 unsigned MissingIndex;
315 unsigned MissingCount;
316 opt::InputArgList Args =
317 Table.ParseArgs(Args: ArgsArr.slice(N: 1), MissingArgIndex&: MissingIndex, MissingArgCount&: MissingCount);
318 if (MissingCount) {
319 llvm::errs() << "missing arg value for \""
320 << Args.getArgString(Index: MissingIndex) << "\", expected "
321 << MissingCount
322 << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
323 return 1;
324 }
325 for (auto *Arg : Args.filtered(Ids: OPT_UNKNOWN))
326 llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args)
327 << "\n";
328
329 // Handle /help
330 if (Args.hasArg(Ids: OPT_help)) {
331 Table.printHelp(OS&: outs(), Usage: "llvm-lib [options] file...", Title: "LLVM Lib");
332 return 0;
333 }
334
335 // Parse /ignore:
336 llvm::StringSet<> IgnoredWarnings;
337 for (auto *Arg : Args.filtered(Ids: OPT_ignore))
338 IgnoredWarnings.insert(key: Arg->getValue());
339
340 // get output library path, if any
341 std::string OutputPath;
342 if (auto *Arg = Args.getLastArg(Ids: 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(Ids: OPT_machine)) {
349 LibMachine = getMachineType(S: 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(Ids: 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(Path: Args.getLastArg(Ids: 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: *MB, Machine: LibMachine, /*MingwDef=*/false);
383
384 if (!Def) {
385 llvm::errs() << "error parsing definition\n"
386 << errorToErrorCode(Err: Def.takeError()).message();
387 return 1;
388 }
389
390 std::vector<COFFShortExport> NativeExports;
391 std::string OutputFile = Def->OutputFile;
392
393 if (isArm64EC(Machine: LibMachine) && Args.hasArg(Ids: OPT_nativedeffile)) {
394 std::unique_ptr<MemoryBuffer> NativeMB =
395 openFile(Path: Args.getLastArg(Ids: OPT_nativedeffile)->getValue());
396 if (!NativeMB)
397 return 1;
398
399 if (!NativeMB->getBufferSize()) {
400 llvm::errs() << "native definition file empty\n";
401 return 1;
402 }
403
404 Expected<COFFModuleDefinition> NativeDef =
405 parseCOFFModuleDefinition(MB: *NativeMB, Machine: COFF::IMAGE_FILE_MACHINE_ARM64);
406
407 if (!NativeDef) {
408 llvm::errs() << "error parsing native definition\n"
409 << errorToErrorCode(Err: NativeDef.takeError()).message();
410 return 1;
411 }
412 NativeExports = std::move(NativeDef->Exports);
413 OutputFile = std::move(NativeDef->OutputFile);
414 }
415
416 if (Error E =
417 writeImportLibrary(ImportName: OutputFile, Path: OutputPath, Exports: Def->Exports, Machine: LibMachine,
418 /*MinGW=*/false, NativeExports)) {
419 handleAllErrors(E: std::move(E), Handlers: [&](const ErrorInfoBase &EI) {
420 llvm::errs() << OutputPath << ": " << EI.message() << "\n";
421 });
422 return 1;
423 }
424 return 0;
425 }
426
427 // If no input files and not told otherwise, silently do nothing to match
428 // lib.exe
429 if (!Args.hasArgNoClaim(Ids: OPT_INPUT) && !Args.hasArg(Ids: OPT_llvmlibempty)) {
430 if (!IgnoredWarnings.contains(key: "emptyoutput")) {
431 llvm::errs() << "warning: no input files, not writing output file\n";
432 llvm::errs() << " pass /llvmlibempty to write empty .lib file,\n";
433 llvm::errs() << " pass /ignore:emptyoutput to suppress warning\n";
434 if (Args.hasFlag(Pos: OPT_WX, Neg: OPT_WX_no, Default: false)) {
435 llvm::errs() << "treating warning as error due to /WX\n";
436 return 1;
437 }
438 }
439 return 0;
440 }
441
442 if (Args.hasArg(Ids: OPT_lst)) {
443 doList(Args);
444 return 0;
445 }
446
447 std::vector<StringRef> SearchPaths = getSearchPaths(Args: &Args, Saver);
448
449 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
450 StringSet<> Seen;
451 std::vector<NewArchiveMember> Members;
452
453 // Create a NewArchiveMember for each input file.
454 for (auto *Arg : Args.filtered(Ids: OPT_INPUT)) {
455 // Find a file
456 std::string Path = findInputFile(File: Arg->getValue(), Paths: SearchPaths);
457 if (Path.empty()) {
458 llvm::errs() << Arg->getValue() << ": no such file or directory\n";
459 return 1;
460 }
461
462 // Input files are uniquified by pathname. If you specify the exact same
463 // path more than once, all but the first one are ignored.
464 //
465 // Note that there's a loophole in the rule; you can prepend `.\` or
466 // something like that to a path to make it look different, and they are
467 // handled as if they were different files. This behavior is compatible with
468 // Microsoft lib.exe.
469 if (!Seen.insert(key: Path).second)
470 continue;
471
472 // Open a file.
473 ErrorOr<std::unique_ptr<MemoryBuffer>> MOrErr = MemoryBuffer::getFile(
474 Filename: Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
475 fatalOpenError(E: errorCodeToError(EC: MOrErr.getError()), File: Path);
476 MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef();
477
478 // Append a file.
479 appendFile(Members, LibMachine, LibMachineSource, MB: MBRef);
480
481 // Take the ownership of the file buffer to keep the file open.
482 MBs.push_back(x: std::move(*MOrErr));
483 }
484
485 // Create an archive file.
486 if (OutputPath.empty()) {
487 if (!Members.empty()) {
488 OutputPath = getDefaultOutputPath(FirstMember: Members[0]);
489 } else {
490 llvm::errs() << "no output path given, and cannot infer with no inputs\n";
491 return 1;
492 }
493 }
494
495 bool Thin = Args.hasArg(Ids: OPT_llvmlibthin);
496 if (Thin) {
497 for (NewArchiveMember &Member : Members) {
498 if (sys::path::is_relative(path: Member.MemberName)) {
499 Expected<std::string> PathOrErr =
500 computeArchiveRelativePath(From: OutputPath, To: Member.MemberName);
501 if (PathOrErr)
502 Member.MemberName = Saver.save(S: *PathOrErr);
503 }
504 }
505 }
506
507 // For compatibility with MSVC, reverse member vector after de-duplication.
508 std::reverse(first: Members.begin(), last: Members.end());
509
510 auto Symtab = Args.hasFlag(Pos: OPT_llvmlibindex, Neg: OPT_llvmlibindex_no,
511 /*default=*/Default: true)
512 ? SymtabWritingMode::NormalSymtab
513 : SymtabWritingMode::NoSymtab;
514
515 if (Error E = writeArchive(
516 ArcName: OutputPath, NewMembers: Members, WriteSymtab: Symtab,
517 Kind: Thin ? object::Archive::K_GNU : object::Archive::K_COFF,
518 /*Deterministic=*/true, Thin, OldArchiveBuf: nullptr, IsEC: COFF::isArm64EC(Machine: LibMachine))) {
519 handleAllErrors(E: std::move(E), Handlers: [&](const ErrorInfoBase &EI) {
520 llvm::errs() << OutputPath << ": " << EI.message() << "\n";
521 });
522 return 1;
523 }
524
525 return 0;
526}
527