Michael J. Spencer | c44c915 | 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" |
| 16 | #include "llvm/Support/MemoryBuffer.h" |
Rafael Espindola | de6fe4d | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 17 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Path.h" |
| 19 | |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 20 | // Include headers for createBinary. |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 21 | #include "llvm/Object/Archive.h" |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 22 | #include "llvm/Object/COFF.h" |
Alexey Samsonov | 9c22f87 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 23 | #include "llvm/Object/MachOUniversal.h" |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 24 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 25 | |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | using namespace object; |
| 28 | |
| 29 | Binary::~Binary() { |
| 30 | delete Data; |
| 31 | } |
| 32 | |
| 33 | Binary::Binary(unsigned int Type, MemoryBuffer *Source) |
| 34 | : TypeID(Type) |
| 35 | , Data(Source) {} |
| 36 | |
| 37 | StringRef Binary::getData() const { |
| 38 | return Data->getBuffer(); |
| 39 | } |
| 40 | |
| 41 | StringRef Binary::getFileName() const { |
| 42 | return Data->getBufferIdentifier(); |
| 43 | } |
| 44 | |
| 45 | error_code object::createBinary(MemoryBuffer *Source, |
| 46 | OwningPtr<Binary> &Result) { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 47 | OwningPtr<MemoryBuffer> scopedSource(Source); |
| 48 | if (!Source) |
| 49 | return make_error_code(errc::invalid_argument); |
Rafael Espindola | de6fe4d | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 50 | sys::fs::file_magic type = sys::fs::identify_magic(Source->getBuffer()); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 51 | error_code ec; |
| 52 | switch (type) { |
Rafael Espindola | de6fe4d | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 53 | case sys::fs::file_magic::archive: { |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 54 | OwningPtr<Binary> ret(new Archive(scopedSource.take(), ec)); |
| 55 | if (ec) return ec; |
| 56 | Result.swap(ret); |
| 57 | return object_error::success; |
| 58 | } |
Rafael Espindola | de6fe4d | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 59 | case sys::fs::file_magic::elf_relocatable: |
| 60 | case sys::fs::file_magic::elf_executable: |
| 61 | case sys::fs::file_magic::elf_shared_object: |
| 62 | case sys::fs::file_magic::elf_core: { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 63 | OwningPtr<Binary> ret( |
| 64 | ObjectFile::createELFObjectFile(scopedSource.take())); |
| 65 | if (!ret) |
| 66 | return object_error::invalid_file_type; |
| 67 | Result.swap(ret); |
| 68 | return object_error::success; |
| 69 | } |
Rafael Espindola | de6fe4d | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 70 | case sys::fs::file_magic::macho_object: |
| 71 | case sys::fs::file_magic::macho_executable: |
| 72 | case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: |
| 73 | case sys::fs::file_magic::macho_core: |
| 74 | case sys::fs::file_magic::macho_preload_executable: |
| 75 | case sys::fs::file_magic::macho_dynamically_linked_shared_lib: |
| 76 | case sys::fs::file_magic::macho_dynamic_linker: |
| 77 | case sys::fs::file_magic::macho_bundle: |
Alexey Samsonov | f49d8fc | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 78 | case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: |
| 79 | case sys::fs::file_magic::macho_dsym_companion: { |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 80 | OwningPtr<Binary> ret( |
| 81 | ObjectFile::createMachOObjectFile(scopedSource.take())); |
| 82 | if (!ret) |
| 83 | return object_error::invalid_file_type; |
| 84 | Result.swap(ret); |
| 85 | return object_error::success; |
| 86 | } |
Alexey Samsonov | 9c22f87 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 87 | case sys::fs::file_magic::macho_universal_binary: { |
| 88 | OwningPtr<Binary> ret(new MachOUniversalBinary(scopedSource.take(), ec)); |
| 89 | if (ec) return ec; |
| 90 | Result.swap(ret); |
| 91 | return object_error::success; |
| 92 | } |
Rafael Espindola | de6fe4d | 2013-06-11 14:39:59 +0000 | [diff] [blame] | 93 | case sys::fs::file_magic::coff_object: |
| 94 | case sys::fs::file_magic::pecoff_executable: { |
Rui Ueyama | 4bf771b | 2013-06-12 19:10:33 +0000 | [diff] [blame] | 95 | OwningPtr<Binary> ret( |
| 96 | ObjectFile::createCOFFObjectFile(scopedSource.take())); |
| 97 | if (!ret) |
| 98 | return object_error::invalid_file_type; |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 99 | Result.swap(ret); |
| 100 | return object_error::success; |
| 101 | } |
Alexey Samsonov | f49d8fc | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 102 | case sys::fs::file_magic::unknown: |
| 103 | case sys::fs::file_magic::bitcode: { |
| 104 | // Unrecognized object file format. |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 105 | return object_error::invalid_file_type; |
Alexey Samsonov | f49d8fc | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 106 | } |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 107 | } |
Alexey Samsonov | f49d8fc | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 108 | llvm_unreachable("Unexpected Binary File Type"); |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | error_code object::createBinary(StringRef Path, OwningPtr<Binary> &Result) { |
| 112 | OwningPtr<MemoryBuffer> File; |
Michael J. Spencer | c6500a5 | 2011-10-08 00:17:58 +0000 | [diff] [blame] | 113 | if (error_code ec = MemoryBuffer::getFileOrSTDIN(Path, File)) |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 114 | return ec; |
| 115 | return createBinary(File.take(), Result); |
| 116 | } |