David Blaikie 95689f0845 Correct the ownership passing semantics of object::createBinary and make them explicit in the type system.
createBinary documented that it destroyed the parameter in error cases,
though by observation it does not. By passing the unique_ptr by value
rather than lvalue reference, callers are now explicit about passing
ownership and the function implements the documented contract. Remove
the explicit documentation, since now the behavior cannot be anything
other than what was documented, so it's redundant.

Also drops a unique_ptr::release in llvm-nm that was always run on a
null unique_ptr anyway.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213557 91177308-0d34-0410-b5e6-96231b3b80d8
2014-07-21 16:26:24 +00:00

137 lines
3.1 KiB
C++

//===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file declares the Binary class.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_OBJECT_BINARY_H
#define LLVM_OBJECT_BINARY_H
#include "llvm/Object/Error.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
namespace llvm {
class LLVMContext;
class MemoryBuffer;
class StringRef;
namespace object {
class Binary {
private:
Binary() LLVM_DELETED_FUNCTION;
Binary(const Binary &other) LLVM_DELETED_FUNCTION;
unsigned int TypeID;
protected:
std::unique_ptr<MemoryBuffer> Data;
Binary(unsigned int Type, std::unique_ptr<MemoryBuffer> Source);
enum {
ID_Archive,
ID_MachOUniversalBinary,
ID_IR, // LLVM IR
// Object and children.
ID_StartObjects,
ID_COFF,
ID_ELF32L, // ELF 32-bit, little endian
ID_ELF32B, // ELF 32-bit, big endian
ID_ELF64L, // ELF 64-bit, little endian
ID_ELF64B, // ELF 64-bit, big endian
ID_MachO32L, // MachO 32-bit, little endian
ID_MachO32B, // MachO 32-bit, big endian
ID_MachO64L, // MachO 64-bit, little endian
ID_MachO64B, // MachO 64-bit, big endian
ID_EndObjects
};
static inline unsigned int getELFType(bool isLE, bool is64Bits) {
if (isLE)
return is64Bits ? ID_ELF64L : ID_ELF32L;
else
return is64Bits ? ID_ELF64B : ID_ELF32B;
}
static unsigned int getMachOType(bool isLE, bool is64Bits) {
if (isLE)
return is64Bits ? ID_MachO64L : ID_MachO32L;
else
return is64Bits ? ID_MachO64B : ID_MachO32B;
}
public:
virtual ~Binary();
StringRef getData() const;
MemoryBuffer *releaseBuffer() { return Data.release(); }
StringRef getFileName() const;
// Cast methods.
unsigned int getType() const { return TypeID; }
// Convenience methods
bool isObject() const {
return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
}
bool isSymbolic() const {
return isIR() || isObject();
}
bool isArchive() const {
return TypeID == ID_Archive;
}
bool isMachOUniversalBinary() const {
return TypeID == ID_MachOUniversalBinary;
}
bool isELF() const {
return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
}
bool isMachO() const {
return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
}
bool isCOFF() const {
return TypeID == ID_COFF;
}
bool isIR() const {
return TypeID == ID_IR;
}
bool isLittleEndian() const {
return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
TypeID == ID_MachO32B || TypeID == ID_MachO64B);
}
};
/// @brief Create a Binary from Source, autodetecting the file type.
///
/// @param Source The data to create the Binary from.
ErrorOr<Binary *> createBinary(std::unique_ptr<MemoryBuffer> Source,
LLVMContext *Context = nullptr);
ErrorOr<Binary *> createBinary(StringRef Path);
}
}
#endif