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