Michael J. Spencer | b02c95d | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 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" |
Rafael Espindola | 110801a | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 16 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | b02c95d | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
| 19 | |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 20 | // Include headers for createBinary. |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 21 | #include "llvm/Object/Archive.h" |
Alexey Samsonov | e6388e6 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 22 | #include "llvm/Object/MachOUniversal.h" |
Michael J. Spencer | d3b7b12 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 23 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 24 | |
Michael J. Spencer | b02c95d | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | using namespace object; |
| 27 | |
| 28 | Binary::~Binary() { |
| 29 | delete Data; |
| 30 | } |
| 31 | |
| 32 | Binary::Binary(unsigned int Type, MemoryBuffer *Source) |
| 33 | : TypeID(Type) |
| 34 | , 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 | |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 44 | ErrorOr<Binary *> object::createBinary(MemoryBuffer *Source) { |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 45 | OwningPtr<MemoryBuffer> scopedSource(Source); |
Rafael Espindola | 110801a | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 46 | sys::fs::file_magic type = sys::fs::identify_magic(Source->getBuffer()); |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 47 | switch (type) { |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 48 | case sys::fs::file_magic::archive: |
| 49 | return Archive::create(scopedSource.take()); |
Rafael Espindola | 110801a | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 50 | case sys::fs::file_magic::elf_relocatable: |
| 51 | case sys::fs::file_magic::elf_executable: |
| 52 | case sys::fs::file_magic::elf_shared_object: |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 53 | case sys::fs::file_magic::elf_core: |
| 54 | return ObjectFile::createELFObjectFile(scopedSource.take()); |
Rafael Espindola | 110801a | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 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: |
Alexey Samsonov | 64188f9 | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 63 | case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 64 | case sys::fs::file_magic::macho_dsym_companion: |
| 65 | return ObjectFile::createMachOObjectFile(scopedSource.take()); |
| 66 | case sys::fs::file_magic::macho_universal_binary: |
| 67 | return MachOUniversalBinary::create(scopedSource.take()); |
Rafael Espindola | 110801a | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 68 | case sys::fs::file_magic::coff_object: |
Rui Ueyama | e448f9e | 2013-11-15 21:22:02 +0000 | [diff] [blame] | 69 | case sys::fs::file_magic::coff_import_library: |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 70 | case sys::fs::file_magic::pecoff_executable: |
| 71 | return ObjectFile::createCOFFObjectFile(scopedSource.take()); |
Alexey Samsonov | 64188f9 | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 72 | case sys::fs::file_magic::unknown: |
Rui Ueyama | fc149a6 | 2013-10-15 22:45:38 +0000 | [diff] [blame] | 73 | case sys::fs::file_magic::bitcode: |
| 74 | case sys::fs::file_magic::windows_resource: { |
Alexey Samsonov | 64188f9 | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 75 | // Unrecognized object file format. |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 76 | return object_error::invalid_file_type; |
Alexey Samsonov | 64188f9 | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 77 | } |
Michael J. Spencer | ec29b12 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 78 | } |
Alexey Samsonov | 64188f9 | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 79 | llvm_unreachable("Unexpected Binary File Type"); |
Michael J. Spencer | b02c95d | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 82 | ErrorOr<Binary *> object::createBinary(StringRef Path) { |
Michael J. Spencer | b02c95d | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 83 | OwningPtr<MemoryBuffer> File; |
Rafael Espindola | 63da295 | 2014-01-15 19:37:43 +0000 | [diff] [blame] | 84 | if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, File)) |
| 85 | return EC; |
| 86 | return createBinary(File.take()); |
Michael J. Spencer | b02c95d | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 87 | } |