Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 1 | //===- Binary.cpp - A generic binary file ---------------------------------===// |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/Object/Binary.h" |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 19ca2b0 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 16 | #include "llvm/BinaryFormat/Magic.h" |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 17 | #include "llvm/Object/Archive.h" |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 18 | #include "llvm/Object/Error.h" |
Alexey Samsonov | 9c22f87 | 2013-06-18 15:03:28 +0000 | [diff] [blame] | 19 | #include "llvm/Object/MachOUniversal.h" |
Michael J. Spencer | a51d7d9 | 2011-09-27 19:36:55 +0000 | [diff] [blame] | 20 | #include "llvm/Object/ObjectFile.h" |
Eric Beckmann | 836dd8e | 2017-05-20 01:49:19 +0000 | [diff] [blame] | 21 | #include "llvm/Object/WindowsResource.h" |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Error.h" |
| 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/ErrorOr.h" |
| 25 | #include "llvm/Support/FileSystem.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
| 27 | #include <algorithm> |
| 28 | #include <memory> |
| 29 | #include <system_error> |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 30 | |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | using namespace object; |
| 33 | |
Eugene Zelenko | 86bfc78 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 34 | Binary::~Binary() = default; |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 35 | |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 36 | Binary::Binary(unsigned int Type, MemoryBufferRef Source) |
| 37 | : TypeID(Type), Data(Source) {} |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 38 | |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 39 | StringRef Binary::getData() const { return Data.getBuffer(); } |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 40 | |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 41 | StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); } |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 42 | |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 43 | MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; } |
| 44 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 45 | Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer, |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 46 | LLVMContext *Context) { |
Zachary Turner | 19ca2b0 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 47 | file_magic Type = identify_magic(Buffer.getBuffer()); |
Rafael Espindola | 2edc3a6 | 2014-01-22 16:04:52 +0000 | [diff] [blame] | 48 | |
| 49 | switch (Type) { |
Zachary Turner | 19ca2b0 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 50 | case file_magic::archive: |
| 51 | return Archive::create(Buffer); |
| 52 | case file_magic::elf: |
| 53 | case file_magic::elf_relocatable: |
| 54 | case file_magic::elf_executable: |
| 55 | case file_magic::elf_shared_object: |
| 56 | case file_magic::elf_core: |
| 57 | case file_magic::macho_object: |
| 58 | case file_magic::macho_executable: |
| 59 | case file_magic::macho_fixed_virtual_memory_shared_lib: |
| 60 | case file_magic::macho_core: |
| 61 | case file_magic::macho_preload_executable: |
| 62 | case file_magic::macho_dynamically_linked_shared_lib: |
| 63 | case file_magic::macho_dynamic_linker: |
| 64 | case file_magic::macho_bundle: |
| 65 | case file_magic::macho_dynamically_linked_shared_lib_stub: |
| 66 | case file_magic::macho_dsym_companion: |
| 67 | case file_magic::macho_kext_bundle: |
| 68 | case file_magic::coff_object: |
| 69 | case file_magic::coff_import_library: |
| 70 | case file_magic::pecoff_executable: |
| 71 | case file_magic::bitcode: |
| 72 | case file_magic::wasm_object: |
| 73 | return ObjectFile::createSymbolicFile(Buffer, Type, Context); |
| 74 | case file_magic::macho_universal_binary: |
| 75 | return MachOUniversalBinary::create(Buffer); |
| 76 | case file_magic::windows_resource: |
| 77 | return WindowsResource::createWindowsResource(Buffer); |
Zachary Turner | a896ada | 2018-03-07 18:58:33 +0000 | [diff] [blame] | 78 | case file_magic::pdb: |
| 79 | // PDB does not support the Binary interface. |
| 80 | return errorCodeToError(object_error::invalid_file_type); |
Zachary Turner | 19ca2b0 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 81 | case file_magic::unknown: |
| 82 | case file_magic::coff_cl_gl_object: |
| 83 | // Unrecognized object file format. |
| 84 | return errorCodeToError(object_error::invalid_file_type); |
Michael J. Spencer | 001c920 | 2011-06-25 17:54:50 +0000 | [diff] [blame] | 85 | } |
Alexey Samsonov | f49d8fc | 2013-06-28 09:44:05 +0000 | [diff] [blame] | 86 | llvm_unreachable("Unexpected Binary File Type"); |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 89 | Expected<OwningBinary<Binary>> object::createBinary(StringRef Path) { |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 90 | ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = |
David Major | af085b4 | 2019-01-09 23:36:32 +0000 | [diff] [blame] | 91 | MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1, |
| 92 | /*RequiresNullTerminator=*/false); |
Rafael Espindola | 7cba2a9 | 2014-07-06 17:43:13 +0000 | [diff] [blame] | 93 | if (std::error_code EC = FileOrErr.getError()) |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 94 | return errorCodeToError(EC); |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 95 | std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); |
| 96 | |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 97 | Expected<std::unique_ptr<Binary>> BinOrErr = |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 98 | createBinary(Buffer->getMemBufferRef()); |
Kevin Enderby | c6bf9be | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 99 | if (!BinOrErr) |
| 100 | return BinOrErr.takeError(); |
Rafael Espindola | 548f2b6 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 101 | std::unique_ptr<Binary> &Bin = BinOrErr.get(); |
| 102 | |
| 103 | return OwningBinary<Binary>(std::move(Bin), std::move(Buffer)); |
Michael J. Spencer | c44c915 | 2011-06-25 17:54:29 +0000 | [diff] [blame] | 104 | } |