Use std::bitset for SubtargetFeatures
[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 class MCELFObjectTargetWriter {
29   const uint8_t OSABI;
30   const uint16_t EMachine;
31   const unsigned HasRelocationAddend : 1;
32   const unsigned Is64Bit : 1;
33   const unsigned IsN64 : 1;
34
35 protected:
36
37   MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
38                           uint16_t EMachine_,  bool HasRelocationAddend,
39                           bool IsN64=false);
40
41 public:
42   static uint8_t getOSABI(Triple::OSType OSType) {
43     switch (OSType) {
44       case Triple::CloudABI:
45         return ELF::ELFOSABI_CLOUDABI;
46       case Triple::PS4:
47       case Triple::FreeBSD:
48         return ELF::ELFOSABI_FREEBSD;
49       case Triple::Linux:
50         return ELF::ELFOSABI_LINUX;
51       default:
52         return ELF::ELFOSABI_NONE;
53     }
54   }
55
56   virtual ~MCELFObjectTargetWriter() {}
57
58   virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
59                                 bool IsPCRel) const = 0;
60
61   virtual bool needsRelocateWithSymbol(const MCSymbolData &SD,
62                                        unsigned Type) const;
63
64   /// @name Accessors
65   /// @{
66   uint8_t getOSABI() const { return OSABI; }
67   uint16_t getEMachine() const { return EMachine; }
68   bool hasRelocationAddend() const { return HasRelocationAddend; }
69   bool is64Bit() const { return Is64Bit; }
70   bool isN64() const { return IsN64; }
71   /// @}
72
73   // Instead of changing everyone's API we pack the N64 Type fields
74   // into the existing 32 bit data unsigned.
75 #define R_TYPE_SHIFT 0
76 #define R_TYPE_MASK 0xffffff00
77 #define R_TYPE2_SHIFT 8
78 #define R_TYPE2_MASK 0xffff00ff
79 #define R_TYPE3_SHIFT 16
80 #define R_TYPE3_MASK 0xff00ffff
81 #define R_SSYM_SHIFT 24
82 #define R_SSYM_MASK 0x00ffffff
83
84   // N64 relocation type accessors
85   uint8_t getRType(uint32_t Type) const {
86     return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
87   }
88   uint8_t getRType2(uint32_t Type) const {
89     return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
90   }
91   uint8_t getRType3(uint32_t Type) const {
92     return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
93   }
94   uint8_t getRSsym(uint32_t Type) const {
95     return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
96   }
97
98   // N64 relocation type setting
99   unsigned setRType(unsigned Value, unsigned Type) const {
100     return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
101   }
102   unsigned setRType2(unsigned Value, unsigned Type) const {
103     return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
104   }
105   unsigned setRType3(unsigned Value, unsigned Type) const {
106     return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
107   }
108   unsigned setRSsym(unsigned Value, unsigned Type) const {
109     return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
110   }
111 };
112
113 /// \brief Construct a new ELF writer instance.
114 ///
115 /// \param MOTW - The target specific ELF writer subclass.
116 /// \param OS - The stream to write to.
117 /// \returns The constructed object writer.
118 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
119                                       raw_ostream &OS, bool IsLittleEndian);
120 } // End llvm namespace
121
122 #endif