1 //===- Binary.cpp - A generic binary file ---------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the Binary class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/Object/Archive.h" 16 #include "llvm/Object/Binary.h" 17 #include "llvm/Object/Error.h" 18 #include "llvm/Object/MachOUniversal.h" 19 #include "llvm/Object/ObjectFile.h" 20 #include "llvm/Support/Error.h" 21 #include "llvm/Support/ErrorHandling.h" 22 #include "llvm/Support/ErrorOr.h" 23 #include "llvm/Support/FileSystem.h" 24 #include "llvm/Support/MemoryBuffer.h" 25 #include <algorithm> 26 #include <memory> 27 #include <system_error> 28 29 using namespace llvm; 30 using namespace object; 31 32 Binary::~Binary() = default; 33 34 Binary::Binary(unsigned int Type, MemoryBufferRef Source) 35 : TypeID(Type), Data(Source) {} 36 37 StringRef Binary::getData() const { return Data.getBuffer(); } 38 39 StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); } 40 41 MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; } 42 43 Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer, 44 LLVMContext *Context) { 45 sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer()); 46 47 switch (Type) { 48 case sys::fs::file_magic::archive: 49 return Archive::create(Buffer); 50 case sys::fs::file_magic::elf: 51 case sys::fs::file_magic::elf_relocatable: 52 case sys::fs::file_magic::elf_executable: 53 case sys::fs::file_magic::elf_shared_object: 54 case sys::fs::file_magic::elf_core: 55 case sys::fs::file_magic::macho_object: 56 case sys::fs::file_magic::macho_executable: 57 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 58 case sys::fs::file_magic::macho_core: 59 case sys::fs::file_magic::macho_preload_executable: 60 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 61 case sys::fs::file_magic::macho_dynamic_linker: 62 case sys::fs::file_magic::macho_bundle: 63 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 64 case sys::fs::file_magic::macho_dsym_companion: 65 case sys::fs::file_magic::macho_kext_bundle: 66 case sys::fs::file_magic::coff_object: 67 case sys::fs::file_magic::coff_import_library: 68 case sys::fs::file_magic::pecoff_executable: 69 case sys::fs::file_magic::bitcode: 70 case sys::fs::file_magic::wasm_object: 71 return ObjectFile::createSymbolicFile(Buffer, Type, Context); 72 case sys::fs::file_magic::macho_universal_binary: 73 return MachOUniversalBinary::create(Buffer); 74 case sys::fs::file_magic::unknown: 75 case sys::fs::file_magic::coff_cl_gl_object: 76 case sys::fs::file_magic::windows_resource: 77 // Unrecognized object file format. 78 return errorCodeToError(object_error::invalid_file_type); 79 } 80 llvm_unreachable("Unexpected Binary File Type"); 81 } 82 83 Expected<OwningBinary<Binary>> object::createBinary(StringRef Path) { 84 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 85 MemoryBuffer::getFileOrSTDIN(Path); 86 if (std::error_code EC = FileOrErr.getError()) 87 return errorCodeToError(EC); 88 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); 89 90 Expected<std::unique_ptr<Binary>> BinOrErr = 91 createBinary(Buffer->getMemBufferRef()); 92 if (!BinOrErr) 93 return BinOrErr.takeError(); 94 std::unique_ptr<Binary> &Bin = BinOrErr.get(); 95 96 return OwningBinary<Binary>(std::move(Bin), std::move(Buffer)); 97 } 98