Remove another unused argument.
[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     unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
43                           bool IsPCRel) const override;
44     virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
45                                            const MCValue &Target,
46                                            const MCFragment &F,
47                                            const MCFixup &Fixup,
48                                            bool IsPCRel) const;
49     virtual void sortRelocs(const MCAssembler &Asm,
50                             std::vector<ELFRelocationEntry> &Relocs);
51   };
52 }
53
54 MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
55                                          bool _isN64, bool IsLittleEndian)
56   : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
57                             /*HasRelocationAddend*/ (_isN64) ? true : false,
58                             /*IsN64*/ _isN64) {}
59
60 MipsELFObjectWriter::~MipsELFObjectWriter() {}
61
62 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
63                                                     const MCValue &Target,
64                                                     const MCFragment &F,
65                                                     const MCFixup &Fixup,
66                                                     bool IsPCRel) const {
67   assert(Target.getSymA() && "SymA cannot be 0.");
68   const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
69
70   if (Sym.getSection().getKind().isMergeableCString() ||
71       Sym.getSection().getKind().isMergeableConst())
72     return &Sym;
73
74   return NULL;
75 }
76
77 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
78                                            const MCFixup &Fixup,
79                                            bool IsPCRel) const {
80   // determine the type of the relocation
81   unsigned Type = (unsigned)ELF::R_MIPS_NONE;
82   unsigned Kind = (unsigned)Fixup.getKind();
83
84   switch (Kind) {
85   default:
86     llvm_unreachable("invalid fixup kind!");
87   case FK_Data_4:
88     Type = ELF::R_MIPS_32;
89     break;
90   case FK_Data_8:
91     Type = ELF::R_MIPS_64;
92     break;
93   case FK_GPRel_4:
94     if (isN64()) {
95       Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
96       Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
97       Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
98     }
99     else
100       Type = ELF::R_MIPS_GPREL32;
101     break;
102   case Mips::fixup_Mips_GPREL16:
103     Type = ELF::R_MIPS_GPREL16;
104     break;
105   case Mips::fixup_Mips_26:
106     Type = ELF::R_MIPS_26;
107     break;
108   case Mips::fixup_Mips_CALL16:
109     Type = ELF::R_MIPS_CALL16;
110     break;
111   case Mips::fixup_Mips_GOT_Global:
112   case Mips::fixup_Mips_GOT_Local:
113     Type = ELF::R_MIPS_GOT16;
114     break;
115   case Mips::fixup_Mips_HI16:
116     Type = ELF::R_MIPS_HI16;
117     break;
118   case Mips::fixup_Mips_LO16:
119     Type = ELF::R_MIPS_LO16;
120     break;
121   case Mips::fixup_Mips_TLSGD:
122     Type = ELF::R_MIPS_TLS_GD;
123     break;
124   case Mips::fixup_Mips_GOTTPREL:
125     Type = ELF::R_MIPS_TLS_GOTTPREL;
126     break;
127   case Mips::fixup_Mips_TPREL_HI:
128     Type = ELF::R_MIPS_TLS_TPREL_HI16;
129     break;
130   case Mips::fixup_Mips_TPREL_LO:
131     Type = ELF::R_MIPS_TLS_TPREL_LO16;
132     break;
133   case Mips::fixup_Mips_TLSLDM:
134     Type = ELF::R_MIPS_TLS_LDM;
135     break;
136   case Mips::fixup_Mips_DTPREL_HI:
137     Type = ELF::R_MIPS_TLS_DTPREL_HI16;
138     break;
139   case Mips::fixup_Mips_DTPREL_LO:
140     Type = ELF::R_MIPS_TLS_DTPREL_LO16;
141     break;
142   case Mips::fixup_Mips_Branch_PCRel:
143   case Mips::fixup_Mips_PC16:
144     Type = ELF::R_MIPS_PC16;
145     break;
146   case Mips::fixup_Mips_GOT_PAGE:
147     Type = ELF::R_MIPS_GOT_PAGE;
148     break;
149   case Mips::fixup_Mips_GOT_OFST:
150     Type = ELF::R_MIPS_GOT_OFST;
151     break;
152   case Mips::fixup_Mips_GOT_DISP:
153     Type = ELF::R_MIPS_GOT_DISP;
154     break;
155   case Mips::fixup_Mips_GPOFF_HI:
156     Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
157     Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
158     Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
159     break;
160   case Mips::fixup_Mips_GPOFF_LO:
161     Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
162     Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
163     Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
164     break;
165   case Mips::fixup_Mips_HIGHER:
166     Type = ELF::R_MIPS_HIGHER;
167     break;
168   case Mips::fixup_Mips_HIGHEST:
169     Type = ELF::R_MIPS_HIGHEST;
170     break;
171   case Mips::fixup_Mips_GOT_HI16:
172     Type = ELF::R_MIPS_GOT_HI16;
173     break;
174   case Mips::fixup_Mips_GOT_LO16:
175     Type = ELF::R_MIPS_GOT_LO16;
176     break;
177   case Mips::fixup_Mips_CALL_HI16:
178     Type = ELF::R_MIPS_CALL_HI16;
179     break;
180   case Mips::fixup_Mips_CALL_LO16:
181     Type = ELF::R_MIPS_CALL_LO16;
182     break;
183   case Mips::fixup_MICROMIPS_26_S1:
184     Type = ELF::R_MICROMIPS_26_S1;
185     break;
186   case Mips::fixup_MICROMIPS_HI16:
187     Type = ELF::R_MICROMIPS_HI16;
188     break;
189   case Mips::fixup_MICROMIPS_LO16:
190     Type = ELF::R_MICROMIPS_LO16;
191     break;
192   case Mips::fixup_MICROMIPS_GOT16:
193     Type = ELF::R_MICROMIPS_GOT16;
194     break;
195   case Mips::fixup_MICROMIPS_PC16_S1:
196     Type = ELF::R_MICROMIPS_PC16_S1;
197     break;
198   case Mips::fixup_MICROMIPS_CALL16:
199     Type = ELF::R_MICROMIPS_CALL16;
200     break;
201   case Mips::fixup_MICROMIPS_GOT_DISP:
202     Type = ELF::R_MICROMIPS_GOT_DISP;
203     break;
204   case Mips::fixup_MICROMIPS_GOT_PAGE:
205     Type = ELF::R_MICROMIPS_GOT_PAGE;
206     break;
207   case Mips::fixup_MICROMIPS_GOT_OFST:
208     Type = ELF::R_MICROMIPS_GOT_OFST;
209     break;
210   case Mips::fixup_MICROMIPS_TLS_GD:
211     Type = ELF::R_MICROMIPS_TLS_GD;
212     break;
213   case Mips::fixup_MICROMIPS_TLS_LDM:
214     Type = ELF::R_MICROMIPS_TLS_LDM;
215     break;
216   case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
217     Type = ELF::R_MICROMIPS_TLS_DTPREL_HI16;
218     break;
219   case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
220     Type = ELF::R_MICROMIPS_TLS_DTPREL_LO16;
221     break;
222   case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
223     Type = ELF::R_MICROMIPS_TLS_TPREL_HI16;
224     break;
225   case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
226     Type = ELF::R_MICROMIPS_TLS_TPREL_LO16;
227     break;
228   }
229   return Type;
230 }
231
232 // Return true if R is either a GOT16 against a local symbol or HI16.
233 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
234   if (!R.Sym)
235     return false;
236
237   MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
238
239   return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
240     (R.Reloc.Type == ELF::R_MIPS_HI16);
241 }
242
243 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
244   if (I == Last)
245     return false;
246
247   RelLsIter Hi = I++;
248
249   return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
250     (Hi->Offset == I->Offset);
251 }
252
253 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
254   return R0.Sym == R1.Sym;
255 }
256
257 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
258   return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
259 }
260
261 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
262                                      std::vector<ELFRelocationEntry> &Relocs) {
263   // Call the default function first. Relocations are sorted in descending
264   // order of r_offset.
265   MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
266
267   RelLs RelocLs;
268   std::vector<RelLsIter> Unmatched;
269
270   // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
271   // are in ascending order of r_offset.
272   for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
273        R != Relocs.rend(); ++R) {
274      std::pair<const MCSymbolRefExpr*, int64_t> P =
275        MipsGetSymAndOffset(*R->Fixup);
276      RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
277                                 P.second));
278   }
279
280   // Get list of unmatched HI16 and GOT16.
281   for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
282     if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
283       Unmatched.push_back(R);
284
285   // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
286   for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
287        U != Unmatched.end(); ++U) {
288     RelLsIter LoPos = RelocLs.end(), HiPos = *U;
289     bool MatchedLo = false;
290
291     for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
292       if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
293           (CompareOffset(*R, *HiPos) >= 0) &&
294           ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
295            (!MatchedLo && !CompareOffset(*R, *LoPos))))
296         LoPos = R;
297
298       MatchedLo = NeedsMatchingLo(Asm, *R) &&
299         HasMatchingLo(Asm, R, --RelocLs.end());
300     }
301
302     // If a matching LoPos was found, move HiPos and insert it before LoPos.
303     // Make the offsets of HiPos and LoPos match.
304     if (LoPos != RelocLs.end()) {
305       HiPos->Offset = LoPos->Offset;
306       RelocLs.insert(LoPos, *HiPos);
307       RelocLs.erase(HiPos);
308     }
309   }
310
311   // Put the sorted list back in reverse order.
312   assert(Relocs.size() == RelocLs.size());
313   unsigned I = RelocLs.size();
314
315   for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
316     Relocs[--I] = R->Reloc;
317 }
318
319 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
320                                                 uint8_t OSABI,
321                                                 bool IsLittleEndian,
322                                                 bool Is64Bit) {
323   MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
324                                                 (Is64Bit) ? true : false,
325                                                 IsLittleEndian);
326   return createELFObjectWriter(MOTW, OS, IsLittleEndian);
327 }