LLVM  10.0.0svn
ArchiveWriter.cpp
Go to the documentation of this file.
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 
14 #include "llvm/ADT/ArrayRef.h"
15 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Alignment.h"
24 #include "llvm/Support/Errc.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/Path.h"
30 
31 #include <map>
32 
33 #if !defined(_MSC_VER) && !defined(__MINGW32__)
34 #include <unistd.h>
35 #else
36 #include <io.h>
37 #endif
38 
39 using namespace llvm;
40 
42  : Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
43  MemberName(BufRef.getBufferIdentifier()) {}
44 
47  bool Deterministic) {
49  if (!BufOrErr)
50  return BufOrErr.takeError();
51 
53  M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
54  M.MemberName = M.Buf->getBufferIdentifier();
55  if (!Deterministic) {
56  auto ModTimeOrErr = OldMember.getLastModified();
57  if (!ModTimeOrErr)
58  return ModTimeOrErr.takeError();
59  M.ModTime = ModTimeOrErr.get();
60  Expected<unsigned> UIDOrErr = OldMember.getUID();
61  if (!UIDOrErr)
62  return UIDOrErr.takeError();
63  M.UID = UIDOrErr.get();
64  Expected<unsigned> GIDOrErr = OldMember.getGID();
65  if (!GIDOrErr)
66  return GIDOrErr.takeError();
67  M.GID = GIDOrErr.get();
68  Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
69  if (!AccessModeOrErr)
70  return AccessModeOrErr.takeError();
71  M.Perms = AccessModeOrErr.get();
72  }
73  return std::move(M);
74 }
75 
77  bool Deterministic) {
79  auto FDOrErr = sys::fs::openNativeFileForRead(FileName);
80  if (!FDOrErr)
81  return FDOrErr.takeError();
82  sys::fs::file_t FD = *FDOrErr;
84 
85  if (auto EC = sys::fs::status(FD, Status))
86  return errorCodeToError(EC);
87 
88  // Opening a directory doesn't make sense. Let it fail.
89  // Linux cannot open directories with open(2), although
90  // cygwin and *bsd can.
93 
94  ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
95  MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
96  if (!MemberBufferOrErr)
97  return errorCodeToError(MemberBufferOrErr.getError());
98 
99  if (auto EC = sys::fs::closeFile(FD))
100  return errorCodeToError(EC);
101 
103  M.Buf = std::move(*MemberBufferOrErr);
104  M.MemberName = M.Buf->getBufferIdentifier();
105  if (!Deterministic) {
106  M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
107  Status.getLastModificationTime());
108  M.UID = Status.getUser();
109  M.GID = Status.getGroup();
110  M.Perms = Status.permissions();
111  }
112  return std::move(M);
113 }
114 
115 template <typename T>
116 static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
117  uint64_t OldPos = OS.tell();
118  OS << Data;
119  unsigned SizeSoFar = OS.tell() - OldPos;
120  assert(SizeSoFar <= Size && "Data doesn't fit in Size");
121  OS.indent(Size - SizeSoFar);
122 }
123 
125  return Kind == object::Archive::K_DARWIN ||
127 }
128 
130  switch (Kind) {
133  return false;
137  return true;
139  break;
140  }
141  llvm_unreachable("not supported for writting");
142 }
143 
144 template <class T>
145 static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) {
146  support::endian::write(Out, Val,
148 }
149 
152  unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
153  printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
154 
155  // The format has only 6 chars for uid and gid. Truncate if the provided
156  // values don't fit.
157  printWithSpacePadding(Out, UID % 1000000, 6);
158  printWithSpacePadding(Out, GID % 1000000, 6);
159 
160  printWithSpacePadding(Out, format("%o", Perms), 8);
161  printWithSpacePadding(Out, Size, 10);
162  Out << "`\n";
163 }
164 
165 static void
168  unsigned UID, unsigned GID, unsigned Perms,
169  uint64_t Size) {
170  printWithSpacePadding(Out, Twine(Name) + "/", 16);
171  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
172 }
173 
174 static void
177  unsigned UID, unsigned GID, unsigned Perms, uint64_t Size) {
178  uint64_t PosAfterHeader = Pos + 60 + Name.size();
179  // Pad so that even 64 bit object files are aligned.
180  unsigned Pad = offsetToAlignment(PosAfterHeader, Align(8));
181  unsigned NameWithPadding = Name.size() + Pad;
182  printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
183  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
184  NameWithPadding + Size);
185  Out << Name;
186  while (Pad--)
187  Out.write(uint8_t(0));
188 }
189 
190 static bool useStringTable(bool Thin, StringRef Name) {
191  return Thin || Name.size() >= 16 || Name.contains('/');
192 }
193 
195  switch (Kind) {
200  return false;
203  return true;
204  }
205  llvm_unreachable("not supported for writting");
206 }
207 
208 static void
209 printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable,
211  bool Thin, const NewArchiveMember &M,
213  if (isBSDLike(Kind))
214  return printBSDMemberHeader(Out, Pos, M.MemberName, ModTime, M.UID, M.GID,
215  M.Perms, Size);
216  if (!useStringTable(Thin, M.MemberName))
217  return printGNUSmallMemberHeader(Out, M.MemberName, ModTime, M.UID, M.GID,
218  M.Perms, Size);
219  Out << '/';
220  uint64_t NamePos;
221  if (Thin) {
222  NamePos = StringTable.tell();
223  StringTable << M.MemberName << "/\n";
224  } else {
225  auto Insertion = MemberNames.insert({M.MemberName, uint64_t(0)});
226  if (Insertion.second) {
227  Insertion.first->second = StringTable.tell();
228  StringTable << M.MemberName << "/\n";
229  }
230  NamePos = Insertion.first->second;
231  }
232  printWithSpacePadding(Out, NamePos, 15);
233  printRestOfMemberHeader(Out, ModTime, M.UID, M.GID, M.Perms, Size);
234 }
235 
236 namespace {
237 struct MemberData {
238  std::vector<unsigned> Symbols;
239  std::string Header;
240  StringRef Data;
241  StringRef Padding;
242 };
243 } // namespace
244 
245 static MemberData computeStringTable(StringRef Names) {
246  unsigned Size = Names.size();
247  unsigned Pad = offsetToAlignment(Size, Align(2));
248  std::string Header;
249  raw_string_ostream Out(Header);
250  printWithSpacePadding(Out, "//", 48);
251  printWithSpacePadding(Out, Size + Pad, 10);
252  Out << "`\n";
253  Out.flush();
254  return {{}, std::move(Header), Names, Pad ? "\n" : ""};
255 }
256 
257 static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
258  using namespace std::chrono;
259 
260  if (!Deterministic)
261  return time_point_cast<seconds>(system_clock::now());
262  return sys::TimePoint<seconds>();
263 }
264 
266  uint32_t Symflags = S.getFlags();
268  return false;
269  if (!(Symflags & object::SymbolRef::SF_Global))
270  return false;
271  if (Symflags & object::SymbolRef::SF_Undefined)
272  return false;
273  return true;
274 }
275 
277  uint64_t Val) {
278  if (is64BitKind(Kind))
279  print<uint64_t>(Out, Kind, Val);
280  else
281  print<uint32_t>(Out, Kind, Val);
282 }
283 
285  bool Deterministic, ArrayRef<MemberData> Members,
286  StringRef StringTable) {
287  // We don't write a symbol table on an archive with no members -- except on
288  // Darwin, where the linker will abort unless the archive has a symbol table.
289  if (StringTable.empty() && !isDarwin(Kind))
290  return;
291 
292  unsigned NumSyms = 0;
293  for (const MemberData &M : Members)
294  NumSyms += M.Symbols.size();
295 
296  unsigned Size = 0;
297  unsigned OffsetSize = is64BitKind(Kind) ? sizeof(uint64_t) : sizeof(uint32_t);
298 
299  Size += OffsetSize; // Number of entries
300  if (isBSDLike(Kind))
301  Size += NumSyms * OffsetSize * 2; // Table
302  else
303  Size += NumSyms * OffsetSize; // Table
304  if (isBSDLike(Kind))
305  Size += OffsetSize; // byte count
306  Size += StringTable.size();
307  // ld64 expects the members to be 8-byte aligned for 64-bit content and at
308  // least 4-byte aligned for 32-bit content. Opt for the larger encoding
309  // uniformly.
310  // We do this for all bsd formats because it simplifies aligning members.
311  const Align Alignment(isBSDLike(Kind) ? 8 : 2);
312  unsigned Pad = offsetToAlignment(Size, Alignment);
313  Size += Pad;
314 
315  if (isBSDLike(Kind)) {
316  const char *Name = is64BitKind(Kind) ? "__.SYMDEF_64" : "__.SYMDEF";
317  printBSDMemberHeader(Out, Out.tell(), Name, now(Deterministic), 0, 0, 0,
318  Size);
319  } else {
320  const char *Name = is64BitKind(Kind) ? "/SYM64" : "";
321  printGNUSmallMemberHeader(Out, Name, now(Deterministic), 0, 0, 0, Size);
322  }
323 
324  uint64_t Pos = Out.tell() + Size;
325 
326  if (isBSDLike(Kind))
327  printNBits(Out, Kind, NumSyms * 2 * OffsetSize);
328  else
329  printNBits(Out, Kind, NumSyms);
330 
331  for (const MemberData &M : Members) {
332  for (unsigned StringOffset : M.Symbols) {
333  if (isBSDLike(Kind))
334  printNBits(Out, Kind, StringOffset);
335  printNBits(Out, Kind, Pos); // member offset
336  }
337  Pos += M.Header.size() + M.Data.size() + M.Padding.size();
338  }
339 
340  if (isBSDLike(Kind))
341  // byte count of the string table
342  printNBits(Out, Kind, StringTable.size());
343  Out << StringTable;
344 
345  while (Pad--)
346  Out.write(uint8_t(0));
347 }
348 
350 getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) {
351  std::vector<unsigned> Ret;
352 
353  // In the scenario when LLVMContext is populated SymbolicFile will contain a
354  // reference to it, thus SymbolicFile should be destroyed first.
356  std::unique_ptr<object::SymbolicFile> Obj;
359  Buf, file_magic::bitcode, &Context);
360  if (!ObjOrErr) {
361  // FIXME: check only for "not an object file" errors.
362  consumeError(ObjOrErr.takeError());
363  return Ret;
364  }
365  Obj = std::move(*ObjOrErr);
366  } else {
367  auto ObjOrErr = object::SymbolicFile::createSymbolicFile(Buf);
368  if (!ObjOrErr) {
369  // FIXME: check only for "not an object file" errors.
370  consumeError(ObjOrErr.takeError());
371  return Ret;
372  }
373  Obj = std::move(*ObjOrErr);
374  }
375 
376  HasObject = true;
377  for (const object::BasicSymbolRef &S : Obj->symbols()) {
378  if (!isArchiveSymbol(S))
379  continue;
380  Ret.push_back(SymNames.tell());
381  if (Error E = S.printName(SymNames))
382  return std::move(E);
383  SymNames << '\0';
384  }
385  return Ret;
386 }
387 
389 computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames,
390  object::Archive::Kind Kind, bool Thin, bool Deterministic,
391  ArrayRef<NewArchiveMember> NewMembers) {
392  static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};
393 
394  // This ignores the symbol table, but we only need the value mod 8 and the
395  // symbol table is aligned to be a multiple of 8 bytes
396  uint64_t Pos = 0;
397 
398  std::vector<MemberData> Ret;
399  bool HasObject = false;
400 
401  // Deduplicate long member names in the string table and reuse earlier name
402  // offsets. This especially saves space for COFF Import libraries where all
403  // members have the same name.
404  StringMap<uint64_t> MemberNames;
405 
406  // UniqueTimestamps is a special case to improve debugging on Darwin:
407  //
408  // The Darwin linker does not link debug info into the final
409  // binary. Instead, it emits entries of type N_OSO in in the output
410  // binary's symbol table, containing references to the linked-in
411  // object files. Using that reference, the debugger can read the
412  // debug data directly from the object files. Alternatively, an
413  // invocation of 'dsymutil' will link the debug data from the object
414  // files into a dSYM bundle, which can be loaded by the debugger,
415  // instead of the object files.
416  //
417  // For an object file, the N_OSO entries contain the absolute path
418  // path to the file, and the file's timestamp. For an object
419  // included in an archive, the path is formatted like
420  // "/absolute/path/to/archive.a(member.o)", and the timestamp is the
421  // archive member's timestamp, rather than the archive's timestamp.
422  //
423  // However, this doesn't always uniquely identify an object within
424  // an archive -- an archive file can have multiple entries with the
425  // same filename. (This will happen commonly if the original object
426  // files started in different directories.) The only way they get
427  // distinguished, then, is via the timestamp. But this process is
428  // unable to find the correct object file in the archive when there
429  // are two files of the same name and timestamp.
430  //
431  // Additionally, timestamp==0 is treated specially, and causes the
432  // timestamp to be ignored as a match criteria.
433  //
434  // That will "usually" work out okay when creating an archive not in
435  // deterministic timestamp mode, because the objects will probably
436  // have been created at different timestamps.
437  //
438  // To ameliorate this problem, in deterministic archive mode (which
439  // is the default), on Darwin we will emit a unique non-zero
440  // timestamp for each entry with a duplicated name. This is still
441  // deterministic: the only thing affecting that timestamp is the
442  // order of the files in the resultant archive.
443  //
444  // See also the functions that handle the lookup:
445  // in lldb: ObjectContainerBSDArchive::Archive::FindObject()
446  // in llvm/tools/dsymutil: BinaryHolder::GetArchiveMemberBuffers().
447  bool UniqueTimestamps = Deterministic && isDarwin(Kind);
448  std::map<StringRef, unsigned> FilenameCount;
449  if (UniqueTimestamps) {
450  for (const NewArchiveMember &M : NewMembers)
451  FilenameCount[M.MemberName]++;
452  for (auto &Entry : FilenameCount)
453  Entry.second = Entry.second > 1 ? 1 : 0;
454  }
455 
456  for (const NewArchiveMember &M : NewMembers) {
457  std::string Header;
458  raw_string_ostream Out(Header);
459 
460  MemoryBufferRef Buf = M.Buf->getMemBufferRef();
461  StringRef Data = Thin ? "" : Buf.getBuffer();
462 
463  // ld64 expects the members to be 8-byte aligned for 64-bit content and at
464  // least 4-byte aligned for 32-bit content. Opt for the larger encoding
465  // uniformly. This matches the behaviour with cctools and ensures that ld64
466  // is happy with archives that we generate.
467  unsigned MemberPadding =
468  isDarwin(Kind) ? offsetToAlignment(Data.size(), Align(8)) : 0;
469  unsigned TailPadding =
470  offsetToAlignment(Data.size() + MemberPadding, Align(2));
471  StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding);
472 
474  if (UniqueTimestamps)
475  // Increment timestamp for each file of a given name.
476  ModTime = sys::toTimePoint(FilenameCount[M.MemberName]++);
477  else
478  ModTime = M.ModTime;
479 
480  uint64_t Size = Buf.getBufferSize() + MemberPadding;
481  if (Size > object::Archive::MaxMemberSize) {
482  std::string StringMsg =
483  "File " + M.MemberName.str() + " exceeds size limit";
484  return make_error<object::GenericBinaryError>(
485  std::move(StringMsg), object::object_error::parse_failed);
486  }
487 
488  printMemberHeader(Out, Pos, StringTable, MemberNames, Kind, Thin, M,
489  ModTime, Size);
490  Out.flush();
491 
493  getSymbols(Buf, SymNames, HasObject);
494  if (auto E = Symbols.takeError())
495  return std::move(E);
496 
497  Pos += Header.size() + Data.size() + Padding.size();
498  Ret.push_back({std::move(*Symbols), std::move(Header), Data, Padding});
499  }
500  // If there are no symbols, emit an empty symbol table, to satisfy Solaris
501  // tools, older versions of which expect a symbol table in a non-empty
502  // archive, regardless of whether there are any symbols in it.
503  if (HasObject && SymNames.tell() == 0)
504  SymNames << '\0' << '\0' << '\0';
505  return Ret;
506 }
507 
508 namespace llvm {
509 
512  std::error_code Err = sys::fs::make_absolute(Ret);
513  if (Err)
514  return Err;
515  sys::path::remove_dots(Ret, /*removedotdot*/ true);
516  return Ret;
517 }
518 
519 // Compute the relative path from From to To.
521  ErrorOr<SmallString<128>> PathToOrErr = canonicalizePath(To);
522  ErrorOr<SmallString<128>> DirFromOrErr = canonicalizePath(From);
523  if (!PathToOrErr || !DirFromOrErr)
524  return errorCodeToError(std::error_code(errno, std::generic_category()));
525 
526  const SmallString<128> &PathTo = *PathToOrErr;
527  const SmallString<128> &DirFrom = sys::path::parent_path(*DirFromOrErr);
528 
529  // Can't construct a relative path between different roots
530  if (sys::path::root_name(PathTo) != sys::path::root_name(DirFrom))
531  return sys::path::convert_to_slash(PathTo);
532 
533  // Skip common prefixes
534  auto FromTo =
535  std::mismatch(sys::path::begin(DirFrom), sys::path::end(DirFrom),
536  sys::path::begin(PathTo));
537  auto FromI = FromTo.first;
538  auto ToI = FromTo.second;
539 
540  // Construct relative path
541  SmallString<128> Relative;
542  for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
544 
545  for (auto ToE = sys::path::end(PathTo); ToI != ToE; ++ToI)
547 
548  return Relative.str();
549 }
550 
552  bool WriteSymtab, object::Archive::Kind Kind,
553  bool Deterministic, bool Thin,
554  std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
555  assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
556 
557  SmallString<0> SymNamesBuf;
558  raw_svector_ostream SymNames(SymNamesBuf);
559  SmallString<0> StringTableBuf;
560  raw_svector_ostream StringTable(StringTableBuf);
561 
563  StringTable, SymNames, Kind, Thin, Deterministic, NewMembers);
564  if (Error E = DataOrErr.takeError())
565  return E;
566  std::vector<MemberData> &Data = *DataOrErr;
567 
568  if (!StringTableBuf.empty())
569  Data.insert(Data.begin(), computeStringTable(StringTableBuf));
570 
571  // We would like to detect if we need to switch to a 64-bit symbol table.
572  if (WriteSymtab) {
573  uint64_t MaxOffset = 0;
574  uint64_t LastOffset = MaxOffset;
575  for (const auto &M : Data) {
576  // Record the start of the member's offset
577  LastOffset = MaxOffset;
578  // Account for the size of each part associated with the member.
579  MaxOffset += M.Header.size() + M.Data.size() + M.Padding.size();
580  // We assume 32-bit symbols to see if 32-bit symbols are possible or not.
581  MaxOffset += M.Symbols.size() * 4;
582  }
583 
584  // The SYM64 format is used when an archive's member offsets are larger than
585  // 32-bits can hold. The need for this shift in format is detected by
586  // writeArchive. To test this we need to generate a file with a member that
587  // has an offset larger than 32-bits but this demands a very slow test. To
588  // speed the test up we use this environment variable to pretend like the
589  // cutoff happens before 32-bits and instead happens at some much smaller
590  // value.
591  const char *Sym64Env = std::getenv("SYM64_THRESHOLD");
592  int Sym64Threshold = 32;
593  if (Sym64Env)
594  StringRef(Sym64Env).getAsInteger(10, Sym64Threshold);
595 
596  // If LastOffset isn't going to fit in a 32-bit varible we need to switch
597  // to 64-bit. Note that the file can be larger than 4GB as long as the last
598  // member starts before the 4GB offset.
599  if (LastOffset >= (1ULL << Sym64Threshold)) {
600  if (Kind == object::Archive::K_DARWIN)
602  else
604  }
605  }
606 
608  sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a");
609  if (!Temp)
610  return Temp.takeError();
611 
612  raw_fd_ostream Out(Temp->FD, false);
613  if (Thin)
614  Out << "!<thin>\n";
615  else
616  Out << "!<arch>\n";
617 
618  if (WriteSymtab)
619  writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf);
620 
621  for (const MemberData &M : Data)
622  Out << M.Header << M.Data << M.Padding;
623 
624  Out.flush();
625 
626  // At this point, we no longer need whatever backing memory
627  // was used to generate the NewMembers. On Windows, this buffer
628  // could be a mapped view of the file we want to replace (if
629  // we're updating an existing archive, say). In that case, the
630  // rename would still succeed, but it would leave behind a
631  // temporary file (actually the original file renamed) because
632  // a file cannot be deleted while there's a handle open on it,
633  // only renamed. So by freeing this buffer, this ensures that
634  // the last open handle on the destination file, if any, is
635  // closed before we attempt to rename.
636  OldArchiveBuf.reset();
637 
638  return Temp->keep(ArcName);
639 }
640 
641 } // namespace llvm
const file_t kInvalidFile
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:233
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
Represents either an error or a value T.
Definition: ErrorOr.h:56
static void printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
LLVMContext & Context
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:224
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, llvm::file_magic Type, LLVMContext *Context)
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
raw_ostream & indent(unsigned NumSpaces)
indent - Insert &#39;NumSpaces&#39; spaces.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:530
std::string convert_to_slash(StringRef path, Style style=Style::native)
Replaces backslashes with slashes if Windows.
Definition: Path.cpp:556
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
static Expected< TempFile > create(const Twine &Model, unsigned Mode=all_read|all_write)
This creates a temporary file with createUniqueFile and schedules it for deletion with sys::RemoveFil...
Definition: Path.cpp:1227
Represents the result of a call to sys::fs::status().
Definition: FileSystem.h:246
static void printRestOfMemberHeader(raw_ostream &Out, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: Alignment.h:193
static void printMemberHeader(raw_ostream &Out, uint64_t Pos, raw_ostream &StringTable, StringMap< uint64_t > &MemberNames, object::Archive::Kind Kind, bool Thin, const NewArchiveMember &M, sys::TimePoint< std::chrono::seconds > ModTime, uint64_t Size)
Error writeArchive(StringRef ArcName, ArrayRef< NewArchiveMember > NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr)
Bitcode file.
Definition: Magic.h:23
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:455
static bool is64BitKind(object::Archive::Kind Kind)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
static Expected< std::vector< unsigned > > getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject)
void make_absolute(const Twine &current_directory, SmallVectorImpl< char > &path)
Make path an absolute path.
Definition: Path.cpp:853
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition: Magic.cpp:34
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatile=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
std::error_code make_error_code(BitcodeError E)
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:140
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:266
static ErrorOr< SmallString< 128 > > canonicalizePath(StringRef P)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:144
static bool isBSDLike(object::Archive::Kind Kind)
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
Definition: Endian.h:99
#define P(N)
static Expected< std::vector< MemberData > > computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames, object::Archive::Kind Kind, bool Thin, bool Deterministic, ArrayRef< NewArchiveMember > NewMembers)
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.h:116
size_t getBufferSize() const
Definition: MemoryBuffer.h:278
std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:36
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
std::error_code status(const Twine &path, file_status &result, bool follow=true)
Get file status as if by POSIX stat().
static void printNBits(raw_ostream &Out, object::Archive::Kind Kind, uint64_t Val)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
static const uint64_t MaxMemberSize
Size field is 10 decimal digits long.
Definition: Archive.h:225
std::error_code getError() const
Definition: ErrorOr.h:159
TimePoint getLastModificationTime() const
The file modification time as reported from the underlying file system.
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
static bool isDarwin(object::Archive::Kind Kind)
Expected< unsigned > getUID() const
Definition: Archive.h:124
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
LLVM_NODISCARD bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:446
StringRef parent_path(StringRef path, Style style=Style::native)
Get parent path.
Definition: Path.cpp:466
static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size)
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1001
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:40
raw_ostream & write(unsigned char C)
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.h:127
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:492
BlockVerifier::State From
bool remove_dots(SmallVectorImpl< char > &path, bool remove_dot_dot=false, Style style=Style::native)
In-place remove any &#39;.
Definition: Path.cpp:716
static bool useStringTable(bool Thin, StringRef Name)
StringRef root_name(StringRef path, Style style=Style::native)
Get root name.
Definition: Path.cpp:372
Expected< file_t > openNativeFileForRead(const Twine &Name, OpenFlags Flags=OF_None, SmallVectorImpl< char > *RealPath=nullptr)
Opens the file with the given name in a read-only mode, returning its open file descriptor.
reference get()
Returns a reference to the stored T value.
Definition: Error.h:532
std::error_code closeFile(file_t &F)
Close the file object.
static Expected< NewArchiveMember > getOldMember(const object::Archive::Child &OldMember, bool Deterministic)
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:393
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:41
static bool isArchiveSymbol(const object::BasicSymbolRef &S)
Expected< MemoryBufferRef > getMemoryBufferRef() const
Definition: Archive.cpp:507
static MemberData computeStringTable(StringRef Names)
Expected< std::string > computeArchiveRelativePath(StringRef From, StringRef To)
uint32_t getFlags() const
Get symbol flags (bitwise OR of SymbolRef::Flags)
Definition: SymbolicFile.h:199
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:384
static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind, bool Deterministic, ArrayRef< MemberData > Members, StringRef StringTable)
static void printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, uint64_t Size)
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:55
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:98
uint32_t Size
Definition: Profile.cpp:46
static Expected< NewArchiveMember > getFile(StringRef FileName, bool Deterministic)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:503
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:111
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
std::unique_ptr< MemoryBuffer > Buf
Definition: ArchiveWriter.h:24
Expected< unsigned > getGID() const
Definition: Archive.h:125
TimePoint< std::chrono::seconds > toTimePoint(std::time_t T)
Convert a std::time_t to a TimePoint.
Definition: Chrono.h:44
sys::TimePoint< std::chrono::seconds > ModTime
Definition: ArchiveWriter.h:26
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:33