blob: 4b51a49cf342d5184d5c8237c89d83da64ceb8cb [file] [log] [blame]
Rafael Espindolaf12b8282014-02-21 20:10:59 +00001//===- 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 Ueyama71ba9bd2015-08-28 07:40:30 +000014#include "llvm/Object/COFF.h"
15#include "llvm/Object/COFFImportFile.h"
Rafael Espindolaf12b8282014-02-21 20:10:59 +000016#include "llvm/Object/IRObjectFile.h"
17#include "llvm/Object/ObjectFile.h"
18#include "llvm/Object/SymbolicFile.h"
19#include "llvm/Support/MemoryBuffer.h"
20
21using namespace llvm;
22using namespace object;
23
Rafael Espindola48af1c22014-08-19 18:44:46 +000024SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
25 : Binary(Type, Source) {}
Rafael Espindolaf12b8282014-02-21 20:10:59 +000026
27SymbolicFile::~SymbolicFile() {}
28
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +000029Expected<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
Rafael Espindola48af1c22014-08-19 18:44:46 +000030 MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) {
31 StringRef Data = Object.getBuffer();
Rafael Espindolaf12b8282014-02-21 20:10:59 +000032 if (Type == sys::fs::file_magic::unknown)
Rafael Espindola48af1c22014-08-19 18:44:46 +000033 Type = sys::fs::identify_magic(Data);
Rafael Espindolaf12b8282014-02-21 20:10:59 +000034
35 switch (Type) {
36 case sys::fs::file_magic::bitcode:
37 if (Context)
Peter Collingbourned9445c42016-11-13 07:00:17 +000038 return IRObjectFile::create(Object, *Context);
Justin Bognerb03fd122016-08-17 05:10:15 +000039 LLVM_FALLTHROUGH;
Rafael Espindolaf12b8282014-02-21 20:10:59 +000040 case sys::fs::file_magic::unknown:
41 case sys::fs::file_magic::archive:
Rui Ueyamae97c34c2016-11-15 00:58:50 +000042 case sys::fs::file_magic::coff_cl_gl_object:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000043 case sys::fs::file_magic::macho_universal_binary:
44 case sys::fs::file_magic::windows_resource:
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +000045 return errorCodeToError(object_error::invalid_file_type);
Michael J. Spencerbbd875b2014-11-18 01:14:25 +000046 case sys::fs::file_magic::elf:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000047 case sys::fs::file_magic::elf_executable:
48 case sys::fs::file_magic::elf_shared_object:
49 case sys::fs::file_magic::elf_core:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000050 case sys::fs::file_magic::macho_executable:
51 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
52 case sys::fs::file_magic::macho_core:
53 case sys::fs::file_magic::macho_preload_executable:
54 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
55 case sys::fs::file_magic::macho_dynamic_linker:
56 case sys::fs::file_magic::macho_bundle:
57 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
58 case sys::fs::file_magic::macho_dsym_companion:
Justin Bognera7ad4b32015-02-25 22:59:20 +000059 case sys::fs::file_magic::macho_kext_bundle:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000060 case sys::fs::file_magic::pecoff_executable:
Derek Schuff2c6f75d2016-11-30 16:49:11 +000061 case sys::fs::file_magic::wasm_object:
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000062 return ObjectFile::createObjectFile(Object, Type);
Rui Ueyama71ba9bd2015-08-28 07:40:30 +000063 case sys::fs::file_magic::coff_import_library:
64 return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
Peter Collingbourne10039c02014-09-18 21:28:49 +000065 case sys::fs::file_magic::elf_relocatable:
66 case sys::fs::file_magic::macho_object:
67 case sys::fs::file_magic::coff_object: {
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +000068 Expected<std::unique_ptr<ObjectFile>> Obj =
Peter Collingbourne10039c02014-09-18 21:28:49 +000069 ObjectFile::createObjectFile(Object, Type);
70 if (!Obj || !Context)
71 return std::move(Obj);
72
73 ErrorOr<MemoryBufferRef> BCData =
74 IRObjectFile::findBitcodeInObject(*Obj->get());
75 if (!BCData)
76 return std::move(Obj);
77
Peter Collingbourned9445c42016-11-13 07:00:17 +000078 return IRObjectFile::create(
79 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
80 *Context);
Peter Collingbourne10039c02014-09-18 21:28:49 +000081 }
Rafael Espindolaf12b8282014-02-21 20:10:59 +000082 }
83 llvm_unreachable("Unexpected Binary File Type");
84}