LCOV - code coverage report
Current view: top level - lib/Object - ArchiveWriter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 192 198 97.0 %
Date: 2018-07-13 00:08:38 Functions: 26 27 96.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file defines the writeArchive function.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "llvm/Object/ArchiveWriter.h"
      15             : #include "llvm/ADT/ArrayRef.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/ObjectFile.h"
      21             : #include "llvm/Object/SymbolicFile.h"
      22             : #include "llvm/Support/EndianStream.h"
      23             : #include "llvm/Support/Errc.h"
      24             : #include "llvm/Support/ErrorHandling.h"
      25             : #include "llvm/Support/Format.h"
      26             : #include "llvm/Support/Path.h"
      27             : #include "llvm/Support/ToolOutputFile.h"
      28             : #include "llvm/Support/raw_ostream.h"
      29             : 
      30             : #if !defined(_MSC_VER) && !defined(__MINGW32__)
      31             : #include <unistd.h>
      32             : #else
      33             : #include <io.h>
      34             : #endif
      35             : 
      36             : using namespace llvm;
      37             : 
      38         585 : NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef)
      39             :     : Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
      40        1170 :       MemberName(BufRef.getBufferIdentifier()) {}
      41             : 
      42             : Expected<NewArchiveMember>
      43          73 : NewArchiveMember::getOldMember(const object::Archive::Child &OldMember,
      44             :                                bool Deterministic) {
      45          73 :   Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef();
      46          73 :   if (!BufOrErr)
      47             :     return BufOrErr.takeError();
      48             : 
      49          73 :   NewArchiveMember M;
      50             :   assert(M.IsNew == false);
      51         146 :   M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
      52          73 :   M.MemberName = M.Buf->getBufferIdentifier();
      53          73 :   if (!Deterministic) {
      54             :     auto ModTimeOrErr = OldMember.getLastModified();
      55           1 :     if (!ModTimeOrErr)
      56             :       return ModTimeOrErr.takeError();
      57           1 :     M.ModTime = ModTimeOrErr.get();
      58             :     Expected<unsigned> UIDOrErr = OldMember.getUID();
      59           1 :     if (!UIDOrErr)
      60             :       return UIDOrErr.takeError();
      61           1 :     M.UID = UIDOrErr.get();
      62             :     Expected<unsigned> GIDOrErr = OldMember.getGID();
      63           1 :     if (!GIDOrErr)
      64             :       return GIDOrErr.takeError();
      65           1 :     M.GID = GIDOrErr.get();
      66             :     Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
      67           1 :     if (!AccessModeOrErr)
      68             :       return AccessModeOrErr.takeError();
      69           1 :     M.Perms = AccessModeOrErr.get();
      70             :   }
      71             :   return std::move(M);
      72             : }
      73             : 
      74         228 : Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
      75             :                                                      bool Deterministic) {
      76         228 :   sys::fs::file_status Status;
      77             :   int FD;
      78         228 :   if (auto EC = sys::fs::openFileForRead(FileName, FD))
      79           2 :     return errorCodeToError(EC);
      80             :   assert(FD != -1);
      81             : 
      82         227 :   if (auto EC = sys::fs::status(FD, Status))
      83           0 :     return errorCodeToError(EC);
      84             : 
      85             :   // Opening a directory doesn't make sense. Let it fail.
      86             :   // Linux cannot open directories with open(2), although
      87             :   // cygwin and *bsd can.
      88         227 :   if (Status.type() == sys::fs::file_type::directory_file)
      89           2 :     return errorCodeToError(make_error_code(errc::is_a_directory));
      90             : 
      91             :   ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
      92         452 :       MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
      93         226 :   if (!MemberBufferOrErr)
      94           0 :     return errorCodeToError(MemberBufferOrErr.getError());
      95             : 
      96         226 :   if (close(FD) != 0)
      97           0 :     return errorCodeToError(std::error_code(errno, std::generic_category()));
      98             : 
      99             :   NewArchiveMember M;
     100             :   M.IsNew = true;
     101             :   M.Buf = std::move(*MemberBufferOrErr);
     102         226 :   M.MemberName = M.Buf->getBufferIdentifier();
     103         226 :   if (!Deterministic) {
     104             :     M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
     105          32 :         Status.getLastModificationTime());
     106          16 :     M.UID = Status.getUser();
     107          16 :     M.GID = Status.getGroup();
     108          16 :     M.Perms = Status.permissions();
     109             :   }
     110             :   return std::move(M);
     111             : }
     112             : 
     113             : template <typename T>
     114        7462 : static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
     115             :   uint64_t OldPos = OS.tell();
     116        3214 :   OS << Data;
     117        7462 :   unsigned SizeSoFar = OS.tell() - OldPos;
     118             :   assert(SizeSoFar <= Size && "Data doesn't fit in Size");
     119        7462 :   OS.indent(Size - SizeSoFar);
     120        7462 : }
     121             : 
     122             : static bool isBSDLike(object::Archive::Kind Kind) {
     123        4885 :   switch (Kind) {
     124             :   case object::Archive::K_GNU:
     125             :   case object::Archive::K_GNU64:
     126             :     return false;
     127             :   case object::Archive::K_BSD:
     128             :   case object::Archive::K_DARWIN:
     129             :     return true;
     130             :   case object::Archive::K_DARWIN64:
     131             :   case object::Archive::K_COFF:
     132             :     break;
     133             :   }
     134           0 :   llvm_unreachable("not supported for writting");
     135             : }
     136             : 
     137             : template <class T>
     138        1307 : static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) {
     139             :   support::endian::write(Out, Val,
     140             :                          isBSDLike(Kind) ? support::little : support::big);
     141        1307 : }
     142             : 
     143        1161 : static void printRestOfMemberHeader(
     144             :     raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime,
     145             :     unsigned UID, unsigned GID, unsigned Perms, unsigned Size) {
     146        1161 :   printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
     147             : 
     148             :   // The format has only 6 chars for uid and gid. Truncate if the provided
     149             :   // values don't fit.
     150        1161 :   printWithSpacePadding(Out, UID % 1000000, 6);
     151        1161 :   printWithSpacePadding(Out, GID % 1000000, 6);
     152             : 
     153        1161 :   printWithSpacePadding(Out, format("%o", Perms), 8);
     154        1161 :   printWithSpacePadding(Out, Size, 10);
     155        1161 :   Out << "`\n";
     156        1161 : }
     157             : 
     158             : static void
     159         476 : printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name,
     160             :                           const sys::TimePoint<std::chrono::seconds> &ModTime,
     161             :                           unsigned UID, unsigned GID, unsigned Perms,
     162             :                           unsigned Size) {
     163         476 :   printWithSpacePadding(Out, Twine(Name) + "/", 16);
     164         476 :   printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
     165         476 : }
     166             : 
     167             : static void
     168          41 : printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name,
     169             :                      const sys::TimePoint<std::chrono::seconds> &ModTime,
     170             :                      unsigned UID, unsigned GID, unsigned Perms,
     171             :                      unsigned Size) {
     172          41 :   uint64_t PosAfterHeader = Pos + 60 + Name.size();
     173             :   // Pad so that even 64 bit object files are aligned.
     174          41 :   unsigned Pad = OffsetToAlignment(PosAfterHeader, 8);
     175          41 :   unsigned NameWithPadding = Name.size() + Pad;
     176          41 :   printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
     177          41 :   printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
     178             :                           NameWithPadding + Size);
     179          41 :   Out << Name;
     180         301 :   while (Pad--)
     181         130 :     Out.write(uint8_t(0));
     182          41 : }
     183             : 
     184             : static bool useStringTable(bool Thin, StringRef Name) {
     185        1875 :   return Thin || Name.size() >= 16 || Name.contains('/');
     186             : }
     187             : 
     188             : // Compute the relative path from From to To.
     189          25 : static std::string computeRelativePath(StringRef From, StringRef To) {
     190          45 :   if (sys::path::is_absolute(From) || sys::path::is_absolute(To))
     191             :     return To;
     192             : 
     193          10 :   StringRef DirFrom = sys::path::parent_path(From);
     194          10 :   auto FromI = sys::path::begin(DirFrom);
     195          10 :   auto ToI = sys::path::begin(To);
     196           3 :   while (*FromI == *ToI) {
     197           3 :     ++FromI;
     198           3 :     ++ToI;
     199             :   }
     200             : 
     201             :   SmallString<128> Relative;
     202          28 :   for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
     203           4 :     sys::path::append(Relative, "..");
     204             : 
     205          44 :   for (auto ToE = sys::path::end(To); ToI != ToE; ++ToI)
     206          12 :     sys::path::append(Relative, *ToI);
     207             : 
     208             : #ifdef _WIN32
     209             :   // Replace backslashes with slashes so that the path is portable between *nix
     210             :   // and Windows.
     211             :   std::replace(Relative.begin(), Relative.end(), '\\', '/');
     212             : #endif
     213             : 
     214             :   return Relative.str();
     215             : }
     216             : 
     217             : static bool is64BitKind(object::Archive::Kind Kind) {
     218             :   switch (Kind) {
     219             :   case object::Archive::K_GNU:
     220             :   case object::Archive::K_BSD:
     221             :   case object::Archive::K_DARWIN:
     222             :   case object::Archive::K_COFF:
     223             :     return false;
     224             :   case object::Archive::K_DARWIN64:
     225             :   case object::Archive::K_GNU64:
     226             :     return true;
     227             :   }
     228           0 :   llvm_unreachable("not supported for writting");
     229             : }
     230             : 
     231         644 : static void addToStringTable(raw_ostream &Out, StringRef ArcName,
     232             :                              const NewArchiveMember &M, bool Thin) {
     233         644 :   StringRef ID = M.Buf->getBufferIdentifier();
     234         644 :   if (Thin) {
     235          30 :     if (M.IsNew)
     236          50 :       Out << computeRelativePath(ArcName, ID);
     237             :     else
     238           5 :       Out << ID;
     239             :   } else
     240         614 :     Out << M.MemberName;
     241         644 :   Out << "/\n";
     242         644 : }
     243             : 
     244         878 : static void printMemberHeader(raw_ostream &Out, uint64_t Pos,
     245             :                               raw_ostream &StringTable,
     246             :                               object::Archive::Kind Kind, bool Thin,
     247             :                               StringRef ArcName, const NewArchiveMember &M,
     248             :                               unsigned Size) {
     249             :   if (isBSDLike(Kind))
     250          29 :     return printBSDMemberHeader(Out, Pos, M.MemberName, M.ModTime, M.UID, M.GID,
     251          58 :                                 M.Perms, Size);
     252         849 :   if (!useStringTable(Thin, M.MemberName))
     253         205 :     return printGNUSmallMemberHeader(Out, M.MemberName, M.ModTime, M.UID, M.GID,
     254         410 :                                      M.Perms, Size);
     255             :   Out << '/';
     256             :   uint64_t NamePos = StringTable.tell();
     257         644 :   addToStringTable(StringTable, ArcName, M, Thin);
     258         644 :   printWithSpacePadding(Out, NamePos, 15);
     259         644 :   printRestOfMemberHeader(Out, M.ModTime, M.UID, M.GID, M.Perms, Size);
     260             : }
     261             : 
     262             : namespace {
     263       15688 : struct MemberData {
     264             :   std::vector<unsigned> Symbols;
     265             :   std::string Header;
     266             :   StringRef Data;
     267             :   StringRef Padding;
     268             : };
     269             : } // namespace
     270             : 
     271         248 : static MemberData computeStringTable(StringRef Names) {
     272         248 :   unsigned Size = Names.size();
     273         248 :   unsigned Pad = OffsetToAlignment(Size, 2);
     274             :   std::string Header;
     275         248 :   raw_string_ostream Out(Header);
     276         248 :   printWithSpacePadding(Out, "//", 48);
     277         248 :   printWithSpacePadding(Out, Size + Pad, 10);
     278         248 :   Out << "`\n";
     279             :   Out.flush();
     280         496 :   return {{}, std::move(Header), Names, Pad ? "\n" : ""};
     281             : }
     282             : 
     283             : static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
     284             :   using namespace std::chrono;
     285             : 
     286          13 :   if (!Deterministic)
     287           8 :     return time_point_cast<seconds>(system_clock::now());
     288             :   return sys::TimePoint<seconds>();
     289             : }
     290             : 
     291        2376 : static bool isArchiveSymbol(const object::BasicSymbolRef &S) {
     292             :   uint32_t Symflags = S.getFlags();
     293        2376 :   if (Symflags & object::SymbolRef::SF_FormatSpecific)
     294             :     return false;
     295        1907 :   if (!(Symflags & object::SymbolRef::SF_Global))
     296             :     return false;
     297        1353 :   if (Symflags & object::SymbolRef::SF_Undefined &&
     298             :       !(Symflags & object::SymbolRef::SF_Indirect))
     299             :     return false;
     300        1015 :   return true;
     301             : }
     302             : 
     303        1273 : static void printNBits(raw_ostream &Out, object::Archive::Kind Kind,
     304             :                        uint64_t Val) {
     305             :   if (is64BitKind(Kind))
     306           2 :     print<uint64_t>(Out, Kind, Val);
     307             :   else
     308        1271 :     print<uint32_t>(Out, Kind, Val);
     309        1273 : }
     310             : 
     311         322 : static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind,
     312             :                              bool Deterministic, ArrayRef<MemberData> Members,
     313             :                              StringRef StringTable) {
     314         322 :   if (StringTable.empty())
     315             :     return;
     316             : 
     317             :   unsigned NumSyms = 0;
     318        2337 :   for (const MemberData &M : Members)
     319        2054 :     NumSyms += M.Symbols.size();
     320             : 
     321             :   unsigned Size = 0;
     322         283 :   Size += is64BitKind(Kind) ? 8 : 4; // Number of entries
     323             :   if (isBSDLike(Kind))
     324          12 :     Size += NumSyms * 8; // Table
     325             :   else if (is64BitKind(Kind))
     326           1 :     Size += NumSyms * 8; // Table
     327             :   else
     328         270 :     Size += NumSyms * 4; // Table
     329             :   if (isBSDLike(Kind))
     330          12 :     Size += 4; // byte count
     331         283 :   Size += StringTable.size();
     332             :   // ld64 expects the members to be 8-byte aligned for 64-bit content and at
     333             :   // least 4-byte aligned for 32-bit content.  Opt for the larger encoding
     334             :   // uniformly.
     335             :   // We do this for all bsd formats because it simplifies aligning members.
     336             :   unsigned Alignment = isBSDLike(Kind) ? 8 : 2;
     337         566 :   unsigned Pad = OffsetToAlignment(Size, Alignment);
     338         283 :   Size += Pad;
     339             : 
     340             :   if (isBSDLike(Kind))
     341          24 :     printBSDMemberHeader(Out, Out.tell(), "__.SYMDEF", now(Deterministic), 0, 0,
     342             :                          0, Size);
     343             :   else if (is64BitKind(Kind))
     344           2 :     printGNUSmallMemberHeader(Out, "/SYM64", now(Deterministic), 0, 0, 0, Size);
     345             :   else
     346         540 :     printGNUSmallMemberHeader(Out, "", now(Deterministic), 0, 0, 0, Size);
     347             : 
     348         283 :   uint64_t Pos = Out.tell() + Size;
     349             : 
     350             :   if (isBSDLike(Kind))
     351          12 :     print<uint32_t>(Out, Kind, NumSyms * 8);
     352             :   else
     353         271 :     printNBits(Out, Kind, NumSyms);
     354             : 
     355        2337 :   for (const MemberData &M : Members) {
     356        1027 :     for (unsigned StringOffset : M.Symbols) {
     357             :       if (isBSDLike(Kind))
     358          10 :         print<uint32_t>(Out, Kind, StringOffset);
     359        1002 :       printNBits(Out, Kind, Pos); // member offset
     360             :     }
     361        3081 :     Pos += M.Header.size() + M.Data.size() + M.Padding.size();
     362             :   }
     363             : 
     364             :   if (isBSDLike(Kind))
     365             :     // byte count of the string table
     366          12 :     print<uint32_t>(Out, Kind, StringTable.size());
     367         283 :   Out << StringTable;
     368             : 
     369         523 :   while (Pad--)
     370         120 :     Out.write(uint8_t(0));
     371             : }
     372             : 
     373             : static Expected<std::vector<unsigned>>
     374         878 : getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) {
     375             :   std::vector<unsigned> Ret;
     376        1756 :   LLVMContext Context;
     377             : 
     378             :   Expected<std::unique_ptr<object::SymbolicFile>> ObjOrErr =
     379             :       object::SymbolicFile::createSymbolicFile(Buf, llvm::file_magic::unknown,
     380        1756 :                                                &Context);
     381         878 :   if (!ObjOrErr) {
     382             :     // FIXME: check only for "not an object file" errors.
     383         184 :     consumeError(ObjOrErr.takeError());
     384             :     return Ret;
     385             :   }
     386             : 
     387         786 :   HasObject = true;
     388             :   object::SymbolicFile &Obj = *ObjOrErr.get();
     389        5520 :   for (const object::BasicSymbolRef &S : Obj.symbols()) {
     390        2376 :     if (!isArchiveSymbol(S))
     391        1361 :       continue;
     392        2030 :     Ret.push_back(SymNames.tell());
     393        1015 :     if (auto EC = S.printName(SymNames))
     394           0 :       return errorCodeToError(EC);
     395             :     SymNames << '\0';
     396             :   }
     397             :   return Ret;
     398             : }
     399             : 
     400             : static Expected<std::vector<MemberData>>
     401         330 : computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames,
     402             :                   object::Archive::Kind Kind, bool Thin, StringRef ArcName,
     403             :                   ArrayRef<NewArchiveMember> NewMembers) {
     404             :   static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};
     405             : 
     406             :   // This ignores the symbol table, but we only need the value mod 8 and the
     407             :   // symbol table is aligned to be a multiple of 8 bytes
     408             :   uint64_t Pos = 0;
     409             : 
     410         330 :   std::vector<MemberData> Ret;
     411         330 :   bool HasObject = false;
     412        2086 :   for (const NewArchiveMember &M : NewMembers) {
     413             :     std::string Header;
     414         878 :     raw_string_ostream Out(Header);
     415             : 
     416         878 :     MemoryBufferRef Buf = M.Buf->getMemBufferRef();
     417         878 :     StringRef Data = Thin ? "" : Buf.getBuffer();
     418             : 
     419             :     // ld64 expects the members to be 8-byte aligned for 64-bit content and at
     420             :     // least 4-byte aligned for 32-bit content.  Opt for the larger encoding
     421             :     // uniformly.  This matches the behaviour with cctools and ensures that ld64
     422             :     // is happy with archives that we generate.
     423             :     unsigned MemberPadding = Kind == object::Archive::K_DARWIN
     424             :                                  ? OffsetToAlignment(Data.size(), 8)
     425         886 :                                  : 0;
     426        1756 :     unsigned TailPadding = OffsetToAlignment(Data.size() + MemberPadding, 2);
     427         878 :     StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding);
     428             : 
     429         878 :     printMemberHeader(Out, Pos, StringTable, Kind, Thin, ArcName, M,
     430             :                       Buf.getBufferSize() + MemberPadding);
     431             :     Out.flush();
     432             : 
     433             :     Expected<std::vector<unsigned>> Symbols =
     434        1756 :         getSymbols(Buf, SymNames, HasObject);
     435         878 :     if (auto E = Symbols.takeError())
     436             :       return std::move(E);
     437             : 
     438         878 :     Pos += Header.size() + Data.size() + Padding.size();
     439        1756 :     Ret.push_back({std::move(*Symbols), std::move(Header), Data, Padding});
     440             :   }
     441             :   // If there are no symbols, emit an empty symbol table, to satisfy Solaris
     442             :   // tools, older versions of which expect a symbol table in a non-empty
     443             :   // archive, regardless of whether there are any symbols in it.
     444         621 :   if (HasObject && SymNames.tell() == 0)
     445             :     SymNames << '\0' << '\0' << '\0';
     446             :   return Ret;
     447             : }
     448             : 
     449         330 : Error llvm::writeArchive(StringRef ArcName,
     450             :                          ArrayRef<NewArchiveMember> NewMembers,
     451             :                          bool WriteSymtab, object::Archive::Kind Kind,
     452             :                          bool Deterministic, bool Thin,
     453             :                          std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
     454             :   assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
     455             : 
     456             :   SmallString<0> SymNamesBuf;
     457             :   raw_svector_ostream SymNames(SymNamesBuf);
     458             :   SmallString<0> StringTableBuf;
     459             :   raw_svector_ostream StringTable(StringTableBuf);
     460             : 
     461             :   Expected<std::vector<MemberData>> DataOrErr =
     462         660 :       computeMemberData(StringTable, SymNames, Kind, Thin, ArcName, NewMembers);
     463         330 :   if (Error E = DataOrErr.takeError())
     464             :     return E;
     465             :   std::vector<MemberData> &Data = *DataOrErr;
     466             : 
     467         330 :   if (!StringTableBuf.empty())
     468         496 :     Data.insert(Data.begin(), computeStringTable(StringTableBuf));
     469             : 
     470             :   // We would like to detect if we need to switch to a 64-bit symbol table.
     471         330 :   if (WriteSymtab) {
     472             :     uint64_t MaxOffset = 0;
     473             :     uint64_t LastOffset = MaxOffset;
     474        1430 :     for (const auto& M : Data) {
     475             :       // Record the start of the member's offset
     476             :       LastOffset = MaxOffset;
     477             :       // Account for the size of each part associated with the member.
     478        3324 :       MaxOffset += M.Header.size() + M.Data.size() + M.Padding.size();
     479             :       // We assume 32-bit symbols to see if 32-bit symbols are possible or not.
     480        2216 :       MaxOffset += M.Symbols.size() * 4;
     481             :     }
     482             : 
     483             :     // The SYM64 format is used when an archive's member offsets are larger than
     484             :     // 32-bits can hold. The need for this shift in format is detected by
     485             :     // writeArchive. To test this we need to generate a file with a member that
     486             :     // has an offset larger than 32-bits but this demands a very slow test. To
     487             :     // speed the test up we use this environment variable to pretend like the
     488             :     // cutoff happens before 32-bits and instead happens at some much smaller
     489             :     // value.
     490         322 :     const char *Sym64Env = std::getenv("SYM64_THRESHOLD");
     491             :     int Sym64Threshold = 32;
     492         322 :     if (Sym64Env)
     493           1 :       StringRef(Sym64Env).getAsInteger(10, Sym64Threshold);
     494             : 
     495             :     // If LastOffset isn't going to fit in a 32-bit varible we need to switch
     496             :     // to 64-bit. Note that the file can be larger than 4GB as long as the last
     497             :     // member starts before the 4GB offset.
     498         322 :     if (LastOffset >= (1ULL << Sym64Threshold))
     499             :       Kind = object::Archive::K_GNU64;
     500             :   }
     501             : 
     502             :   Expected<sys::fs::TempFile> Temp =
     503         660 :       sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a");
     504         330 :   if (!Temp)
     505             :     return Temp.takeError();
     506             : 
     507         660 :   raw_fd_ostream Out(Temp->FD, false);
     508         330 :   if (Thin)
     509          17 :     Out << "!<thin>\n";
     510             :   else
     511         313 :     Out << "!<arch>\n";
     512             : 
     513         330 :   if (WriteSymtab)
     514         322 :     writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf);
     515             : 
     516        1456 :   for (const MemberData &M : Data)
     517        1126 :     Out << M.Header << M.Data << M.Padding;
     518             : 
     519             :   Out.flush();
     520             : 
     521             :   // At this point, we no longer need whatever backing memory
     522             :   // was used to generate the NewMembers. On Windows, this buffer
     523             :   // could be a mapped view of the file we want to replace (if
     524             :   // we're updating an existing archive, say). In that case, the
     525             :   // rename would still succeed, but it would leave behind a
     526             :   // temporary file (actually the original file renamed) because
     527             :   // a file cannot be deleted while there's a handle open on it,
     528             :   // only renamed. So by freeing this buffer, this ensures that
     529             :   // the last open handle on the destination file, if any, is
     530             :   // closed before we attempt to rename.
     531             :   OldArchiveBuf.reset();
     532             : 
     533         330 :   return Temp->keep(ArcName);
     534             : }

Generated by: LCOV version 1.13