blob: 6471b51583ced05e5688b54db74665695bb32e8a [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
Rafael Espindola1dc45d82011-12-22 03:03:17 +00002//
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
Akira Hatanaka5ba593f2012-03-28 00:23:33 +000010#include "MCTargetDesc/MipsBaseInfo.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000011#include "MCTargetDesc/MipsFixupKinds.h"
12#include "MCTargetDesc/MipsMCTargetDesc.h"
Akira Hatanaka5ba593f2012-03-28 00:23:33 +000013#include "llvm/MC/MCAssembler.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000014#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"
Akira Hatanaka5ba593f2012-03-28 00:23:33 +000019#include <list>
Rafael Espindola1dc45d82011-12-22 03:03:17 +000020
21using namespace llvm;
22
23namespace {
Akira Hatanaka5ba593f2012-03-28 00:23:33 +000024 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
Rafael Espindola1dc45d82011-12-22 03:03:17 +000035 class MipsELFObjectWriter : public MCELFObjectTargetWriter {
36 public:
Akira Hatanaka111174b2012-08-17 21:28:04 +000037 MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
38 bool _isN64, bool IsLittleEndian);
Rafael Espindola1dc45d82011-12-22 03:03:17 +000039
40 virtual ~MipsELFObjectWriter();
41
42 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
43 bool IsPCRel, bool IsRelocWithSymbol,
44 int64_t Addend) const;
Rafael Espindola1dc45d82011-12-22 03:03:17 +000045 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
46 const MCValue &Target,
47 const MCFragment &F,
48 const MCFixup &Fixup,
49 bool IsPCRel) const;
Akira Hatanaka5ba593f2012-03-28 00:23:33 +000050 virtual void sortRelocs(const MCAssembler &Asm,
51 std::vector<ELFRelocationEntry> &Relocs);
Rafael Espindola1dc45d82011-12-22 03:03:17 +000052 };
53}
54
Jack Carter8ad0c272012-06-27 22:28:30 +000055MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
Akira Hatanaka111174b2012-08-17 21:28:04 +000056 bool _isN64, bool IsLittleEndian)
Akira Hatanakab1f68f92012-04-02 19:25:22 +000057 : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
Jack Carter77064c02012-08-22 00:49:30 +000058 /*HasRelocationAddend*/ (_isN64) ? true : false,
Jack Carter8ad0c272012-06-27 22:28:30 +000059 /*IsN64*/ _isN64) {}
Rafael Espindola1dc45d82011-12-22 03:03:17 +000060
61MipsELFObjectWriter::~MipsELFObjectWriter() {}
62
Rafael Espindola1dc45d82011-12-22 03:03:17 +000063const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
64 const MCValue &Target,
65 const MCFragment &F,
66 const MCFixup &Fixup,
67 bool IsPCRel) const {
68 assert(Target.getSymA() && "SymA cannot be 0.");
James Molloyb47489d2012-01-28 15:58:32 +000069 const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
Rafael Espindola1dc45d82011-12-22 03:03:17 +000070
71 if (Sym.getSection().getKind().isMergeableCString() ||
72 Sym.getSection().getKind().isMergeableConst())
73 return &Sym;
74
75 return NULL;
76}
77
78unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
79 const MCFixup &Fixup,
80 bool IsPCRel,
81 bool IsRelocWithSymbol,
82 int64_t Addend) const {
83 // determine the type of the relocation
84 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
85 unsigned Kind = (unsigned)Fixup.getKind();
86
87 switch (Kind) {
88 default:
89 llvm_unreachable("invalid fixup kind!");
90 case FK_Data_4:
91 Type = ELF::R_MIPS_32;
92 break;
Jack Carter4c583812012-08-07 00:01:14 +000093 case FK_Data_8:
94 Type = ELF::R_MIPS_64;
95 break;
Rafael Espindola1dc45d82011-12-22 03:03:17 +000096 case FK_GPRel_4:
Jack Carterf2385102013-01-15 01:08:02 +000097 if (isN64()) {
98 Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
99 Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
100 Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
101 }
102 else
103 Type = ELF::R_MIPS_GPREL32;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000104 break;
105 case Mips::fixup_Mips_GPREL16:
106 Type = ELF::R_MIPS_GPREL16;
107 break;
108 case Mips::fixup_Mips_26:
109 Type = ELF::R_MIPS_26;
110 break;
111 case Mips::fixup_Mips_CALL16:
112 Type = ELF::R_MIPS_CALL16;
113 break;
114 case Mips::fixup_Mips_GOT_Global:
115 case Mips::fixup_Mips_GOT_Local:
116 Type = ELF::R_MIPS_GOT16;
117 break;
118 case Mips::fixup_Mips_HI16:
119 Type = ELF::R_MIPS_HI16;
120 break;
121 case Mips::fixup_Mips_LO16:
122 Type = ELF::R_MIPS_LO16;
123 break;
124 case Mips::fixup_Mips_TLSGD:
125 Type = ELF::R_MIPS_TLS_GD;
126 break;
127 case Mips::fixup_Mips_GOTTPREL:
128 Type = ELF::R_MIPS_TLS_GOTTPREL;
129 break;
130 case Mips::fixup_Mips_TPREL_HI:
131 Type = ELF::R_MIPS_TLS_TPREL_HI16;
132 break;
133 case Mips::fixup_Mips_TPREL_LO:
134 Type = ELF::R_MIPS_TLS_TPREL_LO16;
135 break;
136 case Mips::fixup_Mips_TLSLDM:
137 Type = ELF::R_MIPS_TLS_LDM;
138 break;
139 case Mips::fixup_Mips_DTPREL_HI:
140 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
141 break;
142 case Mips::fixup_Mips_DTPREL_LO:
143 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
144 break;
145 case Mips::fixup_Mips_Branch_PCRel:
146 case Mips::fixup_Mips_PC16:
147 Type = ELF::R_MIPS_PC16;
148 break;
Jack Carterb9f9de92012-06-27 22:48:25 +0000149 case Mips::fixup_Mips_GOT_PAGE:
150 Type = ELF::R_MIPS_GOT_PAGE;
151 break;
152 case Mips::fixup_Mips_GOT_OFST:
153 Type = ELF::R_MIPS_GOT_OFST;
154 break;
Jack Carter5ddcfda2012-07-13 19:15:47 +0000155 case Mips::fixup_Mips_GOT_DISP:
156 Type = ELF::R_MIPS_GOT_DISP;
157 break;
Jack Carterb9f9de92012-06-27 22:48:25 +0000158 case Mips::fixup_Mips_GPOFF_HI:
159 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
160 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
161 Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
162 break;
163 case Mips::fixup_Mips_GPOFF_LO:
164 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
165 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
166 Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
167 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000168 case Mips::fixup_Mips_HIGHER:
169 Type = ELF::R_MIPS_HIGHER;
170 break;
171 case Mips::fixup_Mips_HIGHEST:
172 Type = ELF::R_MIPS_HIGHEST;
173 break;
Jack Carterb05cb672012-11-21 23:38:59 +0000174 case Mips::fixup_Mips_GOT_HI16:
175 Type = ELF::R_MIPS_GOT_HI16;
176 break;
177 case Mips::fixup_Mips_GOT_LO16:
178 Type = ELF::R_MIPS_GOT_LO16;
179 break;
180 case Mips::fixup_Mips_CALL_HI16:
181 Type = ELF::R_MIPS_CALL_HI16;
182 break;
183 case Mips::fixup_Mips_CALL_LO16:
184 Type = ELF::R_MIPS_CALL_LO16;
185 break;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000186 }
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000187 return Type;
188}
189
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000190// Return true if R is either a GOT16 against a local symbol or HI16.
191static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
192 if (!R.Sym)
193 return false;
194
195 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
196
197 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
198 (R.Reloc.Type == ELF::R_MIPS_HI16);
199}
200
201static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
202 if (I == Last)
203 return false;
204
205 RelLsIter Hi = I++;
206
207 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
208 (Hi->Offset == I->Offset);
209}
210
211static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
212 return R0.Sym == R1.Sym;
213}
214
215static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
216 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
217}
218
219void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
220 std::vector<ELFRelocationEntry> &Relocs) {
Jack Carter8ad0c272012-06-27 22:28:30 +0000221 // Call the default function first. Relocations are sorted in descending
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000222 // order of r_offset.
223 MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000224
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000225 RelLs RelocLs;
226 std::vector<RelLsIter> Unmatched;
227
228 // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
229 // are in ascending order of r_offset.
230 for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
231 R != Relocs.rend(); ++R) {
232 std::pair<const MCSymbolRefExpr*, int64_t> P =
233 MipsGetSymAndOffset(*R->Fixup);
234 RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
235 P.second));
236 }
237
238 // Get list of unmatched HI16 and GOT16.
239 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
240 if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
241 Unmatched.push_back(R);
242
243 // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
244 for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
245 U != Unmatched.end(); ++U) {
246 RelLsIter LoPos = RelocLs.end(), HiPos = *U;
247 bool MatchedLo = false;
248
249 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
250 if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
251 (CompareOffset(*R, *HiPos) >= 0) &&
252 ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
253 (!MatchedLo && !CompareOffset(*R, *LoPos))))
254 LoPos = R;
255
256 MatchedLo = NeedsMatchingLo(Asm, *R) &&
257 HasMatchingLo(Asm, R, --RelocLs.end());
258 }
259
260 // If a matching LoPos was found, move HiPos and insert it before LoPos.
261 // Make the offsets of HiPos and LoPos match.
262 if (LoPos != RelocLs.end()) {
263 HiPos->Offset = LoPos->Offset;
264 RelocLs.insert(LoPos, *HiPos);
265 RelocLs.erase(HiPos);
266 }
267 }
268
269 // Put the sorted list back in reverse order.
270 assert(Relocs.size() == RelocLs.size());
271 unsigned I = RelocLs.size();
272
273 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
274 Relocs[--I] = R->Reloc;
275}
276
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000277MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
278 uint8_t OSABI,
279 bool IsLittleEndian,
280 bool Is64Bit) {
Jack Carter8ad0c272012-06-27 22:28:30 +0000281 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
Akira Hatanaka111174b2012-08-17 21:28:04 +0000282 (Is64Bit) ? true : false,
283 IsLittleEndian);
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000284 return createELFObjectWriter(MOTW, OS, IsLittleEndian);
285}