xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===- LibDriver.cpp - lib.exe-compatible driver --------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // Defines an interface to a lib.exe-compatible driver that also understands
107330f729Sjoerg // bitcode files. Used by llvm-lib and lld-link /lib.
117330f729Sjoerg //
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg 
147330f729Sjoerg #include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
157330f729Sjoerg #include "llvm/ADT/STLExtras.h"
167330f729Sjoerg #include "llvm/ADT/StringSet.h"
177330f729Sjoerg #include "llvm/BinaryFormat/COFF.h"
187330f729Sjoerg #include "llvm/BinaryFormat/Magic.h"
197330f729Sjoerg #include "llvm/Bitcode/BitcodeReader.h"
207330f729Sjoerg #include "llvm/Object/ArchiveWriter.h"
217330f729Sjoerg #include "llvm/Object/COFF.h"
227330f729Sjoerg #include "llvm/Object/WindowsMachineFlag.h"
237330f729Sjoerg #include "llvm/Option/Arg.h"
247330f729Sjoerg #include "llvm/Option/ArgList.h"
257330f729Sjoerg #include "llvm/Option/Option.h"
267330f729Sjoerg #include "llvm/Support/CommandLine.h"
277330f729Sjoerg #include "llvm/Support/Path.h"
287330f729Sjoerg #include "llvm/Support/Process.h"
297330f729Sjoerg #include "llvm/Support/StringSaver.h"
307330f729Sjoerg #include "llvm/Support/raw_ostream.h"
317330f729Sjoerg 
327330f729Sjoerg using namespace llvm;
337330f729Sjoerg 
347330f729Sjoerg namespace {
357330f729Sjoerg 
367330f729Sjoerg enum {
377330f729Sjoerg   OPT_INVALID = 0,
387330f729Sjoerg #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
397330f729Sjoerg #include "Options.inc"
407330f729Sjoerg #undef OPTION
417330f729Sjoerg };
427330f729Sjoerg 
437330f729Sjoerg #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
447330f729Sjoerg #include "Options.inc"
457330f729Sjoerg #undef PREFIX
467330f729Sjoerg 
477330f729Sjoerg static const opt::OptTable::Info InfoTable[] = {
487330f729Sjoerg #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
497330f729Sjoerg   {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
507330f729Sjoerg    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
517330f729Sjoerg #include "Options.inc"
527330f729Sjoerg #undef OPTION
537330f729Sjoerg };
547330f729Sjoerg 
557330f729Sjoerg class LibOptTable : public opt::OptTable {
567330f729Sjoerg public:
LibOptTable()577330f729Sjoerg   LibOptTable() : OptTable(InfoTable, true) {}
587330f729Sjoerg };
597330f729Sjoerg 
607330f729Sjoerg }
617330f729Sjoerg 
getDefaultOutputPath(const NewArchiveMember & FirstMember)62*82d56013Sjoerg static std::string getDefaultOutputPath(const NewArchiveMember &FirstMember) {
637330f729Sjoerg   SmallString<128> Val = StringRef(FirstMember.Buf->getBufferIdentifier());
647330f729Sjoerg   sys::path::replace_extension(Val, ".lib");
65*82d56013Sjoerg   return std::string(Val.str());
667330f729Sjoerg }
677330f729Sjoerg 
getSearchPaths(opt::InputArgList * Args,StringSaver & Saver)687330f729Sjoerg static std::vector<StringRef> getSearchPaths(opt::InputArgList *Args,
697330f729Sjoerg                                              StringSaver &Saver) {
707330f729Sjoerg   std::vector<StringRef> Ret;
717330f729Sjoerg   // Add current directory as first item of the search path.
727330f729Sjoerg   Ret.push_back("");
737330f729Sjoerg 
747330f729Sjoerg   // Add /libpath flags.
757330f729Sjoerg   for (auto *Arg : Args->filtered(OPT_libpath))
767330f729Sjoerg     Ret.push_back(Arg->getValue());
777330f729Sjoerg 
787330f729Sjoerg   // Add $LIB.
797330f729Sjoerg   Optional<std::string> EnvOpt = sys::Process::GetEnv("LIB");
807330f729Sjoerg   if (!EnvOpt.hasValue())
817330f729Sjoerg     return Ret;
827330f729Sjoerg   StringRef Env = Saver.save(*EnvOpt);
837330f729Sjoerg   while (!Env.empty()) {
847330f729Sjoerg     StringRef Path;
857330f729Sjoerg     std::tie(Path, Env) = Env.split(';');
867330f729Sjoerg     Ret.push_back(Path);
877330f729Sjoerg   }
887330f729Sjoerg   return Ret;
897330f729Sjoerg }
907330f729Sjoerg 
findInputFile(StringRef File,ArrayRef<StringRef> Paths)917330f729Sjoerg static std::string findInputFile(StringRef File, ArrayRef<StringRef> Paths) {
927330f729Sjoerg   for (StringRef Dir : Paths) {
937330f729Sjoerg     SmallString<128> Path = Dir;
947330f729Sjoerg     sys::path::append(Path, File);
957330f729Sjoerg     if (sys::fs::exists(Path))
96*82d56013Sjoerg       return std::string(Path);
977330f729Sjoerg   }
987330f729Sjoerg   return "";
997330f729Sjoerg }
1007330f729Sjoerg 
fatalOpenError(llvm::Error E,Twine File)1017330f729Sjoerg static void fatalOpenError(llvm::Error E, Twine File) {
1027330f729Sjoerg   if (!E)
1037330f729Sjoerg     return;
1047330f729Sjoerg   handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
1057330f729Sjoerg     llvm::errs() << "error opening '" << File << "': " << EIB.message() << '\n';
1067330f729Sjoerg     exit(1);
1077330f729Sjoerg   });
1087330f729Sjoerg }
1097330f729Sjoerg 
doList(opt::InputArgList & Args)1107330f729Sjoerg static void doList(opt::InputArgList& Args) {
1117330f729Sjoerg   // lib.exe prints the contents of the first archive file.
1127330f729Sjoerg   std::unique_ptr<MemoryBuffer> B;
1137330f729Sjoerg   for (auto *Arg : Args.filtered(OPT_INPUT)) {
1147330f729Sjoerg     // Create or open the archive object.
115*82d56013Sjoerg     ErrorOr<std::unique_ptr<MemoryBuffer>> MaybeBuf = MemoryBuffer::getFile(
116*82d56013Sjoerg         Arg->getValue(), /*IsText=*/false, /*RequiresNullTerminator=*/false);
1177330f729Sjoerg     fatalOpenError(errorCodeToError(MaybeBuf.getError()), Arg->getValue());
1187330f729Sjoerg 
1197330f729Sjoerg     if (identify_magic(MaybeBuf.get()->getBuffer()) == file_magic::archive) {
1207330f729Sjoerg       B = std::move(MaybeBuf.get());
1217330f729Sjoerg       break;
1227330f729Sjoerg     }
1237330f729Sjoerg   }
1247330f729Sjoerg 
1257330f729Sjoerg   // lib.exe doesn't print an error if no .lib files are passed.
1267330f729Sjoerg   if (!B)
1277330f729Sjoerg     return;
1287330f729Sjoerg 
1297330f729Sjoerg   Error Err = Error::success();
1307330f729Sjoerg   object::Archive Archive(B.get()->getMemBufferRef(), Err);
1317330f729Sjoerg   fatalOpenError(std::move(Err), B->getBufferIdentifier());
1327330f729Sjoerg 
1337330f729Sjoerg   for (auto &C : Archive.children(Err)) {
1347330f729Sjoerg     Expected<StringRef> NameOrErr = C.getName();
1357330f729Sjoerg     fatalOpenError(NameOrErr.takeError(), B->getBufferIdentifier());
1367330f729Sjoerg     StringRef Name = NameOrErr.get();
1377330f729Sjoerg     llvm::outs() << Name << '\n';
1387330f729Sjoerg   }
1397330f729Sjoerg   fatalOpenError(std::move(Err), B->getBufferIdentifier());
1407330f729Sjoerg }
1417330f729Sjoerg 
getCOFFFileMachine(MemoryBufferRef MB)142*82d56013Sjoerg static Expected<COFF::MachineTypes> getCOFFFileMachine(MemoryBufferRef MB) {
1437330f729Sjoerg   std::error_code EC;
144*82d56013Sjoerg   auto Obj = object::COFFObjectFile::create(MB);
145*82d56013Sjoerg   if (!Obj)
146*82d56013Sjoerg     return Obj.takeError();
1477330f729Sjoerg 
148*82d56013Sjoerg   uint16_t Machine = (*Obj)->getMachine();
1497330f729Sjoerg   if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
1507330f729Sjoerg       Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
1517330f729Sjoerg       Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
1527330f729Sjoerg       Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
153*82d56013Sjoerg     return createStringError(inconvertibleErrorCode(),
154*82d56013Sjoerg                              "unknown machine: " + std::to_string(Machine));
1557330f729Sjoerg   }
1567330f729Sjoerg 
1577330f729Sjoerg   return static_cast<COFF::MachineTypes>(Machine);
1587330f729Sjoerg }
1597330f729Sjoerg 
getBitcodeFileMachine(MemoryBufferRef MB)160*82d56013Sjoerg static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) {
1617330f729Sjoerg   Expected<std::string> TripleStr = getBitcodeTargetTriple(MB);
162*82d56013Sjoerg   if (!TripleStr)
163*82d56013Sjoerg     return TripleStr.takeError();
1647330f729Sjoerg 
1657330f729Sjoerg   switch (Triple(*TripleStr).getArch()) {
1667330f729Sjoerg   case Triple::x86:
1677330f729Sjoerg     return COFF::IMAGE_FILE_MACHINE_I386;
1687330f729Sjoerg   case Triple::x86_64:
1697330f729Sjoerg     return COFF::IMAGE_FILE_MACHINE_AMD64;
1707330f729Sjoerg   case Triple::arm:
1717330f729Sjoerg     return COFF::IMAGE_FILE_MACHINE_ARMNT;
1727330f729Sjoerg   case Triple::aarch64:
1737330f729Sjoerg     return COFF::IMAGE_FILE_MACHINE_ARM64;
1747330f729Sjoerg   default:
175*82d56013Sjoerg     return createStringError(inconvertibleErrorCode(),
176*82d56013Sjoerg                              "unknown arch in target triple: " + *TripleStr);
1777330f729Sjoerg   }
1787330f729Sjoerg }
1797330f729Sjoerg 
appendFile(std::vector<NewArchiveMember> & Members,COFF::MachineTypes & LibMachine,std::string & LibMachineSource,MemoryBufferRef MB)1807330f729Sjoerg static void appendFile(std::vector<NewArchiveMember> &Members,
1817330f729Sjoerg                        COFF::MachineTypes &LibMachine,
1827330f729Sjoerg                        std::string &LibMachineSource, MemoryBufferRef MB) {
1837330f729Sjoerg   file_magic Magic = identify_magic(MB.getBuffer());
1847330f729Sjoerg 
1857330f729Sjoerg   if (Magic != file_magic::coff_object && Magic != file_magic::bitcode &&
186*82d56013Sjoerg       Magic != file_magic::archive && Magic != file_magic::windows_resource &&
187*82d56013Sjoerg       Magic != file_magic::coff_import_library) {
1887330f729Sjoerg     llvm::errs() << MB.getBufferIdentifier()
189*82d56013Sjoerg                  << ": not a COFF object, bitcode, archive, import library or "
190*82d56013Sjoerg                     "resource file\n";
1917330f729Sjoerg     exit(1);
1927330f729Sjoerg   }
1937330f729Sjoerg 
1947330f729Sjoerg   // If a user attempts to add an archive to another archive, llvm-lib doesn't
1957330f729Sjoerg   // handle the first archive file as a single file. Instead, it extracts all
196*82d56013Sjoerg   // members from the archive and add them to the second archive. This behavior
1977330f729Sjoerg   // is for compatibility with Microsoft's lib command.
1987330f729Sjoerg   if (Magic == file_magic::archive) {
1997330f729Sjoerg     Error Err = Error::success();
2007330f729Sjoerg     object::Archive Archive(MB, Err);
2017330f729Sjoerg     fatalOpenError(std::move(Err), MB.getBufferIdentifier());
2027330f729Sjoerg 
2037330f729Sjoerg     for (auto &C : Archive.children(Err)) {
2047330f729Sjoerg       Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef();
2057330f729Sjoerg       if (!ChildMB) {
2067330f729Sjoerg         handleAllErrors(ChildMB.takeError(), [&](const ErrorInfoBase &EIB) {
2077330f729Sjoerg           llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message()
2087330f729Sjoerg                        << "\n";
2097330f729Sjoerg         });
2107330f729Sjoerg         exit(1);
2117330f729Sjoerg       }
2127330f729Sjoerg 
2137330f729Sjoerg       appendFile(Members, LibMachine, LibMachineSource, *ChildMB);
2147330f729Sjoerg     }
2157330f729Sjoerg 
2167330f729Sjoerg     fatalOpenError(std::move(Err), MB.getBufferIdentifier());
2177330f729Sjoerg     return;
2187330f729Sjoerg   }
2197330f729Sjoerg 
2207330f729Sjoerg   // Check that all input files have the same machine type.
2217330f729Sjoerg   // Mixing normal objects and LTO bitcode files is fine as long as they
2227330f729Sjoerg   // have the same machine type.
2237330f729Sjoerg   // Doing this here duplicates the header parsing work that writeArchive()
2247330f729Sjoerg   // below does, but it's not a lot of work and it's a bit awkward to do
2257330f729Sjoerg   // in writeArchive() which needs to support many tools, can't assume the
2267330f729Sjoerg   // input is COFF, and doesn't have a good way to report errors.
2277330f729Sjoerg   if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) {
228*82d56013Sjoerg     Expected<COFF::MachineTypes> MaybeFileMachine =
229*82d56013Sjoerg         (Magic == file_magic::coff_object) ? getCOFFFileMachine(MB)
2307330f729Sjoerg                                            : getBitcodeFileMachine(MB);
231*82d56013Sjoerg     if (!MaybeFileMachine) {
232*82d56013Sjoerg       handleAllErrors(MaybeFileMachine.takeError(), [&](const ErrorInfoBase &EIB) {
233*82d56013Sjoerg         llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message()
234*82d56013Sjoerg                      << "\n";
235*82d56013Sjoerg       });
236*82d56013Sjoerg       exit(1);
237*82d56013Sjoerg     }
238*82d56013Sjoerg     COFF::MachineTypes FileMachine = *MaybeFileMachine;
2397330f729Sjoerg 
2407330f729Sjoerg     // FIXME: Once lld-link rejects multiple resource .obj files:
2417330f729Sjoerg     // Call convertResToCOFF() on .res files and add the resulting
2427330f729Sjoerg     // COFF file to the .lib output instead of adding the .res file, and remove
2437330f729Sjoerg     // this check. See PR42180.
2447330f729Sjoerg     if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
2457330f729Sjoerg       if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
2467330f729Sjoerg         LibMachine = FileMachine;
2477330f729Sjoerg         LibMachineSource =
2487330f729Sjoerg             (" (inferred from earlier file '" + MB.getBufferIdentifier() + "')")
2497330f729Sjoerg                 .str();
2507330f729Sjoerg       } else if (LibMachine != FileMachine) {
2517330f729Sjoerg         llvm::errs() << MB.getBufferIdentifier() << ": file machine type "
2527330f729Sjoerg                      << machineToStr(FileMachine)
2537330f729Sjoerg                      << " conflicts with library machine type "
2547330f729Sjoerg                      << machineToStr(LibMachine) << LibMachineSource << '\n';
2557330f729Sjoerg         exit(1);
2567330f729Sjoerg       }
2577330f729Sjoerg     }
2587330f729Sjoerg   }
2597330f729Sjoerg 
2607330f729Sjoerg   Members.emplace_back(MB);
2617330f729Sjoerg }
2627330f729Sjoerg 
libDriverMain(ArrayRef<const char * > ArgsArr)2637330f729Sjoerg int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
2647330f729Sjoerg   BumpPtrAllocator Alloc;
2657330f729Sjoerg   StringSaver Saver(Alloc);
2667330f729Sjoerg 
2677330f729Sjoerg   // Parse command line arguments.
2687330f729Sjoerg   SmallVector<const char *, 20> NewArgs(ArgsArr.begin(), ArgsArr.end());
2697330f729Sjoerg   cl::ExpandResponseFiles(Saver, cl::TokenizeWindowsCommandLine, NewArgs);
2707330f729Sjoerg   ArgsArr = NewArgs;
2717330f729Sjoerg 
2727330f729Sjoerg   LibOptTable Table;
2737330f729Sjoerg   unsigned MissingIndex;
2747330f729Sjoerg   unsigned MissingCount;
2757330f729Sjoerg   opt::InputArgList Args =
2767330f729Sjoerg       Table.ParseArgs(ArgsArr.slice(1), MissingIndex, MissingCount);
2777330f729Sjoerg   if (MissingCount) {
2787330f729Sjoerg     llvm::errs() << "missing arg value for \""
2797330f729Sjoerg                  << Args.getArgString(MissingIndex) << "\", expected "
2807330f729Sjoerg                  << MissingCount
2817330f729Sjoerg                  << (MissingCount == 1 ? " argument.\n" : " arguments.\n");
2827330f729Sjoerg     return 1;
2837330f729Sjoerg   }
2847330f729Sjoerg   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
2857330f729Sjoerg     llvm::errs() << "ignoring unknown argument: " << Arg->getAsString(Args)
2867330f729Sjoerg                  << "\n";
2877330f729Sjoerg 
2887330f729Sjoerg   // Handle /help
2897330f729Sjoerg   if (Args.hasArg(OPT_help)) {
2907330f729Sjoerg     Table.PrintHelp(outs(), "llvm-lib [options] file...", "LLVM Lib");
2917330f729Sjoerg     return 0;
2927330f729Sjoerg   }
2937330f729Sjoerg 
294*82d56013Sjoerg   // If no input files and not told otherwise, silently do nothing to match
295*82d56013Sjoerg   // lib.exe
296*82d56013Sjoerg   if (!Args.hasArgNoClaim(OPT_INPUT) && !Args.hasArg(OPT_llvmlibempty))
2977330f729Sjoerg     return 0;
2987330f729Sjoerg 
2997330f729Sjoerg   if (Args.hasArg(OPT_lst)) {
3007330f729Sjoerg     doList(Args);
3017330f729Sjoerg     return 0;
3027330f729Sjoerg   }
3037330f729Sjoerg 
3047330f729Sjoerg   std::vector<StringRef> SearchPaths = getSearchPaths(&Args, Saver);
3057330f729Sjoerg 
3067330f729Sjoerg   COFF::MachineTypes LibMachine = COFF::IMAGE_FILE_MACHINE_UNKNOWN;
3077330f729Sjoerg   std::string LibMachineSource;
3087330f729Sjoerg   if (auto *Arg = Args.getLastArg(OPT_machine)) {
3097330f729Sjoerg     LibMachine = getMachineType(Arg->getValue());
3107330f729Sjoerg     if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
3117330f729Sjoerg       llvm::errs() << "unknown /machine: arg " << Arg->getValue() << '\n';
3127330f729Sjoerg       return 1;
3137330f729Sjoerg     }
3147330f729Sjoerg     LibMachineSource =
3157330f729Sjoerg         std::string(" (from '/machine:") + Arg->getValue() + "' flag)";
3167330f729Sjoerg   }
3177330f729Sjoerg 
3187330f729Sjoerg   std::vector<std::unique_ptr<MemoryBuffer>> MBs;
3197330f729Sjoerg   StringSet<> Seen;
3207330f729Sjoerg   std::vector<NewArchiveMember> Members;
3217330f729Sjoerg 
3227330f729Sjoerg   // Create a NewArchiveMember for each input file.
3237330f729Sjoerg   for (auto *Arg : Args.filtered(OPT_INPUT)) {
3247330f729Sjoerg     // Find a file
3257330f729Sjoerg     std::string Path = findInputFile(Arg->getValue(), SearchPaths);
3267330f729Sjoerg     if (Path.empty()) {
3277330f729Sjoerg       llvm::errs() << Arg->getValue() << ": no such file or directory\n";
3287330f729Sjoerg       return 1;
3297330f729Sjoerg     }
3307330f729Sjoerg 
3317330f729Sjoerg     // Input files are uniquified by pathname. If you specify the exact same
3327330f729Sjoerg     // path more than once, all but the first one are ignored.
3337330f729Sjoerg     //
3347330f729Sjoerg     // Note that there's a loophole in the rule; you can prepend `.\` or
3357330f729Sjoerg     // something like that to a path to make it look different, and they are
3367330f729Sjoerg     // handled as if they were different files. This behavior is compatible with
3377330f729Sjoerg     // Microsoft lib.exe.
3387330f729Sjoerg     if (!Seen.insert(Path).second)
3397330f729Sjoerg       continue;
3407330f729Sjoerg 
3417330f729Sjoerg     // Open a file.
342*82d56013Sjoerg     ErrorOr<std::unique_ptr<MemoryBuffer>> MOrErr = MemoryBuffer::getFile(
343*82d56013Sjoerg         Path, /*IsText=*/false, /*RequiresNullTerminator=*/false);
3447330f729Sjoerg     fatalOpenError(errorCodeToError(MOrErr.getError()), Path);
3457330f729Sjoerg     MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef();
3467330f729Sjoerg 
3477330f729Sjoerg     // Append a file.
3487330f729Sjoerg     appendFile(Members, LibMachine, LibMachineSource, MBRef);
3497330f729Sjoerg 
3507330f729Sjoerg     // Take the ownership of the file buffer to keep the file open.
3517330f729Sjoerg     MBs.push_back(std::move(*MOrErr));
3527330f729Sjoerg   }
3537330f729Sjoerg 
3547330f729Sjoerg   // Create an archive file.
355*82d56013Sjoerg   std::string OutputPath;
356*82d56013Sjoerg   if (auto *Arg = Args.getLastArg(OPT_out)) {
357*82d56013Sjoerg     OutputPath = Arg->getValue();
358*82d56013Sjoerg   } else if (!Members.empty()) {
359*82d56013Sjoerg     OutputPath = getDefaultOutputPath(Members[0]);
360*82d56013Sjoerg   } else {
361*82d56013Sjoerg     llvm::errs() << "no output path given, and cannot infer with no inputs\n";
362*82d56013Sjoerg     return 1;
363*82d56013Sjoerg   }
3647330f729Sjoerg   // llvm-lib uses relative paths for both regular and thin archives, unlike
3657330f729Sjoerg   // standard GNU ar, which only uses relative paths for thin archives and
3667330f729Sjoerg   // basenames for regular archives.
3677330f729Sjoerg   for (NewArchiveMember &Member : Members) {
3687330f729Sjoerg     if (sys::path::is_relative(Member.MemberName)) {
3697330f729Sjoerg       Expected<std::string> PathOrErr =
3707330f729Sjoerg           computeArchiveRelativePath(OutputPath, Member.MemberName);
3717330f729Sjoerg       if (PathOrErr)
3727330f729Sjoerg         Member.MemberName = Saver.save(*PathOrErr);
3737330f729Sjoerg     }
3747330f729Sjoerg   }
3757330f729Sjoerg 
3767330f729Sjoerg   if (Error E =
3777330f729Sjoerg           writeArchive(OutputPath, Members,
3787330f729Sjoerg                        /*WriteSymtab=*/true, object::Archive::K_GNU,
3797330f729Sjoerg                        /*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) {
3807330f729Sjoerg     handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
3817330f729Sjoerg       llvm::errs() << OutputPath << ": " << EI.message() << "\n";
3827330f729Sjoerg     });
3837330f729Sjoerg     return 1;
3847330f729Sjoerg   }
3857330f729Sjoerg 
3867330f729Sjoerg   return 0;
3877330f729Sjoerg }
388