LLVM API Documentation

Binary.cpp
Go to the documentation of this file.
00001 //===- Binary.cpp - A generic binary file -----------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the Binary class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Object/Binary.h"
00015 #include "llvm/ADT/StringRef.h"
00016 #include "llvm/Support/FileSystem.h"
00017 #include "llvm/Support/MemoryBuffer.h"
00018 #include "llvm/Support/Path.h"
00019 
00020 // Include headers for createBinary.
00021 #include "llvm/Object/Archive.h"
00022 #include "llvm/Object/MachOUniversal.h"
00023 #include "llvm/Object/ObjectFile.h"
00024 
00025 using namespace llvm;
00026 using namespace object;
00027 
00028 Binary::~Binary() {}
00029 
00030 Binary::Binary(unsigned int Type, MemoryBufferRef Source)
00031     : TypeID(Type), Data(Source) {}
00032 
00033 StringRef Binary::getData() const { return Data.getBuffer(); }
00034 
00035 StringRef Binary::getFileName() const { return Data.getBufferIdentifier(); }
00036 
00037 MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; }
00038 
00039 ErrorOr<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer,
00040                                                       LLVMContext *Context) {
00041   sys::fs::file_magic Type = sys::fs::identify_magic(Buffer.getBuffer());
00042 
00043   switch (Type) {
00044     case sys::fs::file_magic::archive:
00045       return Archive::create(Buffer);
00046     case sys::fs::file_magic::elf_relocatable:
00047     case sys::fs::file_magic::elf_executable:
00048     case sys::fs::file_magic::elf_shared_object:
00049     case sys::fs::file_magic::elf_core:
00050     case sys::fs::file_magic::macho_object:
00051     case sys::fs::file_magic::macho_executable:
00052     case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
00053     case sys::fs::file_magic::macho_core:
00054     case sys::fs::file_magic::macho_preload_executable:
00055     case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
00056     case sys::fs::file_magic::macho_dynamic_linker:
00057     case sys::fs::file_magic::macho_bundle:
00058     case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
00059     case sys::fs::file_magic::macho_dsym_companion:
00060     case sys::fs::file_magic::coff_object:
00061     case sys::fs::file_magic::coff_import_library:
00062     case sys::fs::file_magic::pecoff_executable:
00063     case sys::fs::file_magic::bitcode:
00064       return ObjectFile::createSymbolicFile(Buffer, Type, Context);
00065     case sys::fs::file_magic::macho_universal_binary:
00066       return MachOUniversalBinary::create(Buffer);
00067     case sys::fs::file_magic::unknown:
00068     case sys::fs::file_magic::windows_resource:
00069       // Unrecognized object file format.
00070       return object_error::invalid_file_type;
00071   }
00072   llvm_unreachable("Unexpected Binary File Type");
00073 }
00074 
00075 ErrorOr<OwningBinary<Binary>> object::createBinary(StringRef Path) {
00076   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
00077       MemoryBuffer::getFileOrSTDIN(Path);
00078   if (std::error_code EC = FileOrErr.getError())
00079     return EC;
00080   std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
00081 
00082   ErrorOr<std::unique_ptr<Binary>> BinOrErr =
00083       createBinary(Buffer->getMemBufferRef());
00084   if (std::error_code EC = BinOrErr.getError())
00085     return EC;
00086   std::unique_ptr<Binary> &Bin = BinOrErr.get();
00087 
00088   return OwningBinary<Binary>(std::move(Bin), std::move(Buffer));
00089 }