blob: 854e68e40f4d663c7d2e8c85e556b15d48e8e980 [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
14#include "llvm/Object/IRObjectFile.h"
15#include "llvm/Object/ObjectFile.h"
16#include "llvm/Object/SymbolicFile.h"
17#include "llvm/Support/MemoryBuffer.h"
18
19using namespace llvm;
20using namespace object;
21
Rafael Espindola48af1c22014-08-19 18:44:46 +000022SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
23 : Binary(Type, Source) {}
Rafael Espindolaf12b8282014-02-21 20:10:59 +000024
25SymbolicFile::~SymbolicFile() {}
26
Rafael Espindola48af1c22014-08-19 18:44:46 +000027ErrorOr<std::unique_ptr<SymbolicFile>> SymbolicFile::createSymbolicFile(
28 MemoryBufferRef Object, sys::fs::file_magic Type, LLVMContext *Context) {
29 StringRef Data = Object.getBuffer();
Rafael Espindolaf12b8282014-02-21 20:10:59 +000030 if (Type == sys::fs::file_magic::unknown)
Rafael Espindola48af1c22014-08-19 18:44:46 +000031 Type = sys::fs::identify_magic(Data);
Rafael Espindolaf12b8282014-02-21 20:10:59 +000032
33 switch (Type) {
34 case sys::fs::file_magic::bitcode:
35 if (Context)
Rafael Espindola5dec7ea2014-12-09 20:36:13 +000036 return IRObjectFile::create(Object, *Context);
Rafael Espindolaf12b8282014-02-21 20:10:59 +000037 // Fallthrough
38 case sys::fs::file_magic::unknown:
39 case sys::fs::file_magic::archive:
40 case sys::fs::file_magic::macho_universal_binary:
41 case sys::fs::file_magic::windows_resource:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000042 return object_error::invalid_file_type;
Michael J. Spencerbbd875b2014-11-18 01:14:25 +000043 case sys::fs::file_magic::elf:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000044 case sys::fs::file_magic::elf_executable:
45 case sys::fs::file_magic::elf_shared_object:
46 case sys::fs::file_magic::elf_core:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000047 case sys::fs::file_magic::macho_executable:
48 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
49 case sys::fs::file_magic::macho_core:
50 case sys::fs::file_magic::macho_preload_executable:
51 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
52 case sys::fs::file_magic::macho_dynamic_linker:
53 case sys::fs::file_magic::macho_bundle:
54 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
55 case sys::fs::file_magic::macho_dsym_companion:
Justin Bognera7ad4b32015-02-25 22:59:20 +000056 case sys::fs::file_magic::macho_kext_bundle:
Rafael Espindolaf12b8282014-02-21 20:10:59 +000057 case sys::fs::file_magic::coff_import_library:
58 case sys::fs::file_magic::pecoff_executable:
Rafael Espindolac3f9b5a2014-06-23 21:53:12 +000059 return ObjectFile::createObjectFile(Object, Type);
Peter Collingbourne10039c02014-09-18 21:28:49 +000060 case sys::fs::file_magic::elf_relocatable:
61 case sys::fs::file_magic::macho_object:
62 case sys::fs::file_magic::coff_object: {
63 ErrorOr<std::unique_ptr<ObjectFile>> Obj =
64 ObjectFile::createObjectFile(Object, Type);
65 if (!Obj || !Context)
66 return std::move(Obj);
67
68 ErrorOr<MemoryBufferRef> BCData =
69 IRObjectFile::findBitcodeInObject(*Obj->get());
70 if (!BCData)
71 return std::move(Obj);
72
Rafael Espindola5dec7ea2014-12-09 20:36:13 +000073 return IRObjectFile::create(
Peter Collingbourne10039c02014-09-18 21:28:49 +000074 MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
75 *Context);
76 }
Rafael Espindolaf12b8282014-02-21 20:10:59 +000077 }
78 llvm_unreachable("Unexpected Binary File Type");
79}