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
anchor()26 void ObjectFile::anchor() { }
27
ObjectFile(unsigned int Type,MemoryBufferRef Source)28 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
29 : SymbolicFile(Type, Source) {}
30
printSymbolName(raw_ostream & OS,DataRefImpl Symb) const31 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
getSymbolAlignment(DataRefImpl DRI,uint32_t & Result) const40 std::error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
41 uint32_t &Result) const {
42 Result = 0;
43 return object_error::success;
44 }
45
getRelocatedSection(DataRefImpl Sec) const46 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
47 return section_iterator(SectionRef(Sec, this));
48 }
49
50 ErrorOr<std::unique_ptr<ObjectFile>>
createObjectFile(MemoryBufferRef Object,sys::fs::file_magic Type)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:
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);
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 case sys::fs::file_magic::macho_kext_bundle:
80 return createMachOObjectFile(Object);
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);
85 }
86 llvm_unreachable("Unexpected Object File Type");
87 }
88
89 ErrorOr<OwningBinary<ObjectFile>>
createObjectFile(StringRef ObjectPath)90 ObjectFile::createObjectFile(StringRef ObjectPath) {
91 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
92 MemoryBuffer::getFile(ObjectPath);
93 if (std::error_code EC = FileOrErr.getError())
94 return EC;
95 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
96
97 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
98 createObjectFile(Buffer->getMemBufferRef());
99 if (std::error_code EC = ObjOrErr.getError())
100 return EC;
101 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
102
103 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
104 }
105