LLVM  3.7.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 
39  : IsNewMember(false), Name(Name), OldI(I) {}
40 
42  : IsNewMember(true), Name(Name), NewFilename(NewFilename) {}
43 
44 StringRef NewArchiveIterator::getName() const { return Name; }
45 
46 bool NewArchiveIterator::isNewMember() const { return IsNewMember; }
47 
49  assert(!IsNewMember);
50  return OldI;
51 }
52 
54  assert(IsNewMember);
55  return NewFilename;
56 }
57 
60  assert(IsNewMember);
61  int NewFD;
62  if (auto EC = sys::fs::openFileForRead(NewFilename, NewFD))
63  return EC;
64  assert(NewFD != -1);
65 
66  if (auto EC = sys::fs::status(NewFD, NewStatus))
67  return EC;
68 
69  // Opening a directory doesn't make sense. Let it fail.
70  // Linux cannot open directories with open(2), although
71  // cygwin and *bsd can.
72  if (NewStatus.type() == sys::fs::file_type::directory_file)
74 
75  return NewFD;
76 }
77 
78 template <typename T>
79 static void printWithSpacePadding(raw_fd_ostream &OS, T Data, unsigned Size,
80  bool MayTruncate = false) {
81  uint64_t OldPos = OS.tell();
82  OS << Data;
83  unsigned SizeSoFar = OS.tell() - OldPos;
84  if (Size > SizeSoFar) {
85  OS.indent(Size - SizeSoFar);
86  } else if (Size < SizeSoFar) {
87  assert(MayTruncate && "Data doesn't fit in Size");
88  // Some of the data this is used for (like UID) can be larger than the
89  // space available in the archive format. Truncate in that case.
90  OS.seek(OldPos + Size);
91  }
92 }
93 
95  uint32_t Val) {
96  if (Kind == object::Archive::K_GNU)
98  else
100 }
101 
103  const sys::TimeValue &ModTime, unsigned UID,
104  unsigned GID, unsigned Perms,
105  unsigned Size) {
106  printWithSpacePadding(Out, ModTime.toEpochTime(), 12);
107  printWithSpacePadding(Out, UID, 6, true);
108  printWithSpacePadding(Out, GID, 6, true);
109  printWithSpacePadding(Out, format("%o", Perms), 8);
110  printWithSpacePadding(Out, Size, 10);
111  Out << "`\n";
112 }
113 
115  const sys::TimeValue &ModTime,
116  unsigned UID, unsigned GID,
117  unsigned Perms, unsigned Size) {
118  printWithSpacePadding(Out, Twine(Name) + "/", 16);
119  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
120 }
121 
123  const sys::TimeValue &ModTime, unsigned UID,
124  unsigned GID, unsigned Perms, unsigned Size) {
125  uint64_t PosAfterHeader = Out.tell() + 60 + Name.size();
126  // Pad so that even 64 bit object files are aligned.
127  unsigned Pad = OffsetToAlignment(PosAfterHeader, 8);
128  unsigned NameWithPadding = Name.size() + Pad;
129  printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
130  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
131  NameWithPadding + Size);
132  Out << Name;
133  assert(PosAfterHeader == Out.tell());
134  while (Pad--)
135  Out.write(uint8_t(0));
136 }
137 
138 static void
140  StringRef Name,
141  std::vector<unsigned>::iterator &StringMapIndexIter,
142  const sys::TimeValue &ModTime, unsigned UID, unsigned GID,
143  unsigned Perms, unsigned Size) {
144  if (Kind == object::Archive::K_BSD)
145  return printBSDMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size);
146  if (Name.size() < 16)
147  return printGNUSmallMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size);
148  Out << '/';
149  printWithSpacePadding(Out, *StringMapIndexIter++, 15);
150  printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
151 }
152 
155  std::vector<unsigned> &StringMapIndexes) {
156  unsigned StartOffset = 0;
158  E = Members.end();
159  I != E; ++I) {
160  StringRef Name = I->getName();
161  if (Name.size() < 16)
162  continue;
163  if (StartOffset == 0) {
164  printWithSpacePadding(Out, "//", 58);
165  Out << "`\n";
166  StartOffset = Out.tell();
167  }
168  StringMapIndexes.push_back(Out.tell() - StartOffset);
169  Out << Name << "/\n";
170  }
171  if (StartOffset == 0)
172  return;
173  if (Out.tell() % 2)
174  Out << '\n';
175  int Pos = Out.tell();
176  Out.seek(StartOffset - 12);
177  printWithSpacePadding(Out, Pos - StartOffset, 10);
178  Out.seek(Pos);
179 }
180 
181 static sys::TimeValue now(bool Deterministic) {
182  if (!Deterministic)
183  return sys::TimeValue::now();
184  sys::TimeValue TV;
185  TV.fromEpochTime(0);
186  return TV;
187 }
188 
189 // Returns the offset of the first reference to a member offset.
190 static ErrorOr<unsigned>
194  std::vector<unsigned> &MemberOffsetRefs, bool Deterministic) {
195  unsigned HeaderStartOffset = 0;
196  unsigned BodyStartOffset = 0;
197  SmallString<128> NameBuf;
198  raw_svector_ostream NameOS(NameBuf);
199  LLVMContext Context;
200  for (unsigned MemberNum = 0, N = Members.size(); MemberNum < N; ++MemberNum) {
201  MemoryBufferRef MemberBuffer = Buffers[MemberNum];
204  MemberBuffer, sys::fs::file_magic::unknown, &Context);
205  if (!ObjOrErr)
206  continue; // FIXME: check only for "not an object file" errors.
207  object::SymbolicFile &Obj = *ObjOrErr.get();
208 
209  if (!HeaderStartOffset) {
210  HeaderStartOffset = Out.tell();
211  if (Kind == object::Archive::K_GNU)
212  printGNUSmallMemberHeader(Out, "", now(Deterministic), 0, 0, 0, 0);
213  else
214  printBSDMemberHeader(Out, "__.SYMDEF", now(Deterministic), 0, 0, 0, 0);
215  BodyStartOffset = Out.tell();
216  print32(Out, Kind, 0); // number of entries or bytes
217  }
218 
219  for (const object::BasicSymbolRef &S : Obj.symbols()) {
220  uint32_t Symflags = S.getFlags();
222  continue;
223  if (!(Symflags & object::SymbolRef::SF_Global))
224  continue;
225  if (Symflags & object::SymbolRef::SF_Undefined)
226  continue;
227 
228  unsigned NameOffset = NameOS.tell();
229  if (auto EC = S.printName(NameOS))
230  return EC;
231  NameOS << '\0';
232  MemberOffsetRefs.push_back(MemberNum);
233  if (Kind == object::Archive::K_BSD)
234  print32(Out, Kind, NameOffset);
235  print32(Out, Kind, 0); // member offset
236  }
237  }
238 
239  if (HeaderStartOffset == 0)
240  return 0;
241 
242  StringRef StringTable = NameOS.str();
243  if (Kind == object::Archive::K_BSD)
244  print32(Out, Kind, StringTable.size()); // byte count of the string table
245  Out << StringTable;
246 
247  // ld64 requires the next member header to start at an offset that is
248  // 4 bytes aligned.
249  unsigned Pad = OffsetToAlignment(Out.tell(), 4);
250  while (Pad--)
251  Out.write(uint8_t(0));
252 
253  // Patch up the size of the symbol table now that we know how big it is.
254  unsigned Pos = Out.tell();
255  const unsigned MemberHeaderSize = 60;
256  Out.seek(HeaderStartOffset + 48); // offset of the size field.
257  printWithSpacePadding(Out, Pos - MemberHeaderSize - HeaderStartOffset, 10);
258 
259  // Patch up the number of symbols.
260  Out.seek(BodyStartOffset);
261  unsigned NumSyms = MemberOffsetRefs.size();
262  if (Kind == object::Archive::K_GNU)
263  print32(Out, Kind, NumSyms);
264  else
265  print32(Out, Kind, NumSyms * 8);
266 
267  Out.seek(Pos);
268  return BodyStartOffset + 4;
269 }
270 
271 std::pair<StringRef, std::error_code> llvm::writeArchive(
272  StringRef ArcName, std::vector<NewArchiveIterator> &NewMembers,
273  bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic) {
274  SmallString<128> TmpArchive;
275  int TmpArchiveFD;
276  if (auto EC = sys::fs::createUniqueFile(ArcName + ".temp-archive-%%%%%%%.a",
277  TmpArchiveFD, TmpArchive))
278  return std::make_pair(ArcName, EC);
279 
280  tool_output_file Output(TmpArchive, TmpArchiveFD);
281  raw_fd_ostream &Out = Output.os();
282  Out << "!<arch>\n";
283 
284  std::vector<unsigned> MemberOffsetRefs;
285 
286  std::vector<std::unique_ptr<MemoryBuffer>> Buffers;
287  std::vector<MemoryBufferRef> Members;
288  std::vector<sys::fs::file_status> NewMemberStatus;
289 
290  for (unsigned I = 0, N = NewMembers.size(); I < N; ++I) {
291  NewArchiveIterator &Member = NewMembers[I];
292  MemoryBufferRef MemberRef;
293 
294  if (Member.isNewMember()) {
295  StringRef Filename = Member.getNew();
296  NewMemberStatus.resize(NewMemberStatus.size() + 1);
297  sys::fs::file_status &Status = NewMemberStatus.back();
298  ErrorOr<int> FD = Member.getFD(Status);
299  if (auto EC = FD.getError())
300  return std::make_pair(Filename, EC);
301  ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
302  MemoryBuffer::getOpenFile(FD.get(), Filename, Status.getSize(),
303  false);
304  if (auto EC = MemberBufferOrErr.getError())
305  return std::make_pair(Filename, EC);
306  if (close(FD.get()) != 0)
307  return std::make_pair(Filename,
308  std::error_code(errno, std::generic_category()));
309  Buffers.push_back(std::move(MemberBufferOrErr.get()));
310  MemberRef = Buffers.back()->getMemBufferRef();
311  } else {
312  object::Archive::child_iterator OldMember = Member.getOld();
313  ErrorOr<MemoryBufferRef> MemberBufferOrErr =
314  OldMember->getMemoryBufferRef();
315  if (auto EC = MemberBufferOrErr.getError())
316  return std::make_pair("", EC);
317  MemberRef = MemberBufferOrErr.get();
318  }
319  Members.push_back(MemberRef);
320  }
321 
322  unsigned MemberReferenceOffset = 0;
323  if (WriteSymtab) {
324  ErrorOr<unsigned> MemberReferenceOffsetOrErr = writeSymbolTable(
325  Out, Kind, NewMembers, Members, MemberOffsetRefs, Deterministic);
326  if (auto EC = MemberReferenceOffsetOrErr.getError())
327  return std::make_pair(ArcName, EC);
328  MemberReferenceOffset = MemberReferenceOffsetOrErr.get();
329  }
330 
331  std::vector<unsigned> StringMapIndexes;
332  if (Kind != object::Archive::K_BSD)
333  writeStringTable(Out, NewMembers, StringMapIndexes);
334 
335  unsigned MemberNum = 0;
336  unsigned NewMemberNum = 0;
337  std::vector<unsigned>::iterator StringMapIndexIter = StringMapIndexes.begin();
338  std::vector<unsigned> MemberOffset;
339  for (const NewArchiveIterator &I : NewMembers) {
340  MemoryBufferRef File = Members[MemberNum++];
341 
342  unsigned Pos = Out.tell();
343  MemberOffset.push_back(Pos);
344 
345  sys::TimeValue ModTime;
346  unsigned UID;
347  unsigned GID;
348  unsigned Perms;
349  if (Deterministic) {
350  ModTime.fromEpochTime(0);
351  UID = 0;
352  GID = 0;
353  Perms = 0644;
354  } else if (I.isNewMember()) {
355  const sys::fs::file_status &Status = NewMemberStatus[NewMemberNum];
356  ModTime = Status.getLastModificationTime();
357  UID = Status.getUser();
358  GID = Status.getGroup();
359  Perms = Status.permissions();
360  } else {
361  object::Archive::child_iterator OldMember = I.getOld();
362  ModTime = OldMember->getLastModified();
363  UID = OldMember->getUID();
364  GID = OldMember->getGID();
365  Perms = OldMember->getAccessMode();
366  }
367 
368  if (I.isNewMember()) {
369  StringRef FileName = I.getNew();
370  const sys::fs::file_status &Status = NewMemberStatus[NewMemberNum++];
371  printMemberHeader(Out, Kind, sys::path::filename(FileName),
372  StringMapIndexIter, ModTime, UID, GID, Perms,
373  Status.getSize());
374  } else {
375  object::Archive::child_iterator OldMember = I.getOld();
376  printMemberHeader(Out, Kind, I.getName(), StringMapIndexIter, ModTime,
377  UID, GID, Perms, OldMember->getSize());
378  }
379 
380  Out << File.getBuffer();
381 
382  if (Out.tell() % 2)
383  Out << '\n';
384  }
385 
386  if (MemberReferenceOffset) {
387  Out.seek(MemberReferenceOffset);
388  for (unsigned MemberNum : MemberOffsetRefs) {
389  if (Kind == object::Archive::K_BSD)
390  Out.seek(Out.tell() + 4); // skip over the string offset
391  print32(Out, Kind, MemberOffset[MemberNum]);
392  }
393  }
394 
395  Output.keep();
396  Out.close();
397  sys::fs::rename(TmpArchive, ArcName);
398  return std::make_pair("", std::error_code());
399 }
std::pair< StringRef, std::error_code > writeArchive(StringRef ArcName, std::vector< NewArchiveIterator > &NewMembers, bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic)
std::error_code getError() const
Definition: ErrorOr.h:178
static ErrorOr< unsigned > writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind, ArrayRef< NewArchiveIterator > Members, ArrayRef< MemoryBufferRef > Buffers, std::vector< unsigned > &MemberOffsetRefs, bool Deterministic)
Represents either an error or a value T.
Definition: ErrorOr.h:82
static TimeValue now()
This is a static constructor that returns a TimeValue that represents the current time...
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
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:681
uint64_t seek(uint64_t off)
Flushes the stream and repositions the underlying file descriptor position to the offset specified fr...
llvm::ErrorOr< int > getFD(sys::fs::file_status &NewStatus) const
iterator end() const
Definition: ArrayRef.h:123
static void printBSDMemberHeader(raw_fd_ostream &Out, StringRef Name, const sys::TimeValue &ModTime, unsigned UID, unsigned GID, unsigned Perms, unsigned Size)
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
sys::fs::perms getAccessMode() const
Definition: Archive.h:89
raw_fd_ostream & os()
Return the contained raw_fd_ostream.
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
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.
static ErrorOr< std::unique_ptr< SymbolicFile > > createSymbolicFile(MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context)
file_status - Represents the result of a call to stat and friends.
Definition: FileSystem.h:138
static void print32(raw_ostream &Out, object::Archive::Kind Kind, uint32_t Val)
static void printMemberHeader(raw_fd_ostream &Out, object::Archive::Kind Kind, StringRef Name, std::vector< unsigned >::iterator &StringMapIndexIter, const sys::TimeValue &ModTime, unsigned UID, unsigned GID, unsigned Perms, unsigned Size)
sys::TimeValue getLastModified() const
Definition: Archive.h:81
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
ErrorOr< MemoryBufferRef > getMemoryBufferRef() const
Definition: Archive.cpp:202
object::Archive::child_iterator getOld() const
std::error_code make_error_code(BitcodeError E)
Definition: ReaderWriter.h:150
NewArchiveIterator(object::Archive::child_iterator I, StringRef Name)
#define false
Definition: ConvertUTF.c:65
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:92
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
unsigned getGID() const
Definition: Archive.h:88
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Definition: Format.h:111
StringRef filename(StringRef path)
Get filename.
Definition: Path.cpp:548
StringRef getName() const
#define true
Definition: ConvertUTF.c:66
uint64_t toEpochTime() const
Converts the TimeValue into the corresponding number of seconds since the epoch (00:00:00 Jan 1...
Definition: TimeValue.h:250
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
iterator begin() const
Definition: ArrayRef.h:122
This class contains a raw_fd_ostream and adds a few extra features commonly needed for compiler-like ...
perms permissions() const
Definition: FileSystem.h:197
unsigned getUID() const
Definition: Archive.h:87
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:157
static void printGNUSmallMemberHeader(raw_fd_ostream &Out, StringRef Name, const sys::TimeValue &ModTime, unsigned UID, unsigned GID, unsigned Perms, unsigned Size)
void fromEpochTime(SecondsType seconds)
Converts the seconds argument from PosixTime to the corresponding TimeValue and assigns that value to...
Definition: TimeValue.h:334
basic_symbol_iterator_range symbols() const
Definition: SymbolicFile.h:143
void write(void *memory, value_type value)
Write a value to memory with a particular endianness.
Definition: Endian.h:73
uint64_t getSize() const
Definition: Archive.cpp:109
StringRef str()
Flushes the stream contents to the target vector and return a StringRef for the vector contents...
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:26
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:345
void keep()
Indicate that the tool's job wrt this output file has been successful and the file should not be dele...
std::error_code openFileForRead(const Twine &Name, int &ResultFD)
#define I(x, y, z)
Definition: MD5.cpp:54
#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:78
StringRef getNew() const
void close()
Manually flush the stream and close the file.
const ARM::ArchExtKind Kind
static void printWithSpacePadding(raw_fd_ostream &OS, T Data, unsigned Size, bool MayTruncate=false)
This class is used where a precise fixed point in time is required.
Definition: TimeValue.h:31
TimeValue getLastModificationTime() const
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:616
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
static void printRestOfMemberHeader(raw_fd_ostream &Out, const sys::TimeValue &ModTime, unsigned UID, unsigned GID, unsigned Perms, unsigned Size)
std::error_code status(const Twine &path, file_status &result)
Get file status as if by POSIX stat().
file_type type() const
Definition: FileSystem.h:196
static sys::TimeValue now(bool Deterministic)
reference get()
Definition: ErrorOr.h:175
static void writeStringTable(raw_fd_ostream &Out, ArrayRef< NewArchiveIterator > Members, std::vector< unsigned > &StringMapIndexes)