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/Support/ErrorHandling.h" 16 #include "llvm/Support/MemoryBuffer.h" 17 #include "llvm/System/Path.h" 18 19 using namespace llvm; 20 using namespace object; 21 22 ObjectFile::ObjectFile(MemoryBuffer *Object) 23 : MapFile(Object) { 24 assert(MapFile && "Must be a valid MemoryBuffer!"); 25 base = reinterpret_cast<const uint8_t *>(MapFile->getBufferStart()); 26 } 27 28 ObjectFile::~ObjectFile() { 29 delete MapFile; 30 } 31 32 StringRef ObjectFile::getFilename() const { 33 return MapFile->getBufferIdentifier(); 34 } 35 36 ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) { 37 if (!Object || Object->getBufferSize() < 64) 38 return 0; 39 sys::LLVMFileType type = sys::IdentifyFileType(Object->getBufferStart(), 40 static_cast<unsigned>(Object->getBufferSize())); 41 switch (type) { 42 case sys::ELF_Relocatable_FileType: 43 case sys::ELF_Executable_FileType: 44 case sys::ELF_SharedObject_FileType: 45 case sys::ELF_Core_FileType: 46 return 0; 47 case sys::Mach_O_Object_FileType: 48 case sys::Mach_O_Executable_FileType: 49 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: 50 case sys::Mach_O_Core_FileType: 51 case sys::Mach_O_PreloadExecutable_FileType: 52 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: 53 case sys::Mach_O_DynamicLinker_FileType: 54 case sys::Mach_O_Bundle_FileType: 55 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: 56 return 0; 57 case sys::COFF_FileType: 58 return 0; 59 default: 60 llvm_unreachable("Unknown Object File Type"); 61 } 62 } 63 64 ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) { 65 return createObjectFile(MemoryBuffer::getFile(ObjectPath)); 66 } 67