1 //===- ObjectFile.cpp - File format independent object file ---------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a file format independent ObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Object/ObjectFile.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/BinaryFormat/Magic.h"
16 #include "llvm/Object/Binary.h"
17 #include "llvm/Object/COFF.h"
18 #include "llvm/Object/Error.h"
19 #include "llvm/Object/MachO.h"
20 #include "llvm/Object/Wasm.h"
21 #include "llvm/Support/Error.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/Format.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include <cstdint>
28 #include <memory>
29 #include <system_error>
30
31 using namespace llvm;
32 using namespace object;
33
operator <<(raw_ostream & OS,const SectionedAddress & Addr)34 raw_ostream &object::operator<<(raw_ostream &OS, const SectionedAddress &Addr) {
35 OS << "SectionedAddress{" << format_hex(Addr.Address, 10);
36 if (Addr.SectionIndex != SectionedAddress::UndefSection)
37 OS << ", " << Addr.SectionIndex;
38 return OS << "}";
39 }
40
anchor()41 void ObjectFile::anchor() {}
42
ObjectFile(unsigned int Type,MemoryBufferRef Source)43 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
44 : SymbolicFile(Type, Source) {}
45
containsSymbol(SymbolRef S) const46 bool SectionRef::containsSymbol(SymbolRef S) const {
47 Expected<section_iterator> SymSec = S.getSection();
48 if (!SymSec) {
49 // TODO: Actually report errors helpfully.
50 consumeError(SymSec.takeError());
51 return false;
52 }
53 return *this == **SymSec;
54 }
55
getSymbolValue(DataRefImpl Ref) const56 Expected<uint64_t> ObjectFile::getSymbolValue(DataRefImpl Ref) const {
57 uint32_t Flags;
58 if (Error E = getSymbolFlags(Ref).moveInto(Flags))
59 // TODO: Test this error.
60 return std::move(E);
61
62 if (Flags & SymbolRef::SF_Undefined)
63 return 0;
64 if (Flags & SymbolRef::SF_Common)
65 return getCommonSymbolSize(Ref);
66 return getSymbolValueImpl(Ref);
67 }
68
printSymbolName(raw_ostream & OS,DataRefImpl Symb) const69 Error ObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const {
70 Expected<StringRef> Name = getSymbolName(Symb);
71 if (!Name)
72 return Name.takeError();
73 OS << *Name;
74 return Error::success();
75 }
76
getSymbolAlignment(DataRefImpl DRI) const77 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
78
isSectionBitcode(DataRefImpl Sec) const79 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
80 Expected<StringRef> NameOrErr = getSectionName(Sec);
81 if (NameOrErr)
82 return *NameOrErr == ".llvmbc";
83 consumeError(NameOrErr.takeError());
84 return false;
85 }
86
isSectionStripped(DataRefImpl Sec) const87 bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
88
isBerkeleyText(DataRefImpl Sec) const89 bool ObjectFile::isBerkeleyText(DataRefImpl Sec) const {
90 return isSectionText(Sec);
91 }
92
isBerkeleyData(DataRefImpl Sec) const93 bool ObjectFile::isBerkeleyData(DataRefImpl Sec) const {
94 return isSectionData(Sec);
95 }
96
isDebugSection(DataRefImpl Sec) const97 bool ObjectFile::isDebugSection(DataRefImpl Sec) const { return false; }
98
99 Expected<section_iterator>
getRelocatedSection(DataRefImpl Sec) const100 ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
101 return section_iterator(SectionRef(Sec, this));
102 }
103
makeTriple() const104 Triple ObjectFile::makeTriple() const {
105 Triple TheTriple;
106 auto Arch = getArch();
107 TheTriple.setArch(Triple::ArchType(Arch));
108
109 // For ARM targets, try to use the build attributes to build determine
110 // the build target. Target features are also added, but later during
111 // disassembly.
112 if (Arch == Triple::arm || Arch == Triple::armeb)
113 setARMSubArch(TheTriple);
114
115 // TheTriple defaults to ELF, and COFF doesn't have an environment:
116 // something we can do here is indicate that it is mach-o.
117 if (isMachO()) {
118 TheTriple.setObjectFormat(Triple::MachO);
119 } else if (isCOFF()) {
120 const auto COFFObj = cast<COFFObjectFile>(this);
121 if (COFFObj->getArch() == Triple::thumb)
122 TheTriple.setTriple("thumbv7-windows");
123 } else if (isXCOFF()) {
124 // XCOFF implies AIX.
125 TheTriple.setOS(Triple::AIX);
126 TheTriple.setObjectFormat(Triple::XCOFF);
127 }
128
129 return TheTriple;
130 }
131
132 Expected<std::unique_ptr<ObjectFile>>
createObjectFile(MemoryBufferRef Object,file_magic Type,bool InitContent)133 ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type,
134 bool InitContent) {
135 StringRef Data = Object.getBuffer();
136 if (Type == file_magic::unknown)
137 Type = identify_magic(Data);
138
139 switch (Type) {
140 case file_magic::unknown:
141 case file_magic::bitcode:
142 case file_magic::coff_cl_gl_object:
143 case file_magic::archive:
144 case file_magic::macho_universal_binary:
145 case file_magic::windows_resource:
146 case file_magic::pdb:
147 case file_magic::minidump:
148 case file_magic::goff_object:
149 case file_magic::cuda_fatbinary:
150 case file_magic::offload_binary:
151 case file_magic::dxcontainer_object:
152 return errorCodeToError(object_error::invalid_file_type);
153 case file_magic::tapi_file:
154 return errorCodeToError(object_error::invalid_file_type);
155 case file_magic::elf:
156 case file_magic::elf_relocatable:
157 case file_magic::elf_executable:
158 case file_magic::elf_shared_object:
159 case file_magic::elf_core:
160 return createELFObjectFile(Object, InitContent);
161 case file_magic::macho_object:
162 case file_magic::macho_executable:
163 case file_magic::macho_fixed_virtual_memory_shared_lib:
164 case file_magic::macho_core:
165 case file_magic::macho_preload_executable:
166 case file_magic::macho_dynamically_linked_shared_lib:
167 case file_magic::macho_dynamic_linker:
168 case file_magic::macho_bundle:
169 case file_magic::macho_dynamically_linked_shared_lib_stub:
170 case file_magic::macho_dsym_companion:
171 case file_magic::macho_kext_bundle:
172 return createMachOObjectFile(Object);
173 case file_magic::coff_object:
174 case file_magic::coff_import_library:
175 case file_magic::pecoff_executable:
176 return createCOFFObjectFile(Object);
177 case file_magic::xcoff_object_32:
178 return createXCOFFObjectFile(Object, Binary::ID_XCOFF32);
179 case file_magic::xcoff_object_64:
180 return createXCOFFObjectFile(Object, Binary::ID_XCOFF64);
181 case file_magic::wasm_object:
182 return createWasmObjectFile(Object);
183 }
184 llvm_unreachable("Unexpected Object File Type");
185 }
186
187 Expected<OwningBinary<ObjectFile>>
createObjectFile(StringRef ObjectPath)188 ObjectFile::createObjectFile(StringRef ObjectPath) {
189 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
190 MemoryBuffer::getFile(ObjectPath);
191 if (std::error_code EC = FileOrErr.getError())
192 return errorCodeToError(EC);
193 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
194
195 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
196 createObjectFile(Buffer->getMemBufferRef());
197 if (Error Err = ObjOrErr.takeError())
198 return std::move(Err);
199 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
200
201 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
202 }
203
isReflectionSectionStrippable(llvm::binaryformat::Swift5ReflectionSectionKind ReflectionSectionKind) const204 bool ObjectFile::isReflectionSectionStrippable(
205 llvm::binaryformat::Swift5ReflectionSectionKind ReflectionSectionKind)
206 const {
207 using llvm::binaryformat::Swift5ReflectionSectionKind;
208 return ReflectionSectionKind == Swift5ReflectionSectionKind::fieldmd ||
209 ReflectionSectionKind == Swift5ReflectionSectionKind::reflstr ||
210 ReflectionSectionKind == Swift5ReflectionSectionKind::assocty;
211 }
212