LLVM  4.0.0
ArchiveWriter.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/ObjectFile.h"
22 #include "llvm/Support/Errc.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/Path.h"
28 
29 #if !defined(_MSC_VER) && !defined(__MINGW32__)
30 #include <unistd.h>
31 #else
32 #include <io.h>
33 #endif
34 
35 using namespace llvm;
36 
38  : Buf(MemoryBuffer::getMemBuffer(BufRef, false)) {}
39 
42  bool Deterministic) {
44  if (!BufOrErr)
45  return BufOrErr.takeError();
46 
48  assert(M.IsNew == false);
49  M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
50  if (!Deterministic) {
51  auto ModTimeOrErr = OldMember.getLastModified();
52  if (!ModTimeOrErr)
53  return ModTimeOrErr.takeError();
54  M.ModTime = ModTimeOrErr.get();
55  Expected<unsigned> UIDOrErr = OldMember.getUID();
56  if (!UIDOrErr)
57  return UIDOrErr.takeError();
58  M.UID = UIDOrErr.get();
59  Expected<unsigned> GIDOrErr = OldMember.getGID();
60  if (!GIDOrErr)
61  return GIDOrErr.takeError();
62  M.GID = GIDOrErr.get();
63  Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
64  if (!AccessModeOrErr)
65  return AccessModeOrErr.takeError();
66  M.Perms = AccessModeOrErr.get();
67  }
68  return std::move(M);
69 }
70 
72  bool Deterministic) {
74  int FD;
75  if (auto EC = sys::fs::openFileForRead(FileName, FD))
76  return errorCodeToError(EC);
77  assert(FD != -1);
78 
79  if (auto EC = sys::fs::status(FD, Status))
80  return errorCodeToError(EC);
81 
82  // Opening a directory doesn't make sense. Let it fail.
83  // Linux cannot open directories with open(2), although
84  // cygwin and *bsd can.
87 
88  ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
89  MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
90  if (!MemberBufferOrErr)
91  return errorCodeToError(MemberBufferOrErr.getError());
92 
93  if (close(FD) != 0)
94  return errorCodeToError(std::error_code(errno, std::generic_category()));
95 
97  M.IsNew = true;
98  M.Buf = std::move(*MemberBufferOrErr);
99  if (!Deterministic) {
100  M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
101  Status.getLastModificationTime());
102  M.UID = Status.getUser();
103  M.GID = Status.getGroup();
104  M.Perms = Status.permissions();
105  }
106  return std::move(M);
107 }
108 
109 template <typename T>
110 static void printWithSpacePadding(raw_fd_ostream &OS, T Data, unsigned Size,
111  bool MayTruncate = false) {
112  uint64_t OldPos = OS.tell();
113  OS << Data;
114  unsigned SizeSoFar = OS.tell() - OldPos;
115  if (Size > SizeSoFar) {
116  OS.indent(Size - SizeSoFar);
117  } else if (Size < SizeSoFar) {
118  assert(MayTruncate && "Data doesn't fit in Size");
119  // Some of the data this is used for (like UID) can be larger than the
120  // space available in the archive format. Truncate in that case.
121  OS.seek(OldPos + Size);
122  }
123 }
124 
126  uint32_t Val) {
127  if (Kind == object::Archive::K_GNU)
129  else
131 }
132 
135  unsigned UID, unsigned GID, unsigned Perms, unsigned Size) {
136  printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
137  printWithSpacePadding(Out, UID, 6, true);
138  printWithSpacePadding(Out, GID, 6, true);
139  printWithSpacePadding(Out, format("%o", Perms), 8);
140  printWithSpacePadding(Out, Size, 10);
141  Out << "`\n";
142 }
143 
144 static void
147  unsigned UID, unsigned GID, unsigned Perms,
148  unsigned Size) {
149  printWithSpacePadding(Out, Twine(Name) + "/", 16);
150  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
151 }
152 
153 static void
156  unsigned UID, unsigned GID, unsigned Perms,
157  unsigned Size) {
158  uint64_t PosAfterHeader = Out.tell() + 60 + Name.size();
159  // Pad so that even 64 bit object files are aligned.
160  unsigned Pad = OffsetToAlignment(PosAfterHeader, 8);
161  unsigned NameWithPadding = Name.size() + Pad;
162  printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
163  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
164  NameWithPadding + Size);
165  Out << Name;
166  assert(PosAfterHeader == Out.tell());
167  while (Pad--)
168  Out.write(uint8_t(0));
169 }
170 
171 static bool useStringTable(bool Thin, StringRef Name) {
172  return Thin || Name.size() >= 16;
173 }
174 
175 static void
177  StringRef Name,
178  std::vector<unsigned>::iterator &StringMapIndexIter,
180  unsigned UID, unsigned GID, unsigned Perms, unsigned Size) {
181  if (Kind == object::Archive::K_BSD)
182  return printBSDMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size);
183  if (!useStringTable(Thin, Name))
184  return printGNUSmallMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size);
185  Out << '/';
186  printWithSpacePadding(Out, *StringMapIndexIter++, 15);
187  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
188 }
189 
190 // Compute the relative path from From to To.
191 static std::string computeRelativePath(StringRef From, StringRef To) {
193  return To;
194 
195  StringRef DirFrom = sys::path::parent_path(From);
196  auto FromI = sys::path::begin(DirFrom);
197  auto ToI = sys::path::begin(To);
198  while (*FromI == *ToI) {
199  ++FromI;
200  ++ToI;
201  }
202 
203  SmallString<128> Relative;
204  for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
205  sys::path::append(Relative, "..");
206 
207  for (auto ToE = sys::path::end(To); ToI != ToE; ++ToI)
208  sys::path::append(Relative, *ToI);
209 
210 #ifdef LLVM_ON_WIN32
211  // Replace backslashes with slashes so that the path is portable between *nix
212  // and Windows.
213  std::replace(Relative.begin(), Relative.end(), '\\', '/');
214 #endif
215 
216  return Relative.str();
217 }
218 
219 static void writeStringTable(raw_fd_ostream &Out, StringRef ArcName,
221  std::vector<unsigned> &StringMapIndexes,
222  bool Thin) {
223  unsigned StartOffset = 0;
224  for (const NewArchiveMember &M : Members) {
225  StringRef Path = M.Buf->getBufferIdentifier();
227  if (!useStringTable(Thin, Name))
228  continue;
229  if (StartOffset == 0) {
230  printWithSpacePadding(Out, "//", 58);
231  Out << "`\n";
232  StartOffset = Out.tell();
233  }
234  StringMapIndexes.push_back(Out.tell() - StartOffset);
235 
236  if (Thin) {
237  if (M.IsNew)
238  Out << computeRelativePath(ArcName, Path);
239  else
240  Out << M.Buf->getBufferIdentifier();
241  } else
242  Out << Name;
243 
244  Out << "/\n";
245  }
246  if (StartOffset == 0)
247  return;
248  if (Out.tell() % 2)
249  Out << '\n';
250  int Pos = Out.tell();
251  Out.seek(StartOffset - 12);
252  printWithSpacePadding(Out, Pos - StartOffset, 10);
253  Out.seek(Pos);
254 }
255 
256 static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
257  using namespace std::chrono;
258 
259  if (!Deterministic)
260  return time_point_cast<seconds>(system_clock::now());
261  return sys::TimePoint<seconds>();
262 }
263 
264 // Returns the offset of the first reference to a member offset.
265 static ErrorOr<unsigned>
268  std::vector<unsigned> &MemberOffsetRefs, bool Deterministic) {
269  unsigned HeaderStartOffset = 0;
270  unsigned BodyStartOffset = 0;
271  SmallString<128> NameBuf;
272  raw_svector_ostream NameOS(NameBuf);
274  for (unsigned MemberNum = 0, N = Members.size(); MemberNum < N; ++MemberNum) {
275  MemoryBufferRef MemberBuffer = Members[MemberNum].Buf->getMemBufferRef();
278  MemberBuffer, sys::fs::file_magic::unknown, &Context);
279  if (!ObjOrErr) {
280  // FIXME: check only for "not an object file" errors.
281  consumeError(ObjOrErr.takeError());
282  continue;
283  }
284  object::SymbolicFile &Obj = *ObjOrErr.get();
285 
286  if (!HeaderStartOffset) {
287  HeaderStartOffset = Out.tell();
288  if (Kind == object::Archive::K_GNU)
289  printGNUSmallMemberHeader(Out, "", now(Deterministic), 0, 0, 0, 0);
290  else
291  printBSDMemberHeader(Out, "__.SYMDEF", now(Deterministic), 0, 0, 0, 0);
292  BodyStartOffset = Out.tell();
293  print32(Out, Kind, 0); // number of entries or bytes
294  }
295 
296  for (const object::BasicSymbolRef &S : Obj.symbols()) {
297  uint32_t Symflags = S.getFlags();
299  continue;
300  if (!(Symflags & object::SymbolRef::SF_Global))
301  continue;
302  if (Symflags & object::SymbolRef::SF_Undefined)
303  continue;
304 
305  unsigned NameOffset = NameOS.tell();
306  if (auto EC = S.printName(NameOS))
307  return EC;
308  NameOS << '\0';
309  MemberOffsetRefs.push_back(MemberNum);
310  if (Kind == object::Archive::K_BSD)
311  print32(Out, Kind, NameOffset);
312  print32(Out, Kind, 0); // member offset
313  }
314  }
315 
316  if (HeaderStartOffset == 0)
317  return 0;
318 
319  StringRef StringTable = NameOS.str();
320  if (Kind == object::Archive::K_BSD)
321  print32(Out, Kind, StringTable.size()); // byte count of the string table
322  Out << StringTable;
323 
324  // ld64 requires the next member header to start at an offset that is
325  // 4 bytes aligned.
326  unsigned Pad = OffsetToAlignment(Out.tell(), 4);
327  while (Pad--)
328  Out.write(uint8_t(0));
329 
330  // Patch up the size of the symbol table now that we know how big it is.
331  unsigned Pos = Out.tell();
332  const unsigned MemberHeaderSize = 60;
333  Out.seek(HeaderStartOffset + 48); // offset of the size field.
334  printWithSpacePadding(Out, Pos - MemberHeaderSize - HeaderStartOffset, 10);
335 
336  // Patch up the number of symbols.
337  Out.seek(BodyStartOffset);
338  unsigned NumSyms = MemberOffsetRefs.size();
339  if (Kind == object::Archive::K_GNU)
340  print32(Out, Kind, NumSyms);
341  else
342  print32(Out, Kind, NumSyms * 8);
343 
344  Out.seek(Pos);
345  return BodyStartOffset + 4;
346 }
347 
348 std::pair<StringRef, std::error_code>
350  std::vector<NewArchiveMember> &NewMembers,
351  bool WriteSymtab, object::Archive::Kind Kind,
352  bool Deterministic, bool Thin,
353  std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
354  assert((!Thin || Kind == object::Archive::K_GNU) &&
355  "Only the gnu format has a thin mode");
356  SmallString<128> TmpArchive;
357  int TmpArchiveFD;
358  if (auto EC = sys::fs::createUniqueFile(ArcName + ".temp-archive-%%%%%%%.a",
359  TmpArchiveFD, TmpArchive))
360  return std::make_pair(ArcName, EC);
361 
362  tool_output_file Output(TmpArchive, TmpArchiveFD);
363  raw_fd_ostream &Out = Output.os();
364  if (Thin)
365  Out << "!<thin>\n";
366  else
367  Out << "!<arch>\n";
368 
369  std::vector<unsigned> MemberOffsetRefs;
370 
371  std::vector<std::unique_ptr<MemoryBuffer>> Buffers;
372  std::vector<MemoryBufferRef> Members;
373  std::vector<sys::fs::file_status> NewMemberStatus;
374 
375  unsigned MemberReferenceOffset = 0;
376  if (WriteSymtab) {
377  ErrorOr<unsigned> MemberReferenceOffsetOrErr = writeSymbolTable(
378  Out, Kind, NewMembers, MemberOffsetRefs, Deterministic);
379  if (auto EC = MemberReferenceOffsetOrErr.getError())
380  return std::make_pair(ArcName, EC);
381  MemberReferenceOffset = MemberReferenceOffsetOrErr.get();
382  }
383 
384  std::vector<unsigned> StringMapIndexes;
385  if (Kind != object::Archive::K_BSD)
386  writeStringTable(Out, ArcName, NewMembers, StringMapIndexes, Thin);
387 
388  std::vector<unsigned>::iterator StringMapIndexIter = StringMapIndexes.begin();
389  std::vector<unsigned> MemberOffset;
390  for (const NewArchiveMember &M : NewMembers) {
391  MemoryBufferRef File = M.Buf->getMemBufferRef();
392 
393  unsigned Pos = Out.tell();
394  MemberOffset.push_back(Pos);
395 
396  printMemberHeader(Out, Kind, Thin,
397  sys::path::filename(M.Buf->getBufferIdentifier()),
398  StringMapIndexIter, M.ModTime, M.UID, M.GID, M.Perms,
399  M.Buf->getBufferSize());
400 
401  if (!Thin)
402  Out << File.getBuffer();
403 
404  if (Out.tell() % 2)
405  Out << '\n';
406  }
407 
408  if (MemberReferenceOffset) {
409  Out.seek(MemberReferenceOffset);
410  for (unsigned MemberNum : MemberOffsetRefs) {
411  if (Kind == object::Archive::K_BSD)
412  Out.seek(Out.tell() + 4); // skip over the string offset
413  print32(Out, Kind, MemberOffset[MemberNum]);
414  }
415  }
416 
417  Output.keep();
418  Out.close();
419 
420  // At this point, we no longer need whatever backing memory
421  // was used to generate the NewMembers. On Windows, this buffer
422  // could be a mapped view of the file we want to replace (if
423  // we're updating an existing archive, say). In that case, the
424  // rename would still succeed, but it would leave behind a
425  // temporary file (actually the original file renamed) because
426  // a file cannot be deleted while there's a handle open on it,
427  // only renamed. So by freeing this buffer, this ensures that
428  // the last open handle on the destination file, if any, is
429  // closed before we attempt to rename.
430  OldArchiveBuf.reset();
431 
432  sys::fs::rename(TmpArchive, ArcName);
433  return std::make_pair("", std::error_code());
434 }
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:241
std::error_code getError() const
Definition: ErrorOr.h:169
Represents either an error or a value T.
Definition: ErrorOr.h:68
LLVM_ATTRIBUTE_ALWAYS_INLINE std::time_t toTimeT(TimePoint<> TP)
Convert a TimePoint to std::time_t.
Definition: Chrono.h:36
LLVMContext & Context
std::error_code createUniqueFile(const Twine &Model, int &ResultFD, SmallVectorImpl< char > &ResultPath, unsigned Mode=all_read|all_write)
Create a uniquely named file.
Definition: Path.cpp:762
std::error_code openFileForRead(const Twine &Name, int &ResultFD, SmallVectorImpl< char > *RealPath=nullptr)
static void printMemberHeader(raw_fd_ostream &Out, object::Archive::Kind Kind, bool Thin, StringRef Name, std::vector< unsigned >::iterator &StringMapIndexIter, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, unsigned Size)
Expected< unsigned > getGID() const
Definition: Archive.h:113
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
raw_fd_ostream & os()
Return the contained raw_fd_ostream.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
const_iterator begin(StringRef path)
Get begin iterator over path.
Definition: Path.cpp:233
static ErrorOr< std::unique_ptr< MemoryBuffer > > getOpenFile(int FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Given an already-open file descriptor, read the file and return a MemoryBuffer.
Error takeError()
Take ownership of the stored error.
file_status - Represents the result of a call to stat and friends.
Definition: FileSystem.h:142
static void print32(raw_ostream &Out, object::Archive::Kind Kind, uint32_t Val)
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition: Path.cpp:448
static void printBSDMemberHeader(raw_fd_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, unsigned Size)
std::pair< StringRef, std::error_code > writeArchive(StringRef ArcName, std::vector< NewArchiveMember > &NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic, bool Thin, std::unique_ptr< MemoryBuffer > OldArchiveBuf=nullptr)
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
std::error_code make_error_code(BitcodeError E)
bool is_absolute(const Twine &path)
Is path absolute?
Definition: Path.cpp:686
Tagged union holding either a T or a Error.
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:98
static ErrorOr< unsigned > writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind, ArrayRef< NewArchiveMember > Members, std::vector< unsigned > &MemberOffsetRefs, bool Deterministic)
Function Alias Analysis false
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:124
Expected< sys::fs::perms > getAccessMode() const
Definition: Archive.h:114
StringRef filename(StringRef path)
Get filename.
Definition: Path.cpp:584
static void printRestOfMemberHeader(raw_fd_ostream &Out, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, unsigned Size)
Expected< unsigned > getUID() const
Definition: Archive.h:112
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:48
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
static sys::TimePoint< std::chrono::seconds > now(bool Deterministic)
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
perms permissions() const
Definition: FileSystem.h:212
static void write(bool isBE, void *P, T V)
void consumeError(Error Err)
Consume a Error without doing anything.
raw_ostream & write(unsigned char C)
std::error_code rename(const Twine &from, const Twine &to)
Rename from to to.
StringRef getBuffer() const
Definition: MemoryBuffer.h:169
static bool useStringTable(bool Thin, StringRef Name)
StringRef str()
Return a StringRef for the vector contents.
Definition: raw_ostream.h:515
reference get()
Returns a reference to the stored T value.
static Expected< NewArchiveMember > getOldMember(const object::Archive::Child &OldMember, bool Deterministic)
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:40
Expected< sys::TimePoint< std::chrono::seconds > > getLastModified() const
Definition: Archive.h:106
StringRef parent_path(StringRef path)
Get parent path.
Definition: Path.cpp:493
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:267
basic_symbol_iterator_range symbols() const
Definition: SymbolicFile.h:147
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:27
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:357
void keep()
Indicate that the tool's job wrt this output file has been successful and the file should not be dele...
static void printGNUSmallMemberHeader(raw_fd_ostream &Out, StringRef Name, const sys::TimePoint< std::chrono::seconds > &ModTime, unsigned UID, unsigned GID, unsigned Perms, unsigned Size)
static void writeStringTable(raw_fd_ostream &Out, StringRef ArcName, ArrayRef< NewArchiveMember > Members, std::vector< unsigned > &StringMapIndexes, bool Thin)
#define N
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:86
TimePoint getLastModificationTime() const
static Expected< NewArchiveMember > getFile(StringRef FileName, bool Deterministic)
void close()
Manually flush the stream and close the file.
const unsigned Kind
static std::string computeRelativePath(StringRef From, StringRef To)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void printWithSpacePadding(raw_fd_ostream &OS, T Data, unsigned Size, bool MayTruncate=false)
uint64_t OffsetToAlignment(uint64_t Value, uint64_t Align)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
Definition: MathExtras.h:701
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
std::error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().
std::unique_ptr< MemoryBuffer > Buf
Definition: ArchiveWriter.h:24
file_type type() const
Definition: FileSystem.h:211
static Expected< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context)
sys::TimePoint< std::chrono::seconds > ModTime
Definition: ArchiveWriter.h:25
Expected< MemoryBufferRef > getMemoryBufferRef() const
Definition: Archive.cpp:487
reference get()
Definition: ErrorOr.h:166
std::chrono::time_point< std::chrono::system_clock, D > TimePoint
A time point on the system clock.
Definition: Chrono.h:33