1*9880d681SAndroid Build Coastguard Worker //===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker
10*9880d681SAndroid Build Coastguard Worker #include <algorithm>
11*9880d681SAndroid Build Coastguard Worker #include <list>
12*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/MipsBaseInfo.h"
13*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/MipsFixupKinds.h"
14*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/MipsMCExpr.h"
15*9880d681SAndroid Build Coastguard Worker #include "MCTargetDesc/MipsMCTargetDesc.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCAssembler.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCELFObjectWriter.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCExpr.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSection.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCSymbolELF.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/MC/MCValue.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "mips-elf-object-writer"
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker using namespace llvm;
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker namespace {
31*9880d681SAndroid Build Coastguard Worker /// Holds additional information needed by the relocation ordering algorithm.
32*9880d681SAndroid Build Coastguard Worker struct MipsRelocationEntry {
33*9880d681SAndroid Build Coastguard Worker const ELFRelocationEntry R; ///< The relocation.
34*9880d681SAndroid Build Coastguard Worker bool Matched; ///< Is this relocation part of a match.
35*9880d681SAndroid Build Coastguard Worker
MipsRelocationEntry__anon1cb4236f0111::MipsRelocationEntry36*9880d681SAndroid Build Coastguard Worker MipsRelocationEntry(const ELFRelocationEntry &R) : R(R), Matched(false) {}
37*9880d681SAndroid Build Coastguard Worker
print__anon1cb4236f0111::MipsRelocationEntry38*9880d681SAndroid Build Coastguard Worker void print(raw_ostream &Out) const {
39*9880d681SAndroid Build Coastguard Worker R.print(Out);
40*9880d681SAndroid Build Coastguard Worker Out << ", Matched=" << Matched;
41*9880d681SAndroid Build Coastguard Worker }
42*9880d681SAndroid Build Coastguard Worker };
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
operator <<(raw_ostream & OS,const MipsRelocationEntry & RHS)45*9880d681SAndroid Build Coastguard Worker raw_ostream &operator<<(raw_ostream &OS, const MipsRelocationEntry &RHS) {
46*9880d681SAndroid Build Coastguard Worker RHS.print(OS);
47*9880d681SAndroid Build Coastguard Worker return OS;
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker #endif
50*9880d681SAndroid Build Coastguard Worker
51*9880d681SAndroid Build Coastguard Worker class MipsELFObjectWriter : public MCELFObjectTargetWriter {
52*9880d681SAndroid Build Coastguard Worker public:
53*9880d681SAndroid Build Coastguard Worker MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, bool _isN64,
54*9880d681SAndroid Build Coastguard Worker bool IsLittleEndian);
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker ~MipsELFObjectWriter() override;
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
59*9880d681SAndroid Build Coastguard Worker const MCFixup &Fixup, bool IsPCRel) const override;
60*9880d681SAndroid Build Coastguard Worker bool needsRelocateWithSymbol(const MCSymbol &Sym,
61*9880d681SAndroid Build Coastguard Worker unsigned Type) const override;
62*9880d681SAndroid Build Coastguard Worker virtual void sortRelocs(const MCAssembler &Asm,
63*9880d681SAndroid Build Coastguard Worker std::vector<ELFRelocationEntry> &Relocs) override;
64*9880d681SAndroid Build Coastguard Worker };
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker /// Copy elements in the range [First, Last) to d1 when the predicate is true or
67*9880d681SAndroid Build Coastguard Worker /// d2 when the predicate is false. This is essentially both std::copy_if and
68*9880d681SAndroid Build Coastguard Worker /// std::remove_copy_if combined into a single pass.
69*9880d681SAndroid Build Coastguard Worker template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>
copy_if_else(InputIt First,InputIt Last,OutputIt1 d1,OutputIt2 d2,UnaryPredicate Predicate)70*9880d681SAndroid Build Coastguard Worker std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,
71*9880d681SAndroid Build Coastguard Worker OutputIt1 d1, OutputIt2 d2,
72*9880d681SAndroid Build Coastguard Worker UnaryPredicate Predicate) {
73*9880d681SAndroid Build Coastguard Worker for (InputIt I = First; I != Last; ++I) {
74*9880d681SAndroid Build Coastguard Worker if (Predicate(*I)) {
75*9880d681SAndroid Build Coastguard Worker *d1 = *I;
76*9880d681SAndroid Build Coastguard Worker d1++;
77*9880d681SAndroid Build Coastguard Worker } else {
78*9880d681SAndroid Build Coastguard Worker *d2 = *I;
79*9880d681SAndroid Build Coastguard Worker d2++;
80*9880d681SAndroid Build Coastguard Worker }
81*9880d681SAndroid Build Coastguard Worker }
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker return std::make_pair(d1, d2);
84*9880d681SAndroid Build Coastguard Worker }
85*9880d681SAndroid Build Coastguard Worker
86*9880d681SAndroid Build Coastguard Worker /// The possible results of the Predicate function used by find_best.
87*9880d681SAndroid Build Coastguard Worker enum FindBestPredicateResult {
88*9880d681SAndroid Build Coastguard Worker FindBest_NoMatch = 0, ///< The current element is not a match.
89*9880d681SAndroid Build Coastguard Worker FindBest_Match, ///< The current element is a match but better ones are
90*9880d681SAndroid Build Coastguard Worker /// possible.
91*9880d681SAndroid Build Coastguard Worker FindBest_PerfectMatch, ///< The current element is an unbeatable match.
92*9880d681SAndroid Build Coastguard Worker };
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Worker /// Find the best match in the range [First, Last).
95*9880d681SAndroid Build Coastguard Worker ///
96*9880d681SAndroid Build Coastguard Worker /// An element matches when Predicate(X) returns FindBest_Match or
97*9880d681SAndroid Build Coastguard Worker /// FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates
98*9880d681SAndroid Build Coastguard Worker /// the search. BetterThan(A, B) is a comparator that returns true when A is a
99*9880d681SAndroid Build Coastguard Worker /// better match than B. The return value is the position of the best match.
100*9880d681SAndroid Build Coastguard Worker ///
101*9880d681SAndroid Build Coastguard Worker /// This is similar to std::find_if but finds the best of multiple possible
102*9880d681SAndroid Build Coastguard Worker /// matches.
103*9880d681SAndroid Build Coastguard Worker template <class InputIt, class UnaryPredicate, class Comparator>
find_best(InputIt First,InputIt Last,UnaryPredicate Predicate,Comparator BetterThan)104*9880d681SAndroid Build Coastguard Worker InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,
105*9880d681SAndroid Build Coastguard Worker Comparator BetterThan) {
106*9880d681SAndroid Build Coastguard Worker InputIt Best = Last;
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker for (InputIt I = First; I != Last; ++I) {
109*9880d681SAndroid Build Coastguard Worker unsigned Matched = Predicate(*I);
110*9880d681SAndroid Build Coastguard Worker if (Matched != FindBest_NoMatch) {
111*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << std::distance(First, I) << " is a match (";
112*9880d681SAndroid Build Coastguard Worker I->print(dbgs()); dbgs() << ")\n");
113*9880d681SAndroid Build Coastguard Worker if (Best == Last || BetterThan(*I, *Best)) {
114*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << ".. and it beats the last one\n");
115*9880d681SAndroid Build Coastguard Worker Best = I;
116*9880d681SAndroid Build Coastguard Worker }
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker if (Matched == FindBest_PerfectMatch) {
119*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << ".. and it is unbeatable\n");
120*9880d681SAndroid Build Coastguard Worker break;
121*9880d681SAndroid Build Coastguard Worker }
122*9880d681SAndroid Build Coastguard Worker }
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker return Best;
125*9880d681SAndroid Build Coastguard Worker }
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker /// Determine the low relocation that matches the given relocation.
128*9880d681SAndroid Build Coastguard Worker /// If the relocation does not need a low relocation then the return value
129*9880d681SAndroid Build Coastguard Worker /// is ELF::R_MIPS_NONE.
130*9880d681SAndroid Build Coastguard Worker ///
131*9880d681SAndroid Build Coastguard Worker /// The relocations that need a matching low part are
132*9880d681SAndroid Build Coastguard Worker /// R_(MIPS|MICROMIPS|MIPS16)_HI16 for all symbols and
133*9880d681SAndroid Build Coastguard Worker /// R_(MIPS|MICROMIPS|MIPS16)_GOT16 for local symbols only.
getMatchingLoType(const ELFRelocationEntry & Reloc)134*9880d681SAndroid Build Coastguard Worker static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc) {
135*9880d681SAndroid Build Coastguard Worker unsigned Type = Reloc.Type;
136*9880d681SAndroid Build Coastguard Worker if (Type == ELF::R_MIPS_HI16)
137*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_LO16;
138*9880d681SAndroid Build Coastguard Worker if (Type == ELF::R_MICROMIPS_HI16)
139*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_LO16;
140*9880d681SAndroid Build Coastguard Worker if (Type == ELF::R_MIPS16_HI16)
141*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS16_LO16;
142*9880d681SAndroid Build Coastguard Worker
143*9880d681SAndroid Build Coastguard Worker if (Reloc.OriginalSymbol->getBinding() != ELF::STB_LOCAL)
144*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_NONE;
145*9880d681SAndroid Build Coastguard Worker
146*9880d681SAndroid Build Coastguard Worker if (Type == ELF::R_MIPS_GOT16)
147*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_LO16;
148*9880d681SAndroid Build Coastguard Worker if (Type == ELF::R_MICROMIPS_GOT16)
149*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_LO16;
150*9880d681SAndroid Build Coastguard Worker if (Type == ELF::R_MIPS16_GOT16)
151*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS16_LO16;
152*9880d681SAndroid Build Coastguard Worker
153*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_NONE;
154*9880d681SAndroid Build Coastguard Worker }
155*9880d681SAndroid Build Coastguard Worker
156*9880d681SAndroid Build Coastguard Worker /// Determine whether a relocation (X) matches the one given in R.
157*9880d681SAndroid Build Coastguard Worker ///
158*9880d681SAndroid Build Coastguard Worker /// A relocation matches if:
159*9880d681SAndroid Build Coastguard Worker /// - It's type matches that of a corresponding low part. This is provided in
160*9880d681SAndroid Build Coastguard Worker /// MatchingType for efficiency.
161*9880d681SAndroid Build Coastguard Worker /// - It's based on the same symbol.
162*9880d681SAndroid Build Coastguard Worker /// - It's offset of greater or equal to that of the one given in R.
163*9880d681SAndroid Build Coastguard Worker /// It should be noted that this rule assumes the programmer does not use
164*9880d681SAndroid Build Coastguard Worker /// offsets that exceed the alignment of the symbol. The carry-bit will be
165*9880d681SAndroid Build Coastguard Worker /// incorrect if this is not true.
166*9880d681SAndroid Build Coastguard Worker ///
167*9880d681SAndroid Build Coastguard Worker /// A matching relocation is unbeatable if:
168*9880d681SAndroid Build Coastguard Worker /// - It is not already involved in a match.
169*9880d681SAndroid Build Coastguard Worker /// - It's offset is exactly that of the one given in R.
isMatchingReloc(const MipsRelocationEntry & X,const ELFRelocationEntry & R,unsigned MatchingType)170*9880d681SAndroid Build Coastguard Worker static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X,
171*9880d681SAndroid Build Coastguard Worker const ELFRelocationEntry &R,
172*9880d681SAndroid Build Coastguard Worker unsigned MatchingType) {
173*9880d681SAndroid Build Coastguard Worker if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.OriginalSymbol) {
174*9880d681SAndroid Build Coastguard Worker if (!X.Matched &&
175*9880d681SAndroid Build Coastguard Worker X.R.OriginalAddend == R.OriginalAddend)
176*9880d681SAndroid Build Coastguard Worker return FindBest_PerfectMatch;
177*9880d681SAndroid Build Coastguard Worker else if (X.R.OriginalAddend >= R.OriginalAddend)
178*9880d681SAndroid Build Coastguard Worker return FindBest_Match;
179*9880d681SAndroid Build Coastguard Worker }
180*9880d681SAndroid Build Coastguard Worker return FindBest_NoMatch;
181*9880d681SAndroid Build Coastguard Worker }
182*9880d681SAndroid Build Coastguard Worker
183*9880d681SAndroid Build Coastguard Worker /// Determine whether Candidate or PreviousBest is the better match.
184*9880d681SAndroid Build Coastguard Worker /// The return value is true if Candidate is the better match.
185*9880d681SAndroid Build Coastguard Worker ///
186*9880d681SAndroid Build Coastguard Worker /// A matching relocation is a better match if:
187*9880d681SAndroid Build Coastguard Worker /// - It has a smaller addend.
188*9880d681SAndroid Build Coastguard Worker /// - It is not already involved in a match.
compareMatchingRelocs(const MipsRelocationEntry & Candidate,const MipsRelocationEntry & PreviousBest)189*9880d681SAndroid Build Coastguard Worker static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate,
190*9880d681SAndroid Build Coastguard Worker const MipsRelocationEntry &PreviousBest) {
191*9880d681SAndroid Build Coastguard Worker if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
192*9880d681SAndroid Build Coastguard Worker return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
193*9880d681SAndroid Build Coastguard Worker return PreviousBest.Matched && !Candidate.Matched;
194*9880d681SAndroid Build Coastguard Worker }
195*9880d681SAndroid Build Coastguard Worker
196*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG
197*9880d681SAndroid Build Coastguard Worker /// Print all the relocations.
198*9880d681SAndroid Build Coastguard Worker template <class Container>
dumpRelocs(const char * Prefix,const Container & Relocs)199*9880d681SAndroid Build Coastguard Worker static void dumpRelocs(const char *Prefix, const Container &Relocs) {
200*9880d681SAndroid Build Coastguard Worker for (const auto &R : Relocs)
201*9880d681SAndroid Build Coastguard Worker dbgs() << Prefix << R << "\n";
202*9880d681SAndroid Build Coastguard Worker }
203*9880d681SAndroid Build Coastguard Worker #endif
204*9880d681SAndroid Build Coastguard Worker
205*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
206*9880d681SAndroid Build Coastguard Worker
MipsELFObjectWriter(bool _is64Bit,uint8_t OSABI,bool _isN64,bool IsLittleEndian)207*9880d681SAndroid Build Coastguard Worker MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
208*9880d681SAndroid Build Coastguard Worker bool _isN64, bool IsLittleEndian)
209*9880d681SAndroid Build Coastguard Worker : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
210*9880d681SAndroid Build Coastguard Worker /*HasRelocationAddend*/ _isN64,
211*9880d681SAndroid Build Coastguard Worker /*IsN64*/ _isN64) {}
212*9880d681SAndroid Build Coastguard Worker
~MipsELFObjectWriter()213*9880d681SAndroid Build Coastguard Worker MipsELFObjectWriter::~MipsELFObjectWriter() {}
214*9880d681SAndroid Build Coastguard Worker
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const215*9880d681SAndroid Build Coastguard Worker unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
216*9880d681SAndroid Build Coastguard Worker const MCValue &Target,
217*9880d681SAndroid Build Coastguard Worker const MCFixup &Fixup,
218*9880d681SAndroid Build Coastguard Worker bool IsPCRel) const {
219*9880d681SAndroid Build Coastguard Worker // Determine the type of the relocation.
220*9880d681SAndroid Build Coastguard Worker unsigned Kind = (unsigned)Fixup.getKind();
221*9880d681SAndroid Build Coastguard Worker
222*9880d681SAndroid Build Coastguard Worker switch (Kind) {
223*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_NONE:
224*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_NONE;
225*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_16:
226*9880d681SAndroid Build Coastguard Worker case FK_Data_2:
227*9880d681SAndroid Build Coastguard Worker return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
228*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_32:
229*9880d681SAndroid Build Coastguard Worker case FK_Data_4:
230*9880d681SAndroid Build Coastguard Worker return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
231*9880d681SAndroid Build Coastguard Worker }
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker if (IsPCRel) {
234*9880d681SAndroid Build Coastguard Worker switch (Kind) {
235*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_Branch_PCRel:
236*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_PC16:
237*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_PC16;
238*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_PC7_S1:
239*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_PC7_S1;
240*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_PC10_S1:
241*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_PC10_S1;
242*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_PC16_S1:
243*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_PC16_S1;
244*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_PC26_S1:
245*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_PC26_S1;
246*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_PC19_S2:
247*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_PC19_S2;
248*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_PC18_S3:
249*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_PC18_S3;
250*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_PC21_S1:
251*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_PC21_S1;
252*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MIPS_PC19_S2:
253*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_PC19_S2;
254*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MIPS_PC18_S3:
255*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_PC18_S3;
256*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MIPS_PC21_S2:
257*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_PC21_S2;
258*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MIPS_PC26_S2:
259*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_PC26_S2;
260*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MIPS_PCHI16:
261*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_PCHI16;
262*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MIPS_PCLO16:
263*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_PCLO16;
264*9880d681SAndroid Build Coastguard Worker }
265*9880d681SAndroid Build Coastguard Worker
266*9880d681SAndroid Build Coastguard Worker llvm_unreachable("invalid PC-relative fixup kind!");
267*9880d681SAndroid Build Coastguard Worker }
268*9880d681SAndroid Build Coastguard Worker
269*9880d681SAndroid Build Coastguard Worker switch (Kind) {
270*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_64:
271*9880d681SAndroid Build Coastguard Worker case FK_Data_8:
272*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_64;
273*9880d681SAndroid Build Coastguard Worker case FK_GPRel_4:
274*9880d681SAndroid Build Coastguard Worker if (isN64()) {
275*9880d681SAndroid Build Coastguard Worker unsigned Type = (unsigned)ELF::R_MIPS_NONE;
276*9880d681SAndroid Build Coastguard Worker Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
277*9880d681SAndroid Build Coastguard Worker Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
278*9880d681SAndroid Build Coastguard Worker Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
279*9880d681SAndroid Build Coastguard Worker return Type;
280*9880d681SAndroid Build Coastguard Worker }
281*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_GPREL32;
282*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GPREL16:
283*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_GPREL16;
284*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_26:
285*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_26;
286*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_CALL16:
287*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_CALL16;
288*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GOT:
289*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_GOT16;
290*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_HI16:
291*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_HI16;
292*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_LO16:
293*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_LO16;
294*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_TLSGD:
295*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_TLS_GD;
296*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GOTTPREL:
297*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_TLS_GOTTPREL;
298*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_TPREL_HI:
299*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_TLS_TPREL_HI16;
300*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_TPREL_LO:
301*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_TLS_TPREL_LO16;
302*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_TLSLDM:
303*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_TLS_LDM;
304*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_DTPREL_HI:
305*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_TLS_DTPREL_HI16;
306*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_DTPREL_LO:
307*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_TLS_DTPREL_LO16;
308*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GOT_PAGE:
309*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_GOT_PAGE;
310*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GOT_OFST:
311*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_GOT_OFST;
312*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GOT_DISP:
313*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_GOT_DISP;
314*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GPOFF_HI: {
315*9880d681SAndroid Build Coastguard Worker unsigned Type = (unsigned)ELF::R_MIPS_NONE;
316*9880d681SAndroid Build Coastguard Worker Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
317*9880d681SAndroid Build Coastguard Worker Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
318*9880d681SAndroid Build Coastguard Worker Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
319*9880d681SAndroid Build Coastguard Worker return Type;
320*9880d681SAndroid Build Coastguard Worker }
321*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GPOFF_LO: {
322*9880d681SAndroid Build Coastguard Worker unsigned Type = (unsigned)ELF::R_MIPS_NONE;
323*9880d681SAndroid Build Coastguard Worker Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
324*9880d681SAndroid Build Coastguard Worker Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
325*9880d681SAndroid Build Coastguard Worker Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
326*9880d681SAndroid Build Coastguard Worker return Type;
327*9880d681SAndroid Build Coastguard Worker }
328*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_HIGHER:
329*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_HIGHER;
330*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_HIGHEST:
331*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_HIGHEST;
332*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GOT_HI16:
333*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_GOT_HI16;
334*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_GOT_LO16:
335*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_GOT_LO16;
336*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_CALL_HI16:
337*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_CALL_HI16;
338*9880d681SAndroid Build Coastguard Worker case Mips::fixup_Mips_CALL_LO16:
339*9880d681SAndroid Build Coastguard Worker return ELF::R_MIPS_CALL_LO16;
340*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_26_S1:
341*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_26_S1;
342*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_HI16:
343*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_HI16;
344*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_LO16:
345*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_LO16;
346*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_GOT16:
347*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_GOT16;
348*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_CALL16:
349*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_CALL16;
350*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_GOT_DISP:
351*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_GOT_DISP;
352*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_GOT_PAGE:
353*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_GOT_PAGE;
354*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_GOT_OFST:
355*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_GOT_OFST;
356*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_TLS_GD:
357*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_TLS_GD;
358*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_TLS_LDM:
359*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_TLS_LDM;
360*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
361*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
362*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
363*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
364*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
365*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_TLS_TPREL_HI16;
366*9880d681SAndroid Build Coastguard Worker case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
367*9880d681SAndroid Build Coastguard Worker return ELF::R_MICROMIPS_TLS_TPREL_LO16;
368*9880d681SAndroid Build Coastguard Worker }
369*9880d681SAndroid Build Coastguard Worker
370*9880d681SAndroid Build Coastguard Worker llvm_unreachable("invalid fixup kind!");
371*9880d681SAndroid Build Coastguard Worker }
372*9880d681SAndroid Build Coastguard Worker
373*9880d681SAndroid Build Coastguard Worker /// Sort relocation table entries by offset except where another order is
374*9880d681SAndroid Build Coastguard Worker /// required by the MIPS ABI.
375*9880d681SAndroid Build Coastguard Worker ///
376*9880d681SAndroid Build Coastguard Worker /// MIPS has a few relocations that have an AHL component in the expression used
377*9880d681SAndroid Build Coastguard Worker /// to evaluate them. This AHL component is an addend with the same number of
378*9880d681SAndroid Build Coastguard Worker /// bits as a symbol value but not all of our ABI's are able to supply a
379*9880d681SAndroid Build Coastguard Worker /// sufficiently sized addend in a single relocation.
380*9880d681SAndroid Build Coastguard Worker ///
381*9880d681SAndroid Build Coastguard Worker /// The O32 ABI for example, uses REL relocations which store the addend in the
382*9880d681SAndroid Build Coastguard Worker /// section data. All the relocations with AHL components affect 16-bit fields
383*9880d681SAndroid Build Coastguard Worker /// so the addend for a single relocation is limited to 16-bit. This ABI
384*9880d681SAndroid Build Coastguard Worker /// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and
385*9880d681SAndroid Build Coastguard Worker /// R_MIPS_LO16) and distributing the addend between the linked relocations. The
386*9880d681SAndroid Build Coastguard Worker /// ABI mandates that such relocations must be next to each other in a
387*9880d681SAndroid Build Coastguard Worker /// particular order (e.g. R_MIPS_HI16 must be immediately followed by a
388*9880d681SAndroid Build Coastguard Worker /// matching R_MIPS_LO16) but the rule is less strict in practice.
389*9880d681SAndroid Build Coastguard Worker ///
390*9880d681SAndroid Build Coastguard Worker /// The de facto standard is lenient in the following ways:
391*9880d681SAndroid Build Coastguard Worker /// - 'Immediately following' does not refer to the next relocation entry but
392*9880d681SAndroid Build Coastguard Worker /// the next matching relocation.
393*9880d681SAndroid Build Coastguard Worker /// - There may be multiple high parts relocations for one low part relocation.
394*9880d681SAndroid Build Coastguard Worker /// - There may be multiple low part relocations for one high part relocation.
395*9880d681SAndroid Build Coastguard Worker /// - The AHL addend in each part does not have to be exactly equal as long as
396*9880d681SAndroid Build Coastguard Worker /// the difference does not affect the carry bit from bit 15 into 16. This is
397*9880d681SAndroid Build Coastguard Worker /// to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading
398*9880d681SAndroid Build Coastguard Worker /// both halves of a long long.
399*9880d681SAndroid Build Coastguard Worker ///
400*9880d681SAndroid Build Coastguard Worker /// See getMatchingLoType() for a description of which high part relocations
401*9880d681SAndroid Build Coastguard Worker /// match which low part relocations. One particular thing to note is that
402*9880d681SAndroid Build Coastguard Worker /// R_MIPS_GOT16 and similar only have AHL addends if they refer to local
403*9880d681SAndroid Build Coastguard Worker /// symbols.
404*9880d681SAndroid Build Coastguard Worker ///
405*9880d681SAndroid Build Coastguard Worker /// It should also be noted that this function is not affected by whether
406*9880d681SAndroid Build Coastguard Worker /// the symbol was kept or rewritten into a section-relative equivalent. We
407*9880d681SAndroid Build Coastguard Worker /// always match using the expressions from the source.
sortRelocs(const MCAssembler & Asm,std::vector<ELFRelocationEntry> & Relocs)408*9880d681SAndroid Build Coastguard Worker void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
409*9880d681SAndroid Build Coastguard Worker std::vector<ELFRelocationEntry> &Relocs) {
410*9880d681SAndroid Build Coastguard Worker if (Relocs.size() < 2)
411*9880d681SAndroid Build Coastguard Worker return;
412*9880d681SAndroid Build Coastguard Worker
413*9880d681SAndroid Build Coastguard Worker // Sort relocations by the address they are applied to.
414*9880d681SAndroid Build Coastguard Worker std::sort(Relocs.begin(), Relocs.end(),
415*9880d681SAndroid Build Coastguard Worker [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
416*9880d681SAndroid Build Coastguard Worker return A.Offset < B.Offset;
417*9880d681SAndroid Build Coastguard Worker });
418*9880d681SAndroid Build Coastguard Worker
419*9880d681SAndroid Build Coastguard Worker std::list<MipsRelocationEntry> Sorted;
420*9880d681SAndroid Build Coastguard Worker std::list<ELFRelocationEntry> Remainder;
421*9880d681SAndroid Build Coastguard Worker
422*9880d681SAndroid Build Coastguard Worker DEBUG(dumpRelocs("R: ", Relocs));
423*9880d681SAndroid Build Coastguard Worker
424*9880d681SAndroid Build Coastguard Worker // Separate the movable relocations (AHL relocations using the high bits) from
425*9880d681SAndroid Build Coastguard Worker // the immobile relocations (everything else). This does not preserve high/low
426*9880d681SAndroid Build Coastguard Worker // matches that already existed in the input.
427*9880d681SAndroid Build Coastguard Worker copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
428*9880d681SAndroid Build Coastguard Worker std::back_inserter(Sorted), [](const ELFRelocationEntry &Reloc) {
429*9880d681SAndroid Build Coastguard Worker return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
430*9880d681SAndroid Build Coastguard Worker });
431*9880d681SAndroid Build Coastguard Worker
432*9880d681SAndroid Build Coastguard Worker for (auto &R : Remainder) {
433*9880d681SAndroid Build Coastguard Worker DEBUG(dbgs() << "Matching: " << R << "\n");
434*9880d681SAndroid Build Coastguard Worker
435*9880d681SAndroid Build Coastguard Worker unsigned MatchingType = getMatchingLoType(R);
436*9880d681SAndroid Build Coastguard Worker assert(MatchingType != ELF::R_MIPS_NONE &&
437*9880d681SAndroid Build Coastguard Worker "Wrong list for reloc that doesn't need a match");
438*9880d681SAndroid Build Coastguard Worker
439*9880d681SAndroid Build Coastguard Worker // Find the best matching relocation for the current high part.
440*9880d681SAndroid Build Coastguard Worker // See isMatchingReloc for a description of a matching relocation and
441*9880d681SAndroid Build Coastguard Worker // compareMatchingRelocs for a description of what 'best' means.
442*9880d681SAndroid Build Coastguard Worker auto InsertionPoint =
443*9880d681SAndroid Build Coastguard Worker find_best(Sorted.begin(), Sorted.end(),
444*9880d681SAndroid Build Coastguard Worker [&R, &MatchingType](const MipsRelocationEntry &X) {
445*9880d681SAndroid Build Coastguard Worker return isMatchingReloc(X, R, MatchingType);
446*9880d681SAndroid Build Coastguard Worker },
447*9880d681SAndroid Build Coastguard Worker compareMatchingRelocs);
448*9880d681SAndroid Build Coastguard Worker
449*9880d681SAndroid Build Coastguard Worker // If we matched then insert the high part in front of the match and mark
450*9880d681SAndroid Build Coastguard Worker // both relocations as being involved in a match. We only mark the high
451*9880d681SAndroid Build Coastguard Worker // part for cosmetic reasons in the debug output.
452*9880d681SAndroid Build Coastguard Worker //
453*9880d681SAndroid Build Coastguard Worker // If we failed to find a match then the high part is orphaned. This is not
454*9880d681SAndroid Build Coastguard Worker // permitted since the relocation cannot be evaluated without knowing the
455*9880d681SAndroid Build Coastguard Worker // carry-in. We can sometimes handle this using a matching low part that is
456*9880d681SAndroid Build Coastguard Worker // already used in a match but we already cover that case in
457*9880d681SAndroid Build Coastguard Worker // isMatchingReloc and compareMatchingRelocs. For the remaining cases we
458*9880d681SAndroid Build Coastguard Worker // should insert the high part at the end of the list. This will cause the
459*9880d681SAndroid Build Coastguard Worker // linker to fail but the alternative is to cause the linker to bind the
460*9880d681SAndroid Build Coastguard Worker // high part to a semi-matching low part and silently calculate the wrong
461*9880d681SAndroid Build Coastguard Worker // value. Unfortunately we have no means to warn the user that we did this
462*9880d681SAndroid Build Coastguard Worker // so leave it up to the linker to complain about it.
463*9880d681SAndroid Build Coastguard Worker if (InsertionPoint != Sorted.end())
464*9880d681SAndroid Build Coastguard Worker InsertionPoint->Matched = true;
465*9880d681SAndroid Build Coastguard Worker Sorted.insert(InsertionPoint, R)->Matched = true;
466*9880d681SAndroid Build Coastguard Worker }
467*9880d681SAndroid Build Coastguard Worker
468*9880d681SAndroid Build Coastguard Worker DEBUG(dumpRelocs("S: ", Sorted));
469*9880d681SAndroid Build Coastguard Worker
470*9880d681SAndroid Build Coastguard Worker assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed");
471*9880d681SAndroid Build Coastguard Worker
472*9880d681SAndroid Build Coastguard Worker // Overwrite the original vector with the sorted elements. The caller expects
473*9880d681SAndroid Build Coastguard Worker // them in reverse order.
474*9880d681SAndroid Build Coastguard Worker unsigned CopyTo = 0;
475*9880d681SAndroid Build Coastguard Worker for (const auto &R : reverse(Sorted))
476*9880d681SAndroid Build Coastguard Worker Relocs[CopyTo++] = R.R;
477*9880d681SAndroid Build Coastguard Worker }
478*9880d681SAndroid Build Coastguard Worker
needsRelocateWithSymbol(const MCSymbol & Sym,unsigned Type) const479*9880d681SAndroid Build Coastguard Worker bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
480*9880d681SAndroid Build Coastguard Worker unsigned Type) const {
481*9880d681SAndroid Build Coastguard Worker // If it's a compound relocation for N64 then we need the relocation if any
482*9880d681SAndroid Build Coastguard Worker // sub-relocation needs it.
483*9880d681SAndroid Build Coastguard Worker if (!isUInt<8>(Type))
484*9880d681SAndroid Build Coastguard Worker return needsRelocateWithSymbol(Sym, Type & 0xff) ||
485*9880d681SAndroid Build Coastguard Worker needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
486*9880d681SAndroid Build Coastguard Worker needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
487*9880d681SAndroid Build Coastguard Worker
488*9880d681SAndroid Build Coastguard Worker switch (Type) {
489*9880d681SAndroid Build Coastguard Worker default:
490*9880d681SAndroid Build Coastguard Worker errs() << Type << "\n";
491*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unexpected relocation");
492*9880d681SAndroid Build Coastguard Worker return true;
493*9880d681SAndroid Build Coastguard Worker
494*9880d681SAndroid Build Coastguard Worker // This relocation doesn't affect the section data.
495*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_NONE:
496*9880d681SAndroid Build Coastguard Worker return false;
497*9880d681SAndroid Build Coastguard Worker
498*9880d681SAndroid Build Coastguard Worker // On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done
499*9880d681SAndroid Build Coastguard Worker // by the static linker by matching the symbol and offset.
500*9880d681SAndroid Build Coastguard Worker // We only see one relocation at a time but it's still safe to relocate with
501*9880d681SAndroid Build Coastguard Worker // the section so long as both relocations make the same decision.
502*9880d681SAndroid Build Coastguard Worker //
503*9880d681SAndroid Build Coastguard Worker // Some older linkers may require the symbol for particular cases. Such cases
504*9880d681SAndroid Build Coastguard Worker // are not supported yet but can be added as required.
505*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GOT16:
506*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_GOT16:
507*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_GOT16:
508*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_HI16:
509*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_HI16:
510*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_HI16:
511*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_LO16:
512*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_LO16:
513*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_LO16:
514*9880d681SAndroid Build Coastguard Worker // FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
515*9880d681SAndroid Build Coastguard Worker // we neglect to handle the adjustment to the LSB of the addend that
516*9880d681SAndroid Build Coastguard Worker // it causes in applyFixup() and similar.
517*9880d681SAndroid Build Coastguard Worker if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
518*9880d681SAndroid Build Coastguard Worker return true;
519*9880d681SAndroid Build Coastguard Worker return false;
520*9880d681SAndroid Build Coastguard Worker
521*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_16:
522*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_32:
523*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GPREL32:
524*9880d681SAndroid Build Coastguard Worker if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
525*9880d681SAndroid Build Coastguard Worker return true;
526*9880d681SAndroid Build Coastguard Worker // fallthrough
527*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_26:
528*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_64:
529*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GPREL16:
530*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PC16:
531*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_SUB:
532*9880d681SAndroid Build Coastguard Worker return false;
533*9880d681SAndroid Build Coastguard Worker
534*9880d681SAndroid Build Coastguard Worker // FIXME: Many of these relocations should probably return false but this
535*9880d681SAndroid Build Coastguard Worker // hasn't been confirmed to be safe yet.
536*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_REL32:
537*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_LITERAL:
538*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_CALL16:
539*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_SHIFT5:
540*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_SHIFT6:
541*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GOT_DISP:
542*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GOT_PAGE:
543*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GOT_OFST:
544*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GOT_HI16:
545*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GOT_LO16:
546*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_INSERT_A:
547*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_INSERT_B:
548*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_DELETE:
549*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_HIGHER:
550*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_HIGHEST:
551*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_CALL_HI16:
552*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_CALL_LO16:
553*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_SCN_DISP:
554*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_REL16:
555*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_ADD_IMMEDIATE:
556*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PJUMP:
557*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_RELGOT:
558*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_JALR:
559*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_DTPMOD32:
560*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_DTPREL32:
561*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_DTPMOD64:
562*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_DTPREL64:
563*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_GD:
564*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_LDM:
565*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_DTPREL_HI16:
566*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_DTPREL_LO16:
567*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_GOTTPREL:
568*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_TPREL32:
569*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_TPREL64:
570*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_TPREL_HI16:
571*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_TLS_TPREL_LO16:
572*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_GLOB_DAT:
573*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PC21_S2:
574*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PC26_S2:
575*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PC18_S3:
576*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PC19_S2:
577*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PCHI16:
578*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PCLO16:
579*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_COPY:
580*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_JUMP_SLOT:
581*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_NUM:
582*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_PC32:
583*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS_EH:
584*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_26_S1:
585*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_GPREL16:
586*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_LITERAL:
587*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_PC7_S1:
588*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_PC10_S1:
589*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_PC16_S1:
590*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_CALL16:
591*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_GOT_DISP:
592*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_GOT_PAGE:
593*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_GOT_OFST:
594*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_GOT_HI16:
595*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_GOT_LO16:
596*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_SUB:
597*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_HIGHER:
598*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_HIGHEST:
599*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_CALL_HI16:
600*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_CALL_LO16:
601*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_SCN_DISP:
602*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_JALR:
603*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_HI0_LO16:
604*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_TLS_GD:
605*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_TLS_LDM:
606*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
607*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
608*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_TLS_GOTTPREL:
609*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_TLS_TPREL_HI16:
610*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_TLS_TPREL_LO16:
611*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_GPREL7_S2:
612*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_PC23_S2:
613*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_PC21_S1:
614*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_PC26_S1:
615*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_PC18_S3:
616*9880d681SAndroid Build Coastguard Worker case ELF::R_MICROMIPS_PC19_S2:
617*9880d681SAndroid Build Coastguard Worker return true;
618*9880d681SAndroid Build Coastguard Worker
619*9880d681SAndroid Build Coastguard Worker // FIXME: Many of these should probably return false but MIPS16 isn't
620*9880d681SAndroid Build Coastguard Worker // supported by the integrated assembler.
621*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_26:
622*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_GPREL:
623*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_CALL16:
624*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_TLS_GD:
625*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_TLS_LDM:
626*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_TLS_DTPREL_HI16:
627*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_TLS_DTPREL_LO16:
628*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_TLS_GOTTPREL:
629*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_TLS_TPREL_HI16:
630*9880d681SAndroid Build Coastguard Worker case ELF::R_MIPS16_TLS_TPREL_LO16:
631*9880d681SAndroid Build Coastguard Worker llvm_unreachable("Unsupported MIPS16 relocation");
632*9880d681SAndroid Build Coastguard Worker return true;
633*9880d681SAndroid Build Coastguard Worker }
634*9880d681SAndroid Build Coastguard Worker }
635*9880d681SAndroid Build Coastguard Worker
createMipsELFObjectWriter(raw_pwrite_stream & OS,uint8_t OSABI,bool IsLittleEndian,bool Is64Bit)636*9880d681SAndroid Build Coastguard Worker MCObjectWriter *llvm::createMipsELFObjectWriter(raw_pwrite_stream &OS,
637*9880d681SAndroid Build Coastguard Worker uint8_t OSABI,
638*9880d681SAndroid Build Coastguard Worker bool IsLittleEndian,
639*9880d681SAndroid Build Coastguard Worker bool Is64Bit) {
640*9880d681SAndroid Build Coastguard Worker MCELFObjectTargetWriter *MOTW =
641*9880d681SAndroid Build Coastguard Worker new MipsELFObjectWriter(Is64Bit, OSABI, Is64Bit, IsLittleEndian);
642*9880d681SAndroid Build Coastguard Worker return createELFObjectWriter(MOTW, OS, IsLittleEndian);
643*9880d681SAndroid Build Coastguard Worker }
644