1 //===- ObjectFile.cpp - File format independent object file -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a file format independent ObjectFile class.
12 //===----------------------------------------------------------------------===//
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"
22 using namespace object;
24 void ObjectFile::anchor() { }
26 ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec)
27 : Binary(Type, source) {
30 ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
31 if (!Object || Object->getBufferSize() < 64)
33 sys::LLVMFileType type = sys::IdentifyFileType(Object->getBufferStart(),
34 static_cast<unsigned>(Object->getBufferSize()));
36 case sys::Unknown_FileType:
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);
57 llvm_unreachable("Unexpected Object File Type");
61 ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
62 OwningPtr<MemoryBuffer> File;
63 if (MemoryBuffer::getFile(ObjectPath, File))
65 return createObjectFile(File.take());