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(uint8_t OSABI);
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(uint8_t OSABI)
56 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_MIPS,
57 /*HasRelocationAddend*/ false) {}
59 MipsELFObjectWriter::~MipsELFObjectWriter() {}
61 // FIXME: get the real EABI Version from the Triple.
62 unsigned MipsELFObjectWriter::getEFlags() const {
63 return ELF::EF_MIPS_NOREORDER | ELF::EF_MIPS_ARCH_32R2;
66 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
67 const MCValue &Target,
71 assert(Target.getSymA() && "SymA cannot be 0.");
72 const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
74 if (Sym.getSection().getKind().isMergeableCString() ||
75 Sym.getSection().getKind().isMergeableConst())
81 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
84 bool IsRelocWithSymbol,
85 int64_t Addend) const {
86 // determine the type of the relocation
87 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
88 unsigned Kind = (unsigned)Fixup.getKind();
92 llvm_unreachable("invalid fixup kind!");
94 Type = ELF::R_MIPS_32;
97 Type = ELF::R_MIPS_GPREL32;
99 case Mips::fixup_Mips_GPREL16:
100 Type = ELF::R_MIPS_GPREL16;
102 case Mips::fixup_Mips_26:
103 Type = ELF::R_MIPS_26;
105 case Mips::fixup_Mips_CALL16:
106 Type = ELF::R_MIPS_CALL16;
108 case Mips::fixup_Mips_GOT_Global:
109 case Mips::fixup_Mips_GOT_Local:
110 Type = ELF::R_MIPS_GOT16;
112 case Mips::fixup_Mips_HI16:
113 Type = ELF::R_MIPS_HI16;
115 case Mips::fixup_Mips_LO16:
116 Type = ELF::R_MIPS_LO16;
118 case Mips::fixup_Mips_TLSGD:
119 Type = ELF::R_MIPS_TLS_GD;
121 case Mips::fixup_Mips_GOTTPREL:
122 Type = ELF::R_MIPS_TLS_GOTTPREL;
124 case Mips::fixup_Mips_TPREL_HI:
125 Type = ELF::R_MIPS_TLS_TPREL_HI16;
127 case Mips::fixup_Mips_TPREL_LO:
128 Type = ELF::R_MIPS_TLS_TPREL_LO16;
130 case Mips::fixup_Mips_TLSLDM:
131 Type = ELF::R_MIPS_TLS_LDM;
133 case Mips::fixup_Mips_DTPREL_HI:
134 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
136 case Mips::fixup_Mips_DTPREL_LO:
137 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
139 case Mips::fixup_Mips_Branch_PCRel:
140 case Mips::fixup_Mips_PC16:
141 Type = ELF::R_MIPS_PC16;
148 // Return true if R is either a GOT16 against a local symbol or HI16.
149 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
153 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
155 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
156 (R.Reloc.Type == ELF::R_MIPS_HI16);
159 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
165 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
166 (Hi->Offset == I->Offset);
169 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
170 return R0.Sym == R1.Sym;
173 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
174 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
177 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
178 std::vector<ELFRelocationEntry> &Relocs) {
179 // Call the defualt function first. Relocations are sorted in descending
180 // order of r_offset.
181 MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
184 std::vector<RelLsIter> Unmatched;
186 // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
187 // are in ascending order of r_offset.
188 for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
189 R != Relocs.rend(); ++R) {
190 std::pair<const MCSymbolRefExpr*, int64_t> P =
191 MipsGetSymAndOffset(*R->Fixup);
192 RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
196 // Get list of unmatched HI16 and GOT16.
197 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
198 if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
199 Unmatched.push_back(R);
201 // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
202 for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
203 U != Unmatched.end(); ++U) {
204 RelLsIter LoPos = RelocLs.end(), HiPos = *U;
205 bool MatchedLo = false;
207 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
208 if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
209 (CompareOffset(*R, *HiPos) >= 0) &&
210 ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
211 (!MatchedLo && !CompareOffset(*R, *LoPos))))
214 MatchedLo = NeedsMatchingLo(Asm, *R) &&
215 HasMatchingLo(Asm, R, --RelocLs.end());
218 // If a matching LoPos was found, move HiPos and insert it before LoPos.
219 // Make the offsets of HiPos and LoPos match.
220 if (LoPos != RelocLs.end()) {
221 HiPos->Offset = LoPos->Offset;
222 RelocLs.insert(LoPos, *HiPos);
223 RelocLs.erase(HiPos);
227 // Put the sorted list back in reverse order.
228 assert(Relocs.size() == RelocLs.size());
229 unsigned I = RelocLs.size();
231 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
232 Relocs[--I] = R->Reloc;
235 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS, uint8_t OSABI,
236 bool IsLittleEndian) {
237 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(OSABI);
238 return createELFObjectWriter(MOTW, OS, IsLittleEndian);