]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
MFC r244628:
[FreeBSD/stable/9.git] / contrib / llvm / 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*/ (_isN64) ? true : 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   case Mips::fixup_Mips_GOT_HI16:
183     Type = ELF::R_MIPS_GOT_HI16;
184     break;
185   case Mips::fixup_Mips_GOT_LO16:
186     Type = ELF::R_MIPS_GOT_LO16;
187     break;
188   case Mips::fixup_Mips_CALL_HI16:
189     Type = ELF::R_MIPS_CALL_HI16;
190     break;
191   case Mips::fixup_Mips_CALL_LO16:
192     Type = ELF::R_MIPS_CALL_LO16;
193     break;
194   }
195   return Type;
196 }
197
198 // Return true if R is either a GOT16 against a local symbol or HI16.
199 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
200   if (!R.Sym)
201     return false;
202
203   MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
204
205   return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
206     (R.Reloc.Type == ELF::R_MIPS_HI16);
207 }
208
209 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
210   if (I == Last)
211     return false;
212
213   RelLsIter Hi = I++;
214
215   return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
216     (Hi->Offset == I->Offset);
217 }
218
219 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
220   return R0.Sym == R1.Sym;
221 }
222
223 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
224   return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
225 }
226
227 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
228                                      std::vector<ELFRelocationEntry> &Relocs) {
229   // Call the default function first. Relocations are sorted in descending
230   // order of r_offset.
231   MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
232
233   RelLs RelocLs;
234   std::vector<RelLsIter> Unmatched;
235
236   // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
237   // are in ascending order of r_offset.
238   for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
239        R != Relocs.rend(); ++R) {
240      std::pair<const MCSymbolRefExpr*, int64_t> P =
241        MipsGetSymAndOffset(*R->Fixup);
242      RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
243                                 P.second));
244   }
245
246   // Get list of unmatched HI16 and GOT16.
247   for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
248     if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
249       Unmatched.push_back(R);
250
251   // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
252   for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
253        U != Unmatched.end(); ++U) {
254     RelLsIter LoPos = RelocLs.end(), HiPos = *U;
255     bool MatchedLo = false;
256
257     for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
258       if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
259           (CompareOffset(*R, *HiPos) >= 0) &&
260           ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
261            (!MatchedLo && !CompareOffset(*R, *LoPos))))
262         LoPos = R;
263
264       MatchedLo = NeedsMatchingLo(Asm, *R) &&
265         HasMatchingLo(Asm, R, --RelocLs.end());
266     }
267
268     // If a matching LoPos was found, move HiPos and insert it before LoPos.
269     // Make the offsets of HiPos and LoPos match.
270     if (LoPos != RelocLs.end()) {
271       HiPos->Offset = LoPos->Offset;
272       RelocLs.insert(LoPos, *HiPos);
273       RelocLs.erase(HiPos);
274     }
275   }
276
277   // Put the sorted list back in reverse order.
278   assert(Relocs.size() == RelocLs.size());
279   unsigned I = RelocLs.size();
280
281   for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
282     Relocs[--I] = R->Reloc;
283 }
284
285 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
286                                                 uint8_t OSABI,
287                                                 bool IsLittleEndian,
288                                                 bool Is64Bit) {
289   MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
290                                                 (Is64Bit) ? true : false,
291                                                 IsLittleEndian);
292   return createELFObjectWriter(MOTW, OS, IsLittleEndian);
293 }