LLVM  13.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/Triple.h"
13 #include "llvm/BinaryFormat/ELF.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/MC/MCSectionELF.h"
16 #include "llvm/Support/Casting.h"
18 #include <cstdint>
19 #include <vector>
20 
21 namespace llvm {
22 
23 class MCAssembler;
24 class MCContext;
25 class MCFixup;
26 class MCSymbol;
27 class MCSymbolELF;
28 class 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 
38  ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
39  uint64_t Addend, const MCSymbolELF *OriginalSymbol,
40  uint64_t OriginalAddend)
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  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 
60 protected:
61  MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
62  bool HasRelocationAddend_, uint8_t ABIVersion_ = 0);
63 
64 public:
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) {
74  case Triple::CloudABI:
76  case Triple::HermitCore:
78  case Triple::PS4:
79  case Triple::FreeBSD:
80  return ELF::ELFOSABI_FREEBSD;
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 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 
142 /// Construct a new ELF writer instance.
143 ///
144 /// \param MOTW - The target specific ELF writer subclass.
145 /// \param OS - The stream to write to.
146 /// \returns The constructed object writer.
147 std::unique_ptr<MCObjectWriter>
148 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
149  raw_pwrite_stream &OS, bool IsLittleEndian);
150 
151 std::unique_ptr<MCObjectWriter>
152 createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
153  raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
154  bool IsLittleEndian);
155 
156 } // end namespace llvm
157 
158 #endif // LLVM_MC_MCELFOBJECTWRITER_H
llvm::ELFRelocationEntry::OriginalSymbol
const MCSymbolELF * OriginalSymbol
Definition: MCELFObjectWriter.h:35
llvm
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::createELFObjectWriter
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
Definition: ELFObjectWriter.cpp:1524
llvm::Triple::HermitCore
@ HermitCore
Definition: Triple.h:198
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:71
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:124
MCSectionELF.h
llvm::RISCVFenceField::W
@ W
Definition: RISCVBaseInfo.h:134
llvm::MCELFObjectTargetWriter::getFormat
Triple::ObjectFormatType getFormat() const override
Definition: MCELFObjectWriter.h:67
llvm::MCELFObjectTargetWriter::getRType3
uint8_t getRType3(uint32_t Type) const
Definition: MCELFObjectWriter.h:124
llvm::MCELFObjectTargetWriter::getABIVersion
uint8_t getABIVersion() const
Definition: MCELFObjectWriter.h:100
R_TYPE2_SHIFT
#define R_TYPE2_SHIFT
Definition: MCELFObjectWriter.h:110
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:46
llvm::MCELFObjectTargetWriter::setRSsym
unsigned setRSsym(unsigned Value, unsigned Type) const
Definition: MCELFObjectWriter.h:137
R_SSYM_MASK
#define R_SSYM_MASK
Definition: MCELFObjectWriter.h:115
llvm::ELFRelocationEntry::print
void print(raw_ostream &Out) const
Definition: MCELFObjectWriter.h:44
llvm::ELF::ELFOSABI_STANDALONE
@ ELFOSABI_STANDALONE
Definition: ELF.h:364
llvm::MCELFObjectTargetWriter::hasRelocationAddend
bool hasRelocationAddend() const
Definition: MCELFObjectWriter.h:102
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:892
llvm::ELFRelocationEntry
Definition: MCELFObjectWriter.h:30
llvm::MCSectionELF
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:28
llvm::MCELFObjectTargetWriter::sortRelocs
virtual void sortRelocs(const MCAssembler &Asm, std::vector< ELFRelocationEntry > &Relocs)
Definition: MCELFObjectTargetWriter.cpp:26
llvm::MCSymbolELF
Definition: MCSymbolELF.h:14
llvm::ELFRelocationEntry::dump
void dump() const
Definition: MCELFObjectWriter.h:50
llvm::ELFRelocationEntry::Symbol
const MCSymbolELF * Symbol
Definition: MCELFObjectWriter.h:32
llvm::Triple::FreeBSD
@ FreeBSD
Definition: Triple.h:171
ELF.h
llvm::MCObjectTargetWriter
Base class for classes that define behaviour that is specific to both the target and the object forma...
Definition: MCObjectWriter.h:112
llvm::Triple::PS4
@ PS4
Definition: Triple.h:191
llvm::MCELFObjectTargetWriter::setRTypes
static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3)
Definition: MCELFObjectWriter.h:132
llvm::MCELFObjectTargetWriter
Definition: MCELFObjectWriter.h:53
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::ELFRelocationEntry::ELFRelocationEntry
ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type, uint64_t Addend, const MCSymbolELF *OriginalSymbol, uint64_t OriginalAddend)
Definition: MCELFObjectWriter.h:38
llvm::MCELFObjectTargetWriter::getRSsym
uint8_t getRSsym(uint32_t Type) const
Definition: MCELFObjectWriter.h:127
llvm::MCELFObjectTargetWriter::getRType
uint8_t getRType(uint32_t Type) const
Definition: MCELFObjectWriter.h:118
llvm::MCELFObjectTargetWriter::MCELFObjectTargetWriter
MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_, bool HasRelocationAddend_, uint8_t ABIVersion_=0)
Definition: MCELFObjectTargetWriter.cpp:13
llvm::Triple::ELF
@ ELF
Definition: Triple.h:235
llvm::MCAssembler
Definition: MCAssembler.h:60
llvm::ELF::ELFOSABI_FREEBSD
@ ELFOSABI_FREEBSD
Definition: ELF.h:348
llvm::MCELFObjectTargetWriter::is64Bit
bool is64Bit() const
Definition: MCELFObjectWriter.h:103
llvm::ELFRelocationEntry::Offset
uint64_t Offset
Definition: MCELFObjectWriter.h:31
llvm::ELFRelocationEntry::Type
unsigned Type
Definition: MCELFObjectWriter.h:33
llvm::ELF::ELFOSABI_NONE
@ ELFOSABI_NONE
Definition: ELF.h:339
llvm::ELF::ELFOSABI_CLOUDABI
@ ELFOSABI_CLOUDABI
Definition: ELF.h:356
R_TYPE_SHIFT
#define R_TYPE_SHIFT
Definition: MCELFObjectWriter.h:108
llvm::MCELFObjectTargetWriter::addTargetSectionFlags
virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec)
Definition: MCELFObjectTargetWriter.cpp:30
Triple.h
llvm::MCELFObjectTargetWriter::getOSABI
uint8_t getOSABI() const
Definition: MCELFObjectWriter.h:99
R_TYPE3_SHIFT
#define R_TYPE3_SHIFT
Definition: MCELFObjectWriter.h:112
llvm::ELFRelocationEntry::Addend
uint64_t Addend
Definition: MCELFObjectWriter.h:34
uint32_t
llvm::Triple::ObjectFormatType
ObjectFormatType
Definition: Triple.h:231
MCObjectWriter.h
llvm::ELFRelocationEntry::OriginalAddend
uint64_t OriginalAddend
Definition: MCELFObjectWriter.h:36
llvm::Triple::OSType
OSType
Definition: Triple.h:164
llvm::MCELFObjectTargetWriter::needsRelocateWithSymbol
virtual bool needsRelocateWithSymbol(const MCSymbol &Sym, unsigned Type) const
Definition: MCELFObjectTargetWriter.cpp:20
uint16_t
R_SSYM_SHIFT
#define R_SSYM_SHIFT
Definition: MCELFObjectWriter.h:114
llvm::MCELFObjectTargetWriter::getOSABI
static uint8_t getOSABI(Triple::OSType OSType)
Definition: MCELFObjectWriter.h:72
Casting.h
llvm::MCELFObjectTargetWriter::getEMachine
uint16_t getEMachine() const
Definition: MCELFObjectWriter.h:101
llvm::createELFDwoObjectWriter
std::unique_ptr< MCObjectWriter > createELFDwoObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, bool IsLittleEndian)
Definition: ELFObjectWriter.cpp:1531
llvm::MCELFObjectTargetWriter::classof
static bool classof(const MCObjectTargetWriter *W)
Definition: MCELFObjectWriter.h:68
llvm::Triple::CloudABI
@ CloudABI
Definition: Triple.h:168
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::MCValue
This represents an "assembler immediate".
Definition: MCValue.h:37
llvm::MCELFObjectTargetWriter::getRType2
uint8_t getRType2(uint32_t Type) const
Definition: MCELFObjectWriter.h:121
llvm::MCELFObjectTargetWriter::getRelocType
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
raw_ostream.h
llvm::MCFixup
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:81
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::MCELFObjectTargetWriter::~MCELFObjectTargetWriter
virtual ~MCELFObjectTargetWriter()=default
llvm::ISD::MCSymbol
@ MCSymbol
Definition: ISDOpcodes.h:165