blob: 16cff5c228bdd23413c1fd2467bb3a5593271b8c [file] [log] [blame]
Eugene Zelenkod341c932017-04-19 23:02:10 +00001//===- SymbolicFile.cpp - Interface that only provides symbols ------------===//
Rafael Espindolaf12b8282014-02-21 20:10:59 +00002//
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
Eugene Zelenkod341c932017-04-19 23:02:10 +000014#include "llvm/ADT/StringRef.h"
Rui Ueyama71ba9bd2015-08-28 07:40:30 +000015#include "llvm/Object/COFFImportFile.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000016#include "llvm/Object/Error.h"
Rafael Espindolaf12b8282014-02-21 20:10:59 +000017#include "llvm/Object/IRObjectFile.h"
18#include "llvm/Object/ObjectFile.h"
19#include "llvm/Object/SymbolicFile.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000020#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 Espindolaf12b8282014-02-21 20:10:59 +000025#include "llvm/Support/MemoryBuffer.h"
Eugene Zelenkod341c932017-04-19 23:02:10 +000026#include <algorithm>
27#include <memory>
Rafael Espindolaf12b8282014-02-21 20:10:59 +000028
29using namespace llvm;
30using namespace object;
31
Rafael Espindola48af1c22014-08-19 18:44:46 +000032SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
33 : Binary(Type, Source) {}
Rafael Espindolaf12b8282014-02-21 20:10:59 +000034
Eugene Zelenkod341c932017-04-19 23:02:10 +000035SymbolicFile::~SymbolicFile() = default;
Rafael Espindolaf12b8282014-02-21 20:10:59 +000036
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +000037Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
Rafael Espindola48af1c22014-08-19 18:44:46 +000038 MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) {
39 StringRef Data = Object.getBuffer();
Rafael Espindolaf12b8282014-02-21 20:10:59 +000040 if (Type == sys::fs::file_magic::unknown)
Rafael Espindola48af1c22014-08-19 18:44:46 +000041 Type = sys::fs::identify_magic(Data);
Rafael Espindolaf12b8282014-02-21 20:10:59 +000042
43 switch (Type) {
44 case sys::fs::file_magic::bitcode:
45 if (Context)
Peter Collingbourned9445c42016-11-13 07:00:17 +000046 return IRObjectFile::create(Object, *Context);
Justin Bognerb03fd122016-08-17 05:10:15 +000047 LLVM_FALLTHROUGH;
Rafael Espindolaf12b8282014-02-21 20:10:59 +000048 case sys::fs::file_magic::unknown:
49 case sys::fs::file_magic::archive:
Rui Ueyamae97c34c2016-11-15 00:58:50 +000050 case sys::fs::file_magic::coff_cl_gl_object:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000051 case sys::fs::file_magic::macho_universal_binary:
52 case sys::fs::file_magic::windows_resource:
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +000053 return errorCodeToError(object_error::invalid_file_type);
Michael J. Spencerbbd875b2014-11-18 01:14:25 +000054 case sys::fs::file_magic::elf:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000055 case sys::fs::file_magic::elf_executable:
56 case sys::fs::file_magic::elf_shared_object:
57 case sys::fs::file_magic::elf_core:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000058 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:
Justin Bognera7ad4b32015-02-25 22:59:20 +000067 case sys::fs::file_magic::macho_kext_bundle:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000068 case sys::fs::file_magic::pecoff_executable:
Derek Schuff2c6f75d2016-11-30 16:49:11 +000069 case sys::fs::file_magic::wasm_object:
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000070 return ObjectFile::createObjectFile(Object, Type);
Rui Ueyama71ba9bd2015-08-28 07:40:30 +000071 case sys::fs::file_magic::coff_import_library:
72 return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
Peter Collingbourne10039c02014-09-18 21:28:49 +000073 case sys::fs::file_magic::elf_relocatable:
74 case sys::fs::file_magic::macho_object:
75 case sys::fs::file_magic::coff_object: {
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +000076 Expected<std::unique_ptr<ObjectFile>> Obj =
Peter Collingbourne10039c02014-09-18 21:28:49 +000077 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
Peter Collingbourned9445c42016-11-13 07:00:17 +000086 return IRObjectFile::create(
87 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
88 *Context);
Peter Collingbourne10039c02014-09-18 21:28:49 +000089 }
Rafael Espindolaf12b8282014-02-21 20:10:59 +000090 }
91 llvm_unreachable("Unexpected Binary File Type");
92}