12754fe60SDimitry Andric //===- ObjectFile.cpp - File format independent object file -----*- C++ -*-===// 22754fe60SDimitry Andric // 32754fe60SDimitry Andric // The LLVM Compiler Infrastructure 42754fe60SDimitry Andric // 52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source 62754fe60SDimitry Andric // License. See LICENSE.TXT for details. 72754fe60SDimitry Andric // 82754fe60SDimitry Andric //===----------------------------------------------------------------------===// 92754fe60SDimitry Andric // 102754fe60SDimitry Andric // This file defines a file format independent ObjectFile class. 112754fe60SDimitry Andric // 122754fe60SDimitry Andric //===----------------------------------------------------------------------===// 132754fe60SDimitry Andric 142754fe60SDimitry Andric #include "llvm/Object/ObjectFile.h" 152754fe60SDimitry Andric #include "llvm/ADT/OwningPtr.h" 162754fe60SDimitry Andric #include "llvm/Support/ErrorHandling.h" 172754fe60SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 182754fe60SDimitry Andric #include "llvm/Support/Path.h" 192754fe60SDimitry Andric #include "llvm/Support/system_error.h" 202754fe60SDimitry Andric 212754fe60SDimitry Andric using namespace llvm; 222754fe60SDimitry Andric using namespace object; 232754fe60SDimitry Andric 2417a519f9SDimitry Andric ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec) 2517a519f9SDimitry Andric : Binary(Type, source) { 262754fe60SDimitry Andric } 272754fe60SDimitry Andric 282754fe60SDimitry Andric ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) { 292754fe60SDimitry Andric if (!Object || Object->getBufferSize() < 64) 302754fe60SDimitry Andric return 0; 312754fe60SDimitry Andric sys::LLVMFileType type = sys::IdentifyFileType(Object->getBufferStart(), 322754fe60SDimitry Andric static_cast<unsigned>(Object->getBufferSize())); 332754fe60SDimitry Andric switch (type) { 342754fe60SDimitry Andric case sys::ELF_Relocatable_FileType: 352754fe60SDimitry Andric case sys::ELF_Executable_FileType: 362754fe60SDimitry Andric case sys::ELF_SharedObject_FileType: 372754fe60SDimitry Andric case sys::ELF_Core_FileType: 382754fe60SDimitry Andric return createELFObjectFile(Object); 392754fe60SDimitry Andric case sys::Mach_O_Object_FileType: 402754fe60SDimitry Andric case sys::Mach_O_Executable_FileType: 412754fe60SDimitry Andric case sys::Mach_O_FixedVirtualMemorySharedLib_FileType: 422754fe60SDimitry Andric case sys::Mach_O_Core_FileType: 432754fe60SDimitry Andric case sys::Mach_O_PreloadExecutable_FileType: 442754fe60SDimitry Andric case sys::Mach_O_DynamicallyLinkedSharedLib_FileType: 452754fe60SDimitry Andric case sys::Mach_O_DynamicLinker_FileType: 462754fe60SDimitry Andric case sys::Mach_O_Bundle_FileType: 472754fe60SDimitry Andric case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType: 483b0f4066SDimitry Andric return createMachOObjectFile(Object); 492754fe60SDimitry Andric case sys::COFF_FileType: 502754fe60SDimitry Andric return createCOFFObjectFile(Object); 512754fe60SDimitry Andric default: 522754fe60SDimitry Andric llvm_unreachable("Unknown Object File Type"); 532754fe60SDimitry Andric } 542754fe60SDimitry Andric } 552754fe60SDimitry Andric 562754fe60SDimitry Andric ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) { 572754fe60SDimitry Andric OwningPtr<MemoryBuffer> File; 582754fe60SDimitry Andric if (error_code ec = MemoryBuffer::getFile(ObjectPath, File)) 592754fe60SDimitry Andric return NULL; 602754fe60SDimitry Andric return createObjectFile(File.take()); 612754fe60SDimitry Andric } 62