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 if (BufferOwned) 30 delete Data; 31 } 32 33 Binary::Binary(unsigned int Type, MemoryBuffer *Source, bool BufferOwned) 34 : TypeID(Type), BufferOwned(BufferOwned), Data(Source) {} 35 36 StringRef Binary::getData() const { 37 return Data->getBuffer(); 38 } 39 40 StringRef Binary::getFileName() const { 41 return Data->getBufferIdentifier(); 42 } 43 44 ErrorOr<Binary *> object::createBinary(MemoryBuffer *Source, 45 sys::fs::file_magic Type) { 46 OwningPtr<MemoryBuffer> scopedSource(Source); 47 if (Type == sys::fs::file_magic::unknown) 48 Type = sys::fs::identify_magic(Source->getBuffer()); 49 50 switch (Type) { 51 case sys::fs::file_magic::archive: 52 return Archive::create(scopedSource.take()); 53 case sys::fs::file_magic::elf_relocatable: 54 case sys::fs::file_magic::elf_executable: 55 case sys::fs::file_magic::elf_shared_object: 56 case sys::fs::file_magic::elf_core: 57 case sys::fs::file_magic::macho_object: 58 case sys::fs::file_magic::macho_executable: 59 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 60 case sys::fs::file_magic::macho_core: 61 case sys::fs::file_magic::macho_preload_executable: 62 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 63 case sys::fs::file_magic::macho_dynamic_linker: 64 case sys::fs::file_magic::macho_bundle: 65 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 66 case sys::fs::file_magic::macho_dsym_companion: 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 return ObjectFile::createObjectFile(scopedSource.take(), true, Type); 71 case sys::fs::file_magic::macho_universal_binary: 72 return MachOUniversalBinary::create(scopedSource.take()); 73 case sys::fs::file_magic::unknown: 74 case sys::fs::file_magic::bitcode: 75 case sys::fs::file_magic::windows_resource: 76 // Unrecognized object file format. 77 return object_error::invalid_file_type; 78 } 79 llvm_unreachable("Unexpected Binary File Type"); 80 } 81 82 ErrorOr<Binary *> object::createBinary(StringRef Path) { 83 OwningPtr<MemoryBuffer> File; 84 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, File)) 85 return EC; 86 return createBinary(File.take()); 87 } 88