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 ObjectFile::ObjectFile(MemoryBuffer
*Object
)
26 assert(MapFile
&& "Must be a valid MemoryBuffer!");
27 base
= reinterpret_cast<const uint8_t *>(MapFile
->getBufferStart());
30 ObjectFile::~ObjectFile() {
34 StringRef
ObjectFile::getFilename() const {
35 return MapFile
->getBufferIdentifier();
38 ObjectFile
*ObjectFile::createObjectFile(MemoryBuffer
*Object
) {
39 if (!Object
|| Object
->getBufferSize() < 64)
41 sys::LLVMFileType type
= sys::IdentifyFileType(Object
->getBufferStart(),
42 static_cast<unsigned>(Object
->getBufferSize()));
44 case sys::ELF_Relocatable_FileType
:
45 case sys::ELF_Executable_FileType
:
46 case sys::ELF_SharedObject_FileType
:
47 case sys::ELF_Core_FileType
:
48 return createELFObjectFile(Object
);
49 case sys::Mach_O_Object_FileType
:
50 case sys::Mach_O_Executable_FileType
:
51 case sys::Mach_O_FixedVirtualMemorySharedLib_FileType
:
52 case sys::Mach_O_Core_FileType
:
53 case sys::Mach_O_PreloadExecutable_FileType
:
54 case sys::Mach_O_DynamicallyLinkedSharedLib_FileType
:
55 case sys::Mach_O_DynamicLinker_FileType
:
56 case sys::Mach_O_Bundle_FileType
:
57 case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType
:
59 case sys::COFF_FileType
:
60 return createCOFFObjectFile(Object
);
62 llvm_unreachable("Unknown Object File Type");
66 ObjectFile
*ObjectFile::createObjectFile(StringRef ObjectPath
) {
67 OwningPtr
<MemoryBuffer
> File
;
68 if (error_code ec
= MemoryBuffer::getFile(ObjectPath
, File
))
70 return createObjectFile(File
.take());