[mips][mips64r6] Add relocations R_MIPS_PC21_S2, R_MIPS_PC26_S2
[oota-llvm.git] / lib / Target / Mips / MCTargetDesc / MipsAsmBackend.cpp
1 //===-- MipsAsmBackend.cpp - Mips Asm Backend  ----------------------------===//
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 // This file implements the MipsAsmBackend class.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14
15 #include "MCTargetDesc/MipsFixupKinds.h"
16 #include "MCTargetDesc/MipsAsmBackend.h"
17 #include "MCTargetDesc/MipsMCTargetDesc.h"
18 #include "llvm/MC/MCAsmBackend.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCDirectives.h"
22 #include "llvm/MC/MCELFObjectWriter.h"
23 #include "llvm/MC/MCFixupKindInfo.h"
24 #include "llvm/MC/MCObjectWriter.h"
25 #include "llvm/MC/MCSubtargetInfo.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/MathExtras.h"
28 #include "llvm/Support/raw_ostream.h"
29
30 using namespace llvm;
31
32 // Prepare value for the target space for it
33 static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
34                                  MCContext *Ctx = nullptr) {
35
36   unsigned Kind = Fixup.getKind();
37
38   // Add/subtract and shift
39   switch (Kind) {
40   default:
41     return 0;
42   case FK_Data_2:
43   case FK_GPRel_4:
44   case FK_Data_4:
45   case FK_Data_8:
46   case Mips::fixup_Mips_LO16:
47   case Mips::fixup_Mips_GPREL16:
48   case Mips::fixup_Mips_GPOFF_HI:
49   case Mips::fixup_Mips_GPOFF_LO:
50   case Mips::fixup_Mips_GOT_PAGE:
51   case Mips::fixup_Mips_GOT_OFST:
52   case Mips::fixup_Mips_GOT_DISP:
53   case Mips::fixup_Mips_GOT_LO16:
54   case Mips::fixup_Mips_CALL_LO16:
55   case Mips::fixup_MICROMIPS_LO16:
56   case Mips::fixup_MICROMIPS_GOT_PAGE:
57   case Mips::fixup_MICROMIPS_GOT_OFST:
58   case Mips::fixup_MICROMIPS_GOT_DISP:
59     break;
60   case Mips::fixup_Mips_PC16:
61     // So far we are only using this type for branches.
62     // For branches we start 1 instruction after the branch
63     // so the displacement will be one instruction size less.
64     Value -= 4;
65     // The displacement is then divided by 4 to give us an 18 bit
66     // address range. Forcing a signed division because Value can be negative.
67     Value = (int64_t)Value / 4;
68     // We now check if Value can be encoded as a 16-bit signed immediate.
69     if (!isIntN(16, Value) && Ctx)
70       Ctx->FatalError(Fixup.getLoc(), "out of range PC16 fixup");
71     break;
72   case Mips::fixup_Mips_26:
73     // So far we are only using this type for jumps.
74     // The displacement is then divided by 4 to give us an 28 bit
75     // address range.
76     Value >>= 2;
77     break;
78   case Mips::fixup_Mips_HI16:
79   case Mips::fixup_Mips_GOT_Local:
80   case Mips::fixup_Mips_GOT_HI16:
81   case Mips::fixup_Mips_CALL_HI16:
82   case Mips::fixup_MICROMIPS_HI16:
83     // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
84     Value = ((Value + 0x8000) >> 16) & 0xffff;
85     break;
86   case Mips::fixup_Mips_HIGHER:
87     // Get the 3rd 16-bits.
88     Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
89     break;
90   case Mips::fixup_Mips_HIGHEST:
91     // Get the 4th 16-bits.
92     Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
93     break;
94   case Mips::fixup_MICROMIPS_26_S1:
95     Value >>= 1;
96     break;
97   case Mips::fixup_MICROMIPS_PC16_S1:
98     Value -= 4;
99     // Forcing a signed division because Value can be negative.
100     Value = (int64_t)Value / 2;
101     // We now check if Value can be encoded as a 16-bit signed immediate.
102     if (!isIntN(16, Value) && Ctx)
103       Ctx->FatalError(Fixup.getLoc(), "out of range PC16 fixup");
104     break;
105   case Mips::fixup_MIPS_PC21_S2:
106     Value -= 4;
107     // Forcing a signed division because Value can be negative.
108     Value = (int64_t) Value / 4;
109     // We now check if Value can be encoded as a 21-bit signed immediate.
110     if (!isIntN(21, Value) && Ctx)
111       Ctx->FatalError(Fixup.getLoc(), "out of range PC21 fixup");
112     break;
113   case Mips::fixup_MIPS_PC26_S2:
114     Value -= 4;
115     // Forcing a signed division because Value can be negative.
116     Value = (int64_t) Value / 4;
117     // We now check if Value can be encoded as a 26-bit signed immediate.
118     if (!isIntN(26, Value) && Ctx)
119       Ctx->FatalError(Fixup.getLoc(), "out of range PC26 fixup");
120     break;
121   }
122
123   return Value;
124 }
125
126 MCObjectWriter *MipsAsmBackend::createObjectWriter(raw_ostream &OS) const {
127   return createMipsELFObjectWriter(OS,
128     MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
129 }
130
131 // Little-endian fixup data byte ordering:
132 //   mips32r2:   a | b | x | x
133 //   microMIPS:  x | x | a | b
134
135 static bool needsMMLEByteOrder(unsigned Kind) {
136   return Kind >= Mips::fixup_MICROMIPS_26_S1 &&
137          Kind < Mips::LastTargetFixupKind;
138 }
139
140 // Calculate index for microMIPS specific little endian byte order
141 static unsigned calculateMMLEIndex(unsigned i) {
142   assert(i <= 3 && "Index out of range!");
143
144   return (1 - i / 2) * 2 + i % 2;
145 }
146
147 /// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
148 /// data fragment, at the offset specified by the fixup and following the
149 /// fixup kind as appropriate.
150 void MipsAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
151                                 unsigned DataSize, uint64_t Value,
152                                 bool IsPCRel) const {
153   MCFixupKind Kind = Fixup.getKind();
154   Value = adjustFixupValue(Fixup, Value);
155
156   if (!Value)
157     return; // Doesn't change encoding.
158
159   // Where do we start in the object
160   unsigned Offset = Fixup.getOffset();
161   // Number of bytes we need to fixup
162   unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
163   // Used to point to big endian bytes
164   unsigned FullSize;
165
166   switch ((unsigned)Kind) {
167   case FK_Data_2:
168   case Mips::fixup_Mips_16:
169     FullSize = 2;
170     break;
171   case FK_Data_8:
172   case Mips::fixup_Mips_64:
173     FullSize = 8;
174     break;
175   case FK_Data_4:
176   default:
177     FullSize = 4;
178     break;
179   }
180
181   // Grab current value, if any, from bits.
182   uint64_t CurVal = 0;
183
184   bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind);
185
186   for (unsigned i = 0; i != NumBytes; ++i) {
187     unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
188                                                     : i)
189                             : (FullSize - 1 - i);
190     CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
191   }
192
193   uint64_t Mask = ((uint64_t)(-1) >>
194                     (64 - getFixupKindInfo(Kind).TargetSize));
195   CurVal |= Value & Mask;
196
197   // Write out the fixed up bytes back to the code/data bits.
198   for (unsigned i = 0; i != NumBytes; ++i) {
199     unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
200                                                     : i)
201                             : (FullSize - 1 - i);
202     Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
203   }
204 }
205
206 const MCFixupKindInfo &MipsAsmBackend::
207 getFixupKindInfo(MCFixupKind Kind) const {
208   const static MCFixupKindInfo LittleEndianInfos[Mips::NumTargetFixupKinds] = {
209     // This table *must* be in same the order of fixup_* kinds in
210     // MipsFixupKinds.h.
211     //
212     // name                    offset  bits  flags
213     { "fixup_Mips_16",           0,     16,   0 },
214     { "fixup_Mips_32",           0,     32,   0 },
215     { "fixup_Mips_REL32",        0,     32,   0 },
216     { "fixup_Mips_26",           0,     26,   0 },
217     { "fixup_Mips_HI16",         0,     16,   0 },
218     { "fixup_Mips_LO16",         0,     16,   0 },
219     { "fixup_Mips_GPREL16",      0,     16,   0 },
220     { "fixup_Mips_LITERAL",      0,     16,   0 },
221     { "fixup_Mips_GOT_Global",   0,     16,   0 },
222     { "fixup_Mips_GOT_Local",    0,     16,   0 },
223     { "fixup_Mips_PC16",         0,     16,  MCFixupKindInfo::FKF_IsPCRel },
224     { "fixup_Mips_CALL16",       0,     16,   0 },
225     { "fixup_Mips_GPREL32",      0,     32,   0 },
226     { "fixup_Mips_SHIFT5",       6,      5,   0 },
227     { "fixup_Mips_SHIFT6",       6,      5,   0 },
228     { "fixup_Mips_64",           0,     64,   0 },
229     { "fixup_Mips_TLSGD",        0,     16,   0 },
230     { "fixup_Mips_GOTTPREL",     0,     16,   0 },
231     { "fixup_Mips_TPREL_HI",     0,     16,   0 },
232     { "fixup_Mips_TPREL_LO",     0,     16,   0 },
233     { "fixup_Mips_TLSLDM",       0,     16,   0 },
234     { "fixup_Mips_DTPREL_HI",    0,     16,   0 },
235     { "fixup_Mips_DTPREL_LO",    0,     16,   0 },
236     { "fixup_Mips_Branch_PCRel", 0,     16,  MCFixupKindInfo::FKF_IsPCRel },
237     { "fixup_Mips_GPOFF_HI",     0,     16,   0 },
238     { "fixup_Mips_GPOFF_LO",     0,     16,   0 },
239     { "fixup_Mips_GOT_PAGE",     0,     16,   0 },
240     { "fixup_Mips_GOT_OFST",     0,     16,   0 },
241     { "fixup_Mips_GOT_DISP",     0,     16,   0 },
242     { "fixup_Mips_HIGHER",       0,     16,   0 },
243     { "fixup_Mips_HIGHEST",      0,     16,   0 },
244     { "fixup_Mips_GOT_HI16",     0,     16,   0 },
245     { "fixup_Mips_GOT_LO16",     0,     16,   0 },
246     { "fixup_Mips_CALL_HI16",    0,     16,   0 },
247     { "fixup_Mips_CALL_LO16",    0,     16,   0 },
248     { "fixup_MIPS_PC21_S2",      0,     21,  MCFixupKindInfo::FKF_IsPCRel },
249     { "fixup_MIPS_PC26_S2",      0,     26,  MCFixupKindInfo::FKF_IsPCRel },
250     { "fixup_MICROMIPS_26_S1",   0,     26,   0 },
251     { "fixup_MICROMIPS_HI16",    0,     16,   0 },
252     { "fixup_MICROMIPS_LO16",    0,     16,   0 },
253     { "fixup_MICROMIPS_GOT16",   0,     16,   0 },
254     { "fixup_MICROMIPS_PC16_S1", 0,     16,   MCFixupKindInfo::FKF_IsPCRel },
255     { "fixup_MICROMIPS_CALL16",  0,     16,   0 },
256     { "fixup_MICROMIPS_GOT_DISP",        0,     16,   0 },
257     { "fixup_MICROMIPS_GOT_PAGE",        0,     16,   0 },
258     { "fixup_MICROMIPS_GOT_OFST",        0,     16,   0 },
259     { "fixup_MICROMIPS_TLS_GD",          0,     16,   0 },
260     { "fixup_MICROMIPS_TLS_LDM",         0,     16,   0 },
261     { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0,     16,   0 },
262     { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0,     16,   0 },
263     { "fixup_MICROMIPS_TLS_TPREL_HI16",  0,     16,   0 },
264     { "fixup_MICROMIPS_TLS_TPREL_LO16",  0,     16,   0 }
265   };
266
267   const static MCFixupKindInfo BigEndianInfos[Mips::NumTargetFixupKinds] = {
268     // This table *must* be in same the order of fixup_* kinds in
269     // MipsFixupKinds.h.
270     //
271     // name                    offset  bits  flags
272     { "fixup_Mips_16",          16,     16,   0 },
273     { "fixup_Mips_32",           0,     32,   0 },
274     { "fixup_Mips_REL32",        0,     32,   0 },
275     { "fixup_Mips_26",           6,     26,   0 },
276     { "fixup_Mips_HI16",        16,     16,   0 },
277     { "fixup_Mips_LO16",        16,     16,   0 },
278     { "fixup_Mips_GPREL16",     16,     16,   0 },
279     { "fixup_Mips_LITERAL",     16,     16,   0 },
280     { "fixup_Mips_GOT_Global",  16,     16,   0 },
281     { "fixup_Mips_GOT_Local",   16,     16,   0 },
282     { "fixup_Mips_PC16",        16,     16,  MCFixupKindInfo::FKF_IsPCRel },
283     { "fixup_Mips_CALL16",      16,     16,   0 },
284     { "fixup_Mips_GPREL32",      0,     32,   0 },
285     { "fixup_Mips_SHIFT5",      21,      5,   0 },
286     { "fixup_Mips_SHIFT6",      21,      5,   0 },
287     { "fixup_Mips_64",           0,     64,   0 },
288     { "fixup_Mips_TLSGD",       16,     16,   0 },
289     { "fixup_Mips_GOTTPREL",    16,     16,   0 },
290     { "fixup_Mips_TPREL_HI",    16,     16,   0 },
291     { "fixup_Mips_TPREL_LO",    16,     16,   0 },
292     { "fixup_Mips_TLSLDM",      16,     16,   0 },
293     { "fixup_Mips_DTPREL_HI",   16,     16,   0 },
294     { "fixup_Mips_DTPREL_LO",   16,     16,   0 },
295     { "fixup_Mips_Branch_PCRel",16,     16,  MCFixupKindInfo::FKF_IsPCRel },
296     { "fixup_Mips_GPOFF_HI",    16,     16,   0 },
297     { "fixup_Mips_GPOFF_LO",    16,     16,   0 },
298     { "fixup_Mips_GOT_PAGE",    16,     16,   0 },
299     { "fixup_Mips_GOT_OFST",    16,     16,   0 },
300     { "fixup_Mips_GOT_DISP",    16,     16,   0 },
301     { "fixup_Mips_HIGHER",      16,     16,   0 },
302     { "fixup_Mips_HIGHEST",     16,     16,   0 },
303     { "fixup_Mips_GOT_HI16",    16,     16,   0 },
304     { "fixup_Mips_GOT_LO16",    16,     16,   0 },
305     { "fixup_Mips_CALL_HI16",   16,     16,   0 },
306     { "fixup_Mips_CALL_LO16",   16,     16,   0 },
307     { "fixup_MIPS_PC21_S2",     11,     21,  MCFixupKindInfo::FKF_IsPCRel },
308     { "fixup_MIPS_PC26_S2",      6,     26,  MCFixupKindInfo::FKF_IsPCRel },
309     { "fixup_MICROMIPS_26_S1",   6,     26,   0 },
310     { "fixup_MICROMIPS_HI16",   16,     16,   0 },
311     { "fixup_MICROMIPS_LO16",   16,     16,   0 },
312     { "fixup_MICROMIPS_GOT16",  16,     16,   0 },
313     { "fixup_MICROMIPS_PC16_S1",16,     16,   MCFixupKindInfo::FKF_IsPCRel },
314     { "fixup_MICROMIPS_CALL16", 16,     16,   0 },
315     { "fixup_MICROMIPS_GOT_DISP",        16,     16,   0 },
316     { "fixup_MICROMIPS_GOT_PAGE",        16,     16,   0 },
317     { "fixup_MICROMIPS_GOT_OFST",        16,     16,   0 },
318     { "fixup_MICROMIPS_TLS_GD",          16,     16,   0 },
319     { "fixup_MICROMIPS_TLS_LDM",         16,     16,   0 },
320     { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16,     16,   0 },
321     { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16,     16,   0 },
322     { "fixup_MICROMIPS_TLS_TPREL_HI16",  16,     16,   0 },
323     { "fixup_MICROMIPS_TLS_TPREL_LO16",  16,     16,   0 }
324   };
325
326   if (Kind < FirstTargetFixupKind)
327     return MCAsmBackend::getFixupKindInfo(Kind);
328
329   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
330           "Invalid kind!");
331
332   if (IsLittle)
333     return LittleEndianInfos[Kind - FirstTargetFixupKind];
334   return BigEndianInfos[Kind - FirstTargetFixupKind];
335 }
336
337 /// WriteNopData - Write an (optimal) nop sequence of Count bytes
338 /// to the given output. If the target cannot generate such a sequence,
339 /// it should return an error.
340 ///
341 /// \return - True on success.
342 bool MipsAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
343   // Check for a less than instruction size number of bytes
344   // FIXME: 16 bit instructions are not handled yet here.
345   // We shouldn't be using a hard coded number for instruction size.
346   if (Count % 4) return false;
347
348   uint64_t NumNops = Count / 4;
349   for (uint64_t i = 0; i != NumNops; ++i)
350     OW->Write32(0);
351   return true;
352 }
353
354 /// processFixupValue - Target hook to process the literal value of a fixup
355 /// if necessary.
356 void MipsAsmBackend::processFixupValue(const MCAssembler &Asm,
357                                        const MCAsmLayout &Layout,
358                                        const MCFixup &Fixup,
359                                        const MCFragment *DF,
360                                        const MCValue &Target,
361                                        uint64_t &Value,
362                                        bool &IsResolved) {
363   // At this point we'll ignore the value returned by adjustFixupValue as
364   // we are only checking if the fixup can be applied correctly. We have
365   // access to MCContext from here which allows us to report a fatal error
366   // with *possibly* a source code location.
367   (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
368 }
369
370 // MCAsmBackend
371 MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T,
372                                              const MCRegisterInfo &MRI,
373                                              StringRef TT,
374                                              StringRef CPU) {
375   return new MipsAsmBackend(T, Triple(TT).getOS(),
376                             /*IsLittle*/true, /*Is64Bit*/false);
377 }
378
379 MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T,
380                                              const MCRegisterInfo &MRI,
381                                              StringRef TT,
382                                              StringRef CPU) {
383   return new MipsAsmBackend(T, Triple(TT).getOS(),
384                             /*IsLittle*/false, /*Is64Bit*/false);
385 }
386
387 MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T,
388                                              const MCRegisterInfo &MRI,
389                                              StringRef TT,
390                                              StringRef CPU) {
391   return new MipsAsmBackend(T, Triple(TT).getOS(),
392                             /*IsLittle*/true, /*Is64Bit*/true);
393 }
394
395 MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T,
396                                              const MCRegisterInfo &MRI,
397                                              StringRef TT,
398                                              StringRef CPU) {
399   return new MipsAsmBackend(T, Triple(TT).getOS(),
400                             /*IsLittle*/false, /*Is64Bit*/true);
401 }