diff --git a/llvm/include/llvm-c/Object.h b/llvm/include/llvm-c/Object.h --- a/llvm/include/llvm-c/Object.h +++ b/llvm/include/llvm-c/Object.h @@ -39,6 +39,24 @@ typedef struct LLVMOpaqueSymbolIterator *LLVMSymbolIteratorRef; typedef struct LLVMOpaqueRelocationIterator *LLVMRelocationIteratorRef; +typedef enum { + LLVMBinaryTypeArchive, /**< Archive file. */ + LLVMBinaryTypeMachOUniversalBinary, /**< Mach-O Universal Binary file. */ + LLVMBinaryTypeCOFFImportFile, /**< COFF Import file. */ + LLVMBinaryTypeIR, /**< LLVM IR. */ + LLVMBinaryTypeWinRes, /**< Windows resource (.res) file. */ + LLVMBinaryTypeCOFF, /**< COFF Object file. */ + LLVMBinaryTypeELF32L, /**< ELF 32-bit, little endian. */ + LLVMBinaryTypeELF32B, /**< ELF 32-bit, big endian. */ + LLVMBinaryTypeELF64L, /**< ELF 64-bit, little endian. */ + LLVMBinaryTypeELF64B, /**< ELF 64-bit, big endian. */ + LLVMBinaryTypeMachO32L, /**< MachO 32-bit, little endian. */ + LLVMBinaryTypeMachO32B, /**< MachO 32-bit, big endian. */ + LLVMBinaryTypeMachO64L, /**< MachO 64-bit, little endian. */ + LLVMBinaryTypeMachO64B, /**< MachO 64-bit, big endian. */ + LLVMBinaryTypeWasm, /**< Web Assembly. */ +} LLVMBinaryType; + /** * Create a binary file from the given memory buffer. * @@ -78,6 +96,14 @@ */ LLVMMemoryBufferRef LLVMBinaryCopyMemoryBuffer(LLVMBinaryRef BR); +/** + * Retrieve the specific type of a binary. + * + * @see llvm::object::Binary::getType + */ +LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR); + + // ObjectFile creation LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf); void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile); diff --git a/llvm/lib/Object/Object.cpp b/llvm/lib/Object/Object.cpp --- a/llvm/lib/Object/Object.cpp +++ b/llvm/lib/Object/Object.cpp @@ -85,6 +85,52 @@ delete unwrap(BR); } +LLVMBinaryType LLVMBinaryGetType(LLVMBinaryRef BR) { + class BinaryTypeMapper final : public Binary { + public: + static LLVMBinaryType mapBinaryTypeToLLVMBinaryType(unsigned Kind) { + switch (Kind) { + case ID_Archive: + return LLVMBinaryTypeArchive; + case ID_MachOUniversalBinary: + return LLVMBinaryTypeMachOUniversalBinary; + case ID_COFFImportFile: + return LLVMBinaryTypeCOFFImportFile; + case ID_IR: + return LLVMBinaryTypeIR; + case ID_WinRes: + return LLVMBinaryTypeWinRes; + case ID_COFF: + return LLVMBinaryTypeCOFF; + case ID_ELF32L: + return LLVMBinaryTypeELF32L; + case ID_ELF32B: + return LLVMBinaryTypeELF32B; + case ID_ELF64L: + return LLVMBinaryTypeELF64L; + case ID_ELF64B: + return LLVMBinaryTypeELF64B; + case ID_MachO32L: + return LLVMBinaryTypeMachO32L; + case ID_MachO32B: + return LLVMBinaryTypeMachO32B; + case ID_MachO64L: + return LLVMBinaryTypeMachO64L; + case ID_MachO64B: + return LLVMBinaryTypeMachO64B; + case ID_Wasm: + return LLVMBinaryTypeWasm; + case ID_StartObjects: + case ID_EndObjects: + llvm_unreachable("Marker types are not valid binary kinds!"); + default: + llvm_unreachable("Unknown binary kind!"); + } + } + }; + return BinaryTypeMapper::mapBinaryTypeToLLVMBinaryType(unwrap(BR)->getType()); +} + // ObjectFile creation LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) { std::unique_ptr Buf(unwrap(MemBuf));