2015-01-06 05:29:28 +08:00
|
|
|
//===-- BinaryHolder.cpp --------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This program is a utility that aims to be a dropin replacement for
|
|
|
|
// Darwin's dsymutil.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "BinaryHolder.h"
|
2015-07-24 14:41:04 +08:00
|
|
|
#include "llvm/Object/MachO.h"
|
2015-01-06 05:29:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace dsymutil {
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
static std::vector<MemoryBufferRef>
|
|
|
|
getMachOFatMemoryBuffers(StringRef Filename, MemoryBuffer &Mem,
|
|
|
|
object::MachOUniversalBinary &Fat) {
|
|
|
|
std::vector<MemoryBufferRef> Buffers;
|
|
|
|
StringRef FatData = Fat.getData();
|
|
|
|
for (auto It = Fat.begin_objects(), End = Fat.end_objects(); It != End;
|
|
|
|
++It) {
|
|
|
|
StringRef ObjData = FatData.substr(It->getOffset(), It->getSize());
|
|
|
|
Buffers.emplace_back(ObjData, Filename);
|
|
|
|
}
|
|
|
|
return Buffers;
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:40:59 +08:00
|
|
|
void BinaryHolder::changeBackingMemoryBuffer(
|
|
|
|
std::unique_ptr<MemoryBuffer> &&Buf) {
|
2015-07-24 14:41:11 +08:00
|
|
|
CurrentArchives.clear();
|
|
|
|
CurrentObjectFiles.clear();
|
|
|
|
CurrentFatBinary.reset();
|
2015-07-24 14:40:59 +08:00
|
|
|
|
|
|
|
CurrentMemoryBuffer = std::move(Buf);
|
|
|
|
}
|
|
|
|
|
2016-11-09 19:43:52 +08:00
|
|
|
ErrorOr<std::vector<MemoryBufferRef>> BinaryHolder::GetMemoryBuffersForFile(
|
|
|
|
StringRef Filename, sys::TimePoint<std::chrono::seconds> Timestamp) {
|
2015-01-06 05:29:28 +08:00
|
|
|
if (Verbose)
|
|
|
|
outs() << "trying to open '" << Filename << "'\n";
|
|
|
|
|
|
|
|
// Try that first as it doesn't involve any filesystem access.
|
2015-07-24 14:41:11 +08:00
|
|
|
if (auto ErrOrArchiveMembers = GetArchiveMemberBuffers(Filename, Timestamp))
|
|
|
|
return *ErrOrArchiveMembers;
|
2015-01-06 05:29:28 +08:00
|
|
|
|
|
|
|
// If the name ends with a closing paren, there is a huge chance
|
|
|
|
// it is an archive member specification.
|
|
|
|
if (Filename.endswith(")"))
|
2015-07-24 14:41:11 +08:00
|
|
|
if (auto ErrOrArchiveMembers =
|
|
|
|
MapArchiveAndGetMemberBuffers(Filename, Timestamp))
|
|
|
|
return *ErrOrArchiveMembers;
|
2015-01-06 05:29:28 +08:00
|
|
|
|
|
|
|
// Otherwise, just try opening a standard file. If this is an
|
|
|
|
// archive member specifiaction and any of the above didn't handle it
|
|
|
|
// (either because the archive is not there anymore, or because the
|
|
|
|
// archive doesn't contain the requested member), this will still
|
|
|
|
// provide a sensible error message.
|
|
|
|
auto ErrOrFile = MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
if (auto Err = ErrOrFile.getError())
|
|
|
|
return Err;
|
|
|
|
|
2015-07-24 14:40:59 +08:00
|
|
|
changeBackingMemoryBuffer(std::move(*ErrOrFile));
|
2015-01-06 05:29:28 +08:00
|
|
|
if (Verbose)
|
|
|
|
outs() << "\tloaded file.\n";
|
2015-07-24 14:41:11 +08:00
|
|
|
|
|
|
|
auto ErrOrFat = object::MachOUniversalBinary::create(
|
|
|
|
CurrentMemoryBuffer->getMemBufferRef());
|
2016-06-28 05:39:39 +08:00
|
|
|
if (!ErrOrFat) {
|
|
|
|
consumeError(ErrOrFat.takeError());
|
2015-07-24 14:41:11 +08:00
|
|
|
// Not a fat binary must be a standard one. Return a one element vector.
|
|
|
|
return std::vector<MemoryBufferRef>{CurrentMemoryBuffer->getMemBufferRef()};
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentFatBinary = std::move(*ErrOrFat);
|
2016-05-09 22:44:14 +08:00
|
|
|
CurrentFatBinaryName = Filename;
|
|
|
|
return getMachOFatMemoryBuffers(CurrentFatBinaryName, *CurrentMemoryBuffer,
|
2015-07-24 14:41:11 +08:00
|
|
|
*CurrentFatBinary);
|
2015-01-06 05:29:28 +08:00
|
|
|
}
|
|
|
|
|
2016-11-09 19:43:52 +08:00
|
|
|
ErrorOr<std::vector<MemoryBufferRef>> BinaryHolder::GetArchiveMemberBuffers(
|
|
|
|
StringRef Filename, sys::TimePoint<std::chrono::seconds> Timestamp) {
|
2015-07-24 14:41:11 +08:00
|
|
|
if (CurrentArchives.empty())
|
2015-01-06 05:29:28 +08:00
|
|
|
return make_error_code(errc::no_such_file_or_directory);
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
StringRef CurArchiveName = CurrentArchives.front()->getFileName();
|
2015-01-06 05:29:28 +08:00
|
|
|
if (!Filename.startswith(Twine(CurArchiveName, "(").str()))
|
|
|
|
return make_error_code(errc::no_such_file_or_directory);
|
|
|
|
|
|
|
|
// Remove the archive name and the parens around the archive member name.
|
|
|
|
Filename = Filename.substr(CurArchiveName.size() + 1).drop_back();
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
std::vector<MemoryBufferRef> Buffers;
|
|
|
|
Buffers.reserve(CurrentArchives.size());
|
|
|
|
|
|
|
|
for (const auto &CurrentArchive : CurrentArchives) {
|
2016-11-11 12:28:40 +08:00
|
|
|
Error Err = Error::success();
|
2016-07-14 10:24:01 +08:00
|
|
|
for (auto Child : CurrentArchive->children(Err)) {
|
2015-07-24 14:41:11 +08:00
|
|
|
if (auto NameOrErr = Child.getName()) {
|
|
|
|
if (*NameOrErr == Filename) {
|
2016-11-09 19:43:52 +08:00
|
|
|
auto ModTimeOrErr = Child.getLastModified();
|
2016-08-04 03:02:50 +08:00
|
|
|
if (!ModTimeOrErr)
|
|
|
|
return errorToErrorCode(ModTimeOrErr.takeError());
|
2016-11-09 19:43:52 +08:00
|
|
|
if (Timestamp != sys::TimePoint<>() &&
|
2016-08-04 03:02:50 +08:00
|
|
|
Timestamp != ModTimeOrErr.get()) {
|
2015-07-24 14:41:11 +08:00
|
|
|
if (Verbose)
|
|
|
|
outs() << "\tmember had timestamp mismatch.\n";
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-23 07:24:00 +08:00
|
|
|
if (Verbose)
|
2015-07-24 14:41:11 +08:00
|
|
|
outs() << "\tfound member in current archive.\n";
|
|
|
|
auto ErrOrMem = Child.getMemoryBufferRef();
|
2016-07-30 01:44:13 +08:00
|
|
|
if (!ErrOrMem)
|
|
|
|
return errorToErrorCode(ErrOrMem.takeError());
|
2015-07-24 14:41:11 +08:00
|
|
|
Buffers.push_back(*ErrOrMem);
|
2015-07-23 07:24:00 +08:00
|
|
|
}
|
2015-01-06 05:29:28 +08:00
|
|
|
}
|
2015-07-24 14:41:11 +08:00
|
|
|
}
|
2016-07-14 10:24:01 +08:00
|
|
|
if (Err)
|
|
|
|
return errorToErrorCode(std::move(Err));
|
2015-01-06 05:29:28 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
if (Buffers.empty())
|
|
|
|
return make_error_code(errc::no_such_file_or_directory);
|
|
|
|
return Buffers;
|
2015-01-06 05:29:28 +08:00
|
|
|
}
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
ErrorOr<std::vector<MemoryBufferRef>>
|
2016-11-09 19:43:52 +08:00
|
|
|
BinaryHolder::MapArchiveAndGetMemberBuffers(
|
|
|
|
StringRef Filename, sys::TimePoint<std::chrono::seconds> Timestamp) {
|
2015-01-06 05:29:28 +08:00
|
|
|
StringRef ArchiveFilename = Filename.substr(0, Filename.find('('));
|
|
|
|
|
|
|
|
auto ErrOrBuff = MemoryBuffer::getFileOrSTDIN(ArchiveFilename);
|
|
|
|
if (auto Err = ErrOrBuff.getError())
|
|
|
|
return Err;
|
|
|
|
|
|
|
|
if (Verbose)
|
|
|
|
outs() << "\topened new archive '" << ArchiveFilename << "'\n";
|
2015-07-24 14:40:59 +08:00
|
|
|
|
|
|
|
changeBackingMemoryBuffer(std::move(*ErrOrBuff));
|
2015-07-24 14:41:11 +08:00
|
|
|
std::vector<MemoryBufferRef> ArchiveBuffers;
|
|
|
|
auto ErrOrFat = object::MachOUniversalBinary::create(
|
|
|
|
CurrentMemoryBuffer->getMemBufferRef());
|
2016-06-28 05:39:39 +08:00
|
|
|
if (!ErrOrFat) {
|
|
|
|
consumeError(ErrOrFat.takeError());
|
2015-07-24 14:41:11 +08:00
|
|
|
// Not a fat binary must be a standard one.
|
|
|
|
ArchiveBuffers.push_back(CurrentMemoryBuffer->getMemBufferRef());
|
|
|
|
} else {
|
|
|
|
CurrentFatBinary = std::move(*ErrOrFat);
|
2016-05-09 22:44:14 +08:00
|
|
|
CurrentFatBinaryName = ArchiveFilename;
|
2015-07-24 14:41:11 +08:00
|
|
|
ArchiveBuffers = getMachOFatMemoryBuffers(
|
2016-05-09 22:44:14 +08:00
|
|
|
CurrentFatBinaryName, *CurrentMemoryBuffer, *CurrentFatBinary);
|
2015-07-24 14:41:11 +08:00
|
|
|
}
|
2015-01-06 05:29:28 +08:00
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
for (auto MemRef : ArchiveBuffers) {
|
|
|
|
auto ErrOrArchive = object::Archive::create(MemRef);
|
2016-06-30 04:35:44 +08:00
|
|
|
if (!ErrOrArchive)
|
|
|
|
return errorToErrorCode(ErrOrArchive.takeError());
|
2015-07-24 14:41:11 +08:00
|
|
|
CurrentArchives.push_back(std::move(*ErrOrArchive));
|
|
|
|
}
|
|
|
|
return GetArchiveMemberBuffers(Filename, Timestamp);
|
2015-01-06 05:29:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<const object::ObjectFile &>
|
2015-07-24 14:41:11 +08:00
|
|
|
BinaryHolder::getObjfileForArch(const Triple &T) {
|
|
|
|
for (const auto &Obj : CurrentObjectFiles) {
|
|
|
|
if (const auto *MachO = dyn_cast<object::MachOObjectFile>(Obj.get())) {
|
2016-04-23 07:21:13 +08:00
|
|
|
if (MachO->getArchTriple().str() == T.str())
|
2015-07-24 14:41:11 +08:00
|
|
|
return *MachO;
|
|
|
|
} else if (Obj->getArch() == T.getArch())
|
|
|
|
return *Obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
return make_error_code(object::object_error::arch_not_found);
|
|
|
|
}
|
2015-01-06 05:29:28 +08:00
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
ErrorOr<std::vector<const object::ObjectFile *>>
|
2016-11-09 19:43:52 +08:00
|
|
|
BinaryHolder::GetObjectFiles(StringRef Filename,
|
|
|
|
sys::TimePoint<std::chrono::seconds> Timestamp) {
|
2015-07-24 14:41:11 +08:00
|
|
|
auto ErrOrMemBufferRefs = GetMemoryBuffersForFile(Filename, Timestamp);
|
|
|
|
if (auto Err = ErrOrMemBufferRefs.getError())
|
2015-01-06 05:29:28 +08:00
|
|
|
return Err;
|
|
|
|
|
2015-07-24 14:41:11 +08:00
|
|
|
std::vector<const object::ObjectFile *> Objects;
|
|
|
|
Objects.reserve(ErrOrMemBufferRefs->size());
|
|
|
|
|
|
|
|
CurrentObjectFiles.clear();
|
|
|
|
for (auto MemBuf : *ErrOrMemBufferRefs) {
|
|
|
|
auto ErrOrObjectFile = object::ObjectFile::createObjectFile(MemBuf);
|
2016-04-07 06:14:09 +08:00
|
|
|
if (!ErrOrObjectFile)
|
|
|
|
return errorToErrorCode(ErrOrObjectFile.takeError());
|
2015-07-24 14:41:11 +08:00
|
|
|
|
|
|
|
Objects.push_back(ErrOrObjectFile->get());
|
|
|
|
CurrentObjectFiles.push_back(std::move(*ErrOrObjectFile));
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(Objects);
|
2015-01-06 05:29:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|