1284184Sdim//===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===//
2284184Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6284184Sdim//
7284184Sdim//===----------------------------------------------------------------------===//
8284184Sdim//
9284184Sdim// This file defines the writeArchive function.
10284184Sdim//
11284184Sdim//===----------------------------------------------------------------------===//
12284184Sdim
13284184Sdim#include "llvm/Object/ArchiveWriter.h"
14284184Sdim#include "llvm/ADT/ArrayRef.h"
15284184Sdim#include "llvm/ADT/StringRef.h"
16321369Sdim#include "llvm/BinaryFormat/Magic.h"
17284184Sdim#include "llvm/IR/LLVMContext.h"
18284184Sdim#include "llvm/Object/Archive.h"
19360784Sdim#include "llvm/Object/Error.h"
20284184Sdim#include "llvm/Object/ObjectFile.h"
21284184Sdim#include "llvm/Object/SymbolicFile.h"
22360784Sdim#include "llvm/Support/Alignment.h"
23284734Sdim#include "llvm/Support/EndianStream.h"
24284734Sdim#include "llvm/Support/Errc.h"
25284184Sdim#include "llvm/Support/ErrorHandling.h"
26284184Sdim#include "llvm/Support/Format.h"
27284184Sdim#include "llvm/Support/Path.h"
28284184Sdim#include "llvm/Support/ToolOutputFile.h"
29284184Sdim#include "llvm/Support/raw_ostream.h"
30284184Sdim
31344779Sdim#include <map>
32344779Sdim
33284184Sdim#if !defined(_MSC_VER) && !defined(__MINGW32__)
34284184Sdim#include <unistd.h>
35284184Sdim#else
36284184Sdim#include <io.h>
37284184Sdim#endif
38284184Sdim
39284184Sdimusing namespace llvm;
40284184Sdim
41309124SdimNewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef)
42321369Sdim    : Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
43321369Sdim      MemberName(BufRef.getBufferIdentifier()) {}
44284184Sdim
45309124SdimExpected<NewArchiveMember>
46309124SdimNewArchiveMember::getOldMember(const object::Archive::Child &OldMember,
47309124Sdim                               bool Deterministic) {
48314564Sdim  Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef();
49309124Sdim  if (!BufOrErr)
50314564Sdim    return BufOrErr.takeError();
51284184Sdim
52309124Sdim  NewArchiveMember M;
53309124Sdim  M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
54321369Sdim  M.MemberName = M.Buf->getBufferIdentifier();
55309124Sdim  if (!Deterministic) {
56314564Sdim    auto ModTimeOrErr = OldMember.getLastModified();
57314564Sdim    if (!ModTimeOrErr)
58314564Sdim      return ModTimeOrErr.takeError();
59314564Sdim    M.ModTime = ModTimeOrErr.get();
60314564Sdim    Expected<unsigned> UIDOrErr = OldMember.getUID();
61314564Sdim    if (!UIDOrErr)
62314564Sdim      return UIDOrErr.takeError();
63314564Sdim    M.UID = UIDOrErr.get();
64314564Sdim    Expected<unsigned> GIDOrErr = OldMember.getGID();
65314564Sdim    if (!GIDOrErr)
66314564Sdim      return GIDOrErr.takeError();
67314564Sdim    M.GID = GIDOrErr.get();
68314564Sdim    Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
69314564Sdim    if (!AccessModeOrErr)
70314564Sdim      return AccessModeOrErr.takeError();
71314564Sdim    M.Perms = AccessModeOrErr.get();
72309124Sdim  }
73309124Sdim  return std::move(M);
74284184Sdim}
75284184Sdim
76309124SdimExpected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
77309124Sdim                                                     bool Deterministic) {
78309124Sdim  sys::fs::file_status Status;
79353358Sdim  auto FDOrErr = sys::fs::openNativeFileForRead(FileName);
80353358Sdim  if (!FDOrErr)
81353358Sdim    return FDOrErr.takeError();
82353358Sdim  sys::fs::file_t FD = *FDOrErr;
83353358Sdim  assert(FD != sys::fs::kInvalidFile);
84284184Sdim
85309124Sdim  if (auto EC = sys::fs::status(FD, Status))
86309124Sdim    return errorCodeToError(EC);
87284184Sdim
88284184Sdim  // Opening a directory doesn't make sense. Let it fail.
89284184Sdim  // Linux cannot open directories with open(2), although
90284184Sdim  // cygwin and *bsd can.
91309124Sdim  if (Status.type() == sys::fs::file_type::directory_file)
92309124Sdim    return errorCodeToError(make_error_code(errc::is_a_directory));
93284184Sdim
94309124Sdim  ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
95309124Sdim      MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
96309124Sdim  if (!MemberBufferOrErr)
97309124Sdim    return errorCodeToError(MemberBufferOrErr.getError());
98309124Sdim
99353358Sdim  if (auto EC = sys::fs::closeFile(FD))
100353358Sdim    return errorCodeToError(EC);
101309124Sdim
102309124Sdim  NewArchiveMember M;
103309124Sdim  M.Buf = std::move(*MemberBufferOrErr);
104321369Sdim  M.MemberName = M.Buf->getBufferIdentifier();
105309124Sdim  if (!Deterministic) {
106314564Sdim    M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
107314564Sdim        Status.getLastModificationTime());
108309124Sdim    M.UID = Status.getUser();
109309124Sdim    M.GID = Status.getGroup();
110309124Sdim    M.Perms = Status.permissions();
111309124Sdim  }
112309124Sdim  return std::move(M);
113284184Sdim}
114284184Sdim
115284184Sdimtemplate <typename T>
116327952Sdimstatic void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
117284184Sdim  uint64_t OldPos = OS.tell();
118284184Sdim  OS << Data;
119284184Sdim  unsigned SizeSoFar = OS.tell() - OldPos;
120327952Sdim  assert(SizeSoFar <= Size && "Data doesn't fit in Size");
121327952Sdim  OS.indent(Size - SizeSoFar);
122284184Sdim}
123284184Sdim
124344779Sdimstatic bool isDarwin(object::Archive::Kind Kind) {
125344779Sdim  return Kind == object::Archive::K_DARWIN ||
126344779Sdim         Kind == object::Archive::K_DARWIN64;
127344779Sdim}
128344779Sdim
129321369Sdimstatic bool isBSDLike(object::Archive::Kind Kind) {
130321369Sdim  switch (Kind) {
131321369Sdim  case object::Archive::K_GNU:
132327952Sdim  case object::Archive::K_GNU64:
133321369Sdim    return false;
134321369Sdim  case object::Archive::K_BSD:
135321369Sdim  case object::Archive::K_DARWIN:
136344779Sdim  case object::Archive::K_DARWIN64:
137321369Sdim    return true;
138321369Sdim  case object::Archive::K_COFF:
139321369Sdim    break;
140321369Sdim  }
141321369Sdim  llvm_unreachable("not supported for writting");
142321369Sdim}
143321369Sdim
144327952Sdimtemplate <class T>
145327952Sdimstatic void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) {
146341825Sdim  support::endian::write(Out, Val,
147341825Sdim                         isBSDLike(Kind) ? support::little : support::big);
148284184Sdim}
149284184Sdim
150314564Sdimstatic void printRestOfMemberHeader(
151327952Sdim    raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime,
152360784Sdim    unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
153314564Sdim  printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
154327952Sdim
155327952Sdim  // The format has only 6 chars for uid and gid. Truncate if the provided
156327952Sdim  // values don't fit.
157327952Sdim  printWithSpacePadding(Out, UID % 1000000, 6);
158327952Sdim  printWithSpacePadding(Out, GID % 1000000, 6);
159327952Sdim
160284184Sdim  printWithSpacePadding(Out, format("%o", Perms), 8);
161284184Sdim  printWithSpacePadding(Out, Size, 10);
162284184Sdim  Out << "`\n";
163284184Sdim}
164284184Sdim
165314564Sdimstatic void
166327952SdimprintGNUSmallMemberHeader(raw_ostream &Out, StringRef Name,
167314564Sdim                          const sys::TimePoint<std::chrono::seconds> &ModTime,
168314564Sdim                          unsigned UID, unsigned GID, unsigned Perms,
169360784Sdim                          uint64_t Size) {
170284184Sdim  printWithSpacePadding(Out, Twine(Name) + "/", 16);
171284184Sdim  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
172284184Sdim}
173284184Sdim
174314564Sdimstatic void
175327952SdimprintBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name,
176314564Sdim                     const sys::TimePoint<std::chrono::seconds> &ModTime,
177360784Sdim                     unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
178327952Sdim  uint64_t PosAfterHeader = Pos + 60 + Name.size();
179286684Sdim  // Pad so that even 64 bit object files are aligned.
180360784Sdim  unsigned Pad = offsetToAlignment(PosAfterHeader, Align(8));
181286684Sdim  unsigned NameWithPadding = Name.size() + Pad;
182286684Sdim  printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
183286684Sdim  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
184286684Sdim                          NameWithPadding + Size);
185286684Sdim  Out << Name;
186286684Sdim  while (Pad--)
187286684Sdim    Out.write(uint8_t(0));
188286684Sdim}
189286684Sdim
190296417Sdimstatic bool useStringTable(bool Thin, StringRef Name) {
191321369Sdim  return Thin || Name.size() >= 16 || Name.contains('/');
192296417Sdim}
193296417Sdim
194327952Sdimstatic bool is64BitKind(object::Archive::Kind Kind) {
195327952Sdim  switch (Kind) {
196327952Sdim  case object::Archive::K_GNU:
197327952Sdim  case object::Archive::K_BSD:
198327952Sdim  case object::Archive::K_DARWIN:
199327952Sdim  case object::Archive::K_COFF:
200327952Sdim    return false;
201327952Sdim  case object::Archive::K_DARWIN64:
202327952Sdim  case object::Archive::K_GNU64:
203327952Sdim    return true;
204327952Sdim  }
205327952Sdim  llvm_unreachable("not supported for writting");
206327952Sdim}
207296417Sdim
208353358Sdimstatic void
209353358SdimprintMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable,
210353358Sdim                  StringMap<uint64_t> &MemberNames, object::Archive::Kind Kind,
211353358Sdim                  bool Thin, const NewArchiveMember &M,
212360784Sdim                  sys::TimePoint<std::chrono::seconds> ModTime, uint64_t Size) {
213327952Sdim  if (isBSDLike(Kind))
214344779Sdim    return printBSDMemberHeader(Out, Pos, M.MemberName, ModTime, M.UID, M.GID,
215327952Sdim                                M.Perms, Size);
216327952Sdim  if (!useStringTable(Thin, M.MemberName))
217344779Sdim    return printGNUSmallMemberHeader(Out, M.MemberName, ModTime, M.UID, M.GID,
218327952Sdim                                     M.Perms, Size);
219327952Sdim  Out << '/';
220344779Sdim  uint64_t NamePos;
221344779Sdim  if (Thin) {
222344779Sdim    NamePos = StringTable.tell();
223353358Sdim    StringTable << M.MemberName << "/\n";
224344779Sdim  } else {
225344779Sdim    auto Insertion = MemberNames.insert({M.MemberName, uint64_t(0)});
226344779Sdim    if (Insertion.second) {
227344779Sdim      Insertion.first->second = StringTable.tell();
228353358Sdim      StringTable << M.MemberName << "/\n";
229344779Sdim    }
230344779Sdim    NamePos = Insertion.first->second;
231344779Sdim  }
232327952Sdim  printWithSpacePadding(Out, NamePos, 15);
233344779Sdim  printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size);
234284184Sdim}
235284184Sdim
236327952Sdimnamespace {
237327952Sdimstruct MemberData {
238327952Sdim  std::vector<unsigned> Symbols;
239327952Sdim  std::string Header;
240327952Sdim  StringRef Data;
241327952Sdim  StringRef Padding;
242327952Sdim};
243327952Sdim} // namespace
244327952Sdim
245327952Sdimstatic MemberData computeStringTable(StringRef Names) {
246327952Sdim  unsigned Size = Names.size();
247360784Sdim  unsigned Pad = offsetToAlignment(Size, Align(2));
248327952Sdim  std::string Header;
249327952Sdim  raw_string_ostream Out(Header);
250327952Sdim  printWithSpacePadding(Out, "//", 48);
251327952Sdim  printWithSpacePadding(Out, Size + Pad, 10);
252327952Sdim  Out << "`\n";
253327952Sdim  Out.flush();
254327952Sdim  return {{}, std::move(Header), Names, Pad ? "\n" : ""};
255327952Sdim}
256327952Sdim
257314564Sdimstatic sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
258314564Sdim  using namespace std::chrono;
259314564Sdim
260286684Sdim  if (!Deterministic)
261314564Sdim    return time_point_cast<seconds>(system_clock::now());
262314564Sdim  return sys::TimePoint<seconds>();
263286684Sdim}
264286684Sdim
265327952Sdimstatic bool isArchiveSymbol(const object::BasicSymbolRef &S) {
266327952Sdim  uint32_t Symflags = S.getFlags();
267327952Sdim  if (Symflags & object::SymbolRef::SF_FormatSpecific)
268327952Sdim    return false;
269327952Sdim  if (!(Symflags & object::SymbolRef::SF_Global))
270327952Sdim    return false;
271341825Sdim  if (Symflags & object::SymbolRef::SF_Undefined)
272327952Sdim    return false;
273327952Sdim  return true;
274327952Sdim}
275284184Sdim
276327952Sdimstatic void printNBits(raw_ostream &Out, object::Archive::Kind Kind,
277327952Sdim                       uint64_t Val) {
278327952Sdim  if (is64BitKind(Kind))
279327952Sdim    print<uint64_t>(Out, Kind, Val);
280327952Sdim  else
281327952Sdim    print<uint32_t>(Out, Kind, Val);
282327952Sdim}
283284184Sdim
284327952Sdimstatic void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind,
285327952Sdim                             bool Deterministic, ArrayRef<MemberData> Members,
286327952Sdim                             StringRef StringTable) {
287344779Sdim  // We don't write a symbol table on an archive with no members -- except on
288344779Sdim  // Darwin, where the linker will abort unless the archive has a symbol table.
289344779Sdim  if (StringTable.empty() && !isDarwin(Kind))
290327952Sdim    return;
291286684Sdim
292327952Sdim  unsigned NumSyms = 0;
293327952Sdim  for (const MemberData &M : Members)
294327952Sdim    NumSyms += M.Symbols.size();
295327952Sdim
296327952Sdim  unsigned Size = 0;
297344779Sdim  unsigned OffsetSize = is64BitKind(Kind) ? sizeof(uint64_t) : sizeof(uint32_t);
298344779Sdim
299344779Sdim  Size += OffsetSize; // Number of entries
300327952Sdim  if (isBSDLike(Kind))
301344779Sdim    Size += NumSyms * OffsetSize * 2; // Table
302327952Sdim  else
303344779Sdim    Size += NumSyms * OffsetSize; // Table
304327952Sdim  if (isBSDLike(Kind))
305344779Sdim    Size += OffsetSize; // byte count
306327952Sdim  Size += StringTable.size();
307327952Sdim  // ld64 expects the members to be 8-byte aligned for 64-bit content and at
308327952Sdim  // least 4-byte aligned for 32-bit content.  Opt for the larger encoding
309327952Sdim  // uniformly.
310327952Sdim  // We do this for all bsd formats because it simplifies aligning members.
311360784Sdim  const Align Alignment(isBSDLike(Kind) ? 8 : 2);
312360784Sdim  unsigned Pad = offsetToAlignment(Size, Alignment);
313327952Sdim  Size += Pad;
314327952Sdim
315344779Sdim  if (isBSDLike(Kind)) {
316344779Sdim    const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF";
317344779Sdim    printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0,
318344779Sdim                         Size);
319344779Sdim  } else {
320344779Sdim    const char *Name = is64BitKind(Kind) ? "/SYM64" : "";
321344779Sdim    printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size);
322344779Sdim  }
323327952Sdim
324327952Sdim  uint64_t Pos = Out.tell() + Size;
325327952Sdim
326327952Sdim  if (isBSDLike(Kind))
327344779Sdim    printNBits(Out, Kind, NumSyms * 2 * OffsetSize);
328327952Sdim  else
329327952Sdim    printNBits(Out, Kind, NumSyms);
330327952Sdim
331327952Sdim  for (const MemberData &M : Members) {
332327952Sdim    for (unsigned StringOffset : M.Symbols) {
333321369Sdim      if (isBSDLike(Kind))
334344779Sdim        printNBits(Out, Kind, StringOffset);
335327952Sdim      printNBits(Out, Kind, Pos); // member offset
336284184Sdim    }
337327952Sdim    Pos += M.Header.size() + M.Data.size() + M.Padding.size();
338284184Sdim  }
339284184Sdim
340321369Sdim  if (isBSDLike(Kind))
341327952Sdim    // byte count of the string table
342344779Sdim    printNBits(Out, Kind, StringTable.size());
343286684Sdim  Out << StringTable;
344284184Sdim
345286684Sdim  while (Pad--)
346286684Sdim    Out.write(uint8_t(0));
347327952Sdim}
348286684Sdim
349327952Sdimstatic Expected<std::vector<unsigned>>
350327952SdimgetSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) {
351327952Sdim  std::vector<unsigned> Ret;
352344779Sdim
353344779Sdim  // In the scenario when LLVMContext is populated SymbolicFile will contain a
354344779Sdim  // reference to it, thus SymbolicFile should be destroyed first.
355327952Sdim  LLVMContext Context;
356344779Sdim  std::unique_ptr<object::SymbolicFile> Obj;
357344779Sdim  if (identify_magic(Buf.getBuffer()) == file_magic::bitcode) {
358344779Sdim    auto ObjOrErr = object::SymbolicFile::createSymbolicFile(
359344779Sdim        Buf, file_magic::bitcode, &Context);
360344779Sdim    if (!ObjOrErr) {
361344779Sdim      // FIXME: check only for "not an object file" errors.
362344779Sdim      consumeError(ObjOrErr.takeError());
363344779Sdim      return Ret;
364344779Sdim    }
365344779Sdim    Obj = std::move(*ObjOrErr);
366344779Sdim  } else {
367344779Sdim    auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf);
368344779Sdim    if (!ObjOrErr) {
369344779Sdim      // FIXME: check only for "not an object file" errors.
370344779Sdim      consumeError(ObjOrErr.takeError());
371344779Sdim      return Ret;
372344779Sdim    }
373344779Sdim    Obj = std::move(*ObjOrErr);
374327952Sdim  }
375286684Sdim
376327952Sdim  HasObject = true;
377344779Sdim  for (const object::BasicSymbolRef &S : Obj->symbols()) {
378327952Sdim    if (!isArchiveSymbol(S))
379327952Sdim      continue;
380327952Sdim    Ret.push_back(SymNames.tell());
381353358Sdim    if (Error E = S.printName(SymNames))
382353358Sdim      return std::move(E);
383327952Sdim    SymNames << '\0';
384327952Sdim  }
385327952Sdim  return Ret;
386284184Sdim}
387284184Sdim
388327952Sdimstatic Expected<std::vector<MemberData>>
389327952SdimcomputeMemberData(raw_ostream &StringTable, raw_ostream &SymNames,
390353358Sdim                  object::Archive::Kind Kind, bool Thin, bool Deterministic,
391353358Sdim                  ArrayRef<NewArchiveMember> NewMembers) {
392327952Sdim  static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};
393284184Sdim
394327952Sdim  // This ignores the symbol table, but we only need the value mod 8 and the
395327952Sdim  // symbol table is aligned to be a multiple of 8 bytes
396327952Sdim  uint64_t Pos = 0;
397284184Sdim
398327952Sdim  std::vector<MemberData> Ret;
399327952Sdim  bool HasObject = false;
400344779Sdim
401344779Sdim  // Deduplicate long member names in the string table and reuse earlier name
402344779Sdim  // offsets. This especially saves space for COFF Import libraries where all
403344779Sdim  // members have the same name.
404344779Sdim  StringMap<uint64_t> MemberNames;
405344779Sdim
406344779Sdim  // UniqueTimestamps is a special case to improve debugging on Darwin:
407344779Sdim  //
408344779Sdim  // The Darwin linker does not link debug info into the final
409344779Sdim  // binary. Instead, it emits entries of type N_OSO in in the output
410344779Sdim  // binary's symbol table, containing references to the linked-in
411344779Sdim  // object files. Using that reference, the debugger can read the
412344779Sdim  // debug data directly from the object files. Alternatively, an
413344779Sdim  // invocation of 'dsymutil' will link the debug data from the object
414344779Sdim  // files into a dSYM bundle, which can be loaded by the debugger,
415344779Sdim  // instead of the object files.
416344779Sdim  //
417344779Sdim  // For an object file, the N_OSO entries contain the absolute path
418344779Sdim  // path to the file, and the file's timestamp. For an object
419344779Sdim  // included in an archive, the path is formatted like
420344779Sdim  // "/absolute/path/to/archive.a(member.o)", and the timestamp is the
421344779Sdim  // archive member's timestamp, rather than the archive's timestamp.
422344779Sdim  //
423344779Sdim  // However, this doesn't always uniquely identify an object within
424344779Sdim  // an archive -- an archive file can have multiple entries with the
425344779Sdim  // same filename. (This will happen commonly if the original object
426344779Sdim  // files started in different directories.) The only way they get
427344779Sdim  // distinguished, then, is via the timestamp. But this process is
428344779Sdim  // unable to find the correct object file in the archive when there
429344779Sdim  // are two files of the same name and timestamp.
430344779Sdim  //
431344779Sdim  // Additionally, timestamp==0 is treated specially, and causes the
432344779Sdim  // timestamp to be ignored as a match criteria.
433344779Sdim  //
434344779Sdim  // That will "usually" work out okay when creating an archive not in
435344779Sdim  // deterministic timestamp mode, because the objects will probably
436344779Sdim  // have been created at different timestamps.
437344779Sdim  //
438344779Sdim  // To ameliorate this problem, in deterministic archive mode (which
439344779Sdim  // is the default), on Darwin we will emit a unique non-zero
440344779Sdim  // timestamp for each entry with a duplicated name. This is still
441344779Sdim  // deterministic: the only thing affecting that timestamp is the
442344779Sdim  // order of the files in the resultant archive.
443344779Sdim  //
444344779Sdim  // See also the functions that handle the lookup:
445344779Sdim  // in lldb: ObjectContainerBSDArchive::Archive::FindObject()
446344779Sdim  // in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers().
447344779Sdim  bool UniqueTimestamps = Deterministic && isDarwin(Kind);
448344779Sdim  std::map<StringRef, unsigned> FilenameCount;
449344779Sdim  if (UniqueTimestamps) {
450344779Sdim    for (const NewArchiveMember &M : NewMembers)
451344779Sdim      FilenameCount[M.MemberName]++;
452344779Sdim    for (auto &Entry : FilenameCount)
453344779Sdim      Entry.second = Entry.second > 1 ? 1 : 0;
454344779Sdim  }
455344779Sdim
456309124Sdim  for (const NewArchiveMember &M : NewMembers) {
457327952Sdim    std::string Header;
458327952Sdim    raw_string_ostream Out(Header);
459284184Sdim
460327952Sdim    MemoryBufferRef Buf = M.Buf->getMemBufferRef();
461327952Sdim    StringRef Data = Thin ? "" : Buf.getBuffer();
462284184Sdim
463321369Sdim    // ld64 expects the members to be 8-byte aligned for 64-bit content and at
464321369Sdim    // least 4-byte aligned for 32-bit content.  Opt for the larger encoding
465321369Sdim    // uniformly.  This matches the behaviour with cctools and ensures that ld64
466321369Sdim    // is happy with archives that we generate.
467344779Sdim    unsigned MemberPadding =
468360784Sdim        isDarwin(Kind) ? offsetToAlignment(Data.size(), Align(8)) : 0;
469360784Sdim    unsigned TailPadding =
470360784Sdim        offsetToAlignment(Data.size() + MemberPadding, Align(2));
471327952Sdim    StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding);
472284184Sdim
473344779Sdim    sys::TimePoint<std::chrono::seconds> ModTime;
474344779Sdim    if (UniqueTimestamps)
475344779Sdim      // Increment timestamp for each file of a given name.
476344779Sdim      ModTime = sys::toTimePoint(FilenameCount[M.MemberName]++);
477344779Sdim    else
478344779Sdim      ModTime = M.ModTime;
479360784Sdim
480360784Sdim    uint64_t Size = Buf.getBufferSize() + MemberPadding;
481360784Sdim    if (Size > object::Archive::MaxMemberSize) {
482360784Sdim      std::string StringMsg =
483360784Sdim          "File " + M.MemberName.str() + " exceeds size limit";
484360784Sdim      return make_error<object::GenericBinaryError>(
485360784Sdim          std::move(StringMsg), object::object_error::parse_failed);
486360784Sdim    }
487360784Sdim
488353358Sdim    printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, M,
489360784Sdim                      ModTime, Size);
490327952Sdim    Out.flush();
491321369Sdim
492327952Sdim    Expected<std::vector<unsigned>> Symbols =
493327952Sdim        getSymbols(Buf, SymNames, HasObject);
494327952Sdim    if (auto E = Symbols.takeError())
495327952Sdim      return std::move(E);
496284184Sdim
497327952Sdim    Pos += Header.size() + Data.size() + Padding.size();
498327952Sdim    Ret.push_back({std::move(*Symbols), std::move(Header), Data, Padding});
499284184Sdim  }
500327952Sdim  // If there are no symbols, emit an empty symbol table, to satisfy Solaris
501327952Sdim  // tools, older versions of which expect a symbol table in a non-empty
502327952Sdim  // archive, regardless of whether there are any symbols in it.
503327952Sdim  if (HasObject && SymNames.tell() == 0)
504327952Sdim    SymNames << '\0' << '\0' << '\0';
505327952Sdim  return Ret;
506327952Sdim}
507284184Sdim
508353358Sdimnamespace llvm {
509353358Sdim
510353358Sdimstatic ErrorOr<SmallString<128>> canonicalizePath(StringRef P) {
511353358Sdim  SmallString<128> Ret = P;
512353358Sdim  std::error_code Err = sys::fs::make_absolute(Ret);
513353358Sdim  if (Err)
514353358Sdim    return Err;
515353358Sdim  sys::path::remove_dots(Ret, /*removedotdot*/ true);
516353358Sdim  return Ret;
517353358Sdim}
518353358Sdim
519353358Sdim// Compute the relative path from From to To.
520353358SdimExpected<std::string> computeArchiveRelativePath(StringRef From, StringRef To) {
521353358Sdim  ErrorOr<SmallString<128>> PathToOrErr = canonicalizePath(To);
522353358Sdim  ErrorOr<SmallString<128>> DirFromOrErr = canonicalizePath(From);
523353358Sdim  if (!PathToOrErr || !DirFromOrErr)
524353358Sdim    return errorCodeToError(std::error_code(errno, std::generic_category()));
525353358Sdim
526353358Sdim  const SmallString<128> &PathTo = *PathToOrErr;
527353358Sdim  const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr);
528353358Sdim
529353358Sdim  // Can't construct a relative path between different roots
530353358Sdim  if (sys::path::root_name(PathTo) != sys::path::root_name(DirFrom))
531353358Sdim    return sys::path::convert_to_slash(PathTo);
532353358Sdim
533353358Sdim  // Skip common prefixes
534353358Sdim  auto FromTo =
535353358Sdim      std::mismatch(sys::path::begin(DirFrom), sys::path::end(DirFrom),
536353358Sdim                    sys::path::begin(PathTo));
537353358Sdim  auto FromI = FromTo.first;
538353358Sdim  auto ToI = FromTo.second;
539353358Sdim
540353358Sdim  // Construct relative path
541353358Sdim  SmallString<128> Relative;
542353358Sdim  for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
543353358Sdim    sys::path::append(Relative, sys::path::Style::posix, "..");
544353358Sdim
545353358Sdim  for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI)
546353358Sdim    sys::path::append(Relative, sys::path::Style::posix, *ToI);
547353358Sdim
548353358Sdim  return Relative.str();
549353358Sdim}
550353358Sdim
551353358SdimError writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
552353358Sdim                   bool WriteSymtab, object::Archive::Kind Kind,
553353358Sdim                   bool Deterministic, bool Thin,
554353358Sdim                   std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
555327952Sdim  assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
556327952Sdim
557327952Sdim  SmallString<0> SymNamesBuf;
558327952Sdim  raw_svector_ostream SymNames(SymNamesBuf);
559327952Sdim  SmallString<0> StringTableBuf;
560327952Sdim  raw_svector_ostream StringTable(StringTableBuf);
561327952Sdim
562344779Sdim  Expected<std::vector<MemberData>> DataOrErr = computeMemberData(
563353358Sdim      StringTable, SymNames, Kind, Thin, Deterministic, NewMembers);
564327952Sdim  if (Error E = DataOrErr.takeError())
565327952Sdim    return E;
566327952Sdim  std::vector<MemberData> &Data = *DataOrErr;
567327952Sdim
568327952Sdim  if (!StringTableBuf.empty())
569327952Sdim    Data.insert(Data.begin(), computeStringTable(StringTableBuf));
570327952Sdim
571327952Sdim  // We would like to detect if we need to switch to a 64-bit symbol table.
572327952Sdim  if (WriteSymtab) {
573327952Sdim    uint64_t MaxOffset = 0;
574327952Sdim    uint64_t LastOffset = MaxOffset;
575344779Sdim    for (const auto &M : Data) {
576327952Sdim      // Record the start of the member's offset
577327952Sdim      LastOffset = MaxOffset;
578327952Sdim      // Account for the size of each part associated with the member.
579327952Sdim      MaxOffset += M.Header.size() + M.Data.size() + M.Padding.size();
580327952Sdim      // We assume 32-bit symbols to see if 32-bit symbols are possible or not.
581327952Sdim      MaxOffset += M.Symbols.size() * 4;
582286684Sdim    }
583341825Sdim
584341825Sdim    // The SYM64 format is used when an archive's member offsets are larger than
585341825Sdim    // 32-bits can hold. The need for this shift in format is detected by
586341825Sdim    // writeArchive. To test this we need to generate a file with a member that
587341825Sdim    // has an offset larger than 32-bits but this demands a very slow test. To
588341825Sdim    // speed the test up we use this environment variable to pretend like the
589341825Sdim    // cutoff happens before 32-bits and instead happens at some much smaller
590341825Sdim    // value.
591341825Sdim    const char *Sym64Env = std::getenv("SYM64_THRESHOLD");
592341825Sdim    int Sym64Threshold = 32;
593341825Sdim    if (Sym64Env)
594341825Sdim      StringRef(Sym64Env).getAsInteger(10, Sym64Threshold);
595341825Sdim
596327952Sdim    // If LastOffset isn't going to fit in a 32-bit varible we need to switch
597327952Sdim    // to 64-bit. Note that the file can be larger than 4GB as long as the last
598327952Sdim    // member starts before the 4GB offset.
599344779Sdim    if (LastOffset >= (1ULL << Sym64Threshold)) {
600344779Sdim      if (Kind == object::Archive::K_DARWIN)
601344779Sdim        Kind = object::Archive::K_DARWIN64;
602344779Sdim      else
603344779Sdim        Kind = object::Archive::K_GNU64;
604344779Sdim    }
605284184Sdim  }
606284184Sdim
607327952Sdim  Expected<sys::fs::TempFile> Temp =
608327952Sdim      sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a");
609327952Sdim  if (!Temp)
610327952Sdim    return Temp.takeError();
611309124Sdim
612327952Sdim  raw_fd_ostream Out(Temp->FD, false);
613327952Sdim  if (Thin)
614327952Sdim    Out << "!<thin>\n";
615327952Sdim  else
616327952Sdim    Out << "!<arch>\n";
617327952Sdim
618327952Sdim  if (WriteSymtab)
619327952Sdim    writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf);
620327952Sdim
621327952Sdim  for (const MemberData &M : Data)
622327952Sdim    Out << M.Header << M.Data << M.Padding;
623327952Sdim
624327952Sdim  Out.flush();
625327952Sdim
626309124Sdim  // At this point, we no longer need whatever backing memory
627309124Sdim  // was used to generate the NewMembers. On Windows, this buffer
628309124Sdim  // could be a mapped view of the file we want to replace (if
629309124Sdim  // we're updating an existing archive, say). In that case, the
630309124Sdim  // rename would still succeed, but it would leave behind a
631309124Sdim  // temporary file (actually the original file renamed) because
632309124Sdim  // a file cannot be deleted while there's a handle open on it,
633309124Sdim  // only renamed. So by freeing this buffer, this ensures that
634309124Sdim  // the last open handle on the destination file, if any, is
635309124Sdim  // closed before we attempt to rename.
636309124Sdim  OldArchiveBuf.reset();
637309124Sdim
638327952Sdim  return Temp->keep(ArcName);
639284184Sdim}
640353358Sdim
641353358Sdim} // namespace llvm
642