2015-06-08 10:32:01 +08: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"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2015-06-08 10:32:01 +08:00
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
2015-06-18 00:02:56 +08:00
|
|
|
#include "llvm/Support/EndianStream.h"
|
2015-06-14 01:23:04 +08:00
|
|
|
#include "llvm/Support/Errc.h"
|
2015-06-08 10:32:01 +08: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 10:43:32 +08:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
2015-06-08 10:32:01 +08:00
|
|
|
#include <unistd.h>
|
2015-06-08 10:43:32 +08:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2015-06-08 10:32:01 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2016-06-30 06:27:42 +08:00
|
|
|
NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef)
|
2017-06-13 03:45:35 +08:00
|
|
|
: Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
|
|
|
|
MemberName(BufRef.getBufferIdentifier()) {}
|
2016-06-30 06:27:42 +08:00
|
|
|
|
|
|
|
Expected<NewArchiveMember>
|
|
|
|
NewArchiveMember::getOldMember(const object::Archive::Child &OldMember,
|
|
|
|
bool Deterministic) {
|
2016-07-30 01:44:13 +08:00
|
|
|
Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef();
|
2016-06-30 06:27:42 +08:00
|
|
|
if (!BufOrErr)
|
2016-07-30 01:44:13 +08:00
|
|
|
return BufOrErr.takeError();
|
2016-06-30 06:27:42 +08:00
|
|
|
|
|
|
|
NewArchiveMember M;
|
2016-12-01 06:32:58 +08:00
|
|
|
assert(M.IsNew == false);
|
2016-06-30 06:27:42 +08:00
|
|
|
M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
|
2017-06-13 03:45:35 +08:00
|
|
|
M.MemberName = M.Buf->getBufferIdentifier();
|
2016-06-30 06:27:42 +08:00
|
|
|
if (!Deterministic) {
|
2016-10-24 21:38:27 +08:00
|
|
|
auto ModTimeOrErr = OldMember.getLastModified();
|
2016-08-04 03:02:50 +08:00
|
|
|
if (!ModTimeOrErr)
|
|
|
|
return ModTimeOrErr.takeError();
|
|
|
|
M.ModTime = ModTimeOrErr.get();
|
|
|
|
Expected<unsigned> UIDOrErr = OldMember.getUID();
|
|
|
|
if (!UIDOrErr)
|
|
|
|
return UIDOrErr.takeError();
|
|
|
|
M.UID = UIDOrErr.get();
|
|
|
|
Expected<unsigned> GIDOrErr = OldMember.getGID();
|
|
|
|
if (!GIDOrErr)
|
|
|
|
return GIDOrErr.takeError();
|
|
|
|
M.GID = GIDOrErr.get();
|
|
|
|
Expected<sys::fs::perms> AccessModeOrErr = OldMember.getAccessMode();
|
|
|
|
if (!AccessModeOrErr)
|
|
|
|
return AccessModeOrErr.takeError();
|
|
|
|
M.Perms = AccessModeOrErr.get();
|
2016-06-30 06:27:42 +08:00
|
|
|
}
|
|
|
|
return std::move(M);
|
2015-06-08 10:32:01 +08:00
|
|
|
}
|
|
|
|
|
2016-06-30 06:27:42 +08:00
|
|
|
Expected<NewArchiveMember> NewArchiveMember::getFile(StringRef FileName,
|
|
|
|
bool Deterministic) {
|
|
|
|
sys::fs::file_status Status;
|
|
|
|
int FD;
|
|
|
|
if (auto EC = sys::fs::openFileForRead(FileName, FD))
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
assert(FD != -1);
|
2015-06-08 10:32:01 +08:00
|
|
|
|
2016-06-30 06:27:42 +08:00
|
|
|
if (auto EC = sys::fs::status(FD, Status))
|
|
|
|
return errorCodeToError(EC);
|
2015-06-08 10:32:01 +08:00
|
|
|
|
|
|
|
// Opening a directory doesn't make sense. Let it fail.
|
|
|
|
// Linux cannot open directories with open(2), although
|
|
|
|
// cygwin and *bsd can.
|
2016-06-30 06:27:42 +08:00
|
|
|
if (Status.type() == sys::fs::file_type::directory_file)
|
|
|
|
return errorCodeToError(make_error_code(errc::is_a_directory));
|
|
|
|
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> MemberBufferOrErr =
|
|
|
|
MemoryBuffer::getOpenFile(FD, FileName, Status.getSize(), false);
|
|
|
|
if (!MemberBufferOrErr)
|
|
|
|
return errorCodeToError(MemberBufferOrErr.getError());
|
|
|
|
|
|
|
|
if (close(FD) != 0)
|
|
|
|
return errorCodeToError(std::error_code(errno, std::generic_category()));
|
|
|
|
|
|
|
|
NewArchiveMember M;
|
2016-12-01 06:32:58 +08:00
|
|
|
M.IsNew = true;
|
2016-06-30 06:27:42 +08:00
|
|
|
M.Buf = std::move(*MemberBufferOrErr);
|
2017-06-13 03:45:35 +08:00
|
|
|
M.MemberName = M.Buf->getBufferIdentifier();
|
2016-06-30 06:27:42 +08:00
|
|
|
if (!Deterministic) {
|
2016-10-24 21:38:27 +08:00
|
|
|
M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
|
|
|
|
Status.getLastModificationTime());
|
2016-06-30 06:27:42 +08:00
|
|
|
M.UID = Status.getUser();
|
|
|
|
M.GID = Status.getGroup();
|
|
|
|
M.Perms = Status.permissions();
|
|
|
|
}
|
|
|
|
return std::move(M);
|
2015-06-08 10:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-09-22 07:06:23 +08:00
|
|
|
static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
|
2015-06-08 10:32:01 +08:00
|
|
|
uint64_t OldPos = OS.tell();
|
|
|
|
OS << Data;
|
|
|
|
unsigned SizeSoFar = OS.tell() - OldPos;
|
2017-09-22 07:00:55 +08:00
|
|
|
assert(SizeSoFar <= Size && "Data doesn't fit in Size");
|
|
|
|
OS.indent(Size - SizeSoFar);
|
2015-06-08 10:32:01 +08:00
|
|
|
}
|
|
|
|
|
2017-02-22 04:40:54 +08:00
|
|
|
static bool isBSDLike(object::Archive::Kind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case object::Archive::K_GNU:
|
|
|
|
return false;
|
|
|
|
case object::Archive::K_BSD:
|
|
|
|
case object::Archive::K_DARWIN:
|
|
|
|
return true;
|
2017-09-21 02:23:01 +08:00
|
|
|
case object::Archive::K_GNU64:
|
2017-02-23 03:42:14 +08:00
|
|
|
case object::Archive::K_DARWIN64:
|
|
|
|
case object::Archive::K_COFF:
|
|
|
|
break;
|
2017-02-22 04:40:54 +08:00
|
|
|
}
|
2017-02-23 03:42:14 +08:00
|
|
|
llvm_unreachable("not supported for writting");
|
2017-02-22 04:40:54 +08:00
|
|
|
}
|
|
|
|
|
2015-07-09 23:13:41 +08:00
|
|
|
static void print32(raw_ostream &Out, object::Archive::Kind Kind,
|
|
|
|
uint32_t Val) {
|
2017-02-22 04:40:54 +08:00
|
|
|
if (isBSDLike(Kind))
|
2015-07-09 23:13:41 +08:00
|
|
|
support::endian::Writer<support::little>(Out).write(Val);
|
2017-02-22 04:40:54 +08:00
|
|
|
else
|
|
|
|
support::endian::Writer<support::big>(Out).write(Val);
|
2015-06-08 10:32:01 +08:00
|
|
|
}
|
|
|
|
|
2016-10-24 21:38:27 +08:00
|
|
|
static void printRestOfMemberHeader(
|
2017-09-22 07:06:23 +08:00
|
|
|
raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime,
|
2016-10-24 21:38:27 +08:00
|
|
|
unsigned UID, unsigned GID, unsigned Perms, unsigned Size) {
|
|
|
|
printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
|
2017-09-22 07:00:55 +08:00
|
|
|
|
|
|
|
// The format has only 6 chars for uid and gid. Truncate if the provided
|
|
|
|
// values don't fit.
|
|
|
|
printWithSpacePadding(Out, UID % 1000000, 6);
|
|
|
|
printWithSpacePadding(Out, GID % 1000000, 6);
|
|
|
|
|
2015-06-08 10:32:01 +08:00
|
|
|
printWithSpacePadding(Out, format("%o", Perms), 8);
|
|
|
|
printWithSpacePadding(Out, Size, 10);
|
|
|
|
Out << "`\n";
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:38:27 +08:00
|
|
|
static void
|
2017-09-22 07:06:23 +08:00
|
|
|
printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name,
|
2016-10-24 21:38:27 +08:00
|
|
|
const sys::TimePoint<std::chrono::seconds> &ModTime,
|
|
|
|
unsigned UID, unsigned GID, unsigned Perms,
|
|
|
|
unsigned Size) {
|
2015-06-08 10:32:01 +08:00
|
|
|
printWithSpacePadding(Out, Twine(Name) + "/", 16);
|
|
|
|
printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:38:27 +08:00
|
|
|
static void
|
2017-09-22 07:06:23 +08:00
|
|
|
printBSDMemberHeader(raw_ostream &Out, StringRef Name,
|
2016-10-24 21:38:27 +08:00
|
|
|
const sys::TimePoint<std::chrono::seconds> &ModTime,
|
|
|
|
unsigned UID, unsigned GID, unsigned Perms,
|
|
|
|
unsigned Size) {
|
2015-07-09 22:54:12 +08:00
|
|
|
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 13:47:46 +08:00
|
|
|
static bool useStringTable(bool Thin, StringRef Name) {
|
2017-06-13 03:45:35 +08:00
|
|
|
return Thin || Name.size() >= 16 || Name.contains('/');
|
2015-07-15 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2015-07-09 01:08:26 +08:00
|
|
|
static void
|
2017-09-22 07:06:23 +08:00
|
|
|
printMemberHeader(raw_ostream &Out, object::Archive::Kind Kind, bool Thin,
|
2015-07-09 04:47:32 +08:00
|
|
|
StringRef Name,
|
2015-07-09 01:08:26 +08:00
|
|
|
std::vector<unsigned>::iterator &StringMapIndexIter,
|
2016-10-24 21:38:27 +08:00
|
|
|
const sys::TimePoint<std::chrono::seconds> &ModTime,
|
|
|
|
unsigned UID, unsigned GID, unsigned Perms, unsigned Size) {
|
2017-02-22 04:40:54 +08:00
|
|
|
if (isBSDLike(Kind))
|
2015-07-09 22:54:12 +08:00
|
|
|
return printBSDMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size);
|
2015-07-15 13:47:46 +08:00
|
|
|
if (!useStringTable(Thin, Name))
|
2015-07-09 22:54:12 +08:00
|
|
|
return printGNUSmallMemberHeader(Out, Name, ModTime, UID, GID, Perms, Size);
|
2015-07-09 01:26:24 +08:00
|
|
|
Out << '/';
|
|
|
|
printWithSpacePadding(Out, *StringMapIndexIter++, 15);
|
|
|
|
printRestOfMemberHeader(Out, ModTime, UID, GID, Perms, Size);
|
2015-07-09 01:08:26 +08:00
|
|
|
}
|
|
|
|
|
2015-07-16 08:14:49 +08: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);
|
|
|
|
|
2016-11-16 05:36:35 +08:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
// Replace backslashes with slashes so that the path is portable between *nix
|
|
|
|
// and Windows.
|
|
|
|
std::replace(Relative.begin(), Relative.end(), '\\', '/');
|
|
|
|
#endif
|
|
|
|
|
2015-07-16 08:14:49 +08:00
|
|
|
return Relative.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeStringTable(raw_fd_ostream &Out, StringRef ArcName,
|
2016-06-30 06:27:42 +08:00
|
|
|
ArrayRef<NewArchiveMember> Members,
|
2015-07-15 13:47:46 +08:00
|
|
|
std::vector<unsigned> &StringMapIndexes,
|
|
|
|
bool Thin) {
|
2015-06-08 10:32:01 +08:00
|
|
|
unsigned StartOffset = 0;
|
2016-06-30 06:27:42 +08:00
|
|
|
for (const NewArchiveMember &M : Members) {
|
|
|
|
StringRef Path = M.Buf->getBufferIdentifier();
|
2017-06-13 03:45:35 +08:00
|
|
|
StringRef Name = M.MemberName;
|
2015-07-15 13:47:46 +08:00
|
|
|
if (!useStringTable(Thin, Name))
|
2015-06-08 10:32:01 +08:00
|
|
|
continue;
|
|
|
|
if (StartOffset == 0) {
|
|
|
|
printWithSpacePadding(Out, "//", 58);
|
|
|
|
Out << "`\n";
|
|
|
|
StartOffset = Out.tell();
|
|
|
|
}
|
|
|
|
StringMapIndexes.push_back(Out.tell() - StartOffset);
|
2015-07-16 08:14:49 +08:00
|
|
|
|
2016-12-01 06:32:58 +08:00
|
|
|
if (Thin) {
|
|
|
|
if (M.IsNew)
|
|
|
|
Out << computeRelativePath(ArcName, Path);
|
|
|
|
else
|
|
|
|
Out << M.Buf->getBufferIdentifier();
|
|
|
|
} else
|
2015-07-16 08:14:49 +08:00
|
|
|
Out << Name;
|
|
|
|
|
|
|
|
Out << "/\n";
|
2015-06-08 10:32:01 +08: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);
|
|
|
|
}
|
|
|
|
|
2016-10-24 21:38:27 +08:00
|
|
|
static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
2015-07-14 04:38:09 +08:00
|
|
|
if (!Deterministic)
|
2016-10-24 21:38:27 +08:00
|
|
|
return time_point_cast<seconds>(system_clock::now());
|
|
|
|
return sys::TimePoint<seconds>();
|
2015-07-14 04:38:09 +08:00
|
|
|
}
|
|
|
|
|
2015-06-08 10:32:01 +08:00
|
|
|
// Returns the offset of the first reference to a member offset.
|
2017-09-22 07:13:36 +08:00
|
|
|
static Expected<unsigned>
|
2015-07-09 04:47:32 +08:00
|
|
|
writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind,
|
2016-06-30 06:27:42 +08:00
|
|
|
ArrayRef<NewArchiveMember> Members,
|
2015-07-14 04:38:09 +08:00
|
|
|
std::vector<unsigned> &MemberOffsetRefs, bool Deterministic) {
|
2015-07-09 23:56:23 +08:00
|
|
|
unsigned HeaderStartOffset = 0;
|
|
|
|
unsigned BodyStartOffset = 0;
|
2015-07-09 05:07:18 +08:00
|
|
|
SmallString<128> NameBuf;
|
|
|
|
raw_svector_ostream NameOS(NameBuf);
|
2015-06-08 10:32:01 +08:00
|
|
|
LLVMContext Context;
|
2015-07-09 05:07:18 +08:00
|
|
|
for (unsigned MemberNum = 0, N = Members.size(); MemberNum < N; ++MemberNum) {
|
2016-06-30 06:27:42 +08:00
|
|
|
MemoryBufferRef MemberBuffer = Members[MemberNum].Buf->getMemBufferRef();
|
2016-04-07 06:14:09 +08:00
|
|
|
Expected<std::unique_ptr<object::SymbolicFile>> ObjOrErr =
|
2015-06-08 10:32:01 +08:00
|
|
|
object::SymbolicFile::createSymbolicFile(
|
2017-06-07 11:48:56 +08:00
|
|
|
MemberBuffer, llvm::file_magic::unknown, &Context);
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!ObjOrErr) {
|
|
|
|
// FIXME: check only for "not an object file" errors.
|
|
|
|
consumeError(ObjOrErr.takeError());
|
|
|
|
continue;
|
|
|
|
}
|
2015-06-08 10:32:01 +08:00
|
|
|
object::SymbolicFile &Obj = *ObjOrErr.get();
|
|
|
|
|
2015-07-09 23:56:23 +08:00
|
|
|
if (!HeaderStartOffset) {
|
|
|
|
HeaderStartOffset = Out.tell();
|
2017-02-22 04:40:54 +08:00
|
|
|
if (isBSDLike(Kind))
|
2015-07-14 04:38:09 +08:00
|
|
|
printBSDMemberHeader(Out, "__.SYMDEF", now(Deterministic), 0, 0, 0, 0);
|
2017-02-22 04:40:54 +08:00
|
|
|
else
|
|
|
|
printGNUSmallMemberHeader(Out, "", now(Deterministic), 0, 0, 0, 0);
|
2015-07-09 23:56:23 +08:00
|
|
|
BodyStartOffset = Out.tell();
|
|
|
|
print32(Out, Kind, 0); // number of entries or bytes
|
2015-06-08 10:32:01 +08: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;
|
2017-07-19 05:26:38 +08:00
|
|
|
if (Symflags & object::SymbolRef::SF_Undefined &&
|
|
|
|
!(Symflags & object::SymbolRef::SF_Indirect))
|
2015-06-08 10:32:01 +08:00
|
|
|
continue;
|
2015-07-09 23:56:23 +08:00
|
|
|
|
|
|
|
unsigned NameOffset = NameOS.tell();
|
2017-09-22 07:13:36 +08:00
|
|
|
if (std::error_code EC = S.printName(NameOS))
|
|
|
|
return errorCodeToError(EC);
|
2015-06-08 10:32:01 +08:00
|
|
|
NameOS << '\0';
|
|
|
|
MemberOffsetRefs.push_back(MemberNum);
|
2017-02-22 04:40:54 +08:00
|
|
|
if (isBSDLike(Kind))
|
2015-07-09 23:56:23 +08:00
|
|
|
print32(Out, Kind, NameOffset);
|
|
|
|
print32(Out, Kind, 0); // member offset
|
2015-06-08 10:32:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-09 23:56:23 +08:00
|
|
|
if (HeaderStartOffset == 0)
|
2015-06-08 10:32:01 +08:00
|
|
|
return 0;
|
|
|
|
|
2017-02-09 23:47:58 +08:00
|
|
|
// ld64 prefers the cctools type archive which pads its string table to a
|
|
|
|
// boundary of sizeof(int32_t).
|
2017-02-22 04:40:54 +08:00
|
|
|
if (isBSDLike(Kind))
|
2017-02-09 12:26:21 +08:00
|
|
|
for (unsigned P = OffsetToAlignment(NameOS.tell(), sizeof(int32_t)); P--;)
|
|
|
|
NameOS << '\0';
|
|
|
|
|
2015-07-09 23:56:23 +08:00
|
|
|
StringRef StringTable = NameOS.str();
|
2017-02-22 04:40:54 +08:00
|
|
|
if (isBSDLike(Kind))
|
2015-07-09 23:56:23 +08:00
|
|
|
print32(Out, Kind, StringTable.size()); // byte count of the string table
|
|
|
|
Out << StringTable;
|
2017-03-15 03:57:13 +08:00
|
|
|
// If there are no symbols, emit an empty symbol table, to satisfy Solaris
|
|
|
|
// tools, older versions of which expect a symbol table in a non-empty
|
|
|
|
// archive, regardless of whether there are any symbols in it.
|
|
|
|
if (StringTable.size() == 0)
|
|
|
|
print32(Out, Kind, 0);
|
2015-07-09 23:56:23 +08:00
|
|
|
|
2017-09-23 02:36:00 +08:00
|
|
|
// ld64 expects the members to be 8-byte aligned for 64-bit content and at
|
|
|
|
// least 4-byte aligned for 32-bit content. Opt for the larger encoding
|
|
|
|
// uniformly.
|
|
|
|
// We do this for all bsd formats because it simplifies aligning members.
|
|
|
|
unsigned Alignment = isBSDLike(Kind) ? 8 : 2;
|
2017-09-23 02:16:13 +08:00
|
|
|
unsigned Pad = OffsetToAlignment(Out.tell(), Alignment);
|
2015-07-10 03:48:06 +08:00
|
|
|
while (Pad--)
|
|
|
|
Out.write(uint8_t(0));
|
2015-06-08 10:32:01 +08:00
|
|
|
|
2015-07-09 23:56:23 +08:00
|
|
|
// Patch up the size of the symbol table now that we know how big it is.
|
2015-06-08 10:32:01 +08:00
|
|
|
unsigned Pos = Out.tell();
|
2015-07-09 23:56:23 +08: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 23:24:39 +08:00
|
|
|
unsigned NumSyms = MemberOffsetRefs.size();
|
2017-02-22 04:40:54 +08:00
|
|
|
if (isBSDLike(Kind))
|
2015-07-09 23:56:23 +08:00
|
|
|
print32(Out, Kind, NumSyms * 8);
|
2017-02-22 04:40:54 +08:00
|
|
|
else
|
|
|
|
print32(Out, Kind, NumSyms);
|
2015-07-09 23:56:23 +08:00
|
|
|
|
2015-06-08 10:32:01 +08:00
|
|
|
Out.seek(Pos);
|
2015-07-09 23:56:23 +08:00
|
|
|
return BodyStartOffset + 4;
|
2015-06-08 10:32:01 +08:00
|
|
|
}
|
|
|
|
|
2017-09-22 07:13:36 +08:00
|
|
|
Error llvm::writeArchive(StringRef ArcName,
|
|
|
|
ArrayRef<NewArchiveMember> NewMembers,
|
|
|
|
bool WriteSymtab, object::Archive::Kind Kind,
|
|
|
|
bool Deterministic, bool Thin,
|
|
|
|
std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
|
2017-02-22 04:40:54 +08:00
|
|
|
assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
|
2015-06-08 10:32:01 +08:00
|
|
|
SmallString<128> TmpArchive;
|
|
|
|
int TmpArchiveFD;
|
|
|
|
if (auto EC = sys::fs::createUniqueFile(ArcName + ".temp-archive-%%%%%%%.a",
|
|
|
|
TmpArchiveFD, TmpArchive))
|
2017-09-22 07:13:36 +08:00
|
|
|
return errorCodeToError(EC);
|
2015-06-08 10:32:01 +08:00
|
|
|
|
|
|
|
tool_output_file Output(TmpArchive, TmpArchiveFD);
|
|
|
|
raw_fd_ostream &Out = Output.os();
|
2015-07-15 13:47:46 +08:00
|
|
|
if (Thin)
|
|
|
|
Out << "!<thin>\n";
|
|
|
|
else
|
|
|
|
Out << "!<arch>\n";
|
2015-06-08 10:32:01 +08:00
|
|
|
|
|
|
|
std::vector<unsigned> MemberOffsetRefs;
|
|
|
|
|
|
|
|
unsigned MemberReferenceOffset = 0;
|
|
|
|
if (WriteSymtab) {
|
2017-09-22 07:13:36 +08:00
|
|
|
Expected<unsigned> MemberReferenceOffsetOrErr = writeSymbolTable(
|
2016-06-30 06:27:42 +08:00
|
|
|
Out, Kind, NewMembers, MemberOffsetRefs, Deterministic);
|
2017-09-22 07:13:36 +08:00
|
|
|
if (auto E = MemberReferenceOffsetOrErr.takeError())
|
|
|
|
return E;
|
2015-06-08 10:32:01 +08:00
|
|
|
MemberReferenceOffset = MemberReferenceOffsetOrErr.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<unsigned> StringMapIndexes;
|
2017-02-22 04:40:54 +08:00
|
|
|
if (!isBSDLike(Kind))
|
2015-07-16 08:14:49 +08:00
|
|
|
writeStringTable(Out, ArcName, NewMembers, StringMapIndexes, Thin);
|
2015-06-08 10:32:01 +08:00
|
|
|
|
2015-07-09 01:08:26 +08:00
|
|
|
std::vector<unsigned>::iterator StringMapIndexIter = StringMapIndexes.begin();
|
2015-06-08 10:32:01 +08:00
|
|
|
std::vector<unsigned> MemberOffset;
|
2016-06-30 06:27:42 +08:00
|
|
|
for (const NewArchiveMember &M : NewMembers) {
|
|
|
|
MemoryBufferRef File = M.Buf->getMemBufferRef();
|
2017-02-10 03:29:35 +08:00
|
|
|
unsigned Padding = 0;
|
2015-06-08 10:32:01 +08:00
|
|
|
|
|
|
|
unsigned Pos = Out.tell();
|
|
|
|
MemberOffset.push_back(Pos);
|
|
|
|
|
2017-02-10 03:29:35 +08:00
|
|
|
// ld64 expects the members to be 8-byte aligned for 64-bit content and at
|
|
|
|
// least 4-byte aligned for 32-bit content. Opt for the larger encoding
|
|
|
|
// uniformly. This matches the behaviour with cctools and ensures that ld64
|
|
|
|
// is happy with archives that we generate.
|
2017-02-22 04:40:54 +08:00
|
|
|
if (Kind == object::Archive::K_DARWIN)
|
2017-02-10 03:29:35 +08:00
|
|
|
Padding = OffsetToAlignment(M.Buf->getBufferSize(), 8);
|
|
|
|
|
2017-06-13 03:45:35 +08:00
|
|
|
printMemberHeader(Out, Kind, Thin, M.MemberName, StringMapIndexIter,
|
|
|
|
M.ModTime, M.UID, M.GID, M.Perms,
|
2017-02-10 03:29:35 +08:00
|
|
|
M.Buf->getBufferSize() + Padding);
|
2015-06-08 10:32:01 +08:00
|
|
|
|
2015-07-15 13:47:46 +08:00
|
|
|
if (!Thin)
|
|
|
|
Out << File.getBuffer();
|
2015-06-08 10:32:01 +08:00
|
|
|
|
2017-02-10 03:29:35 +08:00
|
|
|
while (Padding--)
|
|
|
|
Out << '\n';
|
2015-06-08 10:32:01 +08:00
|
|
|
if (Out.tell() % 2)
|
|
|
|
Out << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MemberReferenceOffset) {
|
|
|
|
Out.seek(MemberReferenceOffset);
|
2015-07-09 23:56:23 +08:00
|
|
|
for (unsigned MemberNum : MemberOffsetRefs) {
|
2017-02-22 04:40:54 +08:00
|
|
|
if (isBSDLike(Kind))
|
2015-07-09 23:56:23 +08:00
|
|
|
Out.seek(Out.tell() + 4); // skip over the string offset
|
2015-07-09 23:13:41 +08:00
|
|
|
print32(Out, Kind, MemberOffset[MemberNum]);
|
2015-07-09 23:56:23 +08:00
|
|
|
}
|
2015-06-08 10:32:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Output.keep();
|
|
|
|
Out.close();
|
2016-05-09 21:31:11 +08:00
|
|
|
|
|
|
|
// At this point, we no longer need whatever backing memory
|
|
|
|
// was used to generate the NewMembers. On Windows, this buffer
|
|
|
|
// could be a mapped view of the file we want to replace (if
|
|
|
|
// we're updating an existing archive, say). In that case, the
|
|
|
|
// rename would still succeed, but it would leave behind a
|
|
|
|
// temporary file (actually the original file renamed) because
|
|
|
|
// a file cannot be deleted while there's a handle open on it,
|
|
|
|
// only renamed. So by freeing this buffer, this ensures that
|
|
|
|
// the last open handle on the destination file, if any, is
|
|
|
|
// closed before we attempt to rename.
|
|
|
|
OldArchiveBuf.reset();
|
|
|
|
|
2015-06-08 10:32:01 +08:00
|
|
|
sys::fs::rename(TmpArchive, ArcName);
|
2017-09-22 07:13:36 +08:00
|
|
|
return Error::success();
|
2015-06-08 10:32:01 +08:00
|
|
|
}
|