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 ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec) 25 : Binary(Type, source) { 26 } 27 28 ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) { 29 if (!Object || Object->getBufferSize() < 64) 30 return 0; 31 sys::LLVMFileType type = sys::IdentifyFileType(Object->getBufferStart(), 32 static_cast<unsigned>(Object->getBufferSize())); 33 switch (type) { 34 case sys::ELF_Relocatable_FileType: 35 case sys::ELF_Executable_FileType: 36 case sys::ELF_SharedObject_FileType: 37 case sys::ELF_Core_FileType: 38 return createELFObjectFile(Object); 39 case sys::Mach_O_Object_FileType: 40 case sys::Mach_O_Executable_FileType: 41 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: 42 case sys::Mach_O_Core_FileType: 43 case sys::Mach_O_PreloadExecutable_FileType: 44 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: 45 case sys::Mach_O_DynamicLinker_FileType: 46 case sys::Mach_O_Bundle_FileType: 47 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: 48 return createMachOObjectFile(Object); 49 case sys::COFF_FileType: 50 return createCOFFObjectFile(Object); 51 default: 52 llvm_unreachable("Unknown Object File Type"); 53 } 54 } 55 56 ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) { 57 OwningPtr<MemoryBuffer> File; 58 if (error_code ec = MemoryBuffer::getFile(ObjectPath, File)) 59 return NULL; 60 return createObjectFile(File.take()); 61 } 62