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