Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / lib / Object / ObjectFile.cpp
blob428166f58070d0bcbf745696d5c54bf660efaf54
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.
11 //===----------------------------------------------------------------------===//
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>
31 using namespace llvm;
32 using namespace object;
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 << "}";
41 void ObjectFile::anchor() {}
43 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
44 : SymbolicFile(Type, Source) {}
46 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;
53 return *this == **SymSec;
56 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);
62 if (Flags & SymbolRef::SF_Undefined)
63 return 0;
64 if (Flags & SymbolRef::SF_Common)
65 return getCommonSymbolSize(Ref);
66 return getSymbolValueImpl(Ref);
69 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();
77 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
79 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
80 Expected<StringRef> NameOrErr = getSectionName(Sec);
81 if (NameOrErr)
82 return *NameOrErr == ".llvm.lto";
83 consumeError(NameOrErr.takeError());
84 return false;
87 bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
89 bool ObjectFile::isBerkeleyText(DataRefImpl Sec) const {
90 return isSectionText(Sec);
93 bool ObjectFile::isBerkeleyData(DataRefImpl Sec) const {
94 return isSectionData(Sec);
97 bool ObjectFile::isDebugSection(DataRefImpl Sec) const { return false; }
99 bool ObjectFile::hasDebugInfo() const {
100 return any_of(sections(),
101 [](SectionRef Sec) { return Sec.isDebugSection(); });
104 Expected<section_iterator>
105 ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
106 return section_iterator(SectionRef(Sec, this));
109 Triple ObjectFile::makeTriple() const {
110 Triple TheTriple;
111 auto Arch = getArch();
112 TheTriple.setArch(Triple::ArchType(Arch));
114 // For ARM targets, try to use the build attributes to build determine
115 // the build target. Target features are also added, but later during
116 // disassembly.
117 if (Arch == Triple::arm || Arch == Triple::armeb)
118 setARMSubArch(TheTriple);
120 // TheTriple defaults to ELF, and COFF doesn't have an environment:
121 // something we can do here is indicate that it is mach-o.
122 if (isMachO()) {
123 TheTriple.setObjectFormat(Triple::MachO);
124 } else if (isCOFF()) {
125 const auto COFFObj = cast<COFFObjectFile>(this);
126 if (COFFObj->getArch() == Triple::thumb)
127 TheTriple.setTriple("thumbv7-windows");
128 } else if (isXCOFF()) {
129 // XCOFF implies AIX.
130 TheTriple.setOS(Triple::AIX);
131 TheTriple.setObjectFormat(Triple::XCOFF);
133 else if (isGOFF()) {
134 TheTriple.setOS(Triple::ZOS);
135 TheTriple.setObjectFormat(Triple::GOFF);
138 return TheTriple;
141 Expected<std::unique_ptr<ObjectFile>>
142 ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type,
143 bool InitContent) {
144 StringRef Data = Object.getBuffer();
145 if (Type == file_magic::unknown)
146 Type = identify_magic(Data);
148 switch (Type) {
149 case file_magic::unknown:
150 case file_magic::bitcode:
151 case file_magic::coff_cl_gl_object:
152 case file_magic::archive:
153 case file_magic::macho_universal_binary:
154 case file_magic::windows_resource:
155 case file_magic::pdb:
156 case file_magic::minidump:
157 case file_magic::goff_object:
158 case file_magic::cuda_fatbinary:
159 case file_magic::offload_binary:
160 case file_magic::dxcontainer_object:
161 case file_magic::offload_bundle:
162 case file_magic::offload_bundle_compressed:
163 return errorCodeToError(object_error::invalid_file_type);
164 case file_magic::tapi_file:
165 return errorCodeToError(object_error::invalid_file_type);
166 case file_magic::elf:
167 case file_magic::elf_relocatable:
168 case file_magic::elf_executable:
169 case file_magic::elf_shared_object:
170 case file_magic::elf_core:
171 return createELFObjectFile(Object, InitContent);
172 case file_magic::macho_object:
173 case file_magic::macho_executable:
174 case file_magic::macho_fixed_virtual_memory_shared_lib:
175 case file_magic::macho_core:
176 case file_magic::macho_preload_executable:
177 case file_magic::macho_dynamically_linked_shared_lib:
178 case file_magic::macho_dynamic_linker:
179 case file_magic::macho_bundle:
180 case file_magic::macho_dynamically_linked_shared_lib_stub:
181 case file_magic::macho_dsym_companion:
182 case file_magic::macho_kext_bundle:
183 case file_magic::macho_file_set:
184 return createMachOObjectFile(Object);
185 case file_magic::coff_object:
186 case file_magic::coff_import_library:
187 case file_magic::pecoff_executable:
188 return createCOFFObjectFile(Object);
189 case file_magic::xcoff_object_32:
190 return createXCOFFObjectFile(Object, Binary::ID_XCOFF32);
191 case file_magic::xcoff_object_64:
192 return createXCOFFObjectFile(Object, Binary::ID_XCOFF64);
193 case file_magic::wasm_object:
194 return createWasmObjectFile(Object);
196 llvm_unreachable("Unexpected Object File Type");
199 Expected<OwningBinary<ObjectFile>>
200 ObjectFile::createObjectFile(StringRef ObjectPath) {
201 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
202 MemoryBuffer::getFile(ObjectPath);
203 if (std::error_code EC = FileOrErr.getError())
204 return errorCodeToError(EC);
205 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
207 Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
208 createObjectFile(Buffer->getMemBufferRef());
209 if (Error Err = ObjOrErr.takeError())
210 return std::move(Err);
211 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
213 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
216 bool ObjectFile::isReflectionSectionStrippable(
217 llvm::binaryformat::Swift5ReflectionSectionKind ReflectionSectionKind)
218 const {
219 using llvm::binaryformat::Swift5ReflectionSectionKind;
220 return ReflectionSectionKind == Swift5ReflectionSectionKind::fieldmd ||
221 ReflectionSectionKind == Swift5ReflectionSectionKind::reflstr ||
222 ReflectionSectionKind == Swift5ReflectionSectionKind::assocty;