1 //===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Defines an interface to a lib.exe-compatible driver that also understands
10 // bitcode files. Used by llvm-lib and lld-link /lib.
12 //===----------------------------------------------------------------------===//
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"
36 using namespace llvm::object
;
42 #define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
43 #include "Options.inc"
47 #define PREFIX(NAME, VALUE) \
48 static constexpr StringLiteral NAME##_init[] = VALUE; \
49 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
50 std::size(NAME##_init) - 1);
51 #include "Options.inc"
54 using namespace llvm::opt
;
55 static constexpr opt::OptTable::Info InfoTable
[] = {
56 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
57 #include "Options.inc"
61 class LibOptTable
: public opt::GenericOptTable
{
63 LibOptTable() : opt::GenericOptTable(InfoTable
, true) {}
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
);
73 static std::vector
<StringRef
> getSearchPaths(opt::InputArgList
*Args
,
75 std::vector
<StringRef
> Ret
;
76 // Add current directory as first item of the search path.
79 // Add /libpath flags.
80 for (auto *Arg
: Args
->filtered(OPT_libpath
))
81 Ret
.push_back(Arg
->getValue());
84 std::optional
<std::string
> EnvOpt
= sys::Process::GetEnv("LIB");
87 StringRef Env
= Saver
.save(*EnvOpt
);
88 while (!Env
.empty()) {
90 std::tie(Path
, Env
) = Env
.split(';');
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
);
100 if (std::error_code EC
= MB
.getError()) {
101 llvm::errs() << "cannot open file " << Path
<< ": " << EC
.message() << "\n";
105 return std::move(*MB
);
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
);
118 static void fatalOpenError(llvm::Error E
, Twine File
) {
121 handleAllErrors(std::move(E
), [&](const llvm::ErrorInfoBase
&EIB
) {
122 llvm::errs() << "error opening '" << File
<< "': " << EIB
.message() << '\n';
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());
136 if (identify_magic(MaybeBuf
.get()->getBuffer()) == file_magic::archive
) {
137 B
= std::move(MaybeBuf
.get());
142 // lib.exe doesn't print an error if no .lib files are passed.
146 Error Err
= Error::success();
147 object::Archive
Archive(B
.get()->getMemBufferRef(), Err
);
148 fatalOpenError(std::move(Err
), B
->getBufferIdentifier());
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());
156 for (auto Name
: reverse(Names
))
157 llvm::outs() << Name
<< '\n';
158 fatalOpenError(std::move(Err
), B
->getBufferIdentifier());
161 static Expected
<COFF::MachineTypes
> getCOFFFileMachine(MemoryBufferRef MB
) {
163 auto Obj
= object::COFFObjectFile::create(MB
);
165 return Obj
.takeError();
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
&& !COFF::isAnyArm64(Machine
)) {
171 return createStringError(inconvertibleErrorCode(),
172 "unknown machine: " + std::to_string(Machine
));
175 return static_cast<COFF::MachineTypes
>(Machine
);
178 static Expected
<COFF::MachineTypes
> getBitcodeFileMachine(MemoryBufferRef MB
) {
179 Expected
<std::string
> TripleStr
= getBitcodeTargetTriple(MB
);
181 return TripleStr
.takeError();
183 Triple
T(*TripleStr
);
184 switch (T
.getArch()) {
186 return COFF::IMAGE_FILE_MACHINE_I386
;
188 return COFF::IMAGE_FILE_MACHINE_AMD64
;
190 return COFF::IMAGE_FILE_MACHINE_ARMNT
;
191 case Triple::aarch64
:
192 return T
.isWindowsArm64EC() ? COFF::IMAGE_FILE_MACHINE_ARM64EC
193 : COFF::IMAGE_FILE_MACHINE_ARM64
;
195 return createStringError(inconvertibleErrorCode(),
196 "unknown arch in target triple: " + *TripleStr
);
200 static bool machineMatches(COFF::MachineTypes LibMachine
,
201 COFF::MachineTypes FileMachine
) {
202 if (LibMachine
== FileMachine
)
204 // ARM64EC mode allows both pure ARM64, ARM64EC and X64 objects to be mixed in
206 switch (LibMachine
) {
207 case COFF::IMAGE_FILE_MACHINE_ARM64
:
208 return FileMachine
== COFF::IMAGE_FILE_MACHINE_ARM64X
;
209 case COFF::IMAGE_FILE_MACHINE_ARM64EC
:
210 case COFF::IMAGE_FILE_MACHINE_ARM64X
:
211 return COFF::isAnyArm64(FileMachine
) ||
212 FileMachine
== COFF::IMAGE_FILE_MACHINE_AMD64
;
218 static void appendFile(std::vector
<NewArchiveMember
> &Members
,
219 COFF::MachineTypes
&LibMachine
,
220 std::string
&LibMachineSource
, MemoryBufferRef MB
) {
221 file_magic Magic
= identify_magic(MB
.getBuffer());
223 if (Magic
!= file_magic::coff_object
&& Magic
!= file_magic::bitcode
&&
224 Magic
!= file_magic::archive
&& Magic
!= file_magic::windows_resource
&&
225 Magic
!= file_magic::coff_import_library
) {
226 llvm::errs() << MB
.getBufferIdentifier()
227 << ": not a COFF object, bitcode, archive, import library or "
232 // If a user attempts to add an archive to another archive, llvm-lib doesn't
233 // handle the first archive file as a single file. Instead, it extracts all
234 // members from the archive and add them to the second archive. This behavior
235 // is for compatibility with Microsoft's lib command.
236 if (Magic
== file_magic::archive
) {
237 Error Err
= Error::success();
238 object::Archive
Archive(MB
, Err
);
239 fatalOpenError(std::move(Err
), MB
.getBufferIdentifier());
241 for (auto &C
: Archive
.children(Err
)) {
242 Expected
<MemoryBufferRef
> ChildMB
= C
.getMemoryBufferRef();
244 handleAllErrors(ChildMB
.takeError(), [&](const ErrorInfoBase
&EIB
) {
245 llvm::errs() << MB
.getBufferIdentifier() << ": " << EIB
.message()
251 appendFile(Members
, LibMachine
, LibMachineSource
, *ChildMB
);
254 fatalOpenError(std::move(Err
), MB
.getBufferIdentifier());
258 // Check that all input files have the same machine type.
259 // Mixing normal objects and LTO bitcode files is fine as long as they
260 // have the same machine type.
261 // Doing this here duplicates the header parsing work that writeArchive()
262 // below does, but it's not a lot of work and it's a bit awkward to do
263 // in writeArchive() which needs to support many tools, can't assume the
264 // input is COFF, and doesn't have a good way to report errors.
265 if (Magic
== file_magic::coff_object
|| Magic
== file_magic::bitcode
) {
266 Expected
<COFF::MachineTypes
> MaybeFileMachine
=
267 (Magic
== file_magic::coff_object
) ? getCOFFFileMachine(MB
)
268 : getBitcodeFileMachine(MB
);
269 if (!MaybeFileMachine
) {
270 handleAllErrors(MaybeFileMachine
.takeError(),
271 [&](const ErrorInfoBase
&EIB
) {
272 llvm::errs() << MB
.getBufferIdentifier() << ": "
273 << EIB
.message() << "\n";
277 COFF::MachineTypes FileMachine
= *MaybeFileMachine
;
279 // FIXME: Once lld-link rejects multiple resource .obj files:
280 // Call convertResToCOFF() on .res files and add the resulting
281 // COFF file to the .lib output instead of adding the .res file, and remove
282 // this check. See PR42180.
283 if (FileMachine
!= COFF::IMAGE_FILE_MACHINE_UNKNOWN
) {
284 if (LibMachine
== COFF::IMAGE_FILE_MACHINE_UNKNOWN
) {
285 if (FileMachine
== COFF::IMAGE_FILE_MACHINE_ARM64EC
) {
286 llvm::errs() << MB
.getBufferIdentifier() << ": file machine type "
287 << machineToStr(FileMachine
)
288 << " conflicts with inferred library machine type,"
289 << " use /machine:arm64ec or /machine:arm64x\n";
292 LibMachine
= FileMachine
;
294 (" (inferred from earlier file '" + MB
.getBufferIdentifier() + "')")
296 } else if (!machineMatches(LibMachine
, FileMachine
)) {
297 llvm::errs() << MB
.getBufferIdentifier() << ": file machine type "
298 << machineToStr(FileMachine
)
299 << " conflicts with library machine type "
300 << machineToStr(LibMachine
) << LibMachineSource
<< '\n';
306 Members
.emplace_back(MB
);
309 int llvm::libDriverMain(ArrayRef
<const char *> ArgsArr
) {
310 BumpPtrAllocator Alloc
;
311 StringSaver
Saver(Alloc
);
313 // Parse command line arguments.
314 SmallVector
<const char *, 20> NewArgs(ArgsArr
.begin(), ArgsArr
.end());
315 cl::ExpandResponseFiles(Saver
, cl::TokenizeWindowsCommandLine
, NewArgs
);
319 unsigned MissingIndex
;
320 unsigned MissingCount
;
321 opt::InputArgList Args
=
322 Table
.ParseArgs(ArgsArr
.slice(1), MissingIndex
, MissingCount
);
324 llvm::errs() << "missing arg value for \""
325 << Args
.getArgString(MissingIndex
) << "\", expected "
327 << (MissingCount
== 1 ? " argument.\n" : " arguments.\n");
330 for (auto *Arg
: Args
.filtered(OPT_UNKNOWN
))
331 llvm::errs() << "ignoring unknown argument: " << Arg
->getAsString(Args
)
335 if (Args
.hasArg(OPT_help
)) {
336 Table
.printHelp(outs(), "llvm-lib [options] file...", "LLVM Lib");
341 llvm::StringSet
<> IgnoredWarnings
;
342 for (auto *Arg
: Args
.filtered(OPT_ignore
))
343 IgnoredWarnings
.insert(Arg
->getValue());
345 // get output library path, if any
346 std::string OutputPath
;
347 if (auto *Arg
= Args
.getLastArg(OPT_out
)) {
348 OutputPath
= Arg
->getValue();
351 COFF::MachineTypes LibMachine
= COFF::IMAGE_FILE_MACHINE_UNKNOWN
;
352 std::string LibMachineSource
;
353 if (auto *Arg
= Args
.getLastArg(OPT_machine
)) {
354 LibMachine
= getMachineType(Arg
->getValue());
355 if (LibMachine
== COFF::IMAGE_FILE_MACHINE_UNKNOWN
) {
356 llvm::errs() << "unknown /machine: arg " << Arg
->getValue() << '\n';
360 std::string(" (from '/machine:") + Arg
->getValue() + "' flag)";
363 // create an import library
364 if (Args
.hasArg(OPT_deffile
)) {
366 if (OutputPath
.empty()) {
367 llvm::errs() << "no output path given\n";
371 if (LibMachine
== COFF::IMAGE_FILE_MACHINE_UNKNOWN
) {
372 llvm::errs() << "/def option requires /machine to be specified" << '\n';
376 std::unique_ptr
<MemoryBuffer
> MB
=
377 openFile(Args
.getLastArg(OPT_deffile
)->getValue());
381 if (!MB
->getBufferSize()) {
382 llvm::errs() << "definition file empty\n";
386 Expected
<COFFModuleDefinition
> Def
=
387 parseCOFFModuleDefinition(*MB
, LibMachine
, /*MingwDef=*/false);
390 llvm::errs() << "error parsing definition\n"
391 << errorToErrorCode(Def
.takeError()).message();
395 return writeImportLibrary(Def
->OutputFile
, OutputPath
, Def
->Exports
,
402 // If no input files and not told otherwise, silently do nothing to match
404 if (!Args
.hasArgNoClaim(OPT_INPUT
) && !Args
.hasArg(OPT_llvmlibempty
)) {
405 if (!IgnoredWarnings
.contains("emptyoutput")) {
406 llvm::errs() << "warning: no input files, not writing output file\n";
407 llvm::errs() << " pass /llvmlibempty to write empty .lib file,\n";
408 llvm::errs() << " pass /ignore:emptyoutput to suppress warning\n";
409 if (Args
.hasFlag(OPT_WX
, OPT_WX_no
, false)) {
410 llvm::errs() << "treating warning as error due to /WX\n";
417 if (Args
.hasArg(OPT_lst
)) {
422 std::vector
<StringRef
> SearchPaths
= getSearchPaths(&Args
, Saver
);
424 std::vector
<std::unique_ptr
<MemoryBuffer
>> MBs
;
426 std::vector
<NewArchiveMember
> Members
;
428 // Create a NewArchiveMember for each input file.
429 for (auto *Arg
: Args
.filtered(OPT_INPUT
)) {
431 std::string Path
= findInputFile(Arg
->getValue(), SearchPaths
);
433 llvm::errs() << Arg
->getValue() << ": no such file or directory\n";
437 // Input files are uniquified by pathname. If you specify the exact same
438 // path more than once, all but the first one are ignored.
440 // Note that there's a loophole in the rule; you can prepend `.\` or
441 // something like that to a path to make it look different, and they are
442 // handled as if they were different files. This behavior is compatible with
443 // Microsoft lib.exe.
444 if (!Seen
.insert(Path
).second
)
448 ErrorOr
<std::unique_ptr
<MemoryBuffer
>> MOrErr
= MemoryBuffer::getFile(
449 Path
, /*IsText=*/false, /*RequiresNullTerminator=*/false);
450 fatalOpenError(errorCodeToError(MOrErr
.getError()), Path
);
451 MemoryBufferRef MBRef
= (*MOrErr
)->getMemBufferRef();
454 appendFile(Members
, LibMachine
, LibMachineSource
, MBRef
);
456 // Take the ownership of the file buffer to keep the file open.
457 MBs
.push_back(std::move(*MOrErr
));
460 // Create an archive file.
461 if (OutputPath
.empty()) {
462 if (!Members
.empty()) {
463 OutputPath
= getDefaultOutputPath(Members
[0]);
465 llvm::errs() << "no output path given, and cannot infer with no inputs\n";
469 // llvm-lib uses relative paths for both regular and thin archives, unlike
470 // standard GNU ar, which only uses relative paths for thin archives and
471 // basenames for regular archives.
472 for (NewArchiveMember
&Member
: Members
) {
473 if (sys::path::is_relative(Member
.MemberName
)) {
474 Expected
<std::string
> PathOrErr
=
475 computeArchiveRelativePath(OutputPath
, Member
.MemberName
);
477 Member
.MemberName
= Saver
.save(*PathOrErr
);
481 // For compatibility with MSVC, reverse member vector after de-duplication.
482 std::reverse(Members
.begin(), Members
.end());
484 bool Thin
= Args
.hasArg(OPT_llvmlibthin
);
485 if (Error E
= writeArchive(
486 OutputPath
, Members
, SymtabWritingMode::NormalSymtab
,
487 Thin
? object::Archive::K_GNU
: object::Archive::K_COFF
,
488 /*Deterministic=*/true, Thin
, nullptr, COFF::isArm64EC(LibMachine
))) {
489 handleAllErrors(std::move(E
), [&](const ErrorInfoBase
&EI
) {
490 llvm::errs() << OutputPath
<< ": " << EI
.message() << "\n";