xref: /freebsd/contrib/llvm-project/llvm/lib/Object/ArchiveWriter.cpp (revision 5b56413d04e608379c9a306373554a8e4d321bc0)
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/COFF.h"
21 #include "llvm/Object/COFFImportFile.h"
22 #include "llvm/Object/Error.h"
23 #include "llvm/Object/IRObjectFile.h"
24 #include "llvm/Object/MachO.h"
25 #include "llvm/Object/ObjectFile.h"
26 #include "llvm/Object/SymbolicFile.h"
27 #include "llvm/Object/XCOFFObjectFile.h"
28 #include "llvm/Support/Alignment.h"
29 #include "llvm/Support/EndianStream.h"
30 #include "llvm/Support/Errc.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/Format.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/Path.h"
35 #include "llvm/Support/SmallVectorMemoryBuffer.h"
36 #include "llvm/Support/raw_ostream.h"
37 
38 #include <cerrno>
39 #include <map>
40 
41 #if !defined(_MSC_VER) && !defined(__MINGW32__)
42 #include <unistd.h>
43 #else
44 #include <io.h>
45 #endif
46 
47 using namespace llvm;
48 using namespace llvm::object;
49 
50 struct SymMap {
51   bool UseECMap;
52   std::map<std::string, uint16_t> Map;
53   std::map<std::string, uint16_t> ECMap;
54 };
55 
56 NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef)
57     : Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
58       MemberName(BufRef.getBufferIdentifier()) {}
59 
60 object::Archive::Kind NewArchiveMember::detectKindFromObject() const {
61   auto MemBufferRef = this->Buf->getMemBufferRef();
62   Expected<std::unique_ptr<object::ObjectFile>> OptionalObject =
63       object::ObjectFile::createObjectFile(MemBufferRef);
64 
65   if (OptionalObject)
66     return isa<object::MachOObjectFile>(**OptionalObject)
67                ? object::Archive::K_DARWIN
68                : (isa<object::XCOFFObjectFile>(**OptionalObject)
69                       ? object::Archive::K_AIXBIG
70                       : object::Archive::K_GNU);
71 
72   // Squelch the error in case we had a non-object file.
73   consumeError(OptionalObject.takeError());
74 
75   // If we're adding a bitcode file to the archive, detect the Archive kind
76   // based on the target triple.
77   LLVMContext Context;
78   if (identify_magic(MemBufferRef.getBuffer()) == file_magic::bitcode) {
79     if (auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
80             MemBufferRef, file_magic::bitcode, &Context)) {
81       auto &IRObject = cast<object::IRObjectFile>(**ObjOrErr);
82       auto TargetTriple = Triple(IRObject.getTargetTriple());
83       return TargetTriple.isOSDarwin()
84                  ? object::Archive::K_DARWIN
85                  : (TargetTriple.isOSAIX() ? object::Archive::K_AIXBIG
86                                            : object::Archive::K_GNU);
87     } else {
88       // Squelch the error in case this was not a SymbolicFile.
89       consumeError(ObjOrErr.takeError());
90     }
91   }
92 
93   return object::Archive::getDefaultKindForHost();
94 }
95 
96 Expected<NewArchiveMember>
97 NewArchiveMember::getOldMember(const object::Archive::Child &OldMember,
98                                bool Deterministic) {
99   Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef();
100   if (!BufOrErr)
101     return BufOrErr.takeError();
102 
103   NewArchiveMember M;
104   M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
105   M.MemberName = M.Buf->getBufferIdentifier();
106   if (!Deterministic) {
107     auto ModTimeOrErr = OldMember.getLastModified();
108     if (!ModTimeOrErr)
109       return ModTimeOrErr.takeError();
110     M.ModTime = ModTimeOrErr.get();
111     Expected<unsigned> UIDOrErr = OldMember.getUID();
112     if (!UIDOrErr)
113       return UIDOrErr.takeError();
114     M.UID = UIDOrErr.get();
115     Expected<unsigned> GIDOrErr = OldMember.getGID();
116     if (!GIDOrErr)
117       return GIDOrErr.takeError();
118     M.GID = GIDOrErr.get();
119     Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
120     if (!AccessModeOrErr)
121       return AccessModeOrErr.takeError();
122     M.Perms = AccessModeOrErr.get();
123   }
124   return std::move(M);
125 }
126 
127 Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
128                                                      bool Deterministic) {
129   sys::fs::file_status Status;
130   auto FDOrErr = sys::fs::openNativeFileForRead(FileName);
131   if (!FDOrErr)
132     return FDOrErr.takeError();
133   sys::fs::file_t FD = *FDOrErr;
134   assert(FD != sys::fs::kInvalidFile);
135 
136   if (auto EC = sys::fs::status(FD, Status))
137     return errorCodeToError(EC);
138 
139   // Opening a directory doesn't make sense. Let it fail.
140   // Linux cannot open directories with open(2), although
141   // cygwin and *bsd can.
142   if (Status.type() == sys::fs::file_type::directory_file)
143     return errorCodeToError(make_error_code(errc::is_a_directory));
144 
145   ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
146       MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
147   if (!MemberBufferOrErr)
148     return errorCodeToError(MemberBufferOrErr.getError());
149 
150   if (auto EC = sys::fs::closeFile(FD))
151     return errorCodeToError(EC);
152 
153   NewArchiveMember M;
154   M.Buf = std::move(*MemberBufferOrErr);
155   M.MemberName = M.Buf->getBufferIdentifier();
156   if (!Deterministic) {
157     M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
158         Status.getLastModificationTime());
159     M.UID = Status.getUser();
160     M.GID = Status.getGroup();
161     M.Perms = Status.permissions();
162   }
163   return std::move(M);
164 }
165 
166 template <typename T>
167 static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
168   uint64_t OldPos = OS.tell();
169   OS << Data;
170   unsigned SizeSoFar = OS.tell() - OldPos;
171   assert(SizeSoFar <= Size && "Data doesn't fit in Size");
172   OS.indent(Size - SizeSoFar);
173 }
174 
175 static bool isDarwin(object::Archive::Kind Kind) {
176   return Kind == object::Archive::K_DARWIN ||
177          Kind == object::Archive::K_DARWIN64;
178 }
179 
180 static bool isAIXBigArchive(object::Archive::Kind Kind) {
181   return Kind == object::Archive::K_AIXBIG;
182 }
183 
184 static bool isCOFFArchive(object::Archive::Kind Kind) {
185   return Kind == object::Archive::K_COFF;
186 }
187 
188 static bool isBSDLike(object::Archive::Kind Kind) {
189   switch (Kind) {
190   case object::Archive::K_GNU:
191   case object::Archive::K_GNU64:
192   case object::Archive::K_AIXBIG:
193   case object::Archive::K_COFF:
194     return false;
195   case object::Archive::K_BSD:
196   case object::Archive::K_DARWIN:
197   case object::Archive::K_DARWIN64:
198     return true;
199   }
200   llvm_unreachable("not supported for writting");
201 }
202 
203 template <class T>
204 static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) {
205   support::endian::write(Out, Val,
206                          isBSDLike(Kind) ? llvm::endianness::little
207                                          : llvm::endianness::big);
208 }
209 
210 template <class T> static void printLE(raw_ostream &Out, T Val) {
211   support::endian::write(Out, Val, llvm::endianness::little);
212 }
213 
214 static void printRestOfMemberHeader(
215     raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime,
216     unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
217   printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
218 
219   // The format has only 6 chars for uid and gid. Truncate if the provided
220   // values don't fit.
221   printWithSpacePadding(Out, UID % 1000000, 6);
222   printWithSpacePadding(Out, GID % 1000000, 6);
223 
224   printWithSpacePadding(Out, format("%o", Perms), 8);
225   printWithSpacePadding(Out, Size, 10);
226   Out << "`\n";
227 }
228 
229 static void
230 printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name,
231                           const sys::TimePoint<std::chrono::seconds> &ModTime,
232                           unsigned UID, unsigned GID, unsigned Perms,
233                           uint64_t Size) {
234   printWithSpacePadding(Out, Twine(Name) + "/", 16);
235   printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
236 }
237 
238 static void
239 printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name,
240                      const sys::TimePoint<std::chrono::seconds> &ModTime,
241                      unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
242   uint64_t PosAfterHeader = Pos + 60 + Name.size();
243   // Pad so that even 64 bit object files are aligned.
244   unsigned Pad = offsetToAlignment(PosAfterHeader, Align(8));
245   unsigned NameWithPadding = Name.size() + Pad;
246   printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
247   printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
248                           NameWithPadding + Size);
249   Out << Name;
250   while (Pad--)
251     Out.write(uint8_t(0));
252 }
253 
254 static void
255 printBigArchiveMemberHeader(raw_ostream &Out, StringRef Name,
256                             const sys::TimePoint<std::chrono::seconds> &ModTime,
257                             unsigned UID, unsigned GID, unsigned Perms,
258                             uint64_t Size, uint64_t PrevOffset,
259                             uint64_t NextOffset) {
260   unsigned NameLen = Name.size();
261 
262   printWithSpacePadding(Out, Size, 20);           // File member size
263   printWithSpacePadding(Out, NextOffset, 20);     // Next member header offset
264   printWithSpacePadding(Out, PrevOffset, 20); // Previous member header offset
265   printWithSpacePadding(Out, sys::toTimeT(ModTime), 12); // File member date
266   // The big archive format has 12 chars for uid and gid.
267   printWithSpacePadding(Out, UID % 1000000000000, 12);   // UID
268   printWithSpacePadding(Out, GID % 1000000000000, 12);   // GID
269   printWithSpacePadding(Out, format("%o", Perms), 12);   // Permission
270   printWithSpacePadding(Out, NameLen, 4);                // Name length
271   if (NameLen) {
272     printWithSpacePadding(Out, Name, NameLen); // Name
273     if (NameLen % 2)
274       Out.write(uint8_t(0)); // Null byte padding
275   }
276   Out << "`\n"; // Terminator
277 }
278 
279 static bool useStringTable(bool Thin, StringRef Name) {
280   return Thin || Name.size() >= 16 || Name.contains('/');
281 }
282 
283 static bool is64BitKind(object::Archive::Kind Kind) {
284   switch (Kind) {
285   case object::Archive::K_GNU:
286   case object::Archive::K_BSD:
287   case object::Archive::K_DARWIN:
288   case object::Archive::K_COFF:
289     return false;
290   case object::Archive::K_AIXBIG:
291   case object::Archive::K_DARWIN64:
292   case object::Archive::K_GNU64:
293     return true;
294   }
295   llvm_unreachable("not supported for writting");
296 }
297 
298 static void
299 printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable,
300                   StringMap<uint64_t> &MemberNames, object::Archive::Kind Kind,
301                   bool Thin, const NewArchiveMember &M,
302                   sys::TimePoint<std::chrono::seconds> ModTime, uint64_t Size) {
303   if (isBSDLike(Kind))
304     return printBSDMemberHeader(Out, Pos, M.MemberName, ModTime, M.UID, M.GID,
305                                 M.Perms, Size);
306   if (!useStringTable(Thin, M.MemberName))
307     return printGNUSmallMemberHeader(Out, M.MemberName, ModTime, M.UID, M.GID,
308                                      M.Perms, Size);
309   Out << '/';
310   uint64_t NamePos;
311   if (Thin) {
312     NamePos = StringTable.tell();
313     StringTable << M.MemberName << "/\n";
314   } else {
315     auto Insertion = MemberNames.insert({M.MemberName, uint64_t(0)});
316     if (Insertion.second) {
317       Insertion.first->second = StringTable.tell();
318       StringTable << M.MemberName;
319       if (isCOFFArchive(Kind))
320         StringTable << '\0';
321       else
322         StringTable << "/\n";
323     }
324     NamePos = Insertion.first->second;
325   }
326   printWithSpacePadding(Out, NamePos, 15);
327   printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size);
328 }
329 
330 namespace {
331 struct MemberData {
332   std::vector<unsigned> Symbols;
333   std::string Header;
334   StringRef Data;
335   StringRef Padding;
336   uint64_t PreHeadPadSize = 0;
337   std::unique_ptr<SymbolicFile> SymFile = nullptr;
338 };
339 } // namespace
340 
341 static MemberData computeStringTable(StringRef Names) {
342   unsigned Size = Names.size();
343   unsigned Pad = offsetToAlignment(Size, Align(2));
344   std::string Header;
345   raw_string_ostream Out(Header);
346   printWithSpacePadding(Out, "//", 48);
347   printWithSpacePadding(Out, Size + Pad, 10);
348   Out << "`\n";
349   Out.flush();
350   return {{}, std::move(Header), Names, Pad ? "\n" : ""};
351 }
352 
353 static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
354   using namespace std::chrono;
355 
356   if (!Deterministic)
357     return time_point_cast<seconds>(system_clock::now());
358   return sys::TimePoint<seconds>();
359 }
360 
361 static bool isArchiveSymbol(const object::BasicSymbolRef &S) {
362   Expected<uint32_t> SymFlagsOrErr = S.getFlags();
363   if (!SymFlagsOrErr)
364     // TODO: Actually report errors helpfully.
365     report_fatal_error(SymFlagsOrErr.takeError());
366   if (*SymFlagsOrErr & object::SymbolRef::SF_FormatSpecific)
367     return false;
368   if (!(*SymFlagsOrErr & object::SymbolRef::SF_Global))
369     return false;
370   if (*SymFlagsOrErr & object::SymbolRef::SF_Undefined)
371     return false;
372   return true;
373 }
374 
375 static void printNBits(raw_ostream &Out, object::Archive::Kind Kind,
376                        uint64_t Val) {
377   if (is64BitKind(Kind))
378     print<uint64_t>(Out, Kind, Val);
379   else
380     print<uint32_t>(Out, Kind, Val);
381 }
382 
383 static uint64_t computeSymbolTableSize(object::Archive::Kind Kind,
384                                        uint64_t NumSyms, uint64_t OffsetSize,
385                                        uint64_t StringTableSize,
386                                        uint32_t *Padding = nullptr) {
387   assert((OffsetSize == 4 || OffsetSize == 8) && "Unsupported OffsetSize");
388   uint64_t Size = OffsetSize; // Number of entries
389   if (isBSDLike(Kind))
390     Size += NumSyms * OffsetSize * 2; // Table
391   else
392     Size += NumSyms * OffsetSize; // Table
393   if (isBSDLike(Kind))
394     Size += OffsetSize; // byte count
395   Size += StringTableSize;
396   // ld64 expects the members to be 8-byte aligned for 64-bit content and at
397   // least 4-byte aligned for 32-bit content.  Opt for the larger encoding
398   // uniformly.
399   // We do this for all bsd formats because it simplifies aligning members.
400   // For the big archive format, the symbol table is the last member, so there
401   // is no need to align.
402   uint32_t Pad = isAIXBigArchive(Kind)
403                      ? 0
404                      : offsetToAlignment(Size, Align(isBSDLike(Kind) ? 8 : 2));
405 
406   Size += Pad;
407   if (Padding)
408     *Padding = Pad;
409   return Size;
410 }
411 
412 static uint64_t computeSymbolMapSize(uint64_t NumObj, SymMap &SymMap,
413                                      uint32_t *Padding = nullptr) {
414   uint64_t Size = sizeof(uint32_t) * 2; // Number of symbols and objects entries
415   Size += NumObj * sizeof(uint32_t);    // Offset table
416 
417   for (auto S : SymMap.Map)
418     Size += sizeof(uint16_t) + S.first.length() + 1;
419 
420   uint32_t Pad = offsetToAlignment(Size, Align(2));
421   Size += Pad;
422   if (Padding)
423     *Padding = Pad;
424   return Size;
425 }
426 
427 static uint64_t computeECSymbolsSize(SymMap &SymMap,
428                                      uint32_t *Padding = nullptr) {
429   uint64_t Size = sizeof(uint32_t); // Number of symbols
430 
431   for (auto S : SymMap.ECMap)
432     Size += sizeof(uint16_t) + S.first.length() + 1;
433 
434   uint32_t Pad = offsetToAlignment(Size, Align(2));
435   Size += Pad;
436   if (Padding)
437     *Padding = Pad;
438   return Size;
439 }
440 
441 static void writeSymbolTableHeader(raw_ostream &Out, object::Archive::Kind Kind,
442                                    bool Deterministic, uint64_t Size,
443                                    uint64_t PrevMemberOffset = 0,
444                                    uint64_t NextMemberOffset = 0) {
445   if (isBSDLike(Kind)) {
446     const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF";
447     printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0,
448                          Size);
449   } else if (isAIXBigArchive(Kind)) {
450     printBigArchiveMemberHeader(Out, "", now(Deterministic), 0, 0, 0, Size,
451                                 PrevMemberOffset, NextMemberOffset);
452   } else {
453     const char *Name = is64BitKind(Kind) ? "/SYM64" : "";
454     printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size);
455   }
456 }
457 
458 static uint64_t computeHeadersSize(object::Archive::Kind Kind,
459                                    uint64_t NumMembers,
460                                    uint64_t StringMemberSize, uint64_t NumSyms,
461                                    uint64_t SymNamesSize, SymMap *SymMap) {
462   uint32_t OffsetSize = is64BitKind(Kind) ? 8 : 4;
463   uint64_t SymtabSize =
464       computeSymbolTableSize(Kind, NumSyms, OffsetSize, SymNamesSize);
465   auto computeSymbolTableHeaderSize = [=] {
466     SmallString<0> TmpBuf;
467     raw_svector_ostream Tmp(TmpBuf);
468     writeSymbolTableHeader(Tmp, Kind, true, SymtabSize);
469     return TmpBuf.size();
470   };
471   uint32_t HeaderSize = computeSymbolTableHeaderSize();
472   uint64_t Size = strlen("!<arch>\n") + HeaderSize + SymtabSize;
473 
474   if (SymMap) {
475     Size += HeaderSize + computeSymbolMapSize(NumMembers, *SymMap);
476     if (SymMap->ECMap.size())
477       Size += HeaderSize + computeECSymbolsSize(*SymMap);
478   }
479 
480   return Size + StringMemberSize;
481 }
482 
483 static Expected<std::unique_ptr<SymbolicFile>>
484 getSymbolicFile(MemoryBufferRef Buf, LLVMContext &Context) {
485   const file_magic Type = identify_magic(Buf.getBuffer());
486   // Don't attempt to read non-symbolic file types.
487   if (!object::SymbolicFile::isSymbolicFile(Type, &Context))
488     return nullptr;
489   if (Type == file_magic::bitcode) {
490     auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
491         Buf, file_magic::bitcode, &Context);
492     if (!ObjOrErr)
493       return ObjOrErr.takeError();
494     return std::move(*ObjOrErr);
495   } else {
496     auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf);
497     if (!ObjOrErr)
498       return ObjOrErr.takeError();
499     return std::move(*ObjOrErr);
500   }
501 }
502 
503 static bool is64BitSymbolicFile(const SymbolicFile *SymObj) {
504   return SymObj != nullptr ? SymObj->is64Bit() : false;
505 }
506 
507 // Log2 of PAGESIZE(4096) on an AIX system.
508 static const uint32_t Log2OfAIXPageSize = 12;
509 
510 // In the AIX big archive format, since the data content follows the member file
511 // name, if the name ends on an odd byte, an extra byte will be added for
512 // padding. This ensures that the data within the member file starts at an even
513 // byte.
514 static const uint32_t MinBigArchiveMemDataAlign = 2;
515 
516 template <typename AuxiliaryHeader>
517 uint16_t getAuxMaxAlignment(uint16_t AuxHeaderSize, AuxiliaryHeader *AuxHeader,
518                             uint16_t Log2OfMaxAlign) {
519   // If the member doesn't have an auxiliary header, it isn't a loadable object
520   // and so it just needs aligning at the minimum value.
521   if (AuxHeader == nullptr)
522     return MinBigArchiveMemDataAlign;
523 
524   // If the auxiliary header does not have both MaxAlignOfData and
525   // MaxAlignOfText field, it is not a loadable shared object file, so align at
526   // the minimum value. The 'ModuleType' member is located right after
527   // 'MaxAlignOfData' in the AuxiliaryHeader.
528   if (AuxHeaderSize < offsetof(AuxiliaryHeader, ModuleType))
529     return MinBigArchiveMemDataAlign;
530 
531   // If the XCOFF object file does not have a loader section, it is not
532   // loadable, so align at the minimum value.
533   if (AuxHeader->SecNumOfLoader == 0)
534     return MinBigArchiveMemDataAlign;
535 
536   // The content of the loadable member file needs to be aligned at MAX(maximum
537   // alignment of .text, maximum alignment of .data) if there are both fields.
538   // If the desired alignment is > PAGESIZE, 32-bit members are aligned on a
539   // word boundary, while 64-bit members are aligned on a PAGESIZE(2^12=4096)
540   // boundary.
541   uint16_t Log2OfAlign =
542       std::max(AuxHeader->MaxAlignOfText, AuxHeader->MaxAlignOfData);
543   return 1 << (Log2OfAlign > Log2OfAIXPageSize ? Log2OfMaxAlign : Log2OfAlign);
544 }
545 
546 // AIX big archives may contain shared object members. The AIX OS requires these
547 // members to be aligned if they are 64-bit and recommends it for 32-bit
548 // members. This ensures that when these members are loaded they are aligned in
549 // memory.
550 static uint32_t getMemberAlignment(SymbolicFile *SymObj) {
551   XCOFFObjectFile *XCOFFObj = dyn_cast_or_null<XCOFFObjectFile>(SymObj);
552   if (!XCOFFObj)
553     return MinBigArchiveMemDataAlign;
554 
555   // If the desired alignment is > PAGESIZE, 32-bit members are aligned on a
556   // word boundary, while 64-bit members are aligned on a PAGESIZE boundary.
557   return XCOFFObj->is64Bit()
558              ? getAuxMaxAlignment(XCOFFObj->fileHeader64()->AuxHeaderSize,
559                                   XCOFFObj->auxiliaryHeader64(),
560                                   Log2OfAIXPageSize)
561              : getAuxMaxAlignment(XCOFFObj->fileHeader32()->AuxHeaderSize,
562                                   XCOFFObj->auxiliaryHeader32(), 2);
563 }
564 
565 static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind,
566                              bool Deterministic, ArrayRef<MemberData> Members,
567                              StringRef StringTable, uint64_t MembersOffset,
568                              unsigned NumSyms, uint64_t PrevMemberOffset = 0,
569                              uint64_t NextMemberOffset = 0,
570                              bool Is64Bit = false) {
571   // We don't write a symbol table on an archive with no members -- except on
572   // Darwin, where the linker will abort unless the archive has a symbol table.
573   if (StringTable.empty() && !isDarwin(Kind) && !isCOFFArchive(Kind))
574     return;
575 
576   uint64_t OffsetSize = is64BitKind(Kind) ? 8 : 4;
577   uint32_t Pad;
578   uint64_t Size = computeSymbolTableSize(Kind, NumSyms, OffsetSize,
579                                          StringTable.size(), &Pad);
580   writeSymbolTableHeader(Out, Kind, Deterministic, Size, PrevMemberOffset,
581                          NextMemberOffset);
582 
583   if (isBSDLike(Kind))
584     printNBits(Out, Kind, NumSyms * 2 * OffsetSize);
585   else
586     printNBits(Out, Kind, NumSyms);
587 
588   uint64_t Pos = MembersOffset;
589   for (const MemberData &M : Members) {
590     if (isAIXBigArchive(Kind)) {
591       Pos += M.PreHeadPadSize;
592       if (is64BitSymbolicFile(M.SymFile.get()) != Is64Bit) {
593         Pos += M.Header.size() + M.Data.size() + M.Padding.size();
594         continue;
595       }
596     }
597 
598     for (unsigned StringOffset : M.Symbols) {
599       if (isBSDLike(Kind))
600         printNBits(Out, Kind, StringOffset);
601       printNBits(Out, Kind, Pos); // member offset
602     }
603     Pos += M.Header.size() + M.Data.size() + M.Padding.size();
604   }
605 
606   if (isBSDLike(Kind))
607     // byte count of the string table
608     printNBits(Out, Kind, StringTable.size());
609   Out << StringTable;
610 
611   while (Pad--)
612     Out.write(uint8_t(0));
613 }
614 
615 static void writeSymbolMap(raw_ostream &Out, object::Archive::Kind Kind,
616                            bool Deterministic, ArrayRef<MemberData> Members,
617                            SymMap &SymMap, uint64_t MembersOffset) {
618   uint32_t Pad;
619   uint64_t Size = computeSymbolMapSize(Members.size(), SymMap, &Pad);
620   writeSymbolTableHeader(Out, Kind, Deterministic, Size, 0);
621 
622   uint32_t Pos = MembersOffset;
623 
624   printLE<uint32_t>(Out, Members.size());
625   for (const MemberData &M : Members) {
626     printLE(Out, Pos); // member offset
627     Pos += M.Header.size() + M.Data.size() + M.Padding.size();
628   }
629 
630   printLE<uint32_t>(Out, SymMap.Map.size());
631 
632   for (auto S : SymMap.Map)
633     printLE(Out, S.second);
634   for (auto S : SymMap.Map)
635     Out << S.first << '\0';
636 
637   while (Pad--)
638     Out.write(uint8_t(0));
639 }
640 
641 static void writeECSymbols(raw_ostream &Out, object::Archive::Kind Kind,
642                            bool Deterministic, ArrayRef<MemberData> Members,
643                            SymMap &SymMap) {
644   uint32_t Pad;
645   uint64_t Size = computeECSymbolsSize(SymMap, &Pad);
646   printGNUSmallMemberHeader(Out, "/<ECSYMBOLS>", now(Deterministic), 0, 0, 0,
647                             Size);
648 
649   printLE<uint32_t>(Out, SymMap.ECMap.size());
650 
651   for (auto S : SymMap.ECMap)
652     printLE(Out, S.second);
653   for (auto S : SymMap.ECMap)
654     Out << S.first << '\0';
655   while (Pad--)
656     Out.write(uint8_t(0));
657 }
658 
659 static bool isECObject(object::SymbolicFile &Obj) {
660   if (Obj.isCOFF())
661     return cast<llvm::object::COFFObjectFile>(&Obj)->getMachine() !=
662            COFF::IMAGE_FILE_MACHINE_ARM64;
663 
664   if (Obj.isCOFFImportFile())
665     return cast<llvm::object::COFFImportFile>(&Obj)->getMachine() !=
666            COFF::IMAGE_FILE_MACHINE_ARM64;
667 
668   if (Obj.isIR()) {
669     Expected<std::string> TripleStr =
670         getBitcodeTargetTriple(Obj.getMemoryBufferRef());
671     if (!TripleStr)
672       return false;
673     Triple T(*TripleStr);
674     return T.isWindowsArm64EC() || T.getArch() == Triple::x86_64;
675   }
676 
677   return false;
678 }
679 
680 bool isImportDescriptor(StringRef Name) {
681   return Name.starts_with(ImportDescriptorPrefix) ||
682          Name == StringRef{NullImportDescriptorSymbolName} ||
683          (Name.starts_with(NullThunkDataPrefix) &&
684           Name.ends_with(NullThunkDataSuffix));
685 }
686 
687 static Expected<std::vector<unsigned>> getSymbols(SymbolicFile *Obj,
688                                                   uint16_t Index,
689                                                   raw_ostream &SymNames,
690                                                   SymMap *SymMap) {
691   std::vector<unsigned> Ret;
692 
693   if (Obj == nullptr)
694     return Ret;
695 
696   std::map<std::string, uint16_t> *Map = nullptr;
697   if (SymMap)
698     Map = SymMap->UseECMap && isECObject(*Obj) ? &SymMap->ECMap : &SymMap->Map;
699 
700   for (const object::BasicSymbolRef &S : Obj->symbols()) {
701     if (!isArchiveSymbol(S))
702       continue;
703     if (Map) {
704       std::string Name;
705       raw_string_ostream NameStream(Name);
706       if (Error E = S.printName(NameStream))
707         return std::move(E);
708       if (Map->find(Name) != Map->end())
709         continue; // ignore duplicated symbol
710       (*Map)[Name] = Index;
711       if (Map == &SymMap->Map) {
712         Ret.push_back(SymNames.tell());
713         SymNames << Name << '\0';
714         // If EC is enabled, then the import descriptors are NOT put into EC
715         // objects so we need to copy them to the EC map manually.
716         if (SymMap->UseECMap && isImportDescriptor(Name))
717           SymMap->ECMap[Name] = Index;
718       }
719     } else {
720       Ret.push_back(SymNames.tell());
721       if (Error E = S.printName(SymNames))
722         return std::move(E);
723       SymNames << '\0';
724     }
725   }
726   return Ret;
727 }
728 
729 static Expected<std::vector<MemberData>>
730 computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames,
731                   object::Archive::Kind Kind, bool Thin, bool Deterministic,
732                   SymtabWritingMode NeedSymbols, SymMap *SymMap,
733                   LLVMContext &Context, ArrayRef<NewArchiveMember> NewMembers) {
734   static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};
735   uint64_t MemHeadPadSize = 0;
736   uint64_t Pos =
737       isAIXBigArchive(Kind) ? sizeof(object::BigArchive::FixLenHdr) : 0;
738 
739   std::vector<MemberData> Ret;
740   bool HasObject = false;
741 
742   // Deduplicate long member names in the string table and reuse earlier name
743   // offsets. This especially saves space for COFF Import libraries where all
744   // members have the same name.
745   StringMap<uint64_t> MemberNames;
746 
747   // UniqueTimestamps is a special case to improve debugging on Darwin:
748   //
749   // The Darwin linker does not link debug info into the final
750   // binary. Instead, it emits entries of type N_OSO in the output
751   // binary's symbol table, containing references to the linked-in
752   // object files. Using that reference, the debugger can read the
753   // debug data directly from the object files. Alternatively, an
754   // invocation of 'dsymutil' will link the debug data from the object
755   // files into a dSYM bundle, which can be loaded by the debugger,
756   // instead of the object files.
757   //
758   // For an object file, the N_OSO entries contain the absolute path
759   // path to the file, and the file's timestamp. For an object
760   // included in an archive, the path is formatted like
761   // "/absolute/path/to/archive.a(member.o)", and the timestamp is the
762   // archive member's timestamp, rather than the archive's timestamp.
763   //
764   // However, this doesn't always uniquely identify an object within
765   // an archive -- an archive file can have multiple entries with the
766   // same filename. (This will happen commonly if the original object
767   // files started in different directories.) The only way they get
768   // distinguished, then, is via the timestamp. But this process is
769   // unable to find the correct object file in the archive when there
770   // are two files of the same name and timestamp.
771   //
772   // Additionally, timestamp==0 is treated specially, and causes the
773   // timestamp to be ignored as a match criteria.
774   //
775   // That will "usually" work out okay when creating an archive not in
776   // deterministic timestamp mode, because the objects will probably
777   // have been created at different timestamps.
778   //
779   // To ameliorate this problem, in deterministic archive mode (which
780   // is the default), on Darwin we will emit a unique non-zero
781   // timestamp for each entry with a duplicated name. This is still
782   // deterministic: the only thing affecting that timestamp is the
783   // order of the files in the resultant archive.
784   //
785   // See also the functions that handle the lookup:
786   // in lldb: ObjectContainerBSDArchive::Archive::FindObject()
787   // in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers().
788   bool UniqueTimestamps = Deterministic && isDarwin(Kind);
789   std::map<StringRef, unsigned> FilenameCount;
790   if (UniqueTimestamps) {
791     for (const NewArchiveMember &M : NewMembers)
792       FilenameCount[M.MemberName]++;
793     for (auto &Entry : FilenameCount)
794       Entry.second = Entry.second > 1 ? 1 : 0;
795   }
796 
797   // The big archive format needs to know the offset of the previous member
798   // header.
799   uint64_t PrevOffset = 0;
800   uint64_t NextMemHeadPadSize = 0;
801   std::unique_ptr<SymbolicFile> CurSymFile;
802   std::unique_ptr<SymbolicFile> NextSymFile;
803   uint16_t Index = 0;
804 
805   for (auto M = NewMembers.begin(); M < NewMembers.end(); ++M) {
806     std::string Header;
807     raw_string_ostream Out(Header);
808 
809     MemoryBufferRef Buf = M->Buf->getMemBufferRef();
810     StringRef Data = Thin ? "" : Buf.getBuffer();
811 
812     Index++;
813 
814     // ld64 expects the members to be 8-byte aligned for 64-bit content and at
815     // least 4-byte aligned for 32-bit content.  Opt for the larger encoding
816     // uniformly.  This matches the behaviour with cctools and ensures that ld64
817     // is happy with archives that we generate.
818     unsigned MemberPadding =
819         isDarwin(Kind) ? offsetToAlignment(Data.size(), Align(8)) : 0;
820     unsigned TailPadding =
821         offsetToAlignment(Data.size() + MemberPadding, Align(2));
822     StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding);
823 
824     sys::TimePoint<std::chrono::seconds> ModTime;
825     if (UniqueTimestamps)
826       // Increment timestamp for each file of a given name.
827       ModTime = sys::toTimePoint(FilenameCount[M->MemberName]++);
828     else
829       ModTime = M->ModTime;
830 
831     uint64_t Size = Buf.getBufferSize() + MemberPadding;
832     if (Size > object::Archive::MaxMemberSize) {
833       std::string StringMsg =
834           "File " + M->MemberName.str() + " exceeds size limit";
835       return make_error<object::GenericBinaryError>(
836           std::move(StringMsg), object::object_error::parse_failed);
837     }
838 
839     if (NeedSymbols != SymtabWritingMode::NoSymtab || isAIXBigArchive(Kind)) {
840       auto SetNextSymFile = [&NextSymFile,
841                              &Context](MemoryBufferRef Buf,
842                                        StringRef MemberName) -> Error {
843         Expected<std::unique_ptr<SymbolicFile>> SymFileOrErr =
844             getSymbolicFile(Buf, Context);
845         if (!SymFileOrErr)
846           return createFileError(MemberName, SymFileOrErr.takeError());
847         NextSymFile = std::move(*SymFileOrErr);
848         return Error::success();
849       };
850 
851       if (M == NewMembers.begin())
852         if (Error Err = SetNextSymFile(Buf, M->MemberName))
853           return std::move(Err);
854 
855       CurSymFile = std::move(NextSymFile);
856 
857       if ((M + 1) != NewMembers.end())
858         if (Error Err = SetNextSymFile((M + 1)->Buf->getMemBufferRef(),
859                                        (M + 1)->MemberName))
860           return std::move(Err);
861     }
862 
863     // In the big archive file format, we need to calculate and include the next
864     // member offset and previous member offset in the file member header.
865     if (isAIXBigArchive(Kind)) {
866       uint64_t OffsetToMemData = Pos + sizeof(object::BigArMemHdrType) +
867                                  alignTo(M->MemberName.size(), 2);
868 
869       if (M == NewMembers.begin())
870         NextMemHeadPadSize =
871             alignToPowerOf2(OffsetToMemData,
872                             getMemberAlignment(CurSymFile.get())) -
873             OffsetToMemData;
874 
875       MemHeadPadSize = NextMemHeadPadSize;
876       Pos += MemHeadPadSize;
877       uint64_t NextOffset = Pos + sizeof(object::BigArMemHdrType) +
878                             alignTo(M->MemberName.size(), 2) + alignTo(Size, 2);
879 
880       // If there is another member file after this, we need to calculate the
881       // padding before the header.
882       if ((M + 1) != NewMembers.end()) {
883         uint64_t OffsetToNextMemData = NextOffset +
884                                        sizeof(object::BigArMemHdrType) +
885                                        alignTo((M + 1)->MemberName.size(), 2);
886         NextMemHeadPadSize =
887             alignToPowerOf2(OffsetToNextMemData,
888                             getMemberAlignment(NextSymFile.get())) -
889             OffsetToNextMemData;
890         NextOffset += NextMemHeadPadSize;
891       }
892       printBigArchiveMemberHeader(Out, M->MemberName, ModTime, M->UID, M->GID,
893                                   M->Perms, Size, PrevOffset, NextOffset);
894       PrevOffset = Pos;
895     } else {
896       printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, *M,
897                         ModTime, Size);
898     }
899     Out.flush();
900 
901     std::vector<unsigned> Symbols;
902     if (NeedSymbols != SymtabWritingMode::NoSymtab) {
903       Expected<std::vector<unsigned>> SymbolsOrErr =
904           getSymbols(CurSymFile.get(), Index, SymNames, SymMap);
905       if (!SymbolsOrErr)
906         return createFileError(M->MemberName, SymbolsOrErr.takeError());
907       Symbols = std::move(*SymbolsOrErr);
908       if (CurSymFile)
909         HasObject = true;
910     }
911 
912     Pos += Header.size() + Data.size() + Padding.size();
913     Ret.push_back({std::move(Symbols), std::move(Header), Data, Padding,
914                    MemHeadPadSize, std::move(CurSymFile)});
915   }
916   // If there are no symbols, emit an empty symbol table, to satisfy Solaris
917   // tools, older versions of which expect a symbol table in a non-empty
918   // archive, regardless of whether there are any symbols in it.
919   if (HasObject && SymNames.tell() == 0 && !isCOFFArchive(Kind))
920     SymNames << '\0' << '\0' << '\0';
921   return std::move(Ret);
922 }
923 
924 namespace llvm {
925 
926 static ErrorOr<SmallString<128>> canonicalizePath(StringRef P) {
927   SmallString<128> Ret = P;
928   std::error_code Err = sys::fs::make_absolute(Ret);
929   if (Err)
930     return Err;
931   sys::path::remove_dots(Ret, /*removedotdot*/ true);
932   return Ret;
933 }
934 
935 // Compute the relative path from From to To.
936 Expected<std::string> computeArchiveRelativePath(StringRef From, StringRef To) {
937   ErrorOr<SmallString<128>> PathToOrErr = canonicalizePath(To);
938   ErrorOr<SmallString<128>> DirFromOrErr = canonicalizePath(From);
939   if (!PathToOrErr || !DirFromOrErr)
940     return errorCodeToError(std::error_code(errno, std::generic_category()));
941 
942   const SmallString<128> &PathTo = *PathToOrErr;
943   const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr);
944 
945   // Can't construct a relative path between different roots
946   if (sys::path::root_name(PathTo) != sys::path::root_name(DirFrom))
947     return sys::path::convert_to_slash(PathTo);
948 
949   // Skip common prefixes
950   auto FromTo =
951       std::mismatch(sys::path::begin(DirFrom), sys::path::end(DirFrom),
952                     sys::path::begin(PathTo));
953   auto FromI = FromTo.first;
954   auto ToI = FromTo.second;
955 
956   // Construct relative path
957   SmallString<128> Relative;
958   for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
959     sys::path::append(Relative, sys::path::Style::posix, "..");
960 
961   for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI)
962     sys::path::append(Relative, sys::path::Style::posix, *ToI);
963 
964   return std::string(Relative);
965 }
966 
967 static Error writeArchiveToStream(raw_ostream &Out,
968                                   ArrayRef<NewArchiveMember> NewMembers,
969                                   SymtabWritingMode WriteSymtab,
970                                   object::Archive::Kind Kind,
971                                   bool Deterministic, bool Thin, bool IsEC) {
972   assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
973 
974   SmallString<0> SymNamesBuf;
975   raw_svector_ostream SymNames(SymNamesBuf);
976   SmallString<0> StringTableBuf;
977   raw_svector_ostream StringTable(StringTableBuf);
978   SymMap SymMap;
979 
980   // COFF symbol map uses 16-bit indexes, so we can't use it if there are too
981   // many members.
982   if (isCOFFArchive(Kind) && NewMembers.size() > 0xfffe)
983     Kind = object::Archive::K_GNU;
984 
985   // In the scenario when LLVMContext is populated SymbolicFile will contain a
986   // reference to it, thus SymbolicFile should be destroyed first.
987   LLVMContext Context;
988 
989   SymMap.UseECMap = IsEC;
990   Expected<std::vector<MemberData>> DataOrErr = computeMemberData(
991       StringTable, SymNames, Kind, Thin, Deterministic, WriteSymtab,
992       isCOFFArchive(Kind) ? &SymMap : nullptr, Context, NewMembers);
993   if (Error E = DataOrErr.takeError())
994     return E;
995   std::vector<MemberData> &Data = *DataOrErr;
996 
997   uint64_t StringTableSize = 0;
998   MemberData StringTableMember;
999   if (!StringTableBuf.empty() && !isAIXBigArchive(Kind)) {
1000     StringTableMember = computeStringTable(StringTableBuf);
1001     StringTableSize = StringTableMember.Header.size() +
1002                       StringTableMember.Data.size() +
1003                       StringTableMember.Padding.size();
1004   }
1005 
1006   // We would like to detect if we need to switch to a 64-bit symbol table.
1007   uint64_t LastMemberEndOffset = 0;
1008   uint64_t LastMemberHeaderOffset = 0;
1009   uint64_t NumSyms = 0;
1010   uint64_t NumSyms32 = 0; // Store symbol number of 32-bit member files.
1011   bool ShouldWriteSymtab = WriteSymtab != SymtabWritingMode::NoSymtab;
1012 
1013   for (const auto &M : Data) {
1014     // Record the start of the member's offset
1015     LastMemberEndOffset += M.PreHeadPadSize;
1016     LastMemberHeaderOffset = LastMemberEndOffset;
1017     // Account for the size of each part associated with the member.
1018     LastMemberEndOffset += M.Header.size() + M.Data.size() + M.Padding.size();
1019     NumSyms += M.Symbols.size();
1020 
1021     // AIX big archive files may contain two global symbol tables. The
1022     // first global symbol table locates 32-bit file members that define global
1023     // symbols; the second global symbol table does the same for 64-bit file
1024     // members. As a big archive can have both 32-bit and 64-bit file members,
1025     // we need to know the number of symbols in each symbol table individually.
1026     if (isAIXBigArchive(Kind) && ShouldWriteSymtab) {
1027         if (!is64BitSymbolicFile(M.SymFile.get()))
1028           NumSyms32 += M.Symbols.size();
1029       }
1030   }
1031 
1032   std::optional<uint64_t> HeadersSize;
1033 
1034   // The symbol table is put at the end of the big archive file. The symbol
1035   // table is at the start of the archive file for other archive formats.
1036   if (ShouldWriteSymtab && !is64BitKind(Kind)) {
1037     // We assume 32-bit offsets to see if 32-bit symbols are possible or not.
1038     HeadersSize = computeHeadersSize(Kind, Data.size(), StringTableSize,
1039                                      NumSyms, SymNamesBuf.size(),
1040                                      isCOFFArchive(Kind) ? &SymMap : nullptr);
1041 
1042     // The SYM64 format is used when an archive's member offsets are larger than
1043     // 32-bits can hold. The need for this shift in format is detected by
1044     // writeArchive. To test this we need to generate a file with a member that
1045     // has an offset larger than 32-bits but this demands a very slow test. To
1046     // speed the test up we use this environment variable to pretend like the
1047     // cutoff happens before 32-bits and instead happens at some much smaller
1048     // value.
1049     uint64_t Sym64Threshold = 1ULL << 32;
1050     const char *Sym64Env = std::getenv("SYM64_THRESHOLD");
1051     if (Sym64Env)
1052       StringRef(Sym64Env).getAsInteger(10, Sym64Threshold);
1053 
1054     // If LastMemberHeaderOffset isn't going to fit in a 32-bit varible we need
1055     // to switch to 64-bit. Note that the file can be larger than 4GB as long as
1056     // the last member starts before the 4GB offset.
1057     if (*HeadersSize + LastMemberHeaderOffset >= Sym64Threshold) {
1058       if (Kind == object::Archive::K_DARWIN)
1059         Kind = object::Archive::K_DARWIN64;
1060       else
1061         Kind = object::Archive::K_GNU64;
1062       HeadersSize.reset();
1063     }
1064   }
1065 
1066   if (Thin)
1067     Out << "!<thin>\n";
1068   else if (isAIXBigArchive(Kind))
1069     Out << "<bigaf>\n";
1070   else
1071     Out << "!<arch>\n";
1072 
1073   if (!isAIXBigArchive(Kind)) {
1074     if (ShouldWriteSymtab) {
1075       if (!HeadersSize)
1076         HeadersSize = computeHeadersSize(
1077             Kind, Data.size(), StringTableSize, NumSyms, SymNamesBuf.size(),
1078             isCOFFArchive(Kind) ? &SymMap : nullptr);
1079       writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf,
1080                        *HeadersSize, NumSyms);
1081 
1082       if (isCOFFArchive(Kind))
1083         writeSymbolMap(Out, Kind, Deterministic, Data, SymMap, *HeadersSize);
1084     }
1085 
1086     if (StringTableSize)
1087       Out << StringTableMember.Header << StringTableMember.Data
1088           << StringTableMember.Padding;
1089 
1090     if (ShouldWriteSymtab && SymMap.ECMap.size())
1091       writeECSymbols(Out, Kind, Deterministic, Data, SymMap);
1092 
1093     for (const MemberData &M : Data)
1094       Out << M.Header << M.Data << M.Padding;
1095   } else {
1096     HeadersSize = sizeof(object::BigArchive::FixLenHdr);
1097     LastMemberEndOffset += *HeadersSize;
1098     LastMemberHeaderOffset += *HeadersSize;
1099 
1100     // For the big archive (AIX) format, compute a table of member names and
1101     // offsets, used in the member table.
1102     uint64_t MemberTableNameStrTblSize = 0;
1103     std::vector<size_t> MemberOffsets;
1104     std::vector<StringRef> MemberNames;
1105     // Loop across object to find offset and names.
1106     uint64_t MemberEndOffset = sizeof(object::BigArchive::FixLenHdr);
1107     for (size_t I = 0, Size = NewMembers.size(); I != Size; ++I) {
1108       const NewArchiveMember &Member = NewMembers[I];
1109       MemberTableNameStrTblSize += Member.MemberName.size() + 1;
1110       MemberEndOffset += Data[I].PreHeadPadSize;
1111       MemberOffsets.push_back(MemberEndOffset);
1112       MemberNames.push_back(Member.MemberName);
1113       // File member name ended with "`\n". The length is included in
1114       // BigArMemHdrType.
1115       MemberEndOffset += sizeof(object::BigArMemHdrType) +
1116                          alignTo(Data[I].Data.size(), 2) +
1117                          alignTo(Member.MemberName.size(), 2);
1118     }
1119 
1120     // AIX member table size.
1121     uint64_t MemberTableSize = 20 + // Number of members field
1122                                20 * MemberOffsets.size() +
1123                                MemberTableNameStrTblSize;
1124 
1125     SmallString<0> SymNamesBuf32;
1126     SmallString<0> SymNamesBuf64;
1127     raw_svector_ostream SymNames32(SymNamesBuf32);
1128     raw_svector_ostream SymNames64(SymNamesBuf64);
1129 
1130     if (ShouldWriteSymtab && NumSyms)
1131       // Generate the symbol names for the members.
1132       for (const auto &M : Data) {
1133         Expected<std::vector<unsigned>> SymbolsOrErr = getSymbols(
1134             M.SymFile.get(), 0,
1135             is64BitSymbolicFile(M.SymFile.get()) ? SymNames64 : SymNames32,
1136             nullptr);
1137         if (!SymbolsOrErr)
1138           return SymbolsOrErr.takeError();
1139       }
1140 
1141     uint64_t MemberTableEndOffset =
1142         LastMemberEndOffset +
1143         alignTo(sizeof(object::BigArMemHdrType) + MemberTableSize, 2);
1144 
1145     // In AIX OS, The 'GlobSymOffset' field in the fixed-length header contains
1146     // the offset to the 32-bit global symbol table, and the 'GlobSym64Offset'
1147     // contains the offset to the 64-bit global symbol table.
1148     uint64_t GlobalSymbolOffset =
1149         (ShouldWriteSymtab &&
1150          (WriteSymtab != SymtabWritingMode::BigArchive64) && NumSyms32 > 0)
1151             ? MemberTableEndOffset
1152             : 0;
1153 
1154     uint64_t GlobalSymbolOffset64 = 0;
1155     uint64_t NumSyms64 = NumSyms - NumSyms32;
1156     if (ShouldWriteSymtab && (WriteSymtab != SymtabWritingMode::BigArchive32) &&
1157         NumSyms64 > 0) {
1158       if (GlobalSymbolOffset == 0)
1159         GlobalSymbolOffset64 = MemberTableEndOffset;
1160       else
1161         // If there is a global symbol table for 32-bit members,
1162         // the 64-bit global symbol table is after the 32-bit one.
1163         GlobalSymbolOffset64 =
1164             GlobalSymbolOffset + sizeof(object::BigArMemHdrType) +
1165             (NumSyms32 + 1) * 8 + alignTo(SymNamesBuf32.size(), 2);
1166     }
1167 
1168     // Fixed Sized Header.
1169     printWithSpacePadding(Out, NewMembers.size() ? LastMemberEndOffset : 0,
1170                           20); // Offset to member table
1171     // If there are no file members in the archive, there will be no global
1172     // symbol table.
1173     printWithSpacePadding(Out, GlobalSymbolOffset, 20);
1174     printWithSpacePadding(Out, GlobalSymbolOffset64, 20);
1175     printWithSpacePadding(Out,
1176                           NewMembers.size()
1177                               ? sizeof(object::BigArchive::FixLenHdr) +
1178                                     Data[0].PreHeadPadSize
1179                               : 0,
1180                           20); // Offset to first archive member
1181     printWithSpacePadding(Out, NewMembers.size() ? LastMemberHeaderOffset : 0,
1182                           20); // Offset to last archive member
1183     printWithSpacePadding(
1184         Out, 0,
1185         20); // Offset to first member of free list - Not supported yet
1186 
1187     for (const MemberData &M : Data) {
1188       Out << std::string(M.PreHeadPadSize, '\0');
1189       Out << M.Header << M.Data;
1190       if (M.Data.size() % 2)
1191         Out << '\0';
1192     }
1193 
1194     if (NewMembers.size()) {
1195       // Member table.
1196       printBigArchiveMemberHeader(Out, "", sys::toTimePoint(0), 0, 0, 0,
1197                                   MemberTableSize, LastMemberHeaderOffset,
1198                                   GlobalSymbolOffset ? GlobalSymbolOffset
1199                                                      : GlobalSymbolOffset64);
1200       printWithSpacePadding(Out, MemberOffsets.size(), 20); // Number of members
1201       for (uint64_t MemberOffset : MemberOffsets)
1202         printWithSpacePadding(Out, MemberOffset,
1203                               20); // Offset to member file header.
1204       for (StringRef MemberName : MemberNames)
1205         Out << MemberName << '\0'; // Member file name, null byte padding.
1206 
1207       if (MemberTableNameStrTblSize % 2)
1208         Out << '\0'; // Name table must be tail padded to an even number of
1209                      // bytes.
1210 
1211       if (ShouldWriteSymtab) {
1212         // Write global symbol table for 32-bit file members.
1213         if (GlobalSymbolOffset) {
1214           writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf32,
1215                            *HeadersSize, NumSyms32, LastMemberEndOffset,
1216                            GlobalSymbolOffset64);
1217           // Add padding between the symbol tables, if needed.
1218           if (GlobalSymbolOffset64 && (SymNamesBuf32.size() % 2))
1219             Out << '\0';
1220         }
1221 
1222         // Write global symbol table for 64-bit file members.
1223         if (GlobalSymbolOffset64)
1224           writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf64,
1225                            *HeadersSize, NumSyms64,
1226                            GlobalSymbolOffset ? GlobalSymbolOffset
1227                                               : LastMemberEndOffset,
1228                            0, true);
1229       }
1230     }
1231   }
1232   Out.flush();
1233   return Error::success();
1234 }
1235 
1236 Error writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
1237                    SymtabWritingMode WriteSymtab, object::Archive::Kind Kind,
1238                    bool Deterministic, bool Thin,
1239                    std::unique_ptr<MemoryBuffer> OldArchiveBuf, bool IsEC) {
1240   Expected<sys::fs::TempFile> Temp =
1241       sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a");
1242   if (!Temp)
1243     return Temp.takeError();
1244   raw_fd_ostream Out(Temp->FD, false);
1245 
1246   if (Error E = writeArchiveToStream(Out, NewMembers, WriteSymtab, Kind,
1247                                      Deterministic, Thin, IsEC)) {
1248     if (Error DiscardError = Temp->discard())
1249       return joinErrors(std::move(E), std::move(DiscardError));
1250     return E;
1251   }
1252 
1253   // At this point, we no longer need whatever backing memory
1254   // was used to generate the NewMembers. On Windows, this buffer
1255   // could be a mapped view of the file we want to replace (if
1256   // we're updating an existing archive, say). In that case, the
1257   // rename would still succeed, but it would leave behind a
1258   // temporary file (actually the original file renamed) because
1259   // a file cannot be deleted while there's a handle open on it,
1260   // only renamed. So by freeing this buffer, this ensures that
1261   // the last open handle on the destination file, if any, is
1262   // closed before we attempt to rename.
1263   OldArchiveBuf.reset();
1264 
1265   return Temp->keep(ArcName);
1266 }
1267 
1268 Expected<std::unique_ptr<MemoryBuffer>>
1269 writeArchiveToBuffer(ArrayRef<NewArchiveMember> NewMembers,
1270                      SymtabWritingMode WriteSymtab, object::Archive::Kind Kind,
1271                      bool Deterministic, bool Thin) {
1272   SmallVector<char, 0> ArchiveBufferVector;
1273   raw_svector_ostream ArchiveStream(ArchiveBufferVector);
1274 
1275   if (Error E = writeArchiveToStream(ArchiveStream, NewMembers, WriteSymtab,
1276                                      Kind, Deterministic, Thin, false))
1277     return std::move(E);
1278 
1279   return std::make_unique<SmallVectorMemoryBuffer>(
1280       std::move(ArchiveBufferVector), /*RequiresNullTerminator=*/false);
1281 }
1282 
1283 } // namespace llvm
1284