brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.4 KiB · 47295e6 Raw
133 lines · cpp
1//===- SymbolicFile.cpp - Interface that only provides symbols ------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines a file format independent SymbolicFile class.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/Object/SymbolicFile.h"14#include "llvm/ADT/StringRef.h"15#include "llvm/BinaryFormat/Magic.h"16#include "llvm/Object/COFFImportFile.h"17#include "llvm/Object/Error.h"18#include "llvm/Object/IRObjectFile.h"19#include "llvm/Object/ObjectFile.h"20#include "llvm/Support/Error.h"21#include "llvm/Support/ErrorHandling.h"22#include <memory>23 24using namespace llvm;25using namespace object;26 27namespace llvm {28class LLVMContext;29}30 31SymbolicFile::SymbolicFile(unsigned int Type, MemoryBufferRef Source)32    : Binary(Type, Source) {}33 34SymbolicFile::~SymbolicFile() = default;35 36Expected<std::unique_ptr<SymbolicFile>>37SymbolicFile::createSymbolicFile(MemoryBufferRef Object, file_magic Type,38                                 LLVMContext *Context, bool InitContent) {39  StringRef Data = Object.getBuffer();40  if (Type == file_magic::unknown)41    Type = identify_magic(Data);42 43  if (!isSymbolicFile(Type, Context))44    return errorCodeToError(object_error::invalid_file_type);45 46  switch (Type) {47  case file_magic::bitcode:48    // Context is guaranteed to be non-null here, because bitcode magic only49    // indicates a symbolic file when Context is non-null.50    return IRObjectFile::create(Object, *Context);51  case file_magic::elf:52  case file_magic::elf_executable:53  case file_magic::elf_shared_object:54  case file_magic::elf_core:55  case file_magic::goff_object:56  case file_magic::macho_executable:57  case file_magic::macho_fixed_virtual_memory_shared_lib:58  case file_magic::macho_core:59  case file_magic::macho_preload_executable:60  case file_magic::macho_dynamically_linked_shared_lib:61  case file_magic::macho_dynamic_linker:62  case file_magic::macho_bundle:63  case file_magic::macho_dynamically_linked_shared_lib_stub:64  case file_magic::macho_dsym_companion:65  case file_magic::macho_kext_bundle:66  case file_magic::macho_file_set:67  case file_magic::pecoff_executable:68  case file_magic::xcoff_object_32:69  case file_magic::xcoff_object_64:70  case file_magic::wasm_object:71  case file_magic::dxcontainer_object:72    return ObjectFile::createObjectFile(Object, Type, InitContent);73  case file_magic::coff_import_library:74    return std::unique_ptr<SymbolicFile>(new COFFImportFile(Object));75  case file_magic::elf_relocatable:76  case file_magic::macho_object:77  case file_magic::coff_object: {78    Expected<std::unique_ptr<ObjectFile>> Obj =79        ObjectFile::createObjectFile(Object, Type, InitContent);80    if (!Obj || !Context)81      return std::move(Obj);82 83    Expected<MemoryBufferRef> BCData =84        IRObjectFile::findBitcodeInObject(*Obj->get());85    if (!BCData) {86      consumeError(BCData.takeError());87      return std::move(Obj);88    }89 90    return IRObjectFile::create(91        MemoryBufferRef(BCData->getBuffer(), Object.getBufferIdentifier()),92        *Context);93  }94  default:95    llvm_unreachable("Unexpected Binary File Type");96  }97}98 99bool SymbolicFile::isSymbolicFile(file_magic Type, const LLVMContext *Context) {100  switch (Type) {101  case file_magic::bitcode:102    return Context != nullptr;103  case file_magic::elf:104  case file_magic::elf_executable:105  case file_magic::elf_shared_object:106  case file_magic::elf_core:107  case file_magic::goff_object:108  case file_magic::macho_executable:109  case file_magic::macho_fixed_virtual_memory_shared_lib:110  case file_magic::macho_core:111  case file_magic::macho_preload_executable:112  case file_magic::macho_dynamically_linked_shared_lib:113  case file_magic::macho_dynamic_linker:114  case file_magic::macho_bundle:115  case file_magic::macho_dynamically_linked_shared_lib_stub:116  case file_magic::macho_dsym_companion:117  case file_magic::macho_kext_bundle:118  case file_magic::macho_file_set:119  case file_magic::pecoff_executable:120  case file_magic::xcoff_object_32:121  case file_magic::xcoff_object_64:122  case file_magic::wasm_object:123  case file_magic::coff_import_library:124  case file_magic::elf_relocatable:125  case file_magic::macho_object:126  case file_magic::coff_object:127  case file_magic::dxcontainer_object:128    return true;129  default:130    return false;131  }132}133