1 //===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
25 RelEntry(const ELFRelocationEntry &R, const MCSymbol *S, int64_t O) :
26 Reloc(R), Sym(S), Offset(O) {}
27 ELFRelocationEntry Reloc;
32 typedef std::list<RelEntry> RelLs;
33 typedef RelLs::iterator RelLsIter;
35 class MipsELFObjectWriter : public MCELFObjectTargetWriter {
37 MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
38 bool _isN64, bool IsLittleEndian);
40 virtual ~MipsELFObjectWriter();
42 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
43 bool IsPCRel, bool IsRelocWithSymbol,
44 int64_t Addend) const;
45 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
46 const MCValue &Target,
50 virtual void sortRelocs(const MCAssembler &Asm,
51 std::vector<ELFRelocationEntry> &Relocs);
55 MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
56 bool _isN64, bool IsLittleEndian)
57 : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
58 /*HasRelocationAddend*/ (_isN64) ? true : false,
61 MipsELFObjectWriter::~MipsELFObjectWriter() {}
63 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
64 const MCValue &Target,
68 assert(Target.getSymA() && "SymA cannot be 0.");
69 const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
71 if (Sym.getSection().getKind().isMergeableCString() ||
72 Sym.getSection().getKind().isMergeableConst())
78 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
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();
89 llvm_unreachable("invalid fixup kind!");
91 Type = ELF::R_MIPS_32;
94 Type = ELF::R_MIPS_64;
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);
103 Type = ELF::R_MIPS_GPREL32;
105 case Mips::fixup_Mips_GPREL16:
106 Type = ELF::R_MIPS_GPREL16;
108 case Mips::fixup_Mips_26:
109 Type = ELF::R_MIPS_26;
111 case Mips::fixup_Mips_CALL16:
112 Type = ELF::R_MIPS_CALL16;
114 case Mips::fixup_Mips_GOT_Global:
115 case Mips::fixup_Mips_GOT_Local:
116 Type = ELF::R_MIPS_GOT16;
118 case Mips::fixup_Mips_HI16:
119 Type = ELF::R_MIPS_HI16;
121 case Mips::fixup_Mips_LO16:
122 Type = ELF::R_MIPS_LO16;
124 case Mips::fixup_Mips_TLSGD:
125 Type = ELF::R_MIPS_TLS_GD;
127 case Mips::fixup_Mips_GOTTPREL:
128 Type = ELF::R_MIPS_TLS_GOTTPREL;
130 case Mips::fixup_Mips_TPREL_HI:
131 Type = ELF::R_MIPS_TLS_TPREL_HI16;
133 case Mips::fixup_Mips_TPREL_LO:
134 Type = ELF::R_MIPS_TLS_TPREL_LO16;
136 case Mips::fixup_Mips_TLSLDM:
137 Type = ELF::R_MIPS_TLS_LDM;
139 case Mips::fixup_Mips_DTPREL_HI:
140 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
142 case Mips::fixup_Mips_DTPREL_LO:
143 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
145 case Mips::fixup_Mips_Branch_PCRel:
146 case Mips::fixup_Mips_PC16:
147 Type = ELF::R_MIPS_PC16;
149 case Mips::fixup_Mips_GOT_PAGE:
150 Type = ELF::R_MIPS_GOT_PAGE;
152 case Mips::fixup_Mips_GOT_OFST:
153 Type = ELF::R_MIPS_GOT_OFST;
155 case Mips::fixup_Mips_GOT_DISP:
156 Type = ELF::R_MIPS_GOT_DISP;
158 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);
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);
168 case Mips::fixup_Mips_HIGHER:
169 Type = ELF::R_MIPS_HIGHER;
171 case Mips::fixup_Mips_HIGHEST:
172 Type = ELF::R_MIPS_HIGHEST;
174 case Mips::fixup_Mips_GOT_HI16:
175 Type = ELF::R_MIPS_GOT_HI16;
177 case Mips::fixup_Mips_GOT_LO16:
178 Type = ELF::R_MIPS_GOT_LO16;
180 case Mips::fixup_Mips_CALL_HI16:
181 Type = ELF::R_MIPS_CALL_HI16;
183 case Mips::fixup_Mips_CALL_LO16:
184 Type = ELF::R_MIPS_CALL_LO16;
186 case Mips::fixup_MICROMIPS_26_S1:
187 Type = ELF::R_MICROMIPS_26_S1;
189 case Mips::fixup_MICROMIPS_HI16:
190 Type = ELF::R_MICROMIPS_HI16;
192 case Mips::fixup_MICROMIPS_LO16:
193 Type = ELF::R_MICROMIPS_LO16;
195 case Mips::fixup_MICROMIPS_GOT16:
196 Type = ELF::R_MICROMIPS_GOT16;
198 case Mips::fixup_MICROMIPS_PC16_S1:
199 Type = ELF::R_MICROMIPS_PC16_S1;
201 case Mips::fixup_MICROMIPS_CALL16:
202 Type = ELF::R_MICROMIPS_CALL16;
204 case Mips::fixup_MICROMIPS_GOT_DISP:
205 Type = ELF::R_MICROMIPS_GOT_DISP;
207 case Mips::fixup_MICROMIPS_GOT_PAGE:
208 Type = ELF::R_MICROMIPS_GOT_PAGE;
210 case Mips::fixup_MICROMIPS_GOT_OFST:
211 Type = ELF::R_MICROMIPS_GOT_OFST;
213 case Mips::fixup_MICROMIPS_TLS_GD:
214 Type = ELF::R_MICROMIPS_TLS_GD;
216 case Mips::fixup_MICROMIPS_TLS_LDM:
217 Type = ELF::R_MICROMIPS_TLS_LDM;
219 case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
220 Type = ELF::R_MICROMIPS_TLS_DTPREL_HI16;
222 case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
223 Type = ELF::R_MICROMIPS_TLS_DTPREL_LO16;
225 case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
226 Type = ELF::R_MICROMIPS_TLS_TPREL_HI16;
228 case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
229 Type = ELF::R_MICROMIPS_TLS_TPREL_LO16;
235 // Return true if R is either a GOT16 against a local symbol or HI16.
236 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
240 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
242 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
243 (R.Reloc.Type == ELF::R_MIPS_HI16);
246 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
252 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
253 (Hi->Offset == I->Offset);
256 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
257 return R0.Sym == R1.Sym;
260 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
261 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
264 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
265 std::vector<ELFRelocationEntry> &Relocs) {
266 // Call the default function first. Relocations are sorted in descending
267 // order of r_offset.
268 MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
271 std::vector<RelLsIter> Unmatched;
273 // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
274 // are in ascending order of r_offset.
275 for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
276 R != Relocs.rend(); ++R) {
277 std::pair<const MCSymbolRefExpr*, int64_t> P =
278 MipsGetSymAndOffset(*R->Fixup);
279 RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
283 // Get list of unmatched HI16 and GOT16.
284 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
285 if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
286 Unmatched.push_back(R);
288 // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
289 for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
290 U != Unmatched.end(); ++U) {
291 RelLsIter LoPos = RelocLs.end(), HiPos = *U;
292 bool MatchedLo = false;
294 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
295 if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
296 (CompareOffset(*R, *HiPos) >= 0) &&
297 ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
298 (!MatchedLo && !CompareOffset(*R, *LoPos))))
301 MatchedLo = NeedsMatchingLo(Asm, *R) &&
302 HasMatchingLo(Asm, R, --RelocLs.end());
305 // If a matching LoPos was found, move HiPos and insert it before LoPos.
306 // Make the offsets of HiPos and LoPos match.
307 if (LoPos != RelocLs.end()) {
308 HiPos->Offset = LoPos->Offset;
309 RelocLs.insert(LoPos, *HiPos);
310 RelocLs.erase(HiPos);
314 // Put the sorted list back in reverse order.
315 assert(Relocs.size() == RelocLs.size());
316 unsigned I = RelocLs.size();
318 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
319 Relocs[--I] = R->Reloc;
322 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
326 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
327 (Is64Bit) ? true : false,
329 return createELFObjectWriter(MOTW, OS, IsLittleEndian);