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