Remove system_error.h.
[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/FileSystem.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <system_error>
20
21 using namespace llvm;
22 using namespace object;
23
24 void ObjectFile::anchor() { }
25
26 ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *Source,
27                        bool BufferOwned)
28     : SymbolicFile(Type, Source, BufferOwned) {}
29
30 error_code ObjectFile::printSymbolName(raw_ostream &OS,
31                                        DataRefImpl Symb) const {
32   StringRef Name;
33   if (error_code EC = getSymbolName(Symb, Name))
34     return EC;
35   OS << Name;
36   return object_error::success;
37 }
38
39 error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
40                                           uint32_t &Result) const {
41   Result = 0;
42   return object_error::success;
43 }
44
45 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
46   return section_iterator(SectionRef(Sec, this));
47 }
48
49 ErrorOr<ObjectFile *> ObjectFile::createObjectFile(MemoryBuffer *Object,
50                                                    bool BufferOwned,
51                                                    sys::fs::file_magic Type) {
52   if (Type == sys::fs::file_magic::unknown)
53     Type = sys::fs::identify_magic(Object->getBuffer());
54
55   switch (Type) {
56   case sys::fs::file_magic::unknown:
57   case sys::fs::file_magic::bitcode:
58   case sys::fs::file_magic::archive:
59   case sys::fs::file_magic::macho_universal_binary:
60   case sys::fs::file_magic::windows_resource:
61     if (BufferOwned)
62       delete Object;
63     return object_error::invalid_file_type;
64   case sys::fs::file_magic::elf_relocatable:
65   case sys::fs::file_magic::elf_executable:
66   case sys::fs::file_magic::elf_shared_object:
67   case sys::fs::file_magic::elf_core:
68     return createELFObjectFile(Object, BufferOwned);
69   case sys::fs::file_magic::macho_object:
70   case sys::fs::file_magic::macho_executable:
71   case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
72   case sys::fs::file_magic::macho_core:
73   case sys::fs::file_magic::macho_preload_executable:
74   case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
75   case sys::fs::file_magic::macho_dynamic_linker:
76   case sys::fs::file_magic::macho_bundle:
77   case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
78   case sys::fs::file_magic::macho_dsym_companion:
79     return createMachOObjectFile(Object, BufferOwned);
80   case sys::fs::file_magic::coff_object:
81   case sys::fs::file_magic::coff_import_library:
82   case sys::fs::file_magic::pecoff_executable:
83     return createCOFFObjectFile(Object, BufferOwned);
84   }
85   llvm_unreachable("Unexpected Object File Type");
86 }
87
88 ErrorOr<ObjectFile *> ObjectFile::createObjectFile(StringRef ObjectPath) {
89   std::unique_ptr<MemoryBuffer> File;
90   if (error_code EC = MemoryBuffer::getFile(ObjectPath, File))
91     return EC;
92   return createObjectFile(File.release());
93 }