LLVM 19.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
18#include <cstdint>
19#include <vector>
20
21namespace llvm {
22
23class MCAssembler;
24class MCContext;
25class MCFixup;
26class MCSymbol;
27class MCSymbolELF;
28class MCValue;
29
31 uint64_t Offset; // Where is the relocation.
32 const MCSymbolELF *Symbol; // The symbol to relocate with.
33 unsigned Type; // The type of the relocation.
34 uint64_t Addend; // The addend to use.
35 const MCSymbolELF *OriginalSymbol; // The original value of Symbol if we changed it.
36 uint64_t OriginalAddend; // The original value of addend.
37
43
44 void print(raw_ostream &Out) const {
45 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
46 << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
47 << ", OriginalAddend=" << OriginalAddend;
48 }
49
50 LLVM_DUMP_METHOD void dump() const { print(errs()); }
51};
52
54 const uint8_t OSABI;
55 const uint8_t ABIVersion;
56 const uint16_t EMachine;
57 const unsigned HasRelocationAddend : 1;
58 const unsigned Is64Bit : 1;
59
60protected:
61 MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
62 bool HasRelocationAddend_, uint8_t ABIVersion_ = 0);
63
64public:
65 virtual ~MCELFObjectTargetWriter() = default;
66
67 Triple::ObjectFormatType getFormat() const override { return Triple::ELF; }
68 static bool classof(const MCObjectTargetWriter *W) {
69 return W->getFormat() == Triple::ELF;
70 }
71
72 static uint8_t getOSABI(Triple::OSType OSType) {
73 switch (OSType) {
76 case Triple::PS4:
77 case Triple::FreeBSD:
79 case Triple::Solaris:
81 default:
82 return ELF::ELFOSABI_NONE;
83 }
84 }
85
86 virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
87 const MCFixup &Fixup, bool IsPCRel) const = 0;
88
89 virtual bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
90 unsigned Type) const;
91
92 virtual void sortRelocs(const MCAssembler &Asm,
93 std::vector<ELFRelocationEntry> &Relocs);
94
95 virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec);
96
97 /// \name Accessors
98 /// @{
99 uint8_t getOSABI() const { return OSABI; }
100 uint8_t getABIVersion() const { return ABIVersion; }
101 uint16_t getEMachine() const { return EMachine; }
102 bool hasRelocationAddend() const { return HasRelocationAddend; }
103 bool is64Bit() const { return Is64Bit; }
104 /// @}
105
106 // Instead of changing everyone's API we pack the N64 Type fields
107 // into the existing 32 bit data unsigned.
108#define R_TYPE_SHIFT 0
109#define R_TYPE_MASK 0xffffff00
110#define R_TYPE2_SHIFT 8
111#define R_TYPE2_MASK 0xffff00ff
112#define R_TYPE3_SHIFT 16
113#define R_TYPE3_MASK 0xff00ffff
114#define R_SSYM_SHIFT 24
115#define R_SSYM_MASK 0x00ffffff
116
117 // N64 relocation type accessors
118 uint8_t getRType(uint32_t Type) const {
119 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
120 }
121 uint8_t getRType2(uint32_t Type) const {
122 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
123 }
124 uint8_t getRType3(uint32_t Type) const {
125 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
126 }
127 uint8_t getRSsym(uint32_t Type) const {
128 return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
129 }
130
131 // N64 relocation type setting
132 static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3) {
133 return ((Value1 & 0xff) << R_TYPE_SHIFT) |
134 ((Value2 & 0xff) << R_TYPE2_SHIFT) |
135 ((Value3 & 0xff) << R_TYPE3_SHIFT);
136 }
137 unsigned setRSsym(unsigned Value, unsigned Type) const {
138 return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
139 }
140
141 // On AArch64, return a new section to be added to the ELF object that
142 // contains relocations used to describe every symbol that should have memory
143 // tags applied. Returns nullptr if no such section is necessary (i.e. there's
144 // no tagged globals).
146 return nullptr;
147 }
148};
149
150/// Construct a new ELF writer instance.
151///
152/// \param MOTW - The target specific ELF writer subclass.
153/// \param OS - The stream to write to.
154/// \returns The constructed object writer.
155std::unique_ptr<MCObjectWriter>
156createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
157 raw_pwrite_stream &OS, bool IsLittleEndian);
158
159std::unique_ptr<MCObjectWriter>
160createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
161 raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
162 bool IsLittleEndian);
163
164} // end namespace llvm
165
166#endif // LLVM_MC_MCELFOBJECTWRITER_H
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:529
Symbol * Sym
Definition: ELF_riscv.cpp:479
#define R_TYPE2_SHIFT
#define R_SSYM_MASK
#define R_TYPE_SHIFT
#define R_TYPE3_SHIFT
#define R_SSYM_SHIFT
PowerPC TLS Dynamic Call Fixup
raw_pwrite_stream & OS
Context object for machine code objects.
Definition: MCContext.h:81
static bool classof(const MCObjectTargetWriter *W)
virtual void sortRelocs(const MCAssembler &Asm, std::vector< ELFRelocationEntry > &Relocs)
virtual bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym, unsigned Type) const
Triple::ObjectFormatType getFormat() const override
virtual MCSectionELF * getMemtagRelocsSection(MCContext &Ctx) const
static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3)
uint8_t getRType(uint32_t Type) const
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
uint8_t getRSsym(uint32_t Type) const
virtual ~MCELFObjectTargetWriter()=default
virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec)
static uint8_t getOSABI(Triple::OSType OSType)
unsigned setRSsym(unsigned Value, unsigned Type) const
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:71
Base class for classes that define behaviour that is specific to both the target and the object forma...
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:26
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:40
This represents an "assembler immediate".
Definition: MCValue.h:36
Target - Wrapper for Target specific information.
@ HermitCore
Definition: Triple.h:222
ObjectFormatType
Definition: Triple.h:285
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ ELFOSABI_SOLARIS
Definition: ELF.h:347
@ ELFOSABI_FREEBSD
Definition: ELF.h:350
@ ELFOSABI_STANDALONE
Definition: ELF.h:368
@ ELFOSABI_NONE
Definition: ELF.h:341
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
std::unique_ptr< MCObjectWriter > createELFDwoObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, bool IsLittleEndian)
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 *OriginalSymbol, uint64_t OriginalAddend)
const MCSymbolELF * OriginalSymbol
const MCSymbolELF * Symbol
void print(raw_ostream &Out) const
LLVM_DUMP_METHOD void dump() const