xref: /freebsd/contrib/llvm-project/llvm/lib/Object/ArchiveWriter.cpp (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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/ToolOutputFile.h"
31 #include "llvm/Support/raw_ostream.h"
32 
33 #include <map>
34 
35 #if !defined(_MSC_VER) && !defined(__MINGW32__)
36 #include <unistd.h>
37 #else
38 #include <io.h>
39 #endif
40 
41 using namespace llvm;
42 
43 NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef)
44     : Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
45       MemberName(BufRef.getBufferIdentifier()) {}
46 
47 Expected<NewArchiveMember>
48 NewArchiveMember::getOldMember(const object::Archive::Child &OldMember,
49                                bool Deterministic) {
50   Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef();
51   if (!BufOrErr)
52     return BufOrErr.takeError();
53 
54   NewArchiveMember M;
55   M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
56   M.MemberName = M.Buf->getBufferIdentifier();
57   if (!Deterministic) {
58     auto ModTimeOrErr = OldMember.getLastModified();
59     if (!ModTimeOrErr)
60       return ModTimeOrErr.takeError();
61     M.ModTime = ModTimeOrErr.get();
62     Expected<unsigned> UIDOrErr = OldMember.getUID();
63     if (!UIDOrErr)
64       return UIDOrErr.takeError();
65     M.UID = UIDOrErr.get();
66     Expected<unsigned> GIDOrErr = OldMember.getGID();
67     if (!GIDOrErr)
68       return GIDOrErr.takeError();
69     M.GID = GIDOrErr.get();
70     Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
71     if (!AccessModeOrErr)
72       return AccessModeOrErr.takeError();
73     M.Perms = AccessModeOrErr.get();
74   }
75   return std::move(M);
76 }
77 
78 Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
79                                                      bool Deterministic) {
80   sys::fs::file_status Status;
81   auto FDOrErr = sys::fs::openNativeFileForRead(FileName);
82   if (!FDOrErr)
83     return FDOrErr.takeError();
84   sys::fs::file_t FD = *FDOrErr;
85   assert(FD != sys::fs::kInvalidFile);
86 
87   if (auto EC = sys::fs::status(FD, Status))
88     return errorCodeToError(EC);
89 
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));
95 
96   ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
97       MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
98   if (!MemberBufferOrErr)
99     return errorCodeToError(MemberBufferOrErr.getError());
100 
101   if (auto EC = sys::fs::closeFile(FD))
102     return errorCodeToError(EC);
103 
104   NewArchiveMember M;
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();
113   }
114   return std::move(M);
115 }
116 
117 template <typename T>
118 static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
119   uint64_t OldPos = OS.tell();
120   OS << Data;
121   unsigned SizeSoFar = OS.tell() - OldPos;
122   assert(SizeSoFar <= Size && "Data doesn't fit in Size");
123   OS.indent(Size - SizeSoFar);
124 }
125 
126 static bool isDarwin(object::Archive::Kind Kind) {
127   return Kind == object::Archive::K_DARWIN ||
128          Kind == object::Archive::K_DARWIN64;
129 }
130 
131 static bool isBSDLike(object::Archive::Kind Kind) {
132   switch (Kind) {
133   case object::Archive::K_GNU:
134   case object::Archive::K_GNU64:
135     return false;
136   case object::Archive::K_BSD:
137   case object::Archive::K_DARWIN:
138   case object::Archive::K_DARWIN64:
139     return true;
140   case object::Archive::K_AIXBIG:
141   case object::Archive::K_COFF:
142     break;
143   }
144   llvm_unreachable("not supported for writting");
145 }
146 
147 template <class T>
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);
151 }
152 
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);
157 
158   // The format has only 6 chars for uid and gid. Truncate if the provided
159   // values don't fit.
160   printWithSpacePadding(Out, UID % 1000000, 6);
161   printWithSpacePadding(Out, GID % 1000000, 6);
162 
163   printWithSpacePadding(Out, format("%o", Perms), 8);
164   printWithSpacePadding(Out, Size, 10);
165   Out << "`\n";
166 }
167 
168 static void
169 printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name,
170                           const sys::TimePoint<std::chrono::seconds> &ModTime,
171                           unsigned UID, unsigned GID, unsigned Perms,
172                           uint64_t Size) {
173   printWithSpacePadding(Out, Twine(Name) + "/", 16);
174   printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
175 }
176 
177 static void
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);
188   Out << Name;
189   while (Pad--)
190     Out.write(uint8_t(0));
191 }
192 
193 static bool useStringTable(bool Thin, StringRef Name) {
194   return Thin || Name.size() >= 16 || Name.contains('/');
195 }
196 
197 static bool is64BitKind(object::Archive::Kind Kind) {
198   switch (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:
204     return false;
205   case object::Archive::K_DARWIN64:
206   case object::Archive::K_GNU64:
207     return true;
208   }
209   llvm_unreachable("not supported for writting");
210 }
211 
212 static void
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) {
217   if (isBSDLike(Kind))
218     return printBSDMemberHeader(Out, Pos, M.MemberName, ModTime, M.UID, M.GID,
219                                 M.Perms, Size);
220   if (!useStringTable(Thin, M.MemberName))
221     return printGNUSmallMemberHeader(Out, M.MemberName, ModTime, M.UID, M.GID,
222                                      M.Perms, Size);
223   Out << '/';
224   uint64_t NamePos;
225   if (Thin) {
226     NamePos = StringTable.tell();
227     StringTable << M.MemberName << "/\n";
228   } else {
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";
233     }
234     NamePos = Insertion.first->second;
235   }
236   printWithSpacePadding(Out, NamePos, 15);
237   printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size);
238 }
239 
240 namespace {
241 struct MemberData {
242   std::vector<unsigned> Symbols;
243   std::string Header;
244   StringRef Data;
245   StringRef Padding;
246 };
247 } // namespace
248 
249 static MemberData computeStringTable(StringRef Names) {
250   unsigned Size = Names.size();
251   unsigned Pad = offsetToAlignment(Size, Align(2));
252   std::string Header;
253   raw_string_ostream Out(Header);
254   printWithSpacePadding(Out, "//", 48);
255   printWithSpacePadding(Out, Size + Pad, 10);
256   Out << "`\n";
257   Out.flush();
258   return {{}, std::move(Header), Names, Pad ? "\n" : ""};
259 }
260 
261 static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
262   using namespace std::chrono;
263 
264   if (!Deterministic)
265     return time_point_cast<seconds>(system_clock::now());
266   return sys::TimePoint<seconds>();
267 }
268 
269 static bool isArchiveSymbol(const object::BasicSymbolRef &S) {
270   Expected<uint32_t> SymFlagsOrErr = S.getFlags();
271   if (!SymFlagsOrErr)
272     // TODO: Actually report errors helpfully.
273     report_fatal_error(SymFlagsOrErr.takeError());
274   if (*SymFlagsOrErr & object::SymbolRef::SF_FormatSpecific)
275     return false;
276   if (!(*SymFlagsOrErr & object::SymbolRef::SF_Global))
277     return false;
278   if (*SymFlagsOrErr & object::SymbolRef::SF_Undefined)
279     return false;
280   return true;
281 }
282 
283 static void printNBits(raw_ostream &Out, object::Archive::Kind Kind,
284                        uint64_t Val) {
285   if (is64BitKind(Kind))
286     print<uint64_t>(Out, Kind, Val);
287   else
288     print<uint32_t>(Out, Kind, Val);
289 }
290 
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
297   if (isBSDLike(Kind))
298     Size += NumSyms * OffsetSize * 2; // Table
299   else
300     Size += NumSyms * OffsetSize; // Table
301   if (isBSDLike(Kind))
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
306   // uniformly.
307   // We do this for all bsd formats because it simplifies aligning members.
308   uint32_t Pad = offsetToAlignment(Size, Align(isBSDLike(Kind) ? 8 : 2));
309   Size += Pad;
310   if (Padding)
311     *Padding = Pad;
312   return Size;
313 }
314 
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,
320                          Size);
321   } else {
322     const char *Name = is64BitKind(Kind) ? "/SYM64" : "";
323     printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size);
324   }
325 }
326 
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))
333     return;
334 
335   unsigned NumSyms = 0;
336   for (const MemberData &M : Members)
337     NumSyms += M.Symbols.size();
338 
339   uint64_t OffsetSize = is64BitKind(Kind) ? 8 : 4;
340   uint32_t Pad;
341   uint64_t Size = computeSymbolTableSize(Kind, NumSyms, OffsetSize, StringTable, &Pad);
342   writeSymbolTableHeader(Out, Kind, Deterministic, Size);
343 
344   uint64_t Pos = Out.tell() + Size;
345 
346   if (isBSDLike(Kind))
347     printNBits(Out, Kind, NumSyms * 2 * OffsetSize);
348   else
349     printNBits(Out, Kind, NumSyms);
350 
351   for (const MemberData &M : Members) {
352     for (unsigned StringOffset : M.Symbols) {
353       if (isBSDLike(Kind))
354         printNBits(Out, Kind, StringOffset);
355       printNBits(Out, Kind, Pos); // member offset
356     }
357     Pos += M.Header.size() + M.Data.size() + M.Padding.size();
358   }
359 
360   if (isBSDLike(Kind))
361     // byte count of the string table
362     printNBits(Out, Kind, StringTable.size());
363   Out << StringTable;
364 
365   while (Pad--)
366     Out.write(uint8_t(0));
367 }
368 
369 static Expected<std::vector<unsigned>>
370 getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) {
371   std::vector<unsigned> Ret;
372 
373   // In the scenario when LLVMContext is populated SymbolicFile will contain a
374   // reference to it, thus SymbolicFile should be destroyed first.
375   LLVMContext Context;
376   std::unique_ptr<object::SymbolicFile> Obj;
377 
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))
381     return Ret;
382   if (Type == file_magic::bitcode) {
383     auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
384         Buf, file_magic::bitcode, &Context);
385     if (!ObjOrErr)
386       return ObjOrErr.takeError();
387     Obj = std::move(*ObjOrErr);
388   } else {
389     auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf);
390     if (!ObjOrErr)
391       return ObjOrErr.takeError();
392     Obj = std::move(*ObjOrErr);
393   }
394 
395   HasObject = true;
396   for (const object::BasicSymbolRef &S : Obj->symbols()) {
397     if (!isArchiveSymbol(S))
398       continue;
399     Ret.push_back(SymNames.tell());
400     if (Error E = S.printName(SymNames))
401       return std::move(E);
402     SymNames << '\0';
403   }
404   return Ret;
405 }
406 
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'};
412 
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
415   uint64_t Pos = 0;
416 
417   std::vector<MemberData> Ret;
418   bool HasObject = false;
419 
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;
424 
425   // UniqueTimestamps is a special case to improve debugging on Darwin:
426   //
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.
435   //
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.
441   //
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.
449   //
450   // Additionally, timestamp==0 is treated specially, and causes the
451   // timestamp to be ignored as a match criteria.
452   //
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.
456   //
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.
462   //
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;
473   }
474 
475   for (const NewArchiveMember &M : NewMembers) {
476     std::string Header;
477     raw_string_ostream Out(Header);
478 
479     MemoryBufferRef Buf = M.Buf->getMemBufferRef();
480     StringRef Data = Thin ? "" : Buf.getBuffer();
481 
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);
491 
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]++);
496     else
497       ModTime = M.ModTime;
498 
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);
505     }
506 
507     printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, M,
508                       ModTime, Size);
509     Out.flush();
510 
511     std::vector<unsigned> Symbols;
512     if (NeedSymbols) {
513       Expected<std::vector<unsigned>> SymbolsOrErr =
514           getSymbols(Buf, SymNames, HasObject);
515       if (auto E = SymbolsOrErr.takeError())
516         return std::move(E);
517       Symbols = std::move(*SymbolsOrErr);
518     }
519 
520     Pos += Header.size() + Data.size() + Padding.size();
521     Ret.push_back({std::move(Symbols), std::move(Header), Data, Padding});
522   }
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';
528   return Ret;
529 }
530 
531 namespace llvm {
532 
533 static ErrorOr<SmallString<128>> canonicalizePath(StringRef P) {
534   SmallString<128> Ret = P;
535   std::error_code Err = sys::fs::make_absolute(Ret);
536   if (Err)
537     return Err;
538   sys::path::remove_dots(Ret, /*removedotdot*/ true);
539   return Ret;
540 }
541 
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()));
548 
549   const SmallString<128> &PathTo = *PathToOrErr;
550   const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr);
551 
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);
555 
556   // Skip common prefixes
557   auto FromTo =
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;
562 
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, "..");
567 
568   for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI)
569     sys::path::append(Relative, sys::path::Style::posix, *ToI);
570 
571   return std::string(Relative.str());
572 }
573 
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");
579 
580   SmallString<0> SymNamesBuf;
581   raw_svector_ostream SymNames(SymNamesBuf);
582   SmallString<0> StringTableBuf;
583   raw_svector_ostream StringTable(StringTableBuf);
584 
585   Expected<std::vector<MemberData>> DataOrErr =
586       computeMemberData(StringTable, SymNames, Kind, Thin, Deterministic,
587                         WriteSymtab, NewMembers);
588   if (Error E = DataOrErr.takeError())
589     return E;
590   std::vector<MemberData> &Data = *DataOrErr;
591 
592   if (!StringTableBuf.empty())
593     Data.insert(Data.begin(), computeStringTable(StringTableBuf));
594 
595   // We would like to detect if we need to switch to a 64-bit symbol table.
596   if (WriteSymtab) {
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();
606     }
607 
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 =
611         [=] {
612           SmallString<0> TmpBuf;
613           raw_svector_ostream Tmp(TmpBuf);
614           writeSymbolTableHeader(Tmp, Kind, Deterministic, SymtabSize);
615           return TmpBuf.size();
616         };
617     LastOffset += computeSymbolTableHeaderSize() + SymtabSize;
618 
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
625     // value.
626     uint64_t Sym64Threshold = 1ULL << 32;
627     const char *Sym64Env = std::getenv("SYM64_THRESHOLD");
628     if (Sym64Env)
629       StringRef(Sym64Env).getAsInteger(10, Sym64Threshold);
630 
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;
637       else
638         Kind = object::Archive::K_GNU64;
639     }
640   }
641 
642   if (Thin)
643     Out << "!<thin>\n";
644   else
645     Out << "!<arch>\n";
646 
647   if (WriteSymtab)
648     writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf);
649 
650   for (const MemberData &M : Data)
651     Out << M.Header << M.Data << M.Padding;
652 
653   Out.flush();
654   return Error::success();
655 }
656 
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");
663   if (!Temp)
664     return Temp.takeError();
665   raw_fd_ostream Out(Temp->FD, false);
666 
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));
671     return E;
672   }
673 
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();
685 
686   return Temp->keep(ArcName);
687 }
688 
689 Expected<std::unique_ptr<MemoryBuffer>>
690 writeArchiveToBuffer(ArrayRef<NewArchiveMember> NewMembers, bool WriteSymtab,
691                      object::Archive::Kind Kind, bool Deterministic,
692                      bool Thin) {
693   SmallVector<char, 0> ArchiveBufferVector;
694   raw_svector_ostream ArchiveStream(ArchiveBufferVector);
695 
696   if (Error E = writeArchiveToStream(ArchiveStream, NewMembers, WriteSymtab,
697                                      Kind, Deterministic, Thin))
698     return std::move(E);
699 
700   return std::make_unique<SmallVectorMemoryBuffer>(
701       std::move(ArchiveBufferVector), /*RequiresNullTerminator=*/false);
702 }
703 
704 } // namespace llvm
705