1 //===- Binary.cpp - A generic binary file ---------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines the Binary class. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Object/Binary.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/BinaryFormat/Magic.h" 16 #include "llvm/Object/Archive.h" 17 #include "llvm/Object/Error.h" 18 #include "llvm/Object/MachOUniversal.h" 19 #include "llvm/Object/Minidump.h" 20 #include "llvm/Object/ObjectFile.h" 21 #include "llvm/Object/TapiUniversal.h" 22 #include "llvm/Object/WindowsResource.h" 23 #include "llvm/Support/Error.h" 24 #include "llvm/Support/ErrorHandling.h" 25 #include "llvm/Support/ErrorOr.h" 26 #include "llvm/Support/MemoryBuffer.h" 27 #include <memory> 28 #include <system_error> 29 30 using namespace llvm; 31 using namespace object; 32 33 Binary::~Binary() = default; 34 35 Binary::Binary(unsigned int Type, MemoryBufferRef Source) 36 : TypeID(Type), Data(Source) {} 37 38 StringRef Binary::getData() const { return Data.getBuffer(); } 39 40 StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); } 41 42 MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; } 43 44 Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer, 45 LLVMContext *Context, 46 bool InitContent) { 47 file_magic Type = identify_magic(Buffer.getBuffer()); 48 49 switch (Type) { 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::goff_object: 58 case file_magic::macho_object: 59 case file_magic::macho_executable: 60 case file_magic::macho_fixed_virtual_memory_shared_lib: 61 case file_magic::macho_core: 62 case file_magic::macho_preload_executable: 63 case file_magic::macho_dynamically_linked_shared_lib: 64 case file_magic::macho_dynamic_linker: 65 case file_magic::macho_bundle: 66 case file_magic::macho_dynamically_linked_shared_lib_stub: 67 case file_magic::macho_dsym_companion: 68 case file_magic::macho_kext_bundle: 69 case file_magic::coff_object: 70 case file_magic::coff_import_library: 71 case file_magic::pecoff_executable: 72 case file_magic::bitcode: 73 case file_magic::xcoff_object_32: 74 case file_magic::xcoff_object_64: 75 case file_magic::wasm_object: 76 return ObjectFile::createSymbolicFile(Buffer, Type, Context, InitContent); 77 case file_magic::macho_universal_binary: 78 return MachOUniversalBinary::create(Buffer); 79 case file_magic::windows_resource: 80 return WindowsResource::createWindowsResource(Buffer); 81 case file_magic::pdb: 82 // PDB does not support the Binary interface. 83 return errorCodeToError(object_error::invalid_file_type); 84 case file_magic::unknown: 85 case file_magic::cuda_fatbinary: 86 case file_magic::coff_cl_gl_object: 87 case file_magic::dxcontainer_object: 88 // Unrecognized object file format. 89 return errorCodeToError(object_error::invalid_file_type); 90 case file_magic::minidump: 91 return MinidumpFile::create(Buffer); 92 case file_magic::tapi_file: 93 return TapiUniversal::create(Buffer); 94 } 95 llvm_unreachable("Unexpected Binary File Type"); 96 } 97 98 Expected<OwningBinary<Binary>> 99 object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) { 100 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 101 MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false, 102 /*RequiresNullTerminator=*/false); 103 if (std::error_code EC = FileOrErr.getError()) 104 return errorCodeToError(EC); 105 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); 106 107 Expected<std::unique_ptr<Binary>> BinOrErr = 108 createBinary(Buffer->getMemBufferRef(), Context, InitContent); 109 if (!BinOrErr) 110 return BinOrErr.takeError(); 111 std::unique_ptr<Binary> &Bin = BinOrErr.get(); 112 113 return OwningBinary<Binary>(std::move(Bin), std::move(Buffer)); 114 } 115