LLVM 24.0.0git
MCELFObjectWriter.h
Go to the documentation of this file.
1//===- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ----------*- C++ -*-===//
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#ifndef LLVM_MC_MCELFOBJECTWRITER_H
10#define LLVM_MC_MCELFOBJECTWRITER_H
11
12#include "llvm/ADT/DenseMap.h"
20#include <cstdint>
21#include <memory>
22#include <optional>
23#include <vector>
24
25namespace llvm {
26
27class MCAssembler;
28class MCFixup;
29class MCSymbol;
30class MCSymbolELF;
31class MCTargetOptions;
32class MCValue;
33
35 uint64_t Offset; // Where is the relocation.
36 const MCSymbolELF *Symbol; // The symbol to relocate with.
37 unsigned Type; // The type of the relocation.
38 uint64_t Addend; // The addend to use.
39
43
44 void print(raw_ostream &Out) const {
45 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
46 << ", Addend=" << Addend;
47 }
48
49 LLVM_DUMP_METHOD void dump() const { print(errs()); }
50};
51
53 const uint8_t OSABI;
54 const uint8_t ABIVersion;
55 const uint16_t EMachine;
56 const unsigned HasRelocationAddend : 1;
57 const unsigned Is64Bit : 1;
58
59protected:
60 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
61 bool HasRelocationAddend_, uint8_t ABIVersion_ = 0);
62
63public:
64 ~MCELFObjectTargetWriter() override = default;
65
66 Triple::ObjectFormatType getFormat() const override { return Triple::ELF; }
67 static bool classof(const MCObjectTargetWriter *W) {
68 return W->getFormat() == Triple::ELF;
69 }
70
72 switch (OSType) {
75 case Triple::PS4:
76 case Triple::FreeBSD:
78 case Triple::Solaris:
80 case Triple::OpenBSD:
82 default:
83 return ELF::ELFOSABI_NONE;
84 }
85 }
86
87 virtual unsigned getRelocType(const MCFixup &Fixup, const MCValue &Target,
88 bool IsPCRel) const = 0;
89
90 virtual bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const {
91 return false;
92 }
93
94 virtual void sortRelocs(std::vector<ELFRelocationEntry> &Relocs);
95
96 /// \name Accessors
97 /// @{
98 uint8_t getOSABI() const { return OSABI; }
99 uint8_t getABIVersion() const { return ABIVersion; }
100 uint16_t getEMachine() const { return EMachine; }
101 bool hasRelocationAddend() const { return HasRelocationAddend; }
102 bool is64Bit() const { return Is64Bit; }
103 /// @}
104
105 // Instead of changing everyone's API we pack the N64 Type fields
106 // into the existing 32 bit data unsigned.
107#define R_TYPE_SHIFT 0
108#define R_TYPE_MASK 0xffffff00
109#define R_TYPE2_SHIFT 8
110#define R_TYPE2_MASK 0xffff00ff
111#define R_TYPE3_SHIFT 16
112#define R_TYPE3_MASK 0xff00ffff
113#define R_SSYM_SHIFT 24
114#define R_SSYM_MASK 0x00ffffff
115
116 // N64 relocation type accessors
118 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
119 }
121 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
122 }
124 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
125 }
127 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
128 }
129
130 // N64 relocation type setting
131 static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3) {
132 return ((Value1 & 0xff) << R_TYPE_SHIFT) |
133 ((Value2 & 0xff) << R_TYPE2_SHIFT) |
134 ((Value3 & 0xff) << R_TYPE3_SHIFT);
135 }
136 unsigned setRSsym(unsigned Value, unsigned Type) const {
137 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
138 }
139};
140
142 unsigned ELFHeaderEFlags = 0;
143
144public:
145 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
148
151 // .weakref aliases
153 bool IsLittleEndian = false;
154 bool SeenGnuAbi = false;
155 std::optional<uint8_t> OverrideABIVersion;
156
157 struct Symver {
159 const MCSymbol *Sym;
161 // True if .symver *, *@@@* or .symver *, *, remove.
163 };
165
166 ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
168 ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
170 bool IsLittleEndian);
171
172 void reset() override;
173 void setAssembler(MCAssembler *Asm) override;
174 void executePostLayoutBinding() override;
175 void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
176 MCValue Target, uint64_t &FixedValue) override;
178 const MCFragment &FB, bool InSet,
179 bool IsPCRel) const override;
180 uint64_t writeObject() override;
181
182 bool hasRelocationAddend() const;
183 bool usesRela(const MCTargetOptions &TO, const MCSectionELF &Sec) const;
184
185 bool useSectionSymbol(const MCValue &Val, const MCSymbolELF *Sym, uint64_t C,
186 unsigned Type) const;
187
188 bool checkRelocation(SMLoc Loc, const MCSectionELF *From,
189 const MCSectionELF *To);
190
191 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
192 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
193
194 // Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN, STB_GNU_UNIQUE).
195 void markGnuAbi() { SeenGnuAbi = true; }
196 bool seenGnuAbi() const { return SeenGnuAbi; }
197
198 // Override the default e_ident[EI_ABIVERSION] in the ELF header.
200};
201} // end namespace llvm
202
203#endif // LLVM_MC_MCELFOBJECTWRITER_H
uint64_t uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_ABI
Definition Compiler.h:211
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:702
This file defines the DenseMap class.
#define R_TYPE2_SHIFT
#define R_SSYM_MASK
#define R_TYPE_SHIFT
#define R_TYPE3_SHIFT
#define R_SSYM_SHIFT
#define F(x, y, z)
Definition MD5.cpp:54
This file defines the SmallVector class.
std::unique_ptr< MCELFObjectTargetWriter > TargetObjectWriter
ELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
void setAssembler(MCAssembler *Asm) override
bool usesRela(const MCTargetOptions &TO, const MCSectionELF &Sec) const
SmallVector< const MCSymbolELF *, 0 > Weakrefs
bool checkRelocation(SMLoc Loc, const MCSectionELF *From, const MCSectionELF *To)
unsigned getELFHeaderEFlags() const
void reset() override
lifetime management
void setELFHeaderEFlags(unsigned Flags)
void setOverrideABIVersion(uint8_t V)
std::optional< uint8_t > OverrideABIVersion
uint64_t writeObject() override
Write the object file and returns the number of bytes written.
DenseMap< const MCSectionELF *, std::vector< ELFRelocationEntry > > Relocations
void executePostLayoutBinding() override
Perform any late binding of symbols (for example, to assign symbol indices for use when generating re...
bool isSymbolRefDifferenceFullyResolvedImpl(const MCSymbol &SymA, const MCFragment &FB, bool InSet, bool IsPCRel) const override
SmallVector< Symver, 0 > Symvers
raw_pwrite_stream & OS
void recordRelocation(const MCFragment &F, const MCFixup &Fixup, MCValue Target, uint64_t &FixedValue) override
Record a relocation entry.
raw_pwrite_stream * DwoOS
bool useSectionSymbol(const MCValue &Val, const MCSymbolELF *Sym, uint64_t C, unsigned Type) const
DenseMap< const MCSymbolELF *, const MCSymbolELF * > Renames
static bool classof(const MCObjectTargetWriter *W)
virtual unsigned getRelocType(const MCFixup &Fixup, const MCValue &Target, bool IsPCRel) const =0
Triple::ObjectFormatType getFormat() const override
~MCELFObjectTargetWriter() override=default
static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3)
uint8_t getRType(uint32_t Type) const
uint8_t getRSsym(uint32_t Type) const
virtual bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const
static uint8_t getOSABI(Triple::OSType OSType)
unsigned setRSsym(unsigned Value, unsigned Type) const
MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_, bool HasRelocationAddend_, uint8_t ABIVersion_=0)
uint8_t getRType2(uint32_t Type) const
uint8_t getRType3(uint32_t Type) const
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition MCFixup.h:65
Base class for classes that define behaviour that is specific to both the target and the object forma...
MCObjectWriter()=default
This represents a section on linux, lots of unix variants and some bare metal systems.
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
Represents a location in source code.
Definition SMLoc.h:22
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Target - Wrapper for Target specific information.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
An abstract base class for streams implementations that also support a pwrite operation.
@ ELFOSABI_OPENBSD
Definition ELF.h:166
@ ELFOSABI_SOLARIS
Definition ELF.h:160
@ ELFOSABI_FREEBSD
Definition ELF.h:163
@ ELFOSABI_STANDALONE
Definition ELF.h:182
@ ELFOSABI_NONE
Definition ELF.h:154
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type, uint64_t Addend)
const MCSymbolELF * Symbol
void print(raw_ostream &Out) const
LLVM_DUMP_METHOD void dump() const