2015-06-08 02:32:01 +00:00
|
|
|
//===- ArchiveWriter.cpp - ar File Format implementation --------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the writeArchive function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Object/ArchiveWriter.h"
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
2015-06-17 16:02:56 +00:00
|
|
|
#include "llvm/Support/EndianStream.h"
|
2015-06-13 17:23:04 +00:00
|
|
|
#include "llvm/Support/Errc.h"
|
2015-06-08 02:32:01 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
2015-06-08 02:43:32 +00:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
2015-06-08 02:32:01 +00:00
|
|
|
#include <unistd.h>
|
2015-06-08 02:43:32 +00:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2015-06-08 02:32:01 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-11-01 00:10:37 +00:00
|
|
|
NewArchiveIterator::NewArchiveIterator(const object::Archive::Child &OldMember,
|
2015-06-08 02:32:01 +00:00
|
|
|
StringRef Name)
|
2015-11-01 00:10:37 +00:00
|
|
|
: IsNewMember(false), Name(Name), OldMember(OldMember) {}
|
2015-06-08 02:32:01 +00:00
|
|
|
|
2015-07-15 22:46:53 +00:00
|
|
|
NewArchiveIterator::NewArchiveIterator(StringRef FileName)
|
2015-11-05 19:24:56 +00:00
|
|
|
: IsNewMember(true), Name(FileName), OldMember(nullptr, nullptr, nullptr) {}
|
2015-06-08 02:32:01 +00:00
|
|
|
|
|
|
|
StringRef NewArchiveIterator::getName() const { return Name; }
|
|
|
|
|
|
|
|
bool NewArchiveIterator::isNewMember() const { return IsNewMember; }
|
|
|
|
|
2015-11-01 00:10:37 +00:00
|
|
|
const object::Archive::Child &NewArchiveIterator::getOld() const {
|
2015-06-08 02:32:01 +00:00
|
|
|
assert(!IsNewMember);
|
2015-11-01 00:10:37 +00:00
|
|
|
return OldMember;
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
StringRef NewArchiveIterator::getNew() const {
|
|
|
|
assert(IsNewMember);
|
2015-07-15 22:46:53 +00:00
|
|
|
return Name;
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<int>
|
|
|
|
NewArchiveIterator::getFD(sys::fs::file_status &NewStatus) const {
|
|
|
|
assert(IsNewMember);
|
|
|
|
int NewFD;
|
2015-07-15 22:46:53 +00:00
|
|
|
if (auto EC = sys::fs::openFileForRead(Name, NewFD))
|
2015-06-08 02:32:01 +00:00
|
|
|
return EC;
|
|
|
|
assert(NewFD != -1);
|
|
|
|
|
|
|
|
if (auto EC = sys::fs::status(NewFD, NewStatus))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
// Opening a directory doesn't make sense. Let it fail.
|
|
|
|
// Linux cannot open directories with open(2), although
|
|
|
|
// cygwin and *bsd can.
|
|
|
|
if (NewStatus.type() == sys::fs::file_type::directory_file)
|
2015-06-13 17:23:04 +00:00
|
|
|
return make_error_code(errc::is_a_directory);
|
2015-06-08 02:32:01 +00:00
|
|
|
|
|
|
|
return NewFD;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static void printWithSpacePadding(raw_fd_ostream &OS, T Data, unsigned Size,
|
2015-09-22 11:15:07 +00:00
|
|
|
bool MayTruncate = false) {
|
2015-06-08 02:32:01 +00:00
|
|
|
uint64_t OldPos = OS.tell();
|
|
|
|
OS << Data;
|
|
|
|
unsigned SizeSoFar = OS.tell() - OldPos;
|
|
|
|
if (Size > SizeSoFar) {
|
2015-06-17 16:02:56 +00:00
|
|
|
OS.indent(Size - SizeSoFar);
|
2015-06-08 02:32:01 +00:00
|
|
|
} else if (Size < SizeSoFar) {
|
|
|
|
assert(MayTruncate && "Data doesn't fit in Size");
|
|
|
|
// Some of the data this is used for (like UID) can be larger than the
|
|
|
|
// space available in the archive format. Truncate in that case.
|
|
|
|
OS.seek(OldPos + Size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 15:13:41 +00:00
|
|
|
static void print32(raw_ostream &Out, object::Archive::Kind Kind,
|
|
|
|
uint32_t Val) {
|
|
|
|
if (Kind == object::Archive::K_GNU)
|
|
|
|
support::endian::Writer<support::big>(Out).write(Val);
|
|
|
|
else
|
|
|
|
support::endian::Writer<support::little>(Out).write(Val);
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void printRestOfMemberHeader(raw_fd_ostream &Out,
|
|
|
|
const sys::TimeValue &ModTime, unsigned UID,
|
|
|
|
unsigned GID, unsigned Perms,
|
|
|
|
unsigned Size) {
|
|
|
|
printWithSpacePadding(Out, ModTime.toEpochTime(), 12);
|
|
|
|
printWithSpacePadding(Out, UID, 6, true);
|
|
|
|
printWithSpacePadding(Out, GID, 6, true);
|
|
|
|
printWithSpacePadding(Out, format("%o", Perms), 8);
|
|
|
|
printWithSpacePadding(Out, Size, 10);
|
|
|
|
Out << "`\n";
|
|
|
|
}
|
|
|
|
|
2015-07-09 14:54:12 +00:00
|
|
|
static void printGNUSmallMemberHeader(raw_fd_ostream &Out, StringRef Name,
|
|
|
|
const sys::TimeValue &ModTime,
|
|
|
|
unsigned UID, unsigned GID,
|
|
|
|
unsigned Perms, unsigned Size) {
|
2015-06-08 02:32:01 +00:00
|
|
|
printWithSpacePadding(Out, Twine(Name) + "/", 16);
|
|
|
|
printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
|
|
|
|
}
|
|
|
|
|
2015-07-09 14:54:12 +00:00
|
|
|
static void printBSDMemberHeader(raw_fd_ostream &Out, StringRef Name,
|
|
|
|
const sys::TimeValue &ModTime, unsigned UID,
|
|
|
|
unsigned GID, unsigned Perms, unsigned Size) {
|
|
|
|
uint64_t PosAfterHeader = Out.tell() + 60 + Name.size();
|
|
|
|
// Pad so that even 64 bit object files are aligned.
|
|
|
|
unsigned Pad = OffsetToAlignment(PosAfterHeader, 8);
|
|
|
|
unsigned NameWithPadding = Name.size() + Pad;
|
|
|
|
printWithSpacePadding(Out, Twine("#1/") + Twine(NameWithPadding), 16);
|
|
|
|
printRestOfMemberHeader(Out, ModTime, UID, GID, Perms,
|
|
|
|
NameWithPadding + Size);
|
|
|
|
Out << Name;
|
|
|
|
assert(PosAfterHeader == Out.tell());
|
|
|
|
while (Pad--)
|
|
|
|
Out.write(uint8_t(0));
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:47:46 +00:00
|
|
|
static bool useStringTable(bool Thin, StringRef Name) {
|
|
|
|
return Thin || Name.size() >= 16;
|
|
|
|
}
|
|
|
|
|
2015-07-08 17:08:26 +00:00
|
|
|
static void
|
2015-07-15 05:47:46 +00:00
|
|
|
printMemberHeader(raw_fd_ostream &Out, object::Archive::Kind Kind, bool Thin,
|
2015-07-08 20:47:32 +00:00
|
|
|
StringRef Name,
|
2015-07-08 17:08:26 +00:00
|
|
|
std::vector<unsigned>::iterator &StringMapIndexIter,
|
|
|
|
const sys::TimeValue &ModTime, unsigned UID, unsigned GID,
|
|
|
|
unsigned Perms, unsigned Size) {
|
2015-07-09 14:54:12 +00:00
|
|
|
if (Kind == object::Archive::K_BSD)
|
|
|
|
return printBSDMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size);
|
2015-07-15 05:47:46 +00:00
|
|
|
if (!useStringTable(Thin, Name))
|
2015-07-09 14:54:12 +00:00
|
|
|
return printGNUSmallMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size);
|
2015-07-08 17:26:24 +00:00
|
|
|
Out << '/';
|
|
|
|
printWithSpacePadding(Out, *StringMapIndexIter++, 15);
|
|
|
|
printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
|
2015-07-08 17:08:26 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 00:14:49 +00:00
|
|
|
// Compute the relative path from From to To.
|
|
|
|
static std::string computeRelativePath(StringRef From, StringRef To) {
|
|
|
|
if (sys::path::is_absolute(From) || sys::path::is_absolute(To))
|
|
|
|
return To;
|
|
|
|
|
|
|
|
StringRef DirFrom = sys::path::parent_path(From);
|
|
|
|
auto FromI = sys::path::begin(DirFrom);
|
|
|
|
auto ToI = sys::path::begin(To);
|
|
|
|
while (*FromI == *ToI) {
|
|
|
|
++FromI;
|
|
|
|
++ToI;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallString<128> Relative;
|
|
|
|
for (auto FromE = sys::path::end(DirFrom); FromI != FromE; ++FromI)
|
|
|
|
sys::path::append(Relative, "..");
|
|
|
|
|
|
|
|
for (auto ToE = sys::path::end(To); ToI != ToE; ++ToI)
|
|
|
|
sys::path::append(Relative, *ToI);
|
|
|
|
|
|
|
|
return Relative.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeStringTable(raw_fd_ostream &Out, StringRef ArcName,
|
2015-06-08 02:32:01 +00:00
|
|
|
ArrayRef<NewArchiveIterator> Members,
|
2015-07-15 05:47:46 +00:00
|
|
|
std::vector<unsigned> &StringMapIndexes,
|
|
|
|
bool Thin) {
|
2015-06-08 02:32:01 +00:00
|
|
|
unsigned StartOffset = 0;
|
2015-07-14 23:51:01 +00:00
|
|
|
for (const NewArchiveIterator &I : Members) {
|
2015-07-15 23:15:33 +00:00
|
|
|
StringRef Name = sys::path::filename(I.getName());
|
2015-07-15 05:47:46 +00:00
|
|
|
if (!useStringTable(Thin, Name))
|
2015-06-08 02:32:01 +00:00
|
|
|
continue;
|
|
|
|
if (StartOffset == 0) {
|
|
|
|
printWithSpacePadding(Out, "//", 58);
|
|
|
|
Out << "`\n";
|
|
|
|
StartOffset = Out.tell();
|
|
|
|
}
|
|
|
|
StringMapIndexes.push_back(Out.tell() - StartOffset);
|
2015-07-16 00:14:49 +00:00
|
|
|
|
|
|
|
if (Thin)
|
|
|
|
Out << computeRelativePath(ArcName, I.getName());
|
|
|
|
else
|
|
|
|
Out << Name;
|
|
|
|
|
|
|
|
Out << "/\n";
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
if (StartOffset == 0)
|
|
|
|
return;
|
|
|
|
if (Out.tell() % 2)
|
|
|
|
Out << '\n';
|
|
|
|
int Pos = Out.tell();
|
|
|
|
Out.seek(StartOffset - 12);
|
|
|
|
printWithSpacePadding(Out, Pos - StartOffset, 10);
|
|
|
|
Out.seek(Pos);
|
|
|
|
}
|
|
|
|
|
2015-07-13 20:38:09 +00:00
|
|
|
static sys::TimeValue now(bool Deterministic) {
|
|
|
|
if (!Deterministic)
|
|
|
|
return sys::TimeValue::now();
|
|
|
|
sys::TimeValue TV;
|
|
|
|
TV.fromEpochTime(0);
|
|
|
|
return TV;
|
|
|
|
}
|
|
|
|
|
2015-06-08 02:32:01 +00:00
|
|
|
// Returns the offset of the first reference to a member offset.
|
|
|
|
static ErrorOr<unsigned>
|
2015-07-08 20:47:32 +00:00
|
|
|
writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind,
|
|
|
|
ArrayRef<NewArchiveIterator> Members,
|
2015-06-08 02:32:01 +00:00
|
|
|
ArrayRef<MemoryBufferRef> Buffers,
|
2015-07-13 20:38:09 +00:00
|
|
|
std::vector<unsigned> &MemberOffsetRefs, bool Deterministic) {
|
2015-07-09 15:56:23 +00:00
|
|
|
unsigned HeaderStartOffset = 0;
|
|
|
|
unsigned BodyStartOffset = 0;
|
2015-07-08 21:07:18 +00:00
|
|
|
SmallString<128> NameBuf;
|
|
|
|
raw_svector_ostream NameOS(NameBuf);
|
2015-06-08 02:32:01 +00:00
|
|
|
LLVMContext Context;
|
2015-07-08 21:07:18 +00:00
|
|
|
for (unsigned MemberNum = 0, N = Members.size(); MemberNum < N; ++MemberNum) {
|
2015-06-08 02:32:01 +00:00
|
|
|
MemoryBufferRef MemberBuffer = Buffers[MemberNum];
|
2016-04-06 22:14:09 +00:00
|
|
|
Expected<std::unique_ptr<object::SymbolicFile>> ObjOrErr =
|
2015-06-08 02:32:01 +00:00
|
|
|
object::SymbolicFile::createSymbolicFile(
|
|
|
|
MemberBuffer, sys::fs::file_magic::unknown, &Context);
|
2016-04-06 22:14:09 +00:00
|
|
|
if (!ObjOrErr) {
|
|
|
|
// FIXME: check only for "not an object file" errors.
|
|
|
|
consumeError(ObjOrErr.takeError());
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-08 02:32:01 +00:00
|
|
|
object::SymbolicFile &Obj = *ObjOrErr.get();
|
|
|
|
|
2015-07-09 15:56:23 +00:00
|
|
|
if (!HeaderStartOffset) {
|
|
|
|
HeaderStartOffset = Out.tell();
|
|
|
|
if (Kind == object::Archive::K_GNU)
|
2015-07-13 20:38:09 +00:00
|
|
|
printGNUSmallMemberHeader(Out, "", now(Deterministic), 0, 0, 0, 0);
|
2015-07-09 15:56:23 +00:00
|
|
|
else
|
2015-07-13 20:38:09 +00:00
|
|
|
printBSDMemberHeader(Out, "__.SYMDEF", now(Deterministic), 0, 0, 0, 0);
|
2015-07-09 15:56:23 +00:00
|
|
|
BodyStartOffset = Out.tell();
|
|
|
|
print32(Out, Kind, 0); // number of entries or bytes
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const object::BasicSymbolRef &S : Obj.symbols()) {
|
|
|
|
uint32_t Symflags = S.getFlags();
|
|
|
|
if (Symflags & object::SymbolRef::SF_FormatSpecific)
|
|
|
|
continue;
|
|
|
|
if (!(Symflags & object::SymbolRef::SF_Global))
|
|
|
|
continue;
|
|
|
|
if (Symflags & object::SymbolRef::SF_Undefined)
|
|
|
|
continue;
|
2015-07-09 15:56:23 +00:00
|
|
|
|
|
|
|
unsigned NameOffset = NameOS.tell();
|
2015-06-08 02:32:01 +00:00
|
|
|
if (auto EC = S.printName(NameOS))
|
|
|
|
return EC;
|
|
|
|
NameOS << '\0';
|
|
|
|
MemberOffsetRefs.push_back(MemberNum);
|
2015-07-09 15:56:23 +00:00
|
|
|
if (Kind == object::Archive::K_BSD)
|
|
|
|
print32(Out, Kind, NameOffset);
|
|
|
|
print32(Out, Kind, 0); // member offset
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 15:56:23 +00:00
|
|
|
if (HeaderStartOffset == 0)
|
2015-06-08 02:32:01 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-07-09 15:56:23 +00:00
|
|
|
StringRef StringTable = NameOS.str();
|
|
|
|
if (Kind == object::Archive::K_BSD)
|
|
|
|
print32(Out, Kind, StringTable.size()); // byte count of the string table
|
|
|
|
Out << StringTable;
|
|
|
|
|
2015-07-09 19:48:06 +00:00
|
|
|
// ld64 requires the next member header to start at an offset that is
|
|
|
|
// 4 bytes aligned.
|
|
|
|
unsigned Pad = OffsetToAlignment(Out.tell(), 4);
|
|
|
|
while (Pad--)
|
|
|
|
Out.write(uint8_t(0));
|
2015-06-08 02:32:01 +00:00
|
|
|
|
2015-07-09 15:56:23 +00:00
|
|
|
// Patch up the size of the symbol table now that we know how big it is.
|
2015-06-08 02:32:01 +00:00
|
|
|
unsigned Pos = Out.tell();
|
2015-07-09 15:56:23 +00:00
|
|
|
const unsigned MemberHeaderSize = 60;
|
|
|
|
Out.seek(HeaderStartOffset + 48); // offset of the size field.
|
|
|
|
printWithSpacePadding(Out, Pos - MemberHeaderSize - HeaderStartOffset, 10);
|
|
|
|
|
|
|
|
// Patch up the number of symbols.
|
|
|
|
Out.seek(BodyStartOffset);
|
2015-07-09 15:24:39 +00:00
|
|
|
unsigned NumSyms = MemberOffsetRefs.size();
|
2015-07-09 15:56:23 +00:00
|
|
|
if (Kind == object::Archive::K_GNU)
|
|
|
|
print32(Out, Kind, NumSyms);
|
|
|
|
else
|
|
|
|
print32(Out, Kind, NumSyms * 8);
|
|
|
|
|
2015-06-08 02:32:01 +00:00
|
|
|
Out.seek(Pos);
|
2015-07-09 15:56:23 +00:00
|
|
|
return BodyStartOffset + 4;
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:47:46 +00:00
|
|
|
std::pair<StringRef, std::error_code>
|
|
|
|
llvm::writeArchive(StringRef ArcName,
|
|
|
|
std::vector<NewArchiveIterator> &NewMembers,
|
|
|
|
bool WriteSymtab, object::Archive::Kind Kind,
|
|
|
|
bool Deterministic, bool Thin) {
|
2015-06-08 02:32:01 +00:00
|
|
|
SmallString<128> TmpArchive;
|
|
|
|
int TmpArchiveFD;
|
|
|
|
if (auto EC = sys::fs::createUniqueFile(ArcName + ".temp-archive-%%%%%%%.a",
|
|
|
|
TmpArchiveFD, TmpArchive))
|
|
|
|
return std::make_pair(ArcName, EC);
|
|
|
|
|
|
|
|
tool_output_file Output(TmpArchive, TmpArchiveFD);
|
|
|
|
raw_fd_ostream &Out = Output.os();
|
2015-07-15 05:47:46 +00:00
|
|
|
if (Thin)
|
|
|
|
Out << "!<thin>\n";
|
|
|
|
else
|
|
|
|
Out << "!<arch>\n";
|
2015-06-08 02:32:01 +00:00
|
|
|
|
|
|
|
std::vector<unsigned> MemberOffsetRefs;
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<MemoryBuffer>> Buffers;
|
|
|
|
std::vector<MemoryBufferRef> Members;
|
|
|
|
std::vector<sys::fs::file_status> NewMemberStatus;
|
|
|
|
|
|
|
|
for (unsigned I = 0, N = NewMembers.size(); I < N; ++I) {
|
|
|
|
NewArchiveIterator &Member = NewMembers[I];
|
|
|
|
MemoryBufferRef MemberRef;
|
|
|
|
|
|
|
|
if (Member.isNewMember()) {
|
|
|
|
StringRef Filename = Member.getNew();
|
|
|
|
NewMemberStatus.resize(NewMemberStatus.size() + 1);
|
|
|
|
sys::fs::file_status &Status = NewMemberStatus.back();
|
|
|
|
ErrorOr<int> FD = Member.getFD(Status);
|
|
|
|
if (auto EC = FD.getError())
|
|
|
|
return std::make_pair(Filename, EC);
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
|
|
|
|
MemoryBuffer::getOpenFile(FD.get(), Filename, Status.getSize(),
|
|
|
|
false);
|
|
|
|
if (auto EC = MemberBufferOrErr.getError())
|
|
|
|
return std::make_pair(Filename, EC);
|
|
|
|
if (close(FD.get()) != 0)
|
|
|
|
return std::make_pair(Filename,
|
|
|
|
std::error_code(errno, std::generic_category()));
|
|
|
|
Buffers.push_back(std::move(MemberBufferOrErr.get()));
|
|
|
|
MemberRef = Buffers.back()->getMemBufferRef();
|
|
|
|
} else {
|
2015-11-02 13:17:11 +00:00
|
|
|
const object::Archive::Child &OldMember = Member.getOld();
|
|
|
|
assert((!Thin || OldMember.getParent()->isThin()) &&
|
2015-07-15 22:48:26 +00:00
|
|
|
"Thin archives cannot refers to member of other archives");
|
2015-06-08 02:32:01 +00:00
|
|
|
ErrorOr<MemoryBufferRef> MemberBufferOrErr =
|
2015-11-02 13:17:11 +00:00
|
|
|
OldMember.getMemoryBufferRef();
|
2015-06-08 02:32:01 +00:00
|
|
|
if (auto EC = MemberBufferOrErr.getError())
|
|
|
|
return std::make_pair("", EC);
|
|
|
|
MemberRef = MemberBufferOrErr.get();
|
|
|
|
}
|
|
|
|
Members.push_back(MemberRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned MemberReferenceOffset = 0;
|
|
|
|
if (WriteSymtab) {
|
2015-07-13 20:38:09 +00:00
|
|
|
ErrorOr<unsigned> MemberReferenceOffsetOrErr = writeSymbolTable(
|
|
|
|
Out, Kind, NewMembers, Members, MemberOffsetRefs, Deterministic);
|
2015-06-08 02:32:01 +00:00
|
|
|
if (auto EC = MemberReferenceOffsetOrErr.getError())
|
|
|
|
return std::make_pair(ArcName, EC);
|
|
|
|
MemberReferenceOffset = MemberReferenceOffsetOrErr.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned> StringMapIndexes;
|
2015-07-08 20:47:32 +00:00
|
|
|
if (Kind != object::Archive::K_BSD)
|
2015-07-16 00:14:49 +00:00
|
|
|
writeStringTable(Out, ArcName, NewMembers, StringMapIndexes, Thin);
|
2015-06-08 02:32:01 +00:00
|
|
|
|
|
|
|
unsigned MemberNum = 0;
|
|
|
|
unsigned NewMemberNum = 0;
|
2015-07-08 17:08:26 +00:00
|
|
|
std::vector<unsigned>::iterator StringMapIndexIter = StringMapIndexes.begin();
|
2015-06-08 02:32:01 +00:00
|
|
|
std::vector<unsigned> MemberOffset;
|
2015-07-08 16:16:15 +00:00
|
|
|
for (const NewArchiveIterator &I : NewMembers) {
|
|
|
|
MemoryBufferRef File = Members[MemberNum++];
|
2015-06-08 02:32:01 +00:00
|
|
|
|
|
|
|
unsigned Pos = Out.tell();
|
|
|
|
MemberOffset.push_back(Pos);
|
|
|
|
|
2015-07-13 20:38:09 +00:00
|
|
|
sys::TimeValue ModTime;
|
|
|
|
unsigned UID;
|
|
|
|
unsigned GID;
|
|
|
|
unsigned Perms;
|
|
|
|
if (Deterministic) {
|
|
|
|
ModTime.fromEpochTime(0);
|
|
|
|
UID = 0;
|
|
|
|
GID = 0;
|
|
|
|
Perms = 0644;
|
|
|
|
} else if (I.isNewMember()) {
|
|
|
|
const sys::fs::file_status &Status = NewMemberStatus[NewMemberNum];
|
|
|
|
ModTime = Status.getLastModificationTime();
|
|
|
|
UID = Status.getUser();
|
|
|
|
GID = Status.getGroup();
|
|
|
|
Perms = Status.permissions();
|
|
|
|
} else {
|
2015-11-02 13:17:11 +00:00
|
|
|
const object::Archive::Child &OldMember = I.getOld();
|
|
|
|
ModTime = OldMember.getLastModified();
|
|
|
|
UID = OldMember.getUID();
|
|
|
|
GID = OldMember.getGID();
|
|
|
|
Perms = OldMember.getAccessMode();
|
2015-07-13 20:38:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-08 16:16:15 +00:00
|
|
|
if (I.isNewMember()) {
|
|
|
|
StringRef FileName = I.getNew();
|
2015-07-08 17:08:26 +00:00
|
|
|
const sys::fs::file_status &Status = NewMemberStatus[NewMemberNum++];
|
2015-07-15 05:47:46 +00:00
|
|
|
printMemberHeader(Out, Kind, Thin, sys::path::filename(FileName),
|
2015-07-13 20:38:09 +00:00
|
|
|
StringMapIndexIter, ModTime, UID, GID, Perms,
|
|
|
|
Status.getSize());
|
2015-06-08 02:32:01 +00:00
|
|
|
} else {
|
2015-11-02 13:17:11 +00:00
|
|
|
const object::Archive::Child &OldMember = I.getOld();
|
2015-11-05 19:24:56 +00:00
|
|
|
ErrorOr<uint32_t> Size = OldMember.getSize();
|
|
|
|
if (std::error_code EC = Size.getError())
|
|
|
|
return std::make_pair("", EC);
|
2015-12-18 16:07:17 +00:00
|
|
|
StringRef FileName = I.getName();
|
|
|
|
printMemberHeader(Out, Kind, Thin, sys::path::filename(FileName),
|
|
|
|
StringMapIndexIter, ModTime, UID, GID, Perms,
|
|
|
|
Size.get());
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:47:46 +00:00
|
|
|
if (!Thin)
|
|
|
|
Out << File.getBuffer();
|
2015-06-08 02:32:01 +00:00
|
|
|
|
|
|
|
if (Out.tell() % 2)
|
|
|
|
Out << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MemberReferenceOffset) {
|
|
|
|
Out.seek(MemberReferenceOffset);
|
2015-07-09 15:56:23 +00:00
|
|
|
for (unsigned MemberNum : MemberOffsetRefs) {
|
|
|
|
if (Kind == object::Archive::K_BSD)
|
|
|
|
Out.seek(Out.tell() + 4); // skip over the string offset
|
2015-07-09 15:13:41 +00:00
|
|
|
print32(Out, Kind, MemberOffset[MemberNum]);
|
2015-07-09 15:56:23 +00:00
|
|
|
}
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Output.keep();
|
|
|
|
Out.close();
|
|
|
|
sys::fs::rename(TmpArchive, ArcName);
|
|
|
|
return std::make_pair("", std::error_code());
|
|
|
|
}
|