Eugene Zelenko | d341c93 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 1 | //===- SymbolicFile.cpp - Interface that only provides symbols ------------===// |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines a file format independent SymbolicFile class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 13 | #include "llvm/Object/SymbolicFile.h" |
Eugene Zelenko | d341c93 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 15 | #include "llvm/BinaryFormat/Magic.h" |
Rui Ueyama | 71ba9bd | 2015-08-28 07:40:30 +0000 | [diff] [blame] | 16 | #include "llvm/Object/COFFImportFile.h" |
Eugene Zelenko | d341c93 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 17 | #include "llvm/Object/Error.h" |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 18 | #include "llvm/Object/IRObjectFile.h" |
| 19 | #include "llvm/Object/ObjectFile.h" |
Eugene Zelenko | d341c93 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 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" |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Eugene Zelenko | d341c93 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 26 | #include <algorithm> |
| 27 | #include <memory> |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | using namespace object; |
| 31 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 32 | SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source) |
| 33 | : Binary(Type, Source) {} |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 34 | |
Eugene Zelenko | d341c93 | 2017-04-19 23:02:10 +0000 | [diff] [blame] | 35 | SymbolicFile::~SymbolicFile() = default; |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 36 | |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 37 | Expected<std::unique_ptr<SymbolicFile>> |
| 38 | SymbolicFile::createSymbolicFile(MemoryBufferRef Object, file_magic Type, |
| 39 | LLVMContext *Context) { |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 40 | StringRef Data = Object.getBuffer(); |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 41 | if (Type == file_magic::unknown) |
| 42 | Type = identify_magic(Data); |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 43 | |
| 44 | switch (Type) { |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 45 | case file_magic::bitcode: |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 46 | if (Context) |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 47 | return IRObjectFile::create(Object, *Context); |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 48 | LLVM_FALLTHROUGH; |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 49 | case file_magic::unknown: |
| 50 | case file_magic::archive: |
| 51 | case file_magic::coff_cl_gl_object: |
| 52 | case file_magic::macho_universal_binary: |
| 53 | case file_magic::windows_resource: |
Zachary Turner | aac28f3 | 2018-03-07 18:58:33 +0000 | [diff] [blame] | 54 | case file_magic::pdb: |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 55 | return errorCodeToError(object_error::invalid_file_type); |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 56 | case file_magic::elf: |
| 57 | case file_magic::elf_executable: |
| 58 | case file_magic::elf_shared_object: |
| 59 | case file_magic::elf_core: |
| 60 | case file_magic::macho_executable: |
| 61 | case file_magic::macho_fixed_virtual_memory_shared_lib: |
| 62 | case file_magic::macho_core: |
| 63 | case file_magic::macho_preload_executable: |
| 64 | case file_magic::macho_dynamically_linked_shared_lib: |
| 65 | case file_magic::macho_dynamic_linker: |
| 66 | case file_magic::macho_bundle: |
| 67 | case file_magic::macho_dynamically_linked_shared_lib_stub: |
| 68 | case file_magic::macho_dsym_companion: |
| 69 | case file_magic::macho_kext_bundle: |
| 70 | case file_magic::pecoff_executable: |
| 71 | case file_magic::wasm_object: |
Rafael Espindola | c3f9b5a | 2014-06-23 21:53:12 +0000 | [diff] [blame] | 72 | return ObjectFile::createObjectFile(Object, Type); |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 73 | case file_magic::coff_import_library: |
Rui Ueyama | 71ba9bd | 2015-08-28 07:40:30 +0000 | [diff] [blame] | 74 | return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object)); |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 75 | case file_magic::elf_relocatable: |
| 76 | case file_magic::macho_object: |
| 77 | case file_magic::coff_object: { |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 78 | Expected<std::unique_ptr<ObjectFile>> Obj = |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 79 | ObjectFile::createObjectFile(Object, Type); |
| 80 | if (!Obj || !Context) |
| 81 | return std::move(Obj); |
| 82 | |
Rafael Espindola | 3500f5e | 2017-10-11 18:07:18 +0000 | [diff] [blame] | 83 | Expected<MemoryBufferRef> BCData = |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 84 | IRObjectFile::findBitcodeInObject(*Obj->get()); |
Rafael Espindola | 3500f5e | 2017-10-11 18:07:18 +0000 | [diff] [blame] | 85 | if (!BCData) { |
| 86 | consumeError(BCData.takeError()); |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 87 | return std::move(Obj); |
Rafael Espindola | 3500f5e | 2017-10-11 18:07:18 +0000 | [diff] [blame] | 88 | } |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 89 | |
Peter Collingbourne | d9445c4 | 2016-11-13 07:00:17 +0000 | [diff] [blame] | 90 | return IRObjectFile::create( |
| 91 | MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()), |
| 92 | *Context); |
Peter Collingbourne | 10039c0 | 2014-09-18 21:28:49 +0000 | [diff] [blame] | 93 | } |
Rafael Espindola | f12b828 | 2014-02-21 20:10:59 +0000 | [diff] [blame] | 94 | } |
| 95 | llvm_unreachable("Unexpected Binary File Type"); |
| 96 | } |