1 //===- SymbolicFile.cpp - Interface that only provides symbols ------------===// 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 a file format independent SymbolicFile class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/Object/COFFImportFile.h" 16 #include "llvm/Object/Error.h" 17 #include "llvm/Object/IRObjectFile.h" 18 #include "llvm/Object/ObjectFile.h" 19 #include "llvm/Object/SymbolicFile.h" 20 #include "llvm/Support/Compiler.h" 21 #include "llvm/Support/Error.h" 22 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/ErrorOr.h" 24 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/MemoryBuffer.h" 26 #include <algorithm> 27 #include <memory> 28 29 using namespace llvm; 30 using namespace object; 31 32 SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source) 33 : Binary(Type, Source) {} 34 35 SymbolicFile::~SymbolicFile() = default; 36 37 Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile( 38 MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) { 39 StringRef Data = Object.getBuffer(); 40 if (Type == sys::fs::file_magic::unknown) 41 Type = sys::fs::identify_magic(Data); 42 43 switch (Type) { 44 case sys::fs::file_magic::bitcode: 45 if (Context) 46 return IRObjectFile::create(Object, *Context); 47 LLVM_FALLTHROUGH; 48 case sys::fs::file_magic::unknown: 49 case sys::fs::file_magic::archive: 50 case sys::fs::file_magic::coff_cl_gl_object: 51 case sys::fs::file_magic::macho_universal_binary: 52 case sys::fs::file_magic::windows_resource: 53 return errorCodeToError(object_error::invalid_file_type); 54 case sys::fs::file_magic::elf: 55 case sys::fs::file_magic::elf_executable: 56 case sys::fs::file_magic::elf_shared_object: 57 case sys::fs::file_magic::elf_core: 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::macho_kext_bundle: 68 case sys::fs::file_magic::pecoff_executable: 69 case sys::fs::file_magic::wasm_object: 70 return ObjectFile::createObjectFile(Object, Type); 71 case sys::fs::file_magic::coff_import_library: 72 return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object)); 73 case sys::fs::file_magic::elf_relocatable: 74 case sys::fs::file_magic::macho_object: 75 case sys::fs::file_magic::coff_object: { 76 Expected<std::unique_ptr<ObjectFile>> Obj = 77 ObjectFile::createObjectFile(Object, Type); 78 if (!Obj || !Context) 79 return std::move(Obj); 80 81 ErrorOr<MemoryBufferRef> BCData = 82 IRObjectFile::findBitcodeInObject(*Obj->get()); 83 if (!BCData) 84 return std::move(Obj); 85 86 return IRObjectFile::create( 87 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()), 88 *Context); 89 } 90 } 91 llvm_unreachable("Unexpected Binary File Type"); 92 } 93