LLVM  3.7.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/Support/DataTypes.h"
15 #include "llvm/Support/ELF.h"
16 #include <vector>
17 
18 namespace llvm {
19 class MCAssembler;
20 class MCFixup;
21 class MCFragment;
22 class MCObjectWriter;
23 class MCSymbol;
24 class MCSymbolELF;
25 class MCValue;
26 class raw_pwrite_stream;
27 
29  uint64_t Offset; // Where is the relocation.
30  const MCSymbolELF *Symbol; // The symbol to relocate with.
31  unsigned Type; // The type of the relocation.
32  uint64_t Addend; // The addend to use.
33 
34  ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
35  uint64_t Addend)
36  : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {}
37 };
38 
40  const uint8_t OSABI;
41  const uint16_t EMachine;
42  const unsigned HasRelocationAddend : 1;
43  const unsigned Is64Bit : 1;
44  const unsigned IsN64 : 1;
45 
46 protected:
47 
48  MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
49  uint16_t EMachine_, bool HasRelocationAddend,
50  bool IsN64=false);
51 
52 public:
53  static uint8_t getOSABI(Triple::OSType OSType) {
54  switch (OSType) {
55  case Triple::CloudABI:
57  case Triple::PS4:
58  case Triple::FreeBSD:
59  return ELF::ELFOSABI_FREEBSD;
60  case Triple::Linux:
61  return ELF::ELFOSABI_LINUX;
62  default:
63  return ELF::ELFOSABI_NONE;
64  }
65  }
66 
68 
69  virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
70  bool IsPCRel) const = 0;
71 
72  virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
73  unsigned Type) const;
74 
75  virtual void sortRelocs(const MCAssembler &Asm,
76  std::vector<ELFRelocationEntry> &Relocs);
77 
78  /// \name Accessors
79  /// @{
80  uint8_t getOSABI() const { return OSABI; }
81  uint16_t getEMachine() const { return EMachine; }
82  bool hasRelocationAddend() const { return HasRelocationAddend; }
83  bool is64Bit() const { return Is64Bit; }
84  bool isN64() const { return IsN64; }
85  /// @}
86 
87  // Instead of changing everyone's API we pack the N64 Type fields
88  // into the existing 32 bit data unsigned.
89 #define R_TYPE_SHIFT 0
90 #define R_TYPE_MASK 0xffffff00
91 #define R_TYPE2_SHIFT 8
92 #define R_TYPE2_MASK 0xffff00ff
93 #define R_TYPE3_SHIFT 16
94 #define R_TYPE3_MASK 0xff00ffff
95 #define R_SSYM_SHIFT 24
96 #define R_SSYM_MASK 0x00ffffff
97 
98  // N64 relocation type accessors
99  uint8_t getRType(uint32_t Type) const {
100  return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
101  }
102  uint8_t getRType2(uint32_t Type) const {
103  return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
104  }
105  uint8_t getRType3(uint32_t Type) const {
106  return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
107  }
108  uint8_t getRSsym(uint32_t Type) const {
109  return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
110  }
111 
112  // N64 relocation type setting
113  unsigned setRType(unsigned Value, unsigned Type) const {
114  return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
115  }
116  unsigned setRType2(unsigned Value, unsigned Type) const {
117  return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
118  }
119  unsigned setRType3(unsigned Value, unsigned Type) const {
120  return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
121  }
122  unsigned setRSsym(unsigned Value, unsigned Type) const {
123  return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
124  }
125 };
126 
127 /// \brief Construct a new ELF writer instance.
128 ///
129 /// \param MOTW - The target specific ELF writer subclass.
130 /// \param OS - The stream to write to.
131 /// \returns The constructed object writer.
132 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
133  raw_pwrite_stream &OS,
134  bool IsLittleEndian);
135 } // End llvm namespace
136 
137 #endif
This represents an "assembler immediate".
Definition: MCValue.h:44
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
virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup, bool IsPCRel) const =0
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:62
unsigned setRType3(unsigned Value, unsigned Type) const
static uint8_t getOSABI(Triple::OSType OSType)
unsigned setRType2(unsigned Value, unsigned Type) const
#define R_TYPE2_MASK
#define R_TYPE3_SHIFT
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
PowerPC TLS Dynamic Call Fixup
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
#define R_TYPE_SHIFT
ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type, uint64_t Addend)
virtual bool needsRelocateWithSymbol(const MCSymbol &Sym, unsigned Type) const
LLVM Value Representation.
Definition: Value.h:69
unsigned setRSsym(unsigned Value, unsigned Type) const
unsigned setRType(unsigned Value, unsigned Type) const
#define R_SSYM_SHIFT
const MCSymbolELF * Symbol