blob: f12f9af7471a837f78c4aa8ed69811ad87da70df [file] [log] [blame]
Jia Liuc5707112012-02-17 08:55:11 +00001//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
Rafael Espindola09044592011-12-22 03:03:17 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Akira Hatanaka37ac18e2012-03-28 00:23:33 +000010#include "MCTargetDesc/MipsBaseInfo.h"
Rafael Espindola09044592011-12-22 03:03:17 +000011#include "MCTargetDesc/MipsFixupKinds.h"
12#include "MCTargetDesc/MipsMCTargetDesc.h"
Akira Hatanaka37ac18e2012-03-28 00:23:33 +000013#include "llvm/MC/MCAssembler.h"
Rafael Espindola09044592011-12-22 03:03:17 +000014#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"
Akira Hatanaka37ac18e2012-03-28 00:23:33 +000019#include <list>
Rafael Espindola09044592011-12-22 03:03:17 +000020
21using namespace llvm;
22
23namespace {
Akira Hatanaka37ac18e2012-03-28 00:23:33 +000024 struct RelEntry {
25 RelEntry(const ELFRelocationEntry &R, const MCSymbol *S, int64_t O) :
26 Reloc(R), Sym(S), Offset(O) {}
27 ELFRelocationEntry Reloc;
28 const MCSymbol *Sym;
29 int64_t Offset;
30 };
31
32 typedef std::list<RelEntry> RelLs;
33 typedef RelLs::iterator RelLsIter;
34
Rafael Espindola09044592011-12-22 03:03:17 +000035 class MipsELFObjectWriter : public MCELFObjectTargetWriter {
36 public:
37 MipsELFObjectWriter(uint8_t OSABI);
38
39 virtual ~MipsELFObjectWriter();
40
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,
47 const MCFragment &F,
48 const MCFixup &Fixup,
49 bool IsPCRel) const;
Akira Hatanaka37ac18e2012-03-28 00:23:33 +000050 virtual void sortRelocs(const MCAssembler &Asm,
51 std::vector<ELFRelocationEntry> &Relocs);
Rafael Espindola09044592011-12-22 03:03:17 +000052 };
53}
54
55MipsELFObjectWriter::MipsELFObjectWriter(uint8_t OSABI)
56 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_MIPS,
57 /*HasRelocationAddend*/ false) {}
58
59MipsELFObjectWriter::~MipsELFObjectWriter() {}
60
61// FIXME: get the real EABI Version from the Triple.
62unsigned MipsELFObjectWriter::getEFlags() const {
63 return ELF::EF_MIPS_NOREORDER | ELF::EF_MIPS_ARCH_32R2;
64}
65
66const MCSymbol *MipsELFObjectWriter::ExplicitRelSym(const MCAssembler &Asm,
67 const MCValue &Target,
68 const MCFragment &F,
69 const MCFixup &Fixup,
70 bool IsPCRel) const {
71 assert(Target.getSymA() && "SymA cannot be 0.");
James Molloy2d8955a2012-01-28 15:58:32 +000072 const MCSymbol &Sym = Target.getSymA()->getSymbol().AliasedSymbol();
Rafael Espindola09044592011-12-22 03:03:17 +000073
74 if (Sym.getSection().getKind().isMergeableCString() ||
75 Sym.getSection().getKind().isMergeableConst())
76 return &Sym;
77
78 return NULL;
79}
80
81unsigned MipsELFObjectWriter::GetRelocType(const MCValue &Target,
82 const MCFixup &Fixup,
83 bool IsPCRel,
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();
89
90 switch (Kind) {
91 default:
92 llvm_unreachable("invalid fixup kind!");
93 case FK_Data_4:
94 Type = ELF::R_MIPS_32;
95 break;
96 case FK_GPRel_4:
97 Type = ELF::R_MIPS_GPREL32;
98 break;
99 case Mips::fixup_Mips_GPREL16:
100 Type = ELF::R_MIPS_GPREL16;
101 break;
102 case Mips::fixup_Mips_26:
103 Type = ELF::R_MIPS_26;
104 break;
105 case Mips::fixup_Mips_CALL16:
106 Type = ELF::R_MIPS_CALL16;
107 break;
108 case Mips::fixup_Mips_GOT_Global:
109 case Mips::fixup_Mips_GOT_Local:
110 Type = ELF::R_MIPS_GOT16;
111 break;
112 case Mips::fixup_Mips_HI16:
113 Type = ELF::R_MIPS_HI16;
114 break;
115 case Mips::fixup_Mips_LO16:
116 Type = ELF::R_MIPS_LO16;
117 break;
118 case Mips::fixup_Mips_TLSGD:
119 Type = ELF::R_MIPS_TLS_GD;
120 break;
121 case Mips::fixup_Mips_GOTTPREL:
122 Type = ELF::R_MIPS_TLS_GOTTPREL;
123 break;
124 case Mips::fixup_Mips_TPREL_HI:
125 Type = ELF::R_MIPS_TLS_TPREL_HI16;
126 break;
127 case Mips::fixup_Mips_TPREL_LO:
128 Type = ELF::R_MIPS_TLS_TPREL_LO16;
129 break;
130 case Mips::fixup_Mips_TLSLDM:
131 Type = ELF::R_MIPS_TLS_LDM;
132 break;
133 case Mips::fixup_Mips_DTPREL_HI:
134 Type = ELF::R_MIPS_TLS_DTPREL_HI16;
135 break;
136 case Mips::fixup_Mips_DTPREL_LO:
137 Type = ELF::R_MIPS_TLS_DTPREL_LO16;
138 break;
139 case Mips::fixup_Mips_Branch_PCRel:
140 case Mips::fixup_Mips_PC16:
141 Type = ELF::R_MIPS_PC16;
142 break;
143 }
144
145 return Type;
146}
147
Akira Hatanaka37ac18e2012-03-28 00:23:33 +0000148// Return true if R is either a GOT16 against a local symbol or HI16.
149static bool NeedsMatchingLo(const MCAssembler &Asm, const RelEntry &R) {
150 if (!R.Sym)
151 return false;
152
153 MCSymbolData &SD = Asm.getSymbolData(R.Sym->AliasedSymbol());
154
155 return ((R.Reloc.Type == ELF::R_MIPS_GOT16) && !SD.isExternal()) ||
156 (R.Reloc.Type == ELF::R_MIPS_HI16);
157}
158
159static bool HasMatchingLo(const MCAssembler &Asm, RelLsIter I, RelLsIter Last) {
160 if (I == Last)
161 return false;
162
163 RelLsIter Hi = I++;
164
165 return (I->Reloc.Type == ELF::R_MIPS_LO16) && (Hi->Sym == I->Sym) &&
166 (Hi->Offset == I->Offset);
167}
168
169static bool HasSameSymbol(const RelEntry &R0, const RelEntry &R1) {
170 return R0.Sym == R1.Sym;
171}
172
173static int CompareOffset(const RelEntry &R0, const RelEntry &R1) {
174 return (R0.Offset > R1.Offset) ? 1 : ((R0.Offset == R1.Offset) ? 0 : -1);
175}
176
177void 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);
182
183 RelLs RelocLs;
184 std::vector<RelLsIter> Unmatched;
185
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,
193 P.second));
194 }
195
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);
200
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;
206
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))))
212 LoPos = R;
213
214 MatchedLo = NeedsMatchingLo(Asm, *R) &&
215 HasMatchingLo(Asm, R, --RelocLs.end());
216 }
217
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);
224 }
225 }
226
227 // Put the sorted list back in reverse order.
228 assert(Relocs.size() == RelocLs.size());
229 unsigned I = RelocLs.size();
230
231 for (RelLsIter R = RelocLs.begin(); R != RelocLs.end(); ++R)
232 Relocs[--I] = R->Reloc;
233}
234
Akira Hatanakae9e520f2012-03-01 01:53:15 +0000235MCObjectWriter *llvm::createMipsELFObjectWriter(raw_ostream &OS, uint8_t OSABI,
236 bool IsLittleEndian) {
Rafael Espindola09044592011-12-22 03:03:17 +0000237 MCELFObjectTargetWriter *MOTW = new MipsELFObjectWriter(OSABI);
238 return createELFObjectWriter(MOTW, OS, IsLittleEndian);
239}