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"
|
2017-06-07 03:48:56 +00:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2015-06-08 02:32:01 +00:00
|
|
|
#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;
|
|
|
|
|
2017-12-01 00:54:28 +00:00
|
|
|
// The SYM64 format is used when an archive's member offsets are larger than
|
|
|
|
// 32-bits can hold. The need for this shift in format is detected by
|
|
|
|
// writeArchive. To test this we need to generate a file with a member that has
|
|
|
|
// an offset larger than 32-bits but this demands a very slow test. To speed
|
|
|
|
// the test up we use this flag to pretend like the cutoff happens before
|
|
|
|
// 32-bits and instead happens at some much smaller value.
|
|
|
|
static cl::opt<int> Sym64Threshold("sym64-threshold", cl::Hidden,
|
|
|
|
cl::init(32));
|
|
|
|
|
2016-06-29 22:27:42 +00:00
|
|
|
NewArchiveMember::NewArchiveMember(MemoryBufferRef BufRef)
|
2017-06-12 19:45:35 +00:00
|
|
|
: Buf(MemoryBuffer::getMemBuffer(BufRef, false)),
|
|
|
|
MemberName(BufRef.getBufferIdentifier()) {}
|
2016-06-29 22:27:42 +00:00
|
|
|
|
|
|
|
Expected<NewArchiveMember>
|
|
|
|
NewArchiveMember::getOldMember(const object::Archive::Child &OldMember,
|
|
|
|
bool Deterministic) {
|
2016-07-29 17:44:13 +00:00
|
|
|
Expected<llvm::MemoryBufferRef> BufOrErr = OldMember.getMemoryBufferRef();
|
2016-06-29 22:27:42 +00:00
|
|
|
if (!BufOrErr)
|
2016-07-29 17:44:13 +00:00
|
|
|
return BufOrErr.takeError();
|
2016-06-29 22:27:42 +00:00
|
|
|
|
|
|
|
NewArchiveMember M;
|
2016-11-30 22:32:58 +00:00
|
|
|
assert(M.IsNew == false);
|
2016-06-29 22:27:42 +00:00
|
|
|
M.Buf = MemoryBuffer::getMemBuffer(*BufOrErr, false);
|
2017-06-12 19:45:35 +00:00
|
|
|
M.MemberName = M.Buf->getBufferIdentifier();
|
2016-06-29 22:27:42 +00:00
|
|
|
if (!Deterministic) {
|
2016-10-24 13:38:27 +00:00
|
|
|
auto ModTimeOrErr = OldMember.getLastModified();
|
2016-08-03 19:02:50 +00: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-29 22:27:42 +00:00
|
|
|
}
|
|
|
|
return std::move(M);
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 22:27:42 +00: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 02:32:01 +00:00
|
|
|
|
2016-06-29 22:27:42 +00:00
|
|
|
if (auto EC = sys::fs::status(FD, Status))
|
|
|
|
return errorCodeToError(EC);
|
2015-06-08 02:32:01 +00: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-29 22:27:42 +00: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-11-30 22:32:58 +00:00
|
|
|
M.IsNew = true;
|
2016-06-29 22:27:42 +00:00
|
|
|
M.Buf = std::move(*MemberBufferOrErr);
|
2017-06-12 19:45:35 +00:00
|
|
|
M.MemberName = M.Buf->getBufferIdentifier();
|
2016-06-29 22:27:42 +00:00
|
|
|
if (!Deterministic) {
|
2016-10-24 13:38:27 +00:00
|
|
|
M.ModTime = std::chrono::time_point_cast<std::chrono::seconds>(
|
|
|
|
Status.getLastModificationTime());
|
2016-06-29 22:27:42 +00:00
|
|
|
M.UID = Status.getUser();
|
|
|
|
M.GID = Status.getGroup();
|
|
|
|
M.Perms = Status.permissions();
|
|
|
|
}
|
|
|
|
return std::move(M);
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-09-21 23:06:23 +00:00
|
|
|
static void printWithSpacePadding(raw_ostream &OS, T Data, unsigned Size) {
|
2015-06-08 02:32:01 +00:00
|
|
|
uint64_t OldPos = OS.tell();
|
|
|
|
OS << Data;
|
|
|
|
unsigned SizeSoFar = OS.tell() - OldPos;
|
2017-09-21 23:00:55 +00:00
|
|
|
assert(SizeSoFar <= Size && "Data doesn't fit in Size");
|
|
|
|
OS.indent(Size - SizeSoFar);
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-02-21 20:40:54 +00:00
|
|
|
static bool isBSDLike(object::Archive::Kind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case object::Archive::K_GNU:
|
2017-11-03 19:15:06 +00:00
|
|
|
case object::Archive::K_GNU64:
|
2017-02-21 20:40:54 +00:00
|
|
|
return false;
|
|
|
|
case object::Archive::K_BSD:
|
|
|
|
case object::Archive::K_DARWIN:
|
|
|
|
return true;
|
2017-02-22 19:42:14 +00:00
|
|
|
case object::Archive::K_DARWIN64:
|
|
|
|
case object::Archive::K_COFF:
|
|
|
|
break;
|
2017-02-21 20:40:54 +00:00
|
|
|
}
|
2017-02-22 19:42:14 +00:00
|
|
|
llvm_unreachable("not supported for writting");
|
2017-02-21 20:40:54 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 19:15:06 +00:00
|
|
|
template <class T>
|
|
|
|
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val) {
|
2017-02-21 20:40:54 +00:00
|
|
|
if (isBSDLike(Kind))
|
2015-07-09 15:13:41 +00:00
|
|
|
support::endian::Writer<support::little>(Out).write(Val);
|
2017-02-21 20:40:54 +00:00
|
|
|
else
|
|
|
|
support::endian::Writer<support::big>(Out).write(Val);
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 13:38:27 +00:00
|
|
|
static void printRestOfMemberHeader(
|
2017-09-21 23:06:23 +00:00
|
|
|
raw_ostream &Out, const sys::TimePoint<std::chrono::seconds> &ModTime,
|
2016-10-24 13:38:27 +00:00
|
|
|
unsigned UID, unsigned GID, unsigned Perms, unsigned Size) {
|
|
|
|
printWithSpacePadding(Out, sys::toTimeT(ModTime), 12);
|
2017-09-21 23:00:55 +00: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 02:32:01 +00:00
|
|
|
printWithSpacePadding(Out, format("%o", Perms), 8);
|
|
|
|
printWithSpacePadding(Out, Size, 10);
|
|
|
|
Out << "`\n";
|
|
|
|
}
|
|
|
|
|
2016-10-24 13:38:27 +00:00
|
|
|
static void
|
2017-09-21 23:06:23 +00:00
|
|
|
printGNUSmallMemberHeader(raw_ostream &Out, StringRef Name,
|
2016-10-24 13:38:27 +00:00
|
|
|
const sys::TimePoint<std::chrono::seconds> &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);
|
|
|
|
}
|
|
|
|
|
2016-10-24 13:38:27 +00:00
|
|
|
static void
|
2017-10-03 20:59:43 +00:00
|
|
|
printBSDMemberHeader(raw_ostream &Out, uint64_t Pos, StringRef Name,
|
2016-10-24 13:38:27 +00:00
|
|
|
const sys::TimePoint<std::chrono::seconds> &ModTime,
|
|
|
|
unsigned UID, unsigned GID, unsigned Perms,
|
|
|
|
unsigned Size) {
|
2017-10-03 20:59:43 +00:00
|
|
|
uint64_t PosAfterHeader = Pos + 60 + Name.size();
|
2015-07-09 14:54:12 +00:00
|
|
|
// 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;
|
|
|
|
while (Pad--)
|
|
|
|
Out.write(uint8_t(0));
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:47:46 +00:00
|
|
|
static bool useStringTable(bool Thin, StringRef Name) {
|
2017-06-12 19:45:35 +00:00
|
|
|
return Thin || Name.size() >= 16 || Name.contains('/');
|
2015-07-15 05:47:46 +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);
|
|
|
|
|
2016-11-15 21:36:35 +00: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 00:14:49 +00:00
|
|
|
return Relative.str();
|
|
|
|
}
|
|
|
|
|
2017-11-03 19:15:06 +00:00
|
|
|
static bool is64BitKind(object::Archive::Kind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case object::Archive::K_GNU:
|
|
|
|
case object::Archive::K_BSD:
|
|
|
|
case object::Archive::K_DARWIN:
|
|
|
|
case object::Archive::K_COFF:
|
|
|
|
return false;
|
|
|
|
case object::Archive::K_DARWIN64:
|
|
|
|
case object::Archive::K_GNU64:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
llvm_unreachable("not supported for writting");
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
static void addToStringTable(raw_ostream &Out, StringRef ArcName,
|
|
|
|
const NewArchiveMember &M, bool Thin) {
|
|
|
|
StringRef ID = M.Buf->getBufferIdentifier();
|
|
|
|
if (Thin) {
|
|
|
|
if (M.IsNew)
|
|
|
|
Out << computeRelativePath(ArcName, ID);
|
|
|
|
else
|
|
|
|
Out << ID;
|
|
|
|
} else
|
|
|
|
Out << M.MemberName;
|
|
|
|
Out << "/\n";
|
|
|
|
}
|
2015-07-16 00:14:49 +00:00
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
static void printMemberHeader(raw_ostream &Out, uint64_t Pos,
|
|
|
|
raw_ostream &StringTable,
|
|
|
|
object::Archive::Kind Kind, bool Thin,
|
|
|
|
StringRef ArcName, const NewArchiveMember &M,
|
|
|
|
unsigned Size) {
|
|
|
|
if (isBSDLike(Kind))
|
|
|
|
return printBSDMemberHeader(Out, Pos, M.MemberName, M.ModTime, M.UID, M.GID,
|
|
|
|
M.Perms, Size);
|
|
|
|
if (!useStringTable(Thin, M.MemberName))
|
|
|
|
return printGNUSmallMemberHeader(Out, M.MemberName, M.ModTime, M.UID, M.GID,
|
|
|
|
M.Perms, Size);
|
|
|
|
Out << '/';
|
|
|
|
uint64_t NamePos = StringTable.tell();
|
|
|
|
addToStringTable(StringTable, ArcName, M, Thin);
|
|
|
|
printWithSpacePadding(Out, NamePos, 15);
|
|
|
|
printRestOfMemberHeader(Out, M.ModTime, M.UID, M.GID, M.Perms, Size);
|
|
|
|
}
|
2015-07-16 00:14:49 +00:00
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
namespace {
|
|
|
|
struct MemberData {
|
|
|
|
std::vector<unsigned> Symbols;
|
|
|
|
std::string Header;
|
|
|
|
StringRef Data;
|
|
|
|
StringRef Padding;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static MemberData computeStringTable(StringRef Names) {
|
|
|
|
unsigned Size = Names.size();
|
|
|
|
unsigned Pad = OffsetToAlignment(Size, 2);
|
|
|
|
std::string Header;
|
|
|
|
raw_string_ostream Out(Header);
|
|
|
|
printWithSpacePadding(Out, "//", 48);
|
|
|
|
printWithSpacePadding(Out, Size + Pad, 10);
|
|
|
|
Out << "`\n";
|
|
|
|
Out.flush();
|
|
|
|
return {{}, std::move(Header), Names, Pad ? "\n" : ""};
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 13:38:27 +00:00
|
|
|
static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
|
|
|
|
using namespace std::chrono;
|
|
|
|
|
2015-07-13 20:38:09 +00:00
|
|
|
if (!Deterministic)
|
2016-10-24 13:38:27 +00:00
|
|
|
return time_point_cast<seconds>(system_clock::now());
|
|
|
|
return sys::TimePoint<seconds>();
|
2015-07-13 20:38:09 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 18:40:14 +00:00
|
|
|
static bool isArchiveSymbol(const object::BasicSymbolRef &S) {
|
|
|
|
uint32_t Symflags = S.getFlags();
|
|
|
|
if (Symflags & object::SymbolRef::SF_FormatSpecific)
|
|
|
|
return false;
|
|
|
|
if (!(Symflags & object::SymbolRef::SF_Global))
|
|
|
|
return false;
|
|
|
|
if (Symflags & object::SymbolRef::SF_Undefined &&
|
|
|
|
!(Symflags & object::SymbolRef::SF_Indirect))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-03 19:15:06 +00:00
|
|
|
static void printNBits(raw_ostream &Out, object::Archive::Kind Kind,
|
|
|
|
uint64_t Val) {
|
|
|
|
if (is64BitKind(Kind))
|
|
|
|
print<uint64_t>(Out, Kind, Val);
|
|
|
|
else
|
|
|
|
print<uint32_t>(Out, Kind, Val);
|
|
|
|
}
|
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
static void writeSymbolTable(raw_ostream &Out, object::Archive::Kind Kind,
|
|
|
|
bool Deterministic, ArrayRef<MemberData> Members,
|
|
|
|
StringRef StringTable) {
|
|
|
|
if (StringTable.empty())
|
|
|
|
return;
|
2015-06-08 02:32:01 +00:00
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
unsigned NumSyms = 0;
|
|
|
|
for (const MemberData &M : Members)
|
|
|
|
NumSyms += M.Symbols.size();
|
2015-06-08 02:32:01 +00:00
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
unsigned Size = 0;
|
2017-11-03 19:15:06 +00:00
|
|
|
Size += is64BitKind(Kind) ? 8 : 4; // Number of entries
|
2017-02-21 20:40:54 +00:00
|
|
|
if (isBSDLike(Kind))
|
2017-10-03 20:59:43 +00:00
|
|
|
Size += NumSyms * 8; // Table
|
2017-11-03 19:15:06 +00:00
|
|
|
else if (is64BitKind(Kind))
|
|
|
|
Size += NumSyms * 8; // Table
|
2017-10-03 20:59:43 +00:00
|
|
|
else
|
|
|
|
Size += NumSyms * 4; // Table
|
2017-02-21 20:40:54 +00:00
|
|
|
if (isBSDLike(Kind))
|
2017-10-03 20:59:43 +00:00
|
|
|
Size += 4; // byte count
|
|
|
|
Size += StringTable.size();
|
2017-09-22 18:36:00 +00: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-10-03 20:59:43 +00:00
|
|
|
unsigned Pad = OffsetToAlignment(Size, Alignment);
|
|
|
|
Size += Pad;
|
|
|
|
|
|
|
|
if (isBSDLike(Kind))
|
|
|
|
printBSDMemberHeader(Out, Out.tell(), "__.SYMDEF", now(Deterministic), 0, 0,
|
|
|
|
0, Size);
|
2017-11-03 19:15:06 +00:00
|
|
|
else if (is64BitKind(Kind))
|
|
|
|
printGNUSmallMemberHeader(Out, "/SYM64", now(Deterministic), 0, 0, 0, Size);
|
2017-10-03 20:59:43 +00:00
|
|
|
else
|
|
|
|
printGNUSmallMemberHeader(Out, "", now(Deterministic), 0, 0, 0, Size);
|
2015-06-08 02:32:01 +00:00
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
uint64_t Pos = Out.tell() + Size;
|
2015-07-09 15:56:23 +00:00
|
|
|
|
2017-02-21 20:40:54 +00:00
|
|
|
if (isBSDLike(Kind))
|
2017-11-03 19:15:06 +00:00
|
|
|
print<uint32_t>(Out, Kind, NumSyms * 8);
|
2017-02-21 20:40:54 +00:00
|
|
|
else
|
2017-11-03 19:15:06 +00:00
|
|
|
printNBits(Out, Kind, NumSyms);
|
2015-07-09 15:56:23 +00:00
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
for (const MemberData &M : Members) {
|
|
|
|
for (unsigned StringOffset : M.Symbols) {
|
|
|
|
if (isBSDLike(Kind))
|
2017-11-03 19:15:06 +00:00
|
|
|
print<uint32_t>(Out, Kind, StringOffset);
|
|
|
|
printNBits(Out, Kind, Pos); // member offset
|
2017-10-03 20:59:43 +00:00
|
|
|
}
|
|
|
|
Pos += M.Header.size() + M.Data.size() + M.Padding.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isBSDLike(Kind))
|
2017-11-03 19:15:06 +00:00
|
|
|
// byte count of the string table
|
|
|
|
print<uint32_t>(Out, Kind, StringTable.size());
|
2017-10-03 20:59:43 +00:00
|
|
|
Out << StringTable;
|
|
|
|
|
|
|
|
while (Pad--)
|
|
|
|
Out.write(uint8_t(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expected<std::vector<unsigned>>
|
|
|
|
getSymbols(MemoryBufferRef Buf, raw_ostream &SymNames, bool &HasObject) {
|
|
|
|
std::vector<unsigned> Ret;
|
|
|
|
LLVMContext Context;
|
|
|
|
|
|
|
|
Expected<std::unique_ptr<object::SymbolicFile>> ObjOrErr =
|
|
|
|
object::SymbolicFile::createSymbolicFile(Buf, llvm::file_magic::unknown,
|
|
|
|
&Context);
|
|
|
|
if (!ObjOrErr) {
|
|
|
|
// FIXME: check only for "not an object file" errors.
|
|
|
|
consumeError(ObjOrErr.takeError());
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
HasObject = true;
|
|
|
|
object::SymbolicFile &Obj = *ObjOrErr.get();
|
|
|
|
for (const object::BasicSymbolRef &S : Obj.symbols()) {
|
|
|
|
if (!isArchiveSymbol(S))
|
|
|
|
continue;
|
|
|
|
Ret.push_back(SymNames.tell());
|
|
|
|
if (auto EC = S.printName(SymNames))
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
SymNames << '\0';
|
|
|
|
}
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Expected<std::vector<MemberData>>
|
|
|
|
computeMemberData(raw_ostream &StringTable, raw_ostream &SymNames,
|
|
|
|
object::Archive::Kind Kind, bool Thin, StringRef ArcName,
|
|
|
|
ArrayRef<NewArchiveMember> NewMembers) {
|
|
|
|
static char PaddingData[8] = {'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'};
|
|
|
|
|
|
|
|
// This ignores the symbol table, but we only need the value mod 8 and the
|
|
|
|
// symbol table is aligned to be a multiple of 8 bytes
|
|
|
|
uint64_t Pos = 0;
|
|
|
|
|
|
|
|
std::vector<MemberData> Ret;
|
|
|
|
bool HasObject = false;
|
|
|
|
for (const NewArchiveMember &M : NewMembers) {
|
|
|
|
std::string Header;
|
|
|
|
raw_string_ostream Out(Header);
|
|
|
|
|
|
|
|
MemoryBufferRef Buf = M.Buf->getMemBufferRef();
|
|
|
|
StringRef Data = Thin ? "" : Buf.getBuffer();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
unsigned MemberPadding = Kind == object::Archive::K_DARWIN
|
|
|
|
? OffsetToAlignment(Data.size(), 8)
|
|
|
|
: 0;
|
|
|
|
unsigned TailPadding = OffsetToAlignment(Data.size() + MemberPadding, 2);
|
|
|
|
StringRef Padding = StringRef(PaddingData, MemberPadding + TailPadding);
|
|
|
|
|
|
|
|
printMemberHeader(Out, Pos, StringTable, Kind, Thin, ArcName, M,
|
|
|
|
Buf.getBufferSize() + MemberPadding);
|
|
|
|
Out.flush();
|
|
|
|
|
|
|
|
Expected<std::vector<unsigned>> Symbols =
|
|
|
|
getSymbols(Buf, SymNames, HasObject);
|
|
|
|
if (auto E = Symbols.takeError())
|
|
|
|
return std::move(E);
|
|
|
|
|
|
|
|
Pos += Header.size() + Data.size() + Padding.size();
|
|
|
|
Ret.push_back({std::move(*Symbols), std::move(Header), Data, Padding});
|
|
|
|
}
|
|
|
|
// 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 (HasObject && SymNames.tell() == 0)
|
|
|
|
SymNames << '\0' << '\0' << '\0';
|
|
|
|
return Ret;
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 23:13:36 +00: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-21 20:40:54 +00:00
|
|
|
assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
|
2017-10-03 20:59:43 +00:00
|
|
|
|
|
|
|
SmallString<0> SymNamesBuf;
|
|
|
|
raw_svector_ostream SymNames(SymNamesBuf);
|
|
|
|
SmallString<0> StringTableBuf;
|
|
|
|
raw_svector_ostream StringTable(StringTableBuf);
|
|
|
|
|
|
|
|
Expected<std::vector<MemberData>> DataOrErr =
|
|
|
|
computeMemberData(StringTable, SymNames, Kind, Thin, ArcName, NewMembers);
|
|
|
|
if (Error E = DataOrErr.takeError())
|
|
|
|
return E;
|
|
|
|
std::vector<MemberData> &Data = *DataOrErr;
|
|
|
|
|
|
|
|
if (!StringTableBuf.empty())
|
|
|
|
Data.insert(Data.begin(), computeStringTable(StringTableBuf));
|
|
|
|
|
2017-11-03 19:15:06 +00:00
|
|
|
// We would like to detect if we need to switch to a 64-bit symbol table.
|
|
|
|
if (WriteSymtab) {
|
|
|
|
uint64_t MaxOffset = 0;
|
|
|
|
uint64_t LastOffset = MaxOffset;
|
|
|
|
for (const auto& M : Data) {
|
|
|
|
// Record the start of the member's offset
|
|
|
|
LastOffset = MaxOffset;
|
|
|
|
// Account for the size of each part associated with the member.
|
|
|
|
MaxOffset += M.Header.size() + M.Data.size() + M.Padding.size();
|
|
|
|
// We assume 32-bit symbols to see if 32-bit symbols are possible or not.
|
|
|
|
MaxOffset += M.Symbols.size() * 4;
|
|
|
|
}
|
|
|
|
// If LastOffset isn't going to fit in a 32-bit varible we need to switch
|
|
|
|
// to 64-bit. Note that the file can be larger than 4GB as long as the last
|
|
|
|
// member starts before the 4GB offset.
|
2017-12-01 00:54:28 +00:00
|
|
|
if (LastOffset >= (1ULL << Sym64Threshold))
|
2017-11-03 19:15:06 +00:00
|
|
|
Kind = object::Archive::K_GNU64;
|
|
|
|
}
|
|
|
|
|
2017-11-14 01:21:15 +00:00
|
|
|
Expected<sys::fs::TempFile> Temp =
|
|
|
|
sys::fs::TempFile::create(ArcName + ".temp-archive-%%%%%%%.a");
|
|
|
|
if (!Temp)
|
|
|
|
return Temp.takeError();
|
2015-06-08 02:32:01 +00:00
|
|
|
|
2017-11-14 01:21:15 +00:00
|
|
|
raw_fd_ostream Out(Temp->FD, false);
|
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
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
if (WriteSymtab)
|
|
|
|
writeSymbolTable(Out, Kind, Deterministic, Data, SymNamesBuf);
|
2015-06-08 02:32:01 +00:00
|
|
|
|
2017-10-03 20:59:43 +00:00
|
|
|
for (const MemberData &M : Data)
|
|
|
|
Out << M.Header << M.Data << M.Padding;
|
2015-06-08 02:32:01 +00:00
|
|
|
|
2017-11-14 01:21:15 +00:00
|
|
|
Out.flush();
|
2016-05-09 13:31:11 +00: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();
|
|
|
|
|
2017-11-14 01:21:15 +00:00
|
|
|
return Temp->keep(ArcName);
|
2015-06-08 02:32:01 +00:00
|
|
|
}
|