2011-06-25 17:54:29 +00:00
|
|
|
//===- 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"
|
2014-01-15 19:37:43 +00:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2014-01-22 16:04:52 +00:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2011-06-25 17:54:29 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2014-02-21 20:10:59 +00:00
|
|
|
class LLVMContext;
|
2011-06-25 17:54:29 +00:00
|
|
|
class MemoryBuffer;
|
|
|
|
class StringRef;
|
|
|
|
|
|
|
|
namespace object {
|
|
|
|
|
|
|
|
class Binary {
|
|
|
|
private:
|
2012-09-17 07:16:40 +00:00
|
|
|
Binary() LLVM_DELETED_FUNCTION;
|
|
|
|
Binary(const Binary &other) LLVM_DELETED_FUNCTION;
|
2011-06-25 17:54:29 +00:00
|
|
|
|
|
|
|
unsigned int TypeID;
|
2014-01-24 21:32:21 +00:00
|
|
|
bool BufferOwned;
|
2011-06-25 17:54:29 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
MemoryBuffer *Data;
|
|
|
|
|
2014-01-24 21:32:21 +00:00
|
|
|
Binary(unsigned int Type, MemoryBuffer *Source, bool BufferOwned = true);
|
2011-06-25 17:54:29 +00:00
|
|
|
|
|
|
|
enum {
|
2012-03-09 20:41:57 +00:00
|
|
|
ID_Archive,
|
2013-06-18 15:03:28 +00:00
|
|
|
ID_MachOUniversalBinary,
|
2014-02-21 20:10:59 +00:00
|
|
|
ID_IR, // LLVM IR
|
|
|
|
|
2011-06-25 17:54:50 +00:00
|
|
|
// Object and children.
|
2012-03-09 20:41:57 +00:00
|
|
|
ID_StartObjects,
|
|
|
|
ID_COFF,
|
2013-04-08 13:25:33 +00:00
|
|
|
|
2012-03-09 20:41:57 +00:00
|
|
|
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
|
2013-04-08 13:25:33 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2012-03-09 20:41:57 +00:00
|
|
|
ID_EndObjects
|
2011-06-25 17:54:29 +00:00
|
|
|
};
|
|
|
|
|
2013-01-09 02:45:05 +00:00
|
|
|
static inline unsigned int getELFType(bool isLE, bool is64Bits) {
|
|
|
|
if (isLE)
|
2012-03-09 20:41:57 +00:00
|
|
|
return is64Bits ? ID_ELF64L : ID_ELF32L;
|
|
|
|
else
|
|
|
|
return is64Bits ? ID_ELF64B : ID_ELF32B;
|
|
|
|
}
|
|
|
|
|
2013-04-08 13:25:33 +00:00
|
|
|
static unsigned int getMachOType(bool isLE, bool is64Bits) {
|
|
|
|
if (isLE)
|
|
|
|
return is64Bits ? ID_MachO64L : ID_MachO32L;
|
|
|
|
else
|
|
|
|
return is64Bits ? ID_MachO64B : ID_MachO32B;
|
|
|
|
}
|
|
|
|
|
2011-06-25 17:54:29 +00:00
|
|
|
public:
|
|
|
|
virtual ~Binary();
|
|
|
|
|
|
|
|
StringRef getData() const;
|
|
|
|
StringRef getFileName() const;
|
|
|
|
|
|
|
|
// Cast methods.
|
|
|
|
unsigned int getType() const { return TypeID; }
|
2012-03-09 20:41:57 +00:00
|
|
|
|
|
|
|
// Convenience methods
|
|
|
|
bool isObject() const {
|
|
|
|
return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
|
|
|
|
}
|
|
|
|
|
2014-02-21 20:10:59 +00:00
|
|
|
bool isSymbolic() const {
|
|
|
|
return isIR() || isObject();
|
|
|
|
}
|
|
|
|
|
2012-03-09 20:41:57 +00:00
|
|
|
bool isArchive() const {
|
|
|
|
return TypeID == ID_Archive;
|
|
|
|
}
|
|
|
|
|
2013-06-18 15:03:28 +00:00
|
|
|
bool isMachOUniversalBinary() const {
|
|
|
|
return TypeID == ID_MachOUniversalBinary;
|
|
|
|
}
|
|
|
|
|
2012-03-09 20:41:57 +00:00
|
|
|
bool isELF() const {
|
|
|
|
return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isMachO() const {
|
2013-04-08 13:25:33 +00:00
|
|
|
return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
|
2012-03-09 20:41:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isCOFF() const {
|
|
|
|
return TypeID == ID_COFF;
|
|
|
|
}
|
2013-01-09 02:45:05 +00:00
|
|
|
|
2014-02-21 20:10:59 +00:00
|
|
|
bool isIR() const {
|
|
|
|
return TypeID == ID_IR;
|
|
|
|
}
|
|
|
|
|
2013-01-09 02:45:05 +00:00
|
|
|
bool isLittleEndian() const {
|
2013-04-18 18:08:55 +00:00
|
|
|
return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
|
|
|
|
TypeID == ID_MachO32B || TypeID == ID_MachO64B);
|
2013-01-09 02:45:05 +00:00
|
|
|
}
|
2011-06-25 17:54:29 +00:00
|
|
|
};
|
|
|
|
|
2012-03-19 20:27:15 +00:00
|
|
|
/// @brief Create a Binary from Source, autodetecting the file type.
|
|
|
|
///
|
2012-06-02 10:20:22 +00:00
|
|
|
/// @param Source The data to create the Binary from. Ownership is transferred
|
2014-01-15 19:37:43 +00:00
|
|
|
/// to the Binary if successful. If an error is returned,
|
|
|
|
/// Source is destroyed by createBinary before returning.
|
2014-04-13 04:57:38 +00:00
|
|
|
ErrorOr<Binary *> createBinary(MemoryBuffer *Source,
|
|
|
|
LLVMContext *Context = nullptr);
|
2011-06-25 17:54:29 +00:00
|
|
|
|
2014-01-15 19:37:43 +00:00
|
|
|
ErrorOr<Binary *> createBinary(StringRef Path);
|
2011-06-25 17:54:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|