llvm/tools/dsymutil/BinaryHolder.cpp

201 lines
6.9 KiB
C++
Raw Normal View History

//===-- 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"
#include "llvm/Object/MachO.h"
#include "llvm/Support/raw_ostream.h"
namespace llvm {
namespace dsymutil {
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;
}
void BinaryHolder::changeBackingMemoryBuffer(
std::unique_ptr<MemoryBuffer> &&Buf) {
CurrentArchives.clear();
CurrentObjectFiles.clear();
CurrentFatBinary.reset();
CurrentMemoryBuffer = std::move(Buf);
}
ErrorOr<std::vector<MemoryBufferRef>>
BinaryHolder::GetMemoryBuffersForFile(StringRef Filename,
sys::TimeValue Timestamp) {
if (Verbose)
outs() << "trying to open '" << Filename << "'\n";
// Try that first as it doesn't involve any filesystem access.
if (auto ErrOrArchiveMembers = GetArchiveMemberBuffers(Filename, Timestamp))
return *ErrOrArchiveMembers;
// If the name ends with a closing paren, there is a huge chance
// it is an archive member specification.
if (Filename.endswith(")"))
if (auto ErrOrArchiveMembers =
MapArchiveAndGetMemberBuffers(Filename, Timestamp))
return *ErrOrArchiveMembers;
// 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;
changeBackingMemoryBuffer(std::move(*ErrOrFile));
if (Verbose)
outs() << "\tloaded file.\n";
auto ErrOrFat = object::MachOUniversalBinary::create(
CurrentMemoryBuffer->getMemBufferRef());
if (ErrOrFat.getError()) {
// Not a fat binary must be a standard one. Return a one element vector.
return std::vector<MemoryBufferRef>{CurrentMemoryBuffer->getMemBufferRef()};
}
CurrentFatBinary = std::move(*ErrOrFat);
return getMachOFatMemoryBuffers(Filename, *CurrentMemoryBuffer,
*CurrentFatBinary);
}
ErrorOr<std::vector<MemoryBufferRef>>
BinaryHolder::GetArchiveMemberBuffers(StringRef Filename,
sys::TimeValue Timestamp) {
if (CurrentArchives.empty())
return make_error_code(errc::no_such_file_or_directory);
StringRef CurArchiveName = CurrentArchives.front()->getFileName();
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();
std::vector<MemoryBufferRef> Buffers;
Buffers.reserve(CurrentArchives.size());
for (const auto &CurrentArchive : CurrentArchives) {
Reapply r250906 with many suggested updates from Rafael Espindola. The needed lld matching changes to be submitted immediately next, but this revision will cause lld failures with this alone which is expected. This removes the eating of the error in Archive::Child::getSize() when the characters in the size field in the archive header for the member is not a number. To do this we have all of the needed methods return ErrorOr to push them up until we get out of lib. Then the tools and can handle the error in whatever way is appropriate for that tool. So the solution is to plumb all the ErrorOr stuff through everything that touches archives. This include its iterators as one can create an Archive object but the first or any other Child object may fail to be created due to a bad size field in its header. Thanks to Lang Hames on the changes making child_iterator contain an ErrorOr<Child> instead of a Child and the needed changes to ErrorOr.h to add operator overloading for * and -> . We don’t want to use llvm_unreachable() as it calls abort() and is produces a “crash” and using report_fatal_error() to move the error checking will cause the program to stop, neither of which are really correct in library code. There are still some uses of these that should be cleaned up in this library code for other than the size field. The test cases use archives with text files so one can see the non-digit character, in this case a ‘%’, in the size field. These changes will require corresponding changes to the lld project. That will be committed immediately after this change. But this revision will cause lld failures with this alone which is expected. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@252192 91177308-0d34-0410-b5e6-96231b3b80d8
2015-11-05 19:24:56 +00:00
for (auto ChildOrErr : CurrentArchive->children()) {
if (std::error_code Err = ChildOrErr.getError())
return Err;
const auto &Child = *ChildOrErr;
if (auto NameOrErr = Child.getName()) {
if (*NameOrErr == Filename) {
if (Timestamp != sys::TimeValue::PosixZeroTime() &&
Timestamp != Child.getLastModified()) {
if (Verbose)
outs() << "\tmember had timestamp mismatch.\n";
continue;
}
if (Verbose)
outs() << "\tfound member in current archive.\n";
auto ErrOrMem = Child.getMemoryBufferRef();
if (auto Err = ErrOrMem.getError())
return Err;
Buffers.push_back(*ErrOrMem);
}
}
}
}
if (Buffers.empty())
return make_error_code(errc::no_such_file_or_directory);
return Buffers;
}
ErrorOr<std::vector<MemoryBufferRef>>
BinaryHolder::MapArchiveAndGetMemberBuffers(StringRef Filename,
sys::TimeValue Timestamp) {
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";
changeBackingMemoryBuffer(std::move(*ErrOrBuff));
std::vector<MemoryBufferRef> ArchiveBuffers;
auto ErrOrFat = object::MachOUniversalBinary::create(
CurrentMemoryBuffer->getMemBufferRef());
if (ErrOrFat.getError()) {
// Not a fat binary must be a standard one.
ArchiveBuffers.push_back(CurrentMemoryBuffer->getMemBufferRef());
} else {
CurrentFatBinary = std::move(*ErrOrFat);
ArchiveBuffers = getMachOFatMemoryBuffers(
ArchiveFilename, *CurrentMemoryBuffer, *CurrentFatBinary);
}
for (auto MemRef : ArchiveBuffers) {
auto ErrOrArchive = object::Archive::create(MemRef);
if (auto Err = ErrOrArchive.getError())
return Err;
CurrentArchives.push_back(std::move(*ErrOrArchive));
}
return GetArchiveMemberBuffers(Filename, Timestamp);
}
ErrorOr<const object::ObjectFile &>
BinaryHolder::getObjfileForArch(const Triple &T) {
for (const auto &Obj : CurrentObjectFiles) {
if (const auto *MachO = dyn_cast<object::MachOObjectFile>(Obj.get())) {
if (MachO->getArchTriple().str() == T.str())
return *MachO;
} else if (Obj->getArch() == T.getArch())
return *Obj;
}
return make_error_code(object::object_error::arch_not_found);
}
ErrorOr<std::vector<const object::ObjectFile *>>
BinaryHolder::GetObjectFiles(StringRef Filename, sys::TimeValue Timestamp) {
auto ErrOrMemBufferRefs = GetMemoryBuffersForFile(Filename, Timestamp);
if (auto Err = ErrOrMemBufferRefs.getError())
return Err;
std::vector<const object::ObjectFile *> Objects;
Objects.reserve(ErrOrMemBufferRefs->size());
CurrentObjectFiles.clear();
for (auto MemBuf : *ErrOrMemBufferRefs) {
auto ErrOrObjectFile = object::ObjectFile::createObjectFile(MemBuf);
Thread Expected<...> up from createMachOObjectFile() to allow llvm-objdump to produce a real error message Produce the first specific error message for a malformed Mach-O file describing the problem instead of the generic message for object_error::parse_failed of "Invalid data was encountered while parsing the file”.  Many more good error messages will follow after this first one. This is built on Lang Hames’ great work of adding the ’Error' class for structured error handling and threading Error through MachOObjectFile construction. And making createMachOObjectFile return Expected<...> . So to to get the error to the llvm-obdump tool, I changed the stack of these methods to also return Expected<...> : object::ObjectFile::createObjectFile() object::SymbolicFile::createSymbolicFile() object::createBinary() Then finally in ParseInputMachO() in MachODump.cpp the error can be reported and the specific error message can be printed in llvm-objdump and can be seen in the existing test case for the existing malformed binary but with the updated error message. Converting these interfaces to Expected<> from ErrorOr<> does involve touching a number of places. To contain the changes for now use of errorToErrorCode() and errorOrToExpected() are used where the callers are yet to be converted. Also there some were bugs in the existing code that did not deal with the old ErrorOr<> return values. So now with Expected<> since they must be checked and the error handled, I added a TODO and a comment: “// TODO: Actually report errors helpfully” and a call something like consumeError(ObjOrErr.takeError()) so the buggy code will not crash since needed to deal with the Error. Note there is one fix also needed to lld/COFF/InputFiles.cpp that goes along with this that I will commit right after this. So expect lld not to built after this commit and before the next one. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@265606 91177308-0d34-0410-b5e6-96231b3b80d8
2016-04-06 22:14:09 +00:00
if (!ErrOrObjectFile)
return errorToErrorCode(ErrOrObjectFile.takeError());
Objects.push_back(ErrOrObjectFile->get());
CurrentObjectFiles.push_back(std::move(*ErrOrObjectFile));
}
return std::move(Objects);
}
}
}