LLVM  4.0.0
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/MC/MCValue.h"
15 #include "llvm/Support/DataTypes.h"
16 #include "llvm/Support/ELF.h"
18 #include <vector>
19 
20 namespace llvm {
21 class MCAssembler;
22 class MCContext;
23 class MCFixup;
24 class MCFragment;
25 class MCObjectWriter;
26 class MCSymbol;
27 class MCSymbolELF;
28 class MCValue;
29 class raw_pwrite_stream;
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  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  const unsigned IsN64 : 1;
59 
60 protected:
61 
62  MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
63  uint16_t EMachine_, bool HasRelocationAddend,
64  bool IsN64=false);
65 
66 public:
67  static uint8_t getOSABI(Triple::OSType OSType) {
68  switch (OSType) {
69  case Triple::CloudABI:
71  case Triple::PS4:
72  case Triple::FreeBSD:
73  return ELF::ELFOSABI_FREEBSD;
74  default:
75  return ELF::ELFOSABI_NONE;
76  }
77  }
78 
80 
81  virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
82  const MCFixup &Fixup, bool IsPCRel) const = 0;
83 
84  virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
85  unsigned Type) const;
86 
87  virtual void sortRelocs(const MCAssembler &Asm,
88  std::vector<ELFRelocationEntry> &Relocs);
89 
90  /// \name Accessors
91  /// @{
92  uint8_t getOSABI() const { return OSABI; }
93  uint16_t getEMachine() const { return EMachine; }
94  bool hasRelocationAddend() const { return HasRelocationAddend; }
95  bool is64Bit() const { return Is64Bit; }
96  bool isN64() const { return IsN64; }
97  /// @}
98 
99  // Instead of changing everyone's API we pack the N64 Type fields
100  // into the existing 32 bit data unsigned.
101 #define R_TYPE_SHIFT 0
102 #define R_TYPE_MASK 0xffffff00
103 #define R_TYPE2_SHIFT 8
104 #define R_TYPE2_MASK 0xffff00ff
105 #define R_TYPE3_SHIFT 16
106 #define R_TYPE3_MASK 0xff00ffff
107 #define R_SSYM_SHIFT 24
108 #define R_SSYM_MASK 0x00ffffff
109 
110  // N64 relocation type accessors
111  uint8_t getRType(uint32_t Type) const {
112  return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
113  }
114  uint8_t getRType2(uint32_t Type) const {
115  return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
116  }
117  uint8_t getRType3(uint32_t Type) const {
118  return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
119  }
120  uint8_t getRSsym(uint32_t Type) const {
121  return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
122  }
123 
124  // N64 relocation type setting
125  unsigned setRType(unsigned Value, unsigned Type) const {
126  return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
127  }
128  unsigned setRType2(unsigned Value, unsigned Type) const {
129  return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
130  }
131  unsigned setRType3(unsigned Value, unsigned Type) const {
132  return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
133  }
134  unsigned setRSsym(unsigned Value, unsigned Type) const {
135  return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
136  }
137 };
138 
139 /// \brief Construct a new ELF writer instance.
140 ///
141 /// \param MOTW - The target specific ELF writer subclass.
142 /// \param OS - The stream to write to.
143 /// \returns The constructed object writer.
144 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
145  raw_pwrite_stream &OS,
146  bool IsLittleEndian);
147 } // End llvm namespace
148 
149 #endif
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
This represents an "assembler immediate".
Definition: MCValue.h:40
MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_, uint16_t EMachine_, bool HasRelocationAddend, bool IsN64=false)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
void print(raw_ostream &Out) const
virtual void sortRelocs(const MCAssembler &Asm, std::vector< ELFRelocationEntry > &Relocs)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:66
unsigned setRType3(unsigned Value, unsigned Type) const
static uint8_t getOSABI(Triple::OSType OSType)
unsigned setRType2(unsigned Value, unsigned Type) const
Context object for machine code objects.
Definition: MCContext.h:51
#define R_TYPE2_MASK
#define R_TYPE3_SHIFT
virtual unsigned getRelocType(MCContext &Ctx, const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
uint8_t getRSsym(uint32_t Type) const
#define R_SSYM_MASK
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
uint8_t getRType(uint32_t Type) const
#define R_TYPE2_SHIFT
ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type, uint64_t Addend, const MCSymbolELF *OriginalSymbol, uint64_t OriginalAddend)
uint8_t getRType3(uint32_t Type) const
Target - Wrapper for Target specific information.
MCObjectWriter * createELFObjectWriter(MCELFObjectTargetWriter *MOTW, raw_pwrite_stream &OS, bool IsLittleEndian)
Construct a new ELF writer instance.
uint8_t getRType2(uint32_t Type) const
#define R_TYPE3_MASK
#define R_TYPE_MASK
const MCSymbolELF * OriginalSymbol
#define R_TYPE_SHIFT
virtual bool needsRelocateWithSymbol(const MCSymbol &Sym, unsigned Type) const
LLVM Value Representation.
Definition: Value.h:71
unsigned setRSsym(unsigned Value, unsigned Type) const
unsigned setRType(unsigned Value, unsigned Type) const
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