2017-04-20 01:02:10 +02:00
|
|
|
//===- SymbolicFile.cpp - Interface that only provides symbols ------------===//
|
2014-02-21 21:10:59 +01:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines a file format independent SymbolicFile class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/Object/SymbolicFile.h"
|
2017-04-20 01:02:10 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-07 05:48:56 +02:00
|
|
|
#include "llvm/BinaryFormat/Magic.h"
|
2015-08-28 09:40:30 +02:00
|
|
|
#include "llvm/Object/COFFImportFile.h"
|
2017-04-20 01:02:10 +02:00
|
|
|
#include "llvm/Object/Error.h"
|
2014-02-21 21:10:59 +01:00
|
|
|
#include "llvm/Object/IRObjectFile.h"
|
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2017-04-20 01:02:10 +02:00
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
|
|
|
#include "llvm/Support/FileSystem.h"
|
2014-02-21 21:10:59 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2017-04-20 01:02:10 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2014-02-21 21:10:59 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace object;
|
|
|
|
|
2014-08-19 20:44:46 +02:00
|
|
|
SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)
|
|
|
|
: Binary(Type, Source) {}
|
2014-02-21 21:10:59 +01:00
|
|
|
|
2017-04-20 01:02:10 +02:00
|
|
|
SymbolicFile::~SymbolicFile() = default;
|
2014-02-21 21:10:59 +01:00
|
|
|
|
2017-06-07 05:48:56 +02:00
|
|
|
Expected<std::unique_ptr<SymbolicFile>>
|
|
|
|
SymbolicFile::createSymbolicFile(MemoryBufferRef Object, file_magic Type,
|
|
|
|
LLVMContext *Context) {
|
2014-08-19 20:44:46 +02:00
|
|
|
StringRef Data = Object.getBuffer();
|
2017-06-07 05:48:56 +02:00
|
|
|
if (Type == file_magic::unknown)
|
|
|
|
Type = identify_magic(Data);
|
2014-02-21 21:10:59 +01:00
|
|
|
|
|
|
|
switch (Type) {
|
2017-06-07 05:48:56 +02:00
|
|
|
case file_magic::bitcode:
|
2014-02-21 21:10:59 +01:00
|
|
|
if (Context)
|
2016-11-13 08:00:17 +01:00
|
|
|
return IRObjectFile::create(Object, *Context);
|
2016-08-17 07:10:15 +02:00
|
|
|
LLVM_FALLTHROUGH;
|
2017-06-07 05:48:56 +02:00
|
|
|
case file_magic::unknown:
|
|
|
|
case file_magic::archive:
|
|
|
|
case file_magic::coff_cl_gl_object:
|
|
|
|
case file_magic::macho_universal_binary:
|
|
|
|
case file_magic::windows_resource:
|
2016-04-07 00:14:09 +02:00
|
|
|
return errorCodeToError(object_error::invalid_file_type);
|
2017-06-07 05:48:56 +02:00
|
|
|
case file_magic::elf:
|
|
|
|
case file_magic::elf_executable:
|
|
|
|
case file_magic::elf_shared_object:
|
|
|
|
case file_magic::elf_core:
|
|
|
|
case file_magic::macho_executable:
|
|
|
|
case file_magic::macho_fixed_virtual_memory_shared_lib:
|
|
|
|
case file_magic::macho_core:
|
|
|
|
case file_magic::macho_preload_executable:
|
|
|
|
case file_magic::macho_dynamically_linked_shared_lib:
|
|
|
|
case file_magic::macho_dynamic_linker:
|
|
|
|
case file_magic::macho_bundle:
|
|
|
|
case file_magic::macho_dynamically_linked_shared_lib_stub:
|
|
|
|
case file_magic::macho_dsym_companion:
|
|
|
|
case file_magic::macho_kext_bundle:
|
|
|
|
case file_magic::pecoff_executable:
|
|
|
|
case file_magic::wasm_object:
|
2014-06-23 23:53:12 +02:00
|
|
|
return ObjectFile::createObjectFile(Object, Type);
|
2017-06-07 05:48:56 +02:00
|
|
|
case file_magic::coff_import_library:
|
2015-08-28 09:40:30 +02:00
|
|
|
return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));
|
2017-06-07 05:48:56 +02:00
|
|
|
case file_magic::elf_relocatable:
|
|
|
|
case file_magic::macho_object:
|
|
|
|
case file_magic::coff_object: {
|
2016-04-07 00:14:09 +02:00
|
|
|
Expected<std::unique_ptr<ObjectFile>> Obj =
|
2014-09-18 23:28:49 +02:00
|
|
|
ObjectFile::createObjectFile(Object, Type);
|
|
|
|
if (!Obj || !Context)
|
|
|
|
return std::move(Obj);
|
|
|
|
|
|
|
|
ErrorOr<MemoryBufferRef> BCData =
|
|
|
|
IRObjectFile::findBitcodeInObject(*Obj->get());
|
|
|
|
if (!BCData)
|
|
|
|
return std::move(Obj);
|
|
|
|
|
2016-11-13 08:00:17 +01:00
|
|
|
return IRObjectFile::create(
|
|
|
|
MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),
|
|
|
|
*Context);
|
2014-09-18 23:28:49 +02:00
|
|
|
}
|
2014-02-21 21:10:59 +01:00
|
|
|
}
|
|
|
|
llvm_unreachable("Unexpected Binary File Type");
|
|
|
|
}
|