1 //===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===//
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 // This file defines the writeArchive function.
10 //
11 //===----------------------------------------------------------------------===//
12 
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/raw_ostream.h"
31 
32 #include <map>
33 
34 #if !defined(_MSC_VER) && !defined(__MINGW32__)
35 #include <unistd.h>
36 #else
37 #include <io.h>
38 #endif
39 
40 using namespace llvm;
41 
42 NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef)
43     : Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
44       MemberName(BufRef.getBufferIdentifier()) {}
45 
46 Expected<NewArchiveMember>
47 NewArchiveMember::getOldMember(const object::Archive::Child &OldMember,
48                                bool Deterministic) {
49   Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef();
50   if (!BufOrErr)
51     return BufOrErr.takeError();
52 
53   NewArchiveMember M;
54   M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
55   M.MemberName = M.Buf->getBufferIdentifier();
56   if (!Deterministic) {
57     auto ModTimeOrErr = OldMember.getLastModified();
58     if (!ModTimeOrErr)
59       return ModTimeOrErr.takeError();
60     M.ModTime = ModTimeOrErr.get();
61     Expected<unsigned> UIDOrErr = OldMember.getUID();
62     if (!UIDOrErr)
63       return UIDOrErr.takeError();
64     M.UID = UIDOrErr.get();
65     Expected<unsigned> GIDOrErr = OldMember.getGID();
66     if (!GIDOrErr)
67       return GIDOrErr.takeError();
68     M.GID = GIDOrErr.get();
69     Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
70     if (!AccessModeOrErr)
71       return AccessModeOrErr.takeError();
72     M.Perms = AccessModeOrErr.get();
73   }
74   return std::move(M);
75 }
76 
77 Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
78                                                      bool Deterministic) {
79   sys::fs::file_status Status;
80   auto FDOrErr = sys::fs::openNativeFileForRead(FileName);
81   if (!FDOrErr)
82     return FDOrErr.takeError();
83   sys::fs::file_t FD = *FDOrErr;
84   assert(FD != sys::fs::kInvalidFile);
85 
86   if (auto EC = sys::fs::status(FD, Status))
87     return errorCodeToError(EC);
88 
89   // Opening a directory doesn't make sense. Let it fail.
90   // Linux cannot open directories with open(2), although
91   // cygwin and *bsd can.
92   if (Status.type() == sys::fs::file_type::directory_file)
93     return errorCodeToError(make_error_code(errc::is_a_directory));
94 
95   ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
96       MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
97   if (!MemberBufferOrErr)
98     return errorCodeToError(MemberBufferOrErr.getError());
99 
100   if (auto EC = sys::fs::closeFile(FD))
101     return errorCodeToError(EC);
102 
103   NewArchiveMember M;
104   M.Buf = std::move(*MemberBufferOrErr);
105   M.MemberName = M.Buf->getBufferIdentifier();
106   if (!Deterministic) {
107     M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
108         Status.getLastModificationTime());
109     M.UID = Status.getUser();
110     M.GID = Status.getGroup();
111     M.Perms = Status.permissions();
112   }
113   return std::move(M);
114 }
115 
116 template <typename T>
117 static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
118   uint64_t OldPos = OS.tell();
119   OS << Data;
120   unsigned SizeSoFar = OS.tell() - OldPos;
121   assert(SizeSoFar <= Size && "Data doesn't fit in Size");
122   OS.indent(Size - SizeSoFar);
123 }
124 
125 static bool isDarwin(object::Archive::Kind Kind) {
126   return Kind == object::Archive::K_DARWIN ||
127          Kind == object::Archive::K_DARWIN64;
128 }
129 
130 static bool isBSDLike(object::Archive::Kind Kind) {
131   switch (Kind) {
132   case object::Archive::K_GNU:
133   case object::Archive::K_GNU64:
134     return false;
135   case object::Archive::K_BSD:
136   case object::Archive::K_DARWIN:
137   case object::Archive::K_DARWIN64:
138     return true;
139   case object::Archive::K_AIXBIG:
140   case object::Archive::K_COFF:
141     break;
142   }
143   llvm_unreachable("not supported for writting");
144 }
145 
146 template <class T>
147 static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) {
148   support::endian::write(Out, Val,
149                          isBSDLike(Kind) ? support::little : support::big);
150 }
151 
152 static void printRestOfMemberHeader(
153     raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime,
154     unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
155   printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
156 
157   // The format has only 6 chars for uid and gid. Truncate if the provided
158   // values don't fit.
159   printWithSpacePadding(Out, UID % 1000000, 6);
160   printWithSpacePadding(Out, GID % 1000000, 6);
161 
162   printWithSpacePadding(Out, format("%o", Perms), 8);
163   printWithSpacePadding(Out, Size, 10);
164   Out << "`\n";
165 }
166 
167 static void
168 printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name,
169                           const sys::TimePoint<std::chrono::seconds> &ModTime,
170                           unsigned UID, unsigned GID, unsigned Perms,
171                           uint64_t Size) {
172   printWithSpacePadding(Out, Twine(Name) + "/", 16);
173   printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
174 }
175 
176 static void
177 printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name,
178                      const sys::TimePoint<std::chrono::seconds> &ModTime,
179                      unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
180   uint64_t PosAfterHeader = Pos + 60 + Name.size();
181   // Pad so that even 64 bit object files are aligned.
182   unsigned Pad = offsetToAlignment(PosAfterHeader, Align(8));
183   unsigned NameWithPadding = Name.size() + Pad;
184   printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
185   printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
186                           NameWithPadding + Size);
187   Out << Name;
188   while (Pad--)
189     Out.write(uint8_t(0));
190 }
191 
192 static bool useStringTable(bool Thin, StringRef Name) {
193   return Thin || Name.size() >= 16 || Name.contains('/');
194 }
195 
196 static bool is64BitKind(object::Archive::Kind Kind) {
197   switch (Kind) {
198   case object::Archive::K_GNU:
199   case object::Archive::K_BSD:
200   case object::Archive::K_DARWIN:
201   case object::Archive::K_COFF:
202   case object::Archive::K_AIXBIG:
203     return false;
204   case object::Archive::K_DARWIN64:
205   case object::Archive::K_GNU64:
206     return true;
207   }
208   llvm_unreachable("not supported for writting");
209 }
210 
211 static void
212 printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable,
213                   StringMap<uint64_t> &MemberNames, object::Archive::Kind Kind,
214                   bool Thin, const NewArchiveMember &M,
215                   sys::TimePoint<std::chrono::seconds> ModTime, uint64_t Size) {
216   if (isBSDLike(Kind))
217     return printBSDMemberHeader(Out, Pos, M.MemberName, ModTime, M.UID, M.GID,
218                                 M.Perms, Size);
219   if (!useStringTable(Thin, M.MemberName))
220     return printGNUSmallMemberHeader(Out, M.MemberName, ModTime, M.UID, M.GID,
221                                      M.Perms, Size);
222   Out << '/';
223   uint64_t NamePos;
224   if (Thin) {
225     NamePos = StringTable.tell();
226     StringTable << M.MemberName << "/\n";
227   } else {
228     auto Insertion = MemberNames.insert({M.MemberName, uint64_t(0)});
229     if (Insertion.second) {
230       Insertion.first->second = StringTable.tell();
231       StringTable << M.MemberName << "/\n";
232     }
233     NamePos = Insertion.first->second;
234   }
235   printWithSpacePadding(Out, NamePos, 15);
236   printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size);
237 }
238 
239 namespace {
240 struct MemberData {
241   std::vector<unsigned> Symbols;
242   std::string Header;
243   StringRef Data;
244   StringRef Padding;
245 };
246 } // namespace
247 
248 static MemberData computeStringTable(StringRef Names) {
249   unsigned Size = Names.size();
250   unsigned Pad = offsetToAlignment(Size, Align(2));
251   std::string Header;
252   raw_string_ostream Out(Header);
253   printWithSpacePadding(Out, "//", 48);
254   printWithSpacePadding(Out, Size + Pad, 10);
255   Out << "`\n";
256   Out.flush();
257   return {{}, std::move(Header), Names, Pad ? "\n" : ""};
258 }
259 
260 static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
261   using namespace std::chrono;
262 
263   if (!Deterministic)
264     return time_point_cast<seconds>(system_clock::now());
265   return sys::TimePoint<seconds>();
266 }
267 
268 static bool isArchiveSymbol(const object::BasicSymbolRef &S) {
269   Expected<uint32_t> SymFlagsOrErr = S.getFlags();
270   if (!SymFlagsOrErr)
271     // TODO: Actually report errors helpfully.
272     report_fatal_error(SymFlagsOrErr.takeError());
273   if (*SymFlagsOrErr & object::SymbolRef::SF_FormatSpecific)
274     return false;
275   if (!(*SymFlagsOrErr & object::SymbolRef::SF_Global))
276     return false;
277   if (*SymFlagsOrErr & object::SymbolRef::SF_Undefined)
278     return false;
279   return true;
280 }
281 
282 static void printNBits(raw_ostream &Out, object::Archive::Kind Kind,
283                        uint64_t Val) {
284   if (is64BitKind(Kind))
285     print<uint64_t>(Out, Kind, Val);
286   else
287     print<uint32_t>(Out, Kind, Val);
288 }
289 
290 static uint64_t computeSymbolTableSize(object::Archive::Kind Kind,
291                                        uint64_t NumSyms, uint64_t OffsetSize,
292                                        StringRef StringTable,
293                                        uint32_t *Padding = nullptr) {
294   assert((OffsetSize == 4 || OffsetSize == 8) && "Unsupported OffsetSize");
295   uint64_t Size = OffsetSize; // Number of entries
296   if (isBSDLike(Kind))
297     Size += NumSyms * OffsetSize * 2; // Table
298   else
299     Size += NumSyms * OffsetSize; // Table
300   if (isBSDLike(Kind))
301     Size += OffsetSize; // byte count
302   Size += StringTable.size();
303   // ld64 expects the members to be 8-byte aligned for 64-bit content and at
304   // least 4-byte aligned for 32-bit content.  Opt for the larger encoding
305   // uniformly.
306   // We do this for all bsd formats because it simplifies aligning members.
307   uint32_t Pad = offsetToAlignment(Size, Align(isBSDLike(Kind) ? 8 : 2));
308   Size += Pad;
309   if (Padding)
310     *Padding = Pad;
311   return Size;
312 }
313 
314 static void writeSymbolTableHeader(raw_ostream &Out, object::Archive::Kind Kind,
315                                    bool Deterministic, uint64_t Size) {
316   if (isBSDLike(Kind)) {
317     const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF";
318     printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0,
319                          Size);
320   } else {
321     const char *Name = is64BitKind(Kind) ? "/SYM64" : "";
322     printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size);
323   }
324 }
325 
326 static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind,
327                              bool Deterministic, ArrayRef<MemberData> Members,
328                              StringRef StringTable) {
329   // We don't write a symbol table on an archive with no members -- except on
330   // Darwin, where the linker will abort unless the archive has a symbol table.
331   if (StringTable.empty() && !isDarwin(Kind))
332     return;
333 
334   unsigned NumSyms = 0;
335   for (const MemberData &M : Members)
336     NumSyms += M.Symbols.size();
337 
338   uint64_t OffsetSize = is64BitKind(Kind) ? 8 : 4;
339   uint32_t Pad;
340   uint64_t Size = computeSymbolTableSize(Kind, NumSyms, OffsetSize, StringTable, &Pad);
341   writeSymbolTableHeader(Out, Kind, Deterministic, Size);
342 
343   uint64_t Pos = Out.tell() + Size;
344 
345   if (isBSDLike(Kind))
346     printNBits(Out, Kind, NumSyms * 2 * OffsetSize);
347   else
348     printNBits(Out, Kind, NumSyms);
349 
350   for (const MemberData &M : Members) {
351     for (unsigned StringOffset : M.Symbols) {
352       if (isBSDLike(Kind))
353         printNBits(Out, Kind, StringOffset);
354       printNBits(Out, Kind, Pos); // member offset
355     }
356     Pos += M.Header.size() + M.Data.size() + M.Padding.size();
357   }
358 
359   if (isBSDLike(Kind))
360     // byte count of the string table
361     printNBits(Out, Kind, StringTable.size());
362   Out << StringTable;
363 
364   while (Pad--)
365     Out.write(uint8_t(0));
366 }
367 
368 static Expected<std::vector<unsigned>>
369 getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) {
370   std::vector<unsigned> Ret;
371 
372   // In the scenario when LLVMContext is populated SymbolicFile will contain a
373   // reference to it, thus SymbolicFile should be destroyed first.
374   LLVMContext Context;
375   std::unique_ptr<object::SymbolicFile> Obj;
376 
377   const file_magic Type = identify_magic(Buf.getBuffer());
378   // Treat unsupported file types as having no symbols.
379   if (!object::SymbolicFile::isSymbolicFile(Type, &Context))
380     return Ret;
381   if (Type == file_magic::bitcode) {
382     auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
383         Buf, file_magic::bitcode, &Context);
384     if (!ObjOrErr)
385       return ObjOrErr.takeError();
386     Obj = std::move(*ObjOrErr);
387   } else {
388     auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf);
389     if (!ObjOrErr)
390       return ObjOrErr.takeError();
391     Obj = std::move(*ObjOrErr);
392   }
393 
394   HasObject = true;
395   for (const object::BasicSymbolRef &S : Obj->symbols()) {
396     if (!isArchiveSymbol(S))
397       continue;
398     Ret.push_back(SymNames.tell());
399     if (Error E = S.printName(SymNames))
400       return std::move(E);
401     SymNames << '\0';
402   }
403   return Ret;
404 }
405 
406 static Expected<std::vector<MemberData>>
407 computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames,
408                   object::Archive::Kind Kind, bool Thin, bool Deterministic,
409                   bool NeedSymbols, ArrayRef<NewArchiveMember> NewMembers) {
410   static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};
411 
412   // This ignores the symbol table, but we only need the value mod 8 and the
413   // symbol table is aligned to be a multiple of 8 bytes
414   uint64_t Pos = 0;
415 
416   std::vector<MemberData> Ret;
417   bool HasObject = false;
418 
419   // Deduplicate long member names in the string table and reuse earlier name
420   // offsets. This especially saves space for COFF Import libraries where all
421   // members have the same name.
422   StringMap<uint64_t> MemberNames;
423 
424   // UniqueTimestamps is a special case to improve debugging on Darwin:
425   //
426   // The Darwin linker does not link debug info into the final
427   // binary. Instead, it emits entries of type N_OSO in in the output
428   // binary's symbol table, containing references to the linked-in
429   // object files. Using that reference, the debugger can read the
430   // debug data directly from the object files. Alternatively, an
431   // invocation of 'dsymutil' will link the debug data from the object
432   // files into a dSYM bundle, which can be loaded by the debugger,
433   // instead of the object files.
434   //
435   // For an object file, the N_OSO entries contain the absolute path
436   // path to the file, and the file's timestamp. For an object
437   // included in an archive, the path is formatted like
438   // "/absolute/path/to/archive.a(member.o)", and the timestamp is the
439   // archive member's timestamp, rather than the archive's timestamp.
440   //
441   // However, this doesn't always uniquely identify an object within
442   // an archive -- an archive file can have multiple entries with the
443   // same filename. (This will happen commonly if the original object
444   // files started in different directories.) The only way they get
445   // distinguished, then, is via the timestamp. But this process is
446   // unable to find the correct object file in the archive when there
447   // are two files of the same name and timestamp.
448   //
449   // Additionally, timestamp==0 is treated specially, and causes the
450   // timestamp to be ignored as a match criteria.
451   //
452   // That will "usually" work out okay when creating an archive not in
453   // deterministic timestamp mode, because the objects will probably
454   // have been created at different timestamps.
455   //
456   // To ameliorate this problem, in deterministic archive mode (which
457   // is the default), on Darwin we will emit a unique non-zero
458   // timestamp for each entry with a duplicated name. This is still
459   // deterministic: the only thing affecting that timestamp is the
460   // order of the files in the resultant archive.
461   //
462   // See also the functions that handle the lookup:
463   // in lldb: ObjectContainerBSDArchive::Archive::FindObject()
464   // in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers().
465   bool UniqueTimestamps = Deterministic && isDarwin(Kind);
466   std::map<StringRef, unsigned> FilenameCount;
467   if (UniqueTimestamps) {
468     for (const NewArchiveMember &M : NewMembers)
469       FilenameCount[M.MemberName]++;
470     for (auto &Entry : FilenameCount)
471       Entry.second = Entry.second > 1 ? 1 : 0;
472   }
473 
474   for (const NewArchiveMember &M : NewMembers) {
475     std::string Header;
476     raw_string_ostream Out(Header);
477 
478     MemoryBufferRef Buf = M.Buf->getMemBufferRef();
479     StringRef Data = Thin ? "" : Buf.getBuffer();
480 
481     // ld64 expects the members to be 8-byte aligned for 64-bit content and at
482     // least 4-byte aligned for 32-bit content.  Opt for the larger encoding
483     // uniformly.  This matches the behaviour with cctools and ensures that ld64
484     // is happy with archives that we generate.
485     unsigned MemberPadding =
486         isDarwin(Kind) ? offsetToAlignment(Data.size(), Align(8)) : 0;
487     unsigned TailPadding =
488         offsetToAlignment(Data.size() + MemberPadding, Align(2));
489     StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding);
490 
491     sys::TimePoint<std::chrono::seconds> ModTime;
492     if (UniqueTimestamps)
493       // Increment timestamp for each file of a given name.
494       ModTime = sys::toTimePoint(FilenameCount[M.MemberName]++);
495     else
496       ModTime = M.ModTime;
497 
498     uint64_t Size = Buf.getBufferSize() + MemberPadding;
499     if (Size > object::Archive::MaxMemberSize) {
500       std::string StringMsg =
501           "File " + M.MemberName.str() + " exceeds size limit";
502       return make_error<object::GenericBinaryError>(
503           std::move(StringMsg), object::object_error::parse_failed);
504     }
505 
506     printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, M,
507                       ModTime, Size);
508     Out.flush();
509 
510     std::vector<unsigned> Symbols;
511     if (NeedSymbols) {
512       Expected<std::vector<unsigned>> SymbolsOrErr =
513           getSymbols(Buf, SymNames, HasObject);
514       if (auto E = SymbolsOrErr.takeError())
515         return std::move(E);
516       Symbols = std::move(*SymbolsOrErr);
517     }
518 
519     Pos += Header.size() + Data.size() + Padding.size();
520     Ret.push_back({std::move(Symbols), std::move(Header), Data, Padding});
521   }
522   // If there are no symbols, emit an empty symbol table, to satisfy Solaris
523   // tools, older versions of which expect a symbol table in a non-empty
524   // archive, regardless of whether there are any symbols in it.
525   if (HasObject && SymNames.tell() == 0)
526     SymNames << '\0' << '\0' << '\0';
527   return Ret;
528 }
529 
530 namespace llvm {
531 
532 static ErrorOr<SmallString<128>> canonicalizePath(StringRef P) {
533   SmallString<128> Ret = P;
534   std::error_code Err = sys::fs::make_absolute(Ret);
535   if (Err)
536     return Err;
537   sys::path::remove_dots(Ret, /*removedotdot*/ true);
538   return Ret;
539 }
540 
541 // Compute the relative path from From to To.
542 Expected<std::string> computeArchiveRelativePath(StringRef From, StringRef To) {
543   ErrorOr<SmallString<128>> PathToOrErr = canonicalizePath(To);
544   ErrorOr<SmallString<128>> DirFromOrErr = canonicalizePath(From);
545   if (!PathToOrErr || !DirFromOrErr)
546     return errorCodeToError(std::error_code(errno, std::generic_category()));
547 
548   const SmallString<128> &PathTo = *PathToOrErr;
549   const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr);
550 
551   // Can't construct a relative path between different roots
552   if (sys::path::root_name(PathTo) != sys::path::root_name(DirFrom))
553     return sys::path::convert_to_slash(PathTo);
554 
555   // Skip common prefixes
556   auto FromTo =
557       std::mismatch(sys::path::begin(DirFrom), sys::path::end(DirFrom),
558                     sys::path::begin(PathTo));
559   auto FromI = FromTo.first;
560   auto ToI = FromTo.second;
561 
562   // Construct relative path
563   SmallString<128> Relative;
564   for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
565     sys::path::append(Relative, sys::path::Style::posix, "..");
566 
567   for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI)
568     sys::path::append(Relative, sys::path::Style::posix, *ToI);
569 
570   return std::string(Relative.str());
571 }
572 
573 static Error writeArchiveToStream(raw_ostream &Out,
574                                   ArrayRef<NewArchiveMember> NewMembers,
575                                   bool WriteSymtab, object::Archive::Kind Kind,
576                                   bool Deterministic, bool Thin) {
577   assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
578 
579   SmallString<0> SymNamesBuf;
580   raw_svector_ostream SymNames(SymNamesBuf);
581   SmallString<0> StringTableBuf;
582   raw_svector_ostream StringTable(StringTableBuf);
583 
584   Expected<std::vector<MemberData>> DataOrErr =
585       computeMemberData(StringTable, SymNames, Kind, Thin, Deterministic,
586                         WriteSymtab, NewMembers);
587   if (Error E = DataOrErr.takeError())
588     return E;
589   std::vector<MemberData> &Data = *DataOrErr;
590 
591   if (!StringTableBuf.empty())
592     Data.insert(Data.begin(), computeStringTable(StringTableBuf));
593 
594   // We would like to detect if we need to switch to a 64-bit symbol table.
595   if (WriteSymtab) {
596     uint64_t MaxOffset = 8; // For the file signature.
597     uint64_t LastOffset = MaxOffset;
598     uint64_t NumSyms = 0;
599     for (const auto &M : Data) {
600       // Record the start of the member's offset
601       LastOffset = MaxOffset;
602       // Account for the size of each part associated with the member.
603       MaxOffset += M.Header.size() + M.Data.size() + M.Padding.size();
604       NumSyms += M.Symbols.size();
605     }
606 
607     // We assume 32-bit offsets to see if 32-bit symbols are possible or not.
608     uint64_t SymtabSize = computeSymbolTableSize(Kind, NumSyms, 4, SymNamesBuf);
609     auto computeSymbolTableHeaderSize =
610         [=] {
611           SmallString<0> TmpBuf;
612           raw_svector_ostream Tmp(TmpBuf);
613           writeSymbolTableHeader(Tmp, Kind, Deterministic, SymtabSize);
614           return TmpBuf.size();
615         };
616     LastOffset += computeSymbolTableHeaderSize() + SymtabSize;
617 
618     // The SYM64 format is used when an archive's member offsets are larger than
619     // 32-bits can hold. The need for this shift in format is detected by
620     // writeArchive. To test this we need to generate a file with a member that
621     // has an offset larger than 32-bits but this demands a very slow test. To
622     // speed the test up we use this environment variable to pretend like the
623     // cutoff happens before 32-bits and instead happens at some much smaller
624     // value.
625     uint64_t Sym64Threshold = 1ULL << 32;
626     const char *Sym64Env = std::getenv("SYM64_THRESHOLD");
627     if (Sym64Env)
628       StringRef(Sym64Env).getAsInteger(10, Sym64Threshold);
629 
630     // If LastOffset isn't going to fit in a 32-bit varible we need to switch
631     // to 64-bit. Note that the file can be larger than 4GB as long as the last
632     // member starts before the 4GB offset.
633     if (LastOffset >= Sym64Threshold) {
634       if (Kind == object::Archive::K_DARWIN)
635         Kind = object::Archive::K_DARWIN64;
636       else
637         Kind = object::Archive::K_GNU64;
638     }
639   }
640 
641   if (Thin)
642     Out << "!<thin>\n";
643   else
644     Out << "!<arch>\n";
645 
646   if (WriteSymtab)
647     writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf);
648 
649   for (const MemberData &M : Data)
650     Out << M.Header << M.Data << M.Padding;
651 
652   Out.flush();
653   return Error::success();
654 }
655 
656 Error writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
657                    bool WriteSymtab, object::Archive::Kind Kind,
658                    bool Deterministic, bool Thin,
659                    std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
660   Expected<sys::fs::TempFile> Temp =
661       sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a");
662   if (!Temp)
663     return Temp.takeError();
664   raw_fd_ostream Out(Temp->FD, false);
665 
666   if (Error E = writeArchiveToStream(Out, NewMembers, WriteSymtab, Kind,
667                                      Deterministic, Thin)) {
668     if (Error DiscardError = Temp->discard())
669       return joinErrors(std::move(E), std::move(DiscardError));
670     return E;
671   }
672 
673   // At this point, we no longer need whatever backing memory
674   // was used to generate the NewMembers. On Windows, this buffer
675   // could be a mapped view of the file we want to replace (if
676   // we're updating an existing archive, say). In that case, the
677   // rename would still succeed, but it would leave behind a
678   // temporary file (actually the original file renamed) because
679   // a file cannot be deleted while there's a handle open on it,
680   // only renamed. So by freeing this buffer, this ensures that
681   // the last open handle on the destination file, if any, is
682   // closed before we attempt to rename.
683   OldArchiveBuf.reset();
684 
685   return Temp->keep(ArcName);
686 }
687 
688 Expected<std::unique_ptr<MemoryBuffer>>
689 writeArchiveToBuffer(ArrayRef<NewArchiveMember> NewMembers, bool WriteSymtab,
690                      object::Archive::Kind Kind, bool Deterministic,
691                      bool Thin) {
692   SmallVector<char, 0> ArchiveBufferVector;
693   raw_svector_ostream ArchiveStream(ArchiveBufferVector);
694 
695   if (Error E = writeArchiveToStream(ArchiveStream, NewMembers, WriteSymtab,
696                                      Kind, Deterministic, Thin))
697     return std::move(E);
698 
699   return std::make_unique<SmallVectorMemoryBuffer>(
700       std::move(ArchiveBufferVector), /*RequiresNullTerminator=*/false);
701 }
702 
703 } // namespace llvm
704