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/Object/WindowsResource.h" 21 #include "llvm/Support/Error.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/ErrorOr.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/MemoryBuffer.h" 26 #include <algorithm> 27 #include <memory> 28 #include <system_error> 29 30 using namespace llvm; 31 using namespace object; 32 33 Binary::~Binary() = default; 34 35 Binary::Binary(unsigned int Type, MemoryBufferRef Source) 36 : TypeID(Type), Data(Source) {} 37 38 StringRef Binary::getData() const { return Data.getBuffer(); } 39 40 StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); } 41 42 MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; } 43 44 Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer, 45 LLVMContext *Context) { 46 sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer()); 47 48 switch (Type) { 49 case sys::fs::file_magic::archive: 50 return Archive::create(Buffer); 51 case sys::fs::file_magic::elf: 52 case sys::fs::file_magic::elf_relocatable: 53 case sys::fs::file_magic::elf_executable: 54 case sys::fs::file_magic::elf_shared_object: 55 case sys::fs::file_magic::elf_core: 56 case sys::fs::file_magic::macho_object: 57 case sys::fs::file_magic::macho_executable: 58 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 59 case sys::fs::file_magic::macho_core: 60 case sys::fs::file_magic::macho_preload_executable: 61 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 62 case sys::fs::file_magic::macho_dynamic_linker: 63 case sys::fs::file_magic::macho_bundle: 64 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 65 case sys::fs::file_magic::macho_dsym_companion: 66 case sys::fs::file_magic::macho_kext_bundle: 67 case sys::fs::file_magic::coff_object: 68 case sys::fs::file_magic::coff_import_library: 69 case sys::fs::file_magic::pecoff_executable: 70 case sys::fs::file_magic::bitcode: 71 case sys::fs::file_magic::wasm_object: 72 return ObjectFile::createSymbolicFile(Buffer, Type, Context); 73 case sys::fs::file_magic::macho_universal_binary: 74 return MachOUniversalBinary::create(Buffer); 75 case sys::fs::file_magic::windows_resource: 76 return WindowsResource::createWindowsResource(Buffer); 77 case sys::fs::file_magic::unknown: 78 case sys::fs::file_magic::coff_cl_gl_object: 79 // Unrecognized object file format. 80 return errorCodeToError(object_error::invalid_file_type); 81 } 82 llvm_unreachable("Unexpected Binary File Type"); 83 } 84 85 Expected<OwningBinary<Binary>> object::createBinary(StringRef Path) { 86 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 87 MemoryBuffer::getFileOrSTDIN(Path); 88 if (std::error_code EC = FileOrErr.getError()) 89 return errorCodeToError(EC); 90 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); 91 92 Expected<std::unique_ptr<Binary>> BinOrErr = 93 createBinary(Buffer->getMemBufferRef()); 94 if (!BinOrErr) 95 return BinOrErr.takeError(); 96 std::unique_ptr<Binary> &Bin = BinOrErr.get(); 97 98 return OwningBinary<Binary>(std::move(Bin), std::move(Buffer)); 99 } 100