a59776d5cdaac31656ebcd159e08bb6bbadbe489
[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 MCSymbol;
24 class MCValue;
25
26 /// @name Relocation Data
27 /// @{
28
29 struct ELFRelocationEntry {
30   // Make these big enough for both 32-bit and 64-bit
31   uint64_t r_offset;
32   int Index;
33   unsigned Type;
34   const MCSymbol *Symbol;
35   uint64_t r_addend;
36   const MCFixup *Fixup;
37
38   ELFRelocationEntry()
39     : r_offset(0), Index(0), Type(0), Symbol(0), r_addend(0), Fixup(0) {}
40
41   ELFRelocationEntry(uint64_t RelocOffset, int Idx, unsigned RelType,
42                      const MCSymbol *Sym, uint64_t Addend, const MCFixup &Fixup)
43     : r_offset(RelocOffset), Index(Idx), Type(RelType), Symbol(Sym),
44       r_addend(Addend), Fixup(&Fixup) {}
45
46   // Support lexicographic sorting.
47   bool operator<(const ELFRelocationEntry &RE) const {
48     return RE.r_offset < r_offset;
49   }
50 };
51
52 class MCELFObjectTargetWriter {
53   const uint8_t OSABI;
54   const uint16_t EMachine;
55   const unsigned HasRelocationAddend : 1;
56   const unsigned Is64Bit : 1;
57   const unsigned IsN64 : 1;
58
59 protected:
60
61   MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
62                           uint16_t EMachine_,  bool HasRelocationAddend,
63                           bool IsN64=false);
64
65 public:
66   static uint8_t getOSABI(Triple::OSType OSType) {
67     switch (OSType) {
68       case Triple::FreeBSD:
69         return ELF::ELFOSABI_FREEBSD;
70       case Triple::Linux:
71         return ELF::ELFOSABI_LINUX;
72       default:
73         return ELF::ELFOSABI_NONE;
74     }
75   }
76
77   virtual ~MCELFObjectTargetWriter() {}
78
79   virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
80                                 bool IsPCRel, bool IsRelocWithSymbol,
81                                 int64_t Addend) const = 0;
82   virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
83                                          const MCValue &Target,
84                                          const MCFragment &F,
85                                          const MCFixup &Fixup,
86                                          bool IsPCRel) const;
87   virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
88                                                   const MCFixup &Fixup,
89                                                   bool IsPCRel) const;
90   virtual void adjustFixupOffset(const MCFixup &Fixup,
91                                  uint64_t &RelocOffset);
92
93   virtual void sortRelocs(const MCAssembler &Asm,
94                           std::vector<ELFRelocationEntry> &Relocs);
95
96   /// @name Accessors
97   /// @{
98   uint8_t getOSABI() const { return OSABI; }
99   uint16_t getEMachine() const { return EMachine; }
100   bool hasRelocationAddend() const { return HasRelocationAddend; }
101   bool is64Bit() const { return Is64Bit; }
102   bool isN64() const { return IsN64; }
103   /// @}
104
105   // Instead of changing everyone's API we pack the N64 Type fields
106   // into the existing 32 bit data unsigned.
107 #define R_TYPE_SHIFT 0
108 #define R_TYPE_MASK 0xffffff00
109 #define R_TYPE2_SHIFT 8
110 #define R_TYPE2_MASK 0xffff00ff
111 #define R_TYPE3_SHIFT 16
112 #define R_TYPE3_MASK 0xff00ffff
113 #define R_SSYM_SHIFT 24
114 #define R_SSYM_MASK 0x00ffffff
115
116   // N64 relocation type accessors
117   unsigned getRType(uint32_t Type) const {
118     return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
119   }
120   unsigned getRType2(uint32_t Type) const {
121     return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
122   }
123   unsigned getRType3(uint32_t Type) const {
124     return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
125   }
126   unsigned getRSsym(uint32_t Type) const {
127     return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
128   }
129
130   // N64 relocation type setting
131   unsigned setRType(unsigned Value, unsigned Type) const {
132     return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
133   }
134   unsigned setRType2(unsigned Value, unsigned Type) const {
135     return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
136   }
137   unsigned setRType3(unsigned Value, unsigned Type) const {
138     return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
139   }
140   unsigned setRSsym(unsigned Value, unsigned Type) const {
141     return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
142   }
143 };
144
145 /// \brief Construct a new ELF writer instance.
146 ///
147 /// \param MOTW - The target specific ELF writer subclass.
148 /// \param OS - The stream to write to.
149 /// \returns The constructed object writer.
150 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
151                                       raw_ostream &OS, bool IsLittleEndian);
152 } // End llvm namespace
153
154 #endif