1 //===- ObjectFile.cpp - File format independent object file ---------------===//
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/StringRef.h"
16 #include "llvm/BinaryFormat/Magic.h"
17 #include "llvm/Object/Binary.h"
18 #include "llvm/Object/COFF.h"
19 #include "llvm/Object/Error.h"
20 #include "llvm/Object/MachO.h"
21 #include "llvm/Object/Wasm.h"
22 #include "llvm/Support/Error.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/ErrorOr.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <algorithm>
29 #include <cstdint>
30 #include <memory>
31 #include <system_error>
32
33 using namespace llvm;
34 using namespace object;
35
anchor()36 void ObjectFile::anchor() {}
37
ObjectFile(unsigned int Type,MemoryBufferRef Source)38 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
39 : SymbolicFile(Type, Source) {}
40
containsSymbol(SymbolRef S) const41 bool SectionRef::containsSymbol(SymbolRef S) const {
42 Expected<section_iterator> SymSec = S.getSection();
43 if (!SymSec) {
44 // TODO: Actually report errors helpfully.
45 consumeError(SymSec.takeError());
46 return false;
47 }
48 return *this == **SymSec;
49 }
50
getSymbolValue(DataRefImpl Ref) const51 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
52 uint32_t Flags = getSymbolFlags(Ref);
53 if (Flags & SymbolRef::SF_Undefined)
54 return 0;
55 if (Flags & SymbolRef::SF_Common)
56 return getCommonSymbolSize(Ref);
57 return getSymbolValueImpl(Ref);
58 }
59
printSymbolName(raw_ostream & OS,DataRefImpl Symb) const60 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
61 DataRefImpl Symb) const {
62 Expected<StringRef> Name = getSymbolName(Symb);
63 if (!Name)
64 return errorToErrorCode(Name.takeError());
65 OS << *Name;
66 return std::error_code();
67 }
68
getSymbolAlignment(DataRefImpl DRI) const69 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
70
isSectionBitcode(DataRefImpl Sec) const71 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
72 StringRef SectName;
73 if (!getSectionName(Sec, SectName))
74 return SectName == ".llvmbc";
75 return false;
76 }
77
isSectionStripped(DataRefImpl Sec) const78 bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
79
isBerkeleyText(DataRefImpl Sec) const80 bool ObjectFile::isBerkeleyText(DataRefImpl Sec) const {
81 return isSectionText(Sec);
82 }
83
isBerkeleyData(DataRefImpl Sec) const84 bool ObjectFile::isBerkeleyData(DataRefImpl Sec) const {
85 return isSectionData(Sec);
86 }
87
getRelocatedSection(DataRefImpl Sec) const88 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
89 return section_iterator(SectionRef(Sec, this));
90 }
91
makeTriple() const92 Triple ObjectFile::makeTriple() const {
93 Triple TheTriple;
94 auto Arch = getArch();
95 TheTriple.setArch(Triple::ArchType(Arch));
96
97 // For ARM targets, try to use the build attributes to build determine
98 // the build target. Target features are also added, but later during
99 // disassembly.
100 if (Arch == Triple::arm || Arch == Triple::armeb)
101 setARMSubArch(TheTriple);
102
103 // TheTriple defaults to ELF, and COFF doesn't have an environment:
104 // the best we can do here is indicate that it is mach-o.
105 if (isMachO())
106 TheTriple.setObjectFormat(Triple::MachO);
107
108 if (isCOFF()) {
109 const auto COFFObj = dyn_cast<COFFObjectFile>(this);
110 if (COFFObj->getArch() == Triple::thumb)
111 TheTriple.setTriple("thumbv7-windows");
112 }
113
114 return TheTriple;
115 }
116
117 Expected<std::unique_ptr<ObjectFile>>
createObjectFile(MemoryBufferRef Object,file_magic Type)118 ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
119 StringRef Data = Object.getBuffer();
120 if (Type == file_magic::unknown)
121 Type = identify_magic(Data);
122
123 switch (Type) {
124 case file_magic::unknown:
125 case file_magic::bitcode:
126 case file_magic::coff_cl_gl_object:
127 case file_magic::archive:
128 case file_magic::macho_universal_binary:
129 case file_magic::windows_resource:
130 case file_magic::pdb:
131 return errorCodeToError(object_error::invalid_file_type);
132 case file_magic::elf:
133 case file_magic::elf_relocatable:
134 case file_magic::elf_executable:
135 case file_magic::elf_shared_object:
136 case file_magic::elf_core:
137 return createELFObjectFile(Object);
138 case file_magic::macho_object:
139 case file_magic::macho_executable:
140 case file_magic::macho_fixed_virtual_memory_shared_lib:
141 case file_magic::macho_core:
142 case file_magic::macho_preload_executable:
143 case file_magic::macho_dynamically_linked_shared_lib:
144 case file_magic::macho_dynamic_linker:
145 case file_magic::macho_bundle:
146 case file_magic::macho_dynamically_linked_shared_lib_stub:
147 case file_magic::macho_dsym_companion:
148 case file_magic::macho_kext_bundle:
149 return createMachOObjectFile(Object);
150 case file_magic::coff_object:
151 case file_magic::coff_import_library:
152 case file_magic::pecoff_executable:
153 return createCOFFObjectFile(Object);
154 case file_magic::wasm_object:
155 return createWasmObjectFile(Object);
156 }
157 llvm_unreachable("Unexpected Object File Type");
158 }
159
160 Expected<OwningBinary<ObjectFile>>
createObjectFile(StringRef ObjectPath)161 ObjectFile::createObjectFile(StringRef ObjectPath) {
162 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
163 MemoryBuffer::getFile(ObjectPath);
164 if (std::error_code EC = FileOrErr.getError())
165 return errorCodeToError(EC);
166 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
167
168 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
169 createObjectFile(Buffer->getMemBufferRef());
170 if (Error Err = ObjOrErr.takeError())
171 return std::move(Err);
172 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
173
174 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
175 }
176