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*/ 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;
186 // Return true if R is either a GOT16 against a local symbol or HI16.
187 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
191 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
193 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
194 (R.Reloc.Type == ELF::R_MIPS_HI16);
197 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
203 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
204 (Hi->Offset == I->Offset);
207 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
208 return R0.Sym == R1.Sym;
211 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
212 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
215 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
216 std::vector<ELFRelocationEntry> &Relocs) {
217 // Call the default function first. Relocations are sorted in descending
218 // order of r_offset.
219 MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
222 std::vector<RelLsIter> Unmatched;
224 // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
225 // are in ascending order of r_offset.
226 for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
227 R != Relocs.rend(); ++R) {
228 std::pair<const MCSymbolRefExpr*, int64_t> P =
229 MipsGetSymAndOffset(*R->Fixup);
230 RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
234 // Get list of unmatched HI16 and GOT16.
235 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
236 if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
237 Unmatched.push_back(R);
239 // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
240 for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
241 U != Unmatched.end(); ++U) {
242 RelLsIter LoPos = RelocLs.end(), HiPos = *U;
243 bool MatchedLo = false;
245 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
246 if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
247 (CompareOffset(*R, *HiPos) >= 0) &&
248 ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
249 (!MatchedLo && !CompareOffset(*R, *LoPos))))
252 MatchedLo = NeedsMatchingLo(Asm, *R) &&
253 HasMatchingLo(Asm, R, --RelocLs.end());
256 // If a matching LoPos was found, move HiPos and insert it before LoPos.
257 // Make the offsets of HiPos and LoPos match.
258 if (LoPos != RelocLs.end()) {
259 HiPos->Offset = LoPos->Offset;
260 RelocLs.insert(LoPos, *HiPos);
261 RelocLs.erase(HiPos);
265 // Put the sorted list back in reverse order.
266 assert(Relocs.size() == RelocLs.size());
267 unsigned I = RelocLs.size();
269 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
270 Relocs[--I] = R->Reloc;
273 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
277 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI,
278 (Is64Bit) ? true : false,
280 return createELFObjectWriter(MOTW, OS, IsLittleEndian);