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);
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)
56 : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
57 /*HasRelocationAddend*/ false) {}
59 MipsELFObjectWriter::~MipsELFObjectWriter() {}
61 // FIXME: get the real EABI Version from the Subtarget class.
62 unsigned MipsELFObjectWriter::getEFlags() const {
64 // FIXME: We can't tell if we are PIC (dynamic) or CPIC (static)
65 unsigned Flag = ELF::EF_MIPS_NOREORDER;
68 Flag |= ELF::EF_MIPS_ARCH_64R2;
70 Flag |= ELF::EF_MIPS_ARCH_32R2;
74 const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
75 const MCValue &Target,
79 assert(Target.getSymA() && "SymA cannot be 0.");
80 const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
82 if (Sym.getSection().getKind().isMergeableCString() ||
83 Sym.getSection().getKind().isMergeableConst())
89 unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
92 bool IsRelocWithSymbol,
93 int64_t Addend) const {
94 // determine the type of the relocation
95 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
96 unsigned Kind = (unsigned)Fixup.getKind();
100 llvm_unreachable("invalid fixup kind!");
102 Type = ELF::R_MIPS_32;
105 Type = ELF::R_MIPS_GPREL32;
107 case Mips::fixup_Mips_GPREL16:
108 Type = ELF::R_MIPS_GPREL16;
110 case Mips::fixup_Mips_26:
111 Type = ELF::R_MIPS_26;
113 case Mips::fixup_Mips_CALL16:
114 Type = ELF::R_MIPS_CALL16;
116 case Mips::fixup_Mips_GOT_Global:
117 case Mips::fixup_Mips_GOT_Local:
118 Type = ELF::R_MIPS_GOT16;
120 case Mips::fixup_Mips_HI16:
121 Type = ELF::R_MIPS_HI16;
123 case Mips::fixup_Mips_LO16:
124 Type = ELF::R_MIPS_LO16;
126 case Mips::fixup_Mips_TLSGD:
127 Type = ELF::R_MIPS_TLS_GD;
129 case Mips::fixup_Mips_GOTTPREL:
130 Type = ELF::R_MIPS_TLS_GOTTPREL;
132 case Mips::fixup_Mips_TPREL_HI:
133 Type = ELF::R_MIPS_TLS_TPREL_HI16;
135 case Mips::fixup_Mips_TPREL_LO:
136 Type = ELF::R_MIPS_TLS_TPREL_LO16;
138 case Mips::fixup_Mips_TLSLDM:
139 Type = ELF::R_MIPS_TLS_LDM;
141 case Mips::fixup_Mips_DTPREL_HI:
142 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
144 case Mips::fixup_Mips_DTPREL_LO:
145 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
147 case Mips::fixup_Mips_Branch_PCRel:
148 case Mips::fixup_Mips_PC16:
149 Type = ELF::R_MIPS_PC16;
156 // Return true if R is either a GOT16 against a local symbol or HI16.
157 static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
161 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
163 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
164 (R.Reloc.Type == ELF::R_MIPS_HI16);
167 static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
173 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
174 (Hi->Offset == I->Offset);
177 static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
178 return R0.Sym == R1.Sym;
181 static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
182 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
185 void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
186 std::vector<ELFRelocationEntry> &Relocs) {
187 // Call the defualt function first. Relocations are sorted in descending
188 // order of r_offset.
189 MCELFObjectTargetWriter::sortRelocs(Asm, Relocs);
192 std::vector<RelLsIter> Unmatched;
194 // Fill RelocLs. Traverse Relocs backwards so that relocations in RelocLs
195 // are in ascending order of r_offset.
196 for (std::vector<ELFRelocationEntry>::reverse_iterator R = Relocs.rbegin();
197 R != Relocs.rend(); ++R) {
198 std::pair<const MCSymbolRefExpr*, int64_t> P =
199 MipsGetSymAndOffset(*R->Fixup);
200 RelocLs.push_back(RelEntry(*R, P.first ? &P.first->getSymbol() : 0,
204 // Get list of unmatched HI16 and GOT16.
205 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
206 if (NeedsMatchingLo(Asm, *R) && !HasMatchingLo(Asm, R, --RelocLs.end()))
207 Unmatched.push_back(R);
209 // Insert unmatched HI16 and GOT16 immediately before their matching LO16.
210 for (std::vector<RelLsIter>::iterator U = Unmatched.begin();
211 U != Unmatched.end(); ++U) {
212 RelLsIter LoPos = RelocLs.end(), HiPos = *U;
213 bool MatchedLo = false;
215 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R) {
216 if ((R->Reloc.Type == ELF::R_MIPS_LO16) && HasSameSymbol(*HiPos, *R) &&
217 (CompareOffset(*R, *HiPos) >= 0) &&
218 ((LoPos == RelocLs.end()) || ((CompareOffset(*R, *LoPos) < 0)) ||
219 (!MatchedLo && !CompareOffset(*R, *LoPos))))
222 MatchedLo = NeedsMatchingLo(Asm, *R) &&
223 HasMatchingLo(Asm, R, --RelocLs.end());
226 // If a matching LoPos was found, move HiPos and insert it before LoPos.
227 // Make the offsets of HiPos and LoPos match.
228 if (LoPos != RelocLs.end()) {
229 HiPos->Offset = LoPos->Offset;
230 RelocLs.insert(LoPos, *HiPos);
231 RelocLs.erase(HiPos);
235 // Put the sorted list back in reverse order.
236 assert(Relocs.size() == RelocLs.size());
237 unsigned I = RelocLs.size();
239 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
240 Relocs[--I] = R->Reloc;
243 MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS,
247 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(Is64Bit, OSABI);
248 return createELFObjectWriter(MOTW, OS, IsLittleEndian);