Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 1 | //===- SymbolicFile.cpp - Interface that only provides symbols --*- 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 a file format independent SymbolicFile class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Rui Ueyama | 71ba9bd | 2015-08-28 07:40:30 +0000 | [diff] [blame] | 14 | #include "llvm/Object/COFF.h" |
| 15 | #include "llvm/Object/COFFImportFile.h" |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 16 | #include "llvm/Object/IRObjectFile.h" |
| 17 | #include "llvm/Object/ObjectFile.h" |
| 18 | #include "llvm/Object/SymbolicFile.h" |
| 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace object; |
| 23 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 24 | SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source) |
| 25 | : Binary(Type, Source) {} |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 26 | |
| 27 | SymbolicFile::~SymbolicFile() {} |
| 28 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 29 | Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile( |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 30 | MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) { |
| 31 | StringRef Data = Object.getBuffer(); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 32 | if (Type == sys::fs::file_magic::unknown) |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 33 | Type = sys::fs::identify_magic(Data); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 34 | |
| 35 | switch (Type) { |
| 36 | case sys::fs::file_magic::bitcode: |
| 37 | if (Context) |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 38 | return errorOrToExpected(IRObjectFile::create(Object, *Context)); |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 39 | LLVM_FALLTHROUGH; |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 40 | case sys::fs::file_magic::unknown: |
| 41 | case sys::fs::file_magic::archive: |
| 42 | case sys::fs::file_magic::macho_universal_binary: |
| 43 | case sys::fs::file_magic::windows_resource: |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 44 | return errorCodeToError(object_error::invalid_file_type); |
Michael J. Spencer | bbd875b | 2014-11-18 01:14:25 +0000 | [diff] [blame] | 45 | case sys::fs::file_magic::elf: |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 46 | case sys::fs::file_magic::elf_executable: |
| 47 | case sys::fs::file_magic::elf_shared_object: |
| 48 | case sys::fs::file_magic::elf_core: |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 49 | case sys::fs::file_magic::macho_executable: |
| 50 | case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: |
| 51 | case sys::fs::file_magic::macho_core: |
| 52 | case sys::fs::file_magic::macho_preload_executable: |
| 53 | case sys::fs::file_magic::macho_dynamically_linked_shared_lib: |
| 54 | case sys::fs::file_magic::macho_dynamic_linker: |
| 55 | case sys::fs::file_magic::macho_bundle: |
| 56 | case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: |
| 57 | case sys::fs::file_magic::macho_dsym_companion: |
Justin Bogner | a7ad4b3 | 2015-02-25 22:59:20 +0000 | [diff] [blame] | 58 | case sys::fs::file_magic::macho_kext_bundle: |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 59 | case sys::fs::file_magic::pecoff_executable: |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 60 | return ObjectFile::createObjectFile(Object, Type); |
Rui Ueyama | 71ba9bd | 2015-08-28 07:40:30 +0000 | [diff] [blame] | 61 | case sys::fs::file_magic::coff_import_library: |
| 62 | return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object)); |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 63 | case sys::fs::file_magic::elf_relocatable: |
| 64 | case sys::fs::file_magic::macho_object: |
| 65 | case sys::fs::file_magic::coff_object: { |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 66 | Expected<std::unique_ptr<ObjectFile>> Obj = |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 67 | ObjectFile::createObjectFile(Object, Type); |
| 68 | if (!Obj || !Context) |
| 69 | return std::move(Obj); |
| 70 | |
| 71 | ErrorOr<MemoryBufferRef> BCData = |
| 72 | IRObjectFile::findBitcodeInObject(*Obj->get()); |
| 73 | if (!BCData) |
| 74 | return std::move(Obj); |
| 75 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 76 | return errorOrToExpected(IRObjectFile::create( |
| 77 | MemoryBufferRef(BCData->getBuffer(), |
| 78 | Object.getBufferIdentifier()), *Context)); |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 79 | } |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 80 | } |
| 81 | llvm_unreachable("Unexpected Binary File Type"); |
| 82 | } |