LLVM 22.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 MCContext;
29class MCFixup;
30class MCSymbol;
31class MCSymbolELF;
32class MCTargetOptions;
33class MCValue;
34
36 uint64_t Offset; // Where is the relocation.
37 const MCSymbolELF *Symbol; // The symbol to relocate with.
38 unsigned Type; // The type of the relocation.
39 uint64_t Addend; // The addend to use.
40
44
45 void print(raw_ostream &Out) const {
46 Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
47 << ", Addend=" << Addend;
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
73 switch (OSType) {
76 case Triple::PS4:
77 case Triple::FreeBSD:
79 case Triple::Solaris:
81 case Triple::OpenBSD:
83 default:
84 return ELF::ELFOSABI_NONE;
85 }
86 }
87
88 virtual unsigned getRelocType(const MCFixup &Fixup, const MCValue &Target,
89 bool IsPCRel) const = 0;
90
91 virtual bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const {
92 return false;
93 }
94
95 virtual void sortRelocs(std::vector<ELFRelocationEntry> &Relocs);
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
119 return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
120 }
122 return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
123 }
125 return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
126 }
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
142class ELFObjectWriter final : public MCObjectWriter {
143 unsigned ELFHeaderEFlags = 0;
144
145public:
146 std::unique_ptr<MCELFObjectTargetWriter> TargetObjectWriter;
149
152 // .weakref aliases
154 bool IsLittleEndian = false;
155 bool SeenGnuAbi = false;
156 std::optional<uint8_t> OverrideABIVersion;
157
158 struct Symver {
160 const MCSymbol *Sym;
162 // True if .symver *, *@@@* or .symver *, *, remove.
164 };
166
167 ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
169 ELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
171 bool IsLittleEndian);
172
173 void reset() override;
174 void setAssembler(MCAssembler *Asm) override;
175 void executePostLayoutBinding() override;
176 void recordRelocation(const MCFragment &F, const MCFixup &Fixup,
177 MCValue Target, uint64_t &FixedValue) override;
179 const MCFragment &FB, bool InSet,
180 bool IsPCRel) const override;
181 uint64_t writeObject() override;
182
183 bool hasRelocationAddend() const;
184 bool usesRela(const MCTargetOptions *TO, const MCSectionELF &Sec) const;
185
186 bool useSectionSymbol(const MCValue &Val, const MCSymbolELF *Sym, uint64_t C,
187 unsigned Type) const;
188
189 bool checkRelocation(SMLoc Loc, const MCSectionELF *From,
190 const MCSectionELF *To);
191
192 unsigned getELFHeaderEFlags() const { return ELFHeaderEFlags; }
193 void setELFHeaderEFlags(unsigned Flags) { ELFHeaderEFlags = Flags; }
194
195 // Mark that we have seen GNU ABI usage (e.g. SHF_GNU_RETAIN, STB_GNU_UNIQUE).
196 void markGnuAbi() { SeenGnuAbi = true; }
197 bool seenGnuAbi() const { return SeenGnuAbi; }
198
199 // Override the default e_ident[EI_ABIVERSION] in the ELF header.
201};
202} // end namespace llvm
203
204#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:638
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:55
PowerPC TLS Dynamic Call Fixup
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
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 usesRela(const MCTargetOptions *TO, const MCSectionELF &Sec) const
bool useSectionSymbol(const MCValue &Val, const MCSymbolELF *Sym, uint64_t C, unsigned Type) const
DenseMap< const MCSymbolELF *, const MCSymbolELF * > Renames
Context object for machine code objects.
Definition MCContext.h:83
static bool classof(const MCObjectTargetWriter *W)
virtual unsigned getRelocType(const MCFixup &Fixup, const MCValue &Target, bool IsPCRel) const =0
Triple::ObjectFormatType getFormat() const override
static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3)
uint8_t getRType(uint32_t Type) const
uint8_t getRSsym(uint32_t Type) const
virtual ~MCELFObjectTargetWriter()=default
virtual bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const
static uint8_t getOSABI(Triple::OSType OSType)
unsigned setRSsym(unsigned Value, unsigned Type) const
virtual void sortRelocs(std::vector< ELFRelocationEntry > &Relocs)
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:61
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:23
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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:45
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.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ ELFOSABI_OPENBSD
Definition ELF.h:358
@ ELFOSABI_SOLARIS
Definition ELF.h:352
@ ELFOSABI_FREEBSD
Definition ELF.h:355
@ ELFOSABI_STANDALONE
Definition ELF.h:374
@ ELFOSABI_NONE
Definition ELF.h:346
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