]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/include/llvm/MC/MCELFObjectWriter.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / 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     if (RE.r_offset != r_offset)
49       return RE.r_offset < r_offset;
50     if (Type != RE.Type)
51       return Type < RE.Type;
52     if (Index != RE.Index)
53       return Index < RE.Index;
54     llvm_unreachable("ELFRelocs might be unstable!");
55     return 0;
56   }
57 };
58
59 class MCELFObjectTargetWriter {
60   const uint8_t OSABI;
61   const uint16_t EMachine;
62   const unsigned HasRelocationAddend : 1;
63   const unsigned Is64Bit : 1;
64   const unsigned IsN64 : 1;
65
66 protected:
67
68   MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
69                           uint16_t EMachine_,  bool HasRelocationAddend,
70                           bool IsN64=false);
71
72 public:
73   static uint8_t getOSABI(Triple::OSType OSType) {
74     switch (OSType) {
75       case Triple::FreeBSD:
76         return ELF::ELFOSABI_FREEBSD;
77       case Triple::Linux:
78         return ELF::ELFOSABI_LINUX;
79       default:
80         return ELF::ELFOSABI_NONE;
81     }
82   }
83
84   virtual ~MCELFObjectTargetWriter() {}
85
86   virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
87                                 bool IsPCRel, bool IsRelocWithSymbol,
88                                 int64_t Addend) const = 0;
89   virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
90                                          const MCValue &Target,
91                                          const MCFragment &F,
92                                          const MCFixup &Fixup,
93                                          bool IsPCRel) const;
94   virtual const MCSymbol *undefinedExplicitRelSym(const MCValue &Target,
95                                                   const MCFixup &Fixup,
96                                                   bool IsPCRel) const;
97   virtual void adjustFixupOffset(const MCFixup &Fixup,
98                                  uint64_t &RelocOffset);
99
100   virtual void sortRelocs(const MCAssembler &Asm,
101                           std::vector<ELFRelocationEntry> &Relocs);
102
103   /// @name Accessors
104   /// @{
105   uint8_t getOSABI() const { return OSABI; }
106   uint16_t getEMachine() const { return EMachine; }
107   bool hasRelocationAddend() const { return HasRelocationAddend; }
108   bool is64Bit() const { return Is64Bit; }
109   bool isN64() const { return IsN64; }
110   /// @}
111
112   // Instead of changing everyone's API we pack the N64 Type fields
113   // into the existing 32 bit data unsigned.
114 #define R_TYPE_SHIFT 0
115 #define R_TYPE_MASK 0xffffff00
116 #define R_TYPE2_SHIFT 8
117 #define R_TYPE2_MASK 0xffff00ff
118 #define R_TYPE3_SHIFT 16
119 #define R_TYPE3_MASK 0xff00ffff
120 #define R_SSYM_SHIFT 24
121 #define R_SSYM_MASK 0x00ffffff
122
123   // N64 relocation type accessors
124   unsigned getRType(uint32_t Type) const {
125     return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
126   }
127   unsigned getRType2(uint32_t Type) const {
128     return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
129   }
130   unsigned getRType3(uint32_t Type) const {
131     return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
132   }
133   unsigned getRSsym(uint32_t Type) const {
134     return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
135   }
136
137   // N64 relocation type setting
138   unsigned setRType(unsigned Value, unsigned Type) const {
139     return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
140   }
141   unsigned setRType2(unsigned Value, unsigned Type) const {
142     return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
143   }
144   unsigned setRType3(unsigned Value, unsigned Type) const {
145     return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
146   }
147   unsigned setRSsym(unsigned Value, unsigned Type) const {
148     return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
149   }
150 };
151
152 /// \brief Construct a new ELF writer instance.
153 ///
154 /// \param MOTW - The target specific ELF writer subclass.
155 /// \param OS - The stream to write to.
156 /// \returns The constructed object writer.
157 MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
158                                       raw_ostream &OS, bool IsLittleEndian);
159 } // End llvm namespace
160
161 #endif