1 //===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===//
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 // This file defines the writeArchive function.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Object/ArchiveWriter.h"
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/BinaryFormat/Magic.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/Object/Archive.h"
20 #include "llvm/Object/Error.h"
21 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Object/SymbolicFile.h"
23 #include "llvm/Support/Alignment.h"
24 #include "llvm/Support/EndianStream.h"
25 #include "llvm/Support/Errc.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/Path.h"
29 #include "llvm/Support/SmallVectorMemoryBuffer.h"
30 #include "llvm/Support/ToolOutputFile.h"
31 #include "llvm/Support/raw_ostream.h"
35 #if !defined(_MSC_VER) && !defined(__MINGW32__)
43 NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef)
44 : Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
45 MemberName(BufRef.getBufferIdentifier()) {}
47 Expected<NewArchiveMember>
48 NewArchiveMember::getOldMember(const object::Archive::Child &OldMember,
50 Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef();
52 return BufOrErr.takeError();
55 M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
56 M.MemberName = M.Buf->getBufferIdentifier();
58 auto ModTimeOrErr = OldMember.getLastModified();
60 return ModTimeOrErr.takeError();
61 M.ModTime = ModTimeOrErr.get();
62 Expected<unsigned> UIDOrErr = OldMember.getUID();
64 return UIDOrErr.takeError();
65 M.UID = UIDOrErr.get();
66 Expected<unsigned> GIDOrErr = OldMember.getGID();
68 return GIDOrErr.takeError();
69 M.GID = GIDOrErr.get();
70 Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
72 return AccessModeOrErr.takeError();
73 M.Perms = AccessModeOrErr.get();
78 Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
80 sys::fs::file_status Status;
81 auto FDOrErr = sys::fs::openNativeFileForRead(FileName);
83 return FDOrErr.takeError();
84 sys::fs::file_t FD = *FDOrErr;
85 assert(FD != sys::fs::kInvalidFile);
87 if (auto EC = sys::fs::status(FD, Status))
88 return errorCodeToError(EC);
90 // Opening a directory doesn't make sense. Let it fail.
91 // Linux cannot open directories with open(2), although
92 // cygwin and *bsd can.
93 if (Status.type() == sys::fs::file_type::directory_file)
94 return errorCodeToError(make_error_code(errc::is_a_directory));
96 ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
97 MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
98 if (!MemberBufferOrErr)
99 return errorCodeToError(MemberBufferOrErr.getError());
101 if (auto EC = sys::fs::closeFile(FD))
102 return errorCodeToError(EC);
105 M.Buf = std::move(*MemberBufferOrErr);
106 M.MemberName = M.Buf->getBufferIdentifier();
107 if (!Deterministic) {
108 M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
109 Status.getLastModificationTime());
110 M.UID = Status.getUser();
111 M.GID = Status.getGroup();
112 M.Perms = Status.permissions();
117 template <typename T>
118 static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
119 uint64_t OldPos = OS.tell();
121 unsigned SizeSoFar = OS.tell() - OldPos;
122 assert(SizeSoFar <= Size && "Data doesn't fit in Size");
123 OS.indent(Size - SizeSoFar);
126 static bool isDarwin(object::Archive::Kind Kind) {
127 return Kind == object::Archive::K_DARWIN ||
128 Kind == object::Archive::K_DARWIN64;
131 static bool isBSDLike(object::Archive::Kind Kind) {
133 case object::Archive::K_GNU:
134 case object::Archive::K_GNU64:
136 case object::Archive::K_BSD:
137 case object::Archive::K_DARWIN:
138 case object::Archive::K_DARWIN64:
140 case object::Archive::K_AIXBIG:
141 case object::Archive::K_COFF:
144 llvm_unreachable("not supported for writting");
148 static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) {
149 support::endian::write(Out, Val,
150 isBSDLike(Kind) ? support::little : support::big);
153 static void printRestOfMemberHeader(
154 raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime,
155 unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
156 printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
158 // The format has only 6 chars for uid and gid. Truncate if the provided
160 printWithSpacePadding(Out, UID % 1000000, 6);
161 printWithSpacePadding(Out, GID % 1000000, 6);
163 printWithSpacePadding(Out, format("%o", Perms), 8);
164 printWithSpacePadding(Out, Size, 10);
169 printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name,
170 const sys::TimePoint<std::chrono::seconds> &ModTime,
171 unsigned UID, unsigned GID, unsigned Perms,
173 printWithSpacePadding(Out, Twine(Name) + "/", 16);
174 printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
178 printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name,
179 const sys::TimePoint<std::chrono::seconds> &ModTime,
180 unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
181 uint64_t PosAfterHeader = Pos + 60 + Name.size();
182 // Pad so that even 64 bit object files are aligned.
183 unsigned Pad = offsetToAlignment(PosAfterHeader, Align(8));
184 unsigned NameWithPadding = Name.size() + Pad;
185 printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
186 printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
187 NameWithPadding + Size);
190 Out.write(uint8_t(0));
193 static bool useStringTable(bool Thin, StringRef Name) {
194 return Thin || Name.size() >= 16 || Name.contains('/');
197 static bool is64BitKind(object::Archive::Kind Kind) {
199 case object::Archive::K_GNU:
200 case object::Archive::K_BSD:
201 case object::Archive::K_DARWIN:
202 case object::Archive::K_COFF:
203 case object::Archive::K_AIXBIG:
205 case object::Archive::K_DARWIN64:
206 case object::Archive::K_GNU64:
209 llvm_unreachable("not supported for writting");
213 printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable,
214 StringMap<uint64_t> &MemberNames, object::Archive::Kind Kind,
215 bool Thin, const NewArchiveMember &M,
216 sys::TimePoint<std::chrono::seconds> ModTime, uint64_t Size) {
218 return printBSDMemberHeader(Out, Pos, M.MemberName, ModTime, M.UID, M.GID,
220 if (!useStringTable(Thin, M.MemberName))
221 return printGNUSmallMemberHeader(Out, M.MemberName, ModTime, M.UID, M.GID,
226 NamePos = StringTable.tell();
227 StringTable << M.MemberName << "/\n";
229 auto Insertion = MemberNames.insert({M.MemberName, uint64_t(0)});
230 if (Insertion.second) {
231 Insertion.first->second = StringTable.tell();
232 StringTable << M.MemberName << "/\n";
234 NamePos = Insertion.first->second;
236 printWithSpacePadding(Out, NamePos, 15);
237 printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size);
242 std::vector<unsigned> Symbols;
249 static MemberData computeStringTable(StringRef Names) {
250 unsigned Size = Names.size();
251 unsigned Pad = offsetToAlignment(Size, Align(2));
253 raw_string_ostream Out(Header);
254 printWithSpacePadding(Out, "//", 48);
255 printWithSpacePadding(Out, Size + Pad, 10);
258 return {{}, std::move(Header), Names, Pad ? "\n" : ""};
261 static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
262 using namespace std::chrono;
265 return time_point_cast<seconds>(system_clock::now());
266 return sys::TimePoint<seconds>();
269 static bool isArchiveSymbol(const object::BasicSymbolRef &S) {
270 Expected<uint32_t> SymFlagsOrErr = S.getFlags();
272 // TODO: Actually report errors helpfully.
273 report_fatal_error(SymFlagsOrErr.takeError());
274 if (*SymFlagsOrErr & object::SymbolRef::SF_FormatSpecific)
276 if (!(*SymFlagsOrErr & object::SymbolRef::SF_Global))
278 if (*SymFlagsOrErr & object::SymbolRef::SF_Undefined)
283 static void printNBits(raw_ostream &Out, object::Archive::Kind Kind,
285 if (is64BitKind(Kind))
286 print<uint64_t>(Out, Kind, Val);
288 print<uint32_t>(Out, Kind, Val);
291 static uint64_t computeSymbolTableSize(object::Archive::Kind Kind,
292 uint64_t NumSyms, uint64_t OffsetSize,
293 StringRef StringTable,
294 uint32_t *Padding = nullptr) {
295 assert((OffsetSize == 4 || OffsetSize == 8) && "Unsupported OffsetSize");
296 uint64_t Size = OffsetSize; // Number of entries
298 Size += NumSyms * OffsetSize * 2; // Table
300 Size += NumSyms * OffsetSize; // Table
302 Size += OffsetSize; // byte count
303 Size += StringTable.size();
304 // ld64 expects the members to be 8-byte aligned for 64-bit content and at
305 // least 4-byte aligned for 32-bit content. Opt for the larger encoding
307 // We do this for all bsd formats because it simplifies aligning members.
308 uint32_t Pad = offsetToAlignment(Size, Align(isBSDLike(Kind) ? 8 : 2));
315 static void writeSymbolTableHeader(raw_ostream &Out, object::Archive::Kind Kind,
316 bool Deterministic, uint64_t Size) {
317 if (isBSDLike(Kind)) {
318 const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF";
319 printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0,
322 const char *Name = is64BitKind(Kind) ? "/SYM64" : "";
323 printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size);
327 static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind,
328 bool Deterministic, ArrayRef<MemberData> Members,
329 StringRef StringTable) {
330 // We don't write a symbol table on an archive with no members -- except on
331 // Darwin, where the linker will abort unless the archive has a symbol table.
332 if (StringTable.empty() && !isDarwin(Kind))
335 unsigned NumSyms = 0;
336 for (const MemberData &M : Members)
337 NumSyms += M.Symbols.size();
339 uint64_t OffsetSize = is64BitKind(Kind) ? 8 : 4;
341 uint64_t Size = computeSymbolTableSize(Kind, NumSyms, OffsetSize, StringTable, &Pad);
342 writeSymbolTableHeader(Out, Kind, Deterministic, Size);
344 uint64_t Pos = Out.tell() + Size;
347 printNBits(Out, Kind, NumSyms * 2 * OffsetSize);
349 printNBits(Out, Kind, NumSyms);
351 for (const MemberData &M : Members) {
352 for (unsigned StringOffset : M.Symbols) {
354 printNBits(Out, Kind, StringOffset);
355 printNBits(Out, Kind, Pos); // member offset
357 Pos += M.Header.size() + M.Data.size() + M.Padding.size();
361 // byte count of the string table
362 printNBits(Out, Kind, StringTable.size());
366 Out.write(uint8_t(0));
369 static Expected<std::vector<unsigned>>
370 getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) {
371 std::vector<unsigned> Ret;
373 // In the scenario when LLVMContext is populated SymbolicFile will contain a
374 // reference to it, thus SymbolicFile should be destroyed first.
376 std::unique_ptr<object::SymbolicFile> Obj;
378 const file_magic Type = identify_magic(Buf.getBuffer());
379 // Treat unsupported file types as having no symbols.
380 if (!object::SymbolicFile::isSymbolicFile(Type, &Context))
382 if (Type == file_magic::bitcode) {
383 auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
384 Buf, file_magic::bitcode, &Context);
386 return ObjOrErr.takeError();
387 Obj = std::move(*ObjOrErr);
389 auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf);
391 return ObjOrErr.takeError();
392 Obj = std::move(*ObjOrErr);
396 for (const object::BasicSymbolRef &S : Obj->symbols()) {
397 if (!isArchiveSymbol(S))
399 Ret.push_back(SymNames.tell());
400 if (Error E = S.printName(SymNames))
407 static Expected<std::vector<MemberData>>
408 computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames,
409 object::Archive::Kind Kind, bool Thin, bool Deterministic,
410 bool NeedSymbols, ArrayRef<NewArchiveMember> NewMembers) {
411 static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};
413 // This ignores the symbol table, but we only need the value mod 8 and the
414 // symbol table is aligned to be a multiple of 8 bytes
417 std::vector<MemberData> Ret;
418 bool HasObject = false;
420 // Deduplicate long member names in the string table and reuse earlier name
421 // offsets. This especially saves space for COFF Import libraries where all
422 // members have the same name.
423 StringMap<uint64_t> MemberNames;
425 // UniqueTimestamps is a special case to improve debugging on Darwin:
427 // The Darwin linker does not link debug info into the final
428 // binary. Instead, it emits entries of type N_OSO in in the output
429 // binary's symbol table, containing references to the linked-in
430 // object files. Using that reference, the debugger can read the
431 // debug data directly from the object files. Alternatively, an
432 // invocation of 'dsymutil' will link the debug data from the object
433 // files into a dSYM bundle, which can be loaded by the debugger,
434 // instead of the object files.
436 // For an object file, the N_OSO entries contain the absolute path
437 // path to the file, and the file's timestamp. For an object
438 // included in an archive, the path is formatted like
439 // "/absolute/path/to/archive.a(member.o)", and the timestamp is the
440 // archive member's timestamp, rather than the archive's timestamp.
442 // However, this doesn't always uniquely identify an object within
443 // an archive -- an archive file can have multiple entries with the
444 // same filename. (This will happen commonly if the original object
445 // files started in different directories.) The only way they get
446 // distinguished, then, is via the timestamp. But this process is
447 // unable to find the correct object file in the archive when there
448 // are two files of the same name and timestamp.
450 // Additionally, timestamp==0 is treated specially, and causes the
451 // timestamp to be ignored as a match criteria.
453 // That will "usually" work out okay when creating an archive not in
454 // deterministic timestamp mode, because the objects will probably
455 // have been created at different timestamps.
457 // To ameliorate this problem, in deterministic archive mode (which
458 // is the default), on Darwin we will emit a unique non-zero
459 // timestamp for each entry with a duplicated name. This is still
460 // deterministic: the only thing affecting that timestamp is the
461 // order of the files in the resultant archive.
463 // See also the functions that handle the lookup:
464 // in lldb: ObjectContainerBSDArchive::Archive::FindObject()
465 // in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers().
466 bool UniqueTimestamps = Deterministic && isDarwin(Kind);
467 std::map<StringRef, unsigned> FilenameCount;
468 if (UniqueTimestamps) {
469 for (const NewArchiveMember &M : NewMembers)
470 FilenameCount[M.MemberName]++;
471 for (auto &Entry : FilenameCount)
472 Entry.second = Entry.second > 1 ? 1 : 0;
475 for (const NewArchiveMember &M : NewMembers) {
477 raw_string_ostream Out(Header);
479 MemoryBufferRef Buf = M.Buf->getMemBufferRef();
480 StringRef Data = Thin ? "" : Buf.getBuffer();
482 // ld64 expects the members to be 8-byte aligned for 64-bit content and at
483 // least 4-byte aligned for 32-bit content. Opt for the larger encoding
484 // uniformly. This matches the behaviour with cctools and ensures that ld64
485 // is happy with archives that we generate.
486 unsigned MemberPadding =
487 isDarwin(Kind) ? offsetToAlignment(Data.size(), Align(8)) : 0;
488 unsigned TailPadding =
489 offsetToAlignment(Data.size() + MemberPadding, Align(2));
490 StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding);
492 sys::TimePoint<std::chrono::seconds> ModTime;
493 if (UniqueTimestamps)
494 // Increment timestamp for each file of a given name.
495 ModTime = sys::toTimePoint(FilenameCount[M.MemberName]++);
499 uint64_t Size = Buf.getBufferSize() + MemberPadding;
500 if (Size > object::Archive::MaxMemberSize) {
501 std::string StringMsg =
502 "File " + M.MemberName.str() + " exceeds size limit";
503 return make_error<object::GenericBinaryError>(
504 std::move(StringMsg), object::object_error::parse_failed);
507 printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, M,
511 std::vector<unsigned> Symbols;
513 Expected<std::vector<unsigned>> SymbolsOrErr =
514 getSymbols(Buf, SymNames, HasObject);
515 if (auto E = SymbolsOrErr.takeError())
517 Symbols = std::move(*SymbolsOrErr);
520 Pos += Header.size() + Data.size() + Padding.size();
521 Ret.push_back({std::move(Symbols), std::move(Header), Data, Padding});
523 // If there are no symbols, emit an empty symbol table, to satisfy Solaris
524 // tools, older versions of which expect a symbol table in a non-empty
525 // archive, regardless of whether there are any symbols in it.
526 if (HasObject && SymNames.tell() == 0)
527 SymNames << '\0' << '\0' << '\0';
533 static ErrorOr<SmallString<128>> canonicalizePath(StringRef P) {
534 SmallString<128> Ret = P;
535 std::error_code Err = sys::fs::make_absolute(Ret);
538 sys::path::remove_dots(Ret, /*removedotdot*/ true);
542 // Compute the relative path from From to To.
543 Expected<std::string> computeArchiveRelativePath(StringRef From, StringRef To) {
544 ErrorOr<SmallString<128>> PathToOrErr = canonicalizePath(To);
545 ErrorOr<SmallString<128>> DirFromOrErr = canonicalizePath(From);
546 if (!PathToOrErr || !DirFromOrErr)
547 return errorCodeToError(std::error_code(errno, std::generic_category()));
549 const SmallString<128> &PathTo = *PathToOrErr;
550 const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr);
552 // Can't construct a relative path between different roots
553 if (sys::path::root_name(PathTo) != sys::path::root_name(DirFrom))
554 return sys::path::convert_to_slash(PathTo);
556 // Skip common prefixes
558 std::mismatch(sys::path::begin(DirFrom), sys::path::end(DirFrom),
559 sys::path::begin(PathTo));
560 auto FromI = FromTo.first;
561 auto ToI = FromTo.second;
563 // Construct relative path
564 SmallString<128> Relative;
565 for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
566 sys::path::append(Relative, sys::path::Style::posix, "..");
568 for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI)
569 sys::path::append(Relative, sys::path::Style::posix, *ToI);
571 return std::string(Relative.str());
574 static Error writeArchiveToStream(raw_ostream &Out,
575 ArrayRef<NewArchiveMember> NewMembers,
576 bool WriteSymtab, object::Archive::Kind Kind,
577 bool Deterministic, bool Thin) {
578 assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
580 SmallString<0> SymNamesBuf;
581 raw_svector_ostream SymNames(SymNamesBuf);
582 SmallString<0> StringTableBuf;
583 raw_svector_ostream StringTable(StringTableBuf);
585 Expected<std::vector<MemberData>> DataOrErr =
586 computeMemberData(StringTable, SymNames, Kind, Thin, Deterministic,
587 WriteSymtab, NewMembers);
588 if (Error E = DataOrErr.takeError())
590 std::vector<MemberData> &Data = *DataOrErr;
592 if (!StringTableBuf.empty())
593 Data.insert(Data.begin(), computeStringTable(StringTableBuf));
595 // We would like to detect if we need to switch to a 64-bit symbol table.
597 uint64_t MaxOffset = 8; // For the file signature.
598 uint64_t LastOffset = MaxOffset;
599 uint64_t NumSyms = 0;
600 for (const auto &M : Data) {
601 // Record the start of the member's offset
602 LastOffset = MaxOffset;
603 // Account for the size of each part associated with the member.
604 MaxOffset += M.Header.size() + M.Data.size() + M.Padding.size();
605 NumSyms += M.Symbols.size();
608 // We assume 32-bit offsets to see if 32-bit symbols are possible or not.
609 uint64_t SymtabSize = computeSymbolTableSize(Kind, NumSyms, 4, SymNamesBuf);
610 auto computeSymbolTableHeaderSize =
612 SmallString<0> TmpBuf;
613 raw_svector_ostream Tmp(TmpBuf);
614 writeSymbolTableHeader(Tmp, Kind, Deterministic, SymtabSize);
615 return TmpBuf.size();
617 LastOffset += computeSymbolTableHeaderSize() + SymtabSize;
619 // The SYM64 format is used when an archive's member offsets are larger than
620 // 32-bits can hold. The need for this shift in format is detected by
621 // writeArchive. To test this we need to generate a file with a member that
622 // has an offset larger than 32-bits but this demands a very slow test. To
623 // speed the test up we use this environment variable to pretend like the
624 // cutoff happens before 32-bits and instead happens at some much smaller
626 uint64_t Sym64Threshold = 1ULL << 32;
627 const char *Sym64Env = std::getenv("SYM64_THRESHOLD");
629 StringRef(Sym64Env).getAsInteger(10, Sym64Threshold);
631 // If LastOffset isn't going to fit in a 32-bit varible we need to switch
632 // to 64-bit. Note that the file can be larger than 4GB as long as the last
633 // member starts before the 4GB offset.
634 if (LastOffset >= Sym64Threshold) {
635 if (Kind == object::Archive::K_DARWIN)
636 Kind = object::Archive::K_DARWIN64;
638 Kind = object::Archive::K_GNU64;
648 writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf);
650 for (const MemberData &M : Data)
651 Out << M.Header << M.Data << M.Padding;
654 return Error::success();
657 Error writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
658 bool WriteSymtab, object::Archive::Kind Kind,
659 bool Deterministic, bool Thin,
660 std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
661 Expected<sys::fs::TempFile> Temp =
662 sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a");
664 return Temp.takeError();
665 raw_fd_ostream Out(Temp->FD, false);
667 if (Error E = writeArchiveToStream(Out, NewMembers, WriteSymtab, Kind,
668 Deterministic, Thin)) {
669 if (Error DiscardError = Temp->discard())
670 return joinErrors(std::move(E), std::move(DiscardError));
674 // At this point, we no longer need whatever backing memory
675 // was used to generate the NewMembers. On Windows, this buffer
676 // could be a mapped view of the file we want to replace (if
677 // we're updating an existing archive, say). In that case, the
678 // rename would still succeed, but it would leave behind a
679 // temporary file (actually the original file renamed) because
680 // a file cannot be deleted while there's a handle open on it,
681 // only renamed. So by freeing this buffer, this ensures that
682 // the last open handle on the destination file, if any, is
683 // closed before we attempt to rename.
684 OldArchiveBuf.reset();
686 return Temp->keep(ArcName);
689 Expected<std::unique_ptr<MemoryBuffer>>
690 writeArchiveToBuffer(ArrayRef<NewArchiveMember> NewMembers, bool WriteSymtab,
691 object::Archive::Kind Kind, bool Deterministic,
693 SmallVector<char, 0> ArchiveBufferVector;
694 raw_svector_ostream ArchiveStream(ArchiveBufferVector);
696 if (Error E = writeArchiveToStream(ArchiveStream, NewMembers, WriteSymtab,
697 Kind, Deterministic, Thin))
700 return std::make_unique<SmallVectorMemoryBuffer>(
701 std::move(ArchiveBufferVector), /*RequiresNullTerminator=*/false);