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, bool _isN64);
39 virtual ~MipsELFObjectWriter();
41 virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
42 bool IsPCRel, bool IsRelocWithSymbol,
43 int64_t Addend) const;
44 virtual unsigned getEFlags() 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,
57 : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
58 /*HasRelocationAddend*/ false,
61 MipsELFObjectWriter::~MipsELFObjectWriter() {}
63 // FIXME: get the real EABI Version from the Subtarget class.
64 unsigned MipsELFObjectWriter::getEFlags() const {
66 // FIXME: We can't tell if we are PIC (dynamic) or CPIC (static)
67 unsigned Flag = ELF::EF_MIPS_NOREORDER;
70 Flag |= ELF::EF_MIPS_ARCH_64R2;
72 Flag |= ELF::EF_MIPS_ARCH_32R2;
76 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
77 const MCValue &Target,
81 assert(Target.getSymA() && "SymA cannot be 0.");
82 const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
84 if (Sym.getSection().getKind().isMergeableCString() ||
85 Sym.getSection().getKind().isMergeableConst())
91 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
94 bool IsRelocWithSymbol,
95 int64_t Addend) const {
96 // determine the type of the relocation
97 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
98 unsigned Kind = (unsigned)Fixup.getKind();
102 llvm_unreachable("invalid fixup kind!");
104 Type = ELF::R_MIPS_32;
107 Type = ELF::R_MIPS_GPREL32;
109 case Mips::fixup_Mips_GPREL16:
110 Type = ELF::R_MIPS_GPREL16;
112 case Mips::fixup_Mips_26:
113 Type = ELF::R_MIPS_26;
115 case Mips::fixup_Mips_CALL16:
116 Type = ELF::R_MIPS_CALL16;
118 case Mips::fixup_Mips_GOT_Global:
119 case Mips::fixup_Mips_GOT_Local:
120 Type = ELF::R_MIPS_GOT16;
122 case Mips::fixup_Mips_HI16:
123 Type = ELF::R_MIPS_HI16;
125 case Mips::fixup_Mips_LO16:
126 Type = ELF::R_MIPS_LO16;
128 case Mips::fixup_Mips_TLSGD:
129 Type = ELF::R_MIPS_TLS_GD;
131 case Mips::fixup_Mips_GOTTPREL:
132 Type = ELF::R_MIPS_TLS_GOTTPREL;
134 case Mips::fixup_Mips_TPREL_HI:
135 Type = ELF::R_MIPS_TLS_TPREL_HI16;
137 case Mips::fixup_Mips_TPREL_LO:
138 Type = ELF::R_MIPS_TLS_TPREL_LO16;
140 case Mips::fixup_Mips_TLSLDM:
141 Type = ELF::R_MIPS_TLS_LDM;
143 case Mips::fixup_Mips_DTPREL_HI:
144 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
146 case Mips::fixup_Mips_DTPREL_LO:
147 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
149 case Mips::fixup_Mips_Branch_PCRel:
150 case Mips::fixup_Mips_PC16:
151 Type = ELF::R_MIPS_PC16;
153 case Mips::fixup_Mips_GOT_PAGE:
154 Type = ELF::R_MIPS_GOT_PAGE;
156 case Mips::fixup_Mips_GOT_OFST:
157 Type = ELF::R_MIPS_GOT_OFST;
159 case Mips::fixup_Mips_GPOFF_HI:
160 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
161 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
162 Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
164 case Mips::fixup_Mips_GPOFF_LO:
165 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
166 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
167 Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
173 // Return true if R is either a GOT16 against a local symbol or HI16.
174 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
178 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
180 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
181 (R.Reloc.Type == ELF::R_MIPS_HI16);
184 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
190 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
191 (Hi->Offset == I->Offset);
194 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
195 return R0.Sym == R1.Sym;
198 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
199 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
202 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
203 std::vector<ELFRelocationEntry> &Relocs) {
204 // Call the default function first. Relocations are sorted in descending
205 // order of r_offset.
206 MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
209 std::vector<RelLsIter> Unmatched;
211 // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
212 // are in ascending order of r_offset.
213 for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
214 R != Relocs.rend(); ++R) {
215 std::pair<const MCSymbolRefExpr*, int64_t> P =
216 MipsGetSymAndOffset(*R->Fixup);
217 RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
221 // Get list of unmatched HI16 and GOT16.
222 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
223 if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
224 Unmatched.push_back(R);
226 // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
227 for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
228 U != Unmatched.end(); ++U) {
229 RelLsIter LoPos = RelocLs.end(), HiPos = *U;
230 bool MatchedLo = false;
232 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
233 if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
234 (CompareOffset(*R, *HiPos) >= 0) &&
235 ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
236 (!MatchedLo && !CompareOffset(*R, *LoPos))))
239 MatchedLo = NeedsMatchingLo(Asm, *R) &&
240 HasMatchingLo(Asm, R, --RelocLs.end());
243 // If a matching LoPos was found, move HiPos and insert it before LoPos.
244 // Make the offsets of HiPos and LoPos match.
245 if (LoPos != RelocLs.end()) {
246 HiPos->Offset = LoPos->Offset;
247 RelocLs.insert(LoPos, *HiPos);
248 RelocLs.erase(HiPos);
252 // Put the sorted list back in reverse order.
253 assert(Relocs.size() == RelocLs.size());
254 unsigned I = RelocLs.size();
256 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
257 Relocs[--I] = R->Reloc;
260 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
264 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
265 (Is64Bit) ? true : false);
266 return createELFObjectWriter(MOTW, OS, IsLittleEndian);