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 unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
43 bool IsPCRel, bool IsRelocWithSymbol) const override;
44 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
45 const MCValue &Target,
49 virtual void sortRelocs(const MCAssembler &Asm,
50 std::vector<ELFRelocationEntry> &Relocs);
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,
60 MipsELFObjectWriter::~MipsELFObjectWriter() {}
62 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
63 const MCValue &Target,
67 assert(Target.getSymA() && "SymA cannot be 0.");
68 const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
70 if (Sym.getSection().getKind().isMergeableCString() ||
71 Sym.getSection().getKind().isMergeableConst())
77 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
80 bool IsRelocWithSymbol) const {
81 // determine the type of the relocation
82 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
83 unsigned Kind = (unsigned)Fixup.getKind();
87 llvm_unreachable("invalid fixup kind!");
89 Type = ELF::R_MIPS_32;
92 Type = ELF::R_MIPS_64;
96 Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
97 Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
98 Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
101 Type = ELF::R_MIPS_GPREL32;
103 case Mips::fixup_Mips_GPREL16:
104 Type = ELF::R_MIPS_GPREL16;
106 case Mips::fixup_Mips_26:
107 Type = ELF::R_MIPS_26;
109 case Mips::fixup_Mips_CALL16:
110 Type = ELF::R_MIPS_CALL16;
112 case Mips::fixup_Mips_GOT_Global:
113 case Mips::fixup_Mips_GOT_Local:
114 Type = ELF::R_MIPS_GOT16;
116 case Mips::fixup_Mips_HI16:
117 Type = ELF::R_MIPS_HI16;
119 case Mips::fixup_Mips_LO16:
120 Type = ELF::R_MIPS_LO16;
122 case Mips::fixup_Mips_TLSGD:
123 Type = ELF::R_MIPS_TLS_GD;
125 case Mips::fixup_Mips_GOTTPREL:
126 Type = ELF::R_MIPS_TLS_GOTTPREL;
128 case Mips::fixup_Mips_TPREL_HI:
129 Type = ELF::R_MIPS_TLS_TPREL_HI16;
131 case Mips::fixup_Mips_TPREL_LO:
132 Type = ELF::R_MIPS_TLS_TPREL_LO16;
134 case Mips::fixup_Mips_TLSLDM:
135 Type = ELF::R_MIPS_TLS_LDM;
137 case Mips::fixup_Mips_DTPREL_HI:
138 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
140 case Mips::fixup_Mips_DTPREL_LO:
141 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
143 case Mips::fixup_Mips_Branch_PCRel:
144 case Mips::fixup_Mips_PC16:
145 Type = ELF::R_MIPS_PC16;
147 case Mips::fixup_Mips_GOT_PAGE:
148 Type = ELF::R_MIPS_GOT_PAGE;
150 case Mips::fixup_Mips_GOT_OFST:
151 Type = ELF::R_MIPS_GOT_OFST;
153 case Mips::fixup_Mips_GOT_DISP:
154 Type = ELF::R_MIPS_GOT_DISP;
156 case Mips::fixup_Mips_GPOFF_HI:
157 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
158 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
159 Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
161 case Mips::fixup_Mips_GPOFF_LO:
162 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
163 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
164 Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
166 case Mips::fixup_Mips_HIGHER:
167 Type = ELF::R_MIPS_HIGHER;
169 case Mips::fixup_Mips_HIGHEST:
170 Type = ELF::R_MIPS_HIGHEST;
172 case Mips::fixup_Mips_GOT_HI16:
173 Type = ELF::R_MIPS_GOT_HI16;
175 case Mips::fixup_Mips_GOT_LO16:
176 Type = ELF::R_MIPS_GOT_LO16;
178 case Mips::fixup_Mips_CALL_HI16:
179 Type = ELF::R_MIPS_CALL_HI16;
181 case Mips::fixup_Mips_CALL_LO16:
182 Type = ELF::R_MIPS_CALL_LO16;
184 case Mips::fixup_MICROMIPS_26_S1:
185 Type = ELF::R_MICROMIPS_26_S1;
187 case Mips::fixup_MICROMIPS_HI16:
188 Type = ELF::R_MICROMIPS_HI16;
190 case Mips::fixup_MICROMIPS_LO16:
191 Type = ELF::R_MICROMIPS_LO16;
193 case Mips::fixup_MICROMIPS_GOT16:
194 Type = ELF::R_MICROMIPS_GOT16;
196 case Mips::fixup_MICROMIPS_PC16_S1:
197 Type = ELF::R_MICROMIPS_PC16_S1;
199 case Mips::fixup_MICROMIPS_CALL16:
200 Type = ELF::R_MICROMIPS_CALL16;
202 case Mips::fixup_MICROMIPS_GOT_DISP:
203 Type = ELF::R_MICROMIPS_GOT_DISP;
205 case Mips::fixup_MICROMIPS_GOT_PAGE:
206 Type = ELF::R_MICROMIPS_GOT_PAGE;
208 case Mips::fixup_MICROMIPS_GOT_OFST:
209 Type = ELF::R_MICROMIPS_GOT_OFST;
211 case Mips::fixup_MICROMIPS_TLS_GD:
212 Type = ELF::R_MICROMIPS_TLS_GD;
214 case Mips::fixup_MICROMIPS_TLS_LDM:
215 Type = ELF::R_MICROMIPS_TLS_LDM;
217 case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
218 Type = ELF::R_MICROMIPS_TLS_DTPREL_HI16;
220 case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
221 Type = ELF::R_MICROMIPS_TLS_DTPREL_LO16;
223 case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
224 Type = ELF::R_MICROMIPS_TLS_TPREL_HI16;
226 case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
227 Type = ELF::R_MICROMIPS_TLS_TPREL_LO16;
233 // Return true if R is either a GOT16 against a local symbol or HI16.
234 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
238 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
240 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
241 (R.Reloc.Type == ELF::R_MIPS_HI16);
244 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
250 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
251 (Hi->Offset == I->Offset);
254 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
255 return R0.Sym == R1.Sym;
258 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
259 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
262 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
263 std::vector<ELFRelocationEntry> &Relocs) {
264 // Call the default function first. Relocations are sorted in descending
265 // order of r_offset.
266 MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
269 std::vector<RelLsIter> Unmatched;
271 // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
272 // are in ascending order of r_offset.
273 for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
274 R != Relocs.rend(); ++R) {
275 std::pair<const MCSymbolRefExpr*, int64_t> P =
276 MipsGetSymAndOffset(*R->Fixup);
277 RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
281 // Get list of unmatched HI16 and GOT16.
282 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
283 if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
284 Unmatched.push_back(R);
286 // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
287 for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
288 U != Unmatched.end(); ++U) {
289 RelLsIter LoPos = RelocLs.end(), HiPos = *U;
290 bool MatchedLo = false;
292 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
293 if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
294 (CompareOffset(*R, *HiPos) >= 0) &&
295 ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
296 (!MatchedLo && !CompareOffset(*R, *LoPos))))
299 MatchedLo = NeedsMatchingLo(Asm, *R) &&
300 HasMatchingLo(Asm, R, --RelocLs.end());
303 // If a matching LoPos was found, move HiPos and insert it before LoPos.
304 // Make the offsets of HiPos and LoPos match.
305 if (LoPos != RelocLs.end()) {
306 HiPos->Offset = LoPos->Offset;
307 RelocLs.insert(LoPos, *HiPos);
308 RelocLs.erase(HiPos);
312 // Put the sorted list back in reverse order.
313 assert(Relocs.size() == RelocLs.size());
314 unsigned I = RelocLs.size();
316 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
317 Relocs[--I] = R->Reloc;
320 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
324 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
325 (Is64Bit) ? true : false,
327 return createELFObjectWriter(MOTW, OS, IsLittleEndian);