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 for (auto &C : Archive.children(Err)) { 137 Expected<StringRef> NameOrErr = C.getName(); 138 fatalOpenError(NameOrErr.takeError(), B->getBufferIdentifier()); 139 StringRef Name = NameOrErr.get(); 140 llvm::outs() << Name << '\n'; 141 } 142 fatalOpenError(std::move(Err), B->getBufferIdentifier()); 143 } 144 145 static Expected<COFF::MachineTypes> getCOFFFileMachine(MemoryBufferRef MB) { 146 std::error_code EC; 147 auto Obj = object::COFFObjectFile::create(MB); 148 if (!Obj) 149 return Obj.takeError(); 150 151 uint16_t Machine = (*Obj)->getMachine(); 152 if (Machine != COFF::IMAGE_FILE_MACHINE_I386 && 153 Machine != COFF::IMAGE_FILE_MACHINE_AMD64 && 154 Machine != COFF::IMAGE_FILE_MACHINE_ARMNT && 155 Machine != COFF::IMAGE_FILE_MACHINE_ARM64) { 156 return createStringError(inconvertibleErrorCode(), 157 "unknown machine: " + std::to_string(Machine)); 158 } 159 160 return static_cast<COFF::MachineTypes>(Machine); 161 } 162 163 static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) { 164 Expected<std::string> TripleStr = getBitcodeTargetTriple(MB); 165 if (!TripleStr) 166 return TripleStr.takeError(); 167 168 switch (Triple(*TripleStr).getArch()) { 169 case Triple::x86: 170 return COFF::IMAGE_FILE_MACHINE_I386; 171 case Triple::x86_64: 172 return COFF::IMAGE_FILE_MACHINE_AMD64; 173 case Triple::arm: 174 return COFF::IMAGE_FILE_MACHINE_ARMNT; 175 case Triple::aarch64: 176 return COFF::IMAGE_FILE_MACHINE_ARM64; 177 default: 178 return createStringError(inconvertibleErrorCode(), 179 "unknown arch in target triple: " + *TripleStr); 180 } 181 } 182 183 static void appendFile(std::vector<NewArchiveMember> &Members, 184 COFF::MachineTypes &LibMachine, 185 std::string &LibMachineSource, MemoryBufferRef MB) { 186 file_magic Magic = identify_magic(MB.getBuffer()); 187 188 if (Magic != file_magic::coff_object && Magic != file_magic::bitcode && 189 Magic != file_magic::archive && Magic != file_magic::windows_resource && 190 Magic != file_magic::coff_import_library) { 191 llvm::errs() << MB.getBufferIdentifier() 192 << ": not a COFF object, bitcode, archive, import library or " 193 "resource file\n"; 194 exit(1); 195 } 196 197 // If a user attempts to add an archive to another archive, llvm-lib doesn't 198 // handle the first archive file as a single file. Instead, it extracts all 199 // members from the archive and add them to the second archive. This behavior 200 // is for compatibility with Microsoft's lib command. 201 if (Magic == file_magic::archive) { 202 Error Err = Error::success(); 203 object::Archive Archive(MB, Err); 204 fatalOpenError(std::move(Err), MB.getBufferIdentifier()); 205 206 for (auto &C : Archive.children(Err)) { 207 Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef(); 208 if (!ChildMB) { 209 handleAllErrors(ChildMB.takeError(), [&](const ErrorInfoBase &EIB) { 210 llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message() 211 << "\n"; 212 }); 213 exit(1); 214 } 215 216 appendFile(Members, LibMachine, LibMachineSource, *ChildMB); 217 } 218 219 fatalOpenError(std::move(Err), MB.getBufferIdentifier()); 220 return; 221 } 222 223 // Check that all input files have the same machine type. 224 // Mixing normal objects and LTO bitcode files is fine as long as they 225 // have the same machine type. 226 // Doing this here duplicates the header parsing work that writeArchive() 227 // below does, but it's not a lot of work and it's a bit awkward to do 228 // in writeArchive() which needs to support many tools, can't assume the 229 // input is COFF, and doesn't have a good way to report errors. 230 if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) { 231 Expected<COFF::MachineTypes> MaybeFileMachine = 232 (Magic == file_magic::coff_object) ? getCOFFFileMachine(MB) 233 : getBitcodeFileMachine(MB); 234 if (!MaybeFileMachine) { 235 handleAllErrors(MaybeFileMachine.takeError(), 236 [&](const ErrorInfoBase &EIB) { 237 llvm::errs() << MB.getBufferIdentifier() << ": " 238 << EIB.message() << "\n"; 239 }); 240 exit(1); 241 } 242 COFF::MachineTypes FileMachine = *MaybeFileMachine; 243 244 // FIXME: Once lld-link rejects multiple resource .obj files: 245 // Call convertResToCOFF() on .res files and add the resulting 246 // COFF file to the .lib output instead of adding the .res file, and remove 247 // this check. See PR42180. 248 if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) { 249 if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) { 250 LibMachine = FileMachine; 251 LibMachineSource = 252 (" (inferred from earlier file '" + MB.getBufferIdentifier() + "')") 253 .str(); 254 } else if (LibMachine != FileMachine) { 255 llvm::errs() << MB.getBufferIdentifier() << ": file machine type " 256 << machineToStr(FileMachine) 257 << " conflicts with library machine type " 258 << machineToStr(LibMachine) << LibMachineSource << '\n'; 259 exit(1); 260 } 261 } 262 } 263 264 Members.emplace_back(MB); 265 } 266 267 int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) { 268 BumpPtrAllocator Alloc; 269 StringSaver Saver(Alloc); 270 271 // Parse command line arguments. 272 SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end()); 273 cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs); 274 ArgsArr = NewArgs; 275 276 LibOptTable Table; 277 unsigned MissingIndex; 278 unsigned MissingCount; 279 opt::InputArgList Args = 280 Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount); 281 if (MissingCount) { 282 llvm::errs() << "missing arg value for \"" 283 << Args.getArgString(MissingIndex) << "\", expected " 284 << MissingCount 285 << (MissingCount == 1 ? " argument.\n" : " arguments.\n"); 286 return 1; 287 } 288 for (auto *Arg : Args.filtered(OPT_UNKNOWN)) 289 llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args) 290 << "\n"; 291 292 // Handle /help 293 if (Args.hasArg(OPT_help)) { 294 Table.printHelp(outs(), "llvm-lib [options] file...", "LLVM Lib"); 295 return 0; 296 } 297 298 // Parse /ignore: 299 llvm::StringSet<> IgnoredWarnings; 300 for (auto *Arg : Args.filtered(OPT_ignore)) 301 IgnoredWarnings.insert(Arg->getValue()); 302 303 // If no input files and not told otherwise, silently do nothing to match 304 // lib.exe 305 if (!Args.hasArgNoClaim(OPT_INPUT) && !Args.hasArg(OPT_llvmlibempty)) { 306 if (!IgnoredWarnings.contains("emptyoutput")) { 307 llvm::errs() << "warning: no input files, not writing output file\n"; 308 llvm::errs() << " pass /llvmlibempty to write empty .lib file,\n"; 309 llvm::errs() << " pass /ignore:emptyoutput to suppress warning\n"; 310 if (Args.hasFlag(OPT_WX, OPT_WX_no, false)) { 311 llvm::errs() << "treating warning as error due to /WX\n"; 312 return 1; 313 } 314 } 315 return 0; 316 } 317 318 if (Args.hasArg(OPT_lst)) { 319 doList(Args); 320 return 0; 321 } 322 323 std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver); 324 325 COFF::MachineTypes LibMachine = COFF::IMAGE_FILE_MACHINE_UNKNOWN; 326 std::string LibMachineSource; 327 if (auto *Arg = Args.getLastArg(OPT_machine)) { 328 LibMachine = getMachineType(Arg->getValue()); 329 if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) { 330 llvm::errs() << "unknown /machine: arg " << Arg->getValue() << '\n'; 331 return 1; 332 } 333 LibMachineSource = 334 std::string(" (from '/machine:") + Arg->getValue() + "' flag)"; 335 } 336 337 std::vector<std::unique_ptr<MemoryBuffer>> MBs; 338 StringSet<> Seen; 339 std::vector<NewArchiveMember> Members; 340 341 // Create a NewArchiveMember for each input file. 342 for (auto *Arg : Args.filtered(OPT_INPUT)) { 343 // Find a file 344 std::string Path = findInputFile(Arg->getValue(), SearchPaths); 345 if (Path.empty()) { 346 llvm::errs() << Arg->getValue() << ": no such file or directory\n"; 347 return 1; 348 } 349 350 // Input files are uniquified by pathname. If you specify the exact same 351 // path more than once, all but the first one are ignored. 352 // 353 // Note that there's a loophole in the rule; you can prepend `.\` or 354 // something like that to a path to make it look different, and they are 355 // handled as if they were different files. This behavior is compatible with 356 // Microsoft lib.exe. 357 if (!Seen.insert(Path).second) 358 continue; 359 360 // Open a file. 361 ErrorOr<std::unique_ptr<MemoryBuffer>> MOrErr = MemoryBuffer::getFile( 362 Path, /*IsText=*/false, /*RequiresNullTerminator=*/false); 363 fatalOpenError(errorCodeToError(MOrErr.getError()), Path); 364 MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef(); 365 366 // Append a file. 367 appendFile(Members, LibMachine, LibMachineSource, MBRef); 368 369 // Take the ownership of the file buffer to keep the file open. 370 MBs.push_back(std::move(*MOrErr)); 371 } 372 373 // Create an archive file. 374 std::string OutputPath; 375 if (auto *Arg = Args.getLastArg(OPT_out)) { 376 OutputPath = Arg->getValue(); 377 } else if (!Members.empty()) { 378 OutputPath = getDefaultOutputPath(Members[0]); 379 } else { 380 llvm::errs() << "no output path given, and cannot infer with no inputs\n"; 381 return 1; 382 } 383 // llvm-lib uses relative paths for both regular and thin archives, unlike 384 // standard GNU ar, which only uses relative paths for thin archives and 385 // basenames for regular archives. 386 for (NewArchiveMember &Member : Members) { 387 if (sys::path::is_relative(Member.MemberName)) { 388 Expected<std::string> PathOrErr = 389 computeArchiveRelativePath(OutputPath, Member.MemberName); 390 if (PathOrErr) 391 Member.MemberName = Saver.save(*PathOrErr); 392 } 393 } 394 395 if (Error E = 396 writeArchive(OutputPath, Members, 397 /*WriteSymtab=*/true, object::Archive::K_GNU, 398 /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) { 399 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) { 400 llvm::errs() << OutputPath << ": " << EI.message() << "\n"; 401 }); 402 return 1; 403 } 404 405 return 0; 406 } 407