Don't own the buffer in object::Binary.
[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 object_error::success;
38 }
39
40 std::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<std::unique_ptr<ObjectFile>>
51 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
52   StringRef Data = Object.getBuffer();
53   if (Type == sys::fs::file_magic::unknown)
54     Type = sys::fs::identify_magic(Data);
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     return object_error::invalid_file_type;
63   case sys::fs::file_magic::elf_relocatable:
64   case sys::fs::file_magic::elf_executable:
65   case sys::fs::file_magic::elf_shared_object:
66   case sys::fs::file_magic::elf_core:
67     return createELFObjectFile(Object);
68   case sys::fs::file_magic::macho_object:
69   case sys::fs::file_magic::macho_executable:
70   case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
71   case sys::fs::file_magic::macho_core:
72   case sys::fs::file_magic::macho_preload_executable:
73   case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
74   case sys::fs::file_magic::macho_dynamic_linker:
75   case sys::fs::file_magic::macho_bundle:
76   case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
77   case sys::fs::file_magic::macho_dsym_companion:
78     return createMachOObjectFile(Object);
79   case sys::fs::file_magic::coff_object:
80   case sys::fs::file_magic::coff_import_library:
81   case sys::fs::file_magic::pecoff_executable:
82     return createCOFFObjectFile(Object);
83   }
84   llvm_unreachable("Unexpected Object File Type");
85 }
86
87 ErrorOr<OwningBinary<ObjectFile>>
88 ObjectFile::createObjectFile(StringRef ObjectPath) {
89   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
90       MemoryBuffer::getFile(ObjectPath);
91   if (std::error_code EC = FileOrErr.getError())
92     return EC;
93   std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
94
95   ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
96       createObjectFile(Buffer->getMemBufferRef());
97   if (std::error_code EC = ObjOrErr.getError())
98     return EC;
99   std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
100
101   return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
102 }