LLVM  6.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/Support/Casting.h"
17 #include <cstdint>
18 #include <vector>
19 
20 namespace llvm {
21 
22 class MCAssembler;
23 class MCContext;
24 class MCFixup;
25 class MCObjectWriter;
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)
41  : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend),
42  OriginalSymbol(OriginalSymbol), OriginalAddend(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 uint16_t EMachine;
56  const unsigned HasRelocationAddend : 1;
57  const unsigned Is64Bit : 1;
58 
59 protected:
60  MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_,
61  bool HasRelocationAddend);
62 
63 public:
64  virtual ~MCELFObjectTargetWriter() = default;
65 
66  static uint8_t getOSABI(Triple::OSType OSType) {
67  switch (OSType) {
68  case Triple::CloudABI:
70  case Triple::PS4:
71  case Triple::FreeBSD:
72  return ELF::ELFOSABI_FREEBSD;
73  default:
74  return ELF::ELFOSABI_NONE;
75  }
76  }
77 
78  virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
79  const MCFixup &Fixup, bool IsPCRel) const = 0;
80 
81  virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
82  unsigned Type) const;
83 
84  virtual void sortRelocs(const MCAssembler &Asm,
85  std::vector<ELFRelocationEntry> &Relocs);
86 
87  /// \name Accessors
88  /// @{
89  uint8_t getOSABI() const { return OSABI; }
90  uint16_t getEMachine() const { return EMachine; }
91  bool hasRelocationAddend() const { return HasRelocationAddend; }
92  bool is64Bit() const { return Is64Bit; }
93  /// @}
94 
95  // Instead of changing everyone's API we pack the N64 Type fields
96  // into the existing 32 bit data unsigned.
97 #define R_TYPE_SHIFT 0
98 #define R_TYPE_MASK 0xffffff00
99 #define R_TYPE2_SHIFT 8
100 #define R_TYPE2_MASK 0xffff00ff
101 #define R_TYPE3_SHIFT 16
102 #define R_TYPE3_MASK 0xff00ffff
103 #define R_SSYM_SHIFT 24
104 #define R_SSYM_MASK 0x00ffffff
105 
106  // N64 relocation type accessors
107  uint8_t getRType(uint32_t Type) const {
108  return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
109  }
110  uint8_t getRType2(uint32_t Type) const {
111  return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
112  }
113  uint8_t getRType3(uint32_t Type) const {
114  return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
115  }
116  uint8_t getRSsym(uint32_t Type) const {
117  return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
118  }
119 
120  // N64 relocation type setting
121  unsigned setRType(unsigned Value, unsigned Type) const {
122  return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
123  }
124  unsigned setRType2(unsigned Value, unsigned Type) const {
125  return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
126  }
127  unsigned setRType3(unsigned Value, unsigned Type) const {
128  return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
129  }
130  unsigned setRSsym(unsigned Value, unsigned Type) const {
131  return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
132  }
133 };
134 
135 /// \brief Construct a new ELF writer instance.
136 ///
137 /// \param MOTW - The target specific ELF writer subclass.
138 /// \param OS - The stream to write to.
139 /// \returns The constructed object writer.
140 std::unique_ptr<MCObjectWriter>
141 createELFObjectWriter(std::unique_ptr<MCELFObjectTargetWriter> MOTW,
142  raw_pwrite_stream &OS, bool IsLittleEndian);
143 
144 } // end namespace llvm
145 
146 #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
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:66
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:59
#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)
Target - Wrapper for Target specific information.
#define R_TYPE3_MASK
#define R_TYPE_MASK
void print(raw_ostream &Out) const
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:337
const MCSymbolELF * OriginalSymbol
#define R_TYPE_SHIFT
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:44
#define R_SSYM_SHIFT
const MCSymbolELF * Symbol
uint8_t getRType(uint32_t Type) const