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 && !COFF::isAnyArm64(Machine)) { 171 return createStringError(inconvertibleErrorCode(), 172 "unknown machine: " + std::to_string(Machine)); 173 } 174 175 return static_cast<COFF::MachineTypes>(Machine); 176 } 177 178 static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) { 179 Expected<std::string> TripleStr = getBitcodeTargetTriple(MB); 180 if (!TripleStr) 181 return TripleStr.takeError(); 182 183 Triple T(*TripleStr); 184 switch (T.getArch()) { 185 case Triple::x86: 186 return COFF::IMAGE_FILE_MACHINE_I386; 187 case Triple::x86_64: 188 return COFF::IMAGE_FILE_MACHINE_AMD64; 189 case Triple::arm: 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; 194 default: 195 return createStringError(inconvertibleErrorCode(), 196 "unknown arch in target triple: " + *TripleStr); 197 } 198 } 199 200 static bool machineMatches(COFF::MachineTypes LibMachine, 201 COFF::MachineTypes FileMachine) { 202 if (LibMachine == FileMachine) 203 return true; 204 // ARM64EC mode allows both pure ARM64, ARM64EC and X64 objects to be mixed in 205 // the archive. 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; 213 default: 214 return false; 215 } 216 } 217 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()); 222 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 " 228 "resource file\n"; 229 exit(1); 230 } 231 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()); 240 241 for (auto &C : Archive.children(Err)) { 242 Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef(); 243 if (!ChildMB) { 244 handleAllErrors(ChildMB.takeError(), [&](const ErrorInfoBase &EIB) { 245 llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message() 246 << "\n"; 247 }); 248 exit(1); 249 } 250 251 appendFile(Members, LibMachine, LibMachineSource, *ChildMB); 252 } 253 254 fatalOpenError(std::move(Err), MB.getBufferIdentifier()); 255 return; 256 } 257 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"; 274 }); 275 exit(1); 276 } 277 COFF::MachineTypes FileMachine = *MaybeFileMachine; 278 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"; 290 exit(1); 291 } 292 LibMachine = FileMachine; 293 LibMachineSource = 294 (" (inferred from earlier file '" + MB.getBufferIdentifier() + "')") 295 .str(); 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'; 301 exit(1); 302 } 303 } 304 } 305 306 Members.emplace_back(MB); 307 } 308 309 int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) { 310 BumpPtrAllocator Alloc; 311 StringSaver Saver(Alloc); 312 313 // Parse command line arguments. 314 SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end()); 315 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs); 316 ArgsArr = NewArgs; 317 318 LibOptTable Table; 319 unsigned MissingIndex; 320 unsigned MissingCount; 321 opt::InputArgList Args = 322 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount); 323 if (MissingCount) { 324 llvm::errs() << "missing arg value for \"" 325 << Args.getArgString(MissingIndex) << "\", expected " 326 << MissingCount 327 << (MissingCount == 1 ? " argument.\n" : " arguments.\n"); 328 return 1; 329 } 330 for (auto *Arg : Args.filtered(OPT_UNKNOWN)) 331 llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args) 332 << "\n"; 333 334 // Handle /help 335 if (Args.hasArg(OPT_help)) { 336 Table.printHelp(outs(), "llvm-lib [options] file...", "LLVM Lib"); 337 return 0; 338 } 339 340 // Parse /ignore: 341 llvm::StringSet<> IgnoredWarnings; 342 for (auto *Arg : Args.filtered(OPT_ignore)) 343 IgnoredWarnings.insert(Arg->getValue()); 344 345 // get output library path, if any 346 std::string OutputPath; 347 if (auto *Arg = Args.getLastArg(OPT_out)) { 348 OutputPath = Arg->getValue(); 349 } 350 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'; 357 return 1; 358 } 359 LibMachineSource = 360 std::string(" (from '/machine:") + Arg->getValue() + "' flag)"; 361 } 362 363 // create an import library 364 if (Args.hasArg(OPT_deffile)) { 365 366 if (OutputPath.empty()) { 367 llvm::errs() << "no output path given\n"; 368 return 1; 369 } 370 371 if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) { 372 llvm::errs() << "/def option requires /machine to be specified" << '\n'; 373 return 1; 374 } 375 376 std::unique_ptr<MemoryBuffer> MB = 377 openFile(Args.getLastArg(OPT_deffile)->getValue()); 378 if (!MB) 379 return 1; 380 381 if (!MB->getBufferSize()) { 382 llvm::errs() << "definition file empty\n"; 383 return 1; 384 } 385 386 Expected<COFFModuleDefinition> Def = 387 parseCOFFModuleDefinition(*MB, LibMachine, /*MingwDef=*/false); 388 389 if (!Def) { 390 llvm::errs() << "error parsing definition\n" 391 << errorToErrorCode(Def.takeError()).message(); 392 return 1; 393 } 394 395 return writeImportLibrary(Def->OutputFile, OutputPath, Def->Exports, 396 LibMachine, 397 /*MinGW=*/false) 398 ? 1 399 : 0; 400 } 401 402 // If no input files and not told otherwise, silently do nothing to match 403 // lib.exe 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"; 411 return 1; 412 } 413 } 414 return 0; 415 } 416 417 if (Args.hasArg(OPT_lst)) { 418 doList(Args); 419 return 0; 420 } 421 422 std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver); 423 424 std::vector<std::unique_ptr<MemoryBuffer>> MBs; 425 StringSet<> Seen; 426 std::vector<NewArchiveMember> Members; 427 428 // Create a NewArchiveMember for each input file. 429 for (auto *Arg : Args.filtered(OPT_INPUT)) { 430 // Find a file 431 std::string Path = findInputFile(Arg->getValue(), SearchPaths); 432 if (Path.empty()) { 433 llvm::errs() << Arg->getValue() << ": no such file or directory\n"; 434 return 1; 435 } 436 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. 439 // 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) 445 continue; 446 447 // Open a file. 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(); 452 453 // Append a file. 454 appendFile(Members, LibMachine, LibMachineSource, MBRef); 455 456 // Take the ownership of the file buffer to keep the file open. 457 MBs.push_back(std::move(*MOrErr)); 458 } 459 460 // Create an archive file. 461 if (OutputPath.empty()) { 462 if (!Members.empty()) { 463 OutputPath = getDefaultOutputPath(Members[0]); 464 } else { 465 llvm::errs() << "no output path given, and cannot infer with no inputs\n"; 466 return 1; 467 } 468 } 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); 476 if (PathOrErr) 477 Member.MemberName = Saver.save(*PathOrErr); 478 } 479 } 480 481 // For compatibility with MSVC, reverse member vector after de-duplication. 482 std::reverse(Members.begin(), Members.end()); 483 484 bool Thin = Args.hasArg(OPT_llvmlibthin); 485 if (Error E = 486 writeArchive(OutputPath, Members, 487 /*WriteSymtab=*/true, 488 Thin ? object::Archive::K_GNU : object::Archive::K_COFF, 489 /*Deterministic*/ true, Thin, nullptr, 490 COFF::isArm64EC(LibMachine))) { 491 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) { 492 llvm::errs() << OutputPath << ": " << EI.message() << "\n"; 493 }); 494 return 1; 495 } 496 497 return 0; 498 } 499