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/WindowsMachineFlag.h" 23 #include "llvm/Option/Arg.h" 24 #include "llvm/Option/ArgList.h" 25 #include "llvm/Option/Option.h" 26 #include "llvm/Support/CommandLine.h" 27 #include "llvm/Support/Path.h" 28 #include "llvm/Support/Process.h" 29 #include "llvm/Support/StringSaver.h" 30 #include "llvm/Support/raw_ostream.h" 31 #include <optional> 32 33 using namespace llvm; 34 35 namespace { 36 37 enum { 38 OPT_INVALID = 0, 39 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID, 40 #include "Options.inc" 41 #undef OPTION 42 }; 43 44 #define PREFIX(NAME, VALUE) \ 45 static constexpr StringLiteral NAME##_init[] = VALUE; \ 46 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \ 47 std::size(NAME##_init) - 1); 48 #include "Options.inc" 49 #undef PREFIX 50 51 static constexpr opt::OptTable::Info InfoTable[] = { 52 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ 53 {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \ 54 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, 55 #include "Options.inc" 56 #undef OPTION 57 }; 58 59 class LibOptTable : public opt::GenericOptTable { 60 public: 61 LibOptTable() : opt::GenericOptTable(InfoTable, true) {} 62 }; 63 } 64 65 static std::string getDefaultOutputPath(const NewArchiveMember &FirstMember) { 66 SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier()); 67 sys::path::replace_extension(Val, ".lib"); 68 return std::string(Val.str()); 69 } 70 71 static std::vector<StringRef> getSearchPaths(opt::InputArgList *Args, 72 StringSaver &Saver) { 73 std::vector<StringRef> Ret; 74 // Add current directory as first item of the search path. 75 Ret.push_back(""); 76 77 // Add /libpath flags. 78 for (auto *Arg : Args->filtered(OPT_libpath)) 79 Ret.push_back(Arg->getValue()); 80 81 // Add $LIB. 82 std::optional<std::string> EnvOpt = sys::Process::GetEnv("LIB"); 83 if (!EnvOpt) 84 return Ret; 85 StringRef Env = Saver.save(*EnvOpt); 86 while (!Env.empty()) { 87 StringRef Path; 88 std::tie(Path, Env) = Env.split(';'); 89 Ret.push_back(Path); 90 } 91 return Ret; 92 } 93 94 static std::string findInputFile(StringRef File, ArrayRef<StringRef> Paths) { 95 for (StringRef Dir : Paths) { 96 SmallString<128> Path = Dir; 97 sys::path::append(Path, File); 98 if (sys::fs::exists(Path)) 99 return std::string(Path); 100 } 101 return ""; 102 } 103 104 static void fatalOpenError(llvm::Error E, Twine File) { 105 if (!E) 106 return; 107 handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) { 108 llvm::errs() << "error opening '" << File << "': " << EIB.message() << '\n'; 109 exit(1); 110 }); 111 } 112 113 static void doList(opt::InputArgList& Args) { 114 // lib.exe prints the contents of the first archive file. 115 std::unique_ptr<MemoryBuffer> B; 116 for (auto *Arg : Args.filtered(OPT_INPUT)) { 117 // Create or open the archive object. 118 ErrorOr<std::unique_ptr<MemoryBuffer>> MaybeBuf = MemoryBuffer::getFile( 119 Arg->getValue(), /*IsText=*/false, /*RequiresNullTerminator=*/false); 120 fatalOpenError(errorCodeToError(MaybeBuf.getError()), Arg->getValue()); 121 122 if (identify_magic(MaybeBuf.get()->getBuffer()) == file_magic::archive) { 123 B = std::move(MaybeBuf.get()); 124 break; 125 } 126 } 127 128 // lib.exe doesn't print an error if no .lib files are passed. 129 if (!B) 130 return; 131 132 Error Err = Error::success(); 133 object::Archive Archive(B.get()->getMemBufferRef(), Err); 134 fatalOpenError(std::move(Err), B->getBufferIdentifier()); 135 136 std::vector<StringRef> Names; 137 for (auto &C : Archive.children(Err)) { 138 Expected<StringRef> NameOrErr = C.getName(); 139 fatalOpenError(NameOrErr.takeError(), B->getBufferIdentifier()); 140 Names.push_back(NameOrErr.get()); 141 } 142 for (auto Name : reverse(Names)) 143 llvm::outs() << Name << '\n'; 144 fatalOpenError(std::move(Err), B->getBufferIdentifier()); 145 } 146 147 static Expected<COFF::MachineTypes> getCOFFFileMachine(MemoryBufferRef MB) { 148 std::error_code EC; 149 auto Obj = object::COFFObjectFile::create(MB); 150 if (!Obj) 151 return Obj.takeError(); 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 return createStringError(inconvertibleErrorCode(), 159 "unknown machine: " + std::to_string(Machine)); 160 } 161 162 return static_cast<COFF::MachineTypes>(Machine); 163 } 164 165 static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) { 166 Expected<std::string> TripleStr = getBitcodeTargetTriple(MB); 167 if (!TripleStr) 168 return TripleStr.takeError(); 169 170 switch (Triple(*TripleStr).getArch()) { 171 case Triple::x86: 172 return COFF::IMAGE_FILE_MACHINE_I386; 173 case Triple::x86_64: 174 return COFF::IMAGE_FILE_MACHINE_AMD64; 175 case Triple::arm: 176 return COFF::IMAGE_FILE_MACHINE_ARMNT; 177 case Triple::aarch64: 178 return COFF::IMAGE_FILE_MACHINE_ARM64; 179 default: 180 return createStringError(inconvertibleErrorCode(), 181 "unknown arch in target triple: " + *TripleStr); 182 } 183 } 184 185 static void appendFile(std::vector<NewArchiveMember> &Members, 186 COFF::MachineTypes &LibMachine, 187 std::string &LibMachineSource, MemoryBufferRef MB) { 188 file_magic Magic = identify_magic(MB.getBuffer()); 189 190 if (Magic != file_magic::coff_object && Magic != file_magic::bitcode && 191 Magic != file_magic::archive && Magic != file_magic::windows_resource && 192 Magic != file_magic::coff_import_library) { 193 llvm::errs() << MB.getBufferIdentifier() 194 << ": not a COFF object, bitcode, archive, import library or " 195 "resource file\n"; 196 exit(1); 197 } 198 199 // If a user attempts to add an archive to another archive, llvm-lib doesn't 200 // handle the first archive file as a single file. Instead, it extracts all 201 // members from the archive and add them to the second archive. This behavior 202 // is for compatibility with Microsoft's lib command. 203 if (Magic == file_magic::archive) { 204 Error Err = Error::success(); 205 object::Archive Archive(MB, Err); 206 fatalOpenError(std::move(Err), MB.getBufferIdentifier()); 207 208 for (auto &C : Archive.children(Err)) { 209 Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef(); 210 if (!ChildMB) { 211 handleAllErrors(ChildMB.takeError(), [&](const ErrorInfoBase &EIB) { 212 llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message() 213 << "\n"; 214 }); 215 exit(1); 216 } 217 218 appendFile(Members, LibMachine, LibMachineSource, *ChildMB); 219 } 220 221 fatalOpenError(std::move(Err), MB.getBufferIdentifier()); 222 return; 223 } 224 225 // Check that all input files have the same machine type. 226 // Mixing normal objects and LTO bitcode files is fine as long as they 227 // have the same machine type. 228 // Doing this here duplicates the header parsing work that writeArchive() 229 // below does, but it's not a lot of work and it's a bit awkward to do 230 // in writeArchive() which needs to support many tools, can't assume the 231 // input is COFF, and doesn't have a good way to report errors. 232 if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) { 233 Expected<COFF::MachineTypes> MaybeFileMachine = 234 (Magic == file_magic::coff_object) ? getCOFFFileMachine(MB) 235 : getBitcodeFileMachine(MB); 236 if (!MaybeFileMachine) { 237 handleAllErrors(MaybeFileMachine.takeError(), 238 [&](const ErrorInfoBase &EIB) { 239 llvm::errs() << MB.getBufferIdentifier() << ": " 240 << EIB.message() << "\n"; 241 }); 242 exit(1); 243 } 244 COFF::MachineTypes FileMachine = *MaybeFileMachine; 245 246 // FIXME: Once lld-link rejects multiple resource .obj files: 247 // Call convertResToCOFF() on .res files and add the resulting 248 // COFF file to the .lib output instead of adding the .res file, and remove 249 // this check. See PR42180. 250 if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) { 251 if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) { 252 LibMachine = FileMachine; 253 LibMachineSource = 254 (" (inferred from earlier file '" + MB.getBufferIdentifier() + "')") 255 .str(); 256 } else if (LibMachine != FileMachine) { 257 llvm::errs() << MB.getBufferIdentifier() << ": file machine type " 258 << machineToStr(FileMachine) 259 << " conflicts with library machine type " 260 << machineToStr(LibMachine) << LibMachineSource << '\n'; 261 exit(1); 262 } 263 } 264 } 265 266 Members.emplace_back(MB); 267 } 268 269 int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) { 270 BumpPtrAllocator Alloc; 271 StringSaver Saver(Alloc); 272 273 // Parse command line arguments. 274 SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end()); 275 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs); 276 ArgsArr = NewArgs; 277 278 LibOptTable Table; 279 unsigned MissingIndex; 280 unsigned MissingCount; 281 opt::InputArgList Args = 282 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount); 283 if (MissingCount) { 284 llvm::errs() << "missing arg value for \"" 285 << Args.getArgString(MissingIndex) << "\", expected " 286 << MissingCount 287 << (MissingCount == 1 ? " argument.\n" : " arguments.\n"); 288 return 1; 289 } 290 for (auto *Arg : Args.filtered(OPT_UNKNOWN)) 291 llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args) 292 << "\n"; 293 294 // Handle /help 295 if (Args.hasArg(OPT_help)) { 296 Table.printHelp(outs(), "llvm-lib [options] file...", "LLVM Lib"); 297 return 0; 298 } 299 300 // Parse /ignore: 301 llvm::StringSet<> IgnoredWarnings; 302 for (auto *Arg : Args.filtered(OPT_ignore)) 303 IgnoredWarnings.insert(Arg->getValue()); 304 305 // If no input files and not told otherwise, silently do nothing to match 306 // lib.exe 307 if (!Args.hasArgNoClaim(OPT_INPUT) && !Args.hasArg(OPT_llvmlibempty)) { 308 if (!IgnoredWarnings.contains("emptyoutput")) { 309 llvm::errs() << "warning: no input files, not writing output file\n"; 310 llvm::errs() << " pass /llvmlibempty to write empty .lib file,\n"; 311 llvm::errs() << " pass /ignore:emptyoutput to suppress warning\n"; 312 if (Args.hasFlag(OPT_WX, OPT_WX_no, false)) { 313 llvm::errs() << "treating warning as error due to /WX\n"; 314 return 1; 315 } 316 } 317 return 0; 318 } 319 320 if (Args.hasArg(OPT_lst)) { 321 doList(Args); 322 return 0; 323 } 324 325 std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver); 326 327 COFF::MachineTypes LibMachine = COFF::IMAGE_FILE_MACHINE_UNKNOWN; 328 std::string LibMachineSource; 329 if (auto *Arg = Args.getLastArg(OPT_machine)) { 330 LibMachine = getMachineType(Arg->getValue()); 331 if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) { 332 llvm::errs() << "unknown /machine: arg " << Arg->getValue() << '\n'; 333 return 1; 334 } 335 LibMachineSource = 336 std::string(" (from '/machine:") + Arg->getValue() + "' flag)"; 337 } 338 339 std::vector<std::unique_ptr<MemoryBuffer>> MBs; 340 StringSet<> Seen; 341 std::vector<NewArchiveMember> Members; 342 343 // Create a NewArchiveMember for each input file. 344 for (auto *Arg : Args.filtered(OPT_INPUT)) { 345 // Find a file 346 std::string Path = findInputFile(Arg->getValue(), SearchPaths); 347 if (Path.empty()) { 348 llvm::errs() << Arg->getValue() << ": no such file or directory\n"; 349 return 1; 350 } 351 352 // Input files are uniquified by pathname. If you specify the exact same 353 // path more than once, all but the first one are ignored. 354 // 355 // Note that there's a loophole in the rule; you can prepend `.\` or 356 // something like that to a path to make it look different, and they are 357 // handled as if they were different files. This behavior is compatible with 358 // Microsoft lib.exe. 359 if (!Seen.insert(Path).second) 360 continue; 361 362 // Open a file. 363 ErrorOr<std::unique_ptr<MemoryBuffer>> MOrErr = MemoryBuffer::getFile( 364 Path, /*IsText=*/false, /*RequiresNullTerminator=*/false); 365 fatalOpenError(errorCodeToError(MOrErr.getError()), Path); 366 MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef(); 367 368 // Append a file. 369 appendFile(Members, LibMachine, LibMachineSource, MBRef); 370 371 // Take the ownership of the file buffer to keep the file open. 372 MBs.push_back(std::move(*MOrErr)); 373 } 374 375 // Create an archive file. 376 std::string OutputPath; 377 if (auto *Arg = Args.getLastArg(OPT_out)) { 378 OutputPath = Arg->getValue(); 379 } else if (!Members.empty()) { 380 OutputPath = getDefaultOutputPath(Members[0]); 381 } else { 382 llvm::errs() << "no output path given, and cannot infer with no inputs\n"; 383 return 1; 384 } 385 // llvm-lib uses relative paths for both regular and thin archives, unlike 386 // standard GNU ar, which only uses relative paths for thin archives and 387 // basenames for regular archives. 388 for (NewArchiveMember &Member : Members) { 389 if (sys::path::is_relative(Member.MemberName)) { 390 Expected<std::string> PathOrErr = 391 computeArchiveRelativePath(OutputPath, Member.MemberName); 392 if (PathOrErr) 393 Member.MemberName = Saver.save(*PathOrErr); 394 } 395 } 396 397 // For compatibility with MSVC, reverse member vector after de-duplication. 398 std::reverse(Members.begin(), Members.end()); 399 400 if (Error E = 401 writeArchive(OutputPath, Members, 402 /*WriteSymtab=*/true, object::Archive::K_GNU, 403 /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) { 404 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) { 405 llvm::errs() << OutputPath << ": " << EI.message() << "\n"; 406 }); 407 return 1; 408 } 409 410 return 0; 411 } 412