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