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 unsigned getEFlags() const;
46 virtual const MCSymbol *ExplicitRelSym(const MCAssembler &Asm,
47 const MCValue &Target,
51 virtual void sortRelocs(const MCAssembler &Asm,
52 std::vector<ELFRelocationEntry> &Relocs);
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,
62 MipsELFObjectWriter::~MipsELFObjectWriter() {}
64 // FIXME: get the real EABI Version from the Subtarget class.
65 unsigned MipsELFObjectWriter::getEFlags() const {
67 // FIXME: We can't tell if we are PIC (dynamic) or CPIC (static)
68 unsigned Flag = ELF::EF_MIPS_NOREORDER;
71 Flag |= ELF::EF_MIPS_ARCH_64R2;
73 Flag |= ELF::EF_MIPS_ARCH_32R2;
77 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
78 const MCValue &Target,
82 assert(Target.getSymA() && "SymA cannot be 0.");
83 const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
85 if (Sym.getSection().getKind().isMergeableCString() ||
86 Sym.getSection().getKind().isMergeableConst())
92 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
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();
103 llvm_unreachable("invalid fixup kind!");
105 Type = ELF::R_MIPS_32;
108 Type = ELF::R_MIPS_64;
111 Type = ELF::R_MIPS_GPREL32;
113 case Mips::fixup_Mips_GPREL16:
114 Type = ELF::R_MIPS_GPREL16;
116 case Mips::fixup_Mips_26:
117 Type = ELF::R_MIPS_26;
119 case Mips::fixup_Mips_CALL16:
120 Type = ELF::R_MIPS_CALL16;
122 case Mips::fixup_Mips_GOT_Global:
123 case Mips::fixup_Mips_GOT_Local:
124 Type = ELF::R_MIPS_GOT16;
126 case Mips::fixup_Mips_HI16:
127 Type = ELF::R_MIPS_HI16;
129 case Mips::fixup_Mips_LO16:
130 Type = ELF::R_MIPS_LO16;
132 case Mips::fixup_Mips_TLSGD:
133 Type = ELF::R_MIPS_TLS_GD;
135 case Mips::fixup_Mips_GOTTPREL:
136 Type = ELF::R_MIPS_TLS_GOTTPREL;
138 case Mips::fixup_Mips_TPREL_HI:
139 Type = ELF::R_MIPS_TLS_TPREL_HI16;
141 case Mips::fixup_Mips_TPREL_LO:
142 Type = ELF::R_MIPS_TLS_TPREL_LO16;
144 case Mips::fixup_Mips_TLSLDM:
145 Type = ELF::R_MIPS_TLS_LDM;
147 case Mips::fixup_Mips_DTPREL_HI:
148 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
150 case Mips::fixup_Mips_DTPREL_LO:
151 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
153 case Mips::fixup_Mips_Branch_PCRel:
154 case Mips::fixup_Mips_PC16:
155 Type = ELF::R_MIPS_PC16;
157 case Mips::fixup_Mips_GOT_PAGE:
158 Type = ELF::R_MIPS_GOT_PAGE;
160 case Mips::fixup_Mips_GOT_OFST:
161 Type = ELF::R_MIPS_GOT_OFST;
163 case Mips::fixup_Mips_GOT_DISP:
164 Type = ELF::R_MIPS_GOT_DISP;
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);
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);
176 case Mips::fixup_Mips_HIGHER:
177 Type = ELF::R_MIPS_HIGHER;
179 case Mips::fixup_Mips_HIGHEST:
180 Type = ELF::R_MIPS_HIGHEST;
182 case Mips::fixup_Mips_GOT_HI16:
183 Type = ELF::R_MIPS_GOT_HI16;
185 case Mips::fixup_Mips_GOT_LO16:
186 Type = ELF::R_MIPS_GOT_LO16;
188 case Mips::fixup_Mips_CALL_HI16:
189 Type = ELF::R_MIPS_CALL_HI16;
191 case Mips::fixup_Mips_CALL_LO16:
192 Type = ELF::R_MIPS_CALL_LO16;
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) {
203 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
205 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
206 (R.Reloc.Type == ELF::R_MIPS_HI16);
209 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
215 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
216 (Hi->Offset == I->Offset);
219 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
220 return R0.Sym == R1.Sym;
223 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
224 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
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);
234 std::vector<RelLsIter> Unmatched;
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,
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);
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;
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))))
264 MatchedLo = NeedsMatchingLo(Asm, *R) &&
265 HasMatchingLo(Asm, R, --RelocLs.end());
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);
277 // Put the sorted list back in reverse order.
278 assert(Relocs.size() == RelocLs.size());
279 unsigned I = RelocLs.size();
281 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
282 Relocs[--I] = R->Reloc;
285 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
289 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
290 (Is64Bit) ? true : false,
292 return createELFObjectWriter(MOTW, OS, IsLittleEndian);