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