LLVM  8.0.1
MCELFObjectWriter.h
Go to the documentation of this file.
1 //===- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_MC_MCELFOBJECTWRITER_H
11 #define LLVM_MC_MCELFOBJECTWRITER_H
12 
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/BinaryFormat/ELF.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSectionELF.h"
17 #include "llvm/Support/Casting.h"
19 #include <cstdint>
20 #include <vector>
21 
22 namespace llvm {
23 
24 class MCAssembler;
25 class MCContext;
26 class MCFixup;
27 class MCObjectWriter;
28 class MCSymbol;
29 class MCSymbolELF;
30 class MCValue;
31 
33  uint64_t Offset; // Where is the relocation.
34  const MCSymbolELF *Symbol; // The symbol to relocate with.
35  unsigned Type; // The type of the relocation.
36  uint64_t Addend; // The addend to use.
37  const MCSymbolELF *OriginalSymbol; // The original value of Symbol if we changed it.
38  uint64_t OriginalAddend; // The original value of addend.
39 
40  ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
41  uint64_t Addend, const MCSymbolELF *OriginalSymbol,
42  uint64_t OriginalAddend)
43  : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend),
44  OriginalSymbol(OriginalSymbol), OriginalAddend(OriginalAddend) {}
45 
46  void print(raw_ostream &Out) const {
47  Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
48  << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
49  << ", OriginalAddend=" << OriginalAddend;
50  }
51 
52  void dump() const { print(errs()); }
53 };
54 
56  const uint8_t OSABI;
57  const uint16_t EMachine;
58  const unsigned HasRelocationAddend : 1;
59  const unsigned Is64Bit : 1;
60 
61 protected:
62  MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
63  bool HasRelocationAddend);
64 
65 public:
66  virtual ~MCELFObjectTargetWriter() = default;
67 
68  virtual Triple::ObjectFormatType getFormat() const { return Triple::ELF; }
69  static bool classof(const MCObjectTargetWriter *W) {
70  return W->getFormat() == Triple::ELF;
71  }
72 
73  static uint8_t getOSABI(Triple::OSType OSType) {
74  switch (OSType) {
75  case Triple::CloudABI:
77  case Triple::HermitCore:
79  case Triple::PS4:
80  case Triple::FreeBSD:
81  return ELF::ELFOSABI_FREEBSD;
82  default:
83  return ELF::ELFOSABI_NONE;
84  }
85  }
86 
87  virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
88  const MCFixup &Fixup, bool IsPCRel) const = 0;
89 
90  virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
91  unsigned Type) const;
92 
93  virtual void sortRelocs(const MCAssembler &Asm,
94  std::vector<ELFRelocationEntry> &Relocs);
95 
96  virtual void addTargetSectionFlags(MCContext &Ctx, MCSectionELF &Sec);
97 
98  /// \name Accessors
99  /// @{
100  uint8_t getOSABI() const { return OSABI; }
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  unsigned setRType(unsigned Value, unsigned Type) const {
133  return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
134  }
135  unsigned setRType2(unsigned Value, unsigned Type) const {
136  return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
137  }
138  unsigned setRType3(unsigned Value, unsigned Type) const {
139  return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
140  }
141  unsigned setRSsym(unsigned Value, unsigned Type) const {
142  return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
143  }
144 };
145 
146 /// Construct a new ELF writer instance.
147 ///
148 /// \param MOTW - The target specific ELF writer subclass.
149 /// \param OS - The stream to write to.
150 /// \returns The constructed object writer.
151 std::unique_ptr<MCObjectWriter>
152 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
153  raw_pwrite_stream &OS, bool IsLittleEndian);
154 
155 std::unique_ptr<MCObjectWriter>
156 createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
158  bool IsLittleEndian);
159 
160 } // end namespace llvm
161 
162 #endif // LLVM_MC_MCELFOBJECTWRITER_H
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
uint8_t getRType2(uint32_t Type) const
This represents an "assembler immediate".
Definition: MCValue.h:40
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
unsigned setRSsym(unsigned Value, unsigned Type) const
virtual Triple::ObjectFormatType getFormat() 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:74
virtual Triple::ObjectFormatType getFormat() const =0
static uint8_t getOSABI(Triple::OSType OSType)
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
Context object for machine code objects.
Definition: MCContext.h:63
#define R_TYPE2_MASK
std::unique_ptr< MCObjectWriter > createELFObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
#define R_TYPE3_SHIFT
#define R_SSYM_MASK
unsigned setRType(unsigned Value, unsigned Type) const
unsigned setRType2(unsigned Value, unsigned Type) const
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
uint8_t getRSsym(uint32_t Type) const
#define R_TYPE2_SHIFT
unsigned setRType3(unsigned Value, unsigned Type) const
PowerPC TLS Dynamic Call Fixup
ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type, uint64_t Addend, const MCSymbolELF *OriginalSymbol, uint64_t OriginalAddend)
std::unique_ptr< MCObjectWriter > createELFDwoObjectWriter(std::unique_ptr< MCELFObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS, bool IsLittleEndian)
Target - Wrapper for Target specific information.
static bool classof(const MCObjectTargetWriter *W)
#define R_TYPE3_MASK
#define R_TYPE_MASK
void print(raw_ostream &Out) const
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:28
Base class for classes that define behaviour that is specific to both the target and the object forma...
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:341
const MCSymbolELF * OriginalSymbol
#define R_TYPE_SHIFT
ObjectFormatType
Definition: Triple.h:216
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
#define R_SSYM_SHIFT
const MCSymbolELF * Symbol
uint8_t getRType(uint32_t Type) const