Support/MemoryBuffer: Replace all uses of std::string *ErrMsg with error_code &ec...
[oota-llvm.git] / lib / Object / ObjectFile.cpp
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 #include "llvm/Support/Path.h"
18 #include "llvm/Support/system_error.h"
19
20 using namespace llvm;
21 using namespace object;
22
23 ObjectFile::ObjectFile(MemoryBuffer *Object)
24   : MapFile(Object) {
25   assert(MapFile && "Must be a valid MemoryBuffer!");
26   base = reinterpret_cast<const uint8_t *>(MapFile->getBufferStart());
27 }
28
29 ObjectFile::~ObjectFile() {
30   delete MapFile;
31 }
32
33 StringRef ObjectFile::getFilename() const {
34   return MapFile->getBufferIdentifier();
35 }
36
37 ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
38   if (!Object || Object->getBufferSize() < 64)
39     return 0;
40   sys::LLVMFileType type = sys::IdentifyFileType(Object->getBufferStart(),
41                                 static_cast<unsigned>(Object->getBufferSize()));
42   switch (type) {
43     case sys::ELF_Relocatable_FileType:
44     case sys::ELF_Executable_FileType:
45     case sys::ELF_SharedObject_FileType:
46     case sys::ELF_Core_FileType:
47         return 0;
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:
57       return 0;
58     case sys::COFF_FileType:
59       return 0;
60     default:
61       llvm_unreachable("Unknown Object File Type");
62   }
63 }
64
65 ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
66   error_code ec;
67   return createObjectFile(MemoryBuffer::getFile(ObjectPath, ec));
68 }