LLVM 24.0.0git
ObjectFile.cpp
Go to the documentation of this file.
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
14#include "llvm/Object/COFF.h"
15#include "llvm/Support/Format.h"
17
18using namespace llvm;
19using namespace object;
20
22 OS << "SectionedAddress{" << format_hex(Addr.Address, 10);
24 OS << ", " << Addr.SectionIndex;
25 return OS << "}";
26}
27
28void ObjectFile::anchor() {}
29
31 : SymbolicFile(Type, Source) {}
32
35 if (!SymSec) {
36 // TODO: Actually report errors helpfully.
37 consumeError(SymSec.takeError());
38 return false;
39 }
40 return *this == **SymSec;
41}
42
44 uint32_t Flags;
45 if (Error E = getSymbolFlags(Ref).moveInto(Flags))
46 // TODO: Test this error.
47 return std::move(E);
48
49 if (Flags & SymbolRef::SF_Undefined)
50 return 0;
51 if (Flags & SymbolRef::SF_Common)
53 return getSymbolValueImpl(Ref);
54}
55
58 if (!Name)
59 return Name.takeError();
60 OS << *Name;
61 return Error::success();
62}
63
65
67 Expected<StringRef> NameOrErr = getSectionName(Sec);
68 if (NameOrErr)
69 return *NameOrErr == ".llvm.lto";
70 consumeError(NameOrErr.takeError());
71 return false;
72}
73
74bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
75
77 return isSectionText(Sec);
78}
79
81 return isSectionData(Sec);
82}
83
84bool ObjectFile::isDebugSection(DataRefImpl Sec) const { return false; }
85
87 return any_of(sections(),
88 [](SectionRef Sec) { return Sec.isDebugSection(); });
89}
90
95
97 Triple TheTriple;
98 auto Arch = getArch();
99 TheTriple.setArch(Triple::ArchType(Arch));
100
101 auto OS = getOS();
102 if (OS != Triple::UnknownOS)
103 TheTriple.setOS(OS);
104
105 // For ARM targets, try to use the build attributes to build determine
106 // the build target. Target features are also added, but later during
107 // disassembly.
108 if (Arch == Triple::arm || Arch == Triple::armeb)
109 setARMSubArch(TheTriple);
110
111 // TheTriple defaults to ELF, and COFF doesn't have an environment:
112 // something we can do here is indicate that it is mach-o.
113 if (isMachO()) {
115 } else if (isCOFF()) {
116 const auto COFFObj = cast<COFFObjectFile>(this);
117 if (COFFObj->getArch() == Triple::thumb)
118 TheTriple.setTriple("thumbv7-windows");
119 } else if (isXCOFF()) {
120 // XCOFF implies AIX.
121 TheTriple.setOS(Triple::AIX);
123 } else if (isGOFF()) {
124 TheTriple.setOS(Triple::ZOS);
125 TheTriple.setObjectFormat(Triple::GOFF);
126 } else if (TheTriple.isAMDGPU()) {
127 TheTriple.setVendor(Triple::AMD);
128 } else if (TheTriple.isNVPTX()) {
129 TheTriple.setVendor(Triple::NVIDIA);
130 }
131
132 return TheTriple;
133}
134
137 const {
139 return ReflectionSectionKind == Swift5ReflectionSectionKind::fieldmd ||
140 ReflectionSectionKind == Swift5ReflectionSectionKind::reflstr ||
141 ReflectionSectionKind == Swift5ReflectionSectionKind::assocty;
142}
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
LLVM_ABI void setObjectFormat(ObjectFormatType Kind)
Set the object file format.
Definition Triple.cpp:1701
LLVM_ABI void setTriple(const Twine &Str)
Set all components to the new triple Str.
Definition Triple.cpp:1680
bool isAMDGPU() const
Definition Triple.h:998
bool isNVPTX() const
Tests whether the target is NVPTX (32- or 64-bit).
Definition Triple.h:991
LLVM_ABI void setOS(OSType Kind)
Set the operating system (third) component of the triple to a known type.
Definition Triple.cpp:1690
LLVM_ABI void setArch(ArchType Kind, SubArchType SubArch=NoSubArch)
Set the architecture (first) component of the triple to a known type.
Definition Triple.cpp:1682
LLVM_ABI void setVendor(VendorType Kind)
Set the vendor (second) component of the triple to a known type.
Definition Triple.cpp:1686
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isXCOFF() const
Definition Binary.h:137
bool isMachO() const
Definition Binary.h:129
bool isCOFF() const
Definition Binary.h:133
bool isGOFF() const
Definition Binary.h:151
virtual bool isBerkeleyText(DataRefImpl Sec) const
virtual Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const
uint64_t getCommonSymbolSize(DataRefImpl Symb) const
Definition ObjectFile.h:309
virtual Expected< StringRef > getSectionName(DataRefImpl Sec) const =0
virtual bool isBerkeleyData(DataRefImpl Sec) const
virtual Expected< StringRef > getSymbolName(DataRefImpl Symb) const =0
Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override
Triple makeTriple() const
Create a triple from the data in this object file.
virtual bool isSectionText(DataRefImpl Sec) const =0
section_iterator_range sections() const
Definition ObjectFile.h:331
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const =0
virtual bool isDebugSection(DataRefImpl Sec) const
virtual void setARMSubArch(Triple &TheTriple) const
Definition ObjectFile.h:348
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
virtual bool isSectionData(DataRefImpl Sec) const =0
Expected< uint64_t > getSymbolValue(DataRefImpl Symb) const
virtual Triple::OSType getOS() const
Definition ObjectFile.h:343
bool isReflectionSectionStrippable(llvm::binaryformat::Swift5ReflectionSectionKind ReflectionSectionKind) const
True if the reflection section can be stripped by the linker.
virtual bool hasDebugInfo() const
virtual Triple::ArchType getArch() const =0
virtual bool isSectionStripped(DataRefImpl Sec) const
virtual bool isSectionBitcode(DataRefImpl Sec) const
bool isDebugSection() const
Whether this section is a debug section.
Definition ObjectFile.h:587
LLVM_ABI bool containsSymbol(SymbolRef S) const
Expected< section_iterator > getSection() const
Get section this symbol is defined in reference to.
Definition ObjectFile.h:485
SymbolicFile(unsigned int Type, MemoryBufferRef Source)
virtual Expected< uint32_t > getSymbolFlags(DataRefImpl Symb) const =0
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Swift5ReflectionSectionKind
Definition Swift.h:14
content_iterator< SectionRef > section_iterator
Definition ObjectFile.h:49
LLVM_ABI raw_ostream & operator<<(raw_ostream &OS, const SectionedAddress &Addr)
This is an optimization pass for GlobalISel generic memory operations.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
Definition Format.h:164
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1106
static const uint64_t UndefSection
Definition ObjectFile.h:148