LLVM  14.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 
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 
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  case Triple::Solaris:
82  return ELF::ELFOSABI_SOLARIS;
83  default:
84  return ELF::ELFOSABI_NONE;
85  }
86  }
87 
88  virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
89  const MCFixup &Fixup, bool IsPCRel) const = 0;
90 
91  virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
92  unsigned Type) const;
93 
94  virtual void sortRelocs(const MCAssembler &Asm,
95  std::vector<ELFRelocationEntry> &Relocs);
96 
97  virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec);
98 
99  /// \name Accessors
100  /// @{
101  uint8_t getOSABI() const { return OSABI; }
102  uint8_t getABIVersion() const { return ABIVersion; }
103  uint16_t getEMachine() const { return EMachine; }
104  bool hasRelocationAddend() const { return HasRelocationAddend; }
105  bool is64Bit() const { return Is64Bit; }
106  /// @}
107 
108  // Instead of changing everyone's API we pack the N64 Type fields
109  // into the existing 32 bit data unsigned.
110 #define R_TYPE_SHIFT 0
111 #define R_TYPE_MASK 0xffffff00
112 #define R_TYPE2_SHIFT 8
113 #define R_TYPE2_MASK 0xffff00ff
114 #define R_TYPE3_SHIFT 16
115 #define R_TYPE3_MASK 0xff00ffff
116 #define R_SSYM_SHIFT 24
117 #define R_SSYM_MASK 0x00ffffff
118 
119  // N64 relocation type accessors
120  uint8_t getRType(uint32_t Type) const {
121  return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
122  }
123  uint8_t getRType2(uint32_t Type) const {
124  return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
125  }
126  uint8_t getRType3(uint32_t Type) const {
127  return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
128  }
129  uint8_t getRSsym(uint32_t Type) const {
130  return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
131  }
132 
133  // N64 relocation type setting
134  static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3) {
135  return ((Value1 & 0xff) << R_TYPE_SHIFT) |
136  ((Value2 & 0xff) << R_TYPE2_SHIFT) |
137  ((Value3 & 0xff) << R_TYPE3_SHIFT);
138  }
139  unsigned setRSsym(unsigned Value, unsigned Type) const {
140  return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
141  }
142 };
143 
144 /// Construct a new ELF writer instance.
145 ///
146 /// \param MOTW - The target specific ELF writer subclass.
147 /// \param OS - The stream to write to.
148 /// \returns The constructed object writer.
149 std::unique_ptr<MCObjectWriter>
150 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
151  raw_pwrite_stream &OS, bool IsLittleEndian);
152 
153 std::unique_ptr<MCObjectWriter>
154 createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
155  raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS,
156  bool IsLittleEndian);
157 
158 } // end namespace llvm
159 
160 #endif // LLVM_MC_MCELFOBJECTWRITER_H
llvm::ELFRelocationEntry::OriginalSymbol
const MCSymbolELF * OriginalSymbol
Definition: MCELFObjectWriter.h:35
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:506
llvm
This file implements support for optimizing divisions by a constant.
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:1512
llvm::Triple::HermitCore
@ HermitCore
Definition: Triple.h:201
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:72
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:137
MCSectionELF.h
llvm::RISCVFenceField::W
@ W
Definition: RISCVBaseInfo.h:199
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:126
llvm::MCELFObjectTargetWriter::getABIVersion
uint8_t getABIVersion() const
Definition: MCELFObjectWriter.h:102
R_TYPE2_SHIFT
#define R_TYPE2_SHIFT
Definition: MCELFObjectWriter.h:112
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::ELF::ELFOSABI_SOLARIS
@ ELFOSABI_SOLARIS
Definition: ELF.h:347
llvm::MCELFObjectTargetWriter::setRSsym
unsigned setRSsym(unsigned Value, unsigned Type) const
Definition: MCELFObjectWriter.h:139
R_SSYM_MASK
#define R_SSYM_MASK
Definition: MCELFObjectWriter.h:117
llvm::ELFRelocationEntry::print
void print(raw_ostream &Out) const
Definition: MCELFObjectWriter.h:44
llvm::MCELFObjectTargetWriter::hasRelocationAddend
bool hasRelocationAddend() const
Definition: MCELFObjectWriter.h:104
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:893
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::Symbol
const MCSymbolELF * Symbol
Definition: MCELFObjectWriter.h:32
llvm::Triple::FreeBSD
@ FreeBSD
Definition: Triple.h:174
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::ELF::ELFOSABI_FREEBSD
@ ELFOSABI_FREEBSD
Definition: ELF.h:350
llvm::Triple::PS4
@ PS4
Definition: Triple.h:194
llvm::MCELFObjectTargetWriter::setRTypes
static unsigned setRTypes(unsigned Value1, unsigned Value2, unsigned Value3)
Definition: MCELFObjectWriter.h:134
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:53
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:129
llvm::MCELFObjectTargetWriter::getRType
uint8_t getRType(uint32_t Type) const
Definition: MCELFObjectWriter.h:120
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:238
llvm::ELF::ELFOSABI_STANDALONE
@ ELFOSABI_STANDALONE
Definition: ELF.h:366
llvm::MCAssembler
Definition: MCAssembler.h:60
uint64_t
llvm::MCELFObjectTargetWriter::is64Bit
bool is64Bit() const
Definition: MCELFObjectWriter.h:105
llvm::Triple::Solaris
@ Solaris
Definition: Triple.h:183
llvm::ELF::ELFOSABI_CLOUDABI
@ ELFOSABI_CLOUDABI
Definition: ELF.h:358
llvm::ELFRelocationEntry::Offset
uint64_t Offset
Definition: MCELFObjectWriter.h:31
llvm::ELFRelocationEntry::Type
unsigned Type
Definition: MCELFObjectWriter.h:33
R_TYPE_SHIFT
#define R_TYPE_SHIFT
Definition: MCELFObjectWriter.h:110
llvm::ELFRelocationEntry::dump
LLVM_DUMP_METHOD void dump() const
Definition: MCELFObjectWriter.h:50
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:101
R_TYPE3_SHIFT
#define R_TYPE3_SHIFT
Definition: MCELFObjectWriter.h:114
llvm::ELFRelocationEntry::Addend
uint64_t Addend
Definition: MCELFObjectWriter.h:34
uint32_t
llvm::Triple::ObjectFormatType
ObjectFormatType
Definition: Triple.h:234
MCObjectWriter.h
llvm::ELFRelocationEntry::OriginalAddend
uint64_t OriginalAddend
Definition: MCELFObjectWriter.h:36
llvm::Triple::OSType
OSType
Definition: Triple.h:167
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:116
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:103
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:1519
llvm::MCELFObjectTargetWriter::classof
static bool classof(const MCObjectTargetWriter *W)
Definition: MCELFObjectWriter.h:68
llvm::Triple::CloudABI
@ CloudABI
Definition: Triple.h:171
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:123
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:71
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::ELF::ELFOSABI_NONE
@ ELFOSABI_NONE
Definition: ELF.h:341
llvm::MCELFObjectTargetWriter::~MCELFObjectTargetWriter
virtual ~MCELFObjectTargetWriter()=default
llvm::ISD::MCSymbol
@ MCSymbol
Definition: ISDOpcodes.h:172