blob: 44494b12fe2dd5bbe1595710e8e522e74e723225 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsELFObjectWriter.cpp - Mips ELF Writer -------------------------===//
Rafael Espindola1dc45d82011-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
Daniel Sandersa463d312016-05-06 13:49:25 +000010#include <algorithm>
11#include <list>
Akira Hatanaka5ba593f2012-03-28 00:23:33 +000012#include "MCTargetDesc/MipsBaseInfo.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000013#include "MCTargetDesc/MipsFixupKinds.h"
Daniel Sandersa463d312016-05-06 13:49:25 +000014#include "MCTargetDesc/MipsMCExpr.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000015#include "MCTargetDesc/MipsMCTargetDesc.h"
Petar Jovanovic0380d0b2015-04-14 13:23:34 +000016#include "llvm/ADT/STLExtras.h"
Akira Hatanaka5ba593f2012-03-28 00:23:33 +000017#include "llvm/MC/MCAssembler.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000018#include "llvm/MC/MCELFObjectWriter.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCSection.h"
Rafael Espindola95fb9b92015-06-02 20:38:46 +000021#include "llvm/MC/MCSymbolELF.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000022#include "llvm/MC/MCValue.h"
Daniel Sandersa463d312016-05-06 13:49:25 +000023#include "llvm/Support/Debug.h"
Rafael Espindola1dc45d82011-12-22 03:03:17 +000024#include "llvm/Support/ErrorHandling.h"
25
Daniel Sandersa463d312016-05-06 13:49:25 +000026#define DEBUG_TYPE "mips-elf-object-writer"
27
Rafael Espindola1dc45d82011-12-22 03:03:17 +000028using namespace llvm;
29
30namespace {
Daniel Sandersa463d312016-05-06 13:49:25 +000031/// Holds additional information needed by the relocation ordering algorithm.
Petar Jovanovic0380d0b2015-04-14 13:23:34 +000032struct MipsRelocationEntry {
Daniel Sandersa463d312016-05-06 13:49:25 +000033 const ELFRelocationEntry R; ///< The relocation.
34 bool Matched; ///< Is this relocation part of a match.
35
36 MipsRelocationEntry(const ELFRelocationEntry &R) : R(R), Matched(false) {}
37
38 void print(raw_ostream &Out) const {
39 R.print(Out);
40 Out << ", Matched=" << Matched;
41 }
Petar Jovanovic0380d0b2015-04-14 13:23:34 +000042};
43
NAKAMURA Takumi77edc2e2016-05-07 04:51:51 +000044#ifndef NDEBUG
Daniel Sandersa463d312016-05-06 13:49:25 +000045raw_ostream &operator<<(raw_ostream &OS, const MipsRelocationEntry &RHS) {
46 RHS.print(OS);
47 return OS;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000048}
NAKAMURA Takumi77edc2e2016-05-07 04:51:51 +000049#endif
Rafael Espindola1dc45d82011-12-22 03:03:17 +000050
Daniel Sandersa463d312016-05-06 13:49:25 +000051class MipsELFObjectWriter : public MCELFObjectTargetWriter {
52public:
53 MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI, bool _isN64,
54 bool IsLittleEndian);
55
56 ~MipsELFObjectWriter() override;
57
58 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
59 const MCFixup &Fixup, bool IsPCRel) const override;
60 bool needsRelocateWithSymbol(const MCSymbol &Sym,
61 unsigned Type) const override;
62 virtual void sortRelocs(const MCAssembler &Asm,
63 std::vector<ELFRelocationEntry> &Relocs) override;
64};
65
66/// Copy elements in the range [First, Last) to d1 when the predicate is true or
67/// d2 when the predicate is false. This is essentially both std::copy_if and
68/// std::remove_copy_if combined into a single pass.
69template <class InputIt, class OutputIt1, class OutputIt2, class UnaryPredicate>
70std::pair<OutputIt1, OutputIt2> copy_if_else(InputIt First, InputIt Last,
71 OutputIt1 d1, OutputIt2 d2,
72 UnaryPredicate Predicate) {
73 for (InputIt I = First; I != Last; ++I) {
74 if (Predicate(*I)) {
75 *d1 = *I;
76 d1++;
77 } else {
78 *d2 = *I;
79 d2++;
80 }
81 }
82
83 return std::make_pair(d1, d2);
84}
85
86/// The possible results of the Predicate function used by find_best.
87enum FindBestPredicateResult {
88 FindBest_NoMatch = 0, ///< The current element is not a match.
89 FindBest_Match, ///< The current element is a match but better ones are
90 /// possible.
91 FindBest_PerfectMatch, ///< The current element is an unbeatable match.
92};
93
94/// Find the best match in the range [First, Last).
95///
96/// An element matches when Predicate(X) returns FindBest_Match or
97/// FindBest_PerfectMatch. A value of FindBest_PerfectMatch also terminates
98/// the search. BetterThan(A, B) is a comparator that returns true when A is a
99/// better match than B. The return value is the position of the best match.
100///
101/// This is similar to std::find_if but finds the best of multiple possible
102/// matches.
103template <class InputIt, class UnaryPredicate, class Comparator>
104InputIt find_best(InputIt First, InputIt Last, UnaryPredicate Predicate,
105 Comparator BetterThan) {
106 InputIt Best = Last;
107
108 for (InputIt I = First; I != Last; ++I) {
109 unsigned Matched = Predicate(*I);
110 if (Matched != FindBest_NoMatch) {
111 DEBUG(dbgs() << std::distance(First, I) << " is a match (";
112 I->print(dbgs()); dbgs() << ")\n");
113 if (Best == Last || BetterThan(*I, *Best)) {
114 DEBUG(dbgs() << ".. and it beats the last one\n");
115 Best = I;
116 }
117 }
118 if (Matched == FindBest_PerfectMatch) {
119 DEBUG(dbgs() << ".. and it is unbeatable\n");
120 break;
121 }
122 }
123
124 return Best;
125}
126
127/// Determine the low relocation that matches the given relocation.
128/// If the relocation does not need a low relocation then the return value
129/// is ELF::R_MIPS_NONE.
130///
131/// The relocations that need a matching low part are
132/// R_(MIPS|MICROMIPS|MIPS16)_HI16 for all symbols and
133/// R_(MIPS|MICROMIPS|MIPS16)_GOT16 for local symbols only.
134static unsigned getMatchingLoType(const ELFRelocationEntry &Reloc) {
135 unsigned Type = Reloc.Type;
136 if (Type == ELF::R_MIPS_HI16)
137 return ELF::R_MIPS_LO16;
138 if (Type == ELF::R_MICROMIPS_HI16)
139 return ELF::R_MICROMIPS_LO16;
140 if (Type == ELF::R_MIPS16_HI16)
141 return ELF::R_MIPS16_LO16;
142
143 if (Reloc.OriginalSymbol->getBinding() != ELF::STB_LOCAL)
144 return ELF::R_MIPS_NONE;
145
146 if (Type == ELF::R_MIPS_GOT16)
147 return ELF::R_MIPS_LO16;
148 if (Type == ELF::R_MICROMIPS_GOT16)
149 return ELF::R_MICROMIPS_LO16;
150 if (Type == ELF::R_MIPS16_GOT16)
151 return ELF::R_MIPS16_LO16;
152
153 return ELF::R_MIPS_NONE;
154}
155
156/// Determine whether a relocation (X) matches the one given in R.
157///
158/// A relocation matches if:
159/// - It's type matches that of a corresponding low part. This is provided in
160/// MatchingType for efficiency.
161/// - It's based on the same symbol.
162/// - It's offset of greater or equal to that of the one given in R.
163/// It should be noted that this rule assumes the programmer does not use
164/// offsets that exceed the alignment of the symbol. The carry-bit will be
165/// incorrect if this is not true.
166///
167/// A matching relocation is unbeatable if:
168/// - It is not already involved in a match.
169/// - It's offset is exactly that of the one given in R.
Daniel Sanders108823b2016-05-09 15:50:15 +0000170static FindBestPredicateResult isMatchingReloc(const MipsRelocationEntry &X,
171 const ELFRelocationEntry &R,
172 unsigned MatchingType) {
Daniel Sandersa463d312016-05-06 13:49:25 +0000173 if (X.R.Type == MatchingType && X.R.OriginalSymbol == R.OriginalSymbol) {
174 if (!X.Matched &&
175 X.R.OriginalAddend == R.OriginalAddend)
176 return FindBest_PerfectMatch;
177 else if (X.R.OriginalAddend >= R.OriginalAddend)
178 return FindBest_Match;
179 }
180 return FindBest_NoMatch;
181}
182
183/// Determine whether Candidate or PreviousBest is the better match.
184/// The return value is true if Candidate is the better match.
185///
186/// A matching relocation is a better match if:
187/// - It has a smaller addend.
188/// - It is not already involved in a match.
189static bool compareMatchingRelocs(const MipsRelocationEntry &Candidate,
190 const MipsRelocationEntry &PreviousBest) {
191 if (Candidate.R.OriginalAddend != PreviousBest.R.OriginalAddend)
192 return Candidate.R.OriginalAddend < PreviousBest.R.OriginalAddend;
193 return PreviousBest.Matched && !Candidate.Matched;
194}
195
196#ifndef NDEBUG
197/// Print all the relocations.
198template <class Container>
199static void dumpRelocs(const char *Prefix, const Container &Relocs) {
200 for (const auto &R : Relocs)
201 dbgs() << Prefix << R << "\n";
202}
203#endif
204
205} // end anonymous namespace
206
Jack Carter8ad0c272012-06-27 22:28:30 +0000207MipsELFObjectWriter::MipsELFObjectWriter(bool _is64Bit, uint8_t OSABI,
Akira Hatanaka111174b2012-08-17 21:28:04 +0000208 bool _isN64, bool IsLittleEndian)
Simon Atanasyanc99ce682015-03-24 12:24:56 +0000209 : MCELFObjectTargetWriter(_is64Bit, OSABI, ELF::EM_MIPS,
210 /*HasRelocationAddend*/ _isN64,
211 /*IsN64*/ _isN64) {}
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000212
213MipsELFObjectWriter::~MipsELFObjectWriter() {}
214
Rafael Espindola8340f942016-01-13 22:56:57 +0000215unsigned MipsELFObjectWriter::getRelocType(MCContext &Ctx,
216 const MCValue &Target,
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000217 const MCFixup &Fixup,
Rafael Espindolac03f44c2014-03-27 20:49:35 +0000218 bool IsPCRel) const {
Daniel Sanders58405d82015-06-16 13:46:26 +0000219 // Determine the type of the relocation.
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000220 unsigned Kind = (unsigned)Fixup.getKind();
221
222 switch (Kind) {
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000223 case Mips::fixup_Mips_NONE:
224 return ELF::R_MIPS_NONE;
Daniel Sanders58405d82015-06-16 13:46:26 +0000225 case Mips::fixup_Mips_16:
226 case FK_Data_2:
227 return IsPCRel ? ELF::R_MIPS_PC16 : ELF::R_MIPS_16;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000228 case Mips::fixup_Mips_32:
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000229 case FK_Data_4:
Rafael Espindola7f7caf92015-06-01 15:10:51 +0000230 return IsPCRel ? ELF::R_MIPS_PC32 : ELF::R_MIPS_32;
Daniel Sanders58405d82015-06-16 13:46:26 +0000231 }
232
233 if (IsPCRel) {
234 switch (Kind) {
235 case Mips::fixup_Mips_Branch_PCRel:
236 case Mips::fixup_Mips_PC16:
237 return ELF::R_MIPS_PC16;
238 case Mips::fixup_MICROMIPS_PC7_S1:
239 return ELF::R_MICROMIPS_PC7_S1;
240 case Mips::fixup_MICROMIPS_PC10_S1:
241 return ELF::R_MICROMIPS_PC10_S1;
242 case Mips::fixup_MICROMIPS_PC16_S1:
243 return ELF::R_MICROMIPS_PC16_S1;
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000244 case Mips::fixup_MICROMIPS_PC26_S1:
245 return ELF::R_MICROMIPS_PC26_S1;
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000246 case Mips::fixup_MICROMIPS_PC19_S2:
247 return ELF::R_MICROMIPS_PC19_S2;
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000248 case Mips::fixup_MICROMIPS_PC18_S3:
249 return ELF::R_MICROMIPS_PC18_S3;
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000250 case Mips::fixup_MICROMIPS_PC21_S1:
251 return ELF::R_MICROMIPS_PC21_S1;
Daniel Sanders58405d82015-06-16 13:46:26 +0000252 case Mips::fixup_MIPS_PC19_S2:
253 return ELF::R_MIPS_PC19_S2;
254 case Mips::fixup_MIPS_PC18_S3:
255 return ELF::R_MIPS_PC18_S3;
256 case Mips::fixup_MIPS_PC21_S2:
257 return ELF::R_MIPS_PC21_S2;
258 case Mips::fixup_MIPS_PC26_S2:
259 return ELF::R_MIPS_PC26_S2;
260 case Mips::fixup_MIPS_PCHI16:
261 return ELF::R_MIPS_PCHI16;
262 case Mips::fixup_MIPS_PCLO16:
263 return ELF::R_MIPS_PCLO16;
264 }
265
266 llvm_unreachable("invalid PC-relative fixup kind!");
267 }
268
269 switch (Kind) {
Daniel Sanders60f1db02015-03-13 12:45:09 +0000270 case Mips::fixup_Mips_64:
Jack Carter4c583812012-08-07 00:01:14 +0000271 case FK_Data_8:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000272 return ELF::R_MIPS_64;
Simon Atanasyaneb9ed612016-08-22 16:18:42 +0000273 case FK_DTPRel_4:
274 return ELF::R_MIPS_TLS_DTPREL32;
275 case FK_DTPRel_8:
276 return ELF::R_MIPS_TLS_DTPREL64;
277 case FK_TPRel_4:
278 return ELF::R_MIPS_TLS_TPREL32;
279 case FK_TPRel_8:
280 return ELF::R_MIPS_TLS_TPREL64;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000281 case FK_GPRel_4:
Jack Carterf2385102013-01-15 01:08:02 +0000282 if (isN64()) {
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000283 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterf2385102013-01-15 01:08:02 +0000284 Type = setRType((unsigned)ELF::R_MIPS_GPREL32, Type);
285 Type = setRType2((unsigned)ELF::R_MIPS_64, Type);
286 Type = setRType3((unsigned)ELF::R_MIPS_NONE, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000287 return Type;
Jack Carterf2385102013-01-15 01:08:02 +0000288 }
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000289 return ELF::R_MIPS_GPREL32;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000290 case Mips::fixup_Mips_GPREL16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000291 return ELF::R_MIPS_GPREL16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000292 case Mips::fixup_Mips_26:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000293 return ELF::R_MIPS_26;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000294 case Mips::fixup_Mips_CALL16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000295 return ELF::R_MIPS_CALL16;
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000296 case Mips::fixup_Mips_GOT:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000297 return ELF::R_MIPS_GOT16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000298 case Mips::fixup_Mips_HI16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000299 return ELF::R_MIPS_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000300 case Mips::fixup_Mips_LO16:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000301 return ELF::R_MIPS_LO16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000302 case Mips::fixup_Mips_TLSGD:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000303 return ELF::R_MIPS_TLS_GD;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000304 case Mips::fixup_Mips_GOTTPREL:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000305 return ELF::R_MIPS_TLS_GOTTPREL;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000306 case Mips::fixup_Mips_TPREL_HI:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000307 return ELF::R_MIPS_TLS_TPREL_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000308 case Mips::fixup_Mips_TPREL_LO:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000309 return ELF::R_MIPS_TLS_TPREL_LO16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000310 case Mips::fixup_Mips_TLSLDM:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000311 return ELF::R_MIPS_TLS_LDM;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000312 case Mips::fixup_Mips_DTPREL_HI:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000313 return ELF::R_MIPS_TLS_DTPREL_HI16;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000314 case Mips::fixup_Mips_DTPREL_LO:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000315 return ELF::R_MIPS_TLS_DTPREL_LO16;
Jack Carterb9f9de92012-06-27 22:48:25 +0000316 case Mips::fixup_Mips_GOT_PAGE:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000317 return ELF::R_MIPS_GOT_PAGE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000318 case Mips::fixup_Mips_GOT_OFST:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000319 return ELF::R_MIPS_GOT_OFST;
Jack Carter5ddcfda2012-07-13 19:15:47 +0000320 case Mips::fixup_Mips_GOT_DISP:
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000321 return ELF::R_MIPS_GOT_DISP;
322 case Mips::fixup_Mips_GPOFF_HI: {
323 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000324 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
325 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
326 Type = setRType3((unsigned)ELF::R_MIPS_HI16, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000327 return Type;
328 }
329 case Mips::fixup_Mips_GPOFF_LO: {
330 unsigned Type = (unsigned)ELF::R_MIPS_NONE;
Jack Carterb9f9de92012-06-27 22:48:25 +0000331 Type = setRType((unsigned)ELF::R_MIPS_GPREL16, Type);
332 Type = setRType2((unsigned)ELF::R_MIPS_SUB, Type);
333 Type = setRType3((unsigned)ELF::R_MIPS_LO16, Type);
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000334 return Type;
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000335 }
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000336 case Mips::fixup_Mips_HIGHER:
337 return ELF::R_MIPS_HIGHER;
338 case Mips::fixup_Mips_HIGHEST:
339 return ELF::R_MIPS_HIGHEST;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000340 case Mips::fixup_Mips_SUB:
341 return ELF::R_MIPS_SUB;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000342 case Mips::fixup_Mips_GOT_HI16:
343 return ELF::R_MIPS_GOT_HI16;
344 case Mips::fixup_Mips_GOT_LO16:
345 return ELF::R_MIPS_GOT_LO16;
346 case Mips::fixup_Mips_CALL_HI16:
347 return ELF::R_MIPS_CALL_HI16;
348 case Mips::fixup_Mips_CALL_LO16:
349 return ELF::R_MIPS_CALL_LO16;
350 case Mips::fixup_MICROMIPS_26_S1:
351 return ELF::R_MICROMIPS_26_S1;
352 case Mips::fixup_MICROMIPS_HI16:
353 return ELF::R_MICROMIPS_HI16;
354 case Mips::fixup_MICROMIPS_LO16:
355 return ELF::R_MICROMIPS_LO16;
356 case Mips::fixup_MICROMIPS_GOT16:
357 return ELF::R_MICROMIPS_GOT16;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000358 case Mips::fixup_MICROMIPS_CALL16:
359 return ELF::R_MICROMIPS_CALL16;
360 case Mips::fixup_MICROMIPS_GOT_DISP:
361 return ELF::R_MICROMIPS_GOT_DISP;
362 case Mips::fixup_MICROMIPS_GOT_PAGE:
363 return ELF::R_MICROMIPS_GOT_PAGE;
364 case Mips::fixup_MICROMIPS_GOT_OFST:
365 return ELF::R_MICROMIPS_GOT_OFST;
366 case Mips::fixup_MICROMIPS_TLS_GD:
367 return ELF::R_MICROMIPS_TLS_GD;
368 case Mips::fixup_MICROMIPS_TLS_LDM:
369 return ELF::R_MICROMIPS_TLS_LDM;
370 case Mips::fixup_MICROMIPS_TLS_DTPREL_HI16:
371 return ELF::R_MICROMIPS_TLS_DTPREL_HI16;
372 case Mips::fixup_MICROMIPS_TLS_DTPREL_LO16:
373 return ELF::R_MICROMIPS_TLS_DTPREL_LO16;
374 case Mips::fixup_MICROMIPS_TLS_TPREL_HI16:
375 return ELF::R_MICROMIPS_TLS_TPREL_HI16;
376 case Mips::fixup_MICROMIPS_TLS_TPREL_LO16:
377 return ELF::R_MICROMIPS_TLS_TPREL_LO16;
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000378 case Mips::fixup_MICROMIPS_SUB:
379 return ELF::R_MICROMIPS_SUB;
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000380 }
Daniel Sanders58405d82015-06-16 13:46:26 +0000381
Rafael Espindolaccb8d1a2015-06-01 14:58:29 +0000382 llvm_unreachable("invalid fixup kind!");
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000383}
384
Daniel Sandersa463d312016-05-06 13:49:25 +0000385/// Sort relocation table entries by offset except where another order is
386/// required by the MIPS ABI.
387///
388/// MIPS has a few relocations that have an AHL component in the expression used
389/// to evaluate them. This AHL component is an addend with the same number of
390/// bits as a symbol value but not all of our ABI's are able to supply a
391/// sufficiently sized addend in a single relocation.
392///
393/// The O32 ABI for example, uses REL relocations which store the addend in the
394/// section data. All the relocations with AHL components affect 16-bit fields
395/// so the addend for a single relocation is limited to 16-bit. This ABI
396/// resolves the limitation by linking relocations (e.g. R_MIPS_HI16 and
397/// R_MIPS_LO16) and distributing the addend between the linked relocations. The
398/// ABI mandates that such relocations must be next to each other in a
399/// particular order (e.g. R_MIPS_HI16 must be immediately followed by a
400/// matching R_MIPS_LO16) but the rule is less strict in practice.
401///
402/// The de facto standard is lenient in the following ways:
403/// - 'Immediately following' does not refer to the next relocation entry but
404/// the next matching relocation.
405/// - There may be multiple high parts relocations for one low part relocation.
406/// - There may be multiple low part relocations for one high part relocation.
407/// - The AHL addend in each part does not have to be exactly equal as long as
408/// the difference does not affect the carry bit from bit 15 into 16. This is
409/// to allow, for example, the use of %lo(foo) and %lo(foo+4) when loading
410/// both halves of a long long.
411///
412/// See getMatchingLoType() for a description of which high part relocations
413/// match which low part relocations. One particular thing to note is that
414/// R_MIPS_GOT16 and similar only have AHL addends if they refer to local
415/// symbols.
416///
417/// It should also be noted that this function is not affected by whether
418/// the symbol was kept or rewritten into a section-relative equivalent. We
419/// always match using the expressions from the source.
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000420void MipsELFObjectWriter::sortRelocs(const MCAssembler &Asm,
421 std::vector<ELFRelocationEntry> &Relocs) {
Simon Dardis8fe36cd2016-12-05 12:55:19 +0000422
423 // We do not need to sort the relocation table for RELA relocations which
424 // N32/N64 uses as the relocation addend contains the value we require,
425 // rather than it being split across a pair of relocations.
426 if (hasRelocationAddend())
427 return;
428
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000429 if (Relocs.size() < 2)
430 return;
431
Daniel Sandersa463d312016-05-06 13:49:25 +0000432 // Sort relocations by the address they are applied to.
433 std::sort(Relocs.begin(), Relocs.end(),
434 [](const ELFRelocationEntry &A, const ELFRelocationEntry &B) {
435 return A.Offset < B.Offset;
436 });
Rafael Espindolaf44db242015-12-17 16:22:06 +0000437
Daniel Sandersa463d312016-05-06 13:49:25 +0000438 std::list<MipsRelocationEntry> Sorted;
439 std::list<ELFRelocationEntry> Remainder;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000440
Daniel Sandersa463d312016-05-06 13:49:25 +0000441 DEBUG(dumpRelocs("R: ", Relocs));
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000442
Daniel Sandersa463d312016-05-06 13:49:25 +0000443 // Separate the movable relocations (AHL relocations using the high bits) from
444 // the immobile relocations (everything else). This does not preserve high/low
445 // matches that already existed in the input.
446 copy_if_else(Relocs.begin(), Relocs.end(), std::back_inserter(Remainder),
447 std::back_inserter(Sorted), [](const ELFRelocationEntry &Reloc) {
448 return getMatchingLoType(Reloc) != ELF::R_MIPS_NONE;
449 });
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000450
Daniel Sandersa463d312016-05-06 13:49:25 +0000451 for (auto &R : Remainder) {
452 DEBUG(dbgs() << "Matching: " << R << "\n");
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000453
Daniel Sandersa463d312016-05-06 13:49:25 +0000454 unsigned MatchingType = getMatchingLoType(R);
455 assert(MatchingType != ELF::R_MIPS_NONE &&
456 "Wrong list for reloc that doesn't need a match");
457
458 // Find the best matching relocation for the current high part.
459 // See isMatchingReloc for a description of a matching relocation and
460 // compareMatchingRelocs for a description of what 'best' means.
461 auto InsertionPoint =
462 find_best(Sorted.begin(), Sorted.end(),
463 [&R, &MatchingType](const MipsRelocationEntry &X) {
464 return isMatchingReloc(X, R, MatchingType);
465 },
466 compareMatchingRelocs);
467
468 // If we matched then insert the high part in front of the match and mark
469 // both relocations as being involved in a match. We only mark the high
470 // part for cosmetic reasons in the debug output.
471 //
472 // If we failed to find a match then the high part is orphaned. This is not
473 // permitted since the relocation cannot be evaluated without knowing the
474 // carry-in. We can sometimes handle this using a matching low part that is
475 // already used in a match but we already cover that case in
476 // isMatchingReloc and compareMatchingRelocs. For the remaining cases we
477 // should insert the high part at the end of the list. This will cause the
478 // linker to fail but the alternative is to cause the linker to bind the
479 // high part to a semi-matching low part and silently calculate the wrong
480 // value. Unfortunately we have no means to warn the user that we did this
481 // so leave it up to the linker to complain about it.
482 if (InsertionPoint != Sorted.end())
483 InsertionPoint->Matched = true;
484 Sorted.insert(InsertionPoint, R)->Matched = true;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000485 }
486
Daniel Sandersa463d312016-05-06 13:49:25 +0000487 DEBUG(dumpRelocs("S: ", Sorted));
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000488
Daniel Sandersa463d312016-05-06 13:49:25 +0000489 assert(Relocs.size() == Sorted.size() && "Some relocs were not consumed");
490
491 // Overwrite the original vector with the sorted elements. The caller expects
492 // them in reverse order.
493 unsigned CopyTo = 0;
494 for (const auto &R : reverse(Sorted))
495 Relocs[CopyTo++] = R.R;
Petar Jovanovic0380d0b2015-04-14 13:23:34 +0000496}
497
Rafael Espindolaece40ca2015-05-29 18:26:09 +0000498bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
499 unsigned Type) const {
Daniel Sanders55d38332016-05-12 10:55:00 +0000500 // If it's a compound relocation for N64 then we need the relocation if any
501 // sub-relocation needs it.
Daniel Sanders2225d942016-05-10 12:17:04 +0000502 if (!isUInt<8>(Type))
Daniel Sanders55d38332016-05-12 10:55:00 +0000503 return needsRelocateWithSymbol(Sym, Type & 0xff) ||
504 needsRelocateWithSymbol(Sym, (Type >> 8) & 0xff) ||
505 needsRelocateWithSymbol(Sym, (Type >> 16) & 0xff);
Daniel Sanders2225d942016-05-10 12:17:04 +0000506
Rafael Espindola5904e122014-03-29 06:26:49 +0000507 switch (Type) {
508 default:
Daniel Sanders2225d942016-05-10 12:17:04 +0000509 errs() << Type << "\n";
510 llvm_unreachable("Unexpected relocation");
Rafael Espindola5904e122014-03-29 06:26:49 +0000511 return true;
512
Daniel Sanders2225d942016-05-10 12:17:04 +0000513 // This relocation doesn't affect the section data.
514 case ELF::R_MIPS_NONE:
515 return false;
516
Daniel Sanders3d000562016-05-09 10:21:14 +0000517 // On REL ABI's (e.g. O32), these relocations form pairs. The pairing is done
518 // by the static linker by matching the symbol and offset.
519 // We only see one relocation at a time but it's still safe to relocate with
520 // the section so long as both relocations make the same decision.
521 //
522 // Some older linkers may require the symbol for particular cases. Such cases
523 // are not supported yet but can be added as required.
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000524 case ELF::R_MIPS_GOT16:
525 case ELF::R_MIPS16_GOT16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000526 case ELF::R_MICROMIPS_GOT16:
Simon Dardisca74dd72017-01-27 11:36:52 +0000527 case ELF::R_MIPS_HIGHER:
528 case ELF::R_MIPS_HIGHEST:
Rafael Espindola5904e122014-03-29 06:26:49 +0000529 case ELF::R_MIPS_HI16:
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000530 case ELF::R_MIPS16_HI16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000531 case ELF::R_MICROMIPS_HI16:
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000532 case ELF::R_MIPS_LO16:
533 case ELF::R_MIPS16_LO16:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000534 case ELF::R_MICROMIPS_LO16:
Daniel Sanders45533b42016-05-11 15:44:23 +0000535 // FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
536 // we neglect to handle the adjustment to the LSB of the addend that
537 // it causes in applyFixup() and similar.
538 if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
539 return true;
Daniel Sanders3d000562016-05-09 10:21:14 +0000540 return false;
Rafael Espindolaee1c3422014-03-31 19:00:23 +0000541
Daniel Sanders38784122016-07-19 10:58:06 +0000542 case ELF::R_MIPS_GOT_PAGE:
543 case ELF::R_MICROMIPS_GOT_PAGE:
544 case ELF::R_MIPS_GOT_OFST:
545 case ELF::R_MICROMIPS_GOT_OFST:
Scott Egertond1aeb052016-02-15 16:11:51 +0000546 case ELF::R_MIPS_16:
Rafael Espindola5904e122014-03-29 06:26:49 +0000547 case ELF::R_MIPS_32:
Daniel Sanders5fb391c2016-05-12 13:39:13 +0000548 case ELF::R_MIPS_GPREL32:
Rafael Espindola8c006ee2015-06-04 05:59:23 +0000549 if (cast<MCSymbolELF>(Sym).getOther() & ELF::STO_MIPS_MICROMIPS)
Zoran Jovanovic10646912014-12-30 22:04:16 +0000550 return true;
Justin Bognerb03fd122016-08-17 05:10:15 +0000551 LLVM_FALLTHROUGH;
Zoran Jovanovic10646912014-12-30 22:04:16 +0000552 case ELF::R_MIPS_26:
Rafael Espindola5904e122014-03-29 06:26:49 +0000553 case ELF::R_MIPS_64:
554 case ELF::R_MIPS_GPREL16:
Daniel Sanders3d000562016-05-09 10:21:14 +0000555 case ELF::R_MIPS_PC16:
Daniel Sanders55d38332016-05-12 10:55:00 +0000556 case ELF::R_MIPS_SUB:
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000557 return false;
Daniel Sanders2225d942016-05-10 12:17:04 +0000558
559 // FIXME: Many of these relocations should probably return false but this
560 // hasn't been confirmed to be safe yet.
561 case ELF::R_MIPS_REL32:
562 case ELF::R_MIPS_LITERAL:
563 case ELF::R_MIPS_CALL16:
564 case ELF::R_MIPS_SHIFT5:
565 case ELF::R_MIPS_SHIFT6:
566 case ELF::R_MIPS_GOT_DISP:
Daniel Sanders2225d942016-05-10 12:17:04 +0000567 case ELF::R_MIPS_GOT_HI16:
568 case ELF::R_MIPS_GOT_LO16:
Daniel Sanders2225d942016-05-10 12:17:04 +0000569 case ELF::R_MIPS_INSERT_A:
570 case ELF::R_MIPS_INSERT_B:
571 case ELF::R_MIPS_DELETE:
Daniel Sanders2225d942016-05-10 12:17:04 +0000572 case ELF::R_MIPS_CALL_HI16:
573 case ELF::R_MIPS_CALL_LO16:
574 case ELF::R_MIPS_SCN_DISP:
575 case ELF::R_MIPS_REL16:
576 case ELF::R_MIPS_ADD_IMMEDIATE:
577 case ELF::R_MIPS_PJUMP:
578 case ELF::R_MIPS_RELGOT:
579 case ELF::R_MIPS_JALR:
580 case ELF::R_MIPS_TLS_DTPMOD32:
581 case ELF::R_MIPS_TLS_DTPREL32:
582 case ELF::R_MIPS_TLS_DTPMOD64:
583 case ELF::R_MIPS_TLS_DTPREL64:
584 case ELF::R_MIPS_TLS_GD:
585 case ELF::R_MIPS_TLS_LDM:
586 case ELF::R_MIPS_TLS_DTPREL_HI16:
587 case ELF::R_MIPS_TLS_DTPREL_LO16:
588 case ELF::R_MIPS_TLS_GOTTPREL:
589 case ELF::R_MIPS_TLS_TPREL32:
590 case ELF::R_MIPS_TLS_TPREL64:
591 case ELF::R_MIPS_TLS_TPREL_HI16:
592 case ELF::R_MIPS_TLS_TPREL_LO16:
593 case ELF::R_MIPS_GLOB_DAT:
594 case ELF::R_MIPS_PC21_S2:
595 case ELF::R_MIPS_PC26_S2:
596 case ELF::R_MIPS_PC18_S3:
597 case ELF::R_MIPS_PC19_S2:
598 case ELF::R_MIPS_PCHI16:
599 case ELF::R_MIPS_PCLO16:
600 case ELF::R_MIPS_COPY:
601 case ELF::R_MIPS_JUMP_SLOT:
602 case ELF::R_MIPS_NUM:
603 case ELF::R_MIPS_PC32:
604 case ELF::R_MIPS_EH:
605 case ELF::R_MICROMIPS_26_S1:
606 case ELF::R_MICROMIPS_GPREL16:
607 case ELF::R_MICROMIPS_LITERAL:
608 case ELF::R_MICROMIPS_PC7_S1:
609 case ELF::R_MICROMIPS_PC10_S1:
610 case ELF::R_MICROMIPS_PC16_S1:
611 case ELF::R_MICROMIPS_CALL16:
612 case ELF::R_MICROMIPS_GOT_DISP:
Daniel Sanders2225d942016-05-10 12:17:04 +0000613 case ELF::R_MICROMIPS_GOT_HI16:
614 case ELF::R_MICROMIPS_GOT_LO16:
615 case ELF::R_MICROMIPS_SUB:
616 case ELF::R_MICROMIPS_HIGHER:
617 case ELF::R_MICROMIPS_HIGHEST:
618 case ELF::R_MICROMIPS_CALL_HI16:
619 case ELF::R_MICROMIPS_CALL_LO16:
620 case ELF::R_MICROMIPS_SCN_DISP:
621 case ELF::R_MICROMIPS_JALR:
622 case ELF::R_MICROMIPS_HI0_LO16:
623 case ELF::R_MICROMIPS_TLS_GD:
624 case ELF::R_MICROMIPS_TLS_LDM:
625 case ELF::R_MICROMIPS_TLS_DTPREL_HI16:
626 case ELF::R_MICROMIPS_TLS_DTPREL_LO16:
627 case ELF::R_MICROMIPS_TLS_GOTTPREL:
628 case ELF::R_MICROMIPS_TLS_TPREL_HI16:
629 case ELF::R_MICROMIPS_TLS_TPREL_LO16:
630 case ELF::R_MICROMIPS_GPREL7_S2:
631 case ELF::R_MICROMIPS_PC23_S2:
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000632 case ELF::R_MICROMIPS_PC21_S1:
Daniel Sanders2225d942016-05-10 12:17:04 +0000633 case ELF::R_MICROMIPS_PC26_S1:
634 case ELF::R_MICROMIPS_PC18_S3:
635 case ELF::R_MICROMIPS_PC19_S2:
636 return true;
637
638 // FIXME: Many of these should probably return false but MIPS16 isn't
639 // supported by the integrated assembler.
640 case ELF::R_MIPS16_26:
641 case ELF::R_MIPS16_GPREL:
642 case ELF::R_MIPS16_CALL16:
643 case ELF::R_MIPS16_TLS_GD:
644 case ELF::R_MIPS16_TLS_LDM:
645 case ELF::R_MIPS16_TLS_DTPREL_HI16:
646 case ELF::R_MIPS16_TLS_DTPREL_LO16:
647 case ELF::R_MIPS16_TLS_GOTTPREL:
648 case ELF::R_MIPS16_TLS_TPREL_HI16:
649 case ELF::R_MIPS16_TLS_TPREL_LO16:
650 llvm_unreachable("Unsupported MIPS16 relocation");
651 return true;
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000652 }
Akira Hatanaka5ba593f2012-03-28 00:23:33 +0000653}
654
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000655MCObjectWriter *llvm::createMipsELFObjectWriter(raw_pwrite_stream &OS,
656 uint8_t OSABI,
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000657 bool IsLittleEndian,
658 bool Is64Bit) {
Simon Atanasyanc99ce682015-03-24 12:24:56 +0000659 MCELFObjectTargetWriter *MOTW =
660 new MipsELFObjectWriter(Is64Bit, OSABI, Is64Bit, IsLittleEndian);
Rafael Espindola1dc45d82011-12-22 03:03:17 +0000661 return createELFObjectWriter(MOTW, OS, IsLittleEndian);
662}