LLVM  7.0.0svn
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/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 MCObjectWriter;
27 class MCSymbol;
28 class MCSymbolELF;
29 class MCValue;
30 
32  uint64_t Offset; // Where is the relocation.
33  const MCSymbolELF *Symbol; // The symbol to relocate with.
34  unsigned Type; // The type of the relocation.
35  uint64_t Addend; // The addend to use.
36  const MCSymbolELF *OriginalSymbol; // The original value of Symbol if we changed it.
37  uint64_t OriginalAddend; // The original value of addend.
38 
39  ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
40  uint64_t Addend, const MCSymbolELF *OriginalSymbol,
41  uint64_t OriginalAddend)
42  : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend),
43  OriginalSymbol(OriginalSymbol), OriginalAddend(OriginalAddend) {}
44 
45  void print(raw_ostream &Out) const {
46  Out << "Off=" << Offset << ", Sym=" << Symbol << ", Type=" << Type
47  << ", Addend=" << Addend << ", OriginalSymbol=" << OriginalSymbol
48  << ", OriginalAddend=" << OriginalAddend;
49  }
50 
51  void dump() const { print(errs()); }
52 };
53 
55  const uint8_t OSABI;
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);
63 
64 public:
65  virtual ~MCELFObjectTargetWriter() = default;
66 
67  virtual Triple::ObjectFormatType getFormat() const { 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::PS4:
77  case Triple::FreeBSD:
78  return ELF::ELFOSABI_FREEBSD;
79  default:
80  return ELF::ELFOSABI_NONE;
81  }
82  }
83 
84  virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
85  const MCFixup &Fixup, bool IsPCRel) const = 0;
86 
87  virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
88  unsigned Type) const;
89 
90  virtual void sortRelocs(const MCAssembler &Asm,
91  std::vector<ELFRelocationEntry> &Relocs);
92 
93  /// \name Accessors
94  /// @{
95  uint8_t getOSABI() const { return OSABI; }
96  uint16_t getEMachine() const { return EMachine; }
97  bool hasRelocationAddend() const { return HasRelocationAddend; }
98  bool is64Bit() const { return Is64Bit; }
99  /// @}
100 
101  // Instead of changing everyone's API we pack the N64 Type fields
102  // into the existing 32 bit data unsigned.
103 #define R_TYPE_SHIFT 0
104 #define R_TYPE_MASK 0xffffff00
105 #define R_TYPE2_SHIFT 8
106 #define R_TYPE2_MASK 0xffff00ff
107 #define R_TYPE3_SHIFT 16
108 #define R_TYPE3_MASK 0xff00ffff
109 #define R_SSYM_SHIFT 24
110 #define R_SSYM_MASK 0x00ffffff
111 
112  // N64 relocation type accessors
113  uint8_t getRType(uint32_t Type) const {
114  return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
115  }
116  uint8_t getRType2(uint32_t Type) const {
117  return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
118  }
119  uint8_t getRType3(uint32_t Type) const {
120  return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
121  }
122  uint8_t getRSsym(uint32_t Type) const {
123  return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
124  }
125 
126  // N64 relocation type setting
127  unsigned setRType(unsigned Value, unsigned Type) const {
128  return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
129  }
130  unsigned setRType2(unsigned Value, unsigned Type) const {
131  return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
132  }
133  unsigned setRType3(unsigned Value, unsigned Type) const {
134  return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
135  }
136  unsigned setRSsym(unsigned Value, unsigned Type) const {
137  return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
138  }
139 };
140 
141 /// Construct a new ELF writer instance.
142 ///
143 /// \param MOTW - The target specific ELF writer subclass.
144 /// \param OS - The stream to write to.
145 /// \returns The constructed object writer.
146 std::unique_ptr<MCObjectWriter>
147 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
148  raw_pwrite_stream &OS, bool IsLittleEndian);
149 
150 std::unique_ptr<MCObjectWriter>
151 createELFDwoObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
153  bool IsLittleEndian);
154 
155 } // end namespace llvm
156 
157 #endif // LLVM_MC_MCELFOBJECTWRITER_H
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Compute iterated dominance frontiers using a linear time algorithm.
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
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:211
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