Correct MCJIT functionality for MIPS32 architecture.
[oota-llvm.git] / lib / Target / Mips / MCTargetDesc / MipsELFObjectWriter.cpp
1 //===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
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 #include "MCTargetDesc/MipsBaseInfo.h"
11 #include "MCTargetDesc/MipsFixupKinds.h"
12 #include "MCTargetDesc/MipsMCTargetDesc.h"
13 #include "llvm/MC/MCAssembler.h"
14 #include "llvm/MC/MCELFObjectWriter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCSection.h"
17 #include "llvm/MC/MCValue.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include <list>
20
21 using namespace llvm;
22
23 namespace {
24   struct RelEntry {
25     RelEntry(const ELFRelocationEntry &R, const MCSymbol *S, int64_t O) :
26       Reloc(R), Sym(S), Offset(O) {}
27     ELFRelocationEntry Reloc;
28     const MCSymbol *Sym;
29     int64_t Offset;
30   };
31
32   typedef std::list<RelEntry> RelLs;
33   typedef RelLs::iterator RelLsIter;
34
35   class MipsELFObjectWriter : public MCELFObjectTargetWriter {
36   public:
37     MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
38                         bool _isN64, bool IsLittleEndian);
39
40     virtual ~MipsELFObjectWriter();
41
42     virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
43                                   bool IsPCRel, bool IsRelocWithSymbol,
44                                   int64_t Addend) const;
45     virtual unsigned getEFlags() const;
46     virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
47                                            const MCValue &Target,
48                                            const MCFragment &F,
49                                            const MCFixup &Fixup,
50                                            bool IsPCRel) const;
51     virtual void sortRelocs(const MCAssembler &Asm,
52                             std::vector<ELFRelocationEntry> &Relocs);
53   };
54 }
55
56 MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
57                                          bool _isN64, bool IsLittleEndian)
58   : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
59                             /*HasRelocationAddend*/ false,
60                             /*IsN64*/ _isN64) {}
61
62 MipsELFObjectWriter::~MipsELFObjectWriter() {}
63
64 // FIXME: get the real EABI Version from the Subtarget class.
65 unsigned MipsELFObjectWriter::getEFlags() const {
66
67   // FIXME: We can't tell if we are PIC (dynamic) or CPIC (static)
68   unsigned Flag = ELF::EF_MIPS_NOREORDER;
69
70   if (is64Bit())
71     Flag |= ELF::EF_MIPS_ARCH_64R2;
72   else
73     Flag |= ELF::EF_MIPS_ARCH_32R2;
74   return Flag;
75 }
76
77 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
78                                                     const MCValue &Target,
79                                                     const MCFragment &F,
80                                                     const MCFixup &Fixup,
81                                                     bool IsPCRel) const {
82   assert(Target.getSymA() && "SymA cannot be 0.");
83   const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
84
85   if (Sym.getSection().getKind().isMergeableCString() ||
86       Sym.getSection().getKind().isMergeableConst())
87     return &Sym;
88
89   return NULL;
90 }
91
92 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
93                                            const MCFixup &Fixup,
94                                            bool IsPCRel,
95                                            bool IsRelocWithSymbol,
96                                            int64_t Addend) const {
97   // determine the type of the relocation
98   unsigned Type = (unsigned)ELF::R_MIPS_NONE;
99   unsigned Kind = (unsigned)Fixup.getKind();
100
101   switch (Kind) {
102   default:
103     llvm_unreachable("invalid fixup kind!");
104   case FK_Data_4:
105     Type = ELF::R_MIPS_32;
106     break;
107   case FK_Data_8:
108     Type = ELF::R_MIPS_64;
109     break;
110   case FK_GPRel_4:
111     Type = ELF::R_MIPS_GPREL32;
112     break;
113   case Mips::fixup_Mips_GPREL16:
114     Type = ELF::R_MIPS_GPREL16;
115     break;
116   case Mips::fixup_Mips_26:
117     Type = ELF::R_MIPS_26;
118     break;
119   case Mips::fixup_Mips_CALL16:
120     Type = ELF::R_MIPS_CALL16;
121     break;
122   case Mips::fixup_Mips_GOT_Global:
123   case Mips::fixup_Mips_GOT_Local:
124     Type = ELF::R_MIPS_GOT16;
125     break;
126   case Mips::fixup_Mips_HI16:
127     Type = ELF::R_MIPS_HI16;
128     break;
129   case Mips::fixup_Mips_LO16:
130     Type = ELF::R_MIPS_LO16;
131     break;
132   case Mips::fixup_Mips_TLSGD:
133     Type = ELF::R_MIPS_TLS_GD;
134     break;
135   case Mips::fixup_Mips_GOTTPREL:
136     Type = ELF::R_MIPS_TLS_GOTTPREL;
137     break;
138   case Mips::fixup_Mips_TPREL_HI:
139     Type = ELF::R_MIPS_TLS_TPREL_HI16;
140     break;
141   case Mips::fixup_Mips_TPREL_LO:
142     Type = ELF::R_MIPS_TLS_TPREL_LO16;
143     break;
144   case Mips::fixup_Mips_TLSLDM:
145     Type = ELF::R_MIPS_TLS_LDM;
146     break;
147   case Mips::fixup_Mips_DTPREL_HI:
148     Type = ELF::R_MIPS_TLS_DTPREL_HI16;
149     break;
150   case Mips::fixup_Mips_DTPREL_LO:
151     Type = ELF::R_MIPS_TLS_DTPREL_LO16;
152     break;
153   case Mips::fixup_Mips_Branch_PCRel:
154   case Mips::fixup_Mips_PC16:
155     Type = ELF::R_MIPS_PC16;
156     break;
157   case Mips::fixup_Mips_GOT_PAGE:
158     Type = ELF::R_MIPS_GOT_PAGE;
159     break;
160   case Mips::fixup_Mips_GOT_OFST:
161     Type = ELF::R_MIPS_GOT_OFST;
162     break;
163   case Mips::fixup_Mips_GOT_DISP:
164     Type = ELF::R_MIPS_GOT_DISP;
165     break;
166   case Mips::fixup_Mips_GPOFF_HI:
167     Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
168     Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
169     Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
170     break;
171   case Mips::fixup_Mips_GPOFF_LO:
172     Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
173     Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
174     Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
175     break;
176   case Mips::fixup_Mips_HIGHER:
177     Type = ELF::R_MIPS_HIGHER;
178     break;
179   case Mips::fixup_Mips_HIGHEST:
180     Type = ELF::R_MIPS_HIGHEST;
181     break;
182   }
183   return Type;
184 }
185
186 // Return true if R is either a GOT16 against a local symbol or HI16.
187 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
188   if (!R.Sym)
189     return false;
190
191   MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
192
193   return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
194     (R.Reloc.Type == ELF::R_MIPS_HI16);
195 }
196
197 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
198   if (I == Last)
199     return false;
200
201   RelLsIter Hi = I++;
202
203   return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
204     (Hi->Offset == I->Offset);
205 }
206
207 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
208   return R0.Sym == R1.Sym;
209 }
210
211 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
212   return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
213 }
214
215 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
216                                      std::vector<ELFRelocationEntry> &Relocs) {
217   // Call the default function first. Relocations are sorted in descending
218   // order of r_offset.
219   MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
220
221   RelLs RelocLs;
222   std::vector<RelLsIter> Unmatched;
223
224   // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
225   // are in ascending order of r_offset.
226   for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
227        R != Relocs.rend(); ++R) {
228      std::pair<const MCSymbolRefExpr*, int64_t> P =
229        MipsGetSymAndOffset(*R->Fixup);
230      RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
231                                 P.second));
232   }
233
234   // Get list of unmatched HI16 and GOT16.
235   for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
236     if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
237       Unmatched.push_back(R);
238
239   // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
240   for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
241        U != Unmatched.end(); ++U) {
242     RelLsIter LoPos = RelocLs.end(), HiPos = *U;
243     bool MatchedLo = false;
244
245     for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
246       if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
247           (CompareOffset(*R, *HiPos) >= 0) &&
248           ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
249            (!MatchedLo && !CompareOffset(*R, *LoPos))))
250         LoPos = R;
251
252       MatchedLo = NeedsMatchingLo(Asm, *R) &&
253         HasMatchingLo(Asm, R, --RelocLs.end());
254     }
255
256     // If a matching LoPos was found, move HiPos and insert it before LoPos.
257     // Make the offsets of HiPos and LoPos match.
258     if (LoPos != RelocLs.end()) {
259       HiPos->Offset = LoPos->Offset;
260       RelocLs.insert(LoPos, *HiPos);
261       RelocLs.erase(HiPos);
262     }
263   }
264
265   // Put the sorted list back in reverse order.
266   assert(Relocs.size() == RelocLs.size());
267   unsigned I = RelocLs.size();
268
269   for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
270     Relocs[--I] = R->Reloc;
271 }
272
273 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
274                                                 uint8_t OSABI,
275                                                 bool IsLittleEndian,
276                                                 bool Is64Bit) {
277   MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
278                                                 (Is64Bit) ? true : false,
279                                                 IsLittleEndian);
280   return createELFObjectWriter(MOTW, OS, IsLittleEndian);
281 }