Home | History | Annotate | Download | only in Object
      1 //===- ObjectFile.cpp - File format independent object file -----*- 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 ObjectFile class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/Object/ObjectFile.h"
     15 #include "llvm/ADT/OwningPtr.h"
     16 #include "llvm/Support/ErrorHandling.h"
     17 #include "llvm/Support/MemoryBuffer.h"
     18 #include "llvm/Support/Path.h"
     19 #include "llvm/Support/system_error.h"
     20 
     21 using namespace llvm;
     22 using namespace object;
     23 
     24 void ObjectFile::anchor() { }
     25 
     26 ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec)
     27   : Binary(Type, source) {
     28 }
     29 
     30 ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
     31   if (!Object || Object->getBufferSize() < 64)
     32     return 0;
     33   sys::LLVMFileType type = sys::IdentifyFileType(Object->getBufferStart(),
     34                                 static_cast<unsigned>(Object->getBufferSize()));
     35   switch (type) {
     36     case sys::Unknown_FileType:
     37       return 0;
     38     case sys::ELF_Relocatable_FileType:
     39     case sys::ELF_Executable_FileType:
     40     case sys::ELF_SharedObject_FileType:
     41     case sys::ELF_Core_FileType:
     42       return createELFObjectFile(Object);
     43     case sys::Mach_O_Object_FileType:
     44     case sys::Mach_O_Executable_FileType:
     45     case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
     46     case sys::Mach_O_Core_FileType:
     47     case sys::Mach_O_PreloadExecutable_FileType:
     48     case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
     49     case sys::Mach_O_DynamicLinker_FileType:
     50     case sys::Mach_O_Bundle_FileType:
     51     case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
     52     case sys::Mach_O_DSYMCompanion_FileType:
     53       return createMachOObjectFile(Object);
     54     case sys::COFF_FileType:
     55       return createCOFFObjectFile(Object);
     56     default:
     57       llvm_unreachable("Unexpected Object File Type");
     58   }
     59 }
     60 
     61 ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
     62   OwningPtr<MemoryBuffer> File;
     63   if (MemoryBuffer::getFile(ObjectPath, File))
     64     return NULL;
     65   return createObjectFile(File.take());
     66 }
     67