1 //===- Binary.cpp - A generic binary file -----------------------*- C++ -*-===// 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/Object/Binary.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/Support/FileSystem.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "llvm/Support/Path.h" 19 20 // Include headers for createBinary. 21 #include "llvm/Object/Archive.h" 22 #include "llvm/Object/MachOUniversal.h" 23 #include "llvm/Object/ObjectFile.h" 24 25 using namespace llvm; 26 using namespace object; 27 28 Binary::~Binary() {} 29 30 Binary::Binary(unsigned int Type, std::unique_ptr<MemoryBuffer> Source) 31 : TypeID(Type), Data(std::move(Source)) {} 32 33 StringRef Binary::getData() const { 34 return Data->getBuffer(); 35 } 36 37 StringRef Binary::getFileName() const { 38 return Data->getBufferIdentifier(); 39 } 40 41 ErrorOr<std::unique_ptr<Binary>> 42 object::createBinary(std::unique_ptr<MemoryBuffer> Buffer, 43 LLVMContext *Context) { 44 sys::fs::file_magic Type = sys::fs::identify_magic(Buffer->getBuffer()); 45 46 switch (Type) { 47 case sys::fs::file_magic::archive: 48 return Archive::create(std::move(Buffer)); 49 case sys::fs::file_magic::elf_relocatable: 50 case sys::fs::file_magic::elf_executable: 51 case sys::fs::file_magic::elf_shared_object: 52 case sys::fs::file_magic::elf_core: 53 case sys::fs::file_magic::macho_object: 54 case sys::fs::file_magic::macho_executable: 55 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 56 case sys::fs::file_magic::macho_core: 57 case sys::fs::file_magic::macho_preload_executable: 58 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 59 case sys::fs::file_magic::macho_dynamic_linker: 60 case sys::fs::file_magic::macho_bundle: 61 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 62 case sys::fs::file_magic::macho_dsym_companion: 63 case sys::fs::file_magic::coff_object: 64 case sys::fs::file_magic::coff_import_library: 65 case sys::fs::file_magic::pecoff_executable: 66 case sys::fs::file_magic::bitcode: 67 return ObjectFile::createSymbolicFile(Buffer, Type, Context); 68 case sys::fs::file_magic::macho_universal_binary: 69 return MachOUniversalBinary::create(std::move(Buffer)); 70 case sys::fs::file_magic::unknown: 71 case sys::fs::file_magic::windows_resource: 72 // Unrecognized object file format. 73 return object_error::invalid_file_type; 74 } 75 llvm_unreachable("Unexpected Binary File Type"); 76 } 77 78 ErrorOr<std::unique_ptr<Binary>> object::createBinary(StringRef Path) { 79 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 80 MemoryBuffer::getFileOrSTDIN(Path); 81 if (std::error_code EC = FileOrErr.getError()) 82 return EC; 83 return createBinary(std::move(*FileOrErr)); 84 } 85