9ed8e8024835ac7705038ae56cc970c1e9df90e9
[oota-llvm.git] / include / llvm / MC / MCELFObjectWriter.h
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 MCSectionData;
24 class MCSymbol;
25 class MCSymbolData;
26 class MCValue;
27
28 struct ELFRelocationEntry {
29   uint64_t Offset; // Where is the relocation.
30   const MCSymbol *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 MCSymbol *Symbol, unsigned Type,
35                      uint64_t Addend)
36       : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {}
37 };
38
39 class MCELFObjectTargetWriter {
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:
56         return ELF::ELFOSABI_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
67   virtual ~MCELFObjectTargetWriter() {}
68
69   virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
70                                 bool IsPCRel) const = 0;
71
72   virtual bool needsRelocateWithSymbol(const MCSymbolData &SD,
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_ostream &OS, bool IsLittleEndian);
134 } // End llvm namespace
135
136 #endif