comparison lib/Object/ObjectFile.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children c2174574ed3a
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
1 //===- ObjectFile.cpp - File format independent object file -----*- C++ -*-===// 1 //===- ObjectFile.cpp - File format independent object file ---------------===//
2 // 2 //
3 // The LLVM Compiler Infrastructure 3 // The LLVM Compiler Infrastructure
4 // 4 //
5 // This file is distributed under the University of Illinois Open Source 5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
9 // 9 //
10 // This file defines a file format independent ObjectFile class. 10 // This file defines a file format independent ObjectFile class.
11 // 11 //
12 //===----------------------------------------------------------------------===// 12 //===----------------------------------------------------------------------===//
13 13
14 #include "llvm/Object/ObjectFile.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/BinaryFormat/Magic.h"
17 #include "llvm/Object/Binary.h"
14 #include "llvm/Object/COFF.h" 18 #include "llvm/Object/COFF.h"
19 #include "llvm/Object/Error.h"
15 #include "llvm/Object/MachO.h" 20 #include "llvm/Object/MachO.h"
16 #include "llvm/Object/ObjectFile.h" 21 #include "llvm/Object/Wasm.h"
22 #include "llvm/Support/Error.h"
17 #include "llvm/Support/ErrorHandling.h" 23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ErrorOr.h"
18 #include "llvm/Support/FileSystem.h" 25 #include "llvm/Support/FileSystem.h"
19 #include "llvm/Support/MemoryBuffer.h" 26 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/raw_ostream.h" 27 #include "llvm/Support/raw_ostream.h"
28 #include <algorithm>
29 #include <cstdint>
30 #include <memory>
21 #include <system_error> 31 #include <system_error>
22 32
23 using namespace llvm; 33 using namespace llvm;
24 using namespace object; 34 using namespace object;
25 35
26 void ObjectFile::anchor() { } 36 void ObjectFile::anchor() {}
27 37
28 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source) 38 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
29 : SymbolicFile(Type, Source) {} 39 : SymbolicFile(Type, Source) {}
30 40
31 bool SectionRef::containsSymbol(SymbolRef S) const { 41 bool SectionRef::containsSymbol(SymbolRef S) const {
63 if (!getSectionName(Sec, SectName)) 73 if (!getSectionName(Sec, SectName))
64 return SectName == ".llvmbc"; 74 return SectName == ".llvmbc";
65 return false; 75 return false;
66 } 76 }
67 77
78 bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
79
68 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { 80 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
69 return section_iterator(SectionRef(Sec, this)); 81 return section_iterator(SectionRef(Sec, this));
70 } 82 }
71 83
84 Triple ObjectFile::makeTriple() const {
85 Triple TheTriple;
86 auto Arch = getArch();
87 TheTriple.setArch(Triple::ArchType(Arch));
88
89 // For ARM targets, try to use the build attributes to build determine
90 // the build target. Target features are also added, but later during
91 // disassembly.
92 if (Arch == Triple::arm || Arch == Triple::armeb)
93 setARMSubArch(TheTriple);
94
95 // TheTriple defaults to ELF, and COFF doesn't have an environment:
96 // the best we can do here is indicate that it is mach-o.
97 if (isMachO())
98 TheTriple.setObjectFormat(Triple::MachO);
99
100 if (isCOFF()) {
101 const auto COFFObj = dyn_cast<COFFObjectFile>(this);
102 if (COFFObj->getArch() == Triple::thumb)
103 TheTriple.setTriple("thumbv7-windows");
104 }
105
106 return TheTriple;
107 }
108
72 Expected<std::unique_ptr<ObjectFile>> 109 Expected<std::unique_ptr<ObjectFile>>
73 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) { 110 ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
74 StringRef Data = Object.getBuffer(); 111 StringRef Data = Object.getBuffer();
75 if (Type == sys::fs::file_magic::unknown) 112 if (Type == file_magic::unknown)
76 Type = sys::fs::identify_magic(Data); 113 Type = identify_magic(Data);
77 114
78 switch (Type) { 115 switch (Type) {
79 case sys::fs::file_magic::unknown: 116 case file_magic::unknown:
80 case sys::fs::file_magic::bitcode: 117 case file_magic::bitcode:
81 case sys::fs::file_magic::coff_cl_gl_object: 118 case file_magic::coff_cl_gl_object:
82 case sys::fs::file_magic::archive: 119 case file_magic::archive:
83 case sys::fs::file_magic::macho_universal_binary: 120 case file_magic::macho_universal_binary:
84 case sys::fs::file_magic::windows_resource: 121 case file_magic::windows_resource:
85 return errorCodeToError(object_error::invalid_file_type); 122 return errorCodeToError(object_error::invalid_file_type);
86 case sys::fs::file_magic::elf: 123 case file_magic::elf:
87 case sys::fs::file_magic::elf_relocatable: 124 case file_magic::elf_relocatable:
88 case sys::fs::file_magic::elf_executable: 125 case file_magic::elf_executable:
89 case sys::fs::file_magic::elf_shared_object: 126 case file_magic::elf_shared_object:
90 case sys::fs::file_magic::elf_core: 127 case file_magic::elf_core:
91 return errorOrToExpected(createELFObjectFile(Object)); 128 return createELFObjectFile(Object);
92 case sys::fs::file_magic::macho_object: 129 case file_magic::macho_object:
93 case sys::fs::file_magic::macho_executable: 130 case file_magic::macho_executable:
94 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 131 case file_magic::macho_fixed_virtual_memory_shared_lib:
95 case sys::fs::file_magic::macho_core: 132 case file_magic::macho_core:
96 case sys::fs::file_magic::macho_preload_executable: 133 case file_magic::macho_preload_executable:
97 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 134 case file_magic::macho_dynamically_linked_shared_lib:
98 case sys::fs::file_magic::macho_dynamic_linker: 135 case file_magic::macho_dynamic_linker:
99 case sys::fs::file_magic::macho_bundle: 136 case file_magic::macho_bundle:
100 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 137 case file_magic::macho_dynamically_linked_shared_lib_stub:
101 case sys::fs::file_magic::macho_dsym_companion: 138 case file_magic::macho_dsym_companion:
102 case sys::fs::file_magic::macho_kext_bundle: 139 case file_magic::macho_kext_bundle:
103 return createMachOObjectFile(Object); 140 return createMachOObjectFile(Object);
104 case sys::fs::file_magic::coff_object: 141 case file_magic::coff_object:
105 case sys::fs::file_magic::coff_import_library: 142 case file_magic::coff_import_library:
106 case sys::fs::file_magic::pecoff_executable: 143 case file_magic::pecoff_executable:
107 return errorOrToExpected(createCOFFObjectFile(Object)); 144 return createCOFFObjectFile(Object);
145 case file_magic::wasm_object:
146 return createWasmObjectFile(Object);
108 } 147 }
109 llvm_unreachable("Unexpected Object File Type"); 148 llvm_unreachable("Unexpected Object File Type");
110 } 149 }
111 150
112 Expected<OwningBinary<ObjectFile>> 151 Expected<OwningBinary<ObjectFile>>